Skip to content

Commit 5c0907f

Browse files
committed
keystore: use bitcoin_hashes for hmac
We use both RustCrypto and bitcoin_hashes for hmac, but the latter is much harder to remove, as it's in bip39, bip32, etc, so we prefer this over RustCrypto.
1 parent 021f54e commit 5c0907f

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/rust/bitbox02-rust/src/keystore.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ use bitbox02::keystore;
2323

2424
use util::bip32::HARDENED;
2525

26-
use crate::hash::Sha512;
2726
use crate::secp256k1::SECP256K1;
2827

29-
use hmac::{Mac, SimpleHmac, digest::FixedOutput};
28+
use bitcoin::hashes::{Hash, HashEngine, Hmac, HmacEngine, sha512};
3029

3130
/// Returns the keystore's seed encoded as a BIP-39 mnemonic.
3231
pub fn get_bip39_mnemonic() -> Result<zeroize::Zeroizing<String>, ()> {
@@ -98,12 +97,12 @@ pub fn root_fingerprint() -> Result<Vec<u8>, ()> {
9897

9998
fn bip85_entropy(keypath: &[u32]) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
10099
let priv_key = secp256k1_get_private_key_twice(keypath)?;
101-
let mut mac = SimpleHmac::<Sha512>::new_from_slice(b"bip-entropy-from-k").unwrap();
102-
mac.update(&priv_key);
103-
let mut out = zeroize::Zeroizing::new(vec![0u8; 64]);
104-
let fixed_out: &mut [u8; 64] = out.as_mut_slice().try_into().unwrap();
105-
mac.finalize_into(fixed_out.into());
106-
Ok(out)
100+
101+
let mut engine = HmacEngine::<sha512::Hash>::new(b"bip-entropy-from-k");
102+
engine.input(&priv_key);
103+
Ok(zeroize::Zeroizing::new(
104+
Hmac::from_engine(engine).to_byte_array().to_vec(),
105+
))
107106
}
108107

109108
/// Computes a BIP39 mnemonic according to BIP-85:

0 commit comments

Comments
 (0)