keystore: remove remaining_attempts out param from keystore_unlock
What changed, and why it matters
This commit is a straightforward internal code cleanup. It removes an output parameter that reported how many password attempts were left from the unlock function, and instead makes callers use a separate existing function to get that count. The behavior of the device—how many attempts are allowed, what happens after too many wrong passwords, and how the user is informed—does not change.
No security action required. Treat as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors keystore_unlock() in C and its Rust FFI bindings by removing the remaining_attempts_out output parameter. Callers now invoke keystore::get_remaining_unlock_attempts() separately. The Rust Error::IncorrectPassword variant is simplified from a struct with a remaining_attempts field to a unit variant. The unlock workflow updates its error message construction to query remaining attempts independently. No logic governing attempt counting, max-attempts enforcement, or reset behavior is altered.
Changed components
src/keystore.csrc/keystore.hsrc/rust/bitbox02-rust/src/keystore.rssrc/rust/bitbox02-rust/src/workflow/unlock.rssrc/rust/bitbox02/src/keystore.rsInspect captured patch +9 / −36
diff --git a/src/keystore.c b/src/keystore.c
index 401942f..96dd4be 100644
--- a/src/keystore.c
+++ b/src/keystore.c
@@ -104,7 +104,6 @@ USE_RESULT static keystore_error_t _retain_seed(const uint8_t* seed, size_t seed
keystore_error_t keystore_unlock(
const char* password,
- uint8_t* remaining_attempts_out,
int* securechip_result_out,
uint8_t* seed_out,
size_t* seed_len_out)
@@ -119,7 +118,6 @@ keystore_error_t keystore_unlock(
* is made. So we should never enter this branch...
* This is just an extraordinary measure for added resilience.
*/
- *remaining_attempts_out = 0;
reset_reset(false);
return KEYSTORE_ERR_MAX_ATTEMPTS_EXCEEDED;
}
@@ -132,16 +130,13 @@ keystore_error_t keystore_unlock(
keystore_error_t result =
_get_and_decrypt_seed(password, seed, &seed_len, securechip_result_out);
if (result != KEYSTORE_OK) {
- // Compute remaining attempts
failed_attempts = bitbox02_smarteeprom_get_unlock_attempts();
if (failed_attempts >= MAX_UNLOCK_ATTEMPTS) {
- *remaining_attempts_out = 0;
reset_reset(false);
return KEYSTORE_ERR_MAX_ATTEMPTS_EXCEEDED;
}
- *remaining_attempts_out = MAX_UNLOCK_ATTEMPTS - failed_attempts;
return result;
}
diff --git a/src/keystore.h b/src/keystore.h
index 8a5367f..e2c52d3 100644
--- a/src/keystore.h
+++ b/src/keystore.h
@@ -58,8 +58,6 @@ typedef enum {
* check the password).
* @param[in] password keystore password, used to decrypt the seed.
* If it is false, the keystore is not unlocked.
- * @param[out] On any error, remaining_attempts_out will have the number of remaining attempts.
- * If zero, the keystore is locked until the device is reset.
* @param[out] securechip_result_out, if not NULL, will contain the error code from
* @param[out] seed_out The seed bytes copied from the retained seed.
* The buffer should be KEYSTORE_MAX_SEED_LENGTH bytes long. The caller must
@@ -73,7 +71,6 @@ typedef enum {
*/
USE_RESULT keystore_error_t keystore_unlock(
const char* password,
- uint8_t* remaining_attempts_out,
int* securechip_result_out,
uint8_t* seed_out,
size_t* seed_len_out);
diff --git a/src/rust/bitbox02-rust/src/keystore.rs b/src/rust/bitbox02-rust/src/keystore.rs
index edab318..0fd3531 100644
--- a/src/rust/bitbox02-rust/src/keystore.rs
+++ b/src/rust/bitbox02-rust/src/keystore.rs
@@ -877,8 +877,7 @@ mod tests {
for i in 1..bitbox02::memory::MAX_UNLOCK_ATTEMPTS {
assert!(matches!(
unlock("invalid password"),
- Err(Error::IncorrectPassword { remaining_attempts }) if remaining_attempts
- == bitbox02::memory::MAX_UNLOCK_ATTEMPTS - i
+ Err(Error::IncorrectPassword)
));
assert_eq!(
get_remaining_unlock_attempts(),
@@ -918,8 +917,7 @@ mod tests {
for attempt in 1..bitbox02::memory::MAX_UNLOCK_ATTEMPTS {
assert!(matches!(
unlock("invalid password"),
- Err(Error::IncorrectPassword { remaining_attempts })
- if remaining_attempts == bitbox02::memory::MAX_UNLOCK_ATTEMPTS - attempt
+ Err(Error::IncorrectPassword),
));
assert_eq!(
@@ -990,11 +988,7 @@ mod tests {
lock();
let wrong_attempt = || {
- assert!(matches!(
- unlock("wrong"),
- Err(Error::IncorrectPassword { remaining_attempts })
- if remaining_attempts == bitbox02::memory::MAX_UNLOCK_ATTEMPTS - 1
- ));
+ assert!(matches!(unlock("wrong"), Err(Error::IncorrectPassword)));
assert_eq!(
get_remaining_unlock_attempts(),
bitbox02::memory::MAX_UNLOCK_ATTEMPTS - 1
@@ -1034,11 +1028,7 @@ mod tests {
assert!(copy_seed().is_ok());
let wrong_attempt = || {
- assert!(matches!(
- unlock("wrong"),
- Err(Error::IncorrectPassword { remaining_attempts })
- if remaining_attempts == bitbox02::memory::MAX_UNLOCK_ATTEMPTS - 1
- ));
+ assert!(matches!(unlock("wrong"), Err(Error::IncorrectPassword)));
assert_eq!(
get_remaining_unlock_attempts(),
bitbox02::memory::MAX_UNLOCK_ATTEMPTS - 1
@@ -1687,12 +1677,7 @@ mod tests {
assert!(copy_seed().is_err());
// Wrong password.
- assert!(matches!(
- unlock("bar"),
- Err(Error::IncorrectPassword {
- remaining_attempts: 9
- })
- ));
+ assert!(matches!(unlock("bar"), Err(Error::IncorrectPassword)));
assert_eq!(get_remaining_unlock_attempts(), 9);
// Correct password. First time: unlock. After unlock, it becomes a password check.
diff --git a/src/rust/bitbox02-rust/src/workflow/unlock.rs b/src/rust/bitbox02-rust/src/workflow/unlock.rs
index bf7c867..8bccc21 100644
--- a/src/rust/bitbox02-rust/src/workflow/unlock.rs
+++ b/src/rust/bitbox02-rust/src/workflow/unlock.rs
@@ -91,8 +91,8 @@ pub async fn unlock_keystore(
match crate::keystore::unlock(&password) {
Ok(seed) => Ok(seed),
- Err(crate::keystore::Error::IncorrectPassword { remaining_attempts }) => {
- let msg = match remaining_attempts {
+ Err(crate::keystore::Error::IncorrectPassword) => {
+ let msg = match crate::keystore::get_remaining_unlock_attempts() {
1 => "Wrong password\n1 try remains".into(),
n => format!("Wrong password\n{} tries remain", n),
};
diff --git a/src/rust/bitbox02/src/keystore.rs b/src/rust/bitbox02/src/keystore.rs
index a8cde05..49045ab 100644
--- a/src/rust/bitbox02/src/keystore.rs
+++ b/src/rust/bitbox02/src/keystore.rs
@@ -27,7 +27,7 @@ pub const MAX_SEED_LENGTH: usize = bitbox02_sys::KEYSTORE_MAX_SEED_LENGTH as usi
#[derive(Debug)]
pub enum Error {
CannotUnlockBIP39,
- IncorrectPassword { remaining_attempts: u8 },
+ IncorrectPassword,
MaxAttemptsExceeded,
Unseeded,
Memory,
@@ -71,7 +71,6 @@ impl core::convert::From<keystore_error_t> for Error {
}
pub fn _unlock(password: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, Error> {
- let mut remaining_attempts: u8 = 0;
let mut securechip_result: i32 = 0;
let mut seed = zeroize::Zeroizing::new([0u8; MAX_SEED_LENGTH].to_vec());
let mut seed_len: usize = 0;
@@ -81,7 +80,6 @@ pub fn _unlock(password: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, Error> {
.unwrap()
.as_ptr()
.cast(),
- &mut remaining_attempts,
&mut securechip_result,
seed.as_mut_ptr(),
&mut seed_len,
@@ -91,9 +89,7 @@ pub fn _unlock(password: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, Error> {
seed.truncate(seed_len);
Ok(seed)
}
- keystore_error_t::KEYSTORE_ERR_INCORRECT_PASSWORD => {
- Err(Error::IncorrectPassword { remaining_attempts })
- }
+ keystore_error_t::KEYSTORE_ERR_INCORRECT_PASSWORD => Err(Error::IncorrectPassword),
keystore_error_t::KEYSTORE_ERR_SECURECHIP => Err(Error::SecureChip(securechip_result)),
err => Err(err.into()),
}
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.