What changed, and why it matters
This commit is a straightforward internal refactoring of how the BitBox02 firmware's user-interface code asks for a recovery mnemonic during wallet restore. It moves the 'get_mnemonic' call into a hardware-abstraction trait so unit tests can supply a canned mnemonic instead of simulating button presses. There is no user-facing behavior change and no security fix or vulnerability introduced.
No security action required; this is a testability refactor. Reviewers may optionally verify that the real `Workflows` default implementation still calls the original `mnemonic::get` and that the hard-coded test mnemonic is not used in production builds.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change adds a default-implemented get_mnemonic() method to the Workflows trait and updates restore::from_mnemonic to call hal.ui().get_mnemonic() instead of workflow::mnemonic::get(hal). The real implementation still delegates to mnemonic::get, while the test implementation returns a hard-coded BIP39 sentence. Helper functions in mnemonic.rs are narrowed from accepting impl Hal to accepting impl Ui, and the documented supported word counts are corrected from ‘12, 18 or 24’ to ‘12 or 24’ to match the actual code paths. No cryptographic, memory-safety, or access-control changes are present.
Changed components
src/rust/bitbox02-rust/src/hww/api/restore.rssrc/rust/bitbox02-rust/src/workflow.rssrc/rust/bitbox02-rust/src/workflow/mnemonic.rssrc/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rssrc/rust/bitbox02-rust/src/workflow/testing.rsInspect captured patch +39 / −27
diff --git a/src/rust/bitbox02-rust/src/hww/api/restore.rs b/src/rust/bitbox02-rust/src/hww/api/restore.rs
index a884f49..d89f990 100644
--- a/src/rust/bitbox02-rust/src/hww/api/restore.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/restore.rs
@@ -8,7 +8,7 @@ use pb::response::Response;
#[cfg(feature = "app-u2f")]
use crate::hal::SecureChip;
use crate::hal::{Memory, Ui};
-use crate::workflow::{confirm, mnemonic, password, unlock};
+use crate::workflow::{confirm, password, unlock};
pub async fn from_file(
hal: &mut impl crate::hal::Hal,
@@ -104,7 +104,7 @@ pub async fn from_mnemonic(
.await?;
}
- let mnemonic = mnemonic::get(hal).await?;
+ let mnemonic = hal.ui().get_mnemonic().await?;
let seed = match crate::bip39::mnemonic_to_seed(&mnemonic) {
Ok(seed) => seed,
Err(()) => {
diff --git a/src/rust/bitbox02-rust/src/workflow.rs b/src/rust/bitbox02-rust/src/workflow.rs
index 54d86f6..20e005b 100644
--- a/src/rust/bitbox02-rust/src/workflow.rs
+++ b/src/rust/bitbox02-rust/src/workflow.rs
@@ -83,6 +83,17 @@ pub trait Workflows {
{
mnemonic::show_and_confirm_mnemonic(self, words).await
}
+
+ /// Retrieve a BIP39 mnemonic sentence of 12 or 24 words from the user.
+ ///
+ /// This function is defined in the HAL so unit tests can easily mock it. Real implementations
+ /// should leave the default implementation.
+ async fn get_mnemonic(&mut self) -> Result<zeroize::Zeroizing<String>, cancel::Error>
+ where
+ Self: Sized,
+ {
+ mnemonic::get(self).await
+ }
}
pub struct RealWorkflows;
diff --git a/src/rust/bitbox02-rust/src/workflow/mnemonic.rs b/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
index 758859b..6df3dac 100644
--- a/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
+++ b/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
-use super::Workflows;
pub use super::cancel::Error as CancelError;
use super::cancel::{cancel, set_result, with_cancel};
use super::confirm;
@@ -214,7 +213,7 @@ fn lastword_choices_strings(entered_words: &[&str]) -> Vec<zeroize::Zeroizing<St
/// Returns `Ok(Some(word))` if the user chooses a word.
/// Returns `Err(CancelError::Cancelled)` if the user cancels.
async fn get_24th_word(
- hal: &mut impl crate::hal::Hal,
+ hal_ui: &mut impl crate::hal::Ui,
title: &str,
entered_words: &[&str],
) -> Result<Option<zeroize::Zeroizing<String>>, CancelError> {
@@ -225,7 +224,7 @@ async fn get_24th_word(
choices.len() - 1
};
loop {
- match hal.ui().menu(&as_str_vec(&choices), Some(title)).await {
+ match hal_ui.menu(&as_str_vec(&choices), Some(title)).await {
Err(menu::CancelError::Cancelled) => return Err(CancelError::Cancelled),
Ok(choice_idx) if choice_idx as usize == none_of_them_idx => {
let params = confirm::Params {
@@ -233,7 +232,7 @@ async fn get_24th_word(
body: "Invalid. Check\nrecovery words.\nRestart?",
..Default::default()
};
- if let Ok(()) = hal.ui().confirm(¶ms).await {
+ if let Ok(()) = hal_ui.confirm(¶ms).await {
return Ok(None);
}
}
@@ -241,8 +240,7 @@ async fn get_24th_word(
// Confirm word picked from menu again, as a typo here would be extremely annoying.
// Double checking is also safer, as the user might not even realize they made a typo.
let word = choices[choice_idx as usize].clone();
- if let Ok(()) = hal
- .ui()
+ if let Ok(()) = hal_ui
.confirm(&confirm::Params {
title,
body: &word,
@@ -263,7 +261,7 @@ async fn get_24th_word(
/// Returns `Ok(word)` if the user chooses a word.
/// Returns `Err(CancelError::Cancelled)` if the user cancels.
async fn get_12th_18th_word(
- hal: &mut impl crate::hal::Hal,
+ hal_ui: &mut impl crate::hal::Ui,
title: &str,
entered_words: &[&str],
) -> Result<zeroize::Zeroizing<String>, CancelError> {
@@ -271,8 +269,7 @@ async fn get_12th_18th_word(
// these.
loop {
let choices = lastword_choices(entered_words);
- let word = hal
- .ui()
+ let word = hal_ui
.enter_string(
&trinary_input_string::Params {
title,
@@ -286,8 +283,7 @@ async fn get_12th_18th_word(
// Confirm word picked again, as a typo here would be extremely annoying. Double checking
// is also safer, as the user might not even realize they made a typo.
- if let Ok(()) = hal
- .ui()
+ if let Ok(()) = hal_ui
.confirm(&confirm::Params {
title,
body: &word,
@@ -300,12 +296,11 @@ async fn get_12th_18th_word(
}
}
-/// Retrieve a BIP39 mnemonic sentence of 12, 18 or 24 words from the user.
+/// Retrieve a BIP39 mnemonic sentence of 12 or 24 words from the user.
pub async fn get(
- hal: &mut impl crate::hal::Hal,
+ hal_ui: &mut impl crate::hal::Ui,
) -> Result<zeroize::Zeroizing<String>, CancelError> {
- let num_words: usize = match hal
- .ui()
+ let num_words: usize = match hal_ui
.trinary_choice("How many words?", Some("12"), None, Some("24"))
.await
{
@@ -314,7 +309,7 @@ pub async fn get(
TrinaryChoice::TRINARY_CHOICE_RIGHT => 24,
};
- hal.ui()
+ hal_ui
.status(&format!("Enter {} words", num_words), true)
.await;
@@ -340,16 +335,16 @@ pub async fn get(
// throws, for example.
if num_words == 24 {
// With 24 words there are only 8 valid candidates. We presnet them as a menu.
- match get_24th_word(hal, &title, &as_str_vec(&entered_words[..word_idx])).await {
+ match get_24th_word(hal_ui, &title, &as_str_vec(&entered_words[..word_idx])).await {
Ok(None) => return Err(CancelError::Cancelled),
Ok(Some(r)) => Ok(r),
Err(e) => Err(e),
}
} else {
- get_12th_18th_word(hal, &title, &as_str_vec(&entered_words[..word_idx])).await
+ get_12th_18th_word(hal_ui, &title, &as_str_vec(&entered_words[..word_idx])).await
}
} else {
- hal.ui()
+ hal_ui
.enter_string(
&trinary_input_string::Params {
title: &title,
@@ -378,8 +373,7 @@ pub async fn get(
} else {
// In all other words, we give the choice between editing the previous word and
// cancelling.
- match hal
- .ui()
+ match hal_ui
.menu(&["Edit previous word", "Cancel restore"], Some("Choose"))
.await
{
@@ -402,7 +396,7 @@ pub async fn get(
..Default::default()
};
- if let Err(confirm::UserAbort) = hal.ui().confirm(¶ms).await {
+ if let Err(confirm::UserAbort) = hal_ui.confirm(¶ms).await {
// Cancel cancelled.
continue;
}
diff --git a/src/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rs b/src/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rs
index 43c1275..fc1bb4d 100644
--- a/src/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rs
+++ b/src/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rs
@@ -25,9 +25,7 @@ pub async fn show_and_confirm_mnemonic(
Ok(())
}
-pub async fn get(
- _hal: &mut impl crate::hal::Hal,
-) -> Result<zeroize::Zeroizing<String>, CancelError> {
+pub async fn get(_ui: &mut impl crate::hal::Ui) -> Result<zeroize::Zeroizing<String>, CancelError> {
let words = "boring mistake dish oyster truth pigeon viable emerge sort crash wire portion cannon couple enact box walk height pull today solid off enable tide";
bitbox02::println_stdout("Restored from recovery words below:");
bitbox02::println_stdout(words);
diff --git a/src/rust/bitbox02-rust/src/workflow/testing.rs b/src/rust/bitbox02-rust/src/workflow/testing.rs
index 0736489..58d7c10 100644
--- a/src/rust/bitbox02-rust/src/workflow/testing.rs
+++ b/src/rust/bitbox02-rust/src/workflow/testing.rs
@@ -167,6 +167,15 @@ impl Workflows for TestingWorkflows<'_> {
});
Ok(())
}
+
+ async fn get_mnemonic(&mut self) -> Result<zeroize::Zeroizing<String>, cancel::Error>
+ where
+ Self: Sized,
+ {
+ let words = "boring mistake dish oyster truth pigeon viable emerge sort crash wire portion cannon couple enact box walk height pull today solid off enable tide";
+
+ Ok(zeroize::Zeroizing::new(words.into()))
+ }
}
impl<'a> TestingWorkflows<'a> {
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.