xref: /linux/crypto/aead.c (revision 275a9a3f9b6a2158bfb7826074b72d5bdfb2ac35)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * AEAD: Authenticated Encryption with Associated Data
4  *
5  * This file provides API support for AEAD algorithms.
6  *
7  * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
8  */
9 
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 <linux/string_choices.h>
20 #include <net/netlink.h>
21 
22 #include "internal.h"
23 
24 static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
25 			    unsigned int keylen)
26 {
27 	unsigned long alignmask = crypto_aead_alignmask(tfm);
28 	int ret;
29 	u8 *buffer, *alignbuffer;
30 	unsigned long absize;
31 
32 	absize = keylen + alignmask;
33 	buffer = kmalloc(absize, GFP_ATOMIC);
34 	if (!buffer)
35 		return -ENOMEM;
36 
37 	alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
38 	memcpy(alignbuffer, key, keylen);
39 	ret = crypto_aead_alg(tfm)->setkey(tfm, alignbuffer, keylen);
40 	kfree_sensitive(buffer);
41 	return ret;
42 }
43 
44 int crypto_aead_setkey(struct crypto_aead *tfm,
45 		       const u8 *key, unsigned int keylen)
46 {
47 	unsigned long alignmask = crypto_aead_alignmask(tfm);
48 	int err;
49 
50 	if ((unsigned long)key & alignmask)
51 		err = setkey_unaligned(tfm, key, keylen);
52 	else
53 		err = crypto_aead_alg(tfm)->setkey(tfm, key, keylen);
54 
55 	if (unlikely(err)) {
56 		crypto_aead_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
57 		return err;
58 	}
59 
60 	crypto_aead_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
61 	return 0;
62 }
63 EXPORT_SYMBOL_GPL(crypto_aead_setkey);
64 
65 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
66 {
67 	int err;
68 
69 	if ((!authsize && crypto_aead_maxauthsize(tfm)) ||
70 	    authsize > crypto_aead_maxauthsize(tfm))
71 		return -EINVAL;
72 
73 	if (crypto_aead_alg(tfm)->setauthsize) {
74 		err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize);
75 		if (err)
76 			return err;
77 	}
78 
79 	tfm->authsize = authsize;
80 	return 0;
81 }
82 EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
83 
84 int crypto_aead_encrypt(struct aead_request *req)
85 {
86 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
87 
88 	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
89 		return -ENOKEY;
90 
91 	return crypto_aead_alg(aead)->encrypt(req);
92 }
93 EXPORT_SYMBOL_GPL(crypto_aead_encrypt);
94 
95 int crypto_aead_decrypt(struct aead_request *req)
96 {
97 	struct crypto_aead *aead = crypto_aead_reqtfm(req);
98 
99 	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
100 		return -ENOKEY;
101 
102 	if (req->cryptlen < crypto_aead_authsize(aead))
103 		return -EINVAL;
104 
105 	return crypto_aead_alg(aead)->decrypt(req);
106 }
107 EXPORT_SYMBOL_GPL(crypto_aead_decrypt);
108 
109 static void crypto_aead_exit_tfm(struct crypto_tfm *tfm)
110 {
111 	struct crypto_aead *aead = __crypto_aead_cast(tfm);
112 	struct aead_alg *alg = crypto_aead_alg(aead);
113 
114 	alg->exit(aead);
115 }
116 
117 static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
118 {
119 	struct crypto_aead *aead = __crypto_aead_cast(tfm);
120 	struct aead_alg *alg = crypto_aead_alg(aead);
121 
122 	crypto_aead_set_flags(aead, CRYPTO_TFM_NEED_KEY);
123 	crypto_aead_set_reqsize(aead, crypto_tfm_alg_reqsize(tfm));
124 
125 	aead->authsize = alg->maxauthsize;
126 
127 	if (alg->exit)
128 		aead->base.exit = crypto_aead_exit_tfm;
129 
130 	if (alg->init)
131 		return alg->init(aead);
132 
133 	return 0;
134 }
135 
136 static int __maybe_unused crypto_aead_report(
137 	struct sk_buff *skb, struct crypto_alg *alg)
138 {
139 	struct crypto_report_aead raead;
140 	struct aead_alg *aead = container_of(alg, struct aead_alg, base);
141 
142 	memset(&raead, 0, sizeof(raead));
143 
144 	strscpy(raead.type, "aead", sizeof(raead.type));
145 	strscpy(raead.geniv, "<none>", sizeof(raead.geniv));
146 
147 	raead.blocksize = alg->cra_blocksize;
148 	raead.maxauthsize = aead->maxauthsize;
149 	raead.ivsize = aead->ivsize;
150 
151 	return nla_put(skb, CRYPTOCFGA_REPORT_AEAD, sizeof(raead), &raead);
152 }
153 
154 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
155 	__maybe_unused;
156 static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
157 {
158 	struct aead_alg *aead = container_of(alg, struct aead_alg, base);
159 
160 	seq_printf(m, "type         : aead\n");
161 	seq_printf(m, "async        : %s\n",
162 		   str_yes_no(alg->cra_flags & CRYPTO_ALG_ASYNC));
163 	seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
164 	seq_printf(m, "ivsize       : %u\n", aead->ivsize);
165 	seq_printf(m, "maxauthsize  : %u\n", aead->maxauthsize);
166 	seq_printf(m, "geniv        : <none>\n");
167 }
168 
169 static void crypto_aead_free_instance(struct crypto_instance *inst)
170 {
171 	struct aead_instance *aead = aead_instance(inst);
172 
173 	aead->free(aead);
174 }
175 
176 static const struct crypto_type crypto_aead_type = {
177 	.extsize = crypto_alg_extsize,
178 	.init_tfm = crypto_aead_init_tfm,
179 	.free = crypto_aead_free_instance,
180 #ifdef CONFIG_PROC_FS
181 	.show = crypto_aead_show,
182 #endif
183 #if IS_ENABLED(CONFIG_CRYPTO_USER)
184 	.report = crypto_aead_report,
185 #endif
186 	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
187 	.maskset = CRYPTO_ALG_TYPE_MASK,
188 	.type = CRYPTO_ALG_TYPE_AEAD,
189 	.tfmsize = offsetof(struct crypto_aead, base),
190 	.algsize = offsetof(struct aead_alg, base),
191 };
192 
193 int crypto_grab_aead(struct crypto_aead_spawn *spawn,
194 		     struct crypto_instance *inst,
195 		     const char *name, u32 type, u32 mask)
196 {
197 	spawn->base.frontend = &crypto_aead_type;
198 	return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
199 }
200 EXPORT_SYMBOL_GPL(crypto_grab_aead);
201 
202 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
203 {
204 	return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask);
205 }
206 EXPORT_SYMBOL_GPL(crypto_alloc_aead);
207 
208 int crypto_has_aead(const char *alg_name, u32 type, u32 mask)
209 {
210 	return crypto_type_has_alg(alg_name, &crypto_aead_type, type, mask);
211 }
212 EXPORT_SYMBOL_GPL(crypto_has_aead);
213 
214 static int aead_prepare_alg(struct aead_alg *alg)
215 {
216 	struct crypto_alg *base = &alg->base;
217 
218 	if (max3(alg->maxauthsize, alg->ivsize, alg->chunksize) >
219 	    PAGE_SIZE / 8)
220 		return -EINVAL;
221 
222 	if (!alg->chunksize)
223 		alg->chunksize = base->cra_blocksize;
224 
225 	base->cra_type = &crypto_aead_type;
226 	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
227 	base->cra_flags |= CRYPTO_ALG_TYPE_AEAD;
228 
229 	return 0;
230 }
231 
232 int crypto_register_aead(struct aead_alg *alg)
233 {
234 	struct crypto_alg *base = &alg->base;
235 	int err;
236 
237 	err = aead_prepare_alg(alg);
238 	if (err)
239 		return err;
240 
241 	return crypto_register_alg(base);
242 }
243 EXPORT_SYMBOL_GPL(crypto_register_aead);
244 
245 void crypto_unregister_aead(struct aead_alg *alg)
246 {
247 	crypto_unregister_alg(&alg->base);
248 }
249 EXPORT_SYMBOL_GPL(crypto_unregister_aead);
250 
251 int crypto_register_aeads(struct aead_alg *algs, int count)
252 {
253 	int i, ret;
254 
255 	for (i = 0; i < count; i++) {
256 		ret = crypto_register_aead(&algs[i]);
257 		if (ret)
258 			goto err;
259 	}
260 
261 	return 0;
262 
263 err:
264 	for (--i; i >= 0; --i)
265 		crypto_unregister_aead(&algs[i]);
266 
267 	return ret;
268 }
269 EXPORT_SYMBOL_GPL(crypto_register_aeads);
270 
271 void crypto_unregister_aeads(struct aead_alg *algs, int count)
272 {
273 	int i;
274 
275 	for (i = count - 1; i >= 0; --i)
276 		crypto_unregister_aead(&algs[i]);
277 }
278 EXPORT_SYMBOL_GPL(crypto_unregister_aeads);
279 
280 int aead_register_instance(struct crypto_template *tmpl,
281 			   struct aead_instance *inst)
282 {
283 	int err;
284 
285 	if (WARN_ON(!inst->free))
286 		return -EINVAL;
287 
288 	err = aead_prepare_alg(&inst->alg);
289 	if (err)
290 		return err;
291 
292 	return crypto_register_instance(tmpl, aead_crypto_instance(inst));
293 }
294 EXPORT_SYMBOL_GPL(aead_register_instance);
295 
296 MODULE_LICENSE("GPL");
297 MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");
298