blob: 2bb7348d8d5543364b1c75440e9d1c13ad1a0e2c [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 Xu9ffde352007-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
Steffen Klassertcbdcf802009-08-05 19:35:34 +100026typedef u8 *(*authenc_ahash_t)(struct aead_request *req, unsigned int flags);
27
Herbert Xu3c09f172007-08-30 16:24:15 +080028struct authenc_instance_ctx {
Steffen Klassertcbdcf802009-08-05 19:35:34 +100029 struct crypto_ahash_spawn auth;
Herbert Xu9ffde352007-12-17 20:12:49 +080030 struct crypto_skcipher_spawn enc;
Herbert Xu3c09f172007-08-30 16:24:15 +080031};
32
33struct crypto_authenc_ctx {
Steffen Klassertcbdcf802009-08-05 19:35:34 +100034 unsigned int reqoff;
35 struct crypto_ahash *auth;
Herbert Xu3c09f172007-08-30 16:24:15 +080036 struct crypto_ablkcipher *enc;
37};
38
Steffen Klassertcbdcf802009-08-05 19:35:34 +100039struct 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[];
47};
48
Herbert Xu3c09f172007-08-30 16:24:15 +080049static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
50 unsigned int keylen)
51{
Herbert Xu3c09f172007-08-30 16:24:15 +080052 unsigned int authkeylen;
Herbert Xue236d4a2007-11-22 23:11:53 +080053 unsigned int enckeylen;
Herbert Xu3c09f172007-08-30 16:24:15 +080054 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Steffen Klassertcbdcf802009-08-05 19:35:34 +100055 struct crypto_ahash *auth = ctx->auth;
Herbert Xu3c09f172007-08-30 16:24:15 +080056 struct crypto_ablkcipher *enc = ctx->enc;
Herbert Xue236d4a2007-11-22 23:11:53 +080057 struct rtattr *rta = (void *)key;
58 struct crypto_authenc_key_param *param;
Herbert Xu3c09f172007-08-30 16:24:15 +080059 int err = -EINVAL;
60
Herbert Xu12dc5e62007-12-10 10:55:21 +080061 if (!RTA_OK(rta, keylen))
Herbert Xue236d4a2007-11-22 23:11:53 +080062 goto badkey;
63 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
64 goto badkey;
65 if (RTA_PAYLOAD(rta) < sizeof(*param))
66 goto badkey;
67
68 param = RTA_DATA(rta);
69 enckeylen = be32_to_cpu(param->enckeylen);
70
71 key += RTA_ALIGN(rta->rta_len);
72 keylen -= RTA_ALIGN(rta->rta_len);
73
74 if (keylen < enckeylen)
75 goto badkey;
76
Herbert Xu3c09f172007-08-30 16:24:15 +080077 authkeylen = keylen - enckeylen;
78
Steffen Klassertcbdcf802009-08-05 19:35:34 +100079 crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
80 crypto_ahash_set_flags(auth, crypto_aead_get_flags(authenc) &
Herbert Xu3c09f172007-08-30 16:24:15 +080081 CRYPTO_TFM_REQ_MASK);
Steffen Klassertcbdcf802009-08-05 19:35:34 +100082 err = crypto_ahash_setkey(auth, key, authkeylen);
83 crypto_aead_set_flags(authenc, crypto_ahash_get_flags(auth) &
Herbert Xu3c09f172007-08-30 16:24:15 +080084 CRYPTO_TFM_RES_MASK);
85
86 if (err)
87 goto out;
88
89 crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
90 crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
91 CRYPTO_TFM_REQ_MASK);
92 err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
93 crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
94 CRYPTO_TFM_RES_MASK);
95
96out:
97 return err;
Herbert Xue236d4a2007-11-22 23:11:53 +080098
99badkey:
100 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
101 goto out;
Herbert Xu3c09f172007-08-30 16:24:15 +0800102}
103
Herbert Xue56dd562007-12-10 16:20:24 +0800104static void authenc_chain(struct scatterlist *head, struct scatterlist *sg,
105 int chain)
106{
107 if (chain) {
108 head->length += sg->length;
109 sg = scatterwalk_sg_next(sg);
110 }
111
112 if (sg)
113 scatterwalk_sg_chain(head, 2, sg);
114 else
115 sg_mark_end(head);
116}
117
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000118static void authenc_geniv_ahash_update_done(struct crypto_async_request *areq,
119 int err)
120{
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);
126
127 if (err)
128 goto out;
129
130 ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
131 areq_ctx->cryptlen);
132 ahash_request_set_callback(ahreq, aead_request_flags(req) &
133 CRYPTO_TFM_REQ_MAY_SLEEP,
134 areq_ctx->complete, req);
135
136 err = crypto_ahash_finup(ahreq);
137 if (err)
138 goto out;
139
140 scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
141 areq_ctx->cryptlen,
142 crypto_aead_authsize(authenc), 1);
143
144out:
145 aead_request_complete(req, err);
146}
147
148static void authenc_geniv_ahash_done(struct crypto_async_request *areq, int err)
149{
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);
155
156 if (err)
157 goto out;
158
159 scatterwalk_map_and_copy(ahreq->result, areq_ctx->sg,
160 areq_ctx->cryptlen,
161 crypto_aead_authsize(authenc), 1);
162
163out:
164 aead_request_complete(req, err);
165}
166
167static void authenc_verify_ahash_update_done(struct crypto_async_request *areq,
168 int err)
169{
170 u8 *ihash;
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);
178
179 if (err)
180 goto out;
181
182 ahash_request_set_crypt(ahreq, areq_ctx->sg, ahreq->result,
183 areq_ctx->cryptlen);
184 ahash_request_set_callback(ahreq, aead_request_flags(req) &
185 CRYPTO_TFM_REQ_MAY_SLEEP,
186 areq_ctx->complete, req);
187
188 err = crypto_ahash_finup(ahreq);
189 if (err)
190 goto out;
191
192 authsize = crypto_aead_authsize(authenc);
193 ihash = ahreq->result + authsize;
194 scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
195 authsize, 0);
196
Richard Hartmannf3542e62010-02-16 20:27:20 +0800197 err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000198 if (err)
199 goto out;
200
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);
207
208 err = crypto_ablkcipher_decrypt(abreq);
209
210out:
211 aead_request_complete(req, err);
212}
213
214static void authenc_verify_ahash_done(struct crypto_async_request *areq,
215 int err)
216{
217 u8 *ihash;
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);
225
226 if (err)
227 goto out;
228
229 authsize = crypto_aead_authsize(authenc);
230 ihash = ahreq->result + authsize;
231 scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
232 authsize, 0);
233
Richard Hartmannf3542e62010-02-16 20:27:20 +0800234 err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000235 if (err)
236 goto out;
237
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);
244
245 err = crypto_ablkcipher_decrypt(abreq);
246
247out:
248 aead_request_complete(req, err);
249}
250
251static u8 *crypto_authenc_ahash_fb(struct aead_request *req, unsigned int flags)
Herbert Xu3c09f172007-08-30 16:24:15 +0800252{
253 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Herbert Xu3c09f172007-08-30 16:24:15 +0800254 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000255 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;
Herbert Xu3c09f172007-08-30 16:24:15 +0800259 int err;
260
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000261 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
262 crypto_ahash_alignmask(auth) + 1);
Herbert Xu3c09f172007-08-30 16:24:15 +0800263
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000264 ahash_request_set_tfm(ahreq, auth);
265
266 err = crypto_ahash_init(ahreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800267 if (err)
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000268 return ERR_PTR(err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800269
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000270 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);
273
274 err = crypto_ahash_update(ahreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800275 if (err)
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000276 return ERR_PTR(err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800277
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000278 ahash_request_set_crypt(ahreq, areq_ctx->sg, hash,
279 areq_ctx->cryptlen);
280 ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
281 areq_ctx->complete, req);
282
283 err = crypto_ahash_finup(ahreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800284 if (err)
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000285 return ERR_PTR(err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800286
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000287 return hash;
288}
Herbert Xu3c09f172007-08-30 16:24:15 +0800289
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000290static u8 *crypto_authenc_ahash(struct aead_request *req, unsigned int flags)
291{
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;
298 int err;
299
300 hash = (u8 *)ALIGN((unsigned long)hash + crypto_ahash_alignmask(auth),
301 crypto_ahash_alignmask(auth) + 1);
302
303 ahash_request_set_tfm(ahreq, auth);
304 ahash_request_set_crypt(ahreq, areq_ctx->sg, hash,
305 areq_ctx->cryptlen);
306 ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
307 areq_ctx->complete, req);
308
309 err = crypto_ahash_digest(ahreq);
Herbert Xu3c09f172007-08-30 16:24:15 +0800310 if (err)
Herbert Xu7c3d7032007-12-10 16:15:41 +0800311 return ERR_PTR(err);
312
313 return hash;
314}
315
Herbert Xue56dd562007-12-10 16:20:24 +0800316static int crypto_authenc_genicv(struct aead_request *req, u8 *iv,
317 unsigned int flags)
Herbert Xu7c3d7032007-12-10 16:15:41 +0800318{
319 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000320 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800321 struct scatterlist *dst = req->dst;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000322 struct scatterlist *assoc = req->assoc;
323 struct scatterlist *cipher = areq_ctx->cipher;
324 struct scatterlist *asg = areq_ctx->asg;
Herbert Xue56dd562007-12-10 16:20:24 +0800325 unsigned int ivsize = crypto_aead_ivsize(authenc);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000326 unsigned int cryptlen = req->cryptlen;
327 authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
328 struct page *dstp;
Herbert Xue56dd562007-12-10 16:20:24 +0800329 u8 *vdst;
Herbert Xu7c3d7032007-12-10 16:15:41 +0800330 u8 *hash;
331
Herbert Xue56dd562007-12-10 16:20:24 +0800332 dstp = sg_page(dst);
333 vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + dst->offset;
334
Herbert Xu29b37f42009-01-13 11:26:18 +1100335 if (ivsize) {
336 sg_init_table(cipher, 2);
337 sg_set_buf(cipher, iv, ivsize);
338 authenc_chain(cipher, dst, vdst == iv + ivsize);
339 dst = cipher;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000340 cryptlen += ivsize;
Herbert Xu29b37f42009-01-13 11:26:18 +1100341 }
Herbert Xue56dd562007-12-10 16:20:24 +0800342
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000343 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);
348 dst = asg;
349 cryptlen += req->assoclen;
350 }
351
352 areq_ctx->cryptlen = cryptlen;
353 areq_ctx->sg = dst;
354
355 areq_ctx->complete = authenc_geniv_ahash_done;
356 areq_ctx->update_complete = authenc_geniv_ahash_update_done;
357
358 hash = authenc_ahash_fn(req, flags);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800359 if (IS_ERR(hash))
360 return PTR_ERR(hash);
Herbert Xu3c09f172007-08-30 16:24:15 +0800361
Herbert Xu29b37f42009-01-13 11:26:18 +1100362 scatterwalk_map_and_copy(hash, dst, cryptlen,
Herbert Xu7ba683a2007-12-02 18:49:21 +1100363 crypto_aead_authsize(authenc), 1);
Herbert Xu3c09f172007-08-30 16:24:15 +0800364 return 0;
365}
366
367static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
368 int err)
369{
Herbert Xua6976902008-08-23 01:04:06 +1000370 struct aead_request *areq = req->data;
371
Herbert Xue56dd562007-12-10 16:20:24 +0800372 if (!err) {
Herbert Xue56dd562007-12-10 16:20:24 +0800373 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);
378
379 err = crypto_authenc_genicv(areq, iv, 0);
380 }
Herbert Xu3c09f172007-08-30 16:24:15 +0800381
Herbert Xua6976902008-08-23 01:04:06 +1000382 aead_request_complete(areq, err);
Herbert Xu3c09f172007-08-30 16:24:15 +0800383}
384
385static int crypto_authenc_encrypt(struct aead_request *req)
386{
387 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
388 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
Steffen Klassert50beceb2010-03-03 22:41:08 +0800389 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xue56dd562007-12-10 16:20:24 +0800390 struct crypto_ablkcipher *enc = ctx->enc;
391 struct scatterlist *dst = req->dst;
392 unsigned int cryptlen = req->cryptlen;
Steffen Klassert50beceb2010-03-03 22:41:08 +0800393 struct ablkcipher_request *abreq = (void *)(areq_ctx->tail
394 + ctx->reqoff);
395 u8 *iv = (u8 *)abreq - crypto_ablkcipher_ivsize(enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800396 int err;
397
Herbert Xue56dd562007-12-10 16:20:24 +0800398 ablkcipher_request_set_tfm(abreq, enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800399 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
400 crypto_authenc_encrypt_done, req);
Herbert Xue56dd562007-12-10 16:20:24 +0800401 ablkcipher_request_set_crypt(abreq, req->src, dst, cryptlen, req->iv);
402
403 memcpy(iv, req->iv, crypto_aead_ivsize(authenc));
Herbert Xu3c09f172007-08-30 16:24:15 +0800404
405 err = crypto_ablkcipher_encrypt(abreq);
406 if (err)
407 return err;
408
Herbert Xue56dd562007-12-10 16:20:24 +0800409 return crypto_authenc_genicv(req, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
410}
411
412static void crypto_authenc_givencrypt_done(struct crypto_async_request *req,
413 int err)
414{
Herbert Xua6976902008-08-23 01:04:06 +1000415 struct aead_request *areq = req->data;
416
Herbert Xue56dd562007-12-10 16:20:24 +0800417 if (!err) {
Patrick McHardy16161322008-04-29 21:44:28 +0800418 struct skcipher_givcrypt_request *greq = aead_request_ctx(areq);
Herbert Xue56dd562007-12-10 16:20:24 +0800419
Patrick McHardy16161322008-04-29 21:44:28 +0800420 err = crypto_authenc_genicv(areq, greq->giv, 0);
Herbert Xue56dd562007-12-10 16:20:24 +0800421 }
422
Herbert Xua6976902008-08-23 01:04:06 +1000423 aead_request_complete(areq, err);
Herbert Xue56dd562007-12-10 16:20:24 +0800424}
425
426static int crypto_authenc_givencrypt(struct aead_givcrypt_request *req)
427{
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);
432 u8 *iv = req->giv;
433 int err;
434
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,
439 areq->iv);
440 skcipher_givcrypt_set_giv(greq, iv, req->seq);
441
442 err = crypto_skcipher_givencrypt(greq);
443 if (err)
444 return err;
445
446 return crypto_authenc_genicv(areq, iv, CRYPTO_TFM_REQ_MAY_SLEEP);
Herbert Xu3c09f172007-08-30 16:24:15 +0800447}
448
Herbert Xu481f34a2007-12-04 20:04:21 +1100449static int crypto_authenc_verify(struct aead_request *req,
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000450 authenc_ahash_t authenc_ahash_fn)
Herbert Xu3c09f172007-08-30 16:24:15 +0800451{
452 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000453 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800454 u8 *ohash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800455 u8 *ihash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800456 unsigned int authsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800457
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000458 areq_ctx->complete = authenc_verify_ahash_done;
Steffen Klassert77ba1152010-03-02 21:59:54 +0800459 areq_ctx->update_complete = authenc_verify_ahash_update_done;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000460
461 ohash = authenc_ahash_fn(req, CRYPTO_TFM_REQ_MAY_SLEEP);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800462 if (IS_ERR(ohash))
463 return PTR_ERR(ohash);
Herbert Xu3c09f172007-08-30 16:24:15 +0800464
Herbert Xu7ba683a2007-12-02 18:49:21 +1100465 authsize = crypto_aead_authsize(authenc);
Herbert Xu7c3d7032007-12-10 16:15:41 +0800466 ihash = ohash + authsize;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000467 scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
468 authsize, 0);
Richard Hartmannf3542e62010-02-16 20:27:20 +0800469 return memcmp(ihash, ohash, authsize) ? -EBADMSG : 0;
Herbert Xu3c09f172007-08-30 16:24:15 +0800470}
471
Herbert Xue56dd562007-12-10 16:20:24 +0800472static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,
473 unsigned int cryptlen)
Herbert Xu3c09f172007-08-30 16:24:15 +0800474{
Herbert Xue56dd562007-12-10 16:20:24 +0800475 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000476 struct authenc_request_ctx *areq_ctx = aead_request_ctx(req);
Herbert Xue56dd562007-12-10 16:20:24 +0800477 struct scatterlist *src = req->src;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000478 struct scatterlist *assoc = req->assoc;
479 struct scatterlist *cipher = areq_ctx->cipher;
480 struct scatterlist *asg = areq_ctx->asg;
Herbert Xue56dd562007-12-10 16:20:24 +0800481 unsigned int ivsize = crypto_aead_ivsize(authenc);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000482 authenc_ahash_t authenc_ahash_fn = crypto_authenc_ahash_fb;
483 struct page *srcp;
Herbert Xue56dd562007-12-10 16:20:24 +0800484 u8 *vsrc;
485
486 srcp = sg_page(src);
487 vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + src->offset;
488
Herbert Xu29b37f42009-01-13 11:26:18 +1100489 if (ivsize) {
490 sg_init_table(cipher, 2);
491 sg_set_buf(cipher, iv, ivsize);
492 authenc_chain(cipher, src, vsrc == iv + ivsize);
493 src = cipher;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000494 cryptlen += ivsize;
Herbert Xu29b37f42009-01-13 11:26:18 +1100495 }
Herbert Xue56dd562007-12-10 16:20:24 +0800496
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000497 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);
502 src = asg;
503 cryptlen += req->assoclen;
504 }
505
506 areq_ctx->cryptlen = cryptlen;
507 areq_ctx->sg = src;
508
509 return crypto_authenc_verify(req, authenc_ahash_fn);
Herbert Xu3c09f172007-08-30 16:24:15 +0800510}
511
512static int crypto_authenc_decrypt(struct aead_request *req)
513{
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);
Herbert Xu481f34a2007-12-04 20:04:21 +1100517 unsigned int cryptlen = req->cryptlen;
518 unsigned int authsize = crypto_aead_authsize(authenc);
Herbert Xue56dd562007-12-10 16:20:24 +0800519 u8 *iv = req->iv;
Herbert Xu3c09f172007-08-30 16:24:15 +0800520 int err;
521
Herbert Xu481f34a2007-12-04 20:04:21 +1100522 if (cryptlen < authsize)
523 return -EINVAL;
524 cryptlen -= authsize;
525
Herbert Xue56dd562007-12-10 16:20:24 +0800526 err = crypto_authenc_iverify(req, iv, cryptlen);
Herbert Xu3c09f172007-08-30 16:24:15 +0800527 if (err)
528 return err;
529
530 ablkcipher_request_set_tfm(abreq, ctx->enc);
531 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
Herbert Xue56dd562007-12-10 16:20:24 +0800532 req->base.complete, req->base.data);
533 ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, iv);
Herbert Xu3c09f172007-08-30 16:24:15 +0800534
535 return crypto_ablkcipher_decrypt(abreq);
536}
537
538static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
539{
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000540 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
Herbert Xu3c09f172007-08-30 16:24:15 +0800541 struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
542 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000543 struct crypto_ahash *auth;
Herbert Xu3c09f172007-08-30 16:24:15 +0800544 struct crypto_ablkcipher *enc;
Herbert Xu3c09f172007-08-30 16:24:15 +0800545 int err;
546
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000547 auth = crypto_spawn_ahash(&ictx->auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800548 if (IS_ERR(auth))
549 return PTR_ERR(auth);
550
Herbert Xu9ffde352007-12-17 20:12:49 +0800551 enc = crypto_spawn_skcipher(&ictx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800552 err = PTR_ERR(enc);
553 if (IS_ERR(enc))
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000554 goto err_free_ahash;
Herbert Xu3c09f172007-08-30 16:24:15 +0800555
556 ctx->auth = auth;
557 ctx->enc = enc;
Richard Hartmannf3542e62010-02-16 20:27:20 +0800558
Steffen Klassert50beceb2010-03-03 22:41:08 +0800559 ctx->reqoff = ALIGN(2 * crypto_ahash_digestsize(auth) +
560 crypto_ahash_alignmask(auth),
561 crypto_ahash_alignmask(auth) + 1) +
562 crypto_ablkcipher_ivsize(enc);
563
564 tfm->crt_aead.reqsize = sizeof(struct authenc_request_ctx) +
565 ctx->reqoff +
566 max_t(unsigned int,
567 crypto_ahash_reqsize(auth) +
Richard Hartmannf3542e62010-02-16 20:27:20 +0800568 sizeof(struct ahash_request),
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000569 sizeof(struct skcipher_givcrypt_request) +
Steffen Klassert50beceb2010-03-03 22:41:08 +0800570 crypto_ablkcipher_reqsize(enc));
Herbert Xu3c09f172007-08-30 16:24:15 +0800571
572 return 0;
573
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000574err_free_ahash:
575 crypto_free_ahash(auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800576 return err;
577}
578
579static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
580{
581 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
582
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000583 crypto_free_ahash(ctx->auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800584 crypto_free_ablkcipher(ctx->enc);
585}
586
587static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
588{
Herbert Xu9ffde352007-12-17 20:12:49 +0800589 struct crypto_attr_type *algt;
Herbert Xu3c09f172007-08-30 16:24:15 +0800590 struct crypto_instance *inst;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000591 struct hash_alg_common *auth;
592 struct crypto_alg *auth_base;
Herbert Xu3c09f172007-08-30 16:24:15 +0800593 struct crypto_alg *enc;
594 struct authenc_instance_ctx *ctx;
Herbert Xu9ffde352007-12-17 20:12:49 +0800595 const char *enc_name;
Herbert Xu3c09f172007-08-30 16:24:15 +0800596 int err;
597
Herbert Xu9ffde352007-12-17 20:12:49 +0800598 algt = crypto_get_attr_type(tb);
599 err = PTR_ERR(algt);
600 if (IS_ERR(algt))
Herbert Xu3c09f172007-08-30 16:24:15 +0800601 return ERR_PTR(err);
602
Herbert Xu9ffde352007-12-17 20:12:49 +0800603 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
604 return ERR_PTR(-EINVAL);
605
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000606 auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
607 CRYPTO_ALG_TYPE_AHASH_MASK);
Herbert Xu3c09f172007-08-30 16:24:15 +0800608 if (IS_ERR(auth))
609 return ERR_PTR(PTR_ERR(auth));
610
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000611 auth_base = &auth->base;
612
Herbert Xu9ffde352007-12-17 20:12:49 +0800613 enc_name = crypto_attr_alg_name(tb[2]);
614 err = PTR_ERR(enc_name);
615 if (IS_ERR(enc_name))
Herbert Xu3c09f172007-08-30 16:24:15 +0800616 goto out_put_auth;
617
Herbert Xu3c09f172007-08-30 16:24:15 +0800618 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
619 err = -ENOMEM;
620 if (!inst)
Herbert Xu9ffde352007-12-17 20:12:49 +0800621 goto out_put_auth;
Herbert Xue236d4a2007-11-22 23:11:53 +0800622
Herbert Xu3c09f172007-08-30 16:24:15 +0800623 ctx = crypto_instance_ctx(inst);
Herbert Xu3c09f172007-08-30 16:24:15 +0800624
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000625 err = crypto_init_ahash_spawn(&ctx->auth, auth, inst);
Herbert Xu3c09f172007-08-30 16:24:15 +0800626 if (err)
627 goto err_free_inst;
628
Herbert Xu9ffde352007-12-17 20:12:49 +0800629 crypto_set_skcipher_spawn(&ctx->enc, inst);
630 err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
631 crypto_requires_sync(algt->type,
632 algt->mask));
Herbert Xu3c09f172007-08-30 16:24:15 +0800633 if (err)
634 goto err_drop_auth;
635
Herbert Xu9ffde352007-12-17 20:12:49 +0800636 enc = crypto_skcipher_spawn_alg(&ctx->enc);
637
638 err = -ENAMETOOLONG;
639 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000640 "authenc(%s,%s)", auth_base->cra_name, enc->cra_name) >=
Herbert Xu9ffde352007-12-17 20:12:49 +0800641 CRYPTO_MAX_ALG_NAME)
642 goto err_drop_enc;
643
644 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000645 "authenc(%s,%s)", auth_base->cra_driver_name,
Herbert Xu9ffde352007-12-17 20:12:49 +0800646 enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
647 goto err_drop_enc;
648
649 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
650 inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000651 inst->alg.cra_priority = enc->cra_priority *
652 10 + auth_base->cra_priority;
Herbert Xu3c09f172007-08-30 16:24:15 +0800653 inst->alg.cra_blocksize = enc->cra_blocksize;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000654 inst->alg.cra_alignmask = auth_base->cra_alignmask | enc->cra_alignmask;
Herbert Xu3c09f172007-08-30 16:24:15 +0800655 inst->alg.cra_type = &crypto_aead_type;
656
Herbert Xuc2c61f512007-12-10 10:54:44 +0800657 inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000658 inst->alg.cra_aead.maxauthsize = auth->digestsize;
Herbert Xu3c09f172007-08-30 16:24:15 +0800659
660 inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
661
662 inst->alg.cra_init = crypto_authenc_init_tfm;
663 inst->alg.cra_exit = crypto_authenc_exit_tfm;
664
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;
Herbert Xue56dd562007-12-10 16:20:24 +0800668 inst->alg.cra_aead.givencrypt = crypto_authenc_givencrypt;
Herbert Xu3c09f172007-08-30 16:24:15 +0800669
670out:
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000671 crypto_mod_put(auth_base);
Herbert Xu3c09f172007-08-30 16:24:15 +0800672 return inst;
673
Herbert Xu9ffde352007-12-17 20:12:49 +0800674err_drop_enc:
675 crypto_drop_skcipher(&ctx->enc);
Herbert Xu3c09f172007-08-30 16:24:15 +0800676err_drop_auth:
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000677 crypto_drop_ahash(&ctx->auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800678err_free_inst:
679 kfree(inst);
Herbert Xu9ffde352007-12-17 20:12:49 +0800680out_put_auth:
Herbert Xu3c09f172007-08-30 16:24:15 +0800681 inst = ERR_PTR(err);
682 goto out;
683}
684
685static void crypto_authenc_free(struct crypto_instance *inst)
686{
687 struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
688
Herbert Xu9ffde352007-12-17 20:12:49 +0800689 crypto_drop_skcipher(&ctx->enc);
Steffen Klassertcbdcf802009-08-05 19:35:34 +1000690 crypto_drop_ahash(&ctx->auth);
Herbert Xu3c09f172007-08-30 16:24:15 +0800691 kfree(inst);
692}
693
694static struct crypto_template crypto_authenc_tmpl = {
695 .name = "authenc",
696 .alloc = crypto_authenc_alloc,
697 .free = crypto_authenc_free,
698 .module = THIS_MODULE,
699};
700
701static int __init crypto_authenc_module_init(void)
702{
703 return crypto_register_template(&crypto_authenc_tmpl);
704}
705
706static void __exit crypto_authenc_module_exit(void)
707{
708 crypto_unregister_template(&crypto_authenc_tmpl);
709}
710
711module_init(crypto_authenc_module_init);
712module_exit(crypto_authenc_module_exit);
713
714MODULE_LICENSE("GPL");
715MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");