blob: 193339ec96c2fb64aefe97b9df6601adba454e24 [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];
Dees_Troy066eb302013-08-23 17:20:32 +000078edk_payload_t edk_payload;
a39552696ff55ce2013-01-08 16:14:56 +000079#else
80static unsigned char saved_master_key[KEY_LEN_BYTES];
81#endif
82
83int cryptfs_setup_volume(const char *label, const char *real_blkdev, char *crypto_blkdev);
84
Dees_Troy51a0e822012-09-05 15:24:24 -040085
86static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
87{
88 memset(io, 0, dataSize);
89 io->data_size = dataSize;
90 io->data_start = sizeof(struct dm_ioctl);
91 io->version[0] = 4;
92 io->version[1] = 0;
93 io->version[2] = 0;
94 io->flags = flags;
95 if (name) {
96 strncpy(io->name, name, sizeof(io->name));
97 }
98}
99
Dees_Troy51a0e822012-09-05 15:24:24 -0400100static unsigned int get_blkdev_size(int fd)
101{
102 unsigned int nr_sec;
103
104 if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
105 nr_sec = 0;
106 }
107
108 return nr_sec;
109}
110
111/* key or salt can be NULL, in which case just skip writing that value. Useful to
112 * update the failed mount count but not change the key.
113 */
114static int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
115 unsigned char *key, unsigned char *salt)
116{
a39552696ff55ce2013-01-08 16:14:56 +0000117 // we don't need to update it...
118 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119}
120
121static int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
122 unsigned char *key, unsigned char *salt)
123{
124 int fd;
125 unsigned int nr_sec, cnt;
126 off64_t off;
127 int rc = -1;
128 char key_loc[PROPERTY_VALUE_MAX];
129 char *fname;
130 struct stat statbuf;
131
132 property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
133
134 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
135 fname = real_blk_name;
136 if ( (fd = open(fname, O_RDONLY)) < 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400137 printf("Cannot open real block device %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400138 return -1;
139 }
140
141 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
142 SLOGE("Cannot get size of block device %s\n", fname);
143 goto errout;
144 }
145
146 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
147 * encryption info footer and key, and plenty of bytes to spare for future
148 * growth.
149 */
150 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
151
152 if (lseek64(fd, off, SEEK_SET) == -1) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400153 printf("Cannot seek to real block device footer\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400154 goto errout;
155 }
156 } else if (key_loc[0] == '/') {
157 fname = key_loc;
158 if ( (fd = open(fname, O_RDONLY)) < 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400159 printf("Cannot open footer file %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400160 return -1;
161 }
162
163 /* Make sure it's 16 Kbytes in length */
164 fstat(fd, &statbuf);
a39552696ff55ce2013-01-08 16:14:56 +0000165 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000
166#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
167 && statbuf.st_size != 0x8000
168#endif
169 )) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400170 printf("footer file %s is not the expected size!\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400171 goto errout;
172 }
173 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400174 printf("Unexpected value for" KEY_LOC_PROP "\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400175 return -1;;
176 }
177
178 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400179 printf("Cannot read real block device footer\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400180 goto errout;
181 }
182
183 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
a39552696ff55ce2013-01-08 16:14:56 +0000184#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
185 if (crypt_ftr->magic != CRYPT_MNT_MAGIC_SAMSUNG) {
186 printf("Bad magic for real block device %s\n", fname);
187 goto errout;
188 } else {
189 printf("Using Samsung encryption.\n");
190 using_samsung_encryption = 1;
Dees_Troy066eb302013-08-23 17:20:32 +0000191 if ( (cnt = read(fd, &edk_payload, sizeof(edk_payload_t))) != sizeof(edk_payload_t)) {
192 printf("Cannot read EDK payload from real block device footer\n");
193 goto errout;
194 }
195 if (lseek64(fd, sizeof(__le32), SEEK_CUR) == -1) {
196 printf("Cannot seek past unknown data from real block device footer\n");
197 goto errout;
198 }
199 memcpy(key, &edk_payload, sizeof(edk_payload_t));
a39552696ff55ce2013-01-08 16:14:56 +0000200 }
201#else
Dees_Troyab10ee22012-09-21 14:27:30 -0400202 printf("Bad magic for real block device %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400203 goto errout;
a39552696ff55ce2013-01-08 16:14:56 +0000204#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400205 }
206
207 if (crypt_ftr->major_version != 1) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400208 printf("Cannot understand major version %d real block device footer\n",
Dees_Troy51a0e822012-09-05 15:24:24 -0400209 crypt_ftr->major_version);
210 goto errout;
211 }
212
213 if (crypt_ftr->minor_version != 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400214 printf("Warning: crypto footer minor version %d, expected 0, continuing...\n",
Dees_Troy51a0e822012-09-05 15:24:24 -0400215 crypt_ftr->minor_version);
216 }
217
218 if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) {
219 /* the footer size is bigger than we expected.
220 * Skip to it's stated end so we can read the key.
221 */
Dees_Troya13d74f2013-03-24 08:54:55 -0500222 if (lseek64(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr), SEEK_CUR) == -1) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400223 printf("Cannot seek to start of key\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400224 goto errout;
225 }
226 }
227
Dees_Troy066eb302013-08-23 17:20:32 +0000228 if (crypt_ftr->keysize > sizeof(saved_master_key)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400229 printf("Keysize of %d bits not supported for real block device %s\n",
Dees_Troy51a0e822012-09-05 15:24:24 -0400230 crypt_ftr->keysize * 8, fname);
231 goto errout;
232 }
233
234 if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400235 printf("Cannot read key for real block device %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400236 goto errout;
237 }
238
239 if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400240 printf("Cannot seek to real block device salt\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400241 goto errout;
242 }
243
244 if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400245 printf("Cannot read salt for real block device %s\n", fname);
Dees_Troy51a0e822012-09-05 15:24:24 -0400246 goto errout;
247 }
248
249 /* Success! */
250 rc = 0;
251
252errout:
253 close(fd);
254 return rc;
255}
256
257/* Convert a binary key of specified length into an ascii hex string equivalent,
258 * without the leading 0x and with null termination
259 */
260void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
261 char *master_key_ascii)
262{
263 unsigned int i, a;
264 unsigned char nibble;
265
266 for (i=0, a=0; i<keysize; i++, a+=2) {
267 /* For each byte, write out two ascii hex digits */
268 nibble = (master_key[i] >> 4) & 0xf;
269 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
270
271 nibble = master_key[i] & 0xf;
272 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
273 }
274
275 /* Add the null termination */
276 master_key_ascii[a] = '\0';
277
278}
279
280static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
a39552696ff55ce2013-01-08 16:14:56 +0000281 const char *real_blk_name, char *crypto_blk_name, const char *name)
Dees_Troy51a0e822012-09-05 15:24:24 -0400282{
283 char buffer[DM_CRYPT_BUF_SIZE];
284 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
285 char *crypt_params;
286 struct dm_ioctl *io;
287 struct dm_target_spec *tgt;
288 unsigned int minor;
289 int fd;
290 int retval = -1;
291
292 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400293 printf("Cannot open device-mapper\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400294 goto errout;
295 }
296
297 io = (struct dm_ioctl *) buffer;
298
299 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
300 if (ioctl(fd, DM_DEV_CREATE, io)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400301 printf("Cannot create dm-crypt device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400302 goto errout;
303 }
304
305 /* Get the device status, in particular, the name of it's device file */
306 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
307 if (ioctl(fd, DM_DEV_STATUS, io)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400308 printf("Cannot retrieve dm-crypt device status\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400309 goto errout;
310 }
311 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
312 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
313
314 /* Load the mapping table for this device */
315 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
316
317 ioctl_init(io, 4096, name, 0);
318 io->target_count = 1;
319 tgt->status = 0;
320 tgt->sector_start = 0;
321 tgt->length = crypt_ftr->fs_size;
322 strcpy(tgt->target_type, "crypt");
323
324 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
325 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
326 sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name,
327 master_key_ascii, real_blk_name);
a39552696ff55ce2013-01-08 16:14:56 +0000328 //printf("cryptsetup params: '%s'\n", crypt_params);
Dees_Troy51a0e822012-09-05 15:24:24 -0400329 crypt_params += strlen(crypt_params) + 1;
330 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
331 tgt->next = crypt_params - buffer;
332
333 if (ioctl(fd, DM_TABLE_LOAD, io)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400334 printf("Cannot load dm-crypt mapping table.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400335 goto errout;
336 }
337
338 /* Resume this device to activate it */
339 ioctl_init(io, 4096, name, 0);
340
341 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400342 printf("Cannot resume the dm-crypt device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400343 goto errout;
344 }
345
346 /* We made it here with no errors. Woot! */
347 retval = 0;
348
349errout:
350 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
351
352 return retval;
353}
354
a39552696ff55ce2013-01-08 16:14:56 +0000355static int delete_crypto_blk_dev(const char *name)
Dees_Troy51a0e822012-09-05 15:24:24 -0400356{
357 int fd;
358 char buffer[DM_CRYPT_BUF_SIZE];
359 struct dm_ioctl *io;
360 int retval = -1;
361
362 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400363 printf("Cannot open device-mapper\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400364 goto errout;
365 }
366
367 io = (struct dm_ioctl *) buffer;
368
369 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
370 if (ioctl(fd, DM_DEV_REMOVE, io)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400371 printf("Cannot remove dm-crypt device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400372 goto errout;
373 }
374
375 /* We made it here with no errors. Woot! */
376 retval = 0;
377
378errout:
379 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
380
381 return retval;
382
383}
384
385static void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey)
386{
387 /* Turn the password into a key and IV that can decrypt the master key */
388 PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
389 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
390}
391
Dees_Troy51a0e822012-09-05 15:24:24 -0400392static int decrypt_master_key(char *passwd, unsigned char *salt,
393 unsigned char *encrypted_master_key,
394 unsigned char *decrypted_master_key)
395{
a39552696ff55ce2013-01-08 16:14:56 +0000396#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
397 if (using_samsung_encryption) {
398 property_set("rw.km_fips_status", "ready");
399 return decrypt_EDK((dek_t*)decrypted_master_key, (edk_payload_t*)encrypted_master_key, passwd);
400 }
401#endif
402
Dees_Troy51a0e822012-09-05 15:24:24 -0400403 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
404 EVP_CIPHER_CTX d_ctx;
405 int decrypted_len, final_len;
406
407 /* Turn the password into a key and IV that can decrypt the master key */
408 pbkdf2(passwd, salt, ikey);
409
410 /* Initialize the decryption engine */
411 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
412 return -1;
413 }
414 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
415 /* Decrypt the master key */
416 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
417 encrypted_master_key, KEY_LEN_BYTES)) {
418 return -1;
419 }
420 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
421 return -1;
422 }
423
424 if (decrypted_len + final_len != KEY_LEN_BYTES) {
425 return -1;
426 } else {
427 return 0;
428 }
429}
430
a39552696ff55ce2013-01-08 16:14:56 +0000431static int get_orig_mount_parms(
432 const char *mount_point, char *fs_type, char *real_blkdev,
433 unsigned long *mnt_flags, char *fs_options)
Dees_Troy51a0e822012-09-05 15:24:24 -0400434{
435 char mount_point2[PROPERTY_VALUE_MAX];
436 char fs_flags[PROPERTY_VALUE_MAX];
437
438 property_get("ro.crypto.fs_type", fs_type, "");
439 property_get("ro.crypto.fs_real_blkdev", real_blkdev, "");
440 property_get("ro.crypto.fs_mnt_point", mount_point2, "");
441 property_get("ro.crypto.fs_options", fs_options, "");
442 property_get("ro.crypto.fs_flags", fs_flags, "");
443 *mnt_flags = strtol(fs_flags, 0, 0);
444
445 if (strcmp(mount_point, mount_point2)) {
446 /* Consistency check. These should match. If not, something odd happened. */
447 return -1;
448 }
449
450 return 0;
451}
452
a39552696ff55ce2013-01-08 16:14:56 +0000453static int get_orig_mount_parms_sd(
454 const char *mount_point, char *fs_type, char *real_blkdev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400455{
a39552696ff55ce2013-01-08 16:14:56 +0000456 char mount_point2[PROPERTY_VALUE_MAX];
Dees_Troy51a0e822012-09-05 15:24:24 -0400457
a39552696ff55ce2013-01-08 16:14:56 +0000458 property_get("ro.crypto.sd_fs_type", fs_type, "");
459 property_get("ro.crypto.sd_fs_real_blkdev", real_blkdev, "");
460 property_get("ro.crypto.sd_fs_mnt_point", mount_point2, "");
Dees_Troy51a0e822012-09-05 15:24:24 -0400461
a39552696ff55ce2013-01-08 16:14:56 +0000462 if (strcmp(mount_point, mount_point2)) {
463 /* Consistency check. These should match. If not, something odd happened. */
Dees_Troy51a0e822012-09-05 15:24:24 -0400464 return -1;
465 }
466
a39552696ff55ce2013-01-08 16:14:56 +0000467 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400468}
469
a39552696ff55ce2013-01-08 16:14:56 +0000470static int test_mount_encrypted_fs(
471 char *passwd, char *mount_point, char *label, char *crypto_blkdev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400472{
473 struct crypt_mnt_ftr crypt_ftr;
474 /* Allocate enough space for a 256 bit key, but we may use less */
a39552696ff55ce2013-01-08 16:14:56 +0000475 unsigned char encrypted_master_key[256], decrypted_master_key[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 unsigned char salt[SALT_LEN];
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 char real_blkdev[MAXPATHLEN];
478 char fs_type[PROPERTY_VALUE_MAX];
479 char fs_options[PROPERTY_VALUE_MAX];
a39552696ff55ce2013-01-08 16:14:56 +0000480 char tmp_mount_point[MAXPATHLEN];
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 unsigned long mnt_flags;
482 unsigned int orig_failed_decrypt_count;
483 char encrypted_state[PROPERTY_VALUE_MAX];
484 int rc;
485
486 property_get("ro.crypto.state", encrypted_state, "");
487 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
a39552696ff55ce2013-01-08 16:14:56 +0000488 printf("encrypted fs already validated or not running with encryption, aborting %s\n", encrypted_state);
Dees_Troy51a0e822012-09-05 15:24:24 -0400489 return -1;
490 }
491
492 if (get_orig_mount_parms(mount_point, fs_type, real_blkdev, &mnt_flags, fs_options)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400493 printf("Error reading original mount parms for mount point %s\n", mount_point);
Dees_Troy51a0e822012-09-05 15:24:24 -0400494 return -1;
495 }
496
497 if (get_crypt_ftr_and_key(real_blkdev, &crypt_ftr, encrypted_master_key, salt)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400498 printf("Error getting crypt footer and key\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 return -1;
500 }
501
a39552696ff55ce2013-01-08 16:14:56 +0000502 //printf("crypt_ftr->fs_size = %lld\n", crypt_ftr.fs_size);
Dees_Troy51a0e822012-09-05 15:24:24 -0400503 orig_failed_decrypt_count = crypt_ftr.failed_decrypt_count;
504
505 if (! (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
506 decrypt_master_key(passwd, salt, encrypted_master_key, decrypted_master_key);
507 }
508
a39552696ff55ce2013-01-08 16:14:56 +0000509 if (create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev,
510 crypto_blkdev, label)) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400511 printf("Error creating decrypted block device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 return -1;
513 }
514
515 /* If init detects an encrypted filesystme, it writes a file for each such
516 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
517 * files and passes that data to me */
518 /* Create a tmp mount point to try mounting the decryptd fs
519 * Since we're here, the mount_point should be a tmpfs filesystem, so make
520 * a directory in it to test mount the decrypted filesystem.
521 */
522 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
523 mkdir(tmp_mount_point, 0755);
a39552696ff55ce2013-01-08 16:14:56 +0000524 if ( mount(crypto_blkdev, tmp_mount_point, fs_type, MS_RDONLY, "") ) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400525 printf("Error temp mounting decrypted block device\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400526 delete_crypto_blk_dev(label);
527 crypt_ftr.failed_decrypt_count++;
528 } else {
529 /* Success, so just umount and we'll mount it properly when we restart
530 * the framework.
531 */
532 umount(tmp_mount_point);
533 crypt_ftr.failed_decrypt_count = 0;
534 }
535
a39552696ff55ce2013-01-08 16:14:56 +0000536 rmdir(tmp_mount_point);
Dees_Troy51a0e822012-09-05 15:24:24 -0400537
538 if (crypt_ftr.failed_decrypt_count) {
539 /* We failed to mount the device, so return an error */
540 rc = crypt_ftr.failed_decrypt_count;
541
542 } else {
543 /* Woot! Success! Save the name of the crypto block device
544 * so we can mount it when restarting the framework.
545 */
546 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
547
548 /* Also save a the master key so we can reencrypted the key
549 * the key when we want to change the password on it.
550 */
a39552696ff55ce2013-01-08 16:14:56 +0000551 memcpy(saved_master_key, decrypted_master_key, sizeof(saved_master_key));
Dees_Troy51a0e822012-09-05 15:24:24 -0400552 saved_data_blkdev = strdup(real_blkdev);
553 saved_mount_point = strdup(mount_point);
554 master_key_saved = 1;
555 rc = 0;
556 }
557
558 return rc;
559}
560
a39552696ff55ce2013-01-08 16:14:56 +0000561static int test_mount_encrypted_fs_sd(
562 const char *passwd, const char *mount_point, const char *label)
Dees_Troy51a0e822012-09-05 15:24:24 -0400563{
a39552696ff55ce2013-01-08 16:14:56 +0000564 char real_blkdev[MAXPATHLEN];
565 char crypto_blkdev[MAXPATHLEN];
566 char tmp_mount_point[MAXPATHLEN];
567 char encrypted_state[PROPERTY_VALUE_MAX];
568 char fs_type[PROPERTY_VALUE_MAX];
569 int rc;
570
571 property_get("ro.crypto.state", encrypted_state, "");
572 if ( !master_key_saved || strcmp(encrypted_state, "encrypted") ) {
573 printf("encrypted fs not yet validated or not running with encryption, aborting\n");
574 return -1;
575 }
576
577 if (get_orig_mount_parms_sd(mount_point, fs_type, real_blkdev)) {
578 printf("Error reading original mount parms for mount point %s\n", mount_point);
579 return -1;
580 }
581
582 rc = cryptfs_setup_volume(label, real_blkdev, crypto_blkdev);
583 if(rc){
584 printf("Error setting up cryptfs volume %s\n", real_blkdev);
585 return -1;
586 }
587
588 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
589 mkdir(tmp_mount_point, 0755);
590 if ( mount(crypto_blkdev, tmp_mount_point, fs_type, MS_RDONLY, "") ) {
591 printf("Error temp mounting decrypted block device\n");
592 delete_crypto_blk_dev(label);
593 } else {
594 /* Success, so just umount and we'll mount it properly when we restart
595 * the framework.
596 */
597 umount(tmp_mount_point);
598
599 property_set("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev);
600 }
601
602 rmdir(tmp_mount_point);
603
604 return rc;
Dees_Troy51a0e822012-09-05 15:24:24 -0400605}
606
607/*
608 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
609 * Setup a dm-crypt mapping, use the saved master key from
610 * setting up the /data mapping, and return the new device path.
611 */
a39552696ff55ce2013-01-08 16:14:56 +0000612int cryptfs_setup_volume(const char *label, const char *real_blkdev, char *crypto_blkdev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400613{
Dees_Troy51a0e822012-09-05 15:24:24 -0400614 struct crypt_mnt_ftr sd_crypt_ftr;
a39552696ff55ce2013-01-08 16:14:56 +0000615 unsigned char key[256], salt[32];
Dees_Troy51a0e822012-09-05 15:24:24 -0400616 struct stat statbuf;
a39552696ff55ce2013-01-08 16:14:56 +0000617 int nr_sec, fd, rc;
Dees_Troy51a0e822012-09-05 15:24:24 -0400618
619 /* Just want the footer, but gotta get it all */
620 get_crypt_ftr_and_key(saved_data_blkdev, &sd_crypt_ftr, key, salt);
621
622 /* Update the fs_size field to be the size of the volume */
623 fd = open(real_blkdev, O_RDONLY);
624 nr_sec = get_blkdev_size(fd);
625 close(fd);
626 if (nr_sec == 0) {
627 SLOGE("Cannot get size of volume %s\n", real_blkdev);
628 return -1;
629 }
630
a39552696ff55ce2013-01-08 16:14:56 +0000631#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
632 if(using_samsung_encryption) {
633 if(!access("/efs/essiv", R_OK)){
634 strcpy(sd_crypt_ftr.crypto_type_name, "aes-cbc-plain:sha1");
635 }
636 else if(!access("/efs/cryptprop_essiv", R_OK)){
637 strcpy(sd_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
638 }
639 }
640#endif
641
Dees_Troy51a0e822012-09-05 15:24:24 -0400642 sd_crypt_ftr.fs_size = nr_sec;
a39552696ff55ce2013-01-08 16:14:56 +0000643 rc = create_crypto_blk_dev(
644 &sd_crypt_ftr, saved_master_key, real_blkdev, crypto_blkdev, label);
Dees_Troy51a0e822012-09-05 15:24:24 -0400645
646 stat(crypto_blkdev, &statbuf);
Dees_Troy51a0e822012-09-05 15:24:24 -0400647
a39552696ff55ce2013-01-08 16:14:56 +0000648 return rc;
Dees_Troy51a0e822012-09-05 15:24:24 -0400649}
650
651int cryptfs_crypto_complete(void)
652{
a39552696ff55ce2013-01-08 16:14:56 +0000653 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400654}
655
a39552696ff55ce2013-01-08 16:14:56 +0000656int cryptfs_check_passwd(const char *passwd)
Dees_Troy51a0e822012-09-05 15:24:24 -0400657{
a39552696ff55ce2013-01-08 16:14:56 +0000658 char pwbuf[256];
659 char crypto_blkdev_data[MAXPATHLEN];
Dees_Troy51a0e822012-09-05 15:24:24 -0400660 int rc = -1;
661
a39552696ff55ce2013-01-08 16:14:56 +0000662 strcpy(pwbuf, passwd);
663 rc = test_mount_encrypted_fs(pwbuf, DATA_MNT_POINT, "userdata", crypto_blkdev_data);
Dees_Troy51a0e822012-09-05 15:24:24 -0400664
a39552696ff55ce2013-01-08 16:14:56 +0000665#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
666 if(using_samsung_encryption) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400667
a39552696ff55ce2013-01-08 16:14:56 +0000668 int rc2 = 1;
669#ifndef RECOVERY_SDCARD_ON_DATA
Dees_Troy85f44ed2013-01-09 18:42:36 +0000670#ifdef TW_INTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +0000671 // internal storage for non data/media devices
672 if(!rc) {
673 strcpy(pwbuf, passwd);
674 rc2 = test_mount_encrypted_fs_sd(
675 pwbuf, EXPAND(TW_INTERNAL_STORAGE_PATH),
676 EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT));
677 }
678#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +0000679#endif
a39552696ff55ce2013-01-08 16:14:56 +0000680#ifdef TW_EXTERNAL_STORAGE_PATH
681 printf("Temp mounting /data\n");
682 // mount data so mount_ecryptfs_drive can access edk in /data/system/
683 rc2 = mount(crypto_blkdev_data, DATA_MNT_POINT, CRYPTO_FS_TYPE, MS_RDONLY, "");
684 // external sd
685 char decrypt_external[256], external_blkdev[256];
686 property_get("ro.crypto.external_encrypted", decrypt_external, "0");
a39552696ff55ce2013-01-08 16:14:56 +0000687 // Mount the external storage as ecryptfs so that ecryptfs can act as a pass-through
Dees_Troy85f44ed2013-01-09 18:42:36 +0000688 if (!rc2 && strcmp(decrypt_external, "1") == 0) {
a39552696ff55ce2013-01-08 16:14:56 +0000689 printf("Mounting external with ecryptfs...\n");
690 strcpy(pwbuf, passwd);
691 rc2 = mount_ecryptfs_drive(
692 pwbuf, EXPAND(TW_EXTERNAL_STORAGE_PATH),
693 EXPAND(TW_EXTERNAL_STORAGE_PATH), 0);
Dees_Troy85f44ed2013-01-09 18:42:36 +0000694 if (rc2 == 0)
695 property_set("ro.crypto.external_use_ecryptfs", "1");
696 else
697 property_set("ro.crypto.external_use_ecryptfs", "0");
Dees_Troy51a0e822012-09-05 15:24:24 -0400698 } else {
a39552696ff55ce2013-01-08 16:14:56 +0000699 printf("Unable to mount external storage with ecryptfs.\n");
700 umount(EXPAND(TW_EXTERNAL_STORAGE_PATH));
701 }
702 umount(DATA_MNT_POINT);
Dees_Troy51a0e822012-09-05 15:24:24 -0400703 }
a39552696ff55ce2013-01-08 16:14:56 +0000704#endif // #ifdef TW_EXTERNAL_STORAGE_PATH
705#endif // #ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy51a0e822012-09-05 15:24:24 -0400706 return rc;
707}