keystore: port get_ed25519_seed to Rust
What changed, and why it matters
This commit rewrites an existing function that derives a Cardano/ed25519 seed from the wallet's BIP39 seed. The old C implementation is removed and replaced by a Rust implementation that performs the same cryptographic steps and uses the same test vectors. There is no indication of a security bug being fixed; it appears to be a routine language port.
No security action required. Treat as a normal refactoring review; verify the Rust implementation matches the C implementation and that the duplicated test vectors still pass.
Security signals we found
No security-relevant signal: language port of an existing cryptographic routine
Same algorithm, same test vectors, same key string "ed25519 seed" retained
Sensitive material still zeroized via `zeroize::Zeroizing`
No bounds-check or input-validation changes observed
Evidence from the diff
The change ports keystore_get_ed25519_seed() from C to Rust. The C code used rust_hmac_sha512/rust_hmac_sha256 and manual clamping of the expanded Ed25519 private key. The new Rust code in ed25519.rs uses bitcoin::hashes HMAC-SHA-512/256, copies the same clamping logic (seed_out[0] &= 248; seed_out[31] &= 127; seed_out[31] |= 64), and computes the chain code with the same 0x01-prefixed message. The FFI allowlist and Rust wrapper for the C function are removed. The unit tests that were duplicated in ed25519.rs are removed from bitbox02/src/keystore.rs.
Changed components
src/keystore.csrc/keystore.hsrc/rust/bitbox02-rust/src/keystore/ed25519.rssrc/rust/bitbox02-sys/build.rssrc/rust/bitbox02/src/keystore.rsInspect captured patch +38 / −90
diff --git a/src/keystore.c b/src/keystore.c
index 7f9983d..4219d59 100644
--- a/src/keystore.c
+++ b/src/keystore.c
@@ -547,36 +547,6 @@ bool keystore_get_u2f_seed(uint8_t* seed_out)
return true;
}
-bool keystore_get_ed25519_seed(uint8_t* seed_out)
-{
- uint8_t bip39_seed[64] = {0};
- UTIL_CLEANUP_64(bip39_seed);
- if (!keystore_copy_bip39_seed(bip39_seed)) {
- return false;
- }
-
- const uint8_t key[] = "ed25519 seed";
-
- // Derive a 64 byte expanded ed25519 private key and put it into seed_out.
- memcpy(seed_out, bip39_seed, 64);
- do {
- rust_hmac_sha512(key, sizeof(key), seed_out, 64, seed_out);
- } while (seed_out[31] & 0x20);
-
- seed_out[0] &= 248;
- seed_out[31] &= 127;
- seed_out[31] |= 64;
-
- // Compute chain code and put it into seed_out at offset 64.
- uint8_t message[65] = {0};
- message[0] = 0x01;
- memcpy(&message[1], bip39_seed, 64);
- util_zero(bip39_seed, sizeof(bip39_seed));
- rust_hmac_sha256(key, sizeof(key), message, sizeof(message), &seed_out[64]);
- util_zero(message, sizeof(message));
- 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 a2c50f0..655a714 100644
--- a/src/keystore.h
+++ b/src/keystore.h
@@ -186,16 +186,6 @@ USE_RESULT bool keystore_secp256k1_sign(
*/
USE_RESULT bool keystore_get_u2f_seed(uint8_t* seed_out);
-/**
- * Get the seed to be used for ed25519 applications such as Cardano. The output is the root key to
- * BIP32-ED25519.
- * This implements a derivation compatible with Ledger according to
- * https://github.com/LedgerHQ/orakolo/blob/0b2d5e669ec61df9a824df9fa1a363060116b490/src/python/orakolo/HDEd25519.py.
- * @param seed_out Buffer for the seed. Must be 96 bytes. It will contain a 64 byte expanded
- * ed25519 private key followed by a 32 byte chain code.
- */
-USE_RESULT bool keystore_get_ed25519_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/ed25519.rs b/src/rust/bitbox02-rust/src/keystore/ed25519.rs
index da6b49f..c61e39c 100644
--- a/src/rust/bitbox02-rust/src/keystore/ed25519.rs
+++ b/src/rust/bitbox02-rust/src/keystore/ed25519.rs
@@ -17,8 +17,45 @@ use alloc::vec::Vec;
use crate::hash::Sha512;
use bip32_ed25519::{ED25519_EXPANDED_SECRET_KEY_SIZE, Xprv, Xpub};
+use bitcoin::hashes::{Hash, HashEngine, Hmac, HmacEngine, sha256, sha512};
+
+fn hmac_sha512(key: &[u8], msg: &[u8]) -> [u8; 64] {
+ let mut engine = HmacEngine::<sha512::Hash>::new(key);
+ engine.input(msg);
+ Hmac::from_engine(engine).to_byte_array()
+}
+
+/// Get the seed to be used for ed25519 applications such as Cardano. The output is the root key to
+/// BIP32-ED25519.
+/// This implements a derivation compatible with Ledger according to
+/// https://github.com/LedgerHQ/orakolo/blob/0b2d5e669ec61df9a824df9fa1a363060116b490/src/python/orakolo/HDEd25519.py.
+/// Returns 96 bytes. It will contain a 64 byte expanded ed25519 private key followed by a 32 byte chain code.
fn get_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
- bitbox02::keystore::get_ed25519_seed()
+ let bip39_seed = bitbox02::keystore::copy_bip39_seed()?;
+ let mut seed_out = zeroize::Zeroizing::new(vec![0u8; 96]);
+ let first64: &mut [u8] = &mut seed_out.as_mut_slice()[..64];
+ first64.copy_from_slice(&bip39_seed);
+
+ let key = b"ed25519 seed";
+
+ loop {
+ first64.copy_from_slice(&hmac_sha512(key, first64));
+ if first64[31] & 0x20 == 0 {
+ break;
+ }
+ }
+
+ seed_out[0] &= 248;
+ seed_out[31] &= 127;
+ seed_out[31] |= 64;
+
+ // Compute chain code and put it into seed_out at offset 64.
+ let mut engine = HmacEngine::<sha256::Hash>::new(key);
+ engine.input(&[0x01]);
+ engine.input(&bip39_seed);
+ let hmac_result: [u8; 32] = Hmac::from_engine(engine).to_byte_array();
+ seed_out[64..].copy_from_slice(&hmac_result);
+ Ok(seed_out)
}
fn get_xprv(keypath: &[u32]) -> Result<Xprv<Sha512>, ()> {
diff --git a/src/rust/bitbox02-sys/build.rs b/src/rust/bitbox02-sys/build.rs
index d6d8f50..eb8bae4 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_ed25519_seed",
"keystore_get_u2f_seed",
"keystore_is_locked",
"keystore_lock",
diff --git a/src/rust/bitbox02/src/keystore.rs b/src/rust/bitbox02/src/keystore.rs
index 86a88c3..b4c10d6 100644
--- a/src/rust/bitbox02/src/keystore.rs
+++ b/src/rust/bitbox02/src/keystore.rs
@@ -220,14 +220,6 @@ pub fn encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error>
}
}
-pub fn get_ed25519_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
- let mut seed = zeroize::Zeroizing::new([0u8; 96].to_vec());
- match unsafe { bitbox02_sys::keystore_get_ed25519_seed(seed.as_mut_ptr()) } {
- true => Ok(seed),
- false => Err(()),
- }
-}
-
// Currently only used in the functional tests below.
#[cfg(feature = "testing")]
pub fn get_u2f_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
@@ -371,46 +363,6 @@ mod tests {
);
}
- #[test]
- fn test_get_ed25519_seed() {
- // No seed on a locked keystore.
- lock();
- assert!(get_ed25519_seed().is_err());
-
- // Test vectors taken from:
- // https://github.com/cardano-foundation/CIPs/blob/6c249ef48f8f5b32efc0ec768fadf4321f3173f2/CIP-0003/Ledger.md#test-vectors
- // See also: https://github.com/cardano-foundation/CIPs/pull/132
-
- mock_unlocked_using_mnemonic(
- "recall grace sport punch exhibit mad harbor stand obey short width stem awkward used stairs wool ugly trap season stove worth toward congress jaguar",
- "",
- );
- assert_eq!(
- hex::encode(get_ed25519_seed().unwrap()),
- "a08cf85b564ecf3b947d8d4321fb96d70ee7bb760877e371899b14e2ccf88658104b884682b57efd97decbb318a45c05a527b9cc5c2f64f7352935a049ceea60680d52308194ccef2a18e6812b452a5815fbd7f5babc083856919aaf668fe7e4",
- );
-
- // Multiple loop iterations.
-
- mock_unlocked_using_mnemonic(
- "correct cherry mammal bubble want mandate polar hazard crater better craft exotic choice fun tourist census gap lottery neglect address glow carry old business",
- "",
- );
- assert_eq!(
- hex::encode(get_ed25519_seed().unwrap()),
- "587c6774357ecbf840d4db6404ff7af016dace0400769751ad2abfc77b9a3844cc71702520ef1a4d1b68b91187787a9b8faab0a9bb6b160de541b6ee62469901fc0beda0975fe4763beabd83b7051a5fd5cbce5b88e82c4bbaca265014e524bd",
- );
-
- mock_unlocked_using_mnemonic(
- "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art",
- "foo",
- );
- assert_eq!(
- hex::encode(get_ed25519_seed().unwrap()),
- "f053a1e752de5c26197b60f032a4809f08bb3e5d90484fe42024be31efcba7578d914d3ff992e21652fee6a4d99f6091006938fac2c0c0f9d2de0ba64b754e92a4f3723f23472077aa4cd4dd8a8a175dba07ea1852dad1cf268c61a2679c3890",
- );
- }
-
#[test]
fn test_bip39_mnemonic_from_seed() {
// 12 words
Why this scored 12/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.