What changed, and why it matters
This commit is a code-size optimization for the BitBox02 hardware wallet's Cardano support. It replaces several fixed-output Blake2b hash implementations with a single small wrapper that can produce different output lengths at runtime. The change is framed as saving 472 bytes of firmware storage and includes unit tests verifying the new wrapper matches the old outputs exactly. There is no indication in the commit that this fixes a security bug or changes security behavior.
No security action required. Treat as a normal firmware-size optimization. If reviewing, confirm the new wrapper's test vectors match the replaced fixed-output Blake2b variants and that the `assert!` bounds (1..=64) are acceptable for all Cardano use cases.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change introduces a new Blake2b wrapper around Blake2bVarCore from the blake2 crate, supporting runtime-selected output lengths of 20, 28, and 32 bytes. It updates Cardano address hashing, asset fingerprinting, transaction body hashing, and a test to use this wrapper instead of fixed-output Blake2b<U20>, Blake2b<U28>, and Blake2b256. The wrapper uses the digest crate’s UpdateCore/VariableOutputCore buffered block API. Tests compare the wrapper’s output against the original fixed-output types across various input sizes and chunking patterns.
Changed components
src/rust/bitbox02-rust/src/hww/api/cardano/blake2b.rs (new wrapper)src/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.rsInspect captured patch +95 / −10
### src/rust/bitbox02-rust/src/hww/api/cardano.rs
@@ -4,6 +4,7 @@
compile_error!("Cardano code is being compiled even though the app-cardano feature is not enabled");
mod address;
+mod blake2b;
pub mod keypath;
mod params;
mod sign_transaction;
### src/rust/bitbox02-rust/src/hww/api/cardano/address.rs
@@ -15,7 +15,8 @@ use pb::cardano_script_config::Config;
use bitcoin::bech32;
-use blake2::{Blake2b, Digest, digest::consts::U28};
+use super::blake2b::Blake2b;
+use digest::Update;
use super::params;
@@ -304,9 +305,11 @@ 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 = Blake2b::<U28>::new();
+ let mut hasher = Blake2b::new(ADDRESS_HASH_SIZE);
hasher.update(pubkey_bytes);
- Ok(hasher.finalize().into())
+ let mut output = [0u8; ADDRESS_HASH_SIZE];
+ hasher.finalize(&mut output);
+ Ok(output)
}
/// 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/blake2b.rs
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: Apache-2.0
+
+use blake2::Blake2bVarCore;
+use digest::{
+ Output, Update,
+ block_api::{Buffer, UpdateCore, VariableOutputCore},
+};
+
+pub(super) struct Blake2b {
+ core: Blake2bVarCore,
+ buffer: Buffer<Blake2bVarCore>,
+ output_len: usize,
+}
+
+impl Blake2b {
+ pub(super) fn new(output_len: usize) -> Self {
+ assert!((1..=64).contains(&output_len));
+ Self {
+ core: Blake2bVarCore::new(output_len).unwrap(),
+ buffer: Buffer::<Blake2bVarCore>::default(),
+ output_len,
+ }
+ }
+
+ pub(super) fn finalize(mut self, output: &mut [u8]) {
+ assert_eq!(output.len(), self.output_len);
+ let mut full = Output::<Blake2bVarCore>::default();
+ self.core
+ .finalize_variable_core(&mut self.buffer, &mut full);
+ output.copy_from_slice(&full[..self.output_len]);
+ }
+}
+
+impl Update for Blake2b {
+ fn update(&mut self, data: &[u8]) {
+ let core = &mut self.core;
+ self.buffer
+ .digest_blocks(data, |blocks| core.update_blocks(blocks));
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use alloc::vec::Vec;
+ use blake2::digest::{
+ Digest,
+ consts::{U20, U28, U32},
+ };
+
+ #[test]
+ fn test_runtime_output_lengths_and_chunking() {
+ for input_len in [0, 1, 127, 128, 129, 255, 256, 257, 1024] {
+ let input: Vec<u8> = (0..input_len).map(|n| (n % 251) as u8).collect();
+ for (output_len, expected) in [
+ (20, blake2::Blake2b::<U20>::digest(&input).to_vec()),
+ (28, blake2::Blake2b::<U28>::digest(&input).to_vec()),
+ (32, blake2::Blake2b::<U32>::digest(&input).to_vec()),
+ ] {
+ for chunk_len in [1, 7, 127, 128, 129, 1025] {
+ let mut hasher = Blake2b::new(output_len);
+ hasher.update(&[]);
+ for chunk in input.chunks(chunk_len) {
+ hasher.update(chunk);
+ }
+ hasher.update(&[]);
+ let mut actual = alloc::vec![0u8; output_len];
+ hasher.finalize(&mut actual);
+ assert_eq!(actual, expected);
+ }
+ }
+ }
+ }
+}
### src/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction.rs
@@ -12,7 +12,8 @@ use alloc::vec::Vec;
use bitcoin::bech32;
-use blake2::{Blake2b, Blake2b256, Digest, digest::consts::U20};
+use super::blake2b::Blake2b;
+use digest::Update;
use crate::hal::Ui;
use crate::i18n::I18n as _;
@@ -95,10 +96,11 @@ 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 = Blake2b::<U20>::new();
+ let mut hasher = Blake2b::new(20);
hasher.update(policy_id);
hasher.update(asset_name);
- let hash: [u8; 20] = hasher.finalize().into();
+ let mut hash = [0u8; 20];
+ hasher.finalize(&mut hash);
bech32::encode::<bech32::Bech32>(bech32::Hrp::parse_unchecked("asset"), &hash).unwrap()
}
@@ -332,10 +334,12 @@ async fn _process(
hal.ui().status(&status, true).await;
let tx_body_hash: [u8; 32] = {
- let mut hasher = Blake2b256::new();
+ let mut hasher = Blake2b::new(32);
cbor::encode_transaction_body(hal, request, cbor::HashedWriter::new(&mut hasher)).await?;
- hasher.finalize().into()
+ let mut output = [0u8; 32];
+ hasher.finalize(&mut output);
+ output
};
signing_keypaths.sort_unstable();
### src/rust/bitbox02-rust/src/hww/api/cardano/sign_transaction/cbor.rs
@@ -238,6 +238,7 @@ pub async fn encode_transaction_body<W: Write>(
#[cfg(test)]
mod tests {
+ use super::super::super::blake2b::Blake2b;
use super::*;
use alloc::vec::Vec;
use blake2::{Blake2b256, Digest};
@@ -282,10 +283,12 @@ mod tests {
// Now encode CBOR into the hasher directly and compare results.
let hash: [u8; 32] = {
- let mut hasher = Blake2b256::new();
+ let mut hasher = Blake2b::new(32);
let mut encoder = Encoder::new(HashedWriter::new(&mut hasher));
encode_something(&mut encoder).unwrap();
- hasher.finalize().into()
+ let mut output = [0u8; 32];
+ hasher.finalize(&mut output);
+ output
};
assert_eq!(hash, expected_hash);
}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.