xref: /openbmc/linux/fs/ecryptfs/crypto.c (revision 0df5ed65c14e2c36ed842fcff58118662009f1a1)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  *
4237fead6SMichael Halcrow  * Copyright (C) 1997-2004 Erez Zadok
5237fead6SMichael Halcrow  * Copyright (C) 2001-2004 Stony Brook University
6dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
7237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8237fead6SMichael Halcrow  *   		Michael C. Thompson <mcthomps@us.ibm.com>
9237fead6SMichael Halcrow  *
10237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
11237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
12237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
13237fead6SMichael Halcrow  * License, or (at your option) any later version.
14237fead6SMichael Halcrow  *
15237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
16237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
17237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18237fead6SMichael Halcrow  * General Public License for more details.
19237fead6SMichael Halcrow  *
20237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
21237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
22237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23237fead6SMichael Halcrow  * 02111-1307, USA.
24237fead6SMichael Halcrow  */
25237fead6SMichael Halcrow 
26237fead6SMichael Halcrow #include <linux/fs.h>
27237fead6SMichael Halcrow #include <linux/mount.h>
28237fead6SMichael Halcrow #include <linux/pagemap.h>
29237fead6SMichael Halcrow #include <linux/random.h>
30237fead6SMichael Halcrow #include <linux/compiler.h>
31237fead6SMichael Halcrow #include <linux/key.h>
32237fead6SMichael Halcrow #include <linux/namei.h>
33237fead6SMichael Halcrow #include <linux/crypto.h>
34237fead6SMichael Halcrow #include <linux/file.h>
35237fead6SMichael Halcrow #include <linux/scatterlist.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
3729335c6aSHarvey Harrison #include <asm/unaligned.h>
38237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
39237fead6SMichael Halcrow 
4000a69940STyler Hicks #define DECRYPT		0
4100a69940STyler Hicks #define ENCRYPT		1
4200a69940STyler Hicks 
43237fead6SMichael Halcrow /**
44237fead6SMichael Halcrow  * ecryptfs_to_hex
45237fead6SMichael Halcrow  * @dst: Buffer to take hex character representation of contents of
46237fead6SMichael Halcrow  *       src; must be at least of size (src_size * 2)
47237fead6SMichael Halcrow  * @src: Buffer to be converted to a hex string respresentation
48237fead6SMichael Halcrow  * @src_size: number of bytes to convert
49237fead6SMichael Halcrow  */
50237fead6SMichael Halcrow void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
51237fead6SMichael Halcrow {
52237fead6SMichael Halcrow 	int x;
53237fead6SMichael Halcrow 
54237fead6SMichael Halcrow 	for (x = 0; x < src_size; x++)
55237fead6SMichael Halcrow 		sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
56237fead6SMichael Halcrow }
57237fead6SMichael Halcrow 
58237fead6SMichael Halcrow /**
59237fead6SMichael Halcrow  * ecryptfs_from_hex
60237fead6SMichael Halcrow  * @dst: Buffer to take the bytes from src hex; must be at least of
61237fead6SMichael Halcrow  *       size (src_size / 2)
62237fead6SMichael Halcrow  * @src: Buffer to be converted from a hex string respresentation to raw value
63237fead6SMichael Halcrow  * @dst_size: size of dst buffer, or number of hex characters pairs to convert
64237fead6SMichael Halcrow  */
65237fead6SMichael Halcrow void ecryptfs_from_hex(char *dst, char *src, int dst_size)
66237fead6SMichael Halcrow {
67237fead6SMichael Halcrow 	int x;
68237fead6SMichael Halcrow 	char tmp[3] = { 0, };
69237fead6SMichael Halcrow 
70237fead6SMichael Halcrow 	for (x = 0; x < dst_size; x++) {
71237fead6SMichael Halcrow 		tmp[0] = src[x * 2];
72237fead6SMichael Halcrow 		tmp[1] = src[x * 2 + 1];
73237fead6SMichael Halcrow 		dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
74237fead6SMichael Halcrow 	}
75237fead6SMichael Halcrow }
76237fead6SMichael Halcrow 
77237fead6SMichael Halcrow /**
78237fead6SMichael Halcrow  * ecryptfs_calculate_md5 - calculates the md5 of @src
79237fead6SMichael Halcrow  * @dst: Pointer to 16 bytes of allocated memory
80237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
81237fead6SMichael Halcrow  * @src: Data to be md5'd
82237fead6SMichael Halcrow  * @len: Length of @src
83237fead6SMichael Halcrow  *
84237fead6SMichael Halcrow  * Uses the allocated crypto context that crypt_stat references to
85237fead6SMichael Halcrow  * generate the MD5 sum of the contents of src.
86237fead6SMichael Halcrow  */
87237fead6SMichael Halcrow static int ecryptfs_calculate_md5(char *dst,
88237fead6SMichael Halcrow 				  struct ecryptfs_crypt_stat *crypt_stat,
89237fead6SMichael Halcrow 				  char *src, int len)
90237fead6SMichael Halcrow {
91237fead6SMichael Halcrow 	struct scatterlist sg;
92565d9724SMichael Halcrow 	struct hash_desc desc = {
93565d9724SMichael Halcrow 		.tfm = crypt_stat->hash_tfm,
94565d9724SMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
95565d9724SMichael Halcrow 	};
96565d9724SMichael Halcrow 	int rc = 0;
97237fead6SMichael Halcrow 
98565d9724SMichael Halcrow 	mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
99237fead6SMichael Halcrow 	sg_init_one(&sg, (u8 *)src, len);
100565d9724SMichael Halcrow 	if (!desc.tfm) {
101565d9724SMichael Halcrow 		desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
102565d9724SMichael Halcrow 					     CRYPTO_ALG_ASYNC);
103565d9724SMichael Halcrow 		if (IS_ERR(desc.tfm)) {
104565d9724SMichael Halcrow 			rc = PTR_ERR(desc.tfm);
105237fead6SMichael Halcrow 			ecryptfs_printk(KERN_ERR, "Error attempting to "
106565d9724SMichael Halcrow 					"allocate crypto context; rc = [%d]\n",
107565d9724SMichael Halcrow 					rc);
108237fead6SMichael Halcrow 			goto out;
109237fead6SMichael Halcrow 		}
110565d9724SMichael Halcrow 		crypt_stat->hash_tfm = desc.tfm;
111237fead6SMichael Halcrow 	}
1128a29f2b0SMichael Halcrow 	rc = crypto_hash_init(&desc);
1138a29f2b0SMichael Halcrow 	if (rc) {
1148a29f2b0SMichael Halcrow 		printk(KERN_ERR
1158a29f2b0SMichael Halcrow 		       "%s: Error initializing crypto hash; rc = [%d]\n",
11618d1dbf1SHarvey Harrison 		       __func__, rc);
1178a29f2b0SMichael Halcrow 		goto out;
1188a29f2b0SMichael Halcrow 	}
1198a29f2b0SMichael Halcrow 	rc = crypto_hash_update(&desc, &sg, len);
1208a29f2b0SMichael Halcrow 	if (rc) {
1218a29f2b0SMichael Halcrow 		printk(KERN_ERR
1228a29f2b0SMichael Halcrow 		       "%s: Error updating crypto hash; rc = [%d]\n",
12318d1dbf1SHarvey Harrison 		       __func__, rc);
1248a29f2b0SMichael Halcrow 		goto out;
1258a29f2b0SMichael Halcrow 	}
1268a29f2b0SMichael Halcrow 	rc = crypto_hash_final(&desc, dst);
1278a29f2b0SMichael Halcrow 	if (rc) {
1288a29f2b0SMichael Halcrow 		printk(KERN_ERR
1298a29f2b0SMichael Halcrow 		       "%s: Error finalizing crypto hash; rc = [%d]\n",
13018d1dbf1SHarvey Harrison 		       __func__, rc);
1318a29f2b0SMichael Halcrow 		goto out;
1328a29f2b0SMichael Halcrow 	}
133237fead6SMichael Halcrow out:
1348a29f2b0SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
135237fead6SMichael Halcrow 	return rc;
136237fead6SMichael Halcrow }
137237fead6SMichael Halcrow 
138cd9d67dfSMichael Halcrow static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
1398bba066fSMichael Halcrow 						  char *cipher_name,
1408bba066fSMichael Halcrow 						  char *chaining_modifier)
1418bba066fSMichael Halcrow {
1428bba066fSMichael Halcrow 	int cipher_name_len = strlen(cipher_name);
1438bba066fSMichael Halcrow 	int chaining_modifier_len = strlen(chaining_modifier);
1448bba066fSMichael Halcrow 	int algified_name_len;
1458bba066fSMichael Halcrow 	int rc;
1468bba066fSMichael Halcrow 
1478bba066fSMichael Halcrow 	algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
1488bba066fSMichael Halcrow 	(*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
1497bd473fcSMichael Halcrow 	if (!(*algified_name)) {
1508bba066fSMichael Halcrow 		rc = -ENOMEM;
1518bba066fSMichael Halcrow 		goto out;
1528bba066fSMichael Halcrow 	}
1538bba066fSMichael Halcrow 	snprintf((*algified_name), algified_name_len, "%s(%s)",
1548bba066fSMichael Halcrow 		 chaining_modifier, cipher_name);
1558bba066fSMichael Halcrow 	rc = 0;
1568bba066fSMichael Halcrow out:
1578bba066fSMichael Halcrow 	return rc;
1588bba066fSMichael Halcrow }
1598bba066fSMichael Halcrow 
160237fead6SMichael Halcrow /**
161237fead6SMichael Halcrow  * ecryptfs_derive_iv
162237fead6SMichael Halcrow  * @iv: destination for the derived iv vale
163237fead6SMichael Halcrow  * @crypt_stat: Pointer to crypt_stat struct for the current inode
164d6a13c17SMichael Halcrow  * @offset: Offset of the extent whose IV we are to derive
165237fead6SMichael Halcrow  *
166237fead6SMichael Halcrow  * Generate the initialization vector from the given root IV and page
167237fead6SMichael Halcrow  * offset.
168237fead6SMichael Halcrow  *
169237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
170237fead6SMichael Halcrow  */
171a34f60f7SMichael Halcrow int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
172d6a13c17SMichael Halcrow 		       loff_t offset)
173237fead6SMichael Halcrow {
174237fead6SMichael Halcrow 	int rc = 0;
175237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
176237fead6SMichael Halcrow 	char src[ECRYPTFS_MAX_IV_BYTES + 16];
177237fead6SMichael Halcrow 
178237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
179237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "root iv:\n");
180237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
181237fead6SMichael Halcrow 	}
182237fead6SMichael Halcrow 	/* TODO: It is probably secure to just cast the least
183237fead6SMichael Halcrow 	 * significant bits of the root IV into an unsigned long and
184237fead6SMichael Halcrow 	 * add the offset to that rather than go through all this
185237fead6SMichael Halcrow 	 * hashing business. -Halcrow */
186237fead6SMichael Halcrow 	memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
187237fead6SMichael Halcrow 	memset((src + crypt_stat->iv_bytes), 0, 16);
188d6a13c17SMichael Halcrow 	snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
189237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
190237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "source:\n");
191237fead6SMichael Halcrow 		ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
192237fead6SMichael Halcrow 	}
193237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
194237fead6SMichael Halcrow 				    (crypt_stat->iv_bytes + 16));
195237fead6SMichael Halcrow 	if (rc) {
196237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
197237fead6SMichael Halcrow 				"MD5 while generating IV for a page\n");
198237fead6SMichael Halcrow 		goto out;
199237fead6SMichael Halcrow 	}
200237fead6SMichael Halcrow 	memcpy(iv, dst, crypt_stat->iv_bytes);
201237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
202237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
203237fead6SMichael Halcrow 		ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
204237fead6SMichael Halcrow 	}
205237fead6SMichael Halcrow out:
206237fead6SMichael Halcrow 	return rc;
207237fead6SMichael Halcrow }
208237fead6SMichael Halcrow 
209237fead6SMichael Halcrow /**
210237fead6SMichael Halcrow  * ecryptfs_init_crypt_stat
211237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
212237fead6SMichael Halcrow  *
213237fead6SMichael Halcrow  * Initialize the crypt_stat structure.
214237fead6SMichael Halcrow  */
215237fead6SMichael Halcrow void
216237fead6SMichael Halcrow ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
217237fead6SMichael Halcrow {
218237fead6SMichael Halcrow 	memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
219f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&crypt_stat->keysig_list);
220f4aad16aSMichael Halcrow 	mutex_init(&crypt_stat->keysig_list_mutex);
221237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_mutex);
222237fead6SMichael Halcrow 	mutex_init(&crypt_stat->cs_tfm_mutex);
223565d9724SMichael Halcrow 	mutex_init(&crypt_stat->cs_hash_tfm_mutex);
224e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
225237fead6SMichael Halcrow }
226237fead6SMichael Halcrow 
227237fead6SMichael Halcrow /**
228fcd12835SMichael Halcrow  * ecryptfs_destroy_crypt_stat
229237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
230237fead6SMichael Halcrow  *
231237fead6SMichael Halcrow  * Releases all memory associated with a crypt_stat struct.
232237fead6SMichael Halcrow  */
233fcd12835SMichael Halcrow void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
234237fead6SMichael Halcrow {
235f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
236f4aad16aSMichael Halcrow 
237237fead6SMichael Halcrow 	if (crypt_stat->tfm)
2384dfea4f0STyler Hicks 		crypto_free_ablkcipher(crypt_stat->tfm);
239565d9724SMichael Halcrow 	if (crypt_stat->hash_tfm)
240565d9724SMichael Halcrow 		crypto_free_hash(crypt_stat->hash_tfm);
241f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_sig, key_sig_tmp,
242f4aad16aSMichael Halcrow 				 &crypt_stat->keysig_list, crypt_stat_list) {
243f4aad16aSMichael Halcrow 		list_del(&key_sig->crypt_stat_list);
244f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
245f4aad16aSMichael Halcrow 	}
246237fead6SMichael Halcrow 	memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
247237fead6SMichael Halcrow }
248237fead6SMichael Halcrow 
249fcd12835SMichael Halcrow void ecryptfs_destroy_mount_crypt_stat(
250237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
251237fead6SMichael Halcrow {
252f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
253f4aad16aSMichael Halcrow 
254f4aad16aSMichael Halcrow 	if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
255f4aad16aSMichael Halcrow 		return;
256f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
257f4aad16aSMichael Halcrow 	list_for_each_entry_safe(auth_tok, auth_tok_tmp,
258f4aad16aSMichael Halcrow 				 &mount_crypt_stat->global_auth_tok_list,
259f4aad16aSMichael Halcrow 				 mount_crypt_stat_list) {
260f4aad16aSMichael Halcrow 		list_del(&auth_tok->mount_crypt_stat_list);
261f4aad16aSMichael Halcrow 		if (auth_tok->global_auth_tok_key
262f4aad16aSMichael Halcrow 		    && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
263f4aad16aSMichael Halcrow 			key_put(auth_tok->global_auth_tok_key);
264f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
265f4aad16aSMichael Halcrow 	}
266f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
267237fead6SMichael Halcrow 	memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
268237fead6SMichael Halcrow }
269237fead6SMichael Halcrow 
270237fead6SMichael Halcrow /**
271237fead6SMichael Halcrow  * virt_to_scatterlist
272237fead6SMichael Halcrow  * @addr: Virtual address
273237fead6SMichael Halcrow  * @size: Size of data; should be an even multiple of the block size
274237fead6SMichael Halcrow  * @sg: Pointer to scatterlist array; set to NULL to obtain only
275237fead6SMichael Halcrow  *      the number of scatterlist structs required in array
276237fead6SMichael Halcrow  * @sg_size: Max array size
277237fead6SMichael Halcrow  *
278237fead6SMichael Halcrow  * Fills in a scatterlist array with page references for a passed
279237fead6SMichael Halcrow  * virtual address.
280237fead6SMichael Halcrow  *
281237fead6SMichael Halcrow  * Returns the number of scatterlist structs in array used
282237fead6SMichael Halcrow  */
283237fead6SMichael Halcrow int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
284237fead6SMichael Halcrow 			int sg_size)
285237fead6SMichael Halcrow {
286237fead6SMichael Halcrow 	int i = 0;
287237fead6SMichael Halcrow 	struct page *pg;
288237fead6SMichael Halcrow 	int offset;
289237fead6SMichael Halcrow 	int remainder_of_page;
290237fead6SMichael Halcrow 
29168e3f5ddSHerbert Xu 	sg_init_table(sg, sg_size);
29268e3f5ddSHerbert Xu 
293237fead6SMichael Halcrow 	while (size > 0 && i < sg_size) {
294237fead6SMichael Halcrow 		pg = virt_to_page(addr);
295237fead6SMichael Halcrow 		offset = offset_in_page(addr);
296642f1490SJens Axboe 		sg_set_page(&sg[i], pg, 0, offset);
297237fead6SMichael Halcrow 		remainder_of_page = PAGE_CACHE_SIZE - offset;
298237fead6SMichael Halcrow 		if (size >= remainder_of_page) {
299237fead6SMichael Halcrow 			sg[i].length = remainder_of_page;
300237fead6SMichael Halcrow 			addr += remainder_of_page;
301237fead6SMichael Halcrow 			size -= remainder_of_page;
302237fead6SMichael Halcrow 		} else {
303237fead6SMichael Halcrow 			sg[i].length = size;
304237fead6SMichael Halcrow 			addr += size;
305237fead6SMichael Halcrow 			size = 0;
306237fead6SMichael Halcrow 		}
307237fead6SMichael Halcrow 		i++;
308237fead6SMichael Halcrow 	}
309237fead6SMichael Halcrow 	if (size > 0)
310237fead6SMichael Halcrow 		return -ENOMEM;
311237fead6SMichael Halcrow 	return i;
312237fead6SMichael Halcrow }
313237fead6SMichael Halcrow 
3144dfea4f0STyler Hicks struct extent_crypt_result {
3154dfea4f0STyler Hicks 	struct completion completion;
3164dfea4f0STyler Hicks 	int rc;
3174dfea4f0STyler Hicks };
3184dfea4f0STyler Hicks 
3194dfea4f0STyler Hicks static void extent_crypt_complete(struct crypto_async_request *req, int rc)
3204dfea4f0STyler Hicks {
3214dfea4f0STyler Hicks 	struct extent_crypt_result *ecr = req->data;
3224dfea4f0STyler Hicks 
3234dfea4f0STyler Hicks 	if (rc == -EINPROGRESS)
3244dfea4f0STyler Hicks 		return;
3254dfea4f0STyler Hicks 
3264dfea4f0STyler Hicks 	ecr->rc = rc;
3274dfea4f0STyler Hicks 	complete(&ecr->completion);
3284dfea4f0STyler Hicks }
3294dfea4f0STyler Hicks 
330237fead6SMichael Halcrow /**
33100a69940STyler Hicks  * crypt_scatterlist
332237fead6SMichael Halcrow  * @crypt_stat: Pointer to the crypt_stat struct to initialize.
333*0df5ed65STyler Hicks  * @dst_sg: Destination of the data after performing the crypto operation
33400a69940STyler Hicks  * @src_sg: Data to be encrypted or decrypted
33500a69940STyler Hicks  * @size: Length of data
33600a69940STyler Hicks  * @iv: IV to use
33700a69940STyler Hicks  * @op: ENCRYPT or DECRYPT to indicate the desired operation
338237fead6SMichael Halcrow  *
33900a69940STyler Hicks  * Returns the number of bytes encrypted or decrypted; negative value on error
340237fead6SMichael Halcrow  */
34100a69940STyler Hicks static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
342*0df5ed65STyler Hicks 			     struct scatterlist *dst_sg,
343237fead6SMichael Halcrow 			     struct scatterlist *src_sg, int size,
34400a69940STyler Hicks 			     unsigned char *iv, int op)
345237fead6SMichael Halcrow {
3464dfea4f0STyler Hicks 	struct ablkcipher_request *req = NULL;
3474dfea4f0STyler Hicks 	struct extent_crypt_result ecr;
348237fead6SMichael Halcrow 	int rc = 0;
349237fead6SMichael Halcrow 
350237fead6SMichael Halcrow 	BUG_ON(!crypt_stat || !crypt_stat->tfm
351e2bd99ecSMichael Halcrow 	       || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
352237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
353f24b3887STyler Hicks 		ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
354237fead6SMichael Halcrow 				crypt_stat->key_size);
355237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
356237fead6SMichael Halcrow 				  crypt_stat->key_size);
357237fead6SMichael Halcrow 	}
3584dfea4f0STyler Hicks 
3594dfea4f0STyler Hicks 	init_completion(&ecr.completion);
3604dfea4f0STyler Hicks 
361237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
3624dfea4f0STyler Hicks 	req = ablkcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
3634dfea4f0STyler Hicks 	if (!req) {
3644dfea4f0STyler Hicks 		mutex_unlock(&crypt_stat->cs_tfm_mutex);
3654dfea4f0STyler Hicks 		rc = -ENOMEM;
3664dfea4f0STyler Hicks 		goto out;
3678e3a6f16STrevor Highland 	}
3684dfea4f0STyler Hicks 
3694dfea4f0STyler Hicks 	ablkcipher_request_set_callback(req,
3704dfea4f0STyler Hicks 			CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
3714dfea4f0STyler Hicks 			extent_crypt_complete, &ecr);
3724dfea4f0STyler Hicks 	/* Consider doing this once, when the file is opened */
3734dfea4f0STyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
3744dfea4f0STyler Hicks 		rc = crypto_ablkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
3754dfea4f0STyler Hicks 					      crypt_stat->key_size);
376237fead6SMichael Halcrow 		if (rc) {
3774dfea4f0STyler Hicks 			ecryptfs_printk(KERN_ERR,
3784dfea4f0STyler Hicks 					"Error setting key; rc = [%d]\n",
379237fead6SMichael Halcrow 					rc);
380237fead6SMichael Halcrow 			mutex_unlock(&crypt_stat->cs_tfm_mutex);
381237fead6SMichael Halcrow 			rc = -EINVAL;
382237fead6SMichael Halcrow 			goto out;
383237fead6SMichael Halcrow 		}
3844dfea4f0STyler Hicks 		crypt_stat->flags |= ECRYPTFS_KEY_SET;
3854dfea4f0STyler Hicks 	}
386237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
387*0df5ed65STyler Hicks 	ablkcipher_request_set_crypt(req, src_sg, dst_sg, size, iv);
38800a69940STyler Hicks 	rc = op == ENCRYPT ? crypto_ablkcipher_encrypt(req) :
38900a69940STyler Hicks 			     crypto_ablkcipher_decrypt(req);
3904dfea4f0STyler Hicks 	if (rc == -EINPROGRESS || rc == -EBUSY) {
3914dfea4f0STyler Hicks 		struct extent_crypt_result *ecr = req->base.data;
3924dfea4f0STyler Hicks 
3934dfea4f0STyler Hicks 		wait_for_completion(&ecr->completion);
3944dfea4f0STyler Hicks 		rc = ecr->rc;
3954dfea4f0STyler Hicks 		INIT_COMPLETION(ecr->completion);
3964dfea4f0STyler Hicks 	}
397237fead6SMichael Halcrow out:
3984dfea4f0STyler Hicks 	ablkcipher_request_free(req);
399237fead6SMichael Halcrow 	return rc;
400237fead6SMichael Halcrow }
401237fead6SMichael Halcrow 
402237fead6SMichael Halcrow /**
40324d15266STyler Hicks  * lower_offset_for_page
404237fead6SMichael Halcrow  *
4050216f7f7SMichael Halcrow  * Convert an eCryptfs page index into a lower byte offset
406237fead6SMichael Halcrow  */
40724d15266STyler Hicks static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat,
40824d15266STyler Hicks 				    struct page *page)
409237fead6SMichael Halcrow {
41024d15266STyler Hicks 	return ecryptfs_lower_header_size(crypt_stat) +
41124d15266STyler Hicks 	       (page->index << PAGE_CACHE_SHIFT);
4120216f7f7SMichael Halcrow }
413237fead6SMichael Halcrow 
4140216f7f7SMichael Halcrow /**
415d78de618STyler Hicks  * crypt_extent
4160216f7f7SMichael Halcrow  * @crypt_stat: crypt_stat containing cryptographic context for the
4170216f7f7SMichael Halcrow  *              encryption operation
418*0df5ed65STyler Hicks  * @dst_page: The page to write the result into
419d78de618STyler Hicks  * @src_page: The page to read from
4200216f7f7SMichael Halcrow  * @extent_offset: Page extent offset for use in generating IV
421d78de618STyler Hicks  * @op: ENCRYPT or DECRYPT to indicate the desired operation
4220216f7f7SMichael Halcrow  *
423d78de618STyler Hicks  * Encrypts or decrypts one extent of data.
4240216f7f7SMichael Halcrow  *
4250216f7f7SMichael Halcrow  * Return zero on success; non-zero otherwise
4260216f7f7SMichael Halcrow  */
427*0df5ed65STyler Hicks static int crypt_extent(struct ecryptfs_crypt_stat *crypt_stat,
428*0df5ed65STyler Hicks 			struct page *dst_page,
429d78de618STyler Hicks 			struct page *src_page,
430d78de618STyler Hicks 			unsigned long extent_offset, int op)
4310216f7f7SMichael Halcrow {
432d78de618STyler Hicks 	pgoff_t page_index = op == ENCRYPT ? src_page->index : dst_page->index;
433d6a13c17SMichael Halcrow 	loff_t extent_base;
4340216f7f7SMichael Halcrow 	char extent_iv[ECRYPTFS_MAX_IV_BYTES];
435406c93dfSTyler Hicks 	struct scatterlist src_sg, dst_sg;
436406c93dfSTyler Hicks 	size_t extent_size = crypt_stat->extent_size;
4370216f7f7SMichael Halcrow 	int rc;
4380216f7f7SMichael Halcrow 
439406c93dfSTyler Hicks 	extent_base = (((loff_t)page_index) * (PAGE_CACHE_SIZE / extent_size));
440237fead6SMichael Halcrow 	rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
4410216f7f7SMichael Halcrow 				(extent_base + extent_offset));
442237fead6SMichael Halcrow 	if (rc) {
443888d57bbSJoe Perches 		ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
444888d57bbSJoe Perches 			"extent [0x%.16llx]; rc = [%d]\n",
445888d57bbSJoe Perches 			(unsigned long long)(extent_base + extent_offset), rc);
446237fead6SMichael Halcrow 		goto out;
447237fead6SMichael Halcrow 	}
448406c93dfSTyler Hicks 
449406c93dfSTyler Hicks 	sg_init_table(&src_sg, 1);
450406c93dfSTyler Hicks 	sg_init_table(&dst_sg, 1);
451406c93dfSTyler Hicks 
452406c93dfSTyler Hicks 	sg_set_page(&src_sg, src_page, extent_size,
453406c93dfSTyler Hicks 		    extent_offset * extent_size);
454406c93dfSTyler Hicks 	sg_set_page(&dst_sg, dst_page, extent_size,
455406c93dfSTyler Hicks 		    extent_offset * extent_size);
456406c93dfSTyler Hicks 
457406c93dfSTyler Hicks 	rc = crypt_scatterlist(crypt_stat, &dst_sg, &src_sg, extent_size,
458406c93dfSTyler Hicks 			       extent_iv, op);
4590216f7f7SMichael Halcrow 	if (rc < 0) {
460d78de618STyler Hicks 		printk(KERN_ERR "%s: Error attempting to crypt page with "
461d78de618STyler Hicks 		       "page_index = [%ld], extent_offset = [%ld]; "
462d78de618STyler Hicks 		       "rc = [%d]\n", __func__, page_index, extent_offset, rc);
4630216f7f7SMichael Halcrow 		goto out;
4640216f7f7SMichael Halcrow 	}
4650216f7f7SMichael Halcrow 	rc = 0;
4660216f7f7SMichael Halcrow out:
4670216f7f7SMichael Halcrow 	return rc;
4680216f7f7SMichael Halcrow }
4690216f7f7SMichael Halcrow 
4700216f7f7SMichael Halcrow /**
4710216f7f7SMichael Halcrow  * ecryptfs_encrypt_page
4720216f7f7SMichael Halcrow  * @page: Page mapped from the eCryptfs inode for the file; contains
4730216f7f7SMichael Halcrow  *        decrypted content that needs to be encrypted (to a temporary
4740216f7f7SMichael Halcrow  *        page; not in place) and written out to the lower file
4750216f7f7SMichael Halcrow  *
4760216f7f7SMichael Halcrow  * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
4770216f7f7SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
4780216f7f7SMichael Halcrow  * if the file was created on a machine with an 8K page size
4790216f7f7SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
4800216f7f7SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
4810216f7f7SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
4820216f7f7SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
4830216f7f7SMichael Halcrow  *
4840216f7f7SMichael Halcrow  * Returns zero on success; negative on error
4850216f7f7SMichael Halcrow  */
4860216f7f7SMichael Halcrow int ecryptfs_encrypt_page(struct page *page)
4870216f7f7SMichael Halcrow {
4880216f7f7SMichael Halcrow 	struct inode *ecryptfs_inode;
4890216f7f7SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
4907fcba054SEric Sandeen 	char *enc_extent_virt;
4917fcba054SEric Sandeen 	struct page *enc_extent_page = NULL;
4920216f7f7SMichael Halcrow 	loff_t extent_offset;
4930f896176STyler Hicks 	loff_t lower_offset;
4940216f7f7SMichael Halcrow 	int rc = 0;
4950216f7f7SMichael Halcrow 
4960216f7f7SMichael Halcrow 	ecryptfs_inode = page->mapping->host;
4970216f7f7SMichael Halcrow 	crypt_stat =
4980216f7f7SMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
49913a791b4STyler Hicks 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
5007fcba054SEric Sandeen 	enc_extent_page = alloc_page(GFP_USER);
5017fcba054SEric Sandeen 	if (!enc_extent_page) {
5020216f7f7SMichael Halcrow 		rc = -ENOMEM;
5030216f7f7SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error allocating memory for "
5040216f7f7SMichael Halcrow 				"encrypted extent\n");
5050216f7f7SMichael Halcrow 		goto out;
5060216f7f7SMichael Halcrow 	}
5070f896176STyler Hicks 
5080216f7f7SMichael Halcrow 	for (extent_offset = 0;
5090216f7f7SMichael Halcrow 	     extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
5100216f7f7SMichael Halcrow 	     extent_offset++) {
511*0df5ed65STyler Hicks 		rc = crypt_extent(crypt_stat, enc_extent_page, page,
512d78de618STyler Hicks 				  extent_offset, ENCRYPT);
5130216f7f7SMichael Halcrow 		if (rc) {
5140216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting extent; "
51518d1dbf1SHarvey Harrison 			       "rc = [%d]\n", __func__, rc);
5160216f7f7SMichael Halcrow 			goto out;
5170216f7f7SMichael Halcrow 		}
5180216f7f7SMichael Halcrow 	}
5190f896176STyler Hicks 
52024d15266STyler Hicks 	lower_offset = lower_offset_for_page(crypt_stat, page);
5210f896176STyler Hicks 	enc_extent_virt = kmap(enc_extent_page);
5220f896176STyler Hicks 	rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset,
5230f896176STyler Hicks 				  PAGE_CACHE_SIZE);
5240f896176STyler Hicks 	kunmap(enc_extent_page);
5250f896176STyler Hicks 	if (rc < 0) {
5260f896176STyler Hicks 		ecryptfs_printk(KERN_ERR,
5270f896176STyler Hicks 			"Error attempting to write lower page; rc = [%d]\n",
5280f896176STyler Hicks 			rc);
5290f896176STyler Hicks 		goto out;
530237fead6SMichael Halcrow 	}
53196a7b9c2STyler Hicks 	rc = 0;
5320216f7f7SMichael Halcrow out:
5337fcba054SEric Sandeen 	if (enc_extent_page) {
5347fcba054SEric Sandeen 		__free_page(enc_extent_page);
5357fcba054SEric Sandeen 	}
5360216f7f7SMichael Halcrow 	return rc;
5370216f7f7SMichael Halcrow }
5380216f7f7SMichael Halcrow 
539237fead6SMichael Halcrow /**
540237fead6SMichael Halcrow  * ecryptfs_decrypt_page
5410216f7f7SMichael Halcrow  * @page: Page mapped from the eCryptfs inode for the file; data read
5420216f7f7SMichael Halcrow  *        and decrypted from the lower file will be written into this
5430216f7f7SMichael Halcrow  *        page
544237fead6SMichael Halcrow  *
545237fead6SMichael Halcrow  * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
546237fead6SMichael Halcrow  * that eCryptfs pages may straddle the lower pages -- for instance,
547237fead6SMichael Halcrow  * if the file was created on a machine with an 8K page size
548237fead6SMichael Halcrow  * (resulting in an 8K header), and then the file is copied onto a
549237fead6SMichael Halcrow  * host with a 32K page size, then when reading page 0 of the eCryptfs
550237fead6SMichael Halcrow  * file, 24K of page 0 of the lower file will be read and decrypted,
551237fead6SMichael Halcrow  * and then 8K of page 1 of the lower file will be read and decrypted.
552237fead6SMichael Halcrow  *
553237fead6SMichael Halcrow  * Returns zero on success; negative on error
554237fead6SMichael Halcrow  */
5550216f7f7SMichael Halcrow int ecryptfs_decrypt_page(struct page *page)
556237fead6SMichael Halcrow {
5570216f7f7SMichael Halcrow 	struct inode *ecryptfs_inode;
558237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
5599c6043f4STyler Hicks 	char *page_virt;
5600216f7f7SMichael Halcrow 	unsigned long extent_offset;
5610f896176STyler Hicks 	loff_t lower_offset;
562237fead6SMichael Halcrow 	int rc = 0;
563237fead6SMichael Halcrow 
5640216f7f7SMichael Halcrow 	ecryptfs_inode = page->mapping->host;
5650216f7f7SMichael Halcrow 	crypt_stat =
5660216f7f7SMichael Halcrow 		&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
56713a791b4STyler Hicks 	BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
5680f896176STyler Hicks 
56924d15266STyler Hicks 	lower_offset = lower_offset_for_page(crypt_stat, page);
5709c6043f4STyler Hicks 	page_virt = kmap(page);
5719c6043f4STyler Hicks 	rc = ecryptfs_read_lower(page_virt, lower_offset, PAGE_CACHE_SIZE,
5720f896176STyler Hicks 				 ecryptfs_inode);
5739c6043f4STyler Hicks 	kunmap(page);
5740f896176STyler Hicks 	if (rc < 0) {
5750f896176STyler Hicks 		ecryptfs_printk(KERN_ERR,
5760f896176STyler Hicks 			"Error attempting to read lower page; rc = [%d]\n",
5770f896176STyler Hicks 			rc);
5780f896176STyler Hicks 		goto out;
5790f896176STyler Hicks 	}
5800f896176STyler Hicks 
5810216f7f7SMichael Halcrow 	for (extent_offset = 0;
5820216f7f7SMichael Halcrow 	     extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
5830216f7f7SMichael Halcrow 	     extent_offset++) {
584*0df5ed65STyler Hicks 		rc = crypt_extent(crypt_stat, page, page,
585d78de618STyler Hicks 				  extent_offset, DECRYPT);
5860216f7f7SMichael Halcrow 		if (rc) {
5870216f7f7SMichael Halcrow 			printk(KERN_ERR "%s: Error encrypting extent; "
58818d1dbf1SHarvey Harrison 			       "rc = [%d]\n", __func__, rc);
58916a72c45SMichael Halcrow 			goto out;
590237fead6SMichael Halcrow 		}
591237fead6SMichael Halcrow 	}
592237fead6SMichael Halcrow out:
593237fead6SMichael Halcrow 	return rc;
594237fead6SMichael Halcrow }
595237fead6SMichael Halcrow 
596237fead6SMichael Halcrow #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
597237fead6SMichael Halcrow 
598237fead6SMichael Halcrow /**
599237fead6SMichael Halcrow  * ecryptfs_init_crypt_ctx
600421f91d2SUwe Kleine-König  * @crypt_stat: Uninitialized crypt stats structure
601237fead6SMichael Halcrow  *
602237fead6SMichael Halcrow  * Initialize the crypto context.
603237fead6SMichael Halcrow  *
604237fead6SMichael Halcrow  * TODO: Performance: Keep a cache of initialized cipher contexts;
605237fead6SMichael Halcrow  * only init if needed
606237fead6SMichael Halcrow  */
607237fead6SMichael Halcrow int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
608237fead6SMichael Halcrow {
6098bba066fSMichael Halcrow 	char *full_alg_name;
610237fead6SMichael Halcrow 	int rc = -EINVAL;
611237fead6SMichael Halcrow 
612237fead6SMichael Halcrow 	if (!crypt_stat->cipher) {
613237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "No cipher specified\n");
614237fead6SMichael Halcrow 		goto out;
615237fead6SMichael Halcrow 	}
616237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG,
617237fead6SMichael Halcrow 			"Initializing cipher [%s]; strlen = [%d]; "
618f24b3887STyler Hicks 			"key_size_bits = [%zd]\n",
619237fead6SMichael Halcrow 			crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
620237fead6SMichael Halcrow 			crypt_stat->key_size << 3);
621237fead6SMichael Halcrow 	if (crypt_stat->tfm) {
622237fead6SMichael Halcrow 		rc = 0;
623237fead6SMichael Halcrow 		goto out;
624237fead6SMichael Halcrow 	}
625237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_tfm_mutex);
6268bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
6278bba066fSMichael Halcrow 						    crypt_stat->cipher, "cbc");
6288bba066fSMichael Halcrow 	if (rc)
629c8161f64SEric Sandeen 		goto out_unlock;
6304dfea4f0STyler Hicks 	crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0);
6318bba066fSMichael Halcrow 	kfree(full_alg_name);
632de88777eSAkinobu Mita 	if (IS_ERR(crypt_stat->tfm)) {
633de88777eSAkinobu Mita 		rc = PTR_ERR(crypt_stat->tfm);
634b0105eaeSTyler Hicks 		crypt_stat->tfm = NULL;
635237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
636237fead6SMichael Halcrow 				"Error initializing cipher [%s]\n",
637237fead6SMichael Halcrow 				crypt_stat->cipher);
638c8161f64SEric Sandeen 		goto out_unlock;
639237fead6SMichael Halcrow 	}
6404dfea4f0STyler Hicks 	crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
641237fead6SMichael Halcrow 	rc = 0;
642c8161f64SEric Sandeen out_unlock:
643c8161f64SEric Sandeen 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
644237fead6SMichael Halcrow out:
645237fead6SMichael Halcrow 	return rc;
646237fead6SMichael Halcrow }
647237fead6SMichael Halcrow 
648237fead6SMichael Halcrow static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
649237fead6SMichael Halcrow {
650237fead6SMichael Halcrow 	int extent_size_tmp;
651237fead6SMichael Halcrow 
652237fead6SMichael Halcrow 	crypt_stat->extent_mask = 0xFFFFFFFF;
653237fead6SMichael Halcrow 	crypt_stat->extent_shift = 0;
654237fead6SMichael Halcrow 	if (crypt_stat->extent_size == 0)
655237fead6SMichael Halcrow 		return;
656237fead6SMichael Halcrow 	extent_size_tmp = crypt_stat->extent_size;
657237fead6SMichael Halcrow 	while ((extent_size_tmp & 0x01) == 0) {
658237fead6SMichael Halcrow 		extent_size_tmp >>= 1;
659237fead6SMichael Halcrow 		crypt_stat->extent_mask <<= 1;
660237fead6SMichael Halcrow 		crypt_stat->extent_shift++;
661237fead6SMichael Halcrow 	}
662237fead6SMichael Halcrow }
663237fead6SMichael Halcrow 
664237fead6SMichael Halcrow void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
665237fead6SMichael Halcrow {
666237fead6SMichael Halcrow 	/* Default values; may be overwritten as we are parsing the
667237fead6SMichael Halcrow 	 * packets. */
668237fead6SMichael Halcrow 	crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
669237fead6SMichael Halcrow 	set_extent_mask_and_shift(crypt_stat);
670237fead6SMichael Halcrow 	crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
671dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
672fa3ef1cbSTyler Hicks 		crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
67345eaab79SMichael Halcrow 	else {
67445eaab79SMichael Halcrow 		if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
675fa3ef1cbSTyler Hicks 			crypt_stat->metadata_size =
676cc11beffSMichael Halcrow 				ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
677dd2a3b7aSMichael Halcrow 		else
678fa3ef1cbSTyler Hicks 			crypt_stat->metadata_size = PAGE_CACHE_SIZE;
67945eaab79SMichael Halcrow 	}
680237fead6SMichael Halcrow }
681237fead6SMichael Halcrow 
682237fead6SMichael Halcrow /**
683237fead6SMichael Halcrow  * ecryptfs_compute_root_iv
684237fead6SMichael Halcrow  * @crypt_stats
685237fead6SMichael Halcrow  *
686237fead6SMichael Halcrow  * On error, sets the root IV to all 0's.
687237fead6SMichael Halcrow  */
688237fead6SMichael Halcrow int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
689237fead6SMichael Halcrow {
690237fead6SMichael Halcrow 	int rc = 0;
691237fead6SMichael Halcrow 	char dst[MD5_DIGEST_SIZE];
692237fead6SMichael Halcrow 
693237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
694237fead6SMichael Halcrow 	BUG_ON(crypt_stat->iv_bytes <= 0);
695e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
696237fead6SMichael Halcrow 		rc = -EINVAL;
697237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Session key not valid; "
698237fead6SMichael Halcrow 				"cannot generate root IV\n");
699237fead6SMichael Halcrow 		goto out;
700237fead6SMichael Halcrow 	}
701237fead6SMichael Halcrow 	rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
702237fead6SMichael Halcrow 				    crypt_stat->key_size);
703237fead6SMichael Halcrow 	if (rc) {
704237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
705237fead6SMichael Halcrow 				"MD5 while generating root IV\n");
706237fead6SMichael Halcrow 		goto out;
707237fead6SMichael Halcrow 	}
708237fead6SMichael Halcrow 	memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
709237fead6SMichael Halcrow out:
710237fead6SMichael Halcrow 	if (rc) {
711237fead6SMichael Halcrow 		memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
712e2bd99ecSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
713237fead6SMichael Halcrow 	}
714237fead6SMichael Halcrow 	return rc;
715237fead6SMichael Halcrow }
716237fead6SMichael Halcrow 
717237fead6SMichael Halcrow static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
718237fead6SMichael Halcrow {
719237fead6SMichael Halcrow 	get_random_bytes(crypt_stat->key, crypt_stat->key_size);
720e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
721237fead6SMichael Halcrow 	ecryptfs_compute_root_iv(crypt_stat);
722237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
723237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
724237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
725237fead6SMichael Halcrow 				  crypt_stat->key_size);
726237fead6SMichael Halcrow 	}
727237fead6SMichael Halcrow }
728237fead6SMichael Halcrow 
729237fead6SMichael Halcrow /**
73017398957SMichael Halcrow  * ecryptfs_copy_mount_wide_flags_to_inode_flags
73122e78fafSMichael Halcrow  * @crypt_stat: The inode's cryptographic context
73222e78fafSMichael Halcrow  * @mount_crypt_stat: The mount point's cryptographic context
73317398957SMichael Halcrow  *
73417398957SMichael Halcrow  * This function propagates the mount-wide flags to individual inode
73517398957SMichael Halcrow  * flags.
73617398957SMichael Halcrow  */
73717398957SMichael Halcrow static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
73817398957SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
73917398957SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
74017398957SMichael Halcrow {
74117398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
74217398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
74317398957SMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
74417398957SMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
745addd65adSMichael Halcrow 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
746addd65adSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
747addd65adSMichael Halcrow 		if (mount_crypt_stat->flags
748addd65adSMichael Halcrow 		    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
749addd65adSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
750addd65adSMichael Halcrow 		else if (mount_crypt_stat->flags
751addd65adSMichael Halcrow 			 & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
752addd65adSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
753addd65adSMichael Halcrow 	}
75417398957SMichael Halcrow }
75517398957SMichael Halcrow 
756f4aad16aSMichael Halcrow static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
757f4aad16aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
758f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
759f4aad16aSMichael Halcrow {
760f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
761f4aad16aSMichael Halcrow 	int rc = 0;
762f4aad16aSMichael Halcrow 
763aa06117fSRoland Dreier 	mutex_lock(&crypt_stat->keysig_list_mutex);
764f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
765aa06117fSRoland Dreier 
766f4aad16aSMichael Halcrow 	list_for_each_entry(global_auth_tok,
767f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
768f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
76984814d64STyler Hicks 		if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
77084814d64STyler Hicks 			continue;
771f4aad16aSMichael Halcrow 		rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
772f4aad16aSMichael Halcrow 		if (rc) {
773f4aad16aSMichael Halcrow 			printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
774f4aad16aSMichael Halcrow 			goto out;
775f4aad16aSMichael Halcrow 		}
776f4aad16aSMichael Halcrow 	}
777aa06117fSRoland Dreier 
778f4aad16aSMichael Halcrow out:
779aa06117fSRoland Dreier 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
780aa06117fSRoland Dreier 	mutex_unlock(&crypt_stat->keysig_list_mutex);
781f4aad16aSMichael Halcrow 	return rc;
782f4aad16aSMichael Halcrow }
783f4aad16aSMichael Halcrow 
78417398957SMichael Halcrow /**
785237fead6SMichael Halcrow  * ecryptfs_set_default_crypt_stat_vals
78622e78fafSMichael Halcrow  * @crypt_stat: The inode's cryptographic context
78722e78fafSMichael Halcrow  * @mount_crypt_stat: The mount point's cryptographic context
788237fead6SMichael Halcrow  *
789237fead6SMichael Halcrow  * Default values in the event that policy does not override them.
790237fead6SMichael Halcrow  */
791237fead6SMichael Halcrow static void ecryptfs_set_default_crypt_stat_vals(
792237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
793237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
794237fead6SMichael Halcrow {
79517398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
79617398957SMichael Halcrow 						      mount_crypt_stat);
797237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
798237fead6SMichael Halcrow 	strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
799237fead6SMichael Halcrow 	crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
800e2bd99ecSMichael Halcrow 	crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
801237fead6SMichael Halcrow 	crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
802237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = mount_crypt_stat;
803237fead6SMichael Halcrow }
804237fead6SMichael Halcrow 
805237fead6SMichael Halcrow /**
806237fead6SMichael Halcrow  * ecryptfs_new_file_context
807b59db43aSTyler Hicks  * @ecryptfs_inode: The eCryptfs inode
808237fead6SMichael Halcrow  *
809237fead6SMichael Halcrow  * If the crypto context for the file has not yet been established,
810237fead6SMichael Halcrow  * this is where we do that.  Establishing a new crypto context
811237fead6SMichael Halcrow  * involves the following decisions:
812237fead6SMichael Halcrow  *  - What cipher to use?
813237fead6SMichael Halcrow  *  - What set of authentication tokens to use?
814237fead6SMichael Halcrow  * Here we just worry about getting enough information into the
815237fead6SMichael Halcrow  * authentication tokens so that we know that they are available.
816237fead6SMichael Halcrow  * We associate the available authentication tokens with the new file
817237fead6SMichael Halcrow  * via the set of signatures in the crypt_stat struct.  Later, when
818237fead6SMichael Halcrow  * the headers are actually written out, we may again defer to
819237fead6SMichael Halcrow  * userspace to perform the encryption of the session key; for the
820237fead6SMichael Halcrow  * foreseeable future, this will be the case with public key packets.
821237fead6SMichael Halcrow  *
822237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
823237fead6SMichael Halcrow  */
824b59db43aSTyler Hicks int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
825237fead6SMichael Halcrow {
826237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
827b59db43aSTyler Hicks 	    &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
828237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
829237fead6SMichael Halcrow 	    &ecryptfs_superblock_to_private(
830b59db43aSTyler Hicks 		    ecryptfs_inode->i_sb)->mount_crypt_stat;
831237fead6SMichael Halcrow 	int cipher_name_len;
832f4aad16aSMichael Halcrow 	int rc = 0;
833237fead6SMichael Halcrow 
834237fead6SMichael Halcrow 	ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
835af655dc6SMichael Halcrow 	crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
83617398957SMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
83717398957SMichael Halcrow 						      mount_crypt_stat);
838f4aad16aSMichael Halcrow 	rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
839f4aad16aSMichael Halcrow 							 mount_crypt_stat);
840f4aad16aSMichael Halcrow 	if (rc) {
841f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
842f4aad16aSMichael Halcrow 		       "to the inode key sigs; rc = [%d]\n", rc);
843f4aad16aSMichael Halcrow 		goto out;
844f4aad16aSMichael Halcrow 	}
845237fead6SMichael Halcrow 	cipher_name_len =
846237fead6SMichael Halcrow 		strlen(mount_crypt_stat->global_default_cipher_name);
847237fead6SMichael Halcrow 	memcpy(crypt_stat->cipher,
848237fead6SMichael Halcrow 	       mount_crypt_stat->global_default_cipher_name,
849237fead6SMichael Halcrow 	       cipher_name_len);
850237fead6SMichael Halcrow 	crypt_stat->cipher[cipher_name_len] = '\0';
851237fead6SMichael Halcrow 	crypt_stat->key_size =
852237fead6SMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size;
853237fead6SMichael Halcrow 	ecryptfs_generate_new_key(crypt_stat);
854237fead6SMichael Halcrow 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
855237fead6SMichael Halcrow 	if (rc)
856237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
857237fead6SMichael Halcrow 				"context for cipher [%s]: rc = [%d]\n",
858237fead6SMichael Halcrow 				crypt_stat->cipher, rc);
859f4aad16aSMichael Halcrow out:
860237fead6SMichael Halcrow 	return rc;
861237fead6SMichael Halcrow }
862237fead6SMichael Halcrow 
863237fead6SMichael Halcrow /**
8647a86617eSTyler Hicks  * ecryptfs_validate_marker - check for the ecryptfs marker
865237fead6SMichael Halcrow  * @data: The data block in which to check
866237fead6SMichael Halcrow  *
8677a86617eSTyler Hicks  * Returns zero if marker found; -EINVAL if not found
868237fead6SMichael Halcrow  */
8697a86617eSTyler Hicks static int ecryptfs_validate_marker(char *data)
870237fead6SMichael Halcrow {
871237fead6SMichael Halcrow 	u32 m_1, m_2;
872237fead6SMichael Halcrow 
87329335c6aSHarvey Harrison 	m_1 = get_unaligned_be32(data);
87429335c6aSHarvey Harrison 	m_2 = get_unaligned_be32(data + 4);
875237fead6SMichael Halcrow 	if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
8767a86617eSTyler Hicks 		return 0;
877237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
878237fead6SMichael Halcrow 			"MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
879237fead6SMichael Halcrow 			MAGIC_ECRYPTFS_MARKER);
880237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
881237fead6SMichael Halcrow 			"[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
8827a86617eSTyler Hicks 	return -EINVAL;
883237fead6SMichael Halcrow }
884237fead6SMichael Halcrow 
885237fead6SMichael Halcrow struct ecryptfs_flag_map_elem {
886237fead6SMichael Halcrow 	u32 file_flag;
887237fead6SMichael Halcrow 	u32 local_flag;
888237fead6SMichael Halcrow };
889237fead6SMichael Halcrow 
890237fead6SMichael Halcrow /* Add support for additional flags by adding elements here. */
891237fead6SMichael Halcrow static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
892237fead6SMichael Halcrow 	{0x00000001, ECRYPTFS_ENABLE_HMAC},
893dd2a3b7aSMichael Halcrow 	{0x00000002, ECRYPTFS_ENCRYPTED},
894addd65adSMichael Halcrow 	{0x00000004, ECRYPTFS_METADATA_IN_XATTR},
895addd65adSMichael Halcrow 	{0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
896237fead6SMichael Halcrow };
897237fead6SMichael Halcrow 
898237fead6SMichael Halcrow /**
899237fead6SMichael Halcrow  * ecryptfs_process_flags
90022e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
901237fead6SMichael Halcrow  * @page_virt: Source data to be parsed
902237fead6SMichael Halcrow  * @bytes_read: Updated with the number of bytes read
903237fead6SMichael Halcrow  *
904237fead6SMichael Halcrow  * Returns zero on success; non-zero if the flag set is invalid
905237fead6SMichael Halcrow  */
906237fead6SMichael Halcrow static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
907237fead6SMichael Halcrow 				  char *page_virt, int *bytes_read)
908237fead6SMichael Halcrow {
909237fead6SMichael Halcrow 	int rc = 0;
910237fead6SMichael Halcrow 	int i;
911237fead6SMichael Halcrow 	u32 flags;
912237fead6SMichael Halcrow 
91329335c6aSHarvey Harrison 	flags = get_unaligned_be32(page_virt);
914237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
915237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
916237fead6SMichael Halcrow 		if (flags & ecryptfs_flag_map[i].file_flag) {
917e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
918237fead6SMichael Halcrow 		} else
919e2bd99ecSMichael Halcrow 			crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
920237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
921237fead6SMichael Halcrow 	crypt_stat->file_version = ((flags >> 24) & 0xFF);
922237fead6SMichael Halcrow 	(*bytes_read) = 4;
923237fead6SMichael Halcrow 	return rc;
924237fead6SMichael Halcrow }
925237fead6SMichael Halcrow 
926237fead6SMichael Halcrow /**
927237fead6SMichael Halcrow  * write_ecryptfs_marker
928237fead6SMichael Halcrow  * @page_virt: The pointer to in a page to begin writing the marker
929237fead6SMichael Halcrow  * @written: Number of bytes written
930237fead6SMichael Halcrow  *
931237fead6SMichael Halcrow  * Marker = 0x3c81b7f5
932237fead6SMichael Halcrow  */
933237fead6SMichael Halcrow static void write_ecryptfs_marker(char *page_virt, size_t *written)
934237fead6SMichael Halcrow {
935237fead6SMichael Halcrow 	u32 m_1, m_2;
936237fead6SMichael Halcrow 
937237fead6SMichael Halcrow 	get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
938237fead6SMichael Halcrow 	m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
93929335c6aSHarvey Harrison 	put_unaligned_be32(m_1, page_virt);
94029335c6aSHarvey Harrison 	page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
94129335c6aSHarvey Harrison 	put_unaligned_be32(m_2, page_virt);
942237fead6SMichael Halcrow 	(*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
943237fead6SMichael Halcrow }
944237fead6SMichael Halcrow 
945f4e60e6bSTyler Hicks void ecryptfs_write_crypt_stat_flags(char *page_virt,
946f4e60e6bSTyler Hicks 				     struct ecryptfs_crypt_stat *crypt_stat,
947237fead6SMichael Halcrow 				     size_t *written)
948237fead6SMichael Halcrow {
949237fead6SMichael Halcrow 	u32 flags = 0;
950237fead6SMichael Halcrow 	int i;
951237fead6SMichael Halcrow 
952237fead6SMichael Halcrow 	for (i = 0; i < ((sizeof(ecryptfs_flag_map)
953237fead6SMichael Halcrow 			  / sizeof(struct ecryptfs_flag_map_elem))); i++)
954e2bd99ecSMichael Halcrow 		if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
955237fead6SMichael Halcrow 			flags |= ecryptfs_flag_map[i].file_flag;
956237fead6SMichael Halcrow 	/* Version is in top 8 bits of the 32-bit flag vector */
957237fead6SMichael Halcrow 	flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
95829335c6aSHarvey Harrison 	put_unaligned_be32(flags, page_virt);
959237fead6SMichael Halcrow 	(*written) = 4;
960237fead6SMichael Halcrow }
961237fead6SMichael Halcrow 
962237fead6SMichael Halcrow struct ecryptfs_cipher_code_str_map_elem {
963237fead6SMichael Halcrow 	char cipher_str[16];
96419e66a67STrevor Highland 	u8 cipher_code;
965237fead6SMichael Halcrow };
966237fead6SMichael Halcrow 
967237fead6SMichael Halcrow /* Add support for additional ciphers by adding elements here. The
968237fead6SMichael Halcrow  * cipher_code is whatever OpenPGP applicatoins use to identify the
969237fead6SMichael Halcrow  * ciphers. List in order of probability. */
970237fead6SMichael Halcrow static struct ecryptfs_cipher_code_str_map_elem
971237fead6SMichael Halcrow ecryptfs_cipher_code_str_map[] = {
972237fead6SMichael Halcrow 	{"aes",RFC2440_CIPHER_AES_128 },
973237fead6SMichael Halcrow 	{"blowfish", RFC2440_CIPHER_BLOWFISH},
974237fead6SMichael Halcrow 	{"des3_ede", RFC2440_CIPHER_DES3_EDE},
975237fead6SMichael Halcrow 	{"cast5", RFC2440_CIPHER_CAST_5},
976237fead6SMichael Halcrow 	{"twofish", RFC2440_CIPHER_TWOFISH},
977237fead6SMichael Halcrow 	{"cast6", RFC2440_CIPHER_CAST_6},
978237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_192},
979237fead6SMichael Halcrow 	{"aes", RFC2440_CIPHER_AES_256}
980237fead6SMichael Halcrow };
981237fead6SMichael Halcrow 
982237fead6SMichael Halcrow /**
983237fead6SMichael Halcrow  * ecryptfs_code_for_cipher_string
9849c79f34fSMichael Halcrow  * @cipher_name: The string alias for the cipher
9859c79f34fSMichael Halcrow  * @key_bytes: Length of key in bytes; used for AES code selection
986237fead6SMichael Halcrow  *
987237fead6SMichael Halcrow  * Returns zero on no match, or the cipher code on match
988237fead6SMichael Halcrow  */
9899c79f34fSMichael Halcrow u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
990237fead6SMichael Halcrow {
991237fead6SMichael Halcrow 	int i;
99219e66a67STrevor Highland 	u8 code = 0;
993237fead6SMichael Halcrow 	struct ecryptfs_cipher_code_str_map_elem *map =
994237fead6SMichael Halcrow 		ecryptfs_cipher_code_str_map;
995237fead6SMichael Halcrow 
9969c79f34fSMichael Halcrow 	if (strcmp(cipher_name, "aes") == 0) {
9979c79f34fSMichael Halcrow 		switch (key_bytes) {
998237fead6SMichael Halcrow 		case 16:
999237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_128;
1000237fead6SMichael Halcrow 			break;
1001237fead6SMichael Halcrow 		case 24:
1002237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_192;
1003237fead6SMichael Halcrow 			break;
1004237fead6SMichael Halcrow 		case 32:
1005237fead6SMichael Halcrow 			code = RFC2440_CIPHER_AES_256;
1006237fead6SMichael Halcrow 		}
1007237fead6SMichael Halcrow 	} else {
1008237fead6SMichael Halcrow 		for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
10099c79f34fSMichael Halcrow 			if (strcmp(cipher_name, map[i].cipher_str) == 0) {
1010237fead6SMichael Halcrow 				code = map[i].cipher_code;
1011237fead6SMichael Halcrow 				break;
1012237fead6SMichael Halcrow 			}
1013237fead6SMichael Halcrow 	}
1014237fead6SMichael Halcrow 	return code;
1015237fead6SMichael Halcrow }
1016237fead6SMichael Halcrow 
1017237fead6SMichael Halcrow /**
1018237fead6SMichael Halcrow  * ecryptfs_cipher_code_to_string
1019237fead6SMichael Halcrow  * @str: Destination to write out the cipher name
1020237fead6SMichael Halcrow  * @cipher_code: The code to convert to cipher name string
1021237fead6SMichael Halcrow  *
1022237fead6SMichael Halcrow  * Returns zero on success
1023237fead6SMichael Halcrow  */
102419e66a67STrevor Highland int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
1025237fead6SMichael Halcrow {
1026237fead6SMichael Halcrow 	int rc = 0;
1027237fead6SMichael Halcrow 	int i;
1028237fead6SMichael Halcrow 
1029237fead6SMichael Halcrow 	str[0] = '\0';
1030237fead6SMichael Halcrow 	for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1031237fead6SMichael Halcrow 		if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1032237fead6SMichael Halcrow 			strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1033237fead6SMichael Halcrow 	if (str[0] == '\0') {
1034237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1035237fead6SMichael Halcrow 				"[%d]\n", cipher_code);
1036237fead6SMichael Halcrow 		rc = -EINVAL;
1037237fead6SMichael Halcrow 	}
1038237fead6SMichael Halcrow 	return rc;
1039237fead6SMichael Halcrow }
1040237fead6SMichael Halcrow 
1041778aeb42STyler Hicks int ecryptfs_read_and_validate_header_region(struct inode *inode)
1042dd2a3b7aSMichael Halcrow {
1043778aeb42STyler Hicks 	u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1044778aeb42STyler Hicks 	u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1045dd2a3b7aSMichael Halcrow 	int rc;
1046dd2a3b7aSMichael Halcrow 
1047778aeb42STyler Hicks 	rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
1048778aeb42STyler Hicks 				 inode);
1049778aeb42STyler Hicks 	if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1050778aeb42STyler Hicks 		return rc >= 0 ? -EINVAL : rc;
1051778aeb42STyler Hicks 	rc = ecryptfs_validate_marker(marker);
1052778aeb42STyler Hicks 	if (!rc)
1053778aeb42STyler Hicks 		ecryptfs_i_size_init(file_size, inode);
1054dd2a3b7aSMichael Halcrow 	return rc;
1055dd2a3b7aSMichael Halcrow }
1056dd2a3b7aSMichael Halcrow 
1057e77a56ddSMichael Halcrow void
1058e77a56ddSMichael Halcrow ecryptfs_write_header_metadata(char *virt,
1059e77a56ddSMichael Halcrow 			       struct ecryptfs_crypt_stat *crypt_stat,
1060237fead6SMichael Halcrow 			       size_t *written)
1061237fead6SMichael Halcrow {
1062237fead6SMichael Halcrow 	u32 header_extent_size;
1063237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1064237fead6SMichael Halcrow 
106545eaab79SMichael Halcrow 	header_extent_size = (u32)crypt_stat->extent_size;
1066237fead6SMichael Halcrow 	num_header_extents_at_front =
1067fa3ef1cbSTyler Hicks 		(u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
106829335c6aSHarvey Harrison 	put_unaligned_be32(header_extent_size, virt);
1069237fead6SMichael Halcrow 	virt += 4;
107029335c6aSHarvey Harrison 	put_unaligned_be16(num_header_extents_at_front, virt);
1071237fead6SMichael Halcrow 	(*written) = 6;
1072237fead6SMichael Halcrow }
1073237fead6SMichael Halcrow 
107430632870STyler Hicks struct kmem_cache *ecryptfs_header_cache;
1075237fead6SMichael Halcrow 
1076237fead6SMichael Halcrow /**
1077237fead6SMichael Halcrow  * ecryptfs_write_headers_virt
107822e78fafSMichael Halcrow  * @page_virt: The virtual address to write the headers to
107987b811c3SEric Sandeen  * @max: The size of memory allocated at page_virt
108022e78fafSMichael Halcrow  * @size: Set to the number of bytes written by this function
108122e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
108222e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
1083237fead6SMichael Halcrow  *
1084237fead6SMichael Halcrow  * Format version: 1
1085237fead6SMichael Halcrow  *
1086237fead6SMichael Halcrow  *   Header Extent:
1087237fead6SMichael Halcrow  *     Octets 0-7:        Unencrypted file size (big-endian)
1088237fead6SMichael Halcrow  *     Octets 8-15:       eCryptfs special marker
1089237fead6SMichael Halcrow  *     Octets 16-19:      Flags
1090237fead6SMichael Halcrow  *      Octet 16:         File format version number (between 0 and 255)
1091237fead6SMichael Halcrow  *      Octets 17-18:     Reserved
1092237fead6SMichael Halcrow  *      Octet 19:         Bit 1 (lsb): Reserved
1093237fead6SMichael Halcrow  *                        Bit 2: Encrypted?
1094237fead6SMichael Halcrow  *                        Bits 3-8: Reserved
1095237fead6SMichael Halcrow  *     Octets 20-23:      Header extent size (big-endian)
1096237fead6SMichael Halcrow  *     Octets 24-25:      Number of header extents at front of file
1097237fead6SMichael Halcrow  *                        (big-endian)
1098237fead6SMichael Halcrow  *     Octet  26:         Begin RFC 2440 authentication token packet set
1099237fead6SMichael Halcrow  *   Data Extent 0:
1100237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1101237fead6SMichael Halcrow  *   Data Extent 1:
1102237fead6SMichael Halcrow  *     Lower data (CBC encrypted)
1103237fead6SMichael Halcrow  *   ...
1104237fead6SMichael Halcrow  *
1105237fead6SMichael Halcrow  * Returns zero on success
1106237fead6SMichael Halcrow  */
110787b811c3SEric Sandeen static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
110887b811c3SEric Sandeen 				       size_t *size,
1109237fead6SMichael Halcrow 				       struct ecryptfs_crypt_stat *crypt_stat,
1110237fead6SMichael Halcrow 				       struct dentry *ecryptfs_dentry)
1111237fead6SMichael Halcrow {
1112237fead6SMichael Halcrow 	int rc;
1113237fead6SMichael Halcrow 	size_t written;
1114237fead6SMichael Halcrow 	size_t offset;
1115237fead6SMichael Halcrow 
1116237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
1117237fead6SMichael Halcrow 	write_ecryptfs_marker((page_virt + offset), &written);
1118237fead6SMichael Halcrow 	offset += written;
1119f4e60e6bSTyler Hicks 	ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
1120f4e60e6bSTyler Hicks 					&written);
1121237fead6SMichael Halcrow 	offset += written;
1122e77a56ddSMichael Halcrow 	ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1123e77a56ddSMichael Halcrow 				       &written);
1124237fead6SMichael Halcrow 	offset += written;
1125237fead6SMichael Halcrow 	rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1126237fead6SMichael Halcrow 					      ecryptfs_dentry, &written,
112787b811c3SEric Sandeen 					      max - offset);
1128237fead6SMichael Halcrow 	if (rc)
1129237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1130237fead6SMichael Halcrow 				"set; rc = [%d]\n", rc);
1131dd2a3b7aSMichael Halcrow 	if (size) {
1132dd2a3b7aSMichael Halcrow 		offset += written;
1133dd2a3b7aSMichael Halcrow 		*size = offset;
1134dd2a3b7aSMichael Halcrow 	}
1135dd2a3b7aSMichael Halcrow 	return rc;
1136dd2a3b7aSMichael Halcrow }
1137dd2a3b7aSMichael Halcrow 
113822e78fafSMichael Halcrow static int
1139b59db43aSTyler Hicks ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
11408faece5fSTyler Hicks 				    char *virt, size_t virt_len)
1141dd2a3b7aSMichael Halcrow {
1142d7cdc5feSMichael Halcrow 	int rc;
1143dd2a3b7aSMichael Halcrow 
1144b59db43aSTyler Hicks 	rc = ecryptfs_write_lower(ecryptfs_inode, virt,
11458faece5fSTyler Hicks 				  0, virt_len);
114696a7b9c2STyler Hicks 	if (rc < 0)
1147d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to write header "
114896a7b9c2STyler Hicks 		       "information to lower file; rc = [%d]\n", __func__, rc);
114996a7b9c2STyler Hicks 	else
115096a7b9c2STyler Hicks 		rc = 0;
115170456600SMichael Halcrow 	return rc;
1152dd2a3b7aSMichael Halcrow }
1153dd2a3b7aSMichael Halcrow 
115422e78fafSMichael Halcrow static int
115522e78fafSMichael Halcrow ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1156dd2a3b7aSMichael Halcrow 				 char *page_virt, size_t size)
1157dd2a3b7aSMichael Halcrow {
1158dd2a3b7aSMichael Halcrow 	int rc;
1159dd2a3b7aSMichael Halcrow 
1160dd2a3b7aSMichael Halcrow 	rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1161dd2a3b7aSMichael Halcrow 			       size, 0);
1162237fead6SMichael Halcrow 	return rc;
1163237fead6SMichael Halcrow }
1164237fead6SMichael Halcrow 
11658faece5fSTyler Hicks static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
11668faece5fSTyler Hicks 					       unsigned int order)
11678faece5fSTyler Hicks {
11688faece5fSTyler Hicks 	struct page *page;
11698faece5fSTyler Hicks 
11708faece5fSTyler Hicks 	page = alloc_pages(gfp_mask | __GFP_ZERO, order);
11718faece5fSTyler Hicks 	if (page)
11728faece5fSTyler Hicks 		return (unsigned long) page_address(page);
11738faece5fSTyler Hicks 	return 0;
11748faece5fSTyler Hicks }
11758faece5fSTyler Hicks 
1176237fead6SMichael Halcrow /**
1177dd2a3b7aSMichael Halcrow  * ecryptfs_write_metadata
1178b59db43aSTyler Hicks  * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
1179b59db43aSTyler Hicks  * @ecryptfs_inode: The newly created eCryptfs inode
1180237fead6SMichael Halcrow  *
1181237fead6SMichael Halcrow  * Write the file headers out.  This will likely involve a userspace
1182237fead6SMichael Halcrow  * callout, in which the session key is encrypted with one or more
1183237fead6SMichael Halcrow  * public keys and/or the passphrase necessary to do the encryption is
1184237fead6SMichael Halcrow  * retrieved via a prompt.  Exactly what happens at this point should
1185237fead6SMichael Halcrow  * be policy-dependent.
1186237fead6SMichael Halcrow  *
1187237fead6SMichael Halcrow  * Returns zero on success; non-zero on error
1188237fead6SMichael Halcrow  */
1189b59db43aSTyler Hicks int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1190b59db43aSTyler Hicks 			    struct inode *ecryptfs_inode)
1191237fead6SMichael Halcrow {
1192d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1193b59db43aSTyler Hicks 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
11948faece5fSTyler Hicks 	unsigned int order;
1195cc11beffSMichael Halcrow 	char *virt;
11968faece5fSTyler Hicks 	size_t virt_len;
1197d7cdc5feSMichael Halcrow 	size_t size = 0;
1198237fead6SMichael Halcrow 	int rc = 0;
1199237fead6SMichael Halcrow 
1200e2bd99ecSMichael Halcrow 	if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1201e2bd99ecSMichael Halcrow 		if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
1202d7cdc5feSMichael Halcrow 			printk(KERN_ERR "Key is invalid; bailing out\n");
1203237fead6SMichael Halcrow 			rc = -EINVAL;
1204237fead6SMichael Halcrow 			goto out;
1205237fead6SMichael Halcrow 		}
1206237fead6SMichael Halcrow 	} else {
1207cc11beffSMichael Halcrow 		printk(KERN_WARNING "%s: Encrypted flag not set\n",
120818d1dbf1SHarvey Harrison 		       __func__);
1209237fead6SMichael Halcrow 		rc = -EINVAL;
1210237fead6SMichael Halcrow 		goto out;
1211237fead6SMichael Halcrow 	}
1212fa3ef1cbSTyler Hicks 	virt_len = crypt_stat->metadata_size;
12138faece5fSTyler Hicks 	order = get_order(virt_len);
1214237fead6SMichael Halcrow 	/* Released in this function */
12158faece5fSTyler Hicks 	virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
1216cc11beffSMichael Halcrow 	if (!virt) {
121718d1dbf1SHarvey Harrison 		printk(KERN_ERR "%s: Out of memory\n", __func__);
1218237fead6SMichael Halcrow 		rc = -ENOMEM;
1219237fead6SMichael Halcrow 		goto out;
1220237fead6SMichael Halcrow 	}
1221bd4f0fe8STyler Hicks 	/* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
12228faece5fSTyler Hicks 	rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
12238faece5fSTyler Hicks 					 ecryptfs_dentry);
1224237fead6SMichael Halcrow 	if (unlikely(rc)) {
1225cc11beffSMichael Halcrow 		printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
122618d1dbf1SHarvey Harrison 		       __func__, rc);
1227237fead6SMichael Halcrow 		goto out_free;
1228237fead6SMichael Halcrow 	}
1229dd2a3b7aSMichael Halcrow 	if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
12308faece5fSTyler Hicks 		rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
12318faece5fSTyler Hicks 						      size);
1232dd2a3b7aSMichael Halcrow 	else
1233b59db43aSTyler Hicks 		rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
12348faece5fSTyler Hicks 							 virt_len);
1235dd2a3b7aSMichael Halcrow 	if (rc) {
1236cc11beffSMichael Halcrow 		printk(KERN_ERR "%s: Error writing metadata out to lower file; "
123718d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, rc);
1238dd2a3b7aSMichael Halcrow 		goto out_free;
1239237fead6SMichael Halcrow 	}
1240237fead6SMichael Halcrow out_free:
12418faece5fSTyler Hicks 	free_pages((unsigned long)virt, order);
1242237fead6SMichael Halcrow out:
1243237fead6SMichael Halcrow 	return rc;
1244237fead6SMichael Halcrow }
1245237fead6SMichael Halcrow 
1246dd2a3b7aSMichael Halcrow #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1247dd2a3b7aSMichael Halcrow #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
1248237fead6SMichael Halcrow static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
1249dd2a3b7aSMichael Halcrow 				 char *virt, int *bytes_read,
1250dd2a3b7aSMichael Halcrow 				 int validate_header_size)
1251237fead6SMichael Halcrow {
1252237fead6SMichael Halcrow 	int rc = 0;
1253237fead6SMichael Halcrow 	u32 header_extent_size;
1254237fead6SMichael Halcrow 	u16 num_header_extents_at_front;
1255237fead6SMichael Halcrow 
125629335c6aSHarvey Harrison 	header_extent_size = get_unaligned_be32(virt);
125729335c6aSHarvey Harrison 	virt += sizeof(__be32);
125829335c6aSHarvey Harrison 	num_header_extents_at_front = get_unaligned_be16(virt);
1259fa3ef1cbSTyler Hicks 	crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
1260cc11beffSMichael Halcrow 				     * (size_t)header_extent_size));
126129335c6aSHarvey Harrison 	(*bytes_read) = (sizeof(__be32) + sizeof(__be16));
1262dd2a3b7aSMichael Halcrow 	if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
1263fa3ef1cbSTyler Hicks 	    && (crypt_stat->metadata_size
1264dd2a3b7aSMichael Halcrow 		< ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
1265237fead6SMichael Halcrow 		rc = -EINVAL;
1266cc11beffSMichael Halcrow 		printk(KERN_WARNING "Invalid header size: [%zd]\n",
1267fa3ef1cbSTyler Hicks 		       crypt_stat->metadata_size);
1268237fead6SMichael Halcrow 	}
1269237fead6SMichael Halcrow 	return rc;
1270237fead6SMichael Halcrow }
1271237fead6SMichael Halcrow 
1272237fead6SMichael Halcrow /**
1273237fead6SMichael Halcrow  * set_default_header_data
127422e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1275237fead6SMichael Halcrow  *
1276237fead6SMichael Halcrow  * For version 0 file format; this function is only for backwards
1277237fead6SMichael Halcrow  * compatibility for files created with the prior versions of
1278237fead6SMichael Halcrow  * eCryptfs.
1279237fead6SMichael Halcrow  */
1280237fead6SMichael Halcrow static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1281237fead6SMichael Halcrow {
1282fa3ef1cbSTyler Hicks 	crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
1283237fead6SMichael Halcrow }
1284237fead6SMichael Halcrow 
12853aeb86eaSTyler Hicks void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
12863aeb86eaSTyler Hicks {
12873aeb86eaSTyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
12883aeb86eaSTyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
12893aeb86eaSTyler Hicks 	u64 file_size;
12903aeb86eaSTyler Hicks 
12913aeb86eaSTyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
12923aeb86eaSTyler Hicks 	mount_crypt_stat =
12933aeb86eaSTyler Hicks 		&ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
12943aeb86eaSTyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
12953aeb86eaSTyler Hicks 		file_size = i_size_read(ecryptfs_inode_to_lower(inode));
12963aeb86eaSTyler Hicks 		if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
12973aeb86eaSTyler Hicks 			file_size += crypt_stat->metadata_size;
12983aeb86eaSTyler Hicks 	} else
12993aeb86eaSTyler Hicks 		file_size = get_unaligned_be64(page_virt);
13003aeb86eaSTyler Hicks 	i_size_write(inode, (loff_t)file_size);
13013aeb86eaSTyler Hicks 	crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
13023aeb86eaSTyler Hicks }
13033aeb86eaSTyler Hicks 
1304237fead6SMichael Halcrow /**
1305237fead6SMichael Halcrow  * ecryptfs_read_headers_virt
130622e78fafSMichael Halcrow  * @page_virt: The virtual address into which to read the headers
130722e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
130822e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry
130922e78fafSMichael Halcrow  * @validate_header_size: Whether to validate the header size while reading
1310237fead6SMichael Halcrow  *
1311237fead6SMichael Halcrow  * Read/parse the header data. The header format is detailed in the
1312237fead6SMichael Halcrow  * comment block for the ecryptfs_write_headers_virt() function.
1313237fead6SMichael Halcrow  *
1314237fead6SMichael Halcrow  * Returns zero on success
1315237fead6SMichael Halcrow  */
1316237fead6SMichael Halcrow static int ecryptfs_read_headers_virt(char *page_virt,
1317237fead6SMichael Halcrow 				      struct ecryptfs_crypt_stat *crypt_stat,
1318dd2a3b7aSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
1319dd2a3b7aSMichael Halcrow 				      int validate_header_size)
1320237fead6SMichael Halcrow {
1321237fead6SMichael Halcrow 	int rc = 0;
1322237fead6SMichael Halcrow 	int offset;
1323237fead6SMichael Halcrow 	int bytes_read;
1324237fead6SMichael Halcrow 
1325237fead6SMichael Halcrow 	ecryptfs_set_default_sizes(crypt_stat);
1326237fead6SMichael Halcrow 	crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1327237fead6SMichael Halcrow 		ecryptfs_dentry->d_sb)->mount_crypt_stat;
1328237fead6SMichael Halcrow 	offset = ECRYPTFS_FILE_SIZE_BYTES;
13297a86617eSTyler Hicks 	rc = ecryptfs_validate_marker(page_virt + offset);
13307a86617eSTyler Hicks 	if (rc)
1331237fead6SMichael Halcrow 		goto out;
13323aeb86eaSTyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
13333aeb86eaSTyler Hicks 		ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode);
1334237fead6SMichael Halcrow 	offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1335237fead6SMichael Halcrow 	rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1336237fead6SMichael Halcrow 				    &bytes_read);
1337237fead6SMichael Halcrow 	if (rc) {
1338237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1339237fead6SMichael Halcrow 		goto out;
1340237fead6SMichael Halcrow 	}
1341237fead6SMichael Halcrow 	if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1342237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1343237fead6SMichael Halcrow 				"file version [%d] is supported by this "
1344237fead6SMichael Halcrow 				"version of eCryptfs\n",
1345237fead6SMichael Halcrow 				crypt_stat->file_version,
1346237fead6SMichael Halcrow 				ECRYPTFS_SUPPORTED_FILE_VERSION);
1347237fead6SMichael Halcrow 		rc = -EINVAL;
1348237fead6SMichael Halcrow 		goto out;
1349237fead6SMichael Halcrow 	}
1350237fead6SMichael Halcrow 	offset += bytes_read;
1351237fead6SMichael Halcrow 	if (crypt_stat->file_version >= 1) {
1352237fead6SMichael Halcrow 		rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1353dd2a3b7aSMichael Halcrow 					   &bytes_read, validate_header_size);
1354237fead6SMichael Halcrow 		if (rc) {
1355237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Error reading header "
1356237fead6SMichael Halcrow 					"metadata; rc = [%d]\n", rc);
1357237fead6SMichael Halcrow 		}
1358237fead6SMichael Halcrow 		offset += bytes_read;
1359237fead6SMichael Halcrow 	} else
1360237fead6SMichael Halcrow 		set_default_header_data(crypt_stat);
1361237fead6SMichael Halcrow 	rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1362237fead6SMichael Halcrow 				       ecryptfs_dentry);
1363237fead6SMichael Halcrow out:
1364237fead6SMichael Halcrow 	return rc;
1365237fead6SMichael Halcrow }
1366237fead6SMichael Halcrow 
1367237fead6SMichael Halcrow /**
1368dd2a3b7aSMichael Halcrow  * ecryptfs_read_xattr_region
136922e78fafSMichael Halcrow  * @page_virt: The vitual address into which to read the xattr data
13702ed92554SMichael Halcrow  * @ecryptfs_inode: The eCryptfs inode
1371dd2a3b7aSMichael Halcrow  *
1372dd2a3b7aSMichael Halcrow  * Attempts to read the crypto metadata from the extended attribute
1373dd2a3b7aSMichael Halcrow  * region of the lower file.
137422e78fafSMichael Halcrow  *
137522e78fafSMichael Halcrow  * Returns zero on success; non-zero on error
1376dd2a3b7aSMichael Halcrow  */
1377d7cdc5feSMichael Halcrow int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
1378dd2a3b7aSMichael Halcrow {
1379d7cdc5feSMichael Halcrow 	struct dentry *lower_dentry =
1380d7cdc5feSMichael Halcrow 		ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
1381dd2a3b7aSMichael Halcrow 	ssize_t size;
1382dd2a3b7aSMichael Halcrow 	int rc = 0;
1383dd2a3b7aSMichael Halcrow 
1384d7cdc5feSMichael Halcrow 	size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1385dd2a3b7aSMichael Halcrow 				       page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1386dd2a3b7aSMichael Halcrow 	if (size < 0) {
138725bd8174SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0))
138825bd8174SMichael Halcrow 			printk(KERN_INFO "Error attempting to read the [%s] "
138925bd8174SMichael Halcrow 			       "xattr from the lower file; return value = "
139025bd8174SMichael Halcrow 			       "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
1391dd2a3b7aSMichael Halcrow 		rc = -EINVAL;
1392dd2a3b7aSMichael Halcrow 		goto out;
1393dd2a3b7aSMichael Halcrow 	}
1394dd2a3b7aSMichael Halcrow out:
1395dd2a3b7aSMichael Halcrow 	return rc;
1396dd2a3b7aSMichael Halcrow }
1397dd2a3b7aSMichael Halcrow 
1398778aeb42STyler Hicks int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
13993b06b3ebSTyler Hicks 					    struct inode *inode)
1400dd2a3b7aSMichael Halcrow {
1401778aeb42STyler Hicks 	u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
1402778aeb42STyler Hicks 	u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
1403dd2a3b7aSMichael Halcrow 	int rc;
1404dd2a3b7aSMichael Halcrow 
1405778aeb42STyler Hicks 	rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1406778aeb42STyler Hicks 				     ECRYPTFS_XATTR_NAME, file_size,
1407778aeb42STyler Hicks 				     ECRYPTFS_SIZE_AND_MARKER_BYTES);
1408778aeb42STyler Hicks 	if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
1409778aeb42STyler Hicks 		return rc >= 0 ? -EINVAL : rc;
1410778aeb42STyler Hicks 	rc = ecryptfs_validate_marker(marker);
1411778aeb42STyler Hicks 	if (!rc)
1412778aeb42STyler Hicks 		ecryptfs_i_size_init(file_size, inode);
1413dd2a3b7aSMichael Halcrow 	return rc;
1414dd2a3b7aSMichael Halcrow }
1415dd2a3b7aSMichael Halcrow 
1416dd2a3b7aSMichael Halcrow /**
1417dd2a3b7aSMichael Halcrow  * ecryptfs_read_metadata
1418dd2a3b7aSMichael Halcrow  *
1419dd2a3b7aSMichael Halcrow  * Common entry point for reading file metadata. From here, we could
1420dd2a3b7aSMichael Halcrow  * retrieve the header information from the header region of the file,
1421dd2a3b7aSMichael Halcrow  * the xattr region of the file, or some other repostory that is
1422dd2a3b7aSMichael Halcrow  * stored separately from the file itself. The current implementation
1423dd2a3b7aSMichael Halcrow  * supports retrieving the metadata information from the file contents
1424dd2a3b7aSMichael Halcrow  * and from the xattr region.
1425237fead6SMichael Halcrow  *
1426237fead6SMichael Halcrow  * Returns zero if valid headers found and parsed; non-zero otherwise
1427237fead6SMichael Halcrow  */
1428d7cdc5feSMichael Halcrow int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
1429237fead6SMichael Halcrow {
1430bb450361STim Gardner 	int rc;
1431bb450361STim Gardner 	char *page_virt;
1432d7cdc5feSMichael Halcrow 	struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
1433237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
1434d7cdc5feSMichael Halcrow 	    &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1435e77a56ddSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1436e77a56ddSMichael Halcrow 		&ecryptfs_superblock_to_private(
1437e77a56ddSMichael Halcrow 			ecryptfs_dentry->d_sb)->mount_crypt_stat;
1438237fead6SMichael Halcrow 
1439e77a56ddSMichael Halcrow 	ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1440e77a56ddSMichael Halcrow 						      mount_crypt_stat);
1441237fead6SMichael Halcrow 	/* Read the first page from the underlying file */
144230632870STyler Hicks 	page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
1443237fead6SMichael Halcrow 	if (!page_virt) {
1444237fead6SMichael Halcrow 		rc = -ENOMEM;
1445d7cdc5feSMichael Halcrow 		printk(KERN_ERR "%s: Unable to allocate page_virt\n",
144618d1dbf1SHarvey Harrison 		       __func__);
1447237fead6SMichael Halcrow 		goto out;
1448237fead6SMichael Halcrow 	}
1449d7cdc5feSMichael Halcrow 	rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1450d7cdc5feSMichael Halcrow 				 ecryptfs_inode);
145196a7b9c2STyler Hicks 	if (rc >= 0)
1452237fead6SMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1453dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1454dd2a3b7aSMichael Halcrow 						ECRYPTFS_VALIDATE_HEADER_SIZE);
1455dd2a3b7aSMichael Halcrow 	if (rc) {
1456bb450361STim Gardner 		/* metadata is not in the file header, so try xattrs */
14571984c23fSTyler Hicks 		memset(page_virt, 0, PAGE_CACHE_SIZE);
1458d7cdc5feSMichael Halcrow 		rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
1459237fead6SMichael Halcrow 		if (rc) {
1460dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
146130373dc0STim Gardner 			       "file header region or xattr region, inode %lu\n",
146230373dc0STim Gardner 				ecryptfs_inode->i_ino);
1463237fead6SMichael Halcrow 			rc = -EINVAL;
1464dd2a3b7aSMichael Halcrow 			goto out;
1465dd2a3b7aSMichael Halcrow 		}
1466dd2a3b7aSMichael Halcrow 		rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1467dd2a3b7aSMichael Halcrow 						ecryptfs_dentry,
1468dd2a3b7aSMichael Halcrow 						ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1469dd2a3b7aSMichael Halcrow 		if (rc) {
1470dd2a3b7aSMichael Halcrow 			printk(KERN_DEBUG "Valid eCryptfs headers not found in "
147130373dc0STim Gardner 			       "file xattr region either, inode %lu\n",
147230373dc0STim Gardner 				ecryptfs_inode->i_ino);
1473dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1474dd2a3b7aSMichael Halcrow 		}
1475dd2a3b7aSMichael Halcrow 		if (crypt_stat->mount_crypt_stat->flags
1476dd2a3b7aSMichael Halcrow 		    & ECRYPTFS_XATTR_METADATA_ENABLED) {
1477dd2a3b7aSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1478dd2a3b7aSMichael Halcrow 		} else {
1479dd2a3b7aSMichael Halcrow 			printk(KERN_WARNING "Attempt to access file with "
1480dd2a3b7aSMichael Halcrow 			       "crypto metadata only in the extended attribute "
1481dd2a3b7aSMichael Halcrow 			       "region, but eCryptfs was mounted without "
1482dd2a3b7aSMichael Halcrow 			       "xattr support enabled. eCryptfs will not treat "
148330373dc0STim Gardner 			       "this like an encrypted file, inode %lu\n",
148430373dc0STim Gardner 				ecryptfs_inode->i_ino);
1485dd2a3b7aSMichael Halcrow 			rc = -EINVAL;
1486dd2a3b7aSMichael Halcrow 		}
1487237fead6SMichael Halcrow 	}
1488237fead6SMichael Halcrow out:
1489237fead6SMichael Halcrow 	if (page_virt) {
1490237fead6SMichael Halcrow 		memset(page_virt, 0, PAGE_CACHE_SIZE);
149130632870STyler Hicks 		kmem_cache_free(ecryptfs_header_cache, page_virt);
1492237fead6SMichael Halcrow 	}
1493237fead6SMichael Halcrow 	return rc;
1494237fead6SMichael Halcrow }
1495237fead6SMichael Halcrow 
1496237fead6SMichael Halcrow /**
149751ca58dcSMichael Halcrow  * ecryptfs_encrypt_filename - encrypt filename
149851ca58dcSMichael Halcrow  *
149951ca58dcSMichael Halcrow  * CBC-encrypts the filename. We do not want to encrypt the same
150051ca58dcSMichael Halcrow  * filename with the same key and IV, which may happen with hard
150151ca58dcSMichael Halcrow  * links, so we prepend random bits to each filename.
150251ca58dcSMichael Halcrow  *
150351ca58dcSMichael Halcrow  * Returns zero on success; non-zero otherwise
150451ca58dcSMichael Halcrow  */
150551ca58dcSMichael Halcrow static int
150651ca58dcSMichael Halcrow ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
150751ca58dcSMichael Halcrow 			  struct ecryptfs_crypt_stat *crypt_stat,
150851ca58dcSMichael Halcrow 			  struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
150951ca58dcSMichael Halcrow {
151051ca58dcSMichael Halcrow 	int rc = 0;
151151ca58dcSMichael Halcrow 
151251ca58dcSMichael Halcrow 	filename->encrypted_filename = NULL;
151351ca58dcSMichael Halcrow 	filename->encrypted_filename_size = 0;
151451ca58dcSMichael Halcrow 	if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
151551ca58dcSMichael Halcrow 	    || (mount_crypt_stat && (mount_crypt_stat->flags
151651ca58dcSMichael Halcrow 				     & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
151751ca58dcSMichael Halcrow 		size_t packet_size;
151851ca58dcSMichael Halcrow 		size_t remaining_bytes;
151951ca58dcSMichael Halcrow 
152051ca58dcSMichael Halcrow 		rc = ecryptfs_write_tag_70_packet(
152151ca58dcSMichael Halcrow 			NULL, NULL,
152251ca58dcSMichael Halcrow 			&filename->encrypted_filename_size,
152351ca58dcSMichael Halcrow 			mount_crypt_stat, NULL,
152451ca58dcSMichael Halcrow 			filename->filename_size);
152551ca58dcSMichael Halcrow 		if (rc) {
152651ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to get packet "
152751ca58dcSMichael Halcrow 			       "size for tag 72; rc = [%d]\n", __func__,
152851ca58dcSMichael Halcrow 			       rc);
152951ca58dcSMichael Halcrow 			filename->encrypted_filename_size = 0;
153051ca58dcSMichael Halcrow 			goto out;
153151ca58dcSMichael Halcrow 		}
153251ca58dcSMichael Halcrow 		filename->encrypted_filename =
153351ca58dcSMichael Halcrow 			kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
153451ca58dcSMichael Halcrow 		if (!filename->encrypted_filename) {
153551ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
1536df261c52SMichael Halcrow 			       "to kmalloc [%zd] bytes\n", __func__,
153751ca58dcSMichael Halcrow 			       filename->encrypted_filename_size);
153851ca58dcSMichael Halcrow 			rc = -ENOMEM;
153951ca58dcSMichael Halcrow 			goto out;
154051ca58dcSMichael Halcrow 		}
154151ca58dcSMichael Halcrow 		remaining_bytes = filename->encrypted_filename_size;
154251ca58dcSMichael Halcrow 		rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
154351ca58dcSMichael Halcrow 						  &remaining_bytes,
154451ca58dcSMichael Halcrow 						  &packet_size,
154551ca58dcSMichael Halcrow 						  mount_crypt_stat,
154651ca58dcSMichael Halcrow 						  filename->filename,
154751ca58dcSMichael Halcrow 						  filename->filename_size);
154851ca58dcSMichael Halcrow 		if (rc) {
154951ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to generate "
155051ca58dcSMichael Halcrow 			       "tag 70 packet; rc = [%d]\n", __func__,
155151ca58dcSMichael Halcrow 			       rc);
155251ca58dcSMichael Halcrow 			kfree(filename->encrypted_filename);
155351ca58dcSMichael Halcrow 			filename->encrypted_filename = NULL;
155451ca58dcSMichael Halcrow 			filename->encrypted_filename_size = 0;
155551ca58dcSMichael Halcrow 			goto out;
155651ca58dcSMichael Halcrow 		}
155751ca58dcSMichael Halcrow 		filename->encrypted_filename_size = packet_size;
155851ca58dcSMichael Halcrow 	} else {
155951ca58dcSMichael Halcrow 		printk(KERN_ERR "%s: No support for requested filename "
156051ca58dcSMichael Halcrow 		       "encryption method in this release\n", __func__);
1561df6ad33bSTyler Hicks 		rc = -EOPNOTSUPP;
156251ca58dcSMichael Halcrow 		goto out;
156351ca58dcSMichael Halcrow 	}
156451ca58dcSMichael Halcrow out:
156551ca58dcSMichael Halcrow 	return rc;
156651ca58dcSMichael Halcrow }
156751ca58dcSMichael Halcrow 
156851ca58dcSMichael Halcrow static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
156951ca58dcSMichael Halcrow 				  const char *name, size_t name_size)
157051ca58dcSMichael Halcrow {
157151ca58dcSMichael Halcrow 	int rc = 0;
157251ca58dcSMichael Halcrow 
1573fd9fc842STyler Hicks 	(*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
157451ca58dcSMichael Halcrow 	if (!(*copied_name)) {
157551ca58dcSMichael Halcrow 		rc = -ENOMEM;
157651ca58dcSMichael Halcrow 		goto out;
157751ca58dcSMichael Halcrow 	}
157851ca58dcSMichael Halcrow 	memcpy((void *)(*copied_name), (void *)name, name_size);
157951ca58dcSMichael Halcrow 	(*copied_name)[(name_size)] = '\0';	/* Only for convenience
158051ca58dcSMichael Halcrow 						 * in printing out the
158151ca58dcSMichael Halcrow 						 * string in debug
158251ca58dcSMichael Halcrow 						 * messages */
1583fd9fc842STyler Hicks 	(*copied_name_size) = name_size;
158451ca58dcSMichael Halcrow out:
158551ca58dcSMichael Halcrow 	return rc;
158651ca58dcSMichael Halcrow }
158751ca58dcSMichael Halcrow 
158851ca58dcSMichael Halcrow /**
1589f4aad16aSMichael Halcrow  * ecryptfs_process_key_cipher - Perform key cipher initialization.
1590237fead6SMichael Halcrow  * @key_tfm: Crypto context for key material, set by this function
1591e5d9cbdeSMichael Halcrow  * @cipher_name: Name of the cipher
1592e5d9cbdeSMichael Halcrow  * @key_size: Size of the key in bytes
1593237fead6SMichael Halcrow  *
1594237fead6SMichael Halcrow  * Returns zero on success. Any crypto_tfm structs allocated here
1595237fead6SMichael Halcrow  * should be released by other functions, such as on a superblock put
1596237fead6SMichael Halcrow  * event, regardless of whether this function succeeds for fails.
1597237fead6SMichael Halcrow  */
1598cd9d67dfSMichael Halcrow static int
1599f4aad16aSMichael Halcrow ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1600f4aad16aSMichael Halcrow 			    char *cipher_name, size_t *key_size)
1601237fead6SMichael Halcrow {
1602237fead6SMichael Halcrow 	char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
1603ece550f5SDan Carpenter 	char *full_alg_name = NULL;
1604237fead6SMichael Halcrow 	int rc;
1605237fead6SMichael Halcrow 
1606e5d9cbdeSMichael Halcrow 	*key_tfm = NULL;
1607e5d9cbdeSMichael Halcrow 	if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1608237fead6SMichael Halcrow 		rc = -EINVAL;
1609df261c52SMichael Halcrow 		printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
1610e5d9cbdeSMichael Halcrow 		      "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1611237fead6SMichael Halcrow 		goto out;
1612237fead6SMichael Halcrow 	}
16138bba066fSMichael Halcrow 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
16148bba066fSMichael Halcrow 						    "ecb");
16158bba066fSMichael Halcrow 	if (rc)
16168bba066fSMichael Halcrow 		goto out;
16178bba066fSMichael Halcrow 	*key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
16188bba066fSMichael Halcrow 	if (IS_ERR(*key_tfm)) {
16198bba066fSMichael Halcrow 		rc = PTR_ERR(*key_tfm);
1620237fead6SMichael Halcrow 		printk(KERN_ERR "Unable to allocate crypto cipher with name "
162138268498SDave Hansen 		       "[%s]; rc = [%d]\n", full_alg_name, rc);
1622237fead6SMichael Halcrow 		goto out;
1623237fead6SMichael Halcrow 	}
16248bba066fSMichael Halcrow 	crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
16258bba066fSMichael Halcrow 	if (*key_size == 0) {
16268bba066fSMichael Halcrow 		struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
16278bba066fSMichael Halcrow 
16288bba066fSMichael Halcrow 		*key_size = alg->max_keysize;
16298bba066fSMichael Halcrow 	}
1630e5d9cbdeSMichael Halcrow 	get_random_bytes(dummy_key, *key_size);
16318bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
1632237fead6SMichael Halcrow 	if (rc) {
1633df261c52SMichael Halcrow 		printk(KERN_ERR "Error attempting to set key of size [%zd] for "
163438268498SDave Hansen 		       "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
163538268498SDave Hansen 		       rc);
1636237fead6SMichael Halcrow 		rc = -EINVAL;
1637237fead6SMichael Halcrow 		goto out;
1638237fead6SMichael Halcrow 	}
1639237fead6SMichael Halcrow out:
1640ece550f5SDan Carpenter 	kfree(full_alg_name);
1641237fead6SMichael Halcrow 	return rc;
1642237fead6SMichael Halcrow }
1643f4aad16aSMichael Halcrow 
1644f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_tfm_cache;
16457896b631SAdrian Bunk static struct list_head key_tfm_list;
1646af440f52SEric Sandeen struct mutex key_tfm_list_mutex;
1647f4aad16aSMichael Halcrow 
16487371a382SJerome Marchand int __init ecryptfs_init_crypto(void)
1649f4aad16aSMichael Halcrow {
1650f4aad16aSMichael Halcrow 	mutex_init(&key_tfm_list_mutex);
1651f4aad16aSMichael Halcrow 	INIT_LIST_HEAD(&key_tfm_list);
1652f4aad16aSMichael Halcrow 	return 0;
1653f4aad16aSMichael Halcrow }
1654f4aad16aSMichael Halcrow 
1655af440f52SEric Sandeen /**
1656af440f52SEric Sandeen  * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
1657af440f52SEric Sandeen  *
1658af440f52SEric Sandeen  * Called only at module unload time
1659af440f52SEric Sandeen  */
1660fcd12835SMichael Halcrow int ecryptfs_destroy_crypto(void)
1661f4aad16aSMichael Halcrow {
1662f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1663f4aad16aSMichael Halcrow 
1664f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1665f4aad16aSMichael Halcrow 	list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1666f4aad16aSMichael Halcrow 				 key_tfm_list) {
1667f4aad16aSMichael Halcrow 		list_del(&key_tfm->key_tfm_list);
1668f4aad16aSMichael Halcrow 		if (key_tfm->key_tfm)
1669f4aad16aSMichael Halcrow 			crypto_free_blkcipher(key_tfm->key_tfm);
1670f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1671f4aad16aSMichael Halcrow 	}
1672f4aad16aSMichael Halcrow 	mutex_unlock(&key_tfm_list_mutex);
1673f4aad16aSMichael Halcrow 	return 0;
1674f4aad16aSMichael Halcrow }
1675f4aad16aSMichael Halcrow 
1676f4aad16aSMichael Halcrow int
1677f4aad16aSMichael Halcrow ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1678f4aad16aSMichael Halcrow 			 size_t key_size)
1679f4aad16aSMichael Halcrow {
1680f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *tmp_tfm;
1681f4aad16aSMichael Halcrow 	int rc = 0;
1682f4aad16aSMichael Halcrow 
1683af440f52SEric Sandeen 	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1684af440f52SEric Sandeen 
1685f4aad16aSMichael Halcrow 	tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1686f4aad16aSMichael Halcrow 	if (key_tfm != NULL)
1687f4aad16aSMichael Halcrow 		(*key_tfm) = tmp_tfm;
1688f4aad16aSMichael Halcrow 	if (!tmp_tfm) {
1689f4aad16aSMichael Halcrow 		rc = -ENOMEM;
1690f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to allocate from "
1691f4aad16aSMichael Halcrow 		       "ecryptfs_key_tfm_cache\n");
1692f4aad16aSMichael Halcrow 		goto out;
1693f4aad16aSMichael Halcrow 	}
1694f4aad16aSMichael Halcrow 	mutex_init(&tmp_tfm->key_tfm_mutex);
1695f4aad16aSMichael Halcrow 	strncpy(tmp_tfm->cipher_name, cipher_name,
1696f4aad16aSMichael Halcrow 		ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1697b8862906SEric Sandeen 	tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
1698f4aad16aSMichael Halcrow 	tmp_tfm->key_size = key_size;
16995dda6992SMichael Halcrow 	rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1700f4aad16aSMichael Halcrow 					 tmp_tfm->cipher_name,
17015dda6992SMichael Halcrow 					 &tmp_tfm->key_size);
17025dda6992SMichael Halcrow 	if (rc) {
1703f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error attempting to initialize key TFM "
1704f4aad16aSMichael Halcrow 		       "cipher with name = [%s]; rc = [%d]\n",
1705f4aad16aSMichael Halcrow 		       tmp_tfm->cipher_name, rc);
1706f4aad16aSMichael Halcrow 		kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1707f4aad16aSMichael Halcrow 		if (key_tfm != NULL)
1708f4aad16aSMichael Halcrow 			(*key_tfm) = NULL;
1709f4aad16aSMichael Halcrow 		goto out;
1710f4aad16aSMichael Halcrow 	}
1711f4aad16aSMichael Halcrow 	list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1712f4aad16aSMichael Halcrow out:
1713f4aad16aSMichael Halcrow 	return rc;
1714f4aad16aSMichael Halcrow }
1715f4aad16aSMichael Halcrow 
1716af440f52SEric Sandeen /**
1717af440f52SEric Sandeen  * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
1718af440f52SEric Sandeen  * @cipher_name: the name of the cipher to search for
1719af440f52SEric Sandeen  * @key_tfm: set to corresponding tfm if found
1720af440f52SEric Sandeen  *
1721af440f52SEric Sandeen  * Searches for cached key_tfm matching @cipher_name
1722af440f52SEric Sandeen  * Must be called with &key_tfm_list_mutex held
1723af440f52SEric Sandeen  * Returns 1 if found, with @key_tfm set
1724af440f52SEric Sandeen  * Returns 0 if not found, with @key_tfm set to NULL
1725af440f52SEric Sandeen  */
1726af440f52SEric Sandeen int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
1727af440f52SEric Sandeen {
1728af440f52SEric Sandeen 	struct ecryptfs_key_tfm *tmp_key_tfm;
1729af440f52SEric Sandeen 
1730af440f52SEric Sandeen 	BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
1731af440f52SEric Sandeen 
1732af440f52SEric Sandeen 	list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
1733af440f52SEric Sandeen 		if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
1734af440f52SEric Sandeen 			if (key_tfm)
1735af440f52SEric Sandeen 				(*key_tfm) = tmp_key_tfm;
1736af440f52SEric Sandeen 			return 1;
1737af440f52SEric Sandeen 		}
1738af440f52SEric Sandeen 	}
1739af440f52SEric Sandeen 	if (key_tfm)
1740af440f52SEric Sandeen 		(*key_tfm) = NULL;
1741af440f52SEric Sandeen 	return 0;
1742af440f52SEric Sandeen }
1743af440f52SEric Sandeen 
1744af440f52SEric Sandeen /**
1745af440f52SEric Sandeen  * ecryptfs_get_tfm_and_mutex_for_cipher_name
1746af440f52SEric Sandeen  *
1747af440f52SEric Sandeen  * @tfm: set to cached tfm found, or new tfm created
1748af440f52SEric Sandeen  * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
1749af440f52SEric Sandeen  * @cipher_name: the name of the cipher to search for and/or add
1750af440f52SEric Sandeen  *
1751af440f52SEric Sandeen  * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
1752af440f52SEric Sandeen  * Searches for cached item first, and creates new if not found.
1753af440f52SEric Sandeen  * Returns 0 on success, non-zero if adding new cipher failed
1754af440f52SEric Sandeen  */
1755f4aad16aSMichael Halcrow int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1756f4aad16aSMichael Halcrow 					       struct mutex **tfm_mutex,
1757f4aad16aSMichael Halcrow 					       char *cipher_name)
1758f4aad16aSMichael Halcrow {
1759f4aad16aSMichael Halcrow 	struct ecryptfs_key_tfm *key_tfm;
1760f4aad16aSMichael Halcrow 	int rc = 0;
1761f4aad16aSMichael Halcrow 
1762f4aad16aSMichael Halcrow 	(*tfm) = NULL;
1763f4aad16aSMichael Halcrow 	(*tfm_mutex) = NULL;
1764af440f52SEric Sandeen 
1765f4aad16aSMichael Halcrow 	mutex_lock(&key_tfm_list_mutex);
1766af440f52SEric Sandeen 	if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
17675dda6992SMichael Halcrow 		rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
17685dda6992SMichael Halcrow 		if (rc) {
1769af440f52SEric Sandeen 			printk(KERN_ERR "Error adding new key_tfm to list; "
1770af440f52SEric Sandeen 					"rc = [%d]\n", rc);
1771f4aad16aSMichael Halcrow 			goto out;
1772f4aad16aSMichael Halcrow 		}
1773af440f52SEric Sandeen 	}
1774f4aad16aSMichael Halcrow 	(*tfm) = key_tfm->key_tfm;
1775f4aad16aSMichael Halcrow 	(*tfm_mutex) = &key_tfm->key_tfm_mutex;
1776f4aad16aSMichael Halcrow out:
177771fd5179SCyrill Gorcunov 	mutex_unlock(&key_tfm_list_mutex);
1778f4aad16aSMichael Halcrow 	return rc;
1779f4aad16aSMichael Halcrow }
178051ca58dcSMichael Halcrow 
178151ca58dcSMichael Halcrow /* 64 characters forming a 6-bit target field */
178251ca58dcSMichael Halcrow static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
178351ca58dcSMichael Halcrow 						 "EFGHIJKLMNOPQRST"
178451ca58dcSMichael Halcrow 						 "UVWXYZabcdefghij"
178551ca58dcSMichael Halcrow 						 "klmnopqrstuvwxyz");
178651ca58dcSMichael Halcrow 
178751ca58dcSMichael Halcrow /* We could either offset on every reverse map or just pad some 0x00's
178851ca58dcSMichael Halcrow  * at the front here */
17890f751e64STyler Hicks static const unsigned char filename_rev_map[256] = {
179051ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
179151ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
179251ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
179351ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
179451ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
179551ca58dcSMichael Halcrow 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
179651ca58dcSMichael Halcrow 	0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
179751ca58dcSMichael Halcrow 	0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
179851ca58dcSMichael Halcrow 	0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
179951ca58dcSMichael Halcrow 	0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
180051ca58dcSMichael Halcrow 	0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
180151ca58dcSMichael Halcrow 	0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
180251ca58dcSMichael Halcrow 	0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
180351ca58dcSMichael Halcrow 	0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
180451ca58dcSMichael Halcrow 	0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
18050f751e64STyler Hicks 	0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
180651ca58dcSMichael Halcrow };
180751ca58dcSMichael Halcrow 
180851ca58dcSMichael Halcrow /**
180951ca58dcSMichael Halcrow  * ecryptfs_encode_for_filename
181051ca58dcSMichael Halcrow  * @dst: Destination location for encoded filename
181151ca58dcSMichael Halcrow  * @dst_size: Size of the encoded filename in bytes
181251ca58dcSMichael Halcrow  * @src: Source location for the filename to encode
181351ca58dcSMichael Halcrow  * @src_size: Size of the source in bytes
181451ca58dcSMichael Halcrow  */
181537028758SCong Ding static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
181651ca58dcSMichael Halcrow 				  unsigned char *src, size_t src_size)
181751ca58dcSMichael Halcrow {
181851ca58dcSMichael Halcrow 	size_t num_blocks;
181951ca58dcSMichael Halcrow 	size_t block_num = 0;
182051ca58dcSMichael Halcrow 	size_t dst_offset = 0;
182151ca58dcSMichael Halcrow 	unsigned char last_block[3];
182251ca58dcSMichael Halcrow 
182351ca58dcSMichael Halcrow 	if (src_size == 0) {
182451ca58dcSMichael Halcrow 		(*dst_size) = 0;
182551ca58dcSMichael Halcrow 		goto out;
182651ca58dcSMichael Halcrow 	}
182751ca58dcSMichael Halcrow 	num_blocks = (src_size / 3);
182851ca58dcSMichael Halcrow 	if ((src_size % 3) == 0) {
182951ca58dcSMichael Halcrow 		memcpy(last_block, (&src[src_size - 3]), 3);
183051ca58dcSMichael Halcrow 	} else {
183151ca58dcSMichael Halcrow 		num_blocks++;
183251ca58dcSMichael Halcrow 		last_block[2] = 0x00;
183351ca58dcSMichael Halcrow 		switch (src_size % 3) {
183451ca58dcSMichael Halcrow 		case 1:
183551ca58dcSMichael Halcrow 			last_block[0] = src[src_size - 1];
183651ca58dcSMichael Halcrow 			last_block[1] = 0x00;
183751ca58dcSMichael Halcrow 			break;
183851ca58dcSMichael Halcrow 		case 2:
183951ca58dcSMichael Halcrow 			last_block[0] = src[src_size - 2];
184051ca58dcSMichael Halcrow 			last_block[1] = src[src_size - 1];
184151ca58dcSMichael Halcrow 		}
184251ca58dcSMichael Halcrow 	}
184351ca58dcSMichael Halcrow 	(*dst_size) = (num_blocks * 4);
184451ca58dcSMichael Halcrow 	if (!dst)
184551ca58dcSMichael Halcrow 		goto out;
184651ca58dcSMichael Halcrow 	while (block_num < num_blocks) {
184751ca58dcSMichael Halcrow 		unsigned char *src_block;
184851ca58dcSMichael Halcrow 		unsigned char dst_block[4];
184951ca58dcSMichael Halcrow 
185051ca58dcSMichael Halcrow 		if (block_num == (num_blocks - 1))
185151ca58dcSMichael Halcrow 			src_block = last_block;
185251ca58dcSMichael Halcrow 		else
185351ca58dcSMichael Halcrow 			src_block = &src[block_num * 3];
185451ca58dcSMichael Halcrow 		dst_block[0] = ((src_block[0] >> 2) & 0x3F);
185551ca58dcSMichael Halcrow 		dst_block[1] = (((src_block[0] << 4) & 0x30)
185651ca58dcSMichael Halcrow 				| ((src_block[1] >> 4) & 0x0F));
185751ca58dcSMichael Halcrow 		dst_block[2] = (((src_block[1] << 2) & 0x3C)
185851ca58dcSMichael Halcrow 				| ((src_block[2] >> 6) & 0x03));
185951ca58dcSMichael Halcrow 		dst_block[3] = (src_block[2] & 0x3F);
186051ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[0]];
186151ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[1]];
186251ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[2]];
186351ca58dcSMichael Halcrow 		dst[dst_offset++] = portable_filename_chars[dst_block[3]];
186451ca58dcSMichael Halcrow 		block_num++;
186551ca58dcSMichael Halcrow 	}
186651ca58dcSMichael Halcrow out:
186751ca58dcSMichael Halcrow 	return;
186851ca58dcSMichael Halcrow }
186951ca58dcSMichael Halcrow 
18704a26620dSTyler Hicks static size_t ecryptfs_max_decoded_size(size_t encoded_size)
18714a26620dSTyler Hicks {
18724a26620dSTyler Hicks 	/* Not exact; conservatively long. Every block of 4
18734a26620dSTyler Hicks 	 * encoded characters decodes into a block of 3
18744a26620dSTyler Hicks 	 * decoded characters. This segment of code provides
18754a26620dSTyler Hicks 	 * the caller with the maximum amount of allocated
18764a26620dSTyler Hicks 	 * space that @dst will need to point to in a
18774a26620dSTyler Hicks 	 * subsequent call. */
18784a26620dSTyler Hicks 	return ((encoded_size + 1) * 3) / 4;
18794a26620dSTyler Hicks }
18804a26620dSTyler Hicks 
188171c11c37SMichael Halcrow /**
188271c11c37SMichael Halcrow  * ecryptfs_decode_from_filename
188371c11c37SMichael Halcrow  * @dst: If NULL, this function only sets @dst_size and returns. If
188471c11c37SMichael Halcrow  *       non-NULL, this function decodes the encoded octets in @src
188571c11c37SMichael Halcrow  *       into the memory that @dst points to.
188671c11c37SMichael Halcrow  * @dst_size: Set to the size of the decoded string.
188771c11c37SMichael Halcrow  * @src: The encoded set of octets to decode.
188871c11c37SMichael Halcrow  * @src_size: The size of the encoded set of octets to decode.
188971c11c37SMichael Halcrow  */
189071c11c37SMichael Halcrow static void
189171c11c37SMichael Halcrow ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
189251ca58dcSMichael Halcrow 			      const unsigned char *src, size_t src_size)
189351ca58dcSMichael Halcrow {
189451ca58dcSMichael Halcrow 	u8 current_bit_offset = 0;
189551ca58dcSMichael Halcrow 	size_t src_byte_offset = 0;
189651ca58dcSMichael Halcrow 	size_t dst_byte_offset = 0;
189751ca58dcSMichael Halcrow 
189851ca58dcSMichael Halcrow 	if (dst == NULL) {
18994a26620dSTyler Hicks 		(*dst_size) = ecryptfs_max_decoded_size(src_size);
190051ca58dcSMichael Halcrow 		goto out;
190151ca58dcSMichael Halcrow 	}
190251ca58dcSMichael Halcrow 	while (src_byte_offset < src_size) {
190351ca58dcSMichael Halcrow 		unsigned char src_byte =
190451ca58dcSMichael Halcrow 				filename_rev_map[(int)src[src_byte_offset]];
190551ca58dcSMichael Halcrow 
190651ca58dcSMichael Halcrow 		switch (current_bit_offset) {
190751ca58dcSMichael Halcrow 		case 0:
190851ca58dcSMichael Halcrow 			dst[dst_byte_offset] = (src_byte << 2);
190951ca58dcSMichael Halcrow 			current_bit_offset = 6;
191051ca58dcSMichael Halcrow 			break;
191151ca58dcSMichael Halcrow 		case 6:
191251ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte >> 4);
191351ca58dcSMichael Halcrow 			dst[dst_byte_offset] = ((src_byte & 0xF)
191451ca58dcSMichael Halcrow 						 << 4);
191551ca58dcSMichael Halcrow 			current_bit_offset = 4;
191651ca58dcSMichael Halcrow 			break;
191751ca58dcSMichael Halcrow 		case 4:
191851ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte >> 2);
191951ca58dcSMichael Halcrow 			dst[dst_byte_offset] = (src_byte << 6);
192051ca58dcSMichael Halcrow 			current_bit_offset = 2;
192151ca58dcSMichael Halcrow 			break;
192251ca58dcSMichael Halcrow 		case 2:
192351ca58dcSMichael Halcrow 			dst[dst_byte_offset++] |= (src_byte);
192451ca58dcSMichael Halcrow 			dst[dst_byte_offset] = 0;
192551ca58dcSMichael Halcrow 			current_bit_offset = 0;
192651ca58dcSMichael Halcrow 			break;
192751ca58dcSMichael Halcrow 		}
192851ca58dcSMichael Halcrow 		src_byte_offset++;
192951ca58dcSMichael Halcrow 	}
193051ca58dcSMichael Halcrow 	(*dst_size) = dst_byte_offset;
193151ca58dcSMichael Halcrow out:
193271c11c37SMichael Halcrow 	return;
193351ca58dcSMichael Halcrow }
193451ca58dcSMichael Halcrow 
193551ca58dcSMichael Halcrow /**
193651ca58dcSMichael Halcrow  * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
193751ca58dcSMichael Halcrow  * @crypt_stat: The crypt_stat struct associated with the file anem to encode
193851ca58dcSMichael Halcrow  * @name: The plaintext name
193951ca58dcSMichael Halcrow  * @length: The length of the plaintext
194051ca58dcSMichael Halcrow  * @encoded_name: The encypted name
194151ca58dcSMichael Halcrow  *
194251ca58dcSMichael Halcrow  * Encrypts and encodes a filename into something that constitutes a
194351ca58dcSMichael Halcrow  * valid filename for a filesystem, with printable characters.
194451ca58dcSMichael Halcrow  *
194551ca58dcSMichael Halcrow  * We assume that we have a properly initialized crypto context,
194651ca58dcSMichael Halcrow  * pointed to by crypt_stat->tfm.
194751ca58dcSMichael Halcrow  *
194851ca58dcSMichael Halcrow  * Returns zero on success; non-zero on otherwise
194951ca58dcSMichael Halcrow  */
195051ca58dcSMichael Halcrow int ecryptfs_encrypt_and_encode_filename(
195151ca58dcSMichael Halcrow 	char **encoded_name,
195251ca58dcSMichael Halcrow 	size_t *encoded_name_size,
195351ca58dcSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat,
195451ca58dcSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
195551ca58dcSMichael Halcrow 	const char *name, size_t name_size)
195651ca58dcSMichael Halcrow {
195751ca58dcSMichael Halcrow 	size_t encoded_name_no_prefix_size;
195851ca58dcSMichael Halcrow 	int rc = 0;
195951ca58dcSMichael Halcrow 
196051ca58dcSMichael Halcrow 	(*encoded_name) = NULL;
196151ca58dcSMichael Halcrow 	(*encoded_name_size) = 0;
196251ca58dcSMichael Halcrow 	if ((crypt_stat && (crypt_stat->flags & ECRYPTFS_ENCRYPT_FILENAMES))
196351ca58dcSMichael Halcrow 	    || (mount_crypt_stat && (mount_crypt_stat->flags
196451ca58dcSMichael Halcrow 				     & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) {
196551ca58dcSMichael Halcrow 		struct ecryptfs_filename *filename;
196651ca58dcSMichael Halcrow 
196751ca58dcSMichael Halcrow 		filename = kzalloc(sizeof(*filename), GFP_KERNEL);
196851ca58dcSMichael Halcrow 		if (!filename) {
196951ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
1970a8f12864SMichael Halcrow 			       "to kzalloc [%zd] bytes\n", __func__,
197151ca58dcSMichael Halcrow 			       sizeof(*filename));
197251ca58dcSMichael Halcrow 			rc = -ENOMEM;
197351ca58dcSMichael Halcrow 			goto out;
197451ca58dcSMichael Halcrow 		}
197551ca58dcSMichael Halcrow 		filename->filename = (char *)name;
197651ca58dcSMichael Halcrow 		filename->filename_size = name_size;
197751ca58dcSMichael Halcrow 		rc = ecryptfs_encrypt_filename(filename, crypt_stat,
197851ca58dcSMichael Halcrow 					       mount_crypt_stat);
197951ca58dcSMichael Halcrow 		if (rc) {
198051ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encrypt "
198151ca58dcSMichael Halcrow 			       "filename; rc = [%d]\n", __func__, rc);
198251ca58dcSMichael Halcrow 			kfree(filename);
198351ca58dcSMichael Halcrow 			goto out;
198451ca58dcSMichael Halcrow 		}
198551ca58dcSMichael Halcrow 		ecryptfs_encode_for_filename(
198651ca58dcSMichael Halcrow 			NULL, &encoded_name_no_prefix_size,
198751ca58dcSMichael Halcrow 			filename->encrypted_filename,
198851ca58dcSMichael Halcrow 			filename->encrypted_filename_size);
198951ca58dcSMichael Halcrow 		if ((crypt_stat && (crypt_stat->flags
199051ca58dcSMichael Halcrow 				    & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
199151ca58dcSMichael Halcrow 		    || (mount_crypt_stat
199251ca58dcSMichael Halcrow 			&& (mount_crypt_stat->flags
199351ca58dcSMichael Halcrow 			    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)))
199451ca58dcSMichael Halcrow 			(*encoded_name_size) =
199551ca58dcSMichael Halcrow 				(ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
199651ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
199751ca58dcSMichael Halcrow 		else
199851ca58dcSMichael Halcrow 			(*encoded_name_size) =
199951ca58dcSMichael Halcrow 				(ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
200051ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
200151ca58dcSMichael Halcrow 		(*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
200251ca58dcSMichael Halcrow 		if (!(*encoded_name)) {
200351ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
2004a8f12864SMichael Halcrow 			       "to kzalloc [%zd] bytes\n", __func__,
200551ca58dcSMichael Halcrow 			       (*encoded_name_size));
200651ca58dcSMichael Halcrow 			rc = -ENOMEM;
200751ca58dcSMichael Halcrow 			kfree(filename->encrypted_filename);
200851ca58dcSMichael Halcrow 			kfree(filename);
200951ca58dcSMichael Halcrow 			goto out;
201051ca58dcSMichael Halcrow 		}
201151ca58dcSMichael Halcrow 		if ((crypt_stat && (crypt_stat->flags
201251ca58dcSMichael Halcrow 				    & ECRYPTFS_ENCFN_USE_MOUNT_FNEK))
201351ca58dcSMichael Halcrow 		    || (mount_crypt_stat
201451ca58dcSMichael Halcrow 			&& (mount_crypt_stat->flags
201551ca58dcSMichael Halcrow 			    & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))) {
201651ca58dcSMichael Halcrow 			memcpy((*encoded_name),
201751ca58dcSMichael Halcrow 			       ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
201851ca58dcSMichael Halcrow 			       ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
201951ca58dcSMichael Halcrow 			ecryptfs_encode_for_filename(
202051ca58dcSMichael Halcrow 			    ((*encoded_name)
202151ca58dcSMichael Halcrow 			     + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
202251ca58dcSMichael Halcrow 			    &encoded_name_no_prefix_size,
202351ca58dcSMichael Halcrow 			    filename->encrypted_filename,
202451ca58dcSMichael Halcrow 			    filename->encrypted_filename_size);
202551ca58dcSMichael Halcrow 			(*encoded_name_size) =
202651ca58dcSMichael Halcrow 				(ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
202751ca58dcSMichael Halcrow 				 + encoded_name_no_prefix_size);
202851ca58dcSMichael Halcrow 			(*encoded_name)[(*encoded_name_size)] = '\0';
202951ca58dcSMichael Halcrow 		} else {
2030df6ad33bSTyler Hicks 			rc = -EOPNOTSUPP;
203151ca58dcSMichael Halcrow 		}
203251ca58dcSMichael Halcrow 		if (rc) {
203351ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encode "
203451ca58dcSMichael Halcrow 			       "encrypted filename; rc = [%d]\n", __func__,
203551ca58dcSMichael Halcrow 			       rc);
203651ca58dcSMichael Halcrow 			kfree((*encoded_name));
203751ca58dcSMichael Halcrow 			(*encoded_name) = NULL;
203851ca58dcSMichael Halcrow 			(*encoded_name_size) = 0;
203951ca58dcSMichael Halcrow 		}
204051ca58dcSMichael Halcrow 		kfree(filename->encrypted_filename);
204151ca58dcSMichael Halcrow 		kfree(filename);
204251ca58dcSMichael Halcrow 	} else {
204351ca58dcSMichael Halcrow 		rc = ecryptfs_copy_filename(encoded_name,
204451ca58dcSMichael Halcrow 					    encoded_name_size,
204551ca58dcSMichael Halcrow 					    name, name_size);
204651ca58dcSMichael Halcrow 	}
204751ca58dcSMichael Halcrow out:
204851ca58dcSMichael Halcrow 	return rc;
204951ca58dcSMichael Halcrow }
205051ca58dcSMichael Halcrow 
205151ca58dcSMichael Halcrow /**
205251ca58dcSMichael Halcrow  * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
205351ca58dcSMichael Halcrow  * @plaintext_name: The plaintext name
205451ca58dcSMichael Halcrow  * @plaintext_name_size: The plaintext name size
205551ca58dcSMichael Halcrow  * @ecryptfs_dir_dentry: eCryptfs directory dentry
205651ca58dcSMichael Halcrow  * @name: The filename in cipher text
205751ca58dcSMichael Halcrow  * @name_size: The cipher text name size
205851ca58dcSMichael Halcrow  *
205951ca58dcSMichael Halcrow  * Decrypts and decodes the filename.
206051ca58dcSMichael Halcrow  *
206151ca58dcSMichael Halcrow  * Returns zero on error; non-zero otherwise
206251ca58dcSMichael Halcrow  */
206351ca58dcSMichael Halcrow int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
206451ca58dcSMichael Halcrow 					 size_t *plaintext_name_size,
206551ca58dcSMichael Halcrow 					 struct dentry *ecryptfs_dir_dentry,
206651ca58dcSMichael Halcrow 					 const char *name, size_t name_size)
206751ca58dcSMichael Halcrow {
20682aac0cf8STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
20692aac0cf8STyler Hicks 		&ecryptfs_superblock_to_private(
20702aac0cf8STyler Hicks 			ecryptfs_dir_dentry->d_sb)->mount_crypt_stat;
207151ca58dcSMichael Halcrow 	char *decoded_name;
207251ca58dcSMichael Halcrow 	size_t decoded_name_size;
207351ca58dcSMichael Halcrow 	size_t packet_size;
207451ca58dcSMichael Halcrow 	int rc = 0;
207551ca58dcSMichael Halcrow 
20762aac0cf8STyler Hicks 	if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
20772aac0cf8STyler Hicks 	    && !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
20782aac0cf8STyler Hicks 	    && (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
207951ca58dcSMichael Halcrow 	    && (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
208051ca58dcSMichael Halcrow 			ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
208151ca58dcSMichael Halcrow 		const char *orig_name = name;
208251ca58dcSMichael Halcrow 		size_t orig_name_size = name_size;
208351ca58dcSMichael Halcrow 
208451ca58dcSMichael Halcrow 		name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
208551ca58dcSMichael Halcrow 		name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
208671c11c37SMichael Halcrow 		ecryptfs_decode_from_filename(NULL, &decoded_name_size,
208751ca58dcSMichael Halcrow 					      name, name_size);
208851ca58dcSMichael Halcrow 		decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
208951ca58dcSMichael Halcrow 		if (!decoded_name) {
209051ca58dcSMichael Halcrow 			printk(KERN_ERR "%s: Out of memory whilst attempting "
2091df261c52SMichael Halcrow 			       "to kmalloc [%zd] bytes\n", __func__,
209251ca58dcSMichael Halcrow 			       decoded_name_size);
209351ca58dcSMichael Halcrow 			rc = -ENOMEM;
209451ca58dcSMichael Halcrow 			goto out;
209551ca58dcSMichael Halcrow 		}
209671c11c37SMichael Halcrow 		ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
209751ca58dcSMichael Halcrow 					      name, name_size);
209851ca58dcSMichael Halcrow 		rc = ecryptfs_parse_tag_70_packet(plaintext_name,
209951ca58dcSMichael Halcrow 						  plaintext_name_size,
210051ca58dcSMichael Halcrow 						  &packet_size,
210151ca58dcSMichael Halcrow 						  mount_crypt_stat,
210251ca58dcSMichael Halcrow 						  decoded_name,
210351ca58dcSMichael Halcrow 						  decoded_name_size);
210451ca58dcSMichael Halcrow 		if (rc) {
210551ca58dcSMichael Halcrow 			printk(KERN_INFO "%s: Could not parse tag 70 packet "
210651ca58dcSMichael Halcrow 			       "from filename; copying through filename "
210751ca58dcSMichael Halcrow 			       "as-is\n", __func__);
210851ca58dcSMichael Halcrow 			rc = ecryptfs_copy_filename(plaintext_name,
210951ca58dcSMichael Halcrow 						    plaintext_name_size,
211051ca58dcSMichael Halcrow 						    orig_name, orig_name_size);
211151ca58dcSMichael Halcrow 			goto out_free;
211251ca58dcSMichael Halcrow 		}
211351ca58dcSMichael Halcrow 	} else {
211451ca58dcSMichael Halcrow 		rc = ecryptfs_copy_filename(plaintext_name,
211551ca58dcSMichael Halcrow 					    plaintext_name_size,
211651ca58dcSMichael Halcrow 					    name, name_size);
211751ca58dcSMichael Halcrow 		goto out;
211851ca58dcSMichael Halcrow 	}
211951ca58dcSMichael Halcrow out_free:
212051ca58dcSMichael Halcrow 	kfree(decoded_name);
212151ca58dcSMichael Halcrow out:
212251ca58dcSMichael Halcrow 	return rc;
212351ca58dcSMichael Halcrow }
21244a26620dSTyler Hicks 
21254a26620dSTyler Hicks #define ENC_NAME_MAX_BLOCKLEN_8_OR_16	143
21264a26620dSTyler Hicks 
21274a26620dSTyler Hicks int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
21284a26620dSTyler Hicks 			   struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
21294a26620dSTyler Hicks {
21304a26620dSTyler Hicks 	struct blkcipher_desc desc;
21314a26620dSTyler Hicks 	struct mutex *tfm_mutex;
21324a26620dSTyler Hicks 	size_t cipher_blocksize;
21334a26620dSTyler Hicks 	int rc;
21344a26620dSTyler Hicks 
21354a26620dSTyler Hicks 	if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
21364a26620dSTyler Hicks 		(*namelen) = lower_namelen;
21374a26620dSTyler Hicks 		return 0;
21384a26620dSTyler Hicks 	}
21394a26620dSTyler Hicks 
21404a26620dSTyler Hicks 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
21414a26620dSTyler Hicks 			mount_crypt_stat->global_default_fn_cipher_name);
21424a26620dSTyler Hicks 	if (unlikely(rc)) {
21434a26620dSTyler Hicks 		(*namelen) = 0;
21444a26620dSTyler Hicks 		return rc;
21454a26620dSTyler Hicks 	}
21464a26620dSTyler Hicks 
21474a26620dSTyler Hicks 	mutex_lock(tfm_mutex);
21484a26620dSTyler Hicks 	cipher_blocksize = crypto_blkcipher_blocksize(desc.tfm);
21494a26620dSTyler Hicks 	mutex_unlock(tfm_mutex);
21504a26620dSTyler Hicks 
21514a26620dSTyler Hicks 	/* Return an exact amount for the common cases */
21524a26620dSTyler Hicks 	if (lower_namelen == NAME_MAX
21534a26620dSTyler Hicks 	    && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
21544a26620dSTyler Hicks 		(*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
21554a26620dSTyler Hicks 		return 0;
21564a26620dSTyler Hicks 	}
21574a26620dSTyler Hicks 
21584a26620dSTyler Hicks 	/* Return a safe estimate for the uncommon cases */
21594a26620dSTyler Hicks 	(*namelen) = lower_namelen;
21604a26620dSTyler Hicks 	(*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
21614a26620dSTyler Hicks 	/* Since this is the max decoded size, subtract 1 "decoded block" len */
21624a26620dSTyler Hicks 	(*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
21634a26620dSTyler Hicks 	(*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
21644a26620dSTyler Hicks 	(*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
21654a26620dSTyler Hicks 	/* Worst case is that the filename is padded nearly a full block size */
21664a26620dSTyler Hicks 	(*namelen) -= cipher_blocksize - 1;
21674a26620dSTyler Hicks 
21684a26620dSTyler Hicks 	if ((*namelen) < 0)
21694a26620dSTyler Hicks 		(*namelen) = 0;
21704a26620dSTyler Hicks 
21714a26620dSTyler Hicks 	return 0;
21724a26620dSTyler Hicks }
2173