.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Symmetric key cipher operations. |
---|
3 | 4 | * |
---|
.. | .. |
---|
6 | 7 | * the kernel is given a chance to schedule us once per page. |
---|
7 | 8 | * |
---|
8 | 9 | * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au> |
---|
9 | | - * |
---|
10 | | - * This program is free software; you can redistribute it and/or modify it |
---|
11 | | - * under the terms of the GNU General Public License as published by the Free |
---|
12 | | - * Software Foundation; either version 2 of the License, or (at your option) |
---|
13 | | - * any later version. |
---|
14 | | - * |
---|
15 | 10 | */ |
---|
16 | 11 | |
---|
17 | 12 | #include <crypto/internal/aead.h> |
---|
| 13 | +#include <crypto/internal/cipher.h> |
---|
18 | 14 | #include <crypto/internal/skcipher.h> |
---|
19 | 15 | #include <crypto/scatterwalk.h> |
---|
20 | 16 | #include <linux/bug.h> |
---|
.. | .. |
---|
483 | 479 | { |
---|
484 | 480 | int err; |
---|
485 | 481 | |
---|
| 482 | + might_sleep_if(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
---|
| 483 | + |
---|
486 | 484 | walk->flags &= ~SKCIPHER_WALK_PHYS; |
---|
487 | 485 | |
---|
488 | 486 | err = skcipher_walk_skcipher(walk, req); |
---|
.. | .. |
---|
552 | 550 | return err; |
---|
553 | 551 | } |
---|
554 | 552 | |
---|
555 | | -int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req, |
---|
556 | | - bool atomic) |
---|
557 | | -{ |
---|
558 | | - walk->total = req->cryptlen; |
---|
559 | | - |
---|
560 | | - return skcipher_walk_aead_common(walk, req, atomic); |
---|
561 | | -} |
---|
562 | | -EXPORT_SYMBOL_GPL(skcipher_walk_aead); |
---|
563 | | - |
---|
564 | 553 | int skcipher_walk_aead_encrypt(struct skcipher_walk *walk, |
---|
565 | 554 | struct aead_request *req, bool atomic) |
---|
566 | 555 | { |
---|
.. | .. |
---|
581 | 570 | } |
---|
582 | 571 | EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt); |
---|
583 | 572 | |
---|
584 | | -static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg) |
---|
585 | | -{ |
---|
586 | | - if (alg->cra_type == &crypto_blkcipher_type) |
---|
587 | | - return sizeof(struct crypto_blkcipher *); |
---|
588 | | - |
---|
589 | | - if (alg->cra_type == &crypto_ablkcipher_type || |
---|
590 | | - alg->cra_type == &crypto_givcipher_type) |
---|
591 | | - return sizeof(struct crypto_ablkcipher *); |
---|
592 | | - |
---|
593 | | - return crypto_alg_extsize(alg); |
---|
594 | | -} |
---|
595 | | - |
---|
596 | 573 | static void skcipher_set_needkey(struct crypto_skcipher *tfm) |
---|
597 | 574 | { |
---|
598 | | - if (tfm->keysize) |
---|
| 575 | + if (crypto_skcipher_max_keysize(tfm) != 0) |
---|
599 | 576 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY); |
---|
600 | | -} |
---|
601 | | - |
---|
602 | | -static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm, |
---|
603 | | - const u8 *key, unsigned int keylen) |
---|
604 | | -{ |
---|
605 | | - struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm); |
---|
606 | | - struct crypto_blkcipher *blkcipher = *ctx; |
---|
607 | | - int err; |
---|
608 | | - |
---|
609 | | - crypto_blkcipher_clear_flags(blkcipher, ~0); |
---|
610 | | - crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) & |
---|
611 | | - CRYPTO_TFM_REQ_MASK); |
---|
612 | | - err = crypto_blkcipher_setkey(blkcipher, key, keylen); |
---|
613 | | - crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) & |
---|
614 | | - CRYPTO_TFM_RES_MASK); |
---|
615 | | - if (unlikely(err)) { |
---|
616 | | - skcipher_set_needkey(tfm); |
---|
617 | | - return err; |
---|
618 | | - } |
---|
619 | | - |
---|
620 | | - crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); |
---|
621 | | - return 0; |
---|
622 | | -} |
---|
623 | | - |
---|
624 | | -static int skcipher_crypt_blkcipher(struct skcipher_request *req, |
---|
625 | | - int (*crypt)(struct blkcipher_desc *, |
---|
626 | | - struct scatterlist *, |
---|
627 | | - struct scatterlist *, |
---|
628 | | - unsigned int)) |
---|
629 | | -{ |
---|
630 | | - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
---|
631 | | - struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm); |
---|
632 | | - struct blkcipher_desc desc = { |
---|
633 | | - .tfm = *ctx, |
---|
634 | | - .info = req->iv, |
---|
635 | | - .flags = req->base.flags, |
---|
636 | | - }; |
---|
637 | | - |
---|
638 | | - |
---|
639 | | - return crypt(&desc, req->dst, req->src, req->cryptlen); |
---|
640 | | -} |
---|
641 | | - |
---|
642 | | -static int skcipher_encrypt_blkcipher(struct skcipher_request *req) |
---|
643 | | -{ |
---|
644 | | - struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); |
---|
645 | | - struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher); |
---|
646 | | - struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher; |
---|
647 | | - |
---|
648 | | - return skcipher_crypt_blkcipher(req, alg->encrypt); |
---|
649 | | -} |
---|
650 | | - |
---|
651 | | -static int skcipher_decrypt_blkcipher(struct skcipher_request *req) |
---|
652 | | -{ |
---|
653 | | - struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); |
---|
654 | | - struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher); |
---|
655 | | - struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher; |
---|
656 | | - |
---|
657 | | - return skcipher_crypt_blkcipher(req, alg->decrypt); |
---|
658 | | -} |
---|
659 | | - |
---|
660 | | -static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm) |
---|
661 | | -{ |
---|
662 | | - struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm); |
---|
663 | | - |
---|
664 | | - crypto_free_blkcipher(*ctx); |
---|
665 | | -} |
---|
666 | | - |
---|
667 | | -static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm) |
---|
668 | | -{ |
---|
669 | | - struct crypto_alg *calg = tfm->__crt_alg; |
---|
670 | | - struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm); |
---|
671 | | - struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm); |
---|
672 | | - struct crypto_blkcipher *blkcipher; |
---|
673 | | - struct crypto_tfm *btfm; |
---|
674 | | - |
---|
675 | | - if (!crypto_mod_get(calg)) |
---|
676 | | - return -EAGAIN; |
---|
677 | | - |
---|
678 | | - btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER, |
---|
679 | | - CRYPTO_ALG_TYPE_MASK); |
---|
680 | | - if (IS_ERR(btfm)) { |
---|
681 | | - crypto_mod_put(calg); |
---|
682 | | - return PTR_ERR(btfm); |
---|
683 | | - } |
---|
684 | | - |
---|
685 | | - blkcipher = __crypto_blkcipher_cast(btfm); |
---|
686 | | - *ctx = blkcipher; |
---|
687 | | - tfm->exit = crypto_exit_skcipher_ops_blkcipher; |
---|
688 | | - |
---|
689 | | - skcipher->setkey = skcipher_setkey_blkcipher; |
---|
690 | | - skcipher->encrypt = skcipher_encrypt_blkcipher; |
---|
691 | | - skcipher->decrypt = skcipher_decrypt_blkcipher; |
---|
692 | | - |
---|
693 | | - skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher); |
---|
694 | | - skcipher->keysize = calg->cra_blkcipher.max_keysize; |
---|
695 | | - |
---|
696 | | - skcipher_set_needkey(skcipher); |
---|
697 | | - |
---|
698 | | - return 0; |
---|
699 | | -} |
---|
700 | | - |
---|
701 | | -static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm, |
---|
702 | | - const u8 *key, unsigned int keylen) |
---|
703 | | -{ |
---|
704 | | - struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm); |
---|
705 | | - struct crypto_ablkcipher *ablkcipher = *ctx; |
---|
706 | | - int err; |
---|
707 | | - |
---|
708 | | - crypto_ablkcipher_clear_flags(ablkcipher, ~0); |
---|
709 | | - crypto_ablkcipher_set_flags(ablkcipher, |
---|
710 | | - crypto_skcipher_get_flags(tfm) & |
---|
711 | | - CRYPTO_TFM_REQ_MASK); |
---|
712 | | - err = crypto_ablkcipher_setkey(ablkcipher, key, keylen); |
---|
713 | | - crypto_skcipher_set_flags(tfm, |
---|
714 | | - crypto_ablkcipher_get_flags(ablkcipher) & |
---|
715 | | - CRYPTO_TFM_RES_MASK); |
---|
716 | | - if (unlikely(err)) { |
---|
717 | | - skcipher_set_needkey(tfm); |
---|
718 | | - return err; |
---|
719 | | - } |
---|
720 | | - |
---|
721 | | - crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); |
---|
722 | | - return 0; |
---|
723 | | -} |
---|
724 | | - |
---|
725 | | -static int skcipher_crypt_ablkcipher(struct skcipher_request *req, |
---|
726 | | - int (*crypt)(struct ablkcipher_request *)) |
---|
727 | | -{ |
---|
728 | | - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
---|
729 | | - struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm); |
---|
730 | | - struct ablkcipher_request *subreq = skcipher_request_ctx(req); |
---|
731 | | - |
---|
732 | | - ablkcipher_request_set_tfm(subreq, *ctx); |
---|
733 | | - ablkcipher_request_set_callback(subreq, skcipher_request_flags(req), |
---|
734 | | - req->base.complete, req->base.data); |
---|
735 | | - ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, |
---|
736 | | - req->iv); |
---|
737 | | - |
---|
738 | | - return crypt(subreq); |
---|
739 | | -} |
---|
740 | | - |
---|
741 | | -static int skcipher_encrypt_ablkcipher(struct skcipher_request *req) |
---|
742 | | -{ |
---|
743 | | - struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); |
---|
744 | | - struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher); |
---|
745 | | - struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher; |
---|
746 | | - |
---|
747 | | - return skcipher_crypt_ablkcipher(req, alg->encrypt); |
---|
748 | | -} |
---|
749 | | - |
---|
750 | | -static int skcipher_decrypt_ablkcipher(struct skcipher_request *req) |
---|
751 | | -{ |
---|
752 | | - struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); |
---|
753 | | - struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher); |
---|
754 | | - struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher; |
---|
755 | | - |
---|
756 | | - return skcipher_crypt_ablkcipher(req, alg->decrypt); |
---|
757 | | -} |
---|
758 | | - |
---|
759 | | -static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm) |
---|
760 | | -{ |
---|
761 | | - struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm); |
---|
762 | | - |
---|
763 | | - crypto_free_ablkcipher(*ctx); |
---|
764 | | -} |
---|
765 | | - |
---|
766 | | -static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm) |
---|
767 | | -{ |
---|
768 | | - struct crypto_alg *calg = tfm->__crt_alg; |
---|
769 | | - struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm); |
---|
770 | | - struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm); |
---|
771 | | - struct crypto_ablkcipher *ablkcipher; |
---|
772 | | - struct crypto_tfm *abtfm; |
---|
773 | | - |
---|
774 | | - if (!crypto_mod_get(calg)) |
---|
775 | | - return -EAGAIN; |
---|
776 | | - |
---|
777 | | - abtfm = __crypto_alloc_tfm(calg, 0, 0); |
---|
778 | | - if (IS_ERR(abtfm)) { |
---|
779 | | - crypto_mod_put(calg); |
---|
780 | | - return PTR_ERR(abtfm); |
---|
781 | | - } |
---|
782 | | - |
---|
783 | | - ablkcipher = __crypto_ablkcipher_cast(abtfm); |
---|
784 | | - *ctx = ablkcipher; |
---|
785 | | - tfm->exit = crypto_exit_skcipher_ops_ablkcipher; |
---|
786 | | - |
---|
787 | | - skcipher->setkey = skcipher_setkey_ablkcipher; |
---|
788 | | - skcipher->encrypt = skcipher_encrypt_ablkcipher; |
---|
789 | | - skcipher->decrypt = skcipher_decrypt_ablkcipher; |
---|
790 | | - |
---|
791 | | - skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher); |
---|
792 | | - skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) + |
---|
793 | | - sizeof(struct ablkcipher_request); |
---|
794 | | - skcipher->keysize = calg->cra_ablkcipher.max_keysize; |
---|
795 | | - |
---|
796 | | - skcipher_set_needkey(skcipher); |
---|
797 | | - |
---|
798 | | - return 0; |
---|
799 | 577 | } |
---|
800 | 578 | |
---|
801 | 579 | static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm, |
---|
.. | .. |
---|
815 | 593 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
---|
816 | 594 | memcpy(alignbuffer, key, keylen); |
---|
817 | 595 | ret = cipher->setkey(tfm, alignbuffer, keylen); |
---|
818 | | - kzfree(buffer); |
---|
| 596 | + kfree_sensitive(buffer); |
---|
819 | 597 | return ret; |
---|
820 | 598 | } |
---|
821 | 599 | |
---|
822 | | -static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key, |
---|
| 600 | +int crypto_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key, |
---|
823 | 601 | unsigned int keylen) |
---|
824 | 602 | { |
---|
825 | 603 | struct skcipher_alg *cipher = crypto_skcipher_alg(tfm); |
---|
826 | 604 | unsigned long alignmask = crypto_skcipher_alignmask(tfm); |
---|
827 | 605 | int err; |
---|
828 | 606 | |
---|
829 | | - if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) { |
---|
830 | | - crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
---|
| 607 | + if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) |
---|
831 | 608 | return -EINVAL; |
---|
832 | | - } |
---|
833 | 609 | |
---|
834 | 610 | if ((unsigned long)key & alignmask) |
---|
835 | 611 | err = skcipher_setkey_unaligned(tfm, key, keylen); |
---|
.. | .. |
---|
844 | 620 | crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); |
---|
845 | 621 | return 0; |
---|
846 | 622 | } |
---|
| 623 | +EXPORT_SYMBOL_GPL(crypto_skcipher_setkey); |
---|
| 624 | + |
---|
| 625 | +int crypto_skcipher_encrypt(struct skcipher_request *req) |
---|
| 626 | +{ |
---|
| 627 | + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
---|
| 628 | + struct crypto_alg *alg = tfm->base.__crt_alg; |
---|
| 629 | + unsigned int cryptlen = req->cryptlen; |
---|
| 630 | + int ret; |
---|
| 631 | + |
---|
| 632 | + crypto_stats_get(alg); |
---|
| 633 | + if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
---|
| 634 | + ret = -ENOKEY; |
---|
| 635 | + else |
---|
| 636 | + ret = crypto_skcipher_alg(tfm)->encrypt(req); |
---|
| 637 | + crypto_stats_skcipher_encrypt(cryptlen, ret, alg); |
---|
| 638 | + return ret; |
---|
| 639 | +} |
---|
| 640 | +EXPORT_SYMBOL_GPL(crypto_skcipher_encrypt); |
---|
| 641 | + |
---|
| 642 | +int crypto_skcipher_decrypt(struct skcipher_request *req) |
---|
| 643 | +{ |
---|
| 644 | + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
---|
| 645 | + struct crypto_alg *alg = tfm->base.__crt_alg; |
---|
| 646 | + unsigned int cryptlen = req->cryptlen; |
---|
| 647 | + int ret; |
---|
| 648 | + |
---|
| 649 | + crypto_stats_get(alg); |
---|
| 650 | + if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
---|
| 651 | + ret = -ENOKEY; |
---|
| 652 | + else |
---|
| 653 | + ret = crypto_skcipher_alg(tfm)->decrypt(req); |
---|
| 654 | + crypto_stats_skcipher_decrypt(cryptlen, ret, alg); |
---|
| 655 | + return ret; |
---|
| 656 | +} |
---|
| 657 | +EXPORT_SYMBOL_GPL(crypto_skcipher_decrypt); |
---|
847 | 658 | |
---|
848 | 659 | static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm) |
---|
849 | 660 | { |
---|
.. | .. |
---|
857 | 668 | { |
---|
858 | 669 | struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm); |
---|
859 | 670 | struct skcipher_alg *alg = crypto_skcipher_alg(skcipher); |
---|
860 | | - |
---|
861 | | - if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type) |
---|
862 | | - return crypto_init_skcipher_ops_blkcipher(tfm); |
---|
863 | | - |
---|
864 | | - if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type || |
---|
865 | | - tfm->__crt_alg->cra_type == &crypto_givcipher_type) |
---|
866 | | - return crypto_init_skcipher_ops_ablkcipher(tfm); |
---|
867 | | - |
---|
868 | | - skcipher->setkey = skcipher_setkey; |
---|
869 | | - skcipher->encrypt = alg->encrypt; |
---|
870 | | - skcipher->decrypt = alg->decrypt; |
---|
871 | | - skcipher->ivsize = alg->ivsize; |
---|
872 | | - skcipher->keysize = alg->max_keysize; |
---|
873 | 671 | |
---|
874 | 672 | skcipher_set_needkey(skcipher); |
---|
875 | 673 | |
---|
.. | .. |
---|
915 | 713 | struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg, |
---|
916 | 714 | base); |
---|
917 | 715 | |
---|
918 | | - strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type)); |
---|
919 | | - strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv)); |
---|
| 716 | + memset(&rblkcipher, 0, sizeof(rblkcipher)); |
---|
| 717 | + |
---|
| 718 | + strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type)); |
---|
| 719 | + strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv)); |
---|
920 | 720 | |
---|
921 | 721 | rblkcipher.blocksize = alg->cra_blocksize; |
---|
922 | 722 | rblkcipher.min_keysize = skcipher->min_keysize; |
---|
923 | 723 | rblkcipher.max_keysize = skcipher->max_keysize; |
---|
924 | 724 | rblkcipher.ivsize = skcipher->ivsize; |
---|
925 | 725 | |
---|
926 | | - if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER, |
---|
927 | | - sizeof(struct crypto_report_blkcipher), &rblkcipher)) |
---|
928 | | - goto nla_put_failure; |
---|
929 | | - return 0; |
---|
930 | | - |
---|
931 | | -nla_put_failure: |
---|
932 | | - return -EMSGSIZE; |
---|
| 726 | + return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER, |
---|
| 727 | + sizeof(rblkcipher), &rblkcipher); |
---|
933 | 728 | } |
---|
934 | 729 | #else |
---|
935 | 730 | static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg) |
---|
.. | .. |
---|
938 | 733 | } |
---|
939 | 734 | #endif |
---|
940 | 735 | |
---|
941 | | -static const struct crypto_type crypto_skcipher_type2 = { |
---|
942 | | - .extsize = crypto_skcipher_extsize, |
---|
| 736 | +static const struct crypto_type crypto_skcipher_type = { |
---|
| 737 | + .extsize = crypto_alg_extsize, |
---|
943 | 738 | .init_tfm = crypto_skcipher_init_tfm, |
---|
944 | 739 | .free = crypto_skcipher_free_instance, |
---|
945 | 740 | #ifdef CONFIG_PROC_FS |
---|
.. | .. |
---|
947 | 742 | #endif |
---|
948 | 743 | .report = crypto_skcipher_report, |
---|
949 | 744 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
---|
950 | | - .maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK, |
---|
| 745 | + .maskset = CRYPTO_ALG_TYPE_MASK, |
---|
951 | 746 | .type = CRYPTO_ALG_TYPE_SKCIPHER, |
---|
952 | 747 | .tfmsize = offsetof(struct crypto_skcipher, base), |
---|
953 | 748 | }; |
---|
954 | 749 | |
---|
955 | 750 | int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, |
---|
956 | | - const char *name, u32 type, u32 mask) |
---|
| 751 | + struct crypto_instance *inst, |
---|
| 752 | + const char *name, u32 type, u32 mask) |
---|
957 | 753 | { |
---|
958 | | - spawn->base.frontend = &crypto_skcipher_type2; |
---|
959 | | - return crypto_grab_spawn(&spawn->base, name, type, mask); |
---|
| 754 | + spawn->base.frontend = &crypto_skcipher_type; |
---|
| 755 | + return crypto_grab_spawn(&spawn->base, inst, name, type, mask); |
---|
960 | 756 | } |
---|
961 | 757 | EXPORT_SYMBOL_GPL(crypto_grab_skcipher); |
---|
962 | 758 | |
---|
963 | 759 | struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name, |
---|
964 | 760 | u32 type, u32 mask) |
---|
965 | 761 | { |
---|
966 | | - return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask); |
---|
| 762 | + return crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask); |
---|
967 | 763 | } |
---|
968 | 764 | EXPORT_SYMBOL_GPL(crypto_alloc_skcipher); |
---|
969 | 765 | |
---|
.. | .. |
---|
975 | 771 | /* Only sync algorithms allowed. */ |
---|
976 | 772 | mask |= CRYPTO_ALG_ASYNC; |
---|
977 | 773 | |
---|
978 | | - tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask); |
---|
| 774 | + tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask); |
---|
979 | 775 | |
---|
980 | 776 | /* |
---|
981 | 777 | * Make sure we do not allocate something that might get used with |
---|
.. | .. |
---|
991 | 787 | } |
---|
992 | 788 | EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher); |
---|
993 | 789 | |
---|
994 | | -int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask) |
---|
| 790 | +int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask) |
---|
995 | 791 | { |
---|
996 | | - return crypto_type_has_alg(alg_name, &crypto_skcipher_type2, |
---|
997 | | - type, mask); |
---|
| 792 | + return crypto_type_has_alg(alg_name, &crypto_skcipher_type, type, mask); |
---|
998 | 793 | } |
---|
999 | | -EXPORT_SYMBOL_GPL(crypto_has_skcipher2); |
---|
| 794 | +EXPORT_SYMBOL_GPL(crypto_has_skcipher); |
---|
1000 | 795 | |
---|
1001 | 796 | static int skcipher_prepare_alg(struct skcipher_alg *alg) |
---|
1002 | 797 | { |
---|
.. | .. |
---|
1011 | 806 | if (!alg->walksize) |
---|
1012 | 807 | alg->walksize = alg->chunksize; |
---|
1013 | 808 | |
---|
1014 | | - base->cra_type = &crypto_skcipher_type2; |
---|
| 809 | + base->cra_type = &crypto_skcipher_type; |
---|
1015 | 810 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
---|
1016 | 811 | base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER; |
---|
1017 | 812 | |
---|
.. | .. |
---|
1071 | 866 | { |
---|
1072 | 867 | int err; |
---|
1073 | 868 | |
---|
| 869 | + if (WARN_ON(!inst->free)) |
---|
| 870 | + return -EINVAL; |
---|
| 871 | + |
---|
1074 | 872 | err = skcipher_prepare_alg(&inst->alg); |
---|
1075 | 873 | if (err) |
---|
1076 | 874 | return err; |
---|
.. | .. |
---|
1079 | 877 | } |
---|
1080 | 878 | EXPORT_SYMBOL_GPL(skcipher_register_instance); |
---|
1081 | 879 | |
---|
| 880 | +static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key, |
---|
| 881 | + unsigned int keylen) |
---|
| 882 | +{ |
---|
| 883 | + struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
---|
| 884 | + |
---|
| 885 | + crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK); |
---|
| 886 | + crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) & |
---|
| 887 | + CRYPTO_TFM_REQ_MASK); |
---|
| 888 | + return crypto_cipher_setkey(cipher, key, keylen); |
---|
| 889 | +} |
---|
| 890 | + |
---|
| 891 | +static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm) |
---|
| 892 | +{ |
---|
| 893 | + struct skcipher_instance *inst = skcipher_alg_instance(tfm); |
---|
| 894 | + struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst); |
---|
| 895 | + struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm); |
---|
| 896 | + struct crypto_cipher *cipher; |
---|
| 897 | + |
---|
| 898 | + cipher = crypto_spawn_cipher(spawn); |
---|
| 899 | + if (IS_ERR(cipher)) |
---|
| 900 | + return PTR_ERR(cipher); |
---|
| 901 | + |
---|
| 902 | + ctx->cipher = cipher; |
---|
| 903 | + return 0; |
---|
| 904 | +} |
---|
| 905 | + |
---|
| 906 | +static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm) |
---|
| 907 | +{ |
---|
| 908 | + struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm); |
---|
| 909 | + |
---|
| 910 | + crypto_free_cipher(ctx->cipher); |
---|
| 911 | +} |
---|
| 912 | + |
---|
| 913 | +static void skcipher_free_instance_simple(struct skcipher_instance *inst) |
---|
| 914 | +{ |
---|
| 915 | + crypto_drop_cipher(skcipher_instance_ctx(inst)); |
---|
| 916 | + kfree(inst); |
---|
| 917 | +} |
---|
| 918 | + |
---|
| 919 | +/** |
---|
| 920 | + * skcipher_alloc_instance_simple - allocate instance of simple block cipher mode |
---|
| 921 | + * |
---|
| 922 | + * Allocate an skcipher_instance for a simple block cipher mode of operation, |
---|
| 923 | + * e.g. cbc or ecb. The instance context will have just a single crypto_spawn, |
---|
| 924 | + * that for the underlying cipher. The {min,max}_keysize, ivsize, blocksize, |
---|
| 925 | + * alignmask, and priority are set from the underlying cipher but can be |
---|
| 926 | + * overridden if needed. The tfm context defaults to skcipher_ctx_simple, and |
---|
| 927 | + * default ->setkey(), ->init(), and ->exit() methods are installed. |
---|
| 928 | + * |
---|
| 929 | + * @tmpl: the template being instantiated |
---|
| 930 | + * @tb: the template parameters |
---|
| 931 | + * |
---|
| 932 | + * Return: a pointer to the new instance, or an ERR_PTR(). The caller still |
---|
| 933 | + * needs to register the instance. |
---|
| 934 | + */ |
---|
| 935 | +struct skcipher_instance *skcipher_alloc_instance_simple( |
---|
| 936 | + struct crypto_template *tmpl, struct rtattr **tb) |
---|
| 937 | +{ |
---|
| 938 | + u32 mask; |
---|
| 939 | + struct skcipher_instance *inst; |
---|
| 940 | + struct crypto_cipher_spawn *spawn; |
---|
| 941 | + struct crypto_alg *cipher_alg; |
---|
| 942 | + int err; |
---|
| 943 | + |
---|
| 944 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask); |
---|
| 945 | + if (err) |
---|
| 946 | + return ERR_PTR(err); |
---|
| 947 | + |
---|
| 948 | + inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
---|
| 949 | + if (!inst) |
---|
| 950 | + return ERR_PTR(-ENOMEM); |
---|
| 951 | + spawn = skcipher_instance_ctx(inst); |
---|
| 952 | + |
---|
| 953 | + err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst), |
---|
| 954 | + crypto_attr_alg_name(tb[1]), 0, mask); |
---|
| 955 | + if (err) |
---|
| 956 | + goto err_free_inst; |
---|
| 957 | + cipher_alg = crypto_spawn_cipher_alg(spawn); |
---|
| 958 | + |
---|
| 959 | + err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name, |
---|
| 960 | + cipher_alg); |
---|
| 961 | + if (err) |
---|
| 962 | + goto err_free_inst; |
---|
| 963 | + |
---|
| 964 | + inst->free = skcipher_free_instance_simple; |
---|
| 965 | + |
---|
| 966 | + /* Default algorithm properties, can be overridden */ |
---|
| 967 | + inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize; |
---|
| 968 | + inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask; |
---|
| 969 | + inst->alg.base.cra_priority = cipher_alg->cra_priority; |
---|
| 970 | + inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize; |
---|
| 971 | + inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize; |
---|
| 972 | + inst->alg.ivsize = cipher_alg->cra_blocksize; |
---|
| 973 | + |
---|
| 974 | + /* Use skcipher_ctx_simple by default, can be overridden */ |
---|
| 975 | + inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple); |
---|
| 976 | + inst->alg.setkey = skcipher_setkey_simple; |
---|
| 977 | + inst->alg.init = skcipher_init_tfm_simple; |
---|
| 978 | + inst->alg.exit = skcipher_exit_tfm_simple; |
---|
| 979 | + |
---|
| 980 | + return inst; |
---|
| 981 | + |
---|
| 982 | +err_free_inst: |
---|
| 983 | + skcipher_free_instance_simple(inst); |
---|
| 984 | + return ERR_PTR(err); |
---|
| 985 | +} |
---|
| 986 | +EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple); |
---|
| 987 | + |
---|
1082 | 988 | MODULE_LICENSE("GPL"); |
---|
1083 | 989 | MODULE_DESCRIPTION("Symmetric key cipher type"); |
---|
| 990 | +MODULE_IMPORT_NS(CRYPTO_INTERNAL); |
---|