0001
0002 #include "ubifs.h"
0003
0004 static int ubifs_crypt_get_context(struct inode *inode, void *ctx, size_t len)
0005 {
0006 return ubifs_xattr_get(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
0007 ctx, len);
0008 }
0009
0010 static int ubifs_crypt_set_context(struct inode *inode, const void *ctx,
0011 size_t len, void *fs_data)
0012 {
0013
0014
0015
0016
0017
0018 return ubifs_xattr_set(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
0019 ctx, len, 0, false);
0020 }
0021
0022 static bool ubifs_crypt_empty_dir(struct inode *inode)
0023 {
0024 return ubifs_check_dir_empty(inode) == 0;
0025 }
0026
0027 int ubifs_encrypt(const struct inode *inode, struct ubifs_data_node *dn,
0028 unsigned int in_len, unsigned int *out_len, int block)
0029 {
0030 struct ubifs_info *c = inode->i_sb->s_fs_info;
0031 void *p = &dn->data;
0032 unsigned int pad_len = round_up(in_len, UBIFS_CIPHER_BLOCK_SIZE);
0033 int err;
0034
0035 ubifs_assert(c, pad_len <= *out_len);
0036 dn->compr_size = cpu_to_le16(in_len);
0037
0038
0039 if (pad_len != in_len)
0040 memset(p + in_len, 0, pad_len - in_len);
0041
0042 err = fscrypt_encrypt_block_inplace(inode, virt_to_page(p), pad_len,
0043 offset_in_page(p), block, GFP_NOFS);
0044 if (err) {
0045 ubifs_err(c, "fscrypt_encrypt_block_inplace() failed: %d", err);
0046 return err;
0047 }
0048 *out_len = pad_len;
0049
0050 return 0;
0051 }
0052
0053 int ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn,
0054 unsigned int *out_len, int block)
0055 {
0056 struct ubifs_info *c = inode->i_sb->s_fs_info;
0057 int err;
0058 unsigned int clen = le16_to_cpu(dn->compr_size);
0059 unsigned int dlen = *out_len;
0060
0061 if (clen <= 0 || clen > UBIFS_BLOCK_SIZE || clen > dlen) {
0062 ubifs_err(c, "bad compr_size: %i", clen);
0063 return -EINVAL;
0064 }
0065
0066 ubifs_assert(c, dlen <= UBIFS_BLOCK_SIZE);
0067 err = fscrypt_decrypt_block_inplace(inode, virt_to_page(&dn->data),
0068 dlen, offset_in_page(&dn->data),
0069 block);
0070 if (err) {
0071 ubifs_err(c, "fscrypt_decrypt_block_inplace() failed: %d", err);
0072 return err;
0073 }
0074 *out_len = clen;
0075
0076 return 0;
0077 }
0078
0079 const struct fscrypt_operations ubifs_crypt_operations = {
0080 .flags = FS_CFLG_OWN_PAGES,
0081 .key_prefix = "ubifs:",
0082 .get_context = ubifs_crypt_get_context,
0083 .set_context = ubifs_crypt_set_context,
0084 .empty_dir = ubifs_crypt_empty_dir,
0085 };