1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _CRYPTO_XTS_H 3 #define _CRYPTO_XTS_H 4 5 #include <crypto/b128ops.h> 6 #include <crypto/internal/skcipher.h> 7 #include <linux/fips.h> 8 9 #define XTS_BLOCK_SIZE 16 10 11 static inline int xts_check_key(struct crypto_tfm *tfm, 12 const u8 *key, unsigned int keylen) 13 { 14 u32 *flags = &tfm->crt_flags; 15 16 /* 17 * key consists of keys of equal size concatenated, therefore 18 * the length must be even. 19 */ 20 if (keylen % 2) { 21 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; 22 return -EINVAL; 23 } 24 25 /* ensure that the AES and tweak key are not identical */ 26 if (fips_enabled && 27 !crypto_memneq(key, key + (keylen / 2), keylen / 2)) { 28 *flags |= CRYPTO_TFM_RES_WEAK_KEY; 29 return -EINVAL; 30 } 31 32 return 0; 33 } 34 35 static inline int xts_verify_key(struct crypto_skcipher *tfm, 36 const u8 *key, unsigned int keylen) 37 { 38 /* 39 * key consists of keys of equal size concatenated, therefore 40 * the length must be even. 41 */ 42 if (keylen % 2) { 43 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); 44 return -EINVAL; 45 } 46 47 /* ensure that the AES and tweak key are not identical */ 48 if ((fips_enabled || (crypto_skcipher_get_flags(tfm) & 49 CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) && 50 !crypto_memneq(key, key + (keylen / 2), keylen / 2)) { 51 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY); 52 return -EINVAL; 53 } 54 55 return 0; 56 } 57 58 #endif /* _CRYPTO_XTS_H */ 59