xref: /openbmc/linux/fs/ecryptfs/crypto.c (revision ecbdc93639f69c1f237ccce6a9aaff1e83f1182f)
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>
36237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
37237fead6SMichael Halcrow 
38237fead6SMichael Halcrow static int
39237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
40237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
41237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
42237fead6SMichael Halcrow 			     unsigned char *iv);
43237fead6SMichael Halcrow static int
44237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
45237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
46237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
47237fead6SMichael Halcrow 			     unsigned char *iv);
48237fead6SMichael Halcrow 
49237fead6SMichael Halcrow /**
50237fead6SMichael Halcrow  * ecryptfs_to_hex
51237fead6SMichael Halcrow  * @dst: Buffer to take hex character representation of contents of
52237fead6SMichael Halcrow  *       src; must be at least of size (src_size * 2)
53237fead6SMichael Halcrow  * @src: Buffer to be converted to a hex string respresentation
54237fead6SMichael Halcrow  * @src_size: number of bytes to convert
55237fead6SMichael Halcrow  */
56237fead6SMichael Halcrow void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
57237fead6SMichael Halcrow {
58237fead6SMichael Halcrow 	int x;
59237fead6SMichael Halcrow 
60237fead6SMichael Halcrow 	for (x = 0; x < src_size; x++)
61237fead6SMichael Halcrow 		sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
62237fead6SMichael Halcrow }
63237fead6SMichael Halcrow 
64237fead6SMichael Halcrow /**
65237fead6SMichael Halcrow  * ecryptfs_from_hex
66237fead6SMichael Halcrow  * @dst: Buffer to take the bytes from src hex; must be at least of
67237fead6SMichael Halcrow  *       size (src_size / 2)
68237fead6SMichael Halcrow  * @src: Buffer to be converted from a hex string respresentation to raw value
69237fead6SMichael Halcrow  * @dst_size: size of dst buffer, or number of hex characters pairs to convert
70237fead6SMichael Halcrow  */
71237fead6SMichael Halcrow void ecryptfs_from_hex(char *dst, char *src, int dst_size)
72237fead6SMichael Halcrow {
73237fead6SMichael Halcrow 	int x;
74237fead6SMichael Halcrow 	char tmp[3] = { 0, };
75237fead6SMichael Halcrow 
76237fead6SMichael Halcrow 	for (x = 0; x < dst_size; x++) {
77237fead6SMichael Halcrow 		tmp[0] = src[x * 2];
78237fead6SMichael Halcrow 		tmp[1] = src[x * 2 + 1];
79237fead6SMichael Halcrow 		dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
80237fead6SMichael Halcrow 	}
81237fead6SMichael Halcrow }
82237fead6SMichael Halcrow 
83237fead6SMichael Halcrow /**
84237fead6SMichael Halcrow  * ecryptfs_calculate_md5 - calculates the md5 of @src
85237fead6SMichael Halcrow  * @dst: Pointer to 16 bytes of allocated memory
86237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
87237fead6SMichael Halcrow  * @src: Data to be md5'd
88237fead6SMichael Halcrow  * @len: Length of @src
89237fead6SMichael Halcrow  *
90237fead6SMichael Halcrow  * Uses the allocated crypto context that crypt_stat references to
91237fead6SMichael Halcrow  * generate the MD5 sum of the contents of src.
92237fead6SMichael Halcrow  */
93237fead6SMichael Halcrow static int ecryptfs_calculate_md5(char *dst,
94237fead6SMichael Halcrow 				  struct ecryptfs_crypt_stat *crypt_stat,
95237fead6SMichael Halcrow 				  char *src, int len)
96237fead6SMichael Halcrow {
97237fead6SMichael Halcrow 	struct scatterlist sg;
98565d9724SMichael Halcrow 	struct hash_desc desc = {
99565d9724SMichael Halcrow 		.tfm = crypt_stat->hash_tfm,
100565d9724SMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
101565d9724SMichael Halcrow 	};
102565d9724SMichael Halcrow 	int rc = 0;
103237fead6SMichael Halcrow 
104565d9724SMichael Halcrow 	mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
105237fead6SMichael Halcrow 	sg_init_one(&sg, (u8 *)src, len);
106565d9724SMichael Halcrow 	if (!desc.tfm) {
107565d9724SMichael Halcrow 		desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
108565d9724SMichael Halcrow 					     CRYPTO_ALG_ASYNC);
109565d9724SMichael Halcrow 		if (IS_ERR(desc.tfm)) {
110565d9724SMichael Halcrow 			rc = PTR_ERR(desc.tfm);
111237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
112565d9724SMichael Halcrow 					"allocate crypto context; rc = [%d]\n",
113565d9724SMichael Halcrow 					rc);
114237fead6SMichael Halcrow 			goto out;
115237fead6SMichael Halcrow 		}
116565d9724SMichael Halcrow 		crypt_stat->hash_tfm = desc.tfm;
117237fead6SMichael Halcrow 	}
118565d9724SMichael Halcrow 	crypto_hash_init(&desc);
119565d9724SMichael Halcrow 	crypto_hash_update(&desc, &sg, len);
120565d9724SMichael Halcrow 	crypto_hash_final(&desc, dst);
121565d9724SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
122237fead6SMichael Halcrow out:
123237fead6SMichael Halcrow 	return rc;
124237fead6SMichael Halcrow }
125237fead6SMichael Halcrow 
126cd9d67dfSMichael Halcrow static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
1278bba066fSMichael Halcrow 						  char *cipher_name,
1288bba066fSMichael Halcrow 						  char *chaining_modifier)
1298bba066fSMichael Halcrow {
1308bba066fSMichael Halcrow 	int cipher_name_len = strlen(cipher_name);
1318bba066fSMichael Halcrow 	int chaining_modifier_len = strlen(chaining_modifier);
1328bba066fSMichael Halcrow 	int algified_name_len;
1338bba066fSMichael Halcrow 	int rc;
1348bba066fSMichael Halcrow 
1358bba066fSMichael Halcrow 	algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
1368bba066fSMichael Halcrow 	(*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
1377bd473fcSMichael Halcrow 	if (!(*algified_name)) {
1388bba066fSMichael Halcrow 		rc = -ENOMEM;
1398bba066fSMichael Halcrow 		goto out;
1408bba066fSMichael Halcrow 	}
1418bba066fSMichael Halcrow 	snprintf((*algified_name), algified_name_len, "%s(%s)",
1428bba066fSMichael Halcrow 		 chaining_modifier, cipher_name);
1438bba066fSMichael Halcrow 	rc = 0;
1448bba066fSMichael Halcrow out:
1458bba066fSMichael Halcrow 	return rc;
1468bba066fSMichael Halcrow }
1478bba066fSMichael Halcrow 
148237fead6SMichael Halcrow /**
149237fead6SMichael Halcrow  * ecryptfs_derive_iv
150237fead6SMichael Halcrow  * @iv: destination for the derived iv vale
151237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
152d6a13c17SMichael Halcrow  * @offset: Offset of the extent whose IV we are to derive
153237fead6SMichael Halcrow  *
154237fead6SMichael Halcrow  * Generate the initialization vector from the given root IV and page
155237fead6SMichael Halcrow  * offset.
156237fead6SMichael Halcrow  *
157237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
158237fead6SMichael Halcrow  */
159237fead6SMichael Halcrow static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
160d6a13c17SMichael Halcrow 			      loff_t offset)
161237fead6SMichael Halcrow {
162237fead6SMichael Halcrow 	int rc = 0;
163237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
164237fead6SMichael Halcrow 	char src[ECRYPTFS_MAX_IV_BYTES + 16];
165237fead6SMichael Halcrow 
166237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
167237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "root iv:\n");
168237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
169237fead6SMichael Halcrow 	}
170237fead6SMichael Halcrow 	/* TODO: It is probably secure to just cast the least
171237fead6SMichael Halcrow 	 * significant bits of the root IV into an unsigned long and
172237fead6SMichael Halcrow 	 * add the offset to that rather than go through all this
173237fead6SMichael Halcrow 	 * hashing business. -Halcrow */
174237fead6SMichael Halcrow 	memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
175237fead6SMichael Halcrow 	memset((src + crypt_stat->iv_bytes), 0, 16);
176d6a13c17SMichael Halcrow 	snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
177237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
178237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "source:\n");
179237fead6SMichael Halcrow 		ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
180237fead6SMichael Halcrow 	}
181237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
182237fead6SMichael Halcrow 				    (crypt_stat->iv_bytes + 16));
183237fead6SMichael Halcrow 	if (rc) {
184237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
185237fead6SMichael Halcrow 				"MD5 while generating IV for a page\n");
186237fead6SMichael Halcrow 		goto out;
187237fead6SMichael Halcrow 	}
188237fead6SMichael Halcrow 	memcpy(iv, dst, crypt_stat->iv_bytes);
189237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
190237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
191237fead6SMichael Halcrow 		ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
192237fead6SMichael Halcrow 	}
193237fead6SMichael Halcrow out:
194237fead6SMichael Halcrow 	return rc;
195237fead6SMichael Halcrow }
196237fead6SMichael Halcrow 
197237fead6SMichael Halcrow /**
198237fead6SMichael Halcrow  * ecryptfs_init_crypt_stat
199237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
200237fead6SMichael Halcrow  *
201237fead6SMichael Halcrow  * Initialize the crypt_stat structure.
202237fead6SMichael Halcrow  */
203237fead6SMichael Halcrow void
204237fead6SMichael Halcrow ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
205237fead6SMichael Halcrow {
206237fead6SMichael Halcrow 	memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
207f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&crypt_stat->keysig_list);
208f4aad16aSMichael Halcrow 	mutex_init(&crypt_stat->keysig_list_mutex);
209237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_mutex);
210237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_tfm_mutex);
211565d9724SMichael Halcrow 	mutex_init(&crypt_stat->cs_hash_tfm_mutex);
212e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
213237fead6SMichael Halcrow }
214237fead6SMichael Halcrow 
215237fead6SMichael Halcrow /**
216fcd12835SMichael Halcrow  * ecryptfs_destroy_crypt_stat
217237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
218237fead6SMichael Halcrow  *
219237fead6SMichael Halcrow  * Releases all memory associated with a crypt_stat struct.
220237fead6SMichael Halcrow  */
221fcd12835SMichael Halcrow void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
222237fead6SMichael Halcrow {
223f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
224f4aad16aSMichael Halcrow 
225237fead6SMichael Halcrow 	if (crypt_stat->tfm)
2268bba066fSMichael Halcrow 		crypto_free_blkcipher(crypt_stat->tfm);
227565d9724SMichael Halcrow 	if (crypt_stat->hash_tfm)
228565d9724SMichael Halcrow 		crypto_free_hash(crypt_stat->hash_tfm);
229f4aad16aSMichael Halcrow 	mutex_lock(&crypt_stat->keysig_list_mutex);
230f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_sig, key_sig_tmp,
231f4aad16aSMichael Halcrow 				 &crypt_stat->keysig_list, crypt_stat_list) {
232f4aad16aSMichael Halcrow 		list_del(&key_sig->crypt_stat_list);
233f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
234f4aad16aSMichael Halcrow 	}
235f4aad16aSMichael Halcrow 	mutex_unlock(&crypt_stat->keysig_list_mutex);
236237fead6SMichael Halcrow 	memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
237237fead6SMichael Halcrow }
238237fead6SMichael Halcrow 
239fcd12835SMichael Halcrow void ecryptfs_destroy_mount_crypt_stat(
240237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
241237fead6SMichael Halcrow {
242f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
243f4aad16aSMichael Halcrow 
244f4aad16aSMichael Halcrow 	if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
245f4aad16aSMichael Halcrow 		return;
246f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
247f4aad16aSMichael Halcrow 	list_for_each_entry_safe(auth_tok, auth_tok_tmp,
248f4aad16aSMichael Halcrow 				 &mount_crypt_stat->global_auth_tok_list,
249f4aad16aSMichael Halcrow 				 mount_crypt_stat_list) {
250f4aad16aSMichael Halcrow 		list_del(&auth_tok->mount_crypt_stat_list);
251f4aad16aSMichael Halcrow 		mount_crypt_stat->num_global_auth_toks--;
252f4aad16aSMichael Halcrow 		if (auth_tok->global_auth_tok_key
253f4aad16aSMichael Halcrow 		    && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
254f4aad16aSMichael Halcrow 			key_put(auth_tok->global_auth_tok_key);
255f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
256f4aad16aSMichael Halcrow 	}
257f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
258237fead6SMichael Halcrow 	memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
259237fead6SMichael Halcrow }
260237fead6SMichael Halcrow 
261237fead6SMichael Halcrow /**
262237fead6SMichael Halcrow  * virt_to_scatterlist
263237fead6SMichael Halcrow  * @addr: Virtual address
264237fead6SMichael Halcrow  * @size: Size of data; should be an even multiple of the block size
265237fead6SMichael Halcrow  * @sg: Pointer to scatterlist array; set to NULL to obtain only
266237fead6SMichael Halcrow  *      the number of scatterlist structs required in array
267237fead6SMichael Halcrow  * @sg_size: Max array size
268237fead6SMichael Halcrow  *
269237fead6SMichael Halcrow  * Fills in a scatterlist array with page references for a passed
270237fead6SMichael Halcrow  * virtual address.
271237fead6SMichael Halcrow  *
272237fead6SMichael Halcrow  * Returns the number of scatterlist structs in array used
273237fead6SMichael Halcrow  */
274237fead6SMichael Halcrow int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
275237fead6SMichael Halcrow 			int sg_size)
276237fead6SMichael Halcrow {
277237fead6SMichael Halcrow 	int i = 0;
278237fead6SMichael Halcrow 	struct page *pg;
279237fead6SMichael Halcrow 	int offset;
280237fead6SMichael Halcrow 	int remainder_of_page;
281237fead6SMichael Halcrow 
282237fead6SMichael Halcrow 	while (size > 0 && i < sg_size) {
283237fead6SMichael Halcrow 		pg = virt_to_page(addr);
284237fead6SMichael Halcrow 		offset = offset_in_page(addr);
285237fead6SMichael Halcrow 		if (sg) {
286237fead6SMichael Halcrow 			sg[i].page = pg;
287237fead6SMichael Halcrow 			sg[i].offset = offset;
288237fead6SMichael Halcrow 		}
289237fead6SMichael Halcrow 		remainder_of_page = PAGE_CACHE_SIZE - offset;
290237fead6SMichael Halcrow 		if (size >= remainder_of_page) {
291237fead6SMichael Halcrow 			if (sg)
292237fead6SMichael Halcrow 				sg[i].length = remainder_of_page;
293237fead6SMichael Halcrow 			addr += remainder_of_page;
294237fead6SMichael Halcrow 			size -= remainder_of_page;
295237fead6SMichael Halcrow 		} else {
296237fead6SMichael Halcrow 			if (sg)
297237fead6SMichael Halcrow 				sg[i].length = size;
298237fead6SMichael Halcrow 			addr += size;
299237fead6SMichael Halcrow 			size = 0;
300237fead6SMichael Halcrow 		}
301237fead6SMichael Halcrow 		i++;
302237fead6SMichael Halcrow 	}
303237fead6SMichael Halcrow 	if (size > 0)
304237fead6SMichael Halcrow 		return -ENOMEM;
305237fead6SMichael Halcrow 	return i;
306237fead6SMichael Halcrow }
307237fead6SMichael Halcrow 
308237fead6SMichael Halcrow /**
309237fead6SMichael Halcrow  * encrypt_scatterlist
310237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
311237fead6SMichael Halcrow  * @dest_sg: Destination of encrypted data
312237fead6SMichael Halcrow  * @src_sg: Data to be encrypted
313237fead6SMichael Halcrow  * @size: Length of data to be encrypted
314237fead6SMichael Halcrow  * @iv: iv to use during encryption
315237fead6SMichael Halcrow  *
316237fead6SMichael Halcrow  * Returns the number of bytes encrypted; negative value on error
317237fead6SMichael Halcrow  */
318237fead6SMichael Halcrow static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
319237fead6SMichael Halcrow 			       struct scatterlist *dest_sg,
320237fead6SMichael Halcrow 			       struct scatterlist *src_sg, int size,
321237fead6SMichael Halcrow 			       unsigned char *iv)
322237fead6SMichael Halcrow {
3238bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
3248bba066fSMichael Halcrow 		.tfm = crypt_stat->tfm,
3258bba066fSMichael Halcrow 		.info = iv,
3268bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
3278bba066fSMichael Halcrow 	};
328237fead6SMichael Halcrow 	int rc = 0;
329237fead6SMichael Halcrow 
330237fead6SMichael Halcrow 	BUG_ON(!crypt_stat || !crypt_stat->tfm
331e2bd99ecSMichael Halcrow 	       || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
332237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
333237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
334237fead6SMichael Halcrow 				crypt_stat->key_size);
335237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
336237fead6SMichael Halcrow 				  crypt_stat->key_size);
337237fead6SMichael Halcrow 	}
338237fead6SMichael Halcrow 	/* Consider doing this once, when the file is opened */
339237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
3408bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
341237fead6SMichael Halcrow 				     crypt_stat->key_size);
342237fead6SMichael Halcrow 	if (rc) {
343237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
344237fead6SMichael Halcrow 				rc);
345237fead6SMichael Halcrow 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
346237fead6SMichael Halcrow 		rc = -EINVAL;
347237fead6SMichael Halcrow 		goto out;
348237fead6SMichael Halcrow 	}
349237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
3508bba066fSMichael Halcrow 	crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
351237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
352237fead6SMichael Halcrow out:
353237fead6SMichael Halcrow 	return rc;
354237fead6SMichael Halcrow }
355237fead6SMichael Halcrow 
356237fead6SMichael Halcrow /**
3570216f7f7SMichael Halcrow  * ecryptfs_lower_offset_for_extent
358237fead6SMichael Halcrow  *
3590216f7f7SMichael Halcrow  * Convert an eCryptfs page index into a lower byte offset
360237fead6SMichael Halcrow  */
3610216f7f7SMichael Halcrow void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
3620216f7f7SMichael Halcrow 				      struct ecryptfs_crypt_stat *crypt_stat)
363237fead6SMichael Halcrow {
3640216f7f7SMichael Halcrow 	(*offset) = ((crypt_stat->extent_size
3650216f7f7SMichael Halcrow 		      * crypt_stat->num_header_extents_at_front)
3660216f7f7SMichael Halcrow 		     + (crypt_stat->extent_size * extent_num));
3670216f7f7SMichael Halcrow }
368237fead6SMichael Halcrow 
3690216f7f7SMichael Halcrow /**
3700216f7f7SMichael Halcrow  * ecryptfs_encrypt_extent
3710216f7f7SMichael Halcrow  * @enc_extent_page: Allocated page into which to encrypt the data in
3720216f7f7SMichael Halcrow  *                   @page
3730216f7f7SMichael Halcrow  * @crypt_stat: crypt_stat containing cryptographic context for the
3740216f7f7SMichael Halcrow  *              encryption operation
3750216f7f7SMichael Halcrow  * @page: Page containing plaintext data extent to encrypt
3760216f7f7SMichael Halcrow  * @extent_offset: Page extent offset for use in generating IV
3770216f7f7SMichael Halcrow  *
3780216f7f7SMichael Halcrow  * Encrypts one extent of data.
3790216f7f7SMichael Halcrow  *
3800216f7f7SMichael Halcrow  * Return zero on success; non-zero otherwise
3810216f7f7SMichael Halcrow  */
3820216f7f7SMichael Halcrow static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
3830216f7f7SMichael Halcrow 				   struct ecryptfs_crypt_stat *crypt_stat,
3840216f7f7SMichael Halcrow 				   struct page *page,
3850216f7f7SMichael Halcrow 				   unsigned long extent_offset)
3860216f7f7SMichael Halcrow {
387d6a13c17SMichael Halcrow 	loff_t extent_base;
3880216f7f7SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
3890216f7f7SMichael Halcrow 	int rc;
3900216f7f7SMichael Halcrow 
391d6a13c17SMichael Halcrow 	extent_base = (((loff_t)page->index)
3920216f7f7SMichael Halcrow 		       * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
393237fead6SMichael Halcrow 	rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
3940216f7f7SMichael Halcrow 				(extent_base + extent_offset));
395237fead6SMichael Halcrow 	if (rc) {
396237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to "
397237fead6SMichael Halcrow 				"derive IV for extent [0x%.16x]; "
3980216f7f7SMichael Halcrow 				"rc = [%d]\n", (extent_base + extent_offset),
3990216f7f7SMichael Halcrow 				rc);
400237fead6SMichael Halcrow 		goto out;
401237fead6SMichael Halcrow 	}
402237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
403237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
404237fead6SMichael Halcrow 				"with iv:\n");
405237fead6SMichael Halcrow 		ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
406237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
407237fead6SMichael Halcrow 				"encryption:\n");
408237fead6SMichael Halcrow 		ecryptfs_dump_hex((char *)
4090216f7f7SMichael Halcrow 				  (page_address(page)
4100216f7f7SMichael Halcrow 				   + (extent_offset * crypt_stat->extent_size)),
4110216f7f7SMichael Halcrow 				  8);
412237fead6SMichael Halcrow 	}
4130216f7f7SMichael Halcrow 	rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
4140216f7f7SMichael Halcrow 					  page, (extent_offset
4150216f7f7SMichael Halcrow 						 * crypt_stat->extent_size),
416237fead6SMichael Halcrow 					  crypt_stat->extent_size, extent_iv);
4170216f7f7SMichael Halcrow 	if (rc < 0) {
4180216f7f7SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to encrypt page with "
4190216f7f7SMichael Halcrow 		       "page->index = [%ld], extent_offset = [%ld]; "
4200216f7f7SMichael Halcrow 		       "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
4210216f7f7SMichael Halcrow 		       rc);
4220216f7f7SMichael Halcrow 		goto out;
4230216f7f7SMichael Halcrow 	}
4240216f7f7SMichael Halcrow 	rc = 0;
425237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
4260216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
4270216f7f7SMichael Halcrow 				"rc = [%d]\n", (extent_base + extent_offset),
4280216f7f7SMichael Halcrow 				rc);
429237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
430237fead6SMichael Halcrow 				"encryption:\n");
4310216f7f7SMichael Halcrow 		ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
432237fead6SMichael Halcrow 	}
4330216f7f7SMichael Halcrow out:
4340216f7f7SMichael Halcrow 	return rc;
4350216f7f7SMichael Halcrow }
4360216f7f7SMichael Halcrow 
4370216f7f7SMichael Halcrow /**
4380216f7f7SMichael Halcrow  * ecryptfs_encrypt_page
4390216f7f7SMichael Halcrow  * @page: Page mapped from the eCryptfs inode for the file; contains
4400216f7f7SMichael Halcrow  *        decrypted content that needs to be encrypted (to a temporary
4410216f7f7SMichael Halcrow  *        page; not in place) and written out to the lower file
4420216f7f7SMichael Halcrow  *
4430216f7f7SMichael Halcrow  * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
4440216f7f7SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
4450216f7f7SMichael Halcrow  * if the file was created on a machine with an 8K page size
4460216f7f7SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
4470216f7f7SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
4480216f7f7SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
4490216f7f7SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
4500216f7f7SMichael Halcrow  *
4510216f7f7SMichael Halcrow  * Returns zero on success; negative on error
4520216f7f7SMichael Halcrow  */
4530216f7f7SMichael Halcrow int ecryptfs_encrypt_page(struct page *page)
4540216f7f7SMichael Halcrow {
4550216f7f7SMichael Halcrow 	struct inode *ecryptfs_inode;
4560216f7f7SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
4570216f7f7SMichael Halcrow 	char *enc_extent_virt = NULL;
4580216f7f7SMichael Halcrow 	struct page *enc_extent_page;
4590216f7f7SMichael Halcrow 	loff_t extent_offset;
4600216f7f7SMichael Halcrow 	int rc = 0;
4610216f7f7SMichael Halcrow 
4620216f7f7SMichael Halcrow 	ecryptfs_inode = page->mapping->host;
4630216f7f7SMichael Halcrow 	crypt_stat =
4640216f7f7SMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
4650216f7f7SMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
4660216f7f7SMichael Halcrow 		rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
4670216f7f7SMichael Halcrow 						       0, PAGE_CACHE_SIZE);
4680216f7f7SMichael Halcrow 		if (rc)
4690216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to copy "
4700216f7f7SMichael Halcrow 			       "page at index [%ld]\n", __FUNCTION__,
4710216f7f7SMichael Halcrow 			       page->index);
4720216f7f7SMichael Halcrow 		goto out;
4730216f7f7SMichael Halcrow 	}
4740216f7f7SMichael Halcrow 	enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
4750216f7f7SMichael Halcrow 	if (!enc_extent_virt) {
4760216f7f7SMichael Halcrow 		rc = -ENOMEM;
4770216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error allocating memory for "
4780216f7f7SMichael Halcrow 				"encrypted extent\n");
4790216f7f7SMichael Halcrow 		goto out;
4800216f7f7SMichael Halcrow 	}
4810216f7f7SMichael Halcrow 	enc_extent_page = virt_to_page(enc_extent_virt);
4820216f7f7SMichael Halcrow 	for (extent_offset = 0;
4830216f7f7SMichael Halcrow 	     extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
4840216f7f7SMichael Halcrow 	     extent_offset++) {
4850216f7f7SMichael Halcrow 		loff_t offset;
4860216f7f7SMichael Halcrow 
4870216f7f7SMichael Halcrow 		rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
4880216f7f7SMichael Halcrow 					     extent_offset);
4890216f7f7SMichael Halcrow 		if (rc) {
4900216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting extent; "
4910216f7f7SMichael Halcrow 			       "rc = [%d]\n", __FUNCTION__, rc);
4920216f7f7SMichael Halcrow 			goto out;
4930216f7f7SMichael Halcrow 		}
4940216f7f7SMichael Halcrow 		ecryptfs_lower_offset_for_extent(
495d6a13c17SMichael Halcrow 			&offset, ((((loff_t)page->index)
496d6a13c17SMichael Halcrow 				   * (PAGE_CACHE_SIZE
4970216f7f7SMichael Halcrow 				      / crypt_stat->extent_size))
4980216f7f7SMichael Halcrow 				  + extent_offset), crypt_stat);
4990216f7f7SMichael Halcrow 		rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
5000216f7f7SMichael Halcrow 					  offset, crypt_stat->extent_size);
5010216f7f7SMichael Halcrow 		if (rc) {
5020216f7f7SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting "
5030216f7f7SMichael Halcrow 					"to write lower page; rc = [%d]"
5040216f7f7SMichael Halcrow 					"\n", rc);
5050216f7f7SMichael Halcrow 			goto out;
5060216f7f7SMichael Halcrow 		}
507237fead6SMichael Halcrow 		extent_offset++;
508237fead6SMichael Halcrow 	}
5090216f7f7SMichael Halcrow out:
5100216f7f7SMichael Halcrow 	kfree(enc_extent_virt);
5110216f7f7SMichael Halcrow 	return rc;
5120216f7f7SMichael Halcrow }
5130216f7f7SMichael Halcrow 
5140216f7f7SMichael Halcrow static int ecryptfs_decrypt_extent(struct page *page,
5150216f7f7SMichael Halcrow 				   struct ecryptfs_crypt_stat *crypt_stat,
5160216f7f7SMichael Halcrow 				   struct page *enc_extent_page,
5170216f7f7SMichael Halcrow 				   unsigned long extent_offset)
5180216f7f7SMichael Halcrow {
519d6a13c17SMichael Halcrow 	loff_t extent_base;
5200216f7f7SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
5210216f7f7SMichael Halcrow 	int rc;
5220216f7f7SMichael Halcrow 
523d6a13c17SMichael Halcrow 	extent_base = (((loff_t)page->index)
5240216f7f7SMichael Halcrow 		       * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
5250216f7f7SMichael Halcrow 	rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
5260216f7f7SMichael Halcrow 				(extent_base + extent_offset));
527237fead6SMichael Halcrow 	if (rc) {
5280216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to "
5290216f7f7SMichael Halcrow 				"derive IV for extent [0x%.16x]; "
5300216f7f7SMichael Halcrow 				"rc = [%d]\n", (extent_base + extent_offset),
5310216f7f7SMichael Halcrow 				rc);
532237fead6SMichael Halcrow 		goto out;
533237fead6SMichael Halcrow 	}
5340216f7f7SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
5350216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
5360216f7f7SMichael Halcrow 				"with iv:\n");
5370216f7f7SMichael Halcrow 		ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
5380216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
5390216f7f7SMichael Halcrow 				"decryption:\n");
5400216f7f7SMichael Halcrow 		ecryptfs_dump_hex((char *)
5410216f7f7SMichael Halcrow 				  (page_address(enc_extent_page)
5420216f7f7SMichael Halcrow 				   + (extent_offset * crypt_stat->extent_size)),
5430216f7f7SMichael Halcrow 				  8);
5440216f7f7SMichael Halcrow 	}
5450216f7f7SMichael Halcrow 	rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
5460216f7f7SMichael Halcrow 					  (extent_offset
5470216f7f7SMichael Halcrow 					   * crypt_stat->extent_size),
5480216f7f7SMichael Halcrow 					  enc_extent_page, 0,
5490216f7f7SMichael Halcrow 					  crypt_stat->extent_size, extent_iv);
5500216f7f7SMichael Halcrow 	if (rc < 0) {
5510216f7f7SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to decrypt to page with "
5520216f7f7SMichael Halcrow 		       "page->index = [%ld], extent_offset = [%ld]; "
5530216f7f7SMichael Halcrow 		       "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
5540216f7f7SMichael Halcrow 		       rc);
5550216f7f7SMichael Halcrow 		goto out;
5560216f7f7SMichael Halcrow 	}
5570216f7f7SMichael Halcrow 	rc = 0;
5580216f7f7SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
5590216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
5600216f7f7SMichael Halcrow 				"rc = [%d]\n", (extent_base + extent_offset),
5610216f7f7SMichael Halcrow 				rc);
5620216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
5630216f7f7SMichael Halcrow 				"decryption:\n");
5640216f7f7SMichael Halcrow 		ecryptfs_dump_hex((char *)(page_address(page)
5650216f7f7SMichael Halcrow 					   + (extent_offset
5660216f7f7SMichael Halcrow 					      * crypt_stat->extent_size)), 8);
5670216f7f7SMichael Halcrow 	}
568237fead6SMichael Halcrow out:
569237fead6SMichael Halcrow 	return rc;
570237fead6SMichael Halcrow }
571237fead6SMichael Halcrow 
572237fead6SMichael Halcrow /**
573237fead6SMichael Halcrow  * ecryptfs_decrypt_page
5740216f7f7SMichael Halcrow  * @page: Page mapped from the eCryptfs inode for the file; data read
5750216f7f7SMichael Halcrow  *        and decrypted from the lower file will be written into this
5760216f7f7SMichael Halcrow  *        page
577237fead6SMichael Halcrow  *
578237fead6SMichael Halcrow  * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
579237fead6SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
580237fead6SMichael Halcrow  * if the file was created on a machine with an 8K page size
581237fead6SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
582237fead6SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
583237fead6SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
584237fead6SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
585237fead6SMichael Halcrow  *
586237fead6SMichael Halcrow  * Returns zero on success; negative on error
587237fead6SMichael Halcrow  */
5880216f7f7SMichael Halcrow int ecryptfs_decrypt_page(struct page *page)
589237fead6SMichael Halcrow {
5900216f7f7SMichael Halcrow 	struct inode *ecryptfs_inode;
591237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
5920216f7f7SMichael Halcrow 	char *enc_extent_virt = NULL;
5930216f7f7SMichael Halcrow 	struct page *enc_extent_page;
5940216f7f7SMichael Halcrow 	unsigned long extent_offset;
595237fead6SMichael Halcrow 	int rc = 0;
596237fead6SMichael Halcrow 
5970216f7f7SMichael Halcrow 	ecryptfs_inode = page->mapping->host;
5980216f7f7SMichael Halcrow 	crypt_stat =
5990216f7f7SMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
600e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
6010216f7f7SMichael Halcrow 		rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
6020216f7f7SMichael Halcrow 						      PAGE_CACHE_SIZE,
6030216f7f7SMichael Halcrow 						      ecryptfs_inode);
604237fead6SMichael Halcrow 		if (rc)
6050216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to copy "
6060216f7f7SMichael Halcrow 			       "page at index [%ld]\n", __FUNCTION__,
607237fead6SMichael Halcrow 			       page->index);
6080216f7f7SMichael Halcrow 		goto out_clear_uptodate;
609237fead6SMichael Halcrow 	}
6100216f7f7SMichael Halcrow 	enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
6110216f7f7SMichael Halcrow 	if (!enc_extent_virt) {
612237fead6SMichael Halcrow 		rc = -ENOMEM;
6130216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error allocating memory for "
6140216f7f7SMichael Halcrow 				"encrypted extent\n");
6150216f7f7SMichael Halcrow 		goto out_clear_uptodate;
616237fead6SMichael Halcrow 	}
6170216f7f7SMichael Halcrow 	enc_extent_page = virt_to_page(enc_extent_virt);
6180216f7f7SMichael Halcrow 	for (extent_offset = 0;
6190216f7f7SMichael Halcrow 	     extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
6200216f7f7SMichael Halcrow 	     extent_offset++) {
6210216f7f7SMichael Halcrow 		loff_t offset;
6220216f7f7SMichael Halcrow 
6230216f7f7SMichael Halcrow 		ecryptfs_lower_offset_for_extent(
6240216f7f7SMichael Halcrow 			&offset, ((page->index * (PAGE_CACHE_SIZE
6250216f7f7SMichael Halcrow 						  / crypt_stat->extent_size))
6260216f7f7SMichael Halcrow 				  + extent_offset), crypt_stat);
6270216f7f7SMichael Halcrow 		rc = ecryptfs_read_lower(enc_extent_virt, offset,
628237fead6SMichael Halcrow 					 crypt_stat->extent_size,
6290216f7f7SMichael Halcrow 					 ecryptfs_inode);
6300216f7f7SMichael Halcrow 		if (rc) {
6310216f7f7SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting "
6320216f7f7SMichael Halcrow 					"to read lower page; rc = [%d]"
6330216f7f7SMichael Halcrow 					"\n", rc);
6340216f7f7SMichael Halcrow 			goto out_clear_uptodate;
635237fead6SMichael Halcrow 		}
6360216f7f7SMichael Halcrow 		rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
6370216f7f7SMichael Halcrow 					     extent_offset);
6380216f7f7SMichael Halcrow 		if (rc) {
6390216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting extent; "
6400216f7f7SMichael Halcrow 			       "rc = [%d]\n", __FUNCTION__, rc);
6410216f7f7SMichael Halcrow 			goto out_clear_uptodate;
642237fead6SMichael Halcrow 		}
643237fead6SMichael Halcrow 		extent_offset++;
644237fead6SMichael Halcrow 	}
6450216f7f7SMichael Halcrow 	SetPageUptodate(page);
6460216f7f7SMichael Halcrow 	goto out;
6470216f7f7SMichael Halcrow out_clear_uptodate:
6480216f7f7SMichael Halcrow 	ClearPageUptodate(page);
649237fead6SMichael Halcrow out:
6500216f7f7SMichael Halcrow 	kfree(enc_extent_virt);
651237fead6SMichael Halcrow 	return rc;
652237fead6SMichael Halcrow }
653237fead6SMichael Halcrow 
654237fead6SMichael Halcrow /**
655237fead6SMichael Halcrow  * decrypt_scatterlist
65622e78fafSMichael Halcrow  * @crypt_stat: Cryptographic context
65722e78fafSMichael Halcrow  * @dest_sg: The destination scatterlist to decrypt into
65822e78fafSMichael Halcrow  * @src_sg: The source scatterlist to decrypt from
65922e78fafSMichael Halcrow  * @size: The number of bytes to decrypt
66022e78fafSMichael Halcrow  * @iv: The initialization vector to use for the decryption
661237fead6SMichael Halcrow  *
662237fead6SMichael Halcrow  * Returns the number of bytes decrypted; negative value on error
663237fead6SMichael Halcrow  */
664237fead6SMichael Halcrow static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
665237fead6SMichael Halcrow 			       struct scatterlist *dest_sg,
666237fead6SMichael Halcrow 			       struct scatterlist *src_sg, int size,
667237fead6SMichael Halcrow 			       unsigned char *iv)
668237fead6SMichael Halcrow {
6698bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
6708bba066fSMichael Halcrow 		.tfm = crypt_stat->tfm,
6718bba066fSMichael Halcrow 		.info = iv,
6728bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
6738bba066fSMichael Halcrow 	};
674237fead6SMichael Halcrow 	int rc = 0;
675237fead6SMichael Halcrow 
676237fead6SMichael Halcrow 	/* Consider doing this once, when the file is opened */
677237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
6788bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
679237fead6SMichael Halcrow 				     crypt_stat->key_size);
680237fead6SMichael Halcrow 	if (rc) {
681237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
682237fead6SMichael Halcrow 				rc);
683237fead6SMichael Halcrow 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
684237fead6SMichael Halcrow 		rc = -EINVAL;
685237fead6SMichael Halcrow 		goto out;
686237fead6SMichael Halcrow 	}
687237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
6888bba066fSMichael Halcrow 	rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
689237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
690237fead6SMichael Halcrow 	if (rc) {
691237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
692237fead6SMichael Halcrow 				rc);
693237fead6SMichael Halcrow 		goto out;
694237fead6SMichael Halcrow 	}
695237fead6SMichael Halcrow 	rc = size;
696237fead6SMichael Halcrow out:
697237fead6SMichael Halcrow 	return rc;
698237fead6SMichael Halcrow }
699237fead6SMichael Halcrow 
700237fead6SMichael Halcrow /**
701237fead6SMichael Halcrow  * ecryptfs_encrypt_page_offset
70222e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
70322e78fafSMichael Halcrow  * @dst_page: The page to encrypt into
70422e78fafSMichael Halcrow  * @dst_offset: The offset in the page to encrypt into
70522e78fafSMichael Halcrow  * @src_page: The page to encrypt from
70622e78fafSMichael Halcrow  * @src_offset: The offset in the page to encrypt from
70722e78fafSMichael Halcrow  * @size: The number of bytes to encrypt
70822e78fafSMichael Halcrow  * @iv: The initialization vector to use for the encryption
709237fead6SMichael Halcrow  *
710237fead6SMichael Halcrow  * Returns the number of bytes encrypted
711237fead6SMichael Halcrow  */
712237fead6SMichael Halcrow static int
713237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
714237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
715237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
716237fead6SMichael Halcrow 			     unsigned char *iv)
717237fead6SMichael Halcrow {
718237fead6SMichael Halcrow 	struct scatterlist src_sg, dst_sg;
719237fead6SMichael Halcrow 
720237fead6SMichael Halcrow 	src_sg.page = src_page;
721237fead6SMichael Halcrow 	src_sg.offset = src_offset;
722237fead6SMichael Halcrow 	src_sg.length = size;
723237fead6SMichael Halcrow 	dst_sg.page = dst_page;
724237fead6SMichael Halcrow 	dst_sg.offset = dst_offset;
725237fead6SMichael Halcrow 	dst_sg.length = size;
726237fead6SMichael Halcrow 	return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
727237fead6SMichael Halcrow }
728237fead6SMichael Halcrow 
729237fead6SMichael Halcrow /**
730237fead6SMichael Halcrow  * ecryptfs_decrypt_page_offset
73122e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
73222e78fafSMichael Halcrow  * @dst_page: The page to decrypt into
73322e78fafSMichael Halcrow  * @dst_offset: The offset in the page to decrypt into
73422e78fafSMichael Halcrow  * @src_page: The page to decrypt from
73522e78fafSMichael Halcrow  * @src_offset: The offset in the page to decrypt from
73622e78fafSMichael Halcrow  * @size: The number of bytes to decrypt
73722e78fafSMichael Halcrow  * @iv: The initialization vector to use for the decryption
738237fead6SMichael Halcrow  *
739237fead6SMichael Halcrow  * Returns the number of bytes decrypted
740237fead6SMichael Halcrow  */
741237fead6SMichael Halcrow static int
742237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
743237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
744237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
745237fead6SMichael Halcrow 			     unsigned char *iv)
746237fead6SMichael Halcrow {
747237fead6SMichael Halcrow 	struct scatterlist src_sg, dst_sg;
748237fead6SMichael Halcrow 
749237fead6SMichael Halcrow 	src_sg.page = src_page;
750237fead6SMichael Halcrow 	src_sg.offset = src_offset;
751237fead6SMichael Halcrow 	src_sg.length = size;
752237fead6SMichael Halcrow 	dst_sg.page = dst_page;
753237fead6SMichael Halcrow 	dst_sg.offset = dst_offset;
754237fead6SMichael Halcrow 	dst_sg.length = size;
755237fead6SMichael Halcrow 	return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
756237fead6SMichael Halcrow }
757237fead6SMichael Halcrow 
758237fead6SMichael Halcrow #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
759237fead6SMichael Halcrow 
760237fead6SMichael Halcrow /**
761237fead6SMichael Halcrow  * ecryptfs_init_crypt_ctx
762237fead6SMichael Halcrow  * @crypt_stat: Uninitilized crypt stats structure
763237fead6SMichael Halcrow  *
764237fead6SMichael Halcrow  * Initialize the crypto context.
765237fead6SMichael Halcrow  *
766237fead6SMichael Halcrow  * TODO: Performance: Keep a cache of initialized cipher contexts;
767237fead6SMichael Halcrow  * only init if needed
768237fead6SMichael Halcrow  */
769237fead6SMichael Halcrow int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
770237fead6SMichael Halcrow {
7718bba066fSMichael Halcrow 	char *full_alg_name;
772237fead6SMichael Halcrow 	int rc = -EINVAL;
773237fead6SMichael Halcrow 
774237fead6SMichael Halcrow 	if (!crypt_stat->cipher) {
775237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "No cipher specified\n");
776237fead6SMichael Halcrow 		goto out;
777237fead6SMichael Halcrow 	}
778237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG,
779237fead6SMichael Halcrow 			"Initializing cipher [%s]; strlen = [%d]; "
780237fead6SMichael Halcrow 			"key_size_bits = [%d]\n",
781237fead6SMichael Halcrow 			crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
782237fead6SMichael Halcrow 			crypt_stat->key_size << 3);
783237fead6SMichael Halcrow 	if (crypt_stat->tfm) {
784237fead6SMichael Halcrow 		rc = 0;
785237fead6SMichael Halcrow 		goto out;
786237fead6SMichael Halcrow 	}
787237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
7888bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
7898bba066fSMichael Halcrow 						    crypt_stat->cipher, "cbc");
7908bba066fSMichael Halcrow 	if (rc)
7918bba066fSMichael Halcrow 		goto out;
7928bba066fSMichael Halcrow 	crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
7938bba066fSMichael Halcrow 						 CRYPTO_ALG_ASYNC);
7948bba066fSMichael Halcrow 	kfree(full_alg_name);
795de88777eSAkinobu Mita 	if (IS_ERR(crypt_stat->tfm)) {
796de88777eSAkinobu Mita 		rc = PTR_ERR(crypt_stat->tfm);
797237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
798237fead6SMichael Halcrow 				"Error initializing cipher [%s]\n",
799237fead6SMichael Halcrow 				crypt_stat->cipher);
8008bba066fSMichael Halcrow 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
801237fead6SMichael Halcrow 		goto out;
802237fead6SMichael Halcrow 	}
803f1ddcaf3SHerbert Xu 	crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
8048bba066fSMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
805237fead6SMichael Halcrow 	rc = 0;
806237fead6SMichael Halcrow out:
807237fead6SMichael Halcrow 	return rc;
808237fead6SMichael Halcrow }
809237fead6SMichael Halcrow 
810237fead6SMichael Halcrow static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
811237fead6SMichael Halcrow {
812237fead6SMichael Halcrow 	int extent_size_tmp;
813237fead6SMichael Halcrow 
814237fead6SMichael Halcrow 	crypt_stat->extent_mask = 0xFFFFFFFF;
815237fead6SMichael Halcrow 	crypt_stat->extent_shift = 0;
816237fead6SMichael Halcrow 	if (crypt_stat->extent_size == 0)
817237fead6SMichael Halcrow 		return;
818237fead6SMichael Halcrow 	extent_size_tmp = crypt_stat->extent_size;
819237fead6SMichael Halcrow 	while ((extent_size_tmp & 0x01) == 0) {
820237fead6SMichael Halcrow 		extent_size_tmp >>= 1;
821237fead6SMichael Halcrow 		crypt_stat->extent_mask <<= 1;
822237fead6SMichael Halcrow 		crypt_stat->extent_shift++;
823237fead6SMichael Halcrow 	}
824237fead6SMichael Halcrow }
825237fead6SMichael Halcrow 
826237fead6SMichael Halcrow void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
827237fead6SMichael Halcrow {
828237fead6SMichael Halcrow 	/* Default values; may be overwritten as we are parsing the
829237fead6SMichael Halcrow 	 * packets. */
830237fead6SMichael Halcrow 	crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
831237fead6SMichael Halcrow 	set_extent_mask_and_shift(crypt_stat);
832237fead6SMichael Halcrow 	crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
833dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
834dd2a3b7aSMichael Halcrow 		crypt_stat->num_header_extents_at_front = 0;
83545eaab79SMichael Halcrow 	else {
83645eaab79SMichael Halcrow 		if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
83745eaab79SMichael Halcrow 			crypt_stat->num_header_extents_at_front =
83845eaab79SMichael Halcrow 				(ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
83945eaab79SMichael Halcrow 				 / crypt_stat->extent_size);
840dd2a3b7aSMichael Halcrow 		else
84145eaab79SMichael Halcrow 			crypt_stat->num_header_extents_at_front =
84245eaab79SMichael Halcrow 				(PAGE_CACHE_SIZE / crypt_stat->extent_size);
84345eaab79SMichael Halcrow 	}
844237fead6SMichael Halcrow }
845237fead6SMichael Halcrow 
846237fead6SMichael Halcrow /**
847237fead6SMichael Halcrow  * ecryptfs_compute_root_iv
848237fead6SMichael Halcrow  * @crypt_stats
849237fead6SMichael Halcrow  *
850237fead6SMichael Halcrow  * On error, sets the root IV to all 0's.
851237fead6SMichael Halcrow  */
852237fead6SMichael Halcrow int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
853237fead6SMichael Halcrow {
854237fead6SMichael Halcrow 	int rc = 0;
855237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
856237fead6SMichael Halcrow 
857237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
858237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes <= 0);
859e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
860237fead6SMichael Halcrow 		rc = -EINVAL;
861237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Session key not valid; "
862237fead6SMichael Halcrow 				"cannot generate root IV\n");
863237fead6SMichael Halcrow 		goto out;
864237fead6SMichael Halcrow 	}
865237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
866237fead6SMichael Halcrow 				    crypt_stat->key_size);
867237fead6SMichael Halcrow 	if (rc) {
868237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
869237fead6SMichael Halcrow 				"MD5 while generating root IV\n");
870237fead6SMichael Halcrow 		goto out;
871237fead6SMichael Halcrow 	}
872237fead6SMichael Halcrow 	memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
873237fead6SMichael Halcrow out:
874237fead6SMichael Halcrow 	if (rc) {
875237fead6SMichael Halcrow 		memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
876e2bd99ecSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
877237fead6SMichael Halcrow 	}
878237fead6SMichael Halcrow 	return rc;
879237fead6SMichael Halcrow }
880237fead6SMichael Halcrow 
881237fead6SMichael Halcrow static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
882237fead6SMichael Halcrow {
883237fead6SMichael Halcrow 	get_random_bytes(crypt_stat->key, crypt_stat->key_size);
884e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
885237fead6SMichael Halcrow 	ecryptfs_compute_root_iv(crypt_stat);
886237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
887237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
888237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
889237fead6SMichael Halcrow 				  crypt_stat->key_size);
890237fead6SMichael Halcrow 	}
891237fead6SMichael Halcrow }
892237fead6SMichael Halcrow 
893237fead6SMichael Halcrow /**
89417398957SMichael Halcrow  * ecryptfs_copy_mount_wide_flags_to_inode_flags
89522e78fafSMichael Halcrow  * @crypt_stat: The inode's cryptographic context
89622e78fafSMichael Halcrow  * @mount_crypt_stat: The mount point's cryptographic context
89717398957SMichael Halcrow  *
89817398957SMichael Halcrow  * This function propagates the mount-wide flags to individual inode
89917398957SMichael Halcrow  * flags.
90017398957SMichael Halcrow  */
90117398957SMichael Halcrow static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
90217398957SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
90317398957SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
90417398957SMichael Halcrow {
90517398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
90617398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
90717398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
90817398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
90917398957SMichael Halcrow }
91017398957SMichael Halcrow 
911f4aad16aSMichael Halcrow static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
912f4aad16aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
913f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
914f4aad16aSMichael Halcrow {
915f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
916f4aad16aSMichael Halcrow 	int rc = 0;
917f4aad16aSMichael Halcrow 
918f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
919f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
920f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
921f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
922f4aad16aSMichael Halcrow 		rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
923f4aad16aSMichael Halcrow 		if (rc) {
924f4aad16aSMichael Halcrow 			printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
925f4aad16aSMichael Halcrow 			mutex_unlock(
926f4aad16aSMichael Halcrow 				&mount_crypt_stat->global_auth_tok_list_mutex);
927f4aad16aSMichael Halcrow 			goto out;
928f4aad16aSMichael Halcrow 		}
929f4aad16aSMichael Halcrow 	}
930f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
931f4aad16aSMichael Halcrow out:
932f4aad16aSMichael Halcrow 	return rc;
933f4aad16aSMichael Halcrow }
934f4aad16aSMichael Halcrow 
93517398957SMichael Halcrow /**
936237fead6SMichael Halcrow  * ecryptfs_set_default_crypt_stat_vals
93722e78fafSMichael Halcrow  * @crypt_stat: The inode's cryptographic context
93822e78fafSMichael Halcrow  * @mount_crypt_stat: The mount point's cryptographic context
939237fead6SMichael Halcrow  *
940237fead6SMichael Halcrow  * Default values in the event that policy does not override them.
941237fead6SMichael Halcrow  */
942237fead6SMichael Halcrow static void ecryptfs_set_default_crypt_stat_vals(
943237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
944237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
945237fead6SMichael Halcrow {
94617398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
94717398957SMichael Halcrow 						      mount_crypt_stat);
948237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
949237fead6SMichael Halcrow 	strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
950237fead6SMichael Halcrow 	crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
951e2bd99ecSMichael Halcrow 	crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
952237fead6SMichael Halcrow 	crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
953237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = mount_crypt_stat;
954237fead6SMichael Halcrow }
955237fead6SMichael Halcrow 
956237fead6SMichael Halcrow /**
957237fead6SMichael Halcrow  * ecryptfs_new_file_context
95822e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
959237fead6SMichael Halcrow  *
960237fead6SMichael Halcrow  * If the crypto context for the file has not yet been established,
961237fead6SMichael Halcrow  * this is where we do that.  Establishing a new crypto context
962237fead6SMichael Halcrow  * involves the following decisions:
963237fead6SMichael Halcrow  *  - What cipher to use?
964237fead6SMichael Halcrow  *  - What set of authentication tokens to use?
965237fead6SMichael Halcrow  * Here we just worry about getting enough information into the
966237fead6SMichael Halcrow  * authentication tokens so that we know that they are available.
967237fead6SMichael Halcrow  * We associate the available authentication tokens with the new file
968237fead6SMichael Halcrow  * via the set of signatures in the crypt_stat struct.  Later, when
969237fead6SMichael Halcrow  * the headers are actually written out, we may again defer to
970237fead6SMichael Halcrow  * userspace to perform the encryption of the session key; for the
971237fead6SMichael Halcrow  * foreseeable future, this will be the case with public key packets.
972237fead6SMichael Halcrow  *
973237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
974237fead6SMichael Halcrow  */
975237fead6SMichael Halcrow int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
976237fead6SMichael Halcrow {
977237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
978237fead6SMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
979237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
980237fead6SMichael Halcrow 	    &ecryptfs_superblock_to_private(
981237fead6SMichael Halcrow 		    ecryptfs_dentry->d_sb)->mount_crypt_stat;
982237fead6SMichael Halcrow 	int cipher_name_len;
983f4aad16aSMichael Halcrow 	int rc = 0;
984237fead6SMichael Halcrow 
985237fead6SMichael Halcrow 	ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
986af655dc6SMichael Halcrow 	crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
98717398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
98817398957SMichael Halcrow 						      mount_crypt_stat);
989f4aad16aSMichael Halcrow 	rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
990f4aad16aSMichael Halcrow 							 mount_crypt_stat);
991f4aad16aSMichael Halcrow 	if (rc) {
992f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
993f4aad16aSMichael Halcrow 		       "to the inode key sigs; rc = [%d]\n", rc);
994f4aad16aSMichael Halcrow 		goto out;
995f4aad16aSMichael Halcrow 	}
996237fead6SMichael Halcrow 	cipher_name_len =
997237fead6SMichael Halcrow 		strlen(mount_crypt_stat->global_default_cipher_name);
998237fead6SMichael Halcrow 	memcpy(crypt_stat->cipher,
999237fead6SMichael Halcrow 	       mount_crypt_stat->global_default_cipher_name,
1000237fead6SMichael Halcrow 	       cipher_name_len);
1001237fead6SMichael Halcrow 	crypt_stat->cipher[cipher_name_len] = '\0';
1002237fead6SMichael Halcrow 	crypt_stat->key_size =
1003237fead6SMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size;
1004237fead6SMichael Halcrow 	ecryptfs_generate_new_key(crypt_stat);
1005237fead6SMichael Halcrow 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
1006237fead6SMichael Halcrow 	if (rc)
1007237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1008237fead6SMichael Halcrow 				"context for cipher [%s]: rc = [%d]\n",
1009237fead6SMichael Halcrow 				crypt_stat->cipher, rc);
1010f4aad16aSMichael Halcrow out:
1011237fead6SMichael Halcrow 	return rc;
1012237fead6SMichael Halcrow }
1013237fead6SMichael Halcrow 
1014237fead6SMichael Halcrow /**
1015237fead6SMichael Halcrow  * contains_ecryptfs_marker - check for the ecryptfs marker
1016237fead6SMichael Halcrow  * @data: The data block in which to check
1017237fead6SMichael Halcrow  *
1018237fead6SMichael Halcrow  * Returns one if marker found; zero if not found
1019237fead6SMichael Halcrow  */
1020dd2a3b7aSMichael Halcrow static int contains_ecryptfs_marker(char *data)
1021237fead6SMichael Halcrow {
1022237fead6SMichael Halcrow 	u32 m_1, m_2;
1023237fead6SMichael Halcrow 
1024237fead6SMichael Halcrow 	memcpy(&m_1, data, 4);
1025237fead6SMichael Halcrow 	m_1 = be32_to_cpu(m_1);
1026237fead6SMichael Halcrow 	memcpy(&m_2, (data + 4), 4);
1027237fead6SMichael Halcrow 	m_2 = be32_to_cpu(m_2);
1028237fead6SMichael Halcrow 	if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1029237fead6SMichael Halcrow 		return 1;
1030237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1031237fead6SMichael Halcrow 			"MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1032237fead6SMichael Halcrow 			MAGIC_ECRYPTFS_MARKER);
1033237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1034237fead6SMichael Halcrow 			"[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1035237fead6SMichael Halcrow 	return 0;
1036237fead6SMichael Halcrow }
1037237fead6SMichael Halcrow 
1038237fead6SMichael Halcrow struct ecryptfs_flag_map_elem {
1039237fead6SMichael Halcrow 	u32 file_flag;
1040237fead6SMichael Halcrow 	u32 local_flag;
1041237fead6SMichael Halcrow };
1042237fead6SMichael Halcrow 
1043237fead6SMichael Halcrow /* Add support for additional flags by adding elements here. */
1044237fead6SMichael Halcrow static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1045237fead6SMichael Halcrow 	{0x00000001, ECRYPTFS_ENABLE_HMAC},
1046dd2a3b7aSMichael Halcrow 	{0x00000002, ECRYPTFS_ENCRYPTED},
1047dd2a3b7aSMichael Halcrow 	{0x00000004, ECRYPTFS_METADATA_IN_XATTR}
1048237fead6SMichael Halcrow };
1049237fead6SMichael Halcrow 
1050237fead6SMichael Halcrow /**
1051237fead6SMichael Halcrow  * ecryptfs_process_flags
105222e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1053237fead6SMichael Halcrow  * @page_virt: Source data to be parsed
1054237fead6SMichael Halcrow  * @bytes_read: Updated with the number of bytes read
1055237fead6SMichael Halcrow  *
1056237fead6SMichael Halcrow  * Returns zero on success; non-zero if the flag set is invalid
1057237fead6SMichael Halcrow  */
1058237fead6SMichael Halcrow static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1059237fead6SMichael Halcrow 				  char *page_virt, int *bytes_read)
1060237fead6SMichael Halcrow {
1061237fead6SMichael Halcrow 	int rc = 0;
1062237fead6SMichael Halcrow 	int i;
1063237fead6SMichael Halcrow 	u32 flags;
1064237fead6SMichael Halcrow 
1065237fead6SMichael Halcrow 	memcpy(&flags, page_virt, 4);
1066237fead6SMichael Halcrow 	flags = be32_to_cpu(flags);
1067237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1068237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
1069237fead6SMichael Halcrow 		if (flags & ecryptfs_flag_map[i].file_flag) {
1070e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
1071237fead6SMichael Halcrow 		} else
1072e2bd99ecSMichael Halcrow 			crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
1073237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
1074237fead6SMichael Halcrow 	crypt_stat->file_version = ((flags >> 24) & 0xFF);
1075237fead6SMichael Halcrow 	(*bytes_read) = 4;
1076237fead6SMichael Halcrow 	return rc;
1077237fead6SMichael Halcrow }
1078237fead6SMichael Halcrow 
1079237fead6SMichael Halcrow /**
1080237fead6SMichael Halcrow  * write_ecryptfs_marker
1081237fead6SMichael Halcrow  * @page_virt: The pointer to in a page to begin writing the marker
1082237fead6SMichael Halcrow  * @written: Number of bytes written
1083237fead6SMichael Halcrow  *
1084237fead6SMichael Halcrow  * Marker = 0x3c81b7f5
1085237fead6SMichael Halcrow  */
1086237fead6SMichael Halcrow static void write_ecryptfs_marker(char *page_virt, size_t *written)
1087237fead6SMichael Halcrow {
1088237fead6SMichael Halcrow 	u32 m_1, m_2;
1089237fead6SMichael Halcrow 
1090237fead6SMichael Halcrow 	get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1091237fead6SMichael Halcrow 	m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1092237fead6SMichael Halcrow 	m_1 = cpu_to_be32(m_1);
1093237fead6SMichael Halcrow 	memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1094237fead6SMichael Halcrow 	m_2 = cpu_to_be32(m_2);
1095237fead6SMichael Halcrow 	memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
1096237fead6SMichael Halcrow 	       (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1097237fead6SMichael Halcrow 	(*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1098237fead6SMichael Halcrow }
1099237fead6SMichael Halcrow 
1100237fead6SMichael Halcrow static void
1101237fead6SMichael Halcrow write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1102237fead6SMichael Halcrow 		     size_t *written)
1103237fead6SMichael Halcrow {
1104237fead6SMichael Halcrow 	u32 flags = 0;
1105237fead6SMichael Halcrow 	int i;
1106237fead6SMichael Halcrow 
1107237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1108237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
1109e2bd99ecSMichael Halcrow 		if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
1110237fead6SMichael Halcrow 			flags |= ecryptfs_flag_map[i].file_flag;
1111237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
1112237fead6SMichael Halcrow 	flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1113237fead6SMichael Halcrow 	flags = cpu_to_be32(flags);
1114237fead6SMichael Halcrow 	memcpy(page_virt, &flags, 4);
1115237fead6SMichael Halcrow 	(*written) = 4;
1116237fead6SMichael Halcrow }
1117237fead6SMichael Halcrow 
1118237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem {
1119237fead6SMichael Halcrow 	char cipher_str[16];
1120237fead6SMichael Halcrow 	u16 cipher_code;
1121237fead6SMichael Halcrow };
1122237fead6SMichael Halcrow 
1123237fead6SMichael Halcrow /* Add support for additional ciphers by adding elements here. The
1124237fead6SMichael Halcrow  * cipher_code is whatever OpenPGP applicatoins use to identify the
1125237fead6SMichael Halcrow  * ciphers. List in order of probability. */
1126237fead6SMichael Halcrow static struct ecryptfs_cipher_code_str_map_elem
1127237fead6SMichael Halcrow ecryptfs_cipher_code_str_map[] = {
1128237fead6SMichael Halcrow 	{"aes",RFC2440_CIPHER_AES_128 },
1129237fead6SMichael Halcrow 	{"blowfish", RFC2440_CIPHER_BLOWFISH},
1130237fead6SMichael Halcrow 	{"des3_ede", RFC2440_CIPHER_DES3_EDE},
1131237fead6SMichael Halcrow 	{"cast5", RFC2440_CIPHER_CAST_5},
1132237fead6SMichael Halcrow 	{"twofish", RFC2440_CIPHER_TWOFISH},
1133237fead6SMichael Halcrow 	{"cast6", RFC2440_CIPHER_CAST_6},
1134237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_192},
1135237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_256}
1136237fead6SMichael Halcrow };
1137237fead6SMichael Halcrow 
1138237fead6SMichael Halcrow /**
1139237fead6SMichael Halcrow  * ecryptfs_code_for_cipher_string
114022e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1141237fead6SMichael Halcrow  *
1142237fead6SMichael Halcrow  * Returns zero on no match, or the cipher code on match
1143237fead6SMichael Halcrow  */
1144237fead6SMichael Halcrow u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
1145237fead6SMichael Halcrow {
1146237fead6SMichael Halcrow 	int i;
1147237fead6SMichael Halcrow 	u16 code = 0;
1148237fead6SMichael Halcrow 	struct ecryptfs_cipher_code_str_map_elem *map =
1149237fead6SMichael Halcrow 		ecryptfs_cipher_code_str_map;
1150237fead6SMichael Halcrow 
1151237fead6SMichael Halcrow 	if (strcmp(crypt_stat->cipher, "aes") == 0) {
1152237fead6SMichael Halcrow 		switch (crypt_stat->key_size) {
1153237fead6SMichael Halcrow 		case 16:
1154237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_128;
1155237fead6SMichael Halcrow 			break;
1156237fead6SMichael Halcrow 		case 24:
1157237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_192;
1158237fead6SMichael Halcrow 			break;
1159237fead6SMichael Halcrow 		case 32:
1160237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_256;
1161237fead6SMichael Halcrow 		}
1162237fead6SMichael Halcrow 	} else {
1163237fead6SMichael Halcrow 		for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1164237fead6SMichael Halcrow 			if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
1165237fead6SMichael Halcrow 				code = map[i].cipher_code;
1166237fead6SMichael Halcrow 				break;
1167237fead6SMichael Halcrow 			}
1168237fead6SMichael Halcrow 	}
1169237fead6SMichael Halcrow 	return code;
1170237fead6SMichael Halcrow }
1171237fead6SMichael Halcrow 
1172237fead6SMichael Halcrow /**
1173237fead6SMichael Halcrow  * ecryptfs_cipher_code_to_string
1174237fead6SMichael Halcrow  * @str: Destination to write out the cipher name
1175237fead6SMichael Halcrow  * @cipher_code: The code to convert to cipher name string
1176237fead6SMichael Halcrow  *
1177237fead6SMichael Halcrow  * Returns zero on success
1178237fead6SMichael Halcrow  */
1179237fead6SMichael Halcrow int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
1180237fead6SMichael Halcrow {
1181237fead6SMichael Halcrow 	int rc = 0;
1182237fead6SMichael Halcrow 	int i;
1183237fead6SMichael Halcrow 
1184237fead6SMichael Halcrow 	str[0] = '\0';
1185237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1186237fead6SMichael Halcrow 		if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1187237fead6SMichael Halcrow 			strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1188237fead6SMichael Halcrow 	if (str[0] == '\0') {
1189237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1190237fead6SMichael Halcrow 				"[%d]\n", cipher_code);
1191237fead6SMichael Halcrow 		rc = -EINVAL;
1192237fead6SMichael Halcrow 	}
1193237fead6SMichael Halcrow 	return rc;
1194237fead6SMichael Halcrow }
1195237fead6SMichael Halcrow 
1196d7cdc5feSMichael Halcrow int ecryptfs_read_and_validate_header_region(char *data,
1197d7cdc5feSMichael Halcrow 					     struct inode *ecryptfs_inode)
1198dd2a3b7aSMichael Halcrow {
1199d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1200d7cdc5feSMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
1201dd2a3b7aSMichael Halcrow 	int rc;
1202dd2a3b7aSMichael Halcrow 
1203d7cdc5feSMichael Halcrow 	rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
1204d7cdc5feSMichael Halcrow 				 ecryptfs_inode);
1205d7cdc5feSMichael Halcrow 	if (rc) {
1206d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
1207d7cdc5feSMichael Halcrow 		       __FUNCTION__, rc);
1208dd2a3b7aSMichael Halcrow 		goto out;
1209d7cdc5feSMichael Halcrow 	}
1210d7cdc5feSMichael Halcrow 	if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
1211dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1212d7cdc5feSMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n");
1213d7cdc5feSMichael Halcrow 	}
1214dd2a3b7aSMichael Halcrow out:
1215dd2a3b7aSMichael Halcrow 	return rc;
1216dd2a3b7aSMichael Halcrow }
1217dd2a3b7aSMichael Halcrow 
1218e77a56ddSMichael Halcrow void
1219e77a56ddSMichael Halcrow ecryptfs_write_header_metadata(char *virt,
1220e77a56ddSMichael Halcrow 			       struct ecryptfs_crypt_stat *crypt_stat,
1221237fead6SMichael Halcrow 			       size_t *written)
1222237fead6SMichael Halcrow {
1223237fead6SMichael Halcrow 	u32 header_extent_size;
1224237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1225237fead6SMichael Halcrow 
122645eaab79SMichael Halcrow 	header_extent_size = (u32)crypt_stat->extent_size;
1227237fead6SMichael Halcrow 	num_header_extents_at_front =
1228237fead6SMichael Halcrow 		(u16)crypt_stat->num_header_extents_at_front;
1229237fead6SMichael Halcrow 	header_extent_size = cpu_to_be32(header_extent_size);
1230237fead6SMichael Halcrow 	memcpy(virt, &header_extent_size, 4);
1231237fead6SMichael Halcrow 	virt += 4;
1232237fead6SMichael Halcrow 	num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
1233237fead6SMichael Halcrow 	memcpy(virt, &num_header_extents_at_front, 2);
1234237fead6SMichael Halcrow 	(*written) = 6;
1235237fead6SMichael Halcrow }
1236237fead6SMichael Halcrow 
1237237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_0;
1238237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_1;
1239237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_2;
1240237fead6SMichael Halcrow 
1241237fead6SMichael Halcrow /**
1242237fead6SMichael Halcrow  * ecryptfs_write_headers_virt
124322e78fafSMichael Halcrow  * @page_virt: The virtual address to write the headers to
124422e78fafSMichael Halcrow  * @size: Set to the number of bytes written by this function
124522e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
124622e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
1247237fead6SMichael Halcrow  *
1248237fead6SMichael Halcrow  * Format version: 1
1249237fead6SMichael Halcrow  *
1250237fead6SMichael Halcrow  *   Header Extent:
1251237fead6SMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
1252237fead6SMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
1253237fead6SMichael Halcrow  *     Octets 16-19:      Flags
1254237fead6SMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
1255237fead6SMichael Halcrow  *      Octets 17-18:     Reserved
1256237fead6SMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
1257237fead6SMichael Halcrow  *                        Bit 2: Encrypted?
1258237fead6SMichael Halcrow  *                        Bits 3-8: Reserved
1259237fead6SMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
1260237fead6SMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
1261237fead6SMichael Halcrow  *                        (big-endian)
1262237fead6SMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
1263237fead6SMichael Halcrow  *   Data Extent 0:
1264237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1265237fead6SMichael Halcrow  *   Data Extent 1:
1266237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1267237fead6SMichael Halcrow  *   ...
1268237fead6SMichael Halcrow  *
1269237fead6SMichael Halcrow  * Returns zero on success
1270237fead6SMichael Halcrow  */
1271dd2a3b7aSMichael Halcrow static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
1272237fead6SMichael Halcrow 				       struct ecryptfs_crypt_stat *crypt_stat,
1273237fead6SMichael Halcrow 				       struct dentry *ecryptfs_dentry)
1274237fead6SMichael Halcrow {
1275237fead6SMichael Halcrow 	int rc;
1276237fead6SMichael Halcrow 	size_t written;
1277237fead6SMichael Halcrow 	size_t offset;
1278237fead6SMichael Halcrow 
1279237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1280237fead6SMichael Halcrow 	write_ecryptfs_marker((page_virt + offset), &written);
1281237fead6SMichael Halcrow 	offset += written;
1282237fead6SMichael Halcrow 	write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1283237fead6SMichael Halcrow 	offset += written;
1284e77a56ddSMichael Halcrow 	ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1285e77a56ddSMichael Halcrow 				       &written);
1286237fead6SMichael Halcrow 	offset += written;
1287237fead6SMichael Halcrow 	rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1288237fead6SMichael Halcrow 					      ecryptfs_dentry, &written,
1289237fead6SMichael Halcrow 					      PAGE_CACHE_SIZE - offset);
1290237fead6SMichael Halcrow 	if (rc)
1291237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1292237fead6SMichael Halcrow 				"set; rc = [%d]\n", rc);
1293dd2a3b7aSMichael Halcrow 	if (size) {
1294dd2a3b7aSMichael Halcrow 		offset += written;
1295dd2a3b7aSMichael Halcrow 		*size = offset;
1296dd2a3b7aSMichael Halcrow 	}
1297dd2a3b7aSMichael Halcrow 	return rc;
1298dd2a3b7aSMichael Halcrow }
1299dd2a3b7aSMichael Halcrow 
130022e78fafSMichael Halcrow static int
130122e78fafSMichael Halcrow ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
1302d7cdc5feSMichael Halcrow 				    struct dentry *ecryptfs_dentry,
1303d7cdc5feSMichael Halcrow 				    char *page_virt)
1304dd2a3b7aSMichael Halcrow {
1305dd2a3b7aSMichael Halcrow 	int current_header_page;
1306dd2a3b7aSMichael Halcrow 	int header_pages;
1307d7cdc5feSMichael Halcrow 	int rc;
1308dd2a3b7aSMichael Halcrow 
1309d7cdc5feSMichael Halcrow 	rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, page_virt,
1310d7cdc5feSMichael Halcrow 				  0, PAGE_CACHE_SIZE);
1311d7cdc5feSMichael Halcrow 	if (rc) {
1312d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to write header "
1313d7cdc5feSMichael Halcrow 		       "information to lower file; rc = [%d]\n", __FUNCTION__,
1314d7cdc5feSMichael Halcrow 		       rc);
131570456600SMichael Halcrow 		goto out;
131670456600SMichael Halcrow 	}
131745eaab79SMichael Halcrow 	header_pages = ((crypt_stat->extent_size
1318dd2a3b7aSMichael Halcrow 			 * crypt_stat->num_header_extents_at_front)
1319dd2a3b7aSMichael Halcrow 			/ PAGE_CACHE_SIZE);
1320dd2a3b7aSMichael Halcrow 	memset(page_virt, 0, PAGE_CACHE_SIZE);
1321dd2a3b7aSMichael Halcrow 	current_header_page = 1;
1322dd2a3b7aSMichael Halcrow 	while (current_header_page < header_pages) {
1323d7cdc5feSMichael Halcrow 		loff_t offset;
1324d7cdc5feSMichael Halcrow 
1325d6a13c17SMichael Halcrow 		offset = (((loff_t)current_header_page) << PAGE_CACHE_SHIFT);
1326d7cdc5feSMichael Halcrow 		if ((rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode,
1327d7cdc5feSMichael Halcrow 					       page_virt, offset,
1328d7cdc5feSMichael Halcrow 					       PAGE_CACHE_SIZE))) {
1329d7cdc5feSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to write header "
1330d7cdc5feSMichael Halcrow 			       "information to lower file; rc = [%d]\n",
1331d7cdc5feSMichael Halcrow 			       __FUNCTION__, rc);
133270456600SMichael Halcrow 			goto out;
133370456600SMichael Halcrow 		}
1334dd2a3b7aSMichael Halcrow 		current_header_page++;
1335dd2a3b7aSMichael Halcrow 	}
133670456600SMichael Halcrow out:
133770456600SMichael Halcrow 	return rc;
1338dd2a3b7aSMichael Halcrow }
1339dd2a3b7aSMichael Halcrow 
134022e78fafSMichael Halcrow static int
134122e78fafSMichael Halcrow ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1342dd2a3b7aSMichael Halcrow 				 struct ecryptfs_crypt_stat *crypt_stat,
1343dd2a3b7aSMichael Halcrow 				 char *page_virt, size_t size)
1344dd2a3b7aSMichael Halcrow {
1345dd2a3b7aSMichael Halcrow 	int rc;
1346dd2a3b7aSMichael Halcrow 
1347dd2a3b7aSMichael Halcrow 	rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1348dd2a3b7aSMichael Halcrow 			       size, 0);
1349237fead6SMichael Halcrow 	return rc;
1350237fead6SMichael Halcrow }
1351237fead6SMichael Halcrow 
1352237fead6SMichael Halcrow /**
1353dd2a3b7aSMichael Halcrow  * ecryptfs_write_metadata
135422e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
1355237fead6SMichael Halcrow  *
1356237fead6SMichael Halcrow  * Write the file headers out.  This will likely involve a userspace
1357237fead6SMichael Halcrow  * callout, in which the session key is encrypted with one or more
1358237fead6SMichael Halcrow  * public keys and/or the passphrase necessary to do the encryption is
1359237fead6SMichael Halcrow  * retrieved via a prompt.  Exactly what happens at this point should
1360237fead6SMichael Halcrow  * be policy-dependent.
1361237fead6SMichael Halcrow  *
1362d7cdc5feSMichael Halcrow  * TODO: Support header information spanning multiple pages
1363d7cdc5feSMichael Halcrow  *
1364237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
1365237fead6SMichael Halcrow  */
1366d7cdc5feSMichael Halcrow int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
1367237fead6SMichael Halcrow {
1368d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1369d7cdc5feSMichael Halcrow 		&ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1370237fead6SMichael Halcrow 	char *page_virt;
1371d7cdc5feSMichael Halcrow 	size_t size = 0;
1372237fead6SMichael Halcrow 	int rc = 0;
1373237fead6SMichael Halcrow 
1374e2bd99ecSMichael Halcrow 	if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1375e2bd99ecSMichael Halcrow 		if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
1376d7cdc5feSMichael Halcrow 			printk(KERN_ERR "Key is invalid; bailing out\n");
1377237fead6SMichael Halcrow 			rc = -EINVAL;
1378237fead6SMichael Halcrow 			goto out;
1379237fead6SMichael Halcrow 		}
1380237fead6SMichael Halcrow 	} else {
1381237fead6SMichael Halcrow 		rc = -EINVAL;
1382237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING,
1383237fead6SMichael Halcrow 				"Called with crypt_stat->encrypted == 0\n");
1384237fead6SMichael Halcrow 		goto out;
1385237fead6SMichael Halcrow 	}
1386237fead6SMichael Halcrow 	/* Released in this function */
1387c3762229SRobert P. J. Day 	page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER);
1388237fead6SMichael Halcrow 	if (!page_virt) {
1389237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Out of memory\n");
1390237fead6SMichael Halcrow 		rc = -ENOMEM;
1391237fead6SMichael Halcrow 		goto out;
1392237fead6SMichael Halcrow 	}
1393dd2a3b7aSMichael Halcrow 	rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat,
1394237fead6SMichael Halcrow   					 ecryptfs_dentry);
1395237fead6SMichael Halcrow 	if (unlikely(rc)) {
1396237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
1397237fead6SMichael Halcrow 		memset(page_virt, 0, PAGE_CACHE_SIZE);
1398237fead6SMichael Halcrow 		goto out_free;
1399237fead6SMichael Halcrow 	}
1400dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1401dd2a3b7aSMichael Halcrow 		rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
1402dd2a3b7aSMichael Halcrow 						      crypt_stat, page_virt,
1403dd2a3b7aSMichael Halcrow 						      size);
1404dd2a3b7aSMichael Halcrow 	else
1405d7cdc5feSMichael Halcrow 		rc = ecryptfs_write_metadata_to_contents(crypt_stat,
1406d7cdc5feSMichael Halcrow 							 ecryptfs_dentry,
1407dd2a3b7aSMichael Halcrow 							 page_virt);
1408dd2a3b7aSMichael Halcrow 	if (rc) {
1409dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error writing metadata out to lower file; "
1410dd2a3b7aSMichael Halcrow 		       "rc = [%d]\n", rc);
1411dd2a3b7aSMichael Halcrow 		goto out_free;
1412237fead6SMichael Halcrow 	}
1413237fead6SMichael Halcrow out_free:
1414237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_header_cache_0, page_virt);
1415237fead6SMichael Halcrow out:
1416237fead6SMichael Halcrow 	return rc;
1417237fead6SMichael Halcrow }
1418237fead6SMichael Halcrow 
1419dd2a3b7aSMichael Halcrow #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1420dd2a3b7aSMichael Halcrow #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
1421237fead6SMichael Halcrow static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
1422dd2a3b7aSMichael Halcrow 				 char *virt, int *bytes_read,
1423dd2a3b7aSMichael Halcrow 				 int validate_header_size)
1424237fead6SMichael Halcrow {
1425237fead6SMichael Halcrow 	int rc = 0;
1426237fead6SMichael Halcrow 	u32 header_extent_size;
1427237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1428237fead6SMichael Halcrow 
1429*ecbdc936SMichael Halcrow 	memcpy(&header_extent_size, virt, sizeof(u32));
1430237fead6SMichael Halcrow 	header_extent_size = be32_to_cpu(header_extent_size);
1431*ecbdc936SMichael Halcrow 	virt += sizeof(u32);
1432*ecbdc936SMichael Halcrow 	memcpy(&num_header_extents_at_front, virt, sizeof(u16));
1433237fead6SMichael Halcrow 	num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
1434237fead6SMichael Halcrow 	crypt_stat->num_header_extents_at_front =
1435237fead6SMichael Halcrow 		(int)num_header_extents_at_front;
143645eaab79SMichael Halcrow 	(*bytes_read) = (sizeof(u32) + sizeof(u16));
1437dd2a3b7aSMichael Halcrow 	if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
143845eaab79SMichael Halcrow 	    && ((crypt_stat->extent_size
1439237fead6SMichael Halcrow 		 * crypt_stat->num_header_extents_at_front)
1440dd2a3b7aSMichael Halcrow 		< ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
1441237fead6SMichael Halcrow 		rc = -EINVAL;
144245eaab79SMichael Halcrow 		printk(KERN_WARNING "Invalid number of header extents: [%zd]\n",
144345eaab79SMichael Halcrow 		       crypt_stat->num_header_extents_at_front);
1444237fead6SMichael Halcrow 	}
1445237fead6SMichael Halcrow 	return rc;
1446237fead6SMichael Halcrow }
1447237fead6SMichael Halcrow 
1448237fead6SMichael Halcrow /**
1449237fead6SMichael Halcrow  * set_default_header_data
145022e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1451237fead6SMichael Halcrow  *
1452237fead6SMichael Halcrow  * For version 0 file format; this function is only for backwards
1453237fead6SMichael Halcrow  * compatibility for files created with the prior versions of
1454237fead6SMichael Halcrow  * eCryptfs.
1455237fead6SMichael Halcrow  */
1456237fead6SMichael Halcrow static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1457237fead6SMichael Halcrow {
145845eaab79SMichael Halcrow 	crypt_stat->num_header_extents_at_front = 2;
1459237fead6SMichael Halcrow }
1460237fead6SMichael Halcrow 
1461237fead6SMichael Halcrow /**
1462237fead6SMichael Halcrow  * ecryptfs_read_headers_virt
146322e78fafSMichael Halcrow  * @page_virt: The virtual address into which to read the headers
146422e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
146522e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
146622e78fafSMichael Halcrow  * @validate_header_size: Whether to validate the header size while reading
1467237fead6SMichael Halcrow  *
1468237fead6SMichael Halcrow  * Read/parse the header data. The header format is detailed in the
1469237fead6SMichael Halcrow  * comment block for the ecryptfs_write_headers_virt() function.
1470237fead6SMichael Halcrow  *
1471237fead6SMichael Halcrow  * Returns zero on success
1472237fead6SMichael Halcrow  */
1473237fead6SMichael Halcrow static int ecryptfs_read_headers_virt(char *page_virt,
1474237fead6SMichael Halcrow 				      struct ecryptfs_crypt_stat *crypt_stat,
1475dd2a3b7aSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
1476dd2a3b7aSMichael Halcrow 				      int validate_header_size)
1477237fead6SMichael Halcrow {
1478237fead6SMichael Halcrow 	int rc = 0;
1479237fead6SMichael Halcrow 	int offset;
1480237fead6SMichael Halcrow 	int bytes_read;
1481237fead6SMichael Halcrow 
1482237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
1483237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1484237fead6SMichael Halcrow 		ecryptfs_dentry->d_sb)->mount_crypt_stat;
1485237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1486237fead6SMichael Halcrow 	rc = contains_ecryptfs_marker(page_virt + offset);
1487237fead6SMichael Halcrow 	if (rc == 0) {
1488237fead6SMichael Halcrow 		rc = -EINVAL;
1489237fead6SMichael Halcrow 		goto out;
1490237fead6SMichael Halcrow 	}
1491237fead6SMichael Halcrow 	offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1492237fead6SMichael Halcrow 	rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1493237fead6SMichael Halcrow 				    &bytes_read);
1494237fead6SMichael Halcrow 	if (rc) {
1495237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1496237fead6SMichael Halcrow 		goto out;
1497237fead6SMichael Halcrow 	}
1498237fead6SMichael Halcrow 	if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1499237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1500237fead6SMichael Halcrow 				"file version [%d] is supported by this "
1501237fead6SMichael Halcrow 				"version of eCryptfs\n",
1502237fead6SMichael Halcrow 				crypt_stat->file_version,
1503237fead6SMichael Halcrow 				ECRYPTFS_SUPPORTED_FILE_VERSION);
1504237fead6SMichael Halcrow 		rc = -EINVAL;
1505237fead6SMichael Halcrow 		goto out;
1506237fead6SMichael Halcrow 	}
1507237fead6SMichael Halcrow 	offset += bytes_read;
1508237fead6SMichael Halcrow 	if (crypt_stat->file_version >= 1) {
1509237fead6SMichael Halcrow 		rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1510dd2a3b7aSMichael Halcrow 					   &bytes_read, validate_header_size);
1511237fead6SMichael Halcrow 		if (rc) {
1512237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Error reading header "
1513237fead6SMichael Halcrow 					"metadata; rc = [%d]\n", rc);
1514237fead6SMichael Halcrow 		}
1515237fead6SMichael Halcrow 		offset += bytes_read;
1516237fead6SMichael Halcrow 	} else
1517237fead6SMichael Halcrow 		set_default_header_data(crypt_stat);
1518237fead6SMichael Halcrow 	rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1519237fead6SMichael Halcrow 				       ecryptfs_dentry);
1520237fead6SMichael Halcrow out:
1521237fead6SMichael Halcrow 	return rc;
1522237fead6SMichael Halcrow }
1523237fead6SMichael Halcrow 
1524237fead6SMichael Halcrow /**
1525dd2a3b7aSMichael Halcrow  * ecryptfs_read_xattr_region
152622e78fafSMichael Halcrow  * @page_virt: The vitual address into which to read the xattr data
15272ed92554SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
1528dd2a3b7aSMichael Halcrow  *
1529dd2a3b7aSMichael Halcrow  * Attempts to read the crypto metadata from the extended attribute
1530dd2a3b7aSMichael Halcrow  * region of the lower file.
153122e78fafSMichael Halcrow  *
153222e78fafSMichael Halcrow  * Returns zero on success; non-zero on error
1533dd2a3b7aSMichael Halcrow  */
1534d7cdc5feSMichael Halcrow int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
1535dd2a3b7aSMichael Halcrow {
1536d7cdc5feSMichael Halcrow 	struct dentry *lower_dentry =
1537d7cdc5feSMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
1538dd2a3b7aSMichael Halcrow 	ssize_t size;
1539dd2a3b7aSMichael Halcrow 	int rc = 0;
1540dd2a3b7aSMichael Halcrow 
1541d7cdc5feSMichael Halcrow 	size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1542dd2a3b7aSMichael Halcrow 				       page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1543dd2a3b7aSMichael Halcrow 	if (size < 0) {
1544d7cdc5feSMichael Halcrow 		printk(KERN_ERR "Error attempting to read the [%s] "
1545dd2a3b7aSMichael Halcrow 		       "xattr from the lower file; return value = [%zd]\n",
1546dd2a3b7aSMichael Halcrow 		       ECRYPTFS_XATTR_NAME, size);
1547dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1548dd2a3b7aSMichael Halcrow 		goto out;
1549dd2a3b7aSMichael Halcrow 	}
1550dd2a3b7aSMichael Halcrow out:
1551dd2a3b7aSMichael Halcrow 	return rc;
1552dd2a3b7aSMichael Halcrow }
1553dd2a3b7aSMichael Halcrow 
1554dd2a3b7aSMichael Halcrow int ecryptfs_read_and_validate_xattr_region(char *page_virt,
1555dd2a3b7aSMichael Halcrow 					    struct dentry *ecryptfs_dentry)
1556dd2a3b7aSMichael Halcrow {
1557dd2a3b7aSMichael Halcrow 	int rc;
1558dd2a3b7aSMichael Halcrow 
1559d7cdc5feSMichael Halcrow 	rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
1560dd2a3b7aSMichael Halcrow 	if (rc)
1561dd2a3b7aSMichael Halcrow 		goto out;
1562dd2a3b7aSMichael Halcrow 	if (!contains_ecryptfs_marker(page_virt	+ ECRYPTFS_FILE_SIZE_BYTES)) {
1563dd2a3b7aSMichael Halcrow 		printk(KERN_WARNING "Valid data found in [%s] xattr, but "
1564dd2a3b7aSMichael Halcrow 			"the marker is invalid\n", ECRYPTFS_XATTR_NAME);
1565dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1566dd2a3b7aSMichael Halcrow 	}
1567dd2a3b7aSMichael Halcrow out:
1568dd2a3b7aSMichael Halcrow 	return rc;
1569dd2a3b7aSMichael Halcrow }
1570dd2a3b7aSMichael Halcrow 
1571dd2a3b7aSMichael Halcrow /**
1572dd2a3b7aSMichael Halcrow  * ecryptfs_read_metadata
1573dd2a3b7aSMichael Halcrow  *
1574dd2a3b7aSMichael Halcrow  * Common entry point for reading file metadata. From here, we could
1575dd2a3b7aSMichael Halcrow  * retrieve the header information from the header region of the file,
1576dd2a3b7aSMichael Halcrow  * the xattr region of the file, or some other repostory that is
1577dd2a3b7aSMichael Halcrow  * stored separately from the file itself. The current implementation
1578dd2a3b7aSMichael Halcrow  * supports retrieving the metadata information from the file contents
1579dd2a3b7aSMichael Halcrow  * and from the xattr region.
1580237fead6SMichael Halcrow  *
1581237fead6SMichael Halcrow  * Returns zero if valid headers found and parsed; non-zero otherwise
1582237fead6SMichael Halcrow  */
1583d7cdc5feSMichael Halcrow int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
1584237fead6SMichael Halcrow {
1585237fead6SMichael Halcrow 	int rc = 0;
1586237fead6SMichael Halcrow 	char *page_virt = NULL;
1587d7cdc5feSMichael Halcrow 	struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
1588237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1589d7cdc5feSMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1590e77a56ddSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1591e77a56ddSMichael Halcrow 		&ecryptfs_superblock_to_private(
1592e77a56ddSMichael Halcrow 			ecryptfs_dentry->d_sb)->mount_crypt_stat;
1593237fead6SMichael Halcrow 
1594e77a56ddSMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1595e77a56ddSMichael Halcrow 						      mount_crypt_stat);
1596237fead6SMichael Halcrow 	/* Read the first page from the underlying file */
1597f7267c0cSChristoph Lameter 	page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
1598237fead6SMichael Halcrow 	if (!page_virt) {
1599237fead6SMichael Halcrow 		rc = -ENOMEM;
1600d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Unable to allocate page_virt\n",
1601d7cdc5feSMichael Halcrow 		       __FUNCTION__);
1602237fead6SMichael Halcrow 		goto out;
1603237fead6SMichael Halcrow 	}
1604d7cdc5feSMichael Halcrow 	rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1605d7cdc5feSMichael Halcrow 				 ecryptfs_inode);
1606d7cdc5feSMichael Halcrow 	if (!rc)
1607237fead6SMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1608dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1609dd2a3b7aSMichael Halcrow 						ECRYPTFS_VALIDATE_HEADER_SIZE);
1610dd2a3b7aSMichael Halcrow 	if (rc) {
1611d7cdc5feSMichael Halcrow 		rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
1612237fead6SMichael Halcrow 		if (rc) {
1613dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1614dd2a3b7aSMichael Halcrow 			       "file header region or xattr region\n");
1615237fead6SMichael Halcrow 			rc = -EINVAL;
1616dd2a3b7aSMichael Halcrow 			goto out;
1617dd2a3b7aSMichael Halcrow 		}
1618dd2a3b7aSMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1619dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1620dd2a3b7aSMichael Halcrow 						ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1621dd2a3b7aSMichael Halcrow 		if (rc) {
1622dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1623dd2a3b7aSMichael Halcrow 			       "file xattr region either\n");
1624dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1625dd2a3b7aSMichael Halcrow 		}
1626dd2a3b7aSMichael Halcrow 		if (crypt_stat->mount_crypt_stat->flags
1627dd2a3b7aSMichael Halcrow 		    & ECRYPTFS_XATTR_METADATA_ENABLED) {
1628dd2a3b7aSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1629dd2a3b7aSMichael Halcrow 		} else {
1630dd2a3b7aSMichael Halcrow 			printk(KERN_WARNING "Attempt to access file with "
1631dd2a3b7aSMichael Halcrow 			       "crypto metadata only in the extended attribute "
1632dd2a3b7aSMichael Halcrow 			       "region, but eCryptfs was mounted without "
1633dd2a3b7aSMichael Halcrow 			       "xattr support enabled. eCryptfs will not treat "
1634dd2a3b7aSMichael Halcrow 			       "this like an encrypted file.\n");
1635dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1636dd2a3b7aSMichael Halcrow 		}
1637237fead6SMichael Halcrow 	}
1638237fead6SMichael Halcrow out:
1639237fead6SMichael Halcrow 	if (page_virt) {
1640237fead6SMichael Halcrow 		memset(page_virt, 0, PAGE_CACHE_SIZE);
1641237fead6SMichael Halcrow 		kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1642237fead6SMichael Halcrow 	}
1643237fead6SMichael Halcrow 	return rc;
1644237fead6SMichael Halcrow }
1645237fead6SMichael Halcrow 
1646237fead6SMichael Halcrow /**
1647237fead6SMichael Halcrow  * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1648237fead6SMichael Halcrow  * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1649237fead6SMichael Halcrow  * @name: The plaintext name
1650237fead6SMichael Halcrow  * @length: The length of the plaintext
1651237fead6SMichael Halcrow  * @encoded_name: The encypted name
1652237fead6SMichael Halcrow  *
1653237fead6SMichael Halcrow  * Encrypts and encodes a filename into something that constitutes a
1654237fead6SMichael Halcrow  * valid filename for a filesystem, with printable characters.
1655237fead6SMichael Halcrow  *
1656237fead6SMichael Halcrow  * We assume that we have a properly initialized crypto context,
1657237fead6SMichael Halcrow  * pointed to by crypt_stat->tfm.
1658237fead6SMichael Halcrow  *
1659237fead6SMichael Halcrow  * TODO: Implement filename decoding and decryption here, in place of
1660237fead6SMichael Halcrow  * memcpy. We are keeping the framework around for now to (1)
1661237fead6SMichael Halcrow  * facilitate testing of the components needed to implement filename
1662237fead6SMichael Halcrow  * encryption and (2) to provide a code base from which other
1663237fead6SMichael Halcrow  * developers in the community can easily implement this feature.
1664237fead6SMichael Halcrow  *
1665237fead6SMichael Halcrow  * Returns the length of encoded filename; negative if error
1666237fead6SMichael Halcrow  */
1667237fead6SMichael Halcrow int
1668237fead6SMichael Halcrow ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1669237fead6SMichael Halcrow 			 const char *name, int length, char **encoded_name)
1670237fead6SMichael Halcrow {
1671237fead6SMichael Halcrow 	int error = 0;
1672237fead6SMichael Halcrow 
1673237fead6SMichael Halcrow 	(*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
1674237fead6SMichael Halcrow 	if (!(*encoded_name)) {
1675237fead6SMichael Halcrow 		error = -ENOMEM;
1676237fead6SMichael Halcrow 		goto out;
1677237fead6SMichael Halcrow 	}
1678237fead6SMichael Halcrow 	/* TODO: Filename encryption is a scheduled feature for a
1679237fead6SMichael Halcrow 	 * future version of eCryptfs. This function is here only for
1680237fead6SMichael Halcrow 	 * the purpose of providing a framework for other developers
1681237fead6SMichael Halcrow 	 * to easily implement filename encryption. Hint: Replace this
1682237fead6SMichael Halcrow 	 * memcpy() with a call to encrypt and encode the
1683237fead6SMichael Halcrow 	 * filename, the set the length accordingly. */
1684237fead6SMichael Halcrow 	memcpy((void *)(*encoded_name), (void *)name, length);
1685237fead6SMichael Halcrow 	(*encoded_name)[length] = '\0';
1686237fead6SMichael Halcrow 	error = length + 1;
1687237fead6SMichael Halcrow out:
1688237fead6SMichael Halcrow 	return error;
1689237fead6SMichael Halcrow }
1690237fead6SMichael Halcrow 
1691237fead6SMichael Halcrow /**
1692237fead6SMichael Halcrow  * ecryptfs_decode_filename - converts the cipher text name to plaintext
1693237fead6SMichael Halcrow  * @crypt_stat: The crypt_stat struct associated with the file
1694237fead6SMichael Halcrow  * @name: The filename in cipher text
1695237fead6SMichael Halcrow  * @length: The length of the cipher text name
1696237fead6SMichael Halcrow  * @decrypted_name: The plaintext name
1697237fead6SMichael Halcrow  *
1698237fead6SMichael Halcrow  * Decodes and decrypts the filename.
1699237fead6SMichael Halcrow  *
1700237fead6SMichael Halcrow  * We assume that we have a properly initialized crypto context,
1701237fead6SMichael Halcrow  * pointed to by crypt_stat->tfm.
1702237fead6SMichael Halcrow  *
1703237fead6SMichael Halcrow  * TODO: Implement filename decoding and decryption here, in place of
1704237fead6SMichael Halcrow  * memcpy. We are keeping the framework around for now to (1)
1705237fead6SMichael Halcrow  * facilitate testing of the components needed to implement filename
1706237fead6SMichael Halcrow  * encryption and (2) to provide a code base from which other
1707237fead6SMichael Halcrow  * developers in the community can easily implement this feature.
1708237fead6SMichael Halcrow  *
1709237fead6SMichael Halcrow  * Returns the length of decoded filename; negative if error
1710237fead6SMichael Halcrow  */
1711237fead6SMichael Halcrow int
1712237fead6SMichael Halcrow ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1713237fead6SMichael Halcrow 			 const char *name, int length, char **decrypted_name)
1714237fead6SMichael Halcrow {
1715237fead6SMichael Halcrow 	int error = 0;
1716237fead6SMichael Halcrow 
1717237fead6SMichael Halcrow 	(*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
1718237fead6SMichael Halcrow 	if (!(*decrypted_name)) {
1719237fead6SMichael Halcrow 		error = -ENOMEM;
1720237fead6SMichael Halcrow 		goto out;
1721237fead6SMichael Halcrow 	}
1722237fead6SMichael Halcrow 	/* TODO: Filename encryption is a scheduled feature for a
1723237fead6SMichael Halcrow 	 * future version of eCryptfs. This function is here only for
1724237fead6SMichael Halcrow 	 * the purpose of providing a framework for other developers
1725237fead6SMichael Halcrow 	 * to easily implement filename encryption. Hint: Replace this
1726237fead6SMichael Halcrow 	 * memcpy() with a call to decode and decrypt the
1727237fead6SMichael Halcrow 	 * filename, the set the length accordingly. */
1728237fead6SMichael Halcrow 	memcpy((void *)(*decrypted_name), (void *)name, length);
1729237fead6SMichael Halcrow 	(*decrypted_name)[length + 1] = '\0';	/* Only for convenience
1730237fead6SMichael Halcrow 						 * in printing out the
1731237fead6SMichael Halcrow 						 * string in debug
1732237fead6SMichael Halcrow 						 * messages */
1733237fead6SMichael Halcrow 	error = length;
1734237fead6SMichael Halcrow out:
1735237fead6SMichael Halcrow 	return error;
1736237fead6SMichael Halcrow }
1737237fead6SMichael Halcrow 
1738237fead6SMichael Halcrow /**
1739f4aad16aSMichael Halcrow  * ecryptfs_process_key_cipher - Perform key cipher initialization.
1740237fead6SMichael Halcrow  * @key_tfm: Crypto context for key material, set by this function
1741e5d9cbdeSMichael Halcrow  * @cipher_name: Name of the cipher
1742e5d9cbdeSMichael Halcrow  * @key_size: Size of the key in bytes
1743237fead6SMichael Halcrow  *
1744237fead6SMichael Halcrow  * Returns zero on success. Any crypto_tfm structs allocated here
1745237fead6SMichael Halcrow  * should be released by other functions, such as on a superblock put
1746237fead6SMichael Halcrow  * event, regardless of whether this function succeeds for fails.
1747237fead6SMichael Halcrow  */
1748cd9d67dfSMichael Halcrow static int
1749f4aad16aSMichael Halcrow ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1750f4aad16aSMichael Halcrow 			    char *cipher_name, size_t *key_size)
1751237fead6SMichael Halcrow {
1752237fead6SMichael Halcrow 	char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
17538bba066fSMichael Halcrow 	char *full_alg_name;
1754237fead6SMichael Halcrow 	int rc;
1755237fead6SMichael Halcrow 
1756e5d9cbdeSMichael Halcrow 	*key_tfm = NULL;
1757e5d9cbdeSMichael Halcrow 	if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1758237fead6SMichael Halcrow 		rc = -EINVAL;
1759237fead6SMichael Halcrow 		printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
1760e5d9cbdeSMichael Halcrow 		      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1761237fead6SMichael Halcrow 		goto out;
1762237fead6SMichael Halcrow 	}
17638bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
17648bba066fSMichael Halcrow 						    "ecb");
17658bba066fSMichael Halcrow 	if (rc)
17668bba066fSMichael Halcrow 		goto out;
17678bba066fSMichael Halcrow 	*key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
17688bba066fSMichael Halcrow 	kfree(full_alg_name);
17698bba066fSMichael Halcrow 	if (IS_ERR(*key_tfm)) {
17708bba066fSMichael Halcrow 		rc = PTR_ERR(*key_tfm);
1771237fead6SMichael Halcrow 		printk(KERN_ERR "Unable to allocate crypto cipher with name "
17728bba066fSMichael Halcrow 		       "[%s]; rc = [%d]\n", cipher_name, rc);
1773237fead6SMichael Halcrow 		goto out;
1774237fead6SMichael Halcrow 	}
17758bba066fSMichael Halcrow 	crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
17768bba066fSMichael Halcrow 	if (*key_size == 0) {
17778bba066fSMichael Halcrow 		struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
17788bba066fSMichael Halcrow 
17798bba066fSMichael Halcrow 		*key_size = alg->max_keysize;
17808bba066fSMichael Halcrow 	}
1781e5d9cbdeSMichael Halcrow 	get_random_bytes(dummy_key, *key_size);
17828bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
1783237fead6SMichael Halcrow 	if (rc) {
1784237fead6SMichael Halcrow 		printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
1785e5d9cbdeSMichael Halcrow 		       "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
1786237fead6SMichael Halcrow 		rc = -EINVAL;
1787237fead6SMichael Halcrow 		goto out;
1788237fead6SMichael Halcrow 	}
1789237fead6SMichael Halcrow out:
1790237fead6SMichael Halcrow 	return rc;
1791237fead6SMichael Halcrow }
1792f4aad16aSMichael Halcrow 
1793f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_tfm_cache;
1794f4aad16aSMichael Halcrow struct list_head key_tfm_list;
1795f4aad16aSMichael Halcrow struct mutex key_tfm_list_mutex;
1796f4aad16aSMichael Halcrow 
1797f4aad16aSMichael Halcrow int ecryptfs_init_crypto(void)
1798f4aad16aSMichael Halcrow {
1799f4aad16aSMichael Halcrow 	mutex_init(&key_tfm_list_mutex);
1800f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&key_tfm_list);
1801f4aad16aSMichael Halcrow 	return 0;
1802f4aad16aSMichael Halcrow }
1803f4aad16aSMichael Halcrow 
1804fcd12835SMichael Halcrow int ecryptfs_destroy_crypto(void)
1805f4aad16aSMichael Halcrow {
1806f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1807f4aad16aSMichael Halcrow 
1808f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1809f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1810f4aad16aSMichael Halcrow 				 key_tfm_list) {
1811f4aad16aSMichael Halcrow 		list_del(&key_tfm->key_tfm_list);
1812f4aad16aSMichael Halcrow 		if (key_tfm->key_tfm)
1813f4aad16aSMichael Halcrow 			crypto_free_blkcipher(key_tfm->key_tfm);
1814f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1815f4aad16aSMichael Halcrow 	}
1816f4aad16aSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
1817f4aad16aSMichael Halcrow 	return 0;
1818f4aad16aSMichael Halcrow }
1819f4aad16aSMichael Halcrow 
1820f4aad16aSMichael Halcrow int
1821f4aad16aSMichael Halcrow ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1822f4aad16aSMichael Halcrow 			 size_t key_size)
1823f4aad16aSMichael Halcrow {
1824f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *tmp_tfm;
1825f4aad16aSMichael Halcrow 	int rc = 0;
1826f4aad16aSMichael Halcrow 
1827f4aad16aSMichael Halcrow 	tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1828f4aad16aSMichael Halcrow 	if (key_tfm != NULL)
1829f4aad16aSMichael Halcrow 		(*key_tfm) = tmp_tfm;
1830f4aad16aSMichael Halcrow 	if (!tmp_tfm) {
1831f4aad16aSMichael Halcrow 		rc = -ENOMEM;
1832f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to allocate from "
1833f4aad16aSMichael Halcrow 		       "ecryptfs_key_tfm_cache\n");
1834f4aad16aSMichael Halcrow 		goto out;
1835f4aad16aSMichael Halcrow 	}
1836f4aad16aSMichael Halcrow 	mutex_init(&tmp_tfm->key_tfm_mutex);
1837f4aad16aSMichael Halcrow 	strncpy(tmp_tfm->cipher_name, cipher_name,
1838f4aad16aSMichael Halcrow 		ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1839f4aad16aSMichael Halcrow 	tmp_tfm->key_size = key_size;
18405dda6992SMichael Halcrow 	rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1841f4aad16aSMichael Halcrow 					 tmp_tfm->cipher_name,
18425dda6992SMichael Halcrow 					 &tmp_tfm->key_size);
18435dda6992SMichael Halcrow 	if (rc) {
1844f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to initialize key TFM "
1845f4aad16aSMichael Halcrow 		       "cipher with name = [%s]; rc = [%d]\n",
1846f4aad16aSMichael Halcrow 		       tmp_tfm->cipher_name, rc);
1847f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1848f4aad16aSMichael Halcrow 		if (key_tfm != NULL)
1849f4aad16aSMichael Halcrow 			(*key_tfm) = NULL;
1850f4aad16aSMichael Halcrow 		goto out;
1851f4aad16aSMichael Halcrow 	}
1852f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1853f4aad16aSMichael Halcrow 	list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1854f4aad16aSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
1855f4aad16aSMichael Halcrow out:
1856f4aad16aSMichael Halcrow 	return rc;
1857f4aad16aSMichael Halcrow }
1858f4aad16aSMichael Halcrow 
1859f4aad16aSMichael Halcrow int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1860f4aad16aSMichael Halcrow 					       struct mutex **tfm_mutex,
1861f4aad16aSMichael Halcrow 					       char *cipher_name)
1862f4aad16aSMichael Halcrow {
1863f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm;
1864f4aad16aSMichael Halcrow 	int rc = 0;
1865f4aad16aSMichael Halcrow 
1866f4aad16aSMichael Halcrow 	(*tfm) = NULL;
1867f4aad16aSMichael Halcrow 	(*tfm_mutex) = NULL;
1868f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1869f4aad16aSMichael Halcrow 	list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) {
1870f4aad16aSMichael Halcrow 		if (strcmp(key_tfm->cipher_name, cipher_name) == 0) {
1871f4aad16aSMichael Halcrow 			(*tfm) = key_tfm->key_tfm;
1872f4aad16aSMichael Halcrow 			(*tfm_mutex) = &key_tfm->key_tfm_mutex;
1873f4aad16aSMichael Halcrow 			mutex_unlock(&key_tfm_list_mutex);
1874f4aad16aSMichael Halcrow 			goto out;
1875f4aad16aSMichael Halcrow 		}
1876f4aad16aSMichael Halcrow 	}
1877f4aad16aSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
18785dda6992SMichael Halcrow 	rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
18795dda6992SMichael Halcrow 	if (rc) {
1880f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n",
1881f4aad16aSMichael Halcrow 		       rc);
1882f4aad16aSMichael Halcrow 		goto out;
1883f4aad16aSMichael Halcrow 	}
1884f4aad16aSMichael Halcrow 	(*tfm) = key_tfm->key_tfm;
1885f4aad16aSMichael Halcrow 	(*tfm_mutex) = &key_tfm->key_tfm_mutex;
1886f4aad16aSMichael Halcrow out:
1887f4aad16aSMichael Halcrow 	return rc;
1888f4aad16aSMichael Halcrow }
1889