blob: afba72681525dc2f156cd93c092e4dd9727f06c7 [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>
38#include <sys/reboot.h>
Ken Sumrall3ed82362011-01-28 23:31:16 -080039#include <ext4.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080040#include "cryptfs.h"
41#define LOG_TAG "Cryptfs"
42#include "cutils/log.h"
43#include "cutils/properties.h"
44
45#define DM_CRYPT_BUF_SIZE 4096
Ken Sumrall8ddbe402011-01-17 15:26:29 -080046#define DATA_MNT_POINT "/data"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080047
Jason parks70a4b3f2011-01-28 10:10:47 -060048#define HASH_COUNT 2000
49#define KEY_LEN_BYTES 16
50#define IV_LEN_BYTES 16
51
Ken Sumrall8f869aa2010-12-03 03:47:09 -080052char *me = "cryptfs";
53
Jason parks70a4b3f2011-01-28 10:10:47 -060054static unsigned char saved_master_key[KEY_LEN_BYTES];
55static int master_key_saved = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -080056
Ken Sumrall8f869aa2010-12-03 03:47:09 -080057static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
58{
59 memset(io, 0, dataSize);
60 io->data_size = dataSize;
61 io->data_start = sizeof(struct dm_ioctl);
62 io->version[0] = 4;
63 io->version[1] = 0;
64 io->version[2] = 0;
65 io->flags = flags;
66 if (name) {
67 strncpy(io->name, name, sizeof(io->name));
68 }
69}
70
Ken Sumrall3ed82362011-01-28 23:31:16 -080071static unsigned int get_fs_size(char *dev)
72{
73 int fd, block_size;
74 struct ext4_super_block sb;
75 off64_t len;
76
77 if ((fd = open(dev, O_RDONLY)) < 0) {
78 SLOGE("Cannot open device to get filesystem size ");
79 return 0;
80 }
81
82 if (lseek64(fd, 1024, SEEK_SET) < 0) {
83 SLOGE("Cannot seek to superblock");
84 return 0;
85 }
86
87 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
88 SLOGE("Cannot read superblock");
89 return 0;
90 }
91
92 close(fd);
93
94 block_size = 1024 << sb.s_log_block_size;
95 /* compute length in bytes */
96 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
97
98 /* return length in sectors */
99 return (unsigned int) (len / 512);
100}
101
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800102static unsigned int get_blkdev_size(int fd)
103{
104 unsigned int nr_sec;
105
106 if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
107 nr_sec = 0;
108 }
109
110 return nr_sec;
111}
112
Ken Sumralle8744072011-01-18 22:01:55 -0800113/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800114 * update the failed mount count but not change the key.
115 */
116static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
Ken Sumralle8744072011-01-18 22:01:55 -0800117 unsigned char *key, unsigned char *salt)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800118{
119 int fd;
120 unsigned int nr_sec, cnt;
121 off64_t off;
122 int rc = -1;
123
124 if ( (fd = open(real_blk_name, O_RDWR)) < 0) {
125 SLOGE("Cannot open real block device %s\n", real_blk_name);
126 return -1;
127 }
128
129 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
130 SLOGE("Cannot get size of block device %s\n", real_blk_name);
131 goto errout;
132 }
133
134 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
135 * encryption info footer and key, and plenty of bytes to spare for future
136 * growth.
137 */
138 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
139
140 if (lseek64(fd, off, SEEK_SET) == -1) {
141 SLOGE("Cannot seek to real block device footer\n");
142 goto errout;
143 }
144
145 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
146 SLOGE("Cannot write real block device footer\n");
147 goto errout;
148 }
149
150 if (key) {
Jason parks70a4b3f2011-01-28 10:10:47 -0600151 if (crypt_ftr->keysize != KEY_LEN_BYTES) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800152 SLOGE("Keysize of %d bits not supported for real block device %s\n",
153 crypt_ftr->keysize * 8, real_blk_name);
154 goto errout;
155 }
156
157 if ( (cnt = write(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
158 SLOGE("Cannot write key for real block device %s\n", real_blk_name);
159 goto errout;
160 }
161 }
162
Ken Sumralle8744072011-01-18 22:01:55 -0800163 if (salt) {
164 /* Compute the offset for start of the crypt footer */
165 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
166 /* Add in the length of the footer, key and padding */
167 off += sizeof(struct crypt_mnt_ftr) + crypt_ftr->keysize + KEY_TO_SALT_PADDING;
168
169 if (lseek64(fd, off, SEEK_SET) == -1) {
170 SLOGE("Cannot seek to real block device salt \n");
171 goto errout;
172 }
173
174 if ( (cnt = write(fd, salt, SALT_LEN)) != SALT_LEN) {
175 SLOGE("Cannot write salt for real block device %s\n", real_blk_name);
176 goto errout;
177 }
178 }
179
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800180 /* Success! */
181 rc = 0;
182
183errout:
184 close(fd);
185 return rc;
186
187}
188
189static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
Ken Sumralle8744072011-01-18 22:01:55 -0800190 unsigned char *key, unsigned char *salt)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800191{
192 int fd;
193 unsigned int nr_sec, cnt;
194 off64_t off;
195 int rc = -1;
196
197 if ( (fd = open(real_blk_name, O_RDWR)) < 0) {
198 SLOGE("Cannot open real block device %s\n", real_blk_name);
199 return -1;
200 }
201
202 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
203 SLOGE("Cannot get size of block device %s\n", real_blk_name);
204 goto errout;
205 }
206
207 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
208 * encryption info footer and key, and plenty of bytes to spare for future
209 * growth.
210 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800211 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800212
213 if (lseek64(fd, off, SEEK_SET) == -1) {
214 SLOGE("Cannot seek to real block device footer\n");
215 goto errout;
216 }
217
218 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
219 SLOGE("Cannot read real block device footer\n");
220 goto errout;
221 }
222
223 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
224 SLOGE("Bad magic for real block device %s\n", real_blk_name);
225 goto errout;
226 }
227
228 if (crypt_ftr->major_version != 1) {
229 SLOGE("Cannot understand major version %d real block device footer\n",
230 crypt_ftr->major_version);
231 goto errout;
232 }
233
234 if (crypt_ftr->minor_version != 0) {
235 SLOGW("Warning: crypto footer minor version %d, expected 0, continuing...\n",
236 crypt_ftr->minor_version);
237 }
238
239 if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) {
240 /* the footer size is bigger than we expected.
241 * Skip to it's stated end so we can read the key.
242 */
243 if (lseek(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) {
244 SLOGE("Cannot seek to start of key\n");
245 goto errout;
246 }
247 }
248
Jason parks70a4b3f2011-01-28 10:10:47 -0600249 if (crypt_ftr->keysize != KEY_LEN_BYTES) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800250 SLOGE("Keysize of %d bits not supported for real block device %s\n",
251 crypt_ftr->keysize * 8, real_blk_name);
252 goto errout;
253 }
254
255 if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
256 SLOGE("Cannot read key for real block device %s\n", real_blk_name);
257 goto errout;
258 }
259
Ken Sumralle8744072011-01-18 22:01:55 -0800260 if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) {
261 SLOGE("Cannot seek to real block device salt\n");
262 goto errout;
263 }
264
265 if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) {
266 SLOGE("Cannot read salt for real block device %s\n", real_blk_name);
267 goto errout;
268 }
269
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800270 /* Success! */
271 rc = 0;
272
273errout:
274 close(fd);
275 return rc;
276}
277
278/* Convert a binary key of specified length into an ascii hex string equivalent,
279 * without the leading 0x and with null termination
280 */
281void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
282 char *master_key_ascii)
283{
284 unsigned int i, a;
285 unsigned char nibble;
286
287 for (i=0, a=0; i<keysize; i++, a+=2) {
288 /* For each byte, write out two ascii hex digits */
289 nibble = (master_key[i] >> 4) & 0xf;
290 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
291
292 nibble = master_key[i] & 0xf;
293 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
294 }
295
296 /* Add the null termination */
297 master_key_ascii[a] = '\0';
298
299}
300
301static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
302 char *real_blk_name, char *crypto_blk_name)
303{
304 char buffer[DM_CRYPT_BUF_SIZE];
305 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
306 char *crypt_params;
307 struct dm_ioctl *io;
308 struct dm_target_spec *tgt;
309 unsigned int minor;
310 int fd;
311 int retval = -1;
312 char *name ="datadev"; /* FIX ME: Make me a parameter */
313
314 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
315 SLOGE("Cannot open device-mapper\n");
316 goto errout;
317 }
318
319 io = (struct dm_ioctl *) buffer;
320
321 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
322 if (ioctl(fd, DM_DEV_CREATE, io)) {
323 SLOGE("Cannot create dm-crypt device\n");
324 goto errout;
325 }
326
327 /* Get the device status, in particular, the name of it's device file */
328 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
329 if (ioctl(fd, DM_DEV_STATUS, io)) {
330 SLOGE("Cannot retrieve dm-crypt device status\n");
331 goto errout;
332 }
333 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
334 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
335
336 /* Load the mapping table for this device */
337 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
338
339 ioctl_init(io, 4096, name, 0);
340 io->target_count = 1;
341 tgt->status = 0;
342 tgt->sector_start = 0;
343 tgt->length = crypt_ftr->fs_size;
344 strcpy(tgt->target_type, "crypt");
345
346 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
347 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
348 sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name,
349 master_key_ascii, real_blk_name);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800350 crypt_params += strlen(crypt_params) + 1;
351 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
352 tgt->next = crypt_params - buffer;
353
354 if (ioctl(fd, DM_TABLE_LOAD, io)) {
355 SLOGE("Cannot load dm-crypt mapping table.\n");
356 goto errout;
357 }
358
359 /* Resume this device to activate it */
360 ioctl_init(io, 4096, name, 0);
361
362 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
363 SLOGE("Cannot resume the dm-crypt device\n");
364 goto errout;
365 }
366
367 /* We made it here with no errors. Woot! */
368 retval = 0;
369
370errout:
371 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
372
373 return retval;
374}
375
376static int delete_crypto_blk_dev(char *crypto_blkdev)
377{
378 int fd;
379 char buffer[DM_CRYPT_BUF_SIZE];
380 struct dm_ioctl *io;
381 char *name ="datadev"; /* FIX ME: Make me a paraameter */
382 int retval = -1;
383
384 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
385 SLOGE("Cannot open device-mapper\n");
386 goto errout;
387 }
388
389 io = (struct dm_ioctl *) buffer;
390
391 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
392 if (ioctl(fd, DM_DEV_REMOVE, io)) {
393 SLOGE("Cannot remove dm-crypt device\n");
394 goto errout;
395 }
396
397 /* We made it here with no errors. Woot! */
398 retval = 0;
399
400errout:
401 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
402
403 return retval;
404
405}
406
Ken Sumralle8744072011-01-18 22:01:55 -0800407static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800408{
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800409 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800410 PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800411 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800412}
413
Ken Sumralle8744072011-01-18 22:01:55 -0800414static int encrypt_master_key(char *passwd, unsigned char *salt,
415 unsigned char *decrypted_master_key,
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800416 unsigned char *encrypted_master_key)
417{
418 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
419 EVP_CIPHER_CTX e_ctx;
420 int encrypted_len, final_len;
421
422 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800423 pbkdf2(passwd, salt, ikey);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800424
425 /* Initialize the decryption engine */
426 if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
427 SLOGE("EVP_EncryptInit failed\n");
428 return -1;
429 }
430 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800431
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800432 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800433 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
434 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800435 SLOGE("EVP_EncryptUpdate failed\n");
436 return -1;
437 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800438 if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800439 SLOGE("EVP_EncryptFinal failed\n");
440 return -1;
441 }
442
443 if (encrypted_len + final_len != KEY_LEN_BYTES) {
444 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
445 return -1;
446 } else {
447 return 0;
448 }
449}
450
Ken Sumralle8744072011-01-18 22:01:55 -0800451static int decrypt_master_key(char *passwd, unsigned char *salt,
452 unsigned char *encrypted_master_key,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800453 unsigned char *decrypted_master_key)
454{
455 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 -0800456 EVP_CIPHER_CTX d_ctx;
457 int decrypted_len, final_len;
458
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800459 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800460 pbkdf2(passwd, salt, ikey);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800461
462 /* Initialize the decryption engine */
463 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
464 return -1;
465 }
466 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
467 /* Decrypt the master key */
468 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
469 encrypted_master_key, KEY_LEN_BYTES)) {
470 return -1;
471 }
472 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
473 return -1;
474 }
475
476 if (decrypted_len + final_len != KEY_LEN_BYTES) {
477 return -1;
478 } else {
479 return 0;
480 }
481}
482
Ken Sumralle8744072011-01-18 22:01:55 -0800483static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt)
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800484{
485 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -0800486 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800487 EVP_CIPHER_CTX e_ctx;
488 int encrypted_len, final_len;
489
490 /* Get some random bits for a key */
491 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -0800492 read(fd, key_buf, sizeof(key_buf));
493 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800494 close(fd);
495
496 /* Now encrypt it with the password */
Ken Sumralle8744072011-01-18 22:01:55 -0800497 return encrypt_master_key(passwd, salt, key_buf, master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800498}
499
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800500static int get_orig_mount_parms(char *mount_point, char *fs_type, char *real_blkdev,
501 unsigned long *mnt_flags, char *fs_options)
502{
503 char mount_point2[32];
504 char fs_flags[32];
505
506 property_get("ro.crypto.fs_type", fs_type, "");
507 property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
508 property_get("ro.crypto.fs_mnt_point", mount_point2, "");
509 property_get("ro.crypto.fs_options", fs_options, "");
510 property_get("ro.crypto.fs_flags", fs_flags, "");
511 *mnt_flags = strtol(fs_flags, 0, 0);
512
513 if (strcmp(mount_point, mount_point2)) {
514 /* Consistency check. These should match. If not, something odd happened. */
515 return -1;
516 }
517
518 return 0;
519}
520
521static int wait_and_unmount(char *mountpoint)
522{
523 int i, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -0800524#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800525
526 /* Now umount the tmpfs filesystem */
527 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
528 if (umount(mountpoint)) {
529 sleep(1);
530 i++;
531 } else {
532 break;
533 }
534 }
535
536 if (i < WAIT_UNMOUNT_COUNT) {
537 SLOGD("unmounting %s succeeded\n", mountpoint);
538 rc = 0;
539 } else {
540 SLOGE("unmounting %s failed\n", mountpoint);
541 rc = -1;
542 }
543
544 return rc;
545}
546
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800547#define DATA_PREP_TIMEOUT 100
548static int prep_data_fs(void)
549{
550 int i;
551
552 /* Do the prep of the /data filesystem */
553 property_set("vold.post_fs_data_done", "0");
554 property_set("vold.decrypt", "trigger_post_fs_data");
555 SLOGD("Just triggered post_fs_data\n");
556
557 /* Wait a max of 25 seconds, hopefully it takes much less */
558 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
559 char p[16];;
560
561 property_get("vold.post_fs_data_done", p, "0");
562 if (*p == '1') {
563 break;
564 } else {
565 usleep(250000);
566 }
567 }
568 if (i == DATA_PREP_TIMEOUT) {
569 /* Ugh, we failed to prep /data in time. Bail. */
570 return -1;
571 } else {
572 SLOGD("post_fs_data done\n");
573 return 0;
574 }
575}
576
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800577int cryptfs_restart(void)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800578{
579 char fs_type[32];
580 char real_blkdev[MAXPATHLEN];
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800581 char crypto_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800582 char fs_options[256];
583 unsigned long mnt_flags;
584 struct stat statbuf;
585 int rc = -1, i;
Ken Sumrall0cc16632011-01-18 20:32:26 -0800586 static int restart_successful = 0;
587
588 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -0600589 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -0800590 SLOGE("Encrypted filesystem not validated, aborting");
591 return -1;
592 }
593
594 if (restart_successful) {
595 SLOGE("System already restarted with encrypted disk, aborting");
596 return -1;
597 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800598
599 /* Here is where we shut down the framework. The init scripts
600 * start all services in one of three classes: core, main or late_start.
601 * On boot, we start core and main. Now, we stop main, but not core,
602 * as core includes vold and a few other really important things that
603 * we need to keep running. Once main has stopped, we should be able
604 * to umount the tmpfs /data, then mount the encrypted /data.
605 * We then restart the class main, and also the class late_start.
606 * At the moment, I've only put a few things in late_start that I know
607 * are not needed to bring up the framework, and that also cause problems
608 * with unmounting the tmpfs /data, but I hope to add add more services
609 * to the late_start class as we optimize this to decrease the delay
610 * till the user is asked for the password to the filesystem.
611 */
612
613 /* The init files are setup to stop the class main when vold.decrypt is
614 * set to trigger_reset_main.
615 */
616 property_set("vold.decrypt", "trigger_reset_main");
617 SLOGD("Just asked init to shut down class main\n");
618
619 /* Now that the framework is shutdown, we should be able to umount()
620 * the tmpfs filesystem, and mount the real one.
621 */
622
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800623 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
624 if (strlen(crypto_blkdev) == 0) {
625 SLOGE("fs_crypto_blkdev not set\n");
626 return -1;
627 }
628
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800629 if (! get_orig_mount_parms(DATA_MNT_POINT, fs_type, real_blkdev, &mnt_flags, fs_options)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800630 SLOGD("Just got orig mount parms\n");
631
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800632 if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800633 /* If that succeeded, then mount the decrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800634 mount(crypto_blkdev, DATA_MNT_POINT, fs_type, mnt_flags, fs_options);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800635
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800636 /* Create necessary paths on /data */
637 if (prep_data_fs()) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800638 return -1;
639 }
640
641 /* startup service classes main and late_start */
642 property_set("vold.decrypt", "trigger_restart_framework");
643 SLOGD("Just triggered restart_framework\n");
644
645 /* Give it a few moments to get started */
646 sleep(1);
647 }
648 }
649
Ken Sumrall0cc16632011-01-18 20:32:26 -0800650 if (rc == 0) {
651 restart_successful = 1;
652 }
653
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800654 return rc;
655}
656
657static int test_mount_encrypted_fs(char *passwd, char *mount_point)
658{
659 struct crypt_mnt_ftr crypt_ftr;
660 /* Allocate enough space for a 256 bit key, but we may use less */
661 unsigned char encrypted_master_key[32], decrypted_master_key[32];
Ken Sumralle8744072011-01-18 22:01:55 -0800662 unsigned char salt[SALT_LEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800663 char crypto_blkdev[MAXPATHLEN];
664 char real_blkdev[MAXPATHLEN];
665 char fs_type[32];
666 char fs_options[256];
667 char tmp_mount_point[64];
668 unsigned long mnt_flags;
669 unsigned int orig_failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -0800670 char encrypted_state[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800671 int rc;
672
Ken Sumrall0cc16632011-01-18 20:32:26 -0800673 property_get("ro.crypto.state", encrypted_state, "");
Jason parks70a4b3f2011-01-28 10:10:47 -0600674 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
Ken Sumrall0cc16632011-01-18 20:32:26 -0800675 SLOGE("encrypted fs already validated or not running with encryption, aborting");
676 return -1;
677 }
678
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800679 if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
680 SLOGE("Error reading original mount parms for mount point %s\n", mount_point);
681 return -1;
682 }
683
Ken Sumralle8744072011-01-18 22:01:55 -0800684 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800685 SLOGE("Error getting crypt footer and key\n");
686 return -1;
687 }
688 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size);
689 orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count;
690
691 if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Ken Sumralle8744072011-01-18 22:01:55 -0800692 decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800693 }
694
695 if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key,
696 real_blkdev, crypto_blkdev)) {
697 SLOGE("Error creating decrypted block device\n");
698 return -1;
699 }
700
701 /* If init detects an encrypted filesystme, it writes a file for each such
702 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
703 * files and passes that data to me */
704 /* Create a tmp mount point to try mounting the decryptd fs
705 * Since we're here, the mount_point should be a tmpfs filesystem, so make
706 * a directory in it to test mount the decrypted filesystem.
707 */
708 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
709 mkdir(tmp_mount_point, 0755);
710 if ( mount(crypto_blkdev, tmp_mount_point, "ext4", MS_RDONLY, "") ) {
711 SLOGE("Error temp mounting decrypted block device\n");
712 delete_crypto_blk_dev(crypto_blkdev);
713 crypt_ftr.failed_decrypt_count++;
714 } else {
715 /* Success, so just umount and we'll mount it properly when we restart
716 * the framework.
717 */
718 umount(tmp_mount_point);
719 crypt_ftr.failed_decrypt_count = 0;
720 }
721
722 if (orig_failed_decrypt_count != crypt_ftr.failed_decrypt_count) {
Ken Sumralle8744072011-01-18 22:01:55 -0800723 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800724 }
725
726 if (crypt_ftr.failed_decrypt_count) {
727 /* We failed to mount the device, so return an error */
728 rc = crypt_ftr.failed_decrypt_count;
729
730 } else {
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800731 /* Woot! Success! Save the name of the crypto block device
732 * so we can mount it when restarting the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800733 */
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800734 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -0600735
736 /* Also save a the master key so we can reencrypted the key
737 * the key when we want to change the password on it.
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800738 */
Jason parks70a4b3f2011-01-28 10:10:47 -0600739 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
740 master_key_saved = 1;
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800741 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800742 }
743
744 return rc;
745}
746
747int cryptfs_check_passwd(char *passwd)
748{
749 int rc = -1;
750
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800751 rc = test_mount_encrypted_fs(passwd, DATA_MNT_POINT);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800752
753 return rc;
754}
755
756/* Initialize a crypt_mnt_ftr structure. The keysize is
757 * defaulted to 16 bytes, and the filesystem size to 0.
758 * Presumably, at a minimum, the caller will update the
759 * filesystem size and crypto_type_name after calling this function.
760 */
761static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
762{
763 ftr->magic = CRYPT_MNT_MAGIC;
764 ftr->major_version = 1;
765 ftr->minor_version = 0;
766 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
767 ftr->flags = 0;
Jason parks70a4b3f2011-01-28 10:10:47 -0600768 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800769 ftr->spare1 = 0;
770 ftr->fs_size = 0;
771 ftr->failed_decrypt_count = 0;
772 ftr->crypto_type_name[0] = '\0';
773}
774
775static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size)
776{
777 char cmdline[256];
778 int rc = -1;
779
780 snprintf(cmdline, sizeof(cmdline), "/system/bin/make_ext4fs -a /data -l %lld %s",
781 size * 512, crypto_blkdev);
782 SLOGI("Making empty filesystem with command %s\n", cmdline);
783 if (system(cmdline)) {
784 SLOGE("Error creating empty filesystem on %s\n", crypto_blkdev);
785 } else {
786 SLOGD("Successfully created empty filesystem on %s\n", crypto_blkdev);
787 rc = 0;
788 }
789
790 return rc;
791}
792
793static inline int unix_read(int fd, void* buff, int len)
794{
795 int ret;
796 do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR);
797 return ret;
798}
799
800static inline int unix_write(int fd, const void* buff, int len)
801{
802 int ret;
803 do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR);
804 return ret;
805}
806
807#define CRYPT_INPLACE_BUFSIZE 4096
808#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512)
809static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, off64_t size)
810{
811 int realfd, cryptofd;
812 char *buf[CRYPT_INPLACE_BUFSIZE];
813 int rc = -1;
814 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800815 off64_t one_pct, cur_pct, new_pct;
816
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800817 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
818 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
819 return -1;
820 }
821
822 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
823 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
824 close(realfd);
825 return -1;
826 }
827
828 /* This is pretty much a simple loop of reading 4K, and writing 4K.
829 * The size passed in is the number of 512 byte sectors in the filesystem.
830 * So compute the number of whole 4K blocks we should read/write,
831 * and the remainder.
832 */
833 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
834 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
835
836 SLOGE("Encrypting filesystem in place...");
837
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800838 one_pct = numblocks / 100;
839 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800840 /* process the majority of the filesystem in blocks */
841 for (i=0; i<numblocks; i++) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800842 new_pct = i / one_pct;
843 if (new_pct > cur_pct) {
844 char buf[8];
845
846 cur_pct = new_pct;
847 snprintf(buf, sizeof(buf), "%lld", cur_pct);
848 property_set("vold.encrypt_progress", buf);
849 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800850 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
851 SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev);
852 goto errout;
853 }
854 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
855 SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
856 goto errout;
857 }
858 }
859
860 /* Do any remaining sectors */
861 for (i=0; i<remainder; i++) {
862 if (unix_read(realfd, buf, 512) <= 0) {
863 SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev);
864 goto errout;
865 }
866 if (unix_write(cryptofd, buf, 512) <= 0) {
867 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
868 goto errout;
869 }
870 }
871
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800872 property_set("vold.encrypt_progress", "100");
873
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800874 rc = 0;
875
876errout:
877 close(realfd);
878 close(cryptofd);
879
880 return rc;
881}
882
883#define CRYPTO_ENABLE_WIPE 1
884#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800885
886#define FRAMEWORK_BOOT_WAIT 60
887
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800888int cryptfs_enable(char *howarg, char *passwd)
889{
890 int how = 0;
891 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
892 char fs_type[32], fs_options[256], mount_point[32];
893 unsigned long mnt_flags, nr_sec;
Jason parks70a4b3f2011-01-28 10:10:47 -0600894 unsigned char master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
Ken Sumralle8744072011-01-18 22:01:55 -0800895 unsigned char salt[SALT_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800896 int rc=-1, fd, i;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800897 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800898 char tmpfs_options[80];
Ken Sumrall0cc16632011-01-18 20:32:26 -0800899 char encrypted_state[32];
900
901 property_get("ro.crypto.state", encrypted_state, "");
902 if (strcmp(encrypted_state, "unencrypted")) {
903 SLOGE("Device is already running encrypted, aborting");
Ken Sumrall3ed82362011-01-28 23:31:16 -0800904 goto error_unencrypted;
Ken Sumrall0cc16632011-01-18 20:32:26 -0800905 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800906
907 if (!strcmp(howarg, "wipe")) {
908 how = CRYPTO_ENABLE_WIPE;
909 } else if (! strcmp(howarg, "inplace")) {
910 how = CRYPTO_ENABLE_INPLACE;
911 } else {
912 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -0800913 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800914 }
915
916 get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options);
917
Ken Sumrall3ed82362011-01-28 23:31:16 -0800918 /* Get the size of the real block device */
919 fd = open(real_blkdev, O_RDONLY);
920 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
921 SLOGE("Cannot get size of block device %s\n", real_blkdev);
922 goto error_unencrypted;
923 }
924 close(fd);
925
926 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
927 if (how == CRYPTO_ENABLE_INPLACE) {
928 unsigned int fs_size_sec, max_fs_size_sec;
929
930 fs_size_sec = get_fs_size(real_blkdev);
931 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
932
933 if (fs_size_sec > max_fs_size_sec) {
934 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
935 goto error_unencrypted;
936 }
937 }
938
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800939 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800940 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800941 */
942 property_set("vold.decrypt", "trigger_shutdown_framework");
943 SLOGD("Just asked init to shut down class main\n");
944
Ken Sumrall2eaf7132011-01-14 12:45:48 -0800945 if (wait_and_unmount("/mnt/sdcard")) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800946 goto error_shutting_down;
Ken Sumrall2eaf7132011-01-14 12:45:48 -0800947 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800948
949 /* Now unmount the /data partition. */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800950 if (wait_and_unmount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800951 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800952 }
953
954 /* Do extra work for a better UX when doing the long inplace encryption */
955 if (how == CRYPTO_ENABLE_INPLACE) {
956 /* Now that /data is unmounted, we need to mount a tmpfs
957 * /data, set a property saying we're doing inplace encryption,
958 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800959 */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800960 property_get("ro.crypto.tmpfs_options", tmpfs_options, "");
961 if (mount("tmpfs", DATA_MNT_POINT, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV,
962 tmpfs_options) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800963 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800964 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800965 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -0800966 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800967
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800968 /* restart the framework. */
969 /* Create necessary paths on /data */
970 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800971 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800972 }
973
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800974 /* startup service classes main and late_start */
975 property_set("vold.decrypt", "trigger_restart_min_framework");
976 SLOGD("Just triggered restart_min_framework\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800977
Ken Sumrall7df84122011-01-18 14:04:08 -0800978 /* OK, the framework is restarted and will soon be showing a
979 * progress bar. Time to setup an encrypted mapping, and
980 * either write a new filesystem, or encrypt in place updating
981 * the progress bar as we work.
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800982 */
983 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800984
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800985 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800986 /* Initialize a crypt_mnt_ftr for the partition */
987 cryptfs_init_crypt_mnt_ftr(&crypt_ftr);
988 crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
989 strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
990
991 /* Make an encrypted master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800992 if (create_encrypted_random_key(passwd, master_key, salt)) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800993 SLOGE("Cannot create encrypted master key\n");
Ken Sumrall3ed82362011-01-28 23:31:16 -0800994 goto error_unencrypted;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800995 }
996
997 /* Write the key to the end of the partition */
Ken Sumralle8744072011-01-18 22:01:55 -0800998 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, master_key, salt);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800999
Ken Sumralle8744072011-01-18 22:01:55 -08001000 decrypt_master_key(passwd, salt, master_key, decrypted_master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001001 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev);
1002
1003 if (how == CRYPTO_ENABLE_WIPE) {
1004 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr.fs_size);
1005 } else if (how == CRYPTO_ENABLE_INPLACE) {
1006 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001007 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001008 /* Shouldn't happen */
1009 SLOGE("cryptfs_enable: internal error, unknown option\n");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001010 goto error_unencrypted;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001011 }
1012
1013 /* Undo the dm-crypt mapping whether we succeed or not */
1014 delete_crypto_blk_dev(crypto_blkdev);
1015
1016 if (! rc) {
1017 /* Success */
1018 sleep(2); /* Give the UI a change to show 100% progress */
1019 sync();
1020 reboot(LINUX_REBOOT_CMD_RESTART);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001021 } else {
1022 property_set("vold.encrypt_progress", "error_partially_encrypted");
1023 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001024 }
1025
Ken Sumrall3ed82362011-01-28 23:31:16 -08001026 /* hrm, the encrypt step claims success, but the reboot failed.
1027 * This should not happen.
1028 * Set the property and return. Hope the framework can deal with it.
1029 */
1030 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001031 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08001032
1033error_unencrypted:
1034 property_set("vold.encrypt_progress", "error_not_encrypted");
1035 return -1;
1036
1037error_shutting_down:
1038 /* we failed, and have not encrypted anthing, so the users's data is still intact,
1039 * but the framework is stopped and not restarted to show the error, so it's up to
1040 * vold to restart the system.
1041 */
1042 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
1043 sync();
1044 reboot(LINUX_REBOOT_CMD_RESTART);
1045
1046 /* shouldn't get here */
1047 property_set("vold.encrypt_progress", "error_shutting_down");
1048 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001049}
1050
Jason parks70a4b3f2011-01-28 10:10:47 -06001051int cryptfs_changepw(char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001052{
1053 struct crypt_mnt_ftr crypt_ftr;
Jason parks70a4b3f2011-01-28 10:10:47 -06001054 unsigned char encrypted_master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
Ken Sumralle8744072011-01-18 22:01:55 -08001055 unsigned char salt[SALT_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001056 char real_blkdev[MAXPATHLEN];
1057
1058 /* This is only allowed after we've successfully decrypted the master key */
Jason parks70a4b3f2011-01-28 10:10:47 -06001059 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001060 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001061 return -1;
1062 }
1063
1064 property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
1065 if (strlen(real_blkdev) == 0) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001066 SLOGE("Can't find real blkdev");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001067 return -1;
1068 }
1069
1070 /* get key */
Ken Sumralle8744072011-01-18 22:01:55 -08001071 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001072 SLOGE("Error getting crypt footer and key");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001073 return -1;
1074 }
1075
Jason parks70a4b3f2011-01-28 10:10:47 -06001076 encrypt_master_key(newpw, salt, saved_master_key, encrypted_master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001077
Jason parks70a4b3f2011-01-28 10:10:47 -06001078 /* save the key */
1079 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001080
1081 return 0;
1082}