rust/keystore: wrap encrypt_and_store_seed() in bitbox02-rust
What changed, and why it matters
This commit is a routine internal code reorganization. It moves a function that encrypts and stores a wallet seed from one Rust module to another, renaming the original so the compiler catches any missed references. No security vulnerability is present in the change.
No security action required. Treat as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch wraps bitbox02::keystore::encrypt_and_store_seed() as bitbox02_rust::keystore::encrypt_and_store_seed(), renaming the underlying C-binding to _encrypt_and_store_seed(). Call sites and tests are updated accordingly. A test (test_create_and_unlock_twice) is relocated from the low-level bitbox02 crate to the higher-level bitbox02-rust crate. Function behavior and the underlying C implementation are unchanged.
Changed components
src/rust/bitbox02-rust/src/keystore.rssrc/rust/bitbox02/src/keystore.rssrc/rust/bitbox02-rust/src/hww/api/backup.rssrc/rust/bitbox02-rust/src/hww/api/restore.rssrc/rust/bitbox02-rust/src/hww/api/show_mnemonic.rssrc/rust/bitbox02-rust/src/workflow/unlock.rsInspect captured patch +38 / −32
diff --git a/src/rust/bitbox02-rust/src/hww/api/backup.rs b/src/rust/bitbox02-rust/src/hww/api/backup.rs
index ce4fe3e..690734e 100644
--- a/src/rust/bitbox02-rust/src/hww/api/backup.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/backup.rs
@@ -234,7 +234,7 @@ mod tests {
let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
.unwrap();
- bitbox02::keystore::encrypt_and_store_seed(&seed, "password").unwrap();
+ crate::keystore::encrypt_and_store_seed(&seed, "password").unwrap();
bitbox02::memory::set_initialized().unwrap();
let mut password_entered: bool = false;
diff --git a/src/rust/bitbox02-rust/src/hww/api/restore.rs b/src/rust/bitbox02-rust/src/hww/api/restore.rs
index 3e0d13e..3903a43 100644
--- a/src/rust/bitbox02-rust/src/hww/api/restore.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/restore.rs
@@ -66,7 +66,7 @@ pub async fn from_file(
let password = password::enter_twice(hal).await?;
let seed = data.get_seed();
- if let Err(err) = bitbox02::keystore::encrypt_and_store_seed(seed, &password) {
+ if let Err(err) = crate::keystore::encrypt_and_store_seed(seed, &password) {
hal.ui()
.status(&format!("Could not\nrestore backup\n{:?}", err), false)
.await;
@@ -142,7 +142,7 @@ pub async fn from_mnemonic(
}
};
- if let Err(err) = bitbox02::keystore::encrypt_and_store_seed(&seed, &password) {
+ if let Err(err) = crate::keystore::encrypt_and_store_seed(&seed, &password) {
hal.ui()
.status(&format!("Could not\nrestore backup\n{:?}", err), false)
.await;
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 f327976..4850730 100644
--- a/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
@@ -81,7 +81,7 @@ mod tests {
#[test]
fn test_process_uninitialized() {
mock_memory();
- bitbox02::keystore::encrypt_and_store_seed(
+ crate::keystore::encrypt_and_store_seed(
hex::decode("c7940c13479b8d9a6498f4e50d5a42e0d617bc8e8ac9f2b8cecf97e94c2b035c")
.unwrap()
.as_slice(),
@@ -131,7 +131,7 @@ mod tests {
#[test]
fn test_process_initialized() {
mock_memory();
- bitbox02::keystore::encrypt_and_store_seed(
+ crate::keystore::encrypt_and_store_seed(
hex::decode("c7940c13479b8d9a6498f4e50d5a42e0d617bc8e8ac9f2b8cecf97e94c2b035c")
.unwrap()
.as_slice(),
@@ -188,7 +188,7 @@ mod tests {
#[test]
fn test_process_initialized_wrong_password() {
mock_memory();
- bitbox02::keystore::encrypt_and_store_seed(
+ crate::keystore::encrypt_and_store_seed(
hex::decode("c7940c13479b8d9a6498f4e50d5a42e0d617bc8e8ac9f2b8cecf97e94c2b035c")
.unwrap()
.as_slice(),
diff --git a/src/rust/bitbox02-rust/src/keystore.rs b/src/rust/bitbox02-rust/src/keystore.rs
index 85d4ae3..8856e96 100644
--- a/src/rust/bitbox02-rust/src/keystore.rs
+++ b/src/rust/bitbox02-rust/src/keystore.rs
@@ -51,6 +51,12 @@ pub fn copy_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
keystore::_copy_seed()
}
+/// Restores a seed. This also unlocks the keystore with this seed.
+/// `password` is the password with which we encrypt the seed.
+pub fn encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error> {
+ keystore::_encrypt_and_store_seed(seed, password)
+}
+
/// 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(©_seed()?)
@@ -439,6 +445,24 @@ mod tests {
);
}
+ // This tests that you can create a keystore, unlock it, and then do this again. This is an
+ // expected workflow for when the wallet setup process is restarted after seeding and unlocking,
+ // but before creating a backup, in which case a new seed is created.
+ #[test]
+ fn test_create_and_unlock_twice() {
+ mock_memory();
+ lock();
+
+ let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
+ .unwrap();
+ let seed2 = hex::decode("c28135734876aff9ccf4f1d60df8d19a0a38fd02085883f65fc608eb769a635d")
+ .unwrap();
+ 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);
+ }
+
#[test]
fn test_lock() {
lock();
@@ -446,7 +470,7 @@ mod tests {
let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
.unwrap();
- assert!(keystore::encrypt_and_store_seed(&seed, "password").is_ok());
+ assert!(encrypt_and_store_seed(&seed, "password").is_ok());
assert!(is_locked()); // still locked, it is only unlocked after unlock_bip39.
assert!(
block_on(keystore::unlock_bip39(
@@ -477,7 +501,7 @@ mod tests {
.unwrap();
bitbox02::memory::set_salt_root(mock_salt_root.as_slice().try_into().unwrap()).unwrap();
- assert!(keystore::encrypt_and_store_seed(&seed, "password").is_ok());
+ assert!(encrypt_and_store_seed(&seed, "password").is_ok());
lock();
// First call: unlock. The first one does a seed rentention (1 securechip event).
@@ -908,7 +932,7 @@ mod tests {
);
bitbox02::securechip::fake_event_counter_reset();
- assert!(keystore::encrypt_and_store_seed(seed, "foo").is_ok());
+ assert!(encrypt_and_store_seed(seed, "foo").is_ok());
assert_eq!(bitbox02::securechip::fake_event_counter(), 7);
assert!(is_locked());
@@ -1062,7 +1086,7 @@ mod tests {
// Can repeat until initialized - initialized means backup has been created.
for _ in 0..2 {
- assert!(keystore::encrypt_and_store_seed(&seed[..seed_size], "foo").is_ok());
+ 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]);
@@ -1088,7 +1112,7 @@ mod tests {
// Can't store new seed once initialized.
bitbox02::memory::set_initialized().unwrap();
assert!(matches!(
- keystore::encrypt_and_store_seed(&seed[..seed_size], "foo"),
+ encrypt_and_store_seed(&seed[..seed_size], "foo"),
Err(Error::Memory)
));
}
diff --git a/src/rust/bitbox02-rust/src/workflow/unlock.rs b/src/rust/bitbox02-rust/src/workflow/unlock.rs
index 70ecc29..3d5f57d 100644
--- a/src/rust/bitbox02-rust/src/workflow/unlock.rs
+++ b/src/rust/bitbox02-rust/src/workflow/unlock.rs
@@ -196,7 +196,7 @@ mod tests {
mock_memory();
// Set up an initialized wallet with password
- bitbox02::keystore::encrypt_and_store_seed(
+ crate::keystore::encrypt_and_store_seed(
hex::decode("c7940c13479b8d9a6498f4e50d5a42e0d617bc8e8ac9f2b8cecf97e94c2b035c")
.unwrap()
.as_slice(),
diff --git a/src/rust/bitbox02/src/keystore.rs b/src/rust/bitbox02/src/keystore.rs
index 1c4e9ac..a14f924 100644
--- a/src/rust/bitbox02/src/keystore.rs
+++ b/src/rust/bitbox02/src/keystore.rs
@@ -286,7 +286,7 @@ pub fn bip39_mnemonic_to_seed(mnemonic: &str) -> Result<zeroize::Zeroizing<Vec<u
Ok(zeroize::Zeroizing::new(seed[..seed_len].to_vec()))
}
-pub fn encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error> {
+pub fn _encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error> {
match unsafe {
bitbox02_sys::keystore_encrypt_and_store_seed(
seed.as_ptr(),
@@ -454,7 +454,7 @@ mod tests {
let secp = secp256k1::Secp256k1::new();
assert!(root_fingerprint().is_err());
- assert!(encrypt_and_store_seed(&seed, "password").is_ok());
+ assert!(_encrypt_and_store_seed(&seed, "password").is_ok());
assert!(root_fingerprint().is_err());
// Incorrect seed passed
assert!(
@@ -558,22 +558,4 @@ mod tests {
assert_eq!(decrypted.as_slice(), &expected_seed[..size]);
}
}
-
- // This tests that you can create a keystore, unlock it, and then do this again. This is an
- // expected workflow for when the wallet setup process is restarted after seeding and unlocking,
- // but before creating a backup, in which case a new seed is created.
- #[test]
- fn test_create_and_unlock_twice() {
- mock_memory();
- _lock();
-
- let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
- .unwrap();
- let seed2 = hex::decode("c28135734876aff9ccf4f1d60df8d19a0a38fd02085883f65fc608eb769a635d")
- .unwrap();
- 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);
- }
}
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.