rust: prepare crypto callers for upgrades
What changed, and why it matters
This commit is a routine code cleanup in the BitBox02 firmware's Rust code. It changes how cryptographic hashing functions are called so the project can upgrade a dependency later without breaking anything. There is no indication of a security bug being fixed or introduced.
No security action required. Review as normal refactoring; verify the dependency upgrade that follows preserves the same hash outputs and behavior.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors callers of the digest/blake2/sha2/hmac crates to use APIs that are compatible with both the current and upcoming versions of those dependencies. Changes include switching from Blake2bVar with VariableOutput to fixed-output Blake2b<U28>/Blake2b<U20>/Blake2b<U32>, replacing FixedOutput::finalize_into(hasher, result.as_mut_slice().into()) with (&mut **result).into(), changing mac.finalize_reset() to mac.finalize() in a single-use backup MAC context, and adding a known P-256 test vector. The commit message explicitly frames these as behavior-preserving adaptations to be reviewed separately from the actual dependency upgrade.
Changed components
src/rust/bitbox-core-utils/src/random.rssrc/rust/bitbox-core-utils/src/salt.rssrc/rust/bitbox02-rust/src/backup.rssrc/rust/bitbox02-rust/src/hww/api/cardano/address.rssrc/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction.rssrc/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction/cbor.rssrc/rust/util/src/p256.rsInspect captured patch +25 / −31
### src/rust/bitbox-core-utils/src/random.rs
@@ -27,7 +27,7 @@ pub fn random_32_bytes_with_mixin(
let mut result = Box::new(zeroize::Zeroizing::new([0u8; 32]));
let mut hasher = sha2::Sha256::new();
hasher.update(mixed.as_slice());
- FixedOutput::finalize_into(hasher, result.as_mut_slice().into());
+ FixedOutput::finalize_into(hasher, (&mut **result).into());
result
}
### src/rust/bitbox-core-utils/src/salt.rs
@@ -24,7 +24,7 @@ pub fn hash_data(
hasher.update(purpose.as_bytes());
hasher.update(data);
- FixedOutput::finalize_into(hasher, result.as_mut_slice().into());
+ FixedOutput::finalize_into(hasher, (&mut **result).into());
Ok(result)
}
### src/rust/bitbox02-rust/src/backup.rs
@@ -58,7 +58,7 @@ fn padded_seed(seed: &[u8]) -> Zeroizing<[u8; 32]> {
pub fn id(seed: &[u8]) -> String {
let mut mac = Hmac::<Sha256>::new_from_slice(b"backup").unwrap();
mac.update(padded_seed(seed).as_ref());
- hex::encode(mac.finalize_reset().into_bytes())
+ hex::encode(mac.finalize().into_bytes())
}
/// Replaces characters that cannot be displayed safely in legacy backup names with `?`.
### src/rust/bitbox02-rust/src/hww/api/cardano/address.rs
@@ -15,10 +15,7 @@ use pb::cardano_script_config::Config;
use bitcoin::bech32;
-use blake2::{
- Blake2bVar,
- digest::{Update, VariableOutput},
-};
+use blake2::{Blake2b, Digest, digest::consts::U28};
use super::params;
@@ -307,11 +304,9 @@ pub async fn pubkey_hash_at_keypath(
) -> Result<[u8; ADDRESS_HASH_SIZE], ()> {
let xpub = crate::keystore::ed25519::get_xpub_twice(hal, keypath).await?;
let pubkey_bytes = xpub.pubkey_bytes();
- let mut hasher = Blake2bVar::new(ADDRESS_HASH_SIZE).unwrap();
+ let mut hasher = Blake2b::<U28>::new();
hasher.update(pubkey_bytes);
- let mut out = [0u8; ADDRESS_HASH_SIZE];
- hasher.finalize_variable(&mut out).or(Err(()))?;
- Ok(out)
+ Ok(hasher.finalize().into())
}
/// See https://github.com/input-output-hk/cardano-ledger-specs/blob/d0aa86ded0b973b09b629e5aa62aa1e71364d088/eras/alonzo/test-suite/cddl-files/alonzo.cddl#L119-L127
### src/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction.rs
@@ -13,8 +13,8 @@ use alloc::vec::Vec;
use bitcoin::bech32;
use blake2::{
- Blake2bVar,
- digest::{Update, VariableOutput},
+ Blake2b, Digest,
+ digest::consts::{U20, U32},
};
use crate::hal::Ui;
@@ -98,11 +98,10 @@ async fn verify_slot(
/// Format an asset fingerprint according to CIP-14.
/// https://github.com/cardano-foundation/CIPs/blob/a2ef32d8a2b485fed7f6ffde2781dd58869ff511/CIP-0014/README.md
fn format_asset(policy_id: &[u8], asset_name: &[u8]) -> String {
- let mut hasher = Blake2bVar::new(20).unwrap();
+ let mut hasher = Blake2b::<U20>::new();
hasher.update(policy_id);
hasher.update(asset_name);
- let mut hash = [0u8; 20];
- hasher.finalize_variable(&mut hash).unwrap();
+ let hash: [u8; 20] = hasher.finalize().into();
bech32::encode::<bech32::Bech32>(bech32::Hrp::parse_unchecked("asset"), &hash).unwrap()
}
@@ -336,12 +335,10 @@ async fn _process(
hal.ui().status(&status, true).await;
let tx_body_hash: [u8; 32] = {
- let mut hasher = Blake2bVar::new(32).unwrap();
+ let mut hasher = Blake2b::<U32>::new();
cbor::encode_transaction_body(hal, request, cbor::HashedWriter::new(&mut hasher)).await?;
- let mut out = [0u8; 32];
- hasher.finalize_variable(&mut out).or(Err(Error::Generic))?;
- out
+ hasher.finalize().into()
};
signing_keypaths.sort_unstable();
### src/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction/cbor.rs
@@ -240,7 +240,7 @@ pub async fn encode_transaction_body<W: Write>(
mod tests {
use super::*;
use alloc::vec::Vec;
- use blake2::{Blake2bVar, digest::VariableOutput};
+ use blake2::{Blake2b, Digest, digest::consts::U32};
fn encode_something<W: Write>(
encoder: &mut Encoder<W>,
@@ -275,21 +275,17 @@ mod tests {
let mut encoder = Encoder::new(&mut cbor_encoded);
encode_something(&mut encoder).unwrap();
- let mut hasher = Blake2bVar::new(32).unwrap();
- hasher.update(&cbor_encoded);
- let mut out = [0u8; 32];
- hasher.finalize_variable(&mut out).unwrap();
- out
+ let mut hasher = Blake2b::<U32>::new();
+ Digest::update(&mut hasher, &cbor_encoded);
+ hasher.finalize().into()
};
// Now encode CBOR into the hasher directly and compare results.
- let hash = {
- let mut hasher = Blake2bVar::new(32).unwrap();
+ let hash: [u8; 32] = {
+ let mut hasher = Blake2b::<U32>::new();
let mut encoder = Encoder::new(HashedWriter::new(&mut hasher));
encode_something(&mut encoder).unwrap();
- let mut out = [0u8; 32];
- hasher.finalize_variable(&mut out).unwrap();
- out
+ hasher.finalize().into()
};
assert_eq!(hash, expected_hash);
}
### src/rust/util/src/p256.rs
@@ -49,6 +49,12 @@ mod tests {
unsafe { crate::bytes::rust_util_bytes(privkey.as_ptr(), privkey.len()) },
unsafe { crate::bytes::rust_util_bytes_mut(pubkey.as_mut_ptr(), pubkey.len()) },
);
+ assert_eq!(
+ pubkey,
+ hex_lit::hex!(
+ "84c23f7b11a2948784e39d85d29c92eaef97925bfbf6a5f5aec0e106011034695e1e439a99c70e6a6913206deac5894031ba0c4fcbb7ba3456f52958f30e27c3"
+ ),
+ );
}
#[test]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.