blob: d994449c12350bd3d0c4314588f4103e24b66b88 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* TO DO:
18 * 1. Perhaps keep several copies of the encrypted key, in case something
19 * goes horribly wrong?
20 *
21 */
22
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <unistd.h>
27#include <stdio.h>
28#include <sys/ioctl.h>
29#include <linux/dm-ioctl.h>
30#include <libgen.h>
31#include <stdlib.h>
32#include <sys/param.h>
33#include <string.h>
34#include <sys/mount.h>
35#include <openssl/evp.h>
36#include <openssl/sha.h>
37#include <errno.h>
38#include <cutils/android_reboot.h>
39#include <ext4.h>
40#include <linux/kdev_t.h>
41#include "cryptfs.h"
42#define LOG_TAG "Cryptfs"
43#include "cutils/log.h"
44#include "cutils/properties.h"
45#include "hardware_legacy/power.h"
46//#include "VolumeManager.h"
47
48#define DM_CRYPT_BUF_SIZE 4096
49#define DATA_MNT_POINT "/data"
50
51#define HASH_COUNT 2000
a39552696ff55ce2013-01-08 16:14:56 +000052#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
53#define KEY_LEN_BYTES_SAMSUNG (sizeof(edk_t))
54#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040055#define KEY_LEN_BYTES 16
56#define IV_LEN_BYTES 16
57
58#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
59#define KEY_IN_FOOTER "footer"
60
61#define EXT4_FS 1
62#define FAT_FS 2
63
a39552696ff55ce2013-01-08 16:14:56 +000064#ifndef EXPAND
65#define STRINGIFY(x) #x
66#define EXPAND(x) STRINGIFY(x)
67#endif
68
Dees_Troy51a0e822012-09-05 15:24:24 -040069char *me = "cryptfs";
70
Dees_Troy51a0e822012-09-05 15:24:24 -040071static char *saved_data_blkdev;
72static char *saved_mount_point;
73static int master_key_saved = 0;
a39552696ff55ce2013-01-08 16:14:56 +000074#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
75static int using_samsung_encryption = 0;
76//static edk_t saved_master_key;
77static unsigned char saved_master_key[KEY_LEN_BYTES_SAMSUNG];
78#else
79static unsigned char saved_master_key[KEY_LEN_BYTES];
80#endif
81
82int cryptfs_setup_volume(const char *label, const char *real_blkdev, char *crypto_blkdev);
83
Dees_Troy51a0e822012-09-05 15:24:24 -040084
85static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
86{
87 memset(io, 0, dataSize);
88 io->data_size = dataSize;
89 io->data_start = sizeof(struct dm_ioctl);
90 io->version[0] = 4;
91 io->version[1] = 0;
92 io->version[2] = 0;
93 io->flags = flags;
94 if (name) {
95 strncpy(io->name, name, sizeof(io->name));
96 }
97}
98
Dees_Troy51a0e822012-09-05 15:24:24 -040099static unsigned int get_blkdev_size(int fd)
100{
101 unsigned int nr_sec;
102
103 if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
104 nr_sec = 0;
105 }
106
107 return nr_sec;
108}
109
110/* key or salt can be NULL, in which case just skip writing that value. Useful to
111 * update the failed mount count but not change the key.
112 */
113static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
114 unsigned char *key, unsigned char *salt)
115{
a39552696ff55ce2013-01-08 16:14:56 +0000116 // we don't need to update it...
117 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400118}
119
120static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
121 unsigned char *key, unsigned char *salt)
122{
123 int fd;
124 unsigned int nr_sec, cnt;
125 off64_t off;
126 int rc = -1;
127 char key_loc[PROPERTY_VALUE_MAX];
128 char *fname;
129 struct stat statbuf;
130
131 property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
132
133 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
134 fname = real_blk_name;
135 if ( (fd = open(fname, O_RDONLY)) < 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400136 printf("Cannot open real block device %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400137 return -1;
138 }
139
140 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
141 SLOGE("Cannot get size of block device %s\n", fname);
142 goto errout;
143 }
144
145 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
146 * encryption info footer and key, and plenty of bytes to spare for future
147 * growth.
148 */
149 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
150
151 if (lseek64(fd, off, SEEK_SET) == -1) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400152 printf("Cannot seek to real block device footer\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400153 goto errout;
154 }
155 } else if (key_loc[0] == '/') {
156 fname = key_loc;
157 if ( (fd = open(fname, O_RDONLY)) < 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400158 printf("Cannot open footer file %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400159 return -1;
160 }
161
162 /* Make sure it's 16 Kbytes in length */
163 fstat(fd, &statbuf);
a39552696ff55ce2013-01-08 16:14:56 +0000164 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000
165#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
166 && statbuf.st_size != 0x8000
167#endif
168 )) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400169 printf("footer file %s is not the expected size!\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400170 goto errout;
171 }
172 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400173 printf("Unexpected value for" KEY_LOC_PROP "\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400174 return -1;;
175 }
176
177 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400178 printf("Cannot read real block device footer\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400179 goto errout;
180 }
181
182 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
a39552696ff55ce2013-01-08 16:14:56 +0000183#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
184 if (crypt_ftr->magic != CRYPT_MNT_MAGIC_SAMSUNG) {
185 printf("Bad magic for real block device %s\n", fname);
186 goto errout;
187 } else {
188 printf("Using Samsung encryption.\n");
189 using_samsung_encryption = 1;
190 memcpy(key, &crypt_ftr->edk_payload, sizeof(edk_payload_t));
191
192 }
193#else
Dees_Troyab10ee22012-09-21 14:27:30 -0400194 printf("Bad magic for real block device %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400195 goto errout;
a39552696ff55ce2013-01-08 16:14:56 +0000196#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400197 }
198
199 if (crypt_ftr->major_version != 1) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400200 printf("Cannot understand major version %d real block device footer\n",
Dees_Troy51a0e822012-09-05 15:24:24 -0400201 crypt_ftr->major_version);
202 goto errout;
203 }
204
205 if (crypt_ftr->minor_version != 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400206 printf("Warning: crypto footer minor version %d, expected 0, continuing...\n",
Dees_Troy51a0e822012-09-05 15:24:24 -0400207 crypt_ftr->minor_version);
208 }
209
210 if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) {
211 /* the footer size is bigger than we expected.
212 * Skip to it's stated end so we can read the key.
213 */
Dees_Troya13d74f2013-03-24 08:54:55 -0500214 if (lseek64(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400215 printf("Cannot seek to start of key\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400216 goto errout;
217 }
218 }
219
a39552696ff55ce2013-01-08 16:14:56 +0000220 if (crypt_ftr->keysize != sizeof(saved_master_key)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400221 printf("Keysize of %d bits not supported for real block device %s\n",
Dees_Troy51a0e822012-09-05 15:24:24 -0400222 crypt_ftr->keysize * 8, fname);
223 goto errout;
224 }
225
226 if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400227 printf("Cannot read key for real block device %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400228 goto errout;
229 }
230
231 if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400232 printf("Cannot seek to real block device salt\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400233 goto errout;
234 }
235
236 if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400237 printf("Cannot read salt for real block device %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400238 goto errout;
239 }
240
241 /* Success! */
242 rc = 0;
243
244errout:
245 close(fd);
246 return rc;
247}
248
249/* Convert a binary key of specified length into an ascii hex string equivalent,
250 * without the leading 0x and with null termination
251 */
252void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
253 char *master_key_ascii)
254{
255 unsigned int i, a;
256 unsigned char nibble;
257
258 for (i=0, a=0; i<keysize; i++, a+=2) {
259 /* For each byte, write out two ascii hex digits */
260 nibble = (master_key[i] >> 4) & 0xf;
261 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
262
263 nibble = master_key[i] & 0xf;
264 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
265 }
266
267 /* Add the null termination */
268 master_key_ascii[a] = '\0';
269
270}
271
272static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
a39552696ff55ce2013-01-08 16:14:56 +0000273 const char *real_blk_name, char *crypto_blk_name, const char *name)
Dees_Troy51a0e822012-09-05 15:24:24 -0400274{
275 char buffer[DM_CRYPT_BUF_SIZE];
276 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
277 char *crypt_params;
278 struct dm_ioctl *io;
279 struct dm_target_spec *tgt;
280 unsigned int minor;
281 int fd;
282 int retval = -1;
283
284 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400285 printf("Cannot open device-mapper\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400286 goto errout;
287 }
288
289 io = (struct dm_ioctl *) buffer;
290
291 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
292 if (ioctl(fd, DM_DEV_CREATE, io)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400293 printf("Cannot create dm-crypt device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400294 goto errout;
295 }
296
297 /* Get the device status, in particular, the name of it's device file */
298 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
299 if (ioctl(fd, DM_DEV_STATUS, io)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400300 printf("Cannot retrieve dm-crypt device status\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400301 goto errout;
302 }
303 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
304 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
305
306 /* Load the mapping table for this device */
307 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
308
309 ioctl_init(io, 4096, name, 0);
310 io->target_count = 1;
311 tgt->status = 0;
312 tgt->sector_start = 0;
313 tgt->length = crypt_ftr->fs_size;
314 strcpy(tgt->target_type, "crypt");
315
316 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
317 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
318 sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name,
319 master_key_ascii, real_blk_name);
a39552696ff55ce2013-01-08 16:14:56 +0000320 //printf("cryptsetup params: '%s'\n", crypt_params);
Dees_Troy51a0e822012-09-05 15:24:24 -0400321 crypt_params += strlen(crypt_params) + 1;
322 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
323 tgt->next = crypt_params - buffer;
324
325 if (ioctl(fd, DM_TABLE_LOAD, io)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400326 printf("Cannot load dm-crypt mapping table.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400327 goto errout;
328 }
329
330 /* Resume this device to activate it */
331 ioctl_init(io, 4096, name, 0);
332
333 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400334 printf("Cannot resume the dm-crypt device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400335 goto errout;
336 }
337
338 /* We made it here with no errors. Woot! */
339 retval = 0;
340
341errout:
342 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
343
344 return retval;
345}
346
a39552696ff55ce2013-01-08 16:14:56 +0000347static int delete_crypto_blk_dev(const char *name)
Dees_Troy51a0e822012-09-05 15:24:24 -0400348{
349 int fd;
350 char buffer[DM_CRYPT_BUF_SIZE];
351 struct dm_ioctl *io;
352 int retval = -1;
353
354 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400355 printf("Cannot open device-mapper\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400356 goto errout;
357 }
358
359 io = (struct dm_ioctl *) buffer;
360
361 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
362 if (ioctl(fd, DM_DEV_REMOVE, io)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400363 printf("Cannot remove dm-crypt device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400364 goto errout;
365 }
366
367 /* We made it here with no errors. Woot! */
368 retval = 0;
369
370errout:
371 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
372
373 return retval;
374
375}
376
377static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey)
378{
379 /* Turn the password into a key and IV that can decrypt the master key */
380 PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
381 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
382}
383
Dees_Troy51a0e822012-09-05 15:24:24 -0400384static int decrypt_master_key(char *passwd, unsigned char *salt,
385 unsigned char *encrypted_master_key,
386 unsigned char *decrypted_master_key)
387{
a39552696ff55ce2013-01-08 16:14:56 +0000388#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
389 if (using_samsung_encryption) {
390 property_set("rw.km_fips_status", "ready");
391 return decrypt_EDK((dek_t*)decrypted_master_key, (edk_payload_t*)encrypted_master_key, passwd);
392 }
393#endif
394
Dees_Troy51a0e822012-09-05 15:24:24 -0400395 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
396 EVP_CIPHER_CTX d_ctx;
397 int decrypted_len, final_len;
398
399 /* Turn the password into a key and IV that can decrypt the master key */
400 pbkdf2(passwd, salt, ikey);
401
402 /* Initialize the decryption engine */
403 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
404 return -1;
405 }
406 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
407 /* Decrypt the master key */
408 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
409 encrypted_master_key, KEY_LEN_BYTES)) {
410 return -1;
411 }
412 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
413 return -1;
414 }
415
416 if (decrypted_len + final_len != KEY_LEN_BYTES) {
417 return -1;
418 } else {
419 return 0;
420 }
421}
422
a39552696ff55ce2013-01-08 16:14:56 +0000423static int get_orig_mount_parms(
424 const char *mount_point, char *fs_type, char *real_blkdev,
425 unsigned long *mnt_flags, char *fs_options)
Dees_Troy51a0e822012-09-05 15:24:24 -0400426{
427 char mount_point2[PROPERTY_VALUE_MAX];
428 char fs_flags[PROPERTY_VALUE_MAX];
429
430 property_get("ro.crypto.fs_type", fs_type, "");
431 property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
432 property_get("ro.crypto.fs_mnt_point", mount_point2, "");
433 property_get("ro.crypto.fs_options", fs_options, "");
434 property_get("ro.crypto.fs_flags", fs_flags, "");
435 *mnt_flags = strtol(fs_flags, 0, 0);
436
437 if (strcmp(mount_point, mount_point2)) {
438 /* Consistency check. These should match. If not, something odd happened. */
439 return -1;
440 }
441
442 return 0;
443}
444
a39552696ff55ce2013-01-08 16:14:56 +0000445static int get_orig_mount_parms_sd(
446 const char *mount_point, char *fs_type, char *real_blkdev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400447{
a39552696ff55ce2013-01-08 16:14:56 +0000448 char mount_point2[PROPERTY_VALUE_MAX];
Dees_Troy51a0e822012-09-05 15:24:24 -0400449
a39552696ff55ce2013-01-08 16:14:56 +0000450 property_get("ro.crypto.sd_fs_type", fs_type, "");
451 property_get("ro.crypto.sd_fs_real_blkdev", real_blkdev, "");
452 property_get("ro.crypto.sd_fs_mnt_point", mount_point2, "");
Dees_Troy51a0e822012-09-05 15:24:24 -0400453
a39552696ff55ce2013-01-08 16:14:56 +0000454 if (strcmp(mount_point, mount_point2)) {
455 /* Consistency check. These should match. If not, something odd happened. */
Dees_Troy51a0e822012-09-05 15:24:24 -0400456 return -1;
457 }
458
a39552696ff55ce2013-01-08 16:14:56 +0000459 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400460}
461
a39552696ff55ce2013-01-08 16:14:56 +0000462static int test_mount_encrypted_fs(
463 char *passwd, char *mount_point, char *label, char *crypto_blkdev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400464{
465 struct crypt_mnt_ftr crypt_ftr;
466 /* Allocate enough space for a 256 bit key, but we may use less */
a39552696ff55ce2013-01-08 16:14:56 +0000467 unsigned char encrypted_master_key[256], decrypted_master_key[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400468 unsigned char salt[SALT_LEN];
Dees_Troy51a0e822012-09-05 15:24:24 -0400469 char real_blkdev[MAXPATHLEN];
470 char fs_type[PROPERTY_VALUE_MAX];
471 char fs_options[PROPERTY_VALUE_MAX];
a39552696ff55ce2013-01-08 16:14:56 +0000472 char tmp_mount_point[MAXPATHLEN];
Dees_Troy51a0e822012-09-05 15:24:24 -0400473 unsigned long mnt_flags;
474 unsigned int orig_failed_decrypt_count;
475 char encrypted_state[PROPERTY_VALUE_MAX];
476 int rc;
477
478 property_get("ro.crypto.state", encrypted_state, "");
479 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
a39552696ff55ce2013-01-08 16:14:56 +0000480 printf("encrypted fs already validated or not running with encryption, aborting %s\n", encrypted_state);
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 return -1;
482 }
483
484 if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400485 printf("Error reading original mount parms for mount point %s\n", mount_point);
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 return -1;
487 }
488
489 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400490 printf("Error getting crypt footer and key\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400491 return -1;
492 }
493
a39552696ff55ce2013-01-08 16:14:56 +0000494 //printf("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size);
Dees_Troy51a0e822012-09-05 15:24:24 -0400495 orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count;
496
497 if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
498 decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
499 }
500
a39552696ff55ce2013-01-08 16:14:56 +0000501 if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev,
502 crypto_blkdev, label)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400503 printf("Error creating decrypted block device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 return -1;
505 }
506
507 /* If init detects an encrypted filesystme, it writes a file for each such
508 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
509 * files and passes that data to me */
510 /* Create a tmp mount point to try mounting the decryptd fs
511 * Since we're here, the mount_point should be a tmpfs filesystem, so make
512 * a directory in it to test mount the decrypted filesystem.
513 */
514 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
515 mkdir(tmp_mount_point, 0755);
a39552696ff55ce2013-01-08 16:14:56 +0000516 if ( mount(crypto_blkdev, tmp_mount_point, fs_type, MS_RDONLY, "") ) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400517 printf("Error temp mounting decrypted block device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 delete_crypto_blk_dev(label);
519 crypt_ftr.failed_decrypt_count++;
520 } else {
521 /* Success, so just umount and we'll mount it properly when we restart
522 * the framework.
523 */
524 umount(tmp_mount_point);
525 crypt_ftr.failed_decrypt_count = 0;
526 }
527
a39552696ff55ce2013-01-08 16:14:56 +0000528 rmdir(tmp_mount_point);
Dees_Troy51a0e822012-09-05 15:24:24 -0400529
530 if (crypt_ftr.failed_decrypt_count) {
531 /* We failed to mount the device, so return an error */
532 rc = crypt_ftr.failed_decrypt_count;
533
534 } else {
535 /* Woot! Success! Save the name of the crypto block device
536 * so we can mount it when restarting the framework.
537 */
538 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
539
540 /* Also save a the master key so we can reencrypted the key
541 * the key when we want to change the password on it.
542 */
a39552696ff55ce2013-01-08 16:14:56 +0000543 memcpy(saved_master_key, decrypted_master_key, sizeof(saved_master_key));
Dees_Troy51a0e822012-09-05 15:24:24 -0400544 saved_data_blkdev = strdup(real_blkdev);
545 saved_mount_point = strdup(mount_point);
546 master_key_saved = 1;
547 rc = 0;
548 }
549
550 return rc;
551}
552
a39552696ff55ce2013-01-08 16:14:56 +0000553static int test_mount_encrypted_fs_sd(
554 const char *passwd, const char *mount_point, const char *label)
Dees_Troy51a0e822012-09-05 15:24:24 -0400555{
a39552696ff55ce2013-01-08 16:14:56 +0000556 char real_blkdev[MAXPATHLEN];
557 char crypto_blkdev[MAXPATHLEN];
558 char tmp_mount_point[MAXPATHLEN];
559 char encrypted_state[PROPERTY_VALUE_MAX];
560 char fs_type[PROPERTY_VALUE_MAX];
561 int rc;
562
563 property_get("ro.crypto.state", encrypted_state, "");
564 if ( !master_key_saved || strcmp(encrypted_state, "encrypted") ) {
565 printf("encrypted fs not yet validated or not running with encryption, aborting\n");
566 return -1;
567 }
568
569 if (get_orig_mount_parms_sd(mount_point, fs_type, real_blkdev)) {
570 printf("Error reading original mount parms for mount point %s\n", mount_point);
571 return -1;
572 }
573
574 rc = cryptfs_setup_volume(label, real_blkdev, crypto_blkdev);
575 if(rc){
576 printf("Error setting up cryptfs volume %s\n", real_blkdev);
577 return -1;
578 }
579
580 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
581 mkdir(tmp_mount_point, 0755);
582 if ( mount(crypto_blkdev, tmp_mount_point, fs_type, MS_RDONLY, "") ) {
583 printf("Error temp mounting decrypted block device\n");
584 delete_crypto_blk_dev(label);
585 } else {
586 /* Success, so just umount and we'll mount it properly when we restart
587 * the framework.
588 */
589 umount(tmp_mount_point);
590
591 property_set("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev);
592 }
593
594 rmdir(tmp_mount_point);
595
596 return rc;
Dees_Troy51a0e822012-09-05 15:24:24 -0400597}
598
599/*
600 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
601 * Setup a dm-crypt mapping, use the saved master key from
602 * setting up the /data mapping, and return the new device path.
603 */
a39552696ff55ce2013-01-08 16:14:56 +0000604int cryptfs_setup_volume(const char *label, const char *real_blkdev, char *crypto_blkdev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400605{
Dees_Troy51a0e822012-09-05 15:24:24 -0400606 struct crypt_mnt_ftr sd_crypt_ftr;
a39552696ff55ce2013-01-08 16:14:56 +0000607 unsigned char key[256], salt[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400608 struct stat statbuf;
a39552696ff55ce2013-01-08 16:14:56 +0000609 int nr_sec, fd, rc;
Dees_Troy51a0e822012-09-05 15:24:24 -0400610
611 /* Just want the footer, but gotta get it all */
612 get_crypt_ftr_and_key(saved_data_blkdev, &sd_crypt_ftr, key, salt);
613
614 /* Update the fs_size field to be the size of the volume */
615 fd = open(real_blkdev, O_RDONLY);
616 nr_sec = get_blkdev_size(fd);
617 close(fd);
618 if (nr_sec == 0) {
619 SLOGE("Cannot get size of volume %s\n", real_blkdev);
620 return -1;
621 }
622
a39552696ff55ce2013-01-08 16:14:56 +0000623#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
624 if(using_samsung_encryption) {
625 if(!access("/efs/essiv", R_OK)){
626 strcpy(sd_crypt_ftr.crypto_type_name, "aes-cbc-plain:sha1");
627 }
628 else if(!access("/efs/cryptprop_essiv", R_OK)){
629 strcpy(sd_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
630 }
631 }
632#endif
633
Dees_Troy51a0e822012-09-05 15:24:24 -0400634 sd_crypt_ftr.fs_size = nr_sec;
a39552696ff55ce2013-01-08 16:14:56 +0000635 rc = create_crypto_blk_dev(
636 &sd_crypt_ftr, saved_master_key, real_blkdev, crypto_blkdev, label);
Dees_Troy51a0e822012-09-05 15:24:24 -0400637
638 stat(crypto_blkdev, &statbuf);
Dees_Troy51a0e822012-09-05 15:24:24 -0400639
a39552696ff55ce2013-01-08 16:14:56 +0000640 return rc;
Dees_Troy51a0e822012-09-05 15:24:24 -0400641}
642
643int cryptfs_crypto_complete(void)
644{
a39552696ff55ce2013-01-08 16:14:56 +0000645 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400646}
647
a39552696ff55ce2013-01-08 16:14:56 +0000648int cryptfs_check_passwd(const char *passwd)
Dees_Troy51a0e822012-09-05 15:24:24 -0400649{
a39552696ff55ce2013-01-08 16:14:56 +0000650 char pwbuf[256];
651 char crypto_blkdev_data[MAXPATHLEN];
Dees_Troy51a0e822012-09-05 15:24:24 -0400652 int rc = -1;
653
a39552696ff55ce2013-01-08 16:14:56 +0000654 strcpy(pwbuf, passwd);
655 rc = test_mount_encrypted_fs(pwbuf, DATA_MNT_POINT, "userdata", crypto_blkdev_data);
Dees_Troy51a0e822012-09-05 15:24:24 -0400656
a39552696ff55ce2013-01-08 16:14:56 +0000657#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
658 if(using_samsung_encryption) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400659
a39552696ff55ce2013-01-08 16:14:56 +0000660 int rc2 = 1;
661#ifndef RECOVERY_SDCARD_ON_DATA
Dees_Troy85f44ed2013-01-09 18:42:36 +0000662#ifdef TW_INTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +0000663 // internal storage for non data/media devices
664 if(!rc) {
665 strcpy(pwbuf, passwd);
666 rc2 = test_mount_encrypted_fs_sd(
667 pwbuf, EXPAND(TW_INTERNAL_STORAGE_PATH),
668 EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT));
669 }
670#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +0000671#endif
a39552696ff55ce2013-01-08 16:14:56 +0000672#ifdef TW_EXTERNAL_STORAGE_PATH
673 printf("Temp mounting /data\n");
674 // mount data so mount_ecryptfs_drive can access edk in /data/system/
675 rc2 = mount(crypto_blkdev_data, DATA_MNT_POINT, CRYPTO_FS_TYPE, MS_RDONLY, "");
676 // external sd
677 char decrypt_external[256], external_blkdev[256];
678 property_get("ro.crypto.external_encrypted", decrypt_external, "0");
a39552696ff55ce2013-01-08 16:14:56 +0000679 // Mount the external storage as ecryptfs so that ecryptfs can act as a pass-through
Dees_Troy85f44ed2013-01-09 18:42:36 +0000680 if (!rc2 && strcmp(decrypt_external, "1") == 0) {
a39552696ff55ce2013-01-08 16:14:56 +0000681 printf("Mounting external with ecryptfs...\n");
682 strcpy(pwbuf, passwd);
683 rc2 = mount_ecryptfs_drive(
684 pwbuf, EXPAND(TW_EXTERNAL_STORAGE_PATH),
685 EXPAND(TW_EXTERNAL_STORAGE_PATH), 0);
Dees_Troy85f44ed2013-01-09 18:42:36 +0000686 if (rc2 == 0)
687 property_set("ro.crypto.external_use_ecryptfs", "1");
688 else
689 property_set("ro.crypto.external_use_ecryptfs", "0");
Dees_Troy51a0e822012-09-05 15:24:24 -0400690 } else {
a39552696ff55ce2013-01-08 16:14:56 +0000691 printf("Unable to mount external storage with ecryptfs.\n");
692 umount(EXPAND(TW_EXTERNAL_STORAGE_PATH));
693 }
694 umount(DATA_MNT_POINT);
Dees_Troy51a0e822012-09-05 15:24:24 -0400695 }
a39552696ff55ce2013-01-08 16:14:56 +0000696#endif // #ifdef TW_EXTERNAL_STORAGE_PATH
697#endif // #ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy51a0e822012-09-05 15:24:24 -0400698 return rc;
699}