Skip to content

x86-64 SEED, Camellia and SM4 modes return without secret residue - #4895

Merged
reaperhulk merged 4 commits into
mainfrom
claude/residue-seed-camellia-sm4
Nov 10, 2026
Merged

reaperhulk merged 4 commits into
mainfrom
claude/residue-seed-camellia-sm4

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

A residue burn-down, following #4892 (Blowfish and IDEA). It converts the ECB, CBC, CFB and OFB functions of SEED, Camellia and SM4, plus SEED's and Camellia's key expansion, taking 32 lines off ci/residue_allowlist.txt. No trust change: nothing under TCB/ or Spec/ changes.

What is converted

  • SEED (11) and Camellia (11): expand_key; ecb_encrypt and ecb_decrypt; cbc_encrypt_chained and cbc_decrypt_chained; cfb128_encrypt, cfb128_decrypt and their _final forms; ofb and ofb_final.
  • SM4 (10): the same, without expand_key.

Each function is scrubbed with X86_64.Scrub.withScrub, using a residue analysis the kernel checks, as in #4892.

What the scrub adds: these functions already wipe their stack scratch themselves (withStackScratchWiped). Since #4907 the analysis counts that wipe, so each scrub tail adds one wipe store besides clearing registers. The first version of this PR, built before #4907, wiped the whole frame a second time: 205 stores for each Camellia mode, 201 for SM4, and 97 for SEED (69 for ECB).

What stays listed, and why (30)

  • vg_camellia_ctr and vg_sm4_ctr: vg_*_ctr_final calls them by name with their current code (Impl/*/X86_64/CtrFinal.lean).

  • vg_sm4_expand_key: SM4-GCM calls it by name with its current code.

    In these cases the emitter refuses a name whose code differs from the code its callers run. Converting them needs the callers to call the unscrubbed code under a name of its own, as x86-64 hash inits, SHA-512 digest and zeroize return without secret residue #4904 does for the hash inits.

  • vg_camellia_ctr_final and vg_sm4_ctr_final: the analysis rejected them when this PR was written. Since x86-64 residue analysis: know the stack pointer again after a return #4907 it passes, so a follow-up needs only their PostOut proofs.

  • The 25 SM4-GCM functions: the analysis does not complete on them. Converting them needs work on the code or the analysis.

Proofs

  • Proof/{Seed,Camellia,Sm4}/X86_64/Scrub.lean (new): one PostOut proof per contract.
    • SEED and SM4 reuse the existing *PostOut_local and Proof.Modes.*FinalPost_local lemmas.
    • Camellia's postconditions quantify over the key schedule, so its file proves locality itself, with small helpers.
  • The twelve registration files (Artifacts/{Seed,Camellia,Sm4}{,Cbc,Cfb,Ofb}/X86_64.lean) now only bind the scrubbed registrations.

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 42852455288, at this head, against ac141ad29. It covers x86-64, the ECB, CBC, CFB and OFB benchmarks of SEED, Camellia and SM4 (105 benchmarks), in one interleaved run.

  • 104 of the 105 benchmarks are within ±1.2%.
  • The exception is Camellia CBC decryption with a 128-bit key: 6.6% slower at 64 B (1.70 → 1.81 µs) and 2.8% slower at 1 KiB. With 192- and 256-bit keys, which run the same mode function, it is within ±0.7%. The scrub adds one store per call, so this is likely noise.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa

Key expansion (but SM4's) and the ECB, CBC, CFB128 and OFB functions of
SEED, Camellia and SM4 on x86-64 are followed by the residue scrub
(`X86_64.Scrub.withScrub`), with their postconditions shown to read the
memory on return only within the buffers (`PostOut`), in
`Proof/Seed/X86_64/Scrub.lean`, `Proof/Camellia/X86_64/Scrub.lean` and
`Proof/Sm4/X86_64/Scrub.lean`. The registration files bind them, and the 32
functions leave `ci/residue_allowlist.txt`.

Camellia's and SM4's CTR functions stay listed (`vg_*_ctr_final` calls
`vg_*_ctr` by name with its code as it is, and its own analysis fails), as
does `vg_sm4_expand_key`, which SM4-GCM calls by name, and SM4-GCM, whose
analysis fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
@github-actions

Copy link
Copy Markdown

This pull request has conflicts with main, found after 3baa1ab landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

…amellia-sm4

# Conflicts:
#	ci/residue_allowlist.txt
The residue analysis now knows the stack pointer after a return, so the
scrub tails no longer wipe the frames that the code already wiped.

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 ready for review November 10, 2026 17:00
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 10, 2026
Merged via the queue into main with commit 1ca7890 Nov 10, 2026
36 checks passed
@reaperhulk
reaperhulk deleted the claude/residue-seed-camellia-sm4 branch November 10, 2026 20:34
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