Skip to content

Derive WinZip AES keys with precomputed HMAC pads and only the blocks needed - #2

Merged
xbmc4lyfe merged 1 commit into
mainfrom
claude/pbkdf2
Nov 6, 2026
Merged

xbmc4lyfe merged 1 commit into
mainfrom
claude/pbkdf2

Conversation

@xbmc4lyfe

Copy link
Copy Markdown
Collaborator

What

WinZip AES derives its key and password verifier with PBKDF2-HMAC-SHA1 over 1000 iterations. Each iteration used to re-hash the HMAC key blocks: four SHA-1 compressions and two Vec allocations.

  • Precomputed pads. HmacSha1 compresses the key's ipad and opad blocks once. An iteration is now two compressions over fixed-layout 20-byte-message blocks, with no allocation.
  • Constant folding. sha1_compress is inlined into that path, so the constant padding words fold into the message schedule. The once-per-message paths call it out of line.
  • Rounds. They are spelled out with the five working variables renamed between rounds rather than shifted.
  • Only the needed blocks. PBKDF2's 20-byte output blocks are independent, so pbkdf2_sha1 computes just the blocks covering the requested range:
    • PasswordCheck::accepts derives only the block holding the verifier: 1 block instead of 2, 3 or 4.
    • decrypt_winzip_aes checks the verifier before deriving the key, and never derives the MAC key it doesn't use. A wrong password costs one block; AES-256 decryption costs 3 blocks instead of 4.
  • aes_derive keeps its output. Its length arithmetic is now checked, returning None instead of overflowing.

Measured (fastest of 11 rounds, examples/bench.rs)

case M4 Air M5 Air (load ~180) i7-8700K (load ~20)
aes256-accepts 2.45 → 0.15 ms 2.68 → 0.16 ms 4.20 → 0.29 ms
aes128-accepts 1.22 → 0.17 ms 1.57 → 0.16 ms 2.22 → 0.30 ms
aes-derive-256 2.44 → 0.66 ms 2.55 → 0.69 ms 3.94 → 1.12 ms
aes256-entry-1M 8.58 → 6.62 ms 9.16 → 6.78 ms 31.3 → 29.8 ms

The rest of a whole entry's time is AES-CTR itself.

Correctness

  • SHA-1 matches the FIPS 180 vectors ("", "abc", the 448-bit message, a million 'a's) and the old implementation at every length from 0 to 199 bytes.
  • PBKDF2 matches RFC 6070 (1, 2 and 4096 iterations, and the 25-byte output), and any sub-range equals the same bytes of the whole.
  • aes_derive and aes_verifier match the old implementation, which is kept in the tests as the reference. Cases: empty, short, exactly 64-byte and 100-byte passwords; salts of 0–70 bytes; all three key lengths.
  • The existing ZipCrypto/AES round-trip test passes unchanged.
  • 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

… needed

PBKDF2-HMAC-SHA1 hashed every iteration from scratch: four SHA-1 compressions
and two allocations, for a key block and a 20-byte message that never change
shape. HmacSha1 now compresses the key's two padded blocks once, so an
iteration costs two compressions on fixed-layout blocks, with
sha1_compress inlined there so the constant padding folds into the message
schedule. PBKDF2's output blocks are independent: a password check computes
only the block holding the verifier, and decryption checks the verifier
before deriving the key and never derives the unused MAC key. AES-256 password
check: 2.23 -> 0.16 ms on the M4, 2.37 -> 0.17 ms on the M5, 5.1 -> 0.27 ms
on an i7-8700K.

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:04:27.356878Z 0d73525 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 24e3376 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