1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * AMD Cryptographic Coprocessor (CCP) crypto API support
5 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
10 #ifndef __CCP_CRYPTO_H__
11 #define __CCP_CRYPTO_H__
13 #include <linux/list.h>
14 #include <linux/wait.h>
15 #include <linux/ccp.h>
16 #include <crypto/algapi.h>
17 #include <crypto/aes.h>
18 #include <crypto/internal/aead.h>
19 #include <crypto/aead.h>
20 #include <crypto/ctr.h>
21 #include <crypto/hash.h>
22 #include <crypto/sha1.h>
23 #include <crypto/sha2.h>
24 #include <crypto/akcipher.h>
25 #include <crypto/skcipher.h>
26 #include <crypto/internal/rsa.h>
28 /* We want the module name in front of our messages */
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #define CCP_LOG_LEVEL KERN_INFO
34 #define CCP_CRA_PRIORITY 300
36 struct ccp_crypto_skcipher_alg {
37 struct list_head entry;
41 struct skcipher_alg alg;
44 struct ccp_crypto_aead {
45 struct list_head entry;
52 struct ccp_crypto_ahash_alg {
53 struct list_head entry;
59 /* Child algorithm used for HMAC, CMAC, etc */
60 char child_alg[CRYPTO_MAX_ALG_NAME];
65 struct ccp_crypto_akcipher_alg {
66 struct list_head entry;
68 struct akcipher_alg alg;
71 static inline struct ccp_crypto_skcipher_alg *
72 ccp_crypto_skcipher_alg(struct crypto_skcipher *tfm)
74 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
76 return container_of(alg, struct ccp_crypto_skcipher_alg, alg);
79 static inline struct ccp_crypto_ahash_alg *
80 ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
82 struct crypto_alg *alg = tfm->__crt_alg;
83 struct ahash_alg *ahash_alg;
85 ahash_alg = container_of(alg, struct ahash_alg, halg.base);
87 return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
90 /***** AES related defines *****/
92 /* Fallback cipher for XTS with unsupported unit sizes */
93 struct crypto_skcipher *tfm_skcipher;
95 enum ccp_engine engine;
96 enum ccp_aes_type type;
97 enum ccp_aes_mode mode;
99 struct scatterlist key_sg;
100 unsigned int key_len;
101 u8 key[AES_MAX_KEY_SIZE * 2];
103 u8 nonce[CTR_RFC3686_NONCE_SIZE];
105 /* CMAC key structures */
106 struct scatterlist k1_sg;
107 struct scatterlist k2_sg;
109 u8 k1[AES_BLOCK_SIZE];
110 u8 k2[AES_BLOCK_SIZE];
113 struct ccp_aes_req_ctx {
114 struct scatterlist iv_sg;
115 u8 iv[AES_BLOCK_SIZE];
117 struct scatterlist tag_sg;
118 u8 tag[AES_BLOCK_SIZE];
120 /* Fields used for RFC3686 requests */
122 u8 rfc3686_iv[AES_BLOCK_SIZE];
126 struct skcipher_request fallback_req; // keep at the end
129 struct ccp_aes_cmac_req_ctx {
130 unsigned int null_msg;
133 struct scatterlist *src;
137 unsigned int hash_rem;
139 struct sg_table data_sg;
141 struct scatterlist iv_sg;
142 u8 iv[AES_BLOCK_SIZE];
144 struct scatterlist buf_sg;
145 unsigned int buf_count;
146 u8 buf[AES_BLOCK_SIZE];
148 struct scatterlist pad_sg;
149 unsigned int pad_count;
150 u8 pad[AES_BLOCK_SIZE];
155 struct ccp_aes_cmac_exp_ctx {
156 unsigned int null_msg;
158 u8 iv[AES_BLOCK_SIZE];
160 unsigned int buf_count;
161 u8 buf[AES_BLOCK_SIZE];
164 /***** 3DES related defines *****/
165 struct ccp_des3_ctx {
166 enum ccp_engine engine;
167 enum ccp_des3_type type;
168 enum ccp_des3_mode mode;
170 struct scatterlist key_sg;
171 unsigned int key_len;
172 u8 key[AES_MAX_KEY_SIZE];
175 struct ccp_des3_req_ctx {
176 struct scatterlist iv_sg;
177 u8 iv[AES_BLOCK_SIZE];
182 /* SHA-related defines
183 * These values must be large enough to accommodate any variant
185 #define MAX_SHA_CONTEXT_SIZE SHA512_DIGEST_SIZE
186 #define MAX_SHA_BLOCK_SIZE SHA512_BLOCK_SIZE
189 struct scatterlist opad_sg;
190 unsigned int opad_count;
192 unsigned int key_len;
193 u8 key[MAX_SHA_BLOCK_SIZE];
194 u8 ipad[MAX_SHA_BLOCK_SIZE];
195 u8 opad[MAX_SHA_BLOCK_SIZE];
196 struct crypto_shash *hmac_tfm;
199 struct ccp_sha_req_ctx {
200 enum ccp_sha_type type;
207 struct scatterlist *src;
211 unsigned int hash_rem;
213 struct sg_table data_sg;
215 struct scatterlist ctx_sg;
216 u8 ctx[MAX_SHA_CONTEXT_SIZE];
218 struct scatterlist buf_sg;
219 unsigned int buf_count;
220 u8 buf[MAX_SHA_BLOCK_SIZE];
222 /* CCP driver command */
226 struct ccp_sha_exp_ctx {
227 enum ccp_sha_type type;
233 u8 ctx[MAX_SHA_CONTEXT_SIZE];
235 unsigned int buf_count;
236 u8 buf[MAX_SHA_BLOCK_SIZE];
239 /***** RSA related defines *****/
242 unsigned int key_len; /* in bits */
243 struct scatterlist e_sg;
246 struct scatterlist n_sg;
249 struct scatterlist d_sg;
254 struct ccp_rsa_req_ctx {
258 #define CCP_RSA_MAXMOD (4 * 1024 / 8)
259 #define CCP5_RSA_MAXMOD (16 * 1024 / 8)
261 /***** Common Context Structure *****/
263 int (*complete)(struct crypto_async_request *req, int ret);
266 struct ccp_aes_ctx aes;
267 struct ccp_rsa_ctx rsa;
268 struct ccp_sha_ctx sha;
269 struct ccp_des3_ctx des3;
273 int ccp_crypto_enqueue_request(struct crypto_async_request *req,
274 struct ccp_cmd *cmd);
275 struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
276 struct scatterlist *sg_add);
278 int ccp_register_aes_algs(struct list_head *head);
279 int ccp_register_aes_cmac_algs(struct list_head *head);
280 int ccp_register_aes_xts_algs(struct list_head *head);
281 int ccp_register_aes_aeads(struct list_head *head);
282 int ccp_register_sha_algs(struct list_head *head);
283 int ccp_register_des3_algs(struct list_head *head);
284 int ccp_register_rsa_algs(struct list_head *head);