blob: 07bf99773548bf9f088b6ff8380edb4caf7cda4a [file] [log] [blame]
Herbert Xu1ae97822007-08-30 15:36:14 +08001/*
2 * AEAD: Authenticated Encryption with Associated Data
Richard Hartmann39225382010-02-16 20:24:07 +08003 *
Herbert Xu1ae97822007-08-30 15:36:14 +08004 * This file provides API support for AEAD algorithms.
5 *
6 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
7 *
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
Richard Hartmann39225382010-02-16 20:24:07 +080010 * Software Foundation; either version 2 of the License, or (at your option)
Herbert Xu1ae97822007-08-30 15:36:14 +080011 * any later version.
12 *
13 */
14
Herbert Xu63504492015-05-27 14:37:30 +080015#include <crypto/internal/geniv.h>
Herbert Xu996d98d2015-05-21 15:11:01 +080016#include <crypto/scatterwalk.h>
Herbert Xu5b6d2d72007-12-12 19:23:36 +080017#include <linux/err.h>
Herbert Xu1ae97822007-08-30 15:36:14 +080018#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
Herbert Xud29ce982007-12-12 19:24:27 +080021#include <linux/rtnetlink.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040022#include <linux/sched.h>
Herbert Xu1ae97822007-08-30 15:36:14 +080023#include <linux/slab.h>
24#include <linux/seq_file.h>
Steffen Klassert6ad414f2011-09-27 07:44:27 +020025#include <linux/cryptouser.h>
26#include <net/netlink.h>
Herbert Xu1ae97822007-08-30 15:36:14 +080027
Herbert Xu5b6d2d72007-12-12 19:23:36 +080028#include "internal.h"
29
Herbert Xu63504492015-05-27 14:37:30 +080030struct compat_request_ctx {
31 struct scatterlist src[2];
32 struct scatterlist dst[2];
33 struct scatterlist ivbuf[2];
34 struct scatterlist *ivsg;
35 struct aead_givcrypt_request subreq;
36};
37
Herbert Xu5d1d65f2015-05-11 17:48:12 +080038static int aead_null_givencrypt(struct aead_givcrypt_request *req);
39static int aead_null_givdecrypt(struct aead_givcrypt_request *req);
40
Herbert Xu1ae97822007-08-30 15:36:14 +080041static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
42 unsigned int keylen)
43{
Herbert Xu1ae97822007-08-30 15:36:14 +080044 unsigned long alignmask = crypto_aead_alignmask(tfm);
45 int ret;
46 u8 *buffer, *alignbuffer;
47 unsigned long absize;
48
49 absize = keylen + alignmask;
50 buffer = kmalloc(absize, GFP_ATOMIC);
51 if (!buffer)
52 return -ENOMEM;
53
54 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
55 memcpy(alignbuffer, key, keylen);
Herbert Xu63293c62015-05-21 15:11:08 +080056 ret = tfm->setkey(tfm, alignbuffer, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +080057 memset(alignbuffer, 0, keylen);
58 kfree(buffer);
59 return ret;
60}
61
Herbert Xu5d1d65f2015-05-11 17:48:12 +080062int crypto_aead_setkey(struct crypto_aead *tfm,
63 const u8 *key, unsigned int keylen)
Herbert Xu1ae97822007-08-30 15:36:14 +080064{
Herbert Xu1ae97822007-08-30 15:36:14 +080065 unsigned long alignmask = crypto_aead_alignmask(tfm);
66
Herbert Xu5d1d65f2015-05-11 17:48:12 +080067 tfm = tfm->child;
68
Herbert Xu1ae97822007-08-30 15:36:14 +080069 if ((unsigned long)key & alignmask)
70 return setkey_unaligned(tfm, key, keylen);
71
Herbert Xu63293c62015-05-21 15:11:08 +080072 return tfm->setkey(tfm, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +080073}
Herbert Xu5d1d65f2015-05-11 17:48:12 +080074EXPORT_SYMBOL_GPL(crypto_aead_setkey);
Herbert Xu1ae97822007-08-30 15:36:14 +080075
Herbert Xu7ba683a2007-12-02 18:49:21 +110076int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
77{
78 int err;
79
Herbert Xu30e4c012015-05-22 16:30:48 +080080 if (authsize > crypto_aead_maxauthsize(tfm))
Herbert Xu7ba683a2007-12-02 18:49:21 +110081 return -EINVAL;
82
Herbert Xu63293c62015-05-21 15:11:08 +080083 if (tfm->setauthsize) {
84 err = tfm->setauthsize(tfm->child, authsize);
Herbert Xu7ba683a2007-12-02 18:49:21 +110085 if (err)
86 return err;
87 }
88
Herbert Xu5d1d65f2015-05-11 17:48:12 +080089 tfm->child->authsize = authsize;
90 tfm->authsize = authsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +110091 return 0;
92}
93EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
94
Herbert Xu996d98d2015-05-21 15:11:01 +080095struct aead_old_request {
96 struct scatterlist srcbuf[2];
97 struct scatterlist dstbuf[2];
98 struct aead_request subreq;
99};
100
101unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
102{
103 return tfm->reqsize + sizeof(struct aead_old_request);
104}
105EXPORT_SYMBOL_GPL(crypto_aead_reqsize);
106
107static int old_crypt(struct aead_request *req,
108 int (*crypt)(struct aead_request *req))
109{
110 struct aead_old_request *nreq = aead_request_ctx(req);
111 struct crypto_aead *aead = crypto_aead_reqtfm(req);
112 struct scatterlist *src, *dst;
113
114 if (req->old)
115 return crypt(req);
116
Herbert Xu374d4ad2015-05-23 15:41:57 +0800117 src = scatterwalk_ffwd(nreq->srcbuf, req->src, req->assoclen);
Herbert Xub455eb82015-05-27 14:37:29 +0800118 dst = req->src == req->dst ?
119 src : scatterwalk_ffwd(nreq->dstbuf, req->dst, req->assoclen);
Herbert Xu996d98d2015-05-21 15:11:01 +0800120
121 aead_request_set_tfm(&nreq->subreq, aead);
122 aead_request_set_callback(&nreq->subreq, aead_request_flags(req),
123 req->base.complete, req->base.data);
124 aead_request_set_crypt(&nreq->subreq, src, dst, req->cryptlen,
125 req->iv);
126 aead_request_set_assoc(&nreq->subreq, req->src, req->assoclen);
127
128 return crypt(&nreq->subreq);
129}
130
131static int old_encrypt(struct aead_request *req)
132{
133 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu2d0f2302015-05-21 15:11:02 +0800134 struct old_aead_alg *alg = crypto_old_aead_alg(aead);
Herbert Xu996d98d2015-05-21 15:11:01 +0800135
136 return old_crypt(req, alg->encrypt);
137}
138
139static int old_decrypt(struct aead_request *req)
140{
141 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu2d0f2302015-05-21 15:11:02 +0800142 struct old_aead_alg *alg = crypto_old_aead_alg(aead);
Herbert Xu996d98d2015-05-21 15:11:01 +0800143
144 return old_crypt(req, alg->decrypt);
145}
146
Herbert Xuaedb30d2007-12-12 19:27:25 +0800147static int no_givcrypt(struct aead_givcrypt_request *req)
Herbert Xu743edf52007-12-10 16:18:01 +0800148{
149 return -ENOSYS;
150}
151
Herbert Xu63293c62015-05-21 15:11:08 +0800152static int crypto_old_aead_init_tfm(struct crypto_tfm *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800153{
Herbert Xu2d0f2302015-05-21 15:11:02 +0800154 struct old_aead_alg *alg = &tfm->__crt_alg->cra_aead;
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800155 struct crypto_aead *crt = __crypto_aead_cast(tfm);
Herbert Xu1ae97822007-08-30 15:36:14 +0800156
Herbert Xu7ba683a2007-12-02 18:49:21 +1100157 if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
Herbert Xu1ae97822007-08-30 15:36:14 +0800158 return -EINVAL;
159
Herbert Xu63293c62015-05-21 15:11:08 +0800160 crt->setkey = alg->setkey;
161 crt->setauthsize = alg->setauthsize;
Herbert Xu996d98d2015-05-21 15:11:01 +0800162 crt->encrypt = old_encrypt;
163 crt->decrypt = old_decrypt;
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800164 if (alg->ivsize) {
165 crt->givencrypt = alg->givencrypt ?: no_givcrypt;
166 crt->givdecrypt = alg->givdecrypt ?: no_givcrypt;
167 } else {
168 crt->givencrypt = aead_null_givencrypt;
169 crt->givdecrypt = aead_null_givdecrypt;
170 }
171 crt->child = __crypto_aead_cast(tfm);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100172 crt->authsize = alg->maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800173
174 return 0;
175}
176
Herbert Xu5eb8ec62015-05-28 22:07:53 +0800177static void crypto_aead_exit_tfm(struct crypto_tfm *tfm)
178{
179 struct crypto_aead *aead = __crypto_aead_cast(tfm);
180 struct aead_alg *alg = crypto_aead_alg(aead);
181
182 alg->exit(aead);
183}
184
Herbert Xu63293c62015-05-21 15:11:08 +0800185static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
186{
187 struct crypto_aead *aead = __crypto_aead_cast(tfm);
188 struct aead_alg *alg = crypto_aead_alg(aead);
189
190 if (crypto_old_aead_alg(aead)->encrypt)
191 return crypto_old_aead_init_tfm(tfm);
192
193 aead->setkey = alg->setkey;
194 aead->setauthsize = alg->setauthsize;
195 aead->encrypt = alg->encrypt;
196 aead->decrypt = alg->decrypt;
197 aead->child = __crypto_aead_cast(tfm);
Herbert Xu63293c62015-05-21 15:11:08 +0800198 aead->authsize = alg->maxauthsize;
199
Herbert Xu5eb8ec62015-05-28 22:07:53 +0800200 if (alg->exit)
201 aead->base.exit = crypto_aead_exit_tfm;
202
203 if (alg->init)
204 return alg->init(aead);
205
Herbert Xu63293c62015-05-21 15:11:08 +0800206 return 0;
207}
208
Herbert Xu3acc8472011-11-03 23:46:07 +1100209#ifdef CONFIG_NET
Herbert Xu63293c62015-05-21 15:11:08 +0800210static int crypto_old_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
Steffen Klassert6ad414f2011-09-27 07:44:27 +0200211{
212 struct crypto_report_aead raead;
Herbert Xu2d0f2302015-05-21 15:11:02 +0800213 struct old_aead_alg *aead = &alg->cra_aead;
Steffen Klassert6ad414f2011-09-27 07:44:27 +0200214
Mathias Krause9a5467b2013-02-05 18:19:13 +0100215 strncpy(raead.type, "aead", sizeof(raead.type));
216 strncpy(raead.geniv, aead->geniv ?: "<built-in>", sizeof(raead.geniv));
Steffen Klassert6ad414f2011-09-27 07:44:27 +0200217
218 raead.blocksize = alg->cra_blocksize;
219 raead.maxauthsize = aead->maxauthsize;
220 raead.ivsize = aead->ivsize;
221
David S. Miller6662df32012-04-01 20:19:05 -0400222 if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD,
223 sizeof(struct crypto_report_aead), &raead))
224 goto nla_put_failure;
Steffen Klassert6ad414f2011-09-27 07:44:27 +0200225 return 0;
226
227nla_put_failure:
228 return -EMSGSIZE;
229}
Herbert Xu3acc8472011-11-03 23:46:07 +1100230#else
Herbert Xu63293c62015-05-21 15:11:08 +0800231static int crypto_old_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
232{
233 return -ENOSYS;
234}
235#endif
236
237static void crypto_old_aead_show(struct seq_file *m, struct crypto_alg *alg)
238 __attribute__ ((unused));
239static void crypto_old_aead_show(struct seq_file *m, struct crypto_alg *alg)
240{
241 struct old_aead_alg *aead = &alg->cra_aead;
242
243 seq_printf(m, "type : aead\n");
244 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
245 "yes" : "no");
246 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
247 seq_printf(m, "ivsize : %u\n", aead->ivsize);
248 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
249 seq_printf(m, "geniv : %s\n", aead->geniv ?: "<built-in>");
250}
251
252const struct crypto_type crypto_aead_type = {
253 .extsize = crypto_alg_extsize,
254 .init_tfm = crypto_aead_init_tfm,
255#ifdef CONFIG_PROC_FS
256 .show = crypto_old_aead_show,
257#endif
258 .report = crypto_old_aead_report,
259 .lookup = crypto_lookup_aead,
260 .maskclear = ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV),
261 .maskset = CRYPTO_ALG_TYPE_MASK,
262 .type = CRYPTO_ALG_TYPE_AEAD,
263 .tfmsize = offsetof(struct crypto_aead, base),
264};
265EXPORT_SYMBOL_GPL(crypto_aead_type);
266
267#ifdef CONFIG_NET
268static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
269{
270 struct crypto_report_aead raead;
271 struct aead_alg *aead = container_of(alg, struct aead_alg, base);
272
273 strncpy(raead.type, "aead", sizeof(raead.type));
274 strncpy(raead.geniv, "<none>", sizeof(raead.geniv));
275
276 raead.blocksize = alg->cra_blocksize;
277 raead.maxauthsize = aead->maxauthsize;
278 raead.ivsize = aead->ivsize;
279
280 if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD,
281 sizeof(struct crypto_report_aead), &raead))
282 goto nla_put_failure;
283 return 0;
284
285nla_put_failure:
286 return -EMSGSIZE;
287}
288#else
Herbert Xu3acc8472011-11-03 23:46:07 +1100289static int crypto_aead_report(struct sk_buff *skb, struct crypto_alg *alg)
290{
291 return -ENOSYS;
292}
293#endif
Steffen Klassert6ad414f2011-09-27 07:44:27 +0200294
Herbert Xu1ae97822007-08-30 15:36:14 +0800295static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
296 __attribute__ ((unused));
297static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
298{
Herbert Xu63293c62015-05-21 15:11:08 +0800299 struct aead_alg *aead = container_of(alg, struct aead_alg, base);
Herbert Xu1ae97822007-08-30 15:36:14 +0800300
301 seq_printf(m, "type : aead\n");
Herbert Xu189ed662007-12-14 22:29:37 +0800302 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
303 "yes" : "no");
Herbert Xu1ae97822007-08-30 15:36:14 +0800304 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
305 seq_printf(m, "ivsize : %u\n", aead->ivsize);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100306 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
Herbert Xu63293c62015-05-21 15:11:08 +0800307 seq_printf(m, "geniv : <none>\n");
Herbert Xu1ae97822007-08-30 15:36:14 +0800308}
309
Herbert Xu63293c62015-05-21 15:11:08 +0800310static const struct crypto_type crypto_new_aead_type = {
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800311 .extsize = crypto_alg_extsize,
312 .init_tfm = crypto_aead_init_tfm,
Herbert Xu1ae97822007-08-30 15:36:14 +0800313#ifdef CONFIG_PROC_FS
314 .show = crypto_aead_show,
315#endif
Steffen Klassert6ad414f2011-09-27 07:44:27 +0200316 .report = crypto_aead_report,
Herbert Xu63293c62015-05-21 15:11:08 +0800317 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800318 .maskset = CRYPTO_ALG_TYPE_MASK,
319 .type = CRYPTO_ALG_TYPE_AEAD,
320 .tfmsize = offsetof(struct crypto_aead, base),
Herbert Xu1ae97822007-08-30 15:36:14 +0800321};
Herbert Xu1ae97822007-08-30 15:36:14 +0800322
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800323static int aead_null_givencrypt(struct aead_givcrypt_request *req)
324{
325 return crypto_aead_encrypt(&req->areq);
326}
327
328static int aead_null_givdecrypt(struct aead_givcrypt_request *req)
329{
330 return crypto_aead_decrypt(&req->areq);
331}
332
Herbert Xu3acc8472011-11-03 23:46:07 +1100333#ifdef CONFIG_NET
Steffen Klassertb735d0a2011-09-27 07:45:44 +0200334static int crypto_nivaead_report(struct sk_buff *skb, struct crypto_alg *alg)
335{
336 struct crypto_report_aead raead;
Herbert Xu2d0f2302015-05-21 15:11:02 +0800337 struct old_aead_alg *aead = &alg->cra_aead;
Steffen Klassertb735d0a2011-09-27 07:45:44 +0200338
Mathias Krause9a5467b2013-02-05 18:19:13 +0100339 strncpy(raead.type, "nivaead", sizeof(raead.type));
340 strncpy(raead.geniv, aead->geniv, sizeof(raead.geniv));
Steffen Klassertb735d0a2011-09-27 07:45:44 +0200341
342 raead.blocksize = alg->cra_blocksize;
343 raead.maxauthsize = aead->maxauthsize;
344 raead.ivsize = aead->ivsize;
345
David S. Miller6662df32012-04-01 20:19:05 -0400346 if (nla_put(skb, CRYPTOCFGA_REPORT_AEAD,
347 sizeof(struct crypto_report_aead), &raead))
348 goto nla_put_failure;
Steffen Klassertb735d0a2011-09-27 07:45:44 +0200349 return 0;
350
351nla_put_failure:
352 return -EMSGSIZE;
353}
Herbert Xu3acc8472011-11-03 23:46:07 +1100354#else
355static int crypto_nivaead_report(struct sk_buff *skb, struct crypto_alg *alg)
356{
357 return -ENOSYS;
358}
359#endif
Steffen Klassertb735d0a2011-09-27 07:45:44 +0200360
361
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800362static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
363 __attribute__ ((unused));
364static void crypto_nivaead_show(struct seq_file *m, struct crypto_alg *alg)
365{
Herbert Xu2d0f2302015-05-21 15:11:02 +0800366 struct old_aead_alg *aead = &alg->cra_aead;
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800367
368 seq_printf(m, "type : nivaead\n");
Herbert Xu189ed662007-12-14 22:29:37 +0800369 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
370 "yes" : "no");
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800371 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
372 seq_printf(m, "ivsize : %u\n", aead->ivsize);
373 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
374 seq_printf(m, "geniv : %s\n", aead->geniv);
375}
376
377const struct crypto_type crypto_nivaead_type = {
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800378 .extsize = crypto_alg_extsize,
379 .init_tfm = crypto_aead_init_tfm,
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800380#ifdef CONFIG_PROC_FS
381 .show = crypto_nivaead_show,
382#endif
Steffen Klassertb735d0a2011-09-27 07:45:44 +0200383 .report = crypto_nivaead_report,
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800384 .maskclear = ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV),
385 .maskset = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV,
386 .type = CRYPTO_ALG_TYPE_AEAD,
387 .tfmsize = offsetof(struct crypto_aead, base),
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800388};
389EXPORT_SYMBOL_GPL(crypto_nivaead_type);
390
391static int crypto_grab_nivaead(struct crypto_aead_spawn *spawn,
392 const char *name, u32 type, u32 mask)
393{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800394 spawn->base.frontend = &crypto_nivaead_type;
395 return crypto_grab_spawn(&spawn->base, name, type, mask);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800396}
397
Herbert Xu63504492015-05-27 14:37:30 +0800398static int aead_geniv_setkey(struct crypto_aead *tfm,
399 const u8 *key, unsigned int keylen)
400{
401 struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
402
403 return crypto_aead_setkey(ctx->child, key, keylen);
404}
405
406static int aead_geniv_setauthsize(struct crypto_aead *tfm,
407 unsigned int authsize)
408{
409 struct aead_geniv_ctx *ctx = crypto_aead_ctx(tfm);
410
411 return crypto_aead_setauthsize(ctx->child, authsize);
412}
413
414static void compat_encrypt_complete2(struct aead_request *req, int err)
415{
416 struct compat_request_ctx *rctx = aead_request_ctx(req);
417 struct aead_givcrypt_request *subreq = &rctx->subreq;
418 struct crypto_aead *geniv;
419
420 if (err == -EINPROGRESS)
421 return;
422
423 if (err)
424 goto out;
425
426 geniv = crypto_aead_reqtfm(req);
427 scatterwalk_map_and_copy(subreq->giv, rctx->ivsg, 0,
428 crypto_aead_ivsize(geniv), 1);
429
430out:
431 kzfree(subreq->giv);
432}
433
434static void compat_encrypt_complete(struct crypto_async_request *base, int err)
435{
436 struct aead_request *req = base->data;
437
438 compat_encrypt_complete2(req, err);
439 aead_request_complete(req, err);
440}
441
442static int compat_encrypt(struct aead_request *req)
443{
444 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
445 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
446 struct compat_request_ctx *rctx = aead_request_ctx(req);
447 struct aead_givcrypt_request *subreq = &rctx->subreq;
448 unsigned int ivsize = crypto_aead_ivsize(geniv);
449 struct scatterlist *src, *dst;
450 crypto_completion_t compl;
451 void *data;
452 u8 *info;
453 __be64 seq;
454 int err;
455
456 if (req->cryptlen < ivsize)
457 return -EINVAL;
458
459 compl = req->base.complete;
460 data = req->base.data;
461
462 rctx->ivsg = scatterwalk_ffwd(rctx->ivbuf, req->dst, req->assoclen);
463 info = PageHighMem(sg_page(rctx->ivsg)) ? NULL : sg_virt(rctx->ivsg);
464
465 if (!info) {
466 info = kmalloc(ivsize, req->base.flags &
467 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
468 GFP_ATOMIC);
469 if (!info)
470 return -ENOMEM;
471
472 compl = compat_encrypt_complete;
473 data = req;
474 }
475
476 memcpy(&seq, req->iv + ivsize - sizeof(seq), sizeof(seq));
477
478 src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen + ivsize);
479 dst = req->src == req->dst ?
480 src : scatterwalk_ffwd(rctx->dst, rctx->ivsg, ivsize);
481
482 aead_givcrypt_set_tfm(subreq, ctx->child);
483 aead_givcrypt_set_callback(subreq, req->base.flags,
484 req->base.complete, req->base.data);
485 aead_givcrypt_set_crypt(subreq, src, dst,
486 req->cryptlen - ivsize, req->iv);
487 aead_givcrypt_set_assoc(subreq, req->src, req->assoclen);
488 aead_givcrypt_set_giv(subreq, info, be64_to_cpu(seq));
489
490 err = crypto_aead_givencrypt(subreq);
491 if (unlikely(PageHighMem(sg_page(rctx->ivsg))))
492 compat_encrypt_complete2(req, err);
493 return err;
494}
495
496static int compat_decrypt(struct aead_request *req)
497{
498 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
499 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
500 struct compat_request_ctx *rctx = aead_request_ctx(req);
501 struct aead_request *subreq = &rctx->subreq.areq;
502 unsigned int ivsize = crypto_aead_ivsize(geniv);
503 struct scatterlist *src, *dst;
504 crypto_completion_t compl;
505 void *data;
506
507 if (req->cryptlen < ivsize)
508 return -EINVAL;
509
510 aead_request_set_tfm(subreq, ctx->child);
511
512 compl = req->base.complete;
513 data = req->base.data;
514
515 src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen + ivsize);
516 dst = req->src == req->dst ?
517 src : scatterwalk_ffwd(rctx->dst, req->dst,
518 req->assoclen + ivsize);
519
520 aead_request_set_callback(subreq, req->base.flags, compl, data);
521 aead_request_set_crypt(subreq, src, dst,
522 req->cryptlen - ivsize, req->iv);
523 aead_request_set_assoc(subreq, req->src, req->assoclen);
524
525 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
526
527 return crypto_aead_decrypt(subreq);
528}
529
530static int compat_encrypt_first(struct aead_request *req)
531{
532 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
533 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
534 int err = 0;
535
536 spin_lock_bh(&ctx->lock);
537 if (geniv->encrypt != compat_encrypt_first)
538 goto unlock;
539
540 geniv->encrypt = compat_encrypt;
541
542unlock:
543 spin_unlock_bh(&ctx->lock);
544
545 if (err)
546 return err;
547
548 return compat_encrypt(req);
549}
550
551static int aead_geniv_init_compat(struct crypto_tfm *tfm)
552{
553 struct crypto_aead *geniv = __crypto_aead_cast(tfm);
554 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
555 int err;
556
557 spin_lock_init(&ctx->lock);
558
559 crypto_aead_set_reqsize(geniv, sizeof(struct compat_request_ctx));
560
561 err = aead_geniv_init(tfm);
562
563 ctx->child = geniv->child;
564 geniv->child = geniv;
565
566 return err;
567}
568
569static void aead_geniv_exit_compat(struct crypto_tfm *tfm)
570{
571 struct crypto_aead *geniv = __crypto_aead_cast(tfm);
572 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
573
574 crypto_free_aead(ctx->child);
575}
576
Herbert Xu856e3f402015-05-21 15:11:13 +0800577struct aead_instance *aead_geniv_alloc(struct crypto_template *tmpl,
578 struct rtattr **tb, u32 type, u32 mask)
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800579{
580 const char *name;
581 struct crypto_aead_spawn *spawn;
582 struct crypto_attr_type *algt;
Herbert Xu856e3f402015-05-21 15:11:13 +0800583 struct aead_instance *inst;
584 struct aead_alg *alg;
585 unsigned int ivsize;
586 unsigned int maxauthsize;
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800587 int err;
588
589 algt = crypto_get_attr_type(tb);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800590 if (IS_ERR(algt))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100591 return ERR_CAST(algt);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800592
593 if ((algt->type ^ (CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV)) &
594 algt->mask)
595 return ERR_PTR(-EINVAL);
596
597 name = crypto_attr_alg_name(tb[1]);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800598 if (IS_ERR(name))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100599 return ERR_CAST(name);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800600
601 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
602 if (!inst)
603 return ERR_PTR(-ENOMEM);
604
Herbert Xu856e3f402015-05-21 15:11:13 +0800605 spawn = aead_instance_ctx(inst);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800606
607 /* Ignore async algorithms if necessary. */
608 mask |= crypto_requires_sync(algt->type, algt->mask);
609
Herbert Xu856e3f402015-05-21 15:11:13 +0800610 crypto_set_aead_spawn(spawn, aead_crypto_instance(inst));
Herbert Xu63504492015-05-27 14:37:30 +0800611 err = (algt->mask & CRYPTO_ALG_GENIV) ?
612 crypto_grab_nivaead(spawn, name, type, mask) :
613 crypto_grab_aead(spawn, name, type, mask);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800614 if (err)
615 goto err_free_inst;
616
Herbert Xu856e3f402015-05-21 15:11:13 +0800617 alg = crypto_spawn_aead_alg(spawn);
618
Herbert Xu30e4c012015-05-22 16:30:48 +0800619 ivsize = crypto_aead_alg_ivsize(alg);
620 maxauthsize = crypto_aead_alg_maxauthsize(alg);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800621
622 err = -EINVAL;
Herbert Xu63504492015-05-27 14:37:30 +0800623 if (ivsize < sizeof(u64))
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800624 goto err_drop_alg;
625
626 /*
627 * This is only true if we're constructing an algorithm with its
628 * default IV generator. For the default generator we elide the
629 * template name and double-check the IV generator.
630 */
631 if (algt->mask & CRYPTO_ALG_GENIV) {
Herbert Xu856e3f402015-05-21 15:11:13 +0800632 if (!alg->base.cra_aead.encrypt)
633 goto err_drop_alg;
634 if (strcmp(tmpl->name, alg->base.cra_aead.geniv))
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800635 goto err_drop_alg;
636
Herbert Xu856e3f402015-05-21 15:11:13 +0800637 memcpy(inst->alg.base.cra_name, alg->base.cra_name,
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800638 CRYPTO_MAX_ALG_NAME);
Herbert Xu856e3f402015-05-21 15:11:13 +0800639 memcpy(inst->alg.base.cra_driver_name,
640 alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME);
641
642 inst->alg.base.cra_flags = CRYPTO_ALG_TYPE_AEAD |
643 CRYPTO_ALG_GENIV;
644 inst->alg.base.cra_flags |= alg->base.cra_flags &
645 CRYPTO_ALG_ASYNC;
646 inst->alg.base.cra_priority = alg->base.cra_priority;
647 inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
648 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
649 inst->alg.base.cra_type = &crypto_aead_type;
650
651 inst->alg.base.cra_aead.ivsize = ivsize;
652 inst->alg.base.cra_aead.maxauthsize = maxauthsize;
653
654 inst->alg.base.cra_aead.setkey = alg->base.cra_aead.setkey;
655 inst->alg.base.cra_aead.setauthsize =
656 alg->base.cra_aead.setauthsize;
657 inst->alg.base.cra_aead.encrypt = alg->base.cra_aead.encrypt;
658 inst->alg.base.cra_aead.decrypt = alg->base.cra_aead.decrypt;
659
660 goto out;
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800661 }
662
Herbert Xu856e3f402015-05-21 15:11:13 +0800663 err = -ENAMETOOLONG;
664 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
665 "%s(%s)", tmpl->name, alg->base.cra_name) >=
666 CRYPTO_MAX_ALG_NAME)
667 goto err_drop_alg;
668 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
669 "%s(%s)", tmpl->name, alg->base.cra_driver_name) >=
670 CRYPTO_MAX_ALG_NAME)
671 goto err_drop_alg;
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800672
Herbert Xud1ee1f02015-05-23 15:41:53 +0800673 inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
Herbert Xu856e3f402015-05-21 15:11:13 +0800674 inst->alg.base.cra_priority = alg->base.cra_priority;
675 inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
676 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
Herbert Xu63504492015-05-27 14:37:30 +0800677 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
678
679 inst->alg.setkey = aead_geniv_setkey;
680 inst->alg.setauthsize = aead_geniv_setauthsize;
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800681
Herbert Xu856e3f402015-05-21 15:11:13 +0800682 inst->alg.ivsize = ivsize;
683 inst->alg.maxauthsize = maxauthsize;
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800684
Herbert Xu63504492015-05-27 14:37:30 +0800685 inst->alg.encrypt = compat_encrypt_first;
686 inst->alg.decrypt = compat_decrypt;
687
688 inst->alg.base.cra_init = aead_geniv_init_compat;
689 inst->alg.base.cra_exit = aead_geniv_exit_compat;
690
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800691out:
692 return inst;
693
694err_drop_alg:
695 crypto_drop_aead(spawn);
696err_free_inst:
697 kfree(inst);
698 inst = ERR_PTR(err);
699 goto out;
700}
701EXPORT_SYMBOL_GPL(aead_geniv_alloc);
702
Herbert Xu856e3f402015-05-21 15:11:13 +0800703void aead_geniv_free(struct aead_instance *inst)
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800704{
Herbert Xu856e3f402015-05-21 15:11:13 +0800705 crypto_drop_aead(aead_instance_ctx(inst));
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800706 kfree(inst);
707}
708EXPORT_SYMBOL_GPL(aead_geniv_free);
709
710int aead_geniv_init(struct crypto_tfm *tfm)
711{
712 struct crypto_instance *inst = (void *)tfm->__crt_alg;
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800713 struct crypto_aead *child;
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800714 struct crypto_aead *aead;
715
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800716 aead = __crypto_aead_cast(tfm);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800717
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800718 child = crypto_spawn_aead(crypto_instance_ctx(inst));
719 if (IS_ERR(child))
720 return PTR_ERR(child);
721
722 aead->child = child;
723 aead->reqsize += crypto_aead_reqsize(child);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800724
725 return 0;
726}
727EXPORT_SYMBOL_GPL(aead_geniv_init);
728
729void aead_geniv_exit(struct crypto_tfm *tfm)
730{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800731 crypto_free_aead(__crypto_aead_cast(tfm)->child);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800732}
733EXPORT_SYMBOL_GPL(aead_geniv_exit);
734
Herbert Xud29ce982007-12-12 19:24:27 +0800735static int crypto_nivaead_default(struct crypto_alg *alg, u32 type, u32 mask)
736{
737 struct rtattr *tb[3];
738 struct {
739 struct rtattr attr;
740 struct crypto_attr_type data;
741 } ptype;
742 struct {
743 struct rtattr attr;
744 struct crypto_attr_alg data;
745 } palg;
746 struct crypto_template *tmpl;
747 struct crypto_instance *inst;
748 struct crypto_alg *larval;
749 const char *geniv;
750 int err;
751
752 larval = crypto_larval_lookup(alg->cra_driver_name,
753 CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_GENIV,
754 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
755 err = PTR_ERR(larval);
756 if (IS_ERR(larval))
757 goto out;
758
759 err = -EAGAIN;
760 if (!crypto_is_larval(larval))
761 goto drop_larval;
762
763 ptype.attr.rta_len = sizeof(ptype);
764 ptype.attr.rta_type = CRYPTOA_TYPE;
765 ptype.data.type = type | CRYPTO_ALG_GENIV;
766 /* GENIV tells the template that we're making a default geniv. */
767 ptype.data.mask = mask | CRYPTO_ALG_GENIV;
768 tb[0] = &ptype.attr;
769
770 palg.attr.rta_len = sizeof(palg);
771 palg.attr.rta_type = CRYPTOA_ALG;
772 /* Must use the exact name to locate ourselves. */
773 memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
774 tb[1] = &palg.attr;
775
776 tb[2] = NULL;
777
778 geniv = alg->cra_aead.geniv;
779
780 tmpl = crypto_lookup_template(geniv);
781 err = -ENOENT;
782 if (!tmpl)
783 goto kill_larval;
784
Herbert Xu72af8652015-05-23 15:41:49 +0800785 if (tmpl->create) {
786 err = tmpl->create(tmpl, tb);
787 if (err)
788 goto put_tmpl;
789 goto ok;
790 }
791
Herbert Xud29ce982007-12-12 19:24:27 +0800792 inst = tmpl->alloc(tb);
793 err = PTR_ERR(inst);
794 if (IS_ERR(inst))
795 goto put_tmpl;
796
Joshua I. James4fad4782014-12-05 14:24:44 +0900797 err = crypto_register_instance(tmpl, inst);
798 if (err) {
Herbert Xud29ce982007-12-12 19:24:27 +0800799 tmpl->free(inst);
800 goto put_tmpl;
801 }
802
Herbert Xu72af8652015-05-23 15:41:49 +0800803ok:
Herbert Xud29ce982007-12-12 19:24:27 +0800804 /* Redo the lookup to use the instance we just registered. */
805 err = -EAGAIN;
806
807put_tmpl:
808 crypto_tmpl_put(tmpl);
809kill_larval:
810 crypto_larval_kill(larval);
811drop_larval:
812 crypto_mod_put(larval);
813out:
814 crypto_mod_put(alg);
815 return err;
816}
817
Steffen Klassert1e122992012-03-29 09:03:47 +0200818struct crypto_alg *crypto_lookup_aead(const char *name, u32 type, u32 mask)
Herbert Xud29ce982007-12-12 19:24:27 +0800819{
820 struct crypto_alg *alg;
821
822 alg = crypto_alg_mod_lookup(name, type, mask);
823 if (IS_ERR(alg))
824 return alg;
825
826 if (alg->cra_type == &crypto_aead_type)
827 return alg;
828
829 if (!alg->cra_aead.ivsize)
830 return alg;
831
Herbert Xu5852ae42009-02-18 20:41:47 +0800832 crypto_mod_put(alg);
833 alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
834 mask & ~CRYPTO_ALG_TESTED);
835 if (IS_ERR(alg))
836 return alg;
837
838 if (alg->cra_type == &crypto_aead_type) {
Herbert Xu80f7b352015-04-23 16:37:46 +0800839 if (~alg->cra_flags & (type ^ ~mask) & CRYPTO_ALG_TESTED) {
Herbert Xu5852ae42009-02-18 20:41:47 +0800840 crypto_mod_put(alg);
841 alg = ERR_PTR(-ENOENT);
842 }
843 return alg;
844 }
845
846 BUG_ON(!alg->cra_aead.ivsize);
847
Herbert Xud29ce982007-12-12 19:24:27 +0800848 return ERR_PTR(crypto_nivaead_default(alg, type, mask));
849}
Steffen Klassert1e122992012-03-29 09:03:47 +0200850EXPORT_SYMBOL_GPL(crypto_lookup_aead);
Herbert Xud29ce982007-12-12 19:24:27 +0800851
852int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
853 u32 type, u32 mask)
854{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800855 spawn->base.frontend = &crypto_aead_type;
856 return crypto_grab_spawn(&spawn->base, name, type, mask);
Herbert Xud29ce982007-12-12 19:24:27 +0800857}
858EXPORT_SYMBOL_GPL(crypto_grab_aead);
859
860struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask)
861{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800862 return crypto_alloc_tfm(alg_name, &crypto_aead_type, type, mask);
Herbert Xud29ce982007-12-12 19:24:27 +0800863}
864EXPORT_SYMBOL_GPL(crypto_alloc_aead);
865
Herbert Xu63293c62015-05-21 15:11:08 +0800866static int aead_prepare_alg(struct aead_alg *alg)
867{
868 struct crypto_alg *base = &alg->base;
869
870 if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
871 return -EINVAL;
872
873 base->cra_type = &crypto_new_aead_type;
874 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
875 base->cra_flags |= CRYPTO_ALG_TYPE_AEAD;
876
877 return 0;
878}
879
880int crypto_register_aead(struct aead_alg *alg)
881{
882 struct crypto_alg *base = &alg->base;
883 int err;
884
885 err = aead_prepare_alg(alg);
886 if (err)
887 return err;
888
889 return crypto_register_alg(base);
890}
891EXPORT_SYMBOL_GPL(crypto_register_aead);
892
Herbert Xu43615362015-05-28 22:07:57 +0800893void crypto_unregister_aead(struct aead_alg *alg)
Herbert Xu63293c62015-05-21 15:11:08 +0800894{
Herbert Xu43615362015-05-28 22:07:57 +0800895 crypto_unregister_alg(&alg->base);
Herbert Xu63293c62015-05-21 15:11:08 +0800896}
897EXPORT_SYMBOL_GPL(crypto_unregister_aead);
898
Herbert Xucaab9462015-05-28 22:07:59 +0800899int crypto_register_aeads(struct aead_alg *algs, int count)
900{
901 int i, ret;
902
903 for (i = 0; i < count; i++) {
904 ret = crypto_register_aead(&algs[i]);
905 if (ret)
906 goto err;
907 }
908
909 return 0;
910
911err:
912 for (--i; i >= 0; --i)
913 crypto_unregister_aead(&algs[i]);
914
915 return ret;
916}
917EXPORT_SYMBOL_GPL(crypto_register_aeads);
918
919void crypto_unregister_aeads(struct aead_alg *algs, int count)
920{
921 int i;
922
923 for (i = count - 1; i >= 0; --i)
924 crypto_unregister_aead(&algs[i]);
925}
926EXPORT_SYMBOL_GPL(crypto_unregister_aeads);
927
Herbert Xu63293c62015-05-21 15:11:08 +0800928int aead_register_instance(struct crypto_template *tmpl,
929 struct aead_instance *inst)
930{
931 int err;
932
933 err = aead_prepare_alg(&inst->alg);
934 if (err)
935 return err;
936
937 return crypto_register_instance(tmpl, aead_crypto_instance(inst));
938}
939EXPORT_SYMBOL_GPL(aead_register_instance);
940
Herbert Xu1ae97822007-08-30 15:36:14 +0800941MODULE_LICENSE("GPL");
942MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");