18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * eCryptfs: Linux filesystem encryption layer
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 1997-2004 Erez Zadok
68c2ecf20Sopenharmony_ci * Copyright (C) 2001-2004 Stony Brook University
78c2ecf20Sopenharmony_ci * Copyright (C) 2004-2007 International Business Machines Corp.
88c2ecf20Sopenharmony_ci *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
98c2ecf20Sopenharmony_ci *   		Michael C. Thompson <mcthomps@us.ibm.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <crypto/hash.h>
138c2ecf20Sopenharmony_ci#include <crypto/skcipher.h>
148c2ecf20Sopenharmony_ci#include <linux/fs.h>
158c2ecf20Sopenharmony_ci#include <linux/mount.h>
168c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
178c2ecf20Sopenharmony_ci#include <linux/random.h>
188c2ecf20Sopenharmony_ci#include <linux/compiler.h>
198c2ecf20Sopenharmony_ci#include <linux/key.h>
208c2ecf20Sopenharmony_ci#include <linux/namei.h>
218c2ecf20Sopenharmony_ci#include <linux/file.h>
228c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
238c2ecf20Sopenharmony_ci#include <linux/slab.h>
248c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
258c2ecf20Sopenharmony_ci#include <linux/kernel.h>
268c2ecf20Sopenharmony_ci#include <linux/xattr.h>
278c2ecf20Sopenharmony_ci#include "ecryptfs_kernel.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define DECRYPT		0
308c2ecf20Sopenharmony_ci#define ENCRYPT		1
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/**
338c2ecf20Sopenharmony_ci * ecryptfs_from_hex
348c2ecf20Sopenharmony_ci * @dst: Buffer to take the bytes from src hex; must be at least of
358c2ecf20Sopenharmony_ci *       size (src_size / 2)
368c2ecf20Sopenharmony_ci * @src: Buffer to be converted from a hex string representation to raw value
378c2ecf20Sopenharmony_ci * @dst_size: size of dst buffer, or number of hex characters pairs to convert
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_civoid ecryptfs_from_hex(char *dst, char *src, int dst_size)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	int x;
428c2ecf20Sopenharmony_ci	char tmp[3] = { 0, };
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	for (x = 0; x < dst_size; x++) {
458c2ecf20Sopenharmony_ci		tmp[0] = src[x * 2];
468c2ecf20Sopenharmony_ci		tmp[1] = src[x * 2 + 1];
478c2ecf20Sopenharmony_ci		dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
488c2ecf20Sopenharmony_ci	}
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/**
528c2ecf20Sopenharmony_ci * ecryptfs_calculate_md5 - calculates the md5 of @src
538c2ecf20Sopenharmony_ci * @dst: Pointer to 16 bytes of allocated memory
548c2ecf20Sopenharmony_ci * @crypt_stat: Pointer to crypt_stat struct for the current inode
558c2ecf20Sopenharmony_ci * @src: Data to be md5'd
568c2ecf20Sopenharmony_ci * @len: Length of @src
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * Uses the allocated crypto context that crypt_stat references to
598c2ecf20Sopenharmony_ci * generate the MD5 sum of the contents of src.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_cistatic int ecryptfs_calculate_md5(char *dst,
628c2ecf20Sopenharmony_ci				  struct ecryptfs_crypt_stat *crypt_stat,
638c2ecf20Sopenharmony_ci				  char *src, int len)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	int rc = crypto_shash_tfm_digest(crypt_stat->hash_tfm, src, len, dst);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	if (rc) {
688c2ecf20Sopenharmony_ci		printk(KERN_ERR
698c2ecf20Sopenharmony_ci		       "%s: Error computing crypto hash; rc = [%d]\n",
708c2ecf20Sopenharmony_ci		       __func__, rc);
718c2ecf20Sopenharmony_ci		goto out;
728c2ecf20Sopenharmony_ci	}
738c2ecf20Sopenharmony_ciout:
748c2ecf20Sopenharmony_ci	return rc;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
788c2ecf20Sopenharmony_ci						  char *cipher_name,
798c2ecf20Sopenharmony_ci						  char *chaining_modifier)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	int cipher_name_len = strlen(cipher_name);
828c2ecf20Sopenharmony_ci	int chaining_modifier_len = strlen(chaining_modifier);
838c2ecf20Sopenharmony_ci	int algified_name_len;
848c2ecf20Sopenharmony_ci	int rc;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
878c2ecf20Sopenharmony_ci	(*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
888c2ecf20Sopenharmony_ci	if (!(*algified_name)) {
898c2ecf20Sopenharmony_ci		rc = -ENOMEM;
908c2ecf20Sopenharmony_ci		goto out;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci	snprintf((*algified_name), algified_name_len, "%s(%s)",
938c2ecf20Sopenharmony_ci		 chaining_modifier, cipher_name);
948c2ecf20Sopenharmony_ci	rc = 0;
958c2ecf20Sopenharmony_ciout:
968c2ecf20Sopenharmony_ci	return rc;
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/**
1008c2ecf20Sopenharmony_ci * ecryptfs_derive_iv
1018c2ecf20Sopenharmony_ci * @iv: destination for the derived iv vale
1028c2ecf20Sopenharmony_ci * @crypt_stat: Pointer to crypt_stat struct for the current inode
1038c2ecf20Sopenharmony_ci * @offset: Offset of the extent whose IV we are to derive
1048c2ecf20Sopenharmony_ci *
1058c2ecf20Sopenharmony_ci * Generate the initialization vector from the given root IV and page
1068c2ecf20Sopenharmony_ci * offset.
1078c2ecf20Sopenharmony_ci *
1088c2ecf20Sopenharmony_ci * Returns zero on success; non-zero on error.
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_ciint ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
1118c2ecf20Sopenharmony_ci		       loff_t offset)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	int rc = 0;
1148c2ecf20Sopenharmony_ci	char dst[MD5_DIGEST_SIZE];
1158c2ecf20Sopenharmony_ci	char src[ECRYPTFS_MAX_IV_BYTES + 16];
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (unlikely(ecryptfs_verbosity > 0)) {
1188c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_DEBUG, "root iv:\n");
1198c2ecf20Sopenharmony_ci		ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci	/* TODO: It is probably secure to just cast the least
1228c2ecf20Sopenharmony_ci	 * significant bits of the root IV into an unsigned long and
1238c2ecf20Sopenharmony_ci	 * add the offset to that rather than go through all this
1248c2ecf20Sopenharmony_ci	 * hashing business. -Halcrow */
1258c2ecf20Sopenharmony_ci	memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
1268c2ecf20Sopenharmony_ci	memset((src + crypt_stat->iv_bytes), 0, 16);
1278c2ecf20Sopenharmony_ci	snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
1288c2ecf20Sopenharmony_ci	if (unlikely(ecryptfs_verbosity > 0)) {
1298c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_DEBUG, "source:\n");
1308c2ecf20Sopenharmony_ci		ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci	rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
1338c2ecf20Sopenharmony_ci				    (crypt_stat->iv_bytes + 16));
1348c2ecf20Sopenharmony_ci	if (rc) {
1358c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
1368c2ecf20Sopenharmony_ci				"MD5 while generating IV for a page\n");
1378c2ecf20Sopenharmony_ci		goto out;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci	memcpy(iv, dst, crypt_stat->iv_bytes);
1408c2ecf20Sopenharmony_ci	if (unlikely(ecryptfs_verbosity > 0)) {
1418c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
1428c2ecf20Sopenharmony_ci		ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ciout:
1458c2ecf20Sopenharmony_ci	return rc;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci/**
1498c2ecf20Sopenharmony_ci * ecryptfs_init_crypt_stat
1508c2ecf20Sopenharmony_ci * @crypt_stat: Pointer to the crypt_stat struct to initialize.
1518c2ecf20Sopenharmony_ci *
1528c2ecf20Sopenharmony_ci * Initialize the crypt_stat structure.
1538c2ecf20Sopenharmony_ci */
1548c2ecf20Sopenharmony_ciint ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	struct crypto_shash *tfm;
1578c2ecf20Sopenharmony_ci	int rc;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0);
1608c2ecf20Sopenharmony_ci	if (IS_ERR(tfm)) {
1618c2ecf20Sopenharmony_ci		rc = PTR_ERR(tfm);
1628c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_ERR, "Error attempting to "
1638c2ecf20Sopenharmony_ci				"allocate crypto context; rc = [%d]\n",
1648c2ecf20Sopenharmony_ci				rc);
1658c2ecf20Sopenharmony_ci		return rc;
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
1698c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&crypt_stat->keysig_list);
1708c2ecf20Sopenharmony_ci	mutex_init(&crypt_stat->keysig_list_mutex);
1718c2ecf20Sopenharmony_ci	mutex_init(&crypt_stat->cs_mutex);
1728c2ecf20Sopenharmony_ci	mutex_init(&crypt_stat->cs_tfm_mutex);
1738c2ecf20Sopenharmony_ci	crypt_stat->hash_tfm = tfm;
1748c2ecf20Sopenharmony_ci	crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	return 0;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/**
1808c2ecf20Sopenharmony_ci * ecryptfs_destroy_crypt_stat
1818c2ecf20Sopenharmony_ci * @crypt_stat: Pointer to the crypt_stat struct to initialize.
1828c2ecf20Sopenharmony_ci *
1838c2ecf20Sopenharmony_ci * Releases all memory associated with a crypt_stat struct.
1848c2ecf20Sopenharmony_ci */
1858c2ecf20Sopenharmony_civoid ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	crypto_free_skcipher(crypt_stat->tfm);
1908c2ecf20Sopenharmony_ci	crypto_free_shash(crypt_stat->hash_tfm);
1918c2ecf20Sopenharmony_ci	list_for_each_entry_safe(key_sig, key_sig_tmp,
1928c2ecf20Sopenharmony_ci				 &crypt_stat->keysig_list, crypt_stat_list) {
1938c2ecf20Sopenharmony_ci		list_del(&key_sig->crypt_stat_list);
1948c2ecf20Sopenharmony_ci		kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci	memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_civoid ecryptfs_destroy_mount_crypt_stat(
2008c2ecf20Sopenharmony_ci	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
2058c2ecf20Sopenharmony_ci		return;
2068c2ecf20Sopenharmony_ci	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
2078c2ecf20Sopenharmony_ci	list_for_each_entry_safe(auth_tok, auth_tok_tmp,
2088c2ecf20Sopenharmony_ci				 &mount_crypt_stat->global_auth_tok_list,
2098c2ecf20Sopenharmony_ci				 mount_crypt_stat_list) {
2108c2ecf20Sopenharmony_ci		list_del(&auth_tok->mount_crypt_stat_list);
2118c2ecf20Sopenharmony_ci		if (!(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
2128c2ecf20Sopenharmony_ci			key_put(auth_tok->global_auth_tok_key);
2138c2ecf20Sopenharmony_ci		kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
2168c2ecf20Sopenharmony_ci	memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci/**
2208c2ecf20Sopenharmony_ci * virt_to_scatterlist
2218c2ecf20Sopenharmony_ci * @addr: Virtual address
2228c2ecf20Sopenharmony_ci * @size: Size of data; should be an even multiple of the block size
2238c2ecf20Sopenharmony_ci * @sg: Pointer to scatterlist array; set to NULL to obtain only
2248c2ecf20Sopenharmony_ci *      the number of scatterlist structs required in array
2258c2ecf20Sopenharmony_ci * @sg_size: Max array size
2268c2ecf20Sopenharmony_ci *
2278c2ecf20Sopenharmony_ci * Fills in a scatterlist array with page references for a passed
2288c2ecf20Sopenharmony_ci * virtual address.
2298c2ecf20Sopenharmony_ci *
2308c2ecf20Sopenharmony_ci * Returns the number of scatterlist structs in array used
2318c2ecf20Sopenharmony_ci */
2328c2ecf20Sopenharmony_ciint virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
2338c2ecf20Sopenharmony_ci			int sg_size)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	int i = 0;
2368c2ecf20Sopenharmony_ci	struct page *pg;
2378c2ecf20Sopenharmony_ci	int offset;
2388c2ecf20Sopenharmony_ci	int remainder_of_page;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	sg_init_table(sg, sg_size);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	while (size > 0 && i < sg_size) {
2438c2ecf20Sopenharmony_ci		pg = virt_to_page(addr);
2448c2ecf20Sopenharmony_ci		offset = offset_in_page(addr);
2458c2ecf20Sopenharmony_ci		sg_set_page(&sg[i], pg, 0, offset);
2468c2ecf20Sopenharmony_ci		remainder_of_page = PAGE_SIZE - offset;
2478c2ecf20Sopenharmony_ci		if (size >= remainder_of_page) {
2488c2ecf20Sopenharmony_ci			sg[i].length = remainder_of_page;
2498c2ecf20Sopenharmony_ci			addr += remainder_of_page;
2508c2ecf20Sopenharmony_ci			size -= remainder_of_page;
2518c2ecf20Sopenharmony_ci		} else {
2528c2ecf20Sopenharmony_ci			sg[i].length = size;
2538c2ecf20Sopenharmony_ci			addr += size;
2548c2ecf20Sopenharmony_ci			size = 0;
2558c2ecf20Sopenharmony_ci		}
2568c2ecf20Sopenharmony_ci		i++;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci	if (size > 0)
2598c2ecf20Sopenharmony_ci		return -ENOMEM;
2608c2ecf20Sopenharmony_ci	return i;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistruct extent_crypt_result {
2648c2ecf20Sopenharmony_ci	struct completion completion;
2658c2ecf20Sopenharmony_ci	int rc;
2668c2ecf20Sopenharmony_ci};
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic void extent_crypt_complete(struct crypto_async_request *req, int rc)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	struct extent_crypt_result *ecr = req->data;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	if (rc == -EINPROGRESS)
2738c2ecf20Sopenharmony_ci		return;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	ecr->rc = rc;
2768c2ecf20Sopenharmony_ci	complete(&ecr->completion);
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci/**
2808c2ecf20Sopenharmony_ci * crypt_scatterlist
2818c2ecf20Sopenharmony_ci * @crypt_stat: Pointer to the crypt_stat struct to initialize.
2828c2ecf20Sopenharmony_ci * @dst_sg: Destination of the data after performing the crypto operation
2838c2ecf20Sopenharmony_ci * @src_sg: Data to be encrypted or decrypted
2848c2ecf20Sopenharmony_ci * @size: Length of data
2858c2ecf20Sopenharmony_ci * @iv: IV to use
2868c2ecf20Sopenharmony_ci * @op: ENCRYPT or DECRYPT to indicate the desired operation
2878c2ecf20Sopenharmony_ci *
2888c2ecf20Sopenharmony_ci * Returns the number of bytes encrypted or decrypted; negative value on error
2898c2ecf20Sopenharmony_ci */
2908c2ecf20Sopenharmony_cistatic int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
2918c2ecf20Sopenharmony_ci			     struct scatterlist *dst_sg,
2928c2ecf20Sopenharmony_ci			     struct scatterlist *src_sg, int size,
2938c2ecf20Sopenharmony_ci			     unsigned char *iv, int op)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	struct skcipher_request *req = NULL;
2968c2ecf20Sopenharmony_ci	struct extent_crypt_result ecr;
2978c2ecf20Sopenharmony_ci	int rc = 0;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	BUG_ON(!crypt_stat || !crypt_stat->tfm
3008c2ecf20Sopenharmony_ci	       || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
3018c2ecf20Sopenharmony_ci	if (unlikely(ecryptfs_verbosity > 0)) {
3028c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
3038c2ecf20Sopenharmony_ci				crypt_stat->key_size);
3048c2ecf20Sopenharmony_ci		ecryptfs_dump_hex(crypt_stat->key,
3058c2ecf20Sopenharmony_ci				  crypt_stat->key_size);
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	init_completion(&ecr.completion);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	mutex_lock(&crypt_stat->cs_tfm_mutex);
3118c2ecf20Sopenharmony_ci	req = skcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
3128c2ecf20Sopenharmony_ci	if (!req) {
3138c2ecf20Sopenharmony_ci		mutex_unlock(&crypt_stat->cs_tfm_mutex);
3148c2ecf20Sopenharmony_ci		rc = -ENOMEM;
3158c2ecf20Sopenharmony_ci		goto out;
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	skcipher_request_set_callback(req,
3198c2ecf20Sopenharmony_ci			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
3208c2ecf20Sopenharmony_ci			extent_crypt_complete, &ecr);
3218c2ecf20Sopenharmony_ci	/* Consider doing this once, when the file is opened */
3228c2ecf20Sopenharmony_ci	if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
3238c2ecf20Sopenharmony_ci		rc = crypto_skcipher_setkey(crypt_stat->tfm, crypt_stat->key,
3248c2ecf20Sopenharmony_ci					    crypt_stat->key_size);
3258c2ecf20Sopenharmony_ci		if (rc) {
3268c2ecf20Sopenharmony_ci			ecryptfs_printk(KERN_ERR,
3278c2ecf20Sopenharmony_ci					"Error setting key; rc = [%d]\n",
3288c2ecf20Sopenharmony_ci					rc);
3298c2ecf20Sopenharmony_ci			mutex_unlock(&crypt_stat->cs_tfm_mutex);
3308c2ecf20Sopenharmony_ci			rc = -EINVAL;
3318c2ecf20Sopenharmony_ci			goto out;
3328c2ecf20Sopenharmony_ci		}
3338c2ecf20Sopenharmony_ci		crypt_stat->flags |= ECRYPTFS_KEY_SET;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci	mutex_unlock(&crypt_stat->cs_tfm_mutex);
3368c2ecf20Sopenharmony_ci	skcipher_request_set_crypt(req, src_sg, dst_sg, size, iv);
3378c2ecf20Sopenharmony_ci	rc = op == ENCRYPT ? crypto_skcipher_encrypt(req) :
3388c2ecf20Sopenharmony_ci			     crypto_skcipher_decrypt(req);
3398c2ecf20Sopenharmony_ci	if (rc == -EINPROGRESS || rc == -EBUSY) {
3408c2ecf20Sopenharmony_ci		struct extent_crypt_result *ecr = req->base.data;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci		wait_for_completion(&ecr->completion);
3438c2ecf20Sopenharmony_ci		rc = ecr->rc;
3448c2ecf20Sopenharmony_ci		reinit_completion(&ecr->completion);
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ciout:
3478c2ecf20Sopenharmony_ci	skcipher_request_free(req);
3488c2ecf20Sopenharmony_ci	return rc;
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci/**
3528c2ecf20Sopenharmony_ci * lower_offset_for_page
3538c2ecf20Sopenharmony_ci *
3548c2ecf20Sopenharmony_ci * Convert an eCryptfs page index into a lower byte offset
3558c2ecf20Sopenharmony_ci */
3568c2ecf20Sopenharmony_cistatic loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat,
3578c2ecf20Sopenharmony_ci				    struct page *page)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	return ecryptfs_lower_header_size(crypt_stat) +
3608c2ecf20Sopenharmony_ci	       ((loff_t)page->index << PAGE_SHIFT);
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci/**
3648c2ecf20Sopenharmony_ci * crypt_extent
3658c2ecf20Sopenharmony_ci * @crypt_stat: crypt_stat containing cryptographic context for the
3668c2ecf20Sopenharmony_ci *              encryption operation
3678c2ecf20Sopenharmony_ci * @dst_page: The page to write the result into
3688c2ecf20Sopenharmony_ci * @src_page: The page to read from
3698c2ecf20Sopenharmony_ci * @extent_offset: Page extent offset for use in generating IV
3708c2ecf20Sopenharmony_ci * @op: ENCRYPT or DECRYPT to indicate the desired operation
3718c2ecf20Sopenharmony_ci *
3728c2ecf20Sopenharmony_ci * Encrypts or decrypts one extent of data.
3738c2ecf20Sopenharmony_ci *
3748c2ecf20Sopenharmony_ci * Return zero on success; non-zero otherwise
3758c2ecf20Sopenharmony_ci */
3768c2ecf20Sopenharmony_cistatic int crypt_extent(struct ecryptfs_crypt_stat *crypt_stat,
3778c2ecf20Sopenharmony_ci			struct page *dst_page,
3788c2ecf20Sopenharmony_ci			struct page *src_page,
3798c2ecf20Sopenharmony_ci			unsigned long extent_offset, int op)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	pgoff_t page_index = op == ENCRYPT ? src_page->index : dst_page->index;
3828c2ecf20Sopenharmony_ci	loff_t extent_base;
3838c2ecf20Sopenharmony_ci	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
3848c2ecf20Sopenharmony_ci	struct scatterlist src_sg, dst_sg;
3858c2ecf20Sopenharmony_ci	size_t extent_size = crypt_stat->extent_size;
3868c2ecf20Sopenharmony_ci	int rc;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	extent_base = (((loff_t)page_index) * (PAGE_SIZE / extent_size));
3898c2ecf20Sopenharmony_ci	rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
3908c2ecf20Sopenharmony_ci				(extent_base + extent_offset));
3918c2ecf20Sopenharmony_ci	if (rc) {
3928c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
3938c2ecf20Sopenharmony_ci			"extent [0x%.16llx]; rc = [%d]\n",
3948c2ecf20Sopenharmony_ci			(unsigned long long)(extent_base + extent_offset), rc);
3958c2ecf20Sopenharmony_ci		goto out;
3968c2ecf20Sopenharmony_ci	}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	sg_init_table(&src_sg, 1);
3998c2ecf20Sopenharmony_ci	sg_init_table(&dst_sg, 1);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	sg_set_page(&src_sg, src_page, extent_size,
4028c2ecf20Sopenharmony_ci		    extent_offset * extent_size);
4038c2ecf20Sopenharmony_ci	sg_set_page(&dst_sg, dst_page, extent_size,
4048c2ecf20Sopenharmony_ci		    extent_offset * extent_size);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	rc = crypt_scatterlist(crypt_stat, &dst_sg, &src_sg, extent_size,
4078c2ecf20Sopenharmony_ci			       extent_iv, op);
4088c2ecf20Sopenharmony_ci	if (rc < 0) {
4098c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: Error attempting to crypt page with "
4108c2ecf20Sopenharmony_ci		       "page_index = [%ld], extent_offset = [%ld]; "
4118c2ecf20Sopenharmony_ci		       "rc = [%d]\n", __func__, page_index, extent_offset, rc);
4128c2ecf20Sopenharmony_ci		goto out;
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci	rc = 0;
4158c2ecf20Sopenharmony_ciout:
4168c2ecf20Sopenharmony_ci	return rc;
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci/**
4208c2ecf20Sopenharmony_ci * ecryptfs_encrypt_page
4218c2ecf20Sopenharmony_ci * @page: Page mapped from the eCryptfs inode for the file; contains
4228c2ecf20Sopenharmony_ci *        decrypted content that needs to be encrypted (to a temporary
4238c2ecf20Sopenharmony_ci *        page; not in place) and written out to the lower file
4248c2ecf20Sopenharmony_ci *
4258c2ecf20Sopenharmony_ci * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
4268c2ecf20Sopenharmony_ci * that eCryptfs pages may straddle the lower pages -- for instance,
4278c2ecf20Sopenharmony_ci * if the file was created on a machine with an 8K page size
4288c2ecf20Sopenharmony_ci * (resulting in an 8K header), and then the file is copied onto a
4298c2ecf20Sopenharmony_ci * host with a 32K page size, then when reading page 0 of the eCryptfs
4308c2ecf20Sopenharmony_ci * file, 24K of page 0 of the lower file will be read and decrypted,
4318c2ecf20Sopenharmony_ci * and then 8K of page 1 of the lower file will be read and decrypted.
4328c2ecf20Sopenharmony_ci *
4338c2ecf20Sopenharmony_ci * Returns zero on success; negative on error
4348c2ecf20Sopenharmony_ci */
4358c2ecf20Sopenharmony_ciint ecryptfs_encrypt_page(struct page *page)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	struct inode *ecryptfs_inode;
4388c2ecf20Sopenharmony_ci	struct ecryptfs_crypt_stat *crypt_stat;
4398c2ecf20Sopenharmony_ci	char *enc_extent_virt;
4408c2ecf20Sopenharmony_ci	struct page *enc_extent_page = NULL;
4418c2ecf20Sopenharmony_ci	loff_t extent_offset;
4428c2ecf20Sopenharmony_ci	loff_t lower_offset;
4438c2ecf20Sopenharmony_ci	int rc = 0;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	ecryptfs_inode = page->mapping->host;
4468c2ecf20Sopenharmony_ci	crypt_stat =
4478c2ecf20Sopenharmony_ci		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
4488c2ecf20Sopenharmony_ci	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
4498c2ecf20Sopenharmony_ci	enc_extent_page = alloc_page(GFP_USER);
4508c2ecf20Sopenharmony_ci	if (!enc_extent_page) {
4518c2ecf20Sopenharmony_ci		rc = -ENOMEM;
4528c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_ERR, "Error allocating memory for "
4538c2ecf20Sopenharmony_ci				"encrypted extent\n");
4548c2ecf20Sopenharmony_ci		goto out;
4558c2ecf20Sopenharmony_ci	}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	for (extent_offset = 0;
4588c2ecf20Sopenharmony_ci	     extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
4598c2ecf20Sopenharmony_ci	     extent_offset++) {
4608c2ecf20Sopenharmony_ci		rc = crypt_extent(crypt_stat, enc_extent_page, page,
4618c2ecf20Sopenharmony_ci				  extent_offset, ENCRYPT);
4628c2ecf20Sopenharmony_ci		if (rc) {
4638c2ecf20Sopenharmony_ci			printk(KERN_ERR "%s: Error encrypting extent; "
4648c2ecf20Sopenharmony_ci			       "rc = [%d]\n", __func__, rc);
4658c2ecf20Sopenharmony_ci			goto out;
4668c2ecf20Sopenharmony_ci		}
4678c2ecf20Sopenharmony_ci	}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	lower_offset = lower_offset_for_page(crypt_stat, page);
4708c2ecf20Sopenharmony_ci	enc_extent_virt = kmap(enc_extent_page);
4718c2ecf20Sopenharmony_ci	rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset,
4728c2ecf20Sopenharmony_ci				  PAGE_SIZE);
4738c2ecf20Sopenharmony_ci	kunmap(enc_extent_page);
4748c2ecf20Sopenharmony_ci	if (rc < 0) {
4758c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_ERR,
4768c2ecf20Sopenharmony_ci			"Error attempting to write lower page; rc = [%d]\n",
4778c2ecf20Sopenharmony_ci			rc);
4788c2ecf20Sopenharmony_ci		goto out;
4798c2ecf20Sopenharmony_ci	}
4808c2ecf20Sopenharmony_ci	rc = 0;
4818c2ecf20Sopenharmony_ciout:
4828c2ecf20Sopenharmony_ci	if (enc_extent_page) {
4838c2ecf20Sopenharmony_ci		__free_page(enc_extent_page);
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci	return rc;
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci/**
4898c2ecf20Sopenharmony_ci * ecryptfs_decrypt_page
4908c2ecf20Sopenharmony_ci * @page: Page mapped from the eCryptfs inode for the file; data read
4918c2ecf20Sopenharmony_ci *        and decrypted from the lower file will be written into this
4928c2ecf20Sopenharmony_ci *        page
4938c2ecf20Sopenharmony_ci *
4948c2ecf20Sopenharmony_ci * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
4958c2ecf20Sopenharmony_ci * that eCryptfs pages may straddle the lower pages -- for instance,
4968c2ecf20Sopenharmony_ci * if the file was created on a machine with an 8K page size
4978c2ecf20Sopenharmony_ci * (resulting in an 8K header), and then the file is copied onto a
4988c2ecf20Sopenharmony_ci * host with a 32K page size, then when reading page 0 of the eCryptfs
4998c2ecf20Sopenharmony_ci * file, 24K of page 0 of the lower file will be read and decrypted,
5008c2ecf20Sopenharmony_ci * and then 8K of page 1 of the lower file will be read and decrypted.
5018c2ecf20Sopenharmony_ci *
5028c2ecf20Sopenharmony_ci * Returns zero on success; negative on error
5038c2ecf20Sopenharmony_ci */
5048c2ecf20Sopenharmony_ciint ecryptfs_decrypt_page(struct page *page)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	struct inode *ecryptfs_inode;
5078c2ecf20Sopenharmony_ci	struct ecryptfs_crypt_stat *crypt_stat;
5088c2ecf20Sopenharmony_ci	char *page_virt;
5098c2ecf20Sopenharmony_ci	unsigned long extent_offset;
5108c2ecf20Sopenharmony_ci	loff_t lower_offset;
5118c2ecf20Sopenharmony_ci	int rc = 0;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	ecryptfs_inode = page->mapping->host;
5148c2ecf20Sopenharmony_ci	crypt_stat =
5158c2ecf20Sopenharmony_ci		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
5168c2ecf20Sopenharmony_ci	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	lower_offset = lower_offset_for_page(crypt_stat, page);
5198c2ecf20Sopenharmony_ci	page_virt = kmap(page);
5208c2ecf20Sopenharmony_ci	rc = ecryptfs_read_lower(page_virt, lower_offset, PAGE_SIZE,
5218c2ecf20Sopenharmony_ci				 ecryptfs_inode);
5228c2ecf20Sopenharmony_ci	kunmap(page);
5238c2ecf20Sopenharmony_ci	if (rc < 0) {
5248c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_ERR,
5258c2ecf20Sopenharmony_ci			"Error attempting to read lower page; rc = [%d]\n",
5268c2ecf20Sopenharmony_ci			rc);
5278c2ecf20Sopenharmony_ci		goto out;
5288c2ecf20Sopenharmony_ci	}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	for (extent_offset = 0;
5318c2ecf20Sopenharmony_ci	     extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
5328c2ecf20Sopenharmony_ci	     extent_offset++) {
5338c2ecf20Sopenharmony_ci		rc = crypt_extent(crypt_stat, page, page,
5348c2ecf20Sopenharmony_ci				  extent_offset, DECRYPT);
5358c2ecf20Sopenharmony_ci		if (rc) {
5368c2ecf20Sopenharmony_ci			printk(KERN_ERR "%s: Error encrypting extent; "
5378c2ecf20Sopenharmony_ci			       "rc = [%d]\n", __func__, rc);
5388c2ecf20Sopenharmony_ci			goto out;
5398c2ecf20Sopenharmony_ci		}
5408c2ecf20Sopenharmony_ci	}
5418c2ecf20Sopenharmony_ciout:
5428c2ecf20Sopenharmony_ci	return rc;
5438c2ecf20Sopenharmony_ci}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci/**
5488c2ecf20Sopenharmony_ci * ecryptfs_init_crypt_ctx
5498c2ecf20Sopenharmony_ci * @crypt_stat: Uninitialized crypt stats structure
5508c2ecf20Sopenharmony_ci *
5518c2ecf20Sopenharmony_ci * Initialize the crypto context.
5528c2ecf20Sopenharmony_ci *
5538c2ecf20Sopenharmony_ci * TODO: Performance: Keep a cache of initialized cipher contexts;
5548c2ecf20Sopenharmony_ci * only init if needed
5558c2ecf20Sopenharmony_ci */
5568c2ecf20Sopenharmony_ciint ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	char *full_alg_name;
5598c2ecf20Sopenharmony_ci	int rc = -EINVAL;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	ecryptfs_printk(KERN_DEBUG,
5628c2ecf20Sopenharmony_ci			"Initializing cipher [%s]; strlen = [%d]; "
5638c2ecf20Sopenharmony_ci			"key_size_bits = [%zd]\n",
5648c2ecf20Sopenharmony_ci			crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
5658c2ecf20Sopenharmony_ci			crypt_stat->key_size << 3);
5668c2ecf20Sopenharmony_ci	mutex_lock(&crypt_stat->cs_tfm_mutex);
5678c2ecf20Sopenharmony_ci	if (crypt_stat->tfm) {
5688c2ecf20Sopenharmony_ci		rc = 0;
5698c2ecf20Sopenharmony_ci		goto out_unlock;
5708c2ecf20Sopenharmony_ci	}
5718c2ecf20Sopenharmony_ci	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
5728c2ecf20Sopenharmony_ci						    crypt_stat->cipher, "cbc");
5738c2ecf20Sopenharmony_ci	if (rc)
5748c2ecf20Sopenharmony_ci		goto out_unlock;
5758c2ecf20Sopenharmony_ci	crypt_stat->tfm = crypto_alloc_skcipher(full_alg_name, 0, 0);
5768c2ecf20Sopenharmony_ci	if (IS_ERR(crypt_stat->tfm)) {
5778c2ecf20Sopenharmony_ci		rc = PTR_ERR(crypt_stat->tfm);
5788c2ecf20Sopenharmony_ci		crypt_stat->tfm = NULL;
5798c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
5808c2ecf20Sopenharmony_ci				"Error initializing cipher [%s]\n",
5818c2ecf20Sopenharmony_ci				full_alg_name);
5828c2ecf20Sopenharmony_ci		goto out_free;
5838c2ecf20Sopenharmony_ci	}
5848c2ecf20Sopenharmony_ci	crypto_skcipher_set_flags(crypt_stat->tfm,
5858c2ecf20Sopenharmony_ci				  CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
5868c2ecf20Sopenharmony_ci	rc = 0;
5878c2ecf20Sopenharmony_ciout_free:
5888c2ecf20Sopenharmony_ci	kfree(full_alg_name);
5898c2ecf20Sopenharmony_ciout_unlock:
5908c2ecf20Sopenharmony_ci	mutex_unlock(&crypt_stat->cs_tfm_mutex);
5918c2ecf20Sopenharmony_ci	return rc;
5928c2ecf20Sopenharmony_ci}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_cistatic void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
5958c2ecf20Sopenharmony_ci{
5968c2ecf20Sopenharmony_ci	int extent_size_tmp;
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	crypt_stat->extent_mask = 0xFFFFFFFF;
5998c2ecf20Sopenharmony_ci	crypt_stat->extent_shift = 0;
6008c2ecf20Sopenharmony_ci	if (crypt_stat->extent_size == 0)
6018c2ecf20Sopenharmony_ci		return;
6028c2ecf20Sopenharmony_ci	extent_size_tmp = crypt_stat->extent_size;
6038c2ecf20Sopenharmony_ci	while ((extent_size_tmp & 0x01) == 0) {
6048c2ecf20Sopenharmony_ci		extent_size_tmp >>= 1;
6058c2ecf20Sopenharmony_ci		crypt_stat->extent_mask <<= 1;
6068c2ecf20Sopenharmony_ci		crypt_stat->extent_shift++;
6078c2ecf20Sopenharmony_ci	}
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_civoid ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci	/* Default values; may be overwritten as we are parsing the
6138c2ecf20Sopenharmony_ci	 * packets. */
6148c2ecf20Sopenharmony_ci	crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
6158c2ecf20Sopenharmony_ci	set_extent_mask_and_shift(crypt_stat);
6168c2ecf20Sopenharmony_ci	crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
6178c2ecf20Sopenharmony_ci	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
6188c2ecf20Sopenharmony_ci		crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
6198c2ecf20Sopenharmony_ci	else {
6208c2ecf20Sopenharmony_ci		if (PAGE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
6218c2ecf20Sopenharmony_ci			crypt_stat->metadata_size =
6228c2ecf20Sopenharmony_ci				ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
6238c2ecf20Sopenharmony_ci		else
6248c2ecf20Sopenharmony_ci			crypt_stat->metadata_size = PAGE_SIZE;
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci/**
6298c2ecf20Sopenharmony_ci * ecryptfs_compute_root_iv
6308c2ecf20Sopenharmony_ci * @crypt_stats
6318c2ecf20Sopenharmony_ci *
6328c2ecf20Sopenharmony_ci * On error, sets the root IV to all 0's.
6338c2ecf20Sopenharmony_ci */
6348c2ecf20Sopenharmony_ciint ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	int rc = 0;
6378c2ecf20Sopenharmony_ci	char dst[MD5_DIGEST_SIZE];
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
6408c2ecf20Sopenharmony_ci	BUG_ON(crypt_stat->iv_bytes <= 0);
6418c2ecf20Sopenharmony_ci	if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
6428c2ecf20Sopenharmony_ci		rc = -EINVAL;
6438c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_WARNING, "Session key not valid; "
6448c2ecf20Sopenharmony_ci				"cannot generate root IV\n");
6458c2ecf20Sopenharmony_ci		goto out;
6468c2ecf20Sopenharmony_ci	}
6478c2ecf20Sopenharmony_ci	rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
6488c2ecf20Sopenharmony_ci				    crypt_stat->key_size);
6498c2ecf20Sopenharmony_ci	if (rc) {
6508c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
6518c2ecf20Sopenharmony_ci				"MD5 while generating root IV\n");
6528c2ecf20Sopenharmony_ci		goto out;
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci	memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
6558c2ecf20Sopenharmony_ciout:
6568c2ecf20Sopenharmony_ci	if (rc) {
6578c2ecf20Sopenharmony_ci		memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
6588c2ecf20Sopenharmony_ci		crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
6598c2ecf20Sopenharmony_ci	}
6608c2ecf20Sopenharmony_ci	return rc;
6618c2ecf20Sopenharmony_ci}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_cistatic void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
6648c2ecf20Sopenharmony_ci{
6658c2ecf20Sopenharmony_ci	get_random_bytes(crypt_stat->key, crypt_stat->key_size);
6668c2ecf20Sopenharmony_ci	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
6678c2ecf20Sopenharmony_ci	ecryptfs_compute_root_iv(crypt_stat);
6688c2ecf20Sopenharmony_ci	if (unlikely(ecryptfs_verbosity > 0)) {
6698c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
6708c2ecf20Sopenharmony_ci		ecryptfs_dump_hex(crypt_stat->key,
6718c2ecf20Sopenharmony_ci				  crypt_stat->key_size);
6728c2ecf20Sopenharmony_ci	}
6738c2ecf20Sopenharmony_ci}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci/**
6768c2ecf20Sopenharmony_ci * ecryptfs_copy_mount_wide_flags_to_inode_flags
6778c2ecf20Sopenharmony_ci * @crypt_stat: The inode's cryptographic context
6788c2ecf20Sopenharmony_ci * @mount_crypt_stat: The mount point's cryptographic context
6798c2ecf20Sopenharmony_ci *
6808c2ecf20Sopenharmony_ci * This function propagates the mount-wide flags to individual inode
6818c2ecf20Sopenharmony_ci * flags.
6828c2ecf20Sopenharmony_ci */
6838c2ecf20Sopenharmony_cistatic void ecryptfs_copy_mount_wide_flags_to_inode_flags(
6848c2ecf20Sopenharmony_ci	struct ecryptfs_crypt_stat *crypt_stat,
6858c2ecf20Sopenharmony_ci	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
6868c2ecf20Sopenharmony_ci{
6878c2ecf20Sopenharmony_ci	if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
6888c2ecf20Sopenharmony_ci		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
6898c2ecf20Sopenharmony_ci	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
6908c2ecf20Sopenharmony_ci		crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
6918c2ecf20Sopenharmony_ci	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
6928c2ecf20Sopenharmony_ci		crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
6938c2ecf20Sopenharmony_ci		if (mount_crypt_stat->flags
6948c2ecf20Sopenharmony_ci		    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
6958c2ecf20Sopenharmony_ci			crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
6968c2ecf20Sopenharmony_ci		else if (mount_crypt_stat->flags
6978c2ecf20Sopenharmony_ci			 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
6988c2ecf20Sopenharmony_ci			crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
6998c2ecf20Sopenharmony_ci	}
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_cistatic int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
7038c2ecf20Sopenharmony_ci	struct ecryptfs_crypt_stat *crypt_stat,
7048c2ecf20Sopenharmony_ci	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	struct ecryptfs_global_auth_tok *global_auth_tok;
7078c2ecf20Sopenharmony_ci	int rc = 0;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	mutex_lock(&crypt_stat->keysig_list_mutex);
7108c2ecf20Sopenharmony_ci	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	list_for_each_entry(global_auth_tok,
7138c2ecf20Sopenharmony_ci			    &mount_crypt_stat->global_auth_tok_list,
7148c2ecf20Sopenharmony_ci			    mount_crypt_stat_list) {
7158c2ecf20Sopenharmony_ci		if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
7168c2ecf20Sopenharmony_ci			continue;
7178c2ecf20Sopenharmony_ci		rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
7188c2ecf20Sopenharmony_ci		if (rc) {
7198c2ecf20Sopenharmony_ci			printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
7208c2ecf20Sopenharmony_ci			goto out;
7218c2ecf20Sopenharmony_ci		}
7228c2ecf20Sopenharmony_ci	}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ciout:
7258c2ecf20Sopenharmony_ci	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
7268c2ecf20Sopenharmony_ci	mutex_unlock(&crypt_stat->keysig_list_mutex);
7278c2ecf20Sopenharmony_ci	return rc;
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci/**
7318c2ecf20Sopenharmony_ci * ecryptfs_set_default_crypt_stat_vals
7328c2ecf20Sopenharmony_ci * @crypt_stat: The inode's cryptographic context
7338c2ecf20Sopenharmony_ci * @mount_crypt_stat: The mount point's cryptographic context
7348c2ecf20Sopenharmony_ci *
7358c2ecf20Sopenharmony_ci * Default values in the event that policy does not override them.
7368c2ecf20Sopenharmony_ci */
7378c2ecf20Sopenharmony_cistatic void ecryptfs_set_default_crypt_stat_vals(
7388c2ecf20Sopenharmony_ci	struct ecryptfs_crypt_stat *crypt_stat,
7398c2ecf20Sopenharmony_ci	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
7408c2ecf20Sopenharmony_ci{
7418c2ecf20Sopenharmony_ci	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
7428c2ecf20Sopenharmony_ci						      mount_crypt_stat);
7438c2ecf20Sopenharmony_ci	ecryptfs_set_default_sizes(crypt_stat);
7448c2ecf20Sopenharmony_ci	strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
7458c2ecf20Sopenharmony_ci	crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
7468c2ecf20Sopenharmony_ci	crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
7478c2ecf20Sopenharmony_ci	crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
7488c2ecf20Sopenharmony_ci	crypt_stat->mount_crypt_stat = mount_crypt_stat;
7498c2ecf20Sopenharmony_ci}
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci/**
7528c2ecf20Sopenharmony_ci * ecryptfs_new_file_context
7538c2ecf20Sopenharmony_ci * @ecryptfs_inode: The eCryptfs inode
7548c2ecf20Sopenharmony_ci *
7558c2ecf20Sopenharmony_ci * If the crypto context for the file has not yet been established,
7568c2ecf20Sopenharmony_ci * this is where we do that.  Establishing a new crypto context
7578c2ecf20Sopenharmony_ci * involves the following decisions:
7588c2ecf20Sopenharmony_ci *  - What cipher to use?
7598c2ecf20Sopenharmony_ci *  - What set of authentication tokens to use?
7608c2ecf20Sopenharmony_ci * Here we just worry about getting enough information into the
7618c2ecf20Sopenharmony_ci * authentication tokens so that we know that they are available.
7628c2ecf20Sopenharmony_ci * We associate the available authentication tokens with the new file
7638c2ecf20Sopenharmony_ci * via the set of signatures in the crypt_stat struct.  Later, when
7648c2ecf20Sopenharmony_ci * the headers are actually written out, we may again defer to
7658c2ecf20Sopenharmony_ci * userspace to perform the encryption of the session key; for the
7668c2ecf20Sopenharmony_ci * foreseeable future, this will be the case with public key packets.
7678c2ecf20Sopenharmony_ci *
7688c2ecf20Sopenharmony_ci * Returns zero on success; non-zero otherwise
7698c2ecf20Sopenharmony_ci */
7708c2ecf20Sopenharmony_ciint ecryptfs_new_file_context(struct inode *ecryptfs_inode)
7718c2ecf20Sopenharmony_ci{
7728c2ecf20Sopenharmony_ci	struct ecryptfs_crypt_stat *crypt_stat =
7738c2ecf20Sopenharmony_ci	    &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
7748c2ecf20Sopenharmony_ci	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
7758c2ecf20Sopenharmony_ci	    &ecryptfs_superblock_to_private(
7768c2ecf20Sopenharmony_ci		    ecryptfs_inode->i_sb)->mount_crypt_stat;
7778c2ecf20Sopenharmony_ci	int cipher_name_len;
7788c2ecf20Sopenharmony_ci	int rc = 0;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
7818c2ecf20Sopenharmony_ci	crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
7828c2ecf20Sopenharmony_ci	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
7838c2ecf20Sopenharmony_ci						      mount_crypt_stat);
7848c2ecf20Sopenharmony_ci	rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
7858c2ecf20Sopenharmony_ci							 mount_crypt_stat);
7868c2ecf20Sopenharmony_ci	if (rc) {
7878c2ecf20Sopenharmony_ci		printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
7888c2ecf20Sopenharmony_ci		       "to the inode key sigs; rc = [%d]\n", rc);
7898c2ecf20Sopenharmony_ci		goto out;
7908c2ecf20Sopenharmony_ci	}
7918c2ecf20Sopenharmony_ci	cipher_name_len =
7928c2ecf20Sopenharmony_ci		strlen(mount_crypt_stat->global_default_cipher_name);
7938c2ecf20Sopenharmony_ci	memcpy(crypt_stat->cipher,
7948c2ecf20Sopenharmony_ci	       mount_crypt_stat->global_default_cipher_name,
7958c2ecf20Sopenharmony_ci	       cipher_name_len);
7968c2ecf20Sopenharmony_ci	crypt_stat->cipher[cipher_name_len] = '\0';
7978c2ecf20Sopenharmony_ci	crypt_stat->key_size =
7988c2ecf20Sopenharmony_ci		mount_crypt_stat->global_default_cipher_key_size;
7998c2ecf20Sopenharmony_ci	ecryptfs_generate_new_key(crypt_stat);
8008c2ecf20Sopenharmony_ci	rc = ecryptfs_init_crypt_ctx(crypt_stat);
8018c2ecf20Sopenharmony_ci	if (rc)
8028c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
8038c2ecf20Sopenharmony_ci				"context for cipher [%s]: rc = [%d]\n",
8048c2ecf20Sopenharmony_ci				crypt_stat->cipher, rc);
8058c2ecf20Sopenharmony_ciout:
8068c2ecf20Sopenharmony_ci	return rc;
8078c2ecf20Sopenharmony_ci}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci/**
8108c2ecf20Sopenharmony_ci * ecryptfs_validate_marker - check for the ecryptfs marker
8118c2ecf20Sopenharmony_ci * @data: The data block in which to check
8128c2ecf20Sopenharmony_ci *
8138c2ecf20Sopenharmony_ci * Returns zero if marker found; -EINVAL if not found
8148c2ecf20Sopenharmony_ci */
8158c2ecf20Sopenharmony_cistatic int ecryptfs_validate_marker(char *data)
8168c2ecf20Sopenharmony_ci{
8178c2ecf20Sopenharmony_ci	u32 m_1, m_2;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	m_1 = get_unaligned_be32(data);
8208c2ecf20Sopenharmony_ci	m_2 = get_unaligned_be32(data + 4);
8218c2ecf20Sopenharmony_ci	if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
8228c2ecf20Sopenharmony_ci		return 0;
8238c2ecf20Sopenharmony_ci	ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
8248c2ecf20Sopenharmony_ci			"MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
8258c2ecf20Sopenharmony_ci			MAGIC_ECRYPTFS_MARKER);
8268c2ecf20Sopenharmony_ci	ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
8278c2ecf20Sopenharmony_ci			"[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
8288c2ecf20Sopenharmony_ci	return -EINVAL;
8298c2ecf20Sopenharmony_ci}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_cistruct ecryptfs_flag_map_elem {
8328c2ecf20Sopenharmony_ci	u32 file_flag;
8338c2ecf20Sopenharmony_ci	u32 local_flag;
8348c2ecf20Sopenharmony_ci};
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci/* Add support for additional flags by adding elements here. */
8378c2ecf20Sopenharmony_cistatic struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
8388c2ecf20Sopenharmony_ci	{0x00000001, ECRYPTFS_ENABLE_HMAC},
8398c2ecf20Sopenharmony_ci	{0x00000002, ECRYPTFS_ENCRYPTED},
8408c2ecf20Sopenharmony_ci	{0x00000004, ECRYPTFS_METADATA_IN_XATTR},
8418c2ecf20Sopenharmony_ci	{0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
8428c2ecf20Sopenharmony_ci};
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci/**
8458c2ecf20Sopenharmony_ci * ecryptfs_process_flags
8468c2ecf20Sopenharmony_ci * @crypt_stat: The cryptographic context
8478c2ecf20Sopenharmony_ci * @page_virt: Source data to be parsed
8488c2ecf20Sopenharmony_ci * @bytes_read: Updated with the number of bytes read
8498c2ecf20Sopenharmony_ci */
8508c2ecf20Sopenharmony_cistatic void ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
8518c2ecf20Sopenharmony_ci				  char *page_virt, int *bytes_read)
8528c2ecf20Sopenharmony_ci{
8538c2ecf20Sopenharmony_ci	int i;
8548c2ecf20Sopenharmony_ci	u32 flags;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	flags = get_unaligned_be32(page_virt);
8578c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
8588c2ecf20Sopenharmony_ci		if (flags & ecryptfs_flag_map[i].file_flag) {
8598c2ecf20Sopenharmony_ci			crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
8608c2ecf20Sopenharmony_ci		} else
8618c2ecf20Sopenharmony_ci			crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
8628c2ecf20Sopenharmony_ci	/* Version is in top 8 bits of the 32-bit flag vector */
8638c2ecf20Sopenharmony_ci	crypt_stat->file_version = ((flags >> 24) & 0xFF);
8648c2ecf20Sopenharmony_ci	(*bytes_read) = 4;
8658c2ecf20Sopenharmony_ci}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci/**
8688c2ecf20Sopenharmony_ci * write_ecryptfs_marker
8698c2ecf20Sopenharmony_ci * @page_virt: The pointer to in a page to begin writing the marker
8708c2ecf20Sopenharmony_ci * @written: Number of bytes written
8718c2ecf20Sopenharmony_ci *
8728c2ecf20Sopenharmony_ci * Marker = 0x3c81b7f5
8738c2ecf20Sopenharmony_ci */
8748c2ecf20Sopenharmony_cistatic void write_ecryptfs_marker(char *page_virt, size_t *written)
8758c2ecf20Sopenharmony_ci{
8768c2ecf20Sopenharmony_ci	u32 m_1, m_2;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
8798c2ecf20Sopenharmony_ci	m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
8808c2ecf20Sopenharmony_ci	put_unaligned_be32(m_1, page_virt);
8818c2ecf20Sopenharmony_ci	page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
8828c2ecf20Sopenharmony_ci	put_unaligned_be32(m_2, page_virt);
8838c2ecf20Sopenharmony_ci	(*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
8848c2ecf20Sopenharmony_ci}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_civoid ecryptfs_write_crypt_stat_flags(char *page_virt,
8878c2ecf20Sopenharmony_ci				     struct ecryptfs_crypt_stat *crypt_stat,
8888c2ecf20Sopenharmony_ci				     size_t *written)
8898c2ecf20Sopenharmony_ci{
8908c2ecf20Sopenharmony_ci	u32 flags = 0;
8918c2ecf20Sopenharmony_ci	int i;
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
8948c2ecf20Sopenharmony_ci		if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
8958c2ecf20Sopenharmony_ci			flags |= ecryptfs_flag_map[i].file_flag;
8968c2ecf20Sopenharmony_ci	/* Version is in top 8 bits of the 32-bit flag vector */
8978c2ecf20Sopenharmony_ci	flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
8988c2ecf20Sopenharmony_ci	put_unaligned_be32(flags, page_virt);
8998c2ecf20Sopenharmony_ci	(*written) = 4;
9008c2ecf20Sopenharmony_ci}
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_cistruct ecryptfs_cipher_code_str_map_elem {
9038c2ecf20Sopenharmony_ci	char cipher_str[16];
9048c2ecf20Sopenharmony_ci	u8 cipher_code;
9058c2ecf20Sopenharmony_ci};
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci/* Add support for additional ciphers by adding elements here. The
9088c2ecf20Sopenharmony_ci * cipher_code is whatever OpenPGP applications use to identify the
9098c2ecf20Sopenharmony_ci * ciphers. List in order of probability. */
9108c2ecf20Sopenharmony_cistatic struct ecryptfs_cipher_code_str_map_elem
9118c2ecf20Sopenharmony_ciecryptfs_cipher_code_str_map[] = {
9128c2ecf20Sopenharmony_ci	{"aes",RFC2440_CIPHER_AES_128 },
9138c2ecf20Sopenharmony_ci	{"blowfish", RFC2440_CIPHER_BLOWFISH},
9148c2ecf20Sopenharmony_ci	{"des3_ede", RFC2440_CIPHER_DES3_EDE},
9158c2ecf20Sopenharmony_ci	{"cast5", RFC2440_CIPHER_CAST_5},
9168c2ecf20Sopenharmony_ci	{"twofish", RFC2440_CIPHER_TWOFISH},
9178c2ecf20Sopenharmony_ci	{"cast6", RFC2440_CIPHER_CAST_6},
9188c2ecf20Sopenharmony_ci	{"aes", RFC2440_CIPHER_AES_192},
9198c2ecf20Sopenharmony_ci	{"aes", RFC2440_CIPHER_AES_256}
9208c2ecf20Sopenharmony_ci};
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci/**
9238c2ecf20Sopenharmony_ci * ecryptfs_code_for_cipher_string
9248c2ecf20Sopenharmony_ci * @cipher_name: The string alias for the cipher
9258c2ecf20Sopenharmony_ci * @key_bytes: Length of key in bytes; used for AES code selection
9268c2ecf20Sopenharmony_ci *
9278c2ecf20Sopenharmony_ci * Returns zero on no match, or the cipher code on match
9288c2ecf20Sopenharmony_ci */
9298c2ecf20Sopenharmony_ciu8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
9308c2ecf20Sopenharmony_ci{
9318c2ecf20Sopenharmony_ci	int i;
9328c2ecf20Sopenharmony_ci	u8 code = 0;
9338c2ecf20Sopenharmony_ci	struct ecryptfs_cipher_code_str_map_elem *map =
9348c2ecf20Sopenharmony_ci		ecryptfs_cipher_code_str_map;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	if (strcmp(cipher_name, "aes") == 0) {
9378c2ecf20Sopenharmony_ci		switch (key_bytes) {
9388c2ecf20Sopenharmony_ci		case 16:
9398c2ecf20Sopenharmony_ci			code = RFC2440_CIPHER_AES_128;
9408c2ecf20Sopenharmony_ci			break;
9418c2ecf20Sopenharmony_ci		case 24:
9428c2ecf20Sopenharmony_ci			code = RFC2440_CIPHER_AES_192;
9438c2ecf20Sopenharmony_ci			break;
9448c2ecf20Sopenharmony_ci		case 32:
9458c2ecf20Sopenharmony_ci			code = RFC2440_CIPHER_AES_256;
9468c2ecf20Sopenharmony_ci		}
9478c2ecf20Sopenharmony_ci	} else {
9488c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
9498c2ecf20Sopenharmony_ci			if (strcmp(cipher_name, map[i].cipher_str) == 0) {
9508c2ecf20Sopenharmony_ci				code = map[i].cipher_code;
9518c2ecf20Sopenharmony_ci				break;
9528c2ecf20Sopenharmony_ci			}
9538c2ecf20Sopenharmony_ci	}
9548c2ecf20Sopenharmony_ci	return code;
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci/**
9588c2ecf20Sopenharmony_ci * ecryptfs_cipher_code_to_string
9598c2ecf20Sopenharmony_ci * @str: Destination to write out the cipher name
9608c2ecf20Sopenharmony_ci * @cipher_code: The code to convert to cipher name string
9618c2ecf20Sopenharmony_ci *
9628c2ecf20Sopenharmony_ci * Returns zero on success
9638c2ecf20Sopenharmony_ci */
9648c2ecf20Sopenharmony_ciint ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
9658c2ecf20Sopenharmony_ci{
9668c2ecf20Sopenharmony_ci	int rc = 0;
9678c2ecf20Sopenharmony_ci	int i;
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	str[0] = '\0';
9708c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
9718c2ecf20Sopenharmony_ci		if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
9728c2ecf20Sopenharmony_ci			strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
9738c2ecf20Sopenharmony_ci	if (str[0] == '\0') {
9748c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
9758c2ecf20Sopenharmony_ci				"[%d]\n", cipher_code);
9768c2ecf20Sopenharmony_ci		rc = -EINVAL;
9778c2ecf20Sopenharmony_ci	}
9788c2ecf20Sopenharmony_ci	return rc;
9798c2ecf20Sopenharmony_ci}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ciint ecryptfs_read_and_validate_header_region(struct inode *inode)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
9848c2ecf20Sopenharmony_ci	u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
9858c2ecf20Sopenharmony_ci	int rc;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
9888c2ecf20Sopenharmony_ci				 inode);
9898c2ecf20Sopenharmony_ci	if (rc < 0)
9908c2ecf20Sopenharmony_ci		return rc;
9918c2ecf20Sopenharmony_ci	else if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
9928c2ecf20Sopenharmony_ci		return -EINVAL;
9938c2ecf20Sopenharmony_ci	rc = ecryptfs_validate_marker(marker);
9948c2ecf20Sopenharmony_ci	if (!rc)
9958c2ecf20Sopenharmony_ci		ecryptfs_i_size_init(file_size, inode);
9968c2ecf20Sopenharmony_ci	return rc;
9978c2ecf20Sopenharmony_ci}
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_civoid
10008c2ecf20Sopenharmony_ciecryptfs_write_header_metadata(char *virt,
10018c2ecf20Sopenharmony_ci			       struct ecryptfs_crypt_stat *crypt_stat,
10028c2ecf20Sopenharmony_ci			       size_t *written)
10038c2ecf20Sopenharmony_ci{
10048c2ecf20Sopenharmony_ci	u32 header_extent_size;
10058c2ecf20Sopenharmony_ci	u16 num_header_extents_at_front;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	header_extent_size = (u32)crypt_stat->extent_size;
10088c2ecf20Sopenharmony_ci	num_header_extents_at_front =
10098c2ecf20Sopenharmony_ci		(u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
10108c2ecf20Sopenharmony_ci	put_unaligned_be32(header_extent_size, virt);
10118c2ecf20Sopenharmony_ci	virt += 4;
10128c2ecf20Sopenharmony_ci	put_unaligned_be16(num_header_extents_at_front, virt);
10138c2ecf20Sopenharmony_ci	(*written) = 6;
10148c2ecf20Sopenharmony_ci}
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_cistruct kmem_cache *ecryptfs_header_cache;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci/**
10198c2ecf20Sopenharmony_ci * ecryptfs_write_headers_virt
10208c2ecf20Sopenharmony_ci * @page_virt: The virtual address to write the headers to
10218c2ecf20Sopenharmony_ci * @max: The size of memory allocated at page_virt
10228c2ecf20Sopenharmony_ci * @size: Set to the number of bytes written by this function
10238c2ecf20Sopenharmony_ci * @crypt_stat: The cryptographic context
10248c2ecf20Sopenharmony_ci * @ecryptfs_dentry: The eCryptfs dentry
10258c2ecf20Sopenharmony_ci *
10268c2ecf20Sopenharmony_ci * Format version: 1
10278c2ecf20Sopenharmony_ci *
10288c2ecf20Sopenharmony_ci *   Header Extent:
10298c2ecf20Sopenharmony_ci *     Octets 0-7:        Unencrypted file size (big-endian)
10308c2ecf20Sopenharmony_ci *     Octets 8-15:       eCryptfs special marker
10318c2ecf20Sopenharmony_ci *     Octets 16-19:      Flags
10328c2ecf20Sopenharmony_ci *      Octet 16:         File format version number (between 0 and 255)
10338c2ecf20Sopenharmony_ci *      Octets 17-18:     Reserved
10348c2ecf20Sopenharmony_ci *      Octet 19:         Bit 1 (lsb): Reserved
10358c2ecf20Sopenharmony_ci *                        Bit 2: Encrypted?
10368c2ecf20Sopenharmony_ci *                        Bits 3-8: Reserved
10378c2ecf20Sopenharmony_ci *     Octets 20-23:      Header extent size (big-endian)
10388c2ecf20Sopenharmony_ci *     Octets 24-25:      Number of header extents at front of file
10398c2ecf20Sopenharmony_ci *                        (big-endian)
10408c2ecf20Sopenharmony_ci *     Octet  26:         Begin RFC 2440 authentication token packet set
10418c2ecf20Sopenharmony_ci *   Data Extent 0:
10428c2ecf20Sopenharmony_ci *     Lower data (CBC encrypted)
10438c2ecf20Sopenharmony_ci *   Data Extent 1:
10448c2ecf20Sopenharmony_ci *     Lower data (CBC encrypted)
10458c2ecf20Sopenharmony_ci *   ...
10468c2ecf20Sopenharmony_ci *
10478c2ecf20Sopenharmony_ci * Returns zero on success
10488c2ecf20Sopenharmony_ci */
10498c2ecf20Sopenharmony_cistatic int ecryptfs_write_headers_virt(char *page_virt, size_t max,
10508c2ecf20Sopenharmony_ci				       size_t *size,
10518c2ecf20Sopenharmony_ci				       struct ecryptfs_crypt_stat *crypt_stat,
10528c2ecf20Sopenharmony_ci				       struct dentry *ecryptfs_dentry)
10538c2ecf20Sopenharmony_ci{
10548c2ecf20Sopenharmony_ci	int rc;
10558c2ecf20Sopenharmony_ci	size_t written;
10568c2ecf20Sopenharmony_ci	size_t offset;
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	offset = ECRYPTFS_FILE_SIZE_BYTES;
10598c2ecf20Sopenharmony_ci	write_ecryptfs_marker((page_virt + offset), &written);
10608c2ecf20Sopenharmony_ci	offset += written;
10618c2ecf20Sopenharmony_ci	ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
10628c2ecf20Sopenharmony_ci					&written);
10638c2ecf20Sopenharmony_ci	offset += written;
10648c2ecf20Sopenharmony_ci	ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
10658c2ecf20Sopenharmony_ci				       &written);
10668c2ecf20Sopenharmony_ci	offset += written;
10678c2ecf20Sopenharmony_ci	rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
10688c2ecf20Sopenharmony_ci					      ecryptfs_dentry, &written,
10698c2ecf20Sopenharmony_ci					      max - offset);
10708c2ecf20Sopenharmony_ci	if (rc)
10718c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_WARNING, "Error generating key packet "
10728c2ecf20Sopenharmony_ci				"set; rc = [%d]\n", rc);
10738c2ecf20Sopenharmony_ci	if (size) {
10748c2ecf20Sopenharmony_ci		offset += written;
10758c2ecf20Sopenharmony_ci		*size = offset;
10768c2ecf20Sopenharmony_ci	}
10778c2ecf20Sopenharmony_ci	return rc;
10788c2ecf20Sopenharmony_ci}
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_cistatic int
10818c2ecf20Sopenharmony_ciecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
10828c2ecf20Sopenharmony_ci				    char *virt, size_t virt_len)
10838c2ecf20Sopenharmony_ci{
10848c2ecf20Sopenharmony_ci	int rc;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	rc = ecryptfs_write_lower(ecryptfs_inode, virt,
10878c2ecf20Sopenharmony_ci				  0, virt_len);
10888c2ecf20Sopenharmony_ci	if (rc < 0)
10898c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: Error attempting to write header "
10908c2ecf20Sopenharmony_ci		       "information to lower file; rc = [%d]\n", __func__, rc);
10918c2ecf20Sopenharmony_ci	else
10928c2ecf20Sopenharmony_ci		rc = 0;
10938c2ecf20Sopenharmony_ci	return rc;
10948c2ecf20Sopenharmony_ci}
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_cistatic int
10978c2ecf20Sopenharmony_ciecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
10988c2ecf20Sopenharmony_ci				 struct inode *ecryptfs_inode,
10998c2ecf20Sopenharmony_ci				 char *page_virt, size_t size)
11008c2ecf20Sopenharmony_ci{
11018c2ecf20Sopenharmony_ci	int rc;
11028c2ecf20Sopenharmony_ci	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
11038c2ecf20Sopenharmony_ci	struct inode *lower_inode = d_inode(lower_dentry);
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	if (!(lower_inode->i_opflags & IOP_XATTR)) {
11068c2ecf20Sopenharmony_ci		rc = -EOPNOTSUPP;
11078c2ecf20Sopenharmony_ci		goto out;
11088c2ecf20Sopenharmony_ci	}
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	inode_lock(lower_inode);
11118c2ecf20Sopenharmony_ci	rc = __vfs_setxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME,
11128c2ecf20Sopenharmony_ci			    page_virt, size, 0);
11138c2ecf20Sopenharmony_ci	if (!rc && ecryptfs_inode)
11148c2ecf20Sopenharmony_ci		fsstack_copy_attr_all(ecryptfs_inode, lower_inode);
11158c2ecf20Sopenharmony_ci	inode_unlock(lower_inode);
11168c2ecf20Sopenharmony_ciout:
11178c2ecf20Sopenharmony_ci	return rc;
11188c2ecf20Sopenharmony_ci}
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_cistatic unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
11218c2ecf20Sopenharmony_ci					       unsigned int order)
11228c2ecf20Sopenharmony_ci{
11238c2ecf20Sopenharmony_ci	struct page *page;
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	page = alloc_pages(gfp_mask | __GFP_ZERO, order);
11268c2ecf20Sopenharmony_ci	if (page)
11278c2ecf20Sopenharmony_ci		return (unsigned long) page_address(page);
11288c2ecf20Sopenharmony_ci	return 0;
11298c2ecf20Sopenharmony_ci}
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci/**
11328c2ecf20Sopenharmony_ci * ecryptfs_write_metadata
11338c2ecf20Sopenharmony_ci * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
11348c2ecf20Sopenharmony_ci * @ecryptfs_inode: The newly created eCryptfs inode
11358c2ecf20Sopenharmony_ci *
11368c2ecf20Sopenharmony_ci * Write the file headers out.  This will likely involve a userspace
11378c2ecf20Sopenharmony_ci * callout, in which the session key is encrypted with one or more
11388c2ecf20Sopenharmony_ci * public keys and/or the passphrase necessary to do the encryption is
11398c2ecf20Sopenharmony_ci * retrieved via a prompt.  Exactly what happens at this point should
11408c2ecf20Sopenharmony_ci * be policy-dependent.
11418c2ecf20Sopenharmony_ci *
11428c2ecf20Sopenharmony_ci * Returns zero on success; non-zero on error
11438c2ecf20Sopenharmony_ci */
11448c2ecf20Sopenharmony_ciint ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
11458c2ecf20Sopenharmony_ci			    struct inode *ecryptfs_inode)
11468c2ecf20Sopenharmony_ci{
11478c2ecf20Sopenharmony_ci	struct ecryptfs_crypt_stat *crypt_stat =
11488c2ecf20Sopenharmony_ci		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
11498c2ecf20Sopenharmony_ci	unsigned int order;
11508c2ecf20Sopenharmony_ci	char *virt;
11518c2ecf20Sopenharmony_ci	size_t virt_len;
11528c2ecf20Sopenharmony_ci	size_t size = 0;
11538c2ecf20Sopenharmony_ci	int rc = 0;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
11568c2ecf20Sopenharmony_ci		if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
11578c2ecf20Sopenharmony_ci			printk(KERN_ERR "Key is invalid; bailing out\n");
11588c2ecf20Sopenharmony_ci			rc = -EINVAL;
11598c2ecf20Sopenharmony_ci			goto out;
11608c2ecf20Sopenharmony_ci		}
11618c2ecf20Sopenharmony_ci	} else {
11628c2ecf20Sopenharmony_ci		printk(KERN_WARNING "%s: Encrypted flag not set\n",
11638c2ecf20Sopenharmony_ci		       __func__);
11648c2ecf20Sopenharmony_ci		rc = -EINVAL;
11658c2ecf20Sopenharmony_ci		goto out;
11668c2ecf20Sopenharmony_ci	}
11678c2ecf20Sopenharmony_ci	virt_len = crypt_stat->metadata_size;
11688c2ecf20Sopenharmony_ci	order = get_order(virt_len);
11698c2ecf20Sopenharmony_ci	/* Released in this function */
11708c2ecf20Sopenharmony_ci	virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
11718c2ecf20Sopenharmony_ci	if (!virt) {
11728c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: Out of memory\n", __func__);
11738c2ecf20Sopenharmony_ci		rc = -ENOMEM;
11748c2ecf20Sopenharmony_ci		goto out;
11758c2ecf20Sopenharmony_ci	}
11768c2ecf20Sopenharmony_ci	/* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
11778c2ecf20Sopenharmony_ci	rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
11788c2ecf20Sopenharmony_ci					 ecryptfs_dentry);
11798c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
11808c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
11818c2ecf20Sopenharmony_ci		       __func__, rc);
11828c2ecf20Sopenharmony_ci		goto out_free;
11838c2ecf20Sopenharmony_ci	}
11848c2ecf20Sopenharmony_ci	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
11858c2ecf20Sopenharmony_ci		rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, ecryptfs_inode,
11868c2ecf20Sopenharmony_ci						      virt, size);
11878c2ecf20Sopenharmony_ci	else
11888c2ecf20Sopenharmony_ci		rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
11898c2ecf20Sopenharmony_ci							 virt_len);
11908c2ecf20Sopenharmony_ci	if (rc) {
11918c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: Error writing metadata out to lower file; "
11928c2ecf20Sopenharmony_ci		       "rc = [%d]\n", __func__, rc);
11938c2ecf20Sopenharmony_ci		goto out_free;
11948c2ecf20Sopenharmony_ci	}
11958c2ecf20Sopenharmony_ciout_free:
11968c2ecf20Sopenharmony_ci	free_pages((unsigned long)virt, order);
11978c2ecf20Sopenharmony_ciout:
11988c2ecf20Sopenharmony_ci	return rc;
11998c2ecf20Sopenharmony_ci}
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
12028c2ecf20Sopenharmony_ci#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
12038c2ecf20Sopenharmony_cistatic int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
12048c2ecf20Sopenharmony_ci				 char *virt, int *bytes_read,
12058c2ecf20Sopenharmony_ci				 int validate_header_size)
12068c2ecf20Sopenharmony_ci{
12078c2ecf20Sopenharmony_ci	int rc = 0;
12088c2ecf20Sopenharmony_ci	u32 header_extent_size;
12098c2ecf20Sopenharmony_ci	u16 num_header_extents_at_front;
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci	header_extent_size = get_unaligned_be32(virt);
12128c2ecf20Sopenharmony_ci	virt += sizeof(__be32);
12138c2ecf20Sopenharmony_ci	num_header_extents_at_front = get_unaligned_be16(virt);
12148c2ecf20Sopenharmony_ci	crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
12158c2ecf20Sopenharmony_ci				     * (size_t)header_extent_size));
12168c2ecf20Sopenharmony_ci	(*bytes_read) = (sizeof(__be32) + sizeof(__be16));
12178c2ecf20Sopenharmony_ci	if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
12188c2ecf20Sopenharmony_ci	    && (crypt_stat->metadata_size
12198c2ecf20Sopenharmony_ci		< ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
12208c2ecf20Sopenharmony_ci		rc = -EINVAL;
12218c2ecf20Sopenharmony_ci		printk(KERN_WARNING "Invalid header size: [%zd]\n",
12228c2ecf20Sopenharmony_ci		       crypt_stat->metadata_size);
12238c2ecf20Sopenharmony_ci	}
12248c2ecf20Sopenharmony_ci	return rc;
12258c2ecf20Sopenharmony_ci}
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci/**
12288c2ecf20Sopenharmony_ci * set_default_header_data
12298c2ecf20Sopenharmony_ci * @crypt_stat: The cryptographic context
12308c2ecf20Sopenharmony_ci *
12318c2ecf20Sopenharmony_ci * For version 0 file format; this function is only for backwards
12328c2ecf20Sopenharmony_ci * compatibility for files created with the prior versions of
12338c2ecf20Sopenharmony_ci * eCryptfs.
12348c2ecf20Sopenharmony_ci */
12358c2ecf20Sopenharmony_cistatic void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
12368c2ecf20Sopenharmony_ci{
12378c2ecf20Sopenharmony_ci	crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
12388c2ecf20Sopenharmony_ci}
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_civoid ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
12418c2ecf20Sopenharmony_ci{
12428c2ecf20Sopenharmony_ci	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
12438c2ecf20Sopenharmony_ci	struct ecryptfs_crypt_stat *crypt_stat;
12448c2ecf20Sopenharmony_ci	u64 file_size;
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
12478c2ecf20Sopenharmony_ci	mount_crypt_stat =
12488c2ecf20Sopenharmony_ci		&ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
12498c2ecf20Sopenharmony_ci	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
12508c2ecf20Sopenharmony_ci		file_size = i_size_read(ecryptfs_inode_to_lower(inode));
12518c2ecf20Sopenharmony_ci		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
12528c2ecf20Sopenharmony_ci			file_size += crypt_stat->metadata_size;
12538c2ecf20Sopenharmony_ci	} else
12548c2ecf20Sopenharmony_ci		file_size = get_unaligned_be64(page_virt);
12558c2ecf20Sopenharmony_ci	i_size_write(inode, (loff_t)file_size);
12568c2ecf20Sopenharmony_ci	crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
12578c2ecf20Sopenharmony_ci}
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci/**
12608c2ecf20Sopenharmony_ci * ecryptfs_read_headers_virt
12618c2ecf20Sopenharmony_ci * @page_virt: The virtual address into which to read the headers
12628c2ecf20Sopenharmony_ci * @crypt_stat: The cryptographic context
12638c2ecf20Sopenharmony_ci * @ecryptfs_dentry: The eCryptfs dentry
12648c2ecf20Sopenharmony_ci * @validate_header_size: Whether to validate the header size while reading
12658c2ecf20Sopenharmony_ci *
12668c2ecf20Sopenharmony_ci * Read/parse the header data. The header format is detailed in the
12678c2ecf20Sopenharmony_ci * comment block for the ecryptfs_write_headers_virt() function.
12688c2ecf20Sopenharmony_ci *
12698c2ecf20Sopenharmony_ci * Returns zero on success
12708c2ecf20Sopenharmony_ci */
12718c2ecf20Sopenharmony_cistatic int ecryptfs_read_headers_virt(char *page_virt,
12728c2ecf20Sopenharmony_ci				      struct ecryptfs_crypt_stat *crypt_stat,
12738c2ecf20Sopenharmony_ci				      struct dentry *ecryptfs_dentry,
12748c2ecf20Sopenharmony_ci				      int validate_header_size)
12758c2ecf20Sopenharmony_ci{
12768c2ecf20Sopenharmony_ci	int rc = 0;
12778c2ecf20Sopenharmony_ci	int offset;
12788c2ecf20Sopenharmony_ci	int bytes_read;
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	ecryptfs_set_default_sizes(crypt_stat);
12818c2ecf20Sopenharmony_ci	crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
12828c2ecf20Sopenharmony_ci		ecryptfs_dentry->d_sb)->mount_crypt_stat;
12838c2ecf20Sopenharmony_ci	offset = ECRYPTFS_FILE_SIZE_BYTES;
12848c2ecf20Sopenharmony_ci	rc = ecryptfs_validate_marker(page_virt + offset);
12858c2ecf20Sopenharmony_ci	if (rc)
12868c2ecf20Sopenharmony_ci		goto out;
12878c2ecf20Sopenharmony_ci	if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
12888c2ecf20Sopenharmony_ci		ecryptfs_i_size_init(page_virt, d_inode(ecryptfs_dentry));
12898c2ecf20Sopenharmony_ci	offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
12908c2ecf20Sopenharmony_ci	ecryptfs_process_flags(crypt_stat, (page_virt + offset), &bytes_read);
12918c2ecf20Sopenharmony_ci	if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
12928c2ecf20Sopenharmony_ci		ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
12938c2ecf20Sopenharmony_ci				"file version [%d] is supported by this "
12948c2ecf20Sopenharmony_ci				"version of eCryptfs\n",
12958c2ecf20Sopenharmony_ci				crypt_stat->file_version,
12968c2ecf20Sopenharmony_ci				ECRYPTFS_SUPPORTED_FILE_VERSION);
12978c2ecf20Sopenharmony_ci		rc = -EINVAL;
12988c2ecf20Sopenharmony_ci		goto out;
12998c2ecf20Sopenharmony_ci	}
13008c2ecf20Sopenharmony_ci	offset += bytes_read;
13018c2ecf20Sopenharmony_ci	if (crypt_stat->file_version >= 1) {
13028c2ecf20Sopenharmony_ci		rc = parse_header_metadata(crypt_stat, (page_virt + offset),
13038c2ecf20Sopenharmony_ci					   &bytes_read, validate_header_size);
13048c2ecf20Sopenharmony_ci		if (rc) {
13058c2ecf20Sopenharmony_ci			ecryptfs_printk(KERN_WARNING, "Error reading header "
13068c2ecf20Sopenharmony_ci					"metadata; rc = [%d]\n", rc);
13078c2ecf20Sopenharmony_ci		}
13088c2ecf20Sopenharmony_ci		offset += bytes_read;
13098c2ecf20Sopenharmony_ci	} else
13108c2ecf20Sopenharmony_ci		set_default_header_data(crypt_stat);
13118c2ecf20Sopenharmony_ci	rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
13128c2ecf20Sopenharmony_ci				       ecryptfs_dentry);
13138c2ecf20Sopenharmony_ciout:
13148c2ecf20Sopenharmony_ci	return rc;
13158c2ecf20Sopenharmony_ci}
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci/**
13188c2ecf20Sopenharmony_ci * ecryptfs_read_xattr_region
13198c2ecf20Sopenharmony_ci * @page_virt: The vitual address into which to read the xattr data
13208c2ecf20Sopenharmony_ci * @ecryptfs_inode: The eCryptfs inode
13218c2ecf20Sopenharmony_ci *
13228c2ecf20Sopenharmony_ci * Attempts to read the crypto metadata from the extended attribute
13238c2ecf20Sopenharmony_ci * region of the lower file.
13248c2ecf20Sopenharmony_ci *
13258c2ecf20Sopenharmony_ci * Returns zero on success; non-zero on error
13268c2ecf20Sopenharmony_ci */
13278c2ecf20Sopenharmony_ciint ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
13288c2ecf20Sopenharmony_ci{
13298c2ecf20Sopenharmony_ci	struct dentry *lower_dentry =
13308c2ecf20Sopenharmony_ci		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
13318c2ecf20Sopenharmony_ci	ssize_t size;
13328c2ecf20Sopenharmony_ci	int rc = 0;
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	size = ecryptfs_getxattr_lower(lower_dentry,
13358c2ecf20Sopenharmony_ci				       ecryptfs_inode_to_lower(ecryptfs_inode),
13368c2ecf20Sopenharmony_ci				       ECRYPTFS_XATTR_NAME,
13378c2ecf20Sopenharmony_ci				       page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
13388c2ecf20Sopenharmony_ci	if (size < 0) {
13398c2ecf20Sopenharmony_ci		if (unlikely(ecryptfs_verbosity > 0))
13408c2ecf20Sopenharmony_ci			printk(KERN_INFO "Error attempting to read the [%s] "
13418c2ecf20Sopenharmony_ci			       "xattr from the lower file; return value = "
13428c2ecf20Sopenharmony_ci			       "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
13438c2ecf20Sopenharmony_ci		rc = -EINVAL;
13448c2ecf20Sopenharmony_ci		goto out;
13458c2ecf20Sopenharmony_ci	}
13468c2ecf20Sopenharmony_ciout:
13478c2ecf20Sopenharmony_ci	return rc;
13488c2ecf20Sopenharmony_ci}
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ciint ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
13518c2ecf20Sopenharmony_ci					    struct inode *inode)
13528c2ecf20Sopenharmony_ci{
13538c2ecf20Sopenharmony_ci	u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
13548c2ecf20Sopenharmony_ci	u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
13558c2ecf20Sopenharmony_ci	int rc;
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ci	rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
13588c2ecf20Sopenharmony_ci				     ecryptfs_inode_to_lower(inode),
13598c2ecf20Sopenharmony_ci				     ECRYPTFS_XATTR_NAME, file_size,
13608c2ecf20Sopenharmony_ci				     ECRYPTFS_SIZE_AND_MARKER_BYTES);
13618c2ecf20Sopenharmony_ci	if (rc < 0)
13628c2ecf20Sopenharmony_ci		return rc;
13638c2ecf20Sopenharmony_ci	else if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
13648c2ecf20Sopenharmony_ci		return -EINVAL;
13658c2ecf20Sopenharmony_ci	rc = ecryptfs_validate_marker(marker);
13668c2ecf20Sopenharmony_ci	if (!rc)
13678c2ecf20Sopenharmony_ci		ecryptfs_i_size_init(file_size, inode);
13688c2ecf20Sopenharmony_ci	return rc;
13698c2ecf20Sopenharmony_ci}
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci/**
13728c2ecf20Sopenharmony_ci * ecryptfs_read_metadata
13738c2ecf20Sopenharmony_ci *
13748c2ecf20Sopenharmony_ci * Common entry point for reading file metadata. From here, we could
13758c2ecf20Sopenharmony_ci * retrieve the header information from the header region of the file,
13768c2ecf20Sopenharmony_ci * the xattr region of the file, or some other repository that is
13778c2ecf20Sopenharmony_ci * stored separately from the file itself. The current implementation
13788c2ecf20Sopenharmony_ci * supports retrieving the metadata information from the file contents
13798c2ecf20Sopenharmony_ci * and from the xattr region.
13808c2ecf20Sopenharmony_ci *
13818c2ecf20Sopenharmony_ci * Returns zero if valid headers found and parsed; non-zero otherwise
13828c2ecf20Sopenharmony_ci */
13838c2ecf20Sopenharmony_ciint ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
13848c2ecf20Sopenharmony_ci{
13858c2ecf20Sopenharmony_ci	int rc;
13868c2ecf20Sopenharmony_ci	char *page_virt;
13878c2ecf20Sopenharmony_ci	struct inode *ecryptfs_inode = d_inode(ecryptfs_dentry);
13888c2ecf20Sopenharmony_ci	struct ecryptfs_crypt_stat *crypt_stat =
13898c2ecf20Sopenharmony_ci	    &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
13908c2ecf20Sopenharmony_ci	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
13918c2ecf20Sopenharmony_ci		&ecryptfs_superblock_to_private(
13928c2ecf20Sopenharmony_ci			ecryptfs_dentry->d_sb)->mount_crypt_stat;
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
13958c2ecf20Sopenharmony_ci						      mount_crypt_stat);
13968c2ecf20Sopenharmony_ci	/* Read the first page from the underlying file */
13978c2ecf20Sopenharmony_ci	page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
13988c2ecf20Sopenharmony_ci	if (!page_virt) {
13998c2ecf20Sopenharmony_ci		rc = -ENOMEM;
14008c2ecf20Sopenharmony_ci		goto out;
14018c2ecf20Sopenharmony_ci	}
14028c2ecf20Sopenharmony_ci	rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
14038c2ecf20Sopenharmony_ci				 ecryptfs_inode);
14048c2ecf20Sopenharmony_ci	if (rc >= 0)
14058c2ecf20Sopenharmony_ci		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
14068c2ecf20Sopenharmony_ci						ecryptfs_dentry,
14078c2ecf20Sopenharmony_ci						ECRYPTFS_VALIDATE_HEADER_SIZE);
14088c2ecf20Sopenharmony_ci	if (rc) {
14098c2ecf20Sopenharmony_ci		/* metadata is not in the file header, so try xattrs */
14108c2ecf20Sopenharmony_ci		memset(page_virt, 0, PAGE_SIZE);
14118c2ecf20Sopenharmony_ci		rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
14128c2ecf20Sopenharmony_ci		if (rc) {
14138c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
14148c2ecf20Sopenharmony_ci			       "file header region or xattr region, inode %lu\n",
14158c2ecf20Sopenharmony_ci				ecryptfs_inode->i_ino);
14168c2ecf20Sopenharmony_ci			rc = -EINVAL;
14178c2ecf20Sopenharmony_ci			goto out;
14188c2ecf20Sopenharmony_ci		}
14198c2ecf20Sopenharmony_ci		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
14208c2ecf20Sopenharmony_ci						ecryptfs_dentry,
14218c2ecf20Sopenharmony_ci						ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
14228c2ecf20Sopenharmony_ci		if (rc) {
14238c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
14248c2ecf20Sopenharmony_ci			       "file xattr region either, inode %lu\n",
14258c2ecf20Sopenharmony_ci				ecryptfs_inode->i_ino);
14268c2ecf20Sopenharmony_ci			rc = -EINVAL;
14278c2ecf20Sopenharmony_ci		}
14288c2ecf20Sopenharmony_ci		if (crypt_stat->mount_crypt_stat->flags
14298c2ecf20Sopenharmony_ci		    & ECRYPTFS_XATTR_METADATA_ENABLED) {
14308c2ecf20Sopenharmony_ci			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
14318c2ecf20Sopenharmony_ci		} else {
14328c2ecf20Sopenharmony_ci			printk(KERN_WARNING "Attempt to access file with "
14338c2ecf20Sopenharmony_ci			       "crypto metadata only in the extended attribute "
14348c2ecf20Sopenharmony_ci			       "region, but eCryptfs was mounted without "
14358c2ecf20Sopenharmony_ci			       "xattr support enabled. eCryptfs will not treat "
14368c2ecf20Sopenharmony_ci			       "this like an encrypted file, inode %lu\n",
14378c2ecf20Sopenharmony_ci				ecryptfs_inode->i_ino);
14388c2ecf20Sopenharmony_ci			rc = -EINVAL;
14398c2ecf20Sopenharmony_ci		}
14408c2ecf20Sopenharmony_ci	}
14418c2ecf20Sopenharmony_ciout:
14428c2ecf20Sopenharmony_ci	if (page_virt) {
14438c2ecf20Sopenharmony_ci		memset(page_virt, 0, PAGE_SIZE);
14448c2ecf20Sopenharmony_ci		kmem_cache_free(ecryptfs_header_cache, page_virt);
14458c2ecf20Sopenharmony_ci	}
14468c2ecf20Sopenharmony_ci	return rc;
14478c2ecf20Sopenharmony_ci}
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci/**
14508c2ecf20Sopenharmony_ci * ecryptfs_encrypt_filename - encrypt filename
14518c2ecf20Sopenharmony_ci *
14528c2ecf20Sopenharmony_ci * CBC-encrypts the filename. We do not want to encrypt the same
14538c2ecf20Sopenharmony_ci * filename with the same key and IV, which may happen with hard
14548c2ecf20Sopenharmony_ci * links, so we prepend random bits to each filename.
14558c2ecf20Sopenharmony_ci *
14568c2ecf20Sopenharmony_ci * Returns zero on success; non-zero otherwise
14578c2ecf20Sopenharmony_ci */
14588c2ecf20Sopenharmony_cistatic int
14598c2ecf20Sopenharmony_ciecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
14608c2ecf20Sopenharmony_ci			  struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
14618c2ecf20Sopenharmony_ci{
14628c2ecf20Sopenharmony_ci	int rc = 0;
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	filename->encrypted_filename = NULL;
14658c2ecf20Sopenharmony_ci	filename->encrypted_filename_size = 0;
14668c2ecf20Sopenharmony_ci	if (mount_crypt_stat && (mount_crypt_stat->flags
14678c2ecf20Sopenharmony_ci				     & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) {
14688c2ecf20Sopenharmony_ci		size_t packet_size;
14698c2ecf20Sopenharmony_ci		size_t remaining_bytes;
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci		rc = ecryptfs_write_tag_70_packet(
14728c2ecf20Sopenharmony_ci			NULL, NULL,
14738c2ecf20Sopenharmony_ci			&filename->encrypted_filename_size,
14748c2ecf20Sopenharmony_ci			mount_crypt_stat, NULL,
14758c2ecf20Sopenharmony_ci			filename->filename_size);
14768c2ecf20Sopenharmony_ci		if (rc) {
14778c2ecf20Sopenharmony_ci			printk(KERN_ERR "%s: Error attempting to get packet "
14788c2ecf20Sopenharmony_ci			       "size for tag 72; rc = [%d]\n", __func__,
14798c2ecf20Sopenharmony_ci			       rc);
14808c2ecf20Sopenharmony_ci			filename->encrypted_filename_size = 0;
14818c2ecf20Sopenharmony_ci			goto out;
14828c2ecf20Sopenharmony_ci		}
14838c2ecf20Sopenharmony_ci		filename->encrypted_filename =
14848c2ecf20Sopenharmony_ci			kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
14858c2ecf20Sopenharmony_ci		if (!filename->encrypted_filename) {
14868c2ecf20Sopenharmony_ci			rc = -ENOMEM;
14878c2ecf20Sopenharmony_ci			goto out;
14888c2ecf20Sopenharmony_ci		}
14898c2ecf20Sopenharmony_ci		remaining_bytes = filename->encrypted_filename_size;
14908c2ecf20Sopenharmony_ci		rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
14918c2ecf20Sopenharmony_ci						  &remaining_bytes,
14928c2ecf20Sopenharmony_ci						  &packet_size,
14938c2ecf20Sopenharmony_ci						  mount_crypt_stat,
14948c2ecf20Sopenharmony_ci						  filename->filename,
14958c2ecf20Sopenharmony_ci						  filename->filename_size);
14968c2ecf20Sopenharmony_ci		if (rc) {
14978c2ecf20Sopenharmony_ci			printk(KERN_ERR "%s: Error attempting to generate "
14988c2ecf20Sopenharmony_ci			       "tag 70 packet; rc = [%d]\n", __func__,
14998c2ecf20Sopenharmony_ci			       rc);
15008c2ecf20Sopenharmony_ci			kfree(filename->encrypted_filename);
15018c2ecf20Sopenharmony_ci			filename->encrypted_filename = NULL;
15028c2ecf20Sopenharmony_ci			filename->encrypted_filename_size = 0;
15038c2ecf20Sopenharmony_ci			goto out;
15048c2ecf20Sopenharmony_ci		}
15058c2ecf20Sopenharmony_ci		filename->encrypted_filename_size = packet_size;
15068c2ecf20Sopenharmony_ci	} else {
15078c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: No support for requested filename "
15088c2ecf20Sopenharmony_ci		       "encryption method in this release\n", __func__);
15098c2ecf20Sopenharmony_ci		rc = -EOPNOTSUPP;
15108c2ecf20Sopenharmony_ci		goto out;
15118c2ecf20Sopenharmony_ci	}
15128c2ecf20Sopenharmony_ciout:
15138c2ecf20Sopenharmony_ci	return rc;
15148c2ecf20Sopenharmony_ci}
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_cistatic int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
15178c2ecf20Sopenharmony_ci				  const char *name, size_t name_size)
15188c2ecf20Sopenharmony_ci{
15198c2ecf20Sopenharmony_ci	int rc = 0;
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_ci	(*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
15228c2ecf20Sopenharmony_ci	if (!(*copied_name)) {
15238c2ecf20Sopenharmony_ci		rc = -ENOMEM;
15248c2ecf20Sopenharmony_ci		goto out;
15258c2ecf20Sopenharmony_ci	}
15268c2ecf20Sopenharmony_ci	memcpy((void *)(*copied_name), (void *)name, name_size);
15278c2ecf20Sopenharmony_ci	(*copied_name)[(name_size)] = '\0';	/* Only for convenience
15288c2ecf20Sopenharmony_ci						 * in printing out the
15298c2ecf20Sopenharmony_ci						 * string in debug
15308c2ecf20Sopenharmony_ci						 * messages */
15318c2ecf20Sopenharmony_ci	(*copied_name_size) = name_size;
15328c2ecf20Sopenharmony_ciout:
15338c2ecf20Sopenharmony_ci	return rc;
15348c2ecf20Sopenharmony_ci}
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci/**
15378c2ecf20Sopenharmony_ci * ecryptfs_process_key_cipher - Perform key cipher initialization.
15388c2ecf20Sopenharmony_ci * @key_tfm: Crypto context for key material, set by this function
15398c2ecf20Sopenharmony_ci * @cipher_name: Name of the cipher
15408c2ecf20Sopenharmony_ci * @key_size: Size of the key in bytes
15418c2ecf20Sopenharmony_ci *
15428c2ecf20Sopenharmony_ci * Returns zero on success. Any crypto_tfm structs allocated here
15438c2ecf20Sopenharmony_ci * should be released by other functions, such as on a superblock put
15448c2ecf20Sopenharmony_ci * event, regardless of whether this function succeeds for fails.
15458c2ecf20Sopenharmony_ci */
15468c2ecf20Sopenharmony_cistatic int
15478c2ecf20Sopenharmony_ciecryptfs_process_key_cipher(struct crypto_skcipher **key_tfm,
15488c2ecf20Sopenharmony_ci			    char *cipher_name, size_t *key_size)
15498c2ecf20Sopenharmony_ci{
15508c2ecf20Sopenharmony_ci	char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
15518c2ecf20Sopenharmony_ci	char *full_alg_name = NULL;
15528c2ecf20Sopenharmony_ci	int rc;
15538c2ecf20Sopenharmony_ci
15548c2ecf20Sopenharmony_ci	*key_tfm = NULL;
15558c2ecf20Sopenharmony_ci	if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
15568c2ecf20Sopenharmony_ci		rc = -EINVAL;
15578c2ecf20Sopenharmony_ci		printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
15588c2ecf20Sopenharmony_ci		      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
15598c2ecf20Sopenharmony_ci		goto out;
15608c2ecf20Sopenharmony_ci	}
15618c2ecf20Sopenharmony_ci	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
15628c2ecf20Sopenharmony_ci						    "ecb");
15638c2ecf20Sopenharmony_ci	if (rc)
15648c2ecf20Sopenharmony_ci		goto out;
15658c2ecf20Sopenharmony_ci	*key_tfm = crypto_alloc_skcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
15668c2ecf20Sopenharmony_ci	if (IS_ERR(*key_tfm)) {
15678c2ecf20Sopenharmony_ci		rc = PTR_ERR(*key_tfm);
15688c2ecf20Sopenharmony_ci		printk(KERN_ERR "Unable to allocate crypto cipher with name "
15698c2ecf20Sopenharmony_ci		       "[%s]; rc = [%d]\n", full_alg_name, rc);
15708c2ecf20Sopenharmony_ci		goto out;
15718c2ecf20Sopenharmony_ci	}
15728c2ecf20Sopenharmony_ci	crypto_skcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
15738c2ecf20Sopenharmony_ci	if (*key_size == 0)
15748c2ecf20Sopenharmony_ci		*key_size = crypto_skcipher_max_keysize(*key_tfm);
15758c2ecf20Sopenharmony_ci	get_random_bytes(dummy_key, *key_size);
15768c2ecf20Sopenharmony_ci	rc = crypto_skcipher_setkey(*key_tfm, dummy_key, *key_size);
15778c2ecf20Sopenharmony_ci	if (rc) {
15788c2ecf20Sopenharmony_ci		printk(KERN_ERR "Error attempting to set key of size [%zd] for "
15798c2ecf20Sopenharmony_ci		       "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
15808c2ecf20Sopenharmony_ci		       rc);
15818c2ecf20Sopenharmony_ci		rc = -EINVAL;
15828c2ecf20Sopenharmony_ci		goto out;
15838c2ecf20Sopenharmony_ci	}
15848c2ecf20Sopenharmony_ciout:
15858c2ecf20Sopenharmony_ci	kfree(full_alg_name);
15868c2ecf20Sopenharmony_ci	return rc;
15878c2ecf20Sopenharmony_ci}
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_cistruct kmem_cache *ecryptfs_key_tfm_cache;
15908c2ecf20Sopenharmony_cistatic struct list_head key_tfm_list;
15918c2ecf20Sopenharmony_cistruct mutex key_tfm_list_mutex;
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ciint __init ecryptfs_init_crypto(void)
15948c2ecf20Sopenharmony_ci{
15958c2ecf20Sopenharmony_ci	mutex_init(&key_tfm_list_mutex);
15968c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&key_tfm_list);
15978c2ecf20Sopenharmony_ci	return 0;
15988c2ecf20Sopenharmony_ci}
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci/**
16018c2ecf20Sopenharmony_ci * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
16028c2ecf20Sopenharmony_ci *
16038c2ecf20Sopenharmony_ci * Called only at module unload time
16048c2ecf20Sopenharmony_ci */
16058c2ecf20Sopenharmony_ciint ecryptfs_destroy_crypto(void)
16068c2ecf20Sopenharmony_ci{
16078c2ecf20Sopenharmony_ci	struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_ci	mutex_lock(&key_tfm_list_mutex);
16108c2ecf20Sopenharmony_ci	list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
16118c2ecf20Sopenharmony_ci				 key_tfm_list) {
16128c2ecf20Sopenharmony_ci		list_del(&key_tfm->key_tfm_list);
16138c2ecf20Sopenharmony_ci		crypto_free_skcipher(key_tfm->key_tfm);
16148c2ecf20Sopenharmony_ci		kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
16158c2ecf20Sopenharmony_ci	}
16168c2ecf20Sopenharmony_ci	mutex_unlock(&key_tfm_list_mutex);
16178c2ecf20Sopenharmony_ci	return 0;
16188c2ecf20Sopenharmony_ci}
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ciint
16218c2ecf20Sopenharmony_ciecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
16228c2ecf20Sopenharmony_ci			 size_t key_size)
16238c2ecf20Sopenharmony_ci{
16248c2ecf20Sopenharmony_ci	struct ecryptfs_key_tfm *tmp_tfm;
16258c2ecf20Sopenharmony_ci	int rc = 0;
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_ci	tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
16308c2ecf20Sopenharmony_ci	if (key_tfm)
16318c2ecf20Sopenharmony_ci		(*key_tfm) = tmp_tfm;
16328c2ecf20Sopenharmony_ci	if (!tmp_tfm) {
16338c2ecf20Sopenharmony_ci		rc = -ENOMEM;
16348c2ecf20Sopenharmony_ci		goto out;
16358c2ecf20Sopenharmony_ci	}
16368c2ecf20Sopenharmony_ci	mutex_init(&tmp_tfm->key_tfm_mutex);
16378c2ecf20Sopenharmony_ci	strncpy(tmp_tfm->cipher_name, cipher_name,
16388c2ecf20Sopenharmony_ci		ECRYPTFS_MAX_CIPHER_NAME_SIZE);
16398c2ecf20Sopenharmony_ci	tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
16408c2ecf20Sopenharmony_ci	tmp_tfm->key_size = key_size;
16418c2ecf20Sopenharmony_ci	rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
16428c2ecf20Sopenharmony_ci					 tmp_tfm->cipher_name,
16438c2ecf20Sopenharmony_ci					 &tmp_tfm->key_size);
16448c2ecf20Sopenharmony_ci	if (rc) {
16458c2ecf20Sopenharmony_ci		printk(KERN_ERR "Error attempting to initialize key TFM "
16468c2ecf20Sopenharmony_ci		       "cipher with name = [%s]; rc = [%d]\n",
16478c2ecf20Sopenharmony_ci		       tmp_tfm->cipher_name, rc);
16488c2ecf20Sopenharmony_ci		kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
16498c2ecf20Sopenharmony_ci		if (key_tfm)
16508c2ecf20Sopenharmony_ci			(*key_tfm) = NULL;
16518c2ecf20Sopenharmony_ci		goto out;
16528c2ecf20Sopenharmony_ci	}
16538c2ecf20Sopenharmony_ci	list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
16548c2ecf20Sopenharmony_ciout:
16558c2ecf20Sopenharmony_ci	return rc;
16568c2ecf20Sopenharmony_ci}
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_ci/**
16598c2ecf20Sopenharmony_ci * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
16608c2ecf20Sopenharmony_ci * @cipher_name: the name of the cipher to search for
16618c2ecf20Sopenharmony_ci * @key_tfm: set to corresponding tfm if found
16628c2ecf20Sopenharmony_ci *
16638c2ecf20Sopenharmony_ci * Searches for cached key_tfm matching @cipher_name
16648c2ecf20Sopenharmony_ci * Must be called with &key_tfm_list_mutex held
16658c2ecf20Sopenharmony_ci * Returns 1 if found, with @key_tfm set
16668c2ecf20Sopenharmony_ci * Returns 0 if not found, with @key_tfm set to NULL
16678c2ecf20Sopenharmony_ci */
16688c2ecf20Sopenharmony_ciint ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
16698c2ecf20Sopenharmony_ci{
16708c2ecf20Sopenharmony_ci	struct ecryptfs_key_tfm *tmp_key_tfm;
16718c2ecf20Sopenharmony_ci
16728c2ecf20Sopenharmony_ci	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
16758c2ecf20Sopenharmony_ci		if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
16768c2ecf20Sopenharmony_ci			if (key_tfm)
16778c2ecf20Sopenharmony_ci				(*key_tfm) = tmp_key_tfm;
16788c2ecf20Sopenharmony_ci			return 1;
16798c2ecf20Sopenharmony_ci		}
16808c2ecf20Sopenharmony_ci	}
16818c2ecf20Sopenharmony_ci	if (key_tfm)
16828c2ecf20Sopenharmony_ci		(*key_tfm) = NULL;
16838c2ecf20Sopenharmony_ci	return 0;
16848c2ecf20Sopenharmony_ci}
16858c2ecf20Sopenharmony_ci
16868c2ecf20Sopenharmony_ci/**
16878c2ecf20Sopenharmony_ci * ecryptfs_get_tfm_and_mutex_for_cipher_name
16888c2ecf20Sopenharmony_ci *
16898c2ecf20Sopenharmony_ci * @tfm: set to cached tfm found, or new tfm created
16908c2ecf20Sopenharmony_ci * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
16918c2ecf20Sopenharmony_ci * @cipher_name: the name of the cipher to search for and/or add
16928c2ecf20Sopenharmony_ci *
16938c2ecf20Sopenharmony_ci * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
16948c2ecf20Sopenharmony_ci * Searches for cached item first, and creates new if not found.
16958c2ecf20Sopenharmony_ci * Returns 0 on success, non-zero if adding new cipher failed
16968c2ecf20Sopenharmony_ci */
16978c2ecf20Sopenharmony_ciint ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_skcipher **tfm,
16988c2ecf20Sopenharmony_ci					       struct mutex **tfm_mutex,
16998c2ecf20Sopenharmony_ci					       char *cipher_name)
17008c2ecf20Sopenharmony_ci{
17018c2ecf20Sopenharmony_ci	struct ecryptfs_key_tfm *key_tfm;
17028c2ecf20Sopenharmony_ci	int rc = 0;
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_ci	(*tfm) = NULL;
17058c2ecf20Sopenharmony_ci	(*tfm_mutex) = NULL;
17068c2ecf20Sopenharmony_ci
17078c2ecf20Sopenharmony_ci	mutex_lock(&key_tfm_list_mutex);
17088c2ecf20Sopenharmony_ci	if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
17098c2ecf20Sopenharmony_ci		rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
17108c2ecf20Sopenharmony_ci		if (rc) {
17118c2ecf20Sopenharmony_ci			printk(KERN_ERR "Error adding new key_tfm to list; "
17128c2ecf20Sopenharmony_ci					"rc = [%d]\n", rc);
17138c2ecf20Sopenharmony_ci			goto out;
17148c2ecf20Sopenharmony_ci		}
17158c2ecf20Sopenharmony_ci	}
17168c2ecf20Sopenharmony_ci	(*tfm) = key_tfm->key_tfm;
17178c2ecf20Sopenharmony_ci	(*tfm_mutex) = &key_tfm->key_tfm_mutex;
17188c2ecf20Sopenharmony_ciout:
17198c2ecf20Sopenharmony_ci	mutex_unlock(&key_tfm_list_mutex);
17208c2ecf20Sopenharmony_ci	return rc;
17218c2ecf20Sopenharmony_ci}
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci/* 64 characters forming a 6-bit target field */
17248c2ecf20Sopenharmony_cistatic unsigned char *portable_filename_chars = ("-.0123456789ABCD"
17258c2ecf20Sopenharmony_ci						 "EFGHIJKLMNOPQRST"
17268c2ecf20Sopenharmony_ci						 "UVWXYZabcdefghij"
17278c2ecf20Sopenharmony_ci						 "klmnopqrstuvwxyz");
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ci/* We could either offset on every reverse map or just pad some 0x00's
17308c2ecf20Sopenharmony_ci * at the front here */
17318c2ecf20Sopenharmony_cistatic const unsigned char filename_rev_map[256] = {
17328c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
17338c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
17348c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
17358c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
17368c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
17378c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
17388c2ecf20Sopenharmony_ci	0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
17398c2ecf20Sopenharmony_ci	0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
17408c2ecf20Sopenharmony_ci	0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
17418c2ecf20Sopenharmony_ci	0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
17428c2ecf20Sopenharmony_ci	0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
17438c2ecf20Sopenharmony_ci	0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
17448c2ecf20Sopenharmony_ci	0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
17458c2ecf20Sopenharmony_ci	0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
17468c2ecf20Sopenharmony_ci	0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
17478c2ecf20Sopenharmony_ci	0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
17488c2ecf20Sopenharmony_ci};
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_ci/**
17518c2ecf20Sopenharmony_ci * ecryptfs_encode_for_filename
17528c2ecf20Sopenharmony_ci * @dst: Destination location for encoded filename
17538c2ecf20Sopenharmony_ci * @dst_size: Size of the encoded filename in bytes
17548c2ecf20Sopenharmony_ci * @src: Source location for the filename to encode
17558c2ecf20Sopenharmony_ci * @src_size: Size of the source in bytes
17568c2ecf20Sopenharmony_ci */
17578c2ecf20Sopenharmony_cistatic void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
17588c2ecf20Sopenharmony_ci				  unsigned char *src, size_t src_size)
17598c2ecf20Sopenharmony_ci{
17608c2ecf20Sopenharmony_ci	size_t num_blocks;
17618c2ecf20Sopenharmony_ci	size_t block_num = 0;
17628c2ecf20Sopenharmony_ci	size_t dst_offset = 0;
17638c2ecf20Sopenharmony_ci	unsigned char last_block[3];
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci	if (src_size == 0) {
17668c2ecf20Sopenharmony_ci		(*dst_size) = 0;
17678c2ecf20Sopenharmony_ci		goto out;
17688c2ecf20Sopenharmony_ci	}
17698c2ecf20Sopenharmony_ci	num_blocks = (src_size / 3);
17708c2ecf20Sopenharmony_ci	if ((src_size % 3) == 0) {
17718c2ecf20Sopenharmony_ci		memcpy(last_block, (&src[src_size - 3]), 3);
17728c2ecf20Sopenharmony_ci	} else {
17738c2ecf20Sopenharmony_ci		num_blocks++;
17748c2ecf20Sopenharmony_ci		last_block[2] = 0x00;
17758c2ecf20Sopenharmony_ci		switch (src_size % 3) {
17768c2ecf20Sopenharmony_ci		case 1:
17778c2ecf20Sopenharmony_ci			last_block[0] = src[src_size - 1];
17788c2ecf20Sopenharmony_ci			last_block[1] = 0x00;
17798c2ecf20Sopenharmony_ci			break;
17808c2ecf20Sopenharmony_ci		case 2:
17818c2ecf20Sopenharmony_ci			last_block[0] = src[src_size - 2];
17828c2ecf20Sopenharmony_ci			last_block[1] = src[src_size - 1];
17838c2ecf20Sopenharmony_ci		}
17848c2ecf20Sopenharmony_ci	}
17858c2ecf20Sopenharmony_ci	(*dst_size) = (num_blocks * 4);
17868c2ecf20Sopenharmony_ci	if (!dst)
17878c2ecf20Sopenharmony_ci		goto out;
17888c2ecf20Sopenharmony_ci	while (block_num < num_blocks) {
17898c2ecf20Sopenharmony_ci		unsigned char *src_block;
17908c2ecf20Sopenharmony_ci		unsigned char dst_block[4];
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_ci		if (block_num == (num_blocks - 1))
17938c2ecf20Sopenharmony_ci			src_block = last_block;
17948c2ecf20Sopenharmony_ci		else
17958c2ecf20Sopenharmony_ci			src_block = &src[block_num * 3];
17968c2ecf20Sopenharmony_ci		dst_block[0] = ((src_block[0] >> 2) & 0x3F);
17978c2ecf20Sopenharmony_ci		dst_block[1] = (((src_block[0] << 4) & 0x30)
17988c2ecf20Sopenharmony_ci				| ((src_block[1] >> 4) & 0x0F));
17998c2ecf20Sopenharmony_ci		dst_block[2] = (((src_block[1] << 2) & 0x3C)
18008c2ecf20Sopenharmony_ci				| ((src_block[2] >> 6) & 0x03));
18018c2ecf20Sopenharmony_ci		dst_block[3] = (src_block[2] & 0x3F);
18028c2ecf20Sopenharmony_ci		dst[dst_offset++] = portable_filename_chars[dst_block[0]];
18038c2ecf20Sopenharmony_ci		dst[dst_offset++] = portable_filename_chars[dst_block[1]];
18048c2ecf20Sopenharmony_ci		dst[dst_offset++] = portable_filename_chars[dst_block[2]];
18058c2ecf20Sopenharmony_ci		dst[dst_offset++] = portable_filename_chars[dst_block[3]];
18068c2ecf20Sopenharmony_ci		block_num++;
18078c2ecf20Sopenharmony_ci	}
18088c2ecf20Sopenharmony_ciout:
18098c2ecf20Sopenharmony_ci	return;
18108c2ecf20Sopenharmony_ci}
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_cistatic size_t ecryptfs_max_decoded_size(size_t encoded_size)
18138c2ecf20Sopenharmony_ci{
18148c2ecf20Sopenharmony_ci	/* Not exact; conservatively long. Every block of 4
18158c2ecf20Sopenharmony_ci	 * encoded characters decodes into a block of 3
18168c2ecf20Sopenharmony_ci	 * decoded characters. This segment of code provides
18178c2ecf20Sopenharmony_ci	 * the caller with the maximum amount of allocated
18188c2ecf20Sopenharmony_ci	 * space that @dst will need to point to in a
18198c2ecf20Sopenharmony_ci	 * subsequent call. */
18208c2ecf20Sopenharmony_ci	return ((encoded_size + 1) * 3) / 4;
18218c2ecf20Sopenharmony_ci}
18228c2ecf20Sopenharmony_ci
18238c2ecf20Sopenharmony_ci/**
18248c2ecf20Sopenharmony_ci * ecryptfs_decode_from_filename
18258c2ecf20Sopenharmony_ci * @dst: If NULL, this function only sets @dst_size and returns. If
18268c2ecf20Sopenharmony_ci *       non-NULL, this function decodes the encoded octets in @src
18278c2ecf20Sopenharmony_ci *       into the memory that @dst points to.
18288c2ecf20Sopenharmony_ci * @dst_size: Set to the size of the decoded string.
18298c2ecf20Sopenharmony_ci * @src: The encoded set of octets to decode.
18308c2ecf20Sopenharmony_ci * @src_size: The size of the encoded set of octets to decode.
18318c2ecf20Sopenharmony_ci */
18328c2ecf20Sopenharmony_cistatic void
18338c2ecf20Sopenharmony_ciecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
18348c2ecf20Sopenharmony_ci			      const unsigned char *src, size_t src_size)
18358c2ecf20Sopenharmony_ci{
18368c2ecf20Sopenharmony_ci	u8 current_bit_offset = 0;
18378c2ecf20Sopenharmony_ci	size_t src_byte_offset = 0;
18388c2ecf20Sopenharmony_ci	size_t dst_byte_offset = 0;
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci	if (!dst) {
18418c2ecf20Sopenharmony_ci		(*dst_size) = ecryptfs_max_decoded_size(src_size);
18428c2ecf20Sopenharmony_ci		goto out;
18438c2ecf20Sopenharmony_ci	}
18448c2ecf20Sopenharmony_ci	while (src_byte_offset < src_size) {
18458c2ecf20Sopenharmony_ci		unsigned char src_byte =
18468c2ecf20Sopenharmony_ci				filename_rev_map[(int)src[src_byte_offset]];
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci		switch (current_bit_offset) {
18498c2ecf20Sopenharmony_ci		case 0:
18508c2ecf20Sopenharmony_ci			dst[dst_byte_offset] = (src_byte << 2);
18518c2ecf20Sopenharmony_ci			current_bit_offset = 6;
18528c2ecf20Sopenharmony_ci			break;
18538c2ecf20Sopenharmony_ci		case 6:
18548c2ecf20Sopenharmony_ci			dst[dst_byte_offset++] |= (src_byte >> 4);
18558c2ecf20Sopenharmony_ci			dst[dst_byte_offset] = ((src_byte & 0xF)
18568c2ecf20Sopenharmony_ci						 << 4);
18578c2ecf20Sopenharmony_ci			current_bit_offset = 4;
18588c2ecf20Sopenharmony_ci			break;
18598c2ecf20Sopenharmony_ci		case 4:
18608c2ecf20Sopenharmony_ci			dst[dst_byte_offset++] |= (src_byte >> 2);
18618c2ecf20Sopenharmony_ci			dst[dst_byte_offset] = (src_byte << 6);
18628c2ecf20Sopenharmony_ci			current_bit_offset = 2;
18638c2ecf20Sopenharmony_ci			break;
18648c2ecf20Sopenharmony_ci		case 2:
18658c2ecf20Sopenharmony_ci			dst[dst_byte_offset++] |= (src_byte);
18668c2ecf20Sopenharmony_ci			current_bit_offset = 0;
18678c2ecf20Sopenharmony_ci			break;
18688c2ecf20Sopenharmony_ci		}
18698c2ecf20Sopenharmony_ci		src_byte_offset++;
18708c2ecf20Sopenharmony_ci	}
18718c2ecf20Sopenharmony_ci	(*dst_size) = dst_byte_offset;
18728c2ecf20Sopenharmony_ciout:
18738c2ecf20Sopenharmony_ci	return;
18748c2ecf20Sopenharmony_ci}
18758c2ecf20Sopenharmony_ci
18768c2ecf20Sopenharmony_ci/**
18778c2ecf20Sopenharmony_ci * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
18788c2ecf20Sopenharmony_ci * @crypt_stat: The crypt_stat struct associated with the file anem to encode
18798c2ecf20Sopenharmony_ci * @name: The plaintext name
18808c2ecf20Sopenharmony_ci * @length: The length of the plaintext
18818c2ecf20Sopenharmony_ci * @encoded_name: The encypted name
18828c2ecf20Sopenharmony_ci *
18838c2ecf20Sopenharmony_ci * Encrypts and encodes a filename into something that constitutes a
18848c2ecf20Sopenharmony_ci * valid filename for a filesystem, with printable characters.
18858c2ecf20Sopenharmony_ci *
18868c2ecf20Sopenharmony_ci * We assume that we have a properly initialized crypto context,
18878c2ecf20Sopenharmony_ci * pointed to by crypt_stat->tfm.
18888c2ecf20Sopenharmony_ci *
18898c2ecf20Sopenharmony_ci * Returns zero on success; non-zero on otherwise
18908c2ecf20Sopenharmony_ci */
18918c2ecf20Sopenharmony_ciint ecryptfs_encrypt_and_encode_filename(
18928c2ecf20Sopenharmony_ci	char **encoded_name,
18938c2ecf20Sopenharmony_ci	size_t *encoded_name_size,
18948c2ecf20Sopenharmony_ci	struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
18958c2ecf20Sopenharmony_ci	const char *name, size_t name_size)
18968c2ecf20Sopenharmony_ci{
18978c2ecf20Sopenharmony_ci	size_t encoded_name_no_prefix_size;
18988c2ecf20Sopenharmony_ci	int rc = 0;
18998c2ecf20Sopenharmony_ci
19008c2ecf20Sopenharmony_ci	(*encoded_name) = NULL;
19018c2ecf20Sopenharmony_ci	(*encoded_name_size) = 0;
19028c2ecf20Sopenharmony_ci	if (mount_crypt_stat && (mount_crypt_stat->flags
19038c2ecf20Sopenharmony_ci				     & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
19048c2ecf20Sopenharmony_ci		struct ecryptfs_filename *filename;
19058c2ecf20Sopenharmony_ci
19068c2ecf20Sopenharmony_ci		filename = kzalloc(sizeof(*filename), GFP_KERNEL);
19078c2ecf20Sopenharmony_ci		if (!filename) {
19088c2ecf20Sopenharmony_ci			rc = -ENOMEM;
19098c2ecf20Sopenharmony_ci			goto out;
19108c2ecf20Sopenharmony_ci		}
19118c2ecf20Sopenharmony_ci		filename->filename = (char *)name;
19128c2ecf20Sopenharmony_ci		filename->filename_size = name_size;
19138c2ecf20Sopenharmony_ci		rc = ecryptfs_encrypt_filename(filename, mount_crypt_stat);
19148c2ecf20Sopenharmony_ci		if (rc) {
19158c2ecf20Sopenharmony_ci			printk(KERN_ERR "%s: Error attempting to encrypt "
19168c2ecf20Sopenharmony_ci			       "filename; rc = [%d]\n", __func__, rc);
19178c2ecf20Sopenharmony_ci			kfree(filename);
19188c2ecf20Sopenharmony_ci			goto out;
19198c2ecf20Sopenharmony_ci		}
19208c2ecf20Sopenharmony_ci		ecryptfs_encode_for_filename(
19218c2ecf20Sopenharmony_ci			NULL, &encoded_name_no_prefix_size,
19228c2ecf20Sopenharmony_ci			filename->encrypted_filename,
19238c2ecf20Sopenharmony_ci			filename->encrypted_filename_size);
19248c2ecf20Sopenharmony_ci		if (mount_crypt_stat
19258c2ecf20Sopenharmony_ci			&& (mount_crypt_stat->flags
19268c2ecf20Sopenharmony_ci			    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))
19278c2ecf20Sopenharmony_ci			(*encoded_name_size) =
19288c2ecf20Sopenharmony_ci				(ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
19298c2ecf20Sopenharmony_ci				 + encoded_name_no_prefix_size);
19308c2ecf20Sopenharmony_ci		else
19318c2ecf20Sopenharmony_ci			(*encoded_name_size) =
19328c2ecf20Sopenharmony_ci				(ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
19338c2ecf20Sopenharmony_ci				 + encoded_name_no_prefix_size);
19348c2ecf20Sopenharmony_ci		(*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
19358c2ecf20Sopenharmony_ci		if (!(*encoded_name)) {
19368c2ecf20Sopenharmony_ci			rc = -ENOMEM;
19378c2ecf20Sopenharmony_ci			kfree(filename->encrypted_filename);
19388c2ecf20Sopenharmony_ci			kfree(filename);
19398c2ecf20Sopenharmony_ci			goto out;
19408c2ecf20Sopenharmony_ci		}
19418c2ecf20Sopenharmony_ci		if (mount_crypt_stat
19428c2ecf20Sopenharmony_ci			&& (mount_crypt_stat->flags
19438c2ecf20Sopenharmony_ci			    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) {
19448c2ecf20Sopenharmony_ci			memcpy((*encoded_name),
19458c2ecf20Sopenharmony_ci			       ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
19468c2ecf20Sopenharmony_ci			       ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
19478c2ecf20Sopenharmony_ci			ecryptfs_encode_for_filename(
19488c2ecf20Sopenharmony_ci			    ((*encoded_name)
19498c2ecf20Sopenharmony_ci			     + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
19508c2ecf20Sopenharmony_ci			    &encoded_name_no_prefix_size,
19518c2ecf20Sopenharmony_ci			    filename->encrypted_filename,
19528c2ecf20Sopenharmony_ci			    filename->encrypted_filename_size);
19538c2ecf20Sopenharmony_ci			(*encoded_name_size) =
19548c2ecf20Sopenharmony_ci				(ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
19558c2ecf20Sopenharmony_ci				 + encoded_name_no_prefix_size);
19568c2ecf20Sopenharmony_ci			(*encoded_name)[(*encoded_name_size)] = '\0';
19578c2ecf20Sopenharmony_ci		} else {
19588c2ecf20Sopenharmony_ci			rc = -EOPNOTSUPP;
19598c2ecf20Sopenharmony_ci		}
19608c2ecf20Sopenharmony_ci		if (rc) {
19618c2ecf20Sopenharmony_ci			printk(KERN_ERR "%s: Error attempting to encode "
19628c2ecf20Sopenharmony_ci			       "encrypted filename; rc = [%d]\n", __func__,
19638c2ecf20Sopenharmony_ci			       rc);
19648c2ecf20Sopenharmony_ci			kfree((*encoded_name));
19658c2ecf20Sopenharmony_ci			(*encoded_name) = NULL;
19668c2ecf20Sopenharmony_ci			(*encoded_name_size) = 0;
19678c2ecf20Sopenharmony_ci		}
19688c2ecf20Sopenharmony_ci		kfree(filename->encrypted_filename);
19698c2ecf20Sopenharmony_ci		kfree(filename);
19708c2ecf20Sopenharmony_ci	} else {
19718c2ecf20Sopenharmony_ci		rc = ecryptfs_copy_filename(encoded_name,
19728c2ecf20Sopenharmony_ci					    encoded_name_size,
19738c2ecf20Sopenharmony_ci					    name, name_size);
19748c2ecf20Sopenharmony_ci	}
19758c2ecf20Sopenharmony_ciout:
19768c2ecf20Sopenharmony_ci	return rc;
19778c2ecf20Sopenharmony_ci}
19788c2ecf20Sopenharmony_ci
19798c2ecf20Sopenharmony_cistatic bool is_dot_dotdot(const char *name, size_t name_size)
19808c2ecf20Sopenharmony_ci{
19818c2ecf20Sopenharmony_ci	if (name_size == 1 && name[0] == '.')
19828c2ecf20Sopenharmony_ci		return true;
19838c2ecf20Sopenharmony_ci	else if (name_size == 2 && name[0] == '.' && name[1] == '.')
19848c2ecf20Sopenharmony_ci		return true;
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_ci	return false;
19878c2ecf20Sopenharmony_ci}
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_ci/**
19908c2ecf20Sopenharmony_ci * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
19918c2ecf20Sopenharmony_ci * @plaintext_name: The plaintext name
19928c2ecf20Sopenharmony_ci * @plaintext_name_size: The plaintext name size
19938c2ecf20Sopenharmony_ci * @ecryptfs_dir_dentry: eCryptfs directory dentry
19948c2ecf20Sopenharmony_ci * @name: The filename in cipher text
19958c2ecf20Sopenharmony_ci * @name_size: The cipher text name size
19968c2ecf20Sopenharmony_ci *
19978c2ecf20Sopenharmony_ci * Decrypts and decodes the filename.
19988c2ecf20Sopenharmony_ci *
19998c2ecf20Sopenharmony_ci * Returns zero on error; non-zero otherwise
20008c2ecf20Sopenharmony_ci */
20018c2ecf20Sopenharmony_ciint ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
20028c2ecf20Sopenharmony_ci					 size_t *plaintext_name_size,
20038c2ecf20Sopenharmony_ci					 struct super_block *sb,
20048c2ecf20Sopenharmony_ci					 const char *name, size_t name_size)
20058c2ecf20Sopenharmony_ci{
20068c2ecf20Sopenharmony_ci	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
20078c2ecf20Sopenharmony_ci		&ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
20088c2ecf20Sopenharmony_ci	char *decoded_name;
20098c2ecf20Sopenharmony_ci	size_t decoded_name_size;
20108c2ecf20Sopenharmony_ci	size_t packet_size;
20118c2ecf20Sopenharmony_ci	int rc = 0;
20128c2ecf20Sopenharmony_ci
20138c2ecf20Sopenharmony_ci	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) &&
20148c2ecf20Sopenharmony_ci	    !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)) {
20158c2ecf20Sopenharmony_ci		if (is_dot_dotdot(name, name_size)) {
20168c2ecf20Sopenharmony_ci			rc = ecryptfs_copy_filename(plaintext_name,
20178c2ecf20Sopenharmony_ci						    plaintext_name_size,
20188c2ecf20Sopenharmony_ci						    name, name_size);
20198c2ecf20Sopenharmony_ci			goto out;
20208c2ecf20Sopenharmony_ci		}
20218c2ecf20Sopenharmony_ci
20228c2ecf20Sopenharmony_ci		if (name_size <= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE ||
20238c2ecf20Sopenharmony_ci		    strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
20248c2ecf20Sopenharmony_ci			    ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)) {
20258c2ecf20Sopenharmony_ci			rc = -EINVAL;
20268c2ecf20Sopenharmony_ci			goto out;
20278c2ecf20Sopenharmony_ci		}
20288c2ecf20Sopenharmony_ci
20298c2ecf20Sopenharmony_ci		name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
20308c2ecf20Sopenharmony_ci		name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
20318c2ecf20Sopenharmony_ci		ecryptfs_decode_from_filename(NULL, &decoded_name_size,
20328c2ecf20Sopenharmony_ci					      name, name_size);
20338c2ecf20Sopenharmony_ci		decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
20348c2ecf20Sopenharmony_ci		if (!decoded_name) {
20358c2ecf20Sopenharmony_ci			rc = -ENOMEM;
20368c2ecf20Sopenharmony_ci			goto out;
20378c2ecf20Sopenharmony_ci		}
20388c2ecf20Sopenharmony_ci		ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
20398c2ecf20Sopenharmony_ci					      name, name_size);
20408c2ecf20Sopenharmony_ci		rc = ecryptfs_parse_tag_70_packet(plaintext_name,
20418c2ecf20Sopenharmony_ci						  plaintext_name_size,
20428c2ecf20Sopenharmony_ci						  &packet_size,
20438c2ecf20Sopenharmony_ci						  mount_crypt_stat,
20448c2ecf20Sopenharmony_ci						  decoded_name,
20458c2ecf20Sopenharmony_ci						  decoded_name_size);
20468c2ecf20Sopenharmony_ci		if (rc) {
20478c2ecf20Sopenharmony_ci			ecryptfs_printk(KERN_DEBUG,
20488c2ecf20Sopenharmony_ci					"%s: Could not parse tag 70 packet from filename\n",
20498c2ecf20Sopenharmony_ci					__func__);
20508c2ecf20Sopenharmony_ci			goto out_free;
20518c2ecf20Sopenharmony_ci		}
20528c2ecf20Sopenharmony_ci	} else {
20538c2ecf20Sopenharmony_ci		rc = ecryptfs_copy_filename(plaintext_name,
20548c2ecf20Sopenharmony_ci					    plaintext_name_size,
20558c2ecf20Sopenharmony_ci					    name, name_size);
20568c2ecf20Sopenharmony_ci		goto out;
20578c2ecf20Sopenharmony_ci	}
20588c2ecf20Sopenharmony_ciout_free:
20598c2ecf20Sopenharmony_ci	kfree(decoded_name);
20608c2ecf20Sopenharmony_ciout:
20618c2ecf20Sopenharmony_ci	return rc;
20628c2ecf20Sopenharmony_ci}
20638c2ecf20Sopenharmony_ci
20648c2ecf20Sopenharmony_ci#define ENC_NAME_MAX_BLOCKLEN_8_OR_16	143
20658c2ecf20Sopenharmony_ci
20668c2ecf20Sopenharmony_ciint ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
20678c2ecf20Sopenharmony_ci			   struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
20688c2ecf20Sopenharmony_ci{
20698c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm;
20708c2ecf20Sopenharmony_ci	struct mutex *tfm_mutex;
20718c2ecf20Sopenharmony_ci	size_t cipher_blocksize;
20728c2ecf20Sopenharmony_ci	int rc;
20738c2ecf20Sopenharmony_ci
20748c2ecf20Sopenharmony_ci	if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
20758c2ecf20Sopenharmony_ci		(*namelen) = lower_namelen;
20768c2ecf20Sopenharmony_ci		return 0;
20778c2ecf20Sopenharmony_ci	}
20788c2ecf20Sopenharmony_ci
20798c2ecf20Sopenharmony_ci	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
20808c2ecf20Sopenharmony_ci			mount_crypt_stat->global_default_fn_cipher_name);
20818c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
20828c2ecf20Sopenharmony_ci		(*namelen) = 0;
20838c2ecf20Sopenharmony_ci		return rc;
20848c2ecf20Sopenharmony_ci	}
20858c2ecf20Sopenharmony_ci
20868c2ecf20Sopenharmony_ci	mutex_lock(tfm_mutex);
20878c2ecf20Sopenharmony_ci	cipher_blocksize = crypto_skcipher_blocksize(tfm);
20888c2ecf20Sopenharmony_ci	mutex_unlock(tfm_mutex);
20898c2ecf20Sopenharmony_ci
20908c2ecf20Sopenharmony_ci	/* Return an exact amount for the common cases */
20918c2ecf20Sopenharmony_ci	if (lower_namelen == NAME_MAX
20928c2ecf20Sopenharmony_ci	    && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
20938c2ecf20Sopenharmony_ci		(*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
20948c2ecf20Sopenharmony_ci		return 0;
20958c2ecf20Sopenharmony_ci	}
20968c2ecf20Sopenharmony_ci
20978c2ecf20Sopenharmony_ci	/* Return a safe estimate for the uncommon cases */
20988c2ecf20Sopenharmony_ci	(*namelen) = lower_namelen;
20998c2ecf20Sopenharmony_ci	(*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
21008c2ecf20Sopenharmony_ci	/* Since this is the max decoded size, subtract 1 "decoded block" len */
21018c2ecf20Sopenharmony_ci	(*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
21028c2ecf20Sopenharmony_ci	(*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
21038c2ecf20Sopenharmony_ci	(*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
21048c2ecf20Sopenharmony_ci	/* Worst case is that the filename is padded nearly a full block size */
21058c2ecf20Sopenharmony_ci	(*namelen) -= cipher_blocksize - 1;
21068c2ecf20Sopenharmony_ci
21078c2ecf20Sopenharmony_ci	if ((*namelen) < 0)
21088c2ecf20Sopenharmony_ci		(*namelen) = 0;
21098c2ecf20Sopenharmony_ci
21108c2ecf20Sopenharmony_ci	return 0;
21118c2ecf20Sopenharmony_ci}
2112