Merge tag 'riscv-for-linus-6.4-mw1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / crypto / ctr.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
23e353c8
JL
2/*
3 * CTR: Counter mode
4 *
5 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
23e353c8
JL
6 */
7
8#include <crypto/algapi.h>
5311f248 9#include <crypto/ctr.h>
0eb76ba2 10#include <crypto/internal/cipher.h>
69d3150c 11#include <crypto/internal/skcipher.h>
23e353c8
JL
12#include <linux/err.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
23e353c8
JL
16#include <linux/slab.h>
17
5311f248 18struct crypto_rfc3686_ctx {
b2b39c2f 19 struct crypto_skcipher *child;
5311f248 20 u8 nonce[CTR_RFC3686_NONCE_SIZE];
23e353c8
JL
21};
22
69d3150c
JK
23struct crypto_rfc3686_req_ctx {
24 u8 iv[CTR_RFC3686_BLOCK_SIZE];
b2b39c2f 25 struct skcipher_request subreq CRYPTO_MINALIGN_ATTR;
69d3150c
JK
26};
27
11f14630 28static void crypto_ctr_crypt_final(struct skcipher_walk *walk,
5311f248 29 struct crypto_cipher *tfm)
0971eb0d
HX
30{
31 unsigned int bsize = crypto_cipher_blocksize(tfm);
5311f248
HX
32 unsigned long alignmask = crypto_cipher_alignmask(tfm);
33 u8 *ctrblk = walk->iv;
6650c4de 34 u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
5311f248 35 u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
0971eb0d
HX
36 u8 *src = walk->src.virt.addr;
37 u8 *dst = walk->dst.virt.addr;
38 unsigned int nbytes = walk->nbytes;
39
40 crypto_cipher_encrypt_one(tfm, keystream, ctrblk);
45fe93df 41 crypto_xor_cpy(dst, keystream, src, nbytes);
5311f248
HX
42
43 crypto_inc(ctrblk, bsize);
0971eb0d
HX
44}
45
11f14630 46static int crypto_ctr_crypt_segment(struct skcipher_walk *walk,
5311f248 47 struct crypto_cipher *tfm)
23e353c8
JL
48{
49 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
50 crypto_cipher_alg(tfm)->cia_encrypt;
51 unsigned int bsize = crypto_cipher_blocksize(tfm);
5311f248 52 u8 *ctrblk = walk->iv;
23e353c8
JL
53 u8 *src = walk->src.virt.addr;
54 u8 *dst = walk->dst.virt.addr;
55 unsigned int nbytes = walk->nbytes;
56
57 do {
58 /* create keystream */
0971eb0d
HX
59 fn(crypto_cipher_tfm(tfm), dst, ctrblk);
60 crypto_xor(dst, src, bsize);
23e353c8
JL
61
62 /* increment counter in counterblock */
5311f248 63 crypto_inc(ctrblk, bsize);
23e353c8 64
23e353c8
JL
65 src += bsize;
66 dst += bsize;
0971eb0d 67 } while ((nbytes -= bsize) >= bsize);
23e353c8 68
0971eb0d 69 return nbytes;
23e353c8
JL
70}
71
11f14630 72static int crypto_ctr_crypt_inplace(struct skcipher_walk *walk,
5311f248 73 struct crypto_cipher *tfm)
23e353c8
JL
74{
75 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
76 crypto_cipher_alg(tfm)->cia_encrypt;
77 unsigned int bsize = crypto_cipher_blocksize(tfm);
5311f248 78 unsigned long alignmask = crypto_cipher_alignmask(tfm);
23e353c8 79 unsigned int nbytes = walk->nbytes;
5311f248 80 u8 *ctrblk = walk->iv;
23e353c8 81 u8 *src = walk->src.virt.addr;
6650c4de 82 u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
5311f248 83 u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
23e353c8
JL
84
85 do {
86 /* create keystream */
87 fn(crypto_cipher_tfm(tfm), keystream, ctrblk);
0971eb0d 88 crypto_xor(src, keystream, bsize);
23e353c8
JL
89
90 /* increment counter in counterblock */
5311f248 91 crypto_inc(ctrblk, bsize);
23e353c8 92
23e353c8 93 src += bsize;
0971eb0d 94 } while ((nbytes -= bsize) >= bsize);
23e353c8 95
0971eb0d 96 return nbytes;
23e353c8
JL
97}
98
11f14630 99static int crypto_ctr_crypt(struct skcipher_request *req)
23e353c8 100{
11f14630
EB
101 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
102 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
103 const unsigned int bsize = crypto_cipher_blocksize(cipher);
104 struct skcipher_walk walk;
105 unsigned int nbytes;
23e353c8
JL
106 int err;
107
11f14630 108 err = skcipher_walk_virt(&walk, req, false);
23e353c8 109
0971eb0d 110 while (walk.nbytes >= bsize) {
23e353c8 111 if (walk.src.virt.addr == walk.dst.virt.addr)
11f14630 112 nbytes = crypto_ctr_crypt_inplace(&walk, cipher);
23e353c8 113 else
11f14630 114 nbytes = crypto_ctr_crypt_segment(&walk, cipher);
23e353c8 115
11f14630 116 err = skcipher_walk_done(&walk, nbytes);
23e353c8 117 }
0971eb0d
HX
118
119 if (walk.nbytes) {
11f14630
EB
120 crypto_ctr_crypt_final(&walk, cipher);
121 err = skcipher_walk_done(&walk, 0);
0971eb0d
HX
122 }
123
23e353c8
JL
124 return err;
125}
126
11f14630 127static int crypto_ctr_create(struct crypto_template *tmpl, struct rtattr **tb)
23e353c8 128{
11f14630 129 struct skcipher_instance *inst;
23e353c8 130 struct crypto_alg *alg;
23e353c8
JL
131 int err;
132
b3c16bfc 133 inst = skcipher_alloc_instance_simple(tmpl, tb);
11f14630
EB
134 if (IS_ERR(inst))
135 return PTR_ERR(inst);
23e353c8 136
b3c16bfc
HX
137 alg = skcipher_ialg_simple(inst);
138
5311f248 139 /* Block size must be >= 4 bytes. */
23e353c8 140 err = -EINVAL;
5311f248 141 if (alg->cra_blocksize < 4)
11f14630 142 goto out_free_inst;
23e353c8 143
3f8214ea 144 /* If this is false we'd fail the alignment of crypto_inc. */
5311f248 145 if (alg->cra_blocksize % 4)
11f14630 146 goto out_free_inst;
23e353c8 147
11f14630
EB
148 /* CTR mode is a stream cipher. */
149 inst->alg.base.cra_blocksize = 1;
23e353c8 150
11f14630
EB
151 /*
152 * To simplify the implementation, configure the skcipher walk to only
153 * give a partial block at the very end, never earlier.
154 */
155 inst->alg.chunksize = alg->cra_blocksize;
23e353c8 156
11f14630
EB
157 inst->alg.encrypt = crypto_ctr_crypt;
158 inst->alg.decrypt = crypto_ctr_crypt;
23e353c8 159
11f14630 160 err = skcipher_register_instance(tmpl, inst);
b3c16bfc 161 if (err) {
11f14630 162out_free_inst:
b3c16bfc
HX
163 inst->free(inst);
164 }
165
11f14630 166 return err;
23e353c8
JL
167}
168
b2b39c2f 169static int crypto_rfc3686_setkey(struct crypto_skcipher *parent,
69d3150c 170 const u8 *key, unsigned int keylen)
5311f248 171{
b2b39c2f
HX
172 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(parent);
173 struct crypto_skcipher *child = ctx->child;
5311f248
HX
174
175 /* the nonce is stored in bytes at end of key */
176 if (keylen < CTR_RFC3686_NONCE_SIZE)
177 return -EINVAL;
178
179 memcpy(ctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
180 CTR_RFC3686_NONCE_SIZE);
181
182 keylen -= CTR_RFC3686_NONCE_SIZE;
183
b2b39c2f
HX
184 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
185 crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
186 CRYPTO_TFM_REQ_MASK);
af5034e8 187 return crypto_skcipher_setkey(child, key, keylen);
5311f248
HX
188}
189
b2b39c2f 190static int crypto_rfc3686_crypt(struct skcipher_request *req)
5311f248 191{
b2b39c2f
HX
192 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
193 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
194 struct crypto_skcipher *child = ctx->child;
195 unsigned long align = crypto_skcipher_alignmask(tfm);
69d3150c 196 struct crypto_rfc3686_req_ctx *rctx =
b2b39c2f
HX
197 (void *)PTR_ALIGN((u8 *)skcipher_request_ctx(req), align + 1);
198 struct skcipher_request *subreq = &rctx->subreq;
69d3150c 199 u8 *iv = rctx->iv;
5311f248
HX
200
201 /* set up counter block */
202 memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
b2b39c2f 203 memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
5311f248
HX
204
205 /* initialize counter portion of counter block */
206 *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
207 cpu_to_be32(1);
208
b2b39c2f
HX
209 skcipher_request_set_tfm(subreq, child);
210 skcipher_request_set_callback(subreq, req->base.flags,
211 req->base.complete, req->base.data);
212 skcipher_request_set_crypt(subreq, req->src, req->dst,
213 req->cryptlen, iv);
5311f248 214
b2b39c2f 215 return crypto_skcipher_encrypt(subreq);
5311f248
HX
216}
217
b2b39c2f 218static int crypto_rfc3686_init_tfm(struct crypto_skcipher *tfm)
5311f248 219{
b2b39c2f
HX
220 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
221 struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
222 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
223 struct crypto_skcipher *cipher;
69d3150c 224 unsigned long align;
b2b39c2f 225 unsigned int reqsize;
5311f248 226
60425a8b 227 cipher = crypto_spawn_skcipher(spawn);
5311f248
HX
228 if (IS_ERR(cipher))
229 return PTR_ERR(cipher);
230
231 ctx->child = cipher;
232
b2b39c2f 233 align = crypto_skcipher_alignmask(tfm);
69d3150c 234 align &= ~(crypto_tfm_ctx_alignment() - 1);
b2b39c2f
HX
235 reqsize = align + sizeof(struct crypto_rfc3686_req_ctx) +
236 crypto_skcipher_reqsize(cipher);
237 crypto_skcipher_set_reqsize(tfm, reqsize);
69d3150c 238
5311f248
HX
239 return 0;
240}
241
b2b39c2f 242static void crypto_rfc3686_exit_tfm(struct crypto_skcipher *tfm)
5311f248 243{
b2b39c2f
HX
244 struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
245
246 crypto_free_skcipher(ctx->child);
247}
5311f248 248
b2b39c2f
HX
249static void crypto_rfc3686_free(struct skcipher_instance *inst)
250{
251 struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
252
253 crypto_drop_skcipher(spawn);
254 kfree(inst);
5311f248
HX
255}
256
b2b39c2f
HX
257static int crypto_rfc3686_create(struct crypto_template *tmpl,
258 struct rtattr **tb)
5311f248 259{
b2b39c2f
HX
260 struct skcipher_instance *inst;
261 struct skcipher_alg *alg;
69d3150c 262 struct crypto_skcipher_spawn *spawn;
d2c2a85c 263 u32 mask;
5311f248
HX
264 int err;
265
7bcb2c99
EB
266 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
267 if (err)
268 return err;
69d3150c 269
69d3150c
JK
270 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
271 if (!inst)
b2b39c2f 272 return -ENOMEM;
69d3150c 273
b2b39c2f 274 spawn = skcipher_instance_ctx(inst);
69d3150c 275
b9f76ddd 276 err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst),
a108dfcf 277 crypto_attr_alg_name(tb[1]), 0, mask);
69d3150c
JK
278 if (err)
279 goto err_free_inst;
280
b2b39c2f 281 alg = crypto_spawn_skcipher_alg(spawn);
69d3150c 282
5311f248
HX
283 /* We only support 16-byte blocks. */
284 err = -EINVAL;
b2b39c2f 285 if (crypto_skcipher_alg_ivsize(alg) != CTR_RFC3686_BLOCK_SIZE)
a108dfcf 286 goto err_free_inst;
5311f248
HX
287
288 /* Not a stream cipher? */
b2b39c2f 289 if (alg->base.cra_blocksize != 1)
a108dfcf 290 goto err_free_inst;
5311f248 291
69d3150c 292 err = -ENAMETOOLONG;
b2b39c2f
HX
293 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
294 "rfc3686(%s)", alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
a108dfcf 295 goto err_free_inst;
b2b39c2f
HX
296 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
297 "rfc3686(%s)", alg->base.cra_driver_name) >=
298 CRYPTO_MAX_ALG_NAME)
a108dfcf 299 goto err_free_inst;
5311f248 300
b2b39c2f
HX
301 inst->alg.base.cra_priority = alg->base.cra_priority;
302 inst->alg.base.cra_blocksize = 1;
303 inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
5311f248 304
b2b39c2f
HX
305 inst->alg.ivsize = CTR_RFC3686_IV_SIZE;
306 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
307 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
308 CTR_RFC3686_NONCE_SIZE;
309 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
310 CTR_RFC3686_NONCE_SIZE;
5311f248 311
b2b39c2f
HX
312 inst->alg.setkey = crypto_rfc3686_setkey;
313 inst->alg.encrypt = crypto_rfc3686_crypt;
314 inst->alg.decrypt = crypto_rfc3686_crypt;
69d3150c 315
b2b39c2f 316 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx);
0a270321 317
b2b39c2f
HX
318 inst->alg.init = crypto_rfc3686_init_tfm;
319 inst->alg.exit = crypto_rfc3686_exit_tfm;
5311f248 320
b2b39c2f 321 inst->free = crypto_rfc3686_free;
5311f248 322
b2b39c2f 323 err = skcipher_register_instance(tmpl, inst);
a108dfcf 324 if (err) {
69d3150c 325err_free_inst:
a108dfcf
EB
326 crypto_rfc3686_free(inst);
327 }
328 return err;
5311f248
HX
329}
330
9f8ef365
XW
331static struct crypto_template crypto_ctr_tmpls[] = {
332 {
333 .name = "ctr",
334 .create = crypto_ctr_create,
335 .module = THIS_MODULE,
336 }, {
337 .name = "rfc3686",
338 .create = crypto_rfc3686_create,
339 .module = THIS_MODULE,
340 },
5311f248
HX
341};
342
23e353c8
JL
343static int __init crypto_ctr_module_init(void)
344{
9f8ef365
XW
345 return crypto_register_templates(crypto_ctr_tmpls,
346 ARRAY_SIZE(crypto_ctr_tmpls));
23e353c8
JL
347}
348
349static void __exit crypto_ctr_module_exit(void)
350{
9f8ef365
XW
351 crypto_unregister_templates(crypto_ctr_tmpls,
352 ARRAY_SIZE(crypto_ctr_tmpls));
23e353c8
JL
353}
354
c4741b23 355subsys_initcall(crypto_ctr_module_init);
23e353c8
JL
356module_exit(crypto_ctr_module_exit);
357
358MODULE_LICENSE("GPL");
11f14630 359MODULE_DESCRIPTION("CTR block cipher mode of operation");
5d26a105 360MODULE_ALIAS_CRYPTO("rfc3686");
4943ba16 361MODULE_ALIAS_CRYPTO("ctr");
0eb76ba2 362MODULE_IMPORT_NS(CRYPTO_INTERNAL);