Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: caam/des - switch to new verification routines

Cc: Horia Geanta <horia.geanta@nxp.com>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Ard Biesheuvel and committed by
Herbert Xu
a628c5a1 05a7238d

+31 -92
+13 -36
drivers/crypto/caam/caamalg.c
··· 628 628 unsigned int keylen) 629 629 { 630 630 struct crypto_authenc_keys keys; 631 - u32 flags; 632 631 int err; 633 632 634 633 err = crypto_authenc_extractkeys(&keys, key, keylen); 635 634 if (unlikely(err)) 636 - goto badkey; 635 + return err; 637 636 638 - err = -EINVAL; 639 - if (keys.enckeylen != DES3_EDE_KEY_SIZE) 640 - goto badkey; 637 + err = verify_aead_des3_key(aead, keys.enckey, keys.enckeylen) ?: 638 + aead_setkey(aead, key, keylen); 641 639 642 - flags = crypto_aead_get_flags(aead); 643 - err = __des3_verify_key(&flags, keys.enckey); 644 - if (unlikely(err)) { 645 - crypto_aead_set_flags(aead, flags); 646 - goto out; 647 - } 648 - 649 - err = aead_setkey(aead, key, keylen); 650 - 651 - out: 652 640 memzero_explicit(&keys, sizeof(keys)); 653 641 return err; 654 - 655 - badkey: 656 - crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN); 657 - goto out; 658 642 } 659 643 660 644 static int gcm_setkey(struct crypto_aead *aead, ··· 827 843 static int des_skcipher_setkey(struct crypto_skcipher *skcipher, 828 844 const u8 *key, unsigned int keylen) 829 845 { 830 - u32 tmp[DES3_EDE_EXPKEY_WORDS]; 831 - struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher); 846 + return verify_skcipher_des_key(skcipher, key) ?: 847 + skcipher_setkey(skcipher, key, keylen, 0); 848 + } 832 849 833 - if (keylen == DES3_EDE_KEY_SIZE && 834 - __des3_ede_setkey(tmp, &tfm->crt_flags, key, DES3_EDE_KEY_SIZE)) { 835 - return -EINVAL; 836 - } 837 - 838 - if (!des_ekey(tmp, key) && (crypto_skcipher_get_flags(skcipher) & 839 - CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) { 840 - crypto_skcipher_set_flags(skcipher, 841 - CRYPTO_TFM_RES_WEAK_KEY); 842 - return -EINVAL; 843 - } 844 - 845 - return skcipher_setkey(skcipher, key, keylen, 0); 850 + static int des3_skcipher_setkey(struct crypto_skcipher *skcipher, 851 + const u8 *key, unsigned int keylen) 852 + { 853 + return verify_skcipher_des3_key(skcipher, key) ?: 854 + skcipher_setkey(skcipher, key, keylen, 0); 846 855 } 847 856 848 857 static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, ··· 1931 1954 .cra_driver_name = "cbc-3des-caam", 1932 1955 .cra_blocksize = DES3_EDE_BLOCK_SIZE, 1933 1956 }, 1934 - .setkey = des_skcipher_setkey, 1957 + .setkey = des3_skcipher_setkey, 1935 1958 .encrypt = skcipher_encrypt, 1936 1959 .decrypt = skcipher_decrypt, 1937 1960 .min_keysize = DES3_EDE_KEY_SIZE, ··· 2050 2073 .cra_driver_name = "ecb-des3-caam", 2051 2074 .cra_blocksize = DES3_EDE_BLOCK_SIZE, 2052 2075 }, 2053 - .setkey = des_skcipher_setkey, 2076 + .setkey = des3_skcipher_setkey, 2054 2077 .encrypt = skcipher_encrypt, 2055 2078 .decrypt = skcipher_decrypt, 2056 2079 .min_keysize = DES3_EDE_KEY_SIZE,
+6 -30
drivers/crypto/caam/caamalg_qi.c
··· 278 278 unsigned int keylen) 279 279 { 280 280 struct crypto_authenc_keys keys; 281 - u32 flags; 282 281 int err; 283 282 284 283 err = crypto_authenc_extractkeys(&keys, key, keylen); 285 284 if (unlikely(err)) 286 - goto badkey; 285 + return err; 287 286 288 - err = -EINVAL; 289 - if (keys.enckeylen != DES3_EDE_KEY_SIZE) 290 - goto badkey; 287 + err = verify_aead_des3_key(aead, keys.enckey, keys.enckeylen) ?: 288 + aead_setkey(aead, key, keylen); 291 289 292 - flags = crypto_aead_get_flags(aead); 293 - err = __des3_verify_key(&flags, keys.enckey); 294 - if (unlikely(err)) { 295 - crypto_aead_set_flags(aead, flags); 296 - goto out; 297 - } 298 - 299 - err = aead_setkey(aead, key, keylen); 300 - 301 - out: 302 290 memzero_explicit(&keys, sizeof(keys)); 303 291 return err; 304 - 305 - badkey: 306 - crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN); 307 - goto out; 308 292 } 309 293 310 294 static int gcm_set_sh_desc(struct crypto_aead *aead) ··· 729 745 static int des3_skcipher_setkey(struct crypto_skcipher *skcipher, 730 746 const u8 *key, unsigned int keylen) 731 747 { 732 - return unlikely(des3_verify_key(skcipher, key)) ?: 748 + return verify_skcipher_des3_key(skcipher, key) ?: 733 749 skcipher_setkey(skcipher, key, keylen, 0); 734 750 } 735 751 736 752 static int des_skcipher_setkey(struct crypto_skcipher *skcipher, 737 753 const u8 *key, unsigned int keylen) 738 754 { 739 - u32 tmp[DES_EXPKEY_WORDS]; 740 - 741 - if (!des_ekey(tmp, key) && (crypto_skcipher_get_flags(skcipher) & 742 - CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) { 743 - crypto_skcipher_set_flags(skcipher, 744 - CRYPTO_TFM_RES_WEAK_KEY); 745 - return -EINVAL; 746 - } 747 - 748 - return skcipher_setkey(skcipher, key, keylen, 0); 755 + return verify_skcipher_des_key(skcipher, key) ?: 756 + skcipher_setkey(skcipher, key, keylen, 0); 749 757 } 750 758 751 759 static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
+11 -25
drivers/crypto/caam/caamalg_qi2.c
··· 322 322 unsigned int keylen) 323 323 { 324 324 struct crypto_authenc_keys keys; 325 - u32 flags; 326 325 int err; 327 326 328 327 err = crypto_authenc_extractkeys(&keys, key, keylen); ··· 332 333 if (keys.enckeylen != DES3_EDE_KEY_SIZE) 333 334 goto badkey; 334 335 335 - flags = crypto_aead_get_flags(aead); 336 - err = __des3_verify_key(&flags, keys.enckey); 337 - if (unlikely(err)) { 338 - crypto_aead_set_flags(aead, flags); 339 - goto out; 340 - } 341 - 342 - err = aead_setkey(aead, key, keylen); 336 + err = crypto_des3_ede_verify_key(crypto_aead_tfm(aead), keys.enckey) ?: 337 + aead_setkey(aead, key, keylen); 343 338 344 339 out: 345 340 memzero_explicit(&keys, sizeof(keys)); ··· 1063 1070 static int des_skcipher_setkey(struct crypto_skcipher *skcipher, 1064 1071 const u8 *key, unsigned int keylen) 1065 1072 { 1066 - u32 tmp[DES3_EDE_EXPKEY_WORDS]; 1067 - struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher); 1073 + return verify_skcipher_des_key(skcipher, key) ?: 1074 + skcipher_setkey(skcipher, key, keylen, 0); 1075 + } 1068 1076 1069 - if (keylen == DES3_EDE_KEY_SIZE && 1070 - __des3_ede_setkey(tmp, &tfm->crt_flags, key, DES3_EDE_KEY_SIZE)) { 1071 - return -EINVAL; 1072 - } 1073 - 1074 - if (!des_ekey(tmp, key) && (crypto_skcipher_get_flags(skcipher) & 1075 - CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) { 1076 - crypto_skcipher_set_flags(skcipher, 1077 - CRYPTO_TFM_RES_WEAK_KEY); 1078 - return -EINVAL; 1079 - } 1080 - 1081 - return skcipher_setkey(skcipher, key, keylen, 0); 1077 + static int des3_skcipher_setkey(struct crypto_skcipher *skcipher, 1078 + const u8 *key, unsigned int keylen) 1079 + { 1080 + return verify_skcipher_des3_key(skcipher, key) ?: 1081 + skcipher_setkey(skcipher, key, keylen, 0); 1082 1082 } 1083 1083 1084 1084 static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, ··· 1620 1634 .cra_driver_name = "cbc-3des-caam-qi2", 1621 1635 .cra_blocksize = DES3_EDE_BLOCK_SIZE, 1622 1636 }, 1623 - .setkey = des_skcipher_setkey, 1637 + .setkey = des3_skcipher_setkey, 1624 1638 .encrypt = skcipher_encrypt, 1625 1639 .decrypt = skcipher_decrypt, 1626 1640 .min_keysize = DES3_EDE_KEY_SIZE,
+1 -1
drivers/crypto/caam/compat.h
··· 32 32 #include <crypto/null.h> 33 33 #include <crypto/aes.h> 34 34 #include <crypto/ctr.h> 35 - #include <crypto/des.h> 35 + #include <crypto/internal/des.h> 36 36 #include <crypto/gcm.h> 37 37 #include <crypto/sha.h> 38 38 #include <crypto/md5.h>