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