]>
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 | ||
481f34ae HX |
161 | static int crypto_authenc_verify(struct aead_request *req, |
162 | unsigned int cryptlen) | |
3c09f17c HX |
163 | { |
164 | struct crypto_aead *authenc = crypto_aead_reqtfm(req); | |
3c09f17c HX |
165 | struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc); |
166 | struct crypto_hash *auth = ctx->auth; | |
167 | struct hash_desc desc = { | |
168 | .tfm = auth, | |
169 | .flags = aead_request_flags(req), | |
170 | }; | |
171 | u8 *ohash = aead_request_ctx(req); | |
172 | u8 *ihash; | |
f347c4fa | 173 | struct scatterlist *src = req->src; |
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 | 202 | scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0); |
fe70f5df | 203 | return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0; |
3c09f17c HX |
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); | |
481f34ae HX |
217 | unsigned int cryptlen = req->cryptlen; |
218 | unsigned int authsize = crypto_aead_authsize(authenc); | |
3c09f17c HX |
219 | int err; |
220 | ||
481f34ae HX |
221 | if (cryptlen < authsize) |
222 | return -EINVAL; | |
223 | cryptlen -= authsize; | |
224 | ||
225 | err = crypto_authenc_verify(req, cryptlen); | |
3c09f17c HX |
226 | if (err) |
227 | return err; | |
228 | ||
229 | ablkcipher_request_set_tfm(abreq, ctx->enc); | |
230 | ablkcipher_request_set_callback(abreq, aead_request_flags(req), | |
231 | crypto_authenc_decrypt_done, req); | |
481f34ae | 232 | ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen, |
3c09f17c HX |
233 | req->iv); |
234 | ||
235 | return crypto_ablkcipher_decrypt(abreq); | |
236 | } | |
237 | ||
238 | static int crypto_authenc_init_tfm(struct crypto_tfm *tfm) | |
239 | { | |
240 | struct crypto_instance *inst = (void *)tfm->__crt_alg; | |
241 | struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst); | |
242 | struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm); | |
243 | struct crypto_hash *auth; | |
244 | struct crypto_ablkcipher *enc; | |
3c09f17c HX |
245 | int err; |
246 | ||
247 | auth = crypto_spawn_hash(&ictx->auth); | |
248 | if (IS_ERR(auth)) | |
249 | return PTR_ERR(auth); | |
250 | ||
3c09f17c HX |
251 | enc = crypto_spawn_ablkcipher(&ictx->enc); |
252 | err = PTR_ERR(enc); | |
253 | if (IS_ERR(enc)) | |
254 | goto err_free_hash; | |
255 | ||
256 | ctx->auth = auth; | |
257 | ctx->enc = enc; | |
258 | tfm->crt_aead.reqsize = max_t(unsigned int, | |
259 | (crypto_hash_alignmask(auth) & | |
260 | ~(crypto_tfm_ctx_alignment() - 1)) + | |
7ba683a6 | 261 | crypto_hash_digestsize(auth) * 2, |
3c09f17c HX |
262 | sizeof(struct ablkcipher_request) + |
263 | crypto_ablkcipher_reqsize(enc)); | |
264 | ||
265 | spin_lock_init(&ctx->auth_lock); | |
266 | ||
267 | return 0; | |
268 | ||
269 | err_free_hash: | |
270 | crypto_free_hash(auth); | |
271 | return err; | |
272 | } | |
273 | ||
274 | static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm) | |
275 | { | |
276 | struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm); | |
277 | ||
278 | crypto_free_hash(ctx->auth); | |
279 | crypto_free_ablkcipher(ctx->enc); | |
280 | } | |
281 | ||
282 | static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb) | |
283 | { | |
284 | struct crypto_instance *inst; | |
285 | struct crypto_alg *auth; | |
286 | struct crypto_alg *enc; | |
287 | struct authenc_instance_ctx *ctx; | |
3c09f17c HX |
288 | int err; |
289 | ||
290 | err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD); | |
291 | if (err) | |
292 | return ERR_PTR(err); | |
293 | ||
294 | auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH, | |
295 | CRYPTO_ALG_TYPE_HASH_MASK); | |
296 | if (IS_ERR(auth)) | |
297 | return ERR_PTR(PTR_ERR(auth)); | |
298 | ||
7ba683a6 | 299 | enc = crypto_attr_alg(tb[2], CRYPTO_ALG_TYPE_BLKCIPHER, |
332f8840 | 300 | CRYPTO_ALG_TYPE_BLKCIPHER_MASK); |
3c09f17c HX |
301 | inst = ERR_PTR(PTR_ERR(enc)); |
302 | if (IS_ERR(enc)) | |
303 | goto out_put_auth; | |
304 | ||
3c09f17c HX |
305 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
306 | err = -ENOMEM; | |
307 | if (!inst) | |
308 | goto out_put_enc; | |
309 | ||
310 | err = -ENAMETOOLONG; | |
311 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, | |
e236d4a8 HX |
312 | "authenc(%s,%s)", auth->cra_name, enc->cra_name) >= |
313 | CRYPTO_MAX_ALG_NAME) | |
3c09f17c HX |
314 | goto err_free_inst; |
315 | ||
316 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, | |
e236d4a8 HX |
317 | "authenc(%s,%s)", auth->cra_driver_name, |
318 | enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) | |
3c09f17c HX |
319 | goto err_free_inst; |
320 | ||
321 | ctx = crypto_instance_ctx(inst); | |
3c09f17c HX |
322 | |
323 | err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK); | |
324 | if (err) | |
325 | goto err_free_inst; | |
326 | ||
327 | err = crypto_init_spawn(&ctx->enc, enc, inst, CRYPTO_ALG_TYPE_MASK); | |
328 | if (err) | |
329 | goto err_drop_auth; | |
330 | ||
331 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC; | |
332 | inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority; | |
333 | inst->alg.cra_blocksize = enc->cra_blocksize; | |
e29bc6ad | 334 | inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask; |
3c09f17c HX |
335 | inst->alg.cra_type = &crypto_aead_type; |
336 | ||
337 | inst->alg.cra_aead.ivsize = enc->cra_blkcipher.ivsize; | |
7ba683a6 HX |
338 | inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ? |
339 | auth->cra_hash.digestsize : | |
340 | auth->cra_digest.dia_digestsize; | |
3c09f17c HX |
341 | |
342 | inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx); | |
343 | ||
344 | inst->alg.cra_init = crypto_authenc_init_tfm; | |
345 | inst->alg.cra_exit = crypto_authenc_exit_tfm; | |
346 | ||
347 | inst->alg.cra_aead.setkey = crypto_authenc_setkey; | |
348 | inst->alg.cra_aead.encrypt = crypto_authenc_encrypt; | |
349 | inst->alg.cra_aead.decrypt = crypto_authenc_decrypt; | |
350 | ||
351 | out: | |
352 | crypto_mod_put(enc); | |
353 | out_put_auth: | |
354 | crypto_mod_put(auth); | |
355 | return inst; | |
356 | ||
357 | err_drop_auth: | |
358 | crypto_drop_spawn(&ctx->auth); | |
359 | err_free_inst: | |
360 | kfree(inst); | |
361 | out_put_enc: | |
362 | inst = ERR_PTR(err); | |
363 | goto out; | |
364 | } | |
365 | ||
366 | static void crypto_authenc_free(struct crypto_instance *inst) | |
367 | { | |
368 | struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst); | |
369 | ||
370 | crypto_drop_spawn(&ctx->enc); | |
371 | crypto_drop_spawn(&ctx->auth); | |
372 | kfree(inst); | |
373 | } | |
374 | ||
375 | static struct crypto_template crypto_authenc_tmpl = { | |
376 | .name = "authenc", | |
377 | .alloc = crypto_authenc_alloc, | |
378 | .free = crypto_authenc_free, | |
379 | .module = THIS_MODULE, | |
380 | }; | |
381 | ||
382 | static int __init crypto_authenc_module_init(void) | |
383 | { | |
384 | return crypto_register_template(&crypto_authenc_tmpl); | |
385 | } | |
386 | ||
387 | static void __exit crypto_authenc_module_exit(void) | |
388 | { | |
389 | crypto_unregister_template(&crypto_authenc_tmpl); | |
390 | } | |
391 | ||
392 | module_init(crypto_authenc_module_init); | |
393 | module_exit(crypto_authenc_module_exit); | |
394 | ||
395 | MODULE_LICENSE("GPL"); | |
396 | MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec"); |