blob: 899d4d43caa1b0515e7496567dcd12930c9355d5 [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 Sumrall7f7dbaa2011-02-01 15:46:41 -0800734
735 property_get("ro.crypto.state", encrypted_state, "");
736 if (strcmp(encrypted_state, "encrypted") ) {
737 SLOGE("not running with encryption, aborting");
738 return 1;
739 }
740
741 if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
742 SLOGE("Error reading original mount parms for mount point %s\n", mount_point);
743 return -1;
744 }
745
746 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
747 SLOGE("Error getting crypt footer and key\n");
748 return -1;
749 }
750
751 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
752 SLOGE("Encryption process didn't finish successfully\n");
753 return -2; /* -2 is the clue to the UI that there is no usable data on the disk,
754 * and give the user an option to wipe the disk */
755 }
756
757 /* We passed the test! We shall diminish, and return to the west */
758 return 0;
759}
760
Ken Sumrall29d8da82011-05-18 17:20:07 -0700761static int test_mount_encrypted_fs(char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800762{
763 struct crypt_mnt_ftr crypt_ftr;
764 /* Allocate enough space for a 256 bit key, but we may use less */
765 unsigned char encrypted_master_key[32], decrypted_master_key[32];
Ken Sumralle8744072011-01-18 22:01:55 -0800766 unsigned char salt[SALT_LEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800767 char crypto_blkdev[MAXPATHLEN];
768 char real_blkdev[MAXPATHLEN];
Ken Sumrall29d8da82011-05-18 17:20:07 -0700769 char fs_type[PROPERTY_VALUE_MAX];
770 char fs_options[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800771 char tmp_mount_point[64];
772 unsigned long mnt_flags;
773 unsigned int orig_failed_decrypt_count;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700774 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800775 int rc;
776
Ken Sumrall0cc16632011-01-18 20:32:26 -0800777 property_get("ro.crypto.state", encrypted_state, "");
Jason parks70a4b3f2011-01-28 10:10:47 -0600778 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
Ken Sumrall0cc16632011-01-18 20:32:26 -0800779 SLOGE("encrypted fs already validated or not running with encryption, aborting");
780 return -1;
781 }
782
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800783 if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
784 SLOGE("Error reading original mount parms for mount point %s\n", mount_point);
785 return -1;
786 }
787
Ken Sumralle8744072011-01-18 22:01:55 -0800788 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800789 SLOGE("Error getting crypt footer and key\n");
790 return -1;
791 }
Ken Sumralld33d4172011-02-01 00:49:13 -0800792
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800793 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size);
794 orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count;
795
796 if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Ken Sumralle8744072011-01-18 22:01:55 -0800797 decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800798 }
799
800 if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -0700801 real_blkdev, crypto_blkdev, label)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800802 SLOGE("Error creating decrypted block device\n");
803 return -1;
804 }
805
806 /* If init detects an encrypted filesystme, it writes a file for each such
807 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
808 * files and passes that data to me */
809 /* Create a tmp mount point to try mounting the decryptd fs
810 * Since we're here, the mount_point should be a tmpfs filesystem, so make
811 * a directory in it to test mount the decrypted filesystem.
812 */
813 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
814 mkdir(tmp_mount_point, 0755);
815 if ( mount(crypto_blkdev, tmp_mount_point, "ext4", MS_RDONLY, "") ) {
816 SLOGE("Error temp mounting decrypted block device\n");
Ken Sumrall29d8da82011-05-18 17:20:07 -0700817 delete_crypto_blk_dev(label);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800818 crypt_ftr.failed_decrypt_count++;
819 } else {
820 /* Success, so just umount and we'll mount it properly when we restart
821 * the framework.
822 */
823 umount(tmp_mount_point);
824 crypt_ftr.failed_decrypt_count = 0;
825 }
826
827 if (orig_failed_decrypt_count != crypt_ftr.failed_decrypt_count) {
Ken Sumralle8744072011-01-18 22:01:55 -0800828 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800829 }
830
831 if (crypt_ftr.failed_decrypt_count) {
832 /* We failed to mount the device, so return an error */
833 rc = crypt_ftr.failed_decrypt_count;
834
835 } else {
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800836 /* Woot! Success! Save the name of the crypto block device
837 * so we can mount it when restarting the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800838 */
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800839 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -0600840
841 /* Also save a the master key so we can reencrypted the key
842 * the key when we want to change the password on it.
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800843 */
Jason parks70a4b3f2011-01-28 10:10:47 -0600844 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall29d8da82011-05-18 17:20:07 -0700845 saved_data_blkdev = strdup(real_blkdev);
Ken Sumrall3ad90722011-10-04 20:38:29 -0700846 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -0600847 master_key_saved = 1;
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800848 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800849 }
850
851 return rc;
852}
853
Ken Sumrall0b8b5972011-08-31 16:14:23 -0700854/* Called by vold when it wants to undo the crypto mapping of a volume it
855 * manages. This is usually in response to a factory reset, when we want
856 * to undo the crypto mapping so the volume is formatted in the clear.
857 */
858int cryptfs_revert_volume(const char *label)
859{
860 return delete_crypto_blk_dev((char *)label);
861}
862
Ken Sumrall29d8da82011-05-18 17:20:07 -0700863/*
864 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
865 * Setup a dm-crypt mapping, use the saved master key from
866 * setting up the /data mapping, and return the new device path.
867 */
868int cryptfs_setup_volume(const char *label, int major, int minor,
869 char *crypto_sys_path, unsigned int max_path,
870 int *new_major, int *new_minor)
871{
872 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
873 struct crypt_mnt_ftr sd_crypt_ftr;
874 unsigned char key[32], salt[32];
875 struct stat statbuf;
876 int nr_sec, fd;
877
878 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
879
880 /* Just want the footer, but gotta get it all */
881 get_crypt_ftr_and_key(saved_data_blkdev, &sd_crypt_ftr, key, salt);
882
883 /* Update the fs_size field to be the size of the volume */
884 fd = open(real_blkdev, O_RDONLY);
885 nr_sec = get_blkdev_size(fd);
886 close(fd);
887 if (nr_sec == 0) {
888 SLOGE("Cannot get size of volume %s\n", real_blkdev);
889 return -1;
890 }
891
892 sd_crypt_ftr.fs_size = nr_sec;
893 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
894 crypto_blkdev, label);
895
896 stat(crypto_blkdev, &statbuf);
897 *new_major = MAJOR(statbuf.st_rdev);
898 *new_minor = MINOR(statbuf.st_rdev);
899
900 /* Create path to sys entry for this block device */
901 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
902
903 return 0;
904}
905
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800906int cryptfs_crypto_complete(void)
907{
908 return do_crypto_complete("/data");
909}
910
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800911int cryptfs_check_passwd(char *passwd)
912{
913 int rc = -1;
914
Ken Sumrall29d8da82011-05-18 17:20:07 -0700915 rc = test_mount_encrypted_fs(passwd, DATA_MNT_POINT, "userdata");
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800916
917 return rc;
918}
919
Ken Sumrall3ad90722011-10-04 20:38:29 -0700920int cryptfs_verify_passwd(char *passwd)
921{
922 struct crypt_mnt_ftr crypt_ftr;
923 /* Allocate enough space for a 256 bit key, but we may use less */
924 unsigned char encrypted_master_key[32], decrypted_master_key[32];
925 unsigned char salt[SALT_LEN];
926 char real_blkdev[MAXPATHLEN];
927 char fs_type[PROPERTY_VALUE_MAX];
928 char fs_options[PROPERTY_VALUE_MAX];
929 unsigned long mnt_flags;
930 char encrypted_state[PROPERTY_VALUE_MAX];
931 int rc;
932
933 property_get("ro.crypto.state", encrypted_state, "");
934 if (strcmp(encrypted_state, "encrypted") ) {
935 SLOGE("device not encrypted, aborting");
936 return -2;
937 }
938
939 if (!master_key_saved) {
940 SLOGE("encrypted fs not yet mounted, aborting");
941 return -1;
942 }
943
944 if (!saved_mount_point) {
945 SLOGE("encrypted fs failed to save mount point, aborting");
946 return -1;
947 }
948
949 if (get_orig_mount_parms(saved_mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
950 SLOGE("Error reading original mount parms for mount point %s\n", saved_mount_point);
951 return -1;
952 }
953
954 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
955 SLOGE("Error getting crypt footer and key\n");
956 return -1;
957 }
958
959 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
960 /* If the device has no password, then just say the password is valid */
961 rc = 0;
962 } else {
963 decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
964 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
965 /* They match, the password is correct */
966 rc = 0;
967 } else {
968 /* If incorrect, sleep for a bit to prevent dictionary attacks */
969 sleep(1);
970 rc = 1;
971 }
972 }
973
974 return rc;
975}
976
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800977/* Initialize a crypt_mnt_ftr structure. The keysize is
978 * defaulted to 16 bytes, and the filesystem size to 0.
979 * Presumably, at a minimum, the caller will update the
980 * filesystem size and crypto_type_name after calling this function.
981 */
982static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
983{
984 ftr->magic = CRYPT_MNT_MAGIC;
985 ftr->major_version = 1;
986 ftr->minor_version = 0;
987 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
988 ftr->flags = 0;
Jason parks70a4b3f2011-01-28 10:10:47 -0600989 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800990 ftr->spare1 = 0;
991 ftr->fs_size = 0;
992 ftr->failed_decrypt_count = 0;
993 ftr->crypto_type_name[0] = '\0';
994}
995
Ken Sumrall29d8da82011-05-18 17:20:07 -0700996static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800997{
998 char cmdline[256];
999 int rc = -1;
1000
Ken Sumrall29d8da82011-05-18 17:20:07 -07001001 if (type == EXT4_FS) {
1002 snprintf(cmdline, sizeof(cmdline), "/system/bin/make_ext4fs -a /data -l %lld %s",
1003 size * 512, crypto_blkdev);
1004 SLOGI("Making empty filesystem with command %s\n", cmdline);
1005 } else if (type== FAT_FS) {
1006 snprintf(cmdline, sizeof(cmdline), "/system/bin/newfs_msdos -F 32 -O android -c 8 -s %lld %s",
1007 size, crypto_blkdev);
1008 SLOGI("Making empty filesystem with command %s\n", cmdline);
1009 } else {
1010 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
1011 return -1;
1012 }
1013
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001014 if (system(cmdline)) {
1015 SLOGE("Error creating empty filesystem on %s\n", crypto_blkdev);
1016 } else {
1017 SLOGD("Successfully created empty filesystem on %s\n", crypto_blkdev);
1018 rc = 0;
1019 }
1020
1021 return rc;
1022}
1023
1024static inline int unix_read(int fd, void* buff, int len)
1025{
1026 int ret;
1027 do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR);
1028 return ret;
1029}
1030
1031static inline int unix_write(int fd, const void* buff, int len)
1032{
1033 int ret;
1034 do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR);
1035 return ret;
1036}
1037
1038#define CRYPT_INPLACE_BUFSIZE 4096
1039#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512)
Ken Sumrall29d8da82011-05-18 17:20:07 -07001040static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, off64_t size,
1041 off64_t *size_already_done, off64_t tot_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001042{
1043 int realfd, cryptofd;
1044 char *buf[CRYPT_INPLACE_BUFSIZE];
1045 int rc = -1;
1046 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001047 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001048 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001049
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001050 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
1051 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
1052 return -1;
1053 }
1054
1055 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
1056 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1057 close(realfd);
1058 return -1;
1059 }
1060
1061 /* This is pretty much a simple loop of reading 4K, and writing 4K.
1062 * The size passed in is the number of 512 byte sectors in the filesystem.
1063 * So compute the number of whole 4K blocks we should read/write,
1064 * and the remainder.
1065 */
1066 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
1067 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001068 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
1069 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001070
1071 SLOGE("Encrypting filesystem in place...");
1072
Ken Sumrall29d8da82011-05-18 17:20:07 -07001073 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001074 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001075 /* process the majority of the filesystem in blocks */
1076 for (i=0; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001077 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001078 if (new_pct > cur_pct) {
1079 char buf[8];
1080
1081 cur_pct = new_pct;
1082 snprintf(buf, sizeof(buf), "%lld", cur_pct);
1083 property_set("vold.encrypt_progress", buf);
1084 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001085 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1086 SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1087 goto errout;
1088 }
1089 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1090 SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1091 goto errout;
1092 }
1093 }
1094
1095 /* Do any remaining sectors */
1096 for (i=0; i<remainder; i++) {
1097 if (unix_read(realfd, buf, 512) <= 0) {
1098 SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1099 goto errout;
1100 }
1101 if (unix_write(cryptofd, buf, 512) <= 0) {
1102 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1103 goto errout;
1104 }
1105 }
1106
Ken Sumrall29d8da82011-05-18 17:20:07 -07001107 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001108 rc = 0;
1109
1110errout:
1111 close(realfd);
1112 close(cryptofd);
1113
1114 return rc;
1115}
1116
1117#define CRYPTO_ENABLE_WIPE 1
1118#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001119
1120#define FRAMEWORK_BOOT_WAIT 60
1121
Ken Sumrall29d8da82011-05-18 17:20:07 -07001122static inline int should_encrypt(struct volume_info *volume)
1123{
1124 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
1125 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
1126}
1127
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001128int cryptfs_enable(char *howarg, char *passwd)
1129{
1130 int how = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001131 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN], sd_crypto_blkdev[MAXPATHLEN];
1132 char fs_type[PROPERTY_VALUE_MAX], fs_options[PROPERTY_VALUE_MAX],
1133 mount_point[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001134 unsigned long mnt_flags, nr_sec;
Jason parks70a4b3f2011-01-28 10:10:47 -06001135 unsigned char master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
Ken Sumralle8744072011-01-18 22:01:55 -08001136 unsigned char salt[SALT_LEN];
Ken Sumrall319b1042011-06-14 14:01:55 -07001137 int rc=-1, fd, i, ret;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001138 struct crypt_mnt_ftr crypt_ftr, sd_crypt_ftr;;
1139 char tmpfs_options[PROPERTY_VALUE_MAX];
1140 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001141 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07001142 char key_loc[PROPERTY_VALUE_MAX];
1143 char fuse_sdcard[PROPERTY_VALUE_MAX];
1144 char *sd_mnt_point;
1145 char sd_blk_dev[256] = { 0 };
1146 int num_vols;
1147 struct volume_info *vol_list = 0;
1148 off64_t cur_encryption_done=0, tot_encryption_size=0;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001149
1150 property_get("ro.crypto.state", encrypted_state, "");
1151 if (strcmp(encrypted_state, "unencrypted")) {
1152 SLOGE("Device is already running encrypted, aborting");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001153 goto error_unencrypted;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001154 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001155
Ken Sumrall29d8da82011-05-18 17:20:07 -07001156 property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
1157
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001158 if (!strcmp(howarg, "wipe")) {
1159 how = CRYPTO_ENABLE_WIPE;
1160 } else if (! strcmp(howarg, "inplace")) {
1161 how = CRYPTO_ENABLE_INPLACE;
1162 } else {
1163 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08001164 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001165 }
1166
1167 get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options);
1168
Ken Sumrall3ed82362011-01-28 23:31:16 -08001169 /* Get the size of the real block device */
1170 fd = open(real_blkdev, O_RDONLY);
1171 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
1172 SLOGE("Cannot get size of block device %s\n", real_blkdev);
1173 goto error_unencrypted;
1174 }
1175 close(fd);
1176
1177 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001178 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001179 unsigned int fs_size_sec, max_fs_size_sec;
1180
1181 fs_size_sec = get_fs_size(real_blkdev);
1182 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1183
1184 if (fs_size_sec > max_fs_size_sec) {
1185 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
1186 goto error_unencrypted;
1187 }
1188 }
1189
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001190 /* Get a wakelock as this may take a while, and we don't want the
1191 * device to sleep on us. We'll grab a partial wakelock, and if the UI
1192 * wants to keep the screen on, it can grab a full wakelock.
1193 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001194 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001195 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
1196
Ken Sumrall29d8da82011-05-18 17:20:07 -07001197 /* Get the sdcard mount point */
1198 sd_mnt_point = getenv("EXTERNAL_STORAGE");
1199 if (! sd_mnt_point) {
1200 sd_mnt_point = "/mnt/sdcard";
1201 }
1202
1203 num_vols=vold_getNumDirectVolumes();
1204 vol_list = malloc(sizeof(struct volume_info) * num_vols);
1205 vold_getDirectVolumeList(vol_list);
1206
1207 for (i=0; i<num_vols; i++) {
1208 if (should_encrypt(&vol_list[i])) {
1209 fd = open(vol_list[i].blk_dev, O_RDONLY);
1210 if ( (vol_list[i].size = get_blkdev_size(fd)) == 0) {
1211 SLOGE("Cannot get size of block device %s\n", vol_list[i].blk_dev);
1212 goto error_unencrypted;
1213 }
1214 close(fd);
1215
Ken Sumrall3b170052011-07-11 15:38:57 -07001216 ret=vold_disableVol(vol_list[i].label);
Ken Sumrall319b1042011-06-14 14:01:55 -07001217 if ((ret < 0) && (ret != UNMOUNT_NOT_MOUNTED_ERR)) {
1218 /* -2 is returned when the device exists but is not currently mounted.
1219 * ignore the error and continue. */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001220 SLOGE("Failed to unmount volume %s\n", vol_list[i].label);
1221 goto error_unencrypted;
1222 }
1223 }
1224 }
1225
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001226 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001227 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001228 */
1229 property_set("vold.decrypt", "trigger_shutdown_framework");
1230 SLOGD("Just asked init to shut down class main\n");
1231
Ken Sumrall29d8da82011-05-18 17:20:07 -07001232 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
1233 if (!strcmp(fuse_sdcard, "true")) {
1234 /* This is a device using the fuse layer to emulate the sdcard semantics
1235 * on top of the userdata partition. vold does not manage it, it is managed
1236 * by the sdcard service. The sdcard service was killed by the property trigger
1237 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
1238 * unlike the case for vold managed devices above.
1239 */
1240 if (wait_and_unmount(sd_mnt_point)) {
1241 goto error_shutting_down;
1242 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001243 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001244
1245 /* Now unmount the /data partition. */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001246 if (wait_and_unmount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001247 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001248 }
1249
1250 /* Do extra work for a better UX when doing the long inplace encryption */
1251 if (how == CRYPTO_ENABLE_INPLACE) {
1252 /* Now that /data is unmounted, we need to mount a tmpfs
1253 * /data, set a property saying we're doing inplace encryption,
1254 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001255 */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001256 property_get("ro.crypto.tmpfs_options", tmpfs_options, "");
1257 if (mount("tmpfs", DATA_MNT_POINT, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV,
1258 tmpfs_options) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001259 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001260 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001261 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08001262 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001263
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001264 /* restart the framework. */
1265 /* Create necessary paths on /data */
1266 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001267 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001268 }
1269
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001270 /* startup service classes main and late_start */
1271 property_set("vold.decrypt", "trigger_restart_min_framework");
1272 SLOGD("Just triggered restart_min_framework\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001273
Ken Sumrall7df84122011-01-18 14:04:08 -08001274 /* OK, the framework is restarted and will soon be showing a
1275 * progress bar. Time to setup an encrypted mapping, and
1276 * either write a new filesystem, or encrypt in place updating
1277 * the progress bar as we work.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001278 */
1279 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001280
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001281 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001282 /* Initialize a crypt_mnt_ftr for the partition */
1283 cryptfs_init_crypt_mnt_ftr(&crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001284 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
1285 crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1286 } else {
1287 crypt_ftr.fs_size = nr_sec;
1288 }
Ken Sumralld33d4172011-02-01 00:49:13 -08001289 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001290 strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
1291
1292 /* Make an encrypted master key */
Ken Sumralle8744072011-01-18 22:01:55 -08001293 if (create_encrypted_random_key(passwd, master_key, salt)) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001294 SLOGE("Cannot create encrypted master key\n");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001295 goto error_unencrypted;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001296 }
1297
1298 /* Write the key to the end of the partition */
Ken Sumralle8744072011-01-18 22:01:55 -08001299 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, master_key, salt);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001300
Ken Sumralle8744072011-01-18 22:01:55 -08001301 decrypt_master_key(passwd, salt, master_key, decrypted_master_key);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001302 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
1303 "userdata");
1304
Ken Sumrall128626f2011-06-28 18:45:14 -07001305 /* The size of the userdata partition, and add in the vold volumes below */
1306 tot_encryption_size = crypt_ftr.fs_size;
1307
Ken Sumrall29d8da82011-05-18 17:20:07 -07001308 /* setup crypto mapping for all encryptable volumes handled by vold */
1309 for (i=0; i<num_vols; i++) {
1310 if (should_encrypt(&vol_list[i])) {
1311 vol_list[i].crypt_ftr = crypt_ftr; /* gotta love struct assign */
1312 vol_list[i].crypt_ftr.fs_size = vol_list[i].size;
1313 create_crypto_blk_dev(&vol_list[i].crypt_ftr, decrypted_master_key,
1314 vol_list[i].blk_dev, vol_list[i].crypto_blkdev,
1315 vol_list[i].label);
Ken Sumrall128626f2011-06-28 18:45:14 -07001316 tot_encryption_size += vol_list[i].size;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001317 }
1318 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001319
1320 if (how == CRYPTO_ENABLE_WIPE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001321 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr.fs_size, EXT4_FS);
1322 /* Encrypt all encryptable volumes handled by vold */
1323 if (!rc) {
1324 for (i=0; i<num_vols; i++) {
1325 if (should_encrypt(&vol_list[i])) {
1326 rc = cryptfs_enable_wipe(vol_list[i].crypto_blkdev,
1327 vol_list[i].crypt_ftr.fs_size, FAT_FS);
1328 }
1329 }
1330 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001331 } else if (how == CRYPTO_ENABLE_INPLACE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001332 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size,
1333 &cur_encryption_done, tot_encryption_size);
1334 /* Encrypt all encryptable volumes handled by vold */
1335 if (!rc) {
1336 for (i=0; i<num_vols; i++) {
1337 if (should_encrypt(&vol_list[i])) {
1338 rc = cryptfs_enable_inplace(vol_list[i].crypto_blkdev,
1339 vol_list[i].blk_dev,
1340 vol_list[i].crypt_ftr.fs_size,
1341 &cur_encryption_done, tot_encryption_size);
1342 }
1343 }
1344 }
1345 if (!rc) {
1346 /* The inplace routine never actually sets the progress to 100%
1347 * due to the round down nature of integer division, so set it here */
1348 property_set("vold.encrypt_progress", "100");
1349 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001350 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001351 /* Shouldn't happen */
1352 SLOGE("cryptfs_enable: internal error, unknown option\n");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001353 goto error_unencrypted;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001354 }
1355
1356 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001357 delete_crypto_blk_dev("userdata");
1358 for (i=0; i<num_vols; i++) {
1359 if (should_encrypt(&vol_list[i])) {
1360 delete_crypto_blk_dev(vol_list[i].label);
1361 }
1362 }
1363
1364 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001365
1366 if (! rc) {
1367 /* Success */
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001368
Ken Sumralld33d4172011-02-01 00:49:13 -08001369 /* Clear the encryption in progres flag in the footer */
1370 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
1371 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0);
1372
Ken Sumrall29d8da82011-05-18 17:20:07 -07001373 sleep(2); /* Give the UI a chance to show 100% progress */
Ken Sumrallc290eaf2011-03-07 23:40:35 -08001374 android_reboot(ANDROID_RB_RESTART, 0, 0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001375 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08001376 char value[PROPERTY_VALUE_MAX];
1377
1378 property_get("ro.vold.wipe_on_cyrypt_fail", value, "0");
1379 if (!strcmp(value, "1")) {
1380 /* wipe data if encryption failed */
1381 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
1382 mkdir("/cache/recovery", 0700);
1383 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC);
1384 if (fd >= 0) {
1385 write(fd, "--wipe_data", strlen("--wipe_data") + 1);
1386 close(fd);
1387 } else {
1388 SLOGE("could not open /cache/recovery/command\n");
1389 }
1390 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
1391 } else {
1392 /* set property to trigger dialog */
1393 property_set("vold.encrypt_progress", "error_partially_encrypted");
1394 release_wake_lock(lockid);
1395 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001396 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001397 }
1398
Ken Sumrall3ed82362011-01-28 23:31:16 -08001399 /* hrm, the encrypt step claims success, but the reboot failed.
1400 * This should not happen.
1401 * Set the property and return. Hope the framework can deal with it.
1402 */
1403 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001404 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001405 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08001406
1407error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07001408 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001409 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001410 if (lockid[0]) {
1411 release_wake_lock(lockid);
1412 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001413 return -1;
1414
1415error_shutting_down:
1416 /* we failed, and have not encrypted anthing, so the users's data is still intact,
1417 * but the framework is stopped and not restarted to show the error, so it's up to
1418 * vold to restart the system.
1419 */
1420 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Ken Sumrallc290eaf2011-03-07 23:40:35 -08001421 android_reboot(ANDROID_RB_RESTART, 0, 0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001422
1423 /* shouldn't get here */
1424 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07001425 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001426 if (lockid[0]) {
1427 release_wake_lock(lockid);
1428 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001429 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001430}
1431
Jason parks70a4b3f2011-01-28 10:10:47 -06001432int cryptfs_changepw(char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001433{
1434 struct crypt_mnt_ftr crypt_ftr;
Jason parks70a4b3f2011-01-28 10:10:47 -06001435 unsigned char encrypted_master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
Ken Sumralle8744072011-01-18 22:01:55 -08001436 unsigned char salt[SALT_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001437 char real_blkdev[MAXPATHLEN];
1438
1439 /* This is only allowed after we've successfully decrypted the master key */
Jason parks70a4b3f2011-01-28 10:10:47 -06001440 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001441 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001442 return -1;
1443 }
1444
1445 property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
1446 if (strlen(real_blkdev) == 0) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001447 SLOGE("Can't find real blkdev");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001448 return -1;
1449 }
1450
1451 /* get key */
Ken Sumralle8744072011-01-18 22:01:55 -08001452 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001453 SLOGE("Error getting crypt footer and key");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001454 return -1;
1455 }
1456
Jason parks70a4b3f2011-01-28 10:10:47 -06001457 encrypt_master_key(newpw, salt, saved_master_key, encrypted_master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001458
Jason parks70a4b3f2011-01-28 10:10:47 -06001459 /* save the key */
1460 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001461
1462 return 0;
1463}