Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* TO DO: |
| 18 | * 1. Perhaps keep several copies of the encrypted key, in case something |
| 19 | * goes horribly wrong? |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <sys/types.h> |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 24 | #include <linux/types.h> |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 25 | #include <sys/wait.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <ctype.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <inttypes.h> |
| 30 | #include <unistd.h> |
| 31 | #include <stdio.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <linux/dm-ioctl.h> |
| 34 | #include <libgen.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <sys/param.h> |
| 37 | #include <string.h> |
| 38 | #include <sys/mount.h> |
| 39 | #include <openssl/evp.h> |
| 40 | #include <errno.h> |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 41 | #include <linux/kdev_t.h> |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 42 | #include <time.h> |
| 43 | #include "cryptfs.h" |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 44 | #include "cutils/properties.h" |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 45 | #include "crypto_scrypt.h" |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 46 | |
| 47 | #ifndef TW_CRYPTO_HAVE_KEYMASTERX |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 48 | #include <hardware/keymaster.h> |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 49 | #else |
| 50 | #include <stdbool.h> |
| 51 | #include <openssl/evp.h> |
| 52 | #include <openssl/sha.h> |
| 53 | #include <hardware/keymaster0.h> |
| 54 | #include <hardware/keymaster1.h> |
| 55 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 56 | |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 57 | #ifndef min /* already defined by windows.h */ |
| 58 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 59 | #endif |
| 60 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 61 | #define UNUSED __attribute__((unused)) |
| 62 | |
| 63 | #define UNUSED __attribute__((unused)) |
| 64 | |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 65 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 66 | #include "cryptfs_hw.h" |
| 67 | #endif |
| 68 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 69 | #define DM_CRYPT_BUF_SIZE 4096 |
| 70 | |
| 71 | #define HASH_COUNT 2000 |
| 72 | #define KEY_LEN_BYTES 16 |
| 73 | #define IV_LEN_BYTES 16 |
| 74 | |
| 75 | #define KEY_IN_FOOTER "footer" |
| 76 | |
| 77 | // "default_password" encoded into hex (d=0x64 etc) |
| 78 | #define DEFAULT_PASSWORD "64656661756c745f70617373776f7264" |
| 79 | |
| 80 | #define EXT4_FS 1 |
| 81 | #define F2FS_FS 2 |
| 82 | |
| 83 | #define TABLE_LOAD_RETRIES 10 |
| 84 | |
| 85 | #define RSA_KEY_SIZE 2048 |
| 86 | #define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8) |
| 87 | #define RSA_EXPONENT 0x10001 |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 88 | #define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 89 | |
| 90 | #define RETRY_MOUNT_ATTEMPTS 10 |
| 91 | #define RETRY_MOUNT_DELAY_SECONDS 1 |
| 92 | |
| 93 | char *me = "cryptfs"; |
| 94 | |
| 95 | static unsigned char saved_master_key[KEY_LEN_BYTES]; |
| 96 | static char *saved_mount_point; |
| 97 | static int master_key_saved = 0; |
| 98 | static struct crypt_persist_data *persist_data = NULL; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 99 | static char key_fname[PROPERTY_VALUE_MAX] = ""; |
| 100 | static char real_blkdev[PROPERTY_VALUE_MAX] = ""; |
| 101 | static char file_system[PROPERTY_VALUE_MAX] = ""; |
| 102 | |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 103 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 104 | #define DEFAULT_HEX_PASSWORD "64656661756c745f70617373776f7264" |
| 105 | static int scrypt_keymaster(const char *passwd, const unsigned char *salt, |
| 106 | unsigned char *ikey, void *params); |
| 107 | static void convert_key_to_hex_ascii(const unsigned char *master_key, |
| 108 | unsigned int keysize, char *master_key_ascii); |
| 109 | static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr); |
| 110 | |
| 111 | static int get_keymaster_hw_fde_passwd(const char* passwd, unsigned char* newpw, |
| 112 | unsigned char* salt, |
| 113 | const struct crypt_mnt_ftr *ftr) |
| 114 | { |
| 115 | /* if newpw updated, return 0 |
| 116 | * if newpw not updated return -1 |
| 117 | */ |
| 118 | int rc = -1; |
| 119 | |
| 120 | if (should_use_keymaster()) { |
| 121 | if (scrypt_keymaster(passwd, salt, newpw, (void*)ftr)) { |
| 122 | printf("scrypt failed"); |
| 123 | } else { |
| 124 | rc = 0; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return rc; |
| 129 | } |
| 130 | |
| 131 | static int verify_hw_fde_passwd(char *passwd, struct crypt_mnt_ftr* crypt_ftr) |
| 132 | { |
| 133 | unsigned char newpw[32] = {0}; |
| 134 | int key_index; |
| 135 | if (get_keymaster_hw_fde_passwd(passwd, newpw, crypt_ftr->salt, crypt_ftr)) |
| 136 | key_index = set_hw_device_encryption_key(passwd, |
| 137 | (char*) crypt_ftr->crypto_type_name); |
| 138 | else |
| 139 | key_index = set_hw_device_encryption_key((const char*)newpw, |
| 140 | (char*) crypt_ftr->crypto_type_name); |
| 141 | return key_index; |
| 142 | } |
| 143 | |
| 144 | static int verify_and_update_hw_fde_passwd(char *passwd, |
| 145 | struct crypt_mnt_ftr* crypt_ftr) |
| 146 | { |
| 147 | char* new_passwd = NULL; |
| 148 | unsigned char newpw[32] = {0}; |
| 149 | int key_index = -1; |
| 150 | int passwd_updated = -1; |
| 151 | int ascii_passwd_updated = (crypt_ftr->flags & CRYPT_ASCII_PASSWORD_UPDATED); |
| 152 | |
| 153 | key_index = verify_hw_fde_passwd(passwd, crypt_ftr); |
| 154 | if (key_index < 0) { |
| 155 | ++crypt_ftr->failed_decrypt_count; |
| 156 | |
| 157 | if (ascii_passwd_updated) { |
| 158 | printf("Ascii password was updated"); |
| 159 | } else { |
| 160 | /* Code in else part would execute only once: |
| 161 | * When device is upgraded from L->M release. |
| 162 | * Once upgraded, code flow should never come here. |
| 163 | * L release passed actual password in hex, so try with hex |
| 164 | * Each nible of passwd was encoded as a byte, so allocate memory |
| 165 | * twice of password len plus one more byte for null termination |
| 166 | */ |
| 167 | if (crypt_ftr->crypt_type == CRYPT_TYPE_DEFAULT) { |
| 168 | new_passwd = (char*)malloc(strlen(DEFAULT_HEX_PASSWORD) + 1); |
| 169 | if (new_passwd == NULL) { |
| 170 | printf("System out of memory. Password verification incomplete"); |
| 171 | goto out; |
| 172 | } |
| 173 | strlcpy(new_passwd, DEFAULT_HEX_PASSWORD, strlen(DEFAULT_HEX_PASSWORD) + 1); |
| 174 | } else { |
| 175 | new_passwd = (char*)malloc(strlen(passwd) * 2 + 1); |
| 176 | if (new_passwd == NULL) { |
| 177 | printf("System out of memory. Password verification incomplete"); |
| 178 | goto out; |
| 179 | } |
| 180 | convert_key_to_hex_ascii((const unsigned char*)passwd, |
| 181 | strlen(passwd), new_passwd); |
| 182 | } |
| 183 | key_index = set_hw_device_encryption_key((const char*)new_passwd, |
| 184 | (char*) crypt_ftr->crypto_type_name); |
| 185 | if (key_index >=0) { |
| 186 | crypt_ftr->failed_decrypt_count = 0; |
| 187 | printf("Hex password verified...will try to update with Ascii value"); |
| 188 | /* Before updating password, tie that with keymaster to tie with ROT */ |
| 189 | |
| 190 | if (get_keymaster_hw_fde_passwd(passwd, newpw, |
| 191 | crypt_ftr->salt, crypt_ftr)) { |
| 192 | passwd_updated = update_hw_device_encryption_key(new_passwd, |
| 193 | passwd, (char*)crypt_ftr->crypto_type_name); |
| 194 | } else { |
| 195 | passwd_updated = update_hw_device_encryption_key(new_passwd, |
| 196 | (const char*)newpw, (char*)crypt_ftr->crypto_type_name); |
| 197 | } |
| 198 | |
| 199 | if (passwd_updated >= 0) { |
| 200 | crypt_ftr->flags |= CRYPT_ASCII_PASSWORD_UPDATED; |
| 201 | printf("Ascii password recorded and updated"); |
| 202 | } else { |
| 203 | printf("Passwd verified, could not update...Will try next time"); |
| 204 | } |
| 205 | } else { |
| 206 | ++crypt_ftr->failed_decrypt_count; |
| 207 | } |
| 208 | free(new_passwd); |
| 209 | } |
| 210 | } else { |
| 211 | if (!ascii_passwd_updated) |
| 212 | crypt_ftr->flags |= CRYPT_ASCII_PASSWORD_UPDATED; |
| 213 | } |
| 214 | out: |
| 215 | // DO NOT update footer before leaving |
| 216 | // put_crypt_ftr_and_key(crypt_ftr); |
| 217 | return key_index; |
| 218 | } |
| 219 | #endif |
| 220 | |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 221 | void set_partition_data(const char* block_device, const char* key_location, const char* fs) |
| 222 | { |
| 223 | strcpy(key_fname, key_location); |
| 224 | strcpy(real_blkdev, block_device); |
| 225 | strcpy(file_system, fs); |
| 226 | } |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 227 | |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 228 | #ifndef TW_CRYPTO_HAVE_KEYMASTERX |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 229 | static int keymaster_init(keymaster_device_t **keymaster_dev) |
| 230 | { |
| 231 | int rc; |
| 232 | |
| 233 | const hw_module_t* mod; |
| 234 | rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); |
| 235 | if (rc) { |
| 236 | printf("could not find any keystore module\n"); |
| 237 | goto out; |
| 238 | } |
| 239 | |
| 240 | rc = keymaster_open(mod, keymaster_dev); |
| 241 | if (rc) { |
| 242 | printf("could not open keymaster device in %s (%s)\n", |
| 243 | KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); |
| 244 | goto out; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | |
| 249 | out: |
| 250 | *keymaster_dev = NULL; |
| 251 | return rc; |
| 252 | } |
| 253 | |
| 254 | /* Should we use keymaster? */ |
| 255 | static int keymaster_check_compatibility() |
| 256 | { |
| 257 | keymaster_device_t *keymaster_dev = 0; |
| 258 | int rc = 0; |
| 259 | |
| 260 | if (keymaster_init(&keymaster_dev)) { |
| 261 | printf("Failed to init keymaster\n"); |
| 262 | rc = -1; |
| 263 | goto out; |
| 264 | } |
| 265 | |
| 266 | printf("keymaster version is %d\n", keymaster_dev->common.module->module_api_version); |
| 267 | |
that | ceb7b8e | 2014-12-09 23:15:16 +0100 | [diff] [blame] | 268 | #if (KEYMASTER_HEADER_VERSION >= 3) |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 269 | if (keymaster_dev->common.module->module_api_version |
| 270 | < KEYMASTER_MODULE_API_VERSION_0_3) { |
| 271 | rc = 0; |
| 272 | goto out; |
| 273 | } |
| 274 | |
| 275 | if (keymaster_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE) { |
| 276 | rc = 1; |
| 277 | } |
| 278 | |
that | ceb7b8e | 2014-12-09 23:15:16 +0100 | [diff] [blame] | 279 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 280 | out: |
| 281 | keymaster_close(keymaster_dev); |
| 282 | return rc; |
| 283 | } |
| 284 | |
| 285 | /* Create a new keymaster key and store it in this footer */ |
| 286 | static int keymaster_create_key(struct crypt_mnt_ftr *ftr) |
| 287 | { |
| 288 | uint8_t* key = 0; |
| 289 | keymaster_device_t *keymaster_dev = 0; |
| 290 | |
| 291 | if (keymaster_init(&keymaster_dev)) { |
| 292 | printf("Failed to init keymaster\n"); |
| 293 | return -1; |
| 294 | } |
| 295 | |
| 296 | int rc = 0; |
| 297 | |
| 298 | keymaster_rsa_keygen_params_t params; |
| 299 | memset(¶ms, '\0', sizeof(params)); |
| 300 | params.public_exponent = RSA_EXPONENT; |
| 301 | params.modulus_size = RSA_KEY_SIZE; |
| 302 | |
| 303 | size_t key_size; |
| 304 | if (keymaster_dev->generate_keypair(keymaster_dev, TYPE_RSA, ¶ms, |
| 305 | &key, &key_size)) { |
| 306 | printf("Failed to generate keypair\n"); |
| 307 | rc = -1; |
| 308 | goto out; |
| 309 | } |
| 310 | |
| 311 | if (key_size > KEYMASTER_BLOB_SIZE) { |
| 312 | printf("Keymaster key too large for crypto footer\n"); |
| 313 | rc = -1; |
| 314 | goto out; |
| 315 | } |
| 316 | |
| 317 | memcpy(ftr->keymaster_blob, key, key_size); |
| 318 | ftr->keymaster_blob_size = key_size; |
| 319 | |
| 320 | out: |
| 321 | keymaster_close(keymaster_dev); |
| 322 | free(key); |
| 323 | return rc; |
| 324 | } |
| 325 | |
| 326 | /* This signs the given object using the keymaster key. */ |
| 327 | static int keymaster_sign_object(struct crypt_mnt_ftr *ftr, |
| 328 | const unsigned char *object, |
| 329 | const size_t object_size, |
| 330 | unsigned char **signature, |
| 331 | size_t *signature_size) |
| 332 | { |
| 333 | int rc = 0; |
| 334 | keymaster_device_t *keymaster_dev = 0; |
| 335 | if (keymaster_init(&keymaster_dev)) { |
| 336 | printf("Failed to init keymaster\n"); |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | /* We currently set the digest type to DIGEST_NONE because it's the |
| 341 | * only supported value for keymaster. A similar issue exists with |
| 342 | * PADDING_NONE. Long term both of these should likely change. |
| 343 | */ |
| 344 | keymaster_rsa_sign_params_t params; |
| 345 | params.digest_type = DIGEST_NONE; |
| 346 | params.padding_type = PADDING_NONE; |
| 347 | |
| 348 | unsigned char to_sign[RSA_KEY_SIZE_BYTES]; |
| 349 | size_t to_sign_size = sizeof(to_sign); |
| 350 | memset(to_sign, 0, RSA_KEY_SIZE_BYTES); |
| 351 | |
| 352 | // To sign a message with RSA, the message must satisfy two |
| 353 | // constraints: |
| 354 | // |
| 355 | // 1. The message, when interpreted as a big-endian numeric value, must |
| 356 | // be strictly less than the public modulus of the RSA key. Note |
| 357 | // that because the most significant bit of the public modulus is |
| 358 | // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit |
| 359 | // key), an n-bit message with most significant bit 0 always |
| 360 | // satisfies this requirement. |
| 361 | // |
| 362 | // 2. The message must have the same length in bits as the public |
| 363 | // modulus of the RSA key. This requirement isn't mathematically |
| 364 | // necessary, but is necessary to ensure consistency in |
| 365 | // implementations. |
| 366 | switch (ftr->kdf_type) { |
| 367 | case KDF_SCRYPT_KEYMASTER_UNPADDED: |
| 368 | // This is broken: It produces a message which is shorter than |
| 369 | // the public modulus, failing criterion 2. |
| 370 | memcpy(to_sign, object, object_size); |
| 371 | to_sign_size = object_size; |
| 372 | printf("Signing unpadded object\n"); |
| 373 | break; |
| 374 | case KDF_SCRYPT_KEYMASTER_BADLY_PADDED: |
| 375 | // This is broken: Since the value of object is uniformly |
| 376 | // distributed, it produces a message that is larger than the |
| 377 | // public modulus with probability 0.25. |
| 378 | memcpy(to_sign, object, min(RSA_KEY_SIZE_BYTES, object_size)); |
| 379 | printf("Signing end-padded object\n"); |
| 380 | break; |
| 381 | case KDF_SCRYPT_KEYMASTER: |
| 382 | // This ensures the most significant byte of the signed message |
| 383 | // is zero. We could have zero-padded to the left instead, but |
| 384 | // this approach is slightly more robust against changes in |
| 385 | // object size. However, it's still broken (but not unusably |
| 386 | // so) because we really should be using a proper RSA padding |
| 387 | // function, such as OAEP. |
| 388 | // |
| 389 | // TODO(paullawrence): When keymaster 0.4 is available, change |
| 390 | // this to use the padding options it provides. |
| 391 | memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size)); |
| 392 | printf("Signing safely-padded object\n"); |
| 393 | break; |
| 394 | default: |
| 395 | printf("Unknown KDF type %d\n", ftr->kdf_type); |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | rc = keymaster_dev->sign_data(keymaster_dev, |
| 400 | ¶ms, |
| 401 | ftr->keymaster_blob, |
| 402 | ftr->keymaster_blob_size, |
| 403 | to_sign, |
| 404 | to_sign_size, |
| 405 | signature, |
| 406 | signature_size); |
| 407 | |
| 408 | keymaster_close(keymaster_dev); |
| 409 | return rc; |
| 410 | } |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 411 | #else //#ifndef TW_CRYPTO_HAVE_KEYMASTERX |
| 412 | static int keymaster_init(keymaster0_device_t **keymaster0_dev, |
| 413 | keymaster1_device_t **keymaster1_dev) |
| 414 | { |
| 415 | int rc; |
| 416 | |
| 417 | const hw_module_t* mod; |
| 418 | rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); |
| 419 | if (rc) { |
| 420 | printf("could not find any keystore module\n"); |
| 421 | goto err; |
| 422 | } |
| 423 | |
| 424 | printf("keymaster module name is %s\n", mod->name); |
| 425 | printf("keymaster version is %d\n", mod->module_api_version); |
| 426 | |
| 427 | *keymaster0_dev = NULL; |
| 428 | *keymaster1_dev = NULL; |
| 429 | if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) { |
| 430 | printf("Found keymaster1 module, using keymaster1 API.\n"); |
| 431 | rc = keymaster1_open(mod, keymaster1_dev); |
| 432 | } else { |
| 433 | printf("Found keymaster0 module, using keymaster0 API.\n"); |
| 434 | rc = keymaster0_open(mod, keymaster0_dev); |
| 435 | } |
| 436 | |
| 437 | if (rc) { |
| 438 | printf("could not open keymaster device in %s (%s)\n", |
| 439 | KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); |
| 440 | goto err; |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | |
| 445 | err: |
| 446 | *keymaster0_dev = NULL; |
| 447 | *keymaster1_dev = NULL; |
| 448 | return rc; |
| 449 | } |
| 450 | |
| 451 | /* Should we use keymaster? */ |
| 452 | static int keymaster_check_compatibility() |
| 453 | { |
| 454 | keymaster0_device_t *keymaster0_dev = 0; |
| 455 | keymaster1_device_t *keymaster1_dev = 0; |
| 456 | int rc = 0; |
| 457 | |
| 458 | if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) { |
| 459 | printf("Failed to init keymaster\n"); |
| 460 | rc = -1; |
| 461 | goto out; |
| 462 | } |
| 463 | |
| 464 | if (keymaster1_dev) { |
| 465 | rc = 1; |
| 466 | goto out; |
| 467 | } |
| 468 | |
| 469 | // TODO(swillden): Check to see if there's any reason to require v0.3. I think v0.1 and v0.2 |
| 470 | // should work. |
| 471 | if (keymaster0_dev->common.module->module_api_version |
| 472 | < KEYMASTER_MODULE_API_VERSION_0_3) { |
| 473 | rc = 0; |
| 474 | goto out; |
| 475 | } |
| 476 | |
| 477 | if (!(keymaster0_dev->flags & KEYMASTER_SOFTWARE_ONLY) && |
| 478 | (keymaster0_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE)) { |
| 479 | rc = 1; |
| 480 | } |
| 481 | |
| 482 | out: |
| 483 | if (keymaster1_dev) { |
| 484 | keymaster1_close(keymaster1_dev); |
| 485 | } |
| 486 | if (keymaster0_dev) { |
| 487 | keymaster0_close(keymaster0_dev); |
| 488 | } |
| 489 | return rc; |
| 490 | } |
| 491 | |
| 492 | /* Create a new keymaster key and store it in this footer */ |
| 493 | static int keymaster_create_key(struct crypt_mnt_ftr *ftr) |
| 494 | { |
| 495 | uint8_t* key = 0; |
| 496 | keymaster0_device_t *keymaster0_dev = 0; |
| 497 | keymaster1_device_t *keymaster1_dev = 0; |
| 498 | |
| 499 | if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) { |
| 500 | printf("Failed to init keymaster\n"); |
| 501 | return -1; |
| 502 | } |
| 503 | |
| 504 | int rc = 0; |
| 505 | size_t key_size = 0; |
| 506 | if (keymaster1_dev) { |
| 507 | keymaster_key_param_t params[] = { |
| 508 | /* Algorithm & size specifications. Stick with RSA for now. Switch to AES later. */ |
| 509 | keymaster_param_enum(KM_TAG_ALGORITHM, KM_ALGORITHM_RSA), |
| 510 | keymaster_param_int(KM_TAG_KEY_SIZE, RSA_KEY_SIZE), |
| 511 | keymaster_param_long(KM_TAG_RSA_PUBLIC_EXPONENT, RSA_EXPONENT), |
| 512 | |
| 513 | /* The only allowed purpose for this key is signing. */ |
| 514 | keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_SIGN), |
| 515 | |
| 516 | /* Padding & digest specifications. */ |
| 517 | keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE), |
| 518 | keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE), |
| 519 | |
| 520 | /* Require that the key be usable in standalone mode. File system isn't available. */ |
| 521 | keymaster_param_enum(KM_TAG_BLOB_USAGE_REQUIREMENTS, KM_BLOB_STANDALONE), |
| 522 | |
| 523 | /* No auth requirements, because cryptfs is not yet integrated with gatekeeper. */ |
| 524 | keymaster_param_bool(KM_TAG_NO_AUTH_REQUIRED), |
| 525 | |
| 526 | /* Rate-limit key usage attempts, to rate-limit brute force */ |
| 527 | keymaster_param_int(KM_TAG_MIN_SECONDS_BETWEEN_OPS, KEYMASTER_CRYPTFS_RATE_LIMIT), |
| 528 | }; |
| 529 | keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) }; |
| 530 | keymaster_key_blob_t key_blob; |
| 531 | keymaster_error_t error = keymaster1_dev->generate_key(keymaster1_dev, ¶m_set, |
| 532 | &key_blob, |
| 533 | NULL /* characteristics */); |
| 534 | if (error != KM_ERROR_OK) { |
| 535 | printf("Failed to generate keymaster1 key, error %d\n", error); |
| 536 | rc = -1; |
| 537 | goto out; |
| 538 | } |
| 539 | |
| 540 | key = (uint8_t*)key_blob.key_material; |
| 541 | key_size = key_blob.key_material_size; |
| 542 | } |
| 543 | else if (keymaster0_dev) { |
| 544 | keymaster_rsa_keygen_params_t params; |
| 545 | memset(¶ms, '\0', sizeof(params)); |
| 546 | params.public_exponent = RSA_EXPONENT; |
| 547 | params.modulus_size = RSA_KEY_SIZE; |
| 548 | |
| 549 | if (keymaster0_dev->generate_keypair(keymaster0_dev, TYPE_RSA, ¶ms, |
| 550 | &key, &key_size)) { |
| 551 | printf("Failed to generate keypair\n"); |
| 552 | rc = -1; |
| 553 | goto out; |
| 554 | } |
| 555 | } else { |
| 556 | printf("Cryptfs bug: keymaster_init succeeded but didn't initialize a device\n"); |
| 557 | rc = -1; |
| 558 | goto out; |
| 559 | } |
| 560 | |
| 561 | if (key_size > KEYMASTER_BLOB_SIZE) { |
| 562 | printf("Keymaster key too large for crypto footer\n"); |
| 563 | rc = -1; |
| 564 | goto out; |
| 565 | } |
| 566 | |
| 567 | memcpy(ftr->keymaster_blob, key, key_size); |
| 568 | ftr->keymaster_blob_size = key_size; |
| 569 | |
| 570 | out: |
| 571 | if (keymaster0_dev) |
| 572 | keymaster0_close(keymaster0_dev); |
| 573 | if (keymaster1_dev) |
| 574 | keymaster1_close(keymaster1_dev); |
| 575 | free(key); |
| 576 | return rc; |
| 577 | } |
| 578 | |
| 579 | /* This signs the given object using the keymaster key. */ |
| 580 | static int keymaster_sign_object(struct crypt_mnt_ftr *ftr, |
| 581 | const unsigned char *object, |
| 582 | const size_t object_size, |
| 583 | unsigned char **signature, |
| 584 | size_t *signature_size) |
| 585 | { |
| 586 | int rc = 0; |
| 587 | keymaster0_device_t *keymaster0_dev = 0; |
| 588 | keymaster1_device_t *keymaster1_dev = 0; |
| 589 | if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) { |
| 590 | printf("Failed to init keymaster\n"); |
| 591 | rc = -1; |
| 592 | goto out; |
| 593 | } |
| 594 | |
| 595 | unsigned char to_sign[RSA_KEY_SIZE_BYTES]; |
| 596 | size_t to_sign_size = sizeof(to_sign); |
| 597 | memset(to_sign, 0, RSA_KEY_SIZE_BYTES); |
| 598 | |
| 599 | // To sign a message with RSA, the message must satisfy two |
| 600 | // constraints: |
| 601 | // |
| 602 | // 1. The message, when interpreted as a big-endian numeric value, must |
| 603 | // be strictly less than the public modulus of the RSA key. Note |
| 604 | // that because the most significant bit of the public modulus is |
| 605 | // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit |
| 606 | // key), an n-bit message with most significant bit 0 always |
| 607 | // satisfies this requirement. |
| 608 | // |
| 609 | // 2. The message must have the same length in bits as the public |
| 610 | // modulus of the RSA key. This requirement isn't mathematically |
| 611 | // necessary, but is necessary to ensure consistency in |
| 612 | // implementations. |
| 613 | switch (ftr->kdf_type) { |
| 614 | case KDF_SCRYPT_KEYMASTER: |
| 615 | // This ensures the most significant byte of the signed message |
| 616 | // is zero. We could have zero-padded to the left instead, but |
| 617 | // this approach is slightly more robust against changes in |
| 618 | // object size. However, it's still broken (but not unusably |
| 619 | // so) because we really should be using a proper deterministic |
| 620 | // RSA padding function, such as PKCS1. |
| 621 | memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size)); |
| 622 | printf("Signing safely-padded object\n"); |
| 623 | break; |
| 624 | default: |
| 625 | printf("Unknown KDF type %d\n", ftr->kdf_type); |
| 626 | rc = -1; |
| 627 | goto out; |
| 628 | } |
| 629 | |
| 630 | if (keymaster0_dev) { |
| 631 | keymaster_rsa_sign_params_t params; |
| 632 | params.digest_type = DIGEST_NONE; |
| 633 | params.padding_type = PADDING_NONE; |
| 634 | |
| 635 | rc = keymaster0_dev->sign_data(keymaster0_dev, |
| 636 | ¶ms, |
| 637 | ftr->keymaster_blob, |
| 638 | ftr->keymaster_blob_size, |
| 639 | to_sign, |
| 640 | to_sign_size, |
| 641 | signature, |
| 642 | signature_size); |
| 643 | goto out; |
| 644 | } else if (keymaster1_dev) { |
| 645 | keymaster_key_blob_t key = { ftr->keymaster_blob, ftr->keymaster_blob_size }; |
| 646 | keymaster_key_param_t params[] = { |
| 647 | keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE), |
| 648 | keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE), |
| 649 | }; |
| 650 | keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) }; |
| 651 | keymaster_operation_handle_t op_handle; |
| 652 | keymaster_error_t error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key, |
| 653 | ¶m_set, NULL /* out_params */, |
| 654 | &op_handle); |
| 655 | if (error == KM_ERROR_KEY_RATE_LIMIT_EXCEEDED) { |
| 656 | // Key usage has been rate-limited. Wait a bit and try again. |
| 657 | sleep(KEYMASTER_CRYPTFS_RATE_LIMIT); |
| 658 | error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key, |
| 659 | ¶m_set, NULL /* out_params */, |
| 660 | &op_handle); |
| 661 | } |
| 662 | if (error != KM_ERROR_OK) { |
| 663 | printf("Error starting keymaster signature transaction: %d\n", error); |
| 664 | rc = -1; |
| 665 | goto out; |
| 666 | } |
| 667 | |
| 668 | keymaster_blob_t input = { to_sign, to_sign_size }; |
| 669 | size_t input_consumed; |
| 670 | error = keymaster1_dev->update(keymaster1_dev, op_handle, NULL /* in_params */, |
| 671 | &input, &input_consumed, NULL /* out_params */, |
| 672 | NULL /* output */); |
| 673 | if (error != KM_ERROR_OK) { |
| 674 | printf("Error sending data to keymaster signature transaction: %d\n", error); |
| 675 | rc = -1; |
| 676 | goto out; |
| 677 | } |
| 678 | if (input_consumed != to_sign_size) { |
| 679 | // This should never happen. If it does, it's a bug in the keymaster implementation. |
| 680 | printf("Keymaster update() did not consume all data.\n"); |
| 681 | keymaster1_dev->abort(keymaster1_dev, op_handle); |
| 682 | rc = -1; |
| 683 | goto out; |
| 684 | } |
| 685 | |
| 686 | keymaster_blob_t tmp_sig; |
| 687 | error = keymaster1_dev->finish(keymaster1_dev, op_handle, NULL /* in_params */, |
| 688 | NULL /* verify signature */, NULL /* out_params */, |
| 689 | &tmp_sig); |
| 690 | if (error != KM_ERROR_OK) { |
| 691 | printf("Error finishing keymaster signature transaction: %d\n", error); |
| 692 | rc = -1; |
| 693 | goto out; |
| 694 | } |
| 695 | |
| 696 | *signature = (uint8_t*)tmp_sig.data; |
| 697 | *signature_size = tmp_sig.data_length; |
| 698 | } else { |
| 699 | printf("Cryptfs bug: keymaster_init succeded but didn't initialize a device.\n"); |
| 700 | rc = -1; |
| 701 | goto out; |
| 702 | } |
| 703 | |
| 704 | out: |
| 705 | if (keymaster1_dev) |
| 706 | keymaster1_close(keymaster1_dev); |
| 707 | if (keymaster0_dev) |
| 708 | keymaster0_close(keymaster0_dev); |
| 709 | |
| 710 | return rc; |
| 711 | } |
| 712 | #endif //#ifndef TW_CRYPTO_HAVE_KEYMASTERX |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 713 | |
| 714 | /* Store password when userdata is successfully decrypted and mounted. |
| 715 | * Cleared by cryptfs_clear_password |
| 716 | * |
| 717 | * To avoid a double prompt at boot, we need to store the CryptKeeper |
| 718 | * password and pass it to KeyGuard, which uses it to unlock KeyStore. |
| 719 | * Since the entire framework is torn down and rebuilt after encryption, |
| 720 | * we have to use a daemon or similar to store the password. Since vold |
| 721 | * is secured against IPC except from system processes, it seems a reasonable |
| 722 | * place to store this. |
| 723 | * |
| 724 | * password should be cleared once it has been used. |
| 725 | * |
| 726 | * password is aged out after password_max_age_seconds seconds. |
| 727 | */ |
| 728 | static char* password = 0; |
| 729 | static int password_expiry_time = 0; |
| 730 | static const int password_max_age_seconds = 60; |
| 731 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 732 | static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags) |
| 733 | { |
| 734 | memset(io, 0, dataSize); |
| 735 | io->data_size = dataSize; |
| 736 | io->data_start = sizeof(struct dm_ioctl); |
| 737 | io->version[0] = 4; |
| 738 | io->version[1] = 0; |
| 739 | io->version[2] = 0; |
| 740 | io->flags = flags; |
| 741 | if (name) { |
| 742 | strncpy(io->name, name, sizeof(io->name)); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * Gets the default device scrypt parameters for key derivation time tuning. |
| 748 | * The parameters should lead to about one second derivation time for the |
| 749 | * given device. |
| 750 | */ |
| 751 | static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) { |
| 752 | const int default_params[] = SCRYPT_DEFAULTS; |
| 753 | int params[] = SCRYPT_DEFAULTS; |
| 754 | char paramstr[PROPERTY_VALUE_MAX]; |
| 755 | char *token; |
| 756 | char *saveptr; |
| 757 | int i; |
| 758 | |
| 759 | property_get(SCRYPT_PROP, paramstr, ""); |
| 760 | if (paramstr[0] != '\0') { |
| 761 | /* |
| 762 | * The token we're looking for should be three integers separated by |
| 763 | * colons (e.g., "12:8:1"). Scan the property to make sure it matches. |
| 764 | */ |
| 765 | for (i = 0, token = strtok_r(paramstr, ":", &saveptr); |
| 766 | token != NULL && i < 3; |
| 767 | i++, token = strtok_r(NULL, ":", &saveptr)) { |
| 768 | char *endptr; |
| 769 | params[i] = strtol(token, &endptr, 10); |
| 770 | |
| 771 | /* |
| 772 | * Check that there was a valid number and it's 8-bit. If not, |
| 773 | * break out and the end check will take the default values. |
| 774 | */ |
| 775 | if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) { |
| 776 | break; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * If there were not enough tokens or a token was malformed (not an |
| 782 | * integer), it will end up here and the default parameters can be |
| 783 | * taken. |
| 784 | */ |
| 785 | if ((i != 3) || (token != NULL)) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 786 | printf("bad scrypt parameters '%s' should be like '12:8:1'; using defaults\n", paramstr); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 787 | memcpy(params, default_params, sizeof(params)); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | ftr->N_factor = params[0]; |
| 792 | ftr->r_factor = params[1]; |
| 793 | ftr->p_factor = params[2]; |
| 794 | } |
| 795 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 796 | static unsigned int get_blkdev_size(int fd) |
| 797 | { |
| 798 | unsigned int nr_sec; |
| 799 | |
| 800 | if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) { |
| 801 | nr_sec = 0; |
| 802 | } |
| 803 | |
| 804 | return nr_sec; |
| 805 | } |
| 806 | |
| 807 | static int get_crypt_ftr_info(char **metadata_fname, off64_t *off) |
| 808 | { |
| 809 | static int cached_data = 0; |
| 810 | static off64_t cached_off = 0; |
| 811 | static char cached_metadata_fname[PROPERTY_VALUE_MAX] = ""; |
| 812 | int fd; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 813 | unsigned int nr_sec; |
| 814 | int rc = -1; |
| 815 | |
| 816 | if (!cached_data) { |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 817 | printf("get_crypt_ftr_info crypto key location: '%s'\n", key_fname); |
| 818 | if (!strcmp(key_fname, KEY_IN_FOOTER)) { |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 819 | if ( (fd = open(real_blkdev, O_RDWR)) < 0) { |
| 820 | printf("Cannot open real block device %s\n", real_blkdev); |
| 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | if ((nr_sec = get_blkdev_size(fd))) { |
| 825 | /* If it's an encrypted Android partition, the last 16 Kbytes contain the |
| 826 | * encryption info footer and key, and plenty of bytes to spare for future |
| 827 | * growth. |
| 828 | */ |
| 829 | strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname)); |
| 830 | cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
| 831 | cached_data = 1; |
| 832 | } else { |
| 833 | printf("Cannot get size of block device %s\n", real_blkdev); |
| 834 | } |
| 835 | close(fd); |
| 836 | } else { |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 837 | strlcpy(cached_metadata_fname, key_fname, sizeof(cached_metadata_fname)); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 838 | cached_off = 0; |
| 839 | cached_data = 1; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if (cached_data) { |
| 844 | if (metadata_fname) { |
| 845 | *metadata_fname = cached_metadata_fname; |
| 846 | } |
| 847 | if (off) { |
| 848 | *off = cached_off; |
| 849 | } |
| 850 | rc = 0; |
| 851 | } |
| 852 | |
| 853 | return rc; |
| 854 | } |
| 855 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 856 | static inline int unix_read(int fd, void* buff, int len) |
| 857 | { |
| 858 | return TEMP_FAILURE_RETRY(read(fd, buff, len)); |
| 859 | } |
| 860 | |
| 861 | static inline int unix_write(int fd, const void* buff, int len) |
| 862 | { |
| 863 | return TEMP_FAILURE_RETRY(write(fd, buff, len)); |
| 864 | } |
| 865 | |
| 866 | static void init_empty_persist_data(struct crypt_persist_data *pdata, int len) |
| 867 | { |
| 868 | memset(pdata, 0, len); |
| 869 | pdata->persist_magic = PERSIST_DATA_MAGIC; |
| 870 | pdata->persist_valid_entries = 0; |
| 871 | } |
| 872 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 873 | static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr) |
| 874 | { |
| 875 | int fd; |
| 876 | unsigned int nr_sec, cnt; |
| 877 | off64_t starting_off; |
| 878 | int rc = -1; |
| 879 | char *fname = NULL; |
| 880 | struct stat statbuf; |
| 881 | |
| 882 | if (get_crypt_ftr_info(&fname, &starting_off)) { |
| 883 | printf("Unable to get crypt_ftr_info\n"); |
| 884 | return -1; |
| 885 | } |
| 886 | if (fname[0] != '/') { |
| 887 | printf("Unexpected value for crypto key location\n"); |
| 888 | return -1; |
| 889 | } |
| 890 | if ( (fd = open(fname, O_RDWR)) < 0) { |
| 891 | printf("Cannot open footer file %s for get\n", fname); |
| 892 | return -1; |
| 893 | } |
| 894 | |
| 895 | /* Make sure it's 16 Kbytes in length */ |
| 896 | fstat(fd, &statbuf); |
| 897 | if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) { |
| 898 | printf("footer file %s is not the expected size!\n", fname); |
| 899 | goto errout; |
| 900 | } |
| 901 | |
| 902 | /* Seek to the start of the crypt footer */ |
| 903 | if (lseek64(fd, starting_off, SEEK_SET) == -1) { |
| 904 | printf("Cannot seek to real block device footer\n"); |
| 905 | goto errout; |
| 906 | } |
| 907 | |
| 908 | if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 909 | printf("Cannot read real block device footer\n"); |
| 910 | goto errout; |
| 911 | } |
| 912 | |
| 913 | if (crypt_ftr->magic != CRYPT_MNT_MAGIC) { |
| 914 | printf("Bad magic for real block device %s\n", fname); |
| 915 | goto errout; |
| 916 | } |
| 917 | |
| 918 | if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) { |
| 919 | printf("Cannot understand major version %d real block device footer; expected %d\n", |
| 920 | crypt_ftr->major_version, CURRENT_MAJOR_VERSION); |
| 921 | goto errout; |
| 922 | } |
| 923 | |
| 924 | if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) { |
| 925 | printf("Warning: crypto footer minor version %d, expected <= %d, continuing...\n", |
| 926 | crypt_ftr->minor_version, CURRENT_MINOR_VERSION); |
| 927 | } |
| 928 | |
| 929 | /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the |
| 930 | * copy on disk before returning. |
| 931 | */ |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 932 | /*if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) { |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 933 | upgrade_crypt_ftr(fd, crypt_ftr, starting_off); |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 934 | }*/ |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 935 | |
| 936 | /* Success! */ |
| 937 | rc = 0; |
| 938 | |
| 939 | errout: |
| 940 | close(fd); |
| 941 | return rc; |
| 942 | } |
| 943 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 944 | static int hexdigit (char c) |
| 945 | { |
| 946 | if (c >= '0' && c <= '9') return c - '0'; |
| 947 | c = tolower(c); |
| 948 | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
| 949 | return -1; |
| 950 | } |
| 951 | |
| 952 | static unsigned char* convert_hex_ascii_to_key(const char* master_key_ascii, |
| 953 | unsigned int* out_keysize) |
| 954 | { |
| 955 | unsigned int i; |
| 956 | *out_keysize = 0; |
| 957 | |
| 958 | size_t size = strlen (master_key_ascii); |
| 959 | if (size % 2) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 960 | printf("Trying to convert ascii string of odd length\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 961 | return NULL; |
| 962 | } |
| 963 | |
| 964 | unsigned char* master_key = (unsigned char*) malloc(size / 2); |
| 965 | if (master_key == 0) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 966 | printf("Cannot allocate\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 967 | return NULL; |
| 968 | } |
| 969 | |
| 970 | for (i = 0; i < size; i += 2) { |
| 971 | int high_nibble = hexdigit (master_key_ascii[i]); |
| 972 | int low_nibble = hexdigit (master_key_ascii[i + 1]); |
| 973 | |
| 974 | if(high_nibble < 0 || low_nibble < 0) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 975 | printf("Invalid hex string\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 976 | free (master_key); |
| 977 | return NULL; |
| 978 | } |
| 979 | |
| 980 | master_key[*out_keysize] = high_nibble * 16 + low_nibble; |
| 981 | (*out_keysize)++; |
| 982 | } |
| 983 | |
| 984 | return master_key; |
| 985 | } |
| 986 | |
| 987 | /* Convert a binary key of specified length into an ascii hex string equivalent, |
| 988 | * without the leading 0x and with null termination |
| 989 | */ |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 990 | static void convert_key_to_hex_ascii(const unsigned char *master_key, |
| 991 | unsigned int keysize, char *master_key_ascii) { |
| 992 | unsigned int i, a; |
| 993 | unsigned char nibble; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 994 | |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 995 | for (i=0, a=0; i<keysize; i++, a+=2) { |
| 996 | /* For each byte, write out two ascii hex digits */ |
| 997 | nibble = (master_key[i] >> 4) & 0xf; |
| 998 | master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 999 | |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1000 | nibble = master_key[i] & 0xf; |
| 1001 | master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 1002 | } |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1003 | |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1004 | /* Add the null termination */ |
| 1005 | master_key_ascii[a] = '\0'; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1006 | |
| 1007 | } |
| 1008 | |
| 1009 | static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key, |
| 1010 | char *real_blk_name, const char *name, int fd, |
| 1011 | char *extra_params) |
| 1012 | { |
| 1013 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1014 | struct dm_ioctl *io; |
| 1015 | struct dm_target_spec *tgt; |
| 1016 | char *crypt_params; |
| 1017 | char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */ |
| 1018 | int i; |
| 1019 | |
| 1020 | io = (struct dm_ioctl *) buffer; |
| 1021 | |
| 1022 | /* Load the mapping table for this device */ |
| 1023 | tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)]; |
| 1024 | |
| 1025 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1026 | io->target_count = 1; |
| 1027 | tgt->status = 0; |
| 1028 | tgt->sector_start = 0; |
| 1029 | tgt->length = crypt_ftr->fs_size; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1030 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1031 | if(is_hw_disk_encryption((char*)crypt_ftr->crypto_type_name)) { |
| 1032 | strlcpy(tgt->target_type, "req-crypt",DM_MAX_TYPE_NAME); |
| 1033 | if (is_ice_enabled()) |
| 1034 | convert_key_to_hex_ascii(master_key, sizeof(int), master_key_ascii); |
| 1035 | else |
| 1036 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 1037 | } |
| 1038 | else { |
| 1039 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 1040 | strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME); |
| 1041 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1042 | #else |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1043 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 1044 | strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME); |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1045 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1046 | |
| 1047 | crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec); |
| 1048 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 1049 | sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name, |
| 1050 | master_key_ascii, real_blk_name, extra_params); |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1051 | |
| 1052 | printf("%s: target_type = %s\n", __func__, tgt->target_type); |
| 1053 | printf("%s: real_blk_name = %s, extra_params = %s\n", __func__, real_blk_name, extra_params); |
| 1054 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1055 | crypt_params += strlen(crypt_params) + 1; |
| 1056 | crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */ |
| 1057 | tgt->next = crypt_params - buffer; |
| 1058 | |
| 1059 | for (i = 0; i < TABLE_LOAD_RETRIES; i++) { |
| 1060 | if (! ioctl(fd, DM_TABLE_LOAD, io)) { |
| 1061 | break; |
| 1062 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1063 | printf("%i\n", errno); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1064 | usleep(500000); |
| 1065 | } |
| 1066 | |
| 1067 | if (i == TABLE_LOAD_RETRIES) { |
| 1068 | /* We failed to load the table, return an error */ |
| 1069 | return -1; |
| 1070 | } else { |
| 1071 | return i + 1; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | |
| 1076 | static int get_dm_crypt_version(int fd, const char *name, int *version) |
| 1077 | { |
| 1078 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1079 | struct dm_ioctl *io; |
| 1080 | struct dm_target_versions *v; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1081 | int flag; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1082 | int i; |
| 1083 | |
| 1084 | io = (struct dm_ioctl *) buffer; |
| 1085 | |
| 1086 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1087 | |
| 1088 | if (ioctl(fd, DM_LIST_VERSIONS, io)) { |
| 1089 | return -1; |
| 1090 | } |
| 1091 | |
| 1092 | /* Iterate over the returned versions, looking for name of "crypt". |
| 1093 | * When found, get and return the version. |
| 1094 | */ |
| 1095 | v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)]; |
| 1096 | while (v->next) { |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1097 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1098 | if (is_hw_fde_enabled()) { |
| 1099 | flag = (!strcmp(v->name, "crypt") || !strcmp(v->name, "req-crypt")); |
| 1100 | } else { |
| 1101 | flag = (!strcmp(v->name, "crypt")); |
| 1102 | } |
| 1103 | printf("get_dm_crypt_version flag: %i, name: '%s'\n", flag, v->name); |
| 1104 | if (flag) { |
| 1105 | #else |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1106 | if (! strcmp(v->name, "crypt")) { |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1107 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1108 | /* We found the crypt driver, return the version, and get out */ |
| 1109 | version[0] = v->version[0]; |
| 1110 | version[1] = v->version[1]; |
| 1111 | version[2] = v->version[2]; |
| 1112 | return 0; |
| 1113 | } |
| 1114 | v = (struct dm_target_versions *)(((char *)v) + v->next); |
| 1115 | } |
| 1116 | |
| 1117 | return -1; |
| 1118 | } |
| 1119 | |
| 1120 | static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key, |
| 1121 | char *real_blk_name, char *crypto_blk_name, const char *name) |
| 1122 | { |
| 1123 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1124 | char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */ |
| 1125 | char *crypt_params; |
| 1126 | struct dm_ioctl *io; |
| 1127 | struct dm_target_spec *tgt; |
| 1128 | unsigned int minor; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1129 | int fd=0; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1130 | int i; |
| 1131 | int retval = -1; |
| 1132 | int version[3]; |
| 1133 | char *extra_params; |
| 1134 | int load_count; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1135 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1136 | char encrypted_state[PROPERTY_VALUE_MAX] = {0}; |
| 1137 | char progress[PROPERTY_VALUE_MAX] = {0}; |
| 1138 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1139 | |
| 1140 | if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) { |
| 1141 | printf("Cannot open device-mapper\n"); |
| 1142 | goto errout; |
| 1143 | } |
| 1144 | |
| 1145 | io = (struct dm_ioctl *) buffer; |
| 1146 | |
| 1147 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1148 | if (ioctl(fd, DM_DEV_CREATE, io)) { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1149 | printf("Cannot create dm-crypt device %i\n", errno); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1150 | goto errout; |
| 1151 | } |
| 1152 | |
| 1153 | /* Get the device status, in particular, the name of it's device file */ |
| 1154 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1155 | if (ioctl(fd, DM_DEV_STATUS, io)) { |
| 1156 | printf("Cannot retrieve dm-crypt device status\n"); |
| 1157 | goto errout; |
| 1158 | } |
| 1159 | minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); |
| 1160 | snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor); |
| 1161 | |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1162 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1163 | if(is_hw_disk_encryption((char*)crypt_ftr->crypto_type_name)) { |
| 1164 | /* Set fde_enabled if either FDE completed or in-progress */ |
| 1165 | property_get("ro.crypto.state", encrypted_state, ""); /* FDE completed */ |
| 1166 | property_get("vold.encrypt_progress", progress, ""); /* FDE in progress */ |
| 1167 | if (!strcmp(encrypted_state, "encrypted") || strcmp(progress, "")) { |
| 1168 | if (is_ice_enabled()) |
| 1169 | extra_params = "fde_enabled ice"; |
| 1170 | else |
| 1171 | extra_params = "fde_enabled"; |
| 1172 | } else |
| 1173 | extra_params = "fde_disabled"; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1174 | } else { |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1175 | extra_params = ""; |
| 1176 | if (! get_dm_crypt_version(fd, name, version)) { |
| 1177 | /* Support for allow_discards was added in version 1.11.0 */ |
| 1178 | if ((version[0] >= 2) || |
| 1179 | ((version[0] == 1) && (version[1] >= 11))) { |
| 1180 | extra_params = "1 allow_discards"; |
| 1181 | printf("Enabling support for allow_discards in dmcrypt.\n"); |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1182 | } |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1183 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1184 | } |
| 1185 | #else |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1186 | extra_params = ""; |
| 1187 | if (! get_dm_crypt_version(fd, name, version)) { |
| 1188 | /* Support for allow_discards was added in version 1.11.0 */ |
| 1189 | if ((version[0] >= 2) || |
| 1190 | ((version[0] == 1) && (version[1] >= 11))) { |
| 1191 | extra_params = "1 allow_discards"; |
| 1192 | printf("Enabling support for allow_discards in dmcrypt.\n"); |
| 1193 | } |
| 1194 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1195 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1196 | |
| 1197 | load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, |
| 1198 | fd, extra_params); |
| 1199 | if (load_count < 0) { |
| 1200 | printf("Cannot load dm-crypt mapping table.\n"); |
| 1201 | goto errout; |
| 1202 | } else if (load_count > 1) { |
| 1203 | printf("Took %d tries to load dmcrypt table.\n", load_count); |
| 1204 | } |
| 1205 | |
| 1206 | /* Resume this device to activate it */ |
| 1207 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1208 | |
| 1209 | if (ioctl(fd, DM_DEV_SUSPEND, io)) { |
| 1210 | printf("Cannot resume the dm-crypt device\n"); |
| 1211 | goto errout; |
| 1212 | } |
| 1213 | |
| 1214 | /* We made it here with no errors. Woot! */ |
| 1215 | retval = 0; |
| 1216 | |
| 1217 | errout: |
| 1218 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 1219 | |
| 1220 | return retval; |
| 1221 | } |
| 1222 | |
Ethan Yonker | d79d9bc | 2014-12-20 15:38:29 -0600 | [diff] [blame] | 1223 | int delete_crypto_blk_dev(char *name) |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1224 | { |
| 1225 | int fd; |
| 1226 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1227 | struct dm_ioctl *io; |
| 1228 | int retval = -1; |
| 1229 | |
| 1230 | if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) { |
| 1231 | printf("Cannot open device-mapper\n"); |
| 1232 | goto errout; |
| 1233 | } |
| 1234 | |
| 1235 | io = (struct dm_ioctl *) buffer; |
| 1236 | |
| 1237 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1238 | if (ioctl(fd, DM_DEV_REMOVE, io)) { |
| 1239 | printf("Cannot remove dm-crypt device\n"); |
| 1240 | goto errout; |
| 1241 | } |
| 1242 | |
| 1243 | /* We made it here with no errors. Woot! */ |
| 1244 | retval = 0; |
| 1245 | |
| 1246 | errout: |
| 1247 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 1248 | |
| 1249 | return retval; |
| 1250 | |
| 1251 | } |
| 1252 | |
| 1253 | static int pbkdf2(const char *passwd, const unsigned char *salt, |
| 1254 | unsigned char *ikey, void *params UNUSED) |
| 1255 | { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1256 | printf("Using pbkdf2 for cryptfs KDF\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1257 | |
| 1258 | /* Turn the password into a key and IV that can decrypt the master key */ |
| 1259 | unsigned int keysize; |
| 1260 | char* master_key = (char*)convert_hex_ascii_to_key(passwd, &keysize); |
| 1261 | if (!master_key) return -1; |
| 1262 | PKCS5_PBKDF2_HMAC_SHA1(master_key, keysize, salt, SALT_LEN, |
| 1263 | HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey); |
| 1264 | |
| 1265 | memset(master_key, 0, keysize); |
| 1266 | free (master_key); |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | static int scrypt(const char *passwd, const unsigned char *salt, |
| 1271 | unsigned char *ikey, void *params) |
| 1272 | { |
| 1273 | printf("Using scrypt for cryptfs KDF\n"); |
| 1274 | |
| 1275 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1276 | |
| 1277 | int N = 1 << ftr->N_factor; |
| 1278 | int r = 1 << ftr->r_factor; |
| 1279 | int p = 1 << ftr->p_factor; |
| 1280 | |
| 1281 | /* Turn the password into a key and IV that can decrypt the master key */ |
| 1282 | unsigned int keysize; |
| 1283 | unsigned char* master_key = convert_hex_ascii_to_key(passwd, &keysize); |
| 1284 | if (!master_key) return -1; |
| 1285 | crypto_scrypt(master_key, keysize, salt, SALT_LEN, N, r, p, ikey, |
| 1286 | KEY_LEN_BYTES + IV_LEN_BYTES); |
| 1287 | |
| 1288 | memset(master_key, 0, keysize); |
| 1289 | free (master_key); |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | static int scrypt_keymaster(const char *passwd, const unsigned char *salt, |
| 1294 | unsigned char *ikey, void *params) |
| 1295 | { |
| 1296 | printf("Using scrypt with keymaster for cryptfs KDF\n"); |
| 1297 | |
| 1298 | int rc; |
| 1299 | unsigned int key_size; |
| 1300 | size_t signature_size; |
| 1301 | unsigned char* signature; |
| 1302 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1303 | |
| 1304 | int N = 1 << ftr->N_factor; |
| 1305 | int r = 1 << ftr->r_factor; |
| 1306 | int p = 1 << ftr->p_factor; |
| 1307 | |
| 1308 | unsigned char* master_key = convert_hex_ascii_to_key(passwd, &key_size); |
| 1309 | if (!master_key) { |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1310 | printf("Failed to convert passwd from hex\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1311 | return -1; |
| 1312 | } |
| 1313 | |
| 1314 | rc = crypto_scrypt(master_key, key_size, salt, SALT_LEN, |
| 1315 | N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES); |
| 1316 | memset(master_key, 0, key_size); |
| 1317 | free(master_key); |
| 1318 | |
| 1319 | if (rc) { |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1320 | printf("scrypt failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1321 | return -1; |
| 1322 | } |
| 1323 | |
| 1324 | if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES, |
| 1325 | &signature, &signature_size)) { |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1326 | printf("Signing failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1327 | return -1; |
| 1328 | } |
| 1329 | |
| 1330 | rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN, |
| 1331 | N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES); |
| 1332 | free(signature); |
| 1333 | |
| 1334 | if (rc) { |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1335 | printf("scrypt failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1336 | return -1; |
| 1337 | } |
| 1338 | |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | static int encrypt_master_key(const char *passwd, const unsigned char *salt, |
| 1343 | const unsigned char *decrypted_master_key, |
| 1344 | unsigned char *encrypted_master_key, |
| 1345 | struct crypt_mnt_ftr *crypt_ftr) |
| 1346 | { |
| 1347 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
| 1348 | EVP_CIPHER_CTX e_ctx; |
| 1349 | int encrypted_len, final_len; |
| 1350 | int rc = 0; |
| 1351 | |
| 1352 | /* Turn the password into an intermediate key and IV that can decrypt the master key */ |
| 1353 | get_device_scrypt_params(crypt_ftr); |
| 1354 | |
| 1355 | switch (crypt_ftr->kdf_type) { |
| 1356 | case KDF_SCRYPT_KEYMASTER_UNPADDED: |
| 1357 | case KDF_SCRYPT_KEYMASTER_BADLY_PADDED: |
| 1358 | case KDF_SCRYPT_KEYMASTER: |
| 1359 | if (keymaster_create_key(crypt_ftr)) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1360 | printf("keymaster_create_key failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1361 | return -1; |
| 1362 | } |
| 1363 | |
| 1364 | if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1365 | printf("scrypt failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1366 | return -1; |
| 1367 | } |
| 1368 | break; |
| 1369 | |
| 1370 | case KDF_SCRYPT: |
| 1371 | if (scrypt(passwd, salt, ikey, crypt_ftr)) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1372 | printf("scrypt failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1373 | return -1; |
| 1374 | } |
| 1375 | break; |
| 1376 | |
| 1377 | default: |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1378 | printf("Invalid kdf_type\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1379 | return -1; |
| 1380 | } |
| 1381 | |
| 1382 | /* Initialize the decryption engine */ |
| 1383 | if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) { |
| 1384 | printf("EVP_EncryptInit failed\n"); |
| 1385 | return -1; |
| 1386 | } |
| 1387 | EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */ |
| 1388 | |
| 1389 | /* Encrypt the master key */ |
| 1390 | if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len, |
| 1391 | decrypted_master_key, KEY_LEN_BYTES)) { |
| 1392 | printf("EVP_EncryptUpdate failed\n"); |
| 1393 | return -1; |
| 1394 | } |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1395 | #ifndef TW_CRYPTO_HAVE_KEYMASTERX |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1396 | if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1397 | #else |
| 1398 | if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) { |
| 1399 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1400 | printf("EVP_EncryptFinal failed\n"); |
| 1401 | return -1; |
| 1402 | } |
| 1403 | |
| 1404 | if (encrypted_len + final_len != KEY_LEN_BYTES) { |
| 1405 | printf("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len); |
| 1406 | return -1; |
| 1407 | } |
| 1408 | |
| 1409 | /* Store the scrypt of the intermediate key, so we can validate if it's a |
| 1410 | password error or mount error when things go wrong. |
| 1411 | Note there's no need to check for errors, since if this is incorrect, we |
| 1412 | simply won't wipe userdata, which is the correct default behavior |
| 1413 | */ |
| 1414 | int N = 1 << crypt_ftr->N_factor; |
| 1415 | int r = 1 << crypt_ftr->r_factor; |
| 1416 | int p = 1 << crypt_ftr->p_factor; |
| 1417 | |
| 1418 | rc = crypto_scrypt(ikey, KEY_LEN_BYTES, |
| 1419 | crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p, |
| 1420 | crypt_ftr->scrypted_intermediate_key, |
| 1421 | sizeof(crypt_ftr->scrypted_intermediate_key)); |
| 1422 | |
| 1423 | if (rc) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1424 | printf("encrypt_master_key: crypto_scrypt failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | static int decrypt_master_key_aux(char *passwd, unsigned char *salt, |
| 1431 | unsigned char *encrypted_master_key, |
| 1432 | unsigned char *decrypted_master_key, |
| 1433 | kdf_func kdf, void *kdf_params, |
| 1434 | unsigned char** intermediate_key, |
| 1435 | size_t* intermediate_key_size) |
| 1436 | { |
| 1437 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
| 1438 | EVP_CIPHER_CTX d_ctx; |
| 1439 | int decrypted_len, final_len; |
| 1440 | |
| 1441 | /* Turn the password into an intermediate key and IV that can decrypt the |
| 1442 | master key */ |
| 1443 | if (kdf(passwd, salt, ikey, kdf_params)) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1444 | printf("kdf failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1445 | return -1; |
| 1446 | } |
| 1447 | |
| 1448 | /* Initialize the decryption engine */ |
| 1449 | if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) { |
| 1450 | return -1; |
| 1451 | } |
| 1452 | EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */ |
| 1453 | /* Decrypt the master key */ |
| 1454 | if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len, |
| 1455 | encrypted_master_key, KEY_LEN_BYTES)) { |
| 1456 | return -1; |
| 1457 | } |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1458 | #ifndef TW_CRYPTO_HAVE_KEYMASTERX |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1459 | if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1460 | #else |
| 1461 | if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) { |
| 1462 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1463 | return -1; |
| 1464 | } |
| 1465 | |
| 1466 | if (decrypted_len + final_len != KEY_LEN_BYTES) { |
| 1467 | return -1; |
| 1468 | } |
| 1469 | |
| 1470 | /* Copy intermediate key if needed by params */ |
| 1471 | if (intermediate_key && intermediate_key_size) { |
| 1472 | *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES); |
| 1473 | if (intermediate_key) { |
| 1474 | memcpy(*intermediate_key, ikey, KEY_LEN_BYTES); |
| 1475 | *intermediate_key_size = KEY_LEN_BYTES; |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params) |
| 1483 | { |
| 1484 | if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER_UNPADDED || |
| 1485 | ftr->kdf_type == KDF_SCRYPT_KEYMASTER_BADLY_PADDED || |
| 1486 | ftr->kdf_type == KDF_SCRYPT_KEYMASTER) { |
| 1487 | *kdf = scrypt_keymaster; |
| 1488 | *kdf_params = ftr; |
| 1489 | } else if (ftr->kdf_type == KDF_SCRYPT) { |
| 1490 | *kdf = scrypt; |
| 1491 | *kdf_params = ftr; |
| 1492 | } else { |
| 1493 | *kdf = pbkdf2; |
| 1494 | *kdf_params = NULL; |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | static int decrypt_master_key(char *passwd, unsigned char *decrypted_master_key, |
| 1499 | struct crypt_mnt_ftr *crypt_ftr, |
| 1500 | unsigned char** intermediate_key, |
| 1501 | size_t* intermediate_key_size) |
| 1502 | { |
| 1503 | kdf_func kdf; |
| 1504 | void *kdf_params; |
| 1505 | int ret; |
| 1506 | |
| 1507 | get_kdf_func(crypt_ftr, &kdf, &kdf_params); |
| 1508 | ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key, |
| 1509 | decrypted_master_key, kdf, kdf_params, |
| 1510 | intermediate_key, intermediate_key_size); |
| 1511 | if (ret != 0) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1512 | printf("failure decrypting master key\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | return ret; |
| 1516 | } |
| 1517 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1518 | static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr, |
| 1519 | char *passwd, char *mount_point, char *label) |
| 1520 | { |
| 1521 | /* Allocate enough space for a 256 bit key, but we may use less */ |
| 1522 | unsigned char decrypted_master_key[32]; |
| 1523 | char crypto_blkdev[MAXPATHLEN]; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1524 | char tmp_mount_point[64]; |
| 1525 | unsigned int orig_failed_decrypt_count; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1526 | int rc = 0; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1527 | kdf_func kdf; |
| 1528 | void *kdf_params; |
| 1529 | int use_keymaster = 0; |
| 1530 | int upgrade = 0; |
| 1531 | unsigned char* intermediate_key = 0; |
| 1532 | size_t intermediate_key_size = 0; |
| 1533 | |
| 1534 | printf("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size); |
| 1535 | orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count; |
| 1536 | |
| 1537 | if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) { |
| 1538 | if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr, |
| 1539 | &intermediate_key, &intermediate_key_size)) { |
| 1540 | printf("Failed to decrypt master key\n"); |
| 1541 | rc = -1; |
| 1542 | goto errout; |
| 1543 | } |
| 1544 | } |
| 1545 | |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1546 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1547 | int key_index = 0; |
| 1548 | if(is_hw_disk_encryption((char*)crypt_ftr->crypto_type_name)) { |
| 1549 | key_index = verify_and_update_hw_fde_passwd(passwd, crypt_ftr); |
| 1550 | if (key_index < 0) { |
| 1551 | rc = crypt_ftr->failed_decrypt_count; |
| 1552 | goto errout; |
| 1553 | } |
| 1554 | else { |
| 1555 | if (is_ice_enabled()) { |
| 1556 | if (create_crypto_blk_dev(crypt_ftr, (unsigned char*)&key_index, |
| 1557 | real_blkdev, crypto_blkdev, label)) { |
| 1558 | printf("Error creating decrypted block device"); |
| 1559 | rc = -1; |
| 1560 | goto errout; |
| 1561 | } |
| 1562 | } else { |
| 1563 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, |
| 1564 | real_blkdev, crypto_blkdev, label)) { |
| 1565 | printf("Error creating decrypted block device"); |
| 1566 | rc = -1; |
| 1567 | goto errout; |
| 1568 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1569 | } |
| 1570 | } |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1571 | } else { |
| 1572 | /* in case HW FDE is delivered through OTA and device is already encrypted |
| 1573 | * using SW FDE, we should let user continue using SW FDE until userdata is |
| 1574 | * wiped. |
| 1575 | */ |
| 1576 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, |
| 1577 | real_blkdev, crypto_blkdev, label)) { |
| 1578 | printf("Error creating decrypted block device"); |
| 1579 | rc = -1; |
| 1580 | goto errout; |
| 1581 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1582 | } |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1583 | #else |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1584 | // Create crypto block device - all (non fatal) code paths |
| 1585 | // need it |
| 1586 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, |
| 1587 | real_blkdev, crypto_blkdev, label)) { |
| 1588 | printf("Error creating decrypted block device\n"); |
| 1589 | rc = -1; |
| 1590 | goto errout; |
| 1591 | } |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1592 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1593 | |
| 1594 | /* Work out if the problem is the password or the data */ |
| 1595 | unsigned char scrypted_intermediate_key[sizeof(crypt_ftr-> |
| 1596 | scrypted_intermediate_key)]; |
| 1597 | int N = 1 << crypt_ftr->N_factor; |
| 1598 | int r = 1 << crypt_ftr->r_factor; |
| 1599 | int p = 1 << crypt_ftr->p_factor; |
| 1600 | |
| 1601 | rc = crypto_scrypt(intermediate_key, intermediate_key_size, |
| 1602 | crypt_ftr->salt, sizeof(crypt_ftr->salt), |
| 1603 | N, r, p, scrypted_intermediate_key, |
| 1604 | sizeof(scrypted_intermediate_key)); |
| 1605 | |
| 1606 | // Does the key match the crypto footer? |
| 1607 | if (rc == 0 && memcmp(scrypted_intermediate_key, |
| 1608 | crypt_ftr->scrypted_intermediate_key, |
| 1609 | sizeof(scrypted_intermediate_key)) == 0) { |
| 1610 | printf("Password matches\n"); |
| 1611 | rc = 0; |
| 1612 | } else { |
| 1613 | /* Try mounting the file system anyway, just in case the problem's with |
| 1614 | * the footer, not the key. */ |
| 1615 | sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point); |
| 1616 | mkdir(tmp_mount_point, 0755); |
Ethan Yonker | ceb1e8a | 2015-12-22 11:41:40 -0600 | [diff] [blame] | 1617 | if (mount(crypto_blkdev, tmp_mount_point, file_system, 0, NULL) != 0) { |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1618 | printf("Error temp mounting decrypted block device '%s'\n", crypto_blkdev); |
| 1619 | delete_crypto_blk_dev(label); |
| 1620 | |
| 1621 | rc = ++crypt_ftr->failed_decrypt_count; |
| 1622 | //put_crypt_ftr_and_key(crypt_ftr); // Do not penalize for attempting to decrypt in recovery |
| 1623 | } else { |
| 1624 | /* Success! */ |
| 1625 | printf("Password did not match but decrypted drive mounted - continue\n"); |
| 1626 | umount(tmp_mount_point); |
| 1627 | rc = 0; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | if (rc == 0) { |
| 1632 | /*crypt_ftr->failed_decrypt_count = 0; |
| 1633 | if (orig_failed_decrypt_count != 0) { |
| 1634 | put_crypt_ftr_and_key(crypt_ftr); |
| 1635 | }*/ |
| 1636 | |
| 1637 | /* Save the name of the crypto block device |
| 1638 | * so we can mount it when restarting the framework. */ |
| 1639 | property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev); |
| 1640 | |
| 1641 | /* Also save a the master key so we can reencrypted the key |
| 1642 | * the key when we want to change the password on it. */ |
| 1643 | /*memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES); |
| 1644 | saved_mount_point = strdup(mount_point); |
| 1645 | master_key_saved = 1; |
| 1646 | printf("%s(): Master key saved\n", __FUNCTION__);*/ |
| 1647 | rc = 0; |
| 1648 | |
| 1649 | // Upgrade if we're not using the latest KDF. |
| 1650 | /*use_keymaster = keymaster_check_compatibility(); |
| 1651 | if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) { |
| 1652 | // Don't allow downgrade |
| 1653 | } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) { |
| 1654 | crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER; |
| 1655 | upgrade = 1; |
| 1656 | } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) { |
| 1657 | crypt_ftr->kdf_type = KDF_SCRYPT; |
| 1658 | upgrade = 1; |
| 1659 | } |
| 1660 | |
| 1661 | if (upgrade) { |
| 1662 | rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key, |
| 1663 | crypt_ftr->master_key, crypt_ftr); |
| 1664 | if (!rc) { |
| 1665 | rc = put_crypt_ftr_and_key(crypt_ftr); |
| 1666 | } |
| 1667 | printf("Key Derivation Function upgrade: rc=%d\n", rc); |
| 1668 | |
| 1669 | // Do not fail even if upgrade failed - machine is bootable |
| 1670 | // Note that if this code is ever hit, there is a *serious* problem |
| 1671 | // since KDFs should never fail. You *must* fix the kdf before |
| 1672 | // proceeding! |
| 1673 | if (rc) { |
| 1674 | printf("Upgrade failed with error %d," |
| 1675 | " but continuing with previous state\n", |
| 1676 | rc); |
| 1677 | rc = 0; |
| 1678 | } |
| 1679 | }*/ |
| 1680 | } |
| 1681 | |
| 1682 | errout: |
| 1683 | if (intermediate_key) { |
| 1684 | memset(intermediate_key, 0, intermediate_key_size); |
| 1685 | free(intermediate_key); |
| 1686 | } |
| 1687 | return rc; |
| 1688 | } |
| 1689 | |
| 1690 | /* Called by vold when it wants to undo the crypto mapping of a volume it |
| 1691 | * manages. This is usually in response to a factory reset, when we want |
| 1692 | * to undo the crypto mapping so the volume is formatted in the clear. |
| 1693 | */ |
| 1694 | int cryptfs_revert_volume(const char *label) |
| 1695 | { |
| 1696 | return delete_crypto_blk_dev((char *)label); |
| 1697 | } |
| 1698 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1699 | int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr) |
| 1700 | { |
| 1701 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 1702 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1703 | if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) { |
| 1704 | printf("encrypted fs already validated or not running with encryption," |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1705 | " aborting\n"); |
| 1706 | //return -1; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | if (get_crypt_ftr_and_key(crypt_ftr)) { |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1710 | printf("Error getting crypt footer and key\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1711 | return -1; |
| 1712 | } |
| 1713 | |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * TODO - transition patterns to new format in calling code |
| 1719 | * and remove this vile hack, and the use of hex in |
| 1720 | * the password passing code. |
| 1721 | * |
| 1722 | * Patterns are passed in zero based (i.e. the top left dot |
| 1723 | * is represented by zero, the top middle one etc), but we want |
| 1724 | * to store them '1' based. |
| 1725 | * This is to allow us to migrate the calling code to use this |
| 1726 | * convention. It also solves a nasty problem whereby scrypt ignores |
| 1727 | * trailing zeros, so patterns ending at the top left could be |
| 1728 | * truncated, and similarly, you could add the top left to any |
| 1729 | * pattern and still match. |
| 1730 | * adjust_passwd is a hack function that returns the alternate representation |
| 1731 | * if the password appears to be a pattern (hex numbers all less than 09) |
| 1732 | * If it succeeds we need to try both, and in particular try the alternate |
| 1733 | * first. If the original matches, then we need to update the footer |
| 1734 | * with the alternate. |
| 1735 | * All code that accepts passwords must adjust them first. Since |
| 1736 | * cryptfs_check_passwd is always the first function called after a migration |
| 1737 | * (and indeed on any boot) we only need to do the double try in this |
| 1738 | * function. |
| 1739 | */ |
| 1740 | char* adjust_passwd(const char* passwd) |
| 1741 | { |
| 1742 | size_t index, length; |
| 1743 | |
| 1744 | if (!passwd) { |
| 1745 | return 0; |
| 1746 | } |
| 1747 | |
| 1748 | // Check even length. Hex encoded passwords are always |
| 1749 | // an even length, since each character encodes to two characters. |
| 1750 | length = strlen(passwd); |
| 1751 | if (length % 2) { |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1752 | printf("Password not correctly hex encoded.\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | // Check password is old-style pattern - a collection of hex |
| 1757 | // encoded bytes less than 9 (00 through 08) |
| 1758 | for (index = 0; index < length; index +=2) { |
| 1759 | if (passwd[index] != '0' |
| 1760 | || passwd[index + 1] < '0' || passwd[index + 1] > '8') { |
| 1761 | return 0; |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | // Allocate room for adjusted passwd and null terminate |
| 1766 | char* adjusted = malloc(length + 1); |
| 1767 | adjusted[length] = 0; |
| 1768 | |
| 1769 | // Add 0x31 ('1') to each character |
| 1770 | for (index = 0; index < length; index += 2) { |
| 1771 | // output is 31 through 39 so set first byte to three, second to src + 1 |
| 1772 | adjusted[index] = '3'; |
| 1773 | adjusted[index + 1] = passwd[index + 1] + 1; |
| 1774 | } |
| 1775 | |
| 1776 | return adjusted; |
| 1777 | } |
| 1778 | |
| 1779 | /* |
| 1780 | * Passwords in L get passed from Android to cryptfs in hex, so a '1' |
| 1781 | * gets converted to '31' where 31 is 0x31 which is the ascii character |
| 1782 | * code in hex of the character '1'. This function will convert the |
| 1783 | * regular character codes to their hexadecimal representation to make |
| 1784 | * decrypt work properly with Android 5.0 lollipop decryption. |
| 1785 | */ |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1786 | char* hexadj_passwd(const char* passwd, int has_hw_crypto) |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1787 | { |
| 1788 | size_t index, length; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1789 | const char* ptr = passwd; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1790 | |
| 1791 | if (!passwd) { |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | length = strlen(passwd); |
| 1796 | |
| 1797 | // Allocate room for hex passwd and null terminate |
| 1798 | char* hex = malloc((length * 2) + 1); |
| 1799 | hex[length * 2] = 0; |
| 1800 | |
| 1801 | // Convert to hex |
| 1802 | for (index = 0; index < length; index++) { |
| 1803 | sprintf(hex + (index * 2), "%02X", *ptr); |
| 1804 | ptr++; |
| 1805 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1806 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1807 | if (has_hw_crypto) { |
| 1808 | printf("hexadj_passwd converting to lower case for hardware disk crypto.\n"); |
| 1809 | length *= 2; |
| 1810 | for (index = 0; index < length; index++) { |
| 1811 | hex[index] = tolower(hex[index]); |
| 1812 | } |
| 1813 | } |
| 1814 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1815 | return hex; |
| 1816 | } |
| 1817 | |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1818 | int cryptfs_check_footer() |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1819 | { |
| 1820 | int rc = -1; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1821 | struct crypt_mnt_ftr crypt_ftr; |
| 1822 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1823 | rc = get_crypt_ftr_and_key(&crypt_ftr); |
| 1824 | |
| 1825 | return rc; |
| 1826 | } |
| 1827 | |
| 1828 | int cryptfs_check_passwd(char *passwd) |
| 1829 | { |
| 1830 | struct crypt_mnt_ftr crypt_ftr; |
| 1831 | int rc; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1832 | int has_hw_crypto = 0; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1833 | |
| 1834 | rc = check_unmounted_and_get_ftr(&crypt_ftr); |
| 1835 | if (rc) |
| 1836 | return rc; |
| 1837 | |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1838 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1839 | printf("CONFIG_HW_DISK_ENCRYPTION present\n"); |
| 1840 | if (is_hw_fde_enabled() && is_hw_disk_encryption((char*) crypt_ftr.crypto_type_name)) |
| 1841 | has_hw_crypto = 1; |
| 1842 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1843 | |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1844 | //if (passwd) printf("passwd: '%s'\n", passwd); |
| 1845 | char* adjusted_passwd; |
| 1846 | if (!has_hw_crypto) |
| 1847 | adjusted_passwd = adjust_passwd(passwd); |
| 1848 | //if (adjusted_passwd) printf("adjusted_passwd: '%s'\n", adjusted_passwd); |
| 1849 | char* hex_passwd = hexadj_passwd(passwd, has_hw_crypto); |
| 1850 | //if (hex_passwd) printf("hex_passwd: '%s'\n", hex_passwd); |
| 1851 | printf("has_hw_crypto is %i\n", has_hw_crypto); |
| 1852 | if (!has_hw_crypto && adjusted_passwd) { |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1853 | int failed_decrypt_count = crypt_ftr.failed_decrypt_count; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1854 | //printf("trying adjusted password '%s'\n", adjusted_passwd); |
| 1855 | rc = test_mount_encrypted_fs(&crypt_ftr, hex_passwd, |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1856 | DATA_MNT_POINT, "userdata"); |
| 1857 | |
| 1858 | // Maybe the original one still works? |
| 1859 | if (rc) { |
| 1860 | // Don't double count this failure |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1861 | //printf("trying passwd '%s'\n", passwd); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1862 | crypt_ftr.failed_decrypt_count = failed_decrypt_count; |
| 1863 | rc = test_mount_encrypted_fs(&crypt_ftr, passwd, |
| 1864 | DATA_MNT_POINT, "userdata"); |
| 1865 | if (!rc) { |
| 1866 | // cryptfs_changepw also adjusts so pass original |
| 1867 | // Note that adjust_passwd only recognises patterns |
| 1868 | // so we can safely use CRYPT_TYPE_PATTERN |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1869 | printf("TWRP NOT Updating pattern to new format\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1870 | //cryptfs_changepw(CRYPT_TYPE_PATTERN, passwd); |
| 1871 | } else if (hex_passwd) { |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1872 | //printf("trying hex_passwd '%s'\n", hex_passwd); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1873 | rc = test_mount_encrypted_fs(&crypt_ftr, hex_passwd, |
| 1874 | DATA_MNT_POINT, "userdata"); |
| 1875 | } |
| 1876 | } |
| 1877 | free(adjusted_passwd); |
| 1878 | } else { |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1879 | if (hex_passwd) { |
| 1880 | //printf("2trying hex_passwd '%s'\n", hex_passwd); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1881 | rc = test_mount_encrypted_fs(&crypt_ftr, hex_passwd, |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1882 | DATA_MNT_POINT, "userdata"); |
| 1883 | } else { |
| 1884 | rc = 1; |
| 1885 | } |
| 1886 | if (rc && passwd) { |
| 1887 | //printf("2trying passwd '%s'\n", passwd); |
| 1888 | rc = test_mount_encrypted_fs(&crypt_ftr, passwd, |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1889 | DATA_MNT_POINT, "userdata"); |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | if (hex_passwd) |
| 1894 | free(hex_passwd); |
| 1895 | |
| 1896 | /*if (rc == 0 && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) { |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1897 | printf("cryptfs_check_passwd update expiry time?\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1898 | cryptfs_clear_password(); |
| 1899 | password = strdup(passwd); |
| 1900 | struct timespec now; |
| 1901 | clock_gettime(CLOCK_BOOTTIME, &now); |
| 1902 | password_expiry_time = now.tv_sec + password_max_age_seconds; |
| 1903 | }*/ |
| 1904 | |
| 1905 | return rc; |
| 1906 | } |
| 1907 | |
| 1908 | int cryptfs_verify_passwd(char *passwd) |
| 1909 | { |
| 1910 | struct crypt_mnt_ftr crypt_ftr; |
| 1911 | /* Allocate enough space for a 256 bit key, but we may use less */ |
| 1912 | unsigned char decrypted_master_key[32]; |
| 1913 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 1914 | int rc; |
| 1915 | |
| 1916 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1917 | if (strcmp(encrypted_state, "encrypted") ) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1918 | printf("device not encrypted, aborting\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1919 | return -2; |
| 1920 | } |
| 1921 | |
| 1922 | if (!master_key_saved) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1923 | printf("encrypted fs not yet mounted, aborting\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1924 | return -1; |
| 1925 | } |
| 1926 | |
| 1927 | if (!saved_mount_point) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1928 | printf("encrypted fs failed to save mount point, aborting\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1929 | return -1; |
| 1930 | } |
| 1931 | |
| 1932 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 1933 | printf("Error getting crypt footer and key\n"); |
| 1934 | return -1; |
| 1935 | } |
| 1936 | |
| 1937 | if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) { |
| 1938 | /* If the device has no password, then just say the password is valid */ |
| 1939 | rc = 0; |
| 1940 | } else { |
| 1941 | char* adjusted_passwd = adjust_passwd(passwd); |
| 1942 | if (adjusted_passwd) { |
| 1943 | passwd = adjusted_passwd; |
| 1944 | } |
| 1945 | |
| 1946 | decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0); |
| 1947 | if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) { |
| 1948 | /* They match, the password is correct */ |
| 1949 | rc = 0; |
| 1950 | } else { |
| 1951 | /* If incorrect, sleep for a bit to prevent dictionary attacks */ |
| 1952 | sleep(1); |
| 1953 | rc = 1; |
| 1954 | } |
| 1955 | |
| 1956 | free(adjusted_passwd); |
| 1957 | } |
| 1958 | |
| 1959 | return rc; |
| 1960 | } |
| 1961 | |
| 1962 | /* Initialize a crypt_mnt_ftr structure. The keysize is |
| 1963 | * defaulted to 16 bytes, and the filesystem size to 0. |
| 1964 | * Presumably, at a minimum, the caller will update the |
| 1965 | * filesystem size and crypto_type_name after calling this function. |
| 1966 | */ |
| 1967 | static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr) |
| 1968 | { |
| 1969 | off64_t off; |
| 1970 | |
| 1971 | memset(ftr, 0, sizeof(struct crypt_mnt_ftr)); |
| 1972 | ftr->magic = CRYPT_MNT_MAGIC; |
| 1973 | ftr->major_version = CURRENT_MAJOR_VERSION; |
| 1974 | ftr->minor_version = CURRENT_MINOR_VERSION; |
| 1975 | ftr->ftr_size = sizeof(struct crypt_mnt_ftr); |
| 1976 | ftr->keysize = KEY_LEN_BYTES; |
| 1977 | |
| 1978 | switch (keymaster_check_compatibility()) { |
| 1979 | case 1: |
| 1980 | ftr->kdf_type = KDF_SCRYPT_KEYMASTER; |
| 1981 | break; |
| 1982 | |
| 1983 | case 0: |
| 1984 | ftr->kdf_type = KDF_SCRYPT; |
| 1985 | break; |
| 1986 | |
| 1987 | default: |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1988 | printf("keymaster_check_compatibility failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1989 | return -1; |
| 1990 | } |
| 1991 | |
| 1992 | get_device_scrypt_params(ftr); |
| 1993 | |
| 1994 | ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE; |
| 1995 | if (get_crypt_ftr_info(NULL, &off) == 0) { |
| 1996 | ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET; |
| 1997 | ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET + |
| 1998 | ftr->persist_data_size; |
| 1999 | } |
| 2000 | |
| 2001 | return 0; |
| 2002 | } |
| 2003 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 2004 | /* Returns type of the password, default, pattern, pin or password. |
| 2005 | */ |
| 2006 | int cryptfs_get_password_type(void) |
| 2007 | { |
| 2008 | struct crypt_mnt_ftr crypt_ftr; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 2009 | |
| 2010 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 2011 | printf("Error getting crypt footer and key\n"); |
| 2012 | return -1; |
| 2013 | } |
| 2014 | |
| 2015 | if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) { |
| 2016 | return -1; |
| 2017 | } |
| 2018 | |
| 2019 | return crypt_ftr.crypt_type; |
| 2020 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2021 | |
| 2022 | /* |
| 2023 | * Called by vold when it's asked to mount an encrypted external |
| 2024 | * storage volume. The incoming partition has no crypto header/footer, |
| 2025 | * as any metadata is been stored in a separate, small partition. |
| 2026 | * |
| 2027 | * out_crypto_blkdev must be MAXPATHLEN. |
| 2028 | */ |
| 2029 | int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev, |
| 2030 | const unsigned char* key, int keysize, char* out_crypto_blkdev) { |
| 2031 | int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC); |
| 2032 | if (fd == -1) { |
| 2033 | printf("Failed to open %s: %s", real_blkdev, strerror(errno)); |
| 2034 | return -1; |
| 2035 | } |
| 2036 | |
| 2037 | unsigned long nr_sec = 0; |
| 2038 | nr_sec = get_blkdev_size(fd); |
| 2039 | close(fd); |
| 2040 | |
| 2041 | if (nr_sec == 0) { |
| 2042 | printf("Failed to get size of %s: %s", real_blkdev, strerror(errno)); |
| 2043 | return -1; |
| 2044 | } |
| 2045 | |
| 2046 | struct crypt_mnt_ftr ext_crypt_ftr; |
| 2047 | memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr)); |
| 2048 | ext_crypt_ftr.fs_size = nr_sec; |
| 2049 | ext_crypt_ftr.keysize = keysize; |
| 2050 | strcpy((char*) ext_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256"); |
| 2051 | |
| 2052 | return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev, |
| 2053 | out_crypto_blkdev, label); |
| 2054 | } |
| 2055 | |
| 2056 | /* |
| 2057 | * Called by vold when it's asked to unmount an encrypted external |
| 2058 | * storage volume. |
| 2059 | */ |
| 2060 | int cryptfs_revert_ext_volume(const char* label) { |
| 2061 | return delete_crypto_blk_dev((char*) label); |
| 2062 | } |