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