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