blob: 8b5f5233c6a55e0372f74a69ecaf57a4dfc8e1ca [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"
43#include "cutils/log.h"
44#include "cutils/properties.h"
Ken Sumrall5d4c68e2011-01-30 19:06:03 -080045#include "hardware_legacy/power.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070046#include "VolumeManager.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080047
48#define DM_CRYPT_BUF_SIZE 4096
Ken Sumrall8ddbe402011-01-17 15:26:29 -080049#define DATA_MNT_POINT "/data"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080050
Jason parks70a4b3f2011-01-28 10:10:47 -060051#define HASH_COUNT 2000
52#define KEY_LEN_BYTES 16
53#define IV_LEN_BYTES 16
54
Ken Sumrall29d8da82011-05-18 17:20:07 -070055#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
56#define KEY_IN_FOOTER "footer"
57
58#define EXT4_FS 1
59#define FAT_FS 2
60
Ken Sumrall8f869aa2010-12-03 03:47:09 -080061char *me = "cryptfs";
62
Jason parks70a4b3f2011-01-28 10:10:47 -060063static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall29d8da82011-05-18 17:20:07 -070064static char *saved_data_blkdev;
Jason parks70a4b3f2011-01-28 10:10:47 -060065static int master_key_saved = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -080066
Ken Sumrall8f869aa2010-12-03 03:47:09 -080067static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
68{
69 memset(io, 0, dataSize);
70 io->data_size = dataSize;
71 io->data_start = sizeof(struct dm_ioctl);
72 io->version[0] = 4;
73 io->version[1] = 0;
74 io->version[2] = 0;
75 io->flags = flags;
76 if (name) {
77 strncpy(io->name, name, sizeof(io->name));
78 }
79}
80
Ken Sumrall3ed82362011-01-28 23:31:16 -080081static unsigned int get_fs_size(char *dev)
82{
83 int fd, block_size;
84 struct ext4_super_block sb;
85 off64_t len;
86
87 if ((fd = open(dev, O_RDONLY)) < 0) {
88 SLOGE("Cannot open device to get filesystem size ");
89 return 0;
90 }
91
92 if (lseek64(fd, 1024, SEEK_SET) < 0) {
93 SLOGE("Cannot seek to superblock");
94 return 0;
95 }
96
97 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
98 SLOGE("Cannot read superblock");
99 return 0;
100 }
101
102 close(fd);
103
104 block_size = 1024 << sb.s_log_block_size;
105 /* compute length in bytes */
106 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
107
108 /* return length in sectors */
109 return (unsigned int) (len / 512);
110}
111
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800112static unsigned int get_blkdev_size(int fd)
113{
114 unsigned int nr_sec;
115
116 if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
117 nr_sec = 0;
118 }
119
120 return nr_sec;
121}
122
Ken Sumralle8744072011-01-18 22:01:55 -0800123/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800124 * update the failed mount count but not change the key.
125 */
126static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
Ken Sumralle8744072011-01-18 22:01:55 -0800127 unsigned char *key, unsigned char *salt)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800128{
129 int fd;
130 unsigned int nr_sec, cnt;
131 off64_t off;
132 int rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700133 char *fname;
134 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800135
Ken Sumrall29d8da82011-05-18 17:20:07 -0700136 property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800137
Ken Sumrall29d8da82011-05-18 17:20:07 -0700138 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
139 fname = real_blk_name;
140 if ( (fd = open(fname, O_RDWR)) < 0) {
141 SLOGE("Cannot open real block device %s\n", fname);
142 return -1;
143 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800144
Ken Sumrall29d8da82011-05-18 17:20:07 -0700145 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
146 SLOGE("Cannot get size of block device %s\n", fname);
147 goto errout;
148 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800149
Ken Sumrall29d8da82011-05-18 17:20:07 -0700150 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
151 * encryption info footer and key, and plenty of bytes to spare for future
152 * growth.
153 */
154 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
155
156 if (lseek64(fd, off, SEEK_SET) == -1) {
157 SLOGE("Cannot seek to real block device footer\n");
158 goto errout;
159 }
160 } else if (key_loc[0] == '/') {
161 fname = key_loc;
162 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
163 SLOGE("Cannot open footer file %s\n", fname);
164 return -1;
165 }
166 } else {
167 SLOGE("Unexpected value for" KEY_LOC_PROP "\n");
168 return -1;;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800169 }
170
171 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
172 SLOGE("Cannot write real block device footer\n");
173 goto errout;
174 }
175
176 if (key) {
Jason parks70a4b3f2011-01-28 10:10:47 -0600177 if (crypt_ftr->keysize != KEY_LEN_BYTES) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800178 SLOGE("Keysize of %d bits not supported for real block device %s\n",
Ken Sumrall29d8da82011-05-18 17:20:07 -0700179 crypt_ftr->keysize*8, fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800180 goto errout;
181 }
182
183 if ( (cnt = write(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700184 SLOGE("Cannot write key for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800185 goto errout;
186 }
187 }
188
Ken Sumralle8744072011-01-18 22:01:55 -0800189 if (salt) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700190 /* Compute the offset from the last write to the salt */
191 off = KEY_TO_SALT_PADDING;
192 if (! key)
193 off += crypt_ftr->keysize;
Ken Sumralle8744072011-01-18 22:01:55 -0800194
Ken Sumrall29d8da82011-05-18 17:20:07 -0700195 if (lseek64(fd, off, SEEK_CUR) == -1) {
Ken Sumralle8744072011-01-18 22:01:55 -0800196 SLOGE("Cannot seek to real block device salt \n");
197 goto errout;
198 }
199
200 if ( (cnt = write(fd, salt, SALT_LEN)) != SALT_LEN) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700201 SLOGE("Cannot write salt for real block device %s\n", fname);
202 goto errout;
203 }
204 }
205
206 if (key_loc[0] == '/') {
207 if (ftruncate(fd, 0x4000)) {
208 SLOGE("Cannot set footer file sizen", fname);
Ken Sumralle8744072011-01-18 22:01:55 -0800209 goto errout;
210 }
211 }
212
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800213 /* Success! */
214 rc = 0;
215
216errout:
217 close(fd);
218 return rc;
219
220}
221
222static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
Ken Sumralle8744072011-01-18 22:01:55 -0800223 unsigned char *key, unsigned char *salt)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800224{
225 int fd;
226 unsigned int nr_sec, cnt;
227 off64_t off;
228 int rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700229 char key_loc[PROPERTY_VALUE_MAX];
230 char *fname;
231 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800232
Ken Sumrall29d8da82011-05-18 17:20:07 -0700233 property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800234
Ken Sumrall29d8da82011-05-18 17:20:07 -0700235 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
236 fname = real_blk_name;
237 if ( (fd = open(fname, O_RDONLY)) < 0) {
238 SLOGE("Cannot open real block device %s\n", fname);
239 return -1;
240 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800241
Ken Sumrall29d8da82011-05-18 17:20:07 -0700242 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
243 SLOGE("Cannot get size of block device %s\n", fname);
244 goto errout;
245 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800246
Ken Sumrall29d8da82011-05-18 17:20:07 -0700247 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
248 * encryption info footer and key, and plenty of bytes to spare for future
249 * growth.
250 */
251 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
252
253 if (lseek64(fd, off, SEEK_SET) == -1) {
254 SLOGE("Cannot seek to real block device footer\n");
255 goto errout;
256 }
257 } else if (key_loc[0] == '/') {
258 fname = key_loc;
259 if ( (fd = open(fname, O_RDONLY)) < 0) {
260 SLOGE("Cannot open footer file %s\n", fname);
261 return -1;
262 }
263
264 /* Make sure it's 16 Kbytes in length */
265 fstat(fd, &statbuf);
266 if (statbuf.st_size != 0x4000) {
267 SLOGE("footer file %s is not the expected size!\n", fname);
268 goto errout;
269 }
270 } else {
271 SLOGE("Unexpected value for" KEY_LOC_PROP "\n");
272 return -1;;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800273 }
274
275 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
276 SLOGE("Cannot read real block device footer\n");
277 goto errout;
278 }
279
280 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700281 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800282 goto errout;
283 }
284
285 if (crypt_ftr->major_version != 1) {
286 SLOGE("Cannot understand major version %d real block device footer\n",
287 crypt_ftr->major_version);
288 goto errout;
289 }
290
291 if (crypt_ftr->minor_version != 0) {
292 SLOGW("Warning: crypto footer minor version %d, expected 0, continuing...\n",
293 crypt_ftr->minor_version);
294 }
295
296 if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) {
297 /* the footer size is bigger than we expected.
298 * Skip to it's stated end so we can read the key.
299 */
300 if (lseek(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) {
301 SLOGE("Cannot seek to start of key\n");
302 goto errout;
303 }
304 }
305
Jason parks70a4b3f2011-01-28 10:10:47 -0600306 if (crypt_ftr->keysize != KEY_LEN_BYTES) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800307 SLOGE("Keysize of %d bits not supported for real block device %s\n",
Ken Sumrall29d8da82011-05-18 17:20:07 -0700308 crypt_ftr->keysize * 8, fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800309 goto errout;
310 }
311
312 if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700313 SLOGE("Cannot read key for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800314 goto errout;
315 }
316
Ken Sumralle8744072011-01-18 22:01:55 -0800317 if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) {
318 SLOGE("Cannot seek to real block device salt\n");
319 goto errout;
320 }
321
322 if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700323 SLOGE("Cannot read salt for real block device %s\n", fname);
Ken Sumralle8744072011-01-18 22:01:55 -0800324 goto errout;
325 }
326
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800327 /* Success! */
328 rc = 0;
329
330errout:
331 close(fd);
332 return rc;
333}
334
335/* Convert a binary key of specified length into an ascii hex string equivalent,
336 * without the leading 0x and with null termination
337 */
338void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
339 char *master_key_ascii)
340{
341 unsigned int i, a;
342 unsigned char nibble;
343
344 for (i=0, a=0; i<keysize; i++, a+=2) {
345 /* For each byte, write out two ascii hex digits */
346 nibble = (master_key[i] >> 4) & 0xf;
347 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
348
349 nibble = master_key[i] & 0xf;
350 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
351 }
352
353 /* Add the null termination */
354 master_key_ascii[a] = '\0';
355
356}
357
358static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -0700359 char *real_blk_name, char *crypto_blk_name, const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800360{
361 char buffer[DM_CRYPT_BUF_SIZE];
362 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
363 char *crypt_params;
364 struct dm_ioctl *io;
365 struct dm_target_spec *tgt;
366 unsigned int minor;
367 int fd;
368 int retval = -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800369
370 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
371 SLOGE("Cannot open device-mapper\n");
372 goto errout;
373 }
374
375 io = (struct dm_ioctl *) buffer;
376
377 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
378 if (ioctl(fd, DM_DEV_CREATE, io)) {
379 SLOGE("Cannot create dm-crypt device\n");
380 goto errout;
381 }
382
383 /* Get the device status, in particular, the name of it's device file */
384 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
385 if (ioctl(fd, DM_DEV_STATUS, io)) {
386 SLOGE("Cannot retrieve dm-crypt device status\n");
387 goto errout;
388 }
389 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
390 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
391
392 /* Load the mapping table for this device */
393 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
394
395 ioctl_init(io, 4096, name, 0);
396 io->target_count = 1;
397 tgt->status = 0;
398 tgt->sector_start = 0;
399 tgt->length = crypt_ftr->fs_size;
400 strcpy(tgt->target_type, "crypt");
401
402 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
403 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
404 sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name,
405 master_key_ascii, real_blk_name);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800406 crypt_params += strlen(crypt_params) + 1;
407 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
408 tgt->next = crypt_params - buffer;
409
410 if (ioctl(fd, DM_TABLE_LOAD, io)) {
411 SLOGE("Cannot load dm-crypt mapping table.\n");
412 goto errout;
413 }
414
415 /* Resume this device to activate it */
416 ioctl_init(io, 4096, name, 0);
417
418 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
419 SLOGE("Cannot resume the dm-crypt device\n");
420 goto errout;
421 }
422
423 /* We made it here with no errors. Woot! */
424 retval = 0;
425
426errout:
427 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
428
429 return retval;
430}
431
Ken Sumrall29d8da82011-05-18 17:20:07 -0700432static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800433{
434 int fd;
435 char buffer[DM_CRYPT_BUF_SIZE];
436 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800437 int retval = -1;
438
439 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
440 SLOGE("Cannot open device-mapper\n");
441 goto errout;
442 }
443
444 io = (struct dm_ioctl *) buffer;
445
446 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
447 if (ioctl(fd, DM_DEV_REMOVE, io)) {
448 SLOGE("Cannot remove dm-crypt device\n");
449 goto errout;
450 }
451
452 /* We made it here with no errors. Woot! */
453 retval = 0;
454
455errout:
456 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
457
458 return retval;
459
460}
461
Ken Sumralle8744072011-01-18 22:01:55 -0800462static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800463{
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800464 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800465 PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800466 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800467}
468
Ken Sumralle8744072011-01-18 22:01:55 -0800469static int encrypt_master_key(char *passwd, unsigned char *salt,
470 unsigned char *decrypted_master_key,
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800471 unsigned char *encrypted_master_key)
472{
473 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
474 EVP_CIPHER_CTX e_ctx;
475 int encrypted_len, final_len;
476
477 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800478 pbkdf2(passwd, salt, ikey);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800479
480 /* Initialize the decryption engine */
481 if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
482 SLOGE("EVP_EncryptInit failed\n");
483 return -1;
484 }
485 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800486
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800487 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800488 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
489 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800490 SLOGE("EVP_EncryptUpdate failed\n");
491 return -1;
492 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800493 if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800494 SLOGE("EVP_EncryptFinal failed\n");
495 return -1;
496 }
497
498 if (encrypted_len + final_len != KEY_LEN_BYTES) {
499 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
500 return -1;
501 } else {
502 return 0;
503 }
504}
505
Ken Sumralle8744072011-01-18 22:01:55 -0800506static int decrypt_master_key(char *passwd, unsigned char *salt,
507 unsigned char *encrypted_master_key,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800508 unsigned char *decrypted_master_key)
509{
510 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 -0800511 EVP_CIPHER_CTX d_ctx;
512 int decrypted_len, final_len;
513
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800514 /* Turn the password into a key and IV that can decrypt the master key */
Ken Sumralle8744072011-01-18 22:01:55 -0800515 pbkdf2(passwd, salt, ikey);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800516
517 /* Initialize the decryption engine */
518 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
519 return -1;
520 }
521 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
522 /* Decrypt the master key */
523 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
524 encrypted_master_key, KEY_LEN_BYTES)) {
525 return -1;
526 }
527 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
528 return -1;
529 }
530
531 if (decrypted_len + final_len != KEY_LEN_BYTES) {
532 return -1;
533 } else {
534 return 0;
535 }
536}
537
Ken Sumralle8744072011-01-18 22:01:55 -0800538static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt)
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800539{
540 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -0800541 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800542 EVP_CIPHER_CTX e_ctx;
543 int encrypted_len, final_len;
544
545 /* Get some random bits for a key */
546 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -0800547 read(fd, key_buf, sizeof(key_buf));
548 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800549 close(fd);
550
551 /* Now encrypt it with the password */
Ken Sumralle8744072011-01-18 22:01:55 -0800552 return encrypt_master_key(passwd, salt, key_buf, master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800553}
554
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800555static int get_orig_mount_parms(char *mount_point, char *fs_type, char *real_blkdev,
556 unsigned long *mnt_flags, char *fs_options)
557{
Ken Sumrall29d8da82011-05-18 17:20:07 -0700558 char mount_point2[PROPERTY_VALUE_MAX];
559 char fs_flags[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800560
561 property_get("ro.crypto.fs_type", fs_type, "");
562 property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
563 property_get("ro.crypto.fs_mnt_point", mount_point2, "");
564 property_get("ro.crypto.fs_options", fs_options, "");
565 property_get("ro.crypto.fs_flags", fs_flags, "");
566 *mnt_flags = strtol(fs_flags, 0, 0);
567
568 if (strcmp(mount_point, mount_point2)) {
569 /* Consistency check. These should match. If not, something odd happened. */
570 return -1;
571 }
572
573 return 0;
574}
575
576static int wait_and_unmount(char *mountpoint)
577{
578 int i, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -0800579#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800580
581 /* Now umount the tmpfs filesystem */
582 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
583 if (umount(mountpoint)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700584 if (errno == EINVAL) {
585 /* EINVAL is returned if the directory is not a mountpoint,
586 * i.e. there is no filesystem mounted there. So just get out.
587 */
588 break;
589 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800590 sleep(1);
591 i++;
592 } else {
593 break;
594 }
595 }
596
597 if (i < WAIT_UNMOUNT_COUNT) {
598 SLOGD("unmounting %s succeeded\n", mountpoint);
599 rc = 0;
600 } else {
601 SLOGE("unmounting %s failed\n", mountpoint);
602 rc = -1;
603 }
604
605 return rc;
606}
607
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800608#define DATA_PREP_TIMEOUT 100
609static int prep_data_fs(void)
610{
611 int i;
612
613 /* Do the prep of the /data filesystem */
614 property_set("vold.post_fs_data_done", "0");
615 property_set("vold.decrypt", "trigger_post_fs_data");
616 SLOGD("Just triggered post_fs_data\n");
617
618 /* Wait a max of 25 seconds, hopefully it takes much less */
619 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700620 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800621
622 property_get("vold.post_fs_data_done", p, "0");
623 if (*p == '1') {
624 break;
625 } else {
626 usleep(250000);
627 }
628 }
629 if (i == DATA_PREP_TIMEOUT) {
630 /* Ugh, we failed to prep /data in time. Bail. */
631 return -1;
632 } else {
633 SLOGD("post_fs_data done\n");
634 return 0;
635 }
636}
637
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800638int cryptfs_restart(void)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800639{
640 char fs_type[32];
641 char real_blkdev[MAXPATHLEN];
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800642 char crypto_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800643 char fs_options[256];
644 unsigned long mnt_flags;
645 struct stat statbuf;
646 int rc = -1, i;
Ken Sumrall0cc16632011-01-18 20:32:26 -0800647 static int restart_successful = 0;
648
649 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -0600650 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -0800651 SLOGE("Encrypted filesystem not validated, aborting");
652 return -1;
653 }
654
655 if (restart_successful) {
656 SLOGE("System already restarted with encrypted disk, aborting");
657 return -1;
658 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800659
660 /* Here is where we shut down the framework. The init scripts
661 * start all services in one of three classes: core, main or late_start.
662 * On boot, we start core and main. Now, we stop main, but not core,
663 * as core includes vold and a few other really important things that
664 * we need to keep running. Once main has stopped, we should be able
665 * to umount the tmpfs /data, then mount the encrypted /data.
666 * We then restart the class main, and also the class late_start.
667 * At the moment, I've only put a few things in late_start that I know
668 * are not needed to bring up the framework, and that also cause problems
669 * with unmounting the tmpfs /data, but I hope to add add more services
670 * to the late_start class as we optimize this to decrease the delay
671 * till the user is asked for the password to the filesystem.
672 */
673
674 /* The init files are setup to stop the class main when vold.decrypt is
675 * set to trigger_reset_main.
676 */
677 property_set("vold.decrypt", "trigger_reset_main");
678 SLOGD("Just asked init to shut down class main\n");
679
680 /* Now that the framework is shutdown, we should be able to umount()
681 * the tmpfs filesystem, and mount the real one.
682 */
683
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800684 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
685 if (strlen(crypto_blkdev) == 0) {
686 SLOGE("fs_crypto_blkdev not set\n");
687 return -1;
688 }
689
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800690 if (! get_orig_mount_parms(DATA_MNT_POINT, fs_type, real_blkdev, &mnt_flags, fs_options)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800691 SLOGD("Just got orig mount parms\n");
692
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800693 if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800694 /* If that succeeded, then mount the decrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800695 mount(crypto_blkdev, DATA_MNT_POINT, fs_type, mnt_flags, fs_options);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800696
Ken Sumrallad2ac332011-03-08 17:07:06 -0800697 property_set("vold.decrypt", "trigger_load_persist_props");
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800698 /* Create necessary paths on /data */
699 if (prep_data_fs()) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800700 return -1;
701 }
702
703 /* startup service classes main and late_start */
704 property_set("vold.decrypt", "trigger_restart_framework");
705 SLOGD("Just triggered restart_framework\n");
706
707 /* Give it a few moments to get started */
708 sleep(1);
709 }
710 }
711
Ken Sumrall0cc16632011-01-18 20:32:26 -0800712 if (rc == 0) {
713 restart_successful = 1;
714 }
715
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800716 return rc;
717}
718
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800719static int do_crypto_complete(char *mount_point)
720{
721 struct crypt_mnt_ftr crypt_ftr;
722 unsigned char encrypted_master_key[32];
723 unsigned char salt[SALT_LEN];
724 char real_blkdev[MAXPATHLEN];
Ken Sumrall29d8da82011-05-18 17:20:07 -0700725 char fs_type[PROPERTY_VALUE_MAX];
726 char fs_options[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800727 unsigned long mnt_flags;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700728 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800729
730 property_get("ro.crypto.state", encrypted_state, "");
731 if (strcmp(encrypted_state, "encrypted") ) {
732 SLOGE("not running with encryption, aborting");
733 return 1;
734 }
735
736 if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
737 SLOGE("Error reading original mount parms for mount point %s\n", mount_point);
738 return -1;
739 }
740
741 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
742 SLOGE("Error getting crypt footer and key\n");
743 return -1;
744 }
745
746 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
747 SLOGE("Encryption process didn't finish successfully\n");
748 return -2; /* -2 is the clue to the UI that there is no usable data on the disk,
749 * and give the user an option to wipe the disk */
750 }
751
752 /* We passed the test! We shall diminish, and return to the west */
753 return 0;
754}
755
Ken Sumrall29d8da82011-05-18 17:20:07 -0700756static int test_mount_encrypted_fs(char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800757{
758 struct crypt_mnt_ftr crypt_ftr;
759 /* Allocate enough space for a 256 bit key, but we may use less */
760 unsigned char encrypted_master_key[32], decrypted_master_key[32];
Ken Sumralle8744072011-01-18 22:01:55 -0800761 unsigned char salt[SALT_LEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800762 char crypto_blkdev[MAXPATHLEN];
763 char real_blkdev[MAXPATHLEN];
Ken Sumrall29d8da82011-05-18 17:20:07 -0700764 char fs_type[PROPERTY_VALUE_MAX];
765 char fs_options[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800766 char tmp_mount_point[64];
767 unsigned long mnt_flags;
768 unsigned int orig_failed_decrypt_count;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700769 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800770 int rc;
771
Ken Sumrall0cc16632011-01-18 20:32:26 -0800772 property_get("ro.crypto.state", encrypted_state, "");
Jason parks70a4b3f2011-01-28 10:10:47 -0600773 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
Ken Sumrall0cc16632011-01-18 20:32:26 -0800774 SLOGE("encrypted fs already validated or not running with encryption, aborting");
775 return -1;
776 }
777
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800778 if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
779 SLOGE("Error reading original mount parms for mount point %s\n", mount_point);
780 return -1;
781 }
782
Ken Sumralle8744072011-01-18 22:01:55 -0800783 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800784 SLOGE("Error getting crypt footer and key\n");
785 return -1;
786 }
Ken Sumralld33d4172011-02-01 00:49:13 -0800787
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800788 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size);
789 orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count;
790
791 if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Ken Sumralle8744072011-01-18 22:01:55 -0800792 decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800793 }
794
795 if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -0700796 real_blkdev, crypto_blkdev, label)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800797 SLOGE("Error creating decrypted block device\n");
798 return -1;
799 }
800
801 /* If init detects an encrypted filesystme, it writes a file for each such
802 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
803 * files and passes that data to me */
804 /* Create a tmp mount point to try mounting the decryptd fs
805 * Since we're here, the mount_point should be a tmpfs filesystem, so make
806 * a directory in it to test mount the decrypted filesystem.
807 */
808 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
809 mkdir(tmp_mount_point, 0755);
810 if ( mount(crypto_blkdev, tmp_mount_point, "ext4", MS_RDONLY, "") ) {
811 SLOGE("Error temp mounting decrypted block device\n");
Ken Sumrall29d8da82011-05-18 17:20:07 -0700812 delete_crypto_blk_dev(label);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800813 crypt_ftr.failed_decrypt_count++;
814 } else {
815 /* Success, so just umount and we'll mount it properly when we restart
816 * the framework.
817 */
818 umount(tmp_mount_point);
819 crypt_ftr.failed_decrypt_count = 0;
820 }
821
822 if (orig_failed_decrypt_count != crypt_ftr.failed_decrypt_count) {
Ken Sumralle8744072011-01-18 22:01:55 -0800823 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800824 }
825
826 if (crypt_ftr.failed_decrypt_count) {
827 /* We failed to mount the device, so return an error */
828 rc = crypt_ftr.failed_decrypt_count;
829
830 } else {
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800831 /* Woot! Success! Save the name of the crypto block device
832 * so we can mount it when restarting the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800833 */
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800834 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -0600835
836 /* Also save a the master key so we can reencrypted the key
837 * the key when we want to change the password on it.
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800838 */
Jason parks70a4b3f2011-01-28 10:10:47 -0600839 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall29d8da82011-05-18 17:20:07 -0700840 saved_data_blkdev = strdup(real_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -0600841 master_key_saved = 1;
Ken Sumrall6864b7e2011-01-14 15:20:02 -0800842 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800843 }
844
845 return rc;
846}
847
Ken Sumrall29d8da82011-05-18 17:20:07 -0700848/*
849 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
850 * Setup a dm-crypt mapping, use the saved master key from
851 * setting up the /data mapping, and return the new device path.
852 */
853int cryptfs_setup_volume(const char *label, int major, int minor,
854 char *crypto_sys_path, unsigned int max_path,
855 int *new_major, int *new_minor)
856{
857 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
858 struct crypt_mnt_ftr sd_crypt_ftr;
859 unsigned char key[32], salt[32];
860 struct stat statbuf;
861 int nr_sec, fd;
862
863 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
864
865 /* Just want the footer, but gotta get it all */
866 get_crypt_ftr_and_key(saved_data_blkdev, &sd_crypt_ftr, key, salt);
867
868 /* Update the fs_size field to be the size of the volume */
869 fd = open(real_blkdev, O_RDONLY);
870 nr_sec = get_blkdev_size(fd);
871 close(fd);
872 if (nr_sec == 0) {
873 SLOGE("Cannot get size of volume %s\n", real_blkdev);
874 return -1;
875 }
876
877 sd_crypt_ftr.fs_size = nr_sec;
878 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
879 crypto_blkdev, label);
880
881 stat(crypto_blkdev, &statbuf);
882 *new_major = MAJOR(statbuf.st_rdev);
883 *new_minor = MINOR(statbuf.st_rdev);
884
885 /* Create path to sys entry for this block device */
886 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
887
888 return 0;
889}
890
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -0800891int cryptfs_crypto_complete(void)
892{
893 return do_crypto_complete("/data");
894}
895
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800896int cryptfs_check_passwd(char *passwd)
897{
898 int rc = -1;
899
Ken Sumrall29d8da82011-05-18 17:20:07 -0700900 rc = test_mount_encrypted_fs(passwd, DATA_MNT_POINT, "userdata");
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800901
902 return rc;
903}
904
905/* Initialize a crypt_mnt_ftr structure. The keysize is
906 * defaulted to 16 bytes, and the filesystem size to 0.
907 * Presumably, at a minimum, the caller will update the
908 * filesystem size and crypto_type_name after calling this function.
909 */
910static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
911{
912 ftr->magic = CRYPT_MNT_MAGIC;
913 ftr->major_version = 1;
914 ftr->minor_version = 0;
915 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
916 ftr->flags = 0;
Jason parks70a4b3f2011-01-28 10:10:47 -0600917 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800918 ftr->spare1 = 0;
919 ftr->fs_size = 0;
920 ftr->failed_decrypt_count = 0;
921 ftr->crypto_type_name[0] = '\0';
922}
923
Ken Sumrall29d8da82011-05-18 17:20:07 -0700924static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800925{
926 char cmdline[256];
927 int rc = -1;
928
Ken Sumrall29d8da82011-05-18 17:20:07 -0700929 if (type == EXT4_FS) {
930 snprintf(cmdline, sizeof(cmdline), "/system/bin/make_ext4fs -a /data -l %lld %s",
931 size * 512, crypto_blkdev);
932 SLOGI("Making empty filesystem with command %s\n", cmdline);
933 } else if (type== FAT_FS) {
934 snprintf(cmdline, sizeof(cmdline), "/system/bin/newfs_msdos -F 32 -O android -c 8 -s %lld %s",
935 size, crypto_blkdev);
936 SLOGI("Making empty filesystem with command %s\n", cmdline);
937 } else {
938 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
939 return -1;
940 }
941
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800942 if (system(cmdline)) {
943 SLOGE("Error creating empty filesystem on %s\n", crypto_blkdev);
944 } else {
945 SLOGD("Successfully created empty filesystem on %s\n", crypto_blkdev);
946 rc = 0;
947 }
948
949 return rc;
950}
951
952static inline int unix_read(int fd, void* buff, int len)
953{
954 int ret;
955 do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR);
956 return ret;
957}
958
959static inline int unix_write(int fd, const void* buff, int len)
960{
961 int ret;
962 do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR);
963 return ret;
964}
965
966#define CRYPT_INPLACE_BUFSIZE 4096
967#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512)
Ken Sumrall29d8da82011-05-18 17:20:07 -0700968static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, off64_t size,
969 off64_t *size_already_done, off64_t tot_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800970{
971 int realfd, cryptofd;
972 char *buf[CRYPT_INPLACE_BUFSIZE];
973 int rc = -1;
974 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800975 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700976 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800977
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800978 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
979 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
980 return -1;
981 }
982
983 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
984 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
985 close(realfd);
986 return -1;
987 }
988
989 /* This is pretty much a simple loop of reading 4K, and writing 4K.
990 * The size passed in is the number of 512 byte sectors in the filesystem.
991 * So compute the number of whole 4K blocks we should read/write,
992 * and the remainder.
993 */
994 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
995 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700996 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
997 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800998
999 SLOGE("Encrypting filesystem in place...");
1000
Ken Sumrall29d8da82011-05-18 17:20:07 -07001001 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001002 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001003 /* process the majority of the filesystem in blocks */
1004 for (i=0; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001005 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001006 if (new_pct > cur_pct) {
1007 char buf[8];
1008
1009 cur_pct = new_pct;
1010 snprintf(buf, sizeof(buf), "%lld", cur_pct);
1011 property_set("vold.encrypt_progress", buf);
1012 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001013 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1014 SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1015 goto errout;
1016 }
1017 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1018 SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1019 goto errout;
1020 }
1021 }
1022
1023 /* Do any remaining sectors */
1024 for (i=0; i<remainder; i++) {
1025 if (unix_read(realfd, buf, 512) <= 0) {
1026 SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1027 goto errout;
1028 }
1029 if (unix_write(cryptofd, buf, 512) <= 0) {
1030 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1031 goto errout;
1032 }
1033 }
1034
Ken Sumrall29d8da82011-05-18 17:20:07 -07001035 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001036 rc = 0;
1037
1038errout:
1039 close(realfd);
1040 close(cryptofd);
1041
1042 return rc;
1043}
1044
1045#define CRYPTO_ENABLE_WIPE 1
1046#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001047
1048#define FRAMEWORK_BOOT_WAIT 60
1049
Ken Sumrall29d8da82011-05-18 17:20:07 -07001050static inline int should_encrypt(struct volume_info *volume)
1051{
1052 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
1053 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
1054}
1055
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001056int cryptfs_enable(char *howarg, char *passwd)
1057{
1058 int how = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001059 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN], sd_crypto_blkdev[MAXPATHLEN];
1060 char fs_type[PROPERTY_VALUE_MAX], fs_options[PROPERTY_VALUE_MAX],
1061 mount_point[PROPERTY_VALUE_MAX];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001062 unsigned long mnt_flags, nr_sec;
Jason parks70a4b3f2011-01-28 10:10:47 -06001063 unsigned char master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
Ken Sumralle8744072011-01-18 22:01:55 -08001064 unsigned char salt[SALT_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001065 int rc=-1, fd, i;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001066 struct crypt_mnt_ftr crypt_ftr, sd_crypt_ftr;;
1067 char tmpfs_options[PROPERTY_VALUE_MAX];
1068 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001069 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07001070 char key_loc[PROPERTY_VALUE_MAX];
1071 char fuse_sdcard[PROPERTY_VALUE_MAX];
1072 char *sd_mnt_point;
1073 char sd_blk_dev[256] = { 0 };
1074 int num_vols;
1075 struct volume_info *vol_list = 0;
1076 off64_t cur_encryption_done=0, tot_encryption_size=0;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001077
1078 property_get("ro.crypto.state", encrypted_state, "");
1079 if (strcmp(encrypted_state, "unencrypted")) {
1080 SLOGE("Device is already running encrypted, aborting");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001081 goto error_unencrypted;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001082 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001083
Ken Sumrall29d8da82011-05-18 17:20:07 -07001084 property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
1085
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001086 if (!strcmp(howarg, "wipe")) {
1087 how = CRYPTO_ENABLE_WIPE;
1088 } else if (! strcmp(howarg, "inplace")) {
1089 how = CRYPTO_ENABLE_INPLACE;
1090 } else {
1091 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08001092 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001093 }
1094
1095 get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options);
1096
Ken Sumrall3ed82362011-01-28 23:31:16 -08001097 /* Get the size of the real block device */
1098 fd = open(real_blkdev, O_RDONLY);
1099 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
1100 SLOGE("Cannot get size of block device %s\n", real_blkdev);
1101 goto error_unencrypted;
1102 }
1103 close(fd);
1104
1105 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001106 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001107 unsigned int fs_size_sec, max_fs_size_sec;
1108
1109 fs_size_sec = get_fs_size(real_blkdev);
1110 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1111
1112 if (fs_size_sec > max_fs_size_sec) {
1113 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
1114 goto error_unencrypted;
1115 }
1116 }
1117
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001118 /* Get a wakelock as this may take a while, and we don't want the
1119 * device to sleep on us. We'll grab a partial wakelock, and if the UI
1120 * wants to keep the screen on, it can grab a full wakelock.
1121 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001122 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001123 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
1124
Ken Sumrall29d8da82011-05-18 17:20:07 -07001125 /* Get the sdcard mount point */
1126 sd_mnt_point = getenv("EXTERNAL_STORAGE");
1127 if (! sd_mnt_point) {
1128 sd_mnt_point = "/mnt/sdcard";
1129 }
1130
1131 num_vols=vold_getNumDirectVolumes();
1132 vol_list = malloc(sizeof(struct volume_info) * num_vols);
1133 vold_getDirectVolumeList(vol_list);
1134
1135 for (i=0; i<num_vols; i++) {
1136 if (should_encrypt(&vol_list[i])) {
1137 fd = open(vol_list[i].blk_dev, O_RDONLY);
1138 if ( (vol_list[i].size = get_blkdev_size(fd)) == 0) {
1139 SLOGE("Cannot get size of block device %s\n", vol_list[i].blk_dev);
1140 goto error_unencrypted;
1141 }
1142 close(fd);
1143
1144 if (vold_unmountVol(vol_list[i].label)) {
1145 SLOGE("Failed to unmount volume %s\n", vol_list[i].label);
1146 goto error_unencrypted;
1147 }
1148 }
1149 }
1150
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001151 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001152 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001153 */
1154 property_set("vold.decrypt", "trigger_shutdown_framework");
1155 SLOGD("Just asked init to shut down class main\n");
1156
Ken Sumrall29d8da82011-05-18 17:20:07 -07001157 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
1158 if (!strcmp(fuse_sdcard, "true")) {
1159 /* This is a device using the fuse layer to emulate the sdcard semantics
1160 * on top of the userdata partition. vold does not manage it, it is managed
1161 * by the sdcard service. The sdcard service was killed by the property trigger
1162 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
1163 * unlike the case for vold managed devices above.
1164 */
1165 if (wait_and_unmount(sd_mnt_point)) {
1166 goto error_shutting_down;
1167 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001168 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001169
1170 /* Now unmount the /data partition. */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001171 if (wait_and_unmount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001172 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001173 }
1174
1175 /* Do extra work for a better UX when doing the long inplace encryption */
1176 if (how == CRYPTO_ENABLE_INPLACE) {
1177 /* Now that /data is unmounted, we need to mount a tmpfs
1178 * /data, set a property saying we're doing inplace encryption,
1179 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001180 */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001181 property_get("ro.crypto.tmpfs_options", tmpfs_options, "");
1182 if (mount("tmpfs", DATA_MNT_POINT, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV,
1183 tmpfs_options) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001184 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001185 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001186 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08001187 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001188
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001189 /* restart the framework. */
1190 /* Create necessary paths on /data */
1191 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001192 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001193 }
1194
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001195 /* startup service classes main and late_start */
1196 property_set("vold.decrypt", "trigger_restart_min_framework");
1197 SLOGD("Just triggered restart_min_framework\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001198
Ken Sumrall7df84122011-01-18 14:04:08 -08001199 /* OK, the framework is restarted and will soon be showing a
1200 * progress bar. Time to setup an encrypted mapping, and
1201 * either write a new filesystem, or encrypt in place updating
1202 * the progress bar as we work.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001203 */
1204 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001205
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001206 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001207 /* Initialize a crypt_mnt_ftr for the partition */
1208 cryptfs_init_crypt_mnt_ftr(&crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001209 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
1210 crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1211 } else {
1212 crypt_ftr.fs_size = nr_sec;
1213 }
Ken Sumralld33d4172011-02-01 00:49:13 -08001214 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001215 strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
1216
1217 /* Make an encrypted master key */
Ken Sumralle8744072011-01-18 22:01:55 -08001218 if (create_encrypted_random_key(passwd, master_key, salt)) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001219 SLOGE("Cannot create encrypted master key\n");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001220 goto error_unencrypted;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001221 }
1222
1223 /* Write the key to the end of the partition */
Ken Sumralle8744072011-01-18 22:01:55 -08001224 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, master_key, salt);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001225
Ken Sumralle8744072011-01-18 22:01:55 -08001226 decrypt_master_key(passwd, salt, master_key, decrypted_master_key);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001227 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
1228 "userdata");
1229
1230 /* setup crypto mapping for all encryptable volumes handled by vold */
1231 for (i=0; i<num_vols; i++) {
1232 if (should_encrypt(&vol_list[i])) {
1233 vol_list[i].crypt_ftr = crypt_ftr; /* gotta love struct assign */
1234 vol_list[i].crypt_ftr.fs_size = vol_list[i].size;
1235 create_crypto_blk_dev(&vol_list[i].crypt_ftr, decrypted_master_key,
1236 vol_list[i].blk_dev, vol_list[i].crypto_blkdev,
1237 vol_list[i].label);
1238 tot_encryption_size += vol_list[i].size;
1239 }
1240 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001241
1242 if (how == CRYPTO_ENABLE_WIPE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001243 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr.fs_size, EXT4_FS);
1244 /* Encrypt all encryptable volumes handled by vold */
1245 if (!rc) {
1246 for (i=0; i<num_vols; i++) {
1247 if (should_encrypt(&vol_list[i])) {
1248 rc = cryptfs_enable_wipe(vol_list[i].crypto_blkdev,
1249 vol_list[i].crypt_ftr.fs_size, FAT_FS);
1250 }
1251 }
1252 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001253 } else if (how == CRYPTO_ENABLE_INPLACE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001254 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size,
1255 &cur_encryption_done, tot_encryption_size);
1256 /* Encrypt all encryptable volumes handled by vold */
1257 if (!rc) {
1258 for (i=0; i<num_vols; i++) {
1259 if (should_encrypt(&vol_list[i])) {
1260 rc = cryptfs_enable_inplace(vol_list[i].crypto_blkdev,
1261 vol_list[i].blk_dev,
1262 vol_list[i].crypt_ftr.fs_size,
1263 &cur_encryption_done, tot_encryption_size);
1264 }
1265 }
1266 }
1267 if (!rc) {
1268 /* The inplace routine never actually sets the progress to 100%
1269 * due to the round down nature of integer division, so set it here */
1270 property_set("vold.encrypt_progress", "100");
1271 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001272 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001273 /* Shouldn't happen */
1274 SLOGE("cryptfs_enable: internal error, unknown option\n");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001275 goto error_unencrypted;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001276 }
1277
1278 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001279 delete_crypto_blk_dev("userdata");
1280 for (i=0; i<num_vols; i++) {
1281 if (should_encrypt(&vol_list[i])) {
1282 delete_crypto_blk_dev(vol_list[i].label);
1283 }
1284 }
1285
1286 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001287
1288 if (! rc) {
1289 /* Success */
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001290
Ken Sumralld33d4172011-02-01 00:49:13 -08001291 /* Clear the encryption in progres flag in the footer */
1292 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
1293 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, 0, 0);
1294
Ken Sumrall29d8da82011-05-18 17:20:07 -07001295 sleep(2); /* Give the UI a chance to show 100% progress */
Ken Sumrallc290eaf2011-03-07 23:40:35 -08001296 android_reboot(ANDROID_RB_RESTART, 0, 0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001297 } else {
1298 property_set("vold.encrypt_progress", "error_partially_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001299 release_wake_lock(lockid);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001300 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001301 }
1302
Ken Sumrall3ed82362011-01-28 23:31:16 -08001303 /* hrm, the encrypt step claims success, but the reboot failed.
1304 * This should not happen.
1305 * Set the property and return. Hope the framework can deal with it.
1306 */
1307 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001308 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001309 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08001310
1311error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07001312 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001313 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001314 if (lockid[0]) {
1315 release_wake_lock(lockid);
1316 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001317 return -1;
1318
1319error_shutting_down:
1320 /* we failed, and have not encrypted anthing, so the users's data is still intact,
1321 * but the framework is stopped and not restarted to show the error, so it's up to
1322 * vold to restart the system.
1323 */
1324 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Ken Sumrallc290eaf2011-03-07 23:40:35 -08001325 android_reboot(ANDROID_RB_RESTART, 0, 0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08001326
1327 /* shouldn't get here */
1328 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07001329 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001330 if (lockid[0]) {
1331 release_wake_lock(lockid);
1332 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08001333 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001334}
1335
Jason parks70a4b3f2011-01-28 10:10:47 -06001336int cryptfs_changepw(char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001337{
1338 struct crypt_mnt_ftr crypt_ftr;
Jason parks70a4b3f2011-01-28 10:10:47 -06001339 unsigned char encrypted_master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
Ken Sumralle8744072011-01-18 22:01:55 -08001340 unsigned char salt[SALT_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001341 char real_blkdev[MAXPATHLEN];
1342
1343 /* This is only allowed after we've successfully decrypted the master key */
Jason parks70a4b3f2011-01-28 10:10:47 -06001344 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001345 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001346 return -1;
1347 }
1348
1349 property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
1350 if (strlen(real_blkdev) == 0) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001351 SLOGE("Can't find real blkdev");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001352 return -1;
1353 }
1354
1355 /* get key */
Ken Sumralle8744072011-01-18 22:01:55 -08001356 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Ken Sumrall57b63e62011-01-17 18:29:19 -08001357 SLOGE("Error getting crypt footer and key");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001358 return -1;
1359 }
1360
Jason parks70a4b3f2011-01-28 10:10:47 -06001361 encrypt_master_key(newpw, salt, saved_master_key, encrypted_master_key);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001362
Jason parks70a4b3f2011-01-28 10:10:47 -06001363 /* save the key */
1364 put_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001365
1366 return 0;
1367}