xts.h 1.8 KB

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