blob: 4d1b2b4eb79ecf4b634e926ba39115d71b3e1e22 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08006 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07007 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompson <mcthomps@us.ibm.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/mount.h>
28#include <linux/pagemap.h>
29#include <linux/random.h>
30#include <linux/compiler.h>
31#include <linux/key.h>
32#include <linux/namei.h>
33#include <linux/crypto.h>
34#include <linux/file.h>
35#include <linux/scatterlist.h>
36#include "ecryptfs_kernel.h"
37
38static int
39ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
40 struct page *dst_page, int dst_offset,
41 struct page *src_page, int src_offset, int size,
42 unsigned char *iv);
43static int
44ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
45 struct page *dst_page, int dst_offset,
46 struct page *src_page, int src_offset, int size,
47 unsigned char *iv);
48
49/**
50 * ecryptfs_to_hex
51 * @dst: Buffer to take hex character representation of contents of
52 * src; must be at least of size (src_size * 2)
53 * @src: Buffer to be converted to a hex string respresentation
54 * @src_size: number of bytes to convert
55 */
56void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
57{
58 int x;
59
60 for (x = 0; x < src_size; x++)
61 sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
62}
63
64/**
65 * ecryptfs_from_hex
66 * @dst: Buffer to take the bytes from src hex; must be at least of
67 * size (src_size / 2)
68 * @src: Buffer to be converted from a hex string respresentation to raw value
69 * @dst_size: size of dst buffer, or number of hex characters pairs to convert
70 */
71void ecryptfs_from_hex(char *dst, char *src, int dst_size)
72{
73 int x;
74 char tmp[3] = { 0, };
75
76 for (x = 0; x < dst_size; x++) {
77 tmp[0] = src[x * 2];
78 tmp[1] = src[x * 2 + 1];
79 dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
80 }
81}
82
83/**
84 * ecryptfs_calculate_md5 - calculates the md5 of @src
85 * @dst: Pointer to 16 bytes of allocated memory
86 * @crypt_stat: Pointer to crypt_stat struct for the current inode
87 * @src: Data to be md5'd
88 * @len: Length of @src
89 *
90 * Uses the allocated crypto context that crypt_stat references to
91 * generate the MD5 sum of the contents of src.
92 */
93static int ecryptfs_calculate_md5(char *dst,
94 struct ecryptfs_crypt_stat *crypt_stat,
95 char *src, int len)
96{
Michael Halcrow237fead2006-10-04 02:16:22 -070097 struct scatterlist sg;
Michael Halcrow565d9722006-10-30 22:07:17 -080098 struct hash_desc desc = {
99 .tfm = crypt_stat->hash_tfm,
100 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
101 };
102 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700103
Michael Halcrow565d9722006-10-30 22:07:17 -0800104 mutex_lock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700105 sg_init_one(&sg, (u8 *)src, len);
Michael Halcrow565d9722006-10-30 22:07:17 -0800106 if (!desc.tfm) {
107 desc.tfm = crypto_alloc_hash(ECRYPTFS_DEFAULT_HASH, 0,
108 CRYPTO_ALG_ASYNC);
109 if (IS_ERR(desc.tfm)) {
110 rc = PTR_ERR(desc.tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700111 ecryptfs_printk(KERN_ERR, "Error attempting to "
Michael Halcrow565d9722006-10-30 22:07:17 -0800112 "allocate crypto context; rc = [%d]\n",
113 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700114 goto out;
115 }
Michael Halcrow565d9722006-10-30 22:07:17 -0800116 crypt_stat->hash_tfm = desc.tfm;
Michael Halcrow237fead2006-10-04 02:16:22 -0700117 }
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800118 rc = crypto_hash_init(&desc);
119 if (rc) {
120 printk(KERN_ERR
121 "%s: Error initializing crypto hash; rc = [%d]\n",
122 __FUNCTION__, rc);
123 goto out;
124 }
125 rc = crypto_hash_update(&desc, &sg, len);
126 if (rc) {
127 printk(KERN_ERR
128 "%s: Error updating crypto hash; rc = [%d]\n",
129 __FUNCTION__, rc);
130 goto out;
131 }
132 rc = crypto_hash_final(&desc, dst);
133 if (rc) {
134 printk(KERN_ERR
135 "%s: Error finalizing crypto hash; rc = [%d]\n",
136 __FUNCTION__, rc);
137 goto out;
138 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700139out:
Michael Halcrow8a29f2b2007-11-05 14:51:04 -0800140 mutex_unlock(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700141 return rc;
142}
143
Michael Halcrowcd9d67d2007-10-16 01:28:04 -0700144static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
145 char *cipher_name,
146 char *chaining_modifier)
Michael Halcrow8bba0662006-10-30 22:07:18 -0800147{
148 int cipher_name_len = strlen(cipher_name);
149 int chaining_modifier_len = strlen(chaining_modifier);
150 int algified_name_len;
151 int rc;
152
153 algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
154 (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
Michael Halcrow7bd473f2006-11-02 22:06:56 -0800155 if (!(*algified_name)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -0800156 rc = -ENOMEM;
157 goto out;
158 }
159 snprintf((*algified_name), algified_name_len, "%s(%s)",
160 chaining_modifier, cipher_name);
161 rc = 0;
162out:
163 return rc;
164}
165
Michael Halcrow237fead2006-10-04 02:16:22 -0700166/**
167 * ecryptfs_derive_iv
168 * @iv: destination for the derived iv vale
169 * @crypt_stat: Pointer to crypt_stat struct for the current inode
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700170 * @offset: Offset of the extent whose IV we are to derive
Michael Halcrow237fead2006-10-04 02:16:22 -0700171 *
172 * Generate the initialization vector from the given root IV and page
173 * offset.
174 *
175 * Returns zero on success; non-zero on error.
176 */
177static int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700178 loff_t offset)
Michael Halcrow237fead2006-10-04 02:16:22 -0700179{
180 int rc = 0;
181 char dst[MD5_DIGEST_SIZE];
182 char src[ECRYPTFS_MAX_IV_BYTES + 16];
183
184 if (unlikely(ecryptfs_verbosity > 0)) {
185 ecryptfs_printk(KERN_DEBUG, "root iv:\n");
186 ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
187 }
188 /* TODO: It is probably secure to just cast the least
189 * significant bits of the root IV into an unsigned long and
190 * add the offset to that rather than go through all this
191 * hashing business. -Halcrow */
192 memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
193 memset((src + crypt_stat->iv_bytes), 0, 16);
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700194 snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700195 if (unlikely(ecryptfs_verbosity > 0)) {
196 ecryptfs_printk(KERN_DEBUG, "source:\n");
197 ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
198 }
199 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
200 (crypt_stat->iv_bytes + 16));
201 if (rc) {
202 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
203 "MD5 while generating IV for a page\n");
204 goto out;
205 }
206 memcpy(iv, dst, crypt_stat->iv_bytes);
207 if (unlikely(ecryptfs_verbosity > 0)) {
208 ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
209 ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
210 }
211out:
212 return rc;
213}
214
215/**
216 * ecryptfs_init_crypt_stat
217 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
218 *
219 * Initialize the crypt_stat structure.
220 */
221void
222ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
223{
224 memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
Michael Halcrowf4aad162007-10-16 01:27:53 -0700225 INIT_LIST_HEAD(&crypt_stat->keysig_list);
226 mutex_init(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700227 mutex_init(&crypt_stat->cs_mutex);
228 mutex_init(&crypt_stat->cs_tfm_mutex);
Michael Halcrow565d9722006-10-30 22:07:17 -0800229 mutex_init(&crypt_stat->cs_hash_tfm_mutex);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800230 crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
Michael Halcrow237fead2006-10-04 02:16:22 -0700231}
232
233/**
Michael Halcrowfcd12832007-10-16 01:28:01 -0700234 * ecryptfs_destroy_crypt_stat
Michael Halcrow237fead2006-10-04 02:16:22 -0700235 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
236 *
237 * Releases all memory associated with a crypt_stat struct.
238 */
Michael Halcrowfcd12832007-10-16 01:28:01 -0700239void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -0700240{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700241 struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
242
Michael Halcrow237fead2006-10-04 02:16:22 -0700243 if (crypt_stat->tfm)
Michael Halcrow8bba0662006-10-30 22:07:18 -0800244 crypto_free_blkcipher(crypt_stat->tfm);
Michael Halcrow565d9722006-10-30 22:07:17 -0800245 if (crypt_stat->hash_tfm)
246 crypto_free_hash(crypt_stat->hash_tfm);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700247 mutex_lock(&crypt_stat->keysig_list_mutex);
248 list_for_each_entry_safe(key_sig, key_sig_tmp,
249 &crypt_stat->keysig_list, crypt_stat_list) {
250 list_del(&key_sig->crypt_stat_list);
251 kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
252 }
253 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700254 memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
255}
256
Michael Halcrowfcd12832007-10-16 01:28:01 -0700257void ecryptfs_destroy_mount_crypt_stat(
Michael Halcrow237fead2006-10-04 02:16:22 -0700258 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
259{
Michael Halcrowf4aad162007-10-16 01:27:53 -0700260 struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
261
262 if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
263 return;
264 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
265 list_for_each_entry_safe(auth_tok, auth_tok_tmp,
266 &mount_crypt_stat->global_auth_tok_list,
267 mount_crypt_stat_list) {
268 list_del(&auth_tok->mount_crypt_stat_list);
269 mount_crypt_stat->num_global_auth_toks--;
270 if (auth_tok->global_auth_tok_key
271 && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
272 key_put(auth_tok->global_auth_tok_key);
273 kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
274 }
275 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700276 memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
277}
278
279/**
280 * virt_to_scatterlist
281 * @addr: Virtual address
282 * @size: Size of data; should be an even multiple of the block size
283 * @sg: Pointer to scatterlist array; set to NULL to obtain only
284 * the number of scatterlist structs required in array
285 * @sg_size: Max array size
286 *
287 * Fills in a scatterlist array with page references for a passed
288 * virtual address.
289 *
290 * Returns the number of scatterlist structs in array used
291 */
292int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
293 int sg_size)
294{
295 int i = 0;
296 struct page *pg;
297 int offset;
298 int remainder_of_page;
299
Herbert Xu68e3f5d2007-10-27 00:52:07 -0700300 sg_init_table(sg, sg_size);
301
Michael Halcrow237fead2006-10-04 02:16:22 -0700302 while (size > 0 && i < sg_size) {
303 pg = virt_to_page(addr);
304 offset = offset_in_page(addr);
Jens Axboe642f1492007-10-24 11:20:47 +0200305 if (sg)
306 sg_set_page(&sg[i], pg, 0, offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700307 remainder_of_page = PAGE_CACHE_SIZE - offset;
308 if (size >= remainder_of_page) {
309 if (sg)
310 sg[i].length = remainder_of_page;
311 addr += remainder_of_page;
312 size -= remainder_of_page;
313 } else {
314 if (sg)
315 sg[i].length = size;
316 addr += size;
317 size = 0;
318 }
319 i++;
320 }
321 if (size > 0)
322 return -ENOMEM;
323 return i;
324}
325
326/**
327 * encrypt_scatterlist
328 * @crypt_stat: Pointer to the crypt_stat struct to initialize.
329 * @dest_sg: Destination of encrypted data
330 * @src_sg: Data to be encrypted
331 * @size: Length of data to be encrypted
332 * @iv: iv to use during encryption
333 *
334 * Returns the number of bytes encrypted; negative value on error
335 */
336static int encrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
337 struct scatterlist *dest_sg,
338 struct scatterlist *src_sg, int size,
339 unsigned char *iv)
340{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800341 struct blkcipher_desc desc = {
342 .tfm = crypt_stat->tfm,
343 .info = iv,
344 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
345 };
Michael Halcrow237fead2006-10-04 02:16:22 -0700346 int rc = 0;
347
348 BUG_ON(!crypt_stat || !crypt_stat->tfm
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800349 || !(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED));
Michael Halcrow237fead2006-10-04 02:16:22 -0700350 if (unlikely(ecryptfs_verbosity > 0)) {
351 ecryptfs_printk(KERN_DEBUG, "Key size [%d]; key:\n",
352 crypt_stat->key_size);
353 ecryptfs_dump_hex(crypt_stat->key,
354 crypt_stat->key_size);
355 }
356 /* Consider doing this once, when the file is opened */
357 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800358 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
359 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700360 if (rc) {
361 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
362 rc);
363 mutex_unlock(&crypt_stat->cs_tfm_mutex);
364 rc = -EINVAL;
365 goto out;
366 }
367 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes.\n", size);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800368 crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700369 mutex_unlock(&crypt_stat->cs_tfm_mutex);
370out:
371 return rc;
372}
373
Michael Halcrow237fead2006-10-04 02:16:22 -0700374/**
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700375 * ecryptfs_lower_offset_for_extent
376 *
377 * Convert an eCryptfs page index into a lower byte offset
378 */
Adrian Bunk7896b632008-02-06 01:38:32 -0800379static void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
380 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700381{
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800382 (*offset) = (crypt_stat->num_header_bytes_at_front
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700383 + (crypt_stat->extent_size * extent_num));
384}
385
386/**
387 * ecryptfs_encrypt_extent
388 * @enc_extent_page: Allocated page into which to encrypt the data in
389 * @page
390 * @crypt_stat: crypt_stat containing cryptographic context for the
391 * encryption operation
392 * @page: Page containing plaintext data extent to encrypt
393 * @extent_offset: Page extent offset for use in generating IV
394 *
395 * Encrypts one extent of data.
396 *
397 * Return zero on success; non-zero otherwise
398 */
399static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
400 struct ecryptfs_crypt_stat *crypt_stat,
401 struct page *page,
402 unsigned long extent_offset)
403{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700404 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700405 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
406 int rc;
407
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700408 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700409 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
410 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
411 (extent_base + extent_offset));
412 if (rc) {
413 ecryptfs_printk(KERN_ERR, "Error attempting to "
414 "derive IV for extent [0x%.16x]; "
415 "rc = [%d]\n", (extent_base + extent_offset),
416 rc);
417 goto out;
418 }
419 if (unlikely(ecryptfs_verbosity > 0)) {
420 ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
421 "with iv:\n");
422 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
423 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
424 "encryption:\n");
425 ecryptfs_dump_hex((char *)
426 (page_address(page)
427 + (extent_offset * crypt_stat->extent_size)),
428 8);
429 }
430 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
431 page, (extent_offset
432 * crypt_stat->extent_size),
433 crypt_stat->extent_size, extent_iv);
434 if (rc < 0) {
435 printk(KERN_ERR "%s: Error attempting to encrypt page with "
436 "page->index = [%ld], extent_offset = [%ld]; "
437 "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
438 rc);
439 goto out;
440 }
441 rc = 0;
442 if (unlikely(ecryptfs_verbosity > 0)) {
443 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
444 "rc = [%d]\n", (extent_base + extent_offset),
445 rc);
446 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
447 "encryption:\n");
448 ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
449 }
450out:
451 return rc;
452}
453
454/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700455 * ecryptfs_encrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700456 * @page: Page mapped from the eCryptfs inode for the file; contains
457 * decrypted content that needs to be encrypted (to a temporary
458 * page; not in place) and written out to the lower file
Michael Halcrow237fead2006-10-04 02:16:22 -0700459 *
460 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
461 * that eCryptfs pages may straddle the lower pages -- for instance,
462 * if the file was created on a machine with an 8K page size
463 * (resulting in an 8K header), and then the file is copied onto a
464 * host with a 32K page size, then when reading page 0 of the eCryptfs
465 * file, 24K of page 0 of the lower file will be read and decrypted,
466 * and then 8K of page 1 of the lower file will be read and decrypted.
467 *
Michael Halcrow237fead2006-10-04 02:16:22 -0700468 * Returns zero on success; negative on error
469 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700470int ecryptfs_encrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700471{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700472 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700473 struct ecryptfs_crypt_stat *crypt_stat;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700474 char *enc_extent_virt = NULL;
475 struct page *enc_extent_page;
476 loff_t extent_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700477 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700478
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700479 ecryptfs_inode = page->mapping->host;
480 crypt_stat =
481 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800482 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700483 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
484 0, PAGE_CACHE_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700485 if (rc)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700486 printk(KERN_ERR "%s: Error attempting to copy "
487 "page at index [%ld]\n", __FUNCTION__,
488 page->index);
Michael Halcrow237fead2006-10-04 02:16:22 -0700489 goto out;
490 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700491 enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
492 if (!enc_extent_virt) {
493 rc = -ENOMEM;
494 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
495 "encrypted extent\n");
496 goto out;
497 }
498 enc_extent_page = virt_to_page(enc_extent_virt);
499 for (extent_offset = 0;
500 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
501 extent_offset++) {
502 loff_t offset;
503
504 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
505 extent_offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700506 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700507 printk(KERN_ERR "%s: Error encrypting extent; "
508 "rc = [%d]\n", __FUNCTION__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700509 goto out;
510 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700511 ecryptfs_lower_offset_for_extent(
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700512 &offset, ((((loff_t)page->index)
513 * (PAGE_CACHE_SIZE
514 / crypt_stat->extent_size))
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700515 + extent_offset), crypt_stat);
516 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
517 offset, crypt_stat->extent_size);
518 if (rc) {
519 ecryptfs_printk(KERN_ERR, "Error attempting "
520 "to write lower page; rc = [%d]"
521 "\n", rc);
522 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700523 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700524 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700525out:
526 kfree(enc_extent_virt);
527 return rc;
528}
529
530static int ecryptfs_decrypt_extent(struct page *page,
531 struct ecryptfs_crypt_stat *crypt_stat,
532 struct page *enc_extent_page,
533 unsigned long extent_offset)
534{
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700535 loff_t extent_base;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700536 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
537 int rc;
538
Michael Halcrowd6a13c12007-10-16 01:28:12 -0700539 extent_base = (((loff_t)page->index)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700540 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
541 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
542 (extent_base + extent_offset));
Michael Halcrow237fead2006-10-04 02:16:22 -0700543 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700544 ecryptfs_printk(KERN_ERR, "Error attempting to "
545 "derive IV for extent [0x%.16x]; "
546 "rc = [%d]\n", (extent_base + extent_offset),
547 rc);
548 goto out;
549 }
550 if (unlikely(ecryptfs_verbosity > 0)) {
551 ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
552 "with iv:\n");
553 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
554 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
555 "decryption:\n");
556 ecryptfs_dump_hex((char *)
557 (page_address(enc_extent_page)
558 + (extent_offset * crypt_stat->extent_size)),
559 8);
560 }
561 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
562 (extent_offset
563 * crypt_stat->extent_size),
564 enc_extent_page, 0,
565 crypt_stat->extent_size, extent_iv);
566 if (rc < 0) {
567 printk(KERN_ERR "%s: Error attempting to decrypt to page with "
568 "page->index = [%ld], extent_offset = [%ld]; "
569 "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
570 rc);
571 goto out;
572 }
573 rc = 0;
574 if (unlikely(ecryptfs_verbosity > 0)) {
575 ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
576 "rc = [%d]\n", (extent_base + extent_offset),
577 rc);
578 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
579 "decryption:\n");
580 ecryptfs_dump_hex((char *)(page_address(page)
581 + (extent_offset
582 * crypt_stat->extent_size)), 8);
Michael Halcrow237fead2006-10-04 02:16:22 -0700583 }
584out:
585 return rc;
586}
587
588/**
589 * ecryptfs_decrypt_page
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700590 * @page: Page mapped from the eCryptfs inode for the file; data read
591 * and decrypted from the lower file will be written into this
592 * page
Michael Halcrow237fead2006-10-04 02:16:22 -0700593 *
594 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
595 * that eCryptfs pages may straddle the lower pages -- for instance,
596 * if the file was created on a machine with an 8K page size
597 * (resulting in an 8K header), and then the file is copied onto a
598 * host with a 32K page size, then when reading page 0 of the eCryptfs
599 * file, 24K of page 0 of the lower file will be read and decrypted,
600 * and then 8K of page 1 of the lower file will be read and decrypted.
601 *
602 * Returns zero on success; negative on error
603 */
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700604int ecryptfs_decrypt_page(struct page *page)
Michael Halcrow237fead2006-10-04 02:16:22 -0700605{
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700606 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700607 struct ecryptfs_crypt_stat *crypt_stat;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700608 char *enc_extent_virt = NULL;
609 struct page *enc_extent_page;
610 unsigned long extent_offset;
Michael Halcrow237fead2006-10-04 02:16:22 -0700611 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700612
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700613 ecryptfs_inode = page->mapping->host;
614 crypt_stat =
615 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800616 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700617 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
618 PAGE_CACHE_SIZE,
619 ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700620 if (rc)
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700621 printk(KERN_ERR "%s: Error attempting to copy "
622 "page at index [%ld]\n", __FUNCTION__,
623 page->index);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700624 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700625 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700626 enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
627 if (!enc_extent_virt) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700628 rc = -ENOMEM;
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700629 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
630 "encrypted extent\n");
Michael Halcrow16a72c42007-10-16 01:28:14 -0700631 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700632 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700633 enc_extent_page = virt_to_page(enc_extent_virt);
634 for (extent_offset = 0;
635 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
636 extent_offset++) {
637 loff_t offset;
638
639 ecryptfs_lower_offset_for_extent(
640 &offset, ((page->index * (PAGE_CACHE_SIZE
641 / crypt_stat->extent_size))
642 + extent_offset), crypt_stat);
643 rc = ecryptfs_read_lower(enc_extent_virt, offset,
644 crypt_stat->extent_size,
645 ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700646 if (rc) {
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700647 ecryptfs_printk(KERN_ERR, "Error attempting "
648 "to read lower page; rc = [%d]"
649 "\n", rc);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700650 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700651 }
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700652 rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
653 extent_offset);
654 if (rc) {
655 printk(KERN_ERR "%s: Error encrypting extent; "
656 "rc = [%d]\n", __FUNCTION__, rc);
Michael Halcrow16a72c42007-10-16 01:28:14 -0700657 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700658 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700659 }
660out:
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700661 kfree(enc_extent_virt);
Michael Halcrow237fead2006-10-04 02:16:22 -0700662 return rc;
663}
664
665/**
666 * decrypt_scatterlist
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700667 * @crypt_stat: Cryptographic context
668 * @dest_sg: The destination scatterlist to decrypt into
669 * @src_sg: The source scatterlist to decrypt from
670 * @size: The number of bytes to decrypt
671 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700672 *
673 * Returns the number of bytes decrypted; negative value on error
674 */
675static int decrypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
676 struct scatterlist *dest_sg,
677 struct scatterlist *src_sg, int size,
678 unsigned char *iv)
679{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800680 struct blkcipher_desc desc = {
681 .tfm = crypt_stat->tfm,
682 .info = iv,
683 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
684 };
Michael Halcrow237fead2006-10-04 02:16:22 -0700685 int rc = 0;
686
687 /* Consider doing this once, when the file is opened */
688 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800689 rc = crypto_blkcipher_setkey(crypt_stat->tfm, crypt_stat->key,
690 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700691 if (rc) {
692 ecryptfs_printk(KERN_ERR, "Error setting key; rc = [%d]\n",
693 rc);
694 mutex_unlock(&crypt_stat->cs_tfm_mutex);
695 rc = -EINVAL;
696 goto out;
697 }
698 ecryptfs_printk(KERN_DEBUG, "Decrypting [%d] bytes.\n", size);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800699 rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700700 mutex_unlock(&crypt_stat->cs_tfm_mutex);
701 if (rc) {
702 ecryptfs_printk(KERN_ERR, "Error decrypting; rc = [%d]\n",
703 rc);
704 goto out;
705 }
706 rc = size;
707out:
708 return rc;
709}
710
711/**
712 * ecryptfs_encrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700713 * @crypt_stat: The cryptographic context
714 * @dst_page: The page to encrypt into
715 * @dst_offset: The offset in the page to encrypt into
716 * @src_page: The page to encrypt from
717 * @src_offset: The offset in the page to encrypt from
718 * @size: The number of bytes to encrypt
719 * @iv: The initialization vector to use for the encryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700720 *
721 * Returns the number of bytes encrypted
722 */
723static int
724ecryptfs_encrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
725 struct page *dst_page, int dst_offset,
726 struct page *src_page, int src_offset, int size,
727 unsigned char *iv)
728{
729 struct scatterlist src_sg, dst_sg;
730
Jens Axboe60c74f82007-10-22 19:43:30 +0200731 sg_init_table(&src_sg, 1);
732 sg_init_table(&dst_sg, 1);
733
Jens Axboe642f1492007-10-24 11:20:47 +0200734 sg_set_page(&src_sg, src_page, size, src_offset);
735 sg_set_page(&dst_sg, dst_page, size, dst_offset);
Michael Halcrow237fead2006-10-04 02:16:22 -0700736 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
737}
738
739/**
740 * ecryptfs_decrypt_page_offset
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700741 * @crypt_stat: The cryptographic context
742 * @dst_page: The page to decrypt into
743 * @dst_offset: The offset in the page to decrypt into
744 * @src_page: The page to decrypt from
745 * @src_offset: The offset in the page to decrypt from
746 * @size: The number of bytes to decrypt
747 * @iv: The initialization vector to use for the decryption
Michael Halcrow237fead2006-10-04 02:16:22 -0700748 *
749 * Returns the number of bytes decrypted
750 */
751static int
752ecryptfs_decrypt_page_offset(struct ecryptfs_crypt_stat *crypt_stat,
753 struct page *dst_page, int dst_offset,
754 struct page *src_page, int src_offset, int size,
755 unsigned char *iv)
756{
757 struct scatterlist src_sg, dst_sg;
758
Jens Axboe60c74f82007-10-22 19:43:30 +0200759 sg_init_table(&src_sg, 1);
Jens Axboe642f1492007-10-24 11:20:47 +0200760 sg_set_page(&src_sg, src_page, size, src_offset);
Jens Axboe60c74f82007-10-22 19:43:30 +0200761
Jens Axboe642f1492007-10-24 11:20:47 +0200762 sg_init_table(&dst_sg, 1);
763 sg_set_page(&dst_sg, dst_page, size, dst_offset);
764
Michael Halcrow237fead2006-10-04 02:16:22 -0700765 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
766}
767
768#define ECRYPTFS_MAX_SCATTERLIST_LEN 4
769
770/**
771 * ecryptfs_init_crypt_ctx
772 * @crypt_stat: Uninitilized crypt stats structure
773 *
774 * Initialize the crypto context.
775 *
776 * TODO: Performance: Keep a cache of initialized cipher contexts;
777 * only init if needed
778 */
779int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
780{
Michael Halcrow8bba0662006-10-30 22:07:18 -0800781 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -0700782 int rc = -EINVAL;
783
784 if (!crypt_stat->cipher) {
785 ecryptfs_printk(KERN_ERR, "No cipher specified\n");
786 goto out;
787 }
788 ecryptfs_printk(KERN_DEBUG,
789 "Initializing cipher [%s]; strlen = [%d]; "
790 "key_size_bits = [%d]\n",
791 crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
792 crypt_stat->key_size << 3);
793 if (crypt_stat->tfm) {
794 rc = 0;
795 goto out;
796 }
797 mutex_lock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -0800798 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
799 crypt_stat->cipher, "cbc");
800 if (rc)
Eric Sandeenc8161f62007-12-22 14:03:26 -0800801 goto out_unlock;
Michael Halcrow8bba0662006-10-30 22:07:18 -0800802 crypt_stat->tfm = crypto_alloc_blkcipher(full_alg_name, 0,
803 CRYPTO_ALG_ASYNC);
804 kfree(full_alg_name);
Akinobu Mitade887772006-11-28 12:29:49 -0800805 if (IS_ERR(crypt_stat->tfm)) {
806 rc = PTR_ERR(crypt_stat->tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -0700807 ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
808 "Error initializing cipher [%s]\n",
809 crypt_stat->cipher);
Eric Sandeenc8161f62007-12-22 14:03:26 -0800810 goto out_unlock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700811 }
Herbert Xuf1ddcaf2007-01-27 10:05:15 +1100812 crypto_blkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
Michael Halcrow237fead2006-10-04 02:16:22 -0700813 rc = 0;
Eric Sandeenc8161f62007-12-22 14:03:26 -0800814out_unlock:
815 mutex_unlock(&crypt_stat->cs_tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700816out:
817 return rc;
818}
819
820static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
821{
822 int extent_size_tmp;
823
824 crypt_stat->extent_mask = 0xFFFFFFFF;
825 crypt_stat->extent_shift = 0;
826 if (crypt_stat->extent_size == 0)
827 return;
828 extent_size_tmp = crypt_stat->extent_size;
829 while ((extent_size_tmp & 0x01) == 0) {
830 extent_size_tmp >>= 1;
831 crypt_stat->extent_mask <<= 1;
832 crypt_stat->extent_shift++;
833 }
834}
835
836void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
837{
838 /* Default values; may be overwritten as we are parsing the
839 * packets. */
840 crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
841 set_extent_mask_and_shift(crypt_stat);
842 crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800843 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800844 crypt_stat->num_header_bytes_at_front = 0;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700845 else {
846 if (PAGE_CACHE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800847 crypt_stat->num_header_bytes_at_front =
848 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700849 else
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800850 crypt_stat->num_header_bytes_at_front = PAGE_CACHE_SIZE;
Michael Halcrow45eaab72007-10-16 01:28:05 -0700851 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700852}
853
854/**
855 * ecryptfs_compute_root_iv
856 * @crypt_stats
857 *
858 * On error, sets the root IV to all 0's.
859 */
860int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
861{
862 int rc = 0;
863 char dst[MD5_DIGEST_SIZE];
864
865 BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
866 BUG_ON(crypt_stat->iv_bytes <= 0);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800867 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700868 rc = -EINVAL;
869 ecryptfs_printk(KERN_WARNING, "Session key not valid; "
870 "cannot generate root IV\n");
871 goto out;
872 }
873 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
874 crypt_stat->key_size);
875 if (rc) {
876 ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
877 "MD5 while generating root IV\n");
878 goto out;
879 }
880 memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
881out:
882 if (rc) {
883 memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800884 crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
Michael Halcrow237fead2006-10-04 02:16:22 -0700885 }
886 return rc;
887}
888
889static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
890{
891 get_random_bytes(crypt_stat->key, crypt_stat->key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800892 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrow237fead2006-10-04 02:16:22 -0700893 ecryptfs_compute_root_iv(crypt_stat);
894 if (unlikely(ecryptfs_verbosity > 0)) {
895 ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
896 ecryptfs_dump_hex(crypt_stat->key,
897 crypt_stat->key_size);
898 }
899}
900
901/**
Michael Halcrow17398952007-02-12 00:53:45 -0800902 * ecryptfs_copy_mount_wide_flags_to_inode_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700903 * @crypt_stat: The inode's cryptographic context
904 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow17398952007-02-12 00:53:45 -0800905 *
906 * This function propagates the mount-wide flags to individual inode
907 * flags.
908 */
909static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
910 struct ecryptfs_crypt_stat *crypt_stat,
911 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
912{
913 if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
914 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
915 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
916 crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
917}
918
Michael Halcrowf4aad162007-10-16 01:27:53 -0700919static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
920 struct ecryptfs_crypt_stat *crypt_stat,
921 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
922{
923 struct ecryptfs_global_auth_tok *global_auth_tok;
924 int rc = 0;
925
926 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
927 list_for_each_entry(global_auth_tok,
928 &mount_crypt_stat->global_auth_tok_list,
929 mount_crypt_stat_list) {
930 rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
931 if (rc) {
932 printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
933 mutex_unlock(
934 &mount_crypt_stat->global_auth_tok_list_mutex);
935 goto out;
936 }
937 }
938 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
939out:
940 return rc;
941}
942
Michael Halcrow17398952007-02-12 00:53:45 -0800943/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700944 * ecryptfs_set_default_crypt_stat_vals
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700945 * @crypt_stat: The inode's cryptographic context
946 * @mount_crypt_stat: The mount point's cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -0700947 *
948 * Default values in the event that policy does not override them.
949 */
950static void ecryptfs_set_default_crypt_stat_vals(
951 struct ecryptfs_crypt_stat *crypt_stat,
952 struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
953{
Michael Halcrow17398952007-02-12 00:53:45 -0800954 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
955 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700956 ecryptfs_set_default_sizes(crypt_stat);
957 strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
958 crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800959 crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
Michael Halcrow237fead2006-10-04 02:16:22 -0700960 crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
961 crypt_stat->mount_crypt_stat = mount_crypt_stat;
962}
963
964/**
965 * ecryptfs_new_file_context
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700966 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -0700967 *
968 * If the crypto context for the file has not yet been established,
969 * this is where we do that. Establishing a new crypto context
970 * involves the following decisions:
971 * - What cipher to use?
972 * - What set of authentication tokens to use?
973 * Here we just worry about getting enough information into the
974 * authentication tokens so that we know that they are available.
975 * We associate the available authentication tokens with the new file
976 * via the set of signatures in the crypt_stat struct. Later, when
977 * the headers are actually written out, we may again defer to
978 * userspace to perform the encryption of the session key; for the
979 * foreseeable future, this will be the case with public key packets.
980 *
981 * Returns zero on success; non-zero otherwise
982 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700983int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
984{
Michael Halcrow237fead2006-10-04 02:16:22 -0700985 struct ecryptfs_crypt_stat *crypt_stat =
986 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
987 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
988 &ecryptfs_superblock_to_private(
989 ecryptfs_dentry->d_sb)->mount_crypt_stat;
990 int cipher_name_len;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700991 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700992
993 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
Michael Halcrowaf655dc2007-10-16 01:28:00 -0700994 crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
Michael Halcrowf4aad162007-10-16 01:27:53 -0700995 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
996 mount_crypt_stat);
997 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
998 mount_crypt_stat);
999 if (rc) {
1000 printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
1001 "to the inode key sigs; rc = [%d]\n", rc);
1002 goto out;
1003 }
1004 cipher_name_len =
1005 strlen(mount_crypt_stat->global_default_cipher_name);
1006 memcpy(crypt_stat->cipher,
1007 mount_crypt_stat->global_default_cipher_name,
1008 cipher_name_len);
1009 crypt_stat->cipher[cipher_name_len] = '\0';
1010 crypt_stat->key_size =
1011 mount_crypt_stat->global_default_cipher_key_size;
1012 ecryptfs_generate_new_key(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001013 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1014 if (rc)
1015 ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
1016 "context for cipher [%s]: rc = [%d]\n",
1017 crypt_stat->cipher, rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001018out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001019 return rc;
1020}
1021
1022/**
1023 * contains_ecryptfs_marker - check for the ecryptfs marker
1024 * @data: The data block in which to check
1025 *
1026 * Returns one if marker found; zero if not found
1027 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001028static int contains_ecryptfs_marker(char *data)
Michael Halcrow237fead2006-10-04 02:16:22 -07001029{
1030 u32 m_1, m_2;
1031
1032 memcpy(&m_1, data, 4);
1033 m_1 = be32_to_cpu(m_1);
1034 memcpy(&m_2, (data + 4), 4);
1035 m_2 = be32_to_cpu(m_2);
1036 if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
1037 return 1;
1038 ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
1039 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1040 MAGIC_ECRYPTFS_MARKER);
1041 ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
1042 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1043 return 0;
1044}
1045
1046struct ecryptfs_flag_map_elem {
1047 u32 file_flag;
1048 u32 local_flag;
1049};
1050
1051/* Add support for additional flags by adding elements here. */
1052static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
1053 {0x00000001, ECRYPTFS_ENABLE_HMAC},
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001054 {0x00000002, ECRYPTFS_ENCRYPTED},
1055 {0x00000004, ECRYPTFS_METADATA_IN_XATTR}
Michael Halcrow237fead2006-10-04 02:16:22 -07001056};
1057
1058/**
1059 * ecryptfs_process_flags
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001060 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001061 * @page_virt: Source data to be parsed
1062 * @bytes_read: Updated with the number of bytes read
1063 *
1064 * Returns zero on success; non-zero if the flag set is invalid
1065 */
1066static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
1067 char *page_virt, int *bytes_read)
1068{
1069 int rc = 0;
1070 int i;
1071 u32 flags;
1072
1073 memcpy(&flags, page_virt, 4);
1074 flags = be32_to_cpu(flags);
1075 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1076 / sizeof(struct ecryptfs_flag_map_elem))); i++)
1077 if (flags & ecryptfs_flag_map[i].file_flag) {
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001078 crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
Michael Halcrow237fead2006-10-04 02:16:22 -07001079 } else
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001080 crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
Michael Halcrow237fead2006-10-04 02:16:22 -07001081 /* Version is in top 8 bits of the 32-bit flag vector */
1082 crypt_stat->file_version = ((flags >> 24) & 0xFF);
1083 (*bytes_read) = 4;
1084 return rc;
1085}
1086
1087/**
1088 * write_ecryptfs_marker
1089 * @page_virt: The pointer to in a page to begin writing the marker
1090 * @written: Number of bytes written
1091 *
1092 * Marker = 0x3c81b7f5
1093 */
1094static void write_ecryptfs_marker(char *page_virt, size_t *written)
1095{
1096 u32 m_1, m_2;
1097
1098 get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1099 m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
1100 m_1 = cpu_to_be32(m_1);
1101 memcpy(page_virt, &m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1102 m_2 = cpu_to_be32(m_2);
1103 memcpy(page_virt + (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2), &m_2,
1104 (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
1105 (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1106}
1107
1108static void
1109write_ecryptfs_flags(char *page_virt, struct ecryptfs_crypt_stat *crypt_stat,
1110 size_t *written)
1111{
1112 u32 flags = 0;
1113 int i;
1114
1115 for (i = 0; i < ((sizeof(ecryptfs_flag_map)
1116 / sizeof(struct ecryptfs_flag_map_elem))); i++)
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001117 if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
Michael Halcrow237fead2006-10-04 02:16:22 -07001118 flags |= ecryptfs_flag_map[i].file_flag;
1119 /* Version is in top 8 bits of the 32-bit flag vector */
1120 flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
1121 flags = cpu_to_be32(flags);
1122 memcpy(page_virt, &flags, 4);
1123 (*written) = 4;
1124}
1125
1126struct ecryptfs_cipher_code_str_map_elem {
1127 char cipher_str[16];
1128 u16 cipher_code;
1129};
1130
1131/* Add support for additional ciphers by adding elements here. The
1132 * cipher_code is whatever OpenPGP applicatoins use to identify the
1133 * ciphers. List in order of probability. */
1134static struct ecryptfs_cipher_code_str_map_elem
1135ecryptfs_cipher_code_str_map[] = {
1136 {"aes",RFC2440_CIPHER_AES_128 },
1137 {"blowfish", RFC2440_CIPHER_BLOWFISH},
1138 {"des3_ede", RFC2440_CIPHER_DES3_EDE},
1139 {"cast5", RFC2440_CIPHER_CAST_5},
1140 {"twofish", RFC2440_CIPHER_TWOFISH},
1141 {"cast6", RFC2440_CIPHER_CAST_6},
1142 {"aes", RFC2440_CIPHER_AES_192},
1143 {"aes", RFC2440_CIPHER_AES_256}
1144};
1145
1146/**
1147 * ecryptfs_code_for_cipher_string
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001148 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001149 *
1150 * Returns zero on no match, or the cipher code on match
1151 */
1152u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat)
1153{
1154 int i;
1155 u16 code = 0;
1156 struct ecryptfs_cipher_code_str_map_elem *map =
1157 ecryptfs_cipher_code_str_map;
1158
1159 if (strcmp(crypt_stat->cipher, "aes") == 0) {
1160 switch (crypt_stat->key_size) {
1161 case 16:
1162 code = RFC2440_CIPHER_AES_128;
1163 break;
1164 case 24:
1165 code = RFC2440_CIPHER_AES_192;
1166 break;
1167 case 32:
1168 code = RFC2440_CIPHER_AES_256;
1169 }
1170 } else {
1171 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1172 if (strcmp(crypt_stat->cipher, map[i].cipher_str) == 0){
1173 code = map[i].cipher_code;
1174 break;
1175 }
1176 }
1177 return code;
1178}
1179
1180/**
1181 * ecryptfs_cipher_code_to_string
1182 * @str: Destination to write out the cipher name
1183 * @cipher_code: The code to convert to cipher name string
1184 *
1185 * Returns zero on success
1186 */
1187int ecryptfs_cipher_code_to_string(char *str, u16 cipher_code)
1188{
1189 int rc = 0;
1190 int i;
1191
1192 str[0] = '\0';
1193 for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1194 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1195 strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
1196 if (str[0] == '\0') {
1197 ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
1198 "[%d]\n", cipher_code);
1199 rc = -EINVAL;
1200 }
1201 return rc;
1202}
1203
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001204int ecryptfs_read_and_validate_header_region(char *data,
1205 struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001206{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001207 struct ecryptfs_crypt_stat *crypt_stat =
1208 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001209 int rc;
1210
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001211 rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
1212 ecryptfs_inode);
1213 if (rc) {
1214 printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
1215 __FUNCTION__, rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001216 goto out;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001217 }
1218 if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001219 rc = -EINVAL;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001220 ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n");
1221 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001222out:
1223 return rc;
1224}
1225
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001226void
1227ecryptfs_write_header_metadata(char *virt,
1228 struct ecryptfs_crypt_stat *crypt_stat,
1229 size_t *written)
Michael Halcrow237fead2006-10-04 02:16:22 -07001230{
1231 u32 header_extent_size;
1232 u16 num_header_extents_at_front;
1233
Michael Halcrow45eaab72007-10-16 01:28:05 -07001234 header_extent_size = (u32)crypt_stat->extent_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001235 num_header_extents_at_front =
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001236 (u16)(crypt_stat->num_header_bytes_at_front
1237 / crypt_stat->extent_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001238 header_extent_size = cpu_to_be32(header_extent_size);
1239 memcpy(virt, &header_extent_size, 4);
1240 virt += 4;
1241 num_header_extents_at_front = cpu_to_be16(num_header_extents_at_front);
1242 memcpy(virt, &num_header_extents_at_front, 2);
1243 (*written) = 6;
1244}
1245
1246struct kmem_cache *ecryptfs_header_cache_0;
1247struct kmem_cache *ecryptfs_header_cache_1;
1248struct kmem_cache *ecryptfs_header_cache_2;
1249
1250/**
1251 * ecryptfs_write_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001252 * @page_virt: The virtual address to write the headers to
1253 * @size: Set to the number of bytes written by this function
1254 * @crypt_stat: The cryptographic context
1255 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001256 *
1257 * Format version: 1
1258 *
1259 * Header Extent:
1260 * Octets 0-7: Unencrypted file size (big-endian)
1261 * Octets 8-15: eCryptfs special marker
1262 * Octets 16-19: Flags
1263 * Octet 16: File format version number (between 0 and 255)
1264 * Octets 17-18: Reserved
1265 * Octet 19: Bit 1 (lsb): Reserved
1266 * Bit 2: Encrypted?
1267 * Bits 3-8: Reserved
1268 * Octets 20-23: Header extent size (big-endian)
1269 * Octets 24-25: Number of header extents at front of file
1270 * (big-endian)
1271 * Octet 26: Begin RFC 2440 authentication token packet set
1272 * Data Extent 0:
1273 * Lower data (CBC encrypted)
1274 * Data Extent 1:
1275 * Lower data (CBC encrypted)
1276 * ...
1277 *
1278 * Returns zero on success
1279 */
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001280static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
1281 struct ecryptfs_crypt_stat *crypt_stat,
1282 struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001283{
1284 int rc;
1285 size_t written;
1286 size_t offset;
1287
1288 offset = ECRYPTFS_FILE_SIZE_BYTES;
1289 write_ecryptfs_marker((page_virt + offset), &written);
1290 offset += written;
1291 write_ecryptfs_flags((page_virt + offset), crypt_stat, &written);
1292 offset += written;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001293 ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
1294 &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001295 offset += written;
1296 rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
1297 ecryptfs_dentry, &written,
1298 PAGE_CACHE_SIZE - offset);
1299 if (rc)
1300 ecryptfs_printk(KERN_WARNING, "Error generating key packet "
1301 "set; rc = [%d]\n", rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001302 if (size) {
1303 offset += written;
1304 *size = offset;
1305 }
1306 return rc;
1307}
1308
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001309static int
1310ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001311 struct dentry *ecryptfs_dentry,
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001312 char *virt)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001313{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001314 int rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001315
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001316 rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, virt,
1317 0, crypt_stat->num_header_bytes_at_front);
1318 if (rc)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001319 printk(KERN_ERR "%s: Error attempting to write header "
1320 "information to lower file; rc = [%d]\n", __FUNCTION__,
1321 rc);
Michael Halcrow70456602007-02-12 00:53:48 -08001322 return rc;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001323}
1324
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001325static int
1326ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
1327 struct ecryptfs_crypt_stat *crypt_stat,
1328 char *page_virt, size_t size)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001329{
1330 int rc;
1331
1332 rc = ecryptfs_setxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME, page_virt,
1333 size, 0);
Michael Halcrow237fead2006-10-04 02:16:22 -07001334 return rc;
1335}
1336
1337/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001338 * ecryptfs_write_metadata
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001339 * @ecryptfs_dentry: The eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -07001340 *
1341 * Write the file headers out. This will likely involve a userspace
1342 * callout, in which the session key is encrypted with one or more
1343 * public keys and/or the passphrase necessary to do the encryption is
1344 * retrieved via a prompt. Exactly what happens at this point should
1345 * be policy-dependent.
1346 *
1347 * Returns zero on success; non-zero on error
1348 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001349int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001350{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001351 struct ecryptfs_crypt_stat *crypt_stat =
1352 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001353 char *virt;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001354 size_t size = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001355 int rc = 0;
1356
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001357 if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
1358 if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001359 printk(KERN_ERR "Key is invalid; bailing out\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001360 rc = -EINVAL;
1361 goto out;
1362 }
1363 } else {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001364 printk(KERN_WARNING "%s: Encrypted flag not set\n",
1365 __FUNCTION__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001366 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001367 goto out;
1368 }
1369 /* Released in this function */
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001370 virt = kzalloc(crypt_stat->num_header_bytes_at_front, GFP_KERNEL);
1371 if (!virt) {
1372 printk(KERN_ERR "%s: Out of memory\n", __FUNCTION__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001373 rc = -ENOMEM;
1374 goto out;
1375 }
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001376 rc = ecryptfs_write_headers_virt(virt, &size, crypt_stat,
1377 ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -07001378 if (unlikely(rc)) {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001379 printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
1380 __FUNCTION__, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001381 goto out_free;
1382 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001383 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
1384 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry,
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001385 crypt_stat, virt, size);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001386 else
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001387 rc = ecryptfs_write_metadata_to_contents(crypt_stat,
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001388 ecryptfs_dentry, virt);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001389 if (rc) {
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001390 printk(KERN_ERR "%s: Error writing metadata out to lower file; "
1391 "rc = [%d]\n", __FUNCTION__, rc);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001392 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001393 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001394out_free:
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001395 memset(virt, 0, crypt_stat->num_header_bytes_at_front);
1396 kfree(virt);
Michael Halcrow237fead2006-10-04 02:16:22 -07001397out:
1398 return rc;
1399}
1400
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001401#define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1402#define ECRYPTFS_VALIDATE_HEADER_SIZE 1
Michael Halcrow237fead2006-10-04 02:16:22 -07001403static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001404 char *virt, int *bytes_read,
1405 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001406{
1407 int rc = 0;
1408 u32 header_extent_size;
1409 u16 num_header_extents_at_front;
1410
Michael Halcrowecbdc932007-10-16 01:28:14 -07001411 memcpy(&header_extent_size, virt, sizeof(u32));
Michael Halcrow237fead2006-10-04 02:16:22 -07001412 header_extent_size = be32_to_cpu(header_extent_size);
Michael Halcrowecbdc932007-10-16 01:28:14 -07001413 virt += sizeof(u32);
1414 memcpy(&num_header_extents_at_front, virt, sizeof(u16));
Michael Halcrow237fead2006-10-04 02:16:22 -07001415 num_header_extents_at_front = be16_to_cpu(num_header_extents_at_front);
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001416 crypt_stat->num_header_bytes_at_front =
1417 (((size_t)num_header_extents_at_front
1418 * (size_t)header_extent_size));
Michael Halcrow45eaab72007-10-16 01:28:05 -07001419 (*bytes_read) = (sizeof(u32) + sizeof(u16));
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001420 if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001421 && (crypt_stat->num_header_bytes_at_front
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001422 < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001423 rc = -EINVAL;
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001424 printk(KERN_WARNING "Invalid header size: [%zd]\n",
1425 crypt_stat->num_header_bytes_at_front);
Michael Halcrow237fead2006-10-04 02:16:22 -07001426 }
1427 return rc;
1428}
1429
1430/**
1431 * set_default_header_data
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001432 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001433 *
1434 * For version 0 file format; this function is only for backwards
1435 * compatibility for files created with the prior versions of
1436 * eCryptfs.
1437 */
1438static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
1439{
Michael Halcrowcc11bef2008-02-06 01:38:32 -08001440 crypt_stat->num_header_bytes_at_front =
1441 ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -07001442}
1443
1444/**
1445 * ecryptfs_read_headers_virt
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001446 * @page_virt: The virtual address into which to read the headers
1447 * @crypt_stat: The cryptographic context
1448 * @ecryptfs_dentry: The eCryptfs dentry
1449 * @validate_header_size: Whether to validate the header size while reading
Michael Halcrow237fead2006-10-04 02:16:22 -07001450 *
1451 * Read/parse the header data. The header format is detailed in the
1452 * comment block for the ecryptfs_write_headers_virt() function.
1453 *
1454 * Returns zero on success
1455 */
1456static int ecryptfs_read_headers_virt(char *page_virt,
1457 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001458 struct dentry *ecryptfs_dentry,
1459 int validate_header_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001460{
1461 int rc = 0;
1462 int offset;
1463 int bytes_read;
1464
1465 ecryptfs_set_default_sizes(crypt_stat);
1466 crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
1467 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1468 offset = ECRYPTFS_FILE_SIZE_BYTES;
1469 rc = contains_ecryptfs_marker(page_virt + offset);
1470 if (rc == 0) {
1471 rc = -EINVAL;
1472 goto out;
1473 }
1474 offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
1475 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1476 &bytes_read);
1477 if (rc) {
1478 ecryptfs_printk(KERN_WARNING, "Error processing flags\n");
1479 goto out;
1480 }
1481 if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
1482 ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
1483 "file version [%d] is supported by this "
1484 "version of eCryptfs\n",
1485 crypt_stat->file_version,
1486 ECRYPTFS_SUPPORTED_FILE_VERSION);
1487 rc = -EINVAL;
1488 goto out;
1489 }
1490 offset += bytes_read;
1491 if (crypt_stat->file_version >= 1) {
1492 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001493 &bytes_read, validate_header_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001494 if (rc) {
1495 ecryptfs_printk(KERN_WARNING, "Error reading header "
1496 "metadata; rc = [%d]\n", rc);
1497 }
1498 offset += bytes_read;
1499 } else
1500 set_default_header_data(crypt_stat);
1501 rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
1502 ecryptfs_dentry);
1503out:
1504 return rc;
1505}
1506
1507/**
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001508 * ecryptfs_read_xattr_region
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001509 * @page_virt: The vitual address into which to read the xattr data
Michael Halcrow2ed92552007-10-16 01:28:10 -07001510 * @ecryptfs_inode: The eCryptfs inode
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001511 *
1512 * Attempts to read the crypto metadata from the extended attribute
1513 * region of the lower file.
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001514 *
1515 * Returns zero on success; non-zero on error
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001516 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001517int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001518{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001519 struct dentry *lower_dentry =
1520 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001521 ssize_t size;
1522 int rc = 0;
1523
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001524 size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1525 page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001526 if (size < 0) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001527 printk(KERN_ERR "Error attempting to read the [%s] "
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001528 "xattr from the lower file; return value = [%zd]\n",
1529 ECRYPTFS_XATTR_NAME, size);
1530 rc = -EINVAL;
1531 goto out;
1532 }
1533out:
1534 return rc;
1535}
1536
1537int ecryptfs_read_and_validate_xattr_region(char *page_virt,
1538 struct dentry *ecryptfs_dentry)
1539{
1540 int rc;
1541
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001542 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001543 if (rc)
1544 goto out;
1545 if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
1546 printk(KERN_WARNING "Valid data found in [%s] xattr, but "
1547 "the marker is invalid\n", ECRYPTFS_XATTR_NAME);
1548 rc = -EINVAL;
1549 }
1550out:
1551 return rc;
1552}
1553
1554/**
1555 * ecryptfs_read_metadata
1556 *
1557 * Common entry point for reading file metadata. From here, we could
1558 * retrieve the header information from the header region of the file,
1559 * the xattr region of the file, or some other repostory that is
1560 * stored separately from the file itself. The current implementation
1561 * supports retrieving the metadata information from the file contents
1562 * and from the xattr region.
Michael Halcrow237fead2006-10-04 02:16:22 -07001563 *
1564 * Returns zero if valid headers found and parsed; non-zero otherwise
1565 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001566int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -07001567{
1568 int rc = 0;
1569 char *page_virt = NULL;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001570 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -07001571 struct ecryptfs_crypt_stat *crypt_stat =
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001572 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001573 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1574 &ecryptfs_superblock_to_private(
1575 ecryptfs_dentry->d_sb)->mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -07001576
Michael Halcrowe77a56d2007-02-12 00:53:47 -08001577 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1578 mount_crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -07001579 /* Read the first page from the underlying file */
Christoph Lameterf7267c02006-12-06 20:33:15 -08001580 page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -07001581 if (!page_virt) {
1582 rc = -ENOMEM;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001583 printk(KERN_ERR "%s: Unable to allocate page_virt\n",
1584 __FUNCTION__);
Michael Halcrow237fead2006-10-04 02:16:22 -07001585 goto out;
1586 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001587 rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1588 ecryptfs_inode);
1589 if (!rc)
1590 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1591 ecryptfs_dentry,
1592 ECRYPTFS_VALIDATE_HEADER_SIZE);
Michael Halcrow237fead2006-10-04 02:16:22 -07001593 if (rc) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001594 rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001595 if (rc) {
1596 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1597 "file header region or xattr region\n");
1598 rc = -EINVAL;
1599 goto out;
1600 }
1601 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1602 ecryptfs_dentry,
1603 ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
1604 if (rc) {
1605 printk(KERN_DEBUG "Valid eCryptfs headers not found in "
1606 "file xattr region either\n");
1607 rc = -EINVAL;
1608 }
1609 if (crypt_stat->mount_crypt_stat->flags
1610 & ECRYPTFS_XATTR_METADATA_ENABLED) {
1611 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
1612 } else {
1613 printk(KERN_WARNING "Attempt to access file with "
1614 "crypto metadata only in the extended attribute "
1615 "region, but eCryptfs was mounted without "
1616 "xattr support enabled. eCryptfs will not treat "
1617 "this like an encrypted file.\n");
1618 rc = -EINVAL;
1619 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001620 }
1621out:
1622 if (page_virt) {
1623 memset(page_virt, 0, PAGE_CACHE_SIZE);
1624 kmem_cache_free(ecryptfs_header_cache_1, page_virt);
1625 }
1626 return rc;
1627}
1628
1629/**
1630 * ecryptfs_encode_filename - converts a plaintext file name to cipher text
1631 * @crypt_stat: The crypt_stat struct associated with the file anem to encode
1632 * @name: The plaintext name
1633 * @length: The length of the plaintext
1634 * @encoded_name: The encypted name
1635 *
1636 * Encrypts and encodes a filename into something that constitutes a
1637 * valid filename for a filesystem, with printable characters.
1638 *
1639 * We assume that we have a properly initialized crypto context,
1640 * pointed to by crypt_stat->tfm.
1641 *
1642 * TODO: Implement filename decoding and decryption here, in place of
1643 * memcpy. We are keeping the framework around for now to (1)
1644 * facilitate testing of the components needed to implement filename
1645 * encryption and (2) to provide a code base from which other
1646 * developers in the community can easily implement this feature.
1647 *
1648 * Returns the length of encoded filename; negative if error
1649 */
1650int
1651ecryptfs_encode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1652 const char *name, int length, char **encoded_name)
1653{
1654 int error = 0;
1655
1656 (*encoded_name) = kmalloc(length + 2, GFP_KERNEL);
1657 if (!(*encoded_name)) {
1658 error = -ENOMEM;
1659 goto out;
1660 }
1661 /* TODO: Filename encryption is a scheduled feature for a
1662 * future version of eCryptfs. This function is here only for
1663 * the purpose of providing a framework for other developers
1664 * to easily implement filename encryption. Hint: Replace this
1665 * memcpy() with a call to encrypt and encode the
1666 * filename, the set the length accordingly. */
1667 memcpy((void *)(*encoded_name), (void *)name, length);
1668 (*encoded_name)[length] = '\0';
1669 error = length + 1;
1670out:
1671 return error;
1672}
1673
1674/**
1675 * ecryptfs_decode_filename - converts the cipher text name to plaintext
1676 * @crypt_stat: The crypt_stat struct associated with the file
1677 * @name: The filename in cipher text
1678 * @length: The length of the cipher text name
1679 * @decrypted_name: The plaintext name
1680 *
1681 * Decodes and decrypts the filename.
1682 *
1683 * We assume that we have a properly initialized crypto context,
1684 * pointed to by crypt_stat->tfm.
1685 *
1686 * TODO: Implement filename decoding and decryption here, in place of
1687 * memcpy. We are keeping the framework around for now to (1)
1688 * facilitate testing of the components needed to implement filename
1689 * encryption and (2) to provide a code base from which other
1690 * developers in the community can easily implement this feature.
1691 *
1692 * Returns the length of decoded filename; negative if error
1693 */
1694int
1695ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
1696 const char *name, int length, char **decrypted_name)
1697{
1698 int error = 0;
1699
1700 (*decrypted_name) = kmalloc(length + 2, GFP_KERNEL);
1701 if (!(*decrypted_name)) {
1702 error = -ENOMEM;
1703 goto out;
1704 }
1705 /* TODO: Filename encryption is a scheduled feature for a
1706 * future version of eCryptfs. This function is here only for
1707 * the purpose of providing a framework for other developers
1708 * to easily implement filename encryption. Hint: Replace this
1709 * memcpy() with a call to decode and decrypt the
1710 * filename, the set the length accordingly. */
1711 memcpy((void *)(*decrypted_name), (void *)name, length);
1712 (*decrypted_name)[length + 1] = '\0'; /* Only for convenience
1713 * in printing out the
1714 * string in debug
1715 * messages */
1716 error = length;
1717out:
1718 return error;
1719}
1720
1721/**
Michael Halcrowf4aad162007-10-16 01:27:53 -07001722 * ecryptfs_process_key_cipher - Perform key cipher initialization.
Michael Halcrow237fead2006-10-04 02:16:22 -07001723 * @key_tfm: Crypto context for key material, set by this function
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001724 * @cipher_name: Name of the cipher
1725 * @key_size: Size of the key in bytes
Michael Halcrow237fead2006-10-04 02:16:22 -07001726 *
1727 * Returns zero on success. Any crypto_tfm structs allocated here
1728 * should be released by other functions, such as on a superblock put
1729 * event, regardless of whether this function succeeds for fails.
1730 */
Michael Halcrowcd9d67d2007-10-16 01:28:04 -07001731static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001732ecryptfs_process_key_cipher(struct crypto_blkcipher **key_tfm,
1733 char *cipher_name, size_t *key_size)
Michael Halcrow237fead2006-10-04 02:16:22 -07001734{
1735 char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrow8bba0662006-10-30 22:07:18 -08001736 char *full_alg_name;
Michael Halcrow237fead2006-10-04 02:16:22 -07001737 int rc;
1738
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001739 *key_tfm = NULL;
1740 if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001741 rc = -EINVAL;
1742 printk(KERN_ERR "Requested key size is [%Zd] bytes; maximum "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001743 "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
Michael Halcrow237fead2006-10-04 02:16:22 -07001744 goto out;
1745 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001746 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1747 "ecb");
1748 if (rc)
1749 goto out;
1750 *key_tfm = crypto_alloc_blkcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
1751 kfree(full_alg_name);
1752 if (IS_ERR(*key_tfm)) {
1753 rc = PTR_ERR(*key_tfm);
Michael Halcrow237fead2006-10-04 02:16:22 -07001754 printk(KERN_ERR "Unable to allocate crypto cipher with name "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001755 "[%s]; rc = [%d]\n", cipher_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001756 goto out;
1757 }
Michael Halcrow8bba0662006-10-30 22:07:18 -08001758 crypto_blkcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1759 if (*key_size == 0) {
1760 struct blkcipher_alg *alg = crypto_blkcipher_alg(*key_tfm);
1761
1762 *key_size = alg->max_keysize;
1763 }
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001764 get_random_bytes(dummy_key, *key_size);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001765 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001766 if (rc) {
1767 printk(KERN_ERR "Error attempting to set key of size [%Zd] for "
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001768 "cipher [%s]; rc = [%d]\n", *key_size, cipher_name, rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001769 rc = -EINVAL;
1770 goto out;
1771 }
1772out:
1773 return rc;
1774}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001775
1776struct kmem_cache *ecryptfs_key_tfm_cache;
Adrian Bunk7896b632008-02-06 01:38:32 -08001777static struct list_head key_tfm_list;
1778static struct mutex key_tfm_list_mutex;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001779
1780int ecryptfs_init_crypto(void)
1781{
1782 mutex_init(&key_tfm_list_mutex);
1783 INIT_LIST_HEAD(&key_tfm_list);
1784 return 0;
1785}
1786
Michael Halcrowfcd12832007-10-16 01:28:01 -07001787int ecryptfs_destroy_crypto(void)
Michael Halcrowf4aad162007-10-16 01:27:53 -07001788{
1789 struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
1790
1791 mutex_lock(&key_tfm_list_mutex);
1792 list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
1793 key_tfm_list) {
1794 list_del(&key_tfm->key_tfm_list);
1795 if (key_tfm->key_tfm)
1796 crypto_free_blkcipher(key_tfm->key_tfm);
1797 kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
1798 }
1799 mutex_unlock(&key_tfm_list_mutex);
1800 return 0;
1801}
1802
1803int
1804ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
1805 size_t key_size)
1806{
1807 struct ecryptfs_key_tfm *tmp_tfm;
1808 int rc = 0;
1809
1810 tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1811 if (key_tfm != NULL)
1812 (*key_tfm) = tmp_tfm;
1813 if (!tmp_tfm) {
1814 rc = -ENOMEM;
1815 printk(KERN_ERR "Error attempting to allocate from "
1816 "ecryptfs_key_tfm_cache\n");
1817 goto out;
1818 }
1819 mutex_init(&tmp_tfm->key_tfm_mutex);
1820 strncpy(tmp_tfm->cipher_name, cipher_name,
1821 ECRYPTFS_MAX_CIPHER_NAME_SIZE);
Eric Sandeenb8862902007-12-22 14:03:24 -08001822 tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
Michael Halcrowf4aad162007-10-16 01:27:53 -07001823 tmp_tfm->key_size = key_size;
Michael Halcrow5dda6992007-10-16 01:28:06 -07001824 rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
1825 tmp_tfm->cipher_name,
1826 &tmp_tfm->key_size);
1827 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001828 printk(KERN_ERR "Error attempting to initialize key TFM "
1829 "cipher with name = [%s]; rc = [%d]\n",
1830 tmp_tfm->cipher_name, rc);
1831 kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1832 if (key_tfm != NULL)
1833 (*key_tfm) = NULL;
1834 goto out;
1835 }
1836 mutex_lock(&key_tfm_list_mutex);
1837 list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
1838 mutex_unlock(&key_tfm_list_mutex);
1839out:
1840 return rc;
1841}
1842
1843int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_blkcipher **tfm,
1844 struct mutex **tfm_mutex,
1845 char *cipher_name)
1846{
1847 struct ecryptfs_key_tfm *key_tfm;
1848 int rc = 0;
1849
1850 (*tfm) = NULL;
1851 (*tfm_mutex) = NULL;
1852 mutex_lock(&key_tfm_list_mutex);
1853 list_for_each_entry(key_tfm, &key_tfm_list, key_tfm_list) {
1854 if (strcmp(key_tfm->cipher_name, cipher_name) == 0) {
1855 (*tfm) = key_tfm->key_tfm;
1856 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1857 mutex_unlock(&key_tfm_list_mutex);
1858 goto out;
1859 }
1860 }
1861 mutex_unlock(&key_tfm_list_mutex);
Michael Halcrow5dda6992007-10-16 01:28:06 -07001862 rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
1863 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001864 printk(KERN_ERR "Error adding new key_tfm to list; rc = [%d]\n",
1865 rc);
1866 goto out;
1867 }
1868 (*tfm) = key_tfm->key_tfm;
1869 (*tfm_mutex) = &key_tfm->key_tfm_mutex;
1870out:
1871 return rc;
1872}