Skip to content

crypto: copy HMAC state instead of rehashing key pads - #36812

Open
l0rinc wants to merge 4 commits into
bitcoin:masterfrom
l0rinc:l0rinc/hkdf-hmac-state
Open

l0rinc wants to merge 4 commits into
bitcoin:masterfrom
l0rinc:l0rinc/hkdf-hmac-state

Conversation

@l0rinc

@l0rinc l0rinc commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Problem: Setting up an encrypted connection derives six values with the same secret. Each HKDF expansion repeats two SHA256 compressions to hash the same HMAC key pads.

Fix: Hash the key pads once per HKDF context, then copy the prepared HMAC state for each expansion. The copy replaces two setup compressions, saving ten SHA256 compressions across the six outputs - the derived bytes stay identical. RFC 2104 section 4 describes this optimization:

the intermediate results … on the B-byte blocks (K XOR ipad) and (K XOR opad) can be precomputed only once

Measurements:

BIP324_HKDF (ops/s, higher is better)
Before  █████████████████████████████████░░░░░░░  1,430,064  (baseline)
After   ████████████████████████████████████████  1,724,373  (+20.6%)

The array parameter decays to a pointer, so callers can supply fewer than 32 writable bytes without a compiler diagnostic.
Take a fixed-size byte span and pass output containers directly so their sizes are checked at compile time.
Extract key setup into PrepareHMAC before reusing prepared HMAC state.
Pass input key material as a byte span so callers can use their existing byte containers directly.
Check fixed outputs for all six BIP324 labels and interleaved reuse of the existing HKDF test vectors.
Measure HKDF context creation, the six BIP324 expansions, and clearing together before reusing prepared HMAC states.
Each HKDF expansion hashes the same two HMAC key pads, costing two SHA256 compressions before hashing the label.
Prepare those states once per HKDF context and replace the repeated hashing with a copy of the prepared HMAC state.
Across BIP324's six expansions this saves ten SHA256 compressions, with identical derived bytes.
Cleanse the temporary extracted secret after constructing the prepared state.
@DrahtBot

DrahtBot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

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

Type Reviewers
Concept ACK sedited, w0xlt, Pranav-error

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

Conflicts

No conflicts as of last run.

@sedited

sedited commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@w0xlt

w0xlt commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@Pranav-error

Copy link
Copy Markdown

Code review ACK (read the diff, didn't build).

The optimization is standard and correct: HKDF-Expand here uses the same PRK for all six expansions, so pre-absorbing the key pads once (PrepareHMAC → m_hmac) and copying that prepared state per Expand32 is bit-identical to reconstructing CHMAC_SHA256(prk, ...) each time (RFC 2104 §4).

Two things I checked while reviewing:

  • Expand32 is const and does CHMAC_SHA256{m_hmac} (a copy), so it can't mutate the shared context for later labels — and the new test asserts exactly that ("Expansions must not modify the shared context state"), with the six BIP324 label vectors pinning the outputs.
  • The prepared m_hmac embeds key-deriving material (as the old m_prk did); BIP324Cipher::Initialize still does memory_cleanse(&hkdf, sizeof(hkdf)), which zeroes the m_hmac SHA256 midstates in place, so the "wipe anything that could re-derive the keys" invariant is preserved.

The ~20% on connection setup is a nice win for a hot path. LGTM modulo a Tested ACK from someone who can run crypto_tests.

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.

5 participants