18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _CRYPTO_XTS_H
38c2ecf20Sopenharmony_ci#define _CRYPTO_XTS_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <crypto/b128ops.h>
68c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h>
78c2ecf20Sopenharmony_ci#include <linux/fips.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#define XTS_BLOCK_SIZE 16
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic inline int xts_check_key(struct crypto_tfm *tfm,
128c2ecf20Sopenharmony_ci				const u8 *key, unsigned int keylen)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	/*
158c2ecf20Sopenharmony_ci	 * key consists of keys of equal size concatenated, therefore
168c2ecf20Sopenharmony_ci	 * the length must be even.
178c2ecf20Sopenharmony_ci	 */
188c2ecf20Sopenharmony_ci	if (keylen % 2)
198c2ecf20Sopenharmony_ci		return -EINVAL;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	/* ensure that the AES and tweak key are not identical */
228c2ecf20Sopenharmony_ci	if (fips_enabled && !crypto_memneq(key, key + (keylen / 2), keylen / 2))
238c2ecf20Sopenharmony_ci		return -EINVAL;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	return 0;
268c2ecf20Sopenharmony_ci}
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic inline int xts_verify_key(struct crypto_skcipher *tfm,
298c2ecf20Sopenharmony_ci				 const u8 *key, unsigned int keylen)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	/*
328c2ecf20Sopenharmony_ci	 * key consists of keys of equal size concatenated, therefore
338c2ecf20Sopenharmony_ci	 * the length must be even.
348c2ecf20Sopenharmony_ci	 */
358c2ecf20Sopenharmony_ci	if (keylen % 2)
368c2ecf20Sopenharmony_ci		return -EINVAL;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/* ensure that the AES and tweak key are not identical */
398c2ecf20Sopenharmony_ci	if ((fips_enabled || (crypto_skcipher_get_flags(tfm) &
408c2ecf20Sopenharmony_ci			      CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)) &&
418c2ecf20Sopenharmony_ci	    !crypto_memneq(key, key + (keylen / 2), keylen / 2))
428c2ecf20Sopenharmony_ci		return -EINVAL;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	return 0;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#endif  /* _CRYPTO_XTS_H */
48