2 * Authenc: Simple AEAD wrapper for IPsec
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
13 #include <crypto/aead.h>
14 #include <crypto/internal/hash.h>
15 #include <crypto/internal/skcipher.h>
16 #include <crypto/authenc.h>
17 #include <crypto/scatterwalk.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
26 typedef u8 *(*authenc_ahash_t)(struct aead_request *req, unsigned int flags);
28 struct authenc_instance_ctx {
29 struct crypto_ahash_spawn auth;
30 struct crypto_skcipher_spawn enc;
33 struct crypto_authenc_ctx {
35 struct crypto_ahash *auth;
36 struct crypto_ablkcipher *enc;
39 struct authenc_request_ctx {
40 unsigned int cryptlen;
41 struct scatterlist *sg;
42 struct scatterlist asg[2];
43 struct scatterlist cipher[2];
44 crypto_completion_t complete;
45 crypto_completion_t update_complete;
49 static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
52 unsigned int authkeylen;
53 unsigned int enckeylen;
54 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
55 struct crypto_ahash *auth = ctx->auth;
56 struct crypto_ablkcipher *enc = ctx->enc;
57 struct rtattr *rta = (void *)key;
58 struct crypto_authenc_key_param *param;
61 if (!RTA_OK(rta, keylen))
63 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
65 if (RTA_PAYLOAD(rta) < sizeof(*param))
68 param = RTA_DATA(rta);
69 enckeylen = be32_to_cpu(param->enckeylen);
71 key += RTA_ALIGN(rta->rta_len);
72 keylen -= RTA_ALIGN(rta->rta_len);
74 if (keylen < enckeylen)
77 authkeylen = keylen - enckeylen;
79 crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
80 crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
82 err = crypto_ahash_setkey(auth, key, authkeylen);
83 crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
89 crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
90 crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
92 err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
93 crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
100 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
104 static void authenc_chain(struct scatterlist *head, struct scatterlist *sg,
108 head->length += sg->length;
109 sg = scatterwalk_sg_next(sg);
113 scatterwalk_sg_chain(head, 2, sg);
118 static void authenc_geniv_ahash_update_done(struct crypto_async_request *areq,
121 struct aead_request *req = areq->data;
122 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
123 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
124 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
125 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
130 ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
132 ahash_request_set_callback(ahreq, aead_request_flags(req) &
133 CRYPTO_TFM_REQ_MAY_SLEEP,
134 areq_ctx->complete, req);
136 err = crypto_ahash_finup(ahreq);
140 scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
142 crypto_aead_authsize(authenc), 1);
145 aead_request_complete(req, err);
148 static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
150 struct aead_request *req = areq->data;
151 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
152 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
153 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
154 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
159 scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
161 crypto_aead_authsize(authenc), 1);
164 aead_request_complete(req, err);
167 static void authenc_verify_ahash_update_done(struct crypto_async_request *areq,
171 unsigned int authsize;
172 struct ablkcipher_request *abreq;
173 struct aead_request *req = areq->data;
174 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
175 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
176 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
177 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
182 ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
184 ahash_request_set_callback(ahreq, aead_request_flags(req) &
185 CRYPTO_TFM_REQ_MAY_SLEEP,
186 areq_ctx->complete, req);
188 err = crypto_ahash_finup(ahreq);
192 authsize = crypto_aead_authsize(authenc);
193 ihash = ahreq->result + authsize;
194 scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
197 err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
201 abreq = aead_request_ctx(req);
202 ablkcipher_request_set_tfm(abreq, ctx->enc);
203 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
204 req->base.complete, req->base.data);
205 ablkcipher_request_set_crypt(abreq, req->src, req->dst,
206 req->cryptlen, req->iv);
208 err = crypto_ablkcipher_decrypt(abreq);
211 aead_request_complete(req, err);
214 static void authenc_verify_ahash_done(struct crypto_async_request *areq,
218 unsigned int authsize;
219 struct ablkcipher_request *abreq;
220 struct aead_request *req = areq->data;
221 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
222 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
223 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
224 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
229 authsize = crypto_aead_authsize(authenc);
230 ihash = ahreq->result + authsize;
231 scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
234 err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
238 abreq = aead_request_ctx(req);
239 ablkcipher_request_set_tfm(abreq, ctx->enc);
240 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
241 req->base.complete, req->base.data);
242 ablkcipher_request_set_crypt(abreq, req->src, req->dst,
243 req->cryptlen, req->iv);
245 err = crypto_ablkcipher_decrypt(abreq);
248 aead_request_complete(req, err);
251 static u8 *crypto_authenc_ahash_fb(struct aead_request *req, unsigned int flags)
253 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
254 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
255 struct crypto_ahash *auth = ctx->auth;
256 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
257 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
258 u8 *hash = areq_ctx->tail;
261 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
262 crypto_ahash_alignmask(auth) + 1);
264 ahash_request_set_tfm(ahreq, auth);
266 err = crypto_ahash_init(ahreq);
270 ahash_request_set_crypt(ahreq, req->assoc, hash, req->assoclen);
271 ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
272 areq_ctx->update_complete, req);
274 err = crypto_ahash_update(ahreq);
278 ahash_request_set_crypt(ahreq, areq_ctx->sg, hash,
280 ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
281 areq_ctx->complete, req);
283 err = crypto_ahash_finup(ahreq);
290 static u8 *crypto_authenc_ahash(struct aead_request *req, unsigned int flags)
292 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
293 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
294 struct crypto_ahash *auth = ctx->auth;
295 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
296 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
297 u8 *hash = areq_ctx->tail;
300 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
301 crypto_ahash_alignmask(auth) + 1);
303 ahash_request_set_tfm(ahreq, auth);
304 ahash_request_set_crypt(ahreq, areq_ctx->sg, hash,
306 ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
307 areq_ctx->complete, req);
309 err = crypto_ahash_digest(ahreq);
316 static int crypto_authenc_genicv(struct aead_request *req, u8 *iv,
319 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
320 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
321 struct scatterlist *dst = req->dst;
322 struct scatterlist *assoc = req->assoc;
323 struct scatterlist *cipher = areq_ctx->cipher;
324 struct scatterlist *asg = areq_ctx->asg;
325 unsigned int ivsize = crypto_aead_ivsize(authenc);
326 unsigned int cryptlen = req->cryptlen;
327 authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
333 vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset;
336 sg_init_table(cipher, 2);
337 sg_set_buf(cipher, iv, ivsize);
338 authenc_chain(cipher, dst, vdst == iv + ivsize);
343 if (sg_is_last(assoc)) {
344 authenc_ahash_fn = crypto_authenc_ahash;
345 sg_init_table(asg, 2);
346 sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
347 authenc_chain(asg, dst, 0);
349 cryptlen += req->assoclen;
352 areq_ctx->cryptlen = cryptlen;
355 areq_ctx->complete = authenc_geniv_ahash_done;
356 areq_ctx->update_complete = authenc_geniv_ahash_update_done;
358 hash = authenc_ahash_fn(req, flags);
360 return PTR_ERR(hash);
362 scatterwalk_map_and_copy(hash, dst, cryptlen,
363 crypto_aead_authsize(authenc), 1);
367 static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
370 struct aead_request *areq = req->data;
373 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
374 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
375 struct ablkcipher_request *abreq = aead_request_ctx(areq);
376 u8 *iv = (u8 *)(abreq + 1) +
377 crypto_ablkcipher_reqsize(ctx->enc);
379 err = crypto_authenc_genicv(areq, iv, 0);
382 aead_request_complete(areq, err);
385 static int crypto_authenc_encrypt(struct aead_request *req)
387 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
388 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
389 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
390 struct crypto_ablkcipher *enc = ctx->enc;
391 struct scatterlist *dst = req->dst;
392 unsigned int cryptlen = req->cryptlen;
393 struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
395 u8 *iv = (u8 *)abreq - crypto_ablkcipher_ivsize(enc);
398 ablkcipher_request_set_tfm(abreq, enc);
399 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
400 crypto_authenc_encrypt_done, req);
401 ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
403 memcpy(iv, req->iv, crypto_aead_ivsize(authenc));
405 err = crypto_ablkcipher_encrypt(abreq);
409 return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
412 static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
415 struct aead_request *areq = req->data;
418 struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
420 err = crypto_authenc_genicv(areq, greq->giv, 0);
423 aead_request_complete(areq, err);
426 static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
428 struct crypto_aead *authenc = aead_givcrypt_reqtfm(req);
429 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
430 struct aead_request *areq = &req->areq;
431 struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
435 skcipher_givcrypt_set_tfm(greq, ctx->enc);
436 skcipher_givcrypt_set_callback(greq, aead_request_flags(areq),
437 crypto_authenc_givencrypt_done, areq);
438 skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen,
440 skcipher_givcrypt_set_giv(greq, iv, req->seq);
442 err = crypto_skcipher_givencrypt(greq);
446 return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
449 static int crypto_authenc_verify(struct aead_request *req,
450 authenc_ahash_t authenc_ahash_fn)
452 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
453 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
456 unsigned int authsize;
458 areq_ctx->complete = authenc_verify_ahash_done;
459 areq_ctx->update_complete = authenc_verify_ahash_update_done;
461 ohash = authenc_ahash_fn(req, CRYPTO_TFM_REQ_MAY_SLEEP);
463 return PTR_ERR(ohash);
465 authsize = crypto_aead_authsize(authenc);
466 ihash = ohash + authsize;
467 scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
469 return memcmp(ihash, ohash, authsize) ? -EBADMSG : 0;
472 static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
473 unsigned int cryptlen)
475 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
476 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
477 struct scatterlist *src = req->src;
478 struct scatterlist *assoc = req->assoc;
479 struct scatterlist *cipher = areq_ctx->cipher;
480 struct scatterlist *asg = areq_ctx->asg;
481 unsigned int ivsize = crypto_aead_ivsize(authenc);
482 authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
487 vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
490 sg_init_table(cipher, 2);
491 sg_set_buf(cipher, iv, ivsize);
492 authenc_chain(cipher, src, vsrc == iv + ivsize);
497 if (sg_is_last(assoc)) {
498 authenc_ahash_fn = crypto_authenc_ahash;
499 sg_init_table(asg, 2);
500 sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
501 authenc_chain(asg, src, 0);
503 cryptlen += req->assoclen;
506 areq_ctx->cryptlen = cryptlen;
509 return crypto_authenc_verify(req, authenc_ahash_fn);
512 static int crypto_authenc_decrypt(struct aead_request *req)
514 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
515 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
516 struct ablkcipher_request *abreq = aead_request_ctx(req);
517 unsigned int cryptlen = req->cryptlen;
518 unsigned int authsize = crypto_aead_authsize(authenc);
522 if (cryptlen < authsize)
524 cryptlen -= authsize;
526 err = crypto_authenc_iverify(req, iv, cryptlen);
530 ablkcipher_request_set_tfm(abreq, ctx->enc);
531 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
532 req->base.complete, req->base.data);
533 ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
535 return crypto_ablkcipher_decrypt(abreq);
538 static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
540 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
541 struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
542 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
543 struct crypto_ahash *auth;
544 struct crypto_ablkcipher *enc;
547 auth = crypto_spawn_ahash(&ictx->auth);
549 return PTR_ERR(auth);
551 enc = crypto_spawn_skcipher(&ictx->enc);
559 ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) +
560 crypto_ahash_alignmask(auth),
561 crypto_ahash_alignmask(auth) + 1) +
562 crypto_ablkcipher_ivsize(enc);
564 tfm->crt_aead.reqsize = sizeof(struct authenc_request_ctx) +
567 crypto_ahash_reqsize(auth) +
568 sizeof(struct ahash_request),
569 sizeof(struct skcipher_givcrypt_request) +
570 crypto_ablkcipher_reqsize(enc));
575 crypto_free_ahash(auth);
579 static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
581 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
583 crypto_free_ahash(ctx->auth);
584 crypto_free_ablkcipher(ctx->enc);
587 static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
589 struct crypto_attr_type *algt;
590 struct crypto_instance *inst;
591 struct hash_alg_common *auth;
592 struct crypto_alg *auth_base;
593 struct crypto_alg *enc;
594 struct authenc_instance_ctx *ctx;
595 const char *enc_name;
598 algt = crypto_get_attr_type(tb);
603 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
604 return ERR_PTR(-EINVAL);
606 auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
607 CRYPTO_ALG_TYPE_AHASH_MASK);
609 return ERR_PTR(PTR_ERR(auth));
611 auth_base = &auth->base;
613 enc_name = crypto_attr_alg_name(tb[2]);
614 err = PTR_ERR(enc_name);
615 if (IS_ERR(enc_name))
618 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
623 ctx = crypto_instance_ctx(inst);
625 err = crypto_init_ahash_spawn(&ctx->auth, auth, inst);
629 crypto_set_skcipher_spawn(&ctx->enc, inst);
630 err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
631 crypto_requires_sync(algt->type,
636 enc = crypto_skcipher_spawn_alg(&ctx->enc);
639 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
640 "authenc(%s,%s)", auth_base->cra_name, enc->cra_name) >=
644 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
645 "authenc(%s,%s)", auth_base->cra_driver_name,
646 enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
649 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
650 inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
651 inst->alg.cra_priority = enc->cra_priority *
652 10 + auth_base->cra_priority;
653 inst->alg.cra_blocksize = enc->cra_blocksize;
654 inst->alg.cra_alignmask = auth_base->cra_alignmask | enc->cra_alignmask;
655 inst->alg.cra_type = &crypto_aead_type;
657 inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
658 inst->alg.cra_aead.maxauthsize = auth->digestsize;
660 inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
662 inst->alg.cra_init = crypto_authenc_init_tfm;
663 inst->alg.cra_exit = crypto_authenc_exit_tfm;
665 inst->alg.cra_aead.setkey = crypto_authenc_setkey;
666 inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
667 inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
668 inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt;
671 crypto_mod_put(auth_base);
675 crypto_drop_skcipher(&ctx->enc);
677 crypto_drop_ahash(&ctx->auth);
685 static void crypto_authenc_free(struct crypto_instance *inst)
687 struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
689 crypto_drop_skcipher(&ctx->enc);
690 crypto_drop_ahash(&ctx->auth);
694 static struct crypto_template crypto_authenc_tmpl = {
696 .alloc = crypto_authenc_alloc,
697 .free = crypto_authenc_free,
698 .module = THIS_MODULE,
701 static int __init crypto_authenc_module_init(void)
703 return crypto_register_template(&crypto_authenc_tmpl);
706 static void __exit crypto_authenc_module_exit(void)
708 crypto_unregister_template(&crypto_authenc_tmpl);
711 module_init(crypto_authenc_module_init);
712 module_exit(crypto_authenc_module_exit);
714 MODULE_LICENSE("GPL");
715 MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");