1d3123599STom Lendacky /* 2d3123599STom Lendacky * AMD Cryptographic Coprocessor (CCP) crypto API support 3d3123599STom Lendacky * 468cc652fSGary R Hook * Copyright (C) 2013,2017 Advanced Micro Devices, Inc. 5d3123599STom Lendacky * 6d3123599STom Lendacky * Author: Tom Lendacky <thomas.lendacky@amd.com> 7d3123599STom Lendacky * 8d3123599STom Lendacky * This program is free software; you can redistribute it and/or modify 9d3123599STom Lendacky * it under the terms of the GNU General Public License version 2 as 10d3123599STom Lendacky * published by the Free Software Foundation. 11d3123599STom Lendacky */ 12d3123599STom Lendacky 13d3123599STom Lendacky #ifndef __CCP_CRYPTO_H__ 14d3123599STom Lendacky #define __CCP_CRYPTO_H__ 15d3123599STom Lendacky 16d3123599STom Lendacky #include <linux/list.h> 17d3123599STom Lendacky #include <linux/wait.h> 18d3123599STom Lendacky #include <linux/pci.h> 19d3123599STom Lendacky #include <linux/ccp.h> 20d3123599STom Lendacky #include <crypto/algapi.h> 21d3123599STom Lendacky #include <crypto/aes.h> 2236cf515bSGary R Hook #include <crypto/internal/aead.h> 2336cf515bSGary R Hook #include <crypto/aead.h> 24d3123599STom Lendacky #include <crypto/ctr.h> 25d3123599STom Lendacky #include <crypto/hash.h> 26d3123599STom Lendacky #include <crypto/sha.h> 27ceeec0afSGary R Hook #include <crypto/akcipher.h> 28ceeec0afSGary R Hook #include <crypto/internal/rsa.h> 29d3123599STom Lendacky 30990672d4SGary R Hook #define CCP_LOG_LEVEL KERN_INFO 31990672d4SGary R Hook 32d3123599STom Lendacky #define CCP_CRA_PRIORITY 300 33d3123599STom Lendacky 34d3123599STom Lendacky struct ccp_crypto_ablkcipher_alg { 35d3123599STom Lendacky struct list_head entry; 36d3123599STom Lendacky 37d3123599STom Lendacky u32 mode; 38d3123599STom Lendacky 39d3123599STom Lendacky struct crypto_alg alg; 40d3123599STom Lendacky }; 41d3123599STom Lendacky 4236cf515bSGary R Hook struct ccp_crypto_aead { 4336cf515bSGary R Hook struct list_head entry; 4436cf515bSGary R Hook 4536cf515bSGary R Hook u32 mode; 4636cf515bSGary R Hook 4736cf515bSGary R Hook struct aead_alg alg; 4836cf515bSGary R Hook }; 4936cf515bSGary R Hook 50d3123599STom Lendacky struct ccp_crypto_ahash_alg { 51d3123599STom Lendacky struct list_head entry; 52d3123599STom Lendacky 536f0be9b2STom Lendacky const __be32 *init; 54d3123599STom Lendacky u32 type; 55d3123599STom Lendacky u32 mode; 56d3123599STom Lendacky 57d3123599STom Lendacky /* Child algorithm used for HMAC, CMAC, etc */ 58d3123599STom Lendacky char child_alg[CRYPTO_MAX_ALG_NAME]; 59d3123599STom Lendacky 60d3123599STom Lendacky struct ahash_alg alg; 61d3123599STom Lendacky }; 62d3123599STom Lendacky 63ceeec0afSGary R Hook struct ccp_crypto_akcipher_alg { 64ceeec0afSGary R Hook struct list_head entry; 65ceeec0afSGary R Hook 66ceeec0afSGary R Hook struct akcipher_alg alg; 67ceeec0afSGary R Hook }; 68ceeec0afSGary R Hook 69d3123599STom Lendacky static inline struct ccp_crypto_ablkcipher_alg * 70d3123599STom Lendacky ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm) 71d3123599STom Lendacky { 72d3123599STom Lendacky struct crypto_alg *alg = tfm->__crt_alg; 73d3123599STom Lendacky 74d3123599STom Lendacky return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg); 75d3123599STom Lendacky } 76d3123599STom Lendacky 77d3123599STom Lendacky static inline struct ccp_crypto_ahash_alg * 78d3123599STom Lendacky ccp_crypto_ahash_alg(struct crypto_tfm *tfm) 79d3123599STom Lendacky { 80d3123599STom Lendacky struct crypto_alg *alg = tfm->__crt_alg; 81d3123599STom Lendacky struct ahash_alg *ahash_alg; 82d3123599STom Lendacky 83d3123599STom Lendacky ahash_alg = container_of(alg, struct ahash_alg, halg.base); 84d3123599STom Lendacky 85d3123599STom Lendacky return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg); 86d3123599STom Lendacky } 87d3123599STom Lendacky 88d3123599STom Lendacky /***** AES related defines *****/ 89d3123599STom Lendacky struct ccp_aes_ctx { 90d3123599STom Lendacky /* Fallback cipher for XTS with unsupported unit sizes */ 91*7f28615dSKees Cook struct crypto_sync_skcipher *tfm_skcipher; 92d3123599STom Lendacky 93d3123599STom Lendacky /* Cipher used to generate CMAC K1/K2 keys */ 94d3123599STom Lendacky struct crypto_cipher *tfm_cipher; 95d3123599STom Lendacky 96d3123599STom Lendacky enum ccp_engine engine; 97d3123599STom Lendacky enum ccp_aes_type type; 98d3123599STom Lendacky enum ccp_aes_mode mode; 99d3123599STom Lendacky 100d3123599STom Lendacky struct scatterlist key_sg; 101d3123599STom Lendacky unsigned int key_len; 1025060ffc9SGary R Hook u8 key[AES_MAX_KEY_SIZE * 2]; 103d3123599STom Lendacky 104d3123599STom Lendacky u8 nonce[CTR_RFC3686_NONCE_SIZE]; 105d3123599STom Lendacky 106d3123599STom Lendacky /* CMAC key structures */ 107d3123599STom Lendacky struct scatterlist k1_sg; 108d3123599STom Lendacky struct scatterlist k2_sg; 109d3123599STom Lendacky unsigned int kn_len; 110d3123599STom Lendacky u8 k1[AES_BLOCK_SIZE]; 111d3123599STom Lendacky u8 k2[AES_BLOCK_SIZE]; 112d3123599STom Lendacky }; 113d3123599STom Lendacky 114d3123599STom Lendacky struct ccp_aes_req_ctx { 115d3123599STom Lendacky struct scatterlist iv_sg; 116d3123599STom Lendacky u8 iv[AES_BLOCK_SIZE]; 117d3123599STom Lendacky 11836cf515bSGary R Hook struct scatterlist tag_sg; 11936cf515bSGary R Hook u8 tag[AES_BLOCK_SIZE]; 12036cf515bSGary R Hook 121d3123599STom Lendacky /* Fields used for RFC3686 requests */ 122d3123599STom Lendacky u8 *rfc3686_info; 123d3123599STom Lendacky u8 rfc3686_iv[AES_BLOCK_SIZE]; 124d3123599STom Lendacky 125d3123599STom Lendacky struct ccp_cmd cmd; 126d3123599STom Lendacky }; 127d3123599STom Lendacky 128d3123599STom Lendacky struct ccp_aes_cmac_req_ctx { 129d3123599STom Lendacky unsigned int null_msg; 130d3123599STom Lendacky unsigned int final; 131d3123599STom Lendacky 13281a59f00STom Lendacky struct scatterlist *src; 13381a59f00STom Lendacky unsigned int nbytes; 13481a59f00STom Lendacky 13581a59f00STom Lendacky u64 hash_cnt; 136d3123599STom Lendacky unsigned int hash_rem; 137d3123599STom Lendacky 138d3123599STom Lendacky struct sg_table data_sg; 139d3123599STom Lendacky 140d3123599STom Lendacky struct scatterlist iv_sg; 141d3123599STom Lendacky u8 iv[AES_BLOCK_SIZE]; 142d3123599STom Lendacky 143d3123599STom Lendacky struct scatterlist buf_sg; 144d3123599STom Lendacky unsigned int buf_count; 145d3123599STom Lendacky u8 buf[AES_BLOCK_SIZE]; 146d3123599STom Lendacky 147d3123599STom Lendacky struct scatterlist pad_sg; 148d3123599STom Lendacky unsigned int pad_count; 149d3123599STom Lendacky u8 pad[AES_BLOCK_SIZE]; 150d3123599STom Lendacky 151d3123599STom Lendacky struct ccp_cmd cmd; 152d3123599STom Lendacky }; 153d3123599STom Lendacky 154d1662165STom Lendacky struct ccp_aes_cmac_exp_ctx { 155d1662165STom Lendacky unsigned int null_msg; 156d1662165STom Lendacky 157d1662165STom Lendacky u8 iv[AES_BLOCK_SIZE]; 158d1662165STom Lendacky 159d1662165STom Lendacky unsigned int buf_count; 160d1662165STom Lendacky u8 buf[AES_BLOCK_SIZE]; 161d1662165STom Lendacky }; 162d1662165STom Lendacky 163990672d4SGary R Hook /***** 3DES related defines *****/ 164990672d4SGary R Hook struct ccp_des3_ctx { 165990672d4SGary R Hook enum ccp_engine engine; 166990672d4SGary R Hook enum ccp_des3_type type; 167990672d4SGary R Hook enum ccp_des3_mode mode; 168990672d4SGary R Hook 169990672d4SGary R Hook struct scatterlist key_sg; 170990672d4SGary R Hook unsigned int key_len; 171990672d4SGary R Hook u8 key[AES_MAX_KEY_SIZE]; 172990672d4SGary R Hook }; 173990672d4SGary R Hook 174990672d4SGary R Hook struct ccp_des3_req_ctx { 175990672d4SGary R Hook struct scatterlist iv_sg; 176990672d4SGary R Hook u8 iv[AES_BLOCK_SIZE]; 177990672d4SGary R Hook 178990672d4SGary R Hook struct ccp_cmd cmd; 179990672d4SGary R Hook }; 180990672d4SGary R Hook 181ccebcf3fSGary R Hook /* SHA-related defines 182ccebcf3fSGary R Hook * These values must be large enough to accommodate any variant 183ccebcf3fSGary R Hook */ 184ccebcf3fSGary R Hook #define MAX_SHA_CONTEXT_SIZE SHA512_DIGEST_SIZE 185ccebcf3fSGary R Hook #define MAX_SHA_BLOCK_SIZE SHA512_BLOCK_SIZE 186d3123599STom Lendacky 187d3123599STom Lendacky struct ccp_sha_ctx { 188c11baa02STom Lendacky struct scatterlist opad_sg; 189c11baa02STom Lendacky unsigned int opad_count; 190c11baa02STom Lendacky 191d3123599STom Lendacky unsigned int key_len; 192d3123599STom Lendacky u8 key[MAX_SHA_BLOCK_SIZE]; 193d3123599STom Lendacky u8 ipad[MAX_SHA_BLOCK_SIZE]; 194d3123599STom Lendacky u8 opad[MAX_SHA_BLOCK_SIZE]; 195c11baa02STom Lendacky struct crypto_shash *hmac_tfm; 196d3123599STom Lendacky }; 197d3123599STom Lendacky 198d3123599STom Lendacky struct ccp_sha_req_ctx { 199d3123599STom Lendacky enum ccp_sha_type type; 200d3123599STom Lendacky 201d3123599STom Lendacky u64 msg_bits; 202d3123599STom Lendacky 203d3123599STom Lendacky unsigned int first; 204d3123599STom Lendacky unsigned int final; 205d3123599STom Lendacky 20681a59f00STom Lendacky struct scatterlist *src; 20781a59f00STom Lendacky unsigned int nbytes; 20881a59f00STom Lendacky 20981a59f00STom Lendacky u64 hash_cnt; 210d3123599STom Lendacky unsigned int hash_rem; 211d3123599STom Lendacky 212d3123599STom Lendacky struct sg_table data_sg; 213d3123599STom Lendacky 214d3123599STom Lendacky struct scatterlist ctx_sg; 215d3123599STom Lendacky u8 ctx[MAX_SHA_CONTEXT_SIZE]; 216d3123599STom Lendacky 217d3123599STom Lendacky struct scatterlist buf_sg; 218d3123599STom Lendacky unsigned int buf_count; 219d3123599STom Lendacky u8 buf[MAX_SHA_BLOCK_SIZE]; 220d3123599STom Lendacky 221d3123599STom Lendacky /* CCP driver command */ 222d3123599STom Lendacky struct ccp_cmd cmd; 223d3123599STom Lendacky }; 224d3123599STom Lendacky 225d1662165STom Lendacky struct ccp_sha_exp_ctx { 226d1662165STom Lendacky enum ccp_sha_type type; 227d1662165STom Lendacky 228d1662165STom Lendacky u64 msg_bits; 229d1662165STom Lendacky 230d1662165STom Lendacky unsigned int first; 231d1662165STom Lendacky 232d1662165STom Lendacky u8 ctx[MAX_SHA_CONTEXT_SIZE]; 233d1662165STom Lendacky 234d1662165STom Lendacky unsigned int buf_count; 235d1662165STom Lendacky u8 buf[MAX_SHA_BLOCK_SIZE]; 236d1662165STom Lendacky }; 237d1662165STom Lendacky 238ceeec0afSGary R Hook /***** RSA related defines *****/ 239ceeec0afSGary R Hook 240ceeec0afSGary R Hook struct ccp_rsa_ctx { 241ceeec0afSGary R Hook unsigned int key_len; /* in bits */ 242ceeec0afSGary R Hook struct scatterlist e_sg; 243ceeec0afSGary R Hook u8 *e_buf; 244ceeec0afSGary R Hook unsigned int e_len; 245ceeec0afSGary R Hook struct scatterlist n_sg; 246ceeec0afSGary R Hook u8 *n_buf; 247ceeec0afSGary R Hook unsigned int n_len; 248ceeec0afSGary R Hook struct scatterlist d_sg; 249ceeec0afSGary R Hook u8 *d_buf; 250ceeec0afSGary R Hook unsigned int d_len; 251ceeec0afSGary R Hook }; 252ceeec0afSGary R Hook 253ceeec0afSGary R Hook struct ccp_rsa_req_ctx { 254ceeec0afSGary R Hook struct ccp_cmd cmd; 255ceeec0afSGary R Hook }; 256ceeec0afSGary R Hook 257ceeec0afSGary R Hook #define CCP_RSA_MAXMOD (4 * 1024 / 8) 258e28c190dSGary R Hook #define CCP5_RSA_MAXMOD (16 * 1024 / 8) 259ceeec0afSGary R Hook 260d3123599STom Lendacky /***** Common Context Structure *****/ 261d3123599STom Lendacky struct ccp_ctx { 262d3123599STom Lendacky int (*complete)(struct crypto_async_request *req, int ret); 263d3123599STom Lendacky 264d3123599STom Lendacky union { 265d3123599STom Lendacky struct ccp_aes_ctx aes; 266ceeec0afSGary R Hook struct ccp_rsa_ctx rsa; 267d3123599STom Lendacky struct ccp_sha_ctx sha; 268990672d4SGary R Hook struct ccp_des3_ctx des3; 269d3123599STom Lendacky } u; 270d3123599STom Lendacky }; 271d3123599STom Lendacky 272d3123599STom Lendacky int ccp_crypto_enqueue_request(struct crypto_async_request *req, 273d3123599STom Lendacky struct ccp_cmd *cmd); 274d3123599STom Lendacky struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table, 275d3123599STom Lendacky struct scatterlist *sg_add); 276d3123599STom Lendacky 277d3123599STom Lendacky int ccp_register_aes_algs(struct list_head *head); 278d3123599STom Lendacky int ccp_register_aes_cmac_algs(struct list_head *head); 279d3123599STom Lendacky int ccp_register_aes_xts_algs(struct list_head *head); 28036cf515bSGary R Hook int ccp_register_aes_aeads(struct list_head *head); 281d3123599STom Lendacky int ccp_register_sha_algs(struct list_head *head); 282990672d4SGary R Hook int ccp_register_des3_algs(struct list_head *head); 283ceeec0afSGary R Hook int ccp_register_rsa_algs(struct list_head *head); 284d3123599STom Lendacky 285d3123599STom Lendacky #endif 286