Skip to content

x86-64 HMAC finalize, BLAKE2 and SHA-3/SHAKE return without secret residue - #4898

Merged
reaperhulk merged 1 commit into
mainfrom
claude/residue-hmac-blake2-sha3
Nov 10, 2026
Merged

reaperhulk merged 1 commit into
mainfrom
claude/residue-hmac-blake2-sha3

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

A residue burn-down, following #4892. It takes 38 lines off ci/residue_allowlist.txt. No trust change: nothing under TCB/ or Spec/ changes.

What is converted

  • HMAC finalize (18): over MD5; SHA-1 (scalar, AVX/BMI and SHA-NI); SHA-224 and SHA-256 (scalar, AVX2 and SHA-NI); and SHA-384, SHA-512, SHA-512/224 and SHA-512/256 (scalar and SHA-NI).
  • BLAKE2b (6): update and finalize, scalar, AVX2 and AVX-512.
  • BLAKE2s (5): init, and update and finalize, scalar and AVX.
  • SHA-3 and SHAKE (9): vg_sha3_{224,256,384,512}_update, vg_shake128_{update,finalize,squeeze} and vg_shake256_{finalize,squeeze}.

How

Functions emitted once per implementation take their analysis from the implementation, as XorImpl.apply and BlocksImpl.update do for ChaCha20 and Poly1305 (#4836). Each is an optional field, none where the analysis does not accept the code:

  • MdHash.hmacFinRes, for HMAC finalize;
  • Backend.updateRes/finalizeRes and BackendS.updateRes/finalizeRes, for BLAKE2.

Each variant file sets them from the analysis of its own code, which the kernel checks with taint_decide. MdHash.hmacFinReg and the BLAKE2 builders scrub with withScrub, and the registration files bind them.

  • Proof/Pbkdf2/Md/X86_64/ScrubStream.lean holds hmacFinOut and MdHash.hmacFinReg.
  • Proof/Blake2/X86_64/Scrub.lean and Proof/Sha3/X86_64/Scrub.lean (new) hold the PostOut proofs and registrations.

The SHA-512 family's scalar and SHA-NI MdHash values are now defined in Variants/MdHash/X86_64/Sha512*.lean, next to their analyses.

What stays listed, and why (12)

Checks

  • lake build (all modules), Emit.lean --check, the Static checks job, check_lean_public_imports.py on the changed modules: pass.
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings: pass.
  • cargo test with Wycheproof, with every CPU feature and with VG_CPU_FEATURES=none: pass.

Speed

Benchmarks run 42772093053: x86-64, the HMAC, BLAKE2, SHA-3 and SHAKE groups against main, in each of the 10 configurations that choose among their implementations, one interleaved run each.

This PR has a visible cost on small inputs for BLAKE2 and SHAKE.

  • Their update and squeeze wipe 592 and 656 bytes of stack. The scrub writes those with 8-byte stores, which adds about 16–25 ns per call.
  • With every CPU feature, at 64 B, BLAKE2b is 8.9% slower (178 → 194 ns), BLAKE2s 12.5% (139 → 156 ns), and SHAKE128 and SHAKE256 6.2% and 6.6% (305/304 → 324 ns).
  • At 1 KiB the cost is 0.6–1.7%. At 16 KiB it is gone.
  • Across the configurations, the worst case at 64 B is BLAKE2s 20% slower (135 → 162 ns, AVX-512 IFMA).

Wiping with vector stores would cut this, and is planned as a separate change to the scrub itself.

  • SHA-3: within ±1% with every CPU feature, and within 4.2% slower to 8.3% faster across all configurations.
  • HMAC: within 2.9% faster to 0.6% slower with every CPU feature. Some other configurations show isolated larger moves, the largest being HMAC-MD5 at 1 KiB 15% slower with aes,avx,avx2,pclmulqdq,ssse3. Their finalize wipes at most 576 bytes once per message, and each move is matched by moves of similar size in the opposite direction elsewhere, so I take those to be noise.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa

…et residue

Follow each with scrubTail where the residue analysis handles its code,
proving NoResidue, and drop their 38 lines from ci/residue_allowlist.txt:

* SHA-3/SHAKE (Proof/Sha3/X86_64/Scrub.lean): every update, SHAKE's
  finalize and squeeze.
* BLAKE2b/BLAKE2s update and finalize over each backend, through new
  optional analyses on Backend/BackendS (updateRes, finalizeRes), and
  vg_blake2s_init (Proof/Blake2/X86_64/Scrub.lean).
* HMAC finalize over each MdHash variant, through a new optional
  MdHash.hmacFinRes and MdHash.hmacFinReg (ScrubStream.lean).

Left listed: SHA-3's finalize (the analysis loses r12's frame pointer
across the squeeze call), HMAC-SHA-384/512/512-224/512-256 finalize_avx2
(the analysis fails on the AVX2 SHA-512 code), vg_blake2b_init (Argon2
calls it by name).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 10, 2026
Merged via the queue into main with commit 3baa1ab Nov 10, 2026
44 checks passed
@reaperhulk
reaperhulk deleted the claude/residue-hmac-blake2-sha3 branch November 10, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants