xts.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. struct scatterlist;
  8. struct blkcipher_desc;
  9. #define XTS_BLOCK_SIZE 16
  10. struct xts_crypt_req {
  11. le128 *tbuf;
  12. unsigned int tbuflen;
  13. void *tweak_ctx;
  14. void (*tweak_fn)(void *ctx, u8* dst, const u8* src);
  15. void *crypt_ctx;
  16. void (*crypt_fn)(void *ctx, u8 *blks, unsigned int nbytes);
  17. };
  18. #define XTS_TWEAK_CAST(x) ((void (*)(void *, u8*, const u8*))(x))
  19. int xts_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  20. struct scatterlist *src, unsigned int nbytes,
  21. struct xts_crypt_req *req);
  22. static inline int xts_check_key(struct crypto_tfm *tfm,
  23. const u8 *key, unsigned int keylen)
  24. {
  25. u32 *flags = &tfm->crt_flags;
  26. /*
  27. * key consists of keys of equal size concatenated, therefore
  28. * the length must be even.
  29. */
  30. if (keylen % 2) {
  31. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  32. return -EINVAL;
  33. }
  34. /* ensure that the AES and tweak key are not identical */
  35. if (fips_enabled &&
  36. !crypto_memneq(key, key + (keylen / 2), keylen / 2)) {
  37. *flags |= CRYPTO_TFM_RES_WEAK_KEY;
  38. return -EINVAL;
  39. }
  40. return 0;
  41. }
  42. static inline int xts_verify_key(struct crypto_skcipher *tfm,
  43. const u8 *key, unsigned int keylen)
  44. {
  45. /*
  46. * key consists of keys of equal size concatenated, therefore
  47. * the length must be even.
  48. */
  49. if (keylen % 2) {
  50. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  51. return -EINVAL;
  52. }
  53. /* ensure that the AES and tweak key are not identical */
  54. if ((fips_enabled || crypto_skcipher_get_flags(tfm) &
  55. CRYPTO_TFM_REQ_WEAK_KEY) &&
  56. !crypto_memneq(key, key + (keylen / 2), keylen / 2)) {
  57. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
  58. return -EINVAL;
  59. }
  60. return 0;
  61. }
  62. #endif /* _CRYPTO_XTS_H */