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

Open
l0rinc wants to merge 4 commits from l0rinc/hkdf-hmac-state into master AGit
l0rinc commented 2026-11-09 11:43:22 +00:00 (Migrated from github.com)

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%)
**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](https://www.rfc-editor.org/rfc/rfc2104.html#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:** ```text BIP324_HKDF (ops/s, higher is better) Before █████████████████████████████████░░░░░░░ 1,430,064 (baseline) After ████████████████████████████████████████ 1,724,373 (+20.6%) ```
DrahtBot commented 2026-11-09 11:43:30 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36812 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36812.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36812#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [sedited](https://github.com/bitcoin/bitcoin/pull/36812#issuecomment-6739402511), [w0xlt](https://github.com/bitcoin/bitcoin/pull/36812#issuecomment-6757070503), [Pranav-error](https://github.com/bitcoin/bitcoin/pull/36812#issuecomment-6833425469) | 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 No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
sedited commented 2026-11-09 11:53:49 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
First-time contributor

🔵 Suggestion · Six-label HKDF vectors lack expected-value provenance

The new fixed outputs do not identify their source, so readers cannot distinguish an independent correctness check from a snapshot of the implementation's output.

Location: src/test/crypto_tests.cpp:1080 (head).

Review coverage was incomplete.


Reviewed: 80130b90e2f5 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:055d1cd96b176486294e0a3d052a7e506a00c1f0c7c35fbf579b09cfe6d615cd:237cc81563f53f26a84ba90acce49f0597b0e28b0a77aa425401db6e90431c0b --> <!-- ralph-base:4ef7904c39fa2c9eedc66d1a75caaf34c0c531b9 --> <!-- ralph-head:80130b90e2f5fcb739a389ab4f6c60972e158693 --> <!-- ralph-case:1fff4e1427e279e72b8c64dc8b879bb536ac7fbc389911baef8260591727c13c --> **🔵 Suggestion · Six-label HKDF vectors lack expected-value provenance** The new fixed outputs do not identify their source, so readers cannot distinguish an independent correctness check from a snapshot of the implementation's output. Location: [`src/test/crypto_tests.cpp:1080`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/80130b90e2f5fcb739a389ab4f6c60972e158693/src/test/crypto_tests.cpp#L1080>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`80130b90e2f5`](<https://git.fish.foo/bitcoin/bitcoin/commit/80130b90e2f5fcb739a389ab4f6c60972e158693>) · [Full review report](<https://review.fish.foo/traces/36812-80130b90e2f5fcb739a389ab4f6c60972e158693-2399-1.html>) · [Previous reports](<https://review.fish.foo/traces/36812/>) [permalink](<https://review.fish.foo/traces/36812-80130b90e2f5fcb739a389ab4f6c60972e158693-2399-1.html>)
w0xlt commented 2026-11-10 02:16:30 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
Pranav-error commented 2026-11-12 19:14:26 +00:00 (Migrated from github.com)

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.

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 in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36812
No description provided.