1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AEAD: Authenticated Encryption with Associated Data
5 * This file provides API support for AEAD algorithms.
10 #include <crypto/internal/aead.h>
11 #include <linux/cryptouser.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/seq_file.h>
18 #include <linux/string.h>
19 #include <net/netlink.h>
23 static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
26 unsigned long alignmask = crypto_aead_alignmask(tfm);
28 u8 *buffer, *alignbuffer;
31 absize = keylen + alignmask;
32 buffer = kmalloc(absize, GFP_ATOMIC);
36 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
37 memcpy(alignbuffer, key, keylen);
38 ret = crypto_aead_alg(tfm)->setkey(tfm, alignbuffer, keylen);
39 memset(alignbuffer, 0, keylen);
44 int crypto_aead_setkey(struct crypto_aead *tfm,
45 const u8 *key, unsigned int keylen)
47 unsigned long alignmask = crypto_aead_alignmask(tfm);
50 if ((unsigned long)key & alignmask)
51 err = setkey_unaligned(tfm, key, keylen);
53 err = crypto_aead_alg(tfm)->setkey(tfm, key, keylen);
56 crypto_aead_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
60 crypto_aead_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
63 EXPORT_SYMBOL_GPL(crypto_aead_setkey);
65 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
69 if ((!authsize && crypto_aead_maxauthsize(tfm)) ||
70 authsize > crypto_aead_maxauthsize(tfm))
73 if (crypto_aead_alg(tfm)->setauthsize) {
74 err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize);
79 tfm->authsize = authsize;
82 EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
84 int crypto_aead_encrypt(struct aead_request *req)
86 struct crypto_aead *aead = crypto_aead_reqtfm(req);
88 if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
91 return crypto_aead_alg(aead)->encrypt(req);
93 EXPORT_SYMBOL_GPL(crypto_aead_encrypt);
95 int crypto_aead_decrypt(struct aead_request *req)
97 struct crypto_aead *aead = crypto_aead_reqtfm(req);
99 if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
102 if (req->cryptlen < crypto_aead_authsize(aead))
105 return crypto_aead_alg(aead)->decrypt(req);
107 EXPORT_SYMBOL_GPL(crypto_aead_decrypt);
109 static void crypto_aead_exit_tfm(struct crypto_tfm *tfm)
111 struct crypto_aead *aead = __crypto_aead_cast(tfm);
112 struct aead_alg *alg = crypto_aead_alg(aead);
117 static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
119 struct crypto_aead *aead = __crypto_aead_cast(tfm);
120 struct aead_alg *alg = crypto_aead_alg(aead);
122 crypto_aead_set_flags(aead, CRYPTO_TFM_NEED_KEY);
124 aead->authsize = alg->maxauthsize;
127 aead->base.exit = crypto_aead_exit_tfm;
130 return alg->init(aead);
135 static int __maybe_unused crypto_aead_report(
136 struct sk_buff *skb, struct crypto_alg *alg)
138 struct crypto_report_aead raead;
139 struct aead_alg *aead = container_of(alg, struct aead_alg, base);
141 memset(&raead, 0, sizeof(raead));
143 strscpy(raead.type, "aead", sizeof(raead.type));
144 strscpy(raead.geniv, "<none>", sizeof(raead.geniv));
146 raead.blocksize = alg->cra_blocksize;
147 raead.maxauthsize = aead->maxauthsize;
148 raead.ivsize = aead->ivsize;
150 return nla_put(skb, CRYPTOCFGA_REPORT_AEAD, sizeof(raead), &raead);
153 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
155 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
157 struct aead_alg *aead = container_of(alg, struct aead_alg, base);
159 seq_printf(m, "type : aead\n");
160 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
162 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
163 seq_printf(m, "ivsize : %u\n", aead->ivsize);
164 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
165 seq_printf(m, "geniv : <none>\n");
168 static void crypto_aead_free_instance(struct crypto_instance *inst)
170 struct aead_instance *aead = aead_instance(inst);
175 static const struct crypto_type crypto_aead_type = {
176 .extsize = crypto_alg_extsize,
177 .init_tfm = crypto_aead_init_tfm,
178 .free = crypto_aead_free_instance,
179 #ifdef CONFIG_PROC_FS
180 .show = crypto_aead_show,
182 #if IS_ENABLED(CONFIG_CRYPTO_USER)
183 .report = crypto_aead_report,
185 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
186 .maskset = CRYPTO_ALG_TYPE_MASK,
187 .type = CRYPTO_ALG_TYPE_AEAD,
188 .tfmsize = offsetof(struct crypto_aead, base),
191 int crypto_grab_aead(struct crypto_aead_spawn *spawn,
192 struct crypto_instance *inst,
193 const char *name, u32 type, u32 mask)
195 spawn->base.frontend = &crypto_aead_type;
196 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
198 EXPORT_SYMBOL_GPL(crypto_grab_aead);
200 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
202 return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask);
204 EXPORT_SYMBOL_GPL(crypto_alloc_aead);
206 int crypto_has_aead(const char *alg_name, u32 type, u32 mask)
208 return crypto_type_has_alg(alg_name, &crypto_aead_type, type, mask);
210 EXPORT_SYMBOL_GPL(crypto_has_aead);
212 static int aead_prepare_alg(struct aead_alg *alg)
214 struct crypto_alg *base = &alg->base;
216 if (max3(alg->maxauthsize, alg->ivsize, alg->chunksize) >
221 alg->chunksize = base->cra_blocksize;
223 base->cra_type = &crypto_aead_type;
224 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
225 base->cra_flags |= CRYPTO_ALG_TYPE_AEAD;
230 int crypto_register_aead(struct aead_alg *alg)
232 struct crypto_alg *base = &alg->base;
235 err = aead_prepare_alg(alg);
239 return crypto_register_alg(base);
241 EXPORT_SYMBOL_GPL(crypto_register_aead);
243 void crypto_unregister_aead(struct aead_alg *alg)
245 crypto_unregister_alg(&alg->base);
247 EXPORT_SYMBOL_GPL(crypto_unregister_aead);
249 int crypto_register_aeads(struct aead_alg *algs, int count)
253 for (i = 0; i < count; i++) {
254 ret = crypto_register_aead(&algs[i]);
262 for (--i; i >= 0; --i)
263 crypto_unregister_aead(&algs[i]);
267 EXPORT_SYMBOL_GPL(crypto_register_aeads);
269 void crypto_unregister_aeads(struct aead_alg *algs, int count)
273 for (i = count - 1; i >= 0; --i)
274 crypto_unregister_aead(&algs[i]);
276 EXPORT_SYMBOL_GPL(crypto_unregister_aeads);
278 int aead_register_instance(struct crypto_template *tmpl,
279 struct aead_instance *inst)
283 if (WARN_ON(!inst->free))
286 err = aead_prepare_alg(&inst->alg);
290 return crypto_register_instance(tmpl, aead_crypto_instance(inst));
292 EXPORT_SYMBOL_GPL(aead_register_instance);
294 MODULE_LICENSE("GPL");
295 MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");