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_ctx {
41 struct crypto_aead *child;
45 struct crypto_rfc4543_req_ctx {
48 struct scatterlist cipher[1];
49 struct scatterlist payload[2];
50 struct scatterlist assoc[2];
51 struct aead_request subreq;
54 struct crypto_gcm_ghash_ctx {
55 unsigned int cryptlen;
56 struct scatterlist *src;
57 void (*complete)(struct aead_request *req, int err);
60 struct crypto_gcm_req_priv_ctx {
63 struct scatterlist src[2];
64 struct scatterlist dst[2];
65 struct crypto_gcm_ghash_ctx ghash_ctx;
67 struct ahash_request ahreq;
68 struct ablkcipher_request abreq;
72 struct crypto_gcm_setkey_result {
74 struct completion completion;
77 static void *gcm_zeroes;
79 static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
80 struct aead_request *req)
82 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
84 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
87 static void crypto_gcm_setkey_done(struct crypto_async_request *req, int err)
89 struct crypto_gcm_setkey_result *result = req->data;
91 if (err == -EINPROGRESS)
95 complete(&result->completion);
98 static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
101 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
102 struct crypto_ahash *ghash = ctx->ghash;
103 struct crypto_ablkcipher *ctr = ctx->ctr;
108 struct crypto_gcm_setkey_result result;
110 struct scatterlist sg[1];
111 struct ablkcipher_request req;
115 crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
116 crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
117 CRYPTO_TFM_REQ_MASK);
119 err = crypto_ablkcipher_setkey(ctr, key, keylen);
123 crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
124 CRYPTO_TFM_RES_MASK);
126 data = kzalloc(sizeof(*data) + crypto_ablkcipher_reqsize(ctr),
131 init_completion(&data->result.completion);
132 sg_init_one(data->sg, &data->hash, sizeof(data->hash));
133 ablkcipher_request_set_tfm(&data->req, ctr);
134 ablkcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
135 CRYPTO_TFM_REQ_MAY_BACKLOG,
136 crypto_gcm_setkey_done,
138 ablkcipher_request_set_crypt(&data->req, data->sg, data->sg,
139 sizeof(data->hash), data->iv);
141 err = crypto_ablkcipher_encrypt(&data->req);
142 if (err == -EINPROGRESS || err == -EBUSY) {
143 err = wait_for_completion_interruptible(
144 &data->result.completion);
146 err = data->result.err;
152 crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
153 crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
154 CRYPTO_TFM_REQ_MASK);
155 err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
156 crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) &
157 CRYPTO_TFM_RES_MASK);
164 static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
165 unsigned int authsize)
183 static void crypto_gcm_init_crypt(struct ablkcipher_request *ablk_req,
184 struct aead_request *req,
185 unsigned int cryptlen)
187 struct crypto_aead *aead = crypto_aead_reqtfm(req);
188 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
189 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
190 struct scatterlist *dst;
191 __be32 counter = cpu_to_be32(1);
193 memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
194 memcpy(req->iv + 12, &counter, 4);
196 sg_init_table(pctx->src, 2);
197 sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
198 scatterwalk_sg_chain(pctx->src, 2, req->src);
201 if (req->src != req->dst) {
202 sg_init_table(pctx->dst, 2);
203 sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
204 scatterwalk_sg_chain(pctx->dst, 2, req->dst);
208 ablkcipher_request_set_tfm(ablk_req, ctx->ctr);
209 ablkcipher_request_set_crypt(ablk_req, pctx->src, dst,
210 cryptlen + sizeof(pctx->auth_tag),
214 static inline unsigned int gcm_remain(unsigned int len)
217 return len ? 16 - len : 0;
220 static void gcm_hash_len_done(struct crypto_async_request *areq, int err);
221 static void gcm_hash_final_done(struct crypto_async_request *areq, int err);
223 static int gcm_hash_update(struct aead_request *req,
224 struct crypto_gcm_req_priv_ctx *pctx,
225 crypto_completion_t complete,
226 struct scatterlist *src,
229 struct ahash_request *ahreq = &pctx->u.ahreq;
231 ahash_request_set_callback(ahreq, aead_request_flags(req),
233 ahash_request_set_crypt(ahreq, src, NULL, len);
235 return crypto_ahash_update(ahreq);
238 static int gcm_hash_remain(struct aead_request *req,
239 struct crypto_gcm_req_priv_ctx *pctx,
241 crypto_completion_t complete)
243 struct ahash_request *ahreq = &pctx->u.ahreq;
245 ahash_request_set_callback(ahreq, aead_request_flags(req),
247 sg_init_one(pctx->src, gcm_zeroes, remain);
248 ahash_request_set_crypt(ahreq, pctx->src, NULL, remain);
250 return crypto_ahash_update(ahreq);
253 static int gcm_hash_len(struct aead_request *req,
254 struct crypto_gcm_req_priv_ctx *pctx)
256 struct ahash_request *ahreq = &pctx->u.ahreq;
257 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
260 lengths.a = cpu_to_be64(req->assoclen * 8);
261 lengths.b = cpu_to_be64(gctx->cryptlen * 8);
262 memcpy(pctx->iauth_tag, &lengths, 16);
263 sg_init_one(pctx->src, pctx->iauth_tag, 16);
264 ahash_request_set_callback(ahreq, aead_request_flags(req),
265 gcm_hash_len_done, req);
266 ahash_request_set_crypt(ahreq, pctx->src,
267 NULL, sizeof(lengths));
269 return crypto_ahash_update(ahreq);
272 static int gcm_hash_final(struct aead_request *req,
273 struct crypto_gcm_req_priv_ctx *pctx)
275 struct ahash_request *ahreq = &pctx->u.ahreq;
277 ahash_request_set_callback(ahreq, aead_request_flags(req),
278 gcm_hash_final_done, req);
279 ahash_request_set_crypt(ahreq, NULL, pctx->iauth_tag, 0);
281 return crypto_ahash_final(ahreq);
284 static void __gcm_hash_final_done(struct aead_request *req, int err)
286 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
287 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
290 crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
292 gctx->complete(req, err);
295 static void gcm_hash_final_done(struct crypto_async_request *areq, int err)
297 struct aead_request *req = areq->data;
299 __gcm_hash_final_done(req, err);
302 static void __gcm_hash_len_done(struct aead_request *req, int err)
304 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
307 err = gcm_hash_final(req, pctx);
308 if (err == -EINPROGRESS || err == -EBUSY)
312 __gcm_hash_final_done(req, err);
315 static void gcm_hash_len_done(struct crypto_async_request *areq, int err)
317 struct aead_request *req = areq->data;
319 __gcm_hash_len_done(req, err);
322 static void __gcm_hash_crypt_remain_done(struct aead_request *req, int err)
324 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
327 err = gcm_hash_len(req, pctx);
328 if (err == -EINPROGRESS || err == -EBUSY)
332 __gcm_hash_len_done(req, err);
335 static void gcm_hash_crypt_remain_done(struct crypto_async_request *areq,
338 struct aead_request *req = areq->data;
340 __gcm_hash_crypt_remain_done(req, err);
343 static void __gcm_hash_crypt_done(struct aead_request *req, int err)
345 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
346 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
350 remain = gcm_remain(gctx->cryptlen);
352 err = gcm_hash_remain(req, pctx, remain,
353 gcm_hash_crypt_remain_done);
354 if (err == -EINPROGRESS || err == -EBUSY)
358 __gcm_hash_crypt_remain_done(req, err);
361 static void gcm_hash_crypt_done(struct crypto_async_request *areq, int err)
363 struct aead_request *req = areq->data;
365 __gcm_hash_crypt_done(req, err);
368 static void __gcm_hash_assoc_remain_done(struct aead_request *req, int err)
370 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
371 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
372 crypto_completion_t complete;
373 unsigned int remain = 0;
375 if (!err && gctx->cryptlen) {
376 remain = gcm_remain(gctx->cryptlen);
377 complete = remain ? gcm_hash_crypt_done :
378 gcm_hash_crypt_remain_done;
379 err = gcm_hash_update(req, pctx, complete,
380 gctx->src, gctx->cryptlen);
381 if (err == -EINPROGRESS || err == -EBUSY)
386 __gcm_hash_crypt_done(req, err);
388 __gcm_hash_crypt_remain_done(req, err);
391 static void gcm_hash_assoc_remain_done(struct crypto_async_request *areq,
394 struct aead_request *req = areq->data;
396 __gcm_hash_assoc_remain_done(req, err);
399 static void __gcm_hash_assoc_done(struct aead_request *req, int err)
401 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
405 remain = gcm_remain(req->assoclen);
407 err = gcm_hash_remain(req, pctx, remain,
408 gcm_hash_assoc_remain_done);
409 if (err == -EINPROGRESS || err == -EBUSY)
413 __gcm_hash_assoc_remain_done(req, err);
416 static void gcm_hash_assoc_done(struct crypto_async_request *areq, int err)
418 struct aead_request *req = areq->data;
420 __gcm_hash_assoc_done(req, err);
423 static void __gcm_hash_init_done(struct aead_request *req, int err)
425 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
426 crypto_completion_t complete;
427 unsigned int remain = 0;
429 if (!err && req->assoclen) {
430 remain = gcm_remain(req->assoclen);
431 complete = remain ? gcm_hash_assoc_done :
432 gcm_hash_assoc_remain_done;
433 err = gcm_hash_update(req, pctx, complete,
434 req->assoc, req->assoclen);
435 if (err == -EINPROGRESS || err == -EBUSY)
440 __gcm_hash_assoc_done(req, err);
442 __gcm_hash_assoc_remain_done(req, err);
445 static void gcm_hash_init_done(struct crypto_async_request *areq, int err)
447 struct aead_request *req = areq->data;
449 __gcm_hash_init_done(req, err);
452 static int gcm_hash(struct aead_request *req,
453 struct crypto_gcm_req_priv_ctx *pctx)
455 struct ahash_request *ahreq = &pctx->u.ahreq;
456 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
457 struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
459 crypto_completion_t complete;
462 ahash_request_set_tfm(ahreq, ctx->ghash);
464 ahash_request_set_callback(ahreq, aead_request_flags(req),
465 gcm_hash_init_done, req);
466 err = crypto_ahash_init(ahreq);
469 remain = gcm_remain(req->assoclen);
470 complete = remain ? gcm_hash_assoc_done : gcm_hash_assoc_remain_done;
471 err = gcm_hash_update(req, pctx, complete, req->assoc, req->assoclen);
475 err = gcm_hash_remain(req, pctx, remain,
476 gcm_hash_assoc_remain_done);
480 remain = gcm_remain(gctx->cryptlen);
481 complete = remain ? gcm_hash_crypt_done : gcm_hash_crypt_remain_done;
482 err = gcm_hash_update(req, pctx, complete, gctx->src, gctx->cryptlen);
486 err = gcm_hash_remain(req, pctx, remain,
487 gcm_hash_crypt_remain_done);
491 err = gcm_hash_len(req, pctx);
494 err = gcm_hash_final(req, pctx);
501 static void gcm_enc_copy_hash(struct aead_request *req,
502 struct crypto_gcm_req_priv_ctx *pctx)
504 struct crypto_aead *aead = crypto_aead_reqtfm(req);
505 u8 *auth_tag = pctx->auth_tag;
507 scatterwalk_map_and_copy(auth_tag, req->dst, req->cryptlen,
508 crypto_aead_authsize(aead), 1);
511 static void gcm_enc_hash_done(struct aead_request *req, int err)
513 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
516 gcm_enc_copy_hash(req, pctx);
518 aead_request_complete(req, err);
521 static void gcm_encrypt_done(struct crypto_async_request *areq, int err)
523 struct aead_request *req = areq->data;
524 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
527 err = gcm_hash(req, pctx);
528 if (err == -EINPROGRESS || err == -EBUSY)
531 crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
532 gcm_enc_copy_hash(req, pctx);
536 aead_request_complete(req, err);
539 static int crypto_gcm_encrypt(struct aead_request *req)
541 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
542 struct ablkcipher_request *abreq = &pctx->u.abreq;
543 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
546 crypto_gcm_init_crypt(abreq, req, req->cryptlen);
547 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
548 gcm_encrypt_done, req);
550 gctx->src = req->dst;
551 gctx->cryptlen = req->cryptlen;
552 gctx->complete = gcm_enc_hash_done;
554 err = crypto_ablkcipher_encrypt(abreq);
558 err = gcm_hash(req, pctx);
562 crypto_xor(pctx->auth_tag, pctx->iauth_tag, 16);
563 gcm_enc_copy_hash(req, pctx);
568 static int crypto_gcm_verify(struct aead_request *req,
569 struct crypto_gcm_req_priv_ctx *pctx)
571 struct crypto_aead *aead = crypto_aead_reqtfm(req);
572 u8 *auth_tag = pctx->auth_tag;
573 u8 *iauth_tag = pctx->iauth_tag;
574 unsigned int authsize = crypto_aead_authsize(aead);
575 unsigned int cryptlen = req->cryptlen - authsize;
577 crypto_xor(auth_tag, iauth_tag, 16);
578 scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
579 return memcmp(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
582 static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
584 struct aead_request *req = areq->data;
585 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
588 err = crypto_gcm_verify(req, pctx);
590 aead_request_complete(req, err);
593 static void gcm_dec_hash_done(struct aead_request *req, int err)
595 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
596 struct ablkcipher_request *abreq = &pctx->u.abreq;
597 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
600 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
601 gcm_decrypt_done, req);
602 crypto_gcm_init_crypt(abreq, req, gctx->cryptlen);
603 err = crypto_ablkcipher_decrypt(abreq);
604 if (err == -EINPROGRESS || err == -EBUSY)
607 err = crypto_gcm_verify(req, pctx);
610 aead_request_complete(req, err);
613 static int crypto_gcm_decrypt(struct aead_request *req)
615 struct crypto_aead *aead = crypto_aead_reqtfm(req);
616 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
617 struct ablkcipher_request *abreq = &pctx->u.abreq;
618 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
619 unsigned int authsize = crypto_aead_authsize(aead);
620 unsigned int cryptlen = req->cryptlen;
623 if (cryptlen < authsize)
625 cryptlen -= authsize;
627 gctx->src = req->src;
628 gctx->cryptlen = cryptlen;
629 gctx->complete = gcm_dec_hash_done;
631 err = gcm_hash(req, pctx);
635 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
636 gcm_decrypt_done, req);
637 crypto_gcm_init_crypt(abreq, req, cryptlen);
638 err = crypto_ablkcipher_decrypt(abreq);
642 return crypto_gcm_verify(req, pctx);
645 static int crypto_gcm_init_tfm(struct crypto_tfm *tfm)
647 struct crypto_instance *inst = (void *)tfm->__crt_alg;
648 struct gcm_instance_ctx *ictx = crypto_instance_ctx(inst);
649 struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
650 struct crypto_ablkcipher *ctr;
651 struct crypto_ahash *ghash;
655 ghash = crypto_spawn_ahash(&ictx->ghash);
657 return PTR_ERR(ghash);
659 ctr = crypto_spawn_skcipher(&ictx->ctr);
667 align = crypto_tfm_alg_alignmask(tfm);
668 align &= ~(crypto_tfm_ctx_alignment() - 1);
669 tfm->crt_aead.reqsize = align +
670 offsetof(struct crypto_gcm_req_priv_ctx, u) +
671 max(sizeof(struct ablkcipher_request) +
672 crypto_ablkcipher_reqsize(ctr),
673 sizeof(struct ahash_request) +
674 crypto_ahash_reqsize(ghash));
679 crypto_free_ahash(ghash);
683 static void crypto_gcm_exit_tfm(struct crypto_tfm *tfm)
685 struct crypto_gcm_ctx *ctx = crypto_tfm_ctx(tfm);
687 crypto_free_ahash(ctx->ghash);
688 crypto_free_ablkcipher(ctx->ctr);
691 static struct crypto_instance *crypto_gcm_alloc_common(struct rtattr **tb,
692 const char *full_name,
693 const char *ctr_name,
694 const char *ghash_name)
696 struct crypto_attr_type *algt;
697 struct crypto_instance *inst;
698 struct crypto_alg *ctr;
699 struct crypto_alg *ghash_alg;
700 struct ahash_alg *ghash_ahash_alg;
701 struct gcm_instance_ctx *ctx;
704 algt = crypto_get_attr_type(tb);
706 return ERR_CAST(algt);
708 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
709 return ERR_PTR(-EINVAL);
711 ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type,
712 CRYPTO_ALG_TYPE_HASH,
713 CRYPTO_ALG_TYPE_AHASH_MASK);
714 if (IS_ERR(ghash_alg))
715 return ERR_CAST(ghash_alg);
718 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
722 ctx = crypto_instance_ctx(inst);
723 ghash_ahash_alg = container_of(ghash_alg, struct ahash_alg, halg.base);
724 err = crypto_init_ahash_spawn(&ctx->ghash, &ghash_ahash_alg->halg,
729 crypto_set_skcipher_spawn(&ctx->ctr, inst);
730 err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0,
731 crypto_requires_sync(algt->type,
736 ctr = crypto_skcipher_spawn_alg(&ctx->ctr);
738 /* We only support 16-byte blocks. */
739 if (ctr->cra_ablkcipher.ivsize != 16)
742 /* Not a stream cipher? */
744 if (ctr->cra_blocksize != 1)
748 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
749 "gcm_base(%s,%s)", ctr->cra_driver_name,
750 ghash_alg->cra_driver_name) >=
754 memcpy(inst->alg.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
756 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
757 inst->alg.cra_flags |= ctr->cra_flags & CRYPTO_ALG_ASYNC;
758 inst->alg.cra_priority = ctr->cra_priority;
759 inst->alg.cra_blocksize = 1;
760 inst->alg.cra_alignmask = ctr->cra_alignmask | (__alignof__(u64) - 1);
761 inst->alg.cra_type = &crypto_aead_type;
762 inst->alg.cra_aead.ivsize = 16;
763 inst->alg.cra_aead.maxauthsize = 16;
764 inst->alg.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
765 inst->alg.cra_init = crypto_gcm_init_tfm;
766 inst->alg.cra_exit = crypto_gcm_exit_tfm;
767 inst->alg.cra_aead.setkey = crypto_gcm_setkey;
768 inst->alg.cra_aead.setauthsize = crypto_gcm_setauthsize;
769 inst->alg.cra_aead.encrypt = crypto_gcm_encrypt;
770 inst->alg.cra_aead.decrypt = crypto_gcm_decrypt;
773 crypto_mod_put(ghash_alg);
777 crypto_drop_skcipher(&ctx->ctr);
779 crypto_drop_ahash(&ctx->ghash);
787 static struct crypto_instance *crypto_gcm_alloc(struct rtattr **tb)
789 const char *cipher_name;
790 char ctr_name[CRYPTO_MAX_ALG_NAME];
791 char full_name[CRYPTO_MAX_ALG_NAME];
793 cipher_name = crypto_attr_alg_name(tb[1]);
794 if (IS_ERR(cipher_name))
795 return ERR_CAST(cipher_name);
797 if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
799 return ERR_PTR(-ENAMETOOLONG);
801 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm(%s)", cipher_name) >=
803 return ERR_PTR(-ENAMETOOLONG);
805 return crypto_gcm_alloc_common(tb, full_name, ctr_name, "ghash");
808 static void crypto_gcm_free(struct crypto_instance *inst)
810 struct gcm_instance_ctx *ctx = crypto_instance_ctx(inst);
812 crypto_drop_skcipher(&ctx->ctr);
813 crypto_drop_ahash(&ctx->ghash);
817 static struct crypto_template crypto_gcm_tmpl = {
819 .alloc = crypto_gcm_alloc,
820 .free = crypto_gcm_free,
821 .module = THIS_MODULE,
824 static struct crypto_instance *crypto_gcm_base_alloc(struct rtattr **tb)
826 const char *ctr_name;
827 const char *ghash_name;
828 char full_name[CRYPTO_MAX_ALG_NAME];
830 ctr_name = crypto_attr_alg_name(tb[1]);
831 if (IS_ERR(ctr_name))
832 return ERR_CAST(ctr_name);
834 ghash_name = crypto_attr_alg_name(tb[2]);
835 if (IS_ERR(ghash_name))
836 return ERR_CAST(ghash_name);
838 if (snprintf(full_name, CRYPTO_MAX_ALG_NAME, "gcm_base(%s,%s)",
839 ctr_name, ghash_name) >= CRYPTO_MAX_ALG_NAME)
840 return ERR_PTR(-ENAMETOOLONG);
842 return crypto_gcm_alloc_common(tb, full_name, ctr_name, ghash_name);
845 static struct crypto_template crypto_gcm_base_tmpl = {
847 .alloc = crypto_gcm_base_alloc,
848 .free = crypto_gcm_free,
849 .module = THIS_MODULE,
852 static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
855 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
856 struct crypto_aead *child = ctx->child;
863 memcpy(ctx->nonce, key + keylen, 4);
865 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
866 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
867 CRYPTO_TFM_REQ_MASK);
868 err = crypto_aead_setkey(child, key, keylen);
869 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
870 CRYPTO_TFM_RES_MASK);
875 static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
876 unsigned int authsize)
878 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
889 return crypto_aead_setauthsize(ctx->child, authsize);
892 static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
894 struct aead_request *subreq = aead_request_ctx(req);
895 struct crypto_aead *aead = crypto_aead_reqtfm(req);
896 struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
897 struct crypto_aead *child = ctx->child;
898 u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
899 crypto_aead_alignmask(child) + 1);
901 memcpy(iv, ctx->nonce, 4);
902 memcpy(iv + 4, req->iv, 8);
904 aead_request_set_tfm(subreq, child);
905 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
907 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, iv);
908 aead_request_set_assoc(subreq, req->assoc, req->assoclen);
913 static int crypto_rfc4106_encrypt(struct aead_request *req)
915 req = crypto_rfc4106_crypt(req);
917 return crypto_aead_encrypt(req);
920 static int crypto_rfc4106_decrypt(struct aead_request *req)
922 req = crypto_rfc4106_crypt(req);
924 return crypto_aead_decrypt(req);
927 static int crypto_rfc4106_init_tfm(struct crypto_tfm *tfm)
929 struct crypto_instance *inst = (void *)tfm->__crt_alg;
930 struct crypto_aead_spawn *spawn = crypto_instance_ctx(inst);
931 struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm);
932 struct crypto_aead *aead;
935 aead = crypto_spawn_aead(spawn);
937 return PTR_ERR(aead);
941 align = crypto_aead_alignmask(aead);
942 align &= ~(crypto_tfm_ctx_alignment() - 1);
943 tfm->crt_aead.reqsize = sizeof(struct aead_request) +
944 ALIGN(crypto_aead_reqsize(aead),
945 crypto_tfm_ctx_alignment()) +
951 static void crypto_rfc4106_exit_tfm(struct crypto_tfm *tfm)
953 struct crypto_rfc4106_ctx *ctx = crypto_tfm_ctx(tfm);
955 crypto_free_aead(ctx->child);
958 static struct crypto_instance *crypto_rfc4106_alloc(struct rtattr **tb)
960 struct crypto_attr_type *algt;
961 struct crypto_instance *inst;
962 struct crypto_aead_spawn *spawn;
963 struct crypto_alg *alg;
964 const char *ccm_name;
967 algt = crypto_get_attr_type(tb);
969 return ERR_CAST(algt);
971 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
972 return ERR_PTR(-EINVAL);
974 ccm_name = crypto_attr_alg_name(tb[1]);
975 if (IS_ERR(ccm_name))
976 return ERR_CAST(ccm_name);
978 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
980 return ERR_PTR(-ENOMEM);
982 spawn = crypto_instance_ctx(inst);
983 crypto_set_aead_spawn(spawn, inst);
984 err = crypto_grab_aead(spawn, ccm_name, 0,
985 crypto_requires_sync(algt->type, algt->mask));
989 alg = crypto_aead_spawn_alg(spawn);
993 /* We only support 16-byte blocks. */
994 if (alg->cra_aead.ivsize != 16)
997 /* Not a stream cipher? */
998 if (alg->cra_blocksize != 1)
1001 err = -ENAMETOOLONG;
1002 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
1003 "rfc4106(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
1004 snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1005 "rfc4106(%s)", alg->cra_driver_name) >=
1006 CRYPTO_MAX_ALG_NAME)
1009 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
1010 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
1011 inst->alg.cra_priority = alg->cra_priority;
1012 inst->alg.cra_blocksize = 1;
1013 inst->alg.cra_alignmask = alg->cra_alignmask;
1014 inst->alg.cra_type = &crypto_nivaead_type;
1016 inst->alg.cra_aead.ivsize = 8;
1017 inst->alg.cra_aead.maxauthsize = 16;
1019 inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
1021 inst->alg.cra_init = crypto_rfc4106_init_tfm;
1022 inst->alg.cra_exit = crypto_rfc4106_exit_tfm;
1024 inst->alg.cra_aead.setkey = crypto_rfc4106_setkey;
1025 inst->alg.cra_aead.setauthsize = crypto_rfc4106_setauthsize;
1026 inst->alg.cra_aead.encrypt = crypto_rfc4106_encrypt;
1027 inst->alg.cra_aead.decrypt = crypto_rfc4106_decrypt;
1029 inst->alg.cra_aead.geniv = "seqiv";
1035 crypto_drop_aead(spawn);
1038 inst = ERR_PTR(err);
1042 static void crypto_rfc4106_free(struct crypto_instance *inst)
1044 crypto_drop_spawn(crypto_instance_ctx(inst));
1048 static struct crypto_template crypto_rfc4106_tmpl = {
1050 .alloc = crypto_rfc4106_alloc,
1051 .free = crypto_rfc4106_free,
1052 .module = THIS_MODULE,
1055 static inline struct crypto_rfc4543_req_ctx *crypto_rfc4543_reqctx(
1056 struct aead_request *req)
1058 unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
1060 return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
1063 static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
1064 unsigned int keylen)
1066 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1067 struct crypto_aead *child = ctx->child;
1074 memcpy(ctx->nonce, key + keylen, 4);
1076 crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
1077 crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
1078 CRYPTO_TFM_REQ_MASK);
1079 err = crypto_aead_setkey(child, key, keylen);
1080 crypto_aead_set_flags(parent, crypto_aead_get_flags(child) &
1081 CRYPTO_TFM_RES_MASK);
1086 static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
1087 unsigned int authsize)
1089 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
1094 return crypto_aead_setauthsize(ctx->child, authsize);
1097 static struct aead_request *crypto_rfc4543_crypt(struct aead_request *req,
1100 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1101 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
1102 struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
1103 struct aead_request *subreq = &rctx->subreq;
1104 struct scatterlist *dst = req->dst;
1105 struct scatterlist *cipher = rctx->cipher;
1106 struct scatterlist *payload = rctx->payload;
1107 struct scatterlist *assoc = rctx->assoc;
1108 unsigned int authsize = crypto_aead_authsize(aead);
1109 unsigned int assoclen = req->assoclen;
1112 u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
1113 crypto_aead_alignmask(ctx->child) + 1);
1115 memcpy(iv, ctx->nonce, 4);
1116 memcpy(iv + 4, req->iv, 8);
1118 /* construct cipher/plaintext */
1120 memset(rctx->auth_tag, 0, authsize);
1122 scatterwalk_map_and_copy(rctx->auth_tag, dst,
1123 req->cryptlen - authsize,
1126 sg_init_one(cipher, rctx->auth_tag, authsize);
1128 /* construct the aad */
1129 dstp = sg_page(dst);
1130 vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset;
1132 sg_init_table(payload, 2);
1133 sg_set_buf(payload, req->iv, 8);
1134 scatterwalk_crypto_chain(payload, dst, vdst == req->iv + 8, 2);
1135 assoclen += 8 + req->cryptlen - (enc ? 0 : authsize);
1137 if (req->assoc->length == req->assoclen) {
1138 sg_init_table(assoc, 2);
1139 sg_set_page(assoc, sg_page(req->assoc), req->assoc->length,
1140 req->assoc->offset);
1142 BUG_ON(req->assoclen > sizeof(rctx->assocbuf));
1144 scatterwalk_map_and_copy(rctx->assocbuf, req->assoc, 0,
1147 sg_init_table(assoc, 2);
1148 sg_set_buf(assoc, rctx->assocbuf, req->assoclen);
1150 scatterwalk_crypto_chain(assoc, payload, 0, 2);
1152 aead_request_set_tfm(subreq, ctx->child);
1153 aead_request_set_callback(subreq, req->base.flags, req->base.complete,
1155 aead_request_set_crypt(subreq, cipher, cipher, enc ? 0 : authsize, iv);
1156 aead_request_set_assoc(subreq, assoc, assoclen);
1161 static int crypto_rfc4543_encrypt(struct aead_request *req)
1163 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1164 struct crypto_rfc4543_req_ctx *rctx = crypto_rfc4543_reqctx(req);
1165 struct aead_request *subreq;
1168 subreq = crypto_rfc4543_crypt(req, 1);
1169 err = crypto_aead_encrypt(subreq);
1173 scatterwalk_map_and_copy(rctx->auth_tag, req->dst, req->cryptlen,
1174 crypto_aead_authsize(aead), 1);
1179 static int crypto_rfc4543_decrypt(struct aead_request *req)
1181 req = crypto_rfc4543_crypt(req, 0);
1183 return crypto_aead_decrypt(req);
1186 static int crypto_rfc4543_init_tfm(struct crypto_tfm *tfm)
1188 struct crypto_instance *inst = (void *)tfm->__crt_alg;
1189 struct crypto_aead_spawn *spawn = crypto_instance_ctx(inst);
1190 struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm);
1191 struct crypto_aead *aead;
1192 unsigned long align;
1194 aead = crypto_spawn_aead(spawn);
1196 return PTR_ERR(aead);
1200 align = crypto_aead_alignmask(aead);
1201 align &= ~(crypto_tfm_ctx_alignment() - 1);
1202 tfm->crt_aead.reqsize = sizeof(struct crypto_rfc4543_req_ctx) +
1203 ALIGN(crypto_aead_reqsize(aead),
1204 crypto_tfm_ctx_alignment()) +
1210 static void crypto_rfc4543_exit_tfm(struct crypto_tfm *tfm)
1212 struct crypto_rfc4543_ctx *ctx = crypto_tfm_ctx(tfm);
1214 crypto_free_aead(ctx->child);
1217 static struct crypto_instance *crypto_rfc4543_alloc(struct rtattr **tb)
1219 struct crypto_attr_type *algt;
1220 struct crypto_instance *inst;
1221 struct crypto_aead_spawn *spawn;
1222 struct crypto_alg *alg;
1223 const char *ccm_name;
1226 algt = crypto_get_attr_type(tb);
1228 return ERR_CAST(algt);
1230 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
1231 return ERR_PTR(-EINVAL);
1233 ccm_name = crypto_attr_alg_name(tb[1]);
1234 if (IS_ERR(ccm_name))
1235 return ERR_CAST(ccm_name);
1237 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
1239 return ERR_PTR(-ENOMEM);
1241 spawn = crypto_instance_ctx(inst);
1242 crypto_set_aead_spawn(spawn, inst);
1243 err = crypto_grab_aead(spawn, ccm_name, 0,
1244 crypto_requires_sync(algt->type, algt->mask));
1248 alg = crypto_aead_spawn_alg(spawn);
1252 /* We only support 16-byte blocks. */
1253 if (alg->cra_aead.ivsize != 16)
1256 /* Not a stream cipher? */
1257 if (alg->cra_blocksize != 1)
1260 err = -ENAMETOOLONG;
1261 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
1262 "rfc4543(%s)", alg->cra_name) >= CRYPTO_MAX_ALG_NAME ||
1263 snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1264 "rfc4543(%s)", alg->cra_driver_name) >=
1265 CRYPTO_MAX_ALG_NAME)
1268 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
1269 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
1270 inst->alg.cra_priority = alg->cra_priority;
1271 inst->alg.cra_blocksize = 1;
1272 inst->alg.cra_alignmask = alg->cra_alignmask;
1273 inst->alg.cra_type = &crypto_nivaead_type;
1275 inst->alg.cra_aead.ivsize = 8;
1276 inst->alg.cra_aead.maxauthsize = 16;
1278 inst->alg.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
1280 inst->alg.cra_init = crypto_rfc4543_init_tfm;
1281 inst->alg.cra_exit = crypto_rfc4543_exit_tfm;
1283 inst->alg.cra_aead.setkey = crypto_rfc4543_setkey;
1284 inst->alg.cra_aead.setauthsize = crypto_rfc4543_setauthsize;
1285 inst->alg.cra_aead.encrypt = crypto_rfc4543_encrypt;
1286 inst->alg.cra_aead.decrypt = crypto_rfc4543_decrypt;
1288 inst->alg.cra_aead.geniv = "seqiv";
1294 crypto_drop_aead(spawn);
1297 inst = ERR_PTR(err);
1301 static void crypto_rfc4543_free(struct crypto_instance *inst)
1303 crypto_drop_spawn(crypto_instance_ctx(inst));
1307 static struct crypto_template crypto_rfc4543_tmpl = {
1309 .alloc = crypto_rfc4543_alloc,
1310 .free = crypto_rfc4543_free,
1311 .module = THIS_MODULE,
1314 static int __init crypto_gcm_module_init(void)
1318 gcm_zeroes = kzalloc(16, GFP_KERNEL);
1322 err = crypto_register_template(&crypto_gcm_base_tmpl);
1326 err = crypto_register_template(&crypto_gcm_tmpl);
1330 err = crypto_register_template(&crypto_rfc4106_tmpl);
1334 err = crypto_register_template(&crypto_rfc4543_tmpl);
1336 goto out_undo_rfc4106;
1341 crypto_unregister_template(&crypto_rfc4106_tmpl);
1343 crypto_unregister_template(&crypto_gcm_tmpl);
1345 crypto_unregister_template(&crypto_gcm_base_tmpl);
1351 static void __exit crypto_gcm_module_exit(void)
1354 crypto_unregister_template(&crypto_rfc4543_tmpl);
1355 crypto_unregister_template(&crypto_rfc4106_tmpl);
1356 crypto_unregister_template(&crypto_gcm_tmpl);
1357 crypto_unregister_template(&crypto_gcm_base_tmpl);
1360 module_init(crypto_gcm_module_init);
1361 module_exit(crypto_gcm_module_exit);
1363 MODULE_LICENSE("GPL");
1364 MODULE_DESCRIPTION("Galois/Counter Mode");
1365 MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
1366 MODULE_ALIAS("gcm_base");
1367 MODULE_ALIAS("rfc4106");
1368 MODULE_ALIAS("rfc4543");