keystore: use HAL/Random in create_and_store_seed
What changed, and why it matters
This commit is a code-quality refactor, not a security fix. It changes how random numbers are passed to the seed-creation function so that automated tests can inject predictable values instead of relying on a special 'fake' random generator. There is no indication it fixes a vulnerability or changes real-device behavior.
No security action required. Treat as normal code-quality/testability improvement. Continue to verify that production HAL random implementation uses the device's approved TRNG/DRBG.
Security signals we found
Refactor of randomness plumbing in seed generation
Removal of test-only fake_reset() usage from unit tests
No change to production entropy source (still HAL-backed RNG)
Evidence from the diff
The patch refactors keystore::create_and_store_seed to accept a &mut impl crate::hal::Random rather than calling the global bitbox02::random::random_32_bytes() directly. The production call site in set_password.rs now passes hal.random(), which still resolves to the hardware-backed RNG. The test code replaces the previous bitbox02::random::fake_reset() hack with a deterministic TestingRandom mock that can be preloaded with expected values. This improves testability and removes a test-only backdoor from production code paths, but it does not alter the entropy source used on a real device.
Changed components
src/rust/bitbox02-rust/src/keystore.rssrc/rust/bitbox02-rust/src/hww/api/set_password.rssrc/rust/bitbox02-rust/src/hal.rsInspect captured patch +31 / −13
diff --git a/src/rust/bitbox02-rust/src/hal.rs b/src/rust/bitbox02-rust/src/hal.rs
index 11deb59..06f1ece 100644
--- a/src/rust/bitbox02-rust/src/hal.rs
+++ b/src/rust/bitbox02-rust/src/hal.rs
@@ -128,27 +128,40 @@ impl Hal for BitBox02Hal {
#[cfg(feature = "testing")]
pub mod testing {
use alloc::boxed::Box;
- use alloc::collections::BTreeMap;
+ use alloc::collections::{BTreeMap, VecDeque};
use alloc::string::String;
use alloc::vec::Vec;
use bitcoin::hashes::{Hash, sha256};
pub struct TestingRandom {
+ mock_next_values: VecDeque<[u8; 32]>,
counter: u32,
}
impl TestingRandom {
pub fn new() -> Self {
- Self { counter: 0 }
+ Self {
+ mock_next_values: VecDeque::new(),
+ counter: 0,
+ }
+ }
+
+ pub fn mock_next(&mut self, value: [u8; 32]) {
+ self.mock_next_values.push_back(value)
}
}
impl super::Random for TestingRandom {
fn random_32_bytes(&mut self) -> Box<zeroize::Zeroizing<[u8; 32]>> {
self.counter += 1;
- let hash = sha256::Hash::hash(&self.counter.to_be_bytes());
- Box::new(zeroize::Zeroizing::new(hash.to_byte_array()))
+ let value = if let Some(value) = self.mock_next_values.pop_front() {
+ value
+ } else {
+ let hash = sha256::Hash::hash(&self.counter.to_be_bytes());
+ hash.to_byte_array()
+ };
+ Box::new(zeroize::Zeroizing::new(value))
}
}
diff --git a/src/rust/bitbox02-rust/src/hww/api/set_password.rs b/src/rust/bitbox02-rust/src/hww/api/set_password.rs
index ecf98a5..44c2dc8 100644
--- a/src/rust/bitbox02-rust/src/hww/api/set_password.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/set_password.rs
@@ -36,7 +36,7 @@ pub async fn process(
return Err(Error::InvalidInput);
}
let password = password::enter_twice(hal).await?;
- if let Err(err) = keystore::create_and_store_seed(&password, entropy) {
+ if let Err(err) = keystore::create_and_store_seed(hal.random(), &password, entropy) {
hal.ui().status(&format!("Error\n{:?}", err), false).await;
return Err(Error::Generic);
}
diff --git a/src/rust/bitbox02-rust/src/keystore.rs b/src/rust/bitbox02-rust/src/keystore.rs
index 477b8ff..2ba610d 100644
--- a/src/rust/bitbox02-rust/src/keystore.rs
+++ b/src/rust/bitbox02-rust/src/keystore.rs
@@ -115,13 +115,17 @@ pub fn encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error>
/// password. The size of the host entropy determines the size of the seed. Can be either 16 or 32
/// bytes, resulting in 12 or 24 BIP39 recovery words.
/// This also unlocks the keystore with the new seed.
-pub fn create_and_store_seed(password: &str, host_entropy: &[u8]) -> Result<(), Error> {
+pub fn create_and_store_seed(
+ random: &mut impl crate::hal::Random,
+ password: &str,
+ host_entropy: &[u8],
+) -> Result<(), Error> {
let seed_len = host_entropy.len();
if !matches!(seed_len, 16 | 32) {
return Err(Error::SeedSize);
}
- let mut seed_vec = bitbox02::random::random_32_bytes();
+ let mut seed_vec = random.random_32_bytes();
let seed = &mut seed_vec[..seed_len];
// Mix in host entropy.
@@ -575,19 +579,18 @@ mod tests {
hex::decode("25569b9a11f9db6560459e8e48b4727a4c935300143d978989ed55db1d1b9cbe25569b9a11f9db6560459e8e48b4727a4c935300143d978989ed55db1d1b9cbe")
.unwrap();
+ let mut random = crate::hal::testing::TestingRandom::new();
+
// Invalid seed lengths
for size in [8, 24, 40] {
assert!(matches!(
- create_and_store_seed("password", &host_entropy[..size]),
+ create_and_store_seed(&mut random, "password", &host_entropy[..size]),
Err(Error::SeedSize)
));
}
// Hack to get the random bytes that will be used.
- let seed_random = {
- bitbox02::random::fake_reset();
- bitbox02::random::random_32_bytes()
- };
+ let seed_random = [0x34; 32];
// Derived from mock_salt_root and "password".
let password_salted_hashed =
@@ -608,7 +611,9 @@ mod tests {
bitbox02::memory::set_salt_root(mock_salt_root.as_slice().try_into().unwrap()).unwrap();
lock();
- assert!(create_and_store_seed("password", &host_entropy[..size]).is_ok());
+ let mut random = crate::hal::testing::TestingRandom::new();
+ random.mock_next(seed_random);
+ assert!(create_and_store_seed(&mut random, "password", &host_entropy[..size]).is_ok());
assert_eq!(copy_seed().unwrap().as_slice(), &expected_seed[..size]);
// Check the seed has been stored encrypted with the expected encryption key.
// Decrypt and check seed.
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.