blob: b55c6857a1a92fc0da6d08e7d33fa747c413f5b6 [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 Xu14df4d82007-12-12 12:27:26 +080016#include <crypto/internal/aead.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 {
40 struct crypto_aead *child;
41 spinlock_t lock;
42 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 Xu856e3f402015-05-21 15:11:13 +080048static int seqiv_aead_setkey(struct crypto_aead *tfm,
49 const u8 *key, unsigned int keylen)
50{
51 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(tfm);
52
53 return crypto_aead_setkey(ctx->child, key, keylen);
54}
55
56static int seqiv_aead_setauthsize(struct crypto_aead *tfm,
57 unsigned int authsize)
58{
59 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(tfm);
60
61 return crypto_aead_setauthsize(ctx->child, authsize);
62}
63
Herbert Xu0a270322007-11-30 21:38:37 +110064static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err)
65{
66 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
67 struct crypto_ablkcipher *geniv;
68
69 if (err == -EINPROGRESS)
70 return;
71
72 if (err)
73 goto out;
74
75 geniv = skcipher_givcrypt_reqtfm(req);
76 memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv));
77
78out:
79 kfree(subreq->info);
80}
81
82static void seqiv_complete(struct crypto_async_request *base, int err)
83{
84 struct skcipher_givcrypt_request *req = base->data;
85
86 seqiv_complete2(req, err);
87 skcipher_givcrypt_complete(req, err);
88}
89
Herbert Xu14df4d82007-12-12 12:27:26 +080090static void seqiv_aead_complete2(struct aead_givcrypt_request *req, int err)
91{
92 struct aead_request *subreq = aead_givcrypt_reqctx(req);
93 struct crypto_aead *geniv;
94
95 if (err == -EINPROGRESS)
96 return;
97
98 if (err)
99 goto out;
100
101 geniv = aead_givcrypt_reqtfm(req);
102 memcpy(req->areq.iv, subreq->iv, crypto_aead_ivsize(geniv));
103
104out:
105 kfree(subreq->iv);
106}
107
108static void seqiv_aead_complete(struct crypto_async_request *base, int err)
109{
110 struct aead_givcrypt_request *req = base->data;
111
112 seqiv_aead_complete2(req, err);
113 aead_givcrypt_complete(req, err);
114}
115
Herbert Xu856e3f402015-05-21 15:11:13 +0800116static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
117{
118 struct aead_request *subreq = aead_request_ctx(req);
119 struct crypto_aead *geniv;
120
121 if (err == -EINPROGRESS)
122 return;
123
124 if (err)
125 goto out;
126
127 geniv = crypto_aead_reqtfm(req);
128 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
129
130out:
131 kzfree(subreq->iv);
132}
133
134static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
135 int err)
136{
137 struct aead_request *req = base->data;
138
139 seqiv_aead_encrypt_complete2(req, err);
140 aead_request_complete(req, err);
141}
142
Herbert Xudd044462015-05-23 15:41:56 +0800143static void seqniv_aead_encrypt_complete2(struct aead_request *req, int err)
144{
145 unsigned int ivsize = 8;
146 u8 data[20];
147
148 if (err == -EINPROGRESS)
149 return;
150
151 /* Swap IV and ESP header back to correct order. */
152 scatterwalk_map_and_copy(data, req->dst, 0, req->assoclen + ivsize, 0);
153 scatterwalk_map_and_copy(data + ivsize, req->dst, 0, req->assoclen, 1);
154 scatterwalk_map_and_copy(data, req->dst, req->assoclen, ivsize, 1);
155}
156
157static void seqniv_aead_encrypt_complete(struct crypto_async_request *base,
158 int err)
159{
160 struct aead_request *req = base->data;
161
162 seqniv_aead_encrypt_complete2(req, err);
163 aead_request_complete(req, err);
164}
165
166static void seqniv_aead_decrypt_complete2(struct aead_request *req, int err)
167{
168 u8 data[4];
169
170 if (err == -EINPROGRESS)
171 return;
172
173 /* Move ESP header back to correct location. */
174 scatterwalk_map_and_copy(data, req->dst, 16, req->assoclen - 8, 0);
175 scatterwalk_map_and_copy(data, req->dst, 8, req->assoclen - 8, 1);
176}
177
178static void seqniv_aead_decrypt_complete(struct crypto_async_request *base,
179 int err)
180{
181 struct aead_request *req = base->data;
182
183 seqniv_aead_decrypt_complete2(req, err);
184 aead_request_complete(req, err);
185}
186
Herbert Xu14df4d82007-12-12 12:27:26 +0800187static void seqiv_geniv(struct seqiv_ctx *ctx, u8 *info, u64 seq,
188 unsigned int ivsize)
189{
190 unsigned int len = ivsize;
191
192 if (ivsize > sizeof(u64)) {
193 memset(info, 0, ivsize - sizeof(u64));
194 len = sizeof(u64);
195 }
196 seq = cpu_to_be64(seq);
197 memcpy(info + ivsize - len, &seq, len);
198 crypto_xor(info, ctx->salt, ivsize);
199}
200
Herbert Xu0a270322007-11-30 21:38:37 +1100201static int seqiv_givencrypt(struct skcipher_givcrypt_request *req)
202{
203 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
204 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
205 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
Mark Rustad3e3dc252014-07-25 02:53:38 -0700206 crypto_completion_t compl;
Herbert Xu0a270322007-11-30 21:38:37 +1100207 void *data;
208 u8 *info;
Herbert Xu0a270322007-11-30 21:38:37 +1100209 unsigned int ivsize;
Herbert Xu0a270322007-11-30 21:38:37 +1100210 int err;
211
212 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
213
Mark Rustad3e3dc252014-07-25 02:53:38 -0700214 compl = req->creq.base.complete;
Herbert Xu0a270322007-11-30 21:38:37 +1100215 data = req->creq.base.data;
216 info = req->creq.info;
217
218 ivsize = crypto_ablkcipher_ivsize(geniv);
219
220 if (unlikely(!IS_ALIGNED((unsigned long)info,
221 crypto_ablkcipher_alignmask(geniv) + 1))) {
222 info = kmalloc(ivsize, req->creq.base.flags &
223 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
224 GFP_ATOMIC);
225 if (!info)
226 return -ENOMEM;
227
Mark Rustad3e3dc252014-07-25 02:53:38 -0700228 compl = seqiv_complete;
Herbert Xu0a270322007-11-30 21:38:37 +1100229 data = req;
230 }
231
Mark Rustad3e3dc252014-07-25 02:53:38 -0700232 ablkcipher_request_set_callback(subreq, req->creq.base.flags, compl,
Herbert Xu0a270322007-11-30 21:38:37 +1100233 data);
234 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
235 req->creq.nbytes, info);
236
Herbert Xu14df4d82007-12-12 12:27:26 +0800237 seqiv_geniv(ctx, info, req->seq, ivsize);
Herbert Xu0a270322007-11-30 21:38:37 +1100238 memcpy(req->giv, info, ivsize);
239
240 err = crypto_ablkcipher_encrypt(subreq);
241 if (unlikely(info != req->creq.info))
242 seqiv_complete2(req, err);
243 return err;
244}
245
Herbert Xu14df4d82007-12-12 12:27:26 +0800246static int seqiv_aead_givencrypt(struct aead_givcrypt_request *req)
247{
248 struct crypto_aead *geniv = aead_givcrypt_reqtfm(req);
249 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
250 struct aead_request *areq = &req->areq;
251 struct aead_request *subreq = aead_givcrypt_reqctx(req);
Mark Rustad3e3dc252014-07-25 02:53:38 -0700252 crypto_completion_t compl;
Herbert Xu14df4d82007-12-12 12:27:26 +0800253 void *data;
254 u8 *info;
255 unsigned int ivsize;
256 int err;
257
258 aead_request_set_tfm(subreq, aead_geniv_base(geniv));
259
Mark Rustad3e3dc252014-07-25 02:53:38 -0700260 compl = areq->base.complete;
Herbert Xu14df4d82007-12-12 12:27:26 +0800261 data = areq->base.data;
262 info = areq->iv;
263
264 ivsize = crypto_aead_ivsize(geniv);
265
266 if (unlikely(!IS_ALIGNED((unsigned long)info,
267 crypto_aead_alignmask(geniv) + 1))) {
268 info = kmalloc(ivsize, areq->base.flags &
269 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
270 GFP_ATOMIC);
271 if (!info)
272 return -ENOMEM;
273
Mark Rustad3e3dc252014-07-25 02:53:38 -0700274 compl = seqiv_aead_complete;
Herbert Xu14df4d82007-12-12 12:27:26 +0800275 data = req;
276 }
277
Mark Rustad3e3dc252014-07-25 02:53:38 -0700278 aead_request_set_callback(subreq, areq->base.flags, compl, data);
Herbert Xu14df4d82007-12-12 12:27:26 +0800279 aead_request_set_crypt(subreq, areq->src, areq->dst, areq->cryptlen,
280 info);
281 aead_request_set_assoc(subreq, areq->assoc, areq->assoclen);
282
283 seqiv_geniv(ctx, info, req->seq, ivsize);
284 memcpy(req->giv, info, ivsize);
285
286 err = crypto_aead_encrypt(subreq);
287 if (unlikely(info != areq->iv))
288 seqiv_aead_complete2(req, err);
289 return err;
290}
291
Herbert Xu856e3f402015-05-21 15:11:13 +0800292static int seqiv_aead_encrypt_compat(struct aead_request *req)
293{
294 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
295 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xudd044462015-05-23 15:41:56 +0800296 struct seqniv_request_ctx *rctx = aead_request_ctx(req);
297 struct aead_request *subreq = &rctx->subreq;
298 struct scatterlist *dst;
Herbert Xu856e3f402015-05-21 15:11:13 +0800299 crypto_completion_t compl;
300 void *data;
Herbert Xudd044462015-05-23 15:41:56 +0800301 unsigned int ivsize = 8;
302 u8 buf[20] __attribute__ ((aligned(__alignof__(u32))));
Herbert Xu856e3f402015-05-21 15:11:13 +0800303 int err;
304
Herbert Xudd044462015-05-23 15:41:56 +0800305 if (req->cryptlen < ivsize)
306 return -EINVAL;
307
308 /* ESP AD is at most 12 bytes (ESN). */
309 if (req->assoclen > 12)
310 return -EINVAL;
311
Herbert Xu856e3f402015-05-21 15:11:13 +0800312 aead_request_set_tfm(subreq, ctx->child);
313
Herbert Xudd044462015-05-23 15:41:56 +0800314 compl = seqniv_aead_encrypt_complete;
315 data = req;
Herbert Xu856e3f402015-05-21 15:11:13 +0800316
Herbert Xudd044462015-05-23 15:41:56 +0800317 if (req->src != req->dst) {
Herbert Xudd044462015-05-23 15:41:56 +0800318 struct blkcipher_desc desc = {
319 .tfm = ctx->null,
320 };
Herbert Xu856e3f402015-05-21 15:11:13 +0800321
Herbert Xud0ad1b22015-05-27 14:37:36 +0800322 err = crypto_blkcipher_encrypt(&desc, req->dst, req->src,
323 req->assoclen + req->cryptlen);
Herbert Xudd044462015-05-23 15:41:56 +0800324 if (err)
325 return err;
Herbert Xu856e3f402015-05-21 15:11:13 +0800326 }
327
Herbert Xudd044462015-05-23 15:41:56 +0800328 dst = scatterwalk_ffwd(rctx->dst, req->dst, ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800329
Herbert Xudd044462015-05-23 15:41:56 +0800330 aead_request_set_callback(subreq, req->base.flags, compl, data);
331 aead_request_set_crypt(subreq, dst, dst,
332 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800333 aead_request_set_ad(subreq, req->assoclen);
Herbert Xudd044462015-05-23 15:41:56 +0800334
335 memcpy(buf, req->iv, ivsize);
336 crypto_xor(buf, ctx->salt, ivsize);
337 memcpy(req->iv, buf, ivsize);
338
339 /* Swap order of IV and ESP AD for ICV generation. */
340 scatterwalk_map_and_copy(buf + ivsize, req->dst, 0, req->assoclen, 0);
341 scatterwalk_map_and_copy(buf, req->dst, 0, req->assoclen + ivsize, 1);
Herbert Xu856e3f402015-05-21 15:11:13 +0800342
343 err = crypto_aead_encrypt(subreq);
Herbert Xudd044462015-05-23 15:41:56 +0800344 seqniv_aead_encrypt_complete2(req, err);
Herbert Xu856e3f402015-05-21 15:11:13 +0800345 return err;
346}
347
348static int seqiv_aead_encrypt(struct aead_request *req)
349{
350 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
351 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
352 struct aead_request *subreq = aead_request_ctx(req);
353 crypto_completion_t compl;
354 void *data;
355 u8 *info;
Herbert Xudd044462015-05-23 15:41:56 +0800356 unsigned int ivsize = 8;
Herbert Xu856e3f402015-05-21 15:11:13 +0800357 int err;
358
Herbert Xudd044462015-05-23 15:41:56 +0800359 if (req->cryptlen < ivsize)
360 return -EINVAL;
361
Herbert Xu856e3f402015-05-21 15:11:13 +0800362 aead_request_set_tfm(subreq, ctx->child);
363
364 compl = req->base.complete;
365 data = req->base.data;
366 info = req->iv;
367
Herbert Xu856e3f402015-05-21 15:11:13 +0800368 if (req->src != req->dst) {
Herbert Xu856e3f402015-05-21 15:11:13 +0800369 struct blkcipher_desc desc = {
370 .tfm = ctx->null,
371 };
372
Herbert Xud0ad1b22015-05-27 14:37:36 +0800373 err = crypto_blkcipher_encrypt(&desc, req->dst, req->src,
374 req->assoclen + req->cryptlen);
Herbert Xu856e3f402015-05-21 15:11:13 +0800375 if (err)
376 return err;
377 }
378
379 if (unlikely(!IS_ALIGNED((unsigned long)info,
380 crypto_aead_alignmask(geniv) + 1))) {
381 info = kmalloc(ivsize, req->base.flags &
382 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
383 GFP_ATOMIC);
384 if (!info)
385 return -ENOMEM;
386
387 memcpy(info, req->iv, ivsize);
388 compl = seqiv_aead_encrypt_complete;
389 data = req;
390 }
391
392 aead_request_set_callback(subreq, req->base.flags, compl, data);
393 aead_request_set_crypt(subreq, req->dst, req->dst,
394 req->cryptlen - ivsize, info);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800395 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800396
397 crypto_xor(info, ctx->salt, ivsize);
398 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
399
400 err = crypto_aead_encrypt(subreq);
401 if (unlikely(info != req->iv))
402 seqiv_aead_encrypt_complete2(req, err);
403 return err;
404}
405
406static int seqiv_aead_decrypt_compat(struct aead_request *req)
407{
408 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
409 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xudd044462015-05-23 15:41:56 +0800410 struct seqniv_request_ctx *rctx = aead_request_ctx(req);
411 struct aead_request *subreq = &rctx->subreq;
412 struct scatterlist *dst;
Herbert Xu856e3f402015-05-21 15:11:13 +0800413 crypto_completion_t compl;
414 void *data;
Herbert Xudd044462015-05-23 15:41:56 +0800415 unsigned int ivsize = 8;
416 u8 buf[20];
417 int err;
418
419 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
420 return -EINVAL;
Herbert Xu856e3f402015-05-21 15:11:13 +0800421
422 aead_request_set_tfm(subreq, ctx->child);
423
424 compl = req->base.complete;
425 data = req->base.data;
426
Herbert Xudd044462015-05-23 15:41:56 +0800427 if (req->assoclen > 12)
428 return -EINVAL;
429 else if (req->assoclen > 8) {
430 compl = seqniv_aead_decrypt_complete;
431 data = req;
432 }
433
434 if (req->src != req->dst) {
Herbert Xudd044462015-05-23 15:41:56 +0800435 struct blkcipher_desc desc = {
436 .tfm = ctx->null,
437 };
438
Herbert Xud0ad1b22015-05-27 14:37:36 +0800439 err = crypto_blkcipher_encrypt(&desc, req->dst, req->src,
440 req->assoclen + req->cryptlen);
Herbert Xudd044462015-05-23 15:41:56 +0800441 if (err)
442 return err;
443 }
444
445 /* Move ESP AD forward for ICV generation. */
446 scatterwalk_map_and_copy(buf, req->dst, 0, req->assoclen + ivsize, 0);
447 memcpy(req->iv, buf + req->assoclen, ivsize);
448 scatterwalk_map_and_copy(buf, req->dst, ivsize, req->assoclen, 1);
449
450 dst = scatterwalk_ffwd(rctx->dst, req->dst, ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800451
452 aead_request_set_callback(subreq, req->base.flags, compl, data);
Herbert Xudd044462015-05-23 15:41:56 +0800453 aead_request_set_crypt(subreq, dst, dst,
Herbert Xu856e3f402015-05-21 15:11:13 +0800454 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800455 aead_request_set_ad(subreq, req->assoclen);
Herbert Xu856e3f402015-05-21 15:11:13 +0800456
Herbert Xudd044462015-05-23 15:41:56 +0800457 err = crypto_aead_decrypt(subreq);
458 if (req->assoclen > 8)
459 seqniv_aead_decrypt_complete2(req, err);
460 return err;
Herbert Xu856e3f402015-05-21 15:11:13 +0800461}
462
463static int seqiv_aead_decrypt(struct aead_request *req)
464{
465 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
466 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
467 struct aead_request *subreq = aead_request_ctx(req);
468 crypto_completion_t compl;
469 void *data;
Herbert Xudd044462015-05-23 15:41:56 +0800470 unsigned int ivsize = 8;
471
472 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
473 return -EINVAL;
Herbert Xu856e3f402015-05-21 15:11:13 +0800474
475 aead_request_set_tfm(subreq, ctx->child);
476
477 compl = req->base.complete;
478 data = req->base.data;
479
Herbert Xu856e3f402015-05-21 15:11:13 +0800480 aead_request_set_callback(subreq, req->base.flags, compl, data);
481 aead_request_set_crypt(subreq, req->src, req->dst,
482 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800483 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800484
485 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
486 if (req->src != req->dst)
487 scatterwalk_map_and_copy(req->iv, req->dst,
488 req->assoclen, ivsize, 1);
489
490 return crypto_aead_decrypt(subreq);
491}
492
Herbert Xu0a270322007-11-30 21:38:37 +1100493static int seqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
494{
495 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
496 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
Herbert Xua0f000e2008-08-14 22:21:31 +1000497 int err = 0;
Herbert Xu0a270322007-11-30 21:38:37 +1100498
499 spin_lock_bh(&ctx->lock);
500 if (crypto_ablkcipher_crt(geniv)->givencrypt != seqiv_givencrypt_first)
501 goto unlock;
502
503 crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt;
Herbert Xua0f000e2008-08-14 22:21:31 +1000504 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
505 crypto_ablkcipher_ivsize(geniv));
Herbert Xu0a270322007-11-30 21:38:37 +1100506
507unlock:
508 spin_unlock_bh(&ctx->lock);
509
Herbert Xua0f000e2008-08-14 22:21:31 +1000510 if (err)
511 return err;
512
Herbert Xu0a270322007-11-30 21:38:37 +1100513 return seqiv_givencrypt(req);
514}
515
Herbert Xu14df4d82007-12-12 12:27:26 +0800516static int seqiv_aead_givencrypt_first(struct aead_givcrypt_request *req)
517{
518 struct crypto_aead *geniv = aead_givcrypt_reqtfm(req);
519 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xua0f000e2008-08-14 22:21:31 +1000520 int err = 0;
Herbert Xu14df4d82007-12-12 12:27:26 +0800521
522 spin_lock_bh(&ctx->lock);
523 if (crypto_aead_crt(geniv)->givencrypt != seqiv_aead_givencrypt_first)
524 goto unlock;
525
526 crypto_aead_crt(geniv)->givencrypt = seqiv_aead_givencrypt;
Herbert Xua0f000e2008-08-14 22:21:31 +1000527 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
528 crypto_aead_ivsize(geniv));
Herbert Xu14df4d82007-12-12 12:27:26 +0800529
530unlock:
531 spin_unlock_bh(&ctx->lock);
532
Herbert Xua0f000e2008-08-14 22:21:31 +1000533 if (err)
534 return err;
535
Herbert Xu14df4d82007-12-12 12:27:26 +0800536 return seqiv_aead_givencrypt(req);
537}
538
Herbert Xu856e3f402015-05-21 15:11:13 +0800539static int seqiv_aead_encrypt_compat_first(struct aead_request *req)
540{
541 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
542 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
543 int err = 0;
544
545 spin_lock_bh(&ctx->lock);
546 if (geniv->encrypt != seqiv_aead_encrypt_compat_first)
547 goto unlock;
548
549 geniv->encrypt = seqiv_aead_encrypt_compat;
550 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
551 crypto_aead_ivsize(geniv));
552
553unlock:
554 spin_unlock_bh(&ctx->lock);
555
556 if (err)
557 return err;
558
559 return seqiv_aead_encrypt_compat(req);
560}
561
562static int seqiv_aead_encrypt_first(struct aead_request *req)
563{
564 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
565 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
566 int err = 0;
567
568 spin_lock_bh(&ctx->lock);
569 if (geniv->encrypt != seqiv_aead_encrypt_first)
570 goto unlock;
571
572 geniv->encrypt = seqiv_aead_encrypt;
573 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
574 crypto_aead_ivsize(geniv));
575
576unlock:
577 spin_unlock_bh(&ctx->lock);
578
579 if (err)
580 return err;
581
582 return seqiv_aead_encrypt(req);
583}
584
Herbert Xu0a270322007-11-30 21:38:37 +1100585static int seqiv_init(struct crypto_tfm *tfm)
586{
587 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
588 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
589
590 spin_lock_init(&ctx->lock);
591
592 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
593
594 return skcipher_geniv_init(tfm);
595}
596
Herbert Xu856e3f402015-05-21 15:11:13 +0800597static int seqiv_old_aead_init(struct crypto_tfm *tfm)
Herbert Xu14df4d82007-12-12 12:27:26 +0800598{
599 struct crypto_aead *geniv = __crypto_aead_cast(tfm);
600 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
601
602 spin_lock_init(&ctx->lock);
603
Herbert Xuba6d8e32015-05-11 17:48:03 +0800604 crypto_aead_set_reqsize(__crypto_aead_cast(tfm),
605 sizeof(struct aead_request));
Herbert Xu14df4d82007-12-12 12:27:26 +0800606
607 return aead_geniv_init(tfm);
608}
609
Herbert Xudd044462015-05-23 15:41:56 +0800610static int seqiv_aead_init_common(struct crypto_tfm *tfm, unsigned int reqsize)
Herbert Xu856e3f402015-05-21 15:11:13 +0800611{
612 struct crypto_aead *geniv = __crypto_aead_cast(tfm);
613 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
614 int err;
615
616 spin_lock_init(&ctx->lock);
617
618 crypto_aead_set_reqsize(geniv, sizeof(struct aead_request));
619
620 ctx->null = crypto_get_default_null_skcipher();
621 err = PTR_ERR(ctx->null);
622 if (IS_ERR(ctx->null))
623 goto out;
624
625 err = aead_geniv_init(tfm);
626 if (err)
627 goto drop_null;
628
629 ctx->child = geniv->child;
630 geniv->child = geniv;
631
632out:
633 return err;
634
635drop_null:
636 crypto_put_default_null_skcipher();
637 goto out;
638}
639
Herbert Xudd044462015-05-23 15:41:56 +0800640static int seqiv_aead_init(struct crypto_tfm *tfm)
Herbert Xu856e3f402015-05-21 15:11:13 +0800641{
Herbert Xudd044462015-05-23 15:41:56 +0800642 return seqiv_aead_init_common(tfm, sizeof(struct aead_request));
643}
Herbert Xu856e3f402015-05-21 15:11:13 +0800644
Herbert Xudd044462015-05-23 15:41:56 +0800645static int seqniv_aead_init(struct crypto_tfm *tfm)
646{
647 return seqiv_aead_init_common(tfm, sizeof(struct seqniv_request_ctx));
Herbert Xu856e3f402015-05-21 15:11:13 +0800648}
649
650static void seqiv_aead_exit(struct crypto_tfm *tfm)
651{
652 struct seqiv_aead_ctx *ctx = crypto_tfm_ctx(tfm);
653
654 crypto_free_aead(ctx->child);
655 crypto_put_default_null_skcipher();
656}
657
Herbert Xu06771572015-05-23 15:41:51 +0800658static int seqiv_ablkcipher_create(struct crypto_template *tmpl,
659 struct rtattr **tb)
Herbert Xu0a270322007-11-30 21:38:37 +1100660{
661 struct crypto_instance *inst;
Herbert Xu06771572015-05-23 15:41:51 +0800662 int err;
Herbert Xu0a270322007-11-30 21:38:37 +1100663
Herbert Xu06771572015-05-23 15:41:51 +0800664 inst = skcipher_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xu14df4d82007-12-12 12:27:26 +0800665
Herbert Xu0a270322007-11-30 21:38:37 +1100666 if (IS_ERR(inst))
Herbert Xu06771572015-05-23 15:41:51 +0800667 return PTR_ERR(inst);
Herbert Xu0a270322007-11-30 21:38:37 +1100668
Herbert Xu06771572015-05-23 15:41:51 +0800669 err = -EINVAL;
670 if (inst->alg.cra_ablkcipher.ivsize < sizeof(u64))
671 goto free_inst;
Herbert Xuc0ecf892015-01-16 19:51:20 +1100672
Herbert Xu0a270322007-11-30 21:38:37 +1100673 inst->alg.cra_ablkcipher.givencrypt = seqiv_givencrypt_first;
674
675 inst->alg.cra_init = seqiv_init;
676 inst->alg.cra_exit = skcipher_geniv_exit;
677
Herbert Xu0a270322007-11-30 21:38:37 +1100678 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
Herbert Xu856e3f402015-05-21 15:11:13 +0800679 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
Herbert Xu0a270322007-11-30 21:38:37 +1100680
Herbert Xu06771572015-05-23 15:41:51 +0800681 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
682
683 err = crypto_register_instance(tmpl, inst);
684 if (err)
685 goto free_inst;
686
Herbert Xu0a270322007-11-30 21:38:37 +1100687out:
Herbert Xu06771572015-05-23 15:41:51 +0800688 return err;
689
690free_inst:
691 skcipher_geniv_free(inst);
692 goto out;
Herbert Xu0a270322007-11-30 21:38:37 +1100693}
694
Herbert Xu06771572015-05-23 15:41:51 +0800695static int seqiv_old_aead_create(struct crypto_template *tmpl,
696 struct aead_instance *aead)
Herbert Xu856e3f402015-05-21 15:11:13 +0800697{
698 struct crypto_instance *inst = aead_crypto_instance(aead);
Herbert Xu06771572015-05-23 15:41:51 +0800699 int err = -EINVAL;
Herbert Xu856e3f402015-05-21 15:11:13 +0800700
Herbert Xu06771572015-05-23 15:41:51 +0800701 if (inst->alg.cra_aead.ivsize < sizeof(u64))
702 goto free_inst;
Herbert Xu856e3f402015-05-21 15:11:13 +0800703
704 inst->alg.cra_aead.givencrypt = seqiv_aead_givencrypt_first;
705
706 inst->alg.cra_init = seqiv_old_aead_init;
707 inst->alg.cra_exit = aead_geniv_exit;
708
709 inst->alg.cra_ctxsize = inst->alg.cra_aead.ivsize;
710 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
711
Herbert Xu06771572015-05-23 15:41:51 +0800712 err = crypto_register_instance(tmpl, inst);
713 if (err)
714 goto free_inst;
715
716out:
717 return err;
718
719free_inst:
720 aead_geniv_free(aead);
721 goto out;
Herbert Xu856e3f402015-05-21 15:11:13 +0800722}
723
Herbert Xu06771572015-05-23 15:41:51 +0800724static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800725{
Herbert Xu856e3f402015-05-21 15:11:13 +0800726 struct aead_instance *inst;
727 struct crypto_aead_spawn *spawn;
728 struct aead_alg *alg;
Herbert Xu06771572015-05-23 15:41:51 +0800729 int err;
Herbert Xu14df4d82007-12-12 12:27:26 +0800730
Herbert Xu06771572015-05-23 15:41:51 +0800731 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xu14df4d82007-12-12 12:27:26 +0800732
733 if (IS_ERR(inst))
Herbert Xu06771572015-05-23 15:41:51 +0800734 return PTR_ERR(inst);
735
736 inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
Herbert Xu14df4d82007-12-12 12:27:26 +0800737
Herbert Xu856e3f402015-05-21 15:11:13 +0800738 if (inst->alg.base.cra_aead.encrypt)
Herbert Xu06771572015-05-23 15:41:51 +0800739 return seqiv_old_aead_create(tmpl, inst);
Herbert Xu856e3f402015-05-21 15:11:13 +0800740
Herbert Xu06771572015-05-23 15:41:51 +0800741 err = -EINVAL;
Herbert Xudd044462015-05-23 15:41:56 +0800742 if (inst->alg.ivsize != sizeof(u64))
Herbert Xu06771572015-05-23 15:41:51 +0800743 goto free_inst;
Herbert Xuc0ecf892015-01-16 19:51:20 +1100744
Herbert Xu856e3f402015-05-21 15:11:13 +0800745 spawn = aead_instance_ctx(inst);
746 alg = crypto_spawn_aead_alg(spawn);
Herbert Xu14df4d82007-12-12 12:27:26 +0800747
Herbert Xu856e3f402015-05-21 15:11:13 +0800748 inst->alg.setkey = seqiv_aead_setkey;
749 inst->alg.setauthsize = seqiv_aead_setauthsize;
750 inst->alg.encrypt = seqiv_aead_encrypt_first;
751 inst->alg.decrypt = seqiv_aead_decrypt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800752
Herbert Xu856e3f402015-05-21 15:11:13 +0800753 inst->alg.base.cra_init = seqiv_aead_init;
754 inst->alg.base.cra_exit = seqiv_aead_exit;
755
756 inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx);
757 inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize;
758
759 if (alg->base.cra_aead.encrypt) {
760 inst->alg.encrypt = seqiv_aead_encrypt_compat_first;
761 inst->alg.decrypt = seqiv_aead_decrypt_compat;
762
Herbert Xudd044462015-05-23 15:41:56 +0800763 inst->alg.base.cra_init = seqniv_aead_init;
764 inst->alg.base.cra_exit = seqiv_aead_exit;
Herbert Xu856e3f402015-05-21 15:11:13 +0800765 }
Herbert Xu14df4d82007-12-12 12:27:26 +0800766
Herbert Xu06771572015-05-23 15:41:51 +0800767 err = aead_register_instance(tmpl, inst);
768 if (err)
769 goto free_inst;
770
Herbert Xu14df4d82007-12-12 12:27:26 +0800771out:
Herbert Xu06771572015-05-23 15:41:51 +0800772 return err;
773
774free_inst:
775 aead_geniv_free(inst);
776 goto out;
Herbert Xu14df4d82007-12-12 12:27:26 +0800777}
778
Herbert Xu06771572015-05-23 15:41:51 +0800779static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800780{
781 struct crypto_attr_type *algt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800782 int err;
783
784 algt = crypto_get_attr_type(tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800785 if (IS_ERR(algt))
Herbert Xu06771572015-05-23 15:41:51 +0800786 return PTR_ERR(algt);
Herbert Xu14df4d82007-12-12 12:27:26 +0800787
Herbert Xua0f000e2008-08-14 22:21:31 +1000788 err = crypto_get_default_rng();
789 if (err)
Herbert Xu06771572015-05-23 15:41:51 +0800790 return err;
Herbert Xua0f000e2008-08-14 22:21:31 +1000791
Herbert Xu14df4d82007-12-12 12:27:26 +0800792 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
Herbert Xu06771572015-05-23 15:41:51 +0800793 err = seqiv_ablkcipher_create(tmpl, tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800794 else
Herbert Xu06771572015-05-23 15:41:51 +0800795 err = seqiv_aead_create(tmpl, tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800796
Herbert Xu06771572015-05-23 15:41:51 +0800797 if (err)
798 crypto_put_default_rng();
Herbert Xu14df4d82007-12-12 12:27:26 +0800799
Herbert Xu06771572015-05-23 15:41:51 +0800800 return err;
Herbert Xu14df4d82007-12-12 12:27:26 +0800801}
802
Herbert Xu06771572015-05-23 15:41:51 +0800803static int seqniv_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu3c08fee2015-05-21 15:11:14 +0800804{
805 struct aead_instance *inst;
806 struct crypto_aead_spawn *spawn;
807 struct aead_alg *alg;
808 int err;
809
810 err = crypto_get_default_rng();
811 if (err)
Herbert Xu06771572015-05-23 15:41:51 +0800812 return err;
Herbert Xu3c08fee2015-05-21 15:11:14 +0800813
Herbert Xu06771572015-05-23 15:41:51 +0800814 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
815 err = PTR_ERR(inst);
Herbert Xu3c08fee2015-05-21 15:11:14 +0800816 if (IS_ERR(inst))
817 goto put_rng;
818
Herbert Xu06771572015-05-23 15:41:51 +0800819 err = -EINVAL;
Herbert Xudd044462015-05-23 15:41:56 +0800820 if (inst->alg.ivsize != sizeof(u64))
Herbert Xu06771572015-05-23 15:41:51 +0800821 goto free_inst;
Herbert Xu3c08fee2015-05-21 15:11:14 +0800822
823 spawn = aead_instance_ctx(inst);
824 alg = crypto_spawn_aead_alg(spawn);
825
826 inst->alg.setkey = seqiv_aead_setkey;
827 inst->alg.setauthsize = seqiv_aead_setauthsize;
828 inst->alg.encrypt = seqiv_aead_encrypt_compat_first;
829 inst->alg.decrypt = seqiv_aead_decrypt_compat;
830
Herbert Xudd044462015-05-23 15:41:56 +0800831 inst->alg.base.cra_init = seqniv_aead_init;
832 inst->alg.base.cra_exit = seqiv_aead_exit;
Herbert Xu3c08fee2015-05-21 15:11:14 +0800833
834 inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
835 inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx);
836 inst->alg.base.cra_ctxsize += inst->alg.base.cra_aead.ivsize;
837
Herbert Xu06771572015-05-23 15:41:51 +0800838 err = aead_register_instance(tmpl, inst);
839 if (err)
840 goto free_inst;
Herbert Xu3c08fee2015-05-21 15:11:14 +0800841
Herbert Xu06771572015-05-23 15:41:51 +0800842out:
843 return err;
844
845free_inst:
846 aead_geniv_free(inst);
Herbert Xu3c08fee2015-05-21 15:11:14 +0800847put_rng:
848 crypto_put_default_rng();
849 goto out;
850}
851
Herbert Xu14df4d82007-12-12 12:27:26 +0800852static void seqiv_free(struct crypto_instance *inst)
853{
854 if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
855 skcipher_geniv_free(inst);
856 else
Herbert Xu856e3f402015-05-21 15:11:13 +0800857 aead_geniv_free(aead_instance(inst));
Herbert Xua0f000e2008-08-14 22:21:31 +1000858 crypto_put_default_rng();
Herbert Xu14df4d82007-12-12 12:27:26 +0800859}
860
Herbert Xu0a270322007-11-30 21:38:37 +1100861static struct crypto_template seqiv_tmpl = {
862 .name = "seqiv",
Herbert Xu06771572015-05-23 15:41:51 +0800863 .create = seqiv_create,
Herbert Xu14df4d82007-12-12 12:27:26 +0800864 .free = seqiv_free,
Herbert Xu0a270322007-11-30 21:38:37 +1100865 .module = THIS_MODULE,
866};
867
Herbert Xu3c08fee2015-05-21 15:11:14 +0800868static struct crypto_template seqniv_tmpl = {
869 .name = "seqniv",
Herbert Xu06771572015-05-23 15:41:51 +0800870 .create = seqniv_create,
Herbert Xu3c08fee2015-05-21 15:11:14 +0800871 .free = seqiv_free,
872 .module = THIS_MODULE,
873};
874
Herbert Xu0a270322007-11-30 21:38:37 +1100875static int __init seqiv_module_init(void)
876{
Herbert Xu3c08fee2015-05-21 15:11:14 +0800877 int err;
878
879 err = crypto_register_template(&seqiv_tmpl);
880 if (err)
881 goto out;
882
883 err = crypto_register_template(&seqniv_tmpl);
884 if (err)
885 goto out_undo_niv;
886
887out:
888 return err;
889
890out_undo_niv:
891 crypto_unregister_template(&seqiv_tmpl);
892 goto out;
Herbert Xu0a270322007-11-30 21:38:37 +1100893}
894
895static void __exit seqiv_module_exit(void)
896{
897 crypto_unregister_template(&seqiv_tmpl);
898}
899
900module_init(seqiv_module_init);
901module_exit(seqiv_module_exit);
902
903MODULE_LICENSE("GPL");
904MODULE_DESCRIPTION("Sequence Number IV Generator");
Kees Cook4943ba12014-11-24 16:32:38 -0800905MODULE_ALIAS_CRYPTO("seqiv");
Herbert Xu3c08fee2015-05-21 15:11:14 +0800906MODULE_ALIAS_CRYPTO("seqniv");