Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [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> |
| 24 | #include <sys/wait.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <ctype.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <inttypes.h> |
| 29 | #include <unistd.h> |
| 30 | #include <stdio.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <linux/dm-ioctl.h> |
| 33 | #include <libgen.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <sys/param.h> |
| 36 | #include <string.h> |
| 37 | #include <sys/mount.h> |
| 38 | #include <openssl/evp.h> |
| 39 | #include <openssl/sha.h> |
| 40 | #include <errno.h> |
| 41 | //#include <ext4_utils/ext4_crypt.h> |
| 42 | //#include <ext4_utils/ext4_utils.h> |
| 43 | #include <linux/kdev_t.h> |
| 44 | //#include <fs_mgr.h> |
| 45 | #include <time.h> |
| 46 | #include <math.h> |
| 47 | //#include <selinux/selinux.h> |
| 48 | #include "cryptfs.h" |
| 49 | //#include "secontext.h" |
| 50 | #define LOG_TAG "Cryptfs" |
| 51 | //#include "cutils/log.h" |
| 52 | #include "cutils/properties.h" |
| 53 | //#include "cutils/android_reboot.h" |
| 54 | //#include "hardware_legacy/power.h" |
| 55 | //#include <logwrap/logwrap.h> |
| 56 | //#include "ScryptParameters.h" |
| 57 | //#include "VolumeManager.h" |
| 58 | //#include "VoldUtil.h" |
| 59 | //#include "Ext4Crypt.h" |
| 60 | //#include "f2fs_sparseblock.h" |
| 61 | //#include "EncryptInplace.h" |
| 62 | //#include "Process.h" |
| 63 | #if TW_KEYMASTER_MAX_API == 3 |
| 64 | #include "../ext4crypt/Keymaster3.h" |
| 65 | #endif |
| 66 | #if TW_KEYMASTER_MAX_API == 4 |
| 67 | #include "../ext4crypt/Keymaster4.h" |
| 68 | #endif |
| 69 | #if TW_KEYMASTER_MAX_API == 0 |
| 70 | #include <hardware/keymaster.h> |
| 71 | #else // so far, all trees that have keymaster >= 1 have keymaster 1 support |
| 72 | #include <stdbool.h> |
| 73 | #include <openssl/evp.h> |
| 74 | #include <openssl/sha.h> |
| 75 | #include <hardware/keymaster0.h> |
| 76 | #include <hardware/keymaster1.h> |
| 77 | #endif |
| 78 | //#include "android-base/properties.h" |
| 79 | //#include <bootloader_message/bootloader_message.h> |
| 80 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 81 | #include <cryptfs_hw.h> |
| 82 | #endif |
| 83 | extern "C" { |
| 84 | #include <crypto_scrypt.h> |
| 85 | } |
| 86 | #include <string> |
| 87 | #include <vector> |
| 88 | |
| 89 | #define ALOGE(...) fprintf(stdout, "E:" __VA_ARGS__) |
| 90 | #define SLOGE(...) fprintf(stdout, "E:" __VA_ARGS__) |
| 91 | #define SLOGW(...) fprintf(stdout, "W:" __VA_ARGS__) |
| 92 | #define SLOGI(...) fprintf(stdout, "I:" __VA_ARGS__) |
| 93 | #define SLOGD(...) fprintf(stdout, "D:" __VA_ARGS__) |
| 94 | |
| 95 | #define UNUSED __attribute__((unused)) |
| 96 | |
| 97 | #define DM_CRYPT_BUF_SIZE 4096 |
| 98 | |
| 99 | #define HASH_COUNT 2000 |
| 100 | |
| 101 | #ifndef min /* already defined by windows.h */ |
| 102 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 103 | #endif |
| 104 | |
| 105 | constexpr size_t INTERMEDIATE_KEY_LEN_BYTES = 16; |
| 106 | constexpr size_t INTERMEDIATE_IV_LEN_BYTES = 16; |
| 107 | constexpr size_t INTERMEDIATE_BUF_SIZE = |
| 108 | (INTERMEDIATE_KEY_LEN_BYTES + INTERMEDIATE_IV_LEN_BYTES); |
| 109 | |
| 110 | // SCRYPT_LEN is used by struct crypt_mnt_ftr for its intermediate key. |
| 111 | static_assert(INTERMEDIATE_BUF_SIZE == SCRYPT_LEN, |
| 112 | "Mismatch of intermediate key sizes"); |
| 113 | |
| 114 | #define KEY_IN_FOOTER "footer" |
| 115 | |
| 116 | #define DEFAULT_HEX_PASSWORD "64656661756c745f70617373776f7264" |
| 117 | #define DEFAULT_PASSWORD "default_password" |
| 118 | |
| 119 | #define CRYPTO_BLOCK_DEVICE "userdata" |
| 120 | |
| 121 | #define TABLE_LOAD_RETRIES 10 |
| 122 | |
| 123 | #define RSA_KEY_SIZE 2048 |
| 124 | #define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8) |
| 125 | #define RSA_EXPONENT 0x10001 |
| 126 | #define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second |
| 127 | #define KEY_LEN_BYTES 16 |
| 128 | |
| 129 | #define RETRY_MOUNT_ATTEMPTS 10 |
| 130 | #define RETRY_MOUNT_DELAY_SECONDS 1 |
| 131 | |
| 132 | #define CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE (1) |
| 133 | |
| 134 | static unsigned char saved_master_key[MAX_KEY_LEN]; |
| 135 | static char *saved_mount_point; |
| 136 | static int master_key_saved = 0; |
| 137 | static struct crypt_persist_data *persist_data = NULL; |
| 138 | |
| 139 | static int previous_type; |
| 140 | |
| 141 | static char key_fname[PROPERTY_VALUE_MAX] = ""; |
| 142 | static char real_blkdev[PROPERTY_VALUE_MAX] = ""; |
| 143 | static char file_system[PROPERTY_VALUE_MAX] = ""; |
| 144 | |
| 145 | static void get_blkdev_size(int fd, unsigned long *nr_sec) |
| 146 | { |
| 147 | if ( (ioctl(fd, BLKGETSIZE, nr_sec)) == -1) { |
| 148 | *nr_sec = 0; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | #if TW_KEYMASTER_MAX_API == 0 |
| 153 | static int keymaster_init(keymaster_device_t **keymaster_dev) |
| 154 | { |
| 155 | int rc; |
| 156 | |
| 157 | const hw_module_t* mod; |
| 158 | rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); |
| 159 | if (rc) { |
| 160 | printf("could not find any keystore module\n"); |
| 161 | goto out; |
| 162 | } |
| 163 | |
| 164 | rc = keymaster_open(mod, keymaster_dev); |
| 165 | if (rc) { |
| 166 | printf("could not open keymaster device in %s (%s)\n", |
| 167 | KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); |
| 168 | goto out; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | |
| 173 | out: |
| 174 | *keymaster_dev = NULL; |
| 175 | return rc; |
| 176 | } |
| 177 | #else //TW_KEYMASTER_MAX_API == 0 |
| 178 | static int keymaster_init(keymaster0_device_t **keymaster0_dev, |
| 179 | keymaster1_device_t **keymaster1_dev) |
| 180 | { |
| 181 | int rc; |
| 182 | |
| 183 | const hw_module_t* mod; |
| 184 | rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); |
| 185 | if (rc) { |
| 186 | printf("could not find any keystore module\n"); |
| 187 | goto err; |
| 188 | } |
| 189 | |
| 190 | printf("keymaster module name is %s\n", mod->name); |
| 191 | printf("keymaster version is %d\n", mod->module_api_version); |
| 192 | |
| 193 | *keymaster0_dev = NULL; |
| 194 | *keymaster1_dev = NULL; |
| 195 | if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) { |
| 196 | printf("Found keymaster1 module, using keymaster1 API.\n"); |
| 197 | rc = keymaster1_open(mod, keymaster1_dev); |
| 198 | } else { |
| 199 | printf("Found keymaster0 module, using keymaster0 API.\n"); |
| 200 | rc = keymaster0_open(mod, keymaster0_dev); |
| 201 | } |
| 202 | |
| 203 | if (rc) { |
| 204 | printf("could not open keymaster device in %s (%s)\n", |
| 205 | KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); |
| 206 | goto err; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | |
| 211 | err: |
| 212 | *keymaster0_dev = NULL; |
| 213 | *keymaster1_dev = NULL; |
| 214 | return rc; |
| 215 | } |
| 216 | #endif //TW_KEYMASTER_MAX_API == 0 |
| 217 | |
| 218 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 219 | static int scrypt_keymaster(const char *passwd, const unsigned char *salt, |
| 220 | unsigned char *ikey, void *params); |
| 221 | static void convert_key_to_hex_ascii(const unsigned char *master_key, |
| 222 | unsigned int keysize, char *master_key_ascii); |
| 223 | static int test_mount_hw_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr, |
| 224 | const char *passwd, const char *mount_point, const char *label); |
| 225 | int cryptfs_check_passwd_hw(char *passwd); |
| 226 | int cryptfs_get_master_key(struct crypt_mnt_ftr* ftr, const char* password, |
| 227 | unsigned char* master_key); |
| 228 | |
| 229 | static void convert_key_to_hex_ascii_for_upgrade(const unsigned char *master_key, |
| 230 | unsigned int keysize, char *master_key_ascii) |
| 231 | { |
| 232 | unsigned int i, a; |
| 233 | unsigned char nibble; |
| 234 | |
| 235 | for (i = 0, a = 0; i < keysize; i++, a += 2) { |
| 236 | /* For each byte, write out two ascii hex digits */ |
| 237 | nibble = (master_key[i] >> 4) & 0xf; |
| 238 | master_key_ascii[a] = nibble + (nibble > 9 ? 0x57 : 0x30); |
| 239 | |
| 240 | nibble = master_key[i] & 0xf; |
| 241 | master_key_ascii[a + 1] = nibble + (nibble > 9 ? 0x57 : 0x30); |
| 242 | } |
| 243 | |
| 244 | /* Add the null termination */ |
| 245 | master_key_ascii[a] = '\0'; |
| 246 | } |
| 247 | |
| 248 | static int get_keymaster_hw_fde_passwd(const char* passwd, unsigned char* newpw, |
| 249 | unsigned char* salt, |
| 250 | const struct crypt_mnt_ftr *ftr) |
| 251 | { |
| 252 | /* if newpw updated, return 0 |
| 253 | * if newpw not updated return -1 |
| 254 | */ |
| 255 | int rc = -1; |
| 256 | |
| 257 | if (should_use_keymaster()) { |
| 258 | if (scrypt_keymaster(passwd, salt, newpw, (void*)ftr)) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 259 | SLOGE("scrypt failed"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 260 | } else { |
| 261 | rc = 0; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return rc; |
| 266 | } |
| 267 | |
| 268 | static int verify_hw_fde_passwd(const char *passwd, struct crypt_mnt_ftr* crypt_ftr) |
| 269 | { |
| 270 | unsigned char newpw[32] = {0}; |
| 271 | int key_index; |
| 272 | SLOGI("starting verify_hw_fde_passwd\n"); |
| 273 | if (get_keymaster_hw_fde_passwd(passwd, newpw, crypt_ftr->salt, crypt_ftr)) |
| 274 | key_index = set_hw_device_encryption_key(passwd, |
| 275 | (char*) crypt_ftr->crypto_type_name); |
| 276 | else |
| 277 | key_index = set_hw_device_encryption_key((const char*)newpw, |
| 278 | (char*) crypt_ftr->crypto_type_name); |
| 279 | return key_index; |
| 280 | } |
| 281 | |
| 282 | static int verify_and_update_hw_fde_passwd(const char *passwd, |
| 283 | struct crypt_mnt_ftr* crypt_ftr) |
| 284 | { |
| 285 | char* new_passwd = NULL; |
| 286 | unsigned char newpw[32] = {0}; |
| 287 | int key_index = -1; |
| 288 | int passwd_updated = -1; |
| 289 | int ascii_passwd_updated = (crypt_ftr->flags & CRYPT_ASCII_PASSWORD_UPDATED); |
| 290 | |
| 291 | key_index = verify_hw_fde_passwd(passwd, crypt_ftr); |
| 292 | if (key_index < 0) { |
| 293 | ++crypt_ftr->failed_decrypt_count; |
| 294 | |
| 295 | if (ascii_passwd_updated) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 296 | SLOGI("Ascii password was updated"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 297 | } else { |
| 298 | /* Code in else part would execute only once: |
| 299 | * When device is upgraded from L->M release. |
| 300 | * Once upgraded, code flow should never come here. |
| 301 | * L release passed actual password in hex, so try with hex |
| 302 | * Each nible of passwd was encoded as a byte, so allocate memory |
| 303 | * twice of password len plus one more byte for null termination |
| 304 | */ |
| 305 | if (crypt_ftr->crypt_type == CRYPT_TYPE_DEFAULT) { |
| 306 | new_passwd = (char*)malloc(strlen(DEFAULT_HEX_PASSWORD) + 1); |
| 307 | if (new_passwd == NULL) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 308 | SLOGE("System out of memory. Password verification incomplete"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 309 | goto out; |
| 310 | } |
| 311 | strlcpy(new_passwd, DEFAULT_HEX_PASSWORD, strlen(DEFAULT_HEX_PASSWORD) + 1); |
| 312 | } else { |
| 313 | new_passwd = (char*)malloc(strlen(passwd) * 2 + 1); |
| 314 | if (new_passwd == NULL) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 315 | SLOGE("System out of memory. Password verification incomplete"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 316 | goto out; |
| 317 | } |
| 318 | convert_key_to_hex_ascii_for_upgrade((const unsigned char*)passwd, |
| 319 | strlen(passwd), new_passwd); |
| 320 | } |
| 321 | key_index = set_hw_device_encryption_key((const char*)new_passwd, |
| 322 | (char*) crypt_ftr->crypto_type_name); |
| 323 | if (key_index >=0) { |
| 324 | crypt_ftr->failed_decrypt_count = 0; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 325 | SLOGI("Hex password verified...will try to update with Ascii value"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 326 | /* Before updating password, tie that with keymaster to tie with ROT */ |
| 327 | |
| 328 | if (get_keymaster_hw_fde_passwd(passwd, newpw, |
| 329 | crypt_ftr->salt, crypt_ftr)) { |
| 330 | passwd_updated = update_hw_device_encryption_key(new_passwd, |
| 331 | passwd, (char*)crypt_ftr->crypto_type_name); |
| 332 | } else { |
| 333 | passwd_updated = update_hw_device_encryption_key(new_passwd, |
| 334 | (const char*)newpw, (char*)crypt_ftr->crypto_type_name); |
| 335 | } |
| 336 | |
| 337 | if (passwd_updated >= 0) { |
| 338 | crypt_ftr->flags |= CRYPT_ASCII_PASSWORD_UPDATED; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 339 | SLOGI("Ascii password recorded and updated"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 340 | } else { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 341 | SLOGI("Passwd verified, could not update...Will try next time"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 342 | } |
| 343 | } else { |
| 344 | ++crypt_ftr->failed_decrypt_count; |
| 345 | } |
| 346 | free(new_passwd); |
| 347 | } |
| 348 | } else { |
| 349 | if (!ascii_passwd_updated) |
| 350 | crypt_ftr->flags |= CRYPT_ASCII_PASSWORD_UPDATED; |
| 351 | } |
| 352 | out: |
| 353 | // update footer before leaving |
| 354 | //put_crypt_ftr_and_key(crypt_ftr); |
| 355 | return key_index; |
| 356 | } |
| 357 | #endif |
| 358 | |
| 359 | void set_partition_data(const char* block_device, const char* key_location, const char* fs) |
| 360 | { |
| 361 | strcpy(key_fname, key_location); |
| 362 | strcpy(real_blkdev, block_device); |
| 363 | strcpy(file_system, fs); |
| 364 | } |
| 365 | |
| 366 | /* This signs the given object using the keymaster key. */ |
| 367 | static int keymaster_sign_object(struct crypt_mnt_ftr *ftr, |
| 368 | const unsigned char *object, |
| 369 | const size_t object_size, |
| 370 | unsigned char **signature, |
| 371 | size_t *signature_size) |
| 372 | { |
| 373 | SLOGI("TWRP keymaster max API: %i\n", TW_KEYMASTER_MAX_API); |
| 374 | unsigned char to_sign[RSA_KEY_SIZE_BYTES]; |
| 375 | size_t to_sign_size = sizeof(to_sign); |
| 376 | memset(to_sign, 0, RSA_KEY_SIZE_BYTES); |
| 377 | |
| 378 | // To sign a message with RSA, the message must satisfy two |
| 379 | // constraints: |
| 380 | // |
| 381 | // 1. The message, when interpreted as a big-endian numeric value, must |
| 382 | // be strictly less than the public modulus of the RSA key. Note |
| 383 | // that because the most significant bit of the public modulus is |
| 384 | // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit |
| 385 | // key), an n-bit message with most significant bit 0 always |
| 386 | // satisfies this requirement. |
| 387 | // |
| 388 | // 2. The message must have the same length in bits as the public |
| 389 | // modulus of the RSA key. This requirement isn't mathematically |
| 390 | // necessary, but is necessary to ensure consistency in |
| 391 | // implementations. |
| 392 | switch (ftr->kdf_type) { |
| 393 | case KDF_SCRYPT_KEYMASTER_UNPADDED: |
| 394 | // This is broken: It produces a message which is shorter than |
| 395 | // the public modulus, failing criterion 2. |
| 396 | memcpy(to_sign, object, object_size); |
| 397 | to_sign_size = object_size; |
| 398 | SLOGI("Signing unpadded object\n"); |
| 399 | break; |
| 400 | case KDF_SCRYPT_KEYMASTER_BADLY_PADDED: |
| 401 | // This is broken: Since the value of object is uniformly |
| 402 | // distributed, it produces a message that is larger than the |
| 403 | // public modulus with probability 0.25. |
| 404 | memcpy(to_sign, object, min(RSA_KEY_SIZE_BYTES, object_size)); |
| 405 | SLOGI("Signing end-padded object\n"); |
| 406 | break; |
| 407 | case KDF_SCRYPT_KEYMASTER: |
| 408 | // This ensures the most significant byte of the signed message |
| 409 | // is zero. We could have zero-padded to the left instead, but |
| 410 | // this approach is slightly more robust against changes in |
| 411 | // object size. However, it's still broken (but not unusably |
| 412 | // so) because we really should be using a proper deterministic |
| 413 | // RSA padding function, such as PKCS1. |
| 414 | memcpy(to_sign + 1, object, min((size_t)RSA_KEY_SIZE_BYTES - 1, object_size)); |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 415 | SLOGI("Signing safely-padded object"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 416 | break; |
| 417 | default: |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 418 | SLOGE("Unknown KDF type %d", ftr->kdf_type); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 419 | return -1; |
| 420 | } |
| 421 | |
| 422 | int rc = -1; |
| 423 | |
| 424 | #if TW_KEYMASTER_MAX_API >= 1 |
| 425 | keymaster0_device_t *keymaster0_dev = 0; |
| 426 | keymaster1_device_t *keymaster1_dev = 0; |
| 427 | if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) { |
| 428 | #else |
| 429 | keymaster_device_t *keymaster0_dev = 0; |
| 430 | if (keymaster_init(&keymaster0_dev)) { |
| 431 | #endif |
| 432 | printf("Failed to init keymaster 0/1\n"); |
| 433 | goto initfail; |
| 434 | } |
| 435 | if (keymaster0_dev) { |
| 436 | keymaster_rsa_sign_params_t params; |
| 437 | params.digest_type = DIGEST_NONE; |
| 438 | params.padding_type = PADDING_NONE; |
| 439 | |
| 440 | rc = keymaster0_dev->sign_data(keymaster0_dev, |
| 441 | ¶ms, |
| 442 | ftr->keymaster_blob, |
| 443 | ftr->keymaster_blob_size, |
| 444 | to_sign, |
| 445 | to_sign_size, |
| 446 | signature, |
| 447 | signature_size); |
| 448 | goto out; |
| 449 | } |
| 450 | #if TW_KEYMASTER_MAX_API >= 1 |
| 451 | else if (keymaster1_dev) { |
| 452 | keymaster_key_blob_t key = { ftr->keymaster_blob, ftr->keymaster_blob_size }; |
| 453 | keymaster_key_param_t params[] = { |
| 454 | keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE), |
| 455 | keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE), |
| 456 | }; |
| 457 | keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) }; |
| 458 | keymaster_operation_handle_t op_handle; |
| 459 | keymaster_error_t error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key, |
| 460 | ¶m_set, NULL /* out_params */, |
| 461 | &op_handle); |
| 462 | if (error == KM_ERROR_KEY_RATE_LIMIT_EXCEEDED) { |
| 463 | // Key usage has been rate-limited. Wait a bit and try again. |
| 464 | sleep(KEYMASTER_CRYPTFS_RATE_LIMIT); |
| 465 | error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key, |
| 466 | ¶m_set, NULL /* out_params */, |
| 467 | &op_handle); |
| 468 | } |
| 469 | if (error != KM_ERROR_OK) { |
| 470 | printf("Error starting keymaster signature transaction: %d\n", error); |
| 471 | rc = -1; |
| 472 | goto out; |
| 473 | } |
| 474 | |
| 475 | keymaster_blob_t input = { to_sign, to_sign_size }; |
| 476 | size_t input_consumed; |
| 477 | error = keymaster1_dev->update(keymaster1_dev, op_handle, NULL /* in_params */, |
| 478 | &input, &input_consumed, NULL /* out_params */, |
| 479 | NULL /* output */); |
| 480 | if (error != KM_ERROR_OK) { |
| 481 | printf("Error sending data to keymaster signature transaction: %d\n", error); |
| 482 | rc = -1; |
| 483 | goto out; |
| 484 | } |
| 485 | if (input_consumed != to_sign_size) { |
| 486 | // This should never happen. If it does, it's a bug in the keymaster implementation. |
| 487 | printf("Keymaster update() did not consume all data.\n"); |
| 488 | keymaster1_dev->abort(keymaster1_dev, op_handle); |
| 489 | rc = -1; |
| 490 | goto out; |
| 491 | } |
| 492 | |
| 493 | keymaster_blob_t tmp_sig; |
| 494 | error = keymaster1_dev->finish(keymaster1_dev, op_handle, NULL /* in_params */, |
| 495 | NULL /* verify signature */, NULL /* out_params */, |
| 496 | &tmp_sig); |
| 497 | if (error != KM_ERROR_OK) { |
| 498 | printf("Error finishing keymaster signature transaction: %d\n", error); |
| 499 | rc = -1; |
| 500 | goto out; |
| 501 | } |
| 502 | |
| 503 | *signature = (uint8_t*)tmp_sig.data; |
| 504 | *signature_size = tmp_sig.data_length; |
| 505 | rc = 0; |
| 506 | } |
| 507 | #endif // TW_KEYMASTER_API >= 1 |
| 508 | |
| 509 | out: |
| 510 | #if TW_KEYMASTER_MAX_API >= 1 |
| 511 | if (keymaster1_dev) |
| 512 | keymaster1_close(keymaster1_dev); |
| 513 | #endif |
| 514 | if (keymaster0_dev) |
| 515 | #if TW_KEYMASTER_MAX_API >= 1 |
| 516 | keymaster0_close(keymaster0_dev); |
| 517 | #else |
| 518 | keymaster_close(keymaster0_dev); |
| 519 | #endif |
| 520 | |
| 521 | if (rc == 0) |
| 522 | return 0; // otherwise we'll try for a newer keymaster API |
| 523 | |
| 524 | initfail: |
| 525 | #if TW_KEYMASTER_MAX_API == 3 |
| 526 | return keymaster_sign_object_for_cryptfs_scrypt(ftr->keymaster_blob, ftr->keymaster_blob_size, |
| 527 | KEYMASTER_CRYPTFS_RATE_LIMIT, to_sign, to_sign_size, signature, signature_size, |
| 528 | ftr->keymaster_blob, KEYMASTER_BLOB_SIZE, &ftr->keymaster_blob_size); |
| 529 | #endif //TW_KEYMASTER_MAX_API == 3 |
| 530 | #if TW_KEYMASTER_MAX_API >= 4 |
Mohd Faraz | fb62b18 | 2020-02-11 11:13:32 +0530 | [diff] [blame^] | 531 | for (int c = 1;c <= 20;c++) { // 20 tries are enough for signing keymaster |
| 532 | if (c > 2) |
| 533 | usleep(5000); // if failed in two tries lets rest |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 534 | auto result = keymaster_sign_object_for_cryptfs_scrypt( |
| 535 | ftr->keymaster_blob, ftr->keymaster_blob_size, KEYMASTER_CRYPTFS_RATE_LIMIT, to_sign, |
| 536 | to_sign_size, signature, signature_size); |
| 537 | switch (result) { |
| 538 | case KeymasterSignResult::ok: |
| 539 | return 0; |
| 540 | case KeymasterSignResult::upgrade: |
| 541 | break; |
| 542 | default: |
| 543 | return -1; |
| 544 | } |
| 545 | SLOGD("Upgrading key\n"); |
| 546 | if (keymaster_upgrade_key_for_cryptfs_scrypt( |
| 547 | RSA_KEY_SIZE, RSA_EXPONENT, KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob, |
| 548 | ftr->keymaster_blob_size, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE, |
| 549 | &ftr->keymaster_blob_size) != 0) { |
| 550 | SLOGE("Failed to upgrade key\n"); |
| 551 | return -1; |
| 552 | } |
| 553 | /*if (put_crypt_ftr_and_key(ftr) != 0) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 554 | SLOGE("Failed to write upgraded key to disk"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 555 | }*/ |
| 556 | SLOGD("Key upgraded successfully\n"); |
Mohd Faraz | fb62b18 | 2020-02-11 11:13:32 +0530 | [diff] [blame^] | 557 | } |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 558 | #endif |
| 559 | return -1; |
| 560 | } |
| 561 | |
| 562 | static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags) |
| 563 | { |
| 564 | memset(io, 0, dataSize); |
| 565 | io->data_size = dataSize; |
| 566 | io->data_start = sizeof(struct dm_ioctl); |
| 567 | io->version[0] = 4; |
| 568 | io->version[1] = 0; |
| 569 | io->version[2] = 0; |
| 570 | io->flags = flags; |
| 571 | if (name) { |
| 572 | strlcpy(io->name, name, sizeof(io->name)); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | namespace { |
| 577 | |
| 578 | struct CryptoType; |
| 579 | |
| 580 | // Use to get the CryptoType in use on this device. |
| 581 | const CryptoType &get_crypto_type(); |
| 582 | |
| 583 | struct CryptoType { |
| 584 | // We should only be constructing CryptoTypes as part of |
| 585 | // supported_crypto_types[]. We do it via this pseudo-builder pattern, |
| 586 | // which isn't pure or fully protected as a concession to being able to |
| 587 | // do it all at compile time. Add new CryptoTypes in |
| 588 | // supported_crypto_types[] below. |
| 589 | constexpr CryptoType() : CryptoType(nullptr, nullptr, 0xFFFFFFFF) {} |
| 590 | constexpr CryptoType set_keysize(uint32_t size) const { |
| 591 | return CryptoType(this->property_name, this->crypto_name, size); |
| 592 | } |
| 593 | constexpr CryptoType set_property_name(const char *property) const { |
| 594 | return CryptoType(property, this->crypto_name, this->keysize); |
| 595 | } |
| 596 | constexpr CryptoType set_crypto_name(const char *crypto) const { |
| 597 | return CryptoType(this->property_name, crypto, this->keysize); |
| 598 | } |
| 599 | |
| 600 | constexpr const char *get_property_name() const { return property_name; } |
| 601 | constexpr const char *get_crypto_name() const { return crypto_name; } |
| 602 | constexpr uint32_t get_keysize() const { return keysize; } |
| 603 | |
| 604 | private: |
| 605 | const char *property_name; |
| 606 | const char *crypto_name; |
| 607 | uint32_t keysize; |
| 608 | |
| 609 | constexpr CryptoType(const char *property, const char *crypto, |
| 610 | uint32_t ksize) |
| 611 | : property_name(property), crypto_name(crypto), keysize(ksize) {} |
| 612 | friend const CryptoType &get_crypto_type(); |
| 613 | static const CryptoType &get_device_crypto_algorithm(); |
| 614 | }; |
| 615 | |
| 616 | // We only want to parse this read-only property once. But we need to wait |
| 617 | // until the system is initialized before we can read it. So we use a static |
| 618 | // scoped within this function to get it only once. |
| 619 | const CryptoType &get_crypto_type() { |
| 620 | static CryptoType crypto_type = CryptoType::get_device_crypto_algorithm(); |
| 621 | return crypto_type; |
| 622 | } |
| 623 | |
| 624 | constexpr CryptoType default_crypto_type = CryptoType() |
| 625 | .set_property_name("AES-128-CBC") |
| 626 | .set_crypto_name("aes-cbc-essiv:sha256") |
| 627 | .set_keysize(16); |
| 628 | |
| 629 | constexpr CryptoType supported_crypto_types[] = { |
| 630 | default_crypto_type, |
| 631 | CryptoType() |
| 632 | .set_property_name("Speck128/128-XTS") |
| 633 | .set_crypto_name("speck128-xts-plain64") |
| 634 | .set_keysize(32), |
| 635 | // Add new CryptoTypes here. Order is not important. |
| 636 | }; |
| 637 | |
| 638 | |
| 639 | // ---------- START COMPILE-TIME SANITY CHECK BLOCK ------------------------- |
| 640 | // We confirm all supported_crypto_types have a small enough keysize and |
| 641 | // had both set_property_name() and set_crypto_name() called. |
| 642 | |
| 643 | template <typename T, size_t N> |
| 644 | constexpr size_t array_length(T (&)[N]) { return N; } |
| 645 | |
| 646 | constexpr bool indexOutOfBoundsForCryptoTypes(size_t index) { |
| 647 | return (index >= array_length(supported_crypto_types)); |
| 648 | } |
| 649 | |
| 650 | constexpr bool isValidCryptoType(const CryptoType &crypto_type) { |
| 651 | return ((crypto_type.get_property_name() != nullptr) && |
| 652 | (crypto_type.get_crypto_name() != nullptr) && |
| 653 | (crypto_type.get_keysize() <= MAX_KEY_LEN)); |
| 654 | } |
| 655 | |
| 656 | // Note in C++11 that constexpr functions can only have a single line. |
| 657 | // So our code is a bit convoluted (using recursion instead of a loop), |
| 658 | // but it's asserting at compile time that all of our key lengths are valid. |
| 659 | constexpr bool validateSupportedCryptoTypes(size_t index) { |
| 660 | return indexOutOfBoundsForCryptoTypes(index) || |
| 661 | (isValidCryptoType(supported_crypto_types[index]) && |
| 662 | validateSupportedCryptoTypes(index + 1)); |
| 663 | } |
| 664 | |
| 665 | static_assert(validateSupportedCryptoTypes(0), |
| 666 | "We have a CryptoType with keysize > MAX_KEY_LEN or which was " |
| 667 | "incompletely constructed."); |
| 668 | // ---------- END COMPILE-TIME SANITY CHECK BLOCK ------------------------- |
| 669 | |
| 670 | |
| 671 | // Don't call this directly, use get_crypto_type(), which caches this result. |
| 672 | const CryptoType &CryptoType::get_device_crypto_algorithm() { |
| 673 | constexpr char CRYPT_ALGO_PROP[] = "ro.crypto.fde_algorithm"; |
| 674 | char paramstr[PROPERTY_VALUE_MAX]; |
| 675 | |
| 676 | property_get(CRYPT_ALGO_PROP, paramstr, |
| 677 | default_crypto_type.get_property_name()); |
| 678 | for (auto const &ctype : supported_crypto_types) { |
| 679 | if (strcmp(paramstr, ctype.get_property_name()) == 0) { |
| 680 | return ctype; |
| 681 | } |
| 682 | } |
| 683 | ALOGE("Invalid name (%s) for %s. Defaulting to %s\n", paramstr, |
| 684 | CRYPT_ALGO_PROP, default_crypto_type.get_property_name()); |
| 685 | return default_crypto_type; |
| 686 | } |
| 687 | |
| 688 | } // namespace |
| 689 | |
| 690 | #define SCRYPT_PROP "ro.crypto.scrypt_params" |
| 691 | #define SCRYPT_DEFAULTS "15:3:1" |
| 692 | |
| 693 | bool parse_scrypt_parameters(const char* paramstr, int *Nf, int *rf, int *pf) { |
| 694 | int params[3] = {}; |
| 695 | char *token; |
| 696 | char *saveptr; |
| 697 | int i; |
| 698 | |
| 699 | /* |
| 700 | * The token we're looking for should be three integers separated by |
| 701 | * colons (e.g., "12:8:1"). Scan the property to make sure it matches. |
| 702 | */ |
| 703 | for (i = 0, token = strtok_r(const_cast<char *>(paramstr), ":", &saveptr); |
| 704 | token != nullptr && i < 3; |
| 705 | i++, token = strtok_r(nullptr, ":", &saveptr)) { |
| 706 | char *endptr; |
| 707 | params[i] = strtol(token, &endptr, 10); |
| 708 | |
| 709 | /* |
| 710 | * Check that there was a valid number and it's 8-bit. |
| 711 | */ |
| 712 | if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) { |
| 713 | return false; |
| 714 | } |
| 715 | } |
| 716 | if (token != nullptr) { |
| 717 | return false; |
| 718 | } |
| 719 | *Nf = params[0]; *rf = params[1]; *pf = params[2]; |
| 720 | return true; |
| 721 | } |
| 722 | |
| 723 | uint32_t cryptfs_get_keysize() { |
| 724 | return get_crypto_type().get_keysize(); |
| 725 | } |
| 726 | |
| 727 | const char *cryptfs_get_crypto_name() { |
| 728 | return get_crypto_type().get_crypto_name(); |
| 729 | } |
| 730 | |
| 731 | static int get_crypt_ftr_info(char **metadata_fname, off64_t *off) |
| 732 | { |
| 733 | static int cached_data = 0; |
| 734 | static off64_t cached_off = 0; |
| 735 | static char cached_metadata_fname[PROPERTY_VALUE_MAX] = ""; |
| 736 | int fd; |
| 737 | //char key_loc[PROPERTY_VALUE_MAX]; |
| 738 | //char real_blkdev[PROPERTY_VALUE_MAX]; |
| 739 | int rc = -1; |
| 740 | |
| 741 | if (!cached_data) { |
| 742 | //fs_mgr_get_crypt_info(fstab_default, key_loc, real_blkdev, sizeof(key_loc)); |
| 743 | |
| 744 | if (!strcmp(key_fname, KEY_IN_FOOTER)) { |
| 745 | if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) { |
| 746 | SLOGE("Cannot open real block device %s\n", real_blkdev); |
| 747 | return -1; |
| 748 | } |
| 749 | |
| 750 | unsigned long nr_sec = 0; |
| 751 | get_blkdev_size(fd, &nr_sec); |
| 752 | if (nr_sec != 0) { |
| 753 | /* If it's an encrypted Android partition, the last 16 Kbytes contain the |
| 754 | * encryption info footer and key, and plenty of bytes to spare for future |
| 755 | * growth. |
| 756 | */ |
| 757 | strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname)); |
| 758 | cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
| 759 | cached_data = 1; |
| 760 | } else { |
| 761 | SLOGE("Cannot get size of block device %s\n", real_blkdev); |
| 762 | } |
| 763 | close(fd); |
| 764 | } else { |
| 765 | strlcpy(cached_metadata_fname, key_fname, sizeof(cached_metadata_fname)); |
| 766 | cached_off = 0; |
| 767 | cached_data = 1; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (cached_data) { |
| 772 | if (metadata_fname) { |
| 773 | *metadata_fname = cached_metadata_fname; |
| 774 | } |
| 775 | if (off) { |
| 776 | *off = cached_off; |
| 777 | } |
| 778 | rc = 0; |
| 779 | } |
| 780 | |
| 781 | return rc; |
| 782 | } |
| 783 | |
| 784 | static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr) |
| 785 | { |
| 786 | int fd; |
| 787 | unsigned int cnt; |
| 788 | off64_t starting_off; |
| 789 | int rc = -1; |
| 790 | char *fname = NULL; |
| 791 | struct stat statbuf; |
| 792 | |
| 793 | if (get_crypt_ftr_info(&fname, &starting_off)) { |
| 794 | SLOGE("Unable to get crypt_ftr_info\n"); |
| 795 | return -1; |
| 796 | } |
| 797 | if (fname[0] != '/') { |
| 798 | SLOGE("Unexpected value for crypto key location\n"); |
| 799 | return -1; |
| 800 | } |
| 801 | if ( (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) { |
| 802 | SLOGE("Cannot open footer file %s for get\n", fname); |
| 803 | return -1; |
| 804 | } |
| 805 | |
| 806 | /* Make sure it's 16 Kbytes in length */ |
| 807 | fstat(fd, &statbuf); |
| 808 | if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) { |
| 809 | SLOGE("footer file %s is not the expected size!\n", fname); |
| 810 | goto errout; |
| 811 | } |
| 812 | |
| 813 | /* Seek to the start of the crypt footer */ |
| 814 | if (lseek64(fd, starting_off, SEEK_SET) == -1) { |
| 815 | SLOGE("Cannot seek to real block device footer\n"); |
| 816 | goto errout; |
| 817 | } |
| 818 | |
| 819 | if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 820 | SLOGE("Cannot read real block device footer\n"); |
| 821 | goto errout; |
| 822 | } |
| 823 | |
| 824 | if (crypt_ftr->magic != CRYPT_MNT_MAGIC) { |
| 825 | SLOGE("Bad magic for real block device %s\n", fname); |
| 826 | goto errout; |
| 827 | } |
| 828 | |
| 829 | if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) { |
| 830 | SLOGE("Cannot understand major version %d real block device footer; expected %d\n", |
| 831 | crypt_ftr->major_version, CURRENT_MAJOR_VERSION); |
| 832 | goto errout; |
| 833 | } |
| 834 | |
| 835 | // We risk buffer overflows with oversized keys, so we just reject them. |
| 836 | // 0-sized keys are problematic (essentially by-passing encryption), and |
| 837 | // AES-CBC key wrapping only works for multiples of 16 bytes. |
| 838 | if ((crypt_ftr->keysize == 0) || ((crypt_ftr->keysize % 16) != 0) || |
| 839 | (crypt_ftr->keysize > MAX_KEY_LEN)) { |
| 840 | SLOGE("Invalid keysize (%u) for block device %s; Must be non-zero, " |
| 841 | "divisible by 16, and <= %d\n", crypt_ftr->keysize, fname, |
| 842 | MAX_KEY_LEN); |
| 843 | goto errout; |
| 844 | } |
| 845 | |
| 846 | if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) { |
| 847 | SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n", |
| 848 | crypt_ftr->minor_version, CURRENT_MINOR_VERSION); |
| 849 | } |
| 850 | |
| 851 | /* Success! */ |
| 852 | rc = 0; |
| 853 | |
| 854 | errout: |
| 855 | close(fd); |
| 856 | return rc; |
| 857 | } |
| 858 | |
| 859 | int cryptfs_check_footer() |
| 860 | { |
| 861 | int rc = -1; |
| 862 | struct crypt_mnt_ftr crypt_ftr; |
| 863 | |
| 864 | rc = get_crypt_ftr_and_key(&crypt_ftr); |
| 865 | |
| 866 | return rc; |
| 867 | } |
| 868 | |
| 869 | /* Convert a binary key of specified length into an ascii hex string equivalent, |
| 870 | * without the leading 0x and with null termination |
| 871 | */ |
| 872 | static void convert_key_to_hex_ascii(const unsigned char *master_key, |
| 873 | unsigned int keysize, char *master_key_ascii) { |
| 874 | unsigned int i, a; |
| 875 | unsigned char nibble; |
| 876 | |
| 877 | for (i=0, a=0; i<keysize; i++, a+=2) { |
| 878 | /* For each byte, write out two ascii hex digits */ |
| 879 | nibble = (master_key[i] >> 4) & 0xf; |
| 880 | master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 881 | |
| 882 | nibble = master_key[i] & 0xf; |
| 883 | master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 884 | } |
| 885 | |
| 886 | /* Add the null termination */ |
| 887 | master_key_ascii[a] = '\0'; |
| 888 | |
| 889 | } |
| 890 | |
| 891 | static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, |
| 892 | const unsigned char *master_key, const char *real_blk_name, |
| 893 | const char *name, int fd, const char *extra_params) { |
| 894 | alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE]; |
| 895 | struct dm_ioctl *io; |
| 896 | struct dm_target_spec *tgt; |
| 897 | char *crypt_params; |
| 898 | // We need two ASCII characters to represent each byte, and need space for |
| 899 | // the '\0' terminator. |
| 900 | char master_key_ascii[MAX_KEY_LEN * 2 + 1]; |
| 901 | size_t buff_offset; |
| 902 | int i; |
| 903 | |
| 904 | io = (struct dm_ioctl *) buffer; |
| 905 | |
| 906 | /* Load the mapping table for this device */ |
| 907 | tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)]; |
| 908 | |
| 909 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 910 | io->target_count = 1; |
| 911 | tgt->status = 0; |
| 912 | tgt->sector_start = 0; |
| 913 | tgt->length = crypt_ftr->fs_size; |
| 914 | crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec); |
| 915 | buff_offset = crypt_params - buffer; |
Mohd Faraz | fb62b18 | 2020-02-11 11:13:32 +0530 | [diff] [blame^] | 916 | SLOGI( |
| 917 | "Creating crypto dev \"%s\"; cipher=%s, keysize=%u, real_dev=%s, len=%llu, params=\"%s\"\n", |
| 918 | name, crypt_ftr->crypto_type_name, crypt_ftr->keysize, real_blk_name, tgt->length * 512, |
| 919 | extra_params); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 920 | |
| 921 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 922 | if(is_hw_disk_encryption((char*)crypt_ftr->crypto_type_name)) { |
| 923 | strlcpy(tgt->target_type, "req-crypt",DM_MAX_TYPE_NAME); |
| 924 | if (is_ice_enabled()) |
| 925 | convert_key_to_hex_ascii(master_key, sizeof(int), master_key_ascii); |
| 926 | else |
| 927 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 928 | } |
| 929 | else { |
| 930 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 931 | strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME); |
| 932 | } |
| 933 | snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s 0", |
| 934 | crypt_ftr->crypto_type_name, master_key_ascii, |
| 935 | real_blk_name, extra_params); |
| 936 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 937 | SLOGI("target_type = %s", tgt->target_type); |
| 938 | SLOGI("real_blk_name = %s, extra_params = %s", real_blk_name, extra_params); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 939 | #else |
| 940 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 941 | strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME); |
| 942 | snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s", |
| 943 | crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name, |
| 944 | extra_params); |
| 945 | #endif |
| 946 | |
| 947 | crypt_params += strlen(crypt_params) + 1; |
| 948 | crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */ |
| 949 | tgt->next = crypt_params - buffer; |
| 950 | |
| 951 | for (i = 0; i < TABLE_LOAD_RETRIES; i++) { |
| 952 | if (! ioctl(fd, DM_TABLE_LOAD, io)) { |
| 953 | break; |
| 954 | } |
| 955 | usleep(500000); |
| 956 | } |
| 957 | |
| 958 | if (i == TABLE_LOAD_RETRIES) { |
| 959 | /* We failed to load the table, return an error */ |
| 960 | return -1; |
| 961 | } else { |
| 962 | return i + 1; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | static int get_dm_crypt_version(int fd, const char *name, int *version) |
| 967 | { |
| 968 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 969 | struct dm_ioctl *io; |
| 970 | struct dm_target_versions *v; |
| 971 | |
| 972 | io = (struct dm_ioctl *) buffer; |
| 973 | |
| 974 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 975 | |
| 976 | if (ioctl(fd, DM_LIST_VERSIONS, io)) { |
| 977 | return -1; |
| 978 | } |
| 979 | |
| 980 | /* Iterate over the returned versions, looking for name of "crypt". |
| 981 | * When found, get and return the version. |
| 982 | */ |
| 983 | v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)]; |
| 984 | while (v->next) { |
| 985 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 986 | if (! strcmp(v->name, "crypt") || ! strcmp(v->name, "req-crypt")) { |
| 987 | #else |
| 988 | if (! strcmp(v->name, "crypt")) { |
| 989 | #endif |
| 990 | /* We found the crypt driver, return the version, and get out */ |
| 991 | version[0] = v->version[0]; |
| 992 | version[1] = v->version[1]; |
| 993 | version[2] = v->version[2]; |
| 994 | return 0; |
| 995 | } |
| 996 | v = (struct dm_target_versions *)(((char *)v) + v->next); |
| 997 | } |
| 998 | |
| 999 | return -1; |
| 1000 | } |
| 1001 | |
| 1002 | #ifndef CONFIG_HW_DISK_ENCRYPTION |
| 1003 | static std::string extra_params_as_string(const std::vector<std::string>& extra_params_vec) { |
| 1004 | if (extra_params_vec.empty()) return ""; |
| 1005 | char temp[10]; |
| 1006 | snprintf(temp, sizeof(temp), "%zd", extra_params_vec.size()); |
| 1007 | std::string extra_params = temp; //std::to_string(extra_params_vec.size()); |
| 1008 | for (const auto& p : extra_params_vec) { |
| 1009 | extra_params.append(" "); |
| 1010 | extra_params.append(p); |
| 1011 | } |
| 1012 | return extra_params; |
| 1013 | } |
| 1014 | #endif |
| 1015 | |
| 1016 | static int create_crypto_blk_dev(struct crypt_mnt_ftr* crypt_ftr, const unsigned char* master_key, |
| 1017 | const char* real_blk_name, char* crypto_blk_name, const char* name, |
| 1018 | uint32_t flags) { |
| 1019 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1020 | struct dm_ioctl* io; |
| 1021 | unsigned int minor; |
| 1022 | int fd = 0; |
| 1023 | int err; |
| 1024 | int retval = -1; |
| 1025 | int version[3]; |
| 1026 | int load_count; |
| 1027 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1028 | char encrypted_state[PROPERTY_VALUE_MAX] = {0}; |
| 1029 | char progress[PROPERTY_VALUE_MAX] = {0}; |
| 1030 | const char *extra_params; |
| 1031 | #else |
| 1032 | std::vector<std::string> extra_params_vec; |
| 1033 | #endif |
| 1034 | |
| 1035 | if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) { |
| 1036 | SLOGE("Cannot open device-mapper\n"); |
| 1037 | goto errout; |
| 1038 | } |
| 1039 | |
| 1040 | io = (struct dm_ioctl*)buffer; |
| 1041 | |
| 1042 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1043 | err = ioctl(fd, DM_DEV_CREATE, io); |
| 1044 | if (err) { |
| 1045 | SLOGE("Cannot create dm-crypt device %s: %s\n", name, strerror(errno)); |
| 1046 | goto errout; |
| 1047 | } |
| 1048 | |
| 1049 | /* Get the device status, in particular, the name of it's device file */ |
| 1050 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1051 | if (ioctl(fd, DM_DEV_STATUS, io)) { |
| 1052 | SLOGE("Cannot retrieve dm-crypt device status\n"); |
| 1053 | goto errout; |
| 1054 | } |
| 1055 | minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); |
| 1056 | snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor); |
| 1057 | |
| 1058 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1059 | if(is_hw_disk_encryption((char*)crypt_ftr->crypto_type_name)) { |
| 1060 | /* Set fde_enabled if either FDE completed or in-progress */ |
| 1061 | property_get("ro.crypto.state", encrypted_state, ""); /* FDE completed */ |
| 1062 | property_get("vold.encrypt_progress", progress, ""); /* FDE in progress */ |
| 1063 | if (!strcmp(encrypted_state, "encrypted") || strcmp(progress, "")) { |
| 1064 | if (is_ice_enabled()) { |
| 1065 | if (flags & CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE) |
| 1066 | extra_params = "fde_enabled ice allow_encrypt_override"; |
| 1067 | else |
| 1068 | extra_params = "fde_enabled ice"; |
| 1069 | } else { |
| 1070 | if (flags & CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE) |
| 1071 | extra_params = "fde_enabled allow_encrypt_override"; |
| 1072 | else |
| 1073 | extra_params = "fde_enabled"; |
| 1074 | } |
| 1075 | } else { |
| 1076 | if (flags & CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE) |
| 1077 | extra_params = "fde_enabled allow_encrypt_override"; |
| 1078 | else |
| 1079 | extra_params = "fde_enabled"; |
| 1080 | } |
| 1081 | } else { |
| 1082 | extra_params = ""; |
| 1083 | if (! get_dm_crypt_version(fd, name, version)) { |
| 1084 | /* Support for allow_discards was added in version 1.11.0 */ |
| 1085 | if ((version[0] >= 2) || ((version[0] == 1) && (version[1] >= 11))) { |
| 1086 | if (flags & CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE) |
| 1087 | extra_params = "2 allow_discards allow_encrypt_override"; |
| 1088 | else |
| 1089 | extra_params = "1 allow_discards"; |
| 1090 | SLOGI("Enabling support for allow_discards in dmcrypt.\n"); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, fd, |
| 1095 | extra_params); |
| 1096 | #else |
| 1097 | if (!get_dm_crypt_version(fd, name, version)) { |
| 1098 | /* Support for allow_discards was added in version 1.11.0 */ |
| 1099 | if ((version[0] >= 2) || ((version[0] == 1) && (version[1] >= 11))) { |
| 1100 | extra_params_vec.push_back(std::string("allow_discards")); // Used to be extra_params_vec.emplace_back("allow_discards"); but this won't compile in 5.1 trees |
| 1101 | } |
| 1102 | } |
| 1103 | if (flags & CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE) { |
| 1104 | extra_params_vec.push_back(std::string("allow_encrypt_override")); // Used to be extra_params_vec.emplace_back("allow_encrypt_override"); but this won't compile in 5.1 trees |
| 1105 | } |
| 1106 | load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, fd, |
| 1107 | extra_params_as_string(extra_params_vec).c_str()); |
| 1108 | #endif |
| 1109 | if (load_count < 0) { |
| 1110 | SLOGE("Cannot load dm-crypt mapping table.\n"); |
| 1111 | goto errout; |
| 1112 | } else if (load_count > 1) { |
| 1113 | SLOGI("Took %d tries to load dmcrypt table.\n", load_count); |
| 1114 | } |
| 1115 | |
| 1116 | /* Resume this device to activate it */ |
| 1117 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1118 | |
| 1119 | if (ioctl(fd, DM_DEV_SUSPEND, io)) { |
| 1120 | SLOGE("Cannot resume the dm-crypt device\n"); |
| 1121 | goto errout; |
| 1122 | } |
| 1123 | |
| 1124 | /* We made it here with no errors. Woot! */ |
| 1125 | retval = 0; |
| 1126 | |
| 1127 | errout: |
| 1128 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 1129 | |
| 1130 | return retval; |
| 1131 | } |
| 1132 | |
| 1133 | int delete_crypto_blk_dev(const char *name) |
| 1134 | { |
| 1135 | int fd; |
| 1136 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1137 | struct dm_ioctl *io; |
| 1138 | int retval = -1; |
| 1139 | |
| 1140 | if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) { |
| 1141 | SLOGE("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_REMOVE, io)) { |
| 1149 | SLOGE("Cannot remove dm-crypt device\n"); |
| 1150 | goto errout; |
| 1151 | } |
| 1152 | |
| 1153 | /* We made it here with no errors. Woot! */ |
| 1154 | retval = 0; |
| 1155 | |
| 1156 | errout: |
| 1157 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 1158 | |
| 1159 | return retval; |
| 1160 | |
| 1161 | } |
| 1162 | |
| 1163 | static int pbkdf2(const char *passwd, const unsigned char *salt, |
| 1164 | unsigned char *ikey, void *params UNUSED) |
| 1165 | { |
| 1166 | SLOGI("Using pbkdf2 for cryptfs KDF\n"); |
| 1167 | |
| 1168 | /* Turn the password into a key and IV that can decrypt the master key */ |
| 1169 | return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN, |
| 1170 | HASH_COUNT, INTERMEDIATE_BUF_SIZE, |
| 1171 | ikey) != 1; |
| 1172 | } |
| 1173 | |
| 1174 | static int scrypt(const char *passwd, const unsigned char *salt, |
| 1175 | unsigned char *ikey, void *params) |
| 1176 | { |
| 1177 | SLOGI("Using scrypt for cryptfs KDF\n"); |
| 1178 | |
| 1179 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1180 | |
| 1181 | int N = 1 << ftr->N_factor; |
| 1182 | int r = 1 << ftr->r_factor; |
| 1183 | int p = 1 << ftr->p_factor; |
| 1184 | |
| 1185 | /* Turn the password into a key and IV that can decrypt the master key */ |
| 1186 | crypto_scrypt((const uint8_t*)passwd, strlen(passwd), |
| 1187 | salt, SALT_LEN, N, r, p, ikey, |
| 1188 | INTERMEDIATE_BUF_SIZE); |
| 1189 | |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | static int scrypt_keymaster(const char *passwd, const unsigned char *salt, |
| 1194 | unsigned char *ikey, void *params) |
| 1195 | { |
| 1196 | SLOGI("Using scrypt with keymaster for cryptfs KDF\n"); |
| 1197 | |
| 1198 | int rc; |
| 1199 | size_t signature_size; |
| 1200 | unsigned char* signature; |
| 1201 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1202 | |
| 1203 | int N = 1 << ftr->N_factor; |
| 1204 | int r = 1 << ftr->r_factor; |
| 1205 | int p = 1 << ftr->p_factor; |
| 1206 | |
| 1207 | rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd), |
| 1208 | salt, SALT_LEN, N, r, p, ikey, |
| 1209 | INTERMEDIATE_BUF_SIZE); |
| 1210 | |
| 1211 | if (rc) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1212 | SLOGE("scrypt failed"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1213 | return -1; |
| 1214 | } |
| 1215 | |
| 1216 | if (keymaster_sign_object(ftr, ikey, INTERMEDIATE_BUF_SIZE, |
| 1217 | &signature, &signature_size)) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1218 | SLOGE("Keymaster signing failed"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1219 | return -1; |
| 1220 | } |
| 1221 | |
| 1222 | rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN, |
| 1223 | N, r, p, ikey, INTERMEDIATE_BUF_SIZE); |
| 1224 | free(signature); |
| 1225 | |
| 1226 | if (rc) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1227 | SLOGE("scrypt failed"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1228 | return -1; |
| 1229 | } |
| 1230 | |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | static int decrypt_master_key_aux(const char *passwd, unsigned char *salt, |
| 1235 | const unsigned char *encrypted_master_key, |
| 1236 | size_t keysize, |
| 1237 | unsigned char *decrypted_master_key, |
| 1238 | kdf_func kdf, void *kdf_params, |
| 1239 | unsigned char** intermediate_key, |
| 1240 | size_t* intermediate_key_size) |
| 1241 | { |
| 1242 | unsigned char ikey[INTERMEDIATE_BUF_SIZE] = { 0 }; |
| 1243 | EVP_CIPHER_CTX d_ctx; |
| 1244 | int decrypted_len, final_len; |
| 1245 | |
| 1246 | /* Turn the password into an intermediate key and IV that can decrypt the |
| 1247 | master key */ |
| 1248 | if (kdf(passwd, salt, ikey, kdf_params)) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1249 | SLOGE("kdf failed"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1250 | return -1; |
| 1251 | } |
| 1252 | |
| 1253 | /* Initialize the decryption engine */ |
| 1254 | EVP_CIPHER_CTX_init(&d_ctx); |
| 1255 | if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+INTERMEDIATE_KEY_LEN_BYTES)) { |
| 1256 | return -1; |
| 1257 | } |
| 1258 | EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */ |
| 1259 | /* Decrypt the master key */ |
| 1260 | if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len, |
| 1261 | encrypted_master_key, keysize)) { |
| 1262 | return -1; |
| 1263 | } |
| 1264 | if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) { |
| 1265 | return -1; |
| 1266 | } |
| 1267 | |
| 1268 | if (decrypted_len + final_len != static_cast<int>(keysize)) { |
| 1269 | return -1; |
| 1270 | } |
| 1271 | |
| 1272 | /* Copy intermediate key if needed by params */ |
| 1273 | if (intermediate_key && intermediate_key_size) { |
| 1274 | *intermediate_key = (unsigned char*) malloc(INTERMEDIATE_KEY_LEN_BYTES); |
| 1275 | if (*intermediate_key) { |
| 1276 | memcpy(*intermediate_key, ikey, INTERMEDIATE_KEY_LEN_BYTES); |
| 1277 | *intermediate_key_size = INTERMEDIATE_KEY_LEN_BYTES; |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | EVP_CIPHER_CTX_cleanup(&d_ctx); |
| 1282 | |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params) |
| 1287 | { |
| 1288 | if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) { |
| 1289 | *kdf = scrypt_keymaster; |
| 1290 | *kdf_params = ftr; |
| 1291 | } else if (ftr->kdf_type == KDF_SCRYPT) { |
| 1292 | *kdf = scrypt; |
| 1293 | *kdf_params = ftr; |
| 1294 | } else { |
| 1295 | *kdf = pbkdf2; |
| 1296 | *kdf_params = NULL; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | static int decrypt_master_key(const char *passwd, unsigned char *decrypted_master_key, |
| 1301 | struct crypt_mnt_ftr *crypt_ftr, |
| 1302 | unsigned char** intermediate_key, |
| 1303 | size_t* intermediate_key_size) |
| 1304 | { |
| 1305 | kdf_func kdf; |
| 1306 | void *kdf_params; |
| 1307 | int ret; |
| 1308 | |
| 1309 | get_kdf_func(crypt_ftr, &kdf, &kdf_params); |
| 1310 | ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key, |
| 1311 | crypt_ftr->keysize, |
| 1312 | decrypted_master_key, kdf, kdf_params, |
| 1313 | intermediate_key, intermediate_key_size); |
| 1314 | if (ret != 0) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1315 | SLOGW("failure decrypting master key"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | return ret; |
| 1319 | } |
| 1320 | |
| 1321 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1322 | static int test_mount_hw_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr, |
| 1323 | const char *passwd, const char *mount_point, const char *label) |
| 1324 | { |
| 1325 | /* Allocate enough space for a 256 bit key, but we may use less */ |
| 1326 | unsigned char decrypted_master_key[32]; |
| 1327 | char crypto_blkdev[MAXPATHLEN]; |
| 1328 | //char real_blkdev[MAXPATHLEN]; |
| 1329 | unsigned int orig_failed_decrypt_count; |
| 1330 | int rc = 0; |
| 1331 | |
| 1332 | SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size); |
| 1333 | orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count; |
| 1334 | |
| 1335 | //fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev)); |
| 1336 | |
| 1337 | int key_index = 0; |
| 1338 | if(is_hw_disk_encryption((char*)crypt_ftr->crypto_type_name)) { |
| 1339 | key_index = verify_and_update_hw_fde_passwd(passwd, crypt_ftr); |
| 1340 | if (key_index < 0) { |
| 1341 | rc = -1; |
| 1342 | goto errout; |
| 1343 | } |
| 1344 | else { |
| 1345 | if (is_ice_enabled()) { |
| 1346 | #ifndef CONFIG_HW_DISK_ENCRYPT_PERF |
| 1347 | if (create_crypto_blk_dev(crypt_ftr, (unsigned char*)&key_index, |
| 1348 | real_blkdev, crypto_blkdev, label, 0)) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1349 | SLOGE("Error creating decrypted block device"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1350 | rc = -1; |
| 1351 | goto errout; |
| 1352 | } |
| 1353 | #endif |
| 1354 | } else { |
| 1355 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, |
| 1356 | real_blkdev, crypto_blkdev, label, 0)) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1357 | SLOGE("Error creating decrypted block device"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1358 | rc = -1; |
| 1359 | goto errout; |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | if (rc == 0) { |
| 1366 | /* Save the name of the crypto block device |
| 1367 | * so we can mount it when restarting the framework. */ |
| 1368 | #ifdef CONFIG_HW_DISK_ENCRYPT_PERF |
| 1369 | if (!is_ice_enabled()) |
| 1370 | #endif |
| 1371 | property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev); |
| 1372 | master_key_saved = 1; |
| 1373 | } |
| 1374 | |
| 1375 | errout: |
| 1376 | return rc; |
| 1377 | } |
| 1378 | #endif |
| 1379 | |
| 1380 | static int try_mount_multiple_fs(const char *crypto_blkdev, |
| 1381 | const char *mount_point, |
| 1382 | const char *file_system) |
| 1383 | { |
| 1384 | if (!mount(crypto_blkdev, mount_point, file_system, 0, NULL)) |
| 1385 | return 0; |
| 1386 | if (strcmp(file_system, "ext4") && |
| 1387 | !mount(crypto_blkdev, mount_point, "ext4", 0, NULL)) |
| 1388 | return 0; |
| 1389 | if (strcmp(file_system, "f2fs") && |
| 1390 | !mount(crypto_blkdev, mount_point, "f2fs", 0, NULL)) |
| 1391 | return 0; |
| 1392 | return 1; |
| 1393 | } |
| 1394 | |
| 1395 | static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr, |
| 1396 | const char *passwd, const char *mount_point, const char *label) |
| 1397 | { |
| 1398 | unsigned char decrypted_master_key[MAX_KEY_LEN]; |
| 1399 | char crypto_blkdev[MAXPATHLEN]; |
| 1400 | //char real_blkdev[MAXPATHLEN]; |
| 1401 | char tmp_mount_point[64]; |
| 1402 | unsigned int orig_failed_decrypt_count; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1403 | int rc; |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1404 | int use_keymaster = 0; |
| 1405 | unsigned char* intermediate_key = 0; |
| 1406 | size_t intermediate_key_size = 0; |
| 1407 | int N = 1 << crypt_ftr->N_factor; |
| 1408 | int r = 1 << crypt_ftr->r_factor; |
| 1409 | int p = 1 << crypt_ftr->p_factor; |
| 1410 | |
| 1411 | SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size); |
| 1412 | orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count; |
| 1413 | |
| 1414 | if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) { |
| 1415 | if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr, |
| 1416 | &intermediate_key, &intermediate_key_size)) { |
| 1417 | SLOGE("Failed to decrypt master key\n"); |
| 1418 | rc = -1; |
| 1419 | goto errout; |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | //fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev)); |
| 1424 | |
| 1425 | // Create crypto block device - all (non fatal) code paths |
| 1426 | // need it |
| 1427 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev, label, 0)) { |
| 1428 | SLOGE("Error creating decrypted block device\n"); |
| 1429 | rc = -1; |
| 1430 | goto errout; |
| 1431 | } |
| 1432 | |
| 1433 | /* Work out if the problem is the password or the data */ |
| 1434 | unsigned char scrypted_intermediate_key[sizeof(crypt_ftr-> |
| 1435 | scrypted_intermediate_key)]; |
| 1436 | |
| 1437 | rc = crypto_scrypt(intermediate_key, intermediate_key_size, |
| 1438 | crypt_ftr->salt, sizeof(crypt_ftr->salt), |
| 1439 | N, r, p, scrypted_intermediate_key, |
| 1440 | sizeof(scrypted_intermediate_key)); |
| 1441 | |
| 1442 | // Does the key match the crypto footer? |
| 1443 | if (rc == 0 && memcmp(scrypted_intermediate_key, |
| 1444 | crypt_ftr->scrypted_intermediate_key, |
| 1445 | sizeof(scrypted_intermediate_key)) == 0) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1446 | SLOGI("Password matches"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1447 | rc = 0; |
| 1448 | } else { |
| 1449 | /* Try mounting the file system anyway, just in case the problem's with |
| 1450 | * the footer, not the key. */ |
| 1451 | snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt", |
| 1452 | mount_point); |
| 1453 | mkdir(tmp_mount_point, 0755); |
| 1454 | if (try_mount_multiple_fs(crypto_blkdev, tmp_mount_point, file_system)) { |
| 1455 | SLOGE("Error temp mounting decrypted block device\n"); |
| 1456 | delete_crypto_blk_dev(label); |
| 1457 | |
| 1458 | rc = -1; |
| 1459 | } else { |
| 1460 | /* Success! */ |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1461 | SLOGI("Password did not match but decrypted drive mounted - continue"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1462 | umount(tmp_mount_point); |
| 1463 | rc = 0; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | if (rc == 0) { |
| 1468 | /* Save the name of the crypto block device |
| 1469 | * so we can mount it when restarting the framework. */ |
| 1470 | property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev); |
| 1471 | |
| 1472 | /* Also save a the master key so we can reencrypted the key |
| 1473 | * the key when we want to change the password on it. */ |
| 1474 | memcpy(saved_master_key, decrypted_master_key, crypt_ftr->keysize); |
| 1475 | saved_mount_point = strdup(mount_point); |
| 1476 | master_key_saved = 1; |
| 1477 | SLOGD("%s(): Master key saved\n", __FUNCTION__); |
| 1478 | rc = 0; |
| 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 | |
| 1489 | /* |
| 1490 | * Called by vold when it's asked to mount an encrypted external |
| 1491 | * storage volume. The incoming partition has no crypto header/footer, |
| 1492 | * as any metadata is been stored in a separate, small partition. We |
| 1493 | * assume it must be using our same crypt type and keysize. |
| 1494 | * |
| 1495 | * out_crypto_blkdev must be MAXPATHLEN. |
| 1496 | */ |
| 1497 | int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev, |
| 1498 | const unsigned char* key, int keysize, char* out_crypto_blkdev) { |
| 1499 | int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC); |
| 1500 | if (fd == -1) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1501 | SLOGE("Failed to open %s: %s", real_blkdev, strerror(errno)); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1502 | return -1; |
| 1503 | } |
| 1504 | |
| 1505 | unsigned long nr_sec = 0; |
| 1506 | get_blkdev_size(fd, &nr_sec); |
| 1507 | close(fd); |
| 1508 | |
| 1509 | if (nr_sec == 0) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1510 | SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno)); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1511 | return -1; |
| 1512 | } |
| 1513 | |
| 1514 | struct crypt_mnt_ftr ext_crypt_ftr; |
| 1515 | memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr)); |
| 1516 | ext_crypt_ftr.fs_size = nr_sec; |
| 1517 | ext_crypt_ftr.keysize = cryptfs_get_keysize(); |
| 1518 | strlcpy((char*) ext_crypt_ftr.crypto_type_name, cryptfs_get_crypto_name(), |
| 1519 | MAX_CRYPTO_TYPE_NAME_LEN); |
| 1520 | uint32_t flags = 0; |
| 1521 | /*if (e4crypt_is_native() && |
| 1522 | android::base::GetBoolProperty("ro.crypto.allow_encrypt_override", false)) |
| 1523 | flags |= CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE;*/ |
| 1524 | |
| 1525 | return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev, out_crypto_blkdev, label, flags); |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * Called by vold when it's asked to unmount an encrypted external |
| 1530 | * storage volume. |
| 1531 | */ |
| 1532 | int cryptfs_revert_ext_volume(const char* label) { |
| 1533 | return delete_crypto_blk_dev(label); |
| 1534 | } |
| 1535 | |
| 1536 | int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr) |
| 1537 | { |
| 1538 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 1539 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1540 | if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) { |
| 1541 | SLOGE("encrypted fs already validated or not running with encryption," |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1542 | " aborting"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1543 | return -1; |
| 1544 | } |
| 1545 | |
| 1546 | if (get_crypt_ftr_and_key(crypt_ftr)) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1547 | SLOGE("Error getting crypt footer and key"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1548 | return -1; |
| 1549 | } |
| 1550 | |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
| 1554 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1555 | int cryptfs_check_passwd_hw(const char* passwd) |
| 1556 | { |
| 1557 | struct crypt_mnt_ftr crypt_ftr; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1558 | int rc; |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1559 | unsigned char master_key[KEY_LEN_BYTES]; |
| 1560 | /* get key */ |
| 1561 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1562 | SLOGE("Error getting crypt footer and key"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1563 | return -1; |
| 1564 | } |
| 1565 | |
| 1566 | /* |
| 1567 | * in case of manual encryption (from GUI), the encryption is done with |
| 1568 | * default password |
| 1569 | */ |
| 1570 | if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) { |
| 1571 | /* compare scrypted_intermediate_key with stored scrypted_intermediate_key |
| 1572 | * which was created with actual password before reboot. |
| 1573 | */ |
| 1574 | rc = cryptfs_get_master_key(&crypt_ftr, passwd, master_key); |
| 1575 | if (rc) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1576 | SLOGE("password doesn't match"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1577 | return rc; |
| 1578 | } |
| 1579 | |
| 1580 | rc = test_mount_hw_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD, |
| 1581 | DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE); |
| 1582 | |
| 1583 | if (rc) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1584 | SLOGE("Default password did not match on reboot encryption"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1585 | return rc; |
| 1586 | } |
| 1587 | } else { |
| 1588 | rc = test_mount_hw_encrypted_fs(&crypt_ftr, passwd, |
| 1589 | DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE); |
| 1590 | SLOGE("test mount returned %i\n", rc); |
| 1591 | } |
| 1592 | |
| 1593 | return rc; |
| 1594 | } |
| 1595 | #endif |
| 1596 | |
| 1597 | int cryptfs_check_passwd(const char *passwd) |
| 1598 | { |
| 1599 | /*if (e4crypt_is_native()) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1600 | SLOGE("cryptfs_check_passwd not valid for file encryption"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1601 | return -1; |
| 1602 | }*/ |
| 1603 | |
| 1604 | struct crypt_mnt_ftr crypt_ftr; |
| 1605 | int rc; |
| 1606 | |
| 1607 | rc = check_unmounted_and_get_ftr(&crypt_ftr); |
| 1608 | if (rc) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1609 | SLOGE("Could not get footer"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1610 | return rc; |
| 1611 | } |
| 1612 | |
| 1613 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1614 | if (is_hw_disk_encryption((char*)crypt_ftr.crypto_type_name)) |
| 1615 | return cryptfs_check_passwd_hw(passwd); |
| 1616 | #endif |
| 1617 | |
| 1618 | rc = test_mount_encrypted_fs(&crypt_ftr, passwd, |
| 1619 | DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE); |
| 1620 | |
| 1621 | if (rc) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1622 | SLOGE("Password did not match"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1623 | return rc; |
| 1624 | } |
| 1625 | |
| 1626 | if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) { |
| 1627 | // Here we have a default actual password but a real password |
| 1628 | // we must test against the scrypted value |
| 1629 | // First, we must delete the crypto block device that |
| 1630 | // test_mount_encrypted_fs leaves behind as a side effect |
| 1631 | delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE); |
| 1632 | rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD, |
| 1633 | DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE); |
| 1634 | if (rc) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1635 | SLOGE("Default password did not match on reboot encryption"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1636 | return rc; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | return rc; |
| 1641 | } |
| 1642 | |
| 1643 | int cryptfs_verify_passwd(const char *passwd) |
| 1644 | { |
| 1645 | struct crypt_mnt_ftr crypt_ftr; |
| 1646 | unsigned char decrypted_master_key[MAX_KEY_LEN]; |
| 1647 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 1648 | int rc; |
| 1649 | |
| 1650 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1651 | if (strcmp(encrypted_state, "encrypted") ) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1652 | SLOGE("device not encrypted, aborting"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1653 | return -2; |
| 1654 | } |
| 1655 | |
| 1656 | if (!master_key_saved) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1657 | SLOGE("encrypted fs not yet mounted, aborting"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1658 | return -1; |
| 1659 | } |
| 1660 | |
| 1661 | if (!saved_mount_point) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1662 | SLOGE("encrypted fs failed to save mount point, aborting"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1663 | return -1; |
| 1664 | } |
| 1665 | |
| 1666 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 1667 | SLOGE("Error getting crypt footer and key\n"); |
| 1668 | return -1; |
| 1669 | } |
| 1670 | |
| 1671 | if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) { |
| 1672 | /* If the device has no password, then just say the password is valid */ |
| 1673 | rc = 0; |
| 1674 | } else { |
| 1675 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 1676 | if(is_hw_disk_encryption((char*)crypt_ftr.crypto_type_name)) { |
| 1677 | if (verify_hw_fde_passwd(passwd, &crypt_ftr) >= 0) |
| 1678 | rc = 0; |
| 1679 | else |
| 1680 | rc = -1; |
| 1681 | } else { |
| 1682 | decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0); |
| 1683 | if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) { |
| 1684 | /* They match, the password is correct */ |
| 1685 | rc = 0; |
| 1686 | } else { |
| 1687 | /* If incorrect, sleep for a bit to prevent dictionary attacks */ |
| 1688 | sleep(1); |
| 1689 | rc = 1; |
| 1690 | } |
| 1691 | } |
| 1692 | #else |
| 1693 | decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0); |
| 1694 | if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) { |
| 1695 | /* They match, the password is correct */ |
| 1696 | rc = 0; |
| 1697 | } else { |
| 1698 | /* If incorrect, sleep for a bit to prevent dictionary attacks */ |
| 1699 | sleep(1); |
| 1700 | rc = 1; |
| 1701 | } |
| 1702 | #endif |
| 1703 | } |
| 1704 | |
| 1705 | return rc; |
| 1706 | } |
| 1707 | |
| 1708 | /* Returns type of the password, default, pattern, pin or password. |
| 1709 | */ |
| 1710 | int cryptfs_get_password_type(void) |
| 1711 | { |
| 1712 | struct crypt_mnt_ftr crypt_ftr; |
| 1713 | |
| 1714 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 1715 | SLOGE("Error getting crypt footer and key\n"); |
| 1716 | return -1; |
| 1717 | } |
| 1718 | |
| 1719 | if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) { |
| 1720 | return -1; |
| 1721 | } |
| 1722 | |
| 1723 | return crypt_ftr.crypt_type; |
| 1724 | } |
| 1725 | |
| 1726 | int cryptfs_get_master_key(struct crypt_mnt_ftr* ftr, const char* password, |
| 1727 | unsigned char* master_key) |
| 1728 | { |
| 1729 | int rc; |
| 1730 | |
| 1731 | unsigned char* intermediate_key = 0; |
| 1732 | size_t intermediate_key_size = 0; |
| 1733 | |
| 1734 | if (password == 0 || *password == 0) { |
| 1735 | password = DEFAULT_PASSWORD; |
| 1736 | } |
| 1737 | |
| 1738 | rc = decrypt_master_key(password, master_key, ftr, &intermediate_key, |
| 1739 | &intermediate_key_size); |
| 1740 | |
| 1741 | if (rc) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1742 | SLOGE("Can't calculate intermediate key"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1743 | return rc; |
| 1744 | } |
| 1745 | |
| 1746 | int N = 1 << ftr->N_factor; |
| 1747 | int r = 1 << ftr->r_factor; |
| 1748 | int p = 1 << ftr->p_factor; |
| 1749 | |
| 1750 | unsigned char scrypted_intermediate_key[sizeof(ftr->scrypted_intermediate_key)]; |
| 1751 | |
| 1752 | rc = crypto_scrypt(intermediate_key, intermediate_key_size, |
| 1753 | ftr->salt, sizeof(ftr->salt), N, r, p, |
| 1754 | scrypted_intermediate_key, |
| 1755 | sizeof(scrypted_intermediate_key)); |
| 1756 | |
| 1757 | free(intermediate_key); |
| 1758 | |
| 1759 | if (rc) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 1760 | SLOGE("Can't scrypt intermediate key"); |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 1761 | return rc; |
| 1762 | } |
| 1763 | |
| 1764 | return memcmp(scrypted_intermediate_key, ftr->scrypted_intermediate_key, |
| 1765 | intermediate_key_size); |
| 1766 | } |
| 1767 | |