xref: /openbmc/linux/fs/ecryptfs/crypto.c (revision e5d9cbde6ce0001e49994df5fcdcbeff8be8037b)
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
6237fead6SMichael Halcrow  * Copyright (C) 2004-2006 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 	int rc = 0;
98237fead6SMichael Halcrow 	struct scatterlist sg;
99237fead6SMichael Halcrow 
100237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_md5_tfm_mutex);
101237fead6SMichael Halcrow 	sg_init_one(&sg, (u8 *)src, len);
102237fead6SMichael Halcrow 	if (!crypt_stat->md5_tfm) {
103237fead6SMichael Halcrow 		crypt_stat->md5_tfm =
104237fead6SMichael Halcrow 			crypto_alloc_tfm("md5", CRYPTO_TFM_REQ_MAY_SLEEP);
105237fead6SMichael Halcrow 		if (!crypt_stat->md5_tfm) {
106237fead6SMichael Halcrow 			rc = -ENOMEM;
107237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
108237fead6SMichael Halcrow 					"allocate crypto context\n");
109237fead6SMichael Halcrow 			goto out;
110237fead6SMichael Halcrow 		}
111237fead6SMichael Halcrow 	}
112237fead6SMichael Halcrow 	crypto_digest_init(crypt_stat->md5_tfm);
113237fead6SMichael Halcrow 	crypto_digest_update(crypt_stat->md5_tfm, &sg, 1);
114237fead6SMichael Halcrow 	crypto_digest_final(crypt_stat->md5_tfm, dst);
115237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_md5_tfm_mutex);
116237fead6SMichael Halcrow out:
117237fead6SMichael Halcrow 	return rc;
118237fead6SMichael Halcrow }
119237fead6SMichael Halcrow 
120237fead6SMichael Halcrow /**
121237fead6SMichael Halcrow  * ecryptfs_derive_iv
122237fead6SMichael Halcrow  * @iv: destination for the derived iv vale
123237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
124237fead6SMichael Halcrow  * @offset: Offset of the page whose's iv we are to derive
125237fead6SMichael Halcrow  *
126237fead6SMichael Halcrow  * Generate the initialization vector from the given root IV and page
127237fead6SMichael Halcrow  * offset.
128237fead6SMichael Halcrow  *
129237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
130237fead6SMichael Halcrow  */
131237fead6SMichael Halcrow static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
132237fead6SMichael Halcrow 			      pgoff_t offset)
133237fead6SMichael Halcrow {
134237fead6SMichael Halcrow 	int rc = 0;
135237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
136237fead6SMichael Halcrow 	char src[ECRYPTFS_MAX_IV_BYTES + 16];
137237fead6SMichael Halcrow 
138237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
139237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "root iv:\n");
140237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
141237fead6SMichael Halcrow 	}
142237fead6SMichael Halcrow 	/* TODO: It is probably secure to just cast the least
143237fead6SMichael Halcrow 	 * significant bits of the root IV into an unsigned long and
144237fead6SMichael Halcrow 	 * add the offset to that rather than go through all this
145237fead6SMichael Halcrow 	 * hashing business. -Halcrow */
146237fead6SMichael Halcrow 	memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
147237fead6SMichael Halcrow 	memset((src + crypt_stat->iv_bytes), 0, 16);
148237fead6SMichael Halcrow 	snprintf((src + crypt_stat->iv_bytes), 16, "%ld", offset);
149237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
150237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "source:\n");
151237fead6SMichael Halcrow 		ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
152237fead6SMichael Halcrow 	}
153237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
154237fead6SMichael Halcrow 				    (crypt_stat->iv_bytes + 16));
155237fead6SMichael Halcrow 	if (rc) {
156237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
157237fead6SMichael Halcrow 				"MD5 while generating IV for a page\n");
158237fead6SMichael Halcrow 		goto out;
159237fead6SMichael Halcrow 	}
160237fead6SMichael Halcrow 	memcpy(iv, dst, crypt_stat->iv_bytes);
161237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
162237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
163237fead6SMichael Halcrow 		ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
164237fead6SMichael Halcrow 	}
165237fead6SMichael Halcrow out:
166237fead6SMichael Halcrow 	return rc;
167237fead6SMichael Halcrow }
168237fead6SMichael Halcrow 
169237fead6SMichael Halcrow /**
170237fead6SMichael Halcrow  * ecryptfs_init_crypt_stat
171237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
172237fead6SMichael Halcrow  *
173237fead6SMichael Halcrow  * Initialize the crypt_stat structure.
174237fead6SMichael Halcrow  */
175237fead6SMichael Halcrow void
176237fead6SMichael Halcrow ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
177237fead6SMichael Halcrow {
178237fead6SMichael Halcrow 	memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
179237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_mutex);
180237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_tfm_mutex);
181237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_md5_tfm_mutex);
182237fead6SMichael Halcrow 	ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_STRUCT_INITIALIZED);
183237fead6SMichael Halcrow }
184237fead6SMichael Halcrow 
185237fead6SMichael Halcrow /**
186237fead6SMichael Halcrow  * ecryptfs_destruct_crypt_stat
187237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
188237fead6SMichael Halcrow  *
189237fead6SMichael Halcrow  * Releases all memory associated with a crypt_stat struct.
190237fead6SMichael Halcrow  */
191237fead6SMichael Halcrow void ecryptfs_destruct_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
192237fead6SMichael Halcrow {
193237fead6SMichael Halcrow 	if (crypt_stat->tfm)
194237fead6SMichael Halcrow 		crypto_free_tfm(crypt_stat->tfm);
195237fead6SMichael Halcrow 	if (crypt_stat->md5_tfm)
196237fead6SMichael Halcrow 		crypto_free_tfm(crypt_stat->md5_tfm);
197237fead6SMichael Halcrow 	memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
198237fead6SMichael Halcrow }
199237fead6SMichael Halcrow 
200237fead6SMichael Halcrow void ecryptfs_destruct_mount_crypt_stat(
201237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
202237fead6SMichael Halcrow {
203237fead6SMichael Halcrow 	if (mount_crypt_stat->global_auth_tok_key)
204237fead6SMichael Halcrow 		key_put(mount_crypt_stat->global_auth_tok_key);
205237fead6SMichael Halcrow 	if (mount_crypt_stat->global_key_tfm)
206237fead6SMichael Halcrow 		crypto_free_tfm(mount_crypt_stat->global_key_tfm);
207237fead6SMichael Halcrow 	memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
208237fead6SMichael Halcrow }
209237fead6SMichael Halcrow 
210237fead6SMichael Halcrow /**
211237fead6SMichael Halcrow  * virt_to_scatterlist
212237fead6SMichael Halcrow  * @addr: Virtual address
213237fead6SMichael Halcrow  * @size: Size of data; should be an even multiple of the block size
214237fead6SMichael Halcrow  * @sg: Pointer to scatterlist array; set to NULL to obtain only
215237fead6SMichael Halcrow  *      the number of scatterlist structs required in array
216237fead6SMichael Halcrow  * @sg_size: Max array size
217237fead6SMichael Halcrow  *
218237fead6SMichael Halcrow  * Fills in a scatterlist array with page references for a passed
219237fead6SMichael Halcrow  * virtual address.
220237fead6SMichael Halcrow  *
221237fead6SMichael Halcrow  * Returns the number of scatterlist structs in array used
222237fead6SMichael Halcrow  */
223237fead6SMichael Halcrow int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
224237fead6SMichael Halcrow 			int sg_size)
225237fead6SMichael Halcrow {
226237fead6SMichael Halcrow 	int i = 0;
227237fead6SMichael Halcrow 	struct page *pg;
228237fead6SMichael Halcrow 	int offset;
229237fead6SMichael Halcrow 	int remainder_of_page;
230237fead6SMichael Halcrow 
231237fead6SMichael Halcrow 	while (size > 0 && i < sg_size) {
232237fead6SMichael Halcrow 		pg = virt_to_page(addr);
233237fead6SMichael Halcrow 		offset = offset_in_page(addr);
234237fead6SMichael Halcrow 		if (sg) {
235237fead6SMichael Halcrow 			sg[i].page = pg;
236237fead6SMichael Halcrow 			sg[i].offset = offset;
237237fead6SMichael Halcrow 		}
238237fead6SMichael Halcrow 		remainder_of_page = PAGE_CACHE_SIZE - offset;
239237fead6SMichael Halcrow 		if (size >= remainder_of_page) {
240237fead6SMichael Halcrow 			if (sg)
241237fead6SMichael Halcrow 				sg[i].length = remainder_of_page;
242237fead6SMichael Halcrow 			addr += remainder_of_page;
243237fead6SMichael Halcrow 			size -= remainder_of_page;
244237fead6SMichael Halcrow 		} else {
245237fead6SMichael Halcrow 			if (sg)
246237fead6SMichael Halcrow 				sg[i].length = size;
247237fead6SMichael Halcrow 			addr += size;
248237fead6SMichael Halcrow 			size = 0;
249237fead6SMichael Halcrow 		}
250237fead6SMichael Halcrow 		i++;
251237fead6SMichael Halcrow 	}
252237fead6SMichael Halcrow 	if (size > 0)
253237fead6SMichael Halcrow 		return -ENOMEM;
254237fead6SMichael Halcrow 	return i;
255237fead6SMichael Halcrow }
256237fead6SMichael Halcrow 
257237fead6SMichael Halcrow /**
258237fead6SMichael Halcrow  * encrypt_scatterlist
259237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
260237fead6SMichael Halcrow  * @dest_sg: Destination of encrypted data
261237fead6SMichael Halcrow  * @src_sg: Data to be encrypted
262237fead6SMichael Halcrow  * @size: Length of data to be encrypted
263237fead6SMichael Halcrow  * @iv: iv to use during encryption
264237fead6SMichael Halcrow  *
265237fead6SMichael Halcrow  * Returns the number of bytes encrypted; negative value on error
266237fead6SMichael Halcrow  */
267237fead6SMichael Halcrow static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
268237fead6SMichael Halcrow 			       struct scatterlist *dest_sg,
269237fead6SMichael Halcrow 			       struct scatterlist *src_sg, int size,
270237fead6SMichael Halcrow 			       unsigned char *iv)
271237fead6SMichael Halcrow {
272237fead6SMichael Halcrow 	int rc = 0;
273237fead6SMichael Halcrow 
274237fead6SMichael Halcrow 	BUG_ON(!crypt_stat || !crypt_stat->tfm
275237fead6SMichael Halcrow 	       || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
276237fead6SMichael Halcrow 				       ECRYPTFS_STRUCT_INITIALIZED));
277237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
278237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
279237fead6SMichael Halcrow 				crypt_stat->key_size);
280237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
281237fead6SMichael Halcrow 				  crypt_stat->key_size);
282237fead6SMichael Halcrow 	}
283237fead6SMichael Halcrow 	/* Consider doing this once, when the file is opened */
284237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
285237fead6SMichael Halcrow 	rc = crypto_cipher_setkey(crypt_stat->tfm, crypt_stat->key,
286237fead6SMichael Halcrow 				  crypt_stat->key_size);
287237fead6SMichael Halcrow 	if (rc) {
288237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
289237fead6SMichael Halcrow 				rc);
290237fead6SMichael Halcrow 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
291237fead6SMichael Halcrow 		rc = -EINVAL;
292237fead6SMichael Halcrow 		goto out;
293237fead6SMichael Halcrow 	}
294237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
295237fead6SMichael Halcrow 	crypto_cipher_encrypt_iv(crypt_stat->tfm, dest_sg, src_sg, size, iv);
296237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
297237fead6SMichael Halcrow out:
298237fead6SMichael Halcrow 	return rc;
299237fead6SMichael Halcrow }
300237fead6SMichael Halcrow 
301237fead6SMichael Halcrow static void
302237fead6SMichael Halcrow ecryptfs_extent_to_lwr_pg_idx_and_offset(unsigned long *lower_page_idx,
303237fead6SMichael Halcrow 					 int *byte_offset,
304237fead6SMichael Halcrow 					 struct ecryptfs_crypt_stat *crypt_stat,
305237fead6SMichael Halcrow 					 unsigned long extent_num)
306237fead6SMichael Halcrow {
307237fead6SMichael Halcrow 	unsigned long lower_extent_num;
308237fead6SMichael Halcrow 	int extents_occupied_by_headers_at_front;
309237fead6SMichael Halcrow 	int bytes_occupied_by_headers_at_front;
310237fead6SMichael Halcrow 	int extent_offset;
311237fead6SMichael Halcrow 	int extents_per_page;
312237fead6SMichael Halcrow 
313237fead6SMichael Halcrow 	bytes_occupied_by_headers_at_front =
314237fead6SMichael Halcrow 		( crypt_stat->header_extent_size
315237fead6SMichael Halcrow 		  * crypt_stat->num_header_extents_at_front );
316237fead6SMichael Halcrow 	extents_occupied_by_headers_at_front =
317237fead6SMichael Halcrow 		( bytes_occupied_by_headers_at_front
318237fead6SMichael Halcrow 		  / crypt_stat->extent_size );
319237fead6SMichael Halcrow 	lower_extent_num = extents_occupied_by_headers_at_front + extent_num;
320237fead6SMichael Halcrow 	extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
321237fead6SMichael Halcrow 	(*lower_page_idx) = lower_extent_num / extents_per_page;
322237fead6SMichael Halcrow 	extent_offset = lower_extent_num % extents_per_page;
323237fead6SMichael Halcrow 	(*byte_offset) = extent_offset * crypt_stat->extent_size;
324237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * crypt_stat->header_extent_size = "
325237fead6SMichael Halcrow 			"[%d]\n", crypt_stat->header_extent_size);
326237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * crypt_stat->"
327237fead6SMichael Halcrow 			"num_header_extents_at_front = [%d]\n",
328237fead6SMichael Halcrow 			crypt_stat->num_header_extents_at_front);
329237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * extents_occupied_by_headers_at_"
330237fead6SMichael Halcrow 			"front = [%d]\n", extents_occupied_by_headers_at_front);
331237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * lower_extent_num = [0x%.16x]\n",
332237fead6SMichael Halcrow 			lower_extent_num);
333237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * extents_per_page = [%d]\n",
334237fead6SMichael Halcrow 			extents_per_page);
335237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * (*lower_page_idx) = [0x%.16x]\n",
336237fead6SMichael Halcrow 			(*lower_page_idx));
337237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * extent_offset = [%d]\n",
338237fead6SMichael Halcrow 			extent_offset);
339237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * (*byte_offset) = [%d]\n",
340237fead6SMichael Halcrow 			(*byte_offset));
341237fead6SMichael Halcrow }
342237fead6SMichael Halcrow 
343237fead6SMichael Halcrow static int ecryptfs_write_out_page(struct ecryptfs_page_crypt_context *ctx,
344237fead6SMichael Halcrow 				   struct page *lower_page,
345237fead6SMichael Halcrow 				   struct inode *lower_inode,
346237fead6SMichael Halcrow 				   int byte_offset_in_page, int bytes_to_write)
347237fead6SMichael Halcrow {
348237fead6SMichael Halcrow 	int rc = 0;
349237fead6SMichael Halcrow 
350237fead6SMichael Halcrow 	if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
351237fead6SMichael Halcrow 		rc = ecryptfs_commit_lower_page(lower_page, lower_inode,
352237fead6SMichael Halcrow 						ctx->param.lower_file,
353237fead6SMichael Halcrow 						byte_offset_in_page,
354237fead6SMichael Halcrow 						bytes_to_write);
355237fead6SMichael Halcrow 		if (rc) {
356237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error calling lower "
357237fead6SMichael Halcrow 					"commit; rc = [%d]\n", rc);
358237fead6SMichael Halcrow 			goto out;
359237fead6SMichael Halcrow 		}
360237fead6SMichael Halcrow 	} else {
361237fead6SMichael Halcrow 		rc = ecryptfs_writepage_and_release_lower_page(lower_page,
362237fead6SMichael Halcrow 							       lower_inode,
363237fead6SMichael Halcrow 							       ctx->param.wbc);
364237fead6SMichael Halcrow 		if (rc) {
365237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error calling lower "
366237fead6SMichael Halcrow 					"writepage(); rc = [%d]\n", rc);
367237fead6SMichael Halcrow 			goto out;
368237fead6SMichael Halcrow 		}
369237fead6SMichael Halcrow 	}
370237fead6SMichael Halcrow out:
371237fead6SMichael Halcrow 	return rc;
372237fead6SMichael Halcrow }
373237fead6SMichael Halcrow 
374237fead6SMichael Halcrow static int ecryptfs_read_in_page(struct ecryptfs_page_crypt_context *ctx,
375237fead6SMichael Halcrow 				 struct page **lower_page,
376237fead6SMichael Halcrow 				 struct inode *lower_inode,
377237fead6SMichael Halcrow 				 unsigned long lower_page_idx,
378237fead6SMichael Halcrow 				 int byte_offset_in_page)
379237fead6SMichael Halcrow {
380237fead6SMichael Halcrow 	int rc = 0;
381237fead6SMichael Halcrow 
382237fead6SMichael Halcrow 	if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
383237fead6SMichael Halcrow 		/* TODO: Limit this to only the data extents that are
384237fead6SMichael Halcrow 		 * needed */
385237fead6SMichael Halcrow 		rc = ecryptfs_get_lower_page(lower_page, lower_inode,
386237fead6SMichael Halcrow 					     ctx->param.lower_file,
387237fead6SMichael Halcrow 					     lower_page_idx,
388237fead6SMichael Halcrow 					     byte_offset_in_page,
389237fead6SMichael Halcrow 					     (PAGE_CACHE_SIZE
390237fead6SMichael Halcrow 					      - byte_offset_in_page));
391237fead6SMichael Halcrow 		if (rc) {
392237fead6SMichael Halcrow 			ecryptfs_printk(
393237fead6SMichael Halcrow 				KERN_ERR, "Error attempting to grab, map, "
394237fead6SMichael Halcrow 				"and prepare_write lower page with index "
395237fead6SMichael Halcrow 				"[0x%.16x]; rc = [%d]\n", lower_page_idx, rc);
396237fead6SMichael Halcrow 			goto out;
397237fead6SMichael Halcrow 		}
398237fead6SMichael Halcrow 	} else {
399237fead6SMichael Halcrow 		rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL,
400237fead6SMichael Halcrow 						      lower_inode,
401237fead6SMichael Halcrow 						      lower_page_idx);
402237fead6SMichael Halcrow 		if (rc) {
403237fead6SMichael Halcrow 			ecryptfs_printk(
404237fead6SMichael Halcrow 				KERN_ERR, "Error attempting to grab and map "
405237fead6SMichael Halcrow 				"lower page with index [0x%.16x]; rc = [%d]\n",
406237fead6SMichael Halcrow 				lower_page_idx, rc);
407237fead6SMichael Halcrow 			goto out;
408237fead6SMichael Halcrow 		}
409237fead6SMichael Halcrow 	}
410237fead6SMichael Halcrow out:
411237fead6SMichael Halcrow 	return rc;
412237fead6SMichael Halcrow }
413237fead6SMichael Halcrow 
414237fead6SMichael Halcrow /**
415237fead6SMichael Halcrow  * ecryptfs_encrypt_page
416237fead6SMichael Halcrow  * @ctx: The context of the page
417237fead6SMichael Halcrow  *
418237fead6SMichael Halcrow  * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
419237fead6SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
420237fead6SMichael Halcrow  * if the file was created on a machine with an 8K page size
421237fead6SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
422237fead6SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
423237fead6SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
424237fead6SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
425237fead6SMichael Halcrow  *
426237fead6SMichael Halcrow  * The actual operations performed on each page depends on the
427237fead6SMichael Halcrow  * contents of the ecryptfs_page_crypt_context struct.
428237fead6SMichael Halcrow  *
429237fead6SMichael Halcrow  * Returns zero on success; negative on error
430237fead6SMichael Halcrow  */
431237fead6SMichael Halcrow int ecryptfs_encrypt_page(struct ecryptfs_page_crypt_context *ctx)
432237fead6SMichael Halcrow {
433237fead6SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
434237fead6SMichael Halcrow 	unsigned long base_extent;
435237fead6SMichael Halcrow 	unsigned long extent_offset = 0;
436237fead6SMichael Halcrow 	unsigned long lower_page_idx = 0;
437237fead6SMichael Halcrow 	unsigned long prior_lower_page_idx = 0;
438237fead6SMichael Halcrow 	struct page *lower_page;
439237fead6SMichael Halcrow 	struct inode *lower_inode;
440237fead6SMichael Halcrow 	struct ecryptfs_inode_info *inode_info;
441237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
442237fead6SMichael Halcrow 	int rc = 0;
443237fead6SMichael Halcrow 	int lower_byte_offset = 0;
444237fead6SMichael Halcrow 	int orig_byte_offset = 0;
445237fead6SMichael Halcrow 	int num_extents_per_page;
446237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_UNREAD    0
447237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_READ      1
448237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_MODIFIED  2
449237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_WRITTEN   3
450237fead6SMichael Halcrow 	int page_state;
451237fead6SMichael Halcrow 
452237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(ctx->page->mapping->host);
453237fead6SMichael Halcrow 	inode_info = ecryptfs_inode_to_private(ctx->page->mapping->host);
454237fead6SMichael Halcrow 	crypt_stat = &inode_info->crypt_stat;
455237fead6SMichael Halcrow 	if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)) {
456237fead6SMichael Halcrow 		rc = ecryptfs_copy_page_to_lower(ctx->page, lower_inode,
457237fead6SMichael Halcrow 						 ctx->param.lower_file);
458237fead6SMichael Halcrow 		if (rc)
459237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to copy "
460237fead6SMichael Halcrow 					"page at index [0x%.16x]\n",
461237fead6SMichael Halcrow 					ctx->page->index);
462237fead6SMichael Halcrow 		goto out;
463237fead6SMichael Halcrow 	}
464237fead6SMichael Halcrow 	num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
465237fead6SMichael Halcrow 	base_extent = (ctx->page->index * num_extents_per_page);
466237fead6SMichael Halcrow 	page_state = ECRYPTFS_PAGE_STATE_UNREAD;
467237fead6SMichael Halcrow 	while (extent_offset < num_extents_per_page) {
468237fead6SMichael Halcrow 		ecryptfs_extent_to_lwr_pg_idx_and_offset(
469237fead6SMichael Halcrow 			&lower_page_idx, &lower_byte_offset, crypt_stat,
470237fead6SMichael Halcrow 			(base_extent + extent_offset));
471237fead6SMichael Halcrow 		if (prior_lower_page_idx != lower_page_idx
472237fead6SMichael Halcrow 		    && page_state == ECRYPTFS_PAGE_STATE_MODIFIED) {
473237fead6SMichael Halcrow 			rc = ecryptfs_write_out_page(ctx, lower_page,
474237fead6SMichael Halcrow 						     lower_inode,
475237fead6SMichael Halcrow 						     orig_byte_offset,
476237fead6SMichael Halcrow 						     (PAGE_CACHE_SIZE
477237fead6SMichael Halcrow 						      - orig_byte_offset));
478237fead6SMichael Halcrow 			if (rc) {
479237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error attempting "
480237fead6SMichael Halcrow 						"to write out page; rc = [%d]"
481237fead6SMichael Halcrow 						"\n", rc);
482237fead6SMichael Halcrow 				goto out;
483237fead6SMichael Halcrow 			}
484237fead6SMichael Halcrow 			page_state = ECRYPTFS_PAGE_STATE_WRITTEN;
485237fead6SMichael Halcrow 		}
486237fead6SMichael Halcrow 		if (page_state == ECRYPTFS_PAGE_STATE_UNREAD
487237fead6SMichael Halcrow 		    || page_state == ECRYPTFS_PAGE_STATE_WRITTEN) {
488237fead6SMichael Halcrow 			rc = ecryptfs_read_in_page(ctx, &lower_page,
489237fead6SMichael Halcrow 						   lower_inode, lower_page_idx,
490237fead6SMichael Halcrow 						   lower_byte_offset);
491237fead6SMichael Halcrow 			if (rc) {
492237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error attempting "
493237fead6SMichael Halcrow 						"to read in lower page with "
494237fead6SMichael Halcrow 						"index [0x%.16x]; rc = [%d]\n",
495237fead6SMichael Halcrow 						lower_page_idx, rc);
496237fead6SMichael Halcrow 				goto out;
497237fead6SMichael Halcrow 			}
498237fead6SMichael Halcrow 			orig_byte_offset = lower_byte_offset;
499237fead6SMichael Halcrow 			prior_lower_page_idx = lower_page_idx;
500237fead6SMichael Halcrow 			page_state = ECRYPTFS_PAGE_STATE_READ;
501237fead6SMichael Halcrow 		}
502237fead6SMichael Halcrow 		BUG_ON(!(page_state == ECRYPTFS_PAGE_STATE_MODIFIED
503237fead6SMichael Halcrow 			 || page_state == ECRYPTFS_PAGE_STATE_READ));
504237fead6SMichael Halcrow 		rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
505237fead6SMichael Halcrow 					(base_extent + extent_offset));
506237fead6SMichael Halcrow 		if (rc) {
507237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
508237fead6SMichael Halcrow 					"derive IV for extent [0x%.16x]; "
509237fead6SMichael Halcrow 					"rc = [%d]\n",
510237fead6SMichael Halcrow 					(base_extent + extent_offset), rc);
511237fead6SMichael Halcrow 			goto out;
512237fead6SMichael Halcrow 		}
513237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
514237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
515237fead6SMichael Halcrow 					"with iv:\n");
516237fead6SMichael Halcrow 			ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
517237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
518237fead6SMichael Halcrow 					"encryption:\n");
519237fead6SMichael Halcrow 			ecryptfs_dump_hex((char *)
520237fead6SMichael Halcrow 					  (page_address(ctx->page)
521237fead6SMichael Halcrow 					   + (extent_offset
522237fead6SMichael Halcrow 					      * crypt_stat->extent_size)), 8);
523237fead6SMichael Halcrow 		}
524237fead6SMichael Halcrow 		rc = ecryptfs_encrypt_page_offset(
525237fead6SMichael Halcrow 			crypt_stat, lower_page, lower_byte_offset, ctx->page,
526237fead6SMichael Halcrow 			(extent_offset * crypt_stat->extent_size),
527237fead6SMichael Halcrow 			crypt_stat->extent_size, extent_iv);
528237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
529237fead6SMichael Halcrow 				"rc = [%d]\n",
530237fead6SMichael Halcrow 				(base_extent + extent_offset), rc);
531237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
532237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
533237fead6SMichael Halcrow 					"encryption:\n");
534237fead6SMichael Halcrow 			ecryptfs_dump_hex((char *)(page_address(lower_page)
535237fead6SMichael Halcrow 						   + lower_byte_offset), 8);
536237fead6SMichael Halcrow 		}
537237fead6SMichael Halcrow 		page_state = ECRYPTFS_PAGE_STATE_MODIFIED;
538237fead6SMichael Halcrow 		extent_offset++;
539237fead6SMichael Halcrow 	}
540237fead6SMichael Halcrow 	BUG_ON(orig_byte_offset != 0);
541237fead6SMichael Halcrow 	rc = ecryptfs_write_out_page(ctx, lower_page, lower_inode, 0,
542237fead6SMichael Halcrow 				     (lower_byte_offset
543237fead6SMichael Halcrow 				      + crypt_stat->extent_size));
544237fead6SMichael Halcrow 	if (rc) {
545237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to write out "
546237fead6SMichael Halcrow 				"page; rc = [%d]\n", rc);
547237fead6SMichael Halcrow 				goto out;
548237fead6SMichael Halcrow 	}
549237fead6SMichael Halcrow out:
550237fead6SMichael Halcrow 	return rc;
551237fead6SMichael Halcrow }
552237fead6SMichael Halcrow 
553237fead6SMichael Halcrow /**
554237fead6SMichael Halcrow  * ecryptfs_decrypt_page
555237fead6SMichael Halcrow  * @file: The ecryptfs file
556237fead6SMichael Halcrow  * @page: The page in ecryptfs to decrypt
557237fead6SMichael Halcrow  *
558237fead6SMichael Halcrow  * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
559237fead6SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
560237fead6SMichael Halcrow  * if the file was created on a machine with an 8K page size
561237fead6SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
562237fead6SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
563237fead6SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
564237fead6SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
565237fead6SMichael Halcrow  *
566237fead6SMichael Halcrow  * Returns zero on success; negative on error
567237fead6SMichael Halcrow  */
568237fead6SMichael Halcrow int ecryptfs_decrypt_page(struct file *file, struct page *page)
569237fead6SMichael Halcrow {
570237fead6SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
571237fead6SMichael Halcrow 	unsigned long base_extent;
572237fead6SMichael Halcrow 	unsigned long extent_offset = 0;
573237fead6SMichael Halcrow 	unsigned long lower_page_idx = 0;
574237fead6SMichael Halcrow 	unsigned long prior_lower_page_idx = 0;
575237fead6SMichael Halcrow 	struct page *lower_page;
576237fead6SMichael Halcrow 	char *lower_page_virt = NULL;
577237fead6SMichael Halcrow 	struct inode *lower_inode;
578237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
579237fead6SMichael Halcrow 	int rc = 0;
580237fead6SMichael Halcrow 	int byte_offset;
581237fead6SMichael Halcrow 	int num_extents_per_page;
582237fead6SMichael Halcrow 	int page_state;
583237fead6SMichael Halcrow 
584237fead6SMichael Halcrow 	crypt_stat = &(ecryptfs_inode_to_private(
585237fead6SMichael Halcrow 			       page->mapping->host)->crypt_stat);
586237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(page->mapping->host);
587237fead6SMichael Halcrow 	if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)) {
588237fead6SMichael Halcrow 		rc = ecryptfs_do_readpage(file, page, page->index);
589237fead6SMichael Halcrow 		if (rc)
590237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to copy "
591237fead6SMichael Halcrow 					"page at index [0x%.16x]\n",
592237fead6SMichael Halcrow 					page->index);
593237fead6SMichael Halcrow 		goto out;
594237fead6SMichael Halcrow 	}
595237fead6SMichael Halcrow 	num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
596237fead6SMichael Halcrow 	base_extent = (page->index * num_extents_per_page);
597237fead6SMichael Halcrow 	lower_page_virt = kmem_cache_alloc(ecryptfs_lower_page_cache,
598237fead6SMichael Halcrow 					   SLAB_KERNEL);
599237fead6SMichael Halcrow 	if (!lower_page_virt) {
600237fead6SMichael Halcrow 		rc = -ENOMEM;
601237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error getting page for encrypted "
602237fead6SMichael Halcrow 				"lower page(s)\n");
603237fead6SMichael Halcrow 		goto out;
604237fead6SMichael Halcrow 	}
605237fead6SMichael Halcrow 	lower_page = virt_to_page(lower_page_virt);
606237fead6SMichael Halcrow 	page_state = ECRYPTFS_PAGE_STATE_UNREAD;
607237fead6SMichael Halcrow 	while (extent_offset < num_extents_per_page) {
608237fead6SMichael Halcrow 		ecryptfs_extent_to_lwr_pg_idx_and_offset(
609237fead6SMichael Halcrow 			&lower_page_idx, &byte_offset, crypt_stat,
610237fead6SMichael Halcrow 			(base_extent + extent_offset));
611237fead6SMichael Halcrow 		if (prior_lower_page_idx != lower_page_idx
612237fead6SMichael Halcrow 		    || page_state == ECRYPTFS_PAGE_STATE_UNREAD) {
613237fead6SMichael Halcrow 			rc = ecryptfs_do_readpage(file, lower_page,
614237fead6SMichael Halcrow 						  lower_page_idx);
615237fead6SMichael Halcrow 			if (rc) {
616237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error reading "
617237fead6SMichael Halcrow 						"lower encrypted page; rc = "
618237fead6SMichael Halcrow 						"[%d]\n", rc);
619237fead6SMichael Halcrow 				goto out;
620237fead6SMichael Halcrow 			}
621237fead6SMichael Halcrow 			prior_lower_page_idx = lower_page_idx;
622237fead6SMichael Halcrow 			page_state = ECRYPTFS_PAGE_STATE_READ;
623237fead6SMichael Halcrow 		}
624237fead6SMichael Halcrow 		rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
625237fead6SMichael Halcrow 					(base_extent + extent_offset));
626237fead6SMichael Halcrow 		if (rc) {
627237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
628237fead6SMichael Halcrow 					"derive IV for extent [0x%.16x]; rc = "
629237fead6SMichael Halcrow 					"[%d]\n",
630237fead6SMichael Halcrow 					(base_extent + extent_offset), rc);
631237fead6SMichael Halcrow 			goto out;
632237fead6SMichael Halcrow 		}
633237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
634237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
635237fead6SMichael Halcrow 					"with iv:\n");
636237fead6SMichael Halcrow 			ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
637237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
638237fead6SMichael Halcrow 					"decryption:\n");
639237fead6SMichael Halcrow 			ecryptfs_dump_hex((lower_page_virt + byte_offset), 8);
640237fead6SMichael Halcrow 		}
641237fead6SMichael Halcrow 		rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
642237fead6SMichael Halcrow 						  (extent_offset
643237fead6SMichael Halcrow 						   * crypt_stat->extent_size),
644237fead6SMichael Halcrow 						  lower_page, byte_offset,
645237fead6SMichael Halcrow 						  crypt_stat->extent_size,
646237fead6SMichael Halcrow 						  extent_iv);
647237fead6SMichael Halcrow 		if (rc != crypt_stat->extent_size) {
648237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
649237fead6SMichael Halcrow 					"decrypt extent [0x%.16x]\n",
650237fead6SMichael Halcrow 					(base_extent + extent_offset));
651237fead6SMichael Halcrow 			goto out;
652237fead6SMichael Halcrow 		}
653237fead6SMichael Halcrow 		rc = 0;
654237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
655237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
656237fead6SMichael Halcrow 					"decryption:\n");
657237fead6SMichael Halcrow 			ecryptfs_dump_hex((char *)(page_address(page)
658237fead6SMichael Halcrow 						   + byte_offset), 8);
659237fead6SMichael Halcrow 		}
660237fead6SMichael Halcrow 		extent_offset++;
661237fead6SMichael Halcrow 	}
662237fead6SMichael Halcrow out:
663237fead6SMichael Halcrow 	if (lower_page_virt)
664237fead6SMichael Halcrow 		kmem_cache_free(ecryptfs_lower_page_cache, lower_page_virt);
665237fead6SMichael Halcrow 	return rc;
666237fead6SMichael Halcrow }
667237fead6SMichael Halcrow 
668237fead6SMichael Halcrow /**
669237fead6SMichael Halcrow  * decrypt_scatterlist
670237fead6SMichael Halcrow  *
671237fead6SMichael Halcrow  * Returns the number of bytes decrypted; negative value on error
672237fead6SMichael Halcrow  */
673237fead6SMichael Halcrow static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
674237fead6SMichael Halcrow 			       struct scatterlist *dest_sg,
675237fead6SMichael Halcrow 			       struct scatterlist *src_sg, int size,
676237fead6SMichael Halcrow 			       unsigned char *iv)
677237fead6SMichael Halcrow {
678237fead6SMichael Halcrow 	int rc = 0;
679237fead6SMichael Halcrow 
680237fead6SMichael Halcrow 	/* Consider doing this once, when the file is opened */
681237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
682237fead6SMichael Halcrow 	rc = crypto_cipher_setkey(crypt_stat->tfm, crypt_stat->key,
683237fead6SMichael Halcrow 				  crypt_stat->key_size);
684237fead6SMichael Halcrow 	if (rc) {
685237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
686237fead6SMichael Halcrow 				rc);
687237fead6SMichael Halcrow 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
688237fead6SMichael Halcrow 		rc = -EINVAL;
689237fead6SMichael Halcrow 		goto out;
690237fead6SMichael Halcrow 	}
691237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
692237fead6SMichael Halcrow 	rc = crypto_cipher_decrypt_iv(crypt_stat->tfm, dest_sg, src_sg, size,
693237fead6SMichael Halcrow 				      iv);
694237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
695237fead6SMichael Halcrow 	if (rc) {
696237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
697237fead6SMichael Halcrow 				rc);
698237fead6SMichael Halcrow 		goto out;
699237fead6SMichael Halcrow 	}
700237fead6SMichael Halcrow 	rc = size;
701237fead6SMichael Halcrow out:
702237fead6SMichael Halcrow 	return rc;
703237fead6SMichael Halcrow }
704237fead6SMichael Halcrow 
705237fead6SMichael Halcrow /**
706237fead6SMichael Halcrow  * ecryptfs_encrypt_page_offset
707237fead6SMichael Halcrow  *
708237fead6SMichael Halcrow  * Returns the number of bytes encrypted
709237fead6SMichael Halcrow  */
710237fead6SMichael Halcrow static int
711237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
712237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
713237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
714237fead6SMichael Halcrow 			     unsigned char *iv)
715237fead6SMichael Halcrow {
716237fead6SMichael Halcrow 	struct scatterlist src_sg, dst_sg;
717237fead6SMichael Halcrow 
718237fead6SMichael Halcrow 	src_sg.page = src_page;
719237fead6SMichael Halcrow 	src_sg.offset = src_offset;
720237fead6SMichael Halcrow 	src_sg.length = size;
721237fead6SMichael Halcrow 	dst_sg.page = dst_page;
722237fead6SMichael Halcrow 	dst_sg.offset = dst_offset;
723237fead6SMichael Halcrow 	dst_sg.length = size;
724237fead6SMichael Halcrow 	return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
725237fead6SMichael Halcrow }
726237fead6SMichael Halcrow 
727237fead6SMichael Halcrow /**
728237fead6SMichael Halcrow  * ecryptfs_decrypt_page_offset
729237fead6SMichael Halcrow  *
730237fead6SMichael Halcrow  * Returns the number of bytes decrypted
731237fead6SMichael Halcrow  */
732237fead6SMichael Halcrow static int
733237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
734237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
735237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
736237fead6SMichael Halcrow 			     unsigned char *iv)
737237fead6SMichael Halcrow {
738237fead6SMichael Halcrow 	struct scatterlist src_sg, dst_sg;
739237fead6SMichael Halcrow 
740237fead6SMichael Halcrow 	src_sg.page = src_page;
741237fead6SMichael Halcrow 	src_sg.offset = src_offset;
742237fead6SMichael Halcrow 	src_sg.length = size;
743237fead6SMichael Halcrow 	dst_sg.page = dst_page;
744237fead6SMichael Halcrow 	dst_sg.offset = dst_offset;
745237fead6SMichael Halcrow 	dst_sg.length = size;
746237fead6SMichael Halcrow 	return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
747237fead6SMichael Halcrow }
748237fead6SMichael Halcrow 
749237fead6SMichael Halcrow #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
750237fead6SMichael Halcrow 
751237fead6SMichael Halcrow /**
752237fead6SMichael Halcrow  * ecryptfs_init_crypt_ctx
753237fead6SMichael Halcrow  * @crypt_stat: Uninitilized crypt stats structure
754237fead6SMichael Halcrow  *
755237fead6SMichael Halcrow  * Initialize the crypto context.
756237fead6SMichael Halcrow  *
757237fead6SMichael Halcrow  * TODO: Performance: Keep a cache of initialized cipher contexts;
758237fead6SMichael Halcrow  * only init if needed
759237fead6SMichael Halcrow  */
760237fead6SMichael Halcrow int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
761237fead6SMichael Halcrow {
762237fead6SMichael Halcrow 	int rc = -EINVAL;
763237fead6SMichael Halcrow 
764237fead6SMichael Halcrow 	if (!crypt_stat->cipher) {
765237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "No cipher specified\n");
766237fead6SMichael Halcrow 		goto out;
767237fead6SMichael Halcrow 	}
768237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG,
769237fead6SMichael Halcrow 			"Initializing cipher [%s]; strlen = [%d]; "
770237fead6SMichael Halcrow 			"key_size_bits = [%d]\n",
771237fead6SMichael Halcrow 			crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
772237fead6SMichael Halcrow 			crypt_stat->key_size << 3);
773237fead6SMichael Halcrow 	if (crypt_stat->tfm) {
774237fead6SMichael Halcrow 		rc = 0;
775237fead6SMichael Halcrow 		goto out;
776237fead6SMichael Halcrow 	}
777237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
778237fead6SMichael Halcrow 	crypt_stat->tfm = crypto_alloc_tfm(crypt_stat->cipher,
779237fead6SMichael Halcrow 					   ECRYPTFS_DEFAULT_CHAINING_MODE
780237fead6SMichael Halcrow 					   | CRYPTO_TFM_REQ_WEAK_KEY);
781237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
782237fead6SMichael Halcrow 	if (!crypt_stat->tfm) {
783237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
784237fead6SMichael Halcrow 				"Error initializing cipher [%s]\n",
785237fead6SMichael Halcrow 				crypt_stat->cipher);
786237fead6SMichael Halcrow 		goto out;
787237fead6SMichael Halcrow 	}
788237fead6SMichael Halcrow 	rc = 0;
789237fead6SMichael Halcrow out:
790237fead6SMichael Halcrow 	return rc;
791237fead6SMichael Halcrow }
792237fead6SMichael Halcrow 
793237fead6SMichael Halcrow static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
794237fead6SMichael Halcrow {
795237fead6SMichael Halcrow 	int extent_size_tmp;
796237fead6SMichael Halcrow 
797237fead6SMichael Halcrow 	crypt_stat->extent_mask = 0xFFFFFFFF;
798237fead6SMichael Halcrow 	crypt_stat->extent_shift = 0;
799237fead6SMichael Halcrow 	if (crypt_stat->extent_size == 0)
800237fead6SMichael Halcrow 		return;
801237fead6SMichael Halcrow 	extent_size_tmp = crypt_stat->extent_size;
802237fead6SMichael Halcrow 	while ((extent_size_tmp & 0x01) == 0) {
803237fead6SMichael Halcrow 		extent_size_tmp >>= 1;
804237fead6SMichael Halcrow 		crypt_stat->extent_mask <<= 1;
805237fead6SMichael Halcrow 		crypt_stat->extent_shift++;
806237fead6SMichael Halcrow 	}
807237fead6SMichael Halcrow }
808237fead6SMichael Halcrow 
809237fead6SMichael Halcrow void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
810237fead6SMichael Halcrow {
811237fead6SMichael Halcrow 	/* Default values; may be overwritten as we are parsing the
812237fead6SMichael Halcrow 	 * packets. */
813237fead6SMichael Halcrow 	crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
814237fead6SMichael Halcrow 	set_extent_mask_and_shift(crypt_stat);
815237fead6SMichael Halcrow 	crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
816237fead6SMichael Halcrow 	if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
817237fead6SMichael Halcrow 		crypt_stat->header_extent_size =
818237fead6SMichael Halcrow 			ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
819237fead6SMichael Halcrow 	} else
820237fead6SMichael Halcrow 		crypt_stat->header_extent_size = PAGE_CACHE_SIZE;
821237fead6SMichael Halcrow 	crypt_stat->num_header_extents_at_front = 1;
822237fead6SMichael Halcrow }
823237fead6SMichael Halcrow 
824237fead6SMichael Halcrow /**
825237fead6SMichael Halcrow  * ecryptfs_compute_root_iv
826237fead6SMichael Halcrow  * @crypt_stats
827237fead6SMichael Halcrow  *
828237fead6SMichael Halcrow  * On error, sets the root IV to all 0's.
829237fead6SMichael Halcrow  */
830237fead6SMichael Halcrow int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
831237fead6SMichael Halcrow {
832237fead6SMichael Halcrow 	int rc = 0;
833237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
834237fead6SMichael Halcrow 
835237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
836237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes <= 0);
837237fead6SMichael Halcrow 	if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID)) {
838237fead6SMichael Halcrow 		rc = -EINVAL;
839237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Session key not valid; "
840237fead6SMichael Halcrow 				"cannot generate root IV\n");
841237fead6SMichael Halcrow 		goto out;
842237fead6SMichael Halcrow 	}
843237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
844237fead6SMichael Halcrow 				    crypt_stat->key_size);
845237fead6SMichael Halcrow 	if (rc) {
846237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
847237fead6SMichael Halcrow 				"MD5 while generating root IV\n");
848237fead6SMichael Halcrow 		goto out;
849237fead6SMichael Halcrow 	}
850237fead6SMichael Halcrow 	memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
851237fead6SMichael Halcrow out:
852237fead6SMichael Halcrow 	if (rc) {
853237fead6SMichael Halcrow 		memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
854237fead6SMichael Halcrow 		ECRYPTFS_SET_FLAG(crypt_stat->flags,
855237fead6SMichael Halcrow 				  ECRYPTFS_SECURITY_WARNING);
856237fead6SMichael Halcrow 	}
857237fead6SMichael Halcrow 	return rc;
858237fead6SMichael Halcrow }
859237fead6SMichael Halcrow 
860237fead6SMichael Halcrow static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
861237fead6SMichael Halcrow {
862237fead6SMichael Halcrow 	get_random_bytes(crypt_stat->key, crypt_stat->key_size);
863237fead6SMichael Halcrow 	ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
864237fead6SMichael Halcrow 	ecryptfs_compute_root_iv(crypt_stat);
865237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
866237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
867237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
868237fead6SMichael Halcrow 				  crypt_stat->key_size);
869237fead6SMichael Halcrow 	}
870237fead6SMichael Halcrow }
871237fead6SMichael Halcrow 
872237fead6SMichael Halcrow /**
873237fead6SMichael Halcrow  * ecryptfs_set_default_crypt_stat_vals
874237fead6SMichael Halcrow  * @crypt_stat
875237fead6SMichael Halcrow  *
876237fead6SMichael Halcrow  * Default values in the event that policy does not override them.
877237fead6SMichael Halcrow  */
878237fead6SMichael Halcrow static void ecryptfs_set_default_crypt_stat_vals(
879237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
880237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
881237fead6SMichael Halcrow {
882237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
883237fead6SMichael Halcrow 	strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
884237fead6SMichael Halcrow 	crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
885237fead6SMichael Halcrow 	ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
886237fead6SMichael Halcrow 	crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
887237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = mount_crypt_stat;
888237fead6SMichael Halcrow }
889237fead6SMichael Halcrow 
890237fead6SMichael Halcrow /**
891237fead6SMichael Halcrow  * ecryptfs_new_file_context
892237fead6SMichael Halcrow  * @ecryptfs_dentry
893237fead6SMichael Halcrow  *
894237fead6SMichael Halcrow  * If the crypto context for the file has not yet been established,
895237fead6SMichael Halcrow  * this is where we do that.  Establishing a new crypto context
896237fead6SMichael Halcrow  * involves the following decisions:
897237fead6SMichael Halcrow  *  - What cipher to use?
898237fead6SMichael Halcrow  *  - What set of authentication tokens to use?
899237fead6SMichael Halcrow  * Here we just worry about getting enough information into the
900237fead6SMichael Halcrow  * authentication tokens so that we know that they are available.
901237fead6SMichael Halcrow  * We associate the available authentication tokens with the new file
902237fead6SMichael Halcrow  * via the set of signatures in the crypt_stat struct.  Later, when
903237fead6SMichael Halcrow  * the headers are actually written out, we may again defer to
904237fead6SMichael Halcrow  * userspace to perform the encryption of the session key; for the
905237fead6SMichael Halcrow  * foreseeable future, this will be the case with public key packets.
906237fead6SMichael Halcrow  *
907237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
908237fead6SMichael Halcrow  */
909237fead6SMichael Halcrow /* Associate an authentication token(s) with the file */
910237fead6SMichael Halcrow int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
911237fead6SMichael Halcrow {
912237fead6SMichael Halcrow 	int rc = 0;
913237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
914237fead6SMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
915237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
916237fead6SMichael Halcrow 	    &ecryptfs_superblock_to_private(
917237fead6SMichael Halcrow 		    ecryptfs_dentry->d_sb)->mount_crypt_stat;
918237fead6SMichael Halcrow 	int cipher_name_len;
919237fead6SMichael Halcrow 
920237fead6SMichael Halcrow 	ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
921237fead6SMichael Halcrow 	/* See if there are mount crypt options */
922237fead6SMichael Halcrow 	if (mount_crypt_stat->global_auth_tok) {
923237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Initializing context for new "
924237fead6SMichael Halcrow 				"file using mount_crypt_stat\n");
925237fead6SMichael Halcrow 		ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
926237fead6SMichael Halcrow 		ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
927237fead6SMichael Halcrow 		memcpy(crypt_stat->keysigs[crypt_stat->num_keysigs++],
928237fead6SMichael Halcrow 		       mount_crypt_stat->global_auth_tok_sig,
929237fead6SMichael Halcrow 		       ECRYPTFS_SIG_SIZE_HEX);
930237fead6SMichael Halcrow 		cipher_name_len =
931237fead6SMichael Halcrow 		    strlen(mount_crypt_stat->global_default_cipher_name);
932237fead6SMichael Halcrow 		memcpy(crypt_stat->cipher,
933237fead6SMichael Halcrow 		       mount_crypt_stat->global_default_cipher_name,
934237fead6SMichael Halcrow 		       cipher_name_len);
935237fead6SMichael Halcrow 		crypt_stat->cipher[cipher_name_len] = '\0';
936237fead6SMichael Halcrow 		crypt_stat->key_size =
937237fead6SMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size;
938237fead6SMichael Halcrow 		ecryptfs_generate_new_key(crypt_stat);
939237fead6SMichael Halcrow 	} else
940237fead6SMichael Halcrow 		/* We should not encounter this scenario since we
941237fead6SMichael Halcrow 		 * should detect lack of global_auth_tok at mount time
942237fead6SMichael Halcrow 		 * TODO: Applies to 0.1 release only; remove in future
943237fead6SMichael Halcrow 		 * release */
944237fead6SMichael Halcrow 		BUG();
945237fead6SMichael Halcrow 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
946237fead6SMichael Halcrow 	if (rc)
947237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
948237fead6SMichael Halcrow 				"context for cipher [%s]: rc = [%d]\n",
949237fead6SMichael Halcrow 				crypt_stat->cipher, rc);
950237fead6SMichael Halcrow 	return rc;
951237fead6SMichael Halcrow }
952237fead6SMichael Halcrow 
953237fead6SMichael Halcrow /**
954237fead6SMichael Halcrow  * contains_ecryptfs_marker - check for the ecryptfs marker
955237fead6SMichael Halcrow  * @data: The data block in which to check
956237fead6SMichael Halcrow  *
957237fead6SMichael Halcrow  * Returns one if marker found; zero if not found
958237fead6SMichael Halcrow  */
959237fead6SMichael Halcrow int contains_ecryptfs_marker(char *data)
960237fead6SMichael Halcrow {
961237fead6SMichael Halcrow 	u32 m_1, m_2;
962237fead6SMichael Halcrow 
963237fead6SMichael Halcrow 	memcpy(&m_1, data, 4);
964237fead6SMichael Halcrow 	m_1 = be32_to_cpu(m_1);
965237fead6SMichael Halcrow 	memcpy(&m_2, (data + 4), 4);
966237fead6SMichael Halcrow 	m_2 = be32_to_cpu(m_2);
967237fead6SMichael Halcrow 	if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
968237fead6SMichael Halcrow 		return 1;
969237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
970237fead6SMichael Halcrow 			"MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
971237fead6SMichael Halcrow 			MAGIC_ECRYPTFS_MARKER);
972237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
973237fead6SMichael Halcrow 			"[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
974237fead6SMichael Halcrow 	return 0;
975237fead6SMichael Halcrow }
976237fead6SMichael Halcrow 
977237fead6SMichael Halcrow struct ecryptfs_flag_map_elem {
978237fead6SMichael Halcrow 	u32 file_flag;
979237fead6SMichael Halcrow 	u32 local_flag;
980237fead6SMichael Halcrow };
981237fead6SMichael Halcrow 
982237fead6SMichael Halcrow /* Add support for additional flags by adding elements here. */
983237fead6SMichael Halcrow static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
984237fead6SMichael Halcrow 	{0x00000001, ECRYPTFS_ENABLE_HMAC},
985237fead6SMichael Halcrow 	{0x00000002, ECRYPTFS_ENCRYPTED}
986237fead6SMichael Halcrow };
987237fead6SMichael Halcrow 
988237fead6SMichael Halcrow /**
989237fead6SMichael Halcrow  * ecryptfs_process_flags
990237fead6SMichael Halcrow  * @crypt_stat
991237fead6SMichael Halcrow  * @page_virt: Source data to be parsed
992237fead6SMichael Halcrow  * @bytes_read: Updated with the number of bytes read
993237fead6SMichael Halcrow  *
994237fead6SMichael Halcrow  * Returns zero on success; non-zero if the flag set is invalid
995237fead6SMichael Halcrow  */
996237fead6SMichael Halcrow static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
997237fead6SMichael Halcrow 				  char *page_virt, int *bytes_read)
998237fead6SMichael Halcrow {
999237fead6SMichael Halcrow 	int rc = 0;
1000237fead6SMichael Halcrow 	int i;
1001237fead6SMichael Halcrow 	u32 flags;
1002237fead6SMichael Halcrow 
1003237fead6SMichael Halcrow 	memcpy(&flags, page_virt, 4);
1004237fead6SMichael Halcrow 	flags = be32_to_cpu(flags);
1005237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1006237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
1007237fead6SMichael Halcrow 		if (flags & ecryptfs_flag_map[i].file_flag) {
1008237fead6SMichael Halcrow 			ECRYPTFS_SET_FLAG(crypt_stat->flags,
1009237fead6SMichael Halcrow 					  ecryptfs_flag_map[i].local_flag);
1010237fead6SMichael Halcrow 		} else
1011237fead6SMichael Halcrow 			ECRYPTFS_CLEAR_FLAG(crypt_stat->flags,
1012237fead6SMichael Halcrow 					    ecryptfs_flag_map[i].local_flag);
1013237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
1014237fead6SMichael Halcrow 	crypt_stat->file_version = ((flags >> 24) & 0xFF);
1015237fead6SMichael Halcrow 	(*bytes_read) = 4;
1016237fead6SMichael Halcrow 	return rc;
1017237fead6SMichael Halcrow }
1018237fead6SMichael Halcrow 
1019237fead6SMichael Halcrow /**
1020237fead6SMichael Halcrow  * write_ecryptfs_marker
1021237fead6SMichael Halcrow  * @page_virt: The pointer to in a page to begin writing the marker
1022237fead6SMichael Halcrow  * @written: Number of bytes written
1023237fead6SMichael Halcrow  *
1024237fead6SMichael Halcrow  * Marker = 0x3c81b7f5
1025237fead6SMichael Halcrow  */
1026237fead6SMichael Halcrow static void write_ecryptfs_marker(char *page_virt, size_t *written)
1027237fead6SMichael Halcrow {
1028237fead6SMichael Halcrow 	u32 m_1, m_2;
1029237fead6SMichael Halcrow 
1030237fead6SMichael Halcrow 	get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1031237fead6SMichael Halcrow 	m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1032237fead6SMichael Halcrow 	m_1 = cpu_to_be32(m_1);
1033237fead6SMichael Halcrow 	memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1034237fead6SMichael Halcrow 	m_2 = cpu_to_be32(m_2);
1035237fead6SMichael Halcrow 	memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
1036237fead6SMichael Halcrow 	       (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1037237fead6SMichael Halcrow 	(*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1038237fead6SMichael Halcrow }
1039237fead6SMichael Halcrow 
1040237fead6SMichael Halcrow static void
1041237fead6SMichael Halcrow write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1042237fead6SMichael Halcrow 		     size_t *written)
1043237fead6SMichael Halcrow {
1044237fead6SMichael Halcrow 	u32 flags = 0;
1045237fead6SMichael Halcrow 	int i;
1046237fead6SMichael Halcrow 
1047237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1048237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
1049237fead6SMichael Halcrow 		if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
1050237fead6SMichael Halcrow 					ecryptfs_flag_map[i].local_flag))
1051237fead6SMichael Halcrow 			flags |= ecryptfs_flag_map[i].file_flag;
1052237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
1053237fead6SMichael Halcrow 	flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1054237fead6SMichael Halcrow 	flags = cpu_to_be32(flags);
1055237fead6SMichael Halcrow 	memcpy(page_virt, &flags, 4);
1056237fead6SMichael Halcrow 	(*written) = 4;
1057237fead6SMichael Halcrow }
1058237fead6SMichael Halcrow 
1059237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem {
1060237fead6SMichael Halcrow 	char cipher_str[16];
1061237fead6SMichael Halcrow 	u16 cipher_code;
1062237fead6SMichael Halcrow };
1063237fead6SMichael Halcrow 
1064237fead6SMichael Halcrow /* Add support for additional ciphers by adding elements here. The
1065237fead6SMichael Halcrow  * cipher_code is whatever OpenPGP applicatoins use to identify the
1066237fead6SMichael Halcrow  * ciphers. List in order of probability. */
1067237fead6SMichael Halcrow static struct ecryptfs_cipher_code_str_map_elem
1068237fead6SMichael Halcrow ecryptfs_cipher_code_str_map[] = {
1069237fead6SMichael Halcrow 	{"aes",RFC2440_CIPHER_AES_128 },
1070237fead6SMichael Halcrow 	{"blowfish", RFC2440_CIPHER_BLOWFISH},
1071237fead6SMichael Halcrow 	{"des3_ede", RFC2440_CIPHER_DES3_EDE},
1072237fead6SMichael Halcrow 	{"cast5", RFC2440_CIPHER_CAST_5},
1073237fead6SMichael Halcrow 	{"twofish", RFC2440_CIPHER_TWOFISH},
1074237fead6SMichael Halcrow 	{"cast6", RFC2440_CIPHER_CAST_6},
1075237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_192},
1076237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_256}
1077237fead6SMichael Halcrow };
1078237fead6SMichael Halcrow 
1079237fead6SMichael Halcrow /**
1080237fead6SMichael Halcrow  * ecryptfs_code_for_cipher_string
1081237fead6SMichael Halcrow  * @str: The string representing the cipher name
1082237fead6SMichael Halcrow  *
1083237fead6SMichael Halcrow  * Returns zero on no match, or the cipher code on match
1084237fead6SMichael Halcrow  */
1085237fead6SMichael Halcrow u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
1086237fead6SMichael Halcrow {
1087237fead6SMichael Halcrow 	int i;
1088237fead6SMichael Halcrow 	u16 code = 0;
1089237fead6SMichael Halcrow 	struct ecryptfs_cipher_code_str_map_elem *map =
1090237fead6SMichael Halcrow 		ecryptfs_cipher_code_str_map;
1091237fead6SMichael Halcrow 
1092237fead6SMichael Halcrow 	if (strcmp(crypt_stat->cipher, "aes") == 0) {
1093237fead6SMichael Halcrow 		switch (crypt_stat->key_size) {
1094237fead6SMichael Halcrow 		case 16:
1095237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_128;
1096237fead6SMichael Halcrow 			break;
1097237fead6SMichael Halcrow 		case 24:
1098237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_192;
1099237fead6SMichael Halcrow 			break;
1100237fead6SMichael Halcrow 		case 32:
1101237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_256;
1102237fead6SMichael Halcrow 		}
1103237fead6SMichael Halcrow 	} else {
1104237fead6SMichael Halcrow 		for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1105237fead6SMichael Halcrow 			if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
1106237fead6SMichael Halcrow 				code = map[i].cipher_code;
1107237fead6SMichael Halcrow 				break;
1108237fead6SMichael Halcrow 			}
1109237fead6SMichael Halcrow 	}
1110237fead6SMichael Halcrow 	return code;
1111237fead6SMichael Halcrow }
1112237fead6SMichael Halcrow 
1113237fead6SMichael Halcrow /**
1114237fead6SMichael Halcrow  * ecryptfs_cipher_code_to_string
1115237fead6SMichael Halcrow  * @str: Destination to write out the cipher name
1116237fead6SMichael Halcrow  * @cipher_code: The code to convert to cipher name string
1117237fead6SMichael Halcrow  *
1118237fead6SMichael Halcrow  * Returns zero on success
1119237fead6SMichael Halcrow  */
1120237fead6SMichael Halcrow int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
1121237fead6SMichael Halcrow {
1122237fead6SMichael Halcrow 	int rc = 0;
1123237fead6SMichael Halcrow 	int i;
1124237fead6SMichael Halcrow 
1125237fead6SMichael Halcrow 	str[0] = '\0';
1126237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1127237fead6SMichael Halcrow 		if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1128237fead6SMichael Halcrow 			strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1129237fead6SMichael Halcrow 	if (str[0] == '\0') {
1130237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1131237fead6SMichael Halcrow 				"[%d]\n", cipher_code);
1132237fead6SMichael Halcrow 		rc = -EINVAL;
1133237fead6SMichael Halcrow 	}
1134237fead6SMichael Halcrow 	return rc;
1135237fead6SMichael Halcrow }
1136237fead6SMichael Halcrow 
1137237fead6SMichael Halcrow /**
1138237fead6SMichael Halcrow  * ecryptfs_read_header_region
1139237fead6SMichael Halcrow  * @data
1140237fead6SMichael Halcrow  * @dentry
1141237fead6SMichael Halcrow  * @nd
1142237fead6SMichael Halcrow  *
1143237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
1144237fead6SMichael Halcrow  */
1145237fead6SMichael Halcrow int ecryptfs_read_header_region(char *data, struct dentry *dentry,
1146237fead6SMichael Halcrow 				struct vfsmount *mnt)
1147237fead6SMichael Halcrow {
1148237fead6SMichael Halcrow 	struct file *file;
1149237fead6SMichael Halcrow 	mm_segment_t oldfs;
1150237fead6SMichael Halcrow 	int rc;
1151237fead6SMichael Halcrow 
1152237fead6SMichael Halcrow 	mnt = mntget(mnt);
1153237fead6SMichael Halcrow 	file = dentry_open(dentry, mnt, O_RDONLY);
1154237fead6SMichael Halcrow 	if (IS_ERR(file)) {
1155237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Error opening file to "
1156237fead6SMichael Halcrow 				"read header region\n");
1157237fead6SMichael Halcrow 		mntput(mnt);
1158237fead6SMichael Halcrow 		rc = PTR_ERR(file);
1159237fead6SMichael Halcrow 		goto out;
1160237fead6SMichael Halcrow 	}
1161237fead6SMichael Halcrow 	file->f_pos = 0;
1162237fead6SMichael Halcrow 	oldfs = get_fs();
1163237fead6SMichael Halcrow 	set_fs(get_ds());
1164237fead6SMichael Halcrow 	/* For releases 0.1 and 0.2, all of the header information
1165237fead6SMichael Halcrow 	 * fits in the first data extent-sized region. */
1166237fead6SMichael Halcrow 	rc = file->f_op->read(file, (char __user *)data,
1167237fead6SMichael Halcrow 			      ECRYPTFS_DEFAULT_EXTENT_SIZE, &file->f_pos);
1168237fead6SMichael Halcrow 	set_fs(oldfs);
1169237fead6SMichael Halcrow 	fput(file);
1170237fead6SMichael Halcrow 	rc = 0;
1171237fead6SMichael Halcrow out:
1172237fead6SMichael Halcrow 	return rc;
1173237fead6SMichael Halcrow }
1174237fead6SMichael Halcrow 
1175237fead6SMichael Halcrow static void
1176237fead6SMichael Halcrow write_header_metadata(char *virt, struct ecryptfs_crypt_stat *crypt_stat,
1177237fead6SMichael Halcrow 		      size_t *written)
1178237fead6SMichael Halcrow {
1179237fead6SMichael Halcrow 	u32 header_extent_size;
1180237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1181237fead6SMichael Halcrow 
1182237fead6SMichael Halcrow 	header_extent_size = (u32)crypt_stat->header_extent_size;
1183237fead6SMichael Halcrow 	num_header_extents_at_front =
1184237fead6SMichael Halcrow 		(u16)crypt_stat->num_header_extents_at_front;
1185237fead6SMichael Halcrow 	header_extent_size = cpu_to_be32(header_extent_size);
1186237fead6SMichael Halcrow 	memcpy(virt, &header_extent_size, 4);
1187237fead6SMichael Halcrow 	virt += 4;
1188237fead6SMichael Halcrow 	num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
1189237fead6SMichael Halcrow 	memcpy(virt, &num_header_extents_at_front, 2);
1190237fead6SMichael Halcrow 	(*written) = 6;
1191237fead6SMichael Halcrow }
1192237fead6SMichael Halcrow 
1193237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_0;
1194237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_1;
1195237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_2;
1196237fead6SMichael Halcrow 
1197237fead6SMichael Halcrow /**
1198237fead6SMichael Halcrow  * ecryptfs_write_headers_virt
1199237fead6SMichael Halcrow  * @page_virt
1200237fead6SMichael Halcrow  * @crypt_stat
1201237fead6SMichael Halcrow  * @ecryptfs_dentry
1202237fead6SMichael Halcrow  *
1203237fead6SMichael Halcrow  * Format version: 1
1204237fead6SMichael Halcrow  *
1205237fead6SMichael Halcrow  *   Header Extent:
1206237fead6SMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
1207237fead6SMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
1208237fead6SMichael Halcrow  *     Octets 16-19:      Flags
1209237fead6SMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
1210237fead6SMichael Halcrow  *      Octets 17-18:     Reserved
1211237fead6SMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
1212237fead6SMichael Halcrow  *                        Bit 2: Encrypted?
1213237fead6SMichael Halcrow  *                        Bits 3-8: Reserved
1214237fead6SMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
1215237fead6SMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
1216237fead6SMichael Halcrow  *                        (big-endian)
1217237fead6SMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
1218237fead6SMichael Halcrow  *   Data Extent 0:
1219237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1220237fead6SMichael Halcrow  *   Data Extent 1:
1221237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1222237fead6SMichael Halcrow  *   ...
1223237fead6SMichael Halcrow  *
1224237fead6SMichael Halcrow  * Returns zero on success
1225237fead6SMichael Halcrow  */
1226237fead6SMichael Halcrow int ecryptfs_write_headers_virt(char *page_virt,
1227237fead6SMichael Halcrow 				struct ecryptfs_crypt_stat *crypt_stat,
1228237fead6SMichael Halcrow 				struct dentry *ecryptfs_dentry)
1229237fead6SMichael Halcrow {
1230237fead6SMichael Halcrow 	int rc;
1231237fead6SMichael Halcrow 	size_t written;
1232237fead6SMichael Halcrow 	size_t offset;
1233237fead6SMichael Halcrow 
1234237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1235237fead6SMichael Halcrow 	write_ecryptfs_marker((page_virt + offset), &written);
1236237fead6SMichael Halcrow 	offset += written;
1237237fead6SMichael Halcrow 	write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1238237fead6SMichael Halcrow 	offset += written;
1239237fead6SMichael Halcrow 	write_header_metadata((page_virt + offset), crypt_stat, &written);
1240237fead6SMichael Halcrow 	offset += written;
1241237fead6SMichael Halcrow 	rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1242237fead6SMichael Halcrow 					      ecryptfs_dentry, &written,
1243237fead6SMichael Halcrow 					      PAGE_CACHE_SIZE - offset);
1244237fead6SMichael Halcrow 	if (rc)
1245237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1246237fead6SMichael Halcrow 				"set; rc = [%d]\n", rc);
1247237fead6SMichael Halcrow 	return rc;
1248237fead6SMichael Halcrow }
1249237fead6SMichael Halcrow 
1250237fead6SMichael Halcrow /**
1251237fead6SMichael Halcrow  * ecryptfs_write_headers
1252237fead6SMichael Halcrow  * @lower_file: The lower file struct, which was returned from dentry_open
1253237fead6SMichael Halcrow  *
1254237fead6SMichael Halcrow  * Write the file headers out.  This will likely involve a userspace
1255237fead6SMichael Halcrow  * callout, in which the session key is encrypted with one or more
1256237fead6SMichael Halcrow  * public keys and/or the passphrase necessary to do the encryption is
1257237fead6SMichael Halcrow  * retrieved via a prompt.  Exactly what happens at this point should
1258237fead6SMichael Halcrow  * be policy-dependent.
1259237fead6SMichael Halcrow  *
1260237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
1261237fead6SMichael Halcrow  */
1262237fead6SMichael Halcrow int ecryptfs_write_headers(struct dentry *ecryptfs_dentry,
1263237fead6SMichael Halcrow 			   struct file *lower_file)
1264237fead6SMichael Halcrow {
1265237fead6SMichael Halcrow 	mm_segment_t oldfs;
1266237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
1267237fead6SMichael Halcrow 	char *page_virt;
1268237fead6SMichael Halcrow 	int current_header_page;
1269237fead6SMichael Halcrow 	int header_pages;
1270237fead6SMichael Halcrow 	int rc = 0;
1271237fead6SMichael Halcrow 
1272237fead6SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(
1273237fead6SMichael Halcrow 		ecryptfs_dentry->d_inode)->crypt_stat;
1274237fead6SMichael Halcrow 	if (likely(ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
1275237fead6SMichael Halcrow 				       ECRYPTFS_ENCRYPTED))) {
1276237fead6SMichael Halcrow 		if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
1277237fead6SMichael Halcrow 					 ECRYPTFS_KEY_VALID)) {
1278237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "Key is "
1279237fead6SMichael Halcrow 					"invalid; bailing out\n");
1280237fead6SMichael Halcrow 			rc = -EINVAL;
1281237fead6SMichael Halcrow 			goto out;
1282237fead6SMichael Halcrow 		}
1283237fead6SMichael Halcrow 	} else {
1284237fead6SMichael Halcrow 		rc = -EINVAL;
1285237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING,
1286237fead6SMichael Halcrow 				"Called with crypt_stat->encrypted == 0\n");
1287237fead6SMichael Halcrow 		goto out;
1288237fead6SMichael Halcrow 	}
1289237fead6SMichael Halcrow 	/* Released in this function */
1290237fead6SMichael Halcrow 	page_virt = kmem_cache_alloc(ecryptfs_header_cache_0, SLAB_USER);
1291237fead6SMichael Halcrow 	if (!page_virt) {
1292237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Out of memory\n");
1293237fead6SMichael Halcrow 		rc = -ENOMEM;
1294237fead6SMichael Halcrow 		goto out;
1295237fead6SMichael Halcrow 	}
1296237fead6SMichael Halcrow 	memset(page_virt, 0, PAGE_CACHE_SIZE);
1297237fead6SMichael Halcrow 	rc = ecryptfs_write_headers_virt(page_virt, crypt_stat,
1298237fead6SMichael Halcrow 					 ecryptfs_dentry);
1299237fead6SMichael Halcrow 	if (unlikely(rc)) {
1300237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
1301237fead6SMichael Halcrow 		memset(page_virt, 0, PAGE_CACHE_SIZE);
1302237fead6SMichael Halcrow 		goto out_free;
1303237fead6SMichael Halcrow 	}
1304237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG,
1305237fead6SMichael Halcrow 			"Writing key packet set to underlying file\n");
1306237fead6SMichael Halcrow 	lower_file->f_pos = 0;
1307237fead6SMichael Halcrow 	oldfs = get_fs();
1308237fead6SMichael Halcrow 	set_fs(get_ds());
1309237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->"
1310237fead6SMichael Halcrow 			"write() w/ header page; lower_file->f_pos = "
1311237fead6SMichael Halcrow 			"[0x%.16x]\n", lower_file->f_pos);
1312237fead6SMichael Halcrow 	lower_file->f_op->write(lower_file, (char __user *)page_virt,
1313237fead6SMichael Halcrow 				PAGE_CACHE_SIZE, &lower_file->f_pos);
1314237fead6SMichael Halcrow 	header_pages = ((crypt_stat->header_extent_size
1315237fead6SMichael Halcrow 			 * crypt_stat->num_header_extents_at_front)
1316237fead6SMichael Halcrow 			/ PAGE_CACHE_SIZE);
1317237fead6SMichael Halcrow 	memset(page_virt, 0, PAGE_CACHE_SIZE);
1318237fead6SMichael Halcrow 	current_header_page = 1;
1319237fead6SMichael Halcrow 	while (current_header_page < header_pages) {
1320237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->"
1321237fead6SMichael Halcrow 				"write() w/ zero'd page; lower_file->f_pos = "
1322237fead6SMichael Halcrow 				"[0x%.16x]\n", lower_file->f_pos);
1323237fead6SMichael Halcrow 		lower_file->f_op->write(lower_file, (char __user *)page_virt,
1324237fead6SMichael Halcrow 					PAGE_CACHE_SIZE, &lower_file->f_pos);
1325237fead6SMichael Halcrow 		current_header_page++;
1326237fead6SMichael Halcrow 	}
1327237fead6SMichael Halcrow 	set_fs(oldfs);
1328237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG,
1329237fead6SMichael Halcrow 			"Done writing key packet set to underlying file.\n");
1330237fead6SMichael Halcrow out_free:
1331237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_header_cache_0, page_virt);
1332237fead6SMichael Halcrow out:
1333237fead6SMichael Halcrow 	return rc;
1334237fead6SMichael Halcrow }
1335237fead6SMichael Halcrow 
1336237fead6SMichael Halcrow static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
1337237fead6SMichael Halcrow 				 char *virt, int *bytes_read)
1338237fead6SMichael Halcrow {
1339237fead6SMichael Halcrow 	int rc = 0;
1340237fead6SMichael Halcrow 	u32 header_extent_size;
1341237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1342237fead6SMichael Halcrow 
1343237fead6SMichael Halcrow 	memcpy(&header_extent_size, virt, 4);
1344237fead6SMichael Halcrow 	header_extent_size = be32_to_cpu(header_extent_size);
1345237fead6SMichael Halcrow 	virt += 4;
1346237fead6SMichael Halcrow 	memcpy(&num_header_extents_at_front, virt, 2);
1347237fead6SMichael Halcrow 	num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
1348237fead6SMichael Halcrow 	crypt_stat->header_extent_size = (int)header_extent_size;
1349237fead6SMichael Halcrow 	crypt_stat->num_header_extents_at_front =
1350237fead6SMichael Halcrow 		(int)num_header_extents_at_front;
1351237fead6SMichael Halcrow 	(*bytes_read) = 6;
1352237fead6SMichael Halcrow 	if ((crypt_stat->header_extent_size
1353237fead6SMichael Halcrow 	     * crypt_stat->num_header_extents_at_front)
1354237fead6SMichael Halcrow 	    < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
1355237fead6SMichael Halcrow 		rc = -EINVAL;
1356237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Invalid header extent size: "
1357237fead6SMichael Halcrow 				"[%d]\n", crypt_stat->header_extent_size);
1358237fead6SMichael Halcrow 	}
1359237fead6SMichael Halcrow 	return rc;
1360237fead6SMichael Halcrow }
1361237fead6SMichael Halcrow 
1362237fead6SMichael Halcrow /**
1363237fead6SMichael Halcrow  * set_default_header_data
1364237fead6SMichael Halcrow  *
1365237fead6SMichael Halcrow  * For version 0 file format; this function is only for backwards
1366237fead6SMichael Halcrow  * compatibility for files created with the prior versions of
1367237fead6SMichael Halcrow  * eCryptfs.
1368237fead6SMichael Halcrow  */
1369237fead6SMichael Halcrow static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1370237fead6SMichael Halcrow {
1371237fead6SMichael Halcrow 	crypt_stat->header_extent_size = 4096;
1372237fead6SMichael Halcrow 	crypt_stat->num_header_extents_at_front = 1;
1373237fead6SMichael Halcrow }
1374237fead6SMichael Halcrow 
1375237fead6SMichael Halcrow /**
1376237fead6SMichael Halcrow  * ecryptfs_read_headers_virt
1377237fead6SMichael Halcrow  *
1378237fead6SMichael Halcrow  * Read/parse the header data. The header format is detailed in the
1379237fead6SMichael Halcrow  * comment block for the ecryptfs_write_headers_virt() function.
1380237fead6SMichael Halcrow  *
1381237fead6SMichael Halcrow  * Returns zero on success
1382237fead6SMichael Halcrow  */
1383237fead6SMichael Halcrow static int ecryptfs_read_headers_virt(char *page_virt,
1384237fead6SMichael Halcrow 				      struct ecryptfs_crypt_stat *crypt_stat,
1385237fead6SMichael Halcrow 				      struct dentry *ecryptfs_dentry)
1386237fead6SMichael Halcrow {
1387237fead6SMichael Halcrow 	int rc = 0;
1388237fead6SMichael Halcrow 	int offset;
1389237fead6SMichael Halcrow 	int bytes_read;
1390237fead6SMichael Halcrow 
1391237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
1392237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1393237fead6SMichael Halcrow 		ecryptfs_dentry->d_sb)->mount_crypt_stat;
1394237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1395237fead6SMichael Halcrow 	rc = contains_ecryptfs_marker(page_virt + offset);
1396237fead6SMichael Halcrow 	if (rc == 0) {
1397237fead6SMichael Halcrow 		rc = -EINVAL;
1398237fead6SMichael Halcrow 		goto out;
1399237fead6SMichael Halcrow 	}
1400237fead6SMichael Halcrow 	offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1401237fead6SMichael Halcrow 	rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1402237fead6SMichael Halcrow 				    &bytes_read);
1403237fead6SMichael Halcrow 	if (rc) {
1404237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1405237fead6SMichael Halcrow 		goto out;
1406237fead6SMichael Halcrow 	}
1407237fead6SMichael Halcrow 	if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1408237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1409237fead6SMichael Halcrow 				"file version [%d] is supported by this "
1410237fead6SMichael Halcrow 				"version of eCryptfs\n",
1411237fead6SMichael Halcrow 				crypt_stat->file_version,
1412237fead6SMichael Halcrow 				ECRYPTFS_SUPPORTED_FILE_VERSION);
1413237fead6SMichael Halcrow 		rc = -EINVAL;
1414237fead6SMichael Halcrow 		goto out;
1415237fead6SMichael Halcrow 	}
1416237fead6SMichael Halcrow 	offset += bytes_read;
1417237fead6SMichael Halcrow 	if (crypt_stat->file_version >= 1) {
1418237fead6SMichael Halcrow 		rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1419237fead6SMichael Halcrow 					   &bytes_read);
1420237fead6SMichael Halcrow 		if (rc) {
1421237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Error reading header "
1422237fead6SMichael Halcrow 					"metadata; rc = [%d]\n", rc);
1423237fead6SMichael Halcrow 		}
1424237fead6SMichael Halcrow 		offset += bytes_read;
1425237fead6SMichael Halcrow 	} else
1426237fead6SMichael Halcrow 		set_default_header_data(crypt_stat);
1427237fead6SMichael Halcrow 	rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1428237fead6SMichael Halcrow 				       ecryptfs_dentry);
1429237fead6SMichael Halcrow out:
1430237fead6SMichael Halcrow 	return rc;
1431237fead6SMichael Halcrow }
1432237fead6SMichael Halcrow 
1433237fead6SMichael Halcrow /**
1434237fead6SMichael Halcrow  * ecryptfs_read_headers
1435237fead6SMichael Halcrow  *
1436237fead6SMichael Halcrow  * Returns zero if valid headers found and parsed; non-zero otherwise
1437237fead6SMichael Halcrow  */
1438237fead6SMichael Halcrow int ecryptfs_read_headers(struct dentry *ecryptfs_dentry,
1439237fead6SMichael Halcrow 			  struct file *lower_file)
1440237fead6SMichael Halcrow {
1441237fead6SMichael Halcrow 	int rc = 0;
1442237fead6SMichael Halcrow 	char *page_virt = NULL;
1443237fead6SMichael Halcrow 	mm_segment_t oldfs;
1444237fead6SMichael Halcrow 	ssize_t bytes_read;
1445237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1446237fead6SMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1447237fead6SMichael Halcrow 
1448237fead6SMichael Halcrow 	/* Read the first page from the underlying file */
1449237fead6SMichael Halcrow 	page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, SLAB_USER);
1450237fead6SMichael Halcrow 	if (!page_virt) {
1451237fead6SMichael Halcrow 		rc = -ENOMEM;
1452237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n");
1453237fead6SMichael Halcrow 		goto out;
1454237fead6SMichael Halcrow 	}
1455237fead6SMichael Halcrow 	lower_file->f_pos = 0;
1456237fead6SMichael Halcrow 	oldfs = get_fs();
1457237fead6SMichael Halcrow 	set_fs(get_ds());
1458237fead6SMichael Halcrow 	bytes_read = lower_file->f_op->read(lower_file,
1459237fead6SMichael Halcrow 					    (char __user *)page_virt,
1460237fead6SMichael Halcrow 					    ECRYPTFS_DEFAULT_EXTENT_SIZE,
1461237fead6SMichael Halcrow 					    &lower_file->f_pos);
1462237fead6SMichael Halcrow 	set_fs(oldfs);
1463237fead6SMichael Halcrow 	if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) {
1464237fead6SMichael Halcrow 		rc = -EINVAL;
1465237fead6SMichael Halcrow 		goto out;
1466237fead6SMichael Halcrow 	}
1467237fead6SMichael Halcrow 	rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1468237fead6SMichael Halcrow 					ecryptfs_dentry);
1469237fead6SMichael Halcrow 	if (rc) {
1470237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Valid eCryptfs headers not "
1471237fead6SMichael Halcrow 				"found\n");
1472237fead6SMichael Halcrow 		rc = -EINVAL;
1473237fead6SMichael Halcrow 	}
1474237fead6SMichael Halcrow out:
1475237fead6SMichael Halcrow 	if (page_virt) {
1476237fead6SMichael Halcrow 		memset(page_virt, 0, PAGE_CACHE_SIZE);
1477237fead6SMichael Halcrow 		kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1478237fead6SMichael Halcrow 	}
1479237fead6SMichael Halcrow 	return rc;
1480237fead6SMichael Halcrow }
1481237fead6SMichael Halcrow 
1482237fead6SMichael Halcrow /**
1483237fead6SMichael Halcrow  * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1484237fead6SMichael Halcrow  * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1485237fead6SMichael Halcrow  * @name: The plaintext name
1486237fead6SMichael Halcrow  * @length: The length of the plaintext
1487237fead6SMichael Halcrow  * @encoded_name: The encypted name
1488237fead6SMichael Halcrow  *
1489237fead6SMichael Halcrow  * Encrypts and encodes a filename into something that constitutes a
1490237fead6SMichael Halcrow  * valid filename for a filesystem, with printable characters.
1491237fead6SMichael Halcrow  *
1492237fead6SMichael Halcrow  * We assume that we have a properly initialized crypto context,
1493237fead6SMichael Halcrow  * pointed to by crypt_stat->tfm.
1494237fead6SMichael Halcrow  *
1495237fead6SMichael Halcrow  * TODO: Implement filename decoding and decryption here, in place of
1496237fead6SMichael Halcrow  * memcpy. We are keeping the framework around for now to (1)
1497237fead6SMichael Halcrow  * facilitate testing of the components needed to implement filename
1498237fead6SMichael Halcrow  * encryption and (2) to provide a code base from which other
1499237fead6SMichael Halcrow  * developers in the community can easily implement this feature.
1500237fead6SMichael Halcrow  *
1501237fead6SMichael Halcrow  * Returns the length of encoded filename; negative if error
1502237fead6SMichael Halcrow  */
1503237fead6SMichael Halcrow int
1504237fead6SMichael Halcrow ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1505237fead6SMichael Halcrow 			 const char *name, int length, char **encoded_name)
1506237fead6SMichael Halcrow {
1507237fead6SMichael Halcrow 	int error = 0;
1508237fead6SMichael Halcrow 
1509237fead6SMichael Halcrow 	(*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
1510237fead6SMichael Halcrow 	if (!(*encoded_name)) {
1511237fead6SMichael Halcrow 		error = -ENOMEM;
1512237fead6SMichael Halcrow 		goto out;
1513237fead6SMichael Halcrow 	}
1514237fead6SMichael Halcrow 	/* TODO: Filename encryption is a scheduled feature for a
1515237fead6SMichael Halcrow 	 * future version of eCryptfs. This function is here only for
1516237fead6SMichael Halcrow 	 * the purpose of providing a framework for other developers
1517237fead6SMichael Halcrow 	 * to easily implement filename encryption. Hint: Replace this
1518237fead6SMichael Halcrow 	 * memcpy() with a call to encrypt and encode the
1519237fead6SMichael Halcrow 	 * filename, the set the length accordingly. */
1520237fead6SMichael Halcrow 	memcpy((void *)(*encoded_name), (void *)name, length);
1521237fead6SMichael Halcrow 	(*encoded_name)[length] = '\0';
1522237fead6SMichael Halcrow 	error = length + 1;
1523237fead6SMichael Halcrow out:
1524237fead6SMichael Halcrow 	return error;
1525237fead6SMichael Halcrow }
1526237fead6SMichael Halcrow 
1527237fead6SMichael Halcrow /**
1528237fead6SMichael Halcrow  * ecryptfs_decode_filename - converts the cipher text name to plaintext
1529237fead6SMichael Halcrow  * @crypt_stat: The crypt_stat struct associated with the file
1530237fead6SMichael Halcrow  * @name: The filename in cipher text
1531237fead6SMichael Halcrow  * @length: The length of the cipher text name
1532237fead6SMichael Halcrow  * @decrypted_name: The plaintext name
1533237fead6SMichael Halcrow  *
1534237fead6SMichael Halcrow  * Decodes and decrypts the filename.
1535237fead6SMichael Halcrow  *
1536237fead6SMichael Halcrow  * We assume that we have a properly initialized crypto context,
1537237fead6SMichael Halcrow  * pointed to by crypt_stat->tfm.
1538237fead6SMichael Halcrow  *
1539237fead6SMichael Halcrow  * TODO: Implement filename decoding and decryption here, in place of
1540237fead6SMichael Halcrow  * memcpy. We are keeping the framework around for now to (1)
1541237fead6SMichael Halcrow  * facilitate testing of the components needed to implement filename
1542237fead6SMichael Halcrow  * encryption and (2) to provide a code base from which other
1543237fead6SMichael Halcrow  * developers in the community can easily implement this feature.
1544237fead6SMichael Halcrow  *
1545237fead6SMichael Halcrow  * Returns the length of decoded filename; negative if error
1546237fead6SMichael Halcrow  */
1547237fead6SMichael Halcrow int
1548237fead6SMichael Halcrow ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1549237fead6SMichael Halcrow 			 const char *name, int length, char **decrypted_name)
1550237fead6SMichael Halcrow {
1551237fead6SMichael Halcrow 	int error = 0;
1552237fead6SMichael Halcrow 
1553237fead6SMichael Halcrow 	(*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
1554237fead6SMichael Halcrow 	if (!(*decrypted_name)) {
1555237fead6SMichael Halcrow 		error = -ENOMEM;
1556237fead6SMichael Halcrow 		goto out;
1557237fead6SMichael Halcrow 	}
1558237fead6SMichael Halcrow 	/* TODO: Filename encryption is a scheduled feature for a
1559237fead6SMichael Halcrow 	 * future version of eCryptfs. This function is here only for
1560237fead6SMichael Halcrow 	 * the purpose of providing a framework for other developers
1561237fead6SMichael Halcrow 	 * to easily implement filename encryption. Hint: Replace this
1562237fead6SMichael Halcrow 	 * memcpy() with a call to decode and decrypt the
1563237fead6SMichael Halcrow 	 * filename, the set the length accordingly. */
1564237fead6SMichael Halcrow 	memcpy((void *)(*decrypted_name), (void *)name, length);
1565237fead6SMichael Halcrow 	(*decrypted_name)[length + 1] = '\0';	/* Only for convenience
1566237fead6SMichael Halcrow 						 * in printing out the
1567237fead6SMichael Halcrow 						 * string in debug
1568237fead6SMichael Halcrow 						 * messages */
1569237fead6SMichael Halcrow 	error = length;
1570237fead6SMichael Halcrow out:
1571237fead6SMichael Halcrow 	return error;
1572237fead6SMichael Halcrow }
1573237fead6SMichael Halcrow 
1574237fead6SMichael Halcrow /**
1575237fead6SMichael Halcrow  * ecryptfs_process_cipher - Perform cipher initialization.
1576237fead6SMichael Halcrow  * @key_tfm: Crypto context for key material, set by this function
1577*e5d9cbdeSMichael Halcrow  * @cipher_name: Name of the cipher
1578*e5d9cbdeSMichael Halcrow  * @key_size: Size of the key in bytes
1579237fead6SMichael Halcrow  *
1580237fead6SMichael Halcrow  * Returns zero on success. Any crypto_tfm structs allocated here
1581237fead6SMichael Halcrow  * should be released by other functions, such as on a superblock put
1582237fead6SMichael Halcrow  * event, regardless of whether this function succeeds for fails.
1583237fead6SMichael Halcrow  */
1584237fead6SMichael Halcrow int
1585*e5d9cbdeSMichael Halcrow ecryptfs_process_cipher(struct crypto_tfm **key_tfm, char *cipher_name,
1586*e5d9cbdeSMichael Halcrow 			size_t *key_size)
1587237fead6SMichael Halcrow {
1588237fead6SMichael Halcrow 	char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
1589237fead6SMichael Halcrow 	int rc;
1590237fead6SMichael Halcrow 
1591*e5d9cbdeSMichael Halcrow 	*key_tfm = NULL;
1592*e5d9cbdeSMichael Halcrow 	if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1593237fead6SMichael Halcrow 		rc = -EINVAL;
1594237fead6SMichael Halcrow 		printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
1595*e5d9cbdeSMichael Halcrow 		      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1596237fead6SMichael Halcrow 		goto out;
1597237fead6SMichael Halcrow 	}
1598237fead6SMichael Halcrow 	*key_tfm = crypto_alloc_tfm(cipher_name, CRYPTO_TFM_REQ_WEAK_KEY);
1599237fead6SMichael Halcrow 	if (!(*key_tfm)) {
1600237fead6SMichael Halcrow 		rc = -EINVAL;
1601237fead6SMichael Halcrow 		printk(KERN_ERR "Unable to allocate crypto cipher with name "
1602237fead6SMichael Halcrow 		       "[%s]\n", cipher_name);
1603237fead6SMichael Halcrow 		goto out;
1604237fead6SMichael Halcrow 	}
1605*e5d9cbdeSMichael Halcrow 	if (*key_size == 0)
1606*e5d9cbdeSMichael Halcrow 		*key_size = crypto_tfm_alg_max_keysize(*key_tfm);
1607*e5d9cbdeSMichael Halcrow 	get_random_bytes(dummy_key, *key_size);
1608*e5d9cbdeSMichael Halcrow 	rc = crypto_cipher_setkey(*key_tfm, dummy_key, *key_size);
1609237fead6SMichael Halcrow 	if (rc) {
1610237fead6SMichael Halcrow 		printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
1611*e5d9cbdeSMichael Halcrow 		       "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
1612237fead6SMichael Halcrow 		rc = -EINVAL;
1613237fead6SMichael Halcrow 		goto out;
1614237fead6SMichael Halcrow 	}
1615237fead6SMichael Halcrow out:
1616237fead6SMichael Halcrow 	return rc;
1617237fead6SMichael Halcrow }
1618