Skip to content

Commit 984fe31

Browse files
committed
keystore: split keystore_unlock_bip39() into multiple functions
Simpler to migrate it to Rust, with the goal of making it async.
1 parent 3d65f25 commit 984fe31

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/keystore.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,16 +485,11 @@ keystore_error_t keystore_unlock(
485485
return result;
486486
}
487487

488-
bool keystore_unlock_bip39(
489-
const uint8_t* seed,
490-
size_t seed_length,
491-
const char* mnemonic_passphrase,
492-
uint8_t* root_fingerprint_out)
488+
bool keystore_unlock_bip39_check(const uint8_t* seed, size_t seed_length)
493489
{
494490
if (!_is_unlocked_device) {
495491
return false;
496492
}
497-
usb_processing_timeout_reset(LONG_TIMEOUT);
498493

499494
uint8_t seed_hashed[32] = {0};
500495
UTIL_CLEANUP_32(seed_hashed);
@@ -505,6 +500,30 @@ bool keystore_unlock_bip39(
505500
return false;
506501
}
507502

503+
usb_processing_timeout_reset(LONG_TIMEOUT);
504+
505+
return true;
506+
}
507+
508+
bool keystore_unlock_bip39_finalize(const uint8_t* bip39_seed)
509+
{
510+
if (!_retain_bip39_seed(bip39_seed)) {
511+
return false;
512+
}
513+
_is_unlocked_bip39 = true;
514+
return true;
515+
}
516+
517+
bool keystore_unlock_bip39(
518+
const uint8_t* seed,
519+
size_t seed_length,
520+
const char* mnemonic_passphrase,
521+
uint8_t* root_fingerprint_out)
522+
{
523+
if (!keystore_unlock_bip39_check(seed, seed_length)) {
524+
return false;
525+
}
526+
508527
uint8_t bip39_seed[64] = {0};
509528
UTIL_CLEANUP_64(bip39_seed);
510529
rust_derive_bip39_seed(
@@ -513,11 +532,7 @@ bool keystore_unlock_bip39(
513532
rust_util_bytes_mut(bip39_seed, sizeof(bip39_seed)),
514533
rust_util_bytes_mut(root_fingerprint_out, 4));
515534

516-
if (!_retain_bip39_seed(bip39_seed)) {
517-
return false;
518-
}
519-
_is_unlocked_bip39 = true;
520-
return true;
535+
return keystore_unlock_bip39_finalize(bip39_seed);
521536
}
522537

523538
void keystore_lock(void)

src/keystore.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ USE_RESULT keystore_error_t keystore_create_and_store_seed(
104104
USE_RESULT keystore_error_t
105105
keystore_unlock(const char* password, uint8_t* remaining_attempts_out, int* securechip_result_out);
106106

107+
/**
108+
* Checks if bip39 unlocking can be performed. It can be performed if `keystore_unlock()`
109+
* successfully and the input seed matches the keystore seed (i.e. must match the output
110+
* of `keystore_copy_seed()`).
111+
* @param[in] seed the input seed to BIP39.
112+
* @param[in] seed_length the size of the seed
113+
*/
114+
USE_RESULT bool keystore_unlock_bip39_check(const uint8_t* seed, size_t seed_length);
115+
107116
/** Unlocks the bip39 seed. The input seed must be the keystore seed (i.e. must match the output
108117
* of `keystore_copy_seed()`).
109118
* @param[in] seed the input seed to BIP39.
@@ -120,6 +129,12 @@ USE_RESULT bool keystore_unlock_bip39(
120129
const char* mnemonic_passphrase,
121130
uint8_t* root_fingerprint_out);
122131

132+
/**
133+
* Retains the given bip39 seed and marks the keystore as unlocked.
134+
* @param[in] bip39_seed 64 byte bip39 seed.
135+
*/
136+
USE_RESULT bool keystore_unlock_bip39_finalize(const uint8_t* bip39_seed);
137+
123138
/**
124139
* Locks the keystore (resets to state before `keystore_unlock()`).
125140
*/

0 commit comments

Comments
 (0)