blob: 47f16d139e8e7c73af6fd1b1fd7d075ecb34ab14 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xu1ae97822007-08-30 15:36:14 +08002/*
3 * AEAD: Authenticated Encryption with Associated Data
Richard Hartmann39225382010-02-16 20:24:07 +08004 *
Herbert Xu1ae97822007-08-30 15:36:14 +08005 * This file provides API support for AEAD algorithms.
6 *
Herbert Xub0d955b2015-08-14 15:30:41 +08007 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xu1ae97822007-08-30 15:36:14 +08008 */
9
Herbert Xu20cc01b2019-11-08 18:41:58 +080010#include <crypto/internal/aead.h>
11#include <linux/errno.h>
Herbert Xu1ae97822007-08-30 15:36:14 +080012#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>
Steffen Klassert6ad414f2011-09-27 07:44:27 +020017#include <linux/cryptouser.h>
18#include <net/netlink.h>
Herbert Xu1ae97822007-08-30 15:36:14 +080019
Herbert Xu5b6d2d72007-12-12 19:23:36 +080020#include "internal.h"
21
Herbert Xu1ae97822007-08-30 15:36:14 +080022static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
23 unsigned int keylen)
24{
Herbert Xu1ae97822007-08-30 15:36:14 +080025 unsigned long alignmask = crypto_aead_alignmask(tfm);
26 int ret;
27 u8 *buffer, *alignbuffer;
28 unsigned long absize;
29
30 absize = keylen + alignmask;
31 buffer = kmalloc(absize, GFP_ATOMIC);
32 if (!buffer)
33 return -ENOMEM;
34
35 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
36 memcpy(alignbuffer, key, keylen);
Herbert Xub0d955b2015-08-14 15:30:41 +080037 ret = crypto_aead_alg(tfm)->setkey(tfm, alignbuffer, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +080038 memset(alignbuffer, 0, keylen);
39 kfree(buffer);
40 return ret;
41}
42
Herbert Xu5d1d65f2015-05-11 17:48:12 +080043int crypto_aead_setkey(struct crypto_aead *tfm,
44 const u8 *key, unsigned int keylen)
Herbert Xu1ae97822007-08-30 15:36:14 +080045{
Herbert Xu1ae97822007-08-30 15:36:14 +080046 unsigned long alignmask = crypto_aead_alignmask(tfm);
Eric Biggersdc26c172018-01-03 11:16:30 -080047 int err;
Herbert Xu1ae97822007-08-30 15:36:14 +080048
49 if ((unsigned long)key & alignmask)
Eric Biggersdc26c172018-01-03 11:16:30 -080050 err = setkey_unaligned(tfm, key, keylen);
51 else
52 err = crypto_aead_alg(tfm)->setkey(tfm, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +080053
Eric Biggers6ebc9702019-01-06 18:47:44 -080054 if (unlikely(err)) {
55 crypto_aead_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
Eric Biggersdc26c172018-01-03 11:16:30 -080056 return err;
Eric Biggers6ebc9702019-01-06 18:47:44 -080057 }
Eric Biggersdc26c172018-01-03 11:16:30 -080058
59 crypto_aead_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
60 return 0;
Herbert Xu1ae97822007-08-30 15:36:14 +080061}
Herbert Xu5d1d65f2015-05-11 17:48:12 +080062EXPORT_SYMBOL_GPL(crypto_aead_setkey);
Herbert Xu1ae97822007-08-30 15:36:14 +080063
Herbert Xu7ba683a2007-12-02 18:49:21 +110064int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
65{
66 int err;
67
Pascal van Leeuwena62084d2019-08-09 17:51:07 +020068 if ((!authsize && crypto_aead_maxauthsize(tfm)) ||
69 authsize > crypto_aead_maxauthsize(tfm))
Herbert Xu7ba683a2007-12-02 18:49:21 +110070 return -EINVAL;
71
Herbert Xub0d955b2015-08-14 15:30:41 +080072 if (crypto_aead_alg(tfm)->setauthsize) {
73 err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize);
Herbert Xu7ba683a2007-12-02 18:49:21 +110074 if (err)
75 return err;
76 }
77
Herbert Xu5d1d65f2015-05-11 17:48:12 +080078 tfm->authsize = authsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +110079 return 0;
80}
81EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
82
Eric Biggersf2fe1152019-06-02 22:45:16 -070083int crypto_aead_encrypt(struct aead_request *req)
84{
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;
89
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;
97}
98EXPORT_SYMBOL_GPL(crypto_aead_encrypt);
99
100int crypto_aead_decrypt(struct aead_request *req)
101{
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;
106
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;
116}
117EXPORT_SYMBOL_GPL(crypto_aead_decrypt);
118
Herbert Xu5eb8ec62015-05-28 22:07:53 +0800119static void crypto_aead_exit_tfm(struct crypto_tfm *tfm)
120{
121 struct crypto_aead *aead = __crypto_aead_cast(tfm);
122 struct aead_alg *alg = crypto_aead_alg(aead);
123
124 alg->exit(aead);
125}
126
Herbert Xu63293c62015-05-21 15:11:08 +0800127static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
128{
129 struct crypto_aead *aead = __crypto_aead_cast(tfm);
130 struct aead_alg *alg = crypto_aead_alg(aead);
131
Eric Biggersdc26c172018-01-03 11:16:30 -0800132 crypto_aead_set_flags(aead, CRYPTO_TFM_NEED_KEY);
133
Herbert Xu63293c62015-05-21 15:11:08 +0800134 aead->authsize = alg->maxauthsize;
135
Herbert Xu5eb8ec62015-05-28 22:07:53 +0800136 if (alg->exit)
137 aead->base.exit = crypto_aead_exit_tfm;
138
139 if (alg->init)
140 return alg->init(aead);
141
Herbert Xu63293c62015-05-21 15:11:08 +0800142 return 0;
143}
144
Herbert Xu3acc8472011-11-03 23:46:07 +1100145#ifdef CONFIG_NET
Herbert Xu63293c62015-05-21 15:11:08 +0800146static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
147{
148 struct crypto_report_aead raead;
149 struct aead_alg *aead = container_of(alg, struct aead_alg, base);
150
Eric Biggers37db69e2018-11-03 14:56:03 -0700151 memset(&raead, 0, sizeof(raead));
152
153 strscpy(raead.type, "aead", sizeof(raead.type));
154 strscpy(raead.geniv, "<none>", sizeof(raead.geniv));
Herbert Xu63293c62015-05-21 15:11:08 +0800155
156 raead.blocksize = alg->cra_blocksize;
157 raead.maxauthsize = aead->maxauthsize;
158 raead.ivsize = aead->ivsize;
159
Eric Biggers37db69e2018-11-03 14:56:03 -0700160 return nla_put(skb, CRYPTOCFGA_REPORT_AEAD, sizeof(raead), &raead);
Herbert Xu63293c62015-05-21 15:11:08 +0800161}
162#else
Herbert Xu3acc8472011-11-03 23:46:07 +1100163static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
164{
165 return -ENOSYS;
166}
167#endif
Steffen Klassert6ad414f2011-09-27 07:44:27 +0200168
Herbert Xu1ae97822007-08-30 15:36:14 +0800169static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
Gideon Israel Dsouzad8c34b92016-12-31 21:26:23 +0530170 __maybe_unused;
Herbert Xu1ae97822007-08-30 15:36:14 +0800171static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
172{
Herbert Xu63293c62015-05-21 15:11:08 +0800173 struct aead_alg *aead = container_of(alg, struct aead_alg, base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800174
175 seq_printf(m, "type : aead\n");
Herbert Xu189ed662007-12-14 22:29:37 +0800176 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
177 "yes" : "no");
Herbert Xu1ae97822007-08-30 15:36:14 +0800178 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
179 seq_printf(m, "ivsize : %u\n", aead->ivsize);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100180 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
Herbert Xu63293c62015-05-21 15:11:08 +0800181 seq_printf(m, "geniv : <none>\n");
Herbert Xu1ae97822007-08-30 15:36:14 +0800182}
183
Herbert Xuba75e152015-07-09 07:17:17 +0800184static void crypto_aead_free_instance(struct crypto_instance *inst)
185{
186 struct aead_instance *aead = aead_instance(inst);
187
188 if (!aead->free) {
189 inst->tmpl->free(inst);
190 return;
191 }
192
193 aead->free(aead);
194}
195
Herbert Xub0d955b2015-08-14 15:30:41 +0800196static const struct crypto_type crypto_aead_type = {
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800197 .extsize = crypto_alg_extsize,
198 .init_tfm = crypto_aead_init_tfm,
Herbert Xuba75e152015-07-09 07:17:17 +0800199 .free = crypto_aead_free_instance,
Herbert Xu1ae97822007-08-30 15:36:14 +0800200#ifdef CONFIG_PROC_FS
201 .show = crypto_aead_show,
202#endif
Steffen Klassert6ad414f2011-09-27 07:44:27 +0200203 .report = crypto_aead_report,
Herbert Xu63293c62015-05-21 15:11:08 +0800204 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800205 .maskset = CRYPTO_ALG_TYPE_MASK,
206 .type = CRYPTO_ALG_TYPE_AEAD,
207 .tfmsize = offsetof(struct crypto_aead, base),
Herbert Xu1ae97822007-08-30 15:36:14 +0800208};
Herbert Xu1ae97822007-08-30 15:36:14 +0800209
Herbert Xud29ce982007-12-12 19:24:27 +0800210int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
211 u32 type, u32 mask)
212{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800213 spawn->base.frontend = &crypto_aead_type;
214 return crypto_grab_spawn(&spawn->base, name, type, mask);
Herbert Xud29ce982007-12-12 19:24:27 +0800215}
216EXPORT_SYMBOL_GPL(crypto_grab_aead);
217
218struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
219{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800220 return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask);
Herbert Xud29ce982007-12-12 19:24:27 +0800221}
222EXPORT_SYMBOL_GPL(crypto_alloc_aead);
223
Herbert Xu63293c62015-05-21 15:11:08 +0800224static int aead_prepare_alg(struct aead_alg *alg)
225{
226 struct crypto_alg *base = &alg->base;
227
Herbert Xu7a530aa2016-07-12 13:17:33 +0800228 if (max3(alg->maxauthsize, alg->ivsize, alg->chunksize) >
229 PAGE_SIZE / 8)
Herbert Xu63293c62015-05-21 15:11:08 +0800230 return -EINVAL;
231
Herbert Xu7a530aa2016-07-12 13:17:33 +0800232 if (!alg->chunksize)
233 alg->chunksize = base->cra_blocksize;
234
Herbert Xub0d955b2015-08-14 15:30:41 +0800235 base->cra_type = &crypto_aead_type;
Herbert Xu63293c62015-05-21 15:11:08 +0800236 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
237 base->cra_flags |= CRYPTO_ALG_TYPE_AEAD;
238
239 return 0;
240}
241
242int crypto_register_aead(struct aead_alg *alg)
243{
244 struct crypto_alg *base = &alg->base;
245 int err;
246
247 err = aead_prepare_alg(alg);
248 if (err)
249 return err;
250
251 return crypto_register_alg(base);
252}
253EXPORT_SYMBOL_GPL(crypto_register_aead);
254
Herbert Xu43615362015-05-28 22:07:57 +0800255void crypto_unregister_aead(struct aead_alg *alg)
Herbert Xu63293c62015-05-21 15:11:08 +0800256{
Herbert Xu43615362015-05-28 22:07:57 +0800257 crypto_unregister_alg(&alg->base);
Herbert Xu63293c62015-05-21 15:11:08 +0800258}
259EXPORT_SYMBOL_GPL(crypto_unregister_aead);
260
Herbert Xucaab9462015-05-28 22:07:59 +0800261int crypto_register_aeads(struct aead_alg *algs, int count)
262{
263 int i, ret;
264
265 for (i = 0; i < count; i++) {
266 ret = crypto_register_aead(&algs[i]);
267 if (ret)
268 goto err;
269 }
270
271 return 0;
272
273err:
274 for (--i; i >= 0; --i)
275 crypto_unregister_aead(&algs[i]);
276
277 return ret;
278}
279EXPORT_SYMBOL_GPL(crypto_register_aeads);
280
281void crypto_unregister_aeads(struct aead_alg *algs, int count)
282{
283 int i;
284
285 for (i = count - 1; i >= 0; --i)
286 crypto_unregister_aead(&algs[i]);
287}
288EXPORT_SYMBOL_GPL(crypto_unregister_aeads);
289
Herbert Xu63293c62015-05-21 15:11:08 +0800290int aead_register_instance(struct crypto_template *tmpl,
291 struct aead_instance *inst)
292{
293 int err;
294
295 err = aead_prepare_alg(&inst->alg);
296 if (err)
297 return err;
298
299 return crypto_register_instance(tmpl, aead_crypto_instance(inst));
300}
301EXPORT_SYMBOL_GPL(aead_register_instance);
302
Herbert Xu1ae97822007-08-30 15:36:14 +0800303MODULE_LICENSE("GPL");
304MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");