Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* TO DO: |
| 18 | * 1. Perhaps keep several copies of the encrypted key, in case something |
| 19 | * goes horribly wrong? |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <unistd.h> |
| 27 | #include <stdio.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <linux/dm-ioctl.h> |
| 30 | #include <libgen.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <sys/param.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/mount.h> |
| 35 | #include <openssl/evp.h> |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 36 | #include <openssl/sha.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 37 | #include <errno.h> |
Ken Sumrall | c290eaf | 2011-03-07 23:40:35 -0800 | [diff] [blame] | 38 | #include <cutils/android_reboot.h> |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 39 | #include <ext4.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 40 | #include "cryptfs.h" |
| 41 | #define LOG_TAG "Cryptfs" |
| 42 | #include "cutils/log.h" |
| 43 | #include "cutils/properties.h" |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 44 | #include "hardware_legacy/power.h" |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 45 | |
| 46 | #define DM_CRYPT_BUF_SIZE 4096 |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 47 | #define DATA_MNT_POINT "/data" |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 48 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 49 | #define HASH_COUNT 2000 |
| 50 | #define KEY_LEN_BYTES 16 |
| 51 | #define IV_LEN_BYTES 16 |
| 52 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 53 | char *me = "cryptfs"; |
| 54 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 55 | static unsigned char saved_master_key[KEY_LEN_BYTES]; |
| 56 | static int master_key_saved = 0; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 57 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 58 | static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags) |
| 59 | { |
| 60 | memset(io, 0, dataSize); |
| 61 | io->data_size = dataSize; |
| 62 | io->data_start = sizeof(struct dm_ioctl); |
| 63 | io->version[0] = 4; |
| 64 | io->version[1] = 0; |
| 65 | io->version[2] = 0; |
| 66 | io->flags = flags; |
| 67 | if (name) { |
| 68 | strncpy(io->name, name, sizeof(io->name)); |
| 69 | } |
| 70 | } |
| 71 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 72 | static unsigned int get_fs_size(char *dev) |
| 73 | { |
| 74 | int fd, block_size; |
| 75 | struct ext4_super_block sb; |
| 76 | off64_t len; |
| 77 | |
| 78 | if ((fd = open(dev, O_RDONLY)) < 0) { |
| 79 | SLOGE("Cannot open device to get filesystem size "); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | if (lseek64(fd, 1024, SEEK_SET) < 0) { |
| 84 | SLOGE("Cannot seek to superblock"); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) { |
| 89 | SLOGE("Cannot read superblock"); |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | close(fd); |
| 94 | |
| 95 | block_size = 1024 << sb.s_log_block_size; |
| 96 | /* compute length in bytes */ |
| 97 | len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size; |
| 98 | |
| 99 | /* return length in sectors */ |
| 100 | return (unsigned int) (len / 512); |
| 101 | } |
| 102 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 103 | static unsigned int get_blkdev_size(int fd) |
| 104 | { |
| 105 | unsigned int nr_sec; |
| 106 | |
| 107 | if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) { |
| 108 | nr_sec = 0; |
| 109 | } |
| 110 | |
| 111 | return nr_sec; |
| 112 | } |
| 113 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 114 | /* key or salt can be NULL, in which case just skip writing that value. Useful to |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 115 | * update the failed mount count but not change the key. |
| 116 | */ |
| 117 | static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr, |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 118 | unsigned char *key, unsigned char *salt) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 119 | { |
| 120 | int fd; |
| 121 | unsigned int nr_sec, cnt; |
| 122 | off64_t off; |
| 123 | int rc = -1; |
| 124 | |
| 125 | if ( (fd = open(real_blk_name, O_RDWR)) < 0) { |
| 126 | SLOGE("Cannot open real block device %s\n", real_blk_name); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | if ( (nr_sec = get_blkdev_size(fd)) == 0) { |
| 131 | SLOGE("Cannot get size of block device %s\n", real_blk_name); |
| 132 | goto errout; |
| 133 | } |
| 134 | |
| 135 | /* If it's an encrypted Android partition, the last 16 Kbytes contain the |
| 136 | * encryption info footer and key, and plenty of bytes to spare for future |
| 137 | * growth. |
| 138 | */ |
| 139 | off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
| 140 | |
| 141 | if (lseek64(fd, off, SEEK_SET) == -1) { |
| 142 | SLOGE("Cannot seek to real block device footer\n"); |
| 143 | goto errout; |
| 144 | } |
| 145 | |
| 146 | if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 147 | SLOGE("Cannot write real block device footer\n"); |
| 148 | goto errout; |
| 149 | } |
| 150 | |
| 151 | if (key) { |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 152 | if (crypt_ftr->keysize != KEY_LEN_BYTES) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 153 | SLOGE("Keysize of %d bits not supported for real block device %s\n", |
| 154 | crypt_ftr->keysize * 8, real_blk_name); |
| 155 | goto errout; |
| 156 | } |
| 157 | |
| 158 | if ( (cnt = write(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) { |
| 159 | SLOGE("Cannot write key for real block device %s\n", real_blk_name); |
| 160 | goto errout; |
| 161 | } |
| 162 | } |
| 163 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 164 | if (salt) { |
| 165 | /* Compute the offset for start of the crypt footer */ |
| 166 | off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
| 167 | /* Add in the length of the footer, key and padding */ |
| 168 | off += sizeof(struct crypt_mnt_ftr) + crypt_ftr->keysize + KEY_TO_SALT_PADDING; |
| 169 | |
| 170 | if (lseek64(fd, off, SEEK_SET) == -1) { |
| 171 | SLOGE("Cannot seek to real block device salt \n"); |
| 172 | goto errout; |
| 173 | } |
| 174 | |
| 175 | if ( (cnt = write(fd, salt, SALT_LEN)) != SALT_LEN) { |
| 176 | SLOGE("Cannot write salt for real block device %s\n", real_blk_name); |
| 177 | goto errout; |
| 178 | } |
| 179 | } |
| 180 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 181 | /* Success! */ |
| 182 | rc = 0; |
| 183 | |
| 184 | errout: |
| 185 | close(fd); |
| 186 | return rc; |
| 187 | |
| 188 | } |
| 189 | |
| 190 | static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr, |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 191 | unsigned char *key, unsigned char *salt) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 192 | { |
| 193 | int fd; |
| 194 | unsigned int nr_sec, cnt; |
| 195 | off64_t off; |
| 196 | int rc = -1; |
| 197 | |
| 198 | if ( (fd = open(real_blk_name, O_RDWR)) < 0) { |
| 199 | SLOGE("Cannot open real block device %s\n", real_blk_name); |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | if ( (nr_sec = get_blkdev_size(fd)) == 0) { |
| 204 | SLOGE("Cannot get size of block device %s\n", real_blk_name); |
| 205 | goto errout; |
| 206 | } |
| 207 | |
| 208 | /* If it's an encrypted Android partition, the last 16 Kbytes contain the |
| 209 | * encryption info footer and key, and plenty of bytes to spare for future |
| 210 | * growth. |
| 211 | */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 212 | off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 213 | |
| 214 | if (lseek64(fd, off, SEEK_SET) == -1) { |
| 215 | SLOGE("Cannot seek to real block device footer\n"); |
| 216 | goto errout; |
| 217 | } |
| 218 | |
| 219 | if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 220 | SLOGE("Cannot read real block device footer\n"); |
| 221 | goto errout; |
| 222 | } |
| 223 | |
| 224 | if (crypt_ftr->magic != CRYPT_MNT_MAGIC) { |
| 225 | SLOGE("Bad magic for real block device %s\n", real_blk_name); |
| 226 | goto errout; |
| 227 | } |
| 228 | |
| 229 | if (crypt_ftr->major_version != 1) { |
| 230 | SLOGE("Cannot understand major version %d real block device footer\n", |
| 231 | crypt_ftr->major_version); |
| 232 | goto errout; |
| 233 | } |
| 234 | |
| 235 | if (crypt_ftr->minor_version != 0) { |
| 236 | SLOGW("Warning: crypto footer minor version %d, expected 0, continuing...\n", |
| 237 | crypt_ftr->minor_version); |
| 238 | } |
| 239 | |
| 240 | if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) { |
| 241 | /* the footer size is bigger than we expected. |
| 242 | * Skip to it's stated end so we can read the key. |
| 243 | */ |
| 244 | if (lseek(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) { |
| 245 | SLOGE("Cannot seek to start of key\n"); |
| 246 | goto errout; |
| 247 | } |
| 248 | } |
| 249 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 250 | if (crypt_ftr->keysize != KEY_LEN_BYTES) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 251 | SLOGE("Keysize of %d bits not supported for real block device %s\n", |
| 252 | crypt_ftr->keysize * 8, real_blk_name); |
| 253 | goto errout; |
| 254 | } |
| 255 | |
| 256 | if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) { |
| 257 | SLOGE("Cannot read key for real block device %s\n", real_blk_name); |
| 258 | goto errout; |
| 259 | } |
| 260 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 261 | if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) { |
| 262 | SLOGE("Cannot seek to real block device salt\n"); |
| 263 | goto errout; |
| 264 | } |
| 265 | |
| 266 | if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) { |
| 267 | SLOGE("Cannot read salt for real block device %s\n", real_blk_name); |
| 268 | goto errout; |
| 269 | } |
| 270 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 271 | /* Success! */ |
| 272 | rc = 0; |
| 273 | |
| 274 | errout: |
| 275 | close(fd); |
| 276 | return rc; |
| 277 | } |
| 278 | |
| 279 | /* Convert a binary key of specified length into an ascii hex string equivalent, |
| 280 | * without the leading 0x and with null termination |
| 281 | */ |
| 282 | void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize, |
| 283 | char *master_key_ascii) |
| 284 | { |
| 285 | unsigned int i, a; |
| 286 | unsigned char nibble; |
| 287 | |
| 288 | for (i=0, a=0; i<keysize; i++, a+=2) { |
| 289 | /* For each byte, write out two ascii hex digits */ |
| 290 | nibble = (master_key[i] >> 4) & 0xf; |
| 291 | master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 292 | |
| 293 | nibble = master_key[i] & 0xf; |
| 294 | master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 295 | } |
| 296 | |
| 297 | /* Add the null termination */ |
| 298 | master_key_ascii[a] = '\0'; |
| 299 | |
| 300 | } |
| 301 | |
| 302 | static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key, |
| 303 | char *real_blk_name, char *crypto_blk_name) |
| 304 | { |
| 305 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 306 | char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */ |
| 307 | char *crypt_params; |
| 308 | struct dm_ioctl *io; |
| 309 | struct dm_target_spec *tgt; |
| 310 | unsigned int minor; |
| 311 | int fd; |
| 312 | int retval = -1; |
| 313 | char *name ="datadev"; /* FIX ME: Make me a parameter */ |
| 314 | |
| 315 | if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) { |
| 316 | SLOGE("Cannot open device-mapper\n"); |
| 317 | goto errout; |
| 318 | } |
| 319 | |
| 320 | io = (struct dm_ioctl *) buffer; |
| 321 | |
| 322 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 323 | if (ioctl(fd, DM_DEV_CREATE, io)) { |
| 324 | SLOGE("Cannot create dm-crypt device\n"); |
| 325 | goto errout; |
| 326 | } |
| 327 | |
| 328 | /* Get the device status, in particular, the name of it's device file */ |
| 329 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 330 | if (ioctl(fd, DM_DEV_STATUS, io)) { |
| 331 | SLOGE("Cannot retrieve dm-crypt device status\n"); |
| 332 | goto errout; |
| 333 | } |
| 334 | minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); |
| 335 | snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor); |
| 336 | |
| 337 | /* Load the mapping table for this device */ |
| 338 | tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)]; |
| 339 | |
| 340 | ioctl_init(io, 4096, name, 0); |
| 341 | io->target_count = 1; |
| 342 | tgt->status = 0; |
| 343 | tgt->sector_start = 0; |
| 344 | tgt->length = crypt_ftr->fs_size; |
| 345 | strcpy(tgt->target_type, "crypt"); |
| 346 | |
| 347 | crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec); |
| 348 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 349 | sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name, |
| 350 | master_key_ascii, real_blk_name); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 351 | crypt_params += strlen(crypt_params) + 1; |
| 352 | crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */ |
| 353 | tgt->next = crypt_params - buffer; |
| 354 | |
| 355 | if (ioctl(fd, DM_TABLE_LOAD, io)) { |
| 356 | SLOGE("Cannot load dm-crypt mapping table.\n"); |
| 357 | goto errout; |
| 358 | } |
| 359 | |
| 360 | /* Resume this device to activate it */ |
| 361 | ioctl_init(io, 4096, name, 0); |
| 362 | |
| 363 | if (ioctl(fd, DM_DEV_SUSPEND, io)) { |
| 364 | SLOGE("Cannot resume the dm-crypt device\n"); |
| 365 | goto errout; |
| 366 | } |
| 367 | |
| 368 | /* We made it here with no errors. Woot! */ |
| 369 | retval = 0; |
| 370 | |
| 371 | errout: |
| 372 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 373 | |
| 374 | return retval; |
| 375 | } |
| 376 | |
| 377 | static int delete_crypto_blk_dev(char *crypto_blkdev) |
| 378 | { |
| 379 | int fd; |
| 380 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 381 | struct dm_ioctl *io; |
| 382 | char *name ="datadev"; /* FIX ME: Make me a paraameter */ |
| 383 | int retval = -1; |
| 384 | |
| 385 | if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) { |
| 386 | SLOGE("Cannot open device-mapper\n"); |
| 387 | goto errout; |
| 388 | } |
| 389 | |
| 390 | io = (struct dm_ioctl *) buffer; |
| 391 | |
| 392 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 393 | if (ioctl(fd, DM_DEV_REMOVE, io)) { |
| 394 | SLOGE("Cannot remove dm-crypt device\n"); |
| 395 | goto errout; |
| 396 | } |
| 397 | |
| 398 | /* We made it here with no errors. Woot! */ |
| 399 | retval = 0; |
| 400 | |
| 401 | errout: |
| 402 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 403 | |
| 404 | return retval; |
| 405 | |
| 406 | } |
| 407 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 408 | static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 409 | { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 410 | /* Turn the password into a key and IV that can decrypt the master key */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 411 | PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN, |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 412 | HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 415 | static int encrypt_master_key(char *passwd, unsigned char *salt, |
| 416 | unsigned char *decrypted_master_key, |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 417 | unsigned char *encrypted_master_key) |
| 418 | { |
| 419 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
| 420 | EVP_CIPHER_CTX e_ctx; |
| 421 | int encrypted_len, final_len; |
| 422 | |
| 423 | /* Turn the password into a key and IV that can decrypt the master key */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 424 | pbkdf2(passwd, salt, ikey); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 425 | |
| 426 | /* Initialize the decryption engine */ |
| 427 | if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) { |
| 428 | SLOGE("EVP_EncryptInit failed\n"); |
| 429 | return -1; |
| 430 | } |
| 431 | EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 432 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 433 | /* Encrypt the master key */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 434 | if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len, |
| 435 | decrypted_master_key, KEY_LEN_BYTES)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 436 | SLOGE("EVP_EncryptUpdate failed\n"); |
| 437 | return -1; |
| 438 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 439 | if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 440 | SLOGE("EVP_EncryptFinal failed\n"); |
| 441 | return -1; |
| 442 | } |
| 443 | |
| 444 | if (encrypted_len + final_len != KEY_LEN_BYTES) { |
| 445 | SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len); |
| 446 | return -1; |
| 447 | } else { |
| 448 | return 0; |
| 449 | } |
| 450 | } |
| 451 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 452 | static int decrypt_master_key(char *passwd, unsigned char *salt, |
| 453 | unsigned char *encrypted_master_key, |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 454 | unsigned char *decrypted_master_key) |
| 455 | { |
| 456 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 457 | EVP_CIPHER_CTX d_ctx; |
| 458 | int decrypted_len, final_len; |
| 459 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 460 | /* Turn the password into a key and IV that can decrypt the master key */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 461 | pbkdf2(passwd, salt, ikey); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 462 | |
| 463 | /* Initialize the decryption engine */ |
| 464 | if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) { |
| 465 | return -1; |
| 466 | } |
| 467 | EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */ |
| 468 | /* Decrypt the master key */ |
| 469 | if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len, |
| 470 | encrypted_master_key, KEY_LEN_BYTES)) { |
| 471 | return -1; |
| 472 | } |
| 473 | if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) { |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | if (decrypted_len + final_len != KEY_LEN_BYTES) { |
| 478 | return -1; |
| 479 | } else { |
| 480 | return 0; |
| 481 | } |
| 482 | } |
| 483 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 484 | static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 485 | { |
| 486 | int fd; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 487 | unsigned char key_buf[KEY_LEN_BYTES]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 488 | EVP_CIPHER_CTX e_ctx; |
| 489 | int encrypted_len, final_len; |
| 490 | |
| 491 | /* Get some random bits for a key */ |
| 492 | fd = open("/dev/urandom", O_RDONLY); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 493 | read(fd, key_buf, sizeof(key_buf)); |
| 494 | read(fd, salt, SALT_LEN); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 495 | close(fd); |
| 496 | |
| 497 | /* Now encrypt it with the password */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 498 | return encrypt_master_key(passwd, salt, key_buf, master_key); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 501 | static int get_orig_mount_parms(char *mount_point, char *fs_type, char *real_blkdev, |
| 502 | unsigned long *mnt_flags, char *fs_options) |
| 503 | { |
| 504 | char mount_point2[32]; |
| 505 | char fs_flags[32]; |
| 506 | |
| 507 | property_get("ro.crypto.fs_type", fs_type, ""); |
| 508 | property_get("ro.crypto.fs_real_blkdev", real_blkdev, ""); |
| 509 | property_get("ro.crypto.fs_mnt_point", mount_point2, ""); |
| 510 | property_get("ro.crypto.fs_options", fs_options, ""); |
| 511 | property_get("ro.crypto.fs_flags", fs_flags, ""); |
| 512 | *mnt_flags = strtol(fs_flags, 0, 0); |
| 513 | |
| 514 | if (strcmp(mount_point, mount_point2)) { |
| 515 | /* Consistency check. These should match. If not, something odd happened. */ |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static int wait_and_unmount(char *mountpoint) |
| 523 | { |
| 524 | int i, rc; |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 525 | #define WAIT_UNMOUNT_COUNT 20 |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 526 | |
| 527 | /* Now umount the tmpfs filesystem */ |
| 528 | for (i=0; i<WAIT_UNMOUNT_COUNT; i++) { |
| 529 | if (umount(mountpoint)) { |
| 530 | sleep(1); |
| 531 | i++; |
| 532 | } else { |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | if (i < WAIT_UNMOUNT_COUNT) { |
| 538 | SLOGD("unmounting %s succeeded\n", mountpoint); |
| 539 | rc = 0; |
| 540 | } else { |
| 541 | SLOGE("unmounting %s failed\n", mountpoint); |
| 542 | rc = -1; |
| 543 | } |
| 544 | |
| 545 | return rc; |
| 546 | } |
| 547 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 548 | #define DATA_PREP_TIMEOUT 100 |
| 549 | static int prep_data_fs(void) |
| 550 | { |
| 551 | int i; |
| 552 | |
| 553 | /* Do the prep of the /data filesystem */ |
| 554 | property_set("vold.post_fs_data_done", "0"); |
| 555 | property_set("vold.decrypt", "trigger_post_fs_data"); |
| 556 | SLOGD("Just triggered post_fs_data\n"); |
| 557 | |
| 558 | /* Wait a max of 25 seconds, hopefully it takes much less */ |
| 559 | for (i=0; i<DATA_PREP_TIMEOUT; i++) { |
| 560 | char p[16];; |
| 561 | |
| 562 | property_get("vold.post_fs_data_done", p, "0"); |
| 563 | if (*p == '1') { |
| 564 | break; |
| 565 | } else { |
| 566 | usleep(250000); |
| 567 | } |
| 568 | } |
| 569 | if (i == DATA_PREP_TIMEOUT) { |
| 570 | /* Ugh, we failed to prep /data in time. Bail. */ |
| 571 | return -1; |
| 572 | } else { |
| 573 | SLOGD("post_fs_data done\n"); |
| 574 | return 0; |
| 575 | } |
| 576 | } |
| 577 | |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 578 | int cryptfs_restart(void) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 579 | { |
| 580 | char fs_type[32]; |
| 581 | char real_blkdev[MAXPATHLEN]; |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 582 | char crypto_blkdev[MAXPATHLEN]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 583 | char fs_options[256]; |
| 584 | unsigned long mnt_flags; |
| 585 | struct stat statbuf; |
| 586 | int rc = -1, i; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 587 | static int restart_successful = 0; |
| 588 | |
| 589 | /* Validate that it's OK to call this routine */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 590 | if (! master_key_saved) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 591 | SLOGE("Encrypted filesystem not validated, aborting"); |
| 592 | return -1; |
| 593 | } |
| 594 | |
| 595 | if (restart_successful) { |
| 596 | SLOGE("System already restarted with encrypted disk, aborting"); |
| 597 | return -1; |
| 598 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 599 | |
| 600 | /* Here is where we shut down the framework. The init scripts |
| 601 | * start all services in one of three classes: core, main or late_start. |
| 602 | * On boot, we start core and main. Now, we stop main, but not core, |
| 603 | * as core includes vold and a few other really important things that |
| 604 | * we need to keep running. Once main has stopped, we should be able |
| 605 | * to umount the tmpfs /data, then mount the encrypted /data. |
| 606 | * We then restart the class main, and also the class late_start. |
| 607 | * At the moment, I've only put a few things in late_start that I know |
| 608 | * are not needed to bring up the framework, and that also cause problems |
| 609 | * with unmounting the tmpfs /data, but I hope to add add more services |
| 610 | * to the late_start class as we optimize this to decrease the delay |
| 611 | * till the user is asked for the password to the filesystem. |
| 612 | */ |
| 613 | |
| 614 | /* The init files are setup to stop the class main when vold.decrypt is |
| 615 | * set to trigger_reset_main. |
| 616 | */ |
| 617 | property_set("vold.decrypt", "trigger_reset_main"); |
| 618 | SLOGD("Just asked init to shut down class main\n"); |
| 619 | |
| 620 | /* Now that the framework is shutdown, we should be able to umount() |
| 621 | * the tmpfs filesystem, and mount the real one. |
| 622 | */ |
| 623 | |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 624 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, ""); |
| 625 | if (strlen(crypto_blkdev) == 0) { |
| 626 | SLOGE("fs_crypto_blkdev not set\n"); |
| 627 | return -1; |
| 628 | } |
| 629 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 630 | if (! get_orig_mount_parms(DATA_MNT_POINT, fs_type, real_blkdev, &mnt_flags, fs_options)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 631 | SLOGD("Just got orig mount parms\n"); |
| 632 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 633 | if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 634 | /* If that succeeded, then mount the decrypted filesystem */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 635 | mount(crypto_blkdev, DATA_MNT_POINT, fs_type, mnt_flags, fs_options); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 636 | |
Ken Sumrall | ad2ac33 | 2011-03-08 17:07:06 -0800 | [diff] [blame] | 637 | property_set("vold.decrypt", "trigger_load_persist_props"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 638 | /* Create necessary paths on /data */ |
| 639 | if (prep_data_fs()) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 640 | return -1; |
| 641 | } |
| 642 | |
| 643 | /* startup service classes main and late_start */ |
| 644 | property_set("vold.decrypt", "trigger_restart_framework"); |
| 645 | SLOGD("Just triggered restart_framework\n"); |
| 646 | |
| 647 | /* Give it a few moments to get started */ |
| 648 | sleep(1); |
| 649 | } |
| 650 | } |
| 651 | |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 652 | if (rc == 0) { |
| 653 | restart_successful = 1; |
| 654 | } |
| 655 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 656 | return rc; |
| 657 | } |
| 658 | |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 659 | static int do_crypto_complete(char *mount_point) |
| 660 | { |
| 661 | struct crypt_mnt_ftr crypt_ftr; |
| 662 | unsigned char encrypted_master_key[32]; |
| 663 | unsigned char salt[SALT_LEN]; |
| 664 | char real_blkdev[MAXPATHLEN]; |
| 665 | char fs_type[32]; |
| 666 | char fs_options[256]; |
| 667 | unsigned long mnt_flags; |
| 668 | char encrypted_state[32]; |
| 669 | |
| 670 | property_get("ro.crypto.state", encrypted_state, ""); |
| 671 | if (strcmp(encrypted_state, "encrypted") ) { |
| 672 | SLOGE("not running with encryption, aborting"); |
| 673 | return 1; |
| 674 | } |
| 675 | |
| 676 | if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) { |
| 677 | SLOGE("Error reading original mount parms for mount point %s\n", mount_point); |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) { |
| 682 | SLOGE("Error getting crypt footer and key\n"); |
| 683 | return -1; |
| 684 | } |
| 685 | |
| 686 | if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) { |
| 687 | SLOGE("Encryption process didn't finish successfully\n"); |
| 688 | return -2; /* -2 is the clue to the UI that there is no usable data on the disk, |
| 689 | * and give the user an option to wipe the disk */ |
| 690 | } |
| 691 | |
| 692 | /* We passed the test! We shall diminish, and return to the west */ |
| 693 | return 0; |
| 694 | } |
| 695 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 696 | static int test_mount_encrypted_fs(char *passwd, char *mount_point) |
| 697 | { |
| 698 | struct crypt_mnt_ftr crypt_ftr; |
| 699 | /* Allocate enough space for a 256 bit key, but we may use less */ |
| 700 | unsigned char encrypted_master_key[32], decrypted_master_key[32]; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 701 | unsigned char salt[SALT_LEN]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 702 | char crypto_blkdev[MAXPATHLEN]; |
| 703 | char real_blkdev[MAXPATHLEN]; |
| 704 | char fs_type[32]; |
| 705 | char fs_options[256]; |
| 706 | char tmp_mount_point[64]; |
| 707 | unsigned long mnt_flags; |
| 708 | unsigned int orig_failed_decrypt_count; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 709 | char encrypted_state[32]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 710 | int rc; |
| 711 | |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 712 | property_get("ro.crypto.state", encrypted_state, ""); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 713 | if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 714 | SLOGE("encrypted fs already validated or not running with encryption, aborting"); |
| 715 | return -1; |
| 716 | } |
| 717 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 718 | if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) { |
| 719 | SLOGE("Error reading original mount parms for mount point %s\n", mount_point); |
| 720 | return -1; |
| 721 | } |
| 722 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 723 | if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 724 | SLOGE("Error getting crypt footer and key\n"); |
| 725 | return -1; |
| 726 | } |
Ken Sumrall | d33d417 | 2011-02-01 00:49:13 -0800 | [diff] [blame] | 727 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 728 | SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size); |
| 729 | orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count; |
| 730 | |
| 731 | if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) { |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 732 | decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, |
| 736 | real_blkdev, crypto_blkdev)) { |
| 737 | SLOGE("Error creating decrypted block device\n"); |
| 738 | return -1; |
| 739 | } |
| 740 | |
| 741 | /* If init detects an encrypted filesystme, it writes a file for each such |
| 742 | * encrypted fs into the tmpfs /data filesystem, and then the framework finds those |
| 743 | * files and passes that data to me */ |
| 744 | /* Create a tmp mount point to try mounting the decryptd fs |
| 745 | * Since we're here, the mount_point should be a tmpfs filesystem, so make |
| 746 | * a directory in it to test mount the decrypted filesystem. |
| 747 | */ |
| 748 | sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point); |
| 749 | mkdir(tmp_mount_point, 0755); |
| 750 | if ( mount(crypto_blkdev, tmp_mount_point, "ext4", MS_RDONLY, "") ) { |
| 751 | SLOGE("Error temp mounting decrypted block device\n"); |
| 752 | delete_crypto_blk_dev(crypto_blkdev); |
| 753 | crypt_ftr.failed_decrypt_count++; |
| 754 | } else { |
| 755 | /* Success, so just umount and we'll mount it properly when we restart |
| 756 | * the framework. |
| 757 | */ |
| 758 | umount(tmp_mount_point); |
| 759 | crypt_ftr.failed_decrypt_count = 0; |
| 760 | } |
| 761 | |
| 762 | if (orig_failed_decrypt_count != crypt_ftr.failed_decrypt_count) { |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 763 | put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | if (crypt_ftr.failed_decrypt_count) { |
| 767 | /* We failed to mount the device, so return an error */ |
| 768 | rc = crypt_ftr.failed_decrypt_count; |
| 769 | |
| 770 | } else { |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 771 | /* Woot! Success! Save the name of the crypto block device |
| 772 | * so we can mount it when restarting the framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 773 | */ |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 774 | property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 775 | |
| 776 | /* Also save a the master key so we can reencrypted the key |
| 777 | * the key when we want to change the password on it. |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 778 | */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 779 | memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES); |
| 780 | master_key_saved = 1; |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 781 | rc = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | return rc; |
| 785 | } |
| 786 | |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 787 | int cryptfs_crypto_complete(void) |
| 788 | { |
| 789 | return do_crypto_complete("/data"); |
| 790 | } |
| 791 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 792 | int cryptfs_check_passwd(char *passwd) |
| 793 | { |
| 794 | int rc = -1; |
| 795 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 796 | rc = test_mount_encrypted_fs(passwd, DATA_MNT_POINT); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 797 | |
| 798 | return rc; |
| 799 | } |
| 800 | |
| 801 | /* Initialize a crypt_mnt_ftr structure. The keysize is |
| 802 | * defaulted to 16 bytes, and the filesystem size to 0. |
| 803 | * Presumably, at a minimum, the caller will update the |
| 804 | * filesystem size and crypto_type_name after calling this function. |
| 805 | */ |
| 806 | static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr) |
| 807 | { |
| 808 | ftr->magic = CRYPT_MNT_MAGIC; |
| 809 | ftr->major_version = 1; |
| 810 | ftr->minor_version = 0; |
| 811 | ftr->ftr_size = sizeof(struct crypt_mnt_ftr); |
| 812 | ftr->flags = 0; |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 813 | ftr->keysize = KEY_LEN_BYTES; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 814 | ftr->spare1 = 0; |
| 815 | ftr->fs_size = 0; |
| 816 | ftr->failed_decrypt_count = 0; |
| 817 | ftr->crypto_type_name[0] = '\0'; |
| 818 | } |
| 819 | |
| 820 | static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size) |
| 821 | { |
| 822 | char cmdline[256]; |
| 823 | int rc = -1; |
| 824 | |
| 825 | snprintf(cmdline, sizeof(cmdline), "/system/bin/make_ext4fs -a /data -l %lld %s", |
| 826 | size * 512, crypto_blkdev); |
| 827 | SLOGI("Making empty filesystem with command %s\n", cmdline); |
| 828 | if (system(cmdline)) { |
| 829 | SLOGE("Error creating empty filesystem on %s\n", crypto_blkdev); |
| 830 | } else { |
| 831 | SLOGD("Successfully created empty filesystem on %s\n", crypto_blkdev); |
| 832 | rc = 0; |
| 833 | } |
| 834 | |
| 835 | return rc; |
| 836 | } |
| 837 | |
| 838 | static inline int unix_read(int fd, void* buff, int len) |
| 839 | { |
| 840 | int ret; |
| 841 | do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR); |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | static inline int unix_write(int fd, const void* buff, int len) |
| 846 | { |
| 847 | int ret; |
| 848 | do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR); |
| 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | #define CRYPT_INPLACE_BUFSIZE 4096 |
| 853 | #define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512) |
| 854 | static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, off64_t size) |
| 855 | { |
| 856 | int realfd, cryptofd; |
| 857 | char *buf[CRYPT_INPLACE_BUFSIZE]; |
| 858 | int rc = -1; |
| 859 | off64_t numblocks, i, remainder; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 860 | off64_t one_pct, cur_pct, new_pct; |
| 861 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 862 | if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) { |
| 863 | SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev); |
| 864 | return -1; |
| 865 | } |
| 866 | |
| 867 | if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) { |
| 868 | SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev); |
| 869 | close(realfd); |
| 870 | return -1; |
| 871 | } |
| 872 | |
| 873 | /* This is pretty much a simple loop of reading 4K, and writing 4K. |
| 874 | * The size passed in is the number of 512 byte sectors in the filesystem. |
| 875 | * So compute the number of whole 4K blocks we should read/write, |
| 876 | * and the remainder. |
| 877 | */ |
| 878 | numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 879 | remainder = size % CRYPT_SECTORS_PER_BUFSIZE; |
| 880 | |
| 881 | SLOGE("Encrypting filesystem in place..."); |
| 882 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 883 | one_pct = numblocks / 100; |
| 884 | cur_pct = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 885 | /* process the majority of the filesystem in blocks */ |
| 886 | for (i=0; i<numblocks; i++) { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 887 | new_pct = i / one_pct; |
| 888 | if (new_pct > cur_pct) { |
| 889 | char buf[8]; |
| 890 | |
| 891 | cur_pct = new_pct; |
| 892 | snprintf(buf, sizeof(buf), "%lld", cur_pct); |
| 893 | property_set("vold.encrypt_progress", buf); |
| 894 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 895 | if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) { |
| 896 | SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev); |
| 897 | goto errout; |
| 898 | } |
| 899 | if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) { |
| 900 | SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev); |
| 901 | goto errout; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | /* Do any remaining sectors */ |
| 906 | for (i=0; i<remainder; i++) { |
| 907 | if (unix_read(realfd, buf, 512) <= 0) { |
| 908 | SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev); |
| 909 | goto errout; |
| 910 | } |
| 911 | if (unix_write(cryptofd, buf, 512) <= 0) { |
| 912 | SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev); |
| 913 | goto errout; |
| 914 | } |
| 915 | } |
| 916 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 917 | property_set("vold.encrypt_progress", "100"); |
| 918 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 919 | rc = 0; |
| 920 | |
| 921 | errout: |
| 922 | close(realfd); |
| 923 | close(cryptofd); |
| 924 | |
| 925 | return rc; |
| 926 | } |
| 927 | |
| 928 | #define CRYPTO_ENABLE_WIPE 1 |
| 929 | #define CRYPTO_ENABLE_INPLACE 2 |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 930 | |
| 931 | #define FRAMEWORK_BOOT_WAIT 60 |
| 932 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 933 | int cryptfs_enable(char *howarg, char *passwd) |
| 934 | { |
| 935 | int how = 0; |
| 936 | char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN]; |
| 937 | char fs_type[32], fs_options[256], mount_point[32]; |
| 938 | unsigned long mnt_flags, nr_sec; |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 939 | unsigned char master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES]; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 940 | unsigned char salt[SALT_LEN]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 941 | int rc=-1, fd, i; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 942 | struct crypt_mnt_ftr crypt_ftr; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 943 | char tmpfs_options[80]; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 944 | char encrypted_state[32]; |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 945 | char lockid[32] = { 0 }; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 946 | |
| 947 | property_get("ro.crypto.state", encrypted_state, ""); |
| 948 | if (strcmp(encrypted_state, "unencrypted")) { |
| 949 | SLOGE("Device is already running encrypted, aborting"); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 950 | goto error_unencrypted; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 951 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 952 | |
| 953 | if (!strcmp(howarg, "wipe")) { |
| 954 | how = CRYPTO_ENABLE_WIPE; |
| 955 | } else if (! strcmp(howarg, "inplace")) { |
| 956 | how = CRYPTO_ENABLE_INPLACE; |
| 957 | } else { |
| 958 | /* Shouldn't happen, as CommandListener vets the args */ |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 959 | goto error_unencrypted; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options); |
| 963 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 964 | /* Get the size of the real block device */ |
| 965 | fd = open(real_blkdev, O_RDONLY); |
| 966 | if ( (nr_sec = get_blkdev_size(fd)) == 0) { |
| 967 | SLOGE("Cannot get size of block device %s\n", real_blkdev); |
| 968 | goto error_unencrypted; |
| 969 | } |
| 970 | close(fd); |
| 971 | |
| 972 | /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */ |
| 973 | if (how == CRYPTO_ENABLE_INPLACE) { |
| 974 | unsigned int fs_size_sec, max_fs_size_sec; |
| 975 | |
| 976 | fs_size_sec = get_fs_size(real_blkdev); |
| 977 | max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / 512); |
| 978 | |
| 979 | if (fs_size_sec > max_fs_size_sec) { |
| 980 | SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place."); |
| 981 | goto error_unencrypted; |
| 982 | } |
| 983 | } |
| 984 | |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 985 | /* Get a wakelock as this may take a while, and we don't want the |
| 986 | * device to sleep on us. We'll grab a partial wakelock, and if the UI |
| 987 | * wants to keep the screen on, it can grab a full wakelock. |
| 988 | */ |
| 989 | snprintf(lockid, 32, "enablecrypto%d", (int) getpid()); |
| 990 | acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid); |
| 991 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 992 | /* The init files are setup to stop the class main and late start when |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 993 | * vold sets trigger_shutdown_framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 994 | */ |
| 995 | property_set("vold.decrypt", "trigger_shutdown_framework"); |
| 996 | SLOGD("Just asked init to shut down class main\n"); |
| 997 | |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 998 | if (wait_and_unmount("/mnt/sdcard")) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 999 | goto error_shutting_down; |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 1000 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1001 | |
| 1002 | /* Now unmount the /data partition. */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1003 | if (wait_and_unmount(DATA_MNT_POINT)) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1004 | goto error_shutting_down; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /* Do extra work for a better UX when doing the long inplace encryption */ |
| 1008 | if (how == CRYPTO_ENABLE_INPLACE) { |
| 1009 | /* Now that /data is unmounted, we need to mount a tmpfs |
| 1010 | * /data, set a property saying we're doing inplace encryption, |
| 1011 | * and restart the framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1012 | */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1013 | property_get("ro.crypto.tmpfs_options", tmpfs_options, ""); |
| 1014 | if (mount("tmpfs", DATA_MNT_POINT, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV, |
| 1015 | tmpfs_options) < 0) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1016 | goto error_shutting_down; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1017 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1018 | /* Tells the framework that inplace encryption is starting */ |
Ken Sumrall | 7df8412 | 2011-01-18 14:04:08 -0800 | [diff] [blame] | 1019 | property_set("vold.encrypt_progress", "0"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1020 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1021 | /* restart the framework. */ |
| 1022 | /* Create necessary paths on /data */ |
| 1023 | if (prep_data_fs()) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1024 | goto error_shutting_down; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1027 | /* startup service classes main and late_start */ |
| 1028 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
| 1029 | SLOGD("Just triggered restart_min_framework\n"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1030 | |
Ken Sumrall | 7df8412 | 2011-01-18 14:04:08 -0800 | [diff] [blame] | 1031 | /* OK, the framework is restarted and will soon be showing a |
| 1032 | * progress bar. Time to setup an encrypted mapping, and |
| 1033 | * either write a new filesystem, or encrypt in place updating |
| 1034 | * the progress bar as we work. |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1035 | */ |
| 1036 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1037 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1038 | /* Start the actual work of making an encrypted filesystem */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1039 | /* Initialize a crypt_mnt_ftr for the partition */ |
| 1040 | cryptfs_init_crypt_mnt_ftr(&crypt_ftr); |
| 1041 | crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / 512); |
Ken Sumrall | d33d417 | 2011-02-01 00:49:13 -0800 | [diff] [blame] | 1042 | crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1043 | strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256"); |
| 1044 | |
| 1045 | /* Make an encrypted master key */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1046 | if (create_encrypted_random_key(passwd, master_key, salt)) { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1047 | SLOGE("Cannot create encrypted master key\n"); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1048 | goto error_unencrypted; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* Write the key to the end of the partition */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1052 | put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, master_key, salt); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1053 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1054 | decrypt_master_key(passwd, salt, master_key, decrypted_master_key); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1055 | create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev); |
| 1056 | |
| 1057 | if (how == CRYPTO_ENABLE_WIPE) { |
| 1058 | rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr.fs_size); |
| 1059 | } else if (how == CRYPTO_ENABLE_INPLACE) { |
| 1060 | rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1061 | } else { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1062 | /* Shouldn't happen */ |
| 1063 | SLOGE("cryptfs_enable: internal error, unknown option\n"); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1064 | goto error_unencrypted; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /* Undo the dm-crypt mapping whether we succeed or not */ |
| 1068 | delete_crypto_blk_dev(crypto_blkdev); |
| 1069 | |
| 1070 | if (! rc) { |
| 1071 | /* Success */ |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1072 | |
Ken Sumrall | d33d417 | 2011-02-01 00:49:13 -0800 | [diff] [blame] | 1073 | /* Clear the encryption in progres flag in the footer */ |
| 1074 | crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS; |
| 1075 | put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0); |
| 1076 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1077 | sleep(2); /* Give the UI a change to show 100% progress */ |
Ken Sumrall | c290eaf | 2011-03-07 23:40:35 -0800 | [diff] [blame] | 1078 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1079 | } else { |
| 1080 | property_set("vold.encrypt_progress", "error_partially_encrypted"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 1081 | release_wake_lock(lockid); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1082 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1085 | /* hrm, the encrypt step claims success, but the reboot failed. |
| 1086 | * This should not happen. |
| 1087 | * Set the property and return. Hope the framework can deal with it. |
| 1088 | */ |
| 1089 | property_set("vold.encrypt_progress", "error_reboot_failed"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 1090 | release_wake_lock(lockid); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1091 | return rc; |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1092 | |
| 1093 | error_unencrypted: |
| 1094 | property_set("vold.encrypt_progress", "error_not_encrypted"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 1095 | if (lockid[0]) { |
| 1096 | release_wake_lock(lockid); |
| 1097 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1098 | return -1; |
| 1099 | |
| 1100 | error_shutting_down: |
| 1101 | /* we failed, and have not encrypted anthing, so the users's data is still intact, |
| 1102 | * but the framework is stopped and not restarted to show the error, so it's up to |
| 1103 | * vold to restart the system. |
| 1104 | */ |
| 1105 | SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system"); |
Ken Sumrall | c290eaf | 2011-03-07 23:40:35 -0800 | [diff] [blame] | 1106 | android_reboot(ANDROID_RB_RESTART, 0, 0); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1107 | |
| 1108 | /* shouldn't get here */ |
| 1109 | property_set("vold.encrypt_progress", "error_shutting_down"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 1110 | if (lockid[0]) { |
| 1111 | release_wake_lock(lockid); |
| 1112 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 1113 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1114 | } |
| 1115 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1116 | int cryptfs_changepw(char *newpw) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1117 | { |
| 1118 | struct crypt_mnt_ftr crypt_ftr; |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1119 | unsigned char encrypted_master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES]; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1120 | unsigned char salt[SALT_LEN]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1121 | char real_blkdev[MAXPATHLEN]; |
| 1122 | |
| 1123 | /* This is only allowed after we've successfully decrypted the master key */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1124 | if (! master_key_saved) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1125 | SLOGE("Key not saved, aborting"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1126 | return -1; |
| 1127 | } |
| 1128 | |
| 1129 | property_get("ro.crypto.fs_real_blkdev", real_blkdev, ""); |
| 1130 | if (strlen(real_blkdev) == 0) { |
Ken Sumrall | 57b63e6 | 2011-01-17 18:29:19 -0800 | [diff] [blame] | 1131 | SLOGE("Can't find real blkdev"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1132 | return -1; |
| 1133 | } |
| 1134 | |
| 1135 | /* get key */ |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1136 | if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) { |
Ken Sumrall | 57b63e6 | 2011-01-17 18:29:19 -0800 | [diff] [blame] | 1137 | SLOGE("Error getting crypt footer and key"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1138 | return -1; |
| 1139 | } |
| 1140 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1141 | encrypt_master_key(newpw, salt, saved_master_key, encrypted_master_key); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1142 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1143 | /* save the key */ |
| 1144 | put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1145 | |
| 1146 | return 0; |
| 1147 | } |