Skip to content

Conversation

@cedwies
Copy link
Collaborator

@cedwies cedwies commented Dec 14, 2025

Adds

  1. get_unlock_attempts
  2. fn increment_unlock_attempts
  3. reset_unlock_attempts
  4. get_max_unlock_attempts

to HAL

@cedwies cedwies marked this pull request as ready for review December 14, 2025 18:05
@cedwies cedwies requested a review from benma December 14, 2025 18:05
bitbox02::memory::smarteeprom_reset_unlock_attempts()
}
fn get_max_unlock_attempts(&mut self) -> u8 {
bitbox02::memory::MAX_UNLOCK_ATTEMPTS
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unlikely to change in different implementations, so I'd remove that function and define this constant in keystore.rs instead.

The only uses of it in C are the sanity checks in the smarteeprom code. Try and see if you can get rid of them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the get_max_unlock_attempts from hal.rs, defined MAX_UNLOCK_ATTEMPTS in keystore.rs
But I would keep the sanity checks, I'd prefer to keep them as defense in depth.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not optimal to have the same const defined in two places, though it could be acceptable. In that case, maybe add a comment to both that mention the other.

But maybe they can be removed after all: bitbox02::memory::smarteeprom_increment_unlock_attempts(); in keystore.rs is called only if get_remaining_unlock_attempts() != 0, so the sanity check in there:

if (unlock_attempts == MAX_UNLOCK_ATTEMPTS) {
        Abort("Tried to increment the number of unlocks past the maximum allowed value.");
    }

cannot happen. The other sanity check in bitbox02_smarteeprom_get_unlock_attempts could be ported Rust.

Imho it's okay for the app-layer to do sanity checks instead of the storage layer - the storage layer could just blindly set/get.

What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally agree with you, however, there are some reasons why in this case I prefer the constant in two places:

  1. MAX_UNLOCK_ATTEMPTS in C is also used in optiga.c as SMALL_MONOTONIC_COUNTER_MAX_USE. So C still needs this constant, independent of Smarteeprom sanity checks, no? That means even if you delete the sanity checks in bitbox02_smarteeprom.c, you still have the constant defined in C and in Rust. Duplication would not go away.
  2. You are absolutely correct that under current Rust logic it is unreachable in normal operation. But enforcing the invariant at the storage layer API enforces it for any user of the API and can still catch weird situations (memory corruption, future buggy callers, ...)

I would prefer your suggestion regarding the comment. But I am happy to discuss further (especially if I have a logic error in my argumentation), unlock logic is a sensitive topic.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, and yes I forgot about the optiga part. Please add the comments then 😄

@cedwies cedwies force-pushed the cedwies/unlock-attempts branch 3 times, most recently from 37524fb to 37f0633 Compare December 17, 2025 15:38
@cedwies cedwies requested a review from benma December 18, 2025 09:37
Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

bitbox02::memory::smarteeprom_get_unlock_attempts(),
bitbox02::memory::MAX_UNLOCK_ATTEMPTS
);
let max = MAX_UNLOCK_ATTEMPTS;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could inline this one

@cedwies cedwies force-pushed the cedwies/unlock-attempts branch from 37f0633 to cad37d1 Compare December 18, 2025 13:37
@cedwies cedwies force-pushed the cedwies/unlock-attempts branch from cad37d1 to 1266caf Compare December 18, 2025 13:42
@cedwies cedwies merged commit 278da3a into BitBoxSwiss:master Dec 18, 2025
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants