Skip to content

3DES PPC64LE: CBC and CFB64 decryption 64 blocks at a time - #4679

Closed
alex wants to merge 6 commits into
claude/ppc64le-block-ciphersfrom
claude/ppc64le-3des-cbc-cfb
Closed

alex wants to merge 6 commits into
claude/ppc64le-block-ciphersfrom
claude/ppc64le-3des-cbc-cfb

Conversation

@alex

@alex alex commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Depends on #4671 (the PPC64LE batched-modes framework, Impl/Modes/PPC64LE/Wide.lean, from the Camellia/SEED/SM4 work). This PR's base is #4671's branch, and it stays a draft until #4671 merges; then I'll restack it onto main.

It also builds on #4666 (the bitsliced ECB), now merged. It carries #4666's two commits until the base branch takes main's squash of them.

Finding 4 of the PPC64LE audit (#4141 (comment)): 3DES CBC and CFB64 decryption call the block function once per block, at about 5.6 µs a block. x86-64 batched them in #4416.

What changes

vg_triple_des_cbc_decrypt_chained and vg_triple_des_cfb_decrypt on ppc64le now take 64 blocks a step while at least 64 are left, then one block a step (Core.seq2, with Mode.cbcDecWide and cfbDecWide).

The core. wideCore d has a 64-block buffer. Its block cipher is a call of the new internal functions vg_triple_des_ecb64_{en,de}crypt (Bitslice.ecb64): #4666's bitsliced ECB on exactly 64 blocks, as one batch with the copies unrolled. ECB leaves the scratch buffer's and the schedule's addresses in r10 and r7, and the core restores sb and r3 from there.

Why a call, and why unrolled:

  • Branch range. Inlined, the 32 KB of ECB put the modes' loops out of reach of PPC conditional branches (±32 KB). A bl reaches.
  • Constant time. ECB saves r29–r31 (the modes' state) in the scratch buffer and restores them. With every store at a fixed offset, slotTaint sees those slots come back public, as for SM4's modes. The variable-length copy loops of ecb would make it forget them.

Cost of the tail. A batch costs the same however many blocks it holds, so each block left after the last group of 64 costs a whole batch. That is about 4.8 µs, slightly less than the block function's 5.8 µs for one block.

Stack. The working space grows from 72 to 200 words, so CBC and CFB64 decryption take a 1632-byte frame instead of 608. The generated dispatchers' safety comments say so; the Rust is unchanged. The decryption block function vg_triple_des_decrypt_block is now unused (CMAC and the serial modes encrypt), and the emitter dropped it.

Proofs

  • Proof/TripleDes/PPC64LE/Bitsliced/Ecb64.lean: ecb64_wp reuses 3DES PPC64LE: bitsliced ECB, 64 blocks at a time #4666's setup, transposition, passes and restore. The unrolled copies are checked by evaluation over the variable domain.
  • Proof/TripleDes/PPC64LE/ModeCoreWide.lean: wideCoreSpec, the CoreSpec for the 64-block core.
  • ModesVerified.lean, ModesFrame.lean: the 200-word contracts and frames. Correctness comes from cbcDecWide_wp and cfbDecWide_wp.
  • ModesConstantTime.lean: slotTaint for the two functions.

No TCB or Spec changes.

Benchmarks

Scoped run (powerpc64, triple_des CBC and CFB64 groups; base 9de9b1b9, which is #4671's branch plus #4666): https://github.com/pyca/verified-garbage/actions/runs/42513716834, on POWER9.

Benchmark Base Head Change
triple-des-cbc-decrypt/64 46.41 µs 38.16 µs 17.8% faster
triple-des-cbc-decrypt/1024 719.20 µs 11.11 µs 98.5% faster
triple-des-cbc-decrypt/16384 11.49 ms 156.67 µs 98.6% faster
triple-des-cfb64-decrypt/64 46.43 µs 38.49 µs 17.1% faster
triple-des-cfb64-decrypt/1024 719.21 µs 10.96 µs 98.5% faster
triple-des-cfb64-decrypt/16384 11.48 ms 154.38 µs 98.7% faster
triple-des-cbc-encrypt/*, triple-des-cfb64-encrypt/* within 2% (unchanged)

At 16 KiB both are about 73× faster. The audit measured OpenSSL's CBC and CFB64 decryption at about 11.4 ms / 14×, i.e. about 800 µs, so these are now about 0.2× OpenSSL, as ECB is.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHFbSpgWBYWAsEnfUks

ECB on ppc64le called the block function once per block (about 5.6 µs a
block, 9-15x OpenSSL; finding 4 of the PPC64LE audit in #4141). It now
encrypts and decrypts 64 blocks at a time, bitsliced in 64-bit GPRs, as on
RISC-V: the blocks are copied into the scratch buffer's state words and
transposed, the S-boxes run as Rusakov's circuits (`BitsliceCircuit`),
allocated to registers for this model's and/or/xor (NOT and AND-NOT
through a register of all ones), and the state is transposed back.

The functions keep the contract and the frame they had
(`ecbScratchContract`, `ecb_framed`), so the Rust code is unchanged. The
block-at-a-time ECB loop and its proofs are gone; the modes and CMAC still
call the block functions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFbSpgWBYWAsEnfUks
`ecb_wp` assumed the contract's exact lists of readable and writable
regions. It now needs only the schedule readable and the blocks and the
scratch buffer writable, and also gives the regions it writes and, when
there are blocks, the scratch buffer and the schedule's addresses left in
`r10` and `r7`, so that the modes can run the same code on their buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFbSpgWBYWAsEnfUks
…es regenerating)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFbSpgWBYWAsEnfUks
…its own module

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFbSpgWBYWAsEnfUks
… full emitter

`lake env lean --run Emit.lean --check` found these files out of date: the
internal functions' modules are declared where the full emitter puts them.

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

alex commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

Superseded by #4694: the same change, with the same tree as this PR's head, but as a linear stack on #4671's head (ad3b520b). This branch had a merge commit, and its own copy of the Camellia/SEED/SM4 crates' regeneration, which #4671 now carries. Closing this one.


Generated by Claude Code

@alex alex closed this Nov 8, 2026
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