2 * AEAD: Authenticated Encryption with Associated Data
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)
15 #include <crypto/internal/aead.h>
16 #include <crypto/scatterwalk.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/seq_file.h>
25 #include <linux/cryptouser.h>
26 #include <net/netlink.h>
30 static int aead_null_givencrypt(struct aead_givcrypt_request *req);
31 static int aead_null_givdecrypt(struct aead_givcrypt_request *req);
33 static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
36 unsigned long alignmask = crypto_aead_alignmask(tfm);
38 u8 *buffer, *alignbuffer;
41 absize = keylen + alignmask;
42 buffer = kmalloc(absize, GFP_ATOMIC);
46 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
47 memcpy(alignbuffer, key, keylen);
48 ret = tfm->setkey(tfm, alignbuffer, keylen);
49 memset(alignbuffer, 0, keylen);
54 int crypto_aead_setkey(struct crypto_aead *tfm,
55 const u8 *key, unsigned int keylen)
57 unsigned long alignmask = crypto_aead_alignmask(tfm);
61 if ((unsigned long)key & alignmask)
62 return setkey_unaligned(tfm, key, keylen);
64 return tfm->setkey(tfm, key, keylen);
66 EXPORT_SYMBOL_GPL(crypto_aead_setkey);
68 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
72 if (authsize > tfm->maxauthsize)
75 if (tfm->setauthsize) {
76 err = tfm->setauthsize(tfm->child, authsize);
81 tfm->child->authsize = authsize;
82 tfm->authsize = authsize;
85 EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
87 struct aead_old_request {
88 struct scatterlist srcbuf[2];
89 struct scatterlist dstbuf[2];
90 struct aead_request subreq;
93 unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
95 return tfm->reqsize + sizeof(struct aead_old_request);
97 EXPORT_SYMBOL_GPL(crypto_aead_reqsize);
99 static int old_crypt(struct aead_request *req,
100 int (*crypt)(struct aead_request *req))
102 struct aead_old_request *nreq = aead_request_ctx(req);
103 struct crypto_aead *aead = crypto_aead_reqtfm(req);
104 struct scatterlist *src, *dst;
109 src = scatterwalk_ffwd(nreq->srcbuf, req->src,
110 req->assoclen + req->cryptoff);
111 dst = scatterwalk_ffwd(nreq->dstbuf, req->dst,
112 req->assoclen + req->cryptoff);
114 aead_request_set_tfm(&nreq->subreq, aead);
115 aead_request_set_callback(&nreq->subreq, aead_request_flags(req),
116 req->base.complete, req->base.data);
117 aead_request_set_crypt(&nreq->subreq, src, dst, req->cryptlen,
119 aead_request_set_assoc(&nreq->subreq, req->src, req->assoclen);
121 return crypt(&nreq->subreq);
124 static int old_encrypt(struct aead_request *req)
126 struct crypto_aead *aead = crypto_aead_reqtfm(req);
127 struct old_aead_alg *alg = crypto_old_aead_alg(aead);
129 return old_crypt(req, alg->encrypt);
132 static int old_decrypt(struct aead_request *req)
134 struct crypto_aead *aead = crypto_aead_reqtfm(req);
135 struct old_aead_alg *alg = crypto_old_aead_alg(aead);
137 return old_crypt(req, alg->decrypt);
140 static int no_givcrypt(struct aead_givcrypt_request *req)
145 static int crypto_old_aead_init_tfm(struct crypto_tfm *tfm)
147 struct old_aead_alg *alg = &tfm->__crt_alg->cra_aead;
148 struct crypto_aead *crt = __crypto_aead_cast(tfm);
150 if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
153 crt->setkey = alg->setkey;
154 crt->setauthsize = alg->setauthsize;
155 crt->encrypt = old_encrypt;
156 crt->decrypt = old_decrypt;
158 crt->givencrypt = alg->givencrypt ?: no_givcrypt;
159 crt->givdecrypt = alg->givdecrypt ?: no_givcrypt;
161 crt->givencrypt = aead_null_givencrypt;
162 crt->givdecrypt = aead_null_givdecrypt;
164 crt->child = __crypto_aead_cast(tfm);
165 crt->ivsize = alg->ivsize;
166 crt->maxauthsize = alg->maxauthsize;
167 crt->authsize = alg->maxauthsize;
172 static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
174 struct crypto_aead *aead = __crypto_aead_cast(tfm);
175 struct aead_alg *alg = crypto_aead_alg(aead);
177 if (crypto_old_aead_alg(aead)->encrypt)
178 return crypto_old_aead_init_tfm(tfm);
180 aead->setkey = alg->setkey;
181 aead->setauthsize = alg->setauthsize;
182 aead->encrypt = alg->encrypt;
183 aead->decrypt = alg->decrypt;
184 aead->child = __crypto_aead_cast(tfm);
185 aead->ivsize = alg->ivsize;
186 aead->maxauthsize = alg->maxauthsize;
187 aead->authsize = alg->maxauthsize;
193 static int crypto_old_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
195 struct crypto_report_aead raead;
196 struct old_aead_alg *aead = &alg->cra_aead;
198 strncpy(raead.type, "aead", sizeof(raead.type));
199 strncpy(raead.geniv, aead->geniv ?: "<built-in>", sizeof(raead.geniv));
201 raead.blocksize = alg->cra_blocksize;
202 raead.maxauthsize = aead->maxauthsize;
203 raead.ivsize = aead->ivsize;
205 if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD,
206 sizeof(struct crypto_report_aead), &raead))
207 goto nla_put_failure;
214 static int crypto_old_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
220 static void crypto_old_aead_show(struct seq_file *m, struct crypto_alg *alg)
221 __attribute__ ((unused));
222 static void crypto_old_aead_show(struct seq_file *m, struct crypto_alg *alg)
224 struct old_aead_alg *aead = &alg->cra_aead;
226 seq_printf(m, "type : aead\n");
227 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
229 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
230 seq_printf(m, "ivsize : %u\n", aead->ivsize);
231 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
232 seq_printf(m, "geniv : %s\n", aead->geniv ?: "<built-in>");
235 const struct crypto_type crypto_aead_type = {
236 .extsize = crypto_alg_extsize,
237 .init_tfm = crypto_aead_init_tfm,
238 #ifdef CONFIG_PROC_FS
239 .show = crypto_old_aead_show,
241 .report = crypto_old_aead_report,
242 .lookup = crypto_lookup_aead,
243 .maskclear = ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV),
244 .maskset = CRYPTO_ALG_TYPE_MASK,
245 .type = CRYPTO_ALG_TYPE_AEAD,
246 .tfmsize = offsetof(struct crypto_aead, base),
248 EXPORT_SYMBOL_GPL(crypto_aead_type);
251 static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
253 struct crypto_report_aead raead;
254 struct aead_alg *aead = container_of(alg, struct aead_alg, base);
256 strncpy(raead.type, "aead", sizeof(raead.type));
257 strncpy(raead.geniv, "<none>", sizeof(raead.geniv));
259 raead.blocksize = alg->cra_blocksize;
260 raead.maxauthsize = aead->maxauthsize;
261 raead.ivsize = aead->ivsize;
263 if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD,
264 sizeof(struct crypto_report_aead), &raead))
265 goto nla_put_failure;
272 static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
278 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
279 __attribute__ ((unused));
280 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
282 struct aead_alg *aead = container_of(alg, struct aead_alg, base);
284 seq_printf(m, "type : aead\n");
285 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
287 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
288 seq_printf(m, "ivsize : %u\n", aead->ivsize);
289 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
290 seq_printf(m, "geniv : <none>\n");
293 static const struct crypto_type crypto_new_aead_type = {
294 .extsize = crypto_alg_extsize,
295 .init_tfm = crypto_aead_init_tfm,
296 #ifdef CONFIG_PROC_FS
297 .show = crypto_aead_show,
299 .report = crypto_aead_report,
300 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
301 .maskset = CRYPTO_ALG_TYPE_MASK,
302 .type = CRYPTO_ALG_TYPE_AEAD,
303 .tfmsize = offsetof(struct crypto_aead, base),
306 static int aead_null_givencrypt(struct aead_givcrypt_request *req)
308 return crypto_aead_encrypt(&req->areq);
311 static int aead_null_givdecrypt(struct aead_givcrypt_request *req)
313 return crypto_aead_decrypt(&req->areq);
317 static int crypto_nivaead_report(struct sk_buff *skb, struct crypto_alg *alg)
319 struct crypto_report_aead raead;
320 struct old_aead_alg *aead = &alg->cra_aead;
322 strncpy(raead.type, "nivaead", sizeof(raead.type));
323 strncpy(raead.geniv, aead->geniv, sizeof(raead.geniv));
325 raead.blocksize = alg->cra_blocksize;
326 raead.maxauthsize = aead->maxauthsize;
327 raead.ivsize = aead->ivsize;
329 if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD,
330 sizeof(struct crypto_report_aead), &raead))
331 goto nla_put_failure;
338 static int crypto_nivaead_report(struct sk_buff *skb, struct crypto_alg *alg)
345 static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
346 __attribute__ ((unused));
347 static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
349 struct old_aead_alg *aead = &alg->cra_aead;
351 seq_printf(m, "type : nivaead\n");
352 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
354 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
355 seq_printf(m, "ivsize : %u\n", aead->ivsize);
356 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
357 seq_printf(m, "geniv : %s\n", aead->geniv);
360 const struct crypto_type crypto_nivaead_type = {
361 .extsize = crypto_alg_extsize,
362 .init_tfm = crypto_aead_init_tfm,
363 #ifdef CONFIG_PROC_FS
364 .show = crypto_nivaead_show,
366 .report = crypto_nivaead_report,
367 .maskclear = ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV),
368 .maskset = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV,
369 .type = CRYPTO_ALG_TYPE_AEAD,
370 .tfmsize = offsetof(struct crypto_aead, base),
372 EXPORT_SYMBOL_GPL(crypto_nivaead_type);
374 static int crypto_grab_nivaead(struct crypto_aead_spawn *spawn,
375 const char *name, u32 type, u32 mask)
377 spawn->base.frontend = &crypto_nivaead_type;
378 return crypto_grab_spawn(&spawn->base, name, type, mask);
381 struct crypto_instance *aead_geniv_alloc(struct crypto_template *tmpl,
382 struct rtattr **tb, u32 type,
386 struct crypto_aead_spawn *spawn;
387 struct crypto_attr_type *algt;
388 struct crypto_instance *inst;
389 struct crypto_alg *alg;
392 algt = crypto_get_attr_type(tb);
394 return ERR_CAST(algt);
396 if ((algt->type ^ (CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV)) &
398 return ERR_PTR(-EINVAL);
400 name = crypto_attr_alg_name(tb[1]);
402 return ERR_CAST(name);
404 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
406 return ERR_PTR(-ENOMEM);
408 spawn = crypto_instance_ctx(inst);
410 /* Ignore async algorithms if necessary. */
411 mask |= crypto_requires_sync(algt->type, algt->mask);
413 crypto_set_aead_spawn(spawn, inst);
414 err = crypto_grab_nivaead(spawn, name, type, mask);
418 alg = crypto_aead_spawn_alg(spawn);
421 if (!alg->cra_aead.ivsize)
425 * This is only true if we're constructing an algorithm with its
426 * default IV generator. For the default generator we elide the
427 * template name and double-check the IV generator.
429 if (algt->mask & CRYPTO_ALG_GENIV) {
430 if (strcmp(tmpl->name, alg->cra_aead.geniv))
433 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
434 memcpy(inst->alg.cra_driver_name, alg->cra_driver_name,
435 CRYPTO_MAX_ALG_NAME);
438 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
439 "%s(%s)", tmpl->name, alg->cra_name) >=
442 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
443 "%s(%s)", tmpl->name, alg->cra_driver_name) >=
448 inst->alg.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV;
449 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
450 inst->alg.cra_priority = alg->cra_priority;
451 inst->alg.cra_blocksize = alg->cra_blocksize;
452 inst->alg.cra_alignmask = alg->cra_alignmask;
453 inst->alg.cra_type = &crypto_aead_type;
455 inst->alg.cra_aead.ivsize = alg->cra_aead.ivsize;
456 inst->alg.cra_aead.maxauthsize = alg->cra_aead.maxauthsize;
457 inst->alg.cra_aead.geniv = alg->cra_aead.geniv;
459 inst->alg.cra_aead.setkey = alg->cra_aead.setkey;
460 inst->alg.cra_aead.setauthsize = alg->cra_aead.setauthsize;
461 inst->alg.cra_aead.encrypt = alg->cra_aead.encrypt;
462 inst->alg.cra_aead.decrypt = alg->cra_aead.decrypt;
468 crypto_drop_aead(spawn);
474 EXPORT_SYMBOL_GPL(aead_geniv_alloc);
476 void aead_geniv_free(struct crypto_instance *inst)
478 crypto_drop_aead(crypto_instance_ctx(inst));
481 EXPORT_SYMBOL_GPL(aead_geniv_free);
483 int aead_geniv_init(struct crypto_tfm *tfm)
485 struct crypto_instance *inst = (void *)tfm->__crt_alg;
486 struct crypto_aead *child;
487 struct crypto_aead *aead;
489 aead = __crypto_aead_cast(tfm);
491 child = crypto_spawn_aead(crypto_instance_ctx(inst));
493 return PTR_ERR(child);
496 aead->reqsize += crypto_aead_reqsize(child);
500 EXPORT_SYMBOL_GPL(aead_geniv_init);
502 void aead_geniv_exit(struct crypto_tfm *tfm)
504 crypto_free_aead(__crypto_aead_cast(tfm)->child);
506 EXPORT_SYMBOL_GPL(aead_geniv_exit);
508 static int crypto_nivaead_default(struct crypto_alg *alg, u32 type, u32 mask)
510 struct rtattr *tb[3];
513 struct crypto_attr_type data;
517 struct crypto_attr_alg data;
519 struct crypto_template *tmpl;
520 struct crypto_instance *inst;
521 struct crypto_alg *larval;
525 larval = crypto_larval_lookup(alg->cra_driver_name,
526 CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV,
527 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
528 err = PTR_ERR(larval);
533 if (!crypto_is_larval(larval))
536 ptype.attr.rta_len = sizeof(ptype);
537 ptype.attr.rta_type = CRYPTOA_TYPE;
538 ptype.data.type = type | CRYPTO_ALG_GENIV;
539 /* GENIV tells the template that we're making a default geniv. */
540 ptype.data.mask = mask | CRYPTO_ALG_GENIV;
543 palg.attr.rta_len = sizeof(palg);
544 palg.attr.rta_type = CRYPTOA_ALG;
545 /* Must use the exact name to locate ourselves. */
546 memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
551 geniv = alg->cra_aead.geniv;
553 tmpl = crypto_lookup_template(geniv);
558 inst = tmpl->alloc(tb);
563 err = crypto_register_instance(tmpl, inst);
569 /* Redo the lookup to use the instance we just registered. */
573 crypto_tmpl_put(tmpl);
575 crypto_larval_kill(larval);
577 crypto_mod_put(larval);
583 struct crypto_alg *crypto_lookup_aead(const char *name, u32 type, u32 mask)
585 struct crypto_alg *alg;
587 alg = crypto_alg_mod_lookup(name, type, mask);
591 if (alg->cra_type == &crypto_aead_type)
594 if (!alg->cra_aead.ivsize)
598 alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
599 mask & ~CRYPTO_ALG_TESTED);
603 if (alg->cra_type == &crypto_aead_type) {
604 if (~alg->cra_flags & (type ^ ~mask) & CRYPTO_ALG_TESTED) {
606 alg = ERR_PTR(-ENOENT);
611 BUG_ON(!alg->cra_aead.ivsize);
613 return ERR_PTR(crypto_nivaead_default(alg, type, mask));
615 EXPORT_SYMBOL_GPL(crypto_lookup_aead);
617 int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
620 spawn->base.frontend = &crypto_aead_type;
621 return crypto_grab_spawn(&spawn->base, name, type, mask);
623 EXPORT_SYMBOL_GPL(crypto_grab_aead);
625 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
627 return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask);
629 EXPORT_SYMBOL_GPL(crypto_alloc_aead);
631 static int aead_prepare_alg(struct aead_alg *alg)
633 struct crypto_alg *base = &alg->base;
635 if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
638 base->cra_type = &crypto_new_aead_type;
639 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
640 base->cra_flags |= CRYPTO_ALG_TYPE_AEAD;
645 int crypto_register_aead(struct aead_alg *alg)
647 struct crypto_alg *base = &alg->base;
650 err = aead_prepare_alg(alg);
654 return crypto_register_alg(base);
656 EXPORT_SYMBOL_GPL(crypto_register_aead);
658 int crypto_unregister_aead(struct aead_alg *alg)
660 return crypto_unregister_alg(&alg->base);
662 EXPORT_SYMBOL_GPL(crypto_unregister_aead);
664 int aead_register_instance(struct crypto_template *tmpl,
665 struct aead_instance *inst)
669 err = aead_prepare_alg(&inst->alg);
673 return crypto_register_instance(tmpl, aead_crypto_instance(inst));
675 EXPORT_SYMBOL_GPL(aead_register_instance);
677 MODULE_LICENSE("GPL");
678 MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");