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
, "async : %s\n", alg
->cra_flags
& CRYPTO_ALG_ASYNC
?
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
,
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)
153 crt
->setkey
= setkey
;
154 crt
->encrypt
= alg
->encrypt
;
155 crt
->decrypt
= alg
->decrypt
;
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
;
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
?
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
,
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
;
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
);
205 err
= crypto_init_spawn(&spawn
->base
, alg
, spawn
->base
.inst
, mask
);
210 struct crypto_instance
*aead_geniv_alloc(struct crypto_template
*tmpl
,
211 struct rtattr
**tb
, u32 type
,
215 struct crypto_aead_spawn
*spawn
;
216 struct crypto_attr_type
*algt
;
217 struct crypto_instance
*inst
;
218 struct crypto_alg
*alg
;
221 algt
= crypto_get_attr_type(tb
);
226 if ((algt
->type
^ (CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_GENIV
)) &
228 return ERR_PTR(-EINVAL
);
230 name
= crypto_attr_alg_name(tb
[1]);
235 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
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
);
249 alg
= crypto_aead_spawn_alg(spawn
);
252 if (!alg
->cra_aead
.ivsize
)
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
))
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
);
269 if (snprintf(inst
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
,
270 "%s(%s)", tmpl
->name
, alg
->cra_name
) >=
273 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
,
274 "%s(%s)", tmpl
->name
, alg
->cra_driver_name
) >=
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
;
299 crypto_drop_aead(spawn
);
305 EXPORT_SYMBOL_GPL(aead_geniv_alloc
);
307 void aead_geniv_free(struct crypto_instance
*inst
)
309 crypto_drop_aead(crypto_instance_ctx(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
));
321 return PTR_ERR(aead
);
323 tfm
->crt_aead
.base
= aead
;
324 tfm
->crt_aead
.reqsize
+= crypto_aead_reqsize(aead
);
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];
341 struct crypto_attr_type data
;
345 struct crypto_attr_alg data
;
347 struct crypto_template
*tmpl
;
348 struct crypto_instance
*inst
;
349 struct crypto_alg
*larval
;
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
);
361 if (!crypto_is_larval(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
;
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
);
379 geniv
= alg
->cra_aead
.geniv
;
381 tmpl
= crypto_lookup_template(geniv
);
386 inst
= tmpl
->alloc(tb
);
391 if ((err
= crypto_register_instance(tmpl
, inst
))) {
396 /* Redo the lookup to use the instance we just registered. */
400 crypto_tmpl_put(tmpl
);
402 crypto_larval_kill(larval
);
404 crypto_mod_put(larval
);
410 static struct crypto_alg
*crypto_lookup_aead(const char *name
, u32 type
,
413 struct crypto_alg
*alg
;
415 alg
= crypto_alg_mod_lookup(name
, type
, mask
);
419 if (alg
->cra_type
== &crypto_aead_type
)
422 if (!alg
->cra_aead
.ivsize
)
425 return ERR_PTR(crypto_nivaead_default(alg
, type
, mask
));
428 int crypto_grab_aead(struct crypto_aead_spawn
*spawn
, const char *name
,
431 struct crypto_alg
*alg
;
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
);
443 err
= crypto_init_spawn(&spawn
->base
, alg
, spawn
->base
.inst
, mask
);
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
;
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
;
460 struct crypto_alg
*alg
;
462 alg
= crypto_lookup_aead(alg_name
, type
, mask
);
468 tfm
= __crypto_alloc_tfm(alg
, type
, mask
);
470 return __crypto_aead_cast(tfm
);
478 if (signal_pending(current
)) {
486 EXPORT_SYMBOL_GPL(crypto_alloc_aead
);
488 MODULE_LICENSE("GPL");
489 MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");