xref: /openbmc/linux/fs/ecryptfs/crypto.c (revision aa06117f19944573cda0c4bee026c916b5256090)
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>
3629335c6aSHarvey Harrison #include <asm/unaligned.h>
37237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
38237fead6SMichael Halcrow 
39237fead6SMichael Halcrow static int
40237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
41237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
42237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
43237fead6SMichael Halcrow 			     unsigned char *iv);
44237fead6SMichael Halcrow static int
45237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
46237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
47237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
48237fead6SMichael Halcrow 			     unsigned char *iv);
49237fead6SMichael Halcrow 
50237fead6SMichael Halcrow /**
51237fead6SMichael Halcrow  * ecryptfs_to_hex
52237fead6SMichael Halcrow  * @dst: Buffer to take hex character representation of contents of
53237fead6SMichael Halcrow  *       src; must be at least of size (src_size * 2)
54237fead6SMichael Halcrow  * @src: Buffer to be converted to a hex string respresentation
55237fead6SMichael Halcrow  * @src_size: number of bytes to convert
56237fead6SMichael Halcrow  */
57237fead6SMichael Halcrow void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
58237fead6SMichael Halcrow {
59237fead6SMichael Halcrow 	int x;
60237fead6SMichael Halcrow 
61237fead6SMichael Halcrow 	for (x = 0; x < src_size; x++)
62237fead6SMichael Halcrow 		sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
63237fead6SMichael Halcrow }
64237fead6SMichael Halcrow 
65237fead6SMichael Halcrow /**
66237fead6SMichael Halcrow  * ecryptfs_from_hex
67237fead6SMichael Halcrow  * @dst: Buffer to take the bytes from src hex; must be at least of
68237fead6SMichael Halcrow  *       size (src_size / 2)
69237fead6SMichael Halcrow  * @src: Buffer to be converted from a hex string respresentation to raw value
70237fead6SMichael Halcrow  * @dst_size: size of dst buffer, or number of hex characters pairs to convert
71237fead6SMichael Halcrow  */
72237fead6SMichael Halcrow void ecryptfs_from_hex(char *dst, char *src, int dst_size)
73237fead6SMichael Halcrow {
74237fead6SMichael Halcrow 	int x;
75237fead6SMichael Halcrow 	char tmp[3] = { 0, };
76237fead6SMichael Halcrow 
77237fead6SMichael Halcrow 	for (x = 0; x < dst_size; x++) {
78237fead6SMichael Halcrow 		tmp[0] = src[x * 2];
79237fead6SMichael Halcrow 		tmp[1] = src[x * 2 + 1];
80237fead6SMichael Halcrow 		dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
81237fead6SMichael Halcrow 	}
82237fead6SMichael Halcrow }
83237fead6SMichael Halcrow 
84237fead6SMichael Halcrow /**
85237fead6SMichael Halcrow  * ecryptfs_calculate_md5 - calculates the md5 of @src
86237fead6SMichael Halcrow  * @dst: Pointer to 16 bytes of allocated memory
87237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
88237fead6SMichael Halcrow  * @src: Data to be md5'd
89237fead6SMichael Halcrow  * @len: Length of @src
90237fead6SMichael Halcrow  *
91237fead6SMichael Halcrow  * Uses the allocated crypto context that crypt_stat references to
92237fead6SMichael Halcrow  * generate the MD5 sum of the contents of src.
93237fead6SMichael Halcrow  */
94237fead6SMichael Halcrow static int ecryptfs_calculate_md5(char *dst,
95237fead6SMichael Halcrow 				  struct ecryptfs_crypt_stat *crypt_stat,
96237fead6SMichael Halcrow 				  char *src, int len)
97237fead6SMichael Halcrow {
98237fead6SMichael Halcrow 	struct scatterlist sg;
99565d9724SMichael Halcrow 	struct hash_desc desc = {
100565d9724SMichael Halcrow 		.tfm = crypt_stat->hash_tfm,
101565d9724SMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
102565d9724SMichael Halcrow 	};
103565d9724SMichael Halcrow 	int rc = 0;
104237fead6SMichael Halcrow 
105565d9724SMichael Halcrow 	mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
106237fead6SMichael Halcrow 	sg_init_one(&sg, (u8 *)src, len);
107565d9724SMichael Halcrow 	if (!desc.tfm) {
108565d9724SMichael Halcrow 		desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
109565d9724SMichael Halcrow 					     CRYPTO_ALG_ASYNC);
110565d9724SMichael Halcrow 		if (IS_ERR(desc.tfm)) {
111565d9724SMichael Halcrow 			rc = PTR_ERR(desc.tfm);
112237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
113565d9724SMichael Halcrow 					"allocate crypto context; rc = [%d]\n",
114565d9724SMichael Halcrow 					rc);
115237fead6SMichael Halcrow 			goto out;
116237fead6SMichael Halcrow 		}
117565d9724SMichael Halcrow 		crypt_stat->hash_tfm = desc.tfm;
118237fead6SMichael Halcrow 	}
1198a29f2b0SMichael Halcrow 	rc = crypto_hash_init(&desc);
1208a29f2b0SMichael Halcrow 	if (rc) {
1218a29f2b0SMichael Halcrow 		printk(KERN_ERR
1228a29f2b0SMichael Halcrow 		       "%s: Error initializing crypto hash; rc = [%d]\n",
12318d1dbf1SHarvey Harrison 		       __func__, rc);
1248a29f2b0SMichael Halcrow 		goto out;
1258a29f2b0SMichael Halcrow 	}
1268a29f2b0SMichael Halcrow 	rc = crypto_hash_update(&desc, &sg, len);
1278a29f2b0SMichael Halcrow 	if (rc) {
1288a29f2b0SMichael Halcrow 		printk(KERN_ERR
1298a29f2b0SMichael Halcrow 		       "%s: Error updating crypto hash; rc = [%d]\n",
13018d1dbf1SHarvey Harrison 		       __func__, rc);
1318a29f2b0SMichael Halcrow 		goto out;
1328a29f2b0SMichael Halcrow 	}
1338a29f2b0SMichael Halcrow 	rc = crypto_hash_final(&desc, dst);
1348a29f2b0SMichael Halcrow 	if (rc) {
1358a29f2b0SMichael Halcrow 		printk(KERN_ERR
1368a29f2b0SMichael Halcrow 		       "%s: Error finalizing crypto hash; rc = [%d]\n",
13718d1dbf1SHarvey Harrison 		       __func__, rc);
1388a29f2b0SMichael Halcrow 		goto out;
1398a29f2b0SMichael Halcrow 	}
140237fead6SMichael Halcrow out:
1418a29f2b0SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
142237fead6SMichael Halcrow 	return rc;
143237fead6SMichael Halcrow }
144237fead6SMichael Halcrow 
145cd9d67dfSMichael Halcrow static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
1468bba066fSMichael Halcrow 						  char *cipher_name,
1478bba066fSMichael Halcrow 						  char *chaining_modifier)
1488bba066fSMichael Halcrow {
1498bba066fSMichael Halcrow 	int cipher_name_len = strlen(cipher_name);
1508bba066fSMichael Halcrow 	int chaining_modifier_len = strlen(chaining_modifier);
1518bba066fSMichael Halcrow 	int algified_name_len;
1528bba066fSMichael Halcrow 	int rc;
1538bba066fSMichael Halcrow 
1548bba066fSMichael Halcrow 	algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
1558bba066fSMichael Halcrow 	(*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
1567bd473fcSMichael Halcrow 	if (!(*algified_name)) {
1578bba066fSMichael Halcrow 		rc = -ENOMEM;
1588bba066fSMichael Halcrow 		goto out;
1598bba066fSMichael Halcrow 	}
1608bba066fSMichael Halcrow 	snprintf((*algified_name), algified_name_len, "%s(%s)",
1618bba066fSMichael Halcrow 		 chaining_modifier, cipher_name);
1628bba066fSMichael Halcrow 	rc = 0;
1638bba066fSMichael Halcrow out:
1648bba066fSMichael Halcrow 	return rc;
1658bba066fSMichael Halcrow }
1668bba066fSMichael Halcrow 
167237fead6SMichael Halcrow /**
168237fead6SMichael Halcrow  * ecryptfs_derive_iv
169237fead6SMichael Halcrow  * @iv: destination for the derived iv vale
170237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
171d6a13c17SMichael Halcrow  * @offset: Offset of the extent whose IV we are to derive
172237fead6SMichael Halcrow  *
173237fead6SMichael Halcrow  * Generate the initialization vector from the given root IV and page
174237fead6SMichael Halcrow  * offset.
175237fead6SMichael Halcrow  *
176237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
177237fead6SMichael Halcrow  */
178a34f60f7SMichael Halcrow int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
179d6a13c17SMichael Halcrow 		       loff_t offset)
180237fead6SMichael Halcrow {
181237fead6SMichael Halcrow 	int rc = 0;
182237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
183237fead6SMichael Halcrow 	char src[ECRYPTFS_MAX_IV_BYTES + 16];
184237fead6SMichael Halcrow 
185237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
186237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "root iv:\n");
187237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
188237fead6SMichael Halcrow 	}
189237fead6SMichael Halcrow 	/* TODO: It is probably secure to just cast the least
190237fead6SMichael Halcrow 	 * significant bits of the root IV into an unsigned long and
191237fead6SMichael Halcrow 	 * add the offset to that rather than go through all this
192237fead6SMichael Halcrow 	 * hashing business. -Halcrow */
193237fead6SMichael Halcrow 	memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
194237fead6SMichael Halcrow 	memset((src + crypt_stat->iv_bytes), 0, 16);
195d6a13c17SMichael Halcrow 	snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
196237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
197237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "source:\n");
198237fead6SMichael Halcrow 		ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
199237fead6SMichael Halcrow 	}
200237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
201237fead6SMichael Halcrow 				    (crypt_stat->iv_bytes + 16));
202237fead6SMichael Halcrow 	if (rc) {
203237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
204237fead6SMichael Halcrow 				"MD5 while generating IV for a page\n");
205237fead6SMichael Halcrow 		goto out;
206237fead6SMichael Halcrow 	}
207237fead6SMichael Halcrow 	memcpy(iv, dst, crypt_stat->iv_bytes);
208237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
209237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
210237fead6SMichael Halcrow 		ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
211237fead6SMichael Halcrow 	}
212237fead6SMichael Halcrow out:
213237fead6SMichael Halcrow 	return rc;
214237fead6SMichael Halcrow }
215237fead6SMichael Halcrow 
216237fead6SMichael Halcrow /**
217237fead6SMichael Halcrow  * ecryptfs_init_crypt_stat
218237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
219237fead6SMichael Halcrow  *
220237fead6SMichael Halcrow  * Initialize the crypt_stat structure.
221237fead6SMichael Halcrow  */
222237fead6SMichael Halcrow void
223237fead6SMichael Halcrow ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
224237fead6SMichael Halcrow {
225237fead6SMichael Halcrow 	memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
226f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&crypt_stat->keysig_list);
227f4aad16aSMichael Halcrow 	mutex_init(&crypt_stat->keysig_list_mutex);
228237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_mutex);
229237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_tfm_mutex);
230565d9724SMichael Halcrow 	mutex_init(&crypt_stat->cs_hash_tfm_mutex);
231e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
232237fead6SMichael Halcrow }
233237fead6SMichael Halcrow 
234237fead6SMichael Halcrow /**
235fcd12835SMichael Halcrow  * ecryptfs_destroy_crypt_stat
236237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
237237fead6SMichael Halcrow  *
238237fead6SMichael Halcrow  * Releases all memory associated with a crypt_stat struct.
239237fead6SMichael Halcrow  */
240fcd12835SMichael Halcrow void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
241237fead6SMichael Halcrow {
242f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
243f4aad16aSMichael Halcrow 
244237fead6SMichael Halcrow 	if (crypt_stat->tfm)
2458bba066fSMichael Halcrow 		crypto_free_blkcipher(crypt_stat->tfm);
246565d9724SMichael Halcrow 	if (crypt_stat->hash_tfm)
247565d9724SMichael Halcrow 		crypto_free_hash(crypt_stat->hash_tfm);
248f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_sig, key_sig_tmp,
249f4aad16aSMichael Halcrow 				 &crypt_stat->keysig_list, crypt_stat_list) {
250f4aad16aSMichael Halcrow 		list_del(&key_sig->crypt_stat_list);
251f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
252f4aad16aSMichael Halcrow 	}
253237fead6SMichael Halcrow 	memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
254237fead6SMichael Halcrow }
255237fead6SMichael Halcrow 
256fcd12835SMichael Halcrow void ecryptfs_destroy_mount_crypt_stat(
257237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
258237fead6SMichael Halcrow {
259f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
260f4aad16aSMichael Halcrow 
261f4aad16aSMichael Halcrow 	if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
262f4aad16aSMichael Halcrow 		return;
263f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
264f4aad16aSMichael Halcrow 	list_for_each_entry_safe(auth_tok, auth_tok_tmp,
265f4aad16aSMichael Halcrow 				 &mount_crypt_stat->global_auth_tok_list,
266f4aad16aSMichael Halcrow 				 mount_crypt_stat_list) {
267f4aad16aSMichael Halcrow 		list_del(&auth_tok->mount_crypt_stat_list);
268f4aad16aSMichael Halcrow 		mount_crypt_stat->num_global_auth_toks--;
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 		if (sg)
305642f1490SJens Axboe 			sg_set_page(&sg[i], pg, 0, offset);
306237fead6SMichael Halcrow 		remainder_of_page = PAGE_CACHE_SIZE - offset;
307237fead6SMichael Halcrow 		if (size >= remainder_of_page) {
308237fead6SMichael Halcrow 			if (sg)
309237fead6SMichael Halcrow 				sg[i].length = remainder_of_page;
310237fead6SMichael Halcrow 			addr += remainder_of_page;
311237fead6SMichael Halcrow 			size -= remainder_of_page;
312237fead6SMichael Halcrow 		} else {
313237fead6SMichael Halcrow 			if (sg)
314237fead6SMichael Halcrow 				sg[i].length = size;
315237fead6SMichael Halcrow 			addr += size;
316237fead6SMichael Halcrow 			size = 0;
317237fead6SMichael Halcrow 		}
318237fead6SMichael Halcrow 		i++;
319237fead6SMichael Halcrow 	}
320237fead6SMichael Halcrow 	if (size > 0)
321237fead6SMichael Halcrow 		return -ENOMEM;
322237fead6SMichael Halcrow 	return i;
323237fead6SMichael Halcrow }
324237fead6SMichael Halcrow 
325237fead6SMichael Halcrow /**
326237fead6SMichael Halcrow  * encrypt_scatterlist
327237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
328237fead6SMichael Halcrow  * @dest_sg: Destination of encrypted data
329237fead6SMichael Halcrow  * @src_sg: Data to be encrypted
330237fead6SMichael Halcrow  * @size: Length of data to be encrypted
331237fead6SMichael Halcrow  * @iv: iv to use during encryption
332237fead6SMichael Halcrow  *
333237fead6SMichael Halcrow  * Returns the number of bytes encrypted; negative value on error
334237fead6SMichael Halcrow  */
335237fead6SMichael Halcrow static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
336237fead6SMichael Halcrow 			       struct scatterlist *dest_sg,
337237fead6SMichael Halcrow 			       struct scatterlist *src_sg, int size,
338237fead6SMichael Halcrow 			       unsigned char *iv)
339237fead6SMichael Halcrow {
3408bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
3418bba066fSMichael Halcrow 		.tfm = crypt_stat->tfm,
3428bba066fSMichael Halcrow 		.info = iv,
3438bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
3448bba066fSMichael Halcrow 	};
345237fead6SMichael Halcrow 	int rc = 0;
346237fead6SMichael Halcrow 
347237fead6SMichael Halcrow 	BUG_ON(!crypt_stat || !crypt_stat->tfm
348e2bd99ecSMichael Halcrow 	       || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
349237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
350237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
351237fead6SMichael Halcrow 				crypt_stat->key_size);
352237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
353237fead6SMichael Halcrow 				  crypt_stat->key_size);
354237fead6SMichael Halcrow 	}
355237fead6SMichael Halcrow 	/* Consider doing this once, when the file is opened */
356237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
3578e3a6f16STrevor Highland 	if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
3588bba066fSMichael Halcrow 		rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
359237fead6SMichael Halcrow 					     crypt_stat->key_size);
3608e3a6f16STrevor Highland 		crypt_stat->flags |= ECRYPTFS_KEY_SET;
3618e3a6f16STrevor Highland 	}
362237fead6SMichael Halcrow 	if (rc) {
363237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
364237fead6SMichael Halcrow 				rc);
365237fead6SMichael Halcrow 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
366237fead6SMichael Halcrow 		rc = -EINVAL;
367237fead6SMichael Halcrow 		goto out;
368237fead6SMichael Halcrow 	}
369237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
3708bba066fSMichael Halcrow 	crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
371237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
372237fead6SMichael Halcrow out:
373237fead6SMichael Halcrow 	return rc;
374237fead6SMichael Halcrow }
375237fead6SMichael Halcrow 
376237fead6SMichael Halcrow /**
3770216f7f7SMichael Halcrow  * ecryptfs_lower_offset_for_extent
378237fead6SMichael Halcrow  *
3790216f7f7SMichael Halcrow  * Convert an eCryptfs page index into a lower byte offset
380237fead6SMichael Halcrow  */
3817896b631SAdrian Bunk static void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
3820216f7f7SMichael Halcrow 					     struct ecryptfs_crypt_stat *crypt_stat)
383237fead6SMichael Halcrow {
384cc11beffSMichael Halcrow 	(*offset) = (crypt_stat->num_header_bytes_at_front
3850216f7f7SMichael Halcrow 		     + (crypt_stat->extent_size * extent_num));
3860216f7f7SMichael Halcrow }
387237fead6SMichael Halcrow 
3880216f7f7SMichael Halcrow /**
3890216f7f7SMichael Halcrow  * ecryptfs_encrypt_extent
3900216f7f7SMichael Halcrow  * @enc_extent_page: Allocated page into which to encrypt the data in
3910216f7f7SMichael Halcrow  *                   @page
3920216f7f7SMichael Halcrow  * @crypt_stat: crypt_stat containing cryptographic context for the
3930216f7f7SMichael Halcrow  *              encryption operation
3940216f7f7SMichael Halcrow  * @page: Page containing plaintext data extent to encrypt
3950216f7f7SMichael Halcrow  * @extent_offset: Page extent offset for use in generating IV
3960216f7f7SMichael Halcrow  *
3970216f7f7SMichael Halcrow  * Encrypts one extent of data.
3980216f7f7SMichael Halcrow  *
3990216f7f7SMichael Halcrow  * Return zero on success; non-zero otherwise
4000216f7f7SMichael Halcrow  */
4010216f7f7SMichael Halcrow static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
4020216f7f7SMichael Halcrow 				   struct ecryptfs_crypt_stat *crypt_stat,
4030216f7f7SMichael Halcrow 				   struct page *page,
4040216f7f7SMichael Halcrow 				   unsigned long extent_offset)
4050216f7f7SMichael Halcrow {
406d6a13c17SMichael Halcrow 	loff_t extent_base;
4070216f7f7SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
4080216f7f7SMichael Halcrow 	int rc;
4090216f7f7SMichael Halcrow 
410d6a13c17SMichael Halcrow 	extent_base = (((loff_t)page->index)
4110216f7f7SMichael Halcrow 		       * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
412237fead6SMichael Halcrow 	rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
4130216f7f7SMichael Halcrow 				(extent_base + extent_offset));
414237fead6SMichael Halcrow 	if (rc) {
415237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to "
416237fead6SMichael Halcrow 				"derive IV for extent [0x%.16x]; "
4170216f7f7SMichael Halcrow 				"rc = [%d]\n", (extent_base + extent_offset),
4180216f7f7SMichael Halcrow 				rc);
419237fead6SMichael Halcrow 		goto out;
420237fead6SMichael Halcrow 	}
421237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
422237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
423237fead6SMichael Halcrow 				"with iv:\n");
424237fead6SMichael Halcrow 		ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
425237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
426237fead6SMichael Halcrow 				"encryption:\n");
427237fead6SMichael Halcrow 		ecryptfs_dump_hex((char *)
4280216f7f7SMichael Halcrow 				  (page_address(page)
4290216f7f7SMichael Halcrow 				   + (extent_offset * crypt_stat->extent_size)),
4300216f7f7SMichael Halcrow 				  8);
431237fead6SMichael Halcrow 	}
4320216f7f7SMichael Halcrow 	rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
4330216f7f7SMichael Halcrow 					  page, (extent_offset
4340216f7f7SMichael Halcrow 						 * crypt_stat->extent_size),
435237fead6SMichael Halcrow 					  crypt_stat->extent_size, extent_iv);
4360216f7f7SMichael Halcrow 	if (rc < 0) {
4370216f7f7SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to encrypt page with "
4380216f7f7SMichael Halcrow 		       "page->index = [%ld], extent_offset = [%ld]; "
43918d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, page->index, extent_offset,
4400216f7f7SMichael Halcrow 		       rc);
4410216f7f7SMichael Halcrow 		goto out;
4420216f7f7SMichael Halcrow 	}
4430216f7f7SMichael Halcrow 	rc = 0;
444237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
4450216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
4460216f7f7SMichael Halcrow 				"rc = [%d]\n", (extent_base + extent_offset),
4470216f7f7SMichael Halcrow 				rc);
448237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
449237fead6SMichael Halcrow 				"encryption:\n");
4500216f7f7SMichael Halcrow 		ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
451237fead6SMichael Halcrow 	}
4520216f7f7SMichael Halcrow out:
4530216f7f7SMichael Halcrow 	return rc;
4540216f7f7SMichael Halcrow }
4550216f7f7SMichael Halcrow 
4560216f7f7SMichael Halcrow /**
4570216f7f7SMichael Halcrow  * ecryptfs_encrypt_page
4580216f7f7SMichael Halcrow  * @page: Page mapped from the eCryptfs inode for the file; contains
4590216f7f7SMichael Halcrow  *        decrypted content that needs to be encrypted (to a temporary
4600216f7f7SMichael Halcrow  *        page; not in place) and written out to the lower file
4610216f7f7SMichael Halcrow  *
4620216f7f7SMichael Halcrow  * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
4630216f7f7SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
4640216f7f7SMichael Halcrow  * if the file was created on a machine with an 8K page size
4650216f7f7SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
4660216f7f7SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
4670216f7f7SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
4680216f7f7SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
4690216f7f7SMichael Halcrow  *
4700216f7f7SMichael Halcrow  * Returns zero on success; negative on error
4710216f7f7SMichael Halcrow  */
4720216f7f7SMichael Halcrow int ecryptfs_encrypt_page(struct page *page)
4730216f7f7SMichael Halcrow {
4740216f7f7SMichael Halcrow 	struct inode *ecryptfs_inode;
4750216f7f7SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
4767fcba054SEric Sandeen 	char *enc_extent_virt;
4777fcba054SEric Sandeen 	struct page *enc_extent_page = NULL;
4780216f7f7SMichael Halcrow 	loff_t extent_offset;
4790216f7f7SMichael Halcrow 	int rc = 0;
4800216f7f7SMichael Halcrow 
4810216f7f7SMichael Halcrow 	ecryptfs_inode = page->mapping->host;
4820216f7f7SMichael Halcrow 	crypt_stat =
4830216f7f7SMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
48413a791b4STyler Hicks 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
4857fcba054SEric Sandeen 	enc_extent_page = alloc_page(GFP_USER);
4867fcba054SEric Sandeen 	if (!enc_extent_page) {
4870216f7f7SMichael Halcrow 		rc = -ENOMEM;
4880216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error allocating memory for "
4890216f7f7SMichael Halcrow 				"encrypted extent\n");
4900216f7f7SMichael Halcrow 		goto out;
4910216f7f7SMichael Halcrow 	}
4927fcba054SEric Sandeen 	enc_extent_virt = kmap(enc_extent_page);
4930216f7f7SMichael Halcrow 	for (extent_offset = 0;
4940216f7f7SMichael Halcrow 	     extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
4950216f7f7SMichael Halcrow 	     extent_offset++) {
4960216f7f7SMichael Halcrow 		loff_t offset;
4970216f7f7SMichael Halcrow 
4980216f7f7SMichael Halcrow 		rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
4990216f7f7SMichael Halcrow 					     extent_offset);
5000216f7f7SMichael Halcrow 		if (rc) {
5010216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting extent; "
50218d1dbf1SHarvey Harrison 			       "rc = [%d]\n", __func__, rc);
5030216f7f7SMichael Halcrow 			goto out;
5040216f7f7SMichael Halcrow 		}
5050216f7f7SMichael Halcrow 		ecryptfs_lower_offset_for_extent(
506d6a13c17SMichael Halcrow 			&offset, ((((loff_t)page->index)
507d6a13c17SMichael Halcrow 				   * (PAGE_CACHE_SIZE
5080216f7f7SMichael Halcrow 				      / crypt_stat->extent_size))
5090216f7f7SMichael Halcrow 				  + extent_offset), crypt_stat);
5100216f7f7SMichael Halcrow 		rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
5110216f7f7SMichael Halcrow 					  offset, crypt_stat->extent_size);
5120216f7f7SMichael Halcrow 		if (rc) {
5130216f7f7SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting "
5140216f7f7SMichael Halcrow 					"to write lower page; rc = [%d]"
5150216f7f7SMichael Halcrow 					"\n", rc);
5160216f7f7SMichael Halcrow 			goto out;
5170216f7f7SMichael Halcrow 		}
518237fead6SMichael Halcrow 	}
5190216f7f7SMichael Halcrow out:
5207fcba054SEric Sandeen 	if (enc_extent_page) {
5217fcba054SEric Sandeen 		kunmap(enc_extent_page);
5227fcba054SEric Sandeen 		__free_page(enc_extent_page);
5237fcba054SEric Sandeen 	}
5240216f7f7SMichael Halcrow 	return rc;
5250216f7f7SMichael Halcrow }
5260216f7f7SMichael Halcrow 
5270216f7f7SMichael Halcrow static int ecryptfs_decrypt_extent(struct page *page,
5280216f7f7SMichael Halcrow 				   struct ecryptfs_crypt_stat *crypt_stat,
5290216f7f7SMichael Halcrow 				   struct page *enc_extent_page,
5300216f7f7SMichael Halcrow 				   unsigned long extent_offset)
5310216f7f7SMichael Halcrow {
532d6a13c17SMichael Halcrow 	loff_t extent_base;
5330216f7f7SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
5340216f7f7SMichael Halcrow 	int rc;
5350216f7f7SMichael Halcrow 
536d6a13c17SMichael Halcrow 	extent_base = (((loff_t)page->index)
5370216f7f7SMichael Halcrow 		       * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
5380216f7f7SMichael Halcrow 	rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
5390216f7f7SMichael Halcrow 				(extent_base + extent_offset));
540237fead6SMichael Halcrow 	if (rc) {
5410216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to "
5420216f7f7SMichael Halcrow 				"derive IV for extent [0x%.16x]; "
5430216f7f7SMichael Halcrow 				"rc = [%d]\n", (extent_base + extent_offset),
5440216f7f7SMichael Halcrow 				rc);
545237fead6SMichael Halcrow 		goto out;
546237fead6SMichael Halcrow 	}
5470216f7f7SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
5480216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
5490216f7f7SMichael Halcrow 				"with iv:\n");
5500216f7f7SMichael Halcrow 		ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
5510216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
5520216f7f7SMichael Halcrow 				"decryption:\n");
5530216f7f7SMichael Halcrow 		ecryptfs_dump_hex((char *)
5540216f7f7SMichael Halcrow 				  (page_address(enc_extent_page)
5550216f7f7SMichael Halcrow 				   + (extent_offset * crypt_stat->extent_size)),
5560216f7f7SMichael Halcrow 				  8);
5570216f7f7SMichael Halcrow 	}
5580216f7f7SMichael Halcrow 	rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
5590216f7f7SMichael Halcrow 					  (extent_offset
5600216f7f7SMichael Halcrow 					   * crypt_stat->extent_size),
5610216f7f7SMichael Halcrow 					  enc_extent_page, 0,
5620216f7f7SMichael Halcrow 					  crypt_stat->extent_size, extent_iv);
5630216f7f7SMichael Halcrow 	if (rc < 0) {
5640216f7f7SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to decrypt to page with "
5650216f7f7SMichael Halcrow 		       "page->index = [%ld], extent_offset = [%ld]; "
56618d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, page->index, extent_offset,
5670216f7f7SMichael Halcrow 		       rc);
5680216f7f7SMichael Halcrow 		goto out;
5690216f7f7SMichael Halcrow 	}
5700216f7f7SMichael Halcrow 	rc = 0;
5710216f7f7SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
5720216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
5730216f7f7SMichael Halcrow 				"rc = [%d]\n", (extent_base + extent_offset),
5740216f7f7SMichael Halcrow 				rc);
5750216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
5760216f7f7SMichael Halcrow 				"decryption:\n");
5770216f7f7SMichael Halcrow 		ecryptfs_dump_hex((char *)(page_address(page)
5780216f7f7SMichael Halcrow 					   + (extent_offset
5790216f7f7SMichael Halcrow 					      * crypt_stat->extent_size)), 8);
5800216f7f7SMichael Halcrow 	}
581237fead6SMichael Halcrow out:
582237fead6SMichael Halcrow 	return rc;
583237fead6SMichael Halcrow }
584237fead6SMichael Halcrow 
585237fead6SMichael Halcrow /**
586237fead6SMichael Halcrow  * ecryptfs_decrypt_page
5870216f7f7SMichael Halcrow  * @page: Page mapped from the eCryptfs inode for the file; data read
5880216f7f7SMichael Halcrow  *        and decrypted from the lower file will be written into this
5890216f7f7SMichael Halcrow  *        page
590237fead6SMichael Halcrow  *
591237fead6SMichael Halcrow  * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
592237fead6SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
593237fead6SMichael Halcrow  * if the file was created on a machine with an 8K page size
594237fead6SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
595237fead6SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
596237fead6SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
597237fead6SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
598237fead6SMichael Halcrow  *
599237fead6SMichael Halcrow  * Returns zero on success; negative on error
600237fead6SMichael Halcrow  */
6010216f7f7SMichael Halcrow int ecryptfs_decrypt_page(struct page *page)
602237fead6SMichael Halcrow {
6030216f7f7SMichael Halcrow 	struct inode *ecryptfs_inode;
604237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
6057fcba054SEric Sandeen 	char *enc_extent_virt;
6067fcba054SEric Sandeen 	struct page *enc_extent_page = NULL;
6070216f7f7SMichael Halcrow 	unsigned long extent_offset;
608237fead6SMichael Halcrow 	int rc = 0;
609237fead6SMichael Halcrow 
6100216f7f7SMichael Halcrow 	ecryptfs_inode = page->mapping->host;
6110216f7f7SMichael Halcrow 	crypt_stat =
6120216f7f7SMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
61313a791b4STyler Hicks 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
6147fcba054SEric Sandeen 	enc_extent_page = alloc_page(GFP_USER);
6157fcba054SEric Sandeen 	if (!enc_extent_page) {
616237fead6SMichael Halcrow 		rc = -ENOMEM;
6170216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error allocating memory for "
6180216f7f7SMichael Halcrow 				"encrypted extent\n");
61916a72c45SMichael Halcrow 		goto out;
620237fead6SMichael Halcrow 	}
6217fcba054SEric Sandeen 	enc_extent_virt = kmap(enc_extent_page);
6220216f7f7SMichael Halcrow 	for (extent_offset = 0;
6230216f7f7SMichael Halcrow 	     extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
6240216f7f7SMichael Halcrow 	     extent_offset++) {
6250216f7f7SMichael Halcrow 		loff_t offset;
6260216f7f7SMichael Halcrow 
6270216f7f7SMichael Halcrow 		ecryptfs_lower_offset_for_extent(
6280216f7f7SMichael Halcrow 			&offset, ((page->index * (PAGE_CACHE_SIZE
6290216f7f7SMichael Halcrow 						  / crypt_stat->extent_size))
6300216f7f7SMichael Halcrow 				  + extent_offset), crypt_stat);
6310216f7f7SMichael Halcrow 		rc = ecryptfs_read_lower(enc_extent_virt, offset,
632237fead6SMichael Halcrow 					 crypt_stat->extent_size,
6330216f7f7SMichael Halcrow 					 ecryptfs_inode);
6340216f7f7SMichael Halcrow 		if (rc) {
6350216f7f7SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting "
6360216f7f7SMichael Halcrow 					"to read lower page; rc = [%d]"
6370216f7f7SMichael Halcrow 					"\n", rc);
63816a72c45SMichael Halcrow 			goto out;
639237fead6SMichael Halcrow 		}
6400216f7f7SMichael Halcrow 		rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
6410216f7f7SMichael Halcrow 					     extent_offset);
6420216f7f7SMichael Halcrow 		if (rc) {
6430216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting extent; "
64418d1dbf1SHarvey Harrison 			       "rc = [%d]\n", __func__, rc);
64516a72c45SMichael Halcrow 			goto out;
646237fead6SMichael Halcrow 		}
647237fead6SMichael Halcrow 	}
648237fead6SMichael Halcrow out:
6497fcba054SEric Sandeen 	if (enc_extent_page) {
6507fcba054SEric Sandeen 		kunmap(enc_extent_page);
6517fcba054SEric Sandeen 		__free_page(enc_extent_page);
6527fcba054SEric Sandeen 	}
653237fead6SMichael Halcrow 	return rc;
654237fead6SMichael Halcrow }
655237fead6SMichael Halcrow 
656237fead6SMichael Halcrow /**
657237fead6SMichael Halcrow  * decrypt_scatterlist
65822e78fafSMichael Halcrow  * @crypt_stat: Cryptographic context
65922e78fafSMichael Halcrow  * @dest_sg: The destination scatterlist to decrypt into
66022e78fafSMichael Halcrow  * @src_sg: The source scatterlist to decrypt from
66122e78fafSMichael Halcrow  * @size: The number of bytes to decrypt
66222e78fafSMichael Halcrow  * @iv: The initialization vector to use for the decryption
663237fead6SMichael Halcrow  *
664237fead6SMichael Halcrow  * Returns the number of bytes decrypted; negative value on error
665237fead6SMichael Halcrow  */
666237fead6SMichael Halcrow static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
667237fead6SMichael Halcrow 			       struct scatterlist *dest_sg,
668237fead6SMichael Halcrow 			       struct scatterlist *src_sg, int size,
669237fead6SMichael Halcrow 			       unsigned char *iv)
670237fead6SMichael Halcrow {
6718bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
6728bba066fSMichael Halcrow 		.tfm = crypt_stat->tfm,
6738bba066fSMichael Halcrow 		.info = iv,
6748bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
6758bba066fSMichael Halcrow 	};
676237fead6SMichael Halcrow 	int rc = 0;
677237fead6SMichael Halcrow 
678237fead6SMichael Halcrow 	/* Consider doing this once, when the file is opened */
679237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
6808bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
681237fead6SMichael Halcrow 				     crypt_stat->key_size);
682237fead6SMichael Halcrow 	if (rc) {
683237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
684237fead6SMichael Halcrow 				rc);
685237fead6SMichael Halcrow 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
686237fead6SMichael Halcrow 		rc = -EINVAL;
687237fead6SMichael Halcrow 		goto out;
688237fead6SMichael Halcrow 	}
689237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
6908bba066fSMichael Halcrow 	rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
691237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
692237fead6SMichael Halcrow 	if (rc) {
693237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
694237fead6SMichael Halcrow 				rc);
695237fead6SMichael Halcrow 		goto out;
696237fead6SMichael Halcrow 	}
697237fead6SMichael Halcrow 	rc = size;
698237fead6SMichael Halcrow out:
699237fead6SMichael Halcrow 	return rc;
700237fead6SMichael Halcrow }
701237fead6SMichael Halcrow 
702237fead6SMichael Halcrow /**
703237fead6SMichael Halcrow  * ecryptfs_encrypt_page_offset
70422e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
70522e78fafSMichael Halcrow  * @dst_page: The page to encrypt into
70622e78fafSMichael Halcrow  * @dst_offset: The offset in the page to encrypt into
70722e78fafSMichael Halcrow  * @src_page: The page to encrypt from
70822e78fafSMichael Halcrow  * @src_offset: The offset in the page to encrypt from
70922e78fafSMichael Halcrow  * @size: The number of bytes to encrypt
71022e78fafSMichael Halcrow  * @iv: The initialization vector to use for the encryption
711237fead6SMichael Halcrow  *
712237fead6SMichael Halcrow  * Returns the number of bytes encrypted
713237fead6SMichael Halcrow  */
714237fead6SMichael Halcrow static int
715237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
716237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
717237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
718237fead6SMichael Halcrow 			     unsigned char *iv)
719237fead6SMichael Halcrow {
720237fead6SMichael Halcrow 	struct scatterlist src_sg, dst_sg;
721237fead6SMichael Halcrow 
72260c74f81SJens Axboe 	sg_init_table(&src_sg, 1);
72360c74f81SJens Axboe 	sg_init_table(&dst_sg, 1);
72460c74f81SJens Axboe 
725642f1490SJens Axboe 	sg_set_page(&src_sg, src_page, size, src_offset);
726642f1490SJens Axboe 	sg_set_page(&dst_sg, dst_page, size, dst_offset);
727237fead6SMichael Halcrow 	return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
728237fead6SMichael Halcrow }
729237fead6SMichael Halcrow 
730237fead6SMichael Halcrow /**
731237fead6SMichael Halcrow  * ecryptfs_decrypt_page_offset
73222e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
73322e78fafSMichael Halcrow  * @dst_page: The page to decrypt into
73422e78fafSMichael Halcrow  * @dst_offset: The offset in the page to decrypt into
73522e78fafSMichael Halcrow  * @src_page: The page to decrypt from
73622e78fafSMichael Halcrow  * @src_offset: The offset in the page to decrypt from
73722e78fafSMichael Halcrow  * @size: The number of bytes to decrypt
73822e78fafSMichael Halcrow  * @iv: The initialization vector to use for the decryption
739237fead6SMichael Halcrow  *
740237fead6SMichael Halcrow  * Returns the number of bytes decrypted
741237fead6SMichael Halcrow  */
742237fead6SMichael Halcrow static int
743237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
744237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
745237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
746237fead6SMichael Halcrow 			     unsigned char *iv)
747237fead6SMichael Halcrow {
748237fead6SMichael Halcrow 	struct scatterlist src_sg, dst_sg;
749237fead6SMichael Halcrow 
75060c74f81SJens Axboe 	sg_init_table(&src_sg, 1);
751642f1490SJens Axboe 	sg_set_page(&src_sg, src_page, size, src_offset);
75260c74f81SJens Axboe 
753642f1490SJens Axboe 	sg_init_table(&dst_sg, 1);
754642f1490SJens Axboe 	sg_set_page(&dst_sg, dst_page, size, dst_offset);
755642f1490SJens Axboe 
756237fead6SMichael Halcrow 	return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
757237fead6SMichael Halcrow }
758237fead6SMichael Halcrow 
759237fead6SMichael Halcrow #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
760237fead6SMichael Halcrow 
761237fead6SMichael Halcrow /**
762237fead6SMichael Halcrow  * ecryptfs_init_crypt_ctx
763237fead6SMichael Halcrow  * @crypt_stat: Uninitilized crypt stats structure
764237fead6SMichael Halcrow  *
765237fead6SMichael Halcrow  * Initialize the crypto context.
766237fead6SMichael Halcrow  *
767237fead6SMichael Halcrow  * TODO: Performance: Keep a cache of initialized cipher contexts;
768237fead6SMichael Halcrow  * only init if needed
769237fead6SMichael Halcrow  */
770237fead6SMichael Halcrow int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
771237fead6SMichael Halcrow {
7728bba066fSMichael Halcrow 	char *full_alg_name;
773237fead6SMichael Halcrow 	int rc = -EINVAL;
774237fead6SMichael Halcrow 
775237fead6SMichael Halcrow 	if (!crypt_stat->cipher) {
776237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "No cipher specified\n");
777237fead6SMichael Halcrow 		goto out;
778237fead6SMichael Halcrow 	}
779237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG,
780237fead6SMichael Halcrow 			"Initializing cipher [%s]; strlen = [%d]; "
781237fead6SMichael Halcrow 			"key_size_bits = [%d]\n",
782237fead6SMichael Halcrow 			crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
783237fead6SMichael Halcrow 			crypt_stat->key_size << 3);
784237fead6SMichael Halcrow 	if (crypt_stat->tfm) {
785237fead6SMichael Halcrow 		rc = 0;
786237fead6SMichael Halcrow 		goto out;
787237fead6SMichael Halcrow 	}
788237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
7898bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
7908bba066fSMichael Halcrow 						    crypt_stat->cipher, "cbc");
7918bba066fSMichael Halcrow 	if (rc)
792c8161f64SEric Sandeen 		goto out_unlock;
7938bba066fSMichael Halcrow 	crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
7948bba066fSMichael Halcrow 						 CRYPTO_ALG_ASYNC);
7958bba066fSMichael Halcrow 	kfree(full_alg_name);
796de88777eSAkinobu Mita 	if (IS_ERR(crypt_stat->tfm)) {
797de88777eSAkinobu Mita 		rc = PTR_ERR(crypt_stat->tfm);
798237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
799237fead6SMichael Halcrow 				"Error initializing cipher [%s]\n",
800237fead6SMichael Halcrow 				crypt_stat->cipher);
801c8161f64SEric Sandeen 		goto out_unlock;
802237fead6SMichael Halcrow 	}
803f1ddcaf3SHerbert Xu 	crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
804237fead6SMichael Halcrow 	rc = 0;
805c8161f64SEric Sandeen out_unlock:
806c8161f64SEric Sandeen 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
807237fead6SMichael Halcrow out:
808237fead6SMichael Halcrow 	return rc;
809237fead6SMichael Halcrow }
810237fead6SMichael Halcrow 
811237fead6SMichael Halcrow static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
812237fead6SMichael Halcrow {
813237fead6SMichael Halcrow 	int extent_size_tmp;
814237fead6SMichael Halcrow 
815237fead6SMichael Halcrow 	crypt_stat->extent_mask = 0xFFFFFFFF;
816237fead6SMichael Halcrow 	crypt_stat->extent_shift = 0;
817237fead6SMichael Halcrow 	if (crypt_stat->extent_size == 0)
818237fead6SMichael Halcrow 		return;
819237fead6SMichael Halcrow 	extent_size_tmp = crypt_stat->extent_size;
820237fead6SMichael Halcrow 	while ((extent_size_tmp & 0x01) == 0) {
821237fead6SMichael Halcrow 		extent_size_tmp >>= 1;
822237fead6SMichael Halcrow 		crypt_stat->extent_mask <<= 1;
823237fead6SMichael Halcrow 		crypt_stat->extent_shift++;
824237fead6SMichael Halcrow 	}
825237fead6SMichael Halcrow }
826237fead6SMichael Halcrow 
827237fead6SMichael Halcrow void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
828237fead6SMichael Halcrow {
829237fead6SMichael Halcrow 	/* Default values; may be overwritten as we are parsing the
830237fead6SMichael Halcrow 	 * packets. */
831237fead6SMichael Halcrow 	crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
832237fead6SMichael Halcrow 	set_extent_mask_and_shift(crypt_stat);
833237fead6SMichael Halcrow 	crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
834dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
835cc11beffSMichael Halcrow 		crypt_stat->num_header_bytes_at_front = 0;
83645eaab79SMichael Halcrow 	else {
83745eaab79SMichael Halcrow 		if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
838cc11beffSMichael Halcrow 			crypt_stat->num_header_bytes_at_front =
839cc11beffSMichael Halcrow 				ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
840dd2a3b7aSMichael Halcrow 		else
841cc11beffSMichael Halcrow 			crypt_stat->num_header_bytes_at_front =	PAGE_CACHE_SIZE;
84245eaab79SMichael Halcrow 	}
843237fead6SMichael Halcrow }
844237fead6SMichael Halcrow 
845237fead6SMichael Halcrow /**
846237fead6SMichael Halcrow  * ecryptfs_compute_root_iv
847237fead6SMichael Halcrow  * @crypt_stats
848237fead6SMichael Halcrow  *
849237fead6SMichael Halcrow  * On error, sets the root IV to all 0's.
850237fead6SMichael Halcrow  */
851237fead6SMichael Halcrow int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
852237fead6SMichael Halcrow {
853237fead6SMichael Halcrow 	int rc = 0;
854237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
855237fead6SMichael Halcrow 
856237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
857237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes <= 0);
858e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
859237fead6SMichael Halcrow 		rc = -EINVAL;
860237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Session key not valid; "
861237fead6SMichael Halcrow 				"cannot generate root IV\n");
862237fead6SMichael Halcrow 		goto out;
863237fead6SMichael Halcrow 	}
864237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
865237fead6SMichael Halcrow 				    crypt_stat->key_size);
866237fead6SMichael Halcrow 	if (rc) {
867237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
868237fead6SMichael Halcrow 				"MD5 while generating root IV\n");
869237fead6SMichael Halcrow 		goto out;
870237fead6SMichael Halcrow 	}
871237fead6SMichael Halcrow 	memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
872237fead6SMichael Halcrow out:
873237fead6SMichael Halcrow 	if (rc) {
874237fead6SMichael Halcrow 		memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
875e2bd99ecSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
876237fead6SMichael Halcrow 	}
877237fead6SMichael Halcrow 	return rc;
878237fead6SMichael Halcrow }
879237fead6SMichael Halcrow 
880237fead6SMichael Halcrow static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
881237fead6SMichael Halcrow {
882237fead6SMichael Halcrow 	get_random_bytes(crypt_stat->key, crypt_stat->key_size);
883e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
884237fead6SMichael Halcrow 	ecryptfs_compute_root_iv(crypt_stat);
885237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
886237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
887237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
888237fead6SMichael Halcrow 				  crypt_stat->key_size);
889237fead6SMichael Halcrow 	}
890237fead6SMichael Halcrow }
891237fead6SMichael Halcrow 
892237fead6SMichael Halcrow /**
89317398957SMichael Halcrow  * ecryptfs_copy_mount_wide_flags_to_inode_flags
89422e78fafSMichael Halcrow  * @crypt_stat: The inode's cryptographic context
89522e78fafSMichael Halcrow  * @mount_crypt_stat: The mount point's cryptographic context
89617398957SMichael Halcrow  *
89717398957SMichael Halcrow  * This function propagates the mount-wide flags to individual inode
89817398957SMichael Halcrow  * flags.
89917398957SMichael Halcrow  */
90017398957SMichael Halcrow static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
90117398957SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
90217398957SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
90317398957SMichael Halcrow {
90417398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
90517398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
90617398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
90717398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
908addd65adSMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
909addd65adSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
910addd65adSMichael Halcrow 		if (mount_crypt_stat->flags
911addd65adSMichael Halcrow 		    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
912addd65adSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
913addd65adSMichael Halcrow 		else if (mount_crypt_stat->flags
914addd65adSMichael Halcrow 			 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
915addd65adSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
916addd65adSMichael Halcrow 	}
91717398957SMichael Halcrow }
91817398957SMichael Halcrow 
919f4aad16aSMichael Halcrow static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
920f4aad16aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
921f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
922f4aad16aSMichael Halcrow {
923f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
924f4aad16aSMichael Halcrow 	int rc = 0;
925f4aad16aSMichael Halcrow 
926*aa06117fSRoland Dreier 	mutex_lock(&crypt_stat->keysig_list_mutex);
927f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
928*aa06117fSRoland Dreier 
929f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
930f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
931f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
93284814d64STyler Hicks 		if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
93384814d64STyler Hicks 			continue;
934f4aad16aSMichael Halcrow 		rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
935f4aad16aSMichael Halcrow 		if (rc) {
936f4aad16aSMichael Halcrow 			printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
937f4aad16aSMichael Halcrow 			goto out;
938f4aad16aSMichael Halcrow 		}
939f4aad16aSMichael Halcrow 	}
940*aa06117fSRoland Dreier 
941f4aad16aSMichael Halcrow out:
942*aa06117fSRoland Dreier 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
943*aa06117fSRoland Dreier 	mutex_unlock(&crypt_stat->keysig_list_mutex);
944f4aad16aSMichael Halcrow 	return rc;
945f4aad16aSMichael Halcrow }
946f4aad16aSMichael Halcrow 
94717398957SMichael Halcrow /**
948237fead6SMichael Halcrow  * ecryptfs_set_default_crypt_stat_vals
94922e78fafSMichael Halcrow  * @crypt_stat: The inode's cryptographic context
95022e78fafSMichael Halcrow  * @mount_crypt_stat: The mount point's cryptographic context
951237fead6SMichael Halcrow  *
952237fead6SMichael Halcrow  * Default values in the event that policy does not override them.
953237fead6SMichael Halcrow  */
954237fead6SMichael Halcrow static void ecryptfs_set_default_crypt_stat_vals(
955237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
956237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
957237fead6SMichael Halcrow {
95817398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
95917398957SMichael Halcrow 						      mount_crypt_stat);
960237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
961237fead6SMichael Halcrow 	strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
962237fead6SMichael Halcrow 	crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
963e2bd99ecSMichael Halcrow 	crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
964237fead6SMichael Halcrow 	crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
965237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = mount_crypt_stat;
966237fead6SMichael Halcrow }
967237fead6SMichael Halcrow 
968237fead6SMichael Halcrow /**
969237fead6SMichael Halcrow  * ecryptfs_new_file_context
97022e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
971237fead6SMichael Halcrow  *
972237fead6SMichael Halcrow  * If the crypto context for the file has not yet been established,
973237fead6SMichael Halcrow  * this is where we do that.  Establishing a new crypto context
974237fead6SMichael Halcrow  * involves the following decisions:
975237fead6SMichael Halcrow  *  - What cipher to use?
976237fead6SMichael Halcrow  *  - What set of authentication tokens to use?
977237fead6SMichael Halcrow  * Here we just worry about getting enough information into the
978237fead6SMichael Halcrow  * authentication tokens so that we know that they are available.
979237fead6SMichael Halcrow  * We associate the available authentication tokens with the new file
980237fead6SMichael Halcrow  * via the set of signatures in the crypt_stat struct.  Later, when
981237fead6SMichael Halcrow  * the headers are actually written out, we may again defer to
982237fead6SMichael Halcrow  * userspace to perform the encryption of the session key; for the
983237fead6SMichael Halcrow  * foreseeable future, this will be the case with public key packets.
984237fead6SMichael Halcrow  *
985237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
986237fead6SMichael Halcrow  */
987237fead6SMichael Halcrow int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
988237fead6SMichael Halcrow {
989237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
990237fead6SMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
991237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
992237fead6SMichael Halcrow 	    &ecryptfs_superblock_to_private(
993237fead6SMichael Halcrow 		    ecryptfs_dentry->d_sb)->mount_crypt_stat;
994237fead6SMichael Halcrow 	int cipher_name_len;
995f4aad16aSMichael Halcrow 	int rc = 0;
996237fead6SMichael Halcrow 
997237fead6SMichael Halcrow 	ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
998af655dc6SMichael Halcrow 	crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
99917398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
100017398957SMichael Halcrow 						      mount_crypt_stat);
1001f4aad16aSMichael Halcrow 	rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
1002f4aad16aSMichael Halcrow 							 mount_crypt_stat);
1003f4aad16aSMichael Halcrow 	if (rc) {
1004f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
1005f4aad16aSMichael Halcrow 		       "to the inode key sigs; rc = [%d]\n", rc);
1006f4aad16aSMichael Halcrow 		goto out;
1007f4aad16aSMichael Halcrow 	}
1008237fead6SMichael Halcrow 	cipher_name_len =
1009237fead6SMichael Halcrow 		strlen(mount_crypt_stat->global_default_cipher_name);
1010237fead6SMichael Halcrow 	memcpy(crypt_stat->cipher,
1011237fead6SMichael Halcrow 	       mount_crypt_stat->global_default_cipher_name,
1012237fead6SMichael Halcrow 	       cipher_name_len);
1013237fead6SMichael Halcrow 	crypt_stat->cipher[cipher_name_len] = '\0';
1014237fead6SMichael Halcrow 	crypt_stat->key_size =
1015237fead6SMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size;
1016237fead6SMichael Halcrow 	ecryptfs_generate_new_key(crypt_stat);
1017237fead6SMichael Halcrow 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
1018237fead6SMichael Halcrow 	if (rc)
1019237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1020237fead6SMichael Halcrow 				"context for cipher [%s]: rc = [%d]\n",
1021237fead6SMichael Halcrow 				crypt_stat->cipher, rc);
1022f4aad16aSMichael Halcrow out:
1023237fead6SMichael Halcrow 	return rc;
1024237fead6SMichael Halcrow }
1025237fead6SMichael Halcrow 
1026237fead6SMichael Halcrow /**
1027237fead6SMichael Halcrow  * contains_ecryptfs_marker - check for the ecryptfs marker
1028237fead6SMichael Halcrow  * @data: The data block in which to check
1029237fead6SMichael Halcrow  *
1030237fead6SMichael Halcrow  * Returns one if marker found; zero if not found
1031237fead6SMichael Halcrow  */
1032dd2a3b7aSMichael Halcrow static int contains_ecryptfs_marker(char *data)
1033237fead6SMichael Halcrow {
1034237fead6SMichael Halcrow 	u32 m_1, m_2;
1035237fead6SMichael Halcrow 
103629335c6aSHarvey Harrison 	m_1 = get_unaligned_be32(data);
103729335c6aSHarvey Harrison 	m_2 = get_unaligned_be32(data + 4);
1038237fead6SMichael Halcrow 	if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1039237fead6SMichael Halcrow 		return 1;
1040237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1041237fead6SMichael Halcrow 			"MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1042237fead6SMichael Halcrow 			MAGIC_ECRYPTFS_MARKER);
1043237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1044237fead6SMichael Halcrow 			"[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1045237fead6SMichael Halcrow 	return 0;
1046237fead6SMichael Halcrow }
1047237fead6SMichael Halcrow 
1048237fead6SMichael Halcrow struct ecryptfs_flag_map_elem {
1049237fead6SMichael Halcrow 	u32 file_flag;
1050237fead6SMichael Halcrow 	u32 local_flag;
1051237fead6SMichael Halcrow };
1052237fead6SMichael Halcrow 
1053237fead6SMichael Halcrow /* Add support for additional flags by adding elements here. */
1054237fead6SMichael Halcrow static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1055237fead6SMichael Halcrow 	{0x00000001, ECRYPTFS_ENABLE_HMAC},
1056dd2a3b7aSMichael Halcrow 	{0x00000002, ECRYPTFS_ENCRYPTED},
1057addd65adSMichael Halcrow 	{0x00000004, ECRYPTFS_METADATA_IN_XATTR},
1058addd65adSMichael Halcrow 	{0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
1059237fead6SMichael Halcrow };
1060237fead6SMichael Halcrow 
1061237fead6SMichael Halcrow /**
1062237fead6SMichael Halcrow  * ecryptfs_process_flags
106322e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1064237fead6SMichael Halcrow  * @page_virt: Source data to be parsed
1065237fead6SMichael Halcrow  * @bytes_read: Updated with the number of bytes read
1066237fead6SMichael Halcrow  *
1067237fead6SMichael Halcrow  * Returns zero on success; non-zero if the flag set is invalid
1068237fead6SMichael Halcrow  */
1069237fead6SMichael Halcrow static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1070237fead6SMichael Halcrow 				  char *page_virt, int *bytes_read)
1071237fead6SMichael Halcrow {
1072237fead6SMichael Halcrow 	int rc = 0;
1073237fead6SMichael Halcrow 	int i;
1074237fead6SMichael Halcrow 	u32 flags;
1075237fead6SMichael Halcrow 
107629335c6aSHarvey Harrison 	flags = get_unaligned_be32(page_virt);
1077237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1078237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
1079237fead6SMichael Halcrow 		if (flags & ecryptfs_flag_map[i].file_flag) {
1080e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
1081237fead6SMichael Halcrow 		} else
1082e2bd99ecSMichael Halcrow 			crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
1083237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
1084237fead6SMichael Halcrow 	crypt_stat->file_version = ((flags >> 24) & 0xFF);
1085237fead6SMichael Halcrow 	(*bytes_read) = 4;
1086237fead6SMichael Halcrow 	return rc;
1087237fead6SMichael Halcrow }
1088237fead6SMichael Halcrow 
1089237fead6SMichael Halcrow /**
1090237fead6SMichael Halcrow  * write_ecryptfs_marker
1091237fead6SMichael Halcrow  * @page_virt: The pointer to in a page to begin writing the marker
1092237fead6SMichael Halcrow  * @written: Number of bytes written
1093237fead6SMichael Halcrow  *
1094237fead6SMichael Halcrow  * Marker = 0x3c81b7f5
1095237fead6SMichael Halcrow  */
1096237fead6SMichael Halcrow static void write_ecryptfs_marker(char *page_virt, size_t *written)
1097237fead6SMichael Halcrow {
1098237fead6SMichael Halcrow 	u32 m_1, m_2;
1099237fead6SMichael Halcrow 
1100237fead6SMichael Halcrow 	get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1101237fead6SMichael Halcrow 	m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
110229335c6aSHarvey Harrison 	put_unaligned_be32(m_1, page_virt);
110329335c6aSHarvey Harrison 	page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
110429335c6aSHarvey Harrison 	put_unaligned_be32(m_2, page_virt);
1105237fead6SMichael Halcrow 	(*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1106237fead6SMichael Halcrow }
1107237fead6SMichael Halcrow 
1108237fead6SMichael Halcrow static void
1109237fead6SMichael Halcrow write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1110237fead6SMichael Halcrow 		     size_t *written)
1111237fead6SMichael Halcrow {
1112237fead6SMichael Halcrow 	u32 flags = 0;
1113237fead6SMichael Halcrow 	int i;
1114237fead6SMichael Halcrow 
1115237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1116237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
1117e2bd99ecSMichael Halcrow 		if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
1118237fead6SMichael Halcrow 			flags |= ecryptfs_flag_map[i].file_flag;
1119237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
1120237fead6SMichael Halcrow 	flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
112129335c6aSHarvey Harrison 	put_unaligned_be32(flags, page_virt);
1122237fead6SMichael Halcrow 	(*written) = 4;
1123237fead6SMichael Halcrow }
1124237fead6SMichael Halcrow 
1125237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem {
1126237fead6SMichael Halcrow 	char cipher_str[16];
112719e66a67STrevor Highland 	u8 cipher_code;
1128237fead6SMichael Halcrow };
1129237fead6SMichael Halcrow 
1130237fead6SMichael Halcrow /* Add support for additional ciphers by adding elements here. The
1131237fead6SMichael Halcrow  * cipher_code is whatever OpenPGP applicatoins use to identify the
1132237fead6SMichael Halcrow  * ciphers. List in order of probability. */
1133237fead6SMichael Halcrow static struct ecryptfs_cipher_code_str_map_elem
1134237fead6SMichael Halcrow ecryptfs_cipher_code_str_map[] = {
1135237fead6SMichael Halcrow 	{"aes",RFC2440_CIPHER_AES_128 },
1136237fead6SMichael Halcrow 	{"blowfish", RFC2440_CIPHER_BLOWFISH},
1137237fead6SMichael Halcrow 	{"des3_ede", RFC2440_CIPHER_DES3_EDE},
1138237fead6SMichael Halcrow 	{"cast5", RFC2440_CIPHER_CAST_5},
1139237fead6SMichael Halcrow 	{"twofish", RFC2440_CIPHER_TWOFISH},
1140237fead6SMichael Halcrow 	{"cast6", RFC2440_CIPHER_CAST_6},
1141237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_192},
1142237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_256}
1143237fead6SMichael Halcrow };
1144237fead6SMichael Halcrow 
1145237fead6SMichael Halcrow /**
1146237fead6SMichael Halcrow  * ecryptfs_code_for_cipher_string
11479c79f34fSMichael Halcrow  * @cipher_name: The string alias for the cipher
11489c79f34fSMichael Halcrow  * @key_bytes: Length of key in bytes; used for AES code selection
1149237fead6SMichael Halcrow  *
1150237fead6SMichael Halcrow  * Returns zero on no match, or the cipher code on match
1151237fead6SMichael Halcrow  */
11529c79f34fSMichael Halcrow u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
1153237fead6SMichael Halcrow {
1154237fead6SMichael Halcrow 	int i;
115519e66a67STrevor Highland 	u8 code = 0;
1156237fead6SMichael Halcrow 	struct ecryptfs_cipher_code_str_map_elem *map =
1157237fead6SMichael Halcrow 		ecryptfs_cipher_code_str_map;
1158237fead6SMichael Halcrow 
11599c79f34fSMichael Halcrow 	if (strcmp(cipher_name, "aes") == 0) {
11609c79f34fSMichael Halcrow 		switch (key_bytes) {
1161237fead6SMichael Halcrow 		case 16:
1162237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_128;
1163237fead6SMichael Halcrow 			break;
1164237fead6SMichael Halcrow 		case 24:
1165237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_192;
1166237fead6SMichael Halcrow 			break;
1167237fead6SMichael Halcrow 		case 32:
1168237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_256;
1169237fead6SMichael Halcrow 		}
1170237fead6SMichael Halcrow 	} else {
1171237fead6SMichael Halcrow 		for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
11729c79f34fSMichael Halcrow 			if (strcmp(cipher_name, map[i].cipher_str) == 0) {
1173237fead6SMichael Halcrow 				code = map[i].cipher_code;
1174237fead6SMichael Halcrow 				break;
1175237fead6SMichael Halcrow 			}
1176237fead6SMichael Halcrow 	}
1177237fead6SMichael Halcrow 	return code;
1178237fead6SMichael Halcrow }
1179237fead6SMichael Halcrow 
1180237fead6SMichael Halcrow /**
1181237fead6SMichael Halcrow  * ecryptfs_cipher_code_to_string
1182237fead6SMichael Halcrow  * @str: Destination to write out the cipher name
1183237fead6SMichael Halcrow  * @cipher_code: The code to convert to cipher name string
1184237fead6SMichael Halcrow  *
1185237fead6SMichael Halcrow  * Returns zero on success
1186237fead6SMichael Halcrow  */
118719e66a67STrevor Highland int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
1188237fead6SMichael Halcrow {
1189237fead6SMichael Halcrow 	int rc = 0;
1190237fead6SMichael Halcrow 	int i;
1191237fead6SMichael Halcrow 
1192237fead6SMichael Halcrow 	str[0] = '\0';
1193237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1194237fead6SMichael Halcrow 		if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1195237fead6SMichael Halcrow 			strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1196237fead6SMichael Halcrow 	if (str[0] == '\0') {
1197237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1198237fead6SMichael Halcrow 				"[%d]\n", cipher_code);
1199237fead6SMichael Halcrow 		rc = -EINVAL;
1200237fead6SMichael Halcrow 	}
1201237fead6SMichael Halcrow 	return rc;
1202237fead6SMichael Halcrow }
1203237fead6SMichael Halcrow 
1204d7cdc5feSMichael Halcrow int ecryptfs_read_and_validate_header_region(char *data,
1205d7cdc5feSMichael Halcrow 					     struct inode *ecryptfs_inode)
1206dd2a3b7aSMichael Halcrow {
1207d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1208d7cdc5feSMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
1209dd2a3b7aSMichael Halcrow 	int rc;
1210dd2a3b7aSMichael Halcrow 
1211addd65adSMichael Halcrow 	if (crypt_stat->extent_size == 0)
1212addd65adSMichael Halcrow 		crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
1213d7cdc5feSMichael Halcrow 	rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
1214d7cdc5feSMichael Halcrow 				 ecryptfs_inode);
1215d7cdc5feSMichael Halcrow 	if (rc) {
1216d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
121718d1dbf1SHarvey Harrison 		       __func__, rc);
1218dd2a3b7aSMichael Halcrow 		goto out;
1219d7cdc5feSMichael Halcrow 	}
1220d7cdc5feSMichael Halcrow 	if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
1221dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1222d7cdc5feSMichael Halcrow 	}
1223dd2a3b7aSMichael Halcrow out:
1224dd2a3b7aSMichael Halcrow 	return rc;
1225dd2a3b7aSMichael Halcrow }
1226dd2a3b7aSMichael Halcrow 
1227e77a56ddSMichael Halcrow void
1228e77a56ddSMichael Halcrow ecryptfs_write_header_metadata(char *virt,
1229e77a56ddSMichael Halcrow 			       struct ecryptfs_crypt_stat *crypt_stat,
1230237fead6SMichael Halcrow 			       size_t *written)
1231237fead6SMichael Halcrow {
1232237fead6SMichael Halcrow 	u32 header_extent_size;
1233237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1234237fead6SMichael Halcrow 
123545eaab79SMichael Halcrow 	header_extent_size = (u32)crypt_stat->extent_size;
1236237fead6SMichael Halcrow 	num_header_extents_at_front =
1237cc11beffSMichael Halcrow 		(u16)(crypt_stat->num_header_bytes_at_front
1238cc11beffSMichael Halcrow 		      / crypt_stat->extent_size);
123929335c6aSHarvey Harrison 	put_unaligned_be32(header_extent_size, virt);
1240237fead6SMichael Halcrow 	virt += 4;
124129335c6aSHarvey Harrison 	put_unaligned_be16(num_header_extents_at_front, virt);
1242237fead6SMichael Halcrow 	(*written) = 6;
1243237fead6SMichael Halcrow }
1244237fead6SMichael Halcrow 
1245237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_1;
1246237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_2;
1247237fead6SMichael Halcrow 
1248237fead6SMichael Halcrow /**
1249237fead6SMichael Halcrow  * ecryptfs_write_headers_virt
125022e78fafSMichael Halcrow  * @page_virt: The virtual address to write the headers to
125187b811c3SEric Sandeen  * @max: The size of memory allocated at page_virt
125222e78fafSMichael Halcrow  * @size: Set to the number of bytes written by this function
125322e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
125422e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
1255237fead6SMichael Halcrow  *
1256237fead6SMichael Halcrow  * Format version: 1
1257237fead6SMichael Halcrow  *
1258237fead6SMichael Halcrow  *   Header Extent:
1259237fead6SMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
1260237fead6SMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
1261237fead6SMichael Halcrow  *     Octets 16-19:      Flags
1262237fead6SMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
1263237fead6SMichael Halcrow  *      Octets 17-18:     Reserved
1264237fead6SMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
1265237fead6SMichael Halcrow  *                        Bit 2: Encrypted?
1266237fead6SMichael Halcrow  *                        Bits 3-8: Reserved
1267237fead6SMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
1268237fead6SMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
1269237fead6SMichael Halcrow  *                        (big-endian)
1270237fead6SMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
1271237fead6SMichael Halcrow  *   Data Extent 0:
1272237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1273237fead6SMichael Halcrow  *   Data Extent 1:
1274237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1275237fead6SMichael Halcrow  *   ...
1276237fead6SMichael Halcrow  *
1277237fead6SMichael Halcrow  * Returns zero on success
1278237fead6SMichael Halcrow  */
127987b811c3SEric Sandeen static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
128087b811c3SEric Sandeen 				       size_t *size,
1281237fead6SMichael Halcrow 				       struct ecryptfs_crypt_stat *crypt_stat,
1282237fead6SMichael Halcrow 				       struct dentry *ecryptfs_dentry)
1283237fead6SMichael Halcrow {
1284237fead6SMichael Halcrow 	int rc;
1285237fead6SMichael Halcrow 	size_t written;
1286237fead6SMichael Halcrow 	size_t offset;
1287237fead6SMichael Halcrow 
1288237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1289237fead6SMichael Halcrow 	write_ecryptfs_marker((page_virt + offset), &written);
1290237fead6SMichael Halcrow 	offset += written;
1291237fead6SMichael Halcrow 	write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1292237fead6SMichael Halcrow 	offset += written;
1293e77a56ddSMichael Halcrow 	ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1294e77a56ddSMichael Halcrow 				       &written);
1295237fead6SMichael Halcrow 	offset += written;
1296237fead6SMichael Halcrow 	rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1297237fead6SMichael Halcrow 					      ecryptfs_dentry, &written,
129887b811c3SEric Sandeen 					      max - offset);
1299237fead6SMichael Halcrow 	if (rc)
1300237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1301237fead6SMichael Halcrow 				"set; rc = [%d]\n", rc);
1302dd2a3b7aSMichael Halcrow 	if (size) {
1303dd2a3b7aSMichael Halcrow 		offset += written;
1304dd2a3b7aSMichael Halcrow 		*size = offset;
1305dd2a3b7aSMichael Halcrow 	}
1306dd2a3b7aSMichael Halcrow 	return rc;
1307dd2a3b7aSMichael Halcrow }
1308dd2a3b7aSMichael Halcrow 
130922e78fafSMichael Halcrow static int
13108faece5fSTyler Hicks ecryptfs_write_metadata_to_contents(struct dentry *ecryptfs_dentry,
13118faece5fSTyler Hicks 				    char *virt, size_t virt_len)
1312dd2a3b7aSMichael Halcrow {
1313d7cdc5feSMichael Halcrow 	int rc;
1314dd2a3b7aSMichael Halcrow 
1315cc11beffSMichael Halcrow 	rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, virt,
13168faece5fSTyler Hicks 				  0, virt_len);
1317cc11beffSMichael Halcrow 	if (rc)
1318d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to write header "
131918d1dbf1SHarvey Harrison 		       "information to lower file; rc = [%d]\n", __func__,
1320d7cdc5feSMichael Halcrow 		       rc);
132170456600SMichael Halcrow 	return rc;
1322dd2a3b7aSMichael Halcrow }
1323dd2a3b7aSMichael Halcrow 
132422e78fafSMichael Halcrow static int
132522e78fafSMichael Halcrow ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1326dd2a3b7aSMichael Halcrow 				 char *page_virt, size_t size)
1327dd2a3b7aSMichael Halcrow {
1328dd2a3b7aSMichael Halcrow 	int rc;
1329dd2a3b7aSMichael Halcrow 
1330dd2a3b7aSMichael Halcrow 	rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1331dd2a3b7aSMichael Halcrow 			       size, 0);
1332237fead6SMichael Halcrow 	return rc;
1333237fead6SMichael Halcrow }
1334237fead6SMichael Halcrow 
13358faece5fSTyler Hicks static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
13368faece5fSTyler Hicks 					       unsigned int order)
13378faece5fSTyler Hicks {
13388faece5fSTyler Hicks 	struct page *page;
13398faece5fSTyler Hicks 
13408faece5fSTyler Hicks 	page = alloc_pages(gfp_mask | __GFP_ZERO, order);
13418faece5fSTyler Hicks 	if (page)
13428faece5fSTyler Hicks 		return (unsigned long) page_address(page);
13438faece5fSTyler Hicks 	return 0;
13448faece5fSTyler Hicks }
13458faece5fSTyler Hicks 
1346237fead6SMichael Halcrow /**
1347dd2a3b7aSMichael Halcrow  * ecryptfs_write_metadata
134822e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
1349237fead6SMichael Halcrow  *
1350237fead6SMichael Halcrow  * Write the file headers out.  This will likely involve a userspace
1351237fead6SMichael Halcrow  * callout, in which the session key is encrypted with one or more
1352237fead6SMichael Halcrow  * public keys and/or the passphrase necessary to do the encryption is
1353237fead6SMichael Halcrow  * retrieved via a prompt.  Exactly what happens at this point should
1354237fead6SMichael Halcrow  * be policy-dependent.
1355237fead6SMichael Halcrow  *
1356237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
1357237fead6SMichael Halcrow  */
1358d7cdc5feSMichael Halcrow int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
1359237fead6SMichael Halcrow {
1360d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1361d7cdc5feSMichael Halcrow 		&ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
13628faece5fSTyler Hicks 	unsigned int order;
1363cc11beffSMichael Halcrow 	char *virt;
13648faece5fSTyler Hicks 	size_t virt_len;
1365d7cdc5feSMichael Halcrow 	size_t size = 0;
1366237fead6SMichael Halcrow 	int rc = 0;
1367237fead6SMichael Halcrow 
1368e2bd99ecSMichael Halcrow 	if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1369e2bd99ecSMichael Halcrow 		if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
1370d7cdc5feSMichael Halcrow 			printk(KERN_ERR "Key is invalid; bailing out\n");
1371237fead6SMichael Halcrow 			rc = -EINVAL;
1372237fead6SMichael Halcrow 			goto out;
1373237fead6SMichael Halcrow 		}
1374237fead6SMichael Halcrow 	} else {
1375cc11beffSMichael Halcrow 		printk(KERN_WARNING "%s: Encrypted flag not set\n",
137618d1dbf1SHarvey Harrison 		       __func__);
1377237fead6SMichael Halcrow 		rc = -EINVAL;
1378237fead6SMichael Halcrow 		goto out;
1379237fead6SMichael Halcrow 	}
13808faece5fSTyler Hicks 	virt_len = crypt_stat->num_header_bytes_at_front;
13818faece5fSTyler Hicks 	order = get_order(virt_len);
1382237fead6SMichael Halcrow 	/* Released in this function */
13838faece5fSTyler Hicks 	virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
1384cc11beffSMichael Halcrow 	if (!virt) {
138518d1dbf1SHarvey Harrison 		printk(KERN_ERR "%s: Out of memory\n", __func__);
1386237fead6SMichael Halcrow 		rc = -ENOMEM;
1387237fead6SMichael Halcrow 		goto out;
1388237fead6SMichael Halcrow 	}
13898faece5fSTyler Hicks 	rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
13908faece5fSTyler Hicks 					 ecryptfs_dentry);
1391237fead6SMichael Halcrow 	if (unlikely(rc)) {
1392cc11beffSMichael Halcrow 		printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
139318d1dbf1SHarvey Harrison 		       __func__, rc);
1394237fead6SMichael Halcrow 		goto out_free;
1395237fead6SMichael Halcrow 	}
1396dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
13978faece5fSTyler Hicks 		rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
13988faece5fSTyler Hicks 						      size);
1399dd2a3b7aSMichael Halcrow 	else
14008faece5fSTyler Hicks 		rc = ecryptfs_write_metadata_to_contents(ecryptfs_dentry, virt,
14018faece5fSTyler Hicks 							 virt_len);
1402dd2a3b7aSMichael Halcrow 	if (rc) {
1403cc11beffSMichael Halcrow 		printk(KERN_ERR "%s: Error writing metadata out to lower file; "
140418d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, rc);
1405dd2a3b7aSMichael Halcrow 		goto out_free;
1406237fead6SMichael Halcrow 	}
1407237fead6SMichael Halcrow out_free:
14088faece5fSTyler Hicks 	free_pages((unsigned long)virt, order);
1409237fead6SMichael Halcrow out:
1410237fead6SMichael Halcrow 	return rc;
1411237fead6SMichael Halcrow }
1412237fead6SMichael Halcrow 
1413dd2a3b7aSMichael Halcrow #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1414dd2a3b7aSMichael Halcrow #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
1415237fead6SMichael Halcrow static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
1416dd2a3b7aSMichael Halcrow 				 char *virt, int *bytes_read,
1417dd2a3b7aSMichael Halcrow 				 int validate_header_size)
1418237fead6SMichael Halcrow {
1419237fead6SMichael Halcrow 	int rc = 0;
1420237fead6SMichael Halcrow 	u32 header_extent_size;
1421237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1422237fead6SMichael Halcrow 
142329335c6aSHarvey Harrison 	header_extent_size = get_unaligned_be32(virt);
142429335c6aSHarvey Harrison 	virt += sizeof(__be32);
142529335c6aSHarvey Harrison 	num_header_extents_at_front = get_unaligned_be16(virt);
1426cc11beffSMichael Halcrow 	crypt_stat->num_header_bytes_at_front =
1427cc11beffSMichael Halcrow 		(((size_t)num_header_extents_at_front
1428cc11beffSMichael Halcrow 		  * (size_t)header_extent_size));
142929335c6aSHarvey Harrison 	(*bytes_read) = (sizeof(__be32) + sizeof(__be16));
1430dd2a3b7aSMichael Halcrow 	if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
1431cc11beffSMichael Halcrow 	    && (crypt_stat->num_header_bytes_at_front
1432dd2a3b7aSMichael Halcrow 		< ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
1433237fead6SMichael Halcrow 		rc = -EINVAL;
1434cc11beffSMichael Halcrow 		printk(KERN_WARNING "Invalid header size: [%zd]\n",
1435cc11beffSMichael Halcrow 		       crypt_stat->num_header_bytes_at_front);
1436237fead6SMichael Halcrow 	}
1437237fead6SMichael Halcrow 	return rc;
1438237fead6SMichael Halcrow }
1439237fead6SMichael Halcrow 
1440237fead6SMichael Halcrow /**
1441237fead6SMichael Halcrow  * set_default_header_data
144222e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1443237fead6SMichael Halcrow  *
1444237fead6SMichael Halcrow  * For version 0 file format; this function is only for backwards
1445237fead6SMichael Halcrow  * compatibility for files created with the prior versions of
1446237fead6SMichael Halcrow  * eCryptfs.
1447237fead6SMichael Halcrow  */
1448237fead6SMichael Halcrow static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1449237fead6SMichael Halcrow {
1450cc11beffSMichael Halcrow 	crypt_stat->num_header_bytes_at_front =
1451cc11beffSMichael Halcrow 		ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
1452237fead6SMichael Halcrow }
1453237fead6SMichael Halcrow 
1454237fead6SMichael Halcrow /**
1455237fead6SMichael Halcrow  * ecryptfs_read_headers_virt
145622e78fafSMichael Halcrow  * @page_virt: The virtual address into which to read the headers
145722e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
145822e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
145922e78fafSMichael Halcrow  * @validate_header_size: Whether to validate the header size while reading
1460237fead6SMichael Halcrow  *
1461237fead6SMichael Halcrow  * Read/parse the header data. The header format is detailed in the
1462237fead6SMichael Halcrow  * comment block for the ecryptfs_write_headers_virt() function.
1463237fead6SMichael Halcrow  *
1464237fead6SMichael Halcrow  * Returns zero on success
1465237fead6SMichael Halcrow  */
1466237fead6SMichael Halcrow static int ecryptfs_read_headers_virt(char *page_virt,
1467237fead6SMichael Halcrow 				      struct ecryptfs_crypt_stat *crypt_stat,
1468dd2a3b7aSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
1469dd2a3b7aSMichael Halcrow 				      int validate_header_size)
1470237fead6SMichael Halcrow {
1471237fead6SMichael Halcrow 	int rc = 0;
1472237fead6SMichael Halcrow 	int offset;
1473237fead6SMichael Halcrow 	int bytes_read;
1474237fead6SMichael Halcrow 
1475237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
1476237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1477237fead6SMichael Halcrow 		ecryptfs_dentry->d_sb)->mount_crypt_stat;
1478237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1479237fead6SMichael Halcrow 	rc = contains_ecryptfs_marker(page_virt + offset);
1480237fead6SMichael Halcrow 	if (rc == 0) {
1481237fead6SMichael Halcrow 		rc = -EINVAL;
1482237fead6SMichael Halcrow 		goto out;
1483237fead6SMichael Halcrow 	}
1484237fead6SMichael Halcrow 	offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1485237fead6SMichael Halcrow 	rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1486237fead6SMichael Halcrow 				    &bytes_read);
1487237fead6SMichael Halcrow 	if (rc) {
1488237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1489237fead6SMichael Halcrow 		goto out;
1490237fead6SMichael Halcrow 	}
1491237fead6SMichael Halcrow 	if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1492237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1493237fead6SMichael Halcrow 				"file version [%d] is supported by this "
1494237fead6SMichael Halcrow 				"version of eCryptfs\n",
1495237fead6SMichael Halcrow 				crypt_stat->file_version,
1496237fead6SMichael Halcrow 				ECRYPTFS_SUPPORTED_FILE_VERSION);
1497237fead6SMichael Halcrow 		rc = -EINVAL;
1498237fead6SMichael Halcrow 		goto out;
1499237fead6SMichael Halcrow 	}
1500237fead6SMichael Halcrow 	offset += bytes_read;
1501237fead6SMichael Halcrow 	if (crypt_stat->file_version >= 1) {
1502237fead6SMichael Halcrow 		rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1503dd2a3b7aSMichael Halcrow 					   &bytes_read, validate_header_size);
1504237fead6SMichael Halcrow 		if (rc) {
1505237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Error reading header "
1506237fead6SMichael Halcrow 					"metadata; rc = [%d]\n", rc);
1507237fead6SMichael Halcrow 		}
1508237fead6SMichael Halcrow 		offset += bytes_read;
1509237fead6SMichael Halcrow 	} else
1510237fead6SMichael Halcrow 		set_default_header_data(crypt_stat);
1511237fead6SMichael Halcrow 	rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1512237fead6SMichael Halcrow 				       ecryptfs_dentry);
1513237fead6SMichael Halcrow out:
1514237fead6SMichael Halcrow 	return rc;
1515237fead6SMichael Halcrow }
1516237fead6SMichael Halcrow 
1517237fead6SMichael Halcrow /**
1518dd2a3b7aSMichael Halcrow  * ecryptfs_read_xattr_region
151922e78fafSMichael Halcrow  * @page_virt: The vitual address into which to read the xattr data
15202ed92554SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
1521dd2a3b7aSMichael Halcrow  *
1522dd2a3b7aSMichael Halcrow  * Attempts to read the crypto metadata from the extended attribute
1523dd2a3b7aSMichael Halcrow  * region of the lower file.
152422e78fafSMichael Halcrow  *
152522e78fafSMichael Halcrow  * Returns zero on success; non-zero on error
1526dd2a3b7aSMichael Halcrow  */
1527d7cdc5feSMichael Halcrow int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
1528dd2a3b7aSMichael Halcrow {
1529d7cdc5feSMichael Halcrow 	struct dentry *lower_dentry =
1530d7cdc5feSMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
1531dd2a3b7aSMichael Halcrow 	ssize_t size;
1532dd2a3b7aSMichael Halcrow 	int rc = 0;
1533dd2a3b7aSMichael Halcrow 
1534d7cdc5feSMichael Halcrow 	size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1535dd2a3b7aSMichael Halcrow 				       page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1536dd2a3b7aSMichael Halcrow 	if (size < 0) {
153725bd8174SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0))
153825bd8174SMichael Halcrow 			printk(KERN_INFO "Error attempting to read the [%s] "
153925bd8174SMichael Halcrow 			       "xattr from the lower file; return value = "
154025bd8174SMichael Halcrow 			       "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
1541dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1542dd2a3b7aSMichael Halcrow 		goto out;
1543dd2a3b7aSMichael Halcrow 	}
1544dd2a3b7aSMichael Halcrow out:
1545dd2a3b7aSMichael Halcrow 	return rc;
1546dd2a3b7aSMichael Halcrow }
1547dd2a3b7aSMichael Halcrow 
1548dd2a3b7aSMichael Halcrow int ecryptfs_read_and_validate_xattr_region(char *page_virt,
1549dd2a3b7aSMichael Halcrow 					    struct dentry *ecryptfs_dentry)
1550dd2a3b7aSMichael Halcrow {
1551dd2a3b7aSMichael Halcrow 	int rc;
1552dd2a3b7aSMichael Halcrow 
1553d7cdc5feSMichael Halcrow 	rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
1554dd2a3b7aSMichael Halcrow 	if (rc)
1555dd2a3b7aSMichael Halcrow 		goto out;
1556dd2a3b7aSMichael Halcrow 	if (!contains_ecryptfs_marker(page_virt	+ ECRYPTFS_FILE_SIZE_BYTES)) {
1557dd2a3b7aSMichael Halcrow 		printk(KERN_WARNING "Valid data found in [%s] xattr, but "
1558dd2a3b7aSMichael Halcrow 			"the marker is invalid\n", ECRYPTFS_XATTR_NAME);
1559dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1560dd2a3b7aSMichael Halcrow 	}
1561dd2a3b7aSMichael Halcrow out:
1562dd2a3b7aSMichael Halcrow 	return rc;
1563dd2a3b7aSMichael Halcrow }
1564dd2a3b7aSMichael Halcrow 
1565dd2a3b7aSMichael Halcrow /**
1566dd2a3b7aSMichael Halcrow  * ecryptfs_read_metadata
1567dd2a3b7aSMichael Halcrow  *
1568dd2a3b7aSMichael Halcrow  * Common entry point for reading file metadata. From here, we could
1569dd2a3b7aSMichael Halcrow  * retrieve the header information from the header region of the file,
1570dd2a3b7aSMichael Halcrow  * the xattr region of the file, or some other repostory that is
1571dd2a3b7aSMichael Halcrow  * stored separately from the file itself. The current implementation
1572dd2a3b7aSMichael Halcrow  * supports retrieving the metadata information from the file contents
1573dd2a3b7aSMichael Halcrow  * and from the xattr region.
1574237fead6SMichael Halcrow  *
1575237fead6SMichael Halcrow  * Returns zero if valid headers found and parsed; non-zero otherwise
1576237fead6SMichael Halcrow  */
1577d7cdc5feSMichael Halcrow int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
1578237fead6SMichael Halcrow {
1579237fead6SMichael Halcrow 	int rc = 0;
1580237fead6SMichael Halcrow 	char *page_virt = NULL;
1581d7cdc5feSMichael Halcrow 	struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
1582237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1583d7cdc5feSMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1584e77a56ddSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1585e77a56ddSMichael Halcrow 		&ecryptfs_superblock_to_private(
1586e77a56ddSMichael Halcrow 			ecryptfs_dentry->d_sb)->mount_crypt_stat;
1587237fead6SMichael Halcrow 
1588e77a56ddSMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1589e77a56ddSMichael Halcrow 						      mount_crypt_stat);
1590237fead6SMichael Halcrow 	/* Read the first page from the underlying file */
1591f7267c0cSChristoph Lameter 	page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
1592237fead6SMichael Halcrow 	if (!page_virt) {
1593237fead6SMichael Halcrow 		rc = -ENOMEM;
1594d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Unable to allocate page_virt\n",
159518d1dbf1SHarvey Harrison 		       __func__);
1596237fead6SMichael Halcrow 		goto out;
1597237fead6SMichael Halcrow 	}
1598d7cdc5feSMichael Halcrow 	rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1599d7cdc5feSMichael Halcrow 				 ecryptfs_inode);
1600d7cdc5feSMichael Halcrow 	if (!rc)
1601237fead6SMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1602dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1603dd2a3b7aSMichael Halcrow 						ECRYPTFS_VALIDATE_HEADER_SIZE);
1604dd2a3b7aSMichael Halcrow 	if (rc) {
1605d7cdc5feSMichael Halcrow 		rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
1606237fead6SMichael Halcrow 		if (rc) {
1607dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1608dd2a3b7aSMichael Halcrow 			       "file header region or xattr region\n");
1609237fead6SMichael Halcrow 			rc = -EINVAL;
1610dd2a3b7aSMichael Halcrow 			goto out;
1611dd2a3b7aSMichael Halcrow 		}
1612dd2a3b7aSMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1613dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1614dd2a3b7aSMichael Halcrow 						ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1615dd2a3b7aSMichael Halcrow 		if (rc) {
1616dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1617dd2a3b7aSMichael Halcrow 			       "file xattr region either\n");
1618dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1619dd2a3b7aSMichael Halcrow 		}
1620dd2a3b7aSMichael Halcrow 		if (crypt_stat->mount_crypt_stat->flags
1621dd2a3b7aSMichael Halcrow 		    & ECRYPTFS_XATTR_METADATA_ENABLED) {
1622dd2a3b7aSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1623dd2a3b7aSMichael Halcrow 		} else {
1624dd2a3b7aSMichael Halcrow 			printk(KERN_WARNING "Attempt to access file with "
1625dd2a3b7aSMichael Halcrow 			       "crypto metadata only in the extended attribute "
1626dd2a3b7aSMichael Halcrow 			       "region, but eCryptfs was mounted without "
1627dd2a3b7aSMichael Halcrow 			       "xattr support enabled. eCryptfs will not treat "
1628dd2a3b7aSMichael Halcrow 			       "this like an encrypted file.\n");
1629dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1630dd2a3b7aSMichael Halcrow 		}
1631237fead6SMichael Halcrow 	}
1632237fead6SMichael Halcrow out:
1633237fead6SMichael Halcrow 	if (page_virt) {
1634237fead6SMichael Halcrow 		memset(page_virt, 0, PAGE_CACHE_SIZE);
1635237fead6SMichael Halcrow 		kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1636237fead6SMichael Halcrow 	}
1637237fead6SMichael Halcrow 	return rc;
1638237fead6SMichael Halcrow }
1639237fead6SMichael Halcrow 
1640237fead6SMichael Halcrow /**
164151ca58dcSMichael Halcrow  * ecryptfs_encrypt_filename - encrypt filename
164251ca58dcSMichael Halcrow  *
164351ca58dcSMichael Halcrow  * CBC-encrypts the filename. We do not want to encrypt the same
164451ca58dcSMichael Halcrow  * filename with the same key and IV, which may happen with hard
164551ca58dcSMichael Halcrow  * links, so we prepend random bits to each filename.
164651ca58dcSMichael Halcrow  *
164751ca58dcSMichael Halcrow  * Returns zero on success; non-zero otherwise
164851ca58dcSMichael Halcrow  */
164951ca58dcSMichael Halcrow static int
165051ca58dcSMichael Halcrow ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
165151ca58dcSMichael Halcrow 			  struct ecryptfs_crypt_stat *crypt_stat,
165251ca58dcSMichael Halcrow 			  struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
165351ca58dcSMichael Halcrow {
165451ca58dcSMichael Halcrow 	int rc = 0;
165551ca58dcSMichael Halcrow 
165651ca58dcSMichael Halcrow 	filename->encrypted_filename = NULL;
165751ca58dcSMichael Halcrow 	filename->encrypted_filename_size = 0;
165851ca58dcSMichael Halcrow 	if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
165951ca58dcSMichael Halcrow 	    || (mount_crypt_stat && (mount_crypt_stat->flags
166051ca58dcSMichael Halcrow 				     & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
166151ca58dcSMichael Halcrow 		size_t packet_size;
166251ca58dcSMichael Halcrow 		size_t remaining_bytes;
166351ca58dcSMichael Halcrow 
166451ca58dcSMichael Halcrow 		rc = ecryptfs_write_tag_70_packet(
166551ca58dcSMichael Halcrow 			NULL, NULL,
166651ca58dcSMichael Halcrow 			&filename->encrypted_filename_size,
166751ca58dcSMichael Halcrow 			mount_crypt_stat, NULL,
166851ca58dcSMichael Halcrow 			filename->filename_size);
166951ca58dcSMichael Halcrow 		if (rc) {
167051ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to get packet "
167151ca58dcSMichael Halcrow 			       "size for tag 72; rc = [%d]\n", __func__,
167251ca58dcSMichael Halcrow 			       rc);
167351ca58dcSMichael Halcrow 			filename->encrypted_filename_size = 0;
167451ca58dcSMichael Halcrow 			goto out;
167551ca58dcSMichael Halcrow 		}
167651ca58dcSMichael Halcrow 		filename->encrypted_filename =
167751ca58dcSMichael Halcrow 			kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
167851ca58dcSMichael Halcrow 		if (!filename->encrypted_filename) {
167951ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
1680df261c52SMichael Halcrow 			       "to kmalloc [%zd] bytes\n", __func__,
168151ca58dcSMichael Halcrow 			       filename->encrypted_filename_size);
168251ca58dcSMichael Halcrow 			rc = -ENOMEM;
168351ca58dcSMichael Halcrow 			goto out;
168451ca58dcSMichael Halcrow 		}
168551ca58dcSMichael Halcrow 		remaining_bytes = filename->encrypted_filename_size;
168651ca58dcSMichael Halcrow 		rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
168751ca58dcSMichael Halcrow 						  &remaining_bytes,
168851ca58dcSMichael Halcrow 						  &packet_size,
168951ca58dcSMichael Halcrow 						  mount_crypt_stat,
169051ca58dcSMichael Halcrow 						  filename->filename,
169151ca58dcSMichael Halcrow 						  filename->filename_size);
169251ca58dcSMichael Halcrow 		if (rc) {
169351ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to generate "
169451ca58dcSMichael Halcrow 			       "tag 70 packet; rc = [%d]\n", __func__,
169551ca58dcSMichael Halcrow 			       rc);
169651ca58dcSMichael Halcrow 			kfree(filename->encrypted_filename);
169751ca58dcSMichael Halcrow 			filename->encrypted_filename = NULL;
169851ca58dcSMichael Halcrow 			filename->encrypted_filename_size = 0;
169951ca58dcSMichael Halcrow 			goto out;
170051ca58dcSMichael Halcrow 		}
170151ca58dcSMichael Halcrow 		filename->encrypted_filename_size = packet_size;
170251ca58dcSMichael Halcrow 	} else {
170351ca58dcSMichael Halcrow 		printk(KERN_ERR "%s: No support for requested filename "
170451ca58dcSMichael Halcrow 		       "encryption method in this release\n", __func__);
170551ca58dcSMichael Halcrow 		rc = -ENOTSUPP;
170651ca58dcSMichael Halcrow 		goto out;
170751ca58dcSMichael Halcrow 	}
170851ca58dcSMichael Halcrow out:
170951ca58dcSMichael Halcrow 	return rc;
171051ca58dcSMichael Halcrow }
171151ca58dcSMichael Halcrow 
171251ca58dcSMichael Halcrow static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
171351ca58dcSMichael Halcrow 				  const char *name, size_t name_size)
171451ca58dcSMichael Halcrow {
171551ca58dcSMichael Halcrow 	int rc = 0;
171651ca58dcSMichael Halcrow 
1717fd9fc842STyler Hicks 	(*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
171851ca58dcSMichael Halcrow 	if (!(*copied_name)) {
171951ca58dcSMichael Halcrow 		rc = -ENOMEM;
172051ca58dcSMichael Halcrow 		goto out;
172151ca58dcSMichael Halcrow 	}
172251ca58dcSMichael Halcrow 	memcpy((void *)(*copied_name), (void *)name, name_size);
172351ca58dcSMichael Halcrow 	(*copied_name)[(name_size)] = '\0';	/* Only for convenience
172451ca58dcSMichael Halcrow 						 * in printing out the
172551ca58dcSMichael Halcrow 						 * string in debug
172651ca58dcSMichael Halcrow 						 * messages */
1727fd9fc842STyler Hicks 	(*copied_name_size) = name_size;
172851ca58dcSMichael Halcrow out:
172951ca58dcSMichael Halcrow 	return rc;
173051ca58dcSMichael Halcrow }
173151ca58dcSMichael Halcrow 
173251ca58dcSMichael Halcrow /**
1733f4aad16aSMichael Halcrow  * ecryptfs_process_key_cipher - Perform key cipher initialization.
1734237fead6SMichael Halcrow  * @key_tfm: Crypto context for key material, set by this function
1735e5d9cbdeSMichael Halcrow  * @cipher_name: Name of the cipher
1736e5d9cbdeSMichael Halcrow  * @key_size: Size of the key in bytes
1737237fead6SMichael Halcrow  *
1738237fead6SMichael Halcrow  * Returns zero on success. Any crypto_tfm structs allocated here
1739237fead6SMichael Halcrow  * should be released by other functions, such as on a superblock put
1740237fead6SMichael Halcrow  * event, regardless of whether this function succeeds for fails.
1741237fead6SMichael Halcrow  */
1742cd9d67dfSMichael Halcrow static int
1743f4aad16aSMichael Halcrow ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1744f4aad16aSMichael Halcrow 			    char *cipher_name, size_t *key_size)
1745237fead6SMichael Halcrow {
1746237fead6SMichael Halcrow 	char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
17478bba066fSMichael Halcrow 	char *full_alg_name;
1748237fead6SMichael Halcrow 	int rc;
1749237fead6SMichael Halcrow 
1750e5d9cbdeSMichael Halcrow 	*key_tfm = NULL;
1751e5d9cbdeSMichael Halcrow 	if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1752237fead6SMichael Halcrow 		rc = -EINVAL;
1753df261c52SMichael Halcrow 		printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
1754e5d9cbdeSMichael Halcrow 		      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1755237fead6SMichael Halcrow 		goto out;
1756237fead6SMichael Halcrow 	}
17578bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
17588bba066fSMichael Halcrow 						    "ecb");
17598bba066fSMichael Halcrow 	if (rc)
17608bba066fSMichael Halcrow 		goto out;
17618bba066fSMichael Halcrow 	*key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
17628bba066fSMichael Halcrow 	kfree(full_alg_name);
17638bba066fSMichael Halcrow 	if (IS_ERR(*key_tfm)) {
17648bba066fSMichael Halcrow 		rc = PTR_ERR(*key_tfm);
1765237fead6SMichael Halcrow 		printk(KERN_ERR "Unable to allocate crypto cipher with name "
17668bba066fSMichael Halcrow 		       "[%s]; rc = [%d]\n", cipher_name, rc);
1767237fead6SMichael Halcrow 		goto out;
1768237fead6SMichael Halcrow 	}
17698bba066fSMichael Halcrow 	crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
17708bba066fSMichael Halcrow 	if (*key_size == 0) {
17718bba066fSMichael Halcrow 		struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
17728bba066fSMichael Halcrow 
17738bba066fSMichael Halcrow 		*key_size = alg->max_keysize;
17748bba066fSMichael Halcrow 	}
1775e5d9cbdeSMichael Halcrow 	get_random_bytes(dummy_key, *key_size);
17768bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
1777237fead6SMichael Halcrow 	if (rc) {
1778df261c52SMichael Halcrow 		printk(KERN_ERR "Error attempting to set key of size [%zd] for "
1779e5d9cbdeSMichael Halcrow 		       "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
1780237fead6SMichael Halcrow 		rc = -EINVAL;
1781237fead6SMichael Halcrow 		goto out;
1782237fead6SMichael Halcrow 	}
1783237fead6SMichael Halcrow out:
1784237fead6SMichael Halcrow 	return rc;
1785237fead6SMichael Halcrow }
1786f4aad16aSMichael Halcrow 
1787f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_tfm_cache;
17887896b631SAdrian Bunk static struct list_head key_tfm_list;
1789af440f52SEric Sandeen struct mutex key_tfm_list_mutex;
1790f4aad16aSMichael Halcrow 
1791f4aad16aSMichael Halcrow int ecryptfs_init_crypto(void)
1792f4aad16aSMichael Halcrow {
1793f4aad16aSMichael Halcrow 	mutex_init(&key_tfm_list_mutex);
1794f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&key_tfm_list);
1795f4aad16aSMichael Halcrow 	return 0;
1796f4aad16aSMichael Halcrow }
1797f4aad16aSMichael Halcrow 
1798af440f52SEric Sandeen /**
1799af440f52SEric Sandeen  * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1800af440f52SEric Sandeen  *
1801af440f52SEric Sandeen  * Called only at module unload time
1802af440f52SEric Sandeen  */
1803fcd12835SMichael Halcrow int ecryptfs_destroy_crypto(void)
1804f4aad16aSMichael Halcrow {
1805f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1806f4aad16aSMichael Halcrow 
1807f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1808f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1809f4aad16aSMichael Halcrow 				 key_tfm_list) {
1810f4aad16aSMichael Halcrow 		list_del(&key_tfm->key_tfm_list);
1811f4aad16aSMichael Halcrow 		if (key_tfm->key_tfm)
1812f4aad16aSMichael Halcrow 			crypto_free_blkcipher(key_tfm->key_tfm);
1813f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1814f4aad16aSMichael Halcrow 	}
1815f4aad16aSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
1816f4aad16aSMichael Halcrow 	return 0;
1817f4aad16aSMichael Halcrow }
1818f4aad16aSMichael Halcrow 
1819f4aad16aSMichael Halcrow int
1820f4aad16aSMichael Halcrow ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1821f4aad16aSMichael Halcrow 			 size_t key_size)
1822f4aad16aSMichael Halcrow {
1823f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *tmp_tfm;
1824f4aad16aSMichael Halcrow 	int rc = 0;
1825f4aad16aSMichael Halcrow 
1826af440f52SEric Sandeen 	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1827af440f52SEric Sandeen 
1828f4aad16aSMichael Halcrow 	tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1829f4aad16aSMichael Halcrow 	if (key_tfm != NULL)
1830f4aad16aSMichael Halcrow 		(*key_tfm) = tmp_tfm;
1831f4aad16aSMichael Halcrow 	if (!tmp_tfm) {
1832f4aad16aSMichael Halcrow 		rc = -ENOMEM;
1833f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to allocate from "
1834f4aad16aSMichael Halcrow 		       "ecryptfs_key_tfm_cache\n");
1835f4aad16aSMichael Halcrow 		goto out;
1836f4aad16aSMichael Halcrow 	}
1837f4aad16aSMichael Halcrow 	mutex_init(&tmp_tfm->key_tfm_mutex);
1838f4aad16aSMichael Halcrow 	strncpy(tmp_tfm->cipher_name, cipher_name,
1839f4aad16aSMichael Halcrow 		ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1840b8862906SEric Sandeen 	tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
1841f4aad16aSMichael Halcrow 	tmp_tfm->key_size = key_size;
18425dda6992SMichael Halcrow 	rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1843f4aad16aSMichael Halcrow 					 tmp_tfm->cipher_name,
18445dda6992SMichael Halcrow 					 &tmp_tfm->key_size);
18455dda6992SMichael Halcrow 	if (rc) {
1846f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to initialize key TFM "
1847f4aad16aSMichael Halcrow 		       "cipher with name = [%s]; rc = [%d]\n",
1848f4aad16aSMichael Halcrow 		       tmp_tfm->cipher_name, rc);
1849f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1850f4aad16aSMichael Halcrow 		if (key_tfm != NULL)
1851f4aad16aSMichael Halcrow 			(*key_tfm) = NULL;
1852f4aad16aSMichael Halcrow 		goto out;
1853f4aad16aSMichael Halcrow 	}
1854f4aad16aSMichael Halcrow 	list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1855f4aad16aSMichael Halcrow out:
1856f4aad16aSMichael Halcrow 	return rc;
1857f4aad16aSMichael Halcrow }
1858f4aad16aSMichael Halcrow 
1859af440f52SEric Sandeen /**
1860af440f52SEric Sandeen  * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
1861af440f52SEric Sandeen  * @cipher_name: the name of the cipher to search for
1862af440f52SEric Sandeen  * @key_tfm: set to corresponding tfm if found
1863af440f52SEric Sandeen  *
1864af440f52SEric Sandeen  * Searches for cached key_tfm matching @cipher_name
1865af440f52SEric Sandeen  * Must be called with &key_tfm_list_mutex held
1866af440f52SEric Sandeen  * Returns 1 if found, with @key_tfm set
1867af440f52SEric Sandeen  * Returns 0 if not found, with @key_tfm set to NULL
1868af440f52SEric Sandeen  */
1869af440f52SEric Sandeen int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1870af440f52SEric Sandeen {
1871af440f52SEric Sandeen 	struct ecryptfs_key_tfm *tmp_key_tfm;
1872af440f52SEric Sandeen 
1873af440f52SEric Sandeen 	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1874af440f52SEric Sandeen 
1875af440f52SEric Sandeen 	list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
1876af440f52SEric Sandeen 		if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
1877af440f52SEric Sandeen 			if (key_tfm)
1878af440f52SEric Sandeen 				(*key_tfm) = tmp_key_tfm;
1879af440f52SEric Sandeen 			return 1;
1880af440f52SEric Sandeen 		}
1881af440f52SEric Sandeen 	}
1882af440f52SEric Sandeen 	if (key_tfm)
1883af440f52SEric Sandeen 		(*key_tfm) = NULL;
1884af440f52SEric Sandeen 	return 0;
1885af440f52SEric Sandeen }
1886af440f52SEric Sandeen 
1887af440f52SEric Sandeen /**
1888af440f52SEric Sandeen  * ecryptfs_get_tfm_and_mutex_for_cipher_name
1889af440f52SEric Sandeen  *
1890af440f52SEric Sandeen  * @tfm: set to cached tfm found, or new tfm created
1891af440f52SEric Sandeen  * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
1892af440f52SEric Sandeen  * @cipher_name: the name of the cipher to search for and/or add
1893af440f52SEric Sandeen  *
1894af440f52SEric Sandeen  * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
1895af440f52SEric Sandeen  * Searches for cached item first, and creates new if not found.
1896af440f52SEric Sandeen  * Returns 0 on success, non-zero if adding new cipher failed
1897af440f52SEric Sandeen  */
1898f4aad16aSMichael Halcrow int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1899f4aad16aSMichael Halcrow 					       struct mutex **tfm_mutex,
1900f4aad16aSMichael Halcrow 					       char *cipher_name)
1901f4aad16aSMichael Halcrow {
1902f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm;
1903f4aad16aSMichael Halcrow 	int rc = 0;
1904f4aad16aSMichael Halcrow 
1905f4aad16aSMichael Halcrow 	(*tfm) = NULL;
1906f4aad16aSMichael Halcrow 	(*tfm_mutex) = NULL;
1907af440f52SEric Sandeen 
1908f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1909af440f52SEric Sandeen 	if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
19105dda6992SMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
19115dda6992SMichael Halcrow 		if (rc) {
1912af440f52SEric Sandeen 			printk(KERN_ERR "Error adding new key_tfm to list; "
1913af440f52SEric Sandeen 					"rc = [%d]\n", rc);
1914f4aad16aSMichael Halcrow 			goto out;
1915f4aad16aSMichael Halcrow 		}
1916af440f52SEric Sandeen 	}
1917f4aad16aSMichael Halcrow 	(*tfm) = key_tfm->key_tfm;
1918f4aad16aSMichael Halcrow 	(*tfm_mutex) = &key_tfm->key_tfm_mutex;
1919f4aad16aSMichael Halcrow out:
192071fd5179SCyrill Gorcunov 	mutex_unlock(&key_tfm_list_mutex);
1921f4aad16aSMichael Halcrow 	return rc;
1922f4aad16aSMichael Halcrow }
192351ca58dcSMichael Halcrow 
192451ca58dcSMichael Halcrow /* 64 characters forming a 6-bit target field */
192551ca58dcSMichael Halcrow static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
192651ca58dcSMichael Halcrow 						 "EFGHIJKLMNOPQRST"
192751ca58dcSMichael Halcrow 						 "UVWXYZabcdefghij"
192851ca58dcSMichael Halcrow 						 "klmnopqrstuvwxyz");
192951ca58dcSMichael Halcrow 
193051ca58dcSMichael Halcrow /* We could either offset on every reverse map or just pad some 0x00's
193151ca58dcSMichael Halcrow  * at the front here */
193271c11c37SMichael Halcrow static const unsigned char filename_rev_map[] = {
193351ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
193451ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
193551ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
193651ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
193751ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
193851ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
193951ca58dcSMichael Halcrow 	0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
194051ca58dcSMichael Halcrow 	0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
194151ca58dcSMichael Halcrow 	0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
194251ca58dcSMichael Halcrow 	0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
194351ca58dcSMichael Halcrow 	0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
194451ca58dcSMichael Halcrow 	0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
194551ca58dcSMichael Halcrow 	0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
194651ca58dcSMichael Halcrow 	0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
194751ca58dcSMichael Halcrow 	0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
194851ca58dcSMichael Halcrow 	0x3D, 0x3E, 0x3F
194951ca58dcSMichael Halcrow };
195051ca58dcSMichael Halcrow 
195151ca58dcSMichael Halcrow /**
195251ca58dcSMichael Halcrow  * ecryptfs_encode_for_filename
195351ca58dcSMichael Halcrow  * @dst: Destination location for encoded filename
195451ca58dcSMichael Halcrow  * @dst_size: Size of the encoded filename in bytes
195551ca58dcSMichael Halcrow  * @src: Source location for the filename to encode
195651ca58dcSMichael Halcrow  * @src_size: Size of the source in bytes
195751ca58dcSMichael Halcrow  */
195851ca58dcSMichael Halcrow void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
195951ca58dcSMichael Halcrow 				  unsigned char *src, size_t src_size)
196051ca58dcSMichael Halcrow {
196151ca58dcSMichael Halcrow 	size_t num_blocks;
196251ca58dcSMichael Halcrow 	size_t block_num = 0;
196351ca58dcSMichael Halcrow 	size_t dst_offset = 0;
196451ca58dcSMichael Halcrow 	unsigned char last_block[3];
196551ca58dcSMichael Halcrow 
196651ca58dcSMichael Halcrow 	if (src_size == 0) {
196751ca58dcSMichael Halcrow 		(*dst_size) = 0;
196851ca58dcSMichael Halcrow 		goto out;
196951ca58dcSMichael Halcrow 	}
197051ca58dcSMichael Halcrow 	num_blocks = (src_size / 3);
197151ca58dcSMichael Halcrow 	if ((src_size % 3) == 0) {
197251ca58dcSMichael Halcrow 		memcpy(last_block, (&src[src_size - 3]), 3);
197351ca58dcSMichael Halcrow 	} else {
197451ca58dcSMichael Halcrow 		num_blocks++;
197551ca58dcSMichael Halcrow 		last_block[2] = 0x00;
197651ca58dcSMichael Halcrow 		switch (src_size % 3) {
197751ca58dcSMichael Halcrow 		case 1:
197851ca58dcSMichael Halcrow 			last_block[0] = src[src_size - 1];
197951ca58dcSMichael Halcrow 			last_block[1] = 0x00;
198051ca58dcSMichael Halcrow 			break;
198151ca58dcSMichael Halcrow 		case 2:
198251ca58dcSMichael Halcrow 			last_block[0] = src[src_size - 2];
198351ca58dcSMichael Halcrow 			last_block[1] = src[src_size - 1];
198451ca58dcSMichael Halcrow 		}
198551ca58dcSMichael Halcrow 	}
198651ca58dcSMichael Halcrow 	(*dst_size) = (num_blocks * 4);
198751ca58dcSMichael Halcrow 	if (!dst)
198851ca58dcSMichael Halcrow 		goto out;
198951ca58dcSMichael Halcrow 	while (block_num < num_blocks) {
199051ca58dcSMichael Halcrow 		unsigned char *src_block;
199151ca58dcSMichael Halcrow 		unsigned char dst_block[4];
199251ca58dcSMichael Halcrow 
199351ca58dcSMichael Halcrow 		if (block_num == (num_blocks - 1))
199451ca58dcSMichael Halcrow 			src_block = last_block;
199551ca58dcSMichael Halcrow 		else
199651ca58dcSMichael Halcrow 			src_block = &src[block_num * 3];
199751ca58dcSMichael Halcrow 		dst_block[0] = ((src_block[0] >> 2) & 0x3F);
199851ca58dcSMichael Halcrow 		dst_block[1] = (((src_block[0] << 4) & 0x30)
199951ca58dcSMichael Halcrow 				| ((src_block[1] >> 4) & 0x0F));
200051ca58dcSMichael Halcrow 		dst_block[2] = (((src_block[1] << 2) & 0x3C)
200151ca58dcSMichael Halcrow 				| ((src_block[2] >> 6) & 0x03));
200251ca58dcSMichael Halcrow 		dst_block[3] = (src_block[2] & 0x3F);
200351ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[0]];
200451ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[1]];
200551ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[2]];
200651ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[3]];
200751ca58dcSMichael Halcrow 		block_num++;
200851ca58dcSMichael Halcrow 	}
200951ca58dcSMichael Halcrow out:
201051ca58dcSMichael Halcrow 	return;
201151ca58dcSMichael Halcrow }
201251ca58dcSMichael Halcrow 
201371c11c37SMichael Halcrow /**
201471c11c37SMichael Halcrow  * ecryptfs_decode_from_filename
201571c11c37SMichael Halcrow  * @dst: If NULL, this function only sets @dst_size and returns. If
201671c11c37SMichael Halcrow  *       non-NULL, this function decodes the encoded octets in @src
201771c11c37SMichael Halcrow  *       into the memory that @dst points to.
201871c11c37SMichael Halcrow  * @dst_size: Set to the size of the decoded string.
201971c11c37SMichael Halcrow  * @src: The encoded set of octets to decode.
202071c11c37SMichael Halcrow  * @src_size: The size of the encoded set of octets to decode.
202171c11c37SMichael Halcrow  */
202271c11c37SMichael Halcrow static void
202371c11c37SMichael Halcrow ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
202451ca58dcSMichael Halcrow 			      const unsigned char *src, size_t src_size)
202551ca58dcSMichael Halcrow {
202651ca58dcSMichael Halcrow 	u8 current_bit_offset = 0;
202751ca58dcSMichael Halcrow 	size_t src_byte_offset = 0;
202851ca58dcSMichael Halcrow 	size_t dst_byte_offset = 0;
202951ca58dcSMichael Halcrow 
203051ca58dcSMichael Halcrow 	if (dst == NULL) {
203171c11c37SMichael Halcrow 		/* Not exact; conservatively long. Every block of 4
203271c11c37SMichael Halcrow 		 * encoded characters decodes into a block of 3
203371c11c37SMichael Halcrow 		 * decoded characters. This segment of code provides
203471c11c37SMichael Halcrow 		 * the caller with the maximum amount of allocated
203571c11c37SMichael Halcrow 		 * space that @dst will need to point to in a
203671c11c37SMichael Halcrow 		 * subsequent call. */
203751ca58dcSMichael Halcrow 		(*dst_size) = (((src_size + 1) * 3) / 4);
203851ca58dcSMichael Halcrow 		goto out;
203951ca58dcSMichael Halcrow 	}
204051ca58dcSMichael Halcrow 	while (src_byte_offset < src_size) {
204151ca58dcSMichael Halcrow 		unsigned char src_byte =
204251ca58dcSMichael Halcrow 				filename_rev_map[(int)src[src_byte_offset]];
204351ca58dcSMichael Halcrow 
204451ca58dcSMichael Halcrow 		switch (current_bit_offset) {
204551ca58dcSMichael Halcrow 		case 0:
204651ca58dcSMichael Halcrow 			dst[dst_byte_offset] = (src_byte << 2);
204751ca58dcSMichael Halcrow 			current_bit_offset = 6;
204851ca58dcSMichael Halcrow 			break;
204951ca58dcSMichael Halcrow 		case 6:
205051ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte >> 4);
205151ca58dcSMichael Halcrow 			dst[dst_byte_offset] = ((src_byte & 0xF)
205251ca58dcSMichael Halcrow 						 << 4);
205351ca58dcSMichael Halcrow 			current_bit_offset = 4;
205451ca58dcSMichael Halcrow 			break;
205551ca58dcSMichael Halcrow 		case 4:
205651ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte >> 2);
205751ca58dcSMichael Halcrow 			dst[dst_byte_offset] = (src_byte << 6);
205851ca58dcSMichael Halcrow 			current_bit_offset = 2;
205951ca58dcSMichael Halcrow 			break;
206051ca58dcSMichael Halcrow 		case 2:
206151ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte);
206251ca58dcSMichael Halcrow 			dst[dst_byte_offset] = 0;
206351ca58dcSMichael Halcrow 			current_bit_offset = 0;
206451ca58dcSMichael Halcrow 			break;
206551ca58dcSMichael Halcrow 		}
206651ca58dcSMichael Halcrow 		src_byte_offset++;
206751ca58dcSMichael Halcrow 	}
206851ca58dcSMichael Halcrow 	(*dst_size) = dst_byte_offset;
206951ca58dcSMichael Halcrow out:
207071c11c37SMichael Halcrow 	return;
207151ca58dcSMichael Halcrow }
207251ca58dcSMichael Halcrow 
207351ca58dcSMichael Halcrow /**
207451ca58dcSMichael Halcrow  * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
207551ca58dcSMichael Halcrow  * @crypt_stat: The crypt_stat struct associated with the file anem to encode
207651ca58dcSMichael Halcrow  * @name: The plaintext name
207751ca58dcSMichael Halcrow  * @length: The length of the plaintext
207851ca58dcSMichael Halcrow  * @encoded_name: The encypted name
207951ca58dcSMichael Halcrow  *
208051ca58dcSMichael Halcrow  * Encrypts and encodes a filename into something that constitutes a
208151ca58dcSMichael Halcrow  * valid filename for a filesystem, with printable characters.
208251ca58dcSMichael Halcrow  *
208351ca58dcSMichael Halcrow  * We assume that we have a properly initialized crypto context,
208451ca58dcSMichael Halcrow  * pointed to by crypt_stat->tfm.
208551ca58dcSMichael Halcrow  *
208651ca58dcSMichael Halcrow  * Returns zero on success; non-zero on otherwise
208751ca58dcSMichael Halcrow  */
208851ca58dcSMichael Halcrow int ecryptfs_encrypt_and_encode_filename(
208951ca58dcSMichael Halcrow 	char **encoded_name,
209051ca58dcSMichael Halcrow 	size_t *encoded_name_size,
209151ca58dcSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
209251ca58dcSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
209351ca58dcSMichael Halcrow 	const char *name, size_t name_size)
209451ca58dcSMichael Halcrow {
209551ca58dcSMichael Halcrow 	size_t encoded_name_no_prefix_size;
209651ca58dcSMichael Halcrow 	int rc = 0;
209751ca58dcSMichael Halcrow 
209851ca58dcSMichael Halcrow 	(*encoded_name) = NULL;
209951ca58dcSMichael Halcrow 	(*encoded_name_size) = 0;
210051ca58dcSMichael Halcrow 	if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES))
210151ca58dcSMichael Halcrow 	    || (mount_crypt_stat && (mount_crypt_stat->flags
210251ca58dcSMichael Halcrow 				     & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) {
210351ca58dcSMichael Halcrow 		struct ecryptfs_filename *filename;
210451ca58dcSMichael Halcrow 
210551ca58dcSMichael Halcrow 		filename = kzalloc(sizeof(*filename), GFP_KERNEL);
210651ca58dcSMichael Halcrow 		if (!filename) {
210751ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
2108a8f12864SMichael Halcrow 			       "to kzalloc [%zd] bytes\n", __func__,
210951ca58dcSMichael Halcrow 			       sizeof(*filename));
211051ca58dcSMichael Halcrow 			rc = -ENOMEM;
211151ca58dcSMichael Halcrow 			goto out;
211251ca58dcSMichael Halcrow 		}
211351ca58dcSMichael Halcrow 		filename->filename = (char *)name;
211451ca58dcSMichael Halcrow 		filename->filename_size = name_size;
211551ca58dcSMichael Halcrow 		rc = ecryptfs_encrypt_filename(filename, crypt_stat,
211651ca58dcSMichael Halcrow 					       mount_crypt_stat);
211751ca58dcSMichael Halcrow 		if (rc) {
211851ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encrypt "
211951ca58dcSMichael Halcrow 			       "filename; rc = [%d]\n", __func__, rc);
212051ca58dcSMichael Halcrow 			kfree(filename);
212151ca58dcSMichael Halcrow 			goto out;
212251ca58dcSMichael Halcrow 		}
212351ca58dcSMichael Halcrow 		ecryptfs_encode_for_filename(
212451ca58dcSMichael Halcrow 			NULL, &encoded_name_no_prefix_size,
212551ca58dcSMichael Halcrow 			filename->encrypted_filename,
212651ca58dcSMichael Halcrow 			filename->encrypted_filename_size);
212751ca58dcSMichael Halcrow 		if ((crypt_stat && (crypt_stat->flags
212851ca58dcSMichael Halcrow 				    & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
212951ca58dcSMichael Halcrow 		    || (mount_crypt_stat
213051ca58dcSMichael Halcrow 			&& (mount_crypt_stat->flags
213151ca58dcSMichael Halcrow 			    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)))
213251ca58dcSMichael Halcrow 			(*encoded_name_size) =
213351ca58dcSMichael Halcrow 				(ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
213451ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
213551ca58dcSMichael Halcrow 		else
213651ca58dcSMichael Halcrow 			(*encoded_name_size) =
213751ca58dcSMichael Halcrow 				(ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
213851ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
213951ca58dcSMichael Halcrow 		(*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
214051ca58dcSMichael Halcrow 		if (!(*encoded_name)) {
214151ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
2142a8f12864SMichael Halcrow 			       "to kzalloc [%zd] bytes\n", __func__,
214351ca58dcSMichael Halcrow 			       (*encoded_name_size));
214451ca58dcSMichael Halcrow 			rc = -ENOMEM;
214551ca58dcSMichael Halcrow 			kfree(filename->encrypted_filename);
214651ca58dcSMichael Halcrow 			kfree(filename);
214751ca58dcSMichael Halcrow 			goto out;
214851ca58dcSMichael Halcrow 		}
214951ca58dcSMichael Halcrow 		if ((crypt_stat && (crypt_stat->flags
215051ca58dcSMichael Halcrow 				    & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
215151ca58dcSMichael Halcrow 		    || (mount_crypt_stat
215251ca58dcSMichael Halcrow 			&& (mount_crypt_stat->flags
215351ca58dcSMichael Halcrow 			    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
215451ca58dcSMichael Halcrow 			memcpy((*encoded_name),
215551ca58dcSMichael Halcrow 			       ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
215651ca58dcSMichael Halcrow 			       ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
215751ca58dcSMichael Halcrow 			ecryptfs_encode_for_filename(
215851ca58dcSMichael Halcrow 			    ((*encoded_name)
215951ca58dcSMichael Halcrow 			     + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
216051ca58dcSMichael Halcrow 			    &encoded_name_no_prefix_size,
216151ca58dcSMichael Halcrow 			    filename->encrypted_filename,
216251ca58dcSMichael Halcrow 			    filename->encrypted_filename_size);
216351ca58dcSMichael Halcrow 			(*encoded_name_size) =
216451ca58dcSMichael Halcrow 				(ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
216551ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
216651ca58dcSMichael Halcrow 			(*encoded_name)[(*encoded_name_size)] = '\0';
216751ca58dcSMichael Halcrow 			(*encoded_name_size)++;
216851ca58dcSMichael Halcrow 		} else {
216951ca58dcSMichael Halcrow 			rc = -ENOTSUPP;
217051ca58dcSMichael Halcrow 		}
217151ca58dcSMichael Halcrow 		if (rc) {
217251ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encode "
217351ca58dcSMichael Halcrow 			       "encrypted filename; rc = [%d]\n", __func__,
217451ca58dcSMichael Halcrow 			       rc);
217551ca58dcSMichael Halcrow 			kfree((*encoded_name));
217651ca58dcSMichael Halcrow 			(*encoded_name) = NULL;
217751ca58dcSMichael Halcrow 			(*encoded_name_size) = 0;
217851ca58dcSMichael Halcrow 		}
217951ca58dcSMichael Halcrow 		kfree(filename->encrypted_filename);
218051ca58dcSMichael Halcrow 		kfree(filename);
218151ca58dcSMichael Halcrow 	} else {
218251ca58dcSMichael Halcrow 		rc = ecryptfs_copy_filename(encoded_name,
218351ca58dcSMichael Halcrow 					    encoded_name_size,
218451ca58dcSMichael Halcrow 					    name, name_size);
218551ca58dcSMichael Halcrow 	}
218651ca58dcSMichael Halcrow out:
218751ca58dcSMichael Halcrow 	return rc;
218851ca58dcSMichael Halcrow }
218951ca58dcSMichael Halcrow 
219051ca58dcSMichael Halcrow /**
219151ca58dcSMichael Halcrow  * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
219251ca58dcSMichael Halcrow  * @plaintext_name: The plaintext name
219351ca58dcSMichael Halcrow  * @plaintext_name_size: The plaintext name size
219451ca58dcSMichael Halcrow  * @ecryptfs_dir_dentry: eCryptfs directory dentry
219551ca58dcSMichael Halcrow  * @name: The filename in cipher text
219651ca58dcSMichael Halcrow  * @name_size: The cipher text name size
219751ca58dcSMichael Halcrow  *
219851ca58dcSMichael Halcrow  * Decrypts and decodes the filename.
219951ca58dcSMichael Halcrow  *
220051ca58dcSMichael Halcrow  * Returns zero on error; non-zero otherwise
220151ca58dcSMichael Halcrow  */
220251ca58dcSMichael Halcrow int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
220351ca58dcSMichael Halcrow 					 size_t *plaintext_name_size,
220451ca58dcSMichael Halcrow 					 struct dentry *ecryptfs_dir_dentry,
220551ca58dcSMichael Halcrow 					 const char *name, size_t name_size)
220651ca58dcSMichael Halcrow {
22072aac0cf8STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
22082aac0cf8STyler Hicks 		&ecryptfs_superblock_to_private(
22092aac0cf8STyler Hicks 			ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
221051ca58dcSMichael Halcrow 	char *decoded_name;
221151ca58dcSMichael Halcrow 	size_t decoded_name_size;
221251ca58dcSMichael Halcrow 	size_t packet_size;
221351ca58dcSMichael Halcrow 	int rc = 0;
221451ca58dcSMichael Halcrow 
22152aac0cf8STyler Hicks 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
22162aac0cf8STyler Hicks 	    && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
22172aac0cf8STyler Hicks 	    && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
221851ca58dcSMichael Halcrow 	    && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
221951ca58dcSMichael Halcrow 			ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
222051ca58dcSMichael Halcrow 		const char *orig_name = name;
222151ca58dcSMichael Halcrow 		size_t orig_name_size = name_size;
222251ca58dcSMichael Halcrow 
222351ca58dcSMichael Halcrow 		name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
222451ca58dcSMichael Halcrow 		name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
222571c11c37SMichael Halcrow 		ecryptfs_decode_from_filename(NULL, &decoded_name_size,
222651ca58dcSMichael Halcrow 					      name, name_size);
222751ca58dcSMichael Halcrow 		decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
222851ca58dcSMichael Halcrow 		if (!decoded_name) {
222951ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
2230df261c52SMichael Halcrow 			       "to kmalloc [%zd] bytes\n", __func__,
223151ca58dcSMichael Halcrow 			       decoded_name_size);
223251ca58dcSMichael Halcrow 			rc = -ENOMEM;
223351ca58dcSMichael Halcrow 			goto out;
223451ca58dcSMichael Halcrow 		}
223571c11c37SMichael Halcrow 		ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
223651ca58dcSMichael Halcrow 					      name, name_size);
223751ca58dcSMichael Halcrow 		rc = ecryptfs_parse_tag_70_packet(plaintext_name,
223851ca58dcSMichael Halcrow 						  plaintext_name_size,
223951ca58dcSMichael Halcrow 						  &packet_size,
224051ca58dcSMichael Halcrow 						  mount_crypt_stat,
224151ca58dcSMichael Halcrow 						  decoded_name,
224251ca58dcSMichael Halcrow 						  decoded_name_size);
224351ca58dcSMichael Halcrow 		if (rc) {
224451ca58dcSMichael Halcrow 			printk(KERN_INFO "%s: Could not parse tag 70 packet "
224551ca58dcSMichael Halcrow 			       "from filename; copying through filename "
224651ca58dcSMichael Halcrow 			       "as-is\n", __func__);
224751ca58dcSMichael Halcrow 			rc = ecryptfs_copy_filename(plaintext_name,
224851ca58dcSMichael Halcrow 						    plaintext_name_size,
224951ca58dcSMichael Halcrow 						    orig_name, orig_name_size);
225051ca58dcSMichael Halcrow 			goto out_free;
225151ca58dcSMichael Halcrow 		}
225251ca58dcSMichael Halcrow 	} else {
225351ca58dcSMichael Halcrow 		rc = ecryptfs_copy_filename(plaintext_name,
225451ca58dcSMichael Halcrow 					    plaintext_name_size,
225551ca58dcSMichael Halcrow 					    name, name_size);
225651ca58dcSMichael Halcrow 		goto out;
225751ca58dcSMichael Halcrow 	}
225851ca58dcSMichael Halcrow out_free:
225951ca58dcSMichael Halcrow 	kfree(decoded_name);
226051ca58dcSMichael Halcrow out:
226151ca58dcSMichael Halcrow 	return rc;
226251ca58dcSMichael Halcrow }
2263