1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-02 19:40:57 +00:00

feat(core/rust): randomize PIN digits fully and show last digit to user

[no changelog]
This commit is contained in:
grdddj 2023-06-06 08:49:07 +02:00 committed by Jiří Musil
parent 5efafce856
commit 8c7ad72062

View File

@ -42,6 +42,10 @@ const CHOICES: [(&str, PinAction, Option<Icon>); CHOICE_LENGTH] = [
("9", PinAction::Digit('9'), None), ("9", PinAction::Digit('9'), None),
]; ];
fn get_random_digit_position() -> usize {
random::uniform_between(NUMBER_START_INDEX as u32, (CHOICE_LENGTH - 1) as u32) as usize
}
struct ChoiceFactoryPIN; struct ChoiceFactoryPIN;
impl<T: StringType + Clone> ChoiceFactory<T> for ChoiceFactoryPIN { impl<T: StringType + Clone> ChoiceFactory<T> for ChoiceFactoryPIN {
@ -90,9 +94,9 @@ where
let choices = ChoiceFactoryPIN; let choices = ChoiceFactoryPIN;
Self { Self {
// Starting at the digit 0 // Starting at a random digit.
choice_page: ChoicePage::new(choices) choice_page: ChoicePage::new(choices)
.with_initial_page_counter(NUMBER_START_INDEX) .with_initial_page_counter(get_random_digit_position())
.with_carousel(true), .with_carousel(true),
pin_line: Child::new(ChangingTextLine::center_bold(String::from(prompt.as_ref()))), pin_line: Child::new(ChangingTextLine::center_bold(String::from(prompt.as_ref()))),
subprompt_line: Child::new(ChangingTextLine::center_mono(subprompt)), subprompt_line: Child::new(ChangingTextLine::center_mono(subprompt)),
@ -120,10 +124,13 @@ where
} else if self.show_real_pin { } else if self.show_real_pin {
String::from(self.pin()) String::from(self.pin())
} else { } else {
// Showing asterisks and the last digit.
let mut dots: String<MAX_PIN_LENGTH> = String::new(); let mut dots: String<MAX_PIN_LENGTH> = String::new();
for _ in 0..self.textbox.len() { for _ in 0..self.textbox.len() - 1 {
unwrap!(dots.push_str("*")); unwrap!(dots.push('*'));
} }
let last_char = unwrap!(self.textbox.content().chars().last());
unwrap!(dots.push(last_char));
dots dots
}; };
@ -191,15 +198,9 @@ where
Some(PinAction::Enter) => Some(CancelConfirmMsg::Confirmed), Some(PinAction::Enter) => Some(CancelConfirmMsg::Confirmed),
Some(PinAction::Digit(ch)) if !self.is_full() => { Some(PinAction::Digit(ch)) if !self.is_full() => {
self.textbox.append(ctx, ch); self.textbox.append(ctx, ch);
// Choosing random digit to be shown next, but different // Choosing random digit to be shown next
// from the current choice.
let new_page_counter = random::uniform_between_except(
NUMBER_START_INDEX as u32,
(CHOICE_LENGTH - 1) as u32,
self.choice_page.page_index() as u32,
);
self.choice_page self.choice_page
.set_page_counter(ctx, new_page_counter as usize); .set_page_counter(ctx, get_random_digit_position());
self.update(ctx); self.update(ctx);
None None
} }