aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/blkcipher.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/blkcipher.c')
-rw-r--r--crypto/blkcipher.c185
1 files changed, 184 insertions, 1 deletions
diff --git a/crypto/blkcipher.c b/crypto/blkcipher.c
index 7a3e1a308074..ca6ef065cded 100644
--- a/crypto/blkcipher.c
+++ b/crypto/blkcipher.c
@@ -14,8 +14,8 @@
14 * 14 *
15 */ 15 */
16 16
17#include <crypto/internal/skcipher.h>
17#include <crypto/scatterwalk.h> 18#include <crypto/scatterwalk.h>
18#include <linux/crypto.h>
19#include <linux/errno.h> 19#include <linux/errno.h>
20#include <linux/hardirq.h> 20#include <linux/hardirq.h>
21#include <linux/kernel.h> 21#include <linux/kernel.h>
@@ -450,6 +450,7 @@ static int crypto_init_blkcipher_ops_async(struct crypto_tfm *tfm)
450 crt->setkey = async_setkey; 450 crt->setkey = async_setkey;
451 crt->encrypt = async_encrypt; 451 crt->encrypt = async_encrypt;
452 crt->decrypt = async_decrypt; 452 crt->decrypt = async_decrypt;
453 crt->base = __crypto_ablkcipher_cast(tfm);
453 crt->ivsize = alg->ivsize; 454 crt->ivsize = alg->ivsize;
454 455
455 return 0; 456 return 0;
@@ -509,5 +510,187 @@ const struct crypto_type crypto_blkcipher_type = {
509}; 510};
510EXPORT_SYMBOL_GPL(crypto_blkcipher_type); 511EXPORT_SYMBOL_GPL(crypto_blkcipher_type);
511 512
513static int crypto_grab_nivcipher(struct crypto_skcipher_spawn *spawn,
514 const char *name, u32 type, u32 mask)
515{
516 struct crypto_alg *alg;
517 int err;
518
519 type = crypto_skcipher_type(type);
520 mask = crypto_skcipher_mask(mask) | CRYPTO_ALG_GENIV;
521
522 alg = crypto_alg_mod_lookup(name, type, mask);
523 if (IS_ERR(alg))
524 return PTR_ERR(alg);
525
526 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
527 crypto_mod_put(alg);
528 return err;
529}
530
531struct crypto_instance *skcipher_geniv_alloc(struct crypto_template *tmpl,
532 struct rtattr **tb, u32 type,
533 u32 mask)
534{
535 struct {
536 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
537 unsigned int keylen);
538 int (*encrypt)(struct ablkcipher_request *req);
539 int (*decrypt)(struct ablkcipher_request *req);
540
541 unsigned int min_keysize;
542 unsigned int max_keysize;
543 unsigned int ivsize;
544
545 const char *geniv;
546 } balg;
547 const char *name;
548 struct crypto_skcipher_spawn *spawn;
549 struct crypto_attr_type *algt;
550 struct crypto_instance *inst;
551 struct crypto_alg *alg;
552 int err;
553
554 algt = crypto_get_attr_type(tb);
555 err = PTR_ERR(algt);
556 if (IS_ERR(algt))
557 return ERR_PTR(err);
558
559 if ((algt->type ^ (CRYPTO_ALG_TYPE_GIVCIPHER | CRYPTO_ALG_GENIV)) &
560 algt->mask)
561 return ERR_PTR(-EINVAL);
562
563 name = crypto_attr_alg_name(tb[1]);
564 err = PTR_ERR(name);
565 if (IS_ERR(name))
566 return ERR_PTR(err);
567
568 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
569 if (!inst)
570 return ERR_PTR(-ENOMEM);
571
572 spawn = crypto_instance_ctx(inst);
573
574 /* Ignore async algorithms if necessary. */
575 mask |= crypto_requires_sync(algt->type, algt->mask);
576
577 crypto_set_skcipher_spawn(spawn, inst);
578 err = crypto_grab_nivcipher(spawn, name, type, mask);
579 if (err)
580 goto err_free_inst;
581
582 alg = crypto_skcipher_spawn_alg(spawn);
583
584 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
585 CRYPTO_ALG_TYPE_BLKCIPHER) {
586 balg.ivsize = alg->cra_blkcipher.ivsize;
587 balg.min_keysize = alg->cra_blkcipher.min_keysize;
588 balg.max_keysize = alg->cra_blkcipher.max_keysize;
589
590 balg.setkey = async_setkey;
591 balg.encrypt = async_encrypt;
592 balg.decrypt = async_decrypt;
593
594 balg.geniv = alg->cra_blkcipher.geniv;
595 } else {
596 balg.ivsize = alg->cra_ablkcipher.ivsize;
597 balg.min_keysize = alg->cra_ablkcipher.min_keysize;
598 balg.max_keysize = alg->cra_ablkcipher.max_keysize;
599
600 balg.setkey = alg->cra_ablkcipher.setkey;
601 balg.encrypt = alg->cra_ablkcipher.encrypt;
602 balg.decrypt = alg->cra_ablkcipher.decrypt;
603
604 balg.geniv = alg->cra_ablkcipher.geniv;
605 }
606
607 err = -EINVAL;
608 if (!balg.ivsize)
609 goto err_drop_alg;
610
611 /*
612 * This is only true if we're constructing an algorithm with its
613 * default IV generator. For the default generator we elide the
614 * template name and double-check the IV generator.
615 */
616 if (algt->mask & CRYPTO_ALG_GENIV) {
617 if (!balg.geniv)
618 balg.geniv = crypto_default_geniv(alg);
619 err = -EAGAIN;
620 if (strcmp(tmpl->name, balg.geniv))
621 goto err_drop_alg;
622
623 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
624 memcpy(inst->alg.cra_driver_name, alg->cra_driver_name,
625 CRYPTO_MAX_ALG_NAME);
626 } else {
627 err = -ENAMETOOLONG;
628 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
629 "%s(%s)", tmpl->name, alg->cra_name) >=
630 CRYPTO_MAX_ALG_NAME)
631 goto err_drop_alg;
632 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
633 "%s(%s)", tmpl->name, alg->cra_driver_name) >=
634 CRYPTO_MAX_ALG_NAME)
635 goto err_drop_alg;
636 }
637
638 inst->alg.cra_flags = CRYPTO_ALG_TYPE_GIVCIPHER | CRYPTO_ALG_GENIV;
639 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
640 inst->alg.cra_priority = alg->cra_priority;
641 inst->alg.cra_blocksize = alg->cra_blocksize;
642 inst->alg.cra_alignmask = alg->cra_alignmask;
643 inst->alg.cra_type = &crypto_givcipher_type;
644
645 inst->alg.cra_ablkcipher.ivsize = balg.ivsize;
646 inst->alg.cra_ablkcipher.min_keysize = balg.min_keysize;
647 inst->alg.cra_ablkcipher.max_keysize = balg.max_keysize;
648 inst->alg.cra_ablkcipher.geniv = balg.geniv;
649
650 inst->alg.cra_ablkcipher.setkey = balg.setkey;
651 inst->alg.cra_ablkcipher.encrypt = balg.encrypt;
652 inst->alg.cra_ablkcipher.decrypt = balg.decrypt;
653
654out:
655 return inst;
656
657err_drop_alg:
658 crypto_drop_skcipher(spawn);
659err_free_inst:
660 kfree(inst);
661 inst = ERR_PTR(err);
662 goto out;
663}
664EXPORT_SYMBOL_GPL(skcipher_geniv_alloc);
665
666void skcipher_geniv_free(struct crypto_instance *inst)
667{
668 crypto_drop_skcipher(crypto_instance_ctx(inst));
669 kfree(inst);
670}
671EXPORT_SYMBOL_GPL(skcipher_geniv_free);
672
673int skcipher_geniv_init(struct crypto_tfm *tfm)
674{
675 struct crypto_instance *inst = (void *)tfm->__crt_alg;
676 struct crypto_ablkcipher *cipher;
677
678 cipher = crypto_spawn_skcipher(crypto_instance_ctx(inst));
679 if (IS_ERR(cipher))
680 return PTR_ERR(cipher);
681
682 tfm->crt_ablkcipher.base = cipher;
683 tfm->crt_ablkcipher.reqsize += crypto_ablkcipher_reqsize(cipher);
684
685 return 0;
686}
687EXPORT_SYMBOL_GPL(skcipher_geniv_init);
688
689void skcipher_geniv_exit(struct crypto_tfm *tfm)
690{
691 crypto_free_ablkcipher(tfm->crt_ablkcipher.base);
692}
693EXPORT_SYMBOL_GPL(skcipher_geniv_exit);
694
512MODULE_LICENSE("GPL"); 695MODULE_LICENSE("GPL");
513MODULE_DESCRIPTION("Generic block chaining cipher type"); 696MODULE_DESCRIPTION("Generic block chaining cipher type");