| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * GCM: Galois/Counter Mode. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi> |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 7 | | - * under the terms of the GNU General Public License version 2 as published |
|---|
| 8 | | - * by the Free Software Foundation. |
|---|
| 9 | 6 | */ |
|---|
| 10 | 7 | |
|---|
| 11 | 8 | #include <crypto/gf128mul.h> |
|---|
| .. | .. |
|---|
| 16 | 13 | #include <crypto/scatterwalk.h> |
|---|
| 17 | 14 | #include <crypto/gcm.h> |
|---|
| 18 | 15 | #include <crypto/hash.h> |
|---|
| 19 | | -#include "internal.h" |
|---|
| 20 | 16 | #include <linux/err.h> |
|---|
| 21 | 17 | #include <linux/init.h> |
|---|
| 22 | 18 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 50 | 46 | |
|---|
| 51 | 47 | struct crypto_rfc4543_ctx { |
|---|
| 52 | 48 | struct crypto_aead *child; |
|---|
| 53 | | - struct crypto_skcipher *null; |
|---|
| 49 | + struct crypto_sync_skcipher *null; |
|---|
| 54 | 50 | u8 nonce[4]; |
|---|
| 55 | 51 | }; |
|---|
| 56 | 52 | |
|---|
| .. | .. |
|---|
| 114 | 110 | crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) & |
|---|
| 115 | 111 | CRYPTO_TFM_REQ_MASK); |
|---|
| 116 | 112 | err = crypto_skcipher_setkey(ctr, key, keylen); |
|---|
| 117 | | - crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) & |
|---|
| 118 | | - CRYPTO_TFM_RES_MASK); |
|---|
| 119 | 113 | if (err) |
|---|
| 120 | 114 | return err; |
|---|
| 121 | 115 | |
|---|
| .. | .. |
|---|
| 144 | 138 | crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) & |
|---|
| 145 | 139 | CRYPTO_TFM_REQ_MASK); |
|---|
| 146 | 140 | err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128)); |
|---|
| 147 | | - crypto_aead_set_flags(aead, crypto_ahash_get_flags(ghash) & |
|---|
| 148 | | - CRYPTO_TFM_RES_MASK); |
|---|
| 149 | | - |
|---|
| 150 | 141 | out: |
|---|
| 151 | | - kzfree(data); |
|---|
| 142 | + kfree_sensitive(data); |
|---|
| 152 | 143 | return err; |
|---|
| 153 | 144 | } |
|---|
| 154 | 145 | |
|---|
| 155 | 146 | static int crypto_gcm_setauthsize(struct crypto_aead *tfm, |
|---|
| 156 | 147 | unsigned int authsize) |
|---|
| 157 | 148 | { |
|---|
| 158 | | - switch (authsize) { |
|---|
| 159 | | - case 4: |
|---|
| 160 | | - case 8: |
|---|
| 161 | | - case 12: |
|---|
| 162 | | - case 13: |
|---|
| 163 | | - case 14: |
|---|
| 164 | | - case 15: |
|---|
| 165 | | - case 16: |
|---|
| 166 | | - break; |
|---|
| 167 | | - default: |
|---|
| 168 | | - return -EINVAL; |
|---|
| 169 | | - } |
|---|
| 170 | | - |
|---|
| 171 | | - return 0; |
|---|
| 149 | + return crypto_gcm_check_authsize(authsize); |
|---|
| 172 | 150 | } |
|---|
| 173 | 151 | |
|---|
| 174 | 152 | static void crypto_gcm_init_common(struct aead_request *req) |
|---|
| .. | .. |
|---|
| 247 | 225 | struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); |
|---|
| 248 | 226 | struct ahash_request *ahreq = &pctx->u.ahreq; |
|---|
| 249 | 227 | struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; |
|---|
| 250 | | - u128 lengths; |
|---|
| 228 | + be128 lengths; |
|---|
| 251 | 229 | |
|---|
| 252 | 230 | lengths.a = cpu_to_be64(req->assoclen * 8); |
|---|
| 253 | 231 | lengths.b = cpu_to_be64(gctx->cryptlen * 8); |
|---|
| .. | .. |
|---|
| 600 | 578 | const char *ctr_name, |
|---|
| 601 | 579 | const char *ghash_name) |
|---|
| 602 | 580 | { |
|---|
| 603 | | - struct crypto_attr_type *algt; |
|---|
| 581 | + u32 mask; |
|---|
| 604 | 582 | struct aead_instance *inst; |
|---|
| 605 | | - struct skcipher_alg *ctr; |
|---|
| 606 | | - struct crypto_alg *ghash_alg; |
|---|
| 607 | | - struct hash_alg_common *ghash; |
|---|
| 608 | 583 | struct gcm_instance_ctx *ctx; |
|---|
| 584 | + struct skcipher_alg *ctr; |
|---|
| 585 | + struct hash_alg_common *ghash; |
|---|
| 609 | 586 | int err; |
|---|
| 610 | 587 | |
|---|
| 611 | | - algt = crypto_get_attr_type(tb); |
|---|
| 612 | | - if (IS_ERR(algt)) |
|---|
| 613 | | - return PTR_ERR(algt); |
|---|
| 588 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
|---|
| 589 | + if (err) |
|---|
| 590 | + return err; |
|---|
| 614 | 591 | |
|---|
| 615 | | - if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
|---|
| 616 | | - return -EINVAL; |
|---|
| 617 | | - |
|---|
| 618 | | - ghash_alg = crypto_find_alg(ghash_name, &crypto_ahash_type, |
|---|
| 619 | | - CRYPTO_ALG_TYPE_HASH, |
|---|
| 620 | | - CRYPTO_ALG_TYPE_AHASH_MASK | |
|---|
| 621 | | - crypto_requires_sync(algt->type, |
|---|
| 622 | | - algt->mask)); |
|---|
| 623 | | - if (IS_ERR(ghash_alg)) |
|---|
| 624 | | - return PTR_ERR(ghash_alg); |
|---|
| 625 | | - |
|---|
| 626 | | - ghash = __crypto_hash_alg_common(ghash_alg); |
|---|
| 627 | | - |
|---|
| 628 | | - err = -ENOMEM; |
|---|
| 629 | 592 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
|---|
| 630 | 593 | if (!inst) |
|---|
| 631 | | - goto out_put_ghash; |
|---|
| 632 | | - |
|---|
| 594 | + return -ENOMEM; |
|---|
| 633 | 595 | ctx = aead_instance_ctx(inst); |
|---|
| 634 | | - err = crypto_init_ahash_spawn(&ctx->ghash, ghash, |
|---|
| 635 | | - aead_crypto_instance(inst)); |
|---|
| 596 | + |
|---|
| 597 | + err = crypto_grab_ahash(&ctx->ghash, aead_crypto_instance(inst), |
|---|
| 598 | + ghash_name, 0, mask); |
|---|
| 636 | 599 | if (err) |
|---|
| 637 | 600 | goto err_free_inst; |
|---|
| 601 | + ghash = crypto_spawn_ahash_alg(&ctx->ghash); |
|---|
| 638 | 602 | |
|---|
| 639 | 603 | err = -EINVAL; |
|---|
| 640 | 604 | if (strcmp(ghash->base.cra_name, "ghash") != 0 || |
|---|
| 641 | 605 | ghash->digestsize != 16) |
|---|
| 642 | | - goto err_drop_ghash; |
|---|
| 606 | + goto err_free_inst; |
|---|
| 643 | 607 | |
|---|
| 644 | | - crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst)); |
|---|
| 645 | | - err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0, |
|---|
| 646 | | - crypto_requires_sync(algt->type, |
|---|
| 647 | | - algt->mask)); |
|---|
| 608 | + err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst), |
|---|
| 609 | + ctr_name, 0, mask); |
|---|
| 648 | 610 | if (err) |
|---|
| 649 | | - goto err_drop_ghash; |
|---|
| 650 | | - |
|---|
| 611 | + goto err_free_inst; |
|---|
| 651 | 612 | ctr = crypto_spawn_skcipher_alg(&ctx->ctr); |
|---|
| 652 | 613 | |
|---|
| 653 | 614 | /* The skcipher algorithm must be CTR mode, using 16-byte blocks. */ |
|---|
| .. | .. |
|---|
| 655 | 616 | if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 || |
|---|
| 656 | 617 | crypto_skcipher_alg_ivsize(ctr) != 16 || |
|---|
| 657 | 618 | ctr->base.cra_blocksize != 1) |
|---|
| 658 | | - goto out_put_ctr; |
|---|
| 619 | + goto err_free_inst; |
|---|
| 659 | 620 | |
|---|
| 660 | 621 | err = -ENAMETOOLONG; |
|---|
| 661 | 622 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
|---|
| 662 | 623 | "gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME) |
|---|
| 663 | | - goto out_put_ctr; |
|---|
| 624 | + goto err_free_inst; |
|---|
| 664 | 625 | |
|---|
| 665 | 626 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
|---|
| 666 | 627 | "gcm_base(%s,%s)", ctr->base.cra_driver_name, |
|---|
| 667 | | - ghash_alg->cra_driver_name) >= |
|---|
| 628 | + ghash->base.cra_driver_name) >= |
|---|
| 668 | 629 | CRYPTO_MAX_ALG_NAME) |
|---|
| 669 | | - goto out_put_ctr; |
|---|
| 630 | + goto err_free_inst; |
|---|
| 670 | 631 | |
|---|
| 671 | | - inst->alg.base.cra_flags = (ghash->base.cra_flags | |
|---|
| 672 | | - ctr->base.cra_flags) & CRYPTO_ALG_ASYNC; |
|---|
| 673 | 632 | inst->alg.base.cra_priority = (ghash->base.cra_priority + |
|---|
| 674 | 633 | ctr->base.cra_priority) / 2; |
|---|
| 675 | 634 | inst->alg.base.cra_blocksize = 1; |
|---|
| .. | .. |
|---|
| 689 | 648 | inst->free = crypto_gcm_free; |
|---|
| 690 | 649 | |
|---|
| 691 | 650 | err = aead_register_instance(tmpl, inst); |
|---|
| 692 | | - if (err) |
|---|
| 693 | | - goto out_put_ctr; |
|---|
| 694 | | - |
|---|
| 695 | | -out_put_ghash: |
|---|
| 696 | | - crypto_mod_put(ghash_alg); |
|---|
| 697 | | - return err; |
|---|
| 698 | | - |
|---|
| 699 | | -out_put_ctr: |
|---|
| 700 | | - crypto_drop_skcipher(&ctx->ctr); |
|---|
| 701 | | -err_drop_ghash: |
|---|
| 702 | | - crypto_drop_ahash(&ctx->ghash); |
|---|
| 651 | + if (err) { |
|---|
| 703 | 652 | err_free_inst: |
|---|
| 704 | | - kfree(inst); |
|---|
| 705 | | - goto out_put_ghash; |
|---|
| 653 | + crypto_gcm_free(inst); |
|---|
| 654 | + } |
|---|
| 655 | + return err; |
|---|
| 706 | 656 | } |
|---|
| 707 | 657 | |
|---|
| 708 | 658 | static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb) |
|---|
| .. | .. |
|---|
| 721 | 671 | return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash"); |
|---|
| 722 | 672 | } |
|---|
| 723 | 673 | |
|---|
| 724 | | -static struct crypto_template crypto_gcm_tmpl = { |
|---|
| 725 | | - .name = "gcm", |
|---|
| 726 | | - .create = crypto_gcm_create, |
|---|
| 727 | | - .module = THIS_MODULE, |
|---|
| 728 | | -}; |
|---|
| 729 | | - |
|---|
| 730 | 674 | static int crypto_gcm_base_create(struct crypto_template *tmpl, |
|---|
| 731 | 675 | struct rtattr **tb) |
|---|
| 732 | 676 | { |
|---|
| .. | .. |
|---|
| 744 | 688 | return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name); |
|---|
| 745 | 689 | } |
|---|
| 746 | 690 | |
|---|
| 747 | | -static struct crypto_template crypto_gcm_base_tmpl = { |
|---|
| 748 | | - .name = "gcm_base", |
|---|
| 749 | | - .create = crypto_gcm_base_create, |
|---|
| 750 | | - .module = THIS_MODULE, |
|---|
| 751 | | -}; |
|---|
| 752 | | - |
|---|
| 753 | 691 | static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key, |
|---|
| 754 | 692 | unsigned int keylen) |
|---|
| 755 | 693 | { |
|---|
| 756 | 694 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent); |
|---|
| 757 | 695 | struct crypto_aead *child = ctx->child; |
|---|
| 758 | | - int err; |
|---|
| 759 | 696 | |
|---|
| 760 | 697 | if (keylen < 4) |
|---|
| 761 | 698 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 766 | 703 | crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
|---|
| 767 | 704 | crypto_aead_set_flags(child, crypto_aead_get_flags(parent) & |
|---|
| 768 | 705 | CRYPTO_TFM_REQ_MASK); |
|---|
| 769 | | - err = crypto_aead_setkey(child, key, keylen); |
|---|
| 770 | | - crypto_aead_set_flags(parent, crypto_aead_get_flags(child) & |
|---|
| 771 | | - CRYPTO_TFM_RES_MASK); |
|---|
| 772 | | - |
|---|
| 773 | | - return err; |
|---|
| 706 | + return crypto_aead_setkey(child, key, keylen); |
|---|
| 774 | 707 | } |
|---|
| 775 | 708 | |
|---|
| 776 | 709 | static int crypto_rfc4106_setauthsize(struct crypto_aead *parent, |
|---|
| 777 | 710 | unsigned int authsize) |
|---|
| 778 | 711 | { |
|---|
| 779 | 712 | struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent); |
|---|
| 713 | + int err; |
|---|
| 780 | 714 | |
|---|
| 781 | | - switch (authsize) { |
|---|
| 782 | | - case 8: |
|---|
| 783 | | - case 12: |
|---|
| 784 | | - case 16: |
|---|
| 785 | | - break; |
|---|
| 786 | | - default: |
|---|
| 787 | | - return -EINVAL; |
|---|
| 788 | | - } |
|---|
| 715 | + err = crypto_rfc4106_check_authsize(authsize); |
|---|
| 716 | + if (err) |
|---|
| 717 | + return err; |
|---|
| 789 | 718 | |
|---|
| 790 | 719 | return crypto_aead_setauthsize(ctx->child, authsize); |
|---|
| 791 | 720 | } |
|---|
| .. | .. |
|---|
| 833 | 762 | |
|---|
| 834 | 763 | static int crypto_rfc4106_encrypt(struct aead_request *req) |
|---|
| 835 | 764 | { |
|---|
| 836 | | - if (req->assoclen != 16 && req->assoclen != 20) |
|---|
| 837 | | - return -EINVAL; |
|---|
| 765 | + int err; |
|---|
| 766 | + |
|---|
| 767 | + err = crypto_ipsec_check_assoclen(req->assoclen); |
|---|
| 768 | + if (err) |
|---|
| 769 | + return err; |
|---|
| 838 | 770 | |
|---|
| 839 | 771 | req = crypto_rfc4106_crypt(req); |
|---|
| 840 | 772 | |
|---|
| .. | .. |
|---|
| 843 | 775 | |
|---|
| 844 | 776 | static int crypto_rfc4106_decrypt(struct aead_request *req) |
|---|
| 845 | 777 | { |
|---|
| 846 | | - if (req->assoclen != 16 && req->assoclen != 20) |
|---|
| 847 | | - return -EINVAL; |
|---|
| 778 | + int err; |
|---|
| 779 | + |
|---|
| 780 | + err = crypto_ipsec_check_assoclen(req->assoclen); |
|---|
| 781 | + if (err) |
|---|
| 782 | + return err; |
|---|
| 848 | 783 | |
|---|
| 849 | 784 | req = crypto_rfc4106_crypt(req); |
|---|
| 850 | 785 | |
|---|
| .. | .. |
|---|
| 892 | 827 | static int crypto_rfc4106_create(struct crypto_template *tmpl, |
|---|
| 893 | 828 | struct rtattr **tb) |
|---|
| 894 | 829 | { |
|---|
| 895 | | - struct crypto_attr_type *algt; |
|---|
| 830 | + u32 mask; |
|---|
| 896 | 831 | struct aead_instance *inst; |
|---|
| 897 | 832 | struct crypto_aead_spawn *spawn; |
|---|
| 898 | 833 | struct aead_alg *alg; |
|---|
| 899 | | - const char *ccm_name; |
|---|
| 900 | 834 | int err; |
|---|
| 901 | 835 | |
|---|
| 902 | | - algt = crypto_get_attr_type(tb); |
|---|
| 903 | | - if (IS_ERR(algt)) |
|---|
| 904 | | - return PTR_ERR(algt); |
|---|
| 905 | | - |
|---|
| 906 | | - if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
|---|
| 907 | | - return -EINVAL; |
|---|
| 908 | | - |
|---|
| 909 | | - ccm_name = crypto_attr_alg_name(tb[1]); |
|---|
| 910 | | - if (IS_ERR(ccm_name)) |
|---|
| 911 | | - return PTR_ERR(ccm_name); |
|---|
| 836 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
|---|
| 837 | + if (err) |
|---|
| 838 | + return err; |
|---|
| 912 | 839 | |
|---|
| 913 | 840 | inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); |
|---|
| 914 | 841 | if (!inst) |
|---|
| 915 | 842 | return -ENOMEM; |
|---|
| 916 | 843 | |
|---|
| 917 | 844 | spawn = aead_instance_ctx(inst); |
|---|
| 918 | | - crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); |
|---|
| 919 | | - err = crypto_grab_aead(spawn, ccm_name, 0, |
|---|
| 920 | | - crypto_requires_sync(algt->type, algt->mask)); |
|---|
| 845 | + err = crypto_grab_aead(spawn, aead_crypto_instance(inst), |
|---|
| 846 | + crypto_attr_alg_name(tb[1]), 0, mask); |
|---|
| 921 | 847 | if (err) |
|---|
| 922 | | - goto out_free_inst; |
|---|
| 848 | + goto err_free_inst; |
|---|
| 923 | 849 | |
|---|
| 924 | 850 | alg = crypto_spawn_aead_alg(spawn); |
|---|
| 925 | 851 | |
|---|
| .. | .. |
|---|
| 927 | 853 | |
|---|
| 928 | 854 | /* Underlying IV size must be 12. */ |
|---|
| 929 | 855 | if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE) |
|---|
| 930 | | - goto out_drop_alg; |
|---|
| 856 | + goto err_free_inst; |
|---|
| 931 | 857 | |
|---|
| 932 | 858 | /* Not a stream cipher? */ |
|---|
| 933 | 859 | if (alg->base.cra_blocksize != 1) |
|---|
| 934 | | - goto out_drop_alg; |
|---|
| 860 | + goto err_free_inst; |
|---|
| 935 | 861 | |
|---|
| 936 | 862 | err = -ENAMETOOLONG; |
|---|
| 937 | 863 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
|---|
| .. | .. |
|---|
| 940 | 866 | snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
|---|
| 941 | 867 | "rfc4106(%s)", alg->base.cra_driver_name) >= |
|---|
| 942 | 868 | CRYPTO_MAX_ALG_NAME) |
|---|
| 943 | | - goto out_drop_alg; |
|---|
| 869 | + goto err_free_inst; |
|---|
| 944 | 870 | |
|---|
| 945 | | - inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
|---|
| 946 | 871 | inst->alg.base.cra_priority = alg->base.cra_priority; |
|---|
| 947 | 872 | inst->alg.base.cra_blocksize = 1; |
|---|
| 948 | 873 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
|---|
| .. | .. |
|---|
| 964 | 889 | inst->free = crypto_rfc4106_free; |
|---|
| 965 | 890 | |
|---|
| 966 | 891 | err = aead_register_instance(tmpl, inst); |
|---|
| 967 | | - if (err) |
|---|
| 968 | | - goto out_drop_alg; |
|---|
| 969 | | - |
|---|
| 970 | | -out: |
|---|
| 892 | + if (err) { |
|---|
| 893 | +err_free_inst: |
|---|
| 894 | + crypto_rfc4106_free(inst); |
|---|
| 895 | + } |
|---|
| 971 | 896 | return err; |
|---|
| 972 | | - |
|---|
| 973 | | -out_drop_alg: |
|---|
| 974 | | - crypto_drop_aead(spawn); |
|---|
| 975 | | -out_free_inst: |
|---|
| 976 | | - kfree(inst); |
|---|
| 977 | | - goto out; |
|---|
| 978 | 897 | } |
|---|
| 979 | | - |
|---|
| 980 | | -static struct crypto_template crypto_rfc4106_tmpl = { |
|---|
| 981 | | - .name = "rfc4106", |
|---|
| 982 | | - .create = crypto_rfc4106_create, |
|---|
| 983 | | - .module = THIS_MODULE, |
|---|
| 984 | | -}; |
|---|
| 985 | 898 | |
|---|
| 986 | 899 | static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key, |
|---|
| 987 | 900 | unsigned int keylen) |
|---|
| 988 | 901 | { |
|---|
| 989 | 902 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent); |
|---|
| 990 | 903 | struct crypto_aead *child = ctx->child; |
|---|
| 991 | | - int err; |
|---|
| 992 | 904 | |
|---|
| 993 | 905 | if (keylen < 4) |
|---|
| 994 | 906 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 999 | 911 | crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
|---|
| 1000 | 912 | crypto_aead_set_flags(child, crypto_aead_get_flags(parent) & |
|---|
| 1001 | 913 | CRYPTO_TFM_REQ_MASK); |
|---|
| 1002 | | - err = crypto_aead_setkey(child, key, keylen); |
|---|
| 1003 | | - crypto_aead_set_flags(parent, crypto_aead_get_flags(child) & |
|---|
| 1004 | | - CRYPTO_TFM_RES_MASK); |
|---|
| 1005 | | - |
|---|
| 1006 | | - return err; |
|---|
| 914 | + return crypto_aead_setkey(child, key, keylen); |
|---|
| 1007 | 915 | } |
|---|
| 1008 | 916 | |
|---|
| 1009 | 917 | static int crypto_rfc4543_setauthsize(struct crypto_aead *parent, |
|---|
| .. | .. |
|---|
| 1055 | 963 | unsigned int authsize = crypto_aead_authsize(aead); |
|---|
| 1056 | 964 | unsigned int nbytes = req->assoclen + req->cryptlen - |
|---|
| 1057 | 965 | (enc ? 0 : authsize); |
|---|
| 1058 | | - SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null); |
|---|
| 966 | + SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null); |
|---|
| 1059 | 967 | |
|---|
| 1060 | | - skcipher_request_set_tfm(nreq, ctx->null); |
|---|
| 968 | + skcipher_request_set_sync_tfm(nreq, ctx->null); |
|---|
| 1061 | 969 | skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL); |
|---|
| 1062 | 970 | skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL); |
|---|
| 1063 | 971 | |
|---|
| .. | .. |
|---|
| 1066 | 974 | |
|---|
| 1067 | 975 | static int crypto_rfc4543_encrypt(struct aead_request *req) |
|---|
| 1068 | 976 | { |
|---|
| 1069 | | - return crypto_rfc4543_crypt(req, true); |
|---|
| 977 | + return crypto_ipsec_check_assoclen(req->assoclen) ?: |
|---|
| 978 | + crypto_rfc4543_crypt(req, true); |
|---|
| 1070 | 979 | } |
|---|
| 1071 | 980 | |
|---|
| 1072 | 981 | static int crypto_rfc4543_decrypt(struct aead_request *req) |
|---|
| 1073 | 982 | { |
|---|
| 1074 | | - return crypto_rfc4543_crypt(req, false); |
|---|
| 983 | + return crypto_ipsec_check_assoclen(req->assoclen) ?: |
|---|
| 984 | + crypto_rfc4543_crypt(req, false); |
|---|
| 1075 | 985 | } |
|---|
| 1076 | 986 | |
|---|
| 1077 | 987 | static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm) |
|---|
| .. | .. |
|---|
| 1081 | 991 | struct crypto_aead_spawn *spawn = &ictx->aead; |
|---|
| 1082 | 992 | struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm); |
|---|
| 1083 | 993 | struct crypto_aead *aead; |
|---|
| 1084 | | - struct crypto_skcipher *null; |
|---|
| 994 | + struct crypto_sync_skcipher *null; |
|---|
| 1085 | 995 | unsigned long align; |
|---|
| 1086 | 996 | int err = 0; |
|---|
| 1087 | 997 | |
|---|
| .. | .. |
|---|
| 1132 | 1042 | static int crypto_rfc4543_create(struct crypto_template *tmpl, |
|---|
| 1133 | 1043 | struct rtattr **tb) |
|---|
| 1134 | 1044 | { |
|---|
| 1135 | | - struct crypto_attr_type *algt; |
|---|
| 1045 | + u32 mask; |
|---|
| 1136 | 1046 | struct aead_instance *inst; |
|---|
| 1137 | | - struct crypto_aead_spawn *spawn; |
|---|
| 1138 | 1047 | struct aead_alg *alg; |
|---|
| 1139 | 1048 | struct crypto_rfc4543_instance_ctx *ctx; |
|---|
| 1140 | | - const char *ccm_name; |
|---|
| 1141 | 1049 | int err; |
|---|
| 1142 | 1050 | |
|---|
| 1143 | | - algt = crypto_get_attr_type(tb); |
|---|
| 1144 | | - if (IS_ERR(algt)) |
|---|
| 1145 | | - return PTR_ERR(algt); |
|---|
| 1146 | | - |
|---|
| 1147 | | - if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask) |
|---|
| 1148 | | - return -EINVAL; |
|---|
| 1149 | | - |
|---|
| 1150 | | - ccm_name = crypto_attr_alg_name(tb[1]); |
|---|
| 1151 | | - if (IS_ERR(ccm_name)) |
|---|
| 1152 | | - return PTR_ERR(ccm_name); |
|---|
| 1051 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask); |
|---|
| 1052 | + if (err) |
|---|
| 1053 | + return err; |
|---|
| 1153 | 1054 | |
|---|
| 1154 | 1055 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
|---|
| 1155 | 1056 | if (!inst) |
|---|
| 1156 | 1057 | return -ENOMEM; |
|---|
| 1157 | 1058 | |
|---|
| 1158 | 1059 | ctx = aead_instance_ctx(inst); |
|---|
| 1159 | | - spawn = &ctx->aead; |
|---|
| 1160 | | - crypto_set_aead_spawn(spawn, aead_crypto_instance(inst)); |
|---|
| 1161 | | - err = crypto_grab_aead(spawn, ccm_name, 0, |
|---|
| 1162 | | - crypto_requires_sync(algt->type, algt->mask)); |
|---|
| 1060 | + err = crypto_grab_aead(&ctx->aead, aead_crypto_instance(inst), |
|---|
| 1061 | + crypto_attr_alg_name(tb[1]), 0, mask); |
|---|
| 1163 | 1062 | if (err) |
|---|
| 1164 | | - goto out_free_inst; |
|---|
| 1063 | + goto err_free_inst; |
|---|
| 1165 | 1064 | |
|---|
| 1166 | | - alg = crypto_spawn_aead_alg(spawn); |
|---|
| 1065 | + alg = crypto_spawn_aead_alg(&ctx->aead); |
|---|
| 1167 | 1066 | |
|---|
| 1168 | 1067 | err = -EINVAL; |
|---|
| 1169 | 1068 | |
|---|
| 1170 | 1069 | /* Underlying IV size must be 12. */ |
|---|
| 1171 | 1070 | if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE) |
|---|
| 1172 | | - goto out_drop_alg; |
|---|
| 1071 | + goto err_free_inst; |
|---|
| 1173 | 1072 | |
|---|
| 1174 | 1073 | /* Not a stream cipher? */ |
|---|
| 1175 | 1074 | if (alg->base.cra_blocksize != 1) |
|---|
| 1176 | | - goto out_drop_alg; |
|---|
| 1075 | + goto err_free_inst; |
|---|
| 1177 | 1076 | |
|---|
| 1178 | 1077 | err = -ENAMETOOLONG; |
|---|
| 1179 | 1078 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
|---|
| .. | .. |
|---|
| 1182 | 1081 | snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
|---|
| 1183 | 1082 | "rfc4543(%s)", alg->base.cra_driver_name) >= |
|---|
| 1184 | 1083 | CRYPTO_MAX_ALG_NAME) |
|---|
| 1185 | | - goto out_drop_alg; |
|---|
| 1084 | + goto err_free_inst; |
|---|
| 1186 | 1085 | |
|---|
| 1187 | | - inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC; |
|---|
| 1188 | 1086 | inst->alg.base.cra_priority = alg->base.cra_priority; |
|---|
| 1189 | 1087 | inst->alg.base.cra_blocksize = 1; |
|---|
| 1190 | 1088 | inst->alg.base.cra_alignmask = alg->base.cra_alignmask; |
|---|
| .. | .. |
|---|
| 1203 | 1101 | inst->alg.encrypt = crypto_rfc4543_encrypt; |
|---|
| 1204 | 1102 | inst->alg.decrypt = crypto_rfc4543_decrypt; |
|---|
| 1205 | 1103 | |
|---|
| 1206 | | - inst->free = crypto_rfc4543_free, |
|---|
| 1104 | + inst->free = crypto_rfc4543_free; |
|---|
| 1207 | 1105 | |
|---|
| 1208 | 1106 | err = aead_register_instance(tmpl, inst); |
|---|
| 1209 | | - if (err) |
|---|
| 1210 | | - goto out_drop_alg; |
|---|
| 1211 | | - |
|---|
| 1212 | | -out: |
|---|
| 1107 | + if (err) { |
|---|
| 1108 | +err_free_inst: |
|---|
| 1109 | + crypto_rfc4543_free(inst); |
|---|
| 1110 | + } |
|---|
| 1213 | 1111 | return err; |
|---|
| 1214 | | - |
|---|
| 1215 | | -out_drop_alg: |
|---|
| 1216 | | - crypto_drop_aead(spawn); |
|---|
| 1217 | | -out_free_inst: |
|---|
| 1218 | | - kfree(inst); |
|---|
| 1219 | | - goto out; |
|---|
| 1220 | 1112 | } |
|---|
| 1221 | 1113 | |
|---|
| 1222 | | -static struct crypto_template crypto_rfc4543_tmpl = { |
|---|
| 1223 | | - .name = "rfc4543", |
|---|
| 1224 | | - .create = crypto_rfc4543_create, |
|---|
| 1225 | | - .module = THIS_MODULE, |
|---|
| 1114 | +static struct crypto_template crypto_gcm_tmpls[] = { |
|---|
| 1115 | + { |
|---|
| 1116 | + .name = "gcm_base", |
|---|
| 1117 | + .create = crypto_gcm_base_create, |
|---|
| 1118 | + .module = THIS_MODULE, |
|---|
| 1119 | + }, { |
|---|
| 1120 | + .name = "gcm", |
|---|
| 1121 | + .create = crypto_gcm_create, |
|---|
| 1122 | + .module = THIS_MODULE, |
|---|
| 1123 | + }, { |
|---|
| 1124 | + .name = "rfc4106", |
|---|
| 1125 | + .create = crypto_rfc4106_create, |
|---|
| 1126 | + .module = THIS_MODULE, |
|---|
| 1127 | + }, { |
|---|
| 1128 | + .name = "rfc4543", |
|---|
| 1129 | + .create = crypto_rfc4543_create, |
|---|
| 1130 | + .module = THIS_MODULE, |
|---|
| 1131 | + }, |
|---|
| 1226 | 1132 | }; |
|---|
| 1227 | 1133 | |
|---|
| 1228 | 1134 | static int __init crypto_gcm_module_init(void) |
|---|
| .. | .. |
|---|
| 1235 | 1141 | |
|---|
| 1236 | 1142 | sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf)); |
|---|
| 1237 | 1143 | |
|---|
| 1238 | | - err = crypto_register_template(&crypto_gcm_base_tmpl); |
|---|
| 1144 | + err = crypto_register_templates(crypto_gcm_tmpls, |
|---|
| 1145 | + ARRAY_SIZE(crypto_gcm_tmpls)); |
|---|
| 1239 | 1146 | if (err) |
|---|
| 1240 | | - goto out; |
|---|
| 1147 | + kfree(gcm_zeroes); |
|---|
| 1241 | 1148 | |
|---|
| 1242 | | - err = crypto_register_template(&crypto_gcm_tmpl); |
|---|
| 1243 | | - if (err) |
|---|
| 1244 | | - goto out_undo_base; |
|---|
| 1245 | | - |
|---|
| 1246 | | - err = crypto_register_template(&crypto_rfc4106_tmpl); |
|---|
| 1247 | | - if (err) |
|---|
| 1248 | | - goto out_undo_gcm; |
|---|
| 1249 | | - |
|---|
| 1250 | | - err = crypto_register_template(&crypto_rfc4543_tmpl); |
|---|
| 1251 | | - if (err) |
|---|
| 1252 | | - goto out_undo_rfc4106; |
|---|
| 1253 | | - |
|---|
| 1254 | | - return 0; |
|---|
| 1255 | | - |
|---|
| 1256 | | -out_undo_rfc4106: |
|---|
| 1257 | | - crypto_unregister_template(&crypto_rfc4106_tmpl); |
|---|
| 1258 | | -out_undo_gcm: |
|---|
| 1259 | | - crypto_unregister_template(&crypto_gcm_tmpl); |
|---|
| 1260 | | -out_undo_base: |
|---|
| 1261 | | - crypto_unregister_template(&crypto_gcm_base_tmpl); |
|---|
| 1262 | | -out: |
|---|
| 1263 | | - kfree(gcm_zeroes); |
|---|
| 1264 | 1149 | return err; |
|---|
| 1265 | 1150 | } |
|---|
| 1266 | 1151 | |
|---|
| 1267 | 1152 | static void __exit crypto_gcm_module_exit(void) |
|---|
| 1268 | 1153 | { |
|---|
| 1269 | 1154 | kfree(gcm_zeroes); |
|---|
| 1270 | | - crypto_unregister_template(&crypto_rfc4543_tmpl); |
|---|
| 1271 | | - crypto_unregister_template(&crypto_rfc4106_tmpl); |
|---|
| 1272 | | - crypto_unregister_template(&crypto_gcm_tmpl); |
|---|
| 1273 | | - crypto_unregister_template(&crypto_gcm_base_tmpl); |
|---|
| 1155 | + crypto_unregister_templates(crypto_gcm_tmpls, |
|---|
| 1156 | + ARRAY_SIZE(crypto_gcm_tmpls)); |
|---|
| 1274 | 1157 | } |
|---|
| 1275 | 1158 | |
|---|
| 1276 | | -module_init(crypto_gcm_module_init); |
|---|
| 1159 | +subsys_initcall(crypto_gcm_module_init); |
|---|
| 1277 | 1160 | module_exit(crypto_gcm_module_exit); |
|---|
| 1278 | 1161 | |
|---|
| 1279 | 1162 | MODULE_LICENSE("GPL"); |
|---|