1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * CCM: Counter with CBC-MAC
5 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
8 #include <crypto/internal/aead.h>
9 #include <crypto/internal/hash.h>
10 #include <crypto/internal/skcipher.h>
11 #include <crypto/scatterwalk.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
18 struct ccm_instance_ctx
{
19 struct crypto_skcipher_spawn ctr
;
20 struct crypto_ahash_spawn mac
;
23 struct crypto_ccm_ctx
{
24 struct crypto_ahash
*mac
;
25 struct crypto_skcipher
*ctr
;
28 struct crypto_rfc4309_ctx
{
29 struct crypto_aead
*child
;
33 struct crypto_rfc4309_req_ctx
{
34 struct scatterlist src
[3];
35 struct scatterlist dst
[3];
36 struct aead_request subreq
;
39 struct crypto_ccm_req_priv_ctx
{
44 struct scatterlist src
[3];
45 struct scatterlist dst
[3];
47 struct ahash_request ahreq
;
48 struct skcipher_request skreq
;
52 struct cbcmac_tfm_ctx
{
53 struct crypto_cipher
*child
;
56 struct cbcmac_desc_ctx
{
60 static inline struct crypto_ccm_req_priv_ctx
*crypto_ccm_reqctx(
61 struct aead_request
*req
)
63 unsigned long align
= crypto_aead_alignmask(crypto_aead_reqtfm(req
));
65 return (void *)PTR_ALIGN((u8
*)aead_request_ctx(req
), align
+ 1);
68 static int set_msg_len(u8
*block
, unsigned int msglen
, int csize
)
72 memset(block
, 0, csize
);
77 else if (msglen
> (1 << (8 * csize
)))
80 data
= cpu_to_be32(msglen
);
81 memcpy(block
- csize
, (u8
*)&data
+ 4 - csize
, csize
);
86 static int crypto_ccm_setkey(struct crypto_aead
*aead
, const u8
*key
,
89 struct crypto_ccm_ctx
*ctx
= crypto_aead_ctx(aead
);
90 struct crypto_skcipher
*ctr
= ctx
->ctr
;
91 struct crypto_ahash
*mac
= ctx
->mac
;
94 crypto_skcipher_clear_flags(ctr
, CRYPTO_TFM_REQ_MASK
);
95 crypto_skcipher_set_flags(ctr
, crypto_aead_get_flags(aead
) &
97 err
= crypto_skcipher_setkey(ctr
, key
, keylen
);
101 crypto_ahash_clear_flags(mac
, CRYPTO_TFM_REQ_MASK
);
102 crypto_ahash_set_flags(mac
, crypto_aead_get_flags(aead
) &
103 CRYPTO_TFM_REQ_MASK
);
104 return crypto_ahash_setkey(mac
, key
, keylen
);
107 static int crypto_ccm_setauthsize(struct crypto_aead
*tfm
,
108 unsigned int authsize
)
126 static int format_input(u8
*info
, struct aead_request
*req
,
127 unsigned int cryptlen
)
129 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
130 unsigned int lp
= req
->iv
[0];
131 unsigned int l
= lp
+ 1;
134 m
= crypto_aead_authsize(aead
);
136 memcpy(info
, req
->iv
, 16);
138 /* format control info per RFC 3610 and
139 * NIST Special Publication 800-38C
141 *info
|= (8 * ((m
- 2) / 2));
145 return set_msg_len(info
+ 16 - l
, cryptlen
, l
);
148 static int format_adata(u8
*adata
, unsigned int a
)
152 /* add control info for associated data
153 * RFC 3610 and NIST Special Publication 800-38C
156 *(__be16
*)adata
= cpu_to_be16(a
);
159 *(__be16
*)adata
= cpu_to_be16(0xfffe);
160 *(__be32
*)&adata
[2] = cpu_to_be32(a
);
167 static int crypto_ccm_auth(struct aead_request
*req
, struct scatterlist
*plain
,
168 unsigned int cryptlen
)
170 struct crypto_ccm_req_priv_ctx
*pctx
= crypto_ccm_reqctx(req
);
171 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
172 struct crypto_ccm_ctx
*ctx
= crypto_aead_ctx(aead
);
173 struct ahash_request
*ahreq
= &pctx
->ahreq
;
174 unsigned int assoclen
= req
->assoclen
;
175 struct scatterlist sg
[3];
176 u8
*odata
= pctx
->odata
;
177 u8
*idata
= pctx
->idata
;
180 /* format control data for input */
181 err
= format_input(odata
, req
, cryptlen
);
185 sg_init_table(sg
, 3);
186 sg_set_buf(&sg
[0], odata
, 16);
188 /* format associated data and compute into mac */
190 ilen
= format_adata(idata
, assoclen
);
191 sg_set_buf(&sg
[1], idata
, ilen
);
192 sg_chain(sg
, 3, req
->src
);
195 sg_chain(sg
, 2, req
->src
);
198 ahash_request_set_tfm(ahreq
, ctx
->mac
);
199 ahash_request_set_callback(ahreq
, pctx
->flags
, NULL
, NULL
);
200 ahash_request_set_crypt(ahreq
, sg
, NULL
, assoclen
+ ilen
+ 16);
201 err
= crypto_ahash_init(ahreq
);
204 err
= crypto_ahash_update(ahreq
);
208 /* we need to pad the MAC input to a round multiple of the block size */
209 ilen
= 16 - (assoclen
+ ilen
) % 16;
211 memset(idata
, 0, ilen
);
212 sg_init_table(sg
, 2);
213 sg_set_buf(&sg
[0], idata
, ilen
);
215 sg_chain(sg
, 2, plain
);
220 ahash_request_set_crypt(ahreq
, plain
, pctx
->odata
, cryptlen
);
221 err
= crypto_ahash_finup(ahreq
);
226 static void crypto_ccm_encrypt_done(struct crypto_async_request
*areq
, int err
)
228 struct aead_request
*req
= areq
->data
;
229 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
230 struct crypto_ccm_req_priv_ctx
*pctx
= crypto_ccm_reqctx(req
);
231 u8
*odata
= pctx
->odata
;
234 scatterwalk_map_and_copy(odata
, req
->dst
,
235 req
->assoclen
+ req
->cryptlen
,
236 crypto_aead_authsize(aead
), 1);
237 aead_request_complete(req
, err
);
240 static inline int crypto_ccm_check_iv(const u8
*iv
)
242 /* 2 <= L <= 8, so 1 <= L' <= 7. */
243 if (1 > iv
[0] || iv
[0] > 7)
249 static int crypto_ccm_init_crypt(struct aead_request
*req
, u8
*tag
)
251 struct crypto_ccm_req_priv_ctx
*pctx
= crypto_ccm_reqctx(req
);
252 struct scatterlist
*sg
;
256 err
= crypto_ccm_check_iv(iv
);
260 pctx
->flags
= aead_request_flags(req
);
262 /* Note: rfc 3610 and NIST 800-38C require counter of
263 * zero to encrypt auth tag.
265 memset(iv
+ 15 - iv
[0], 0, iv
[0] + 1);
267 sg_init_table(pctx
->src
, 3);
268 sg_set_buf(pctx
->src
, tag
, 16);
269 sg
= scatterwalk_ffwd(pctx
->src
+ 1, req
->src
, req
->assoclen
);
270 if (sg
!= pctx
->src
+ 1)
271 sg_chain(pctx
->src
, 2, sg
);
273 if (req
->src
!= req
->dst
) {
274 sg_init_table(pctx
->dst
, 3);
275 sg_set_buf(pctx
->dst
, tag
, 16);
276 sg
= scatterwalk_ffwd(pctx
->dst
+ 1, req
->dst
, req
->assoclen
);
277 if (sg
!= pctx
->dst
+ 1)
278 sg_chain(pctx
->dst
, 2, sg
);
284 static int crypto_ccm_encrypt(struct aead_request
*req
)
286 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
287 struct crypto_ccm_ctx
*ctx
= crypto_aead_ctx(aead
);
288 struct crypto_ccm_req_priv_ctx
*pctx
= crypto_ccm_reqctx(req
);
289 struct skcipher_request
*skreq
= &pctx
->skreq
;
290 struct scatterlist
*dst
;
291 unsigned int cryptlen
= req
->cryptlen
;
292 u8
*odata
= pctx
->odata
;
296 err
= crypto_ccm_init_crypt(req
, odata
);
300 err
= crypto_ccm_auth(req
, sg_next(pctx
->src
), cryptlen
);
305 if (req
->src
!= req
->dst
)
308 skcipher_request_set_tfm(skreq
, ctx
->ctr
);
309 skcipher_request_set_callback(skreq
, pctx
->flags
,
310 crypto_ccm_encrypt_done
, req
);
311 skcipher_request_set_crypt(skreq
, pctx
->src
, dst
, cryptlen
+ 16, iv
);
312 err
= crypto_skcipher_encrypt(skreq
);
316 /* copy authtag to end of dst */
317 scatterwalk_map_and_copy(odata
, sg_next(dst
), cryptlen
,
318 crypto_aead_authsize(aead
), 1);
322 static void crypto_ccm_decrypt_done(struct crypto_async_request
*areq
,
325 struct aead_request
*req
= areq
->data
;
326 struct crypto_ccm_req_priv_ctx
*pctx
= crypto_ccm_reqctx(req
);
327 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
328 unsigned int authsize
= crypto_aead_authsize(aead
);
329 unsigned int cryptlen
= req
->cryptlen
- authsize
;
330 struct scatterlist
*dst
;
334 dst
= sg_next(req
->src
== req
->dst
? pctx
->src
: pctx
->dst
);
337 err
= crypto_ccm_auth(req
, dst
, cryptlen
);
338 if (!err
&& crypto_memneq(pctx
->auth_tag
, pctx
->odata
, authsize
))
341 aead_request_complete(req
, err
);
344 static int crypto_ccm_decrypt(struct aead_request
*req
)
346 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
347 struct crypto_ccm_ctx
*ctx
= crypto_aead_ctx(aead
);
348 struct crypto_ccm_req_priv_ctx
*pctx
= crypto_ccm_reqctx(req
);
349 struct skcipher_request
*skreq
= &pctx
->skreq
;
350 struct scatterlist
*dst
;
351 unsigned int authsize
= crypto_aead_authsize(aead
);
352 unsigned int cryptlen
= req
->cryptlen
;
353 u8
*authtag
= pctx
->auth_tag
;
354 u8
*odata
= pctx
->odata
;
355 u8
*iv
= pctx
->idata
;
358 cryptlen
-= authsize
;
360 err
= crypto_ccm_init_crypt(req
, authtag
);
364 scatterwalk_map_and_copy(authtag
, sg_next(pctx
->src
), cryptlen
,
368 if (req
->src
!= req
->dst
)
371 memcpy(iv
, req
->iv
, 16);
373 skcipher_request_set_tfm(skreq
, ctx
->ctr
);
374 skcipher_request_set_callback(skreq
, pctx
->flags
,
375 crypto_ccm_decrypt_done
, req
);
376 skcipher_request_set_crypt(skreq
, pctx
->src
, dst
, cryptlen
+ 16, iv
);
377 err
= crypto_skcipher_decrypt(skreq
);
381 err
= crypto_ccm_auth(req
, sg_next(dst
), cryptlen
);
386 if (crypto_memneq(authtag
, odata
, authsize
))
392 static int crypto_ccm_init_tfm(struct crypto_aead
*tfm
)
394 struct aead_instance
*inst
= aead_alg_instance(tfm
);
395 struct ccm_instance_ctx
*ictx
= aead_instance_ctx(inst
);
396 struct crypto_ccm_ctx
*ctx
= crypto_aead_ctx(tfm
);
397 struct crypto_ahash
*mac
;
398 struct crypto_skcipher
*ctr
;
402 mac
= crypto_spawn_ahash(&ictx
->mac
);
406 ctr
= crypto_spawn_skcipher(&ictx
->ctr
);
414 align
= crypto_aead_alignmask(tfm
);
415 align
&= ~(crypto_tfm_ctx_alignment() - 1);
416 crypto_aead_set_reqsize(
418 align
+ sizeof(struct crypto_ccm_req_priv_ctx
) +
419 max(crypto_ahash_reqsize(mac
), crypto_skcipher_reqsize(ctr
)));
424 crypto_free_ahash(mac
);
428 static void crypto_ccm_exit_tfm(struct crypto_aead
*tfm
)
430 struct crypto_ccm_ctx
*ctx
= crypto_aead_ctx(tfm
);
432 crypto_free_ahash(ctx
->mac
);
433 crypto_free_skcipher(ctx
->ctr
);
436 static void crypto_ccm_free(struct aead_instance
*inst
)
438 struct ccm_instance_ctx
*ctx
= aead_instance_ctx(inst
);
440 crypto_drop_ahash(&ctx
->mac
);
441 crypto_drop_skcipher(&ctx
->ctr
);
445 static int crypto_ccm_create_common(struct crypto_template
*tmpl
,
447 const char *ctr_name
,
448 const char *mac_name
)
450 struct crypto_attr_type
*algt
;
452 struct aead_instance
*inst
;
453 struct ccm_instance_ctx
*ictx
;
454 struct skcipher_alg
*ctr
;
455 struct hash_alg_common
*mac
;
458 algt
= crypto_get_attr_type(tb
);
460 return PTR_ERR(algt
);
462 if ((algt
->type
^ CRYPTO_ALG_TYPE_AEAD
) & algt
->mask
)
465 mask
= crypto_requires_sync(algt
->type
, algt
->mask
);
467 inst
= kzalloc(sizeof(*inst
) + sizeof(*ictx
), GFP_KERNEL
);
470 ictx
= aead_instance_ctx(inst
);
472 err
= crypto_grab_ahash(&ictx
->mac
, aead_crypto_instance(inst
),
473 mac_name
, 0, CRYPTO_ALG_ASYNC
);
476 mac
= crypto_spawn_ahash_alg(&ictx
->mac
);
479 if (strncmp(mac
->base
.cra_name
, "cbcmac(", 7) != 0 ||
480 mac
->digestsize
!= 16)
483 err
= crypto_grab_skcipher(&ictx
->ctr
, aead_crypto_instance(inst
),
487 ctr
= crypto_spawn_skcipher_alg(&ictx
->ctr
);
489 /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
491 if (strncmp(ctr
->base
.cra_name
, "ctr(", 4) != 0 ||
492 crypto_skcipher_alg_ivsize(ctr
) != 16 ||
493 ctr
->base
.cra_blocksize
!= 1)
496 /* ctr and cbcmac must use the same underlying block cipher. */
497 if (strcmp(ctr
->base
.cra_name
+ 4, mac
->base
.cra_name
+ 7) != 0)
501 if (snprintf(inst
->alg
.base
.cra_name
, CRYPTO_MAX_ALG_NAME
,
502 "ccm(%s", ctr
->base
.cra_name
+ 4) >= CRYPTO_MAX_ALG_NAME
)
505 if (snprintf(inst
->alg
.base
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
,
506 "ccm_base(%s,%s)", ctr
->base
.cra_driver_name
,
507 mac
->base
.cra_driver_name
) >= CRYPTO_MAX_ALG_NAME
)
510 inst
->alg
.base
.cra_flags
= ctr
->base
.cra_flags
& CRYPTO_ALG_ASYNC
;
511 inst
->alg
.base
.cra_priority
= (mac
->base
.cra_priority
+
512 ctr
->base
.cra_priority
) / 2;
513 inst
->alg
.base
.cra_blocksize
= 1;
514 inst
->alg
.base
.cra_alignmask
= mac
->base
.cra_alignmask
|
515 ctr
->base
.cra_alignmask
;
516 inst
->alg
.ivsize
= 16;
517 inst
->alg
.chunksize
= crypto_skcipher_alg_chunksize(ctr
);
518 inst
->alg
.maxauthsize
= 16;
519 inst
->alg
.base
.cra_ctxsize
= sizeof(struct crypto_ccm_ctx
);
520 inst
->alg
.init
= crypto_ccm_init_tfm
;
521 inst
->alg
.exit
= crypto_ccm_exit_tfm
;
522 inst
->alg
.setkey
= crypto_ccm_setkey
;
523 inst
->alg
.setauthsize
= crypto_ccm_setauthsize
;
524 inst
->alg
.encrypt
= crypto_ccm_encrypt
;
525 inst
->alg
.decrypt
= crypto_ccm_decrypt
;
527 inst
->free
= crypto_ccm_free
;
529 err
= aead_register_instance(tmpl
, inst
);
532 crypto_ccm_free(inst
);
537 static int crypto_ccm_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
539 const char *cipher_name
;
540 char ctr_name
[CRYPTO_MAX_ALG_NAME
];
541 char mac_name
[CRYPTO_MAX_ALG_NAME
];
543 cipher_name
= crypto_attr_alg_name(tb
[1]);
544 if (IS_ERR(cipher_name
))
545 return PTR_ERR(cipher_name
);
547 if (snprintf(ctr_name
, CRYPTO_MAX_ALG_NAME
, "ctr(%s)",
548 cipher_name
) >= CRYPTO_MAX_ALG_NAME
)
549 return -ENAMETOOLONG
;
551 if (snprintf(mac_name
, CRYPTO_MAX_ALG_NAME
, "cbcmac(%s)",
552 cipher_name
) >= CRYPTO_MAX_ALG_NAME
)
553 return -ENAMETOOLONG
;
555 return crypto_ccm_create_common(tmpl
, tb
, ctr_name
, mac_name
);
558 static int crypto_ccm_base_create(struct crypto_template
*tmpl
,
561 const char *ctr_name
;
562 const char *mac_name
;
564 ctr_name
= crypto_attr_alg_name(tb
[1]);
565 if (IS_ERR(ctr_name
))
566 return PTR_ERR(ctr_name
);
568 mac_name
= crypto_attr_alg_name(tb
[2]);
569 if (IS_ERR(mac_name
))
570 return PTR_ERR(mac_name
);
572 return crypto_ccm_create_common(tmpl
, tb
, ctr_name
, mac_name
);
575 static int crypto_rfc4309_setkey(struct crypto_aead
*parent
, const u8
*key
,
578 struct crypto_rfc4309_ctx
*ctx
= crypto_aead_ctx(parent
);
579 struct crypto_aead
*child
= ctx
->child
;
585 memcpy(ctx
->nonce
, key
+ keylen
, 3);
587 crypto_aead_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
588 crypto_aead_set_flags(child
, crypto_aead_get_flags(parent
) &
589 CRYPTO_TFM_REQ_MASK
);
590 return crypto_aead_setkey(child
, key
, keylen
);
593 static int crypto_rfc4309_setauthsize(struct crypto_aead
*parent
,
594 unsigned int authsize
)
596 struct crypto_rfc4309_ctx
*ctx
= crypto_aead_ctx(parent
);
607 return crypto_aead_setauthsize(ctx
->child
, authsize
);
610 static struct aead_request
*crypto_rfc4309_crypt(struct aead_request
*req
)
612 struct crypto_rfc4309_req_ctx
*rctx
= aead_request_ctx(req
);
613 struct aead_request
*subreq
= &rctx
->subreq
;
614 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
615 struct crypto_rfc4309_ctx
*ctx
= crypto_aead_ctx(aead
);
616 struct crypto_aead
*child
= ctx
->child
;
617 struct scatterlist
*sg
;
618 u8
*iv
= PTR_ALIGN((u8
*)(subreq
+ 1) + crypto_aead_reqsize(child
),
619 crypto_aead_alignmask(child
) + 1);
624 memcpy(iv
+ 1, ctx
->nonce
, 3);
625 memcpy(iv
+ 4, req
->iv
, 8);
627 scatterwalk_map_and_copy(iv
+ 16, req
->src
, 0, req
->assoclen
- 8, 0);
629 sg_init_table(rctx
->src
, 3);
630 sg_set_buf(rctx
->src
, iv
+ 16, req
->assoclen
- 8);
631 sg
= scatterwalk_ffwd(rctx
->src
+ 1, req
->src
, req
->assoclen
);
632 if (sg
!= rctx
->src
+ 1)
633 sg_chain(rctx
->src
, 2, sg
);
635 if (req
->src
!= req
->dst
) {
636 sg_init_table(rctx
->dst
, 3);
637 sg_set_buf(rctx
->dst
, iv
+ 16, req
->assoclen
- 8);
638 sg
= scatterwalk_ffwd(rctx
->dst
+ 1, req
->dst
, req
->assoclen
);
639 if (sg
!= rctx
->dst
+ 1)
640 sg_chain(rctx
->dst
, 2, sg
);
643 aead_request_set_tfm(subreq
, child
);
644 aead_request_set_callback(subreq
, req
->base
.flags
, req
->base
.complete
,
646 aead_request_set_crypt(subreq
, rctx
->src
,
647 req
->src
== req
->dst
? rctx
->src
: rctx
->dst
,
649 aead_request_set_ad(subreq
, req
->assoclen
- 8);
654 static int crypto_rfc4309_encrypt(struct aead_request
*req
)
656 if (req
->assoclen
!= 16 && req
->assoclen
!= 20)
659 req
= crypto_rfc4309_crypt(req
);
661 return crypto_aead_encrypt(req
);
664 static int crypto_rfc4309_decrypt(struct aead_request
*req
)
666 if (req
->assoclen
!= 16 && req
->assoclen
!= 20)
669 req
= crypto_rfc4309_crypt(req
);
671 return crypto_aead_decrypt(req
);
674 static int crypto_rfc4309_init_tfm(struct crypto_aead
*tfm
)
676 struct aead_instance
*inst
= aead_alg_instance(tfm
);
677 struct crypto_aead_spawn
*spawn
= aead_instance_ctx(inst
);
678 struct crypto_rfc4309_ctx
*ctx
= crypto_aead_ctx(tfm
);
679 struct crypto_aead
*aead
;
682 aead
= crypto_spawn_aead(spawn
);
684 return PTR_ERR(aead
);
688 align
= crypto_aead_alignmask(aead
);
689 align
&= ~(crypto_tfm_ctx_alignment() - 1);
690 crypto_aead_set_reqsize(
692 sizeof(struct crypto_rfc4309_req_ctx
) +
693 ALIGN(crypto_aead_reqsize(aead
), crypto_tfm_ctx_alignment()) +
699 static void crypto_rfc4309_exit_tfm(struct crypto_aead
*tfm
)
701 struct crypto_rfc4309_ctx
*ctx
= crypto_aead_ctx(tfm
);
703 crypto_free_aead(ctx
->child
);
706 static void crypto_rfc4309_free(struct aead_instance
*inst
)
708 crypto_drop_aead(aead_instance_ctx(inst
));
712 static int crypto_rfc4309_create(struct crypto_template
*tmpl
,
715 struct crypto_attr_type
*algt
;
717 struct aead_instance
*inst
;
718 struct crypto_aead_spawn
*spawn
;
719 struct aead_alg
*alg
;
720 const char *ccm_name
;
723 algt
= crypto_get_attr_type(tb
);
725 return PTR_ERR(algt
);
727 if ((algt
->type
^ CRYPTO_ALG_TYPE_AEAD
) & algt
->mask
)
730 mask
= crypto_requires_sync(algt
->type
, algt
->mask
);
732 ccm_name
= crypto_attr_alg_name(tb
[1]);
733 if (IS_ERR(ccm_name
))
734 return PTR_ERR(ccm_name
);
736 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
740 spawn
= aead_instance_ctx(inst
);
741 err
= crypto_grab_aead(spawn
, aead_crypto_instance(inst
),
746 alg
= crypto_spawn_aead_alg(spawn
);
750 /* We only support 16-byte blocks. */
751 if (crypto_aead_alg_ivsize(alg
) != 16)
754 /* Not a stream cipher? */
755 if (alg
->base
.cra_blocksize
!= 1)
759 if (snprintf(inst
->alg
.base
.cra_name
, CRYPTO_MAX_ALG_NAME
,
760 "rfc4309(%s)", alg
->base
.cra_name
) >=
761 CRYPTO_MAX_ALG_NAME
||
762 snprintf(inst
->alg
.base
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
,
763 "rfc4309(%s)", alg
->base
.cra_driver_name
) >=
767 inst
->alg
.base
.cra_flags
= alg
->base
.cra_flags
& CRYPTO_ALG_ASYNC
;
768 inst
->alg
.base
.cra_priority
= alg
->base
.cra_priority
;
769 inst
->alg
.base
.cra_blocksize
= 1;
770 inst
->alg
.base
.cra_alignmask
= alg
->base
.cra_alignmask
;
772 inst
->alg
.ivsize
= 8;
773 inst
->alg
.chunksize
= crypto_aead_alg_chunksize(alg
);
774 inst
->alg
.maxauthsize
= 16;
776 inst
->alg
.base
.cra_ctxsize
= sizeof(struct crypto_rfc4309_ctx
);
778 inst
->alg
.init
= crypto_rfc4309_init_tfm
;
779 inst
->alg
.exit
= crypto_rfc4309_exit_tfm
;
781 inst
->alg
.setkey
= crypto_rfc4309_setkey
;
782 inst
->alg
.setauthsize
= crypto_rfc4309_setauthsize
;
783 inst
->alg
.encrypt
= crypto_rfc4309_encrypt
;
784 inst
->alg
.decrypt
= crypto_rfc4309_decrypt
;
786 inst
->free
= crypto_rfc4309_free
;
788 err
= aead_register_instance(tmpl
, inst
);
796 crypto_drop_aead(spawn
);
802 static int crypto_cbcmac_digest_setkey(struct crypto_shash
*parent
,
803 const u8
*inkey
, unsigned int keylen
)
805 struct cbcmac_tfm_ctx
*ctx
= crypto_shash_ctx(parent
);
807 return crypto_cipher_setkey(ctx
->child
, inkey
, keylen
);
810 static int crypto_cbcmac_digest_init(struct shash_desc
*pdesc
)
812 struct cbcmac_desc_ctx
*ctx
= shash_desc_ctx(pdesc
);
813 int bs
= crypto_shash_digestsize(pdesc
->tfm
);
814 u8
*dg
= (u8
*)ctx
+ crypto_shash_descsize(pdesc
->tfm
) - bs
;
822 static int crypto_cbcmac_digest_update(struct shash_desc
*pdesc
, const u8
*p
,
825 struct crypto_shash
*parent
= pdesc
->tfm
;
826 struct cbcmac_tfm_ctx
*tctx
= crypto_shash_ctx(parent
);
827 struct cbcmac_desc_ctx
*ctx
= shash_desc_ctx(pdesc
);
828 struct crypto_cipher
*tfm
= tctx
->child
;
829 int bs
= crypto_shash_digestsize(parent
);
830 u8
*dg
= (u8
*)ctx
+ crypto_shash_descsize(parent
) - bs
;
833 unsigned int l
= min(len
, bs
- ctx
->len
);
835 crypto_xor(dg
+ ctx
->len
, p
, l
);
840 if (ctx
->len
== bs
) {
841 crypto_cipher_encrypt_one(tfm
, dg
, dg
);
849 static int crypto_cbcmac_digest_final(struct shash_desc
*pdesc
, u8
*out
)
851 struct crypto_shash
*parent
= pdesc
->tfm
;
852 struct cbcmac_tfm_ctx
*tctx
= crypto_shash_ctx(parent
);
853 struct cbcmac_desc_ctx
*ctx
= shash_desc_ctx(pdesc
);
854 struct crypto_cipher
*tfm
= tctx
->child
;
855 int bs
= crypto_shash_digestsize(parent
);
856 u8
*dg
= (u8
*)ctx
+ crypto_shash_descsize(parent
) - bs
;
859 crypto_cipher_encrypt_one(tfm
, dg
, dg
);
865 static int cbcmac_init_tfm(struct crypto_tfm
*tfm
)
867 struct crypto_cipher
*cipher
;
868 struct crypto_instance
*inst
= (void *)tfm
->__crt_alg
;
869 struct crypto_cipher_spawn
*spawn
= crypto_instance_ctx(inst
);
870 struct cbcmac_tfm_ctx
*ctx
= crypto_tfm_ctx(tfm
);
872 cipher
= crypto_spawn_cipher(spawn
);
874 return PTR_ERR(cipher
);
881 static void cbcmac_exit_tfm(struct crypto_tfm
*tfm
)
883 struct cbcmac_tfm_ctx
*ctx
= crypto_tfm_ctx(tfm
);
884 crypto_free_cipher(ctx
->child
);
887 static int cbcmac_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
889 struct shash_instance
*inst
;
890 struct crypto_cipher_spawn
*spawn
;
891 struct crypto_alg
*alg
;
894 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_SHASH
);
898 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
901 spawn
= shash_instance_ctx(inst
);
903 err
= crypto_grab_cipher(spawn
, shash_crypto_instance(inst
),
904 crypto_attr_alg_name(tb
[1]), 0, 0);
907 alg
= crypto_spawn_cipher_alg(spawn
);
909 err
= crypto_inst_setname(shash_crypto_instance(inst
), tmpl
->name
, alg
);
913 inst
->alg
.base
.cra_priority
= alg
->cra_priority
;
914 inst
->alg
.base
.cra_blocksize
= 1;
916 inst
->alg
.digestsize
= alg
->cra_blocksize
;
917 inst
->alg
.descsize
= ALIGN(sizeof(struct cbcmac_desc_ctx
),
918 alg
->cra_alignmask
+ 1) +
921 inst
->alg
.base
.cra_ctxsize
= sizeof(struct cbcmac_tfm_ctx
);
922 inst
->alg
.base
.cra_init
= cbcmac_init_tfm
;
923 inst
->alg
.base
.cra_exit
= cbcmac_exit_tfm
;
925 inst
->alg
.init
= crypto_cbcmac_digest_init
;
926 inst
->alg
.update
= crypto_cbcmac_digest_update
;
927 inst
->alg
.final
= crypto_cbcmac_digest_final
;
928 inst
->alg
.setkey
= crypto_cbcmac_digest_setkey
;
930 inst
->free
= shash_free_singlespawn_instance
;
932 err
= shash_register_instance(tmpl
, inst
);
935 shash_free_singlespawn_instance(inst
);
940 static struct crypto_template crypto_ccm_tmpls
[] = {
943 .create
= cbcmac_create
,
944 .module
= THIS_MODULE
,
947 .create
= crypto_ccm_base_create
,
948 .module
= THIS_MODULE
,
951 .create
= crypto_ccm_create
,
952 .module
= THIS_MODULE
,
955 .create
= crypto_rfc4309_create
,
956 .module
= THIS_MODULE
,
960 static int __init
crypto_ccm_module_init(void)
962 return crypto_register_templates(crypto_ccm_tmpls
,
963 ARRAY_SIZE(crypto_ccm_tmpls
));
966 static void __exit
crypto_ccm_module_exit(void)
968 crypto_unregister_templates(crypto_ccm_tmpls
,
969 ARRAY_SIZE(crypto_ccm_tmpls
));
972 subsys_initcall(crypto_ccm_module_init
);
973 module_exit(crypto_ccm_module_exit
);
975 MODULE_LICENSE("GPL");
976 MODULE_DESCRIPTION("Counter with CBC MAC");
977 MODULE_ALIAS_CRYPTO("ccm_base");
978 MODULE_ALIAS_CRYPTO("rfc4309");
979 MODULE_ALIAS_CRYPTO("ccm");
980 MODULE_ALIAS_CRYPTO("cbcmac");