Linux 3.16.75
[linux/fpc-iii.git] / crypto / authenc.c
blob98e7082e039014649e6f7fee58809be79e9da3be
1 /*
2 * Authenc: Simple AEAD wrapper for IPsec
4 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
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)
9 * any later version.
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 {
34 unsigned int reqoff;
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;
46 char tail[];
49 static void authenc_request_complete(struct aead_request *req, int err)
51 if (err != -EINPROGRESS)
52 aead_request_complete(req, err);
55 int crypto_authenc_extractkeys(struct crypto_authenc_keys *keys, const u8 *key,
56 unsigned int keylen)
58 struct rtattr *rta = (struct rtattr *)key;
59 struct crypto_authenc_key_param *param;
61 if (!RTA_OK(rta, keylen))
62 return -EINVAL;
63 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
64 return -EINVAL;
67 * RTA_OK() didn't align the rtattr's payload when validating that it
68 * fits in the buffer. Yet, the keys should start on the next 4-byte
69 * aligned boundary. To avoid confusion, require that the rtattr
70 * payload be exactly the param struct, which has a 4-byte aligned size.
72 if (RTA_PAYLOAD(rta) != sizeof(*param))
73 return -EINVAL;
74 BUILD_BUG_ON(sizeof(*param) % RTA_ALIGNTO);
76 param = RTA_DATA(rta);
77 keys->enckeylen = be32_to_cpu(param->enckeylen);
79 key += rta->rta_len;
80 keylen -= rta->rta_len;
82 if (keylen < keys->enckeylen)
83 return -EINVAL;
85 keys->authkeylen = keylen - keys->enckeylen;
86 keys->authkey = key;
87 keys->enckey = key + keys->authkeylen;
89 return 0;
91 EXPORT_SYMBOL_GPL(crypto_authenc_extractkeys);
93 static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
94 unsigned int keylen)
96 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
97 struct crypto_ahash *auth = ctx->auth;
98 struct crypto_ablkcipher *enc = ctx->enc;
99 struct crypto_authenc_keys keys;
100 int err = -EINVAL;
102 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
103 goto badkey;
105 crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
106 crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
107 CRYPTO_TFM_REQ_MASK);
108 err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
109 crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
110 CRYPTO_TFM_RES_MASK);
112 if (err)
113 goto out;
115 crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
116 crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
117 CRYPTO_TFM_REQ_MASK);
118 err = crypto_ablkcipher_setkey(enc, keys.enckey, keys.enckeylen);
119 crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
120 CRYPTO_TFM_RES_MASK);
122 out:
123 return err;
125 badkey:
126 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
127 goto out;
130 static void authenc_geniv_ahash_update_done(struct crypto_async_request *areq,
131 int err)
133 struct aead_request *req = areq->data;
134 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
135 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
136 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
137 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
139 if (err)
140 goto out;
142 ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
143 areq_ctx->cryptlen);
144 ahash_request_set_callback(ahreq, aead_request_flags(req) &
145 CRYPTO_TFM_REQ_MAY_SLEEP,
146 areq_ctx->complete, req);
148 err = crypto_ahash_finup(ahreq);
149 if (err)
150 goto out;
152 scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
153 areq_ctx->cryptlen,
154 crypto_aead_authsize(authenc), 1);
156 out:
157 authenc_request_complete(req, err);
160 static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
162 struct aead_request *req = areq->data;
163 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
164 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
165 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
166 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
168 if (err)
169 goto out;
171 scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
172 areq_ctx->cryptlen,
173 crypto_aead_authsize(authenc), 1);
175 out:
176 aead_request_complete(req, err);
179 static void authenc_verify_ahash_update_done(struct crypto_async_request *areq,
180 int err)
182 u8 *ihash;
183 unsigned int authsize;
184 struct ablkcipher_request *abreq;
185 struct aead_request *req = areq->data;
186 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
187 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
188 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
189 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
190 unsigned int cryptlen = req->cryptlen;
192 if (err)
193 goto out;
195 ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
196 areq_ctx->cryptlen);
197 ahash_request_set_callback(ahreq, aead_request_flags(req) &
198 CRYPTO_TFM_REQ_MAY_SLEEP,
199 areq_ctx->complete, req);
201 err = crypto_ahash_finup(ahreq);
202 if (err)
203 goto out;
205 authsize = crypto_aead_authsize(authenc);
206 cryptlen -= authsize;
207 ihash = ahreq->result + authsize;
208 scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
209 authsize, 0);
211 err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
212 if (err)
213 goto out;
215 abreq = aead_request_ctx(req);
216 ablkcipher_request_set_tfm(abreq, ctx->enc);
217 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
218 req->base.complete, req->base.data);
219 ablkcipher_request_set_crypt(abreq, req->src, req->dst,
220 cryptlen, req->iv);
222 err = crypto_ablkcipher_decrypt(abreq);
224 out:
225 authenc_request_complete(req, err);
228 static void authenc_verify_ahash_done(struct crypto_async_request *areq,
229 int err)
231 u8 *ihash;
232 unsigned int authsize;
233 struct ablkcipher_request *abreq;
234 struct aead_request *req = areq->data;
235 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
236 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
237 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
238 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
239 unsigned int cryptlen = req->cryptlen;
241 if (err)
242 goto out;
244 authsize = crypto_aead_authsize(authenc);
245 cryptlen -= authsize;
246 ihash = ahreq->result + authsize;
247 scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
248 authsize, 0);
250 err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
251 if (err)
252 goto out;
254 abreq = aead_request_ctx(req);
255 ablkcipher_request_set_tfm(abreq, ctx->enc);
256 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
257 req->base.complete, req->base.data);
258 ablkcipher_request_set_crypt(abreq, req->src, req->dst,
259 cryptlen, req->iv);
261 err = crypto_ablkcipher_decrypt(abreq);
263 out:
264 authenc_request_complete(req, err);
267 static u8 *crypto_authenc_ahash_fb(struct aead_request *req, unsigned int flags)
269 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
270 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
271 struct crypto_ahash *auth = ctx->auth;
272 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
273 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
274 u8 *hash = areq_ctx->tail;
275 int err;
277 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
278 crypto_ahash_alignmask(auth) + 1);
280 ahash_request_set_tfm(ahreq, auth);
282 err = crypto_ahash_init(ahreq);
283 if (err)
284 return ERR_PTR(err);
286 ahash_request_set_crypt(ahreq, req->assoc, hash, req->assoclen);
287 ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
288 areq_ctx->update_complete, req);
290 err = crypto_ahash_update(ahreq);
291 if (err)
292 return ERR_PTR(err);
294 ahash_request_set_crypt(ahreq, areq_ctx->sg, hash,
295 areq_ctx->cryptlen);
296 ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
297 areq_ctx->complete, req);
299 err = crypto_ahash_finup(ahreq);
300 if (err)
301 return ERR_PTR(err);
303 return hash;
306 static u8 *crypto_authenc_ahash(struct aead_request *req, unsigned int flags)
308 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
309 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
310 struct crypto_ahash *auth = ctx->auth;
311 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
312 struct ahash_request *ahreq = (void *)(areq_ctx->tail + ctx->reqoff);
313 u8 *hash = areq_ctx->tail;
314 int err;
316 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
317 crypto_ahash_alignmask(auth) + 1);
319 ahash_request_set_tfm(ahreq, auth);
320 ahash_request_set_crypt(ahreq, areq_ctx->sg, hash,
321 areq_ctx->cryptlen);
322 ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
323 areq_ctx->complete, req);
325 err = crypto_ahash_digest(ahreq);
326 if (err)
327 return ERR_PTR(err);
329 return hash;
332 static int crypto_authenc_genicv(struct aead_request *req, u8 *iv,
333 unsigned int flags)
335 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
336 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
337 struct scatterlist *dst = req->dst;
338 struct scatterlist *assoc = req->assoc;
339 struct scatterlist *cipher = areq_ctx->cipher;
340 struct scatterlist *asg = areq_ctx->asg;
341 unsigned int ivsize = crypto_aead_ivsize(authenc);
342 unsigned int cryptlen = req->cryptlen;
343 authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
344 struct page *dstp;
345 u8 *vdst;
346 u8 *hash;
348 dstp = sg_page(dst);
349 vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset;
351 if (ivsize) {
352 sg_init_table(cipher, 2);
353 sg_set_buf(cipher, iv, ivsize);
354 scatterwalk_crypto_chain(cipher, dst, vdst == iv + ivsize, 2);
355 dst = cipher;
356 cryptlen += ivsize;
359 if (req->assoclen && sg_is_last(assoc)) {
360 authenc_ahash_fn = crypto_authenc_ahash;
361 sg_init_table(asg, 2);
362 sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
363 scatterwalk_crypto_chain(asg, dst, 0, 2);
364 dst = asg;
365 cryptlen += req->assoclen;
368 areq_ctx->cryptlen = cryptlen;
369 areq_ctx->sg = dst;
371 areq_ctx->complete = authenc_geniv_ahash_done;
372 areq_ctx->update_complete = authenc_geniv_ahash_update_done;
374 hash = authenc_ahash_fn(req, flags);
375 if (IS_ERR(hash))
376 return PTR_ERR(hash);
378 scatterwalk_map_and_copy(hash, dst, cryptlen,
379 crypto_aead_authsize(authenc), 1);
380 return 0;
383 static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
384 int err)
386 struct aead_request *areq = req->data;
388 if (!err) {
389 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
390 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
391 struct authenc_request_ctx *areq_ctx = aead_request_ctx(areq);
392 struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
393 + ctx->reqoff);
394 u8 *iv = (u8 *)abreq - crypto_ablkcipher_ivsize(ctx->enc);
396 err = crypto_authenc_genicv(areq, iv, 0);
399 authenc_request_complete(areq, err);
402 static int crypto_authenc_encrypt(struct aead_request *req)
404 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
405 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
406 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
407 struct crypto_ablkcipher *enc = ctx->enc;
408 struct scatterlist *dst = req->dst;
409 unsigned int cryptlen = req->cryptlen;
410 struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
411 + ctx->reqoff);
412 u8 *iv = (u8 *)abreq - crypto_ablkcipher_ivsize(enc);
413 int err;
415 ablkcipher_request_set_tfm(abreq, enc);
416 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
417 crypto_authenc_encrypt_done, req);
418 ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
420 memcpy(iv, req->iv, crypto_aead_ivsize(authenc));
422 err = crypto_ablkcipher_encrypt(abreq);
423 if (err)
424 return err;
426 return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
429 static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
430 int err)
432 struct aead_request *areq = req->data;
434 if (!err) {
435 struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
437 err = crypto_authenc_genicv(areq, greq->giv, 0);
440 authenc_request_complete(areq, err);
443 static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
445 struct crypto_aead *authenc = aead_givcrypt_reqtfm(req);
446 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
447 struct aead_request *areq = &req->areq;
448 struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
449 u8 *iv = req->giv;
450 int err;
452 skcipher_givcrypt_set_tfm(greq, ctx->enc);
453 skcipher_givcrypt_set_callback(greq, aead_request_flags(areq),
454 crypto_authenc_givencrypt_done, areq);
455 skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen,
456 areq->iv);
457 skcipher_givcrypt_set_giv(greq, iv, req->seq);
459 err = crypto_skcipher_givencrypt(greq);
460 if (err)
461 return err;
463 return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
466 static int crypto_authenc_verify(struct aead_request *req,
467 authenc_ahash_t authenc_ahash_fn)
469 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
470 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
471 u8 *ohash;
472 u8 *ihash;
473 unsigned int authsize;
475 areq_ctx->complete = authenc_verify_ahash_done;
476 areq_ctx->update_complete = authenc_verify_ahash_update_done;
478 ohash = authenc_ahash_fn(req, CRYPTO_TFM_REQ_MAY_SLEEP);
479 if (IS_ERR(ohash))
480 return PTR_ERR(ohash);
482 authsize = crypto_aead_authsize(authenc);
483 ihash = ohash + authsize;
484 scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
485 authsize, 0);
486 return crypto_memneq(ihash, ohash, authsize) ? -EBADMSG : 0;
489 static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
490 unsigned int cryptlen)
492 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
493 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
494 struct scatterlist *src = req->src;
495 struct scatterlist *assoc = req->assoc;
496 struct scatterlist *cipher = areq_ctx->cipher;
497 struct scatterlist *asg = areq_ctx->asg;
498 unsigned int ivsize = crypto_aead_ivsize(authenc);
499 authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
500 struct page *srcp;
501 u8 *vsrc;
503 srcp = sg_page(src);
504 vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
506 if (ivsize) {
507 sg_init_table(cipher, 2);
508 sg_set_buf(cipher, iv, ivsize);
509 scatterwalk_crypto_chain(cipher, src, vsrc == iv + ivsize, 2);
510 src = cipher;
511 cryptlen += ivsize;
514 if (req->assoclen && sg_is_last(assoc)) {
515 authenc_ahash_fn = crypto_authenc_ahash;
516 sg_init_table(asg, 2);
517 sg_set_page(asg, sg_page(assoc), assoc->length, assoc->offset);
518 scatterwalk_crypto_chain(asg, src, 0, 2);
519 src = asg;
520 cryptlen += req->assoclen;
523 areq_ctx->cryptlen = cryptlen;
524 areq_ctx->sg = src;
526 return crypto_authenc_verify(req, authenc_ahash_fn);
529 static int crypto_authenc_decrypt(struct aead_request *req)
531 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
532 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
533 struct ablkcipher_request *abreq = aead_request_ctx(req);
534 unsigned int cryptlen = req->cryptlen;
535 unsigned int authsize = crypto_aead_authsize(authenc);
536 u8 *iv = req->iv;
537 int err;
539 if (cryptlen < authsize)
540 return -EINVAL;
541 cryptlen -= authsize;
543 err = crypto_authenc_iverify(req, iv, cryptlen);
544 if (err)
545 return err;
547 ablkcipher_request_set_tfm(abreq, ctx->enc);
548 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
549 req->base.complete, req->base.data);
550 ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
552 return crypto_ablkcipher_decrypt(abreq);
555 static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
557 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
558 struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
559 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
560 struct crypto_ahash *auth;
561 struct crypto_ablkcipher *enc;
562 int err;
564 auth = crypto_spawn_ahash(&ictx->auth);
565 if (IS_ERR(auth))
566 return PTR_ERR(auth);
568 enc = crypto_spawn_skcipher(&ictx->enc);
569 err = PTR_ERR(enc);
570 if (IS_ERR(enc))
571 goto err_free_ahash;
573 ctx->auth = auth;
574 ctx->enc = enc;
576 ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) +
577 crypto_ahash_alignmask(auth),
578 crypto_ahash_alignmask(auth) + 1) +
579 crypto_ablkcipher_ivsize(enc);
581 tfm->crt_aead.reqsize = sizeof(struct authenc_request_ctx) +
582 ctx->reqoff +
583 max_t(unsigned int,
584 crypto_ahash_reqsize(auth) +
585 sizeof(struct ahash_request),
586 sizeof(struct skcipher_givcrypt_request) +
587 crypto_ablkcipher_reqsize(enc));
589 return 0;
591 err_free_ahash:
592 crypto_free_ahash(auth);
593 return err;
596 static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
598 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
600 crypto_free_ahash(ctx->auth);
601 crypto_free_ablkcipher(ctx->enc);
604 static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
606 struct crypto_attr_type *algt;
607 struct crypto_instance *inst;
608 struct hash_alg_common *auth;
609 struct crypto_alg *auth_base;
610 struct crypto_alg *enc;
611 struct authenc_instance_ctx *ctx;
612 const char *enc_name;
613 int err;
615 algt = crypto_get_attr_type(tb);
616 if (IS_ERR(algt))
617 return ERR_CAST(algt);
619 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
620 return ERR_PTR(-EINVAL);
622 auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
623 CRYPTO_ALG_TYPE_AHASH_MASK);
624 if (IS_ERR(auth))
625 return ERR_CAST(auth);
627 auth_base = &auth->base;
629 enc_name = crypto_attr_alg_name(tb[2]);
630 err = PTR_ERR(enc_name);
631 if (IS_ERR(enc_name))
632 goto out_put_auth;
634 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
635 err = -ENOMEM;
636 if (!inst)
637 goto out_put_auth;
639 ctx = crypto_instance_ctx(inst);
641 err = crypto_init_ahash_spawn(&ctx->auth, auth, inst);
642 if (err)
643 goto err_free_inst;
645 crypto_set_skcipher_spawn(&ctx->enc, inst);
646 err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
647 crypto_requires_sync(algt->type,
648 algt->mask));
649 if (err)
650 goto err_drop_auth;
652 enc = crypto_skcipher_spawn_alg(&ctx->enc);
654 err = -ENAMETOOLONG;
655 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
656 "authenc(%s,%s)", auth_base->cra_name, enc->cra_name) >=
657 CRYPTO_MAX_ALG_NAME)
658 goto err_drop_enc;
660 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
661 "authenc(%s,%s)", auth_base->cra_driver_name,
662 enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
663 goto err_drop_enc;
665 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
666 inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
667 inst->alg.cra_priority = enc->cra_priority *
668 10 + auth_base->cra_priority;
669 inst->alg.cra_blocksize = enc->cra_blocksize;
670 inst->alg.cra_alignmask = auth_base->cra_alignmask | enc->cra_alignmask;
671 inst->alg.cra_type = &crypto_aead_type;
673 inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
674 inst->alg.cra_aead.maxauthsize = auth->digestsize;
676 inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
678 inst->alg.cra_init = crypto_authenc_init_tfm;
679 inst->alg.cra_exit = crypto_authenc_exit_tfm;
681 inst->alg.cra_aead.setkey = crypto_authenc_setkey;
682 inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
683 inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
684 inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt;
686 out:
687 crypto_mod_put(auth_base);
688 return inst;
690 err_drop_enc:
691 crypto_drop_skcipher(&ctx->enc);
692 err_drop_auth:
693 crypto_drop_ahash(&ctx->auth);
694 err_free_inst:
695 kfree(inst);
696 out_put_auth:
697 inst = ERR_PTR(err);
698 goto out;
701 static void crypto_authenc_free(struct crypto_instance *inst)
703 struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
705 crypto_drop_skcipher(&ctx->enc);
706 crypto_drop_ahash(&ctx->auth);
707 kfree(inst);
710 static struct crypto_template crypto_authenc_tmpl = {
711 .name = "authenc",
712 .alloc = crypto_authenc_alloc,
713 .free = crypto_authenc_free,
714 .module = THIS_MODULE,
717 static int __init crypto_authenc_module_init(void)
719 return crypto_register_template(&crypto_authenc_tmpl);
722 static void __exit crypto_authenc_module_exit(void)
724 crypto_unregister_template(&crypto_authenc_tmpl);
727 module_init(crypto_authenc_module_init);
728 module_exit(crypto_authenc_module_exit);
730 MODULE_LICENSE("GPL");
731 MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");
732 MODULE_ALIAS_CRYPTO("authenc");