blob: 15a749a5cab72ea53d597cfa1983a2a175154cc2 [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 Xua0f000e2008-08-14 22:21:31 +100018#include <crypto/rng.h>
Herbert Xu856e3f402015-05-21 15:11:13 +080019#include <crypto/scatterwalk.h>
Herbert Xu0a270322007-11-30 21:38:37 +110020#include <linux/err.h>
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Herbert Xu0a270322007-11-30 21:38:37 +110025#include <linux/spinlock.h>
26#include <linux/string.h>
27
28struct seqiv_ctx {
29 spinlock_t lock;
30 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
31};
32
Herbert Xu06771572015-05-23 15:41:51 +080033static void seqiv_free(struct crypto_instance *inst);
34
Herbert Xu0a270322007-11-30 21:38:37 +110035static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err)
36{
37 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
38 struct crypto_ablkcipher *geniv;
39
40 if (err == -EINPROGRESS)
41 return;
42
43 if (err)
44 goto out;
45
46 geniv = skcipher_givcrypt_reqtfm(req);
47 memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv));
48
49out:
50 kfree(subreq->info);
51}
52
53static void seqiv_complete(struct crypto_async_request *base, int err)
54{
55 struct skcipher_givcrypt_request *req = base->data;
56
57 seqiv_complete2(req, err);
58 skcipher_givcrypt_complete(req, err);
59}
60
Herbert Xu856e3f402015-05-21 15:11:13 +080061static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
62{
63 struct aead_request *subreq = aead_request_ctx(req);
64 struct crypto_aead *geniv;
65
66 if (err == -EINPROGRESS)
67 return;
68
69 if (err)
70 goto out;
71
72 geniv = crypto_aead_reqtfm(req);
73 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
74
75out:
76 kzfree(subreq->iv);
77}
78
79static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
80 int err)
81{
82 struct aead_request *req = base->data;
83
84 seqiv_aead_encrypt_complete2(req, err);
85 aead_request_complete(req, err);
86}
87
Herbert Xu14df4d82007-12-12 12:27:26 +080088static void seqiv_geniv(struct seqiv_ctx *ctx, u8 *info, u64 seq,
89 unsigned int ivsize)
90{
91 unsigned int len = ivsize;
92
93 if (ivsize > sizeof(u64)) {
94 memset(info, 0, ivsize - sizeof(u64));
95 len = sizeof(u64);
96 }
97 seq = cpu_to_be64(seq);
98 memcpy(info + ivsize - len, &seq, len);
99 crypto_xor(info, ctx->salt, ivsize);
100}
101
Herbert Xu0a270322007-11-30 21:38:37 +1100102static int seqiv_givencrypt(struct skcipher_givcrypt_request *req)
103{
104 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
105 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
106 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
Mark Rustad3e3dc252014-07-25 02:53:38 -0700107 crypto_completion_t compl;
Herbert Xu0a270322007-11-30 21:38:37 +1100108 void *data;
109 u8 *info;
Herbert Xu0a270322007-11-30 21:38:37 +1100110 unsigned int ivsize;
Herbert Xu0a270322007-11-30 21:38:37 +1100111 int err;
112
113 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
114
Mark Rustad3e3dc252014-07-25 02:53:38 -0700115 compl = req->creq.base.complete;
Herbert Xu0a270322007-11-30 21:38:37 +1100116 data = req->creq.base.data;
117 info = req->creq.info;
118
119 ivsize = crypto_ablkcipher_ivsize(geniv);
120
121 if (unlikely(!IS_ALIGNED((unsigned long)info,
122 crypto_ablkcipher_alignmask(geniv) + 1))) {
123 info = kmalloc(ivsize, req->creq.base.flags &
124 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
125 GFP_ATOMIC);
126 if (!info)
127 return -ENOMEM;
128
Mark Rustad3e3dc252014-07-25 02:53:38 -0700129 compl = seqiv_complete;
Herbert Xu0a270322007-11-30 21:38:37 +1100130 data = req;
131 }
132
Mark Rustad3e3dc252014-07-25 02:53:38 -0700133 ablkcipher_request_set_callback(subreq, req->creq.base.flags, compl,
Herbert Xu0a270322007-11-30 21:38:37 +1100134 data);
135 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
136 req->creq.nbytes, info);
137
Herbert Xu14df4d82007-12-12 12:27:26 +0800138 seqiv_geniv(ctx, info, req->seq, ivsize);
Herbert Xu0a270322007-11-30 21:38:37 +1100139 memcpy(req->giv, info, ivsize);
140
141 err = crypto_ablkcipher_encrypt(subreq);
142 if (unlikely(info != req->creq.info))
143 seqiv_complete2(req, err);
144 return err;
145}
146
Herbert Xu856e3f402015-05-21 15:11:13 +0800147static int seqiv_aead_encrypt(struct aead_request *req)
148{
149 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu659e7f52015-08-13 17:28:59 +0800150 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xu856e3f402015-05-21 15:11:13 +0800151 struct aead_request *subreq = aead_request_ctx(req);
152 crypto_completion_t compl;
153 void *data;
154 u8 *info;
Herbert Xudd044462015-05-23 15:41:56 +0800155 unsigned int ivsize = 8;
Herbert Xu856e3f402015-05-21 15:11:13 +0800156 int err;
157
Herbert Xudd044462015-05-23 15:41:56 +0800158 if (req->cryptlen < ivsize)
159 return -EINVAL;
160
Herbert Xu659e7f52015-08-13 17:28:59 +0800161 aead_request_set_tfm(subreq, ctx->child);
Herbert Xu856e3f402015-05-21 15:11:13 +0800162
163 compl = req->base.complete;
164 data = req->base.data;
165 info = req->iv;
166
Herbert Xu856e3f402015-05-21 15:11:13 +0800167 if (req->src != req->dst) {
Herbert Xu856e3f402015-05-21 15:11:13 +0800168 struct blkcipher_desc desc = {
169 .tfm = ctx->null,
170 };
171
Herbert Xud0ad1b22015-05-27 14:37:36 +0800172 err = crypto_blkcipher_encrypt(&desc, req->dst, req->src,
173 req->assoclen + req->cryptlen);
Herbert Xu856e3f402015-05-21 15:11:13 +0800174 if (err)
175 return err;
176 }
177
178 if (unlikely(!IS_ALIGNED((unsigned long)info,
179 crypto_aead_alignmask(geniv) + 1))) {
180 info = kmalloc(ivsize, req->base.flags &
181 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
182 GFP_ATOMIC);
183 if (!info)
184 return -ENOMEM;
185
186 memcpy(info, req->iv, ivsize);
187 compl = seqiv_aead_encrypt_complete;
188 data = req;
189 }
190
191 aead_request_set_callback(subreq, req->base.flags, compl, data);
192 aead_request_set_crypt(subreq, req->dst, req->dst,
193 req->cryptlen - ivsize, info);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800194 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800195
196 crypto_xor(info, ctx->salt, ivsize);
197 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
198
199 err = crypto_aead_encrypt(subreq);
200 if (unlikely(info != req->iv))
201 seqiv_aead_encrypt_complete2(req, err);
202 return err;
203}
204
Herbert Xu856e3f402015-05-21 15:11:13 +0800205static int seqiv_aead_decrypt(struct aead_request *req)
206{
207 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu659e7f52015-08-13 17:28:59 +0800208 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xu856e3f402015-05-21 15:11:13 +0800209 struct aead_request *subreq = aead_request_ctx(req);
210 crypto_completion_t compl;
211 void *data;
Herbert Xudd044462015-05-23 15:41:56 +0800212 unsigned int ivsize = 8;
213
214 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
215 return -EINVAL;
Herbert Xu856e3f402015-05-21 15:11:13 +0800216
Herbert Xu659e7f52015-08-13 17:28:59 +0800217 aead_request_set_tfm(subreq, ctx->child);
Herbert Xu856e3f402015-05-21 15:11:13 +0800218
219 compl = req->base.complete;
220 data = req->base.data;
221
Herbert Xu856e3f402015-05-21 15:11:13 +0800222 aead_request_set_callback(subreq, req->base.flags, compl, data);
223 aead_request_set_crypt(subreq, req->src, req->dst,
224 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800225 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800226
227 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
Herbert Xu856e3f402015-05-21 15:11:13 +0800228
229 return crypto_aead_decrypt(subreq);
230}
231
Herbert Xu0a270322007-11-30 21:38:37 +1100232static int seqiv_init(struct crypto_tfm *tfm)
233{
234 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
235 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
Herbert Xueeee12a2015-06-21 19:11:49 +0800236 int err;
Herbert Xu0a270322007-11-30 21:38:37 +1100237
238 spin_lock_init(&ctx->lock);
239
240 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
241
Herbert Xueeee12a2015-06-21 19:11:49 +0800242 err = 0;
243 if (!crypto_get_default_rng()) {
244 crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt;
245 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
246 crypto_ablkcipher_ivsize(geniv));
247 crypto_put_default_rng();
248 }
249
250 return err ?: skcipher_geniv_init(tfm);
Herbert Xu0a270322007-11-30 21:38:37 +1100251}
252
Herbert Xu06771572015-05-23 15:41:51 +0800253static int seqiv_ablkcipher_create(struct crypto_template *tmpl,
254 struct rtattr **tb)
Herbert Xu0a270322007-11-30 21:38:37 +1100255{
256 struct crypto_instance *inst;
Herbert Xu06771572015-05-23 15:41:51 +0800257 int err;
Herbert Xu0a270322007-11-30 21:38:37 +1100258
Herbert Xu06771572015-05-23 15:41:51 +0800259 inst = skcipher_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xu14df4d82007-12-12 12:27:26 +0800260
Herbert Xu0a270322007-11-30 21:38:37 +1100261 if (IS_ERR(inst))
Herbert Xu06771572015-05-23 15:41:51 +0800262 return PTR_ERR(inst);
Herbert Xu0a270322007-11-30 21:38:37 +1100263
Herbert Xu06771572015-05-23 15:41:51 +0800264 err = -EINVAL;
265 if (inst->alg.cra_ablkcipher.ivsize < sizeof(u64))
266 goto free_inst;
Herbert Xuc0ecf892015-01-16 19:51:20 +1100267
Herbert Xu0a270322007-11-30 21:38:37 +1100268 inst->alg.cra_init = seqiv_init;
269 inst->alg.cra_exit = skcipher_geniv_exit;
270
Herbert Xu0a270322007-11-30 21:38:37 +1100271 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
Herbert Xu856e3f402015-05-21 15:11:13 +0800272 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
Herbert Xu0a270322007-11-30 21:38:37 +1100273
Herbert Xu06771572015-05-23 15:41:51 +0800274 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
275
276 err = crypto_register_instance(tmpl, inst);
277 if (err)
278 goto free_inst;
279
Herbert Xu0a270322007-11-30 21:38:37 +1100280out:
Herbert Xu06771572015-05-23 15:41:51 +0800281 return err;
282
283free_inst:
284 skcipher_geniv_free(inst);
285 goto out;
Herbert Xu0a270322007-11-30 21:38:37 +1100286}
287
Herbert Xu06771572015-05-23 15:41:51 +0800288static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800289{
Herbert Xu856e3f402015-05-21 15:11:13 +0800290 struct aead_instance *inst;
291 struct crypto_aead_spawn *spawn;
292 struct aead_alg *alg;
Herbert Xu06771572015-05-23 15:41:51 +0800293 int err;
Herbert Xu14df4d82007-12-12 12:27:26 +0800294
Herbert Xu06771572015-05-23 15:41:51 +0800295 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xu14df4d82007-12-12 12:27:26 +0800296
297 if (IS_ERR(inst))
Herbert Xu06771572015-05-23 15:41:51 +0800298 return PTR_ERR(inst);
299
300 inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
Herbert Xu14df4d82007-12-12 12:27:26 +0800301
Herbert Xu661cfd02015-05-27 14:37:37 +0800302 spawn = aead_instance_ctx(inst);
303 alg = crypto_spawn_aead_alg(spawn);
304
Herbert Xu06771572015-05-23 15:41:51 +0800305 err = -EINVAL;
Herbert Xudd044462015-05-23 15:41:56 +0800306 if (inst->alg.ivsize != sizeof(u64))
Herbert Xu06771572015-05-23 15:41:51 +0800307 goto free_inst;
Herbert Xuc0ecf892015-01-16 19:51:20 +1100308
Herbert Xub7dcfab2015-06-03 14:49:27 +0800309 inst->alg.encrypt = seqiv_aead_encrypt;
Herbert Xu856e3f402015-05-21 15:11:13 +0800310 inst->alg.decrypt = seqiv_aead_decrypt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800311
Herbert Xu659e7f52015-08-13 17:28:59 +0800312 inst->alg.init = aead_init_geniv;
313 inst->alg.exit = aead_exit_geniv;
Herbert Xu856e3f402015-05-21 15:11:13 +0800314
Herbert Xu659e7f52015-08-13 17:28:59 +0800315 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
Herbert Xu5964f262015-07-09 07:17:22 +0800316 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
Herbert Xu856e3f402015-05-21 15:11:13 +0800317
Herbert Xu06771572015-05-23 15:41:51 +0800318 err = aead_register_instance(tmpl, inst);
319 if (err)
320 goto free_inst;
321
Herbert Xu14df4d82007-12-12 12:27:26 +0800322out:
Herbert Xu06771572015-05-23 15:41:51 +0800323 return err;
324
325free_inst:
326 aead_geniv_free(inst);
327 goto out;
Herbert Xu14df4d82007-12-12 12:27:26 +0800328}
329
Herbert Xu06771572015-05-23 15:41:51 +0800330static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800331{
332 struct crypto_attr_type *algt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800333 int err;
334
335 algt = crypto_get_attr_type(tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800336 if (IS_ERR(algt))
Herbert Xu06771572015-05-23 15:41:51 +0800337 return PTR_ERR(algt);
Herbert Xu14df4d82007-12-12 12:27:26 +0800338
339 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
Herbert Xu06771572015-05-23 15:41:51 +0800340 err = seqiv_ablkcipher_create(tmpl, tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800341 else
Herbert Xu06771572015-05-23 15:41:51 +0800342 err = seqiv_aead_create(tmpl, tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800343
Herbert Xu06771572015-05-23 15:41:51 +0800344 return err;
Herbert Xu14df4d82007-12-12 12:27:26 +0800345}
346
347static void seqiv_free(struct crypto_instance *inst)
348{
349 if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
350 skcipher_geniv_free(inst);
351 else
Herbert Xu856e3f402015-05-21 15:11:13 +0800352 aead_geniv_free(aead_instance(inst));
Herbert Xu14df4d82007-12-12 12:27:26 +0800353}
354
Herbert Xu0a270322007-11-30 21:38:37 +1100355static struct crypto_template seqiv_tmpl = {
356 .name = "seqiv",
Herbert Xu06771572015-05-23 15:41:51 +0800357 .create = seqiv_create,
Herbert Xu14df4d82007-12-12 12:27:26 +0800358 .free = seqiv_free,
Herbert Xu0a270322007-11-30 21:38:37 +1100359 .module = THIS_MODULE,
360};
361
362static int __init seqiv_module_init(void)
363{
Herbert Xu8a2cd1c2015-08-13 17:28:53 +0800364 return crypto_register_template(&seqiv_tmpl);
Herbert Xu0a270322007-11-30 21:38:37 +1100365}
366
367static void __exit seqiv_module_exit(void)
368{
369 crypto_unregister_template(&seqiv_tmpl);
370}
371
372module_init(seqiv_module_init);
373module_exit(seqiv_module_exit);
374
375MODULE_LICENSE("GPL");
376MODULE_DESCRIPTION("Sequence Number IV Generator");
Kees Cook4943ba12014-11-24 16:32:38 -0800377MODULE_ALIAS_CRYPTO("seqiv");