keystore: wrap and unit test _stretch_retained_seed_encryption_key in Rust
What changed, and why it matters
This commit is a routine code refactoring: it renames an internal-only C function so it can be called from Rust, adds a Rust wrapper, and includes unit tests. The actual key-stretching logic does not change. There is no security fix or vulnerability here.
No security action required; this is a test/refactoring commit. Continue normal review of any follow-up commit that actually replaces the C implementation with Rust.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change exposes the previously static C function _stretch_retained_seed_encryption_key as keystore_stretch_retained_seed_encryption_key, adds a corresponding Rust FFI binding and safe wrapper, and adds two Rust unit tests (success and salt-error cases). The implementation body in C remains identical; only visibility and naming changed to enable future Rust replacement and testing.
Changed components
src/keystore.csrc/keystore.hsrc/rust/bitbox02-rust/src/keystore.rssrc/rust/bitbox02-sys/build.rssrc/rust/bitbox02/src/keystore.rsInspect captured patch +74 / −5
diff --git a/src/keystore.c b/src/keystore.c
index 84fba92..6c0da48 100644
--- a/src/keystore.c
+++ b/src/keystore.c
@@ -63,7 +63,7 @@ static bool _validate_seed_length(size_t seed_len)
return seed_len == 16 || seed_len == 24 || seed_len == 32;
}
-USE_RESULT static keystore_error_t _stretch_retained_seed_encryption_key(
+USE_RESULT keystore_error_t keystore_stretch_retained_seed_encryption_key(
const uint8_t* encryption_key,
const char* purpose_in,
const char* purpose_out,
@@ -92,7 +92,7 @@ bool keystore_copy_seed(uint8_t* seed_out, size_t* length_out)
uint8_t retained_seed_encryption_key[32] = {0};
UTIL_CLEANUP_32(retained_seed_encryption_key);
- if (_stretch_retained_seed_encryption_key(
+ if (keystore_stretch_retained_seed_encryption_key(
_unstretched_retained_seed_encryption_key,
"keystore_retained_seed_access_in",
"keystore_retained_seed_access_out",
@@ -122,7 +122,7 @@ bool keystore_copy_bip39_seed(uint8_t* bip39_seed_out)
uint8_t retained_bip39_seed_encryption_key[32] = {0};
UTIL_CLEANUP_32(retained_bip39_seed_encryption_key);
- if (_stretch_retained_seed_encryption_key(
+ if (keystore_stretch_retained_seed_encryption_key(
_unstretched_retained_bip39_seed_encryption_key,
"keystore_retained_bip39_seed_access_in",
"keystore_retained_bip39_seed_access_out",
@@ -266,7 +266,7 @@ USE_RESULT static keystore_error_t _retain_seed(const uint8_t* seed, size_t seed
#endif
uint8_t retained_seed_encryption_key[32] = {0};
UTIL_CLEANUP_32(retained_seed_encryption_key);
- keystore_error_t result = _stretch_retained_seed_encryption_key(
+ keystore_error_t result = keystore_stretch_retained_seed_encryption_key(
_unstretched_retained_seed_encryption_key,
"keystore_retained_seed_access_in",
"keystore_retained_seed_access_out",
@@ -299,7 +299,7 @@ USE_RESULT static bool _retain_bip39_seed(const uint8_t* bip39_seed)
#endif
uint8_t retained_bip39_seed_encryption_key[32] = {0};
UTIL_CLEANUP_32(retained_bip39_seed_encryption_key);
- if (_stretch_retained_seed_encryption_key(
+ if (keystore_stretch_retained_seed_encryption_key(
_unstretched_retained_bip39_seed_encryption_key,
"keystore_retained_bip39_seed_access_in",
"keystore_retained_bip39_seed_access_out",
diff --git a/src/keystore.h b/src/keystore.h
index 7761f24..9fcf71c 100644
--- a/src/keystore.h
+++ b/src/keystore.h
@@ -62,6 +62,12 @@ USE_RESULT bool keystore_copy_seed(uint8_t* seed_out, size_t* length_out);
*/
USE_RESULT bool keystore_copy_bip39_seed(uint8_t* bip32_seed_out);
+USE_RESULT keystore_error_t keystore_stretch_retained_seed_encryption_key(
+ const uint8_t* encryption_key,
+ const char* purpose_in,
+ const char* purpose_out,
+ uint8_t* out);
+
/**
* Restores a seed. This also unlocks the keystore with this seed.
* @param[in] seed The seed that is to be restored.
diff --git a/src/rust/bitbox02-rust/src/keystore.rs b/src/rust/bitbox02-rust/src/keystore.rs
index d962cdb..7cf5a96 100644
--- a/src/rust/bitbox02-rust/src/keystore.rs
+++ b/src/rust/bitbox02-rust/src/keystore.rs
@@ -131,6 +131,16 @@ pub fn root_fingerprint() -> Result<Vec<u8>, ()> {
keystore::root_fingerprint()
}
+/// Stretches the given encryption_key using the securechip. The resulting key is used to encrypt
+/// the retained seed or bip39 seed.
+pub fn stretch_retained_seed_encryption_key(
+ encryption_key: &[u8; 32],
+ purpose_in: &str,
+ purpose_out: &str,
+) -> Result<zeroize::Zeroizing<Vec<u8>>, keystore::Error> {
+ keystore::stretch_retained_seed_encryption_key(encryption_key, purpose_in, purpose_out)
+}
+
fn bip85_entropy(keypath: &[u32]) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
let priv_key = secp256k1_get_private_key_twice(keypath)?;
@@ -455,6 +465,37 @@ mod tests {
assert_eq!(root_fingerprint(), Err(()));
}
+ #[test]
+ fn test_stretch_retained_seed_encryption_key_success() {
+ mock_memory();
+ let salt_root = hex!("0000000000000000111111111111111122222222222222223333333333333333");
+ bitbox02::memory::set_salt_root(&salt_root).unwrap();
+
+ let encryption_key =
+ hex!("00112233445566778899aabbccddeeff112233445566778899aabbccddeeff00");
+
+ let stretched = stretch_retained_seed_encryption_key(
+ &encryption_key,
+ "keystore_retained_seed_access_in",
+ "keystore_retained_seed_access_out",
+ )
+ .unwrap();
+
+ let expected = hex!("b6b20683810aee16b5603ae95d14eaae5ae2c8d9df9b66e1b67c698e627bb208");
+ assert_eq!(stretched.as_slice(), expected.as_slice());
+ }
+
+ #[test]
+ fn test_stretch_retained_seed_encryption_key_salt_error() {
+ mock_memory();
+ bitbox02::memory::set_salt_root(&[0xffu8; 32]).unwrap();
+
+ let encryption_key = [0u8; 32];
+ let result =
+ stretch_retained_seed_encryption_key(&encryption_key, "purpose_in", "purpose_out");
+ assert!(matches!(result, Err(keystore::Error::Salt)));
+ }
+
#[test]
fn test_bip85_bip39() {
keystore::lock();
diff --git a/src/rust/bitbox02-sys/build.rs b/src/rust/bitbox02-sys/build.rs
index 22f1879..cf0d9f9 100644
--- a/src/rust/bitbox02-sys/build.rs
+++ b/src/rust/bitbox02-sys/build.rs
@@ -77,6 +77,7 @@ const ALLOWLIST_FNS: &[&str] = &[
"keystore_get_bip39_word",
"keystore_is_locked",
"keystore_lock",
+ "keystore_stretch_retained_seed_encryption_key",
"keystore_mock_unlocked",
"keystore_secp256k1_nonce_commit",
"keystore_secp256k1_sign",
diff --git a/src/rust/bitbox02/src/keystore.rs b/src/rust/bitbox02/src/keystore.rs
index a0f5b23..10807dc 100644
--- a/src/rust/bitbox02/src/keystore.rs
+++ b/src/rust/bitbox02/src/keystore.rs
@@ -213,6 +213,27 @@ pub fn copy_bip39_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
}
}
+pub fn stretch_retained_seed_encryption_key(
+ encryption_key: &[u8; 32],
+ purpose_in: &str,
+ purpose_out: &str,
+) -> Result<zeroize::Zeroizing<Vec<u8>>, Error> {
+ let purpose_in_c = crate::util::str_to_cstr_vec(purpose_in).unwrap();
+ let purpose_out_c = crate::util::str_to_cstr_vec(purpose_out).unwrap();
+ let mut out = zeroize::Zeroizing::new(vec![0u8; 32]);
+ match unsafe {
+ bitbox02_sys::keystore_stretch_retained_seed_encryption_key(
+ encryption_key.as_ptr(),
+ purpose_in_c.as_ptr().cast(),
+ purpose_out_c.as_ptr().cast(),
+ out.as_mut_ptr(),
+ )
+ } {
+ keystore_error_t::KEYSTORE_OK => Ok(out),
+ err => Err(err.into()),
+ }
+}
+
pub fn bip39_mnemonic_from_seed(seed: &[u8]) -> Result<zeroize::Zeroizing<String>, ()> {
let mnemonic = bip39::Mnemonic::from_entropy(seed).map_err(|_| ())?;
Ok(zeroize::Zeroizing::new(mnemonic.to_string()))
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.