]>
Commit | Line | Data |
---|---|---|
3c09f17c HX |
1 | /* |
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 | ||
13 | #include <crypto/algapi.h> | |
e236d4a8 | 14 | #include <crypto/authenc.h> |
42c271c6 | 15 | #include <crypto/scatterwalk.h> |
3c09f17c HX |
16 | #include <linux/err.h> |
17 | #include <linux/init.h> | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/module.h> | |
e236d4a8 | 20 | #include <linux/rtnetlink.h> |
3c09f17c HX |
21 | #include <linux/slab.h> |
22 | #include <linux/spinlock.h> | |
23 | ||
3c09f17c HX |
24 | struct authenc_instance_ctx { |
25 | struct crypto_spawn auth; | |
26 | struct crypto_spawn enc; | |
3c09f17c HX |
27 | }; |
28 | ||
29 | struct crypto_authenc_ctx { | |
30 | spinlock_t auth_lock; | |
31 | struct crypto_hash *auth; | |
32 | struct crypto_ablkcipher *enc; | |
33 | }; | |
34 | ||
35 | static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key, | |
36 | unsigned int keylen) | |
37 | { | |
3c09f17c | 38 | unsigned int authkeylen; |
e236d4a8 | 39 | unsigned int enckeylen; |
3c09f17c HX |
40 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
41 | struct crypto_hash *auth = ctx->auth; | |
42 | struct crypto_ablkcipher *enc = ctx->enc; | |
e236d4a8 HX |
43 | struct rtattr *rta = (void *)key; |
44 | struct crypto_authenc_key_param *param; | |
3c09f17c HX |
45 | int err = -EINVAL; |
46 | ||
12dc5e62 | 47 | if (!RTA_OK(rta, keylen)) |
e236d4a8 HX |
48 | goto badkey; |
49 | if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM) | |
50 | goto badkey; | |
51 | if (RTA_PAYLOAD(rta) < sizeof(*param)) | |
52 | goto badkey; | |
53 | ||
54 | param = RTA_DATA(rta); | |
55 | enckeylen = be32_to_cpu(param->enckeylen); | |
56 | ||
57 | key += RTA_ALIGN(rta->rta_len); | |
58 | keylen -= RTA_ALIGN(rta->rta_len); | |
59 | ||
60 | if (keylen < enckeylen) | |
61 | goto badkey; | |
62 | ||
3c09f17c HX |
63 | authkeylen = keylen - enckeylen; |
64 | ||
65 | crypto_hash_clear_flags(auth, CRYPTO_TFM_REQ_MASK); | |
66 | crypto_hash_set_flags(auth, crypto_aead_get_flags(authenc) & | |
67 | CRYPTO_TFM_REQ_MASK); | |
68 | err = crypto_hash_setkey(auth, key, authkeylen); | |
69 | crypto_aead_set_flags(authenc, crypto_hash_get_flags(auth) & | |
70 | CRYPTO_TFM_RES_MASK); | |
71 | ||
72 | if (err) | |
73 | goto out; | |
74 | ||
75 | crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK); | |
76 | crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) & | |
77 | CRYPTO_TFM_REQ_MASK); | |
78 | err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen); | |
79 | crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) & | |
80 | CRYPTO_TFM_RES_MASK); | |
81 | ||
82 | out: | |
83 | return err; | |
e236d4a8 HX |
84 | |
85 | badkey: | |
86 | crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN); | |
87 | goto out; | |
3c09f17c HX |
88 | } |
89 | ||
7c3d703f HX |
90 | static u8 *crypto_authenc_hash(struct aead_request *req, unsigned int flags, |
91 | struct scatterlist *cipher, | |
92 | unsigned int cryptlen) | |
3c09f17c HX |
93 | { |
94 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); | |
3c09f17c HX |
95 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
96 | struct crypto_hash *auth = ctx->auth; | |
97 | struct hash_desc desc = { | |
98 | .tfm = auth, | |
7c3d703f | 99 | .flags = aead_request_flags(req) & flags, |
3c09f17c HX |
100 | }; |
101 | u8 *hash = aead_request_ctx(req); | |
3c09f17c HX |
102 | int err; |
103 | ||
104 | hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth), | |
105 | crypto_hash_alignmask(auth) + 1); | |
106 | ||
107 | spin_lock_bh(&ctx->auth_lock); | |
108 | err = crypto_hash_init(&desc); | |
109 | if (err) | |
110 | goto auth_unlock; | |
111 | ||
112 | err = crypto_hash_update(&desc, req->assoc, req->assoclen); | |
113 | if (err) | |
114 | goto auth_unlock; | |
115 | ||
7c3d703f | 116 | err = crypto_hash_update(&desc, cipher, cryptlen); |
3c09f17c HX |
117 | if (err) |
118 | goto auth_unlock; | |
119 | ||
120 | err = crypto_hash_final(&desc, hash); | |
121 | auth_unlock: | |
122 | spin_unlock_bh(&ctx->auth_lock); | |
123 | ||
124 | if (err) | |
7c3d703f HX |
125 | return ERR_PTR(err); |
126 | ||
127 | return hash; | |
128 | } | |
129 | ||
130 | static int crypto_authenc_genicv(struct aead_request *req, unsigned int flags) | |
131 | { | |
132 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); | |
133 | struct scatterlist *dst = req->dst; | |
134 | unsigned int cryptlen = req->cryptlen; | |
135 | u8 *hash; | |
136 | ||
137 | hash = crypto_authenc_hash(req, flags, dst, cryptlen); | |
138 | if (IS_ERR(hash)) | |
139 | return PTR_ERR(hash); | |
3c09f17c | 140 | |
7ba683a6 HX |
141 | scatterwalk_map_and_copy(hash, dst, cryptlen, |
142 | crypto_aead_authsize(authenc), 1); | |
3c09f17c HX |
143 | return 0; |
144 | } | |
145 | ||
146 | static void crypto_authenc_encrypt_done(struct crypto_async_request *req, | |
147 | int err) | |
148 | { | |
149 | if (!err) | |
7c3d703f | 150 | err = crypto_authenc_genicv(req->data, 0); |
3c09f17c HX |
151 | |
152 | aead_request_complete(req->data, err); | |
153 | } | |
154 | ||
155 | static int crypto_authenc_encrypt(struct aead_request *req) | |
156 | { | |
157 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); | |
158 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); | |
159 | struct ablkcipher_request *abreq = aead_request_ctx(req); | |
160 | int err; | |
161 | ||
162 | ablkcipher_request_set_tfm(abreq, ctx->enc); | |
163 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), | |
164 | crypto_authenc_encrypt_done, req); | |
165 | ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen, | |
166 | req->iv); | |
167 | ||
168 | err = crypto_ablkcipher_encrypt(abreq); | |
169 | if (err) | |
170 | return err; | |
171 | ||
7c3d703f | 172 | return crypto_authenc_genicv(req, CRYPTO_TFM_REQ_MAY_SLEEP); |
3c09f17c HX |
173 | } |
174 | ||
481f34ae HX |
175 | static int crypto_authenc_verify(struct aead_request *req, |
176 | unsigned int cryptlen) | |
3c09f17c HX |
177 | { |
178 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); | |
7c3d703f | 179 | u8 *ohash; |
3c09f17c | 180 | u8 *ihash; |
f347c4fa | 181 | struct scatterlist *src = req->src; |
3c09f17c | 182 | unsigned int authsize; |
3c09f17c | 183 | |
7c3d703f HX |
184 | ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, src, |
185 | cryptlen); | |
186 | if (IS_ERR(ohash)) | |
187 | return PTR_ERR(ohash); | |
3c09f17c | 188 | |
7ba683a6 | 189 | authsize = crypto_aead_authsize(authenc); |
7c3d703f | 190 | ihash = ohash + authsize; |
3c09f17c | 191 | scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0); |
fe70f5df | 192 | return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0; |
3c09f17c HX |
193 | } |
194 | ||
195 | static void crypto_authenc_decrypt_done(struct crypto_async_request *req, | |
196 | int err) | |
197 | { | |
198 | aead_request_complete(req->data, err); | |
199 | } | |
200 | ||
201 | static int crypto_authenc_decrypt(struct aead_request *req) | |
202 | { | |
203 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); | |
204 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); | |
205 | struct ablkcipher_request *abreq = aead_request_ctx(req); | |
481f34ae HX |
206 | unsigned int cryptlen = req->cryptlen; |
207 | unsigned int authsize = crypto_aead_authsize(authenc); | |
3c09f17c HX |
208 | int err; |
209 | ||
481f34ae HX |
210 | if (cryptlen < authsize) |
211 | return -EINVAL; | |
212 | cryptlen -= authsize; | |
213 | ||
214 | err = crypto_authenc_verify(req, cryptlen); | |
3c09f17c HX |
215 | if (err) |
216 | return err; | |
217 | ||
218 | ablkcipher_request_set_tfm(abreq, ctx->enc); | |
219 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), | |
220 | crypto_authenc_decrypt_done, req); | |
481f34ae | 221 | ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, |
3c09f17c HX |
222 | req->iv); |
223 | ||
224 | return crypto_ablkcipher_decrypt(abreq); | |
225 | } | |
226 | ||
227 | static int crypto_authenc_init_tfm(struct crypto_tfm *tfm) | |
228 | { | |
229 | struct crypto_instance *inst = (void *)tfm->__crt_alg; | |
230 | struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst); | |
231 | struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm); | |
232 | struct crypto_hash *auth; | |
233 | struct crypto_ablkcipher *enc; | |
3c09f17c HX |
234 | int err; |
235 | ||
236 | auth = crypto_spawn_hash(&ictx->auth); | |
237 | if (IS_ERR(auth)) | |
238 | return PTR_ERR(auth); | |
239 | ||
3c09f17c HX |
240 | enc = crypto_spawn_ablkcipher(&ictx->enc); |
241 | err = PTR_ERR(enc); | |
242 | if (IS_ERR(enc)) | |
243 | goto err_free_hash; | |
244 | ||
245 | ctx->auth = auth; | |
246 | ctx->enc = enc; | |
247 | tfm->crt_aead.reqsize = max_t(unsigned int, | |
248 | (crypto_hash_alignmask(auth) & | |
249 | ~(crypto_tfm_ctx_alignment() - 1)) + | |
7ba683a6 | 250 | crypto_hash_digestsize(auth) * 2, |
3c09f17c HX |
251 | sizeof(struct ablkcipher_request) + |
252 | crypto_ablkcipher_reqsize(enc)); | |
253 | ||
254 | spin_lock_init(&ctx->auth_lock); | |
255 | ||
256 | return 0; | |
257 | ||
258 | err_free_hash: | |
259 | crypto_free_hash(auth); | |
260 | return err; | |
261 | } | |
262 | ||
263 | static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm) | |
264 | { | |
265 | struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm); | |
266 | ||
267 | crypto_free_hash(ctx->auth); | |
268 | crypto_free_ablkcipher(ctx->enc); | |
269 | } | |
270 | ||
271 | static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb) | |
272 | { | |
273 | struct crypto_instance *inst; | |
274 | struct crypto_alg *auth; | |
275 | struct crypto_alg *enc; | |
276 | struct authenc_instance_ctx *ctx; | |
3c09f17c HX |
277 | int err; |
278 | ||
279 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD); | |
280 | if (err) | |
281 | return ERR_PTR(err); | |
282 | ||
283 | auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, | |
284 | CRYPTO_ALG_TYPE_HASH_MASK); | |
285 | if (IS_ERR(auth)) | |
286 | return ERR_PTR(PTR_ERR(auth)); | |
287 | ||
7ba683a6 | 288 | enc = crypto_attr_alg(tb[2], CRYPTO_ALG_TYPE_BLKCIPHER, |
332f8840 | 289 | CRYPTO_ALG_TYPE_BLKCIPHER_MASK); |
3c09f17c HX |
290 | inst = ERR_PTR(PTR_ERR(enc)); |
291 | if (IS_ERR(enc)) | |
292 | goto out_put_auth; | |
293 | ||
3c09f17c HX |
294 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
295 | err = -ENOMEM; | |
296 | if (!inst) | |
297 | goto out_put_enc; | |
298 | ||
299 | err = -ENAMETOOLONG; | |
300 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, | |
e236d4a8 HX |
301 | "authenc(%s,%s)", auth->cra_name, enc->cra_name) >= |
302 | CRYPTO_MAX_ALG_NAME) | |
3c09f17c HX |
303 | goto err_free_inst; |
304 | ||
305 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, | |
e236d4a8 HX |
306 | "authenc(%s,%s)", auth->cra_driver_name, |
307 | enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) | |
3c09f17c HX |
308 | goto err_free_inst; |
309 | ||
310 | ctx = crypto_instance_ctx(inst); | |
3c09f17c HX |
311 | |
312 | err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK); | |
313 | if (err) | |
314 | goto err_free_inst; | |
315 | ||
316 | err = crypto_init_spawn(&ctx->enc, enc, inst, CRYPTO_ALG_TYPE_MASK); | |
317 | if (err) | |
318 | goto err_drop_auth; | |
319 | ||
320 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC; | |
321 | inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority; | |
322 | inst->alg.cra_blocksize = enc->cra_blocksize; | |
e29bc6ad | 323 | inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask; |
3c09f17c HX |
324 | inst->alg.cra_type = &crypto_aead_type; |
325 | ||
c2c61f51 | 326 | inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize; |
7ba683a6 HX |
327 | inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ? |
328 | auth->cra_hash.digestsize : | |
329 | auth->cra_digest.dia_digestsize; | |
3c09f17c HX |
330 | |
331 | inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx); | |
332 | ||
333 | inst->alg.cra_init = crypto_authenc_init_tfm; | |
334 | inst->alg.cra_exit = crypto_authenc_exit_tfm; | |
335 | ||
336 | inst->alg.cra_aead.setkey = crypto_authenc_setkey; | |
337 | inst->alg.cra_aead.encrypt = crypto_authenc_encrypt; | |
338 | inst->alg.cra_aead.decrypt = crypto_authenc_decrypt; | |
339 | ||
340 | out: | |
341 | crypto_mod_put(enc); | |
342 | out_put_auth: | |
343 | crypto_mod_put(auth); | |
344 | return inst; | |
345 | ||
346 | err_drop_auth: | |
347 | crypto_drop_spawn(&ctx->auth); | |
348 | err_free_inst: | |
349 | kfree(inst); | |
350 | out_put_enc: | |
351 | inst = ERR_PTR(err); | |
352 | goto out; | |
353 | } | |
354 | ||
355 | static void crypto_authenc_free(struct crypto_instance *inst) | |
356 | { | |
357 | struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst); | |
358 | ||
359 | crypto_drop_spawn(&ctx->enc); | |
360 | crypto_drop_spawn(&ctx->auth); | |
361 | kfree(inst); | |
362 | } | |
363 | ||
364 | static struct crypto_template crypto_authenc_tmpl = { | |
365 | .name = "authenc", | |
366 | .alloc = crypto_authenc_alloc, | |
367 | .free = crypto_authenc_free, | |
368 | .module = THIS_MODULE, | |
369 | }; | |
370 | ||
371 | static int __init crypto_authenc_module_init(void) | |
372 | { | |
373 | return crypto_register_template(&crypto_authenc_tmpl); | |
374 | } | |
375 | ||
376 | static void __exit crypto_authenc_module_exit(void) | |
377 | { | |
378 | crypto_unregister_template(&crypto_authenc_tmpl); | |
379 | } | |
380 | ||
381 | module_init(crypto_authenc_module_init); | |
382 | module_exit(crypto_authenc_module_exit); | |
383 | ||
384 | MODULE_LICENSE("GPL"); | |
385 | MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec"); |