Skip to content

x86-64 AES key expansion, CFB8, ECB encryption and GCM prepare return without secret residue - #4901

Merged
reaperhulk merged 3 commits into
mainfrom
claude/residue-aes
Nov 10, 2026
Merged

reaperhulk merged 3 commits into
mainfrom
claude/residue-aes

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Continues the x86-64 residue burn-down (#4836). It converts 12 AES functions to prove NoResidue, and removes their 12 lines from ci/residue_allowlist.txt (−12/+0). No trust change: nothing under TCB/ or Spec/ changes.

What is converted

  • Key expansion: vg_aes_expand_key and vg_aes_expand_key_aesni (Proof/Aes/X86_64/Scrub.lean).

  • GCM prepare: vg_aes_gcm_prepare, _pclmul_avx, _vpclmul and _vpclmul_avx512 (Proof/Gcm/X86_64/PrepareScrub.lean).

  • Over the block implementations (BlocksImpl):

    • vg_aes_cfb8_{en,de}crypt_{aesni,vaes}
    • vg_aes_ecb_encrypt_{aesni,vaes}

    BlocksImpl gets three optional analysis fields: cfb8EncRes, cfb8DecRes and ecbEncRes. They are set in Proof/Variants/AesBlocks/X86_64.lean from Proof/Aes/X86_64/BlocksResidue.lean. The registrations are in Proof/Aes/X86_64/ModesScrub.lean, and ECB's in Proof/AesEcb/X86_64.lean.

What stays listed and why

  • CMAC (init, absorb, finish), GCM init and OCB init: these take a 2.3–2.6 KB stack scratch (withStackScratch 2312/2568), sized for every implementation. The analysis cannot bound what the callee leaves in it, so a scrub wipes all of it, about 145–160 16-byte stores per call.
    • The first version of this PR converted cmac_aes_finish, cmac_aes_absorb_aesni, gcm_init and ocb_init. Benchmarks run 42786235913 measured, with AES-NI at 64 B:
      • CMAC: 54–87% slower;
      • OCB encryption: 17–39% slower;
      • GCM encryption: 11–40% slower.
    • Those conversions are dropped. Converting them cheaply needs a scratch sized per implementation, or a scrub of only what the callee writes, in a separate PR.
  • Called by name with their current code, which the emitter refuses to change:
    • vg_aes_cfb128_{en,de}crypt_{aesni,vaes} and vg_aes_ofb_{aesni,vaes}: their _final functions call them.
    • vg_aes_gcm_stream_init* and vg_aes_gcm_stream_aad*: vg_aes_gcm_seal_gather_prepared* calls them. Their analyses pass, so a later PR can give the callers the unscrubbed code under its own name.
  • vg_aes_gcm_wipe_prepared{,_avx}: the code branches on the power count, which the contract does not let it leak.
  • vg_aes_cbc_encrypt_aesni: its registration writes a scratch buffer (written), which withScrub does not support yet.
  • vg_aes_ocb_seal, ocb_open and siv_init: the analysis fails.

Proofs

  • No sorry, native_decide, bv_decide, axioms or resource options.
  • BlocksResidue takes about a minute to build: each large-stack analysis is 20–30 s in the kernel, even with materialize_code. It stays within the default heartbeats, and check_lean_speed.py passes.

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 on the changed modules: 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

Two scoped Benchmarks runs, each x86-64 against the merge base, in every configuration that chooses among AES's implementations, one interleaved run:

  • CFB8 (encryption and decryption) and ECB encryption (run 42786235913, whose code for these is unchanged since): within about ±5% at every size. The one larger change is ECB at 64 B, 8.7% slower at AES-256 (40 → 43 ns).
  • AES-128 GCM encryption, OCB encryption and CMAC at this head (run 42792996692), whose key setup expands the key and, for GCM, prepares it: within ±3% at 64 B and 1 KiB in every configuration. CMAC at 64 B ranges from 2.8% slower (177 → 182 ns) to 6.6% faster.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa

…M prepare and CMAC finish return without secret residue

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

* vg_aes_expand_key, vg_aes_expand_key_aesni (Proof/Aes/X86_64/Scrub.lean).
* vg_aes_gcm_prepare and its _pclmul_avx, _vpclmul, _vpclmul_avx512
  variants (Proof/Gcm/X86_64/PrepareScrub.lean).
* Over the AES-NI and VAES block implementations, through new optional
  analyses on BlocksImpl (cfb8EncRes, cfb8DecRes, ecbEncRes, ocbInitRes,
  given in Variants/AesBlocks/X86_64.lean from BlocksResidue.lean):
  vg_aes_cfb8_{en,de}crypt_{aesni,vaes}, vg_aes_ecb_encrypt_{aesni,vaes},
  vg_aes_ocb_init_{aesni,vaes} (ModesScrub.lean, AesEcb/X86_64.lean).
* Over the implementations of vg_aes_ctr32, through new optional analyses
  on Ctr32Impl (cmacFinishRes, gcmInitRes, from Ctr32Residue.lean):
  vg_cmac_aes_finish{,_aesni,_vaes} and vg_aes_gcm_init_{aesni,vaes}
  (on Aes.X86_64.gcmInitCode, equal to init_framed's code).
* vg_cmac_aes_absorb_aesni, through a new optional UpdateImpl.absorbRes.

postOut_of_local_pre (Proof/Aes/X86_64/Scrub.lean) gives PostOut for the
key setups, whose postconditions read only the key context for the key
lengths the precondition allows.

Left listed: vg_aes_cfb128_{en,de}crypt_{aesni,vaes} and vg_aes_ofb_{aesni,vaes}
(their _final functions call them by name), vg_aes_gcm_stream_init*,
vg_aes_gcm_stream_aad* (vg_aes_gcm_seal_gather_prepared* calls them by name),
vg_aes_gcm_wipe_prepared* (it branches on the power count, which its contract
leaks only as flagLeak), vg_aes_cbc_encrypt_aesni (its registration states
`written`, which withScrub does not take).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
@reaperhulk
reaperhulk marked this pull request as draft November 10, 2026 08:58
Benchmarks showed that converting vg_cmac_aes_finish{,_aesni,_vaes},
vg_cmac_aes_absorb_aesni, vg_aes_gcm_init_{aesni,vaes} and
vg_aes_ocb_init_{aesni,vaes} cost 50-130 ns per call: each wipes its
2.3-2.6 KB withStackScratch buffer on return. Restore their registrations
and allowlist lines as on main, and delete what only they used
(Ctr32Impl.cmacFinishRes/gcmInitRes, UpdateImpl.absorbRes,
BlocksImpl.ocbInitRes and its analyses, gcmInitCode, postOut_of_local_pre,
Proof/Aes/X86_64/Ctr32Residue.lean, Proof/AesGcm/X86_64/Scrub.lean and
Proof/CmacAes/Stream/X86_64/Scrub.lean). Key expansion, GCM prepare,
CFB8 and ECB encryption stay converted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
@reaperhulk reaperhulk changed the title x86-64 AES key expansion, CFB8, ECB, OCB/GCM key setup, GCM prepare and CMAC-AES return without secret residue x86-64 AES key expansion, CFB8, ECB encryption and GCM prepare return without secret residue Nov 10, 2026
@reaperhulk
reaperhulk marked this pull request as ready for review November 10, 2026 09:49
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 10, 2026
Merged via the queue into main with commit 7009992 Nov 10, 2026
33 checks passed
@reaperhulk
reaperhulk deleted the claude/residue-aes branch November 10, 2026 20:29
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