blob: 9408ea484164c2d414b91c4726307421873d34ce [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 Halcrow565d97242006-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 Halcrow565d97242006-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 Halcrow565d97242006-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 Halcrow565d97242006-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 Halcrow565d97242006-10-30 22:07:17 -0800116 crypt_stat->hash_tfm = desc.tfm;
Michael Halcrow237fead2006-10-04 02:16:22 -0700117 }
Michael Halcrow565d97242006-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 Halcrow565d97242006-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 Halcrow565d97242006-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) {
286 sg[i].page = pg;
287 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
356static void
357ecryptfs_extent_to_lwr_pg_idx_and_offset(unsigned long *lower_page_idx,
358 int *byte_offset,
359 struct ecryptfs_crypt_stat *crypt_stat,
360 unsigned long extent_num)
361{
362 unsigned long lower_extent_num;
363 int extents_occupied_by_headers_at_front;
364 int bytes_occupied_by_headers_at_front;
365 int extent_offset;
366 int extents_per_page;
367
368 bytes_occupied_by_headers_at_front =
Michael Halcrow45eaab72007-10-16 01:28:05 -0700369 (crypt_stat->extent_size
370 * crypt_stat->num_header_extents_at_front);
Michael Halcrow237fead2006-10-04 02:16:22 -0700371 extents_occupied_by_headers_at_front =
372 ( bytes_occupied_by_headers_at_front
373 / crypt_stat->extent_size );
374 lower_extent_num = extents_occupied_by_headers_at_front + extent_num;
375 extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
376 (*lower_page_idx) = lower_extent_num / extents_per_page;
377 extent_offset = lower_extent_num % extents_per_page;
378 (*byte_offset) = extent_offset * crypt_stat->extent_size;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700379 ecryptfs_printk(KERN_DEBUG, " * crypt_stat->extent_size = "
380 "[%d]\n", crypt_stat->extent_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700381 ecryptfs_printk(KERN_DEBUG, " * crypt_stat->"
382 "num_header_extents_at_front = [%d]\n",
383 crypt_stat->num_header_extents_at_front);
384 ecryptfs_printk(KERN_DEBUG, " * extents_occupied_by_headers_at_"
385 "front = [%d]\n", extents_occupied_by_headers_at_front);
386 ecryptfs_printk(KERN_DEBUG, " * lower_extent_num = [0x%.16x]\n",
387 lower_extent_num);
388 ecryptfs_printk(KERN_DEBUG, " * extents_per_page = [%d]\n",
389 extents_per_page);
390 ecryptfs_printk(KERN_DEBUG, " * (*lower_page_idx) = [0x%.16x]\n",
391 (*lower_page_idx));
392 ecryptfs_printk(KERN_DEBUG, " * extent_offset = [%d]\n",
393 extent_offset);
394 ecryptfs_printk(KERN_DEBUG, " * (*byte_offset) = [%d]\n",
395 (*byte_offset));
396}
397
398static int ecryptfs_write_out_page(struct ecryptfs_page_crypt_context *ctx,
399 struct page *lower_page,
400 struct inode *lower_inode,
401 int byte_offset_in_page, int bytes_to_write)
402{
403 int rc = 0;
404
405 if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
406 rc = ecryptfs_commit_lower_page(lower_page, lower_inode,
407 ctx->param.lower_file,
408 byte_offset_in_page,
409 bytes_to_write);
410 if (rc) {
411 ecryptfs_printk(KERN_ERR, "Error calling lower "
412 "commit; rc = [%d]\n", rc);
413 goto out;
414 }
415 } else {
416 rc = ecryptfs_writepage_and_release_lower_page(lower_page,
417 lower_inode,
418 ctx->param.wbc);
419 if (rc) {
420 ecryptfs_printk(KERN_ERR, "Error calling lower "
421 "writepage(); rc = [%d]\n", rc);
422 goto out;
423 }
424 }
425out:
426 return rc;
427}
428
429static int ecryptfs_read_in_page(struct ecryptfs_page_crypt_context *ctx,
430 struct page **lower_page,
431 struct inode *lower_inode,
432 unsigned long lower_page_idx,
433 int byte_offset_in_page)
434{
435 int rc = 0;
436
437 if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
438 /* TODO: Limit this to only the data extents that are
439 * needed */
440 rc = ecryptfs_get_lower_page(lower_page, lower_inode,
441 ctx->param.lower_file,
442 lower_page_idx,
443 byte_offset_in_page,
444 (PAGE_CACHE_SIZE
445 - byte_offset_in_page));
446 if (rc) {
447 ecryptfs_printk(
448 KERN_ERR, "Error attempting to grab, map, "
449 "and prepare_write lower page with index "
450 "[0x%.16x]; rc = [%d]\n", lower_page_idx, rc);
451 goto out;
452 }
453 } else {
Michael Halcrow9d8b8ce2007-02-12 00:53:48 -0800454 *lower_page = grab_cache_page(lower_inode->i_mapping,
455 lower_page_idx);
456 if (!(*lower_page)) {
457 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700458 ecryptfs_printk(
459 KERN_ERR, "Error attempting to grab and map "
460 "lower page with index [0x%.16x]; rc = [%d]\n",
461 lower_page_idx, rc);
462 goto out;
463 }
464 }
465out:
466 return rc;
467}
468
469/**
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700470 * ecryptfs_lower_offset_for_extent
471 *
472 * Convert an eCryptfs page index into a lower byte offset
473 */
474void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
475 struct ecryptfs_crypt_stat *crypt_stat)
476{
477 (*offset) = ((crypt_stat->extent_size
478 * crypt_stat->num_header_extents_at_front)
479 + (crypt_stat->extent_size * extent_num));
480}
481
482/**
483 * ecryptfs_encrypt_extent
484 * @enc_extent_page: Allocated page into which to encrypt the data in
485 * @page
486 * @crypt_stat: crypt_stat containing cryptographic context for the
487 * encryption operation
488 * @page: Page containing plaintext data extent to encrypt
489 * @extent_offset: Page extent offset for use in generating IV
490 *
491 * Encrypts one extent of data.
492 *
493 * Return zero on success; non-zero otherwise
494 */
495static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
496 struct ecryptfs_crypt_stat *crypt_stat,
497 struct page *page,
498 unsigned long extent_offset)
499{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700500 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700501 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
502 int rc;
503
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700504 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700505 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
506 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
507 (extent_base + extent_offset));
508 if (rc) {
509 ecryptfs_printk(KERN_ERR, "Error attempting to "
510 "derive IV for extent [0x%.16x]; "
511 "rc = [%d]\n", (extent_base + extent_offset),
512 rc);
513 goto out;
514 }
515 if (unlikely(ecryptfs_verbosity > 0)) {
516 ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
517 "with iv:\n");
518 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
519 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
520 "encryption:\n");
521 ecryptfs_dump_hex((char *)
522 (page_address(page)
523 + (extent_offset * crypt_stat->extent_size)),
524 8);
525 }
526 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
527 page, (extent_offset
528 * crypt_stat->extent_size),
529 crypt_stat->extent_size, extent_iv);
530 if (rc < 0) {
531 printk(KERN_ERR "%s: Error attempting to encrypt page with "
532 "page->index = [%ld], extent_offset = [%ld]; "
533 "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
534 rc);
535 goto out;
536 }
537 rc = 0;
538 if (unlikely(ecryptfs_verbosity > 0)) {
539 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
540 "rc = [%d]\n", (extent_base + extent_offset),
541 rc);
542 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
543 "encryption:\n");
544 ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
545 }
546out:
547 return rc;
548}
549
550/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700551 * ecryptfs_encrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700552 * @page: Page mapped from the eCryptfs inode for the file; contains
553 * decrypted content that needs to be encrypted (to a temporary
554 * page; not in place) and written out to the lower file
Michael Halcrow237fead2006-10-04 02:16:22 -0700555 *
556 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
557 * that eCryptfs pages may straddle the lower pages -- for instance,
558 * if the file was created on a machine with an 8K page size
559 * (resulting in an 8K header), and then the file is copied onto a
560 * host with a 32K page size, then when reading page 0 of the eCryptfs
561 * file, 24K of page 0 of the lower file will be read and decrypted,
562 * and then 8K of page 1 of the lower file will be read and decrypted.
563 *
Michael Halcrow237fead2006-10-04 02:16:22 -0700564 * Returns zero on success; negative on error
565 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700566int ecryptfs_encrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700567{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700568 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700569 struct ecryptfs_crypt_stat *crypt_stat;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700570 char *enc_extent_virt = NULL;
571 struct page *enc_extent_page;
572 loff_t extent_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700573 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700574
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700575 ecryptfs_inode = page->mapping->host;
576 crypt_stat =
577 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800578 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700579 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
580 0, PAGE_CACHE_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700581 if (rc)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700582 printk(KERN_ERR "%s: Error attempting to copy "
583 "page at index [%ld]\n", __FUNCTION__,
584 page->index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700585 goto out;
586 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700587 enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
588 if (!enc_extent_virt) {
589 rc = -ENOMEM;
590 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
591 "encrypted extent\n");
592 goto out;
593 }
594 enc_extent_page = virt_to_page(enc_extent_virt);
595 for (extent_offset = 0;
596 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
597 extent_offset++) {
598 loff_t offset;
599
600 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
601 extent_offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700602 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700603 printk(KERN_ERR "%s: Error encrypting extent; "
604 "rc = [%d]\n", __FUNCTION__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700605 goto out;
606 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700607 ecryptfs_lower_offset_for_extent(
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700608 &offset, ((((loff_t)page->index)
609 * (PAGE_CACHE_SIZE
610 / crypt_stat->extent_size))
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700611 + extent_offset), crypt_stat);
612 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
613 offset, crypt_stat->extent_size);
614 if (rc) {
615 ecryptfs_printk(KERN_ERR, "Error attempting "
616 "to write lower page; rc = [%d]"
617 "\n", rc);
618 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700619 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700620 extent_offset++;
621 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700622out:
623 kfree(enc_extent_virt);
624 return rc;
625}
626
627static int ecryptfs_decrypt_extent(struct page *page,
628 struct ecryptfs_crypt_stat *crypt_stat,
629 struct page *enc_extent_page,
630 unsigned long extent_offset)
631{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700632 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700633 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
634 int rc;
635
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700636 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700637 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
638 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
639 (extent_base + extent_offset));
Michael Halcrow237fead2006-10-04 02:16:22 -0700640 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700641 ecryptfs_printk(KERN_ERR, "Error attempting to "
642 "derive IV for extent [0x%.16x]; "
643 "rc = [%d]\n", (extent_base + extent_offset),
644 rc);
645 goto out;
646 }
647 if (unlikely(ecryptfs_verbosity > 0)) {
648 ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
649 "with iv:\n");
650 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
651 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
652 "decryption:\n");
653 ecryptfs_dump_hex((char *)
654 (page_address(enc_extent_page)
655 + (extent_offset * crypt_stat->extent_size)),
656 8);
657 }
658 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
659 (extent_offset
660 * crypt_stat->extent_size),
661 enc_extent_page, 0,
662 crypt_stat->extent_size, extent_iv);
663 if (rc < 0) {
664 printk(KERN_ERR "%s: Error attempting to decrypt to page with "
665 "page->index = [%ld], extent_offset = [%ld]; "
666 "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
667 rc);
668 goto out;
669 }
670 rc = 0;
671 if (unlikely(ecryptfs_verbosity > 0)) {
672 ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
673 "rc = [%d]\n", (extent_base + extent_offset),
674 rc);
675 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
676 "decryption:\n");
677 ecryptfs_dump_hex((char *)(page_address(page)
678 + (extent_offset
679 * crypt_stat->extent_size)), 8);
Michael Halcrow237fead2006-10-04 02:16:22 -0700680 }
681out:
682 return rc;
683}
684
685/**
686 * ecryptfs_decrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700687 * @page: Page mapped from the eCryptfs inode for the file; data read
688 * and decrypted from the lower file will be written into this
689 * page
Michael Halcrow237fead2006-10-04 02:16:22 -0700690 *
691 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
692 * that eCryptfs pages may straddle the lower pages -- for instance,
693 * if the file was created on a machine with an 8K page size
694 * (resulting in an 8K header), and then the file is copied onto a
695 * host with a 32K page size, then when reading page 0 of the eCryptfs
696 * file, 24K of page 0 of the lower file will be read and decrypted,
697 * and then 8K of page 1 of the lower file will be read and decrypted.
698 *
699 * Returns zero on success; negative on error
700 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700701int ecryptfs_decrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700702{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700703 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700704 struct ecryptfs_crypt_stat *crypt_stat;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700705 char *enc_extent_virt = NULL;
706 struct page *enc_extent_page;
707 unsigned long extent_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700708 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700709
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700710 ecryptfs_inode = page->mapping->host;
711 crypt_stat =
712 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800713 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700714 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
715 PAGE_CACHE_SIZE,
716 ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700717 if (rc)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700718 printk(KERN_ERR "%s: Error attempting to copy "
719 "page at index [%ld]\n", __FUNCTION__,
720 page->index);
721 goto out_clear_uptodate;
Michael Halcrow237fead2006-10-04 02:16:22 -0700722 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700723 enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
724 if (!enc_extent_virt) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700725 rc = -ENOMEM;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700726 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
727 "encrypted extent\n");
728 goto out_clear_uptodate;
Michael Halcrow237fead2006-10-04 02:16:22 -0700729 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700730 enc_extent_page = virt_to_page(enc_extent_virt);
731 for (extent_offset = 0;
732 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
733 extent_offset++) {
734 loff_t offset;
735
736 ecryptfs_lower_offset_for_extent(
737 &offset, ((page->index * (PAGE_CACHE_SIZE
738 / crypt_stat->extent_size))
739 + extent_offset), crypt_stat);
740 rc = ecryptfs_read_lower(enc_extent_virt, offset,
741 crypt_stat->extent_size,
742 ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700743 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700744 ecryptfs_printk(KERN_ERR, "Error attempting "
745 "to read lower page; rc = [%d]"
746 "\n", rc);
747 goto out_clear_uptodate;
Michael Halcrow237fead2006-10-04 02:16:22 -0700748 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700749 rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
750 extent_offset);
751 if (rc) {
752 printk(KERN_ERR "%s: Error encrypting extent; "
753 "rc = [%d]\n", __FUNCTION__, rc);
754 goto out_clear_uptodate;
Michael Halcrow237fead2006-10-04 02:16:22 -0700755 }
756 extent_offset++;
757 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700758 SetPageUptodate(page);
759 goto out;
760out_clear_uptodate:
761 ClearPageUptodate(page);
Michael Halcrow237fead2006-10-04 02:16:22 -0700762out:
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700763 kfree(enc_extent_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -0700764 return rc;
765}
766
767/**
768 * decrypt_scatterlist
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700769 * @crypt_stat: Cryptographic context
770 * @dest_sg: The destination scatterlist to decrypt into
771 * @src_sg: The source scatterlist to decrypt from
772 * @size: The number of bytes to decrypt
773 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700774 *
775 * Returns the number of bytes decrypted; negative value on error
776 */
777static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
778 struct scatterlist *dest_sg,
779 struct scatterlist *src_sg, int size,
780 unsigned char *iv)
781{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800782 struct blkcipher_desc desc = {
783 .tfm = crypt_stat->tfm,
784 .info = iv,
785 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
786 };
Michael Halcrow237fead2006-10-04 02:16:22 -0700787 int rc = 0;
788
789 /* Consider doing this once, when the file is opened */
790 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800791 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
792 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700793 if (rc) {
794 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
795 rc);
796 mutex_unlock(&crypt_stat->cs_tfm_mutex);
797 rc = -EINVAL;
798 goto out;
799 }
800 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800801 rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700802 mutex_unlock(&crypt_stat->cs_tfm_mutex);
803 if (rc) {
804 ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
805 rc);
806 goto out;
807 }
808 rc = size;
809out:
810 return rc;
811}
812
813/**
814 * ecryptfs_encrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700815 * @crypt_stat: The cryptographic context
816 * @dst_page: The page to encrypt into
817 * @dst_offset: The offset in the page to encrypt into
818 * @src_page: The page to encrypt from
819 * @src_offset: The offset in the page to encrypt from
820 * @size: The number of bytes to encrypt
821 * @iv: The initialization vector to use for the encryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700822 *
823 * Returns the number of bytes encrypted
824 */
825static int
826ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
827 struct page *dst_page, int dst_offset,
828 struct page *src_page, int src_offset, int size,
829 unsigned char *iv)
830{
831 struct scatterlist src_sg, dst_sg;
832
833 src_sg.page = src_page;
834 src_sg.offset = src_offset;
835 src_sg.length = size;
836 dst_sg.page = dst_page;
837 dst_sg.offset = dst_offset;
838 dst_sg.length = size;
839 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
840}
841
842/**
843 * ecryptfs_decrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700844 * @crypt_stat: The cryptographic context
845 * @dst_page: The page to decrypt into
846 * @dst_offset: The offset in the page to decrypt into
847 * @src_page: The page to decrypt from
848 * @src_offset: The offset in the page to decrypt from
849 * @size: The number of bytes to decrypt
850 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700851 *
852 * Returns the number of bytes decrypted
853 */
854static int
855ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
856 struct page *dst_page, int dst_offset,
857 struct page *src_page, int src_offset, int size,
858 unsigned char *iv)
859{
860 struct scatterlist src_sg, dst_sg;
861
862 src_sg.page = src_page;
863 src_sg.offset = src_offset;
864 src_sg.length = size;
865 dst_sg.page = dst_page;
866 dst_sg.offset = dst_offset;
867 dst_sg.length = size;
868 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
869}
870
871#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
872
873/**
874 * ecryptfs_init_crypt_ctx
875 * @crypt_stat: Uninitilized crypt stats structure
876 *
877 * Initialize the crypto context.
878 *
879 * TODO: Performance: Keep a cache of initialized cipher contexts;
880 * only init if needed
881 */
882int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
883{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800884 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -0700885 int rc = -EINVAL;
886
887 if (!crypt_stat->cipher) {
888 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
889 goto out;
890 }
891 ecryptfs_printk(KERN_DEBUG,
892 "Initializing cipher [%s]; strlen = [%d]; "
893 "key_size_bits = [%d]\n",
894 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
895 crypt_stat->key_size << 3);
896 if (crypt_stat->tfm) {
897 rc = 0;
898 goto out;
899 }
900 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800901 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
902 crypt_stat->cipher, "cbc");
903 if (rc)
904 goto out;
905 crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
906 CRYPTO_ALG_ASYNC);
907 kfree(full_alg_name);
Akinobu Mitade887772006-11-28 12:29:49 -0800908 if (IS_ERR(crypt_stat->tfm)) {
909 rc = PTR_ERR(crypt_stat->tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700910 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
911 "Error initializing cipher [%s]\n",
912 crypt_stat->cipher);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800913 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700914 goto out;
915 }
Herbert Xuf1ddcaf2007-01-27 10:05:15 +1100916 crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800917 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700918 rc = 0;
919out:
920 return rc;
921}
922
923static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
924{
925 int extent_size_tmp;
926
927 crypt_stat->extent_mask = 0xFFFFFFFF;
928 crypt_stat->extent_shift = 0;
929 if (crypt_stat->extent_size == 0)
930 return;
931 extent_size_tmp = crypt_stat->extent_size;
932 while ((extent_size_tmp & 0x01) == 0) {
933 extent_size_tmp >>= 1;
934 crypt_stat->extent_mask <<= 1;
935 crypt_stat->extent_shift++;
936 }
937}
938
939void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
940{
941 /* Default values; may be overwritten as we are parsing the
942 * packets. */
943 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
944 set_extent_mask_and_shift(crypt_stat);
945 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800946 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
947 crypt_stat->num_header_extents_at_front = 0;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700948 else {
949 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
950 crypt_stat->num_header_extents_at_front =
951 (ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
952 / crypt_stat->extent_size);
953 else
954 crypt_stat->num_header_extents_at_front =
955 (PAGE_CACHE_SIZE / crypt_stat->extent_size);
956 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700957}
958
959/**
960 * ecryptfs_compute_root_iv
961 * @crypt_stats
962 *
963 * On error, sets the root IV to all 0's.
964 */
965int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
966{
967 int rc = 0;
968 char dst[MD5_DIGEST_SIZE];
969
970 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
971 BUG_ON(crypt_stat->iv_bytes <= 0);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800972 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700973 rc = -EINVAL;
974 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
975 "cannot generate root IV\n");
976 goto out;
977 }
978 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
979 crypt_stat->key_size);
980 if (rc) {
981 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
982 "MD5 while generating root IV\n");
983 goto out;
984 }
985 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
986out:
987 if (rc) {
988 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800989 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
Michael Halcrow237fead2006-10-04 02:16:22 -0700990 }
991 return rc;
992}
993
994static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
995{
996 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800997 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700998 ecryptfs_compute_root_iv(crypt_stat);
999 if (unlikely(ecryptfs_verbosity > 0)) {
1000 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
1001 ecryptfs_dump_hex(crypt_stat->key,
1002 crypt_stat->key_size);
1003 }
1004}
1005
1006/**
Michael Halcrow17398952007-02-12 00:53:45 -08001007 * ecryptfs_copy_mount_wide_flags_to_inode_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001008 * @crypt_stat: The inode's cryptographic context
1009 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow17398952007-02-12 00:53:45 -08001010 *
1011 * This function propagates the mount-wide flags to individual inode
1012 * flags.
1013 */
1014static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
1015 struct ecryptfs_crypt_stat *crypt_stat,
1016 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
1017{
1018 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
1019 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1020 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
1021 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
1022}
1023
Michael Halcrowf4aad162007-10-16 01:27:53 -07001024static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
1025 struct ecryptfs_crypt_stat *crypt_stat,
1026 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
1027{
1028 struct ecryptfs_global_auth_tok *global_auth_tok;
1029 int rc = 0;
1030
1031 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1032 list_for_each_entry(global_auth_tok,
1033 &mount_crypt_stat->global_auth_tok_list,
1034 mount_crypt_stat_list) {
1035 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
1036 if (rc) {
1037 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
1038 mutex_unlock(
1039 &mount_crypt_stat->global_auth_tok_list_mutex);
1040 goto out;
1041 }
1042 }
1043 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1044out:
1045 return rc;
1046}
1047
Michael Halcrow17398952007-02-12 00:53:45 -08001048/**
Michael Halcrow237fead2006-10-04 02:16:22 -07001049 * ecryptfs_set_default_crypt_stat_vals
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001050 * @crypt_stat: The inode's cryptographic context
1051 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001052 *
1053 * Default values in the event that policy does not override them.
1054 */
1055static void ecryptfs_set_default_crypt_stat_vals(
1056 struct ecryptfs_crypt_stat *crypt_stat,
1057 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
1058{
Michael Halcrow17398952007-02-12 00:53:45 -08001059 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1060 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001061 ecryptfs_set_default_sizes(crypt_stat);
1062 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
1063 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001064 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
Michael Halcrow237fead2006-10-04 02:16:22 -07001065 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
1066 crypt_stat->mount_crypt_stat = mount_crypt_stat;
1067}
1068
1069/**
1070 * ecryptfs_new_file_context
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001071 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001072 *
1073 * If the crypto context for the file has not yet been established,
1074 * this is where we do that. Establishing a new crypto context
1075 * involves the following decisions:
1076 * - What cipher to use?
1077 * - What set of authentication tokens to use?
1078 * Here we just worry about getting enough information into the
1079 * authentication tokens so that we know that they are available.
1080 * We associate the available authentication tokens with the new file
1081 * via the set of signatures in the crypt_stat struct. Later, when
1082 * the headers are actually written out, we may again defer to
1083 * userspace to perform the encryption of the session key; for the
1084 * foreseeable future, this will be the case with public key packets.
1085 *
1086 * Returns zero on success; non-zero otherwise
1087 */
Michael Halcrow237fead2006-10-04 02:16:22 -07001088int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
1089{
Michael Halcrow237fead2006-10-04 02:16:22 -07001090 struct ecryptfs_crypt_stat *crypt_stat =
1091 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1092 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1093 &ecryptfs_superblock_to_private(
1094 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1095 int cipher_name_len;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001096 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001097
1098 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
Michael Halcrowaf655dc2007-10-16 01:28:00 -07001099 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001100 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1101 mount_crypt_stat);
1102 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
1103 mount_crypt_stat);
1104 if (rc) {
1105 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
1106 "to the inode key sigs; rc = [%d]\n", rc);
1107 goto out;
1108 }
1109 cipher_name_len =
1110 strlen(mount_crypt_stat->global_default_cipher_name);
1111 memcpy(crypt_stat->cipher,
1112 mount_crypt_stat->global_default_cipher_name,
1113 cipher_name_len);
1114 crypt_stat->cipher[cipher_name_len] = '\0';
1115 crypt_stat->key_size =
1116 mount_crypt_stat->global_default_cipher_key_size;
1117 ecryptfs_generate_new_key(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001118 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1119 if (rc)
1120 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1121 "context for cipher [%s]: rc = [%d]\n",
1122 crypt_stat->cipher, rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001123out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001124 return rc;
1125}
1126
1127/**
1128 * contains_ecryptfs_marker - check for the ecryptfs marker
1129 * @data: The data block in which to check
1130 *
1131 * Returns one if marker found; zero if not found
1132 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001133static int contains_ecryptfs_marker(char *data)
Michael Halcrow237fead2006-10-04 02:16:22 -07001134{
1135 u32 m_1, m_2;
1136
1137 memcpy(&m_1, data, 4);
1138 m_1 = be32_to_cpu(m_1);
1139 memcpy(&m_2, (data + 4), 4);
1140 m_2 = be32_to_cpu(m_2);
1141 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1142 return 1;
1143 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1144 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1145 MAGIC_ECRYPTFS_MARKER);
1146 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1147 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1148 return 0;
1149}
1150
1151struct ecryptfs_flag_map_elem {
1152 u32 file_flag;
1153 u32 local_flag;
1154};
1155
1156/* Add support for additional flags by adding elements here. */
1157static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1158 {0x00000001, ECRYPTFS_ENABLE_HMAC},
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001159 {0x00000002, ECRYPTFS_ENCRYPTED},
1160 {0x00000004, ECRYPTFS_METADATA_IN_XATTR}
Michael Halcrow237fead2006-10-04 02:16:22 -07001161};
1162
1163/**
1164 * ecryptfs_process_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001165 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001166 * @page_virt: Source data to be parsed
1167 * @bytes_read: Updated with the number of bytes read
1168 *
1169 * Returns zero on success; non-zero if the flag set is invalid
1170 */
1171static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1172 char *page_virt, int *bytes_read)
1173{
1174 int rc = 0;
1175 int i;
1176 u32 flags;
1177
1178 memcpy(&flags, page_virt, 4);
1179 flags = be32_to_cpu(flags);
1180 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1181 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1182 if (flags & ecryptfs_flag_map[i].file_flag) {
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001183 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
Michael Halcrow237fead2006-10-04 02:16:22 -07001184 } else
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001185 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
Michael Halcrow237fead2006-10-04 02:16:22 -07001186 /* Version is in top 8 bits of the 32-bit flag vector */
1187 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1188 (*bytes_read) = 4;
1189 return rc;
1190}
1191
1192/**
1193 * write_ecryptfs_marker
1194 * @page_virt: The pointer to in a page to begin writing the marker
1195 * @written: Number of bytes written
1196 *
1197 * Marker = 0x3c81b7f5
1198 */
1199static void write_ecryptfs_marker(char *page_virt, size_t *written)
1200{
1201 u32 m_1, m_2;
1202
1203 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1204 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1205 m_1 = cpu_to_be32(m_1);
1206 memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1207 m_2 = cpu_to_be32(m_2);
1208 memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
1209 (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1210 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1211}
1212
1213static void
1214write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1215 size_t *written)
1216{
1217 u32 flags = 0;
1218 int i;
1219
1220 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1221 / sizeof(struct ecryptfs_flag_map_elem))); i++)
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001222 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
Michael Halcrow237fead2006-10-04 02:16:22 -07001223 flags |= ecryptfs_flag_map[i].file_flag;
1224 /* Version is in top 8 bits of the 32-bit flag vector */
1225 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1226 flags = cpu_to_be32(flags);
1227 memcpy(page_virt, &flags, 4);
1228 (*written) = 4;
1229}
1230
1231struct ecryptfs_cipher_code_str_map_elem {
1232 char cipher_str[16];
1233 u16 cipher_code;
1234};
1235
1236/* Add support for additional ciphers by adding elements here. The
1237 * cipher_code is whatever OpenPGP applicatoins use to identify the
1238 * ciphers. List in order of probability. */
1239static struct ecryptfs_cipher_code_str_map_elem
1240ecryptfs_cipher_code_str_map[] = {
1241 {"aes",RFC2440_CIPHER_AES_128 },
1242 {"blowfish", RFC2440_CIPHER_BLOWFISH},
1243 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
1244 {"cast5", RFC2440_CIPHER_CAST_5},
1245 {"twofish", RFC2440_CIPHER_TWOFISH},
1246 {"cast6", RFC2440_CIPHER_CAST_6},
1247 {"aes", RFC2440_CIPHER_AES_192},
1248 {"aes", RFC2440_CIPHER_AES_256}
1249};
1250
1251/**
1252 * ecryptfs_code_for_cipher_string
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001253 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001254 *
1255 * Returns zero on no match, or the cipher code on match
1256 */
1257u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
1258{
1259 int i;
1260 u16 code = 0;
1261 struct ecryptfs_cipher_code_str_map_elem *map =
1262 ecryptfs_cipher_code_str_map;
1263
1264 if (strcmp(crypt_stat->cipher, "aes") == 0) {
1265 switch (crypt_stat->key_size) {
1266 case 16:
1267 code = RFC2440_CIPHER_AES_128;
1268 break;
1269 case 24:
1270 code = RFC2440_CIPHER_AES_192;
1271 break;
1272 case 32:
1273 code = RFC2440_CIPHER_AES_256;
1274 }
1275 } else {
1276 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1277 if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
1278 code = map[i].cipher_code;
1279 break;
1280 }
1281 }
1282 return code;
1283}
1284
1285/**
1286 * ecryptfs_cipher_code_to_string
1287 * @str: Destination to write out the cipher name
1288 * @cipher_code: The code to convert to cipher name string
1289 *
1290 * Returns zero on success
1291 */
1292int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
1293{
1294 int rc = 0;
1295 int i;
1296
1297 str[0] = '\0';
1298 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1299 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1300 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1301 if (str[0] == '\0') {
1302 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1303 "[%d]\n", cipher_code);
1304 rc = -EINVAL;
1305 }
1306 return rc;
1307}
1308
1309/**
1310 * ecryptfs_read_header_region
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001311 * @data: The virtual address to write header region data into
1312 * @dentry: The lower dentry
1313 * @mnt: The lower VFS mount
Michael Halcrow237fead2006-10-04 02:16:22 -07001314 *
1315 * Returns zero on success; non-zero otherwise
1316 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001317static int ecryptfs_read_header_region(char *data, struct dentry *dentry,
1318 struct vfsmount *mnt)
Michael Halcrow237fead2006-10-04 02:16:22 -07001319{
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001320 struct file *lower_file;
Michael Halcrow237fead2006-10-04 02:16:22 -07001321 mm_segment_t oldfs;
1322 int rc;
1323
Michael Halcrow5dda6992007-10-16 01:28:06 -07001324 rc = ecryptfs_open_lower_file(&lower_file, dentry, mnt, O_RDONLY);
1325 if (rc) {
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001326 printk(KERN_ERR
1327 "Error opening lower_file to read header region\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001328 goto out;
1329 }
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001330 lower_file->f_pos = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001331 oldfs = get_fs();
1332 set_fs(get_ds());
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001333 rc = lower_file->f_op->read(lower_file, (char __user *)data,
1334 ECRYPTFS_DEFAULT_EXTENT_SIZE, &lower_file->f_pos);
Michael Halcrow237fead2006-10-04 02:16:22 -07001335 set_fs(oldfs);
Michael Halcrow5dda6992007-10-16 01:28:06 -07001336 rc = ecryptfs_close_lower_file(lower_file);
1337 if (rc) {
Michael Halcrow7ff1d742006-10-30 22:07:19 -08001338 printk(KERN_ERR "Error closing lower_file\n");
1339 goto out;
1340 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001341 rc = 0;
1342out:
1343 return rc;
1344}
1345
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001346int ecryptfs_read_and_validate_header_region(char *data,
1347 struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001348{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001349 struct ecryptfs_crypt_stat *crypt_stat =
1350 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001351 int rc;
1352
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001353 rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
1354 ecryptfs_inode);
1355 if (rc) {
1356 printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
1357 __FUNCTION__, rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001358 goto out;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001359 }
1360 if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001361 rc = -EINVAL;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001362 ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n");
1363 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001364out:
1365 return rc;
1366}
1367
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001368void
1369ecryptfs_write_header_metadata(char *virt,
1370 struct ecryptfs_crypt_stat *crypt_stat,
1371 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001372{
1373 u32 header_extent_size;
1374 u16 num_header_extents_at_front;
1375
Michael Halcrow45eaab72007-10-16 01:28:05 -07001376 header_extent_size = (u32)crypt_stat->extent_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001377 num_header_extents_at_front =
1378 (u16)crypt_stat->num_header_extents_at_front;
1379 header_extent_size = cpu_to_be32(header_extent_size);
1380 memcpy(virt, &header_extent_size, 4);
1381 virt += 4;
1382 num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
1383 memcpy(virt, &num_header_extents_at_front, 2);
1384 (*written) = 6;
1385}
1386
1387struct kmem_cache *ecryptfs_header_cache_0;
1388struct kmem_cache *ecryptfs_header_cache_1;
1389struct kmem_cache *ecryptfs_header_cache_2;
1390
1391/**
1392 * ecryptfs_write_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001393 * @page_virt: The virtual address to write the headers to
1394 * @size: Set to the number of bytes written by this function
1395 * @crypt_stat: The cryptographic context
1396 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001397 *
1398 * Format version: 1
1399 *
1400 * Header Extent:
1401 * Octets 0-7: Unencrypted file size (big-endian)
1402 * Octets 8-15: eCryptfs special marker
1403 * Octets 16-19: Flags
1404 * Octet 16: File format version number (between 0 and 255)
1405 * Octets 17-18: Reserved
1406 * Octet 19: Bit 1 (lsb): Reserved
1407 * Bit 2: Encrypted?
1408 * Bits 3-8: Reserved
1409 * Octets 20-23: Header extent size (big-endian)
1410 * Octets 24-25: Number of header extents at front of file
1411 * (big-endian)
1412 * Octet 26: Begin RFC 2440 authentication token packet set
1413 * Data Extent 0:
1414 * Lower data (CBC encrypted)
1415 * Data Extent 1:
1416 * Lower data (CBC encrypted)
1417 * ...
1418 *
1419 * Returns zero on success
1420 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001421static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
1422 struct ecryptfs_crypt_stat *crypt_stat,
1423 struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001424{
1425 int rc;
1426 size_t written;
1427 size_t offset;
1428
1429 offset = ECRYPTFS_FILE_SIZE_BYTES;
1430 write_ecryptfs_marker((page_virt + offset), &written);
1431 offset += written;
1432 write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1433 offset += written;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001434 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1435 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001436 offset += written;
1437 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1438 ecryptfs_dentry, &written,
1439 PAGE_CACHE_SIZE - offset);
1440 if (rc)
1441 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1442 "set; rc = [%d]\n", rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001443 if (size) {
1444 offset += written;
1445 *size = offset;
1446 }
1447 return rc;
1448}
1449
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001450static int
1451ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001452 struct dentry *ecryptfs_dentry,
1453 char *page_virt)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001454{
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001455 int current_header_page;
1456 int header_pages;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001457 int rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001458
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001459 rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, page_virt,
1460 0, PAGE_CACHE_SIZE);
1461 if (rc) {
1462 printk(KERN_ERR "%s: Error attempting to write header "
1463 "information to lower file; rc = [%d]\n", __FUNCTION__,
1464 rc);
Michael Halcrow70456602007-02-12 00:53:48 -08001465 goto out;
1466 }
Michael Halcrow45eaab72007-10-16 01:28:05 -07001467 header_pages = ((crypt_stat->extent_size
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001468 * crypt_stat->num_header_extents_at_front)
1469 / PAGE_CACHE_SIZE);
1470 memset(page_virt, 0, PAGE_CACHE_SIZE);
1471 current_header_page = 1;
1472 while (current_header_page < header_pages) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001473 loff_t offset;
1474
Michael Halcrowd6a13c12007-10-16 01:28:12 -07001475 offset = (((loff_t)current_header_page) << PAGE_CACHE_SHIFT);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001476 if ((rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode,
1477 page_virt, offset,
1478 PAGE_CACHE_SIZE))) {
1479 printk(KERN_ERR "%s: Error attempting to write header "
1480 "information to lower file; rc = [%d]\n",
1481 __FUNCTION__, rc);
Michael Halcrow70456602007-02-12 00:53:48 -08001482 goto out;
1483 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001484 current_header_page++;
1485 }
Michael Halcrow70456602007-02-12 00:53:48 -08001486out:
1487 return rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001488}
1489
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001490static int
1491ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1492 struct ecryptfs_crypt_stat *crypt_stat,
1493 char *page_virt, size_t size)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001494{
1495 int rc;
1496
1497 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1498 size, 0);
Michael Halcrow237fead2006-10-04 02:16:22 -07001499 return rc;
1500}
1501
1502/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001503 * ecryptfs_write_metadata
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001504 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001505 *
1506 * Write the file headers out. This will likely involve a userspace
1507 * callout, in which the session key is encrypted with one or more
1508 * public keys and/or the passphrase necessary to do the encryption is
1509 * retrieved via a prompt. Exactly what happens at this point should
1510 * be policy-dependent.
1511 *
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001512 * TODO: Support header information spanning multiple pages
1513 *
Michael Halcrow237fead2006-10-04 02:16:22 -07001514 * Returns zero on success; non-zero on error
1515 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001516int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001517{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001518 struct ecryptfs_crypt_stat *crypt_stat =
1519 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001520 char *page_virt;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001521 size_t size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001522 int rc = 0;
1523
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001524 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1525 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001526 printk(KERN_ERR "Key is invalid; bailing out\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001527 rc = -EINVAL;
1528 goto out;
1529 }
1530 } else {
1531 rc = -EINVAL;
1532 ecryptfs_printk(KERN_WARNING,
1533 "Called with crypt_stat->encrypted == 0\n");
1534 goto out;
1535 }
1536 /* Released in this function */
Robert P. J. Dayc3762222007-02-10 01:45:03 -08001537 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001538 if (!page_virt) {
1539 ecryptfs_printk(KERN_ERR, "Out of memory\n");
1540 rc = -ENOMEM;
1541 goto out;
1542 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001543 rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat,
1544 ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -07001545 if (unlikely(rc)) {
1546 ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
1547 memset(page_virt, 0, PAGE_CACHE_SIZE);
1548 goto out_free;
1549 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001550 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1551 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
1552 crypt_stat, page_virt,
1553 size);
1554 else
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001555 rc = ecryptfs_write_metadata_to_contents(crypt_stat,
1556 ecryptfs_dentry,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001557 page_virt);
1558 if (rc) {
1559 printk(KERN_ERR "Error writing metadata out to lower file; "
1560 "rc = [%d]\n", rc);
1561 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001562 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001563out_free:
1564 kmem_cache_free(ecryptfs_header_cache_0, page_virt);
1565out:
1566 return rc;
1567}
1568
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001569#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1570#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
Michael Halcrow237fead2006-10-04 02:16:22 -07001571static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001572 char *virt, int *bytes_read,
1573 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001574{
1575 int rc = 0;
1576 u32 header_extent_size;
1577 u16 num_header_extents_at_front;
1578
1579 memcpy(&header_extent_size, virt, 4);
1580 header_extent_size = be32_to_cpu(header_extent_size);
1581 virt += 4;
1582 memcpy(&num_header_extents_at_front, virt, 2);
1583 num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
Michael Halcrow237fead2006-10-04 02:16:22 -07001584 crypt_stat->num_header_extents_at_front =
1585 (int)num_header_extents_at_front;
Michael Halcrow45eaab72007-10-16 01:28:05 -07001586 (*bytes_read) = (sizeof(u32) + sizeof(u16));
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001587 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
Michael Halcrow45eaab72007-10-16 01:28:05 -07001588 && ((crypt_stat->extent_size
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001589 * crypt_stat->num_header_extents_at_front)
1590 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001591 rc = -EINVAL;
Michael Halcrow45eaab72007-10-16 01:28:05 -07001592 printk(KERN_WARNING "Invalid number of header extents: [%zd]\n",
1593 crypt_stat->num_header_extents_at_front);
Michael Halcrow237fead2006-10-04 02:16:22 -07001594 }
1595 return rc;
1596}
1597
1598/**
1599 * set_default_header_data
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001600 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001601 *
1602 * For version 0 file format; this function is only for backwards
1603 * compatibility for files created with the prior versions of
1604 * eCryptfs.
1605 */
1606static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1607{
Michael Halcrow45eaab72007-10-16 01:28:05 -07001608 crypt_stat->num_header_extents_at_front = 2;
Michael Halcrow237fead2006-10-04 02:16:22 -07001609}
1610
1611/**
1612 * ecryptfs_read_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001613 * @page_virt: The virtual address into which to read the headers
1614 * @crypt_stat: The cryptographic context
1615 * @ecryptfs_dentry: The eCryptfs dentry
1616 * @validate_header_size: Whether to validate the header size while reading
Michael Halcrow237fead2006-10-04 02:16:22 -07001617 *
1618 * Read/parse the header data. The header format is detailed in the
1619 * comment block for the ecryptfs_write_headers_virt() function.
1620 *
1621 * Returns zero on success
1622 */
1623static int ecryptfs_read_headers_virt(char *page_virt,
1624 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001625 struct dentry *ecryptfs_dentry,
1626 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001627{
1628 int rc = 0;
1629 int offset;
1630 int bytes_read;
1631
1632 ecryptfs_set_default_sizes(crypt_stat);
1633 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1634 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1635 offset = ECRYPTFS_FILE_SIZE_BYTES;
1636 rc = contains_ecryptfs_marker(page_virt + offset);
1637 if (rc == 0) {
1638 rc = -EINVAL;
1639 goto out;
1640 }
1641 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1642 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1643 &bytes_read);
1644 if (rc) {
1645 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1646 goto out;
1647 }
1648 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1649 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1650 "file version [%d] is supported by this "
1651 "version of eCryptfs\n",
1652 crypt_stat->file_version,
1653 ECRYPTFS_SUPPORTED_FILE_VERSION);
1654 rc = -EINVAL;
1655 goto out;
1656 }
1657 offset += bytes_read;
1658 if (crypt_stat->file_version >= 1) {
1659 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001660 &bytes_read, validate_header_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001661 if (rc) {
1662 ecryptfs_printk(KERN_WARNING, "Error reading header "
1663 "metadata; rc = [%d]\n", rc);
1664 }
1665 offset += bytes_read;
1666 } else
1667 set_default_header_data(crypt_stat);
1668 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1669 ecryptfs_dentry);
1670out:
1671 return rc;
1672}
1673
1674/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001675 * ecryptfs_read_xattr_region
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001676 * @page_virt: The vitual address into which to read the xattr data
Michael Halcrow2ed92552007-10-16 01:28:10 -07001677 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001678 *
1679 * Attempts to read the crypto metadata from the extended attribute
1680 * region of the lower file.
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001681 *
1682 * Returns zero on success; non-zero on error
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001683 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001684int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001685{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001686 struct dentry *lower_dentry =
1687 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001688 ssize_t size;
1689 int rc = 0;
1690
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001691 size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1692 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001693 if (size < 0) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001694 printk(KERN_ERR "Error attempting to read the [%s] "
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001695 "xattr from the lower file; return value = [%zd]\n",
1696 ECRYPTFS_XATTR_NAME, size);
1697 rc = -EINVAL;
1698 goto out;
1699 }
1700out:
1701 return rc;
1702}
1703
1704int ecryptfs_read_and_validate_xattr_region(char *page_virt,
1705 struct dentry *ecryptfs_dentry)
1706{
1707 int rc;
1708
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001709 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001710 if (rc)
1711 goto out;
1712 if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
1713 printk(KERN_WARNING "Valid data found in [%s] xattr, but "
1714 "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
1715 rc = -EINVAL;
1716 }
1717out:
1718 return rc;
1719}
1720
1721/**
1722 * ecryptfs_read_metadata
1723 *
1724 * Common entry point for reading file metadata. From here, we could
1725 * retrieve the header information from the header region of the file,
1726 * the xattr region of the file, or some other repostory that is
1727 * stored separately from the file itself. The current implementation
1728 * supports retrieving the metadata information from the file contents
1729 * and from the xattr region.
Michael Halcrow237fead2006-10-04 02:16:22 -07001730 *
1731 * Returns zero if valid headers found and parsed; non-zero otherwise
1732 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001733int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001734{
1735 int rc = 0;
1736 char *page_virt = NULL;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001737 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -07001738 struct ecryptfs_crypt_stat *crypt_stat =
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001739 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001740 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1741 &ecryptfs_superblock_to_private(
1742 ecryptfs_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001743
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001744 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1745 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001746 /* Read the first page from the underlying file */
Christoph Lameterf7267c02006-12-06 20:33:15 -08001747 page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001748 if (!page_virt) {
1749 rc = -ENOMEM;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001750 printk(KERN_ERR "%s: Unable to allocate page_virt\n",
1751 __FUNCTION__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001752 goto out;
1753 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001754 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1755 ecryptfs_inode);
1756 if (!rc)
1757 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1758 ecryptfs_dentry,
1759 ECRYPTFS_VALIDATE_HEADER_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -07001760 if (rc) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001761 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001762 if (rc) {
1763 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1764 "file header region or xattr region\n");
1765 rc = -EINVAL;
1766 goto out;
1767 }
1768 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1769 ecryptfs_dentry,
1770 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1771 if (rc) {
1772 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1773 "file xattr region either\n");
1774 rc = -EINVAL;
1775 }
1776 if (crypt_stat->mount_crypt_stat->flags
1777 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1778 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1779 } else {
1780 printk(KERN_WARNING "Attempt to access file with "
1781 "crypto metadata only in the extended attribute "
1782 "region, but eCryptfs was mounted without "
1783 "xattr support enabled. eCryptfs will not treat "
1784 "this like an encrypted file.\n");
1785 rc = -EINVAL;
1786 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001787 }
1788out:
1789 if (page_virt) {
1790 memset(page_virt, 0, PAGE_CACHE_SIZE);
1791 kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1792 }
1793 return rc;
1794}
1795
1796/**
1797 * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1798 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1799 * @name: The plaintext name
1800 * @length: The length of the plaintext
1801 * @encoded_name: The encypted name
1802 *
1803 * Encrypts and encodes a filename into something that constitutes a
1804 * valid filename for a filesystem, with printable characters.
1805 *
1806 * We assume that we have a properly initialized crypto context,
1807 * pointed to by crypt_stat->tfm.
1808 *
1809 * TODO: Implement filename decoding and decryption here, in place of
1810 * memcpy. We are keeping the framework around for now to (1)
1811 * facilitate testing of the components needed to implement filename
1812 * encryption and (2) to provide a code base from which other
1813 * developers in the community can easily implement this feature.
1814 *
1815 * Returns the length of encoded filename; negative if error
1816 */
1817int
1818ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1819 const char *name, int length, char **encoded_name)
1820{
1821 int error = 0;
1822
1823 (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
1824 if (!(*encoded_name)) {
1825 error = -ENOMEM;
1826 goto out;
1827 }
1828 /* TODO: Filename encryption is a scheduled feature for a
1829 * future version of eCryptfs. This function is here only for
1830 * the purpose of providing a framework for other developers
1831 * to easily implement filename encryption. Hint: Replace this
1832 * memcpy() with a call to encrypt and encode the
1833 * filename, the set the length accordingly. */
1834 memcpy((void *)(*encoded_name), (void *)name, length);
1835 (*encoded_name)[length] = '\0';
1836 error = length + 1;
1837out:
1838 return error;
1839}
1840
1841/**
1842 * ecryptfs_decode_filename - converts the cipher text name to plaintext
1843 * @crypt_stat: The crypt_stat struct associated with the file
1844 * @name: The filename in cipher text
1845 * @length: The length of the cipher text name
1846 * @decrypted_name: The plaintext name
1847 *
1848 * Decodes and decrypts the filename.
1849 *
1850 * We assume that we have a properly initialized crypto context,
1851 * pointed to by crypt_stat->tfm.
1852 *
1853 * TODO: Implement filename decoding and decryption here, in place of
1854 * memcpy. We are keeping the framework around for now to (1)
1855 * facilitate testing of the components needed to implement filename
1856 * encryption and (2) to provide a code base from which other
1857 * developers in the community can easily implement this feature.
1858 *
1859 * Returns the length of decoded filename; negative if error
1860 */
1861int
1862ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1863 const char *name, int length, char **decrypted_name)
1864{
1865 int error = 0;
1866
1867 (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
1868 if (!(*decrypted_name)) {
1869 error = -ENOMEM;
1870 goto out;
1871 }
1872 /* TODO: Filename encryption is a scheduled feature for a
1873 * future version of eCryptfs. This function is here only for
1874 * the purpose of providing a framework for other developers
1875 * to easily implement filename encryption. Hint: Replace this
1876 * memcpy() with a call to decode and decrypt the
1877 * filename, the set the length accordingly. */
1878 memcpy((void *)(*decrypted_name), (void *)name, length);
1879 (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
1880 * in printing out the
1881 * string in debug
1882 * messages */
1883 error = length;
1884out:
1885 return error;
1886}
1887
1888/**
Michael Halcrowf4aad162007-10-16 01:27:53 -07001889 * ecryptfs_process_key_cipher - Perform key cipher initialization.
Michael Halcrow237fead2006-10-04 02:16:22 -07001890 * @key_tfm: Crypto context for key material, set by this function
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001891 * @cipher_name: Name of the cipher
1892 * @key_size: Size of the key in bytes
Michael Halcrow237fead2006-10-04 02:16:22 -07001893 *
1894 * Returns zero on success. Any crypto_tfm structs allocated here
1895 * should be released by other functions, such as on a superblock put
1896 * event, regardless of whether this function succeeds for fails.
1897 */
Michael Halcrowcd9d67d2007-10-16 01:28:04 -07001898static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001899ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1900 char *cipher_name, size_t *key_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001901{
1902 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrow8bba0662006-10-30 22:07:18 -08001903 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -07001904 int rc;
1905
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001906 *key_tfm = NULL;
1907 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001908 rc = -EINVAL;
1909 printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001910 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
Michael Halcrow237fead2006-10-04 02:16:22 -07001911 goto out;
1912 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001913 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1914 "ecb");
1915 if (rc)
1916 goto out;
1917 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
1918 kfree(full_alg_name);
1919 if (IS_ERR(*key_tfm)) {
1920 rc = PTR_ERR(*key_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -07001921 printk(KERN_ERR "Unable to allocate crypto cipher with name "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001922 "[%s]; rc = [%d]\n", cipher_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001923 goto out;
1924 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001925 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1926 if (*key_size == 0) {
1927 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
1928
1929 *key_size = alg->max_keysize;
1930 }
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001931 get_random_bytes(dummy_key, *key_size);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001932 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001933 if (rc) {
1934 printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001935 "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001936 rc = -EINVAL;
1937 goto out;
1938 }
1939out:
1940 return rc;
1941}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001942
1943struct kmem_cache *ecryptfs_key_tfm_cache;
1944struct list_head key_tfm_list;
1945struct mutex key_tfm_list_mutex;
1946
1947int ecryptfs_init_crypto(void)
1948{
1949 mutex_init(&key_tfm_list_mutex);
1950 INIT_LIST_HEAD(&key_tfm_list);
1951 return 0;
1952}
1953
Michael Halcrowfcd12832007-10-16 01:28:01 -07001954int ecryptfs_destroy_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001955{
1956 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1957
1958 mutex_lock(&key_tfm_list_mutex);
1959 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1960 key_tfm_list) {
1961 list_del(&key_tfm->key_tfm_list);
1962 if (key_tfm->key_tfm)
1963 crypto_free_blkcipher(key_tfm->key_tfm);
1964 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1965 }
1966 mutex_unlock(&key_tfm_list_mutex);
1967 return 0;
1968}
1969
1970int
1971ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1972 size_t key_size)
1973{
1974 struct ecryptfs_key_tfm *tmp_tfm;
1975 int rc = 0;
1976
1977 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1978 if (key_tfm != NULL)
1979 (*key_tfm) = tmp_tfm;
1980 if (!tmp_tfm) {
1981 rc = -ENOMEM;
1982 printk(KERN_ERR "Error attempting to allocate from "
1983 "ecryptfs_key_tfm_cache\n");
1984 goto out;
1985 }
1986 mutex_init(&tmp_tfm->key_tfm_mutex);
1987 strncpy(tmp_tfm->cipher_name, cipher_name,
1988 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1989 tmp_tfm->key_size = key_size;
Michael Halcrow5dda6992007-10-16 01:28:06 -07001990 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1991 tmp_tfm->cipher_name,
1992 &tmp_tfm->key_size);
1993 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001994 printk(KERN_ERR "Error attempting to initialize key TFM "
1995 "cipher with name = [%s]; rc = [%d]\n",
1996 tmp_tfm->cipher_name, rc);
1997 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1998 if (key_tfm != NULL)
1999 (*key_tfm) = NULL;
2000 goto out;
2001 }
2002 mutex_lock(&key_tfm_list_mutex);
2003 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
2004 mutex_unlock(&key_tfm_list_mutex);
2005out:
2006 return rc;
2007}
2008
2009int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
2010 struct mutex **tfm_mutex,
2011 char *cipher_name)
2012{
2013 struct ecryptfs_key_tfm *key_tfm;
2014 int rc = 0;
2015
2016 (*tfm) = NULL;
2017 (*tfm_mutex) = NULL;
2018 mutex_lock(&key_tfm_list_mutex);
2019 list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) {
2020 if (strcmp(key_tfm->cipher_name, cipher_name) == 0) {
2021 (*tfm) = key_tfm->key_tfm;
2022 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
2023 mutex_unlock(&key_tfm_list_mutex);
2024 goto out;
2025 }
2026 }
2027 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrow5dda6992007-10-16 01:28:06 -07002028 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
2029 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07002030 printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n",
2031 rc);
2032 goto out;
2033 }
2034 (*tfm) = key_tfm->key_tfm;
2035 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
2036out:
2037 return rc;
2038}