hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/include/crypto/internal/des.h
....@@ -25,18 +25,17 @@
2525 */
2626 static inline int crypto_des_verify_key(struct crypto_tfm *tfm, const u8 *key)
2727 {
28
- u32 tmp[DES_EXPKEY_WORDS];
29
- int err = 0;
28
+ struct des_ctx tmp;
29
+ int err;
3030
31
- if (!(crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS))
32
- return 0;
33
-
34
- if (!des_ekey(tmp, key)) {
35
- crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
36
- err = -EINVAL;
31
+ err = des_expand_key(&tmp, key, DES_KEY_SIZE);
32
+ if (err == -ENOKEY) {
33
+ if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
34
+ err = -EINVAL;
35
+ else
36
+ err = 0;
3737 }
38
-
39
- memzero_explicit(tmp, sizeof(tmp));
38
+ memzero_explicit(&tmp, sizeof(tmp));
4039 return err;
4140 }
4241
....@@ -53,6 +52,28 @@
5352 * property.
5453 *
5554 */
55
+static inline int des3_ede_verify_key(const u8 *key, unsigned int key_len,
56
+ bool check_weak)
57
+{
58
+ int ret = fips_enabled ? -EINVAL : -ENOKEY;
59
+ u32 K[6];
60
+
61
+ memcpy(K, key, DES3_EDE_KEY_SIZE);
62
+
63
+ if ((!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
64
+ !((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
65
+ (fips_enabled || check_weak))
66
+ goto bad;
67
+
68
+ if ((!((K[0] ^ K[4]) | (K[1] ^ K[5]))) && fips_enabled)
69
+ goto bad;
70
+
71
+ ret = 0;
72
+bad:
73
+ memzero_explicit(K, DES3_EDE_KEY_SIZE);
74
+
75
+ return ret;
76
+}
5677
5778 /**
5879 * crypto_des3_ede_verify_key - Check whether a DES3-EDE key is weak
....@@ -70,28 +91,9 @@
7091 static inline int crypto_des3_ede_verify_key(struct crypto_tfm *tfm,
7192 const u8 *key)
7293 {
73
- int err = -EINVAL;
74
- u32 K[6];
75
-
76
- memcpy(K, key, DES3_EDE_KEY_SIZE);
77
-
78
- if ((!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
79
- !((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
80
- (fips_enabled || (crypto_tfm_get_flags(tfm) &
81
- CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)))
82
- goto bad;
83
-
84
- if ((!((K[0] ^ K[4]) | (K[1] ^ K[5]))) && fips_enabled)
85
- goto bad;
86
-
87
- err = 0;
88
-out:
89
- memzero_explicit(K, DES3_EDE_KEY_SIZE);
90
- return err;
91
-
92
-bad:
93
- crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
94
- goto out;
94
+ return des3_ede_verify_key(key, DES3_EDE_KEY_SIZE,
95
+ crypto_tfm_get_flags(tfm) &
96
+ CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
9597 }
9698
9799 static inline int verify_skcipher_des_key(struct crypto_skcipher *tfm,
....@@ -106,35 +108,19 @@
106108 return crypto_des3_ede_verify_key(crypto_skcipher_tfm(tfm), key);
107109 }
108110
109
-static inline int verify_ablkcipher_des_key(struct crypto_ablkcipher *tfm,
110
- const u8 *key)
111
-{
112
- return crypto_des_verify_key(crypto_ablkcipher_tfm(tfm), key);
113
-}
114
-
115
-static inline int verify_ablkcipher_des3_key(struct crypto_ablkcipher *tfm,
116
- const u8 *key)
117
-{
118
- return crypto_des3_ede_verify_key(crypto_ablkcipher_tfm(tfm), key);
119
-}
120
-
121111 static inline int verify_aead_des_key(struct crypto_aead *tfm, const u8 *key,
122112 int keylen)
123113 {
124
- if (keylen != DES_KEY_SIZE) {
125
- crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
114
+ if (keylen != DES_KEY_SIZE)
126115 return -EINVAL;
127
- }
128116 return crypto_des_verify_key(crypto_aead_tfm(tfm), key);
129117 }
130118
131119 static inline int verify_aead_des3_key(struct crypto_aead *tfm, const u8 *key,
132120 int keylen)
133121 {
134
- if (keylen != DES3_EDE_KEY_SIZE) {
135
- crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
122
+ if (keylen != DES3_EDE_KEY_SIZE)
136123 return -EINVAL;
137
- }
138124 return crypto_des3_ede_verify_key(crypto_aead_tfm(tfm), key);
139125 }
140126