Skip to content

crypto: accept empty HMAC keys - #35688

Open
l0rinc wants to merge 2 commits into
bitcoin:masterfrom
l0rinc:l0rinc/crypto-empty-input-hashing
Open

l0rinc wants to merge 2 commits into
bitcoin:masterfrom
l0rinc:l0rinc/crypto-empty-input-hashing

Conversation

@l0rinc

@l0rinc l0rinc commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem: CHMAC_SHA256 and CHMAC_SHA512 use memcpy to copy key bytes.
Empty vectors can provide a null data() pointer, which triggers a UBSan null-pointer warning even for zero-length copies and forces the crypto fuzz target to avoid empty inputs.

Fix: Use std::copy, which has defined semantics for empty ranges, add empty-key HMAC test vectors, and let the crypto fuzz target pass empty byte vectors directly.
Remove the stale eval_script empty-input guard in a separate commit because ConsumeRemainingBytes() already returns before copying when no bytes remain.

@DrahtBot

DrahtBot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35688.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK sedited

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

@l0rinc l0rinc closed this Jul 8, 2026
@l0rinc l0rinc reopened this Jul 8, 2026
@DrahtBot DrahtBot removed the CI failed label Jul 9, 2026

@maflcko maflcko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to change this, but at least for me it is really hard to follow the commit description, as it seems to mix several different broad topics:

  • It claims that #35010 is related to null-annotations where both the src/dst are zero-size. This is not true, because #35010 is about size mismatches, where the dst is too small to fit the src.
  • It claims that #35010 is related to the discussion (and was picked up in the discussion) in #35118 (comment), however I fail to see the link.
  • Then it links to https://godbolt.org/z/jMPP8EhEr, which also has a type-check includes. However, the types here are the same: both are uint8_t for src and dst.
  • It mixes in the eval_script fuzz target change, which could make sense to split up into a separate commit.

Again, seems fine to change, but the commit message can be a bit shorter and basically just say something like:

crypto: accept empty HMAC keys

This avoids an Ubsan nullptr warning on memcpy, when passing empty HMAC keys.

With the warning gone, workarounds in the tests can be removed.

Though, feel free to ignore/adjust. I think that commit messages and pull descriptions should be clean and concise, and opinions and extended discussions are better left to GH review or issue comments.

Comment thread src/crypto/hmac_sha256.cpp Resolved
l0rinc and others added 2 commits July 9, 2026 12:24
Replace `memcpy` with `std::copy` so empty HMAC keys do not trigger a UBSan null-pointer warning.
Add empty-key test vectors and let the crypto fuzz target exercise empty byte vectors directly.

Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^[email protected]>
`ConsumeRemainingBytes()` already returns before copying when no bytes remain, so call it directly.
@l0rinc
l0rinc force-pushed the l0rinc/crypto-empty-input-hashing branch from ac1ddae to dc67c4c Compare July 9, 2026 19:44
@l0rinc
l0rinc requested a review from maflcko July 22, 2026 22:50

@sedited sedited left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK dc67c4c

@sedited
sedited requested a review from willcl-ark October 19, 2026 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants