crypto.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. return ubifs_xattr_set(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
  11. ctx, len, 0);
  12. }
  13. static bool ubifs_crypt_empty_dir(struct inode *inode)
  14. {
  15. return ubifs_check_dir_empty(inode) == 0;
  16. }
  17. static unsigned int ubifs_crypt_max_namelen(struct inode *inode)
  18. {
  19. if (S_ISLNK(inode->i_mode))
  20. return UBIFS_MAX_INO_DATA;
  21. else
  22. return UBIFS_MAX_NLEN;
  23. }
  24. static int ubifs_key_prefix(struct inode *inode, u8 **key)
  25. {
  26. static char prefix[] = "ubifs:";
  27. *key = prefix;
  28. return sizeof(prefix) - 1;
  29. }
  30. int ubifs_encrypt(const struct inode *inode, struct ubifs_data_node *dn,
  31. unsigned int in_len, unsigned int *out_len, int block)
  32. {
  33. struct ubifs_info *c = inode->i_sb->s_fs_info;
  34. void *p = &dn->data;
  35. struct page *ret;
  36. unsigned int pad_len = round_up(in_len, UBIFS_CIPHER_BLOCK_SIZE);
  37. ubifs_assert(pad_len <= *out_len);
  38. dn->compr_size = cpu_to_le16(in_len);
  39. /* pad to full block cipher length */
  40. if (pad_len != in_len)
  41. memset(p + in_len, 0, pad_len - in_len);
  42. ret = fscrypt_encrypt_page(inode, virt_to_page(&dn->data), pad_len,
  43. offset_in_page(&dn->data), block, GFP_NOFS);
  44. if (IS_ERR(ret)) {
  45. ubifs_err(c, "fscrypt_encrypt_page failed: %ld", PTR_ERR(ret));
  46. return PTR_ERR(ret);
  47. }
  48. *out_len = pad_len;
  49. return 0;
  50. }
  51. int ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn,
  52. unsigned int *out_len, int block)
  53. {
  54. struct ubifs_info *c = inode->i_sb->s_fs_info;
  55. int err;
  56. unsigned int clen = le16_to_cpu(dn->compr_size);
  57. unsigned int dlen = *out_len;
  58. if (clen <= 0 || clen > UBIFS_BLOCK_SIZE || clen > dlen) {
  59. ubifs_err(c, "bad compr_size: %i", clen);
  60. return -EINVAL;
  61. }
  62. ubifs_assert(dlen <= UBIFS_BLOCK_SIZE);
  63. err = fscrypt_decrypt_page(inode, virt_to_page(&dn->data), dlen,
  64. offset_in_page(&dn->data), block);
  65. if (err) {
  66. ubifs_err(c, "fscrypt_decrypt_page failed: %i", err);
  67. return err;
  68. }
  69. *out_len = clen;
  70. return 0;
  71. }
  72. struct fscrypt_operations ubifs_crypt_operations = {
  73. .flags = FS_CFLG_OWN_PAGES,
  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. .key_prefix = ubifs_key_prefix,
  80. };