xref: /kernel/linux/linux-5.10/fs/ubifs/crypto.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include "ubifs.h"
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_cistatic int ubifs_crypt_get_context(struct inode *inode, void *ctx, size_t len)
58c2ecf20Sopenharmony_ci{
68c2ecf20Sopenharmony_ci	return ubifs_xattr_get(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
78c2ecf20Sopenharmony_ci			       ctx, len);
88c2ecf20Sopenharmony_ci}
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic int ubifs_crypt_set_context(struct inode *inode, const void *ctx,
118c2ecf20Sopenharmony_ci				   size_t len, void *fs_data)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	/*
148c2ecf20Sopenharmony_ci	 * Creating an encryption context is done unlocked since we
158c2ecf20Sopenharmony_ci	 * operate on a new inode which is not visible to other users
168c2ecf20Sopenharmony_ci	 * at this point. So, no need to check whether inode is locked.
178c2ecf20Sopenharmony_ci	 */
188c2ecf20Sopenharmony_ci	return ubifs_xattr_set(inode, UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT,
198c2ecf20Sopenharmony_ci			       ctx, len, 0, false);
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic bool ubifs_crypt_empty_dir(struct inode *inode)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	return ubifs_check_dir_empty(inode) == 0;
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciint ubifs_encrypt(const struct inode *inode, struct ubifs_data_node *dn,
288c2ecf20Sopenharmony_ci		  unsigned int in_len, unsigned int *out_len, int block)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct ubifs_info *c = inode->i_sb->s_fs_info;
318c2ecf20Sopenharmony_ci	void *p = &dn->data;
328c2ecf20Sopenharmony_ci	unsigned int pad_len = round_up(in_len, UBIFS_CIPHER_BLOCK_SIZE);
338c2ecf20Sopenharmony_ci	int err;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	ubifs_assert(c, pad_len <= *out_len);
368c2ecf20Sopenharmony_ci	dn->compr_size = cpu_to_le16(in_len);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/* pad to full block cipher length */
398c2ecf20Sopenharmony_ci	if (pad_len != in_len)
408c2ecf20Sopenharmony_ci		memset(p + in_len, 0, pad_len - in_len);
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	err = fscrypt_encrypt_block_inplace(inode, virt_to_page(p), pad_len,
438c2ecf20Sopenharmony_ci					    offset_in_page(p), block, GFP_NOFS);
448c2ecf20Sopenharmony_ci	if (err) {
458c2ecf20Sopenharmony_ci		ubifs_err(c, "fscrypt_encrypt_block_inplace() failed: %d", err);
468c2ecf20Sopenharmony_ci		return err;
478c2ecf20Sopenharmony_ci	}
488c2ecf20Sopenharmony_ci	*out_len = pad_len;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	return 0;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciint ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn,
548c2ecf20Sopenharmony_ci		  unsigned int *out_len, int block)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct ubifs_info *c = inode->i_sb->s_fs_info;
578c2ecf20Sopenharmony_ci	int err;
588c2ecf20Sopenharmony_ci	unsigned int clen = le16_to_cpu(dn->compr_size);
598c2ecf20Sopenharmony_ci	unsigned int dlen = *out_len;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	if (clen <= 0 || clen > UBIFS_BLOCK_SIZE || clen > dlen) {
628c2ecf20Sopenharmony_ci		ubifs_err(c, "bad compr_size: %i", clen);
638c2ecf20Sopenharmony_ci		return -EINVAL;
648c2ecf20Sopenharmony_ci	}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	ubifs_assert(c, dlen <= UBIFS_BLOCK_SIZE);
678c2ecf20Sopenharmony_ci	err = fscrypt_decrypt_block_inplace(inode, virt_to_page(&dn->data),
688c2ecf20Sopenharmony_ci					    dlen, offset_in_page(&dn->data),
698c2ecf20Sopenharmony_ci					    block);
708c2ecf20Sopenharmony_ci	if (err) {
718c2ecf20Sopenharmony_ci		ubifs_err(c, "fscrypt_decrypt_block_inplace() failed: %d", err);
728c2ecf20Sopenharmony_ci		return err;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci	*out_len = clen;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ciconst struct fscrypt_operations ubifs_crypt_operations = {
808c2ecf20Sopenharmony_ci	.flags			= FS_CFLG_OWN_PAGES,
818c2ecf20Sopenharmony_ci	.key_prefix		= "ubifs:",
828c2ecf20Sopenharmony_ci	.get_context		= ubifs_crypt_get_context,
838c2ecf20Sopenharmony_ci	.set_context		= ubifs_crypt_set_context,
848c2ecf20Sopenharmony_ci	.empty_dir		= ubifs_crypt_empty_dir,
858c2ecf20Sopenharmony_ci	.max_namelen		= UBIFS_MAX_NLEN,
868c2ecf20Sopenharmony_ci};
87