blob: 4b226768752abed604033f708d093f0b04aee41b [file] [log] [blame]
Herbert Xu3c09f172007-08-30 16:24:15 +08001/*
2 * Authenc: Simple AEAD wrapper for IPsec
3 *
4 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
5 *
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.
10 *
11 */
12
Herbert Xue56dd562007-12-10 16:20:24 +080013#include <crypto/aead.h>
Herbert Xu9ffde352007-12-17 20:12:49 +080014#include <crypto/internal/skcipher.h>
Herbert Xue236d4a2007-11-22 23:11:53 +080015#include <crypto/authenc.h>
Herbert Xu42c271c2007-12-07 18:52:49 +080016#include <crypto/scatterwalk.h>
Herbert Xu3c09f172007-08-30 16:24:15 +080017#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
Herbert Xue236d4a2007-11-22 23:11:53 +080021#include <linux/rtnetlink.h>
Herbert Xu3c09f172007-08-30 16:24:15 +080022#include <linux/slab.h>
23#include <linux/spinlock.h>
24
Herbert Xu3c09f172007-08-30 16:24:15 +080025struct authenc_instance_ctx {
26 struct crypto_spawn auth;
Herbert Xu9ffde352007-12-17 20:12:49 +080027 struct crypto_skcipher_spawn enc;
Herbert Xu3c09f172007-08-30 16:24:15 +080028};
29
30struct crypto_authenc_ctx {
31 spinlock_t auth_lock;
32 struct crypto_hash *auth;
33 struct crypto_ablkcipher *enc;
34};
35
36static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
37 unsigned int keylen)
38{
Herbert Xu3c09f172007-08-30 16:24:15 +080039 unsigned int authkeylen;
Herbert Xue236d4a2007-11-22 23:11:53 +080040 unsigned int enckeylen;
Herbert Xu3c09f172007-08-30 16:24:15 +080041 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
42 struct crypto_hash *auth = ctx->auth;
43 struct crypto_ablkcipher *enc = ctx->enc;
Herbert Xue236d4a2007-11-22 23:11:53 +080044 struct rtattr *rta = (void *)key;
45 struct crypto_authenc_key_param *param;
Herbert Xu3c09f172007-08-30 16:24:15 +080046 int err = -EINVAL;
47
Herbert Xu12dc5e62007-12-10 10:55:21 +080048 if (!RTA_OK(rta, keylen))
Herbert Xue236d4a2007-11-22 23:11:53 +080049 goto badkey;
50 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
51 goto badkey;
52 if (RTA_PAYLOAD(rta) < sizeof(*param))
53 goto badkey;
54
55 param = RTA_DATA(rta);
56 enckeylen = be32_to_cpu(param->enckeylen);
57
58 key += RTA_ALIGN(rta->rta_len);
59 keylen -= RTA_ALIGN(rta->rta_len);
60
61 if (keylen < enckeylen)
62 goto badkey;
63
Herbert Xu3c09f172007-08-30 16:24:15 +080064 authkeylen = keylen - enckeylen;
65
66 crypto_hash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
67 crypto_hash_set_flags(auth, crypto_aead_get_flags(authenc) &
68 CRYPTO_TFM_REQ_MASK);
69 err = crypto_hash_setkey(auth, key, authkeylen);
70 crypto_aead_set_flags(authenc, crypto_hash_get_flags(auth) &
71 CRYPTO_TFM_RES_MASK);
72
73 if (err)
74 goto out;
75
76 crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
77 crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
78 CRYPTO_TFM_REQ_MASK);
79 err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
80 crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
81 CRYPTO_TFM_RES_MASK);
82
83out:
84 return err;
Herbert Xue236d4a2007-11-22 23:11:53 +080085
86badkey:
87 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
88 goto out;
Herbert Xu3c09f172007-08-30 16:24:15 +080089}
90
Herbert Xue56dd562007-12-10 16:20:24 +080091static void authenc_chain(struct scatterlist *head, struct scatterlist *sg,
92 int chain)
93{
94 if (chain) {
95 head->length += sg->length;
96 sg = scatterwalk_sg_next(sg);
97 }
98
99 if (sg)
100 scatterwalk_sg_chain(head, 2, sg);
101 else
102 sg_mark_end(head);
103}
104
Herbert Xu7c3d7032007-12-10 16:15:41 +0800105static u8 *crypto_authenc_hash(struct aead_request *req, unsigned int flags,
106 struct scatterlist *cipher,
107 unsigned int cryptlen)
Herbert Xu3c09f172007-08-30 16:24:15 +0800108{
109 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu3c09f172007-08-30 16:24:15 +0800110 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
111 struct crypto_hash *auth = ctx->auth;
112 struct hash_desc desc = {
113 .tfm = auth,
Herbert Xu7c3d7032007-12-10 16:15:41 +0800114 .flags = aead_request_flags(req) & flags,
Herbert Xu3c09f172007-08-30 16:24:15 +0800115 };
116 u8 *hash = aead_request_ctx(req);
Herbert Xu3c09f172007-08-30 16:24:15 +0800117 int err;
118
119 hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth),
120 crypto_hash_alignmask(auth) + 1);
121
122 spin_lock_bh(&ctx->auth_lock);
123 err = crypto_hash_init(&desc);
124 if (err)
125 goto auth_unlock;
126
127 err = crypto_hash_update(&desc, req->assoc, req->assoclen);
128 if (err)
129 goto auth_unlock;
130
Herbert Xu7c3d7032007-12-10 16:15:41 +0800131 err = crypto_hash_update(&desc, cipher, cryptlen);
Herbert Xu3c09f172007-08-30 16:24:15 +0800132 if (err)
133 goto auth_unlock;
134
135 err = crypto_hash_final(&desc, hash);
136auth_unlock:
137 spin_unlock_bh(&ctx->auth_lock);
138
139 if (err)
Herbert Xu7c3d7032007-12-10 16:15:41 +0800140 return ERR_PTR(err);
141
142 return hash;
143}
144
Herbert Xue56dd562007-12-10 16:20:24 +0800145static int crypto_authenc_genicv(struct aead_request *req, u8 *iv,
146 unsigned int flags)
Herbert Xu7c3d7032007-12-10 16:15:41 +0800147{
148 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
149 struct scatterlist *dst = req->dst;
Herbert Xue56dd562007-12-10 16:20:24 +0800150 struct scatterlist cipher[2];
151 struct page *dstp;
152 unsigned int ivsize = crypto_aead_ivsize(authenc);
153 unsigned int cryptlen;
154 u8 *vdst;
Herbert Xu7c3d7032007-12-10 16:15:41 +0800155 u8 *hash;
156
Herbert Xue56dd562007-12-10 16:20:24 +0800157 dstp = sg_page(dst);
158 vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset;
159
160 sg_init_table(cipher, 2);
161 sg_set_buf(cipher, iv, ivsize);
162 authenc_chain(cipher, dst, vdst == iv + ivsize);
163
164 cryptlen = req->cryptlen + ivsize;
165 hash = crypto_authenc_hash(req, flags, cipher, cryptlen);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800166 if (IS_ERR(hash))
167 return PTR_ERR(hash);
Herbert Xu3c09f172007-08-30 16:24:15 +0800168
Herbert Xue56dd562007-12-10 16:20:24 +0800169 scatterwalk_map_and_copy(hash, cipher, cryptlen,
Herbert Xu7ba683a2007-12-02 18:49:21 +1100170 crypto_aead_authsize(authenc), 1);
Herbert Xu3c09f172007-08-30 16:24:15 +0800171 return 0;
172}
173
174static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
175 int err)
176{
Herbert Xue56dd562007-12-10 16:20:24 +0800177 if (!err) {
178 struct aead_request *areq = req->data;
179 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
180 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
181 struct ablkcipher_request *abreq = aead_request_ctx(areq);
182 u8 *iv = (u8 *)(abreq + 1) +
183 crypto_ablkcipher_reqsize(ctx->enc);
184
185 err = crypto_authenc_genicv(areq, iv, 0);
186 }
Herbert Xu3c09f172007-08-30 16:24:15 +0800187
188 aead_request_complete(req->data, err);
189}
190
191static int crypto_authenc_encrypt(struct aead_request *req)
192{
193 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
194 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
195 struct ablkcipher_request *abreq = aead_request_ctx(req);
Herbert Xue56dd562007-12-10 16:20:24 +0800196 struct crypto_ablkcipher *enc = ctx->enc;
197 struct scatterlist *dst = req->dst;
198 unsigned int cryptlen = req->cryptlen;
199 u8 *iv = (u8 *)(abreq + 1) + crypto_ablkcipher_reqsize(enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800200 int err;
201
Herbert Xue56dd562007-12-10 16:20:24 +0800202 ablkcipher_request_set_tfm(abreq, enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800203 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
204 crypto_authenc_encrypt_done, req);
Herbert Xue56dd562007-12-10 16:20:24 +0800205 ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
206
207 memcpy(iv, req->iv, crypto_aead_ivsize(authenc));
Herbert Xu3c09f172007-08-30 16:24:15 +0800208
209 err = crypto_ablkcipher_encrypt(abreq);
210 if (err)
211 return err;
212
Herbert Xue56dd562007-12-10 16:20:24 +0800213 return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
214}
215
216static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
217 int err)
218{
219 if (!err) {
Patrick McHardy16161322008-04-29 21:44:28 +0800220 struct aead_request *areq = req->data;
221 struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
Herbert Xue56dd562007-12-10 16:20:24 +0800222
Patrick McHardy16161322008-04-29 21:44:28 +0800223 err = crypto_authenc_genicv(areq, greq->giv, 0);
Herbert Xue56dd562007-12-10 16:20:24 +0800224 }
225
226 aead_request_complete(req->data, err);
227}
228
229static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
230{
231 struct crypto_aead *authenc = aead_givcrypt_reqtfm(req);
232 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
233 struct aead_request *areq = &req->areq;
234 struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
235 u8 *iv = req->giv;
236 int err;
237
238 skcipher_givcrypt_set_tfm(greq, ctx->enc);
239 skcipher_givcrypt_set_callback(greq, aead_request_flags(areq),
240 crypto_authenc_givencrypt_done, areq);
241 skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen,
242 areq->iv);
243 skcipher_givcrypt_set_giv(greq, iv, req->seq);
244
245 err = crypto_skcipher_givencrypt(greq);
246 if (err)
247 return err;
248
249 return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
Herbert Xu3c09f172007-08-30 16:24:15 +0800250}
251
Herbert Xu481f34a2007-12-04 20:04:21 +1100252static int crypto_authenc_verify(struct aead_request *req,
Herbert Xue56dd562007-12-10 16:20:24 +0800253 struct scatterlist *cipher,
Herbert Xu481f34a2007-12-04 20:04:21 +1100254 unsigned int cryptlen)
Herbert Xu3c09f172007-08-30 16:24:15 +0800255{
256 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800257 u8 *ohash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800258 u8 *ihash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800259 unsigned int authsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800260
Herbert Xue56dd562007-12-10 16:20:24 +0800261 ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, cipher,
Herbert Xu7c3d7032007-12-10 16:15:41 +0800262 cryptlen);
263 if (IS_ERR(ohash))
264 return PTR_ERR(ohash);
Herbert Xu3c09f172007-08-30 16:24:15 +0800265
Herbert Xu7ba683a2007-12-02 18:49:21 +1100266 authsize = crypto_aead_authsize(authenc);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800267 ihash = ohash + authsize;
Herbert Xue56dd562007-12-10 16:20:24 +0800268 scatterwalk_map_and_copy(ihash, cipher, cryptlen, authsize, 0);
Herbert Xufe70f5d2007-12-04 20:07:27 +1100269 return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
Herbert Xu3c09f172007-08-30 16:24:15 +0800270}
271
Herbert Xue56dd562007-12-10 16:20:24 +0800272static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
273 unsigned int cryptlen)
Herbert Xu3c09f172007-08-30 16:24:15 +0800274{
Herbert Xue56dd562007-12-10 16:20:24 +0800275 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
276 struct scatterlist *src = req->src;
277 struct scatterlist cipher[2];
278 struct page *srcp;
279 unsigned int ivsize = crypto_aead_ivsize(authenc);
280 u8 *vsrc;
281
282 srcp = sg_page(src);
283 vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
284
285 sg_init_table(cipher, 2);
286 sg_set_buf(cipher, iv, ivsize);
287 authenc_chain(cipher, src, vsrc == iv + ivsize);
288
289 return crypto_authenc_verify(req, cipher, cryptlen + ivsize);
Herbert Xu3c09f172007-08-30 16:24:15 +0800290}
291
292static int crypto_authenc_decrypt(struct aead_request *req)
293{
294 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
295 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
296 struct ablkcipher_request *abreq = aead_request_ctx(req);
Herbert Xu481f34a2007-12-04 20:04:21 +1100297 unsigned int cryptlen = req->cryptlen;
298 unsigned int authsize = crypto_aead_authsize(authenc);
Herbert Xue56dd562007-12-10 16:20:24 +0800299 u8 *iv = req->iv;
Herbert Xu3c09f172007-08-30 16:24:15 +0800300 int err;
301
Herbert Xu481f34a2007-12-04 20:04:21 +1100302 if (cryptlen < authsize)
303 return -EINVAL;
304 cryptlen -= authsize;
305
Herbert Xue56dd562007-12-10 16:20:24 +0800306 err = crypto_authenc_iverify(req, iv, cryptlen);
Herbert Xu3c09f172007-08-30 16:24:15 +0800307 if (err)
308 return err;
309
310 ablkcipher_request_set_tfm(abreq, ctx->enc);
311 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
Herbert Xue56dd562007-12-10 16:20:24 +0800312 req->base.complete, req->base.data);
313 ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
Herbert Xu3c09f172007-08-30 16:24:15 +0800314
315 return crypto_ablkcipher_decrypt(abreq);
316}
317
318static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
319{
320 struct crypto_instance *inst = (void *)tfm->__crt_alg;
321 struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
322 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
323 struct crypto_hash *auth;
324 struct crypto_ablkcipher *enc;
Herbert Xu3c09f172007-08-30 16:24:15 +0800325 int err;
326
327 auth = crypto_spawn_hash(&ictx->auth);
328 if (IS_ERR(auth))
329 return PTR_ERR(auth);
330
Herbert Xu9ffde352007-12-17 20:12:49 +0800331 enc = crypto_spawn_skcipher(&ictx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800332 err = PTR_ERR(enc);
333 if (IS_ERR(enc))
334 goto err_free_hash;
335
336 ctx->auth = auth;
337 ctx->enc = enc;
338 tfm->crt_aead.reqsize = max_t(unsigned int,
339 (crypto_hash_alignmask(auth) &
340 ~(crypto_tfm_ctx_alignment() - 1)) +
Herbert Xu7ba683a2007-12-02 18:49:21 +1100341 crypto_hash_digestsize(auth) * 2,
Herbert Xue56dd562007-12-10 16:20:24 +0800342 sizeof(struct skcipher_givcrypt_request) +
343 crypto_ablkcipher_reqsize(enc) +
344 crypto_ablkcipher_ivsize(enc));
Herbert Xu3c09f172007-08-30 16:24:15 +0800345
346 spin_lock_init(&ctx->auth_lock);
347
348 return 0;
349
350err_free_hash:
351 crypto_free_hash(auth);
352 return err;
353}
354
355static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
356{
357 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
358
359 crypto_free_hash(ctx->auth);
360 crypto_free_ablkcipher(ctx->enc);
361}
362
363static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
364{
Herbert Xu9ffde352007-12-17 20:12:49 +0800365 struct crypto_attr_type *algt;
Herbert Xu3c09f172007-08-30 16:24:15 +0800366 struct crypto_instance *inst;
367 struct crypto_alg *auth;
368 struct crypto_alg *enc;
369 struct authenc_instance_ctx *ctx;
Herbert Xu9ffde352007-12-17 20:12:49 +0800370 const char *enc_name;
Herbert Xu3c09f172007-08-30 16:24:15 +0800371 int err;
372
Herbert Xu9ffde352007-12-17 20:12:49 +0800373 algt = crypto_get_attr_type(tb);
374 err = PTR_ERR(algt);
375 if (IS_ERR(algt))
Herbert Xu3c09f172007-08-30 16:24:15 +0800376 return ERR_PTR(err);
377
Herbert Xu9ffde352007-12-17 20:12:49 +0800378 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
379 return ERR_PTR(-EINVAL);
380
Herbert Xu3c09f172007-08-30 16:24:15 +0800381 auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
382 CRYPTO_ALG_TYPE_HASH_MASK);
383 if (IS_ERR(auth))
384 return ERR_PTR(PTR_ERR(auth));
385
Herbert Xu9ffde352007-12-17 20:12:49 +0800386 enc_name = crypto_attr_alg_name(tb[2]);
387 err = PTR_ERR(enc_name);
388 if (IS_ERR(enc_name))
Herbert Xu3c09f172007-08-30 16:24:15 +0800389 goto out_put_auth;
390
Herbert Xu3c09f172007-08-30 16:24:15 +0800391 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
392 err = -ENOMEM;
393 if (!inst)
Herbert Xu9ffde352007-12-17 20:12:49 +0800394 goto out_put_auth;
Herbert Xue236d4a2007-11-22 23:11:53 +0800395
Herbert Xu3c09f172007-08-30 16:24:15 +0800396 ctx = crypto_instance_ctx(inst);
Herbert Xu3c09f172007-08-30 16:24:15 +0800397
398 err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
399 if (err)
400 goto err_free_inst;
401
Herbert Xu9ffde352007-12-17 20:12:49 +0800402 crypto_set_skcipher_spawn(&ctx->enc, inst);
403 err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
404 crypto_requires_sync(algt->type,
405 algt->mask));
Herbert Xu3c09f172007-08-30 16:24:15 +0800406 if (err)
407 goto err_drop_auth;
408
Herbert Xu9ffde352007-12-17 20:12:49 +0800409 enc = crypto_skcipher_spawn_alg(&ctx->enc);
410
411 err = -ENAMETOOLONG;
412 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
413 "authenc(%s,%s)", auth->cra_name, enc->cra_name) >=
414 CRYPTO_MAX_ALG_NAME)
415 goto err_drop_enc;
416
417 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
418 "authenc(%s,%s)", auth->cra_driver_name,
419 enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
420 goto err_drop_enc;
421
422 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
423 inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xu3c09f172007-08-30 16:24:15 +0800424 inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
425 inst->alg.cra_blocksize = enc->cra_blocksize;
Herbert Xue29bc6a2007-11-22 22:46:40 +0800426 inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask;
Herbert Xu3c09f172007-08-30 16:24:15 +0800427 inst->alg.cra_type = &crypto_aead_type;
428
Herbert Xuc2c61f512007-12-10 10:54:44 +0800429 inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100430 inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ?
431 auth->cra_hash.digestsize :
432 auth->cra_digest.dia_digestsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800433
434 inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
435
436 inst->alg.cra_init = crypto_authenc_init_tfm;
437 inst->alg.cra_exit = crypto_authenc_exit_tfm;
438
439 inst->alg.cra_aead.setkey = crypto_authenc_setkey;
440 inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
441 inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
Herbert Xue56dd562007-12-10 16:20:24 +0800442 inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt;
Herbert Xu3c09f172007-08-30 16:24:15 +0800443
444out:
Herbert Xu3c09f172007-08-30 16:24:15 +0800445 crypto_mod_put(auth);
446 return inst;
447
Herbert Xu9ffde352007-12-17 20:12:49 +0800448err_drop_enc:
449 crypto_drop_skcipher(&ctx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800450err_drop_auth:
451 crypto_drop_spawn(&ctx->auth);
452err_free_inst:
453 kfree(inst);
Herbert Xu9ffde352007-12-17 20:12:49 +0800454out_put_auth:
Herbert Xu3c09f172007-08-30 16:24:15 +0800455 inst = ERR_PTR(err);
456 goto out;
457}
458
459static void crypto_authenc_free(struct crypto_instance *inst)
460{
461 struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
462
Herbert Xu9ffde352007-12-17 20:12:49 +0800463 crypto_drop_skcipher(&ctx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800464 crypto_drop_spawn(&ctx->auth);
465 kfree(inst);
466}
467
468static struct crypto_template crypto_authenc_tmpl = {
469 .name = "authenc",
470 .alloc = crypto_authenc_alloc,
471 .free = crypto_authenc_free,
472 .module = THIS_MODULE,
473};
474
475static int __init crypto_authenc_module_init(void)
476{
477 return crypto_register_template(&crypto_authenc_tmpl);
478}
479
480static void __exit crypto_authenc_module_exit(void)
481{
482 crypto_unregister_template(&crypto_authenc_tmpl);
483}
484
485module_init(crypto_authenc_module_init);
486module_exit(crypto_authenc_module_exit);
487
488MODULE_LICENSE("GPL");
489MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");