btc: validate prevtx input hash length
What changed, and why it matters
This commit fixes a missing length check in the BitBox02 hardware wallet's Bitcoin transaction signing code. When verifying a previous transaction supplied by the host computer, the device was hashing the previous output hash without first confirming it was the expected 32 bytes. A malicious or buggy host could supply a shortened or lengthened hash that still matched a transaction hash the device itself computed, letting malformed data pass verification. The patch now rejects any hash that is not exactly 32 bytes before hashing it.
Treat this as a security-relevant hardening fix. Review whether other fields serialized into the prevtx hash (scriptSig, sequence, output value, scriptPubKey, locktime, sighash) have similar missing length checks, and audit the host protocol for any other variable-length fields that could be used to shape a matching hash. Ensure the fix is included in the next firmware release.
Security signals we found
Missing input-length validation in cryptographic hashing path
Host-supplied data trusted without strict length check
Potential bypass of previous-transaction verification via malformed prev_out_hash
Patch adds explicit 32-byte length check and unit tests for off-by-one/empty cases
Evidence from the diff
In src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs, inside handle_prevtx, the code now checks prevtx_input.prev_out_hash.len() != 32 and returns Error::InvalidInput before calling hasher.update(...) on the hash. Previously the hash was fed directly into the transaction hasher regardless of length. The added unit test verifies that resized hashes of 0, 31, and 33 bytes are rejected for both previous-transaction input indices, even when the host-provided transaction hash is recomputed to match the resized input.
Changed components
BitBox02 firmware Bitcoin signing flowsrc/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rshandle_prevtx() functionInspect captured patch +28 / −0
### src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs
@@ -384,6 +384,9 @@ async fn handle_prevtx(
);
let prevtx_input = get_prevtx_input(input_index, prevtx_input_index, next_response).await?;
+ if prevtx_input.prev_out_hash.len() != 32 {
+ return Err(Error::InvalidInput);
+ }
hasher.update(prevtx_input.prev_out_hash.as_slice());
hasher.update(prevtx_input.prev_out_index.to_le_bytes());
hasher.update(serialize(&VarInt(
@@ -3027,6 +3030,31 @@ mod tests {
}
}
+ #[async_test::test]
+ async fn test_process_invalid_prevtx_input_hash_length() {
+ for prevtx_input_index in [0, 1] {
+ for hash_len in [0, 31, 33] {
+ mock_unlocked();
+ let transaction = new_transaction(pb::BtcCoin::Btc).await;
+ {
+ let mut transaction = transaction.borrow_mut();
+ let input = &mut transaction.inputs[0];
+ input.prevtx_inputs[prevtx_input_index]
+ .prev_out_hash
+ .resize(hash_len, 0);
+ input.input.prev_out_hash = prevtx_hash(input);
+ }
+ mock_host_responder(transaction.clone());
+ let init_request = transaction.borrow().init_request();
+ assert_eq!(
+ process(&mut TestingHal::new(), &init_request).await,
+ Err(Error::InvalidInput),
+ "prevtx input {prevtx_input_index}, hash length {hash_len}"
+ );
+ }
+ }
+ }
+
#[async_test::test]
async fn test_silent_payment_rejects_input_keypath_mismatch() {
for (simple_type, purpose) in [Why this scored 59/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.