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