1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linear symmetric key cipher operations.
5 * Generic encrypt/decrypt wrapper for ciphers.
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>
20 static inline struct crypto_lskcipher *__crypto_lskcipher_cast(
21 struct crypto_tfm *tfm)
23 return container_of(tfm, struct crypto_lskcipher, base);
26 static inline struct lskcipher_alg *__crypto_lskcipher_alg(
27 struct crypto_alg *alg)
29 return container_of(alg, struct lskcipher_alg, co.base);
32 static inline struct crypto_istat_cipher *lskcipher_get_stat(
33 struct lskcipher_alg *alg)
35 return skcipher_get_stat_common(&alg->co);
38 static inline int crypto_lskcipher_errstat(struct lskcipher_alg *alg, int err)
40 struct crypto_istat_cipher *istat = lskcipher_get_stat(alg);
42 if (!IS_ENABLED(CONFIG_CRYPTO_STATS))
46 atomic64_inc(&istat->err_cnt);
51 static int lskcipher_setkey_unaligned(struct crypto_lskcipher *tfm,
52 const u8 *key, unsigned int keylen)
54 unsigned long alignmask = crypto_lskcipher_alignmask(tfm);
55 struct lskcipher_alg *cipher = crypto_lskcipher_alg(tfm);
56 u8 *buffer, *alignbuffer;
60 absize = keylen + alignmask;
61 buffer = kmalloc(absize, GFP_ATOMIC);
65 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
66 memcpy(alignbuffer, key, keylen);
67 ret = cipher->setkey(tfm, alignbuffer, keylen);
68 kfree_sensitive(buffer);
72 int crypto_lskcipher_setkey(struct crypto_lskcipher *tfm, const u8 *key,
75 unsigned long alignmask = crypto_lskcipher_alignmask(tfm);
76 struct lskcipher_alg *cipher = crypto_lskcipher_alg(tfm);
78 if (keylen < cipher->co.min_keysize || keylen > cipher->co.max_keysize)
81 if ((unsigned long)key & alignmask)
82 return lskcipher_setkey_unaligned(tfm, key, keylen);
84 return cipher->setkey(tfm, key, keylen);
86 EXPORT_SYMBOL_GPL(crypto_lskcipher_setkey);
88 static int crypto_lskcipher_crypt_unaligned(
89 struct crypto_lskcipher *tfm, const u8 *src, u8 *dst, unsigned len,
90 u8 *iv, int (*crypt)(struct crypto_lskcipher *tfm, const u8 *src,
91 u8 *dst, unsigned len, u8 *iv, u32 flags))
93 unsigned statesize = crypto_lskcipher_statesize(tfm);
94 unsigned ivsize = crypto_lskcipher_ivsize(tfm);
95 unsigned bs = crypto_lskcipher_blocksize(tfm);
96 unsigned cs = crypto_lskcipher_chunksize(tfm);
101 BUILD_BUG_ON(MAX_CIPHER_BLOCKSIZE > PAGE_SIZE ||
102 MAX_CIPHER_ALIGNMASK >= PAGE_SIZE);
104 tiv = kmalloc(PAGE_SIZE, GFP_ATOMIC);
108 memcpy(tiv, iv, ivsize + statesize);
110 p = kmalloc(PAGE_SIZE, GFP_ATOMIC);
116 unsigned chunk = min((unsigned)PAGE_SIZE, len);
122 memcpy(p, src, chunk);
123 err = crypt(tfm, p, p, chunk, tiv, CRYPTO_LSKCIPHER_FLAG_FINAL);
127 memcpy(dst, p, chunk);
133 err = len ? -EINVAL : 0;
136 memcpy(iv, tiv, ivsize + statesize);
138 kfree_sensitive(tiv);
142 static int crypto_lskcipher_crypt(struct crypto_lskcipher *tfm, const u8 *src,
143 u8 *dst, unsigned len, u8 *iv,
144 int (*crypt)(struct crypto_lskcipher *tfm,
145 const u8 *src, u8 *dst,
146 unsigned len, u8 *iv,
149 unsigned long alignmask = crypto_lskcipher_alignmask(tfm);
150 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm);
153 if (((unsigned long)src | (unsigned long)dst | (unsigned long)iv) &
155 ret = crypto_lskcipher_crypt_unaligned(tfm, src, dst, len, iv,
160 ret = crypt(tfm, src, dst, len, iv, CRYPTO_LSKCIPHER_FLAG_FINAL);
163 return crypto_lskcipher_errstat(alg, ret);
166 int crypto_lskcipher_encrypt(struct crypto_lskcipher *tfm, const u8 *src,
167 u8 *dst, unsigned len, u8 *iv)
169 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm);
171 if (IS_ENABLED(CONFIG_CRYPTO_STATS)) {
172 struct crypto_istat_cipher *istat = lskcipher_get_stat(alg);
174 atomic64_inc(&istat->encrypt_cnt);
175 atomic64_add(len, &istat->encrypt_tlen);
178 return crypto_lskcipher_crypt(tfm, src, dst, len, iv, alg->encrypt);
180 EXPORT_SYMBOL_GPL(crypto_lskcipher_encrypt);
182 int crypto_lskcipher_decrypt(struct crypto_lskcipher *tfm, const u8 *src,
183 u8 *dst, unsigned len, u8 *iv)
185 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm);
187 if (IS_ENABLED(CONFIG_CRYPTO_STATS)) {
188 struct crypto_istat_cipher *istat = lskcipher_get_stat(alg);
190 atomic64_inc(&istat->decrypt_cnt);
191 atomic64_add(len, &istat->decrypt_tlen);
194 return crypto_lskcipher_crypt(tfm, src, dst, len, iv, alg->decrypt);
196 EXPORT_SYMBOL_GPL(crypto_lskcipher_decrypt);
198 static int crypto_lskcipher_crypt_sg(struct skcipher_request *req,
199 int (*crypt)(struct crypto_lskcipher *tfm,
200 const u8 *src, u8 *dst,
201 unsigned len, u8 *ivs,
204 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
205 struct crypto_lskcipher **ctx = crypto_skcipher_ctx(skcipher);
206 u8 *ivs = skcipher_request_ctx(req);
207 struct crypto_lskcipher *tfm = *ctx;
208 struct skcipher_walk walk;
213 ivsize = crypto_lskcipher_ivsize(tfm);
214 ivs = PTR_ALIGN(ivs, crypto_skcipher_alignmask(skcipher) + 1);
215 memcpy(ivs, req->iv, ivsize);
217 flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
219 if (req->base.flags & CRYPTO_SKCIPHER_REQ_CONT)
220 flags |= CRYPTO_LSKCIPHER_FLAG_CONT;
222 if (!(req->base.flags & CRYPTO_SKCIPHER_REQ_NOTFINAL))
223 flags |= CRYPTO_LSKCIPHER_FLAG_FINAL;
225 err = skcipher_walk_virt(&walk, req, false);
227 while (walk.nbytes) {
228 err = crypt(tfm, walk.src.virt.addr, walk.dst.virt.addr,
230 flags & ~(walk.nbytes == walk.total ?
231 0 : CRYPTO_LSKCIPHER_FLAG_FINAL));
232 err = skcipher_walk_done(&walk, err);
233 flags |= CRYPTO_LSKCIPHER_FLAG_CONT;
236 memcpy(req->iv, ivs, ivsize);
241 int crypto_lskcipher_encrypt_sg(struct skcipher_request *req)
243 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
244 struct crypto_lskcipher **ctx = crypto_skcipher_ctx(skcipher);
245 struct lskcipher_alg *alg = crypto_lskcipher_alg(*ctx);
247 return crypto_lskcipher_crypt_sg(req, alg->encrypt);
250 int crypto_lskcipher_decrypt_sg(struct skcipher_request *req)
252 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
253 struct crypto_lskcipher **ctx = crypto_skcipher_ctx(skcipher);
254 struct lskcipher_alg *alg = crypto_lskcipher_alg(*ctx);
256 return crypto_lskcipher_crypt_sg(req, alg->decrypt);
259 static void crypto_lskcipher_exit_tfm(struct crypto_tfm *tfm)
261 struct crypto_lskcipher *skcipher = __crypto_lskcipher_cast(tfm);
262 struct lskcipher_alg *alg = crypto_lskcipher_alg(skcipher);
267 static int crypto_lskcipher_init_tfm(struct crypto_tfm *tfm)
269 struct crypto_lskcipher *skcipher = __crypto_lskcipher_cast(tfm);
270 struct lskcipher_alg *alg = crypto_lskcipher_alg(skcipher);
273 skcipher->base.exit = crypto_lskcipher_exit_tfm;
276 return alg->init(skcipher);
281 static void crypto_lskcipher_free_instance(struct crypto_instance *inst)
283 struct lskcipher_instance *skcipher =
284 container_of(inst, struct lskcipher_instance, s.base);
286 skcipher->free(skcipher);
289 static void __maybe_unused crypto_lskcipher_show(
290 struct seq_file *m, struct crypto_alg *alg)
292 struct lskcipher_alg *skcipher = __crypto_lskcipher_alg(alg);
294 seq_printf(m, "type : lskcipher\n");
295 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
296 seq_printf(m, "min keysize : %u\n", skcipher->co.min_keysize);
297 seq_printf(m, "max keysize : %u\n", skcipher->co.max_keysize);
298 seq_printf(m, "ivsize : %u\n", skcipher->co.ivsize);
299 seq_printf(m, "chunksize : %u\n", skcipher->co.chunksize);
300 seq_printf(m, "statesize : %u\n", skcipher->co.statesize);
303 static int __maybe_unused crypto_lskcipher_report(
304 struct sk_buff *skb, struct crypto_alg *alg)
306 struct lskcipher_alg *skcipher = __crypto_lskcipher_alg(alg);
307 struct crypto_report_blkcipher rblkcipher;
309 memset(&rblkcipher, 0, sizeof(rblkcipher));
311 strscpy(rblkcipher.type, "lskcipher", sizeof(rblkcipher.type));
312 strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
314 rblkcipher.blocksize = alg->cra_blocksize;
315 rblkcipher.min_keysize = skcipher->co.min_keysize;
316 rblkcipher.max_keysize = skcipher->co.max_keysize;
317 rblkcipher.ivsize = skcipher->co.ivsize;
319 return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
320 sizeof(rblkcipher), &rblkcipher);
323 static int __maybe_unused crypto_lskcipher_report_stat(
324 struct sk_buff *skb, struct crypto_alg *alg)
326 struct lskcipher_alg *skcipher = __crypto_lskcipher_alg(alg);
327 struct crypto_istat_cipher *istat;
328 struct crypto_stat_cipher rcipher;
330 istat = lskcipher_get_stat(skcipher);
332 memset(&rcipher, 0, sizeof(rcipher));
334 strscpy(rcipher.type, "cipher", sizeof(rcipher.type));
336 rcipher.stat_encrypt_cnt = atomic64_read(&istat->encrypt_cnt);
337 rcipher.stat_encrypt_tlen = atomic64_read(&istat->encrypt_tlen);
338 rcipher.stat_decrypt_cnt = atomic64_read(&istat->decrypt_cnt);
339 rcipher.stat_decrypt_tlen = atomic64_read(&istat->decrypt_tlen);
340 rcipher.stat_err_cnt = atomic64_read(&istat->err_cnt);
342 return nla_put(skb, CRYPTOCFGA_STAT_CIPHER, sizeof(rcipher), &rcipher);
345 static const struct crypto_type crypto_lskcipher_type = {
346 .extsize = crypto_alg_extsize,
347 .init_tfm = crypto_lskcipher_init_tfm,
348 .free = crypto_lskcipher_free_instance,
349 #ifdef CONFIG_PROC_FS
350 .show = crypto_lskcipher_show,
352 #if IS_ENABLED(CONFIG_CRYPTO_USER)
353 .report = crypto_lskcipher_report,
355 #ifdef CONFIG_CRYPTO_STATS
356 .report_stat = crypto_lskcipher_report_stat,
358 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
359 .maskset = CRYPTO_ALG_TYPE_MASK,
360 .type = CRYPTO_ALG_TYPE_LSKCIPHER,
361 .tfmsize = offsetof(struct crypto_lskcipher, base),
364 static void crypto_lskcipher_exit_tfm_sg(struct crypto_tfm *tfm)
366 struct crypto_lskcipher **ctx = crypto_tfm_ctx(tfm);
368 crypto_free_lskcipher(*ctx);
371 int crypto_init_lskcipher_ops_sg(struct crypto_tfm *tfm)
373 struct crypto_lskcipher **ctx = crypto_tfm_ctx(tfm);
374 struct crypto_alg *calg = tfm->__crt_alg;
375 struct crypto_lskcipher *skcipher;
377 if (!crypto_mod_get(calg))
380 skcipher = crypto_create_tfm(calg, &crypto_lskcipher_type);
381 if (IS_ERR(skcipher)) {
382 crypto_mod_put(calg);
383 return PTR_ERR(skcipher);
387 tfm->exit = crypto_lskcipher_exit_tfm_sg;
392 int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn,
393 struct crypto_instance *inst,
394 const char *name, u32 type, u32 mask)
396 spawn->base.frontend = &crypto_lskcipher_type;
397 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
399 EXPORT_SYMBOL_GPL(crypto_grab_lskcipher);
401 struct crypto_lskcipher *crypto_alloc_lskcipher(const char *alg_name,
404 return crypto_alloc_tfm(alg_name, &crypto_lskcipher_type, type, mask);
406 EXPORT_SYMBOL_GPL(crypto_alloc_lskcipher);
408 static int lskcipher_prepare_alg(struct lskcipher_alg *alg)
410 struct crypto_alg *base = &alg->co.base;
413 err = skcipher_prepare_alg_common(&alg->co);
417 if (alg->co.chunksize & (alg->co.chunksize - 1))
420 base->cra_type = &crypto_lskcipher_type;
421 base->cra_flags |= CRYPTO_ALG_TYPE_LSKCIPHER;
426 int crypto_register_lskcipher(struct lskcipher_alg *alg)
428 struct crypto_alg *base = &alg->co.base;
431 err = lskcipher_prepare_alg(alg);
435 return crypto_register_alg(base);
437 EXPORT_SYMBOL_GPL(crypto_register_lskcipher);
439 void crypto_unregister_lskcipher(struct lskcipher_alg *alg)
441 crypto_unregister_alg(&alg->co.base);
443 EXPORT_SYMBOL_GPL(crypto_unregister_lskcipher);
445 int crypto_register_lskciphers(struct lskcipher_alg *algs, int count)
449 for (i = 0; i < count; i++) {
450 ret = crypto_register_lskcipher(&algs[i]);
458 for (--i; i >= 0; --i)
459 crypto_unregister_lskcipher(&algs[i]);
463 EXPORT_SYMBOL_GPL(crypto_register_lskciphers);
465 void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count)
469 for (i = count - 1; i >= 0; --i)
470 crypto_unregister_lskcipher(&algs[i]);
472 EXPORT_SYMBOL_GPL(crypto_unregister_lskciphers);
474 int lskcipher_register_instance(struct crypto_template *tmpl,
475 struct lskcipher_instance *inst)
479 if (WARN_ON(!inst->free))
482 err = lskcipher_prepare_alg(&inst->alg);
486 return crypto_register_instance(tmpl, lskcipher_crypto_instance(inst));
488 EXPORT_SYMBOL_GPL(lskcipher_register_instance);
490 static int lskcipher_setkey_simple(struct crypto_lskcipher *tfm, const u8 *key,
493 struct crypto_lskcipher *cipher = lskcipher_cipher_simple(tfm);
495 crypto_lskcipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
496 crypto_lskcipher_set_flags(cipher, crypto_lskcipher_get_flags(tfm) &
497 CRYPTO_TFM_REQ_MASK);
498 return crypto_lskcipher_setkey(cipher, key, keylen);
501 static int lskcipher_init_tfm_simple(struct crypto_lskcipher *tfm)
503 struct lskcipher_instance *inst = lskcipher_alg_instance(tfm);
504 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
505 struct crypto_lskcipher_spawn *spawn;
506 struct crypto_lskcipher *cipher;
508 spawn = lskcipher_instance_ctx(inst);
509 cipher = crypto_spawn_lskcipher(spawn);
511 return PTR_ERR(cipher);
517 static void lskcipher_exit_tfm_simple(struct crypto_lskcipher *tfm)
519 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
521 crypto_free_lskcipher(*ctx);
524 static void lskcipher_free_instance_simple(struct lskcipher_instance *inst)
526 crypto_drop_lskcipher(lskcipher_instance_ctx(inst));
531 * lskcipher_alloc_instance_simple - allocate instance of simple block cipher
533 * Allocate an lskcipher_instance for a simple block cipher mode of operation,
534 * e.g. cbc or ecb. The instance context will have just a single crypto_spawn,
535 * that for the underlying cipher. The {min,max}_keysize, ivsize, blocksize,
536 * alignmask, and priority are set from the underlying cipher but can be
537 * overridden if needed. The tfm context defaults to
538 * struct crypto_lskcipher *, and default ->setkey(), ->init(), and
539 * ->exit() methods are installed.
541 * @tmpl: the template being instantiated
542 * @tb: the template parameters
544 * Return: a pointer to the new instance, or an ERR_PTR(). The caller still
545 * needs to register the instance.
547 struct lskcipher_instance *lskcipher_alloc_instance_simple(
548 struct crypto_template *tmpl, struct rtattr **tb)
551 struct lskcipher_instance *inst;
552 struct crypto_lskcipher_spawn *spawn;
553 char ecb_name[CRYPTO_MAX_ALG_NAME];
554 struct lskcipher_alg *cipher_alg;
555 const char *cipher_name;
558 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_LSKCIPHER, &mask);
562 cipher_name = crypto_attr_alg_name(tb[1]);
563 if (IS_ERR(cipher_name))
564 return ERR_CAST(cipher_name);
566 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
568 return ERR_PTR(-ENOMEM);
570 spawn = lskcipher_instance_ctx(inst);
571 err = crypto_grab_lskcipher(spawn,
572 lskcipher_crypto_instance(inst),
573 cipher_name, 0, mask);
576 if (err == -ENOENT && !!memcmp(tmpl->name, "ecb", 4)) {
578 if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
579 cipher_name) >= CRYPTO_MAX_ALG_NAME)
582 err = crypto_grab_lskcipher(spawn,
583 lskcipher_crypto_instance(inst),
590 cipher_alg = crypto_lskcipher_spawn_alg(spawn);
592 err = crypto_inst_setname(lskcipher_crypto_instance(inst), tmpl->name,
593 &cipher_alg->co.base);
601 len = strscpy(ecb_name, &cipher_alg->co.base.cra_name[4],
606 if (ecb_name[len - 1] != ')')
609 ecb_name[len - 1] = 0;
612 if (snprintf(inst->alg.co.base.cra_name, CRYPTO_MAX_ALG_NAME,
613 "%s(%s)", tmpl->name, ecb_name) >=
617 if (strcmp(ecb_name, cipher_name) &&
618 snprintf(inst->alg.co.base.cra_driver_name,
620 "%s(%s)", tmpl->name, cipher_name) >=
624 /* Don't allow nesting. */
626 if ((cipher_alg->co.base.cra_flags & CRYPTO_ALG_INSTANCE))
631 if (cipher_alg->co.ivsize)
634 inst->free = lskcipher_free_instance_simple;
636 /* Default algorithm properties, can be overridden */
637 inst->alg.co.base.cra_blocksize = cipher_alg->co.base.cra_blocksize;
638 inst->alg.co.base.cra_alignmask = cipher_alg->co.base.cra_alignmask;
639 inst->alg.co.base.cra_priority = cipher_alg->co.base.cra_priority;
640 inst->alg.co.min_keysize = cipher_alg->co.min_keysize;
641 inst->alg.co.max_keysize = cipher_alg->co.max_keysize;
642 inst->alg.co.ivsize = cipher_alg->co.base.cra_blocksize;
643 inst->alg.co.statesize = cipher_alg->co.statesize;
645 /* Use struct crypto_lskcipher * by default, can be overridden */
646 inst->alg.co.base.cra_ctxsize = sizeof(struct crypto_lskcipher *);
647 inst->alg.setkey = lskcipher_setkey_simple;
648 inst->alg.init = lskcipher_init_tfm_simple;
649 inst->alg.exit = lskcipher_exit_tfm_simple;
654 lskcipher_free_instance_simple(inst);
657 EXPORT_SYMBOL_GPL(lskcipher_alloc_instance_simple);