xref: /openbmc/linux/fs/ecryptfs/crypto.c (revision 12003e5b18ca33807b3f9448309ec92184192b85)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  *
4237fead6SMichael Halcrow  * Copyright (C) 1997-2004 Erez Zadok
5237fead6SMichael Halcrow  * Copyright (C) 2001-2004 Stony Brook University
6dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
7237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8237fead6SMichael Halcrow  *   		Michael C. Thompson <mcthomps@us.ibm.com>
9237fead6SMichael Halcrow  *
10237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
11237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
12237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
13237fead6SMichael Halcrow  * License, or (at your option) any later version.
14237fead6SMichael Halcrow  *
15237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
16237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
17237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18237fead6SMichael Halcrow  * General Public License for more details.
19237fead6SMichael Halcrow  *
20237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
21237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
22237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23237fead6SMichael Halcrow  * 02111-1307, USA.
24237fead6SMichael Halcrow  */
25237fead6SMichael Halcrow 
26237fead6SMichael Halcrow #include <linux/fs.h>
27237fead6SMichael Halcrow #include <linux/mount.h>
28237fead6SMichael Halcrow #include <linux/pagemap.h>
29237fead6SMichael Halcrow #include <linux/random.h>
30237fead6SMichael Halcrow #include <linux/compiler.h>
31237fead6SMichael Halcrow #include <linux/key.h>
32237fead6SMichael Halcrow #include <linux/namei.h>
33237fead6SMichael Halcrow #include <linux/crypto.h>
34237fead6SMichael Halcrow #include <linux/file.h>
35237fead6SMichael Halcrow #include <linux/scatterlist.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
3729335c6aSHarvey Harrison #include <asm/unaligned.h>
38237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
39237fead6SMichael Halcrow 
40237fead6SMichael Halcrow static int
41237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
42237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
43237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
44237fead6SMichael Halcrow 			     unsigned char *iv);
45237fead6SMichael Halcrow static int
46237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
47237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
48237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
49237fead6SMichael Halcrow 			     unsigned char *iv);
50237fead6SMichael Halcrow 
51237fead6SMichael Halcrow /**
52237fead6SMichael Halcrow  * ecryptfs_to_hex
53237fead6SMichael Halcrow  * @dst: Buffer to take hex character representation of contents of
54237fead6SMichael Halcrow  *       src; must be at least of size (src_size * 2)
55237fead6SMichael Halcrow  * @src: Buffer to be converted to a hex string respresentation
56237fead6SMichael Halcrow  * @src_size: number of bytes to convert
57237fead6SMichael Halcrow  */
58237fead6SMichael Halcrow void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
59237fead6SMichael Halcrow {
60237fead6SMichael Halcrow 	int x;
61237fead6SMichael Halcrow 
62237fead6SMichael Halcrow 	for (x = 0; x < src_size; x++)
63237fead6SMichael Halcrow 		sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
64237fead6SMichael Halcrow }
65237fead6SMichael Halcrow 
66237fead6SMichael Halcrow /**
67237fead6SMichael Halcrow  * ecryptfs_from_hex
68237fead6SMichael Halcrow  * @dst: Buffer to take the bytes from src hex; must be at least of
69237fead6SMichael Halcrow  *       size (src_size / 2)
70237fead6SMichael Halcrow  * @src: Buffer to be converted from a hex string respresentation to raw value
71237fead6SMichael Halcrow  * @dst_size: size of dst buffer, or number of hex characters pairs to convert
72237fead6SMichael Halcrow  */
73237fead6SMichael Halcrow void ecryptfs_from_hex(char *dst, char *src, int dst_size)
74237fead6SMichael Halcrow {
75237fead6SMichael Halcrow 	int x;
76237fead6SMichael Halcrow 	char tmp[3] = { 0, };
77237fead6SMichael Halcrow 
78237fead6SMichael Halcrow 	for (x = 0; x < dst_size; x++) {
79237fead6SMichael Halcrow 		tmp[0] = src[x * 2];
80237fead6SMichael Halcrow 		tmp[1] = src[x * 2 + 1];
81237fead6SMichael Halcrow 		dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
82237fead6SMichael Halcrow 	}
83237fead6SMichael Halcrow }
84237fead6SMichael Halcrow 
85237fead6SMichael Halcrow /**
86237fead6SMichael Halcrow  * ecryptfs_calculate_md5 - calculates the md5 of @src
87237fead6SMichael Halcrow  * @dst: Pointer to 16 bytes of allocated memory
88237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
89237fead6SMichael Halcrow  * @src: Data to be md5'd
90237fead6SMichael Halcrow  * @len: Length of @src
91237fead6SMichael Halcrow  *
92237fead6SMichael Halcrow  * Uses the allocated crypto context that crypt_stat references to
93237fead6SMichael Halcrow  * generate the MD5 sum of the contents of src.
94237fead6SMichael Halcrow  */
95237fead6SMichael Halcrow static int ecryptfs_calculate_md5(char *dst,
96237fead6SMichael Halcrow 				  struct ecryptfs_crypt_stat *crypt_stat,
97237fead6SMichael Halcrow 				  char *src, int len)
98237fead6SMichael Halcrow {
99237fead6SMichael Halcrow 	struct scatterlist sg;
100565d9724SMichael Halcrow 	struct hash_desc desc = {
101565d9724SMichael Halcrow 		.tfm = crypt_stat->hash_tfm,
102565d9724SMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
103565d9724SMichael Halcrow 	};
104565d9724SMichael Halcrow 	int rc = 0;
105237fead6SMichael Halcrow 
106565d9724SMichael Halcrow 	mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
107237fead6SMichael Halcrow 	sg_init_one(&sg, (u8 *)src, len);
108565d9724SMichael Halcrow 	if (!desc.tfm) {
109565d9724SMichael Halcrow 		desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
110565d9724SMichael Halcrow 					     CRYPTO_ALG_ASYNC);
111565d9724SMichael Halcrow 		if (IS_ERR(desc.tfm)) {
112565d9724SMichael Halcrow 			rc = PTR_ERR(desc.tfm);
113237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
114565d9724SMichael Halcrow 					"allocate crypto context; rc = [%d]\n",
115565d9724SMichael Halcrow 					rc);
116237fead6SMichael Halcrow 			goto out;
117237fead6SMichael Halcrow 		}
118565d9724SMichael Halcrow 		crypt_stat->hash_tfm = desc.tfm;
119237fead6SMichael Halcrow 	}
1208a29f2b0SMichael Halcrow 	rc = crypto_hash_init(&desc);
1218a29f2b0SMichael Halcrow 	if (rc) {
1228a29f2b0SMichael Halcrow 		printk(KERN_ERR
1238a29f2b0SMichael Halcrow 		       "%s: Error initializing crypto hash; rc = [%d]\n",
12418d1dbf1SHarvey Harrison 		       __func__, rc);
1258a29f2b0SMichael Halcrow 		goto out;
1268a29f2b0SMichael Halcrow 	}
1278a29f2b0SMichael Halcrow 	rc = crypto_hash_update(&desc, &sg, len);
1288a29f2b0SMichael Halcrow 	if (rc) {
1298a29f2b0SMichael Halcrow 		printk(KERN_ERR
1308a29f2b0SMichael Halcrow 		       "%s: Error updating crypto hash; rc = [%d]\n",
13118d1dbf1SHarvey Harrison 		       __func__, rc);
1328a29f2b0SMichael Halcrow 		goto out;
1338a29f2b0SMichael Halcrow 	}
1348a29f2b0SMichael Halcrow 	rc = crypto_hash_final(&desc, dst);
1358a29f2b0SMichael Halcrow 	if (rc) {
1368a29f2b0SMichael Halcrow 		printk(KERN_ERR
1378a29f2b0SMichael Halcrow 		       "%s: Error finalizing crypto hash; rc = [%d]\n",
13818d1dbf1SHarvey Harrison 		       __func__, rc);
1398a29f2b0SMichael Halcrow 		goto out;
1408a29f2b0SMichael Halcrow 	}
141237fead6SMichael Halcrow out:
1428a29f2b0SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
143237fead6SMichael Halcrow 	return rc;
144237fead6SMichael Halcrow }
145237fead6SMichael Halcrow 
146cd9d67dfSMichael Halcrow static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
1478bba066fSMichael Halcrow 						  char *cipher_name,
1488bba066fSMichael Halcrow 						  char *chaining_modifier)
1498bba066fSMichael Halcrow {
1508bba066fSMichael Halcrow 	int cipher_name_len = strlen(cipher_name);
1518bba066fSMichael Halcrow 	int chaining_modifier_len = strlen(chaining_modifier);
1528bba066fSMichael Halcrow 	int algified_name_len;
1538bba066fSMichael Halcrow 	int rc;
1548bba066fSMichael Halcrow 
1558bba066fSMichael Halcrow 	algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
1568bba066fSMichael Halcrow 	(*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
1577bd473fcSMichael Halcrow 	if (!(*algified_name)) {
1588bba066fSMichael Halcrow 		rc = -ENOMEM;
1598bba066fSMichael Halcrow 		goto out;
1608bba066fSMichael Halcrow 	}
1618bba066fSMichael Halcrow 	snprintf((*algified_name), algified_name_len, "%s(%s)",
1628bba066fSMichael Halcrow 		 chaining_modifier, cipher_name);
1638bba066fSMichael Halcrow 	rc = 0;
1648bba066fSMichael Halcrow out:
1658bba066fSMichael Halcrow 	return rc;
1668bba066fSMichael Halcrow }
1678bba066fSMichael Halcrow 
168237fead6SMichael Halcrow /**
169237fead6SMichael Halcrow  * ecryptfs_derive_iv
170237fead6SMichael Halcrow  * @iv: destination for the derived iv vale
171237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
172d6a13c17SMichael Halcrow  * @offset: Offset of the extent whose IV we are to derive
173237fead6SMichael Halcrow  *
174237fead6SMichael Halcrow  * Generate the initialization vector from the given root IV and page
175237fead6SMichael Halcrow  * offset.
176237fead6SMichael Halcrow  *
177237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
178237fead6SMichael Halcrow  */
179a34f60f7SMichael Halcrow int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
180d6a13c17SMichael Halcrow 		       loff_t offset)
181237fead6SMichael Halcrow {
182237fead6SMichael Halcrow 	int rc = 0;
183237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
184237fead6SMichael Halcrow 	char src[ECRYPTFS_MAX_IV_BYTES + 16];
185237fead6SMichael Halcrow 
186237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
187237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "root iv:\n");
188237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
189237fead6SMichael Halcrow 	}
190237fead6SMichael Halcrow 	/* TODO: It is probably secure to just cast the least
191237fead6SMichael Halcrow 	 * significant bits of the root IV into an unsigned long and
192237fead6SMichael Halcrow 	 * add the offset to that rather than go through all this
193237fead6SMichael Halcrow 	 * hashing business. -Halcrow */
194237fead6SMichael Halcrow 	memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
195237fead6SMichael Halcrow 	memset((src + crypt_stat->iv_bytes), 0, 16);
196d6a13c17SMichael Halcrow 	snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
197237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
198237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "source:\n");
199237fead6SMichael Halcrow 		ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
200237fead6SMichael Halcrow 	}
201237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
202237fead6SMichael Halcrow 				    (crypt_stat->iv_bytes + 16));
203237fead6SMichael Halcrow 	if (rc) {
204237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
205237fead6SMichael Halcrow 				"MD5 while generating IV for a page\n");
206237fead6SMichael Halcrow 		goto out;
207237fead6SMichael Halcrow 	}
208237fead6SMichael Halcrow 	memcpy(iv, dst, crypt_stat->iv_bytes);
209237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
210237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
211237fead6SMichael Halcrow 		ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
212237fead6SMichael Halcrow 	}
213237fead6SMichael Halcrow out:
214237fead6SMichael Halcrow 	return rc;
215237fead6SMichael Halcrow }
216237fead6SMichael Halcrow 
217237fead6SMichael Halcrow /**
218237fead6SMichael Halcrow  * ecryptfs_init_crypt_stat
219237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
220237fead6SMichael Halcrow  *
221237fead6SMichael Halcrow  * Initialize the crypt_stat structure.
222237fead6SMichael Halcrow  */
223237fead6SMichael Halcrow void
224237fead6SMichael Halcrow ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
225237fead6SMichael Halcrow {
226237fead6SMichael Halcrow 	memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
227f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&crypt_stat->keysig_list);
228f4aad16aSMichael Halcrow 	mutex_init(&crypt_stat->keysig_list_mutex);
229237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_mutex);
230237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_tfm_mutex);
231565d9724SMichael Halcrow 	mutex_init(&crypt_stat->cs_hash_tfm_mutex);
232e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
233237fead6SMichael Halcrow }
234237fead6SMichael Halcrow 
235237fead6SMichael Halcrow /**
236fcd12835SMichael Halcrow  * ecryptfs_destroy_crypt_stat
237237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
238237fead6SMichael Halcrow  *
239237fead6SMichael Halcrow  * Releases all memory associated with a crypt_stat struct.
240237fead6SMichael Halcrow  */
241fcd12835SMichael Halcrow void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
242237fead6SMichael Halcrow {
243f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
244f4aad16aSMichael Halcrow 
245237fead6SMichael Halcrow 	if (crypt_stat->tfm)
2464dfea4f0STyler Hicks 		crypto_free_ablkcipher(crypt_stat->tfm);
247565d9724SMichael Halcrow 	if (crypt_stat->hash_tfm)
248565d9724SMichael Halcrow 		crypto_free_hash(crypt_stat->hash_tfm);
249f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_sig, key_sig_tmp,
250f4aad16aSMichael Halcrow 				 &crypt_stat->keysig_list, crypt_stat_list) {
251f4aad16aSMichael Halcrow 		list_del(&key_sig->crypt_stat_list);
252f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
253f4aad16aSMichael Halcrow 	}
254237fead6SMichael Halcrow 	memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
255237fead6SMichael Halcrow }
256237fead6SMichael Halcrow 
257fcd12835SMichael Halcrow void ecryptfs_destroy_mount_crypt_stat(
258237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
259237fead6SMichael Halcrow {
260f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
261f4aad16aSMichael Halcrow 
262f4aad16aSMichael Halcrow 	if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
263f4aad16aSMichael Halcrow 		return;
264f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
265f4aad16aSMichael Halcrow 	list_for_each_entry_safe(auth_tok, auth_tok_tmp,
266f4aad16aSMichael Halcrow 				 &mount_crypt_stat->global_auth_tok_list,
267f4aad16aSMichael Halcrow 				 mount_crypt_stat_list) {
268f4aad16aSMichael Halcrow 		list_del(&auth_tok->mount_crypt_stat_list);
269f4aad16aSMichael Halcrow 		if (auth_tok->global_auth_tok_key
270f4aad16aSMichael Halcrow 		    && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
271f4aad16aSMichael Halcrow 			key_put(auth_tok->global_auth_tok_key);
272f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
273f4aad16aSMichael Halcrow 	}
274f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
275237fead6SMichael Halcrow 	memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
276237fead6SMichael Halcrow }
277237fead6SMichael Halcrow 
278237fead6SMichael Halcrow /**
279237fead6SMichael Halcrow  * virt_to_scatterlist
280237fead6SMichael Halcrow  * @addr: Virtual address
281237fead6SMichael Halcrow  * @size: Size of data; should be an even multiple of the block size
282237fead6SMichael Halcrow  * @sg: Pointer to scatterlist array; set to NULL to obtain only
283237fead6SMichael Halcrow  *      the number of scatterlist structs required in array
284237fead6SMichael Halcrow  * @sg_size: Max array size
285237fead6SMichael Halcrow  *
286237fead6SMichael Halcrow  * Fills in a scatterlist array with page references for a passed
287237fead6SMichael Halcrow  * virtual address.
288237fead6SMichael Halcrow  *
289237fead6SMichael Halcrow  * Returns the number of scatterlist structs in array used
290237fead6SMichael Halcrow  */
291237fead6SMichael Halcrow int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
292237fead6SMichael Halcrow 			int sg_size)
293237fead6SMichael Halcrow {
294237fead6SMichael Halcrow 	int i = 0;
295237fead6SMichael Halcrow 	struct page *pg;
296237fead6SMichael Halcrow 	int offset;
297237fead6SMichael Halcrow 	int remainder_of_page;
298237fead6SMichael Halcrow 
29968e3f5ddSHerbert Xu 	sg_init_table(sg, sg_size);
30068e3f5ddSHerbert Xu 
301237fead6SMichael Halcrow 	while (size > 0 && i < sg_size) {
302237fead6SMichael Halcrow 		pg = virt_to_page(addr);
303237fead6SMichael Halcrow 		offset = offset_in_page(addr);
304642f1490SJens Axboe 		sg_set_page(&sg[i], pg, 0, offset);
305237fead6SMichael Halcrow 		remainder_of_page = PAGE_CACHE_SIZE - offset;
306237fead6SMichael Halcrow 		if (size >= remainder_of_page) {
307237fead6SMichael Halcrow 			sg[i].length = remainder_of_page;
308237fead6SMichael Halcrow 			addr += remainder_of_page;
309237fead6SMichael Halcrow 			size -= remainder_of_page;
310237fead6SMichael Halcrow 		} else {
311237fead6SMichael Halcrow 			sg[i].length = size;
312237fead6SMichael Halcrow 			addr += size;
313237fead6SMichael Halcrow 			size = 0;
314237fead6SMichael Halcrow 		}
315237fead6SMichael Halcrow 		i++;
316237fead6SMichael Halcrow 	}
317237fead6SMichael Halcrow 	if (size > 0)
318237fead6SMichael Halcrow 		return -ENOMEM;
319237fead6SMichael Halcrow 	return i;
320237fead6SMichael Halcrow }
321237fead6SMichael Halcrow 
3224dfea4f0STyler Hicks struct extent_crypt_result {
3234dfea4f0STyler Hicks 	struct completion completion;
3244dfea4f0STyler Hicks 	int rc;
3254dfea4f0STyler Hicks };
3264dfea4f0STyler Hicks 
3274dfea4f0STyler Hicks static void extent_crypt_complete(struct crypto_async_request *req, int rc)
3284dfea4f0STyler Hicks {
3294dfea4f0STyler Hicks 	struct extent_crypt_result *ecr = req->data;
3304dfea4f0STyler Hicks 
3314dfea4f0STyler Hicks 	if (rc == -EINPROGRESS)
3324dfea4f0STyler Hicks 		return;
3334dfea4f0STyler Hicks 
3344dfea4f0STyler Hicks 	ecr->rc = rc;
3354dfea4f0STyler Hicks 	complete(&ecr->completion);
3364dfea4f0STyler Hicks }
3374dfea4f0STyler Hicks 
338237fead6SMichael Halcrow /**
339237fead6SMichael Halcrow  * encrypt_scatterlist
340237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
341237fead6SMichael Halcrow  * @dest_sg: Destination of encrypted data
342237fead6SMichael Halcrow  * @src_sg: Data to be encrypted
343237fead6SMichael Halcrow  * @size: Length of data to be encrypted
344237fead6SMichael Halcrow  * @iv: iv to use during encryption
345237fead6SMichael Halcrow  *
346237fead6SMichael Halcrow  * Returns the number of bytes encrypted; negative value on error
347237fead6SMichael Halcrow  */
348237fead6SMichael Halcrow static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
349237fead6SMichael Halcrow 			       struct scatterlist *dest_sg,
350237fead6SMichael Halcrow 			       struct scatterlist *src_sg, int size,
351237fead6SMichael Halcrow 			       unsigned char *iv)
352237fead6SMichael Halcrow {
3534dfea4f0STyler Hicks 	struct ablkcipher_request *req = NULL;
3544dfea4f0STyler Hicks 	struct extent_crypt_result ecr;
355237fead6SMichael Halcrow 	int rc = 0;
356237fead6SMichael Halcrow 
357237fead6SMichael Halcrow 	BUG_ON(!crypt_stat || !crypt_stat->tfm
358e2bd99ecSMichael Halcrow 	       || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
359237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
360f24b3887STyler Hicks 		ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
361237fead6SMichael Halcrow 				crypt_stat->key_size);
362237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
363237fead6SMichael Halcrow 				  crypt_stat->key_size);
364237fead6SMichael Halcrow 	}
3654dfea4f0STyler Hicks 
3664dfea4f0STyler Hicks 	init_completion(&ecr.completion);
3674dfea4f0STyler Hicks 
368237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
3694dfea4f0STyler Hicks 	req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
3704dfea4f0STyler Hicks 	if (!req) {
3714dfea4f0STyler Hicks 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
3724dfea4f0STyler Hicks 		rc = -ENOMEM;
3734dfea4f0STyler Hicks 		goto out;
3748e3a6f16STrevor Highland 	}
3754dfea4f0STyler Hicks 
3764dfea4f0STyler Hicks 	ablkcipher_request_set_callback(req,
3774dfea4f0STyler Hicks 			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
3784dfea4f0STyler Hicks 			extent_crypt_complete, &ecr);
3794dfea4f0STyler Hicks 	/* Consider doing this once, when the file is opened */
3804dfea4f0STyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
3814dfea4f0STyler Hicks 		rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
3824dfea4f0STyler Hicks 					      crypt_stat->key_size);
383237fead6SMichael Halcrow 		if (rc) {
3844dfea4f0STyler Hicks 			ecryptfs_printk(KERN_ERR,
3854dfea4f0STyler Hicks 					"Error setting key; rc = [%d]\n",
386237fead6SMichael Halcrow 					rc);
387237fead6SMichael Halcrow 			mutex_unlock(&crypt_stat->cs_tfm_mutex);
388237fead6SMichael Halcrow 			rc = -EINVAL;
389237fead6SMichael Halcrow 			goto out;
390237fead6SMichael Halcrow 		}
3914dfea4f0STyler Hicks 		crypt_stat->flags |= ECRYPTFS_KEY_SET;
3924dfea4f0STyler Hicks 	}
393237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
3944dfea4f0STyler Hicks 	ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
3954dfea4f0STyler Hicks 	ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv);
3964dfea4f0STyler Hicks 	rc = crypto_ablkcipher_encrypt(req);
3974dfea4f0STyler Hicks 	if (rc == -EINPROGRESS || rc == -EBUSY) {
3984dfea4f0STyler Hicks 		struct extent_crypt_result *ecr = req->base.data;
3994dfea4f0STyler Hicks 
4004dfea4f0STyler Hicks 		wait_for_completion(&ecr->completion);
4014dfea4f0STyler Hicks 		rc = ecr->rc;
4024dfea4f0STyler Hicks 		INIT_COMPLETION(ecr->completion);
4034dfea4f0STyler Hicks 	}
404237fead6SMichael Halcrow out:
4054dfea4f0STyler Hicks 	ablkcipher_request_free(req);
406237fead6SMichael Halcrow 	return rc;
407237fead6SMichael Halcrow }
408237fead6SMichael Halcrow 
409237fead6SMichael Halcrow /**
4100216f7f7SMichael Halcrow  * ecryptfs_lower_offset_for_extent
411237fead6SMichael Halcrow  *
4120216f7f7SMichael Halcrow  * Convert an eCryptfs page index into a lower byte offset
413237fead6SMichael Halcrow  */
4147896b631SAdrian Bunk static void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
4150216f7f7SMichael Halcrow 					     struct ecryptfs_crypt_stat *crypt_stat)
416237fead6SMichael Halcrow {
417157f1071STyler Hicks 	(*offset) = ecryptfs_lower_header_size(crypt_stat)
418157f1071STyler Hicks 		    + (crypt_stat->extent_size * extent_num);
4190216f7f7SMichael Halcrow }
420237fead6SMichael Halcrow 
4210216f7f7SMichael Halcrow /**
4220216f7f7SMichael Halcrow  * ecryptfs_encrypt_extent
4230216f7f7SMichael Halcrow  * @enc_extent_page: Allocated page into which to encrypt the data in
4240216f7f7SMichael Halcrow  *                   @page
4250216f7f7SMichael Halcrow  * @crypt_stat: crypt_stat containing cryptographic context for the
4260216f7f7SMichael Halcrow  *              encryption operation
4270216f7f7SMichael Halcrow  * @page: Page containing plaintext data extent to encrypt
4280216f7f7SMichael Halcrow  * @extent_offset: Page extent offset for use in generating IV
4290216f7f7SMichael Halcrow  *
4300216f7f7SMichael Halcrow  * Encrypts one extent of data.
4310216f7f7SMichael Halcrow  *
4320216f7f7SMichael Halcrow  * Return zero on success; non-zero otherwise
4330216f7f7SMichael Halcrow  */
4340216f7f7SMichael Halcrow static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
4350216f7f7SMichael Halcrow 				   struct ecryptfs_crypt_stat *crypt_stat,
4360216f7f7SMichael Halcrow 				   struct page *page,
4370216f7f7SMichael Halcrow 				   unsigned long extent_offset)
4380216f7f7SMichael Halcrow {
439d6a13c17SMichael Halcrow 	loff_t extent_base;
4400216f7f7SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
4410216f7f7SMichael Halcrow 	int rc;
4420216f7f7SMichael Halcrow 
443d6a13c17SMichael Halcrow 	extent_base = (((loff_t)page->index)
4440216f7f7SMichael Halcrow 		       * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
445237fead6SMichael Halcrow 	rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
4460216f7f7SMichael Halcrow 				(extent_base + extent_offset));
447237fead6SMichael Halcrow 	if (rc) {
448888d57bbSJoe Perches 		ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
449888d57bbSJoe Perches 			"extent [0x%.16llx]; rc = [%d]\n",
450888d57bbSJoe Perches 			(unsigned long long)(extent_base + extent_offset), rc);
451237fead6SMichael Halcrow 		goto out;
452237fead6SMichael Halcrow 	}
453*12003e5bSTyler Hicks 	rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page,
454*12003e5bSTyler Hicks 					extent_offset * crypt_stat->extent_size,
455*12003e5bSTyler Hicks 					page,
456*12003e5bSTyler Hicks 					extent_offset * crypt_stat->extent_size,
457237fead6SMichael Halcrow 					crypt_stat->extent_size, extent_iv);
4580216f7f7SMichael Halcrow 	if (rc < 0) {
4590216f7f7SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to encrypt page with "
4600216f7f7SMichael Halcrow 		       "page->index = [%ld], extent_offset = [%ld]; "
46118d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, page->index, extent_offset,
4620216f7f7SMichael Halcrow 		       rc);
4630216f7f7SMichael Halcrow 		goto out;
4640216f7f7SMichael Halcrow 	}
4650216f7f7SMichael Halcrow 	rc = 0;
4660216f7f7SMichael Halcrow out:
4670216f7f7SMichael Halcrow 	return rc;
4680216f7f7SMichael Halcrow }
4690216f7f7SMichael Halcrow 
4700216f7f7SMichael Halcrow /**
4710216f7f7SMichael Halcrow  * ecryptfs_encrypt_page
4720216f7f7SMichael Halcrow  * @page: Page mapped from the eCryptfs inode for the file; contains
4730216f7f7SMichael Halcrow  *        decrypted content that needs to be encrypted (to a temporary
4740216f7f7SMichael Halcrow  *        page; not in place) and written out to the lower file
4750216f7f7SMichael Halcrow  *
4760216f7f7SMichael Halcrow  * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
4770216f7f7SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
4780216f7f7SMichael Halcrow  * if the file was created on a machine with an 8K page size
4790216f7f7SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
4800216f7f7SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
4810216f7f7SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
4820216f7f7SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
4830216f7f7SMichael Halcrow  *
4840216f7f7SMichael Halcrow  * Returns zero on success; negative on error
4850216f7f7SMichael Halcrow  */
4860216f7f7SMichael Halcrow int ecryptfs_encrypt_page(struct page *page)
4870216f7f7SMichael Halcrow {
4880216f7f7SMichael Halcrow 	struct inode *ecryptfs_inode;
4890216f7f7SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
4907fcba054SEric Sandeen 	char *enc_extent_virt;
4917fcba054SEric Sandeen 	struct page *enc_extent_page = NULL;
4920216f7f7SMichael Halcrow 	loff_t extent_offset;
4930216f7f7SMichael Halcrow 	int rc = 0;
4940216f7f7SMichael Halcrow 
4950216f7f7SMichael Halcrow 	ecryptfs_inode = page->mapping->host;
4960216f7f7SMichael Halcrow 	crypt_stat =
4970216f7f7SMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
49813a791b4STyler Hicks 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
4997fcba054SEric Sandeen 	enc_extent_page = alloc_page(GFP_USER);
5007fcba054SEric Sandeen 	if (!enc_extent_page) {
5010216f7f7SMichael Halcrow 		rc = -ENOMEM;
5020216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error allocating memory for "
5030216f7f7SMichael Halcrow 				"encrypted extent\n");
5040216f7f7SMichael Halcrow 		goto out;
5050216f7f7SMichael Halcrow 	}
5067fcba054SEric Sandeen 	enc_extent_virt = kmap(enc_extent_page);
5070216f7f7SMichael Halcrow 	for (extent_offset = 0;
5080216f7f7SMichael Halcrow 	     extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
5090216f7f7SMichael Halcrow 	     extent_offset++) {
5100216f7f7SMichael Halcrow 		loff_t offset;
5110216f7f7SMichael Halcrow 
5120216f7f7SMichael Halcrow 		rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
5130216f7f7SMichael Halcrow 					     extent_offset);
5140216f7f7SMichael Halcrow 		if (rc) {
5150216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting extent; "
51618d1dbf1SHarvey Harrison 			       "rc = [%d]\n", __func__, rc);
5170216f7f7SMichael Halcrow 			goto out;
5180216f7f7SMichael Halcrow 		}
5190216f7f7SMichael Halcrow 		ecryptfs_lower_offset_for_extent(
520d6a13c17SMichael Halcrow 			&offset, ((((loff_t)page->index)
521d6a13c17SMichael Halcrow 				   * (PAGE_CACHE_SIZE
5220216f7f7SMichael Halcrow 				      / crypt_stat->extent_size))
5230216f7f7SMichael Halcrow 				  + extent_offset), crypt_stat);
524*12003e5bSTyler Hicks 		rc = ecryptfs_write_lower(ecryptfs_inode, (enc_extent_virt +
525*12003e5bSTyler Hicks 				extent_offset * crypt_stat->extent_size),
5260216f7f7SMichael Halcrow 				offset, crypt_stat->extent_size);
52796a7b9c2STyler Hicks 		if (rc < 0) {
5280216f7f7SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting "
5290216f7f7SMichael Halcrow 					"to write lower page; rc = [%d]"
5300216f7f7SMichael Halcrow 					"\n", rc);
5310216f7f7SMichael Halcrow 			goto out;
5320216f7f7SMichael Halcrow 		}
533237fead6SMichael Halcrow 	}
53496a7b9c2STyler Hicks 	rc = 0;
5350216f7f7SMichael Halcrow out:
5367fcba054SEric Sandeen 	if (enc_extent_page) {
5377fcba054SEric Sandeen 		kunmap(enc_extent_page);
5387fcba054SEric Sandeen 		__free_page(enc_extent_page);
5397fcba054SEric Sandeen 	}
5400216f7f7SMichael Halcrow 	return rc;
5410216f7f7SMichael Halcrow }
5420216f7f7SMichael Halcrow 
5430216f7f7SMichael Halcrow static int ecryptfs_decrypt_extent(struct page *page,
5440216f7f7SMichael Halcrow 				   struct ecryptfs_crypt_stat *crypt_stat,
5450216f7f7SMichael Halcrow 				   struct page *enc_extent_page,
5460216f7f7SMichael Halcrow 				   unsigned long extent_offset)
5470216f7f7SMichael Halcrow {
548d6a13c17SMichael Halcrow 	loff_t extent_base;
5490216f7f7SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
5500216f7f7SMichael Halcrow 	int rc;
5510216f7f7SMichael Halcrow 
552d6a13c17SMichael Halcrow 	extent_base = (((loff_t)page->index)
5530216f7f7SMichael Halcrow 		       * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
5540216f7f7SMichael Halcrow 	rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
5550216f7f7SMichael Halcrow 				(extent_base + extent_offset));
556237fead6SMichael Halcrow 	if (rc) {
557888d57bbSJoe Perches 		ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
558888d57bbSJoe Perches 			"extent [0x%.16llx]; rc = [%d]\n",
559888d57bbSJoe Perches 			(unsigned long long)(extent_base + extent_offset), rc);
560237fead6SMichael Halcrow 		goto out;
561237fead6SMichael Halcrow 	}
5620216f7f7SMichael Halcrow 	rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
563*12003e5bSTyler Hicks 					extent_offset * crypt_stat->extent_size,
564*12003e5bSTyler Hicks 					enc_extent_page,
565*12003e5bSTyler Hicks 					extent_offset * crypt_stat->extent_size,
5660216f7f7SMichael Halcrow 					crypt_stat->extent_size, extent_iv);
5670216f7f7SMichael Halcrow 	if (rc < 0) {
5680216f7f7SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to decrypt to page with "
5690216f7f7SMichael Halcrow 		       "page->index = [%ld], extent_offset = [%ld]; "
57018d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, page->index, extent_offset,
5710216f7f7SMichael Halcrow 		       rc);
5720216f7f7SMichael Halcrow 		goto out;
5730216f7f7SMichael Halcrow 	}
5740216f7f7SMichael Halcrow 	rc = 0;
575237fead6SMichael Halcrow out:
576237fead6SMichael Halcrow 	return rc;
577237fead6SMichael Halcrow }
578237fead6SMichael Halcrow 
579237fead6SMichael Halcrow /**
580237fead6SMichael Halcrow  * ecryptfs_decrypt_page
5810216f7f7SMichael Halcrow  * @page: Page mapped from the eCryptfs inode for the file; data read
5820216f7f7SMichael Halcrow  *        and decrypted from the lower file will be written into this
5830216f7f7SMichael Halcrow  *        page
584237fead6SMichael Halcrow  *
585237fead6SMichael Halcrow  * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
586237fead6SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
587237fead6SMichael Halcrow  * if the file was created on a machine with an 8K page size
588237fead6SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
589237fead6SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
590237fead6SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
591237fead6SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
592237fead6SMichael Halcrow  *
593237fead6SMichael Halcrow  * Returns zero on success; negative on error
594237fead6SMichael Halcrow  */
5950216f7f7SMichael Halcrow int ecryptfs_decrypt_page(struct page *page)
596237fead6SMichael Halcrow {
5970216f7f7SMichael Halcrow 	struct inode *ecryptfs_inode;
598237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
5997fcba054SEric Sandeen 	char *enc_extent_virt;
6007fcba054SEric Sandeen 	struct page *enc_extent_page = NULL;
6010216f7f7SMichael Halcrow 	unsigned long extent_offset;
602237fead6SMichael Halcrow 	int rc = 0;
603237fead6SMichael Halcrow 
6040216f7f7SMichael Halcrow 	ecryptfs_inode = page->mapping->host;
6050216f7f7SMichael Halcrow 	crypt_stat =
6060216f7f7SMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
60713a791b4STyler Hicks 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
6087fcba054SEric Sandeen 	enc_extent_page = alloc_page(GFP_USER);
6097fcba054SEric Sandeen 	if (!enc_extent_page) {
610237fead6SMichael Halcrow 		rc = -ENOMEM;
6110216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error allocating memory for "
6120216f7f7SMichael Halcrow 				"encrypted extent\n");
61316a72c45SMichael Halcrow 		goto out;
614237fead6SMichael Halcrow 	}
6157fcba054SEric Sandeen 	enc_extent_virt = kmap(enc_extent_page);
6160216f7f7SMichael Halcrow 	for (extent_offset = 0;
6170216f7f7SMichael Halcrow 	     extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
6180216f7f7SMichael Halcrow 	     extent_offset++) {
6190216f7f7SMichael Halcrow 		loff_t offset;
6200216f7f7SMichael Halcrow 
6210216f7f7SMichael Halcrow 		ecryptfs_lower_offset_for_extent(
6220216f7f7SMichael Halcrow 			&offset, ((page->index * (PAGE_CACHE_SIZE
6230216f7f7SMichael Halcrow 						  / crypt_stat->extent_size))
6240216f7f7SMichael Halcrow 				  + extent_offset), crypt_stat);
625*12003e5bSTyler Hicks 		rc = ecryptfs_read_lower((enc_extent_virt +
626*12003e5bSTyler Hicks 				extent_offset * crypt_stat->extent_size),
627*12003e5bSTyler Hicks 				offset, crypt_stat->extent_size,
6280216f7f7SMichael Halcrow 				ecryptfs_inode);
62996a7b9c2STyler Hicks 		if (rc < 0) {
6300216f7f7SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting "
6310216f7f7SMichael Halcrow 					"to read lower page; rc = [%d]"
6320216f7f7SMichael Halcrow 					"\n", rc);
63316a72c45SMichael Halcrow 			goto out;
634237fead6SMichael Halcrow 		}
6350216f7f7SMichael Halcrow 		rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
6360216f7f7SMichael Halcrow 					     extent_offset);
6370216f7f7SMichael Halcrow 		if (rc) {
6380216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting extent; "
63918d1dbf1SHarvey Harrison 			       "rc = [%d]\n", __func__, rc);
64016a72c45SMichael Halcrow 			goto out;
641237fead6SMichael Halcrow 		}
642237fead6SMichael Halcrow 	}
643237fead6SMichael Halcrow out:
6447fcba054SEric Sandeen 	if (enc_extent_page) {
6457fcba054SEric Sandeen 		kunmap(enc_extent_page);
6467fcba054SEric Sandeen 		__free_page(enc_extent_page);
6477fcba054SEric Sandeen 	}
648237fead6SMichael Halcrow 	return rc;
649237fead6SMichael Halcrow }
650237fead6SMichael Halcrow 
651237fead6SMichael Halcrow /**
652237fead6SMichael Halcrow  * decrypt_scatterlist
65322e78fafSMichael Halcrow  * @crypt_stat: Cryptographic context
65422e78fafSMichael Halcrow  * @dest_sg: The destination scatterlist to decrypt into
65522e78fafSMichael Halcrow  * @src_sg: The source scatterlist to decrypt from
65622e78fafSMichael Halcrow  * @size: The number of bytes to decrypt
65722e78fafSMichael Halcrow  * @iv: The initialization vector to use for the decryption
658237fead6SMichael Halcrow  *
659237fead6SMichael Halcrow  * Returns the number of bytes decrypted; negative value on error
660237fead6SMichael Halcrow  */
661237fead6SMichael Halcrow static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
662237fead6SMichael Halcrow 			       struct scatterlist *dest_sg,
663237fead6SMichael Halcrow 			       struct scatterlist *src_sg, int size,
664237fead6SMichael Halcrow 			       unsigned char *iv)
665237fead6SMichael Halcrow {
6664dfea4f0STyler Hicks 	struct ablkcipher_request *req = NULL;
6674dfea4f0STyler Hicks 	struct extent_crypt_result ecr;
668237fead6SMichael Halcrow 	int rc = 0;
669237fead6SMichael Halcrow 
6704dfea4f0STyler Hicks 	BUG_ON(!crypt_stat || !crypt_stat->tfm
6714dfea4f0STyler Hicks 	       || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
6724dfea4f0STyler Hicks 	if (unlikely(ecryptfs_verbosity > 0)) {
6734dfea4f0STyler Hicks 		ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
6744dfea4f0STyler Hicks 				crypt_stat->key_size);
6754dfea4f0STyler Hicks 		ecryptfs_dump_hex(crypt_stat->key,
6764dfea4f0STyler Hicks 				  crypt_stat->key_size);
6774dfea4f0STyler Hicks 	}
6784dfea4f0STyler Hicks 
6794dfea4f0STyler Hicks 	init_completion(&ecr.completion);
6804dfea4f0STyler Hicks 
681237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
6824dfea4f0STyler Hicks 	req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
6834dfea4f0STyler Hicks 	if (!req) {
6844dfea4f0STyler Hicks 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
6854dfea4f0STyler Hicks 		rc = -ENOMEM;
6864dfea4f0STyler Hicks 		goto out;
6874dfea4f0STyler Hicks 	}
6884dfea4f0STyler Hicks 
6894dfea4f0STyler Hicks 	ablkcipher_request_set_callback(req,
6904dfea4f0STyler Hicks 			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
6914dfea4f0STyler Hicks 			extent_crypt_complete, &ecr);
6924dfea4f0STyler Hicks 	/* Consider doing this once, when the file is opened */
6934dfea4f0STyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
6944dfea4f0STyler Hicks 		rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
695237fead6SMichael Halcrow 					      crypt_stat->key_size);
696237fead6SMichael Halcrow 		if (rc) {
6974dfea4f0STyler Hicks 			ecryptfs_printk(KERN_ERR,
6984dfea4f0STyler Hicks 					"Error setting key; rc = [%d]\n",
699237fead6SMichael Halcrow 					rc);
700237fead6SMichael Halcrow 			mutex_unlock(&crypt_stat->cs_tfm_mutex);
701237fead6SMichael Halcrow 			rc = -EINVAL;
702237fead6SMichael Halcrow 			goto out;
703237fead6SMichael Halcrow 		}
7044dfea4f0STyler Hicks 		crypt_stat->flags |= ECRYPTFS_KEY_SET;
705237fead6SMichael Halcrow 	}
7064dfea4f0STyler Hicks 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
7074dfea4f0STyler Hicks 	ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
7084dfea4f0STyler Hicks 	ablkcipher_request_set_crypt(req, src_sg, dest_sg, size, iv);
7094dfea4f0STyler Hicks 	rc = crypto_ablkcipher_decrypt(req);
7104dfea4f0STyler Hicks 	if (rc == -EINPROGRESS || rc == -EBUSY) {
7114dfea4f0STyler Hicks 		struct extent_crypt_result *ecr = req->base.data;
7124dfea4f0STyler Hicks 
7134dfea4f0STyler Hicks 		wait_for_completion(&ecr->completion);
7144dfea4f0STyler Hicks 		rc = ecr->rc;
7154dfea4f0STyler Hicks 		INIT_COMPLETION(ecr->completion);
7164dfea4f0STyler Hicks 	}
717237fead6SMichael Halcrow out:
7184dfea4f0STyler Hicks 	ablkcipher_request_free(req);
719237fead6SMichael Halcrow 	return rc;
7204dfea4f0STyler Hicks 
721237fead6SMichael Halcrow }
722237fead6SMichael Halcrow 
723237fead6SMichael Halcrow /**
724237fead6SMichael Halcrow  * ecryptfs_encrypt_page_offset
72522e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
72622e78fafSMichael Halcrow  * @dst_page: The page to encrypt into
72722e78fafSMichael Halcrow  * @dst_offset: The offset in the page to encrypt into
72822e78fafSMichael Halcrow  * @src_page: The page to encrypt from
72922e78fafSMichael Halcrow  * @src_offset: The offset in the page to encrypt from
73022e78fafSMichael Halcrow  * @size: The number of bytes to encrypt
73122e78fafSMichael Halcrow  * @iv: The initialization vector to use for the encryption
732237fead6SMichael Halcrow  *
733237fead6SMichael Halcrow  * Returns the number of bytes encrypted
734237fead6SMichael Halcrow  */
735237fead6SMichael Halcrow static int
736237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
737237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
738237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
739237fead6SMichael Halcrow 			     unsigned char *iv)
740237fead6SMichael Halcrow {
741237fead6SMichael Halcrow 	struct scatterlist src_sg, dst_sg;
742237fead6SMichael Halcrow 
74360c74f81SJens Axboe 	sg_init_table(&src_sg, 1);
74460c74f81SJens Axboe 	sg_init_table(&dst_sg, 1);
74560c74f81SJens Axboe 
746642f1490SJens Axboe 	sg_set_page(&src_sg, src_page, size, src_offset);
747642f1490SJens Axboe 	sg_set_page(&dst_sg, dst_page, size, dst_offset);
748237fead6SMichael Halcrow 	return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
749237fead6SMichael Halcrow }
750237fead6SMichael Halcrow 
751237fead6SMichael Halcrow /**
752237fead6SMichael Halcrow  * ecryptfs_decrypt_page_offset
75322e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
75422e78fafSMichael Halcrow  * @dst_page: The page to decrypt into
75522e78fafSMichael Halcrow  * @dst_offset: The offset in the page to decrypt into
75622e78fafSMichael Halcrow  * @src_page: The page to decrypt from
75722e78fafSMichael Halcrow  * @src_offset: The offset in the page to decrypt from
75822e78fafSMichael Halcrow  * @size: The number of bytes to decrypt
75922e78fafSMichael Halcrow  * @iv: The initialization vector to use for the decryption
760237fead6SMichael Halcrow  *
761237fead6SMichael Halcrow  * Returns the number of bytes decrypted
762237fead6SMichael Halcrow  */
763237fead6SMichael Halcrow static int
764237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
765237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
766237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
767237fead6SMichael Halcrow 			     unsigned char *iv)
768237fead6SMichael Halcrow {
769237fead6SMichael Halcrow 	struct scatterlist src_sg, dst_sg;
770237fead6SMichael Halcrow 
77160c74f81SJens Axboe 	sg_init_table(&src_sg, 1);
772642f1490SJens Axboe 	sg_set_page(&src_sg, src_page, size, src_offset);
77360c74f81SJens Axboe 
774642f1490SJens Axboe 	sg_init_table(&dst_sg, 1);
775642f1490SJens Axboe 	sg_set_page(&dst_sg, dst_page, size, dst_offset);
776642f1490SJens Axboe 
777237fead6SMichael Halcrow 	return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
778237fead6SMichael Halcrow }
779237fead6SMichael Halcrow 
780237fead6SMichael Halcrow #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
781237fead6SMichael Halcrow 
782237fead6SMichael Halcrow /**
783237fead6SMichael Halcrow  * ecryptfs_init_crypt_ctx
784421f91d2SUwe Kleine-König  * @crypt_stat: Uninitialized crypt stats structure
785237fead6SMichael Halcrow  *
786237fead6SMichael Halcrow  * Initialize the crypto context.
787237fead6SMichael Halcrow  *
788237fead6SMichael Halcrow  * TODO: Performance: Keep a cache of initialized cipher contexts;
789237fead6SMichael Halcrow  * only init if needed
790237fead6SMichael Halcrow  */
791237fead6SMichael Halcrow int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
792237fead6SMichael Halcrow {
7938bba066fSMichael Halcrow 	char *full_alg_name;
794237fead6SMichael Halcrow 	int rc = -EINVAL;
795237fead6SMichael Halcrow 
796237fead6SMichael Halcrow 	if (!crypt_stat->cipher) {
797237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "No cipher specified\n");
798237fead6SMichael Halcrow 		goto out;
799237fead6SMichael Halcrow 	}
800237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG,
801237fead6SMichael Halcrow 			"Initializing cipher [%s]; strlen = [%d]; "
802f24b3887STyler Hicks 			"key_size_bits = [%zd]\n",
803237fead6SMichael Halcrow 			crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
804237fead6SMichael Halcrow 			crypt_stat->key_size << 3);
805237fead6SMichael Halcrow 	if (crypt_stat->tfm) {
806237fead6SMichael Halcrow 		rc = 0;
807237fead6SMichael Halcrow 		goto out;
808237fead6SMichael Halcrow 	}
809237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
8108bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
8118bba066fSMichael Halcrow 						    crypt_stat->cipher, "cbc");
8128bba066fSMichael Halcrow 	if (rc)
813c8161f64SEric Sandeen 		goto out_unlock;
8144dfea4f0STyler Hicks 	crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0);
8158bba066fSMichael Halcrow 	kfree(full_alg_name);
816de88777eSAkinobu Mita 	if (IS_ERR(crypt_stat->tfm)) {
817de88777eSAkinobu Mita 		rc = PTR_ERR(crypt_stat->tfm);
818b0105eaeSTyler Hicks 		crypt_stat->tfm = NULL;
819237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
820237fead6SMichael Halcrow 				"Error initializing cipher [%s]\n",
821237fead6SMichael Halcrow 				crypt_stat->cipher);
822c8161f64SEric Sandeen 		goto out_unlock;
823237fead6SMichael Halcrow 	}
8244dfea4f0STyler Hicks 	crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
825237fead6SMichael Halcrow 	rc = 0;
826c8161f64SEric Sandeen out_unlock:
827c8161f64SEric Sandeen 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
828237fead6SMichael Halcrow out:
829237fead6SMichael Halcrow 	return rc;
830237fead6SMichael Halcrow }
831237fead6SMichael Halcrow 
832237fead6SMichael Halcrow static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
833237fead6SMichael Halcrow {
834237fead6SMichael Halcrow 	int extent_size_tmp;
835237fead6SMichael Halcrow 
836237fead6SMichael Halcrow 	crypt_stat->extent_mask = 0xFFFFFFFF;
837237fead6SMichael Halcrow 	crypt_stat->extent_shift = 0;
838237fead6SMichael Halcrow 	if (crypt_stat->extent_size == 0)
839237fead6SMichael Halcrow 		return;
840237fead6SMichael Halcrow 	extent_size_tmp = crypt_stat->extent_size;
841237fead6SMichael Halcrow 	while ((extent_size_tmp & 0x01) == 0) {
842237fead6SMichael Halcrow 		extent_size_tmp >>= 1;
843237fead6SMichael Halcrow 		crypt_stat->extent_mask <<= 1;
844237fead6SMichael Halcrow 		crypt_stat->extent_shift++;
845237fead6SMichael Halcrow 	}
846237fead6SMichael Halcrow }
847237fead6SMichael Halcrow 
848237fead6SMichael Halcrow void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
849237fead6SMichael Halcrow {
850237fead6SMichael Halcrow 	/* Default values; may be overwritten as we are parsing the
851237fead6SMichael Halcrow 	 * packets. */
852237fead6SMichael Halcrow 	crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
853237fead6SMichael Halcrow 	set_extent_mask_and_shift(crypt_stat);
854237fead6SMichael Halcrow 	crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
855dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
856fa3ef1cbSTyler Hicks 		crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
85745eaab79SMichael Halcrow 	else {
85845eaab79SMichael Halcrow 		if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
859fa3ef1cbSTyler Hicks 			crypt_stat->metadata_size =
860cc11beffSMichael Halcrow 				ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
861dd2a3b7aSMichael Halcrow 		else
862fa3ef1cbSTyler Hicks 			crypt_stat->metadata_size = PAGE_CACHE_SIZE;
86345eaab79SMichael Halcrow 	}
864237fead6SMichael Halcrow }
865237fead6SMichael Halcrow 
866237fead6SMichael Halcrow /**
867237fead6SMichael Halcrow  * ecryptfs_compute_root_iv
868237fead6SMichael Halcrow  * @crypt_stats
869237fead6SMichael Halcrow  *
870237fead6SMichael Halcrow  * On error, sets the root IV to all 0's.
871237fead6SMichael Halcrow  */
872237fead6SMichael Halcrow int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
873237fead6SMichael Halcrow {
874237fead6SMichael Halcrow 	int rc = 0;
875237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
876237fead6SMichael Halcrow 
877237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
878237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes <= 0);
879e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
880237fead6SMichael Halcrow 		rc = -EINVAL;
881237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Session key not valid; "
882237fead6SMichael Halcrow 				"cannot generate root IV\n");
883237fead6SMichael Halcrow 		goto out;
884237fead6SMichael Halcrow 	}
885237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
886237fead6SMichael Halcrow 				    crypt_stat->key_size);
887237fead6SMichael Halcrow 	if (rc) {
888237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
889237fead6SMichael Halcrow 				"MD5 while generating root IV\n");
890237fead6SMichael Halcrow 		goto out;
891237fead6SMichael Halcrow 	}
892237fead6SMichael Halcrow 	memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
893237fead6SMichael Halcrow out:
894237fead6SMichael Halcrow 	if (rc) {
895237fead6SMichael Halcrow 		memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
896e2bd99ecSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
897237fead6SMichael Halcrow 	}
898237fead6SMichael Halcrow 	return rc;
899237fead6SMichael Halcrow }
900237fead6SMichael Halcrow 
901237fead6SMichael Halcrow static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
902237fead6SMichael Halcrow {
903237fead6SMichael Halcrow 	get_random_bytes(crypt_stat->key, crypt_stat->key_size);
904e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
905237fead6SMichael Halcrow 	ecryptfs_compute_root_iv(crypt_stat);
906237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
907237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
908237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
909237fead6SMichael Halcrow 				  crypt_stat->key_size);
910237fead6SMichael Halcrow 	}
911237fead6SMichael Halcrow }
912237fead6SMichael Halcrow 
913237fead6SMichael Halcrow /**
91417398957SMichael Halcrow  * ecryptfs_copy_mount_wide_flags_to_inode_flags
91522e78fafSMichael Halcrow  * @crypt_stat: The inode's cryptographic context
91622e78fafSMichael Halcrow  * @mount_crypt_stat: The mount point's cryptographic context
91717398957SMichael Halcrow  *
91817398957SMichael Halcrow  * This function propagates the mount-wide flags to individual inode
91917398957SMichael Halcrow  * flags.
92017398957SMichael Halcrow  */
92117398957SMichael Halcrow static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
92217398957SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
92317398957SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
92417398957SMichael Halcrow {
92517398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
92617398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
92717398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
92817398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
929addd65adSMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
930addd65adSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
931addd65adSMichael Halcrow 		if (mount_crypt_stat->flags
932addd65adSMichael Halcrow 		    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
933addd65adSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
934addd65adSMichael Halcrow 		else if (mount_crypt_stat->flags
935addd65adSMichael Halcrow 			 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
936addd65adSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
937addd65adSMichael Halcrow 	}
93817398957SMichael Halcrow }
93917398957SMichael Halcrow 
940f4aad16aSMichael Halcrow static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
941f4aad16aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
942f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
943f4aad16aSMichael Halcrow {
944f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
945f4aad16aSMichael Halcrow 	int rc = 0;
946f4aad16aSMichael Halcrow 
947aa06117fSRoland Dreier 	mutex_lock(&crypt_stat->keysig_list_mutex);
948f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
949aa06117fSRoland Dreier 
950f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
951f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
952f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
95384814d64STyler Hicks 		if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
95484814d64STyler Hicks 			continue;
955f4aad16aSMichael Halcrow 		rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
956f4aad16aSMichael Halcrow 		if (rc) {
957f4aad16aSMichael Halcrow 			printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
958f4aad16aSMichael Halcrow 			goto out;
959f4aad16aSMichael Halcrow 		}
960f4aad16aSMichael Halcrow 	}
961aa06117fSRoland Dreier 
962f4aad16aSMichael Halcrow out:
963aa06117fSRoland Dreier 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
964aa06117fSRoland Dreier 	mutex_unlock(&crypt_stat->keysig_list_mutex);
965f4aad16aSMichael Halcrow 	return rc;
966f4aad16aSMichael Halcrow }
967f4aad16aSMichael Halcrow 
96817398957SMichael Halcrow /**
969237fead6SMichael Halcrow  * ecryptfs_set_default_crypt_stat_vals
97022e78fafSMichael Halcrow  * @crypt_stat: The inode's cryptographic context
97122e78fafSMichael Halcrow  * @mount_crypt_stat: The mount point's cryptographic context
972237fead6SMichael Halcrow  *
973237fead6SMichael Halcrow  * Default values in the event that policy does not override them.
974237fead6SMichael Halcrow  */
975237fead6SMichael Halcrow static void ecryptfs_set_default_crypt_stat_vals(
976237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
977237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
978237fead6SMichael Halcrow {
97917398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
98017398957SMichael Halcrow 						      mount_crypt_stat);
981237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
982237fead6SMichael Halcrow 	strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
983237fead6SMichael Halcrow 	crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
984e2bd99ecSMichael Halcrow 	crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
985237fead6SMichael Halcrow 	crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
986237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = mount_crypt_stat;
987237fead6SMichael Halcrow }
988237fead6SMichael Halcrow 
989237fead6SMichael Halcrow /**
990237fead6SMichael Halcrow  * ecryptfs_new_file_context
991b59db43aSTyler Hicks  * @ecryptfs_inode: The eCryptfs inode
992237fead6SMichael Halcrow  *
993237fead6SMichael Halcrow  * If the crypto context for the file has not yet been established,
994237fead6SMichael Halcrow  * this is where we do that.  Establishing a new crypto context
995237fead6SMichael Halcrow  * involves the following decisions:
996237fead6SMichael Halcrow  *  - What cipher to use?
997237fead6SMichael Halcrow  *  - What set of authentication tokens to use?
998237fead6SMichael Halcrow  * Here we just worry about getting enough information into the
999237fead6SMichael Halcrow  * authentication tokens so that we know that they are available.
1000237fead6SMichael Halcrow  * We associate the available authentication tokens with the new file
1001237fead6SMichael Halcrow  * via the set of signatures in the crypt_stat struct.  Later, when
1002237fead6SMichael Halcrow  * the headers are actually written out, we may again defer to
1003237fead6SMichael Halcrow  * userspace to perform the encryption of the session key; for the
1004237fead6SMichael Halcrow  * foreseeable future, this will be the case with public key packets.
1005237fead6SMichael Halcrow  *
1006237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
1007237fead6SMichael Halcrow  */
1008b59db43aSTyler Hicks int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
1009237fead6SMichael Halcrow {
1010237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1011b59db43aSTyler Hicks 	    &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1012237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1013237fead6SMichael Halcrow 	    &ecryptfs_superblock_to_private(
1014b59db43aSTyler Hicks 		    ecryptfs_inode->i_sb)->mount_crypt_stat;
1015237fead6SMichael Halcrow 	int cipher_name_len;
1016f4aad16aSMichael Halcrow 	int rc = 0;
1017237fead6SMichael Halcrow 
1018237fead6SMichael Halcrow 	ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
1019af655dc6SMichael Halcrow 	crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
102017398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
102117398957SMichael Halcrow 						      mount_crypt_stat);
1022f4aad16aSMichael Halcrow 	rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
1023f4aad16aSMichael Halcrow 							 mount_crypt_stat);
1024f4aad16aSMichael Halcrow 	if (rc) {
1025f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
1026f4aad16aSMichael Halcrow 		       "to the inode key sigs; rc = [%d]\n", rc);
1027f4aad16aSMichael Halcrow 		goto out;
1028f4aad16aSMichael Halcrow 	}
1029237fead6SMichael Halcrow 	cipher_name_len =
1030237fead6SMichael Halcrow 		strlen(mount_crypt_stat->global_default_cipher_name);
1031237fead6SMichael Halcrow 	memcpy(crypt_stat->cipher,
1032237fead6SMichael Halcrow 	       mount_crypt_stat->global_default_cipher_name,
1033237fead6SMichael Halcrow 	       cipher_name_len);
1034237fead6SMichael Halcrow 	crypt_stat->cipher[cipher_name_len] = '\0';
1035237fead6SMichael Halcrow 	crypt_stat->key_size =
1036237fead6SMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size;
1037237fead6SMichael Halcrow 	ecryptfs_generate_new_key(crypt_stat);
1038237fead6SMichael Halcrow 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
1039237fead6SMichael Halcrow 	if (rc)
1040237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1041237fead6SMichael Halcrow 				"context for cipher [%s]: rc = [%d]\n",
1042237fead6SMichael Halcrow 				crypt_stat->cipher, rc);
1043f4aad16aSMichael Halcrow out:
1044237fead6SMichael Halcrow 	return rc;
1045237fead6SMichael Halcrow }
1046237fead6SMichael Halcrow 
1047237fead6SMichael Halcrow /**
10487a86617eSTyler Hicks  * ecryptfs_validate_marker - check for the ecryptfs marker
1049237fead6SMichael Halcrow  * @data: The data block in which to check
1050237fead6SMichael Halcrow  *
10517a86617eSTyler Hicks  * Returns zero if marker found; -EINVAL if not found
1052237fead6SMichael Halcrow  */
10537a86617eSTyler Hicks static int ecryptfs_validate_marker(char *data)
1054237fead6SMichael Halcrow {
1055237fead6SMichael Halcrow 	u32 m_1, m_2;
1056237fead6SMichael Halcrow 
105729335c6aSHarvey Harrison 	m_1 = get_unaligned_be32(data);
105829335c6aSHarvey Harrison 	m_2 = get_unaligned_be32(data + 4);
1059237fead6SMichael Halcrow 	if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
10607a86617eSTyler Hicks 		return 0;
1061237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1062237fead6SMichael Halcrow 			"MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1063237fead6SMichael Halcrow 			MAGIC_ECRYPTFS_MARKER);
1064237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1065237fead6SMichael Halcrow 			"[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
10667a86617eSTyler Hicks 	return -EINVAL;
1067237fead6SMichael Halcrow }
1068237fead6SMichael Halcrow 
1069237fead6SMichael Halcrow struct ecryptfs_flag_map_elem {
1070237fead6SMichael Halcrow 	u32 file_flag;
1071237fead6SMichael Halcrow 	u32 local_flag;
1072237fead6SMichael Halcrow };
1073237fead6SMichael Halcrow 
1074237fead6SMichael Halcrow /* Add support for additional flags by adding elements here. */
1075237fead6SMichael Halcrow static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1076237fead6SMichael Halcrow 	{0x00000001, ECRYPTFS_ENABLE_HMAC},
1077dd2a3b7aSMichael Halcrow 	{0x00000002, ECRYPTFS_ENCRYPTED},
1078addd65adSMichael Halcrow 	{0x00000004, ECRYPTFS_METADATA_IN_XATTR},
1079addd65adSMichael Halcrow 	{0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
1080237fead6SMichael Halcrow };
1081237fead6SMichael Halcrow 
1082237fead6SMichael Halcrow /**
1083237fead6SMichael Halcrow  * ecryptfs_process_flags
108422e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1085237fead6SMichael Halcrow  * @page_virt: Source data to be parsed
1086237fead6SMichael Halcrow  * @bytes_read: Updated with the number of bytes read
1087237fead6SMichael Halcrow  *
1088237fead6SMichael Halcrow  * Returns zero on success; non-zero if the flag set is invalid
1089237fead6SMichael Halcrow  */
1090237fead6SMichael Halcrow static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1091237fead6SMichael Halcrow 				  char *page_virt, int *bytes_read)
1092237fead6SMichael Halcrow {
1093237fead6SMichael Halcrow 	int rc = 0;
1094237fead6SMichael Halcrow 	int i;
1095237fead6SMichael Halcrow 	u32 flags;
1096237fead6SMichael Halcrow 
109729335c6aSHarvey Harrison 	flags = get_unaligned_be32(page_virt);
1098237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1099237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
1100237fead6SMichael Halcrow 		if (flags & ecryptfs_flag_map[i].file_flag) {
1101e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
1102237fead6SMichael Halcrow 		} else
1103e2bd99ecSMichael Halcrow 			crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
1104237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
1105237fead6SMichael Halcrow 	crypt_stat->file_version = ((flags >> 24) & 0xFF);
1106237fead6SMichael Halcrow 	(*bytes_read) = 4;
1107237fead6SMichael Halcrow 	return rc;
1108237fead6SMichael Halcrow }
1109237fead6SMichael Halcrow 
1110237fead6SMichael Halcrow /**
1111237fead6SMichael Halcrow  * write_ecryptfs_marker
1112237fead6SMichael Halcrow  * @page_virt: The pointer to in a page to begin writing the marker
1113237fead6SMichael Halcrow  * @written: Number of bytes written
1114237fead6SMichael Halcrow  *
1115237fead6SMichael Halcrow  * Marker = 0x3c81b7f5
1116237fead6SMichael Halcrow  */
1117237fead6SMichael Halcrow static void write_ecryptfs_marker(char *page_virt, size_t *written)
1118237fead6SMichael Halcrow {
1119237fead6SMichael Halcrow 	u32 m_1, m_2;
1120237fead6SMichael Halcrow 
1121237fead6SMichael Halcrow 	get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1122237fead6SMichael Halcrow 	m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
112329335c6aSHarvey Harrison 	put_unaligned_be32(m_1, page_virt);
112429335c6aSHarvey Harrison 	page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
112529335c6aSHarvey Harrison 	put_unaligned_be32(m_2, page_virt);
1126237fead6SMichael Halcrow 	(*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1127237fead6SMichael Halcrow }
1128237fead6SMichael Halcrow 
1129f4e60e6bSTyler Hicks void ecryptfs_write_crypt_stat_flags(char *page_virt,
1130f4e60e6bSTyler Hicks 				     struct ecryptfs_crypt_stat *crypt_stat,
1131237fead6SMichael Halcrow 				     size_t *written)
1132237fead6SMichael Halcrow {
1133237fead6SMichael Halcrow 	u32 flags = 0;
1134237fead6SMichael Halcrow 	int i;
1135237fead6SMichael Halcrow 
1136237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1137237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
1138e2bd99ecSMichael Halcrow 		if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
1139237fead6SMichael Halcrow 			flags |= ecryptfs_flag_map[i].file_flag;
1140237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
1141237fead6SMichael Halcrow 	flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
114229335c6aSHarvey Harrison 	put_unaligned_be32(flags, page_virt);
1143237fead6SMichael Halcrow 	(*written) = 4;
1144237fead6SMichael Halcrow }
1145237fead6SMichael Halcrow 
1146237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem {
1147237fead6SMichael Halcrow 	char cipher_str[16];
114819e66a67STrevor Highland 	u8 cipher_code;
1149237fead6SMichael Halcrow };
1150237fead6SMichael Halcrow 
1151237fead6SMichael Halcrow /* Add support for additional ciphers by adding elements here. The
1152237fead6SMichael Halcrow  * cipher_code is whatever OpenPGP applicatoins use to identify the
1153237fead6SMichael Halcrow  * ciphers. List in order of probability. */
1154237fead6SMichael Halcrow static struct ecryptfs_cipher_code_str_map_elem
1155237fead6SMichael Halcrow ecryptfs_cipher_code_str_map[] = {
1156237fead6SMichael Halcrow 	{"aes",RFC2440_CIPHER_AES_128 },
1157237fead6SMichael Halcrow 	{"blowfish", RFC2440_CIPHER_BLOWFISH},
1158237fead6SMichael Halcrow 	{"des3_ede", RFC2440_CIPHER_DES3_EDE},
1159237fead6SMichael Halcrow 	{"cast5", RFC2440_CIPHER_CAST_5},
1160237fead6SMichael Halcrow 	{"twofish", RFC2440_CIPHER_TWOFISH},
1161237fead6SMichael Halcrow 	{"cast6", RFC2440_CIPHER_CAST_6},
1162237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_192},
1163237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_256}
1164237fead6SMichael Halcrow };
1165237fead6SMichael Halcrow 
1166237fead6SMichael Halcrow /**
1167237fead6SMichael Halcrow  * ecryptfs_code_for_cipher_string
11689c79f34fSMichael Halcrow  * @cipher_name: The string alias for the cipher
11699c79f34fSMichael Halcrow  * @key_bytes: Length of key in bytes; used for AES code selection
1170237fead6SMichael Halcrow  *
1171237fead6SMichael Halcrow  * Returns zero on no match, or the cipher code on match
1172237fead6SMichael Halcrow  */
11739c79f34fSMichael Halcrow u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
1174237fead6SMichael Halcrow {
1175237fead6SMichael Halcrow 	int i;
117619e66a67STrevor Highland 	u8 code = 0;
1177237fead6SMichael Halcrow 	struct ecryptfs_cipher_code_str_map_elem *map =
1178237fead6SMichael Halcrow 		ecryptfs_cipher_code_str_map;
1179237fead6SMichael Halcrow 
11809c79f34fSMichael Halcrow 	if (strcmp(cipher_name, "aes") == 0) {
11819c79f34fSMichael Halcrow 		switch (key_bytes) {
1182237fead6SMichael Halcrow 		case 16:
1183237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_128;
1184237fead6SMichael Halcrow 			break;
1185237fead6SMichael Halcrow 		case 24:
1186237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_192;
1187237fead6SMichael Halcrow 			break;
1188237fead6SMichael Halcrow 		case 32:
1189237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_256;
1190237fead6SMichael Halcrow 		}
1191237fead6SMichael Halcrow 	} else {
1192237fead6SMichael Halcrow 		for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
11939c79f34fSMichael Halcrow 			if (strcmp(cipher_name, map[i].cipher_str) == 0) {
1194237fead6SMichael Halcrow 				code = map[i].cipher_code;
1195237fead6SMichael Halcrow 				break;
1196237fead6SMichael Halcrow 			}
1197237fead6SMichael Halcrow 	}
1198237fead6SMichael Halcrow 	return code;
1199237fead6SMichael Halcrow }
1200237fead6SMichael Halcrow 
1201237fead6SMichael Halcrow /**
1202237fead6SMichael Halcrow  * ecryptfs_cipher_code_to_string
1203237fead6SMichael Halcrow  * @str: Destination to write out the cipher name
1204237fead6SMichael Halcrow  * @cipher_code: The code to convert to cipher name string
1205237fead6SMichael Halcrow  *
1206237fead6SMichael Halcrow  * Returns zero on success
1207237fead6SMichael Halcrow  */
120819e66a67STrevor Highland int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
1209237fead6SMichael Halcrow {
1210237fead6SMichael Halcrow 	int rc = 0;
1211237fead6SMichael Halcrow 	int i;
1212237fead6SMichael Halcrow 
1213237fead6SMichael Halcrow 	str[0] = '\0';
1214237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1215237fead6SMichael Halcrow 		if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1216237fead6SMichael Halcrow 			strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1217237fead6SMichael Halcrow 	if (str[0] == '\0') {
1218237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1219237fead6SMichael Halcrow 				"[%d]\n", cipher_code);
1220237fead6SMichael Halcrow 		rc = -EINVAL;
1221237fead6SMichael Halcrow 	}
1222237fead6SMichael Halcrow 	return rc;
1223237fead6SMichael Halcrow }
1224237fead6SMichael Halcrow 
1225778aeb42STyler Hicks int ecryptfs_read_and_validate_header_region(struct inode *inode)
1226dd2a3b7aSMichael Halcrow {
1227778aeb42STyler Hicks 	u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1228778aeb42STyler Hicks 	u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1229dd2a3b7aSMichael Halcrow 	int rc;
1230dd2a3b7aSMichael Halcrow 
1231778aeb42STyler Hicks 	rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
1232778aeb42STyler Hicks 				 inode);
1233778aeb42STyler Hicks 	if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1234778aeb42STyler Hicks 		return rc >= 0 ? -EINVAL : rc;
1235778aeb42STyler Hicks 	rc = ecryptfs_validate_marker(marker);
1236778aeb42STyler Hicks 	if (!rc)
1237778aeb42STyler Hicks 		ecryptfs_i_size_init(file_size, inode);
1238dd2a3b7aSMichael Halcrow 	return rc;
1239dd2a3b7aSMichael Halcrow }
1240dd2a3b7aSMichael Halcrow 
1241e77a56ddSMichael Halcrow void
1242e77a56ddSMichael Halcrow ecryptfs_write_header_metadata(char *virt,
1243e77a56ddSMichael Halcrow 			       struct ecryptfs_crypt_stat *crypt_stat,
1244237fead6SMichael Halcrow 			       size_t *written)
1245237fead6SMichael Halcrow {
1246237fead6SMichael Halcrow 	u32 header_extent_size;
1247237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1248237fead6SMichael Halcrow 
124945eaab79SMichael Halcrow 	header_extent_size = (u32)crypt_stat->extent_size;
1250237fead6SMichael Halcrow 	num_header_extents_at_front =
1251fa3ef1cbSTyler Hicks 		(u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
125229335c6aSHarvey Harrison 	put_unaligned_be32(header_extent_size, virt);
1253237fead6SMichael Halcrow 	virt += 4;
125429335c6aSHarvey Harrison 	put_unaligned_be16(num_header_extents_at_front, virt);
1255237fead6SMichael Halcrow 	(*written) = 6;
1256237fead6SMichael Halcrow }
1257237fead6SMichael Halcrow 
125830632870STyler Hicks struct kmem_cache *ecryptfs_header_cache;
1259237fead6SMichael Halcrow 
1260237fead6SMichael Halcrow /**
1261237fead6SMichael Halcrow  * ecryptfs_write_headers_virt
126222e78fafSMichael Halcrow  * @page_virt: The virtual address to write the headers to
126387b811c3SEric Sandeen  * @max: The size of memory allocated at page_virt
126422e78fafSMichael Halcrow  * @size: Set to the number of bytes written by this function
126522e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
126622e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
1267237fead6SMichael Halcrow  *
1268237fead6SMichael Halcrow  * Format version: 1
1269237fead6SMichael Halcrow  *
1270237fead6SMichael Halcrow  *   Header Extent:
1271237fead6SMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
1272237fead6SMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
1273237fead6SMichael Halcrow  *     Octets 16-19:      Flags
1274237fead6SMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
1275237fead6SMichael Halcrow  *      Octets 17-18:     Reserved
1276237fead6SMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
1277237fead6SMichael Halcrow  *                        Bit 2: Encrypted?
1278237fead6SMichael Halcrow  *                        Bits 3-8: Reserved
1279237fead6SMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
1280237fead6SMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
1281237fead6SMichael Halcrow  *                        (big-endian)
1282237fead6SMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
1283237fead6SMichael Halcrow  *   Data Extent 0:
1284237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1285237fead6SMichael Halcrow  *   Data Extent 1:
1286237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1287237fead6SMichael Halcrow  *   ...
1288237fead6SMichael Halcrow  *
1289237fead6SMichael Halcrow  * Returns zero on success
1290237fead6SMichael Halcrow  */
129187b811c3SEric Sandeen static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
129287b811c3SEric Sandeen 				       size_t *size,
1293237fead6SMichael Halcrow 				       struct ecryptfs_crypt_stat *crypt_stat,
1294237fead6SMichael Halcrow 				       struct dentry *ecryptfs_dentry)
1295237fead6SMichael Halcrow {
1296237fead6SMichael Halcrow 	int rc;
1297237fead6SMichael Halcrow 	size_t written;
1298237fead6SMichael Halcrow 	size_t offset;
1299237fead6SMichael Halcrow 
1300237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1301237fead6SMichael Halcrow 	write_ecryptfs_marker((page_virt + offset), &written);
1302237fead6SMichael Halcrow 	offset += written;
1303f4e60e6bSTyler Hicks 	ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
1304f4e60e6bSTyler Hicks 					&written);
1305237fead6SMichael Halcrow 	offset += written;
1306e77a56ddSMichael Halcrow 	ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1307e77a56ddSMichael Halcrow 				       &written);
1308237fead6SMichael Halcrow 	offset += written;
1309237fead6SMichael Halcrow 	rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1310237fead6SMichael Halcrow 					      ecryptfs_dentry, &written,
131187b811c3SEric Sandeen 					      max - offset);
1312237fead6SMichael Halcrow 	if (rc)
1313237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1314237fead6SMichael Halcrow 				"set; rc = [%d]\n", rc);
1315dd2a3b7aSMichael Halcrow 	if (size) {
1316dd2a3b7aSMichael Halcrow 		offset += written;
1317dd2a3b7aSMichael Halcrow 		*size = offset;
1318dd2a3b7aSMichael Halcrow 	}
1319dd2a3b7aSMichael Halcrow 	return rc;
1320dd2a3b7aSMichael Halcrow }
1321dd2a3b7aSMichael Halcrow 
132222e78fafSMichael Halcrow static int
1323b59db43aSTyler Hicks ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
13248faece5fSTyler Hicks 				    char *virt, size_t virt_len)
1325dd2a3b7aSMichael Halcrow {
1326d7cdc5feSMichael Halcrow 	int rc;
1327dd2a3b7aSMichael Halcrow 
1328b59db43aSTyler Hicks 	rc = ecryptfs_write_lower(ecryptfs_inode, virt,
13298faece5fSTyler Hicks 				  0, virt_len);
133096a7b9c2STyler Hicks 	if (rc < 0)
1331d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to write header "
133296a7b9c2STyler Hicks 		       "information to lower file; rc = [%d]\n", __func__, rc);
133396a7b9c2STyler Hicks 	else
133496a7b9c2STyler Hicks 		rc = 0;
133570456600SMichael Halcrow 	return rc;
1336dd2a3b7aSMichael Halcrow }
1337dd2a3b7aSMichael Halcrow 
133822e78fafSMichael Halcrow static int
133922e78fafSMichael Halcrow ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1340dd2a3b7aSMichael Halcrow 				 char *page_virt, size_t size)
1341dd2a3b7aSMichael Halcrow {
1342dd2a3b7aSMichael Halcrow 	int rc;
1343dd2a3b7aSMichael Halcrow 
1344dd2a3b7aSMichael Halcrow 	rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1345dd2a3b7aSMichael Halcrow 			       size, 0);
1346237fead6SMichael Halcrow 	return rc;
1347237fead6SMichael Halcrow }
1348237fead6SMichael Halcrow 
13498faece5fSTyler Hicks static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
13508faece5fSTyler Hicks 					       unsigned int order)
13518faece5fSTyler Hicks {
13528faece5fSTyler Hicks 	struct page *page;
13538faece5fSTyler Hicks 
13548faece5fSTyler Hicks 	page = alloc_pages(gfp_mask | __GFP_ZERO, order);
13558faece5fSTyler Hicks 	if (page)
13568faece5fSTyler Hicks 		return (unsigned long) page_address(page);
13578faece5fSTyler Hicks 	return 0;
13588faece5fSTyler Hicks }
13598faece5fSTyler Hicks 
1360237fead6SMichael Halcrow /**
1361dd2a3b7aSMichael Halcrow  * ecryptfs_write_metadata
1362b59db43aSTyler Hicks  * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
1363b59db43aSTyler Hicks  * @ecryptfs_inode: The newly created eCryptfs inode
1364237fead6SMichael Halcrow  *
1365237fead6SMichael Halcrow  * Write the file headers out.  This will likely involve a userspace
1366237fead6SMichael Halcrow  * callout, in which the session key is encrypted with one or more
1367237fead6SMichael Halcrow  * public keys and/or the passphrase necessary to do the encryption is
1368237fead6SMichael Halcrow  * retrieved via a prompt.  Exactly what happens at this point should
1369237fead6SMichael Halcrow  * be policy-dependent.
1370237fead6SMichael Halcrow  *
1371237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
1372237fead6SMichael Halcrow  */
1373b59db43aSTyler Hicks int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1374b59db43aSTyler Hicks 			    struct inode *ecryptfs_inode)
1375237fead6SMichael Halcrow {
1376d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1377b59db43aSTyler Hicks 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
13788faece5fSTyler Hicks 	unsigned int order;
1379cc11beffSMichael Halcrow 	char *virt;
13808faece5fSTyler Hicks 	size_t virt_len;
1381d7cdc5feSMichael Halcrow 	size_t size = 0;
1382237fead6SMichael Halcrow 	int rc = 0;
1383237fead6SMichael Halcrow 
1384e2bd99ecSMichael Halcrow 	if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1385e2bd99ecSMichael Halcrow 		if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
1386d7cdc5feSMichael Halcrow 			printk(KERN_ERR "Key is invalid; bailing out\n");
1387237fead6SMichael Halcrow 			rc = -EINVAL;
1388237fead6SMichael Halcrow 			goto out;
1389237fead6SMichael Halcrow 		}
1390237fead6SMichael Halcrow 	} else {
1391cc11beffSMichael Halcrow 		printk(KERN_WARNING "%s: Encrypted flag not set\n",
139218d1dbf1SHarvey Harrison 		       __func__);
1393237fead6SMichael Halcrow 		rc = -EINVAL;
1394237fead6SMichael Halcrow 		goto out;
1395237fead6SMichael Halcrow 	}
1396fa3ef1cbSTyler Hicks 	virt_len = crypt_stat->metadata_size;
13978faece5fSTyler Hicks 	order = get_order(virt_len);
1398237fead6SMichael Halcrow 	/* Released in this function */
13998faece5fSTyler Hicks 	virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
1400cc11beffSMichael Halcrow 	if (!virt) {
140118d1dbf1SHarvey Harrison 		printk(KERN_ERR "%s: Out of memory\n", __func__);
1402237fead6SMichael Halcrow 		rc = -ENOMEM;
1403237fead6SMichael Halcrow 		goto out;
1404237fead6SMichael Halcrow 	}
1405bd4f0fe8STyler Hicks 	/* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
14068faece5fSTyler Hicks 	rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
14078faece5fSTyler Hicks 					 ecryptfs_dentry);
1408237fead6SMichael Halcrow 	if (unlikely(rc)) {
1409cc11beffSMichael Halcrow 		printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
141018d1dbf1SHarvey Harrison 		       __func__, rc);
1411237fead6SMichael Halcrow 		goto out_free;
1412237fead6SMichael Halcrow 	}
1413dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
14148faece5fSTyler Hicks 		rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
14158faece5fSTyler Hicks 						      size);
1416dd2a3b7aSMichael Halcrow 	else
1417b59db43aSTyler Hicks 		rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
14188faece5fSTyler Hicks 							 virt_len);
1419dd2a3b7aSMichael Halcrow 	if (rc) {
1420cc11beffSMichael Halcrow 		printk(KERN_ERR "%s: Error writing metadata out to lower file; "
142118d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, rc);
1422dd2a3b7aSMichael Halcrow 		goto out_free;
1423237fead6SMichael Halcrow 	}
1424237fead6SMichael Halcrow out_free:
14258faece5fSTyler Hicks 	free_pages((unsigned long)virt, order);
1426237fead6SMichael Halcrow out:
1427237fead6SMichael Halcrow 	return rc;
1428237fead6SMichael Halcrow }
1429237fead6SMichael Halcrow 
1430dd2a3b7aSMichael Halcrow #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1431dd2a3b7aSMichael Halcrow #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
1432237fead6SMichael Halcrow static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
1433dd2a3b7aSMichael Halcrow 				 char *virt, int *bytes_read,
1434dd2a3b7aSMichael Halcrow 				 int validate_header_size)
1435237fead6SMichael Halcrow {
1436237fead6SMichael Halcrow 	int rc = 0;
1437237fead6SMichael Halcrow 	u32 header_extent_size;
1438237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1439237fead6SMichael Halcrow 
144029335c6aSHarvey Harrison 	header_extent_size = get_unaligned_be32(virt);
144129335c6aSHarvey Harrison 	virt += sizeof(__be32);
144229335c6aSHarvey Harrison 	num_header_extents_at_front = get_unaligned_be16(virt);
1443fa3ef1cbSTyler Hicks 	crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
1444cc11beffSMichael Halcrow 				     * (size_t)header_extent_size));
144529335c6aSHarvey Harrison 	(*bytes_read) = (sizeof(__be32) + sizeof(__be16));
1446dd2a3b7aSMichael Halcrow 	if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
1447fa3ef1cbSTyler Hicks 	    && (crypt_stat->metadata_size
1448dd2a3b7aSMichael Halcrow 		< ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
1449237fead6SMichael Halcrow 		rc = -EINVAL;
1450cc11beffSMichael Halcrow 		printk(KERN_WARNING "Invalid header size: [%zd]\n",
1451fa3ef1cbSTyler Hicks 		       crypt_stat->metadata_size);
1452237fead6SMichael Halcrow 	}
1453237fead6SMichael Halcrow 	return rc;
1454237fead6SMichael Halcrow }
1455237fead6SMichael Halcrow 
1456237fead6SMichael Halcrow /**
1457237fead6SMichael Halcrow  * set_default_header_data
145822e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1459237fead6SMichael Halcrow  *
1460237fead6SMichael Halcrow  * For version 0 file format; this function is only for backwards
1461237fead6SMichael Halcrow  * compatibility for files created with the prior versions of
1462237fead6SMichael Halcrow  * eCryptfs.
1463237fead6SMichael Halcrow  */
1464237fead6SMichael Halcrow static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1465237fead6SMichael Halcrow {
1466fa3ef1cbSTyler Hicks 	crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
1467237fead6SMichael Halcrow }
1468237fead6SMichael Halcrow 
14693aeb86eaSTyler Hicks void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
14703aeb86eaSTyler Hicks {
14713aeb86eaSTyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
14723aeb86eaSTyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
14733aeb86eaSTyler Hicks 	u64 file_size;
14743aeb86eaSTyler Hicks 
14753aeb86eaSTyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
14763aeb86eaSTyler Hicks 	mount_crypt_stat =
14773aeb86eaSTyler Hicks 		&ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
14783aeb86eaSTyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
14793aeb86eaSTyler Hicks 		file_size = i_size_read(ecryptfs_inode_to_lower(inode));
14803aeb86eaSTyler Hicks 		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
14813aeb86eaSTyler Hicks 			file_size += crypt_stat->metadata_size;
14823aeb86eaSTyler Hicks 	} else
14833aeb86eaSTyler Hicks 		file_size = get_unaligned_be64(page_virt);
14843aeb86eaSTyler Hicks 	i_size_write(inode, (loff_t)file_size);
14853aeb86eaSTyler Hicks 	crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
14863aeb86eaSTyler Hicks }
14873aeb86eaSTyler Hicks 
1488237fead6SMichael Halcrow /**
1489237fead6SMichael Halcrow  * ecryptfs_read_headers_virt
149022e78fafSMichael Halcrow  * @page_virt: The virtual address into which to read the headers
149122e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
149222e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
149322e78fafSMichael Halcrow  * @validate_header_size: Whether to validate the header size while reading
1494237fead6SMichael Halcrow  *
1495237fead6SMichael Halcrow  * Read/parse the header data. The header format is detailed in the
1496237fead6SMichael Halcrow  * comment block for the ecryptfs_write_headers_virt() function.
1497237fead6SMichael Halcrow  *
1498237fead6SMichael Halcrow  * Returns zero on success
1499237fead6SMichael Halcrow  */
1500237fead6SMichael Halcrow static int ecryptfs_read_headers_virt(char *page_virt,
1501237fead6SMichael Halcrow 				      struct ecryptfs_crypt_stat *crypt_stat,
1502dd2a3b7aSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
1503dd2a3b7aSMichael Halcrow 				      int validate_header_size)
1504237fead6SMichael Halcrow {
1505237fead6SMichael Halcrow 	int rc = 0;
1506237fead6SMichael Halcrow 	int offset;
1507237fead6SMichael Halcrow 	int bytes_read;
1508237fead6SMichael Halcrow 
1509237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
1510237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1511237fead6SMichael Halcrow 		ecryptfs_dentry->d_sb)->mount_crypt_stat;
1512237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
15137a86617eSTyler Hicks 	rc = ecryptfs_validate_marker(page_virt + offset);
15147a86617eSTyler Hicks 	if (rc)
1515237fead6SMichael Halcrow 		goto out;
15163aeb86eaSTyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
15173aeb86eaSTyler Hicks 		ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode);
1518237fead6SMichael Halcrow 	offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1519237fead6SMichael Halcrow 	rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1520237fead6SMichael Halcrow 				    &bytes_read);
1521237fead6SMichael Halcrow 	if (rc) {
1522237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1523237fead6SMichael Halcrow 		goto out;
1524237fead6SMichael Halcrow 	}
1525237fead6SMichael Halcrow 	if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1526237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1527237fead6SMichael Halcrow 				"file version [%d] is supported by this "
1528237fead6SMichael Halcrow 				"version of eCryptfs\n",
1529237fead6SMichael Halcrow 				crypt_stat->file_version,
1530237fead6SMichael Halcrow 				ECRYPTFS_SUPPORTED_FILE_VERSION);
1531237fead6SMichael Halcrow 		rc = -EINVAL;
1532237fead6SMichael Halcrow 		goto out;
1533237fead6SMichael Halcrow 	}
1534237fead6SMichael Halcrow 	offset += bytes_read;
1535237fead6SMichael Halcrow 	if (crypt_stat->file_version >= 1) {
1536237fead6SMichael Halcrow 		rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1537dd2a3b7aSMichael Halcrow 					   &bytes_read, validate_header_size);
1538237fead6SMichael Halcrow 		if (rc) {
1539237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Error reading header "
1540237fead6SMichael Halcrow 					"metadata; rc = [%d]\n", rc);
1541237fead6SMichael Halcrow 		}
1542237fead6SMichael Halcrow 		offset += bytes_read;
1543237fead6SMichael Halcrow 	} else
1544237fead6SMichael Halcrow 		set_default_header_data(crypt_stat);
1545237fead6SMichael Halcrow 	rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1546237fead6SMichael Halcrow 				       ecryptfs_dentry);
1547237fead6SMichael Halcrow out:
1548237fead6SMichael Halcrow 	return rc;
1549237fead6SMichael Halcrow }
1550237fead6SMichael Halcrow 
1551237fead6SMichael Halcrow /**
1552dd2a3b7aSMichael Halcrow  * ecryptfs_read_xattr_region
155322e78fafSMichael Halcrow  * @page_virt: The vitual address into which to read the xattr data
15542ed92554SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
1555dd2a3b7aSMichael Halcrow  *
1556dd2a3b7aSMichael Halcrow  * Attempts to read the crypto metadata from the extended attribute
1557dd2a3b7aSMichael Halcrow  * region of the lower file.
155822e78fafSMichael Halcrow  *
155922e78fafSMichael Halcrow  * Returns zero on success; non-zero on error
1560dd2a3b7aSMichael Halcrow  */
1561d7cdc5feSMichael Halcrow int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
1562dd2a3b7aSMichael Halcrow {
1563d7cdc5feSMichael Halcrow 	struct dentry *lower_dentry =
1564d7cdc5feSMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
1565dd2a3b7aSMichael Halcrow 	ssize_t size;
1566dd2a3b7aSMichael Halcrow 	int rc = 0;
1567dd2a3b7aSMichael Halcrow 
1568d7cdc5feSMichael Halcrow 	size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1569dd2a3b7aSMichael Halcrow 				       page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1570dd2a3b7aSMichael Halcrow 	if (size < 0) {
157125bd8174SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0))
157225bd8174SMichael Halcrow 			printk(KERN_INFO "Error attempting to read the [%s] "
157325bd8174SMichael Halcrow 			       "xattr from the lower file; return value = "
157425bd8174SMichael Halcrow 			       "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
1575dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1576dd2a3b7aSMichael Halcrow 		goto out;
1577dd2a3b7aSMichael Halcrow 	}
1578dd2a3b7aSMichael Halcrow out:
1579dd2a3b7aSMichael Halcrow 	return rc;
1580dd2a3b7aSMichael Halcrow }
1581dd2a3b7aSMichael Halcrow 
1582778aeb42STyler Hicks int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
15833b06b3ebSTyler Hicks 					    struct inode *inode)
1584dd2a3b7aSMichael Halcrow {
1585778aeb42STyler Hicks 	u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1586778aeb42STyler Hicks 	u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1587dd2a3b7aSMichael Halcrow 	int rc;
1588dd2a3b7aSMichael Halcrow 
1589778aeb42STyler Hicks 	rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1590778aeb42STyler Hicks 				     ECRYPTFS_XATTR_NAME, file_size,
1591778aeb42STyler Hicks 				     ECRYPTFS_SIZE_AND_MARKER_BYTES);
1592778aeb42STyler Hicks 	if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1593778aeb42STyler Hicks 		return rc >= 0 ? -EINVAL : rc;
1594778aeb42STyler Hicks 	rc = ecryptfs_validate_marker(marker);
1595778aeb42STyler Hicks 	if (!rc)
1596778aeb42STyler Hicks 		ecryptfs_i_size_init(file_size, inode);
1597dd2a3b7aSMichael Halcrow 	return rc;
1598dd2a3b7aSMichael Halcrow }
1599dd2a3b7aSMichael Halcrow 
1600dd2a3b7aSMichael Halcrow /**
1601dd2a3b7aSMichael Halcrow  * ecryptfs_read_metadata
1602dd2a3b7aSMichael Halcrow  *
1603dd2a3b7aSMichael Halcrow  * Common entry point for reading file metadata. From here, we could
1604dd2a3b7aSMichael Halcrow  * retrieve the header information from the header region of the file,
1605dd2a3b7aSMichael Halcrow  * the xattr region of the file, or some other repostory that is
1606dd2a3b7aSMichael Halcrow  * stored separately from the file itself. The current implementation
1607dd2a3b7aSMichael Halcrow  * supports retrieving the metadata information from the file contents
1608dd2a3b7aSMichael Halcrow  * and from the xattr region.
1609237fead6SMichael Halcrow  *
1610237fead6SMichael Halcrow  * Returns zero if valid headers found and parsed; non-zero otherwise
1611237fead6SMichael Halcrow  */
1612d7cdc5feSMichael Halcrow int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
1613237fead6SMichael Halcrow {
1614bb450361STim Gardner 	int rc;
1615bb450361STim Gardner 	char *page_virt;
1616d7cdc5feSMichael Halcrow 	struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
1617237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1618d7cdc5feSMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1619e77a56ddSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1620e77a56ddSMichael Halcrow 		&ecryptfs_superblock_to_private(
1621e77a56ddSMichael Halcrow 			ecryptfs_dentry->d_sb)->mount_crypt_stat;
1622237fead6SMichael Halcrow 
1623e77a56ddSMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1624e77a56ddSMichael Halcrow 						      mount_crypt_stat);
1625237fead6SMichael Halcrow 	/* Read the first page from the underlying file */
162630632870STyler Hicks 	page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
1627237fead6SMichael Halcrow 	if (!page_virt) {
1628237fead6SMichael Halcrow 		rc = -ENOMEM;
1629d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Unable to allocate page_virt\n",
163018d1dbf1SHarvey Harrison 		       __func__);
1631237fead6SMichael Halcrow 		goto out;
1632237fead6SMichael Halcrow 	}
1633d7cdc5feSMichael Halcrow 	rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1634d7cdc5feSMichael Halcrow 				 ecryptfs_inode);
163596a7b9c2STyler Hicks 	if (rc >= 0)
1636237fead6SMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1637dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1638dd2a3b7aSMichael Halcrow 						ECRYPTFS_VALIDATE_HEADER_SIZE);
1639dd2a3b7aSMichael Halcrow 	if (rc) {
1640bb450361STim Gardner 		/* metadata is not in the file header, so try xattrs */
16411984c23fSTyler Hicks 		memset(page_virt, 0, PAGE_CACHE_SIZE);
1642d7cdc5feSMichael Halcrow 		rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
1643237fead6SMichael Halcrow 		if (rc) {
1644dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
164530373dc0STim Gardner 			       "file header region or xattr region, inode %lu\n",
164630373dc0STim Gardner 				ecryptfs_inode->i_ino);
1647237fead6SMichael Halcrow 			rc = -EINVAL;
1648dd2a3b7aSMichael Halcrow 			goto out;
1649dd2a3b7aSMichael Halcrow 		}
1650dd2a3b7aSMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1651dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1652dd2a3b7aSMichael Halcrow 						ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1653dd2a3b7aSMichael Halcrow 		if (rc) {
1654dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
165530373dc0STim Gardner 			       "file xattr region either, inode %lu\n",
165630373dc0STim Gardner 				ecryptfs_inode->i_ino);
1657dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1658dd2a3b7aSMichael Halcrow 		}
1659dd2a3b7aSMichael Halcrow 		if (crypt_stat->mount_crypt_stat->flags
1660dd2a3b7aSMichael Halcrow 		    & ECRYPTFS_XATTR_METADATA_ENABLED) {
1661dd2a3b7aSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1662dd2a3b7aSMichael Halcrow 		} else {
1663dd2a3b7aSMichael Halcrow 			printk(KERN_WARNING "Attempt to access file with "
1664dd2a3b7aSMichael Halcrow 			       "crypto metadata only in the extended attribute "
1665dd2a3b7aSMichael Halcrow 			       "region, but eCryptfs was mounted without "
1666dd2a3b7aSMichael Halcrow 			       "xattr support enabled. eCryptfs will not treat "
166730373dc0STim Gardner 			       "this like an encrypted file, inode %lu\n",
166830373dc0STim Gardner 				ecryptfs_inode->i_ino);
1669dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1670dd2a3b7aSMichael Halcrow 		}
1671237fead6SMichael Halcrow 	}
1672237fead6SMichael Halcrow out:
1673237fead6SMichael Halcrow 	if (page_virt) {
1674237fead6SMichael Halcrow 		memset(page_virt, 0, PAGE_CACHE_SIZE);
167530632870STyler Hicks 		kmem_cache_free(ecryptfs_header_cache, page_virt);
1676237fead6SMichael Halcrow 	}
1677237fead6SMichael Halcrow 	return rc;
1678237fead6SMichael Halcrow }
1679237fead6SMichael Halcrow 
1680237fead6SMichael Halcrow /**
168151ca58dcSMichael Halcrow  * ecryptfs_encrypt_filename - encrypt filename
168251ca58dcSMichael Halcrow  *
168351ca58dcSMichael Halcrow  * CBC-encrypts the filename. We do not want to encrypt the same
168451ca58dcSMichael Halcrow  * filename with the same key and IV, which may happen with hard
168551ca58dcSMichael Halcrow  * links, so we prepend random bits to each filename.
168651ca58dcSMichael Halcrow  *
168751ca58dcSMichael Halcrow  * Returns zero on success; non-zero otherwise
168851ca58dcSMichael Halcrow  */
168951ca58dcSMichael Halcrow static int
169051ca58dcSMichael Halcrow ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
169151ca58dcSMichael Halcrow 			  struct ecryptfs_crypt_stat *crypt_stat,
169251ca58dcSMichael Halcrow 			  struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
169351ca58dcSMichael Halcrow {
169451ca58dcSMichael Halcrow 	int rc = 0;
169551ca58dcSMichael Halcrow 
169651ca58dcSMichael Halcrow 	filename->encrypted_filename = NULL;
169751ca58dcSMichael Halcrow 	filename->encrypted_filename_size = 0;
169851ca58dcSMichael Halcrow 	if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
169951ca58dcSMichael Halcrow 	    || (mount_crypt_stat && (mount_crypt_stat->flags
170051ca58dcSMichael Halcrow 				     & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
170151ca58dcSMichael Halcrow 		size_t packet_size;
170251ca58dcSMichael Halcrow 		size_t remaining_bytes;
170351ca58dcSMichael Halcrow 
170451ca58dcSMichael Halcrow 		rc = ecryptfs_write_tag_70_packet(
170551ca58dcSMichael Halcrow 			NULL, NULL,
170651ca58dcSMichael Halcrow 			&filename->encrypted_filename_size,
170751ca58dcSMichael Halcrow 			mount_crypt_stat, NULL,
170851ca58dcSMichael Halcrow 			filename->filename_size);
170951ca58dcSMichael Halcrow 		if (rc) {
171051ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to get packet "
171151ca58dcSMichael Halcrow 			       "size for tag 72; rc = [%d]\n", __func__,
171251ca58dcSMichael Halcrow 			       rc);
171351ca58dcSMichael Halcrow 			filename->encrypted_filename_size = 0;
171451ca58dcSMichael Halcrow 			goto out;
171551ca58dcSMichael Halcrow 		}
171651ca58dcSMichael Halcrow 		filename->encrypted_filename =
171751ca58dcSMichael Halcrow 			kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
171851ca58dcSMichael Halcrow 		if (!filename->encrypted_filename) {
171951ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
1720df261c52SMichael Halcrow 			       "to kmalloc [%zd] bytes\n", __func__,
172151ca58dcSMichael Halcrow 			       filename->encrypted_filename_size);
172251ca58dcSMichael Halcrow 			rc = -ENOMEM;
172351ca58dcSMichael Halcrow 			goto out;
172451ca58dcSMichael Halcrow 		}
172551ca58dcSMichael Halcrow 		remaining_bytes = filename->encrypted_filename_size;
172651ca58dcSMichael Halcrow 		rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
172751ca58dcSMichael Halcrow 						  &remaining_bytes,
172851ca58dcSMichael Halcrow 						  &packet_size,
172951ca58dcSMichael Halcrow 						  mount_crypt_stat,
173051ca58dcSMichael Halcrow 						  filename->filename,
173151ca58dcSMichael Halcrow 						  filename->filename_size);
173251ca58dcSMichael Halcrow 		if (rc) {
173351ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to generate "
173451ca58dcSMichael Halcrow 			       "tag 70 packet; rc = [%d]\n", __func__,
173551ca58dcSMichael Halcrow 			       rc);
173651ca58dcSMichael Halcrow 			kfree(filename->encrypted_filename);
173751ca58dcSMichael Halcrow 			filename->encrypted_filename = NULL;
173851ca58dcSMichael Halcrow 			filename->encrypted_filename_size = 0;
173951ca58dcSMichael Halcrow 			goto out;
174051ca58dcSMichael Halcrow 		}
174151ca58dcSMichael Halcrow 		filename->encrypted_filename_size = packet_size;
174251ca58dcSMichael Halcrow 	} else {
174351ca58dcSMichael Halcrow 		printk(KERN_ERR "%s: No support for requested filename "
174451ca58dcSMichael Halcrow 		       "encryption method in this release\n", __func__);
1745df6ad33bSTyler Hicks 		rc = -EOPNOTSUPP;
174651ca58dcSMichael Halcrow 		goto out;
174751ca58dcSMichael Halcrow 	}
174851ca58dcSMichael Halcrow out:
174951ca58dcSMichael Halcrow 	return rc;
175051ca58dcSMichael Halcrow }
175151ca58dcSMichael Halcrow 
175251ca58dcSMichael Halcrow static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
175351ca58dcSMichael Halcrow 				  const char *name, size_t name_size)
175451ca58dcSMichael Halcrow {
175551ca58dcSMichael Halcrow 	int rc = 0;
175651ca58dcSMichael Halcrow 
1757fd9fc842STyler Hicks 	(*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
175851ca58dcSMichael Halcrow 	if (!(*copied_name)) {
175951ca58dcSMichael Halcrow 		rc = -ENOMEM;
176051ca58dcSMichael Halcrow 		goto out;
176151ca58dcSMichael Halcrow 	}
176251ca58dcSMichael Halcrow 	memcpy((void *)(*copied_name), (void *)name, name_size);
176351ca58dcSMichael Halcrow 	(*copied_name)[(name_size)] = '\0';	/* Only for convenience
176451ca58dcSMichael Halcrow 						 * in printing out the
176551ca58dcSMichael Halcrow 						 * string in debug
176651ca58dcSMichael Halcrow 						 * messages */
1767fd9fc842STyler Hicks 	(*copied_name_size) = name_size;
176851ca58dcSMichael Halcrow out:
176951ca58dcSMichael Halcrow 	return rc;
177051ca58dcSMichael Halcrow }
177151ca58dcSMichael Halcrow 
177251ca58dcSMichael Halcrow /**
1773f4aad16aSMichael Halcrow  * ecryptfs_process_key_cipher - Perform key cipher initialization.
1774237fead6SMichael Halcrow  * @key_tfm: Crypto context for key material, set by this function
1775e5d9cbdeSMichael Halcrow  * @cipher_name: Name of the cipher
1776e5d9cbdeSMichael Halcrow  * @key_size: Size of the key in bytes
1777237fead6SMichael Halcrow  *
1778237fead6SMichael Halcrow  * Returns zero on success. Any crypto_tfm structs allocated here
1779237fead6SMichael Halcrow  * should be released by other functions, such as on a superblock put
1780237fead6SMichael Halcrow  * event, regardless of whether this function succeeds for fails.
1781237fead6SMichael Halcrow  */
1782cd9d67dfSMichael Halcrow static int
1783f4aad16aSMichael Halcrow ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1784f4aad16aSMichael Halcrow 			    char *cipher_name, size_t *key_size)
1785237fead6SMichael Halcrow {
1786237fead6SMichael Halcrow 	char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
1787ece550f5SDan Carpenter 	char *full_alg_name = NULL;
1788237fead6SMichael Halcrow 	int rc;
1789237fead6SMichael Halcrow 
1790e5d9cbdeSMichael Halcrow 	*key_tfm = NULL;
1791e5d9cbdeSMichael Halcrow 	if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1792237fead6SMichael Halcrow 		rc = -EINVAL;
1793df261c52SMichael Halcrow 		printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
1794e5d9cbdeSMichael Halcrow 		      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1795237fead6SMichael Halcrow 		goto out;
1796237fead6SMichael Halcrow 	}
17978bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
17988bba066fSMichael Halcrow 						    "ecb");
17998bba066fSMichael Halcrow 	if (rc)
18008bba066fSMichael Halcrow 		goto out;
18018bba066fSMichael Halcrow 	*key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
18028bba066fSMichael Halcrow 	if (IS_ERR(*key_tfm)) {
18038bba066fSMichael Halcrow 		rc = PTR_ERR(*key_tfm);
1804237fead6SMichael Halcrow 		printk(KERN_ERR "Unable to allocate crypto cipher with name "
180538268498SDave Hansen 		       "[%s]; rc = [%d]\n", full_alg_name, rc);
1806237fead6SMichael Halcrow 		goto out;
1807237fead6SMichael Halcrow 	}
18088bba066fSMichael Halcrow 	crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
18098bba066fSMichael Halcrow 	if (*key_size == 0) {
18108bba066fSMichael Halcrow 		struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
18118bba066fSMichael Halcrow 
18128bba066fSMichael Halcrow 		*key_size = alg->max_keysize;
18138bba066fSMichael Halcrow 	}
1814e5d9cbdeSMichael Halcrow 	get_random_bytes(dummy_key, *key_size);
18158bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
1816237fead6SMichael Halcrow 	if (rc) {
1817df261c52SMichael Halcrow 		printk(KERN_ERR "Error attempting to set key of size [%zd] for "
181838268498SDave Hansen 		       "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
181938268498SDave Hansen 		       rc);
1820237fead6SMichael Halcrow 		rc = -EINVAL;
1821237fead6SMichael Halcrow 		goto out;
1822237fead6SMichael Halcrow 	}
1823237fead6SMichael Halcrow out:
1824ece550f5SDan Carpenter 	kfree(full_alg_name);
1825237fead6SMichael Halcrow 	return rc;
1826237fead6SMichael Halcrow }
1827f4aad16aSMichael Halcrow 
1828f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_tfm_cache;
18297896b631SAdrian Bunk static struct list_head key_tfm_list;
1830af440f52SEric Sandeen struct mutex key_tfm_list_mutex;
1831f4aad16aSMichael Halcrow 
18327371a382SJerome Marchand int __init ecryptfs_init_crypto(void)
1833f4aad16aSMichael Halcrow {
1834f4aad16aSMichael Halcrow 	mutex_init(&key_tfm_list_mutex);
1835f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&key_tfm_list);
1836f4aad16aSMichael Halcrow 	return 0;
1837f4aad16aSMichael Halcrow }
1838f4aad16aSMichael Halcrow 
1839af440f52SEric Sandeen /**
1840af440f52SEric Sandeen  * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1841af440f52SEric Sandeen  *
1842af440f52SEric Sandeen  * Called only at module unload time
1843af440f52SEric Sandeen  */
1844fcd12835SMichael Halcrow int ecryptfs_destroy_crypto(void)
1845f4aad16aSMichael Halcrow {
1846f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1847f4aad16aSMichael Halcrow 
1848f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1849f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1850f4aad16aSMichael Halcrow 				 key_tfm_list) {
1851f4aad16aSMichael Halcrow 		list_del(&key_tfm->key_tfm_list);
1852f4aad16aSMichael Halcrow 		if (key_tfm->key_tfm)
1853f4aad16aSMichael Halcrow 			crypto_free_blkcipher(key_tfm->key_tfm);
1854f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1855f4aad16aSMichael Halcrow 	}
1856f4aad16aSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
1857f4aad16aSMichael Halcrow 	return 0;
1858f4aad16aSMichael Halcrow }
1859f4aad16aSMichael Halcrow 
1860f4aad16aSMichael Halcrow int
1861f4aad16aSMichael Halcrow ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1862f4aad16aSMichael Halcrow 			 size_t key_size)
1863f4aad16aSMichael Halcrow {
1864f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *tmp_tfm;
1865f4aad16aSMichael Halcrow 	int rc = 0;
1866f4aad16aSMichael Halcrow 
1867af440f52SEric Sandeen 	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1868af440f52SEric Sandeen 
1869f4aad16aSMichael Halcrow 	tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1870f4aad16aSMichael Halcrow 	if (key_tfm != NULL)
1871f4aad16aSMichael Halcrow 		(*key_tfm) = tmp_tfm;
1872f4aad16aSMichael Halcrow 	if (!tmp_tfm) {
1873f4aad16aSMichael Halcrow 		rc = -ENOMEM;
1874f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to allocate from "
1875f4aad16aSMichael Halcrow 		       "ecryptfs_key_tfm_cache\n");
1876f4aad16aSMichael Halcrow 		goto out;
1877f4aad16aSMichael Halcrow 	}
1878f4aad16aSMichael Halcrow 	mutex_init(&tmp_tfm->key_tfm_mutex);
1879f4aad16aSMichael Halcrow 	strncpy(tmp_tfm->cipher_name, cipher_name,
1880f4aad16aSMichael Halcrow 		ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1881b8862906SEric Sandeen 	tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
1882f4aad16aSMichael Halcrow 	tmp_tfm->key_size = key_size;
18835dda6992SMichael Halcrow 	rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1884f4aad16aSMichael Halcrow 					 tmp_tfm->cipher_name,
18855dda6992SMichael Halcrow 					 &tmp_tfm->key_size);
18865dda6992SMichael Halcrow 	if (rc) {
1887f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to initialize key TFM "
1888f4aad16aSMichael Halcrow 		       "cipher with name = [%s]; rc = [%d]\n",
1889f4aad16aSMichael Halcrow 		       tmp_tfm->cipher_name, rc);
1890f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1891f4aad16aSMichael Halcrow 		if (key_tfm != NULL)
1892f4aad16aSMichael Halcrow 			(*key_tfm) = NULL;
1893f4aad16aSMichael Halcrow 		goto out;
1894f4aad16aSMichael Halcrow 	}
1895f4aad16aSMichael Halcrow 	list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1896f4aad16aSMichael Halcrow out:
1897f4aad16aSMichael Halcrow 	return rc;
1898f4aad16aSMichael Halcrow }
1899f4aad16aSMichael Halcrow 
1900af440f52SEric Sandeen /**
1901af440f52SEric Sandeen  * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
1902af440f52SEric Sandeen  * @cipher_name: the name of the cipher to search for
1903af440f52SEric Sandeen  * @key_tfm: set to corresponding tfm if found
1904af440f52SEric Sandeen  *
1905af440f52SEric Sandeen  * Searches for cached key_tfm matching @cipher_name
1906af440f52SEric Sandeen  * Must be called with &key_tfm_list_mutex held
1907af440f52SEric Sandeen  * Returns 1 if found, with @key_tfm set
1908af440f52SEric Sandeen  * Returns 0 if not found, with @key_tfm set to NULL
1909af440f52SEric Sandeen  */
1910af440f52SEric Sandeen int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1911af440f52SEric Sandeen {
1912af440f52SEric Sandeen 	struct ecryptfs_key_tfm *tmp_key_tfm;
1913af440f52SEric Sandeen 
1914af440f52SEric Sandeen 	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1915af440f52SEric Sandeen 
1916af440f52SEric Sandeen 	list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
1917af440f52SEric Sandeen 		if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
1918af440f52SEric Sandeen 			if (key_tfm)
1919af440f52SEric Sandeen 				(*key_tfm) = tmp_key_tfm;
1920af440f52SEric Sandeen 			return 1;
1921af440f52SEric Sandeen 		}
1922af440f52SEric Sandeen 	}
1923af440f52SEric Sandeen 	if (key_tfm)
1924af440f52SEric Sandeen 		(*key_tfm) = NULL;
1925af440f52SEric Sandeen 	return 0;
1926af440f52SEric Sandeen }
1927af440f52SEric Sandeen 
1928af440f52SEric Sandeen /**
1929af440f52SEric Sandeen  * ecryptfs_get_tfm_and_mutex_for_cipher_name
1930af440f52SEric Sandeen  *
1931af440f52SEric Sandeen  * @tfm: set to cached tfm found, or new tfm created
1932af440f52SEric Sandeen  * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
1933af440f52SEric Sandeen  * @cipher_name: the name of the cipher to search for and/or add
1934af440f52SEric Sandeen  *
1935af440f52SEric Sandeen  * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
1936af440f52SEric Sandeen  * Searches for cached item first, and creates new if not found.
1937af440f52SEric Sandeen  * Returns 0 on success, non-zero if adding new cipher failed
1938af440f52SEric Sandeen  */
1939f4aad16aSMichael Halcrow int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1940f4aad16aSMichael Halcrow 					       struct mutex **tfm_mutex,
1941f4aad16aSMichael Halcrow 					       char *cipher_name)
1942f4aad16aSMichael Halcrow {
1943f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm;
1944f4aad16aSMichael Halcrow 	int rc = 0;
1945f4aad16aSMichael Halcrow 
1946f4aad16aSMichael Halcrow 	(*tfm) = NULL;
1947f4aad16aSMichael Halcrow 	(*tfm_mutex) = NULL;
1948af440f52SEric Sandeen 
1949f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1950af440f52SEric Sandeen 	if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
19515dda6992SMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
19525dda6992SMichael Halcrow 		if (rc) {
1953af440f52SEric Sandeen 			printk(KERN_ERR "Error adding new key_tfm to list; "
1954af440f52SEric Sandeen 					"rc = [%d]\n", rc);
1955f4aad16aSMichael Halcrow 			goto out;
1956f4aad16aSMichael Halcrow 		}
1957af440f52SEric Sandeen 	}
1958f4aad16aSMichael Halcrow 	(*tfm) = key_tfm->key_tfm;
1959f4aad16aSMichael Halcrow 	(*tfm_mutex) = &key_tfm->key_tfm_mutex;
1960f4aad16aSMichael Halcrow out:
196171fd5179SCyrill Gorcunov 	mutex_unlock(&key_tfm_list_mutex);
1962f4aad16aSMichael Halcrow 	return rc;
1963f4aad16aSMichael Halcrow }
196451ca58dcSMichael Halcrow 
196551ca58dcSMichael Halcrow /* 64 characters forming a 6-bit target field */
196651ca58dcSMichael Halcrow static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
196751ca58dcSMichael Halcrow 						 "EFGHIJKLMNOPQRST"
196851ca58dcSMichael Halcrow 						 "UVWXYZabcdefghij"
196951ca58dcSMichael Halcrow 						 "klmnopqrstuvwxyz");
197051ca58dcSMichael Halcrow 
197151ca58dcSMichael Halcrow /* We could either offset on every reverse map or just pad some 0x00's
197251ca58dcSMichael Halcrow  * at the front here */
19730f751e64STyler Hicks static const unsigned char filename_rev_map[256] = {
197451ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
197551ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
197651ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
197751ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
197851ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
197951ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
198051ca58dcSMichael Halcrow 	0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
198151ca58dcSMichael Halcrow 	0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
198251ca58dcSMichael Halcrow 	0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
198351ca58dcSMichael Halcrow 	0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
198451ca58dcSMichael Halcrow 	0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
198551ca58dcSMichael Halcrow 	0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
198651ca58dcSMichael Halcrow 	0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
198751ca58dcSMichael Halcrow 	0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
198851ca58dcSMichael Halcrow 	0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
19890f751e64STyler Hicks 	0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
199051ca58dcSMichael Halcrow };
199151ca58dcSMichael Halcrow 
199251ca58dcSMichael Halcrow /**
199351ca58dcSMichael Halcrow  * ecryptfs_encode_for_filename
199451ca58dcSMichael Halcrow  * @dst: Destination location for encoded filename
199551ca58dcSMichael Halcrow  * @dst_size: Size of the encoded filename in bytes
199651ca58dcSMichael Halcrow  * @src: Source location for the filename to encode
199751ca58dcSMichael Halcrow  * @src_size: Size of the source in bytes
199851ca58dcSMichael Halcrow  */
199937028758SCong Ding static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
200051ca58dcSMichael Halcrow 				  unsigned char *src, size_t src_size)
200151ca58dcSMichael Halcrow {
200251ca58dcSMichael Halcrow 	size_t num_blocks;
200351ca58dcSMichael Halcrow 	size_t block_num = 0;
200451ca58dcSMichael Halcrow 	size_t dst_offset = 0;
200551ca58dcSMichael Halcrow 	unsigned char last_block[3];
200651ca58dcSMichael Halcrow 
200751ca58dcSMichael Halcrow 	if (src_size == 0) {
200851ca58dcSMichael Halcrow 		(*dst_size) = 0;
200951ca58dcSMichael Halcrow 		goto out;
201051ca58dcSMichael Halcrow 	}
201151ca58dcSMichael Halcrow 	num_blocks = (src_size / 3);
201251ca58dcSMichael Halcrow 	if ((src_size % 3) == 0) {
201351ca58dcSMichael Halcrow 		memcpy(last_block, (&src[src_size - 3]), 3);
201451ca58dcSMichael Halcrow 	} else {
201551ca58dcSMichael Halcrow 		num_blocks++;
201651ca58dcSMichael Halcrow 		last_block[2] = 0x00;
201751ca58dcSMichael Halcrow 		switch (src_size % 3) {
201851ca58dcSMichael Halcrow 		case 1:
201951ca58dcSMichael Halcrow 			last_block[0] = src[src_size - 1];
202051ca58dcSMichael Halcrow 			last_block[1] = 0x00;
202151ca58dcSMichael Halcrow 			break;
202251ca58dcSMichael Halcrow 		case 2:
202351ca58dcSMichael Halcrow 			last_block[0] = src[src_size - 2];
202451ca58dcSMichael Halcrow 			last_block[1] = src[src_size - 1];
202551ca58dcSMichael Halcrow 		}
202651ca58dcSMichael Halcrow 	}
202751ca58dcSMichael Halcrow 	(*dst_size) = (num_blocks * 4);
202851ca58dcSMichael Halcrow 	if (!dst)
202951ca58dcSMichael Halcrow 		goto out;
203051ca58dcSMichael Halcrow 	while (block_num < num_blocks) {
203151ca58dcSMichael Halcrow 		unsigned char *src_block;
203251ca58dcSMichael Halcrow 		unsigned char dst_block[4];
203351ca58dcSMichael Halcrow 
203451ca58dcSMichael Halcrow 		if (block_num == (num_blocks - 1))
203551ca58dcSMichael Halcrow 			src_block = last_block;
203651ca58dcSMichael Halcrow 		else
203751ca58dcSMichael Halcrow 			src_block = &src[block_num * 3];
203851ca58dcSMichael Halcrow 		dst_block[0] = ((src_block[0] >> 2) & 0x3F);
203951ca58dcSMichael Halcrow 		dst_block[1] = (((src_block[0] << 4) & 0x30)
204051ca58dcSMichael Halcrow 				| ((src_block[1] >> 4) & 0x0F));
204151ca58dcSMichael Halcrow 		dst_block[2] = (((src_block[1] << 2) & 0x3C)
204251ca58dcSMichael Halcrow 				| ((src_block[2] >> 6) & 0x03));
204351ca58dcSMichael Halcrow 		dst_block[3] = (src_block[2] & 0x3F);
204451ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[0]];
204551ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[1]];
204651ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[2]];
204751ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[3]];
204851ca58dcSMichael Halcrow 		block_num++;
204951ca58dcSMichael Halcrow 	}
205051ca58dcSMichael Halcrow out:
205151ca58dcSMichael Halcrow 	return;
205251ca58dcSMichael Halcrow }
205351ca58dcSMichael Halcrow 
20544a26620dSTyler Hicks static size_t ecryptfs_max_decoded_size(size_t encoded_size)
20554a26620dSTyler Hicks {
20564a26620dSTyler Hicks 	/* Not exact; conservatively long. Every block of 4
20574a26620dSTyler Hicks 	 * encoded characters decodes into a block of 3
20584a26620dSTyler Hicks 	 * decoded characters. This segment of code provides
20594a26620dSTyler Hicks 	 * the caller with the maximum amount of allocated
20604a26620dSTyler Hicks 	 * space that @dst will need to point to in a
20614a26620dSTyler Hicks 	 * subsequent call. */
20624a26620dSTyler Hicks 	return ((encoded_size + 1) * 3) / 4;
20634a26620dSTyler Hicks }
20644a26620dSTyler Hicks 
206571c11c37SMichael Halcrow /**
206671c11c37SMichael Halcrow  * ecryptfs_decode_from_filename
206771c11c37SMichael Halcrow  * @dst: If NULL, this function only sets @dst_size and returns. If
206871c11c37SMichael Halcrow  *       non-NULL, this function decodes the encoded octets in @src
206971c11c37SMichael Halcrow  *       into the memory that @dst points to.
207071c11c37SMichael Halcrow  * @dst_size: Set to the size of the decoded string.
207171c11c37SMichael Halcrow  * @src: The encoded set of octets to decode.
207271c11c37SMichael Halcrow  * @src_size: The size of the encoded set of octets to decode.
207371c11c37SMichael Halcrow  */
207471c11c37SMichael Halcrow static void
207571c11c37SMichael Halcrow ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
207651ca58dcSMichael Halcrow 			      const unsigned char *src, size_t src_size)
207751ca58dcSMichael Halcrow {
207851ca58dcSMichael Halcrow 	u8 current_bit_offset = 0;
207951ca58dcSMichael Halcrow 	size_t src_byte_offset = 0;
208051ca58dcSMichael Halcrow 	size_t dst_byte_offset = 0;
208151ca58dcSMichael Halcrow 
208251ca58dcSMichael Halcrow 	if (dst == NULL) {
20834a26620dSTyler Hicks 		(*dst_size) = ecryptfs_max_decoded_size(src_size);
208451ca58dcSMichael Halcrow 		goto out;
208551ca58dcSMichael Halcrow 	}
208651ca58dcSMichael Halcrow 	while (src_byte_offset < src_size) {
208751ca58dcSMichael Halcrow 		unsigned char src_byte =
208851ca58dcSMichael Halcrow 				filename_rev_map[(int)src[src_byte_offset]];
208951ca58dcSMichael Halcrow 
209051ca58dcSMichael Halcrow 		switch (current_bit_offset) {
209151ca58dcSMichael Halcrow 		case 0:
209251ca58dcSMichael Halcrow 			dst[dst_byte_offset] = (src_byte << 2);
209351ca58dcSMichael Halcrow 			current_bit_offset = 6;
209451ca58dcSMichael Halcrow 			break;
209551ca58dcSMichael Halcrow 		case 6:
209651ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte >> 4);
209751ca58dcSMichael Halcrow 			dst[dst_byte_offset] = ((src_byte & 0xF)
209851ca58dcSMichael Halcrow 						 << 4);
209951ca58dcSMichael Halcrow 			current_bit_offset = 4;
210051ca58dcSMichael Halcrow 			break;
210151ca58dcSMichael Halcrow 		case 4:
210251ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte >> 2);
210351ca58dcSMichael Halcrow 			dst[dst_byte_offset] = (src_byte << 6);
210451ca58dcSMichael Halcrow 			current_bit_offset = 2;
210551ca58dcSMichael Halcrow 			break;
210651ca58dcSMichael Halcrow 		case 2:
210751ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte);
210851ca58dcSMichael Halcrow 			dst[dst_byte_offset] = 0;
210951ca58dcSMichael Halcrow 			current_bit_offset = 0;
211051ca58dcSMichael Halcrow 			break;
211151ca58dcSMichael Halcrow 		}
211251ca58dcSMichael Halcrow 		src_byte_offset++;
211351ca58dcSMichael Halcrow 	}
211451ca58dcSMichael Halcrow 	(*dst_size) = dst_byte_offset;
211551ca58dcSMichael Halcrow out:
211671c11c37SMichael Halcrow 	return;
211751ca58dcSMichael Halcrow }
211851ca58dcSMichael Halcrow 
211951ca58dcSMichael Halcrow /**
212051ca58dcSMichael Halcrow  * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
212151ca58dcSMichael Halcrow  * @crypt_stat: The crypt_stat struct associated with the file anem to encode
212251ca58dcSMichael Halcrow  * @name: The plaintext name
212351ca58dcSMichael Halcrow  * @length: The length of the plaintext
212451ca58dcSMichael Halcrow  * @encoded_name: The encypted name
212551ca58dcSMichael Halcrow  *
212651ca58dcSMichael Halcrow  * Encrypts and encodes a filename into something that constitutes a
212751ca58dcSMichael Halcrow  * valid filename for a filesystem, with printable characters.
212851ca58dcSMichael Halcrow  *
212951ca58dcSMichael Halcrow  * We assume that we have a properly initialized crypto context,
213051ca58dcSMichael Halcrow  * pointed to by crypt_stat->tfm.
213151ca58dcSMichael Halcrow  *
213251ca58dcSMichael Halcrow  * Returns zero on success; non-zero on otherwise
213351ca58dcSMichael Halcrow  */
213451ca58dcSMichael Halcrow int ecryptfs_encrypt_and_encode_filename(
213551ca58dcSMichael Halcrow 	char **encoded_name,
213651ca58dcSMichael Halcrow 	size_t *encoded_name_size,
213751ca58dcSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
213851ca58dcSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
213951ca58dcSMichael Halcrow 	const char *name, size_t name_size)
214051ca58dcSMichael Halcrow {
214151ca58dcSMichael Halcrow 	size_t encoded_name_no_prefix_size;
214251ca58dcSMichael Halcrow 	int rc = 0;
214351ca58dcSMichael Halcrow 
214451ca58dcSMichael Halcrow 	(*encoded_name) = NULL;
214551ca58dcSMichael Halcrow 	(*encoded_name_size) = 0;
214651ca58dcSMichael Halcrow 	if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES))
214751ca58dcSMichael Halcrow 	    || (mount_crypt_stat && (mount_crypt_stat->flags
214851ca58dcSMichael Halcrow 				     & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) {
214951ca58dcSMichael Halcrow 		struct ecryptfs_filename *filename;
215051ca58dcSMichael Halcrow 
215151ca58dcSMichael Halcrow 		filename = kzalloc(sizeof(*filename), GFP_KERNEL);
215251ca58dcSMichael Halcrow 		if (!filename) {
215351ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
2154a8f12864SMichael Halcrow 			       "to kzalloc [%zd] bytes\n", __func__,
215551ca58dcSMichael Halcrow 			       sizeof(*filename));
215651ca58dcSMichael Halcrow 			rc = -ENOMEM;
215751ca58dcSMichael Halcrow 			goto out;
215851ca58dcSMichael Halcrow 		}
215951ca58dcSMichael Halcrow 		filename->filename = (char *)name;
216051ca58dcSMichael Halcrow 		filename->filename_size = name_size;
216151ca58dcSMichael Halcrow 		rc = ecryptfs_encrypt_filename(filename, crypt_stat,
216251ca58dcSMichael Halcrow 					       mount_crypt_stat);
216351ca58dcSMichael Halcrow 		if (rc) {
216451ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encrypt "
216551ca58dcSMichael Halcrow 			       "filename; rc = [%d]\n", __func__, rc);
216651ca58dcSMichael Halcrow 			kfree(filename);
216751ca58dcSMichael Halcrow 			goto out;
216851ca58dcSMichael Halcrow 		}
216951ca58dcSMichael Halcrow 		ecryptfs_encode_for_filename(
217051ca58dcSMichael Halcrow 			NULL, &encoded_name_no_prefix_size,
217151ca58dcSMichael Halcrow 			filename->encrypted_filename,
217251ca58dcSMichael Halcrow 			filename->encrypted_filename_size);
217351ca58dcSMichael Halcrow 		if ((crypt_stat && (crypt_stat->flags
217451ca58dcSMichael Halcrow 				    & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
217551ca58dcSMichael Halcrow 		    || (mount_crypt_stat
217651ca58dcSMichael Halcrow 			&& (mount_crypt_stat->flags
217751ca58dcSMichael Halcrow 			    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)))
217851ca58dcSMichael Halcrow 			(*encoded_name_size) =
217951ca58dcSMichael Halcrow 				(ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
218051ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
218151ca58dcSMichael Halcrow 		else
218251ca58dcSMichael Halcrow 			(*encoded_name_size) =
218351ca58dcSMichael Halcrow 				(ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
218451ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
218551ca58dcSMichael Halcrow 		(*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
218651ca58dcSMichael Halcrow 		if (!(*encoded_name)) {
218751ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
2188a8f12864SMichael Halcrow 			       "to kzalloc [%zd] bytes\n", __func__,
218951ca58dcSMichael Halcrow 			       (*encoded_name_size));
219051ca58dcSMichael Halcrow 			rc = -ENOMEM;
219151ca58dcSMichael Halcrow 			kfree(filename->encrypted_filename);
219251ca58dcSMichael Halcrow 			kfree(filename);
219351ca58dcSMichael Halcrow 			goto out;
219451ca58dcSMichael Halcrow 		}
219551ca58dcSMichael Halcrow 		if ((crypt_stat && (crypt_stat->flags
219651ca58dcSMichael Halcrow 				    & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
219751ca58dcSMichael Halcrow 		    || (mount_crypt_stat
219851ca58dcSMichael Halcrow 			&& (mount_crypt_stat->flags
219951ca58dcSMichael Halcrow 			    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
220051ca58dcSMichael Halcrow 			memcpy((*encoded_name),
220151ca58dcSMichael Halcrow 			       ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
220251ca58dcSMichael Halcrow 			       ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
220351ca58dcSMichael Halcrow 			ecryptfs_encode_for_filename(
220451ca58dcSMichael Halcrow 			    ((*encoded_name)
220551ca58dcSMichael Halcrow 			     + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
220651ca58dcSMichael Halcrow 			    &encoded_name_no_prefix_size,
220751ca58dcSMichael Halcrow 			    filename->encrypted_filename,
220851ca58dcSMichael Halcrow 			    filename->encrypted_filename_size);
220951ca58dcSMichael Halcrow 			(*encoded_name_size) =
221051ca58dcSMichael Halcrow 				(ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
221151ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
221251ca58dcSMichael Halcrow 			(*encoded_name)[(*encoded_name_size)] = '\0';
221351ca58dcSMichael Halcrow 		} else {
2214df6ad33bSTyler Hicks 			rc = -EOPNOTSUPP;
221551ca58dcSMichael Halcrow 		}
221651ca58dcSMichael Halcrow 		if (rc) {
221751ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encode "
221851ca58dcSMichael Halcrow 			       "encrypted filename; rc = [%d]\n", __func__,
221951ca58dcSMichael Halcrow 			       rc);
222051ca58dcSMichael Halcrow 			kfree((*encoded_name));
222151ca58dcSMichael Halcrow 			(*encoded_name) = NULL;
222251ca58dcSMichael Halcrow 			(*encoded_name_size) = 0;
222351ca58dcSMichael Halcrow 		}
222451ca58dcSMichael Halcrow 		kfree(filename->encrypted_filename);
222551ca58dcSMichael Halcrow 		kfree(filename);
222651ca58dcSMichael Halcrow 	} else {
222751ca58dcSMichael Halcrow 		rc = ecryptfs_copy_filename(encoded_name,
222851ca58dcSMichael Halcrow 					    encoded_name_size,
222951ca58dcSMichael Halcrow 					    name, name_size);
223051ca58dcSMichael Halcrow 	}
223151ca58dcSMichael Halcrow out:
223251ca58dcSMichael Halcrow 	return rc;
223351ca58dcSMichael Halcrow }
223451ca58dcSMichael Halcrow 
223551ca58dcSMichael Halcrow /**
223651ca58dcSMichael Halcrow  * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
223751ca58dcSMichael Halcrow  * @plaintext_name: The plaintext name
223851ca58dcSMichael Halcrow  * @plaintext_name_size: The plaintext name size
223951ca58dcSMichael Halcrow  * @ecryptfs_dir_dentry: eCryptfs directory dentry
224051ca58dcSMichael Halcrow  * @name: The filename in cipher text
224151ca58dcSMichael Halcrow  * @name_size: The cipher text name size
224251ca58dcSMichael Halcrow  *
224351ca58dcSMichael Halcrow  * Decrypts and decodes the filename.
224451ca58dcSMichael Halcrow  *
224551ca58dcSMichael Halcrow  * Returns zero on error; non-zero otherwise
224651ca58dcSMichael Halcrow  */
224751ca58dcSMichael Halcrow int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
224851ca58dcSMichael Halcrow 					 size_t *plaintext_name_size,
224951ca58dcSMichael Halcrow 					 struct dentry *ecryptfs_dir_dentry,
225051ca58dcSMichael Halcrow 					 const char *name, size_t name_size)
225151ca58dcSMichael Halcrow {
22522aac0cf8STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
22532aac0cf8STyler Hicks 		&ecryptfs_superblock_to_private(
22542aac0cf8STyler Hicks 			ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
225551ca58dcSMichael Halcrow 	char *decoded_name;
225651ca58dcSMichael Halcrow 	size_t decoded_name_size;
225751ca58dcSMichael Halcrow 	size_t packet_size;
225851ca58dcSMichael Halcrow 	int rc = 0;
225951ca58dcSMichael Halcrow 
22602aac0cf8STyler Hicks 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
22612aac0cf8STyler Hicks 	    && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
22622aac0cf8STyler Hicks 	    && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
226351ca58dcSMichael Halcrow 	    && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
226451ca58dcSMichael Halcrow 			ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
226551ca58dcSMichael Halcrow 		const char *orig_name = name;
226651ca58dcSMichael Halcrow 		size_t orig_name_size = name_size;
226751ca58dcSMichael Halcrow 
226851ca58dcSMichael Halcrow 		name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
226951ca58dcSMichael Halcrow 		name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
227071c11c37SMichael Halcrow 		ecryptfs_decode_from_filename(NULL, &decoded_name_size,
227151ca58dcSMichael Halcrow 					      name, name_size);
227251ca58dcSMichael Halcrow 		decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
227351ca58dcSMichael Halcrow 		if (!decoded_name) {
227451ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
2275df261c52SMichael Halcrow 			       "to kmalloc [%zd] bytes\n", __func__,
227651ca58dcSMichael Halcrow 			       decoded_name_size);
227751ca58dcSMichael Halcrow 			rc = -ENOMEM;
227851ca58dcSMichael Halcrow 			goto out;
227951ca58dcSMichael Halcrow 		}
228071c11c37SMichael Halcrow 		ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
228151ca58dcSMichael Halcrow 					      name, name_size);
228251ca58dcSMichael Halcrow 		rc = ecryptfs_parse_tag_70_packet(plaintext_name,
228351ca58dcSMichael Halcrow 						  plaintext_name_size,
228451ca58dcSMichael Halcrow 						  &packet_size,
228551ca58dcSMichael Halcrow 						  mount_crypt_stat,
228651ca58dcSMichael Halcrow 						  decoded_name,
228751ca58dcSMichael Halcrow 						  decoded_name_size);
228851ca58dcSMichael Halcrow 		if (rc) {
228951ca58dcSMichael Halcrow 			printk(KERN_INFO "%s: Could not parse tag 70 packet "
229051ca58dcSMichael Halcrow 			       "from filename; copying through filename "
229151ca58dcSMichael Halcrow 			       "as-is\n", __func__);
229251ca58dcSMichael Halcrow 			rc = ecryptfs_copy_filename(plaintext_name,
229351ca58dcSMichael Halcrow 						    plaintext_name_size,
229451ca58dcSMichael Halcrow 						    orig_name, orig_name_size);
229551ca58dcSMichael Halcrow 			goto out_free;
229651ca58dcSMichael Halcrow 		}
229751ca58dcSMichael Halcrow 	} else {
229851ca58dcSMichael Halcrow 		rc = ecryptfs_copy_filename(plaintext_name,
229951ca58dcSMichael Halcrow 					    plaintext_name_size,
230051ca58dcSMichael Halcrow 					    name, name_size);
230151ca58dcSMichael Halcrow 		goto out;
230251ca58dcSMichael Halcrow 	}
230351ca58dcSMichael Halcrow out_free:
230451ca58dcSMichael Halcrow 	kfree(decoded_name);
230551ca58dcSMichael Halcrow out:
230651ca58dcSMichael Halcrow 	return rc;
230751ca58dcSMichael Halcrow }
23084a26620dSTyler Hicks 
23094a26620dSTyler Hicks #define ENC_NAME_MAX_BLOCKLEN_8_OR_16	143
23104a26620dSTyler Hicks 
23114a26620dSTyler Hicks int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
23124a26620dSTyler Hicks 			   struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
23134a26620dSTyler Hicks {
23144a26620dSTyler Hicks 	struct blkcipher_desc desc;
23154a26620dSTyler Hicks 	struct mutex *tfm_mutex;
23164a26620dSTyler Hicks 	size_t cipher_blocksize;
23174a26620dSTyler Hicks 	int rc;
23184a26620dSTyler Hicks 
23194a26620dSTyler Hicks 	if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
23204a26620dSTyler Hicks 		(*namelen) = lower_namelen;
23214a26620dSTyler Hicks 		return 0;
23224a26620dSTyler Hicks 	}
23234a26620dSTyler Hicks 
23244a26620dSTyler Hicks 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
23254a26620dSTyler Hicks 			mount_crypt_stat->global_default_fn_cipher_name);
23264a26620dSTyler Hicks 	if (unlikely(rc)) {
23274a26620dSTyler Hicks 		(*namelen) = 0;
23284a26620dSTyler Hicks 		return rc;
23294a26620dSTyler Hicks 	}
23304a26620dSTyler Hicks 
23314a26620dSTyler Hicks 	mutex_lock(tfm_mutex);
23324a26620dSTyler Hicks 	cipher_blocksize = crypto_blkcipher_blocksize(desc.tfm);
23334a26620dSTyler Hicks 	mutex_unlock(tfm_mutex);
23344a26620dSTyler Hicks 
23354a26620dSTyler Hicks 	/* Return an exact amount for the common cases */
23364a26620dSTyler Hicks 	if (lower_namelen == NAME_MAX
23374a26620dSTyler Hicks 	    && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
23384a26620dSTyler Hicks 		(*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
23394a26620dSTyler Hicks 		return 0;
23404a26620dSTyler Hicks 	}
23414a26620dSTyler Hicks 
23424a26620dSTyler Hicks 	/* Return a safe estimate for the uncommon cases */
23434a26620dSTyler Hicks 	(*namelen) = lower_namelen;
23444a26620dSTyler Hicks 	(*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
23454a26620dSTyler Hicks 	/* Since this is the max decoded size, subtract 1 "decoded block" len */
23464a26620dSTyler Hicks 	(*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
23474a26620dSTyler Hicks 	(*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
23484a26620dSTyler Hicks 	(*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
23494a26620dSTyler Hicks 	/* Worst case is that the filename is padded nearly a full block size */
23504a26620dSTyler Hicks 	(*namelen) -= cipher_blocksize - 1;
23514a26620dSTyler Hicks 
23524a26620dSTyler Hicks 	if ((*namelen) < 0)
23534a26620dSTyler Hicks 		(*namelen) = 0;
23544a26620dSTyler Hicks 
23554a26620dSTyler Hicks 	return 0;
23564a26620dSTyler Hicks }
2357