blob: 0a9882edf56222721abbcf7f0b185b5c6a009456 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08006 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07007 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/mount.h>
28#include <linux/pagemap.h>
29#include <linux/random.h>
30#include <linux/compiler.h>
31#include <linux/key.h>
32#include <linux/namei.h>
33#include <linux/crypto.h>
34#include <linux/file.h>
35#include <linux/scatterlist.h>
36#include "ecryptfs_kernel.h"
37
38static int
39ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
40 struct page *dst_page, int dst_offset,
41 struct page *src_page, int src_offset, int size,
42 unsigned char *iv);
43static int
44ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
45 struct page *dst_page, int dst_offset,
46 struct page *src_page, int src_offset, int size,
47 unsigned char *iv);
48
49/**
50 * ecryptfs_to_hex
51 * @dst: Buffer to take hex character representation of contents of
52 * src; must be at least of size (src_size * 2)
53 * @src: Buffer to be converted to a hex string respresentation
54 * @src_size: number of bytes to convert
55 */
56void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
57{
58 int x;
59
60 for (x = 0; x < src_size; x++)
61 sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
62}
63
64/**
65 * ecryptfs_from_hex
66 * @dst: Buffer to take the bytes from src hex; must be at least of
67 * size (src_size / 2)
68 * @src: Buffer to be converted from a hex string respresentation to raw value
69 * @dst_size: size of dst buffer, or number of hex characters pairs to convert
70 */
71void ecryptfs_from_hex(char *dst, char *src, int dst_size)
72{
73 int x;
74 char tmp[3] = { 0, };
75
76 for (x = 0; x < dst_size; x++) {
77 tmp[0] = src[x * 2];
78 tmp[1] = src[x * 2 + 1];
79 dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
80 }
81}
82
83/**
84 * ecryptfs_calculate_md5 - calculates the md5 of @src
85 * @dst: Pointer to 16 bytes of allocated memory
86 * @crypt_stat: Pointer to crypt_stat struct for the current inode
87 * @src: Data to be md5'd
88 * @len: Length of @src
89 *
90 * Uses the allocated crypto context that crypt_stat references to
91 * generate the MD5 sum of the contents of src.
92 */
93static int ecryptfs_calculate_md5(char *dst,
94 struct ecryptfs_crypt_stat *crypt_stat,
95 char *src, int len)
96{
Michael Halcrow237fead2006-10-04 02:16:22 -070097 struct scatterlist sg;
Michael Halcrow565d9722006-10-30 22:07:17 -080098 struct hash_desc desc = {
99 .tfm = crypt_stat->hash_tfm,
100 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
101 };
102 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700103
Michael Halcrow565d9722006-10-30 22:07:17 -0800104 mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700105 sg_init_one(&sg, (u8 *)src, len);
Michael Halcrow565d9722006-10-30 22:07:17 -0800106 if (!desc.tfm) {
107 desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
108 CRYPTO_ALG_ASYNC);
109 if (IS_ERR(desc.tfm)) {
110 rc = PTR_ERR(desc.tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700111 ecryptfs_printk(KERN_ERR, "Error attempting to "
Michael Halcrow565d9722006-10-30 22:07:17 -0800112 "allocate crypto context; rc = [%d]\n",
113 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700114 goto out;
115 }
Michael Halcrow565d9722006-10-30 22:07:17 -0800116 crypt_stat->hash_tfm = desc.tfm;
Michael Halcrow237fead2006-10-04 02:16:22 -0700117 }
Michael Halcrow565d9722006-10-30 22:07:17 -0800118 crypto_hash_init(&desc);
119 crypto_hash_update(&desc, &sg, len);
120 crypto_hash_final(&desc, dst);
121 mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700122out:
123 return rc;
124}
125
Michael Halcrowcd9d67d2007-10-16 01:28:04 -0700126static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
127 char *cipher_name,
128 char *chaining_modifier)
Michael Halcrow8bba0662006-10-30 22:07:18 -0800129{
130 int cipher_name_len = strlen(cipher_name);
131 int chaining_modifier_len = strlen(chaining_modifier);
132 int algified_name_len;
133 int rc;
134
135 algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
136 (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
Michael Halcrow7bd473f2006-11-02 22:06:56 -0800137 if (!(*algified_name)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -0800138 rc = -ENOMEM;
139 goto out;
140 }
141 snprintf((*algified_name), algified_name_len, "%s(%s)",
142 chaining_modifier, cipher_name);
143 rc = 0;
144out:
145 return rc;
146}
147
Michael Halcrow237fead2006-10-04 02:16:22 -0700148/**
149 * ecryptfs_derive_iv
150 * @iv: destination for the derived iv vale
151 * @crypt_stat: Pointer to crypt_stat struct for the current inode
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700152 * @offset: Offset of the extent whose IV we are to derive
Michael Halcrow237fead2006-10-04 02:16:22 -0700153 *
154 * Generate the initialization vector from the given root IV and page
155 * offset.
156 *
157 * Returns zero on success; non-zero on error.
158 */
159static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700160 loff_t offset)
Michael Halcrow237fead2006-10-04 02:16:22 -0700161{
162 int rc = 0;
163 char dst[MD5_DIGEST_SIZE];
164 char src[ECRYPTFS_MAX_IV_BYTES + 16];
165
166 if (unlikely(ecryptfs_verbosity > 0)) {
167 ecryptfs_printk(KERN_DEBUG, "root iv:\n");
168 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
169 }
170 /* TODO: It is probably secure to just cast the least
171 * significant bits of the root IV into an unsigned long and
172 * add the offset to that rather than go through all this
173 * hashing business. -Halcrow */
174 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
175 memset((src + crypt_stat->iv_bytes), 0, 16);
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700176 snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700177 if (unlikely(ecryptfs_verbosity > 0)) {
178 ecryptfs_printk(KERN_DEBUG, "source:\n");
179 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
180 }
181 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
182 (crypt_stat->iv_bytes + 16));
183 if (rc) {
184 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
185 "MD5 while generating IV for a page\n");
186 goto out;
187 }
188 memcpy(iv, dst, crypt_stat->iv_bytes);
189 if (unlikely(ecryptfs_verbosity > 0)) {
190 ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
191 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
192 }
193out:
194 return rc;
195}
196
197/**
198 * ecryptfs_init_crypt_stat
199 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
200 *
201 * Initialize the crypt_stat structure.
202 */
203void
204ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
205{
206 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
Michael Halcrowf4aad162007-10-16 01:27:53 -0700207 INIT_LIST_HEAD(&crypt_stat->keysig_list);
208 mutex_init(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700209 mutex_init(&crypt_stat->cs_mutex);
210 mutex_init(&crypt_stat->cs_tfm_mutex);
Michael Halcrow565d9722006-10-30 22:07:17 -0800211 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800212 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
Michael Halcrow237fead2006-10-04 02:16:22 -0700213}
214
215/**
Michael Halcrowfcd12832007-10-16 01:28:01 -0700216 * ecryptfs_destroy_crypt_stat
Michael Halcrow237fead2006-10-04 02:16:22 -0700217 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
218 *
219 * Releases all memory associated with a crypt_stat struct.
220 */
Michael Halcrowfcd12832007-10-16 01:28:01 -0700221void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700222{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700223 struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
224
Michael Halcrow237fead2006-10-04 02:16:22 -0700225 if (crypt_stat->tfm)
Michael Halcrow8bba0662006-10-30 22:07:18 -0800226 crypto_free_blkcipher(crypt_stat->tfm);
Michael Halcrow565d9722006-10-30 22:07:17 -0800227 if (crypt_stat->hash_tfm)
228 crypto_free_hash(crypt_stat->hash_tfm);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700229 mutex_lock(&crypt_stat->keysig_list_mutex);
230 list_for_each_entry_safe(key_sig, key_sig_tmp,
231 &crypt_stat->keysig_list, crypt_stat_list) {
232 list_del(&key_sig->crypt_stat_list);
233 kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
234 }
235 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700236 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
237}
238
Michael Halcrowfcd12832007-10-16 01:28:01 -0700239void ecryptfs_destroy_mount_crypt_stat(
Michael Halcrow237fead2006-10-04 02:16:22 -0700240 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
241{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700242 struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
243
244 if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
245 return;
246 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
247 list_for_each_entry_safe(auth_tok, auth_tok_tmp,
248 &mount_crypt_stat->global_auth_tok_list,
249 mount_crypt_stat_list) {
250 list_del(&auth_tok->mount_crypt_stat_list);
251 mount_crypt_stat->num_global_auth_toks--;
252 if (auth_tok->global_auth_tok_key
253 && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
254 key_put(auth_tok->global_auth_tok_key);
255 kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
256 }
257 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700258 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
259}
260
261/**
262 * virt_to_scatterlist
263 * @addr: Virtual address
264 * @size: Size of data; should be an even multiple of the block size
265 * @sg: Pointer to scatterlist array; set to NULL to obtain only
266 * the number of scatterlist structs required in array
267 * @sg_size: Max array size
268 *
269 * Fills in a scatterlist array with page references for a passed
270 * virtual address.
271 *
272 * Returns the number of scatterlist structs in array used
273 */
274int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
275 int sg_size)
276{
277 int i = 0;
278 struct page *pg;
279 int offset;
280 int remainder_of_page;
281
282 while (size > 0 && i < sg_size) {
283 pg = virt_to_page(addr);
284 offset = offset_in_page(addr);
285 if (sg) {
Jens Axboe60c74f82007-10-22 19:43:30 +0200286 sg_set_page(&sg[i], pg);
Michael Halcrow237fead2006-10-04 02:16:22 -0700287 sg[i].offset = offset;
288 }
289 remainder_of_page = PAGE_CACHE_SIZE - offset;
290 if (size >= remainder_of_page) {
291 if (sg)
292 sg[i].length = remainder_of_page;
293 addr += remainder_of_page;
294 size -= remainder_of_page;
295 } else {
296 if (sg)
297 sg[i].length = size;
298 addr += size;
299 size = 0;
300 }
301 i++;
302 }
303 if (size > 0)
304 return -ENOMEM;
305 return i;
306}
307
308/**
309 * encrypt_scatterlist
310 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
311 * @dest_sg: Destination of encrypted data
312 * @src_sg: Data to be encrypted
313 * @size: Length of data to be encrypted
314 * @iv: iv to use during encryption
315 *
316 * Returns the number of bytes encrypted; negative value on error
317 */
318static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
319 struct scatterlist *dest_sg,
320 struct scatterlist *src_sg, int size,
321 unsigned char *iv)
322{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800323 struct blkcipher_desc desc = {
324 .tfm = crypt_stat->tfm,
325 .info = iv,
326 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
327 };
Michael Halcrow237fead2006-10-04 02:16:22 -0700328 int rc = 0;
329
330 BUG_ON(!crypt_stat || !crypt_stat->tfm
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800331 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
Michael Halcrow237fead2006-10-04 02:16:22 -0700332 if (unlikely(ecryptfs_verbosity > 0)) {
333 ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
334 crypt_stat->key_size);
335 ecryptfs_dump_hex(crypt_stat->key,
336 crypt_stat->key_size);
337 }
338 /* Consider doing this once, when the file is opened */
339 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800340 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
341 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700342 if (rc) {
343 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
344 rc);
345 mutex_unlock(&crypt_stat->cs_tfm_mutex);
346 rc = -EINVAL;
347 goto out;
348 }
349 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800350 crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700351 mutex_unlock(&crypt_stat->cs_tfm_mutex);
352out:
353 return rc;
354}
355
Michael Halcrow237fead2006-10-04 02:16:22 -0700356/**
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700357 * ecryptfs_lower_offset_for_extent
358 *
359 * Convert an eCryptfs page index into a lower byte offset
360 */
361void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
362 struct ecryptfs_crypt_stat *crypt_stat)
363{
364 (*offset) = ((crypt_stat->extent_size
365 * crypt_stat->num_header_extents_at_front)
366 + (crypt_stat->extent_size * extent_num));
367}
368
369/**
370 * ecryptfs_encrypt_extent
371 * @enc_extent_page: Allocated page into which to encrypt the data in
372 * @page
373 * @crypt_stat: crypt_stat containing cryptographic context for the
374 * encryption operation
375 * @page: Page containing plaintext data extent to encrypt
376 * @extent_offset: Page extent offset for use in generating IV
377 *
378 * Encrypts one extent of data.
379 *
380 * Return zero on success; non-zero otherwise
381 */
382static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
383 struct ecryptfs_crypt_stat *crypt_stat,
384 struct page *page,
385 unsigned long extent_offset)
386{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700387 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700388 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
389 int rc;
390
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700391 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700392 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
393 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
394 (extent_base + extent_offset));
395 if (rc) {
396 ecryptfs_printk(KERN_ERR, "Error attempting to "
397 "derive IV for extent [0x%.16x]; "
398 "rc = [%d]\n", (extent_base + extent_offset),
399 rc);
400 goto out;
401 }
402 if (unlikely(ecryptfs_verbosity > 0)) {
403 ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
404 "with iv:\n");
405 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
406 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
407 "encryption:\n");
408 ecryptfs_dump_hex((char *)
409 (page_address(page)
410 + (extent_offset * crypt_stat->extent_size)),
411 8);
412 }
413 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
414 page, (extent_offset
415 * crypt_stat->extent_size),
416 crypt_stat->extent_size, extent_iv);
417 if (rc < 0) {
418 printk(KERN_ERR "%s: Error attempting to encrypt page with "
419 "page->index = [%ld], extent_offset = [%ld]; "
420 "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
421 rc);
422 goto out;
423 }
424 rc = 0;
425 if (unlikely(ecryptfs_verbosity > 0)) {
426 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
427 "rc = [%d]\n", (extent_base + extent_offset),
428 rc);
429 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
430 "encryption:\n");
431 ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
432 }
433out:
434 return rc;
435}
436
437/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700438 * ecryptfs_encrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700439 * @page: Page mapped from the eCryptfs inode for the file; contains
440 * decrypted content that needs to be encrypted (to a temporary
441 * page; not in place) and written out to the lower file
Michael Halcrow237fead2006-10-04 02:16:22 -0700442 *
443 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
444 * that eCryptfs pages may straddle the lower pages -- for instance,
445 * if the file was created on a machine with an 8K page size
446 * (resulting in an 8K header), and then the file is copied onto a
447 * host with a 32K page size, then when reading page 0 of the eCryptfs
448 * file, 24K of page 0 of the lower file will be read and decrypted,
449 * and then 8K of page 1 of the lower file will be read and decrypted.
450 *
Michael Halcrow237fead2006-10-04 02:16:22 -0700451 * Returns zero on success; negative on error
452 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700453int ecryptfs_encrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700454{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700455 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700456 struct ecryptfs_crypt_stat *crypt_stat;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700457 char *enc_extent_virt = NULL;
458 struct page *enc_extent_page;
459 loff_t extent_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700460 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700461
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700462 ecryptfs_inode = page->mapping->host;
463 crypt_stat =
464 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800465 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700466 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
467 0, PAGE_CACHE_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700468 if (rc)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700469 printk(KERN_ERR "%s: Error attempting to copy "
470 "page at index [%ld]\n", __FUNCTION__,
471 page->index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700472 goto out;
473 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700474 enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
475 if (!enc_extent_virt) {
476 rc = -ENOMEM;
477 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
478 "encrypted extent\n");
479 goto out;
480 }
481 enc_extent_page = virt_to_page(enc_extent_virt);
482 for (extent_offset = 0;
483 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
484 extent_offset++) {
485 loff_t offset;
486
487 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
488 extent_offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700489 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700490 printk(KERN_ERR "%s: Error encrypting extent; "
491 "rc = [%d]\n", __FUNCTION__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700492 goto out;
493 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700494 ecryptfs_lower_offset_for_extent(
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700495 &offset, ((((loff_t)page->index)
496 * (PAGE_CACHE_SIZE
497 / crypt_stat->extent_size))
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700498 + extent_offset), crypt_stat);
499 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
500 offset, crypt_stat->extent_size);
501 if (rc) {
502 ecryptfs_printk(KERN_ERR, "Error attempting "
503 "to write lower page; rc = [%d]"
504 "\n", rc);
505 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700506 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700507 extent_offset++;
508 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700509out:
510 kfree(enc_extent_virt);
511 return rc;
512}
513
514static int ecryptfs_decrypt_extent(struct page *page,
515 struct ecryptfs_crypt_stat *crypt_stat,
516 struct page *enc_extent_page,
517 unsigned long extent_offset)
518{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700519 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700520 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
521 int rc;
522
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700523 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700524 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
525 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
526 (extent_base + extent_offset));
Michael Halcrow237fead2006-10-04 02:16:22 -0700527 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700528 ecryptfs_printk(KERN_ERR, "Error attempting to "
529 "derive IV for extent [0x%.16x]; "
530 "rc = [%d]\n", (extent_base + extent_offset),
531 rc);
532 goto out;
533 }
534 if (unlikely(ecryptfs_verbosity > 0)) {
535 ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
536 "with iv:\n");
537 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
538 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
539 "decryption:\n");
540 ecryptfs_dump_hex((char *)
541 (page_address(enc_extent_page)
542 + (extent_offset * crypt_stat->extent_size)),
543 8);
544 }
545 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
546 (extent_offset
547 * crypt_stat->extent_size),
548 enc_extent_page, 0,
549 crypt_stat->extent_size, extent_iv);
550 if (rc < 0) {
551 printk(KERN_ERR "%s: Error attempting to decrypt to page with "
552 "page->index = [%ld], extent_offset = [%ld]; "
553 "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
554 rc);
555 goto out;
556 }
557 rc = 0;
558 if (unlikely(ecryptfs_verbosity > 0)) {
559 ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
560 "rc = [%d]\n", (extent_base + extent_offset),
561 rc);
562 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
563 "decryption:\n");
564 ecryptfs_dump_hex((char *)(page_address(page)
565 + (extent_offset
566 * crypt_stat->extent_size)), 8);
Michael Halcrow237fead2006-10-04 02:16:22 -0700567 }
568out:
569 return rc;
570}
571
572/**
573 * ecryptfs_decrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700574 * @page: Page mapped from the eCryptfs inode for the file; data read
575 * and decrypted from the lower file will be written into this
576 * page
Michael Halcrow237fead2006-10-04 02:16:22 -0700577 *
578 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
579 * that eCryptfs pages may straddle the lower pages -- for instance,
580 * if the file was created on a machine with an 8K page size
581 * (resulting in an 8K header), and then the file is copied onto a
582 * host with a 32K page size, then when reading page 0 of the eCryptfs
583 * file, 24K of page 0 of the lower file will be read and decrypted,
584 * and then 8K of page 1 of the lower file will be read and decrypted.
585 *
586 * Returns zero on success; negative on error
587 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700588int ecryptfs_decrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700589{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700590 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700591 struct ecryptfs_crypt_stat *crypt_stat;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700592 char *enc_extent_virt = NULL;
593 struct page *enc_extent_page;
594 unsigned long extent_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700595 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700596
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700597 ecryptfs_inode = page->mapping->host;
598 crypt_stat =
599 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800600 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700601 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
602 PAGE_CACHE_SIZE,
603 ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700604 if (rc)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700605 printk(KERN_ERR "%s: Error attempting to copy "
606 "page at index [%ld]\n", __FUNCTION__,
607 page->index);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700608 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700609 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700610 enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
611 if (!enc_extent_virt) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700612 rc = -ENOMEM;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700613 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
614 "encrypted extent\n");
Michael Halcrow16a72c42007-10-16 01:28:14 -0700615 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700616 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700617 enc_extent_page = virt_to_page(enc_extent_virt);
618 for (extent_offset = 0;
619 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
620 extent_offset++) {
621 loff_t offset;
622
623 ecryptfs_lower_offset_for_extent(
624 &offset, ((page->index * (PAGE_CACHE_SIZE
625 / crypt_stat->extent_size))
626 + extent_offset), crypt_stat);
627 rc = ecryptfs_read_lower(enc_extent_virt, offset,
628 crypt_stat->extent_size,
629 ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700630 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700631 ecryptfs_printk(KERN_ERR, "Error attempting "
632 "to read lower page; rc = [%d]"
633 "\n", rc);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700634 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700635 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700636 rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
637 extent_offset);
638 if (rc) {
639 printk(KERN_ERR "%s: Error encrypting extent; "
640 "rc = [%d]\n", __FUNCTION__, rc);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700641 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700642 }
643 extent_offset++;
644 }
645out:
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700646 kfree(enc_extent_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -0700647 return rc;
648}
649
650/**
651 * decrypt_scatterlist
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700652 * @crypt_stat: Cryptographic context
653 * @dest_sg: The destination scatterlist to decrypt into
654 * @src_sg: The source scatterlist to decrypt from
655 * @size: The number of bytes to decrypt
656 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700657 *
658 * Returns the number of bytes decrypted; negative value on error
659 */
660static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
661 struct scatterlist *dest_sg,
662 struct scatterlist *src_sg, int size,
663 unsigned char *iv)
664{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800665 struct blkcipher_desc desc = {
666 .tfm = crypt_stat->tfm,
667 .info = iv,
668 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
669 };
Michael Halcrow237fead2006-10-04 02:16:22 -0700670 int rc = 0;
671
672 /* Consider doing this once, when the file is opened */
673 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800674 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
675 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700676 if (rc) {
677 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
678 rc);
679 mutex_unlock(&crypt_stat->cs_tfm_mutex);
680 rc = -EINVAL;
681 goto out;
682 }
683 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800684 rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700685 mutex_unlock(&crypt_stat->cs_tfm_mutex);
686 if (rc) {
687 ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
688 rc);
689 goto out;
690 }
691 rc = size;
692out:
693 return rc;
694}
695
696/**
697 * ecryptfs_encrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700698 * @crypt_stat: The cryptographic context
699 * @dst_page: The page to encrypt into
700 * @dst_offset: The offset in the page to encrypt into
701 * @src_page: The page to encrypt from
702 * @src_offset: The offset in the page to encrypt from
703 * @size: The number of bytes to encrypt
704 * @iv: The initialization vector to use for the encryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700705 *
706 * Returns the number of bytes encrypted
707 */
708static int
709ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
710 struct page *dst_page, int dst_offset,
711 struct page *src_page, int src_offset, int size,
712 unsigned char *iv)
713{
714 struct scatterlist src_sg, dst_sg;
715
Jens Axboe60c74f82007-10-22 19:43:30 +0200716 sg_init_table(&src_sg, 1);
717 sg_init_table(&dst_sg, 1);
718
719 sg_set_page(&src_sg, src_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700720 src_sg.offset = src_offset;
721 src_sg.length = size;
Jens Axboe60c74f82007-10-22 19:43:30 +0200722 sg_set_page(&dst_sg, dst_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700723 dst_sg.offset = dst_offset;
724 dst_sg.length = size;
725 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
726}
727
728/**
729 * ecryptfs_decrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700730 * @crypt_stat: The cryptographic context
731 * @dst_page: The page to decrypt into
732 * @dst_offset: The offset in the page to decrypt into
733 * @src_page: The page to decrypt from
734 * @src_offset: The offset in the page to decrypt from
735 * @size: The number of bytes to decrypt
736 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700737 *
738 * Returns the number of bytes decrypted
739 */
740static int
741ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
742 struct page *dst_page, int dst_offset,
743 struct page *src_page, int src_offset, int size,
744 unsigned char *iv)
745{
746 struct scatterlist src_sg, dst_sg;
747
Jens Axboe60c74f82007-10-22 19:43:30 +0200748 sg_init_table(&src_sg, 1);
749 sg_init_table(&dst_sg, 1);
750
751 sg_set_page(&src_sg, src_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700752 src_sg.offset = src_offset;
753 src_sg.length = size;
Jens Axboe60c74f82007-10-22 19:43:30 +0200754 sg_set_page(&dst_sg, dst_page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700755 dst_sg.offset = dst_offset;
756 dst_sg.length = size;
757 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
758}
759
760#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
761
762/**
763 * ecryptfs_init_crypt_ctx
764 * @crypt_stat: Uninitilized crypt stats structure
765 *
766 * Initialize the crypto context.
767 *
768 * TODO: Performance: Keep a cache of initialized cipher contexts;
769 * only init if needed
770 */
771int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
772{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800773 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -0700774 int rc = -EINVAL;
775
776 if (!crypt_stat->cipher) {
777 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
778 goto out;
779 }
780 ecryptfs_printk(KERN_DEBUG,
781 "Initializing cipher [%s]; strlen = [%d]; "
782 "key_size_bits = [%d]\n",
783 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
784 crypt_stat->key_size << 3);
785 if (crypt_stat->tfm) {
786 rc = 0;
787 goto out;
788 }
789 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800790 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
791 crypt_stat->cipher, "cbc");
792 if (rc)
793 goto out;
794 crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
795 CRYPTO_ALG_ASYNC);
796 kfree(full_alg_name);
Akinobu Mitade887772006-11-28 12:29:49 -0800797 if (IS_ERR(crypt_stat->tfm)) {
798 rc = PTR_ERR(crypt_stat->tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700799 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
800 "Error initializing cipher [%s]\n",
801 crypt_stat->cipher);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800802 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700803 goto out;
804 }
Herbert Xuf1ddcaf2007-01-27 10:05:15 +1100805 crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800806 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700807 rc = 0;
808out:
809 return rc;
810}
811
812static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
813{
814 int extent_size_tmp;
815
816 crypt_stat->extent_mask = 0xFFFFFFFF;
817 crypt_stat->extent_shift = 0;
818 if (crypt_stat->extent_size == 0)
819 return;
820 extent_size_tmp = crypt_stat->extent_size;
821 while ((extent_size_tmp & 0x01) == 0) {
822 extent_size_tmp >>= 1;
823 crypt_stat->extent_mask <<= 1;
824 crypt_stat->extent_shift++;
825 }
826}
827
828void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
829{
830 /* Default values; may be overwritten as we are parsing the
831 * packets. */
832 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
833 set_extent_mask_and_shift(crypt_stat);
834 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800835 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
836 crypt_stat->num_header_extents_at_front = 0;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700837 else {
838 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
839 crypt_stat->num_header_extents_at_front =
840 (ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
841 / crypt_stat->extent_size);
842 else
843 crypt_stat->num_header_extents_at_front =
844 (PAGE_CACHE_SIZE / crypt_stat->extent_size);
845 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700846}
847
848/**
849 * ecryptfs_compute_root_iv
850 * @crypt_stats
851 *
852 * On error, sets the root IV to all 0's.
853 */
854int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
855{
856 int rc = 0;
857 char dst[MD5_DIGEST_SIZE];
858
859 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
860 BUG_ON(crypt_stat->iv_bytes <= 0);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800861 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700862 rc = -EINVAL;
863 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
864 "cannot generate root IV\n");
865 goto out;
866 }
867 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
868 crypt_stat->key_size);
869 if (rc) {
870 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
871 "MD5 while generating root IV\n");
872 goto out;
873 }
874 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
875out:
876 if (rc) {
877 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800878 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
Michael Halcrow237fead2006-10-04 02:16:22 -0700879 }
880 return rc;
881}
882
883static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
884{
885 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800886 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700887 ecryptfs_compute_root_iv(crypt_stat);
888 if (unlikely(ecryptfs_verbosity > 0)) {
889 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
890 ecryptfs_dump_hex(crypt_stat->key,
891 crypt_stat->key_size);
892 }
893}
894
895/**
Michael Halcrow17398952007-02-12 00:53:45 -0800896 * ecryptfs_copy_mount_wide_flags_to_inode_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700897 * @crypt_stat: The inode's cryptographic context
898 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow17398952007-02-12 00:53:45 -0800899 *
900 * This function propagates the mount-wide flags to individual inode
901 * flags.
902 */
903static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
904 struct ecryptfs_crypt_stat *crypt_stat,
905 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
906{
907 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
908 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
909 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
910 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
911}
912
Michael Halcrowf4aad162007-10-16 01:27:53 -0700913static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
914 struct ecryptfs_crypt_stat *crypt_stat,
915 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
916{
917 struct ecryptfs_global_auth_tok *global_auth_tok;
918 int rc = 0;
919
920 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
921 list_for_each_entry(global_auth_tok,
922 &mount_crypt_stat->global_auth_tok_list,
923 mount_crypt_stat_list) {
924 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
925 if (rc) {
926 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
927 mutex_unlock(
928 &mount_crypt_stat->global_auth_tok_list_mutex);
929 goto out;
930 }
931 }
932 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
933out:
934 return rc;
935}
936
Michael Halcrow17398952007-02-12 00:53:45 -0800937/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700938 * ecryptfs_set_default_crypt_stat_vals
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700939 * @crypt_stat: The inode's cryptographic context
940 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -0700941 *
942 * Default values in the event that policy does not override them.
943 */
944static void ecryptfs_set_default_crypt_stat_vals(
945 struct ecryptfs_crypt_stat *crypt_stat,
946 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
947{
Michael Halcrow17398952007-02-12 00:53:45 -0800948 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
949 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700950 ecryptfs_set_default_sizes(crypt_stat);
951 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
952 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800953 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
Michael Halcrow237fead2006-10-04 02:16:22 -0700954 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
955 crypt_stat->mount_crypt_stat = mount_crypt_stat;
956}
957
958/**
959 * ecryptfs_new_file_context
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700960 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -0700961 *
962 * If the crypto context for the file has not yet been established,
963 * this is where we do that. Establishing a new crypto context
964 * involves the following decisions:
965 * - What cipher to use?
966 * - What set of authentication tokens to use?
967 * Here we just worry about getting enough information into the
968 * authentication tokens so that we know that they are available.
969 * We associate the available authentication tokens with the new file
970 * via the set of signatures in the crypt_stat struct. Later, when
971 * the headers are actually written out, we may again defer to
972 * userspace to perform the encryption of the session key; for the
973 * foreseeable future, this will be the case with public key packets.
974 *
975 * Returns zero on success; non-zero otherwise
976 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700977int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
978{
Michael Halcrow237fead2006-10-04 02:16:22 -0700979 struct ecryptfs_crypt_stat *crypt_stat =
980 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
981 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
982 &ecryptfs_superblock_to_private(
983 ecryptfs_dentry->d_sb)->mount_crypt_stat;
984 int cipher_name_len;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700985 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700986
987 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
Michael Halcrowaf655dc2007-10-16 01:28:00 -0700988 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700989 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
990 mount_crypt_stat);
991 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
992 mount_crypt_stat);
993 if (rc) {
994 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
995 "to the inode key sigs; rc = [%d]\n", rc);
996 goto out;
997 }
998 cipher_name_len =
999 strlen(mount_crypt_stat->global_default_cipher_name);
1000 memcpy(crypt_stat->cipher,
1001 mount_crypt_stat->global_default_cipher_name,
1002 cipher_name_len);
1003 crypt_stat->cipher[cipher_name_len] = '\0';
1004 crypt_stat->key_size =
1005 mount_crypt_stat->global_default_cipher_key_size;
1006 ecryptfs_generate_new_key(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001007 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1008 if (rc)
1009 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1010 "context for cipher [%s]: rc = [%d]\n",
1011 crypt_stat->cipher, rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001012out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001013 return rc;
1014}
1015
1016/**
1017 * contains_ecryptfs_marker - check for the ecryptfs marker
1018 * @data: The data block in which to check
1019 *
1020 * Returns one if marker found; zero if not found
1021 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001022static int contains_ecryptfs_marker(char *data)
Michael Halcrow237fead2006-10-04 02:16:22 -07001023{
1024 u32 m_1, m_2;
1025
1026 memcpy(&m_1, data, 4);
1027 m_1 = be32_to_cpu(m_1);
1028 memcpy(&m_2, (data + 4), 4);
1029 m_2 = be32_to_cpu(m_2);
1030 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1031 return 1;
1032 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1033 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1034 MAGIC_ECRYPTFS_MARKER);
1035 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1036 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1037 return 0;
1038}
1039
1040struct ecryptfs_flag_map_elem {
1041 u32 file_flag;
1042 u32 local_flag;
1043};
1044
1045/* Add support for additional flags by adding elements here. */
1046static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1047 {0x00000001, ECRYPTFS_ENABLE_HMAC},
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001048 {0x00000002, ECRYPTFS_ENCRYPTED},
1049 {0x00000004, ECRYPTFS_METADATA_IN_XATTR}
Michael Halcrow237fead2006-10-04 02:16:22 -07001050};
1051
1052/**
1053 * ecryptfs_process_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001054 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001055 * @page_virt: Source data to be parsed
1056 * @bytes_read: Updated with the number of bytes read
1057 *
1058 * Returns zero on success; non-zero if the flag set is invalid
1059 */
1060static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1061 char *page_virt, int *bytes_read)
1062{
1063 int rc = 0;
1064 int i;
1065 u32 flags;
1066
1067 memcpy(&flags, page_virt, 4);
1068 flags = be32_to_cpu(flags);
1069 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1070 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1071 if (flags & ecryptfs_flag_map[i].file_flag) {
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001072 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
Michael Halcrow237fead2006-10-04 02:16:22 -07001073 } else
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001074 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
Michael Halcrow237fead2006-10-04 02:16:22 -07001075 /* Version is in top 8 bits of the 32-bit flag vector */
1076 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1077 (*bytes_read) = 4;
1078 return rc;
1079}
1080
1081/**
1082 * write_ecryptfs_marker
1083 * @page_virt: The pointer to in a page to begin writing the marker
1084 * @written: Number of bytes written
1085 *
1086 * Marker = 0x3c81b7f5
1087 */
1088static void write_ecryptfs_marker(char *page_virt, size_t *written)
1089{
1090 u32 m_1, m_2;
1091
1092 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1093 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1094 m_1 = cpu_to_be32(m_1);
1095 memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1096 m_2 = cpu_to_be32(m_2);
1097 memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
1098 (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1099 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1100}
1101
1102static void
1103write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1104 size_t *written)
1105{
1106 u32 flags = 0;
1107 int i;
1108
1109 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1110 / sizeof(struct ecryptfs_flag_map_elem))); i++)
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001111 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
Michael Halcrow237fead2006-10-04 02:16:22 -07001112 flags |= ecryptfs_flag_map[i].file_flag;
1113 /* Version is in top 8 bits of the 32-bit flag vector */
1114 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1115 flags = cpu_to_be32(flags);
1116 memcpy(page_virt, &flags, 4);
1117 (*written) = 4;
1118}
1119
1120struct ecryptfs_cipher_code_str_map_elem {
1121 char cipher_str[16];
1122 u16 cipher_code;
1123};
1124
1125/* Add support for additional ciphers by adding elements here. The
1126 * cipher_code is whatever OpenPGP applicatoins use to identify the
1127 * ciphers. List in order of probability. */
1128static struct ecryptfs_cipher_code_str_map_elem
1129ecryptfs_cipher_code_str_map[] = {
1130 {"aes",RFC2440_CIPHER_AES_128 },
1131 {"blowfish", RFC2440_CIPHER_BLOWFISH},
1132 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
1133 {"cast5", RFC2440_CIPHER_CAST_5},
1134 {"twofish", RFC2440_CIPHER_TWOFISH},
1135 {"cast6", RFC2440_CIPHER_CAST_6},
1136 {"aes", RFC2440_CIPHER_AES_192},
1137 {"aes", RFC2440_CIPHER_AES_256}
1138};
1139
1140/**
1141 * ecryptfs_code_for_cipher_string
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001142 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001143 *
1144 * Returns zero on no match, or the cipher code on match
1145 */
1146u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
1147{
1148 int i;
1149 u16 code = 0;
1150 struct ecryptfs_cipher_code_str_map_elem *map =
1151 ecryptfs_cipher_code_str_map;
1152
1153 if (strcmp(crypt_stat->cipher, "aes") == 0) {
1154 switch (crypt_stat->key_size) {
1155 case 16:
1156 code = RFC2440_CIPHER_AES_128;
1157 break;
1158 case 24:
1159 code = RFC2440_CIPHER_AES_192;
1160 break;
1161 case 32:
1162 code = RFC2440_CIPHER_AES_256;
1163 }
1164 } else {
1165 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1166 if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
1167 code = map[i].cipher_code;
1168 break;
1169 }
1170 }
1171 return code;
1172}
1173
1174/**
1175 * ecryptfs_cipher_code_to_string
1176 * @str: Destination to write out the cipher name
1177 * @cipher_code: The code to convert to cipher name string
1178 *
1179 * Returns zero on success
1180 */
1181int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
1182{
1183 int rc = 0;
1184 int i;
1185
1186 str[0] = '\0';
1187 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1188 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1189 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1190 if (str[0] == '\0') {
1191 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1192 "[%d]\n", cipher_code);
1193 rc = -EINVAL;
1194 }
1195 return rc;
1196}
1197
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001198int ecryptfs_read_and_validate_header_region(char *data,
1199 struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001200{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001201 struct ecryptfs_crypt_stat *crypt_stat =
1202 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001203 int rc;
1204
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001205 rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
1206 ecryptfs_inode);
1207 if (rc) {
1208 printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
1209 __FUNCTION__, rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001210 goto out;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001211 }
1212 if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001213 rc = -EINVAL;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001214 ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n");
1215 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001216out:
1217 return rc;
1218}
1219
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001220void
1221ecryptfs_write_header_metadata(char *virt,
1222 struct ecryptfs_crypt_stat *crypt_stat,
1223 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001224{
1225 u32 header_extent_size;
1226 u16 num_header_extents_at_front;
1227
Michael Halcrow45eaab72007-10-16 01:28:05 -07001228 header_extent_size = (u32)crypt_stat->extent_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001229 num_header_extents_at_front =
1230 (u16)crypt_stat->num_header_extents_at_front;
1231 header_extent_size = cpu_to_be32(header_extent_size);
1232 memcpy(virt, &header_extent_size, 4);
1233 virt += 4;
1234 num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
1235 memcpy(virt, &num_header_extents_at_front, 2);
1236 (*written) = 6;
1237}
1238
1239struct kmem_cache *ecryptfs_header_cache_0;
1240struct kmem_cache *ecryptfs_header_cache_1;
1241struct kmem_cache *ecryptfs_header_cache_2;
1242
1243/**
1244 * ecryptfs_write_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001245 * @page_virt: The virtual address to write the headers to
1246 * @size: Set to the number of bytes written by this function
1247 * @crypt_stat: The cryptographic context
1248 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001249 *
1250 * Format version: 1
1251 *
1252 * Header Extent:
1253 * Octets 0-7: Unencrypted file size (big-endian)
1254 * Octets 8-15: eCryptfs special marker
1255 * Octets 16-19: Flags
1256 * Octet 16: File format version number (between 0 and 255)
1257 * Octets 17-18: Reserved
1258 * Octet 19: Bit 1 (lsb): Reserved
1259 * Bit 2: Encrypted?
1260 * Bits 3-8: Reserved
1261 * Octets 20-23: Header extent size (big-endian)
1262 * Octets 24-25: Number of header extents at front of file
1263 * (big-endian)
1264 * Octet 26: Begin RFC 2440 authentication token packet set
1265 * Data Extent 0:
1266 * Lower data (CBC encrypted)
1267 * Data Extent 1:
1268 * Lower data (CBC encrypted)
1269 * ...
1270 *
1271 * Returns zero on success
1272 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001273static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
1274 struct ecryptfs_crypt_stat *crypt_stat,
1275 struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001276{
1277 int rc;
1278 size_t written;
1279 size_t offset;
1280
1281 offset = ECRYPTFS_FILE_SIZE_BYTES;
1282 write_ecryptfs_marker((page_virt + offset), &written);
1283 offset += written;
1284 write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1285 offset += written;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001286 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1287 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001288 offset += written;
1289 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1290 ecryptfs_dentry, &written,
1291 PAGE_CACHE_SIZE - offset);
1292 if (rc)
1293 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1294 "set; rc = [%d]\n", rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001295 if (size) {
1296 offset += written;
1297 *size = offset;
1298 }
1299 return rc;
1300}
1301
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001302static int
1303ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001304 struct dentry *ecryptfs_dentry,
1305 char *page_virt)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001306{
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001307 int current_header_page;
1308 int header_pages;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001309 int rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001310
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001311 rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, page_virt,
1312 0, PAGE_CACHE_SIZE);
1313 if (rc) {
1314 printk(KERN_ERR "%s: Error attempting to write header "
1315 "information to lower file; rc = [%d]\n", __FUNCTION__,
1316 rc);
Michael Halcrow70456602007-02-12 00:53:48 -08001317 goto out;
1318 }
Michael Halcrow45eaab72007-10-16 01:28:05 -07001319 header_pages = ((crypt_stat->extent_size
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001320 * crypt_stat->num_header_extents_at_front)
1321 / PAGE_CACHE_SIZE);
1322 memset(page_virt, 0, PAGE_CACHE_SIZE);
1323 current_header_page = 1;
1324 while (current_header_page < header_pages) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001325 loff_t offset;
1326
Michael Halcrowd6a13c12007-10-16 01:28:12 -07001327 offset = (((loff_t)current_header_page) << PAGE_CACHE_SHIFT);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001328 if ((rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode,
1329 page_virt, offset,
1330 PAGE_CACHE_SIZE))) {
1331 printk(KERN_ERR "%s: Error attempting to write header "
1332 "information to lower file; rc = [%d]\n",
1333 __FUNCTION__, rc);
Michael Halcrow70456602007-02-12 00:53:48 -08001334 goto out;
1335 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001336 current_header_page++;
1337 }
Michael Halcrow70456602007-02-12 00:53:48 -08001338out:
1339 return rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001340}
1341
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001342static int
1343ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1344 struct ecryptfs_crypt_stat *crypt_stat,
1345 char *page_virt, size_t size)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001346{
1347 int rc;
1348
1349 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1350 size, 0);
Michael Halcrow237fead2006-10-04 02:16:22 -07001351 return rc;
1352}
1353
1354/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001355 * ecryptfs_write_metadata
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001356 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001357 *
1358 * Write the file headers out. This will likely involve a userspace
1359 * callout, in which the session key is encrypted with one or more
1360 * public keys and/or the passphrase necessary to do the encryption is
1361 * retrieved via a prompt. Exactly what happens at this point should
1362 * be policy-dependent.
1363 *
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001364 * TODO: Support header information spanning multiple pages
1365 *
Michael Halcrow237fead2006-10-04 02:16:22 -07001366 * Returns zero on success; non-zero on error
1367 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001368int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001369{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001370 struct ecryptfs_crypt_stat *crypt_stat =
1371 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001372 char *page_virt;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001373 size_t size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001374 int rc = 0;
1375
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001376 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1377 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001378 printk(KERN_ERR "Key is invalid; bailing out\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001379 rc = -EINVAL;
1380 goto out;
1381 }
1382 } else {
1383 rc = -EINVAL;
1384 ecryptfs_printk(KERN_WARNING,
1385 "Called with crypt_stat->encrypted == 0\n");
1386 goto out;
1387 }
1388 /* Released in this function */
Robert P. J. Dayc3762222007-02-10 01:45:03 -08001389 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001390 if (!page_virt) {
1391 ecryptfs_printk(KERN_ERR, "Out of memory\n");
1392 rc = -ENOMEM;
1393 goto out;
1394 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001395 rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat,
1396 ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -07001397 if (unlikely(rc)) {
1398 ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
1399 memset(page_virt, 0, PAGE_CACHE_SIZE);
1400 goto out_free;
1401 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001402 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1403 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
1404 crypt_stat, page_virt,
1405 size);
1406 else
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001407 rc = ecryptfs_write_metadata_to_contents(crypt_stat,
1408 ecryptfs_dentry,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001409 page_virt);
1410 if (rc) {
1411 printk(KERN_ERR "Error writing metadata out to lower file; "
1412 "rc = [%d]\n", rc);
1413 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001414 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001415out_free:
1416 kmem_cache_free(ecryptfs_header_cache_0, page_virt);
1417out:
1418 return rc;
1419}
1420
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001421#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1422#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
Michael Halcrow237fead2006-10-04 02:16:22 -07001423static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001424 char *virt, int *bytes_read,
1425 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001426{
1427 int rc = 0;
1428 u32 header_extent_size;
1429 u16 num_header_extents_at_front;
1430
Michael Halcrowecbdc932007-10-16 01:28:14 -07001431 memcpy(&header_extent_size, virt, sizeof(u32));
Michael Halcrow237fead2006-10-04 02:16:22 -07001432 header_extent_size = be32_to_cpu(header_extent_size);
Michael Halcrowecbdc932007-10-16 01:28:14 -07001433 virt += sizeof(u32);
1434 memcpy(&num_header_extents_at_front, virt, sizeof(u16));
Michael Halcrow237fead2006-10-04 02:16:22 -07001435 num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
Michael Halcrow237fead2006-10-04 02:16:22 -07001436 crypt_stat->num_header_extents_at_front =
1437 (int)num_header_extents_at_front;
Michael Halcrow45eaab72007-10-16 01:28:05 -07001438 (*bytes_read) = (sizeof(u32) + sizeof(u16));
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001439 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
Michael Halcrow45eaab72007-10-16 01:28:05 -07001440 && ((crypt_stat->extent_size
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001441 * crypt_stat->num_header_extents_at_front)
1442 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001443 rc = -EINVAL;
Michael Halcrow45eaab72007-10-16 01:28:05 -07001444 printk(KERN_WARNING "Invalid number of header extents: [%zd]\n",
1445 crypt_stat->num_header_extents_at_front);
Michael Halcrow237fead2006-10-04 02:16:22 -07001446 }
1447 return rc;
1448}
1449
1450/**
1451 * set_default_header_data
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001452 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001453 *
1454 * For version 0 file format; this function is only for backwards
1455 * compatibility for files created with the prior versions of
1456 * eCryptfs.
1457 */
1458static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1459{
Michael Halcrow45eaab72007-10-16 01:28:05 -07001460 crypt_stat->num_header_extents_at_front = 2;
Michael Halcrow237fead2006-10-04 02:16:22 -07001461}
1462
1463/**
1464 * ecryptfs_read_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001465 * @page_virt: The virtual address into which to read the headers
1466 * @crypt_stat: The cryptographic context
1467 * @ecryptfs_dentry: The eCryptfs dentry
1468 * @validate_header_size: Whether to validate the header size while reading
Michael Halcrow237fead2006-10-04 02:16:22 -07001469 *
1470 * Read/parse the header data. The header format is detailed in the
1471 * comment block for the ecryptfs_write_headers_virt() function.
1472 *
1473 * Returns zero on success
1474 */
1475static int ecryptfs_read_headers_virt(char *page_virt,
1476 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001477 struct dentry *ecryptfs_dentry,
1478 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001479{
1480 int rc = 0;
1481 int offset;
1482 int bytes_read;
1483
1484 ecryptfs_set_default_sizes(crypt_stat);
1485 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1486 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1487 offset = ECRYPTFS_FILE_SIZE_BYTES;
1488 rc = contains_ecryptfs_marker(page_virt + offset);
1489 if (rc == 0) {
1490 rc = -EINVAL;
1491 goto out;
1492 }
1493 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1494 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1495 &bytes_read);
1496 if (rc) {
1497 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1498 goto out;
1499 }
1500 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1501 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1502 "file version [%d] is supported by this "
1503 "version of eCryptfs\n",
1504 crypt_stat->file_version,
1505 ECRYPTFS_SUPPORTED_FILE_VERSION);
1506 rc = -EINVAL;
1507 goto out;
1508 }
1509 offset += bytes_read;
1510 if (crypt_stat->file_version >= 1) {
1511 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001512 &bytes_read, validate_header_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001513 if (rc) {
1514 ecryptfs_printk(KERN_WARNING, "Error reading header "
1515 "metadata; rc = [%d]\n", rc);
1516 }
1517 offset += bytes_read;
1518 } else
1519 set_default_header_data(crypt_stat);
1520 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1521 ecryptfs_dentry);
1522out:
1523 return rc;
1524}
1525
1526/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001527 * ecryptfs_read_xattr_region
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001528 * @page_virt: The vitual address into which to read the xattr data
Michael Halcrow2ed92552007-10-16 01:28:10 -07001529 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001530 *
1531 * Attempts to read the crypto metadata from the extended attribute
1532 * region of the lower file.
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001533 *
1534 * Returns zero on success; non-zero on error
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001535 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001536int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001537{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001538 struct dentry *lower_dentry =
1539 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001540 ssize_t size;
1541 int rc = 0;
1542
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001543 size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1544 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001545 if (size < 0) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001546 printk(KERN_ERR "Error attempting to read the [%s] "
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001547 "xattr from the lower file; return value = [%zd]\n",
1548 ECRYPTFS_XATTR_NAME, size);
1549 rc = -EINVAL;
1550 goto out;
1551 }
1552out:
1553 return rc;
1554}
1555
1556int ecryptfs_read_and_validate_xattr_region(char *page_virt,
1557 struct dentry *ecryptfs_dentry)
1558{
1559 int rc;
1560
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001561 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001562 if (rc)
1563 goto out;
1564 if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
1565 printk(KERN_WARNING "Valid data found in [%s] xattr, but "
1566 "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
1567 rc = -EINVAL;
1568 }
1569out:
1570 return rc;
1571}
1572
1573/**
1574 * ecryptfs_read_metadata
1575 *
1576 * Common entry point for reading file metadata. From here, we could
1577 * retrieve the header information from the header region of the file,
1578 * the xattr region of the file, or some other repostory that is
1579 * stored separately from the file itself. The current implementation
1580 * supports retrieving the metadata information from the file contents
1581 * and from the xattr region.
Michael Halcrow237fead2006-10-04 02:16:22 -07001582 *
1583 * Returns zero if valid headers found and parsed; non-zero otherwise
1584 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001585int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001586{
1587 int rc = 0;
1588 char *page_virt = NULL;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001589 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -07001590 struct ecryptfs_crypt_stat *crypt_stat =
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001591 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001592 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1593 &ecryptfs_superblock_to_private(
1594 ecryptfs_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001595
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001596 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1597 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001598 /* Read the first page from the underlying file */
Christoph Lameterf7267c02006-12-06 20:33:15 -08001599 page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001600 if (!page_virt) {
1601 rc = -ENOMEM;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001602 printk(KERN_ERR "%s: Unable to allocate page_virt\n",
1603 __FUNCTION__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001604 goto out;
1605 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001606 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1607 ecryptfs_inode);
1608 if (!rc)
1609 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1610 ecryptfs_dentry,
1611 ECRYPTFS_VALIDATE_HEADER_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -07001612 if (rc) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001613 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001614 if (rc) {
1615 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1616 "file header region or xattr region\n");
1617 rc = -EINVAL;
1618 goto out;
1619 }
1620 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1621 ecryptfs_dentry,
1622 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1623 if (rc) {
1624 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1625 "file xattr region either\n");
1626 rc = -EINVAL;
1627 }
1628 if (crypt_stat->mount_crypt_stat->flags
1629 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1630 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1631 } else {
1632 printk(KERN_WARNING "Attempt to access file with "
1633 "crypto metadata only in the extended attribute "
1634 "region, but eCryptfs was mounted without "
1635 "xattr support enabled. eCryptfs will not treat "
1636 "this like an encrypted file.\n");
1637 rc = -EINVAL;
1638 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001639 }
1640out:
1641 if (page_virt) {
1642 memset(page_virt, 0, PAGE_CACHE_SIZE);
1643 kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1644 }
1645 return rc;
1646}
1647
1648/**
1649 * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1650 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1651 * @name: The plaintext name
1652 * @length: The length of the plaintext
1653 * @encoded_name: The encypted name
1654 *
1655 * Encrypts and encodes a filename into something that constitutes a
1656 * valid filename for a filesystem, with printable characters.
1657 *
1658 * We assume that we have a properly initialized crypto context,
1659 * pointed to by crypt_stat->tfm.
1660 *
1661 * TODO: Implement filename decoding and decryption here, in place of
1662 * memcpy. We are keeping the framework around for now to (1)
1663 * facilitate testing of the components needed to implement filename
1664 * encryption and (2) to provide a code base from which other
1665 * developers in the community can easily implement this feature.
1666 *
1667 * Returns the length of encoded filename; negative if error
1668 */
1669int
1670ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1671 const char *name, int length, char **encoded_name)
1672{
1673 int error = 0;
1674
1675 (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
1676 if (!(*encoded_name)) {
1677 error = -ENOMEM;
1678 goto out;
1679 }
1680 /* TODO: Filename encryption is a scheduled feature for a
1681 * future version of eCryptfs. This function is here only for
1682 * the purpose of providing a framework for other developers
1683 * to easily implement filename encryption. Hint: Replace this
1684 * memcpy() with a call to encrypt and encode the
1685 * filename, the set the length accordingly. */
1686 memcpy((void *)(*encoded_name), (void *)name, length);
1687 (*encoded_name)[length] = '\0';
1688 error = length + 1;
1689out:
1690 return error;
1691}
1692
1693/**
1694 * ecryptfs_decode_filename - converts the cipher text name to plaintext
1695 * @crypt_stat: The crypt_stat struct associated with the file
1696 * @name: The filename in cipher text
1697 * @length: The length of the cipher text name
1698 * @decrypted_name: The plaintext name
1699 *
1700 * Decodes and decrypts the filename.
1701 *
1702 * We assume that we have a properly initialized crypto context,
1703 * pointed to by crypt_stat->tfm.
1704 *
1705 * TODO: Implement filename decoding and decryption here, in place of
1706 * memcpy. We are keeping the framework around for now to (1)
1707 * facilitate testing of the components needed to implement filename
1708 * encryption and (2) to provide a code base from which other
1709 * developers in the community can easily implement this feature.
1710 *
1711 * Returns the length of decoded filename; negative if error
1712 */
1713int
1714ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1715 const char *name, int length, char **decrypted_name)
1716{
1717 int error = 0;
1718
1719 (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
1720 if (!(*decrypted_name)) {
1721 error = -ENOMEM;
1722 goto out;
1723 }
1724 /* TODO: Filename encryption is a scheduled feature for a
1725 * future version of eCryptfs. This function is here only for
1726 * the purpose of providing a framework for other developers
1727 * to easily implement filename encryption. Hint: Replace this
1728 * memcpy() with a call to decode and decrypt the
1729 * filename, the set the length accordingly. */
1730 memcpy((void *)(*decrypted_name), (void *)name, length);
1731 (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
1732 * in printing out the
1733 * string in debug
1734 * messages */
1735 error = length;
1736out:
1737 return error;
1738}
1739
1740/**
Michael Halcrowf4aad162007-10-16 01:27:53 -07001741 * ecryptfs_process_key_cipher - Perform key cipher initialization.
Michael Halcrow237fead2006-10-04 02:16:22 -07001742 * @key_tfm: Crypto context for key material, set by this function
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001743 * @cipher_name: Name of the cipher
1744 * @key_size: Size of the key in bytes
Michael Halcrow237fead2006-10-04 02:16:22 -07001745 *
1746 * Returns zero on success. Any crypto_tfm structs allocated here
1747 * should be released by other functions, such as on a superblock put
1748 * event, regardless of whether this function succeeds for fails.
1749 */
Michael Halcrowcd9d67d2007-10-16 01:28:04 -07001750static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001751ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1752 char *cipher_name, size_t *key_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001753{
1754 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrow8bba0662006-10-30 22:07:18 -08001755 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -07001756 int rc;
1757
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001758 *key_tfm = NULL;
1759 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001760 rc = -EINVAL;
1761 printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001762 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
Michael Halcrow237fead2006-10-04 02:16:22 -07001763 goto out;
1764 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001765 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1766 "ecb");
1767 if (rc)
1768 goto out;
1769 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
1770 kfree(full_alg_name);
1771 if (IS_ERR(*key_tfm)) {
1772 rc = PTR_ERR(*key_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -07001773 printk(KERN_ERR "Unable to allocate crypto cipher with name "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001774 "[%s]; rc = [%d]\n", cipher_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001775 goto out;
1776 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001777 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1778 if (*key_size == 0) {
1779 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
1780
1781 *key_size = alg->max_keysize;
1782 }
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001783 get_random_bytes(dummy_key, *key_size);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001784 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001785 if (rc) {
1786 printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001787 "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001788 rc = -EINVAL;
1789 goto out;
1790 }
1791out:
1792 return rc;
1793}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001794
1795struct kmem_cache *ecryptfs_key_tfm_cache;
1796struct list_head key_tfm_list;
1797struct mutex key_tfm_list_mutex;
1798
1799int ecryptfs_init_crypto(void)
1800{
1801 mutex_init(&key_tfm_list_mutex);
1802 INIT_LIST_HEAD(&key_tfm_list);
1803 return 0;
1804}
1805
Michael Halcrowfcd12832007-10-16 01:28:01 -07001806int ecryptfs_destroy_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001807{
1808 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1809
1810 mutex_lock(&key_tfm_list_mutex);
1811 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1812 key_tfm_list) {
1813 list_del(&key_tfm->key_tfm_list);
1814 if (key_tfm->key_tfm)
1815 crypto_free_blkcipher(key_tfm->key_tfm);
1816 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1817 }
1818 mutex_unlock(&key_tfm_list_mutex);
1819 return 0;
1820}
1821
1822int
1823ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1824 size_t key_size)
1825{
1826 struct ecryptfs_key_tfm *tmp_tfm;
1827 int rc = 0;
1828
1829 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1830 if (key_tfm != NULL)
1831 (*key_tfm) = tmp_tfm;
1832 if (!tmp_tfm) {
1833 rc = -ENOMEM;
1834 printk(KERN_ERR "Error attempting to allocate from "
1835 "ecryptfs_key_tfm_cache\n");
1836 goto out;
1837 }
1838 mutex_init(&tmp_tfm->key_tfm_mutex);
1839 strncpy(tmp_tfm->cipher_name, cipher_name,
1840 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1841 tmp_tfm->key_size = key_size;
Michael Halcrow5dda6992007-10-16 01:28:06 -07001842 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1843 tmp_tfm->cipher_name,
1844 &tmp_tfm->key_size);
1845 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001846 printk(KERN_ERR "Error attempting to initialize key TFM "
1847 "cipher with name = [%s]; rc = [%d]\n",
1848 tmp_tfm->cipher_name, rc);
1849 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1850 if (key_tfm != NULL)
1851 (*key_tfm) = NULL;
1852 goto out;
1853 }
1854 mutex_lock(&key_tfm_list_mutex);
1855 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1856 mutex_unlock(&key_tfm_list_mutex);
1857out:
1858 return rc;
1859}
1860
1861int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1862 struct mutex **tfm_mutex,
1863 char *cipher_name)
1864{
1865 struct ecryptfs_key_tfm *key_tfm;
1866 int rc = 0;
1867
1868 (*tfm) = NULL;
1869 (*tfm_mutex) = NULL;
1870 mutex_lock(&key_tfm_list_mutex);
1871 list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) {
1872 if (strcmp(key_tfm->cipher_name, cipher_name) == 0) {
1873 (*tfm) = key_tfm->key_tfm;
1874 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1875 mutex_unlock(&key_tfm_list_mutex);
1876 goto out;
1877 }
1878 }
1879 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrow5dda6992007-10-16 01:28:06 -07001880 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
1881 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001882 printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n",
1883 rc);
1884 goto out;
1885 }
1886 (*tfm) = key_tfm->key_tfm;
1887 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1888out:
1889 return rc;
1890}