blob: 42e4ee532d35830cec2689db47d849660acd3120 [file] [log] [blame]
Herbert Xu0a270322007-11-30 21:38:37 +11001/*
2 * seqiv: Sequence Number IV Generator
3 *
4 * This generator generates an IV based on a sequence number by xoring it
5 * with a salt. This algorithm is mainly useful for CTR and similar modes.
6 *
7 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 */
15
Herbert Xu661cfd02015-05-27 14:37:37 +080016#include <crypto/internal/geniv.h>
Herbert Xu0a270322007-11-30 21:38:37 +110017#include <crypto/internal/skcipher.h>
Herbert Xu856e3f402015-05-21 15:11:13 +080018#include <crypto/null.h>
Herbert Xua0f000e2008-08-14 22:21:31 +100019#include <crypto/rng.h>
Herbert Xu856e3f402015-05-21 15:11:13 +080020#include <crypto/scatterwalk.h>
Herbert Xu0a270322007-11-30 21:38:37 +110021#include <linux/err.h>
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Herbert Xu0a270322007-11-30 21:38:37 +110026#include <linux/spinlock.h>
27#include <linux/string.h>
28
Herbert Xudd044462015-05-23 15:41:56 +080029struct seqniv_request_ctx {
30 struct scatterlist dst[2];
31 struct aead_request subreq;
32};
33
Herbert Xu0a270322007-11-30 21:38:37 +110034struct seqiv_ctx {
35 spinlock_t lock;
36 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
37};
38
Herbert Xu856e3f402015-05-21 15:11:13 +080039struct seqiv_aead_ctx {
Herbert Xu661cfd02015-05-27 14:37:37 +080040 /* aead_geniv_ctx must be first the element */
41 struct aead_geniv_ctx geniv;
Herbert Xu856e3f402015-05-21 15:11:13 +080042 struct crypto_blkcipher *null;
43 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
44};
45
Herbert Xu06771572015-05-23 15:41:51 +080046static void seqiv_free(struct crypto_instance *inst);
47
Herbert Xu0a270322007-11-30 21:38:37 +110048static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err)
49{
50 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
51 struct crypto_ablkcipher *geniv;
52
53 if (err == -EINPROGRESS)
54 return;
55
56 if (err)
57 goto out;
58
59 geniv = skcipher_givcrypt_reqtfm(req);
60 memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv));
61
62out:
63 kfree(subreq->info);
64}
65
66static void seqiv_complete(struct crypto_async_request *base, int err)
67{
68 struct skcipher_givcrypt_request *req = base->data;
69
70 seqiv_complete2(req, err);
71 skcipher_givcrypt_complete(req, err);
72}
73
Herbert Xu14df4d82007-12-12 12:27:26 +080074static void seqiv_aead_complete2(struct aead_givcrypt_request *req, int err)
75{
76 struct aead_request *subreq = aead_givcrypt_reqctx(req);
77 struct crypto_aead *geniv;
78
79 if (err == -EINPROGRESS)
80 return;
81
82 if (err)
83 goto out;
84
85 geniv = aead_givcrypt_reqtfm(req);
86 memcpy(req->areq.iv, subreq->iv, crypto_aead_ivsize(geniv));
87
88out:
89 kfree(subreq->iv);
90}
91
92static void seqiv_aead_complete(struct crypto_async_request *base, int err)
93{
94 struct aead_givcrypt_request *req = base->data;
95
96 seqiv_aead_complete2(req, err);
97 aead_givcrypt_complete(req, err);
98}
99
Herbert Xu856e3f402015-05-21 15:11:13 +0800100static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
101{
102 struct aead_request *subreq = aead_request_ctx(req);
103 struct crypto_aead *geniv;
104
105 if (err == -EINPROGRESS)
106 return;
107
108 if (err)
109 goto out;
110
111 geniv = crypto_aead_reqtfm(req);
112 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
113
114out:
115 kzfree(subreq->iv);
116}
117
118static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
119 int err)
120{
121 struct aead_request *req = base->data;
122
123 seqiv_aead_encrypt_complete2(req, err);
124 aead_request_complete(req, err);
125}
126
Herbert Xudd044462015-05-23 15:41:56 +0800127static void seqniv_aead_encrypt_complete2(struct aead_request *req, int err)
128{
129 unsigned int ivsize = 8;
130 u8 data[20];
131
132 if (err == -EINPROGRESS)
133 return;
134
135 /* Swap IV and ESP header back to correct order. */
136 scatterwalk_map_and_copy(data, req->dst, 0, req->assoclen + ivsize, 0);
137 scatterwalk_map_and_copy(data + ivsize, req->dst, 0, req->assoclen, 1);
138 scatterwalk_map_and_copy(data, req->dst, req->assoclen, ivsize, 1);
139}
140
141static void seqniv_aead_encrypt_complete(struct crypto_async_request *base,
142 int err)
143{
144 struct aead_request *req = base->data;
145
146 seqniv_aead_encrypt_complete2(req, err);
147 aead_request_complete(req, err);
148}
149
150static void seqniv_aead_decrypt_complete2(struct aead_request *req, int err)
151{
152 u8 data[4];
153
154 if (err == -EINPROGRESS)
155 return;
156
157 /* Move ESP header back to correct location. */
158 scatterwalk_map_and_copy(data, req->dst, 16, req->assoclen - 8, 0);
159 scatterwalk_map_and_copy(data, req->dst, 8, req->assoclen - 8, 1);
160}
161
162static void seqniv_aead_decrypt_complete(struct crypto_async_request *base,
163 int err)
164{
165 struct aead_request *req = base->data;
166
167 seqniv_aead_decrypt_complete2(req, err);
168 aead_request_complete(req, err);
169}
170
Herbert Xu14df4d82007-12-12 12:27:26 +0800171static void seqiv_geniv(struct seqiv_ctx *ctx, u8 *info, u64 seq,
172 unsigned int ivsize)
173{
174 unsigned int len = ivsize;
175
176 if (ivsize > sizeof(u64)) {
177 memset(info, 0, ivsize - sizeof(u64));
178 len = sizeof(u64);
179 }
180 seq = cpu_to_be64(seq);
181 memcpy(info + ivsize - len, &seq, len);
182 crypto_xor(info, ctx->salt, ivsize);
183}
184
Herbert Xu0a270322007-11-30 21:38:37 +1100185static int seqiv_givencrypt(struct skcipher_givcrypt_request *req)
186{
187 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
188 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
189 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
Mark Rustad3e3dc252014-07-25 02:53:38 -0700190 crypto_completion_t compl;
Herbert Xu0a270322007-11-30 21:38:37 +1100191 void *data;
192 u8 *info;
Herbert Xu0a270322007-11-30 21:38:37 +1100193 unsigned int ivsize;
Herbert Xu0a270322007-11-30 21:38:37 +1100194 int err;
195
196 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
197
Mark Rustad3e3dc252014-07-25 02:53:38 -0700198 compl = req->creq.base.complete;
Herbert Xu0a270322007-11-30 21:38:37 +1100199 data = req->creq.base.data;
200 info = req->creq.info;
201
202 ivsize = crypto_ablkcipher_ivsize(geniv);
203
204 if (unlikely(!IS_ALIGNED((unsigned long)info,
205 crypto_ablkcipher_alignmask(geniv) + 1))) {
206 info = kmalloc(ivsize, req->creq.base.flags &
207 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
208 GFP_ATOMIC);
209 if (!info)
210 return -ENOMEM;
211
Mark Rustad3e3dc252014-07-25 02:53:38 -0700212 compl = seqiv_complete;
Herbert Xu0a270322007-11-30 21:38:37 +1100213 data = req;
214 }
215
Mark Rustad3e3dc252014-07-25 02:53:38 -0700216 ablkcipher_request_set_callback(subreq, req->creq.base.flags, compl,
Herbert Xu0a270322007-11-30 21:38:37 +1100217 data);
218 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
219 req->creq.nbytes, info);
220
Herbert Xu14df4d82007-12-12 12:27:26 +0800221 seqiv_geniv(ctx, info, req->seq, ivsize);
Herbert Xu0a270322007-11-30 21:38:37 +1100222 memcpy(req->giv, info, ivsize);
223
224 err = crypto_ablkcipher_encrypt(subreq);
225 if (unlikely(info != req->creq.info))
226 seqiv_complete2(req, err);
227 return err;
228}
229
Herbert Xu14df4d82007-12-12 12:27:26 +0800230static int seqiv_aead_givencrypt(struct aead_givcrypt_request *req)
231{
232 struct crypto_aead *geniv = aead_givcrypt_reqtfm(req);
233 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
234 struct aead_request *areq = &req->areq;
235 struct aead_request *subreq = aead_givcrypt_reqctx(req);
Mark Rustad3e3dc252014-07-25 02:53:38 -0700236 crypto_completion_t compl;
Herbert Xu14df4d82007-12-12 12:27:26 +0800237 void *data;
238 u8 *info;
239 unsigned int ivsize;
240 int err;
241
242 aead_request_set_tfm(subreq, aead_geniv_base(geniv));
243
Mark Rustad3e3dc252014-07-25 02:53:38 -0700244 compl = areq->base.complete;
Herbert Xu14df4d82007-12-12 12:27:26 +0800245 data = areq->base.data;
246 info = areq->iv;
247
248 ivsize = crypto_aead_ivsize(geniv);
249
250 if (unlikely(!IS_ALIGNED((unsigned long)info,
251 crypto_aead_alignmask(geniv) + 1))) {
252 info = kmalloc(ivsize, areq->base.flags &
253 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
254 GFP_ATOMIC);
255 if (!info)
256 return -ENOMEM;
257
Mark Rustad3e3dc252014-07-25 02:53:38 -0700258 compl = seqiv_aead_complete;
Herbert Xu14df4d82007-12-12 12:27:26 +0800259 data = req;
260 }
261
Mark Rustad3e3dc252014-07-25 02:53:38 -0700262 aead_request_set_callback(subreq, areq->base.flags, compl, data);
Herbert Xu14df4d82007-12-12 12:27:26 +0800263 aead_request_set_crypt(subreq, areq->src, areq->dst, areq->cryptlen,
264 info);
265 aead_request_set_assoc(subreq, areq->assoc, areq->assoclen);
266
267 seqiv_geniv(ctx, info, req->seq, ivsize);
268 memcpy(req->giv, info, ivsize);
269
270 err = crypto_aead_encrypt(subreq);
271 if (unlikely(info != areq->iv))
272 seqiv_aead_complete2(req, err);
273 return err;
274}
275
Herbert Xu661cfd02015-05-27 14:37:37 +0800276static int seqniv_aead_encrypt(struct aead_request *req)
Herbert Xu856e3f402015-05-21 15:11:13 +0800277{
278 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
279 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xudd044462015-05-23 15:41:56 +0800280 struct seqniv_request_ctx *rctx = aead_request_ctx(req);
281 struct aead_request *subreq = &rctx->subreq;
282 struct scatterlist *dst;
Herbert Xu856e3f402015-05-21 15:11:13 +0800283 crypto_completion_t compl;
284 void *data;
Herbert Xudd044462015-05-23 15:41:56 +0800285 unsigned int ivsize = 8;
286 u8 buf[20] __attribute__ ((aligned(__alignof__(u32))));
Herbert Xu856e3f402015-05-21 15:11:13 +0800287 int err;
288
Herbert Xudd044462015-05-23 15:41:56 +0800289 if (req->cryptlen < ivsize)
290 return -EINVAL;
291
292 /* ESP AD is at most 12 bytes (ESN). */
293 if (req->assoclen > 12)
294 return -EINVAL;
295
Herbert Xu661cfd02015-05-27 14:37:37 +0800296 aead_request_set_tfm(subreq, ctx->geniv.child);
Herbert Xu856e3f402015-05-21 15:11:13 +0800297
Herbert Xudd044462015-05-23 15:41:56 +0800298 compl = seqniv_aead_encrypt_complete;
299 data = req;
Herbert Xu856e3f402015-05-21 15:11:13 +0800300
Herbert Xudd044462015-05-23 15:41:56 +0800301 if (req->src != req->dst) {
Herbert Xudd044462015-05-23 15:41:56 +0800302 struct blkcipher_desc desc = {
303 .tfm = ctx->null,
304 };
Herbert Xu856e3f402015-05-21 15:11:13 +0800305
Herbert Xud0ad1b22015-05-27 14:37:36 +0800306 err = crypto_blkcipher_encrypt(&desc, req->dst, req->src,
307 req->assoclen + req->cryptlen);
Herbert Xudd044462015-05-23 15:41:56 +0800308 if (err)
309 return err;
Herbert Xu856e3f402015-05-21 15:11:13 +0800310 }
311
Herbert Xudd044462015-05-23 15:41:56 +0800312 dst = scatterwalk_ffwd(rctx->dst, req->dst, ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800313
Herbert Xudd044462015-05-23 15:41:56 +0800314 aead_request_set_callback(subreq, req->base.flags, compl, data);
315 aead_request_set_crypt(subreq, dst, dst,
316 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800317 aead_request_set_ad(subreq, req->assoclen);
Herbert Xudd044462015-05-23 15:41:56 +0800318
319 memcpy(buf, req->iv, ivsize);
320 crypto_xor(buf, ctx->salt, ivsize);
321 memcpy(req->iv, buf, ivsize);
322
323 /* Swap order of IV and ESP AD for ICV generation. */
324 scatterwalk_map_and_copy(buf + ivsize, req->dst, 0, req->assoclen, 0);
325 scatterwalk_map_and_copy(buf, req->dst, 0, req->assoclen + ivsize, 1);
Herbert Xu856e3f402015-05-21 15:11:13 +0800326
327 err = crypto_aead_encrypt(subreq);
Herbert Xudd044462015-05-23 15:41:56 +0800328 seqniv_aead_encrypt_complete2(req, err);
Herbert Xu856e3f402015-05-21 15:11:13 +0800329 return err;
330}
331
332static int seqiv_aead_encrypt(struct aead_request *req)
333{
334 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
335 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
336 struct aead_request *subreq = aead_request_ctx(req);
337 crypto_completion_t compl;
338 void *data;
339 u8 *info;
Herbert Xudd044462015-05-23 15:41:56 +0800340 unsigned int ivsize = 8;
Herbert Xu856e3f402015-05-21 15:11:13 +0800341 int err;
342
Herbert Xudd044462015-05-23 15:41:56 +0800343 if (req->cryptlen < ivsize)
344 return -EINVAL;
345
Herbert Xu661cfd02015-05-27 14:37:37 +0800346 aead_request_set_tfm(subreq, ctx->geniv.child);
Herbert Xu856e3f402015-05-21 15:11:13 +0800347
348 compl = req->base.complete;
349 data = req->base.data;
350 info = req->iv;
351
Herbert Xu856e3f402015-05-21 15:11:13 +0800352 if (req->src != req->dst) {
Herbert Xu856e3f402015-05-21 15:11:13 +0800353 struct blkcipher_desc desc = {
354 .tfm = ctx->null,
355 };
356
Herbert Xud0ad1b22015-05-27 14:37:36 +0800357 err = crypto_blkcipher_encrypt(&desc, req->dst, req->src,
358 req->assoclen + req->cryptlen);
Herbert Xu856e3f402015-05-21 15:11:13 +0800359 if (err)
360 return err;
361 }
362
363 if (unlikely(!IS_ALIGNED((unsigned long)info,
364 crypto_aead_alignmask(geniv) + 1))) {
365 info = kmalloc(ivsize, req->base.flags &
366 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
367 GFP_ATOMIC);
368 if (!info)
369 return -ENOMEM;
370
371 memcpy(info, req->iv, ivsize);
372 compl = seqiv_aead_encrypt_complete;
373 data = req;
374 }
375
376 aead_request_set_callback(subreq, req->base.flags, compl, data);
377 aead_request_set_crypt(subreq, req->dst, req->dst,
378 req->cryptlen - ivsize, info);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800379 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800380
381 crypto_xor(info, ctx->salt, ivsize);
382 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
383
384 err = crypto_aead_encrypt(subreq);
385 if (unlikely(info != req->iv))
386 seqiv_aead_encrypt_complete2(req, err);
387 return err;
388}
389
Herbert Xu661cfd02015-05-27 14:37:37 +0800390static int seqniv_aead_decrypt(struct aead_request *req)
Herbert Xu856e3f402015-05-21 15:11:13 +0800391{
392 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
393 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xudd044462015-05-23 15:41:56 +0800394 struct seqniv_request_ctx *rctx = aead_request_ctx(req);
395 struct aead_request *subreq = &rctx->subreq;
396 struct scatterlist *dst;
Herbert Xu856e3f402015-05-21 15:11:13 +0800397 crypto_completion_t compl;
398 void *data;
Herbert Xudd044462015-05-23 15:41:56 +0800399 unsigned int ivsize = 8;
400 u8 buf[20];
401 int err;
402
403 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
404 return -EINVAL;
Herbert Xu856e3f402015-05-21 15:11:13 +0800405
Herbert Xu661cfd02015-05-27 14:37:37 +0800406 aead_request_set_tfm(subreq, ctx->geniv.child);
Herbert Xu856e3f402015-05-21 15:11:13 +0800407
408 compl = req->base.complete;
409 data = req->base.data;
410
Herbert Xudd044462015-05-23 15:41:56 +0800411 if (req->assoclen > 12)
412 return -EINVAL;
413 else if (req->assoclen > 8) {
414 compl = seqniv_aead_decrypt_complete;
415 data = req;
416 }
417
418 if (req->src != req->dst) {
Herbert Xudd044462015-05-23 15:41:56 +0800419 struct blkcipher_desc desc = {
420 .tfm = ctx->null,
421 };
422
Herbert Xud0ad1b22015-05-27 14:37:36 +0800423 err = crypto_blkcipher_encrypt(&desc, req->dst, req->src,
424 req->assoclen + req->cryptlen);
Herbert Xudd044462015-05-23 15:41:56 +0800425 if (err)
426 return err;
427 }
428
429 /* Move ESP AD forward for ICV generation. */
430 scatterwalk_map_and_copy(buf, req->dst, 0, req->assoclen + ivsize, 0);
431 memcpy(req->iv, buf + req->assoclen, ivsize);
432 scatterwalk_map_and_copy(buf, req->dst, ivsize, req->assoclen, 1);
433
434 dst = scatterwalk_ffwd(rctx->dst, req->dst, ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800435
436 aead_request_set_callback(subreq, req->base.flags, compl, data);
Herbert Xudd044462015-05-23 15:41:56 +0800437 aead_request_set_crypt(subreq, dst, dst,
Herbert Xu856e3f402015-05-21 15:11:13 +0800438 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800439 aead_request_set_ad(subreq, req->assoclen);
Herbert Xu856e3f402015-05-21 15:11:13 +0800440
Herbert Xudd044462015-05-23 15:41:56 +0800441 err = crypto_aead_decrypt(subreq);
442 if (req->assoclen > 8)
443 seqniv_aead_decrypt_complete2(req, err);
444 return err;
Herbert Xu856e3f402015-05-21 15:11:13 +0800445}
446
447static int seqiv_aead_decrypt(struct aead_request *req)
448{
449 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
450 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
451 struct aead_request *subreq = aead_request_ctx(req);
452 crypto_completion_t compl;
453 void *data;
Herbert Xudd044462015-05-23 15:41:56 +0800454 unsigned int ivsize = 8;
455
456 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
457 return -EINVAL;
Herbert Xu856e3f402015-05-21 15:11:13 +0800458
Herbert Xu661cfd02015-05-27 14:37:37 +0800459 aead_request_set_tfm(subreq, ctx->geniv.child);
Herbert Xu856e3f402015-05-21 15:11:13 +0800460
461 compl = req->base.complete;
462 data = req->base.data;
463
Herbert Xu856e3f402015-05-21 15:11:13 +0800464 aead_request_set_callback(subreq, req->base.flags, compl, data);
465 aead_request_set_crypt(subreq, req->src, req->dst,
466 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800467 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800468
469 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
470 if (req->src != req->dst)
471 scatterwalk_map_and_copy(req->iv, req->dst,
472 req->assoclen, ivsize, 1);
473
474 return crypto_aead_decrypt(subreq);
475}
476
Herbert Xu0a270322007-11-30 21:38:37 +1100477static int seqiv_init(struct crypto_tfm *tfm)
478{
479 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
480 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
481
482 spin_lock_init(&ctx->lock);
483
484 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
485
Herbert Xub7dcfab2015-06-03 14:49:27 +0800486 return crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
487 crypto_ablkcipher_ivsize(geniv)) ?:
488 skcipher_geniv_init(tfm);
Herbert Xu0a270322007-11-30 21:38:37 +1100489}
490
Herbert Xu856e3f402015-05-21 15:11:13 +0800491static int seqiv_old_aead_init(struct crypto_tfm *tfm)
Herbert Xu14df4d82007-12-12 12:27:26 +0800492{
493 struct crypto_aead *geniv = __crypto_aead_cast(tfm);
494 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
495
496 spin_lock_init(&ctx->lock);
497
Herbert Xuba6d8e32015-05-11 17:48:03 +0800498 crypto_aead_set_reqsize(__crypto_aead_cast(tfm),
499 sizeof(struct aead_request));
Herbert Xu14df4d82007-12-12 12:27:26 +0800500
Herbert Xub7dcfab2015-06-03 14:49:27 +0800501 return crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
502 crypto_aead_ivsize(geniv)) ?:
503 aead_geniv_init(tfm);
Herbert Xu14df4d82007-12-12 12:27:26 +0800504}
505
Herbert Xudd044462015-05-23 15:41:56 +0800506static int seqiv_aead_init_common(struct crypto_tfm *tfm, unsigned int reqsize)
Herbert Xu856e3f402015-05-21 15:11:13 +0800507{
508 struct crypto_aead *geniv = __crypto_aead_cast(tfm);
509 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
510 int err;
511
Herbert Xu661cfd02015-05-27 14:37:37 +0800512 spin_lock_init(&ctx->geniv.lock);
Herbert Xu856e3f402015-05-21 15:11:13 +0800513
514 crypto_aead_set_reqsize(geniv, sizeof(struct aead_request));
515
Herbert Xub7dcfab2015-06-03 14:49:27 +0800516 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
517 crypto_aead_ivsize(geniv));
518 if (err)
519 goto out;
520
Herbert Xu856e3f402015-05-21 15:11:13 +0800521 ctx->null = crypto_get_default_null_skcipher();
522 err = PTR_ERR(ctx->null);
523 if (IS_ERR(ctx->null))
524 goto out;
525
526 err = aead_geniv_init(tfm);
527 if (err)
528 goto drop_null;
529
Herbert Xu661cfd02015-05-27 14:37:37 +0800530 ctx->geniv.child = geniv->child;
Herbert Xu856e3f402015-05-21 15:11:13 +0800531 geniv->child = geniv;
532
533out:
534 return err;
535
536drop_null:
537 crypto_put_default_null_skcipher();
538 goto out;
539}
540
Herbert Xudd044462015-05-23 15:41:56 +0800541static int seqiv_aead_init(struct crypto_tfm *tfm)
Herbert Xu856e3f402015-05-21 15:11:13 +0800542{
Herbert Xudd044462015-05-23 15:41:56 +0800543 return seqiv_aead_init_common(tfm, sizeof(struct aead_request));
544}
Herbert Xu856e3f402015-05-21 15:11:13 +0800545
Herbert Xudd044462015-05-23 15:41:56 +0800546static int seqniv_aead_init(struct crypto_tfm *tfm)
547{
548 return seqiv_aead_init_common(tfm, sizeof(struct seqniv_request_ctx));
Herbert Xu856e3f402015-05-21 15:11:13 +0800549}
550
551static void seqiv_aead_exit(struct crypto_tfm *tfm)
552{
553 struct seqiv_aead_ctx *ctx = crypto_tfm_ctx(tfm);
554
Herbert Xu661cfd02015-05-27 14:37:37 +0800555 crypto_free_aead(ctx->geniv.child);
Herbert Xu856e3f402015-05-21 15:11:13 +0800556 crypto_put_default_null_skcipher();
557}
558
Herbert Xu06771572015-05-23 15:41:51 +0800559static int seqiv_ablkcipher_create(struct crypto_template *tmpl,
560 struct rtattr **tb)
Herbert Xu0a270322007-11-30 21:38:37 +1100561{
562 struct crypto_instance *inst;
Herbert Xu06771572015-05-23 15:41:51 +0800563 int err;
Herbert Xu0a270322007-11-30 21:38:37 +1100564
Herbert Xu06771572015-05-23 15:41:51 +0800565 inst = skcipher_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xu14df4d82007-12-12 12:27:26 +0800566
Herbert Xu0a270322007-11-30 21:38:37 +1100567 if (IS_ERR(inst))
Herbert Xu06771572015-05-23 15:41:51 +0800568 return PTR_ERR(inst);
Herbert Xu0a270322007-11-30 21:38:37 +1100569
Herbert Xu06771572015-05-23 15:41:51 +0800570 err = -EINVAL;
571 if (inst->alg.cra_ablkcipher.ivsize < sizeof(u64))
572 goto free_inst;
Herbert Xuc0ecf892015-01-16 19:51:20 +1100573
Herbert Xub7dcfab2015-06-03 14:49:27 +0800574 inst->alg.cra_ablkcipher.givencrypt = seqiv_givencrypt;
Herbert Xu0a270322007-11-30 21:38:37 +1100575
576 inst->alg.cra_init = seqiv_init;
577 inst->alg.cra_exit = skcipher_geniv_exit;
578
Herbert Xu0a270322007-11-30 21:38:37 +1100579 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
Herbert Xu856e3f402015-05-21 15:11:13 +0800580 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
Herbert Xu0a270322007-11-30 21:38:37 +1100581
Herbert Xu06771572015-05-23 15:41:51 +0800582 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
583
584 err = crypto_register_instance(tmpl, inst);
585 if (err)
586 goto free_inst;
587
Herbert Xu0a270322007-11-30 21:38:37 +1100588out:
Herbert Xu06771572015-05-23 15:41:51 +0800589 return err;
590
591free_inst:
592 skcipher_geniv_free(inst);
593 goto out;
Herbert Xu0a270322007-11-30 21:38:37 +1100594}
595
Herbert Xu06771572015-05-23 15:41:51 +0800596static int seqiv_old_aead_create(struct crypto_template *tmpl,
597 struct aead_instance *aead)
Herbert Xu856e3f402015-05-21 15:11:13 +0800598{
599 struct crypto_instance *inst = aead_crypto_instance(aead);
Herbert Xu06771572015-05-23 15:41:51 +0800600 int err = -EINVAL;
Herbert Xu856e3f402015-05-21 15:11:13 +0800601
Herbert Xu06771572015-05-23 15:41:51 +0800602 if (inst->alg.cra_aead.ivsize < sizeof(u64))
603 goto free_inst;
Herbert Xu856e3f402015-05-21 15:11:13 +0800604
Herbert Xub7dcfab2015-06-03 14:49:27 +0800605 inst->alg.cra_aead.givencrypt = seqiv_aead_givencrypt;
Herbert Xu856e3f402015-05-21 15:11:13 +0800606
607 inst->alg.cra_init = seqiv_old_aead_init;
608 inst->alg.cra_exit = aead_geniv_exit;
609
610 inst->alg.cra_ctxsize = inst->alg.cra_aead.ivsize;
611 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
612
Herbert Xu06771572015-05-23 15:41:51 +0800613 err = crypto_register_instance(tmpl, inst);
614 if (err)
615 goto free_inst;
616
617out:
618 return err;
619
620free_inst:
621 aead_geniv_free(aead);
622 goto out;
Herbert Xu856e3f402015-05-21 15:11:13 +0800623}
624
Herbert Xu06771572015-05-23 15:41:51 +0800625static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800626{
Herbert Xu856e3f402015-05-21 15:11:13 +0800627 struct aead_instance *inst;
628 struct crypto_aead_spawn *spawn;
629 struct aead_alg *alg;
Herbert Xu06771572015-05-23 15:41:51 +0800630 int err;
Herbert Xu14df4d82007-12-12 12:27:26 +0800631
Herbert Xu06771572015-05-23 15:41:51 +0800632 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xu14df4d82007-12-12 12:27:26 +0800633
634 if (IS_ERR(inst))
Herbert Xu06771572015-05-23 15:41:51 +0800635 return PTR_ERR(inst);
636
637 inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
Herbert Xu14df4d82007-12-12 12:27:26 +0800638
Herbert Xu856e3f402015-05-21 15:11:13 +0800639 if (inst->alg.base.cra_aead.encrypt)
Herbert Xu06771572015-05-23 15:41:51 +0800640 return seqiv_old_aead_create(tmpl, inst);
Herbert Xu856e3f402015-05-21 15:11:13 +0800641
Herbert Xu661cfd02015-05-27 14:37:37 +0800642 spawn = aead_instance_ctx(inst);
643 alg = crypto_spawn_aead_alg(spawn);
644
645 if (alg->base.cra_aead.encrypt)
646 goto done;
647
Herbert Xu06771572015-05-23 15:41:51 +0800648 err = -EINVAL;
Herbert Xudd044462015-05-23 15:41:56 +0800649 if (inst->alg.ivsize != sizeof(u64))
Herbert Xu06771572015-05-23 15:41:51 +0800650 goto free_inst;
Herbert Xuc0ecf892015-01-16 19:51:20 +1100651
Herbert Xub7dcfab2015-06-03 14:49:27 +0800652 inst->alg.encrypt = seqiv_aead_encrypt;
Herbert Xu856e3f402015-05-21 15:11:13 +0800653 inst->alg.decrypt = seqiv_aead_decrypt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800654
Herbert Xu856e3f402015-05-21 15:11:13 +0800655 inst->alg.base.cra_init = seqiv_aead_init;
656 inst->alg.base.cra_exit = seqiv_aead_exit;
657
658 inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx);
659 inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize;
660
Herbert Xu661cfd02015-05-27 14:37:37 +0800661done:
Herbert Xu06771572015-05-23 15:41:51 +0800662 err = aead_register_instance(tmpl, inst);
663 if (err)
664 goto free_inst;
665
Herbert Xu14df4d82007-12-12 12:27:26 +0800666out:
Herbert Xu06771572015-05-23 15:41:51 +0800667 return err;
668
669free_inst:
670 aead_geniv_free(inst);
671 goto out;
Herbert Xu14df4d82007-12-12 12:27:26 +0800672}
673
Herbert Xu06771572015-05-23 15:41:51 +0800674static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800675{
676 struct crypto_attr_type *algt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800677 int err;
678
679 algt = crypto_get_attr_type(tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800680 if (IS_ERR(algt))
Herbert Xu06771572015-05-23 15:41:51 +0800681 return PTR_ERR(algt);
Herbert Xu14df4d82007-12-12 12:27:26 +0800682
Herbert Xua0f000e2008-08-14 22:21:31 +1000683 err = crypto_get_default_rng();
684 if (err)
Herbert Xu06771572015-05-23 15:41:51 +0800685 return err;
Herbert Xua0f000e2008-08-14 22:21:31 +1000686
Herbert Xu14df4d82007-12-12 12:27:26 +0800687 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
Herbert Xu06771572015-05-23 15:41:51 +0800688 err = seqiv_ablkcipher_create(tmpl, tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800689 else
Herbert Xu06771572015-05-23 15:41:51 +0800690 err = seqiv_aead_create(tmpl, tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800691
Herbert Xu06771572015-05-23 15:41:51 +0800692 if (err)
693 crypto_put_default_rng();
Herbert Xu14df4d82007-12-12 12:27:26 +0800694
Herbert Xu06771572015-05-23 15:41:51 +0800695 return err;
Herbert Xu14df4d82007-12-12 12:27:26 +0800696}
697
Herbert Xu06771572015-05-23 15:41:51 +0800698static int seqniv_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu3c08fee2015-05-21 15:11:14 +0800699{
700 struct aead_instance *inst;
701 struct crypto_aead_spawn *spawn;
702 struct aead_alg *alg;
703 int err;
704
705 err = crypto_get_default_rng();
706 if (err)
Herbert Xu06771572015-05-23 15:41:51 +0800707 return err;
Herbert Xu3c08fee2015-05-21 15:11:14 +0800708
Herbert Xu06771572015-05-23 15:41:51 +0800709 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
710 err = PTR_ERR(inst);
Herbert Xu3c08fee2015-05-21 15:11:14 +0800711 if (IS_ERR(inst))
712 goto put_rng;
713
Herbert Xu661cfd02015-05-27 14:37:37 +0800714 spawn = aead_instance_ctx(inst);
715 alg = crypto_spawn_aead_alg(spawn);
716
717 if (alg->base.cra_aead.encrypt)
718 goto done;
719
Herbert Xu06771572015-05-23 15:41:51 +0800720 err = -EINVAL;
Herbert Xudd044462015-05-23 15:41:56 +0800721 if (inst->alg.ivsize != sizeof(u64))
Herbert Xu06771572015-05-23 15:41:51 +0800722 goto free_inst;
Herbert Xu3c08fee2015-05-21 15:11:14 +0800723
Herbert Xub7dcfab2015-06-03 14:49:27 +0800724 inst->alg.encrypt = seqniv_aead_encrypt;
Herbert Xu661cfd02015-05-27 14:37:37 +0800725 inst->alg.decrypt = seqniv_aead_decrypt;
Herbert Xu3c08fee2015-05-21 15:11:14 +0800726
Herbert Xudd044462015-05-23 15:41:56 +0800727 inst->alg.base.cra_init = seqniv_aead_init;
728 inst->alg.base.cra_exit = seqiv_aead_exit;
Herbert Xu3c08fee2015-05-21 15:11:14 +0800729
730 inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
731 inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx);
Herbert Xuccdb8a02015-05-27 14:37:39 +0800732 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
Herbert Xu3c08fee2015-05-21 15:11:14 +0800733
Herbert Xu661cfd02015-05-27 14:37:37 +0800734done:
Herbert Xu06771572015-05-23 15:41:51 +0800735 err = aead_register_instance(tmpl, inst);
736 if (err)
737 goto free_inst;
Herbert Xu3c08fee2015-05-21 15:11:14 +0800738
Herbert Xu06771572015-05-23 15:41:51 +0800739out:
740 return err;
741
742free_inst:
743 aead_geniv_free(inst);
Herbert Xu3c08fee2015-05-21 15:11:14 +0800744put_rng:
745 crypto_put_default_rng();
746 goto out;
747}
748
Herbert Xu14df4d82007-12-12 12:27:26 +0800749static void seqiv_free(struct crypto_instance *inst)
750{
751 if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
752 skcipher_geniv_free(inst);
753 else
Herbert Xu856e3f402015-05-21 15:11:13 +0800754 aead_geniv_free(aead_instance(inst));
Herbert Xua0f000e2008-08-14 22:21:31 +1000755 crypto_put_default_rng();
Herbert Xu14df4d82007-12-12 12:27:26 +0800756}
757
Herbert Xu0a270322007-11-30 21:38:37 +1100758static struct crypto_template seqiv_tmpl = {
759 .name = "seqiv",
Herbert Xu06771572015-05-23 15:41:51 +0800760 .create = seqiv_create,
Herbert Xu14df4d82007-12-12 12:27:26 +0800761 .free = seqiv_free,
Herbert Xu0a270322007-11-30 21:38:37 +1100762 .module = THIS_MODULE,
763};
764
Herbert Xu3c08fee2015-05-21 15:11:14 +0800765static struct crypto_template seqniv_tmpl = {
766 .name = "seqniv",
Herbert Xu06771572015-05-23 15:41:51 +0800767 .create = seqniv_create,
Herbert Xu3c08fee2015-05-21 15:11:14 +0800768 .free = seqiv_free,
769 .module = THIS_MODULE,
770};
771
Herbert Xu0a270322007-11-30 21:38:37 +1100772static int __init seqiv_module_init(void)
773{
Herbert Xu3c08fee2015-05-21 15:11:14 +0800774 int err;
775
776 err = crypto_register_template(&seqiv_tmpl);
777 if (err)
778 goto out;
779
780 err = crypto_register_template(&seqniv_tmpl);
781 if (err)
782 goto out_undo_niv;
783
784out:
785 return err;
786
787out_undo_niv:
788 crypto_unregister_template(&seqiv_tmpl);
789 goto out;
Herbert Xu0a270322007-11-30 21:38:37 +1100790}
791
792static void __exit seqiv_module_exit(void)
793{
Herbert Xu056c04b2015-05-27 14:37:40 +0800794 crypto_unregister_template(&seqniv_tmpl);
Herbert Xu0a270322007-11-30 21:38:37 +1100795 crypto_unregister_template(&seqiv_tmpl);
796}
797
798module_init(seqiv_module_init);
799module_exit(seqiv_module_exit);
800
801MODULE_LICENSE("GPL");
802MODULE_DESCRIPTION("Sequence Number IV Generator");
Kees Cook4943ba12014-11-24 16:32:38 -0800803MODULE_ALIAS_CRYPTO("seqiv");
Herbert Xu3c08fee2015-05-21 15:11:14 +0800804MODULE_ALIAS_CRYPTO("seqniv");