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
);
126 int ecryptfs_crypto_api_algify_cipher_name(char **algified_name
,
128 char *chaining_modifier
)
130 int cipher_name_len
= strlen(cipher_name
);
131 int chaining_modifier_len
= strlen(chaining_modifier
);
132 int algified_name_len
;
135 algified_name_len
= (chaining_modifier_len
+ cipher_name_len
+ 3);
136 (*algified_name
) = kmalloc(algified_name_len
, GFP_KERNEL
);
137 if (!(*algified_name
)) {
141 snprintf((*algified_name
), algified_name_len
, "%s(%s)",
142 chaining_modifier
, cipher_name
);
150 * @iv: destination for the derived iv vale
151 * @crypt_stat: Pointer to crypt_stat struct for the current inode
152 * @offset: Offset of the page whose's iv we are to derive
154 * Generate the initialization vector from the given root IV and page
157 * Returns zero on success; non-zero on error.
159 static int ecryptfs_derive_iv(char *iv
, struct ecryptfs_crypt_stat
*crypt_stat
,
163 char dst
[MD5_DIGEST_SIZE
];
164 char src
[ECRYPTFS_MAX_IV_BYTES
+ 16];
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
);
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);
176 snprintf((src
+ crypt_stat
->iv_bytes
), 16, "%ld", offset
);
177 if (unlikely(ecryptfs_verbosity
> 0)) {
178 ecryptfs_printk(KERN_DEBUG
, "source:\n");
179 ecryptfs_dump_hex(src
, (crypt_stat
->iv_bytes
+ 16));
181 rc
= ecryptfs_calculate_md5(dst
, crypt_stat
, src
,
182 (crypt_stat
->iv_bytes
+ 16));
184 ecryptfs_printk(KERN_WARNING
, "Error attempting to compute "
185 "MD5 while generating IV for a page\n");
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
);
198 * ecryptfs_init_crypt_stat
199 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
201 * Initialize the crypt_stat structure.
204 ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat
*crypt_stat
)
206 memset((void *)crypt_stat
, 0, sizeof(struct ecryptfs_crypt_stat
));
207 mutex_init(&crypt_stat
->cs_mutex
);
208 mutex_init(&crypt_stat
->cs_tfm_mutex
);
209 mutex_init(&crypt_stat
->cs_hash_tfm_mutex
);
210 ECRYPTFS_SET_FLAG(crypt_stat
->flags
, ECRYPTFS_STRUCT_INITIALIZED
);
214 * ecryptfs_destruct_crypt_stat
215 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
217 * Releases all memory associated with a crypt_stat struct.
219 void ecryptfs_destruct_crypt_stat(struct ecryptfs_crypt_stat
*crypt_stat
)
222 crypto_free_blkcipher(crypt_stat
->tfm
);
223 if (crypt_stat
->hash_tfm
)
224 crypto_free_hash(crypt_stat
->hash_tfm
);
225 memset(crypt_stat
, 0, sizeof(struct ecryptfs_crypt_stat
));
228 void ecryptfs_destruct_mount_crypt_stat(
229 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
)
231 if (mount_crypt_stat
->global_auth_tok_key
)
232 key_put(mount_crypt_stat
->global_auth_tok_key
);
233 if (mount_crypt_stat
->global_key_tfm
)
234 crypto_free_blkcipher(mount_crypt_stat
->global_key_tfm
);
235 memset(mount_crypt_stat
, 0, sizeof(struct ecryptfs_mount_crypt_stat
));
239 * virt_to_scatterlist
240 * @addr: Virtual address
241 * @size: Size of data; should be an even multiple of the block size
242 * @sg: Pointer to scatterlist array; set to NULL to obtain only
243 * the number of scatterlist structs required in array
244 * @sg_size: Max array size
246 * Fills in a scatterlist array with page references for a passed
249 * Returns the number of scatterlist structs in array used
251 int virt_to_scatterlist(const void *addr
, int size
, struct scatterlist
*sg
,
257 int remainder_of_page
;
259 while (size
> 0 && i
< sg_size
) {
260 pg
= virt_to_page(addr
);
261 offset
= offset_in_page(addr
);
264 sg
[i
].offset
= offset
;
266 remainder_of_page
= PAGE_CACHE_SIZE
- offset
;
267 if (size
>= remainder_of_page
) {
269 sg
[i
].length
= remainder_of_page
;
270 addr
+= remainder_of_page
;
271 size
-= remainder_of_page
;
286 * encrypt_scatterlist
287 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
288 * @dest_sg: Destination of encrypted data
289 * @src_sg: Data to be encrypted
290 * @size: Length of data to be encrypted
291 * @iv: iv to use during encryption
293 * Returns the number of bytes encrypted; negative value on error
295 static int encrypt_scatterlist(struct ecryptfs_crypt_stat
*crypt_stat
,
296 struct scatterlist
*dest_sg
,
297 struct scatterlist
*src_sg
, int size
,
300 struct blkcipher_desc desc
= {
301 .tfm
= crypt_stat
->tfm
,
303 .flags
= CRYPTO_TFM_REQ_MAY_SLEEP
307 BUG_ON(!crypt_stat
|| !crypt_stat
->tfm
308 || !ECRYPTFS_CHECK_FLAG(crypt_stat
->flags
,
309 ECRYPTFS_STRUCT_INITIALIZED
));
310 if (unlikely(ecryptfs_verbosity
> 0)) {
311 ecryptfs_printk(KERN_DEBUG
, "Key size [%d]; key:\n",
312 crypt_stat
->key_size
);
313 ecryptfs_dump_hex(crypt_stat
->key
,
314 crypt_stat
->key_size
);
316 /* Consider doing this once, when the file is opened */
317 mutex_lock(&crypt_stat
->cs_tfm_mutex
);
318 rc
= crypto_blkcipher_setkey(crypt_stat
->tfm
, crypt_stat
->key
,
319 crypt_stat
->key_size
);
321 ecryptfs_printk(KERN_ERR
, "Error setting key; rc = [%d]\n",
323 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
327 ecryptfs_printk(KERN_DEBUG
, "Encrypting [%d] bytes.\n", size
);
328 crypto_blkcipher_encrypt_iv(&desc
, dest_sg
, src_sg
, size
);
329 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
335 ecryptfs_extent_to_lwr_pg_idx_and_offset(unsigned long *lower_page_idx
,
337 struct ecryptfs_crypt_stat
*crypt_stat
,
338 unsigned long extent_num
)
340 unsigned long lower_extent_num
;
341 int extents_occupied_by_headers_at_front
;
342 int bytes_occupied_by_headers_at_front
;
344 int extents_per_page
;
346 bytes_occupied_by_headers_at_front
=
347 ( crypt_stat
->header_extent_size
348 * crypt_stat
->num_header_extents_at_front
);
349 extents_occupied_by_headers_at_front
=
350 ( bytes_occupied_by_headers_at_front
351 / crypt_stat
->extent_size
);
352 lower_extent_num
= extents_occupied_by_headers_at_front
+ extent_num
;
353 extents_per_page
= PAGE_CACHE_SIZE
/ crypt_stat
->extent_size
;
354 (*lower_page_idx
) = lower_extent_num
/ extents_per_page
;
355 extent_offset
= lower_extent_num
% extents_per_page
;
356 (*byte_offset
) = extent_offset
* crypt_stat
->extent_size
;
357 ecryptfs_printk(KERN_DEBUG
, " * crypt_stat->header_extent_size = "
358 "[%d]\n", crypt_stat
->header_extent_size
);
359 ecryptfs_printk(KERN_DEBUG
, " * crypt_stat->"
360 "num_header_extents_at_front = [%d]\n",
361 crypt_stat
->num_header_extents_at_front
);
362 ecryptfs_printk(KERN_DEBUG
, " * extents_occupied_by_headers_at_"
363 "front = [%d]\n", extents_occupied_by_headers_at_front
);
364 ecryptfs_printk(KERN_DEBUG
, " * lower_extent_num = [0x%.16x]\n",
366 ecryptfs_printk(KERN_DEBUG
, " * extents_per_page = [%d]\n",
368 ecryptfs_printk(KERN_DEBUG
, " * (*lower_page_idx) = [0x%.16x]\n",
370 ecryptfs_printk(KERN_DEBUG
, " * extent_offset = [%d]\n",
372 ecryptfs_printk(KERN_DEBUG
, " * (*byte_offset) = [%d]\n",
376 static int ecryptfs_write_out_page(struct ecryptfs_page_crypt_context
*ctx
,
377 struct page
*lower_page
,
378 struct inode
*lower_inode
,
379 int byte_offset_in_page
, int bytes_to_write
)
383 if (ctx
->mode
== ECRYPTFS_PREPARE_COMMIT_MODE
) {
384 rc
= ecryptfs_commit_lower_page(lower_page
, lower_inode
,
385 ctx
->param
.lower_file
,
389 ecryptfs_printk(KERN_ERR
, "Error calling lower "
390 "commit; rc = [%d]\n", rc
);
394 rc
= ecryptfs_writepage_and_release_lower_page(lower_page
,
398 ecryptfs_printk(KERN_ERR
, "Error calling lower "
399 "writepage(); rc = [%d]\n", rc
);
407 static int ecryptfs_read_in_page(struct ecryptfs_page_crypt_context
*ctx
,
408 struct page
**lower_page
,
409 struct inode
*lower_inode
,
410 unsigned long lower_page_idx
,
411 int byte_offset_in_page
)
415 if (ctx
->mode
== ECRYPTFS_PREPARE_COMMIT_MODE
) {
416 /* TODO: Limit this to only the data extents that are
418 rc
= ecryptfs_get_lower_page(lower_page
, lower_inode
,
419 ctx
->param
.lower_file
,
423 - byte_offset_in_page
));
426 KERN_ERR
, "Error attempting to grab, map, "
427 "and prepare_write lower page with index "
428 "[0x%.16x]; rc = [%d]\n", lower_page_idx
, rc
);
432 rc
= ecryptfs_grab_and_map_lower_page(lower_page
, NULL
,
437 KERN_ERR
, "Error attempting to grab and map "
438 "lower page with index [0x%.16x]; rc = [%d]\n",
448 * ecryptfs_encrypt_page
449 * @ctx: The context of the page
451 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
452 * that eCryptfs pages may straddle the lower pages -- for instance,
453 * if the file was created on a machine with an 8K page size
454 * (resulting in an 8K header), and then the file is copied onto a
455 * host with a 32K page size, then when reading page 0 of the eCryptfs
456 * file, 24K of page 0 of the lower file will be read and decrypted,
457 * and then 8K of page 1 of the lower file will be read and decrypted.
459 * The actual operations performed on each page depends on the
460 * contents of the ecryptfs_page_crypt_context struct.
462 * Returns zero on success; negative on error
464 int ecryptfs_encrypt_page(struct ecryptfs_page_crypt_context
*ctx
)
466 char extent_iv
[ECRYPTFS_MAX_IV_BYTES
];
467 unsigned long base_extent
;
468 unsigned long extent_offset
= 0;
469 unsigned long lower_page_idx
= 0;
470 unsigned long prior_lower_page_idx
= 0;
471 struct page
*lower_page
;
472 struct inode
*lower_inode
;
473 struct ecryptfs_inode_info
*inode_info
;
474 struct ecryptfs_crypt_stat
*crypt_stat
;
476 int lower_byte_offset
= 0;
477 int orig_byte_offset
= 0;
478 int num_extents_per_page
;
479 #define ECRYPTFS_PAGE_STATE_UNREAD 0
480 #define ECRYPTFS_PAGE_STATE_READ 1
481 #define ECRYPTFS_PAGE_STATE_MODIFIED 2
482 #define ECRYPTFS_PAGE_STATE_WRITTEN 3
485 lower_inode
= ecryptfs_inode_to_lower(ctx
->page
->mapping
->host
);
486 inode_info
= ecryptfs_inode_to_private(ctx
->page
->mapping
->host
);
487 crypt_stat
= &inode_info
->crypt_stat
;
488 if (!ECRYPTFS_CHECK_FLAG(crypt_stat
->flags
, ECRYPTFS_ENCRYPTED
)) {
489 rc
= ecryptfs_copy_page_to_lower(ctx
->page
, lower_inode
,
490 ctx
->param
.lower_file
);
492 ecryptfs_printk(KERN_ERR
, "Error attempting to copy "
493 "page at index [0x%.16x]\n",
497 num_extents_per_page
= PAGE_CACHE_SIZE
/ crypt_stat
->extent_size
;
498 base_extent
= (ctx
->page
->index
* num_extents_per_page
);
499 page_state
= ECRYPTFS_PAGE_STATE_UNREAD
;
500 while (extent_offset
< num_extents_per_page
) {
501 ecryptfs_extent_to_lwr_pg_idx_and_offset(
502 &lower_page_idx
, &lower_byte_offset
, crypt_stat
,
503 (base_extent
+ extent_offset
));
504 if (prior_lower_page_idx
!= lower_page_idx
505 && page_state
== ECRYPTFS_PAGE_STATE_MODIFIED
) {
506 rc
= ecryptfs_write_out_page(ctx
, lower_page
,
510 - orig_byte_offset
));
512 ecryptfs_printk(KERN_ERR
, "Error attempting "
513 "to write out page; rc = [%d]"
517 page_state
= ECRYPTFS_PAGE_STATE_WRITTEN
;
519 if (page_state
== ECRYPTFS_PAGE_STATE_UNREAD
520 || page_state
== ECRYPTFS_PAGE_STATE_WRITTEN
) {
521 rc
= ecryptfs_read_in_page(ctx
, &lower_page
,
522 lower_inode
, lower_page_idx
,
525 ecryptfs_printk(KERN_ERR
, "Error attempting "
526 "to read in lower page with "
527 "index [0x%.16x]; rc = [%d]\n",
531 orig_byte_offset
= lower_byte_offset
;
532 prior_lower_page_idx
= lower_page_idx
;
533 page_state
= ECRYPTFS_PAGE_STATE_READ
;
535 BUG_ON(!(page_state
== ECRYPTFS_PAGE_STATE_MODIFIED
536 || page_state
== ECRYPTFS_PAGE_STATE_READ
));
537 rc
= ecryptfs_derive_iv(extent_iv
, crypt_stat
,
538 (base_extent
+ extent_offset
));
540 ecryptfs_printk(KERN_ERR
, "Error attempting to "
541 "derive IV for extent [0x%.16x]; "
543 (base_extent
+ extent_offset
), rc
);
546 if (unlikely(ecryptfs_verbosity
> 0)) {
547 ecryptfs_printk(KERN_DEBUG
, "Encrypting extent "
549 ecryptfs_dump_hex(extent_iv
, crypt_stat
->iv_bytes
);
550 ecryptfs_printk(KERN_DEBUG
, "First 8 bytes before "
552 ecryptfs_dump_hex((char *)
553 (page_address(ctx
->page
)
555 * crypt_stat
->extent_size
)), 8);
557 rc
= ecryptfs_encrypt_page_offset(
558 crypt_stat
, lower_page
, lower_byte_offset
, ctx
->page
,
559 (extent_offset
* crypt_stat
->extent_size
),
560 crypt_stat
->extent_size
, extent_iv
);
561 ecryptfs_printk(KERN_DEBUG
, "Encrypt extent [0x%.16x]; "
563 (base_extent
+ extent_offset
), rc
);
564 if (unlikely(ecryptfs_verbosity
> 0)) {
565 ecryptfs_printk(KERN_DEBUG
, "First 8 bytes after "
567 ecryptfs_dump_hex((char *)(page_address(lower_page
)
568 + lower_byte_offset
), 8);
570 page_state
= ECRYPTFS_PAGE_STATE_MODIFIED
;
573 BUG_ON(orig_byte_offset
!= 0);
574 rc
= ecryptfs_write_out_page(ctx
, lower_page
, lower_inode
, 0,
576 + crypt_stat
->extent_size
));
578 ecryptfs_printk(KERN_ERR
, "Error attempting to write out "
579 "page; rc = [%d]\n", rc
);
587 * ecryptfs_decrypt_page
588 * @file: The ecryptfs file
589 * @page: The page in ecryptfs to decrypt
591 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
592 * that eCryptfs pages may straddle the lower pages -- for instance,
593 * if the file was created on a machine with an 8K page size
594 * (resulting in an 8K header), and then the file is copied onto a
595 * host with a 32K page size, then when reading page 0 of the eCryptfs
596 * file, 24K of page 0 of the lower file will be read and decrypted,
597 * and then 8K of page 1 of the lower file will be read and decrypted.
599 * Returns zero on success; negative on error
601 int ecryptfs_decrypt_page(struct file
*file
, struct page
*page
)
603 char extent_iv
[ECRYPTFS_MAX_IV_BYTES
];
604 unsigned long base_extent
;
605 unsigned long extent_offset
= 0;
606 unsigned long lower_page_idx
= 0;
607 unsigned long prior_lower_page_idx
= 0;
608 struct page
*lower_page
;
609 char *lower_page_virt
= NULL
;
610 struct inode
*lower_inode
;
611 struct ecryptfs_crypt_stat
*crypt_stat
;
614 int num_extents_per_page
;
617 crypt_stat
= &(ecryptfs_inode_to_private(
618 page
->mapping
->host
)->crypt_stat
);
619 lower_inode
= ecryptfs_inode_to_lower(page
->mapping
->host
);
620 if (!ECRYPTFS_CHECK_FLAG(crypt_stat
->flags
, ECRYPTFS_ENCRYPTED
)) {
621 rc
= ecryptfs_do_readpage(file
, page
, page
->index
);
623 ecryptfs_printk(KERN_ERR
, "Error attempting to copy "
624 "page at index [0x%.16x]\n",
628 num_extents_per_page
= PAGE_CACHE_SIZE
/ crypt_stat
->extent_size
;
629 base_extent
= (page
->index
* num_extents_per_page
);
630 lower_page_virt
= kmem_cache_alloc(ecryptfs_lower_page_cache
,
632 if (!lower_page_virt
) {
634 ecryptfs_printk(KERN_ERR
, "Error getting page for encrypted "
638 lower_page
= virt_to_page(lower_page_virt
);
639 page_state
= ECRYPTFS_PAGE_STATE_UNREAD
;
640 while (extent_offset
< num_extents_per_page
) {
641 ecryptfs_extent_to_lwr_pg_idx_and_offset(
642 &lower_page_idx
, &byte_offset
, crypt_stat
,
643 (base_extent
+ extent_offset
));
644 if (prior_lower_page_idx
!= lower_page_idx
645 || page_state
== ECRYPTFS_PAGE_STATE_UNREAD
) {
646 rc
= ecryptfs_do_readpage(file
, lower_page
,
649 ecryptfs_printk(KERN_ERR
, "Error reading "
650 "lower encrypted page; rc = "
654 prior_lower_page_idx
= lower_page_idx
;
655 page_state
= ECRYPTFS_PAGE_STATE_READ
;
657 rc
= ecryptfs_derive_iv(extent_iv
, crypt_stat
,
658 (base_extent
+ extent_offset
));
660 ecryptfs_printk(KERN_ERR
, "Error attempting to "
661 "derive IV for extent [0x%.16x]; rc = "
663 (base_extent
+ extent_offset
), rc
);
666 if (unlikely(ecryptfs_verbosity
> 0)) {
667 ecryptfs_printk(KERN_DEBUG
, "Decrypting extent "
669 ecryptfs_dump_hex(extent_iv
, crypt_stat
->iv_bytes
);
670 ecryptfs_printk(KERN_DEBUG
, "First 8 bytes before "
672 ecryptfs_dump_hex((lower_page_virt
+ byte_offset
), 8);
674 rc
= ecryptfs_decrypt_page_offset(crypt_stat
, page
,
676 * crypt_stat
->extent_size
),
677 lower_page
, byte_offset
,
678 crypt_stat
->extent_size
,
680 if (rc
!= crypt_stat
->extent_size
) {
681 ecryptfs_printk(KERN_ERR
, "Error attempting to "
682 "decrypt extent [0x%.16x]\n",
683 (base_extent
+ extent_offset
));
687 if (unlikely(ecryptfs_verbosity
> 0)) {
688 ecryptfs_printk(KERN_DEBUG
, "First 8 bytes after "
690 ecryptfs_dump_hex((char *)(page_address(page
)
697 kmem_cache_free(ecryptfs_lower_page_cache
, lower_page_virt
);
702 * decrypt_scatterlist
704 * Returns the number of bytes decrypted; negative value on error
706 static int decrypt_scatterlist(struct ecryptfs_crypt_stat
*crypt_stat
,
707 struct scatterlist
*dest_sg
,
708 struct scatterlist
*src_sg
, int size
,
711 struct blkcipher_desc desc
= {
712 .tfm
= crypt_stat
->tfm
,
714 .flags
= CRYPTO_TFM_REQ_MAY_SLEEP
718 /* Consider doing this once, when the file is opened */
719 mutex_lock(&crypt_stat
->cs_tfm_mutex
);
720 rc
= crypto_blkcipher_setkey(crypt_stat
->tfm
, crypt_stat
->key
,
721 crypt_stat
->key_size
);
723 ecryptfs_printk(KERN_ERR
, "Error setting key; rc = [%d]\n",
725 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
729 ecryptfs_printk(KERN_DEBUG
, "Decrypting [%d] bytes.\n", size
);
730 rc
= crypto_blkcipher_decrypt_iv(&desc
, dest_sg
, src_sg
, size
);
731 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
733 ecryptfs_printk(KERN_ERR
, "Error decrypting; rc = [%d]\n",
743 * ecryptfs_encrypt_page_offset
745 * Returns the number of bytes encrypted
748 ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat
*crypt_stat
,
749 struct page
*dst_page
, int dst_offset
,
750 struct page
*src_page
, int src_offset
, int size
,
753 struct scatterlist src_sg
, dst_sg
;
755 src_sg
.page
= src_page
;
756 src_sg
.offset
= src_offset
;
757 src_sg
.length
= size
;
758 dst_sg
.page
= dst_page
;
759 dst_sg
.offset
= dst_offset
;
760 dst_sg
.length
= size
;
761 return encrypt_scatterlist(crypt_stat
, &dst_sg
, &src_sg
, size
, iv
);
765 * ecryptfs_decrypt_page_offset
767 * Returns the number of bytes decrypted
770 ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat
*crypt_stat
,
771 struct page
*dst_page
, int dst_offset
,
772 struct page
*src_page
, int src_offset
, int size
,
775 struct scatterlist src_sg
, dst_sg
;
777 src_sg
.page
= src_page
;
778 src_sg
.offset
= src_offset
;
779 src_sg
.length
= size
;
780 dst_sg
.page
= dst_page
;
781 dst_sg
.offset
= dst_offset
;
782 dst_sg
.length
= size
;
783 return decrypt_scatterlist(crypt_stat
, &dst_sg
, &src_sg
, size
, iv
);
786 #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
789 * ecryptfs_init_crypt_ctx
790 * @crypt_stat: Uninitilized crypt stats structure
792 * Initialize the crypto context.
794 * TODO: Performance: Keep a cache of initialized cipher contexts;
795 * only init if needed
797 int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat
*crypt_stat
)
802 if (!crypt_stat
->cipher
) {
803 ecryptfs_printk(KERN_ERR
, "No cipher specified\n");
806 ecryptfs_printk(KERN_DEBUG
,
807 "Initializing cipher [%s]; strlen = [%d]; "
808 "key_size_bits = [%d]\n",
809 crypt_stat
->cipher
, (int)strlen(crypt_stat
->cipher
),
810 crypt_stat
->key_size
<< 3);
811 if (crypt_stat
->tfm
) {
815 mutex_lock(&crypt_stat
->cs_tfm_mutex
);
816 rc
= ecryptfs_crypto_api_algify_cipher_name(&full_alg_name
,
817 crypt_stat
->cipher
, "cbc");
820 crypt_stat
->tfm
= crypto_alloc_blkcipher(full_alg_name
, 0,
822 kfree(full_alg_name
);
823 if (IS_ERR(crypt_stat
->tfm
)) {
824 rc
= PTR_ERR(crypt_stat
->tfm
);
825 ecryptfs_printk(KERN_ERR
, "cryptfs: init_crypt_ctx(): "
826 "Error initializing cipher [%s]\n",
828 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
831 crypto_blkcipher_set_flags(crypt_stat
->tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
832 mutex_unlock(&crypt_stat
->cs_tfm_mutex
);
838 static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat
*crypt_stat
)
842 crypt_stat
->extent_mask
= 0xFFFFFFFF;
843 crypt_stat
->extent_shift
= 0;
844 if (crypt_stat
->extent_size
== 0)
846 extent_size_tmp
= crypt_stat
->extent_size
;
847 while ((extent_size_tmp
& 0x01) == 0) {
848 extent_size_tmp
>>= 1;
849 crypt_stat
->extent_mask
<<= 1;
850 crypt_stat
->extent_shift
++;
854 void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat
*crypt_stat
)
856 /* Default values; may be overwritten as we are parsing the
858 crypt_stat
->extent_size
= ECRYPTFS_DEFAULT_EXTENT_SIZE
;
859 set_extent_mask_and_shift(crypt_stat
);
860 crypt_stat
->iv_bytes
= ECRYPTFS_DEFAULT_IV_BYTES
;
861 if (PAGE_CACHE_SIZE
<= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
) {
862 crypt_stat
->header_extent_size
=
863 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
;
865 crypt_stat
->header_extent_size
= PAGE_CACHE_SIZE
;
866 crypt_stat
->num_header_extents_at_front
= 1;
870 * ecryptfs_compute_root_iv
873 * On error, sets the root IV to all 0's.
875 int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat
*crypt_stat
)
878 char dst
[MD5_DIGEST_SIZE
];
880 BUG_ON(crypt_stat
->iv_bytes
> MD5_DIGEST_SIZE
);
881 BUG_ON(crypt_stat
->iv_bytes
<= 0);
882 if (!ECRYPTFS_CHECK_FLAG(crypt_stat
->flags
, ECRYPTFS_KEY_VALID
)) {
884 ecryptfs_printk(KERN_WARNING
, "Session key not valid; "
885 "cannot generate root IV\n");
888 rc
= ecryptfs_calculate_md5(dst
, crypt_stat
, crypt_stat
->key
,
889 crypt_stat
->key_size
);
891 ecryptfs_printk(KERN_WARNING
, "Error attempting to compute "
892 "MD5 while generating root IV\n");
895 memcpy(crypt_stat
->root_iv
, dst
, crypt_stat
->iv_bytes
);
898 memset(crypt_stat
->root_iv
, 0, crypt_stat
->iv_bytes
);
899 ECRYPTFS_SET_FLAG(crypt_stat
->flags
,
900 ECRYPTFS_SECURITY_WARNING
);
905 static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat
*crypt_stat
)
907 get_random_bytes(crypt_stat
->key
, crypt_stat
->key_size
);
908 ECRYPTFS_SET_FLAG(crypt_stat
->flags
, ECRYPTFS_KEY_VALID
);
909 ecryptfs_compute_root_iv(crypt_stat
);
910 if (unlikely(ecryptfs_verbosity
> 0)) {
911 ecryptfs_printk(KERN_DEBUG
, "Generated new session key:\n");
912 ecryptfs_dump_hex(crypt_stat
->key
,
913 crypt_stat
->key_size
);
918 * ecryptfs_set_default_crypt_stat_vals
921 * Default values in the event that policy does not override them.
923 static void ecryptfs_set_default_crypt_stat_vals(
924 struct ecryptfs_crypt_stat
*crypt_stat
,
925 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
)
927 ecryptfs_set_default_sizes(crypt_stat
);
928 strcpy(crypt_stat
->cipher
, ECRYPTFS_DEFAULT_CIPHER
);
929 crypt_stat
->key_size
= ECRYPTFS_DEFAULT_KEY_BYTES
;
930 ECRYPTFS_CLEAR_FLAG(crypt_stat
->flags
, ECRYPTFS_KEY_VALID
);
931 crypt_stat
->file_version
= ECRYPTFS_FILE_VERSION
;
932 crypt_stat
->mount_crypt_stat
= mount_crypt_stat
;
936 * ecryptfs_new_file_context
939 * If the crypto context for the file has not yet been established,
940 * this is where we do that. Establishing a new crypto context
941 * involves the following decisions:
942 * - What cipher to use?
943 * - What set of authentication tokens to use?
944 * Here we just worry about getting enough information into the
945 * authentication tokens so that we know that they are available.
946 * We associate the available authentication tokens with the new file
947 * via the set of signatures in the crypt_stat struct. Later, when
948 * the headers are actually written out, we may again defer to
949 * userspace to perform the encryption of the session key; for the
950 * foreseeable future, this will be the case with public key packets.
952 * Returns zero on success; non-zero otherwise
954 /* Associate an authentication token(s) with the file */
955 int ecryptfs_new_file_context(struct dentry
*ecryptfs_dentry
)
958 struct ecryptfs_crypt_stat
*crypt_stat
=
959 &ecryptfs_inode_to_private(ecryptfs_dentry
->d_inode
)->crypt_stat
;
960 struct ecryptfs_mount_crypt_stat
*mount_crypt_stat
=
961 &ecryptfs_superblock_to_private(
962 ecryptfs_dentry
->d_sb
)->mount_crypt_stat
;
965 ecryptfs_set_default_crypt_stat_vals(crypt_stat
, mount_crypt_stat
);
966 /* See if there are mount crypt options */
967 if (mount_crypt_stat
->global_auth_tok
) {
968 ecryptfs_printk(KERN_DEBUG
, "Initializing context for new "
969 "file using mount_crypt_stat\n");
970 ECRYPTFS_SET_FLAG(crypt_stat
->flags
, ECRYPTFS_ENCRYPTED
);
971 ECRYPTFS_SET_FLAG(crypt_stat
->flags
, ECRYPTFS_KEY_VALID
);
972 memcpy(crypt_stat
->keysigs
[crypt_stat
->num_keysigs
++],
973 mount_crypt_stat
->global_auth_tok_sig
,
974 ECRYPTFS_SIG_SIZE_HEX
);
976 strlen(mount_crypt_stat
->global_default_cipher_name
);
977 memcpy(crypt_stat
->cipher
,
978 mount_crypt_stat
->global_default_cipher_name
,
980 crypt_stat
->cipher
[cipher_name_len
] = '\0';
981 crypt_stat
->key_size
=
982 mount_crypt_stat
->global_default_cipher_key_size
;
983 ecryptfs_generate_new_key(crypt_stat
);
985 /* We should not encounter this scenario since we
986 * should detect lack of global_auth_tok at mount time
987 * TODO: Applies to 0.1 release only; remove in future
990 rc
= ecryptfs_init_crypt_ctx(crypt_stat
);
992 ecryptfs_printk(KERN_ERR
, "Error initializing cryptographic "
993 "context for cipher [%s]: rc = [%d]\n",
994 crypt_stat
->cipher
, rc
);
999 * contains_ecryptfs_marker - check for the ecryptfs marker
1000 * @data: The data block in which to check
1002 * Returns one if marker found; zero if not found
1004 int contains_ecryptfs_marker(char *data
)
1008 memcpy(&m_1
, data
, 4);
1009 m_1
= be32_to_cpu(m_1
);
1010 memcpy(&m_2
, (data
+ 4), 4);
1011 m_2
= be32_to_cpu(m_2
);
1012 if ((m_1
^ MAGIC_ECRYPTFS_MARKER
) == m_2
)
1014 ecryptfs_printk(KERN_DEBUG
, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1015 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1
, m_2
,
1016 MAGIC_ECRYPTFS_MARKER
);
1017 ecryptfs_printk(KERN_DEBUG
, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1018 "[0x%.8x]\n", (m_1
^ MAGIC_ECRYPTFS_MARKER
));
1022 struct ecryptfs_flag_map_elem
{
1027 /* Add support for additional flags by adding elements here. */
1028 static struct ecryptfs_flag_map_elem ecryptfs_flag_map
[] = {
1029 {0x00000001, ECRYPTFS_ENABLE_HMAC
},
1030 {0x00000002, ECRYPTFS_ENCRYPTED
}
1034 * ecryptfs_process_flags
1036 * @page_virt: Source data to be parsed
1037 * @bytes_read: Updated with the number of bytes read
1039 * Returns zero on success; non-zero if the flag set is invalid
1041 static int ecryptfs_process_flags(struct ecryptfs_crypt_stat
*crypt_stat
,
1042 char *page_virt
, int *bytes_read
)
1048 memcpy(&flags
, page_virt
, 4);
1049 flags
= be32_to_cpu(flags
);
1050 for (i
= 0; i
< ((sizeof(ecryptfs_flag_map
)
1051 / sizeof(struct ecryptfs_flag_map_elem
))); i
++)
1052 if (flags
& ecryptfs_flag_map
[i
].file_flag
) {
1053 ECRYPTFS_SET_FLAG(crypt_stat
->flags
,
1054 ecryptfs_flag_map
[i
].local_flag
);
1056 ECRYPTFS_CLEAR_FLAG(crypt_stat
->flags
,
1057 ecryptfs_flag_map
[i
].local_flag
);
1058 /* Version is in top 8 bits of the 32-bit flag vector */
1059 crypt_stat
->file_version
= ((flags
>> 24) & 0xFF);
1065 * write_ecryptfs_marker
1066 * @page_virt: The pointer to in a page to begin writing the marker
1067 * @written: Number of bytes written
1069 * Marker = 0x3c81b7f5
1071 static void write_ecryptfs_marker(char *page_virt
, size_t *written
)
1075 get_random_bytes(&m_1
, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
/ 2));
1076 m_2
= (m_1
^ MAGIC_ECRYPTFS_MARKER
);
1077 m_1
= cpu_to_be32(m_1
);
1078 memcpy(page_virt
, &m_1
, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
/ 2));
1079 m_2
= cpu_to_be32(m_2
);
1080 memcpy(page_virt
+ (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
/ 2), &m_2
,
1081 (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
/ 2));
1082 (*written
) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
;
1086 write_ecryptfs_flags(char *page_virt
, struct ecryptfs_crypt_stat
*crypt_stat
,
1092 for (i
= 0; i
< ((sizeof(ecryptfs_flag_map
)
1093 / sizeof(struct ecryptfs_flag_map_elem
))); i
++)
1094 if (ECRYPTFS_CHECK_FLAG(crypt_stat
->flags
,
1095 ecryptfs_flag_map
[i
].local_flag
))
1096 flags
|= ecryptfs_flag_map
[i
].file_flag
;
1097 /* Version is in top 8 bits of the 32-bit flag vector */
1098 flags
|= ((((u8
)crypt_stat
->file_version
) << 24) & 0xFF000000);
1099 flags
= cpu_to_be32(flags
);
1100 memcpy(page_virt
, &flags
, 4);
1104 struct ecryptfs_cipher_code_str_map_elem
{
1105 char cipher_str
[16];
1109 /* Add support for additional ciphers by adding elements here. The
1110 * cipher_code is whatever OpenPGP applicatoins use to identify the
1111 * ciphers. List in order of probability. */
1112 static struct ecryptfs_cipher_code_str_map_elem
1113 ecryptfs_cipher_code_str_map
[] = {
1114 {"aes",RFC2440_CIPHER_AES_128
},
1115 {"blowfish", RFC2440_CIPHER_BLOWFISH
},
1116 {"des3_ede", RFC2440_CIPHER_DES3_EDE
},
1117 {"cast5", RFC2440_CIPHER_CAST_5
},
1118 {"twofish", RFC2440_CIPHER_TWOFISH
},
1119 {"cast6", RFC2440_CIPHER_CAST_6
},
1120 {"aes", RFC2440_CIPHER_AES_192
},
1121 {"aes", RFC2440_CIPHER_AES_256
}
1125 * ecryptfs_code_for_cipher_string
1126 * @str: The string representing the cipher name
1128 * Returns zero on no match, or the cipher code on match
1130 u16
ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat
*crypt_stat
)
1134 struct ecryptfs_cipher_code_str_map_elem
*map
=
1135 ecryptfs_cipher_code_str_map
;
1137 if (strcmp(crypt_stat
->cipher
, "aes") == 0) {
1138 switch (crypt_stat
->key_size
) {
1140 code
= RFC2440_CIPHER_AES_128
;
1143 code
= RFC2440_CIPHER_AES_192
;
1146 code
= RFC2440_CIPHER_AES_256
;
1149 for (i
= 0; i
< ARRAY_SIZE(ecryptfs_cipher_code_str_map
); i
++)
1150 if (strcmp(crypt_stat
->cipher
, map
[i
].cipher_str
) == 0){
1151 code
= map
[i
].cipher_code
;
1159 * ecryptfs_cipher_code_to_string
1160 * @str: Destination to write out the cipher name
1161 * @cipher_code: The code to convert to cipher name string
1163 * Returns zero on success
1165 int ecryptfs_cipher_code_to_string(char *str
, u16 cipher_code
)
1171 for (i
= 0; i
< ARRAY_SIZE(ecryptfs_cipher_code_str_map
); i
++)
1172 if (cipher_code
== ecryptfs_cipher_code_str_map
[i
].cipher_code
)
1173 strcpy(str
, ecryptfs_cipher_code_str_map
[i
].cipher_str
);
1174 if (str
[0] == '\0') {
1175 ecryptfs_printk(KERN_WARNING
, "Cipher code not recognized: "
1176 "[%d]\n", cipher_code
);
1183 * ecryptfs_read_header_region
1188 * Returns zero on success; non-zero otherwise
1190 int ecryptfs_read_header_region(char *data
, struct dentry
*dentry
,
1191 struct vfsmount
*mnt
)
1193 struct file
*lower_file
;
1197 if ((rc
= ecryptfs_open_lower_file(&lower_file
, dentry
, mnt
,
1200 "Error opening lower_file to read header region\n");
1203 lower_file
->f_pos
= 0;
1206 /* For releases 0.1 and 0.2, all of the header information
1207 * fits in the first data extent-sized region. */
1208 rc
= lower_file
->f_op
->read(lower_file
, (char __user
*)data
,
1209 ECRYPTFS_DEFAULT_EXTENT_SIZE
, &lower_file
->f_pos
);
1211 if ((rc
= ecryptfs_close_lower_file(lower_file
))) {
1212 printk(KERN_ERR
"Error closing lower_file\n");
1221 write_header_metadata(char *virt
, struct ecryptfs_crypt_stat
*crypt_stat
,
1224 u32 header_extent_size
;
1225 u16 num_header_extents_at_front
;
1227 header_extent_size
= (u32
)crypt_stat
->header_extent_size
;
1228 num_header_extents_at_front
=
1229 (u16
)crypt_stat
->num_header_extents_at_front
;
1230 header_extent_size
= cpu_to_be32(header_extent_size
);
1231 memcpy(virt
, &header_extent_size
, 4);
1233 num_header_extents_at_front
= cpu_to_be16(num_header_extents_at_front
);
1234 memcpy(virt
, &num_header_extents_at_front
, 2);
1238 struct kmem_cache
*ecryptfs_header_cache_0
;
1239 struct kmem_cache
*ecryptfs_header_cache_1
;
1240 struct kmem_cache
*ecryptfs_header_cache_2
;
1243 * ecryptfs_write_headers_virt
1251 * Octets 0-7: Unencrypted file size (big-endian)
1252 * Octets 8-15: eCryptfs special marker
1253 * Octets 16-19: Flags
1254 * Octet 16: File format version number (between 0 and 255)
1255 * Octets 17-18: Reserved
1256 * Octet 19: Bit 1 (lsb): Reserved
1258 * Bits 3-8: Reserved
1259 * Octets 20-23: Header extent size (big-endian)
1260 * Octets 24-25: Number of header extents at front of file
1262 * Octet 26: Begin RFC 2440 authentication token packet set
1264 * Lower data (CBC encrypted)
1266 * Lower data (CBC encrypted)
1269 * Returns zero on success
1271 int ecryptfs_write_headers_virt(char *page_virt
,
1272 struct ecryptfs_crypt_stat
*crypt_stat
,
1273 struct dentry
*ecryptfs_dentry
)
1279 offset
= ECRYPTFS_FILE_SIZE_BYTES
;
1280 write_ecryptfs_marker((page_virt
+ offset
), &written
);
1282 write_ecryptfs_flags((page_virt
+ offset
), crypt_stat
, &written
);
1284 write_header_metadata((page_virt
+ offset
), crypt_stat
, &written
);
1286 rc
= ecryptfs_generate_key_packet_set((page_virt
+ offset
), crypt_stat
,
1287 ecryptfs_dentry
, &written
,
1288 PAGE_CACHE_SIZE
- offset
);
1290 ecryptfs_printk(KERN_WARNING
, "Error generating key packet "
1291 "set; rc = [%d]\n", rc
);
1296 * ecryptfs_write_headers
1297 * @lower_file: The lower file struct, which was returned from dentry_open
1299 * Write the file headers out. This will likely involve a userspace
1300 * callout, in which the session key is encrypted with one or more
1301 * public keys and/or the passphrase necessary to do the encryption is
1302 * retrieved via a prompt. Exactly what happens at this point should
1303 * be policy-dependent.
1305 * Returns zero on success; non-zero on error
1307 int ecryptfs_write_headers(struct dentry
*ecryptfs_dentry
,
1308 struct file
*lower_file
)
1311 struct ecryptfs_crypt_stat
*crypt_stat
;
1313 int current_header_page
;
1317 crypt_stat
= &ecryptfs_inode_to_private(
1318 ecryptfs_dentry
->d_inode
)->crypt_stat
;
1319 if (likely(ECRYPTFS_CHECK_FLAG(crypt_stat
->flags
,
1320 ECRYPTFS_ENCRYPTED
))) {
1321 if (!ECRYPTFS_CHECK_FLAG(crypt_stat
->flags
,
1322 ECRYPTFS_KEY_VALID
)) {
1323 ecryptfs_printk(KERN_DEBUG
, "Key is "
1324 "invalid; bailing out\n");
1330 ecryptfs_printk(KERN_WARNING
,
1331 "Called with crypt_stat->encrypted == 0\n");
1334 /* Released in this function */
1335 page_virt
= kmem_cache_zalloc(ecryptfs_header_cache_0
, GFP_USER
);
1337 ecryptfs_printk(KERN_ERR
, "Out of memory\n");
1342 rc
= ecryptfs_write_headers_virt(page_virt
, crypt_stat
,
1345 ecryptfs_printk(KERN_ERR
, "Error whilst writing headers\n");
1346 memset(page_virt
, 0, PAGE_CACHE_SIZE
);
1349 ecryptfs_printk(KERN_DEBUG
,
1350 "Writing key packet set to underlying file\n");
1351 lower_file
->f_pos
= 0;
1354 ecryptfs_printk(KERN_DEBUG
, "Calling lower_file->f_op->"
1355 "write() w/ header page; lower_file->f_pos = "
1356 "[0x%.16x]\n", lower_file
->f_pos
);
1357 lower_file
->f_op
->write(lower_file
, (char __user
*)page_virt
,
1358 PAGE_CACHE_SIZE
, &lower_file
->f_pos
);
1359 header_pages
= ((crypt_stat
->header_extent_size
1360 * crypt_stat
->num_header_extents_at_front
)
1362 memset(page_virt
, 0, PAGE_CACHE_SIZE
);
1363 current_header_page
= 1;
1364 while (current_header_page
< header_pages
) {
1365 ecryptfs_printk(KERN_DEBUG
, "Calling lower_file->f_op->"
1366 "write() w/ zero'd page; lower_file->f_pos = "
1367 "[0x%.16x]\n", lower_file
->f_pos
);
1368 lower_file
->f_op
->write(lower_file
, (char __user
*)page_virt
,
1369 PAGE_CACHE_SIZE
, &lower_file
->f_pos
);
1370 current_header_page
++;
1373 ecryptfs_printk(KERN_DEBUG
,
1374 "Done writing key packet set to underlying file.\n");
1376 kmem_cache_free(ecryptfs_header_cache_0
, page_virt
);
1381 static int parse_header_metadata(struct ecryptfs_crypt_stat
*crypt_stat
,
1382 char *virt
, int *bytes_read
)
1385 u32 header_extent_size
;
1386 u16 num_header_extents_at_front
;
1388 memcpy(&header_extent_size
, virt
, 4);
1389 header_extent_size
= be32_to_cpu(header_extent_size
);
1391 memcpy(&num_header_extents_at_front
, virt
, 2);
1392 num_header_extents_at_front
= be16_to_cpu(num_header_extents_at_front
);
1393 crypt_stat
->header_extent_size
= (int)header_extent_size
;
1394 crypt_stat
->num_header_extents_at_front
=
1395 (int)num_header_extents_at_front
;
1397 if ((crypt_stat
->header_extent_size
1398 * crypt_stat
->num_header_extents_at_front
)
1399 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
) {
1401 ecryptfs_printk(KERN_WARNING
, "Invalid header extent size: "
1402 "[%d]\n", crypt_stat
->header_extent_size
);
1408 * set_default_header_data
1410 * For version 0 file format; this function is only for backwards
1411 * compatibility for files created with the prior versions of
1414 static void set_default_header_data(struct ecryptfs_crypt_stat
*crypt_stat
)
1416 crypt_stat
->header_extent_size
= 4096;
1417 crypt_stat
->num_header_extents_at_front
= 1;
1421 * ecryptfs_read_headers_virt
1423 * Read/parse the header data. The header format is detailed in the
1424 * comment block for the ecryptfs_write_headers_virt() function.
1426 * Returns zero on success
1428 static int ecryptfs_read_headers_virt(char *page_virt
,
1429 struct ecryptfs_crypt_stat
*crypt_stat
,
1430 struct dentry
*ecryptfs_dentry
)
1436 ecryptfs_set_default_sizes(crypt_stat
);
1437 crypt_stat
->mount_crypt_stat
= &ecryptfs_superblock_to_private(
1438 ecryptfs_dentry
->d_sb
)->mount_crypt_stat
;
1439 offset
= ECRYPTFS_FILE_SIZE_BYTES
;
1440 rc
= contains_ecryptfs_marker(page_virt
+ offset
);
1445 offset
+= MAGIC_ECRYPTFS_MARKER_SIZE_BYTES
;
1446 rc
= ecryptfs_process_flags(crypt_stat
, (page_virt
+ offset
),
1449 ecryptfs_printk(KERN_WARNING
, "Error processing flags\n");
1452 if (crypt_stat
->file_version
> ECRYPTFS_SUPPORTED_FILE_VERSION
) {
1453 ecryptfs_printk(KERN_WARNING
, "File version is [%d]; only "
1454 "file version [%d] is supported by this "
1455 "version of eCryptfs\n",
1456 crypt_stat
->file_version
,
1457 ECRYPTFS_SUPPORTED_FILE_VERSION
);
1461 offset
+= bytes_read
;
1462 if (crypt_stat
->file_version
>= 1) {
1463 rc
= parse_header_metadata(crypt_stat
, (page_virt
+ offset
),
1466 ecryptfs_printk(KERN_WARNING
, "Error reading header "
1467 "metadata; rc = [%d]\n", rc
);
1469 offset
+= bytes_read
;
1471 set_default_header_data(crypt_stat
);
1472 rc
= ecryptfs_parse_packet_set(crypt_stat
, (page_virt
+ offset
),
1479 * ecryptfs_read_headers
1481 * Returns zero if valid headers found and parsed; non-zero otherwise
1483 int ecryptfs_read_headers(struct dentry
*ecryptfs_dentry
,
1484 struct file
*lower_file
)
1487 char *page_virt
= NULL
;
1490 struct ecryptfs_crypt_stat
*crypt_stat
=
1491 &ecryptfs_inode_to_private(ecryptfs_dentry
->d_inode
)->crypt_stat
;
1493 /* Read the first page from the underlying file */
1494 page_virt
= kmem_cache_alloc(ecryptfs_header_cache_1
, GFP_USER
);
1497 ecryptfs_printk(KERN_ERR
, "Unable to allocate page_virt\n");
1500 lower_file
->f_pos
= 0;
1503 bytes_read
= lower_file
->f_op
->read(lower_file
,
1504 (char __user
*)page_virt
,
1505 ECRYPTFS_DEFAULT_EXTENT_SIZE
,
1506 &lower_file
->f_pos
);
1508 if (bytes_read
!= ECRYPTFS_DEFAULT_EXTENT_SIZE
) {
1512 rc
= ecryptfs_read_headers_virt(page_virt
, crypt_stat
,
1515 ecryptfs_printk(KERN_DEBUG
, "Valid eCryptfs headers not "
1521 memset(page_virt
, 0, PAGE_CACHE_SIZE
);
1522 kmem_cache_free(ecryptfs_header_cache_1
, page_virt
);
1528 * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1529 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1530 * @name: The plaintext name
1531 * @length: The length of the plaintext
1532 * @encoded_name: The encypted name
1534 * Encrypts and encodes a filename into something that constitutes a
1535 * valid filename for a filesystem, with printable characters.
1537 * We assume that we have a properly initialized crypto context,
1538 * pointed to by crypt_stat->tfm.
1540 * TODO: Implement filename decoding and decryption here, in place of
1541 * memcpy. We are keeping the framework around for now to (1)
1542 * facilitate testing of the components needed to implement filename
1543 * encryption and (2) to provide a code base from which other
1544 * developers in the community can easily implement this feature.
1546 * Returns the length of encoded filename; negative if error
1549 ecryptfs_encode_filename(struct ecryptfs_crypt_stat
*crypt_stat
,
1550 const char *name
, int length
, char **encoded_name
)
1554 (*encoded_name
) = kmalloc(length
+ 2, GFP_KERNEL
);
1555 if (!(*encoded_name
)) {
1559 /* TODO: Filename encryption is a scheduled feature for a
1560 * future version of eCryptfs. This function is here only for
1561 * the purpose of providing a framework for other developers
1562 * to easily implement filename encryption. Hint: Replace this
1563 * memcpy() with a call to encrypt and encode the
1564 * filename, the set the length accordingly. */
1565 memcpy((void *)(*encoded_name
), (void *)name
, length
);
1566 (*encoded_name
)[length
] = '\0';
1573 * ecryptfs_decode_filename - converts the cipher text name to plaintext
1574 * @crypt_stat: The crypt_stat struct associated with the file
1575 * @name: The filename in cipher text
1576 * @length: The length of the cipher text name
1577 * @decrypted_name: The plaintext name
1579 * Decodes and decrypts the filename.
1581 * We assume that we have a properly initialized crypto context,
1582 * pointed to by crypt_stat->tfm.
1584 * TODO: Implement filename decoding and decryption here, in place of
1585 * memcpy. We are keeping the framework around for now to (1)
1586 * facilitate testing of the components needed to implement filename
1587 * encryption and (2) to provide a code base from which other
1588 * developers in the community can easily implement this feature.
1590 * Returns the length of decoded filename; negative if error
1593 ecryptfs_decode_filename(struct ecryptfs_crypt_stat
*crypt_stat
,
1594 const char *name
, int length
, char **decrypted_name
)
1598 (*decrypted_name
) = kmalloc(length
+ 2, GFP_KERNEL
);
1599 if (!(*decrypted_name
)) {
1603 /* TODO: Filename encryption is a scheduled feature for a
1604 * future version of eCryptfs. This function is here only for
1605 * the purpose of providing a framework for other developers
1606 * to easily implement filename encryption. Hint: Replace this
1607 * memcpy() with a call to decode and decrypt the
1608 * filename, the set the length accordingly. */
1609 memcpy((void *)(*decrypted_name
), (void *)name
, length
);
1610 (*decrypted_name
)[length
+ 1] = '\0'; /* Only for convenience
1611 * in printing out the
1620 * ecryptfs_process_cipher - Perform cipher initialization.
1621 * @key_tfm: Crypto context for key material, set by this function
1622 * @cipher_name: Name of the cipher
1623 * @key_size: Size of the key in bytes
1625 * Returns zero on success. Any crypto_tfm structs allocated here
1626 * should be released by other functions, such as on a superblock put
1627 * event, regardless of whether this function succeeds for fails.
1630 ecryptfs_process_cipher(struct crypto_blkcipher
**key_tfm
, char *cipher_name
,
1633 char dummy_key
[ECRYPTFS_MAX_KEY_BYTES
];
1634 char *full_alg_name
;
1638 if (*key_size
> ECRYPTFS_MAX_KEY_BYTES
) {
1640 printk(KERN_ERR
"Requested key size is [%Zd] bytes; maximum "
1641 "allowable is [%d]\n", *key_size
, ECRYPTFS_MAX_KEY_BYTES
);
1644 rc
= ecryptfs_crypto_api_algify_cipher_name(&full_alg_name
, cipher_name
,
1648 *key_tfm
= crypto_alloc_blkcipher(full_alg_name
, 0, CRYPTO_ALG_ASYNC
);
1649 kfree(full_alg_name
);
1650 if (IS_ERR(*key_tfm
)) {
1651 rc
= PTR_ERR(*key_tfm
);
1652 printk(KERN_ERR
"Unable to allocate crypto cipher with name "
1653 "[%s]; rc = [%d]\n", cipher_name
, rc
);
1656 crypto_blkcipher_set_flags(*key_tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
1657 if (*key_size
== 0) {
1658 struct blkcipher_alg
*alg
= crypto_blkcipher_alg(*key_tfm
);
1660 *key_size
= alg
->max_keysize
;
1662 get_random_bytes(dummy_key
, *key_size
);
1663 rc
= crypto_blkcipher_setkey(*key_tfm
, dummy_key
, *key_size
);
1665 printk(KERN_ERR
"Error attempting to set key of size [%Zd] for "
1666 "cipher [%s]; rc = [%d]\n", *key_size
, cipher_name
, rc
);