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