replace some function names
[linux-2.6/zen-sources.git] / crypto / aead.c
blob3a6f3f52c7c71911ac6cd7506493fb24ccd8eb50
1 /*
2 * AEAD: Authenticated Encryption with Associated Data
3 *
4 * This file provides API support for AEAD algorithms.
6 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
15 #include <crypto/internal/aead.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/seq_file.h>
24 #include "internal.h"
26 static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
27 unsigned int keylen)
29 struct aead_alg *aead = crypto_aead_alg(tfm);
30 unsigned long alignmask = crypto_aead_alignmask(tfm);
31 int ret;
32 u8 *buffer, *alignbuffer;
33 unsigned long absize;
35 absize = keylen + alignmask;
36 buffer = kmalloc(absize, GFP_ATOMIC);
37 if (!buffer)
38 return -ENOMEM;
40 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
41 memcpy(alignbuffer, key, keylen);
42 ret = aead->setkey(tfm, alignbuffer, keylen);
43 memset(alignbuffer, 0, keylen);
44 kfree(buffer);
45 return ret;
48 static int setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
50 struct aead_alg *aead = crypto_aead_alg(tfm);
51 unsigned long alignmask = crypto_aead_alignmask(tfm);
53 if ((unsigned long)key & alignmask)
54 return setkey_unaligned(tfm, key, keylen);
56 return aead->setkey(tfm, key, keylen);
59 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
61 struct aead_tfm *crt = crypto_aead_crt(tfm);
62 int err;
64 if (authsize > crypto_aead_alg(tfm)->maxauthsize)
65 return -EINVAL;
67 if (crypto_aead_alg(tfm)->setauthsize) {
68 err = crypto_aead_alg(tfm)->setauthsize(crt->base, authsize);
69 if (err)
70 return err;
73 crypto_aead_crt(crt->base)->authsize = authsize;
74 crt->authsize = authsize;
75 return 0;
77 EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
79 static unsigned int crypto_aead_ctxsize(struct crypto_alg *alg, u32 type,
80 u32 mask)
82 return alg->cra_ctxsize;
85 static int no_givcrypt(struct aead_givcrypt_request *req)
87 return -ENOSYS;
90 static int crypto_init_aead_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
92 struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
93 struct aead_tfm *crt = &tfm->crt_aead;
95 if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
96 return -EINVAL;
98 crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
99 alg->setkey : setkey;
100 crt->encrypt = alg->encrypt;
101 crt->decrypt = alg->decrypt;
102 crt->givencrypt = alg->givencrypt ?: no_givcrypt;
103 crt->givdecrypt = alg->givdecrypt ?: no_givcrypt;
104 crt->base = __crypto_aead_cast(tfm);
105 crt->ivsize = alg->ivsize;
106 crt->authsize = alg->maxauthsize;
108 return 0;
111 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
112 __attribute__ ((unused));
113 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
115 struct aead_alg *aead = &alg->cra_aead;
117 seq_printf(m, "type : aead\n");
118 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
119 "yes" : "no");
120 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
121 seq_printf(m, "ivsize : %u\n", aead->ivsize);
122 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
123 seq_printf(m, "geniv : %s\n", aead->geniv ?: "<built-in>");
126 const struct crypto_type crypto_aead_type = {
127 .ctxsize = crypto_aead_ctxsize,
128 .init = crypto_init_aead_ops,
129 #ifdef CONFIG_PROC_FS
130 .show = crypto_aead_show,
131 #endif
133 EXPORT_SYMBOL_GPL(crypto_aead_type);
135 static int aead_null_givencrypt(struct aead_givcrypt_request *req)
137 return crypto_aead_encrypt(&req->areq);
140 static int aead_null_givdecrypt(struct aead_givcrypt_request *req)
142 return crypto_aead_decrypt(&req->areq);
145 static int crypto_init_nivaead_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
147 struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
148 struct aead_tfm *crt = &tfm->crt_aead;
150 if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
151 return -EINVAL;
153 crt->setkey = setkey;
154 crt->encrypt = alg->encrypt;
155 crt->decrypt = alg->decrypt;
156 if (!alg->ivsize) {
157 crt->givencrypt = aead_null_givencrypt;
158 crt->givdecrypt = aead_null_givdecrypt;
160 crt->base = __crypto_aead_cast(tfm);
161 crt->ivsize = alg->ivsize;
162 crt->authsize = alg->maxauthsize;
164 return 0;
167 static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
168 __attribute__ ((unused));
169 static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
171 struct aead_alg *aead = &alg->cra_aead;
173 seq_printf(m, "type : nivaead\n");
174 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
175 "yes" : "no");
176 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
177 seq_printf(m, "ivsize : %u\n", aead->ivsize);
178 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
179 seq_printf(m, "geniv : %s\n", aead->geniv);
182 const struct crypto_type crypto_nivaead_type = {
183 .ctxsize = crypto_aead_ctxsize,
184 .init = crypto_init_nivaead_ops,
185 #ifdef CONFIG_PROC_FS
186 .show = crypto_nivaead_show,
187 #endif
189 EXPORT_SYMBOL_GPL(crypto_nivaead_type);
191 static int crypto_grab_nivaead(struct crypto_aead_spawn *spawn,
192 const char *name, u32 type, u32 mask)
194 struct crypto_alg *alg;
195 int err;
197 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
198 type |= CRYPTO_ALG_TYPE_AEAD;
199 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV;
201 alg = crypto_alg_mod_lookup(name, type, mask);
202 if (IS_ERR(alg))
203 return PTR_ERR(alg);
205 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
206 crypto_mod_put(alg);
207 return err;
210 struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl,
211 struct rtattr **tb, u32 type,
212 u32 mask)
214 const char *name;
215 struct crypto_aead_spawn *spawn;
216 struct crypto_attr_type *algt;
217 struct crypto_instance *inst;
218 struct crypto_alg *alg;
219 int err;
221 algt = crypto_get_attr_type(tb);
222 err = PTR_ERR(algt);
223 if (IS_ERR(algt))
224 return ERR_PTR(err);
226 if ((algt->type ^ (CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV)) &
227 algt->mask)
228 return ERR_PTR(-EINVAL);
230 name = crypto_attr_alg_name(tb[1]);
231 err = PTR_ERR(name);
232 if (IS_ERR(name))
233 return ERR_PTR(err);
235 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
236 if (!inst)
237 return ERR_PTR(-ENOMEM);
239 spawn = crypto_instance_ctx(inst);
241 /* Ignore async algorithms if necessary. */
242 mask |= crypto_requires_sync(algt->type, algt->mask);
244 crypto_set_aead_spawn(spawn, inst);
245 err = crypto_grab_nivaead(spawn, name, type, mask);
246 if (err)
247 goto err_free_inst;
249 alg = crypto_aead_spawn_alg(spawn);
251 err = -EINVAL;
252 if (!alg->cra_aead.ivsize)
253 goto err_drop_alg;
256 * This is only true if we're constructing an algorithm with its
257 * default IV generator. For the default generator we elide the
258 * template name and double-check the IV generator.
260 if (algt->mask & CRYPTO_ALG_GENIV) {
261 if (strcmp(tmpl->name, alg->cra_aead.geniv))
262 goto err_drop_alg;
264 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
265 memcpy(inst->alg.cra_driver_name, alg->cra_driver_name,
266 CRYPTO_MAX_ALG_NAME);
267 } else {
268 err = -ENAMETOOLONG;
269 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
270 "%s(%s)", tmpl->name, alg->cra_name) >=
271 CRYPTO_MAX_ALG_NAME)
272 goto err_drop_alg;
273 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
274 "%s(%s)", tmpl->name, alg->cra_driver_name) >=
275 CRYPTO_MAX_ALG_NAME)
276 goto err_drop_alg;
279 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV;
280 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
281 inst->alg.cra_priority = alg->cra_priority;
282 inst->alg.cra_blocksize = alg->cra_blocksize;
283 inst->alg.cra_alignmask = alg->cra_alignmask;
284 inst->alg.cra_type = &crypto_aead_type;
286 inst->alg.cra_aead.ivsize = alg->cra_aead.ivsize;
287 inst->alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize;
288 inst->alg.cra_aead.geniv = alg->cra_aead.geniv;
290 inst->alg.cra_aead.setkey = alg->cra_aead.setkey;
291 inst->alg.cra_aead.setauthsize = alg->cra_aead.setauthsize;
292 inst->alg.cra_aead.encrypt = alg->cra_aead.encrypt;
293 inst->alg.cra_aead.decrypt = alg->cra_aead.decrypt;
295 out:
296 return inst;
298 err_drop_alg:
299 crypto_drop_aead(spawn);
300 err_free_inst:
301 kfree(inst);
302 inst = ERR_PTR(err);
303 goto out;
305 EXPORT_SYMBOL_GPL(aead_geniv_alloc);
307 void aead_geniv_free(struct crypto_instance *inst)
309 crypto_drop_aead(crypto_instance_ctx(inst));
310 kfree(inst);
312 EXPORT_SYMBOL_GPL(aead_geniv_free);
314 int aead_geniv_init(struct crypto_tfm *tfm)
316 struct crypto_instance *inst = (void *)tfm->__crt_alg;
317 struct crypto_aead *aead;
319 aead = crypto_spawn_aead(crypto_instance_ctx(inst));
320 if (IS_ERR(aead))
321 return PTR_ERR(aead);
323 tfm->crt_aead.base = aead;
324 tfm->crt_aead.reqsize += crypto_aead_reqsize(aead);
326 return 0;
328 EXPORT_SYMBOL_GPL(aead_geniv_init);
330 void aead_geniv_exit(struct crypto_tfm *tfm)
332 crypto_free_aead(tfm->crt_aead.base);
334 EXPORT_SYMBOL_GPL(aead_geniv_exit);
336 static int crypto_nivaead_default(struct crypto_alg *alg, u32 type, u32 mask)
338 struct rtattr *tb[3];
339 struct {
340 struct rtattr attr;
341 struct crypto_attr_type data;
342 } ptype;
343 struct {
344 struct rtattr attr;
345 struct crypto_attr_alg data;
346 } palg;
347 struct crypto_template *tmpl;
348 struct crypto_instance *inst;
349 struct crypto_alg *larval;
350 const char *geniv;
351 int err;
353 larval = crypto_larval_lookup(alg->cra_driver_name,
354 CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV,
355 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
356 err = PTR_ERR(larval);
357 if (IS_ERR(larval))
358 goto out;
360 err = -EAGAIN;
361 if (!crypto_is_larval(larval))
362 goto drop_larval;
364 ptype.attr.rta_len = sizeof(ptype);
365 ptype.attr.rta_type = CRYPTOA_TYPE;
366 ptype.data.type = type | CRYPTO_ALG_GENIV;
367 /* GENIV tells the template that we're making a default geniv. */
368 ptype.data.mask = mask | CRYPTO_ALG_GENIV;
369 tb[0] = &ptype.attr;
371 palg.attr.rta_len = sizeof(palg);
372 palg.attr.rta_type = CRYPTOA_ALG;
373 /* Must use the exact name to locate ourselves. */
374 memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
375 tb[1] = &palg.attr;
377 tb[2] = NULL;
379 geniv = alg->cra_aead.geniv;
381 tmpl = crypto_lookup_template(geniv);
382 err = -ENOENT;
383 if (!tmpl)
384 goto kill_larval;
386 inst = tmpl->alloc(tb);
387 err = PTR_ERR(inst);
388 if (IS_ERR(inst))
389 goto put_tmpl;
391 if ((err = crypto_register_instance(tmpl, inst))) {
392 tmpl->free(inst);
393 goto put_tmpl;
396 /* Redo the lookup to use the instance we just registered. */
397 err = -EAGAIN;
399 put_tmpl:
400 crypto_tmpl_put(tmpl);
401 kill_larval:
402 crypto_larval_kill(larval);
403 drop_larval:
404 crypto_mod_put(larval);
405 out:
406 crypto_mod_put(alg);
407 return err;
410 static struct crypto_alg *crypto_lookup_aead(const char *name, u32 type,
411 u32 mask)
413 struct crypto_alg *alg;
415 alg = crypto_alg_mod_lookup(name, type, mask);
416 if (IS_ERR(alg))
417 return alg;
419 if (alg->cra_type == &crypto_aead_type)
420 return alg;
422 if (!alg->cra_aead.ivsize)
423 return alg;
425 return ERR_PTR(crypto_nivaead_default(alg, type, mask));
428 int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
429 u32 type, u32 mask)
431 struct crypto_alg *alg;
432 int err;
434 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
435 type |= CRYPTO_ALG_TYPE_AEAD;
436 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
437 mask |= CRYPTO_ALG_TYPE_MASK;
439 alg = crypto_lookup_aead(name, type, mask);
440 if (IS_ERR(alg))
441 return PTR_ERR(alg);
443 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
444 crypto_mod_put(alg);
445 return err;
447 EXPORT_SYMBOL_GPL(crypto_grab_aead);
449 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
451 struct crypto_tfm *tfm;
452 int err;
454 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
455 type |= CRYPTO_ALG_TYPE_AEAD;
456 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
457 mask |= CRYPTO_ALG_TYPE_MASK;
459 for (;;) {
460 struct crypto_alg *alg;
462 alg = crypto_lookup_aead(alg_name, type, mask);
463 if (IS_ERR(alg)) {
464 err = PTR_ERR(alg);
465 goto err;
468 tfm = __crypto_alloc_tfm(alg, type, mask);
469 if (!IS_ERR(tfm))
470 return __crypto_aead_cast(tfm);
472 crypto_mod_put(alg);
473 err = PTR_ERR(tfm);
475 err:
476 if (err != -EAGAIN)
477 break;
478 if (signal_pending(current)) {
479 err = -EINTR;
480 break;
484 return ERR_PTR(err);
486 EXPORT_SYMBOL_GPL(crypto_alloc_aead);
488 MODULE_LICENSE("GPL");
489 MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");