Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [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> |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 24 | #include <sys/wait.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 25 | #include <sys/stat.h> |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 26 | #include <ctype.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 27 | #include <fcntl.h> |
Elliott Hughes | 7373716 | 2014-06-25 17:27:42 -0700 | [diff] [blame] | 28 | #include <inttypes.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 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> |
Adam Langley | 41405bb | 2015-01-22 16:45:28 -0800 | [diff] [blame] | 39 | #include <openssl/sha.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 40 | #include <errno.h> |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 41 | #include <ext4.h> |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 42 | #include <linux/kdev_t.h> |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 43 | #include <fs_mgr.h> |
Paul Lawrence | 9c58a87 | 2014-09-30 09:12:51 -0700 | [diff] [blame] | 44 | #include <time.h> |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 45 | #include <math.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 46 | #include "cryptfs.h" |
| 47 | #define LOG_TAG "Cryptfs" |
| 48 | #include "cutils/log.h" |
| 49 | #include "cutils/properties.h" |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 50 | #include "cutils/android_reboot.h" |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 51 | #include "hardware_legacy/power.h" |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 52 | #include <logwrap/logwrap.h> |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 53 | #include "VolumeManager.h" |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 54 | #include "VoldUtil.h" |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 55 | #include "crypto_scrypt.h" |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 56 | #include "Ext4Crypt.h" |
| 57 | #include "ext4_crypt_init_extensions.h" |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 58 | #include "ext4_utils.h" |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 59 | #include "f2fs_sparseblock.h" |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 60 | #include "CheckBattery.h" |
jessica_yu | 3f14fe4 | 2014-09-22 15:57:40 +0800 | [diff] [blame] | 61 | #include "Process.h" |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 62 | |
Shawn Willden | 8af3335 | 2015-02-24 09:51:34 -0700 | [diff] [blame] | 63 | #include <hardware/keymaster0.h> |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 64 | #include <hardware/keymaster1.h> |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 65 | |
Mark Salyzyn | 3e97127 | 2014-01-21 13:27:04 -0800 | [diff] [blame] | 66 | #define UNUSED __attribute__((unused)) |
| 67 | |
Mark Salyzyn | 5eecc44 | 2014-02-12 14:16:14 -0800 | [diff] [blame] | 68 | #define UNUSED __attribute__((unused)) |
| 69 | |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
| 71 | #include "cryptfs_hw.h" |
| 72 | #endif |
| 73 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 74 | #define DM_CRYPT_BUF_SIZE 4096 |
| 75 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 76 | #define HASH_COUNT 2000 |
| 77 | #define KEY_LEN_BYTES 16 |
| 78 | #define IV_LEN_BYTES 16 |
| 79 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 80 | #define KEY_IN_FOOTER "footer" |
| 81 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 82 | #define DEFAULT_PASSWORD "default_password" |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 83 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 84 | #define CRYPTO_BLOCK_DEVICE "userdata" |
| 85 | |
| 86 | #define BREADCRUMB_FILE "/data/misc/vold/convert_fde" |
| 87 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 88 | #define EXT4_FS 1 |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 89 | #define F2FS_FS 2 |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 90 | |
Ken Sumrall | e919efe | 2012-09-29 17:07:41 -0700 | [diff] [blame] | 91 | #define TABLE_LOAD_RETRIES 10 |
| 92 | |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 93 | #define RSA_KEY_SIZE 2048 |
| 94 | #define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8) |
| 95 | #define RSA_EXPONENT 0x10001 |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 96 | #define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 97 | |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 98 | #define RETRY_MOUNT_ATTEMPTS 10 |
| 99 | #define RETRY_MOUNT_DELAY_SECONDS 1 |
| 100 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 101 | char *me = "cryptfs"; |
| 102 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 103 | static unsigned char saved_master_key[KEY_LEN_BYTES]; |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 104 | static char *saved_mount_point; |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 105 | static int master_key_saved = 0; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 106 | static struct crypt_persist_data *persist_data = NULL; |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 107 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 108 | static int keymaster_init(keymaster0_device_t **keymaster0_dev, |
| 109 | keymaster1_device_t **keymaster1_dev) |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 110 | { |
| 111 | int rc; |
| 112 | |
| 113 | const hw_module_t* mod; |
| 114 | rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); |
| 115 | if (rc) { |
| 116 | ALOGE("could not find any keystore module"); |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 117 | goto err; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 120 | SLOGI("keymaster module name is %s", mod->name); |
| 121 | SLOGI("keymaster version is %d", mod->module_api_version); |
| 122 | |
| 123 | *keymaster0_dev = NULL; |
| 124 | *keymaster1_dev = NULL; |
| 125 | if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) { |
| 126 | SLOGI("Found keymaster1 module, using keymaster1 API."); |
| 127 | rc = keymaster1_open(mod, keymaster1_dev); |
| 128 | } else { |
| 129 | SLOGI("Found keymaster0 module, using keymaster0 API."); |
| 130 | rc = keymaster0_open(mod, keymaster0_dev); |
| 131 | } |
| 132 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 133 | if (rc) { |
| 134 | ALOGE("could not open keymaster device in %s (%s)", |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 135 | KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); |
| 136 | goto err; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 141 | err: |
| 142 | *keymaster0_dev = NULL; |
| 143 | *keymaster1_dev = NULL; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 144 | return rc; |
| 145 | } |
| 146 | |
| 147 | /* Should we use keymaster? */ |
| 148 | static int keymaster_check_compatibility() |
| 149 | { |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 150 | keymaster0_device_t *keymaster0_dev = 0; |
| 151 | keymaster1_device_t *keymaster1_dev = 0; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 152 | int rc = 0; |
| 153 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 154 | if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 155 | SLOGE("Failed to init keymaster"); |
| 156 | rc = -1; |
| 157 | goto out; |
| 158 | } |
| 159 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 160 | if (keymaster1_dev) { |
| 161 | rc = 1; |
| 162 | goto out; |
| 163 | } |
Paul Lawrence | 8c00839 | 2014-05-06 14:02:48 -0700 | [diff] [blame] | 164 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 165 | // TODO(swillden): Check to see if there's any reason to require v0.3. I think v0.1 and v0.2 |
| 166 | // should work. |
| 167 | if (keymaster0_dev->common.module->module_api_version |
Paul Lawrence | 8c00839 | 2014-05-06 14:02:48 -0700 | [diff] [blame] | 168 | < KEYMASTER_MODULE_API_VERSION_0_3) { |
| 169 | rc = 0; |
| 170 | goto out; |
| 171 | } |
| 172 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 173 | if (!(keymaster0_dev->flags & KEYMASTER_SOFTWARE_ONLY) && |
| 174 | (keymaster0_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE)) { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 175 | rc = 1; |
| 176 | } |
| 177 | |
| 178 | out: |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 179 | if (keymaster1_dev) { |
| 180 | keymaster1_close(keymaster1_dev); |
| 181 | } |
| 182 | if (keymaster0_dev) { |
| 183 | keymaster0_close(keymaster0_dev); |
| 184 | } |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 185 | return rc; |
| 186 | } |
| 187 | |
| 188 | /* Create a new keymaster key and store it in this footer */ |
| 189 | static int keymaster_create_key(struct crypt_mnt_ftr *ftr) |
| 190 | { |
| 191 | uint8_t* key = 0; |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 192 | keymaster0_device_t *keymaster0_dev = 0; |
| 193 | keymaster1_device_t *keymaster1_dev = 0; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 194 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 195 | if (ftr->keymaster_blob_size) { |
| 196 | SLOGI("Already have key"); |
| 197 | return 0; |
| 198 | } |
| 199 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 200 | if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 201 | SLOGE("Failed to init keymaster"); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | int rc = 0; |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 206 | size_t key_size = 0; |
| 207 | if (keymaster1_dev) { |
| 208 | keymaster_key_param_t params[] = { |
| 209 | /* Algorithm & size specifications. Stick with RSA for now. Switch to AES later. */ |
| 210 | keymaster_param_enum(KM_TAG_ALGORITHM, KM_ALGORITHM_RSA), |
| 211 | keymaster_param_int(KM_TAG_KEY_SIZE, RSA_KEY_SIZE), |
| 212 | keymaster_param_long(KM_TAG_RSA_PUBLIC_EXPONENT, RSA_EXPONENT), |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 213 | |
Shawn Willden | 86af355 | 2015-06-24 07:21:54 -0700 | [diff] [blame] | 214 | /* The only allowed purpose for this key is signing. */ |
| 215 | keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_SIGN), |
| 216 | |
| 217 | /* Padding & digest specifications. */ |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 218 | keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE), |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 219 | keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE), |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 220 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 221 | /* Require that the key be usable in standalone mode. File system isn't available. */ |
| 222 | keymaster_param_enum(KM_TAG_BLOB_USAGE_REQUIREMENTS, KM_BLOB_STANDALONE), |
| 223 | |
| 224 | /* No auth requirements, because cryptfs is not yet integrated with gatekeeper. */ |
| 225 | keymaster_param_bool(KM_TAG_NO_AUTH_REQUIRED), |
| 226 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 227 | /* Rate-limit key usage attempts, to rate-limit brute force */ |
| 228 | keymaster_param_int(KM_TAG_MIN_SECONDS_BETWEEN_OPS, KEYMASTER_CRYPTFS_RATE_LIMIT), |
| 229 | }; |
| 230 | keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) }; |
| 231 | keymaster_key_blob_t key_blob; |
| 232 | keymaster_error_t error = keymaster1_dev->generate_key(keymaster1_dev, ¶m_set, |
| 233 | &key_blob, |
| 234 | NULL /* characteristics */); |
| 235 | if (error != KM_ERROR_OK) { |
| 236 | SLOGE("Failed to generate keymaster1 key, error %d", error); |
| 237 | rc = -1; |
| 238 | goto out; |
| 239 | } |
| 240 | |
| 241 | key = (uint8_t*)key_blob.key_material; |
| 242 | key_size = key_blob.key_material_size; |
| 243 | } |
| 244 | else if (keymaster0_dev) { |
| 245 | keymaster_rsa_keygen_params_t params; |
| 246 | memset(¶ms, '\0', sizeof(params)); |
| 247 | params.public_exponent = RSA_EXPONENT; |
| 248 | params.modulus_size = RSA_KEY_SIZE; |
| 249 | |
| 250 | if (keymaster0_dev->generate_keypair(keymaster0_dev, TYPE_RSA, ¶ms, |
| 251 | &key, &key_size)) { |
| 252 | SLOGE("Failed to generate keypair"); |
| 253 | rc = -1; |
| 254 | goto out; |
| 255 | } |
| 256 | } else { |
| 257 | SLOGE("Cryptfs bug: keymaster_init succeeded but didn't initialize a device"); |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 258 | rc = -1; |
| 259 | goto out; |
| 260 | } |
| 261 | |
| 262 | if (key_size > KEYMASTER_BLOB_SIZE) { |
| 263 | SLOGE("Keymaster key too large for crypto footer"); |
| 264 | rc = -1; |
| 265 | goto out; |
| 266 | } |
| 267 | |
| 268 | memcpy(ftr->keymaster_blob, key, key_size); |
| 269 | ftr->keymaster_blob_size = key_size; |
| 270 | |
| 271 | out: |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 272 | if (keymaster0_dev) |
| 273 | keymaster0_close(keymaster0_dev); |
| 274 | if (keymaster1_dev) |
| 275 | keymaster1_close(keymaster1_dev); |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 276 | free(key); |
| 277 | return rc; |
| 278 | } |
| 279 | |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 280 | /* This signs the given object using the keymaster key. */ |
| 281 | static int keymaster_sign_object(struct crypt_mnt_ftr *ftr, |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 282 | const unsigned char *object, |
| 283 | const size_t object_size, |
| 284 | unsigned char **signature, |
| 285 | size_t *signature_size) |
| 286 | { |
| 287 | int rc = 0; |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 288 | keymaster0_device_t *keymaster0_dev = 0; |
| 289 | keymaster1_device_t *keymaster1_dev = 0; |
| 290 | if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) { |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 291 | SLOGE("Failed to init keymaster"); |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 292 | rc = -1; |
| 293 | goto out; |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 294 | } |
| 295 | |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 296 | unsigned char to_sign[RSA_KEY_SIZE_BYTES]; |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 297 | size_t to_sign_size = sizeof(to_sign); |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 298 | memset(to_sign, 0, RSA_KEY_SIZE_BYTES); |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 299 | |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 300 | // To sign a message with RSA, the message must satisfy two |
| 301 | // constraints: |
| 302 | // |
| 303 | // 1. The message, when interpreted as a big-endian numeric value, must |
| 304 | // be strictly less than the public modulus of the RSA key. Note |
| 305 | // that because the most significant bit of the public modulus is |
| 306 | // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit |
| 307 | // key), an n-bit message with most significant bit 0 always |
| 308 | // satisfies this requirement. |
| 309 | // |
| 310 | // 2. The message must have the same length in bits as the public |
| 311 | // modulus of the RSA key. This requirement isn't mathematically |
| 312 | // necessary, but is necessary to ensure consistency in |
| 313 | // implementations. |
| 314 | switch (ftr->kdf_type) { |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 315 | case KDF_SCRYPT_KEYMASTER: |
| 316 | // This ensures the most significant byte of the signed message |
| 317 | // is zero. We could have zero-padded to the left instead, but |
| 318 | // this approach is slightly more robust against changes in |
| 319 | // object size. However, it's still broken (but not unusably |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 320 | // so) because we really should be using a proper deterministic |
| 321 | // RSA padding function, such as PKCS1. |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 322 | memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size)); |
| 323 | SLOGI("Signing safely-padded object"); |
| 324 | break; |
| 325 | default: |
| 326 | SLOGE("Unknown KDF type %d", ftr->kdf_type); |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 327 | rc = -1; |
| 328 | goto out; |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 329 | } |
| 330 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 331 | if (keymaster0_dev) { |
| 332 | keymaster_rsa_sign_params_t params; |
| 333 | params.digest_type = DIGEST_NONE; |
| 334 | params.padding_type = PADDING_NONE; |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 335 | |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 336 | rc = keymaster0_dev->sign_data(keymaster0_dev, |
| 337 | ¶ms, |
| 338 | ftr->keymaster_blob, |
| 339 | ftr->keymaster_blob_size, |
| 340 | to_sign, |
| 341 | to_sign_size, |
| 342 | signature, |
| 343 | signature_size); |
| 344 | goto out; |
| 345 | } else if (keymaster1_dev) { |
| 346 | keymaster_key_blob_t key = { ftr->keymaster_blob, ftr->keymaster_blob_size }; |
| 347 | keymaster_key_param_t params[] = { |
| 348 | keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE), |
| 349 | keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE), |
| 350 | }; |
| 351 | keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) }; |
| 352 | keymaster_operation_handle_t op_handle; |
| 353 | keymaster_error_t error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key, |
| 354 | ¶m_set, NULL /* out_params */, |
| 355 | &op_handle); |
Shawn Willden | 0417060 | 2015-06-18 12:26:59 -0600 | [diff] [blame] | 356 | if (error == KM_ERROR_KEY_RATE_LIMIT_EXCEEDED) { |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 357 | // Key usage has been rate-limited. Wait a bit and try again. |
| 358 | sleep(KEYMASTER_CRYPTFS_RATE_LIMIT); |
| 359 | error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key, |
| 360 | ¶m_set, NULL /* out_params */, |
| 361 | &op_handle); |
| 362 | } |
| 363 | if (error != KM_ERROR_OK) { |
| 364 | SLOGE("Error starting keymaster signature transaction: %d", error); |
| 365 | rc = -1; |
| 366 | goto out; |
| 367 | } |
| 368 | |
| 369 | keymaster_blob_t input = { to_sign, to_sign_size }; |
| 370 | size_t input_consumed; |
| 371 | error = keymaster1_dev->update(keymaster1_dev, op_handle, NULL /* in_params */, |
| 372 | &input, &input_consumed, NULL /* out_params */, |
| 373 | NULL /* output */); |
| 374 | if (error != KM_ERROR_OK) { |
| 375 | SLOGE("Error sending data to keymaster signature transaction: %d", error); |
| 376 | rc = -1; |
| 377 | goto out; |
| 378 | } |
| 379 | if (input_consumed != to_sign_size) { |
| 380 | // This should never happen. If it does, it's a bug in the keymaster implementation. |
| 381 | SLOGE("Keymaster update() did not consume all data."); |
| 382 | keymaster1_dev->abort(keymaster1_dev, op_handle); |
| 383 | rc = -1; |
| 384 | goto out; |
| 385 | } |
| 386 | |
| 387 | keymaster_blob_t tmp_sig; |
| 388 | error = keymaster1_dev->finish(keymaster1_dev, op_handle, NULL /* in_params */, |
| 389 | NULL /* verify signature */, NULL /* out_params */, |
| 390 | &tmp_sig); |
| 391 | if (error != KM_ERROR_OK) { |
| 392 | SLOGE("Error finishing keymaster signature transaction: %d", error); |
| 393 | rc = -1; |
| 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | *signature = (uint8_t*)tmp_sig.data; |
| 398 | *signature_size = tmp_sig.data_length; |
| 399 | } else { |
| 400 | SLOGE("Cryptfs bug: keymaster_init succeded but didn't initialize a device."); |
| 401 | rc = -1; |
| 402 | goto out; |
| 403 | } |
| 404 | |
| 405 | out: |
| 406 | if (keymaster1_dev) |
| 407 | keymaster1_close(keymaster1_dev); |
| 408 | if (keymaster0_dev) |
| 409 | keymaster0_close(keymaster0_dev); |
| 410 | |
| 411 | return rc; |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 412 | } |
| 413 | |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 414 | /* Store password when userdata is successfully decrypted and mounted. |
| 415 | * Cleared by cryptfs_clear_password |
| 416 | * |
| 417 | * To avoid a double prompt at boot, we need to store the CryptKeeper |
| 418 | * password and pass it to KeyGuard, which uses it to unlock KeyStore. |
| 419 | * Since the entire framework is torn down and rebuilt after encryption, |
| 420 | * we have to use a daemon or similar to store the password. Since vold |
| 421 | * is secured against IPC except from system processes, it seems a reasonable |
| 422 | * place to store this. |
| 423 | * |
| 424 | * password should be cleared once it has been used. |
| 425 | * |
| 426 | * password is aged out after password_max_age_seconds seconds. |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 427 | */ |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 428 | static char* password = 0; |
| 429 | static int password_expiry_time = 0; |
| 430 | static const int password_max_age_seconds = 60; |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 431 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 432 | extern struct fstab *fstab; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 433 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 434 | enum RebootType {reboot, recovery, shutdown}; |
| 435 | static void cryptfs_reboot(enum RebootType rt) |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 436 | { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 437 | switch(rt) { |
| 438 | case reboot: |
| 439 | property_set(ANDROID_RB_PROPERTY, "reboot"); |
| 440 | break; |
| 441 | |
| 442 | case recovery: |
| 443 | property_set(ANDROID_RB_PROPERTY, "reboot,recovery"); |
| 444 | break; |
| 445 | |
| 446 | case shutdown: |
| 447 | property_set(ANDROID_RB_PROPERTY, "shutdown"); |
| 448 | break; |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 449 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 450 | |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 451 | sleep(20); |
| 452 | |
| 453 | /* Shouldn't get here, reboot should happen before sleep times out */ |
| 454 | return; |
| 455 | } |
| 456 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 457 | static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags) |
| 458 | { |
| 459 | memset(io, 0, dataSize); |
| 460 | io->data_size = dataSize; |
| 461 | io->data_start = sizeof(struct dm_ioctl); |
| 462 | io->version[0] = 4; |
| 463 | io->version[1] = 0; |
| 464 | io->version[2] = 0; |
| 465 | io->flags = flags; |
| 466 | if (name) { |
Marek Pola | 5e6b914 | 2015-02-05 14:22:34 +0100 | [diff] [blame] | 467 | strlcpy(io->name, name, sizeof(io->name)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 471 | /** |
| 472 | * Gets the default device scrypt parameters for key derivation time tuning. |
| 473 | * The parameters should lead to about one second derivation time for the |
| 474 | * given device. |
| 475 | */ |
| 476 | static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) { |
| 477 | const int default_params[] = SCRYPT_DEFAULTS; |
| 478 | int params[] = SCRYPT_DEFAULTS; |
| 479 | char paramstr[PROPERTY_VALUE_MAX]; |
| 480 | char *token; |
| 481 | char *saveptr; |
| 482 | int i; |
| 483 | |
| 484 | property_get(SCRYPT_PROP, paramstr, ""); |
| 485 | if (paramstr[0] != '\0') { |
| 486 | /* |
| 487 | * The token we're looking for should be three integers separated by |
| 488 | * colons (e.g., "12:8:1"). Scan the property to make sure it matches. |
| 489 | */ |
Kenny Root | 2947e34 | 2013-08-14 15:54:49 -0700 | [diff] [blame] | 490 | for (i = 0, token = strtok_r(paramstr, ":", &saveptr); |
| 491 | token != NULL && i < 3; |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 492 | i++, token = strtok_r(NULL, ":", &saveptr)) { |
| 493 | char *endptr; |
| 494 | params[i] = strtol(token, &endptr, 10); |
| 495 | |
| 496 | /* |
| 497 | * Check that there was a valid number and it's 8-bit. If not, |
| 498 | * break out and the end check will take the default values. |
| 499 | */ |
| 500 | if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) { |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * If there were not enough tokens or a token was malformed (not an |
| 507 | * integer), it will end up here and the default parameters can be |
| 508 | * taken. |
| 509 | */ |
| 510 | if ((i != 3) || (token != NULL)) { |
| 511 | SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr); |
| 512 | memcpy(params, default_params, sizeof(params)); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | ftr->N_factor = params[0]; |
| 517 | ftr->r_factor = params[1]; |
| 518 | ftr->p_factor = params[2]; |
| 519 | } |
| 520 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 521 | static unsigned int get_fs_size(char *dev) |
| 522 | { |
| 523 | int fd, block_size; |
| 524 | struct ext4_super_block sb; |
| 525 | off64_t len; |
| 526 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 527 | if ((fd = open(dev, O_RDONLY|O_CLOEXEC)) < 0) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 528 | SLOGE("Cannot open device to get filesystem size "); |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | if (lseek64(fd, 1024, SEEK_SET) < 0) { |
| 533 | SLOGE("Cannot seek to superblock"); |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) { |
| 538 | SLOGE("Cannot read superblock"); |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | close(fd); |
| 543 | |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 544 | if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) { |
| 545 | SLOGE("Not a valid ext4 superblock"); |
| 546 | return 0; |
| 547 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 548 | block_size = 1024 << sb.s_log_block_size; |
| 549 | /* compute length in bytes */ |
| 550 | len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size; |
| 551 | |
| 552 | /* return length in sectors */ |
| 553 | return (unsigned int) (len / 512); |
| 554 | } |
| 555 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 556 | static int get_crypt_ftr_info(char **metadata_fname, off64_t *off) |
| 557 | { |
| 558 | static int cached_data = 0; |
| 559 | static off64_t cached_off = 0; |
| 560 | static char cached_metadata_fname[PROPERTY_VALUE_MAX] = ""; |
| 561 | int fd; |
| 562 | char key_loc[PROPERTY_VALUE_MAX]; |
| 563 | char real_blkdev[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 564 | int rc = -1; |
| 565 | |
| 566 | if (!cached_data) { |
| 567 | fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc)); |
| 568 | |
| 569 | if (!strcmp(key_loc, KEY_IN_FOOTER)) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 570 | if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 571 | SLOGE("Cannot open real block device %s\n", real_blkdev); |
| 572 | return -1; |
| 573 | } |
| 574 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 575 | unsigned long nr_sec = 0; |
| 576 | get_blkdev_size(fd, &nr_sec); |
| 577 | if (nr_sec != 0) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 578 | /* If it's an encrypted Android partition, the last 16 Kbytes contain the |
| 579 | * encryption info footer and key, and plenty of bytes to spare for future |
| 580 | * growth. |
| 581 | */ |
| 582 | strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname)); |
| 583 | cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
| 584 | cached_data = 1; |
| 585 | } else { |
| 586 | SLOGE("Cannot get size of block device %s\n", real_blkdev); |
| 587 | } |
| 588 | close(fd); |
| 589 | } else { |
| 590 | strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname)); |
| 591 | cached_off = 0; |
| 592 | cached_data = 1; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | if (cached_data) { |
| 597 | if (metadata_fname) { |
| 598 | *metadata_fname = cached_metadata_fname; |
| 599 | } |
| 600 | if (off) { |
| 601 | *off = cached_off; |
| 602 | } |
| 603 | rc = 0; |
| 604 | } |
| 605 | |
| 606 | return rc; |
| 607 | } |
| 608 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 609 | /* Set sha256 checksum in structure */ |
| 610 | static void set_ftr_sha(struct crypt_mnt_ftr *crypt_ftr) |
| 611 | { |
| 612 | SHA256_CTX c; |
| 613 | SHA256_Init(&c); |
| 614 | memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256)); |
| 615 | SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr)); |
| 616 | SHA256_Final(crypt_ftr->sha256, &c); |
| 617 | } |
| 618 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 619 | /* key or salt can be NULL, in which case just skip writing that value. Useful to |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 620 | * update the failed mount count but not change the key. |
| 621 | */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 622 | static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 623 | { |
| 624 | int fd; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 625 | unsigned int cnt; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 626 | /* starting_off is set to the SEEK_SET offset |
| 627 | * where the crypto structure starts |
| 628 | */ |
| 629 | off64_t starting_off; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 630 | int rc = -1; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 631 | char *fname = NULL; |
Ken Sumrall | 3be890f | 2011-09-14 16:53:46 -0700 | [diff] [blame] | 632 | struct stat statbuf; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 633 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 634 | set_ftr_sha(crypt_ftr); |
| 635 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 636 | if (get_crypt_ftr_info(&fname, &starting_off)) { |
| 637 | SLOGE("Unable to get crypt_ftr_info\n"); |
| 638 | return -1; |
| 639 | } |
| 640 | if (fname[0] != '/') { |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 641 | SLOGE("Unexpected value for crypto key location\n"); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 642 | return -1; |
| 643 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 644 | if ( (fd = open(fname, O_RDWR | O_CREAT|O_CLOEXEC, 0600)) < 0) { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 645 | SLOGE("Cannot open footer file %s for put\n", fname); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 646 | return -1; |
| 647 | } |
| 648 | |
| 649 | /* Seek to the start of the crypt footer */ |
| 650 | if (lseek64(fd, starting_off, SEEK_SET) == -1) { |
| 651 | SLOGE("Cannot seek to real block device footer\n"); |
| 652 | goto errout; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 656 | SLOGE("Cannot write real block device footer\n"); |
| 657 | goto errout; |
| 658 | } |
| 659 | |
Ken Sumrall | 3be890f | 2011-09-14 16:53:46 -0700 | [diff] [blame] | 660 | fstat(fd, &statbuf); |
| 661 | /* If the keys are kept on a raw block device, do not try to truncate it. */ |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 662 | if (S_ISREG(statbuf.st_mode)) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 663 | if (ftruncate(fd, 0x4000)) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 664 | SLOGE("Cannot set footer file size\n"); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 665 | goto errout; |
| 666 | } |
| 667 | } |
| 668 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 669 | /* Success! */ |
| 670 | rc = 0; |
| 671 | |
| 672 | errout: |
| 673 | close(fd); |
| 674 | return rc; |
| 675 | |
| 676 | } |
| 677 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 678 | static bool check_ftr_sha(const struct crypt_mnt_ftr *crypt_ftr) |
| 679 | { |
| 680 | struct crypt_mnt_ftr copy; |
| 681 | memcpy(©, crypt_ftr, sizeof(copy)); |
| 682 | set_ftr_sha(©); |
| 683 | return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0; |
| 684 | } |
| 685 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 686 | static inline int unix_read(int fd, void* buff, int len) |
| 687 | { |
| 688 | return TEMP_FAILURE_RETRY(read(fd, buff, len)); |
| 689 | } |
| 690 | |
| 691 | static inline int unix_write(int fd, const void* buff, int len) |
| 692 | { |
| 693 | return TEMP_FAILURE_RETRY(write(fd, buff, len)); |
| 694 | } |
| 695 | |
| 696 | static void init_empty_persist_data(struct crypt_persist_data *pdata, int len) |
| 697 | { |
| 698 | memset(pdata, 0, len); |
| 699 | pdata->persist_magic = PERSIST_DATA_MAGIC; |
| 700 | pdata->persist_valid_entries = 0; |
| 701 | } |
| 702 | |
| 703 | /* A routine to update the passed in crypt_ftr to the lastest version. |
| 704 | * fd is open read/write on the device that holds the crypto footer and persistent |
| 705 | * data, crypt_ftr is a pointer to the struct to be updated, and offset is the |
| 706 | * absolute offset to the start of the crypt_mnt_ftr on the passed in fd. |
| 707 | */ |
| 708 | static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset) |
| 709 | { |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 710 | int orig_major = crypt_ftr->major_version; |
| 711 | int orig_minor = crypt_ftr->minor_version; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 712 | |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 713 | if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) { |
| 714 | struct crypt_persist_data *pdata; |
| 715 | off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 716 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 717 | SLOGW("upgrading crypto footer to 1.1"); |
| 718 | |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 719 | pdata = malloc(CRYPT_PERSIST_DATA_SIZE); |
| 720 | if (pdata == NULL) { |
| 721 | SLOGE("Cannot allocate persisent data\n"); |
| 722 | return; |
| 723 | } |
| 724 | memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE); |
| 725 | |
| 726 | /* Need to initialize the persistent data area */ |
| 727 | if (lseek64(fd, pdata_offset, SEEK_SET) == -1) { |
| 728 | SLOGE("Cannot seek to persisent data offset\n"); |
Henrik Baard | 9106463 | 2015-02-05 15:09:17 +0100 | [diff] [blame] | 729 | free(pdata); |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 730 | return; |
| 731 | } |
| 732 | /* Write all zeros to the first copy, making it invalid */ |
| 733 | unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE); |
| 734 | |
| 735 | /* Write a valid but empty structure to the second copy */ |
| 736 | init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE); |
| 737 | unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE); |
| 738 | |
| 739 | /* Update the footer */ |
| 740 | crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE; |
| 741 | crypt_ftr->persist_data_offset[0] = pdata_offset; |
| 742 | crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE; |
| 743 | crypt_ftr->minor_version = 1; |
Henrik Baard | 9106463 | 2015-02-05 15:09:17 +0100 | [diff] [blame] | 744 | free(pdata); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 747 | if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) { |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 748 | SLOGW("upgrading crypto footer to 1.2"); |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 749 | /* But keep the old kdf_type. |
| 750 | * It will get updated later to KDF_SCRYPT after the password has been verified. |
| 751 | */ |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 752 | crypt_ftr->kdf_type = KDF_PBKDF2; |
| 753 | get_device_scrypt_params(crypt_ftr); |
| 754 | crypt_ftr->minor_version = 2; |
| 755 | } |
| 756 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 757 | if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) { |
| 758 | SLOGW("upgrading crypto footer to 1.3"); |
| 759 | crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD; |
| 760 | crypt_ftr->minor_version = 3; |
| 761 | } |
| 762 | |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 763 | if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) { |
| 764 | if (lseek64(fd, offset, SEEK_SET) == -1) { |
| 765 | SLOGE("Cannot seek to crypt footer\n"); |
| 766 | return; |
| 767 | } |
| 768 | unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr)); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 769 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | |
| 773 | static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 774 | { |
| 775 | int fd; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 776 | unsigned int cnt; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 777 | off64_t starting_off; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 778 | int rc = -1; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 779 | char *fname = NULL; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 780 | struct stat statbuf; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 781 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 782 | if (get_crypt_ftr_info(&fname, &starting_off)) { |
| 783 | SLOGE("Unable to get crypt_ftr_info\n"); |
| 784 | return -1; |
| 785 | } |
| 786 | if (fname[0] != '/') { |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 787 | SLOGE("Unexpected value for crypto key location\n"); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 788 | return -1; |
| 789 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 790 | if ( (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 791 | SLOGE("Cannot open footer file %s for get\n", fname); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 792 | return -1; |
| 793 | } |
| 794 | |
| 795 | /* Make sure it's 16 Kbytes in length */ |
| 796 | fstat(fd, &statbuf); |
| 797 | if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) { |
| 798 | SLOGE("footer file %s is not the expected size!\n", fname); |
| 799 | goto errout; |
| 800 | } |
| 801 | |
| 802 | /* Seek to the start of the crypt footer */ |
| 803 | if (lseek64(fd, starting_off, SEEK_SET) == -1) { |
| 804 | SLOGE("Cannot seek to real block device footer\n"); |
| 805 | goto errout; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 809 | SLOGE("Cannot read real block device footer\n"); |
| 810 | goto errout; |
| 811 | } |
| 812 | |
| 813 | if (crypt_ftr->magic != CRYPT_MNT_MAGIC) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 814 | SLOGE("Bad magic for real block device %s\n", fname); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 815 | goto errout; |
| 816 | } |
| 817 | |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 818 | if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) { |
| 819 | SLOGE("Cannot understand major version %d real block device footer; expected %d\n", |
| 820 | crypt_ftr->major_version, CURRENT_MAJOR_VERSION); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 821 | goto errout; |
| 822 | } |
| 823 | |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 824 | if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) { |
| 825 | SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n", |
| 826 | crypt_ftr->minor_version, CURRENT_MINOR_VERSION); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 827 | } |
| 828 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 829 | /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the |
| 830 | * copy on disk before returning. |
| 831 | */ |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 832 | if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 833 | upgrade_crypt_ftr(fd, crypt_ftr, starting_off); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 836 | /* Success! */ |
| 837 | rc = 0; |
| 838 | |
| 839 | errout: |
| 840 | close(fd); |
| 841 | return rc; |
| 842 | } |
| 843 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 844 | static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr) |
| 845 | { |
| 846 | if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size > |
| 847 | crypt_ftr->persist_data_offset[1]) { |
| 848 | SLOGE("Crypt_ftr persist data regions overlap"); |
| 849 | return -1; |
| 850 | } |
| 851 | |
| 852 | if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) { |
| 853 | SLOGE("Crypt_ftr persist data region 0 starts after region 1"); |
| 854 | return -1; |
| 855 | } |
| 856 | |
| 857 | if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) - |
| 858 | (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) > |
| 859 | CRYPT_FOOTER_OFFSET) { |
| 860 | SLOGE("Persistent data extends past crypto footer"); |
| 861 | return -1; |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | static int load_persistent_data(void) |
| 868 | { |
| 869 | struct crypt_mnt_ftr crypt_ftr; |
| 870 | struct crypt_persist_data *pdata = NULL; |
| 871 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 872 | char *fname; |
| 873 | int found = 0; |
| 874 | int fd; |
| 875 | int ret; |
| 876 | int i; |
| 877 | |
| 878 | if (persist_data) { |
| 879 | /* Nothing to do, we've already loaded or initialized it */ |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | |
| 884 | /* If not encrypted, just allocate an empty table and initialize it */ |
| 885 | property_get("ro.crypto.state", encrypted_state, ""); |
| 886 | if (strcmp(encrypted_state, "encrypted") ) { |
| 887 | pdata = malloc(CRYPT_PERSIST_DATA_SIZE); |
| 888 | if (pdata) { |
| 889 | init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE); |
| 890 | persist_data = pdata; |
| 891 | return 0; |
| 892 | } |
| 893 | return -1; |
| 894 | } |
| 895 | |
| 896 | if(get_crypt_ftr_and_key(&crypt_ftr)) { |
| 897 | return -1; |
| 898 | } |
| 899 | |
Paul Lawrence | 8561b5c | 2014-03-17 14:10:51 -0700 | [diff] [blame] | 900 | if ((crypt_ftr.major_version < 1) |
| 901 | || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 902 | SLOGE("Crypt_ftr version doesn't support persistent data"); |
| 903 | return -1; |
| 904 | } |
| 905 | |
| 906 | if (get_crypt_ftr_info(&fname, NULL)) { |
| 907 | return -1; |
| 908 | } |
| 909 | |
| 910 | ret = validate_persistent_data_storage(&crypt_ftr); |
| 911 | if (ret) { |
| 912 | return -1; |
| 913 | } |
| 914 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 915 | fd = open(fname, O_RDONLY|O_CLOEXEC); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 916 | if (fd < 0) { |
| 917 | SLOGE("Cannot open %s metadata file", fname); |
| 918 | return -1; |
| 919 | } |
| 920 | |
| 921 | if (persist_data == NULL) { |
| 922 | pdata = malloc(crypt_ftr.persist_data_size); |
| 923 | if (pdata == NULL) { |
| 924 | SLOGE("Cannot allocate memory for persistent data"); |
| 925 | goto err; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | for (i = 0; i < 2; i++) { |
| 930 | if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) { |
| 931 | SLOGE("Cannot seek to read persistent data on %s", fname); |
| 932 | goto err2; |
| 933 | } |
| 934 | if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){ |
| 935 | SLOGE("Error reading persistent data on iteration %d", i); |
| 936 | goto err2; |
| 937 | } |
| 938 | if (pdata->persist_magic == PERSIST_DATA_MAGIC) { |
| 939 | found = 1; |
| 940 | break; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | if (!found) { |
| 945 | SLOGI("Could not find valid persistent data, creating"); |
| 946 | init_empty_persist_data(pdata, crypt_ftr.persist_data_size); |
| 947 | } |
| 948 | |
| 949 | /* Success */ |
| 950 | persist_data = pdata; |
| 951 | close(fd); |
| 952 | return 0; |
| 953 | |
| 954 | err2: |
| 955 | free(pdata); |
| 956 | |
| 957 | err: |
| 958 | close(fd); |
| 959 | return -1; |
| 960 | } |
| 961 | |
| 962 | static int save_persistent_data(void) |
| 963 | { |
| 964 | struct crypt_mnt_ftr crypt_ftr; |
| 965 | struct crypt_persist_data *pdata; |
| 966 | char *fname; |
| 967 | off64_t write_offset; |
| 968 | off64_t erase_offset; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 969 | int fd; |
| 970 | int ret; |
| 971 | |
| 972 | if (persist_data == NULL) { |
| 973 | SLOGE("No persistent data to save"); |
| 974 | return -1; |
| 975 | } |
| 976 | |
| 977 | if(get_crypt_ftr_and_key(&crypt_ftr)) { |
| 978 | return -1; |
| 979 | } |
| 980 | |
Paul Lawrence | 8561b5c | 2014-03-17 14:10:51 -0700 | [diff] [blame] | 981 | if ((crypt_ftr.major_version < 1) |
| 982 | || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 983 | SLOGE("Crypt_ftr version doesn't support persistent data"); |
| 984 | return -1; |
| 985 | } |
| 986 | |
| 987 | ret = validate_persistent_data_storage(&crypt_ftr); |
| 988 | if (ret) { |
| 989 | return -1; |
| 990 | } |
| 991 | |
| 992 | if (get_crypt_ftr_info(&fname, NULL)) { |
| 993 | return -1; |
| 994 | } |
| 995 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 996 | fd = open(fname, O_RDWR|O_CLOEXEC); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 997 | if (fd < 0) { |
| 998 | SLOGE("Cannot open %s metadata file", fname); |
| 999 | return -1; |
| 1000 | } |
| 1001 | |
| 1002 | pdata = malloc(crypt_ftr.persist_data_size); |
| 1003 | if (pdata == NULL) { |
| 1004 | SLOGE("Cannot allocate persistant data"); |
| 1005 | goto err; |
| 1006 | } |
| 1007 | |
| 1008 | if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) { |
| 1009 | SLOGE("Cannot seek to read persistent data on %s", fname); |
| 1010 | goto err2; |
| 1011 | } |
| 1012 | |
| 1013 | if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) { |
| 1014 | SLOGE("Error reading persistent data before save"); |
| 1015 | goto err2; |
| 1016 | } |
| 1017 | |
| 1018 | if (pdata->persist_magic == PERSIST_DATA_MAGIC) { |
| 1019 | /* The first copy is the curent valid copy, so write to |
| 1020 | * the second copy and erase this one */ |
| 1021 | write_offset = crypt_ftr.persist_data_offset[1]; |
| 1022 | erase_offset = crypt_ftr.persist_data_offset[0]; |
| 1023 | } else { |
| 1024 | /* The second copy must be the valid copy, so write to |
| 1025 | * the first copy, and erase the second */ |
| 1026 | write_offset = crypt_ftr.persist_data_offset[0]; |
| 1027 | erase_offset = crypt_ftr.persist_data_offset[1]; |
| 1028 | } |
| 1029 | |
| 1030 | /* Write the new copy first, if successful, then erase the old copy */ |
Björn Landström | 96dbee7 | 2015-01-20 12:43:56 +0100 | [diff] [blame] | 1031 | if (lseek64(fd, write_offset, SEEK_SET) < 0) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1032 | SLOGE("Cannot seek to write persistent data"); |
| 1033 | goto err2; |
| 1034 | } |
| 1035 | if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) == |
| 1036 | (int) crypt_ftr.persist_data_size) { |
Björn Landström | 96dbee7 | 2015-01-20 12:43:56 +0100 | [diff] [blame] | 1037 | if (lseek64(fd, erase_offset, SEEK_SET) < 0) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1038 | SLOGE("Cannot seek to erase previous persistent data"); |
| 1039 | goto err2; |
| 1040 | } |
| 1041 | fsync(fd); |
| 1042 | memset(pdata, 0, crypt_ftr.persist_data_size); |
| 1043 | if (unix_write(fd, pdata, crypt_ftr.persist_data_size) != |
| 1044 | (int) crypt_ftr.persist_data_size) { |
| 1045 | SLOGE("Cannot write to erase previous persistent data"); |
| 1046 | goto err2; |
| 1047 | } |
| 1048 | fsync(fd); |
| 1049 | } else { |
| 1050 | SLOGE("Cannot write to save persistent data"); |
| 1051 | goto err2; |
| 1052 | } |
| 1053 | |
| 1054 | /* Success */ |
| 1055 | free(pdata); |
| 1056 | close(fd); |
| 1057 | return 0; |
| 1058 | |
| 1059 | err2: |
| 1060 | free(pdata); |
| 1061 | err: |
| 1062 | close(fd); |
| 1063 | return -1; |
| 1064 | } |
| 1065 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1066 | /* Convert a binary key of specified length into an ascii hex string equivalent, |
| 1067 | * without the leading 0x and with null termination |
| 1068 | */ |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1069 | static void convert_key_to_hex_ascii(const unsigned char *master_key, |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1070 | unsigned int keysize, char *master_key_ascii) { |
| 1071 | unsigned int i, a; |
| 1072 | unsigned char nibble; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1073 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1074 | for (i=0, a=0; i<keysize; i++, a+=2) { |
| 1075 | /* For each byte, write out two ascii hex digits */ |
| 1076 | nibble = (master_key[i] >> 4) & 0xf; |
| 1077 | master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1078 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1079 | nibble = master_key[i] & 0xf; |
| 1080 | master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 1081 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1082 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1083 | /* Add the null termination */ |
| 1084 | master_key_ascii[a] = '\0'; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1085 | |
| 1086 | } |
| 1087 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1088 | static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, |
| 1089 | const unsigned char *master_key, const char *real_blk_name, |
| 1090 | const char *name, int fd, const char *extra_params) { |
Dan Albert | c07fa3f | 2014-12-18 10:00:55 -0800 | [diff] [blame] | 1091 | _Alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE]; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1092 | struct dm_ioctl *io; |
| 1093 | struct dm_target_spec *tgt; |
| 1094 | char *crypt_params; |
| 1095 | char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */ |
| 1096 | int i; |
| 1097 | |
| 1098 | io = (struct dm_ioctl *) buffer; |
| 1099 | |
| 1100 | /* Load the mapping table for this device */ |
| 1101 | tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)]; |
| 1102 | |
| 1103 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1104 | io->target_count = 1; |
| 1105 | tgt->status = 0; |
| 1106 | tgt->sector_start = 0; |
| 1107 | tgt->length = crypt_ftr->fs_size; |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 1108 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Iliyan Malchev | bb7d9af | 2014-11-20 18:42:23 -0800 | [diff] [blame] | 1109 | if (!strcmp((char *)crypt_ftr->crypto_type_name, "aes-xts")) { |
| 1110 | strlcpy(tgt->target_type, "req-crypt", DM_MAX_TYPE_NAME); |
| 1111 | } |
| 1112 | else { |
| 1113 | strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME); |
| 1114 | } |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 1115 | #else |
| 1116 | strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME); |
| 1117 | #endif |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1118 | |
| 1119 | crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec); |
| 1120 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 1121 | sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name, |
| 1122 | master_key_ascii, real_blk_name, extra_params); |
| 1123 | crypt_params += strlen(crypt_params) + 1; |
| 1124 | crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */ |
| 1125 | tgt->next = crypt_params - buffer; |
| 1126 | |
| 1127 | for (i = 0; i < TABLE_LOAD_RETRIES; i++) { |
| 1128 | if (! ioctl(fd, DM_TABLE_LOAD, io)) { |
| 1129 | break; |
| 1130 | } |
| 1131 | usleep(500000); |
| 1132 | } |
| 1133 | |
| 1134 | if (i == TABLE_LOAD_RETRIES) { |
| 1135 | /* We failed to load the table, return an error */ |
| 1136 | return -1; |
| 1137 | } else { |
| 1138 | return i + 1; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | static int get_dm_crypt_version(int fd, const char *name, int *version) |
| 1144 | { |
| 1145 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1146 | struct dm_ioctl *io; |
| 1147 | struct dm_target_versions *v; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1148 | |
| 1149 | io = (struct dm_ioctl *) buffer; |
| 1150 | |
| 1151 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1152 | |
| 1153 | if (ioctl(fd, DM_LIST_VERSIONS, io)) { |
| 1154 | return -1; |
| 1155 | } |
| 1156 | |
| 1157 | /* Iterate over the returned versions, looking for name of "crypt". |
| 1158 | * When found, get and return the version. |
| 1159 | */ |
| 1160 | v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)]; |
| 1161 | while (v->next) { |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 1162 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Iliyan Malchev | bb7d9af | 2014-11-20 18:42:23 -0800 | [diff] [blame] | 1163 | if (! strcmp(v->name, "crypt") || ! strcmp(v->name, "req-crypt")) { |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 1164 | #else |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1165 | if (! strcmp(v->name, "crypt")) { |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 1166 | #endif |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1167 | /* We found the crypt driver, return the version, and get out */ |
| 1168 | version[0] = v->version[0]; |
| 1169 | version[1] = v->version[1]; |
| 1170 | version[2] = v->version[2]; |
| 1171 | return 0; |
| 1172 | } |
| 1173 | v = (struct dm_target_versions *)(((char *)v) + v->next); |
| 1174 | } |
| 1175 | |
| 1176 | return -1; |
| 1177 | } |
| 1178 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1179 | static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, |
| 1180 | const unsigned char *master_key, const char *real_blk_name, |
| 1181 | char *crypto_blk_name, const char *name) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1182 | char buffer[DM_CRYPT_BUF_SIZE]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1183 | struct dm_ioctl *io; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1184 | unsigned int minor; |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 1185 | int fd=0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1186 | int retval = -1; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1187 | int version[3]; |
| 1188 | char *extra_params; |
| 1189 | int load_count; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1190 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1191 | if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1192 | SLOGE("Cannot open device-mapper\n"); |
| 1193 | goto errout; |
| 1194 | } |
| 1195 | |
| 1196 | io = (struct dm_ioctl *) buffer; |
| 1197 | |
| 1198 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1199 | if (ioctl(fd, DM_DEV_CREATE, io)) { |
| 1200 | SLOGE("Cannot create dm-crypt device\n"); |
| 1201 | goto errout; |
| 1202 | } |
| 1203 | |
| 1204 | /* Get the device status, in particular, the name of it's device file */ |
| 1205 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1206 | if (ioctl(fd, DM_DEV_STATUS, io)) { |
| 1207 | SLOGE("Cannot retrieve dm-crypt device status\n"); |
| 1208 | goto errout; |
| 1209 | } |
| 1210 | minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); |
| 1211 | snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor); |
| 1212 | |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1213 | extra_params = ""; |
| 1214 | if (! get_dm_crypt_version(fd, name, version)) { |
| 1215 | /* Support for allow_discards was added in version 1.11.0 */ |
| 1216 | if ((version[0] >= 2) || |
| 1217 | ((version[0] == 1) && (version[1] >= 11))) { |
| 1218 | extra_params = "1 allow_discards"; |
| 1219 | SLOGI("Enabling support for allow_discards in dmcrypt.\n"); |
| 1220 | } |
Ken Sumrall | e919efe | 2012-09-29 17:07:41 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1223 | load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, |
| 1224 | fd, extra_params); |
| 1225 | if (load_count < 0) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1226 | SLOGE("Cannot load dm-crypt mapping table.\n"); |
| 1227 | goto errout; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1228 | } else if (load_count > 1) { |
| 1229 | SLOGI("Took %d tries to load dmcrypt table.\n", load_count); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* Resume this device to activate it */ |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1233 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1234 | |
| 1235 | if (ioctl(fd, DM_DEV_SUSPEND, io)) { |
| 1236 | SLOGE("Cannot resume the dm-crypt device\n"); |
| 1237 | goto errout; |
| 1238 | } |
| 1239 | |
| 1240 | /* We made it here with no errors. Woot! */ |
| 1241 | retval = 0; |
| 1242 | |
| 1243 | errout: |
| 1244 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 1245 | |
| 1246 | return retval; |
| 1247 | } |
| 1248 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1249 | static int delete_crypto_blk_dev(char *name) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1250 | { |
| 1251 | int fd; |
| 1252 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1253 | struct dm_ioctl *io; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1254 | int retval = -1; |
| 1255 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1256 | if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1257 | SLOGE("Cannot open device-mapper\n"); |
| 1258 | goto errout; |
| 1259 | } |
| 1260 | |
| 1261 | io = (struct dm_ioctl *) buffer; |
| 1262 | |
| 1263 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1264 | if (ioctl(fd, DM_DEV_REMOVE, io)) { |
| 1265 | SLOGE("Cannot remove dm-crypt device\n"); |
| 1266 | goto errout; |
| 1267 | } |
| 1268 | |
| 1269 | /* We made it here with no errors. Woot! */ |
| 1270 | retval = 0; |
| 1271 | |
| 1272 | errout: |
| 1273 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 1274 | |
| 1275 | return retval; |
| 1276 | |
| 1277 | } |
| 1278 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1279 | static int pbkdf2(const char *passwd, const unsigned char *salt, |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1280 | unsigned char *ikey, void *params UNUSED) |
| 1281 | { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1282 | SLOGI("Using pbkdf2 for cryptfs KDF"); |
| 1283 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1284 | /* Turn the password into a key and IV that can decrypt the master key */ |
Adam Langley | bf0d972 | 2015-11-04 14:51:39 -0800 | [diff] [blame] | 1285 | return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN, |
| 1286 | HASH_COUNT, KEY_LEN_BYTES + IV_LEN_BYTES, |
| 1287 | ikey) != 1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1290 | static int scrypt(const char *passwd, const unsigned char *salt, |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1291 | unsigned char *ikey, void *params) |
| 1292 | { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1293 | SLOGI("Using scrypt for cryptfs KDF"); |
| 1294 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1295 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1296 | |
| 1297 | int N = 1 << ftr->N_factor; |
| 1298 | int r = 1 << ftr->r_factor; |
| 1299 | int p = 1 << ftr->p_factor; |
| 1300 | |
| 1301 | /* Turn the password into a key and IV that can decrypt the master key */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1302 | unsigned int keysize; |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1303 | crypto_scrypt((const uint8_t*)passwd, strlen(passwd), |
| 1304 | salt, SALT_LEN, N, r, p, ikey, |
| 1305 | KEY_LEN_BYTES + IV_LEN_BYTES); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1306 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1307 | return 0; |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1310 | static int scrypt_keymaster(const char *passwd, const unsigned char *salt, |
| 1311 | unsigned char *ikey, void *params) |
| 1312 | { |
| 1313 | SLOGI("Using scrypt with keymaster for cryptfs KDF"); |
| 1314 | |
| 1315 | int rc; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1316 | size_t signature_size; |
| 1317 | unsigned char* signature; |
| 1318 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1319 | |
| 1320 | int N = 1 << ftr->N_factor; |
| 1321 | int r = 1 << ftr->r_factor; |
| 1322 | int p = 1 << ftr->p_factor; |
| 1323 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1324 | rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd), |
| 1325 | salt, SALT_LEN, N, r, p, ikey, |
| 1326 | KEY_LEN_BYTES + IV_LEN_BYTES); |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1327 | |
| 1328 | if (rc) { |
| 1329 | SLOGE("scrypt failed"); |
| 1330 | return -1; |
| 1331 | } |
| 1332 | |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 1333 | if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES, |
| 1334 | &signature, &signature_size)) { |
| 1335 | SLOGE("Signing failed"); |
| 1336 | return -1; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN, |
| 1340 | N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES); |
| 1341 | free(signature); |
| 1342 | |
| 1343 | if (rc) { |
| 1344 | SLOGE("scrypt failed"); |
| 1345 | return -1; |
| 1346 | } |
| 1347 | |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | static int encrypt_master_key(const char *passwd, const unsigned char *salt, |
| 1352 | const unsigned char *decrypted_master_key, |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1353 | unsigned char *encrypted_master_key, |
| 1354 | struct crypt_mnt_ftr *crypt_ftr) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1355 | { |
| 1356 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
| 1357 | EVP_CIPHER_CTX e_ctx; |
| 1358 | int encrypted_len, final_len; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1359 | int rc = 0; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1360 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1361 | /* Turn the password into an intermediate key and IV that can decrypt the master key */ |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1362 | get_device_scrypt_params(crypt_ftr); |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1363 | |
| 1364 | switch (crypt_ftr->kdf_type) { |
| 1365 | case KDF_SCRYPT_KEYMASTER: |
| 1366 | if (keymaster_create_key(crypt_ftr)) { |
| 1367 | SLOGE("keymaster_create_key failed"); |
| 1368 | return -1; |
| 1369 | } |
| 1370 | |
| 1371 | if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) { |
| 1372 | SLOGE("scrypt failed"); |
| 1373 | return -1; |
| 1374 | } |
| 1375 | break; |
| 1376 | |
| 1377 | case KDF_SCRYPT: |
| 1378 | if (scrypt(passwd, salt, ikey, crypt_ftr)) { |
| 1379 | SLOGE("scrypt failed"); |
| 1380 | return -1; |
| 1381 | } |
| 1382 | break; |
| 1383 | |
| 1384 | default: |
| 1385 | SLOGE("Invalid kdf_type"); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1386 | return -1; |
| 1387 | } |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1388 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1389 | /* Initialize the decryption engine */ |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1390 | EVP_CIPHER_CTX_init(&e_ctx); |
| 1391 | if (! EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1392 | SLOGE("EVP_EncryptInit failed\n"); |
| 1393 | return -1; |
| 1394 | } |
| 1395 | EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1396 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1397 | /* Encrypt the master key */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1398 | if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len, |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 1399 | decrypted_master_key, KEY_LEN_BYTES)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1400 | SLOGE("EVP_EncryptUpdate failed\n"); |
| 1401 | return -1; |
| 1402 | } |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1403 | if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1404 | SLOGE("EVP_EncryptFinal failed\n"); |
| 1405 | return -1; |
| 1406 | } |
| 1407 | |
| 1408 | if (encrypted_len + final_len != KEY_LEN_BYTES) { |
| 1409 | SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len); |
| 1410 | return -1; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1411 | } |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1412 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1413 | /* Store the scrypt of the intermediate key, so we can validate if it's a |
| 1414 | password error or mount error when things go wrong. |
| 1415 | Note there's no need to check for errors, since if this is incorrect, we |
| 1416 | simply won't wipe userdata, which is the correct default behavior |
| 1417 | */ |
| 1418 | int N = 1 << crypt_ftr->N_factor; |
| 1419 | int r = 1 << crypt_ftr->r_factor; |
| 1420 | int p = 1 << crypt_ftr->p_factor; |
| 1421 | |
| 1422 | rc = crypto_scrypt(ikey, KEY_LEN_BYTES, |
| 1423 | crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p, |
| 1424 | crypt_ftr->scrypted_intermediate_key, |
| 1425 | sizeof(crypt_ftr->scrypted_intermediate_key)); |
| 1426 | |
| 1427 | if (rc) { |
| 1428 | SLOGE("encrypt_master_key: crypto_scrypt failed"); |
| 1429 | } |
| 1430 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1431 | return 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1432 | } |
| 1433 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 1434 | static int decrypt_master_key_aux(const char *passwd, unsigned char *salt, |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1435 | unsigned char *encrypted_master_key, |
| 1436 | unsigned char *decrypted_master_key, |
| 1437 | kdf_func kdf, void *kdf_params, |
| 1438 | unsigned char** intermediate_key, |
| 1439 | size_t* intermediate_key_size) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1440 | { |
| 1441 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1442 | EVP_CIPHER_CTX d_ctx; |
| 1443 | int decrypted_len, final_len; |
| 1444 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1445 | /* Turn the password into an intermediate key and IV that can decrypt the |
| 1446 | master key */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1447 | if (kdf(passwd, salt, ikey, kdf_params)) { |
| 1448 | SLOGE("kdf failed"); |
| 1449 | return -1; |
| 1450 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1451 | |
| 1452 | /* Initialize the decryption engine */ |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1453 | EVP_CIPHER_CTX_init(&d_ctx); |
| 1454 | if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1455 | return -1; |
| 1456 | } |
| 1457 | EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */ |
| 1458 | /* Decrypt the master key */ |
| 1459 | if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len, |
| 1460 | encrypted_master_key, KEY_LEN_BYTES)) { |
| 1461 | return -1; |
| 1462 | } |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1463 | if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1464 | return -1; |
| 1465 | } |
| 1466 | |
| 1467 | if (decrypted_len + final_len != KEY_LEN_BYTES) { |
| 1468 | return -1; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1469 | } |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1470 | |
| 1471 | /* Copy intermediate key if needed by params */ |
| 1472 | if (intermediate_key && intermediate_key_size) { |
| 1473 | *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES); |
| 1474 | if (intermediate_key) { |
| 1475 | memcpy(*intermediate_key, ikey, KEY_LEN_BYTES); |
| 1476 | *intermediate_key_size = KEY_LEN_BYTES; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | return 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1483 | static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1484 | { |
Paul Lawrence | db3730c | 2015-02-03 13:08:10 -0800 | [diff] [blame] | 1485 | if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1486 | *kdf = scrypt_keymaster; |
| 1487 | *kdf_params = ftr; |
| 1488 | } else if (ftr->kdf_type == KDF_SCRYPT) { |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1489 | *kdf = scrypt; |
| 1490 | *kdf_params = ftr; |
| 1491 | } else { |
| 1492 | *kdf = pbkdf2; |
| 1493 | *kdf_params = NULL; |
| 1494 | } |
| 1495 | } |
| 1496 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 1497 | static int decrypt_master_key(const char *passwd, unsigned char *decrypted_master_key, |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1498 | struct crypt_mnt_ftr *crypt_ftr, |
| 1499 | unsigned char** intermediate_key, |
| 1500 | size_t* intermediate_key_size) |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1501 | { |
| 1502 | kdf_func kdf; |
| 1503 | void *kdf_params; |
| 1504 | int ret; |
| 1505 | |
| 1506 | get_kdf_func(crypt_ftr, &kdf, &kdf_params); |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1507 | ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key, |
| 1508 | decrypted_master_key, kdf, kdf_params, |
| 1509 | intermediate_key, intermediate_key_size); |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1510 | if (ret != 0) { |
| 1511 | SLOGW("failure decrypting master key"); |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
| 1517 | static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt, |
| 1518 | struct crypt_mnt_ftr *crypt_ftr) { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1519 | int fd; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1520 | unsigned char key_buf[KEY_LEN_BYTES]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1521 | |
| 1522 | /* Get some random bits for a key */ |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1523 | fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1524 | read(fd, key_buf, sizeof(key_buf)); |
| 1525 | read(fd, salt, SALT_LEN); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1526 | close(fd); |
| 1527 | |
| 1528 | /* Now encrypt it with the password */ |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1529 | return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1530 | } |
| 1531 | |
Paul Lawrence | 2f32cda | 2015-05-05 14:28:25 -0700 | [diff] [blame] | 1532 | int wait_and_unmount(const char *mountpoint, bool kill) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1533 | { |
Greg Hackmann | 955653e | 2014-09-24 14:55:20 -0700 | [diff] [blame] | 1534 | int i, err, rc; |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 1535 | #define WAIT_UNMOUNT_COUNT 20 |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1536 | |
| 1537 | /* Now umount the tmpfs filesystem */ |
| 1538 | for (i=0; i<WAIT_UNMOUNT_COUNT; i++) { |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1539 | if (umount(mountpoint) == 0) { |
| 1540 | break; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1541 | } |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1542 | |
| 1543 | if (errno == EINVAL) { |
| 1544 | /* EINVAL is returned if the directory is not a mountpoint, |
| 1545 | * i.e. there is no filesystem mounted there. So just get out. |
| 1546 | */ |
| 1547 | break; |
| 1548 | } |
| 1549 | |
| 1550 | err = errno; |
| 1551 | |
| 1552 | /* If allowed, be increasingly aggressive before the last two retries */ |
| 1553 | if (kill) { |
| 1554 | if (i == (WAIT_UNMOUNT_COUNT - 3)) { |
| 1555 | SLOGW("sending SIGHUP to processes with open files\n"); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1556 | vold_killProcessesWithOpenFiles(mountpoint, SIGTERM); |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1557 | } else if (i == (WAIT_UNMOUNT_COUNT - 2)) { |
| 1558 | SLOGW("sending SIGKILL to processes with open files\n"); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 1559 | vold_killProcessesWithOpenFiles(mountpoint, SIGKILL); |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | sleep(1); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | if (i < WAIT_UNMOUNT_COUNT) { |
| 1567 | SLOGD("unmounting %s succeeded\n", mountpoint); |
| 1568 | rc = 0; |
| 1569 | } else { |
jessica_yu | 3f14fe4 | 2014-09-22 15:57:40 +0800 | [diff] [blame] | 1570 | vold_killProcessesWithOpenFiles(mountpoint, 0); |
Greg Hackmann | 955653e | 2014-09-24 14:55:20 -0700 | [diff] [blame] | 1571 | SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1572 | rc = -1; |
| 1573 | } |
| 1574 | |
| 1575 | return rc; |
| 1576 | } |
| 1577 | |
Paul Lawrence | b1ef466 | 2015-06-11 11:15:29 -0700 | [diff] [blame] | 1578 | #define DATA_PREP_TIMEOUT 1000 |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1579 | static int prep_data_fs(void) |
| 1580 | { |
| 1581 | int i; |
| 1582 | |
| 1583 | /* Do the prep of the /data filesystem */ |
| 1584 | property_set("vold.post_fs_data_done", "0"); |
| 1585 | property_set("vold.decrypt", "trigger_post_fs_data"); |
| 1586 | SLOGD("Just triggered post_fs_data\n"); |
| 1587 | |
Ken Sumrall | c587269 | 2013-05-14 15:26:31 -0700 | [diff] [blame] | 1588 | /* Wait a max of 50 seconds, hopefully it takes much less */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1589 | for (i=0; i<DATA_PREP_TIMEOUT; i++) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1590 | char p[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1591 | |
| 1592 | property_get("vold.post_fs_data_done", p, "0"); |
| 1593 | if (*p == '1') { |
| 1594 | break; |
| 1595 | } else { |
Paul Lawrence | b1ef466 | 2015-06-11 11:15:29 -0700 | [diff] [blame] | 1596 | usleep(50000); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | if (i == DATA_PREP_TIMEOUT) { |
| 1600 | /* Ugh, we failed to prep /data in time. Bail. */ |
Ken Sumrall | c587269 | 2013-05-14 15:26:31 -0700 | [diff] [blame] | 1601 | SLOGE("post_fs_data timed out!\n"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1602 | return -1; |
| 1603 | } else { |
| 1604 | SLOGD("post_fs_data done\n"); |
| 1605 | return 0; |
| 1606 | } |
| 1607 | } |
| 1608 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1609 | static void cryptfs_set_corrupt() |
| 1610 | { |
| 1611 | // Mark the footer as bad |
| 1612 | struct crypt_mnt_ftr crypt_ftr; |
| 1613 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 1614 | SLOGE("Failed to get crypto footer - panic"); |
| 1615 | return; |
| 1616 | } |
| 1617 | |
| 1618 | crypt_ftr.flags |= CRYPT_DATA_CORRUPT; |
| 1619 | if (put_crypt_ftr_and_key(&crypt_ftr)) { |
| 1620 | SLOGE("Failed to set crypto footer - panic"); |
| 1621 | return; |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | static void cryptfs_trigger_restart_min_framework() |
| 1626 | { |
| 1627 | if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) { |
| 1628 | SLOGE("Failed to mount tmpfs on data - panic"); |
| 1629 | return; |
| 1630 | } |
| 1631 | |
| 1632 | if (property_set("vold.decrypt", "trigger_post_fs_data")) { |
| 1633 | SLOGE("Failed to trigger post fs data - panic"); |
| 1634 | return; |
| 1635 | } |
| 1636 | |
| 1637 | if (property_set("vold.decrypt", "trigger_restart_min_framework")) { |
| 1638 | SLOGE("Failed to trigger restart min framework - panic"); |
| 1639 | return; |
| 1640 | } |
| 1641 | } |
| 1642 | |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 1643 | /* returns < 0 on failure */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1644 | static int cryptfs_restart_internal(int restart_main) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1645 | { |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1646 | char crypto_blkdev[MAXPATHLEN]; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 1647 | int rc = -1; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1648 | static int restart_successful = 0; |
| 1649 | |
| 1650 | /* Validate that it's OK to call this routine */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1651 | if (! master_key_saved) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1652 | SLOGE("Encrypted filesystem not validated, aborting"); |
| 1653 | return -1; |
| 1654 | } |
| 1655 | |
| 1656 | if (restart_successful) { |
| 1657 | SLOGE("System already restarted with encrypted disk, aborting"); |
| 1658 | return -1; |
| 1659 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1660 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1661 | if (restart_main) { |
| 1662 | /* Here is where we shut down the framework. The init scripts |
| 1663 | * start all services in one of three classes: core, main or late_start. |
| 1664 | * On boot, we start core and main. Now, we stop main, but not core, |
| 1665 | * as core includes vold and a few other really important things that |
| 1666 | * we need to keep running. Once main has stopped, we should be able |
| 1667 | * to umount the tmpfs /data, then mount the encrypted /data. |
| 1668 | * We then restart the class main, and also the class late_start. |
| 1669 | * At the moment, I've only put a few things in late_start that I know |
| 1670 | * are not needed to bring up the framework, and that also cause problems |
| 1671 | * with unmounting the tmpfs /data, but I hope to add add more services |
| 1672 | * to the late_start class as we optimize this to decrease the delay |
| 1673 | * till the user is asked for the password to the filesystem. |
| 1674 | */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1675 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1676 | /* The init files are setup to stop the class main when vold.decrypt is |
| 1677 | * set to trigger_reset_main. |
| 1678 | */ |
| 1679 | property_set("vold.decrypt", "trigger_reset_main"); |
| 1680 | SLOGD("Just asked init to shut down class main\n"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1681 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1682 | /* Ugh, shutting down the framework is not synchronous, so until it |
| 1683 | * can be fixed, this horrible hack will wait a moment for it all to |
| 1684 | * shut down before proceeding. Without it, some devices cannot |
| 1685 | * restart the graphics services. |
| 1686 | */ |
| 1687 | sleep(2); |
| 1688 | } |
Ken Sumrall | 9dedfd4 | 2012-10-09 14:16:59 -0700 | [diff] [blame] | 1689 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1690 | /* Now that the framework is shutdown, we should be able to umount() |
| 1691 | * the tmpfs filesystem, and mount the real one. |
| 1692 | */ |
| 1693 | |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1694 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, ""); |
| 1695 | if (strlen(crypto_blkdev) == 0) { |
| 1696 | SLOGE("fs_crypto_blkdev not set\n"); |
| 1697 | return -1; |
| 1698 | } |
| 1699 | |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1700 | if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) { |
Doug Zongker | 6fd5771 | 2013-12-17 09:43:23 -0800 | [diff] [blame] | 1701 | /* If ro.crypto.readonly is set to 1, mount the decrypted |
| 1702 | * filesystem readonly. This is used when /data is mounted by |
| 1703 | * recovery mode. |
| 1704 | */ |
| 1705 | char ro_prop[PROPERTY_VALUE_MAX]; |
| 1706 | property_get("ro.crypto.readonly", ro_prop, ""); |
| 1707 | if (strlen(ro_prop) > 0 && atoi(ro_prop)) { |
| 1708 | struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT); |
| 1709 | rec->flags |= MS_RDONLY; |
| 1710 | } |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 1711 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1712 | /* If that succeeded, then mount the decrypted filesystem */ |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 1713 | int retries = RETRY_MOUNT_ATTEMPTS; |
| 1714 | int mount_rc; |
| 1715 | while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT, |
| 1716 | crypto_blkdev, 0)) |
| 1717 | != 0) { |
| 1718 | if (mount_rc == FS_MGR_DOMNT_BUSY) { |
| 1719 | /* TODO: invoke something similar to |
| 1720 | Process::killProcessWithOpenFiles(DATA_MNT_POINT, |
| 1721 | retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */ |
| 1722 | SLOGI("Failed to mount %s because it is busy - waiting", |
| 1723 | crypto_blkdev); |
| 1724 | if (--retries) { |
| 1725 | sleep(RETRY_MOUNT_DELAY_SECONDS); |
| 1726 | } else { |
| 1727 | /* Let's hope that a reboot clears away whatever is keeping |
| 1728 | the mount busy */ |
| 1729 | cryptfs_reboot(reboot); |
| 1730 | } |
| 1731 | } else { |
| 1732 | SLOGE("Failed to mount decrypted data"); |
| 1733 | cryptfs_set_corrupt(); |
| 1734 | cryptfs_trigger_restart_min_framework(); |
| 1735 | SLOGI("Started framework to offer wipe"); |
| 1736 | return -1; |
| 1737 | } |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1738 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1739 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1740 | property_set("vold.decrypt", "trigger_load_persist_props"); |
| 1741 | /* Create necessary paths on /data */ |
| 1742 | if (prep_data_fs()) { |
| 1743 | return -1; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1744 | } |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1745 | |
| 1746 | /* startup service classes main and late_start */ |
| 1747 | property_set("vold.decrypt", "trigger_restart_framework"); |
| 1748 | SLOGD("Just triggered restart_framework\n"); |
| 1749 | |
| 1750 | /* Give it a few moments to get started */ |
| 1751 | sleep(1); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1752 | } |
| 1753 | |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1754 | if (rc == 0) { |
| 1755 | restart_successful = 1; |
| 1756 | } |
| 1757 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1758 | return rc; |
| 1759 | } |
| 1760 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1761 | int cryptfs_restart(void) |
| 1762 | { |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 1763 | SLOGI("cryptfs_restart"); |
| 1764 | if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) { |
| 1765 | struct fstab_rec* rec; |
| 1766 | int rc; |
| 1767 | |
| 1768 | if (e4crypt_restart(DATA_MNT_POINT)) { |
| 1769 | SLOGE("Can't unmount e4crypt temp volume\n"); |
| 1770 | return -1; |
| 1771 | } |
| 1772 | |
| 1773 | rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT); |
| 1774 | if (!rec) { |
| 1775 | SLOGE("Can't get fstab record for %s\n", DATA_MNT_POINT); |
| 1776 | return -1; |
| 1777 | } |
| 1778 | |
| 1779 | rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT, rec->blk_device, 0); |
| 1780 | if (rc) { |
| 1781 | SLOGE("Can't mount %s\n", DATA_MNT_POINT); |
| 1782 | return rc; |
| 1783 | } |
| 1784 | |
| 1785 | property_set("vold.decrypt", "trigger_restart_framework"); |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1789 | /* Call internal implementation forcing a restart of main service group */ |
| 1790 | return cryptfs_restart_internal(1); |
| 1791 | } |
| 1792 | |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 1793 | static int do_crypto_complete(char *mount_point) |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1794 | { |
| 1795 | struct crypt_mnt_ftr crypt_ftr; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1796 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1797 | char key_loc[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1798 | |
| 1799 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1800 | if (strcmp(encrypted_state, "encrypted") ) { |
| 1801 | SLOGE("not running with encryption, aborting"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1802 | return CRYPTO_COMPLETE_NOT_ENCRYPTED; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1803 | } |
| 1804 | |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 1805 | if (e4crypt_crypto_complete(mount_point) == 0) { |
| 1806 | return CRYPTO_COMPLETE_ENCRYPTED; |
| 1807 | } |
| 1808 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1809 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 1810 | fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc)); |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1811 | |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1812 | /* |
| 1813 | * Only report this error if key_loc is a file and it exists. |
| 1814 | * If the device was never encrypted, and /data is not mountable for |
| 1815 | * some reason, returning 1 should prevent the UI from presenting the |
| 1816 | * a "enter password" screen, or worse, a "press button to wipe the |
| 1817 | * device" screen. |
| 1818 | */ |
| 1819 | if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) { |
| 1820 | SLOGE("master key file does not exist, aborting"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1821 | return CRYPTO_COMPLETE_NOT_ENCRYPTED; |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1822 | } else { |
| 1823 | SLOGE("Error getting crypt footer and key\n"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1824 | return CRYPTO_COMPLETE_BAD_METADATA; |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1825 | } |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1826 | } |
| 1827 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1828 | // Test for possible error flags |
| 1829 | if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){ |
| 1830 | SLOGE("Encryption process is partway completed\n"); |
| 1831 | return CRYPTO_COMPLETE_PARTIAL; |
| 1832 | } |
| 1833 | |
| 1834 | if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){ |
| 1835 | SLOGE("Encryption process was interrupted but cannot continue\n"); |
| 1836 | return CRYPTO_COMPLETE_INCONSISTENT; |
| 1837 | } |
| 1838 | |
| 1839 | if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){ |
| 1840 | SLOGE("Encryption is successful but data is corrupt\n"); |
| 1841 | return CRYPTO_COMPLETE_CORRUPT; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | /* We passed the test! We shall diminish, and return to the west */ |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1845 | return CRYPTO_COMPLETE_ENCRYPTED; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1846 | } |
| 1847 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1848 | static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr, |
| 1849 | char *passwd, char *mount_point, char *label) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1850 | { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1851 | /* Allocate enough space for a 256 bit key, but we may use less */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1852 | unsigned char decrypted_master_key[32]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1853 | char crypto_blkdev[MAXPATHLEN]; |
| 1854 | char real_blkdev[MAXPATHLEN]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1855 | char tmp_mount_point[64]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1856 | unsigned int orig_failed_decrypt_count; |
| 1857 | int rc; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1858 | int use_keymaster = 0; |
| 1859 | int upgrade = 0; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1860 | unsigned char* intermediate_key = 0; |
| 1861 | size_t intermediate_key_size = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1862 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1863 | SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size); |
| 1864 | orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1865 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1866 | if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) { |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1867 | if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr, |
| 1868 | &intermediate_key, &intermediate_key_size)) { |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1869 | SLOGE("Failed to decrypt master key\n"); |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1870 | rc = -1; |
| 1871 | goto errout; |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1872 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1873 | } |
| 1874 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1875 | fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev)); |
| 1876 | |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 1877 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Iliyan Malchev | bb7d9af | 2014-11-20 18:42:23 -0800 | [diff] [blame] | 1878 | if (!strcmp((char *)crypt_ftr->crypto_type_name, "aes-xts")) { |
| 1879 | if(!set_hw_device_encryption_key(passwd, (char*) crypt_ftr->crypto_type_name)) { |
| 1880 | SLOGE("Hardware encryption key does not match"); |
| 1881 | } |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 1882 | } |
| 1883 | #endif |
| 1884 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1885 | // Create crypto block device - all (non fatal) code paths |
| 1886 | // need it |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1887 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, |
| 1888 | real_blkdev, crypto_blkdev, label)) { |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1889 | SLOGE("Error creating decrypted block device\n"); |
| 1890 | rc = -1; |
| 1891 | goto errout; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1892 | } |
| 1893 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1894 | /* Work out if the problem is the password or the data */ |
| 1895 | unsigned char scrypted_intermediate_key[sizeof(crypt_ftr-> |
| 1896 | scrypted_intermediate_key)]; |
| 1897 | int N = 1 << crypt_ftr->N_factor; |
| 1898 | int r = 1 << crypt_ftr->r_factor; |
| 1899 | int p = 1 << crypt_ftr->p_factor; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1900 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1901 | rc = crypto_scrypt(intermediate_key, intermediate_key_size, |
| 1902 | crypt_ftr->salt, sizeof(crypt_ftr->salt), |
| 1903 | N, r, p, scrypted_intermediate_key, |
| 1904 | sizeof(scrypted_intermediate_key)); |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1905 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1906 | // Does the key match the crypto footer? |
| 1907 | if (rc == 0 && memcmp(scrypted_intermediate_key, |
| 1908 | crypt_ftr->scrypted_intermediate_key, |
| 1909 | sizeof(scrypted_intermediate_key)) == 0) { |
| 1910 | SLOGI("Password matches"); |
| 1911 | rc = 0; |
| 1912 | } else { |
| 1913 | /* Try mounting the file system anyway, just in case the problem's with |
| 1914 | * the footer, not the key. */ |
| 1915 | sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point); |
| 1916 | mkdir(tmp_mount_point, 0755); |
| 1917 | if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) { |
| 1918 | SLOGE("Error temp mounting decrypted block device\n"); |
| 1919 | delete_crypto_blk_dev(label); |
| 1920 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1921 | rc = ++crypt_ftr->failed_decrypt_count; |
| 1922 | put_crypt_ftr_and_key(crypt_ftr); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1923 | } else { |
| 1924 | /* Success! */ |
| 1925 | SLOGI("Password did not match but decrypted drive mounted - continue"); |
| 1926 | umount(tmp_mount_point); |
| 1927 | rc = 0; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1928 | } |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | if (rc == 0) { |
| 1932 | crypt_ftr->failed_decrypt_count = 0; |
Paul Lawrence | 72b8b82 | 2014-10-05 12:57:37 -0700 | [diff] [blame] | 1933 | if (orig_failed_decrypt_count != 0) { |
| 1934 | put_crypt_ftr_and_key(crypt_ftr); |
| 1935 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1936 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1937 | /* Save the name of the crypto block device |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1938 | * so we can mount it when restarting the framework. */ |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1939 | property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1940 | |
| 1941 | /* Also save a the master key so we can reencrypted the key |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1942 | * the key when we want to change the password on it. */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1943 | memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 1944 | saved_mount_point = strdup(mount_point); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1945 | master_key_saved = 1; |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1946 | SLOGD("%s(): Master key saved\n", __FUNCTION__); |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1947 | rc = 0; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1948 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1949 | // Upgrade if we're not using the latest KDF. |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1950 | use_keymaster = keymaster_check_compatibility(); |
| 1951 | if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) { |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 1952 | // Don't allow downgrade |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1953 | } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) { |
| 1954 | crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER; |
| 1955 | upgrade = 1; |
| 1956 | } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1957 | crypt_ftr->kdf_type = KDF_SCRYPT; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1958 | upgrade = 1; |
| 1959 | } |
| 1960 | |
| 1961 | if (upgrade) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1962 | rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key, |
| 1963 | crypt_ftr->master_key, crypt_ftr); |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1964 | if (!rc) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1965 | rc = put_crypt_ftr_and_key(crypt_ftr); |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1966 | } |
| 1967 | SLOGD("Key Derivation Function upgrade: rc=%d\n", rc); |
Paul Lawrence | b2f682b | 2014-09-08 11:28:19 -0700 | [diff] [blame] | 1968 | |
| 1969 | // Do not fail even if upgrade failed - machine is bootable |
| 1970 | // Note that if this code is ever hit, there is a *serious* problem |
| 1971 | // since KDFs should never fail. You *must* fix the kdf before |
| 1972 | // proceeding! |
| 1973 | if (rc) { |
| 1974 | SLOGW("Upgrade failed with error %d," |
| 1975 | " but continuing with previous state", |
| 1976 | rc); |
| 1977 | rc = 0; |
| 1978 | } |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1979 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1980 | } |
| 1981 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1982 | errout: |
| 1983 | if (intermediate_key) { |
| 1984 | memset(intermediate_key, 0, intermediate_key_size); |
| 1985 | free(intermediate_key); |
| 1986 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1987 | return rc; |
| 1988 | } |
| 1989 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1990 | /* |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1991 | * Called by vold when it's asked to mount an encrypted external |
| 1992 | * storage volume. The incoming partition has no crypto header/footer, |
| 1993 | * as any metadata is been stored in a separate, small partition. |
| 1994 | * |
| 1995 | * out_crypto_blkdev must be MAXPATHLEN. |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1996 | */ |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1997 | int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev, |
| 1998 | const unsigned char* key, int keysize, char* out_crypto_blkdev) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1999 | int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC); |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 2000 | if (fd == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 2001 | SLOGE("Failed to open %s: %s", real_blkdev, strerror(errno)); |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 2002 | return -1; |
| 2003 | } |
| 2004 | |
| 2005 | unsigned long nr_sec = 0; |
| 2006 | get_blkdev_size(fd, &nr_sec); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2007 | close(fd); |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 2008 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2009 | if (nr_sec == 0) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 2010 | SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno)); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2011 | return -1; |
| 2012 | } |
| 2013 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 2014 | struct crypt_mnt_ftr ext_crypt_ftr; |
| 2015 | memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr)); |
| 2016 | ext_crypt_ftr.fs_size = nr_sec; |
| 2017 | ext_crypt_ftr.keysize = keysize; |
| 2018 | strcpy((char*) ext_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256"); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2019 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 2020 | return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev, |
| 2021 | out_crypto_blkdev, label); |
| 2022 | } |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2023 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 2024 | /* |
| 2025 | * Called by vold when it's asked to unmount an encrypted external |
| 2026 | * storage volume. |
| 2027 | */ |
| 2028 | int cryptfs_revert_ext_volume(const char* label) { |
| 2029 | return delete_crypto_blk_dev((char*) label); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 2032 | int cryptfs_crypto_complete(void) |
| 2033 | { |
| 2034 | return do_crypto_complete("/data"); |
| 2035 | } |
| 2036 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2037 | int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr) |
| 2038 | { |
| 2039 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 2040 | property_get("ro.crypto.state", encrypted_state, ""); |
| 2041 | if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) { |
| 2042 | SLOGE("encrypted fs already validated or not running with encryption," |
| 2043 | " aborting"); |
| 2044 | return -1; |
| 2045 | } |
| 2046 | |
| 2047 | if (get_crypt_ftr_and_key(crypt_ftr)) { |
| 2048 | SLOGE("Error getting crypt footer and key"); |
| 2049 | return -1; |
| 2050 | } |
| 2051 | |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2055 | int cryptfs_check_passwd(char *passwd) |
| 2056 | { |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 2057 | SLOGI("cryptfs_check_passwd"); |
| 2058 | if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) { |
| 2059 | return e4crypt_check_passwd(DATA_MNT_POINT, passwd); |
| 2060 | } |
| 2061 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2062 | struct crypt_mnt_ftr crypt_ftr; |
| 2063 | int rc; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2064 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2065 | rc = check_unmounted_and_get_ftr(&crypt_ftr); |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2066 | if (rc) { |
| 2067 | SLOGE("Could not get footer"); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2068 | return rc; |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2069 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2070 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 2071 | rc = test_mount_encrypted_fs(&crypt_ftr, passwd, |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2072 | DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE); |
| 2073 | if (rc) { |
| 2074 | SLOGE("Password did not match"); |
| 2075 | return rc; |
| 2076 | } |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 2077 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2078 | if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) { |
| 2079 | // Here we have a default actual password but a real password |
| 2080 | // we must test against the scrypted value |
| 2081 | // First, we must delete the crypto block device that |
| 2082 | // test_mount_encrypted_fs leaves behind as a side effect |
| 2083 | delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE); |
| 2084 | rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD, |
| 2085 | DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE); |
| 2086 | if (rc) { |
| 2087 | SLOGE("Default password did not match on reboot encryption"); |
| 2088 | return rc; |
| 2089 | } |
| 2090 | |
| 2091 | crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE; |
| 2092 | put_crypt_ftr_and_key(&crypt_ftr); |
| 2093 | rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd); |
| 2094 | if (rc) { |
| 2095 | SLOGE("Could not change password on reboot encryption"); |
| 2096 | return rc; |
| 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) { |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 2101 | cryptfs_clear_password(); |
| 2102 | password = strdup(passwd); |
| 2103 | struct timespec now; |
| 2104 | clock_gettime(CLOCK_BOOTTIME, &now); |
| 2105 | password_expiry_time = now.tv_sec + password_max_age_seconds; |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 2106 | } |
| 2107 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2108 | return rc; |
| 2109 | } |
| 2110 | |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 2111 | int cryptfs_verify_passwd(char *passwd) |
| 2112 | { |
| 2113 | struct crypt_mnt_ftr crypt_ftr; |
| 2114 | /* Allocate enough space for a 256 bit key, but we may use less */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2115 | unsigned char decrypted_master_key[32]; |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 2116 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 2117 | int rc; |
| 2118 | |
| 2119 | property_get("ro.crypto.state", encrypted_state, ""); |
| 2120 | if (strcmp(encrypted_state, "encrypted") ) { |
| 2121 | SLOGE("device not encrypted, aborting"); |
| 2122 | return -2; |
| 2123 | } |
| 2124 | |
| 2125 | if (!master_key_saved) { |
| 2126 | SLOGE("encrypted fs not yet mounted, aborting"); |
| 2127 | return -1; |
| 2128 | } |
| 2129 | |
| 2130 | if (!saved_mount_point) { |
| 2131 | SLOGE("encrypted fs failed to save mount point, aborting"); |
| 2132 | return -1; |
| 2133 | } |
| 2134 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2135 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 2136 | SLOGE("Error getting crypt footer and key\n"); |
| 2137 | return -1; |
| 2138 | } |
| 2139 | |
| 2140 | if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) { |
| 2141 | /* If the device has no password, then just say the password is valid */ |
| 2142 | rc = 0; |
| 2143 | } else { |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 2144 | decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 2145 | if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) { |
| 2146 | /* They match, the password is correct */ |
| 2147 | rc = 0; |
| 2148 | } else { |
| 2149 | /* If incorrect, sleep for a bit to prevent dictionary attacks */ |
| 2150 | sleep(1); |
| 2151 | rc = 1; |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | return rc; |
| 2156 | } |
| 2157 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2158 | /* Initialize a crypt_mnt_ftr structure. The keysize is |
| 2159 | * defaulted to 16 bytes, and the filesystem size to 0. |
| 2160 | * Presumably, at a minimum, the caller will update the |
| 2161 | * filesystem size and crypto_type_name after calling this function. |
| 2162 | */ |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 2163 | static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2164 | { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2165 | off64_t off; |
| 2166 | |
| 2167 | memset(ftr, 0, sizeof(struct crypt_mnt_ftr)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2168 | ftr->magic = CRYPT_MNT_MAGIC; |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 2169 | ftr->major_version = CURRENT_MAJOR_VERSION; |
| 2170 | ftr->minor_version = CURRENT_MINOR_VERSION; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2171 | ftr->ftr_size = sizeof(struct crypt_mnt_ftr); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 2172 | ftr->keysize = KEY_LEN_BYTES; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2173 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 2174 | switch (keymaster_check_compatibility()) { |
| 2175 | case 1: |
| 2176 | ftr->kdf_type = KDF_SCRYPT_KEYMASTER; |
| 2177 | break; |
| 2178 | |
| 2179 | case 0: |
| 2180 | ftr->kdf_type = KDF_SCRYPT; |
| 2181 | break; |
| 2182 | |
| 2183 | default: |
| 2184 | SLOGE("keymaster_check_compatibility failed"); |
| 2185 | return -1; |
| 2186 | } |
| 2187 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 2188 | get_device_scrypt_params(ftr); |
| 2189 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2190 | ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE; |
| 2191 | if (get_crypt_ftr_info(NULL, &off) == 0) { |
| 2192 | ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET; |
| 2193 | ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET + |
| 2194 | ftr->persist_data_size; |
| 2195 | } |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 2196 | |
| 2197 | return 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2198 | } |
| 2199 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2200 | static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2201 | { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 2202 | const char *args[10]; |
| 2203 | char size_str[32]; /* Must be large enough to hold a %lld and null byte */ |
| 2204 | int num_args; |
| 2205 | int status; |
| 2206 | int tmp; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2207 | int rc = -1; |
| 2208 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2209 | if (type == EXT4_FS) { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 2210 | args[0] = "/system/bin/make_ext4fs"; |
| 2211 | args[1] = "-a"; |
| 2212 | args[2] = "/data"; |
| 2213 | args[3] = "-l"; |
Elliott Hughes | 7373716 | 2014-06-25 17:27:42 -0700 | [diff] [blame] | 2214 | snprintf(size_str, sizeof(size_str), "%" PRId64, size * 512); |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 2215 | args[4] = size_str; |
| 2216 | args[5] = crypto_blkdev; |
| 2217 | num_args = 6; |
| 2218 | SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n", |
| 2219 | args[0], args[1], args[2], args[3], args[4], args[5]); |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 2220 | } else if (type == F2FS_FS) { |
| 2221 | args[0] = "/system/bin/mkfs.f2fs"; |
| 2222 | args[1] = "-t"; |
| 2223 | args[2] = "-d1"; |
| 2224 | args[3] = crypto_blkdev; |
Elliott Hughes | 7373716 | 2014-06-25 17:27:42 -0700 | [diff] [blame] | 2225 | snprintf(size_str, sizeof(size_str), "%" PRId64, size); |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 2226 | args[4] = size_str; |
| 2227 | num_args = 5; |
| 2228 | SLOGI("Making empty filesystem with command %s %s %s %s %s\n", |
| 2229 | args[0], args[1], args[2], args[3], args[4]); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2230 | } else { |
| 2231 | SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type); |
| 2232 | return -1; |
| 2233 | } |
| 2234 | |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 2235 | tmp = android_fork_execvp(num_args, (char **)args, &status, false, true); |
| 2236 | |
| 2237 | if (tmp != 0) { |
| 2238 | SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2239 | } else { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 2240 | if (WIFEXITED(status)) { |
| 2241 | if (WEXITSTATUS(status)) { |
| 2242 | SLOGE("Error creating filesystem on %s, exit status %d ", |
| 2243 | crypto_blkdev, WEXITSTATUS(status)); |
| 2244 | } else { |
| 2245 | SLOGD("Successfully created filesystem on %s\n", crypto_blkdev); |
| 2246 | rc = 0; |
| 2247 | } |
| 2248 | } else { |
| 2249 | SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev); |
| 2250 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | return rc; |
| 2254 | } |
| 2255 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2256 | #define CRYPT_INPLACE_BUFSIZE 4096 |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2257 | #define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE) |
| 2258 | #define CRYPT_SECTOR_SIZE 512 |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2259 | |
| 2260 | /* aligned 32K writes tends to make flash happy. |
| 2261 | * SD card association recommends it. |
| 2262 | */ |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 2263 | #ifndef CONFIG_HW_DISK_ENCRYPTION |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2264 | #define BLOCKS_AT_A_TIME 8 |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 2265 | #else |
| 2266 | #define BLOCKS_AT_A_TIME 1024 |
| 2267 | #endif |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2268 | |
| 2269 | struct encryptGroupsData |
| 2270 | { |
| 2271 | int realfd; |
| 2272 | int cryptofd; |
| 2273 | off64_t numblocks; |
| 2274 | off64_t one_pct, cur_pct, new_pct; |
| 2275 | off64_t blocks_already_done, tot_numblocks; |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2276 | off64_t used_blocks_already_done, tot_used_blocks; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2277 | char* real_blkdev, * crypto_blkdev; |
| 2278 | int count; |
| 2279 | off64_t offset; |
| 2280 | char* buffer; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2281 | off64_t last_written_sector; |
| 2282 | int completed; |
Paul Lawrence | a96d9c9 | 2014-06-04 14:05:01 -0700 | [diff] [blame] | 2283 | time_t time_started; |
| 2284 | int remaining_time; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2285 | }; |
| 2286 | |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2287 | static void update_progress(struct encryptGroupsData* data, int is_used) |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2288 | { |
| 2289 | data->blocks_already_done++; |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2290 | |
| 2291 | if (is_used) { |
| 2292 | data->used_blocks_already_done++; |
| 2293 | } |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2294 | if (data->tot_used_blocks) { |
| 2295 | data->new_pct = data->used_blocks_already_done / data->one_pct; |
| 2296 | } else { |
| 2297 | data->new_pct = data->blocks_already_done / data->one_pct; |
| 2298 | } |
| 2299 | |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2300 | if (data->new_pct > data->cur_pct) { |
| 2301 | char buf[8]; |
| 2302 | data->cur_pct = data->new_pct; |
Elliott Hughes | cb33f57 | 2014-06-25 18:25:11 -0700 | [diff] [blame] | 2303 | snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2304 | property_set("vold.encrypt_progress", buf); |
| 2305 | } |
Paul Lawrence | a96d9c9 | 2014-06-04 14:05:01 -0700 | [diff] [blame] | 2306 | |
| 2307 | if (data->cur_pct >= 5) { |
Paul Lawrence | 9c58a87 | 2014-09-30 09:12:51 -0700 | [diff] [blame] | 2308 | struct timespec time_now; |
| 2309 | if (clock_gettime(CLOCK_MONOTONIC, &time_now)) { |
| 2310 | SLOGW("Error getting time"); |
| 2311 | } else { |
| 2312 | double elapsed_time = difftime(time_now.tv_sec, data->time_started); |
| 2313 | off64_t remaining_blocks = data->tot_used_blocks |
| 2314 | - data->used_blocks_already_done; |
| 2315 | int remaining_time = (int)(elapsed_time * remaining_blocks |
| 2316 | / data->used_blocks_already_done); |
Paul Lawrence | 7157750 | 2014-08-13 14:55:55 -0700 | [diff] [blame] | 2317 | |
Paul Lawrence | 9c58a87 | 2014-09-30 09:12:51 -0700 | [diff] [blame] | 2318 | // Change time only if not yet set, lower, or a lot higher for |
| 2319 | // best user experience |
| 2320 | if (data->remaining_time == -1 |
| 2321 | || remaining_time < data->remaining_time |
| 2322 | || remaining_time > data->remaining_time + 60) { |
| 2323 | char buf[8]; |
| 2324 | snprintf(buf, sizeof(buf), "%d", remaining_time); |
| 2325 | property_set("vold.encrypt_time_remaining", buf); |
| 2326 | data->remaining_time = remaining_time; |
| 2327 | } |
Paul Lawrence | a96d9c9 | 2014-06-04 14:05:01 -0700 | [diff] [blame] | 2328 | } |
| 2329 | } |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2330 | } |
| 2331 | |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2332 | static void log_progress(struct encryptGroupsData const* data, bool completed) |
| 2333 | { |
| 2334 | // Precondition - if completed data = 0 else data != 0 |
| 2335 | |
| 2336 | // Track progress so we can skip logging blocks |
| 2337 | static off64_t offset = -1; |
| 2338 | |
| 2339 | // Need to close existing 'Encrypting from' log? |
| 2340 | if (completed || (offset != -1 && data->offset != offset)) { |
| 2341 | SLOGI("Encrypted to sector %" PRId64, |
| 2342 | offset / info.block_size * CRYPT_SECTOR_SIZE); |
| 2343 | offset = -1; |
| 2344 | } |
| 2345 | |
| 2346 | // Need to start new 'Encrypting from' log? |
| 2347 | if (!completed && offset != data->offset) { |
| 2348 | SLOGI("Encrypting from sector %" PRId64, |
| 2349 | data->offset / info.block_size * CRYPT_SECTOR_SIZE); |
| 2350 | } |
| 2351 | |
| 2352 | // Update offset |
| 2353 | if (!completed) { |
| 2354 | offset = data->offset + (off64_t)data->count * info.block_size; |
| 2355 | } |
| 2356 | } |
| 2357 | |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2358 | static int flush_outstanding_data(struct encryptGroupsData* data) |
| 2359 | { |
| 2360 | if (data->count == 0) { |
| 2361 | return 0; |
| 2362 | } |
| 2363 | |
Elliott Hughes | 231bdba | 2014-06-25 18:36:19 -0700 | [diff] [blame] | 2364 | SLOGV("Copying %d blocks at offset %" PRIx64, data->count, data->offset); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2365 | |
| 2366 | if (pread64(data->realfd, data->buffer, |
| 2367 | info.block_size * data->count, data->offset) |
| 2368 | <= 0) { |
| 2369 | SLOGE("Error reading real_blkdev %s for inplace encrypt", |
| 2370 | data->real_blkdev); |
| 2371 | return -1; |
| 2372 | } |
| 2373 | |
| 2374 | if (pwrite64(data->cryptofd, data->buffer, |
| 2375 | info.block_size * data->count, data->offset) |
| 2376 | <= 0) { |
| 2377 | SLOGE("Error writing crypto_blkdev %s for inplace encrypt", |
| 2378 | data->crypto_blkdev); |
| 2379 | return -1; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2380 | } else { |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2381 | log_progress(data, false); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | data->count = 0; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2385 | data->last_written_sector = (data->offset + data->count) |
| 2386 | / info.block_size * CRYPT_SECTOR_SIZE - 1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2387 | return 0; |
| 2388 | } |
| 2389 | |
| 2390 | static int encrypt_groups(struct encryptGroupsData* data) |
| 2391 | { |
| 2392 | unsigned int i; |
| 2393 | u8 *block_bitmap = 0; |
| 2394 | unsigned int block; |
| 2395 | off64_t ret; |
| 2396 | int rc = -1; |
| 2397 | |
| 2398 | data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME); |
| 2399 | if (!data->buffer) { |
| 2400 | SLOGE("Failed to allocate crypto buffer"); |
| 2401 | goto errout; |
| 2402 | } |
| 2403 | |
| 2404 | block_bitmap = malloc(info.block_size); |
| 2405 | if (!block_bitmap) { |
| 2406 | SLOGE("failed to allocate block bitmap"); |
| 2407 | goto errout; |
| 2408 | } |
| 2409 | |
| 2410 | for (i = 0; i < aux_info.groups; ++i) { |
| 2411 | SLOGI("Encrypting group %d", i); |
| 2412 | |
| 2413 | u32 first_block = aux_info.first_data_block + i * info.blocks_per_group; |
| 2414 | u32 block_count = min(info.blocks_per_group, |
| 2415 | aux_info.len_blocks - first_block); |
| 2416 | |
| 2417 | off64_t offset = (u64)info.block_size |
| 2418 | * aux_info.bg_desc[i].bg_block_bitmap; |
| 2419 | |
| 2420 | ret = pread64(data->realfd, block_bitmap, info.block_size, offset); |
| 2421 | if (ret != (int)info.block_size) { |
| 2422 | SLOGE("failed to read all of block group bitmap %d", i); |
| 2423 | goto errout; |
| 2424 | } |
| 2425 | |
| 2426 | offset = (u64)info.block_size * first_block; |
| 2427 | |
| 2428 | data->count = 0; |
| 2429 | |
| 2430 | for (block = 0; block < block_count; block++) { |
liminghao | aa08e58 | 2016-01-06 10:30:49 +0800 | [diff] [blame] | 2431 | int used = (aux_info.bg_desc[i].bg_flags & EXT4_BG_BLOCK_UNINIT) ? |
| 2432 | 0 : bitmap_get_bit(block_bitmap, block); |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2433 | update_progress(data, used); |
| 2434 | if (used) { |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2435 | if (data->count == 0) { |
| 2436 | data->offset = offset; |
| 2437 | } |
| 2438 | data->count++; |
| 2439 | } else { |
| 2440 | if (flush_outstanding_data(data)) { |
| 2441 | goto errout; |
| 2442 | } |
| 2443 | } |
| 2444 | |
| 2445 | offset += info.block_size; |
| 2446 | |
| 2447 | /* Write data if we are aligned or buffer size reached */ |
| 2448 | if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0 |
| 2449 | || data->count == BLOCKS_AT_A_TIME) { |
| 2450 | if (flush_outstanding_data(data)) { |
| 2451 | goto errout; |
| 2452 | } |
| 2453 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2454 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2455 | if (!is_battery_ok_to_continue()) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2456 | SLOGE("Stopping encryption due to low battery"); |
| 2457 | rc = 0; |
| 2458 | goto errout; |
| 2459 | } |
| 2460 | |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2461 | } |
| 2462 | if (flush_outstanding_data(data)) { |
| 2463 | goto errout; |
| 2464 | } |
| 2465 | } |
| 2466 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2467 | data->completed = 1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2468 | rc = 0; |
| 2469 | |
| 2470 | errout: |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2471 | log_progress(0, true); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2472 | free(data->buffer); |
| 2473 | free(block_bitmap); |
| 2474 | return rc; |
| 2475 | } |
| 2476 | |
| 2477 | static int cryptfs_enable_inplace_ext4(char *crypto_blkdev, |
| 2478 | char *real_blkdev, |
| 2479 | off64_t size, |
| 2480 | off64_t *size_already_done, |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2481 | off64_t tot_size, |
| 2482 | off64_t previously_encrypted_upto) |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2483 | { |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2484 | u32 i; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2485 | struct encryptGroupsData data; |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 2486 | int rc; // Can't initialize without causing warning -Wclobbered |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2487 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2488 | if (previously_encrypted_upto > *size_already_done) { |
| 2489 | SLOGD("Not fast encrypting since resuming part way through"); |
| 2490 | return -1; |
| 2491 | } |
| 2492 | |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2493 | memset(&data, 0, sizeof(data)); |
| 2494 | data.real_blkdev = real_blkdev; |
| 2495 | data.crypto_blkdev = crypto_blkdev; |
| 2496 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 2497 | if ( (data.realfd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) { |
JP Abgrall | 3334c6a | 2014-10-10 15:52:11 -0700 | [diff] [blame] | 2498 | SLOGE("Error opening real_blkdev %s for inplace encrypt. err=%d(%s)\n", |
| 2499 | real_blkdev, errno, strerror(errno)); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 2500 | rc = -1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2501 | goto errout; |
| 2502 | } |
| 2503 | |
David Ng | 82fd804 | 2015-01-21 13:55:21 -0800 | [diff] [blame] | 2504 | // Wait until the block device appears. Re-use the mount retry values since it is reasonable. |
| 2505 | int retries = RETRY_MOUNT_ATTEMPTS; |
| 2506 | while ((data.cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) { |
| 2507 | if (--retries) { |
| 2508 | SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s), retrying\n", |
| 2509 | crypto_blkdev, errno, strerror(errno)); |
| 2510 | sleep(RETRY_MOUNT_DELAY_SECONDS); |
| 2511 | } else { |
| 2512 | SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s)\n", |
| 2513 | crypto_blkdev, errno, strerror(errno)); |
| 2514 | rc = ENABLE_INPLACE_ERR_DEV; |
| 2515 | goto errout; |
| 2516 | } |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | if (setjmp(setjmp_env)) { |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2520 | SLOGE("Reading ext4 extent caused an exception\n"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 2521 | rc = -1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2522 | goto errout; |
| 2523 | } |
| 2524 | |
| 2525 | if (read_ext(data.realfd, 0) != 0) { |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2526 | SLOGE("Failed to read ext4 extent\n"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 2527 | rc = -1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2528 | goto errout; |
| 2529 | } |
| 2530 | |
| 2531 | data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2532 | data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2533 | data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE; |
| 2534 | |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2535 | SLOGI("Encrypting ext4 filesystem in place..."); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2536 | |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2537 | data.tot_used_blocks = data.numblocks; |
| 2538 | for (i = 0; i < aux_info.groups; ++i) { |
| 2539 | data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count; |
| 2540 | } |
| 2541 | |
| 2542 | data.one_pct = data.tot_used_blocks / 100; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2543 | data.cur_pct = 0; |
Paul Lawrence | 9c58a87 | 2014-09-30 09:12:51 -0700 | [diff] [blame] | 2544 | |
| 2545 | struct timespec time_started = {0}; |
| 2546 | if (clock_gettime(CLOCK_MONOTONIC, &time_started)) { |
| 2547 | SLOGW("Error getting time at start"); |
| 2548 | // Note - continue anyway - we'll run with 0 |
| 2549 | } |
| 2550 | data.time_started = time_started.tv_sec; |
Paul Lawrence | a96d9c9 | 2014-06-04 14:05:01 -0700 | [diff] [blame] | 2551 | data.remaining_time = -1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2552 | |
| 2553 | rc = encrypt_groups(&data); |
| 2554 | if (rc) { |
| 2555 | SLOGE("Error encrypting groups"); |
| 2556 | goto errout; |
| 2557 | } |
| 2558 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2559 | *size_already_done += data.completed ? size : data.last_written_sector; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2560 | rc = 0; |
| 2561 | |
| 2562 | errout: |
| 2563 | close(data.realfd); |
| 2564 | close(data.cryptofd); |
| 2565 | |
| 2566 | return rc; |
| 2567 | } |
| 2568 | |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2569 | static void log_progress_f2fs(u64 block, bool completed) |
| 2570 | { |
| 2571 | // Precondition - if completed data = 0 else data != 0 |
| 2572 | |
| 2573 | // Track progress so we can skip logging blocks |
| 2574 | static u64 last_block = (u64)-1; |
| 2575 | |
| 2576 | // Need to close existing 'Encrypting from' log? |
| 2577 | if (completed || (last_block != (u64)-1 && block != last_block + 1)) { |
| 2578 | SLOGI("Encrypted to block %" PRId64, last_block); |
| 2579 | last_block = -1; |
| 2580 | } |
| 2581 | |
| 2582 | // Need to start new 'Encrypting from' log? |
| 2583 | if (!completed && (last_block == (u64)-1 || block != last_block + 1)) { |
| 2584 | SLOGI("Encrypting from block %" PRId64, block); |
| 2585 | } |
| 2586 | |
| 2587 | // Update offset |
| 2588 | if (!completed) { |
| 2589 | last_block = block; |
| 2590 | } |
| 2591 | } |
| 2592 | |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2593 | static int encrypt_one_block_f2fs(u64 pos, void *data) |
| 2594 | { |
| 2595 | struct encryptGroupsData *priv_dat = (struct encryptGroupsData *)data; |
| 2596 | |
| 2597 | priv_dat->blocks_already_done = pos - 1; |
| 2598 | update_progress(priv_dat, 1); |
| 2599 | |
| 2600 | off64_t offset = pos * CRYPT_INPLACE_BUFSIZE; |
| 2601 | |
| 2602 | if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) { |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2603 | SLOGE("Error reading real_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2604 | return -1; |
| 2605 | } |
| 2606 | |
| 2607 | if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) { |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2608 | SLOGE("Error writing crypto_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2609 | return -1; |
| 2610 | } else { |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2611 | log_progress_f2fs(pos, false); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
| 2617 | static int cryptfs_enable_inplace_f2fs(char *crypto_blkdev, |
| 2618 | char *real_blkdev, |
| 2619 | off64_t size, |
| 2620 | off64_t *size_already_done, |
| 2621 | off64_t tot_size, |
| 2622 | off64_t previously_encrypted_upto) |
| 2623 | { |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2624 | struct encryptGroupsData data; |
| 2625 | struct f2fs_info *f2fs_info = NULL; |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2626 | int rc = ENABLE_INPLACE_ERR_OTHER; |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2627 | if (previously_encrypted_upto > *size_already_done) { |
| 2628 | SLOGD("Not fast encrypting since resuming part way through"); |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2629 | return ENABLE_INPLACE_ERR_OTHER; |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2630 | } |
| 2631 | memset(&data, 0, sizeof(data)); |
| 2632 | data.real_blkdev = real_blkdev; |
| 2633 | data.crypto_blkdev = crypto_blkdev; |
| 2634 | data.realfd = -1; |
| 2635 | data.cryptofd = -1; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 2636 | if ( (data.realfd = open64(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) { |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2637 | SLOGE("Error opening real_blkdev %s for f2fs inplace encrypt\n", |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2638 | real_blkdev); |
| 2639 | goto errout; |
| 2640 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 2641 | if ( (data.cryptofd = open64(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) { |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2642 | SLOGE("Error opening crypto_blkdev %s for f2fs inplace encrypt. err=%d(%s)\n", |
JP Abgrall | 3334c6a | 2014-10-10 15:52:11 -0700 | [diff] [blame] | 2643 | crypto_blkdev, errno, strerror(errno)); |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2644 | rc = ENABLE_INPLACE_ERR_DEV; |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2645 | goto errout; |
| 2646 | } |
| 2647 | |
| 2648 | f2fs_info = generate_f2fs_info(data.realfd); |
| 2649 | if (!f2fs_info) |
| 2650 | goto errout; |
| 2651 | |
| 2652 | data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2653 | data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2654 | data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE; |
| 2655 | |
| 2656 | data.tot_used_blocks = get_num_blocks_used(f2fs_info); |
| 2657 | |
| 2658 | data.one_pct = data.tot_used_blocks / 100; |
| 2659 | data.cur_pct = 0; |
| 2660 | data.time_started = time(NULL); |
| 2661 | data.remaining_time = -1; |
| 2662 | |
| 2663 | data.buffer = malloc(f2fs_info->block_size); |
| 2664 | if (!data.buffer) { |
| 2665 | SLOGE("Failed to allocate crypto buffer"); |
| 2666 | goto errout; |
| 2667 | } |
| 2668 | |
| 2669 | data.count = 0; |
| 2670 | |
| 2671 | /* Currently, this either runs to completion, or hits a nonrecoverable error */ |
| 2672 | rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data); |
| 2673 | |
| 2674 | if (rc) { |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2675 | SLOGE("Error in running over f2fs blocks"); |
| 2676 | rc = ENABLE_INPLACE_ERR_OTHER; |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2677 | goto errout; |
| 2678 | } |
| 2679 | |
| 2680 | *size_already_done += size; |
| 2681 | rc = 0; |
| 2682 | |
| 2683 | errout: |
| 2684 | if (rc) |
| 2685 | SLOGE("Failed to encrypt f2fs filesystem on %s", real_blkdev); |
| 2686 | |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2687 | log_progress_f2fs(0, true); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2688 | free(f2fs_info); |
| 2689 | free(data.buffer); |
| 2690 | close(data.realfd); |
| 2691 | close(data.cryptofd); |
| 2692 | |
| 2693 | return rc; |
| 2694 | } |
| 2695 | |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2696 | static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev, |
| 2697 | off64_t size, off64_t *size_already_done, |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2698 | off64_t tot_size, |
| 2699 | off64_t previously_encrypted_upto) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2700 | { |
| 2701 | int realfd, cryptofd; |
| 2702 | char *buf[CRYPT_INPLACE_BUFSIZE]; |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2703 | int rc = ENABLE_INPLACE_ERR_OTHER; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2704 | off64_t numblocks, i, remainder; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2705 | off64_t one_pct, cur_pct, new_pct; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2706 | off64_t blocks_already_done, tot_numblocks; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2707 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 2708 | if ( (realfd = open(real_blkdev, O_RDONLY|O_CLOEXEC)) < 0) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2709 | SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev); |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2710 | return ENABLE_INPLACE_ERR_OTHER; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2711 | } |
| 2712 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 2713 | if ( (cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) { |
JP Abgrall | 3334c6a | 2014-10-10 15:52:11 -0700 | [diff] [blame] | 2714 | SLOGE("Error opening crypto_blkdev %s for inplace encrypt. err=%d(%s)\n", |
| 2715 | crypto_blkdev, errno, strerror(errno)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2716 | close(realfd); |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2717 | return ENABLE_INPLACE_ERR_DEV; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | /* This is pretty much a simple loop of reading 4K, and writing 4K. |
| 2721 | * The size passed in is the number of 512 byte sectors in the filesystem. |
| 2722 | * So compute the number of whole 4K blocks we should read/write, |
| 2723 | * and the remainder. |
| 2724 | */ |
| 2725 | numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2726 | remainder = size % CRYPT_SECTORS_PER_BUFSIZE; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2727 | tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2728 | blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2729 | |
| 2730 | SLOGE("Encrypting filesystem in place..."); |
| 2731 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2732 | i = previously_encrypted_upto + 1 - *size_already_done; |
| 2733 | |
| 2734 | if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) { |
| 2735 | SLOGE("Cannot seek to previously encrypted point on %s", real_blkdev); |
| 2736 | goto errout; |
| 2737 | } |
| 2738 | |
| 2739 | if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) { |
| 2740 | SLOGE("Cannot seek to previously encrypted point on %s", crypto_blkdev); |
| 2741 | goto errout; |
| 2742 | } |
| 2743 | |
| 2744 | for (;i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) { |
| 2745 | if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
| 2746 | SLOGE("Error reading initial sectors from real_blkdev %s for " |
| 2747 | "inplace encrypt\n", crypto_blkdev); |
| 2748 | goto errout; |
| 2749 | } |
| 2750 | if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
| 2751 | SLOGE("Error writing initial sectors to crypto_blkdev %s for " |
| 2752 | "inplace encrypt\n", crypto_blkdev); |
| 2753 | goto errout; |
| 2754 | } else { |
Elliott Hughes | cb33f57 | 2014-06-25 18:25:11 -0700 | [diff] [blame] | 2755 | SLOGI("Encrypted 1 block at %" PRId64, i); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2759 | one_pct = tot_numblocks / 100; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2760 | cur_pct = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2761 | /* process the majority of the filesystem in blocks */ |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2762 | for (i/=CRYPT_SECTORS_PER_BUFSIZE; i<numblocks; i++) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2763 | new_pct = (i + blocks_already_done) / one_pct; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2764 | if (new_pct > cur_pct) { |
| 2765 | char buf[8]; |
| 2766 | |
| 2767 | cur_pct = new_pct; |
Elliott Hughes | 7373716 | 2014-06-25 17:27:42 -0700 | [diff] [blame] | 2768 | snprintf(buf, sizeof(buf), "%" PRId64, cur_pct); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2769 | property_set("vold.encrypt_progress", buf); |
| 2770 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2771 | if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2772 | SLOGE("Error reading real_blkdev %s for inplace encrypt", crypto_blkdev); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2773 | goto errout; |
| 2774 | } |
| 2775 | if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2776 | SLOGE("Error writing crypto_blkdev %s for inplace encrypt", crypto_blkdev); |
| 2777 | goto errout; |
| 2778 | } else { |
Elliott Hughes | cb33f57 | 2014-06-25 18:25:11 -0700 | [diff] [blame] | 2779 | SLOGD("Encrypted %d block at %" PRId64, |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2780 | CRYPT_SECTORS_PER_BUFSIZE, |
| 2781 | i * CRYPT_SECTORS_PER_BUFSIZE); |
| 2782 | } |
| 2783 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2784 | if (!is_battery_ok_to_continue()) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2785 | SLOGE("Stopping encryption due to low battery"); |
| 2786 | *size_already_done += (i + 1) * CRYPT_SECTORS_PER_BUFSIZE - 1; |
| 2787 | rc = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2788 | goto errout; |
| 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | /* Do any remaining sectors */ |
| 2793 | for (i=0; i<remainder; i++) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2794 | if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
| 2795 | SLOGE("Error reading final sectors from real_blkdev %s for inplace encrypt", crypto_blkdev); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2796 | goto errout; |
| 2797 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2798 | if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
| 2799 | SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt", crypto_blkdev); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2800 | goto errout; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2801 | } else { |
| 2802 | SLOGI("Encrypted 1 block at next location"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2806 | *size_already_done += size; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2807 | rc = 0; |
| 2808 | |
| 2809 | errout: |
| 2810 | close(realfd); |
| 2811 | close(cryptofd); |
| 2812 | |
| 2813 | return rc; |
| 2814 | } |
| 2815 | |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2816 | /* returns on of the ENABLE_INPLACE_* return codes */ |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2817 | static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, |
| 2818 | off64_t size, off64_t *size_already_done, |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2819 | off64_t tot_size, |
| 2820 | off64_t previously_encrypted_upto) |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2821 | { |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2822 | int rc_ext4, rc_f2fs, rc_full; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2823 | if (previously_encrypted_upto) { |
Elliott Hughes | cb33f57 | 2014-06-25 18:25:11 -0700 | [diff] [blame] | 2824 | SLOGD("Continuing encryption from %" PRId64, previously_encrypted_upto); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | if (*size_already_done + size < previously_encrypted_upto) { |
| 2828 | *size_already_done += size; |
| 2829 | return 0; |
| 2830 | } |
| 2831 | |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2832 | /* TODO: identify filesystem type. |
| 2833 | * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and |
| 2834 | * then we will drop down to cryptfs_enable_inplace_f2fs. |
| 2835 | * */ |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2836 | if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev, |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2837 | size, size_already_done, |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2838 | tot_size, previously_encrypted_upto)) == 0) { |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2839 | return 0; |
| 2840 | } |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2841 | SLOGD("cryptfs_enable_inplace_ext4()=%d\n", rc_ext4); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2842 | |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2843 | if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev, |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2844 | size, size_already_done, |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2845 | tot_size, previously_encrypted_upto)) == 0) { |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2846 | return 0; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2847 | } |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2848 | SLOGD("cryptfs_enable_inplace_f2fs()=%d\n", rc_f2fs); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2849 | |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2850 | rc_full = cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev, |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2851 | size, size_already_done, tot_size, |
| 2852 | previously_encrypted_upto); |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2853 | SLOGD("cryptfs_enable_inplace_full()=%d\n", rc_full); |
| 2854 | |
| 2855 | /* Hack for b/17898962, the following is the symptom... */ |
| 2856 | if (rc_ext4 == ENABLE_INPLACE_ERR_DEV |
| 2857 | && rc_f2fs == ENABLE_INPLACE_ERR_DEV |
| 2858 | && rc_full == ENABLE_INPLACE_ERR_DEV) { |
| 2859 | return ENABLE_INPLACE_ERR_DEV; |
| 2860 | } |
| 2861 | return rc_full; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2862 | } |
| 2863 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2864 | #define CRYPTO_ENABLE_WIPE 1 |
| 2865 | #define CRYPTO_ENABLE_INPLACE 2 |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2866 | |
| 2867 | #define FRAMEWORK_BOOT_WAIT 60 |
| 2868 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2869 | static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf) |
| 2870 | { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 2871 | int fd = open(filename, O_RDONLY|O_CLOEXEC); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2872 | if (fd == -1) { |
| 2873 | SLOGE("Error opening file %s", filename); |
| 2874 | return -1; |
| 2875 | } |
| 2876 | |
| 2877 | char block[CRYPT_INPLACE_BUFSIZE]; |
| 2878 | memset(block, 0, sizeof(block)); |
| 2879 | if (unix_read(fd, block, sizeof(block)) < 0) { |
| 2880 | SLOGE("Error reading file %s", filename); |
| 2881 | close(fd); |
| 2882 | return -1; |
| 2883 | } |
| 2884 | |
| 2885 | close(fd); |
| 2886 | |
| 2887 | SHA256_CTX c; |
| 2888 | SHA256_Init(&c); |
| 2889 | SHA256_Update(&c, block, sizeof(block)); |
| 2890 | SHA256_Final(buf, &c); |
| 2891 | |
| 2892 | return 0; |
| 2893 | } |
| 2894 | |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 2895 | static int get_fs_type(struct fstab_rec *rec) |
| 2896 | { |
| 2897 | if (!strcmp(rec->fs_type, "ext4")) { |
| 2898 | return EXT4_FS; |
| 2899 | } else if (!strcmp(rec->fs_type, "f2fs")) { |
| 2900 | return F2FS_FS; |
| 2901 | } else { |
| 2902 | return -1; |
| 2903 | } |
| 2904 | } |
| 2905 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2906 | static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how, |
| 2907 | char *crypto_blkdev, char *real_blkdev, |
| 2908 | int previously_encrypted_upto) |
| 2909 | { |
| 2910 | off64_t cur_encryption_done=0, tot_encryption_size=0; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 2911 | int rc = -1; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2912 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2913 | if (!is_battery_ok_to_start()) { |
| 2914 | SLOGW("Not starting encryption due to low battery"); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2915 | return 0; |
| 2916 | } |
| 2917 | |
| 2918 | /* The size of the userdata partition, and add in the vold volumes below */ |
| 2919 | tot_encryption_size = crypt_ftr->fs_size; |
| 2920 | |
| 2921 | if (how == CRYPTO_ENABLE_WIPE) { |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 2922 | struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT); |
| 2923 | int fs_type = get_fs_type(rec); |
| 2924 | if (fs_type < 0) { |
| 2925 | SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type); |
| 2926 | return -1; |
| 2927 | } |
| 2928 | rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2929 | } else if (how == CRYPTO_ENABLE_INPLACE) { |
| 2930 | rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, |
| 2931 | crypt_ftr->fs_size, &cur_encryption_done, |
| 2932 | tot_encryption_size, |
| 2933 | previously_encrypted_upto); |
| 2934 | |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2935 | if (rc == ENABLE_INPLACE_ERR_DEV) { |
| 2936 | /* Hack for b/17898962 */ |
| 2937 | SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n"); |
| 2938 | cryptfs_reboot(reboot); |
| 2939 | } |
| 2940 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2941 | if (!rc) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2942 | crypt_ftr->encrypted_upto = cur_encryption_done; |
| 2943 | } |
| 2944 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2945 | if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2946 | /* The inplace routine never actually sets the progress to 100% due |
| 2947 | * to the round down nature of integer division, so set it here */ |
| 2948 | property_set("vold.encrypt_progress", "100"); |
| 2949 | } |
| 2950 | } else { |
| 2951 | /* Shouldn't happen */ |
| 2952 | SLOGE("cryptfs_enable: internal error, unknown option\n"); |
| 2953 | rc = -1; |
| 2954 | } |
| 2955 | |
| 2956 | return rc; |
| 2957 | } |
| 2958 | |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 2959 | int cryptfs_enable_internal(char *howarg, int crypt_type, char *passwd, |
Paul Lawrence | 569649f | 2015-09-09 12:13:00 -0700 | [diff] [blame] | 2960 | int no_ui) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2961 | { |
| 2962 | int how = 0; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2963 | char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN]; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2964 | unsigned char decrypted_master_key[KEY_LEN_BYTES]; |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 2965 | int rc=-1, i; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2966 | struct crypt_mnt_ftr crypt_ftr; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2967 | struct crypt_persist_data *pdata; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2968 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 2969 | char lockid[32] = { 0 }; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2970 | char key_loc[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2971 | int num_vols; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2972 | off64_t previously_encrypted_upto = 0; |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2973 | bool rebootEncryption = false; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2974 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2975 | if (!strcmp(howarg, "wipe")) { |
| 2976 | how = CRYPTO_ENABLE_WIPE; |
| 2977 | } else if (! strcmp(howarg, "inplace")) { |
| 2978 | how = CRYPTO_ENABLE_INPLACE; |
| 2979 | } else { |
| 2980 | /* Shouldn't happen, as CommandListener vets the args */ |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2981 | goto error_unencrypted; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2982 | } |
| 2983 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2984 | if (how == CRYPTO_ENABLE_INPLACE |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2985 | && get_crypt_ftr_and_key(&crypt_ftr) == 0) { |
| 2986 | if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) { |
| 2987 | /* An encryption was underway and was interrupted */ |
| 2988 | previously_encrypted_upto = crypt_ftr.encrypted_upto; |
| 2989 | crypt_ftr.encrypted_upto = 0; |
| 2990 | crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS; |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2991 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2992 | /* At this point, we are in an inconsistent state. Until we successfully |
| 2993 | complete encryption, a reboot will leave us broken. So mark the |
| 2994 | encryption failed in case that happens. |
| 2995 | On successfully completing encryption, remove this flag */ |
| 2996 | crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE; |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2997 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2998 | put_crypt_ftr_and_key(&crypt_ftr); |
| 2999 | } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) { |
| 3000 | if (!check_ftr_sha(&crypt_ftr)) { |
| 3001 | memset(&crypt_ftr, 0, sizeof(crypt_ftr)); |
| 3002 | put_crypt_ftr_and_key(&crypt_ftr); |
| 3003 | goto error_unencrypted; |
| 3004 | } |
| 3005 | |
| 3006 | /* Doing a reboot-encryption*/ |
| 3007 | crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION; |
| 3008 | crypt_ftr.flags |= CRYPT_FORCE_COMPLETE; |
| 3009 | rebootEncryption = true; |
| 3010 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | property_get("ro.crypto.state", encrypted_state, ""); |
| 3014 | if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) { |
| 3015 | SLOGE("Device is already running encrypted, aborting"); |
| 3016 | goto error_unencrypted; |
| 3017 | } |
| 3018 | |
| 3019 | // TODO refactor fs_mgr_get_crypt_info to get both in one call |
| 3020 | fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc)); |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 3021 | fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3022 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3023 | /* Get the size of the real block device */ |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 3024 | int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC); |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 3025 | if (fd == -1) { |
| 3026 | SLOGE("Cannot open block device %s\n", real_blkdev); |
| 3027 | goto error_unencrypted; |
| 3028 | } |
| 3029 | unsigned long nr_sec; |
| 3030 | get_blkdev_size(fd, &nr_sec); |
| 3031 | if (nr_sec == 0) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3032 | SLOGE("Cannot get size of block device %s\n", real_blkdev); |
| 3033 | goto error_unencrypted; |
| 3034 | } |
| 3035 | close(fd); |
| 3036 | |
| 3037 | /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */ |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3038 | if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3039 | unsigned int fs_size_sec, max_fs_size_sec; |
Jim Miller | a70abc6 | 2014-08-15 02:00:45 +0000 | [diff] [blame] | 3040 | fs_size_sec = get_fs_size(real_blkdev); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 3041 | if (fs_size_sec == 0) |
| 3042 | fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev); |
| 3043 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3044 | max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3045 | |
| 3046 | if (fs_size_sec > max_fs_size_sec) { |
| 3047 | SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place."); |
| 3048 | goto error_unencrypted; |
| 3049 | } |
| 3050 | } |
| 3051 | |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 3052 | /* Get a wakelock as this may take a while, and we don't want the |
| 3053 | * device to sleep on us. We'll grab a partial wakelock, and if the UI |
| 3054 | * wants to keep the screen on, it can grab a full wakelock. |
| 3055 | */ |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3056 | snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid()); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 3057 | acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid); |
| 3058 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3059 | /* The init files are setup to stop the class main and late start when |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3060 | * vold sets trigger_shutdown_framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3061 | */ |
| 3062 | property_set("vold.decrypt", "trigger_shutdown_framework"); |
| 3063 | SLOGD("Just asked init to shut down class main\n"); |
| 3064 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 3065 | /* Ask vold to unmount all devices that it manages */ |
| 3066 | if (vold_unmountAll()) { |
| 3067 | SLOGE("Failed to unmount all vold managed devices"); |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 3068 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3069 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 3070 | /* no_ui means we are being called from init, not settings. |
| 3071 | Now we always reboot from settings, so !no_ui means reboot |
| 3072 | */ |
| 3073 | bool onlyCreateHeader = false; |
| 3074 | if (!no_ui) { |
| 3075 | /* Try fallback, which is to reboot and try there */ |
| 3076 | onlyCreateHeader = true; |
| 3077 | FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we"); |
| 3078 | if (breadcrumb == 0) { |
| 3079 | SLOGE("Failed to create breadcrumb file"); |
| 3080 | goto error_shutting_down; |
| 3081 | } |
| 3082 | fclose(breadcrumb); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | /* Do extra work for a better UX when doing the long inplace encryption */ |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 3086 | if (how == CRYPTO_ENABLE_INPLACE && !onlyCreateHeader) { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3087 | /* Now that /data is unmounted, we need to mount a tmpfs |
| 3088 | * /data, set a property saying we're doing inplace encryption, |
| 3089 | * and restart the framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3090 | */ |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 3091 | if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3092 | goto error_shutting_down; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3093 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3094 | /* Tells the framework that inplace encryption is starting */ |
Ken Sumrall | 7df8412 | 2011-01-18 14:04:08 -0800 | [diff] [blame] | 3095 | property_set("vold.encrypt_progress", "0"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3096 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3097 | /* restart the framework. */ |
| 3098 | /* Create necessary paths on /data */ |
| 3099 | if (prep_data_fs()) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3100 | goto error_shutting_down; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3101 | } |
| 3102 | |
Ken Sumrall | 92736ef | 2012-10-17 20:57:14 -0700 | [diff] [blame] | 3103 | /* Ugh, shutting down the framework is not synchronous, so until it |
| 3104 | * can be fixed, this horrible hack will wait a moment for it all to |
| 3105 | * shut down before proceeding. Without it, some devices cannot |
| 3106 | * restart the graphics services. |
| 3107 | */ |
| 3108 | sleep(2); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3109 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3110 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3111 | /* Start the actual work of making an encrypted filesystem */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3112 | /* Initialize a crypt_mnt_ftr for the partition */ |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 3113 | if (previously_encrypted_upto == 0 && !rebootEncryption) { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 3114 | if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) { |
| 3115 | goto error_shutting_down; |
| 3116 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3117 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3118 | if (!strcmp(key_loc, KEY_IN_FOOTER)) { |
| 3119 | crypt_ftr.fs_size = nr_sec |
| 3120 | - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE); |
| 3121 | } else { |
| 3122 | crypt_ftr.fs_size = nr_sec; |
| 3123 | } |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3124 | /* At this point, we are in an inconsistent state. Until we successfully |
| 3125 | complete encryption, a reboot will leave us broken. So mark the |
| 3126 | encryption failed in case that happens. |
| 3127 | On successfully completing encryption, remove this flag */ |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 3128 | if (onlyCreateHeader) { |
| 3129 | crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION; |
| 3130 | } else { |
| 3131 | crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE; |
| 3132 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3133 | crypt_ftr.crypt_type = crypt_type; |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 3134 | #ifndef CONFIG_HW_DISK_ENCRYPTION |
| 3135 | strlcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", MAX_CRYPTO_TYPE_NAME_LEN); |
| 3136 | #else |
| 3137 | strlcpy((char *)crypt_ftr.crypto_type_name, "aes-xts", MAX_CRYPTO_TYPE_NAME_LEN); |
| 3138 | |
Iliyan Malchev | bb7d9af | 2014-11-20 18:42:23 -0800 | [diff] [blame] | 3139 | rc = clear_hw_device_encryption_key(); |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 3140 | if (!rc) { |
| 3141 | SLOGE("Error clearing device encryption hardware key. rc = %d", rc); |
| 3142 | } |
| 3143 | |
| 3144 | rc = set_hw_device_encryption_key(passwd, |
| 3145 | (char*) crypt_ftr.crypto_type_name); |
| 3146 | if (!rc) { |
| 3147 | SLOGE("Error initializing device encryption hardware key. rc = %d", rc); |
| 3148 | goto error_shutting_down; |
| 3149 | } |
| 3150 | #endif |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3151 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3152 | /* Make an encrypted master key */ |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 3153 | if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd, |
| 3154 | crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3155 | SLOGE("Cannot create encrypted master key\n"); |
| 3156 | goto error_shutting_down; |
| 3157 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3158 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 3159 | /* Replace scrypted intermediate key if we are preparing for a reboot */ |
| 3160 | if (onlyCreateHeader) { |
| 3161 | unsigned char fake_master_key[KEY_LEN_BYTES]; |
| 3162 | unsigned char encrypted_fake_master_key[KEY_LEN_BYTES]; |
| 3163 | memset(fake_master_key, 0, sizeof(fake_master_key)); |
| 3164 | encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key, |
| 3165 | encrypted_fake_master_key, &crypt_ftr); |
| 3166 | } |
| 3167 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3168 | /* Write the key to the end of the partition */ |
| 3169 | put_crypt_ftr_and_key(&crypt_ftr); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3170 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3171 | /* If any persistent data has been remembered, save it. |
| 3172 | * If none, create a valid empty table and save that. |
| 3173 | */ |
| 3174 | if (!persist_data) { |
| 3175 | pdata = malloc(CRYPT_PERSIST_DATA_SIZE); |
| 3176 | if (pdata) { |
| 3177 | init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE); |
| 3178 | persist_data = pdata; |
| 3179 | } |
| 3180 | } |
| 3181 | if (persist_data) { |
| 3182 | save_persistent_data(); |
| 3183 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3184 | } |
| 3185 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 3186 | if (onlyCreateHeader) { |
| 3187 | sleep(2); |
| 3188 | cryptfs_reboot(reboot); |
| 3189 | } |
| 3190 | |
| 3191 | if (how == CRYPTO_ENABLE_INPLACE && (!no_ui || rebootEncryption)) { |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 3192 | /* startup service classes main and late_start */ |
| 3193 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
| 3194 | SLOGD("Just triggered restart_min_framework\n"); |
| 3195 | |
| 3196 | /* OK, the framework is restarted and will soon be showing a |
| 3197 | * progress bar. Time to setup an encrypted mapping, and |
| 3198 | * either write a new filesystem, or encrypt in place updating |
| 3199 | * the progress bar as we work. |
| 3200 | */ |
| 3201 | } |
| 3202 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 3203 | decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3204 | create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev, |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 3205 | CRYPTO_BLOCK_DEVICE); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3206 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3207 | /* If we are continuing, check checksums match */ |
| 3208 | rc = 0; |
| 3209 | if (previously_encrypted_upto) { |
| 3210 | __le8 hash_first_block[SHA256_DIGEST_LENGTH]; |
| 3211 | rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block); |
Ken Sumrall | 128626f | 2011-06-28 18:45:14 -0700 | [diff] [blame] | 3212 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3213 | if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block, |
| 3214 | sizeof(hash_first_block)) != 0) { |
| 3215 | SLOGE("Checksums do not match - trigger wipe"); |
| 3216 | rc = -1; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3217 | } |
| 3218 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3219 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3220 | if (!rc) { |
| 3221 | rc = cryptfs_enable_all_volumes(&crypt_ftr, how, |
| 3222 | crypto_blkdev, real_blkdev, |
| 3223 | previously_encrypted_upto); |
| 3224 | } |
| 3225 | |
| 3226 | /* Calculate checksum if we are not finished */ |
Paul Lawrence | b1eb7a0 | 2014-11-25 14:57:32 -0800 | [diff] [blame] | 3227 | if (!rc && how == CRYPTO_ENABLE_INPLACE |
| 3228 | && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3229 | rc = cryptfs_SHA256_fileblock(crypto_blkdev, |
| 3230 | crypt_ftr.hash_first_block); |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 3231 | if (rc) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3232 | SLOGE("Error calculating checksum for continuing encryption"); |
| 3233 | rc = -1; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3234 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3235 | } |
| 3236 | |
| 3237 | /* Undo the dm-crypt mapping whether we succeed or not */ |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 3238 | delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3239 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3240 | if (! rc) { |
| 3241 | /* Success */ |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3242 | crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 3243 | |
Paul Lawrence | b1eb7a0 | 2014-11-25 14:57:32 -0800 | [diff] [blame] | 3244 | if (how == CRYPTO_ENABLE_INPLACE |
| 3245 | && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3246 | SLOGD("Encrypted up to sector %lld - will continue after reboot", |
| 3247 | crypt_ftr.encrypted_upto); |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3248 | crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3249 | } |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 3250 | |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3251 | put_crypt_ftr_and_key(&crypt_ftr); |
Ken Sumrall | d33d417 | 2011-02-01 00:49:13 -0800 | [diff] [blame] | 3252 | |
Paul Lawrence | b1eb7a0 | 2014-11-25 14:57:32 -0800 | [diff] [blame] | 3253 | if (how == CRYPTO_ENABLE_WIPE |
| 3254 | || crypt_ftr.encrypted_upto == crypt_ftr.fs_size) { |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 3255 | char value[PROPERTY_VALUE_MAX]; |
| 3256 | property_get("ro.crypto.state", value, ""); |
| 3257 | if (!strcmp(value, "")) { |
| 3258 | /* default encryption - continue first boot sequence */ |
| 3259 | property_set("ro.crypto.state", "encrypted"); |
| 3260 | release_wake_lock(lockid); |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 3261 | if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) { |
| 3262 | // Bring up cryptkeeper that will check the password and set it |
| 3263 | property_set("vold.decrypt", "trigger_shutdown_framework"); |
| 3264 | sleep(2); |
| 3265 | property_set("vold.encrypt_progress", ""); |
| 3266 | cryptfs_trigger_restart_min_framework(); |
| 3267 | } else { |
| 3268 | cryptfs_check_passwd(DEFAULT_PASSWORD); |
| 3269 | cryptfs_restart_internal(1); |
| 3270 | } |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 3271 | return 0; |
| 3272 | } else { |
| 3273 | sleep(2); /* Give the UI a chance to show 100% progress */ |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3274 | cryptfs_reboot(reboot); |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 3275 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3276 | } else { |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 3277 | sleep(2); /* Partially encrypted, ensure writes flushed to ssd */ |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3278 | cryptfs_reboot(shutdown); |
| 3279 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3280 | } else { |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 3281 | char value[PROPERTY_VALUE_MAX]; |
| 3282 | |
Ken Sumrall | 319369a | 2012-06-27 16:30:18 -0700 | [diff] [blame] | 3283 | property_get("ro.vold.wipe_on_crypt_fail", value, "0"); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 3284 | if (!strcmp(value, "1")) { |
| 3285 | /* wipe data if encryption failed */ |
| 3286 | SLOGE("encryption failed - rebooting into recovery to wipe data\n"); |
| 3287 | mkdir("/cache/recovery", 0700); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 3288 | int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 3289 | if (fd >= 0) { |
Jeff Sharkey | dd1a804 | 2014-09-24 11:46:51 -0700 | [diff] [blame] | 3290 | write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1); |
| 3291 | write(fd, "--reason=cryptfs_enable_internal\n", strlen("--reason=cryptfs_enable_internal\n") + 1); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 3292 | close(fd); |
| 3293 | } else { |
| 3294 | SLOGE("could not open /cache/recovery/command\n"); |
| 3295 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3296 | cryptfs_reboot(recovery); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 3297 | } else { |
| 3298 | /* set property to trigger dialog */ |
| 3299 | property_set("vold.encrypt_progress", "error_partially_encrypted"); |
| 3300 | release_wake_lock(lockid); |
| 3301 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3302 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3303 | } |
| 3304 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3305 | /* hrm, the encrypt step claims success, but the reboot failed. |
| 3306 | * This should not happen. |
| 3307 | * Set the property and return. Hope the framework can deal with it. |
| 3308 | */ |
| 3309 | property_set("vold.encrypt_progress", "error_reboot_failed"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 3310 | release_wake_lock(lockid); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3311 | return rc; |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3312 | |
| 3313 | error_unencrypted: |
| 3314 | property_set("vold.encrypt_progress", "error_not_encrypted"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 3315 | if (lockid[0]) { |
| 3316 | release_wake_lock(lockid); |
| 3317 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3318 | return -1; |
| 3319 | |
| 3320 | error_shutting_down: |
| 3321 | /* we failed, and have not encrypted anthing, so the users's data is still intact, |
| 3322 | * but the framework is stopped and not restarted to show the error, so it's up to |
| 3323 | * vold to restart the system. |
| 3324 | */ |
| 3325 | SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system"); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3326 | cryptfs_reboot(reboot); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3327 | |
| 3328 | /* shouldn't get here */ |
| 3329 | property_set("vold.encrypt_progress", "error_shutting_down"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 3330 | if (lockid[0]) { |
| 3331 | release_wake_lock(lockid); |
| 3332 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3333 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3334 | } |
| 3335 | |
Paul Lawrence | 569649f | 2015-09-09 12:13:00 -0700 | [diff] [blame] | 3336 | int cryptfs_enable(char *howarg, int type, char *passwd, int no_ui) |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 3337 | { |
Paul Lawrence | 569649f | 2015-09-09 12:13:00 -0700 | [diff] [blame] | 3338 | return cryptfs_enable_internal(howarg, type, passwd, no_ui); |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 3339 | } |
| 3340 | |
Paul Lawrence | 569649f | 2015-09-09 12:13:00 -0700 | [diff] [blame] | 3341 | int cryptfs_enable_default(char *howarg, int no_ui) |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 3342 | { |
| 3343 | return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT, |
Paul Lawrence | 569649f | 2015-09-09 12:13:00 -0700 | [diff] [blame] | 3344 | DEFAULT_PASSWORD, no_ui); |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | int cryptfs_changepw(int crypt_type, const char *newpw) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3348 | { |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 3349 | if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) { |
Paul Lawrence | f733ae6 | 2015-07-07 15:43:14 -0700 | [diff] [blame] | 3350 | return e4crypt_change_password(DATA_MNT_POINT, crypt_type, |
| 3351 | crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD |
| 3352 | : newpw); |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 3353 | } |
| 3354 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3355 | struct crypt_mnt_ftr crypt_ftr; |
JP Abgrall | 933216c | 2015-02-11 13:44:32 -0800 | [diff] [blame] | 3356 | int rc; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3357 | |
| 3358 | /* This is only allowed after we've successfully decrypted the master key */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3359 | if (!master_key_saved) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 3360 | SLOGE("Key not saved, aborting"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3361 | return -1; |
| 3362 | } |
| 3363 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3364 | if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) { |
| 3365 | SLOGE("Invalid crypt_type %d", crypt_type); |
| 3366 | return -1; |
| 3367 | } |
| 3368 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3369 | /* get key */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3370 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3371 | SLOGE("Error getting crypt footer and key"); |
| 3372 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3373 | } |
| 3374 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3375 | crypt_ftr.crypt_type = crypt_type; |
| 3376 | |
JP Abgrall | 933216c | 2015-02-11 13:44:32 -0800 | [diff] [blame] | 3377 | rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3378 | : newpw, |
| 3379 | crypt_ftr.salt, |
| 3380 | saved_master_key, |
| 3381 | crypt_ftr.master_key, |
| 3382 | &crypt_ftr); |
JP Abgrall | 933216c | 2015-02-11 13:44:32 -0800 | [diff] [blame] | 3383 | if (rc) { |
| 3384 | SLOGE("Encrypt master key failed: %d", rc); |
| 3385 | return -1; |
| 3386 | } |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 3387 | /* save the key */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3388 | put_crypt_ftr_and_key(&crypt_ftr); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3389 | |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 3390 | #ifdef CONFIG_HW_DISK_ENCRYPTION |
Iliyan Malchev | bb7d9af | 2014-11-20 18:42:23 -0800 | [diff] [blame] | 3391 | if (!strcmp((char *)crypt_ftr.crypto_type_name, "aes-xts")) { |
| 3392 | if (crypt_type == CRYPT_TYPE_DEFAULT) { |
| 3393 | int rc = update_hw_device_encryption_key(DEFAULT_PASSWORD, (char*) crypt_ftr.crypto_type_name); |
| 3394 | SLOGD("Update hardware encryption key to default for crypt_type: %d. rc = %d", crypt_type, rc); |
| 3395 | if (!rc) |
| 3396 | return -1; |
| 3397 | } else { |
| 3398 | int rc = update_hw_device_encryption_key(newpw, (char*) crypt_ftr.crypto_type_name); |
| 3399 | SLOGD("Update hardware encryption key for crypt_type: %d. rc = %d", crypt_type, rc); |
| 3400 | if (!rc) |
| 3401 | return -1; |
| 3402 | } |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 3403 | } |
| 3404 | #endif |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3405 | return 0; |
| 3406 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3407 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3408 | static unsigned int persist_get_max_entries(int encrypted) { |
| 3409 | struct crypt_mnt_ftr crypt_ftr; |
| 3410 | unsigned int dsize; |
| 3411 | unsigned int max_persistent_entries; |
| 3412 | |
| 3413 | /* If encrypted, use the values from the crypt_ftr, otherwise |
| 3414 | * use the values for the current spec. |
| 3415 | */ |
| 3416 | if (encrypted) { |
| 3417 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 3418 | return -1; |
| 3419 | } |
| 3420 | dsize = crypt_ftr.persist_data_size; |
| 3421 | } else { |
| 3422 | dsize = CRYPT_PERSIST_DATA_SIZE; |
| 3423 | } |
| 3424 | |
| 3425 | max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) / |
| 3426 | sizeof(struct crypt_persist_entry); |
| 3427 | |
| 3428 | return max_persistent_entries; |
| 3429 | } |
| 3430 | |
| 3431 | static int persist_get_key(const char *fieldname, char *value) |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3432 | { |
| 3433 | unsigned int i; |
| 3434 | |
| 3435 | if (persist_data == NULL) { |
| 3436 | return -1; |
| 3437 | } |
| 3438 | for (i = 0; i < persist_data->persist_valid_entries; i++) { |
| 3439 | if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) { |
| 3440 | /* We found it! */ |
| 3441 | strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX); |
| 3442 | return 0; |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | return -1; |
| 3447 | } |
| 3448 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3449 | static int persist_set_key(const char *fieldname, const char *value, int encrypted) |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3450 | { |
| 3451 | unsigned int i; |
| 3452 | unsigned int num; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3453 | unsigned int max_persistent_entries; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3454 | |
| 3455 | if (persist_data == NULL) { |
| 3456 | return -1; |
| 3457 | } |
| 3458 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3459 | max_persistent_entries = persist_get_max_entries(encrypted); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3460 | |
| 3461 | num = persist_data->persist_valid_entries; |
| 3462 | |
| 3463 | for (i = 0; i < num; i++) { |
| 3464 | if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) { |
| 3465 | /* We found an existing entry, update it! */ |
| 3466 | memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX); |
| 3467 | strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX); |
| 3468 | return 0; |
| 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | /* We didn't find it, add it to the end, if there is room */ |
| 3473 | if (persist_data->persist_valid_entries < max_persistent_entries) { |
| 3474 | memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry)); |
| 3475 | strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX); |
| 3476 | strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX); |
| 3477 | persist_data->persist_valid_entries++; |
| 3478 | return 0; |
| 3479 | } |
| 3480 | |
| 3481 | return -1; |
| 3482 | } |
| 3483 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3484 | /** |
| 3485 | * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the |
| 3486 | * sequence and its index is greater than or equal to index. Return 0 otherwise. |
| 3487 | */ |
| 3488 | static int match_multi_entry(const char *key, const char *field, unsigned index) { |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3489 | unsigned int field_len; |
| 3490 | unsigned int key_index; |
| 3491 | field_len = strlen(field); |
| 3492 | |
| 3493 | if (index == 0) { |
| 3494 | // The first key in a multi-entry field is just the filedname itself. |
| 3495 | if (!strcmp(key, field)) { |
| 3496 | return 1; |
| 3497 | } |
| 3498 | } |
| 3499 | // Match key against "%s_%d" % (field, index) |
| 3500 | if (strlen(key) < field_len + 1 + 1) { |
| 3501 | // Need at least a '_' and a digit. |
| 3502 | return 0; |
| 3503 | } |
| 3504 | if (strncmp(key, field, field_len)) { |
| 3505 | // If the key does not begin with field, it's not a match. |
| 3506 | return 0; |
| 3507 | } |
| 3508 | if (1 != sscanf(&key[field_len],"_%d", &key_index)) { |
| 3509 | return 0; |
| 3510 | } |
| 3511 | return key_index >= index; |
| 3512 | } |
| 3513 | |
| 3514 | /* |
| 3515 | * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all |
| 3516 | * remaining entries starting from index will be deleted. |
| 3517 | * returns PERSIST_DEL_KEY_OK if deletion succeeds, |
| 3518 | * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist, |
| 3519 | * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs. |
| 3520 | * |
| 3521 | */ |
| 3522 | static int persist_del_keys(const char *fieldname, unsigned index) |
| 3523 | { |
| 3524 | unsigned int i; |
| 3525 | unsigned int j; |
| 3526 | unsigned int num; |
| 3527 | |
| 3528 | if (persist_data == NULL) { |
| 3529 | return PERSIST_DEL_KEY_ERROR_OTHER; |
| 3530 | } |
| 3531 | |
| 3532 | num = persist_data->persist_valid_entries; |
| 3533 | |
| 3534 | j = 0; // points to the end of non-deleted entries. |
| 3535 | // Filter out to-be-deleted entries in place. |
| 3536 | for (i = 0; i < num; i++) { |
| 3537 | if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) { |
| 3538 | persist_data->persist_entry[j] = persist_data->persist_entry[i]; |
| 3539 | j++; |
| 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | if (j < num) { |
| 3544 | persist_data->persist_valid_entries = j; |
| 3545 | // Zeroise the remaining entries |
| 3546 | memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry)); |
| 3547 | return PERSIST_DEL_KEY_OK; |
| 3548 | } else { |
| 3549 | // Did not find an entry matching the given fieldname |
| 3550 | return PERSIST_DEL_KEY_ERROR_NO_FIELD; |
| 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | static int persist_count_keys(const char *fieldname) |
| 3555 | { |
| 3556 | unsigned int i; |
| 3557 | unsigned int count; |
| 3558 | |
| 3559 | if (persist_data == NULL) { |
| 3560 | return -1; |
| 3561 | } |
| 3562 | |
| 3563 | count = 0; |
| 3564 | for (i = 0; i < persist_data->persist_valid_entries; i++) { |
| 3565 | if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) { |
| 3566 | count++; |
| 3567 | } |
| 3568 | } |
| 3569 | |
| 3570 | return count; |
| 3571 | } |
| 3572 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3573 | /* Return the value of the specified field. */ |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3574 | int cryptfs_getfield(const char *fieldname, char *value, int len) |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3575 | { |
Paul Lawrence | 368d794 | 2015-04-15 14:12:00 -0700 | [diff] [blame] | 3576 | if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) { |
| 3577 | return e4crypt_get_field(DATA_MNT_POINT, fieldname, value, len); |
| 3578 | } |
| 3579 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3580 | char temp_value[PROPERTY_VALUE_MAX]; |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3581 | /* CRYPTO_GETFIELD_OK is success, |
| 3582 | * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set, |
| 3583 | * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small, |
| 3584 | * CRYPTO_GETFIELD_ERROR_OTHER is any other error |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3585 | */ |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3586 | int rc = CRYPTO_GETFIELD_ERROR_OTHER; |
| 3587 | int i; |
| 3588 | char temp_field[PROPERTY_KEY_MAX]; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3589 | |
| 3590 | if (persist_data == NULL) { |
| 3591 | load_persistent_data(); |
| 3592 | if (persist_data == NULL) { |
| 3593 | SLOGE("Getfield error, cannot load persistent data"); |
| 3594 | goto out; |
| 3595 | } |
| 3596 | } |
| 3597 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3598 | // Read value from persistent entries. If the original value is split into multiple entries, |
| 3599 | // stitch them back together. |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3600 | if (!persist_get_key(fieldname, temp_value)) { |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3601 | // We found it, copy it to the caller's buffer and keep going until all entries are read. |
| 3602 | if (strlcpy(value, temp_value, len) >= (unsigned) len) { |
| 3603 | // value too small |
| 3604 | rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL; |
| 3605 | goto out; |
| 3606 | } |
| 3607 | rc = CRYPTO_GETFIELD_OK; |
| 3608 | |
| 3609 | for (i = 1; /* break explicitly */; i++) { |
| 3610 | if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >= |
| 3611 | (int) sizeof(temp_field)) { |
| 3612 | // If the fieldname is very long, we stop as soon as it begins to overflow the |
| 3613 | // maximum field length. At this point we have in fact fully read out the original |
| 3614 | // value because cryptfs_setfield would not allow fields with longer names to be |
| 3615 | // written in the first place. |
| 3616 | break; |
| 3617 | } |
| 3618 | if (!persist_get_key(temp_field, temp_value)) { |
| 3619 | if (strlcat(value, temp_value, len) >= (unsigned)len) { |
| 3620 | // value too small. |
| 3621 | rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL; |
| 3622 | goto out; |
| 3623 | } |
| 3624 | } else { |
| 3625 | // Exhaust all entries. |
| 3626 | break; |
| 3627 | } |
| 3628 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3629 | } else { |
| 3630 | /* Sadness, it's not there. Return the error */ |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3631 | rc = CRYPTO_GETFIELD_ERROR_NO_FIELD; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | out: |
| 3635 | return rc; |
| 3636 | } |
| 3637 | |
| 3638 | /* Set the value of the specified field. */ |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3639 | int cryptfs_setfield(const char *fieldname, const char *value) |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3640 | { |
Paul Lawrence | 368d794 | 2015-04-15 14:12:00 -0700 | [diff] [blame] | 3641 | if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) { |
| 3642 | return e4crypt_set_field(DATA_MNT_POINT, fieldname, value); |
| 3643 | } |
| 3644 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3645 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3646 | /* 0 is success, negative values are error */ |
| 3647 | int rc = CRYPTO_SETFIELD_ERROR_OTHER; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3648 | int encrypted = 0; |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3649 | unsigned int field_id; |
| 3650 | char temp_field[PROPERTY_KEY_MAX]; |
| 3651 | unsigned int num_entries; |
| 3652 | unsigned int max_keylen; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3653 | |
| 3654 | if (persist_data == NULL) { |
| 3655 | load_persistent_data(); |
| 3656 | if (persist_data == NULL) { |
| 3657 | SLOGE("Setfield error, cannot load persistent data"); |
| 3658 | goto out; |
| 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | property_get("ro.crypto.state", encrypted_state, ""); |
| 3663 | if (!strcmp(encrypted_state, "encrypted") ) { |
| 3664 | encrypted = 1; |
| 3665 | } |
| 3666 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3667 | // Compute the number of entries required to store value, each entry can store up to |
| 3668 | // (PROPERTY_VALUE_MAX - 1) chars |
| 3669 | if (strlen(value) == 0) { |
| 3670 | // Empty value also needs one entry to store. |
| 3671 | num_entries = 1; |
| 3672 | } else { |
| 3673 | num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1); |
| 3674 | } |
| 3675 | |
| 3676 | max_keylen = strlen(fieldname); |
| 3677 | if (num_entries > 1) { |
| 3678 | // Need an extra "_%d" suffix. |
| 3679 | max_keylen += 1 + log10(num_entries); |
| 3680 | } |
| 3681 | if (max_keylen > PROPERTY_KEY_MAX - 1) { |
| 3682 | rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3683 | goto out; |
| 3684 | } |
| 3685 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3686 | // Make sure we have enough space to write the new value |
| 3687 | if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) > |
| 3688 | persist_get_max_entries(encrypted)) { |
| 3689 | rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG; |
| 3690 | goto out; |
| 3691 | } |
| 3692 | |
| 3693 | // Now that we know persist_data has enough space for value, let's delete the old field first |
| 3694 | // to make up space. |
| 3695 | persist_del_keys(fieldname, 0); |
| 3696 | |
| 3697 | if (persist_set_key(fieldname, value, encrypted)) { |
| 3698 | // fail to set key, should not happen as we have already checked the available space |
| 3699 | SLOGE("persist_set_key() error during setfield()"); |
| 3700 | goto out; |
| 3701 | } |
| 3702 | |
| 3703 | for (field_id = 1; field_id < num_entries; field_id++) { |
| 3704 | snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id); |
| 3705 | |
| 3706 | if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) { |
| 3707 | // fail to set key, should not happen as we have already checked the available space. |
| 3708 | SLOGE("persist_set_key() error during setfield()"); |
| 3709 | goto out; |
| 3710 | } |
| 3711 | } |
| 3712 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3713 | /* If we are running encrypted, save the persistent data now */ |
| 3714 | if (encrypted) { |
| 3715 | if (save_persistent_data()) { |
| 3716 | SLOGE("Setfield error, cannot save persistent data"); |
| 3717 | goto out; |
| 3718 | } |
| 3719 | } |
| 3720 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 3721 | rc = CRYPTO_SETFIELD_OK; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3722 | |
| 3723 | out: |
| 3724 | return rc; |
| 3725 | } |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3726 | |
| 3727 | /* Checks userdata. Attempt to mount the volume if default- |
| 3728 | * encrypted. |
| 3729 | * On success trigger next init phase and return 0. |
| 3730 | * Currently do not handle failure - see TODO below. |
| 3731 | */ |
| 3732 | int cryptfs_mount_default_encrypted(void) |
| 3733 | { |
| 3734 | char decrypt_state[PROPERTY_VALUE_MAX]; |
| 3735 | property_get("vold.decrypt", decrypt_state, "0"); |
| 3736 | if (!strcmp(decrypt_state, "0")) { |
| 3737 | SLOGE("Not encrypted - should not call here"); |
| 3738 | } else { |
| 3739 | int crypt_type = cryptfs_get_password_type(); |
| 3740 | if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) { |
| 3741 | SLOGE("Bad crypt type - error"); |
| 3742 | } else if (crypt_type != CRYPT_TYPE_DEFAULT) { |
| 3743 | SLOGD("Password is not default - " |
| 3744 | "starting min framework to prompt"); |
| 3745 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
| 3746 | return 0; |
| 3747 | } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) { |
| 3748 | SLOGD("Password is default - restarting filesystem"); |
| 3749 | cryptfs_restart_internal(0); |
| 3750 | return 0; |
| 3751 | } else { |
| 3752 | SLOGE("Encrypted, default crypt type but can't decrypt"); |
| 3753 | } |
| 3754 | } |
| 3755 | |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3756 | /** Corrupt. Allow us to boot into framework, which will detect bad |
| 3757 | crypto when it calls do_crypto_complete, then do a factory reset |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3758 | */ |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3759 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3760 | return 0; |
| 3761 | } |
| 3762 | |
| 3763 | /* Returns type of the password, default, pattern, pin or password. |
| 3764 | */ |
| 3765 | int cryptfs_get_password_type(void) |
| 3766 | { |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 3767 | if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) { |
| 3768 | return e4crypt_get_password_type(DATA_MNT_POINT); |
| 3769 | } |
| 3770 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3771 | struct crypt_mnt_ftr crypt_ftr; |
| 3772 | |
| 3773 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 3774 | SLOGE("Error getting crypt footer and key\n"); |
| 3775 | return -1; |
| 3776 | } |
| 3777 | |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3778 | if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) { |
| 3779 | return -1; |
| 3780 | } |
| 3781 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3782 | return crypt_ftr.crypt_type; |
| 3783 | } |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 3784 | |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 3785 | const char* cryptfs_get_password() |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 3786 | { |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 3787 | if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) { |
| 3788 | return e4crypt_get_password(DATA_MNT_POINT); |
| 3789 | } |
| 3790 | |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 3791 | struct timespec now; |
Paul Lawrence | ef2b5be | 2014-11-11 12:47:03 -0800 | [diff] [blame] | 3792 | clock_gettime(CLOCK_BOOTTIME, &now); |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 3793 | if (now.tv_sec < password_expiry_time) { |
| 3794 | return password; |
| 3795 | } else { |
| 3796 | cryptfs_clear_password(); |
| 3797 | return 0; |
| 3798 | } |
| 3799 | } |
| 3800 | |
| 3801 | void cryptfs_clear_password() |
| 3802 | { |
Paul Lawrence | 86c942a | 2015-05-06 13:53:43 -0700 | [diff] [blame] | 3803 | if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) { |
| 3804 | e4crypt_clear_password(DATA_MNT_POINT); |
| 3805 | } |
| 3806 | |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 3807 | if (password) { |
| 3808 | size_t len = strlen(password); |
| 3809 | memset(password, 0, len); |
| 3810 | free(password); |
| 3811 | password = 0; |
| 3812 | password_expiry_time = 0; |
| 3813 | } |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 3814 | } |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 3815 | |
| 3816 | int cryptfs_enable_file() |
| 3817 | { |
| 3818 | return e4crypt_enable(DATA_MNT_POINT); |
| 3819 | } |
| 3820 | |
Paul Lawrence | 0c24746 | 2015-10-29 10:30:57 -0700 | [diff] [blame] | 3821 | int cryptfs_isConvertibleToFBE() |
| 3822 | { |
| 3823 | struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT); |
| 3824 | return fs_mgr_is_convertible_to_fbe(rec) ? 1 : 0; |
| 3825 | } |
| 3826 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 3827 | int cryptfs_create_default_ftr(struct crypt_mnt_ftr* crypt_ftr, __attribute__((unused))int key_length) |
| 3828 | { |
| 3829 | if (cryptfs_init_crypt_mnt_ftr(crypt_ftr)) { |
| 3830 | SLOGE("Failed to initialize crypt_ftr"); |
| 3831 | return -1; |
| 3832 | } |
| 3833 | |
| 3834 | if (create_encrypted_random_key(DEFAULT_PASSWORD, crypt_ftr->master_key, |
| 3835 | crypt_ftr->salt, crypt_ftr)) { |
| 3836 | SLOGE("Cannot create encrypted master key\n"); |
| 3837 | return -1; |
| 3838 | } |
| 3839 | |
| 3840 | //crypt_ftr->keysize = key_length / 8; |
| 3841 | return 0; |
| 3842 | } |
| 3843 | |
| 3844 | int cryptfs_get_master_key(struct crypt_mnt_ftr* ftr, const char* password, |
| 3845 | unsigned char* master_key) |
| 3846 | { |
| 3847 | int rc; |
| 3848 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 3849 | unsigned char* intermediate_key = 0; |
| 3850 | size_t intermediate_key_size = 0; |
Paul Lawrence | c78c71b | 2015-04-14 15:26:29 -0700 | [diff] [blame] | 3851 | |
| 3852 | if (password == 0 || *password == 0) { |
| 3853 | password = DEFAULT_PASSWORD; |
| 3854 | } |
| 3855 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 3856 | rc = decrypt_master_key(password, master_key, ftr, &intermediate_key, |
| 3857 | &intermediate_key_size); |
| 3858 | |
Paul Lawrence | c78c71b | 2015-04-14 15:26:29 -0700 | [diff] [blame] | 3859 | int N = 1 << ftr->N_factor; |
| 3860 | int r = 1 << ftr->r_factor; |
| 3861 | int p = 1 << ftr->p_factor; |
| 3862 | |
| 3863 | unsigned char scrypted_intermediate_key[sizeof(ftr->scrypted_intermediate_key)]; |
| 3864 | |
| 3865 | rc = crypto_scrypt(intermediate_key, intermediate_key_size, |
| 3866 | ftr->salt, sizeof(ftr->salt), N, r, p, |
| 3867 | scrypted_intermediate_key, |
| 3868 | sizeof(scrypted_intermediate_key)); |
| 3869 | |
| 3870 | free(intermediate_key); |
| 3871 | |
| 3872 | if (rc) { |
| 3873 | SLOGE("Can't calculate intermediate key"); |
| 3874 | return rc; |
| 3875 | } |
| 3876 | |
| 3877 | return memcmp(scrypted_intermediate_key, ftr->scrypted_intermediate_key, |
| 3878 | intermediate_key_size); |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password, |
| 3882 | const unsigned char* master_key) |
| 3883 | { |
| 3884 | return encrypt_master_key(password, ftr->salt, master_key, ftr->master_key, |
| 3885 | ftr); |
| 3886 | } |