]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - crypto/seqiv.c
crypto: aead - Add type-safe geniv init/exit helpers
[mirror_ubuntu-jammy-kernel.git] / crypto / seqiv.c
CommitLineData
0a270321
HX
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
661cfd0e 16#include <crypto/internal/geniv.h>
0a270321 17#include <crypto/internal/skcipher.h>
856e3f40 18#include <crypto/null.h>
a0f000ec 19#include <crypto/rng.h>
856e3f40 20#include <crypto/scatterwalk.h>
0a270321
HX
21#include <linux/err.h>
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
5a0e3ad6 25#include <linux/slab.h>
0a270321
HX
26#include <linux/spinlock.h>
27#include <linux/string.h>
28
29struct seqiv_ctx {
30 spinlock_t lock;
31 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
32};
33
856e3f40 34struct seqiv_aead_ctx {
661cfd0e
HX
35 /* aead_geniv_ctx must be first the element */
36 struct aead_geniv_ctx geniv;
856e3f40
HX
37 struct crypto_blkcipher *null;
38 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
39};
40
0677157b
HX
41static void seqiv_free(struct crypto_instance *inst);
42
0a270321
HX
43static void seqiv_complete2(struct skcipher_givcrypt_request *req, int err)
44{
45 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
46 struct crypto_ablkcipher *geniv;
47
48 if (err == -EINPROGRESS)
49 return;
50
51 if (err)
52 goto out;
53
54 geniv = skcipher_givcrypt_reqtfm(req);
55 memcpy(req->creq.info, subreq->info, crypto_ablkcipher_ivsize(geniv));
56
57out:
58 kfree(subreq->info);
59}
60
61static void seqiv_complete(struct crypto_async_request *base, int err)
62{
63 struct skcipher_givcrypt_request *req = base->data;
64
65 seqiv_complete2(req, err);
66 skcipher_givcrypt_complete(req, err);
67}
68
856e3f40
HX
69static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
70{
71 struct aead_request *subreq = aead_request_ctx(req);
72 struct crypto_aead *geniv;
73
74 if (err == -EINPROGRESS)
75 return;
76
77 if (err)
78 goto out;
79
80 geniv = crypto_aead_reqtfm(req);
81 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
82
83out:
84 kzfree(subreq->iv);
85}
86
87static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
88 int err)
89{
90 struct aead_request *req = base->data;
91
92 seqiv_aead_encrypt_complete2(req, err);
93 aead_request_complete(req, err);
94}
95
14df4d80
HX
96static void seqiv_geniv(struct seqiv_ctx *ctx, u8 *info, u64 seq,
97 unsigned int ivsize)
98{
99 unsigned int len = ivsize;
100
101 if (ivsize > sizeof(u64)) {
102 memset(info, 0, ivsize - sizeof(u64));
103 len = sizeof(u64);
104 }
105 seq = cpu_to_be64(seq);
106 memcpy(info + ivsize - len, &seq, len);
107 crypto_xor(info, ctx->salt, ivsize);
108}
109
0a270321
HX
110static int seqiv_givencrypt(struct skcipher_givcrypt_request *req)
111{
112 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
113 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
114 struct ablkcipher_request *subreq = skcipher_givcrypt_reqctx(req);
3e3dc25f 115 crypto_completion_t compl;
0a270321
HX
116 void *data;
117 u8 *info;
0a270321 118 unsigned int ivsize;
0a270321
HX
119 int err;
120
121 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
122
3e3dc25f 123 compl = req->creq.base.complete;
0a270321
HX
124 data = req->creq.base.data;
125 info = req->creq.info;
126
127 ivsize = crypto_ablkcipher_ivsize(geniv);
128
129 if (unlikely(!IS_ALIGNED((unsigned long)info,
130 crypto_ablkcipher_alignmask(geniv) + 1))) {
131 info = kmalloc(ivsize, req->creq.base.flags &
132 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
133 GFP_ATOMIC);
134 if (!info)
135 return -ENOMEM;
136
3e3dc25f 137 compl = seqiv_complete;
0a270321
HX
138 data = req;
139 }
140
3e3dc25f 141 ablkcipher_request_set_callback(subreq, req->creq.base.flags, compl,
0a270321
HX
142 data);
143 ablkcipher_request_set_crypt(subreq, req->creq.src, req->creq.dst,
144 req->creq.nbytes, info);
145
14df4d80 146 seqiv_geniv(ctx, info, req->seq, ivsize);
0a270321
HX
147 memcpy(req->giv, info, ivsize);
148
149 err = crypto_ablkcipher_encrypt(subreq);
150 if (unlikely(info != req->creq.info))
151 seqiv_complete2(req, err);
152 return err;
153}
154
856e3f40
HX
155static int seqiv_aead_encrypt(struct aead_request *req)
156{
157 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
158 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
159 struct aead_request *subreq = aead_request_ctx(req);
160 crypto_completion_t compl;
161 void *data;
162 u8 *info;
dd04446e 163 unsigned int ivsize = 8;
856e3f40
HX
164 int err;
165
dd04446e
HX
166 if (req->cryptlen < ivsize)
167 return -EINVAL;
168
661cfd0e 169 aead_request_set_tfm(subreq, ctx->geniv.child);
856e3f40
HX
170
171 compl = req->base.complete;
172 data = req->base.data;
173 info = req->iv;
174
856e3f40 175 if (req->src != req->dst) {
856e3f40
HX
176 struct blkcipher_desc desc = {
177 .tfm = ctx->null,
178 };
179
d0ad1b24
HX
180 err = crypto_blkcipher_encrypt(&desc, req->dst, req->src,
181 req->assoclen + req->cryptlen);
856e3f40
HX
182 if (err)
183 return err;
184 }
185
186 if (unlikely(!IS_ALIGNED((unsigned long)info,
187 crypto_aead_alignmask(geniv) + 1))) {
188 info = kmalloc(ivsize, req->base.flags &
189 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
190 GFP_ATOMIC);
191 if (!info)
192 return -ENOMEM;
193
194 memcpy(info, req->iv, ivsize);
195 compl = seqiv_aead_encrypt_complete;
196 data = req;
197 }
198
199 aead_request_set_callback(subreq, req->base.flags, compl, data);
200 aead_request_set_crypt(subreq, req->dst, req->dst,
201 req->cryptlen - ivsize, info);
374d4ad1 202 aead_request_set_ad(subreq, req->assoclen + ivsize);
856e3f40
HX
203
204 crypto_xor(info, ctx->salt, ivsize);
205 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
206
207 err = crypto_aead_encrypt(subreq);
208 if (unlikely(info != req->iv))
209 seqiv_aead_encrypt_complete2(req, err);
210 return err;
211}
212
856e3f40
HX
213static int seqiv_aead_decrypt(struct aead_request *req)
214{
215 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
216 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
217 struct aead_request *subreq = aead_request_ctx(req);
218 crypto_completion_t compl;
219 void *data;
dd04446e
HX
220 unsigned int ivsize = 8;
221
222 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
223 return -EINVAL;
856e3f40 224
661cfd0e 225 aead_request_set_tfm(subreq, ctx->geniv.child);
856e3f40
HX
226
227 compl = req->base.complete;
228 data = req->base.data;
229
856e3f40
HX
230 aead_request_set_callback(subreq, req->base.flags, compl, data);
231 aead_request_set_crypt(subreq, req->src, req->dst,
232 req->cryptlen - ivsize, req->iv);
374d4ad1 233 aead_request_set_ad(subreq, req->assoclen + ivsize);
856e3f40
HX
234
235 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
856e3f40
HX
236
237 return crypto_aead_decrypt(subreq);
238}
239
0a270321
HX
240static int seqiv_init(struct crypto_tfm *tfm)
241{
242 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
243 struct seqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
eeee12aa 244 int err;
0a270321
HX
245
246 spin_lock_init(&ctx->lock);
247
248 tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request);
249
eeee12aa
HX
250 err = 0;
251 if (!crypto_get_default_rng()) {
252 crypto_ablkcipher_crt(geniv)->givencrypt = seqiv_givencrypt;
253 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
254 crypto_ablkcipher_ivsize(geniv));
255 crypto_put_default_rng();
256 }
257
258 return err ?: skcipher_geniv_init(tfm);
0a270321
HX
259}
260
5964f26c
HX
261static int seqiv_aead_init_common(struct crypto_aead *geniv,
262 unsigned int reqsize)
856e3f40 263{
856e3f40
HX
264 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(geniv);
265 int err;
266
661cfd0e 267 spin_lock_init(&ctx->geniv.lock);
856e3f40
HX
268
269 crypto_aead_set_reqsize(geniv, sizeof(struct aead_request));
270
eeee12aa
HX
271 err = crypto_get_default_rng();
272 if (err)
273 goto out;
274
b7dcfab4
HX
275 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
276 crypto_aead_ivsize(geniv));
eeee12aa 277 crypto_put_default_rng();
b7dcfab4
HX
278 if (err)
279 goto out;
280
856e3f40
HX
281 ctx->null = crypto_get_default_null_skcipher();
282 err = PTR_ERR(ctx->null);
283 if (IS_ERR(ctx->null))
284 goto out;
285
5964f26c 286 err = aead_geniv_init(crypto_aead_tfm(geniv));
856e3f40
HX
287 if (err)
288 goto drop_null;
289
661cfd0e 290 ctx->geniv.child = geniv->child;
856e3f40
HX
291 geniv->child = geniv;
292
293out:
294 return err;
295
296drop_null:
297 crypto_put_default_null_skcipher();
298 goto out;
299}
300
5964f26c 301static int seqiv_aead_init(struct crypto_aead *tfm)
856e3f40 302{
dd04446e
HX
303 return seqiv_aead_init_common(tfm, sizeof(struct aead_request));
304}
856e3f40 305
5964f26c 306static void seqiv_aead_exit(struct crypto_aead *tfm)
856e3f40 307{
5964f26c 308 struct seqiv_aead_ctx *ctx = crypto_aead_ctx(tfm);
856e3f40 309
661cfd0e 310 crypto_free_aead(ctx->geniv.child);
856e3f40
HX
311 crypto_put_default_null_skcipher();
312}
313
0677157b
HX
314static int seqiv_ablkcipher_create(struct crypto_template *tmpl,
315 struct rtattr **tb)
0a270321
HX
316{
317 struct crypto_instance *inst;
0677157b 318 int err;
0a270321 319
0677157b 320 inst = skcipher_geniv_alloc(tmpl, tb, 0, 0);
14df4d80 321
0a270321 322 if (IS_ERR(inst))
0677157b 323 return PTR_ERR(inst);
0a270321 324
0677157b
HX
325 err = -EINVAL;
326 if (inst->alg.cra_ablkcipher.ivsize < sizeof(u64))
327 goto free_inst;
c0ecf891 328
0a270321
HX
329 inst->alg.cra_init = seqiv_init;
330 inst->alg.cra_exit = skcipher_geniv_exit;
331
0a270321 332 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
856e3f40 333 inst->alg.cra_ctxsize += sizeof(struct seqiv_ctx);
0a270321 334
0677157b
HX
335 inst->alg.cra_alignmask |= __alignof__(u32) - 1;
336
337 err = crypto_register_instance(tmpl, inst);
338 if (err)
339 goto free_inst;
340
0a270321 341out:
0677157b
HX
342 return err;
343
344free_inst:
345 skcipher_geniv_free(inst);
346 goto out;
0a270321
HX
347}
348
0677157b 349static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
14df4d80 350{
856e3f40
HX
351 struct aead_instance *inst;
352 struct crypto_aead_spawn *spawn;
353 struct aead_alg *alg;
0677157b 354 int err;
14df4d80 355
0677157b 356 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
14df4d80
HX
357
358 if (IS_ERR(inst))
0677157b
HX
359 return PTR_ERR(inst);
360
361 inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
14df4d80 362
661cfd0e
HX
363 spawn = aead_instance_ctx(inst);
364 alg = crypto_spawn_aead_alg(spawn);
365
0677157b 366 err = -EINVAL;
dd04446e 367 if (inst->alg.ivsize != sizeof(u64))
0677157b 368 goto free_inst;
c0ecf891 369
b7dcfab4 370 inst->alg.encrypt = seqiv_aead_encrypt;
856e3f40 371 inst->alg.decrypt = seqiv_aead_decrypt;
14df4d80 372
5964f26c
HX
373 inst->alg.init = seqiv_aead_init;
374 inst->alg.exit = seqiv_aead_exit;
856e3f40
HX
375
376 inst->alg.base.cra_ctxsize = sizeof(struct seqiv_aead_ctx);
5964f26c 377 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
856e3f40 378
0677157b
HX
379 err = aead_register_instance(tmpl, inst);
380 if (err)
381 goto free_inst;
382
14df4d80 383out:
0677157b
HX
384 return err;
385
386free_inst:
387 aead_geniv_free(inst);
388 goto out;
14df4d80
HX
389}
390
0677157b 391static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
14df4d80
HX
392{
393 struct crypto_attr_type *algt;
14df4d80
HX
394 int err;
395
396 algt = crypto_get_attr_type(tb);
14df4d80 397 if (IS_ERR(algt))
0677157b 398 return PTR_ERR(algt);
14df4d80
HX
399
400 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
0677157b 401 err = seqiv_ablkcipher_create(tmpl, tb);
14df4d80 402 else
0677157b 403 err = seqiv_aead_create(tmpl, tb);
14df4d80 404
0677157b 405 return err;
14df4d80
HX
406}
407
408static void seqiv_free(struct crypto_instance *inst)
409{
410 if ((inst->alg.cra_flags ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
411 skcipher_geniv_free(inst);
412 else
856e3f40 413 aead_geniv_free(aead_instance(inst));
14df4d80
HX
414}
415
0a270321
HX
416static struct crypto_template seqiv_tmpl = {
417 .name = "seqiv",
0677157b 418 .create = seqiv_create,
14df4d80 419 .free = seqiv_free,
0a270321
HX
420 .module = THIS_MODULE,
421};
422
423static int __init seqiv_module_init(void)
424{
8a2cd1c4 425 return crypto_register_template(&seqiv_tmpl);
0a270321
HX
426}
427
428static void __exit seqiv_module_exit(void)
429{
430 crypto_unregister_template(&seqiv_tmpl);
431}
432
433module_init(seqiv_module_init);
434module_exit(seqiv_module_exit);
435
436MODULE_LICENSE("GPL");
437MODULE_DESCRIPTION("Sequence Number IV Generator");
4943ba16 438MODULE_ALIAS_CRYPTO("seqiv");