Skip to content

OFB and CTR: verified final functions on every target; delete Rust's partial-block code - #4451

Merged
alex merged 20 commits into
mainfrom
claude/audit-4139-cfb-partial
Nov 7, 2026
Merged

alex merged 20 commits into
mainfrom
claude/audit-4139-cfb-partial

Conversation

@alex

@alex alex commented Nov 7, 2026

Copy link
Copy Markdown
Member

Part of #4139 (finding 4: CFB's partial last segment is XORed in Rust, here for OFB and CTR)

This PR gives the OFB and CTR final functions specified in #4334 verified implementations on all six targets (x86-64, AArch64, x86, Arm, PPC64LE, RISC-V):

  • vg_<cipher>_ofb_final for all eight ciphers: AES, Blowfish, Camellia, CAST5, IDEA, SEED, SM4 and Triple DES.
  • vg_<cipher>_ctr_final for AES, Camellia and SM4.

AES's final functions cover every variant (_aesni, _vaes, _aes), on x86-64, x86 and AArch64.

apply_keystream_final still gives a message's whole blocks to the whole-block functions. If a partial last block remains, it now goes to the final function, which continues from the IV or counter the whole blocks left. This is what #4394 and #4430 did for CFB. The Rust code that ran OFB or CTR over the last bytes padded to a block is deleted, along with its misleading comment ("The next output block, as OFB of a zero block, XORed into the last bytes"). Rust now only splits the buffer at a block boundary. With this PR, no mode's partial last block is computed in Rust.

Design

  • OFB of the seven non-AES ciphers. This uses CFB: verified final functions for the non-AES ciphers; delete Rust's partial-segment XOR #4394's generic final modes with OFB's step:

    • fbFinal on x86-64 and AArch64;
    • seqFinal on x86, Arm, PPC64LE and RISC-V.

    The loop's partial last segment leaves Oₙ = CIPH(Oₙ₋₁) in the IV, as Spec.Ofb.CryptFinal requires. The target-independent lemmas are ofbFinal_out/ofbFinal_of/ofbFinal_of_bytes (Proof/Modes/FbFinal.lean) and the scratch/locality lemmas (Proof/Modes/CfbFinalScratch.lean). Each target adds an OFB section to its Proof/Modes/<T>/SeqFinal.lean or FbFinal.lean, and each cipher adds an OFB section next to its CFB final in Proof/<C>/<T>/FbFinal*.lean. Only on Arm did the OFB proof need an extra fact: every output of the cipher is a block long, for every input.

  • OFB of AES. This is AES-CFB: verified final functions on every target; delete Rust's partial-segment XOR #4430's AES-CFB final caller, unchanged, around the whole-block AES-OFB function of the same variant. That caller's tail calls the block function on the IV in place, then XORs the bytes; that is exactly OFB's last step. The constant-time lemmas of Proof/AesCfb/<T>/Final.lean are generalized over the Mode; their statements are otherwise unchanged. Proof/AesOfb/Final.lean is target-independent.

  • CTR (AES, Camellia, SM4). The final function first calls the whole-block CTR function, of the same variant, on the whole blocks. If bytes are left:

    1. it zeroes one block in its own save area;
    2. it calls the same function on that one block, which leaves Oₙ = CIPH(Tₙ) in the block and the counter at Tₙ₊₁, as Ctr.next … ⌈len/16⌉ says;
    3. it XORs the block's first bytes into the tail.

    The save area, keystream included, is in a stack frame zeroed on return. The function relies only on the whole-block function's contract and constant-time proof. For Camellia and SM4, the callee is the registered stack-framed function. Target-independent pieces:

    • Proof/Modes/CtrFinal.lean, generic in the cipher;
    • Proof/AesCtr/Final.lean;
    • Proof/{Camellia,Sm4}/CtrFinal.lean, which holds the contract plumbing and the locality lemmas the frames need.

    AES-CTR's constant time is proven with the whole-block contract's leak: the counter's last 32 bits agree at both calls.

Each final function calls exactly the code and symbol its whole-block function's registration emits; on Arm and RISC-V, AES-OFB's tail calls the registered vg_aes_encrypt_blocks. Constant time is checked by the taint analysis, plus the callees' own constant-time proofs for the calls. Only pointers, rounds and len affect timing.

No TCB or Spec changes.

Tests

The existing tests check published vectors through apply_keystream_final, whole, truncated at every length and split at every point:

  • NIST SP 800-38A's for OFB (tests/cavp/*_ofb.rs);
  • the CAST5 and SM4 sets (tests/cryptography_cast5, tests/draft_ribose_sm4);
  • RFC 3686 for AES-CTR and RFC 5528 for Camellia-CTR (tests/rfc3686, tests/rfc5528), including RFC 3686's counter-carry case.

Every length that is not a multiple of the block size now ends in a verified final function. No vectors are typed in.

I ran these locally:

  • on x86-64 with VG_CPU_FEATURES set to none, aes,ssse3 and aes,ssse3,avx,avx2,vaes;
  • under qemu-user on i686, aarch64, armv7, ppc64le and riscv64, and on aarch64 and i686 also with VG_CPU_FEATURES=none.

All of them pass. So does the whole host test suite. The following also pass:

  • the full lake build and Emit.lean;
  • every step of the Static checks job;
  • check_lean_public_imports.py on the changed modules, run per target, since lake shake on all of them at once hits a pre-existing clash between two Ecb modules of main;
  • cargo fmt, and clippy on all six targets.

Benchmarks

None were run. The OFB and CTR benchmarks' sizes (64, 1024 and 16384 bytes) are whole blocks, so for them apply_keystream_final calls the same whole-block functions as on main and nothing else. The change cannot affect their speed, and the benchmarking skill says not to run a benchmark for such a change. A partial last block, which no benchmark measures, used to cost a call of the whole-block function on a padded block. It now costs a call of the final function.


Generated by Claude Code

claude added 18 commits November 7, 2026 11:11
…ED, SM4 and Triple DES (WIP: crates to come)

vg_<cipher>_ofb_final: the generic fbFinal with OFB's step, as #4394's CFB
finals, whose tail leaves O_n in the IV; the target-independent OFB lemmas
in Proof/Modes/FbFinal.lean and CfbFinalScratch.lean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…EED, SM4 and Triple DES (WIP: crates to come)

As on x86-64: the generic fbFinal with OFB's step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
… SM4 and Triple DES (WIP: crates to come)

x86's seqFinal with OFB's mode; its proof (ofbFinal_wp) in
Proof/Modes/X86/SeqFinal.lean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…D, SM4 and Triple DES (WIP: crates to come)

Arm's seqFinal with seq's OFB mode; its final-segment obligation
(ofb_finalOk) and postcondition in Proof/Modes/Arm/SeqFinal.lean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…EED, SM4 and Triple DES (WIP: crates to come)

PPC64LE's seqFinal with OFB's mode; its proof (ofbFinal_wp) in
Proof/Modes/PPC64LE/SeqFinal.lean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…ED, SM4 and Triple DES (WIP: crates to come)

RISC-V's seqFinal with OFB's mode; its proof (ofbFinal_wp) in
Proof/Modes/RISCV64/SeqFinal.lean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…P: needs the regenerated crates)

Whole blocks stay with the whole-block functions; only the bytes after them
go to vg_<cipher>_ofb_final. The Rust code that ran OFB on the padded last
bytes, and its comment, are deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…tes to come)

vg_aes_ofb_final: AES-CFB's final caller around the whole-block AES-OFB
function, whose tail (the block function on the IV) is OFB's.
vg_<cipher>_ctr_final: a call of the whole-block CTR function on the whole
blocks, then one on a zeroed block in a wiped stack frame, whose first
bytes are XORed into the tail; the counter is left at T_{n+1}. AES's for
every variant; target-independent lemmas in Proof/Modes/CtrFinal.lean,
Proof/AesOfb/Final.lean and Proof/AesCtr/Final.lean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…eds the regenerated crates)

Whole blocks stay with the whole-block functions; only the bytes after them
go to vg_aes_ofb_final, vg_aes_ctr_final, vg_camellia_ctr_final or
vg_sm4_ctr_final (AES's of the variant chosen, whose features select now
also requires). The Rust code that ran OFB or CTR on the padded last bytes
is deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
For every x86 block-function variant, as on x86-64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
… crates to come)

As on x86-64; AES's for every variant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…rates to come)

As on the other targets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
As on the other targets: a call of the framed whole-block CTR function on
the whole blocks, then one on a zeroed block in the wiped save area.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
… crates to come)

As on the other targets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
…crates to come)

As on the other targets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
ofbFinal_of (and RISC-V's ofbFinal_out) move into the target-independent
Proof/Modes/FbFinal.lean; the CTR finals' entry-memory locality lemmas
(Camellia's ctrFinalPre_local and ctrFinalPost_local, SM4's
ctrFinalPost_local) into Proof/{Camellia,Sm4}/CtrFinal.lean. No statement,
code or registration changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
Also renames the x86-64 Camellia CTR proof's cipher_length to
ctr_cipher_length, which clashed with the OFB/CFB final proof's in the same
namespace once the emitter imported both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
@alex
alex added this pull request to the merge queue Nov 7, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 7, 2026
#4428 emits a dispatcher for every function's implementations; the final
functions are new implementations of the OFB and CTR modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHGJJidJdRDMDSSd58W
@alex
alex enabled auto-merge November 7, 2026 14:37
@alex
alex added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit b614087 Nov 7, 2026
38 checks passed
@alex
alex deleted the claude/audit-4139-cfb-partial branch November 7, 2026 15:23
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