1*d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 2d3123599STom Lendacky /* 3d3123599STom Lendacky * AMD Cryptographic Coprocessor (CCP) crypto API support 4d3123599STom Lendacky * 568cc652fSGary R Hook * Copyright (C) 2013,2017 Advanced Micro Devices, Inc. 6d3123599STom Lendacky * 7d3123599STom Lendacky * Author: Tom Lendacky <thomas.lendacky@amd.com> 8d3123599STom Lendacky */ 9d3123599STom Lendacky 10d3123599STom Lendacky #ifndef __CCP_CRYPTO_H__ 11d3123599STom Lendacky #define __CCP_CRYPTO_H__ 12d3123599STom Lendacky 13d3123599STom Lendacky #include <linux/list.h> 14d3123599STom Lendacky #include <linux/wait.h> 15d3123599STom Lendacky #include <linux/pci.h> 16d3123599STom Lendacky #include <linux/ccp.h> 17d3123599STom Lendacky #include <crypto/algapi.h> 18d3123599STom Lendacky #include <crypto/aes.h> 1936cf515bSGary R Hook #include <crypto/internal/aead.h> 2036cf515bSGary R Hook #include <crypto/aead.h> 21d3123599STom Lendacky #include <crypto/ctr.h> 22d3123599STom Lendacky #include <crypto/hash.h> 23d3123599STom Lendacky #include <crypto/sha.h> 24ceeec0afSGary R Hook #include <crypto/akcipher.h> 25ceeec0afSGary R Hook #include <crypto/internal/rsa.h> 26d3123599STom Lendacky 27990672d4SGary R Hook #define CCP_LOG_LEVEL KERN_INFO 28990672d4SGary R Hook 29d3123599STom Lendacky #define CCP_CRA_PRIORITY 300 30d3123599STom Lendacky 31d3123599STom Lendacky struct ccp_crypto_ablkcipher_alg { 32d3123599STom Lendacky struct list_head entry; 33d3123599STom Lendacky 34d3123599STom Lendacky u32 mode; 35d3123599STom Lendacky 36d3123599STom Lendacky struct crypto_alg alg; 37d3123599STom Lendacky }; 38d3123599STom Lendacky 3936cf515bSGary R Hook struct ccp_crypto_aead { 4036cf515bSGary R Hook struct list_head entry; 4136cf515bSGary R Hook 4236cf515bSGary R Hook u32 mode; 4336cf515bSGary R Hook 4436cf515bSGary R Hook struct aead_alg alg; 4536cf515bSGary R Hook }; 4636cf515bSGary R Hook 47d3123599STom Lendacky struct ccp_crypto_ahash_alg { 48d3123599STom Lendacky struct list_head entry; 49d3123599STom Lendacky 506f0be9b2STom Lendacky const __be32 *init; 51d3123599STom Lendacky u32 type; 52d3123599STom Lendacky u32 mode; 53d3123599STom Lendacky 54d3123599STom Lendacky /* Child algorithm used for HMAC, CMAC, etc */ 55d3123599STom Lendacky char child_alg[CRYPTO_MAX_ALG_NAME]; 56d3123599STom Lendacky 57d3123599STom Lendacky struct ahash_alg alg; 58d3123599STom Lendacky }; 59d3123599STom Lendacky 60ceeec0afSGary R Hook struct ccp_crypto_akcipher_alg { 61ceeec0afSGary R Hook struct list_head entry; 62ceeec0afSGary R Hook 63ceeec0afSGary R Hook struct akcipher_alg alg; 64ceeec0afSGary R Hook }; 65ceeec0afSGary R Hook 66d3123599STom Lendacky static inline struct ccp_crypto_ablkcipher_alg * 67d3123599STom Lendacky ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm) 68d3123599STom Lendacky { 69d3123599STom Lendacky struct crypto_alg *alg = tfm->__crt_alg; 70d3123599STom Lendacky 71d3123599STom Lendacky return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg); 72d3123599STom Lendacky } 73d3123599STom Lendacky 74d3123599STom Lendacky static inline struct ccp_crypto_ahash_alg * 75d3123599STom Lendacky ccp_crypto_ahash_alg(struct crypto_tfm *tfm) 76d3123599STom Lendacky { 77d3123599STom Lendacky struct crypto_alg *alg = tfm->__crt_alg; 78d3123599STom Lendacky struct ahash_alg *ahash_alg; 79d3123599STom Lendacky 80d3123599STom Lendacky ahash_alg = container_of(alg, struct ahash_alg, halg.base); 81d3123599STom Lendacky 82d3123599STom Lendacky return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg); 83d3123599STom Lendacky } 84d3123599STom Lendacky 85d3123599STom Lendacky /***** AES related defines *****/ 86d3123599STom Lendacky struct ccp_aes_ctx { 87d3123599STom Lendacky /* Fallback cipher for XTS with unsupported unit sizes */ 887f28615dSKees Cook struct crypto_sync_skcipher *tfm_skcipher; 89d3123599STom Lendacky 90d3123599STom Lendacky /* Cipher used to generate CMAC K1/K2 keys */ 91d3123599STom Lendacky struct crypto_cipher *tfm_cipher; 92d3123599STom Lendacky 93d3123599STom Lendacky enum ccp_engine engine; 94d3123599STom Lendacky enum ccp_aes_type type; 95d3123599STom Lendacky enum ccp_aes_mode mode; 96d3123599STom Lendacky 97d3123599STom Lendacky struct scatterlist key_sg; 98d3123599STom Lendacky unsigned int key_len; 995060ffc9SGary R Hook u8 key[AES_MAX_KEY_SIZE * 2]; 100d3123599STom Lendacky 101d3123599STom Lendacky u8 nonce[CTR_RFC3686_NONCE_SIZE]; 102d3123599STom Lendacky 103d3123599STom Lendacky /* CMAC key structures */ 104d3123599STom Lendacky struct scatterlist k1_sg; 105d3123599STom Lendacky struct scatterlist k2_sg; 106d3123599STom Lendacky unsigned int kn_len; 107d3123599STom Lendacky u8 k1[AES_BLOCK_SIZE]; 108d3123599STom Lendacky u8 k2[AES_BLOCK_SIZE]; 109d3123599STom Lendacky }; 110d3123599STom Lendacky 111d3123599STom Lendacky struct ccp_aes_req_ctx { 112d3123599STom Lendacky struct scatterlist iv_sg; 113d3123599STom Lendacky u8 iv[AES_BLOCK_SIZE]; 114d3123599STom Lendacky 11536cf515bSGary R Hook struct scatterlist tag_sg; 11636cf515bSGary R Hook u8 tag[AES_BLOCK_SIZE]; 11736cf515bSGary R Hook 118d3123599STom Lendacky /* Fields used for RFC3686 requests */ 119d3123599STom Lendacky u8 *rfc3686_info; 120d3123599STom Lendacky u8 rfc3686_iv[AES_BLOCK_SIZE]; 121d3123599STom Lendacky 122d3123599STom Lendacky struct ccp_cmd cmd; 123d3123599STom Lendacky }; 124d3123599STom Lendacky 125d3123599STom Lendacky struct ccp_aes_cmac_req_ctx { 126d3123599STom Lendacky unsigned int null_msg; 127d3123599STom Lendacky unsigned int final; 128d3123599STom Lendacky 12981a59f00STom Lendacky struct scatterlist *src; 13081a59f00STom Lendacky unsigned int nbytes; 13181a59f00STom Lendacky 13281a59f00STom Lendacky u64 hash_cnt; 133d3123599STom Lendacky unsigned int hash_rem; 134d3123599STom Lendacky 135d3123599STom Lendacky struct sg_table data_sg; 136d3123599STom Lendacky 137d3123599STom Lendacky struct scatterlist iv_sg; 138d3123599STom Lendacky u8 iv[AES_BLOCK_SIZE]; 139d3123599STom Lendacky 140d3123599STom Lendacky struct scatterlist buf_sg; 141d3123599STom Lendacky unsigned int buf_count; 142d3123599STom Lendacky u8 buf[AES_BLOCK_SIZE]; 143d3123599STom Lendacky 144d3123599STom Lendacky struct scatterlist pad_sg; 145d3123599STom Lendacky unsigned int pad_count; 146d3123599STom Lendacky u8 pad[AES_BLOCK_SIZE]; 147d3123599STom Lendacky 148d3123599STom Lendacky struct ccp_cmd cmd; 149d3123599STom Lendacky }; 150d3123599STom Lendacky 151d1662165STom Lendacky struct ccp_aes_cmac_exp_ctx { 152d1662165STom Lendacky unsigned int null_msg; 153d1662165STom Lendacky 154d1662165STom Lendacky u8 iv[AES_BLOCK_SIZE]; 155d1662165STom Lendacky 156d1662165STom Lendacky unsigned int buf_count; 157d1662165STom Lendacky u8 buf[AES_BLOCK_SIZE]; 158d1662165STom Lendacky }; 159d1662165STom Lendacky 160990672d4SGary R Hook /***** 3DES related defines *****/ 161990672d4SGary R Hook struct ccp_des3_ctx { 162990672d4SGary R Hook enum ccp_engine engine; 163990672d4SGary R Hook enum ccp_des3_type type; 164990672d4SGary R Hook enum ccp_des3_mode mode; 165990672d4SGary R Hook 166990672d4SGary R Hook struct scatterlist key_sg; 167990672d4SGary R Hook unsigned int key_len; 168990672d4SGary R Hook u8 key[AES_MAX_KEY_SIZE]; 169990672d4SGary R Hook }; 170990672d4SGary R Hook 171990672d4SGary R Hook struct ccp_des3_req_ctx { 172990672d4SGary R Hook struct scatterlist iv_sg; 173990672d4SGary R Hook u8 iv[AES_BLOCK_SIZE]; 174990672d4SGary R Hook 175990672d4SGary R Hook struct ccp_cmd cmd; 176990672d4SGary R Hook }; 177990672d4SGary R Hook 178ccebcf3fSGary R Hook /* SHA-related defines 179ccebcf3fSGary R Hook * These values must be large enough to accommodate any variant 180ccebcf3fSGary R Hook */ 181ccebcf3fSGary R Hook #define MAX_SHA_CONTEXT_SIZE SHA512_DIGEST_SIZE 182ccebcf3fSGary R Hook #define MAX_SHA_BLOCK_SIZE SHA512_BLOCK_SIZE 183d3123599STom Lendacky 184d3123599STom Lendacky struct ccp_sha_ctx { 185c11baa02STom Lendacky struct scatterlist opad_sg; 186c11baa02STom Lendacky unsigned int opad_count; 187c11baa02STom Lendacky 188d3123599STom Lendacky unsigned int key_len; 189d3123599STom Lendacky u8 key[MAX_SHA_BLOCK_SIZE]; 190d3123599STom Lendacky u8 ipad[MAX_SHA_BLOCK_SIZE]; 191d3123599STom Lendacky u8 opad[MAX_SHA_BLOCK_SIZE]; 192c11baa02STom Lendacky struct crypto_shash *hmac_tfm; 193d3123599STom Lendacky }; 194d3123599STom Lendacky 195d3123599STom Lendacky struct ccp_sha_req_ctx { 196d3123599STom Lendacky enum ccp_sha_type type; 197d3123599STom Lendacky 198d3123599STom Lendacky u64 msg_bits; 199d3123599STom Lendacky 200d3123599STom Lendacky unsigned int first; 201d3123599STom Lendacky unsigned int final; 202d3123599STom Lendacky 20381a59f00STom Lendacky struct scatterlist *src; 20481a59f00STom Lendacky unsigned int nbytes; 20581a59f00STom Lendacky 20681a59f00STom Lendacky u64 hash_cnt; 207d3123599STom Lendacky unsigned int hash_rem; 208d3123599STom Lendacky 209d3123599STom Lendacky struct sg_table data_sg; 210d3123599STom Lendacky 211d3123599STom Lendacky struct scatterlist ctx_sg; 212d3123599STom Lendacky u8 ctx[MAX_SHA_CONTEXT_SIZE]; 213d3123599STom Lendacky 214d3123599STom Lendacky struct scatterlist buf_sg; 215d3123599STom Lendacky unsigned int buf_count; 216d3123599STom Lendacky u8 buf[MAX_SHA_BLOCK_SIZE]; 217d3123599STom Lendacky 218d3123599STom Lendacky /* CCP driver command */ 219d3123599STom Lendacky struct ccp_cmd cmd; 220d3123599STom Lendacky }; 221d3123599STom Lendacky 222d1662165STom Lendacky struct ccp_sha_exp_ctx { 223d1662165STom Lendacky enum ccp_sha_type type; 224d1662165STom Lendacky 225d1662165STom Lendacky u64 msg_bits; 226d1662165STom Lendacky 227d1662165STom Lendacky unsigned int first; 228d1662165STom Lendacky 229d1662165STom Lendacky u8 ctx[MAX_SHA_CONTEXT_SIZE]; 230d1662165STom Lendacky 231d1662165STom Lendacky unsigned int buf_count; 232d1662165STom Lendacky u8 buf[MAX_SHA_BLOCK_SIZE]; 233d1662165STom Lendacky }; 234d1662165STom Lendacky 235ceeec0afSGary R Hook /***** RSA related defines *****/ 236ceeec0afSGary R Hook 237ceeec0afSGary R Hook struct ccp_rsa_ctx { 238ceeec0afSGary R Hook unsigned int key_len; /* in bits */ 239ceeec0afSGary R Hook struct scatterlist e_sg; 240ceeec0afSGary R Hook u8 *e_buf; 241ceeec0afSGary R Hook unsigned int e_len; 242ceeec0afSGary R Hook struct scatterlist n_sg; 243ceeec0afSGary R Hook u8 *n_buf; 244ceeec0afSGary R Hook unsigned int n_len; 245ceeec0afSGary R Hook struct scatterlist d_sg; 246ceeec0afSGary R Hook u8 *d_buf; 247ceeec0afSGary R Hook unsigned int d_len; 248ceeec0afSGary R Hook }; 249ceeec0afSGary R Hook 250ceeec0afSGary R Hook struct ccp_rsa_req_ctx { 251ceeec0afSGary R Hook struct ccp_cmd cmd; 252ceeec0afSGary R Hook }; 253ceeec0afSGary R Hook 254ceeec0afSGary R Hook #define CCP_RSA_MAXMOD (4 * 1024 / 8) 255e28c190dSGary R Hook #define CCP5_RSA_MAXMOD (16 * 1024 / 8) 256ceeec0afSGary R Hook 257d3123599STom Lendacky /***** Common Context Structure *****/ 258d3123599STom Lendacky struct ccp_ctx { 259d3123599STom Lendacky int (*complete)(struct crypto_async_request *req, int ret); 260d3123599STom Lendacky 261d3123599STom Lendacky union { 262d3123599STom Lendacky struct ccp_aes_ctx aes; 263ceeec0afSGary R Hook struct ccp_rsa_ctx rsa; 264d3123599STom Lendacky struct ccp_sha_ctx sha; 265990672d4SGary R Hook struct ccp_des3_ctx des3; 266d3123599STom Lendacky } u; 267d3123599STom Lendacky }; 268d3123599STom Lendacky 269d3123599STom Lendacky int ccp_crypto_enqueue_request(struct crypto_async_request *req, 270d3123599STom Lendacky struct ccp_cmd *cmd); 271d3123599STom Lendacky struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table, 272d3123599STom Lendacky struct scatterlist *sg_add); 273d3123599STom Lendacky 274d3123599STom Lendacky int ccp_register_aes_algs(struct list_head *head); 275d3123599STom Lendacky int ccp_register_aes_cmac_algs(struct list_head *head); 276d3123599STom Lendacky int ccp_register_aes_xts_algs(struct list_head *head); 27736cf515bSGary R Hook int ccp_register_aes_aeads(struct list_head *head); 278d3123599STom Lendacky int ccp_register_sha_algs(struct list_head *head); 279990672d4SGary R Hook int ccp_register_des3_algs(struct list_head *head); 280ceeec0afSGary R Hook int ccp_register_rsa_algs(struct list_head *head); 281d3123599STom Lendacky 282d3123599STom Lendacky #endif 283