Skip to content

TDEA-CMAC streaming on x86-64: a working space of the size the code uses - #4937

Merged
reaperhulk merged 1 commit into
mainfrom
claude/residue-cmac-tdes-wipe
Nov 11, 2026
Merged

reaperhulk merged 1 commit into
mainfrom
claude/residue-cmac-tdes-wipe

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

This PR makes the x86-64 TDEA-CMAC streaming functions, vg_cmac_triple_des_stream_init, _absorb and _finish, reserve only the stack scratch their code uses. Their scrub tails (#4897) then wipe 4–5× less. No trust change: nothing under TCB/ or Spec/ changes, and the contracts the Rust code sees only shrink their stack bound.

Why the tails were large

Each function took a 640-byte working space. The tail wiped the whole frame on every call: 41, 44 and 41 granules (656/704/656 bytes).

  • The inlined init, update and finalize only use slots 0–14 (120 bytes):
    • the block function's state ([0,48));
    • saved registers ([48,96));
    • the DES keys, the data pointer and the block count, or Mₙ.
  • absorb also keeps the caller's callee-saved registers at [640,680). Bytes [120,640) were never written.

The residue analysis still selected the whole frame, because each function makes one store it cannot place:

  • the round-key stores in init's loop, whose base register advances by 128;
  • copy's [rdx+r10] in absorb;
  • finalize's [r15+r10+96] in finish.

Each such store makes every byte of the frame possibly secret. Wiping the scratch in the code (withStackScratchWiped) would not help, since the analysis would still select the frame. Wiping fewer bytes means a smaller frame.

The change

  • The working space is 120 bytes, and 160 for absorb, whose saves of the caller's registers move from 640–672 to 120–152 (saved in Impl/CmacTripleDes/Stream/X86_64.lean). The frames go from 648/696/648 to 128/168/128 bytes. The code is otherwise unchanged.
  • The proofs change only region sizes. x86-64 now has scratch contracts of its own (15/20/15 words, Proof/CmacTripleDes/Stream/X86_64/Implies.lean). Frame.lean and Scrub.lean pass the new frame sizes. The other architectures and the shared StreamScratch.lean do not change.
Function Tail stores before After
vg_cmac_triple_des_stream_init 41 (656 B) 8 (128 B)
vg_cmac_triple_des_absorb 44 (704 B) 11 (168 B)
vg_cmac_triple_des_finish 41 (656 B) 8 (128 B)

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. That includes the TDEA-CMAC CAVP and unit tests.

Speed

Benchmarks run 42944776393: x86-64, cmac_triple_des against 02b499097, in the default configuration, one interleaved run.

  • tdes-cmac and tdes-cmac-verify are within ±0.2% at 64 B, 1 KiB and 16 KiB. For example, tdes-cmac/64 went from 16.48 to 16.44 µs.
  • Triple DES takes about 16 µs for 64 B, so the 33 stores saved per call (a few nanoseconds) do not show.
  • What this PR buys is a smaller stack footprint per call: 128–168 bytes instead of 648–696.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa

The code of `init`, `update` and `finalize` that `vg_cmac_triple_des_stream_init`,
`_absorb` and `_finish` run uses only slots 0-14 (120 bytes) of its 640-byte
working space. Their scrub tails wiped the whole frame: stores the residue
analysis cannot place (the round keys through the advancing `rbp` in `init`,
the byte copies indexed by a loop counter in `absorb` and `finalize`) drop
every public slot of the frame, so all of it is listed on free.

The working space is now 120 bytes (160 for `absorb`, which keeps our
caller's registers at 120 rather than 640), so the frames are 128 and 168
bytes: the tails wipe 8, 11 and 8 granules instead of 41, 44 and 41
(128/168/128 bytes instead of 656/704/656 per call). The proofs only change
the region sizes; x86-64 states its own contracts with the working space as
an argument (`Stream/X86_64/Implies.lean`). No contract in `Spec/` changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 11, 2026
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit bbb80e2 Nov 11, 2026
36 checks passed
@reaperhulk
reaperhulk deleted the claude/residue-cmac-tdes-wipe branch November 11, 2026 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified.

Development

Successfully merging this pull request may close these issues.

2 participants