1d2912cb1SThomas 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/ccp.h> 16d3123599STom Lendacky #include <crypto/algapi.h> 17d3123599STom Lendacky #include <crypto/aes.h> 1836cf515bSGary R Hook #include <crypto/internal/aead.h> 1936cf515bSGary R Hook #include <crypto/aead.h> 20d3123599STom Lendacky #include <crypto/ctr.h> 21d3123599STom Lendacky #include <crypto/hash.h> 22*a24d22b2SEric Biggers #include <crypto/sha1.h> 23*a24d22b2SEric Biggers #include <crypto/sha2.h> 24ceeec0afSGary R Hook #include <crypto/akcipher.h> 25be9fe620SArd Biesheuvel #include <crypto/skcipher.h> 26ceeec0afSGary R Hook #include <crypto/internal/rsa.h> 27d3123599STom Lendacky 282a03e3a5SHook, Gary /* We want the module name in front of our messages */ 292a03e3a5SHook, Gary #undef pr_fmt 302a03e3a5SHook, Gary #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 312a03e3a5SHook, Gary 32990672d4SGary R Hook #define CCP_LOG_LEVEL KERN_INFO 33990672d4SGary R Hook 34d3123599STom Lendacky #define CCP_CRA_PRIORITY 300 35d3123599STom Lendacky 36be9fe620SArd Biesheuvel struct ccp_crypto_skcipher_alg { 37d3123599STom Lendacky struct list_head entry; 38d3123599STom Lendacky 39d3123599STom Lendacky u32 mode; 40d3123599STom Lendacky 41be9fe620SArd Biesheuvel struct skcipher_alg alg; 42d3123599STom Lendacky }; 43d3123599STom Lendacky 4436cf515bSGary R Hook struct ccp_crypto_aead { 4536cf515bSGary R Hook struct list_head entry; 4636cf515bSGary R Hook 4736cf515bSGary R Hook u32 mode; 4836cf515bSGary R Hook 4936cf515bSGary R Hook struct aead_alg alg; 5036cf515bSGary R Hook }; 5136cf515bSGary R Hook 52d3123599STom Lendacky struct ccp_crypto_ahash_alg { 53d3123599STom Lendacky struct list_head entry; 54d3123599STom Lendacky 556f0be9b2STom Lendacky const __be32 *init; 56d3123599STom Lendacky u32 type; 57d3123599STom Lendacky u32 mode; 58d3123599STom Lendacky 59d3123599STom Lendacky /* Child algorithm used for HMAC, CMAC, etc */ 60d3123599STom Lendacky char child_alg[CRYPTO_MAX_ALG_NAME]; 61d3123599STom Lendacky 62d3123599STom Lendacky struct ahash_alg alg; 63d3123599STom Lendacky }; 64d3123599STom Lendacky 65ceeec0afSGary R Hook struct ccp_crypto_akcipher_alg { 66ceeec0afSGary R Hook struct list_head entry; 67ceeec0afSGary R Hook 68ceeec0afSGary R Hook struct akcipher_alg alg; 69ceeec0afSGary R Hook }; 70ceeec0afSGary R Hook 71be9fe620SArd Biesheuvel static inline struct ccp_crypto_skcipher_alg * ccp_crypto_skcipher_alg(struct crypto_skcipher * tfm)72be9fe620SArd Biesheuvel ccp_crypto_skcipher_alg(struct crypto_skcipher *tfm) 73d3123599STom Lendacky { 74be9fe620SArd Biesheuvel struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 75d3123599STom Lendacky 76be9fe620SArd Biesheuvel return container_of(alg, struct ccp_crypto_skcipher_alg, alg); 77d3123599STom Lendacky } 78d3123599STom Lendacky 79d3123599STom Lendacky static inline struct ccp_crypto_ahash_alg * ccp_crypto_ahash_alg(struct crypto_tfm * tfm)80d3123599STom Lendacky ccp_crypto_ahash_alg(struct crypto_tfm *tfm) 81d3123599STom Lendacky { 82d3123599STom Lendacky struct crypto_alg *alg = tfm->__crt_alg; 83d3123599STom Lendacky struct ahash_alg *ahash_alg; 84d3123599STom Lendacky 85d3123599STom Lendacky ahash_alg = container_of(alg, struct ahash_alg, halg.base); 86d3123599STom Lendacky 87d3123599STom Lendacky return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg); 88d3123599STom Lendacky } 89d3123599STom Lendacky 90d3123599STom Lendacky /***** AES related defines *****/ 91d3123599STom Lendacky struct ccp_aes_ctx { 92d3123599STom Lendacky /* Fallback cipher for XTS with unsupported unit sizes */ 93413b61ceSArd Biesheuvel struct crypto_skcipher *tfm_skcipher; 94d3123599STom Lendacky 95d3123599STom Lendacky enum ccp_engine engine; 96d3123599STom Lendacky enum ccp_aes_type type; 97d3123599STom Lendacky enum ccp_aes_mode mode; 98d3123599STom Lendacky 99d3123599STom Lendacky struct scatterlist key_sg; 100d3123599STom Lendacky unsigned int key_len; 1015060ffc9SGary R Hook u8 key[AES_MAX_KEY_SIZE * 2]; 102d3123599STom Lendacky 103d3123599STom Lendacky u8 nonce[CTR_RFC3686_NONCE_SIZE]; 104d3123599STom Lendacky 105d3123599STom Lendacky /* CMAC key structures */ 106d3123599STom Lendacky struct scatterlist k1_sg; 107d3123599STom Lendacky struct scatterlist k2_sg; 108d3123599STom Lendacky unsigned int kn_len; 109d3123599STom Lendacky u8 k1[AES_BLOCK_SIZE]; 110d3123599STom Lendacky u8 k2[AES_BLOCK_SIZE]; 111d3123599STom Lendacky }; 112d3123599STom Lendacky 113d3123599STom Lendacky struct ccp_aes_req_ctx { 114d3123599STom Lendacky struct scatterlist iv_sg; 115d3123599STom Lendacky u8 iv[AES_BLOCK_SIZE]; 116d3123599STom Lendacky 11736cf515bSGary R Hook struct scatterlist tag_sg; 11836cf515bSGary R Hook u8 tag[AES_BLOCK_SIZE]; 11936cf515bSGary R Hook 120d3123599STom Lendacky /* Fields used for RFC3686 requests */ 121d3123599STom Lendacky u8 *rfc3686_info; 122d3123599STom Lendacky u8 rfc3686_iv[AES_BLOCK_SIZE]; 123d3123599STom Lendacky 124d3123599STom Lendacky struct ccp_cmd cmd; 125413b61ceSArd Biesheuvel 126413b61ceSArd Biesheuvel struct skcipher_request fallback_req; // keep at the end 127d3123599STom Lendacky }; 128d3123599STom Lendacky 129d3123599STom Lendacky struct ccp_aes_cmac_req_ctx { 130d3123599STom Lendacky unsigned int null_msg; 131d3123599STom Lendacky unsigned int final; 132d3123599STom Lendacky 13381a59f00STom Lendacky struct scatterlist *src; 13481a59f00STom Lendacky unsigned int nbytes; 13581a59f00STom Lendacky 13681a59f00STom Lendacky u64 hash_cnt; 137d3123599STom Lendacky unsigned int hash_rem; 138d3123599STom Lendacky 139d3123599STom Lendacky struct sg_table data_sg; 140d3123599STom Lendacky 141d3123599STom Lendacky struct scatterlist iv_sg; 142d3123599STom Lendacky u8 iv[AES_BLOCK_SIZE]; 143d3123599STom Lendacky 144d3123599STom Lendacky struct scatterlist buf_sg; 145d3123599STom Lendacky unsigned int buf_count; 146d3123599STom Lendacky u8 buf[AES_BLOCK_SIZE]; 147d3123599STom Lendacky 148d3123599STom Lendacky struct scatterlist pad_sg; 149d3123599STom Lendacky unsigned int pad_count; 150d3123599STom Lendacky u8 pad[AES_BLOCK_SIZE]; 151d3123599STom Lendacky 152d3123599STom Lendacky struct ccp_cmd cmd; 153d3123599STom Lendacky }; 154d3123599STom Lendacky 155d1662165STom Lendacky struct ccp_aes_cmac_exp_ctx { 156d1662165STom Lendacky unsigned int null_msg; 157d1662165STom Lendacky 158d1662165STom Lendacky u8 iv[AES_BLOCK_SIZE]; 159d1662165STom Lendacky 160d1662165STom Lendacky unsigned int buf_count; 161d1662165STom Lendacky u8 buf[AES_BLOCK_SIZE]; 162d1662165STom Lendacky }; 163d1662165STom Lendacky 164990672d4SGary R Hook /***** 3DES related defines *****/ 165990672d4SGary R Hook struct ccp_des3_ctx { 166990672d4SGary R Hook enum ccp_engine engine; 167990672d4SGary R Hook enum ccp_des3_type type; 168990672d4SGary R Hook enum ccp_des3_mode mode; 169990672d4SGary R Hook 170990672d4SGary R Hook struct scatterlist key_sg; 171990672d4SGary R Hook unsigned int key_len; 172990672d4SGary R Hook u8 key[AES_MAX_KEY_SIZE]; 173990672d4SGary R Hook }; 174990672d4SGary R Hook 175990672d4SGary R Hook struct ccp_des3_req_ctx { 176990672d4SGary R Hook struct scatterlist iv_sg; 177990672d4SGary R Hook u8 iv[AES_BLOCK_SIZE]; 178990672d4SGary R Hook 179990672d4SGary R Hook struct ccp_cmd cmd; 180990672d4SGary R Hook }; 181990672d4SGary R Hook 182ccebcf3fSGary R Hook /* SHA-related defines 183ccebcf3fSGary R Hook * These values must be large enough to accommodate any variant 184ccebcf3fSGary R Hook */ 185ccebcf3fSGary R Hook #define MAX_SHA_CONTEXT_SIZE SHA512_DIGEST_SIZE 186ccebcf3fSGary R Hook #define MAX_SHA_BLOCK_SIZE SHA512_BLOCK_SIZE 187d3123599STom Lendacky 188d3123599STom Lendacky struct ccp_sha_ctx { 189c11baa02STom Lendacky struct scatterlist opad_sg; 190c11baa02STom Lendacky unsigned int opad_count; 191c11baa02STom Lendacky 192d3123599STom Lendacky unsigned int key_len; 193d3123599STom Lendacky u8 key[MAX_SHA_BLOCK_SIZE]; 194d3123599STom Lendacky u8 ipad[MAX_SHA_BLOCK_SIZE]; 195d3123599STom Lendacky u8 opad[MAX_SHA_BLOCK_SIZE]; 196c11baa02STom Lendacky struct crypto_shash *hmac_tfm; 197d3123599STom Lendacky }; 198d3123599STom Lendacky 199d3123599STom Lendacky struct ccp_sha_req_ctx { 200d3123599STom Lendacky enum ccp_sha_type type; 201d3123599STom Lendacky 202d3123599STom Lendacky u64 msg_bits; 203d3123599STom Lendacky 204d3123599STom Lendacky unsigned int first; 205d3123599STom Lendacky unsigned int final; 206d3123599STom Lendacky 20781a59f00STom Lendacky struct scatterlist *src; 20881a59f00STom Lendacky unsigned int nbytes; 20981a59f00STom Lendacky 21081a59f00STom Lendacky u64 hash_cnt; 211d3123599STom Lendacky unsigned int hash_rem; 212d3123599STom Lendacky 213d3123599STom Lendacky struct sg_table data_sg; 214d3123599STom Lendacky 215d3123599STom Lendacky struct scatterlist ctx_sg; 216d3123599STom Lendacky u8 ctx[MAX_SHA_CONTEXT_SIZE]; 217d3123599STom Lendacky 218d3123599STom Lendacky struct scatterlist buf_sg; 219d3123599STom Lendacky unsigned int buf_count; 220d3123599STom Lendacky u8 buf[MAX_SHA_BLOCK_SIZE]; 221d3123599STom Lendacky 222d3123599STom Lendacky /* CCP driver command */ 223d3123599STom Lendacky struct ccp_cmd cmd; 224d3123599STom Lendacky }; 225d3123599STom Lendacky 226d1662165STom Lendacky struct ccp_sha_exp_ctx { 227d1662165STom Lendacky enum ccp_sha_type type; 228d1662165STom Lendacky 229d1662165STom Lendacky u64 msg_bits; 230d1662165STom Lendacky 231d1662165STom Lendacky unsigned int first; 232d1662165STom Lendacky 233d1662165STom Lendacky u8 ctx[MAX_SHA_CONTEXT_SIZE]; 234d1662165STom Lendacky 235d1662165STom Lendacky unsigned int buf_count; 236d1662165STom Lendacky u8 buf[MAX_SHA_BLOCK_SIZE]; 237d1662165STom Lendacky }; 238d1662165STom Lendacky 239ceeec0afSGary R Hook /***** RSA related defines *****/ 240ceeec0afSGary R Hook 241ceeec0afSGary R Hook struct ccp_rsa_ctx { 242ceeec0afSGary R Hook unsigned int key_len; /* in bits */ 243ceeec0afSGary R Hook struct scatterlist e_sg; 244ceeec0afSGary R Hook u8 *e_buf; 245ceeec0afSGary R Hook unsigned int e_len; 246ceeec0afSGary R Hook struct scatterlist n_sg; 247ceeec0afSGary R Hook u8 *n_buf; 248ceeec0afSGary R Hook unsigned int n_len; 249ceeec0afSGary R Hook struct scatterlist d_sg; 250ceeec0afSGary R Hook u8 *d_buf; 251ceeec0afSGary R Hook unsigned int d_len; 252ceeec0afSGary R Hook }; 253ceeec0afSGary R Hook 254ceeec0afSGary R Hook struct ccp_rsa_req_ctx { 255ceeec0afSGary R Hook struct ccp_cmd cmd; 256ceeec0afSGary R Hook }; 257ceeec0afSGary R Hook 258ceeec0afSGary R Hook #define CCP_RSA_MAXMOD (4 * 1024 / 8) 259e28c190dSGary R Hook #define CCP5_RSA_MAXMOD (16 * 1024 / 8) 260ceeec0afSGary R Hook 261d3123599STom Lendacky /***** Common Context Structure *****/ 262d3123599STom Lendacky struct ccp_ctx { 263d3123599STom Lendacky int (*complete)(struct crypto_async_request *req, int ret); 264d3123599STom Lendacky 265d3123599STom Lendacky union { 266d3123599STom Lendacky struct ccp_aes_ctx aes; 267ceeec0afSGary R Hook struct ccp_rsa_ctx rsa; 268d3123599STom Lendacky struct ccp_sha_ctx sha; 269990672d4SGary R Hook struct ccp_des3_ctx des3; 270d3123599STom Lendacky } u; 271d3123599STom Lendacky }; 272d3123599STom Lendacky 273d3123599STom Lendacky int ccp_crypto_enqueue_request(struct crypto_async_request *req, 274d3123599STom Lendacky struct ccp_cmd *cmd); 275d3123599STom Lendacky struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table, 276d3123599STom Lendacky struct scatterlist *sg_add); 277d3123599STom Lendacky 278d3123599STom Lendacky int ccp_register_aes_algs(struct list_head *head); 279d3123599STom Lendacky int ccp_register_aes_cmac_algs(struct list_head *head); 280d3123599STom Lendacky int ccp_register_aes_xts_algs(struct list_head *head); 28136cf515bSGary R Hook int ccp_register_aes_aeads(struct list_head *head); 282d3123599STom Lendacky int ccp_register_sha_algs(struct list_head *head); 283990672d4SGary R Hook int ccp_register_des3_algs(struct list_head *head); 284ceeec0afSGary R Hook int ccp_register_rsa_algs(struct list_head *head); 285d3123599STom Lendacky 286d3123599STom Lendacky #endif 287