2 * AMD Cryptographic Coprocessor (CCP) AES crypto API support
4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
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.
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/delay.h>
16 #include <linux/scatterlist.h>
17 #include <linux/crypto.h>
18 #include <crypto/algapi.h>
19 #include <crypto/aes.h>
20 #include <crypto/ctr.h>
21 #include <crypto/scatterwalk.h>
23 #include "ccp-crypto.h"
25 static int ccp_aes_complete(struct crypto_async_request
*async_req
, int ret
)
27 struct ablkcipher_request
*req
= ablkcipher_request_cast(async_req
);
28 struct ccp_ctx
*ctx
= crypto_tfm_ctx(req
->base
.tfm
);
29 struct ccp_aes_req_ctx
*rctx
= ablkcipher_request_ctx(req
);
34 if (ctx
->u
.aes
.mode
!= CCP_AES_MODE_ECB
)
35 memcpy(req
->info
, rctx
->iv
, AES_BLOCK_SIZE
);
40 static int ccp_aes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
43 struct ccp_ctx
*ctx
= crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm
));
44 struct ccp_crypto_ablkcipher_alg
*alg
=
45 ccp_crypto_ablkcipher_alg(crypto_ablkcipher_tfm(tfm
));
49 ctx
->u
.aes
.type
= CCP_AES_TYPE_128
;
52 ctx
->u
.aes
.type
= CCP_AES_TYPE_192
;
55 ctx
->u
.aes
.type
= CCP_AES_TYPE_256
;
58 crypto_ablkcipher_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
61 ctx
->u
.aes
.mode
= alg
->mode
;
62 ctx
->u
.aes
.key_len
= key_len
;
64 memcpy(ctx
->u
.aes
.key
, key
, key_len
);
65 sg_init_one(&ctx
->u
.aes
.key_sg
, ctx
->u
.aes
.key
, key_len
);
70 static int ccp_aes_crypt(struct ablkcipher_request
*req
, bool encrypt
)
72 struct ccp_ctx
*ctx
= crypto_tfm_ctx(req
->base
.tfm
);
73 struct ccp_aes_req_ctx
*rctx
= ablkcipher_request_ctx(req
);
74 struct scatterlist
*iv_sg
= NULL
;
75 unsigned int iv_len
= 0;
78 if (!ctx
->u
.aes
.key_len
)
81 if (((ctx
->u
.aes
.mode
== CCP_AES_MODE_ECB
) ||
82 (ctx
->u
.aes
.mode
== CCP_AES_MODE_CBC
) ||
83 (ctx
->u
.aes
.mode
== CCP_AES_MODE_CFB
)) &&
84 (req
->nbytes
& (AES_BLOCK_SIZE
- 1)))
87 if (ctx
->u
.aes
.mode
!= CCP_AES_MODE_ECB
) {
91 memcpy(rctx
->iv
, req
->info
, AES_BLOCK_SIZE
);
93 iv_len
= AES_BLOCK_SIZE
;
94 sg_init_one(iv_sg
, rctx
->iv
, iv_len
);
97 memset(&rctx
->cmd
, 0, sizeof(rctx
->cmd
));
98 INIT_LIST_HEAD(&rctx
->cmd
.entry
);
99 rctx
->cmd
.engine
= CCP_ENGINE_AES
;
100 rctx
->cmd
.u
.aes
.type
= ctx
->u
.aes
.type
;
101 rctx
->cmd
.u
.aes
.mode
= ctx
->u
.aes
.mode
;
102 rctx
->cmd
.u
.aes
.action
=
103 (encrypt
) ? CCP_AES_ACTION_ENCRYPT
: CCP_AES_ACTION_DECRYPT
;
104 rctx
->cmd
.u
.aes
.key
= &ctx
->u
.aes
.key_sg
;
105 rctx
->cmd
.u
.aes
.key_len
= ctx
->u
.aes
.key_len
;
106 rctx
->cmd
.u
.aes
.iv
= iv_sg
;
107 rctx
->cmd
.u
.aes
.iv_len
= iv_len
;
108 rctx
->cmd
.u
.aes
.src
= req
->src
;
109 rctx
->cmd
.u
.aes
.src_len
= req
->nbytes
;
110 rctx
->cmd
.u
.aes
.dst
= req
->dst
;
112 ret
= ccp_crypto_enqueue_request(&req
->base
, &rctx
->cmd
);
117 static int ccp_aes_encrypt(struct ablkcipher_request
*req
)
119 return ccp_aes_crypt(req
, true);
122 static int ccp_aes_decrypt(struct ablkcipher_request
*req
)
124 return ccp_aes_crypt(req
, false);
127 static int ccp_aes_cra_init(struct crypto_tfm
*tfm
)
129 struct ccp_ctx
*ctx
= crypto_tfm_ctx(tfm
);
131 ctx
->complete
= ccp_aes_complete
;
132 ctx
->u
.aes
.key_len
= 0;
134 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct ccp_aes_req_ctx
);
139 static void ccp_aes_cra_exit(struct crypto_tfm
*tfm
)
143 static int ccp_aes_rfc3686_complete(struct crypto_async_request
*async_req
,
146 struct ablkcipher_request
*req
= ablkcipher_request_cast(async_req
);
147 struct ccp_aes_req_ctx
*rctx
= ablkcipher_request_ctx(req
);
149 /* Restore the original pointer */
150 req
->info
= rctx
->rfc3686_info
;
152 return ccp_aes_complete(async_req
, ret
);
155 static int ccp_aes_rfc3686_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
156 unsigned int key_len
)
158 struct ccp_ctx
*ctx
= crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm
));
160 if (key_len
< CTR_RFC3686_NONCE_SIZE
)
163 key_len
-= CTR_RFC3686_NONCE_SIZE
;
164 memcpy(ctx
->u
.aes
.nonce
, key
+ key_len
, CTR_RFC3686_NONCE_SIZE
);
166 return ccp_aes_setkey(tfm
, key
, key_len
);
169 static int ccp_aes_rfc3686_crypt(struct ablkcipher_request
*req
, bool encrypt
)
171 struct ccp_ctx
*ctx
= crypto_tfm_ctx(req
->base
.tfm
);
172 struct ccp_aes_req_ctx
*rctx
= ablkcipher_request_ctx(req
);
175 /* Initialize the CTR block */
176 iv
= rctx
->rfc3686_iv
;
177 memcpy(iv
, ctx
->u
.aes
.nonce
, CTR_RFC3686_NONCE_SIZE
);
179 iv
+= CTR_RFC3686_NONCE_SIZE
;
180 memcpy(iv
, req
->info
, CTR_RFC3686_IV_SIZE
);
182 iv
+= CTR_RFC3686_IV_SIZE
;
183 *(__be32
*)iv
= cpu_to_be32(1);
185 /* Point to the new IV */
186 rctx
->rfc3686_info
= req
->info
;
187 req
->info
= rctx
->rfc3686_iv
;
189 return ccp_aes_crypt(req
, encrypt
);
192 static int ccp_aes_rfc3686_encrypt(struct ablkcipher_request
*req
)
194 return ccp_aes_rfc3686_crypt(req
, true);
197 static int ccp_aes_rfc3686_decrypt(struct ablkcipher_request
*req
)
199 return ccp_aes_rfc3686_crypt(req
, false);
202 static int ccp_aes_rfc3686_cra_init(struct crypto_tfm
*tfm
)
204 struct ccp_ctx
*ctx
= crypto_tfm_ctx(tfm
);
206 ctx
->complete
= ccp_aes_rfc3686_complete
;
207 ctx
->u
.aes
.key_len
= 0;
209 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct ccp_aes_req_ctx
);
214 static void ccp_aes_rfc3686_cra_exit(struct crypto_tfm
*tfm
)
218 static struct crypto_alg ccp_aes_defaults
= {
219 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
221 CRYPTO_ALG_KERN_DRIVER_ONLY
|
222 CRYPTO_ALG_NEED_FALLBACK
,
223 .cra_blocksize
= AES_BLOCK_SIZE
,
224 .cra_ctxsize
= sizeof(struct ccp_ctx
),
225 .cra_priority
= CCP_CRA_PRIORITY
,
226 .cra_type
= &crypto_ablkcipher_type
,
227 .cra_init
= ccp_aes_cra_init
,
228 .cra_exit
= ccp_aes_cra_exit
,
229 .cra_module
= THIS_MODULE
,
231 .setkey
= ccp_aes_setkey
,
232 .encrypt
= ccp_aes_encrypt
,
233 .decrypt
= ccp_aes_decrypt
,
234 .min_keysize
= AES_MIN_KEY_SIZE
,
235 .max_keysize
= AES_MAX_KEY_SIZE
,
239 static struct crypto_alg ccp_aes_rfc3686_defaults
= {
240 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
242 CRYPTO_ALG_KERN_DRIVER_ONLY
|
243 CRYPTO_ALG_NEED_FALLBACK
,
244 .cra_blocksize
= CTR_RFC3686_BLOCK_SIZE
,
245 .cra_ctxsize
= sizeof(struct ccp_ctx
),
246 .cra_priority
= CCP_CRA_PRIORITY
,
247 .cra_type
= &crypto_ablkcipher_type
,
248 .cra_init
= ccp_aes_rfc3686_cra_init
,
249 .cra_exit
= ccp_aes_rfc3686_cra_exit
,
250 .cra_module
= THIS_MODULE
,
252 .setkey
= ccp_aes_rfc3686_setkey
,
253 .encrypt
= ccp_aes_rfc3686_encrypt
,
254 .decrypt
= ccp_aes_rfc3686_decrypt
,
255 .min_keysize
= AES_MIN_KEY_SIZE
+ CTR_RFC3686_NONCE_SIZE
,
256 .max_keysize
= AES_MAX_KEY_SIZE
+ CTR_RFC3686_NONCE_SIZE
,
261 enum ccp_aes_mode mode
;
262 unsigned int version
;
264 const char *driver_name
;
265 unsigned int blocksize
;
267 struct crypto_alg
*alg_defaults
;
270 static struct ccp_aes_def aes_algs
[] = {
272 .mode
= CCP_AES_MODE_ECB
,
273 .version
= CCP_VERSION(3, 0),
275 .driver_name
= "ecb-aes-ccp",
276 .blocksize
= AES_BLOCK_SIZE
,
278 .alg_defaults
= &ccp_aes_defaults
,
281 .mode
= CCP_AES_MODE_CBC
,
282 .version
= CCP_VERSION(3, 0),
284 .driver_name
= "cbc-aes-ccp",
285 .blocksize
= AES_BLOCK_SIZE
,
286 .ivsize
= AES_BLOCK_SIZE
,
287 .alg_defaults
= &ccp_aes_defaults
,
290 .mode
= CCP_AES_MODE_CFB
,
291 .version
= CCP_VERSION(3, 0),
293 .driver_name
= "cfb-aes-ccp",
294 .blocksize
= AES_BLOCK_SIZE
,
295 .ivsize
= AES_BLOCK_SIZE
,
296 .alg_defaults
= &ccp_aes_defaults
,
299 .mode
= CCP_AES_MODE_OFB
,
300 .version
= CCP_VERSION(3, 0),
302 .driver_name
= "ofb-aes-ccp",
304 .ivsize
= AES_BLOCK_SIZE
,
305 .alg_defaults
= &ccp_aes_defaults
,
308 .mode
= CCP_AES_MODE_CTR
,
309 .version
= CCP_VERSION(3, 0),
311 .driver_name
= "ctr-aes-ccp",
313 .ivsize
= AES_BLOCK_SIZE
,
314 .alg_defaults
= &ccp_aes_defaults
,
317 .mode
= CCP_AES_MODE_CTR
,
318 .version
= CCP_VERSION(3, 0),
319 .name
= "rfc3686(ctr(aes))",
320 .driver_name
= "rfc3686-ctr-aes-ccp",
322 .ivsize
= CTR_RFC3686_IV_SIZE
,
323 .alg_defaults
= &ccp_aes_rfc3686_defaults
,
327 static int ccp_register_aes_alg(struct list_head
*head
,
328 const struct ccp_aes_def
*def
)
330 struct ccp_crypto_ablkcipher_alg
*ccp_alg
;
331 struct crypto_alg
*alg
;
334 ccp_alg
= kzalloc(sizeof(*ccp_alg
), GFP_KERNEL
);
338 INIT_LIST_HEAD(&ccp_alg
->entry
);
340 ccp_alg
->mode
= def
->mode
;
342 /* Copy the defaults and override as necessary */
344 *alg
= *def
->alg_defaults
;
345 snprintf(alg
->cra_name
, CRYPTO_MAX_ALG_NAME
, "%s", def
->name
);
346 snprintf(alg
->cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s",
348 alg
->cra_blocksize
= def
->blocksize
;
349 alg
->cra_ablkcipher
.ivsize
= def
->ivsize
;
351 ret
= crypto_register_alg(alg
);
353 pr_err("%s ablkcipher algorithm registration error (%d)\n",
359 list_add(&ccp_alg
->entry
, head
);
364 int ccp_register_aes_algs(struct list_head
*head
)
367 unsigned int ccpversion
= ccp_version();
369 for (i
= 0; i
< ARRAY_SIZE(aes_algs
); i
++) {
370 if (aes_algs
[i
].version
> ccpversion
)
372 ret
= ccp_register_aes_alg(head
, &aes_algs
[i
]);