]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - crypto/ccm.c
crypto: caam - fix state buffer DMA (un)mapping
[thirdparty/kernel/stable.git] / crypto / ccm.c
CommitLineData
4a49b499
JL
1/*
2 * CCM: Counter with CBC-MAC
3 *
4 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <crypto/internal/aead.h>
f15f05b0 14#include <crypto/internal/hash.h>
4a49b499
JL
15#include <crypto/internal/skcipher.h>
16#include <crypto/scatterwalk.h>
17#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/slab.h>
22
23#include "internal.h"
24
25struct ccm_instance_ctx {
26 struct crypto_skcipher_spawn ctr;
f15f05b0 27 struct crypto_ahash_spawn mac;
4a49b499
JL
28};
29
30struct crypto_ccm_ctx {
f15f05b0 31 struct crypto_ahash *mac;
464b93a3 32 struct crypto_skcipher *ctr;
4a49b499
JL
33};
34
35struct crypto_rfc4309_ctx {
36 struct crypto_aead *child;
37 u8 nonce[3];
38};
39
81c4c35e
HX
40struct crypto_rfc4309_req_ctx {
41 struct scatterlist src[3];
42 struct scatterlist dst[3];
43 struct aead_request subreq;
44};
45
4a49b499
JL
46struct crypto_ccm_req_priv_ctx {
47 u8 odata[16];
4a49b499 48 u8 auth_tag[16];
4a49b499 49 u32 flags;
81c4c35e
HX
50 struct scatterlist src[3];
51 struct scatterlist dst[3];
464b93a3 52 struct skcipher_request skreq;
4a49b499
JL
53};
54
f15f05b0
AB
55struct cbcmac_tfm_ctx {
56 struct crypto_cipher *child;
57};
58
59struct cbcmac_desc_ctx {
60 unsigned int len;
61 u8 dg[];
62};
63
4a49b499
JL
64static inline struct crypto_ccm_req_priv_ctx *crypto_ccm_reqctx(
65 struct aead_request *req)
66{
67 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
68
69 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
70}
71
72static int set_msg_len(u8 *block, unsigned int msglen, int csize)
73{
74 __be32 data;
75
76 memset(block, 0, csize);
77 block += csize;
78
79 if (csize >= 4)
80 csize = 4;
81 else if (msglen > (1 << (8 * csize)))
82 return -EOVERFLOW;
83
84 data = cpu_to_be32(msglen);
85 memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
86
87 return 0;
88}
89
90static int crypto_ccm_setkey(struct crypto_aead *aead, const u8 *key,
91 unsigned int keylen)
92{
93 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
464b93a3 94 struct crypto_skcipher *ctr = ctx->ctr;
f15f05b0 95 struct crypto_ahash *mac = ctx->mac;
4a49b499
JL
96 int err = 0;
97
464b93a3
HX
98 crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
99 crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
100 CRYPTO_TFM_REQ_MASK);
101 err = crypto_skcipher_setkey(ctr, key, keylen);
102 crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
4a49b499
JL
103 CRYPTO_TFM_RES_MASK);
104 if (err)
105 goto out;
106
f15f05b0
AB
107 crypto_ahash_clear_flags(mac, CRYPTO_TFM_REQ_MASK);
108 crypto_ahash_set_flags(mac, crypto_aead_get_flags(aead) &
4a49b499 109 CRYPTO_TFM_REQ_MASK);
f15f05b0
AB
110 err = crypto_ahash_setkey(mac, key, keylen);
111 crypto_aead_set_flags(aead, crypto_ahash_get_flags(mac) &
4a49b499
JL
112 CRYPTO_TFM_RES_MASK);
113
114out:
115 return err;
116}
117
118static int crypto_ccm_setauthsize(struct crypto_aead *tfm,
119 unsigned int authsize)
120{
121 switch (authsize) {
122 case 4:
123 case 6:
124 case 8:
125 case 10:
126 case 12:
127 case 14:
128 case 16:
129 break;
130 default:
131 return -EINVAL;
132 }
133
134 return 0;
135}
136
137static int format_input(u8 *info, struct aead_request *req,
138 unsigned int cryptlen)
139{
140 struct crypto_aead *aead = crypto_aead_reqtfm(req);
141 unsigned int lp = req->iv[0];
142 unsigned int l = lp + 1;
143 unsigned int m;
144
145 m = crypto_aead_authsize(aead);
146
147 memcpy(info, req->iv, 16);
148
149 /* format control info per RFC 3610 and
150 * NIST Special Publication 800-38C
151 */
152 *info |= (8 * ((m - 2) / 2));
153 if (req->assoclen)
154 *info |= 64;
155
156 return set_msg_len(info + 16 - l, cryptlen, l);
157}
158
159static int format_adata(u8 *adata, unsigned int a)
160{
161 int len = 0;
162
163 /* add control info for associated data
164 * RFC 3610 and NIST Special Publication 800-38C
165 */
166 if (a < 65280) {
167 *(__be16 *)adata = cpu_to_be16(a);
168 len = 2;
169 } else {
170 *(__be16 *)adata = cpu_to_be16(0xfffe);
171 *(__be32 *)&adata[2] = cpu_to_be32(a);
172 len = 6;
173 }
174
175 return len;
176}
177
4a49b499
JL
178static int crypto_ccm_auth(struct aead_request *req, struct scatterlist *plain,
179 unsigned int cryptlen)
180{
f15f05b0 181 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
4a49b499
JL
182 struct crypto_aead *aead = crypto_aead_reqtfm(req);
183 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
f15f05b0 184 AHASH_REQUEST_ON_STACK(ahreq, ctx->mac);
4a49b499 185 unsigned int assoclen = req->assoclen;
f15f05b0
AB
186 struct scatterlist sg[3];
187 u8 odata[16];
188 u8 idata[16];
189 int ilen, err;
4a49b499
JL
190
191 /* format control data for input */
192 err = format_input(odata, req, cryptlen);
193 if (err)
194 goto out;
195
f15f05b0
AB
196 sg_init_table(sg, 3);
197 sg_set_buf(&sg[0], odata, 16);
4a49b499
JL
198
199 /* format associated data and compute into mac */
200 if (assoclen) {
f15f05b0
AB
201 ilen = format_adata(idata, assoclen);
202 sg_set_buf(&sg[1], idata, ilen);
203 sg_chain(sg, 3, req->src);
516280e7 204 } else {
f15f05b0
AB
205 ilen = 0;
206 sg_chain(sg, 2, req->src);
4a49b499
JL
207 }
208
f15f05b0
AB
209 ahash_request_set_tfm(ahreq, ctx->mac);
210 ahash_request_set_callback(ahreq, pctx->flags, NULL, NULL);
211 ahash_request_set_crypt(ahreq, sg, NULL, assoclen + ilen + 16);
212 err = crypto_ahash_init(ahreq);
213 if (err)
214 goto out;
215 err = crypto_ahash_update(ahreq);
216 if (err)
217 goto out;
4a49b499 218
f15f05b0
AB
219 /* we need to pad the MAC input to a round multiple of the block size */
220 ilen = 16 - (assoclen + ilen) % 16;
221 if (ilen < 16) {
222 memset(idata, 0, ilen);
223 sg_init_table(sg, 2);
224 sg_set_buf(&sg[0], idata, ilen);
225 if (plain)
226 sg_chain(sg, 2, plain);
227 plain = sg;
228 cryptlen += ilen;
229 }
230
231 ahash_request_set_crypt(ahreq, plain, pctx->odata, cryptlen);
232 err = crypto_ahash_finup(ahreq);
4a49b499
JL
233out:
234 return err;
235}
236
237static void crypto_ccm_encrypt_done(struct crypto_async_request *areq, int err)
238{
239 struct aead_request *req = areq->data;
240 struct crypto_aead *aead = crypto_aead_reqtfm(req);
241 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
242 u8 *odata = pctx->odata;
243
244 if (!err)
81c4c35e
HX
245 scatterwalk_map_and_copy(odata, req->dst,
246 req->assoclen + req->cryptlen,
4a49b499
JL
247 crypto_aead_authsize(aead), 1);
248 aead_request_complete(req, err);
249}
250
251static inline int crypto_ccm_check_iv(const u8 *iv)
252{
253 /* 2 <= L <= 8, so 1 <= L' <= 7. */
254 if (1 > iv[0] || iv[0] > 7)
255 return -EINVAL;
256
257 return 0;
258}
259
81c4c35e
HX
260static int crypto_ccm_init_crypt(struct aead_request *req, u8 *tag)
261{
262 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
263 struct scatterlist *sg;
264 u8 *iv = req->iv;
265 int err;
266
267 err = crypto_ccm_check_iv(iv);
268 if (err)
269 return err;
270
271 pctx->flags = aead_request_flags(req);
272
273 /* Note: rfc 3610 and NIST 800-38C require counter of
274 * zero to encrypt auth tag.
275 */
276 memset(iv + 15 - iv[0], 0, iv[0] + 1);
277
278 sg_init_table(pctx->src, 3);
279 sg_set_buf(pctx->src, tag, 16);
280 sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
281 if (sg != pctx->src + 1)
282 sg_chain(pctx->src, 2, sg);
283
284 if (req->src != req->dst) {
285 sg_init_table(pctx->dst, 3);
286 sg_set_buf(pctx->dst, tag, 16);
287 sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
288 if (sg != pctx->dst + 1)
289 sg_chain(pctx->dst, 2, sg);
290 }
291
292 return 0;
293}
294
4a49b499
JL
295static int crypto_ccm_encrypt(struct aead_request *req)
296{
297 struct crypto_aead *aead = crypto_aead_reqtfm(req);
298 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
299 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
464b93a3 300 struct skcipher_request *skreq = &pctx->skreq;
4a49b499
JL
301 struct scatterlist *dst;
302 unsigned int cryptlen = req->cryptlen;
303 u8 *odata = pctx->odata;
304 u8 *iv = req->iv;
305 int err;
306
81c4c35e 307 err = crypto_ccm_init_crypt(req, odata);
4a49b499
JL
308 if (err)
309 return err;
310
81c4c35e 311 err = crypto_ccm_auth(req, sg_next(pctx->src), cryptlen);
4a49b499
JL
312 if (err)
313 return err;
314
4a49b499 315 dst = pctx->src;
81c4c35e 316 if (req->src != req->dst)
4a49b499 317 dst = pctx->dst;
4a49b499 318
464b93a3
HX
319 skcipher_request_set_tfm(skreq, ctx->ctr);
320 skcipher_request_set_callback(skreq, pctx->flags,
321 crypto_ccm_encrypt_done, req);
322 skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
323 err = crypto_skcipher_encrypt(skreq);
4a49b499
JL
324 if (err)
325 return err;
326
327 /* copy authtag to end of dst */
81c4c35e 328 scatterwalk_map_and_copy(odata, sg_next(dst), cryptlen,
4a49b499
JL
329 crypto_aead_authsize(aead), 1);
330 return err;
331}
332
333static void crypto_ccm_decrypt_done(struct crypto_async_request *areq,
334 int err)
335{
336 struct aead_request *req = areq->data;
337 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
338 struct crypto_aead *aead = crypto_aead_reqtfm(req);
339 unsigned int authsize = crypto_aead_authsize(aead);
340 unsigned int cryptlen = req->cryptlen - authsize;
81c4c35e
HX
341 struct scatterlist *dst;
342
343 pctx->flags = 0;
344
345 dst = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
4a49b499
JL
346
347 if (!err) {
81c4c35e 348 err = crypto_ccm_auth(req, dst, cryptlen);
6bf37e5a 349 if (!err && crypto_memneq(pctx->auth_tag, pctx->odata, authsize))
4a49b499
JL
350 err = -EBADMSG;
351 }
352 aead_request_complete(req, err);
353}
354
355static int crypto_ccm_decrypt(struct aead_request *req)
356{
357 struct crypto_aead *aead = crypto_aead_reqtfm(req);
358 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
359 struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
464b93a3 360 struct skcipher_request *skreq = &pctx->skreq;
4a49b499
JL
361 struct scatterlist *dst;
362 unsigned int authsize = crypto_aead_authsize(aead);
363 unsigned int cryptlen = req->cryptlen;
364 u8 *authtag = pctx->auth_tag;
365 u8 *odata = pctx->odata;
366 u8 *iv = req->iv;
367 int err;
368
4a49b499
JL
369 cryptlen -= authsize;
370
81c4c35e 371 err = crypto_ccm_init_crypt(req, authtag);
4a49b499
JL
372 if (err)
373 return err;
374
81c4c35e
HX
375 scatterwalk_map_and_copy(authtag, sg_next(pctx->src), cryptlen,
376 authsize, 0);
4a49b499
JL
377
378 dst = pctx->src;
81c4c35e 379 if (req->src != req->dst)
4a49b499 380 dst = pctx->dst;
4a49b499 381
464b93a3
HX
382 skcipher_request_set_tfm(skreq, ctx->ctr);
383 skcipher_request_set_callback(skreq, pctx->flags,
384 crypto_ccm_decrypt_done, req);
385 skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
386 err = crypto_skcipher_decrypt(skreq);
4a49b499
JL
387 if (err)
388 return err;
389
81c4c35e 390 err = crypto_ccm_auth(req, sg_next(dst), cryptlen);
4a49b499
JL
391 if (err)
392 return err;
393
394 /* verify */
6bf37e5a 395 if (crypto_memneq(authtag, odata, authsize))
4a49b499
JL
396 return -EBADMSG;
397
398 return err;
399}
400
81c4c35e 401static int crypto_ccm_init_tfm(struct crypto_aead *tfm)
4a49b499 402{
81c4c35e
HX
403 struct aead_instance *inst = aead_alg_instance(tfm);
404 struct ccm_instance_ctx *ictx = aead_instance_ctx(inst);
405 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
f15f05b0 406 struct crypto_ahash *mac;
464b93a3 407 struct crypto_skcipher *ctr;
4a49b499
JL
408 unsigned long align;
409 int err;
410
f15f05b0
AB
411 mac = crypto_spawn_ahash(&ictx->mac);
412 if (IS_ERR(mac))
413 return PTR_ERR(mac);
4a49b499 414
60425a8b 415 ctr = crypto_spawn_skcipher(&ictx->ctr);
4a49b499
JL
416 err = PTR_ERR(ctr);
417 if (IS_ERR(ctr))
f15f05b0 418 goto err_free_mac;
4a49b499 419
f15f05b0 420 ctx->mac = mac;
4a49b499
JL
421 ctx->ctr = ctr;
422
81c4c35e 423 align = crypto_aead_alignmask(tfm);
4a49b499 424 align &= ~(crypto_tfm_ctx_alignment() - 1);
81c4c35e
HX
425 crypto_aead_set_reqsize(
426 tfm,
2c221ad3 427 align + sizeof(struct crypto_ccm_req_priv_ctx) +
464b93a3 428 crypto_skcipher_reqsize(ctr));
4a49b499
JL
429
430 return 0;
431
f15f05b0
AB
432err_free_mac:
433 crypto_free_ahash(mac);
4a49b499
JL
434 return err;
435}
436
81c4c35e 437static void crypto_ccm_exit_tfm(struct crypto_aead *tfm)
4a49b499 438{
81c4c35e 439 struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
4a49b499 440
f15f05b0 441 crypto_free_ahash(ctx->mac);
464b93a3 442 crypto_free_skcipher(ctx->ctr);
4a49b499
JL
443}
444
81c4c35e
HX
445static void crypto_ccm_free(struct aead_instance *inst)
446{
447 struct ccm_instance_ctx *ctx = aead_instance_ctx(inst);
448
f15f05b0 449 crypto_drop_ahash(&ctx->mac);
81c4c35e
HX
450 crypto_drop_skcipher(&ctx->ctr);
451 kfree(inst);
452}
453
454static int crypto_ccm_create_common(struct crypto_template *tmpl,
455 struct rtattr **tb,
456 const char *full_name,
457 const char *ctr_name,
f15f05b0 458 const char *mac_name)
4a49b499
JL
459{
460 struct crypto_attr_type *algt;
81c4c35e 461 struct aead_instance *inst;
464b93a3 462 struct skcipher_alg *ctr;
f15f05b0
AB
463 struct crypto_alg *mac_alg;
464 struct hash_alg_common *mac;
4a49b499
JL
465 struct ccm_instance_ctx *ictx;
466 int err;
467
468 algt = crypto_get_attr_type(tb);
4a49b499 469 if (IS_ERR(algt))
81c4c35e 470 return PTR_ERR(algt);
4a49b499 471
5e4b8c1f 472 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
81c4c35e 473 return -EINVAL;
4a49b499 474
f15f05b0
AB
475 mac_alg = crypto_find_alg(mac_name, &crypto_ahash_type,
476 CRYPTO_ALG_TYPE_HASH,
477 CRYPTO_ALG_TYPE_AHASH_MASK |
478 CRYPTO_ALG_ASYNC);
479 if (IS_ERR(mac_alg))
480 return PTR_ERR(mac_alg);
4a49b499 481
f15f05b0 482 mac = __crypto_hash_alg_common(mac_alg);
4a49b499 483 err = -EINVAL;
f15f05b0
AB
484 if (mac->digestsize != 16)
485 goto out_put_mac;
4a49b499
JL
486
487 inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
488 err = -ENOMEM;
489 if (!inst)
f15f05b0 490 goto out_put_mac;
4a49b499 491
81c4c35e 492 ictx = aead_instance_ctx(inst);
f15f05b0
AB
493 err = crypto_init_ahash_spawn(&ictx->mac, mac,
494 aead_crypto_instance(inst));
4a49b499
JL
495 if (err)
496 goto err_free_inst;
497
81c4c35e 498 crypto_set_skcipher_spawn(&ictx->ctr, aead_crypto_instance(inst));
a35528ec
EB
499 err = crypto_grab_skcipher(&ictx->ctr, ctr_name, 0,
500 crypto_requires_sync(algt->type,
501 algt->mask));
4a49b499 502 if (err)
f15f05b0 503 goto err_drop_mac;
4a49b499 504
464b93a3 505 ctr = crypto_spawn_skcipher_alg(&ictx->ctr);
4a49b499
JL
506
507 /* Not a stream cipher? */
508 err = -EINVAL;
464b93a3 509 if (ctr->base.cra_blocksize != 1)
4a49b499
JL
510 goto err_drop_ctr;
511
512 /* We want the real thing! */
464b93a3 513 if (crypto_skcipher_alg_ivsize(ctr) != 16)
4a49b499
JL
514 goto err_drop_ctr;
515
516 err = -ENAMETOOLONG;
81c4c35e 517 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
464b93a3 518 "ccm_base(%s,%s)", ctr->base.cra_driver_name,
f15f05b0 519 mac->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
4a49b499
JL
520 goto err_drop_ctr;
521
81c4c35e
HX
522 memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
523
464b93a3 524 inst->alg.base.cra_flags = ctr->base.cra_flags & CRYPTO_ALG_ASYNC;
f15f05b0 525 inst->alg.base.cra_priority = (mac->base.cra_priority +
464b93a3 526 ctr->base.cra_priority) / 2;
81c4c35e 527 inst->alg.base.cra_blocksize = 1;
f15f05b0 528 inst->alg.base.cra_alignmask = mac->base.cra_alignmask |
464b93a3 529 ctr->base.cra_alignmask |
81c4c35e
HX
530 (__alignof__(u32) - 1);
531 inst->alg.ivsize = 16;
464b93a3 532 inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
81c4c35e
HX
533 inst->alg.maxauthsize = 16;
534 inst->alg.base.cra_ctxsize = sizeof(struct crypto_ccm_ctx);
535 inst->alg.init = crypto_ccm_init_tfm;
536 inst->alg.exit = crypto_ccm_exit_tfm;
537 inst->alg.setkey = crypto_ccm_setkey;
538 inst->alg.setauthsize = crypto_ccm_setauthsize;
539 inst->alg.encrypt = crypto_ccm_encrypt;
540 inst->alg.decrypt = crypto_ccm_decrypt;
541
542 inst->free = crypto_ccm_free;
543
544 err = aead_register_instance(tmpl, inst);
545 if (err)
546 goto err_drop_ctr;
4a49b499 547
f15f05b0
AB
548out_put_mac:
549 crypto_mod_put(mac_alg);
81c4c35e 550 return err;
4a49b499
JL
551
552err_drop_ctr:
553 crypto_drop_skcipher(&ictx->ctr);
f15f05b0
AB
554err_drop_mac:
555 crypto_drop_ahash(&ictx->mac);
4a49b499
JL
556err_free_inst:
557 kfree(inst);
f15f05b0 558 goto out_put_mac;
4a49b499
JL
559}
560
81c4c35e 561static int crypto_ccm_create(struct crypto_template *tmpl, struct rtattr **tb)
4a49b499 562{
4a49b499
JL
563 const char *cipher_name;
564 char ctr_name[CRYPTO_MAX_ALG_NAME];
f15f05b0 565 char mac_name[CRYPTO_MAX_ALG_NAME];
4a49b499
JL
566 char full_name[CRYPTO_MAX_ALG_NAME];
567
568 cipher_name = crypto_attr_alg_name(tb[1]);
4a49b499 569 if (IS_ERR(cipher_name))
81c4c35e 570 return PTR_ERR(cipher_name);
4a49b499
JL
571
572 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
573 cipher_name) >= CRYPTO_MAX_ALG_NAME)
81c4c35e 574 return -ENAMETOOLONG;
4a49b499 575
f15f05b0
AB
576 if (snprintf(mac_name, CRYPTO_MAX_ALG_NAME, "cbcmac(%s)",
577 cipher_name) >= CRYPTO_MAX_ALG_NAME)
578 return -ENAMETOOLONG;
579
4a49b499
JL
580 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "ccm(%s)", cipher_name) >=
581 CRYPTO_MAX_ALG_NAME)
81c4c35e 582 return -ENAMETOOLONG;
4a49b499 583
81c4c35e 584 return crypto_ccm_create_common(tmpl, tb, full_name, ctr_name,
f15f05b0 585 mac_name);
4a49b499
JL
586}
587
588static struct crypto_template crypto_ccm_tmpl = {
589 .name = "ccm",
81c4c35e 590 .create = crypto_ccm_create,
4a49b499
JL
591 .module = THIS_MODULE,
592};
593
81c4c35e
HX
594static int crypto_ccm_base_create(struct crypto_template *tmpl,
595 struct rtattr **tb)
4a49b499 596{
4a49b499
JL
597 const char *ctr_name;
598 const char *cipher_name;
599 char full_name[CRYPTO_MAX_ALG_NAME];
600
601 ctr_name = crypto_attr_alg_name(tb[1]);
4a49b499 602 if (IS_ERR(ctr_name))
81c4c35e 603 return PTR_ERR(ctr_name);
4a49b499
JL
604
605 cipher_name = crypto_attr_alg_name(tb[2]);
4a49b499 606 if (IS_ERR(cipher_name))
81c4c35e 607 return PTR_ERR(cipher_name);
4a49b499
JL
608
609 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "ccm_base(%s,%s)",
610 ctr_name, cipher_name) >= CRYPTO_MAX_ALG_NAME)
81c4c35e 611 return -ENAMETOOLONG;
4a49b499 612
81c4c35e
HX
613 return crypto_ccm_create_common(tmpl, tb, full_name, ctr_name,
614 cipher_name);
4a49b499
JL
615}
616
617static struct crypto_template crypto_ccm_base_tmpl = {
618 .name = "ccm_base",
81c4c35e 619 .create = crypto_ccm_base_create,
4a49b499
JL
620 .module = THIS_MODULE,
621};
622
623static int crypto_rfc4309_setkey(struct crypto_aead *parent, const u8 *key,
624 unsigned int keylen)
625{
626 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
627 struct crypto_aead *child = ctx->child;
628 int err;
629
630 if (keylen < 3)
631 return -EINVAL;
632
633 keylen -= 3;
634 memcpy(ctx->nonce, key + keylen, 3);
635
636 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
637 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
638 CRYPTO_TFM_REQ_MASK);
639 err = crypto_aead_setkey(child, key, keylen);
640 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
641 CRYPTO_TFM_RES_MASK);
642
643 return err;
644}
645
646static int crypto_rfc4309_setauthsize(struct crypto_aead *parent,
647 unsigned int authsize)
648{
649 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(parent);
650
651 switch (authsize) {
652 case 8:
653 case 12:
654 case 16:
655 break;
656 default:
657 return -EINVAL;
658 }
659
660 return crypto_aead_setauthsize(ctx->child, authsize);
661}
662
663static struct aead_request *crypto_rfc4309_crypt(struct aead_request *req)
664{
81c4c35e
HX
665 struct crypto_rfc4309_req_ctx *rctx = aead_request_ctx(req);
666 struct aead_request *subreq = &rctx->subreq;
4a49b499
JL
667 struct crypto_aead *aead = crypto_aead_reqtfm(req);
668 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(aead);
669 struct crypto_aead *child = ctx->child;
81c4c35e 670 struct scatterlist *sg;
4a49b499
JL
671 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
672 crypto_aead_alignmask(child) + 1);
673
674 /* L' */
675 iv[0] = 3;
676
677 memcpy(iv + 1, ctx->nonce, 3);
678 memcpy(iv + 4, req->iv, 8);
679
81c4c35e
HX
680 scatterwalk_map_and_copy(iv + 16, req->src, 0, req->assoclen - 8, 0);
681
682 sg_init_table(rctx->src, 3);
683 sg_set_buf(rctx->src, iv + 16, req->assoclen - 8);
684 sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
685 if (sg != rctx->src + 1)
686 sg_chain(rctx->src, 2, sg);
687
688 if (req->src != req->dst) {
689 sg_init_table(rctx->dst, 3);
690 sg_set_buf(rctx->dst, iv + 16, req->assoclen - 8);
691 sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
692 if (sg != rctx->dst + 1)
693 sg_chain(rctx->dst, 2, sg);
694 }
695
4a49b499
JL
696 aead_request_set_tfm(subreq, child);
697 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
698 req->base.data);
81c4c35e
HX
699 aead_request_set_crypt(subreq, rctx->src,
700 req->src == req->dst ? rctx->src : rctx->dst,
701 req->cryptlen, iv);
702 aead_request_set_ad(subreq, req->assoclen - 8);
4a49b499
JL
703
704 return subreq;
705}
706
707static int crypto_rfc4309_encrypt(struct aead_request *req)
708{
81c4c35e
HX
709 if (req->assoclen != 16 && req->assoclen != 20)
710 return -EINVAL;
711
4a49b499
JL
712 req = crypto_rfc4309_crypt(req);
713
714 return crypto_aead_encrypt(req);
715}
716
717static int crypto_rfc4309_decrypt(struct aead_request *req)
718{
81c4c35e
HX
719 if (req->assoclen != 16 && req->assoclen != 20)
720 return -EINVAL;
721
4a49b499
JL
722 req = crypto_rfc4309_crypt(req);
723
724 return crypto_aead_decrypt(req);
725}
726
81c4c35e 727static int crypto_rfc4309_init_tfm(struct crypto_aead *tfm)
4a49b499 728{
81c4c35e
HX
729 struct aead_instance *inst = aead_alg_instance(tfm);
730 struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
731 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
4a49b499
JL
732 struct crypto_aead *aead;
733 unsigned long align;
734
735 aead = crypto_spawn_aead(spawn);
736 if (IS_ERR(aead))
737 return PTR_ERR(aead);
738
739 ctx->child = aead;
740
741 align = crypto_aead_alignmask(aead);
742 align &= ~(crypto_tfm_ctx_alignment() - 1);
81c4c35e
HX
743 crypto_aead_set_reqsize(
744 tfm,
745 sizeof(struct crypto_rfc4309_req_ctx) +
2c221ad3 746 ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
81c4c35e 747 align + 32);
4a49b499
JL
748
749 return 0;
750}
751
81c4c35e 752static void crypto_rfc4309_exit_tfm(struct crypto_aead *tfm)
4a49b499 753{
81c4c35e 754 struct crypto_rfc4309_ctx *ctx = crypto_aead_ctx(tfm);
4a49b499
JL
755
756 crypto_free_aead(ctx->child);
757}
758
81c4c35e
HX
759static void crypto_rfc4309_free(struct aead_instance *inst)
760{
761 crypto_drop_aead(aead_instance_ctx(inst));
762 kfree(inst);
763}
764
765static int crypto_rfc4309_create(struct crypto_template *tmpl,
766 struct rtattr **tb)
4a49b499
JL
767{
768 struct crypto_attr_type *algt;
81c4c35e 769 struct aead_instance *inst;
4a49b499 770 struct crypto_aead_spawn *spawn;
81c4c35e 771 struct aead_alg *alg;
4a49b499
JL
772 const char *ccm_name;
773 int err;
774
775 algt = crypto_get_attr_type(tb);
4a49b499 776 if (IS_ERR(algt))
81c4c35e 777 return PTR_ERR(algt);
4a49b499 778
5e4b8c1f 779 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
81c4c35e 780 return -EINVAL;
4a49b499
JL
781
782 ccm_name = crypto_attr_alg_name(tb[1]);
4a49b499 783 if (IS_ERR(ccm_name))
81c4c35e 784 return PTR_ERR(ccm_name);
4a49b499
JL
785
786 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
787 if (!inst)
81c4c35e 788 return -ENOMEM;
4a49b499 789
81c4c35e
HX
790 spawn = aead_instance_ctx(inst);
791 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
4a49b499
JL
792 err = crypto_grab_aead(spawn, ccm_name, 0,
793 crypto_requires_sync(algt->type, algt->mask));
794 if (err)
795 goto out_free_inst;
796
81c4c35e 797 alg = crypto_spawn_aead_alg(spawn);
4a49b499
JL
798
799 err = -EINVAL;
800
801 /* We only support 16-byte blocks. */
81c4c35e 802 if (crypto_aead_alg_ivsize(alg) != 16)
4a49b499
JL
803 goto out_drop_alg;
804
805 /* Not a stream cipher? */
81c4c35e 806 if (alg->base.cra_blocksize != 1)
4a49b499
JL
807 goto out_drop_alg;
808
809 err = -ENAMETOOLONG;
81c4c35e
HX
810 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
811 "rfc4309(%s)", alg->base.cra_name) >=
812 CRYPTO_MAX_ALG_NAME ||
813 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
814 "rfc4309(%s)", alg->base.cra_driver_name) >=
4a49b499
JL
815 CRYPTO_MAX_ALG_NAME)
816 goto out_drop_alg;
817
81c4c35e 818 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
81c4c35e
HX
819 inst->alg.base.cra_priority = alg->base.cra_priority;
820 inst->alg.base.cra_blocksize = 1;
821 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
4a49b499 822
81c4c35e 823 inst->alg.ivsize = 8;
464b93a3 824 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
81c4c35e 825 inst->alg.maxauthsize = 16;
4a49b499 826
81c4c35e 827 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx);
4a49b499 828
81c4c35e
HX
829 inst->alg.init = crypto_rfc4309_init_tfm;
830 inst->alg.exit = crypto_rfc4309_exit_tfm;
4a49b499 831
81c4c35e
HX
832 inst->alg.setkey = crypto_rfc4309_setkey;
833 inst->alg.setauthsize = crypto_rfc4309_setauthsize;
834 inst->alg.encrypt = crypto_rfc4309_encrypt;
835 inst->alg.decrypt = crypto_rfc4309_decrypt;
4a49b499 836
81c4c35e
HX
837 inst->free = crypto_rfc4309_free;
838
839 err = aead_register_instance(tmpl, inst);
840 if (err)
841 goto out_drop_alg;
4a49b499
JL
842
843out:
81c4c35e 844 return err;
4a49b499
JL
845
846out_drop_alg:
847 crypto_drop_aead(spawn);
848out_free_inst:
849 kfree(inst);
4a49b499
JL
850 goto out;
851}
852
4a49b499
JL
853static struct crypto_template crypto_rfc4309_tmpl = {
854 .name = "rfc4309",
81c4c35e 855 .create = crypto_rfc4309_create,
4a49b499
JL
856 .module = THIS_MODULE,
857};
858
f15f05b0
AB
859static int crypto_cbcmac_digest_setkey(struct crypto_shash *parent,
860 const u8 *inkey, unsigned int keylen)
861{
862 struct cbcmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
863
864 return crypto_cipher_setkey(ctx->child, inkey, keylen);
865}
866
867static int crypto_cbcmac_digest_init(struct shash_desc *pdesc)
868{
869 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
870 int bs = crypto_shash_digestsize(pdesc->tfm);
871
872 ctx->len = 0;
873 memset(ctx->dg, 0, bs);
874
875 return 0;
876}
877
878static int crypto_cbcmac_digest_update(struct shash_desc *pdesc, const u8 *p,
879 unsigned int len)
880{
881 struct crypto_shash *parent = pdesc->tfm;
882 struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
883 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
884 struct crypto_cipher *tfm = tctx->child;
885 int bs = crypto_shash_digestsize(parent);
886
887 while (len > 0) {
888 unsigned int l = min(len, bs - ctx->len);
889
890 crypto_xor(ctx->dg + ctx->len, p, l);
891 ctx->len +=l;
892 len -= l;
893 p += l;
894
895 if (ctx->len == bs) {
896 crypto_cipher_encrypt_one(tfm, ctx->dg, ctx->dg);
897 ctx->len = 0;
898 }
899 }
900
901 return 0;
902}
903
904static int crypto_cbcmac_digest_final(struct shash_desc *pdesc, u8 *out)
905{
906 struct crypto_shash *parent = pdesc->tfm;
907 struct cbcmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
908 struct cbcmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
909 struct crypto_cipher *tfm = tctx->child;
910 int bs = crypto_shash_digestsize(parent);
911
912 if (ctx->len)
913 crypto_cipher_encrypt_one(tfm, out, ctx->dg);
914 else
915 memcpy(out, ctx->dg, bs);
916
917 return 0;
918}
919
920static int cbcmac_init_tfm(struct crypto_tfm *tfm)
921{
922 struct crypto_cipher *cipher;
923 struct crypto_instance *inst = (void *)tfm->__crt_alg;
924 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
925 struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
926
927 cipher = crypto_spawn_cipher(spawn);
928 if (IS_ERR(cipher))
929 return PTR_ERR(cipher);
930
931 ctx->child = cipher;
932
933 return 0;
934};
935
936static void cbcmac_exit_tfm(struct crypto_tfm *tfm)
937{
938 struct cbcmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
939 crypto_free_cipher(ctx->child);
940}
941
942static int cbcmac_create(struct crypto_template *tmpl, struct rtattr **tb)
943{
944 struct shash_instance *inst;
945 struct crypto_alg *alg;
946 int err;
947
948 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
949 if (err)
950 return err;
951
952 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
953 CRYPTO_ALG_TYPE_MASK);
954 if (IS_ERR(alg))
955 return PTR_ERR(alg);
956
957 inst = shash_alloc_instance("cbcmac", alg);
958 err = PTR_ERR(inst);
959 if (IS_ERR(inst))
960 goto out_put_alg;
961
962 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
963 shash_crypto_instance(inst),
964 CRYPTO_ALG_TYPE_MASK);
965 if (err)
966 goto out_free_inst;
967
968 inst->alg.base.cra_priority = alg->cra_priority;
969 inst->alg.base.cra_blocksize = 1;
970
971 inst->alg.digestsize = alg->cra_blocksize;
972 inst->alg.descsize = sizeof(struct cbcmac_desc_ctx) +
973 alg->cra_blocksize;
974
975 inst->alg.base.cra_ctxsize = sizeof(struct cbcmac_tfm_ctx);
976 inst->alg.base.cra_init = cbcmac_init_tfm;
977 inst->alg.base.cra_exit = cbcmac_exit_tfm;
978
979 inst->alg.init = crypto_cbcmac_digest_init;
980 inst->alg.update = crypto_cbcmac_digest_update;
981 inst->alg.final = crypto_cbcmac_digest_final;
982 inst->alg.setkey = crypto_cbcmac_digest_setkey;
983
984 err = shash_register_instance(tmpl, inst);
985
986out_free_inst:
987 if (err)
988 shash_free_instance(shash_crypto_instance(inst));
989
990out_put_alg:
991 crypto_mod_put(alg);
992 return err;
993}
994
995static struct crypto_template crypto_cbcmac_tmpl = {
996 .name = "cbcmac",
997 .create = cbcmac_create,
998 .free = shash_free_instance,
999 .module = THIS_MODULE,
1000};
1001
4a49b499
JL
1002static int __init crypto_ccm_module_init(void)
1003{
1004 int err;
1005
f15f05b0 1006 err = crypto_register_template(&crypto_cbcmac_tmpl);
4a49b499
JL
1007 if (err)
1008 goto out;
1009
f15f05b0
AB
1010 err = crypto_register_template(&crypto_ccm_base_tmpl);
1011 if (err)
1012 goto out_undo_cbcmac;
1013
4a49b499
JL
1014 err = crypto_register_template(&crypto_ccm_tmpl);
1015 if (err)
1016 goto out_undo_base;
1017
1018 err = crypto_register_template(&crypto_rfc4309_tmpl);
1019 if (err)
1020 goto out_undo_ccm;
1021
1022out:
1023 return err;
1024
1025out_undo_ccm:
1026 crypto_unregister_template(&crypto_ccm_tmpl);
1027out_undo_base:
1028 crypto_unregister_template(&crypto_ccm_base_tmpl);
f15f05b0
AB
1029out_undo_cbcmac:
1030 crypto_register_template(&crypto_cbcmac_tmpl);
4a49b499
JL
1031 goto out;
1032}
1033
1034static void __exit crypto_ccm_module_exit(void)
1035{
1036 crypto_unregister_template(&crypto_rfc4309_tmpl);
1037 crypto_unregister_template(&crypto_ccm_tmpl);
1038 crypto_unregister_template(&crypto_ccm_base_tmpl);
f15f05b0 1039 crypto_unregister_template(&crypto_cbcmac_tmpl);
4a49b499
JL
1040}
1041
1042module_init(crypto_ccm_module_init);
1043module_exit(crypto_ccm_module_exit);
1044
1045MODULE_LICENSE("GPL");
1046MODULE_DESCRIPTION("Counter with CBC MAC");
5d26a105
KC
1047MODULE_ALIAS_CRYPTO("ccm_base");
1048MODULE_ALIAS_CRYPTO("rfc4309");
4943ba16 1049MODULE_ALIAS_CRYPTO("ccm");