pidns: Place under CONFIG_EXPERIMENTAL
[linux-2.6/kmemtrace.git] / crypto / authenc.c
blob0b29a6ae673d257baba2eb5b1a17460e28204f4a
1 /*
2 * Authenc: Simple AEAD wrapper for IPsec
4 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
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.
13 #include <crypto/algapi.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
21 #include "scatterwalk.h"
23 struct authenc_instance_ctx {
24 struct crypto_spawn auth;
25 struct crypto_spawn enc;
27 unsigned int authsize;
28 unsigned int enckeylen;
31 struct crypto_authenc_ctx {
32 spinlock_t auth_lock;
33 struct crypto_hash *auth;
34 struct crypto_ablkcipher *enc;
37 static int crypto_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
38 unsigned int keylen)
40 struct authenc_instance_ctx *ictx =
41 crypto_instance_ctx(crypto_aead_alg_instance(authenc));
42 unsigned int enckeylen = ictx->enckeylen;
43 unsigned int authkeylen;
44 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
45 struct crypto_hash *auth = ctx->auth;
46 struct crypto_ablkcipher *enc = ctx->enc;
47 int err = -EINVAL;
49 if (keylen < enckeylen) {
50 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
51 goto out;
53 authkeylen = keylen - enckeylen;
55 crypto_hash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
56 crypto_hash_set_flags(auth, crypto_aead_get_flags(authenc) &
57 CRYPTO_TFM_REQ_MASK);
58 err = crypto_hash_setkey(auth, key, authkeylen);
59 crypto_aead_set_flags(authenc, crypto_hash_get_flags(auth) &
60 CRYPTO_TFM_RES_MASK);
62 if (err)
63 goto out;
65 crypto_ablkcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
66 crypto_ablkcipher_set_flags(enc, crypto_aead_get_flags(authenc) &
67 CRYPTO_TFM_REQ_MASK);
68 err = crypto_ablkcipher_setkey(enc, key + authkeylen, enckeylen);
69 crypto_aead_set_flags(authenc, crypto_ablkcipher_get_flags(enc) &
70 CRYPTO_TFM_RES_MASK);
72 out:
73 return err;
76 static int crypto_authenc_hash(struct aead_request *req)
78 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
79 struct authenc_instance_ctx *ictx =
80 crypto_instance_ctx(crypto_aead_alg_instance(authenc));
81 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
82 struct crypto_hash *auth = ctx->auth;
83 struct hash_desc desc = {
84 .tfm = auth,
86 u8 *hash = aead_request_ctx(req);
87 struct scatterlist *dst;
88 unsigned int cryptlen;
89 int err;
91 hash = (u8 *)ALIGN((unsigned long)hash + crypto_hash_alignmask(auth),
92 crypto_hash_alignmask(auth) + 1);
94 spin_lock_bh(&ctx->auth_lock);
95 err = crypto_hash_init(&desc);
96 if (err)
97 goto auth_unlock;
99 err = crypto_hash_update(&desc, req->assoc, req->assoclen);
100 if (err)
101 goto auth_unlock;
103 cryptlen = req->cryptlen;
104 dst = req->dst;
105 err = crypto_hash_update(&desc, dst, cryptlen);
106 if (err)
107 goto auth_unlock;
109 err = crypto_hash_final(&desc, hash);
110 auth_unlock:
111 spin_unlock_bh(&ctx->auth_lock);
113 if (err)
114 return err;
116 scatterwalk_map_and_copy(hash, dst, cryptlen, ictx->authsize, 1);
117 return 0;
120 static void crypto_authenc_encrypt_done(struct crypto_async_request *req,
121 int err)
123 if (!err)
124 err = crypto_authenc_hash(req->data);
126 aead_request_complete(req->data, err);
129 static int crypto_authenc_encrypt(struct aead_request *req)
131 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
132 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
133 struct ablkcipher_request *abreq = aead_request_ctx(req);
134 int err;
136 ablkcipher_request_set_tfm(abreq, ctx->enc);
137 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
138 crypto_authenc_encrypt_done, req);
139 ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen,
140 req->iv);
142 err = crypto_ablkcipher_encrypt(abreq);
143 if (err)
144 return err;
146 return crypto_authenc_hash(req);
149 static int crypto_authenc_verify(struct aead_request *req)
151 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
152 struct authenc_instance_ctx *ictx =
153 crypto_instance_ctx(crypto_aead_alg_instance(authenc));
154 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
155 struct crypto_hash *auth = ctx->auth;
156 struct hash_desc desc = {
157 .tfm = auth,
158 .flags = aead_request_flags(req),
160 u8 *ohash = aead_request_ctx(req);
161 u8 *ihash;
162 struct scatterlist *src;
163 unsigned int cryptlen;
164 unsigned int authsize;
165 int err;
167 ohash = (u8 *)ALIGN((unsigned long)ohash + crypto_hash_alignmask(auth),
168 crypto_hash_alignmask(auth) + 1);
169 ihash = ohash + crypto_hash_digestsize(auth);
171 spin_lock_bh(&ctx->auth_lock);
172 err = crypto_hash_init(&desc);
173 if (err)
174 goto auth_unlock;
176 err = crypto_hash_update(&desc, req->assoc, req->assoclen);
177 if (err)
178 goto auth_unlock;
180 cryptlen = req->cryptlen;
181 src = req->src;
182 err = crypto_hash_update(&desc, src, cryptlen);
183 if (err)
184 goto auth_unlock;
186 err = crypto_hash_final(&desc, ohash);
187 auth_unlock:
188 spin_unlock_bh(&ctx->auth_lock);
190 if (err)
191 return err;
193 authsize = ictx->authsize;
194 scatterwalk_map_and_copy(ihash, src, cryptlen, authsize, 0);
195 return memcmp(ihash, ohash, authsize) ? -EINVAL : 0;
198 static void crypto_authenc_decrypt_done(struct crypto_async_request *req,
199 int err)
201 aead_request_complete(req->data, err);
204 static int crypto_authenc_decrypt(struct aead_request *req)
206 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
207 struct crypto_authenc_ctx *ctx = crypto_aead_ctx(authenc);
208 struct ablkcipher_request *abreq = aead_request_ctx(req);
209 int err;
211 err = crypto_authenc_verify(req);
212 if (err)
213 return err;
215 ablkcipher_request_set_tfm(abreq, ctx->enc);
216 ablkcipher_request_set_callback(abreq, aead_request_flags(req),
217 crypto_authenc_decrypt_done, req);
218 ablkcipher_request_set_crypt(abreq, req->src, req->dst, req->cryptlen,
219 req->iv);
221 return crypto_ablkcipher_decrypt(abreq);
224 static int crypto_authenc_init_tfm(struct crypto_tfm *tfm)
226 struct crypto_instance *inst = (void *)tfm->__crt_alg;
227 struct authenc_instance_ctx *ictx = crypto_instance_ctx(inst);
228 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
229 struct crypto_hash *auth;
230 struct crypto_ablkcipher *enc;
231 unsigned int digestsize;
232 int err;
234 auth = crypto_spawn_hash(&ictx->auth);
235 if (IS_ERR(auth))
236 return PTR_ERR(auth);
238 err = -EINVAL;
239 digestsize = crypto_hash_digestsize(auth);
240 if (ictx->authsize > digestsize)
241 goto err_free_hash;
243 enc = crypto_spawn_ablkcipher(&ictx->enc);
244 err = PTR_ERR(enc);
245 if (IS_ERR(enc))
246 goto err_free_hash;
248 ctx->auth = auth;
249 ctx->enc = enc;
250 tfm->crt_aead.reqsize = max_t(unsigned int,
251 (crypto_hash_alignmask(auth) &
252 ~(crypto_tfm_ctx_alignment() - 1)) +
253 digestsize * 2,
254 sizeof(struct ablkcipher_request) +
255 crypto_ablkcipher_reqsize(enc));
257 spin_lock_init(&ctx->auth_lock);
259 return 0;
261 err_free_hash:
262 crypto_free_hash(auth);
263 return err;
266 static void crypto_authenc_exit_tfm(struct crypto_tfm *tfm)
268 struct crypto_authenc_ctx *ctx = crypto_tfm_ctx(tfm);
270 crypto_free_hash(ctx->auth);
271 crypto_free_ablkcipher(ctx->enc);
274 static struct crypto_instance *crypto_authenc_alloc(struct rtattr **tb)
276 struct crypto_instance *inst;
277 struct crypto_alg *auth;
278 struct crypto_alg *enc;
279 struct authenc_instance_ctx *ctx;
280 unsigned int authsize;
281 unsigned int enckeylen;
282 int err;
284 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD);
285 if (err)
286 return ERR_PTR(err);
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));
293 err = crypto_attr_u32(tb[2], &authsize);
294 inst = ERR_PTR(err);
295 if (err)
296 goto out_put_auth;
298 enc = crypto_attr_alg(tb[3], CRYPTO_ALG_TYPE_BLKCIPHER,
299 CRYPTO_ALG_TYPE_MASK);
300 inst = ERR_PTR(PTR_ERR(enc));
301 if (IS_ERR(enc))
302 goto out_put_auth;
304 err = crypto_attr_u32(tb[4], &enckeylen);
305 if (err)
306 goto out_put_enc;
308 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
309 err = -ENOMEM;
310 if (!inst)
311 goto out_put_enc;
313 err = -ENAMETOOLONG;
314 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
315 "authenc(%s,%u,%s,%u)", auth->cra_name, authsize,
316 enc->cra_name, enckeylen) >= CRYPTO_MAX_ALG_NAME)
317 goto err_free_inst;
319 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
320 "authenc(%s,%u,%s,%u)", auth->cra_driver_name,
321 authsize, enc->cra_driver_name, enckeylen) >=
322 CRYPTO_MAX_ALG_NAME)
323 goto err_free_inst;
325 ctx = crypto_instance_ctx(inst);
326 ctx->authsize = authsize;
327 ctx->enckeylen = enckeylen;
329 err = crypto_init_spawn(&ctx->auth, auth, inst, CRYPTO_ALG_TYPE_MASK);
330 if (err)
331 goto err_free_inst;
333 err = crypto_init_spawn(&ctx->enc, enc, inst, CRYPTO_ALG_TYPE_MASK);
334 if (err)
335 goto err_drop_auth;
337 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
338 inst->alg.cra_priority = enc->cra_priority * 10 + auth->cra_priority;
339 inst->alg.cra_blocksize = enc->cra_blocksize;
340 inst->alg.cra_alignmask = max(auth->cra_alignmask, enc->cra_alignmask);
341 inst->alg.cra_type = &crypto_aead_type;
343 inst->alg.cra_aead.ivsize = enc->cra_blkcipher.ivsize;
344 inst->alg.cra_aead.authsize = authsize;
346 inst->alg.cra_ctxsize = sizeof(struct crypto_authenc_ctx);
348 inst->alg.cra_init = crypto_authenc_init_tfm;
349 inst->alg.cra_exit = crypto_authenc_exit_tfm;
351 inst->alg.cra_aead.setkey = crypto_authenc_setkey;
352 inst->alg.cra_aead.encrypt = crypto_authenc_encrypt;
353 inst->alg.cra_aead.decrypt = crypto_authenc_decrypt;
355 out:
356 crypto_mod_put(enc);
357 out_put_auth:
358 crypto_mod_put(auth);
359 return inst;
361 err_drop_auth:
362 crypto_drop_spawn(&ctx->auth);
363 err_free_inst:
364 kfree(inst);
365 out_put_enc:
366 inst = ERR_PTR(err);
367 goto out;
370 static void crypto_authenc_free(struct crypto_instance *inst)
372 struct authenc_instance_ctx *ctx = crypto_instance_ctx(inst);
374 crypto_drop_spawn(&ctx->enc);
375 crypto_drop_spawn(&ctx->auth);
376 kfree(inst);
379 static struct crypto_template crypto_authenc_tmpl = {
380 .name = "authenc",
381 .alloc = crypto_authenc_alloc,
382 .free = crypto_authenc_free,
383 .module = THIS_MODULE,
386 static int __init crypto_authenc_module_init(void)
388 return crypto_register_template(&crypto_authenc_tmpl);
391 static void __exit crypto_authenc_module_exit(void)
393 crypto_unregister_template(&crypto_authenc_tmpl);
396 module_init(crypto_authenc_module_init);
397 module_exit(crypto_authenc_module_exit);
399 MODULE_LICENSE("GPL");
400 MODULE_DESCRIPTION("Simple AEAD wrapper for IPsec");