18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
53 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
55 return (
void *)
PTR_ALIGN((
u8 *)aead_request_ctx(req), align + 1);
58 static int set_msg_len(
u8 *
block,
unsigned int msglen,
int csize)
67 else if (msglen > (1 << (8 * csize)))
71 memcpy(block - csize, (
u8 *)&data + 4 - csize, csize);
85 crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
87 err = crypto_ablkcipher_setkey(ctr, key, keylen);
88 crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
94 crypto_cipher_set_flags(tfm, crypto_aead_get_flags(aead) &
96 err = crypto_cipher_setkey(tfm, key, keylen);
97 crypto_aead_set_flags(aead, crypto_cipher_get_flags(tfm) &
104 static int crypto_ccm_setauthsize(
struct crypto_aead *tfm,
105 unsigned int authsize)
124 unsigned int cryptlen)
126 struct crypto_aead *aead = crypto_aead_reqtfm(req);
127 unsigned int lp = req->
iv[0];
128 unsigned int l = lp + 1;
131 m = crypto_aead_authsize(aead);
138 *info |= (8 * ((m - 2) / 2));
142 return set_msg_len(info + 16 - l, cryptlen, l);
145 static int format_adata(
u8 *adata,
unsigned int a)
167 unsigned int bs = 16;
175 getlen = bs - pctx->
ilen;
176 if (datalen >= getlen) {
179 crypto_cipher_encrypt_one(tfm, odata, odata);
186 while (datalen >= bs) {
188 crypto_cipher_encrypt_one(tfm, odata, odata);
214 n = scatterwalk_clamp(&walk, len);
217 n = scatterwalk_clamp(&walk, len);
221 compute_mac(tfm, data_src, n, pctx);
224 scatterwalk_unmap(data_src);
225 scatterwalk_advance(&walk, n);
228 crypto_yield(pctx->
flags);
237 padlen = 16 - pctx->
ilen;
240 crypto_cipher_encrypt_one(tfm, odata, odata);
246 unsigned int cryptlen)
248 struct crypto_aead *aead = crypto_aead_reqtfm(req);
252 unsigned int assoclen = req->
assoclen;
258 err = format_input(odata, req, cryptlen);
263 crypto_cipher_encrypt_one(cipher, odata, odata);
267 pctx->
ilen = format_adata(idata, assoclen);
268 get_data_to_compute(cipher, pctx, req->
assoc, req->
assoclen);
274 get_data_to_compute(cipher, pctx, plain, cryptlen);
283 struct crypto_aead *aead = crypto_aead_reqtfm(req);
289 crypto_aead_authsize(aead), 1);
290 aead_request_complete(req, err);
293 static inline int crypto_ccm_check_iv(
const u8 *
iv)
296 if (1 > iv[0] || iv[0] > 7)
304 struct crypto_aead *aead = crypto_aead_reqtfm(req);
309 unsigned int cryptlen = req->
cryptlen;
314 err = crypto_ccm_check_iv(iv);
318 pctx->
flags = aead_request_flags(req);
320 err = crypto_ccm_auth(req, req->
src, cryptlen);
327 memset(iv + 15 - iv[0], 0, iv[0] + 1);
330 sg_set_buf(pctx->
src, odata, 16);
331 scatterwalk_sg_chain(pctx->
src, 2, req->
src);
334 if (req->
src != req->
dst) {
336 sg_set_buf(pctx->
dst, odata, 16);
337 scatterwalk_sg_chain(pctx->
dst, 2, req->
dst);
341 ablkcipher_request_set_tfm(abreq, ctx->
ctr);
342 ablkcipher_request_set_callback(abreq, pctx->
flags,
343 crypto_ccm_encrypt_done, req);
344 ablkcipher_request_set_crypt(abreq, pctx->
src, dst, cryptlen + 16, iv);
345 err = crypto_ablkcipher_encrypt(abreq);
351 crypto_aead_authsize(aead), 1);
360 struct crypto_aead *aead = crypto_aead_reqtfm(req);
361 unsigned int authsize = crypto_aead_authsize(aead);
362 unsigned int cryptlen = req->
cryptlen - authsize;
365 err = crypto_ccm_auth(req, req->
dst, cryptlen);
369 aead_request_complete(req, err);
374 struct crypto_aead *aead = crypto_aead_reqtfm(req);
379 unsigned int authsize = crypto_aead_authsize(aead);
380 unsigned int cryptlen = req->
cryptlen;
386 if (cryptlen < authsize)
388 cryptlen -= authsize;
390 err = crypto_ccm_check_iv(iv);
394 pctx->
flags = aead_request_flags(req);
398 memset(iv + 15 - iv[0], 0, iv[0] + 1);
401 sg_set_buf(pctx->
src, authtag, 16);
402 scatterwalk_sg_chain(pctx->
src, 2, req->
src);
405 if (req->
src != req->
dst) {
407 sg_set_buf(pctx->
dst, authtag, 16);
408 scatterwalk_sg_chain(pctx->
dst, 2, req->
dst);
412 ablkcipher_request_set_tfm(abreq, ctx->
ctr);
413 ablkcipher_request_set_callback(abreq, pctx->
flags,
414 crypto_ccm_decrypt_done, req);
415 ablkcipher_request_set_crypt(abreq, pctx->
src, dst, cryptlen + 16, iv);
416 err = crypto_ablkcipher_decrypt(abreq);
420 err = crypto_ccm_auth(req, req->
dst, cryptlen);
425 if (
memcmp(authtag, odata, authsize))
431 static int crypto_ccm_init_tfm(
struct crypto_tfm *tfm)
441 cipher = crypto_spawn_cipher(&ictx->cipher);
443 return PTR_ERR(cipher);
445 ctr = crypto_spawn_skcipher(&ictx->ctr);
448 goto err_free_cipher;
453 align = crypto_tfm_alg_alignmask(tfm);
454 align &= ~(crypto_tfm_ctx_alignment() - 1);
455 tfm->crt_aead.reqsize = align +
457 crypto_ablkcipher_reqsize(ctr);
462 crypto_free_cipher(cipher);
466 static void crypto_ccm_exit_tfm(
struct crypto_tfm *tfm)
470 crypto_free_cipher(ctx->
cipher);
471 crypto_free_ablkcipher(ctx->
ctr);
475 const char *full_name,
476 const char *ctr_name,
477 const char *cipher_name)
496 err = PTR_ERR(cipher);
504 inst = kzalloc(
sizeof(*inst) +
sizeof(*ictx),
GFP_KERNEL);
509 ictx = crypto_instance_ctx(inst);
516 crypto_set_skcipher_spawn(&ictx->
ctr, inst);
518 crypto_requires_sync(algt->
type,
521 goto err_drop_cipher;
523 ctr = crypto_skcipher_spawn_alg(&ictx->
ctr);
531 if (ctr->cra_ablkcipher.ivsize != 16)
545 inst->
alg.cra_blocksize = 1;
547 (__alignof__(
u32) - 1);
549 inst->
alg.cra_aead.ivsize = 16;
550 inst->
alg.cra_aead.maxauthsize = 16;
552 inst->
alg.cra_init = crypto_ccm_init_tfm;
553 inst->
alg.cra_exit = crypto_ccm_exit_tfm;
554 inst->
alg.cra_aead.setkey = crypto_ccm_setkey;
555 inst->
alg.cra_aead.setauthsize = crypto_ccm_setauthsize;
556 inst->
alg.cra_aead.encrypt = crypto_ccm_encrypt;
557 inst->
alg.cra_aead.decrypt = crypto_ccm_decrypt;
564 crypto_drop_skcipher(&ictx->
ctr);
577 const char *cipher_name;
582 err = PTR_ERR(cipher_name);
583 if (IS_ERR(cipher_name))
594 return crypto_ccm_alloc_common(tb, full_name, ctr_name, cipher_name);
602 crypto_drop_skcipher(&ctx->
ctr);
608 .alloc = crypto_ccm_alloc,
609 .free = crypto_ccm_free,
616 const char *ctr_name;
617 const char *cipher_name;
621 err = PTR_ERR(ctr_name);
622 if (IS_ERR(ctr_name))
626 err = PTR_ERR(cipher_name);
627 if (IS_ERR(cipher_name))
634 return crypto_ccm_alloc_common(tb, full_name, ctr_name, cipher_name);
639 .alloc = crypto_ccm_base_alloc,
640 .free = crypto_ccm_free,
644 static int crypto_rfc4309_setkey(
struct crypto_aead *parent,
const u8 *key,
658 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
660 err = crypto_aead_setkey(child, key, keylen);
661 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
667 static int crypto_rfc4309_setauthsize(
struct crypto_aead *parent,
668 unsigned int authsize)
687 struct crypto_aead *aead = crypto_aead_reqtfm(req);
690 u8 *iv =
PTR_ALIGN((
u8 *)(subreq + 1) + crypto_aead_reqsize(child),
691 crypto_aead_alignmask(child) + 1);
699 aead_request_set_tfm(subreq, child);
700 aead_request_set_callback(subreq, req->
base.flags, req->
base.complete,
702 aead_request_set_crypt(subreq, req->
src, req->
dst, req->
cryptlen, iv);
708 static int crypto_rfc4309_encrypt(
struct aead_request *req)
710 req = crypto_rfc4309_crypt(req);
712 return crypto_aead_encrypt(req);
715 static int crypto_rfc4309_decrypt(
struct aead_request *req)
717 req = crypto_rfc4309_crypt(req);
719 return crypto_aead_decrypt(req);
722 static int crypto_rfc4309_init_tfm(
struct crypto_tfm *tfm)
730 aead = crypto_spawn_aead(spawn);
732 return PTR_ERR(aead);
736 align = crypto_aead_alignmask(aead);
737 align &= ~(crypto_tfm_ctx_alignment() - 1);
739 ALIGN(crypto_aead_reqsize(aead),
740 crypto_tfm_ctx_alignment()) +
746 static void crypto_rfc4309_exit_tfm(
struct crypto_tfm *tfm)
750 crypto_free_aead(ctx->
child);
759 const char *ccm_name;
771 err = PTR_ERR(ccm_name);
772 if (IS_ERR(ccm_name))
775 inst = kzalloc(
sizeof(*inst) +
sizeof(*spawn),
GFP_KERNEL);
779 spawn = crypto_instance_ctx(inst);
780 crypto_set_aead_spawn(spawn, inst);
782 crypto_requires_sync(algt->
type, algt->
mask));
786 alg = crypto_aead_spawn_alg(spawn);
791 if (alg->cra_aead.ivsize != 16)
809 inst->
alg.cra_blocksize = 1;
813 inst->
alg.cra_aead.ivsize = 8;
814 inst->
alg.cra_aead.maxauthsize = 16;
818 inst->
alg.cra_init = crypto_rfc4309_init_tfm;
819 inst->
alg.cra_exit = crypto_rfc4309_exit_tfm;
821 inst->
alg.cra_aead.setkey = crypto_rfc4309_setkey;
822 inst->
alg.cra_aead.setauthsize = crypto_rfc4309_setauthsize;
823 inst->
alg.cra_aead.encrypt = crypto_rfc4309_encrypt;
824 inst->
alg.cra_aead.decrypt = crypto_rfc4309_decrypt;
826 inst->
alg.cra_aead.geniv =
"seqiv";
832 crypto_drop_aead(spawn);
847 .alloc = crypto_rfc4309_alloc,
848 .free = crypto_rfc4309_free,
852 static int __init crypto_ccm_module_init(
void)
878 static void __exit crypto_ccm_module_exit(
void)