Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * seqiv: Sequence Number IV Generator
4 *
5 * This generator generates an IV based on a sequence number by xoring it
6 * with a salt. This algorithm is mainly useful for CTR and similar modes.
7 *
8 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
9 */
10
11#include <crypto/internal/geniv.h>
12#include <crypto/scatterwalk.h>
13#include <crypto/skcipher.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/string.h>
20
21static void seqiv_free(struct crypto_instance *inst);
22
23static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
24{
25 struct aead_request *subreq = aead_request_ctx(req);
26 struct crypto_aead *geniv;
27
28 if (err == -EINPROGRESS)
29 return;
30
31 if (err)
32 goto out;
33
34 geniv = crypto_aead_reqtfm(req);
35 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
36
37out:
38 kzfree(subreq->iv);
39}
40
41static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
42 int err)
43{
44 struct aead_request *req = base->data;
45
46 seqiv_aead_encrypt_complete2(req, err);
47 aead_request_complete(req, err);
48}
49
50static int seqiv_aead_encrypt(struct aead_request *req)
51{
52 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
53 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
54 struct aead_request *subreq = aead_request_ctx(req);
55 crypto_completion_t compl;
56 void *data;
57 u8 *info;
58 unsigned int ivsize = 8;
59 int err;
60
61 if (req->cryptlen < ivsize)
62 return -EINVAL;
63
64 aead_request_set_tfm(subreq, ctx->child);
65
66 compl = req->base.complete;
67 data = req->base.data;
68 info = req->iv;
69
70 if (req->src != req->dst) {
71 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
72
73 skcipher_request_set_sync_tfm(nreq, ctx->sknull);
74 skcipher_request_set_callback(nreq, req->base.flags,
75 NULL, NULL);
76 skcipher_request_set_crypt(nreq, req->src, req->dst,
77 req->assoclen + req->cryptlen,
78 NULL);
79
80 err = crypto_skcipher_encrypt(nreq);
81 if (err)
82 return err;
83 }
84
85 if (unlikely(!IS_ALIGNED((unsigned long)info,
86 crypto_aead_alignmask(geniv) + 1))) {
87 info = kmemdup(req->iv, ivsize, req->base.flags &
88 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
89 GFP_ATOMIC);
90 if (!info)
91 return -ENOMEM;
92
93 compl = seqiv_aead_encrypt_complete;
94 data = req;
95 }
96
97 aead_request_set_callback(subreq, req->base.flags, compl, data);
98 aead_request_set_crypt(subreq, req->dst, req->dst,
99 req->cryptlen - ivsize, info);
100 aead_request_set_ad(subreq, req->assoclen + ivsize);
101
102 crypto_xor(info, ctx->salt, ivsize);
103 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
104
105 err = crypto_aead_encrypt(subreq);
106 if (unlikely(info != req->iv))
107 seqiv_aead_encrypt_complete2(req, err);
108 return err;
109}
110
111static int seqiv_aead_decrypt(struct aead_request *req)
112{
113 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
114 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
115 struct aead_request *subreq = aead_request_ctx(req);
116 crypto_completion_t compl;
117 void *data;
118 unsigned int ivsize = 8;
119
120 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
121 return -EINVAL;
122
123 aead_request_set_tfm(subreq, ctx->child);
124
125 compl = req->base.complete;
126 data = req->base.data;
127
128 aead_request_set_callback(subreq, req->base.flags, compl, data);
129 aead_request_set_crypt(subreq, req->src, req->dst,
130 req->cryptlen - ivsize, req->iv);
131 aead_request_set_ad(subreq, req->assoclen + ivsize);
132
133 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
134
135 return crypto_aead_decrypt(subreq);
136}
137
138static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
139{
140 struct aead_instance *inst;
141 int err;
142
143 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
144
145 if (IS_ERR(inst))
146 return PTR_ERR(inst);
147
148 err = -EINVAL;
149 if (inst->alg.ivsize != sizeof(u64))
150 goto free_inst;
151
152 inst->alg.encrypt = seqiv_aead_encrypt;
153 inst->alg.decrypt = seqiv_aead_decrypt;
154
155 inst->alg.init = aead_init_geniv;
156 inst->alg.exit = aead_exit_geniv;
157
158 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
159 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
160
161 err = aead_register_instance(tmpl, inst);
162 if (err)
163 goto free_inst;
164
165out:
166 return err;
167
168free_inst:
169 aead_geniv_free(inst);
170 goto out;
171}
172
173static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
174{
175 struct crypto_attr_type *algt;
176
177 algt = crypto_get_attr_type(tb);
178 if (IS_ERR(algt))
179 return PTR_ERR(algt);
180
181 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
182 return -EINVAL;
183
184 return seqiv_aead_create(tmpl, tb);
185}
186
187static void seqiv_free(struct crypto_instance *inst)
188{
189 aead_geniv_free(aead_instance(inst));
190}
191
192static struct crypto_template seqiv_tmpl = {
193 .name = "seqiv",
194 .create = seqiv_create,
195 .free = seqiv_free,
196 .module = THIS_MODULE,
197};
198
199static int __init seqiv_module_init(void)
200{
201 return crypto_register_template(&seqiv_tmpl);
202}
203
204static void __exit seqiv_module_exit(void)
205{
206 crypto_unregister_template(&seqiv_tmpl);
207}
208
209subsys_initcall(seqiv_module_init);
210module_exit(seqiv_module_exit);
211
212MODULE_LICENSE("GPL");
213MODULE_DESCRIPTION("Sequence Number IV Generator");
214MODULE_ALIAS_CRYPTO("seqiv");
1/*
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
16#include <crypto/internal/aead.h>
17#include <crypto/internal/skcipher.h>
18#include <crypto/rng.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/spinlock.h>
25#include <linux/string.h>
26
27struct seqiv_ctx {
28 spinlock_t lock;
29 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
30};
31
32static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err)
33{
34 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
35 struct crypto_ablkcipher *geniv;
36
37 if (err == -EINPROGRESS)
38 return;
39
40 if (err)
41 goto out;
42
43 geniv = skcipher_givcrypt_reqtfm(req);
44 memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv));
45
46out:
47 kfree(subreq->info);
48}
49
50static void seqiv_complete(struct crypto_async_request *base, int err)
51{
52 struct skcipher_givcrypt_request *req = base->data;
53
54 seqiv_complete2(req, err);
55 skcipher_givcrypt_complete(req, err);
56}
57
58static void seqiv_aead_complete2(struct aead_givcrypt_request *req, int err)
59{
60 struct aead_request *subreq = aead_givcrypt_reqctx(req);
61 struct crypto_aead *geniv;
62
63 if (err == -EINPROGRESS)
64 return;
65
66 if (err)
67 goto out;
68
69 geniv = aead_givcrypt_reqtfm(req);
70 memcpy(req->areq.iv, subreq->iv, crypto_aead_ivsize(geniv));
71
72out:
73 kfree(subreq->iv);
74}
75
76static void seqiv_aead_complete(struct crypto_async_request *base, int err)
77{
78 struct aead_givcrypt_request *req = base->data;
79
80 seqiv_aead_complete2(req, err);
81 aead_givcrypt_complete(req, err);
82}
83
84static void seqiv_geniv(struct seqiv_ctx *ctx, u8 *info, u64 seq,
85 unsigned int ivsize)
86{
87 unsigned int len = ivsize;
88
89 if (ivsize > sizeof(u64)) {
90 memset(info, 0, ivsize - sizeof(u64));
91 len = sizeof(u64);
92 }
93 seq = cpu_to_be64(seq);
94 memcpy(info + ivsize - len, &seq, len);
95 crypto_xor(info, ctx->salt, ivsize);
96}
97
98static int seqiv_givencrypt(struct skcipher_givcrypt_request *req)
99{
100 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
101 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
102 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
103 crypto_completion_t complete;
104 void *data;
105 u8 *info;
106 unsigned int ivsize;
107 int err;
108
109 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
110
111 complete = req->creq.base.complete;
112 data = req->creq.base.data;
113 info = req->creq.info;
114
115 ivsize = crypto_ablkcipher_ivsize(geniv);
116
117 if (unlikely(!IS_ALIGNED((unsigned long)info,
118 crypto_ablkcipher_alignmask(geniv) + 1))) {
119 info = kmalloc(ivsize, req->creq.base.flags &
120 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
121 GFP_ATOMIC);
122 if (!info)
123 return -ENOMEM;
124
125 complete = seqiv_complete;
126 data = req;
127 }
128
129 ablkcipher_request_set_callback(subreq, req->creq.base.flags, complete,
130 data);
131 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
132 req->creq.nbytes, info);
133
134 seqiv_geniv(ctx, info, req->seq, ivsize);
135 memcpy(req->giv, info, ivsize);
136
137 err = crypto_ablkcipher_encrypt(subreq);
138 if (unlikely(info != req->creq.info))
139 seqiv_complete2(req, err);
140 return err;
141}
142
143static int seqiv_aead_givencrypt(struct aead_givcrypt_request *req)
144{
145 struct crypto_aead *geniv = aead_givcrypt_reqtfm(req);
146 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
147 struct aead_request *areq = &req->areq;
148 struct aead_request *subreq = aead_givcrypt_reqctx(req);
149 crypto_completion_t complete;
150 void *data;
151 u8 *info;
152 unsigned int ivsize;
153 int err;
154
155 aead_request_set_tfm(subreq, aead_geniv_base(geniv));
156
157 complete = areq->base.complete;
158 data = areq->base.data;
159 info = areq->iv;
160
161 ivsize = crypto_aead_ivsize(geniv);
162
163 if (unlikely(!IS_ALIGNED((unsigned long)info,
164 crypto_aead_alignmask(geniv) + 1))) {
165 info = kmalloc(ivsize, areq->base.flags &
166 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
167 GFP_ATOMIC);
168 if (!info)
169 return -ENOMEM;
170
171 complete = seqiv_aead_complete;
172 data = req;
173 }
174
175 aead_request_set_callback(subreq, areq->base.flags, complete, data);
176 aead_request_set_crypt(subreq, areq->src, areq->dst, areq->cryptlen,
177 info);
178 aead_request_set_assoc(subreq, areq->assoc, areq->assoclen);
179
180 seqiv_geniv(ctx, info, req->seq, ivsize);
181 memcpy(req->giv, info, ivsize);
182
183 err = crypto_aead_encrypt(subreq);
184 if (unlikely(info != areq->iv))
185 seqiv_aead_complete2(req, err);
186 return err;
187}
188
189static int seqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
190{
191 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
192 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
193 int err = 0;
194
195 spin_lock_bh(&ctx->lock);
196 if (crypto_ablkcipher_crt(geniv)->givencrypt != seqiv_givencrypt_first)
197 goto unlock;
198
199 crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt;
200 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
201 crypto_ablkcipher_ivsize(geniv));
202
203unlock:
204 spin_unlock_bh(&ctx->lock);
205
206 if (err)
207 return err;
208
209 return seqiv_givencrypt(req);
210}
211
212static int seqiv_aead_givencrypt_first(struct aead_givcrypt_request *req)
213{
214 struct crypto_aead *geniv = aead_givcrypt_reqtfm(req);
215 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
216 int err = 0;
217
218 spin_lock_bh(&ctx->lock);
219 if (crypto_aead_crt(geniv)->givencrypt != seqiv_aead_givencrypt_first)
220 goto unlock;
221
222 crypto_aead_crt(geniv)->givencrypt = seqiv_aead_givencrypt;
223 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
224 crypto_aead_ivsize(geniv));
225
226unlock:
227 spin_unlock_bh(&ctx->lock);
228
229 if (err)
230 return err;
231
232 return seqiv_aead_givencrypt(req);
233}
234
235static int seqiv_init(struct crypto_tfm *tfm)
236{
237 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
238 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
239
240 spin_lock_init(&ctx->lock);
241
242 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
243
244 return skcipher_geniv_init(tfm);
245}
246
247static int seqiv_aead_init(struct crypto_tfm *tfm)
248{
249 struct crypto_aead *geniv = __crypto_aead_cast(tfm);
250 struct seqiv_ctx *ctx = crypto_aead_ctx(geniv);
251
252 spin_lock_init(&ctx->lock);
253
254 tfm->crt_aead.reqsize = sizeof(struct aead_request);
255
256 return aead_geniv_init(tfm);
257}
258
259static struct crypto_template seqiv_tmpl;
260
261static struct crypto_instance *seqiv_ablkcipher_alloc(struct rtattr **tb)
262{
263 struct crypto_instance *inst;
264
265 inst = skcipher_geniv_alloc(&seqiv_tmpl, tb, 0, 0);
266
267 if (IS_ERR(inst))
268 goto out;
269
270 inst->alg.cra_ablkcipher.givencrypt = seqiv_givencrypt_first;
271
272 inst->alg.cra_init = seqiv_init;
273 inst->alg.cra_exit = skcipher_geniv_exit;
274
275 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
276
277out:
278 return inst;
279}
280
281static struct crypto_instance *seqiv_aead_alloc(struct rtattr **tb)
282{
283 struct crypto_instance *inst;
284
285 inst = aead_geniv_alloc(&seqiv_tmpl, tb, 0, 0);
286
287 if (IS_ERR(inst))
288 goto out;
289
290 inst->alg.cra_aead.givencrypt = seqiv_aead_givencrypt_first;
291
292 inst->alg.cra_init = seqiv_aead_init;
293 inst->alg.cra_exit = aead_geniv_exit;
294
295 inst->alg.cra_ctxsize = inst->alg.cra_aead.ivsize;
296
297out:
298 return inst;
299}
300
301static struct crypto_instance *seqiv_alloc(struct rtattr **tb)
302{
303 struct crypto_attr_type *algt;
304 struct crypto_instance *inst;
305 int err;
306
307 algt = crypto_get_attr_type(tb);
308 err = PTR_ERR(algt);
309 if (IS_ERR(algt))
310 return ERR_PTR(err);
311
312 err = crypto_get_default_rng();
313 if (err)
314 return ERR_PTR(err);
315
316 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
317 inst = seqiv_ablkcipher_alloc(tb);
318 else
319 inst = seqiv_aead_alloc(tb);
320
321 if (IS_ERR(inst))
322 goto put_rng;
323
324 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
325 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
326
327out:
328 return inst;
329
330put_rng:
331 crypto_put_default_rng();
332 goto out;
333}
334
335static void seqiv_free(struct crypto_instance *inst)
336{
337 if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
338 skcipher_geniv_free(inst);
339 else
340 aead_geniv_free(inst);
341 crypto_put_default_rng();
342}
343
344static struct crypto_template seqiv_tmpl = {
345 .name = "seqiv",
346 .alloc = seqiv_alloc,
347 .free = seqiv_free,
348 .module = THIS_MODULE,
349};
350
351static int __init seqiv_module_init(void)
352{
353 return crypto_register_template(&seqiv_tmpl);
354}
355
356static void __exit seqiv_module_exit(void)
357{
358 crypto_unregister_template(&seqiv_tmpl);
359}
360
361module_init(seqiv_module_init);
362module_exit(seqiv_module_exit);
363
364MODULE_LICENSE("GPL");
365MODULE_DESCRIPTION("Sequence Number IV Generator");