eCryptfs: remove unused functions and kmem_cache
[deliverable/linux.git] / fs / ecryptfs / crypto.c
CommitLineData
237fead6
MH
1/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
dd2a3b7a 6 * Copyright (C) 2004-2007 International Business Machines Corp.
237fead6
MH
7 * 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{
237fead6 97 struct scatterlist sg;
565d9724
MH
98 struct hash_desc desc = {
99 .tfm = crypt_stat->hash_tfm,
100 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
101 };
102 int rc = 0;
237fead6 103
565d9724 104 mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
237fead6 105 sg_init_one(&sg, (u8 *)src, len);
565d9724
MH
106 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);
237fead6 111 ecryptfs_printk(KERN_ERR, "Error attempting to "
565d9724
MH
112 "allocate crypto context; rc = [%d]\n",
113 rc);
237fead6
MH
114 goto out;
115 }
565d9724 116 crypt_stat->hash_tfm = desc.tfm;
237fead6 117 }
565d9724
MH
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);
237fead6
MH
122out:
123 return rc;
124}
125
cd9d67df
MH
126static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
127 char *cipher_name,
128 char *chaining_modifier)
8bba066f
MH
129{
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);
7bd473fc 137 if (!(*algified_name)) {
8bba066f
MH
138 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
237fead6
MH
148/**
149 * ecryptfs_derive_iv
150 * @iv: destination for the derived iv vale
151 * @crypt_stat: Pointer to crypt_stat struct for the current inode
d6a13c17 152 * @offset: Offset of the extent whose IV we are to derive
237fead6
MH
153 *
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,
d6a13c17 160 loff_t offset)
237fead6
MH
161{
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);
d6a13c17 176 snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
237fead6
MH
177 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));
f4aad16a
MH
207 INIT_LIST_HEAD(&crypt_stat->keysig_list);
208 mutex_init(&crypt_stat->keysig_list_mutex);
237fead6
MH
209 mutex_init(&crypt_stat->cs_mutex);
210 mutex_init(&crypt_stat->cs_tfm_mutex);
565d9724 211 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
e2bd99ec 212 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
237fead6
MH
213}
214
215/**
fcd12835 216 * ecryptfs_destroy_crypt_stat
237fead6
MH
217 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
218 *
219 * Releases all memory associated with a crypt_stat struct.
220 */
fcd12835 221void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
237fead6 222{
f4aad16a
MH
223 struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
224
237fead6 225 if (crypt_stat->tfm)
8bba066f 226 crypto_free_blkcipher(crypt_stat->tfm);
565d9724
MH
227 if (crypt_stat->hash_tfm)
228 crypto_free_hash(crypt_stat->hash_tfm);
f4aad16a
MH
229 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);
237fead6
MH
236 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
237}
238
fcd12835 239void ecryptfs_destroy_mount_crypt_stat(
237fead6
MH
240 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
241{
f4aad16a
MH
242 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);
237fead6
MH
258 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{
8bba066f
MH
323 struct blkcipher_desc desc = {
324 .tfm = crypt_stat->tfm,
325 .info = iv,
326 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
327 };
237fead6
MH
328 int rc = 0;
329
330 BUG_ON(!crypt_stat || !crypt_stat->tfm
e2bd99ec 331 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
237fead6
MH
332 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);
8bba066f
MH
340 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
341 crypt_stat->key_size);
237fead6
MH
342 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);
8bba066f 350 crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
237fead6
MH
351 mutex_unlock(&crypt_stat->cs_tfm_mutex);
352out:
353 return rc;
354}
355
0216f7f7
MH
356/**
357 * ecryptfs_lower_offset_for_extent
358 *
359 * Convert an eCryptfs page index into a lower byte offset
360 */
361void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
362 struct ecryptfs_crypt_stat *crypt_stat)
363{
364 (*offset) = ((crypt_stat->extent_size
365 * crypt_stat->num_header_extents_at_front)
366 + (crypt_stat->extent_size * extent_num));
367}
368
369/**
370 * ecryptfs_encrypt_extent
371 * @enc_extent_page: Allocated page into which to encrypt the data in
372 * @page
373 * @crypt_stat: crypt_stat containing cryptographic context for the
374 * encryption operation
375 * @page: Page containing plaintext data extent to encrypt
376 * @extent_offset: Page extent offset for use in generating IV
377 *
378 * Encrypts one extent of data.
379 *
380 * Return zero on success; non-zero otherwise
381 */
382static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
383 struct ecryptfs_crypt_stat *crypt_stat,
384 struct page *page,
385 unsigned long extent_offset)
386{
d6a13c17 387 loff_t extent_base;
0216f7f7
MH
388 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
389 int rc;
390
d6a13c17 391 extent_base = (((loff_t)page->index)
0216f7f7
MH
392 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
393 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
394 (extent_base + extent_offset));
395 if (rc) {
396 ecryptfs_printk(KERN_ERR, "Error attempting to "
397 "derive IV for extent [0x%.16x]; "
398 "rc = [%d]\n", (extent_base + extent_offset),
399 rc);
400 goto out;
401 }
402 if (unlikely(ecryptfs_verbosity > 0)) {
403 ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
404 "with iv:\n");
405 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
406 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
407 "encryption:\n");
408 ecryptfs_dump_hex((char *)
409 (page_address(page)
410 + (extent_offset * crypt_stat->extent_size)),
411 8);
412 }
413 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
414 page, (extent_offset
415 * crypt_stat->extent_size),
416 crypt_stat->extent_size, extent_iv);
417 if (rc < 0) {
418 printk(KERN_ERR "%s: Error attempting to encrypt page with "
419 "page->index = [%ld], extent_offset = [%ld]; "
420 "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
421 rc);
422 goto out;
423 }
424 rc = 0;
425 if (unlikely(ecryptfs_verbosity > 0)) {
426 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
427 "rc = [%d]\n", (extent_base + extent_offset),
428 rc);
429 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
430 "encryption:\n");
431 ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
432 }
433out:
434 return rc;
435}
436
237fead6
MH
437/**
438 * ecryptfs_encrypt_page
0216f7f7
MH
439 * @page: Page mapped from the eCryptfs inode for the file; contains
440 * decrypted content that needs to be encrypted (to a temporary
441 * page; not in place) and written out to the lower file
237fead6
MH
442 *
443 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
444 * that eCryptfs pages may straddle the lower pages -- for instance,
445 * if the file was created on a machine with an 8K page size
446 * (resulting in an 8K header), and then the file is copied onto a
447 * host with a 32K page size, then when reading page 0 of the eCryptfs
448 * file, 24K of page 0 of the lower file will be read and decrypted,
449 * and then 8K of page 1 of the lower file will be read and decrypted.
450 *
237fead6
MH
451 * Returns zero on success; negative on error
452 */
0216f7f7 453int ecryptfs_encrypt_page(struct page *page)
237fead6 454{
0216f7f7 455 struct inode *ecryptfs_inode;
237fead6 456 struct ecryptfs_crypt_stat *crypt_stat;
0216f7f7
MH
457 char *enc_extent_virt = NULL;
458 struct page *enc_extent_page;
459 loff_t extent_offset;
237fead6 460 int rc = 0;
0216f7f7
MH
461
462 ecryptfs_inode = page->mapping->host;
463 crypt_stat =
464 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
e2bd99ec 465 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
0216f7f7
MH
466 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
467 0, PAGE_CACHE_SIZE);
237fead6 468 if (rc)
0216f7f7
MH
469 printk(KERN_ERR "%s: Error attempting to copy "
470 "page at index [%ld]\n", __FUNCTION__,
471 page->index);
237fead6
MH
472 goto out;
473 }
0216f7f7
MH
474 enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
475 if (!enc_extent_virt) {
476 rc = -ENOMEM;
477 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
478 "encrypted extent\n");
479 goto out;
480 }
481 enc_extent_page = virt_to_page(enc_extent_virt);
482 for (extent_offset = 0;
483 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
484 extent_offset++) {
485 loff_t offset;
486
487 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
488 extent_offset);
237fead6 489 if (rc) {
0216f7f7
MH
490 printk(KERN_ERR "%s: Error encrypting extent; "
491 "rc = [%d]\n", __FUNCTION__, rc);
237fead6
MH
492 goto out;
493 }
0216f7f7 494 ecryptfs_lower_offset_for_extent(
d6a13c17
MH
495 &offset, ((((loff_t)page->index)
496 * (PAGE_CACHE_SIZE
497 / crypt_stat->extent_size))
0216f7f7
MH
498 + extent_offset), crypt_stat);
499 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
500 offset, crypt_stat->extent_size);
501 if (rc) {
502 ecryptfs_printk(KERN_ERR, "Error attempting "
503 "to write lower page; rc = [%d]"
504 "\n", rc);
505 goto out;
237fead6 506 }
237fead6
MH
507 extent_offset++;
508 }
0216f7f7
MH
509out:
510 kfree(enc_extent_virt);
511 return rc;
512}
513
514static int ecryptfs_decrypt_extent(struct page *page,
515 struct ecryptfs_crypt_stat *crypt_stat,
516 struct page *enc_extent_page,
517 unsigned long extent_offset)
518{
d6a13c17 519 loff_t extent_base;
0216f7f7
MH
520 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
521 int rc;
522
d6a13c17 523 extent_base = (((loff_t)page->index)
0216f7f7
MH
524 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
525 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
526 (extent_base + extent_offset));
237fead6 527 if (rc) {
0216f7f7
MH
528 ecryptfs_printk(KERN_ERR, "Error attempting to "
529 "derive IV for extent [0x%.16x]; "
530 "rc = [%d]\n", (extent_base + extent_offset),
531 rc);
532 goto out;
533 }
534 if (unlikely(ecryptfs_verbosity > 0)) {
535 ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
536 "with iv:\n");
537 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
538 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
539 "decryption:\n");
540 ecryptfs_dump_hex((char *)
541 (page_address(enc_extent_page)
542 + (extent_offset * crypt_stat->extent_size)),
543 8);
544 }
545 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
546 (extent_offset
547 * crypt_stat->extent_size),
548 enc_extent_page, 0,
549 crypt_stat->extent_size, extent_iv);
550 if (rc < 0) {
551 printk(KERN_ERR "%s: Error attempting to decrypt to page with "
552 "page->index = [%ld], extent_offset = [%ld]; "
553 "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
554 rc);
555 goto out;
556 }
557 rc = 0;
558 if (unlikely(ecryptfs_verbosity > 0)) {
559 ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
560 "rc = [%d]\n", (extent_base + extent_offset),
561 rc);
562 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
563 "decryption:\n");
564 ecryptfs_dump_hex((char *)(page_address(page)
565 + (extent_offset
566 * crypt_stat->extent_size)), 8);
237fead6
MH
567 }
568out:
569 return rc;
570}
571
572/**
573 * ecryptfs_decrypt_page
0216f7f7
MH
574 * @page: Page mapped from the eCryptfs inode for the file; data read
575 * and decrypted from the lower file will be written into this
576 * page
237fead6
MH
577 *
578 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
579 * that eCryptfs pages may straddle the lower pages -- for instance,
580 * if the file was created on a machine with an 8K page size
581 * (resulting in an 8K header), and then the file is copied onto a
582 * host with a 32K page size, then when reading page 0 of the eCryptfs
583 * file, 24K of page 0 of the lower file will be read and decrypted,
584 * and then 8K of page 1 of the lower file will be read and decrypted.
585 *
586 * Returns zero on success; negative on error
587 */
0216f7f7 588int ecryptfs_decrypt_page(struct page *page)
237fead6 589{
0216f7f7 590 struct inode *ecryptfs_inode;
237fead6 591 struct ecryptfs_crypt_stat *crypt_stat;
0216f7f7
MH
592 char *enc_extent_virt = NULL;
593 struct page *enc_extent_page;
594 unsigned long extent_offset;
237fead6 595 int rc = 0;
237fead6 596
0216f7f7
MH
597 ecryptfs_inode = page->mapping->host;
598 crypt_stat =
599 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
e2bd99ec 600 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
0216f7f7
MH
601 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
602 PAGE_CACHE_SIZE,
603 ecryptfs_inode);
237fead6 604 if (rc)
0216f7f7
MH
605 printk(KERN_ERR "%s: Error attempting to copy "
606 "page at index [%ld]\n", __FUNCTION__,
607 page->index);
608 goto out_clear_uptodate;
237fead6 609 }
0216f7f7
MH
610 enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
611 if (!enc_extent_virt) {
237fead6 612 rc = -ENOMEM;
0216f7f7
MH
613 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
614 "encrypted extent\n");
615 goto out_clear_uptodate;
237fead6 616 }
0216f7f7
MH
617 enc_extent_page = virt_to_page(enc_extent_virt);
618 for (extent_offset = 0;
619 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
620 extent_offset++) {
621 loff_t offset;
622
623 ecryptfs_lower_offset_for_extent(
624 &offset, ((page->index * (PAGE_CACHE_SIZE
625 / crypt_stat->extent_size))
626 + extent_offset), crypt_stat);
627 rc = ecryptfs_read_lower(enc_extent_virt, offset,
628 crypt_stat->extent_size,
629 ecryptfs_inode);
237fead6 630 if (rc) {
0216f7f7
MH
631 ecryptfs_printk(KERN_ERR, "Error attempting "
632 "to read lower page; rc = [%d]"
633 "\n", rc);
634 goto out_clear_uptodate;
237fead6 635 }
0216f7f7
MH
636 rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
637 extent_offset);
638 if (rc) {
639 printk(KERN_ERR "%s: Error encrypting extent; "
640 "rc = [%d]\n", __FUNCTION__, rc);
641 goto out_clear_uptodate;
237fead6
MH
642 }
643 extent_offset++;
644 }
0216f7f7
MH
645 SetPageUptodate(page);
646 goto out;
647out_clear_uptodate:
648 ClearPageUptodate(page);
237fead6 649out:
0216f7f7 650 kfree(enc_extent_virt);
237fead6
MH
651 return rc;
652}
653
654/**
655 * decrypt_scatterlist
22e78faf
MH
656 * @crypt_stat: Cryptographic context
657 * @dest_sg: The destination scatterlist to decrypt into
658 * @src_sg: The source scatterlist to decrypt from
659 * @size: The number of bytes to decrypt
660 * @iv: The initialization vector to use for the decryption
237fead6
MH
661 *
662 * Returns the number of bytes decrypted; negative value on error
663 */
664static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
665 struct scatterlist *dest_sg,
666 struct scatterlist *src_sg, int size,
667 unsigned char *iv)
668{
8bba066f
MH
669 struct blkcipher_desc desc = {
670 .tfm = crypt_stat->tfm,
671 .info = iv,
672 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
673 };
237fead6
MH
674 int rc = 0;
675
676 /* Consider doing this once, when the file is opened */
677 mutex_lock(&crypt_stat->cs_tfm_mutex);
8bba066f
MH
678 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
679 crypt_stat->key_size);
237fead6
MH
680 if (rc) {
681 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
682 rc);
683 mutex_unlock(&crypt_stat->cs_tfm_mutex);
684 rc = -EINVAL;
685 goto out;
686 }
687 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
8bba066f 688 rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
237fead6
MH
689 mutex_unlock(&crypt_stat->cs_tfm_mutex);
690 if (rc) {
691 ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
692 rc);
693 goto out;
694 }
695 rc = size;
696out:
697 return rc;
698}
699
700/**
701 * ecryptfs_encrypt_page_offset
22e78faf
MH
702 * @crypt_stat: The cryptographic context
703 * @dst_page: The page to encrypt into
704 * @dst_offset: The offset in the page to encrypt into
705 * @src_page: The page to encrypt from
706 * @src_offset: The offset in the page to encrypt from
707 * @size: The number of bytes to encrypt
708 * @iv: The initialization vector to use for the encryption
237fead6
MH
709 *
710 * Returns the number of bytes encrypted
711 */
712static int
713ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
714 struct page *dst_page, int dst_offset,
715 struct page *src_page, int src_offset, int size,
716 unsigned char *iv)
717{
718 struct scatterlist src_sg, dst_sg;
719
720 src_sg.page = src_page;
721 src_sg.offset = src_offset;
722 src_sg.length = size;
723 dst_sg.page = dst_page;
724 dst_sg.offset = dst_offset;
725 dst_sg.length = size;
726 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
727}
728
729/**
730 * ecryptfs_decrypt_page_offset
22e78faf
MH
731 * @crypt_stat: The cryptographic context
732 * @dst_page: The page to decrypt into
733 * @dst_offset: The offset in the page to decrypt into
734 * @src_page: The page to decrypt from
735 * @src_offset: The offset in the page to decrypt from
736 * @size: The number of bytes to decrypt
737 * @iv: The initialization vector to use for the decryption
237fead6
MH
738 *
739 * Returns the number of bytes decrypted
740 */
741static int
742ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
743 struct page *dst_page, int dst_offset,
744 struct page *src_page, int src_offset, int size,
745 unsigned char *iv)
746{
747 struct scatterlist src_sg, dst_sg;
748
749 src_sg.page = src_page;
750 src_sg.offset = src_offset;
751 src_sg.length = size;
752 dst_sg.page = dst_page;
753 dst_sg.offset = dst_offset;
754 dst_sg.length = size;
755 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
756}
757
758#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
759
760/**
761 * ecryptfs_init_crypt_ctx
762 * @crypt_stat: Uninitilized crypt stats structure
763 *
764 * Initialize the crypto context.
765 *
766 * TODO: Performance: Keep a cache of initialized cipher contexts;
767 * only init if needed
768 */
769int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
770{
8bba066f 771 char *full_alg_name;
237fead6
MH
772 int rc = -EINVAL;
773
774 if (!crypt_stat->cipher) {
775 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
776 goto out;
777 }
778 ecryptfs_printk(KERN_DEBUG,
779 "Initializing cipher [%s]; strlen = [%d]; "
780 "key_size_bits = [%d]\n",
781 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
782 crypt_stat->key_size << 3);
783 if (crypt_stat->tfm) {
784 rc = 0;
785 goto out;
786 }
787 mutex_lock(&crypt_stat->cs_tfm_mutex);
8bba066f
MH
788 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
789 crypt_stat->cipher, "cbc");
790 if (rc)
791 goto out;
792 crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
793 CRYPTO_ALG_ASYNC);
794 kfree(full_alg_name);
de88777e
AM
795 if (IS_ERR(crypt_stat->tfm)) {
796 rc = PTR_ERR(crypt_stat->tfm);
237fead6
MH
797 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
798 "Error initializing cipher [%s]\n",
799 crypt_stat->cipher);
8bba066f 800 mutex_unlock(&crypt_stat->cs_tfm_mutex);
237fead6
MH
801 goto out;
802 }
f1ddcaf3 803 crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
8bba066f 804 mutex_unlock(&crypt_stat->cs_tfm_mutex);
237fead6
MH
805 rc = 0;
806out:
807 return rc;
808}
809
810static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
811{
812 int extent_size_tmp;
813
814 crypt_stat->extent_mask = 0xFFFFFFFF;
815 crypt_stat->extent_shift = 0;
816 if (crypt_stat->extent_size == 0)
817 return;
818 extent_size_tmp = crypt_stat->extent_size;
819 while ((extent_size_tmp & 0x01) == 0) {
820 extent_size_tmp >>= 1;
821 crypt_stat->extent_mask <<= 1;
822 crypt_stat->extent_shift++;
823 }
824}
825
826void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
827{
828 /* Default values; may be overwritten as we are parsing the
829 * packets. */
830 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
831 set_extent_mask_and_shift(crypt_stat);
832 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
dd2a3b7a
MH
833 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
834 crypt_stat->num_header_extents_at_front = 0;
45eaab79
MH
835 else {
836 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
837 crypt_stat->num_header_extents_at_front =
838 (ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE
839 / crypt_stat->extent_size);
840 else
841 crypt_stat->num_header_extents_at_front =
842 (PAGE_CACHE_SIZE / crypt_stat->extent_size);
843 }
237fead6
MH
844}
845
846/**
847 * ecryptfs_compute_root_iv
848 * @crypt_stats
849 *
850 * On error, sets the root IV to all 0's.
851 */
852int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
853{
854 int rc = 0;
855 char dst[MD5_DIGEST_SIZE];
856
857 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
858 BUG_ON(crypt_stat->iv_bytes <= 0);
e2bd99ec 859 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
237fead6
MH
860 rc = -EINVAL;
861 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
862 "cannot generate root IV\n");
863 goto out;
864 }
865 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
866 crypt_stat->key_size);
867 if (rc) {
868 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
869 "MD5 while generating root IV\n");
870 goto out;
871 }
872 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
873out:
874 if (rc) {
875 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
e2bd99ec 876 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
237fead6
MH
877 }
878 return rc;
879}
880
881static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
882{
883 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
e2bd99ec 884 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
237fead6
MH
885 ecryptfs_compute_root_iv(crypt_stat);
886 if (unlikely(ecryptfs_verbosity > 0)) {
887 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
888 ecryptfs_dump_hex(crypt_stat->key,
889 crypt_stat->key_size);
890 }
891}
892
17398957
MH
893/**
894 * ecryptfs_copy_mount_wide_flags_to_inode_flags
22e78faf
MH
895 * @crypt_stat: The inode's cryptographic context
896 * @mount_crypt_stat: The mount point's cryptographic context
17398957
MH
897 *
898 * This function propagates the mount-wide flags to individual inode
899 * flags.
900 */
901static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
902 struct ecryptfs_crypt_stat *crypt_stat,
903 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
904{
905 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
906 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
907 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
908 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
909}
910
f4aad16a
MH
911static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
912 struct ecryptfs_crypt_stat *crypt_stat,
913 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
914{
915 struct ecryptfs_global_auth_tok *global_auth_tok;
916 int rc = 0;
917
918 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
919 list_for_each_entry(global_auth_tok,
920 &mount_crypt_stat->global_auth_tok_list,
921 mount_crypt_stat_list) {
922 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
923 if (rc) {
924 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
925 mutex_unlock(
926 &mount_crypt_stat->global_auth_tok_list_mutex);
927 goto out;
928 }
929 }
930 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
931out:
932 return rc;
933}
934
237fead6
MH
935/**
936 * ecryptfs_set_default_crypt_stat_vals
22e78faf
MH
937 * @crypt_stat: The inode's cryptographic context
938 * @mount_crypt_stat: The mount point's cryptographic context
237fead6
MH
939 *
940 * Default values in the event that policy does not override them.
941 */
942static void ecryptfs_set_default_crypt_stat_vals(
943 struct ecryptfs_crypt_stat *crypt_stat,
944 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
945{
17398957
MH
946 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
947 mount_crypt_stat);
237fead6
MH
948 ecryptfs_set_default_sizes(crypt_stat);
949 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
950 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
e2bd99ec 951 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
237fead6
MH
952 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
953 crypt_stat->mount_crypt_stat = mount_crypt_stat;
954}
955
956/**
957 * ecryptfs_new_file_context
22e78faf 958 * @ecryptfs_dentry: The eCryptfs dentry
237fead6
MH
959 *
960 * If the crypto context for the file has not yet been established,
961 * this is where we do that. Establishing a new crypto context
962 * involves the following decisions:
963 * - What cipher to use?
964 * - What set of authentication tokens to use?
965 * Here we just worry about getting enough information into the
966 * authentication tokens so that we know that they are available.
967 * We associate the available authentication tokens with the new file
968 * via the set of signatures in the crypt_stat struct. Later, when
969 * the headers are actually written out, we may again defer to
970 * userspace to perform the encryption of the session key; for the
971 * foreseeable future, this will be the case with public key packets.
972 *
973 * Returns zero on success; non-zero otherwise
974 */
237fead6
MH
975int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
976{
237fead6
MH
977 struct ecryptfs_crypt_stat *crypt_stat =
978 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
979 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
980 &ecryptfs_superblock_to_private(
981 ecryptfs_dentry->d_sb)->mount_crypt_stat;
982 int cipher_name_len;
f4aad16a 983 int rc = 0;
237fead6
MH
984
985 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
af655dc6 986 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
f4aad16a
MH
987 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
988 mount_crypt_stat);
989 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
990 mount_crypt_stat);
991 if (rc) {
992 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
993 "to the inode key sigs; rc = [%d]\n", rc);
994 goto out;
995 }
996 cipher_name_len =
997 strlen(mount_crypt_stat->global_default_cipher_name);
998 memcpy(crypt_stat->cipher,
999 mount_crypt_stat->global_default_cipher_name,
1000 cipher_name_len);
1001 crypt_stat->cipher[cipher_name_len] = '\0';
1002 crypt_stat->key_size =
1003 mount_crypt_stat->global_default_cipher_key_size;
1004 ecryptfs_generate_new_key(crypt_stat);
237fead6
MH
1005 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1006 if (rc)
1007 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1008 "context for cipher [%s]: rc = [%d]\n",
1009 crypt_stat->cipher, rc);
f4aad16a 1010out:
237fead6
MH
1011 return rc;
1012}
1013
1014/**
1015 * contains_ecryptfs_marker - check for the ecryptfs marker
1016 * @data: The data block in which to check
1017 *
1018 * Returns one if marker found; zero if not found
1019 */
dd2a3b7a 1020static int contains_ecryptfs_marker(char *data)
237fead6
MH
1021{
1022 u32 m_1, m_2;
1023
1024 memcpy(&m_1, data, 4);
1025 m_1 = be32_to_cpu(m_1);
1026 memcpy(&m_2, (data + 4), 4);
1027 m_2 = be32_to_cpu(m_2);
1028 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1029 return 1;
1030 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1031 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1032 MAGIC_ECRYPTFS_MARKER);
1033 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1034 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1035 return 0;
1036}
1037
1038struct ecryptfs_flag_map_elem {
1039 u32 file_flag;
1040 u32 local_flag;
1041};
1042
1043/* Add support for additional flags by adding elements here. */
1044static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1045 {0x00000001, ECRYPTFS_ENABLE_HMAC},
dd2a3b7a
MH
1046 {0x00000002, ECRYPTFS_ENCRYPTED},
1047 {0x00000004, ECRYPTFS_METADATA_IN_XATTR}
237fead6
MH
1048};
1049
1050/**
1051 * ecryptfs_process_flags
22e78faf 1052 * @crypt_stat: The cryptographic context
237fead6
MH
1053 * @page_virt: Source data to be parsed
1054 * @bytes_read: Updated with the number of bytes read
1055 *
1056 * Returns zero on success; non-zero if the flag set is invalid
1057 */
1058static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1059 char *page_virt, int *bytes_read)
1060{
1061 int rc = 0;
1062 int i;
1063 u32 flags;
1064
1065 memcpy(&flags, page_virt, 4);
1066 flags = be32_to_cpu(flags);
1067 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1068 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1069 if (flags & ecryptfs_flag_map[i].file_flag) {
e2bd99ec 1070 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
237fead6 1071 } else
e2bd99ec 1072 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
237fead6
MH
1073 /* Version is in top 8 bits of the 32-bit flag vector */
1074 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1075 (*bytes_read) = 4;
1076 return rc;
1077}
1078
1079/**
1080 * write_ecryptfs_marker
1081 * @page_virt: The pointer to in a page to begin writing the marker
1082 * @written: Number of bytes written
1083 *
1084 * Marker = 0x3c81b7f5
1085 */
1086static void write_ecryptfs_marker(char *page_virt, size_t *written)
1087{
1088 u32 m_1, m_2;
1089
1090 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1091 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1092 m_1 = cpu_to_be32(m_1);
1093 memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1094 m_2 = cpu_to_be32(m_2);
1095 memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
1096 (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1097 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1098}
1099
1100static void
1101write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1102 size_t *written)
1103{
1104 u32 flags = 0;
1105 int i;
1106
1107 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1108 / sizeof(struct ecryptfs_flag_map_elem))); i++)
e2bd99ec 1109 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
237fead6
MH
1110 flags |= ecryptfs_flag_map[i].file_flag;
1111 /* Version is in top 8 bits of the 32-bit flag vector */
1112 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1113 flags = cpu_to_be32(flags);
1114 memcpy(page_virt, &flags, 4);
1115 (*written) = 4;
1116}
1117
1118struct ecryptfs_cipher_code_str_map_elem {
1119 char cipher_str[16];
1120 u16 cipher_code;
1121};
1122
1123/* Add support for additional ciphers by adding elements here. The
1124 * cipher_code is whatever OpenPGP applicatoins use to identify the
1125 * ciphers. List in order of probability. */
1126static struct ecryptfs_cipher_code_str_map_elem
1127ecryptfs_cipher_code_str_map[] = {
1128 {"aes",RFC2440_CIPHER_AES_128 },
1129 {"blowfish", RFC2440_CIPHER_BLOWFISH},
1130 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
1131 {"cast5", RFC2440_CIPHER_CAST_5},
1132 {"twofish", RFC2440_CIPHER_TWOFISH},
1133 {"cast6", RFC2440_CIPHER_CAST_6},
1134 {"aes", RFC2440_CIPHER_AES_192},
1135 {"aes", RFC2440_CIPHER_AES_256}
1136};
1137
1138/**
1139 * ecryptfs_code_for_cipher_string
22e78faf 1140 * @crypt_stat: The cryptographic context
237fead6
MH
1141 *
1142 * Returns zero on no match, or the cipher code on match
1143 */
1144u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
1145{
1146 int i;
1147 u16 code = 0;
1148 struct ecryptfs_cipher_code_str_map_elem *map =
1149 ecryptfs_cipher_code_str_map;
1150
1151 if (strcmp(crypt_stat->cipher, "aes") == 0) {
1152 switch (crypt_stat->key_size) {
1153 case 16:
1154 code = RFC2440_CIPHER_AES_128;
1155 break;
1156 case 24:
1157 code = RFC2440_CIPHER_AES_192;
1158 break;
1159 case 32:
1160 code = RFC2440_CIPHER_AES_256;
1161 }
1162 } else {
1163 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1164 if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
1165 code = map[i].cipher_code;
1166 break;
1167 }
1168 }
1169 return code;
1170}
1171
1172/**
1173 * ecryptfs_cipher_code_to_string
1174 * @str: Destination to write out the cipher name
1175 * @cipher_code: The code to convert to cipher name string
1176 *
1177 * Returns zero on success
1178 */
1179int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
1180{
1181 int rc = 0;
1182 int i;
1183
1184 str[0] = '\0';
1185 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1186 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1187 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1188 if (str[0] == '\0') {
1189 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1190 "[%d]\n", cipher_code);
1191 rc = -EINVAL;
1192 }
1193 return rc;
1194}
1195
d7cdc5fe
MH
1196int ecryptfs_read_and_validate_header_region(char *data,
1197 struct inode *ecryptfs_inode)
dd2a3b7a 1198{
d7cdc5fe
MH
1199 struct ecryptfs_crypt_stat *crypt_stat =
1200 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
dd2a3b7a
MH
1201 int rc;
1202
d7cdc5fe
MH
1203 rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
1204 ecryptfs_inode);
1205 if (rc) {
1206 printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
1207 __FUNCTION__, rc);
dd2a3b7a 1208 goto out;
d7cdc5fe
MH
1209 }
1210 if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
dd2a3b7a 1211 rc = -EINVAL;
d7cdc5fe
MH
1212 ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n");
1213 }
dd2a3b7a
MH
1214out:
1215 return rc;
1216}
1217
e77a56dd
MH
1218void
1219ecryptfs_write_header_metadata(char *virt,
1220 struct ecryptfs_crypt_stat *crypt_stat,
1221 size_t *written)
237fead6
MH
1222{
1223 u32 header_extent_size;
1224 u16 num_header_extents_at_front;
1225
45eaab79 1226 header_extent_size = (u32)crypt_stat->extent_size;
237fead6
MH
1227 num_header_extents_at_front =
1228 (u16)crypt_stat->num_header_extents_at_front;
1229 header_extent_size = cpu_to_be32(header_extent_size);
1230 memcpy(virt, &header_extent_size, 4);
1231 virt += 4;
1232 num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
1233 memcpy(virt, &num_header_extents_at_front, 2);
1234 (*written) = 6;
1235}
1236
1237struct kmem_cache *ecryptfs_header_cache_0;
1238struct kmem_cache *ecryptfs_header_cache_1;
1239struct kmem_cache *ecryptfs_header_cache_2;
1240
1241/**
1242 * ecryptfs_write_headers_virt
22e78faf
MH
1243 * @page_virt: The virtual address to write the headers to
1244 * @size: Set to the number of bytes written by this function
1245 * @crypt_stat: The cryptographic context
1246 * @ecryptfs_dentry: The eCryptfs dentry
237fead6
MH
1247 *
1248 * Format version: 1
1249 *
1250 * Header Extent:
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
1257 * Bit 2: Encrypted?
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
1261 * (big-endian)
1262 * Octet 26: Begin RFC 2440 authentication token packet set
1263 * Data Extent 0:
1264 * Lower data (CBC encrypted)
1265 * Data Extent 1:
1266 * Lower data (CBC encrypted)
1267 * ...
1268 *
1269 * Returns zero on success
1270 */
dd2a3b7a
MH
1271static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
1272 struct ecryptfs_crypt_stat *crypt_stat,
1273 struct dentry *ecryptfs_dentry)
237fead6
MH
1274{
1275 int rc;
1276 size_t written;
1277 size_t offset;
1278
1279 offset = ECRYPTFS_FILE_SIZE_BYTES;
1280 write_ecryptfs_marker((page_virt + offset), &written);
1281 offset += written;
1282 write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1283 offset += written;
e77a56dd
MH
1284 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1285 &written);
237fead6
MH
1286 offset += written;
1287 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1288 ecryptfs_dentry, &written,
1289 PAGE_CACHE_SIZE - offset);
1290 if (rc)
1291 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1292 "set; rc = [%d]\n", rc);
dd2a3b7a
MH
1293 if (size) {
1294 offset += written;
1295 *size = offset;
1296 }
1297 return rc;
1298}
1299
22e78faf
MH
1300static int
1301ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
d7cdc5fe
MH
1302 struct dentry *ecryptfs_dentry,
1303 char *page_virt)
dd2a3b7a 1304{
dd2a3b7a
MH
1305 int current_header_page;
1306 int header_pages;
d7cdc5fe 1307 int rc;
dd2a3b7a 1308
d7cdc5fe
MH
1309 rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, page_virt,
1310 0, PAGE_CACHE_SIZE);
1311 if (rc) {
1312 printk(KERN_ERR "%s: Error attempting to write header "
1313 "information to lower file; rc = [%d]\n", __FUNCTION__,
1314 rc);
70456600
MH
1315 goto out;
1316 }
45eaab79 1317 header_pages = ((crypt_stat->extent_size
dd2a3b7a
MH
1318 * crypt_stat->num_header_extents_at_front)
1319 / PAGE_CACHE_SIZE);
1320 memset(page_virt, 0, PAGE_CACHE_SIZE);
1321 current_header_page = 1;
1322 while (current_header_page < header_pages) {
d7cdc5fe
MH
1323 loff_t offset;
1324
d6a13c17 1325 offset = (((loff_t)current_header_page) << PAGE_CACHE_SHIFT);
d7cdc5fe
MH
1326 if ((rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode,
1327 page_virt, offset,
1328 PAGE_CACHE_SIZE))) {
1329 printk(KERN_ERR "%s: Error attempting to write header "
1330 "information to lower file; rc = [%d]\n",
1331 __FUNCTION__, rc);
70456600
MH
1332 goto out;
1333 }
dd2a3b7a
MH
1334 current_header_page++;
1335 }
70456600
MH
1336out:
1337 return rc;
dd2a3b7a
MH
1338}
1339
22e78faf
MH
1340static int
1341ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1342 struct ecryptfs_crypt_stat *crypt_stat,
1343 char *page_virt, size_t size)
dd2a3b7a
MH
1344{
1345 int rc;
1346
1347 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1348 size, 0);
237fead6
MH
1349 return rc;
1350}
1351
1352/**
dd2a3b7a 1353 * ecryptfs_write_metadata
22e78faf 1354 * @ecryptfs_dentry: The eCryptfs dentry
237fead6
MH
1355 *
1356 * Write the file headers out. This will likely involve a userspace
1357 * callout, in which the session key is encrypted with one or more
1358 * public keys and/or the passphrase necessary to do the encryption is
1359 * retrieved via a prompt. Exactly what happens at this point should
1360 * be policy-dependent.
1361 *
d7cdc5fe
MH
1362 * TODO: Support header information spanning multiple pages
1363 *
237fead6
MH
1364 * Returns zero on success; non-zero on error
1365 */
d7cdc5fe 1366int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
237fead6 1367{
d7cdc5fe
MH
1368 struct ecryptfs_crypt_stat *crypt_stat =
1369 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
237fead6 1370 char *page_virt;
d7cdc5fe 1371 size_t size = 0;
237fead6
MH
1372 int rc = 0;
1373
e2bd99ec
MH
1374 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1375 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
d7cdc5fe 1376 printk(KERN_ERR "Key is invalid; bailing out\n");
237fead6
MH
1377 rc = -EINVAL;
1378 goto out;
1379 }
1380 } else {
1381 rc = -EINVAL;
1382 ecryptfs_printk(KERN_WARNING,
1383 "Called with crypt_stat->encrypted == 0\n");
1384 goto out;
1385 }
1386 /* Released in this function */
c3762229 1387 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_0, GFP_USER);
237fead6
MH
1388 if (!page_virt) {
1389 ecryptfs_printk(KERN_ERR, "Out of memory\n");
1390 rc = -ENOMEM;
1391 goto out;
1392 }
dd2a3b7a
MH
1393 rc = ecryptfs_write_headers_virt(page_virt, &size, crypt_stat,
1394 ecryptfs_dentry);
237fead6
MH
1395 if (unlikely(rc)) {
1396 ecryptfs_printk(KERN_ERR, "Error whilst writing headers\n");
1397 memset(page_virt, 0, PAGE_CACHE_SIZE);
1398 goto out_free;
1399 }
dd2a3b7a
MH
1400 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1401 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
1402 crypt_stat, page_virt,
1403 size);
1404 else
d7cdc5fe
MH
1405 rc = ecryptfs_write_metadata_to_contents(crypt_stat,
1406 ecryptfs_dentry,
dd2a3b7a
MH
1407 page_virt);
1408 if (rc) {
1409 printk(KERN_ERR "Error writing metadata out to lower file; "
1410 "rc = [%d]\n", rc);
1411 goto out_free;
237fead6 1412 }
237fead6
MH
1413out_free:
1414 kmem_cache_free(ecryptfs_header_cache_0, page_virt);
1415out:
1416 return rc;
1417}
1418
dd2a3b7a
MH
1419#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1420#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
237fead6 1421static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
dd2a3b7a
MH
1422 char *virt, int *bytes_read,
1423 int validate_header_size)
237fead6
MH
1424{
1425 int rc = 0;
1426 u32 header_extent_size;
1427 u16 num_header_extents_at_front;
1428
1429 memcpy(&header_extent_size, virt, 4);
1430 header_extent_size = be32_to_cpu(header_extent_size);
1431 virt += 4;
1432 memcpy(&num_header_extents_at_front, virt, 2);
1433 num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
237fead6
MH
1434 crypt_stat->num_header_extents_at_front =
1435 (int)num_header_extents_at_front;
45eaab79 1436 (*bytes_read) = (sizeof(u32) + sizeof(u16));
dd2a3b7a 1437 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
45eaab79 1438 && ((crypt_stat->extent_size
dd2a3b7a
MH
1439 * crypt_stat->num_header_extents_at_front)
1440 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
237fead6 1441 rc = -EINVAL;
45eaab79
MH
1442 printk(KERN_WARNING "Invalid number of header extents: [%zd]\n",
1443 crypt_stat->num_header_extents_at_front);
237fead6
MH
1444 }
1445 return rc;
1446}
1447
1448/**
1449 * set_default_header_data
22e78faf 1450 * @crypt_stat: The cryptographic context
237fead6
MH
1451 *
1452 * For version 0 file format; this function is only for backwards
1453 * compatibility for files created with the prior versions of
1454 * eCryptfs.
1455 */
1456static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1457{
45eaab79 1458 crypt_stat->num_header_extents_at_front = 2;
237fead6
MH
1459}
1460
1461/**
1462 * ecryptfs_read_headers_virt
22e78faf
MH
1463 * @page_virt: The virtual address into which to read the headers
1464 * @crypt_stat: The cryptographic context
1465 * @ecryptfs_dentry: The eCryptfs dentry
1466 * @validate_header_size: Whether to validate the header size while reading
237fead6
MH
1467 *
1468 * Read/parse the header data. The header format is detailed in the
1469 * comment block for the ecryptfs_write_headers_virt() function.
1470 *
1471 * Returns zero on success
1472 */
1473static int ecryptfs_read_headers_virt(char *page_virt,
1474 struct ecryptfs_crypt_stat *crypt_stat,
dd2a3b7a
MH
1475 struct dentry *ecryptfs_dentry,
1476 int validate_header_size)
237fead6
MH
1477{
1478 int rc = 0;
1479 int offset;
1480 int bytes_read;
1481
1482 ecryptfs_set_default_sizes(crypt_stat);
1483 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1484 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1485 offset = ECRYPTFS_FILE_SIZE_BYTES;
1486 rc = contains_ecryptfs_marker(page_virt + offset);
1487 if (rc == 0) {
1488 rc = -EINVAL;
1489 goto out;
1490 }
1491 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1492 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1493 &bytes_read);
1494 if (rc) {
1495 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1496 goto out;
1497 }
1498 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1499 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1500 "file version [%d] is supported by this "
1501 "version of eCryptfs\n",
1502 crypt_stat->file_version,
1503 ECRYPTFS_SUPPORTED_FILE_VERSION);
1504 rc = -EINVAL;
1505 goto out;
1506 }
1507 offset += bytes_read;
1508 if (crypt_stat->file_version >= 1) {
1509 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
dd2a3b7a 1510 &bytes_read, validate_header_size);
237fead6
MH
1511 if (rc) {
1512 ecryptfs_printk(KERN_WARNING, "Error reading header "
1513 "metadata; rc = [%d]\n", rc);
1514 }
1515 offset += bytes_read;
1516 } else
1517 set_default_header_data(crypt_stat);
1518 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1519 ecryptfs_dentry);
1520out:
1521 return rc;
1522}
1523
1524/**
dd2a3b7a 1525 * ecryptfs_read_xattr_region
22e78faf 1526 * @page_virt: The vitual address into which to read the xattr data
2ed92554 1527 * @ecryptfs_inode: The eCryptfs inode
dd2a3b7a
MH
1528 *
1529 * Attempts to read the crypto metadata from the extended attribute
1530 * region of the lower file.
22e78faf
MH
1531 *
1532 * Returns zero on success; non-zero on error
dd2a3b7a 1533 */
d7cdc5fe 1534int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
dd2a3b7a 1535{
d7cdc5fe
MH
1536 struct dentry *lower_dentry =
1537 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
dd2a3b7a
MH
1538 ssize_t size;
1539 int rc = 0;
1540
d7cdc5fe
MH
1541 size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1542 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
dd2a3b7a 1543 if (size < 0) {
d7cdc5fe 1544 printk(KERN_ERR "Error attempting to read the [%s] "
dd2a3b7a
MH
1545 "xattr from the lower file; return value = [%zd]\n",
1546 ECRYPTFS_XATTR_NAME, size);
1547 rc = -EINVAL;
1548 goto out;
1549 }
1550out:
1551 return rc;
1552}
1553
1554int ecryptfs_read_and_validate_xattr_region(char *page_virt,
1555 struct dentry *ecryptfs_dentry)
1556{
1557 int rc;
1558
d7cdc5fe 1559 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
dd2a3b7a
MH
1560 if (rc)
1561 goto out;
1562 if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
1563 printk(KERN_WARNING "Valid data found in [%s] xattr, but "
1564 "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
1565 rc = -EINVAL;
1566 }
1567out:
1568 return rc;
1569}
1570
1571/**
1572 * ecryptfs_read_metadata
1573 *
1574 * Common entry point for reading file metadata. From here, we could
1575 * retrieve the header information from the header region of the file,
1576 * the xattr region of the file, or some other repostory that is
1577 * stored separately from the file itself. The current implementation
1578 * supports retrieving the metadata information from the file contents
1579 * and from the xattr region.
237fead6
MH
1580 *
1581 * Returns zero if valid headers found and parsed; non-zero otherwise
1582 */
d7cdc5fe 1583int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
237fead6
MH
1584{
1585 int rc = 0;
1586 char *page_virt = NULL;
d7cdc5fe 1587 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
237fead6 1588 struct ecryptfs_crypt_stat *crypt_stat =
d7cdc5fe 1589 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
e77a56dd
MH
1590 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1591 &ecryptfs_superblock_to_private(
1592 ecryptfs_dentry->d_sb)->mount_crypt_stat;
237fead6 1593
e77a56dd
MH
1594 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1595 mount_crypt_stat);
237fead6 1596 /* Read the first page from the underlying file */
f7267c0c 1597 page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
237fead6
MH
1598 if (!page_virt) {
1599 rc = -ENOMEM;
d7cdc5fe
MH
1600 printk(KERN_ERR "%s: Unable to allocate page_virt\n",
1601 __FUNCTION__);
237fead6
MH
1602 goto out;
1603 }
d7cdc5fe
MH
1604 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1605 ecryptfs_inode);
1606 if (!rc)
1607 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1608 ecryptfs_dentry,
1609 ECRYPTFS_VALIDATE_HEADER_SIZE);
237fead6 1610 if (rc) {
d7cdc5fe 1611 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
dd2a3b7a
MH
1612 if (rc) {
1613 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1614 "file header region or xattr region\n");
1615 rc = -EINVAL;
1616 goto out;
1617 }
1618 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1619 ecryptfs_dentry,
1620 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1621 if (rc) {
1622 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1623 "file xattr region either\n");
1624 rc = -EINVAL;
1625 }
1626 if (crypt_stat->mount_crypt_stat->flags
1627 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1628 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1629 } else {
1630 printk(KERN_WARNING "Attempt to access file with "
1631 "crypto metadata only in the extended attribute "
1632 "region, but eCryptfs was mounted without "
1633 "xattr support enabled. eCryptfs will not treat "
1634 "this like an encrypted file.\n");
1635 rc = -EINVAL;
1636 }
237fead6
MH
1637 }
1638out:
1639 if (page_virt) {
1640 memset(page_virt, 0, PAGE_CACHE_SIZE);
1641 kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1642 }
1643 return rc;
1644}
1645
1646/**
1647 * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1648 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1649 * @name: The plaintext name
1650 * @length: The length of the plaintext
1651 * @encoded_name: The encypted name
1652 *
1653 * Encrypts and encodes a filename into something that constitutes a
1654 * valid filename for a filesystem, with printable characters.
1655 *
1656 * We assume that we have a properly initialized crypto context,
1657 * pointed to by crypt_stat->tfm.
1658 *
1659 * TODO: Implement filename decoding and decryption here, in place of
1660 * memcpy. We are keeping the framework around for now to (1)
1661 * facilitate testing of the components needed to implement filename
1662 * encryption and (2) to provide a code base from which other
1663 * developers in the community can easily implement this feature.
1664 *
1665 * Returns the length of encoded filename; negative if error
1666 */
1667int
1668ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1669 const char *name, int length, char **encoded_name)
1670{
1671 int error = 0;
1672
1673 (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
1674 if (!(*encoded_name)) {
1675 error = -ENOMEM;
1676 goto out;
1677 }
1678 /* TODO: Filename encryption is a scheduled feature for a
1679 * future version of eCryptfs. This function is here only for
1680 * the purpose of providing a framework for other developers
1681 * to easily implement filename encryption. Hint: Replace this
1682 * memcpy() with a call to encrypt and encode the
1683 * filename, the set the length accordingly. */
1684 memcpy((void *)(*encoded_name), (void *)name, length);
1685 (*encoded_name)[length] = '\0';
1686 error = length + 1;
1687out:
1688 return error;
1689}
1690
1691/**
1692 * ecryptfs_decode_filename - converts the cipher text name to plaintext
1693 * @crypt_stat: The crypt_stat struct associated with the file
1694 * @name: The filename in cipher text
1695 * @length: The length of the cipher text name
1696 * @decrypted_name: The plaintext name
1697 *
1698 * Decodes and decrypts the filename.
1699 *
1700 * We assume that we have a properly initialized crypto context,
1701 * pointed to by crypt_stat->tfm.
1702 *
1703 * TODO: Implement filename decoding and decryption here, in place of
1704 * memcpy. We are keeping the framework around for now to (1)
1705 * facilitate testing of the components needed to implement filename
1706 * encryption and (2) to provide a code base from which other
1707 * developers in the community can easily implement this feature.
1708 *
1709 * Returns the length of decoded filename; negative if error
1710 */
1711int
1712ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1713 const char *name, int length, char **decrypted_name)
1714{
1715 int error = 0;
1716
1717 (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
1718 if (!(*decrypted_name)) {
1719 error = -ENOMEM;
1720 goto out;
1721 }
1722 /* TODO: Filename encryption is a scheduled feature for a
1723 * future version of eCryptfs. This function is here only for
1724 * the purpose of providing a framework for other developers
1725 * to easily implement filename encryption. Hint: Replace this
1726 * memcpy() with a call to decode and decrypt the
1727 * filename, the set the length accordingly. */
1728 memcpy((void *)(*decrypted_name), (void *)name, length);
1729 (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
1730 * in printing out the
1731 * string in debug
1732 * messages */
1733 error = length;
1734out:
1735 return error;
1736}
1737
1738/**
f4aad16a 1739 * ecryptfs_process_key_cipher - Perform key cipher initialization.
237fead6 1740 * @key_tfm: Crypto context for key material, set by this function
e5d9cbde
MH
1741 * @cipher_name: Name of the cipher
1742 * @key_size: Size of the key in bytes
237fead6
MH
1743 *
1744 * Returns zero on success. Any crypto_tfm structs allocated here
1745 * should be released by other functions, such as on a superblock put
1746 * event, regardless of whether this function succeeds for fails.
1747 */
cd9d67df 1748static int
f4aad16a
MH
1749ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1750 char *cipher_name, size_t *key_size)
237fead6
MH
1751{
1752 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
8bba066f 1753 char *full_alg_name;
237fead6
MH
1754 int rc;
1755
e5d9cbde
MH
1756 *key_tfm = NULL;
1757 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
237fead6
MH
1758 rc = -EINVAL;
1759 printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
e5d9cbde 1760 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
237fead6
MH
1761 goto out;
1762 }
8bba066f
MH
1763 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1764 "ecb");
1765 if (rc)
1766 goto out;
1767 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
1768 kfree(full_alg_name);
1769 if (IS_ERR(*key_tfm)) {
1770 rc = PTR_ERR(*key_tfm);
237fead6 1771 printk(KERN_ERR "Unable to allocate crypto cipher with name "
8bba066f 1772 "[%s]; rc = [%d]\n", cipher_name, rc);
237fead6
MH
1773 goto out;
1774 }
8bba066f
MH
1775 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1776 if (*key_size == 0) {
1777 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
1778
1779 *key_size = alg->max_keysize;
1780 }
e5d9cbde 1781 get_random_bytes(dummy_key, *key_size);
8bba066f 1782 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
237fead6
MH
1783 if (rc) {
1784 printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
e5d9cbde 1785 "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
237fead6
MH
1786 rc = -EINVAL;
1787 goto out;
1788 }
1789out:
1790 return rc;
1791}
f4aad16a
MH
1792
1793struct kmem_cache *ecryptfs_key_tfm_cache;
1794struct list_head key_tfm_list;
1795struct mutex key_tfm_list_mutex;
1796
1797int ecryptfs_init_crypto(void)
1798{
1799 mutex_init(&key_tfm_list_mutex);
1800 INIT_LIST_HEAD(&key_tfm_list);
1801 return 0;
1802}
1803
fcd12835 1804int ecryptfs_destroy_crypto(void)
f4aad16a
MH
1805{
1806 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1807
1808 mutex_lock(&key_tfm_list_mutex);
1809 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1810 key_tfm_list) {
1811 list_del(&key_tfm->key_tfm_list);
1812 if (key_tfm->key_tfm)
1813 crypto_free_blkcipher(key_tfm->key_tfm);
1814 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1815 }
1816 mutex_unlock(&key_tfm_list_mutex);
1817 return 0;
1818}
1819
1820int
1821ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1822 size_t key_size)
1823{
1824 struct ecryptfs_key_tfm *tmp_tfm;
1825 int rc = 0;
1826
1827 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1828 if (key_tfm != NULL)
1829 (*key_tfm) = tmp_tfm;
1830 if (!tmp_tfm) {
1831 rc = -ENOMEM;
1832 printk(KERN_ERR "Error attempting to allocate from "
1833 "ecryptfs_key_tfm_cache\n");
1834 goto out;
1835 }
1836 mutex_init(&tmp_tfm->key_tfm_mutex);
1837 strncpy(tmp_tfm->cipher_name, cipher_name,
1838 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
1839 tmp_tfm->key_size = key_size;
5dda6992
MH
1840 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1841 tmp_tfm->cipher_name,
1842 &tmp_tfm->key_size);
1843 if (rc) {
f4aad16a
MH
1844 printk(KERN_ERR "Error attempting to initialize key TFM "
1845 "cipher with name = [%s]; rc = [%d]\n",
1846 tmp_tfm->cipher_name, rc);
1847 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1848 if (key_tfm != NULL)
1849 (*key_tfm) = NULL;
1850 goto out;
1851 }
1852 mutex_lock(&key_tfm_list_mutex);
1853 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1854 mutex_unlock(&key_tfm_list_mutex);
1855out:
1856 return rc;
1857}
1858
1859int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1860 struct mutex **tfm_mutex,
1861 char *cipher_name)
1862{
1863 struct ecryptfs_key_tfm *key_tfm;
1864 int rc = 0;
1865
1866 (*tfm) = NULL;
1867 (*tfm_mutex) = NULL;
1868 mutex_lock(&key_tfm_list_mutex);
1869 list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) {
1870 if (strcmp(key_tfm->cipher_name, cipher_name) == 0) {
1871 (*tfm) = key_tfm->key_tfm;
1872 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1873 mutex_unlock(&key_tfm_list_mutex);
1874 goto out;
1875 }
1876 }
1877 mutex_unlock(&key_tfm_list_mutex);
5dda6992
MH
1878 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
1879 if (rc) {
f4aad16a
MH
1880 printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n",
1881 rc);
1882 goto out;
1883 }
1884 (*tfm) = key_tfm->key_tfm;
1885 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1886out:
1887 return rc;
1888}
This page took 0.217499 seconds and 5 git commands to generate.