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 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 77 | #define EXT4_FS 1 |
| 78 | #define F2FS_FS 2 |
| 79 | |
| 80 | #define TABLE_LOAD_RETRIES 10 |
| 81 | |
| 82 | #define RSA_KEY_SIZE 2048 |
| 83 | #define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8) |
| 84 | #define RSA_EXPONENT 0x10001 |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 85 | #define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 86 | |
| 87 | #define RETRY_MOUNT_ATTEMPTS 10 |
| 88 | #define RETRY_MOUNT_DELAY_SECONDS 1 |
| 89 | |
| 90 | char *me = "cryptfs"; |
| 91 | |
| 92 | static unsigned char saved_master_key[KEY_LEN_BYTES]; |
| 93 | static char *saved_mount_point; |
| 94 | static int master_key_saved = 0; |
| 95 | static struct crypt_persist_data *persist_data = NULL; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 96 | static char key_fname[PROPERTY_VALUE_MAX] = ""; |
| 97 | static char real_blkdev[PROPERTY_VALUE_MAX] = ""; |
| 98 | static char file_system[PROPERTY_VALUE_MAX] = ""; |
| 99 | |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 100 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 101 | static int scrypt_keymaster(const char *passwd, const unsigned char *salt, |
| 102 | unsigned char *ikey, void *params); |
| 103 | static void convert_key_to_hex_ascii(const unsigned char *master_key, |
| 104 | unsigned int keysize, char *master_key_ascii); |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 105 | static int get_keymaster_hw_fde_passwd(const char* passwd, unsigned char* newpw, |
| 106 | unsigned char* salt, |
| 107 | const struct crypt_mnt_ftr *ftr) |
| 108 | { |
| 109 | /* if newpw updated, return 0 |
| 110 | * if newpw not updated return -1 |
| 111 | */ |
| 112 | int rc = -1; |
| 113 | |
| 114 | if (should_use_keymaster()) { |
| 115 | if (scrypt_keymaster(passwd, salt, newpw, (void*)ftr)) { |
| 116 | printf("scrypt failed"); |
| 117 | } else { |
| 118 | rc = 0; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return rc; |
| 123 | } |
| 124 | |
| 125 | static int verify_hw_fde_passwd(char *passwd, struct crypt_mnt_ftr* crypt_ftr) |
| 126 | { |
| 127 | unsigned char newpw[32] = {0}; |
| 128 | int key_index; |
| 129 | if (get_keymaster_hw_fde_passwd(passwd, newpw, crypt_ftr->salt, crypt_ftr)) |
| 130 | key_index = set_hw_device_encryption_key(passwd, |
| 131 | (char*) crypt_ftr->crypto_type_name); |
| 132 | else |
| 133 | key_index = set_hw_device_encryption_key((const char*)newpw, |
| 134 | (char*) crypt_ftr->crypto_type_name); |
| 135 | return key_index; |
| 136 | } |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 137 | #endif |
| 138 | |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 139 | void set_partition_data(const char* block_device, const char* key_location, const char* fs) |
| 140 | { |
| 141 | strcpy(key_fname, key_location); |
| 142 | strcpy(real_blkdev, block_device); |
| 143 | strcpy(file_system, fs); |
| 144 | } |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 145 | |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 146 | #ifndef TW_CRYPTO_HAVE_KEYMASTERX |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 147 | static int keymaster_init(keymaster_device_t **keymaster_dev) |
| 148 | { |
| 149 | int rc; |
| 150 | |
| 151 | const hw_module_t* mod; |
| 152 | rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); |
| 153 | if (rc) { |
| 154 | printf("could not find any keystore module\n"); |
| 155 | goto out; |
| 156 | } |
| 157 | |
| 158 | rc = keymaster_open(mod, keymaster_dev); |
| 159 | if (rc) { |
| 160 | printf("could not open keymaster device in %s (%s)\n", |
| 161 | KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); |
| 162 | goto out; |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | |
| 167 | out: |
| 168 | *keymaster_dev = NULL; |
| 169 | return rc; |
| 170 | } |
| 171 | |
| 172 | /* Should we use keymaster? */ |
| 173 | static int keymaster_check_compatibility() |
| 174 | { |
| 175 | keymaster_device_t *keymaster_dev = 0; |
| 176 | int rc = 0; |
| 177 | |
| 178 | if (keymaster_init(&keymaster_dev)) { |
| 179 | printf("Failed to init keymaster\n"); |
| 180 | rc = -1; |
| 181 | goto out; |
| 182 | } |
| 183 | |
| 184 | printf("keymaster version is %d\n", keymaster_dev->common.module->module_api_version); |
| 185 | |
that | ceb7b8e | 2014-12-09 23:15:16 +0100 | [diff] [blame] | 186 | #if (KEYMASTER_HEADER_VERSION >= 3) |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 187 | if (keymaster_dev->common.module->module_api_version |
| 188 | < KEYMASTER_MODULE_API_VERSION_0_3) { |
| 189 | rc = 0; |
| 190 | goto out; |
| 191 | } |
| 192 | |
| 193 | if (keymaster_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE) { |
| 194 | rc = 1; |
| 195 | } |
| 196 | |
that | ceb7b8e | 2014-12-09 23:15:16 +0100 | [diff] [blame] | 197 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 198 | out: |
| 199 | keymaster_close(keymaster_dev); |
| 200 | return rc; |
| 201 | } |
| 202 | |
| 203 | /* Create a new keymaster key and store it in this footer */ |
| 204 | static int keymaster_create_key(struct crypt_mnt_ftr *ftr) |
| 205 | { |
| 206 | uint8_t* key = 0; |
| 207 | keymaster_device_t *keymaster_dev = 0; |
| 208 | |
| 209 | if (keymaster_init(&keymaster_dev)) { |
| 210 | printf("Failed to init keymaster\n"); |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | int rc = 0; |
| 215 | |
| 216 | keymaster_rsa_keygen_params_t params; |
| 217 | memset(¶ms, '\0', sizeof(params)); |
| 218 | params.public_exponent = RSA_EXPONENT; |
| 219 | params.modulus_size = RSA_KEY_SIZE; |
| 220 | |
| 221 | size_t key_size; |
| 222 | if (keymaster_dev->generate_keypair(keymaster_dev, TYPE_RSA, ¶ms, |
| 223 | &key, &key_size)) { |
| 224 | printf("Failed to generate keypair\n"); |
| 225 | rc = -1; |
| 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | if (key_size > KEYMASTER_BLOB_SIZE) { |
| 230 | printf("Keymaster key too large for crypto footer\n"); |
| 231 | rc = -1; |
| 232 | goto out; |
| 233 | } |
| 234 | |
| 235 | memcpy(ftr->keymaster_blob, key, key_size); |
| 236 | ftr->keymaster_blob_size = key_size; |
| 237 | |
| 238 | out: |
| 239 | keymaster_close(keymaster_dev); |
| 240 | free(key); |
| 241 | return rc; |
| 242 | } |
| 243 | |
| 244 | /* This signs the given object using the keymaster key. */ |
| 245 | static int keymaster_sign_object(struct crypt_mnt_ftr *ftr, |
| 246 | const unsigned char *object, |
| 247 | const size_t object_size, |
| 248 | unsigned char **signature, |
| 249 | size_t *signature_size) |
| 250 | { |
| 251 | int rc = 0; |
| 252 | keymaster_device_t *keymaster_dev = 0; |
| 253 | if (keymaster_init(&keymaster_dev)) { |
| 254 | printf("Failed to init keymaster\n"); |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | /* We currently set the digest type to DIGEST_NONE because it's the |
| 259 | * only supported value for keymaster. A similar issue exists with |
| 260 | * PADDING_NONE. Long term both of these should likely change. |
| 261 | */ |
| 262 | keymaster_rsa_sign_params_t params; |
| 263 | params.digest_type = DIGEST_NONE; |
| 264 | params.padding_type = PADDING_NONE; |
| 265 | |
| 266 | unsigned char to_sign[RSA_KEY_SIZE_BYTES]; |
| 267 | size_t to_sign_size = sizeof(to_sign); |
| 268 | memset(to_sign, 0, RSA_KEY_SIZE_BYTES); |
| 269 | |
| 270 | // To sign a message with RSA, the message must satisfy two |
| 271 | // constraints: |
| 272 | // |
| 273 | // 1. The message, when interpreted as a big-endian numeric value, must |
| 274 | // be strictly less than the public modulus of the RSA key. Note |
| 275 | // that because the most significant bit of the public modulus is |
| 276 | // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit |
| 277 | // key), an n-bit message with most significant bit 0 always |
| 278 | // satisfies this requirement. |
| 279 | // |
| 280 | // 2. The message must have the same length in bits as the public |
| 281 | // modulus of the RSA key. This requirement isn't mathematically |
| 282 | // necessary, but is necessary to ensure consistency in |
| 283 | // implementations. |
| 284 | switch (ftr->kdf_type) { |
| 285 | case KDF_SCRYPT_KEYMASTER_UNPADDED: |
| 286 | // This is broken: It produces a message which is shorter than |
| 287 | // the public modulus, failing criterion 2. |
| 288 | memcpy(to_sign, object, object_size); |
| 289 | to_sign_size = object_size; |
| 290 | printf("Signing unpadded object\n"); |
| 291 | break; |
| 292 | case KDF_SCRYPT_KEYMASTER_BADLY_PADDED: |
| 293 | // This is broken: Since the value of object is uniformly |
| 294 | // distributed, it produces a message that is larger than the |
| 295 | // public modulus with probability 0.25. |
| 296 | memcpy(to_sign, object, min(RSA_KEY_SIZE_BYTES, object_size)); |
| 297 | printf("Signing end-padded object\n"); |
| 298 | break; |
| 299 | case KDF_SCRYPT_KEYMASTER: |
| 300 | // This ensures the most significant byte of the signed message |
| 301 | // is zero. We could have zero-padded to the left instead, but |
| 302 | // this approach is slightly more robust against changes in |
| 303 | // object size. However, it's still broken (but not unusably |
| 304 | // so) because we really should be using a proper RSA padding |
| 305 | // function, such as OAEP. |
| 306 | // |
| 307 | // TODO(paullawrence): When keymaster 0.4 is available, change |
| 308 | // this to use the padding options it provides. |
| 309 | memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size)); |
| 310 | printf("Signing safely-padded object\n"); |
| 311 | break; |
| 312 | default: |
| 313 | printf("Unknown KDF type %d\n", ftr->kdf_type); |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | rc = keymaster_dev->sign_data(keymaster_dev, |
| 318 | ¶ms, |
| 319 | ftr->keymaster_blob, |
| 320 | ftr->keymaster_blob_size, |
| 321 | to_sign, |
| 322 | to_sign_size, |
| 323 | signature, |
| 324 | signature_size); |
| 325 | |
| 326 | keymaster_close(keymaster_dev); |
| 327 | return rc; |
| 328 | } |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 329 | #else //#ifndef TW_CRYPTO_HAVE_KEYMASTERX |
| 330 | static int keymaster_init(keymaster0_device_t **keymaster0_dev, |
| 331 | keymaster1_device_t **keymaster1_dev) |
| 332 | { |
| 333 | int rc; |
| 334 | |
| 335 | const hw_module_t* mod; |
| 336 | rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); |
| 337 | if (rc) { |
| 338 | printf("could not find any keystore module\n"); |
| 339 | goto err; |
| 340 | } |
| 341 | |
| 342 | printf("keymaster module name is %s\n", mod->name); |
| 343 | printf("keymaster version is %d\n", mod->module_api_version); |
| 344 | |
| 345 | *keymaster0_dev = NULL; |
| 346 | *keymaster1_dev = NULL; |
| 347 | if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) { |
| 348 | printf("Found keymaster1 module, using keymaster1 API.\n"); |
| 349 | rc = keymaster1_open(mod, keymaster1_dev); |
| 350 | } else { |
| 351 | printf("Found keymaster0 module, using keymaster0 API.\n"); |
| 352 | rc = keymaster0_open(mod, keymaster0_dev); |
| 353 | } |
| 354 | |
| 355 | if (rc) { |
| 356 | printf("could not open keymaster device in %s (%s)\n", |
| 357 | KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); |
| 358 | goto err; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | |
| 363 | err: |
| 364 | *keymaster0_dev = NULL; |
| 365 | *keymaster1_dev = NULL; |
| 366 | return rc; |
| 367 | } |
| 368 | |
| 369 | /* Should we use keymaster? */ |
| 370 | static int keymaster_check_compatibility() |
| 371 | { |
| 372 | keymaster0_device_t *keymaster0_dev = 0; |
| 373 | keymaster1_device_t *keymaster1_dev = 0; |
| 374 | int rc = 0; |
| 375 | |
| 376 | if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) { |
| 377 | printf("Failed to init keymaster\n"); |
| 378 | rc = -1; |
| 379 | goto out; |
| 380 | } |
| 381 | |
| 382 | if (keymaster1_dev) { |
| 383 | rc = 1; |
| 384 | goto out; |
| 385 | } |
| 386 | |
| 387 | // TODO(swillden): Check to see if there's any reason to require v0.3. I think v0.1 and v0.2 |
| 388 | // should work. |
| 389 | if (keymaster0_dev->common.module->module_api_version |
| 390 | < KEYMASTER_MODULE_API_VERSION_0_3) { |
| 391 | rc = 0; |
| 392 | goto out; |
| 393 | } |
| 394 | |
| 395 | if (!(keymaster0_dev->flags & KEYMASTER_SOFTWARE_ONLY) && |
| 396 | (keymaster0_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE)) { |
| 397 | rc = 1; |
| 398 | } |
| 399 | |
| 400 | out: |
| 401 | if (keymaster1_dev) { |
| 402 | keymaster1_close(keymaster1_dev); |
| 403 | } |
| 404 | if (keymaster0_dev) { |
| 405 | keymaster0_close(keymaster0_dev); |
| 406 | } |
| 407 | return rc; |
| 408 | } |
| 409 | |
| 410 | /* Create a new keymaster key and store it in this footer */ |
| 411 | static int keymaster_create_key(struct crypt_mnt_ftr *ftr) |
| 412 | { |
| 413 | uint8_t* key = 0; |
| 414 | keymaster0_device_t *keymaster0_dev = 0; |
| 415 | keymaster1_device_t *keymaster1_dev = 0; |
| 416 | |
| 417 | if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) { |
| 418 | printf("Failed to init keymaster\n"); |
| 419 | return -1; |
| 420 | } |
| 421 | |
| 422 | int rc = 0; |
| 423 | size_t key_size = 0; |
| 424 | if (keymaster1_dev) { |
| 425 | keymaster_key_param_t params[] = { |
| 426 | /* Algorithm & size specifications. Stick with RSA for now. Switch to AES later. */ |
| 427 | keymaster_param_enum(KM_TAG_ALGORITHM, KM_ALGORITHM_RSA), |
| 428 | keymaster_param_int(KM_TAG_KEY_SIZE, RSA_KEY_SIZE), |
| 429 | keymaster_param_long(KM_TAG_RSA_PUBLIC_EXPONENT, RSA_EXPONENT), |
| 430 | |
| 431 | /* The only allowed purpose for this key is signing. */ |
| 432 | keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_SIGN), |
| 433 | |
| 434 | /* Padding & digest specifications. */ |
| 435 | keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE), |
| 436 | keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE), |
| 437 | |
| 438 | /* Require that the key be usable in standalone mode. File system isn't available. */ |
| 439 | keymaster_param_enum(KM_TAG_BLOB_USAGE_REQUIREMENTS, KM_BLOB_STANDALONE), |
| 440 | |
| 441 | /* No auth requirements, because cryptfs is not yet integrated with gatekeeper. */ |
| 442 | keymaster_param_bool(KM_TAG_NO_AUTH_REQUIRED), |
| 443 | |
| 444 | /* Rate-limit key usage attempts, to rate-limit brute force */ |
| 445 | keymaster_param_int(KM_TAG_MIN_SECONDS_BETWEEN_OPS, KEYMASTER_CRYPTFS_RATE_LIMIT), |
| 446 | }; |
| 447 | keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) }; |
| 448 | keymaster_key_blob_t key_blob; |
| 449 | keymaster_error_t error = keymaster1_dev->generate_key(keymaster1_dev, ¶m_set, |
| 450 | &key_blob, |
| 451 | NULL /* characteristics */); |
| 452 | if (error != KM_ERROR_OK) { |
| 453 | printf("Failed to generate keymaster1 key, error %d\n", error); |
| 454 | rc = -1; |
| 455 | goto out; |
| 456 | } |
| 457 | |
| 458 | key = (uint8_t*)key_blob.key_material; |
| 459 | key_size = key_blob.key_material_size; |
| 460 | } |
| 461 | else if (keymaster0_dev) { |
| 462 | keymaster_rsa_keygen_params_t params; |
| 463 | memset(¶ms, '\0', sizeof(params)); |
| 464 | params.public_exponent = RSA_EXPONENT; |
| 465 | params.modulus_size = RSA_KEY_SIZE; |
| 466 | |
| 467 | if (keymaster0_dev->generate_keypair(keymaster0_dev, TYPE_RSA, ¶ms, |
| 468 | &key, &key_size)) { |
| 469 | printf("Failed to generate keypair\n"); |
| 470 | rc = -1; |
| 471 | goto out; |
| 472 | } |
| 473 | } else { |
| 474 | printf("Cryptfs bug: keymaster_init succeeded but didn't initialize a device\n"); |
| 475 | rc = -1; |
| 476 | goto out; |
| 477 | } |
| 478 | |
| 479 | if (key_size > KEYMASTER_BLOB_SIZE) { |
| 480 | printf("Keymaster key too large for crypto footer\n"); |
| 481 | rc = -1; |
| 482 | goto out; |
| 483 | } |
| 484 | |
| 485 | memcpy(ftr->keymaster_blob, key, key_size); |
| 486 | ftr->keymaster_blob_size = key_size; |
| 487 | |
| 488 | out: |
| 489 | if (keymaster0_dev) |
| 490 | keymaster0_close(keymaster0_dev); |
| 491 | if (keymaster1_dev) |
| 492 | keymaster1_close(keymaster1_dev); |
| 493 | free(key); |
| 494 | return rc; |
| 495 | } |
| 496 | |
| 497 | /* This signs the given object using the keymaster key. */ |
| 498 | static int keymaster_sign_object(struct crypt_mnt_ftr *ftr, |
| 499 | const unsigned char *object, |
| 500 | const size_t object_size, |
| 501 | unsigned char **signature, |
| 502 | size_t *signature_size) |
| 503 | { |
| 504 | int rc = 0; |
| 505 | keymaster0_device_t *keymaster0_dev = 0; |
| 506 | keymaster1_device_t *keymaster1_dev = 0; |
| 507 | if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) { |
| 508 | printf("Failed to init keymaster\n"); |
| 509 | rc = -1; |
| 510 | goto out; |
| 511 | } |
| 512 | |
| 513 | unsigned char to_sign[RSA_KEY_SIZE_BYTES]; |
| 514 | size_t to_sign_size = sizeof(to_sign); |
| 515 | memset(to_sign, 0, RSA_KEY_SIZE_BYTES); |
| 516 | |
| 517 | // To sign a message with RSA, the message must satisfy two |
| 518 | // constraints: |
| 519 | // |
| 520 | // 1. The message, when interpreted as a big-endian numeric value, must |
| 521 | // be strictly less than the public modulus of the RSA key. Note |
| 522 | // that because the most significant bit of the public modulus is |
| 523 | // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit |
| 524 | // key), an n-bit message with most significant bit 0 always |
| 525 | // satisfies this requirement. |
| 526 | // |
| 527 | // 2. The message must have the same length in bits as the public |
| 528 | // modulus of the RSA key. This requirement isn't mathematically |
| 529 | // necessary, but is necessary to ensure consistency in |
| 530 | // implementations. |
| 531 | switch (ftr->kdf_type) { |
| 532 | case KDF_SCRYPT_KEYMASTER: |
| 533 | // This ensures the most significant byte of the signed message |
| 534 | // is zero. We could have zero-padded to the left instead, but |
| 535 | // this approach is slightly more robust against changes in |
| 536 | // object size. However, it's still broken (but not unusably |
| 537 | // so) because we really should be using a proper deterministic |
| 538 | // RSA padding function, such as PKCS1. |
| 539 | memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size)); |
| 540 | printf("Signing safely-padded object\n"); |
| 541 | break; |
| 542 | default: |
| 543 | printf("Unknown KDF type %d\n", ftr->kdf_type); |
| 544 | rc = -1; |
| 545 | goto out; |
| 546 | } |
| 547 | |
| 548 | if (keymaster0_dev) { |
| 549 | keymaster_rsa_sign_params_t params; |
| 550 | params.digest_type = DIGEST_NONE; |
| 551 | params.padding_type = PADDING_NONE; |
| 552 | |
| 553 | rc = keymaster0_dev->sign_data(keymaster0_dev, |
| 554 | ¶ms, |
| 555 | ftr->keymaster_blob, |
| 556 | ftr->keymaster_blob_size, |
| 557 | to_sign, |
| 558 | to_sign_size, |
| 559 | signature, |
| 560 | signature_size); |
| 561 | goto out; |
| 562 | } else if (keymaster1_dev) { |
| 563 | keymaster_key_blob_t key = { ftr->keymaster_blob, ftr->keymaster_blob_size }; |
| 564 | keymaster_key_param_t params[] = { |
| 565 | keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE), |
| 566 | keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE), |
| 567 | }; |
| 568 | keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) }; |
| 569 | keymaster_operation_handle_t op_handle; |
| 570 | keymaster_error_t error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key, |
| 571 | ¶m_set, NULL /* out_params */, |
| 572 | &op_handle); |
| 573 | if (error == KM_ERROR_KEY_RATE_LIMIT_EXCEEDED) { |
| 574 | // Key usage has been rate-limited. Wait a bit and try again. |
| 575 | sleep(KEYMASTER_CRYPTFS_RATE_LIMIT); |
| 576 | error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key, |
| 577 | ¶m_set, NULL /* out_params */, |
| 578 | &op_handle); |
| 579 | } |
| 580 | if (error != KM_ERROR_OK) { |
| 581 | printf("Error starting keymaster signature transaction: %d\n", error); |
| 582 | rc = -1; |
| 583 | goto out; |
| 584 | } |
| 585 | |
| 586 | keymaster_blob_t input = { to_sign, to_sign_size }; |
| 587 | size_t input_consumed; |
| 588 | error = keymaster1_dev->update(keymaster1_dev, op_handle, NULL /* in_params */, |
| 589 | &input, &input_consumed, NULL /* out_params */, |
| 590 | NULL /* output */); |
| 591 | if (error != KM_ERROR_OK) { |
| 592 | printf("Error sending data to keymaster signature transaction: %d\n", error); |
| 593 | rc = -1; |
| 594 | goto out; |
| 595 | } |
| 596 | if (input_consumed != to_sign_size) { |
| 597 | // This should never happen. If it does, it's a bug in the keymaster implementation. |
| 598 | printf("Keymaster update() did not consume all data.\n"); |
| 599 | keymaster1_dev->abort(keymaster1_dev, op_handle); |
| 600 | rc = -1; |
| 601 | goto out; |
| 602 | } |
| 603 | |
| 604 | keymaster_blob_t tmp_sig; |
| 605 | error = keymaster1_dev->finish(keymaster1_dev, op_handle, NULL /* in_params */, |
| 606 | NULL /* verify signature */, NULL /* out_params */, |
| 607 | &tmp_sig); |
| 608 | if (error != KM_ERROR_OK) { |
| 609 | printf("Error finishing keymaster signature transaction: %d\n", error); |
| 610 | rc = -1; |
| 611 | goto out; |
| 612 | } |
| 613 | |
| 614 | *signature = (uint8_t*)tmp_sig.data; |
| 615 | *signature_size = tmp_sig.data_length; |
| 616 | } else { |
| 617 | printf("Cryptfs bug: keymaster_init succeded but didn't initialize a device.\n"); |
| 618 | rc = -1; |
| 619 | goto out; |
| 620 | } |
| 621 | |
| 622 | out: |
| 623 | if (keymaster1_dev) |
| 624 | keymaster1_close(keymaster1_dev); |
| 625 | if (keymaster0_dev) |
| 626 | keymaster0_close(keymaster0_dev); |
| 627 | |
| 628 | return rc; |
| 629 | } |
| 630 | #endif //#ifndef TW_CRYPTO_HAVE_KEYMASTERX |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 631 | |
| 632 | /* Store password when userdata is successfully decrypted and mounted. |
| 633 | * Cleared by cryptfs_clear_password |
| 634 | * |
| 635 | * To avoid a double prompt at boot, we need to store the CryptKeeper |
| 636 | * password and pass it to KeyGuard, which uses it to unlock KeyStore. |
| 637 | * Since the entire framework is torn down and rebuilt after encryption, |
| 638 | * we have to use a daemon or similar to store the password. Since vold |
| 639 | * is secured against IPC except from system processes, it seems a reasonable |
| 640 | * place to store this. |
| 641 | * |
| 642 | * password should be cleared once it has been used. |
| 643 | * |
| 644 | * password is aged out after password_max_age_seconds seconds. |
| 645 | */ |
| 646 | static char* password = 0; |
| 647 | static int password_expiry_time = 0; |
| 648 | static const int password_max_age_seconds = 60; |
| 649 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 650 | static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags) |
| 651 | { |
| 652 | memset(io, 0, dataSize); |
| 653 | io->data_size = dataSize; |
| 654 | io->data_start = sizeof(struct dm_ioctl); |
| 655 | io->version[0] = 4; |
| 656 | io->version[1] = 0; |
| 657 | io->version[2] = 0; |
| 658 | io->flags = flags; |
| 659 | if (name) { |
| 660 | strncpy(io->name, name, sizeof(io->name)); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * Gets the default device scrypt parameters for key derivation time tuning. |
| 666 | * The parameters should lead to about one second derivation time for the |
| 667 | * given device. |
| 668 | */ |
| 669 | static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) { |
| 670 | const int default_params[] = SCRYPT_DEFAULTS; |
| 671 | int params[] = SCRYPT_DEFAULTS; |
| 672 | char paramstr[PROPERTY_VALUE_MAX]; |
| 673 | char *token; |
| 674 | char *saveptr; |
| 675 | int i; |
| 676 | |
| 677 | property_get(SCRYPT_PROP, paramstr, ""); |
| 678 | if (paramstr[0] != '\0') { |
| 679 | /* |
| 680 | * The token we're looking for should be three integers separated by |
| 681 | * colons (e.g., "12:8:1"). Scan the property to make sure it matches. |
| 682 | */ |
| 683 | for (i = 0, token = strtok_r(paramstr, ":", &saveptr); |
| 684 | token != NULL && i < 3; |
| 685 | i++, token = strtok_r(NULL, ":", &saveptr)) { |
| 686 | char *endptr; |
| 687 | params[i] = strtol(token, &endptr, 10); |
| 688 | |
| 689 | /* |
| 690 | * Check that there was a valid number and it's 8-bit. If not, |
| 691 | * break out and the end check will take the default values. |
| 692 | */ |
| 693 | if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) { |
| 694 | break; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * If there were not enough tokens or a token was malformed (not an |
| 700 | * integer), it will end up here and the default parameters can be |
| 701 | * taken. |
| 702 | */ |
| 703 | if ((i != 3) || (token != NULL)) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 704 | 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] | 705 | memcpy(params, default_params, sizeof(params)); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | ftr->N_factor = params[0]; |
| 710 | ftr->r_factor = params[1]; |
| 711 | ftr->p_factor = params[2]; |
| 712 | } |
| 713 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 714 | static unsigned int get_blkdev_size(int fd) |
| 715 | { |
nijel8 | 2dd66f4 | 2017-02-18 21:15:41 -0500 | [diff] [blame] | 716 | unsigned long nr_sec; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 717 | |
| 718 | if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) { |
| 719 | nr_sec = 0; |
| 720 | } |
| 721 | |
nijel8 | 2dd66f4 | 2017-02-18 21:15:41 -0500 | [diff] [blame] | 722 | return (unsigned int) nr_sec; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static int get_crypt_ftr_info(char **metadata_fname, off64_t *off) |
| 726 | { |
| 727 | static int cached_data = 0; |
| 728 | static off64_t cached_off = 0; |
| 729 | static char cached_metadata_fname[PROPERTY_VALUE_MAX] = ""; |
| 730 | int fd; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 731 | unsigned int nr_sec; |
| 732 | int rc = -1; |
| 733 | |
| 734 | if (!cached_data) { |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 735 | printf("get_crypt_ftr_info crypto key location: '%s'\n", key_fname); |
| 736 | if (!strcmp(key_fname, KEY_IN_FOOTER)) { |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 737 | if ( (fd = open(real_blkdev, O_RDWR)) < 0) { |
| 738 | printf("Cannot open real block device %s\n", real_blkdev); |
| 739 | return -1; |
| 740 | } |
| 741 | |
| 742 | if ((nr_sec = get_blkdev_size(fd))) { |
| 743 | /* If it's an encrypted Android partition, the last 16 Kbytes contain the |
| 744 | * encryption info footer and key, and plenty of bytes to spare for future |
| 745 | * growth. |
| 746 | */ |
| 747 | strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname)); |
| 748 | cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
| 749 | cached_data = 1; |
| 750 | } else { |
| 751 | printf("Cannot get size of block device %s\n", real_blkdev); |
| 752 | } |
| 753 | close(fd); |
| 754 | } else { |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 755 | strlcpy(cached_metadata_fname, key_fname, sizeof(cached_metadata_fname)); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 756 | cached_off = 0; |
| 757 | cached_data = 1; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if (cached_data) { |
| 762 | if (metadata_fname) { |
| 763 | *metadata_fname = cached_metadata_fname; |
| 764 | } |
| 765 | if (off) { |
| 766 | *off = cached_off; |
| 767 | } |
| 768 | rc = 0; |
| 769 | } |
| 770 | |
| 771 | return rc; |
| 772 | } |
| 773 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 774 | static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr) |
| 775 | { |
| 776 | int fd; |
| 777 | unsigned int nr_sec, cnt; |
| 778 | off64_t starting_off; |
| 779 | int rc = -1; |
| 780 | char *fname = NULL; |
| 781 | struct stat statbuf; |
| 782 | |
| 783 | if (get_crypt_ftr_info(&fname, &starting_off)) { |
| 784 | printf("Unable to get crypt_ftr_info\n"); |
| 785 | return -1; |
| 786 | } |
| 787 | if (fname[0] != '/') { |
| 788 | printf("Unexpected value for crypto key location\n"); |
| 789 | return -1; |
| 790 | } |
| 791 | if ( (fd = open(fname, O_RDWR)) < 0) { |
| 792 | printf("Cannot open footer file %s for get\n", fname); |
| 793 | return -1; |
| 794 | } |
| 795 | |
| 796 | /* Make sure it's 16 Kbytes in length */ |
| 797 | fstat(fd, &statbuf); |
| 798 | if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) { |
| 799 | printf("footer file %s is not the expected size!\n", fname); |
| 800 | goto errout; |
| 801 | } |
| 802 | |
| 803 | /* Seek to the start of the crypt footer */ |
| 804 | if (lseek64(fd, starting_off, SEEK_SET) == -1) { |
| 805 | printf("Cannot seek to real block device footer\n"); |
| 806 | goto errout; |
| 807 | } |
| 808 | |
| 809 | if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 810 | printf("Cannot read real block device footer\n"); |
| 811 | goto errout; |
| 812 | } |
| 813 | |
| 814 | if (crypt_ftr->magic != CRYPT_MNT_MAGIC) { |
| 815 | printf("Bad magic for real block device %s\n", fname); |
| 816 | goto errout; |
| 817 | } |
| 818 | |
| 819 | if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) { |
| 820 | printf("Cannot understand major version %d real block device footer; expected %d\n", |
| 821 | crypt_ftr->major_version, CURRENT_MAJOR_VERSION); |
| 822 | goto errout; |
| 823 | } |
| 824 | |
| 825 | if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) { |
| 826 | printf("Warning: crypto footer minor version %d, expected <= %d, continuing...\n", |
| 827 | crypt_ftr->minor_version, CURRENT_MINOR_VERSION); |
| 828 | } |
| 829 | |
| 830 | /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the |
| 831 | * copy on disk before returning. |
| 832 | */ |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 833 | /*if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) { |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 834 | upgrade_crypt_ftr(fd, crypt_ftr, starting_off); |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 835 | }*/ |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 836 | |
| 837 | /* Success! */ |
| 838 | rc = 0; |
| 839 | |
| 840 | errout: |
| 841 | close(fd); |
| 842 | return rc; |
| 843 | } |
| 844 | |
Ethan Yonker | d3e96ff | 2016-02-17 13:36:43 -0600 | [diff] [blame] | 845 | static int hexdigit (char c) |
| 846 | { |
| 847 | if (c >= '0' && c <= '9') return c - '0'; |
| 848 | c = tolower(c); |
| 849 | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
| 850 | return -1; |
| 851 | } |
| 852 | |
| 853 | static unsigned char* convert_hex_ascii_to_key(const char* master_key_ascii, |
| 854 | unsigned int* out_keysize) |
| 855 | { |
| 856 | unsigned int i; |
| 857 | *out_keysize = 0; |
| 858 | |
| 859 | size_t size = strlen (master_key_ascii); |
| 860 | if (size % 2) { |
| 861 | printf("Trying to convert ascii string of odd length\n"); |
| 862 | return NULL; |
| 863 | } |
| 864 | |
| 865 | unsigned char* master_key = (unsigned char*) malloc(size / 2); |
| 866 | if (master_key == 0) { |
| 867 | printf("Cannot allocate\n"); |
| 868 | return NULL; |
| 869 | } |
| 870 | |
| 871 | for (i = 0; i < size; i += 2) { |
| 872 | int high_nibble = hexdigit (master_key_ascii[i]); |
| 873 | int low_nibble = hexdigit (master_key_ascii[i + 1]); |
| 874 | |
| 875 | if(high_nibble < 0 || low_nibble < 0) { |
| 876 | printf("Invalid hex string\n"); |
| 877 | free (master_key); |
| 878 | return NULL; |
| 879 | } |
| 880 | |
| 881 | master_key[*out_keysize] = high_nibble * 16 + low_nibble; |
| 882 | (*out_keysize)++; |
| 883 | } |
| 884 | |
| 885 | return master_key; |
| 886 | } |
| 887 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 888 | /* Convert a binary key of specified length into an ascii hex string equivalent, |
| 889 | * without the leading 0x and with null termination |
| 890 | */ |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 891 | static void convert_key_to_hex_ascii(const unsigned char *master_key, |
| 892 | unsigned int keysize, char *master_key_ascii) { |
| 893 | unsigned int i, a; |
| 894 | unsigned char nibble; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 895 | |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 896 | for (i=0, a=0; i<keysize; i++, a+=2) { |
| 897 | /* For each byte, write out two ascii hex digits */ |
| 898 | nibble = (master_key[i] >> 4) & 0xf; |
| 899 | master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 900 | |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 901 | nibble = master_key[i] & 0xf; |
| 902 | master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 903 | } |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 904 | |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 905 | /* Add the null termination */ |
| 906 | master_key_ascii[a] = '\0'; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 907 | |
| 908 | } |
| 909 | |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 910 | static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, const unsigned char *master_key, |
| 911 | const char *real_blk_name, const char *name, int fd, |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 912 | char *extra_params) |
| 913 | { |
| 914 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 915 | struct dm_ioctl *io; |
| 916 | struct dm_target_spec *tgt; |
| 917 | char *crypt_params; |
| 918 | char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */ |
| 919 | int i; |
| 920 | |
| 921 | io = (struct dm_ioctl *) buffer; |
| 922 | |
| 923 | /* Load the mapping table for this device */ |
| 924 | tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)]; |
| 925 | |
| 926 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 927 | io->target_count = 1; |
| 928 | tgt->status = 0; |
| 929 | tgt->sector_start = 0; |
| 930 | tgt->length = crypt_ftr->fs_size; |
Captain Throwback | 35df638 | 2016-05-17 11:25:52 -0400 | [diff] [blame] | 931 | crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec); |
| 932 | |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 933 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 934 | if(is_hw_disk_encryption((char*)crypt_ftr->crypto_type_name)) { |
| 935 | strlcpy(tgt->target_type, "req-crypt",DM_MAX_TYPE_NAME); |
| 936 | if (is_ice_enabled()) |
| 937 | convert_key_to_hex_ascii(master_key, sizeof(int), master_key_ascii); |
| 938 | else |
| 939 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 940 | } |
| 941 | else { |
| 942 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 943 | strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME); |
| 944 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 945 | #else |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 946 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 947 | strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME); |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 948 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 949 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 950 | sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name, |
| 951 | master_key_ascii, real_blk_name, extra_params); |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 952 | |
| 953 | printf("%s: target_type = %s\n", __func__, tgt->target_type); |
| 954 | printf("%s: real_blk_name = %s, extra_params = %s\n", __func__, real_blk_name, extra_params); |
| 955 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 956 | crypt_params += strlen(crypt_params) + 1; |
| 957 | crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */ |
| 958 | tgt->next = crypt_params - buffer; |
| 959 | |
| 960 | for (i = 0; i < TABLE_LOAD_RETRIES; i++) { |
| 961 | if (! ioctl(fd, DM_TABLE_LOAD, io)) { |
| 962 | break; |
| 963 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 964 | printf("%i\n", errno); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 965 | usleep(500000); |
| 966 | } |
| 967 | |
| 968 | if (i == TABLE_LOAD_RETRIES) { |
| 969 | /* We failed to load the table, return an error */ |
| 970 | return -1; |
| 971 | } else { |
| 972 | return i + 1; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | |
| 977 | static int get_dm_crypt_version(int fd, const char *name, int *version) |
| 978 | { |
| 979 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 980 | struct dm_ioctl *io; |
| 981 | struct dm_target_versions *v; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 982 | int flag; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 983 | int i; |
| 984 | |
| 985 | io = (struct dm_ioctl *) buffer; |
| 986 | |
| 987 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 988 | |
| 989 | if (ioctl(fd, DM_LIST_VERSIONS, io)) { |
| 990 | return -1; |
| 991 | } |
| 992 | |
| 993 | /* Iterate over the returned versions, looking for name of "crypt". |
| 994 | * When found, get and return the version. |
| 995 | */ |
| 996 | v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)]; |
| 997 | while (v->next) { |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 998 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 999 | if (is_hw_fde_enabled()) { |
| 1000 | flag = (!strcmp(v->name, "crypt") || !strcmp(v->name, "req-crypt")); |
| 1001 | } else { |
| 1002 | flag = (!strcmp(v->name, "crypt")); |
| 1003 | } |
| 1004 | printf("get_dm_crypt_version flag: %i, name: '%s'\n", flag, v->name); |
| 1005 | if (flag) { |
| 1006 | #else |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1007 | if (! strcmp(v->name, "crypt")) { |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1008 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1009 | /* We found the crypt driver, return the version, and get out */ |
| 1010 | version[0] = v->version[0]; |
| 1011 | version[1] = v->version[1]; |
| 1012 | version[2] = v->version[2]; |
| 1013 | return 0; |
| 1014 | } |
| 1015 | v = (struct dm_target_versions *)(((char *)v) + v->next); |
| 1016 | } |
| 1017 | |
| 1018 | return -1; |
| 1019 | } |
| 1020 | |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1021 | static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, const unsigned char *master_key, |
| 1022 | const char *real_blk_name, char *crypto_blk_name, const char *name) |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1023 | { |
| 1024 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1025 | char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */ |
| 1026 | char *crypt_params; |
| 1027 | struct dm_ioctl *io; |
| 1028 | struct dm_target_spec *tgt; |
| 1029 | unsigned int minor; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1030 | int fd=0; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1031 | int i; |
| 1032 | int retval = -1; |
| 1033 | int version[3]; |
| 1034 | char *extra_params; |
| 1035 | int load_count; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1036 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1037 | char encrypted_state[PROPERTY_VALUE_MAX] = {0}; |
| 1038 | char progress[PROPERTY_VALUE_MAX] = {0}; |
| 1039 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1040 | |
| 1041 | if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) { |
| 1042 | printf("Cannot open device-mapper\n"); |
| 1043 | goto errout; |
| 1044 | } |
| 1045 | |
| 1046 | io = (struct dm_ioctl *) buffer; |
| 1047 | |
| 1048 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1049 | if (ioctl(fd, DM_DEV_CREATE, io)) { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1050 | printf("Cannot create dm-crypt device %i\n", errno); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1051 | goto errout; |
| 1052 | } |
| 1053 | |
| 1054 | /* Get the device status, in particular, the name of it's device file */ |
| 1055 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1056 | if (ioctl(fd, DM_DEV_STATUS, io)) { |
| 1057 | printf("Cannot retrieve dm-crypt device status\n"); |
| 1058 | goto errout; |
| 1059 | } |
| 1060 | minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); |
| 1061 | snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor); |
| 1062 | |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1063 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1064 | if(is_hw_disk_encryption((char*)crypt_ftr->crypto_type_name)) { |
| 1065 | /* Set fde_enabled if either FDE completed or in-progress */ |
| 1066 | property_get("ro.crypto.state", encrypted_state, ""); /* FDE completed */ |
| 1067 | property_get("vold.encrypt_progress", progress, ""); /* FDE in progress */ |
| 1068 | if (!strcmp(encrypted_state, "encrypted") || strcmp(progress, "")) { |
| 1069 | if (is_ice_enabled()) |
| 1070 | extra_params = "fde_enabled ice"; |
| 1071 | else |
| 1072 | extra_params = "fde_enabled"; |
| 1073 | } else |
| 1074 | extra_params = "fde_disabled"; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1075 | } else { |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1076 | extra_params = ""; |
| 1077 | if (! get_dm_crypt_version(fd, name, version)) { |
| 1078 | /* Support for allow_discards was added in version 1.11.0 */ |
| 1079 | if ((version[0] >= 2) || |
| 1080 | ((version[0] == 1) && (version[1] >= 11))) { |
| 1081 | extra_params = "1 allow_discards"; |
| 1082 | printf("Enabling support for allow_discards in dmcrypt.\n"); |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1083 | } |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1084 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1085 | } |
| 1086 | #else |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1087 | extra_params = ""; |
| 1088 | if (! get_dm_crypt_version(fd, name, version)) { |
| 1089 | /* Support for allow_discards was added in version 1.11.0 */ |
| 1090 | if ((version[0] >= 2) || |
| 1091 | ((version[0] == 1) && (version[1] >= 11))) { |
| 1092 | extra_params = "1 allow_discards"; |
| 1093 | printf("Enabling support for allow_discards in dmcrypt.\n"); |
| 1094 | } |
| 1095 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1096 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1097 | |
| 1098 | load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, |
| 1099 | fd, extra_params); |
| 1100 | if (load_count < 0) { |
| 1101 | printf("Cannot load dm-crypt mapping table.\n"); |
nkk71 | 5073003 | 2017-04-05 22:14:22 +0300 | [diff] [blame] | 1102 | |
| 1103 | // Remove the dm-crypt device, otherwise it cannot be used later on by other |
| 1104 | // processes (eg vold_decrypt) or further testing/debugging in recovery |
| 1105 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1106 | if (ioctl(fd, DM_DEV_REMOVE, io)) { |
| 1107 | printf("Cannot remove dm-crypt device %i\n", errno); |
| 1108 | } |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1109 | goto errout; |
| 1110 | } else if (load_count > 1) { |
| 1111 | printf("Took %d tries to load dmcrypt table.\n", load_count); |
| 1112 | } |
| 1113 | |
| 1114 | /* Resume this device to activate it */ |
| 1115 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1116 | |
| 1117 | if (ioctl(fd, DM_DEV_SUSPEND, io)) { |
| 1118 | printf("Cannot resume the dm-crypt device\n"); |
nkk71 | 5073003 | 2017-04-05 22:14:22 +0300 | [diff] [blame] | 1119 | |
| 1120 | // Remove the dm-crypt device, otherwise it cannot be used later on by other |
| 1121 | // processes (eg vold_decrypt) or further testing/debugging in recovery |
| 1122 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1123 | if (ioctl(fd, DM_DEV_REMOVE, io)) { |
| 1124 | printf("Cannot remove dm-crypt device %i\n", errno); |
| 1125 | } |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1126 | goto errout; |
| 1127 | } |
| 1128 | |
| 1129 | /* We made it here with no errors. Woot! */ |
| 1130 | retval = 0; |
| 1131 | |
| 1132 | errout: |
| 1133 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 1134 | |
| 1135 | return retval; |
| 1136 | } |
| 1137 | |
Ethan Yonker | d79d9bc | 2014-12-20 15:38:29 -0600 | [diff] [blame] | 1138 | int delete_crypto_blk_dev(char *name) |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1139 | { |
| 1140 | int fd; |
| 1141 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1142 | struct dm_ioctl *io; |
| 1143 | int retval = -1; |
| 1144 | |
| 1145 | if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) { |
| 1146 | printf("Cannot open device-mapper\n"); |
| 1147 | goto errout; |
| 1148 | } |
| 1149 | |
| 1150 | io = (struct dm_ioctl *) buffer; |
| 1151 | |
| 1152 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1153 | if (ioctl(fd, DM_DEV_REMOVE, io)) { |
| 1154 | printf("Cannot remove dm-crypt device\n"); |
| 1155 | goto errout; |
| 1156 | } |
| 1157 | |
| 1158 | /* We made it here with no errors. Woot! */ |
| 1159 | retval = 0; |
| 1160 | |
| 1161 | errout: |
| 1162 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 1163 | |
| 1164 | return retval; |
| 1165 | |
| 1166 | } |
| 1167 | |
| 1168 | static int pbkdf2(const char *passwd, const unsigned char *salt, |
| 1169 | unsigned char *ikey, void *params UNUSED) |
| 1170 | { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1171 | printf("Using pbkdf2 for cryptfs KDF\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1172 | |
| 1173 | /* Turn the password into a key and IV that can decrypt the master key */ |
Ethan Yonker | d3e96ff | 2016-02-17 13:36:43 -0600 | [diff] [blame] | 1174 | unsigned int keysize; |
| 1175 | char* master_key = (char*)convert_hex_ascii_to_key(passwd, &keysize); |
| 1176 | if (!master_key) return -1; |
| 1177 | PKCS5_PBKDF2_HMAC_SHA1(master_key, keysize, salt, SALT_LEN, |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1178 | HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey); |
| 1179 | |
Ethan Yonker | d3e96ff | 2016-02-17 13:36:43 -0600 | [diff] [blame] | 1180 | memset(master_key, 0, keysize); |
| 1181 | free (master_key); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | static int scrypt(const char *passwd, const unsigned char *salt, |
| 1186 | unsigned char *ikey, void *params) |
| 1187 | { |
| 1188 | printf("Using scrypt for cryptfs KDF\n"); |
| 1189 | |
| 1190 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1191 | |
| 1192 | int N = 1 << ftr->N_factor; |
| 1193 | int r = 1 << ftr->r_factor; |
| 1194 | int p = 1 << ftr->p_factor; |
| 1195 | |
| 1196 | /* Turn the password into a key and IV that can decrypt the master key */ |
Ethan Yonker | d3e96ff | 2016-02-17 13:36:43 -0600 | [diff] [blame] | 1197 | unsigned int keysize; |
| 1198 | unsigned char* master_key = convert_hex_ascii_to_key(passwd, &keysize); |
| 1199 | if (!master_key) return -1; |
| 1200 | crypto_scrypt(master_key, keysize, salt, SALT_LEN, N, r, p, ikey, |
| 1201 | KEY_LEN_BYTES + IV_LEN_BYTES); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1202 | |
Ethan Yonker | d3e96ff | 2016-02-17 13:36:43 -0600 | [diff] [blame] | 1203 | memset(master_key, 0, keysize); |
| 1204 | free (master_key); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | static int scrypt_keymaster(const char *passwd, const unsigned char *salt, |
| 1209 | unsigned char *ikey, void *params) |
| 1210 | { |
| 1211 | printf("Using scrypt with keymaster for cryptfs KDF\n"); |
| 1212 | |
| 1213 | int rc; |
| 1214 | unsigned int key_size; |
| 1215 | size_t signature_size; |
| 1216 | unsigned char* signature; |
| 1217 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1218 | |
| 1219 | int N = 1 << ftr->N_factor; |
| 1220 | int r = 1 << ftr->r_factor; |
| 1221 | int p = 1 << ftr->p_factor; |
| 1222 | |
Ethan Yonker | d3e96ff | 2016-02-17 13:36:43 -0600 | [diff] [blame] | 1223 | unsigned char* master_key = convert_hex_ascii_to_key(passwd, &key_size); |
| 1224 | if (!master_key) { |
Ethan Yonker | 9f1f2f7 | 2016-04-21 11:54:00 -0500 | [diff] [blame] | 1225 | printf("Failed to convert passwd from hex, using passwd instead\n"); |
| 1226 | master_key = strdup(passwd); |
Ethan Yonker | d3e96ff | 2016-02-17 13:36:43 -0600 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | rc = crypto_scrypt(master_key, key_size, salt, SALT_LEN, |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1230 | N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES); |
Ethan Yonker | d3e96ff | 2016-02-17 13:36:43 -0600 | [diff] [blame] | 1231 | memset(master_key, 0, key_size); |
| 1232 | free(master_key); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1233 | |
| 1234 | if (rc) { |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1235 | printf("scrypt failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1236 | return -1; |
| 1237 | } |
| 1238 | |
| 1239 | if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES, |
| 1240 | &signature, &signature_size)) { |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1241 | printf("Signing failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1242 | return -1; |
| 1243 | } |
| 1244 | |
| 1245 | rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN, |
| 1246 | N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES); |
| 1247 | free(signature); |
| 1248 | |
| 1249 | if (rc) { |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1250 | printf("scrypt failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1251 | return -1; |
| 1252 | } |
| 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1257 | static int decrypt_master_key_aux(char *passwd, unsigned char *salt, |
| 1258 | unsigned char *encrypted_master_key, |
| 1259 | unsigned char *decrypted_master_key, |
| 1260 | kdf_func kdf, void *kdf_params, |
| 1261 | unsigned char** intermediate_key, |
| 1262 | size_t* intermediate_key_size) |
| 1263 | { |
| 1264 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
| 1265 | EVP_CIPHER_CTX d_ctx; |
| 1266 | int decrypted_len, final_len; |
| 1267 | |
| 1268 | /* Turn the password into an intermediate key and IV that can decrypt the |
| 1269 | master key */ |
| 1270 | if (kdf(passwd, salt, ikey, kdf_params)) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1271 | printf("kdf failed\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1272 | return -1; |
| 1273 | } |
| 1274 | |
| 1275 | /* Initialize the decryption engine */ |
| 1276 | if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) { |
| 1277 | return -1; |
| 1278 | } |
| 1279 | EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */ |
| 1280 | /* Decrypt the master key */ |
| 1281 | if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len, |
| 1282 | encrypted_master_key, KEY_LEN_BYTES)) { |
| 1283 | return -1; |
| 1284 | } |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1285 | #ifndef TW_CRYPTO_HAVE_KEYMASTERX |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1286 | if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1287 | #else |
| 1288 | if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) { |
| 1289 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1290 | return -1; |
| 1291 | } |
| 1292 | |
| 1293 | if (decrypted_len + final_len != KEY_LEN_BYTES) { |
| 1294 | return -1; |
| 1295 | } |
| 1296 | |
| 1297 | /* Copy intermediate key if needed by params */ |
| 1298 | if (intermediate_key && intermediate_key_size) { |
| 1299 | *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES); |
| 1300 | if (intermediate_key) { |
| 1301 | memcpy(*intermediate_key, ikey, KEY_LEN_BYTES); |
| 1302 | *intermediate_key_size = KEY_LEN_BYTES; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params) |
| 1310 | { |
| 1311 | if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER_UNPADDED || |
| 1312 | ftr->kdf_type == KDF_SCRYPT_KEYMASTER_BADLY_PADDED || |
| 1313 | ftr->kdf_type == KDF_SCRYPT_KEYMASTER) { |
| 1314 | *kdf = scrypt_keymaster; |
| 1315 | *kdf_params = ftr; |
| 1316 | } else if (ftr->kdf_type == KDF_SCRYPT) { |
| 1317 | *kdf = scrypt; |
| 1318 | *kdf_params = ftr; |
| 1319 | } else { |
| 1320 | *kdf = pbkdf2; |
| 1321 | *kdf_params = NULL; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | static int decrypt_master_key(char *passwd, unsigned char *decrypted_master_key, |
| 1326 | struct crypt_mnt_ftr *crypt_ftr, |
| 1327 | unsigned char** intermediate_key, |
| 1328 | size_t* intermediate_key_size) |
| 1329 | { |
| 1330 | kdf_func kdf; |
| 1331 | void *kdf_params; |
| 1332 | int ret; |
| 1333 | |
| 1334 | get_kdf_func(crypt_ftr, &kdf, &kdf_params); |
| 1335 | ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key, |
| 1336 | decrypted_master_key, kdf, kdf_params, |
| 1337 | intermediate_key, intermediate_key_size); |
| 1338 | if (ret != 0) { |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 1339 | printf("failure decrypting master key\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | return ret; |
| 1343 | } |
| 1344 | |
James Christopher Adduono | c51bd35 | 2017-01-21 00:17:30 -0500 | [diff] [blame] | 1345 | static int try_mount_multiple_fs(const char *crypto_blkdev, |
| 1346 | const char *mount_point, |
| 1347 | const char *file_system) |
| 1348 | { |
| 1349 | if (!mount(crypto_blkdev, mount_point, file_system, 0, NULL)) |
| 1350 | return 0; |
| 1351 | if (strcmp(file_system, "ext4") && |
| 1352 | !mount(crypto_blkdev, mount_point, "ext4", 0, NULL)) |
| 1353 | return 0; |
| 1354 | if (strcmp(file_system, "f2fs") && |
| 1355 | !mount(crypto_blkdev, mount_point, "f2fs", 0, NULL)) |
| 1356 | return 0; |
| 1357 | return 1; |
| 1358 | } |
| 1359 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1360 | static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr, |
| 1361 | char *passwd, char *mount_point, char *label) |
| 1362 | { |
| 1363 | /* Allocate enough space for a 256 bit key, but we may use less */ |
| 1364 | unsigned char decrypted_master_key[32]; |
| 1365 | char crypto_blkdev[MAXPATHLEN]; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1366 | char tmp_mount_point[64]; |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1367 | int rc = 0; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1368 | kdf_func kdf; |
| 1369 | void *kdf_params; |
| 1370 | int use_keymaster = 0; |
| 1371 | int upgrade = 0; |
| 1372 | unsigned char* intermediate_key = 0; |
| 1373 | size_t intermediate_key_size = 0; |
| 1374 | |
| 1375 | printf("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1376 | |
| 1377 | if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) { |
| 1378 | if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr, |
| 1379 | &intermediate_key, &intermediate_key_size)) { |
| 1380 | printf("Failed to decrypt master key\n"); |
| 1381 | rc = -1; |
| 1382 | goto errout; |
| 1383 | } |
| 1384 | } |
| 1385 | |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1386 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1387 | int key_index = 0; |
| 1388 | if(is_hw_disk_encryption((char*)crypt_ftr->crypto_type_name)) { |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1389 | key_index = verify_hw_fde_passwd(passwd, crypt_ftr); |
| 1390 | |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1391 | if (key_index < 0) { |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1392 | rc = 1; |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1393 | goto errout; |
| 1394 | } |
| 1395 | else { |
| 1396 | if (is_ice_enabled()) { |
| 1397 | if (create_crypto_blk_dev(crypt_ftr, (unsigned char*)&key_index, |
| 1398 | real_blkdev, crypto_blkdev, label)) { |
| 1399 | printf("Error creating decrypted block device"); |
| 1400 | rc = -1; |
| 1401 | goto errout; |
| 1402 | } |
| 1403 | } else { |
| 1404 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, |
| 1405 | real_blkdev, crypto_blkdev, label)) { |
| 1406 | printf("Error creating decrypted block device"); |
| 1407 | rc = -1; |
| 1408 | goto errout; |
| 1409 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1412 | } else { |
| 1413 | /* in case HW FDE is delivered through OTA and device is already encrypted |
| 1414 | * using SW FDE, we should let user continue using SW FDE until userdata is |
| 1415 | * wiped. |
| 1416 | */ |
| 1417 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, |
| 1418 | real_blkdev, crypto_blkdev, label)) { |
| 1419 | printf("Error creating decrypted block device"); |
| 1420 | rc = -1; |
| 1421 | goto errout; |
| 1422 | } |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1423 | } |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1424 | #else |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1425 | // Create crypto block device - all (non fatal) code paths |
| 1426 | // need it |
| 1427 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, |
| 1428 | real_blkdev, crypto_blkdev, label)) { |
| 1429 | printf("Error creating decrypted block device\n"); |
| 1430 | rc = -1; |
| 1431 | goto errout; |
| 1432 | } |
Ethan Yonker | ba95ad1 | 2016-01-18 15:18:15 -0600 | [diff] [blame] | 1433 | #endif |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1434 | |
| 1435 | /* Work out if the problem is the password or the data */ |
| 1436 | unsigned char scrypted_intermediate_key[sizeof(crypt_ftr-> |
| 1437 | scrypted_intermediate_key)]; |
| 1438 | int N = 1 << crypt_ftr->N_factor; |
| 1439 | int r = 1 << crypt_ftr->r_factor; |
| 1440 | int p = 1 << crypt_ftr->p_factor; |
| 1441 | |
| 1442 | rc = crypto_scrypt(intermediate_key, intermediate_key_size, |
| 1443 | crypt_ftr->salt, sizeof(crypt_ftr->salt), |
| 1444 | N, r, p, scrypted_intermediate_key, |
| 1445 | sizeof(scrypted_intermediate_key)); |
| 1446 | |
| 1447 | // Does the key match the crypto footer? |
| 1448 | if (rc == 0 && memcmp(scrypted_intermediate_key, |
| 1449 | crypt_ftr->scrypted_intermediate_key, |
| 1450 | sizeof(scrypted_intermediate_key)) == 0) { |
| 1451 | printf("Password matches\n"); |
| 1452 | rc = 0; |
| 1453 | } else { |
| 1454 | /* Try mounting the file system anyway, just in case the problem's with |
| 1455 | * the footer, not the key. */ |
| 1456 | sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point); |
| 1457 | mkdir(tmp_mount_point, 0755); |
James Christopher Adduono | c51bd35 | 2017-01-21 00:17:30 -0500 | [diff] [blame] | 1458 | if (try_mount_multiple_fs(crypto_blkdev, tmp_mount_point, file_system)) { |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1459 | printf("Error temp mounting decrypted block device '%s'\n", crypto_blkdev); |
| 1460 | delete_crypto_blk_dev(label); |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1461 | rc = 1; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1462 | } else { |
| 1463 | /* Success! */ |
| 1464 | printf("Password did not match but decrypted drive mounted - continue\n"); |
| 1465 | umount(tmp_mount_point); |
| 1466 | rc = 0; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | if (rc == 0) { |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1471 | // Don't increment the failed attempt counter as it doesn't |
| 1472 | // make sense to do so in TWRP |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1473 | |
| 1474 | /* Save the name of the crypto block device |
| 1475 | * so we can mount it when restarting the framework. */ |
| 1476 | property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev); |
| 1477 | |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1478 | // TWRP shouldn't change the stored key |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | errout: |
| 1482 | if (intermediate_key) { |
| 1483 | memset(intermediate_key, 0, intermediate_key_size); |
| 1484 | free(intermediate_key); |
| 1485 | } |
| 1486 | return rc; |
| 1487 | } |
| 1488 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1489 | int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr) |
| 1490 | { |
| 1491 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 1492 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1493 | if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) { |
| 1494 | printf("encrypted fs already validated or not running with encryption," |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1495 | " aborting\n"); |
| 1496 | //return -1; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | if (get_crypt_ftr_and_key(crypt_ftr)) { |
Ethan Yonker | cceebb8 | 2014-11-18 10:17:59 -0600 | [diff] [blame] | 1500 | printf("Error getting crypt footer and key\n"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1501 | return -1; |
| 1502 | } |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1507 | int cryptfs_check_footer() |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1508 | { |
| 1509 | int rc = -1; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1510 | struct crypt_mnt_ftr crypt_ftr; |
| 1511 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1512 | rc = get_crypt_ftr_and_key(&crypt_ftr); |
| 1513 | |
| 1514 | return rc; |
| 1515 | } |
| 1516 | |
| 1517 | int cryptfs_check_passwd(char *passwd) |
| 1518 | { |
| 1519 | struct crypt_mnt_ftr crypt_ftr; |
| 1520 | int rc; |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1521 | |
| 1522 | if (!passwd) { |
| 1523 | printf("cryptfs_check_passwd: passwd is NULL!\n"); |
| 1524 | return -1; |
| 1525 | } |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1526 | |
| 1527 | rc = check_unmounted_and_get_ftr(&crypt_ftr); |
| 1528 | if (rc) |
| 1529 | return rc; |
| 1530 | |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1531 | rc = test_mount_encrypted_fs(&crypt_ftr, passwd, |
| 1532 | DATA_MNT_POINT, "userdata"); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1533 | |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1534 | // try falling back to Lollipop hex passwords |
| 1535 | if (rc) { |
| 1536 | int hex_pass_len = strlen(passwd) * 2 + 1; |
| 1537 | char *hex_passwd = (char *)malloc(hex_pass_len); |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1538 | if (hex_passwd) { |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1539 | convert_key_to_hex_ascii((unsigned char *)passwd, |
| 1540 | strlen(passwd), hex_passwd); |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1541 | rc = test_mount_encrypted_fs(&crypt_ftr, hex_passwd, |
Sultan Qasim Khan | a7e63a2 | 2016-02-12 20:57:15 -0500 | [diff] [blame] | 1542 | DATA_MNT_POINT, "userdata"); |
| 1543 | memset(hex_passwd, 0, hex_pass_len); |
| 1544 | free(hex_passwd); |
Dees Troy | c657cc0 | 2015-01-16 22:48:47 +0000 | [diff] [blame] | 1545 | } |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | return rc; |
| 1549 | } |
| 1550 | |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1551 | /* Returns type of the password, default, pattern, pin or password. |
| 1552 | */ |
| 1553 | int cryptfs_get_password_type(void) |
| 1554 | { |
| 1555 | struct crypt_mnt_ftr crypt_ftr; |
Ethan Yonker | 4eca40d | 2014-11-11 14:52:28 -0600 | [diff] [blame] | 1556 | |
| 1557 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 1558 | printf("Error getting crypt footer and key\n"); |
| 1559 | return -1; |
| 1560 | } |
| 1561 | |
| 1562 | if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) { |
| 1563 | return -1; |
| 1564 | } |
| 1565 | |
| 1566 | return crypt_ftr.crypt_type; |
| 1567 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1568 | |
| 1569 | /* |
| 1570 | * Called by vold when it's asked to mount an encrypted external |
| 1571 | * storage volume. The incoming partition has no crypto header/footer, |
| 1572 | * as any metadata is been stored in a separate, small partition. |
| 1573 | * |
| 1574 | * out_crypto_blkdev must be MAXPATHLEN. |
| 1575 | */ |
| 1576 | int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev, |
| 1577 | const unsigned char* key, int keysize, char* out_crypto_blkdev) { |
| 1578 | int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC); |
| 1579 | if (fd == -1) { |
| 1580 | printf("Failed to open %s: %s", real_blkdev, strerror(errno)); |
| 1581 | return -1; |
| 1582 | } |
| 1583 | |
| 1584 | unsigned long nr_sec = 0; |
| 1585 | nr_sec = get_blkdev_size(fd); |
| 1586 | close(fd); |
| 1587 | |
| 1588 | if (nr_sec == 0) { |
| 1589 | printf("Failed to get size of %s: %s", real_blkdev, strerror(errno)); |
| 1590 | return -1; |
| 1591 | } |
| 1592 | |
| 1593 | struct crypt_mnt_ftr ext_crypt_ftr; |
| 1594 | memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr)); |
| 1595 | ext_crypt_ftr.fs_size = nr_sec; |
| 1596 | ext_crypt_ftr.keysize = keysize; |
| 1597 | strcpy((char*) ext_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256"); |
| 1598 | |
| 1599 | return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev, |
| 1600 | out_crypto_blkdev, label); |
| 1601 | } |
| 1602 | |
| 1603 | /* |
| 1604 | * Called by vold when it's asked to unmount an encrypted external |
| 1605 | * storage volume. |
| 1606 | */ |
| 1607 | int cryptfs_revert_ext_volume(const char* label) { |
| 1608 | return delete_crypto_blk_dev((char*) label); |
| 1609 | } |