2 * eCryptfs: Linux filesystem encryption layer
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
6 * Copyright (C) 2004-2006 International Business Machines Corp.
7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>
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.
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.
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
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"
39 ecryptfs_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,
44 ecryptfs_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,
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
56 void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
60 for (x = 0; x < src_size; x++)
61 sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
66 * @dst: Buffer to take the bytes from src hex; must be at least of
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
71 void ecryptfs_from_hex(char *dst, char *src, int dst_size)
76 for (x = 0; x < dst_size; x++) {
78 tmp[1] = src[x * 2 + 1];
79 dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
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
90 * Uses the allocated crypto context that crypt_stat references to
91 * generate the MD5 sum of the contents of src.
93 static int ecryptfs_calculate_md5(char *dst,
94 struct ecryptfs_crypt_stat *crypt_stat,
97 struct scatterlist sg;
98 struct hash_desc desc = {
99 .tfm = crypt_stat->hash_tfm,
100 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
104 mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
105 sg_init_one(&sg, (u8 *)src, len);
107 desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
109 if (IS_ERR(desc.tfm)) {
110 rc = PTR_ERR(desc.tfm);
111 ecryptfs_printk(KERN_ERR, "Error attempting to "
112 "allocate crypto context; rc = [%d]\n",
116 crypt_stat->hash_tfm = desc.tfm;
118 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);
128 * @iv: destination for the derived iv vale
129 * @crypt_stat: Pointer to crypt_stat struct for the current inode
130 * @offset: Offset of the page whose's iv we are to derive
132 * Generate the initialization vector from the given root IV and page
135 * Returns zero on success; non-zero on error.
137 static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
141 char dst[MD5_DIGEST_SIZE];
142 char src[ECRYPTFS_MAX_IV_BYTES + 16];
144 if (unlikely(ecryptfs_verbosity > 0)) {
145 ecryptfs_printk(KERN_DEBUG, "root iv:\n");
146 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
148 /* TODO: It is probably secure to just cast the least
149 * significant bits of the root IV into an unsigned long and
150 * add the offset to that rather than go through all this
151 * hashing business. -Halcrow */
152 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
153 memset((src + crypt_stat->iv_bytes), 0, 16);
154 snprintf((src + crypt_stat->iv_bytes), 16, "%ld", offset);
155 if (unlikely(ecryptfs_verbosity > 0)) {
156 ecryptfs_printk(KERN_DEBUG, "source:\n");
157 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
159 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
160 (crypt_stat->iv_bytes + 16));
162 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
163 "MD5 while generating IV for a page\n");
166 memcpy(iv, dst, crypt_stat->iv_bytes);
167 if (unlikely(ecryptfs_verbosity > 0)) {
168 ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
169 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
176 * ecryptfs_init_crypt_stat
177 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
179 * Initialize the crypt_stat structure.
182 ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
184 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
185 mutex_init(&crypt_stat->cs_mutex);
186 mutex_init(&crypt_stat->cs_tfm_mutex);
187 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
188 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_STRUCT_INITIALIZED);
192 * ecryptfs_destruct_crypt_stat
193 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
195 * Releases all memory associated with a crypt_stat struct.
197 void ecryptfs_destruct_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
200 crypto_free_tfm(crypt_stat->tfm);
201 if (crypt_stat->hash_tfm)
202 crypto_free_hash(crypt_stat->hash_tfm);
203 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
206 void ecryptfs_destruct_mount_crypt_stat(
207 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
209 if (mount_crypt_stat->global_auth_tok_key)
210 key_put(mount_crypt_stat->global_auth_tok_key);
211 if (mount_crypt_stat->global_key_tfm)
212 crypto_free_tfm(mount_crypt_stat->global_key_tfm);
213 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
217 * virt_to_scatterlist
218 * @addr: Virtual address
219 * @size: Size of data; should be an even multiple of the block size
220 * @sg: Pointer to scatterlist array; set to NULL to obtain only
221 * the number of scatterlist structs required in array
222 * @sg_size: Max array size
224 * Fills in a scatterlist array with page references for a passed
227 * Returns the number of scatterlist structs in array used
229 int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
235 int remainder_of_page;
237 while (size > 0 && i < sg_size) {
238 pg = virt_to_page(addr);
239 offset = offset_in_page(addr);
242 sg[i].offset = offset;
244 remainder_of_page = PAGE_CACHE_SIZE - offset;
245 if (size >= remainder_of_page) {
247 sg[i].length = remainder_of_page;
248 addr += remainder_of_page;
249 size -= remainder_of_page;
264 * encrypt_scatterlist
265 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
266 * @dest_sg: Destination of encrypted data
267 * @src_sg: Data to be encrypted
268 * @size: Length of data to be encrypted
269 * @iv: iv to use during encryption
271 * Returns the number of bytes encrypted; negative value on error
273 static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
274 struct scatterlist *dest_sg,
275 struct scatterlist *src_sg, int size,
280 BUG_ON(!crypt_stat || !crypt_stat->tfm
281 || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
282 ECRYPTFS_STRUCT_INITIALIZED));
283 if (unlikely(ecryptfs_verbosity > 0)) {
284 ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
285 crypt_stat->key_size);
286 ecryptfs_dump_hex(crypt_stat->key,
287 crypt_stat->key_size);
289 /* Consider doing this once, when the file is opened */
290 mutex_lock(&crypt_stat->cs_tfm_mutex);
291 rc = crypto_cipher_setkey(crypt_stat->tfm, crypt_stat->key,
292 crypt_stat->key_size);
294 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
296 mutex_unlock(&crypt_stat->cs_tfm_mutex);
300 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
301 crypto_cipher_encrypt_iv(crypt_stat->tfm, dest_sg, src_sg, size, iv);
302 mutex_unlock(&crypt_stat->cs_tfm_mutex);
308 ecryptfs_extent_to_lwr_pg_idx_and_offset(unsigned long *lower_page_idx,
310 struct ecryptfs_crypt_stat *crypt_stat,
311 unsigned long extent_num)
313 unsigned long lower_extent_num;
314 int extents_occupied_by_headers_at_front;
315 int bytes_occupied_by_headers_at_front;
317 int extents_per_page;
319 bytes_occupied_by_headers_at_front =
320 ( crypt_stat->header_extent_size
321 * crypt_stat->num_header_extents_at_front );
322 extents_occupied_by_headers_at_front =
323 ( bytes_occupied_by_headers_at_front
324 / crypt_stat->extent_size );
325 lower_extent_num = extents_occupied_by_headers_at_front + extent_num;
326 extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
327 (*lower_page_idx) = lower_extent_num / extents_per_page;
328 extent_offset = lower_extent_num % extents_per_page;
329 (*byte_offset) = extent_offset * crypt_stat->extent_size;
330 ecryptfs_printk(KERN_DEBUG, " * crypt_stat->header_extent_size = "
331 "[%d]\n", crypt_stat->header_extent_size);
332 ecryptfs_printk(KERN_DEBUG, " * crypt_stat->"
333 "num_header_extents_at_front = [%d]\n",
334 crypt_stat->num_header_extents_at_front);
335 ecryptfs_printk(KERN_DEBUG, " * extents_occupied_by_headers_at_"
336 "front = [%d]\n", extents_occupied_by_headers_at_front);
337 ecryptfs_printk(KERN_DEBUG, " * lower_extent_num = [0x%.16x]\n",
339 ecryptfs_printk(KERN_DEBUG, " * extents_per_page = [%d]\n",
341 ecryptfs_printk(KERN_DEBUG, " * (*lower_page_idx) = [0x%.16x]\n",
343 ecryptfs_printk(KERN_DEBUG, " * extent_offset = [%d]\n",
345 ecryptfs_printk(KERN_DEBUG, " * (*byte_offset) = [%d]\n",
349 static int ecryptfs_write_out_page(struct ecryptfs_page_crypt_context *ctx,
350 struct page *lower_page,
351 struct inode *lower_inode,
352 int byte_offset_in_page, int bytes_to_write)
356 if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
357 rc = ecryptfs_commit_lower_page(lower_page, lower_inode,
358 ctx->param.lower_file,
362 ecryptfs_printk(KERN_ERR, "Error calling lower "
363 "commit; rc = [%d]\n", rc);
367 rc = ecryptfs_writepage_and_release_lower_page(lower_page,
371 ecryptfs_printk(KERN_ERR, "Error calling lower "
372 "writepage(); rc = [%d]\n", rc);
380 static int ecryptfs_read_in_page(struct ecryptfs_page_crypt_context *ctx,
381 struct page **lower_page,
382 struct inode *lower_inode,
383 unsigned long lower_page_idx,
384 int byte_offset_in_page)
388 if (ctx->mode == ECRYPTFS_PREPARE_COMMIT_MODE) {
389 /* TODO: Limit this to only the data extents that are
391 rc = ecryptfs_get_lower_page(lower_page, lower_inode,
392 ctx->param.lower_file,
396 - byte_offset_in_page));
399 KERN_ERR, "Error attempting to grab, map, "
400 "and prepare_write lower page with index "
401 "[0x%.16x]; rc = [%d]\n", lower_page_idx, rc);
405 rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL,
410 KERN_ERR, "Error attempting to grab and map "
411 "lower page with index [0x%.16x]; rc = [%d]\n",
421 * ecryptfs_encrypt_page
422 * @ctx: The context of the page
424 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
425 * that eCryptfs pages may straddle the lower pages -- for instance,
426 * if the file was created on a machine with an 8K page size
427 * (resulting in an 8K header), and then the file is copied onto a
428 * host with a 32K page size, then when reading page 0 of the eCryptfs
429 * file, 24K of page 0 of the lower file will be read and decrypted,
430 * and then 8K of page 1 of the lower file will be read and decrypted.
432 * The actual operations performed on each page depends on the
433 * contents of the ecryptfs_page_crypt_context struct.
435 * Returns zero on success; negative on error
437 int ecryptfs_encrypt_page(struct ecryptfs_page_crypt_context *ctx)
439 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
440 unsigned long base_extent;
441 unsigned long extent_offset = 0;
442 unsigned long lower_page_idx = 0;
443 unsigned long prior_lower_page_idx = 0;
444 struct page *lower_page;
445 struct inode *lower_inode;
446 struct ecryptfs_inode_info *inode_info;
447 struct ecryptfs_crypt_stat *crypt_stat;
449 int lower_byte_offset = 0;
450 int orig_byte_offset = 0;
451 int num_extents_per_page;
452 #define ECRYPTFS_PAGE_STATE_UNREAD 0
453 #define ECRYPTFS_PAGE_STATE_READ 1
454 #define ECRYPTFS_PAGE_STATE_MODIFIED 2
455 #define ECRYPTFS_PAGE_STATE_WRITTEN 3
458 lower_inode = ecryptfs_inode_to_lower(ctx->page->mapping->host);
459 inode_info = ecryptfs_inode_to_private(ctx->page->mapping->host);
460 crypt_stat = &inode_info->crypt_stat;
461 if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)) {
462 rc = ecryptfs_copy_page_to_lower(ctx->page, lower_inode,
463 ctx->param.lower_file);
465 ecryptfs_printk(KERN_ERR, "Error attempting to copy "
466 "page at index [0x%.16x]\n",
470 num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
471 base_extent = (ctx->page->index * num_extents_per_page);
472 page_state = ECRYPTFS_PAGE_STATE_UNREAD;
473 while (extent_offset < num_extents_per_page) {
474 ecryptfs_extent_to_lwr_pg_idx_and_offset(
475 &lower_page_idx, &lower_byte_offset, crypt_stat,
476 (base_extent + extent_offset));
477 if (prior_lower_page_idx != lower_page_idx
478 && page_state == ECRYPTFS_PAGE_STATE_MODIFIED) {
479 rc = ecryptfs_write_out_page(ctx, lower_page,
483 - orig_byte_offset));
485 ecryptfs_printk(KERN_ERR, "Error attempting "
486 "to write out page; rc = [%d]"
490 page_state = ECRYPTFS_PAGE_STATE_WRITTEN;
492 if (page_state == ECRYPTFS_PAGE_STATE_UNREAD
493 || page_state == ECRYPTFS_PAGE_STATE_WRITTEN) {
494 rc = ecryptfs_read_in_page(ctx, &lower_page,
495 lower_inode, lower_page_idx,
498 ecryptfs_printk(KERN_ERR, "Error attempting "
499 "to read in lower page with "
500 "index [0x%.16x]; rc = [%d]\n",
504 orig_byte_offset = lower_byte_offset;
505 prior_lower_page_idx = lower_page_idx;
506 page_state = ECRYPTFS_PAGE_STATE_READ;
508 BUG_ON(!(page_state == ECRYPTFS_PAGE_STATE_MODIFIED
509 || page_state == ECRYPTFS_PAGE_STATE_READ));
510 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
511 (base_extent + extent_offset));
513 ecryptfs_printk(KERN_ERR, "Error attempting to "
514 "derive IV for extent [0x%.16x]; "
516 (base_extent + extent_offset), rc);
519 if (unlikely(ecryptfs_verbosity > 0)) {
520 ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
522 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
523 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
525 ecryptfs_dump_hex((char *)
526 (page_address(ctx->page)
528 * crypt_stat->extent_size)), 8);
530 rc = ecryptfs_encrypt_page_offset(
531 crypt_stat, lower_page, lower_byte_offset, ctx->page,
532 (extent_offset * crypt_stat->extent_size),
533 crypt_stat->extent_size, extent_iv);
534 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
536 (base_extent + extent_offset), rc);
537 if (unlikely(ecryptfs_verbosity > 0)) {
538 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
540 ecryptfs_dump_hex((char *)(page_address(lower_page)
541 + lower_byte_offset), 8);
543 page_state = ECRYPTFS_PAGE_STATE_MODIFIED;
546 BUG_ON(orig_byte_offset != 0);
547 rc = ecryptfs_write_out_page(ctx, lower_page, lower_inode, 0,
549 + crypt_stat->extent_size));
551 ecryptfs_printk(KERN_ERR, "Error attempting to write out "
552 "page; rc = [%d]\n", rc);
560 * ecryptfs_decrypt_page
561 * @file: The ecryptfs file
562 * @page: The page in ecryptfs to decrypt
564 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
565 * that eCryptfs pages may straddle the lower pages -- for instance,
566 * if the file was created on a machine with an 8K page size
567 * (resulting in an 8K header), and then the file is copied onto a
568 * host with a 32K page size, then when reading page 0 of the eCryptfs
569 * file, 24K of page 0 of the lower file will be read and decrypted,
570 * and then 8K of page 1 of the lower file will be read and decrypted.
572 * Returns zero on success; negative on error
574 int ecryptfs_decrypt_page(struct file *file, struct page *page)
576 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
577 unsigned long base_extent;
578 unsigned long extent_offset = 0;
579 unsigned long lower_page_idx = 0;
580 unsigned long prior_lower_page_idx = 0;
581 struct page *lower_page;
582 char *lower_page_virt = NULL;
583 struct inode *lower_inode;
584 struct ecryptfs_crypt_stat *crypt_stat;
587 int num_extents_per_page;
590 crypt_stat = &(ecryptfs_inode_to_private(
591 page->mapping->host)->crypt_stat);
592 lower_inode = ecryptfs_inode_to_lower(page->mapping->host);
593 if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)) {
594 rc = ecryptfs_do_readpage(file, page, page->index);
596 ecryptfs_printk(KERN_ERR, "Error attempting to copy "
597 "page at index [0x%.16x]\n",
601 num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size;
602 base_extent = (page->index * num_extents_per_page);
603 lower_page_virt = kmem_cache_alloc(ecryptfs_lower_page_cache,
605 if (!lower_page_virt) {
607 ecryptfs_printk(KERN_ERR, "Error getting page for encrypted "
611 lower_page = virt_to_page(lower_page_virt);
612 page_state = ECRYPTFS_PAGE_STATE_UNREAD;
613 while (extent_offset < num_extents_per_page) {
614 ecryptfs_extent_to_lwr_pg_idx_and_offset(
615 &lower_page_idx, &byte_offset, crypt_stat,
616 (base_extent + extent_offset));
617 if (prior_lower_page_idx != lower_page_idx
618 || page_state == ECRYPTFS_PAGE_STATE_UNREAD) {
619 rc = ecryptfs_do_readpage(file, lower_page,
622 ecryptfs_printk(KERN_ERR, "Error reading "
623 "lower encrypted page; rc = "
627 prior_lower_page_idx = lower_page_idx;
628 page_state = ECRYPTFS_PAGE_STATE_READ;
630 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
631 (base_extent + extent_offset));
633 ecryptfs_printk(KERN_ERR, "Error attempting to "
634 "derive IV for extent [0x%.16x]; rc = "
636 (base_extent + extent_offset), rc);
639 if (unlikely(ecryptfs_verbosity > 0)) {
640 ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
642 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
643 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
645 ecryptfs_dump_hex((lower_page_virt + byte_offset), 8);
647 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
649 * crypt_stat->extent_size),
650 lower_page, byte_offset,
651 crypt_stat->extent_size,
653 if (rc != crypt_stat->extent_size) {
654 ecryptfs_printk(KERN_ERR, "Error attempting to "
655 "decrypt extent [0x%.16x]\n",
656 (base_extent + extent_offset));
660 if (unlikely(ecryptfs_verbosity > 0)) {
661 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
663 ecryptfs_dump_hex((char *)(page_address(page)
670 kmem_cache_free(ecryptfs_lower_page_cache, lower_page_virt);
675 * decrypt_scatterlist
677 * Returns the number of bytes decrypted; negative value on error
679 static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
680 struct scatterlist *dest_sg,
681 struct scatterlist *src_sg, int size,
686 /* Consider doing this once, when the file is opened */
687 mutex_lock(&crypt_stat->cs_tfm_mutex);
688 rc = crypto_cipher_setkey(crypt_stat->tfm, crypt_stat->key,
689 crypt_stat->key_size);
691 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
693 mutex_unlock(&crypt_stat->cs_tfm_mutex);
697 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
698 rc = crypto_cipher_decrypt_iv(crypt_stat->tfm, dest_sg, src_sg, size,
700 mutex_unlock(&crypt_stat->cs_tfm_mutex);
702 ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
712 * ecryptfs_encrypt_page_offset
714 * Returns the number of bytes encrypted
717 ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
718 struct page *dst_page, int dst_offset,
719 struct page *src_page, int src_offset, int size,
722 struct scatterlist src_sg, dst_sg;
724 src_sg.page = src_page;
725 src_sg.offset = src_offset;
726 src_sg.length = size;
727 dst_sg.page = dst_page;
728 dst_sg.offset = dst_offset;
729 dst_sg.length = size;
730 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
734 * ecryptfs_decrypt_page_offset
736 * Returns the number of bytes decrypted
739 ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
740 struct page *dst_page, int dst_offset,
741 struct page *src_page, int src_offset, int size,
744 struct scatterlist src_sg, dst_sg;
746 src_sg.page = src_page;
747 src_sg.offset = src_offset;
748 src_sg.length = size;
749 dst_sg.page = dst_page;
750 dst_sg.offset = dst_offset;
751 dst_sg.length = size;
752 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
755 #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
758 * ecryptfs_init_crypt_ctx
759 * @crypt_stat: Uninitilized crypt stats structure
761 * Initialize the crypto context.
763 * TODO: Performance: Keep a cache of initialized cipher contexts;
764 * only init if needed
766 int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
770 if (!crypt_stat->cipher) {
771 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
774 ecryptfs_printk(KERN_DEBUG,
775 "Initializing cipher [%s]; strlen = [%d]; "
776 "key_size_bits = [%d]\n",
777 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
778 crypt_stat->key_size << 3);
779 if (crypt_stat->tfm) {
783 mutex_lock(&crypt_stat->cs_tfm_mutex);
784 crypt_stat->tfm = crypto_alloc_tfm(crypt_stat->cipher,
785 ECRYPTFS_DEFAULT_CHAINING_MODE
786 | CRYPTO_TFM_REQ_WEAK_KEY);
787 mutex_unlock(&crypt_stat->cs_tfm_mutex);
788 if (!crypt_stat->tfm) {
789 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
790 "Error initializing cipher [%s]\n",
799 static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
803 crypt_stat->extent_mask = 0xFFFFFFFF;
804 crypt_stat->extent_shift = 0;
805 if (crypt_stat->extent_size == 0)
807 extent_size_tmp = crypt_stat->extent_size;
808 while ((extent_size_tmp & 0x01) == 0) {
809 extent_size_tmp >>= 1;
810 crypt_stat->extent_mask <<= 1;
811 crypt_stat->extent_shift++;
815 void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
817 /* Default values; may be overwritten as we are parsing the
819 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
820 set_extent_mask_and_shift(crypt_stat);
821 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
822 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
823 crypt_stat->header_extent_size =
824 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
826 crypt_stat->header_extent_size = PAGE_CACHE_SIZE;
827 crypt_stat->num_header_extents_at_front = 1;
831 * ecryptfs_compute_root_iv
834 * On error, sets the root IV to all 0's.
836 int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
839 char dst[MD5_DIGEST_SIZE];
841 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
842 BUG_ON(crypt_stat->iv_bytes <= 0);
843 if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID)) {
845 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
846 "cannot generate root IV\n");
849 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
850 crypt_stat->key_size);
852 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
853 "MD5 while generating root IV\n");
856 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
859 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
860 ECRYPTFS_SET_FLAG(crypt_stat->flags,
861 ECRYPTFS_SECURITY_WARNING);
866 static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
868 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
869 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
870 ecryptfs_compute_root_iv(crypt_stat);
871 if (unlikely(ecryptfs_verbosity > 0)) {
872 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
873 ecryptfs_dump_hex(crypt_stat->key,
874 crypt_stat->key_size);
879 * ecryptfs_set_default_crypt_stat_vals
882 * Default values in the event that policy does not override them.
884 static void ecryptfs_set_default_crypt_stat_vals(
885 struct ecryptfs_crypt_stat *crypt_stat,
886 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
888 ecryptfs_set_default_sizes(crypt_stat);
889 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
890 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
891 ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
892 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
893 crypt_stat->mount_crypt_stat = mount_crypt_stat;
897 * ecryptfs_new_file_context
900 * If the crypto context for the file has not yet been established,
901 * this is where we do that. Establishing a new crypto context
902 * involves the following decisions:
903 * - What cipher to use?
904 * - What set of authentication tokens to use?
905 * Here we just worry about getting enough information into the
906 * authentication tokens so that we know that they are available.
907 * We associate the available authentication tokens with the new file
908 * via the set of signatures in the crypt_stat struct. Later, when
909 * the headers are actually written out, we may again defer to
910 * userspace to perform the encryption of the session key; for the
911 * foreseeable future, this will be the case with public key packets.
913 * Returns zero on success; non-zero otherwise
915 /* Associate an authentication token(s) with the file */
916 int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
919 struct ecryptfs_crypt_stat *crypt_stat =
920 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
921 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
922 &ecryptfs_superblock_to_private(
923 ecryptfs_dentry->d_sb)->mount_crypt_stat;
926 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
927 /* See if there are mount crypt options */
928 if (mount_crypt_stat->global_auth_tok) {
929 ecryptfs_printk(KERN_DEBUG, "Initializing context for new "
930 "file using mount_crypt_stat\n");
931 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
932 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
933 memcpy(crypt_stat->keysigs[crypt_stat->num_keysigs++],
934 mount_crypt_stat->global_auth_tok_sig,
935 ECRYPTFS_SIG_SIZE_HEX);
937 strlen(mount_crypt_stat->global_default_cipher_name);
938 memcpy(crypt_stat->cipher,
939 mount_crypt_stat->global_default_cipher_name,
941 crypt_stat->cipher[cipher_name_len] = '\0';
942 crypt_stat->key_size =
943 mount_crypt_stat->global_default_cipher_key_size;
944 ecryptfs_generate_new_key(crypt_stat);
946 /* We should not encounter this scenario since we
947 * should detect lack of global_auth_tok at mount time
948 * TODO: Applies to 0.1 release only; remove in future
951 rc = ecryptfs_init_crypt_ctx(crypt_stat);
953 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
954 "context for cipher [%s]: rc = [%d]\n",
955 crypt_stat->cipher, rc);
960 * contains_ecryptfs_marker - check for the ecryptfs marker
961 * @data: The data block in which to check
963 * Returns one if marker found; zero if not found
965 int contains_ecryptfs_marker(char *data)
969 memcpy(&m_1, data, 4);
970 m_1 = be32_to_cpu(m_1);
971 memcpy(&m_2, (data + 4), 4);
972 m_2 = be32_to_cpu(m_2);
973 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
975 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
976 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
977 MAGIC_ECRYPTFS_MARKER);
978 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
979 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
983 struct ecryptfs_flag_map_elem {
988 /* Add support for additional flags by adding elements here. */
989 static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
990 {0x00000001, ECRYPTFS_ENABLE_HMAC},
991 {0x00000002, ECRYPTFS_ENCRYPTED}
995 * ecryptfs_process_flags
997 * @page_virt: Source data to be parsed
998 * @bytes_read: Updated with the number of bytes read
1000 * Returns zero on success; non-zero if the flag set is invalid
1002 static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1003 char *page_virt, int *bytes_read)
1009 memcpy(&flags, page_virt, 4);
1010 flags = be32_to_cpu(flags);
1011 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1012 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1013 if (flags & ecryptfs_flag_map[i].file_flag) {
1014 ECRYPTFS_SET_FLAG(crypt_stat->flags,
1015 ecryptfs_flag_map[i].local_flag);
1017 ECRYPTFS_CLEAR_FLAG(crypt_stat->flags,
1018 ecryptfs_flag_map[i].local_flag);
1019 /* Version is in top 8 bits of the 32-bit flag vector */
1020 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1026 * write_ecryptfs_marker
1027 * @page_virt: The pointer to in a page to begin writing the marker
1028 * @written: Number of bytes written
1030 * Marker = 0x3c81b7f5
1032 static void write_ecryptfs_marker(char *page_virt, size_t *written)
1036 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1037 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1038 m_1 = cpu_to_be32(m_1);
1039 memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1040 m_2 = cpu_to_be32(m_2);
1041 memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
1042 (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1043 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1047 write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1053 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1054 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1055 if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
1056 ecryptfs_flag_map[i].local_flag))
1057 flags |= ecryptfs_flag_map[i].file_flag;
1058 /* Version is in top 8 bits of the 32-bit flag vector */
1059 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1060 flags = cpu_to_be32(flags);
1061 memcpy(page_virt, &flags, 4);
1065 struct ecryptfs_cipher_code_str_map_elem {
1066 char cipher_str[16];
1070 /* Add support for additional ciphers by adding elements here. The
1071 * cipher_code is whatever OpenPGP applicatoins use to identify the
1072 * ciphers. List in order of probability. */
1073 static struct ecryptfs_cipher_code_str_map_elem
1074 ecryptfs_cipher_code_str_map[] = {
1075 {"aes",RFC2440_CIPHER_AES_128 },
1076 {"blowfish", RFC2440_CIPHER_BLOWFISH},
1077 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
1078 {"cast5", RFC2440_CIPHER_CAST_5},
1079 {"twofish", RFC2440_CIPHER_TWOFISH},
1080 {"cast6", RFC2440_CIPHER_CAST_6},
1081 {"aes", RFC2440_CIPHER_AES_192},
1082 {"aes", RFC2440_CIPHER_AES_256}
1086 * ecryptfs_code_for_cipher_string
1087 * @str: The string representing the cipher name
1089 * Returns zero on no match, or the cipher code on match
1091 u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
1095 struct ecryptfs_cipher_code_str_map_elem *map =
1096 ecryptfs_cipher_code_str_map;
1098 if (strcmp(crypt_stat->cipher, "aes") == 0) {
1099 switch (crypt_stat->key_size) {
1101 code = RFC2440_CIPHER_AES_128;
1104 code = RFC2440_CIPHER_AES_192;
1107 code = RFC2440_CIPHER_AES_256;
1110 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1111 if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
1112 code = map[i].cipher_code;
1120 * ecryptfs_cipher_code_to_string
1121 * @str: Destination to write out the cipher name
1122 * @cipher_code: The code to convert to cipher name string
1124 * Returns zero on success
1126 int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
1132 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1133 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1134 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1135 if (str[0] == '\0') {
1136 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1137 "[%d]\n", cipher_code);
1144 * ecryptfs_read_header_region
1149 * Returns zero on success; non-zero otherwise
1151 int ecryptfs_read_header_region(char *data, struct dentry *dentry,
1152 struct vfsmount *mnt)
1159 file = dentry_open(dentry, mnt, O_RDONLY);
1161 ecryptfs_printk(KERN_DEBUG, "Error opening file to "
1162 "read header region\n");
1170 /* For releases 0.1 and 0.2, all of the header information
1171 * fits in the first data extent-sized region. */
1172 rc = file->f_op->read(file, (char __user *)data,
1173 ECRYPTFS_DEFAULT_EXTENT_SIZE, &file->f_pos);
1182 write_header_metadata(char *virt, struct ecryptfs_crypt_stat *crypt_stat,
1185 u32 header_extent_size;
1186 u16 num_header_extents_at_front;
1188 header_extent_size = (u32)crypt_stat->header_extent_size;
1189 num_header_extents_at_front =
1190 (u16)crypt_stat->num_header_extents_at_front;
1191 header_extent_size = cpu_to_be32(header_extent_size);
1192 memcpy(virt, &header_extent_size, 4);
1194 num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
1195 memcpy(virt, &num_header_extents_at_front, 2);
1199 struct kmem_cache *ecryptfs_header_cache_0;
1200 struct kmem_cache *ecryptfs_header_cache_1;
1201 struct kmem_cache *ecryptfs_header_cache_2;
1204 * ecryptfs_write_headers_virt
1212 * Octets 0-7: Unencrypted file size (big-endian)
1213 * Octets 8-15: eCryptfs special marker
1214 * Octets 16-19: Flags
1215 * Octet 16: File format version number (between 0 and 255)
1216 * Octets 17-18: Reserved
1217 * Octet 19: Bit 1 (lsb): Reserved
1219 * Bits 3-8: Reserved
1220 * Octets 20-23: Header extent size (big-endian)
1221 * Octets 24-25: Number of header extents at front of file
1223 * Octet 26: Begin RFC 2440 authentication token packet set
1225 * Lower data (CBC encrypted)
1227 * Lower data (CBC encrypted)
1230 * Returns zero on success
1232 int ecryptfs_write_headers_virt(char *page_virt,
1233 struct ecryptfs_crypt_stat *crypt_stat,
1234 struct dentry *ecryptfs_dentry)
1240 offset = ECRYPTFS_FILE_SIZE_BYTES;
1241 write_ecryptfs_marker((page_virt + offset), &written);
1243 write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1245 write_header_metadata((page_virt + offset), crypt_stat, &written);
1247 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1248 ecryptfs_dentry, &written,
1249 PAGE_CACHE_SIZE - offset);
1251 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1252 "set; rc = [%d]\n", rc);
1257 * ecryptfs_write_headers
1258 * @lower_file: The lower file struct, which was returned from dentry_open
1260 * Write the file headers out. This will likely involve a userspace
1261 * callout, in which the session key is encrypted with one or more
1262 * public keys and/or the passphrase necessary to do the encryption is
1263 * retrieved via a prompt. Exactly what happens at this point should
1264 * be policy-dependent.
1266 * Returns zero on success; non-zero on error
1268 int ecryptfs_write_headers(struct dentry *ecryptfs_dentry,
1269 struct file *lower_file)
1272 struct ecryptfs_crypt_stat *crypt_stat;
1274 int current_header_page;
1278 crypt_stat = &ecryptfs_inode_to_private(
1279 ecryptfs_dentry->d_inode)->crypt_stat;
1280 if (likely(ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
1281 ECRYPTFS_ENCRYPTED))) {
1282 if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags,
1283 ECRYPTFS_KEY_VALID)) {
1284 ecryptfs_printk(KERN_DEBUG, "Key is "
1285 "invalid; bailing out\n");
1291 ecryptfs_printk(KERN_WARNING,
1292 "Called with crypt_stat->encrypted == 0\n");
1295 /* Released in this function */
1296 page_virt = kmem_cache_alloc(ecryptfs_header_cache_0, SLAB_USER);
1298 ecryptfs_printk(KERN_ERR, "Out of memory\n");
1302 memset(page_virt, 0, PAGE_CACHE_SIZE);
1303 rc = ecryptfs_write_headers_virt(page_virt, crypt_stat,
1306 ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
1307 memset(page_virt, 0, PAGE_CACHE_SIZE);
1310 ecryptfs_printk(KERN_DEBUG,
1311 "Writing key packet set to underlying file\n");
1312 lower_file->f_pos = 0;
1315 ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->"
1316 "write() w/ header page; lower_file->f_pos = "
1317 "[0x%.16x]\n", lower_file->f_pos);
1318 lower_file->f_op->write(lower_file, (char __user *)page_virt,
1319 PAGE_CACHE_SIZE, &lower_file->f_pos);
1320 header_pages = ((crypt_stat->header_extent_size
1321 * crypt_stat->num_header_extents_at_front)
1323 memset(page_virt, 0, PAGE_CACHE_SIZE);
1324 current_header_page = 1;
1325 while (current_header_page < header_pages) {
1326 ecryptfs_printk(KERN_DEBUG, "Calling lower_file->f_op->"
1327 "write() w/ zero'd page; lower_file->f_pos = "
1328 "[0x%.16x]\n", lower_file->f_pos);
1329 lower_file->f_op->write(lower_file, (char __user *)page_virt,
1330 PAGE_CACHE_SIZE, &lower_file->f_pos);
1331 current_header_page++;
1334 ecryptfs_printk(KERN_DEBUG,
1335 "Done writing key packet set to underlying file.\n");
1337 kmem_cache_free(ecryptfs_header_cache_0, page_virt);
1342 static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
1343 char *virt, int *bytes_read)
1346 u32 header_extent_size;
1347 u16 num_header_extents_at_front;
1349 memcpy(&header_extent_size, virt, 4);
1350 header_extent_size = be32_to_cpu(header_extent_size);
1352 memcpy(&num_header_extents_at_front, virt, 2);
1353 num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
1354 crypt_stat->header_extent_size = (int)header_extent_size;
1355 crypt_stat->num_header_extents_at_front =
1356 (int)num_header_extents_at_front;
1358 if ((crypt_stat->header_extent_size
1359 * crypt_stat->num_header_extents_at_front)
1360 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) {
1362 ecryptfs_printk(KERN_WARNING, "Invalid header extent size: "
1363 "[%d]\n", crypt_stat->header_extent_size);
1369 * set_default_header_data
1371 * For version 0 file format; this function is only for backwards
1372 * compatibility for files created with the prior versions of
1375 static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1377 crypt_stat->header_extent_size = 4096;
1378 crypt_stat->num_header_extents_at_front = 1;
1382 * ecryptfs_read_headers_virt
1384 * Read/parse the header data. The header format is detailed in the
1385 * comment block for the ecryptfs_write_headers_virt() function.
1387 * Returns zero on success
1389 static int ecryptfs_read_headers_virt(char *page_virt,
1390 struct ecryptfs_crypt_stat *crypt_stat,
1391 struct dentry *ecryptfs_dentry)
1397 ecryptfs_set_default_sizes(crypt_stat);
1398 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1399 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1400 offset = ECRYPTFS_FILE_SIZE_BYTES;
1401 rc = contains_ecryptfs_marker(page_virt + offset);
1406 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1407 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1410 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1413 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1414 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1415 "file version [%d] is supported by this "
1416 "version of eCryptfs\n",
1417 crypt_stat->file_version,
1418 ECRYPTFS_SUPPORTED_FILE_VERSION);
1422 offset += bytes_read;
1423 if (crypt_stat->file_version >= 1) {
1424 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1427 ecryptfs_printk(KERN_WARNING, "Error reading header "
1428 "metadata; rc = [%d]\n", rc);
1430 offset += bytes_read;
1432 set_default_header_data(crypt_stat);
1433 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1440 * ecryptfs_read_headers
1442 * Returns zero if valid headers found and parsed; non-zero otherwise
1444 int ecryptfs_read_headers(struct dentry *ecryptfs_dentry,
1445 struct file *lower_file)
1448 char *page_virt = NULL;
1451 struct ecryptfs_crypt_stat *crypt_stat =
1452 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1454 /* Read the first page from the underlying file */
1455 page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, SLAB_USER);
1458 ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n");
1461 lower_file->f_pos = 0;
1464 bytes_read = lower_file->f_op->read(lower_file,
1465 (char __user *)page_virt,
1466 ECRYPTFS_DEFAULT_EXTENT_SIZE,
1467 &lower_file->f_pos);
1469 if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) {
1473 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1476 ecryptfs_printk(KERN_DEBUG, "Valid eCryptfs headers not "
1482 memset(page_virt, 0, PAGE_CACHE_SIZE);
1483 kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1489 * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1490 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1491 * @name: The plaintext name
1492 * @length: The length of the plaintext
1493 * @encoded_name: The encypted name
1495 * Encrypts and encodes a filename into something that constitutes a
1496 * valid filename for a filesystem, with printable characters.
1498 * We assume that we have a properly initialized crypto context,
1499 * pointed to by crypt_stat->tfm.
1501 * TODO: Implement filename decoding and decryption here, in place of
1502 * memcpy. We are keeping the framework around for now to (1)
1503 * facilitate testing of the components needed to implement filename
1504 * encryption and (2) to provide a code base from which other
1505 * developers in the community can easily implement this feature.
1507 * Returns the length of encoded filename; negative if error
1510 ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1511 const char *name, int length, char **encoded_name)
1515 (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
1516 if (!(*encoded_name)) {
1520 /* TODO: Filename encryption is a scheduled feature for a
1521 * future version of eCryptfs. This function is here only for
1522 * the purpose of providing a framework for other developers
1523 * to easily implement filename encryption. Hint: Replace this
1524 * memcpy() with a call to encrypt and encode the
1525 * filename, the set the length accordingly. */
1526 memcpy((void *)(*encoded_name), (void *)name, length);
1527 (*encoded_name)[length] = '\0';
1534 * ecryptfs_decode_filename - converts the cipher text name to plaintext
1535 * @crypt_stat: The crypt_stat struct associated with the file
1536 * @name: The filename in cipher text
1537 * @length: The length of the cipher text name
1538 * @decrypted_name: The plaintext name
1540 * Decodes and decrypts the filename.
1542 * We assume that we have a properly initialized crypto context,
1543 * pointed to by crypt_stat->tfm.
1545 * TODO: Implement filename decoding and decryption here, in place of
1546 * memcpy. We are keeping the framework around for now to (1)
1547 * facilitate testing of the components needed to implement filename
1548 * encryption and (2) to provide a code base from which other
1549 * developers in the community can easily implement this feature.
1551 * Returns the length of decoded filename; negative if error
1554 ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1555 const char *name, int length, char **decrypted_name)
1559 (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
1560 if (!(*decrypted_name)) {
1564 /* TODO: Filename encryption is a scheduled feature for a
1565 * future version of eCryptfs. This function is here only for
1566 * the purpose of providing a framework for other developers
1567 * to easily implement filename encryption. Hint: Replace this
1568 * memcpy() with a call to decode and decrypt the
1569 * filename, the set the length accordingly. */
1570 memcpy((void *)(*decrypted_name), (void *)name, length);
1571 (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
1572 * in printing out the
1581 * ecryptfs_process_cipher - Perform cipher initialization.
1582 * @key_tfm: Crypto context for key material, set by this function
1583 * @cipher_name: Name of the cipher
1584 * @key_size: Size of the key in bytes
1586 * Returns zero on success. Any crypto_tfm structs allocated here
1587 * should be released by other functions, such as on a superblock put
1588 * event, regardless of whether this function succeeds for fails.
1591 ecryptfs_process_cipher(struct crypto_tfm **key_tfm, char *cipher_name,
1594 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
1598 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
1600 printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
1601 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
1604 *key_tfm = crypto_alloc_tfm(cipher_name, CRYPTO_TFM_REQ_WEAK_KEY);
1607 printk(KERN_ERR "Unable to allocate crypto cipher with name "
1608 "[%s]\n", cipher_name);
1612 *key_size = crypto_tfm_alg_max_keysize(*key_tfm);
1613 get_random_bytes(dummy_key, *key_size);
1614 rc = crypto_cipher_setkey(*key_tfm, dummy_key, *key_size);
1616 printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
1617 "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);