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

pull l0rinc wants to merge 4 commits into bitcoin:master from l0rinc:l0rinc/hkdf-hmac-state changing 6 files +97 −25
  1. l0rinc commented at 11:43 AM on November 9, 2026: 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%)
    
  2. crypto: require a fixed-size HKDF output buffer
    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.
    448ac7bcef
  3. refactor: extract HKDF key setup
    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.
    1cda969466
  4. test, bench: cover repeated HKDF expansion
    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.
    d1d1409d17
  5. crypto: copy HMAC state instead of rehashing key pads
    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.
    80130b90e2
  6. DrahtBot added the label Utils/log/libs on Nov 9, 2026
  7. DrahtBot commented at 11:43 AM on November 9, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    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 <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36808 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36808.svg"></sub> (iwyu: Fix warnings in src/test/fuzz and treat them as errors by hebasto)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  8. sedited commented at 11:53 AM on November 9, 2026: contributor

    Concept ACK

  9. w0xlt commented at 2:16 AM on November 10, 2026: contributor

    Concept ACK

  10. Pranav-error commented at 7:14 PM on November 12, 2026: none

    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.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-20 15:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me