What changed, and why it matters
This commit rewrites a small security function that checks whether a stored wallet seed was saved correctly. The old C version manually decrypted the stored seed and compared it to the expected seed, then wiped the temporary decrypted copy from memory. The new Rust version does the same job using a Rust AES/HMAC helper and a built-in slice comparison. The change is mostly a language port, but it removes the explicit memory-zeroing step and slightly changes how errors are handled.
Treat as a routine refactor but verify two things in review: (1) that `bitbox_aes::decrypt_with_hmac` and the Rust comparison path zeroize the decrypted seed buffer before it is freed, matching the C code's explicit `util_zero`; and (2) that changing the underflow abort to a silent false return does not allow a corrupted/short encrypted seed to be handled as a failed verification rather than a fatal error. No immediate exploit is evident from the diff alone.
Security signals we found
Sensitive plaintext (decrypted seed) is compared and then dropped; diff does not show explicit zeroization that was present in C
Abort-on-underflow behavior in C is replaced by silent false return in Rust, changing failure semantics
Memory function `get_encrypted_seed_and_hmac` exposed outside test configuration
Language-port of a seed-verification routine that sits on the critical path of seed storage
Evidence from the diff
The C _verify_seed is removed and replaced by rust_keystore_verify_seed -> verify_seed in Rust. The Rust implementation validates the 32-byte encryption key, fetches the encrypted seed+HMAC from memory, calls bitbox_aes::decrypt_with_hmac, and compares the decrypted bytes to expected_seed using == on slices. Notable differences from the C version: (1) the decrypted buffer is a local Vec/Zeroizing? — the diff does not show explicit zeroization of the decrypted plaintext after comparison; (2) the C code had explicit util_zero calls on the decrypted buffer before each false/return path; (3) the Rust code returns false on any error rather than calling Abort() on an underflow/zero size condition; (4) get_encrypted_seed_and_hmac is no longer gated by #[cfg(feature = "testing")], making it available in production builds. The bitbox-aes crate moves from a dev-dependency to a normal dependency.
Changed components
src/keystore.csrc/rust/bitbox02-rust/src/keystore.rssrc/rust/bitbox02/src/memory.rssrc/rust/bitbox02-rust/Cargo.tomlInspect captured patch +29 / −38
diff --git a/src/keystore.c b/src/keystore.c
index d2b4fa1..66fe60a 100644
--- a/src/keystore.c
+++ b/src/keystore.c
@@ -192,39 +192,6 @@ static keystore_error_t _get_and_decrypt_seed(
return KEYSTORE_OK;
}
-static bool _verify_seed(
- const uint8_t* encryption_key,
- const uint8_t* expected_seed,
- size_t expected_seed_len)
-{
- uint8_t encrypted_seed_and_hmac[96];
- UTIL_CLEANUP_32(encrypted_seed_and_hmac);
- uint8_t encrypted_len;
- if (!memory_get_encrypted_seed_and_hmac(encrypted_seed_and_hmac, &encrypted_len)) {
- return false;
- }
- if (encrypted_len < 49) {
- Abort("_verify_seed: underflow / zero size");
- }
- size_t decrypted_len = encrypted_len - 48;
- uint8_t decrypted[decrypted_len];
- bool password_correct = cipher_aes_hmac_decrypt(
- encrypted_seed_and_hmac, encrypted_len, decrypted, &decrypted_len, encryption_key);
- if (!password_correct) {
- return false;
- }
- if (expected_seed_len != decrypted_len) {
- util_zero(decrypted, sizeof(decrypted));
- return false;
- }
- if (!MEMEQ(expected_seed, decrypted, expected_seed_len)) {
- util_zero(decrypted, sizeof(decrypted));
- return false;
- }
- util_zero(decrypted, sizeof(decrypted));
- return true;
-}
-
static keystore_error_t _hash_seed(const uint8_t* seed, size_t seed_len, uint8_t* out)
{
uint8_t salted_key[32] = {0};
@@ -362,7 +329,9 @@ keystore_error_t keystore_encrypt_and_store_seed(
if (!memory_set_encrypted_seed_and_hmac(encrypted_seed, encrypted_seed_len_u8)) {
return KEYSTORE_ERR_MEMORY;
}
- if (!_verify_seed(secret, seed, seed_length)) {
+ if (!rust_keystore_verify_seed(
+ rust_util_bytes(secret, sizeof(secret)),
+ rust_util_bytes(seed, seed_length))) {
if (!memory_reset_hww()) {
return KEYSTORE_ERR_MEMORY;
}
diff --git a/src/rust/bitbox02-rust/Cargo.toml b/src/rust/bitbox02-rust/Cargo.toml
index 7e312b7..9e60548 100644
--- a/src/rust/bitbox02-rust/Cargo.toml
+++ b/src/rust/bitbox02-rust/Cargo.toml
@@ -33,6 +33,7 @@ erc20_params = { path = "../erc20_params", optional = true }
binascii = { version = "0.1.4", default-features = false, features = ["encode"] }
bitbox02-noise = {path = "../bitbox02-noise"}
streaming-silent-payments = { path = "../streaming-silent-payments", optional = true }
+bitbox-aes = { path = "../bitbox-aes" }
hex = { workspace = true }
sha2 = { workspace = true }
sha3 = { workspace = true, optional = true }
@@ -65,9 +66,6 @@ version = "0.13.1"
default-features = false
features = ["derive"]
-[dev-dependencies]
-bitbox-aes = { path = "../bitbox-aes" }
-
[features]
ed25519 = [
"dep:bip32-ed25519",
diff --git a/src/rust/bitbox02-rust/src/keystore.rs b/src/rust/bitbox02-rust/src/keystore.rs
index 2b68e55..3dbf55b 100644
--- a/src/rust/bitbox02-rust/src/keystore.rs
+++ b/src/rust/bitbox02-rust/src/keystore.rs
@@ -46,6 +46,23 @@ pub fn is_locked() -> bool {
keystore::_is_locked()
}
+fn verify_seed(encryption_key: &[u8], expected_seed: &[u8]) -> bool {
+ if encryption_key.len() != 32 {
+ return false;
+ }
+
+ let cipher = match bitbox02::memory::get_encrypted_seed_and_hmac() {
+ Ok(cipher) => cipher,
+ Err(_) => return false,
+ };
+ let decrypted = match bitbox_aes::decrypt_with_hmac(encryption_key, &cipher) {
+ Ok(decrypted) => decrypted,
+ Err(_) => return false,
+ };
+
+ decrypted.as_slice() == expected_seed
+}
+
pub fn unlock(password: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, Error> {
keystore::_unlock(password)
}
@@ -291,6 +308,14 @@ pub unsafe extern "C" fn rust_keystore_stretch_retained_seed_encryption_key(
}
}
+#[unsafe(no_mangle)]
+pub extern "C" fn rust_keystore_verify_seed(
+ encryption_key: util::bytes::Bytes,
+ expected_seed: util::bytes::Bytes,
+) -> bool {
+ verify_seed(encryption_key.as_ref(), expected_seed.as_ref())
+}
+
fn bip85_entropy(keypath: &[u32]) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
let priv_key = secp256k1_get_private_key_twice(keypath)?;
diff --git a/src/rust/bitbox02/src/memory.rs b/src/rust/bitbox02/src/memory.rs
index ff52f68..0a9232b 100644
--- a/src/rust/bitbox02/src/memory.rs
+++ b/src/rust/bitbox02/src/memory.rs
@@ -98,7 +98,6 @@ pub fn get_attestation_pubkey_and_certificate(
}
}
-#[cfg(feature = "testing")]
pub fn get_encrypted_seed_and_hmac() -> Result<alloc::vec::Vec<u8>, ()> {
let mut out = vec![0u8; 96];
let mut len = 0u8;
Why this scored 24/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.