@@ -422,6 +422,23 @@ keystore_error_t keystore_create_and_store_seed(
422
422
return keystore_encrypt_and_store_seed (seed , host_entropy_size , password );
423
423
}
424
424
425
+ // Checks if the retained seed matches the passed seed.
426
+ static bool _check_retained_seed (const uint8_t * seed , size_t seed_length )
427
+ {
428
+ if (!_is_unlocked_device ) {
429
+ return false;
430
+ }
431
+ uint8_t seed_hashed [32 ] = {0 };
432
+ UTIL_CLEANUP_32 (seed_hashed );
433
+ if (_hash_seed (seed , seed_length , seed_hashed ) != KEYSTORE_OK ) {
434
+ return false;
435
+ }
436
+ if (!MEMEQ (seed_hashed , _retained_seed_hash , sizeof (_retained_seed_hash ))) {
437
+ return false;
438
+ }
439
+ return true;
440
+ }
441
+
425
442
keystore_error_t keystore_unlock (
426
443
const char * password ,
427
444
uint8_t * remaining_attempts_out ,
@@ -455,12 +472,7 @@ keystore_error_t keystore_unlock(
455
472
if (result == KEYSTORE_OK ) {
456
473
if (_is_unlocked_device ) {
457
474
// Already unlocked. Fail if the seed changed under our feet (should never happen).
458
- uint8_t current_seed [KEYSTORE_MAX_SEED_LENGTH ] = {0 };
459
- size_t current_seed_len = 0 ;
460
- if (!keystore_copy_seed (current_seed , & current_seed_len )) {
461
- return KEYSTORE_ERR_DECRYPT ;
462
- }
463
- if (seed_len != current_seed_len || !MEMEQ (current_seed , seed , current_seed_len )) {
475
+ if (!_check_retained_seed (seed , seed_len )) {
464
476
Abort ("Seed has suddenly changed. This should never happen." );
465
477
}
466
478
} else {
@@ -491,15 +503,9 @@ bool keystore_unlock_bip39_check(const uint8_t* seed, size_t seed_length)
491
503
return false;
492
504
}
493
505
494
- uint8_t seed_hashed [32 ] = {0 };
495
- UTIL_CLEANUP_32 (seed_hashed );
496
- if (_hash_seed (seed , seed_length , seed_hashed ) != KEYSTORE_OK ) {
506
+ if (!_check_retained_seed (seed , seed_length )) {
497
507
return false;
498
508
}
499
- if (!MEMEQ (seed_hashed , _retained_seed_hash , sizeof (_retained_seed_hash ))) {
500
- return false;
501
- }
502
-
503
509
usb_processing_timeout_reset (LONG_TIMEOUT );
504
510
505
511
return true;
0 commit comments