2 * GCM: Galois/Counter Mode.
4 * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
11 #include <crypto/gf128mul.h>
12 #include <crypto/internal/aead.h>
13 #include <crypto/internal/skcipher.h>
14 #include <crypto/internal/hash.h>
15 #include <crypto/scatterwalk.h>
16 #include <crypto/hash.h>
18 #include <linux/completion.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
25 struct gcm_instance_ctx {
26 struct crypto_skcipher_spawn ctr;
27 struct crypto_ahash_spawn ghash;
30 struct crypto_gcm_ctx {
31 struct crypto_ablkcipher *ctr;
32 struct crypto_ahash *ghash;
35 struct crypto_rfc4106_ctx {
36 struct crypto_aead *child;
40 struct crypto_rfc4543_instance_ctx {
41 struct crypto_aead_spawn aead;
42 struct crypto_skcipher_spawn null;
45 struct crypto_rfc4543_ctx {
46 struct crypto_aead *child;
47 struct crypto_blkcipher *null;
51 struct crypto_rfc4543_req_ctx {
53 struct scatterlist cipher[1];
54 struct scatterlist payload[2];
55 struct scatterlist assoc[2];
56 struct aead_request subreq;
59 struct crypto_gcm_ghash_ctx {
60 unsigned int cryptlen;
61 struct scatterlist *src;
62 void (*complete)(struct aead_request *req, int err);
65 struct crypto_gcm_req_priv_ctx {
68 struct scatterlist src[2];
69 struct scatterlist dst[2];
70 struct crypto_gcm_ghash_ctx ghash_ctx;
72 struct ahash_request ahreq;
73 struct ablkcipher_request abreq;
77 struct crypto_gcm_setkey_result {
79 struct completion completion;
82 static void *gcm_zeroes;
84 static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
85 struct aead_request *req)
87 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
89 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
92 static void crypto_gcm_setkey_done(struct crypto_async_request *req, int err)
94 struct crypto_gcm_setkey_result *result = req->data;
96 if (err == -EINPROGRESS)
100 complete(&result->completion);
103 static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
106 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
107 struct crypto_ahash *ghash = ctx->ghash;
108 struct crypto_ablkcipher *ctr = ctx->ctr;
113 struct crypto_gcm_setkey_result result;
115 struct scatterlist sg[1];
116 struct ablkcipher_request req;
120 crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
121 crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
122 CRYPTO_TFM_REQ_MASK);
124 err = crypto_ablkcipher_setkey(ctr, key, keylen);
128 crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
129 CRYPTO_TFM_RES_MASK);
131 data = kzalloc(sizeof(*data) + crypto_ablkcipher_reqsize(ctr),
136 init_completion(&data->result.completion);
137 sg_init_one(data->sg, &data->hash, sizeof(data->hash));
138 ablkcipher_request_set_tfm(&data->req, ctr);
139 ablkcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
140 CRYPTO_TFM_REQ_MAY_BACKLOG,
141 crypto_gcm_setkey_done,
143 ablkcipher_request_set_crypt(&data->req, data->sg, data->sg,
144 sizeof(data->hash), data->iv);
146 err = crypto_ablkcipher_encrypt(&data->req);
147 if (err == -EINPROGRESS || err == -EBUSY) {
148 err = wait_for_completion_interruptible(
149 &data->result.completion);
151 err = data->result.err;
157 crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
158 crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
159 CRYPTO_TFM_REQ_MASK);
160 err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
161 crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) &
162 CRYPTO_TFM_RES_MASK);
169 static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
170 unsigned int authsize)
188 static void crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
189 struct aead_request *req,
190 unsigned int cryptlen)
192 struct crypto_aead *aead = crypto_aead_reqtfm(req);
193 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
194 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
195 struct scatterlist *dst;
196 __be32 counter = cpu_to_be32(1);
198 memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
199 memcpy(req->iv + 12, &counter, 4);
201 sg_init_table(pctx->src, 2);
202 sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
203 scatterwalk_sg_chain(pctx->src, 2, req->src);
206 if (req->src != req->dst) {
207 sg_init_table(pctx->dst, 2);
208 sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
209 scatterwalk_sg_chain(pctx->dst, 2, req->dst);
213 ablkcipher_request_set_tfm(ablk_req, ctx->ctr);
214 ablkcipher_request_set_crypt(ablk_req, pctx->src, dst,
215 cryptlen + sizeof(pctx->auth_tag),
219 static inline unsigned int gcm_remain(unsigned int len)
222 return len ? 16 - len : 0;
225 static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
226 static void gcm_hash_final_done(struct crypto_async_request *areq, int err);
228 static int gcm_hash_update(struct aead_request *req,
229 struct crypto_gcm_req_priv_ctx *pctx,
230 crypto_completion_t complete,
231 struct scatterlist *src,
234 struct ahash_request *ahreq = &pctx->u.ahreq;
236 ahash_request_set_callback(ahreq, aead_request_flags(req),
238 ahash_request_set_crypt(ahreq, src, NULL, len);
240 return crypto_ahash_update(ahreq);
243 static int gcm_hash_remain(struct aead_request *req,
244 struct crypto_gcm_req_priv_ctx *pctx,
246 crypto_completion_t complete)
248 struct ahash_request *ahreq = &pctx->u.ahreq;
250 ahash_request_set_callback(ahreq, aead_request_flags(req),
252 sg_init_one(pctx->src, gcm_zeroes, remain);
253 ahash_request_set_crypt(ahreq, pctx->src, NULL, remain);
255 return crypto_ahash_update(ahreq);
258 static int gcm_hash_len(struct aead_request *req,
259 struct crypto_gcm_req_priv_ctx *pctx)
261 struct ahash_request *ahreq = &pctx->u.ahreq;
262 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
265 lengths.a = cpu_to_be64(req->assoclen * 8);
266 lengths.b = cpu_to_be64(gctx->cryptlen * 8);
267 memcpy(pctx->iauth_tag, &lengths, 16);
268 sg_init_one(pctx->src, pctx->iauth_tag, 16);
269 ahash_request_set_callback(ahreq, aead_request_flags(req),
270 gcm_hash_len_done, req);
271 ahash_request_set_crypt(ahreq, pctx->src,
272 NULL, sizeof(lengths));
274 return crypto_ahash_update(ahreq);
277 static int gcm_hash_final(struct aead_request *req,
278 struct crypto_gcm_req_priv_ctx *pctx)
280 struct ahash_request *ahreq = &pctx->u.ahreq;
282 ahash_request_set_callback(ahreq, aead_request_flags(req),
283 gcm_hash_final_done, req);
284 ahash_request_set_crypt(ahreq, NULL, pctx->iauth_tag, 0);
286 return crypto_ahash_final(ahreq);
289 static void __gcm_hash_final_done(struct aead_request *req, int err)
291 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
292 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
295 crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
297 gctx->complete(req, err);
300 static void gcm_hash_final_done(struct crypto_async_request *areq, int err)
302 struct aead_request *req = areq->data;
304 __gcm_hash_final_done(req, err);
307 static void __gcm_hash_len_done(struct aead_request *req, int err)
309 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
312 err = gcm_hash_final(req, pctx);
313 if (err == -EINPROGRESS || err == -EBUSY)
317 __gcm_hash_final_done(req, err);
320 static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
322 struct aead_request *req = areq->data;
324 __gcm_hash_len_done(req, err);
327 static void __gcm_hash_crypt_remain_done(struct aead_request *req, int err)
329 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
332 err = gcm_hash_len(req, pctx);
333 if (err == -EINPROGRESS || err == -EBUSY)
337 __gcm_hash_len_done(req, err);
340 static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
343 struct aead_request *req = areq->data;
345 __gcm_hash_crypt_remain_done(req, err);
348 static void __gcm_hash_crypt_done(struct aead_request *req, int err)
350 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
351 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
355 remain = gcm_remain(gctx->cryptlen);
357 err = gcm_hash_remain(req, pctx, remain,
358 gcm_hash_crypt_remain_done);
359 if (err == -EINPROGRESS || err == -EBUSY)
363 __gcm_hash_crypt_remain_done(req, err);
366 static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
368 struct aead_request *req = areq->data;
370 __gcm_hash_crypt_done(req, err);
373 static void __gcm_hash_assoc_remain_done(struct aead_request *req, int err)
375 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
376 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
377 crypto_completion_t complete;
378 unsigned int remain = 0;
380 if (!err && gctx->cryptlen) {
381 remain = gcm_remain(gctx->cryptlen);
382 complete = remain ? gcm_hash_crypt_done :
383 gcm_hash_crypt_remain_done;
384 err = gcm_hash_update(req, pctx, complete,
385 gctx->src, gctx->cryptlen);
386 if (err == -EINPROGRESS || err == -EBUSY)
391 __gcm_hash_crypt_done(req, err);
393 __gcm_hash_crypt_remain_done(req, err);
396 static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
399 struct aead_request *req = areq->data;
401 __gcm_hash_assoc_remain_done(req, err);
404 static void __gcm_hash_assoc_done(struct aead_request *req, int err)
406 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
410 remain = gcm_remain(req->assoclen);
412 err = gcm_hash_remain(req, pctx, remain,
413 gcm_hash_assoc_remain_done);
414 if (err == -EINPROGRESS || err == -EBUSY)
418 __gcm_hash_assoc_remain_done(req, err);
421 static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
423 struct aead_request *req = areq->data;
425 __gcm_hash_assoc_done(req, err);
428 static void __gcm_hash_init_done(struct aead_request *req, int err)
430 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
431 crypto_completion_t complete;
432 unsigned int remain = 0;
434 if (!err && req->assoclen) {
435 remain = gcm_remain(req->assoclen);
436 complete = remain ? gcm_hash_assoc_done :
437 gcm_hash_assoc_remain_done;
438 err = gcm_hash_update(req, pctx, complete,
439 req->assoc, req->assoclen);
440 if (err == -EINPROGRESS || err == -EBUSY)
445 __gcm_hash_assoc_done(req, err);
447 __gcm_hash_assoc_remain_done(req, err);
450 static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
452 struct aead_request *req = areq->data;
454 __gcm_hash_init_done(req, err);
457 static int gcm_hash(struct aead_request *req,
458 struct crypto_gcm_req_priv_ctx *pctx)
460 struct ahash_request *ahreq = &pctx->u.ahreq;
461 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
462 struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
464 crypto_completion_t complete;
467 ahash_request_set_tfm(ahreq, ctx->ghash);
469 ahash_request_set_callback(ahreq, aead_request_flags(req),
470 gcm_hash_init_done, req);
471 err = crypto_ahash_init(ahreq);
474 remain = gcm_remain(req->assoclen);
475 complete = remain ? gcm_hash_assoc_done : gcm_hash_assoc_remain_done;
476 err = gcm_hash_update(req, pctx, complete, req->assoc, req->assoclen);
480 err = gcm_hash_remain(req, pctx, remain,
481 gcm_hash_assoc_remain_done);
485 remain = gcm_remain(gctx->cryptlen);
486 complete = remain ? gcm_hash_crypt_done : gcm_hash_crypt_remain_done;
487 err = gcm_hash_update(req, pctx, complete, gctx->src, gctx->cryptlen);
491 err = gcm_hash_remain(req, pctx, remain,
492 gcm_hash_crypt_remain_done);
496 err = gcm_hash_len(req, pctx);
499 err = gcm_hash_final(req, pctx);
506 static void gcm_enc_copy_hash(struct aead_request *req,
507 struct crypto_gcm_req_priv_ctx *pctx)
509 struct crypto_aead *aead = crypto_aead_reqtfm(req);
510 u8 *auth_tag = pctx->auth_tag;
512 scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
513 crypto_aead_authsize(aead), 1);
516 static void gcm_enc_hash_done(struct aead_request *req, int err)
518 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
521 gcm_enc_copy_hash(req, pctx);
523 aead_request_complete(req, err);
526 static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
528 struct aead_request *req = areq->data;
529 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
532 err = gcm_hash(req, pctx);
533 if (err == -EINPROGRESS || err == -EBUSY)
536 crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
537 gcm_enc_copy_hash(req, pctx);
541 aead_request_complete(req, err);
544 static int crypto_gcm_encrypt(struct aead_request *req)
546 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
547 struct ablkcipher_request *abreq = &pctx->u.abreq;
548 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
551 crypto_gcm_init_crypt(abreq, req, req->cryptlen);
552 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
553 gcm_encrypt_done, req);
555 gctx->src = req->dst;
556 gctx->cryptlen = req->cryptlen;
557 gctx->complete = gcm_enc_hash_done;
559 err = crypto_ablkcipher_encrypt(abreq);
563 err = gcm_hash(req, pctx);
567 crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
568 gcm_enc_copy_hash(req, pctx);
573 static int crypto_gcm_verify(struct aead_request *req,
574 struct crypto_gcm_req_priv_ctx *pctx)
576 struct crypto_aead *aead = crypto_aead_reqtfm(req);
577 u8 *auth_tag = pctx->auth_tag;
578 u8 *iauth_tag = pctx->iauth_tag;
579 unsigned int authsize = crypto_aead_authsize(aead);
580 unsigned int cryptlen = req->cryptlen - authsize;
582 crypto_xor(auth_tag, iauth_tag, 16);
583 scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
584 return memcmp(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
587 static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
589 struct aead_request *req = areq->data;
590 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
593 err = crypto_gcm_verify(req, pctx);
595 aead_request_complete(req, err);
598 static void gcm_dec_hash_done(struct aead_request *req, int err)
600 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
601 struct ablkcipher_request *abreq = &pctx->u.abreq;
602 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
605 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
606 gcm_decrypt_done, req);
607 crypto_gcm_init_crypt(abreq, req, gctx->cryptlen);
608 err = crypto_ablkcipher_decrypt(abreq);
609 if (err == -EINPROGRESS || err == -EBUSY)
612 err = crypto_gcm_verify(req, pctx);
615 aead_request_complete(req, err);
618 static int crypto_gcm_decrypt(struct aead_request *req)
620 struct crypto_aead *aead = crypto_aead_reqtfm(req);
621 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
622 struct ablkcipher_request *abreq = &pctx->u.abreq;
623 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
624 unsigned int authsize = crypto_aead_authsize(aead);
625 unsigned int cryptlen = req->cryptlen;
628 if (cryptlen < authsize)
630 cryptlen -= authsize;
632 gctx->src = req->src;
633 gctx->cryptlen = cryptlen;
634 gctx->complete = gcm_dec_hash_done;
636 err = gcm_hash(req, pctx);
640 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
641 gcm_decrypt_done, req);
642 crypto_gcm_init_crypt(abreq, req, cryptlen);
643 err = crypto_ablkcipher_decrypt(abreq);
647 return crypto_gcm_verify(req, pctx);
650 static int crypto_gcm_init_tfm(struct crypto_tfm *tfm)
652 struct crypto_instance *inst = (void *)tfm->__crt_alg;
653 struct gcm_instance_ctx *ictx = crypto_instance_ctx(inst);
654 struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
655 struct crypto_ablkcipher *ctr;
656 struct crypto_ahash *ghash;
660 ghash = crypto_spawn_ahash(&ictx->ghash);
662 return PTR_ERR(ghash);
664 ctr = crypto_spawn_skcipher(&ictx->ctr);
672 align = crypto_tfm_alg_alignmask(tfm);
673 align &= ~(crypto_tfm_ctx_alignment() - 1);
674 tfm->crt_aead.reqsize = align +
675 offsetof(struct crypto_gcm_req_priv_ctx, u) +
676 max(sizeof(struct ablkcipher_request) +
677 crypto_ablkcipher_reqsize(ctr),
678 sizeof(struct ahash_request) +
679 crypto_ahash_reqsize(ghash));
684 crypto_free_ahash(ghash);
688 static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm)
690 struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
692 crypto_free_ahash(ctx->ghash);
693 crypto_free_ablkcipher(ctx->ctr);
696 static struct crypto_instance *crypto_gcm_alloc_common(struct rtattr **tb,
697 const char *full_name,
698 const char *ctr_name,
699 const char *ghash_name)
701 struct crypto_attr_type *algt;
702 struct crypto_instance *inst;
703 struct crypto_alg *ctr;
704 struct crypto_alg *ghash_alg;
705 struct ahash_alg *ghash_ahash_alg;
706 struct gcm_instance_ctx *ctx;
709 algt = crypto_get_attr_type(tb);
711 return ERR_CAST(algt);
713 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
714 return ERR_PTR(-EINVAL);
716 ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
717 CRYPTO_ALG_TYPE_HASH,
718 CRYPTO_ALG_TYPE_AHASH_MASK);
719 if (IS_ERR(ghash_alg))
720 return ERR_CAST(ghash_alg);
723 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
727 ctx = crypto_instance_ctx(inst);
728 ghash_ahash_alg = container_of(ghash_alg, struct ahash_alg, halg.base);
729 err = crypto_init_ahash_spawn(&ctx->ghash, &ghash_ahash_alg->halg,
734 crypto_set_skcipher_spawn(&ctx->ctr, inst);
735 err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
736 crypto_requires_sync(algt->type,
741 ctr = crypto_skcipher_spawn_alg(&ctx->ctr);
743 /* We only support 16-byte blocks. */
744 if (ctr->cra_ablkcipher.ivsize != 16)
747 /* Not a stream cipher? */
749 if (ctr->cra_blocksize != 1)
753 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
754 "gcm_base(%s,%s)", ctr->cra_driver_name,
755 ghash_alg->cra_driver_name) >=
759 memcpy(inst->alg.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
761 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
762 inst->alg.cra_flags |= ctr->cra_flags & CRYPTO_ALG_ASYNC;
763 inst->alg.cra_priority = ctr->cra_priority;
764 inst->alg.cra_blocksize = 1;
765 inst->alg.cra_alignmask = ctr->cra_alignmask | (__alignof__(u64) - 1);
766 inst->alg.cra_type = &crypto_aead_type;
767 inst->alg.cra_aead.ivsize = 16;
768 inst->alg.cra_aead.maxauthsize = 16;
769 inst->alg.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
770 inst->alg.cra_init = crypto_gcm_init_tfm;
771 inst->alg.cra_exit = crypto_gcm_exit_tfm;
772 inst->alg.cra_aead.setkey = crypto_gcm_setkey;
773 inst->alg.cra_aead.setauthsize = crypto_gcm_setauthsize;
774 inst->alg.cra_aead.encrypt = crypto_gcm_encrypt;
775 inst->alg.cra_aead.decrypt = crypto_gcm_decrypt;
778 crypto_mod_put(ghash_alg);
782 crypto_drop_skcipher(&ctx->ctr);
784 crypto_drop_ahash(&ctx->ghash);
792 static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb)
794 const char *cipher_name;
795 char ctr_name[CRYPTO_MAX_ALG_NAME];
796 char full_name[CRYPTO_MAX_ALG_NAME];
798 cipher_name = crypto_attr_alg_name(tb[1]);
799 if (IS_ERR(cipher_name))
800 return ERR_CAST(cipher_name);
802 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
804 return ERR_PTR(-ENAMETOOLONG);
806 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
808 return ERR_PTR(-ENAMETOOLONG);
810 return crypto_gcm_alloc_common(tb, full_name, ctr_name, "ghash");
813 static void crypto_gcm_free(struct crypto_instance *inst)
815 struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst);
817 crypto_drop_skcipher(&ctx->ctr);
818 crypto_drop_ahash(&ctx->ghash);
822 static struct crypto_template crypto_gcm_tmpl = {
824 .alloc = crypto_gcm_alloc,
825 .free = crypto_gcm_free,
826 .module = THIS_MODULE,
829 static struct crypto_instance *crypto_gcm_base_alloc(struct rtattr **tb)
831 const char *ctr_name;
832 const char *ghash_name;
833 char full_name[CRYPTO_MAX_ALG_NAME];
835 ctr_name = crypto_attr_alg_name(tb[1]);
836 if (IS_ERR(ctr_name))
837 return ERR_CAST(ctr_name);
839 ghash_name = crypto_attr_alg_name(tb[2]);
840 if (IS_ERR(ghash_name))
841 return ERR_CAST(ghash_name);
843 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
844 ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
845 return ERR_PTR(-ENAMETOOLONG);
847 return crypto_gcm_alloc_common(tb, full_name, ctr_name, ghash_name);
850 static struct crypto_template crypto_gcm_base_tmpl = {
852 .alloc = crypto_gcm_base_alloc,
853 .free = crypto_gcm_free,
854 .module = THIS_MODULE,
857 static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
860 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
861 struct crypto_aead *child = ctx->child;
868 memcpy(ctx->nonce, key + keylen, 4);
870 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
871 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
872 CRYPTO_TFM_REQ_MASK);
873 err = crypto_aead_setkey(child, key, keylen);
874 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
875 CRYPTO_TFM_RES_MASK);
880 static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
881 unsigned int authsize)
883 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
894 return crypto_aead_setauthsize(ctx->child, authsize);
897 static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
899 struct aead_request *subreq = aead_request_ctx(req);
900 struct crypto_aead *aead = crypto_aead_reqtfm(req);
901 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
902 struct crypto_aead *child = ctx->child;
903 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
904 crypto_aead_alignmask(child) + 1);
906 memcpy(iv, ctx->nonce, 4);
907 memcpy(iv + 4, req->iv, 8);
909 aead_request_set_tfm(subreq, child);
910 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
912 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, iv);
913 aead_request_set_assoc(subreq, req->assoc, req->assoclen);
918 static int crypto_rfc4106_encrypt(struct aead_request *req)
920 req = crypto_rfc4106_crypt(req);
922 return crypto_aead_encrypt(req);
925 static int crypto_rfc4106_decrypt(struct aead_request *req)
927 req = crypto_rfc4106_crypt(req);
929 return crypto_aead_decrypt(req);
932 static int crypto_rfc4106_init_tfm(struct crypto_tfm *tfm)
934 struct crypto_instance *inst = (void *)tfm->__crt_alg;
935 struct crypto_aead_spawn *spawn = crypto_instance_ctx(inst);
936 struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm);
937 struct crypto_aead *aead;
940 aead = crypto_spawn_aead(spawn);
942 return PTR_ERR(aead);
946 align = crypto_aead_alignmask(aead);
947 align &= ~(crypto_tfm_ctx_alignment() - 1);
948 tfm->crt_aead.reqsize = sizeof(struct aead_request) +
949 ALIGN(crypto_aead_reqsize(aead),
950 crypto_tfm_ctx_alignment()) +
956 static void crypto_rfc4106_exit_tfm(struct crypto_tfm *tfm)
958 struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm);
960 crypto_free_aead(ctx->child);
963 static struct crypto_instance *crypto_rfc4106_alloc(struct rtattr **tb)
965 struct crypto_attr_type *algt;
966 struct crypto_instance *inst;
967 struct crypto_aead_spawn *spawn;
968 struct crypto_alg *alg;
969 const char *ccm_name;
972 algt = crypto_get_attr_type(tb);
974 return ERR_CAST(algt);
976 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
977 return ERR_PTR(-EINVAL);
979 ccm_name = crypto_attr_alg_name(tb[1]);
980 if (IS_ERR(ccm_name))
981 return ERR_CAST(ccm_name);
983 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
985 return ERR_PTR(-ENOMEM);
987 spawn = crypto_instance_ctx(inst);
988 crypto_set_aead_spawn(spawn, inst);
989 err = crypto_grab_aead(spawn, ccm_name, 0,
990 crypto_requires_sync(algt->type, algt->mask));
994 alg = crypto_aead_spawn_alg(spawn);
998 /* We only support 16-byte blocks. */
999 if (alg->cra_aead.ivsize != 16)
1002 /* Not a stream cipher? */
1003 if (alg->cra_blocksize != 1)
1006 err = -ENAMETOOLONG;
1007 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
1008 "rfc4106(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
1009 snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1010 "rfc4106(%s)", alg->cra_driver_name) >=
1011 CRYPTO_MAX_ALG_NAME)
1014 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
1015 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
1016 inst->alg.cra_priority = alg->cra_priority;
1017 inst->alg.cra_blocksize = 1;
1018 inst->alg.cra_alignmask = alg->cra_alignmask;
1019 inst->alg.cra_type = &crypto_nivaead_type;
1021 inst->alg.cra_aead.ivsize = 8;
1022 inst->alg.cra_aead.maxauthsize = 16;
1024 inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
1026 inst->alg.cra_init = crypto_rfc4106_init_tfm;
1027 inst->alg.cra_exit = crypto_rfc4106_exit_tfm;
1029 inst->alg.cra_aead.setkey = crypto_rfc4106_setkey;
1030 inst->alg.cra_aead.setauthsize = crypto_rfc4106_setauthsize;
1031 inst->alg.cra_aead.encrypt = crypto_rfc4106_encrypt;
1032 inst->alg.cra_aead.decrypt = crypto_rfc4106_decrypt;
1034 inst->alg.cra_aead.geniv = "seqiv";
1040 crypto_drop_aead(spawn);
1043 inst = ERR_PTR(err);
1047 static void crypto_rfc4106_free(struct crypto_instance *inst)
1049 crypto_drop_spawn(crypto_instance_ctx(inst));
1053 static struct crypto_template crypto_rfc4106_tmpl = {
1055 .alloc = crypto_rfc4106_alloc,
1056 .free = crypto_rfc4106_free,
1057 .module = THIS_MODULE,
1060 static inline struct crypto_rfc4543_req_ctx *crypto_rfc4543_reqctx(
1061 struct aead_request *req)
1063 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
1065 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
1068 static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
1069 unsigned int keylen)
1071 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1072 struct crypto_aead *child = ctx->child;
1079 memcpy(ctx->nonce, key + keylen, 4);
1081 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
1082 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
1083 CRYPTO_TFM_REQ_MASK);
1084 err = crypto_aead_setkey(child, key, keylen);
1085 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
1086 CRYPTO_TFM_RES_MASK);
1091 static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
1092 unsigned int authsize)
1094 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1099 return crypto_aead_setauthsize(ctx->child, authsize);
1102 static void crypto_rfc4543_done(struct crypto_async_request *areq, int err)
1104 struct aead_request *req = areq->data;
1105 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1106 struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
1109 scatterwalk_map_and_copy(rctx->auth_tag, req->dst,
1111 crypto_aead_authsize(aead), 1);
1114 aead_request_complete(req, err);
1117 static struct aead_request *crypto_rfc4543_crypt(struct aead_request *req,
1120 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1121 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1122 struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
1123 struct aead_request *subreq = &rctx->subreq;
1124 struct scatterlist *src = req->src;
1125 struct scatterlist *cipher = rctx->cipher;
1126 struct scatterlist *payload = rctx->payload;
1127 struct scatterlist *assoc = rctx->assoc;
1128 unsigned int authsize = crypto_aead_authsize(aead);
1129 unsigned int assoclen = req->assoclen;
1132 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
1133 crypto_aead_alignmask(ctx->child) + 1);
1135 memcpy(iv, ctx->nonce, 4);
1136 memcpy(iv + 4, req->iv, 8);
1138 /* construct cipher/plaintext */
1140 memset(rctx->auth_tag, 0, authsize);
1142 scatterwalk_map_and_copy(rctx->auth_tag, src,
1143 req->cryptlen - authsize,
1146 sg_init_one(cipher, rctx->auth_tag, authsize);
1148 /* construct the aad */
1149 srcp = sg_page(src);
1150 vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
1152 sg_init_table(payload, 2);
1153 sg_set_buf(payload, req->iv, 8);
1154 scatterwalk_crypto_chain(payload, src, vsrc == req->iv + 8, 2);
1155 assoclen += 8 + req->cryptlen - (enc ? 0 : authsize);
1157 sg_init_table(assoc, 2);
1158 sg_set_page(assoc, sg_page(req->assoc), req->assoc->length,
1159 req->assoc->offset);
1160 scatterwalk_crypto_chain(assoc, payload, 0, 2);
1162 aead_request_set_tfm(subreq, ctx->child);
1163 aead_request_set_callback(subreq, req->base.flags, crypto_rfc4543_done,
1165 aead_request_set_crypt(subreq, cipher, cipher, enc ? 0 : authsize, iv);
1166 aead_request_set_assoc(subreq, assoc, assoclen);
1171 static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1173 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1174 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1175 unsigned int authsize = crypto_aead_authsize(aead);
1176 unsigned int nbytes = req->cryptlen - (enc ? 0 : authsize);
1177 struct blkcipher_desc desc = {
1181 return crypto_blkcipher_encrypt(&desc, req->dst, req->src, nbytes);
1184 static int crypto_rfc4543_encrypt(struct aead_request *req)
1186 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1187 struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
1188 struct aead_request *subreq;
1191 if (req->src != req->dst) {
1192 err = crypto_rfc4543_copy_src_to_dst(req, true);
1197 subreq = crypto_rfc4543_crypt(req, true);
1198 err = crypto_aead_encrypt(subreq);
1202 scatterwalk_map_and_copy(rctx->auth_tag, req->dst, req->cryptlen,
1203 crypto_aead_authsize(aead), 1);
1208 static int crypto_rfc4543_decrypt(struct aead_request *req)
1212 if (req->src != req->dst) {
1213 err = crypto_rfc4543_copy_src_to_dst(req, false);
1218 req = crypto_rfc4543_crypt(req, false);
1220 return crypto_aead_decrypt(req);
1223 static int crypto_rfc4543_init_tfm(struct crypto_tfm *tfm)
1225 struct crypto_instance *inst = (void *)tfm->__crt_alg;
1226 struct crypto_rfc4543_instance_ctx *ictx = crypto_instance_ctx(inst);
1227 struct crypto_aead_spawn *spawn = &ictx->aead;
1228 struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm);
1229 struct crypto_aead *aead;
1230 struct crypto_blkcipher *null;
1231 unsigned long align;
1234 aead = crypto_spawn_aead(spawn);
1236 return PTR_ERR(aead);
1238 null = crypto_spawn_blkcipher(&ictx->null.base);
1239 err = PTR_ERR(null);
1246 align = crypto_aead_alignmask(aead);
1247 align &= ~(crypto_tfm_ctx_alignment() - 1);
1248 tfm->crt_aead.reqsize = sizeof(struct crypto_rfc4543_req_ctx) +
1249 ALIGN(crypto_aead_reqsize(aead),
1250 crypto_tfm_ctx_alignment()) +
1256 crypto_free_aead(aead);
1260 static void crypto_rfc4543_exit_tfm(struct crypto_tfm *tfm)
1262 struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm);
1264 crypto_free_aead(ctx->child);
1265 crypto_free_blkcipher(ctx->null);
1268 static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb)
1270 struct crypto_attr_type *algt;
1271 struct crypto_instance *inst;
1272 struct crypto_aead_spawn *spawn;
1273 struct crypto_alg *alg;
1274 struct crypto_rfc4543_instance_ctx *ctx;
1275 const char *ccm_name;
1278 algt = crypto_get_attr_type(tb);
1280 return ERR_CAST(algt);
1282 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
1283 return ERR_PTR(-EINVAL);
1285 ccm_name = crypto_attr_alg_name(tb[1]);
1286 if (IS_ERR(ccm_name))
1287 return ERR_CAST(ccm_name);
1289 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
1291 return ERR_PTR(-ENOMEM);
1293 ctx = crypto_instance_ctx(inst);
1295 crypto_set_aead_spawn(spawn, inst);
1296 err = crypto_grab_aead(spawn, ccm_name, 0,
1297 crypto_requires_sync(algt->type, algt->mask));
1301 alg = crypto_aead_spawn_alg(spawn);
1303 crypto_set_skcipher_spawn(&ctx->null, inst);
1304 err = crypto_grab_skcipher(&ctx->null, "ecb(cipher_null)", 0,
1309 crypto_skcipher_spawn_alg(&ctx->null);
1313 /* We only support 16-byte blocks. */
1314 if (alg->cra_aead.ivsize != 16)
1315 goto out_drop_ecbnull;
1317 /* Not a stream cipher? */
1318 if (alg->cra_blocksize != 1)
1319 goto out_drop_ecbnull;
1321 err = -ENAMETOOLONG;
1322 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
1323 "rfc4543(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
1324 snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1325 "rfc4543(%s)", alg->cra_driver_name) >=
1326 CRYPTO_MAX_ALG_NAME)
1327 goto out_drop_ecbnull;
1329 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
1330 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
1331 inst->alg.cra_priority = alg->cra_priority;
1332 inst->alg.cra_blocksize = 1;
1333 inst->alg.cra_alignmask = alg->cra_alignmask;
1334 inst->alg.cra_type = &crypto_nivaead_type;
1336 inst->alg.cra_aead.ivsize = 8;
1337 inst->alg.cra_aead.maxauthsize = 16;
1339 inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
1341 inst->alg.cra_init = crypto_rfc4543_init_tfm;
1342 inst->alg.cra_exit = crypto_rfc4543_exit_tfm;
1344 inst->alg.cra_aead.setkey = crypto_rfc4543_setkey;
1345 inst->alg.cra_aead.setauthsize = crypto_rfc4543_setauthsize;
1346 inst->alg.cra_aead.encrypt = crypto_rfc4543_encrypt;
1347 inst->alg.cra_aead.decrypt = crypto_rfc4543_decrypt;
1349 inst->alg.cra_aead.geniv = "seqiv";
1355 crypto_drop_skcipher(&ctx->null);
1357 crypto_drop_aead(spawn);
1360 inst = ERR_PTR(err);
1364 static void crypto_rfc4543_free(struct crypto_instance *inst)
1366 struct crypto_rfc4543_instance_ctx *ctx = crypto_instance_ctx(inst);
1368 crypto_drop_aead(&ctx->aead);
1369 crypto_drop_skcipher(&ctx->null);
1374 static struct crypto_template crypto_rfc4543_tmpl = {
1376 .alloc = crypto_rfc4543_alloc,
1377 .free = crypto_rfc4543_free,
1378 .module = THIS_MODULE,
1381 static int __init crypto_gcm_module_init(void)
1385 gcm_zeroes = kzalloc(16, GFP_KERNEL);
1389 err = crypto_register_template(&crypto_gcm_base_tmpl);
1393 err = crypto_register_template(&crypto_gcm_tmpl);
1397 err = crypto_register_template(&crypto_rfc4106_tmpl);
1401 err = crypto_register_template(&crypto_rfc4543_tmpl);
1403 goto out_undo_rfc4106;
1408 crypto_unregister_template(&crypto_rfc4106_tmpl);
1410 crypto_unregister_template(&crypto_gcm_tmpl);
1412 crypto_unregister_template(&crypto_gcm_base_tmpl);
1418 static void __exit crypto_gcm_module_exit(void)
1421 crypto_unregister_template(&crypto_rfc4543_tmpl);
1422 crypto_unregister_template(&crypto_rfc4106_tmpl);
1423 crypto_unregister_template(&crypto_gcm_tmpl);
1424 crypto_unregister_template(&crypto_gcm_base_tmpl);
1427 module_init(crypto_gcm_module_init);
1428 module_exit(crypto_gcm_module_exit);
1430 MODULE_LICENSE("GPL");
1431 MODULE_DESCRIPTION("Galois/Counter Mode");
1432 MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
1433 MODULE_ALIAS("gcm_base");
1434 MODULE_ALIAS("rfc4106");
1435 MODULE_ALIAS("rfc4543");