1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ChaCha20-Poly1305 AEAD, RFC7539
5 * Copyright (C) 2015 Martin Willi
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 <crypto/chacha.h>
13 #include <crypto/poly1305.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
21 struct chachapoly_instance_ctx {
22 struct crypto_skcipher_spawn chacha;
23 struct crypto_ahash_spawn poly;
27 struct chachapoly_ctx {
28 struct crypto_skcipher *chacha;
29 struct crypto_ahash *poly;
30 /* key bytes we use for the ChaCha20 IV */
36 /* zero byte padding for AD/ciphertext, as needed */
37 u8 pad[POLY1305_BLOCK_SIZE];
38 /* tail data with AD/ciphertext lengths */
43 struct scatterlist src[1];
44 struct ahash_request req; /* must be last member */
48 u8 iv[CHACHA_IV_SIZE];
49 struct scatterlist src[1];
50 struct skcipher_request req; /* must be last member */
53 struct chachapoly_req_ctx {
54 struct scatterlist src[2];
55 struct scatterlist dst[2];
56 /* the key we generate for Poly1305 using Chacha20 */
57 u8 key[POLY1305_KEY_SIZE];
58 /* calculated Poly1305 tag */
59 u8 tag[POLY1305_DIGEST_SIZE];
60 /* length of data to en/decrypt, without ICV */
61 unsigned int cryptlen;
62 /* Actual AD, excluding IV */
63 unsigned int assoclen;
64 /* request flags, with MAY_SLEEP cleared if needed */
68 struct chacha_req chacha;
72 static inline void async_done_continue(struct aead_request *req, int err,
73 int (*cont)(struct aead_request *))
76 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
78 rctx->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
82 if (err != -EINPROGRESS && err != -EBUSY)
83 aead_request_complete(req, err);
86 static void chacha_iv(u8 *iv, struct aead_request *req, u32 icb)
88 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
89 __le32 leicb = cpu_to_le32(icb);
91 memcpy(iv, &leicb, sizeof(leicb));
92 memcpy(iv + sizeof(leicb), ctx->salt, ctx->saltlen);
93 memcpy(iv + sizeof(leicb) + ctx->saltlen, req->iv,
94 CHACHA_IV_SIZE - sizeof(leicb) - ctx->saltlen);
97 static int poly_verify_tag(struct aead_request *req)
99 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
100 u8 tag[sizeof(rctx->tag)];
102 scatterwalk_map_and_copy(tag, req->src,
103 req->assoclen + rctx->cryptlen,
105 if (crypto_memneq(tag, rctx->tag, sizeof(tag)))
110 static int poly_copy_tag(struct aead_request *req)
112 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
114 scatterwalk_map_and_copy(rctx->tag, req->dst,
115 req->assoclen + rctx->cryptlen,
116 sizeof(rctx->tag), 1);
120 static void chacha_decrypt_done(struct crypto_async_request *areq, int err)
122 async_done_continue(areq->data, err, poly_verify_tag);
125 static int chacha_decrypt(struct aead_request *req)
127 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
128 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
129 struct chacha_req *creq = &rctx->u.chacha;
130 struct scatterlist *src, *dst;
133 if (rctx->cryptlen == 0)
136 chacha_iv(creq->iv, req, 1);
138 src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
140 if (req->src != req->dst)
141 dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
143 skcipher_request_set_callback(&creq->req, rctx->flags,
144 chacha_decrypt_done, req);
145 skcipher_request_set_tfm(&creq->req, ctx->chacha);
146 skcipher_request_set_crypt(&creq->req, src, dst,
147 rctx->cryptlen, creq->iv);
148 err = crypto_skcipher_decrypt(&creq->req);
153 return poly_verify_tag(req);
156 static int poly_tail_continue(struct aead_request *req)
158 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
160 if (rctx->cryptlen == req->cryptlen) /* encrypting */
161 return poly_copy_tag(req);
163 return chacha_decrypt(req);
166 static void poly_tail_done(struct crypto_async_request *areq, int err)
168 async_done_continue(areq->data, err, poly_tail_continue);
171 static int poly_tail(struct aead_request *req)
173 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
174 struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
175 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
176 struct poly_req *preq = &rctx->u.poly;
179 preq->tail.assoclen = cpu_to_le64(rctx->assoclen);
180 preq->tail.cryptlen = cpu_to_le64(rctx->cryptlen);
181 sg_init_one(preq->src, &preq->tail, sizeof(preq->tail));
183 ahash_request_set_callback(&preq->req, rctx->flags,
184 poly_tail_done, req);
185 ahash_request_set_tfm(&preq->req, ctx->poly);
186 ahash_request_set_crypt(&preq->req, preq->src,
187 rctx->tag, sizeof(preq->tail));
189 err = crypto_ahash_finup(&preq->req);
193 return poly_tail_continue(req);
196 static void poly_cipherpad_done(struct crypto_async_request *areq, int err)
198 async_done_continue(areq->data, err, poly_tail);
201 static int poly_cipherpad(struct aead_request *req)
203 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
204 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
205 struct poly_req *preq = &rctx->u.poly;
209 padlen = -rctx->cryptlen % POLY1305_BLOCK_SIZE;
210 memset(preq->pad, 0, sizeof(preq->pad));
211 sg_init_one(preq->src, preq->pad, padlen);
213 ahash_request_set_callback(&preq->req, rctx->flags,
214 poly_cipherpad_done, req);
215 ahash_request_set_tfm(&preq->req, ctx->poly);
216 ahash_request_set_crypt(&preq->req, preq->src, NULL, padlen);
218 err = crypto_ahash_update(&preq->req);
222 return poly_tail(req);
225 static void poly_cipher_done(struct crypto_async_request *areq, int err)
227 async_done_continue(areq->data, err, poly_cipherpad);
230 static int poly_cipher(struct aead_request *req)
232 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
233 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
234 struct poly_req *preq = &rctx->u.poly;
235 struct scatterlist *crypt = req->src;
238 if (rctx->cryptlen == req->cryptlen) /* encrypting */
241 crypt = scatterwalk_ffwd(rctx->src, crypt, req->assoclen);
243 ahash_request_set_callback(&preq->req, rctx->flags,
244 poly_cipher_done, req);
245 ahash_request_set_tfm(&preq->req, ctx->poly);
246 ahash_request_set_crypt(&preq->req, crypt, NULL, rctx->cryptlen);
248 err = crypto_ahash_update(&preq->req);
252 return poly_cipherpad(req);
255 static void poly_adpad_done(struct crypto_async_request *areq, int err)
257 async_done_continue(areq->data, err, poly_cipher);
260 static int poly_adpad(struct aead_request *req)
262 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
263 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
264 struct poly_req *preq = &rctx->u.poly;
268 padlen = -rctx->assoclen % POLY1305_BLOCK_SIZE;
269 memset(preq->pad, 0, sizeof(preq->pad));
270 sg_init_one(preq->src, preq->pad, padlen);
272 ahash_request_set_callback(&preq->req, rctx->flags,
273 poly_adpad_done, req);
274 ahash_request_set_tfm(&preq->req, ctx->poly);
275 ahash_request_set_crypt(&preq->req, preq->src, NULL, padlen);
277 err = crypto_ahash_update(&preq->req);
281 return poly_cipher(req);
284 static void poly_ad_done(struct crypto_async_request *areq, int err)
286 async_done_continue(areq->data, err, poly_adpad);
289 static int poly_ad(struct aead_request *req)
291 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
292 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
293 struct poly_req *preq = &rctx->u.poly;
296 ahash_request_set_callback(&preq->req, rctx->flags,
298 ahash_request_set_tfm(&preq->req, ctx->poly);
299 ahash_request_set_crypt(&preq->req, req->src, NULL, rctx->assoclen);
301 err = crypto_ahash_update(&preq->req);
305 return poly_adpad(req);
308 static void poly_setkey_done(struct crypto_async_request *areq, int err)
310 async_done_continue(areq->data, err, poly_ad);
313 static int poly_setkey(struct aead_request *req)
315 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
316 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
317 struct poly_req *preq = &rctx->u.poly;
320 sg_init_one(preq->src, rctx->key, sizeof(rctx->key));
322 ahash_request_set_callback(&preq->req, rctx->flags,
323 poly_setkey_done, req);
324 ahash_request_set_tfm(&preq->req, ctx->poly);
325 ahash_request_set_crypt(&preq->req, preq->src, NULL, sizeof(rctx->key));
327 err = crypto_ahash_update(&preq->req);
334 static void poly_init_done(struct crypto_async_request *areq, int err)
336 async_done_continue(areq->data, err, poly_setkey);
339 static int poly_init(struct aead_request *req)
341 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
342 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
343 struct poly_req *preq = &rctx->u.poly;
346 ahash_request_set_callback(&preq->req, rctx->flags,
347 poly_init_done, req);
348 ahash_request_set_tfm(&preq->req, ctx->poly);
350 err = crypto_ahash_init(&preq->req);
354 return poly_setkey(req);
357 static void poly_genkey_done(struct crypto_async_request *areq, int err)
359 async_done_continue(areq->data, err, poly_init);
362 static int poly_genkey(struct aead_request *req)
364 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
365 struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
366 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
367 struct chacha_req *creq = &rctx->u.chacha;
370 rctx->assoclen = req->assoclen;
372 if (crypto_aead_ivsize(tfm) == 8) {
373 if (rctx->assoclen < 8)
378 memset(rctx->key, 0, sizeof(rctx->key));
379 sg_init_one(creq->src, rctx->key, sizeof(rctx->key));
381 chacha_iv(creq->iv, req, 0);
383 skcipher_request_set_callback(&creq->req, rctx->flags,
384 poly_genkey_done, req);
385 skcipher_request_set_tfm(&creq->req, ctx->chacha);
386 skcipher_request_set_crypt(&creq->req, creq->src, creq->src,
387 POLY1305_KEY_SIZE, creq->iv);
389 err = crypto_skcipher_decrypt(&creq->req);
393 return poly_init(req);
396 static void chacha_encrypt_done(struct crypto_async_request *areq, int err)
398 async_done_continue(areq->data, err, poly_genkey);
401 static int chacha_encrypt(struct aead_request *req)
403 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
404 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
405 struct chacha_req *creq = &rctx->u.chacha;
406 struct scatterlist *src, *dst;
409 if (req->cryptlen == 0)
412 chacha_iv(creq->iv, req, 1);
414 src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
416 if (req->src != req->dst)
417 dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
419 skcipher_request_set_callback(&creq->req, rctx->flags,
420 chacha_encrypt_done, req);
421 skcipher_request_set_tfm(&creq->req, ctx->chacha);
422 skcipher_request_set_crypt(&creq->req, src, dst,
423 req->cryptlen, creq->iv);
424 err = crypto_skcipher_encrypt(&creq->req);
429 return poly_genkey(req);
432 static int chachapoly_encrypt(struct aead_request *req)
434 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
436 rctx->cryptlen = req->cryptlen;
437 rctx->flags = aead_request_flags(req);
439 /* encrypt call chain:
440 * - chacha_encrypt/done()
441 * - poly_genkey/done()
443 * - poly_setkey/done()
445 * - poly_adpad/done()
446 * - poly_cipher/done()
447 * - poly_cipherpad/done()
448 * - poly_tail/done/continue()
451 return chacha_encrypt(req);
454 static int chachapoly_decrypt(struct aead_request *req)
456 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
458 rctx->cryptlen = req->cryptlen - POLY1305_DIGEST_SIZE;
459 rctx->flags = aead_request_flags(req);
461 /* decrypt call chain:
462 * - poly_genkey/done()
464 * - poly_setkey/done()
466 * - poly_adpad/done()
467 * - poly_cipher/done()
468 * - poly_cipherpad/done()
469 * - poly_tail/done/continue()
470 * - chacha_decrypt/done()
471 * - poly_verify_tag()
473 return poly_genkey(req);
476 static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key,
479 struct chachapoly_ctx *ctx = crypto_aead_ctx(aead);
481 if (keylen != ctx->saltlen + CHACHA_KEY_SIZE)
484 keylen -= ctx->saltlen;
485 memcpy(ctx->salt, key + keylen, ctx->saltlen);
487 crypto_skcipher_clear_flags(ctx->chacha, CRYPTO_TFM_REQ_MASK);
488 crypto_skcipher_set_flags(ctx->chacha, crypto_aead_get_flags(aead) &
489 CRYPTO_TFM_REQ_MASK);
490 return crypto_skcipher_setkey(ctx->chacha, key, keylen);
493 static int chachapoly_setauthsize(struct crypto_aead *tfm,
494 unsigned int authsize)
496 if (authsize != POLY1305_DIGEST_SIZE)
502 static int chachapoly_init(struct crypto_aead *tfm)
504 struct aead_instance *inst = aead_alg_instance(tfm);
505 struct chachapoly_instance_ctx *ictx = aead_instance_ctx(inst);
506 struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
507 struct crypto_skcipher *chacha;
508 struct crypto_ahash *poly;
511 poly = crypto_spawn_ahash(&ictx->poly);
513 return PTR_ERR(poly);
515 chacha = crypto_spawn_skcipher(&ictx->chacha);
516 if (IS_ERR(chacha)) {
517 crypto_free_ahash(poly);
518 return PTR_ERR(chacha);
521 ctx->chacha = chacha;
523 ctx->saltlen = ictx->saltlen;
525 align = crypto_aead_alignmask(tfm);
526 align &= ~(crypto_tfm_ctx_alignment() - 1);
527 crypto_aead_set_reqsize(
529 align + offsetof(struct chachapoly_req_ctx, u) +
530 max(offsetof(struct chacha_req, req) +
531 sizeof(struct skcipher_request) +
532 crypto_skcipher_reqsize(chacha),
533 offsetof(struct poly_req, req) +
534 sizeof(struct ahash_request) +
535 crypto_ahash_reqsize(poly)));
540 static void chachapoly_exit(struct crypto_aead *tfm)
542 struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
544 crypto_free_ahash(ctx->poly);
545 crypto_free_skcipher(ctx->chacha);
548 static void chachapoly_free(struct aead_instance *inst)
550 struct chachapoly_instance_ctx *ctx = aead_instance_ctx(inst);
552 crypto_drop_skcipher(&ctx->chacha);
553 crypto_drop_ahash(&ctx->poly);
557 static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
558 const char *name, unsigned int ivsize)
560 struct crypto_attr_type *algt;
561 struct aead_instance *inst;
562 struct skcipher_alg *chacha;
563 struct crypto_alg *poly;
564 struct hash_alg_common *poly_hash;
565 struct chachapoly_instance_ctx *ctx;
566 const char *chacha_name, *poly_name;
569 if (ivsize > CHACHAPOLY_IV_SIZE)
572 algt = crypto_get_attr_type(tb);
574 return PTR_ERR(algt);
576 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
579 chacha_name = crypto_attr_alg_name(tb[1]);
580 if (IS_ERR(chacha_name))
581 return PTR_ERR(chacha_name);
582 poly_name = crypto_attr_alg_name(tb[2]);
583 if (IS_ERR(poly_name))
584 return PTR_ERR(poly_name);
586 poly = crypto_find_alg(poly_name, &crypto_ahash_type,
587 CRYPTO_ALG_TYPE_HASH,
588 CRYPTO_ALG_TYPE_AHASH_MASK |
589 crypto_requires_sync(algt->type,
592 return PTR_ERR(poly);
593 poly_hash = __crypto_hash_alg_common(poly);
596 if (poly_hash->digestsize != POLY1305_DIGEST_SIZE)
600 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
604 ctx = aead_instance_ctx(inst);
605 ctx->saltlen = CHACHAPOLY_IV_SIZE - ivsize;
606 err = crypto_init_ahash_spawn(&ctx->poly, poly_hash,
607 aead_crypto_instance(inst));
611 crypto_set_skcipher_spawn(&ctx->chacha, aead_crypto_instance(inst));
612 err = crypto_grab_skcipher(&ctx->chacha, chacha_name, 0,
613 crypto_requires_sync(algt->type,
618 chacha = crypto_spawn_skcipher_alg(&ctx->chacha);
621 /* Need 16-byte IV size, including Initial Block Counter value */
622 if (crypto_skcipher_alg_ivsize(chacha) != CHACHA_IV_SIZE)
623 goto out_drop_chacha;
624 /* Not a stream cipher? */
625 if (chacha->base.cra_blocksize != 1)
626 goto out_drop_chacha;
629 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
630 "%s(%s,%s)", name, chacha->base.cra_name,
631 poly->cra_name) >= CRYPTO_MAX_ALG_NAME)
632 goto out_drop_chacha;
633 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
634 "%s(%s,%s)", name, chacha->base.cra_driver_name,
635 poly->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
636 goto out_drop_chacha;
638 inst->alg.base.cra_flags = (chacha->base.cra_flags | poly->cra_flags) &
640 inst->alg.base.cra_priority = (chacha->base.cra_priority +
641 poly->cra_priority) / 2;
642 inst->alg.base.cra_blocksize = 1;
643 inst->alg.base.cra_alignmask = chacha->base.cra_alignmask |
645 inst->alg.base.cra_ctxsize = sizeof(struct chachapoly_ctx) +
647 inst->alg.ivsize = ivsize;
648 inst->alg.chunksize = crypto_skcipher_alg_chunksize(chacha);
649 inst->alg.maxauthsize = POLY1305_DIGEST_SIZE;
650 inst->alg.init = chachapoly_init;
651 inst->alg.exit = chachapoly_exit;
652 inst->alg.encrypt = chachapoly_encrypt;
653 inst->alg.decrypt = chachapoly_decrypt;
654 inst->alg.setkey = chachapoly_setkey;
655 inst->alg.setauthsize = chachapoly_setauthsize;
657 inst->free = chachapoly_free;
659 err = aead_register_instance(tmpl, inst);
661 goto out_drop_chacha;
664 crypto_mod_put(poly);
668 crypto_drop_skcipher(&ctx->chacha);
670 crypto_drop_ahash(&ctx->poly);
676 static int rfc7539_create(struct crypto_template *tmpl, struct rtattr **tb)
678 return chachapoly_create(tmpl, tb, "rfc7539", 12);
681 static int rfc7539esp_create(struct crypto_template *tmpl, struct rtattr **tb)
683 return chachapoly_create(tmpl, tb, "rfc7539esp", 8);
686 static struct crypto_template rfc7539_tmpls[] = {
689 .create = rfc7539_create,
690 .module = THIS_MODULE,
692 .name = "rfc7539esp",
693 .create = rfc7539esp_create,
694 .module = THIS_MODULE,
698 static int __init chacha20poly1305_module_init(void)
700 return crypto_register_templates(rfc7539_tmpls,
701 ARRAY_SIZE(rfc7539_tmpls));
704 static void __exit chacha20poly1305_module_exit(void)
706 crypto_unregister_templates(rfc7539_tmpls,
707 ARRAY_SIZE(rfc7539_tmpls));
710 subsys_initcall(chacha20poly1305_module_init);
711 module_exit(chacha20poly1305_module_exit);
713 MODULE_LICENSE("GPL");
714 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
715 MODULE_DESCRIPTION("ChaCha20-Poly1305 AEAD");
716 MODULE_ALIAS_CRYPTO("rfc7539");
717 MODULE_ALIAS_CRYPTO("rfc7539esp");