[CRYPTO] authenc: Use crypto_grab_skcipher
[deliverable/linux.git] / crypto / authenc.c
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/internal/skcipher.h>
14 #include <crypto/authenc.h>
15 #include <crypto/scatterwalk.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23
24 struct authenc_instance_ctx {
25 struct crypto_spawn auth;
26 struct crypto_skcipher_spawn enc;
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 {
38 unsigned int authkeylen;
39 unsigned int enckeylen;
40 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
41 struct crypto_hash *auth = ctx->auth;
42 struct crypto_ablkcipher *enc = ctx->enc;
43 struct rtattr *rta = (void *)key;
44 struct crypto_authenc_key_param *param;
45 int err = -EINVAL;
46
47 if (!RTA_OK(rta, keylen))
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
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;
84
85 badkey:
86 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
87 goto out;
88 }
89
90 static u8 *crypto_authenc_hash(struct aead_request *req, unsigned int flags,
91 struct scatterlist *cipher,
92 unsigned int cryptlen)
93 {
94 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
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,
99 .flags = aead_request_flags(req) & flags,
100 };
101 u8 *hash = aead_request_ctx(req);
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
116 err = crypto_hash_update(&desc, cipher, 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_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);
140
141 scatterwalk_map_and_copy(hash, dst, cryptlen,
142 crypto_aead_authsize(authenc), 1);
143 return 0;
144 }
145
146 static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
147 int err)
148 {
149 if (!err)
150 err = crypto_authenc_genicv(req->data, 0);
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
172 return crypto_authenc_genicv(req, CRYPTO_TFM_REQ_MAY_SLEEP);
173 }
174
175 static int crypto_authenc_verify(struct aead_request *req,
176 unsigned int cryptlen)
177 {
178 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
179 u8 *ohash;
180 u8 *ihash;
181 struct scatterlist *src = req->src;
182 unsigned int authsize;
183
184 ohash = crypto_authenc_hash(req, CRYPTO_TFM_REQ_MAY_SLEEP, src,
185 cryptlen);
186 if (IS_ERR(ohash))
187 return PTR_ERR(ohash);
188
189 authsize = crypto_aead_authsize(authenc);
190 ihash = ohash + authsize;
191 scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
192 return memcmp(ihash, ohash, authsize) ? -EBADMSG: 0;
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);
206 unsigned int cryptlen = req->cryptlen;
207 unsigned int authsize = crypto_aead_authsize(authenc);
208 int err;
209
210 if (cryptlen < authsize)
211 return -EINVAL;
212 cryptlen -= authsize;
213
214 err = crypto_authenc_verify(req, cryptlen);
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);
221 ablkcipher_request_set_crypt(abreq, req->src, req->dst, cryptlen,
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;
234 int err;
235
236 auth = crypto_spawn_hash(&ictx->auth);
237 if (IS_ERR(auth))
238 return PTR_ERR(auth);
239
240 enc = crypto_spawn_skcipher(&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)) +
250 crypto_hash_digestsize(auth) * 2,
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_attr_type *algt;
274 struct crypto_instance *inst;
275 struct crypto_alg *auth;
276 struct crypto_alg *enc;
277 struct authenc_instance_ctx *ctx;
278 const char *enc_name;
279 int err;
280
281 algt = crypto_get_attr_type(tb);
282 err = PTR_ERR(algt);
283 if (IS_ERR(algt))
284 return ERR_PTR(err);
285
286 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
287 return ERR_PTR(-EINVAL);
288
289 auth = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
290 CRYPTO_ALG_TYPE_HASH_MASK);
291 if (IS_ERR(auth))
292 return ERR_PTR(PTR_ERR(auth));
293
294 enc_name = crypto_attr_alg_name(tb[2]);
295 err = PTR_ERR(enc_name);
296 if (IS_ERR(enc_name))
297 goto out_put_auth;
298
299 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
300 err = -ENOMEM;
301 if (!inst)
302 goto out_put_auth;
303
304 ctx = crypto_instance_ctx(inst);
305
306 err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
307 if (err)
308 goto err_free_inst;
309
310 crypto_set_skcipher_spawn(&ctx->enc, inst);
311 err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
312 crypto_requires_sync(algt->type,
313 algt->mask));
314 if (err)
315 goto err_drop_auth;
316
317 enc = crypto_skcipher_spawn_alg(&ctx->enc);
318
319 err = -ENAMETOOLONG;
320 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
321 "authenc(%s,%s)", auth->cra_name, enc->cra_name) >=
322 CRYPTO_MAX_ALG_NAME)
323 goto err_drop_enc;
324
325 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
326 "authenc(%s,%s)", auth->cra_driver_name,
327 enc->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
328 goto err_drop_enc;
329
330 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD;
331 inst->alg.cra_flags |= enc->cra_flags & CRYPTO_ALG_ASYNC;
332 inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
333 inst->alg.cra_blocksize = enc->cra_blocksize;
334 inst->alg.cra_alignmask = auth->cra_alignmask | enc->cra_alignmask;
335 inst->alg.cra_type = &crypto_aead_type;
336
337 inst->alg.cra_aead.ivsize = enc->cra_ablkcipher.ivsize;
338 inst->alg.cra_aead.maxauthsize = auth->cra_type == &crypto_hash_type ?
339 auth->cra_hash.digestsize :
340 auth->cra_digest.dia_digestsize;
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(auth);
353 return inst;
354
355 err_drop_enc:
356 crypto_drop_skcipher(&ctx->enc);
357 err_drop_auth:
358 crypto_drop_spawn(&ctx->auth);
359 err_free_inst:
360 kfree(inst);
361 out_put_auth:
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_skcipher(&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");
This page took 0.063967 seconds and 6 git commands to generate.