keystore: port keystore_get_u2f_seed to Rust
What changed, and why it matters
This commit rewrites a small function that derives the U2F security seed from the wallet seed, moving it from C to Rust. The change is a routine code port, not a security fix. It keeps the same cryptographic calculation and the same requirement that the device be unlocked. There is no indication this addresses a known vulnerability.
No security action required. Treat as a normal refactoring commit. If reviewing, verify the Rust HMAC uses the exact same message (`b"u2f\0"`) and that `copy_bip39_seed()` preserves the locked-state check, both of which the diff confirms.
Security signals we found
Port of existing cryptographic derivation to a memory-safe language (Rust)
Continues to require device unlock before accessing BIP39 seed
Output is wrapped in `zeroize::Zeroizing` to reduce sensitive-data lifetime in Rust memory
No change to algorithm, key material, or access control semantics
Evidence from the diff
The patch ports keystore_get_u2f_seed() from C to Rust. The C implementation checked keystore_is_locked(), copied the BIP39 seed, and computed HMAC-SHA256(key=bip39_seed, msg="u2f\0"). The Rust implementation uses copy_bip39_seed() (which itself enforces the unlocked check) and the same HMAC-SHA256 with the same null-terminated message for backwards compatibility. The C call site in src/u2f.c is updated to call the new Rust FFI export rust_keystore_get_u2f_seed(). Bindings and tests are updated accordingly.
Changed components
src/keystore.csrc/keystore.hsrc/rust/bitbox02-rust/src/keystore.rssrc/rust/bitbox02-sys/build.rssrc/rust/bitbox02/src/keystore.rssrc/u2f.cInspect captured patch +27 / −35
diff --git a/src/keystore.c b/src/keystore.c
index 4219d59..0e68613 100644
--- a/src/keystore.c
+++ b/src/keystore.c
@@ -532,21 +532,6 @@ bool keystore_secp256k1_sign(
return true;
}
-bool keystore_get_u2f_seed(uint8_t* seed_out)
-{
- if (keystore_is_locked()) {
- return false;
- }
- uint8_t bip39_seed[64] = {0};
- UTIL_CLEANUP_64(bip39_seed);
- if (!keystore_copy_bip39_seed(bip39_seed)) {
- return false;
- }
- const uint8_t message[] = "u2f";
- rust_hmac_sha256(bip39_seed, 64, message, sizeof(message), seed_out);
- return true;
-}
-
#ifdef TESTING
void keystore_mock_unlocked(const uint8_t* seed, size_t seed_len, const uint8_t* bip39_seed)
{
diff --git a/src/keystore.h b/src/keystore.h
index 655a714..2093321 100644
--- a/src/keystore.h
+++ b/src/keystore.h
@@ -179,13 +179,6 @@ USE_RESULT bool keystore_secp256k1_sign(
uint8_t* sig_compact_out,
int* recid_out);
-/**
- * Get the seed to be used for u2f
- * @param seed_out Buffer for seed, must be KEYSTORE_U2F_SEED_LENGTH
- * @return true if succes
- */
-USE_RESULT bool keystore_get_u2f_seed(uint8_t* seed_out);
-
#ifdef TESTING
/**
* convenience to mock the keystore state (locked, seed) in tests.
diff --git a/src/rust/bitbox02-rust/src/keystore.rs b/src/rust/bitbox02-rust/src/keystore.rs
index 016cde7..8ab758f 100644
--- a/src/rust/bitbox02-rust/src/keystore.rs
+++ b/src/rust/bitbox02-rust/src/keystore.rs
@@ -183,6 +183,19 @@ pub fn secp256k1_schnorr_sign(
Ok(sig.serialize())
}
+/// Get the seed to be used for u2f
+#[cfg(feature = "app-u2f")]
+pub fn get_u2f_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
+ let bip39_seed = keystore::copy_bip39_seed()?;
+
+ let mut engine = HmacEngine::<bitcoin::hashes::sha256::Hash>::new(&bip39_seed);
+ // Null-terminator for backwards compatibility from the time when this was coded in C.
+ engine.input(b"u2f\0");
+ Ok(zeroize::Zeroizing::new(
+ Hmac::from_engine(engine).to_byte_array().to_vec(),
+ ))
+}
+
/// # Safety
///
/// keypath pointer has point to a buffer of length `keypath_len` uint32 elements.
@@ -202,6 +215,18 @@ pub unsafe extern "C" fn rust_secp256k1_get_private_key(
}
}
+#[cfg(feature = "app-u2f")]
+#[unsafe(no_mangle)]
+pub extern "C" fn rust_keystore_get_u2f_seed(mut seed_out: util::bytes::BytesMut) -> bool {
+ match get_u2f_seed() {
+ Ok(seed) => {
+ seed_out.as_mut().copy_from_slice(&seed);
+ true
+ }
+ Err(_) => false,
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -506,7 +531,7 @@ mod tests {
test.expected_xpub,
);
assert_eq!(
- hex::encode(keystore::get_u2f_seed().unwrap()),
+ hex::encode(get_u2f_seed().unwrap()),
test.expected_u2f_seed_hex,
);
}
diff --git a/src/rust/bitbox02-sys/build.rs b/src/rust/bitbox02-sys/build.rs
index 5e3b37f..94fdb9a 100644
--- a/src/rust/bitbox02-sys/build.rs
+++ b/src/rust/bitbox02-sys/build.rs
@@ -73,7 +73,6 @@ const ALLOWLIST_FNS: &[&str] = &[
"keystore_create_and_store_seed",
"keystore_encrypt_and_store_seed",
"keystore_get_bip39_word",
- "keystore_get_u2f_seed",
"keystore_is_locked",
"keystore_lock",
"keystore_mock_unlocked",
diff --git a/src/rust/bitbox02/src/keystore.rs b/src/rust/bitbox02/src/keystore.rs
index 44cb4d6..8dd74a0 100644
--- a/src/rust/bitbox02/src/keystore.rs
+++ b/src/rust/bitbox02/src/keystore.rs
@@ -220,16 +220,6 @@ pub fn encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error>
}
}
-// Currently only used in the functional tests below.
-#[cfg(feature = "testing")]
-pub fn get_u2f_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
- let mut seed = zeroize::Zeroizing::new([0u8; 32].to_vec());
- match unsafe { bitbox02_sys::keystore_get_u2f_seed(seed.as_mut_ptr()) } {
- true => Ok(seed),
- false => Err(()),
- }
-}
-
#[cfg(test)]
mod tests {
use super::*;
diff --git a/src/u2f.c b/src/u2f.c
index 140304f..70f37c2 100644
--- a/src/u2f.c
+++ b/src/u2f.c
@@ -257,7 +257,7 @@ USE_RESULT static bool _keyhandle_gen(
uint8_t hmac_in[U2F_APPID_SIZE + U2F_NONCE_LENGTH];
uint8_t seed[32];
UTIL_CLEANUP_32(seed);
- if (!keystore_get_u2f_seed(seed)) {
+ if (!rust_keystore_get_u2f_seed(rust_util_bytes_mut(seed, sizeof(seed)))) {
return false;
}
Why this scored 18/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.