1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * DES & Triple DES EDE key verification helpers
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #ifndef __CRYPTO_INTERNAL_DES_H
7*4882a593Smuzhiyun #define __CRYPTO_INTERNAL_DES_H
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/crypto.h>
10*4882a593Smuzhiyun #include <linux/fips.h>
11*4882a593Smuzhiyun #include <crypto/des.h>
12*4882a593Smuzhiyun #include <crypto/aead.h>
13*4882a593Smuzhiyun #include <crypto/skcipher.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /**
16*4882a593Smuzhiyun * crypto_des_verify_key - Check whether a DES key is weak
17*4882a593Smuzhiyun * @tfm: the crypto algo
18*4882a593Smuzhiyun * @key: the key buffer
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * Returns -EINVAL if the key is weak and the crypto TFM does not permit weak
21*4882a593Smuzhiyun * keys. Otherwise, 0 is returned.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * It is the job of the caller to ensure that the size of the key equals
24*4882a593Smuzhiyun * DES_KEY_SIZE.
25*4882a593Smuzhiyun */
crypto_des_verify_key(struct crypto_tfm * tfm,const u8 * key)26*4882a593Smuzhiyun static inline int crypto_des_verify_key(struct crypto_tfm *tfm, const u8 *key)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun struct des_ctx tmp;
29*4882a593Smuzhiyun int err;
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun err = des_expand_key(&tmp, key, DES_KEY_SIZE);
32*4882a593Smuzhiyun if (err == -ENOKEY) {
33*4882a593Smuzhiyun if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
34*4882a593Smuzhiyun err = -EINVAL;
35*4882a593Smuzhiyun else
36*4882a593Smuzhiyun err = 0;
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun memzero_explicit(&tmp, sizeof(tmp));
39*4882a593Smuzhiyun return err;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun * RFC2451:
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * For DES-EDE3, there is no known need to reject weak or
46*4882a593Smuzhiyun * complementation keys. Any weakness is obviated by the use of
47*4882a593Smuzhiyun * multiple keys.
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * However, if the first two or last two independent 64-bit keys are
50*4882a593Smuzhiyun * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the
51*4882a593Smuzhiyun * same as DES. Implementers MUST reject keys that exhibit this
52*4882a593Smuzhiyun * property.
53*4882a593Smuzhiyun *
54*4882a593Smuzhiyun */
des3_ede_verify_key(const u8 * key,unsigned int key_len,bool check_weak)55*4882a593Smuzhiyun static inline int des3_ede_verify_key(const u8 *key, unsigned int key_len,
56*4882a593Smuzhiyun bool check_weak)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun int ret = fips_enabled ? -EINVAL : -ENOKEY;
59*4882a593Smuzhiyun u32 K[6];
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun memcpy(K, key, DES3_EDE_KEY_SIZE);
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun if ((!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
64*4882a593Smuzhiyun !((K[2] ^ K[4]) | (K[3] ^ K[5]))) &&
65*4882a593Smuzhiyun (fips_enabled || check_weak))
66*4882a593Smuzhiyun goto bad;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun if ((!((K[0] ^ K[4]) | (K[1] ^ K[5]))) && fips_enabled)
69*4882a593Smuzhiyun goto bad;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun ret = 0;
72*4882a593Smuzhiyun bad:
73*4882a593Smuzhiyun memzero_explicit(K, DES3_EDE_KEY_SIZE);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun return ret;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /**
79*4882a593Smuzhiyun * crypto_des3_ede_verify_key - Check whether a DES3-EDE key is weak
80*4882a593Smuzhiyun * @tfm: the crypto algo
81*4882a593Smuzhiyun * @key: the key buffer
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * Returns -EINVAL if the key is weak and the crypto TFM does not permit weak
84*4882a593Smuzhiyun * keys or when running in FIPS mode. Otherwise, 0 is returned. Note that some
85*4882a593Smuzhiyun * keys are rejected in FIPS mode even if weak keys are permitted by the TFM
86*4882a593Smuzhiyun * flags.
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * It is the job of the caller to ensure that the size of the key equals
89*4882a593Smuzhiyun * DES3_EDE_KEY_SIZE.
90*4882a593Smuzhiyun */
crypto_des3_ede_verify_key(struct crypto_tfm * tfm,const u8 * key)91*4882a593Smuzhiyun static inline int crypto_des3_ede_verify_key(struct crypto_tfm *tfm,
92*4882a593Smuzhiyun const u8 *key)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun return des3_ede_verify_key(key, DES3_EDE_KEY_SIZE,
95*4882a593Smuzhiyun crypto_tfm_get_flags(tfm) &
96*4882a593Smuzhiyun CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
verify_skcipher_des_key(struct crypto_skcipher * tfm,const u8 * key)99*4882a593Smuzhiyun static inline int verify_skcipher_des_key(struct crypto_skcipher *tfm,
100*4882a593Smuzhiyun const u8 *key)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun return crypto_des_verify_key(crypto_skcipher_tfm(tfm), key);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
verify_skcipher_des3_key(struct crypto_skcipher * tfm,const u8 * key)105*4882a593Smuzhiyun static inline int verify_skcipher_des3_key(struct crypto_skcipher *tfm,
106*4882a593Smuzhiyun const u8 *key)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun return crypto_des3_ede_verify_key(crypto_skcipher_tfm(tfm), key);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
verify_aead_des_key(struct crypto_aead * tfm,const u8 * key,int keylen)111*4882a593Smuzhiyun static inline int verify_aead_des_key(struct crypto_aead *tfm, const u8 *key,
112*4882a593Smuzhiyun int keylen)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun if (keylen != DES_KEY_SIZE)
115*4882a593Smuzhiyun return -EINVAL;
116*4882a593Smuzhiyun return crypto_des_verify_key(crypto_aead_tfm(tfm), key);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
verify_aead_des3_key(struct crypto_aead * tfm,const u8 * key,int keylen)119*4882a593Smuzhiyun static inline int verify_aead_des3_key(struct crypto_aead *tfm, const u8 *key,
120*4882a593Smuzhiyun int keylen)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun if (keylen != DES3_EDE_KEY_SIZE)
123*4882a593Smuzhiyun return -EINVAL;
124*4882a593Smuzhiyun return crypto_des3_ede_verify_key(crypto_aead_tfm(tfm), key);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun #endif /* __CRYPTO_INTERNAL_DES_H */
128