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