xts.h 1.4 KB

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