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