blob: b2499aaef609e3aaa073d3c31a010edb50383570 [file] [log] [blame]
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001/*
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 Sumrall8ddbe402011-01-17 15:26:29 -080036#include <openssl/sha.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080037#include <errno.h>
Ken Sumrallc290eaf2011-03-07 23:40:35 -080038#include <cutils/android_reboot.h>
Ken Sumrall3ed82362011-01-28 23:31:16 -080039#include <ext4.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070040#include <linux/kdev_t.h>
Ken Sumralle5032c42012-04-01 23:58:44 -070041#include <fs_mgr.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080042#include "cryptfs.h"
43#define LOG_TAG "Cryptfs"
Mike Lockwoodee6d8c42012-02-15 13:43:28 -080044#include "cutils/android_reboot.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080045#include "cutils/log.h"
46#include "cutils/properties.h"
Ken Sumrall5d4c68e2011-01-30 19:06:03 -080047#include "hardware_legacy/power.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070048#include "VolumeManager.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080049
50#define DM_CRYPT_BUF_SIZE 4096
Ken Sumrall8ddbe402011-01-17 15:26:29 -080051#define DATA_MNT_POINT "/data"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080052
Jason parks70a4b3f2011-01-28 10:10:47 -060053#define HASH_COUNT 2000
54#define KEY_LEN_BYTES 16
55#define IV_LEN_BYTES 16
56
Ken Sumrall29d8da82011-05-18 17:20:07 -070057#define KEY_IN_FOOTER "footer"
58
59#define EXT4_FS 1
60#define FAT_FS 2
61
Ken Sumralle919efe2012-09-29 17:07:41 -070062#define TABLE_LOAD_RETRIES 10
63
Ken Sumrall8f869aa2010-12-03 03:47:09 -080064char *me = "cryptfs";
65
Jason parks70a4b3f2011-01-28 10:10:47 -060066static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall29d8da82011-05-18 17:20:07 -070067static char *saved_data_blkdev;
Ken Sumrall3ad90722011-10-04 20:38:29 -070068static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -060069static int master_key_saved = 0;
Ken Sumralle5032c42012-04-01 23:58:44 -070070#define FSTAB_PREFIX "/fstab."
71static char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
Ken Sumrall8ddbe402011-01-17 15:26:29 -080072
Ken Sumrall8f869aa2010-12-03 03:47:09 -080073static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
74{
75 memset(io, 0, dataSize);
76 io->data_size = dataSize;
77 io->data_start = sizeof(struct dm_ioctl);
78 io->version[0] = 4;
79 io->version[1] = 0;
80 io->version[2] = 0;
81 io->flags = flags;
82 if (name) {
83 strncpy(io->name, name, sizeof(io->name));
84 }
85}
86
Ken Sumrall3ed82362011-01-28 23:31:16 -080087static unsigned int get_fs_size(char *dev)
88{
89 int fd, block_size;
90 struct ext4_super_block sb;
91 off64_t len;
92
93 if ((fd = open(dev, O_RDONLY)) < 0) {
94 SLOGE("Cannot open device to get filesystem size ");
95 return 0;
96 }
97
98 if (lseek64(fd, 1024, SEEK_SET) < 0) {
99 SLOGE("Cannot seek to superblock");
100 return 0;
101 }
102
103 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
104 SLOGE("Cannot read superblock");
105 return 0;
106 }
107
108 close(fd);
109
110 block_size = 1024 << sb.s_log_block_size;
111 /* compute length in bytes */
112 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
113
114 /* return length in sectors */
115 return (unsigned int) (len / 512);
116}
117
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800118static unsigned int get_blkdev_size(int fd)
119{
120 unsigned int nr_sec;
121
122 if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
123 nr_sec = 0;
124 }
125
126 return nr_sec;
127}
128
Ken Sumralle5032c42012-04-01 23:58:44 -0700129/* Get and cache the name of the fstab file so we don't
130 * keep talking over the socket to the property service.
131 */
132static char *get_fstab_filename(void)
133{
134 if (fstab_filename[0] == 0) {
135 strcpy(fstab_filename, FSTAB_PREFIX);
136 property_get("ro.hardware", fstab_filename + sizeof(FSTAB_PREFIX) - 1, "");
137 }
138
139 return fstab_filename;
140}
141
Ken Sumralle8744072011-01-18 22:01:55 -0800142/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800143 * update the failed mount count but not change the key.
144 */
145static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
Ken Sumralle8744072011-01-18 22:01:55 -0800146 unsigned char *key, unsigned char *salt)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800147{
148 int fd;
149 unsigned int nr_sec, cnt;
150 off64_t off;
151 int rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700152 char *fname;
153 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall3be890f2011-09-14 16:53:46 -0700154 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800155
Ken Sumralle5032c42012-04-01 23:58:44 -0700156 fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800157
Ken Sumrall29d8da82011-05-18 17:20:07 -0700158 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
159 fname = real_blk_name;
160 if ( (fd = open(fname, O_RDWR)) < 0) {
161 SLOGE("Cannot open real block device %s\n", fname);
162 return -1;
163 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800164
Ken Sumrall29d8da82011-05-18 17:20:07 -0700165 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
166 SLOGE("Cannot get size of block device %s\n", fname);
167 goto errout;
168 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800169
Ken Sumrall29d8da82011-05-18 17:20:07 -0700170 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
171 * encryption info footer and key, and plenty of bytes to spare for future
172 * growth.
173 */
174 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
175
176 if (lseek64(fd, off, SEEK_SET) == -1) {
177 SLOGE("Cannot seek to real block device footer\n");
178 goto errout;
179 }
180 } else if (key_loc[0] == '/') {
181 fname = key_loc;
182 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
183 SLOGE("Cannot open footer file %s\n", fname);
184 return -1;
185 }
186 } else {
Ken Sumralle5032c42012-04-01 23:58:44 -0700187 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall29d8da82011-05-18 17:20:07 -0700188 return -1;;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800189 }
190
191 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
192 SLOGE("Cannot write real block device footer\n");
193 goto errout;
194 }
195
196 if (key) {
Jason parks70a4b3f2011-01-28 10:10:47 -0600197 if (crypt_ftr->keysize != KEY_LEN_BYTES) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800198 SLOGE("Keysize of %d bits not supported for real block device %s\n",
Ken Sumrall29d8da82011-05-18 17:20:07 -0700199 crypt_ftr->keysize*8, fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800200 goto errout;
201 }
202
203 if ( (cnt = write(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700204 SLOGE("Cannot write key for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800205 goto errout;
206 }
207 }
208
Ken Sumralle8744072011-01-18 22:01:55 -0800209 if (salt) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700210 /* Compute the offset from the last write to the salt */
211 off = KEY_TO_SALT_PADDING;
212 if (! key)
213 off += crypt_ftr->keysize;
Ken Sumralle8744072011-01-18 22:01:55 -0800214
Ken Sumrall29d8da82011-05-18 17:20:07 -0700215 if (lseek64(fd, off, SEEK_CUR) == -1) {
Ken Sumralle8744072011-01-18 22:01:55 -0800216 SLOGE("Cannot seek to real block device salt \n");
217 goto errout;
218 }
219
220 if ( (cnt = write(fd, salt, SALT_LEN)) != SALT_LEN) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700221 SLOGE("Cannot write salt for real block device %s\n", fname);
222 goto errout;
223 }
224 }
225
Ken Sumrall3be890f2011-09-14 16:53:46 -0700226 fstat(fd, &statbuf);
227 /* If the keys are kept on a raw block device, do not try to truncate it. */
228 if (S_ISREG(statbuf.st_mode) && (key_loc[0] == '/')) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700229 if (ftruncate(fd, 0x4000)) {
Ken Sumrall3be890f2011-09-14 16:53:46 -0700230 SLOGE("Cannot set footer file size\n", fname);
Ken Sumralle8744072011-01-18 22:01:55 -0800231 goto errout;
232 }
233 }
234
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800235 /* Success! */
236 rc = 0;
237
238errout:
239 close(fd);
240 return rc;
241
242}
243
244static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
Ken Sumralle8744072011-01-18 22:01:55 -0800245 unsigned char *key, unsigned char *salt)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800246{
247 int fd;
248 unsigned int nr_sec, cnt;
249 off64_t off;
250 int rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700251 char key_loc[PROPERTY_VALUE_MAX];
252 char *fname;
253 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800254
Ken Sumralle5032c42012-04-01 23:58:44 -0700255 fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800256
Ken Sumrall29d8da82011-05-18 17:20:07 -0700257 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
258 fname = real_blk_name;
259 if ( (fd = open(fname, O_RDONLY)) < 0) {
260 SLOGE("Cannot open real block device %s\n", fname);
261 return -1;
262 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800263
Ken Sumrall29d8da82011-05-18 17:20:07 -0700264 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
265 SLOGE("Cannot get size of block device %s\n", fname);
266 goto errout;
267 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800268
Ken Sumrall29d8da82011-05-18 17:20:07 -0700269 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
270 * encryption info footer and key, and plenty of bytes to spare for future
271 * growth.
272 */
273 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
274
275 if (lseek64(fd, off, SEEK_SET) == -1) {
276 SLOGE("Cannot seek to real block device footer\n");
277 goto errout;
278 }
279 } else if (key_loc[0] == '/') {
280 fname = key_loc;
281 if ( (fd = open(fname, O_RDONLY)) < 0) {
282 SLOGE("Cannot open footer file %s\n", fname);
283 return -1;
284 }
285
286 /* Make sure it's 16 Kbytes in length */
287 fstat(fd, &statbuf);
Ken Sumrall3be890f2011-09-14 16:53:46 -0700288 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700289 SLOGE("footer file %s is not the expected size!\n", fname);
290 goto errout;
291 }
292 } else {
Ken Sumralle5032c42012-04-01 23:58:44 -0700293 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall29d8da82011-05-18 17:20:07 -0700294 return -1;;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800295 }
296
297 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
298 SLOGE("Cannot read real block device footer\n");
299 goto errout;
300 }
301
302 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700303 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800304 goto errout;
305 }
306
307 if (crypt_ftr->major_version != 1) {
308 SLOGE("Cannot understand major version %d real block device footer\n",
309 crypt_ftr->major_version);
310 goto errout;
311 }
312
313 if (crypt_ftr->minor_version != 0) {
314 SLOGW("Warning: crypto footer minor version %d, expected 0, continuing...\n",
315 crypt_ftr->minor_version);
316 }
317
318 if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) {
319 /* the footer size is bigger than we expected.
320 * Skip to it's stated end so we can read the key.
321 */
322 if (lseek(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) {
323 SLOGE("Cannot seek to start of key\n");
324 goto errout;
325 }
326 }
327
Jason parks70a4b3f2011-01-28 10:10:47 -0600328 if (crypt_ftr->keysize != KEY_LEN_BYTES) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800329 SLOGE("Keysize of %d bits not supported for real block device %s\n",
Ken Sumrall29d8da82011-05-18 17:20:07 -0700330 crypt_ftr->keysize * 8, fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800331 goto errout;
332 }
333
334 if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700335 SLOGE("Cannot read key for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800336 goto errout;
337 }
338
Ken Sumralle8744072011-01-18 22:01:55 -0800339 if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) {
340 SLOGE("Cannot seek to real block device salt\n");
341 goto errout;
342 }
343
344 if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700345 SLOGE("Cannot read salt for real block device %s\n", fname);
Ken Sumralle8744072011-01-18 22:01:55 -0800346 goto errout;
347 }
348
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800349 /* Success! */
350 rc = 0;
351
352errout:
353 close(fd);
354 return rc;
355}
356
357/* Convert a binary key of specified length into an ascii hex string equivalent,
358 * without the leading 0x and with null termination
359 */
360void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
361 char *master_key_ascii)
362{
363 unsigned int i, a;
364 unsigned char nibble;
365
366 for (i=0, a=0; i<keysize; i++, a+=2) {
367 /* For each byte, write out two ascii hex digits */
368 nibble = (master_key[i] >> 4) & 0xf;
369 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
370
371 nibble = master_key[i] & 0xf;
372 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
373 }
374
375 /* Add the null termination */
376 master_key_ascii[a] = '\0';
377
378}
379
380static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -0700381 char *real_blk_name, char *crypto_blk_name, const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800382{
383 char buffer[DM_CRYPT_BUF_SIZE];
384 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
385 char *crypt_params;
386 struct dm_ioctl *io;
387 struct dm_target_spec *tgt;
388 unsigned int minor;
389 int fd;
Ken Sumralle919efe2012-09-29 17:07:41 -0700390 int i;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800391 int retval = -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800392
393 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
394 SLOGE("Cannot open device-mapper\n");
395 goto errout;
396 }
397
398 io = (struct dm_ioctl *) buffer;
399
400 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
401 if (ioctl(fd, DM_DEV_CREATE, io)) {
402 SLOGE("Cannot create dm-crypt device\n");
403 goto errout;
404 }
405
406 /* Get the device status, in particular, the name of it's device file */
407 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
408 if (ioctl(fd, DM_DEV_STATUS, io)) {
409 SLOGE("Cannot retrieve dm-crypt device status\n");
410 goto errout;
411 }
412 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
413 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
414
415 /* Load the mapping table for this device */
416 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
417
418 ioctl_init(io, 4096, name, 0);
419 io->target_count = 1;
420 tgt->status = 0;
421 tgt->sector_start = 0;
422 tgt->length = crypt_ftr->fs_size;
423 strcpy(tgt->target_type, "crypt");
424
425 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
426 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
427 sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name,
428 master_key_ascii, real_blk_name);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800429 crypt_params += strlen(crypt_params) + 1;
430 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
431 tgt->next = crypt_params - buffer;
432
Ken Sumralle919efe2012-09-29 17:07:41 -0700433 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
434 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
435 break;
436 }
437 usleep(500000);
438 }
439
440 if (i == TABLE_LOAD_RETRIES) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800441 SLOGE("Cannot load dm-crypt mapping table.\n");
442 goto errout;
Ken Sumralle919efe2012-09-29 17:07:41 -0700443 } else if (i) {
444 SLOGI("Took %d tries to load dmcrypt table.\n", i + 1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800445 }
446
447 /* Resume this device to activate it */
448 ioctl_init(io, 4096, name, 0);
449
450 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
451 SLOGE("Cannot resume the dm-crypt device\n");
452 goto errout;
453 }
454
455 /* We made it here with no errors. Woot! */
456 retval = 0;
457
458errout:
459 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
460
461 return retval;
462}
463
Ken Sumrall29d8da82011-05-18 17:20:07 -0700464static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800465{
466 int fd;
467 char buffer[DM_CRYPT_BUF_SIZE];
468 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800469 int retval = -1;
470
471 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
472 SLOGE("Cannot open device-mapper\n");
473 goto errout;
474 }
475
476 io = (struct dm_ioctl *) buffer;
477
478 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
479 if (ioctl(fd, DM_DEV_REMOVE, io)) {
480 SLOGE("Cannot remove dm-crypt device\n");
481 goto errout;
482 }
483
484 /* We made it here with no errors. Woot! */
485 retval = 0;
486
487errout:
488 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
489
490 return retval;
491
492}
493
Ken Sumralle8744072011-01-18 22:01:55 -0800494static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800495{
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800496 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800497 PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800498 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800499}
500
Ken Sumralle8744072011-01-18 22:01:55 -0800501static int encrypt_master_key(char *passwd, unsigned char *salt,
502 unsigned char *decrypted_master_key,
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800503 unsigned char *encrypted_master_key)
504{
505 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
506 EVP_CIPHER_CTX e_ctx;
507 int encrypted_len, final_len;
508
509 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800510 pbkdf2(passwd, salt, ikey);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800511
512 /* Initialize the decryption engine */
513 if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
514 SLOGE("EVP_EncryptInit failed\n");
515 return -1;
516 }
517 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800518
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800519 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800520 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
521 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800522 SLOGE("EVP_EncryptUpdate failed\n");
523 return -1;
524 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800525 if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800526 SLOGE("EVP_EncryptFinal failed\n");
527 return -1;
528 }
529
530 if (encrypted_len + final_len != KEY_LEN_BYTES) {
531 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
532 return -1;
533 } else {
534 return 0;
535 }
536}
537
Ken Sumralle8744072011-01-18 22:01:55 -0800538static int decrypt_master_key(char *passwd, unsigned char *salt,
539 unsigned char *encrypted_master_key,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800540 unsigned char *decrypted_master_key)
541{
542 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800543 EVP_CIPHER_CTX d_ctx;
544 int decrypted_len, final_len;
545
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800546 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800547 pbkdf2(passwd, salt, ikey);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800548
549 /* Initialize the decryption engine */
550 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
551 return -1;
552 }
553 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
554 /* Decrypt the master key */
555 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
556 encrypted_master_key, KEY_LEN_BYTES)) {
557 return -1;
558 }
559 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
560 return -1;
561 }
562
563 if (decrypted_len + final_len != KEY_LEN_BYTES) {
564 return -1;
565 } else {
566 return 0;
567 }
568}
569
Ken Sumralle8744072011-01-18 22:01:55 -0800570static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt)
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800571{
572 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -0800573 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800574 EVP_CIPHER_CTX e_ctx;
575 int encrypted_len, final_len;
576
577 /* Get some random bits for a key */
578 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -0800579 read(fd, key_buf, sizeof(key_buf));
580 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800581 close(fd);
582
583 /* Now encrypt it with the password */
Ken Sumralle8744072011-01-18 22:01:55 -0800584 return encrypt_master_key(passwd, salt, key_buf, master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800585}
586
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800587static int wait_and_unmount(char *mountpoint)
588{
589 int i, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -0800590#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800591
592 /* Now umount the tmpfs filesystem */
593 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
594 if (umount(mountpoint)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700595 if (errno == EINVAL) {
596 /* EINVAL is returned if the directory is not a mountpoint,
597 * i.e. there is no filesystem mounted there. So just get out.
598 */
599 break;
600 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800601 sleep(1);
602 i++;
603 } else {
604 break;
605 }
606 }
607
608 if (i < WAIT_UNMOUNT_COUNT) {
609 SLOGD("unmounting %s succeeded\n", mountpoint);
610 rc = 0;
611 } else {
612 SLOGE("unmounting %s failed\n", mountpoint);
613 rc = -1;
614 }
615
616 return rc;
617}
618
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800619#define DATA_PREP_TIMEOUT 100
620static int prep_data_fs(void)
621{
622 int i;
623
624 /* Do the prep of the /data filesystem */
625 property_set("vold.post_fs_data_done", "0");
626 property_set("vold.decrypt", "trigger_post_fs_data");
627 SLOGD("Just triggered post_fs_data\n");
628
629 /* Wait a max of 25 seconds, hopefully it takes much less */
630 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700631 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800632
633 property_get("vold.post_fs_data_done", p, "0");
634 if (*p == '1') {
635 break;
636 } else {
637 usleep(250000);
638 }
639 }
640 if (i == DATA_PREP_TIMEOUT) {
641 /* Ugh, we failed to prep /data in time. Bail. */
642 return -1;
643 } else {
644 SLOGD("post_fs_data done\n");
645 return 0;
646 }
647}
648
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800649int cryptfs_restart(void)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800650{
651 char fs_type[32];
652 char real_blkdev[MAXPATHLEN];
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800653 char crypto_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800654 char fs_options[256];
655 unsigned long mnt_flags;
656 struct stat statbuf;
657 int rc = -1, i;
Ken Sumrall0cc16632011-01-18 20:32:26 -0800658 static int restart_successful = 0;
659
660 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -0600661 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -0800662 SLOGE("Encrypted filesystem not validated, aborting");
663 return -1;
664 }
665
666 if (restart_successful) {
667 SLOGE("System already restarted with encrypted disk, aborting");
668 return -1;
669 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800670
671 /* Here is where we shut down the framework. The init scripts
672 * start all services in one of three classes: core, main or late_start.
673 * On boot, we start core and main. Now, we stop main, but not core,
674 * as core includes vold and a few other really important things that
675 * we need to keep running. Once main has stopped, we should be able
676 * to umount the tmpfs /data, then mount the encrypted /data.
677 * We then restart the class main, and also the class late_start.
678 * At the moment, I've only put a few things in late_start that I know
679 * are not needed to bring up the framework, and that also cause problems
680 * with unmounting the tmpfs /data, but I hope to add add more services
681 * to the late_start class as we optimize this to decrease the delay
682 * till the user is asked for the password to the filesystem.
683 */
684
685 /* The init files are setup to stop the class main when vold.decrypt is
686 * set to trigger_reset_main.
687 */
688 property_set("vold.decrypt", "trigger_reset_main");
689 SLOGD("Just asked init to shut down class main\n");
690
691 /* Now that the framework is shutdown, we should be able to umount()
692 * the tmpfs filesystem, and mount the real one.
693 */
694
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800695 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
696 if (strlen(crypto_blkdev) == 0) {
697 SLOGE("fs_crypto_blkdev not set\n");
698 return -1;
699 }
700
Ken Sumralle5032c42012-04-01 23:58:44 -0700701 if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) {
702 /* If that succeeded, then mount the decrypted filesystem */
703 fs_mgr_do_mount(get_fstab_filename(), DATA_MNT_POINT, crypto_blkdev, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800704
Ken Sumralle5032c42012-04-01 23:58:44 -0700705 property_set("vold.decrypt", "trigger_load_persist_props");
706 /* Create necessary paths on /data */
707 if (prep_data_fs()) {
708 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800709 }
Ken Sumralle5032c42012-04-01 23:58:44 -0700710
711 /* startup service classes main and late_start */
712 property_set("vold.decrypt", "trigger_restart_framework");
713 SLOGD("Just triggered restart_framework\n");
714
715 /* Give it a few moments to get started */
716 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800717 }
718
Ken Sumrall0cc16632011-01-18 20:32:26 -0800719 if (rc == 0) {
720 restart_successful = 1;
721 }
722
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800723 return rc;
724}
725
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800726static int do_crypto_complete(char *mount_point)
727{
728 struct crypt_mnt_ftr crypt_ftr;
729 unsigned char encrypted_master_key[32];
730 unsigned char salt[SALT_LEN];
731 char real_blkdev[MAXPATHLEN];
Ken Sumrall29d8da82011-05-18 17:20:07 -0700732 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -0800733 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800734
735 property_get("ro.crypto.state", encrypted_state, "");
736 if (strcmp(encrypted_state, "encrypted") ) {
737 SLOGE("not running with encryption, aborting");
738 return 1;
739 }
740
Ken Sumralle5032c42012-04-01 23:58:44 -0700741 fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800742
743 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumralle5032c42012-04-01 23:58:44 -0700744 fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc));
745
Ken Sumralle1a45852011-12-14 21:24:27 -0800746 /*
747 * Only report this error if key_loc is a file and it exists.
748 * If the device was never encrypted, and /data is not mountable for
749 * some reason, returning 1 should prevent the UI from presenting the
750 * a "enter password" screen, or worse, a "press button to wipe the
751 * device" screen.
752 */
753 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
754 SLOGE("master key file does not exist, aborting");
755 return 1;
756 } else {
757 SLOGE("Error getting crypt footer and key\n");
758 return -1;
759 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800760 }
761
762 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
763 SLOGE("Encryption process didn't finish successfully\n");
764 return -2; /* -2 is the clue to the UI that there is no usable data on the disk,
765 * and give the user an option to wipe the disk */
766 }
767
768 /* We passed the test! We shall diminish, and return to the west */
769 return 0;
770}
771
Ken Sumrall29d8da82011-05-18 17:20:07 -0700772static int test_mount_encrypted_fs(char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800773{
774 struct crypt_mnt_ftr crypt_ftr;
775 /* Allocate enough space for a 256 bit key, but we may use less */
776 unsigned char encrypted_master_key[32], decrypted_master_key[32];
Ken Sumralle8744072011-01-18 22:01:55 -0800777 unsigned char salt[SALT_LEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800778 char crypto_blkdev[MAXPATHLEN];
779 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800780 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800781 unsigned int orig_failed_decrypt_count;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700782 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800783 int rc;
784
Ken Sumrall0cc16632011-01-18 20:32:26 -0800785 property_get("ro.crypto.state", encrypted_state, "");
Jason parks70a4b3f2011-01-28 10:10:47 -0600786 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
Ken Sumrall0cc16632011-01-18 20:32:26 -0800787 SLOGE("encrypted fs already validated or not running with encryption, aborting");
788 return -1;
789 }
790
Ken Sumralle5032c42012-04-01 23:58:44 -0700791 fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800792
Ken Sumralle8744072011-01-18 22:01:55 -0800793 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800794 SLOGE("Error getting crypt footer and key\n");
795 return -1;
796 }
Ken Sumralld33d4172011-02-01 00:49:13 -0800797
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800798 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size);
799 orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count;
800
801 if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Ken Sumralle8744072011-01-18 22:01:55 -0800802 decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800803 }
804
805 if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -0700806 real_blkdev, crypto_blkdev, label)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800807 SLOGE("Error creating decrypted block device\n");
808 return -1;
809 }
810
811 /* If init detects an encrypted filesystme, it writes a file for each such
812 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
813 * files and passes that data to me */
814 /* Create a tmp mount point to try mounting the decryptd fs
815 * Since we're here, the mount_point should be a tmpfs filesystem, so make
816 * a directory in it to test mount the decrypted filesystem.
817 */
818 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
819 mkdir(tmp_mount_point, 0755);
Ken Sumralle5032c42012-04-01 23:58:44 -0700820 if (fs_mgr_do_mount(get_fstab_filename(), DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800821 SLOGE("Error temp mounting decrypted block device\n");
Ken Sumrall29d8da82011-05-18 17:20:07 -0700822 delete_crypto_blk_dev(label);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800823 crypt_ftr.failed_decrypt_count++;
824 } else {
825 /* Success, so just umount and we'll mount it properly when we restart
826 * the framework.
827 */
828 umount(tmp_mount_point);
829 crypt_ftr.failed_decrypt_count = 0;
830 }
831
832 if (orig_failed_decrypt_count != crypt_ftr.failed_decrypt_count) {
Ken Sumralle8744072011-01-18 22:01:55 -0800833 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800834 }
835
836 if (crypt_ftr.failed_decrypt_count) {
837 /* We failed to mount the device, so return an error */
838 rc = crypt_ftr.failed_decrypt_count;
839
840 } else {
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800841 /* Woot! Success! Save the name of the crypto block device
842 * so we can mount it when restarting the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800843 */
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800844 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -0600845
846 /* Also save a the master key so we can reencrypted the key
847 * the key when we want to change the password on it.
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800848 */
Jason parks70a4b3f2011-01-28 10:10:47 -0600849 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall29d8da82011-05-18 17:20:07 -0700850 saved_data_blkdev = strdup(real_blkdev);
Ken Sumrall3ad90722011-10-04 20:38:29 -0700851 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -0600852 master_key_saved = 1;
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800853 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800854 }
855
856 return rc;
857}
858
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700859/* Called by vold when it wants to undo the crypto mapping of a volume it
860 * manages. This is usually in response to a factory reset, when we want
861 * to undo the crypto mapping so the volume is formatted in the clear.
862 */
863int cryptfs_revert_volume(const char *label)
864{
865 return delete_crypto_blk_dev((char *)label);
866}
867
Ken Sumrall29d8da82011-05-18 17:20:07 -0700868/*
869 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
870 * Setup a dm-crypt mapping, use the saved master key from
871 * setting up the /data mapping, and return the new device path.
872 */
873int cryptfs_setup_volume(const char *label, int major, int minor,
874 char *crypto_sys_path, unsigned int max_path,
875 int *new_major, int *new_minor)
876{
877 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
878 struct crypt_mnt_ftr sd_crypt_ftr;
879 unsigned char key[32], salt[32];
880 struct stat statbuf;
881 int nr_sec, fd;
882
883 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
884
885 /* Just want the footer, but gotta get it all */
886 get_crypt_ftr_and_key(saved_data_blkdev, &sd_crypt_ftr, key, salt);
887
888 /* Update the fs_size field to be the size of the volume */
889 fd = open(real_blkdev, O_RDONLY);
890 nr_sec = get_blkdev_size(fd);
891 close(fd);
892 if (nr_sec == 0) {
893 SLOGE("Cannot get size of volume %s\n", real_blkdev);
894 return -1;
895 }
896
897 sd_crypt_ftr.fs_size = nr_sec;
898 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
899 crypto_blkdev, label);
900
901 stat(crypto_blkdev, &statbuf);
902 *new_major = MAJOR(statbuf.st_rdev);
903 *new_minor = MINOR(statbuf.st_rdev);
904
905 /* Create path to sys entry for this block device */
906 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
907
908 return 0;
909}
910
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800911int cryptfs_crypto_complete(void)
912{
913 return do_crypto_complete("/data");
914}
915
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800916int cryptfs_check_passwd(char *passwd)
917{
918 int rc = -1;
919
Ken Sumrall29d8da82011-05-18 17:20:07 -0700920 rc = test_mount_encrypted_fs(passwd, DATA_MNT_POINT, "userdata");
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800921
922 return rc;
923}
924
Ken Sumrall3ad90722011-10-04 20:38:29 -0700925int cryptfs_verify_passwd(char *passwd)
926{
927 struct crypt_mnt_ftr crypt_ftr;
928 /* Allocate enough space for a 256 bit key, but we may use less */
929 unsigned char encrypted_master_key[32], decrypted_master_key[32];
930 unsigned char salt[SALT_LEN];
931 char real_blkdev[MAXPATHLEN];
Ken Sumrall3ad90722011-10-04 20:38:29 -0700932 char encrypted_state[PROPERTY_VALUE_MAX];
933 int rc;
934
935 property_get("ro.crypto.state", encrypted_state, "");
936 if (strcmp(encrypted_state, "encrypted") ) {
937 SLOGE("device not encrypted, aborting");
938 return -2;
939 }
940
941 if (!master_key_saved) {
942 SLOGE("encrypted fs not yet mounted, aborting");
943 return -1;
944 }
945
946 if (!saved_mount_point) {
947 SLOGE("encrypted fs failed to save mount point, aborting");
948 return -1;
949 }
950
Ken Sumralle5032c42012-04-01 23:58:44 -0700951 fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall3ad90722011-10-04 20:38:29 -0700952
953 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
954 SLOGE("Error getting crypt footer and key\n");
955 return -1;
956 }
957
958 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
959 /* If the device has no password, then just say the password is valid */
960 rc = 0;
961 } else {
962 decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
963 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
964 /* They match, the password is correct */
965 rc = 0;
966 } else {
967 /* If incorrect, sleep for a bit to prevent dictionary attacks */
968 sleep(1);
969 rc = 1;
970 }
971 }
972
973 return rc;
974}
975
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800976/* Initialize a crypt_mnt_ftr structure. The keysize is
977 * defaulted to 16 bytes, and the filesystem size to 0.
978 * Presumably, at a minimum, the caller will update the
979 * filesystem size and crypto_type_name after calling this function.
980 */
981static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
982{
983 ftr->magic = CRYPT_MNT_MAGIC;
984 ftr->major_version = 1;
985 ftr->minor_version = 0;
986 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
987 ftr->flags = 0;
Jason parks70a4b3f2011-01-28 10:10:47 -0600988 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800989 ftr->spare1 = 0;
990 ftr->fs_size = 0;
991 ftr->failed_decrypt_count = 0;
992 ftr->crypto_type_name[0] = '\0';
993}
994
Ken Sumrall29d8da82011-05-18 17:20:07 -0700995static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800996{
997 char cmdline[256];
998 int rc = -1;
999
Ken Sumrall29d8da82011-05-18 17:20:07 -07001000 if (type == EXT4_FS) {
1001 snprintf(cmdline, sizeof(cmdline), "/system/bin/make_ext4fs -a /data -l %lld %s",
1002 size * 512, crypto_blkdev);
1003 SLOGI("Making empty filesystem with command %s\n", cmdline);
1004 } else if (type== FAT_FS) {
1005 snprintf(cmdline, sizeof(cmdline), "/system/bin/newfs_msdos -F 32 -O android -c 8 -s %lld %s",
1006 size, crypto_blkdev);
1007 SLOGI("Making empty filesystem with command %s\n", cmdline);
1008 } else {
1009 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
1010 return -1;
1011 }
1012
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001013 if (system(cmdline)) {
1014 SLOGE("Error creating empty filesystem on %s\n", crypto_blkdev);
1015 } else {
1016 SLOGD("Successfully created empty filesystem on %s\n", crypto_blkdev);
1017 rc = 0;
1018 }
1019
1020 return rc;
1021}
1022
1023static inline int unix_read(int fd, void* buff, int len)
1024{
1025 int ret;
1026 do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR);
1027 return ret;
1028}
1029
1030static inline int unix_write(int fd, const void* buff, int len)
1031{
1032 int ret;
1033 do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR);
1034 return ret;
1035}
1036
1037#define CRYPT_INPLACE_BUFSIZE 4096
1038#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512)
Ken Sumrall29d8da82011-05-18 17:20:07 -07001039static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, off64_t size,
1040 off64_t *size_already_done, off64_t tot_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001041{
1042 int realfd, cryptofd;
1043 char *buf[CRYPT_INPLACE_BUFSIZE];
1044 int rc = -1;
1045 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001046 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001047 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001048
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001049 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
1050 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
1051 return -1;
1052 }
1053
1054 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
1055 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1056 close(realfd);
1057 return -1;
1058 }
1059
1060 /* This is pretty much a simple loop of reading 4K, and writing 4K.
1061 * The size passed in is the number of 512 byte sectors in the filesystem.
1062 * So compute the number of whole 4K blocks we should read/write,
1063 * and the remainder.
1064 */
1065 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
1066 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001067 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
1068 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001069
1070 SLOGE("Encrypting filesystem in place...");
1071
Ken Sumrall29d8da82011-05-18 17:20:07 -07001072 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001073 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001074 /* process the majority of the filesystem in blocks */
1075 for (i=0; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001076 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001077 if (new_pct > cur_pct) {
1078 char buf[8];
1079
1080 cur_pct = new_pct;
1081 snprintf(buf, sizeof(buf), "%lld", cur_pct);
1082 property_set("vold.encrypt_progress", buf);
1083 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001084 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1085 SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1086 goto errout;
1087 }
1088 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1089 SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1090 goto errout;
1091 }
1092 }
1093
1094 /* Do any remaining sectors */
1095 for (i=0; i<remainder; i++) {
1096 if (unix_read(realfd, buf, 512) <= 0) {
1097 SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1098 goto errout;
1099 }
1100 if (unix_write(cryptofd, buf, 512) <= 0) {
1101 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1102 goto errout;
1103 }
1104 }
1105
Ken Sumrall29d8da82011-05-18 17:20:07 -07001106 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001107 rc = 0;
1108
1109errout:
1110 close(realfd);
1111 close(cryptofd);
1112
1113 return rc;
1114}
1115
1116#define CRYPTO_ENABLE_WIPE 1
1117#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001118
1119#define FRAMEWORK_BOOT_WAIT 60
1120
Ken Sumrall29d8da82011-05-18 17:20:07 -07001121static inline int should_encrypt(struct volume_info *volume)
1122{
1123 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
1124 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
1125}
1126
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001127int cryptfs_enable(char *howarg, char *passwd)
1128{
1129 int how = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001130 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN], sd_crypto_blkdev[MAXPATHLEN];
Ken Sumralle5032c42012-04-01 23:58:44 -07001131 unsigned long nr_sec;
Jason parks70a4b3f2011-01-28 10:10:47 -06001132 unsigned char master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
Ken Sumralle8744072011-01-18 22:01:55 -08001133 unsigned char salt[SALT_LEN];
Ken Sumrall319b1042011-06-14 14:01:55 -07001134 int rc=-1, fd, i, ret;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001135 struct crypt_mnt_ftr crypt_ftr, sd_crypt_ftr;;
1136 char tmpfs_options[PROPERTY_VALUE_MAX];
1137 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001138 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07001139 char key_loc[PROPERTY_VALUE_MAX];
1140 char fuse_sdcard[PROPERTY_VALUE_MAX];
1141 char *sd_mnt_point;
1142 char sd_blk_dev[256] = { 0 };
1143 int num_vols;
1144 struct volume_info *vol_list = 0;
1145 off64_t cur_encryption_done=0, tot_encryption_size=0;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001146
1147 property_get("ro.crypto.state", encrypted_state, "");
1148 if (strcmp(encrypted_state, "unencrypted")) {
1149 SLOGE("Device is already running encrypted, aborting");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001150 goto error_unencrypted;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001151 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001152
Ken Sumralle5032c42012-04-01 23:58:44 -07001153 fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001154
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001155 if (!strcmp(howarg, "wipe")) {
1156 how = CRYPTO_ENABLE_WIPE;
1157 } else if (! strcmp(howarg, "inplace")) {
1158 how = CRYPTO_ENABLE_INPLACE;
1159 } else {
1160 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08001161 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001162 }
1163
Ken Sumralle5032c42012-04-01 23:58:44 -07001164 fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001165
Ken Sumrall3ed82362011-01-28 23:31:16 -08001166 /* Get the size of the real block device */
1167 fd = open(real_blkdev, O_RDONLY);
1168 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
1169 SLOGE("Cannot get size of block device %s\n", real_blkdev);
1170 goto error_unencrypted;
1171 }
1172 close(fd);
1173
1174 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001175 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001176 unsigned int fs_size_sec, max_fs_size_sec;
1177
1178 fs_size_sec = get_fs_size(real_blkdev);
1179 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1180
1181 if (fs_size_sec > max_fs_size_sec) {
1182 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
1183 goto error_unencrypted;
1184 }
1185 }
1186
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001187 /* Get a wakelock as this may take a while, and we don't want the
1188 * device to sleep on us. We'll grab a partial wakelock, and if the UI
1189 * wants to keep the screen on, it can grab a full wakelock.
1190 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001191 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001192 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
1193
Jeff Sharkey7382f812012-08-23 14:08:59 -07001194 /* Get the sdcard mount point */
1195 sd_mnt_point = getenv("MULTIUSER_EXTERNAL_STORAGE");
1196 if (!sd_mnt_point) {
1197 sd_mnt_point = getenv("EXTERNAL_STORAGE");
1198 }
1199 if (!sd_mnt_point) {
1200 sd_mnt_point = "/mnt/sdcard";
1201 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07001202
1203 num_vols=vold_getNumDirectVolumes();
1204 vol_list = malloc(sizeof(struct volume_info) * num_vols);
1205 vold_getDirectVolumeList(vol_list);
1206
1207 for (i=0; i<num_vols; i++) {
1208 if (should_encrypt(&vol_list[i])) {
1209 fd = open(vol_list[i].blk_dev, O_RDONLY);
1210 if ( (vol_list[i].size = get_blkdev_size(fd)) == 0) {
1211 SLOGE("Cannot get size of block device %s\n", vol_list[i].blk_dev);
1212 goto error_unencrypted;
1213 }
1214 close(fd);
1215
Ken Sumrall3b170052011-07-11 15:38:57 -07001216 ret=vold_disableVol(vol_list[i].label);
Ken Sumrall319b1042011-06-14 14:01:55 -07001217 if ((ret < 0) && (ret != UNMOUNT_NOT_MOUNTED_ERR)) {
1218 /* -2 is returned when the device exists but is not currently mounted.
1219 * ignore the error and continue. */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001220 SLOGE("Failed to unmount volume %s\n", vol_list[i].label);
1221 goto error_unencrypted;
1222 }
1223 }
1224 }
1225
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001226 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001227 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001228 */
1229 property_set("vold.decrypt", "trigger_shutdown_framework");
1230 SLOGD("Just asked init to shut down class main\n");
1231
Ken Sumrall425524d2012-06-14 20:55:28 -07001232 if (vold_unmountAllAsecs()) {
1233 /* Just report the error. If any are left mounted,
1234 * umounting /data below will fail and handle the error.
1235 */
1236 SLOGE("Error unmounting internal asecs");
1237 }
1238
Ken Sumrall29d8da82011-05-18 17:20:07 -07001239 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
1240 if (!strcmp(fuse_sdcard, "true")) {
1241 /* This is a device using the fuse layer to emulate the sdcard semantics
1242 * on top of the userdata partition. vold does not manage it, it is managed
1243 * by the sdcard service. The sdcard service was killed by the property trigger
1244 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
1245 * unlike the case for vold managed devices above.
1246 */
1247 if (wait_and_unmount(sd_mnt_point)) {
1248 goto error_shutting_down;
1249 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001250 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001251
1252 /* Now unmount the /data partition. */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001253 if (wait_and_unmount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001254 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001255 }
1256
1257 /* Do extra work for a better UX when doing the long inplace encryption */
1258 if (how == CRYPTO_ENABLE_INPLACE) {
1259 /* Now that /data is unmounted, we need to mount a tmpfs
1260 * /data, set a property saying we're doing inplace encryption,
1261 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001262 */
Ken Sumralle5032c42012-04-01 23:58:44 -07001263 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001264 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001265 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001266 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08001267 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001268
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001269 /* restart the framework. */
1270 /* Create necessary paths on /data */
1271 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001272 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001273 }
1274
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001275 /* startup service classes main and late_start */
1276 property_set("vold.decrypt", "trigger_restart_min_framework");
1277 SLOGD("Just triggered restart_min_framework\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001278
Ken Sumrall7df84122011-01-18 14:04:08 -08001279 /* OK, the framework is restarted and will soon be showing a
1280 * progress bar. Time to setup an encrypted mapping, and
1281 * either write a new filesystem, or encrypt in place updating
1282 * the progress bar as we work.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001283 */
1284 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001285
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001286 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001287 /* Initialize a crypt_mnt_ftr for the partition */
1288 cryptfs_init_crypt_mnt_ftr(&crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001289 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
1290 crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1291 } else {
1292 crypt_ftr.fs_size = nr_sec;
1293 }
Ken Sumralld33d4172011-02-01 00:49:13 -08001294 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001295 strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
1296
1297 /* Make an encrypted master key */
Ken Sumralle8744072011-01-18 22:01:55 -08001298 if (create_encrypted_random_key(passwd, master_key, salt)) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001299 SLOGE("Cannot create encrypted master key\n");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001300 goto error_unencrypted;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001301 }
1302
1303 /* Write the key to the end of the partition */
Ken Sumralle8744072011-01-18 22:01:55 -08001304 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, master_key, salt);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001305
Ken Sumralle8744072011-01-18 22:01:55 -08001306 decrypt_master_key(passwd, salt, master_key, decrypted_master_key);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001307 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
1308 "userdata");
1309
Ken Sumrall128626f2011-06-28 18:45:14 -07001310 /* The size of the userdata partition, and add in the vold volumes below */
1311 tot_encryption_size = crypt_ftr.fs_size;
1312
Ken Sumrall29d8da82011-05-18 17:20:07 -07001313 /* setup crypto mapping for all encryptable volumes handled by vold */
1314 for (i=0; i<num_vols; i++) {
1315 if (should_encrypt(&vol_list[i])) {
1316 vol_list[i].crypt_ftr = crypt_ftr; /* gotta love struct assign */
1317 vol_list[i].crypt_ftr.fs_size = vol_list[i].size;
1318 create_crypto_blk_dev(&vol_list[i].crypt_ftr, decrypted_master_key,
1319 vol_list[i].blk_dev, vol_list[i].crypto_blkdev,
1320 vol_list[i].label);
Ken Sumrall128626f2011-06-28 18:45:14 -07001321 tot_encryption_size += vol_list[i].size;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001322 }
1323 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001324
1325 if (how == CRYPTO_ENABLE_WIPE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001326 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr.fs_size, EXT4_FS);
1327 /* Encrypt all encryptable volumes handled by vold */
1328 if (!rc) {
1329 for (i=0; i<num_vols; i++) {
1330 if (should_encrypt(&vol_list[i])) {
1331 rc = cryptfs_enable_wipe(vol_list[i].crypto_blkdev,
1332 vol_list[i].crypt_ftr.fs_size, FAT_FS);
1333 }
1334 }
1335 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001336 } else if (how == CRYPTO_ENABLE_INPLACE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001337 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size,
1338 &cur_encryption_done, tot_encryption_size);
1339 /* Encrypt all encryptable volumes handled by vold */
1340 if (!rc) {
1341 for (i=0; i<num_vols; i++) {
1342 if (should_encrypt(&vol_list[i])) {
1343 rc = cryptfs_enable_inplace(vol_list[i].crypto_blkdev,
1344 vol_list[i].blk_dev,
1345 vol_list[i].crypt_ftr.fs_size,
1346 &cur_encryption_done, tot_encryption_size);
1347 }
1348 }
1349 }
1350 if (!rc) {
1351 /* The inplace routine never actually sets the progress to 100%
1352 * due to the round down nature of integer division, so set it here */
1353 property_set("vold.encrypt_progress", "100");
1354 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001355 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001356 /* Shouldn't happen */
1357 SLOGE("cryptfs_enable: internal error, unknown option\n");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001358 goto error_unencrypted;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001359 }
1360
1361 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001362 delete_crypto_blk_dev("userdata");
1363 for (i=0; i<num_vols; i++) {
1364 if (should_encrypt(&vol_list[i])) {
1365 delete_crypto_blk_dev(vol_list[i].label);
1366 }
1367 }
1368
1369 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001370
1371 if (! rc) {
1372 /* Success */
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001373
Ken Sumralld33d4172011-02-01 00:49:13 -08001374 /* Clear the encryption in progres flag in the footer */
1375 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
1376 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0);
1377
Ken Sumrall29d8da82011-05-18 17:20:07 -07001378 sleep(2); /* Give the UI a chance to show 100% progress */
Ken Sumrallc290eaf2011-03-07 23:40:35 -08001379 android_reboot(ANDROID_RB_RESTART, 0, 0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001380 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08001381 char value[PROPERTY_VALUE_MAX];
1382
Ken Sumrall319369a2012-06-27 16:30:18 -07001383 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08001384 if (!strcmp(value, "1")) {
1385 /* wipe data if encryption failed */
1386 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
1387 mkdir("/cache/recovery", 0700);
Nick Kralevich4684e582012-06-26 15:07:03 -07001388 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08001389 if (fd >= 0) {
1390 write(fd, "--wipe_data", strlen("--wipe_data") + 1);
1391 close(fd);
1392 } else {
1393 SLOGE("could not open /cache/recovery/command\n");
1394 }
1395 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
1396 } else {
1397 /* set property to trigger dialog */
1398 property_set("vold.encrypt_progress", "error_partially_encrypted");
1399 release_wake_lock(lockid);
1400 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001401 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001402 }
1403
Ken Sumrall3ed82362011-01-28 23:31:16 -08001404 /* hrm, the encrypt step claims success, but the reboot failed.
1405 * This should not happen.
1406 * Set the property and return. Hope the framework can deal with it.
1407 */
1408 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001409 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001410 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08001411
1412error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07001413 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001414 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001415 if (lockid[0]) {
1416 release_wake_lock(lockid);
1417 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001418 return -1;
1419
1420error_shutting_down:
1421 /* we failed, and have not encrypted anthing, so the users's data is still intact,
1422 * but the framework is stopped and not restarted to show the error, so it's up to
1423 * vold to restart the system.
1424 */
1425 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Ken Sumrallc290eaf2011-03-07 23:40:35 -08001426 android_reboot(ANDROID_RB_RESTART, 0, 0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001427
1428 /* shouldn't get here */
1429 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07001430 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001431 if (lockid[0]) {
1432 release_wake_lock(lockid);
1433 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001434 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001435}
1436
Jason parks70a4b3f2011-01-28 10:10:47 -06001437int cryptfs_changepw(char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001438{
1439 struct crypt_mnt_ftr crypt_ftr;
Jason parks70a4b3f2011-01-28 10:10:47 -06001440 unsigned char encrypted_master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
Ken Sumralle8744072011-01-18 22:01:55 -08001441 unsigned char salt[SALT_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001442 char real_blkdev[MAXPATHLEN];
1443
1444 /* This is only allowed after we've successfully decrypted the master key */
Jason parks70a4b3f2011-01-28 10:10:47 -06001445 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001446 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001447 return -1;
1448 }
1449
Ken Sumralle5032c42012-04-01 23:58:44 -07001450 fs_mgr_get_crypt_info(get_fstab_filename(), 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001451 if (strlen(real_blkdev) == 0) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001452 SLOGE("Can't find real blkdev");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001453 return -1;
1454 }
1455
1456 /* get key */
Ken Sumralle8744072011-01-18 22:01:55 -08001457 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001458 SLOGE("Error getting crypt footer and key");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001459 return -1;
1460 }
1461
Jason parks70a4b3f2011-01-28 10:10:47 -06001462 encrypt_master_key(newpw, salt, saved_master_key, encrypted_master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001463
Jason parks70a4b3f2011-01-28 10:10:47 -06001464 /* save the key */
1465 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001466
1467 return 0;
1468}