blob: 4e5706b64300712c3e52e71a908569c5982838f3 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
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>
Dees Troy4dff2e62013-11-10 04:11:43 +000024#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <sys/stat.h>
26#include <fcntl.h>
27#include <unistd.h>
28#include <stdio.h>
29#include <sys/ioctl.h>
30#include <linux/dm-ioctl.h>
31#include <libgen.h>
32#include <stdlib.h>
33#include <sys/param.h>
34#include <string.h>
35#include <sys/mount.h>
36#include <openssl/evp.h>
37#include <openssl/sha.h>
38#include <errno.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include <ext4.h>
40#include <linux/kdev_t.h>
Dees Troy4dff2e62013-11-10 04:11:43 +000041#include <fs_mgr.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040042#include "cryptfs.h"
43#define LOG_TAG "Cryptfs"
Dees_Troy51a0e822012-09-05 15:24:24 -040044#include "cutils/log.h"
45#include "cutils/properties.h"
Dees Troy4dff2e62013-11-10 04:11:43 +000046#include "cutils/android_reboot.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040047#include "hardware_legacy/power.h"
Dees Troy4dff2e62013-11-10 04:11:43 +000048/*#include <logwrap/logwrap.h>
49#include "VolumeManager.h"
50#include "VoldUtil.h"*/
51#include "crypto_scrypt.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040052
53#define DM_CRYPT_BUF_SIZE 4096
54#define DATA_MNT_POINT "/data"
55
56#define HASH_COUNT 2000
57#define KEY_LEN_BYTES 16
58#define IV_LEN_BYTES 16
59
60#define KEY_IN_FOOTER "footer"
61
62#define EXT4_FS 1
63#define FAT_FS 2
64
Dees Troy4dff2e62013-11-10 04:11:43 +000065#define TABLE_LOAD_RETRIES 10
66
Dees_Troy51a0e822012-09-05 15:24:24 -040067char *me = "cryptfs";
68
69static unsigned char saved_master_key[KEY_LEN_BYTES];
Dees_Troy51a0e822012-09-05 15:24:24 -040070static char *saved_mount_point;
71static int master_key_saved = 0;
Dees Troy4dff2e62013-11-10 04:11:43 +000072static struct crypt_persist_data *persist_data = NULL;
73
74struct fstab *fstab;
75
76static void cryptfs_reboot(int recovery)
77{
78 /*if (recovery) {
79 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
80 } else {
81 property_set(ANDROID_RB_PROPERTY, "reboot");
82 }
83 sleep(20);*/
84
85 /* Shouldn't get here, reboot should happen before sleep times out */
86 return;
87}
Dees_Troy51a0e822012-09-05 15:24:24 -040088
89static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
90{
91 memset(io, 0, dataSize);
92 io->data_size = dataSize;
93 io->data_start = sizeof(struct dm_ioctl);
94 io->version[0] = 4;
95 io->version[1] = 0;
96 io->version[2] = 0;
97 io->flags = flags;
98 if (name) {
99 strncpy(io->name, name, sizeof(io->name));
100 }
101}
102
Dees Troy4dff2e62013-11-10 04:11:43 +0000103/**
104 * Gets the default device scrypt parameters for key derivation time tuning.
105 * The parameters should lead to about one second derivation time for the
106 * given device.
107 */
108static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
109 const int default_params[] = SCRYPT_DEFAULTS;
110 int params[] = SCRYPT_DEFAULTS;
111 char paramstr[PROPERTY_VALUE_MAX];
112 char *token;
113 char *saveptr;
114 int i;
115
116 property_get(SCRYPT_PROP, paramstr, "");
117 if (paramstr[0] != '\0') {
118 /*
119 * The token we're looking for should be three integers separated by
120 * colons (e.g., "12:8:1"). Scan the property to make sure it matches.
121 */
122 for (i = 0, token = strtok_r(paramstr, ":", &saveptr);
123 token != NULL && i < 3;
124 i++, token = strtok_r(NULL, ":", &saveptr)) {
125 char *endptr;
126 params[i] = strtol(token, &endptr, 10);
127
128 /*
129 * Check that there was a valid number and it's 8-bit. If not,
130 * break out and the end check will take the default values.
131 */
132 if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) {
133 break;
134 }
135 }
136
137 /*
138 * If there were not enough tokens or a token was malformed (not an
139 * integer), it will end up here and the default parameters can be
140 * taken.
141 */
142 if ((i != 3) || (token != NULL)) {
143 printf("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
144 memcpy(params, default_params, sizeof(params));
145 }
146 }
147
148 ftr->N_factor = params[0];
149 ftr->r_factor = params[1];
150 ftr->p_factor = params[2];
151}
152
Dees_Troy51a0e822012-09-05 15:24:24 -0400153static unsigned int get_fs_size(char *dev)
154{
155 int fd, block_size;
156 struct ext4_super_block sb;
157 off64_t len;
158
159 if ((fd = open(dev, O_RDONLY)) < 0) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000160 printf("Cannot open device to get filesystem size ");
Dees_Troy51a0e822012-09-05 15:24:24 -0400161 return 0;
162 }
163
164 if (lseek64(fd, 1024, SEEK_SET) < 0) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000165 printf("Cannot seek to superblock");
Dees_Troy51a0e822012-09-05 15:24:24 -0400166 return 0;
167 }
168
169 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000170 printf("Cannot read superblock");
Dees_Troy51a0e822012-09-05 15:24:24 -0400171 return 0;
172 }
173
174 close(fd);
175
176 block_size = 1024 << sb.s_log_block_size;
177 /* compute length in bytes */
178 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
179
180 /* return length in sectors */
181 return (unsigned int) (len / 512);
182}
183
184static unsigned int get_blkdev_size(int fd)
185{
186 unsigned int nr_sec;
187
188 if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
189 nr_sec = 0;
190 }
191
192 return nr_sec;
193}
194
Dees Troy4dff2e62013-11-10 04:11:43 +0000195static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
Dees_Troy51a0e822012-09-05 15:24:24 -0400196{
Dees Troy4dff2e62013-11-10 04:11:43 +0000197 static int cached_data = 0;
198 static off64_t cached_off = 0;
199 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
200 int fd;
201 char key_loc[PROPERTY_VALUE_MAX];
202 char real_blkdev[PROPERTY_VALUE_MAX];
203 unsigned int nr_sec;
204 int rc = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400205
Dees Troy4dff2e62013-11-10 04:11:43 +0000206 if (!cached_data) {
207 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
208
209 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
210 if ( (fd = open(real_blkdev, O_RDWR)) < 0) {
211 printf("Cannot open real block device %s\n", real_blkdev);
212 return -1;
213 }
214
215 if ((nr_sec = get_blkdev_size(fd))) {
216 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
217 * encryption info footer and key, and plenty of bytes to spare for future
218 * growth.
219 */
220 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
221 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
222 cached_data = 1;
223 } else {
224 printf("Cannot get size of block device %s\n", real_blkdev);
225 }
226 close(fd);
227 } else {
228 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
229 cached_off = 0;
230 cached_data = 1;
231 }
232 }
233
234 if (cached_data) {
235 if (metadata_fname) {
236 *metadata_fname = cached_metadata_fname;
237 }
238 if (off) {
239 *off = cached_off;
240 }
241 rc = 0;
242 }
243
244 return rc;
Dees_Troy51a0e822012-09-05 15:24:24 -0400245}
246
247/* key or salt can be NULL, in which case just skip writing that value. Useful to
248 * update the failed mount count but not change the key.
249 */
Dees Troy4dff2e62013-11-10 04:11:43 +0000250static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Dees_Troy51a0e822012-09-05 15:24:24 -0400251{
252 int fd;
253 unsigned int nr_sec, cnt;
Dees Troy4dff2e62013-11-10 04:11:43 +0000254 /* starting_off is set to the SEEK_SET offset
255 * where the crypto structure starts
256 */
257 off64_t starting_off;
Dees_Troy51a0e822012-09-05 15:24:24 -0400258 int rc = -1;
Dees Troy4dff2e62013-11-10 04:11:43 +0000259 char *fname = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400260 struct stat statbuf;
261
Dees Troy4dff2e62013-11-10 04:11:43 +0000262 if (get_crypt_ftr_info(&fname, &starting_off)) {
263 printf("Unable to get crypt_ftr_info\n");
264 return -1;
265 }
266 if (fname[0] != '/') {
267 printf("Unexpected value for crypto key location\n");
268 return -1;
269 }
270 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
271 printf("Cannot open footer file %s for put\n", fname);
272 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400273 }
274
Dees Troy4dff2e62013-11-10 04:11:43 +0000275 /* Seek to the start of the crypt footer */
276 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
277 printf("Cannot seek to real block device footer\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400278 goto errout;
279 }
280
Dees Troy4dff2e62013-11-10 04:11:43 +0000281 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
282 printf("Cannot write real block device footer\n");
283 goto errout;
Dees_Troy51a0e822012-09-05 15:24:24 -0400284 }
285
286 fstat(fd, &statbuf);
287 /* If the keys are kept on a raw block device, do not try to truncate it. */
Dees Troy4dff2e62013-11-10 04:11:43 +0000288 if (S_ISREG(statbuf.st_mode)) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400289 if (ftruncate(fd, 0x4000)) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000290 printf("Cannot set footer file size\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400291 goto errout;
292 }
293 }
294
295 /* Success! */
296 rc = 0;
297
298errout:
299 close(fd);
300 return rc;
301
302}
303
Dees Troy4dff2e62013-11-10 04:11:43 +0000304static inline int unix_read(int fd, void* buff, int len)
305{
306 return TEMP_FAILURE_RETRY(read(fd, buff, len));
307}
308
309static inline int unix_write(int fd, const void* buff, int len)
310{
311 return TEMP_FAILURE_RETRY(write(fd, buff, len));
312}
313
314static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
315{
316 memset(pdata, 0, len);
317 pdata->persist_magic = PERSIST_DATA_MAGIC;
318 pdata->persist_valid_entries = 0;
319}
320
321/* A routine to update the passed in crypt_ftr to the lastest version.
322 * fd is open read/write on the device that holds the crypto footer and persistent
323 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
324 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
325 */
326static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
327{
328 int orig_major = crypt_ftr->major_version;
329 int orig_minor = crypt_ftr->minor_version;
330 return; // in recovery we don't want to upgrade
331 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
332 struct crypt_persist_data *pdata;
333 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
334
335 printf("upgrading crypto footer to 1.1");
336
337 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
338 if (pdata == NULL) {
339 printf("Cannot allocate persisent data\n");
340 return;
341 }
342 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
343
344 /* Need to initialize the persistent data area */
345 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
346 printf("Cannot seek to persisent data offset\n");
347 return;
348 }
349 /* Write all zeros to the first copy, making it invalid */
350 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
351
352 /* Write a valid but empty structure to the second copy */
353 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
354 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
355
356 /* Update the footer */
357 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
358 crypt_ftr->persist_data_offset[0] = pdata_offset;
359 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
360 crypt_ftr->minor_version = 1;
361 }
362
363 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version)) {
364 printf("upgrading crypto footer to 1.2");
365 crypt_ftr->kdf_type = KDF_PBKDF2;
366 get_device_scrypt_params(crypt_ftr);
367 crypt_ftr->minor_version = 2;
368 }
369
370 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
371 if (lseek64(fd, offset, SEEK_SET) == -1) {
372 printf("Cannot seek to crypt footer\n");
373 return;
374 }
375 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
376 }
377}
378
379
380static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Dees_Troy51a0e822012-09-05 15:24:24 -0400381{
382 int fd;
383 unsigned int nr_sec, cnt;
Dees Troy4dff2e62013-11-10 04:11:43 +0000384 off64_t starting_off;
Dees_Troy51a0e822012-09-05 15:24:24 -0400385 int rc = -1;
Dees Troy4dff2e62013-11-10 04:11:43 +0000386 char *fname = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400387 struct stat statbuf;
388
Dees Troy4dff2e62013-11-10 04:11:43 +0000389 if (get_crypt_ftr_info(&fname, &starting_off)) {
390 printf("Unable to get crypt_ftr_info\n");
391 return -1;
392 }
393 if (fname[0] != '/') {
394 printf("Unexpected value for crypto key location\n");
395 return -1;
396 }
397 if ( (fd = open(fname, O_RDWR)) < 0) {
398 printf("Cannot open footer file %s for get\n", fname);
399 return -1;
400 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400401
Dees Troy4dff2e62013-11-10 04:11:43 +0000402 /* Make sure it's 16 Kbytes in length */
403 fstat(fd, &statbuf);
404 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
405 printf("footer file %s is not the expected size!\n", fname);
406 goto errout;
407 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400408
Dees Troy4dff2e62013-11-10 04:11:43 +0000409 /* Seek to the start of the crypt footer */
410 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
411 printf("Cannot seek to real block device footer\n");
412 goto errout;
Dees_Troy51a0e822012-09-05 15:24:24 -0400413 }
414
415 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000416 printf("Cannot read real block device footer\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 goto errout;
418 }
419
420 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000421 printf("Bad magic for real block device %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 goto errout;
423 }
424
Dees Troy4dff2e62013-11-10 04:11:43 +0000425 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
426 printf("Cannot understand major version %d real block device footer; expected %d\n",
427 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Dees_Troy51a0e822012-09-05 15:24:24 -0400428 goto errout;
429 }
430
Dees Troy4dff2e62013-11-10 04:11:43 +0000431 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
432 printf("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
433 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Dees_Troy51a0e822012-09-05 15:24:24 -0400434 }
435
Dees Troy4dff2e62013-11-10 04:11:43 +0000436 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
437 * copy on disk before returning.
438 */
439 /*if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
440 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
441 }*/
Dees_Troy51a0e822012-09-05 15:24:24 -0400442
443 /* Success! */
444 rc = 0;
445
446errout:
447 close(fd);
448 return rc;
449}
450
Dees Troy4dff2e62013-11-10 04:11:43 +0000451static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
452{
453 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
454 crypt_ftr->persist_data_offset[1]) {
455 printf("Crypt_ftr persist data regions overlap");
456 return -1;
457 }
458
459 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
460 printf("Crypt_ftr persist data region 0 starts after region 1");
461 return -1;
462 }
463
464 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
465 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
466 CRYPT_FOOTER_OFFSET) {
467 printf("Persistent data extends past crypto footer");
468 return -1;
469 }
470
471 return 0;
472}
473
474static int load_persistent_data(void)
475{
476 struct crypt_mnt_ftr crypt_ftr;
477 struct crypt_persist_data *pdata = NULL;
478 char encrypted_state[PROPERTY_VALUE_MAX];
479 char *fname;
480 int found = 0;
481 int fd;
482 int ret;
483 int i;
484
485 if (persist_data) {
486 /* Nothing to do, we've already loaded or initialized it */
487 return 0;
488 }
489
490
491 /* If not encrypted, just allocate an empty table and initialize it */
492 property_get("ro.crypto.state", encrypted_state, "");
493 if (strcmp(encrypted_state, "encrypted") ) {
494 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
495 if (pdata) {
496 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
497 persist_data = pdata;
498 return 0;
499 }
500 return -1;
501 }
502
503 if(get_crypt_ftr_and_key(&crypt_ftr)) {
504 return -1;
505 }
506
507 if ((crypt_ftr.major_version != 1) || (crypt_ftr.minor_version != 1)) {
508 printf("Crypt_ftr version doesn't support persistent data");
509 return -1;
510 }
511
512 if (get_crypt_ftr_info(&fname, NULL)) {
513 return -1;
514 }
515
516 ret = validate_persistent_data_storage(&crypt_ftr);
517 if (ret) {
518 return -1;
519 }
520
521 fd = open(fname, O_RDONLY);
522 if (fd < 0) {
523 printf("Cannot open %s metadata file", fname);
524 return -1;
525 }
526
527 if (persist_data == NULL) {
528 pdata = malloc(crypt_ftr.persist_data_size);
529 if (pdata == NULL) {
530 printf("Cannot allocate memory for persistent data");
531 goto err;
532 }
533 }
534
535 for (i = 0; i < 2; i++) {
536 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
537 printf("Cannot seek to read persistent data on %s", fname);
538 goto err2;
539 }
540 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
541 printf("Error reading persistent data on iteration %d", i);
542 goto err2;
543 }
544 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
545 found = 1;
546 break;
547 }
548 }
549
550 if (!found) {
551 printf("Could not find valid persistent data, creating");
552 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
553 }
554
555 /* Success */
556 persist_data = pdata;
557 close(fd);
558 return 0;
559
560err2:
561 free(pdata);
562
563err:
564 close(fd);
565 return -1;
566}
567
568static int save_persistent_data(void)
569{
570 struct crypt_mnt_ftr crypt_ftr;
571 struct crypt_persist_data *pdata;
572 char *fname;
573 off64_t write_offset;
574 off64_t erase_offset;
575 int found = 0;
576 int fd;
577 int ret;
578
579 if (persist_data == NULL) {
580 printf("No persistent data to save");
581 return -1;
582 }
583
584 if(get_crypt_ftr_and_key(&crypt_ftr)) {
585 return -1;
586 }
587
588 if ((crypt_ftr.major_version != 1) || (crypt_ftr.minor_version != 1)) {
589 printf("Crypt_ftr version doesn't support persistent data");
590 return -1;
591 }
592
593 ret = validate_persistent_data_storage(&crypt_ftr);
594 if (ret) {
595 return -1;
596 }
597
598 if (get_crypt_ftr_info(&fname, NULL)) {
599 return -1;
600 }
601
602 fd = open(fname, O_RDWR);
603 if (fd < 0) {
604 printf("Cannot open %s metadata file", fname);
605 return -1;
606 }
607
608 pdata = malloc(crypt_ftr.persist_data_size);
609 if (pdata == NULL) {
610 printf("Cannot allocate persistant data");
611 goto err;
612 }
613
614 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
615 printf("Cannot seek to read persistent data on %s", fname);
616 goto err2;
617 }
618
619 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
620 printf("Error reading persistent data before save");
621 goto err2;
622 }
623
624 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
625 /* The first copy is the curent valid copy, so write to
626 * the second copy and erase this one */
627 write_offset = crypt_ftr.persist_data_offset[1];
628 erase_offset = crypt_ftr.persist_data_offset[0];
629 } else {
630 /* The second copy must be the valid copy, so write to
631 * the first copy, and erase the second */
632 write_offset = crypt_ftr.persist_data_offset[0];
633 erase_offset = crypt_ftr.persist_data_offset[1];
634 }
635
636 /* Write the new copy first, if successful, then erase the old copy */
637 if (lseek(fd, write_offset, SEEK_SET) < 0) {
638 printf("Cannot seek to write persistent data");
639 goto err2;
640 }
641 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
642 (int) crypt_ftr.persist_data_size) {
643 if (lseek(fd, erase_offset, SEEK_SET) < 0) {
644 printf("Cannot seek to erase previous persistent data");
645 goto err2;
646 }
647 fsync(fd);
648 memset(pdata, 0, crypt_ftr.persist_data_size);
649 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
650 (int) crypt_ftr.persist_data_size) {
651 printf("Cannot write to erase previous persistent data");
652 goto err2;
653 }
654 fsync(fd);
655 } else {
656 printf("Cannot write to save persistent data");
657 goto err2;
658 }
659
660 /* Success */
661 free(pdata);
662 close(fd);
663 return 0;
664
665err2:
666 free(pdata);
667err:
668 close(fd);
669 return -1;
670}
671
Dees_Troy51a0e822012-09-05 15:24:24 -0400672/* Convert a binary key of specified length into an ascii hex string equivalent,
673 * without the leading 0x and with null termination
674 */
675void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
676 char *master_key_ascii)
677{
678 unsigned int i, a;
679 unsigned char nibble;
680
681 for (i=0, a=0; i<keysize; i++, a+=2) {
682 /* For each byte, write out two ascii hex digits */
683 nibble = (master_key[i] >> 4) & 0xf;
684 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
685
686 nibble = master_key[i] & 0xf;
687 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
688 }
689
690 /* Add the null termination */
691 master_key_ascii[a] = '\0';
692
693}
694
Dees Troy4dff2e62013-11-10 04:11:43 +0000695static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
696 char *real_blk_name, const char *name, int fd,
697 char *extra_params)
698{
699 char buffer[DM_CRYPT_BUF_SIZE];
700 struct dm_ioctl *io;
701 struct dm_target_spec *tgt;
702 char *crypt_params;
703 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
704 int i;
705
706 io = (struct dm_ioctl *) buffer;
707
708 /* Load the mapping table for this device */
709 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
710
711 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
712 io->target_count = 1;
713 tgt->status = 0;
714 tgt->sector_start = 0;
715 tgt->length = crypt_ftr->fs_size;
716 strcpy(tgt->target_type, "crypt");
717
718 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
719 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
720 sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
721 master_key_ascii, real_blk_name, extra_params);
722 crypt_params += strlen(crypt_params) + 1;
723 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
724 tgt->next = crypt_params - buffer;
725
726 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
727 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
728 break;
729 }
730 usleep(500000);
731 }
732
733 if (i == TABLE_LOAD_RETRIES) {
734 /* We failed to load the table, return an error */
735 return -1;
736 } else {
737 return i + 1;
738 }
739}
740
741
742static int get_dm_crypt_version(int fd, const char *name, int *version)
743{
744 char buffer[DM_CRYPT_BUF_SIZE];
745 struct dm_ioctl *io;
746 struct dm_target_versions *v;
747 int i;
748
749 io = (struct dm_ioctl *) buffer;
750
751 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
752
753 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
754 return -1;
755 }
756
757 /* Iterate over the returned versions, looking for name of "crypt".
758 * When found, get and return the version.
759 */
760 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
761 while (v->next) {
762 if (! strcmp(v->name, "crypt")) {
763 /* We found the crypt driver, return the version, and get out */
764 version[0] = v->version[0];
765 version[1] = v->version[1];
766 version[2] = v->version[2];
767 return 0;
768 }
769 v = (struct dm_target_versions *)(((char *)v) + v->next);
770 }
771
772 return -1;
773}
774
Dees_Troy51a0e822012-09-05 15:24:24 -0400775static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
776 char *real_blk_name, char *crypto_blk_name, const char *name)
777{
778 char buffer[DM_CRYPT_BUF_SIZE];
779 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
780 char *crypt_params;
781 struct dm_ioctl *io;
782 struct dm_target_spec *tgt;
783 unsigned int minor;
784 int fd;
Dees Troy4dff2e62013-11-10 04:11:43 +0000785 int i;
Dees_Troy51a0e822012-09-05 15:24:24 -0400786 int retval = -1;
Dees Troy4dff2e62013-11-10 04:11:43 +0000787 int version[3];
788 char *extra_params;
789 int load_count;
Dees_Troy51a0e822012-09-05 15:24:24 -0400790
791 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000792 printf("Cannot open device-mapper\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400793 goto errout;
794 }
795
796 io = (struct dm_ioctl *) buffer;
797
798 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
799 if (ioctl(fd, DM_DEV_CREATE, io)) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000800 printf("Cannot create dm-crypt device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400801 goto errout;
802 }
803
804 /* Get the device status, in particular, the name of it's device file */
805 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
806 if (ioctl(fd, DM_DEV_STATUS, io)) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000807 printf("Cannot retrieve dm-crypt device status\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400808 goto errout;
809 }
810 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
811 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
812
Dees Troy4dff2e62013-11-10 04:11:43 +0000813 extra_params = "";
814 if (! get_dm_crypt_version(fd, name, version)) {
815 /* Support for allow_discards was added in version 1.11.0 */
816 if ((version[0] >= 2) ||
817 ((version[0] == 1) && (version[1] >= 11))) {
818 extra_params = "1 allow_discards";
819 printf("Enabling support for allow_discards in dmcrypt.\n");
820 }
821 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400822
Dees Troy4dff2e62013-11-10 04:11:43 +0000823 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
824 fd, extra_params);
825 if (load_count < 0) {
826 printf("Cannot load dm-crypt mapping table.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400827 goto errout;
Dees Troy4dff2e62013-11-10 04:11:43 +0000828 } else if (load_count > 1) {
829 printf("Took %d tries to load dmcrypt table.\n", load_count);
Dees_Troy51a0e822012-09-05 15:24:24 -0400830 }
831
832 /* Resume this device to activate it */
Dees Troy4dff2e62013-11-10 04:11:43 +0000833 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400834
835 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000836 printf("Cannot resume the dm-crypt device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400837 goto errout;
838 }
839
840 /* We made it here with no errors. Woot! */
841 retval = 0;
842
843errout:
844 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
845
846 return retval;
847}
848
849static int delete_crypto_blk_dev(char *name)
850{
851 int fd;
852 char buffer[DM_CRYPT_BUF_SIZE];
853 struct dm_ioctl *io;
854 int retval = -1;
855
856 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000857 printf("Cannot open device-mapper\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400858 goto errout;
859 }
860
861 io = (struct dm_ioctl *) buffer;
862
863 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
864 if (ioctl(fd, DM_DEV_REMOVE, io)) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000865 printf("Cannot remove dm-crypt device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400866 goto errout;
867 }
868
869 /* We made it here with no errors. Woot! */
870 retval = 0;
871
872errout:
873 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
874
875 return retval;
876
877}
878
Dees Troy4dff2e62013-11-10 04:11:43 +0000879static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey, void *params) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400880 /* Turn the password into a key and IV that can decrypt the master key */
881 PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
882 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
883}
884
Dees Troy4dff2e62013-11-10 04:11:43 +0000885static void scrypt(char *passwd, unsigned char *salt, unsigned char *ikey, void *params) {
886 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
887
888 int N = 1 << ftr->N_factor;
889 int r = 1 << ftr->r_factor;
890 int p = 1 << ftr->p_factor;
891
892 /* Turn the password into a key and IV that can decrypt the master key */
893 crypto_scrypt((unsigned char *) passwd, strlen(passwd), salt, SALT_LEN, N, r, p, ikey,
894 KEY_LEN_BYTES + IV_LEN_BYTES);
895}
896
Dees_Troy51a0e822012-09-05 15:24:24 -0400897static int encrypt_master_key(char *passwd, unsigned char *salt,
898 unsigned char *decrypted_master_key,
Dees Troy4dff2e62013-11-10 04:11:43 +0000899 unsigned char *encrypted_master_key,
900 struct crypt_mnt_ftr *crypt_ftr)
Dees_Troy51a0e822012-09-05 15:24:24 -0400901{
902 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
903 EVP_CIPHER_CTX e_ctx;
904 int encrypted_len, final_len;
905
906 /* Turn the password into a key and IV that can decrypt the master key */
Dees Troy4dff2e62013-11-10 04:11:43 +0000907 get_device_scrypt_params(crypt_ftr);
908 scrypt(passwd, salt, ikey, crypt_ftr);
909
Dees_Troy51a0e822012-09-05 15:24:24 -0400910 /* Initialize the decryption engine */
911 if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000912 printf("EVP_EncryptInit failed\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400913 return -1;
914 }
915 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
916
917 /* Encrypt the master key */
918 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
919 decrypted_master_key, KEY_LEN_BYTES)) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000920 printf("EVP_EncryptUpdate failed\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400921 return -1;
922 }
923 if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000924 printf("EVP_EncryptFinal failed\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400925 return -1;
926 }
927
928 if (encrypted_len + final_len != KEY_LEN_BYTES) {
Dees Troy4dff2e62013-11-10 04:11:43 +0000929 printf("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
Dees_Troy51a0e822012-09-05 15:24:24 -0400930 return -1;
931 } else {
932 return 0;
933 }
934}
935
936static int decrypt_master_key(char *passwd, unsigned char *salt,
937 unsigned char *encrypted_master_key,
Dees Troy4dff2e62013-11-10 04:11:43 +0000938 unsigned char *decrypted_master_key,
939 kdf_func kdf, void *kdf_params)
Dees_Troy51a0e822012-09-05 15:24:24 -0400940{
941 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
942 EVP_CIPHER_CTX d_ctx;
943 int decrypted_len, final_len;
944
945 /* Turn the password into a key and IV that can decrypt the master key */
Dees Troy4dff2e62013-11-10 04:11:43 +0000946 kdf(passwd, salt, ikey, kdf_params);
Dees_Troy51a0e822012-09-05 15:24:24 -0400947
948 /* Initialize the decryption engine */
949 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
950 return -1;
951 }
952 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
953 /* Decrypt the master key */
954 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
955 encrypted_master_key, KEY_LEN_BYTES)) {
956 return -1;
957 }
958 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
959 return -1;
960 }
961
962 if (decrypted_len + final_len != KEY_LEN_BYTES) {
963 return -1;
964 } else {
965 return 0;
966 }
967}
968
Dees Troy4dff2e62013-11-10 04:11:43 +0000969static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Dees_Troy51a0e822012-09-05 15:24:24 -0400970{
Dees Troy4dff2e62013-11-10 04:11:43 +0000971 if (ftr->kdf_type == KDF_SCRYPT) {
972 *kdf = scrypt;
973 *kdf_params = ftr;
974 } else {
975 *kdf = pbkdf2;
976 *kdf_params = NULL;
977 }
978}
979
980static int decrypt_master_key_and_upgrade(char *passwd, unsigned char *decrypted_master_key,
981 struct crypt_mnt_ftr *crypt_ftr)
982{
983 kdf_func kdf;
984 void *kdf_params;
985 int ret;
986
987 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
988 ret = decrypt_master_key(passwd, crypt_ftr->salt, crypt_ftr->master_key, decrypted_master_key, kdf,
989 kdf_params);
990 if (ret != 0) {
991 printf("failure decrypting master key");
992 return ret;
993 }
994
995 /*
996 * Upgrade if we're not using the latest KDF.
997 */
998 /*if (crypt_ftr->kdf_type != KDF_SCRYPT) {
999 crypt_ftr->kdf_type = KDF_SCRYPT;
1000 encrypt_master_key(passwd, crypt_ftr->salt, decrypted_master_key, crypt_ftr->master_key,
1001 crypt_ftr);
1002 put_crypt_ftr_and_key(crypt_ftr);
1003 }*/
1004
1005 return ret;
1006}
1007
1008static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt,
1009 struct crypt_mnt_ftr *crypt_ftr) {
Dees_Troy51a0e822012-09-05 15:24:24 -04001010 int fd;
1011 unsigned char key_buf[KEY_LEN_BYTES];
1012 EVP_CIPHER_CTX e_ctx;
1013 int encrypted_len, final_len;
1014
1015 /* Get some random bits for a key */
1016 fd = open("/dev/urandom", O_RDONLY);
1017 read(fd, key_buf, sizeof(key_buf));
1018 read(fd, salt, SALT_LEN);
1019 close(fd);
1020
1021 /* Now encrypt it with the password */
Dees Troy4dff2e62013-11-10 04:11:43 +00001022 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001023}
1024
1025static int wait_and_unmount(char *mountpoint)
1026{
1027 int i, rc;
1028#define WAIT_UNMOUNT_COUNT 20
1029
1030 /* Now umount the tmpfs filesystem */
1031 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
1032 if (umount(mountpoint)) {
1033 if (errno == EINVAL) {
1034 /* EINVAL is returned if the directory is not a mountpoint,
1035 * i.e. there is no filesystem mounted there. So just get out.
1036 */
1037 break;
1038 }
1039 sleep(1);
1040 i++;
1041 } else {
1042 break;
1043 }
1044 }
1045
1046 if (i < WAIT_UNMOUNT_COUNT) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001047 printf("unmounting %s succeeded\n", mountpoint);
Dees_Troy51a0e822012-09-05 15:24:24 -04001048 rc = 0;
1049 } else {
Dees Troy4dff2e62013-11-10 04:11:43 +00001050 printf("unmounting %s failed\n", mountpoint);
Dees_Troy51a0e822012-09-05 15:24:24 -04001051 rc = -1;
1052 }
1053
1054 return rc;
1055}
1056
Dees Troy4dff2e62013-11-10 04:11:43 +00001057#define DATA_PREP_TIMEOUT 200
Dees_Troy51a0e822012-09-05 15:24:24 -04001058static int prep_data_fs(void)
1059{
1060 int i;
1061
1062 /* Do the prep of the /data filesystem */
1063 property_set("vold.post_fs_data_done", "0");
1064 property_set("vold.decrypt", "trigger_post_fs_data");
Dees Troy4dff2e62013-11-10 04:11:43 +00001065 printf("Just triggered post_fs_data\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001066
Dees Troy4dff2e62013-11-10 04:11:43 +00001067 /* Wait a max of 50 seconds, hopefully it takes much less */
Dees_Troy51a0e822012-09-05 15:24:24 -04001068 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
1069 char p[PROPERTY_VALUE_MAX];
1070
1071 property_get("vold.post_fs_data_done", p, "0");
1072 if (*p == '1') {
1073 break;
1074 } else {
1075 usleep(250000);
1076 }
1077 }
1078 if (i == DATA_PREP_TIMEOUT) {
1079 /* Ugh, we failed to prep /data in time. Bail. */
Dees Troy4dff2e62013-11-10 04:11:43 +00001080 printf("post_fs_data timed out!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001081 return -1;
1082 } else {
Dees Troy4dff2e62013-11-10 04:11:43 +00001083 printf("post_fs_data done\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001084 return 0;
1085 }
1086}
1087
1088int cryptfs_restart(void)
1089{
1090 char fs_type[32];
1091 char real_blkdev[MAXPATHLEN];
1092 char crypto_blkdev[MAXPATHLEN];
1093 char fs_options[256];
1094 unsigned long mnt_flags;
1095 struct stat statbuf;
1096 int rc = -1, i;
1097 static int restart_successful = 0;
1098
1099 /* Validate that it's OK to call this routine */
1100 if (! master_key_saved) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001101 printf("Encrypted filesystem not validated, aborting");
Dees_Troy51a0e822012-09-05 15:24:24 -04001102 return -1;
1103 }
1104
1105 if (restart_successful) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001106 printf("System already restarted with encrypted disk, aborting");
Dees_Troy51a0e822012-09-05 15:24:24 -04001107 return -1;
1108 }
1109
1110 /* Here is where we shut down the framework. The init scripts
1111 * start all services in one of three classes: core, main or late_start.
1112 * On boot, we start core and main. Now, we stop main, but not core,
1113 * as core includes vold and a few other really important things that
1114 * we need to keep running. Once main has stopped, we should be able
1115 * to umount the tmpfs /data, then mount the encrypted /data.
1116 * We then restart the class main, and also the class late_start.
1117 * At the moment, I've only put a few things in late_start that I know
1118 * are not needed to bring up the framework, and that also cause problems
1119 * with unmounting the tmpfs /data, but I hope to add add more services
1120 * to the late_start class as we optimize this to decrease the delay
1121 * till the user is asked for the password to the filesystem.
1122 */
1123
1124 /* The init files are setup to stop the class main when vold.decrypt is
1125 * set to trigger_reset_main.
1126 */
1127 property_set("vold.decrypt", "trigger_reset_main");
Dees Troy4dff2e62013-11-10 04:11:43 +00001128 printf("Just asked init to shut down class main\n");
1129
1130 /* Ugh, shutting down the framework is not synchronous, so until it
1131 * can be fixed, this horrible hack will wait a moment for it all to
1132 * shut down before proceeding. Without it, some devices cannot
1133 * restart the graphics services.
1134 */
1135 sleep(2);
Dees_Troy51a0e822012-09-05 15:24:24 -04001136
1137 /* Now that the framework is shutdown, we should be able to umount()
1138 * the tmpfs filesystem, and mount the real one.
1139 */
1140
1141 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1142 if (strlen(crypto_blkdev) == 0) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001143 printf("fs_crypto_blkdev not set\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001144 return -1;
1145 }
1146
1147 if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) {
1148 /* If that succeeded, then mount the decrypted filesystem */
Dees Troy4dff2e62013-11-10 04:11:43 +00001149 fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -04001150
1151 property_set("vold.decrypt", "trigger_load_persist_props");
1152 /* Create necessary paths on /data */
1153 if (prep_data_fs()) {
1154 return -1;
1155 }
1156
1157 /* startup service classes main and late_start */
1158 property_set("vold.decrypt", "trigger_restart_framework");
Dees Troy4dff2e62013-11-10 04:11:43 +00001159 printf("Just triggered restart_framework\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001160
1161 /* Give it a few moments to get started */
1162 sleep(1);
1163 }
1164
1165 if (rc == 0) {
1166 restart_successful = 1;
1167 }
1168
1169 return rc;
1170}
1171
1172static int do_crypto_complete(char *mount_point)
1173{
1174 struct crypt_mnt_ftr crypt_ftr;
Dees_Troy51a0e822012-09-05 15:24:24 -04001175 char encrypted_state[PROPERTY_VALUE_MAX];
1176 char key_loc[PROPERTY_VALUE_MAX];
1177
1178 property_get("ro.crypto.state", encrypted_state, "");
1179 if (strcmp(encrypted_state, "encrypted") ) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001180 printf("not running with encryption, aborting");
Dees_Troy51a0e822012-09-05 15:24:24 -04001181 return 1;
1182 }
1183
Dees Troy4dff2e62013-11-10 04:11:43 +00001184 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1185 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Dees_Troy51a0e822012-09-05 15:24:24 -04001186
1187 /*
1188 * Only report this error if key_loc is a file and it exists.
1189 * If the device was never encrypted, and /data is not mountable for
1190 * some reason, returning 1 should prevent the UI from presenting the
1191 * a "enter password" screen, or worse, a "press button to wipe the
1192 * device" screen.
1193 */
1194 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001195 printf("master key file does not exist, aborting");
Dees_Troy51a0e822012-09-05 15:24:24 -04001196 return 1;
1197 } else {
Dees Troy4dff2e62013-11-10 04:11:43 +00001198 printf("Error getting crypt footer and key\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001199 return -1;
1200 }
1201 }
1202
1203 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001204 printf("Encryption process didn't finish successfully\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001205 return -2; /* -2 is the clue to the UI that there is no usable data on the disk,
1206 * and give the user an option to wipe the disk */
1207 }
1208
1209 /* We passed the test! We shall diminish, and return to the west */
1210 return 0;
1211}
1212
1213static int test_mount_encrypted_fs(char *passwd, char *mount_point, char *label)
1214{
1215 struct crypt_mnt_ftr crypt_ftr;
1216 /* Allocate enough space for a 256 bit key, but we may use less */
Dees Troy4dff2e62013-11-10 04:11:43 +00001217 unsigned char decrypted_master_key[32];
Dees_Troy51a0e822012-09-05 15:24:24 -04001218 char crypto_blkdev[MAXPATHLEN];
1219 char real_blkdev[MAXPATHLEN];
1220 char tmp_mount_point[64];
1221 unsigned int orig_failed_decrypt_count;
1222 char encrypted_state[PROPERTY_VALUE_MAX];
1223 int rc;
Dees Troy4dff2e62013-11-10 04:11:43 +00001224 kdf_func kdf;
1225 void *kdf_params;
Dees_Troy51a0e822012-09-05 15:24:24 -04001226
1227 property_get("ro.crypto.state", encrypted_state, "");
1228 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001229 printf("encrypted fs already validated or not running with encryption, aborting");
Dees_Troy51a0e822012-09-05 15:24:24 -04001230 return -1;
1231 }
1232
Dees Troy4dff2e62013-11-10 04:11:43 +00001233 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Dees_Troy51a0e822012-09-05 15:24:24 -04001234
Dees Troy4dff2e62013-11-10 04:11:43 +00001235 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1236 printf("Error getting crypt footer and key\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001237 return -1;
1238 }
1239
Dees Troy4dff2e62013-11-10 04:11:43 +00001240 printf("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size);
Dees_Troy51a0e822012-09-05 15:24:24 -04001241 orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count;
1242
1243 if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001244 decrypt_master_key_and_upgrade(passwd, decrypted_master_key, &crypt_ftr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001245 }
1246
1247 if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key,
1248 real_blkdev, crypto_blkdev, label)) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001249 printf("Error creating decrypted block device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001250 return -1;
1251 }
1252
Dees Troy4dff2e62013-11-10 04:11:43 +00001253 /* If init detects an encrypted filesystem, it writes a file for each such
Dees_Troy51a0e822012-09-05 15:24:24 -04001254 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
1255 * files and passes that data to me */
1256 /* Create a tmp mount point to try mounting the decryptd fs
1257 * Since we're here, the mount_point should be a tmpfs filesystem, so make
1258 * a directory in it to test mount the decrypted filesystem.
1259 */
1260 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
1261 mkdir(tmp_mount_point, 0755);
Dees Troy4dff2e62013-11-10 04:11:43 +00001262 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
1263 printf("Error temp mounting decrypted block device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001264 delete_crypto_blk_dev(label);
1265 crypt_ftr.failed_decrypt_count++;
1266 } else {
1267 /* Success, so just umount and we'll mount it properly when we restart
1268 * the framework.
1269 */
1270 umount(tmp_mount_point);
1271 crypt_ftr.failed_decrypt_count = 0;
1272 }
1273
1274 if (orig_failed_decrypt_count != crypt_ftr.failed_decrypt_count) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001275 put_crypt_ftr_and_key(&crypt_ftr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001276 }
1277
1278 if (crypt_ftr.failed_decrypt_count) {
1279 /* We failed to mount the device, so return an error */
1280 rc = crypt_ftr.failed_decrypt_count;
1281
1282 } else {
1283 /* Woot! Success! Save the name of the crypto block device
1284 * so we can mount it when restarting the framework.
1285 */
1286 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
1287
1288 /* Also save a the master key so we can reencrypted the key
1289 * the key when we want to change the password on it.
1290 */
1291 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Dees_Troy51a0e822012-09-05 15:24:24 -04001292 saved_mount_point = strdup(mount_point);
1293 master_key_saved = 1;
1294 rc = 0;
1295 }
1296
1297 return rc;
1298}
1299
1300/* Called by vold when it wants to undo the crypto mapping of a volume it
1301 * manages. This is usually in response to a factory reset, when we want
1302 * to undo the crypto mapping so the volume is formatted in the clear.
1303 */
1304int cryptfs_revert_volume(const char *label)
1305{
1306 return delete_crypto_blk_dev((char *)label);
1307}
1308
1309/*
1310 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
1311 * Setup a dm-crypt mapping, use the saved master key from
1312 * setting up the /data mapping, and return the new device path.
1313 */
1314int cryptfs_setup_volume(const char *label, int major, int minor,
1315 char *crypto_sys_path, unsigned int max_path,
1316 int *new_major, int *new_minor)
1317{
1318 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
1319 struct crypt_mnt_ftr sd_crypt_ftr;
Dees_Troy51a0e822012-09-05 15:24:24 -04001320 struct stat statbuf;
1321 int nr_sec, fd;
1322
1323 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
1324
Dees Troy4dff2e62013-11-10 04:11:43 +00001325 get_crypt_ftr_and_key(&sd_crypt_ftr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001326
1327 /* Update the fs_size field to be the size of the volume */
1328 fd = open(real_blkdev, O_RDONLY);
1329 nr_sec = get_blkdev_size(fd);
1330 close(fd);
1331 if (nr_sec == 0) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001332 printf("Cannot get size of volume %s\n", real_blkdev);
Dees_Troy51a0e822012-09-05 15:24:24 -04001333 return -1;
1334 }
1335
1336 sd_crypt_ftr.fs_size = nr_sec;
1337 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
1338 crypto_blkdev, label);
1339
1340 stat(crypto_blkdev, &statbuf);
1341 *new_major = MAJOR(statbuf.st_rdev);
1342 *new_minor = MINOR(statbuf.st_rdev);
1343
1344 /* Create path to sys entry for this block device */
1345 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
1346
1347 return 0;
1348}
1349
1350int cryptfs_crypto_complete(void)
1351{
1352 return do_crypto_complete("/data");
1353}
1354
Dees Troy4dff2e62013-11-10 04:11:43 +00001355#define FSTAB_PREFIX "/fstab."
1356
Dees_Troy51a0e822012-09-05 15:24:24 -04001357int cryptfs_check_passwd(char *passwd)
1358{
1359 int rc = -1;
Dees Troy4dff2e62013-11-10 04:11:43 +00001360 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
1361 char propbuf[PROPERTY_VALUE_MAX];
1362 int i;
1363 int flags;
1364
1365 property_get("ro.hardware", propbuf, "");
1366 snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
1367
1368 fstab = fs_mgr_read_fstab(fstab_filename);
1369 if (!fstab) {
1370 printf("failed to open %s\n", fstab_filename);
1371 return -1;
1372 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001373
1374 rc = test_mount_encrypted_fs(passwd, DATA_MNT_POINT, "userdata");
1375
1376 return rc;
1377}
1378
1379int cryptfs_verify_passwd(char *passwd)
1380{
1381 struct crypt_mnt_ftr crypt_ftr;
1382 /* Allocate enough space for a 256 bit key, but we may use less */
Dees Troy4dff2e62013-11-10 04:11:43 +00001383 unsigned char decrypted_master_key[32];
Dees_Troy51a0e822012-09-05 15:24:24 -04001384 char encrypted_state[PROPERTY_VALUE_MAX];
1385 int rc;
1386
1387 property_get("ro.crypto.state", encrypted_state, "");
1388 if (strcmp(encrypted_state, "encrypted") ) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001389 printf("device not encrypted, aborting");
Dees_Troy51a0e822012-09-05 15:24:24 -04001390 return -2;
1391 }
1392
1393 if (!master_key_saved) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001394 printf("encrypted fs not yet mounted, aborting");
Dees_Troy51a0e822012-09-05 15:24:24 -04001395 return -1;
1396 }
1397
1398 if (!saved_mount_point) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001399 printf("encrypted fs failed to save mount point, aborting");
Dees_Troy51a0e822012-09-05 15:24:24 -04001400 return -1;
1401 }
1402
Dees Troy4dff2e62013-11-10 04:11:43 +00001403 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1404 printf("Error getting crypt footer and key\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001405 return -1;
1406 }
1407
1408 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
1409 /* If the device has no password, then just say the password is valid */
1410 rc = 0;
1411 } else {
Dees Troy4dff2e62013-11-10 04:11:43 +00001412 decrypt_master_key_and_upgrade(passwd, decrypted_master_key, &crypt_ftr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001413 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
1414 /* They match, the password is correct */
1415 rc = 0;
1416 } else {
1417 /* If incorrect, sleep for a bit to prevent dictionary attacks */
1418 sleep(1);
1419 rc = 1;
1420 }
1421 }
1422
1423 return rc;
1424}
1425
1426/* Initialize a crypt_mnt_ftr structure. The keysize is
1427 * defaulted to 16 bytes, and the filesystem size to 0.
1428 * Presumably, at a minimum, the caller will update the
1429 * filesystem size and crypto_type_name after calling this function.
1430 */
1431static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
1432{
Dees Troy4dff2e62013-11-10 04:11:43 +00001433 off64_t off;
1434
1435 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Dees_Troy51a0e822012-09-05 15:24:24 -04001436 ftr->magic = CRYPT_MNT_MAGIC;
Dees Troy4dff2e62013-11-10 04:11:43 +00001437 ftr->major_version = CURRENT_MAJOR_VERSION;
1438 ftr->minor_version = CURRENT_MINOR_VERSION;
Dees_Troy51a0e822012-09-05 15:24:24 -04001439 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001440 ftr->keysize = KEY_LEN_BYTES;
Dees Troy4dff2e62013-11-10 04:11:43 +00001441
1442 ftr->kdf_type = KDF_SCRYPT;
1443 get_device_scrypt_params(ftr);
1444
1445 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
1446 if (get_crypt_ftr_info(NULL, &off) == 0) {
1447 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
1448 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
1449 ftr->persist_data_size;
1450 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001451}
1452
1453static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
1454{
Dees Troy4dff2e62013-11-10 04:11:43 +00001455 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -04001456}
1457
1458#define CRYPT_INPLACE_BUFSIZE 4096
1459#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512)
1460static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, off64_t size,
1461 off64_t *size_already_done, off64_t tot_size)
1462{
1463 int realfd, cryptofd;
1464 char *buf[CRYPT_INPLACE_BUFSIZE];
1465 int rc = -1;
1466 off64_t numblocks, i, remainder;
1467 off64_t one_pct, cur_pct, new_pct;
1468 off64_t blocks_already_done, tot_numblocks;
1469
1470 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001471 printf("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
Dees_Troy51a0e822012-09-05 15:24:24 -04001472 return -1;
1473 }
1474
1475 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001476 printf("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
Dees_Troy51a0e822012-09-05 15:24:24 -04001477 close(realfd);
1478 return -1;
1479 }
1480
1481 /* This is pretty much a simple loop of reading 4K, and writing 4K.
1482 * The size passed in is the number of 512 byte sectors in the filesystem.
1483 * So compute the number of whole 4K blocks we should read/write,
1484 * and the remainder.
1485 */
1486 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
1487 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
1488 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
1489 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
1490
Dees Troy4dff2e62013-11-10 04:11:43 +00001491 printf("Encrypting filesystem in place...");
Dees_Troy51a0e822012-09-05 15:24:24 -04001492
1493 one_pct = tot_numblocks / 100;
1494 cur_pct = 0;
1495 /* process the majority of the filesystem in blocks */
1496 for (i=0; i<numblocks; i++) {
1497 new_pct = (i + blocks_already_done) / one_pct;
1498 if (new_pct > cur_pct) {
1499 char buf[8];
1500
1501 cur_pct = new_pct;
1502 snprintf(buf, sizeof(buf), "%lld", cur_pct);
1503 property_set("vold.encrypt_progress", buf);
1504 }
1505 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001506 printf("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev);
Dees_Troy51a0e822012-09-05 15:24:24 -04001507 goto errout;
1508 }
1509 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001510 printf("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
Dees_Troy51a0e822012-09-05 15:24:24 -04001511 goto errout;
1512 }
1513 }
1514
1515 /* Do any remaining sectors */
1516 for (i=0; i<remainder; i++) {
1517 if (unix_read(realfd, buf, 512) <= 0) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001518 printf("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev);
Dees_Troy51a0e822012-09-05 15:24:24 -04001519 goto errout;
1520 }
1521 if (unix_write(cryptofd, buf, 512) <= 0) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001522 printf("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
Dees_Troy51a0e822012-09-05 15:24:24 -04001523 goto errout;
1524 }
1525 }
1526
1527 *size_already_done += size;
1528 rc = 0;
1529
1530errout:
1531 close(realfd);
1532 close(cryptofd);
1533
1534 return rc;
1535}
1536
1537#define CRYPTO_ENABLE_WIPE 1
1538#define CRYPTO_ENABLE_INPLACE 2
1539
1540#define FRAMEWORK_BOOT_WAIT 60
1541
1542static inline int should_encrypt(struct volume_info *volume)
1543{
1544 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
1545 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
1546}
1547
1548int cryptfs_enable(char *howarg, char *passwd)
1549{
Dees_Troy51a0e822012-09-05 15:24:24 -04001550 return -1;
1551}
1552
1553int cryptfs_changepw(char *newpw)
1554{
1555 struct crypt_mnt_ftr crypt_ftr;
Dees Troy4dff2e62013-11-10 04:11:43 +00001556 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Dees_Troy51a0e822012-09-05 15:24:24 -04001557
1558 /* This is only allowed after we've successfully decrypted the master key */
1559 if (! master_key_saved) {
Dees Troy4dff2e62013-11-10 04:11:43 +00001560 printf("Key not saved, aborting");
Dees_Troy51a0e822012-09-05 15:24:24 -04001561 return -1;
1562 }
1563
1564 /* get key */
Dees Troy4dff2e62013-11-10 04:11:43 +00001565 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1566 printf("Error getting crypt footer and key");
Dees_Troy51a0e822012-09-05 15:24:24 -04001567 return -1;
1568 }
1569
Dees Troy4dff2e62013-11-10 04:11:43 +00001570 encrypt_master_key(newpw, crypt_ftr.salt, saved_master_key, crypt_ftr.master_key, &crypt_ftr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001571
1572 /* save the key */
Dees Troy4dff2e62013-11-10 04:11:43 +00001573 put_crypt_ftr_and_key(&crypt_ftr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001574
1575 return 0;
1576}
Dees Troy4dff2e62013-11-10 04:11:43 +00001577
1578static int persist_get_key(char *fieldname, char *value)
1579{
1580 unsigned int i;
1581
1582 if (persist_data == NULL) {
1583 return -1;
1584 }
1585 for (i = 0; i < persist_data->persist_valid_entries; i++) {
1586 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
1587 /* We found it! */
1588 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
1589 return 0;
1590 }
1591 }
1592
1593 return -1;
1594}
1595
1596static int persist_set_key(char *fieldname, char *value, int encrypted)
1597{
1598 unsigned int i;
1599 unsigned int num;
1600 struct crypt_mnt_ftr crypt_ftr;
1601 unsigned int max_persistent_entries;
1602 unsigned int dsize;
1603
1604 if (persist_data == NULL) {
1605 return -1;
1606 }
1607
1608 /* If encrypted, use the values from the crypt_ftr, otherwise
1609 * use the values for the current spec.
1610 */
1611 if (encrypted) {
1612 if(get_crypt_ftr_and_key(&crypt_ftr)) {
1613 return -1;
1614 }
1615 dsize = crypt_ftr.persist_data_size;
1616 } else {
1617 dsize = CRYPT_PERSIST_DATA_SIZE;
1618 }
1619 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
1620 sizeof(struct crypt_persist_entry);
1621
1622 num = persist_data->persist_valid_entries;
1623
1624 for (i = 0; i < num; i++) {
1625 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
1626 /* We found an existing entry, update it! */
1627 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
1628 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
1629 return 0;
1630 }
1631 }
1632
1633 /* We didn't find it, add it to the end, if there is room */
1634 if (persist_data->persist_valid_entries < max_persistent_entries) {
1635 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
1636 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
1637 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
1638 persist_data->persist_valid_entries++;
1639 return 0;
1640 }
1641
1642 return -1;
1643}
1644
1645/* Return the value of the specified field. */
1646int cryptfs_getfield(char *fieldname, char *value, int len)
1647{
1648 char temp_value[PROPERTY_VALUE_MAX];
1649 char real_blkdev[MAXPATHLEN];
1650 /* 0 is success, 1 is not encrypted,
1651 * -1 is value not set, -2 is any other error
1652 */
1653 int rc = -2;
1654
1655 if (persist_data == NULL) {
1656 load_persistent_data();
1657 if (persist_data == NULL) {
1658 printf("Getfield error, cannot load persistent data");
1659 goto out;
1660 }
1661 }
1662
1663 if (!persist_get_key(fieldname, temp_value)) {
1664 /* We found it, copy it to the caller's buffer and return */
1665 strlcpy(value, temp_value, len);
1666 rc = 0;
1667 } else {
1668 /* Sadness, it's not there. Return the error */
1669 rc = -1;
1670 }
1671
1672out:
1673 return rc;
1674}
1675
1676/* Set the value of the specified field. */
1677int cryptfs_setfield(char *fieldname, char *value)
1678{
1679 struct crypt_persist_data stored_pdata;
1680 struct crypt_persist_data *pdata_p;
1681 struct crypt_mnt_ftr crypt_ftr;
1682 char encrypted_state[PROPERTY_VALUE_MAX];
1683 /* 0 is success, -1 is an error */
1684 int rc = -1;
1685 int encrypted = 0;
1686
1687 if (persist_data == NULL) {
1688 load_persistent_data();
1689 if (persist_data == NULL) {
1690 printf("Setfield error, cannot load persistent data");
1691 goto out;
1692 }
1693 }
1694
1695 property_get("ro.crypto.state", encrypted_state, "");
1696 if (!strcmp(encrypted_state, "encrypted") ) {
1697 encrypted = 1;
1698 }
1699
1700 if (persist_set_key(fieldname, value, encrypted)) {
1701 goto out;
1702 }
1703
1704 /* If we are running encrypted, save the persistent data now */
1705 if (encrypted) {
1706 if (save_persistent_data()) {
1707 printf("Setfield error, cannot save persistent data");
1708 goto out;
1709 }
1710 }
1711
1712 rc = 0;
1713
1714out:
1715 return rc;
1716}