Skip to content

x86-64 AES-NI/VAES GCM and OCB key setups and CMAC finish: a working space of the size the code uses, without secret residue - #4939

Merged
reaperhulk merged 1 commit into
mainfrom
claude/residue-aes-scratch
Nov 11, 2026
Merged

reaperhulk merged 1 commit into
mainfrom
claude/residue-aes-scratch

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

This PR converts six x86-64 functions to prove NoResidue, removing their 6 lines from ci/residue_allowlist.txt (−6/+0):

  • vg_aes_gcm_init_{aesni,vaes}
  • vg_aes_ocb_init_{aesni,vaes}
  • vg_cmac_aes_finish_{aesni,vaes}

It gives each a stack working space of the size its code uses, so their scrub tails are small. No trust change: nothing under TCB/ or Spec/ changes, and no existing contract is weakened.

Why

#4901 converted these functions at first, then dropped them: wiping their 2.3–2.6 KB working space (withStackScratch 2312/2568, sized for every implementation) on every call cost too much. At 64 B, AES-NI CMAC was 54–87% slower, and OCB and GCM encryption 11–40% slower.

The AES-NI and VAES callees (ctr32, encrypt-blocks, key expansion) never touch that scratch. Their code uses only:

  • GCM init: [96,112) and [128,176), 176 of 2560 bytes;
  • OCB init: [0,24);
  • CMAC finish: the 16-byte counter block at +2048.

The analysis still selected the whole frame, because the callees store through advancing data pointers it cannot place. That makes every byte of the frame possibly secret. As in #4937 for TDEA-CMAC, the fix is a smaller frame.

The change

  • Contracts without scratch for the AES-NI/VAES callees: AesNi.ctr32X86_64F and Proof.Aes.blocksX86_64F, alongside the existing expandKey3X86_64.
    • The AES-NI/VAES proofs now take a Pre with any trailing regions. Decryption's scratch facts move to PreS.
    • They are exposed as new optional Ctr32Impl.free and BlocksImpl.free fields.
  • GCM and OCB init: the code is unchanged; the frames are now 184 and 32 bytes.
    • Their F variants of the wp, CT, verified and framed proofs reuse the old structure.
    • save_ok/restore_ok/exit_ok take any working space of at least 176 bytes.
  • CMAC finish:
    • New finishF code passes scratch - 2048 to finalize, with a 24-byte frame.
    • FPre is generalized over finalize's writable scratch region, with a new contract finalizeX86_64F.
    • SIV's call site is adjusted for the new FPre.
  • The residue conversion:
Function Tail stores (first attempt, #4901) Now
vg_aes_gcm_init_aesni / _vaes 161 12 / 6
vg_aes_ocb_init_aesni / _vaes 161 2 / 1
vg_cmac_aes_finish_aesni / _vaes 145 2 / 1

What stays listed

  • The baseline vg_cmac_aes_finish: the scalar ctr32 really uses about 2 KB of the scratch (round keys up to r9+1920).
  • vg_cmac_aes_absorb_aesni: the same approach should work. A trial analysis gives a 56-byte frame with a 4-granule wipe. It needs the same rework of the absorb proofs (AbsorbCorrect, AbsorbBlocks, UArgs) and a scratch-free update_aesni contract, left for a follow-up.
  • The other AES functions listed in x86-64 AES key expansion, CFB8, ECB encryption and GCM prepare return without secret residue #4901, for the reasons given there.

Checks

  • lake build (all modules), Emit.lean and Emit.lean --check: pass.
  • The Static checks job (--base origin/main where it applies): pass.
  • check_lean_public_imports.py: pass.
  • cargo fmt --check and 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 42952046134: x86-64, AES-128 GCM and OCB encryption and AES-128 CMAC (the benchmarks that showed #4901's regressions) against 16824a76c, in all 15 configurations that choose among the implementations, one interleaved run.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa

…ace of the size the code uses

The AES-NI and VAES implementations of vg_aes_ctr32, vg_aes_encrypt_blocks
and vg_aes_expand_key_scratch never touch their scratch buffers, yet their
callers set aside 2-2.5 KB for them, and the residue analysis cannot place
the callees' stores through advancing data pointers, so the scrub tails
wiped the whole frame (145-161 stores). Each now also gives its contract
without a scratch buffer (Ctr32Impl.free, BlocksImpl.free,
AesNi.Key.expandKey3X86_64; the existing proofs, generalized over the
scratch region, cover both), and over those:

* vg_aes_gcm_init_{aesni,vaes}: a 176-byte working space (the block T and
  the saved registers it writes itself), same code; tail 161 -> 12 / 6 stores.
* vg_aes_ocb_init_{aesni,vaes}: 24 bytes (the saved registers); 161 -> 2 / 1.
* vg_cmac_aes_finish_{aesni,vaes}: 16 bytes, vg_cmac_aes_finalize's counter
  block, passed as scratch - 2048 (finishF); 145 -> 2 / 1.

The finalize proof (FPre) is generalized over the writable part of its
scratch buffer, with a contract finalizeX86_64F of the counter block alone;
the 2304/2560-byte contracts and the baseline instances are unchanged. These
six functions now prove NoResidue and leave ci/residue_allowlist.txt.

No contract in Spec/ and nothing in TCB/ changes.

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 11, 2026
Merged via the queue into main with commit c940ddb Nov 11, 2026
51 checks passed
@reaperhulk
reaperhulk deleted the claude/residue-aes-scratch branch November 11, 2026 04:01
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