blob: 5bd6de7b9514a3f3ca5a7c2cb291d3fc81dff019 [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 Sumrall8f869aa2010-12-03 03:47:09 -080041#include "cryptfs.h"
42#define LOG_TAG "Cryptfs"
Mike Lockwoodee6d8c42012-02-15 13:43:28 -080043#include "cutils/android_reboot.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080044#include "cutils/log.h"
45#include "cutils/properties.h"
Ken Sumrall5d4c68e2011-01-30 19:06:03 -080046#include "hardware_legacy/power.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070047#include "VolumeManager.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080048
49#define DM_CRYPT_BUF_SIZE 4096
Ken Sumrall8ddbe402011-01-17 15:26:29 -080050#define DATA_MNT_POINT "/data"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080051
Jason parks70a4b3f2011-01-28 10:10:47 -060052#define HASH_COUNT 2000
53#define KEY_LEN_BYTES 16
54#define IV_LEN_BYTES 16
55
Ken Sumrall29d8da82011-05-18 17:20:07 -070056#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
57#define KEY_IN_FOOTER "footer"
58
59#define EXT4_FS 1
60#define FAT_FS 2
61
Ken Sumrall8f869aa2010-12-03 03:47:09 -080062char *me = "cryptfs";
63
Jason parks70a4b3f2011-01-28 10:10:47 -060064static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall29d8da82011-05-18 17:20:07 -070065static char *saved_data_blkdev;
Ken Sumrall3ad90722011-10-04 20:38:29 -070066static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -060067static int master_key_saved = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -080068
Ken Sumrall8f869aa2010-12-03 03:47:09 -080069static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
70{
71 memset(io, 0, dataSize);
72 io->data_size = dataSize;
73 io->data_start = sizeof(struct dm_ioctl);
74 io->version[0] = 4;
75 io->version[1] = 0;
76 io->version[2] = 0;
77 io->flags = flags;
78 if (name) {
79 strncpy(io->name, name, sizeof(io->name));
80 }
81}
82
Ken Sumrall3ed82362011-01-28 23:31:16 -080083static unsigned int get_fs_size(char *dev)
84{
85 int fd, block_size;
86 struct ext4_super_block sb;
87 off64_t len;
88
89 if ((fd = open(dev, O_RDONLY)) < 0) {
90 SLOGE("Cannot open device to get filesystem size ");
91 return 0;
92 }
93
94 if (lseek64(fd, 1024, SEEK_SET) < 0) {
95 SLOGE("Cannot seek to superblock");
96 return 0;
97 }
98
99 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
100 SLOGE("Cannot read superblock");
101 return 0;
102 }
103
104 close(fd);
105
106 block_size = 1024 << sb.s_log_block_size;
107 /* compute length in bytes */
108 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
109
110 /* return length in sectors */
111 return (unsigned int) (len / 512);
112}
113
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800114static unsigned int get_blkdev_size(int fd)
115{
116 unsigned int nr_sec;
117
118 if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
119 nr_sec = 0;
120 }
121
122 return nr_sec;
123}
124
Ken Sumralle8744072011-01-18 22:01:55 -0800125/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800126 * update the failed mount count but not change the key.
127 */
128static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
Ken Sumralle8744072011-01-18 22:01:55 -0800129 unsigned char *key, unsigned char *salt)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800130{
131 int fd;
132 unsigned int nr_sec, cnt;
133 off64_t off;
134 int rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700135 char *fname;
136 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall3be890f2011-09-14 16:53:46 -0700137 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800138
Ken Sumrall29d8da82011-05-18 17:20:07 -0700139 property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800140
Ken Sumrall29d8da82011-05-18 17:20:07 -0700141 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
142 fname = real_blk_name;
143 if ( (fd = open(fname, O_RDWR)) < 0) {
144 SLOGE("Cannot open real block device %s\n", fname);
145 return -1;
146 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800147
Ken Sumrall29d8da82011-05-18 17:20:07 -0700148 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
149 SLOGE("Cannot get size of block device %s\n", fname);
150 goto errout;
151 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800152
Ken Sumrall29d8da82011-05-18 17:20:07 -0700153 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
154 * encryption info footer and key, and plenty of bytes to spare for future
155 * growth.
156 */
157 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
158
159 if (lseek64(fd, off, SEEK_SET) == -1) {
160 SLOGE("Cannot seek to real block device footer\n");
161 goto errout;
162 }
163 } else if (key_loc[0] == '/') {
164 fname = key_loc;
165 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
166 SLOGE("Cannot open footer file %s\n", fname);
167 return -1;
168 }
169 } else {
170 SLOGE("Unexpected value for" KEY_LOC_PROP "\n");
171 return -1;;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800172 }
173
174 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
175 SLOGE("Cannot write real block device footer\n");
176 goto errout;
177 }
178
179 if (key) {
Jason parks70a4b3f2011-01-28 10:10:47 -0600180 if (crypt_ftr->keysize != KEY_LEN_BYTES) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800181 SLOGE("Keysize of %d bits not supported for real block device %s\n",
Ken Sumrall29d8da82011-05-18 17:20:07 -0700182 crypt_ftr->keysize*8, fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800183 goto errout;
184 }
185
186 if ( (cnt = write(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700187 SLOGE("Cannot write key for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800188 goto errout;
189 }
190 }
191
Ken Sumralle8744072011-01-18 22:01:55 -0800192 if (salt) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700193 /* Compute the offset from the last write to the salt */
194 off = KEY_TO_SALT_PADDING;
195 if (! key)
196 off += crypt_ftr->keysize;
Ken Sumralle8744072011-01-18 22:01:55 -0800197
Ken Sumrall29d8da82011-05-18 17:20:07 -0700198 if (lseek64(fd, off, SEEK_CUR) == -1) {
Ken Sumralle8744072011-01-18 22:01:55 -0800199 SLOGE("Cannot seek to real block device salt \n");
200 goto errout;
201 }
202
203 if ( (cnt = write(fd, salt, SALT_LEN)) != SALT_LEN) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700204 SLOGE("Cannot write salt for real block device %s\n", fname);
205 goto errout;
206 }
207 }
208
Ken Sumrall3be890f2011-09-14 16:53:46 -0700209 fstat(fd, &statbuf);
210 /* If the keys are kept on a raw block device, do not try to truncate it. */
211 if (S_ISREG(statbuf.st_mode) && (key_loc[0] == '/')) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700212 if (ftruncate(fd, 0x4000)) {
Ken Sumrall3be890f2011-09-14 16:53:46 -0700213 SLOGE("Cannot set footer file size\n", fname);
Ken Sumralle8744072011-01-18 22:01:55 -0800214 goto errout;
215 }
216 }
217
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800218 /* Success! */
219 rc = 0;
220
221errout:
222 close(fd);
223 return rc;
224
225}
226
227static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
Ken Sumralle8744072011-01-18 22:01:55 -0800228 unsigned char *key, unsigned char *salt)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800229{
230 int fd;
231 unsigned int nr_sec, cnt;
232 off64_t off;
233 int rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700234 char key_loc[PROPERTY_VALUE_MAX];
235 char *fname;
236 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800237
Ken Sumrall29d8da82011-05-18 17:20:07 -0700238 property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800239
Ken Sumrall29d8da82011-05-18 17:20:07 -0700240 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
241 fname = real_blk_name;
242 if ( (fd = open(fname, O_RDONLY)) < 0) {
243 SLOGE("Cannot open real block device %s\n", fname);
244 return -1;
245 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800246
Ken Sumrall29d8da82011-05-18 17:20:07 -0700247 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
248 SLOGE("Cannot get size of block device %s\n", fname);
249 goto errout;
250 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800251
Ken Sumrall29d8da82011-05-18 17:20:07 -0700252 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
253 * encryption info footer and key, and plenty of bytes to spare for future
254 * growth.
255 */
256 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
257
258 if (lseek64(fd, off, SEEK_SET) == -1) {
259 SLOGE("Cannot seek to real block device footer\n");
260 goto errout;
261 }
262 } else if (key_loc[0] == '/') {
263 fname = key_loc;
264 if ( (fd = open(fname, O_RDONLY)) < 0) {
265 SLOGE("Cannot open footer file %s\n", fname);
266 return -1;
267 }
268
269 /* Make sure it's 16 Kbytes in length */
270 fstat(fd, &statbuf);
Ken Sumrall3be890f2011-09-14 16:53:46 -0700271 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700272 SLOGE("footer file %s is not the expected size!\n", fname);
273 goto errout;
274 }
275 } else {
276 SLOGE("Unexpected value for" KEY_LOC_PROP "\n");
277 return -1;;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800278 }
279
280 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
281 SLOGE("Cannot read real block device footer\n");
282 goto errout;
283 }
284
285 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700286 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800287 goto errout;
288 }
289
290 if (crypt_ftr->major_version != 1) {
291 SLOGE("Cannot understand major version %d real block device footer\n",
292 crypt_ftr->major_version);
293 goto errout;
294 }
295
296 if (crypt_ftr->minor_version != 0) {
297 SLOGW("Warning: crypto footer minor version %d, expected 0, continuing...\n",
298 crypt_ftr->minor_version);
299 }
300
301 if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) {
302 /* the footer size is bigger than we expected.
303 * Skip to it's stated end so we can read the key.
304 */
305 if (lseek(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) {
306 SLOGE("Cannot seek to start of key\n");
307 goto errout;
308 }
309 }
310
Jason parks70a4b3f2011-01-28 10:10:47 -0600311 if (crypt_ftr->keysize != KEY_LEN_BYTES) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800312 SLOGE("Keysize of %d bits not supported for real block device %s\n",
Ken Sumrall29d8da82011-05-18 17:20:07 -0700313 crypt_ftr->keysize * 8, fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800314 goto errout;
315 }
316
317 if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700318 SLOGE("Cannot read key for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800319 goto errout;
320 }
321
Ken Sumralle8744072011-01-18 22:01:55 -0800322 if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) {
323 SLOGE("Cannot seek to real block device salt\n");
324 goto errout;
325 }
326
327 if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700328 SLOGE("Cannot read salt for real block device %s\n", fname);
Ken Sumralle8744072011-01-18 22:01:55 -0800329 goto errout;
330 }
331
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800332 /* Success! */
333 rc = 0;
334
335errout:
336 close(fd);
337 return rc;
338}
339
340/* Convert a binary key of specified length into an ascii hex string equivalent,
341 * without the leading 0x and with null termination
342 */
343void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
344 char *master_key_ascii)
345{
346 unsigned int i, a;
347 unsigned char nibble;
348
349 for (i=0, a=0; i<keysize; i++, a+=2) {
350 /* For each byte, write out two ascii hex digits */
351 nibble = (master_key[i] >> 4) & 0xf;
352 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
353
354 nibble = master_key[i] & 0xf;
355 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
356 }
357
358 /* Add the null termination */
359 master_key_ascii[a] = '\0';
360
361}
362
363static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -0700364 char *real_blk_name, char *crypto_blk_name, const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800365{
366 char buffer[DM_CRYPT_BUF_SIZE];
367 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
368 char *crypt_params;
369 struct dm_ioctl *io;
370 struct dm_target_spec *tgt;
371 unsigned int minor;
372 int fd;
373 int retval = -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800374
375 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
376 SLOGE("Cannot open device-mapper\n");
377 goto errout;
378 }
379
380 io = (struct dm_ioctl *) buffer;
381
382 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
383 if (ioctl(fd, DM_DEV_CREATE, io)) {
384 SLOGE("Cannot create dm-crypt device\n");
385 goto errout;
386 }
387
388 /* Get the device status, in particular, the name of it's device file */
389 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
390 if (ioctl(fd, DM_DEV_STATUS, io)) {
391 SLOGE("Cannot retrieve dm-crypt device status\n");
392 goto errout;
393 }
394 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
395 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
396
397 /* Load the mapping table for this device */
398 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
399
400 ioctl_init(io, 4096, name, 0);
401 io->target_count = 1;
402 tgt->status = 0;
403 tgt->sector_start = 0;
404 tgt->length = crypt_ftr->fs_size;
405 strcpy(tgt->target_type, "crypt");
406
407 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
408 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
409 sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name,
410 master_key_ascii, real_blk_name);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800411 crypt_params += strlen(crypt_params) + 1;
412 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
413 tgt->next = crypt_params - buffer;
414
415 if (ioctl(fd, DM_TABLE_LOAD, io)) {
416 SLOGE("Cannot load dm-crypt mapping table.\n");
417 goto errout;
418 }
419
420 /* Resume this device to activate it */
421 ioctl_init(io, 4096, name, 0);
422
423 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
424 SLOGE("Cannot resume the dm-crypt device\n");
425 goto errout;
426 }
427
428 /* We made it here with no errors. Woot! */
429 retval = 0;
430
431errout:
432 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
433
434 return retval;
435}
436
Ken Sumrall29d8da82011-05-18 17:20:07 -0700437static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800438{
439 int fd;
440 char buffer[DM_CRYPT_BUF_SIZE];
441 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800442 int retval = -1;
443
444 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
445 SLOGE("Cannot open device-mapper\n");
446 goto errout;
447 }
448
449 io = (struct dm_ioctl *) buffer;
450
451 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
452 if (ioctl(fd, DM_DEV_REMOVE, io)) {
453 SLOGE("Cannot remove dm-crypt device\n");
454 goto errout;
455 }
456
457 /* We made it here with no errors. Woot! */
458 retval = 0;
459
460errout:
461 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
462
463 return retval;
464
465}
466
Ken Sumralle8744072011-01-18 22:01:55 -0800467static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800468{
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800469 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800470 PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800471 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800472}
473
Ken Sumralle8744072011-01-18 22:01:55 -0800474static int encrypt_master_key(char *passwd, unsigned char *salt,
475 unsigned char *decrypted_master_key,
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800476 unsigned char *encrypted_master_key)
477{
478 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
479 EVP_CIPHER_CTX e_ctx;
480 int encrypted_len, final_len;
481
482 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800483 pbkdf2(passwd, salt, ikey);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800484
485 /* Initialize the decryption engine */
486 if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
487 SLOGE("EVP_EncryptInit failed\n");
488 return -1;
489 }
490 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800491
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800492 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800493 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
494 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800495 SLOGE("EVP_EncryptUpdate failed\n");
496 return -1;
497 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800498 if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800499 SLOGE("EVP_EncryptFinal failed\n");
500 return -1;
501 }
502
503 if (encrypted_len + final_len != KEY_LEN_BYTES) {
504 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
505 return -1;
506 } else {
507 return 0;
508 }
509}
510
Ken Sumralle8744072011-01-18 22:01:55 -0800511static int decrypt_master_key(char *passwd, unsigned char *salt,
512 unsigned char *encrypted_master_key,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800513 unsigned char *decrypted_master_key)
514{
515 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 -0800516 EVP_CIPHER_CTX d_ctx;
517 int decrypted_len, final_len;
518
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800519 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800520 pbkdf2(passwd, salt, ikey);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800521
522 /* Initialize the decryption engine */
523 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
524 return -1;
525 }
526 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
527 /* Decrypt the master key */
528 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
529 encrypted_master_key, KEY_LEN_BYTES)) {
530 return -1;
531 }
532 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
533 return -1;
534 }
535
536 if (decrypted_len + final_len != KEY_LEN_BYTES) {
537 return -1;
538 } else {
539 return 0;
540 }
541}
542
Ken Sumralle8744072011-01-18 22:01:55 -0800543static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt)
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800544{
545 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -0800546 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800547 EVP_CIPHER_CTX e_ctx;
548 int encrypted_len, final_len;
549
550 /* Get some random bits for a key */
551 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -0800552 read(fd, key_buf, sizeof(key_buf));
553 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800554 close(fd);
555
556 /* Now encrypt it with the password */
Ken Sumralle8744072011-01-18 22:01:55 -0800557 return encrypt_master_key(passwd, salt, key_buf, master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800558}
559
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800560static int get_orig_mount_parms(char *mount_point, char *fs_type, char *real_blkdev,
561 unsigned long *mnt_flags, char *fs_options)
562{
Ken Sumrall29d8da82011-05-18 17:20:07 -0700563 char mount_point2[PROPERTY_VALUE_MAX];
564 char fs_flags[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800565
566 property_get("ro.crypto.fs_type", fs_type, "");
567 property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
568 property_get("ro.crypto.fs_mnt_point", mount_point2, "");
569 property_get("ro.crypto.fs_options", fs_options, "");
570 property_get("ro.crypto.fs_flags", fs_flags, "");
571 *mnt_flags = strtol(fs_flags, 0, 0);
572
573 if (strcmp(mount_point, mount_point2)) {
574 /* Consistency check. These should match. If not, something odd happened. */
575 return -1;
576 }
577
578 return 0;
579}
580
581static int wait_and_unmount(char *mountpoint)
582{
583 int i, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -0800584#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800585
586 /* Now umount the tmpfs filesystem */
587 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
588 if (umount(mountpoint)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700589 if (errno == EINVAL) {
590 /* EINVAL is returned if the directory is not a mountpoint,
591 * i.e. there is no filesystem mounted there. So just get out.
592 */
593 break;
594 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800595 sleep(1);
596 i++;
597 } else {
598 break;
599 }
600 }
601
602 if (i < WAIT_UNMOUNT_COUNT) {
603 SLOGD("unmounting %s succeeded\n", mountpoint);
604 rc = 0;
605 } else {
606 SLOGE("unmounting %s failed\n", mountpoint);
607 rc = -1;
608 }
609
610 return rc;
611}
612
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800613#define DATA_PREP_TIMEOUT 100
614static int prep_data_fs(void)
615{
616 int i;
617
618 /* Do the prep of the /data filesystem */
619 property_set("vold.post_fs_data_done", "0");
620 property_set("vold.decrypt", "trigger_post_fs_data");
621 SLOGD("Just triggered post_fs_data\n");
622
623 /* Wait a max of 25 seconds, hopefully it takes much less */
624 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700625 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800626
627 property_get("vold.post_fs_data_done", p, "0");
628 if (*p == '1') {
629 break;
630 } else {
631 usleep(250000);
632 }
633 }
634 if (i == DATA_PREP_TIMEOUT) {
635 /* Ugh, we failed to prep /data in time. Bail. */
636 return -1;
637 } else {
638 SLOGD("post_fs_data done\n");
639 return 0;
640 }
641}
642
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800643int cryptfs_restart(void)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800644{
645 char fs_type[32];
646 char real_blkdev[MAXPATHLEN];
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800647 char crypto_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800648 char fs_options[256];
649 unsigned long mnt_flags;
650 struct stat statbuf;
651 int rc = -1, i;
Ken Sumrall0cc16632011-01-18 20:32:26 -0800652 static int restart_successful = 0;
653
654 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -0600655 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -0800656 SLOGE("Encrypted filesystem not validated, aborting");
657 return -1;
658 }
659
660 if (restart_successful) {
661 SLOGE("System already restarted with encrypted disk, aborting");
662 return -1;
663 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800664
665 /* Here is where we shut down the framework. The init scripts
666 * start all services in one of three classes: core, main or late_start.
667 * On boot, we start core and main. Now, we stop main, but not core,
668 * as core includes vold and a few other really important things that
669 * we need to keep running. Once main has stopped, we should be able
670 * to umount the tmpfs /data, then mount the encrypted /data.
671 * We then restart the class main, and also the class late_start.
672 * At the moment, I've only put a few things in late_start that I know
673 * are not needed to bring up the framework, and that also cause problems
674 * with unmounting the tmpfs /data, but I hope to add add more services
675 * to the late_start class as we optimize this to decrease the delay
676 * till the user is asked for the password to the filesystem.
677 */
678
679 /* The init files are setup to stop the class main when vold.decrypt is
680 * set to trigger_reset_main.
681 */
682 property_set("vold.decrypt", "trigger_reset_main");
683 SLOGD("Just asked init to shut down class main\n");
684
685 /* Now that the framework is shutdown, we should be able to umount()
686 * the tmpfs filesystem, and mount the real one.
687 */
688
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800689 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
690 if (strlen(crypto_blkdev) == 0) {
691 SLOGE("fs_crypto_blkdev not set\n");
692 return -1;
693 }
694
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800695 if (! get_orig_mount_parms(DATA_MNT_POINT, fs_type, real_blkdev, &mnt_flags, fs_options)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800696 SLOGD("Just got orig mount parms\n");
697
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800698 if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800699 /* If that succeeded, then mount the decrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800700 mount(crypto_blkdev, DATA_MNT_POINT, fs_type, mnt_flags, fs_options);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800701
Ken Sumrallad2ac332011-03-08 17:07:06 -0800702 property_set("vold.decrypt", "trigger_load_persist_props");
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800703 /* Create necessary paths on /data */
704 if (prep_data_fs()) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800705 return -1;
706 }
707
708 /* startup service classes main and late_start */
709 property_set("vold.decrypt", "trigger_restart_framework");
710 SLOGD("Just triggered restart_framework\n");
711
712 /* Give it a few moments to get started */
713 sleep(1);
714 }
715 }
716
Ken Sumrall0cc16632011-01-18 20:32:26 -0800717 if (rc == 0) {
718 restart_successful = 1;
719 }
720
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800721 return rc;
722}
723
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800724static int do_crypto_complete(char *mount_point)
725{
726 struct crypt_mnt_ftr crypt_ftr;
727 unsigned char encrypted_master_key[32];
728 unsigned char salt[SALT_LEN];
729 char real_blkdev[MAXPATHLEN];
Ken Sumrall29d8da82011-05-18 17:20:07 -0700730 char fs_type[PROPERTY_VALUE_MAX];
731 char fs_options[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800732 unsigned long mnt_flags;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700733 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -0800734 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800735
736 property_get("ro.crypto.state", encrypted_state, "");
737 if (strcmp(encrypted_state, "encrypted") ) {
738 SLOGE("not running with encryption, aborting");
739 return 1;
740 }
741
742 if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
743 SLOGE("Error reading original mount parms for mount point %s\n", mount_point);
744 return -1;
745 }
746
747 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumralle1a45852011-12-14 21:24:27 -0800748 property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
749 /*
750 * Only report this error if key_loc is a file and it exists.
751 * If the device was never encrypted, and /data is not mountable for
752 * some reason, returning 1 should prevent the UI from presenting the
753 * a "enter password" screen, or worse, a "press button to wipe the
754 * device" screen.
755 */
756 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
757 SLOGE("master key file does not exist, aborting");
758 return 1;
759 } else {
760 SLOGE("Error getting crypt footer and key\n");
761 return -1;
762 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800763 }
764
765 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
766 SLOGE("Encryption process didn't finish successfully\n");
767 return -2; /* -2 is the clue to the UI that there is no usable data on the disk,
768 * and give the user an option to wipe the disk */
769 }
770
771 /* We passed the test! We shall diminish, and return to the west */
772 return 0;
773}
774
Ken Sumrall29d8da82011-05-18 17:20:07 -0700775static int test_mount_encrypted_fs(char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800776{
777 struct crypt_mnt_ftr crypt_ftr;
778 /* Allocate enough space for a 256 bit key, but we may use less */
779 unsigned char encrypted_master_key[32], decrypted_master_key[32];
Ken Sumralle8744072011-01-18 22:01:55 -0800780 unsigned char salt[SALT_LEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800781 char crypto_blkdev[MAXPATHLEN];
782 char real_blkdev[MAXPATHLEN];
Ken Sumrall29d8da82011-05-18 17:20:07 -0700783 char fs_type[PROPERTY_VALUE_MAX];
784 char fs_options[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800785 char tmp_mount_point[64];
786 unsigned long mnt_flags;
787 unsigned int orig_failed_decrypt_count;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700788 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800789 int rc;
790
Ken Sumrall0cc16632011-01-18 20:32:26 -0800791 property_get("ro.crypto.state", encrypted_state, "");
Jason parks70a4b3f2011-01-28 10:10:47 -0600792 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
Ken Sumrall0cc16632011-01-18 20:32:26 -0800793 SLOGE("encrypted fs already validated or not running with encryption, aborting");
794 return -1;
795 }
796
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800797 if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
798 SLOGE("Error reading original mount parms for mount point %s\n", mount_point);
799 return -1;
800 }
801
Ken Sumralle8744072011-01-18 22:01:55 -0800802 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800803 SLOGE("Error getting crypt footer and key\n");
804 return -1;
805 }
Ken Sumralld33d4172011-02-01 00:49:13 -0800806
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800807 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size);
808 orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count;
809
810 if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Ken Sumralle8744072011-01-18 22:01:55 -0800811 decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800812 }
813
814 if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -0700815 real_blkdev, crypto_blkdev, label)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800816 SLOGE("Error creating decrypted block device\n");
817 return -1;
818 }
819
820 /* If init detects an encrypted filesystme, it writes a file for each such
821 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
822 * files and passes that data to me */
823 /* Create a tmp mount point to try mounting the decryptd fs
824 * Since we're here, the mount_point should be a tmpfs filesystem, so make
825 * a directory in it to test mount the decrypted filesystem.
826 */
827 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
828 mkdir(tmp_mount_point, 0755);
829 if ( mount(crypto_blkdev, tmp_mount_point, "ext4", MS_RDONLY, "") ) {
830 SLOGE("Error temp mounting decrypted block device\n");
Ken Sumrall29d8da82011-05-18 17:20:07 -0700831 delete_crypto_blk_dev(label);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800832 crypt_ftr.failed_decrypt_count++;
833 } else {
834 /* Success, so just umount and we'll mount it properly when we restart
835 * the framework.
836 */
837 umount(tmp_mount_point);
838 crypt_ftr.failed_decrypt_count = 0;
839 }
840
841 if (orig_failed_decrypt_count != crypt_ftr.failed_decrypt_count) {
Ken Sumralle8744072011-01-18 22:01:55 -0800842 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800843 }
844
845 if (crypt_ftr.failed_decrypt_count) {
846 /* We failed to mount the device, so return an error */
847 rc = crypt_ftr.failed_decrypt_count;
848
849 } else {
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800850 /* Woot! Success! Save the name of the crypto block device
851 * so we can mount it when restarting the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800852 */
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800853 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -0600854
855 /* Also save a the master key so we can reencrypted the key
856 * the key when we want to change the password on it.
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800857 */
Jason parks70a4b3f2011-01-28 10:10:47 -0600858 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall29d8da82011-05-18 17:20:07 -0700859 saved_data_blkdev = strdup(real_blkdev);
Ken Sumrall3ad90722011-10-04 20:38:29 -0700860 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -0600861 master_key_saved = 1;
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800862 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800863 }
864
865 return rc;
866}
867
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700868/* Called by vold when it wants to undo the crypto mapping of a volume it
869 * manages. This is usually in response to a factory reset, when we want
870 * to undo the crypto mapping so the volume is formatted in the clear.
871 */
872int cryptfs_revert_volume(const char *label)
873{
874 return delete_crypto_blk_dev((char *)label);
875}
876
Ken Sumrall29d8da82011-05-18 17:20:07 -0700877/*
878 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
879 * Setup a dm-crypt mapping, use the saved master key from
880 * setting up the /data mapping, and return the new device path.
881 */
882int cryptfs_setup_volume(const char *label, int major, int minor,
883 char *crypto_sys_path, unsigned int max_path,
884 int *new_major, int *new_minor)
885{
886 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
887 struct crypt_mnt_ftr sd_crypt_ftr;
888 unsigned char key[32], salt[32];
889 struct stat statbuf;
890 int nr_sec, fd;
891
892 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
893
894 /* Just want the footer, but gotta get it all */
895 get_crypt_ftr_and_key(saved_data_blkdev, &sd_crypt_ftr, key, salt);
896
897 /* Update the fs_size field to be the size of the volume */
898 fd = open(real_blkdev, O_RDONLY);
899 nr_sec = get_blkdev_size(fd);
900 close(fd);
901 if (nr_sec == 0) {
902 SLOGE("Cannot get size of volume %s\n", real_blkdev);
903 return -1;
904 }
905
906 sd_crypt_ftr.fs_size = nr_sec;
907 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
908 crypto_blkdev, label);
909
910 stat(crypto_blkdev, &statbuf);
911 *new_major = MAJOR(statbuf.st_rdev);
912 *new_minor = MINOR(statbuf.st_rdev);
913
914 /* Create path to sys entry for this block device */
915 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
916
917 return 0;
918}
919
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800920int cryptfs_crypto_complete(void)
921{
922 return do_crypto_complete("/data");
923}
924
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800925int cryptfs_check_passwd(char *passwd)
926{
927 int rc = -1;
928
Ken Sumrall29d8da82011-05-18 17:20:07 -0700929 rc = test_mount_encrypted_fs(passwd, DATA_MNT_POINT, "userdata");
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800930
931 return rc;
932}
933
Ken Sumrall3ad90722011-10-04 20:38:29 -0700934int cryptfs_verify_passwd(char *passwd)
935{
936 struct crypt_mnt_ftr crypt_ftr;
937 /* Allocate enough space for a 256 bit key, but we may use less */
938 unsigned char encrypted_master_key[32], decrypted_master_key[32];
939 unsigned char salt[SALT_LEN];
940 char real_blkdev[MAXPATHLEN];
941 char fs_type[PROPERTY_VALUE_MAX];
942 char fs_options[PROPERTY_VALUE_MAX];
943 unsigned long mnt_flags;
944 char encrypted_state[PROPERTY_VALUE_MAX];
945 int rc;
946
947 property_get("ro.crypto.state", encrypted_state, "");
948 if (strcmp(encrypted_state, "encrypted") ) {
949 SLOGE("device not encrypted, aborting");
950 return -2;
951 }
952
953 if (!master_key_saved) {
954 SLOGE("encrypted fs not yet mounted, aborting");
955 return -1;
956 }
957
958 if (!saved_mount_point) {
959 SLOGE("encrypted fs failed to save mount point, aborting");
960 return -1;
961 }
962
963 if (get_orig_mount_parms(saved_mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
964 SLOGE("Error reading original mount parms for mount point %s\n", saved_mount_point);
965 return -1;
966 }
967
968 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
969 SLOGE("Error getting crypt footer and key\n");
970 return -1;
971 }
972
973 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
974 /* If the device has no password, then just say the password is valid */
975 rc = 0;
976 } else {
977 decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
978 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
979 /* They match, the password is correct */
980 rc = 0;
981 } else {
982 /* If incorrect, sleep for a bit to prevent dictionary attacks */
983 sleep(1);
984 rc = 1;
985 }
986 }
987
988 return rc;
989}
990
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800991/* Initialize a crypt_mnt_ftr structure. The keysize is
992 * defaulted to 16 bytes, and the filesystem size to 0.
993 * Presumably, at a minimum, the caller will update the
994 * filesystem size and crypto_type_name after calling this function.
995 */
996static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
997{
998 ftr->magic = CRYPT_MNT_MAGIC;
999 ftr->major_version = 1;
1000 ftr->minor_version = 0;
1001 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
1002 ftr->flags = 0;
Jason parks70a4b3f2011-01-28 10:10:47 -06001003 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001004 ftr->spare1 = 0;
1005 ftr->fs_size = 0;
1006 ftr->failed_decrypt_count = 0;
1007 ftr->crypto_type_name[0] = '\0';
1008}
1009
Ken Sumrall29d8da82011-05-18 17:20:07 -07001010static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001011{
1012 char cmdline[256];
1013 int rc = -1;
1014
Ken Sumrall29d8da82011-05-18 17:20:07 -07001015 if (type == EXT4_FS) {
1016 snprintf(cmdline, sizeof(cmdline), "/system/bin/make_ext4fs -a /data -l %lld %s",
1017 size * 512, crypto_blkdev);
1018 SLOGI("Making empty filesystem with command %s\n", cmdline);
1019 } else if (type== FAT_FS) {
1020 snprintf(cmdline, sizeof(cmdline), "/system/bin/newfs_msdos -F 32 -O android -c 8 -s %lld %s",
1021 size, crypto_blkdev);
1022 SLOGI("Making empty filesystem with command %s\n", cmdline);
1023 } else {
1024 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
1025 return -1;
1026 }
1027
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001028 if (system(cmdline)) {
1029 SLOGE("Error creating empty filesystem on %s\n", crypto_blkdev);
1030 } else {
1031 SLOGD("Successfully created empty filesystem on %s\n", crypto_blkdev);
1032 rc = 0;
1033 }
1034
1035 return rc;
1036}
1037
1038static inline int unix_read(int fd, void* buff, int len)
1039{
1040 int ret;
1041 do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR);
1042 return ret;
1043}
1044
1045static inline int unix_write(int fd, const void* buff, int len)
1046{
1047 int ret;
1048 do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR);
1049 return ret;
1050}
1051
1052#define CRYPT_INPLACE_BUFSIZE 4096
1053#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512)
Ken Sumrall29d8da82011-05-18 17:20:07 -07001054static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, off64_t size,
1055 off64_t *size_already_done, off64_t tot_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001056{
1057 int realfd, cryptofd;
1058 char *buf[CRYPT_INPLACE_BUFSIZE];
1059 int rc = -1;
1060 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001061 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001062 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001063
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001064 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
1065 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
1066 return -1;
1067 }
1068
1069 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
1070 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1071 close(realfd);
1072 return -1;
1073 }
1074
1075 /* This is pretty much a simple loop of reading 4K, and writing 4K.
1076 * The size passed in is the number of 512 byte sectors in the filesystem.
1077 * So compute the number of whole 4K blocks we should read/write,
1078 * and the remainder.
1079 */
1080 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
1081 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001082 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
1083 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001084
1085 SLOGE("Encrypting filesystem in place...");
1086
Ken Sumrall29d8da82011-05-18 17:20:07 -07001087 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001088 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001089 /* process the majority of the filesystem in blocks */
1090 for (i=0; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001091 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001092 if (new_pct > cur_pct) {
1093 char buf[8];
1094
1095 cur_pct = new_pct;
1096 snprintf(buf, sizeof(buf), "%lld", cur_pct);
1097 property_set("vold.encrypt_progress", buf);
1098 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001099 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1100 SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1101 goto errout;
1102 }
1103 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1104 SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1105 goto errout;
1106 }
1107 }
1108
1109 /* Do any remaining sectors */
1110 for (i=0; i<remainder; i++) {
1111 if (unix_read(realfd, buf, 512) <= 0) {
1112 SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1113 goto errout;
1114 }
1115 if (unix_write(cryptofd, buf, 512) <= 0) {
1116 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1117 goto errout;
1118 }
1119 }
1120
Ken Sumrall29d8da82011-05-18 17:20:07 -07001121 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001122 rc = 0;
1123
1124errout:
1125 close(realfd);
1126 close(cryptofd);
1127
1128 return rc;
1129}
1130
1131#define CRYPTO_ENABLE_WIPE 1
1132#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001133
1134#define FRAMEWORK_BOOT_WAIT 60
1135
Ken Sumrall29d8da82011-05-18 17:20:07 -07001136static inline int should_encrypt(struct volume_info *volume)
1137{
1138 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
1139 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
1140}
1141
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001142int cryptfs_enable(char *howarg, char *passwd)
1143{
1144 int how = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001145 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN], sd_crypto_blkdev[MAXPATHLEN];
1146 char fs_type[PROPERTY_VALUE_MAX], fs_options[PROPERTY_VALUE_MAX],
1147 mount_point[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001148 unsigned long mnt_flags, nr_sec;
Jason parks70a4b3f2011-01-28 10:10:47 -06001149 unsigned char master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
Ken Sumralle8744072011-01-18 22:01:55 -08001150 unsigned char salt[SALT_LEN];
Ken Sumrall319b1042011-06-14 14:01:55 -07001151 int rc=-1, fd, i, ret;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001152 struct crypt_mnt_ftr crypt_ftr, sd_crypt_ftr;;
1153 char tmpfs_options[PROPERTY_VALUE_MAX];
1154 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001155 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07001156 char key_loc[PROPERTY_VALUE_MAX];
1157 char fuse_sdcard[PROPERTY_VALUE_MAX];
1158 char *sd_mnt_point;
1159 char sd_blk_dev[256] = { 0 };
1160 int num_vols;
1161 struct volume_info *vol_list = 0;
1162 off64_t cur_encryption_done=0, tot_encryption_size=0;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001163
1164 property_get("ro.crypto.state", encrypted_state, "");
1165 if (strcmp(encrypted_state, "unencrypted")) {
1166 SLOGE("Device is already running encrypted, aborting");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001167 goto error_unencrypted;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001168 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001169
Ken Sumrall29d8da82011-05-18 17:20:07 -07001170 property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
1171
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001172 if (!strcmp(howarg, "wipe")) {
1173 how = CRYPTO_ENABLE_WIPE;
1174 } else if (! strcmp(howarg, "inplace")) {
1175 how = CRYPTO_ENABLE_INPLACE;
1176 } else {
1177 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08001178 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001179 }
1180
1181 get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options);
1182
Ken Sumrall3ed82362011-01-28 23:31:16 -08001183 /* Get the size of the real block device */
1184 fd = open(real_blkdev, O_RDONLY);
1185 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
1186 SLOGE("Cannot get size of block device %s\n", real_blkdev);
1187 goto error_unencrypted;
1188 }
1189 close(fd);
1190
1191 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001192 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001193 unsigned int fs_size_sec, max_fs_size_sec;
1194
1195 fs_size_sec = get_fs_size(real_blkdev);
1196 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1197
1198 if (fs_size_sec > max_fs_size_sec) {
1199 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
1200 goto error_unencrypted;
1201 }
1202 }
1203
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001204 /* Get a wakelock as this may take a while, and we don't want the
1205 * device to sleep on us. We'll grab a partial wakelock, and if the UI
1206 * wants to keep the screen on, it can grab a full wakelock.
1207 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001208 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001209 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
1210
Ken Sumrall29d8da82011-05-18 17:20:07 -07001211 /* Get the sdcard mount point */
1212 sd_mnt_point = getenv("EXTERNAL_STORAGE");
1213 if (! sd_mnt_point) {
1214 sd_mnt_point = "/mnt/sdcard";
1215 }
1216
1217 num_vols=vold_getNumDirectVolumes();
1218 vol_list = malloc(sizeof(struct volume_info) * num_vols);
1219 vold_getDirectVolumeList(vol_list);
1220
1221 for (i=0; i<num_vols; i++) {
1222 if (should_encrypt(&vol_list[i])) {
1223 fd = open(vol_list[i].blk_dev, O_RDONLY);
1224 if ( (vol_list[i].size = get_blkdev_size(fd)) == 0) {
1225 SLOGE("Cannot get size of block device %s\n", vol_list[i].blk_dev);
1226 goto error_unencrypted;
1227 }
1228 close(fd);
1229
Ken Sumrall3b170052011-07-11 15:38:57 -07001230 ret=vold_disableVol(vol_list[i].label);
Ken Sumrall319b1042011-06-14 14:01:55 -07001231 if ((ret < 0) && (ret != UNMOUNT_NOT_MOUNTED_ERR)) {
1232 /* -2 is returned when the device exists but is not currently mounted.
1233 * ignore the error and continue. */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001234 SLOGE("Failed to unmount volume %s\n", vol_list[i].label);
1235 goto error_unencrypted;
1236 }
1237 }
1238 }
1239
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001240 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001241 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001242 */
1243 property_set("vold.decrypt", "trigger_shutdown_framework");
1244 SLOGD("Just asked init to shut down class main\n");
1245
Ken Sumrall29d8da82011-05-18 17:20:07 -07001246 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
1247 if (!strcmp(fuse_sdcard, "true")) {
1248 /* This is a device using the fuse layer to emulate the sdcard semantics
1249 * on top of the userdata partition. vold does not manage it, it is managed
1250 * by the sdcard service. The sdcard service was killed by the property trigger
1251 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
1252 * unlike the case for vold managed devices above.
1253 */
1254 if (wait_and_unmount(sd_mnt_point)) {
1255 goto error_shutting_down;
1256 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001257 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001258
1259 /* Now unmount the /data partition. */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001260 if (wait_and_unmount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001261 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001262 }
1263
1264 /* Do extra work for a better UX when doing the long inplace encryption */
1265 if (how == CRYPTO_ENABLE_INPLACE) {
1266 /* Now that /data is unmounted, we need to mount a tmpfs
1267 * /data, set a property saying we're doing inplace encryption,
1268 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001269 */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001270 property_get("ro.crypto.tmpfs_options", tmpfs_options, "");
1271 if (mount("tmpfs", DATA_MNT_POINT, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV,
1272 tmpfs_options) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001273 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001274 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001275 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08001276 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001277
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001278 /* restart the framework. */
1279 /* Create necessary paths on /data */
1280 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001281 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001282 }
1283
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001284 /* startup service classes main and late_start */
1285 property_set("vold.decrypt", "trigger_restart_min_framework");
1286 SLOGD("Just triggered restart_min_framework\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001287
Ken Sumrall7df84122011-01-18 14:04:08 -08001288 /* OK, the framework is restarted and will soon be showing a
1289 * progress bar. Time to setup an encrypted mapping, and
1290 * either write a new filesystem, or encrypt in place updating
1291 * the progress bar as we work.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001292 */
1293 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001294
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001295 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001296 /* Initialize a crypt_mnt_ftr for the partition */
1297 cryptfs_init_crypt_mnt_ftr(&crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001298 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
1299 crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1300 } else {
1301 crypt_ftr.fs_size = nr_sec;
1302 }
Ken Sumralld33d4172011-02-01 00:49:13 -08001303 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001304 strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
1305
1306 /* Make an encrypted master key */
Ken Sumralle8744072011-01-18 22:01:55 -08001307 if (create_encrypted_random_key(passwd, master_key, salt)) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001308 SLOGE("Cannot create encrypted master key\n");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001309 goto error_unencrypted;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001310 }
1311
1312 /* Write the key to the end of the partition */
Ken Sumralle8744072011-01-18 22:01:55 -08001313 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, master_key, salt);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001314
Ken Sumralle8744072011-01-18 22:01:55 -08001315 decrypt_master_key(passwd, salt, master_key, decrypted_master_key);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001316 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
1317 "userdata");
1318
Ken Sumrall128626f2011-06-28 18:45:14 -07001319 /* The size of the userdata partition, and add in the vold volumes below */
1320 tot_encryption_size = crypt_ftr.fs_size;
1321
Ken Sumrall29d8da82011-05-18 17:20:07 -07001322 /* setup crypto mapping for all encryptable volumes handled by vold */
1323 for (i=0; i<num_vols; i++) {
1324 if (should_encrypt(&vol_list[i])) {
1325 vol_list[i].crypt_ftr = crypt_ftr; /* gotta love struct assign */
1326 vol_list[i].crypt_ftr.fs_size = vol_list[i].size;
1327 create_crypto_blk_dev(&vol_list[i].crypt_ftr, decrypted_master_key,
1328 vol_list[i].blk_dev, vol_list[i].crypto_blkdev,
1329 vol_list[i].label);
Ken Sumrall128626f2011-06-28 18:45:14 -07001330 tot_encryption_size += vol_list[i].size;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001331 }
1332 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001333
1334 if (how == CRYPTO_ENABLE_WIPE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001335 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr.fs_size, EXT4_FS);
1336 /* Encrypt all encryptable volumes handled by vold */
1337 if (!rc) {
1338 for (i=0; i<num_vols; i++) {
1339 if (should_encrypt(&vol_list[i])) {
1340 rc = cryptfs_enable_wipe(vol_list[i].crypto_blkdev,
1341 vol_list[i].crypt_ftr.fs_size, FAT_FS);
1342 }
1343 }
1344 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001345 } else if (how == CRYPTO_ENABLE_INPLACE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001346 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size,
1347 &cur_encryption_done, tot_encryption_size);
1348 /* Encrypt all encryptable volumes handled by vold */
1349 if (!rc) {
1350 for (i=0; i<num_vols; i++) {
1351 if (should_encrypt(&vol_list[i])) {
1352 rc = cryptfs_enable_inplace(vol_list[i].crypto_blkdev,
1353 vol_list[i].blk_dev,
1354 vol_list[i].crypt_ftr.fs_size,
1355 &cur_encryption_done, tot_encryption_size);
1356 }
1357 }
1358 }
1359 if (!rc) {
1360 /* The inplace routine never actually sets the progress to 100%
1361 * due to the round down nature of integer division, so set it here */
1362 property_set("vold.encrypt_progress", "100");
1363 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001364 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001365 /* Shouldn't happen */
1366 SLOGE("cryptfs_enable: internal error, unknown option\n");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001367 goto error_unencrypted;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001368 }
1369
1370 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001371 delete_crypto_blk_dev("userdata");
1372 for (i=0; i<num_vols; i++) {
1373 if (should_encrypt(&vol_list[i])) {
1374 delete_crypto_blk_dev(vol_list[i].label);
1375 }
1376 }
1377
1378 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001379
1380 if (! rc) {
1381 /* Success */
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001382
Ken Sumralld33d4172011-02-01 00:49:13 -08001383 /* Clear the encryption in progres flag in the footer */
1384 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
1385 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0);
1386
Ken Sumrall29d8da82011-05-18 17:20:07 -07001387 sleep(2); /* Give the UI a chance to show 100% progress */
Ken Sumrallc290eaf2011-03-07 23:40:35 -08001388 android_reboot(ANDROID_RB_RESTART, 0, 0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001389 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08001390 char value[PROPERTY_VALUE_MAX];
1391
1392 property_get("ro.vold.wipe_on_cyrypt_fail", value, "0");
1393 if (!strcmp(value, "1")) {
1394 /* wipe data if encryption failed */
1395 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
1396 mkdir("/cache/recovery", 0700);
1397 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC);
1398 if (fd >= 0) {
1399 write(fd, "--wipe_data", strlen("--wipe_data") + 1);
1400 close(fd);
1401 } else {
1402 SLOGE("could not open /cache/recovery/command\n");
1403 }
1404 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
1405 } else {
1406 /* set property to trigger dialog */
1407 property_set("vold.encrypt_progress", "error_partially_encrypted");
1408 release_wake_lock(lockid);
1409 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001410 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001411 }
1412
Ken Sumrall3ed82362011-01-28 23:31:16 -08001413 /* hrm, the encrypt step claims success, but the reboot failed.
1414 * This should not happen.
1415 * Set the property and return. Hope the framework can deal with it.
1416 */
1417 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001418 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001419 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08001420
1421error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07001422 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001423 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001424 if (lockid[0]) {
1425 release_wake_lock(lockid);
1426 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001427 return -1;
1428
1429error_shutting_down:
1430 /* we failed, and have not encrypted anthing, so the users's data is still intact,
1431 * but the framework is stopped and not restarted to show the error, so it's up to
1432 * vold to restart the system.
1433 */
1434 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Ken Sumrallc290eaf2011-03-07 23:40:35 -08001435 android_reboot(ANDROID_RB_RESTART, 0, 0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001436
1437 /* shouldn't get here */
1438 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07001439 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001440 if (lockid[0]) {
1441 release_wake_lock(lockid);
1442 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001443 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001444}
1445
Jason parks70a4b3f2011-01-28 10:10:47 -06001446int cryptfs_changepw(char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001447{
1448 struct crypt_mnt_ftr crypt_ftr;
Jason parks70a4b3f2011-01-28 10:10:47 -06001449 unsigned char encrypted_master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
Ken Sumralle8744072011-01-18 22:01:55 -08001450 unsigned char salt[SALT_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001451 char real_blkdev[MAXPATHLEN];
1452
1453 /* This is only allowed after we've successfully decrypted the master key */
Jason parks70a4b3f2011-01-28 10:10:47 -06001454 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001455 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001456 return -1;
1457 }
1458
1459 property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
1460 if (strlen(real_blkdev) == 0) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001461 SLOGE("Can't find real blkdev");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001462 return -1;
1463 }
1464
1465 /* get key */
Ken Sumralle8744072011-01-18 22:01:55 -08001466 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001467 SLOGE("Error getting crypt footer and key");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001468 return -1;
1469 }
1470
Jason parks70a4b3f2011-01-28 10:10:47 -06001471 encrypt_master_key(newpw, salt, saved_master_key, encrypted_master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001472
Jason parks70a4b3f2011-01-28 10:10:47 -06001473 /* save the key */
1474 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001475
1476 return 0;
1477}