blob: 5793b64c81a86449c7b09f56ae918f1ffcb73b9f [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 Xu5f7082e2008-08-31 22:21:09 +100014#include <crypto/internal/hash.h>
Herbert Xu9ffde35a2007-12-17 20:12:49 +080015#include <crypto/internal/skcipher.h>
Herbert Xue236d4a2007-11-22 23:11:53 +080016#include <crypto/authenc.h>
Herbert Xu42c271c2007-12-07 18:52:49 +080017#include <crypto/scatterwalk.h>
Herbert Xu3c09f172007-08-30 16:24:15 +080018#include <linux/err.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
Herbert Xue236d4a2007-11-22 23:11:53 +080022#include <linux/rtnetlink.h>
Herbert Xu3c09f172007-08-30 16:24:15 +080023#include <linux/slab.h>
24#include <linux/spinlock.h>
25
Herbert Xu3c09f172007-08-30 16:24:15 +080026struct authenc_instance_ctx {
27 struct crypto_spawn auth;
Herbert Xu9ffde35a2007-12-17 20:12:49 +080028 struct crypto_skcipher_spawn enc;
Herbert Xu3c09f172007-08-30 16:24:15 +080029};
30
31struct crypto_authenc_ctx {
32 spinlock_t auth_lock;
33 struct crypto_hash *auth;
34 struct crypto_ablkcipher *enc;
35};
36
37static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
38 unsigned int keylen)
39{
Herbert Xu3c09f172007-08-30 16:24:15 +080040 unsigned int authkeylen;
Herbert Xue236d4a2007-11-22 23:11:53 +080041 unsigned int enckeylen;
Herbert Xu3c09f172007-08-30 16:24:15 +080042 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
43 struct crypto_hash *auth = ctx->auth;
44 struct crypto_ablkcipher *enc = ctx->enc;
Herbert Xue236d4a2007-11-22 23:11:53 +080045 struct rtattr *rta = (void *)key;
46 struct crypto_authenc_key_param *param;
Herbert Xu3c09f172007-08-30 16:24:15 +080047 int err = -EINVAL;
48
Herbert Xu12dc5e62007-12-10 10:55:21 +080049 if (!RTA_OK(rta, keylen))
Herbert Xue236d4a2007-11-22 23:11:53 +080050 goto badkey;
51 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
52 goto badkey;
53 if (RTA_PAYLOAD(rta) < sizeof(*param))
54 goto badkey;
55
56 param = RTA_DATA(rta);
57 enckeylen = be32_to_cpu(param->enckeylen);
58
59 key += RTA_ALIGN(rta->rta_len);
60 keylen -= RTA_ALIGN(rta->rta_len);
61
62 if (keylen < enckeylen)
63 goto badkey;
64
Herbert Xu3c09f172007-08-30 16:24:15 +080065 authkeylen = keylen - enckeylen;
66
67 crypto_hash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
68 crypto_hash_set_flags(auth, crypto_aead_get_flags(authenc) &
69 CRYPTO_TFM_REQ_MASK);
70 err = crypto_hash_setkey(auth, key, authkeylen);
71 crypto_aead_set_flags(authenc, crypto_hash_get_flags(auth) &
72 CRYPTO_TFM_RES_MASK);
73
74 if (err)
75 goto out;
76
77 crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
78 crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
79 CRYPTO_TFM_REQ_MASK);
80 err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
81 crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
82 CRYPTO_TFM_RES_MASK);
83
84out:
85 return err;
Herbert Xue236d4a2007-11-22 23:11:53 +080086
87badkey:
88 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
89 goto out;
Herbert Xu3c09f172007-08-30 16:24:15 +080090}
91
Herbert Xue56dd562007-12-10 16:20:24 +080092static void authenc_chain(struct scatterlist *head, struct scatterlist *sg,
93 int chain)
94{
95 if (chain) {
96 head->length += sg->length;
97 sg = scatterwalk_sg_next(sg);
98 }
99
100 if (sg)
101 scatterwalk_sg_chain(head, 2, sg);
102 else
103 sg_mark_end(head);
104}
105
Herbert Xu7c3d7032007-12-10 16:15:41 +0800106static u8 *crypto_authenc_hash(struct aead_request *req, unsigned int flags,
107 struct scatterlist *cipher,
108 unsigned int cryptlen)
Herbert Xu3c09f172007-08-30 16:24:15 +0800109{
110 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu3c09f172007-08-30 16:24:15 +0800111 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
112 struct crypto_hash *auth = ctx->auth;
113 struct hash_desc desc = {
114 .tfm = auth,
Herbert Xu7c3d7032007-12-10 16:15:41 +0800115 .flags = aead_request_flags(req) & flags,
Herbert Xu3c09f172007-08-30 16:24:15 +0800116 };
117 u8 *hash = aead_request_ctx(req);
Herbert Xu3c09f172007-08-30 16:24:15 +0800118 int err;
119
120 hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth),
121 crypto_hash_alignmask(auth) + 1);
122
123 spin_lock_bh(&ctx->auth_lock);
124 err = crypto_hash_init(&desc);
125 if (err)
126 goto auth_unlock;
127
128 err = crypto_hash_update(&desc, req->assoc, req->assoclen);
129 if (err)
130 goto auth_unlock;
131
Herbert Xu7c3d7032007-12-10 16:15:41 +0800132 err = crypto_hash_update(&desc, cipher, cryptlen);
Herbert Xu3c09f172007-08-30 16:24:15 +0800133 if (err)
134 goto auth_unlock;
135
136 err = crypto_hash_final(&desc, hash);
137auth_unlock:
138 spin_unlock_bh(&ctx->auth_lock);
139
140 if (err)
Herbert Xu7c3d7032007-12-10 16:15:41 +0800141 return ERR_PTR(err);
142
143 return hash;
144}
145
Herbert Xue56dd562007-12-10 16:20:24 +0800146static int crypto_authenc_genicv(struct aead_request *req, u8 *iv,
147 unsigned int flags)
Herbert Xu7c3d7032007-12-10 16:15:41 +0800148{
149 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
150 struct scatterlist *dst = req->dst;
Herbert Xue56dd562007-12-10 16:20:24 +0800151 struct scatterlist cipher[2];
152 struct page *dstp;
153 unsigned int ivsize = crypto_aead_ivsize(authenc);
154 unsigned int cryptlen;
155 u8 *vdst;
Herbert Xu7c3d7032007-12-10 16:15:41 +0800156 u8 *hash;
157
Herbert Xue56dd562007-12-10 16:20:24 +0800158 dstp = sg_page(dst);
159 vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset;
160
Herbert Xu29b37f42009-01-13 11:26:18 +1100161 if (ivsize) {
162 sg_init_table(cipher, 2);
163 sg_set_buf(cipher, iv, ivsize);
164 authenc_chain(cipher, dst, vdst == iv + ivsize);
165 dst = cipher;
166 }
Herbert Xue56dd562007-12-10 16:20:24 +0800167
168 cryptlen = req->cryptlen + ivsize;
Herbert Xu29b37f42009-01-13 11:26:18 +1100169 hash = crypto_authenc_hash(req, flags, dst, cryptlen);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800170 if (IS_ERR(hash))
171 return PTR_ERR(hash);
Herbert Xu3c09f172007-08-30 16:24:15 +0800172
Herbert Xu29b37f42009-01-13 11:26:18 +1100173 scatterwalk_map_and_copy(hash, dst, cryptlen,
Herbert Xu7ba683a2007-12-02 18:49:21 +1100174 crypto_aead_authsize(authenc), 1);
Herbert Xu3c09f172007-08-30 16:24:15 +0800175 return 0;
176}
177
178static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
179 int err)
180{
Herbert Xua6976902008-08-23 01:04:06 +1000181 struct aead_request *areq = req->data;
182
Herbert Xue56dd562007-12-10 16:20:24 +0800183 if (!err) {
Herbert Xue56dd562007-12-10 16:20:24 +0800184 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
185 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
186 struct ablkcipher_request *abreq = aead_request_ctx(areq);
187 u8 *iv = (u8 *)(abreq + 1) +
188 crypto_ablkcipher_reqsize(ctx->enc);
189
190 err = crypto_authenc_genicv(areq, iv, 0);
191 }
Herbert Xu3c09f172007-08-30 16:24:15 +0800192
Herbert Xua6976902008-08-23 01:04:06 +1000193 aead_request_complete(areq, err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800194}
195
196static int crypto_authenc_encrypt(struct aead_request *req)
197{
198 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
199 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
200 struct ablkcipher_request *abreq = aead_request_ctx(req);
Herbert Xue56dd562007-12-10 16:20:24 +0800201 struct crypto_ablkcipher *enc = ctx->enc;
202 struct scatterlist *dst = req->dst;
203 unsigned int cryptlen = req->cryptlen;
204 u8 *iv = (u8 *)(abreq + 1) + crypto_ablkcipher_reqsize(enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800205 int err;
206
Herbert Xue56dd562007-12-10 16:20:24 +0800207 ablkcipher_request_set_tfm(abreq, enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800208 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
209 crypto_authenc_encrypt_done, req);
Herbert Xue56dd562007-12-10 16:20:24 +0800210 ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
211
212 memcpy(iv, req->iv, crypto_aead_ivsize(authenc));
Herbert Xu3c09f172007-08-30 16:24:15 +0800213
214 err = crypto_ablkcipher_encrypt(abreq);
215 if (err)
216 return err;
217
Herbert Xue56dd562007-12-10 16:20:24 +0800218 return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
219}
220
221static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
222 int err)
223{
Herbert Xua6976902008-08-23 01:04:06 +1000224 struct aead_request *areq = req->data;
225
Herbert Xue56dd562007-12-10 16:20:24 +0800226 if (!err) {
Patrick McHardy16161322008-04-29 21:44:28 +0800227 struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
Herbert Xue56dd562007-12-10 16:20:24 +0800228
Patrick McHardy16161322008-04-29 21:44:28 +0800229 err = crypto_authenc_genicv(areq, greq->giv, 0);
Herbert Xue56dd562007-12-10 16:20:24 +0800230 }
231
Herbert Xua6976902008-08-23 01:04:06 +1000232 aead_request_complete(areq, err);
Herbert Xue56dd562007-12-10 16:20:24 +0800233}
234
235static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
236{
237 struct crypto_aead *authenc = aead_givcrypt_reqtfm(req);
238 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
239 struct aead_request *areq = &req->areq;
240 struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
241 u8 *iv = req->giv;
242 int err;
243
244 skcipher_givcrypt_set_tfm(greq, ctx->enc);
245 skcipher_givcrypt_set_callback(greq, aead_request_flags(areq),
246 crypto_authenc_givencrypt_done, areq);
247 skcipher_givcrypt_set_crypt(greq, areq->src, areq->dst, areq->cryptlen,
248 areq->iv);
249 skcipher_givcrypt_set_giv(greq, iv, req->seq);
250
251 err = crypto_skcipher_givencrypt(greq);
252 if (err)
253 return err;
254
255 return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
Herbert Xu3c09f172007-08-30 16:24:15 +0800256}
257
Herbert Xu481f34a2007-12-04 20:04:21 +1100258static int crypto_authenc_verify(struct aead_request *req,
Herbert Xue56dd562007-12-10 16:20:24 +0800259 struct scatterlist *cipher,
Herbert Xu481f34a2007-12-04 20:04:21 +1100260 unsigned int cryptlen)
Herbert Xu3c09f172007-08-30 16:24:15 +0800261{
262 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800263 u8 *ohash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800264 u8 *ihash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800265 unsigned int authsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800266
Herbert Xue56dd562007-12-10 16:20:24 +0800267 ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, cipher,
Herbert Xu7c3d7032007-12-10 16:15:41 +0800268 cryptlen);
269 if (IS_ERR(ohash))
270 return PTR_ERR(ohash);
Herbert Xu3c09f172007-08-30 16:24:15 +0800271
Herbert Xu7ba683a2007-12-02 18:49:21 +1100272 authsize = crypto_aead_authsize(authenc);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800273 ihash = ohash + authsize;
Herbert Xue56dd562007-12-10 16:20:24 +0800274 scatterwalk_map_and_copy(ihash, cipher, cryptlen, authsize, 0);
Herbert Xufe70f5d2007-12-04 20:07:27 +1100275 return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
Herbert Xu3c09f172007-08-30 16:24:15 +0800276}
277
Herbert Xue56dd562007-12-10 16:20:24 +0800278static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
279 unsigned int cryptlen)
Herbert Xu3c09f172007-08-30 16:24:15 +0800280{
Herbert Xue56dd562007-12-10 16:20:24 +0800281 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
282 struct scatterlist *src = req->src;
283 struct scatterlist cipher[2];
284 struct page *srcp;
285 unsigned int ivsize = crypto_aead_ivsize(authenc);
286 u8 *vsrc;
287
288 srcp = sg_page(src);
289 vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
290
Herbert Xu29b37f42009-01-13 11:26:18 +1100291 if (ivsize) {
292 sg_init_table(cipher, 2);
293 sg_set_buf(cipher, iv, ivsize);
294 authenc_chain(cipher, src, vsrc == iv + ivsize);
295 src = cipher;
296 }
Herbert Xue56dd562007-12-10 16:20:24 +0800297
Herbert Xu29b37f42009-01-13 11:26:18 +1100298 return crypto_authenc_verify(req, src, cryptlen + ivsize);
Herbert Xu3c09f172007-08-30 16:24:15 +0800299}
300
301static int crypto_authenc_decrypt(struct aead_request *req)
302{
303 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
304 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
305 struct ablkcipher_request *abreq = aead_request_ctx(req);
Herbert Xu481f34a2007-12-04 20:04:21 +1100306 unsigned int cryptlen = req->cryptlen;
307 unsigned int authsize = crypto_aead_authsize(authenc);
Herbert Xue56dd562007-12-10 16:20:24 +0800308 u8 *iv = req->iv;
Herbert Xu3c09f172007-08-30 16:24:15 +0800309 int err;
310
Herbert Xu481f34a2007-12-04 20:04:21 +1100311 if (cryptlen < authsize)
312 return -EINVAL;
313 cryptlen -= authsize;
314
Herbert Xue56dd562007-12-10 16:20:24 +0800315 err = crypto_authenc_iverify(req, iv, cryptlen);
Herbert Xu3c09f172007-08-30 16:24:15 +0800316 if (err)
317 return err;
318
319 ablkcipher_request_set_tfm(abreq, ctx->enc);
320 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
Herbert Xue56dd562007-12-10 16:20:24 +0800321 req->base.complete, req->base.data);
322 ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
Herbert Xu3c09f172007-08-30 16:24:15 +0800323
324 return crypto_ablkcipher_decrypt(abreq);
325}
326
327static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
328{
329 struct crypto_instance *inst = (void *)tfm->__crt_alg;
330 struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
331 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
332 struct crypto_hash *auth;
333 struct crypto_ablkcipher *enc;
Herbert Xu3c09f172007-08-30 16:24:15 +0800334 int err;
335
336 auth = crypto_spawn_hash(&ictx->auth);
337 if (IS_ERR(auth))
338 return PTR_ERR(auth);
339
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800340 enc = crypto_spawn_skcipher(&ictx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800341 err = PTR_ERR(enc);
342 if (IS_ERR(enc))
343 goto err_free_hash;
344
345 ctx->auth = auth;
346 ctx->enc = enc;
347 tfm->crt_aead.reqsize = max_t(unsigned int,
348 (crypto_hash_alignmask(auth) &
349 ~(crypto_tfm_ctx_alignment() - 1)) +
Herbert Xu7ba683a2007-12-02 18:49:21 +1100350 crypto_hash_digestsize(auth) * 2,
Herbert Xue56dd562007-12-10 16:20:24 +0800351 sizeof(struct skcipher_givcrypt_request) +
352 crypto_ablkcipher_reqsize(enc) +
353 crypto_ablkcipher_ivsize(enc));
Herbert Xu3c09f172007-08-30 16:24:15 +0800354
355 spin_lock_init(&ctx->auth_lock);
356
357 return 0;
358
359err_free_hash:
360 crypto_free_hash(auth);
361 return err;
362}
363
364static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
365{
366 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
367
368 crypto_free_hash(ctx->auth);
369 crypto_free_ablkcipher(ctx->enc);
370}
371
372static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
373{
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800374 struct crypto_attr_type *algt;
Herbert Xu3c09f172007-08-30 16:24:15 +0800375 struct crypto_instance *inst;
376 struct crypto_alg *auth;
377 struct crypto_alg *enc;
378 struct authenc_instance_ctx *ctx;
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800379 const char *enc_name;
Herbert Xu3c09f172007-08-30 16:24:15 +0800380 int err;
381
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800382 algt = crypto_get_attr_type(tb);
383 err = PTR_ERR(algt);
384 if (IS_ERR(algt))
Herbert Xu3c09f172007-08-30 16:24:15 +0800385 return ERR_PTR(err);
386
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800387 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
388 return ERR_PTR(-EINVAL);
389
Herbert Xu3c09f172007-08-30 16:24:15 +0800390 auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
391 CRYPTO_ALG_TYPE_HASH_MASK);
392 if (IS_ERR(auth))
393 return ERR_PTR(PTR_ERR(auth));
394
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800395 enc_name = crypto_attr_alg_name(tb[2]);
396 err = PTR_ERR(enc_name);
397 if (IS_ERR(enc_name))
Herbert Xu3c09f172007-08-30 16:24:15 +0800398 goto out_put_auth;
399
Herbert Xu3c09f172007-08-30 16:24:15 +0800400 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
401 err = -ENOMEM;
402 if (!inst)
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800403 goto out_put_auth;
Herbert Xue236d4a2007-11-22 23:11:53 +0800404
Herbert Xu3c09f172007-08-30 16:24:15 +0800405 ctx = crypto_instance_ctx(inst);
Herbert Xu3c09f172007-08-30 16:24:15 +0800406
407 err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
408 if (err)
409 goto err_free_inst;
410
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800411 crypto_set_skcipher_spawn(&ctx->enc, inst);
412 err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
413 crypto_requires_sync(algt->type,
414 algt->mask));
Herbert Xu3c09f172007-08-30 16:24:15 +0800415 if (err)
416 goto err_drop_auth;
417
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800418 enc = crypto_skcipher_spawn_alg(&ctx->enc);
419
420 err = -ENAMETOOLONG;
421 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
422 "authenc(%s,%s)", auth->cra_name, enc->cra_name) >=
423 CRYPTO_MAX_ALG_NAME)
424 goto err_drop_enc;
425
426 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
427 "authenc(%s,%s)", auth->cra_driver_name,
428 enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
429 goto err_drop_enc;
430
431 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
432 inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xu3c09f172007-08-30 16:24:15 +0800433 inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
434 inst->alg.cra_blocksize = enc->cra_blocksize;
Herbert Xue29bc6a2007-11-22 22:46:40 +0800435 inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask;
Herbert Xu3c09f172007-08-30 16:24:15 +0800436 inst->alg.cra_type = &crypto_aead_type;
437
Herbert Xuc2c61f512007-12-10 10:54:44 +0800438 inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100439 inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ?
440 auth->cra_hash.digestsize :
Herbert Xu5f7082e2008-08-31 22:21:09 +1000441 auth->cra_type ?
442 __crypto_shash_alg(auth)->digestsize :
Herbert Xu7ba683a2007-12-02 18:49:21 +1100443 auth->cra_digest.dia_digestsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800444
445 inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
446
447 inst->alg.cra_init = crypto_authenc_init_tfm;
448 inst->alg.cra_exit = crypto_authenc_exit_tfm;
449
450 inst->alg.cra_aead.setkey = crypto_authenc_setkey;
451 inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
452 inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
Herbert Xue56dd562007-12-10 16:20:24 +0800453 inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt;
Herbert Xu3c09f172007-08-30 16:24:15 +0800454
455out:
Herbert Xu3c09f172007-08-30 16:24:15 +0800456 crypto_mod_put(auth);
457 return inst;
458
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800459err_drop_enc:
460 crypto_drop_skcipher(&ctx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800461err_drop_auth:
462 crypto_drop_spawn(&ctx->auth);
463err_free_inst:
464 kfree(inst);
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800465out_put_auth:
Herbert Xu3c09f172007-08-30 16:24:15 +0800466 inst = ERR_PTR(err);
467 goto out;
468}
469
470static void crypto_authenc_free(struct crypto_instance *inst)
471{
472 struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
473
Herbert Xu9ffde35a2007-12-17 20:12:49 +0800474 crypto_drop_skcipher(&ctx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800475 crypto_drop_spawn(&ctx->auth);
476 kfree(inst);
477}
478
479static struct crypto_template crypto_authenc_tmpl = {
480 .name = "authenc",
481 .alloc = crypto_authenc_alloc,
482 .free = crypto_authenc_free,
483 .module = THIS_MODULE,
484};
485
486static int __init crypto_authenc_module_init(void)
487{
488 return crypto_register_template(&crypto_authenc_tmpl);
489}
490
491static void __exit crypto_authenc_module_exit(void)
492{
493 crypto_unregister_template(&crypto_authenc_tmpl);
494}
495
496module_init(crypto_authenc_module_init);
497module_exit(crypto_authenc_module_exit);
498
499MODULE_LICENSE("GPL");
500MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");