1 /* 2 * AMD Cryptographic Coprocessor (CCP) crypto API support 3 * 4 * Copyright (C) 2013 Advanced Micro Devices, Inc. 5 * 6 * Author: Tom Lendacky <thomas.lendacky@amd.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 13 #ifndef __CCP_CRYPTO_H__ 14 #define __CCP_CRYPTO_H__ 15 16 #include <linux/list.h> 17 #include <linux/wait.h> 18 #include <linux/pci.h> 19 #include <linux/ccp.h> 20 #include <crypto/algapi.h> 21 #include <crypto/aes.h> 22 #include <crypto/ctr.h> 23 #include <crypto/hash.h> 24 #include <crypto/sha.h> 25 26 #define CCP_CRA_PRIORITY 300 27 28 struct ccp_crypto_ablkcipher_alg { 29 struct list_head entry; 30 31 u32 mode; 32 33 struct crypto_alg alg; 34 }; 35 36 struct ccp_crypto_ahash_alg { 37 struct list_head entry; 38 39 const __be32 *init; 40 u32 type; 41 u32 mode; 42 43 /* Child algorithm used for HMAC, CMAC, etc */ 44 char child_alg[CRYPTO_MAX_ALG_NAME]; 45 46 struct ahash_alg alg; 47 }; 48 49 static inline struct ccp_crypto_ablkcipher_alg * 50 ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm) 51 { 52 struct crypto_alg *alg = tfm->__crt_alg; 53 54 return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg); 55 } 56 57 static inline struct ccp_crypto_ahash_alg * 58 ccp_crypto_ahash_alg(struct crypto_tfm *tfm) 59 { 60 struct crypto_alg *alg = tfm->__crt_alg; 61 struct ahash_alg *ahash_alg; 62 63 ahash_alg = container_of(alg, struct ahash_alg, halg.base); 64 65 return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg); 66 } 67 68 /***** AES related defines *****/ 69 struct ccp_aes_ctx { 70 /* Fallback cipher for XTS with unsupported unit sizes */ 71 struct crypto_skcipher *tfm_skcipher; 72 73 /* Cipher used to generate CMAC K1/K2 keys */ 74 struct crypto_cipher *tfm_cipher; 75 76 enum ccp_engine engine; 77 enum ccp_aes_type type; 78 enum ccp_aes_mode mode; 79 80 struct scatterlist key_sg; 81 unsigned int key_len; 82 u8 key[AES_MAX_KEY_SIZE]; 83 84 u8 nonce[CTR_RFC3686_NONCE_SIZE]; 85 86 /* CMAC key structures */ 87 struct scatterlist k1_sg; 88 struct scatterlist k2_sg; 89 unsigned int kn_len; 90 u8 k1[AES_BLOCK_SIZE]; 91 u8 k2[AES_BLOCK_SIZE]; 92 }; 93 94 struct ccp_aes_req_ctx { 95 struct scatterlist iv_sg; 96 u8 iv[AES_BLOCK_SIZE]; 97 98 /* Fields used for RFC3686 requests */ 99 u8 *rfc3686_info; 100 u8 rfc3686_iv[AES_BLOCK_SIZE]; 101 102 struct ccp_cmd cmd; 103 }; 104 105 struct ccp_aes_cmac_req_ctx { 106 unsigned int null_msg; 107 unsigned int final; 108 109 struct scatterlist *src; 110 unsigned int nbytes; 111 112 u64 hash_cnt; 113 unsigned int hash_rem; 114 115 struct sg_table data_sg; 116 117 struct scatterlist iv_sg; 118 u8 iv[AES_BLOCK_SIZE]; 119 120 struct scatterlist buf_sg; 121 unsigned int buf_count; 122 u8 buf[AES_BLOCK_SIZE]; 123 124 struct scatterlist pad_sg; 125 unsigned int pad_count; 126 u8 pad[AES_BLOCK_SIZE]; 127 128 struct ccp_cmd cmd; 129 }; 130 131 struct ccp_aes_cmac_exp_ctx { 132 unsigned int null_msg; 133 134 u8 iv[AES_BLOCK_SIZE]; 135 136 unsigned int buf_count; 137 u8 buf[AES_BLOCK_SIZE]; 138 }; 139 140 /***** SHA related defines *****/ 141 #define MAX_SHA_CONTEXT_SIZE SHA256_DIGEST_SIZE 142 #define MAX_SHA_BLOCK_SIZE SHA256_BLOCK_SIZE 143 144 struct ccp_sha_ctx { 145 struct scatterlist opad_sg; 146 unsigned int opad_count; 147 148 unsigned int key_len; 149 u8 key[MAX_SHA_BLOCK_SIZE]; 150 u8 ipad[MAX_SHA_BLOCK_SIZE]; 151 u8 opad[MAX_SHA_BLOCK_SIZE]; 152 struct crypto_shash *hmac_tfm; 153 }; 154 155 struct ccp_sha_req_ctx { 156 enum ccp_sha_type type; 157 158 u64 msg_bits; 159 160 unsigned int first; 161 unsigned int final; 162 163 struct scatterlist *src; 164 unsigned int nbytes; 165 166 u64 hash_cnt; 167 unsigned int hash_rem; 168 169 struct sg_table data_sg; 170 171 struct scatterlist ctx_sg; 172 u8 ctx[MAX_SHA_CONTEXT_SIZE]; 173 174 struct scatterlist buf_sg; 175 unsigned int buf_count; 176 u8 buf[MAX_SHA_BLOCK_SIZE]; 177 178 /* CCP driver command */ 179 struct ccp_cmd cmd; 180 }; 181 182 struct ccp_sha_exp_ctx { 183 enum ccp_sha_type type; 184 185 u64 msg_bits; 186 187 unsigned int first; 188 189 u8 ctx[MAX_SHA_CONTEXT_SIZE]; 190 191 unsigned int buf_count; 192 u8 buf[MAX_SHA_BLOCK_SIZE]; 193 }; 194 195 /***** Common Context Structure *****/ 196 struct ccp_ctx { 197 int (*complete)(struct crypto_async_request *req, int ret); 198 199 union { 200 struct ccp_aes_ctx aes; 201 struct ccp_sha_ctx sha; 202 } u; 203 }; 204 205 int ccp_crypto_enqueue_request(struct crypto_async_request *req, 206 struct ccp_cmd *cmd); 207 struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table, 208 struct scatterlist *sg_add); 209 210 int ccp_register_aes_algs(struct list_head *head); 211 int ccp_register_aes_cmac_algs(struct list_head *head); 212 int ccp_register_aes_xts_algs(struct list_head *head); 213 int ccp_register_sha_algs(struct list_head *head); 214 215 #endif 216