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 <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>
26 static int setkey_unaligned(struct crypto_aead
*tfm
, const u8
*key
,
29 struct aead_alg
*aead
= crypto_aead_alg(tfm
);
30 unsigned long alignmask
= crypto_aead_alignmask(tfm
);
32 u8
*buffer
, *alignbuffer
;
35 absize
= keylen
+ alignmask
;
36 buffer
= kmalloc(absize
, GFP_ATOMIC
);
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
);
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
);
64 if (authsize
> crypto_aead_alg(tfm
)->maxauthsize
)
67 if (crypto_aead_alg(tfm
)->setauthsize
) {
68 err
= crypto_aead_alg(tfm
)->setauthsize(crt
->base
, authsize
);
73 crypto_aead_crt(crt
->base
)->authsize
= authsize
;
74 crt
->authsize
= authsize
;
77 EXPORT_SYMBOL_GPL(crypto_aead_setauthsize
);
79 static unsigned int crypto_aead_ctxsize(struct crypto_alg
*alg
, u32 type
,
82 return alg
->cra_ctxsize
;
85 static int no_givcrypt(struct aead_givcrypt_request
*req
)
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)
98 crt
->setkey
= tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_GENIV
?
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
;
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
, "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>");
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
,
131 EXPORT_SYMBOL_GPL(crypto_aead_type
);
133 static int aead_null_givencrypt(struct aead_givcrypt_request
*req
)
135 return crypto_aead_encrypt(&req
->areq
);
138 static int aead_null_givdecrypt(struct aead_givcrypt_request
*req
)
140 return crypto_aead_decrypt(&req
->areq
);
143 static int crypto_init_nivaead_ops(struct crypto_tfm
*tfm
, u32 type
, u32 mask
)
145 struct aead_alg
*alg
= &tfm
->__crt_alg
->cra_aead
;
146 struct aead_tfm
*crt
= &tfm
->crt_aead
;
148 if (max(alg
->maxauthsize
, alg
->ivsize
) > PAGE_SIZE
/ 8)
151 crt
->setkey
= setkey
;
152 crt
->encrypt
= alg
->encrypt
;
153 crt
->decrypt
= alg
->decrypt
;
155 crt
->givencrypt
= aead_null_givencrypt
;
156 crt
->givdecrypt
= aead_null_givdecrypt
;
158 crt
->base
= __crypto_aead_cast(tfm
);
159 crt
->ivsize
= alg
->ivsize
;
160 crt
->authsize
= alg
->maxauthsize
;
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
)
169 struct aead_alg
*aead
= &alg
->cra_aead
;
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
);
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
,
185 EXPORT_SYMBOL_GPL(crypto_nivaead_type
);
187 static int crypto_grab_nivaead(struct crypto_aead_spawn
*spawn
,
188 const char *name
, u32 type
, u32 mask
)
190 struct crypto_alg
*alg
;
193 type
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
194 type
|= CRYPTO_ALG_TYPE_AEAD
;
195 mask
|= CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
;
197 alg
= crypto_alg_mod_lookup(name
, type
, mask
);
201 err
= crypto_init_spawn(&spawn
->base
, alg
, spawn
->base
.inst
, mask
);
206 struct crypto_instance
*aead_geniv_alloc(struct crypto_template
*tmpl
,
207 struct rtattr
**tb
, u32 type
,
211 struct crypto_aead_spawn
*spawn
;
212 struct crypto_attr_type
*algt
;
213 struct crypto_instance
*inst
;
214 struct crypto_alg
*alg
;
217 algt
= crypto_get_attr_type(tb
);
222 if ((algt
->type
^ (CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_GENIV
)) &
224 return ERR_PTR(-EINVAL
);
226 name
= crypto_attr_alg_name(tb
[1]);
231 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
233 return ERR_PTR(-ENOMEM
);
235 spawn
= crypto_instance_ctx(inst
);
237 /* Ignore async algorithms if necessary. */
238 mask
|= crypto_requires_sync(algt
->type
, algt
->mask
);
240 crypto_set_aead_spawn(spawn
, inst
);
241 err
= crypto_grab_nivaead(spawn
, name
, type
, mask
);
245 alg
= crypto_aead_spawn_alg(spawn
);
248 if (!alg
->cra_aead
.ivsize
)
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.
256 if (algt
->mask
& CRYPTO_ALG_GENIV
) {
257 if (strcmp(tmpl
->name
, alg
->cra_aead
.geniv
))
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
);
265 if (snprintf(inst
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
,
266 "%s(%s)", tmpl
->name
, alg
->cra_name
) >=
269 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
,
270 "%s(%s)", tmpl
->name
, alg
->cra_driver_name
) >=
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
;
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
;
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
;
295 crypto_drop_aead(spawn
);
301 EXPORT_SYMBOL_GPL(aead_geniv_alloc
);
303 void aead_geniv_free(struct crypto_instance
*inst
)
305 crypto_drop_aead(crypto_instance_ctx(inst
));
308 EXPORT_SYMBOL_GPL(aead_geniv_free
);
310 int aead_geniv_init(struct crypto_tfm
*tfm
)
312 struct crypto_instance
*inst
= (void *)tfm
->__crt_alg
;
313 struct crypto_aead
*aead
;
315 aead
= crypto_spawn_aead(crypto_instance_ctx(inst
));
317 return PTR_ERR(aead
);
319 tfm
->crt_aead
.base
= aead
;
320 tfm
->crt_aead
.reqsize
+= crypto_aead_reqsize(aead
);
324 EXPORT_SYMBOL_GPL(aead_geniv_init
);
326 void aead_geniv_exit(struct crypto_tfm
*tfm
)
328 crypto_free_aead(tfm
->crt_aead
.base
);
330 EXPORT_SYMBOL_GPL(aead_geniv_exit
);
332 static int crypto_nivaead_default(struct crypto_alg
*alg
, u32 type
, u32 mask
)
334 struct rtattr
*tb
[3];
337 struct crypto_attr_type data
;
341 struct crypto_attr_alg data
;
343 struct crypto_template
*tmpl
;
344 struct crypto_instance
*inst
;
345 struct crypto_alg
*larval
;
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
);
357 if (!crypto_is_larval(larval
))
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
;
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
);
375 geniv
= alg
->cra_aead
.geniv
;
377 tmpl
= crypto_lookup_template(geniv
);
382 inst
= tmpl
->alloc(tb
);
387 if ((err
= crypto_register_instance(tmpl
, inst
))) {
392 /* Redo the lookup to use the instance we just registered. */
396 crypto_tmpl_put(tmpl
);
398 crypto_larval_kill(larval
);
400 crypto_mod_put(larval
);
406 static struct crypto_alg
*crypto_lookup_aead(const char *name
, u32 type
,
409 struct crypto_alg
*alg
;
411 alg
= crypto_alg_mod_lookup(name
, type
, mask
);
415 if (alg
->cra_type
== &crypto_aead_type
)
418 if (!alg
->cra_aead
.ivsize
)
421 return ERR_PTR(crypto_nivaead_default(alg
, type
, mask
));
424 int crypto_grab_aead(struct crypto_aead_spawn
*spawn
, const char *name
,
427 struct crypto_alg
*alg
;
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
;
435 alg
= crypto_lookup_aead(name
, type
, mask
);
439 err
= crypto_init_spawn(&spawn
->base
, alg
, spawn
->base
.inst
, mask
);
443 EXPORT_SYMBOL_GPL(crypto_grab_aead
);
445 struct crypto_aead
*crypto_alloc_aead(const char *alg_name
, u32 type
, u32 mask
)
447 struct crypto_tfm
*tfm
;
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
;
456 struct crypto_alg
*alg
;
458 alg
= crypto_lookup_aead(alg_name
, type
, mask
);
464 tfm
= __crypto_alloc_tfm(alg
, type
, mask
);
466 return __crypto_aead_cast(tfm
);
474 if (signal_pending(current
)) {
482 EXPORT_SYMBOL_GPL(crypto_alloc_aead
);
484 MODULE_LICENSE("GPL");
485 MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");