blob: 4dd7a6b339290263cd18d5f1f1392826c0e286a9 [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>
Ken Sumralle550f782013-08-20 13:48:23 -070024#include <sys/wait.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080025#include <sys/stat.h>
Paul Lawrencef4faa572014-01-29 13:31:03 -080026#include <ctype.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080027#include <fcntl.h>
28#include <unistd.h>
29#include <stdio.h>
30#include <sys/ioctl.h>
31#include <linux/dm-ioctl.h>
32#include <libgen.h>
33#include <stdlib.h>
34#include <sys/param.h>
35#include <string.h>
36#include <sys/mount.h>
37#include <openssl/evp.h>
Ken Sumrall8ddbe402011-01-17 15:26:29 -080038#include <openssl/sha.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080039#include <errno.h>
Ken Sumrall3ed82362011-01-28 23:31:16 -080040#include <ext4.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070041#include <linux/kdev_t.h>
Ken Sumralle5032c42012-04-01 23:58:44 -070042#include <fs_mgr.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080043#include "cryptfs.h"
44#define LOG_TAG "Cryptfs"
45#include "cutils/log.h"
46#include "cutils/properties.h"
Ken Sumralladfba362013-06-04 16:37:52 -070047#include "cutils/android_reboot.h"
Ken Sumrall5d4c68e2011-01-30 19:06:03 -080048#include "hardware_legacy/power.h"
Ken Sumralle550f782013-08-20 13:48:23 -070049#include <logwrap/logwrap.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070050#include "VolumeManager.h"
Ken Sumrall9caab762013-06-11 19:10:20 -070051#include "VoldUtil.h"
Kenny Rootc4c70f12013-06-14 12:11:38 -070052#include "crypto_scrypt.h"
Paul Lawrenceae59fe62014-01-21 08:23:27 -080053#include "ext4_utils.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080054
Mark Salyzyn3e971272014-01-21 13:27:04 -080055#define UNUSED __attribute__((unused))
56
Ken Sumrall8f869aa2010-12-03 03:47:09 -080057#define DM_CRYPT_BUF_SIZE 4096
58
Jason parks70a4b3f2011-01-28 10:10:47 -060059#define HASH_COUNT 2000
60#define KEY_LEN_BYTES 16
61#define IV_LEN_BYTES 16
62
Ken Sumrall29d8da82011-05-18 17:20:07 -070063#define KEY_IN_FOOTER "footer"
64
Paul Lawrencef4faa572014-01-29 13:31:03 -080065// "default_password" encoded into hex (d=0x64 etc)
66#define DEFAULT_PASSWORD "64656661756c745f70617373776f7264"
67
Ken Sumrall29d8da82011-05-18 17:20:07 -070068#define EXT4_FS 1
69#define FAT_FS 2
70
Ken Sumralle919efe2012-09-29 17:07:41 -070071#define TABLE_LOAD_RETRIES 10
72
Ken Sumrall8f869aa2010-12-03 03:47:09 -080073char *me = "cryptfs";
74
Jason parks70a4b3f2011-01-28 10:10:47 -060075static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall3ad90722011-10-04 20:38:29 -070076static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -060077static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -070078static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -080079
80extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -080081
Ken Sumralladfba362013-06-04 16:37:52 -070082static void cryptfs_reboot(int recovery)
83{
84 if (recovery) {
85 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
86 } else {
87 property_set(ANDROID_RB_PROPERTY, "reboot");
88 }
89 sleep(20);
90
91 /* Shouldn't get here, reboot should happen before sleep times out */
92 return;
93}
94
Ken Sumrall8f869aa2010-12-03 03:47:09 -080095static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
96{
97 memset(io, 0, dataSize);
98 io->data_size = dataSize;
99 io->data_start = sizeof(struct dm_ioctl);
100 io->version[0] = 4;
101 io->version[1] = 0;
102 io->version[2] = 0;
103 io->flags = flags;
104 if (name) {
105 strncpy(io->name, name, sizeof(io->name));
106 }
107}
108
Kenny Rootc4c70f12013-06-14 12:11:38 -0700109/**
110 * Gets the default device scrypt parameters for key derivation time tuning.
111 * The parameters should lead to about one second derivation time for the
112 * given device.
113 */
114static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
115 const int default_params[] = SCRYPT_DEFAULTS;
116 int params[] = SCRYPT_DEFAULTS;
117 char paramstr[PROPERTY_VALUE_MAX];
118 char *token;
119 char *saveptr;
120 int i;
121
122 property_get(SCRYPT_PROP, paramstr, "");
123 if (paramstr[0] != '\0') {
124 /*
125 * The token we're looking for should be three integers separated by
126 * colons (e.g., "12:8:1"). Scan the property to make sure it matches.
127 */
Kenny Root2947e342013-08-14 15:54:49 -0700128 for (i = 0, token = strtok_r(paramstr, ":", &saveptr);
129 token != NULL && i < 3;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700130 i++, token = strtok_r(NULL, ":", &saveptr)) {
131 char *endptr;
132 params[i] = strtol(token, &endptr, 10);
133
134 /*
135 * Check that there was a valid number and it's 8-bit. If not,
136 * break out and the end check will take the default values.
137 */
138 if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) {
139 break;
140 }
141 }
142
143 /*
144 * If there were not enough tokens or a token was malformed (not an
145 * integer), it will end up here and the default parameters can be
146 * taken.
147 */
148 if ((i != 3) || (token != NULL)) {
149 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
150 memcpy(params, default_params, sizeof(params));
151 }
152 }
153
154 ftr->N_factor = params[0];
155 ftr->r_factor = params[1];
156 ftr->p_factor = params[2];
157}
158
Ken Sumrall3ed82362011-01-28 23:31:16 -0800159static unsigned int get_fs_size(char *dev)
160{
161 int fd, block_size;
162 struct ext4_super_block sb;
163 off64_t len;
164
165 if ((fd = open(dev, O_RDONLY)) < 0) {
166 SLOGE("Cannot open device to get filesystem size ");
167 return 0;
168 }
169
170 if (lseek64(fd, 1024, SEEK_SET) < 0) {
171 SLOGE("Cannot seek to superblock");
172 return 0;
173 }
174
175 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
176 SLOGE("Cannot read superblock");
177 return 0;
178 }
179
180 close(fd);
181
182 block_size = 1024 << sb.s_log_block_size;
183 /* compute length in bytes */
184 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
185
186 /* return length in sectors */
187 return (unsigned int) (len / 512);
188}
189
Ken Sumrall160b4d62013-04-22 12:15:39 -0700190static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
191{
192 static int cached_data = 0;
193 static off64_t cached_off = 0;
194 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
195 int fd;
196 char key_loc[PROPERTY_VALUE_MAX];
197 char real_blkdev[PROPERTY_VALUE_MAX];
198 unsigned int nr_sec;
199 int rc = -1;
200
201 if (!cached_data) {
202 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
203
204 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
205 if ( (fd = open(real_blkdev, O_RDWR)) < 0) {
206 SLOGE("Cannot open real block device %s\n", real_blkdev);
207 return -1;
208 }
209
210 if ((nr_sec = get_blkdev_size(fd))) {
211 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
212 * encryption info footer and key, and plenty of bytes to spare for future
213 * growth.
214 */
215 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
216 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
217 cached_data = 1;
218 } else {
219 SLOGE("Cannot get size of block device %s\n", real_blkdev);
220 }
221 close(fd);
222 } else {
223 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
224 cached_off = 0;
225 cached_data = 1;
226 }
227 }
228
229 if (cached_data) {
230 if (metadata_fname) {
231 *metadata_fname = cached_metadata_fname;
232 }
233 if (off) {
234 *off = cached_off;
235 }
236 rc = 0;
237 }
238
239 return rc;
240}
241
Ken Sumralle8744072011-01-18 22:01:55 -0800242/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800243 * update the failed mount count but not change the key.
244 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700245static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800246{
247 int fd;
248 unsigned int nr_sec, cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700249 /* starting_off is set to the SEEK_SET offset
250 * where the crypto structure starts
251 */
252 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800253 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700254 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700255 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800256
Ken Sumrall160b4d62013-04-22 12:15:39 -0700257 if (get_crypt_ftr_info(&fname, &starting_off)) {
258 SLOGE("Unable to get crypt_ftr_info\n");
259 return -1;
260 }
261 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700262 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700263 return -1;
264 }
Ken Sumralle550f782013-08-20 13:48:23 -0700265 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
266 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700267 return -1;
268 }
269
270 /* Seek to the start of the crypt footer */
271 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
272 SLOGE("Cannot seek to real block device footer\n");
273 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800274 }
275
276 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
277 SLOGE("Cannot write real block device footer\n");
278 goto errout;
279 }
280
Ken Sumrall3be890f2011-09-14 16:53:46 -0700281 fstat(fd, &statbuf);
282 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700283 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700284 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800285 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800286 goto errout;
287 }
288 }
289
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800290 /* Success! */
291 rc = 0;
292
293errout:
294 close(fd);
295 return rc;
296
297}
298
Ken Sumrall160b4d62013-04-22 12:15:39 -0700299static inline int unix_read(int fd, void* buff, int len)
300{
301 return TEMP_FAILURE_RETRY(read(fd, buff, len));
302}
303
304static inline int unix_write(int fd, const void* buff, int len)
305{
306 return TEMP_FAILURE_RETRY(write(fd, buff, len));
307}
308
309static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
310{
311 memset(pdata, 0, len);
312 pdata->persist_magic = PERSIST_DATA_MAGIC;
313 pdata->persist_valid_entries = 0;
314}
315
316/* A routine to update the passed in crypt_ftr to the lastest version.
317 * fd is open read/write on the device that holds the crypto footer and persistent
318 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
319 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
320 */
321static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
322{
Kenny Root7434b312013-06-14 11:29:53 -0700323 int orig_major = crypt_ftr->major_version;
324 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700325
Kenny Root7434b312013-06-14 11:29:53 -0700326 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
327 struct crypt_persist_data *pdata;
328 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700329
Kenny Rootc4c70f12013-06-14 12:11:38 -0700330 SLOGW("upgrading crypto footer to 1.1");
331
Kenny Root7434b312013-06-14 11:29:53 -0700332 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
333 if (pdata == NULL) {
334 SLOGE("Cannot allocate persisent data\n");
335 return;
336 }
337 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
338
339 /* Need to initialize the persistent data area */
340 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
341 SLOGE("Cannot seek to persisent data offset\n");
342 return;
343 }
344 /* Write all zeros to the first copy, making it invalid */
345 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
346
347 /* Write a valid but empty structure to the second copy */
348 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
349 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
350
351 /* Update the footer */
352 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
353 crypt_ftr->persist_data_offset[0] = pdata_offset;
354 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
355 crypt_ftr->minor_version = 1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700356 }
357
Paul Lawrencef4faa572014-01-29 13:31:03 -0800358 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700359 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800360 /* But keep the old kdf_type.
361 * It will get updated later to KDF_SCRYPT after the password has been verified.
362 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700363 crypt_ftr->kdf_type = KDF_PBKDF2;
364 get_device_scrypt_params(crypt_ftr);
365 crypt_ftr->minor_version = 2;
366 }
367
Paul Lawrencef4faa572014-01-29 13:31:03 -0800368 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
369 SLOGW("upgrading crypto footer to 1.3");
370 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
371 crypt_ftr->minor_version = 3;
372 }
373
Kenny Root7434b312013-06-14 11:29:53 -0700374 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
375 if (lseek64(fd, offset, SEEK_SET) == -1) {
376 SLOGE("Cannot seek to crypt footer\n");
377 return;
378 }
379 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700380 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700381}
382
383
384static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800385{
386 int fd;
387 unsigned int nr_sec, cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700388 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800389 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700390 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700391 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800392
Ken Sumrall160b4d62013-04-22 12:15:39 -0700393 if (get_crypt_ftr_info(&fname, &starting_off)) {
394 SLOGE("Unable to get crypt_ftr_info\n");
395 return -1;
396 }
397 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700398 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700399 return -1;
400 }
401 if ( (fd = open(fname, O_RDWR)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700402 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700403 return -1;
404 }
405
406 /* Make sure it's 16 Kbytes in length */
407 fstat(fd, &statbuf);
408 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
409 SLOGE("footer file %s is not the expected size!\n", fname);
410 goto errout;
411 }
412
413 /* Seek to the start of the crypt footer */
414 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
415 SLOGE("Cannot seek to real block device footer\n");
416 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800417 }
418
419 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
420 SLOGE("Cannot read real block device footer\n");
421 goto errout;
422 }
423
424 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700425 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800426 goto errout;
427 }
428
Kenny Rootc96a5f82013-06-14 12:08:28 -0700429 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
430 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
431 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800432 goto errout;
433 }
434
Kenny Rootc96a5f82013-06-14 12:08:28 -0700435 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
436 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
437 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800438 }
439
Ken Sumrall160b4d62013-04-22 12:15:39 -0700440 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
441 * copy on disk before returning.
442 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700443 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700444 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800445 }
446
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800447 /* Success! */
448 rc = 0;
449
450errout:
451 close(fd);
452 return rc;
453}
454
Ken Sumrall160b4d62013-04-22 12:15:39 -0700455static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
456{
457 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
458 crypt_ftr->persist_data_offset[1]) {
459 SLOGE("Crypt_ftr persist data regions overlap");
460 return -1;
461 }
462
463 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
464 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
465 return -1;
466 }
467
468 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
469 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
470 CRYPT_FOOTER_OFFSET) {
471 SLOGE("Persistent data extends past crypto footer");
472 return -1;
473 }
474
475 return 0;
476}
477
478static int load_persistent_data(void)
479{
480 struct crypt_mnt_ftr crypt_ftr;
481 struct crypt_persist_data *pdata = NULL;
482 char encrypted_state[PROPERTY_VALUE_MAX];
483 char *fname;
484 int found = 0;
485 int fd;
486 int ret;
487 int i;
488
489 if (persist_data) {
490 /* Nothing to do, we've already loaded or initialized it */
491 return 0;
492 }
493
494
495 /* If not encrypted, just allocate an empty table and initialize it */
496 property_get("ro.crypto.state", encrypted_state, "");
497 if (strcmp(encrypted_state, "encrypted") ) {
498 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
499 if (pdata) {
500 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
501 persist_data = pdata;
502 return 0;
503 }
504 return -1;
505 }
506
507 if(get_crypt_ftr_and_key(&crypt_ftr)) {
508 return -1;
509 }
510
511 if ((crypt_ftr.major_version != 1) || (crypt_ftr.minor_version != 1)) {
512 SLOGE("Crypt_ftr version doesn't support persistent data");
513 return -1;
514 }
515
516 if (get_crypt_ftr_info(&fname, NULL)) {
517 return -1;
518 }
519
520 ret = validate_persistent_data_storage(&crypt_ftr);
521 if (ret) {
522 return -1;
523 }
524
525 fd = open(fname, O_RDONLY);
526 if (fd < 0) {
527 SLOGE("Cannot open %s metadata file", fname);
528 return -1;
529 }
530
531 if (persist_data == NULL) {
532 pdata = malloc(crypt_ftr.persist_data_size);
533 if (pdata == NULL) {
534 SLOGE("Cannot allocate memory for persistent data");
535 goto err;
536 }
537 }
538
539 for (i = 0; i < 2; i++) {
540 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
541 SLOGE("Cannot seek to read persistent data on %s", fname);
542 goto err2;
543 }
544 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
545 SLOGE("Error reading persistent data on iteration %d", i);
546 goto err2;
547 }
548 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
549 found = 1;
550 break;
551 }
552 }
553
554 if (!found) {
555 SLOGI("Could not find valid persistent data, creating");
556 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
557 }
558
559 /* Success */
560 persist_data = pdata;
561 close(fd);
562 return 0;
563
564err2:
565 free(pdata);
566
567err:
568 close(fd);
569 return -1;
570}
571
572static int save_persistent_data(void)
573{
574 struct crypt_mnt_ftr crypt_ftr;
575 struct crypt_persist_data *pdata;
576 char *fname;
577 off64_t write_offset;
578 off64_t erase_offset;
579 int found = 0;
580 int fd;
581 int ret;
582
583 if (persist_data == NULL) {
584 SLOGE("No persistent data to save");
585 return -1;
586 }
587
588 if(get_crypt_ftr_and_key(&crypt_ftr)) {
589 return -1;
590 }
591
592 if ((crypt_ftr.major_version != 1) || (crypt_ftr.minor_version != 1)) {
593 SLOGE("Crypt_ftr version doesn't support persistent data");
594 return -1;
595 }
596
597 ret = validate_persistent_data_storage(&crypt_ftr);
598 if (ret) {
599 return -1;
600 }
601
602 if (get_crypt_ftr_info(&fname, NULL)) {
603 return -1;
604 }
605
606 fd = open(fname, O_RDWR);
607 if (fd < 0) {
608 SLOGE("Cannot open %s metadata file", fname);
609 return -1;
610 }
611
612 pdata = malloc(crypt_ftr.persist_data_size);
613 if (pdata == NULL) {
614 SLOGE("Cannot allocate persistant data");
615 goto err;
616 }
617
618 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
619 SLOGE("Cannot seek to read persistent data on %s", fname);
620 goto err2;
621 }
622
623 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
624 SLOGE("Error reading persistent data before save");
625 goto err2;
626 }
627
628 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
629 /* The first copy is the curent valid copy, so write to
630 * the second copy and erase this one */
631 write_offset = crypt_ftr.persist_data_offset[1];
632 erase_offset = crypt_ftr.persist_data_offset[0];
633 } else {
634 /* The second copy must be the valid copy, so write to
635 * the first copy, and erase the second */
636 write_offset = crypt_ftr.persist_data_offset[0];
637 erase_offset = crypt_ftr.persist_data_offset[1];
638 }
639
640 /* Write the new copy first, if successful, then erase the old copy */
641 if (lseek(fd, write_offset, SEEK_SET) < 0) {
642 SLOGE("Cannot seek to write persistent data");
643 goto err2;
644 }
645 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
646 (int) crypt_ftr.persist_data_size) {
647 if (lseek(fd, erase_offset, SEEK_SET) < 0) {
648 SLOGE("Cannot seek to erase previous persistent data");
649 goto err2;
650 }
651 fsync(fd);
652 memset(pdata, 0, crypt_ftr.persist_data_size);
653 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
654 (int) crypt_ftr.persist_data_size) {
655 SLOGE("Cannot write to erase previous persistent data");
656 goto err2;
657 }
658 fsync(fd);
659 } else {
660 SLOGE("Cannot write to save persistent data");
661 goto err2;
662 }
663
664 /* Success */
665 free(pdata);
666 close(fd);
667 return 0;
668
669err2:
670 free(pdata);
671err:
672 close(fd);
673 return -1;
674}
675
Paul Lawrencef4faa572014-01-29 13:31:03 -0800676static int hexdigit (char c)
677{
678 if (c >= '0' && c <= '9') return c - '0';
679 c = tolower(c);
680 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
681 return -1;
682}
683
684static unsigned char* convert_hex_ascii_to_key(const char* master_key_ascii,
685 unsigned int* out_keysize)
686{
687 unsigned int i;
688 *out_keysize = 0;
689
690 size_t size = strlen (master_key_ascii);
691 if (size % 2) {
692 SLOGE("Trying to convert ascii string of odd length");
693 return NULL;
694 }
695
696 unsigned char* master_key = (unsigned char*) malloc(size / 2);
697 if (master_key == 0) {
698 SLOGE("Cannot allocate");
699 return NULL;
700 }
701
702 for (i = 0; i < size; i += 2) {
703 int high_nibble = hexdigit (master_key_ascii[i]);
704 int low_nibble = hexdigit (master_key_ascii[i + 1]);
705
706 if(high_nibble < 0 || low_nibble < 0) {
707 SLOGE("Invalid hex string");
708 free (master_key);
709 return NULL;
710 }
711
712 master_key[*out_keysize] = high_nibble * 16 + low_nibble;
713 (*out_keysize)++;
714 }
715
716 return master_key;
717}
718
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800719/* Convert a binary key of specified length into an ascii hex string equivalent,
720 * without the leading 0x and with null termination
721 */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800722static void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800723 char *master_key_ascii)
724{
725 unsigned int i, a;
726 unsigned char nibble;
727
728 for (i=0, a=0; i<keysize; i++, a+=2) {
729 /* For each byte, write out two ascii hex digits */
730 nibble = (master_key[i] >> 4) & 0xf;
731 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
732
733 nibble = master_key[i] & 0xf;
734 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
735 }
736
737 /* Add the null termination */
738 master_key_ascii[a] = '\0';
739
740}
741
Ken Sumralldb5e0262013-02-05 17:39:48 -0800742static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
743 char *real_blk_name, const char *name, int fd,
744 char *extra_params)
745{
746 char buffer[DM_CRYPT_BUF_SIZE];
747 struct dm_ioctl *io;
748 struct dm_target_spec *tgt;
749 char *crypt_params;
750 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
751 int i;
752
753 io = (struct dm_ioctl *) buffer;
754
755 /* Load the mapping table for this device */
756 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
757
758 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
759 io->target_count = 1;
760 tgt->status = 0;
761 tgt->sector_start = 0;
762 tgt->length = crypt_ftr->fs_size;
763 strcpy(tgt->target_type, "crypt");
764
765 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
766 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
767 sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
768 master_key_ascii, real_blk_name, extra_params);
769 crypt_params += strlen(crypt_params) + 1;
770 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
771 tgt->next = crypt_params - buffer;
772
773 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
774 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
775 break;
776 }
777 usleep(500000);
778 }
779
780 if (i == TABLE_LOAD_RETRIES) {
781 /* We failed to load the table, return an error */
782 return -1;
783 } else {
784 return i + 1;
785 }
786}
787
788
789static int get_dm_crypt_version(int fd, const char *name, int *version)
790{
791 char buffer[DM_CRYPT_BUF_SIZE];
792 struct dm_ioctl *io;
793 struct dm_target_versions *v;
794 int i;
795
796 io = (struct dm_ioctl *) buffer;
797
798 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
799
800 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
801 return -1;
802 }
803
804 /* Iterate over the returned versions, looking for name of "crypt".
805 * When found, get and return the version.
806 */
807 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
808 while (v->next) {
809 if (! strcmp(v->name, "crypt")) {
810 /* We found the crypt driver, return the version, and get out */
811 version[0] = v->version[0];
812 version[1] = v->version[1];
813 version[2] = v->version[2];
814 return 0;
815 }
816 v = (struct dm_target_versions *)(((char *)v) + v->next);
817 }
818
819 return -1;
820}
821
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800822static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -0700823 char *real_blk_name, char *crypto_blk_name, const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800824{
825 char buffer[DM_CRYPT_BUF_SIZE];
826 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
827 char *crypt_params;
828 struct dm_ioctl *io;
829 struct dm_target_spec *tgt;
830 unsigned int minor;
831 int fd;
Ken Sumralle919efe2012-09-29 17:07:41 -0700832 int i;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800833 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800834 int version[3];
835 char *extra_params;
836 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800837
838 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
839 SLOGE("Cannot open device-mapper\n");
840 goto errout;
841 }
842
843 io = (struct dm_ioctl *) buffer;
844
845 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
846 if (ioctl(fd, DM_DEV_CREATE, io)) {
847 SLOGE("Cannot create dm-crypt device\n");
848 goto errout;
849 }
850
851 /* Get the device status, in particular, the name of it's device file */
852 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
853 if (ioctl(fd, DM_DEV_STATUS, io)) {
854 SLOGE("Cannot retrieve dm-crypt device status\n");
855 goto errout;
856 }
857 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
858 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
859
Ken Sumralldb5e0262013-02-05 17:39:48 -0800860 extra_params = "";
861 if (! get_dm_crypt_version(fd, name, version)) {
862 /* Support for allow_discards was added in version 1.11.0 */
863 if ((version[0] >= 2) ||
864 ((version[0] == 1) && (version[1] >= 11))) {
865 extra_params = "1 allow_discards";
866 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
867 }
Ken Sumralle919efe2012-09-29 17:07:41 -0700868 }
869
Ken Sumralldb5e0262013-02-05 17:39:48 -0800870 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
871 fd, extra_params);
872 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800873 SLOGE("Cannot load dm-crypt mapping table.\n");
874 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800875 } else if (load_count > 1) {
876 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800877 }
878
879 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -0800880 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800881
882 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
883 SLOGE("Cannot resume the dm-crypt device\n");
884 goto errout;
885 }
886
887 /* We made it here with no errors. Woot! */
888 retval = 0;
889
890errout:
891 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
892
893 return retval;
894}
895
Ken Sumrall29d8da82011-05-18 17:20:07 -0700896static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800897{
898 int fd;
899 char buffer[DM_CRYPT_BUF_SIZE];
900 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800901 int retval = -1;
902
903 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
904 SLOGE("Cannot open device-mapper\n");
905 goto errout;
906 }
907
908 io = (struct dm_ioctl *) buffer;
909
910 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
911 if (ioctl(fd, DM_DEV_REMOVE, io)) {
912 SLOGE("Cannot remove dm-crypt device\n");
913 goto errout;
914 }
915
916 /* We made it here with no errors. Woot! */
917 retval = 0;
918
919errout:
920 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
921
922 return retval;
923
924}
925
Paul Lawrencef4faa572014-01-29 13:31:03 -0800926static int pbkdf2(char *passwd, unsigned char *salt,
927 unsigned char *ikey, void *params UNUSED)
928{
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800929 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800930 unsigned int keysize;
931 char* master_key = (char*)convert_hex_ascii_to_key(passwd, &keysize);
932 if (!master_key) return -1;
933 PKCS5_PBKDF2_HMAC_SHA1(master_key, keysize, salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800934 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800935
936 free (master_key);
937 return 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800938}
939
Paul Lawrencef4faa572014-01-29 13:31:03 -0800940static int scrypt(char *passwd, unsigned char *salt,
941 unsigned char *ikey, void *params)
942{
Kenny Rootc4c70f12013-06-14 12:11:38 -0700943 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
944
945 int N = 1 << ftr->N_factor;
946 int r = 1 << ftr->r_factor;
947 int p = 1 << ftr->p_factor;
948
949 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800950 unsigned int keysize;
951 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &keysize);
952 if (!master_key) return -1;
953 crypto_scrypt(master_key, keysize, salt, SALT_LEN, N, r, p, ikey,
Kenny Rootc4c70f12013-06-14 12:11:38 -0700954 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800955
956 free (master_key);
957 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700958}
959
Ken Sumralle8744072011-01-18 22:01:55 -0800960static int encrypt_master_key(char *passwd, unsigned char *salt,
961 unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -0700962 unsigned char *encrypted_master_key,
963 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800964{
965 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
966 EVP_CIPHER_CTX e_ctx;
967 int encrypted_len, final_len;
968
969 /* Turn the password into a key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700970 get_device_scrypt_params(crypt_ftr);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800971 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
972 SLOGE("scrypt failed");
973 return -1;
974 }
Kenny Rootc4c70f12013-06-14 12:11:38 -0700975
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800976 /* Initialize the decryption engine */
977 if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
978 SLOGE("EVP_EncryptInit failed\n");
979 return -1;
980 }
981 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800982
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800983 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800984 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
985 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800986 SLOGE("EVP_EncryptUpdate failed\n");
987 return -1;
988 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800989 if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800990 SLOGE("EVP_EncryptFinal failed\n");
991 return -1;
992 }
993
994 if (encrypted_len + final_len != KEY_LEN_BYTES) {
995 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
996 return -1;
997 } else {
998 return 0;
999 }
1000}
1001
JP Abgrall7bdfa522013-11-15 13:42:56 -08001002static int decrypt_master_key_aux(char *passwd, unsigned char *salt,
Ken Sumralle8744072011-01-18 22:01:55 -08001003 unsigned char *encrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001004 unsigned char *decrypted_master_key,
1005 kdf_func kdf, void *kdf_params)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001006{
1007 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 -08001008 EVP_CIPHER_CTX d_ctx;
1009 int decrypted_len, final_len;
1010
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001011 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001012 if (kdf(passwd, salt, ikey, kdf_params)) {
1013 SLOGE("kdf failed");
1014 return -1;
1015 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001016
1017 /* Initialize the decryption engine */
1018 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
1019 return -1;
1020 }
1021 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1022 /* Decrypt the master key */
1023 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1024 encrypted_master_key, KEY_LEN_BYTES)) {
1025 return -1;
1026 }
1027 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
1028 return -1;
1029 }
1030
1031 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1032 return -1;
1033 } else {
1034 return 0;
1035 }
1036}
1037
Kenny Rootc4c70f12013-06-14 12:11:38 -07001038static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001039{
Kenny Rootc4c70f12013-06-14 12:11:38 -07001040 if (ftr->kdf_type == KDF_SCRYPT) {
1041 *kdf = scrypt;
1042 *kdf_params = ftr;
1043 } else {
1044 *kdf = pbkdf2;
1045 *kdf_params = NULL;
1046 }
1047}
1048
JP Abgrall7bdfa522013-11-15 13:42:56 -08001049static int decrypt_master_key(char *passwd, unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001050 struct crypt_mnt_ftr *crypt_ftr)
1051{
1052 kdf_func kdf;
1053 void *kdf_params;
1054 int ret;
1055
1056 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001057 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key, decrypted_master_key, kdf,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001058 kdf_params);
1059 if (ret != 0) {
1060 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001061 }
1062
1063 return ret;
1064}
1065
1066static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt,
1067 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001068 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001069 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001070 EVP_CIPHER_CTX e_ctx;
1071 int encrypted_len, final_len;
1072
1073 /* Get some random bits for a key */
1074 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -08001075 read(fd, key_buf, sizeof(key_buf));
1076 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001077 close(fd);
1078
1079 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001080 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001081}
1082
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001083static int wait_and_unmount(char *mountpoint)
1084{
1085 int i, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001086#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001087
1088 /* Now umount the tmpfs filesystem */
1089 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
1090 if (umount(mountpoint)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001091 if (errno == EINVAL) {
1092 /* EINVAL is returned if the directory is not a mountpoint,
1093 * i.e. there is no filesystem mounted there. So just get out.
1094 */
1095 break;
1096 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001097 sleep(1);
1098 i++;
1099 } else {
1100 break;
1101 }
1102 }
1103
1104 if (i < WAIT_UNMOUNT_COUNT) {
1105 SLOGD("unmounting %s succeeded\n", mountpoint);
1106 rc = 0;
1107 } else {
1108 SLOGE("unmounting %s failed\n", mountpoint);
1109 rc = -1;
1110 }
1111
1112 return rc;
1113}
1114
Ken Sumrallc5872692013-05-14 15:26:31 -07001115#define DATA_PREP_TIMEOUT 200
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001116static int prep_data_fs(void)
1117{
1118 int i;
1119
1120 /* Do the prep of the /data filesystem */
1121 property_set("vold.post_fs_data_done", "0");
1122 property_set("vold.decrypt", "trigger_post_fs_data");
1123 SLOGD("Just triggered post_fs_data\n");
1124
Ken Sumrallc5872692013-05-14 15:26:31 -07001125 /* Wait a max of 50 seconds, hopefully it takes much less */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001126 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001127 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001128
1129 property_get("vold.post_fs_data_done", p, "0");
1130 if (*p == '1') {
1131 break;
1132 } else {
1133 usleep(250000);
1134 }
1135 }
1136 if (i == DATA_PREP_TIMEOUT) {
1137 /* Ugh, we failed to prep /data in time. Bail. */
Ken Sumrallc5872692013-05-14 15:26:31 -07001138 SLOGE("post_fs_data timed out!\n");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001139 return -1;
1140 } else {
1141 SLOGD("post_fs_data done\n");
1142 return 0;
1143 }
1144}
1145
Paul Lawrencef4faa572014-01-29 13:31:03 -08001146static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001147{
1148 char fs_type[32];
1149 char real_blkdev[MAXPATHLEN];
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001150 char crypto_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001151 char fs_options[256];
1152 unsigned long mnt_flags;
1153 struct stat statbuf;
1154 int rc = -1, i;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001155 static int restart_successful = 0;
1156
1157 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001158 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001159 SLOGE("Encrypted filesystem not validated, aborting");
1160 return -1;
1161 }
1162
1163 if (restart_successful) {
1164 SLOGE("System already restarted with encrypted disk, aborting");
1165 return -1;
1166 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001167
Paul Lawrencef4faa572014-01-29 13:31:03 -08001168 if (restart_main) {
1169 /* Here is where we shut down the framework. The init scripts
1170 * start all services in one of three classes: core, main or late_start.
1171 * On boot, we start core and main. Now, we stop main, but not core,
1172 * as core includes vold and a few other really important things that
1173 * we need to keep running. Once main has stopped, we should be able
1174 * to umount the tmpfs /data, then mount the encrypted /data.
1175 * We then restart the class main, and also the class late_start.
1176 * At the moment, I've only put a few things in late_start that I know
1177 * are not needed to bring up the framework, and that also cause problems
1178 * with unmounting the tmpfs /data, but I hope to add add more services
1179 * to the late_start class as we optimize this to decrease the delay
1180 * till the user is asked for the password to the filesystem.
1181 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001182
Paul Lawrencef4faa572014-01-29 13:31:03 -08001183 /* The init files are setup to stop the class main when vold.decrypt is
1184 * set to trigger_reset_main.
1185 */
1186 property_set("vold.decrypt", "trigger_reset_main");
1187 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001188
Paul Lawrencef4faa572014-01-29 13:31:03 -08001189 /* Ugh, shutting down the framework is not synchronous, so until it
1190 * can be fixed, this horrible hack will wait a moment for it all to
1191 * shut down before proceeding. Without it, some devices cannot
1192 * restart the graphics services.
1193 */
1194 sleep(2);
1195 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001196
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001197 /* Now that the framework is shutdown, we should be able to umount()
1198 * the tmpfs filesystem, and mount the real one.
1199 */
1200
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001201 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1202 if (strlen(crypto_blkdev) == 0) {
1203 SLOGE("fs_crypto_blkdev not set\n");
1204 return -1;
1205 }
1206
Ken Sumralle5032c42012-04-01 23:58:44 -07001207 if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001208 /* If ro.crypto.readonly is set to 1, mount the decrypted
1209 * filesystem readonly. This is used when /data is mounted by
1210 * recovery mode.
1211 */
1212 char ro_prop[PROPERTY_VALUE_MAX];
1213 property_get("ro.crypto.readonly", ro_prop, "");
1214 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1215 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1216 rec->flags |= MS_RDONLY;
1217 }
1218
Ken Sumralle5032c42012-04-01 23:58:44 -07001219 /* If that succeeded, then mount the decrypted filesystem */
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001220 fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001221
Ken Sumralle5032c42012-04-01 23:58:44 -07001222 property_set("vold.decrypt", "trigger_load_persist_props");
1223 /* Create necessary paths on /data */
1224 if (prep_data_fs()) {
1225 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001226 }
Ken Sumralle5032c42012-04-01 23:58:44 -07001227
1228 /* startup service classes main and late_start */
1229 property_set("vold.decrypt", "trigger_restart_framework");
1230 SLOGD("Just triggered restart_framework\n");
1231
1232 /* Give it a few moments to get started */
1233 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001234 }
1235
Ken Sumrall0cc16632011-01-18 20:32:26 -08001236 if (rc == 0) {
1237 restart_successful = 1;
1238 }
1239
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001240 return rc;
1241}
1242
Paul Lawrencef4faa572014-01-29 13:31:03 -08001243int cryptfs_restart(void)
1244{
1245 /* Call internal implementation forcing a restart of main service group */
1246 return cryptfs_restart_internal(1);
1247}
1248
Mark Salyzyn3e971272014-01-21 13:27:04 -08001249static int do_crypto_complete(char *mount_point UNUSED)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001250{
1251 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001252 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001253 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001254
1255 property_get("ro.crypto.state", encrypted_state, "");
1256 if (strcmp(encrypted_state, "encrypted") ) {
1257 SLOGE("not running with encryption, aborting");
1258 return 1;
1259 }
1260
Ken Sumrall160b4d62013-04-22 12:15:39 -07001261 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001262 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001263
Ken Sumralle1a45852011-12-14 21:24:27 -08001264 /*
1265 * Only report this error if key_loc is a file and it exists.
1266 * If the device was never encrypted, and /data is not mountable for
1267 * some reason, returning 1 should prevent the UI from presenting the
1268 * a "enter password" screen, or worse, a "press button to wipe the
1269 * device" screen.
1270 */
1271 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1272 SLOGE("master key file does not exist, aborting");
1273 return 1;
1274 } else {
1275 SLOGE("Error getting crypt footer and key\n");
1276 return -1;
1277 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001278 }
1279
1280 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
1281 SLOGE("Encryption process didn't finish successfully\n");
1282 return -2; /* -2 is the clue to the UI that there is no usable data on the disk,
1283 * and give the user an option to wipe the disk */
1284 }
1285
1286 /* We passed the test! We shall diminish, and return to the west */
1287 return 0;
1288}
1289
Paul Lawrencef4faa572014-01-29 13:31:03 -08001290static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
1291 char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001292{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001293 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001294 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001295 char crypto_blkdev[MAXPATHLEN];
1296 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001297 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001298 unsigned int orig_failed_decrypt_count;
1299 int rc;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001300 kdf_func kdf;
1301 void *kdf_params;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001302
Paul Lawrencef4faa572014-01-29 13:31:03 -08001303 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1304 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001305
Paul Lawrencef4faa572014-01-29 13:31:03 -08001306 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
1307 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001308 SLOGE("Failed to decrypt master key\n");
1309 return -1;
1310 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001311 }
1312
Paul Lawrencef4faa572014-01-29 13:31:03 -08001313 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1314
1315 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1316 real_blkdev, crypto_blkdev, label)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001317 SLOGE("Error creating decrypted block device\n");
1318 return -1;
1319 }
1320
Alex Klyubin707795a2013-05-10 15:17:07 -07001321 /* If init detects an encrypted filesystem, it writes a file for each such
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001322 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
1323 * files and passes that data to me */
1324 /* Create a tmp mount point to try mounting the decryptd fs
1325 * Since we're here, the mount_point should be a tmpfs filesystem, so make
1326 * a directory in it to test mount the decrypted filesystem.
1327 */
1328 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
1329 mkdir(tmp_mount_point, 0755);
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001330 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001331 SLOGE("Error temp mounting decrypted block device\n");
Ken Sumrall29d8da82011-05-18 17:20:07 -07001332 delete_crypto_blk_dev(label);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001333 crypt_ftr->failed_decrypt_count++;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001334 } else {
1335 /* Success, so just umount and we'll mount it properly when we restart
1336 * the framework.
1337 */
1338 umount(tmp_mount_point);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001339 crypt_ftr->failed_decrypt_count = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001340 }
1341
Paul Lawrencef4faa572014-01-29 13:31:03 -08001342 if (orig_failed_decrypt_count != crypt_ftr->failed_decrypt_count) {
1343 put_crypt_ftr_and_key(crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001344 }
1345
Paul Lawrencef4faa572014-01-29 13:31:03 -08001346 if (crypt_ftr->failed_decrypt_count) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001347 /* We failed to mount the device, so return an error */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001348 rc = crypt_ftr->failed_decrypt_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001349
1350 } else {
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001351 /* Woot! Success! Save the name of the crypto block device
1352 * so we can mount it when restarting the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001353 */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001354 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001355
1356 /* Also save a the master key so we can reencrypted the key
1357 * the key when we want to change the password on it.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001358 */
Jason parks70a4b3f2011-01-28 10:10:47 -06001359 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001360 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001361 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001362 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001363 rc = 0;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001364 /*
1365 * Upgrade if we're not using the latest KDF.
1366 */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001367 if (crypt_ftr->kdf_type != KDF_SCRYPT) {
1368 crypt_ftr->kdf_type = KDF_SCRYPT;
1369 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1370 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001371 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001372 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001373 }
1374 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
1375 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001376 }
1377
1378 return rc;
1379}
1380
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001381/* Called by vold when it wants to undo the crypto mapping of a volume it
1382 * manages. This is usually in response to a factory reset, when we want
1383 * to undo the crypto mapping so the volume is formatted in the clear.
1384 */
1385int cryptfs_revert_volume(const char *label)
1386{
1387 return delete_crypto_blk_dev((char *)label);
1388}
1389
Ken Sumrall29d8da82011-05-18 17:20:07 -07001390/*
1391 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
1392 * Setup a dm-crypt mapping, use the saved master key from
1393 * setting up the /data mapping, and return the new device path.
1394 */
1395int cryptfs_setup_volume(const char *label, int major, int minor,
1396 char *crypto_sys_path, unsigned int max_path,
1397 int *new_major, int *new_minor)
1398{
1399 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
1400 struct crypt_mnt_ftr sd_crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001401 struct stat statbuf;
1402 int nr_sec, fd;
1403
1404 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
1405
Ken Sumrall160b4d62013-04-22 12:15:39 -07001406 get_crypt_ftr_and_key(&sd_crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001407
1408 /* Update the fs_size field to be the size of the volume */
1409 fd = open(real_blkdev, O_RDONLY);
1410 nr_sec = get_blkdev_size(fd);
1411 close(fd);
1412 if (nr_sec == 0) {
1413 SLOGE("Cannot get size of volume %s\n", real_blkdev);
1414 return -1;
1415 }
1416
1417 sd_crypt_ftr.fs_size = nr_sec;
1418 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
1419 crypto_blkdev, label);
1420
1421 stat(crypto_blkdev, &statbuf);
1422 *new_major = MAJOR(statbuf.st_rdev);
1423 *new_minor = MINOR(statbuf.st_rdev);
1424
1425 /* Create path to sys entry for this block device */
1426 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
1427
1428 return 0;
1429}
1430
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001431int cryptfs_crypto_complete(void)
1432{
1433 return do_crypto_complete("/data");
1434}
1435
Paul Lawrencef4faa572014-01-29 13:31:03 -08001436int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1437{
1438 char encrypted_state[PROPERTY_VALUE_MAX];
1439 property_get("ro.crypto.state", encrypted_state, "");
1440 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1441 SLOGE("encrypted fs already validated or not running with encryption,"
1442 " aborting");
1443 return -1;
1444 }
1445
1446 if (get_crypt_ftr_and_key(crypt_ftr)) {
1447 SLOGE("Error getting crypt footer and key");
1448 return -1;
1449 }
1450
1451 return 0;
1452}
1453
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001454int cryptfs_check_passwd(char *passwd)
1455{
Paul Lawrencef4faa572014-01-29 13:31:03 -08001456 struct crypt_mnt_ftr crypt_ftr;
1457 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001458
Paul Lawrencef4faa572014-01-29 13:31:03 -08001459 rc = check_unmounted_and_get_ftr(&crypt_ftr);
1460 if (rc)
1461 return rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001462
Paul Lawrencef4faa572014-01-29 13:31:03 -08001463 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
1464 DATA_MNT_POINT, "userdata");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001465 return rc;
1466}
1467
Ken Sumrall3ad90722011-10-04 20:38:29 -07001468int cryptfs_verify_passwd(char *passwd)
1469{
1470 struct crypt_mnt_ftr crypt_ftr;
1471 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001472 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07001473 char encrypted_state[PROPERTY_VALUE_MAX];
1474 int rc;
1475
1476 property_get("ro.crypto.state", encrypted_state, "");
1477 if (strcmp(encrypted_state, "encrypted") ) {
1478 SLOGE("device not encrypted, aborting");
1479 return -2;
1480 }
1481
1482 if (!master_key_saved) {
1483 SLOGE("encrypted fs not yet mounted, aborting");
1484 return -1;
1485 }
1486
1487 if (!saved_mount_point) {
1488 SLOGE("encrypted fs failed to save mount point, aborting");
1489 return -1;
1490 }
1491
Ken Sumrall160b4d62013-04-22 12:15:39 -07001492 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07001493 SLOGE("Error getting crypt footer and key\n");
1494 return -1;
1495 }
1496
1497 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
1498 /* If the device has no password, then just say the password is valid */
1499 rc = 0;
1500 } else {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001501 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001502 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
1503 /* They match, the password is correct */
1504 rc = 0;
1505 } else {
1506 /* If incorrect, sleep for a bit to prevent dictionary attacks */
1507 sleep(1);
1508 rc = 1;
1509 }
1510 }
1511
1512 return rc;
1513}
1514
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001515/* Initialize a crypt_mnt_ftr structure. The keysize is
1516 * defaulted to 16 bytes, and the filesystem size to 0.
1517 * Presumably, at a minimum, the caller will update the
1518 * filesystem size and crypto_type_name after calling this function.
1519 */
1520static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
1521{
Ken Sumrall160b4d62013-04-22 12:15:39 -07001522 off64_t off;
1523
1524 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001525 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07001526 ftr->major_version = CURRENT_MAJOR_VERSION;
1527 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001528 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06001529 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001530
Kenny Rootc4c70f12013-06-14 12:11:38 -07001531 ftr->kdf_type = KDF_SCRYPT;
1532 get_device_scrypt_params(ftr);
1533
Ken Sumrall160b4d62013-04-22 12:15:39 -07001534 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
1535 if (get_crypt_ftr_info(NULL, &off) == 0) {
1536 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
1537 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
1538 ftr->persist_data_size;
1539 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001540}
1541
Ken Sumrall29d8da82011-05-18 17:20:07 -07001542static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001543{
Ken Sumralle550f782013-08-20 13:48:23 -07001544 const char *args[10];
1545 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
1546 int num_args;
1547 int status;
1548 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001549 int rc = -1;
1550
Ken Sumrall29d8da82011-05-18 17:20:07 -07001551 if (type == EXT4_FS) {
Ken Sumralle550f782013-08-20 13:48:23 -07001552 args[0] = "/system/bin/make_ext4fs";
1553 args[1] = "-a";
1554 args[2] = "/data";
1555 args[3] = "-l";
1556 snprintf(size_str, sizeof(size_str), "%lld", size * 512);
1557 args[4] = size_str;
1558 args[5] = crypto_blkdev;
1559 num_args = 6;
1560 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
1561 args[0], args[1], args[2], args[3], args[4], args[5]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001562 } else if (type== FAT_FS) {
Ken Sumralle550f782013-08-20 13:48:23 -07001563 args[0] = "/system/bin/newfs_msdos";
1564 args[1] = "-F";
1565 args[2] = "32";
1566 args[3] = "-O";
1567 args[4] = "android";
1568 args[5] = "-c";
1569 args[6] = "8";
1570 args[7] = "-s";
1571 snprintf(size_str, sizeof(size_str), "%lld", size);
1572 args[8] = size_str;
1573 args[9] = crypto_blkdev;
1574 num_args = 10;
1575 SLOGI("Making empty filesystem with command %s %s %s %s %s %s %s %s %s %s\n",
1576 args[0], args[1], args[2], args[3], args[4], args[5],
1577 args[6], args[7], args[8], args[9]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001578 } else {
1579 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
1580 return -1;
1581 }
1582
Ken Sumralle550f782013-08-20 13:48:23 -07001583 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
1584
1585 if (tmp != 0) {
1586 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001587 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07001588 if (WIFEXITED(status)) {
1589 if (WEXITSTATUS(status)) {
1590 SLOGE("Error creating filesystem on %s, exit status %d ",
1591 crypto_blkdev, WEXITSTATUS(status));
1592 } else {
1593 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
1594 rc = 0;
1595 }
1596 } else {
1597 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
1598 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001599 }
1600
1601 return rc;
1602}
1603
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001604#define CRYPT_INPLACE_BUFSIZE 4096
1605#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001606
1607/* aligned 32K writes tends to make flash happy.
1608 * SD card association recommends it.
1609 */
1610#define BLOCKS_AT_A_TIME 8
1611
1612struct encryptGroupsData
1613{
1614 int realfd;
1615 int cryptofd;
1616 off64_t numblocks;
1617 off64_t one_pct, cur_pct, new_pct;
1618 off64_t blocks_already_done, tot_numblocks;
1619 char* real_blkdev, * crypto_blkdev;
1620 int count;
1621 off64_t offset;
1622 char* buffer;
1623};
1624
1625static void update_progress(struct encryptGroupsData* data)
1626{
1627 data->blocks_already_done++;
1628 data->new_pct = data->blocks_already_done / data->one_pct;
1629 if (data->new_pct > data->cur_pct) {
1630 char buf[8];
1631 data->cur_pct = data->new_pct;
1632 snprintf(buf, sizeof(buf), "%lld", data->cur_pct);
1633 property_set("vold.encrypt_progress", buf);
1634 }
1635}
1636
1637static int flush_outstanding_data(struct encryptGroupsData* data)
1638{
1639 if (data->count == 0) {
1640 return 0;
1641 }
1642
1643 SLOGV("Copying %d blocks at offset %llx", data->count, data->offset);
1644
1645 if (pread64(data->realfd, data->buffer,
1646 info.block_size * data->count, data->offset)
1647 <= 0) {
1648 SLOGE("Error reading real_blkdev %s for inplace encrypt",
1649 data->real_blkdev);
1650 return -1;
1651 }
1652
1653 if (pwrite64(data->cryptofd, data->buffer,
1654 info.block_size * data->count, data->offset)
1655 <= 0) {
1656 SLOGE("Error writing crypto_blkdev %s for inplace encrypt",
1657 data->crypto_blkdev);
1658 return -1;
1659 }
1660
1661 data->count = 0;
1662 return 0;
1663}
1664
1665static int encrypt_groups(struct encryptGroupsData* data)
1666{
1667 unsigned int i;
1668 u8 *block_bitmap = 0;
1669 unsigned int block;
1670 off64_t ret;
1671 int rc = -1;
1672
1673 data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME);
1674 if (!data->buffer) {
1675 SLOGE("Failed to allocate crypto buffer");
1676 goto errout;
1677 }
1678
1679 block_bitmap = malloc(info.block_size);
1680 if (!block_bitmap) {
1681 SLOGE("failed to allocate block bitmap");
1682 goto errout;
1683 }
1684
1685 for (i = 0; i < aux_info.groups; ++i) {
1686 SLOGI("Encrypting group %d", i);
1687
1688 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
1689 u32 block_count = min(info.blocks_per_group,
1690 aux_info.len_blocks - first_block);
1691
1692 off64_t offset = (u64)info.block_size
1693 * aux_info.bg_desc[i].bg_block_bitmap;
1694
1695 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
1696 if (ret != (int)info.block_size) {
1697 SLOGE("failed to read all of block group bitmap %d", i);
1698 goto errout;
1699 }
1700
1701 offset = (u64)info.block_size * first_block;
1702
1703 data->count = 0;
1704
1705 for (block = 0; block < block_count; block++) {
1706 update_progress(data);
1707 if (bitmap_get_bit(block_bitmap, block)) {
1708 if (data->count == 0) {
1709 data->offset = offset;
1710 }
1711 data->count++;
1712 } else {
1713 if (flush_outstanding_data(data)) {
1714 goto errout;
1715 }
1716 }
1717
1718 offset += info.block_size;
1719
1720 /* Write data if we are aligned or buffer size reached */
1721 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0
1722 || data->count == BLOCKS_AT_A_TIME) {
1723 if (flush_outstanding_data(data)) {
1724 goto errout;
1725 }
1726 }
1727 }
1728 if (flush_outstanding_data(data)) {
1729 goto errout;
1730 }
1731 }
1732
1733 rc = 0;
1734
1735errout:
1736 free(data->buffer);
1737 free(block_bitmap);
1738 return rc;
1739}
1740
1741static int cryptfs_enable_inplace_ext4(char *crypto_blkdev,
1742 char *real_blkdev,
1743 off64_t size,
1744 off64_t *size_already_done,
1745 off64_t tot_size)
1746{
1747 int i;
1748 struct encryptGroupsData data;
1749 int rc = -1;
1750
1751 memset(&data, 0, sizeof(data));
1752 data.real_blkdev = real_blkdev;
1753 data.crypto_blkdev = crypto_blkdev;
1754
1755 if ( (data.realfd = open(real_blkdev, O_RDWR)) < 0) {
1756 SLOGE("Error opening real_blkdev %s for inplace encrypt\n",
1757 real_blkdev);
1758 goto errout;
1759 }
1760
1761 if ( (data.cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
1762 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n",
1763 crypto_blkdev);
1764 goto errout;
1765 }
1766
1767 if (setjmp(setjmp_env)) {
1768 SLOGE("Reading extent caused an exception");
1769 goto errout;
1770 }
1771
1772 if (read_ext(data.realfd, 0) != 0) {
1773 SLOGE("Failed to read extent");
1774 goto errout;
1775 }
1776
1777 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
1778 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
1779 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
1780
1781 SLOGI("Encrypting filesystem in place...");
1782
1783 data.one_pct = data.tot_numblocks / 100;
1784 data.cur_pct = 0;
1785
1786 rc = encrypt_groups(&data);
1787 if (rc) {
1788 SLOGE("Error encrypting groups");
1789 goto errout;
1790 }
1791
1792 *size_already_done += size;
1793 rc = 0;
1794
1795errout:
1796 close(data.realfd);
1797 close(data.cryptofd);
1798
1799 return rc;
1800}
1801
1802static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev,
1803 off64_t size, off64_t *size_already_done,
1804 off64_t tot_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001805{
1806 int realfd, cryptofd;
1807 char *buf[CRYPT_INPLACE_BUFSIZE];
1808 int rc = -1;
1809 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001810 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001811 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001812
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001813 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
1814 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
1815 return -1;
1816 }
1817
1818 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
1819 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1820 close(realfd);
1821 return -1;
1822 }
1823
1824 /* This is pretty much a simple loop of reading 4K, and writing 4K.
1825 * The size passed in is the number of 512 byte sectors in the filesystem.
1826 * So compute the number of whole 4K blocks we should read/write,
1827 * and the remainder.
1828 */
1829 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
1830 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001831 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
1832 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001833
1834 SLOGE("Encrypting filesystem in place...");
1835
Ken Sumrall29d8da82011-05-18 17:20:07 -07001836 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001837 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001838 /* process the majority of the filesystem in blocks */
1839 for (i=0; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001840 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001841 if (new_pct > cur_pct) {
1842 char buf[8];
1843
1844 cur_pct = new_pct;
1845 snprintf(buf, sizeof(buf), "%lld", cur_pct);
1846 property_set("vold.encrypt_progress", buf);
1847 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001848 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1849 SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1850 goto errout;
1851 }
1852 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1853 SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1854 goto errout;
1855 }
1856 }
1857
1858 /* Do any remaining sectors */
1859 for (i=0; i<remainder; i++) {
1860 if (unix_read(realfd, buf, 512) <= 0) {
1861 SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1862 goto errout;
1863 }
1864 if (unix_write(cryptofd, buf, 512) <= 0) {
1865 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1866 goto errout;
1867 }
1868 }
1869
Ken Sumrall29d8da82011-05-18 17:20:07 -07001870 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001871 rc = 0;
1872
1873errout:
1874 close(realfd);
1875 close(cryptofd);
1876
1877 return rc;
1878}
1879
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001880static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev,
1881 off64_t size, off64_t *size_already_done,
1882 off64_t tot_size)
1883{
1884 if (cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev,
1885 size, size_already_done, tot_size) == 0) {
1886 return 0;
1887 }
1888
1889 return cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev,
1890 size, size_already_done, tot_size);
1891}
1892
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001893#define CRYPTO_ENABLE_WIPE 1
1894#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001895
1896#define FRAMEWORK_BOOT_WAIT 60
1897
Ken Sumrall29d8da82011-05-18 17:20:07 -07001898static inline int should_encrypt(struct volume_info *volume)
1899{
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001900 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
Ken Sumrall29d8da82011-05-18 17:20:07 -07001901 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
1902}
1903
JP Abgrall502dc742013-11-01 13:06:20 -07001904int cryptfs_enable(char *howarg, char *passwd, int allow_reboot)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001905{
1906 int how = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001907 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN], sd_crypto_blkdev[MAXPATHLEN];
Ken Sumralle5032c42012-04-01 23:58:44 -07001908 unsigned long nr_sec;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001909 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Ken Sumrall319b1042011-06-14 14:01:55 -07001910 int rc=-1, fd, i, ret;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001911 struct crypt_mnt_ftr crypt_ftr, sd_crypt_ftr;;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001912 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001913 char tmpfs_options[PROPERTY_VALUE_MAX];
1914 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001915 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07001916 char key_loc[PROPERTY_VALUE_MAX];
1917 char fuse_sdcard[PROPERTY_VALUE_MAX];
1918 char *sd_mnt_point;
1919 char sd_blk_dev[256] = { 0 };
1920 int num_vols;
1921 struct volume_info *vol_list = 0;
1922 off64_t cur_encryption_done=0, tot_encryption_size=0;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001923
1924 property_get("ro.crypto.state", encrypted_state, "");
JP Abgrall502dc742013-11-01 13:06:20 -07001925 if (!strcmp(encrypted_state, "encrypted")) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001926 SLOGE("Device is already running encrypted, aborting");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001927 goto error_unencrypted;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001928 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001929
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001930 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001931
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001932 if (!strcmp(howarg, "wipe")) {
1933 how = CRYPTO_ENABLE_WIPE;
1934 } else if (! strcmp(howarg, "inplace")) {
1935 how = CRYPTO_ENABLE_INPLACE;
1936 } else {
1937 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08001938 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001939 }
1940
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001941 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001942
Ken Sumrall3ed82362011-01-28 23:31:16 -08001943 /* Get the size of the real block device */
1944 fd = open(real_blkdev, O_RDONLY);
1945 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
1946 SLOGE("Cannot get size of block device %s\n", real_blkdev);
1947 goto error_unencrypted;
1948 }
1949 close(fd);
1950
1951 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001952 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001953 unsigned int fs_size_sec, max_fs_size_sec;
1954
1955 fs_size_sec = get_fs_size(real_blkdev);
1956 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1957
1958 if (fs_size_sec > max_fs_size_sec) {
1959 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
1960 goto error_unencrypted;
1961 }
1962 }
1963
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001964 /* Get a wakelock as this may take a while, and we don't want the
1965 * device to sleep on us. We'll grab a partial wakelock, and if the UI
1966 * wants to keep the screen on, it can grab a full wakelock.
1967 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001968 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001969 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
1970
Jeff Sharkey7382f812012-08-23 14:08:59 -07001971 /* Get the sdcard mount point */
Jeff Sharkeyb77bc462012-10-01 14:36:26 -07001972 sd_mnt_point = getenv("EMULATED_STORAGE_SOURCE");
Jeff Sharkey7382f812012-08-23 14:08:59 -07001973 if (!sd_mnt_point) {
1974 sd_mnt_point = getenv("EXTERNAL_STORAGE");
1975 }
1976 if (!sd_mnt_point) {
1977 sd_mnt_point = "/mnt/sdcard";
1978 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07001979
1980 num_vols=vold_getNumDirectVolumes();
1981 vol_list = malloc(sizeof(struct volume_info) * num_vols);
1982 vold_getDirectVolumeList(vol_list);
1983
1984 for (i=0; i<num_vols; i++) {
1985 if (should_encrypt(&vol_list[i])) {
1986 fd = open(vol_list[i].blk_dev, O_RDONLY);
1987 if ( (vol_list[i].size = get_blkdev_size(fd)) == 0) {
1988 SLOGE("Cannot get size of block device %s\n", vol_list[i].blk_dev);
1989 goto error_unencrypted;
1990 }
1991 close(fd);
1992
Ken Sumrall3b170052011-07-11 15:38:57 -07001993 ret=vold_disableVol(vol_list[i].label);
Ken Sumrall319b1042011-06-14 14:01:55 -07001994 if ((ret < 0) && (ret != UNMOUNT_NOT_MOUNTED_ERR)) {
1995 /* -2 is returned when the device exists but is not currently mounted.
1996 * ignore the error and continue. */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001997 SLOGE("Failed to unmount volume %s\n", vol_list[i].label);
1998 goto error_unencrypted;
1999 }
2000 }
2001 }
2002
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002003 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002004 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002005 */
2006 property_set("vold.decrypt", "trigger_shutdown_framework");
2007 SLOGD("Just asked init to shut down class main\n");
2008
Ken Sumrall425524d2012-06-14 20:55:28 -07002009 if (vold_unmountAllAsecs()) {
2010 /* Just report the error. If any are left mounted,
2011 * umounting /data below will fail and handle the error.
2012 */
2013 SLOGE("Error unmounting internal asecs");
2014 }
2015
Ken Sumrall29d8da82011-05-18 17:20:07 -07002016 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
2017 if (!strcmp(fuse_sdcard, "true")) {
2018 /* This is a device using the fuse layer to emulate the sdcard semantics
2019 * on top of the userdata partition. vold does not manage it, it is managed
2020 * by the sdcard service. The sdcard service was killed by the property trigger
2021 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
2022 * unlike the case for vold managed devices above.
2023 */
2024 if (wait_and_unmount(sd_mnt_point)) {
2025 goto error_shutting_down;
2026 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08002027 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002028
2029 /* Now unmount the /data partition. */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002030 if (wait_and_unmount(DATA_MNT_POINT)) {
JP Abgrall502dc742013-11-01 13:06:20 -07002031 if (allow_reboot) {
2032 goto error_shutting_down;
2033 } else {
2034 goto error_unencrypted;
2035 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002036 }
2037
2038 /* Do extra work for a better UX when doing the long inplace encryption */
2039 if (how == CRYPTO_ENABLE_INPLACE) {
2040 /* Now that /data is unmounted, we need to mount a tmpfs
2041 * /data, set a property saying we're doing inplace encryption,
2042 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002043 */
Ken Sumralle5032c42012-04-01 23:58:44 -07002044 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002045 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002046 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002047 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08002048 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002049
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002050 /* restart the framework. */
2051 /* Create necessary paths on /data */
2052 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002053 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002054 }
2055
Ken Sumrall92736ef2012-10-17 20:57:14 -07002056 /* Ugh, shutting down the framework is not synchronous, so until it
2057 * can be fixed, this horrible hack will wait a moment for it all to
2058 * shut down before proceeding. Without it, some devices cannot
2059 * restart the graphics services.
2060 */
2061 sleep(2);
2062
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002063 /* startup service classes main and late_start */
2064 property_set("vold.decrypt", "trigger_restart_min_framework");
2065 SLOGD("Just triggered restart_min_framework\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002066
Ken Sumrall7df84122011-01-18 14:04:08 -08002067 /* OK, the framework is restarted and will soon be showing a
2068 * progress bar. Time to setup an encrypted mapping, and
2069 * either write a new filesystem, or encrypt in place updating
2070 * the progress bar as we work.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002071 */
2072 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002073
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002074 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002075 /* Initialize a crypt_mnt_ftr for the partition */
2076 cryptfs_init_crypt_mnt_ftr(&crypt_ftr);
Ken Sumrall160b4d62013-04-22 12:15:39 -07002077
Ken Sumrall29d8da82011-05-18 17:20:07 -07002078 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
2079 crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
2080 } else {
2081 crypt_ftr.fs_size = nr_sec;
2082 }
Ken Sumralld33d4172011-02-01 00:49:13 -08002083 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrencef4faa572014-01-29 13:31:03 -08002084
2085 /** @TODO If we keep this route, must pass in crypt_type.
2086 * If all devices are encrypted by default, we don't need that change.
2087 */
2088 crypt_ftr.crypt_type = CRYPT_TYPE_PASSWORD;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002089 strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
2090
2091 /* Make an encrypted master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07002092 if (create_encrypted_random_key(passwd, crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002093 SLOGE("Cannot create encrypted master key\n");
JP Abgrall502dc742013-11-01 13:06:20 -07002094 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002095 }
2096
2097 /* Write the key to the end of the partition */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002098 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002099
Ken Sumrall160b4d62013-04-22 12:15:39 -07002100 /* If any persistent data has been remembered, save it.
2101 * If none, create a valid empty table and save that.
2102 */
2103 if (!persist_data) {
2104 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
2105 if (pdata) {
2106 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2107 persist_data = pdata;
2108 }
2109 }
2110 if (persist_data) {
2111 save_persistent_data();
2112 }
2113
JP Abgrall7bdfa522013-11-15 13:42:56 -08002114 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002115 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
2116 "userdata");
2117
Ken Sumrall128626f2011-06-28 18:45:14 -07002118 /* The size of the userdata partition, and add in the vold volumes below */
2119 tot_encryption_size = crypt_ftr.fs_size;
2120
Ken Sumrall29d8da82011-05-18 17:20:07 -07002121 /* setup crypto mapping for all encryptable volumes handled by vold */
2122 for (i=0; i<num_vols; i++) {
2123 if (should_encrypt(&vol_list[i])) {
2124 vol_list[i].crypt_ftr = crypt_ftr; /* gotta love struct assign */
2125 vol_list[i].crypt_ftr.fs_size = vol_list[i].size;
2126 create_crypto_blk_dev(&vol_list[i].crypt_ftr, decrypted_master_key,
2127 vol_list[i].blk_dev, vol_list[i].crypto_blkdev,
2128 vol_list[i].label);
Ken Sumrall128626f2011-06-28 18:45:14 -07002129 tot_encryption_size += vol_list[i].size;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002130 }
2131 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002132
2133 if (how == CRYPTO_ENABLE_WIPE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002134 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr.fs_size, EXT4_FS);
2135 /* Encrypt all encryptable volumes handled by vold */
2136 if (!rc) {
2137 for (i=0; i<num_vols; i++) {
2138 if (should_encrypt(&vol_list[i])) {
2139 rc = cryptfs_enable_wipe(vol_list[i].crypto_blkdev,
2140 vol_list[i].crypt_ftr.fs_size, FAT_FS);
2141 }
2142 }
2143 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002144 } else if (how == CRYPTO_ENABLE_INPLACE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002145 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size,
2146 &cur_encryption_done, tot_encryption_size);
2147 /* Encrypt all encryptable volumes handled by vold */
2148 if (!rc) {
2149 for (i=0; i<num_vols; i++) {
2150 if (should_encrypt(&vol_list[i])) {
2151 rc = cryptfs_enable_inplace(vol_list[i].crypto_blkdev,
2152 vol_list[i].blk_dev,
2153 vol_list[i].crypt_ftr.fs_size,
2154 &cur_encryption_done, tot_encryption_size);
2155 }
2156 }
2157 }
2158 if (!rc) {
2159 /* The inplace routine never actually sets the progress to 100%
2160 * due to the round down nature of integer division, so set it here */
2161 property_set("vold.encrypt_progress", "100");
2162 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002163 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002164 /* Shouldn't happen */
2165 SLOGE("cryptfs_enable: internal error, unknown option\n");
JP Abgrall502dc742013-11-01 13:06:20 -07002166 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002167 }
2168
2169 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002170 delete_crypto_blk_dev("userdata");
2171 for (i=0; i<num_vols; i++) {
2172 if (should_encrypt(&vol_list[i])) {
2173 delete_crypto_blk_dev(vol_list[i].label);
2174 }
2175 }
2176
2177 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002178
2179 if (! rc) {
2180 /* Success */
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08002181
Ken Sumralld33d4172011-02-01 00:49:13 -08002182 /* Clear the encryption in progres flag in the footer */
2183 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002184 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08002185
Ken Sumrall29d8da82011-05-18 17:20:07 -07002186 sleep(2); /* Give the UI a chance to show 100% progress */
Ken Sumralladfba362013-06-04 16:37:52 -07002187 cryptfs_reboot(0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002188 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002189 char value[PROPERTY_VALUE_MAX];
2190
Ken Sumrall319369a2012-06-27 16:30:18 -07002191 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002192 if (!strcmp(value, "1")) {
2193 /* wipe data if encryption failed */
2194 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
2195 mkdir("/cache/recovery", 0700);
Nick Kralevich4684e582012-06-26 15:07:03 -07002196 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002197 if (fd >= 0) {
2198 write(fd, "--wipe_data", strlen("--wipe_data") + 1);
2199 close(fd);
2200 } else {
2201 SLOGE("could not open /cache/recovery/command\n");
2202 }
Ken Sumralladfba362013-06-04 16:37:52 -07002203 cryptfs_reboot(1);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002204 } else {
2205 /* set property to trigger dialog */
2206 property_set("vold.encrypt_progress", "error_partially_encrypted");
2207 release_wake_lock(lockid);
2208 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002209 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002210 }
2211
Ken Sumrall3ed82362011-01-28 23:31:16 -08002212 /* hrm, the encrypt step claims success, but the reboot failed.
2213 * This should not happen.
2214 * Set the property and return. Hope the framework can deal with it.
2215 */
2216 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002217 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002218 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08002219
2220error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07002221 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002222 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002223 if (lockid[0]) {
2224 release_wake_lock(lockid);
2225 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002226 return -1;
2227
2228error_shutting_down:
2229 /* we failed, and have not encrypted anthing, so the users's data is still intact,
2230 * but the framework is stopped and not restarted to show the error, so it's up to
2231 * vold to restart the system.
2232 */
2233 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Ken Sumralladfba362013-06-04 16:37:52 -07002234 cryptfs_reboot(0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002235
2236 /* shouldn't get here */
2237 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07002238 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002239 if (lockid[0]) {
2240 release_wake_lock(lockid);
2241 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002242 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002243}
2244
Paul Lawrencef4faa572014-01-29 13:31:03 -08002245int cryptfs_changepw(int crypt_type, char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002246{
2247 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002248 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002249
2250 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08002251 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08002252 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002253 return -1;
2254 }
2255
Paul Lawrencef4faa572014-01-29 13:31:03 -08002256 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2257 SLOGE("Invalid crypt_type %d", crypt_type);
2258 return -1;
2259 }
2260
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002261 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002262 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08002263 SLOGE("Error getting crypt footer and key");
2264 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002265 }
2266
Paul Lawrencef4faa572014-01-29 13:31:03 -08002267 crypt_ftr.crypt_type = crypt_type;
2268
2269 encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
2270 : newpw,
2271 crypt_ftr.salt,
2272 saved_master_key,
2273 crypt_ftr.master_key,
2274 &crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002275
Jason parks70a4b3f2011-01-28 10:10:47 -06002276 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002277 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002278
2279 return 0;
2280}
Ken Sumrall160b4d62013-04-22 12:15:39 -07002281
2282static int persist_get_key(char *fieldname, char *value)
2283{
2284 unsigned int i;
2285
2286 if (persist_data == NULL) {
2287 return -1;
2288 }
2289 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2290 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2291 /* We found it! */
2292 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
2293 return 0;
2294 }
2295 }
2296
2297 return -1;
2298}
2299
2300static int persist_set_key(char *fieldname, char *value, int encrypted)
2301{
2302 unsigned int i;
2303 unsigned int num;
2304 struct crypt_mnt_ftr crypt_ftr;
2305 unsigned int max_persistent_entries;
2306 unsigned int dsize;
2307
2308 if (persist_data == NULL) {
2309 return -1;
2310 }
2311
2312 /* If encrypted, use the values from the crypt_ftr, otherwise
2313 * use the values for the current spec.
2314 */
2315 if (encrypted) {
2316 if(get_crypt_ftr_and_key(&crypt_ftr)) {
2317 return -1;
2318 }
2319 dsize = crypt_ftr.persist_data_size;
2320 } else {
2321 dsize = CRYPT_PERSIST_DATA_SIZE;
2322 }
2323 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
2324 sizeof(struct crypt_persist_entry);
2325
2326 num = persist_data->persist_valid_entries;
2327
2328 for (i = 0; i < num; i++) {
2329 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2330 /* We found an existing entry, update it! */
2331 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
2332 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
2333 return 0;
2334 }
2335 }
2336
2337 /* We didn't find it, add it to the end, if there is room */
2338 if (persist_data->persist_valid_entries < max_persistent_entries) {
2339 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
2340 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
2341 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
2342 persist_data->persist_valid_entries++;
2343 return 0;
2344 }
2345
2346 return -1;
2347}
2348
2349/* Return the value of the specified field. */
2350int cryptfs_getfield(char *fieldname, char *value, int len)
2351{
2352 char temp_value[PROPERTY_VALUE_MAX];
2353 char real_blkdev[MAXPATHLEN];
2354 /* 0 is success, 1 is not encrypted,
2355 * -1 is value not set, -2 is any other error
2356 */
2357 int rc = -2;
2358
2359 if (persist_data == NULL) {
2360 load_persistent_data();
2361 if (persist_data == NULL) {
2362 SLOGE("Getfield error, cannot load persistent data");
2363 goto out;
2364 }
2365 }
2366
2367 if (!persist_get_key(fieldname, temp_value)) {
2368 /* We found it, copy it to the caller's buffer and return */
2369 strlcpy(value, temp_value, len);
2370 rc = 0;
2371 } else {
2372 /* Sadness, it's not there. Return the error */
2373 rc = -1;
2374 }
2375
2376out:
2377 return rc;
2378}
2379
2380/* Set the value of the specified field. */
2381int cryptfs_setfield(char *fieldname, char *value)
2382{
2383 struct crypt_persist_data stored_pdata;
2384 struct crypt_persist_data *pdata_p;
2385 struct crypt_mnt_ftr crypt_ftr;
2386 char encrypted_state[PROPERTY_VALUE_MAX];
2387 /* 0 is success, -1 is an error */
2388 int rc = -1;
2389 int encrypted = 0;
2390
2391 if (persist_data == NULL) {
2392 load_persistent_data();
2393 if (persist_data == NULL) {
2394 SLOGE("Setfield error, cannot load persistent data");
2395 goto out;
2396 }
2397 }
2398
2399 property_get("ro.crypto.state", encrypted_state, "");
2400 if (!strcmp(encrypted_state, "encrypted") ) {
2401 encrypted = 1;
2402 }
2403
2404 if (persist_set_key(fieldname, value, encrypted)) {
2405 goto out;
2406 }
2407
2408 /* If we are running encrypted, save the persistent data now */
2409 if (encrypted) {
2410 if (save_persistent_data()) {
2411 SLOGE("Setfield error, cannot save persistent data");
2412 goto out;
2413 }
2414 }
2415
2416 rc = 0;
2417
2418out:
2419 return rc;
2420}
Paul Lawrencef4faa572014-01-29 13:31:03 -08002421
2422/* Checks userdata. Attempt to mount the volume if default-
2423 * encrypted.
2424 * On success trigger next init phase and return 0.
2425 * Currently do not handle failure - see TODO below.
2426 */
2427int cryptfs_mount_default_encrypted(void)
2428{
2429 char decrypt_state[PROPERTY_VALUE_MAX];
2430 property_get("vold.decrypt", decrypt_state, "0");
2431 if (!strcmp(decrypt_state, "0")) {
2432 SLOGE("Not encrypted - should not call here");
2433 } else {
2434 int crypt_type = cryptfs_get_password_type();
2435 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2436 SLOGE("Bad crypt type - error");
2437 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
2438 SLOGD("Password is not default - "
2439 "starting min framework to prompt");
2440 property_set("vold.decrypt", "trigger_restart_min_framework");
2441 return 0;
2442 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
2443 SLOGD("Password is default - restarting filesystem");
2444 cryptfs_restart_internal(0);
2445 return 0;
2446 } else {
2447 SLOGE("Encrypted, default crypt type but can't decrypt");
2448 }
2449 }
2450
2451 /** @TODO make sure we factory wipe in this situation
2452 * In general if we got here there is no recovery
2453 */
2454 return 0;
2455}
2456
2457/* Returns type of the password, default, pattern, pin or password.
2458 */
2459int cryptfs_get_password_type(void)
2460{
2461 struct crypt_mnt_ftr crypt_ftr;
2462
2463 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2464 SLOGE("Error getting crypt footer and key\n");
2465 return -1;
2466 }
2467
2468 return crypt_ftr.crypt_type;
2469}