Skip to content

Encrypt WinZip AES-CTR keystream with T-tables, two blocks at a time on ARM - #3

Merged
xbmc4lyfe merged 1 commit into
mainfrom
claude/aes-ttable
Nov 6, 2026
Merged

xbmc4lyfe merged 1 commit into
mainfrom
claude/aes-ttable

Conversation

@xbmc4lyfe

Copy link
Copy Markdown
Collaborator

What

WinZip AES-CTR used to encrypt each counter block a byte at a time: per round, 16 S-box lookups, a ShiftRows permutation, and MixColumns via xtime. It then pushed the output one byte at a time.

  • T-tables. Each round is now sixteen lookups in four 256-word tables, plus the round key. The tables (TE0..TE3) are built at compile time from the existing S-box; one lookup does SubBytes, ShiftRows (by which byte is looked up) and MixColumns at once. The last round still uses the S-box.
  • Unrolled rounds. The round count is a const generic (10/12/14), so the rounds unroll and every table index is provably in range.
  • Whole-block XOR. CTR XORs whole 16-byte blocks into a copy of the ciphertext, in place.
  • Blocks side by side (CTR_LANES). 64-bit ARM encrypts two counter blocks at once, so their lookups overlap. x86-64 does one at a time: its 16 registers spill with more.

Interleaving measured per machine (AES-256-CTR, 8 MiB, best of 15–25):

lanes M4 i7-8700K
1 388 MB/s 237 MB/s
2 587 MB/s 179–223 MB/s
4 519 MB/s 192–226 MB/s
8 546 MB/s 216–236 MB/s

Still no unsafe, and no new dependency. Hardware AES (AES-NI, ARMv8 crypto) would need an unsafe call boundary even when the target already enables those features, so it stays out.

Measured (fastest round, examples/bench.rs)

case M4 Air M5 Air (load ~180) i7-8700K (load ~24)
aes128-ctr-8M 242 → 738 MB/s 226 → 705 MB/s 60 → 243 MB/s
aes256-ctr-8M 175 → 567 MB/s 163 → 543 MB/s 44 → 186 MB/s
aes256-entry-1M (derive + decrypt) 8.4 → 4.2 ms 9.0 → 4.6 ms 26.5 → 10.2 ms

Those entry times predate #2, which cuts the derivation further.

Correctness

  • AES-128/192/256 match the FIPS-197 appendix C vectors.
  • CTR output matches the old byte-wise implementation, kept in the tests as the reference, for all three key sizes. Inputs: a 1 MiB + 4133-byte buffer (crossing the counter's carries into its second and third bytes), every length 0–199, and lengths around 4 KiB and 64 KiB.
  • The reference test fails when the counter sequence is broken on purpose.
  • A bad key length still gives None.
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings and cargo test pass on aarch64-apple-darwin and x86_64-linux.

🤖 Generated with Claude Code

…on ARM

AES went a byte at a time: SubBytes, a ShiftRows permutation and MixColumns
with xtime on every round, then one push per output byte. Each round is now
sixteen lookups in four 256-word tables, built at compile time from the
S-box, that do SubBytes, ShiftRows and MixColumns together, on big-endian
state words, with the round count a const generic so the rounds unroll. CTR
XORs whole 16-byte blocks in place. On 64-bit ARM two counter blocks go side
by side so their lookups overlap (13% faster than four on the M4, 50% faster
than one); x86-64 runs one, since its sixteen registers spill with more.
AES-128-CTR: M4 242 -> 738 MB/s, M5 226 -> 705, i7-8700K 60 -> 243.
AES-256-CTR: M4 175 -> 567, M5 163 -> 543, i7-8700K 44 -> 186.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T19:12:19.959675Z 6deb74f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@xbmc4lyfe
xbmc4lyfe merged commit 3061ce3 into main Nov 6, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant