crypto.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "ubifs.h"
  3. static int ubifs_crypt_get_context(struct inode *inode, void *ctx, size_t len)
  4. {
  5. return ubifs_xattr_get(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
  6. ctx, len);
  7. }
  8. static int ubifs_crypt_set_context(struct inode *inode, const void *ctx,
  9. size_t len, void *fs_data)
  10. {
  11. /*
  12. * Creating an encryption context is done unlocked since we
  13. * operate on a new inode which is not visible to other users
  14. * at this point. So, no need to check whether inode is locked.
  15. */
  16. return ubifs_xattr_set(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
  17. ctx, len, 0, false);
  18. }
  19. static bool ubifs_crypt_empty_dir(struct inode *inode)
  20. {
  21. return ubifs_check_dir_empty(inode) == 0;
  22. }
  23. /**
  24. * ubifs_encrypt - Encrypt data.
  25. * @inode: inode which refers to the data node
  26. * @dn: data node to encrypt
  27. * @in_len: length of data to be compressed
  28. * @out_len: allocated memory size for the data area of @dn
  29. * @block: logical block number of the block
  30. *
  31. * This function encrypt a possibly-compressed data in the data node.
  32. * The encrypted data length will store in @out_len.
  33. */
  34. int ubifs_encrypt(const struct inode *inode, struct ubifs_data_node *dn,
  35. unsigned int in_len, unsigned int *out_len, int block)
  36. {
  37. struct ubifs_info *c = inode->i_sb->s_fs_info;
  38. void *p = &dn->data;
  39. unsigned int pad_len = round_up(in_len, UBIFS_CIPHER_BLOCK_SIZE);
  40. int err;
  41. ubifs_assert(c, pad_len <= *out_len);
  42. dn->compr_size = cpu_to_le16(in_len);
  43. /* pad to full block cipher length */
  44. if (pad_len != in_len)
  45. memset(p + in_len, 0, pad_len - in_len);
  46. err = fscrypt_encrypt_block_inplace(inode, virt_to_page(p), pad_len,
  47. offset_in_page(p), block);
  48. if (err) {
  49. ubifs_err(c, "fscrypt_encrypt_block_inplace() failed: %d", err);
  50. return err;
  51. }
  52. *out_len = pad_len;
  53. return 0;
  54. }
  55. int ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn,
  56. unsigned int *out_len, int block)
  57. {
  58. struct ubifs_info *c = inode->i_sb->s_fs_info;
  59. int err;
  60. unsigned int clen = le16_to_cpu(dn->compr_size);
  61. unsigned int dlen = *out_len;
  62. if (clen <= 0 || clen > UBIFS_BLOCK_SIZE || clen > dlen) {
  63. ubifs_err(c, "bad compr_size: %i", clen);
  64. return -EINVAL;
  65. }
  66. ubifs_assert(c, dlen <= UBIFS_BLOCK_SIZE);
  67. err = fscrypt_decrypt_block_inplace(inode, virt_to_page(&dn->data),
  68. dlen, offset_in_page(&dn->data),
  69. block);
  70. if (err) {
  71. ubifs_err(c, "fscrypt_decrypt_block_inplace() failed: %d", err);
  72. return err;
  73. }
  74. *out_len = clen;
  75. return 0;
  76. }
  77. const struct fscrypt_operations ubifs_crypt_operations = {
  78. .inode_info_offs = (int)offsetof(struct ubifs_inode, i_crypt_info) -
  79. (int)offsetof(struct ubifs_inode, vfs_inode),
  80. .legacy_key_prefix = "ubifs:",
  81. .get_context = ubifs_crypt_get_context,
  82. .set_context = ubifs_crypt_set_context,
  83. .empty_dir = ubifs_crypt_empty_dir,
  84. };