blob: 3d9acc53d2473818d1e37c4af432a4afcbdf4ec4 [file] [log] [blame]
Tom Lendacky7c185372013-11-12 11:46:34 -06001/*
2 * AMD Cryptographic Coprocessor (CCP) AES CMAC 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#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/hash.h>
21#include <crypto/internal/hash.h>
22#include <crypto/scatterwalk.h>
23
24#include "ccp-crypto.h"
25
Tom Lendacky7c185372013-11-12 11:46:34 -060026static int ccp_aes_cmac_complete(struct crypto_async_request *async_req,
27 int ret)
28{
29 struct ahash_request *req = ahash_request_cast(async_req);
30 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
31 struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
32 unsigned int digest_size = crypto_ahash_digestsize(tfm);
33
34 if (ret)
35 goto e_free;
36
37 if (rctx->hash_rem) {
38 /* Save remaining data to buffer */
Tom Lendacky81a59f02014-01-06 13:34:17 -060039 unsigned int offset = rctx->nbytes - rctx->hash_rem;
Tom Lendacky8db88462015-02-03 13:07:05 -060040
Tom Lendacky81a59f02014-01-06 13:34:17 -060041 scatterwalk_map_and_copy(rctx->buf, rctx->src,
42 offset, rctx->hash_rem, 0);
Tom Lendacky7c185372013-11-12 11:46:34 -060043 rctx->buf_count = rctx->hash_rem;
Tom Lendacky8db88462015-02-03 13:07:05 -060044 } else {
Tom Lendacky7c185372013-11-12 11:46:34 -060045 rctx->buf_count = 0;
Tom Lendacky8db88462015-02-03 13:07:05 -060046 }
Tom Lendacky7c185372013-11-12 11:46:34 -060047
Tom Lendacky393897c2014-01-06 13:34:11 -060048 /* Update result area if supplied */
49 if (req->result)
50 memcpy(req->result, rctx->iv, digest_size);
Tom Lendacky7c185372013-11-12 11:46:34 -060051
52e_free:
53 sg_free_table(&rctx->data_sg);
54
55 return ret;
56}
57
58static int ccp_do_cmac_update(struct ahash_request *req, unsigned int nbytes,
59 unsigned int final)
60{
61 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
62 struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
63 struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
64 struct scatterlist *sg, *cmac_key_sg = NULL;
65 unsigned int block_size =
66 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
Tom Lendacky81a59f02014-01-06 13:34:17 -060067 unsigned int need_pad, sg_count;
Tom Lendacky5258de82014-01-06 13:33:59 -060068 gfp_t gfp;
Tom Lendacky81a59f02014-01-06 13:34:17 -060069 u64 len;
Tom Lendacky7c185372013-11-12 11:46:34 -060070 int ret;
71
Tom Lendacky369f3da2013-12-10 10:38:44 -060072 if (!ctx->u.aes.key_len)
Tom Lendacky7c185372013-11-12 11:46:34 -060073 return -EINVAL;
Tom Lendacky7c185372013-11-12 11:46:34 -060074
75 if (nbytes)
76 rctx->null_msg = 0;
77
Tom Lendacky81a59f02014-01-06 13:34:17 -060078 len = (u64)rctx->buf_count + (u64)nbytes;
79
80 if (!final && (len <= block_size)) {
Tom Lendacky7c185372013-11-12 11:46:34 -060081 scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
82 0, nbytes, 0);
83 rctx->buf_count += nbytes;
84
85 return 0;
86 }
87
Tom Lendacky81a59f02014-01-06 13:34:17 -060088 rctx->src = req->src;
89 rctx->nbytes = nbytes;
Tom Lendacky7c185372013-11-12 11:46:34 -060090
91 rctx->final = final;
Tom Lendacky81a59f02014-01-06 13:34:17 -060092 rctx->hash_rem = final ? 0 : len & (block_size - 1);
93 rctx->hash_cnt = len - rctx->hash_rem;
94 if (!final && !rctx->hash_rem) {
Tom Lendacky7c185372013-11-12 11:46:34 -060095 /* CCP can't do zero length final, so keep some data around */
96 rctx->hash_cnt -= block_size;
97 rctx->hash_rem = block_size;
98 }
99
100 if (final && (rctx->null_msg || (len & (block_size - 1))))
101 need_pad = 1;
102 else
103 need_pad = 0;
104
105 sg_init_one(&rctx->iv_sg, rctx->iv, sizeof(rctx->iv));
106
107 /* Build the data scatterlist table - allocate enough entries for all
108 * possible data pieces (buffer, input data, padding)
109 */
110 sg_count = (nbytes) ? sg_nents(req->src) + 2 : 2;
Tom Lendacky5258de82014-01-06 13:33:59 -0600111 gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
112 GFP_KERNEL : GFP_ATOMIC;
113 ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
Tom Lendacky7c185372013-11-12 11:46:34 -0600114 if (ret)
115 return ret;
116
117 sg = NULL;
118 if (rctx->buf_count) {
119 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
120 sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
Tom Lendacky355eba52015-10-01 16:32:31 -0500121 if (!sg) {
122 ret = -EINVAL;
123 goto e_free;
124 }
Tom Lendacky7c185372013-11-12 11:46:34 -0600125 }
126
Tom Lendacky355eba52015-10-01 16:32:31 -0500127 if (nbytes) {
Tom Lendacky7c185372013-11-12 11:46:34 -0600128 sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
Tom Lendacky355eba52015-10-01 16:32:31 -0500129 if (!sg) {
130 ret = -EINVAL;
131 goto e_free;
132 }
133 }
Tom Lendacky7c185372013-11-12 11:46:34 -0600134
135 if (need_pad) {
136 int pad_length = block_size - (len & (block_size - 1));
137
138 rctx->hash_cnt += pad_length;
139
140 memset(rctx->pad, 0, sizeof(rctx->pad));
141 rctx->pad[0] = 0x80;
142 sg_init_one(&rctx->pad_sg, rctx->pad, pad_length);
143 sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->pad_sg);
Tom Lendacky355eba52015-10-01 16:32:31 -0500144 if (!sg) {
145 ret = -EINVAL;
146 goto e_free;
147 }
Tom Lendacky7c185372013-11-12 11:46:34 -0600148 }
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600149 if (sg) {
Tom Lendacky7c185372013-11-12 11:46:34 -0600150 sg_mark_end(sg);
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600151 sg = rctx->data_sg.sgl;
152 }
Tom Lendacky7c185372013-11-12 11:46:34 -0600153
154 /* Initialize the K1/K2 scatterlist */
155 if (final)
156 cmac_key_sg = (need_pad) ? &ctx->u.aes.k2_sg
157 : &ctx->u.aes.k1_sg;
158
159 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
160 INIT_LIST_HEAD(&rctx->cmd.entry);
161 rctx->cmd.engine = CCP_ENGINE_AES;
162 rctx->cmd.u.aes.type = ctx->u.aes.type;
163 rctx->cmd.u.aes.mode = ctx->u.aes.mode;
164 rctx->cmd.u.aes.action = CCP_AES_ACTION_ENCRYPT;
165 rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
166 rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
167 rctx->cmd.u.aes.iv = &rctx->iv_sg;
168 rctx->cmd.u.aes.iv_len = AES_BLOCK_SIZE;
Tom Lendacky77dc4a52014-01-06 13:34:05 -0600169 rctx->cmd.u.aes.src = sg;
Tom Lendacky7c185372013-11-12 11:46:34 -0600170 rctx->cmd.u.aes.src_len = rctx->hash_cnt;
171 rctx->cmd.u.aes.dst = NULL;
172 rctx->cmd.u.aes.cmac_key = cmac_key_sg;
173 rctx->cmd.u.aes.cmac_key_len = ctx->u.aes.kn_len;
174 rctx->cmd.u.aes.cmac_final = final;
175
176 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
177
178 return ret;
Tom Lendacky355eba52015-10-01 16:32:31 -0500179
180e_free:
181 sg_free_table(&rctx->data_sg);
182
183 return ret;
Tom Lendacky7c185372013-11-12 11:46:34 -0600184}
185
186static int ccp_aes_cmac_init(struct ahash_request *req)
187{
188 struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
189
190 memset(rctx, 0, sizeof(*rctx));
191
192 rctx->null_msg = 1;
193
194 return 0;
195}
196
197static int ccp_aes_cmac_update(struct ahash_request *req)
198{
199 return ccp_do_cmac_update(req, req->nbytes, 0);
200}
201
202static int ccp_aes_cmac_final(struct ahash_request *req)
203{
204 return ccp_do_cmac_update(req, 0, 1);
205}
206
207static int ccp_aes_cmac_finup(struct ahash_request *req)
208{
209 return ccp_do_cmac_update(req, req->nbytes, 1);
210}
211
212static int ccp_aes_cmac_digest(struct ahash_request *req)
213{
214 int ret;
215
216 ret = ccp_aes_cmac_init(req);
217 if (ret)
218 return ret;
219
Tom Lendacky82d15852014-01-06 13:34:23 -0600220 return ccp_aes_cmac_finup(req);
Tom Lendacky7c185372013-11-12 11:46:34 -0600221}
222
Tom Lendacky952bce92016-01-12 11:17:38 -0600223static int ccp_aes_cmac_export(struct ahash_request *req, void *out)
224{
225 struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
Tom Lendackyb31dde22016-02-02 11:38:21 -0600226 struct ccp_aes_cmac_exp_ctx state;
Tom Lendacky952bce92016-01-12 11:17:38 -0600227
Tom Lendackyb31dde22016-02-02 11:38:21 -0600228 state.null_msg = rctx->null_msg;
229 memcpy(state.iv, rctx->iv, sizeof(state.iv));
230 state.buf_count = rctx->buf_count;
231 memcpy(state.buf, rctx->buf, sizeof(state.buf));
232
233 /* 'out' may not be aligned so memcpy from local variable */
234 memcpy(out, &state, sizeof(state));
Tom Lendacky952bce92016-01-12 11:17:38 -0600235
236 return 0;
237}
238
239static int ccp_aes_cmac_import(struct ahash_request *req, const void *in)
240{
241 struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
Tom Lendackyb31dde22016-02-02 11:38:21 -0600242 struct ccp_aes_cmac_exp_ctx state;
Tom Lendacky952bce92016-01-12 11:17:38 -0600243
Tom Lendackyb31dde22016-02-02 11:38:21 -0600244 /* 'in' may not be aligned so memcpy to local variable */
245 memcpy(&state, in, sizeof(state));
246
Tom Lendackyce0ae262016-02-25 16:48:13 -0600247 memset(rctx, 0, sizeof(*rctx));
Tom Lendackyb31dde22016-02-02 11:38:21 -0600248 rctx->null_msg = state.null_msg;
249 memcpy(rctx->iv, state.iv, sizeof(rctx->iv));
250 rctx->buf_count = state.buf_count;
251 memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
Tom Lendacky952bce92016-01-12 11:17:38 -0600252
253 return 0;
254}
255
Tom Lendacky7c185372013-11-12 11:46:34 -0600256static int ccp_aes_cmac_setkey(struct crypto_ahash *tfm, const u8 *key,
Tom Lendacky8db88462015-02-03 13:07:05 -0600257 unsigned int key_len)
Tom Lendacky7c185372013-11-12 11:46:34 -0600258{
259 struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
260 struct ccp_crypto_ahash_alg *alg =
261 ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
262 u64 k0_hi, k0_lo, k1_hi, k1_lo, k2_hi, k2_lo;
263 u64 rb_hi = 0x00, rb_lo = 0x87;
264 __be64 *gk;
265 int ret;
266
267 switch (key_len) {
268 case AES_KEYSIZE_128:
269 ctx->u.aes.type = CCP_AES_TYPE_128;
270 break;
271 case AES_KEYSIZE_192:
272 ctx->u.aes.type = CCP_AES_TYPE_192;
273 break;
274 case AES_KEYSIZE_256:
275 ctx->u.aes.type = CCP_AES_TYPE_256;
276 break;
277 default:
278 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
279 return -EINVAL;
280 }
281 ctx->u.aes.mode = alg->mode;
282
283 /* Set to zero until complete */
284 ctx->u.aes.key_len = 0;
285
286 /* Set the key for the AES cipher used to generate the keys */
287 ret = crypto_cipher_setkey(ctx->u.aes.tfm_cipher, key, key_len);
288 if (ret)
289 return ret;
290
291 /* Encrypt a block of zeroes - use key area in context */
292 memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key));
293 crypto_cipher_encrypt_one(ctx->u.aes.tfm_cipher, ctx->u.aes.key,
294 ctx->u.aes.key);
295
296 /* Generate K1 and K2 */
297 k0_hi = be64_to_cpu(*((__be64 *)ctx->u.aes.key));
298 k0_lo = be64_to_cpu(*((__be64 *)ctx->u.aes.key + 1));
299
300 k1_hi = (k0_hi << 1) | (k0_lo >> 63);
301 k1_lo = k0_lo << 1;
302 if (ctx->u.aes.key[0] & 0x80) {
303 k1_hi ^= rb_hi;
304 k1_lo ^= rb_lo;
305 }
306 gk = (__be64 *)ctx->u.aes.k1;
307 *gk = cpu_to_be64(k1_hi);
308 gk++;
309 *gk = cpu_to_be64(k1_lo);
310
311 k2_hi = (k1_hi << 1) | (k1_lo >> 63);
312 k2_lo = k1_lo << 1;
313 if (ctx->u.aes.k1[0] & 0x80) {
314 k2_hi ^= rb_hi;
315 k2_lo ^= rb_lo;
316 }
317 gk = (__be64 *)ctx->u.aes.k2;
318 *gk = cpu_to_be64(k2_hi);
319 gk++;
320 *gk = cpu_to_be64(k2_lo);
321
322 ctx->u.aes.kn_len = sizeof(ctx->u.aes.k1);
323 sg_init_one(&ctx->u.aes.k1_sg, ctx->u.aes.k1, sizeof(ctx->u.aes.k1));
324 sg_init_one(&ctx->u.aes.k2_sg, ctx->u.aes.k2, sizeof(ctx->u.aes.k2));
325
326 /* Save the supplied key */
327 memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key));
328 memcpy(ctx->u.aes.key, key, key_len);
329 ctx->u.aes.key_len = key_len;
330 sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
331
332 return ret;
333}
334
335static int ccp_aes_cmac_cra_init(struct crypto_tfm *tfm)
336{
337 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
338 struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
339 struct crypto_cipher *cipher_tfm;
340
341 ctx->complete = ccp_aes_cmac_complete;
342 ctx->u.aes.key_len = 0;
343
344 crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_aes_cmac_req_ctx));
345
346 cipher_tfm = crypto_alloc_cipher("aes", 0,
Tom Lendacky8db88462015-02-03 13:07:05 -0600347 CRYPTO_ALG_ASYNC |
348 CRYPTO_ALG_NEED_FALLBACK);
Tom Lendacky7c185372013-11-12 11:46:34 -0600349 if (IS_ERR(cipher_tfm)) {
350 pr_warn("could not load aes cipher driver\n");
351 return PTR_ERR(cipher_tfm);
352 }
353 ctx->u.aes.tfm_cipher = cipher_tfm;
354
355 return 0;
356}
357
358static void ccp_aes_cmac_cra_exit(struct crypto_tfm *tfm)
359{
360 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
361
362 if (ctx->u.aes.tfm_cipher)
363 crypto_free_cipher(ctx->u.aes.tfm_cipher);
364 ctx->u.aes.tfm_cipher = NULL;
365}
366
367int ccp_register_aes_cmac_algs(struct list_head *head)
368{
369 struct ccp_crypto_ahash_alg *ccp_alg;
370 struct ahash_alg *alg;
371 struct hash_alg_common *halg;
372 struct crypto_alg *base;
373 int ret;
374
375 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
376 if (!ccp_alg)
377 return -ENOMEM;
378
379 INIT_LIST_HEAD(&ccp_alg->entry);
380 ccp_alg->mode = CCP_AES_MODE_CMAC;
381
382 alg = &ccp_alg->alg;
383 alg->init = ccp_aes_cmac_init;
384 alg->update = ccp_aes_cmac_update;
385 alg->final = ccp_aes_cmac_final;
386 alg->finup = ccp_aes_cmac_finup;
387 alg->digest = ccp_aes_cmac_digest;
Tom Lendacky952bce92016-01-12 11:17:38 -0600388 alg->export = ccp_aes_cmac_export;
389 alg->import = ccp_aes_cmac_import;
Tom Lendacky7c185372013-11-12 11:46:34 -0600390 alg->setkey = ccp_aes_cmac_setkey;
391
392 halg = &alg->halg;
393 halg->digestsize = AES_BLOCK_SIZE;
Tom Lendackyd1662162016-01-29 12:45:14 -0600394 halg->statesize = sizeof(struct ccp_aes_cmac_exp_ctx);
Tom Lendacky7c185372013-11-12 11:46:34 -0600395
396 base = &halg->base;
397 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "cmac(aes)");
398 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "cmac-aes-ccp");
399 base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
400 CRYPTO_ALG_KERN_DRIVER_ONLY |
401 CRYPTO_ALG_NEED_FALLBACK;
402 base->cra_blocksize = AES_BLOCK_SIZE;
403 base->cra_ctxsize = sizeof(struct ccp_ctx);
404 base->cra_priority = CCP_CRA_PRIORITY;
405 base->cra_type = &crypto_ahash_type;
406 base->cra_init = ccp_aes_cmac_cra_init;
407 base->cra_exit = ccp_aes_cmac_cra_exit;
408 base->cra_module = THIS_MODULE;
409
410 ret = crypto_register_ahash(alg);
411 if (ret) {
412 pr_err("%s ahash algorithm registration error (%d)\n",
Tom Lendacky8db88462015-02-03 13:07:05 -0600413 base->cra_name, ret);
Tom Lendacky7c185372013-11-12 11:46:34 -0600414 kfree(ccp_alg);
415 return ret;
416 }
417
418 list_add(&ccp_alg->entry, head);
419
420 return 0;
421}