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) + |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 46 | sizeof(union {pw_request_insert_leaf_t insert_leaf; |
| 47 | pw_request_remove_leaf_t remove_leaf; |
| 48 | pw_request_try_auth_t try_auth; |
| 49 | pw_request_reset_auth_t reset_auth; |
| 50 | pw_request_get_log_t get_log; |
| 51 | pw_request_log_replay_t log_replay; }) + |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 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) + \ |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 57 | sizeof(union {pw_response_insert_leaf_t insert_leaf; \ |
| 58 | pw_response_try_auth_t try_auth; \ |
| 59 | pw_response_reset_auth_t reset_auth; \ |
| 60 | pw_response_log_replay_t log_replay; }) + \ |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 61 | PW_LEAF_PAYLOAD_SIZE) |
| 62 | #define PW_VALID_PCR_CRITERIA_SIZE \ |
| 63 | (sizeof(struct valid_pcr_value_t) * PW_MAX_PCR_CRITERIA_COUNT) |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 64 | #define PW_EXPIRATION_DATA_SIZE (sizeof(struct pw_timestamp_t) + 4) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 65 | /* Verify that the request structs will fit into the message. */ |
| 66 | BUILD_ASSERT(PW_MAX_MESSAGE_SIZE >= PW_MAX_RESPONSE_SIZE); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 67 | |
| 68 | /* PW_MAX_PATH_SIZE should not change unless PW_LEAF_MAJOR_VERSION changes too. |
| 69 | * Update these statements whenever these constants are changed to remind future |
| 70 | * maintainers about this requirement. |
| 71 | * |
| 72 | * This requirement helps guarantee that forward compatibility across the same |
| 73 | * PW_LEAF_MAJOR_VERSION doesn't break because of a path length becoming too |
| 74 | * long after new fields are added to struct wrapped_leaf_data_t or its sub |
| 75 | * fields. |
| 76 | */ |
| 77 | BUILD_ASSERT(PW_LEAF_MAJOR_VERSION == 0); |
| 78 | BUILD_ASSERT(PW_MAX_PATH_SIZE == 1024); |
| 79 | |
| 80 | /* If fields are appended to struct leaf_sensitive_data_t, an encryption |
| 81 | * operation should be performed on them reusing the same IV since the prefix |
| 82 | * won't change. |
| 83 | * |
| 84 | * If any data in the original struct leaf_sensitive_data_t changes, a new IV |
| 85 | * should be generated and stored as part of the log for a replay to be |
| 86 | * possible. |
| 87 | */ |
| 88 | BUILD_ASSERT(sizeof(struct leaf_sensitive_data_t) == 3 * PW_SECRET_SIZE); |
| 89 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 90 | #define RESTART_TIMER_THRESHOLD (10 /* seconds */) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 91 | |
| 92 | /* This var caches the restart count so the nvram log structure doesn't need to |
| 93 | * be walked every time try_auth request is made. |
| 94 | */ |
| 95 | uint32_t pw_restart_count; |
| 96 | |
| 97 | /******************************************************************************/ |
| 98 | /* Struct helper functions. |
| 99 | */ |
| 100 | |
| 101 | void import_leaf(const struct unimported_leaf_data_t *unimported, |
| 102 | struct imported_leaf_data_t *imported) |
| 103 | { |
| 104 | imported->head = &unimported->head; |
| 105 | imported->hmac = unimported->hmac; |
| 106 | imported->iv = unimported->iv; |
| 107 | imported->pub = (const struct leaf_public_data_t *)unimported->payload; |
| 108 | imported->cipher_text = unimported->payload + unimported->head.pub_len; |
| 109 | imported->hashes = (const uint8_t (*)[PW_HASH_SIZE])( |
| 110 | imported->cipher_text + unimported->head.sec_len); |
| 111 | } |
| 112 | |
| 113 | /******************************************************************************/ |
| 114 | /* Basic operations required by the Merkle tree. |
| 115 | */ |
| 116 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 117 | /* Creates an empty merkle_tree with the given parameters. */ |
| 118 | static int create_merkle_tree(struct bits_per_level_t bits_per_level, |
| 119 | struct height_t height, |
| 120 | struct merkle_tree_t *merkle_tree) |
| 121 | { |
| 122 | uint16_t fan_out = 1 << bits_per_level.v; |
| 123 | uint8_t temp_hash[PW_HASH_SIZE] = {}; |
| 124 | uint8_t hx; |
| 125 | uint16_t kx; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 126 | pinweaver_eal_sha256_ctx_t ctx; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 127 | |
| 128 | merkle_tree->bits_per_level = bits_per_level; |
| 129 | merkle_tree->height = height; |
| 130 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 131 | int ret; |
| 132 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 133 | /* Initialize the root hash. */ |
| 134 | for (hx = 0; hx < height.v; ++hx) { |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 135 | ret = pinweaver_eal_sha256_init(&ctx); |
| 136 | if (ret) |
| 137 | return ret; |
| 138 | for (kx = 0; kx < fan_out; ++kx) { |
| 139 | ret = pinweaver_eal_sha256_update(&ctx, temp_hash, |
| 140 | PW_HASH_SIZE); |
| 141 | if (ret) { |
| 142 | pinweaver_eal_sha256_final(&ctx, temp_hash); |
| 143 | return ret; |
| 144 | } |
| 145 | } |
| 146 | ret = pinweaver_eal_sha256_final(&ctx, temp_hash); |
| 147 | if (ret) |
| 148 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 149 | } |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 150 | ret = pinweaver_eal_memcpy_s(merkle_tree->root, |
| 151 | sizeof(merkle_tree->root), temp_hash, |
| 152 | PW_HASH_SIZE); |
| 153 | if (ret != EC_SUCCESS) |
| 154 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 155 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 156 | ret = pinweaver_eal_rand_bytes( |
| 157 | merkle_tree->key_derivation_nonce, |
| 158 | sizeof(merkle_tree->key_derivation_nonce)); |
| 159 | if (ret) |
| 160 | return ret; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 161 | return pinweaver_eal_derive_keys(merkle_tree); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* 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] | 165 | static int compute_hmac(const struct merkle_tree_t *merkle_tree, |
| 166 | const struct imported_leaf_data_t *imported_leaf_data, |
| 167 | uint8_t result[PW_HASH_SIZE]) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 168 | { |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 169 | int ret; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 170 | pinweaver_eal_hmac_sha256_ctx_t hmac; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 171 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 172 | ret = pinweaver_eal_hmac_sha256_init(&hmac, merkle_tree->hmac_key, |
| 173 | sizeof(merkle_tree->hmac_key)); |
| 174 | if (ret) |
| 175 | return ret; |
| 176 | ret = pinweaver_eal_hmac_sha256_update( |
| 177 | &hmac, imported_leaf_data->head, |
| 178 | sizeof(*imported_leaf_data->head)); |
| 179 | if (ret) { |
| 180 | pinweaver_eal_hmac_sha256_final(&hmac, result); |
| 181 | return ret; |
| 182 | } |
| 183 | ret = pinweaver_eal_hmac_sha256_update(&hmac, imported_leaf_data->iv, |
| 184 | sizeof(PW_WRAP_BLOCK_SIZE)); |
| 185 | if (ret) { |
| 186 | pinweaver_eal_hmac_sha256_final(&hmac, result); |
| 187 | return ret; |
| 188 | } |
| 189 | ret = pinweaver_eal_hmac_sha256_update( |
| 190 | &hmac, imported_leaf_data->pub, |
| 191 | imported_leaf_data->head->pub_len); |
| 192 | if (ret) { |
| 193 | pinweaver_eal_hmac_sha256_final(&hmac, result); |
| 194 | return ret; |
| 195 | } |
| 196 | ret = pinweaver_eal_hmac_sha256_update( |
| 197 | &hmac, imported_leaf_data->cipher_text, |
| 198 | imported_leaf_data->head->sec_len); |
| 199 | if (ret) { |
| 200 | pinweaver_eal_hmac_sha256_final(&hmac, result); |
| 201 | return ret; |
| 202 | } |
| 203 | return pinweaver_eal_hmac_sha256_final(&hmac, result); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* Computes the root hash for the specified path and child hash. */ |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 207 | static int compute_root_hash(const struct merkle_tree_t *merkle_tree, |
| 208 | struct label_t path, |
| 209 | const uint8_t hashes[][PW_HASH_SIZE], |
| 210 | const uint8_t child_hash[PW_HASH_SIZE], |
| 211 | uint8_t new_root[PW_HASH_SIZE]) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 212 | { |
| 213 | /* This is one less than the fan out, the number of sibling hashes. */ |
| 214 | const uint16_t num_aux = (1 << merkle_tree->bits_per_level.v) - 1; |
| 215 | const uint16_t path_suffix_mask = num_aux; |
| 216 | uint8_t temp_hash[PW_HASH_SIZE]; |
| 217 | uint8_t hx = 0; |
| 218 | uint64_t index = path.v; |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 219 | int ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 220 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 221 | if (compute_hash(hashes, num_aux, |
| 222 | (struct index_t){ index & path_suffix_mask }, |
| 223 | child_hash, temp_hash)) { |
| 224 | return PW_ERR_CRYPTO_FAILURE; |
| 225 | } |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 226 | for (hx = 1; hx < merkle_tree->height.v; ++hx) { |
| 227 | hashes += num_aux; |
| 228 | index = index >> merkle_tree->bits_per_level.v; |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 229 | if (compute_hash(hashes, num_aux, |
| 230 | (struct index_t){ index & path_suffix_mask }, |
| 231 | temp_hash, temp_hash)) { |
| 232 | return PW_ERR_CRYPTO_FAILURE; |
| 233 | } |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 234 | } |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 235 | ret = pinweaver_eal_memcpy_s(new_root, PW_HASH_SIZE, temp_hash, |
| 236 | sizeof(temp_hash)); |
| 237 | if (ret != EC_SUCCESS) |
| 238 | return PW_ERR_INTERNAL_FAILURE; |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 239 | return EC_SUCCESS; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* Checks to see the specified path is valid. The length of the path should be |
| 243 | * validated prior to calling this function. |
| 244 | * |
| 245 | * Returns 0 on success or an error code otherwise. |
| 246 | */ |
| 247 | static int authenticate_path(const struct merkle_tree_t *merkle_tree, |
| 248 | struct label_t path, |
| 249 | const uint8_t hashes[][PW_HASH_SIZE], |
| 250 | const uint8_t child_hash[PW_HASH_SIZE]) |
| 251 | { |
| 252 | uint8_t parent[PW_HASH_SIZE]; |
| 253 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 254 | if (compute_root_hash(merkle_tree, path, hashes, child_hash, parent) != |
| 255 | 0) |
| 256 | return PW_ERR_CRYPTO_FAILURE; |
Yi Chou | ddf3b45 | 2021-08-11 13:40:11 +0800 | [diff] [blame] | 257 | if (pinweaver_eal_safe_memcmp(parent, merkle_tree->root, |
| 258 | sizeof(parent)) != 0) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 259 | return PW_ERR_PATH_AUTH_FAILED; |
| 260 | return EC_SUCCESS; |
| 261 | } |
| 262 | |
| 263 | static void init_wrapped_leaf_data( |
| 264 | struct wrapped_leaf_data_t *wrapped_leaf_data) |
| 265 | { |
| 266 | wrapped_leaf_data->head.leaf_version.major = PW_LEAF_MAJOR_VERSION; |
| 267 | wrapped_leaf_data->head.leaf_version.minor = PW_LEAF_MINOR_VERSION; |
| 268 | wrapped_leaf_data->head.pub_len = sizeof(wrapped_leaf_data->pub); |
| 269 | wrapped_leaf_data->head.sec_len = |
| 270 | sizeof(wrapped_leaf_data->cipher_text); |
| 271 | } |
| 272 | |
| 273 | /* Encrypts the leaf meta data. */ |
| 274 | static int encrypt_leaf_data(const struct merkle_tree_t *merkle_tree, |
| 275 | const struct leaf_data_t *leaf_data, |
| 276 | struct wrapped_leaf_data_t *wrapped_leaf_data) |
| 277 | { |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 278 | int ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 279 | /* Generate a random IV. |
| 280 | * |
| 281 | * If fields are appended to struct leaf_sensitive_data_t, an encryption |
| 282 | * operation should be performed on them reusing the same IV since the |
| 283 | * prefix won't change. |
| 284 | * |
| 285 | * If any data of in the original struct leaf_sensitive_data_t changes, |
| 286 | * a new IV should be generated and stored as part of the log for a |
| 287 | * replay to be possible. |
| 288 | */ |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 289 | if (pinweaver_eal_rand_bytes(wrapped_leaf_data->iv, |
| 290 | sizeof(wrapped_leaf_data->iv))) { |
| 291 | return PW_ERR_CRYPTO_FAILURE; |
| 292 | } |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 293 | ret = pinweaver_eal_memcpy_s(&wrapped_leaf_data->pub, |
| 294 | sizeof(wrapped_leaf_data->pub), |
| 295 | &leaf_data->pub, sizeof(leaf_data->pub)); |
| 296 | if (ret != EC_SUCCESS) |
| 297 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 1197a0d | 2021-05-14 20:18:05 -0700 | [diff] [blame] | 298 | if (pinweaver_eal_aes256_ctr(merkle_tree->wrap_key, |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 299 | sizeof(merkle_tree->wrap_key), |
| 300 | wrapped_leaf_data->iv, |
| 301 | &leaf_data->sec, |
| 302 | sizeof(leaf_data->sec), |
| 303 | wrapped_leaf_data->cipher_text)) { |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 304 | return PW_ERR_CRYPTO_FAILURE; |
| 305 | } |
| 306 | return EC_SUCCESS; |
| 307 | } |
| 308 | |
| 309 | /* Decrypts the leaf meta data. */ |
| 310 | static int decrypt_leaf_data( |
| 311 | const struct merkle_tree_t *merkle_tree, |
| 312 | const struct imported_leaf_data_t *imported_leaf_data, |
| 313 | struct leaf_data_t *leaf_data) |
| 314 | { |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 315 | int ret; |
| 316 | ret = pinweaver_eal_memcpy_s(&leaf_data->pub, sizeof(leaf_data->pub), |
| 317 | imported_leaf_data->pub, |
| 318 | MIN(imported_leaf_data->head->pub_len, |
| 319 | sizeof(struct leaf_public_data_t))); |
| 320 | if (ret != EC_SUCCESS) |
| 321 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 1197a0d | 2021-05-14 20:18:05 -0700 | [diff] [blame] | 322 | if (pinweaver_eal_aes256_ctr(merkle_tree->wrap_key, |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 323 | sizeof(merkle_tree->wrap_key), |
| 324 | imported_leaf_data->iv, |
| 325 | imported_leaf_data->cipher_text, |
| 326 | sizeof(leaf_data->sec), |
| 327 | &leaf_data->sec)) { |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 328 | return PW_ERR_CRYPTO_FAILURE; |
| 329 | } |
| 330 | return EC_SUCCESS; |
| 331 | } |
| 332 | |
| 333 | static int handle_leaf_update( |
| 334 | const struct merkle_tree_t *merkle_tree, |
| 335 | const struct leaf_data_t *leaf_data, |
| 336 | const uint8_t hashes[][PW_HASH_SIZE], |
| 337 | struct wrapped_leaf_data_t *wrapped_leaf_data, |
| 338 | uint8_t new_root[PW_HASH_SIZE], |
| 339 | const struct imported_leaf_data_t *optional_old_wrapped_data) |
| 340 | { |
| 341 | int ret; |
| 342 | struct imported_leaf_data_t ptrs; |
| 343 | |
| 344 | init_wrapped_leaf_data(wrapped_leaf_data); |
| 345 | if (optional_old_wrapped_data == NULL) { |
| 346 | ret = encrypt_leaf_data(merkle_tree, leaf_data, |
| 347 | wrapped_leaf_data); |
| 348 | if (ret != EC_SUCCESS) |
| 349 | return ret; |
| 350 | } else { |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 351 | ret = pinweaver_eal_memcpy_s(wrapped_leaf_data->iv, |
| 352 | sizeof(wrapped_leaf_data->iv), |
| 353 | optional_old_wrapped_data->iv, |
| 354 | sizeof(wrapped_leaf_data->iv)); |
| 355 | if (ret != EC_SUCCESS) |
| 356 | return PW_ERR_INTERNAL_FAILURE; |
| 357 | ret = pinweaver_eal_memcpy_s(&wrapped_leaf_data->pub, |
| 358 | sizeof(wrapped_leaf_data->pub), |
| 359 | &leaf_data->pub, |
| 360 | sizeof(leaf_data->pub)); |
| 361 | if (ret != EC_SUCCESS) |
| 362 | return PW_ERR_INTERNAL_FAILURE; |
| 363 | ret = pinweaver_eal_memcpy_s( |
| 364 | wrapped_leaf_data->cipher_text, |
| 365 | sizeof(wrapped_leaf_data->cipher_text), |
| 366 | optional_old_wrapped_data->cipher_text, |
| 367 | sizeof(wrapped_leaf_data->cipher_text)); |
| 368 | if (ret != EC_SUCCESS) |
| 369 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | import_leaf((const struct unimported_leaf_data_t *)wrapped_leaf_data, |
| 373 | &ptrs); |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 374 | ret = compute_hmac(merkle_tree, &ptrs, wrapped_leaf_data->hmac); |
| 375 | if (ret) |
| 376 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 377 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 378 | ret = compute_root_hash(merkle_tree, leaf_data->pub.label, hashes, |
| 379 | wrapped_leaf_data->hmac, new_root); |
| 380 | if (ret) |
| 381 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 382 | |
| 383 | return EC_SUCCESS; |
| 384 | } |
| 385 | |
| 386 | /******************************************************************************/ |
| 387 | /* Parameter and state validation functions. |
| 388 | */ |
| 389 | |
| 390 | static int validate_tree_parameters(struct bits_per_level_t bits_per_level, |
| 391 | struct height_t height) |
| 392 | { |
| 393 | uint8_t fan_out = 1 << bits_per_level.v; |
| 394 | |
| 395 | if (bits_per_level.v < BITS_PER_LEVEL_MIN || |
| 396 | bits_per_level.v > BITS_PER_LEVEL_MAX) |
| 397 | return PW_ERR_BITS_PER_LEVEL_INVALID; |
| 398 | |
| 399 | if (height.v < HEIGHT_MIN || |
| 400 | height.v > HEIGHT_MAX(bits_per_level.v) || |
| 401 | ((fan_out - 1) * height.v) * PW_HASH_SIZE > PW_MAX_PATH_SIZE) |
| 402 | return PW_ERR_HEIGHT_INVALID; |
| 403 | |
| 404 | return EC_SUCCESS; |
| 405 | } |
| 406 | |
| 407 | /* Verifies that merkle_tree has been initialized. */ |
| 408 | static int validate_tree(const struct merkle_tree_t *merkle_tree) |
| 409 | { |
| 410 | if (validate_tree_parameters(merkle_tree->bits_per_level, |
| 411 | merkle_tree->height) != EC_SUCCESS) |
| 412 | return PW_ERR_TREE_INVALID; |
| 413 | return EC_SUCCESS; |
| 414 | } |
| 415 | |
| 416 | /* Checks the following conditions: |
| 417 | * Extra index fields should be all zero. |
| 418 | */ |
| 419 | static int validate_label(const struct merkle_tree_t *merkle_tree, |
| 420 | struct label_t path) |
| 421 | { |
| 422 | uint8_t shift_by = merkle_tree->bits_per_level.v * |
| 423 | merkle_tree->height.v; |
| 424 | |
| 425 | if ((path.v >> shift_by) == 0) |
| 426 | return EC_SUCCESS; |
| 427 | return PW_ERR_LABEL_INVALID; |
| 428 | } |
| 429 | |
| 430 | /* Checks the following conditions: |
| 431 | * Columns should be strictly increasing. |
| 432 | * Zeroes for filler at the end of the delay_schedule are permitted. |
| 433 | */ |
| 434 | static int validate_delay_schedule(const struct delay_schedule_entry_t |
| 435 | delay_schedule[PW_SCHED_COUNT]) |
| 436 | { |
| 437 | size_t x; |
| 438 | |
| 439 | /* The first entry should not be useless. */ |
Yi Chou | eed2b65 | 2021-08-11 13:47:15 +0800 | [diff] [blame] | 440 | if (delay_schedule[0].attempt_count.v == 0 || |
| 441 | delay_schedule[0].time_diff.v == 0) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 442 | return PW_ERR_DELAY_SCHEDULE_INVALID; |
| 443 | |
| 444 | for (x = PW_SCHED_COUNT - 1; x > 0; --x) { |
| 445 | if (delay_schedule[x].attempt_count.v == 0) { |
| 446 | if (delay_schedule[x].time_diff.v != 0) |
| 447 | return PW_ERR_DELAY_SCHEDULE_INVALID; |
| 448 | } else if (delay_schedule[x].attempt_count.v <= |
| 449 | delay_schedule[x - 1].attempt_count.v || |
| 450 | delay_schedule[x].time_diff.v <= |
| 451 | delay_schedule[x - 1].time_diff.v) { |
| 452 | return PW_ERR_DELAY_SCHEDULE_INVALID; |
| 453 | } |
| 454 | } |
| 455 | return EC_SUCCESS; |
| 456 | } |
| 457 | |
| 458 | static int validate_pcr_value(const struct valid_pcr_value_t |
| 459 | valid_pcr_criteria[PW_MAX_PCR_CRITERIA_COUNT]) |
| 460 | { |
| 461 | size_t index; |
| 462 | uint8_t sha256_of_selected_pcr[SHA256_DIGEST_SIZE]; |
| 463 | |
| 464 | for (index = 0; index < PW_MAX_PCR_CRITERIA_COUNT; ++index) { |
| 465 | /* The criteria with bitmask[0] = bitmask[1] = 0 is considered |
| 466 | * the end of list criteria. If it happens that the first |
| 467 | * bitmask is zero, we consider that no criteria has to be |
| 468 | * satisfied and return success in that case. |
| 469 | */ |
| 470 | if (valid_pcr_criteria[index].bitmask[0] == 0 && |
| 471 | valid_pcr_criteria[index].bitmask[1] == 0) { |
| 472 | if (index == 0) |
| 473 | return EC_SUCCESS; |
| 474 | |
| 475 | return PW_ERR_PCR_NOT_MATCH; |
| 476 | } |
| 477 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 478 | if (pinweaver_eal_get_current_pcr_digest( |
| 479 | valid_pcr_criteria[index].bitmask, |
| 480 | sha256_of_selected_pcr)) { |
| 481 | PINWEAVER_EAL_INFO( |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 482 | "PinWeaver: Read PCR error, bitmask: %d, %d", |
| 483 | valid_pcr_criteria[index].bitmask[0], |
| 484 | valid_pcr_criteria[index].bitmask[1]); |
| 485 | return PW_ERR_PCR_NOT_MATCH; |
| 486 | } |
| 487 | |
| 488 | /* Check if the curent PCR digest is the same as expected by |
| 489 | * criteria. |
| 490 | */ |
Yi Chou | ddf3b45 | 2021-08-11 13:40:11 +0800 | [diff] [blame] | 491 | if (pinweaver_eal_safe_memcmp(sha256_of_selected_pcr, |
| 492 | valid_pcr_criteria[index].digest, |
| 493 | SHA256_DIGEST_SIZE) == 0) { |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 494 | return EC_SUCCESS; |
| 495 | } |
| 496 | } |
| 497 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 498 | PINWEAVER_EAL_INFO("PinWeaver: No criteria matches PCR values"); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 499 | return PW_ERR_PCR_NOT_MATCH; |
| 500 | } |
| 501 | |
Leo Lai | 8c9892c | 2021-06-22 21:32:02 +0800 | [diff] [blame] | 502 | static uint32_t expected_payload_len(int minor_version) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 503 | { |
| 504 | switch (minor_version) { |
| 505 | case 0: |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 506 | #if !BIOMETRICS_DEV |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 507 | return PW_LEAF_PAYLOAD_SIZE - PW_VALID_PCR_CRITERIA_SIZE; |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 508 | #else |
| 509 | return PW_LEAF_PAYLOAD_SIZE - PW_VALID_PCR_CRITERIA_SIZE |
| 510 | - PW_EXPIRATION_DATA_SIZE - sizeof(struct pw_leaf_type_t); |
| 511 | case 1: |
| 512 | return PW_LEAF_PAYLOAD_SIZE - PW_EXPIRATION_DATA_SIZE |
| 513 | - sizeof(struct pw_leaf_type_t); |
| 514 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 515 | case PW_LEAF_MINOR_VERSION: |
| 516 | return PW_LEAF_PAYLOAD_SIZE; |
| 517 | default: |
| 518 | return 0; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | static int validate_leaf_header(const struct leaf_header_t *head, |
| 523 | uint16_t payload_len, uint16_t aux_hash_len) |
| 524 | { |
| 525 | uint32_t leaf_payload_len = head->pub_len + head->sec_len; |
| 526 | |
| 527 | if (head->leaf_version.major != PW_LEAF_MAJOR_VERSION) |
| 528 | return PW_ERR_LEAF_VERSION_MISMATCH; |
| 529 | |
| 530 | if (head->leaf_version.minor <= PW_LEAF_MINOR_VERSION && |
| 531 | leaf_payload_len != |
| 532 | expected_payload_len(head->leaf_version.minor)) { |
| 533 | return PW_ERR_LENGTH_INVALID; |
| 534 | } |
| 535 | |
| 536 | if (payload_len != leaf_payload_len + aux_hash_len * PW_HASH_SIZE) |
| 537 | return PW_ERR_LENGTH_INVALID; |
| 538 | |
Yi Chou | e416ce3 | 2021-08-11 19:57:24 +0800 | [diff] [blame] | 539 | if (head->sec_len < sizeof(struct leaf_sensitive_data_t)) |
| 540 | return PW_ERR_LENGTH_INVALID; |
| 541 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 542 | return EC_SUCCESS; |
| 543 | } |
| 544 | |
| 545 | /* Common validation for requests that include a path to authenticate. */ |
| 546 | static int validate_request_with_path(const struct merkle_tree_t *merkle_tree, |
| 547 | struct label_t path, |
| 548 | const uint8_t hashes[][PW_HASH_SIZE], |
| 549 | const uint8_t hmac[PW_HASH_SIZE]) |
| 550 | { |
| 551 | int ret; |
| 552 | |
| 553 | ret = validate_tree(merkle_tree); |
| 554 | if (ret != EC_SUCCESS) |
| 555 | return ret; |
| 556 | |
| 557 | ret = validate_label(merkle_tree, path); |
| 558 | if (ret != EC_SUCCESS) |
| 559 | return ret; |
| 560 | |
| 561 | return authenticate_path(merkle_tree, path, hashes, hmac); |
| 562 | } |
| 563 | |
| 564 | /* Common validation for requests that import a leaf. */ |
| 565 | static int validate_request_with_wrapped_leaf( |
| 566 | const struct merkle_tree_t *merkle_tree, |
| 567 | uint16_t payload_len, |
| 568 | const struct unimported_leaf_data_t *unimported_leaf_data, |
| 569 | struct imported_leaf_data_t *imported_leaf_data, |
| 570 | struct leaf_data_t *leaf_data) |
| 571 | { |
| 572 | int ret; |
| 573 | uint8_t hmac[PW_HASH_SIZE]; |
| 574 | |
| 575 | ret = validate_leaf_header(&unimported_leaf_data->head, payload_len, |
| 576 | get_path_auxiliary_hash_count(merkle_tree)); |
| 577 | if (ret != EC_SUCCESS) |
| 578 | return ret; |
| 579 | |
| 580 | import_leaf(unimported_leaf_data, imported_leaf_data); |
| 581 | ret = validate_request_with_path(merkle_tree, |
| 582 | imported_leaf_data->pub->label, |
| 583 | imported_leaf_data->hashes, |
| 584 | imported_leaf_data->hmac); |
| 585 | if (ret != EC_SUCCESS) |
| 586 | return ret; |
| 587 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 588 | ret = compute_hmac(merkle_tree, imported_leaf_data, hmac); |
| 589 | if (ret != EC_SUCCESS) |
| 590 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 591 | /* Safe memcmp is used here to prevent an attacker from being able to |
| 592 | * brute force a valid HMAC for a crafted wrapped_leaf_data. |
| 593 | * memcmp provides an attacker a timing side-channel they can use to |
| 594 | * determine how much of a prefix is correct. |
| 595 | */ |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 596 | if (pinweaver_eal_safe_memcmp(hmac, unimported_leaf_data->hmac, |
| 597 | sizeof(hmac))) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 598 | return PW_ERR_HMAC_AUTH_FAILED; |
| 599 | |
| 600 | ret = decrypt_leaf_data(merkle_tree, imported_leaf_data, leaf_data); |
| 601 | if (ret != EC_SUCCESS) |
| 602 | return ret; |
| 603 | |
| 604 | /* The code below handles version upgrades. */ |
| 605 | if (unimported_leaf_data->head.leaf_version.minor == 0 && |
| 606 | unimported_leaf_data->head.leaf_version.major == 0) { |
| 607 | /* Populate the leaf_data with default pcr value */ |
| 608 | memset(&leaf_data->pub.valid_pcr_criteria, 0, |
| 609 | PW_VALID_PCR_CRITERIA_SIZE); |
| 610 | } |
| 611 | |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 612 | #if BIOMETRICS_DEV |
| 613 | if (unimported_leaf_data->head.leaf_version.major == 0 && |
| 614 | unimported_leaf_data->head.leaf_version.minor <= 1) { |
| 615 | /* Populate the leaf_data with default expiration timestamp value, |
| 616 | * expiration_delay_s will be set to 0 too, meaning the leaf won't |
| 617 | * expire. |
| 618 | */ |
| 619 | memset(&leaf_data->pub.expiration_ts, 0, PW_EXPIRATION_DATA_SIZE); |
| 620 | /* Populate the leaf_data with default leaf type. */ |
| 621 | leaf_data->pub.leaf_type.v = PW_LEAF_TYPE_NORMAL; |
| 622 | } |
| 623 | #endif |
| 624 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 625 | return EC_SUCCESS; |
| 626 | } |
| 627 | |
| 628 | /* Sets the value of ts to the current notion of time. */ |
| 629 | static void update_timestamp(struct pw_timestamp_t *ts) |
| 630 | { |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 631 | ts->timer_value = pinweaver_eal_seconds_since_boot(); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 632 | ts->boot_count = pw_restart_count; |
| 633 | } |
| 634 | |
Howard Yang | c96c5e8 | 2022-07-29 17:11:22 +0800 | [diff] [blame] | 635 | /* Checks if an auth attempt can be made or not based on the delay schedule: |
| 636 | * - EC_SUCCESS is returned when a new attempt can be made, |
| 637 | * - PW_ERR_EXPIRED is returned if the leaf is expired, and otherwise |
| 638 | * - PW_ERR_RATE_LIMIT_REACHED is returned with seconds_to_wait updated to |
| 639 | * the remaining waiting time required. |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 640 | */ |
| 641 | static int test_rate_limit(struct leaf_data_t *leaf_data, |
| 642 | struct time_diff_t *seconds_to_wait) |
| 643 | { |
| 644 | uint64_t ready_time; |
| 645 | uint8_t x; |
| 646 | struct pw_timestamp_t current_time; |
| 647 | struct time_diff_t delay = {0}; |
| 648 | |
Howard Yang | c96c5e8 | 2022-07-29 17:11:22 +0800 | [diff] [blame] | 649 | update_timestamp(¤t_time); |
| 650 | |
| 651 | #if BIOMETRICS_DEV |
| 652 | if (leaf_data->pub.expiration_delay_s.v != 0 && |
| 653 | (leaf_data->pub.expiration_ts.boot_count != current_time.boot_count || |
| 654 | leaf_data->pub.expiration_ts.timer_value <= current_time.timer_value)) { |
| 655 | return PW_ERR_EXPIRED; |
| 656 | } |
| 657 | #endif |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 658 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 659 | /* This loop ends when x is one greater than the index that applies. */ |
| 660 | for (x = 0; x < ARRAY_SIZE(leaf_data->pub.delay_schedule); ++x) { |
| 661 | /* Stop if a null entry is reached. The first part of the delay |
| 662 | * schedule has a list of increasing (attempt_count, time_diff) |
| 663 | * pairs with any unused entries zeroed out at the end. |
| 664 | */ |
| 665 | if (leaf_data->pub.delay_schedule[x].attempt_count.v == 0) |
| 666 | break; |
| 667 | |
| 668 | /* Stop once a delay schedule entry is reached whose |
| 669 | * threshold is greater than the current number of |
| 670 | * attempts. |
| 671 | */ |
| 672 | if (leaf_data->pub.attempt_count.v < |
| 673 | leaf_data->pub.delay_schedule[x].attempt_count.v) |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | /* If the first threshold was greater than the current number of |
| 678 | * attempts, there is no delay. Otherwise, grab the delay from the |
| 679 | * entry prior to the one that was too big. |
| 680 | */ |
| 681 | if (x > 0) |
| 682 | delay = leaf_data->pub.delay_schedule[x - 1].time_diff; |
| 683 | |
| 684 | if (delay.v == 0) |
| 685 | return EC_SUCCESS; |
| 686 | |
| 687 | if (delay.v == PW_BLOCK_ATTEMPTS) { |
| 688 | seconds_to_wait->v = PW_BLOCK_ATTEMPTS; |
| 689 | return PW_ERR_RATE_LIMIT_REACHED; |
| 690 | } |
| 691 | |
Howard Yang | 2b189b2 | 2022-07-29 11:11:34 +0800 | [diff] [blame] | 692 | if (leaf_data->pub.last_access_ts.boot_count == current_time.boot_count) |
| 693 | ready_time = delay.v + leaf_data->pub.last_access_ts.timer_value; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 694 | else |
| 695 | ready_time = delay.v; |
| 696 | |
| 697 | if (current_time.timer_value >= ready_time) |
| 698 | return EC_SUCCESS; |
| 699 | |
| 700 | seconds_to_wait->v = ready_time - current_time.timer_value; |
| 701 | return PW_ERR_RATE_LIMIT_REACHED; |
| 702 | } |
| 703 | |
| 704 | /******************************************************************************/ |
| 705 | /* Logging implementation. |
| 706 | */ |
| 707 | |
| 708 | /* Once the storage version is incremented, the update code needs to be written |
| 709 | * to handle differences in the structs. |
| 710 | * |
| 711 | * See the two comments "Add storage format updates here." below. |
| 712 | */ |
| 713 | BUILD_ASSERT(PW_STORAGE_VERSION == 0); |
| 714 | |
| 715 | void force_restart_count(uint32_t mock_value) |
| 716 | { |
| 717 | pw_restart_count = mock_value; |
| 718 | } |
| 719 | |
| 720 | /* Returns EC_SUCCESS if the root hash was found. Sets *index to the first index |
| 721 | * of the log entry with a matching root hash, or the index of the last valid |
| 722 | * entry. |
| 723 | */ |
| 724 | static int find_relevant_entry(const struct pw_log_storage_t *log, |
| 725 | const uint8_t root[PW_HASH_SIZE], int *index) |
| 726 | { |
| 727 | /* Find the relevant log entry. */ |
| 728 | for (*index = 0; *index < PW_LOG_ENTRY_COUNT; ++*index) { |
Howard Yang | c2f080c | 2022-07-29 11:28:13 +0800 | [diff] [blame] | 729 | if (log->entries[*index].type.v == LOG_PW_MT_INVALID) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 730 | break; |
Yi Chou | ddf3b45 | 2021-08-11 13:40:11 +0800 | [diff] [blame] | 731 | if (pinweaver_eal_safe_memcmp(root, log->entries[*index].root, |
| 732 | PW_HASH_SIZE) == 0) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 733 | return EC_SUCCESS; |
| 734 | } |
| 735 | --*index; |
| 736 | return PW_ERR_ROOT_NOT_FOUND; |
| 737 | } |
| 738 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 739 | /* TODO(apronin): get rid of temporary redirect methods */ |
| 740 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 741 | static int load_log_data(struct pw_log_storage_t *log) |
| 742 | { |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 743 | return pinweaver_eal_storage_get_log(log); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | int store_log_data(const struct pw_log_storage_t *log) |
| 747 | { |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 748 | return pinweaver_eal_storage_set_log(log); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static int load_merkle_tree(struct merkle_tree_t *merkle_tree) |
| 752 | { |
| 753 | int ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 754 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 755 | PINWEAVER_EAL_INFO("PinWeaver: Loading Tree!"); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 756 | |
| 757 | /* Handle the immutable data. */ |
| 758 | { |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 759 | struct pw_long_term_storage_t tree; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 760 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 761 | ret = pinweaver_eal_storage_get_tree_data(&tree); |
| 762 | if (ret != EC_SUCCESS) |
| 763 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 764 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 765 | merkle_tree->bits_per_level = tree.bits_per_level; |
| 766 | merkle_tree->height = tree.height; |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 767 | ret = pinweaver_eal_memcpy_s( |
| 768 | merkle_tree->key_derivation_nonce, |
| 769 | sizeof(merkle_tree->key_derivation_nonce), |
| 770 | tree.key_derivation_nonce, |
| 771 | sizeof(tree.key_derivation_nonce)); |
| 772 | if (ret != EC_SUCCESS) |
| 773 | return ret; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 774 | ret = pinweaver_eal_derive_keys(merkle_tree); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 775 | if (ret != EC_SUCCESS) |
| 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | /* Handle the root hash. */ |
| 780 | { |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 781 | ret = pinweaver_eal_storage_init_state(merkle_tree->root, |
| 782 | &pw_restart_count); |
| 783 | if (ret != EC_SUCCESS) |
| 784 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 787 | PINWEAVER_EAL_INFO("PinWeaver: Loaded Tree. restart_count = %d", |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 788 | pw_restart_count); |
| 789 | |
| 790 | return EC_SUCCESS; |
| 791 | } |
| 792 | |
| 793 | /* This should only be called when a new tree is created. */ |
| 794 | int store_merkle_tree(const struct merkle_tree_t *merkle_tree) |
| 795 | { |
| 796 | int ret; |
| 797 | |
| 798 | /* Handle the immutable data. */ |
| 799 | { |
| 800 | struct pw_long_term_storage_t data; |
| 801 | |
| 802 | data.storage_version = PW_STORAGE_VERSION; |
| 803 | data.bits_per_level = merkle_tree->bits_per_level; |
| 804 | data.height = merkle_tree->height; |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 805 | ret = pinweaver_eal_memcpy_s(data.key_derivation_nonce, |
| 806 | sizeof(data.key_derivation_nonce), |
| 807 | merkle_tree->key_derivation_nonce, |
| 808 | sizeof(data.key_derivation_nonce)); |
| 809 | if (ret != EC_SUCCESS) |
| 810 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 811 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 812 | ret = pinweaver_eal_storage_set_tree_data(&data); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 813 | if (ret != EC_SUCCESS) |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | /* Handle the root hash. */ |
| 818 | { |
| 819 | struct pw_log_storage_t log = {}; |
| 820 | struct pw_get_log_entry_t *entry = log.entries; |
| 821 | |
| 822 | log.storage_version = PW_STORAGE_VERSION; |
Howard Yang | c2f080c | 2022-07-29 11:28:13 +0800 | [diff] [blame] | 823 | entry->type.v = LOG_PW_RESET_TREE; |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 824 | ret = pinweaver_eal_memcpy_s(entry->root, sizeof(entry->root), |
| 825 | merkle_tree->root, |
| 826 | sizeof(merkle_tree->root)); |
| 827 | if (ret != EC_SUCCESS) |
| 828 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 829 | |
| 830 | ret = store_log_data(&log); |
| 831 | if (ret == EC_SUCCESS) |
| 832 | pw_restart_count = 0; |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | } |
| 837 | |
| 838 | static int log_roll_for_append(struct pw_log_storage_t *log) |
| 839 | { |
| 840 | int ret; |
| 841 | |
| 842 | ret = load_log_data(log); |
| 843 | if (ret != EC_SUCCESS) |
| 844 | return ret; |
| 845 | |
| 846 | memmove(&log->entries[1], &log->entries[0], |
| 847 | sizeof(log->entries[0]) * (PW_LOG_ENTRY_COUNT - 1)); |
| 848 | memset(&log->entries[0], 0, sizeof(log->entries[0])); |
| 849 | return EC_SUCCESS; |
| 850 | } |
| 851 | |
| 852 | int log_insert_leaf(struct label_t label, const uint8_t root[PW_HASH_SIZE], |
| 853 | const uint8_t hmac[PW_HASH_SIZE]) |
| 854 | { |
| 855 | int ret; |
| 856 | struct pw_log_storage_t log; |
| 857 | struct pw_get_log_entry_t *entry = log.entries; |
| 858 | |
| 859 | ret = log_roll_for_append(&log); |
| 860 | if (ret != EC_SUCCESS) |
| 861 | return ret; |
| 862 | |
Howard Yang | c2f080c | 2022-07-29 11:28:13 +0800 | [diff] [blame] | 863 | entry->type.v = LOG_PW_INSERT_LEAF; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 864 | entry->label.v = label.v; |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 865 | ret = pinweaver_eal_memcpy_s(entry->root, sizeof(entry->root), root, |
| 866 | sizeof(entry->root)); |
| 867 | if (ret != EC_SUCCESS) |
| 868 | return ret; |
| 869 | ret = pinweaver_eal_memcpy_s(entry->leaf_hmac, sizeof(entry->leaf_hmac), |
| 870 | hmac, sizeof(entry->leaf_hmac)); |
| 871 | if (ret != EC_SUCCESS) |
| 872 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 873 | |
| 874 | return store_log_data(&log); |
| 875 | } |
| 876 | |
| 877 | int log_remove_leaf(struct label_t label, const uint8_t root[PW_HASH_SIZE]) |
| 878 | { |
| 879 | int ret; |
| 880 | struct pw_log_storage_t log; |
| 881 | struct pw_get_log_entry_t *entry = log.entries; |
| 882 | |
| 883 | ret = log_roll_for_append(&log); |
| 884 | if (ret != EC_SUCCESS) |
| 885 | return ret; |
| 886 | |
Howard Yang | c2f080c | 2022-07-29 11:28:13 +0800 | [diff] [blame] | 887 | entry->type.v = LOG_PW_REMOVE_LEAF; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 888 | entry->label.v = label.v; |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 889 | ret = pinweaver_eal_memcpy_s(entry->root, sizeof(entry->root), root, |
| 890 | sizeof(entry->root)); |
| 891 | if (ret != EC_SUCCESS) |
| 892 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 893 | |
| 894 | return store_log_data(&log); |
| 895 | } |
| 896 | |
| 897 | int log_auth(struct label_t label, const uint8_t root[PW_HASH_SIZE], int code, |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 898 | struct pw_timestamp_t last_access_ts, |
| 899 | struct pw_timestamp_t expiration_ts) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 900 | { |
| 901 | int ret; |
| 902 | struct pw_log_storage_t log; |
| 903 | struct pw_get_log_entry_t *entry = log.entries; |
| 904 | |
| 905 | ret = log_roll_for_append(&log); |
| 906 | if (ret != EC_SUCCESS) |
| 907 | return ret; |
| 908 | |
Howard Yang | c2f080c | 2022-07-29 11:28:13 +0800 | [diff] [blame] | 909 | entry->type.v = LOG_PW_TRY_AUTH; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 910 | entry->label.v = label.v; |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 911 | ret = pinweaver_eal_memcpy_s(entry->root, sizeof(entry->root), root, |
| 912 | sizeof(entry->root)); |
| 913 | if (ret != EC_SUCCESS) |
| 914 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 915 | entry->return_code = code; |
Howard Yang | 2b189b2 | 2022-07-29 11:11:34 +0800 | [diff] [blame] | 916 | ret = pinweaver_eal_memcpy_s(&entry->last_access_ts, |
| 917 | sizeof(entry->last_access_ts), &last_access_ts, |
| 918 | sizeof(entry->last_access_ts)); |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 919 | if (ret != EC_SUCCESS) |
| 920 | return ret; |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 921 | #if BIOMETRICS_DEV |
| 922 | ret = pinweaver_eal_memcpy_s(&entry->expiration_ts, |
| 923 | sizeof(entry->expiration_ts), &expiration_ts, |
| 924 | sizeof(entry->expiration_ts)); |
| 925 | if (ret != EC_SUCCESS) |
| 926 | return ret; |
| 927 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 928 | |
| 929 | return store_log_data(&log); |
| 930 | } |
| 931 | |
| 932 | /******************************************************************************/ |
| 933 | /* Per-request-type handler implementations. |
| 934 | */ |
| 935 | |
| 936 | static int pw_handle_reset_tree(struct merkle_tree_t *merkle_tree, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 937 | const pw_request_reset_tree_t *request, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 938 | uint16_t req_size) |
| 939 | { |
| 940 | struct merkle_tree_t new_tree = {}; |
| 941 | int ret; |
| 942 | |
| 943 | if (req_size != sizeof(*request)) |
| 944 | return PW_ERR_LENGTH_INVALID; |
| 945 | |
| 946 | ret = validate_tree_parameters(request->bits_per_level, |
| 947 | request->height); |
| 948 | if (ret != EC_SUCCESS) |
| 949 | return ret; |
| 950 | |
| 951 | ret = create_merkle_tree(request->bits_per_level, request->height, |
| 952 | &new_tree); |
| 953 | if (ret != EC_SUCCESS) |
| 954 | return ret; |
| 955 | |
| 956 | ret = store_merkle_tree(&new_tree); |
| 957 | if (ret != EC_SUCCESS) |
| 958 | return ret; |
| 959 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 960 | ret = pinweaver_eal_memcpy_s(merkle_tree, sizeof(*merkle_tree), |
| 961 | &new_tree, sizeof(new_tree)); |
| 962 | if (ret != EC_SUCCESS) |
| 963 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 964 | return EC_SUCCESS; |
| 965 | } |
| 966 | |
| 967 | static int pw_handle_insert_leaf(struct merkle_tree_t *merkle_tree, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 968 | const pw_request_insert_leaf_t *request, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 969 | uint16_t req_size, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 970 | pw_response_insert_leaf_t *response, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 971 | uint16_t *response_size) |
| 972 | { |
| 973 | int ret = EC_SUCCESS; |
| 974 | struct leaf_data_t leaf_data = {}; |
Leo Lai | 6574908 | 2021-08-10 16:32:18 +0800 | [diff] [blame] | 975 | struct wrapped_leaf_data_t wrapped_leaf_data = {}; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 976 | const uint8_t empty_hash[PW_HASH_SIZE] = {}; |
| 977 | uint8_t new_root[PW_HASH_SIZE]; |
Howard Yang | c96c5e8 | 2022-07-29 17:11:22 +0800 | [diff] [blame] | 978 | #if BIOMETRICS_DEV |
| 979 | uint32_t delay_s; |
| 980 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 981 | |
| 982 | if (req_size != sizeof(*request) + |
| 983 | get_path_auxiliary_hash_count(merkle_tree) * |
| 984 | PW_HASH_SIZE) |
| 985 | return PW_ERR_LENGTH_INVALID; |
| 986 | |
| 987 | ret = validate_request_with_path(merkle_tree, request->label, |
| 988 | request->path_hashes, empty_hash); |
| 989 | if (ret != EC_SUCCESS) |
| 990 | return ret; |
| 991 | |
| 992 | ret = validate_delay_schedule(request->delay_schedule); |
| 993 | if (ret != EC_SUCCESS) |
| 994 | return ret; |
| 995 | |
| 996 | memset(&leaf_data, 0, sizeof(leaf_data)); |
| 997 | leaf_data.pub.label.v = request->label.v; |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 998 | ret = pinweaver_eal_memcpy_s(&leaf_data.pub.valid_pcr_criteria, |
| 999 | sizeof(leaf_data.pub.valid_pcr_criteria), |
| 1000 | request->valid_pcr_criteria, |
| 1001 | sizeof(request->valid_pcr_criteria)); |
| 1002 | if (ret != EC_SUCCESS) |
| 1003 | return PW_ERR_INTERNAL_FAILURE; |
| 1004 | ret = pinweaver_eal_memcpy_s(&leaf_data.pub.delay_schedule, |
| 1005 | sizeof(leaf_data.pub.delay_schedule), |
| 1006 | &request->delay_schedule, |
| 1007 | sizeof(request->delay_schedule)); |
| 1008 | if (ret != EC_SUCCESS) |
| 1009 | return PW_ERR_INTERNAL_FAILURE; |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1010 | |
| 1011 | #if BIOMETRICS_DEV |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1012 | if (request->expiration_delay_s.v != 0) { |
Howard Yang | c96c5e8 | 2022-07-29 17:11:22 +0800 | [diff] [blame] | 1013 | update_timestamp(&leaf_data.pub.expiration_ts); |
| 1014 | delay_s = request->expiration_delay_s.v; |
| 1015 | if (leaf_data.pub.expiration_ts.timer_value <= UINT64_MAX - delay_s) { |
| 1016 | leaf_data.pub.expiration_ts.timer_value += delay_s; |
| 1017 | } else { |
| 1018 | // Timer has become too large, and expiration won't work properly. |
| 1019 | return PW_ERR_INTERNAL_FAILURE; |
| 1020 | } |
| 1021 | leaf_data.pub.expiration_delay_s.v = delay_s; |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /* TODO: Support biometrics leaf type. */ |
| 1025 | if (request->leaf_type.v != PW_LEAF_TYPE_NORMAL) { |
| 1026 | return PW_ERR_INTERNAL_FAILURE; |
| 1027 | } |
| 1028 | #endif |
| 1029 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1030 | ret = pinweaver_eal_memcpy_s(&leaf_data.sec.low_entropy_secret, |
| 1031 | sizeof(leaf_data.sec.low_entropy_secret), |
| 1032 | &request->low_entropy_secret, |
| 1033 | sizeof(request->low_entropy_secret)); |
| 1034 | if (ret != EC_SUCCESS) |
| 1035 | return PW_ERR_INTERNAL_FAILURE; |
| 1036 | ret = pinweaver_eal_memcpy_s(&leaf_data.sec.high_entropy_secret, |
| 1037 | sizeof(leaf_data.sec.high_entropy_secret), |
| 1038 | &request->high_entropy_secret, |
| 1039 | sizeof(request->high_entropy_secret)); |
| 1040 | if (ret != EC_SUCCESS) |
| 1041 | return PW_ERR_INTERNAL_FAILURE; |
| 1042 | ret = pinweaver_eal_memcpy_s(&leaf_data.sec.reset_secret, |
| 1043 | sizeof(leaf_data.sec.reset_secret), |
| 1044 | &request->reset_secret, |
| 1045 | sizeof(request->reset_secret)); |
| 1046 | if (ret != EC_SUCCESS) |
| 1047 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1048 | |
| 1049 | ret = handle_leaf_update(merkle_tree, &leaf_data, request->path_hashes, |
| 1050 | &wrapped_leaf_data, new_root, NULL); |
| 1051 | if (ret != EC_SUCCESS) |
| 1052 | return ret; |
| 1053 | |
| 1054 | ret = log_insert_leaf(request->label, new_root, |
| 1055 | wrapped_leaf_data.hmac); |
| 1056 | if (ret != EC_SUCCESS) |
| 1057 | return ret; |
| 1058 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1059 | ret = pinweaver_eal_memcpy_s(merkle_tree->root, |
| 1060 | sizeof(merkle_tree->root), new_root, |
| 1061 | sizeof(new_root)); |
| 1062 | if (ret != EC_SUCCESS) |
| 1063 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1064 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1065 | ret = pinweaver_eal_memcpy_s(&response->unimported_leaf_data, |
| 1066 | sizeof(wrapped_leaf_data), |
| 1067 | &wrapped_leaf_data, |
| 1068 | sizeof(wrapped_leaf_data)); |
| 1069 | if (ret != EC_SUCCESS) |
| 1070 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1071 | |
| 1072 | *response_size = sizeof(*response) + PW_LEAF_PAYLOAD_SIZE; |
| 1073 | |
| 1074 | return ret; |
| 1075 | } |
| 1076 | |
| 1077 | static int pw_handle_remove_leaf(struct merkle_tree_t *merkle_tree, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 1078 | const pw_request_remove_leaf_t *request, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1079 | uint16_t req_size) |
| 1080 | { |
| 1081 | int ret = EC_SUCCESS; |
| 1082 | const uint8_t empty_hash[PW_HASH_SIZE] = {}; |
| 1083 | uint8_t new_root[PW_HASH_SIZE]; |
| 1084 | |
| 1085 | if (req_size != sizeof(*request) + |
| 1086 | get_path_auxiliary_hash_count(merkle_tree) * |
| 1087 | PW_HASH_SIZE) |
| 1088 | return PW_ERR_LENGTH_INVALID; |
| 1089 | |
| 1090 | ret = validate_request_with_path(merkle_tree, request->leaf_location, |
| 1091 | request->path_hashes, |
| 1092 | request->leaf_hmac); |
| 1093 | if (ret != EC_SUCCESS) |
| 1094 | return ret; |
| 1095 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1096 | ret = compute_root_hash(merkle_tree, request->leaf_location, |
| 1097 | request->path_hashes, empty_hash, new_root); |
| 1098 | if (ret != EC_SUCCESS) |
| 1099 | return ret; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1100 | |
| 1101 | ret = log_remove_leaf(request->leaf_location, new_root); |
| 1102 | if (ret != EC_SUCCESS) |
| 1103 | return ret; |
| 1104 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1105 | ret = pinweaver_eal_memcpy_s(merkle_tree->root, |
| 1106 | sizeof(merkle_tree->root), new_root, |
| 1107 | sizeof(new_root)); |
| 1108 | if (ret != EC_SUCCESS) |
| 1109 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1110 | return ret; |
| 1111 | } |
| 1112 | |
| 1113 | /* Processes a try_auth request. |
| 1114 | * |
| 1115 | * The valid fields in response based on return code are: |
| 1116 | * EC_SUCCESS -> unimported_leaf_data and high_entropy_secret |
| 1117 | * PW_ERR_RATE_LIMIT_REACHED -> seconds_to_wait |
| 1118 | * PW_ERR_LOWENT_AUTH_FAILED -> unimported_leaf_data |
| 1119 | */ |
| 1120 | static int pw_handle_try_auth(struct merkle_tree_t *merkle_tree, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 1121 | const pw_request_try_auth_t *request, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1122 | uint16_t req_size, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 1123 | pw_response_try_auth_t *response, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1124 | uint16_t *data_length) |
| 1125 | { |
| 1126 | int ret = EC_SUCCESS; |
| 1127 | struct leaf_data_t leaf_data = {}; |
| 1128 | struct imported_leaf_data_t imported_leaf_data; |
Andrey Pronin | 8ec6dc5 | 2021-08-12 14:00:09 -0700 | [diff] [blame] | 1129 | struct wrapped_leaf_data_t wrapped_leaf_data = {}; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1130 | struct time_diff_t seconds_to_wait; |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1131 | struct pw_timestamp_t expiration_ts = {}; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1132 | uint8_t zeros[PW_SECRET_SIZE] = {}; |
| 1133 | uint8_t new_root[PW_HASH_SIZE]; |
| 1134 | |
| 1135 | /* These variables help eliminate the possibility of a timing side |
| 1136 | * channel that would allow an attacker to prevent the log write. |
| 1137 | */ |
| 1138 | volatile int auth_result; |
| 1139 | |
| 1140 | volatile struct { |
| 1141 | uint32_t attempts; |
| 1142 | int ret; |
| 1143 | uint8_t *secret; |
| 1144 | uint8_t *reset_secret; |
| 1145 | } results_table[2] = { |
| 1146 | { 0, PW_ERR_LOWENT_AUTH_FAILED, zeros, zeros }, |
| 1147 | { 0, EC_SUCCESS, leaf_data.sec.high_entropy_secret, |
| 1148 | leaf_data.sec.reset_secret }, |
| 1149 | }; |
| 1150 | |
| 1151 | if (req_size < sizeof(*request)) |
| 1152 | return PW_ERR_LENGTH_INVALID; |
| 1153 | |
| 1154 | ret = validate_request_with_wrapped_leaf( |
| 1155 | merkle_tree, req_size - sizeof(*request), |
| 1156 | &request->unimported_leaf_data, &imported_leaf_data, |
| 1157 | &leaf_data); |
| 1158 | if (ret != EC_SUCCESS) |
| 1159 | return ret; |
| 1160 | |
| 1161 | /* Check if at least one PCR criteria is satisfied if the leaf is |
| 1162 | * bound to PCR. |
| 1163 | */ |
| 1164 | ret = validate_pcr_value(leaf_data.pub.valid_pcr_criteria); |
| 1165 | if (ret != EC_SUCCESS) |
| 1166 | return ret; |
| 1167 | |
| 1168 | ret = test_rate_limit(&leaf_data, &seconds_to_wait); |
| 1169 | if (ret != EC_SUCCESS) { |
| 1170 | *data_length = sizeof(*response) + PW_LEAF_PAYLOAD_SIZE; |
| 1171 | memset(response, 0, *data_length); |
Howard Yang | c96c5e8 | 2022-07-29 17:11:22 +0800 | [diff] [blame] | 1172 | if (ret == PW_ERR_RATE_LIMIT_REACHED) { |
| 1173 | pinweaver_eal_memcpy_s(&response->seconds_to_wait, |
| 1174 | sizeof(response->seconds_to_wait), |
| 1175 | &seconds_to_wait, |
| 1176 | sizeof(seconds_to_wait)); |
| 1177 | } |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1178 | return ret; |
| 1179 | } |
| 1180 | |
Howard Yang | 2b189b2 | 2022-07-29 11:11:34 +0800 | [diff] [blame] | 1181 | update_timestamp(&leaf_data.pub.last_access_ts); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1182 | |
| 1183 | /* Precompute the failed attempts. */ |
| 1184 | results_table[0].attempts = leaf_data.pub.attempt_count.v; |
| 1185 | if (results_table[0].attempts != UINT32_MAX) |
| 1186 | ++results_table[0].attempts; |
| 1187 | |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1188 | /* TODO: Make biometrics type leaf use different results table. */ |
| 1189 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1190 | /**********************************************************************/ |
| 1191 | /* After this: |
| 1192 | * 1) results_table should not be changed; |
| 1193 | * 2) the runtime of the code paths for failed and successful |
| 1194 | * authentication attempts should not diverge. |
| 1195 | */ |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1196 | auth_result = pinweaver_eal_safe_memcmp( |
| 1197 | request->low_entropy_secret, |
| 1198 | leaf_data.sec.low_entropy_secret, |
| 1199 | sizeof(request->low_entropy_secret)) == 0; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1200 | leaf_data.pub.attempt_count.v = results_table[auth_result].attempts; |
| 1201 | |
| 1202 | /* This has a non-constant time path, but it doesn't convey information |
| 1203 | * about whether a PW_ERR_LOWENT_AUTH_FAILED happened or not. |
| 1204 | */ |
| 1205 | ret = handle_leaf_update(merkle_tree, &leaf_data, |
| 1206 | imported_leaf_data.hashes, &wrapped_leaf_data, |
| 1207 | new_root, &imported_leaf_data); |
| 1208 | if (ret != EC_SUCCESS) |
| 1209 | return ret; |
| 1210 | |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1211 | |
| 1212 | #if BIOMETRICS_DEV |
| 1213 | expiration_ts = leaf_data.pub.expiration_ts; |
| 1214 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1215 | ret = log_auth(wrapped_leaf_data.pub.label, new_root, |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1216 | results_table[auth_result].ret, leaf_data.pub.last_access_ts, |
| 1217 | expiration_ts); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1218 | if (ret != EC_SUCCESS) { |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1219 | pinweaver_eal_memcpy_s(new_root, sizeof(new_root), |
| 1220 | merkle_tree->root, |
| 1221 | sizeof(merkle_tree->root)); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1222 | return ret; |
| 1223 | } |
| 1224 | /**********************************************************************/ |
| 1225 | /* At this point the log should be written so it should be safe for the |
| 1226 | * runtime of the code paths to diverge. |
| 1227 | */ |
| 1228 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1229 | ret = pinweaver_eal_memcpy_s(merkle_tree->root, |
| 1230 | sizeof(merkle_tree->root), new_root, |
| 1231 | sizeof(new_root)); |
| 1232 | if (ret != EC_SUCCESS) |
| 1233 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1234 | |
| 1235 | *data_length = sizeof(*response) + PW_LEAF_PAYLOAD_SIZE; |
| 1236 | memset(response, 0, *data_length); |
| 1237 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1238 | ret = pinweaver_eal_memcpy_s(&response->unimported_leaf_data, |
| 1239 | sizeof(wrapped_leaf_data), |
| 1240 | &wrapped_leaf_data, |
| 1241 | sizeof(wrapped_leaf_data)); |
| 1242 | if (ret != EC_SUCCESS) |
| 1243 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1244 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1245 | ret = pinweaver_eal_memcpy_s(&response->high_entropy_secret, |
| 1246 | sizeof(response->high_entropy_secret), |
| 1247 | results_table[auth_result].secret, |
| 1248 | sizeof(response->high_entropy_secret)); |
| 1249 | if (ret != EC_SUCCESS) |
| 1250 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1251 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1252 | ret = pinweaver_eal_memcpy_s(&response->reset_secret, |
| 1253 | sizeof(response->reset_secret), |
| 1254 | results_table[auth_result].reset_secret, |
| 1255 | sizeof(response->reset_secret)); |
| 1256 | if (ret != EC_SUCCESS) |
| 1257 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1258 | |
| 1259 | return results_table[auth_result].ret; |
| 1260 | } |
| 1261 | |
| 1262 | static int pw_handle_reset_auth(struct merkle_tree_t *merkle_tree, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 1263 | const pw_request_reset_auth_t *request, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1264 | uint16_t req_size, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 1265 | pw_response_reset_auth_t *response, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1266 | uint16_t *response_size) |
| 1267 | { |
| 1268 | int ret = EC_SUCCESS; |
| 1269 | struct leaf_data_t leaf_data = {}; |
| 1270 | struct imported_leaf_data_t imported_leaf_data; |
Andrey Pronin | 8ec6dc5 | 2021-08-12 14:00:09 -0700 | [diff] [blame] | 1271 | struct wrapped_leaf_data_t wrapped_leaf_data = {}; |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1272 | struct pw_timestamp_t expiration_ts = {}; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1273 | uint8_t new_root[PW_HASH_SIZE]; |
Howard Yang | c96c5e8 | 2022-07-29 17:11:22 +0800 | [diff] [blame] | 1274 | #if BIOMETRICS_DEV |
| 1275 | uint32_t delay_s; |
| 1276 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1277 | |
| 1278 | if (req_size < sizeof(*request)) |
| 1279 | return PW_ERR_LENGTH_INVALID; |
| 1280 | |
| 1281 | ret = validate_request_with_wrapped_leaf( |
| 1282 | merkle_tree, req_size - sizeof(*request), |
| 1283 | &request->unimported_leaf_data, &imported_leaf_data, |
| 1284 | &leaf_data); |
| 1285 | if (ret != EC_SUCCESS) |
| 1286 | return ret; |
| 1287 | |
| 1288 | /* Safe memcmp is used here to prevent an attacker from being able to |
| 1289 | * brute force the reset secret and use it to unlock the leaf. |
| 1290 | * memcmp provides an attacker a timing side-channel they can use to |
| 1291 | * determine how much of a prefix is correct. |
| 1292 | */ |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1293 | if (pinweaver_eal_safe_memcmp(request->reset_secret, |
| 1294 | leaf_data.sec.reset_secret, |
| 1295 | sizeof(request->reset_secret)) != 0) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1296 | return PW_ERR_RESET_AUTH_FAILED; |
| 1297 | |
| 1298 | leaf_data.pub.attempt_count.v = 0; |
| 1299 | |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1300 | #if BIOMETRICS_DEV |
Howard Yang | c96c5e8 | 2022-07-29 17:11:22 +0800 | [diff] [blame] | 1301 | if (request->strong_reset && leaf_data.pub.expiration_delay_s.v != 0) { |
| 1302 | update_timestamp(&leaf_data.pub.expiration_ts); |
| 1303 | delay_s = leaf_data.pub.expiration_delay_s.v; |
| 1304 | if (leaf_data.pub.expiration_ts.timer_value <= UINT64_MAX - delay_s) { |
| 1305 | leaf_data.pub.expiration_ts.timer_value += delay_s; |
| 1306 | } else { |
| 1307 | leaf_data.pub.expiration_ts.timer_value = UINT64_MAX; |
| 1308 | } |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1309 | } |
| 1310 | #endif |
| 1311 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1312 | ret = handle_leaf_update(merkle_tree, &leaf_data, |
| 1313 | imported_leaf_data.hashes, &wrapped_leaf_data, |
| 1314 | new_root, &imported_leaf_data); |
| 1315 | if (ret != EC_SUCCESS) |
| 1316 | return ret; |
| 1317 | |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1318 | #if BIOMETRICS_DEV |
| 1319 | expiration_ts = leaf_data.pub.expiration_ts; |
| 1320 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1321 | ret = log_auth(leaf_data.pub.label, new_root, ret, |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1322 | leaf_data.pub.last_access_ts, expiration_ts); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1323 | if (ret != EC_SUCCESS) |
| 1324 | return ret; |
| 1325 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1326 | ret = pinweaver_eal_memcpy_s(merkle_tree->root, |
| 1327 | sizeof(merkle_tree->root), new_root, |
| 1328 | sizeof(new_root)); |
| 1329 | if (ret != EC_SUCCESS) |
| 1330 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1331 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1332 | ret = pinweaver_eal_memcpy_s(&response->unimported_leaf_data, |
| 1333 | sizeof(wrapped_leaf_data), |
| 1334 | &wrapped_leaf_data, |
| 1335 | sizeof(wrapped_leaf_data)); |
| 1336 | if (ret != EC_SUCCESS) |
| 1337 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1338 | |
Howard Yang | 9f21e3d | 2022-08-01 18:24:58 +0800 | [diff] [blame] | 1339 | #if !BIOMETRICS_DEV |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1340 | ret = pinweaver_eal_memcpy_s(response->high_entropy_secret, |
| 1341 | sizeof(response->high_entropy_secret), |
| 1342 | leaf_data.sec.high_entropy_secret, |
| 1343 | sizeof(response->high_entropy_secret)); |
Howard Yang | 9f21e3d | 2022-08-01 18:24:58 +0800 | [diff] [blame] | 1344 | #endif |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1345 | if (ret != EC_SUCCESS) |
| 1346 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1347 | |
| 1348 | *response_size = sizeof(*response) + PW_LEAF_PAYLOAD_SIZE; |
| 1349 | |
| 1350 | return ret; |
| 1351 | } |
| 1352 | |
| 1353 | static int pw_handle_get_log(const struct merkle_tree_t *merkle_tree, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 1354 | const pw_request_get_log_t *request, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1355 | uint16_t req_size, |
| 1356 | struct pw_get_log_entry_t response[], |
| 1357 | uint16_t *response_size) |
| 1358 | { |
| 1359 | int ret; |
| 1360 | int x; |
| 1361 | struct pw_log_storage_t log; |
| 1362 | |
| 1363 | if (req_size != sizeof(*request)) |
| 1364 | return PW_ERR_LENGTH_INVALID; |
| 1365 | |
| 1366 | ret = validate_tree(merkle_tree); |
| 1367 | if (ret != EC_SUCCESS) |
| 1368 | return ret; |
| 1369 | |
| 1370 | ret = load_log_data(&log); |
| 1371 | if (ret != EC_SUCCESS) |
| 1372 | return ret; |
| 1373 | |
| 1374 | /* Find the relevant log entry. The return value isn't used because if |
| 1375 | * the entry isn't found the entire log is returned. This makes it |
| 1376 | * easier to recover when the log is too short. |
| 1377 | * |
| 1378 | * Here is an example: |
| 1379 | * 50 attempts have been made against a leaf that becomes out of sync |
| 1380 | * because of a disk flush failing. The copy of the leaf on disk is |
| 1381 | * behind by 50 and the log contains less than 50 entries. The CrOS |
| 1382 | * implementation can check the public parameters of the local copy with |
| 1383 | * the log entry to determine that leaf is out of sync. It can then send |
| 1384 | * any valid copy of that leaf with a log replay request that will only |
| 1385 | * succeed if the HMAC of the resulting leaf matches the log entry. |
| 1386 | */ |
| 1387 | find_relevant_entry(&log, request->root, &x); |
| 1388 | /* If there are no valid entries, return. */ |
| 1389 | if (x < 0) |
| 1390 | return EC_SUCCESS; |
| 1391 | |
| 1392 | /* Copy the entries in reverse order. */ |
| 1393 | while (1) { |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1394 | ret = pinweaver_eal_memcpy_s(&response[x], sizeof(response[x]), |
| 1395 | &log.entries[x], |
| 1396 | sizeof(log.entries[x])); |
| 1397 | if (ret != EC_SUCCESS) |
| 1398 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1399 | *response_size += sizeof(log.entries[x]); |
| 1400 | if (x == 0) |
| 1401 | break; |
| 1402 | --x; |
| 1403 | } |
| 1404 | |
| 1405 | return EC_SUCCESS; |
| 1406 | } |
| 1407 | |
| 1408 | static int pw_handle_log_replay(const struct merkle_tree_t *merkle_tree, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 1409 | const pw_request_log_replay_t *request, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1410 | uint16_t req_size, |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 1411 | pw_response_log_replay_t *response, |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1412 | uint16_t *response_size) |
| 1413 | { |
| 1414 | int ret; |
| 1415 | int x; |
| 1416 | struct pw_log_storage_t log; |
| 1417 | struct leaf_data_t leaf_data = {}; |
| 1418 | struct imported_leaf_data_t imported_leaf_data; |
Andrey Pronin | 8ec6dc5 | 2021-08-12 14:00:09 -0700 | [diff] [blame] | 1419 | struct wrapped_leaf_data_t wrapped_leaf_data = {}; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1420 | uint8_t hmac[PW_HASH_SIZE]; |
| 1421 | uint8_t root[PW_HASH_SIZE]; |
| 1422 | |
| 1423 | if (req_size < sizeof(*request)) |
| 1424 | return PW_ERR_LENGTH_INVALID; |
| 1425 | |
| 1426 | ret = validate_tree(merkle_tree); |
| 1427 | if (ret != EC_SUCCESS) |
| 1428 | return ret; |
| 1429 | |
| 1430 | /* validate_request_with_wrapped_leaf() isn't used here because the |
| 1431 | * path validation is delayed to allow any valid copy of the same leaf |
| 1432 | * to be used in the replay operation as long as the result passes path |
| 1433 | * validation. |
| 1434 | */ |
| 1435 | ret = validate_leaf_header(&request->unimported_leaf_data.head, |
| 1436 | req_size - sizeof(*request), |
| 1437 | get_path_auxiliary_hash_count(merkle_tree)); |
| 1438 | if (ret != EC_SUCCESS) |
| 1439 | return ret; |
| 1440 | |
| 1441 | import_leaf(&request->unimported_leaf_data, &imported_leaf_data); |
| 1442 | |
| 1443 | ret = load_log_data(&log); |
| 1444 | if (ret != EC_SUCCESS) |
| 1445 | return ret; |
| 1446 | |
| 1447 | /* Find the relevant log entry. */ |
| 1448 | ret = find_relevant_entry(&log, request->log_root, &x); |
| 1449 | if (ret != EC_SUCCESS) |
| 1450 | return ret; |
| 1451 | |
| 1452 | /* The other message types don't need to be handled by Cr50. */ |
Howard Yang | c2f080c | 2022-07-29 11:28:13 +0800 | [diff] [blame] | 1453 | if (log.entries[x].type.v != LOG_PW_TRY_AUTH) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1454 | return PW_ERR_TYPE_INVALID; |
| 1455 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1456 | ret = compute_hmac(merkle_tree, &imported_leaf_data, hmac); |
| 1457 | if (ret != EC_SUCCESS) |
| 1458 | return ret; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1459 | if (pinweaver_eal_safe_memcmp(hmac, |
| 1460 | request->unimported_leaf_data.hmac, |
| 1461 | sizeof(hmac))) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1462 | return PW_ERR_HMAC_AUTH_FAILED; |
| 1463 | |
| 1464 | ret = decrypt_leaf_data(merkle_tree, &imported_leaf_data, &leaf_data); |
| 1465 | if (ret != EC_SUCCESS) |
| 1466 | return ret; |
| 1467 | |
| 1468 | if (leaf_data.pub.label.v != log.entries[x].label.v) |
| 1469 | return PW_ERR_LABEL_INVALID; |
| 1470 | |
| 1471 | /* Update the metadata to match the log. */ |
| 1472 | if (log.entries[x].return_code == EC_SUCCESS) |
| 1473 | leaf_data.pub.attempt_count.v = 0; |
| 1474 | else |
| 1475 | ++leaf_data.pub.attempt_count.v; |
Howard Yang | 2b189b2 | 2022-07-29 11:11:34 +0800 | [diff] [blame] | 1476 | ret = pinweaver_eal_memcpy_s(&leaf_data.pub.last_access_ts, |
| 1477 | sizeof(leaf_data.pub.last_access_ts), |
| 1478 | &log.entries[x].last_access_ts, |
| 1479 | sizeof(leaf_data.pub.last_access_ts)); |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1480 | if (ret != EC_SUCCESS) |
| 1481 | return PW_ERR_INTERNAL_FAILURE; |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1482 | #if BIOMETRICS_DEV |
| 1483 | ret = pinweaver_eal_memcpy_s(&leaf_data.pub.expiration_ts, |
| 1484 | sizeof(leaf_data.pub.expiration_ts), |
| 1485 | &log.entries[x].expiration_ts, |
| 1486 | sizeof(leaf_data.pub.expiration_ts)); |
| 1487 | if (ret != EC_SUCCESS) |
| 1488 | return PW_ERR_INTERNAL_FAILURE; |
| 1489 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1490 | |
| 1491 | ret = handle_leaf_update(merkle_tree, &leaf_data, |
| 1492 | imported_leaf_data.hashes, &wrapped_leaf_data, |
| 1493 | root, &imported_leaf_data); |
| 1494 | if (ret != EC_SUCCESS) |
| 1495 | return ret; |
| 1496 | |
Yi Chou | ddf3b45 | 2021-08-11 13:40:11 +0800 | [diff] [blame] | 1497 | if (pinweaver_eal_safe_memcmp(root, log.entries[x].root, PW_HASH_SIZE)) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1498 | return PW_ERR_PATH_AUTH_FAILED; |
| 1499 | |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1500 | ret = pinweaver_eal_memcpy_s(&response->unimported_leaf_data, |
| 1501 | sizeof(wrapped_leaf_data), |
| 1502 | &wrapped_leaf_data, |
| 1503 | sizeof(wrapped_leaf_data)); |
| 1504 | if (ret != EC_SUCCESS) |
| 1505 | return PW_ERR_INTERNAL_FAILURE; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1506 | |
| 1507 | *response_size = sizeof(*response) + PW_LEAF_PAYLOAD_SIZE; |
| 1508 | |
| 1509 | return EC_SUCCESS; |
| 1510 | } |
| 1511 | |
Howard Yang | f3a5e9f | 2022-08-01 21:52:27 +0800 | [diff] [blame] | 1512 | #if BIOMETRICS_DEV |
| 1513 | static int pw_handle_sys_info(pw_response_sys_info_t *response, |
| 1514 | uint16_t *response_size) |
| 1515 | { |
| 1516 | update_timestamp(&response->current_ts); |
| 1517 | *response_size = sizeof(*response); |
| 1518 | return EC_SUCCESS; |
| 1519 | } |
Howard Yang | fad8b97 | 2022-08-05 16:58:33 +0800 | [diff] [blame^] | 1520 | |
| 1521 | static int pw_handle_generate_pk(struct merkle_tree_t *merkle_tree, |
| 1522 | const pw_request_generate_ba_pk_t *request, |
| 1523 | uint16_t req_size, |
| 1524 | pw_response_generate_ba_pk_t *response, |
| 1525 | uint16_t *response_size) |
| 1526 | { |
| 1527 | int ret; |
| 1528 | uint8_t auth_channel; |
| 1529 | struct pw_ba_pk_status_t pk_status; |
| 1530 | struct pw_ba_pk_t pk; |
| 1531 | uint8_t secret[PW_SECRET_SIZE]; |
| 1532 | size_t secret_size = PW_SECRET_SIZE; |
| 1533 | pinweaver_eal_sha256_ctx_t sha; |
| 1534 | |
| 1535 | if (req_size != sizeof(*request)) |
| 1536 | return PW_ERR_LENGTH_INVALID; |
| 1537 | auth_channel = request->auth_channel; |
| 1538 | |
| 1539 | /* Currently we only support v0 public key format. */ |
| 1540 | if (request->client_pbk.version != 0) { |
| 1541 | return PW_ERR_BIO_AUTH_PUBLIC_KEY_VERSION_MISMATCH; |
| 1542 | } |
| 1543 | response->server_pbk.version = 0; |
| 1544 | |
| 1545 | if (auth_channel >= PW_BA_PK_ENTRY_COUNT) { |
| 1546 | return PW_ERR_BIO_AUTH_CHANNEL_INVALID; |
| 1547 | } |
| 1548 | |
| 1549 | /* Pk can only be generated on the specified auth_channel slot |
| 1550 | * if no Pk is established yet and it's not blocked. |
| 1551 | */ |
| 1552 | ret = pinweaver_eal_get_ba_pk(auth_channel, &pk_status, &pk); |
| 1553 | if (ret != EC_SUCCESS) |
| 1554 | return ret; |
| 1555 | if (pk_status.v != PW_BA_PK_NOT_ESTABLISHED) |
| 1556 | return PW_ERR_BIO_AUTH_ACCESS_DENIED; |
| 1557 | |
| 1558 | /* Perform ECDH to derive the shared secret. */ |
| 1559 | ret = pinweaver_eal_ecdh_derive(&request->client_pbk.pt, |
| 1560 | secret, &secret_size, &response->server_pbk.pt); |
| 1561 | if (ret != EC_SUCCESS) |
| 1562 | return ret; |
| 1563 | if (secret_size != PW_SECRET_SIZE) |
| 1564 | return PW_ERR_INTERNAL_FAILURE; |
| 1565 | |
| 1566 | /* sha256 the shared secret into Pk. */ |
| 1567 | if (pinweaver_eal_sha256_init(&sha)) { |
| 1568 | return PW_ERR_CRYPTO_FAILURE; |
| 1569 | } |
| 1570 | if (pinweaver_eal_sha256_update(&sha, secret, secret_size)) { |
| 1571 | pinweaver_eal_sha256_final(&sha, pk.key); |
| 1572 | return PW_ERR_CRYPTO_FAILURE; |
| 1573 | } |
| 1574 | if (pinweaver_eal_sha256_final(&sha, pk.key)) |
| 1575 | return PW_ERR_CRYPTO_FAILURE; |
| 1576 | |
| 1577 | /* Persist the Pk. */ |
| 1578 | ret = pinweaver_eal_set_ba_pk(auth_channel, &pk); |
| 1579 | if (ret != EC_SUCCESS) |
| 1580 | return ret; |
| 1581 | |
| 1582 | *response_size = sizeof(*response); |
| 1583 | return EC_SUCCESS; |
| 1584 | } |
Howard Yang | f3a5e9f | 2022-08-01 21:52:27 +0800 | [diff] [blame] | 1585 | #endif |
| 1586 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1587 | struct merkle_tree_t pw_merkle_tree; |
| 1588 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1589 | /******************************************************************************/ |
| 1590 | /* Non-static functions. |
| 1591 | */ |
| 1592 | |
| 1593 | void pinweaver_init(void) |
| 1594 | { |
| 1595 | load_merkle_tree(&pw_merkle_tree); |
| 1596 | } |
| 1597 | |
| 1598 | int get_path_auxiliary_hash_count(const struct merkle_tree_t *merkle_tree) |
| 1599 | { |
| 1600 | return ((1 << merkle_tree->bits_per_level.v) - 1) * |
| 1601 | merkle_tree->height.v; |
| 1602 | } |
| 1603 | |
| 1604 | /* Computes the SHA256 parent hash of a set of child hashes given num_hashes |
| 1605 | * sibling hashes in hashes[] and the index of child_hash. |
| 1606 | * |
| 1607 | * Assumptions: |
| 1608 | * num_hashes == fan_out - 1 |
| 1609 | * ARRAY_SIZE(hashes) == num_hashes |
| 1610 | * 0 <= location <= num_hashes |
| 1611 | */ |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1612 | int compute_hash(const uint8_t hashes[][PW_HASH_SIZE], uint16_t num_hashes, |
| 1613 | struct index_t location, |
| 1614 | const uint8_t child_hash[PW_HASH_SIZE], |
| 1615 | uint8_t result[PW_HASH_SIZE]) |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1616 | { |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1617 | int ret; |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1618 | pinweaver_eal_sha256_ctx_t ctx; |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1619 | |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1620 | ret = pinweaver_eal_sha256_init(&ctx); |
| 1621 | if (ret) |
| 1622 | return ret; |
Yi Chou | 89f8d53 | 2021-08-12 16:36:46 +0800 | [diff] [blame] | 1623 | if (location.v > 0) { |
| 1624 | ret = pinweaver_eal_sha256_update(&ctx, hashes[0], |
| 1625 | PW_HASH_SIZE * location.v); |
| 1626 | if (ret) { |
| 1627 | pinweaver_eal_sha256_final(&ctx, result); |
| 1628 | return ret; |
| 1629 | } |
Leo Lai | 788f41e | 2021-08-11 00:36:24 +0800 | [diff] [blame] | 1630 | } |
| 1631 | ret = pinweaver_eal_sha256_update(&ctx, child_hash, PW_HASH_SIZE); |
| 1632 | if (ret) { |
| 1633 | pinweaver_eal_sha256_final(&ctx, result); |
| 1634 | return ret; |
| 1635 | } |
| 1636 | if (location.v < num_hashes) { |
| 1637 | ret = pinweaver_eal_sha256_update( |
| 1638 | &ctx, hashes[location.v], |
| 1639 | PW_HASH_SIZE * (num_hashes - location.v)); |
| 1640 | if (ret) { |
| 1641 | pinweaver_eal_sha256_final(&ctx, result); |
| 1642 | return ret; |
| 1643 | } |
| 1644 | } |
| 1645 | return pinweaver_eal_sha256_final(&ctx, result); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /* If a request from older protocol comes, this method should make it |
| 1649 | * compatible with the current request structure. |
| 1650 | */ |
| 1651 | int make_compatible_request(struct merkle_tree_t *merkle_tree, |
| 1652 | struct pw_request_t *request) |
| 1653 | { |
| 1654 | switch (request->header.version) { |
| 1655 | case 0: |
| 1656 | /* The switch from protocol version 0 to 1 means all the |
| 1657 | * requests have the same format, except insert_leaf. |
| 1658 | * Update the request in that case. |
| 1659 | */ |
| 1660 | if (request->header.type.v == PW_INSERT_LEAF) { |
| 1661 | unsigned char *src = (unsigned char *) |
| 1662 | (&request->data.insert_leaf00.path_hashes); |
| 1663 | unsigned char *dest = (unsigned char *) |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1664 | (&request->data.insert_leaf01.path_hashes); |
Yi Chou | c3901ca | 2021-08-11 14:11:15 +0800 | [diff] [blame] | 1665 | const uint16_t src_size = |
| 1666 | request->header.data_length - |
| 1667 | offsetof(struct pw_request_insert_leaf00_t, |
| 1668 | path_hashes); |
Howard Yang | d4d2e7a | 2022-08-02 17:57:53 +0800 | [diff] [blame] | 1669 | if (offsetof(struct pw_request_insert_leaf01_t, |
Andrey Pronin | ee44ef0 | 2021-08-13 15:11:38 -0700 | [diff] [blame] | 1670 | path_hashes) + src_size > |
| 1671 | PW_MAX_MESSAGE_SIZE) { |
Yi Chou | c3901ca | 2021-08-11 14:11:15 +0800 | [diff] [blame] | 1672 | /* The total length of request overflowed. */ |
| 1673 | return 0; |
| 1674 | } |
| 1675 | memmove(dest, src, src_size); |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1676 | memset(&request->data.insert_leaf01.valid_pcr_criteria, 0, |
Yi Chou | c3901ca | 2021-08-11 14:11:15 +0800 | [diff] [blame] | 1677 | PW_VALID_PCR_CRITERIA_SIZE); |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1678 | request->header.data_length += |
| 1679 | PW_VALID_PCR_CRITERIA_SIZE; |
| 1680 | } |
| 1681 | /* Fallthrough to make compatible from next version */ |
Leo Lai | 8c9892c | 2021-06-22 21:32:02 +0800 | [diff] [blame] | 1682 | __attribute__((fallthrough)); |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1683 | #if BIOMETRICS_DEV |
| 1684 | case 1: |
| 1685 | /* The switch from protocol version 1 to 2 means all the |
| 1686 | * requests have the same format, except insert_leaf and |
| 1687 | * reset_auth. |
| 1688 | * Update the request in that case. |
| 1689 | */ |
| 1690 | if (request->header.type.v == PW_INSERT_LEAF) { |
| 1691 | unsigned char *src = (unsigned char *) |
| 1692 | (&request->data.insert_leaf01.path_hashes); |
| 1693 | unsigned char *dest = (unsigned char *) |
| 1694 | (&request->data.insert_leaf.path_hashes); |
| 1695 | const size_t src_offset = offsetof(struct pw_request_insert_leaf01_t, |
| 1696 | path_hashes); |
| 1697 | const size_t dest_offset = offsetof(struct pw_request_insert_leaf02_t, |
| 1698 | path_hashes); |
| 1699 | const uint16_t src_size = request->header.data_length - src_offset; |
| 1700 | if (dest_offset + src_size > PW_MAX_MESSAGE_SIZE) { |
| 1701 | /* The total length of request overflowed. */ |
| 1702 | return 0; |
| 1703 | } |
| 1704 | memmove(dest, src, src_size); |
| 1705 | request->data.insert_leaf.expiration_delay_s.v = 0; |
| 1706 | request->data.insert_leaf.leaf_type.v = PW_LEAF_TYPE_NORMAL; |
| 1707 | request->header.data_length += dest_offset - src_offset; |
| 1708 | } else if (request->header.type.v == PW_RESET_AUTH) { |
| 1709 | unsigned char *src = (unsigned char *) |
| 1710 | (&request->data.reset_auth00.unimported_leaf_data); |
| 1711 | unsigned char *dest = (unsigned char *) |
| 1712 | (&request->data.reset_auth.unimported_leaf_data); |
| 1713 | const uint16_t src_size = |
| 1714 | request->header.data_length - |
| 1715 | offsetof(struct pw_request_reset_auth00_t, |
| 1716 | unimported_leaf_data); |
| 1717 | if (offsetof(struct pw_request_reset_auth02_t, |
| 1718 | unimported_leaf_data) + src_size > |
| 1719 | PW_MAX_MESSAGE_SIZE) { |
| 1720 | /* The total length of request overflowed. */ |
| 1721 | return 0; |
| 1722 | } |
| 1723 | memmove(dest, src, src_size); |
| 1724 | request->data.reset_auth.strong_reset = 0; |
| 1725 | request->header.data_length += 1; |
| 1726 | } |
| 1727 | /* Fallthrough to make compatible from next version */ |
| 1728 | __attribute__((fallthrough)); |
| 1729 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1730 | case PW_PROTOCOL_VERSION: |
| 1731 | return 1; |
| 1732 | } |
| 1733 | /* Unsupported version. */ |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | /* Converts the response to be understandable by an older protocol. |
| 1738 | */ |
| 1739 | void make_compatible_response(int version, int req_type, |
| 1740 | struct pw_response_t *response) |
| 1741 | { |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1742 | #if BIOMETRICS_DEV |
| 1743 | size_t offset; |
| 1744 | #endif |
| 1745 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1746 | if (version >= PW_PROTOCOL_VERSION) |
| 1747 | return; |
| 1748 | |
| 1749 | response->header.version = version; |
| 1750 | if (version == 0) { |
| 1751 | if (req_type == PW_TRY_AUTH) { |
| 1752 | unsigned char *src = (unsigned char *) |
| 1753 | (&response->data.try_auth.unimported_leaf_data); |
| 1754 | unsigned char *dest = (unsigned char *) |
| 1755 | (&response->data.try_auth00.unimported_leaf_data); |
| 1756 | memmove(dest, src, |
| 1757 | PW_LEAF_PAYLOAD_SIZE + |
| 1758 | sizeof(struct unimported_leaf_data_t)); |
| 1759 | response->header.data_length -= PW_SECRET_SIZE; |
| 1760 | } |
| 1761 | } |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1762 | |
| 1763 | #if BIOMETRICS_DEV |
| 1764 | if (version <= 1) { |
| 1765 | if (req_type == PW_GET_LOG) { |
| 1766 | for (offset = 0; |
| 1767 | offset + sizeof(struct pw_get_log_entry_t) <= |
| 1768 | response->header.data_length; |
| 1769 | offset += sizeof(struct pw_get_log_entry_t)) { |
| 1770 | struct pw_get_log_entry_t * entry = |
| 1771 | (struct pw_get_log_entry_t *) (response->data.get_log + offset); |
| 1772 | /* LOG_PW_TRY_AUTH00's union variant is the prefix of |
| 1773 | * LOG_PW_TRY_AUTH's, so we can just change the type here. |
| 1774 | */ |
| 1775 | if (entry->type.v == LOG_PW_TRY_AUTH) |
| 1776 | entry->type.v = LOG_PW_TRY_AUTH00; |
| 1777 | } |
Howard Yang | 9f21e3d | 2022-08-01 18:24:58 +0800 | [diff] [blame] | 1778 | } else if (req_type == PW_RESET_AUTH) { |
| 1779 | unsigned char *src = (unsigned char *) |
| 1780 | (&response->data.reset_auth02.unimported_leaf_data); |
| 1781 | unsigned char *dest = (unsigned char *) |
| 1782 | (&response->data.reset_auth00.unimported_leaf_data); |
| 1783 | memmove(dest, src, |
| 1784 | PW_LEAF_PAYLOAD_SIZE + |
| 1785 | sizeof(struct unimported_leaf_data_t)); |
| 1786 | /* clients from protocol version <= 1 won't be able to retrieve |
| 1787 | * the HEC from reset_auth anymore, they can parse the response |
| 1788 | * with same structure but need to deal with the logical change |
| 1789 | * themselves. |
| 1790 | */ |
| 1791 | memset(response->data.reset_auth00.high_entropy_secret, 0, |
| 1792 | PW_SECRET_SIZE); |
| 1793 | response->header.data_length += PW_SECRET_SIZE; |
Howard Yang | aa7a897 | 2022-07-26 16:39:36 +0800 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
Andrey Pronin | cd7bcce | 2021-04-14 00:54:12 -0700 | [diff] [blame] | 1799 | enum pinweaver_command_res_t pinweaver_command(void *request_buf, |
| 1800 | size_t request_size, |
| 1801 | void *response_buf, |
| 1802 | size_t *response_size) { |
| 1803 | struct pw_request_t *request = request_buf; |
| 1804 | struct pw_response_t *response = response_buf; |
| 1805 | |
| 1806 | if (request_size < sizeof(request->header)) { |
| 1807 | PINWEAVER_EAL_INFO( |
| 1808 | "PinWeaver: message smaller than a header (%zd).\n", |
| 1809 | request_size); |
| 1810 | return PW_CMD_RES_TOO_SMALL; |
| 1811 | } |
| 1812 | |
| 1813 | if (request_size != request->header.data_length + |
| 1814 | sizeof(request->header)) { |
| 1815 | PINWEAVER_EAL_INFO( |
| 1816 | "PinWeaver: header size mismatch %zd != %zd.\n", |
| 1817 | request_size, |
| 1818 | request->header.data_length + sizeof(request->header)); |
| 1819 | return PW_CMD_RES_SIZE; |
| 1820 | } |
| 1821 | |
| 1822 | /* The response_size is validated by compile time checks. */ |
| 1823 | |
| 1824 | /* The return value of this function call is intentionally unused. */ |
| 1825 | pw_handle_request(&pw_merkle_tree, request, response); |
| 1826 | |
| 1827 | *response_size = response->header.data_length + |
| 1828 | sizeof(response->header); |
| 1829 | |
| 1830 | /* The response is only sent for EC_SUCCESS so it is used even for |
| 1831 | * errors which are reported through header.return_code. |
| 1832 | */ |
| 1833 | return PW_CMD_RES_SUCCESS; |
| 1834 | } |
| 1835 | |
| 1836 | |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1837 | /* Handles the message in request using the context in merkle_tree and writes |
| 1838 | * the results to response. The return value captures any error conditions that |
| 1839 | * occurred or EC_SUCCESS if there were no errors. |
| 1840 | * |
| 1841 | * This implementation is written to handle the case where request and response |
| 1842 | * exist at the same memory location---are backed by the same buffer. This means |
| 1843 | * the implementation requires that no reads are made to request after response |
| 1844 | * has been written to. |
| 1845 | */ |
| 1846 | int pw_handle_request(struct merkle_tree_t *merkle_tree, |
| 1847 | struct pw_request_t *request, |
| 1848 | struct pw_response_t *response) |
| 1849 | { |
| 1850 | int32_t ret; |
| 1851 | uint16_t resp_length; |
| 1852 | /* Store the message type of the request since it may be overwritten |
| 1853 | * inside the switch whenever response and request overlap in memory. |
| 1854 | */ |
| 1855 | struct pw_message_type_t type = request->header.type; |
| 1856 | int version = request->header.version; |
| 1857 | |
| 1858 | resp_length = 0; |
| 1859 | |
| 1860 | if (!make_compatible_request(merkle_tree, request)) { |
| 1861 | ret = PW_ERR_VERSION_MISMATCH; |
| 1862 | goto cleanup; |
| 1863 | } |
| 1864 | switch (type.v) { |
| 1865 | case PW_RESET_TREE: |
| 1866 | ret = pw_handle_reset_tree(merkle_tree, |
| 1867 | &request->data.reset_tree, |
| 1868 | request->header.data_length); |
| 1869 | break; |
| 1870 | case PW_INSERT_LEAF: |
| 1871 | ret = pw_handle_insert_leaf(merkle_tree, |
| 1872 | &request->data.insert_leaf, |
| 1873 | request->header.data_length, |
| 1874 | &response->data.insert_leaf, |
| 1875 | &resp_length); |
| 1876 | break; |
| 1877 | case PW_REMOVE_LEAF: |
| 1878 | ret = pw_handle_remove_leaf(merkle_tree, |
| 1879 | &request->data.remove_leaf, |
| 1880 | request->header.data_length); |
| 1881 | break; |
| 1882 | case PW_TRY_AUTH: |
| 1883 | ret = pw_handle_try_auth(merkle_tree, &request->data.try_auth, |
| 1884 | request->header.data_length, |
| 1885 | &response->data.try_auth, |
| 1886 | &resp_length); |
| 1887 | break; |
| 1888 | case PW_RESET_AUTH: |
| 1889 | ret = pw_handle_reset_auth(merkle_tree, |
| 1890 | &request->data.reset_auth, |
| 1891 | request->header.data_length, |
| 1892 | &response->data.reset_auth, |
| 1893 | &resp_length); |
| 1894 | break; |
| 1895 | case PW_GET_LOG: |
| 1896 | ret = pw_handle_get_log(merkle_tree, &request->data.get_log, |
| 1897 | request->header.data_length, |
| 1898 | (void *)&response->data, &resp_length); |
| 1899 | break; |
| 1900 | case PW_LOG_REPLAY: |
| 1901 | ret = pw_handle_log_replay(merkle_tree, |
| 1902 | &request->data.log_replay, |
| 1903 | request->header.data_length, |
| 1904 | &response->data.log_replay, |
| 1905 | &resp_length); |
| 1906 | break; |
Howard Yang | f3a5e9f | 2022-08-01 21:52:27 +0800 | [diff] [blame] | 1907 | #if BIOMETRICS_DEV |
| 1908 | case PW_SYS_INFO: |
| 1909 | ret = pw_handle_sys_info(&response->data.sys_info, &resp_length); |
| 1910 | break; |
Howard Yang | fad8b97 | 2022-08-05 16:58:33 +0800 | [diff] [blame^] | 1911 | case PW_GENERATE_BA_PK: |
| 1912 | ret = pw_handle_generate_pk(merkle_tree, |
| 1913 | &request->data.generate_pk, |
| 1914 | request->header.data_length, |
| 1915 | &response->data.generate_pk, |
| 1916 | &resp_length); |
| 1917 | break; |
Howard Yang | f3a5e9f | 2022-08-01 21:52:27 +0800 | [diff] [blame] | 1918 | #endif |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1919 | default: |
| 1920 | ret = PW_ERR_TYPE_INVALID; |
| 1921 | break; |
| 1922 | } |
| 1923 | cleanup: |
| 1924 | response->header.version = PW_PROTOCOL_VERSION; |
| 1925 | response->header.data_length = resp_length; |
| 1926 | response->header.result_code = ret; |
Leo Lai | db20791 | 2021-08-11 16:41:18 +0800 | [diff] [blame] | 1927 | if (pinweaver_eal_memcpy_s(&response->header.root, |
| 1928 | sizeof(response->header.root), |
| 1929 | merkle_tree->root, |
| 1930 | sizeof(merkle_tree->root))) { |
| 1931 | ret = PW_ERR_INTERNAL_FAILURE; |
| 1932 | } |
Andrey Pronin | 9b10e51 | 2021-04-13 11:18:53 -0700 | [diff] [blame] | 1933 | |
| 1934 | make_compatible_response(version, type.v, response); |
| 1935 | |
| 1936 | return ret; |
| 1937 | }; |