keystore: cover early reset in unit test
What changed, and why it matters
This commit only adds a new unit test and the test-only helper functions needed to run it. It checks that the device refuses further unlock attempts once the maximum failed-attempt count has already been reached. No production code behavior is changed, so this is not a security fix or vulnerability by itself.
No action required; treat as routine test-coverage improvement. If reviewing a larger series, verify that the production behavior being tested was already present and correct before this commit.
Security signals we found
Adds test coverage for an existing lockout/anti-hammering check
Exposes smart-EEPROM unlock-attempt counter functions only under testing feature
No modification to production unlock, encryption, or seed-handling logic
Evidence from the diff
The change adds test_unlock_preexisting_lockout in keystore.rs to verify that unlock() returns Error::MaxAttemptsExceeded when smarteeprom_get_unlock_attempts() already equals MAX_UNLOCK_ATTEMPTS. To support the test, it exposes four C smart-EEPROM functions in bitbox02-sys/build.rs and wraps them under #[cfg(feature = "testing")] in memory.rs, plus a set_unlock_attempts_for_testing helper. The production unlock path is unchanged.
Changed components
src/rust/bitbox02-rust/src/keystore.rssrc/rust/bitbox02-sys/build.rssrc/rust/bitbox02/src/memory.rsInspect captured patch +66 / −0
diff --git a/src/rust/bitbox02-rust/src/keystore.rs b/src/rust/bitbox02-rust/src/keystore.rs
index b3c230c..b5798a7 100644
--- a/src/rust/bitbox02-rust/src/keystore.rs
+++ b/src/rust/bitbox02-rust/src/keystore.rs
@@ -925,6 +925,38 @@ mod tests {
assert!(matches!(unlock("password"), Err(Error::Unseeded)));
}
+ /// Ensures that if the recorded unlock attempts already reached the maximum before calling
+ /// `unlock()`, the keystore immediately returns `MaxAttemptsExceeded` without performing any
+ /// secure chip operations.
+ #[test]
+ fn test_unlock_preexisting_lockout() {
+ mock_memory();
+ lock();
+
+ let seed = hex!("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044");
+ let mock_salt_root =
+ hex!("3333333333333333444444444444444411111111111111112222222222222222");
+ bitbox02::memory::set_salt_root(&mock_salt_root).unwrap();
+
+ assert!(encrypt_and_store_seed(&mut TestingHal::new(), &seed, "password").is_ok());
+ lock();
+ assert!(is_locked());
+
+ bitbox02::memory::set_unlock_attempts_for_testing(bitbox02::memory::MAX_UNLOCK_ATTEMPTS);
+ assert_eq!(
+ bitbox02::memory::smarteeprom_get_unlock_attempts(),
+ bitbox02::memory::MAX_UNLOCK_ATTEMPTS
+ );
+
+ assert!(matches!(
+ unlock("password"),
+ Err(Error::MaxAttemptsExceeded)
+ ));
+ assert!(is_locked());
+ assert!(copy_seed().is_err());
+ assert!(!bitbox02::memory::is_seeded());
+ }
+
/// Ensures the failed-attempt counter resets once a correct password is entered while the
/// keystore is locked, so a later wrong attempt after relocking still sees the full allowance.
#[test]
diff --git a/src/rust/bitbox02-sys/build.rs b/src/rust/bitbox02-sys/build.rs
index 3b7af49..1acffe5 100644
--- a/src/rust/bitbox02-sys/build.rs
+++ b/src/rust/bitbox02-sys/build.rs
@@ -58,6 +58,10 @@ const ALLOWLIST_TYPES: &[&str] = &[
const ALLOWLIST_FNS: &[&str] = &[
"bip32_derive_xpub",
+ "bitbox02_smarteeprom_init",
+ "bitbox02_smarteeprom_get_unlock_attempts",
+ "bitbox02_smarteeprom_increment_unlock_attempts",
+ "bitbox02_smarteeprom_reset_unlock_attempts",
"bitbox_secp256k1_dleq_prove",
"bitbox_secp256k1_dleq_verify",
"bitbox02_smarteeprom_init",
diff --git a/src/rust/bitbox02/src/memory.rs b/src/rust/bitbox02/src/memory.rs
index a0661a9..e173b47 100644
--- a/src/rust/bitbox02/src/memory.rs
+++ b/src/rust/bitbox02/src/memory.rs
@@ -170,6 +170,36 @@ pub fn reset_hww() -> Result<(), ()> {
}
}
+#[cfg(feature = "testing")]
+pub fn smarteeprom_get_unlock_attempts() -> u8 {
+ unsafe { bitbox02_sys::bitbox02_smarteeprom_get_unlock_attempts() }
+}
+
+#[cfg(feature = "testing")]
+pub fn smarteeprom_increment_unlock_attempts() {
+ unsafe {
+ bitbox02_sys::bitbox02_smarteeprom_increment_unlock_attempts();
+ }
+}
+
+#[cfg(feature = "testing")]
+pub fn smarteeprom_reset_unlock_attempts() {
+ unsafe {
+ bitbox02_sys::bitbox02_smarteeprom_reset_unlock_attempts();
+ }
+}
+
+/// Testing helper to set the recorded number of unlock attempts to a precise value.
+/// Panics if `attempts` exceeds the maximum supported by the firmware.
+#[cfg(feature = "testing")]
+pub fn set_unlock_attempts_for_testing(attempts: u8) {
+ assert!(attempts <= MAX_UNLOCK_ATTEMPTS);
+ smarteeprom_reset_unlock_attempts();
+ for _ in 0..attempts {
+ smarteeprom_increment_unlock_attempts();
+ }
+}
+
pub fn multisig_set_by_hash(hash: &[u8], name: &str) -> Result<(), MemoryError> {
if hash.len() != 32 {
return Err(MemoryError::MEMORY_ERR_INVALID_INPUT);
Why this scored 12/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.