blob: 5b3a504083ab4511d4f20e99f4bb55a86df6c201 [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
48char *me = "cryptfs";
49
Ken Sumrall8ddbe402011-01-17 15:26:29 -080050static unsigned char saved_key_sha1[20] = { '\0' };
51static int key_sha1_saved = 0;
52
Ken Sumrall8f869aa2010-12-03 03:47:09 -080053static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
54{
55 memset(io, 0, dataSize);
56 io->data_size = dataSize;
57 io->data_start = sizeof(struct dm_ioctl);
58 io->version[0] = 4;
59 io->version[1] = 0;
60 io->version[2] = 0;
61 io->flags = flags;
62 if (name) {
63 strncpy(io->name, name, sizeof(io->name));
64 }
65}
66
Ken Sumrall3ed82362011-01-28 23:31:16 -080067static unsigned int get_fs_size(char *dev)
68{
69 int fd, block_size;
70 struct ext4_super_block sb;
71 off64_t len;
72
73 if ((fd = open(dev, O_RDONLY)) < 0) {
74 SLOGE("Cannot open device to get filesystem size ");
75 return 0;
76 }
77
78 if (lseek64(fd, 1024, SEEK_SET) < 0) {
79 SLOGE("Cannot seek to superblock");
80 return 0;
81 }
82
83 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
84 SLOGE("Cannot read superblock");
85 return 0;
86 }
87
88 close(fd);
89
90 block_size = 1024 << sb.s_log_block_size;
91 /* compute length in bytes */
92 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
93
94 /* return length in sectors */
95 return (unsigned int) (len / 512);
96}
97
Ken Sumrall8f869aa2010-12-03 03:47:09 -080098static unsigned int get_blkdev_size(int fd)
99{
100 unsigned int nr_sec;
101
102 if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
103 nr_sec = 0;
104 }
105
106 return nr_sec;
107}
108
Ken Sumralle8744072011-01-18 22:01:55 -0800109/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800110 * update the failed mount count but not change the key.
111 */
112static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
Ken Sumralle8744072011-01-18 22:01:55 -0800113 unsigned char *key, unsigned char *salt)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800114{
115 int fd;
116 unsigned int nr_sec, cnt;
117 off64_t off;
118 int rc = -1;
119
120 if ( (fd = open(real_blk_name, O_RDWR)) < 0) {
121 SLOGE("Cannot open real block device %s\n", real_blk_name);
122 return -1;
123 }
124
125 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
126 SLOGE("Cannot get size of block device %s\n", real_blk_name);
127 goto errout;
128 }
129
130 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
131 * encryption info footer and key, and plenty of bytes to spare for future
132 * growth.
133 */
134 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
135
136 if (lseek64(fd, off, SEEK_SET) == -1) {
137 SLOGE("Cannot seek to real block device footer\n");
138 goto errout;
139 }
140
141 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
142 SLOGE("Cannot write real block device footer\n");
143 goto errout;
144 }
145
146 if (key) {
147 if (crypt_ftr->keysize != 16) {
148 SLOGE("Keysize of %d bits not supported for real block device %s\n",
149 crypt_ftr->keysize * 8, real_blk_name);
150 goto errout;
151 }
152
153 if ( (cnt = write(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
154 SLOGE("Cannot write key for real block device %s\n", real_blk_name);
155 goto errout;
156 }
157 }
158
Ken Sumralle8744072011-01-18 22:01:55 -0800159 if (salt) {
160 /* Compute the offset for start of the crypt footer */
161 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
162 /* Add in the length of the footer, key and padding */
163 off += sizeof(struct crypt_mnt_ftr) + crypt_ftr->keysize + KEY_TO_SALT_PADDING;
164
165 if (lseek64(fd, off, SEEK_SET) == -1) {
166 SLOGE("Cannot seek to real block device salt \n");
167 goto errout;
168 }
169
170 if ( (cnt = write(fd, salt, SALT_LEN)) != SALT_LEN) {
171 SLOGE("Cannot write salt for real block device %s\n", real_blk_name);
172 goto errout;
173 }
174 }
175
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800176 /* Success! */
177 rc = 0;
178
179errout:
180 close(fd);
181 return rc;
182
183}
184
185static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
Ken Sumralle8744072011-01-18 22:01:55 -0800186 unsigned char *key, unsigned char *salt)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800187{
188 int fd;
189 unsigned int nr_sec, cnt;
190 off64_t off;
191 int rc = -1;
192
193 if ( (fd = open(real_blk_name, O_RDWR)) < 0) {
194 SLOGE("Cannot open real block device %s\n", real_blk_name);
195 return -1;
196 }
197
198 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
199 SLOGE("Cannot get size of block device %s\n", real_blk_name);
200 goto errout;
201 }
202
203 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
204 * encryption info footer and key, and plenty of bytes to spare for future
205 * growth.
206 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800207 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800208
209 if (lseek64(fd, off, SEEK_SET) == -1) {
210 SLOGE("Cannot seek to real block device footer\n");
211 goto errout;
212 }
213
214 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
215 SLOGE("Cannot read real block device footer\n");
216 goto errout;
217 }
218
219 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
220 SLOGE("Bad magic for real block device %s\n", real_blk_name);
221 goto errout;
222 }
223
224 if (crypt_ftr->major_version != 1) {
225 SLOGE("Cannot understand major version %d real block device footer\n",
226 crypt_ftr->major_version);
227 goto errout;
228 }
229
230 if (crypt_ftr->minor_version != 0) {
231 SLOGW("Warning: crypto footer minor version %d, expected 0, continuing...\n",
232 crypt_ftr->minor_version);
233 }
234
235 if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) {
236 /* the footer size is bigger than we expected.
237 * Skip to it's stated end so we can read the key.
238 */
239 if (lseek(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) {
240 SLOGE("Cannot seek to start of key\n");
241 goto errout;
242 }
243 }
244
245 if (crypt_ftr->keysize != 16) {
246 SLOGE("Keysize of %d bits not supported for real block device %s\n",
247 crypt_ftr->keysize * 8, real_blk_name);
248 goto errout;
249 }
250
251 if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
252 SLOGE("Cannot read key for real block device %s\n", real_blk_name);
253 goto errout;
254 }
255
Ken Sumralle8744072011-01-18 22:01:55 -0800256 if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) {
257 SLOGE("Cannot seek to real block device salt\n");
258 goto errout;
259 }
260
261 if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) {
262 SLOGE("Cannot read salt for real block device %s\n", real_blk_name);
263 goto errout;
264 }
265
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800266 /* Success! */
267 rc = 0;
268
269errout:
270 close(fd);
271 return rc;
272}
273
274/* Convert a binary key of specified length into an ascii hex string equivalent,
275 * without the leading 0x and with null termination
276 */
277void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
278 char *master_key_ascii)
279{
280 unsigned int i, a;
281 unsigned char nibble;
282
283 for (i=0, a=0; i<keysize; i++, a+=2) {
284 /* For each byte, write out two ascii hex digits */
285 nibble = (master_key[i] >> 4) & 0xf;
286 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
287
288 nibble = master_key[i] & 0xf;
289 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
290 }
291
292 /* Add the null termination */
293 master_key_ascii[a] = '\0';
294
295}
296
297static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
298 char *real_blk_name, char *crypto_blk_name)
299{
300 char buffer[DM_CRYPT_BUF_SIZE];
301 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
302 char *crypt_params;
303 struct dm_ioctl *io;
304 struct dm_target_spec *tgt;
305 unsigned int minor;
306 int fd;
307 int retval = -1;
308 char *name ="datadev"; /* FIX ME: Make me a parameter */
309
310 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
311 SLOGE("Cannot open device-mapper\n");
312 goto errout;
313 }
314
315 io = (struct dm_ioctl *) buffer;
316
317 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
318 if (ioctl(fd, DM_DEV_CREATE, io)) {
319 SLOGE("Cannot create dm-crypt device\n");
320 goto errout;
321 }
322
323 /* Get the device status, in particular, the name of it's device file */
324 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
325 if (ioctl(fd, DM_DEV_STATUS, io)) {
326 SLOGE("Cannot retrieve dm-crypt device status\n");
327 goto errout;
328 }
329 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
330 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
331
332 /* Load the mapping table for this device */
333 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
334
335 ioctl_init(io, 4096, name, 0);
336 io->target_count = 1;
337 tgt->status = 0;
338 tgt->sector_start = 0;
339 tgt->length = crypt_ftr->fs_size;
340 strcpy(tgt->target_type, "crypt");
341
342 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
343 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
344 sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name,
345 master_key_ascii, real_blk_name);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800346 crypt_params += strlen(crypt_params) + 1;
347 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
348 tgt->next = crypt_params - buffer;
349
350 if (ioctl(fd, DM_TABLE_LOAD, io)) {
351 SLOGE("Cannot load dm-crypt mapping table.\n");
352 goto errout;
353 }
354
355 /* Resume this device to activate it */
356 ioctl_init(io, 4096, name, 0);
357
358 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
359 SLOGE("Cannot resume the dm-crypt device\n");
360 goto errout;
361 }
362
363 /* We made it here with no errors. Woot! */
364 retval = 0;
365
366errout:
367 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
368
369 return retval;
370}
371
372static int delete_crypto_blk_dev(char *crypto_blkdev)
373{
374 int fd;
375 char buffer[DM_CRYPT_BUF_SIZE];
376 struct dm_ioctl *io;
377 char *name ="datadev"; /* FIX ME: Make me a paraameter */
378 int retval = -1;
379
380 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
381 SLOGE("Cannot open device-mapper\n");
382 goto errout;
383 }
384
385 io = (struct dm_ioctl *) buffer;
386
387 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
388 if (ioctl(fd, DM_DEV_REMOVE, io)) {
389 SLOGE("Cannot remove dm-crypt device\n");
390 goto errout;
391 }
392
393 /* We made it here with no errors. Woot! */
394 retval = 0;
395
396errout:
397 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
398
399 return retval;
400
401}
402
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800403#define HASH_COUNT 2000
404#define KEY_LEN_BYTES 16
405#define IV_LEN_BYTES 16
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 */
589 if (! key_sha1_saved) {
590 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, "");
674 if ( key_sha1_saved || strcmp(encrypted_state, "encrypted") ) {
675 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);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800735 /* Also save a SHA1 of the master key so we can know if we
736 * successfully decrypted the key when we want to change the
737 * password on it.
738 */
739 SHA1(decrypted_master_key, KEY_LEN_BYTES, saved_key_sha1);
740 key_sha1_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;
768 ftr->keysize = 16;
769 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;
894 unsigned char master_key[16], decrypted_master_key[16];
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
1051int cryptfs_changepw(char *oldpw, char *newpw)
1052{
1053 struct crypt_mnt_ftr crypt_ftr;
1054 unsigned char encrypted_master_key[32], decrypted_master_key[32];
Ken Sumralle8744072011-01-18 22:01:55 -08001055 unsigned char salt[SALT_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001056 unsigned char new_key_sha1[20];
1057 char real_blkdev[MAXPATHLEN];
1058
1059 /* This is only allowed after we've successfully decrypted the master key */
1060 if (! key_sha1_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001061 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001062 return -1;
1063 }
1064
1065 property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
1066 if (strlen(real_blkdev) == 0) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001067 SLOGE("Can't find real blkdev");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001068 return -1;
1069 }
1070
1071 /* get key */
Ken Sumralle8744072011-01-18 22:01:55 -08001072 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001073 SLOGE("Error getting crypt footer and key");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001074 return -1;
1075 }
1076
1077 /* decrypt key with old passwd */
Ken Sumralle8744072011-01-18 22:01:55 -08001078 decrypt_master_key(oldpw, salt, encrypted_master_key, decrypted_master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001079
1080 /* compute sha1 of decrypted key */
1081 SHA1(decrypted_master_key, KEY_LEN_BYTES, new_key_sha1);
1082
1083 /* If computed sha1 and saved sha1 match, encrypt key with new passwd */
1084 if (! memcmp(saved_key_sha1, new_key_sha1, sizeof(saved_key_sha1))) {
1085 /* they match, it's safe to re-encrypt the key */
Ken Sumralle8744072011-01-18 22:01:55 -08001086 encrypt_master_key(newpw, salt, decrypted_master_key, encrypted_master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001087
1088 /* save the key */
Ken Sumralle8744072011-01-18 22:01:55 -08001089 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, 0);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001090 } else {
1091 SLOGE("SHA1 mismatch");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001092 return -1;
1093 }
1094
1095 return 0;
1096}
1097