Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1 | /* Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | */ |
| 5 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 6 | #include <string.h> |
| 7 | |
Andrey Pronin | 04db55a | 2021-04-26 22:09:34 -0700 | [diff] [blame] | 8 | #include "pinweaver.h" |
| 9 | #include "pinweaver_eal.h" |
| 10 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 11 | /* TODO(apronin): get rid of temporary #defines */ |
| 12 | |
| 13 | #ifndef SHA256_DIGEST_SIZE |
| 14 | #define SHA256_DIGEST_SIZE (256/8) |
| 15 | #endif |
| 16 | |
| 17 | #ifndef AES256_BLOCK_CIPHER_KEY_SIZE |
| 18 | #define AES256_BLOCK_CIPHER_KEY_SIZE (256/8) |
| 19 | #endif |
| 20 | |
| 21 | #ifndef EC_SUCCESS |
| 22 | #define EC_SUCCESS 0 |
| 23 | #endif |
| 24 | |
| 25 | #ifndef MIN |
| 26 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
| 27 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 28 | |
| 29 | /* Compile time sanity checks. */ |
| 30 | /* Make sure the hash size is consistent with dcrypto. */ |
| 31 | BUILD_ASSERT(PW_HASH_SIZE >= SHA256_DIGEST_SIZE); |
| 32 | |
| 33 | /* sizeof(struct leaf_data_t) % 16 should be zero */ |
| 34 | BUILD_ASSERT(sizeof(struct leaf_sensitive_data_t) % PW_WRAP_BLOCK_SIZE == 0); |
| 35 | |
| 36 | BUILD_ASSERT(sizeof(((struct merkle_tree_t *)0)->wrap_key) == |
| 37 | AES256_BLOCK_CIPHER_KEY_SIZE); |
| 38 | |
| 39 | /* Verify that the nvmem_vars log entries have the correct sizes. */ |
| 40 | BUILD_ASSERT(sizeof(struct pw_long_term_storage_t) + |
| 41 | sizeof(struct pw_log_storage_t) <= PW_MAX_VAR_USAGE); |
| 42 | |
| 43 | /* Verify that the request structs will fit into the message. */ |
| 44 | BUILD_ASSERT(PW_MAX_MESSAGE_SIZE >= |
| 45 | sizeof(struct pw_request_header_t) + |
| 46 | sizeof(union {struct pw_request_insert_leaf_t insert_leaf; |
| 47 | struct pw_request_remove_leaf_t remove_leaf; |
| 48 | struct pw_request_try_auth_t try_auth; |
| 49 | struct pw_request_reset_auth_t reset_auth; |
| 50 | struct pw_request_get_log_t get_log; |
| 51 | struct pw_request_log_replay_t log_replay; }) + |
| 52 | sizeof(struct leaf_public_data_t) + |
| 53 | sizeof(struct leaf_sensitive_data_t) + |
| 54 | PW_MAX_PATH_SIZE); |
| 55 | |
| 56 | #define PW_MAX_RESPONSE_SIZE (sizeof(struct pw_response_header_t) + \ |
| 57 | sizeof(union {struct pw_response_insert_leaf_t insert_leaf; \ |
| 58 | struct pw_response_try_auth_t try_auth; \ |
| 59 | struct pw_response_reset_auth_t reset_auth; \ |
| 60 | struct pw_response_log_replay_t log_replay; }) + \ |
| 61 | PW_LEAF_PAYLOAD_SIZE) |
| 62 | #define PW_VALID_PCR_CRITERIA_SIZE \ |
| 63 | (sizeof(struct valid_pcr_value_t) * PW_MAX_PCR_CRITERIA_COUNT) |
| 64 | /* Verify that the request structs will fit into the message. */ |
| 65 | BUILD_ASSERT(PW_MAX_MESSAGE_SIZE >= PW_MAX_RESPONSE_SIZE); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 66 | |
| 67 | /* PW_MAX_PATH_SIZE should not change unless PW_LEAF_MAJOR_VERSION changes too. |
| 68 | * Update these statements whenever these constants are changed to remind future |
| 69 | * maintainers about this requirement. |
| 70 | * |
| 71 | * This requirement helps guarantee that forward compatibility across the same |
| 72 | * PW_LEAF_MAJOR_VERSION doesn't break because of a path length becoming too |
| 73 | * long after new fields are added to struct wrapped_leaf_data_t or its sub |
| 74 | * fields. |
| 75 | */ |
| 76 | BUILD_ASSERT(PW_LEAF_MAJOR_VERSION == 0); |
| 77 | BUILD_ASSERT(PW_MAX_PATH_SIZE == 1024); |
| 78 | |
| 79 | /* If fields are appended to struct leaf_sensitive_data_t, an encryption |
| 80 | * operation should be performed on them reusing the same IV since the prefix |
| 81 | * won't change. |
| 82 | * |
| 83 | * If any data in the original struct leaf_sensitive_data_t changes, a new IV |
| 84 | * should be generated and stored as part of the log for a replay to be |
| 85 | * possible. |
| 86 | */ |
| 87 | BUILD_ASSERT(sizeof(struct leaf_sensitive_data_t) == 3 * PW_SECRET_SIZE); |
| 88 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 89 | #define RESTART_TIMER_THRESHOLD (10 /* seconds */) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 90 | |
| 91 | /* This var caches the restart count so the nvram log structure doesn't need to |
| 92 | * be walked every time try_auth request is made. |
| 93 | */ |
| 94 | uint32_t pw_restart_count; |
| 95 | |
| 96 | /******************************************************************************/ |
| 97 | /* Struct helper functions. |
| 98 | */ |
| 99 | |
| 100 | void import_leaf(const struct unimported_leaf_data_t *unimported, |
| 101 | struct imported_leaf_data_t *imported) |
| 102 | { |
| 103 | imported->head = &unimported->head; |
| 104 | imported->hmac = unimported->hmac; |
| 105 | imported->iv = unimported->iv; |
| 106 | imported->pub = (const struct leaf_public_data_t *)unimported->payload; |
| 107 | imported->cipher_text = unimported->payload + unimported->head.pub_len; |
| 108 | imported->hashes = (const uint8_t (*)[PW_HASH_SIZE])( |
| 109 | imported->cipher_text + unimported->head.sec_len); |
| 110 | } |
| 111 | |
| 112 | /******************************************************************************/ |
| 113 | /* Basic operations required by the Merkle tree. |
| 114 | */ |
| 115 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 116 | /* Creates an empty merkle_tree with the given parameters. */ |
| 117 | static int create_merkle_tree(struct bits_per_level_t bits_per_level, |
| 118 | struct height_t height, |
| 119 | struct merkle_tree_t *merkle_tree) |
| 120 | { |
| 121 | uint16_t fan_out = 1 << bits_per_level.v; |
| 122 | uint8_t temp_hash[PW_HASH_SIZE] = {}; |
| 123 | uint8_t hx; |
| 124 | uint16_t kx; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 125 | pinweaver_eal_sha256_ctx_t ctx; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 126 | |
| 127 | merkle_tree->bits_per_level = bits_per_level; |
| 128 | merkle_tree->height = height; |
| 129 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 130 | int ret; |
| 131 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 132 | /* Initialize the root hash. */ |
| 133 | for (hx = 0; hx < height.v; ++hx) { |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 134 | ret = pinweaver_eal_sha256_init(&ctx); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | for (kx = 0; kx < fan_out; ++kx) { |
| 138 | ret = pinweaver_eal_sha256_update(&ctx, temp_hash, |
| 139 | PW_HASH_SIZE); |
| 140 | if (ret) { |
| 141 | pinweaver_eal_sha256_final(&ctx, temp_hash); |
| 142 | return ret; |
| 143 | } |
| 144 | } |
| 145 | ret = pinweaver_eal_sha256_final(&ctx, temp_hash); |
| 146 | if (ret) |
| 147 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 148 | } |
| 149 | memcpy(merkle_tree->root, temp_hash, PW_HASH_SIZE); |
| 150 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 151 | ret = pinweaver_eal_rand_bytes( |
| 152 | merkle_tree->key_derivation_nonce, |
| 153 | sizeof(merkle_tree->key_derivation_nonce)); |
| 154 | if (ret) |
| 155 | return ret; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 156 | return pinweaver_eal_derive_keys(merkle_tree); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* Computes the HMAC for an encrypted leaf using the key in the merkle_tree. */ |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 160 | static int compute_hmac(const struct merkle_tree_t *merkle_tree, |
| 161 | const struct imported_leaf_data_t *imported_leaf_data, |
| 162 | uint8_t result[PW_HASH_SIZE]) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 163 | { |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 164 | int ret; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 165 | pinweaver_eal_hmac_sha256_ctx_t hmac; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 166 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 167 | ret = pinweaver_eal_hmac_sha256_init(&hmac, merkle_tree->hmac_key, |
| 168 | sizeof(merkle_tree->hmac_key)); |
| 169 | if (ret) |
| 170 | return ret; |
| 171 | ret = pinweaver_eal_hmac_sha256_update( |
| 172 | &hmac, imported_leaf_data->head, |
| 173 | sizeof(*imported_leaf_data->head)); |
| 174 | if (ret) { |
| 175 | pinweaver_eal_hmac_sha256_final(&hmac, result); |
| 176 | return ret; |
| 177 | } |
| 178 | ret = pinweaver_eal_hmac_sha256_update(&hmac, imported_leaf_data->iv, |
| 179 | sizeof(PW_WRAP_BLOCK_SIZE)); |
| 180 | if (ret) { |
| 181 | pinweaver_eal_hmac_sha256_final(&hmac, result); |
| 182 | return ret; |
| 183 | } |
| 184 | ret = pinweaver_eal_hmac_sha256_update( |
| 185 | &hmac, imported_leaf_data->pub, |
| 186 | imported_leaf_data->head->pub_len); |
| 187 | if (ret) { |
| 188 | pinweaver_eal_hmac_sha256_final(&hmac, result); |
| 189 | return ret; |
| 190 | } |
| 191 | ret = pinweaver_eal_hmac_sha256_update( |
| 192 | &hmac, imported_leaf_data->cipher_text, |
| 193 | imported_leaf_data->head->sec_len); |
| 194 | if (ret) { |
| 195 | pinweaver_eal_hmac_sha256_final(&hmac, result); |
| 196 | return ret; |
| 197 | } |
| 198 | return pinweaver_eal_hmac_sha256_final(&hmac, result); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* Computes the root hash for the specified path and child hash. */ |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 202 | static int compute_root_hash(const struct merkle_tree_t *merkle_tree, |
| 203 | struct label_t path, |
| 204 | const uint8_t hashes[][PW_HASH_SIZE], |
| 205 | const uint8_t child_hash[PW_HASH_SIZE], |
| 206 | uint8_t new_root[PW_HASH_SIZE]) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 207 | { |
| 208 | /* This is one less than the fan out, the number of sibling hashes. */ |
| 209 | const uint16_t num_aux = (1 << merkle_tree->bits_per_level.v) - 1; |
| 210 | const uint16_t path_suffix_mask = num_aux; |
| 211 | uint8_t temp_hash[PW_HASH_SIZE]; |
| 212 | uint8_t hx = 0; |
| 213 | uint64_t index = path.v; |
| 214 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 215 | if (compute_hash(hashes, num_aux, |
| 216 | (struct index_t){ index & path_suffix_mask }, |
| 217 | child_hash, temp_hash)) { |
| 218 | return PW_ERR_CRYPTO_FAILURE; |
| 219 | } |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 220 | for (hx = 1; hx < merkle_tree->height.v; ++hx) { |
| 221 | hashes += num_aux; |
| 222 | index = index >> merkle_tree->bits_per_level.v; |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 223 | if (compute_hash(hashes, num_aux, |
| 224 | (struct index_t){ index & path_suffix_mask }, |
| 225 | temp_hash, temp_hash)) { |
| 226 | return PW_ERR_CRYPTO_FAILURE; |
| 227 | } |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 228 | } |
| 229 | memcpy(new_root, temp_hash, sizeof(temp_hash)); |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 230 | return EC_SUCCESS; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* Checks to see the specified path is valid. The length of the path should be |
| 234 | * validated prior to calling this function. |
| 235 | * |
| 236 | * Returns 0 on success or an error code otherwise. |
| 237 | */ |
| 238 | static int authenticate_path(const struct merkle_tree_t *merkle_tree, |
| 239 | struct label_t path, |
| 240 | const uint8_t hashes[][PW_HASH_SIZE], |
| 241 | const uint8_t child_hash[PW_HASH_SIZE]) |
| 242 | { |
| 243 | uint8_t parent[PW_HASH_SIZE]; |
| 244 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 245 | if (compute_root_hash(merkle_tree, path, hashes, child_hash, parent) != |
| 246 | 0) |
| 247 | return PW_ERR_CRYPTO_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 248 | if (memcmp(parent, merkle_tree->root, sizeof(parent)) != 0) |
| 249 | return PW_ERR_PATH_AUTH_FAILED; |
| 250 | return EC_SUCCESS; |
| 251 | } |
| 252 | |
| 253 | static void init_wrapped_leaf_data( |
| 254 | struct wrapped_leaf_data_t *wrapped_leaf_data) |
| 255 | { |
| 256 | wrapped_leaf_data->head.leaf_version.major = PW_LEAF_MAJOR_VERSION; |
| 257 | wrapped_leaf_data->head.leaf_version.minor = PW_LEAF_MINOR_VERSION; |
| 258 | wrapped_leaf_data->head.pub_len = sizeof(wrapped_leaf_data->pub); |
| 259 | wrapped_leaf_data->head.sec_len = |
| 260 | sizeof(wrapped_leaf_data->cipher_text); |
| 261 | } |
| 262 | |
| 263 | /* Encrypts the leaf meta data. */ |
| 264 | static int encrypt_leaf_data(const struct merkle_tree_t *merkle_tree, |
| 265 | const struct leaf_data_t *leaf_data, |
| 266 | struct wrapped_leaf_data_t *wrapped_leaf_data) |
| 267 | { |
| 268 | /* Generate a random IV. |
| 269 | * |
| 270 | * If fields are appended to struct leaf_sensitive_data_t, an encryption |
| 271 | * operation should be performed on them reusing the same IV since the |
| 272 | * prefix won't change. |
| 273 | * |
| 274 | * If any data of in the original struct leaf_sensitive_data_t changes, |
| 275 | * a new IV should be generated and stored as part of the log for a |
| 276 | * replay to be possible. |
| 277 | */ |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 278 | if (pinweaver_eal_rand_bytes(wrapped_leaf_data->iv, |
| 279 | sizeof(wrapped_leaf_data->iv))) { |
| 280 | return PW_ERR_CRYPTO_FAILURE; |
| 281 | } |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 282 | memcpy(&wrapped_leaf_data->pub, &leaf_data->pub, |
| 283 | sizeof(leaf_data->pub)); |
Andrey Pronin | 1197a0d | 2021-05-14 20:18:05 -0700 | [diff] [blame] | 284 | if (pinweaver_eal_aes256_ctr(merkle_tree->wrap_key, |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 285 | sizeof(merkle_tree->wrap_key), |
| 286 | wrapped_leaf_data->iv, |
| 287 | &leaf_data->sec, |
| 288 | sizeof(leaf_data->sec), |
| 289 | wrapped_leaf_data->cipher_text)) { |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 290 | return PW_ERR_CRYPTO_FAILURE; |
| 291 | } |
| 292 | return EC_SUCCESS; |
| 293 | } |
| 294 | |
| 295 | /* Decrypts the leaf meta data. */ |
| 296 | static int decrypt_leaf_data( |
| 297 | const struct merkle_tree_t *merkle_tree, |
| 298 | const struct imported_leaf_data_t *imported_leaf_data, |
| 299 | struct leaf_data_t *leaf_data) |
| 300 | { |
| 301 | memcpy(&leaf_data->pub, imported_leaf_data->pub, |
| 302 | MIN(imported_leaf_data->head->pub_len, |
| 303 | sizeof(struct leaf_public_data_t))); |
Andrey Pronin | 1197a0d | 2021-05-14 20:18:05 -0700 | [diff] [blame] | 304 | if (pinweaver_eal_aes256_ctr(merkle_tree->wrap_key, |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 305 | sizeof(merkle_tree->wrap_key), |
| 306 | imported_leaf_data->iv, |
| 307 | imported_leaf_data->cipher_text, |
| 308 | sizeof(leaf_data->sec), |
| 309 | &leaf_data->sec)) { |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 310 | return PW_ERR_CRYPTO_FAILURE; |
| 311 | } |
| 312 | return EC_SUCCESS; |
| 313 | } |
| 314 | |
| 315 | static int handle_leaf_update( |
| 316 | const struct merkle_tree_t *merkle_tree, |
| 317 | const struct leaf_data_t *leaf_data, |
| 318 | const uint8_t hashes[][PW_HASH_SIZE], |
| 319 | struct wrapped_leaf_data_t *wrapped_leaf_data, |
| 320 | uint8_t new_root[PW_HASH_SIZE], |
| 321 | const struct imported_leaf_data_t *optional_old_wrapped_data) |
| 322 | { |
| 323 | int ret; |
| 324 | struct imported_leaf_data_t ptrs; |
| 325 | |
| 326 | init_wrapped_leaf_data(wrapped_leaf_data); |
| 327 | if (optional_old_wrapped_data == NULL) { |
| 328 | ret = encrypt_leaf_data(merkle_tree, leaf_data, |
| 329 | wrapped_leaf_data); |
| 330 | if (ret != EC_SUCCESS) |
| 331 | return ret; |
| 332 | } else { |
| 333 | memcpy(wrapped_leaf_data->iv, optional_old_wrapped_data->iv, |
| 334 | sizeof(wrapped_leaf_data->iv)); |
| 335 | memcpy(&wrapped_leaf_data->pub, &leaf_data->pub, |
| 336 | sizeof(leaf_data->pub)); |
| 337 | memcpy(wrapped_leaf_data->cipher_text, |
| 338 | optional_old_wrapped_data->cipher_text, |
| 339 | sizeof(wrapped_leaf_data->cipher_text)); |
| 340 | } |
| 341 | |
| 342 | import_leaf((const struct unimported_leaf_data_t *)wrapped_leaf_data, |
| 343 | &ptrs); |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 344 | ret = compute_hmac(merkle_tree, &ptrs, wrapped_leaf_data->hmac); |
| 345 | if (ret) |
| 346 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 347 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 348 | ret = compute_root_hash(merkle_tree, leaf_data->pub.label, hashes, |
| 349 | wrapped_leaf_data->hmac, new_root); |
| 350 | if (ret) |
| 351 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 352 | |
| 353 | return EC_SUCCESS; |
| 354 | } |
| 355 | |
| 356 | /******************************************************************************/ |
| 357 | /* Parameter and state validation functions. |
| 358 | */ |
| 359 | |
| 360 | static int validate_tree_parameters(struct bits_per_level_t bits_per_level, |
| 361 | struct height_t height) |
| 362 | { |
| 363 | uint8_t fan_out = 1 << bits_per_level.v; |
| 364 | |
| 365 | if (bits_per_level.v < BITS_PER_LEVEL_MIN || |
| 366 | bits_per_level.v > BITS_PER_LEVEL_MAX) |
| 367 | return PW_ERR_BITS_PER_LEVEL_INVALID; |
| 368 | |
| 369 | if (height.v < HEIGHT_MIN || |
| 370 | height.v > HEIGHT_MAX(bits_per_level.v) || |
| 371 | ((fan_out - 1) * height.v) * PW_HASH_SIZE > PW_MAX_PATH_SIZE) |
| 372 | return PW_ERR_HEIGHT_INVALID; |
| 373 | |
| 374 | return EC_SUCCESS; |
| 375 | } |
| 376 | |
| 377 | /* Verifies that merkle_tree has been initialized. */ |
| 378 | static int validate_tree(const struct merkle_tree_t *merkle_tree) |
| 379 | { |
| 380 | if (validate_tree_parameters(merkle_tree->bits_per_level, |
| 381 | merkle_tree->height) != EC_SUCCESS) |
| 382 | return PW_ERR_TREE_INVALID; |
| 383 | return EC_SUCCESS; |
| 384 | } |
| 385 | |
| 386 | /* Checks the following conditions: |
| 387 | * Extra index fields should be all zero. |
| 388 | */ |
| 389 | static int validate_label(const struct merkle_tree_t *merkle_tree, |
| 390 | struct label_t path) |
| 391 | { |
| 392 | uint8_t shift_by = merkle_tree->bits_per_level.v * |
| 393 | merkle_tree->height.v; |
| 394 | |
| 395 | if ((path.v >> shift_by) == 0) |
| 396 | return EC_SUCCESS; |
| 397 | return PW_ERR_LABEL_INVALID; |
| 398 | } |
| 399 | |
| 400 | /* Checks the following conditions: |
| 401 | * Columns should be strictly increasing. |
| 402 | * Zeroes for filler at the end of the delay_schedule are permitted. |
| 403 | */ |
| 404 | static int validate_delay_schedule(const struct delay_schedule_entry_t |
| 405 | delay_schedule[PW_SCHED_COUNT]) |
| 406 | { |
| 407 | size_t x; |
| 408 | |
| 409 | /* The first entry should not be useless. */ |
| 410 | if (delay_schedule[0].time_diff.v == 0) |
| 411 | return PW_ERR_DELAY_SCHEDULE_INVALID; |
| 412 | |
| 413 | for (x = PW_SCHED_COUNT - 1; x > 0; --x) { |
| 414 | if (delay_schedule[x].attempt_count.v == 0) { |
| 415 | if (delay_schedule[x].time_diff.v != 0) |
| 416 | return PW_ERR_DELAY_SCHEDULE_INVALID; |
| 417 | } else if (delay_schedule[x].attempt_count.v <= |
| 418 | delay_schedule[x - 1].attempt_count.v || |
| 419 | delay_schedule[x].time_diff.v <= |
| 420 | delay_schedule[x - 1].time_diff.v) { |
| 421 | return PW_ERR_DELAY_SCHEDULE_INVALID; |
| 422 | } |
| 423 | } |
| 424 | return EC_SUCCESS; |
| 425 | } |
| 426 | |
| 427 | static int validate_pcr_value(const struct valid_pcr_value_t |
| 428 | valid_pcr_criteria[PW_MAX_PCR_CRITERIA_COUNT]) |
| 429 | { |
| 430 | size_t index; |
| 431 | uint8_t sha256_of_selected_pcr[SHA256_DIGEST_SIZE]; |
| 432 | |
| 433 | for (index = 0; index < PW_MAX_PCR_CRITERIA_COUNT; ++index) { |
| 434 | /* The criteria with bitmask[0] = bitmask[1] = 0 is considered |
| 435 | * the end of list criteria. If it happens that the first |
| 436 | * bitmask is zero, we consider that no criteria has to be |
| 437 | * satisfied and return success in that case. |
| 438 | */ |
| 439 | if (valid_pcr_criteria[index].bitmask[0] == 0 && |
| 440 | valid_pcr_criteria[index].bitmask[1] == 0) { |
| 441 | if (index == 0) |
| 442 | return EC_SUCCESS; |
| 443 | |
| 444 | return PW_ERR_PCR_NOT_MATCH; |
| 445 | } |
| 446 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 447 | if (pinweaver_eal_get_current_pcr_digest( |
| 448 | valid_pcr_criteria[index].bitmask, |
| 449 | sha256_of_selected_pcr)) { |
| 450 | PINWEAVER_EAL_INFO( |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 451 | "PinWeaver: Read PCR error, bitmask: %d, %d", |
| 452 | valid_pcr_criteria[index].bitmask[0], |
| 453 | valid_pcr_criteria[index].bitmask[1]); |
| 454 | return PW_ERR_PCR_NOT_MATCH; |
| 455 | } |
| 456 | |
| 457 | /* Check if the curent PCR digest is the same as expected by |
| 458 | * criteria. |
| 459 | */ |
| 460 | if (memcmp(sha256_of_selected_pcr, |
| 461 | valid_pcr_criteria[index].digest, |
| 462 | SHA256_DIGEST_SIZE) == 0) { |
| 463 | return EC_SUCCESS; |
| 464 | } |
| 465 | } |
| 466 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 467 | PINWEAVER_EAL_INFO("PinWeaver: No criteria matches PCR values"); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 468 | return PW_ERR_PCR_NOT_MATCH; |
| 469 | } |
| 470 | |
Leo Lai | 8c9892c | 2021-06-22 21:32:02 +0800 | [diff] [blame] | 471 | static uint32_t expected_payload_len(int minor_version) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 472 | { |
| 473 | switch (minor_version) { |
| 474 | case 0: |
| 475 | return PW_LEAF_PAYLOAD_SIZE - PW_VALID_PCR_CRITERIA_SIZE; |
| 476 | case PW_LEAF_MINOR_VERSION: |
| 477 | return PW_LEAF_PAYLOAD_SIZE; |
| 478 | default: |
| 479 | return 0; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static int validate_leaf_header(const struct leaf_header_t *head, |
| 484 | uint16_t payload_len, uint16_t aux_hash_len) |
| 485 | { |
| 486 | uint32_t leaf_payload_len = head->pub_len + head->sec_len; |
| 487 | |
| 488 | if (head->leaf_version.major != PW_LEAF_MAJOR_VERSION) |
| 489 | return PW_ERR_LEAF_VERSION_MISMATCH; |
| 490 | |
| 491 | if (head->leaf_version.minor <= PW_LEAF_MINOR_VERSION && |
| 492 | leaf_payload_len != |
| 493 | expected_payload_len(head->leaf_version.minor)) { |
| 494 | return PW_ERR_LENGTH_INVALID; |
| 495 | } |
| 496 | |
| 497 | if (payload_len != leaf_payload_len + aux_hash_len * PW_HASH_SIZE) |
| 498 | return PW_ERR_LENGTH_INVALID; |
| 499 | |
| 500 | return EC_SUCCESS; |
| 501 | } |
| 502 | |
| 503 | /* Common validation for requests that include a path to authenticate. */ |
| 504 | static int validate_request_with_path(const struct merkle_tree_t *merkle_tree, |
| 505 | struct label_t path, |
| 506 | const uint8_t hashes[][PW_HASH_SIZE], |
| 507 | const uint8_t hmac[PW_HASH_SIZE]) |
| 508 | { |
| 509 | int ret; |
| 510 | |
| 511 | ret = validate_tree(merkle_tree); |
| 512 | if (ret != EC_SUCCESS) |
| 513 | return ret; |
| 514 | |
| 515 | ret = validate_label(merkle_tree, path); |
| 516 | if (ret != EC_SUCCESS) |
| 517 | return ret; |
| 518 | |
| 519 | return authenticate_path(merkle_tree, path, hashes, hmac); |
| 520 | } |
| 521 | |
| 522 | /* Common validation for requests that import a leaf. */ |
| 523 | static int validate_request_with_wrapped_leaf( |
| 524 | const struct merkle_tree_t *merkle_tree, |
| 525 | uint16_t payload_len, |
| 526 | const struct unimported_leaf_data_t *unimported_leaf_data, |
| 527 | struct imported_leaf_data_t *imported_leaf_data, |
| 528 | struct leaf_data_t *leaf_data) |
| 529 | { |
| 530 | int ret; |
| 531 | uint8_t hmac[PW_HASH_SIZE]; |
| 532 | |
| 533 | ret = validate_leaf_header(&unimported_leaf_data->head, payload_len, |
| 534 | get_path_auxiliary_hash_count(merkle_tree)); |
| 535 | if (ret != EC_SUCCESS) |
| 536 | return ret; |
| 537 | |
| 538 | import_leaf(unimported_leaf_data, imported_leaf_data); |
| 539 | ret = validate_request_with_path(merkle_tree, |
| 540 | imported_leaf_data->pub->label, |
| 541 | imported_leaf_data->hashes, |
| 542 | imported_leaf_data->hmac); |
| 543 | if (ret != EC_SUCCESS) |
| 544 | return ret; |
| 545 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 546 | ret = compute_hmac(merkle_tree, imported_leaf_data, hmac); |
| 547 | if (ret != EC_SUCCESS) |
| 548 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 549 | /* Safe memcmp is used here to prevent an attacker from being able to |
| 550 | * brute force a valid HMAC for a crafted wrapped_leaf_data. |
| 551 | * memcmp provides an attacker a timing side-channel they can use to |
| 552 | * determine how much of a prefix is correct. |
| 553 | */ |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 554 | if (pinweaver_eal_safe_memcmp(hmac, unimported_leaf_data->hmac, |
| 555 | sizeof(hmac))) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 556 | return PW_ERR_HMAC_AUTH_FAILED; |
| 557 | |
| 558 | ret = decrypt_leaf_data(merkle_tree, imported_leaf_data, leaf_data); |
| 559 | if (ret != EC_SUCCESS) |
| 560 | return ret; |
| 561 | |
| 562 | /* The code below handles version upgrades. */ |
| 563 | if (unimported_leaf_data->head.leaf_version.minor == 0 && |
| 564 | unimported_leaf_data->head.leaf_version.major == 0) { |
| 565 | /* Populate the leaf_data with default pcr value */ |
| 566 | memset(&leaf_data->pub.valid_pcr_criteria, 0, |
| 567 | PW_VALID_PCR_CRITERIA_SIZE); |
| 568 | } |
| 569 | |
| 570 | return EC_SUCCESS; |
| 571 | } |
| 572 | |
| 573 | /* Sets the value of ts to the current notion of time. */ |
| 574 | static void update_timestamp(struct pw_timestamp_t *ts) |
| 575 | { |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 576 | ts->timer_value = pinweaver_eal_seconds_since_boot(); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 577 | ts->boot_count = pw_restart_count; |
| 578 | } |
| 579 | |
| 580 | /* Checks if an auth attempt can be made or not based on the delay schedule. |
| 581 | * EC_SUCCESS is returned when a new attempt can be made otherwise |
| 582 | * seconds_to_wait will be updated with the remaining wait time required. |
| 583 | */ |
| 584 | static int test_rate_limit(struct leaf_data_t *leaf_data, |
| 585 | struct time_diff_t *seconds_to_wait) |
| 586 | { |
| 587 | uint64_t ready_time; |
| 588 | uint8_t x; |
| 589 | struct pw_timestamp_t current_time; |
| 590 | struct time_diff_t delay = {0}; |
| 591 | |
| 592 | /* This loop ends when x is one greater than the index that applies. */ |
| 593 | for (x = 0; x < ARRAY_SIZE(leaf_data->pub.delay_schedule); ++x) { |
| 594 | /* Stop if a null entry is reached. The first part of the delay |
| 595 | * schedule has a list of increasing (attempt_count, time_diff) |
| 596 | * pairs with any unused entries zeroed out at the end. |
| 597 | */ |
| 598 | if (leaf_data->pub.delay_schedule[x].attempt_count.v == 0) |
| 599 | break; |
| 600 | |
| 601 | /* Stop once a delay schedule entry is reached whose |
| 602 | * threshold is greater than the current number of |
| 603 | * attempts. |
| 604 | */ |
| 605 | if (leaf_data->pub.attempt_count.v < |
| 606 | leaf_data->pub.delay_schedule[x].attempt_count.v) |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | /* If the first threshold was greater than the current number of |
| 611 | * attempts, there is no delay. Otherwise, grab the delay from the |
| 612 | * entry prior to the one that was too big. |
| 613 | */ |
| 614 | if (x > 0) |
| 615 | delay = leaf_data->pub.delay_schedule[x - 1].time_diff; |
| 616 | |
| 617 | if (delay.v == 0) |
| 618 | return EC_SUCCESS; |
| 619 | |
| 620 | if (delay.v == PW_BLOCK_ATTEMPTS) { |
| 621 | seconds_to_wait->v = PW_BLOCK_ATTEMPTS; |
| 622 | return PW_ERR_RATE_LIMIT_REACHED; |
| 623 | } |
| 624 | |
| 625 | update_timestamp(¤t_time); |
| 626 | |
| 627 | if (leaf_data->pub.timestamp.boot_count == current_time.boot_count) |
| 628 | ready_time = delay.v + leaf_data->pub.timestamp.timer_value; |
| 629 | else |
| 630 | ready_time = delay.v; |
| 631 | |
| 632 | if (current_time.timer_value >= ready_time) |
| 633 | return EC_SUCCESS; |
| 634 | |
| 635 | seconds_to_wait->v = ready_time - current_time.timer_value; |
| 636 | return PW_ERR_RATE_LIMIT_REACHED; |
| 637 | } |
| 638 | |
| 639 | /******************************************************************************/ |
| 640 | /* Logging implementation. |
| 641 | */ |
| 642 | |
| 643 | /* Once the storage version is incremented, the update code needs to be written |
| 644 | * to handle differences in the structs. |
| 645 | * |
| 646 | * See the two comments "Add storage format updates here." below. |
| 647 | */ |
| 648 | BUILD_ASSERT(PW_STORAGE_VERSION == 0); |
| 649 | |
| 650 | void force_restart_count(uint32_t mock_value) |
| 651 | { |
| 652 | pw_restart_count = mock_value; |
| 653 | } |
| 654 | |
| 655 | /* Returns EC_SUCCESS if the root hash was found. Sets *index to the first index |
| 656 | * of the log entry with a matching root hash, or the index of the last valid |
| 657 | * entry. |
| 658 | */ |
| 659 | static int find_relevant_entry(const struct pw_log_storage_t *log, |
| 660 | const uint8_t root[PW_HASH_SIZE], int *index) |
| 661 | { |
| 662 | /* Find the relevant log entry. */ |
| 663 | for (*index = 0; *index < PW_LOG_ENTRY_COUNT; ++*index) { |
| 664 | if (log->entries[*index].type.v == PW_MT_INVALID) |
| 665 | break; |
| 666 | if (memcmp(root, log->entries[*index].root, PW_HASH_SIZE) == 0) |
| 667 | return EC_SUCCESS; |
| 668 | } |
| 669 | --*index; |
| 670 | return PW_ERR_ROOT_NOT_FOUND; |
| 671 | } |
| 672 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 673 | /* TODO(apronin): get rid of temporary redirect methods */ |
| 674 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 675 | static int load_log_data(struct pw_log_storage_t *log) |
| 676 | { |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 677 | return pinweaver_eal_storage_get_log(log); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | int store_log_data(const struct pw_log_storage_t *log) |
| 681 | { |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 682 | return pinweaver_eal_storage_set_log(log); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static int load_merkle_tree(struct merkle_tree_t *merkle_tree) |
| 686 | { |
| 687 | int ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 688 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 689 | PINWEAVER_EAL_INFO("PinWeaver: Loading Tree!"); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 690 | |
| 691 | /* Handle the immutable data. */ |
| 692 | { |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 693 | struct pw_long_term_storage_t tree; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 694 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 695 | ret = pinweaver_eal_storage_get_tree_data(&tree); |
| 696 | if (ret != EC_SUCCESS) |
| 697 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 698 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 699 | merkle_tree->bits_per_level = tree.bits_per_level; |
| 700 | merkle_tree->height = tree.height; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 701 | memcpy(merkle_tree->key_derivation_nonce, |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 702 | tree.key_derivation_nonce, |
| 703 | sizeof(tree.key_derivation_nonce)); |
| 704 | ret = pinweaver_eal_derive_keys(merkle_tree); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 705 | if (ret != EC_SUCCESS) |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | /* Handle the root hash. */ |
| 710 | { |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 711 | ret = pinweaver_eal_storage_init_state(merkle_tree->root, |
| 712 | &pw_restart_count); |
| 713 | if (ret != EC_SUCCESS) |
| 714 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 717 | PINWEAVER_EAL_INFO("PinWeaver: Loaded Tree. restart_count = %d", |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 718 | pw_restart_count); |
| 719 | |
| 720 | return EC_SUCCESS; |
| 721 | } |
| 722 | |
| 723 | /* This should only be called when a new tree is created. */ |
| 724 | int store_merkle_tree(const struct merkle_tree_t *merkle_tree) |
| 725 | { |
| 726 | int ret; |
| 727 | |
| 728 | /* Handle the immutable data. */ |
| 729 | { |
| 730 | struct pw_long_term_storage_t data; |
| 731 | |
| 732 | data.storage_version = PW_STORAGE_VERSION; |
| 733 | data.bits_per_level = merkle_tree->bits_per_level; |
| 734 | data.height = merkle_tree->height; |
| 735 | memcpy(data.key_derivation_nonce, |
| 736 | merkle_tree->key_derivation_nonce, |
| 737 | sizeof(data.key_derivation_nonce)); |
| 738 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 739 | ret = pinweaver_eal_storage_set_tree_data(&data); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 740 | if (ret != EC_SUCCESS) |
| 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | /* Handle the root hash. */ |
| 745 | { |
| 746 | struct pw_log_storage_t log = {}; |
| 747 | struct pw_get_log_entry_t *entry = log.entries; |
| 748 | |
| 749 | log.storage_version = PW_STORAGE_VERSION; |
| 750 | entry->type.v = PW_RESET_TREE; |
| 751 | memcpy(entry->root, merkle_tree->root, |
| 752 | sizeof(merkle_tree->root)); |
| 753 | |
| 754 | ret = store_log_data(&log); |
| 755 | if (ret == EC_SUCCESS) |
| 756 | pw_restart_count = 0; |
| 757 | return ret; |
| 758 | } |
| 759 | |
| 760 | } |
| 761 | |
| 762 | static int log_roll_for_append(struct pw_log_storage_t *log) |
| 763 | { |
| 764 | int ret; |
| 765 | |
| 766 | ret = load_log_data(log); |
| 767 | if (ret != EC_SUCCESS) |
| 768 | return ret; |
| 769 | |
| 770 | memmove(&log->entries[1], &log->entries[0], |
| 771 | sizeof(log->entries[0]) * (PW_LOG_ENTRY_COUNT - 1)); |
| 772 | memset(&log->entries[0], 0, sizeof(log->entries[0])); |
| 773 | return EC_SUCCESS; |
| 774 | } |
| 775 | |
| 776 | int log_insert_leaf(struct label_t label, const uint8_t root[PW_HASH_SIZE], |
| 777 | const uint8_t hmac[PW_HASH_SIZE]) |
| 778 | { |
| 779 | int ret; |
| 780 | struct pw_log_storage_t log; |
| 781 | struct pw_get_log_entry_t *entry = log.entries; |
| 782 | |
| 783 | ret = log_roll_for_append(&log); |
| 784 | if (ret != EC_SUCCESS) |
| 785 | return ret; |
| 786 | |
| 787 | entry->type.v = PW_INSERT_LEAF; |
| 788 | entry->label.v = label.v; |
| 789 | memcpy(entry->root, root, sizeof(entry->root)); |
| 790 | memcpy(entry->leaf_hmac, hmac, sizeof(entry->leaf_hmac)); |
| 791 | |
| 792 | return store_log_data(&log); |
| 793 | } |
| 794 | |
| 795 | int log_remove_leaf(struct label_t label, const uint8_t root[PW_HASH_SIZE]) |
| 796 | { |
| 797 | int ret; |
| 798 | struct pw_log_storage_t log; |
| 799 | struct pw_get_log_entry_t *entry = log.entries; |
| 800 | |
| 801 | ret = log_roll_for_append(&log); |
| 802 | if (ret != EC_SUCCESS) |
| 803 | return ret; |
| 804 | |
| 805 | entry->type.v = PW_REMOVE_LEAF; |
| 806 | entry->label.v = label.v; |
| 807 | memcpy(entry->root, root, sizeof(entry->root)); |
| 808 | |
| 809 | return store_log_data(&log); |
| 810 | } |
| 811 | |
| 812 | int log_auth(struct label_t label, const uint8_t root[PW_HASH_SIZE], int code, |
| 813 | struct pw_timestamp_t timestamp) |
| 814 | { |
| 815 | int ret; |
| 816 | struct pw_log_storage_t log; |
| 817 | struct pw_get_log_entry_t *entry = log.entries; |
| 818 | |
| 819 | ret = log_roll_for_append(&log); |
| 820 | if (ret != EC_SUCCESS) |
| 821 | return ret; |
| 822 | |
| 823 | entry->type.v = PW_TRY_AUTH; |
| 824 | entry->label.v = label.v; |
| 825 | memcpy(entry->root, root, sizeof(entry->root)); |
| 826 | entry->return_code = code; |
| 827 | memcpy(&entry->timestamp, ×tamp, sizeof(entry->timestamp)); |
| 828 | |
| 829 | return store_log_data(&log); |
| 830 | } |
| 831 | |
| 832 | /******************************************************************************/ |
| 833 | /* Per-request-type handler implementations. |
| 834 | */ |
| 835 | |
| 836 | static int pw_handle_reset_tree(struct merkle_tree_t *merkle_tree, |
| 837 | const struct pw_request_reset_tree_t *request, |
| 838 | uint16_t req_size) |
| 839 | { |
| 840 | struct merkle_tree_t new_tree = {}; |
| 841 | int ret; |
| 842 | |
| 843 | if (req_size != sizeof(*request)) |
| 844 | return PW_ERR_LENGTH_INVALID; |
| 845 | |
| 846 | ret = validate_tree_parameters(request->bits_per_level, |
| 847 | request->height); |
| 848 | if (ret != EC_SUCCESS) |
| 849 | return ret; |
| 850 | |
| 851 | ret = create_merkle_tree(request->bits_per_level, request->height, |
| 852 | &new_tree); |
| 853 | if (ret != EC_SUCCESS) |
| 854 | return ret; |
| 855 | |
| 856 | ret = store_merkle_tree(&new_tree); |
| 857 | if (ret != EC_SUCCESS) |
| 858 | return ret; |
| 859 | |
| 860 | memcpy(merkle_tree, &new_tree, sizeof(new_tree)); |
| 861 | return EC_SUCCESS; |
| 862 | } |
| 863 | |
| 864 | static int pw_handle_insert_leaf(struct merkle_tree_t *merkle_tree, |
| 865 | const struct pw_request_insert_leaf_t *request, |
| 866 | uint16_t req_size, |
| 867 | struct pw_response_insert_leaf_t *response, |
| 868 | uint16_t *response_size) |
| 869 | { |
| 870 | int ret = EC_SUCCESS; |
| 871 | struct leaf_data_t leaf_data = {}; |
Leo Lai | 6574908 | 2021-08-10 16:32:18 +0800 | [diff] [blame] | 872 | struct wrapped_leaf_data_t wrapped_leaf_data = {}; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 873 | const uint8_t empty_hash[PW_HASH_SIZE] = {}; |
| 874 | uint8_t new_root[PW_HASH_SIZE]; |
| 875 | |
| 876 | if (req_size != sizeof(*request) + |
| 877 | get_path_auxiliary_hash_count(merkle_tree) * |
| 878 | PW_HASH_SIZE) |
| 879 | return PW_ERR_LENGTH_INVALID; |
| 880 | |
| 881 | ret = validate_request_with_path(merkle_tree, request->label, |
| 882 | request->path_hashes, empty_hash); |
| 883 | if (ret != EC_SUCCESS) |
| 884 | return ret; |
| 885 | |
| 886 | ret = validate_delay_schedule(request->delay_schedule); |
| 887 | if (ret != EC_SUCCESS) |
| 888 | return ret; |
| 889 | |
| 890 | memset(&leaf_data, 0, sizeof(leaf_data)); |
| 891 | leaf_data.pub.label.v = request->label.v; |
| 892 | memcpy(&leaf_data.pub.valid_pcr_criteria, request->valid_pcr_criteria, |
| 893 | sizeof(request->valid_pcr_criteria)); |
| 894 | memcpy(&leaf_data.pub.delay_schedule, &request->delay_schedule, |
| 895 | sizeof(request->delay_schedule)); |
| 896 | memcpy(&leaf_data.sec.low_entropy_secret, &request->low_entropy_secret, |
| 897 | sizeof(request->low_entropy_secret)); |
| 898 | memcpy(&leaf_data.sec.high_entropy_secret, |
| 899 | &request->high_entropy_secret, |
| 900 | sizeof(request->high_entropy_secret)); |
| 901 | memcpy(&leaf_data.sec.reset_secret, &request->reset_secret, |
| 902 | sizeof(request->reset_secret)); |
| 903 | |
| 904 | ret = handle_leaf_update(merkle_tree, &leaf_data, request->path_hashes, |
| 905 | &wrapped_leaf_data, new_root, NULL); |
| 906 | if (ret != EC_SUCCESS) |
| 907 | return ret; |
| 908 | |
| 909 | ret = log_insert_leaf(request->label, new_root, |
| 910 | wrapped_leaf_data.hmac); |
| 911 | if (ret != EC_SUCCESS) |
| 912 | return ret; |
| 913 | |
| 914 | memcpy(merkle_tree->root, new_root, sizeof(new_root)); |
| 915 | |
| 916 | memcpy(&response->unimported_leaf_data, &wrapped_leaf_data, |
| 917 | sizeof(wrapped_leaf_data)); |
| 918 | |
| 919 | *response_size = sizeof(*response) + PW_LEAF_PAYLOAD_SIZE; |
| 920 | |
| 921 | return ret; |
| 922 | } |
| 923 | |
| 924 | static int pw_handle_remove_leaf(struct merkle_tree_t *merkle_tree, |
| 925 | const struct pw_request_remove_leaf_t *request, |
| 926 | uint16_t req_size) |
| 927 | { |
| 928 | int ret = EC_SUCCESS; |
| 929 | const uint8_t empty_hash[PW_HASH_SIZE] = {}; |
| 930 | uint8_t new_root[PW_HASH_SIZE]; |
| 931 | |
| 932 | if (req_size != sizeof(*request) + |
| 933 | get_path_auxiliary_hash_count(merkle_tree) * |
| 934 | PW_HASH_SIZE) |
| 935 | return PW_ERR_LENGTH_INVALID; |
| 936 | |
| 937 | ret = validate_request_with_path(merkle_tree, request->leaf_location, |
| 938 | request->path_hashes, |
| 939 | request->leaf_hmac); |
| 940 | if (ret != EC_SUCCESS) |
| 941 | return ret; |
| 942 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 943 | ret = compute_root_hash(merkle_tree, request->leaf_location, |
| 944 | request->path_hashes, empty_hash, new_root); |
| 945 | if (ret != EC_SUCCESS) |
| 946 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 947 | |
| 948 | ret = log_remove_leaf(request->leaf_location, new_root); |
| 949 | if (ret != EC_SUCCESS) |
| 950 | return ret; |
| 951 | |
| 952 | memcpy(merkle_tree->root, new_root, sizeof(new_root)); |
| 953 | return ret; |
| 954 | } |
| 955 | |
| 956 | /* Processes a try_auth request. |
| 957 | * |
| 958 | * The valid fields in response based on return code are: |
| 959 | * EC_SUCCESS -> unimported_leaf_data and high_entropy_secret |
| 960 | * PW_ERR_RATE_LIMIT_REACHED -> seconds_to_wait |
| 961 | * PW_ERR_LOWENT_AUTH_FAILED -> unimported_leaf_data |
| 962 | */ |
| 963 | static int pw_handle_try_auth(struct merkle_tree_t *merkle_tree, |
| 964 | const struct pw_request_try_auth_t *request, |
| 965 | uint16_t req_size, |
| 966 | struct pw_response_try_auth_t *response, |
| 967 | uint16_t *data_length) |
| 968 | { |
| 969 | int ret = EC_SUCCESS; |
| 970 | struct leaf_data_t leaf_data = {}; |
| 971 | struct imported_leaf_data_t imported_leaf_data; |
| 972 | struct wrapped_leaf_data_t wrapped_leaf_data; |
| 973 | struct time_diff_t seconds_to_wait; |
| 974 | uint8_t zeros[PW_SECRET_SIZE] = {}; |
| 975 | uint8_t new_root[PW_HASH_SIZE]; |
| 976 | |
| 977 | /* These variables help eliminate the possibility of a timing side |
| 978 | * channel that would allow an attacker to prevent the log write. |
| 979 | */ |
| 980 | volatile int auth_result; |
| 981 | |
| 982 | volatile struct { |
| 983 | uint32_t attempts; |
| 984 | int ret; |
| 985 | uint8_t *secret; |
| 986 | uint8_t *reset_secret; |
| 987 | } results_table[2] = { |
| 988 | { 0, PW_ERR_LOWENT_AUTH_FAILED, zeros, zeros }, |
| 989 | { 0, EC_SUCCESS, leaf_data.sec.high_entropy_secret, |
| 990 | leaf_data.sec.reset_secret }, |
| 991 | }; |
| 992 | |
| 993 | if (req_size < sizeof(*request)) |
| 994 | return PW_ERR_LENGTH_INVALID; |
| 995 | |
| 996 | ret = validate_request_with_wrapped_leaf( |
| 997 | merkle_tree, req_size - sizeof(*request), |
| 998 | &request->unimported_leaf_data, &imported_leaf_data, |
| 999 | &leaf_data); |
| 1000 | if (ret != EC_SUCCESS) |
| 1001 | return ret; |
| 1002 | |
| 1003 | /* Check if at least one PCR criteria is satisfied if the leaf is |
| 1004 | * bound to PCR. |
| 1005 | */ |
| 1006 | ret = validate_pcr_value(leaf_data.pub.valid_pcr_criteria); |
| 1007 | if (ret != EC_SUCCESS) |
| 1008 | return ret; |
| 1009 | |
| 1010 | ret = test_rate_limit(&leaf_data, &seconds_to_wait); |
| 1011 | if (ret != EC_SUCCESS) { |
| 1012 | *data_length = sizeof(*response) + PW_LEAF_PAYLOAD_SIZE; |
| 1013 | memset(response, 0, *data_length); |
| 1014 | memcpy(&response->seconds_to_wait, &seconds_to_wait, |
| 1015 | sizeof(seconds_to_wait)); |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
| 1019 | update_timestamp(&leaf_data.pub.timestamp); |
| 1020 | |
| 1021 | /* Precompute the failed attempts. */ |
| 1022 | results_table[0].attempts = leaf_data.pub.attempt_count.v; |
| 1023 | if (results_table[0].attempts != UINT32_MAX) |
| 1024 | ++results_table[0].attempts; |
| 1025 | |
| 1026 | /**********************************************************************/ |
| 1027 | /* After this: |
| 1028 | * 1) results_table should not be changed; |
| 1029 | * 2) the runtime of the code paths for failed and successful |
| 1030 | * authentication attempts should not diverge. |
| 1031 | */ |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1032 | auth_result = pinweaver_eal_safe_memcmp( |
| 1033 | request->low_entropy_secret, |
| 1034 | leaf_data.sec.low_entropy_secret, |
| 1035 | sizeof(request->low_entropy_secret)) == 0; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1036 | leaf_data.pub.attempt_count.v = results_table[auth_result].attempts; |
| 1037 | |
| 1038 | /* This has a non-constant time path, but it doesn't convey information |
| 1039 | * about whether a PW_ERR_LOWENT_AUTH_FAILED happened or not. |
| 1040 | */ |
| 1041 | ret = handle_leaf_update(merkle_tree, &leaf_data, |
| 1042 | imported_leaf_data.hashes, &wrapped_leaf_data, |
| 1043 | new_root, &imported_leaf_data); |
| 1044 | if (ret != EC_SUCCESS) |
| 1045 | return ret; |
| 1046 | |
| 1047 | ret = log_auth(wrapped_leaf_data.pub.label, new_root, |
| 1048 | results_table[auth_result].ret, leaf_data.pub.timestamp); |
| 1049 | if (ret != EC_SUCCESS) { |
| 1050 | memcpy(new_root, merkle_tree->root, sizeof(merkle_tree->root)); |
| 1051 | return ret; |
| 1052 | } |
| 1053 | /**********************************************************************/ |
| 1054 | /* At this point the log should be written so it should be safe for the |
| 1055 | * runtime of the code paths to diverge. |
| 1056 | */ |
| 1057 | |
| 1058 | memcpy(merkle_tree->root, new_root, sizeof(new_root)); |
| 1059 | |
| 1060 | *data_length = sizeof(*response) + PW_LEAF_PAYLOAD_SIZE; |
| 1061 | memset(response, 0, *data_length); |
| 1062 | |
| 1063 | memcpy(&response->unimported_leaf_data, &wrapped_leaf_data, |
| 1064 | sizeof(wrapped_leaf_data)); |
| 1065 | |
| 1066 | memcpy(&response->high_entropy_secret, |
| 1067 | results_table[auth_result].secret, |
| 1068 | sizeof(response->high_entropy_secret)); |
| 1069 | |
| 1070 | memcpy(&response->reset_secret, |
| 1071 | results_table[auth_result].reset_secret, |
| 1072 | sizeof(response->reset_secret)); |
| 1073 | |
| 1074 | return results_table[auth_result].ret; |
| 1075 | } |
| 1076 | |
| 1077 | static int pw_handle_reset_auth(struct merkle_tree_t *merkle_tree, |
| 1078 | const struct pw_request_reset_auth_t *request, |
| 1079 | uint16_t req_size, |
| 1080 | struct pw_response_reset_auth_t *response, |
| 1081 | uint16_t *response_size) |
| 1082 | { |
| 1083 | int ret = EC_SUCCESS; |
| 1084 | struct leaf_data_t leaf_data = {}; |
| 1085 | struct imported_leaf_data_t imported_leaf_data; |
| 1086 | struct wrapped_leaf_data_t wrapped_leaf_data; |
| 1087 | uint8_t new_root[PW_HASH_SIZE]; |
| 1088 | |
| 1089 | if (req_size < sizeof(*request)) |
| 1090 | return PW_ERR_LENGTH_INVALID; |
| 1091 | |
| 1092 | ret = validate_request_with_wrapped_leaf( |
| 1093 | merkle_tree, req_size - sizeof(*request), |
| 1094 | &request->unimported_leaf_data, &imported_leaf_data, |
| 1095 | &leaf_data); |
| 1096 | if (ret != EC_SUCCESS) |
| 1097 | return ret; |
| 1098 | |
| 1099 | /* Safe memcmp is used here to prevent an attacker from being able to |
| 1100 | * brute force the reset secret and use it to unlock the leaf. |
| 1101 | * memcmp provides an attacker a timing side-channel they can use to |
| 1102 | * determine how much of a prefix is correct. |
| 1103 | */ |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1104 | if (pinweaver_eal_safe_memcmp(request->reset_secret, |
| 1105 | leaf_data.sec.reset_secret, |
| 1106 | sizeof(request->reset_secret)) != 0) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1107 | return PW_ERR_RESET_AUTH_FAILED; |
| 1108 | |
| 1109 | leaf_data.pub.attempt_count.v = 0; |
| 1110 | |
| 1111 | ret = handle_leaf_update(merkle_tree, &leaf_data, |
| 1112 | imported_leaf_data.hashes, &wrapped_leaf_data, |
| 1113 | new_root, &imported_leaf_data); |
| 1114 | if (ret != EC_SUCCESS) |
| 1115 | return ret; |
| 1116 | |
| 1117 | ret = log_auth(leaf_data.pub.label, new_root, ret, |
| 1118 | leaf_data.pub.timestamp); |
| 1119 | if (ret != EC_SUCCESS) |
| 1120 | return ret; |
| 1121 | |
| 1122 | memcpy(merkle_tree->root, new_root, sizeof(new_root)); |
| 1123 | |
| 1124 | memcpy(&response->unimported_leaf_data, &wrapped_leaf_data, |
| 1125 | sizeof(wrapped_leaf_data)); |
| 1126 | |
| 1127 | memcpy(response->high_entropy_secret, |
| 1128 | leaf_data.sec.high_entropy_secret, |
| 1129 | sizeof(response->high_entropy_secret)); |
| 1130 | |
| 1131 | *response_size = sizeof(*response) + PW_LEAF_PAYLOAD_SIZE; |
| 1132 | |
| 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | static int pw_handle_get_log(const struct merkle_tree_t *merkle_tree, |
| 1137 | const struct pw_request_get_log_t *request, |
| 1138 | uint16_t req_size, |
| 1139 | struct pw_get_log_entry_t response[], |
| 1140 | uint16_t *response_size) |
| 1141 | { |
| 1142 | int ret; |
| 1143 | int x; |
| 1144 | struct pw_log_storage_t log; |
| 1145 | |
| 1146 | if (req_size != sizeof(*request)) |
| 1147 | return PW_ERR_LENGTH_INVALID; |
| 1148 | |
| 1149 | ret = validate_tree(merkle_tree); |
| 1150 | if (ret != EC_SUCCESS) |
| 1151 | return ret; |
| 1152 | |
| 1153 | ret = load_log_data(&log); |
| 1154 | if (ret != EC_SUCCESS) |
| 1155 | return ret; |
| 1156 | |
| 1157 | /* Find the relevant log entry. The return value isn't used because if |
| 1158 | * the entry isn't found the entire log is returned. This makes it |
| 1159 | * easier to recover when the log is too short. |
| 1160 | * |
| 1161 | * Here is an example: |
| 1162 | * 50 attempts have been made against a leaf that becomes out of sync |
| 1163 | * because of a disk flush failing. The copy of the leaf on disk is |
| 1164 | * behind by 50 and the log contains less than 50 entries. The CrOS |
| 1165 | * implementation can check the public parameters of the local copy with |
| 1166 | * the log entry to determine that leaf is out of sync. It can then send |
| 1167 | * any valid copy of that leaf with a log replay request that will only |
| 1168 | * succeed if the HMAC of the resulting leaf matches the log entry. |
| 1169 | */ |
| 1170 | find_relevant_entry(&log, request->root, &x); |
| 1171 | /* If there are no valid entries, return. */ |
| 1172 | if (x < 0) |
| 1173 | return EC_SUCCESS; |
| 1174 | |
| 1175 | /* Copy the entries in reverse order. */ |
| 1176 | while (1) { |
| 1177 | memcpy(&response[x], &log.entries[x], sizeof(log.entries[x])); |
| 1178 | *response_size += sizeof(log.entries[x]); |
| 1179 | if (x == 0) |
| 1180 | break; |
| 1181 | --x; |
| 1182 | } |
| 1183 | |
| 1184 | return EC_SUCCESS; |
| 1185 | } |
| 1186 | |
| 1187 | static int pw_handle_log_replay(const struct merkle_tree_t *merkle_tree, |
| 1188 | const struct pw_request_log_replay_t *request, |
| 1189 | uint16_t req_size, |
| 1190 | struct pw_response_log_replay_t *response, |
| 1191 | uint16_t *response_size) |
| 1192 | { |
| 1193 | int ret; |
| 1194 | int x; |
| 1195 | struct pw_log_storage_t log; |
| 1196 | struct leaf_data_t leaf_data = {}; |
| 1197 | struct imported_leaf_data_t imported_leaf_data; |
| 1198 | struct wrapped_leaf_data_t wrapped_leaf_data; |
| 1199 | uint8_t hmac[PW_HASH_SIZE]; |
| 1200 | uint8_t root[PW_HASH_SIZE]; |
| 1201 | |
| 1202 | if (req_size < sizeof(*request)) |
| 1203 | return PW_ERR_LENGTH_INVALID; |
| 1204 | |
| 1205 | ret = validate_tree(merkle_tree); |
| 1206 | if (ret != EC_SUCCESS) |
| 1207 | return ret; |
| 1208 | |
| 1209 | /* validate_request_with_wrapped_leaf() isn't used here because the |
| 1210 | * path validation is delayed to allow any valid copy of the same leaf |
| 1211 | * to be used in the replay operation as long as the result passes path |
| 1212 | * validation. |
| 1213 | */ |
| 1214 | ret = validate_leaf_header(&request->unimported_leaf_data.head, |
| 1215 | req_size - sizeof(*request), |
| 1216 | get_path_auxiliary_hash_count(merkle_tree)); |
| 1217 | if (ret != EC_SUCCESS) |
| 1218 | return ret; |
| 1219 | |
| 1220 | import_leaf(&request->unimported_leaf_data, &imported_leaf_data); |
| 1221 | |
| 1222 | ret = load_log_data(&log); |
| 1223 | if (ret != EC_SUCCESS) |
| 1224 | return ret; |
| 1225 | |
| 1226 | /* Find the relevant log entry. */ |
| 1227 | ret = find_relevant_entry(&log, request->log_root, &x); |
| 1228 | if (ret != EC_SUCCESS) |
| 1229 | return ret; |
| 1230 | |
| 1231 | /* The other message types don't need to be handled by Cr50. */ |
| 1232 | if (log.entries[x].type.v != PW_TRY_AUTH) |
| 1233 | return PW_ERR_TYPE_INVALID; |
| 1234 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1235 | ret = compute_hmac(merkle_tree, &imported_leaf_data, hmac); |
| 1236 | if (ret != EC_SUCCESS) |
| 1237 | return ret; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1238 | if (pinweaver_eal_safe_memcmp(hmac, |
| 1239 | request->unimported_leaf_data.hmac, |
| 1240 | sizeof(hmac))) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1241 | return PW_ERR_HMAC_AUTH_FAILED; |
| 1242 | |
| 1243 | ret = decrypt_leaf_data(merkle_tree, &imported_leaf_data, &leaf_data); |
| 1244 | if (ret != EC_SUCCESS) |
| 1245 | return ret; |
| 1246 | |
| 1247 | if (leaf_data.pub.label.v != log.entries[x].label.v) |
| 1248 | return PW_ERR_LABEL_INVALID; |
| 1249 | |
| 1250 | /* Update the metadata to match the log. */ |
| 1251 | if (log.entries[x].return_code == EC_SUCCESS) |
| 1252 | leaf_data.pub.attempt_count.v = 0; |
| 1253 | else |
| 1254 | ++leaf_data.pub.attempt_count.v; |
| 1255 | memcpy(&leaf_data.pub.timestamp, &log.entries[x].timestamp, |
| 1256 | sizeof(leaf_data.pub.timestamp)); |
| 1257 | |
| 1258 | ret = handle_leaf_update(merkle_tree, &leaf_data, |
| 1259 | imported_leaf_data.hashes, &wrapped_leaf_data, |
| 1260 | root, &imported_leaf_data); |
| 1261 | if (ret != EC_SUCCESS) |
| 1262 | return ret; |
| 1263 | |
| 1264 | if (memcmp(root, log.entries[x].root, PW_HASH_SIZE)) |
| 1265 | return PW_ERR_PATH_AUTH_FAILED; |
| 1266 | |
| 1267 | memcpy(&response->unimported_leaf_data, &wrapped_leaf_data, |
| 1268 | sizeof(wrapped_leaf_data)); |
| 1269 | |
| 1270 | *response_size = sizeof(*response) + PW_LEAF_PAYLOAD_SIZE; |
| 1271 | |
| 1272 | return EC_SUCCESS; |
| 1273 | } |
| 1274 | |
| 1275 | struct merkle_tree_t pw_merkle_tree; |
| 1276 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1277 | /******************************************************************************/ |
| 1278 | /* Non-static functions. |
| 1279 | */ |
| 1280 | |
| 1281 | void pinweaver_init(void) |
| 1282 | { |
| 1283 | load_merkle_tree(&pw_merkle_tree); |
| 1284 | } |
| 1285 | |
| 1286 | int get_path_auxiliary_hash_count(const struct merkle_tree_t *merkle_tree) |
| 1287 | { |
| 1288 | return ((1 << merkle_tree->bits_per_level.v) - 1) * |
| 1289 | merkle_tree->height.v; |
| 1290 | } |
| 1291 | |
| 1292 | /* Computes the SHA256 parent hash of a set of child hashes given num_hashes |
| 1293 | * sibling hashes in hashes[] and the index of child_hash. |
| 1294 | * |
| 1295 | * Assumptions: |
| 1296 | * num_hashes == fan_out - 1 |
| 1297 | * ARRAY_SIZE(hashes) == num_hashes |
| 1298 | * 0 <= location <= num_hashes |
| 1299 | */ |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1300 | int compute_hash(const uint8_t hashes[][PW_HASH_SIZE], uint16_t num_hashes, |
| 1301 | struct index_t location, |
| 1302 | const uint8_t child_hash[PW_HASH_SIZE], |
| 1303 | uint8_t result[PW_HASH_SIZE]) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1304 | { |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1305 | int ret; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1306 | pinweaver_eal_sha256_ctx_t ctx; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1307 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1308 | ret = pinweaver_eal_sha256_init(&ctx); |
| 1309 | if (ret) |
| 1310 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1311 | if (location.v > 0) |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1312 | pinweaver_eal_sha256_update(&ctx, hashes[0], |
| 1313 | PW_HASH_SIZE * location.v); |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1314 | if (ret) { |
| 1315 | pinweaver_eal_sha256_final(&ctx, result); |
| 1316 | return ret; |
| 1317 | } |
| 1318 | ret = pinweaver_eal_sha256_update(&ctx, child_hash, PW_HASH_SIZE); |
| 1319 | if (ret) { |
| 1320 | pinweaver_eal_sha256_final(&ctx, result); |
| 1321 | return ret; |
| 1322 | } |
| 1323 | if (location.v < num_hashes) { |
| 1324 | ret = pinweaver_eal_sha256_update( |
| 1325 | &ctx, hashes[location.v], |
| 1326 | PW_HASH_SIZE * (num_hashes - location.v)); |
| 1327 | if (ret) { |
| 1328 | pinweaver_eal_sha256_final(&ctx, result); |
| 1329 | return ret; |
| 1330 | } |
| 1331 | } |
| 1332 | return pinweaver_eal_sha256_final(&ctx, result); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /* If a request from older protocol comes, this method should make it |
| 1336 | * compatible with the current request structure. |
| 1337 | */ |
| 1338 | int make_compatible_request(struct merkle_tree_t *merkle_tree, |
| 1339 | struct pw_request_t *request) |
| 1340 | { |
| 1341 | switch (request->header.version) { |
| 1342 | case 0: |
| 1343 | /* The switch from protocol version 0 to 1 means all the |
| 1344 | * requests have the same format, except insert_leaf. |
| 1345 | * Update the request in that case. |
| 1346 | */ |
| 1347 | if (request->header.type.v == PW_INSERT_LEAF) { |
| 1348 | unsigned char *src = (unsigned char *) |
| 1349 | (&request->data.insert_leaf00.path_hashes); |
| 1350 | unsigned char *dest = (unsigned char *) |
| 1351 | (&request->data.insert_leaf.path_hashes); |
| 1352 | const int hash_count = |
| 1353 | get_path_auxiliary_hash_count(merkle_tree); |
| 1354 | const uint16_t hashes_size = hash_count * PW_HASH_SIZE; |
| 1355 | |
| 1356 | memmove(dest, src, hashes_size); |
| 1357 | memset(&request->data.insert_leaf.valid_pcr_criteria, |
| 1358 | 0, PW_VALID_PCR_CRITERIA_SIZE); |
| 1359 | request->header.data_length += |
| 1360 | PW_VALID_PCR_CRITERIA_SIZE; |
| 1361 | } |
| 1362 | /* Fallthrough to make compatible from next version */ |
Leo Lai | 8c9892c | 2021-06-22 21:32:02 +0800 | [diff] [blame] | 1363 | __attribute__((fallthrough)); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1364 | case PW_PROTOCOL_VERSION: |
| 1365 | return 1; |
| 1366 | } |
| 1367 | /* Unsupported version. */ |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | /* Converts the response to be understandable by an older protocol. |
| 1372 | */ |
| 1373 | void make_compatible_response(int version, int req_type, |
| 1374 | struct pw_response_t *response) |
| 1375 | { |
| 1376 | if (version >= PW_PROTOCOL_VERSION) |
| 1377 | return; |
| 1378 | |
| 1379 | response->header.version = version; |
| 1380 | if (version == 0) { |
| 1381 | if (req_type == PW_TRY_AUTH) { |
| 1382 | unsigned char *src = (unsigned char *) |
| 1383 | (&response->data.try_auth.unimported_leaf_data); |
| 1384 | unsigned char *dest = (unsigned char *) |
| 1385 | (&response->data.try_auth00.unimported_leaf_data); |
| 1386 | memmove(dest, src, |
| 1387 | PW_LEAF_PAYLOAD_SIZE + |
| 1388 | sizeof(struct unimported_leaf_data_t)); |
| 1389 | response->header.data_length -= PW_SECRET_SIZE; |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1394 | enum pinweaver_command_res_t pinweaver_command(void *request_buf, |
| 1395 | size_t request_size, |
| 1396 | void *response_buf, |
| 1397 | size_t *response_size) { |
| 1398 | struct pw_request_t *request = request_buf; |
| 1399 | struct pw_response_t *response = response_buf; |
| 1400 | |
| 1401 | if (request_size < sizeof(request->header)) { |
| 1402 | PINWEAVER_EAL_INFO( |
| 1403 | "PinWeaver: message smaller than a header (%zd).\n", |
| 1404 | request_size); |
| 1405 | return PW_CMD_RES_TOO_SMALL; |
| 1406 | } |
| 1407 | |
| 1408 | if (request_size != request->header.data_length + |
| 1409 | sizeof(request->header)) { |
| 1410 | PINWEAVER_EAL_INFO( |
| 1411 | "PinWeaver: header size mismatch %zd != %zd.\n", |
| 1412 | request_size, |
| 1413 | request->header.data_length + sizeof(request->header)); |
| 1414 | return PW_CMD_RES_SIZE; |
| 1415 | } |
| 1416 | |
| 1417 | /* The response_size is validated by compile time checks. */ |
| 1418 | |
| 1419 | /* The return value of this function call is intentionally unused. */ |
| 1420 | pw_handle_request(&pw_merkle_tree, request, response); |
| 1421 | |
| 1422 | *response_size = response->header.data_length + |
| 1423 | sizeof(response->header); |
| 1424 | |
| 1425 | /* The response is only sent for EC_SUCCESS so it is used even for |
| 1426 | * errors which are reported through header.return_code. |
| 1427 | */ |
| 1428 | return PW_CMD_RES_SUCCESS; |
| 1429 | } |
| 1430 | |
| 1431 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1432 | /* Handles the message in request using the context in merkle_tree and writes |
| 1433 | * the results to response. The return value captures any error conditions that |
| 1434 | * occurred or EC_SUCCESS if there were no errors. |
| 1435 | * |
| 1436 | * This implementation is written to handle the case where request and response |
| 1437 | * exist at the same memory location---are backed by the same buffer. This means |
| 1438 | * the implementation requires that no reads are made to request after response |
| 1439 | * has been written to. |
| 1440 | */ |
| 1441 | int pw_handle_request(struct merkle_tree_t *merkle_tree, |
| 1442 | struct pw_request_t *request, |
| 1443 | struct pw_response_t *response) |
| 1444 | { |
| 1445 | int32_t ret; |
| 1446 | uint16_t resp_length; |
| 1447 | /* Store the message type of the request since it may be overwritten |
| 1448 | * inside the switch whenever response and request overlap in memory. |
| 1449 | */ |
| 1450 | struct pw_message_type_t type = request->header.type; |
| 1451 | int version = request->header.version; |
| 1452 | |
| 1453 | resp_length = 0; |
| 1454 | |
| 1455 | if (!make_compatible_request(merkle_tree, request)) { |
| 1456 | ret = PW_ERR_VERSION_MISMATCH; |
| 1457 | goto cleanup; |
| 1458 | } |
| 1459 | switch (type.v) { |
| 1460 | case PW_RESET_TREE: |
| 1461 | ret = pw_handle_reset_tree(merkle_tree, |
| 1462 | &request->data.reset_tree, |
| 1463 | request->header.data_length); |
| 1464 | break; |
| 1465 | case PW_INSERT_LEAF: |
| 1466 | ret = pw_handle_insert_leaf(merkle_tree, |
| 1467 | &request->data.insert_leaf, |
| 1468 | request->header.data_length, |
| 1469 | &response->data.insert_leaf, |
| 1470 | &resp_length); |
| 1471 | break; |
| 1472 | case PW_REMOVE_LEAF: |
| 1473 | ret = pw_handle_remove_leaf(merkle_tree, |
| 1474 | &request->data.remove_leaf, |
| 1475 | request->header.data_length); |
| 1476 | break; |
| 1477 | case PW_TRY_AUTH: |
| 1478 | ret = pw_handle_try_auth(merkle_tree, &request->data.try_auth, |
| 1479 | request->header.data_length, |
| 1480 | &response->data.try_auth, |
| 1481 | &resp_length); |
| 1482 | break; |
| 1483 | case PW_RESET_AUTH: |
| 1484 | ret = pw_handle_reset_auth(merkle_tree, |
| 1485 | &request->data.reset_auth, |
| 1486 | request->header.data_length, |
| 1487 | &response->data.reset_auth, |
| 1488 | &resp_length); |
| 1489 | break; |
| 1490 | case PW_GET_LOG: |
| 1491 | ret = pw_handle_get_log(merkle_tree, &request->data.get_log, |
| 1492 | request->header.data_length, |
| 1493 | (void *)&response->data, &resp_length); |
| 1494 | break; |
| 1495 | case PW_LOG_REPLAY: |
| 1496 | ret = pw_handle_log_replay(merkle_tree, |
| 1497 | &request->data.log_replay, |
| 1498 | request->header.data_length, |
| 1499 | &response->data.log_replay, |
| 1500 | &resp_length); |
| 1501 | break; |
| 1502 | default: |
| 1503 | ret = PW_ERR_TYPE_INVALID; |
| 1504 | break; |
| 1505 | } |
| 1506 | cleanup: |
| 1507 | response->header.version = PW_PROTOCOL_VERSION; |
| 1508 | response->header.data_length = resp_length; |
| 1509 | response->header.result_code = ret; |
| 1510 | memcpy(&response->header.root, merkle_tree->root, |
| 1511 | sizeof(merkle_tree->root)); |
| 1512 | |
| 1513 | make_compatible_response(version, type.v, response); |
| 1514 | |
| 1515 | return ret; |
| 1516 | }; |