What changed, and why it matters
This commit is a routine code refactoring: it moves the 'is the device locked?' check from C to Rust. The actual logic (checking whether two internal seed values are present) stays exactly the same. There is no security bug being fixed or introduced here.
No security action required. Treat as normal refactoring; standard code review and CI testing are sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change ports keystore_is_locked() from C to Rust. Previously, C’s keystore_is_locked() called two Rust FFI functions (rust_keystore_is_unlocked_device and rust_keystore_is_unlocked_bip39) and returned !unlocked. Now, Rust’s is_locked() directly reads RETAINED_SEED and RETAINED_BIP39_SEED and returns !unlocked, and a new FFI export rust_keystore_is_locked() is exposed for C callers. The C header declaration and an allowlist entry are removed; call sites in hww.c and u2f.c are updated to use the new Rust FFI name. The behavior is functionally equivalent.
Changed components
keystore state reporting (hww.c API info)U2F unlock helper (u2f.c)Rust keystore moduleInspect captured patch +8 / −24
diff --git a/src/hww.c b/src/hww.c
index 40b5edc..390027b 100644
--- a/src/hww.c
+++ b/src/hww.c
@@ -106,7 +106,7 @@ static size_t _api_info(uint8_t* buf)
current++;
// 1 byte locked status
- *current = keystore_is_locked() ? 0x00 : 0x01;
+ *current = rust_keystore_is_locked() ? 0x00 : 0x01;
current++;
// 1 byte initialized status
diff --git a/src/keystore.c b/src/keystore.c
index d36163c..21edc7e 100644
--- a/src/keystore.c
+++ b/src/keystore.c
@@ -220,12 +220,6 @@ keystore_error_t keystore_unlock(
return result;
}
-bool keystore_is_locked(void)
-{
- bool unlocked = rust_keystore_is_unlocked_device() && rust_keystore_is_unlocked_bip39();
- return !unlocked;
-}
-
bool keystore_get_bip39_word_stack(uint16_t idx, char* word_out, size_t word_out_size)
{
return rust_get_bip39_word(idx, rust_util_bytes_mut((uint8_t*)word_out, word_out_size));
diff --git a/src/keystore.h b/src/keystore.h
index b1d9903..a01e974 100644
--- a/src/keystore.h
+++ b/src/keystore.h
@@ -81,12 +81,6 @@ USE_RESULT keystore_error_t keystore_unlock(
uint8_t* seed_out,
size_t* seed_len_out);
-/**
- * @return false if the keystore is unlocked (keystore_unlock() followed by
- * keystore_unlock_bip39()), true otherwise.
- */
-USE_RESULT bool keystore_is_locked(void);
-
/**
* Retrieves the BIP39 word by index. `word_out` should be of at least 9 bytes long.
*/
diff --git a/src/rust/bitbox02-rust/src/keystore.rs b/src/rust/bitbox02-rust/src/keystore.rs
index 09431d0..38e4f81 100644
--- a/src/rust/bitbox02-rust/src/keystore.rs
+++ b/src/rust/bitbox02-rust/src/keystore.rs
@@ -121,7 +121,8 @@ pub fn lock() {
/// Returns false if the keystore is unlocked (unlock() followed by unlock_bip39()), true otherwise.
pub fn is_locked() -> bool {
- keystore::_is_locked()
+ let unlocked = RETAINED_SEED.read().is_some() && RETAINED_BIP39_SEED.read().is_some();
+ !unlocked
}
fn verify_seed(encryption_key: &[u8], expected_seed: &[u8]) -> bool {
@@ -412,13 +413,13 @@ pub extern "C" fn rust_keystore_lock() {
}
#[unsafe(no_mangle)]
-pub extern "C" fn rust_keystore_is_unlocked_device() -> bool {
- RETAINED_SEED.read().is_some()
+pub extern "C" fn rust_keystore_is_locked() -> bool {
+ is_locked()
}
#[unsafe(no_mangle)]
-pub extern "C" fn rust_keystore_is_unlocked_bip39() -> bool {
- RETAINED_BIP39_SEED.read().is_some()
+pub extern "C" fn rust_keystore_is_unlocked_device() -> bool {
+ RETAINED_SEED.read().is_some()
}
#[unsafe(no_mangle)]
diff --git a/src/rust/bitbox02-sys/build.rs b/src/rust/bitbox02-sys/build.rs
index fcec222..56dfd9a 100644
--- a/src/rust/bitbox02-sys/build.rs
+++ b/src/rust/bitbox02-sys/build.rs
@@ -75,7 +75,6 @@ const ALLOWLIST_FNS: &[&str] = &[
"keystore_bip39_mnemonic_to_seed",
"keystore_encrypt_and_store_seed",
"keystore_get_bip39_word",
- "keystore_is_locked",
"keystore_secp256k1_nonce_commit",
"keystore_secp256k1_sign",
"keystore_unlock",
diff --git a/src/rust/bitbox02/src/keystore.rs b/src/rust/bitbox02/src/keystore.rs
index c3707a7..b74f5c5 100644
--- a/src/rust/bitbox02/src/keystore.rs
+++ b/src/rust/bitbox02/src/keystore.rs
@@ -26,10 +26,6 @@ use bitbox02_sys::keystore_error_t;
const EC_PUBLIC_KEY_LEN: usize = 33;
pub const MAX_SEED_LENGTH: usize = bitbox02_sys::KEYSTORE_MAX_SEED_LENGTH as usize;
-pub fn _is_locked() -> bool {
- unsafe { bitbox02_sys::keystore_is_locked() }
-}
-
#[derive(Debug)]
pub enum Error {
CannotUnlockBIP39,
diff --git a/src/u2f.c b/src/u2f.c
index 04e0737..a4c9d0e 100644
--- a/src/u2f.c
+++ b/src/u2f.c
@@ -185,7 +185,7 @@ static void _stop_refresh_webpage_screen(void)
*/
static bool _unlock_if_locked(void)
{
- if (keystore_is_locked()) {
+ if (rust_keystore_is_locked()) {
rust_workflow_spawn_unlock();
return false;
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.