Merge tag 'phy-for-5.6_v2' of git://git.kernel.org/pub/scm/linux/kernel/git/kishon...
[linux/fpc-iii.git] / crypto / aead.c
blob47f16d139e8e7c73af6fd1b1fd7d075ecb34ab14
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * AEAD: Authenticated Encryption with Associated Data
5 * This file provides API support for AEAD algorithms.
7 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
8 */
10 #include <crypto/internal/aead.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/seq_file.h>
17 #include <linux/cryptouser.h>
18 #include <net/netlink.h>
20 #include "internal.h"
22 static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
23 unsigned int keylen)
25 unsigned long alignmask = crypto_aead_alignmask(tfm);
26 int ret;
27 u8 *buffer, *alignbuffer;
28 unsigned long absize;
30 absize = keylen + alignmask;
31 buffer = kmalloc(absize, GFP_ATOMIC);
32 if (!buffer)
33 return -ENOMEM;
35 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
36 memcpy(alignbuffer, key, keylen);
37 ret = crypto_aead_alg(tfm)->setkey(tfm, alignbuffer, keylen);
38 memset(alignbuffer, 0, keylen);
39 kfree(buffer);
40 return ret;
43 int crypto_aead_setkey(struct crypto_aead *tfm,
44 const u8 *key, unsigned int keylen)
46 unsigned long alignmask = crypto_aead_alignmask(tfm);
47 int err;
49 if ((unsigned long)key & alignmask)
50 err = setkey_unaligned(tfm, key, keylen);
51 else
52 err = crypto_aead_alg(tfm)->setkey(tfm, key, keylen);
54 if (unlikely(err)) {
55 crypto_aead_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
56 return err;
59 crypto_aead_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
60 return 0;
62 EXPORT_SYMBOL_GPL(crypto_aead_setkey);
64 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
66 int err;
68 if ((!authsize && crypto_aead_maxauthsize(tfm)) ||
69 authsize > crypto_aead_maxauthsize(tfm))
70 return -EINVAL;
72 if (crypto_aead_alg(tfm)->setauthsize) {
73 err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize);
74 if (err)
75 return err;
78 tfm->authsize = authsize;
79 return 0;
81 EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
83 int crypto_aead_encrypt(struct aead_request *req)
85 struct crypto_aead *aead = crypto_aead_reqtfm(req);
86 struct crypto_alg *alg = aead->base.__crt_alg;
87 unsigned int cryptlen = req->cryptlen;
88 int ret;
90 crypto_stats_get(alg);
91 if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
92 ret = -ENOKEY;
93 else
94 ret = crypto_aead_alg(aead)->encrypt(req);
95 crypto_stats_aead_encrypt(cryptlen, alg, ret);
96 return ret;
98 EXPORT_SYMBOL_GPL(crypto_aead_encrypt);
100 int crypto_aead_decrypt(struct aead_request *req)
102 struct crypto_aead *aead = crypto_aead_reqtfm(req);
103 struct crypto_alg *alg = aead->base.__crt_alg;
104 unsigned int cryptlen = req->cryptlen;
105 int ret;
107 crypto_stats_get(alg);
108 if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
109 ret = -ENOKEY;
110 else if (req->cryptlen < crypto_aead_authsize(aead))
111 ret = -EINVAL;
112 else
113 ret = crypto_aead_alg(aead)->decrypt(req);
114 crypto_stats_aead_decrypt(cryptlen, alg, ret);
115 return ret;
117 EXPORT_SYMBOL_GPL(crypto_aead_decrypt);
119 static void crypto_aead_exit_tfm(struct crypto_tfm *tfm)
121 struct crypto_aead *aead = __crypto_aead_cast(tfm);
122 struct aead_alg *alg = crypto_aead_alg(aead);
124 alg->exit(aead);
127 static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
129 struct crypto_aead *aead = __crypto_aead_cast(tfm);
130 struct aead_alg *alg = crypto_aead_alg(aead);
132 crypto_aead_set_flags(aead, CRYPTO_TFM_NEED_KEY);
134 aead->authsize = alg->maxauthsize;
136 if (alg->exit)
137 aead->base.exit = crypto_aead_exit_tfm;
139 if (alg->init)
140 return alg->init(aead);
142 return 0;
145 #ifdef CONFIG_NET
146 static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
148 struct crypto_report_aead raead;
149 struct aead_alg *aead = container_of(alg, struct aead_alg, base);
151 memset(&raead, 0, sizeof(raead));
153 strscpy(raead.type, "aead", sizeof(raead.type));
154 strscpy(raead.geniv, "<none>", sizeof(raead.geniv));
156 raead.blocksize = alg->cra_blocksize;
157 raead.maxauthsize = aead->maxauthsize;
158 raead.ivsize = aead->ivsize;
160 return nla_put(skb, CRYPTOCFGA_REPORT_AEAD, sizeof(raead), &raead);
162 #else
163 static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
165 return -ENOSYS;
167 #endif
169 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
170 __maybe_unused;
171 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
173 struct aead_alg *aead = container_of(alg, struct aead_alg, base);
175 seq_printf(m, "type : aead\n");
176 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
177 "yes" : "no");
178 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
179 seq_printf(m, "ivsize : %u\n", aead->ivsize);
180 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
181 seq_printf(m, "geniv : <none>\n");
184 static void crypto_aead_free_instance(struct crypto_instance *inst)
186 struct aead_instance *aead = aead_instance(inst);
188 if (!aead->free) {
189 inst->tmpl->free(inst);
190 return;
193 aead->free(aead);
196 static const struct crypto_type crypto_aead_type = {
197 .extsize = crypto_alg_extsize,
198 .init_tfm = crypto_aead_init_tfm,
199 .free = crypto_aead_free_instance,
200 #ifdef CONFIG_PROC_FS
201 .show = crypto_aead_show,
202 #endif
203 .report = crypto_aead_report,
204 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
205 .maskset = CRYPTO_ALG_TYPE_MASK,
206 .type = CRYPTO_ALG_TYPE_AEAD,
207 .tfmsize = offsetof(struct crypto_aead, base),
210 int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
211 u32 type, u32 mask)
213 spawn->base.frontend = &crypto_aead_type;
214 return crypto_grab_spawn(&spawn->base, name, type, mask);
216 EXPORT_SYMBOL_GPL(crypto_grab_aead);
218 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
220 return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask);
222 EXPORT_SYMBOL_GPL(crypto_alloc_aead);
224 static int aead_prepare_alg(struct aead_alg *alg)
226 struct crypto_alg *base = &alg->base;
228 if (max3(alg->maxauthsize, alg->ivsize, alg->chunksize) >
229 PAGE_SIZE / 8)
230 return -EINVAL;
232 if (!alg->chunksize)
233 alg->chunksize = base->cra_blocksize;
235 base->cra_type = &crypto_aead_type;
236 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
237 base->cra_flags |= CRYPTO_ALG_TYPE_AEAD;
239 return 0;
242 int crypto_register_aead(struct aead_alg *alg)
244 struct crypto_alg *base = &alg->base;
245 int err;
247 err = aead_prepare_alg(alg);
248 if (err)
249 return err;
251 return crypto_register_alg(base);
253 EXPORT_SYMBOL_GPL(crypto_register_aead);
255 void crypto_unregister_aead(struct aead_alg *alg)
257 crypto_unregister_alg(&alg->base);
259 EXPORT_SYMBOL_GPL(crypto_unregister_aead);
261 int crypto_register_aeads(struct aead_alg *algs, int count)
263 int i, ret;
265 for (i = 0; i < count; i++) {
266 ret = crypto_register_aead(&algs[i]);
267 if (ret)
268 goto err;
271 return 0;
273 err:
274 for (--i; i >= 0; --i)
275 crypto_unregister_aead(&algs[i]);
277 return ret;
279 EXPORT_SYMBOL_GPL(crypto_register_aeads);
281 void crypto_unregister_aeads(struct aead_alg *algs, int count)
283 int i;
285 for (i = count - 1; i >= 0; --i)
286 crypto_unregister_aead(&algs[i]);
288 EXPORT_SYMBOL_GPL(crypto_unregister_aeads);
290 int aead_register_instance(struct crypto_template *tmpl,
291 struct aead_instance *inst)
293 int err;
295 err = aead_prepare_alg(&inst->alg);
296 if (err)
297 return err;
299 return crypto_register_instance(tmpl, aead_crypto_instance(inst));
301 EXPORT_SYMBOL_GPL(aead_register_instance);
303 MODULE_LICENSE("GPL");
304 MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");