fscrypt_ice.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Copyright (c) 2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include "fscrypt_ice.h"
  13. int fscrypt_using_hardware_encryption(const struct inode *inode)
  14. {
  15. struct fscrypt_info *ci = inode->i_crypt_info;
  16. return ci && is_private_mode(ci->ci_mode);
  17. }
  18. EXPORT_SYMBOL(fscrypt_using_hardware_encryption);
  19. /*
  20. * Retrieves encryption key from the inode
  21. */
  22. char *fscrypt_get_ice_encryption_key(const struct inode *inode)
  23. {
  24. struct fscrypt_info *ci = NULL;
  25. if (!inode)
  26. return NULL;
  27. ci = inode->i_crypt_info;
  28. if (!ci)
  29. return NULL;
  30. return &(ci->ci_raw_key[0]);
  31. }
  32. /*
  33. * Retrieves encryption salt from the inode
  34. */
  35. char *fscrypt_get_ice_encryption_salt(const struct inode *inode)
  36. {
  37. struct fscrypt_info *ci = NULL;
  38. if (!inode)
  39. return NULL;
  40. ci = inode->i_crypt_info;
  41. if (!ci)
  42. return NULL;
  43. return &(ci->ci_raw_key[fscrypt_get_ice_encryption_key_size(inode)]);
  44. }
  45. /*
  46. * returns true if the cipher mode in inode is AES XTS
  47. */
  48. int fscrypt_is_aes_xts_cipher(const struct inode *inode)
  49. {
  50. struct fscrypt_info *ci = inode->i_crypt_info;
  51. if (!ci)
  52. return 0;
  53. return (ci->ci_data_mode == FS_ENCRYPTION_MODE_PRIVATE);
  54. }
  55. /*
  56. * returns true if encryption info in both inodes is equal
  57. */
  58. bool fscrypt_is_ice_encryption_info_equal(const struct inode *inode1,
  59. const struct inode *inode2)
  60. {
  61. char *key1 = NULL;
  62. char *key2 = NULL;
  63. char *salt1 = NULL;
  64. char *salt2 = NULL;
  65. if (!inode1 || !inode2)
  66. return false;
  67. if (inode1 == inode2)
  68. return true;
  69. /* both do not belong to ice, so we don't care, they are equal for us */
  70. if (!fscrypt_should_be_processed_by_ice(inode1) &&
  71. !fscrypt_should_be_processed_by_ice(inode2))
  72. return true;
  73. /* one belongs to ice, the other does not -> not equal */
  74. if (fscrypt_should_be_processed_by_ice(inode1) ^
  75. fscrypt_should_be_processed_by_ice(inode2))
  76. return false;
  77. key1 = fscrypt_get_ice_encryption_key(inode1);
  78. key2 = fscrypt_get_ice_encryption_key(inode2);
  79. salt1 = fscrypt_get_ice_encryption_salt(inode1);
  80. salt2 = fscrypt_get_ice_encryption_salt(inode2);
  81. /* key and salt should not be null by this point */
  82. if (!key1 || !key2 || !salt1 || !salt2 ||
  83. (fscrypt_get_ice_encryption_key_size(inode1) !=
  84. fscrypt_get_ice_encryption_key_size(inode2)) ||
  85. (fscrypt_get_ice_encryption_salt_size(inode1) !=
  86. fscrypt_get_ice_encryption_salt_size(inode2)))
  87. return false;
  88. if ((memcmp(key1, key2,
  89. fscrypt_get_ice_encryption_key_size(inode1)) == 0) &&
  90. (memcmp(salt1, salt2,
  91. fscrypt_get_ice_encryption_salt_size(inode1)) == 0))
  92. return true;
  93. return false;
  94. }
  95. void fscrypt_set_ice_dun(const struct inode *inode, struct bio *bio, u64 dun)
  96. {
  97. if (fscrypt_should_be_processed_by_ice(inode))
  98. bio->bi_iter.bi_dun = dun;
  99. }
  100. EXPORT_SYMBOL(fscrypt_set_ice_dun);
  101. void fscrypt_set_ice_skip(struct bio *bio, int bi_crypt_skip)
  102. {
  103. #ifdef CONFIG_DM_DEFAULT_KEY
  104. bio->bi_crypt_skip = bi_crypt_skip;
  105. #endif
  106. }
  107. EXPORT_SYMBOL(fscrypt_set_ice_skip);
  108. /*
  109. * This function will be used for filesystem when deciding to merge bios.
  110. * Basic assumption is, if inline_encryption is set, single bio has to
  111. * guarantee consecutive LBAs as well as ino|pg->index.
  112. */
  113. bool fscrypt_mergeable_bio(struct bio *bio, u64 dun, bool bio_encrypted,
  114. int bi_crypt_skip)
  115. {
  116. if (!bio)
  117. return true;
  118. #ifdef CONFIG_DM_DEFAULT_KEY
  119. if (bi_crypt_skip != bio->bi_crypt_skip)
  120. return false;
  121. #endif
  122. /* if both of them are not encrypted, no further check is needed */
  123. if (!bio_dun(bio) && !bio_encrypted)
  124. return true;
  125. /* ICE allows only consecutive iv_key stream. */
  126. return bio_end_dun(bio) == dun;
  127. }
  128. EXPORT_SYMBOL(fscrypt_mergeable_bio);