rust/keystore: wrap copy_seed() in bitbox02-rust
What changed, and why it matters
This commit is a routine internal code reorganization. It moves a function called copy_seed() from a low-level C-binding Rust module into a higher-level Rust module, renames the original to _copy_seed(), and updates all callers. The actual behavior of the firmware does not change; it is purely a refactoring step to make future Rust-only implementations easier.
No security action required. Treat as normal refactoring and verify via existing unit tests.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change wraps bitbox02::keystore::copy_seed() as bitbox02_rust::keystore::copy_seed(), renaming the original C-binding function to _copy_seed(). All call sites in hww.rs, backup.rs, restore.rs, set_password.rs, and show_mnemonic.rs are updated to use the new wrapper. Unit tests are relocated accordingly. The function still returns a zeroized Vec
Changed components
src/rust/bitbox02-rust/src/keystore.rssrc/rust/bitbox02/src/keystore.rssrc/rust/bitbox02-rust/src/hww.rssrc/rust/bitbox02-rust/src/hww/api/backup.rssrc/rust/bitbox02-rust/src/hww/api/restore.rssrc/rust/bitbox02-rust/src/hww/api/set_password.rssrc/rust/bitbox02-rust/src/hww/api/show_mnemonic.rsInspect captured patch +56 / −51
diff --git a/src/rust/bitbox02-rust/src/hww.rs b/src/rust/bitbox02-rust/src/hww.rs
index 1c49d63..43e33ff 100644
--- a/src/rust/bitbox02-rust/src/hww.rs
+++ b/src/rust/bitbox02-rust/src/hww.rs
@@ -552,7 +552,7 @@ mod tests {
]
);
- let seed = bitbox02::keystore::copy_seed().unwrap();
+ let seed = crate::keystore::copy_seed().unwrap();
assert_eq!(seed.len(), host_entropy.len());
mock_hal.ui = crate::workflow::testing::TestingWorkflows::new();
assert!(matches!(
@@ -718,7 +718,7 @@ mod tests {
);
// Restored seed is the same as the seed that was backed up.
- assert_eq!(seed, bitbox02::keystore::copy_seed().unwrap());
+ assert_eq!(seed, crate::keystore::copy_seed().unwrap());
}
}
}
diff --git a/src/rust/bitbox02-rust/src/hww/api/backup.rs b/src/rust/bitbox02-rust/src/hww/api/backup.rs
index 51a531d..ce4fe3e 100644
--- a/src/rust/bitbox02-rust/src/hww/api/backup.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/backup.rs
@@ -30,7 +30,7 @@ pub async fn check(
return Err(Error::InvalidInput);
}
- let seed = bitbox02::keystore::copy_seed()?;
+ let seed = crate::keystore::copy_seed()?;
let id = backup::id(&seed);
let (backup_data, metadata) = backup::load(hal, &id).await?;
if seed.as_slice() != backup_data.get_seed() {
@@ -102,7 +102,7 @@ pub async fn create(
let seed = if is_initialized {
unlock::unlock_keystore(hal, "Unlock device", unlock::CanCancel::Yes).await?
} else {
- let seed = bitbox02::keystore::copy_seed()?;
+ let seed = crate::keystore::copy_seed()?;
// Yield now to give executor a chance to process USB/BLE communication, as copy_seed() causes
// some delay.
futures_lite::future::yield_now().await;
diff --git a/src/rust/bitbox02-rust/src/hww/api/restore.rs b/src/rust/bitbox02-rust/src/hww/api/restore.rs
index 55924c7..52dd088 100644
--- a/src/rust/bitbox02-rust/src/hww/api/restore.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/restore.rs
@@ -208,7 +208,7 @@ mod tests {
// Seed of hardcoded phrase used in unit tests:
// boring mistake dish oyster truth pigeon viable emerge sort crash wire portion cannon couple enact box walk height pull today solid off enable tide
assert_eq!(
- hex::encode(keystore::copy_seed().unwrap()),
+ hex::encode(crate::keystore::copy_seed().unwrap()),
"19f1bcfccf3e9d497cd245cf864ff0d42216625258d4f68d56b571aceb329257"
);
assert_eq!(
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 36cd2aa..cb88984 100644
--- a/src/rust/bitbox02-rust/src/hww/api/set_password.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/set_password.rs
@@ -40,7 +40,7 @@ pub async fn process(
hal.ui().status(&format!("Error\n{:?}", err), false).await;
return Err(Error::Generic);
}
- unlock::unlock_bip39(hal, &keystore::copy_seed()?).await;
+ unlock::unlock_bip39(hal, &crate::keystore::copy_seed()?).await;
Ok(Response::Success(pb::Success {}))
}
@@ -84,7 +84,7 @@ mod tests {
drop(mock_hal); // to remove mutable borrow of counter
assert_eq!(counter, 2);
assert!(!keystore::is_locked());
- assert!(keystore::copy_seed().unwrap().len() == 32);
+ assert!(crate::keystore::copy_seed().unwrap().len() == 32);
}
/// Shorter host entropy results in shorter seed.
@@ -106,7 +106,7 @@ mod tests {
Ok(Response::Success(pb::Success {}))
);
assert!(!keystore::is_locked());
- assert!(keystore::copy_seed().unwrap().len() == 16);
+ assert!(crate::keystore::copy_seed().unwrap().len() == 16);
}
/// Invalid host entropy size.
diff --git a/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs b/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
index e02e7af..f327976 100644
--- a/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
@@ -31,7 +31,7 @@ pub async fn process(hal: &mut impl crate::hal::Hal) -> Result<Response, Error>
let seed = if bitbox02::memory::is_initialized() {
unlock::unlock_keystore(hal, "Unlock device", unlock::CanCancel::Yes).await?
} else {
- bitbox02::keystore::copy_seed()?
+ crate::keystore::copy_seed()?
};
bitbox02::keystore::bip39_mnemonic_from_seed(&seed)?
diff --git a/src/rust/bitbox02-rust/src/keystore.rs b/src/rust/bitbox02-rust/src/keystore.rs
index 5fb0150..1438b50 100644
--- a/src/rust/bitbox02-rust/src/keystore.rs
+++ b/src/rust/bitbox02-rust/src/keystore.rs
@@ -33,9 +33,14 @@ pub fn lock() {
keystore::_lock();
}
+/// Returns a copy of the retained seed. Errors if the keystore is locked.
+pub fn copy_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
+ keystore::_copy_seed()
+}
+
/// Returns the keystore's seed encoded as a BIP-39 mnemonic.
pub fn get_bip39_mnemonic() -> Result<zeroize::Zeroizing<String>, ()> {
- keystore::bip39_mnemonic_from_seed(&keystore::copy_seed()?)
+ keystore::bip39_mnemonic_from_seed(©_seed()?)
}
fn get_xprv(keypath: &[u32]) -> Result<bip32::Xprv, ()> {
@@ -369,6 +374,38 @@ mod tests {
use bitcoin::secp256k1;
+ #[test]
+ fn test_copy_seed() {
+ // 12 words
+ mock_unlocked_using_mnemonic(
+ "trust cradle viable innocent stand equal little small junior frost laundry room",
+ "",
+ );
+ assert_eq!(
+ copy_seed().unwrap().as_slice(),
+ b"\xe9\xa6\x3f\xcd\x3a\x4d\x48\x98\x20\xa6\x63\x79\x2b\xad\xf6\xdd",
+ );
+
+ // 18 words
+ mock_unlocked_using_mnemonic(
+ "pupil parent toe bright slam plastic spy suspect verb battle nominee loan call crystal upset razor luggage join",
+ "",
+ );
+ assert_eq!(
+ copy_seed().unwrap().as_slice(),
+ b"\xad\xf4\x07\x8e\x0e\x0c\xb1\x4c\x34\xd6\xd6\xf2\x82\x6a\x57\xc1\x82\x06\x6a\xbb\xcd\x95\x84\xcf",
+ );
+
+ mock_unlocked_using_mnemonic(
+ "purity concert above invest pigeon category peace tuition hazard vivid latin since legal speak nation session onion library travel spell region blast estate stay",
+ "",
+ );
+ assert_eq!(
+ copy_seed().unwrap().as_slice(),
+ b"\xae\x45\xd4\x02\x3a\xfa\x4a\x48\x68\x77\x51\x69\xfe\xa5\xf5\xe4\x97\xf7\xa1\xa4\xd6\x22\x9a\xd0\x23\x9e\x68\x9b\x48\x2e\xd3\x5e",
+ );
+ }
+
#[test]
fn test_lock() {
lock();
diff --git a/src/rust/bitbox02/src/keystore.rs b/src/rust/bitbox02/src/keystore.rs
index 23a2141..a139d6a 100644
--- a/src/rust/bitbox02/src/keystore.rs
+++ b/src/rust/bitbox02/src/keystore.rs
@@ -193,7 +193,7 @@ pub fn create_and_store_seed(password: &str, host_entropy: &[u8]) -> Result<(),
}
}
-pub fn copy_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
+pub fn _copy_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
let mut seed = zeroize::Zeroizing::new([0u8; MAX_SEED_LENGTH].to_vec());
let mut seed_len: usize = 0;
match unsafe { bitbox02_sys::keystore_copy_seed(seed.as_mut_ptr(), &mut seed_len) } {
@@ -298,7 +298,7 @@ mod tests {
use super::*;
use bitcoin::secp256k1;
- use crate::testing::{mock_memory, mock_unlocked_using_mnemonic};
+ use crate::testing::mock_memory;
use util::bb02_async::block_on;
#[test]
@@ -357,38 +357,6 @@ mod tests {
);
}
- #[test]
- fn test_copy_seed() {
- // 12 words
- mock_unlocked_using_mnemonic(
- "trust cradle viable innocent stand equal little small junior frost laundry room",
- "",
- );
- assert_eq!(
- copy_seed().unwrap().as_slice(),
- b"\xe9\xa6\x3f\xcd\x3a\x4d\x48\x98\x20\xa6\x63\x79\x2b\xad\xf6\xdd",
- );
-
- // 18 words
- mock_unlocked_using_mnemonic(
- "pupil parent toe bright slam plastic spy suspect verb battle nominee loan call crystal upset razor luggage join",
- "",
- );
- assert_eq!(
- copy_seed().unwrap().as_slice(),
- b"\xad\xf4\x07\x8e\x0e\x0c\xb1\x4c\x34\xd6\xd6\xf2\x82\x6a\x57\xc1\x82\x06\x6a\xbb\xcd\x95\x84\xcf",
- );
-
- mock_unlocked_using_mnemonic(
- "purity concert above invest pigeon category peace tuition hazard vivid latin since legal speak nation session onion library travel spell region blast estate stay",
- "",
- );
- assert_eq!(
- copy_seed().unwrap().as_slice(),
- b"\xae\x45\xd4\x02\x3a\xfa\x4a\x48\x68\x77\x51\x69\xfe\xa5\xf5\xe4\x97\xf7\xa1\xa4\xd6\x22\x9a\xd0\x23\x9e\x68\x9b\x48\x2e\xd3\x5e",
- );
- }
-
#[test]
fn test_bip39_mnemonic_from_seed() {
// 12 words
@@ -473,7 +441,7 @@ mod tests {
// Still seeded.
assert!(crate::memory::is_seeded());
// Wrong password does not lock the keystore again if already unlocked.
- assert!(copy_seed().is_ok());
+ assert!(_copy_seed().is_ok());
}
// Last attempt, triggers reset.
assert!(matches!(
@@ -482,7 +450,7 @@ mod tests {
));
// Last wrong attempt locks & resets. There is no more seed.
assert!(!crate::memory::is_seeded());
- assert!(copy_seed().is_err());
+ assert!(_copy_seed().is_err());
assert!(matches!(unlock("password"), Err(Error::Unseeded)));
}
@@ -658,7 +626,7 @@ mod tests {
_lock();
assert!(create_and_store_seed("password", &host_entropy[..size]).is_ok());
- assert_eq!(copy_seed().unwrap().as_slice(), &expected_seed[..size]);
+ 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.
let cipher = crate::memory::get_encrypted_seed_and_hmac().unwrap();
@@ -690,7 +658,7 @@ mod tests {
assert!(encrypt_and_store_seed(&seed, "password").is_ok());
// Create new (different) seed.
assert!(encrypt_and_store_seed(&seed2, "password").is_ok());
- assert_eq!(copy_seed().unwrap().as_slice(), &seed2);
+ assert_eq!(_copy_seed().unwrap().as_slice(), &seed2);
}
// Functional test to store seeds, lock/unlock, retrieve seed.
@@ -708,11 +676,11 @@ mod tests {
assert!(encrypt_and_store_seed(&seed[..seed_size], "foo").is_ok());
}
// Also unlocks, so we can get the retained seed.
- assert_eq!(copy_seed().unwrap().as_slice(), &seed[..seed_size]);
+ assert_eq!(_copy_seed().unwrap().as_slice(), &seed[..seed_size]);
_lock();
// Can't get seed before unlock.
- assert!(copy_seed().is_err());
+ assert!(_copy_seed().is_err());
// Wrong password.
assert!(matches!(
@@ -726,7 +694,7 @@ mod tests {
for _ in 0..3 {
assert_eq!(unlock("foo").unwrap().as_slice(), &seed[..seed_size]);
}
- assert_eq!(copy_seed().unwrap().as_slice(), &seed[..seed_size]);
+ assert_eq!(_copy_seed().unwrap().as_slice(), &seed[..seed_size]);
// Can't store new seed once initialized.
crate::memory::set_initialized().unwrap();
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.