1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Linear symmetric key cipher operations.
4 *
5 * Generic encrypt/decrypt wrapper for ciphers.
6 *
7 * Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au>
8 */
9
10#include <linux/cryptouser.h>
11#include <linux/err.h>
12#include <linux/export.h>
13#include <linux/kernel.h>
14#include <linux/seq_file.h>
15#include <linux/slab.h>
16#include <linux/string.h>
17#include <net/netlink.h>
18#include "skcipher.h"
19
20static inline struct crypto_lskcipher *__crypto_lskcipher_cast(
21 struct crypto_tfm *tfm)
22{
23 return container_of(tfm, struct crypto_lskcipher, base);
24}
25
26static inline struct lskcipher_alg *__crypto_lskcipher_alg(
27 struct crypto_alg *alg)
28{
29 return container_of(alg, struct lskcipher_alg, co.base);
30}
31
32static int lskcipher_setkey_unaligned(struct crypto_lskcipher *tfm,
33 const u8 *key, unsigned int keylen)
34{
35 unsigned long alignmask = crypto_lskcipher_alignmask(tfm);
36 struct lskcipher_alg *cipher = crypto_lskcipher_alg(tfm);
37 u8 *buffer, *alignbuffer;
38 unsigned long absize;
39 int ret;
40
41 absize = keylen + alignmask;
42 buffer = kmalloc(absize, GFP_ATOMIC);
43 if (!buffer)
44 return -ENOMEM;
45
46 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
47 memcpy(to: alignbuffer, from: key, len: keylen);
48 ret = cipher->setkey(tfm, alignbuffer, keylen);
49 kfree_sensitive(objp: buffer);
50 return ret;
51}
52
53int crypto_lskcipher_setkey(struct crypto_lskcipher *tfm, const u8 *key,
54 unsigned int keylen)
55{
56 unsigned long alignmask = crypto_lskcipher_alignmask(tfm);
57 struct lskcipher_alg *cipher = crypto_lskcipher_alg(tfm);
58
59 if (keylen < cipher->co.min_keysize || keylen > cipher->co.max_keysize)
60 return -EINVAL;
61
62 if ((unsigned long)key & alignmask)
63 return lskcipher_setkey_unaligned(tfm, key, keylen);
64 else
65 return cipher->setkey(tfm, key, keylen);
66}
67EXPORT_SYMBOL_GPL(crypto_lskcipher_setkey);
68
69static int crypto_lskcipher_crypt_unaligned(
70 struct crypto_lskcipher *tfm, const u8 *src, u8 *dst, unsigned len,
71 u8 *iv, int (*crypt)(struct crypto_lskcipher *tfm, const u8 *src,
72 u8 *dst, unsigned len, u8 *iv, u32 flags))
73{
74 unsigned statesize = crypto_lskcipher_statesize(tfm);
75 unsigned ivsize = crypto_lskcipher_ivsize(tfm);
76 unsigned bs = crypto_lskcipher_blocksize(tfm);
77 unsigned cs = crypto_lskcipher_chunksize(tfm);
78 int err;
79 u8 *tiv;
80 u8 *p;
81
82 BUILD_BUG_ON(MAX_CIPHER_BLOCKSIZE > PAGE_SIZE ||
83 MAX_CIPHER_ALIGNMASK >= PAGE_SIZE);
84
85 tiv = kmalloc(PAGE_SIZE, GFP_ATOMIC);
86 if (!tiv)
87 return -ENOMEM;
88
89 memcpy(to: tiv, from: iv, len: ivsize + statesize);
90
91 p = kmalloc(PAGE_SIZE, GFP_ATOMIC);
92 err = -ENOMEM;
93 if (!p)
94 goto out;
95
96 while (len >= bs) {
97 unsigned chunk = min((unsigned)PAGE_SIZE, len);
98 int err;
99
100 if (chunk > cs)
101 chunk &= ~(cs - 1);
102
103 memcpy(to: p, from: src, len: chunk);
104 err = crypt(tfm, p, p, chunk, tiv, CRYPTO_LSKCIPHER_FLAG_FINAL);
105 if (err)
106 goto out;
107
108 memcpy(to: dst, from: p, len: chunk);
109 src += chunk;
110 dst += chunk;
111 len -= chunk;
112 }
113
114 err = len ? -EINVAL : 0;
115
116out:
117 memcpy(to: iv, from: tiv, len: ivsize + statesize);
118 kfree_sensitive(objp: p);
119 kfree_sensitive(objp: tiv);
120 return err;
121}
122
123static int crypto_lskcipher_crypt(struct crypto_lskcipher *tfm, const u8 *src,
124 u8 *dst, unsigned len, u8 *iv,
125 int (*crypt)(struct crypto_lskcipher *tfm,
126 const u8 *src, u8 *dst,
127 unsigned len, u8 *iv,
128 u32 flags))
129{
130 unsigned long alignmask = crypto_lskcipher_alignmask(tfm);
131
132 if (((unsigned long)src | (unsigned long)dst | (unsigned long)iv) &
133 alignmask)
134 return crypto_lskcipher_crypt_unaligned(tfm, src, dst, len, iv,
135 crypt);
136
137 return crypt(tfm, src, dst, len, iv, CRYPTO_LSKCIPHER_FLAG_FINAL);
138}
139
140int crypto_lskcipher_encrypt(struct crypto_lskcipher *tfm, const u8 *src,
141 u8 *dst, unsigned len, u8 *iv)
142{
143 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm);
144
145 return crypto_lskcipher_crypt(tfm, src, dst, len, iv, crypt: alg->encrypt);
146}
147EXPORT_SYMBOL_GPL(crypto_lskcipher_encrypt);
148
149int crypto_lskcipher_decrypt(struct crypto_lskcipher *tfm, const u8 *src,
150 u8 *dst, unsigned len, u8 *iv)
151{
152 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm);
153
154 return crypto_lskcipher_crypt(tfm, src, dst, len, iv, crypt: alg->decrypt);
155}
156EXPORT_SYMBOL_GPL(crypto_lskcipher_decrypt);
157
158static int crypto_lskcipher_crypt_sg(struct skcipher_request *req,
159 int (*crypt)(struct crypto_lskcipher *tfm,
160 const u8 *src, u8 *dst,
161 unsigned len, u8 *ivs,
162 u32 flags))
163{
164 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
165 struct crypto_lskcipher **ctx = crypto_skcipher_ctx(tfm: skcipher);
166 u8 *ivs = skcipher_request_ctx(req);
167 struct crypto_lskcipher *tfm = *ctx;
168 struct skcipher_walk walk;
169 unsigned ivsize;
170 u32 flags;
171 int err;
172
173 ivsize = crypto_lskcipher_ivsize(tfm);
174 ivs = PTR_ALIGN(ivs, crypto_skcipher_alignmask(skcipher) + 1);
175 memcpy(to: ivs, from: req->iv, len: ivsize);
176
177 flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
178
179 if (req->base.flags & CRYPTO_SKCIPHER_REQ_CONT)
180 flags |= CRYPTO_LSKCIPHER_FLAG_CONT;
181
182 if (!(req->base.flags & CRYPTO_SKCIPHER_REQ_NOTFINAL))
183 flags |= CRYPTO_LSKCIPHER_FLAG_FINAL;
184
185 err = skcipher_walk_virt(walk: &walk, req, atomic: false);
186
187 while (walk.nbytes) {
188 err = crypt(tfm, walk.src.virt.addr, walk.dst.virt.addr,
189 walk.nbytes, ivs,
190 flags & ~(walk.nbytes == walk.total ?
191 0 : CRYPTO_LSKCIPHER_FLAG_FINAL));
192 err = skcipher_walk_done(walk: &walk, res: err);
193 flags |= CRYPTO_LSKCIPHER_FLAG_CONT;
194 }
195
196 memcpy(to: req->iv, from: ivs, len: ivsize);
197
198 return err;
199}
200
201int crypto_lskcipher_encrypt_sg(struct skcipher_request *req)
202{
203 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
204 struct crypto_lskcipher **ctx = crypto_skcipher_ctx(tfm: skcipher);
205 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm: *ctx);
206
207 return crypto_lskcipher_crypt_sg(req, crypt: alg->encrypt);
208}
209
210int crypto_lskcipher_decrypt_sg(struct skcipher_request *req)
211{
212 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
213 struct crypto_lskcipher **ctx = crypto_skcipher_ctx(tfm: skcipher);
214 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm: *ctx);
215
216 return crypto_lskcipher_crypt_sg(req, crypt: alg->decrypt);
217}
218
219static void crypto_lskcipher_exit_tfm(struct crypto_tfm *tfm)
220{
221 struct crypto_lskcipher *skcipher = __crypto_lskcipher_cast(tfm);
222 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm: skcipher);
223
224 alg->exit(skcipher);
225}
226
227static int crypto_lskcipher_init_tfm(struct crypto_tfm *tfm)
228{
229 struct crypto_lskcipher *skcipher = __crypto_lskcipher_cast(tfm);
230 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm: skcipher);
231
232 if (alg->exit)
233 skcipher->base.exit = crypto_lskcipher_exit_tfm;
234
235 if (alg->init)
236 return alg->init(skcipher);
237
238 return 0;
239}
240
241static void crypto_lskcipher_free_instance(struct crypto_instance *inst)
242{
243 struct lskcipher_instance *skcipher =
244 container_of(inst, struct lskcipher_instance, s.base);
245
246 skcipher->free(skcipher);
247}
248
249static void __maybe_unused crypto_lskcipher_show(
250 struct seq_file *m, struct crypto_alg *alg)
251{
252 struct lskcipher_alg *skcipher = __crypto_lskcipher_alg(alg);
253
254 seq_printf(m, fmt: "type : lskcipher\n");
255 seq_printf(m, fmt: "blocksize : %u\n", alg->cra_blocksize);
256 seq_printf(m, fmt: "min keysize : %u\n", skcipher->co.min_keysize);
257 seq_printf(m, fmt: "max keysize : %u\n", skcipher->co.max_keysize);
258 seq_printf(m, fmt: "ivsize : %u\n", skcipher->co.ivsize);
259 seq_printf(m, fmt: "chunksize : %u\n", skcipher->co.chunksize);
260 seq_printf(m, fmt: "statesize : %u\n", skcipher->co.statesize);
261}
262
263static int __maybe_unused crypto_lskcipher_report(
264 struct sk_buff *skb, struct crypto_alg *alg)
265{
266 struct lskcipher_alg *skcipher = __crypto_lskcipher_alg(alg);
267 struct crypto_report_blkcipher rblkcipher;
268
269 memset(s: &rblkcipher, c: 0, n: sizeof(rblkcipher));
270
271 strscpy(rblkcipher.type, "lskcipher", sizeof(rblkcipher.type));
272 strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
273
274 rblkcipher.blocksize = alg->cra_blocksize;
275 rblkcipher.min_keysize = skcipher->co.min_keysize;
276 rblkcipher.max_keysize = skcipher->co.max_keysize;
277 rblkcipher.ivsize = skcipher->co.ivsize;
278
279 return nla_put(skb, attrtype: CRYPTOCFGA_REPORT_BLKCIPHER,
280 attrlen: sizeof(rblkcipher), data: &rblkcipher);
281}
282
283static const struct crypto_type crypto_lskcipher_type = {
284 .extsize = crypto_alg_extsize,
285 .init_tfm = crypto_lskcipher_init_tfm,
286 .free = crypto_lskcipher_free_instance,
287#ifdef CONFIG_PROC_FS
288 .show = crypto_lskcipher_show,
289#endif
290#if IS_ENABLED(CONFIG_CRYPTO_USER)
291 .report = crypto_lskcipher_report,
292#endif
293 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
294 .maskset = CRYPTO_ALG_TYPE_MASK,
295 .type = CRYPTO_ALG_TYPE_LSKCIPHER,
296 .tfmsize = offsetof(struct crypto_lskcipher, base),
297 .algsize = offsetof(struct lskcipher_alg, co.base),
298};
299
300static void crypto_lskcipher_exit_tfm_sg(struct crypto_tfm *tfm)
301{
302 struct crypto_lskcipher **ctx = crypto_tfm_ctx(tfm);
303
304 crypto_free_lskcipher(tfm: *ctx);
305}
306
307int crypto_init_lskcipher_ops_sg(struct crypto_tfm *tfm)
308{
309 struct crypto_lskcipher **ctx = crypto_tfm_ctx(tfm);
310 struct crypto_alg *calg = tfm->__crt_alg;
311 struct crypto_lskcipher *skcipher;
312
313 if (!crypto_mod_get(alg: calg))
314 return -EAGAIN;
315
316 skcipher = crypto_create_tfm(alg: calg, frontend: &crypto_lskcipher_type);
317 if (IS_ERR(ptr: skcipher)) {
318 crypto_mod_put(alg: calg);
319 return PTR_ERR(ptr: skcipher);
320 }
321
322 *ctx = skcipher;
323 tfm->exit = crypto_lskcipher_exit_tfm_sg;
324
325 return 0;
326}
327
328int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn,
329 struct crypto_instance *inst,
330 const char *name, u32 type, u32 mask)
331{
332 spawn->base.frontend = &crypto_lskcipher_type;
333 return crypto_grab_spawn(spawn: &spawn->base, inst, name, type, mask);
334}
335EXPORT_SYMBOL_GPL(crypto_grab_lskcipher);
336
337struct crypto_lskcipher *crypto_alloc_lskcipher(const char *alg_name,
338 u32 type, u32 mask)
339{
340 return crypto_alloc_tfm(alg_name, frontend: &crypto_lskcipher_type, type, mask);
341}
342EXPORT_SYMBOL_GPL(crypto_alloc_lskcipher);
343
344static int lskcipher_prepare_alg(struct lskcipher_alg *alg)
345{
346 struct crypto_alg *base = &alg->co.base;
347 int err;
348
349 err = skcipher_prepare_alg_common(alg: &alg->co);
350 if (err)
351 return err;
352
353 if (alg->co.chunksize & (alg->co.chunksize - 1))
354 return -EINVAL;
355
356 base->cra_type = &crypto_lskcipher_type;
357 base->cra_flags |= CRYPTO_ALG_TYPE_LSKCIPHER;
358
359 return 0;
360}
361
362int crypto_register_lskcipher(struct lskcipher_alg *alg)
363{
364 struct crypto_alg *base = &alg->co.base;
365 int err;
366
367 err = lskcipher_prepare_alg(alg);
368 if (err)
369 return err;
370
371 return crypto_register_alg(alg: base);
372}
373EXPORT_SYMBOL_GPL(crypto_register_lskcipher);
374
375void crypto_unregister_lskcipher(struct lskcipher_alg *alg)
376{
377 crypto_unregister_alg(alg: &alg->co.base);
378}
379EXPORT_SYMBOL_GPL(crypto_unregister_lskcipher);
380
381int crypto_register_lskciphers(struct lskcipher_alg *algs, int count)
382{
383 int i, ret;
384
385 for (i = 0; i < count; i++) {
386 ret = crypto_register_lskcipher(&algs[i]);
387 if (ret)
388 goto err;
389 }
390
391 return 0;
392
393err:
394 for (--i; i >= 0; --i)
395 crypto_unregister_lskcipher(&algs[i]);
396
397 return ret;
398}
399EXPORT_SYMBOL_GPL(crypto_register_lskciphers);
400
401void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count)
402{
403 int i;
404
405 for (i = count - 1; i >= 0; --i)
406 crypto_unregister_lskcipher(&algs[i]);
407}
408EXPORT_SYMBOL_GPL(crypto_unregister_lskciphers);
409
410int lskcipher_register_instance(struct crypto_template *tmpl,
411 struct lskcipher_instance *inst)
412{
413 int err;
414
415 if (WARN_ON(!inst->free))
416 return -EINVAL;
417
418 err = lskcipher_prepare_alg(alg: &inst->alg);
419 if (err)
420 return err;
421
422 return crypto_register_instance(tmpl, inst: lskcipher_crypto_instance(inst));
423}
424EXPORT_SYMBOL_GPL(lskcipher_register_instance);
425
426static int lskcipher_setkey_simple(struct crypto_lskcipher *tfm, const u8 *key,
427 unsigned int keylen)
428{
429 struct crypto_lskcipher *cipher = lskcipher_cipher_simple(tfm);
430
431 crypto_lskcipher_clear_flags(tfm: cipher, CRYPTO_TFM_REQ_MASK);
432 crypto_lskcipher_set_flags(tfm: cipher, flags: crypto_lskcipher_get_flags(tfm) &
433 CRYPTO_TFM_REQ_MASK);
434 return crypto_lskcipher_setkey(cipher, key, keylen);
435}
436
437static int lskcipher_init_tfm_simple(struct crypto_lskcipher *tfm)
438{
439 struct lskcipher_instance *inst = lskcipher_alg_instance(lskcipher: tfm);
440 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
441 struct crypto_lskcipher_spawn *spawn;
442 struct crypto_lskcipher *cipher;
443
444 spawn = lskcipher_instance_ctx(inst);
445 cipher = crypto_spawn_lskcipher(spawn);
446 if (IS_ERR(ptr: cipher))
447 return PTR_ERR(ptr: cipher);
448
449 *ctx = cipher;
450 return 0;
451}
452
453static void lskcipher_exit_tfm_simple(struct crypto_lskcipher *tfm)
454{
455 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
456
457 crypto_free_lskcipher(tfm: *ctx);
458}
459
460static void lskcipher_free_instance_simple(struct lskcipher_instance *inst)
461{
462 crypto_drop_lskcipher(spawn: lskcipher_instance_ctx(inst));
463 kfree(objp: inst);
464}
465
466/**
467 * lskcipher_alloc_instance_simple - allocate instance of simple block cipher
468 *
469 * Allocate an lskcipher_instance for a simple block cipher mode of operation,
470 * e.g. cbc or ecb. The instance context will have just a single crypto_spawn,
471 * that for the underlying cipher. The {min,max}_keysize, ivsize, blocksize,
472 * alignmask, and priority are set from the underlying cipher but can be
473 * overridden if needed. The tfm context defaults to
474 * struct crypto_lskcipher *, and default ->setkey(), ->init(), and
475 * ->exit() methods are installed.
476 *
477 * @tmpl: the template being instantiated
478 * @tb: the template parameters
479 *
480 * Return: a pointer to the new instance, or an ERR_PTR(). The caller still
481 * needs to register the instance.
482 */
483struct lskcipher_instance *lskcipher_alloc_instance_simple(
484 struct crypto_template *tmpl, struct rtattr **tb)
485{
486 u32 mask;
487 struct lskcipher_instance *inst;
488 struct crypto_lskcipher_spawn *spawn;
489 char ecb_name[CRYPTO_MAX_ALG_NAME];
490 struct lskcipher_alg *cipher_alg;
491 const char *cipher_name;
492 int err;
493
494 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_LSKCIPHER, mask_ret: &mask);
495 if (err)
496 return ERR_PTR(error: err);
497
498 cipher_name = crypto_attr_alg_name(rta: tb[1]);
499 if (IS_ERR(ptr: cipher_name))
500 return ERR_CAST(ptr: cipher_name);
501
502 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
503 if (!inst)
504 return ERR_PTR(error: -ENOMEM);
505
506 spawn = lskcipher_instance_ctx(inst);
507 err = crypto_grab_lskcipher(spawn,
508 lskcipher_crypto_instance(inst),
509 cipher_name, 0, mask);
510
511 ecb_name[0] = 0;
512 if (err == -ENOENT && !!memcmp(tmpl->name, "ecb", 4)) {
513 err = -ENAMETOOLONG;
514 if (snprintf(buf: ecb_name, CRYPTO_MAX_ALG_NAME, fmt: "ecb(%s)",
515 cipher_name) >= CRYPTO_MAX_ALG_NAME)
516 goto err_free_inst;
517
518 err = crypto_grab_lskcipher(spawn,
519 lskcipher_crypto_instance(inst),
520 ecb_name, 0, mask);
521 }
522
523 if (err)
524 goto err_free_inst;
525
526 cipher_alg = crypto_lskcipher_spawn_alg(spawn);
527
528 err = crypto_inst_setname(lskcipher_crypto_instance(inst), tmpl->name,
529 &cipher_alg->co.base);
530 if (err)
531 goto err_free_inst;
532
533 if (ecb_name[0]) {
534 int len;
535
536 err = -EINVAL;
537 len = strscpy(ecb_name, &cipher_alg->co.base.cra_name[4],
538 sizeof(ecb_name));
539 if (len < 2)
540 goto err_free_inst;
541
542 if (ecb_name[len - 1] != ')')
543 goto err_free_inst;
544
545 ecb_name[len - 1] = 0;
546
547 err = -ENAMETOOLONG;
548 if (snprintf(buf: inst->alg.co.base.cra_name, CRYPTO_MAX_ALG_NAME,
549 fmt: "%s(%s)", tmpl->name, ecb_name) >=
550 CRYPTO_MAX_ALG_NAME)
551 goto err_free_inst;
552
553 if (strcmp(ecb_name, cipher_name) &&
554 snprintf(buf: inst->alg.co.base.cra_driver_name,
555 CRYPTO_MAX_ALG_NAME,
556 fmt: "%s(%s)", tmpl->name, cipher_name) >=
557 CRYPTO_MAX_ALG_NAME)
558 goto err_free_inst;
559 } else {
560 /* Don't allow nesting. */
561 err = -ELOOP;
562 if ((cipher_alg->co.base.cra_flags & CRYPTO_ALG_INSTANCE))
563 goto err_free_inst;
564 }
565
566 err = -EINVAL;
567 if (cipher_alg->co.ivsize)
568 goto err_free_inst;
569
570 inst->free = lskcipher_free_instance_simple;
571
572 /* Default algorithm properties, can be overridden */
573 inst->alg.co.base.cra_blocksize = cipher_alg->co.base.cra_blocksize;
574 inst->alg.co.base.cra_alignmask = cipher_alg->co.base.cra_alignmask;
575 inst->alg.co.base.cra_priority = cipher_alg->co.base.cra_priority;
576 inst->alg.co.min_keysize = cipher_alg->co.min_keysize;
577 inst->alg.co.max_keysize = cipher_alg->co.max_keysize;
578 inst->alg.co.ivsize = cipher_alg->co.base.cra_blocksize;
579 inst->alg.co.statesize = cipher_alg->co.statesize;
580
581 /* Use struct crypto_lskcipher * by default, can be overridden */
582 inst->alg.co.base.cra_ctxsize = sizeof(struct crypto_lskcipher *);
583 inst->alg.setkey = lskcipher_setkey_simple;
584 inst->alg.init = lskcipher_init_tfm_simple;
585 inst->alg.exit = lskcipher_exit_tfm_simple;
586
587 return inst;
588
589err_free_inst:
590 lskcipher_free_instance_simple(inst);
591 return ERR_PTR(error: err);
592}
593EXPORT_SYMBOL_GPL(lskcipher_alloc_instance_simple);
594