[CRYPTO] aead: Create default givcipher instances
[safe/jmp/linux-2.6] / crypto / aead.c
1 /*
2  * AEAD: Authenticated Encryption with Associated Data
3  * 
4  * This file provides API support for AEAD algorithms.
5  *
6  * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
7  *
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.
12  *
13  */
14
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>
23
24 #include "internal.h"
25
26 static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
27                             unsigned int keylen)
28 {
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;
34
35         absize = keylen + alignmask;
36         buffer = kmalloc(absize, GFP_ATOMIC);
37         if (!buffer)
38                 return -ENOMEM;
39
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;
46 }
47
48 static int setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
49 {
50         struct aead_alg *aead = crypto_aead_alg(tfm);
51         unsigned long alignmask = crypto_aead_alignmask(tfm);
52
53         if ((unsigned long)key & alignmask)
54                 return setkey_unaligned(tfm, key, keylen);
55
56         return aead->setkey(tfm, key, keylen);
57 }
58
59 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
60 {
61         struct aead_tfm *crt = crypto_aead_crt(tfm);
62         int err;
63
64         if (authsize > crypto_aead_alg(tfm)->maxauthsize)
65                 return -EINVAL;
66
67         if (crypto_aead_alg(tfm)->setauthsize) {
68                 err = crypto_aead_alg(tfm)->setauthsize(crt->base, authsize);
69                 if (err)
70                         return err;
71         }
72
73         crypto_aead_crt(crt->base)->authsize = authsize;
74         crt->authsize = authsize;
75         return 0;
76 }
77 EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
78
79 static unsigned int crypto_aead_ctxsize(struct crypto_alg *alg, u32 type,
80                                         u32 mask)
81 {
82         return alg->cra_ctxsize;
83 }
84
85 static int no_givcrypt(struct aead_givcrypt_request *req)
86 {
87         return -ENOSYS;
88 }
89
90 static int crypto_init_aead_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
91 {
92         struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
93         struct aead_tfm *crt = &tfm->crt_aead;
94
95         if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
96                 return -EINVAL;
97
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;
107
108         return 0;
109 }
110
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)
114 {
115         struct aead_alg *aead = &alg->cra_aead;
116
117         seq_printf(m, "type         : aead\n");
118         seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
119         seq_printf(m, "ivsize       : %u\n", aead->ivsize);
120         seq_printf(m, "maxauthsize  : %u\n", aead->maxauthsize);
121         seq_printf(m, "geniv        : %s\n", aead->geniv ?: "<built-in>");
122 }
123
124 const struct crypto_type crypto_aead_type = {
125         .ctxsize = crypto_aead_ctxsize,
126         .init = crypto_init_aead_ops,
127 #ifdef CONFIG_PROC_FS
128         .show = crypto_aead_show,
129 #endif
130 };
131 EXPORT_SYMBOL_GPL(crypto_aead_type);
132
133 static int aead_null_givencrypt(struct aead_givcrypt_request *req)
134 {
135         return crypto_aead_encrypt(&req->areq);
136 }
137
138 static int aead_null_givdecrypt(struct aead_givcrypt_request *req)
139 {
140         return crypto_aead_decrypt(&req->areq);
141 }
142
143 static int crypto_init_nivaead_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
144 {
145         struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
146         struct aead_tfm *crt = &tfm->crt_aead;
147
148         if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
149                 return -EINVAL;
150
151         crt->setkey = setkey;
152         crt->encrypt = alg->encrypt;
153         crt->decrypt = alg->decrypt;
154         if (!alg->ivsize) {
155                 crt->givencrypt = aead_null_givencrypt;
156                 crt->givdecrypt = aead_null_givdecrypt;
157         }
158         crt->base = __crypto_aead_cast(tfm);
159         crt->ivsize = alg->ivsize;
160         crt->authsize = alg->maxauthsize;
161
162         return 0;
163 }
164
165 static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
166         __attribute__ ((unused));
167 static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
168 {
169         struct aead_alg *aead = &alg->cra_aead;
170
171         seq_printf(m, "type         : nivaead\n");
172         seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
173         seq_printf(m, "ivsize       : %u\n", aead->ivsize);
174         seq_printf(m, "maxauthsize  : %u\n", aead->maxauthsize);
175         seq_printf(m, "geniv        : %s\n", aead->geniv);
176 }
177
178 const struct crypto_type crypto_nivaead_type = {
179         .ctxsize = crypto_aead_ctxsize,
180         .init = crypto_init_nivaead_ops,
181 #ifdef CONFIG_PROC_FS
182         .show = crypto_nivaead_show,
183 #endif
184 };
185 EXPORT_SYMBOL_GPL(crypto_nivaead_type);
186
187 static int crypto_grab_nivaead(struct crypto_aead_spawn *spawn,
188                                const char *name, u32 type, u32 mask)
189 {
190         struct crypto_alg *alg;
191         int err;
192
193         type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
194         type |= CRYPTO_ALG_TYPE_AEAD;
195         mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV;
196
197         alg = crypto_alg_mod_lookup(name, type, mask);
198         if (IS_ERR(alg))
199                 return PTR_ERR(alg);
200
201         err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
202         crypto_mod_put(alg);
203         return err;
204 }
205
206 struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl,
207                                          struct rtattr **tb, u32 type,
208                                          u32 mask)
209 {
210         const char *name;
211         struct crypto_aead_spawn *spawn;
212         struct crypto_attr_type *algt;
213         struct crypto_instance *inst;
214         struct crypto_alg *alg;
215         int err;
216
217         algt = crypto_get_attr_type(tb);
218         err = PTR_ERR(algt);
219         if (IS_ERR(algt))
220                 return ERR_PTR(err);
221
222         if ((algt->type ^ (CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV)) &
223             algt->mask)
224                 return ERR_PTR(-EINVAL);
225
226         name = crypto_attr_alg_name(tb[1]);
227         err = PTR_ERR(name);
228         if (IS_ERR(name))
229                 return ERR_PTR(err);
230
231         inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
232         if (!inst)
233                 return ERR_PTR(-ENOMEM);
234
235         spawn = crypto_instance_ctx(inst);
236
237         /* Ignore async algorithms if necessary. */
238         mask |= crypto_requires_sync(algt->type, algt->mask);
239
240         crypto_set_aead_spawn(spawn, inst);
241         err = crypto_grab_nivaead(spawn, name, type, mask);
242         if (err)
243                 goto err_free_inst;
244
245         alg = crypto_aead_spawn_alg(spawn);
246
247         err = -EINVAL;
248         if (!alg->cra_aead.ivsize)
249                 goto err_drop_alg;
250
251         /*
252          * This is only true if we're constructing an algorithm with its
253          * default IV generator.  For the default generator we elide the
254          * template name and double-check the IV generator.
255          */
256         if (algt->mask & CRYPTO_ALG_GENIV) {
257                 if (strcmp(tmpl->name, alg->cra_aead.geniv))
258                         goto err_drop_alg;
259
260                 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
261                 memcpy(inst->alg.cra_driver_name, alg->cra_driver_name,
262                        CRYPTO_MAX_ALG_NAME);
263         } else {
264                 err = -ENAMETOOLONG;
265                 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
266                              "%s(%s)", tmpl->name, alg->cra_name) >=
267                     CRYPTO_MAX_ALG_NAME)
268                         goto err_drop_alg;
269                 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
270                              "%s(%s)", tmpl->name, alg->cra_driver_name) >=
271                     CRYPTO_MAX_ALG_NAME)
272                         goto err_drop_alg;
273         }
274
275         inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV;
276         inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
277         inst->alg.cra_priority = alg->cra_priority;
278         inst->alg.cra_blocksize = alg->cra_blocksize;
279         inst->alg.cra_alignmask = alg->cra_alignmask;
280         inst->alg.cra_type = &crypto_aead_type;
281
282         inst->alg.cra_aead.ivsize = alg->cra_aead.ivsize;
283         inst->alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize;
284         inst->alg.cra_aead.geniv = alg->cra_aead.geniv;
285
286         inst->alg.cra_aead.setkey = alg->cra_aead.setkey;
287         inst->alg.cra_aead.setauthsize = alg->cra_aead.setauthsize;
288         inst->alg.cra_aead.encrypt = alg->cra_aead.encrypt;
289         inst->alg.cra_aead.decrypt = alg->cra_aead.decrypt;
290
291 out:
292         return inst;
293
294 err_drop_alg:
295         crypto_drop_aead(spawn);
296 err_free_inst:
297         kfree(inst);
298         inst = ERR_PTR(err);
299         goto out;
300 }
301 EXPORT_SYMBOL_GPL(aead_geniv_alloc);
302
303 void aead_geniv_free(struct crypto_instance *inst)
304 {
305         crypto_drop_aead(crypto_instance_ctx(inst));
306         kfree(inst);
307 }
308 EXPORT_SYMBOL_GPL(aead_geniv_free);
309
310 int aead_geniv_init(struct crypto_tfm *tfm)
311 {
312         struct crypto_instance *inst = (void *)tfm->__crt_alg;
313         struct crypto_aead *aead;
314
315         aead = crypto_spawn_aead(crypto_instance_ctx(inst));
316         if (IS_ERR(aead))
317                 return PTR_ERR(aead);
318
319         tfm->crt_aead.base = aead;
320         tfm->crt_aead.reqsize += crypto_aead_reqsize(aead);
321
322         return 0;
323 }
324 EXPORT_SYMBOL_GPL(aead_geniv_init);
325
326 void aead_geniv_exit(struct crypto_tfm *tfm)
327 {
328         crypto_free_aead(tfm->crt_aead.base);
329 }
330 EXPORT_SYMBOL_GPL(aead_geniv_exit);
331
332 static int crypto_nivaead_default(struct crypto_alg *alg, u32 type, u32 mask)
333 {
334         struct rtattr *tb[3];
335         struct {
336                 struct rtattr attr;
337                 struct crypto_attr_type data;
338         } ptype;
339         struct {
340                 struct rtattr attr;
341                 struct crypto_attr_alg data;
342         } palg;
343         struct crypto_template *tmpl;
344         struct crypto_instance *inst;
345         struct crypto_alg *larval;
346         const char *geniv;
347         int err;
348
349         larval = crypto_larval_lookup(alg->cra_driver_name,
350                                       CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV,
351                                       CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
352         err = PTR_ERR(larval);
353         if (IS_ERR(larval))
354                 goto out;
355
356         err = -EAGAIN;
357         if (!crypto_is_larval(larval))
358                 goto drop_larval;
359
360         ptype.attr.rta_len = sizeof(ptype);
361         ptype.attr.rta_type = CRYPTOA_TYPE;
362         ptype.data.type = type | CRYPTO_ALG_GENIV;
363         /* GENIV tells the template that we're making a default geniv. */
364         ptype.data.mask = mask | CRYPTO_ALG_GENIV;
365         tb[0] = &ptype.attr;
366
367         palg.attr.rta_len = sizeof(palg);
368         palg.attr.rta_type = CRYPTOA_ALG;
369         /* Must use the exact name to locate ourselves. */
370         memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
371         tb[1] = &palg.attr;
372
373         tb[2] = NULL;
374
375         geniv = alg->cra_aead.geniv;
376
377         tmpl = crypto_lookup_template(geniv);
378         err = -ENOENT;
379         if (!tmpl)
380                 goto kill_larval;
381
382         inst = tmpl->alloc(tb);
383         err = PTR_ERR(inst);
384         if (IS_ERR(inst))
385                 goto put_tmpl;
386
387         if ((err = crypto_register_instance(tmpl, inst))) {
388                 tmpl->free(inst);
389                 goto put_tmpl;
390         }
391
392         /* Redo the lookup to use the instance we just registered. */
393         err = -EAGAIN;
394
395 put_tmpl:
396         crypto_tmpl_put(tmpl);
397 kill_larval:
398         crypto_larval_kill(larval);
399 drop_larval:
400         crypto_mod_put(larval);
401 out:
402         crypto_mod_put(alg);
403         return err;
404 }
405
406 static struct crypto_alg *crypto_lookup_aead(const char *name, u32 type,
407                                              u32 mask)
408 {
409         struct crypto_alg *alg;
410
411         alg = crypto_alg_mod_lookup(name, type, mask);
412         if (IS_ERR(alg))
413                 return alg;
414
415         if (alg->cra_type == &crypto_aead_type)
416                 return alg;
417
418         if (!alg->cra_aead.ivsize)
419                 return alg;
420
421         return ERR_PTR(crypto_nivaead_default(alg, type, mask));
422 }
423
424 int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
425                      u32 type, u32 mask)
426 {
427         struct crypto_alg *alg;
428         int err;
429
430         type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
431         type |= CRYPTO_ALG_TYPE_AEAD;
432         mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
433         mask |= CRYPTO_ALG_TYPE_MASK;
434
435         alg = crypto_lookup_aead(name, type, mask);
436         if (IS_ERR(alg))
437                 return PTR_ERR(alg);
438
439         err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
440         crypto_mod_put(alg);
441         return err;
442 }
443 EXPORT_SYMBOL_GPL(crypto_grab_aead);
444
445 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
446 {
447         struct crypto_tfm *tfm;
448         int err;
449
450         type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
451         type |= CRYPTO_ALG_TYPE_AEAD;
452         mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
453         mask |= CRYPTO_ALG_TYPE_MASK;
454
455         for (;;) {
456                 struct crypto_alg *alg;
457
458                 alg = crypto_lookup_aead(alg_name, type, mask);
459                 if (IS_ERR(alg)) {
460                         err = PTR_ERR(alg);
461                         goto err;
462                 }
463
464                 tfm = __crypto_alloc_tfm(alg, type, mask);
465                 if (!IS_ERR(tfm))
466                         return __crypto_aead_cast(tfm);
467
468                 crypto_mod_put(alg);
469                 err = PTR_ERR(tfm);
470
471 err:
472                 if (err != -EAGAIN)
473                         break;
474                 if (signal_pending(current)) {
475                         err = -EINTR;
476                         break;
477                 }
478         }
479
480         return ERR_PTR(err);
481 }
482 EXPORT_SYMBOL_GPL(crypto_alloc_aead);
483
484 MODULE_LICENSE("GPL");
485 MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");