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
> crypto_aead_maxauthsize(tfm
))
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
, req
->assoclen
);
110 dst
= scatterwalk_ffwd(nreq
->dstbuf
, req
->dst
, req
->assoclen
);
112 aead_request_set_tfm(&nreq
->subreq
, aead
);
113 aead_request_set_callback(&nreq
->subreq
, aead_request_flags(req
),
114 req
->base
.complete
, req
->base
.data
);
115 aead_request_set_crypt(&nreq
->subreq
, src
, dst
, req
->cryptlen
,
117 aead_request_set_assoc(&nreq
->subreq
, req
->src
, req
->assoclen
);
119 return crypt(&nreq
->subreq
);
122 static int old_encrypt(struct aead_request
*req
)
124 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
125 struct old_aead_alg
*alg
= crypto_old_aead_alg(aead
);
127 return old_crypt(req
, alg
->encrypt
);
130 static int old_decrypt(struct aead_request
*req
)
132 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
133 struct old_aead_alg
*alg
= crypto_old_aead_alg(aead
);
135 return old_crypt(req
, alg
->decrypt
);
138 static int no_givcrypt(struct aead_givcrypt_request
*req
)
143 static int crypto_old_aead_init_tfm(struct crypto_tfm
*tfm
)
145 struct old_aead_alg
*alg
= &tfm
->__crt_alg
->cra_aead
;
146 struct crypto_aead
*crt
= __crypto_aead_cast(tfm
);
148 if (max(alg
->maxauthsize
, alg
->ivsize
) > PAGE_SIZE
/ 8)
151 crt
->setkey
= alg
->setkey
;
152 crt
->setauthsize
= alg
->setauthsize
;
153 crt
->encrypt
= old_encrypt
;
154 crt
->decrypt
= old_decrypt
;
156 crt
->givencrypt
= alg
->givencrypt
?: no_givcrypt
;
157 crt
->givdecrypt
= alg
->givdecrypt
?: no_givcrypt
;
159 crt
->givencrypt
= aead_null_givencrypt
;
160 crt
->givdecrypt
= aead_null_givdecrypt
;
162 crt
->child
= __crypto_aead_cast(tfm
);
163 crt
->authsize
= alg
->maxauthsize
;
168 static int crypto_aead_init_tfm(struct crypto_tfm
*tfm
)
170 struct crypto_aead
*aead
= __crypto_aead_cast(tfm
);
171 struct aead_alg
*alg
= crypto_aead_alg(aead
);
173 if (crypto_old_aead_alg(aead
)->encrypt
)
174 return crypto_old_aead_init_tfm(tfm
);
176 aead
->setkey
= alg
->setkey
;
177 aead
->setauthsize
= alg
->setauthsize
;
178 aead
->encrypt
= alg
->encrypt
;
179 aead
->decrypt
= alg
->decrypt
;
180 aead
->child
= __crypto_aead_cast(tfm
);
181 aead
->authsize
= alg
->maxauthsize
;
187 static int crypto_old_aead_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
189 struct crypto_report_aead raead
;
190 struct old_aead_alg
*aead
= &alg
->cra_aead
;
192 strncpy(raead
.type
, "aead", sizeof(raead
.type
));
193 strncpy(raead
.geniv
, aead
->geniv
?: "<built-in>", sizeof(raead
.geniv
));
195 raead
.blocksize
= alg
->cra_blocksize
;
196 raead
.maxauthsize
= aead
->maxauthsize
;
197 raead
.ivsize
= aead
->ivsize
;
199 if (nla_put(skb
, CRYPTOCFGA_REPORT_AEAD
,
200 sizeof(struct crypto_report_aead
), &raead
))
201 goto nla_put_failure
;
208 static int crypto_old_aead_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
214 static void crypto_old_aead_show(struct seq_file
*m
, struct crypto_alg
*alg
)
215 __attribute__ ((unused
));
216 static void crypto_old_aead_show(struct seq_file
*m
, struct crypto_alg
*alg
)
218 struct old_aead_alg
*aead
= &alg
->cra_aead
;
220 seq_printf(m
, "type : aead\n");
221 seq_printf(m
, "async : %s\n", alg
->cra_flags
& CRYPTO_ALG_ASYNC
?
223 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
224 seq_printf(m
, "ivsize : %u\n", aead
->ivsize
);
225 seq_printf(m
, "maxauthsize : %u\n", aead
->maxauthsize
);
226 seq_printf(m
, "geniv : %s\n", aead
->geniv
?: "<built-in>");
229 const struct crypto_type crypto_aead_type
= {
230 .extsize
= crypto_alg_extsize
,
231 .init_tfm
= crypto_aead_init_tfm
,
232 #ifdef CONFIG_PROC_FS
233 .show
= crypto_old_aead_show
,
235 .report
= crypto_old_aead_report
,
236 .lookup
= crypto_lookup_aead
,
237 .maskclear
= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
),
238 .maskset
= CRYPTO_ALG_TYPE_MASK
,
239 .type
= CRYPTO_ALG_TYPE_AEAD
,
240 .tfmsize
= offsetof(struct crypto_aead
, base
),
242 EXPORT_SYMBOL_GPL(crypto_aead_type
);
245 static int crypto_aead_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
247 struct crypto_report_aead raead
;
248 struct aead_alg
*aead
= container_of(alg
, struct aead_alg
, base
);
250 strncpy(raead
.type
, "aead", sizeof(raead
.type
));
251 strncpy(raead
.geniv
, "<none>", sizeof(raead
.geniv
));
253 raead
.blocksize
= alg
->cra_blocksize
;
254 raead
.maxauthsize
= aead
->maxauthsize
;
255 raead
.ivsize
= aead
->ivsize
;
257 if (nla_put(skb
, CRYPTOCFGA_REPORT_AEAD
,
258 sizeof(struct crypto_report_aead
), &raead
))
259 goto nla_put_failure
;
266 static int crypto_aead_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
272 static void crypto_aead_show(struct seq_file
*m
, struct crypto_alg
*alg
)
273 __attribute__ ((unused
));
274 static void crypto_aead_show(struct seq_file
*m
, struct crypto_alg
*alg
)
276 struct aead_alg
*aead
= container_of(alg
, struct aead_alg
, base
);
278 seq_printf(m
, "type : aead\n");
279 seq_printf(m
, "async : %s\n", alg
->cra_flags
& CRYPTO_ALG_ASYNC
?
281 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
282 seq_printf(m
, "ivsize : %u\n", aead
->ivsize
);
283 seq_printf(m
, "maxauthsize : %u\n", aead
->maxauthsize
);
284 seq_printf(m
, "geniv : <none>\n");
287 static const struct crypto_type crypto_new_aead_type
= {
288 .extsize
= crypto_alg_extsize
,
289 .init_tfm
= crypto_aead_init_tfm
,
290 #ifdef CONFIG_PROC_FS
291 .show
= crypto_aead_show
,
293 .report
= crypto_aead_report
,
294 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
295 .maskset
= CRYPTO_ALG_TYPE_MASK
,
296 .type
= CRYPTO_ALG_TYPE_AEAD
,
297 .tfmsize
= offsetof(struct crypto_aead
, base
),
300 static int aead_null_givencrypt(struct aead_givcrypt_request
*req
)
302 return crypto_aead_encrypt(&req
->areq
);
305 static int aead_null_givdecrypt(struct aead_givcrypt_request
*req
)
307 return crypto_aead_decrypt(&req
->areq
);
311 static int crypto_nivaead_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
313 struct crypto_report_aead raead
;
314 struct old_aead_alg
*aead
= &alg
->cra_aead
;
316 strncpy(raead
.type
, "nivaead", sizeof(raead
.type
));
317 strncpy(raead
.geniv
, aead
->geniv
, sizeof(raead
.geniv
));
319 raead
.blocksize
= alg
->cra_blocksize
;
320 raead
.maxauthsize
= aead
->maxauthsize
;
321 raead
.ivsize
= aead
->ivsize
;
323 if (nla_put(skb
, CRYPTOCFGA_REPORT_AEAD
,
324 sizeof(struct crypto_report_aead
), &raead
))
325 goto nla_put_failure
;
332 static int crypto_nivaead_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
339 static void crypto_nivaead_show(struct seq_file
*m
, struct crypto_alg
*alg
)
340 __attribute__ ((unused
));
341 static void crypto_nivaead_show(struct seq_file
*m
, struct crypto_alg
*alg
)
343 struct old_aead_alg
*aead
= &alg
->cra_aead
;
345 seq_printf(m
, "type : nivaead\n");
346 seq_printf(m
, "async : %s\n", alg
->cra_flags
& CRYPTO_ALG_ASYNC
?
348 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
349 seq_printf(m
, "ivsize : %u\n", aead
->ivsize
);
350 seq_printf(m
, "maxauthsize : %u\n", aead
->maxauthsize
);
351 seq_printf(m
, "geniv : %s\n", aead
->geniv
);
354 const struct crypto_type crypto_nivaead_type
= {
355 .extsize
= crypto_alg_extsize
,
356 .init_tfm
= crypto_aead_init_tfm
,
357 #ifdef CONFIG_PROC_FS
358 .show
= crypto_nivaead_show
,
360 .report
= crypto_nivaead_report
,
361 .maskclear
= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
),
362 .maskset
= CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
,
363 .type
= CRYPTO_ALG_TYPE_AEAD
,
364 .tfmsize
= offsetof(struct crypto_aead
, base
),
366 EXPORT_SYMBOL_GPL(crypto_nivaead_type
);
368 static int crypto_grab_nivaead(struct crypto_aead_spawn
*spawn
,
369 const char *name
, u32 type
, u32 mask
)
371 spawn
->base
.frontend
= &crypto_nivaead_type
;
372 return crypto_grab_spawn(&spawn
->base
, name
, type
, mask
);
375 struct aead_instance
*aead_geniv_alloc(struct crypto_template
*tmpl
,
376 struct rtattr
**tb
, u32 type
, u32 mask
)
379 struct crypto_aead_spawn
*spawn
;
380 struct crypto_attr_type
*algt
;
381 struct aead_instance
*inst
;
382 struct aead_alg
*alg
;
384 unsigned int maxauthsize
;
387 algt
= crypto_get_attr_type(tb
);
389 return ERR_CAST(algt
);
391 if ((algt
->type
^ (CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_GENIV
)) &
393 return ERR_PTR(-EINVAL
);
395 name
= crypto_attr_alg_name(tb
[1]);
397 return ERR_CAST(name
);
399 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
401 return ERR_PTR(-ENOMEM
);
403 spawn
= aead_instance_ctx(inst
);
405 /* Ignore async algorithms if necessary. */
406 mask
|= crypto_requires_sync(algt
->type
, algt
->mask
);
408 crypto_set_aead_spawn(spawn
, aead_crypto_instance(inst
));
409 err
= crypto_grab_nivaead(spawn
, name
, type
, mask
);
413 alg
= crypto_spawn_aead_alg(spawn
);
415 ivsize
= crypto_aead_alg_ivsize(alg
);
416 maxauthsize
= crypto_aead_alg_maxauthsize(alg
);
423 * This is only true if we're constructing an algorithm with its
424 * default IV generator. For the default generator we elide the
425 * template name and double-check the IV generator.
427 if (algt
->mask
& CRYPTO_ALG_GENIV
) {
428 if (!alg
->base
.cra_aead
.encrypt
)
430 if (strcmp(tmpl
->name
, alg
->base
.cra_aead
.geniv
))
433 memcpy(inst
->alg
.base
.cra_name
, alg
->base
.cra_name
,
434 CRYPTO_MAX_ALG_NAME
);
435 memcpy(inst
->alg
.base
.cra_driver_name
,
436 alg
->base
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
);
438 inst
->alg
.base
.cra_flags
= CRYPTO_ALG_TYPE_AEAD
|
440 inst
->alg
.base
.cra_flags
|= alg
->base
.cra_flags
&
442 inst
->alg
.base
.cra_priority
= alg
->base
.cra_priority
;
443 inst
->alg
.base
.cra_blocksize
= alg
->base
.cra_blocksize
;
444 inst
->alg
.base
.cra_alignmask
= alg
->base
.cra_alignmask
;
445 inst
->alg
.base
.cra_type
= &crypto_aead_type
;
447 inst
->alg
.base
.cra_aead
.ivsize
= ivsize
;
448 inst
->alg
.base
.cra_aead
.maxauthsize
= maxauthsize
;
450 inst
->alg
.base
.cra_aead
.setkey
= alg
->base
.cra_aead
.setkey
;
451 inst
->alg
.base
.cra_aead
.setauthsize
=
452 alg
->base
.cra_aead
.setauthsize
;
453 inst
->alg
.base
.cra_aead
.encrypt
= alg
->base
.cra_aead
.encrypt
;
454 inst
->alg
.base
.cra_aead
.decrypt
= alg
->base
.cra_aead
.decrypt
;
460 if (snprintf(inst
->alg
.base
.cra_name
, CRYPTO_MAX_ALG_NAME
,
461 "%s(%s)", tmpl
->name
, alg
->base
.cra_name
) >=
464 if (snprintf(inst
->alg
.base
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
,
465 "%s(%s)", tmpl
->name
, alg
->base
.cra_driver_name
) >=
469 inst
->alg
.base
.cra_flags
= alg
->base
.cra_flags
& CRYPTO_ALG_ASYNC
;
470 inst
->alg
.base
.cra_priority
= alg
->base
.cra_priority
;
471 inst
->alg
.base
.cra_blocksize
= alg
->base
.cra_blocksize
;
472 inst
->alg
.base
.cra_alignmask
= alg
->base
.cra_alignmask
;
474 inst
->alg
.ivsize
= ivsize
;
475 inst
->alg
.maxauthsize
= maxauthsize
;
481 crypto_drop_aead(spawn
);
487 EXPORT_SYMBOL_GPL(aead_geniv_alloc
);
489 void aead_geniv_free(struct aead_instance
*inst
)
491 crypto_drop_aead(aead_instance_ctx(inst
));
494 EXPORT_SYMBOL_GPL(aead_geniv_free
);
496 int aead_geniv_init(struct crypto_tfm
*tfm
)
498 struct crypto_instance
*inst
= (void *)tfm
->__crt_alg
;
499 struct crypto_aead
*child
;
500 struct crypto_aead
*aead
;
502 aead
= __crypto_aead_cast(tfm
);
504 child
= crypto_spawn_aead(crypto_instance_ctx(inst
));
506 return PTR_ERR(child
);
509 aead
->reqsize
+= crypto_aead_reqsize(child
);
513 EXPORT_SYMBOL_GPL(aead_geniv_init
);
515 void aead_geniv_exit(struct crypto_tfm
*tfm
)
517 crypto_free_aead(__crypto_aead_cast(tfm
)->child
);
519 EXPORT_SYMBOL_GPL(aead_geniv_exit
);
521 static int crypto_nivaead_default(struct crypto_alg
*alg
, u32 type
, u32 mask
)
523 struct rtattr
*tb
[3];
526 struct crypto_attr_type data
;
530 struct crypto_attr_alg data
;
532 struct crypto_template
*tmpl
;
533 struct crypto_instance
*inst
;
534 struct crypto_alg
*larval
;
538 larval
= crypto_larval_lookup(alg
->cra_driver_name
,
539 CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_GENIV
,
540 CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
541 err
= PTR_ERR(larval
);
546 if (!crypto_is_larval(larval
))
549 ptype
.attr
.rta_len
= sizeof(ptype
);
550 ptype
.attr
.rta_type
= CRYPTOA_TYPE
;
551 ptype
.data
.type
= type
| CRYPTO_ALG_GENIV
;
552 /* GENIV tells the template that we're making a default geniv. */
553 ptype
.data
.mask
= mask
| CRYPTO_ALG_GENIV
;
556 palg
.attr
.rta_len
= sizeof(palg
);
557 palg
.attr
.rta_type
= CRYPTOA_ALG
;
558 /* Must use the exact name to locate ourselves. */
559 memcpy(palg
.data
.name
, alg
->cra_driver_name
, CRYPTO_MAX_ALG_NAME
);
564 geniv
= alg
->cra_aead
.geniv
;
566 tmpl
= crypto_lookup_template(geniv
);
572 err
= tmpl
->create(tmpl
, tb
);
578 inst
= tmpl
->alloc(tb
);
583 err
= crypto_register_instance(tmpl
, inst
);
590 /* Redo the lookup to use the instance we just registered. */
594 crypto_tmpl_put(tmpl
);
596 crypto_larval_kill(larval
);
598 crypto_mod_put(larval
);
604 struct crypto_alg
*crypto_lookup_aead(const char *name
, u32 type
, u32 mask
)
606 struct crypto_alg
*alg
;
608 alg
= crypto_alg_mod_lookup(name
, type
, mask
);
612 if (alg
->cra_type
== &crypto_aead_type
)
615 if (!alg
->cra_aead
.ivsize
)
619 alg
= crypto_alg_mod_lookup(name
, type
| CRYPTO_ALG_TESTED
,
620 mask
& ~CRYPTO_ALG_TESTED
);
624 if (alg
->cra_type
== &crypto_aead_type
) {
625 if (~alg
->cra_flags
& (type
^ ~mask
) & CRYPTO_ALG_TESTED
) {
627 alg
= ERR_PTR(-ENOENT
);
632 BUG_ON(!alg
->cra_aead
.ivsize
);
634 return ERR_PTR(crypto_nivaead_default(alg
, type
, mask
));
636 EXPORT_SYMBOL_GPL(crypto_lookup_aead
);
638 int crypto_grab_aead(struct crypto_aead_spawn
*spawn
, const char *name
,
641 spawn
->base
.frontend
= &crypto_aead_type
;
642 return crypto_grab_spawn(&spawn
->base
, name
, type
, mask
);
644 EXPORT_SYMBOL_GPL(crypto_grab_aead
);
646 struct crypto_aead
*crypto_alloc_aead(const char *alg_name
, u32 type
, u32 mask
)
648 return crypto_alloc_tfm(alg_name
, &crypto_aead_type
, type
, mask
);
650 EXPORT_SYMBOL_GPL(crypto_alloc_aead
);
652 static int aead_prepare_alg(struct aead_alg
*alg
)
654 struct crypto_alg
*base
= &alg
->base
;
656 if (max(alg
->maxauthsize
, alg
->ivsize
) > PAGE_SIZE
/ 8)
659 base
->cra_type
= &crypto_new_aead_type
;
660 base
->cra_flags
&= ~CRYPTO_ALG_TYPE_MASK
;
661 base
->cra_flags
|= CRYPTO_ALG_TYPE_AEAD
;
666 int crypto_register_aead(struct aead_alg
*alg
)
668 struct crypto_alg
*base
= &alg
->base
;
671 err
= aead_prepare_alg(alg
);
675 return crypto_register_alg(base
);
677 EXPORT_SYMBOL_GPL(crypto_register_aead
);
679 int crypto_unregister_aead(struct aead_alg
*alg
)
681 return crypto_unregister_alg(&alg
->base
);
683 EXPORT_SYMBOL_GPL(crypto_unregister_aead
);
685 int aead_register_instance(struct crypto_template
*tmpl
,
686 struct aead_instance
*inst
)
690 err
= aead_prepare_alg(&inst
->alg
);
694 return crypto_register_instance(tmpl
, aead_crypto_instance(inst
));
696 EXPORT_SYMBOL_GPL(aead_register_instance
);
698 MODULE_LICENSE("GPL");
699 MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");