xref: /openbmc/linux/fs/ecryptfs/crypto.c (revision f4aad16adfb8f0a2d666fdf8af4bd0dff2ce75e4)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  *
4237fead6SMichael Halcrow  * Copyright (C) 1997-2004 Erez Zadok
5237fead6SMichael Halcrow  * Copyright (C) 2001-2004 Stony Brook University
6dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
7237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8237fead6SMichael Halcrow  *   		Michael C. Thompson <mcthomps@us.ibm.com>
9237fead6SMichael Halcrow  *
10237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
11237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
12237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
13237fead6SMichael Halcrow  * License, or (at your option) any later version.
14237fead6SMichael Halcrow  *
15237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
16237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
17237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18237fead6SMichael Halcrow  * General Public License for more details.
19237fead6SMichael Halcrow  *
20237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
21237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
22237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23237fead6SMichael Halcrow  * 02111-1307, USA.
24237fead6SMichael Halcrow  */
25237fead6SMichael Halcrow 
26237fead6SMichael Halcrow #include <linux/fs.h>
27237fead6SMichael Halcrow #include <linux/mount.h>
28237fead6SMichael Halcrow #include <linux/pagemap.h>
29237fead6SMichael Halcrow #include <linux/random.h>
30237fead6SMichael Halcrow #include <linux/compiler.h>
31237fead6SMichael Halcrow #include <linux/key.h>
32237fead6SMichael Halcrow #include <linux/namei.h>
33237fead6SMichael Halcrow #include <linux/crypto.h>
34237fead6SMichael Halcrow #include <linux/file.h>
35237fead6SMichael Halcrow #include <linux/scatterlist.h>
36237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
37237fead6SMichael Halcrow 
38237fead6SMichael Halcrow static int
39237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
40237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
41237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
42237fead6SMichael Halcrow 			     unsigned char *iv);
43237fead6SMichael Halcrow static int
44237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
45237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
46237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
47237fead6SMichael Halcrow 			     unsigned char *iv);
48237fead6SMichael Halcrow 
49237fead6SMichael Halcrow /**
50237fead6SMichael Halcrow  * ecryptfs_to_hex
51237fead6SMichael Halcrow  * @dst: Buffer to take hex character representation of contents of
52237fead6SMichael Halcrow  *       src; must be at least of size (src_size * 2)
53237fead6SMichael Halcrow  * @src: Buffer to be converted to a hex string respresentation
54237fead6SMichael Halcrow  * @src_size: number of bytes to convert
55237fead6SMichael Halcrow  */
56237fead6SMichael Halcrow void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
57237fead6SMichael Halcrow {
58237fead6SMichael Halcrow 	int x;
59237fead6SMichael Halcrow 
60237fead6SMichael Halcrow 	for (x = 0; x < src_size; x++)
61237fead6SMichael Halcrow 		sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
62237fead6SMichael Halcrow }
63237fead6SMichael Halcrow 
64237fead6SMichael Halcrow /**
65237fead6SMichael Halcrow  * ecryptfs_from_hex
66237fead6SMichael Halcrow  * @dst: Buffer to take the bytes from src hex; must be at least of
67237fead6SMichael Halcrow  *       size (src_size / 2)
68237fead6SMichael Halcrow  * @src: Buffer to be converted from a hex string respresentation to raw value
69237fead6SMichael Halcrow  * @dst_size: size of dst buffer, or number of hex characters pairs to convert
70237fead6SMichael Halcrow  */
71237fead6SMichael Halcrow void ecryptfs_from_hex(char *dst, char *src, int dst_size)
72237fead6SMichael Halcrow {
73237fead6SMichael Halcrow 	int x;
74237fead6SMichael Halcrow 	char tmp[3] = { 0, };
75237fead6SMichael Halcrow 
76237fead6SMichael Halcrow 	for (x = 0; x < dst_size; x++) {
77237fead6SMichael Halcrow 		tmp[0] = src[x * 2];
78237fead6SMichael Halcrow 		tmp[1] = src[x * 2 + 1];
79237fead6SMichael Halcrow 		dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
80237fead6SMichael Halcrow 	}
81237fead6SMichael Halcrow }
82237fead6SMichael Halcrow 
83237fead6SMichael Halcrow /**
84237fead6SMichael Halcrow  * ecryptfs_calculate_md5 - calculates the md5 of @src
85237fead6SMichael Halcrow  * @dst: Pointer to 16 bytes of allocated memory
86237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
87237fead6SMichael Halcrow  * @src: Data to be md5'd
88237fead6SMichael Halcrow  * @len: Length of @src
89237fead6SMichael Halcrow  *
90237fead6SMichael Halcrow  * Uses the allocated crypto context that crypt_stat references to
91237fead6SMichael Halcrow  * generate the MD5 sum of the contents of src.
92237fead6SMichael Halcrow  */
93237fead6SMichael Halcrow static int ecryptfs_calculate_md5(char *dst,
94237fead6SMichael Halcrow 				  struct ecryptfs_crypt_stat *crypt_stat,
95237fead6SMichael Halcrow 				  char *src, int len)
96237fead6SMichael Halcrow {
97237fead6SMichael Halcrow 	struct scatterlist sg;
98565d9724SMichael Halcrow 	struct hash_desc desc = {
99565d9724SMichael Halcrow 		.tfm = crypt_stat->hash_tfm,
100565d9724SMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
101565d9724SMichael Halcrow 	};
102565d9724SMichael Halcrow 	int rc = 0;
103237fead6SMichael Halcrow 
104565d9724SMichael Halcrow 	mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
105237fead6SMichael Halcrow 	sg_init_one(&sg, (u8 *)src, len);
106565d9724SMichael Halcrow 	if (!desc.tfm) {
107565d9724SMichael Halcrow 		desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
108565d9724SMichael Halcrow 					     CRYPTO_ALG_ASYNC);
109565d9724SMichael Halcrow 		if (IS_ERR(desc.tfm)) {
110565d9724SMichael Halcrow 			rc = PTR_ERR(desc.tfm);
111237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
112565d9724SMichael Halcrow 					"allocate crypto context; rc = [%d]\n",
113565d9724SMichael Halcrow 					rc);
114237fead6SMichael Halcrow 			goto out;
115237fead6SMichael Halcrow 		}
116565d9724SMichael Halcrow 		crypt_stat->hash_tfm = desc.tfm;
117237fead6SMichael Halcrow 	}
118565d9724SMichael Halcrow 	crypto_hash_init(&desc);
119565d9724SMichael Halcrow 	crypto_hash_update(&desc, &sg, len);
120565d9724SMichael Halcrow 	crypto_hash_final(&desc, dst);
121565d9724SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
122237fead6SMichael Halcrow out:
123237fead6SMichael Halcrow 	return rc;
124237fead6SMichael Halcrow }
125237fead6SMichael Halcrow 
1268bba066fSMichael Halcrow int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
1278bba066fSMichael Halcrow 					   char *cipher_name,
1288bba066fSMichael Halcrow 					   char *chaining_modifier)
1298bba066fSMichael Halcrow {
1308bba066fSMichael Halcrow 	int cipher_name_len = strlen(cipher_name);
1318bba066fSMichael Halcrow 	int chaining_modifier_len = strlen(chaining_modifier);
1328bba066fSMichael Halcrow 	int algified_name_len;
1338bba066fSMichael Halcrow 	int rc;
1348bba066fSMichael Halcrow 
1358bba066fSMichael Halcrow 	algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
1368bba066fSMichael Halcrow 	(*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
1377bd473fcSMichael Halcrow 	if (!(*algified_name)) {
1388bba066fSMichael Halcrow 		rc = -ENOMEM;
1398bba066fSMichael Halcrow 		goto out;
1408bba066fSMichael Halcrow 	}
1418bba066fSMichael Halcrow 	snprintf((*algified_name), algified_name_len, "%s(%s)",
1428bba066fSMichael Halcrow 		 chaining_modifier, cipher_name);
1438bba066fSMichael Halcrow 	rc = 0;
1448bba066fSMichael Halcrow out:
1458bba066fSMichael Halcrow 	return rc;
1468bba066fSMichael Halcrow }
1478bba066fSMichael Halcrow 
148237fead6SMichael Halcrow /**
149237fead6SMichael Halcrow  * ecryptfs_derive_iv
150237fead6SMichael Halcrow  * @iv: destination for the derived iv vale
151237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
152237fead6SMichael Halcrow  * @offset: Offset of the page whose's iv we are to derive
153237fead6SMichael Halcrow  *
154237fead6SMichael Halcrow  * Generate the initialization vector from the given root IV and page
155237fead6SMichael Halcrow  * offset.
156237fead6SMichael Halcrow  *
157237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
158237fead6SMichael Halcrow  */
159237fead6SMichael Halcrow static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
160237fead6SMichael Halcrow 			      pgoff_t offset)
161237fead6SMichael Halcrow {
162237fead6SMichael Halcrow 	int rc = 0;
163237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
164237fead6SMichael Halcrow 	char src[ECRYPTFS_MAX_IV_BYTES + 16];
165237fead6SMichael Halcrow 
166237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
167237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "root iv:\n");
168237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
169237fead6SMichael Halcrow 	}
170237fead6SMichael Halcrow 	/* TODO: It is probably secure to just cast the least
171237fead6SMichael Halcrow 	 * significant bits of the root IV into an unsigned long and
172237fead6SMichael Halcrow 	 * add the offset to that rather than go through all this
173237fead6SMichael Halcrow 	 * hashing business. -Halcrow */
174237fead6SMichael Halcrow 	memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
175237fead6SMichael Halcrow 	memset((src + crypt_stat->iv_bytes), 0, 16);
176237fead6SMichael Halcrow 	snprintf((src + crypt_stat->iv_bytes), 16, "%ld", offset);
177237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
178237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "source:\n");
179237fead6SMichael Halcrow 		ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
180237fead6SMichael Halcrow 	}
181237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
182237fead6SMichael Halcrow 				    (crypt_stat->iv_bytes + 16));
183237fead6SMichael Halcrow 	if (rc) {
184237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
185237fead6SMichael Halcrow 				"MD5 while generating IV for a page\n");
186237fead6SMichael Halcrow 		goto out;
187237fead6SMichael Halcrow 	}
188237fead6SMichael Halcrow 	memcpy(iv, dst, crypt_stat->iv_bytes);
189237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
190237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
191237fead6SMichael Halcrow 		ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
192237fead6SMichael Halcrow 	}
193237fead6SMichael Halcrow out:
194237fead6SMichael Halcrow 	return rc;
195237fead6SMichael Halcrow }
196237fead6SMichael Halcrow 
197237fead6SMichael Halcrow /**
198237fead6SMichael Halcrow  * ecryptfs_init_crypt_stat
199237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
200237fead6SMichael Halcrow  *
201237fead6SMichael Halcrow  * Initialize the crypt_stat structure.
202237fead6SMichael Halcrow  */
203237fead6SMichael Halcrow void
204237fead6SMichael Halcrow ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
205237fead6SMichael Halcrow {
206237fead6SMichael Halcrow 	memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
207*f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&crypt_stat->keysig_list);
208*f4aad16aSMichael Halcrow 	mutex_init(&crypt_stat->keysig_list_mutex);
209237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_mutex);
210237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_tfm_mutex);
211565d9724SMichael Halcrow 	mutex_init(&crypt_stat->cs_hash_tfm_mutex);
212e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
213237fead6SMichael Halcrow }
214237fead6SMichael Halcrow 
215237fead6SMichael Halcrow /**
216237fead6SMichael Halcrow  * ecryptfs_destruct_crypt_stat
217237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
218237fead6SMichael Halcrow  *
219237fead6SMichael Halcrow  * Releases all memory associated with a crypt_stat struct.
220237fead6SMichael Halcrow  */
221237fead6SMichael Halcrow void ecryptfs_destruct_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
222237fead6SMichael Halcrow {
223*f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
224*f4aad16aSMichael Halcrow 
225237fead6SMichael Halcrow 	if (crypt_stat->tfm)
2268bba066fSMichael Halcrow 		crypto_free_blkcipher(crypt_stat->tfm);
227565d9724SMichael Halcrow 	if (crypt_stat->hash_tfm)
228565d9724SMichael Halcrow 		crypto_free_hash(crypt_stat->hash_tfm);
229*f4aad16aSMichael Halcrow 	mutex_lock(&crypt_stat->keysig_list_mutex);
230*f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_sig, key_sig_tmp,
231*f4aad16aSMichael Halcrow 				 &crypt_stat->keysig_list, crypt_stat_list) {
232*f4aad16aSMichael Halcrow 		list_del(&key_sig->crypt_stat_list);
233*f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
234*f4aad16aSMichael Halcrow 	}
235*f4aad16aSMichael Halcrow 	mutex_unlock(&crypt_stat->keysig_list_mutex);
236237fead6SMichael Halcrow 	memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
237237fead6SMichael Halcrow }
238237fead6SMichael Halcrow 
239237fead6SMichael Halcrow void ecryptfs_destruct_mount_crypt_stat(
240237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
241237fead6SMichael Halcrow {
242*f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
243*f4aad16aSMichael Halcrow 
244*f4aad16aSMichael Halcrow 	if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
245*f4aad16aSMichael Halcrow 		return;
246*f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
247*f4aad16aSMichael Halcrow 	list_for_each_entry_safe(auth_tok, auth_tok_tmp,
248*f4aad16aSMichael Halcrow 				 &mount_crypt_stat->global_auth_tok_list,
249*f4aad16aSMichael Halcrow 				 mount_crypt_stat_list) {
250*f4aad16aSMichael Halcrow 		list_del(&auth_tok->mount_crypt_stat_list);
251*f4aad16aSMichael Halcrow 		mount_crypt_stat->num_global_auth_toks--;
252*f4aad16aSMichael Halcrow 		if (auth_tok->global_auth_tok_key
253*f4aad16aSMichael Halcrow 		    && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
254*f4aad16aSMichael Halcrow 			key_put(auth_tok->global_auth_tok_key);
255*f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
256*f4aad16aSMichael Halcrow 	}
257*f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
258237fead6SMichael Halcrow 	memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
259237fead6SMichael Halcrow }
260237fead6SMichael Halcrow 
261237fead6SMichael Halcrow /**
262237fead6SMichael Halcrow  * virt_to_scatterlist
263237fead6SMichael Halcrow  * @addr: Virtual address
264237fead6SMichael Halcrow  * @size: Size of data; should be an even multiple of the block size
265237fead6SMichael Halcrow  * @sg: Pointer to scatterlist array; set to NULL to obtain only
266237fead6SMichael Halcrow  *      the number of scatterlist structs required in array
267237fead6SMichael Halcrow  * @sg_size: Max array size
268237fead6SMichael Halcrow  *
269237fead6SMichael Halcrow  * Fills in a scatterlist array with page references for a passed
270237fead6SMichael Halcrow  * virtual address.
271237fead6SMichael Halcrow  *
272237fead6SMichael Halcrow  * Returns the number of scatterlist structs in array used
273237fead6SMichael Halcrow  */
274237fead6SMichael Halcrow int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
275237fead6SMichael Halcrow 			int sg_size)
276237fead6SMichael Halcrow {
277237fead6SMichael Halcrow 	int i = 0;
278237fead6SMichael Halcrow 	struct page *pg;
279237fead6SMichael Halcrow 	int offset;
280237fead6SMichael Halcrow 	int remainder_of_page;
281237fead6SMichael Halcrow 
282237fead6SMichael Halcrow 	while (size > 0 && i < sg_size) {
283237fead6SMichael Halcrow 		pg = virt_to_page(addr);
284237fead6SMichael Halcrow 		offset = offset_in_page(addr);
285237fead6SMichael Halcrow 		if (sg) {
286237fead6SMichael Halcrow 			sg[i].page = pg;
287237fead6SMichael Halcrow 			sg[i].offset = offset;
288237fead6SMichael Halcrow 		}
289237fead6SMichael Halcrow 		remainder_of_page = PAGE_CACHE_SIZE - offset;
290237fead6SMichael Halcrow 		if (size >= remainder_of_page) {
291237fead6SMichael Halcrow 			if (sg)
292237fead6SMichael Halcrow 				sg[i].length = remainder_of_page;
293237fead6SMichael Halcrow 			addr += remainder_of_page;
294237fead6SMichael Halcrow 			size -= remainder_of_page;
295237fead6SMichael Halcrow 		} else {
296237fead6SMichael Halcrow 			if (sg)
297237fead6SMichael Halcrow 				sg[i].length = size;
298237fead6SMichael Halcrow 			addr += size;
299237fead6SMichael Halcrow 			size = 0;
300237fead6SMichael Halcrow 		}
301237fead6SMichael Halcrow 		i++;
302237fead6SMichael Halcrow 	}
303237fead6SMichael Halcrow 	if (size > 0)
304237fead6SMichael Halcrow 		return -ENOMEM;
305237fead6SMichael Halcrow 	return i;
306237fead6SMichael Halcrow }
307237fead6SMichael Halcrow 
308237fead6SMichael Halcrow /**
309237fead6SMichael Halcrow  * encrypt_scatterlist
310237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
311237fead6SMichael Halcrow  * @dest_sg: Destination of encrypted data
312237fead6SMichael Halcrow  * @src_sg: Data to be encrypted
313237fead6SMichael Halcrow  * @size: Length of data to be encrypted
314237fead6SMichael Halcrow  * @iv: iv to use during encryption
315237fead6SMichael Halcrow  *
316237fead6SMichael Halcrow  * Returns the number of bytes encrypted; negative value on error
317237fead6SMichael Halcrow  */
318237fead6SMichael Halcrow static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
319237fead6SMichael Halcrow 			       struct scatterlist *dest_sg,
320237fead6SMichael Halcrow 			       struct scatterlist *src_sg, int size,
321237fead6SMichael Halcrow 			       unsigned char *iv)
322237fead6SMichael Halcrow {
3238bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
3248bba066fSMichael Halcrow 		.tfm = crypt_stat->tfm,
3258bba066fSMichael Halcrow 		.info = iv,
3268bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
3278bba066fSMichael Halcrow 	};
328237fead6SMichael Halcrow 	int rc = 0;
329237fead6SMichael Halcrow 
330237fead6SMichael Halcrow 	BUG_ON(!crypt_stat || !crypt_stat->tfm
331e2bd99ecSMichael Halcrow 	       || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
332237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
333237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
334237fead6SMichael Halcrow 				crypt_stat->key_size);
335237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
336237fead6SMichael Halcrow 				  crypt_stat->key_size);
337237fead6SMichael Halcrow 	}
338237fead6SMichael Halcrow 	/* Consider doing this once, when the file is opened */
339237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
3408bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
341237fead6SMichael Halcrow 				     crypt_stat->key_size);
342237fead6SMichael Halcrow 	if (rc) {
343237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
344237fead6SMichael Halcrow 				rc);
345237fead6SMichael Halcrow 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
346237fead6SMichael Halcrow 		rc = -EINVAL;
347237fead6SMichael Halcrow 		goto out;
348237fead6SMichael Halcrow 	}
349237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
3508bba066fSMichael Halcrow 	crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
351237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
352237fead6SMichael Halcrow out:
353237fead6SMichael Halcrow 	return rc;
354237fead6SMichael Halcrow }
355237fead6SMichael Halcrow 
356237fead6SMichael Halcrow static void
357237fead6SMichael Halcrow ecryptfs_extent_to_lwr_pg_idx_and_offset(unsigned long *lower_page_idx,
358237fead6SMichael Halcrow 					 int *byte_offset,
359237fead6SMichael Halcrow 					 struct ecryptfs_crypt_stat *crypt_stat,
360237fead6SMichael Halcrow 					 unsigned long extent_num)
361237fead6SMichael Halcrow {
362237fead6SMichael Halcrow 	unsigned long lower_extent_num;
363237fead6SMichael Halcrow 	int extents_occupied_by_headers_at_front;
364237fead6SMichael Halcrow 	int bytes_occupied_by_headers_at_front;
365237fead6SMichael Halcrow 	int extent_offset;
366237fead6SMichael Halcrow 	int extents_per_page;
367237fead6SMichael Halcrow 
368237fead6SMichael Halcrow 	bytes_occupied_by_headers_at_front =
369237fead6SMichael Halcrow 		( crypt_stat->header_extent_size
370237fead6SMichael Halcrow 		  * crypt_stat->num_header_extents_at_front );
371237fead6SMichael Halcrow 	extents_occupied_by_headers_at_front =
372237fead6SMichael Halcrow 		( bytes_occupied_by_headers_at_front
373237fead6SMichael Halcrow 		  / crypt_stat->extent_size );
374237fead6SMichael Halcrow 	lower_extent_num = extents_occupied_by_headers_at_front + extent_num;
375237fead6SMichael Halcrow 	extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
376237fead6SMichael Halcrow 	(*lower_page_idx) = lower_extent_num / extents_per_page;
377237fead6SMichael Halcrow 	extent_offset = lower_extent_num % extents_per_page;
378237fead6SMichael Halcrow 	(*byte_offset) = extent_offset * crypt_stat->extent_size;
379237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * crypt_stat->header_extent_size = "
380237fead6SMichael Halcrow 			"[%d]\n", crypt_stat->header_extent_size);
381237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * crypt_stat->"
382237fead6SMichael Halcrow 			"num_header_extents_at_front = [%d]\n",
383237fead6SMichael Halcrow 			crypt_stat->num_header_extents_at_front);
384237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * extents_occupied_by_headers_at_"
385237fead6SMichael Halcrow 			"front = [%d]\n", extents_occupied_by_headers_at_front);
386237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * lower_extent_num = [0x%.16x]\n",
387237fead6SMichael Halcrow 			lower_extent_num);
388237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * extents_per_page = [%d]\n",
389237fead6SMichael Halcrow 			extents_per_page);
390237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * (*lower_page_idx) = [0x%.16x]\n",
391237fead6SMichael Halcrow 			(*lower_page_idx));
392237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * extent_offset = [%d]\n",
393237fead6SMichael Halcrow 			extent_offset);
394237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, " * (*byte_offset) = [%d]\n",
395237fead6SMichael Halcrow 			(*byte_offset));
396237fead6SMichael Halcrow }
397237fead6SMichael Halcrow 
398237fead6SMichael Halcrow static int ecryptfs_write_out_page(struct ecryptfs_page_crypt_context *ctx,
399237fead6SMichael Halcrow 				   struct page *lower_page,
400237fead6SMichael Halcrow 				   struct inode *lower_inode,
401237fead6SMichael Halcrow 				   int byte_offset_in_page, int bytes_to_write)
402237fead6SMichael Halcrow {
403237fead6SMichael Halcrow 	int rc = 0;
404237fead6SMichael Halcrow 
405237fead6SMichael Halcrow 	if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
406237fead6SMichael Halcrow 		rc = ecryptfs_commit_lower_page(lower_page, lower_inode,
407237fead6SMichael Halcrow 						ctx->param.lower_file,
408237fead6SMichael Halcrow 						byte_offset_in_page,
409237fead6SMichael Halcrow 						bytes_to_write);
410237fead6SMichael Halcrow 		if (rc) {
411237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error calling lower "
412237fead6SMichael Halcrow 					"commit; rc = [%d]\n", rc);
413237fead6SMichael Halcrow 			goto out;
414237fead6SMichael Halcrow 		}
415237fead6SMichael Halcrow 	} else {
416237fead6SMichael Halcrow 		rc = ecryptfs_writepage_and_release_lower_page(lower_page,
417237fead6SMichael Halcrow 							       lower_inode,
418237fead6SMichael Halcrow 							       ctx->param.wbc);
419237fead6SMichael Halcrow 		if (rc) {
420237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error calling lower "
421237fead6SMichael Halcrow 					"writepage(); rc = [%d]\n", rc);
422237fead6SMichael Halcrow 			goto out;
423237fead6SMichael Halcrow 		}
424237fead6SMichael Halcrow 	}
425237fead6SMichael Halcrow out:
426237fead6SMichael Halcrow 	return rc;
427237fead6SMichael Halcrow }
428237fead6SMichael Halcrow 
429237fead6SMichael Halcrow static int ecryptfs_read_in_page(struct ecryptfs_page_crypt_context *ctx,
430237fead6SMichael Halcrow 				 struct page **lower_page,
431237fead6SMichael Halcrow 				 struct inode *lower_inode,
432237fead6SMichael Halcrow 				 unsigned long lower_page_idx,
433237fead6SMichael Halcrow 				 int byte_offset_in_page)
434237fead6SMichael Halcrow {
435237fead6SMichael Halcrow 	int rc = 0;
436237fead6SMichael Halcrow 
437237fead6SMichael Halcrow 	if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
438237fead6SMichael Halcrow 		/* TODO: Limit this to only the data extents that are
439237fead6SMichael Halcrow 		 * needed */
440237fead6SMichael Halcrow 		rc = ecryptfs_get_lower_page(lower_page, lower_inode,
441237fead6SMichael Halcrow 					     ctx->param.lower_file,
442237fead6SMichael Halcrow 					     lower_page_idx,
443237fead6SMichael Halcrow 					     byte_offset_in_page,
444237fead6SMichael Halcrow 					     (PAGE_CACHE_SIZE
445237fead6SMichael Halcrow 					      - byte_offset_in_page));
446237fead6SMichael Halcrow 		if (rc) {
447237fead6SMichael Halcrow 			ecryptfs_printk(
448237fead6SMichael Halcrow 				KERN_ERR, "Error attempting to grab, map, "
449237fead6SMichael Halcrow 				"and prepare_write lower page with index "
450237fead6SMichael Halcrow 				"[0x%.16x]; rc = [%d]\n", lower_page_idx, rc);
451237fead6SMichael Halcrow 			goto out;
452237fead6SMichael Halcrow 		}
453237fead6SMichael Halcrow 	} else {
4549d8b8ce5SMichael Halcrow 		*lower_page = grab_cache_page(lower_inode->i_mapping,
455237fead6SMichael Halcrow 					      lower_page_idx);
4569d8b8ce5SMichael Halcrow 		if (!(*lower_page)) {
4579d8b8ce5SMichael Halcrow 			rc = -EINVAL;
458237fead6SMichael Halcrow 			ecryptfs_printk(
459237fead6SMichael Halcrow 				KERN_ERR, "Error attempting to grab and map "
460237fead6SMichael Halcrow 				"lower page with index [0x%.16x]; rc = [%d]\n",
461237fead6SMichael Halcrow 				lower_page_idx, rc);
462237fead6SMichael Halcrow 			goto out;
463237fead6SMichael Halcrow 		}
464237fead6SMichael Halcrow 	}
465237fead6SMichael Halcrow out:
466237fead6SMichael Halcrow 	return rc;
467237fead6SMichael Halcrow }
468237fead6SMichael Halcrow 
469237fead6SMichael Halcrow /**
470237fead6SMichael Halcrow  * ecryptfs_encrypt_page
471237fead6SMichael Halcrow  * @ctx: The context of the page
472237fead6SMichael Halcrow  *
473237fead6SMichael Halcrow  * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
474237fead6SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
475237fead6SMichael Halcrow  * if the file was created on a machine with an 8K page size
476237fead6SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
477237fead6SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
478237fead6SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
479237fead6SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
480237fead6SMichael Halcrow  *
481237fead6SMichael Halcrow  * The actual operations performed on each page depends on the
482237fead6SMichael Halcrow  * contents of the ecryptfs_page_crypt_context struct.
483237fead6SMichael Halcrow  *
484237fead6SMichael Halcrow  * Returns zero on success; negative on error
485237fead6SMichael Halcrow  */
486237fead6SMichael Halcrow int ecryptfs_encrypt_page(struct ecryptfs_page_crypt_context *ctx)
487237fead6SMichael Halcrow {
488237fead6SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
489237fead6SMichael Halcrow 	unsigned long base_extent;
490237fead6SMichael Halcrow 	unsigned long extent_offset = 0;
491237fead6SMichael Halcrow 	unsigned long lower_page_idx = 0;
492237fead6SMichael Halcrow 	unsigned long prior_lower_page_idx = 0;
493237fead6SMichael Halcrow 	struct page *lower_page;
494237fead6SMichael Halcrow 	struct inode *lower_inode;
495237fead6SMichael Halcrow 	struct ecryptfs_inode_info *inode_info;
496237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
497237fead6SMichael Halcrow 	int rc = 0;
498237fead6SMichael Halcrow 	int lower_byte_offset = 0;
499237fead6SMichael Halcrow 	int orig_byte_offset = 0;
500237fead6SMichael Halcrow 	int num_extents_per_page;
501237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_UNREAD    0
502237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_READ      1
503237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_MODIFIED  2
504237fead6SMichael Halcrow #define ECRYPTFS_PAGE_STATE_WRITTEN   3
505237fead6SMichael Halcrow 	int page_state;
506237fead6SMichael Halcrow 
507237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(ctx->page->mapping->host);
508237fead6SMichael Halcrow 	inode_info = ecryptfs_inode_to_private(ctx->page->mapping->host);
509237fead6SMichael Halcrow 	crypt_stat = &inode_info->crypt_stat;
510e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
511237fead6SMichael Halcrow 		rc = ecryptfs_copy_page_to_lower(ctx->page, lower_inode,
512237fead6SMichael Halcrow 						 ctx->param.lower_file);
513237fead6SMichael Halcrow 		if (rc)
514237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to copy "
515237fead6SMichael Halcrow 					"page at index [0x%.16x]\n",
516237fead6SMichael Halcrow 					ctx->page->index);
517237fead6SMichael Halcrow 		goto out;
518237fead6SMichael Halcrow 	}
519237fead6SMichael Halcrow 	num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
520237fead6SMichael Halcrow 	base_extent = (ctx->page->index * num_extents_per_page);
521237fead6SMichael Halcrow 	page_state = ECRYPTFS_PAGE_STATE_UNREAD;
522237fead6SMichael Halcrow 	while (extent_offset < num_extents_per_page) {
523237fead6SMichael Halcrow 		ecryptfs_extent_to_lwr_pg_idx_and_offset(
524237fead6SMichael Halcrow 			&lower_page_idx, &lower_byte_offset, crypt_stat,
525237fead6SMichael Halcrow 			(base_extent + extent_offset));
526237fead6SMichael Halcrow 		if (prior_lower_page_idx != lower_page_idx
527237fead6SMichael Halcrow 		    && page_state == ECRYPTFS_PAGE_STATE_MODIFIED) {
528237fead6SMichael Halcrow 			rc = ecryptfs_write_out_page(ctx, lower_page,
529237fead6SMichael Halcrow 						     lower_inode,
530237fead6SMichael Halcrow 						     orig_byte_offset,
531237fead6SMichael Halcrow 						     (PAGE_CACHE_SIZE
532237fead6SMichael Halcrow 						      - orig_byte_offset));
533237fead6SMichael Halcrow 			if (rc) {
534237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error attempting "
535237fead6SMichael Halcrow 						"to write out page; rc = [%d]"
536237fead6SMichael Halcrow 						"\n", rc);
537237fead6SMichael Halcrow 				goto out;
538237fead6SMichael Halcrow 			}
539237fead6SMichael Halcrow 			page_state = ECRYPTFS_PAGE_STATE_WRITTEN;
540237fead6SMichael Halcrow 		}
541237fead6SMichael Halcrow 		if (page_state == ECRYPTFS_PAGE_STATE_UNREAD
542237fead6SMichael Halcrow 		    || page_state == ECRYPTFS_PAGE_STATE_WRITTEN) {
543237fead6SMichael Halcrow 			rc = ecryptfs_read_in_page(ctx, &lower_page,
544237fead6SMichael Halcrow 						   lower_inode, lower_page_idx,
545237fead6SMichael Halcrow 						   lower_byte_offset);
546237fead6SMichael Halcrow 			if (rc) {
547237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error attempting "
548237fead6SMichael Halcrow 						"to read in lower page with "
549237fead6SMichael Halcrow 						"index [0x%.16x]; rc = [%d]\n",
550237fead6SMichael Halcrow 						lower_page_idx, rc);
551237fead6SMichael Halcrow 				goto out;
552237fead6SMichael Halcrow 			}
553237fead6SMichael Halcrow 			orig_byte_offset = lower_byte_offset;
554237fead6SMichael Halcrow 			prior_lower_page_idx = lower_page_idx;
555237fead6SMichael Halcrow 			page_state = ECRYPTFS_PAGE_STATE_READ;
556237fead6SMichael Halcrow 		}
557237fead6SMichael Halcrow 		BUG_ON(!(page_state == ECRYPTFS_PAGE_STATE_MODIFIED
558237fead6SMichael Halcrow 			 || page_state == ECRYPTFS_PAGE_STATE_READ));
559237fead6SMichael Halcrow 		rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
560237fead6SMichael Halcrow 					(base_extent + extent_offset));
561237fead6SMichael Halcrow 		if (rc) {
562237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
563237fead6SMichael Halcrow 					"derive IV for extent [0x%.16x]; "
564237fead6SMichael Halcrow 					"rc = [%d]\n",
565237fead6SMichael Halcrow 					(base_extent + extent_offset), rc);
566237fead6SMichael Halcrow 			goto out;
567237fead6SMichael Halcrow 		}
568237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
569237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
570237fead6SMichael Halcrow 					"with iv:\n");
571237fead6SMichael Halcrow 			ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
572237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
573237fead6SMichael Halcrow 					"encryption:\n");
574237fead6SMichael Halcrow 			ecryptfs_dump_hex((char *)
575237fead6SMichael Halcrow 					  (page_address(ctx->page)
576237fead6SMichael Halcrow 					   + (extent_offset
577237fead6SMichael Halcrow 					      * crypt_stat->extent_size)), 8);
578237fead6SMichael Halcrow 		}
579237fead6SMichael Halcrow 		rc = ecryptfs_encrypt_page_offset(
580237fead6SMichael Halcrow 			crypt_stat, lower_page, lower_byte_offset, ctx->page,
581237fead6SMichael Halcrow 			(extent_offset * crypt_stat->extent_size),
582237fead6SMichael Halcrow 			crypt_stat->extent_size, extent_iv);
583237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
584237fead6SMichael Halcrow 				"rc = [%d]\n",
585237fead6SMichael Halcrow 				(base_extent + extent_offset), rc);
586237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
587237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
588237fead6SMichael Halcrow 					"encryption:\n");
589237fead6SMichael Halcrow 			ecryptfs_dump_hex((char *)(page_address(lower_page)
590237fead6SMichael Halcrow 						   + lower_byte_offset), 8);
591237fead6SMichael Halcrow 		}
592237fead6SMichael Halcrow 		page_state = ECRYPTFS_PAGE_STATE_MODIFIED;
593237fead6SMichael Halcrow 		extent_offset++;
594237fead6SMichael Halcrow 	}
595237fead6SMichael Halcrow 	BUG_ON(orig_byte_offset != 0);
596237fead6SMichael Halcrow 	rc = ecryptfs_write_out_page(ctx, lower_page, lower_inode, 0,
597237fead6SMichael Halcrow 				     (lower_byte_offset
598237fead6SMichael Halcrow 				      + crypt_stat->extent_size));
599237fead6SMichael Halcrow 	if (rc) {
600237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error attempting to write out "
601237fead6SMichael Halcrow 				"page; rc = [%d]\n", rc);
602237fead6SMichael Halcrow 				goto out;
603237fead6SMichael Halcrow 	}
604237fead6SMichael Halcrow out:
605237fead6SMichael Halcrow 	return rc;
606237fead6SMichael Halcrow }
607237fead6SMichael Halcrow 
608237fead6SMichael Halcrow /**
609237fead6SMichael Halcrow  * ecryptfs_decrypt_page
610237fead6SMichael Halcrow  * @file: The ecryptfs file
611237fead6SMichael Halcrow  * @page: The page in ecryptfs to decrypt
612237fead6SMichael Halcrow  *
613237fead6SMichael Halcrow  * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
614237fead6SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
615237fead6SMichael Halcrow  * if the file was created on a machine with an 8K page size
616237fead6SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
617237fead6SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
618237fead6SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
619237fead6SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
620237fead6SMichael Halcrow  *
621237fead6SMichael Halcrow  * Returns zero on success; negative on error
622237fead6SMichael Halcrow  */
623237fead6SMichael Halcrow int ecryptfs_decrypt_page(struct file *file, struct page *page)
624237fead6SMichael Halcrow {
625237fead6SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
626237fead6SMichael Halcrow 	unsigned long base_extent;
627237fead6SMichael Halcrow 	unsigned long extent_offset = 0;
628237fead6SMichael Halcrow 	unsigned long lower_page_idx = 0;
629237fead6SMichael Halcrow 	unsigned long prior_lower_page_idx = 0;
630237fead6SMichael Halcrow 	struct page *lower_page;
631237fead6SMichael Halcrow 	char *lower_page_virt = NULL;
632237fead6SMichael Halcrow 	struct inode *lower_inode;
633237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
634237fead6SMichael Halcrow 	int rc = 0;
635237fead6SMichael Halcrow 	int byte_offset;
636237fead6SMichael Halcrow 	int num_extents_per_page;
637237fead6SMichael Halcrow 	int page_state;
638237fead6SMichael Halcrow 
639237fead6SMichael Halcrow 	crypt_stat = &(ecryptfs_inode_to_private(
640237fead6SMichael Halcrow 			       page->mapping->host)->crypt_stat);
641237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(page->mapping->host);
642e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
643237fead6SMichael Halcrow 		rc = ecryptfs_do_readpage(file, page, page->index);
644237fead6SMichael Halcrow 		if (rc)
645237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to copy "
646237fead6SMichael Halcrow 					"page at index [0x%.16x]\n",
647237fead6SMichael Halcrow 					page->index);
648237fead6SMichael Halcrow 		goto out;
649237fead6SMichael Halcrow 	}
650237fead6SMichael Halcrow 	num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
651237fead6SMichael Halcrow 	base_extent = (page->index * num_extents_per_page);
652237fead6SMichael Halcrow 	lower_page_virt = kmem_cache_alloc(ecryptfs_lower_page_cache,
653e94b1766SChristoph Lameter 					   GFP_KERNEL);
654237fead6SMichael Halcrow 	if (!lower_page_virt) {
655237fead6SMichael Halcrow 		rc = -ENOMEM;
656237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error getting page for encrypted "
657237fead6SMichael Halcrow 				"lower page(s)\n");
658237fead6SMichael Halcrow 		goto out;
659237fead6SMichael Halcrow 	}
660237fead6SMichael Halcrow 	lower_page = virt_to_page(lower_page_virt);
661237fead6SMichael Halcrow 	page_state = ECRYPTFS_PAGE_STATE_UNREAD;
662237fead6SMichael Halcrow 	while (extent_offset < num_extents_per_page) {
663237fead6SMichael Halcrow 		ecryptfs_extent_to_lwr_pg_idx_and_offset(
664237fead6SMichael Halcrow 			&lower_page_idx, &byte_offset, crypt_stat,
665237fead6SMichael Halcrow 			(base_extent + extent_offset));
666237fead6SMichael Halcrow 		if (prior_lower_page_idx != lower_page_idx
667237fead6SMichael Halcrow 		    || page_state == ECRYPTFS_PAGE_STATE_UNREAD) {
668237fead6SMichael Halcrow 			rc = ecryptfs_do_readpage(file, lower_page,
669237fead6SMichael Halcrow 						  lower_page_idx);
670237fead6SMichael Halcrow 			if (rc) {
671237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error reading "
672237fead6SMichael Halcrow 						"lower encrypted page; rc = "
673237fead6SMichael Halcrow 						"[%d]\n", rc);
674237fead6SMichael Halcrow 				goto out;
675237fead6SMichael Halcrow 			}
676237fead6SMichael Halcrow 			prior_lower_page_idx = lower_page_idx;
677237fead6SMichael Halcrow 			page_state = ECRYPTFS_PAGE_STATE_READ;
678237fead6SMichael Halcrow 		}
679237fead6SMichael Halcrow 		rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
680237fead6SMichael Halcrow 					(base_extent + extent_offset));
681237fead6SMichael Halcrow 		if (rc) {
682237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
683237fead6SMichael Halcrow 					"derive IV for extent [0x%.16x]; rc = "
684237fead6SMichael Halcrow 					"[%d]\n",
685237fead6SMichael Halcrow 					(base_extent + extent_offset), rc);
686237fead6SMichael Halcrow 			goto out;
687237fead6SMichael Halcrow 		}
688237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
689237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
690237fead6SMichael Halcrow 					"with iv:\n");
691237fead6SMichael Halcrow 			ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
692237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
693237fead6SMichael Halcrow 					"decryption:\n");
694237fead6SMichael Halcrow 			ecryptfs_dump_hex((lower_page_virt + byte_offset), 8);
695237fead6SMichael Halcrow 		}
696237fead6SMichael Halcrow 		rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
697237fead6SMichael Halcrow 						  (extent_offset
698237fead6SMichael Halcrow 						   * crypt_stat->extent_size),
699237fead6SMichael Halcrow 						  lower_page, byte_offset,
700237fead6SMichael Halcrow 						  crypt_stat->extent_size,
701237fead6SMichael Halcrow 						  extent_iv);
702237fead6SMichael Halcrow 		if (rc != crypt_stat->extent_size) {
703237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
704237fead6SMichael Halcrow 					"decrypt extent [0x%.16x]\n",
705237fead6SMichael Halcrow 					(base_extent + extent_offset));
706237fead6SMichael Halcrow 			goto out;
707237fead6SMichael Halcrow 		}
708237fead6SMichael Halcrow 		rc = 0;
709237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
710237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
711237fead6SMichael Halcrow 					"decryption:\n");
712237fead6SMichael Halcrow 			ecryptfs_dump_hex((char *)(page_address(page)
713237fead6SMichael Halcrow 						   + byte_offset), 8);
714237fead6SMichael Halcrow 		}
715237fead6SMichael Halcrow 		extent_offset++;
716237fead6SMichael Halcrow 	}
717237fead6SMichael Halcrow out:
718237fead6SMichael Halcrow 	if (lower_page_virt)
719237fead6SMichael Halcrow 		kmem_cache_free(ecryptfs_lower_page_cache, lower_page_virt);
720237fead6SMichael Halcrow 	return rc;
721237fead6SMichael Halcrow }
722237fead6SMichael Halcrow 
723237fead6SMichael Halcrow /**
724237fead6SMichael Halcrow  * decrypt_scatterlist
725237fead6SMichael Halcrow  *
726237fead6SMichael Halcrow  * Returns the number of bytes decrypted; negative value on error
727237fead6SMichael Halcrow  */
728237fead6SMichael Halcrow static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
729237fead6SMichael Halcrow 			       struct scatterlist *dest_sg,
730237fead6SMichael Halcrow 			       struct scatterlist *src_sg, int size,
731237fead6SMichael Halcrow 			       unsigned char *iv)
732237fead6SMichael Halcrow {
7338bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
7348bba066fSMichael Halcrow 		.tfm = crypt_stat->tfm,
7358bba066fSMichael Halcrow 		.info = iv,
7368bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
7378bba066fSMichael Halcrow 	};
738237fead6SMichael Halcrow 	int rc = 0;
739237fead6SMichael Halcrow 
740237fead6SMichael Halcrow 	/* Consider doing this once, when the file is opened */
741237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
7428bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
743237fead6SMichael Halcrow 				     crypt_stat->key_size);
744237fead6SMichael Halcrow 	if (rc) {
745237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
746237fead6SMichael Halcrow 				rc);
747237fead6SMichael Halcrow 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
748237fead6SMichael Halcrow 		rc = -EINVAL;
749237fead6SMichael Halcrow 		goto out;
750237fead6SMichael Halcrow 	}
751237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
7528bba066fSMichael Halcrow 	rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
753237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
754237fead6SMichael Halcrow 	if (rc) {
755237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
756237fead6SMichael Halcrow 				rc);
757237fead6SMichael Halcrow 		goto out;
758237fead6SMichael Halcrow 	}
759237fead6SMichael Halcrow 	rc = size;
760237fead6SMichael Halcrow out:
761237fead6SMichael Halcrow 	return rc;
762237fead6SMichael Halcrow }
763237fead6SMichael Halcrow 
764237fead6SMichael Halcrow /**
765237fead6SMichael Halcrow  * ecryptfs_encrypt_page_offset
766237fead6SMichael Halcrow  *
767237fead6SMichael Halcrow  * Returns the number of bytes encrypted
768237fead6SMichael Halcrow  */
769237fead6SMichael Halcrow static int
770237fead6SMichael Halcrow ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
771237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
772237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
773237fead6SMichael Halcrow 			     unsigned char *iv)
774237fead6SMichael Halcrow {
775237fead6SMichael Halcrow 	struct scatterlist src_sg, dst_sg;
776237fead6SMichael Halcrow 
777237fead6SMichael Halcrow 	src_sg.page = src_page;
778237fead6SMichael Halcrow 	src_sg.offset = src_offset;
779237fead6SMichael Halcrow 	src_sg.length = size;
780237fead6SMichael Halcrow 	dst_sg.page = dst_page;
781237fead6SMichael Halcrow 	dst_sg.offset = dst_offset;
782237fead6SMichael Halcrow 	dst_sg.length = size;
783237fead6SMichael Halcrow 	return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
784237fead6SMichael Halcrow }
785237fead6SMichael Halcrow 
786237fead6SMichael Halcrow /**
787237fead6SMichael Halcrow  * ecryptfs_decrypt_page_offset
788237fead6SMichael Halcrow  *
789237fead6SMichael Halcrow  * Returns the number of bytes decrypted
790237fead6SMichael Halcrow  */
791237fead6SMichael Halcrow static int
792237fead6SMichael Halcrow ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
793237fead6SMichael Halcrow 			     struct page *dst_page, int dst_offset,
794237fead6SMichael Halcrow 			     struct page *src_page, int src_offset, int size,
795237fead6SMichael Halcrow 			     unsigned char *iv)
796237fead6SMichael Halcrow {
797237fead6SMichael Halcrow 	struct scatterlist src_sg, dst_sg;
798237fead6SMichael Halcrow 
799237fead6SMichael Halcrow 	src_sg.page = src_page;
800237fead6SMichael Halcrow 	src_sg.offset = src_offset;
801237fead6SMichael Halcrow 	src_sg.length = size;
802237fead6SMichael Halcrow 	dst_sg.page = dst_page;
803237fead6SMichael Halcrow 	dst_sg.offset = dst_offset;
804237fead6SMichael Halcrow 	dst_sg.length = size;
805237fead6SMichael Halcrow 	return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
806237fead6SMichael Halcrow }
807237fead6SMichael Halcrow 
808237fead6SMichael Halcrow #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
809237fead6SMichael Halcrow 
810237fead6SMichael Halcrow /**
811237fead6SMichael Halcrow  * ecryptfs_init_crypt_ctx
812237fead6SMichael Halcrow  * @crypt_stat: Uninitilized crypt stats structure
813237fead6SMichael Halcrow  *
814237fead6SMichael Halcrow  * Initialize the crypto context.
815237fead6SMichael Halcrow  *
816237fead6SMichael Halcrow  * TODO: Performance: Keep a cache of initialized cipher contexts;
817237fead6SMichael Halcrow  * only init if needed
818237fead6SMichael Halcrow  */
819237fead6SMichael Halcrow int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
820237fead6SMichael Halcrow {
8218bba066fSMichael Halcrow 	char *full_alg_name;
822237fead6SMichael Halcrow 	int rc = -EINVAL;
823237fead6SMichael Halcrow 
824237fead6SMichael Halcrow 	if (!crypt_stat->cipher) {
825237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "No cipher specified\n");
826237fead6SMichael Halcrow 		goto out;
827237fead6SMichael Halcrow 	}
828237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG,
829237fead6SMichael Halcrow 			"Initializing cipher [%s]; strlen = [%d]; "
830237fead6SMichael Halcrow 			"key_size_bits = [%d]\n",
831237fead6SMichael Halcrow 			crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
832237fead6SMichael Halcrow 			crypt_stat->key_size << 3);
833237fead6SMichael Halcrow 	if (crypt_stat->tfm) {
834237fead6SMichael Halcrow 		rc = 0;
835237fead6SMichael Halcrow 		goto out;
836237fead6SMichael Halcrow 	}
837237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
8388bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
8398bba066fSMichael Halcrow 						    crypt_stat->cipher, "cbc");
8408bba066fSMichael Halcrow 	if (rc)
8418bba066fSMichael Halcrow 		goto out;
8428bba066fSMichael Halcrow 	crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
8438bba066fSMichael Halcrow 						 CRYPTO_ALG_ASYNC);
8448bba066fSMichael Halcrow 	kfree(full_alg_name);
845de88777eSAkinobu Mita 	if (IS_ERR(crypt_stat->tfm)) {
846de88777eSAkinobu Mita 		rc = PTR_ERR(crypt_stat->tfm);
847237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
848237fead6SMichael Halcrow 				"Error initializing cipher [%s]\n",
849237fead6SMichael Halcrow 				crypt_stat->cipher);
8508bba066fSMichael Halcrow 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
851237fead6SMichael Halcrow 		goto out;
852237fead6SMichael Halcrow 	}
853f1ddcaf3SHerbert Xu 	crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
8548bba066fSMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
855237fead6SMichael Halcrow 	rc = 0;
856237fead6SMichael Halcrow out:
857237fead6SMichael Halcrow 	return rc;
858237fead6SMichael Halcrow }
859237fead6SMichael Halcrow 
860237fead6SMichael Halcrow static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
861237fead6SMichael Halcrow {
862237fead6SMichael Halcrow 	int extent_size_tmp;
863237fead6SMichael Halcrow 
864237fead6SMichael Halcrow 	crypt_stat->extent_mask = 0xFFFFFFFF;
865237fead6SMichael Halcrow 	crypt_stat->extent_shift = 0;
866237fead6SMichael Halcrow 	if (crypt_stat->extent_size == 0)
867237fead6SMichael Halcrow 		return;
868237fead6SMichael Halcrow 	extent_size_tmp = crypt_stat->extent_size;
869237fead6SMichael Halcrow 	while ((extent_size_tmp & 0x01) == 0) {
870237fead6SMichael Halcrow 		extent_size_tmp >>= 1;
871237fead6SMichael Halcrow 		crypt_stat->extent_mask <<= 1;
872237fead6SMichael Halcrow 		crypt_stat->extent_shift++;
873237fead6SMichael Halcrow 	}
874237fead6SMichael Halcrow }
875237fead6SMichael Halcrow 
876237fead6SMichael Halcrow void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
877237fead6SMichael Halcrow {
878237fead6SMichael Halcrow 	/* Default values; may be overwritten as we are parsing the
879237fead6SMichael Halcrow 	 * packets. */
880237fead6SMichael Halcrow 	crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
881237fead6SMichael Halcrow 	set_extent_mask_and_shift(crypt_stat);
882237fead6SMichael Halcrow 	crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
883237fead6SMichael Halcrow 	if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
884237fead6SMichael Halcrow 		crypt_stat->header_extent_size =
885237fead6SMichael Halcrow 			ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
886237fead6SMichael Halcrow 	} else
887237fead6SMichael Halcrow 		crypt_stat->header_extent_size = PAGE_CACHE_SIZE;
888dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
889dd2a3b7aSMichael Halcrow 		crypt_stat->num_header_extents_at_front = 0;
890dd2a3b7aSMichael Halcrow 	else
891237fead6SMichael Halcrow 		crypt_stat->num_header_extents_at_front = 1;
892237fead6SMichael Halcrow }
893237fead6SMichael Halcrow 
894237fead6SMichael Halcrow /**
895237fead6SMichael Halcrow  * ecryptfs_compute_root_iv
896237fead6SMichael Halcrow  * @crypt_stats
897237fead6SMichael Halcrow  *
898237fead6SMichael Halcrow  * On error, sets the root IV to all 0's.
899237fead6SMichael Halcrow  */
900237fead6SMichael Halcrow int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
901237fead6SMichael Halcrow {
902237fead6SMichael Halcrow 	int rc = 0;
903237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
904237fead6SMichael Halcrow 
905237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
906237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes <= 0);
907e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
908237fead6SMichael Halcrow 		rc = -EINVAL;
909237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Session key not valid; "
910237fead6SMichael Halcrow 				"cannot generate root IV\n");
911237fead6SMichael Halcrow 		goto out;
912237fead6SMichael Halcrow 	}
913237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
914237fead6SMichael Halcrow 				    crypt_stat->key_size);
915237fead6SMichael Halcrow 	if (rc) {
916237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
917237fead6SMichael Halcrow 				"MD5 while generating root IV\n");
918237fead6SMichael Halcrow 		goto out;
919237fead6SMichael Halcrow 	}
920237fead6SMichael Halcrow 	memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
921237fead6SMichael Halcrow out:
922237fead6SMichael Halcrow 	if (rc) {
923237fead6SMichael Halcrow 		memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
924e2bd99ecSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
925237fead6SMichael Halcrow 	}
926237fead6SMichael Halcrow 	return rc;
927237fead6SMichael Halcrow }
928237fead6SMichael Halcrow 
929237fead6SMichael Halcrow static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
930237fead6SMichael Halcrow {
931237fead6SMichael Halcrow 	get_random_bytes(crypt_stat->key, crypt_stat->key_size);
932e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
933237fead6SMichael Halcrow 	ecryptfs_compute_root_iv(crypt_stat);
934237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
935237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
936237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
937237fead6SMichael Halcrow 				  crypt_stat->key_size);
938237fead6SMichael Halcrow 	}
939237fead6SMichael Halcrow }
940237fead6SMichael Halcrow 
941237fead6SMichael Halcrow /**
94217398957SMichael Halcrow  * ecryptfs_copy_mount_wide_flags_to_inode_flags
94317398957SMichael Halcrow  *
94417398957SMichael Halcrow  * This function propagates the mount-wide flags to individual inode
94517398957SMichael Halcrow  * flags.
94617398957SMichael Halcrow  */
94717398957SMichael Halcrow static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
94817398957SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
94917398957SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
95017398957SMichael Halcrow {
95117398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
95217398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
95317398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
95417398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
95517398957SMichael Halcrow }
95617398957SMichael Halcrow 
957*f4aad16aSMichael Halcrow static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
958*f4aad16aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
959*f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
960*f4aad16aSMichael Halcrow {
961*f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
962*f4aad16aSMichael Halcrow 	int rc = 0;
963*f4aad16aSMichael Halcrow 
964*f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
965*f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
966*f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
967*f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
968*f4aad16aSMichael Halcrow 		rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
969*f4aad16aSMichael Halcrow 		if (rc) {
970*f4aad16aSMichael Halcrow 			printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
971*f4aad16aSMichael Halcrow 			mutex_unlock(
972*f4aad16aSMichael Halcrow 				&mount_crypt_stat->global_auth_tok_list_mutex);
973*f4aad16aSMichael Halcrow 			goto out;
974*f4aad16aSMichael Halcrow 		}
975*f4aad16aSMichael Halcrow 	}
976*f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
977*f4aad16aSMichael Halcrow out:
978*f4aad16aSMichael Halcrow 	return rc;
979*f4aad16aSMichael Halcrow }
980*f4aad16aSMichael Halcrow 
98117398957SMichael Halcrow /**
982237fead6SMichael Halcrow  * ecryptfs_set_default_crypt_stat_vals
983237fead6SMichael Halcrow  * @crypt_stat
984237fead6SMichael Halcrow  *
985237fead6SMichael Halcrow  * Default values in the event that policy does not override them.
986237fead6SMichael Halcrow  */
987237fead6SMichael Halcrow static void ecryptfs_set_default_crypt_stat_vals(
988237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
989237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
990237fead6SMichael Halcrow {
99117398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
99217398957SMichael Halcrow 						      mount_crypt_stat);
993237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
994237fead6SMichael Halcrow 	strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
995237fead6SMichael Halcrow 	crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
996e2bd99ecSMichael Halcrow 	crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
997237fead6SMichael Halcrow 	crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
998237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = mount_crypt_stat;
999237fead6SMichael Halcrow }
1000237fead6SMichael Halcrow 
1001237fead6SMichael Halcrow /**
1002237fead6SMichael Halcrow  * ecryptfs_new_file_context
1003237fead6SMichael Halcrow  * @ecryptfs_dentry
1004237fead6SMichael Halcrow  *
1005237fead6SMichael Halcrow  * If the crypto context for the file has not yet been established,
1006237fead6SMichael Halcrow  * this is where we do that.  Establishing a new crypto context
1007237fead6SMichael Halcrow  * involves the following decisions:
1008237fead6SMichael Halcrow  *  - What cipher to use?
1009237fead6SMichael Halcrow  *  - What set of authentication tokens to use?
1010237fead6SMichael Halcrow  * Here we just worry about getting enough information into the
1011237fead6SMichael Halcrow  * authentication tokens so that we know that they are available.
1012237fead6SMichael Halcrow  * We associate the available authentication tokens with the new file
1013237fead6SMichael Halcrow  * via the set of signatures in the crypt_stat struct.  Later, when
1014237fead6SMichael Halcrow  * the headers are actually written out, we may again defer to
1015237fead6SMichael Halcrow  * userspace to perform the encryption of the session key; for the
1016237fead6SMichael Halcrow  * foreseeable future, this will be the case with public key packets.
1017237fead6SMichael Halcrow  *
1018237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
1019237fead6SMichael Halcrow  */
1020237fead6SMichael Halcrow /* Associate an authentication token(s) with the file */
1021237fead6SMichael Halcrow int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
1022237fead6SMichael Halcrow {
1023237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1024237fead6SMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1025237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1026237fead6SMichael Halcrow 	    &ecryptfs_superblock_to_private(
1027237fead6SMichael Halcrow 		    ecryptfs_dentry->d_sb)->mount_crypt_stat;
1028237fead6SMichael Halcrow 	int cipher_name_len;
1029*f4aad16aSMichael Halcrow 	int rc = 0;
1030237fead6SMichael Halcrow 
1031237fead6SMichael Halcrow 	ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
1032*f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1033*f4aad16aSMichael Halcrow 	BUG_ON(mount_crypt_stat->num_global_auth_toks == 0);
1034*f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1035e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
1036e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
103717398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
103817398957SMichael Halcrow 						      mount_crypt_stat);
1039*f4aad16aSMichael Halcrow 	rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
1040*f4aad16aSMichael Halcrow 							 mount_crypt_stat);
1041*f4aad16aSMichael Halcrow 	if (rc) {
1042*f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
1043*f4aad16aSMichael Halcrow 		       "to the inode key sigs; rc = [%d]\n", rc);
1044*f4aad16aSMichael Halcrow 		goto out;
1045*f4aad16aSMichael Halcrow 	}
1046237fead6SMichael Halcrow 	cipher_name_len =
1047237fead6SMichael Halcrow 		strlen(mount_crypt_stat->global_default_cipher_name);
1048237fead6SMichael Halcrow 	memcpy(crypt_stat->cipher,
1049237fead6SMichael Halcrow 	       mount_crypt_stat->global_default_cipher_name,
1050237fead6SMichael Halcrow 	       cipher_name_len);
1051237fead6SMichael Halcrow 	crypt_stat->cipher[cipher_name_len] = '\0';
1052237fead6SMichael Halcrow 	crypt_stat->key_size =
1053237fead6SMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size;
1054237fead6SMichael Halcrow 	ecryptfs_generate_new_key(crypt_stat);
1055237fead6SMichael Halcrow 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
1056237fead6SMichael Halcrow 	if (rc)
1057237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1058237fead6SMichael Halcrow 				"context for cipher [%s]: rc = [%d]\n",
1059237fead6SMichael Halcrow 				crypt_stat->cipher, rc);
1060*f4aad16aSMichael Halcrow out:
1061237fead6SMichael Halcrow 	return rc;
1062237fead6SMichael Halcrow }
1063237fead6SMichael Halcrow 
1064237fead6SMichael Halcrow /**
1065237fead6SMichael Halcrow  * contains_ecryptfs_marker - check for the ecryptfs marker
1066237fead6SMichael Halcrow  * @data: The data block in which to check
1067237fead6SMichael Halcrow  *
1068237fead6SMichael Halcrow  * Returns one if marker found; zero if not found
1069237fead6SMichael Halcrow  */
1070dd2a3b7aSMichael Halcrow static int contains_ecryptfs_marker(char *data)
1071237fead6SMichael Halcrow {
1072237fead6SMichael Halcrow 	u32 m_1, m_2;
1073237fead6SMichael Halcrow 
1074237fead6SMichael Halcrow 	memcpy(&m_1, data, 4);
1075237fead6SMichael Halcrow 	m_1 = be32_to_cpu(m_1);
1076237fead6SMichael Halcrow 	memcpy(&m_2, (data + 4), 4);
1077237fead6SMichael Halcrow 	m_2 = be32_to_cpu(m_2);
1078237fead6SMichael Halcrow 	if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1079237fead6SMichael Halcrow 		return 1;
1080237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1081237fead6SMichael Halcrow 			"MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1082237fead6SMichael Halcrow 			MAGIC_ECRYPTFS_MARKER);
1083237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1084237fead6SMichael Halcrow 			"[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1085237fead6SMichael Halcrow 	return 0;
1086237fead6SMichael Halcrow }
1087237fead6SMichael Halcrow 
1088237fead6SMichael Halcrow struct ecryptfs_flag_map_elem {
1089237fead6SMichael Halcrow 	u32 file_flag;
1090237fead6SMichael Halcrow 	u32 local_flag;
1091237fead6SMichael Halcrow };
1092237fead6SMichael Halcrow 
1093237fead6SMichael Halcrow /* Add support for additional flags by adding elements here. */
1094237fead6SMichael Halcrow static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1095237fead6SMichael Halcrow 	{0x00000001, ECRYPTFS_ENABLE_HMAC},
1096dd2a3b7aSMichael Halcrow 	{0x00000002, ECRYPTFS_ENCRYPTED},
1097dd2a3b7aSMichael Halcrow 	{0x00000004, ECRYPTFS_METADATA_IN_XATTR}
1098237fead6SMichael Halcrow };
1099237fead6SMichael Halcrow 
1100237fead6SMichael Halcrow /**
1101237fead6SMichael Halcrow  * ecryptfs_process_flags
1102237fead6SMichael Halcrow  * @crypt_stat
1103237fead6SMichael Halcrow  * @page_virt: Source data to be parsed
1104237fead6SMichael Halcrow  * @bytes_read: Updated with the number of bytes read
1105237fead6SMichael Halcrow  *
1106237fead6SMichael Halcrow  * Returns zero on success; non-zero if the flag set is invalid
1107237fead6SMichael Halcrow  */
1108237fead6SMichael Halcrow static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1109237fead6SMichael Halcrow 				  char *page_virt, int *bytes_read)
1110237fead6SMichael Halcrow {
1111237fead6SMichael Halcrow 	int rc = 0;
1112237fead6SMichael Halcrow 	int i;
1113237fead6SMichael Halcrow 	u32 flags;
1114237fead6SMichael Halcrow 
1115237fead6SMichael Halcrow 	memcpy(&flags, page_virt, 4);
1116237fead6SMichael Halcrow 	flags = be32_to_cpu(flags);
1117237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1118237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
1119237fead6SMichael Halcrow 		if (flags & ecryptfs_flag_map[i].file_flag) {
1120e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
1121237fead6SMichael Halcrow 		} else
1122e2bd99ecSMichael Halcrow 			crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
1123237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
1124237fead6SMichael Halcrow 	crypt_stat->file_version = ((flags >> 24) & 0xFF);
1125237fead6SMichael Halcrow 	(*bytes_read) = 4;
1126237fead6SMichael Halcrow 	return rc;
1127237fead6SMichael Halcrow }
1128237fead6SMichael Halcrow 
1129237fead6SMichael Halcrow /**
1130237fead6SMichael Halcrow  * write_ecryptfs_marker
1131237fead6SMichael Halcrow  * @page_virt: The pointer to in a page to begin writing the marker
1132237fead6SMichael Halcrow  * @written: Number of bytes written
1133237fead6SMichael Halcrow  *
1134237fead6SMichael Halcrow  * Marker = 0x3c81b7f5
1135237fead6SMichael Halcrow  */
1136237fead6SMichael Halcrow static void write_ecryptfs_marker(char *page_virt, size_t *written)
1137237fead6SMichael Halcrow {
1138237fead6SMichael Halcrow 	u32 m_1, m_2;
1139237fead6SMichael Halcrow 
1140237fead6SMichael Halcrow 	get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1141237fead6SMichael Halcrow 	m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1142237fead6SMichael Halcrow 	m_1 = cpu_to_be32(m_1);
1143237fead6SMichael Halcrow 	memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1144237fead6SMichael Halcrow 	m_2 = cpu_to_be32(m_2);
1145237fead6SMichael Halcrow 	memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
1146237fead6SMichael Halcrow 	       (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1147237fead6SMichael Halcrow 	(*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1148237fead6SMichael Halcrow }
1149237fead6SMichael Halcrow 
1150237fead6SMichael Halcrow static void
1151237fead6SMichael Halcrow write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1152237fead6SMichael Halcrow 		     size_t *written)
1153237fead6SMichael Halcrow {
1154237fead6SMichael Halcrow 	u32 flags = 0;
1155237fead6SMichael Halcrow 	int i;
1156237fead6SMichael Halcrow 
1157237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1158237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
1159e2bd99ecSMichael Halcrow 		if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
1160237fead6SMichael Halcrow 			flags |= ecryptfs_flag_map[i].file_flag;
1161237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
1162237fead6SMichael Halcrow 	flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1163237fead6SMichael Halcrow 	flags = cpu_to_be32(flags);
1164237fead6SMichael Halcrow 	memcpy(page_virt, &flags, 4);
1165237fead6SMichael Halcrow 	(*written) = 4;
1166237fead6SMichael Halcrow }
1167237fead6SMichael Halcrow 
1168237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem {
1169237fead6SMichael Halcrow 	char cipher_str[16];
1170237fead6SMichael Halcrow 	u16 cipher_code;
1171237fead6SMichael Halcrow };
1172237fead6SMichael Halcrow 
1173237fead6SMichael Halcrow /* Add support for additional ciphers by adding elements here. The
1174237fead6SMichael Halcrow  * cipher_code is whatever OpenPGP applicatoins use to identify the
1175237fead6SMichael Halcrow  * ciphers. List in order of probability. */
1176237fead6SMichael Halcrow static struct ecryptfs_cipher_code_str_map_elem
1177237fead6SMichael Halcrow ecryptfs_cipher_code_str_map[] = {
1178237fead6SMichael Halcrow 	{"aes",RFC2440_CIPHER_AES_128 },
1179237fead6SMichael Halcrow 	{"blowfish", RFC2440_CIPHER_BLOWFISH},
1180237fead6SMichael Halcrow 	{"des3_ede", RFC2440_CIPHER_DES3_EDE},
1181237fead6SMichael Halcrow 	{"cast5", RFC2440_CIPHER_CAST_5},
1182237fead6SMichael Halcrow 	{"twofish", RFC2440_CIPHER_TWOFISH},
1183237fead6SMichael Halcrow 	{"cast6", RFC2440_CIPHER_CAST_6},
1184237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_192},
1185237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_256}
1186237fead6SMichael Halcrow };
1187237fead6SMichael Halcrow 
1188237fead6SMichael Halcrow /**
1189237fead6SMichael Halcrow  * ecryptfs_code_for_cipher_string
1190237fead6SMichael Halcrow  * @str: The string representing the cipher name
1191237fead6SMichael Halcrow  *
1192237fead6SMichael Halcrow  * Returns zero on no match, or the cipher code on match
1193237fead6SMichael Halcrow  */
1194237fead6SMichael Halcrow u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
1195237fead6SMichael Halcrow {
1196237fead6SMichael Halcrow 	int i;
1197237fead6SMichael Halcrow 	u16 code = 0;
1198237fead6SMichael Halcrow 	struct ecryptfs_cipher_code_str_map_elem *map =
1199237fead6SMichael Halcrow 		ecryptfs_cipher_code_str_map;
1200237fead6SMichael Halcrow 
1201237fead6SMichael Halcrow 	if (strcmp(crypt_stat->cipher, "aes") == 0) {
1202237fead6SMichael Halcrow 		switch (crypt_stat->key_size) {
1203237fead6SMichael Halcrow 		case 16:
1204237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_128;
1205237fead6SMichael Halcrow 			break;
1206237fead6SMichael Halcrow 		case 24:
1207237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_192;
1208237fead6SMichael Halcrow 			break;
1209237fead6SMichael Halcrow 		case 32:
1210237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_256;
1211237fead6SMichael Halcrow 		}
1212237fead6SMichael Halcrow 	} else {
1213237fead6SMichael Halcrow 		for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1214237fead6SMichael Halcrow 			if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
1215237fead6SMichael Halcrow 				code = map[i].cipher_code;
1216237fead6SMichael Halcrow 				break;
1217237fead6SMichael Halcrow 			}
1218237fead6SMichael Halcrow 	}
1219237fead6SMichael Halcrow 	return code;
1220237fead6SMichael Halcrow }
1221237fead6SMichael Halcrow 
1222237fead6SMichael Halcrow /**
1223237fead6SMichael Halcrow  * ecryptfs_cipher_code_to_string
1224237fead6SMichael Halcrow  * @str: Destination to write out the cipher name
1225237fead6SMichael Halcrow  * @cipher_code: The code to convert to cipher name string
1226237fead6SMichael Halcrow  *
1227237fead6SMichael Halcrow  * Returns zero on success
1228237fead6SMichael Halcrow  */
1229237fead6SMichael Halcrow int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
1230237fead6SMichael Halcrow {
1231237fead6SMichael Halcrow 	int rc = 0;
1232237fead6SMichael Halcrow 	int i;
1233237fead6SMichael Halcrow 
1234237fead6SMichael Halcrow 	str[0] = '\0';
1235237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1236237fead6SMichael Halcrow 		if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1237237fead6SMichael Halcrow 			strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1238237fead6SMichael Halcrow 	if (str[0] == '\0') {
1239237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1240237fead6SMichael Halcrow 				"[%d]\n", cipher_code);
1241237fead6SMichael Halcrow 		rc = -EINVAL;
1242237fead6SMichael Halcrow 	}
1243237fead6SMichael Halcrow 	return rc;
1244237fead6SMichael Halcrow }
1245237fead6SMichael Halcrow 
1246237fead6SMichael Halcrow /**
1247237fead6SMichael Halcrow  * ecryptfs_read_header_region
1248237fead6SMichael Halcrow  * @data
1249237fead6SMichael Halcrow  * @dentry
1250237fead6SMichael Halcrow  * @nd
1251237fead6SMichael Halcrow  *
1252237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
1253237fead6SMichael Halcrow  */
1254dd2a3b7aSMichael Halcrow static int ecryptfs_read_header_region(char *data, struct dentry *dentry,
1255237fead6SMichael Halcrow 				       struct vfsmount *mnt)
1256237fead6SMichael Halcrow {
12577ff1d74fSMichael Halcrow 	struct file *lower_file;
1258237fead6SMichael Halcrow 	mm_segment_t oldfs;
1259237fead6SMichael Halcrow 	int rc;
1260237fead6SMichael Halcrow 
12617ff1d74fSMichael Halcrow 	if ((rc = ecryptfs_open_lower_file(&lower_file, dentry, mnt,
12627ff1d74fSMichael Halcrow 					   O_RDONLY))) {
12637ff1d74fSMichael Halcrow 		printk(KERN_ERR
12647ff1d74fSMichael Halcrow 		       "Error opening lower_file to read header region\n");
1265237fead6SMichael Halcrow 		goto out;
1266237fead6SMichael Halcrow 	}
12677ff1d74fSMichael Halcrow 	lower_file->f_pos = 0;
1268237fead6SMichael Halcrow 	oldfs = get_fs();
1269237fead6SMichael Halcrow 	set_fs(get_ds());
1270237fead6SMichael Halcrow 	/* For releases 0.1 and 0.2, all of the header information
1271237fead6SMichael Halcrow 	 * fits in the first data extent-sized region. */
12727ff1d74fSMichael Halcrow 	rc = lower_file->f_op->read(lower_file, (char __user *)data,
12737ff1d74fSMichael Halcrow 			      ECRYPTFS_DEFAULT_EXTENT_SIZE, &lower_file->f_pos);
1274237fead6SMichael Halcrow 	set_fs(oldfs);
12757ff1d74fSMichael Halcrow 	if ((rc = ecryptfs_close_lower_file(lower_file))) {
12767ff1d74fSMichael Halcrow 		printk(KERN_ERR "Error closing lower_file\n");
12777ff1d74fSMichael Halcrow 		goto out;
12787ff1d74fSMichael Halcrow 	}
1279237fead6SMichael Halcrow 	rc = 0;
1280237fead6SMichael Halcrow out:
1281237fead6SMichael Halcrow 	return rc;
1282237fead6SMichael Halcrow }
1283237fead6SMichael Halcrow 
1284dd2a3b7aSMichael Halcrow int ecryptfs_read_and_validate_header_region(char *data, struct dentry *dentry,
1285dd2a3b7aSMichael Halcrow 					     struct vfsmount *mnt)
1286dd2a3b7aSMichael Halcrow {
1287dd2a3b7aSMichael Halcrow 	int rc;
1288dd2a3b7aSMichael Halcrow 
1289dd2a3b7aSMichael Halcrow 	rc = ecryptfs_read_header_region(data, dentry, mnt);
1290dd2a3b7aSMichael Halcrow 	if (rc)
1291dd2a3b7aSMichael Halcrow 		goto out;
1292dd2a3b7aSMichael Halcrow 	if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES))
1293dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1294dd2a3b7aSMichael Halcrow out:
1295dd2a3b7aSMichael Halcrow 	return rc;
1296dd2a3b7aSMichael Halcrow }
1297dd2a3b7aSMichael Halcrow 
1298dd2a3b7aSMichael Halcrow 
1299e77a56ddSMichael Halcrow void
1300e77a56ddSMichael Halcrow ecryptfs_write_header_metadata(char *virt,
1301e77a56ddSMichael Halcrow 			       struct ecryptfs_crypt_stat *crypt_stat,
1302237fead6SMichael Halcrow 			       size_t *written)
1303237fead6SMichael Halcrow {
1304237fead6SMichael Halcrow 	u32 header_extent_size;
1305237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1306237fead6SMichael Halcrow 
1307237fead6SMichael Halcrow 	header_extent_size = (u32)crypt_stat->header_extent_size;
1308237fead6SMichael Halcrow 	num_header_extents_at_front =
1309237fead6SMichael Halcrow 		(u16)crypt_stat->num_header_extents_at_front;
1310237fead6SMichael Halcrow 	header_extent_size = cpu_to_be32(header_extent_size);
1311237fead6SMichael Halcrow 	memcpy(virt, &header_extent_size, 4);
1312237fead6SMichael Halcrow 	virt += 4;
1313237fead6SMichael Halcrow 	num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
1314237fead6SMichael Halcrow 	memcpy(virt, &num_header_extents_at_front, 2);
1315237fead6SMichael Halcrow 	(*written) = 6;
1316237fead6SMichael Halcrow }
1317237fead6SMichael Halcrow 
1318237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_0;
1319237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_1;
1320237fead6SMichael Halcrow struct kmem_cache *ecryptfs_header_cache_2;
1321237fead6SMichael Halcrow 
1322237fead6SMichael Halcrow /**
1323237fead6SMichael Halcrow  * ecryptfs_write_headers_virt
1324237fead6SMichael Halcrow  * @page_virt
1325237fead6SMichael Halcrow  * @crypt_stat
1326237fead6SMichael Halcrow  * @ecryptfs_dentry
1327237fead6SMichael Halcrow  *
1328237fead6SMichael Halcrow  * Format version: 1
1329237fead6SMichael Halcrow  *
1330237fead6SMichael Halcrow  *   Header Extent:
1331237fead6SMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
1332237fead6SMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
1333237fead6SMichael Halcrow  *     Octets 16-19:      Flags
1334237fead6SMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
1335237fead6SMichael Halcrow  *      Octets 17-18:     Reserved
1336237fead6SMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
1337237fead6SMichael Halcrow  *                        Bit 2: Encrypted?
1338237fead6SMichael Halcrow  *                        Bits 3-8: Reserved
1339237fead6SMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
1340237fead6SMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
1341237fead6SMichael Halcrow  *                        (big-endian)
1342237fead6SMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
1343237fead6SMichael Halcrow  *   Data Extent 0:
1344237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1345237fead6SMichael Halcrow  *   Data Extent 1:
1346237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1347237fead6SMichael Halcrow  *   ...
1348237fead6SMichael Halcrow  *
1349237fead6SMichael Halcrow  * Returns zero on success
1350237fead6SMichael Halcrow  */
1351dd2a3b7aSMichael Halcrow static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
1352237fead6SMichael Halcrow 				       struct ecryptfs_crypt_stat *crypt_stat,
1353237fead6SMichael Halcrow 				       struct dentry *ecryptfs_dentry)
1354237fead6SMichael Halcrow {
1355237fead6SMichael Halcrow 	int rc;
1356237fead6SMichael Halcrow 	size_t written;
1357237fead6SMichael Halcrow 	size_t offset;
1358237fead6SMichael Halcrow 
1359237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1360237fead6SMichael Halcrow 	write_ecryptfs_marker((page_virt + offset), &written);
1361237fead6SMichael Halcrow 	offset += written;
1362237fead6SMichael Halcrow 	write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1363237fead6SMichael Halcrow 	offset += written;
1364e77a56ddSMichael Halcrow 	ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1365e77a56ddSMichael Halcrow 				       &written);
1366237fead6SMichael Halcrow 	offset += written;
1367237fead6SMichael Halcrow 	rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1368237fead6SMichael Halcrow 					      ecryptfs_dentry, &written,
1369237fead6SMichael Halcrow 					      PAGE_CACHE_SIZE - offset);
1370237fead6SMichael Halcrow 	if (rc)
1371237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1372237fead6SMichael Halcrow 				"set; rc = [%d]\n", rc);
1373dd2a3b7aSMichael Halcrow 	if (size) {
1374dd2a3b7aSMichael Halcrow 		offset += written;
1375dd2a3b7aSMichael Halcrow 		*size = offset;
1376dd2a3b7aSMichael Halcrow 	}
1377dd2a3b7aSMichael Halcrow 	return rc;
1378dd2a3b7aSMichael Halcrow }
1379dd2a3b7aSMichael Halcrow 
1380dd2a3b7aSMichael Halcrow static int ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
1381dd2a3b7aSMichael Halcrow 					       struct file *lower_file,
1382dd2a3b7aSMichael Halcrow 					       char *page_virt)
1383dd2a3b7aSMichael Halcrow {
1384dd2a3b7aSMichael Halcrow 	mm_segment_t oldfs;
1385dd2a3b7aSMichael Halcrow 	int current_header_page;
1386dd2a3b7aSMichael Halcrow 	int header_pages;
138770456600SMichael Halcrow 	ssize_t size;
138870456600SMichael Halcrow 	int rc = 0;
1389dd2a3b7aSMichael Halcrow 
1390dd2a3b7aSMichael Halcrow 	lower_file->f_pos = 0;
1391dd2a3b7aSMichael Halcrow 	oldfs = get_fs();
1392dd2a3b7aSMichael Halcrow 	set_fs(get_ds());
139370456600SMichael Halcrow 	size = vfs_write(lower_file, (char __user *)page_virt, PAGE_CACHE_SIZE,
139470456600SMichael Halcrow 			 &lower_file->f_pos);
139570456600SMichael Halcrow 	if (size < 0) {
139670456600SMichael Halcrow 		rc = (int)size;
139770456600SMichael Halcrow 		printk(KERN_ERR "Error attempting to write lower page; "
139870456600SMichael Halcrow 		       "rc = [%d]\n", rc);
139970456600SMichael Halcrow 		set_fs(oldfs);
140070456600SMichael Halcrow 		goto out;
140170456600SMichael Halcrow 	}
1402dd2a3b7aSMichael Halcrow 	header_pages = ((crypt_stat->header_extent_size
1403dd2a3b7aSMichael Halcrow 			 * crypt_stat->num_header_extents_at_front)
1404dd2a3b7aSMichael Halcrow 			/ PAGE_CACHE_SIZE);
1405dd2a3b7aSMichael Halcrow 	memset(page_virt, 0, PAGE_CACHE_SIZE);
1406dd2a3b7aSMichael Halcrow 	current_header_page = 1;
1407dd2a3b7aSMichael Halcrow 	while (current_header_page < header_pages) {
140870456600SMichael Halcrow 		size = vfs_write(lower_file, (char __user *)page_virt,
1409dd2a3b7aSMichael Halcrow 				 PAGE_CACHE_SIZE, &lower_file->f_pos);
141070456600SMichael Halcrow 		if (size < 0) {
141170456600SMichael Halcrow 			rc = (int)size;
141270456600SMichael Halcrow 			printk(KERN_ERR "Error attempting to write lower page; "
141370456600SMichael Halcrow 			       "rc = [%d]\n", rc);
141470456600SMichael Halcrow 			set_fs(oldfs);
141570456600SMichael Halcrow 			goto out;
141670456600SMichael Halcrow 		}
1417dd2a3b7aSMichael Halcrow 		current_header_page++;
1418dd2a3b7aSMichael Halcrow 	}
1419dd2a3b7aSMichael Halcrow 	set_fs(oldfs);
142070456600SMichael Halcrow out:
142170456600SMichael Halcrow 	return rc;
1422dd2a3b7aSMichael Halcrow }
1423dd2a3b7aSMichael Halcrow 
1424dd2a3b7aSMichael Halcrow static int ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1425dd2a3b7aSMichael Halcrow 					    struct ecryptfs_crypt_stat *crypt_stat,
1426dd2a3b7aSMichael Halcrow 					    char *page_virt, size_t size)
1427dd2a3b7aSMichael Halcrow {
1428dd2a3b7aSMichael Halcrow 	int rc;
1429dd2a3b7aSMichael Halcrow 
1430dd2a3b7aSMichael Halcrow 	rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1431dd2a3b7aSMichael Halcrow 			       size, 0);
1432237fead6SMichael Halcrow 	return rc;
1433237fead6SMichael Halcrow }
1434237fead6SMichael Halcrow 
1435237fead6SMichael Halcrow /**
1436dd2a3b7aSMichael Halcrow  * ecryptfs_write_metadata
1437237fead6SMichael Halcrow  * @lower_file: The lower file struct, which was returned from dentry_open
1438237fead6SMichael Halcrow  *
1439237fead6SMichael Halcrow  * Write the file headers out.  This will likely involve a userspace
1440237fead6SMichael Halcrow  * callout, in which the session key is encrypted with one or more
1441237fead6SMichael Halcrow  * public keys and/or the passphrase necessary to do the encryption is
1442237fead6SMichael Halcrow  * retrieved via a prompt.  Exactly what happens at this point should
1443237fead6SMichael Halcrow  * be policy-dependent.
1444237fead6SMichael Halcrow  *
1445237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
1446237fead6SMichael Halcrow  */
1447dd2a3b7aSMichael Halcrow int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1448237fead6SMichael Halcrow 			    struct file *lower_file)
1449237fead6SMichael Halcrow {
1450237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
1451237fead6SMichael Halcrow 	char *page_virt;
1452dd2a3b7aSMichael Halcrow 	size_t size;
1453237fead6SMichael Halcrow 	int rc = 0;
1454237fead6SMichael Halcrow 
1455237fead6SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(
1456237fead6SMichael Halcrow 		ecryptfs_dentry->d_inode)->crypt_stat;
1457e2bd99ecSMichael Halcrow 	if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1458e2bd99ecSMichael Halcrow 		if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
1459237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "Key is "
1460237fead6SMichael Halcrow 					"invalid; bailing out\n");
1461237fead6SMichael Halcrow 			rc = -EINVAL;
1462237fead6SMichael Halcrow 			goto out;
1463237fead6SMichael Halcrow 		}
1464237fead6SMichael Halcrow 	} else {
1465237fead6SMichael Halcrow 		rc = -EINVAL;
1466237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING,
1467237fead6SMichael Halcrow 				"Called with crypt_stat->encrypted == 0\n");
1468237fead6SMichael Halcrow 		goto out;
1469237fead6SMichael Halcrow 	}
1470237fead6SMichael Halcrow 	/* Released in this function */
1471c3762229SRobert P. J. Day 	page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER);
1472237fead6SMichael Halcrow 	if (!page_virt) {
1473237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Out of memory\n");
1474237fead6SMichael Halcrow 		rc = -ENOMEM;
1475237fead6SMichael Halcrow 		goto out;
1476237fead6SMichael Halcrow 	}
1477dd2a3b7aSMichael Halcrow 	rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat,
1478237fead6SMichael Halcrow   					 ecryptfs_dentry);
1479237fead6SMichael Halcrow 	if (unlikely(rc)) {
1480237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
1481237fead6SMichael Halcrow 		memset(page_virt, 0, PAGE_CACHE_SIZE);
1482237fead6SMichael Halcrow 		goto out_free;
1483237fead6SMichael Halcrow 	}
1484dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1485dd2a3b7aSMichael Halcrow 		rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
1486dd2a3b7aSMichael Halcrow 						      crypt_stat, page_virt,
1487dd2a3b7aSMichael Halcrow 						      size);
1488dd2a3b7aSMichael Halcrow 	else
1489dd2a3b7aSMichael Halcrow 		rc = ecryptfs_write_metadata_to_contents(crypt_stat, lower_file,
1490dd2a3b7aSMichael Halcrow 							 page_virt);
1491dd2a3b7aSMichael Halcrow 	if (rc) {
1492dd2a3b7aSMichael Halcrow 		printk(KERN_ERR "Error writing metadata out to lower file; "
1493dd2a3b7aSMichael Halcrow 		       "rc = [%d]\n", rc);
1494dd2a3b7aSMichael Halcrow 		goto out_free;
1495237fead6SMichael Halcrow 	}
1496237fead6SMichael Halcrow out_free:
1497237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_header_cache_0, page_virt);
1498237fead6SMichael Halcrow out:
1499237fead6SMichael Halcrow 	return rc;
1500237fead6SMichael Halcrow }
1501237fead6SMichael Halcrow 
1502dd2a3b7aSMichael Halcrow #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1503dd2a3b7aSMichael Halcrow #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
1504237fead6SMichael Halcrow static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
1505dd2a3b7aSMichael Halcrow 				 char *virt, int *bytes_read,
1506dd2a3b7aSMichael Halcrow 				 int validate_header_size)
1507237fead6SMichael Halcrow {
1508237fead6SMichael Halcrow 	int rc = 0;
1509237fead6SMichael Halcrow 	u32 header_extent_size;
1510237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1511237fead6SMichael Halcrow 
1512237fead6SMichael Halcrow 	memcpy(&header_extent_size, virt, 4);
1513237fead6SMichael Halcrow 	header_extent_size = be32_to_cpu(header_extent_size);
1514237fead6SMichael Halcrow 	virt += 4;
1515237fead6SMichael Halcrow 	memcpy(&num_header_extents_at_front, virt, 2);
1516237fead6SMichael Halcrow 	num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
1517237fead6SMichael Halcrow 	crypt_stat->header_extent_size = (int)header_extent_size;
1518237fead6SMichael Halcrow 	crypt_stat->num_header_extents_at_front =
1519237fead6SMichael Halcrow 		(int)num_header_extents_at_front;
1520237fead6SMichael Halcrow 	(*bytes_read) = 6;
1521dd2a3b7aSMichael Halcrow 	if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
1522dd2a3b7aSMichael Halcrow 	    && ((crypt_stat->header_extent_size
1523237fead6SMichael Halcrow 		 * crypt_stat->num_header_extents_at_front)
1524dd2a3b7aSMichael Halcrow 		< ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
1525237fead6SMichael Halcrow 		rc = -EINVAL;
1526237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Invalid header extent size: "
1527237fead6SMichael Halcrow 				"[%d]\n", crypt_stat->header_extent_size);
1528237fead6SMichael Halcrow 	}
1529237fead6SMichael Halcrow 	return rc;
1530237fead6SMichael Halcrow }
1531237fead6SMichael Halcrow 
1532237fead6SMichael Halcrow /**
1533237fead6SMichael Halcrow  * set_default_header_data
1534237fead6SMichael Halcrow  *
1535237fead6SMichael Halcrow  * For version 0 file format; this function is only for backwards
1536237fead6SMichael Halcrow  * compatibility for files created with the prior versions of
1537237fead6SMichael Halcrow  * eCryptfs.
1538237fead6SMichael Halcrow  */
1539237fead6SMichael Halcrow static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1540237fead6SMichael Halcrow {
1541237fead6SMichael Halcrow 	crypt_stat->header_extent_size = 4096;
1542237fead6SMichael Halcrow 	crypt_stat->num_header_extents_at_front = 1;
1543237fead6SMichael Halcrow }
1544237fead6SMichael Halcrow 
1545237fead6SMichael Halcrow /**
1546237fead6SMichael Halcrow  * ecryptfs_read_headers_virt
1547237fead6SMichael Halcrow  *
1548237fead6SMichael Halcrow  * Read/parse the header data. The header format is detailed in the
1549237fead6SMichael Halcrow  * comment block for the ecryptfs_write_headers_virt() function.
1550237fead6SMichael Halcrow  *
1551237fead6SMichael Halcrow  * Returns zero on success
1552237fead6SMichael Halcrow  */
1553237fead6SMichael Halcrow static int ecryptfs_read_headers_virt(char *page_virt,
1554237fead6SMichael Halcrow 				      struct ecryptfs_crypt_stat *crypt_stat,
1555dd2a3b7aSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
1556dd2a3b7aSMichael Halcrow 				      int validate_header_size)
1557237fead6SMichael Halcrow {
1558237fead6SMichael Halcrow 	int rc = 0;
1559237fead6SMichael Halcrow 	int offset;
1560237fead6SMichael Halcrow 	int bytes_read;
1561237fead6SMichael Halcrow 
1562237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
1563237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1564237fead6SMichael Halcrow 		ecryptfs_dentry->d_sb)->mount_crypt_stat;
1565237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1566237fead6SMichael Halcrow 	rc = contains_ecryptfs_marker(page_virt + offset);
1567237fead6SMichael Halcrow 	if (rc == 0) {
1568237fead6SMichael Halcrow 		rc = -EINVAL;
1569237fead6SMichael Halcrow 		goto out;
1570237fead6SMichael Halcrow 	}
1571237fead6SMichael Halcrow 	offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1572237fead6SMichael Halcrow 	rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1573237fead6SMichael Halcrow 				    &bytes_read);
1574237fead6SMichael Halcrow 	if (rc) {
1575237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1576237fead6SMichael Halcrow 		goto out;
1577237fead6SMichael Halcrow 	}
1578237fead6SMichael Halcrow 	if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1579237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1580237fead6SMichael Halcrow 				"file version [%d] is supported by this "
1581237fead6SMichael Halcrow 				"version of eCryptfs\n",
1582237fead6SMichael Halcrow 				crypt_stat->file_version,
1583237fead6SMichael Halcrow 				ECRYPTFS_SUPPORTED_FILE_VERSION);
1584237fead6SMichael Halcrow 		rc = -EINVAL;
1585237fead6SMichael Halcrow 		goto out;
1586237fead6SMichael Halcrow 	}
1587237fead6SMichael Halcrow 	offset += bytes_read;
1588237fead6SMichael Halcrow 	if (crypt_stat->file_version >= 1) {
1589237fead6SMichael Halcrow 		rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1590dd2a3b7aSMichael Halcrow 					   &bytes_read, validate_header_size);
1591237fead6SMichael Halcrow 		if (rc) {
1592237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Error reading header "
1593237fead6SMichael Halcrow 					"metadata; rc = [%d]\n", rc);
1594237fead6SMichael Halcrow 		}
1595237fead6SMichael Halcrow 		offset += bytes_read;
1596237fead6SMichael Halcrow 	} else
1597237fead6SMichael Halcrow 		set_default_header_data(crypt_stat);
1598237fead6SMichael Halcrow 	rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1599237fead6SMichael Halcrow 				       ecryptfs_dentry);
1600237fead6SMichael Halcrow out:
1601237fead6SMichael Halcrow 	return rc;
1602237fead6SMichael Halcrow }
1603237fead6SMichael Halcrow 
1604237fead6SMichael Halcrow /**
1605dd2a3b7aSMichael Halcrow  * ecryptfs_read_xattr_region
1606dd2a3b7aSMichael Halcrow  *
1607dd2a3b7aSMichael Halcrow  * Attempts to read the crypto metadata from the extended attribute
1608dd2a3b7aSMichael Halcrow  * region of the lower file.
1609dd2a3b7aSMichael Halcrow  */
1610dd2a3b7aSMichael Halcrow int ecryptfs_read_xattr_region(char *page_virt, struct dentry *ecryptfs_dentry)
1611dd2a3b7aSMichael Halcrow {
1612dd2a3b7aSMichael Halcrow 	ssize_t size;
1613dd2a3b7aSMichael Halcrow 	int rc = 0;
1614dd2a3b7aSMichael Halcrow 
1615dd2a3b7aSMichael Halcrow 	size = ecryptfs_getxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME,
1616dd2a3b7aSMichael Halcrow 				 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1617dd2a3b7aSMichael Halcrow 	if (size < 0) {
1618dd2a3b7aSMichael Halcrow 		printk(KERN_DEBUG "Error attempting to read the [%s] "
1619dd2a3b7aSMichael Halcrow 		       "xattr from the lower file; return value = [%zd]\n",
1620dd2a3b7aSMichael Halcrow 		       ECRYPTFS_XATTR_NAME, size);
1621dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1622dd2a3b7aSMichael Halcrow 		goto out;
1623dd2a3b7aSMichael Halcrow 	}
1624dd2a3b7aSMichael Halcrow out:
1625dd2a3b7aSMichael Halcrow 	return rc;
1626dd2a3b7aSMichael Halcrow }
1627dd2a3b7aSMichael Halcrow 
1628dd2a3b7aSMichael Halcrow int ecryptfs_read_and_validate_xattr_region(char *page_virt,
1629dd2a3b7aSMichael Halcrow 					    struct dentry *ecryptfs_dentry)
1630dd2a3b7aSMichael Halcrow {
1631dd2a3b7aSMichael Halcrow 	int rc;
1632dd2a3b7aSMichael Halcrow 
1633dd2a3b7aSMichael Halcrow 	rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry);
1634dd2a3b7aSMichael Halcrow 	if (rc)
1635dd2a3b7aSMichael Halcrow 		goto out;
1636dd2a3b7aSMichael Halcrow 	if (!contains_ecryptfs_marker(page_virt	+ ECRYPTFS_FILE_SIZE_BYTES)) {
1637dd2a3b7aSMichael Halcrow 		printk(KERN_WARNING "Valid data found in [%s] xattr, but "
1638dd2a3b7aSMichael Halcrow 			"the marker is invalid\n", ECRYPTFS_XATTR_NAME);
1639dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1640dd2a3b7aSMichael Halcrow 	}
1641dd2a3b7aSMichael Halcrow out:
1642dd2a3b7aSMichael Halcrow 	return rc;
1643dd2a3b7aSMichael Halcrow }
1644dd2a3b7aSMichael Halcrow 
1645dd2a3b7aSMichael Halcrow /**
1646dd2a3b7aSMichael Halcrow  * ecryptfs_read_metadata
1647dd2a3b7aSMichael Halcrow  *
1648dd2a3b7aSMichael Halcrow  * Common entry point for reading file metadata. From here, we could
1649dd2a3b7aSMichael Halcrow  * retrieve the header information from the header region of the file,
1650dd2a3b7aSMichael Halcrow  * the xattr region of the file, or some other repostory that is
1651dd2a3b7aSMichael Halcrow  * stored separately from the file itself. The current implementation
1652dd2a3b7aSMichael Halcrow  * supports retrieving the metadata information from the file contents
1653dd2a3b7aSMichael Halcrow  * and from the xattr region.
1654237fead6SMichael Halcrow  *
1655237fead6SMichael Halcrow  * Returns zero if valid headers found and parsed; non-zero otherwise
1656237fead6SMichael Halcrow  */
1657dd2a3b7aSMichael Halcrow int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry,
1658237fead6SMichael Halcrow 			   struct file *lower_file)
1659237fead6SMichael Halcrow {
1660237fead6SMichael Halcrow 	int rc = 0;
1661237fead6SMichael Halcrow 	char *page_virt = NULL;
1662237fead6SMichael Halcrow 	mm_segment_t oldfs;
1663237fead6SMichael Halcrow 	ssize_t bytes_read;
1664237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1665237fead6SMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1666e77a56ddSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1667e77a56ddSMichael Halcrow 		&ecryptfs_superblock_to_private(
1668e77a56ddSMichael Halcrow 			ecryptfs_dentry->d_sb)->mount_crypt_stat;
1669237fead6SMichael Halcrow 
1670e77a56ddSMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1671e77a56ddSMichael Halcrow 						      mount_crypt_stat);
1672237fead6SMichael Halcrow 	/* Read the first page from the underlying file */
1673f7267c0cSChristoph Lameter 	page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
1674237fead6SMichael Halcrow 	if (!page_virt) {
1675237fead6SMichael Halcrow 		rc = -ENOMEM;
1676237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n");
1677237fead6SMichael Halcrow 		goto out;
1678237fead6SMichael Halcrow 	}
1679237fead6SMichael Halcrow 	lower_file->f_pos = 0;
1680237fead6SMichael Halcrow 	oldfs = get_fs();
1681237fead6SMichael Halcrow 	set_fs(get_ds());
1682237fead6SMichael Halcrow 	bytes_read = lower_file->f_op->read(lower_file,
1683237fead6SMichael Halcrow 					    (char __user *)page_virt,
1684237fead6SMichael Halcrow 					    ECRYPTFS_DEFAULT_EXTENT_SIZE,
1685237fead6SMichael Halcrow 					    &lower_file->f_pos);
1686237fead6SMichael Halcrow 	set_fs(oldfs);
1687237fead6SMichael Halcrow 	if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) {
1688237fead6SMichael Halcrow 		rc = -EINVAL;
1689237fead6SMichael Halcrow 		goto out;
1690237fead6SMichael Halcrow 	}
1691237fead6SMichael Halcrow 	rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1692dd2a3b7aSMichael Halcrow 					ecryptfs_dentry,
1693dd2a3b7aSMichael Halcrow 					ECRYPTFS_VALIDATE_HEADER_SIZE);
1694dd2a3b7aSMichael Halcrow 	if (rc) {
1695dd2a3b7aSMichael Halcrow 		rc = ecryptfs_read_xattr_region(page_virt,
1696237fead6SMichael Halcrow 						ecryptfs_dentry);
1697237fead6SMichael Halcrow 		if (rc) {
1698dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1699dd2a3b7aSMichael Halcrow 			       "file header region or xattr region\n");
1700237fead6SMichael Halcrow 			rc = -EINVAL;
1701dd2a3b7aSMichael Halcrow 			goto out;
1702dd2a3b7aSMichael Halcrow 		}
1703dd2a3b7aSMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1704dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1705dd2a3b7aSMichael Halcrow 						ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1706dd2a3b7aSMichael Halcrow 		if (rc) {
1707dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1708dd2a3b7aSMichael Halcrow 			       "file xattr region either\n");
1709dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1710dd2a3b7aSMichael Halcrow 		}
1711dd2a3b7aSMichael Halcrow 		if (crypt_stat->mount_crypt_stat->flags
1712dd2a3b7aSMichael Halcrow 		    & ECRYPTFS_XATTR_METADATA_ENABLED) {
1713dd2a3b7aSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1714dd2a3b7aSMichael Halcrow 		} else {
1715dd2a3b7aSMichael Halcrow 			printk(KERN_WARNING "Attempt to access file with "
1716dd2a3b7aSMichael Halcrow 			       "crypto metadata only in the extended attribute "
1717dd2a3b7aSMichael Halcrow 			       "region, but eCryptfs was mounted without "
1718dd2a3b7aSMichael Halcrow 			       "xattr support enabled. eCryptfs will not treat "
1719dd2a3b7aSMichael Halcrow 			       "this like an encrypted file.\n");
1720dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1721dd2a3b7aSMichael Halcrow 		}
1722237fead6SMichael Halcrow 	}
1723237fead6SMichael Halcrow out:
1724237fead6SMichael Halcrow 	if (page_virt) {
1725237fead6SMichael Halcrow 		memset(page_virt, 0, PAGE_CACHE_SIZE);
1726237fead6SMichael Halcrow 		kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1727237fead6SMichael Halcrow 	}
1728237fead6SMichael Halcrow 	return rc;
1729237fead6SMichael Halcrow }
1730237fead6SMichael Halcrow 
1731237fead6SMichael Halcrow /**
1732237fead6SMichael Halcrow  * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1733237fead6SMichael Halcrow  * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1734237fead6SMichael Halcrow  * @name: The plaintext name
1735237fead6SMichael Halcrow  * @length: The length of the plaintext
1736237fead6SMichael Halcrow  * @encoded_name: The encypted name
1737237fead6SMichael Halcrow  *
1738237fead6SMichael Halcrow  * Encrypts and encodes a filename into something that constitutes a
1739237fead6SMichael Halcrow  * valid filename for a filesystem, with printable characters.
1740237fead6SMichael Halcrow  *
1741237fead6SMichael Halcrow  * We assume that we have a properly initialized crypto context,
1742237fead6SMichael Halcrow  * pointed to by crypt_stat->tfm.
1743237fead6SMichael Halcrow  *
1744237fead6SMichael Halcrow  * TODO: Implement filename decoding and decryption here, in place of
1745237fead6SMichael Halcrow  * memcpy. We are keeping the framework around for now to (1)
1746237fead6SMichael Halcrow  * facilitate testing of the components needed to implement filename
1747237fead6SMichael Halcrow  * encryption and (2) to provide a code base from which other
1748237fead6SMichael Halcrow  * developers in the community can easily implement this feature.
1749237fead6SMichael Halcrow  *
1750237fead6SMichael Halcrow  * Returns the length of encoded filename; negative if error
1751237fead6SMichael Halcrow  */
1752237fead6SMichael Halcrow int
1753237fead6SMichael Halcrow ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1754237fead6SMichael Halcrow 			 const char *name, int length, char **encoded_name)
1755237fead6SMichael Halcrow {
1756237fead6SMichael Halcrow 	int error = 0;
1757237fead6SMichael Halcrow 
1758237fead6SMichael Halcrow 	(*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
1759237fead6SMichael Halcrow 	if (!(*encoded_name)) {
1760237fead6SMichael Halcrow 		error = -ENOMEM;
1761237fead6SMichael Halcrow 		goto out;
1762237fead6SMichael Halcrow 	}
1763237fead6SMichael Halcrow 	/* TODO: Filename encryption is a scheduled feature for a
1764237fead6SMichael Halcrow 	 * future version of eCryptfs. This function is here only for
1765237fead6SMichael Halcrow 	 * the purpose of providing a framework for other developers
1766237fead6SMichael Halcrow 	 * to easily implement filename encryption. Hint: Replace this
1767237fead6SMichael Halcrow 	 * memcpy() with a call to encrypt and encode the
1768237fead6SMichael Halcrow 	 * filename, the set the length accordingly. */
1769237fead6SMichael Halcrow 	memcpy((void *)(*encoded_name), (void *)name, length);
1770237fead6SMichael Halcrow 	(*encoded_name)[length] = '\0';
1771237fead6SMichael Halcrow 	error = length + 1;
1772237fead6SMichael Halcrow out:
1773237fead6SMichael Halcrow 	return error;
1774237fead6SMichael Halcrow }
1775237fead6SMichael Halcrow 
1776237fead6SMichael Halcrow /**
1777237fead6SMichael Halcrow  * ecryptfs_decode_filename - converts the cipher text name to plaintext
1778237fead6SMichael Halcrow  * @crypt_stat: The crypt_stat struct associated with the file
1779237fead6SMichael Halcrow  * @name: The filename in cipher text
1780237fead6SMichael Halcrow  * @length: The length of the cipher text name
1781237fead6SMichael Halcrow  * @decrypted_name: The plaintext name
1782237fead6SMichael Halcrow  *
1783237fead6SMichael Halcrow  * Decodes and decrypts the filename.
1784237fead6SMichael Halcrow  *
1785237fead6SMichael Halcrow  * We assume that we have a properly initialized crypto context,
1786237fead6SMichael Halcrow  * pointed to by crypt_stat->tfm.
1787237fead6SMichael Halcrow  *
1788237fead6SMichael Halcrow  * TODO: Implement filename decoding and decryption here, in place of
1789237fead6SMichael Halcrow  * memcpy. We are keeping the framework around for now to (1)
1790237fead6SMichael Halcrow  * facilitate testing of the components needed to implement filename
1791237fead6SMichael Halcrow  * encryption and (2) to provide a code base from which other
1792237fead6SMichael Halcrow  * developers in the community can easily implement this feature.
1793237fead6SMichael Halcrow  *
1794237fead6SMichael Halcrow  * Returns the length of decoded filename; negative if error
1795237fead6SMichael Halcrow  */
1796237fead6SMichael Halcrow int
1797237fead6SMichael Halcrow ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1798237fead6SMichael Halcrow 			 const char *name, int length, char **decrypted_name)
1799237fead6SMichael Halcrow {
1800237fead6SMichael Halcrow 	int error = 0;
1801237fead6SMichael Halcrow 
1802237fead6SMichael Halcrow 	(*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
1803237fead6SMichael Halcrow 	if (!(*decrypted_name)) {
1804237fead6SMichael Halcrow 		error = -ENOMEM;
1805237fead6SMichael Halcrow 		goto out;
1806237fead6SMichael Halcrow 	}
1807237fead6SMichael Halcrow 	/* TODO: Filename encryption is a scheduled feature for a
1808237fead6SMichael Halcrow 	 * future version of eCryptfs. This function is here only for
1809237fead6SMichael Halcrow 	 * the purpose of providing a framework for other developers
1810237fead6SMichael Halcrow 	 * to easily implement filename encryption. Hint: Replace this
1811237fead6SMichael Halcrow 	 * memcpy() with a call to decode and decrypt the
1812237fead6SMichael Halcrow 	 * filename, the set the length accordingly. */
1813237fead6SMichael Halcrow 	memcpy((void *)(*decrypted_name), (void *)name, length);
1814237fead6SMichael Halcrow 	(*decrypted_name)[length + 1] = '\0';	/* Only for convenience
1815237fead6SMichael Halcrow 						 * in printing out the
1816237fead6SMichael Halcrow 						 * string in debug
1817237fead6SMichael Halcrow 						 * messages */
1818237fead6SMichael Halcrow 	error = length;
1819237fead6SMichael Halcrow out:
1820237fead6SMichael Halcrow 	return error;
1821237fead6SMichael Halcrow }
1822237fead6SMichael Halcrow 
1823237fead6SMichael Halcrow /**
1824*f4aad16aSMichael Halcrow  * ecryptfs_process_key_cipher - Perform key cipher initialization.
1825237fead6SMichael Halcrow  * @key_tfm: Crypto context for key material, set by this function
1826e5d9cbdeSMichael Halcrow  * @cipher_name: Name of the cipher
1827e5d9cbdeSMichael Halcrow  * @key_size: Size of the key in bytes
1828237fead6SMichael Halcrow  *
1829237fead6SMichael Halcrow  * Returns zero on success. Any crypto_tfm structs allocated here
1830237fead6SMichael Halcrow  * should be released by other functions, such as on a superblock put
1831237fead6SMichael Halcrow  * event, regardless of whether this function succeeds for fails.
1832237fead6SMichael Halcrow  */
1833237fead6SMichael Halcrow int
1834*f4aad16aSMichael Halcrow ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1835*f4aad16aSMichael Halcrow 			    char *cipher_name, size_t *key_size)
1836237fead6SMichael Halcrow {
1837237fead6SMichael Halcrow 	char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
18388bba066fSMichael Halcrow 	char *full_alg_name;
1839237fead6SMichael Halcrow 	int rc;
1840237fead6SMichael Halcrow 
1841e5d9cbdeSMichael Halcrow 	*key_tfm = NULL;
1842e5d9cbdeSMichael Halcrow 	if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1843237fead6SMichael Halcrow 		rc = -EINVAL;
1844237fead6SMichael Halcrow 		printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
1845e5d9cbdeSMichael Halcrow 		      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1846237fead6SMichael Halcrow 		goto out;
1847237fead6SMichael Halcrow 	}
18488bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
18498bba066fSMichael Halcrow 						    "ecb");
18508bba066fSMichael Halcrow 	if (rc)
18518bba066fSMichael Halcrow 		goto out;
18528bba066fSMichael Halcrow 	*key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
18538bba066fSMichael Halcrow 	kfree(full_alg_name);
18548bba066fSMichael Halcrow 	if (IS_ERR(*key_tfm)) {
18558bba066fSMichael Halcrow 		rc = PTR_ERR(*key_tfm);
1856237fead6SMichael Halcrow 		printk(KERN_ERR "Unable to allocate crypto cipher with name "
18578bba066fSMichael Halcrow 		       "[%s]; rc = [%d]\n", cipher_name, rc);
1858237fead6SMichael Halcrow 		goto out;
1859237fead6SMichael Halcrow 	}
18608bba066fSMichael Halcrow 	crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
18618bba066fSMichael Halcrow 	if (*key_size == 0) {
18628bba066fSMichael Halcrow 		struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
18638bba066fSMichael Halcrow 
18648bba066fSMichael Halcrow 		*key_size = alg->max_keysize;
18658bba066fSMichael Halcrow 	}
1866e5d9cbdeSMichael Halcrow 	get_random_bytes(dummy_key, *key_size);
18678bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
1868237fead6SMichael Halcrow 	if (rc) {
1869237fead6SMichael Halcrow 		printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
1870e5d9cbdeSMichael Halcrow 		       "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
1871237fead6SMichael Halcrow 		rc = -EINVAL;
1872237fead6SMichael Halcrow 		goto out;
1873237fead6SMichael Halcrow 	}
1874237fead6SMichael Halcrow out:
1875237fead6SMichael Halcrow 	return rc;
1876237fead6SMichael Halcrow }
1877*f4aad16aSMichael Halcrow 
1878*f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_tfm_cache;
1879*f4aad16aSMichael Halcrow struct list_head key_tfm_list;
1880*f4aad16aSMichael Halcrow struct mutex key_tfm_list_mutex;
1881*f4aad16aSMichael Halcrow 
1882*f4aad16aSMichael Halcrow int ecryptfs_init_crypto(void)
1883*f4aad16aSMichael Halcrow {
1884*f4aad16aSMichael Halcrow 	mutex_init(&key_tfm_list_mutex);
1885*f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&key_tfm_list);
1886*f4aad16aSMichael Halcrow 	return 0;
1887*f4aad16aSMichael Halcrow }
1888*f4aad16aSMichael Halcrow 
1889*f4aad16aSMichael Halcrow int ecryptfs_destruct_crypto(void)
1890*f4aad16aSMichael Halcrow {
1891*f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1892*f4aad16aSMichael Halcrow 
1893*f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1894*f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1895*f4aad16aSMichael Halcrow 				 key_tfm_list) {
1896*f4aad16aSMichael Halcrow 		list_del(&key_tfm->key_tfm_list);
1897*f4aad16aSMichael Halcrow 		if (key_tfm->key_tfm)
1898*f4aad16aSMichael Halcrow 			crypto_free_blkcipher(key_tfm->key_tfm);
1899*f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1900*f4aad16aSMichael Halcrow 	}
1901*f4aad16aSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
1902*f4aad16aSMichael Halcrow 	return 0;
1903*f4aad16aSMichael Halcrow }
1904*f4aad16aSMichael Halcrow 
1905*f4aad16aSMichael Halcrow int
1906*f4aad16aSMichael Halcrow ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1907*f4aad16aSMichael Halcrow 			 size_t key_size)
1908*f4aad16aSMichael Halcrow {
1909*f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *tmp_tfm;
1910*f4aad16aSMichael Halcrow 	int rc = 0;
1911*f4aad16aSMichael Halcrow 
1912*f4aad16aSMichael Halcrow 	tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1913*f4aad16aSMichael Halcrow 	if (key_tfm != NULL)
1914*f4aad16aSMichael Halcrow 		(*key_tfm) = tmp_tfm;
1915*f4aad16aSMichael Halcrow 	if (!tmp_tfm) {
1916*f4aad16aSMichael Halcrow 		rc = -ENOMEM;
1917*f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to allocate from "
1918*f4aad16aSMichael Halcrow 		       "ecryptfs_key_tfm_cache\n");
1919*f4aad16aSMichael Halcrow 		goto out;
1920*f4aad16aSMichael Halcrow 	}
1921*f4aad16aSMichael Halcrow 	mutex_init(&tmp_tfm->key_tfm_mutex);
1922*f4aad16aSMichael Halcrow 	strncpy(tmp_tfm->cipher_name, cipher_name,
1923*f4aad16aSMichael Halcrow 		ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1924*f4aad16aSMichael Halcrow 	tmp_tfm->key_size = key_size;
1925*f4aad16aSMichael Halcrow 	if ((rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1926*f4aad16aSMichael Halcrow 					      tmp_tfm->cipher_name,
1927*f4aad16aSMichael Halcrow 					      &tmp_tfm->key_size))) {
1928*f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to initialize key TFM "
1929*f4aad16aSMichael Halcrow 		       "cipher with name = [%s]; rc = [%d]\n",
1930*f4aad16aSMichael Halcrow 		       tmp_tfm->cipher_name, rc);
1931*f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1932*f4aad16aSMichael Halcrow 		if (key_tfm != NULL)
1933*f4aad16aSMichael Halcrow 			(*key_tfm) = NULL;
1934*f4aad16aSMichael Halcrow 		goto out;
1935*f4aad16aSMichael Halcrow 	}
1936*f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1937*f4aad16aSMichael Halcrow 	list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1938*f4aad16aSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
1939*f4aad16aSMichael Halcrow out:
1940*f4aad16aSMichael Halcrow 	return rc;
1941*f4aad16aSMichael Halcrow }
1942*f4aad16aSMichael Halcrow 
1943*f4aad16aSMichael Halcrow int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1944*f4aad16aSMichael Halcrow 					       struct mutex **tfm_mutex,
1945*f4aad16aSMichael Halcrow 					       char *cipher_name)
1946*f4aad16aSMichael Halcrow {
1947*f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm;
1948*f4aad16aSMichael Halcrow 	int rc = 0;
1949*f4aad16aSMichael Halcrow 
1950*f4aad16aSMichael Halcrow 	(*tfm) = NULL;
1951*f4aad16aSMichael Halcrow 	(*tfm_mutex) = NULL;
1952*f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1953*f4aad16aSMichael Halcrow 	list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) {
1954*f4aad16aSMichael Halcrow 		if (strcmp(key_tfm->cipher_name, cipher_name) == 0) {
1955*f4aad16aSMichael Halcrow 			(*tfm) = key_tfm->key_tfm;
1956*f4aad16aSMichael Halcrow 			(*tfm_mutex) = &key_tfm->key_tfm_mutex;
1957*f4aad16aSMichael Halcrow 			mutex_unlock(&key_tfm_list_mutex);
1958*f4aad16aSMichael Halcrow 			goto out;
1959*f4aad16aSMichael Halcrow 		}
1960*f4aad16aSMichael Halcrow 	}
1961*f4aad16aSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
1962*f4aad16aSMichael Halcrow 	if ((rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0))) {
1963*f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n",
1964*f4aad16aSMichael Halcrow 		       rc);
1965*f4aad16aSMichael Halcrow 		goto out;
1966*f4aad16aSMichael Halcrow 	}
1967*f4aad16aSMichael Halcrow 	(*tfm) = key_tfm->key_tfm;
1968*f4aad16aSMichael Halcrow 	(*tfm_mutex) = &key_tfm->key_tfm_mutex;
1969*f4aad16aSMichael Halcrow out:
1970*f4aad16aSMichael Halcrow 	return rc;
1971*f4aad16aSMichael Halcrow }
1972