crypto.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "ubifs.h"
  2. static int ubifs_crypt_get_context(struct inode *inode, void *ctx, size_t len)
  3. {
  4. return ubifs_xattr_get(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
  5. ctx, len);
  6. }
  7. static int ubifs_crypt_set_context(struct inode *inode, const void *ctx,
  8. size_t len, void *fs_data)
  9. {
  10. /*
  11. * Creating an encryption context is done unlocked since we
  12. * operate on a new inode which is not visible to other users
  13. * at this point. So, no need to check whether inode is locked.
  14. */
  15. return ubifs_xattr_set(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
  16. ctx, len, 0, false);
  17. }
  18. static bool ubifs_crypt_empty_dir(struct inode *inode)
  19. {
  20. return ubifs_check_dir_empty(inode) == 0;
  21. }
  22. static unsigned int ubifs_crypt_max_namelen(struct inode *inode)
  23. {
  24. if (S_ISLNK(inode->i_mode))
  25. return UBIFS_MAX_INO_DATA;
  26. else
  27. return UBIFS_MAX_NLEN;
  28. }
  29. int ubifs_encrypt(const struct inode *inode, struct ubifs_data_node *dn,
  30. unsigned int in_len, unsigned int *out_len, int block)
  31. {
  32. struct ubifs_info *c = inode->i_sb->s_fs_info;
  33. void *p = &dn->data;
  34. struct page *ret;
  35. unsigned int pad_len = round_up(in_len, UBIFS_CIPHER_BLOCK_SIZE);
  36. ubifs_assert(pad_len <= *out_len);
  37. dn->compr_size = cpu_to_le16(in_len);
  38. /* pad to full block cipher length */
  39. if (pad_len != in_len)
  40. memset(p + in_len, 0, pad_len - in_len);
  41. ret = fscrypt_encrypt_page(inode, virt_to_page(&dn->data), pad_len,
  42. offset_in_page(&dn->data), block, GFP_NOFS);
  43. if (IS_ERR(ret)) {
  44. ubifs_err(c, "fscrypt_encrypt_page failed: %ld", PTR_ERR(ret));
  45. return PTR_ERR(ret);
  46. }
  47. *out_len = pad_len;
  48. return 0;
  49. }
  50. int ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn,
  51. unsigned int *out_len, int block)
  52. {
  53. struct ubifs_info *c = inode->i_sb->s_fs_info;
  54. int err;
  55. unsigned int clen = le16_to_cpu(dn->compr_size);
  56. unsigned int dlen = *out_len;
  57. if (clen <= 0 || clen > UBIFS_BLOCK_SIZE || clen > dlen) {
  58. ubifs_err(c, "bad compr_size: %i", clen);
  59. return -EINVAL;
  60. }
  61. ubifs_assert(dlen <= UBIFS_BLOCK_SIZE);
  62. err = fscrypt_decrypt_page(inode, virt_to_page(&dn->data), dlen,
  63. offset_in_page(&dn->data), block);
  64. if (err) {
  65. ubifs_err(c, "fscrypt_decrypt_page failed: %i", err);
  66. return err;
  67. }
  68. *out_len = clen;
  69. return 0;
  70. }
  71. const struct fscrypt_operations ubifs_crypt_operations = {
  72. .flags = FS_CFLG_OWN_PAGES,
  73. .key_prefix = "ubifs:",
  74. .get_context = ubifs_crypt_get_context,
  75. .set_context = ubifs_crypt_set_context,
  76. .is_encrypted = __ubifs_crypt_is_encrypted,
  77. .empty_dir = ubifs_crypt_empty_dir,
  78. .max_namelen = ubifs_crypt_max_namelen,
  79. };