Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / crypto / ccp / ccp-crypto-aes-cmac.c
blob8e162ad820857a4d6ed221c10b327642fd72dfd2
1 /*
2 * AMD Cryptographic Coprocessor (CCP) AES CMAC crypto API support
4 * Copyright (C) 2013 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/hash.h>
21 #include <crypto/internal/hash.h>
22 #include <crypto/scatterwalk.h>
24 #include "ccp-crypto.h"
27 static int ccp_aes_cmac_complete(struct crypto_async_request *async_req,
28 int ret)
30 struct ahash_request *req = ahash_request_cast(async_req);
31 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
32 struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
33 unsigned int digest_size = crypto_ahash_digestsize(tfm);
35 if (ret)
36 goto e_free;
38 if (rctx->hash_rem) {
39 /* Save remaining data to buffer */
40 unsigned int offset = rctx->nbytes - rctx->hash_rem;
41 scatterwalk_map_and_copy(rctx->buf, rctx->src,
42 offset, rctx->hash_rem, 0);
43 rctx->buf_count = rctx->hash_rem;
44 } else
45 rctx->buf_count = 0;
47 /* Update result area if supplied */
48 if (req->result)
49 memcpy(req->result, rctx->iv, digest_size);
51 e_free:
52 sg_free_table(&rctx->data_sg);
54 return ret;
57 static int ccp_do_cmac_update(struct ahash_request *req, unsigned int nbytes,
58 unsigned int final)
60 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
61 struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
62 struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
63 struct scatterlist *sg, *cmac_key_sg = NULL;
64 unsigned int block_size =
65 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
66 unsigned int need_pad, sg_count;
67 gfp_t gfp;
68 u64 len;
69 int ret;
71 if (!ctx->u.aes.key_len)
72 return -EINVAL;
74 if (nbytes)
75 rctx->null_msg = 0;
77 len = (u64)rctx->buf_count + (u64)nbytes;
79 if (!final && (len <= block_size)) {
80 scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
81 0, nbytes, 0);
82 rctx->buf_count += nbytes;
84 return 0;
87 rctx->src = req->src;
88 rctx->nbytes = nbytes;
90 rctx->final = final;
91 rctx->hash_rem = final ? 0 : len & (block_size - 1);
92 rctx->hash_cnt = len - rctx->hash_rem;
93 if (!final && !rctx->hash_rem) {
94 /* CCP can't do zero length final, so keep some data around */
95 rctx->hash_cnt -= block_size;
96 rctx->hash_rem = block_size;
99 if (final && (rctx->null_msg || (len & (block_size - 1))))
100 need_pad = 1;
101 else
102 need_pad = 0;
104 sg_init_one(&rctx->iv_sg, rctx->iv, sizeof(rctx->iv));
106 /* Build the data scatterlist table - allocate enough entries for all
107 * possible data pieces (buffer, input data, padding)
109 sg_count = (nbytes) ? sg_nents(req->src) + 2 : 2;
110 gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
111 GFP_KERNEL : GFP_ATOMIC;
112 ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
113 if (ret)
114 return ret;
116 sg = NULL;
117 if (rctx->buf_count) {
118 sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
119 sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
122 if (nbytes)
123 sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
125 if (need_pad) {
126 int pad_length = block_size - (len & (block_size - 1));
128 rctx->hash_cnt += pad_length;
130 memset(rctx->pad, 0, sizeof(rctx->pad));
131 rctx->pad[0] = 0x80;
132 sg_init_one(&rctx->pad_sg, rctx->pad, pad_length);
133 sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->pad_sg);
135 if (sg) {
136 sg_mark_end(sg);
137 sg = rctx->data_sg.sgl;
140 /* Initialize the K1/K2 scatterlist */
141 if (final)
142 cmac_key_sg = (need_pad) ? &ctx->u.aes.k2_sg
143 : &ctx->u.aes.k1_sg;
145 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
146 INIT_LIST_HEAD(&rctx->cmd.entry);
147 rctx->cmd.engine = CCP_ENGINE_AES;
148 rctx->cmd.u.aes.type = ctx->u.aes.type;
149 rctx->cmd.u.aes.mode = ctx->u.aes.mode;
150 rctx->cmd.u.aes.action = CCP_AES_ACTION_ENCRYPT;
151 rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
152 rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
153 rctx->cmd.u.aes.iv = &rctx->iv_sg;
154 rctx->cmd.u.aes.iv_len = AES_BLOCK_SIZE;
155 rctx->cmd.u.aes.src = sg;
156 rctx->cmd.u.aes.src_len = rctx->hash_cnt;
157 rctx->cmd.u.aes.dst = NULL;
158 rctx->cmd.u.aes.cmac_key = cmac_key_sg;
159 rctx->cmd.u.aes.cmac_key_len = ctx->u.aes.kn_len;
160 rctx->cmd.u.aes.cmac_final = final;
162 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
164 return ret;
167 static int ccp_aes_cmac_init(struct ahash_request *req)
169 struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
171 memset(rctx, 0, sizeof(*rctx));
173 rctx->null_msg = 1;
175 return 0;
178 static int ccp_aes_cmac_update(struct ahash_request *req)
180 return ccp_do_cmac_update(req, req->nbytes, 0);
183 static int ccp_aes_cmac_final(struct ahash_request *req)
185 return ccp_do_cmac_update(req, 0, 1);
188 static int ccp_aes_cmac_finup(struct ahash_request *req)
190 return ccp_do_cmac_update(req, req->nbytes, 1);
193 static int ccp_aes_cmac_digest(struct ahash_request *req)
195 int ret;
197 ret = ccp_aes_cmac_init(req);
198 if (ret)
199 return ret;
201 return ccp_aes_cmac_finup(req);
204 static int ccp_aes_cmac_setkey(struct crypto_ahash *tfm, const u8 *key,
205 unsigned int key_len)
207 struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
208 struct ccp_crypto_ahash_alg *alg =
209 ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
210 u64 k0_hi, k0_lo, k1_hi, k1_lo, k2_hi, k2_lo;
211 u64 rb_hi = 0x00, rb_lo = 0x87;
212 __be64 *gk;
213 int ret;
215 switch (key_len) {
216 case AES_KEYSIZE_128:
217 ctx->u.aes.type = CCP_AES_TYPE_128;
218 break;
219 case AES_KEYSIZE_192:
220 ctx->u.aes.type = CCP_AES_TYPE_192;
221 break;
222 case AES_KEYSIZE_256:
223 ctx->u.aes.type = CCP_AES_TYPE_256;
224 break;
225 default:
226 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
227 return -EINVAL;
229 ctx->u.aes.mode = alg->mode;
231 /* Set to zero until complete */
232 ctx->u.aes.key_len = 0;
234 /* Set the key for the AES cipher used to generate the keys */
235 ret = crypto_cipher_setkey(ctx->u.aes.tfm_cipher, key, key_len);
236 if (ret)
237 return ret;
239 /* Encrypt a block of zeroes - use key area in context */
240 memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key));
241 crypto_cipher_encrypt_one(ctx->u.aes.tfm_cipher, ctx->u.aes.key,
242 ctx->u.aes.key);
244 /* Generate K1 and K2 */
245 k0_hi = be64_to_cpu(*((__be64 *)ctx->u.aes.key));
246 k0_lo = be64_to_cpu(*((__be64 *)ctx->u.aes.key + 1));
248 k1_hi = (k0_hi << 1) | (k0_lo >> 63);
249 k1_lo = k0_lo << 1;
250 if (ctx->u.aes.key[0] & 0x80) {
251 k1_hi ^= rb_hi;
252 k1_lo ^= rb_lo;
254 gk = (__be64 *)ctx->u.aes.k1;
255 *gk = cpu_to_be64(k1_hi);
256 gk++;
257 *gk = cpu_to_be64(k1_lo);
259 k2_hi = (k1_hi << 1) | (k1_lo >> 63);
260 k2_lo = k1_lo << 1;
261 if (ctx->u.aes.k1[0] & 0x80) {
262 k2_hi ^= rb_hi;
263 k2_lo ^= rb_lo;
265 gk = (__be64 *)ctx->u.aes.k2;
266 *gk = cpu_to_be64(k2_hi);
267 gk++;
268 *gk = cpu_to_be64(k2_lo);
270 ctx->u.aes.kn_len = sizeof(ctx->u.aes.k1);
271 sg_init_one(&ctx->u.aes.k1_sg, ctx->u.aes.k1, sizeof(ctx->u.aes.k1));
272 sg_init_one(&ctx->u.aes.k2_sg, ctx->u.aes.k2, sizeof(ctx->u.aes.k2));
274 /* Save the supplied key */
275 memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key));
276 memcpy(ctx->u.aes.key, key, key_len);
277 ctx->u.aes.key_len = key_len;
278 sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
280 return ret;
283 static int ccp_aes_cmac_cra_init(struct crypto_tfm *tfm)
285 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
286 struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
287 struct crypto_cipher *cipher_tfm;
289 ctx->complete = ccp_aes_cmac_complete;
290 ctx->u.aes.key_len = 0;
292 crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_aes_cmac_req_ctx));
294 cipher_tfm = crypto_alloc_cipher("aes", 0,
295 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
296 if (IS_ERR(cipher_tfm)) {
297 pr_warn("could not load aes cipher driver\n");
298 return PTR_ERR(cipher_tfm);
300 ctx->u.aes.tfm_cipher = cipher_tfm;
302 return 0;
305 static void ccp_aes_cmac_cra_exit(struct crypto_tfm *tfm)
307 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
309 if (ctx->u.aes.tfm_cipher)
310 crypto_free_cipher(ctx->u.aes.tfm_cipher);
311 ctx->u.aes.tfm_cipher = NULL;
314 int ccp_register_aes_cmac_algs(struct list_head *head)
316 struct ccp_crypto_ahash_alg *ccp_alg;
317 struct ahash_alg *alg;
318 struct hash_alg_common *halg;
319 struct crypto_alg *base;
320 int ret;
322 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
323 if (!ccp_alg)
324 return -ENOMEM;
326 INIT_LIST_HEAD(&ccp_alg->entry);
327 ccp_alg->mode = CCP_AES_MODE_CMAC;
329 alg = &ccp_alg->alg;
330 alg->init = ccp_aes_cmac_init;
331 alg->update = ccp_aes_cmac_update;
332 alg->final = ccp_aes_cmac_final;
333 alg->finup = ccp_aes_cmac_finup;
334 alg->digest = ccp_aes_cmac_digest;
335 alg->setkey = ccp_aes_cmac_setkey;
337 halg = &alg->halg;
338 halg->digestsize = AES_BLOCK_SIZE;
340 base = &halg->base;
341 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "cmac(aes)");
342 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "cmac-aes-ccp");
343 base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
344 CRYPTO_ALG_KERN_DRIVER_ONLY |
345 CRYPTO_ALG_NEED_FALLBACK;
346 base->cra_blocksize = AES_BLOCK_SIZE;
347 base->cra_ctxsize = sizeof(struct ccp_ctx);
348 base->cra_priority = CCP_CRA_PRIORITY;
349 base->cra_type = &crypto_ahash_type;
350 base->cra_init = ccp_aes_cmac_cra_init;
351 base->cra_exit = ccp_aes_cmac_cra_exit;
352 base->cra_module = THIS_MODULE;
354 ret = crypto_register_ahash(alg);
355 if (ret) {
356 pr_err("%s ahash algorithm registration error (%d)\n",
357 base->cra_name, ret);
358 kfree(ccp_alg);
359 return ret;
362 list_add(&ccp_alg->entry, head);
364 return 0;