Skip to content

Run WinZip AES key derivation on the x86 SHA extensions - #8

Merged
xbmc4lyfe merged 1 commit into
mainfrom
claude/sha-ni
Nov 6, 2026
Merged

xbmc4lyfe merged 1 commit into
mainfrom
claude/sha-ni

Conversation

@xbmc4lyfe

@xbmc4lyfe xbmc4lyfe commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

What

WinZip AES derives its key and password verifier with PBKDF2-HMAC-SHA1: 999 chained HMACs per 20-byte block, two SHA-1 compressions each. #7 runs those on ARMv8's SHA-1 instructions. This does the same on x86-64 processors with the SHA extensions (SHA-NI), checked at run time with is_x86_feature_detected!("sha"), in accel::pbkdf2_sha1_rest.

  • Instructions. SHA1RNDS4 does four rounds; its immediate picks the round function and constant. It takes e added to the first of its four words. SHA1NEXTE supplies that: the e four rounds on is the a from before them, rotated. SHA1MSG1, a XOR and SHA1MSG2 extend the message schedule four words at a time.
  • Blocks built in registers. A digest's e vector is its fifth word with zeros below. So each iteration's block is the previous digest's two vectors, with the padding bit ORed into the second, then zeros and the 84-byte length. The 1000-iteration loop never touches memory, as on ARM.
  • Fallback. Without SHA-NI the portable loop (HmacSha1::rest) runs as before. That includes the i7-8700K and every Intel desktop core before Ice Lake.

Testing without the hardware

No machine here has SHA-NI: the i7-8700K lacks it, and Rosetta on the Mac doesn't report it. So the new path was run under Intel's Software Development Emulator (10.13.1) on the 8700K.

  • cargo test --release passes under sde64 -icl (Ice Lake). It also passes natively, where the portable path runs.
  • The PBKDF2, SHA-1 vector and AES tests pass under sde64 -glm (Goldmont, an Atom core with SHA-NI but no AVX).
  • Under the emulator, pbkdf2_iterations_agree_with_and_without_sha1_instructions holds this path to the portable loop. It uses random keys of 0–89 bytes and 1, 2, 3 and 1000 iterations. The RFC 6070 and reference aes_derive tests run through it too.
  • Giving the last four rounds the wrong round function on purpose fails four tests under the emulator and none natively, so the tests do reach this path.
  • objdump shows the 40 sha1rnds4 (20 per compression, inlined twice) in the release test binary.
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings and cargo test pass on aarch64-apple-darwin and x86_64-linux.

Speed

Not measured: the emulator's timings mean nothing. The structure is the one that gave ARM 3.7× in #7: twenty 4-round instructions per compression, with no loads or stores in the loop. Many of GitHub's x86 runners (AMD EPYC, Ice Lake Xeon) have SHA-NI, so CI may well run this path on real hardware, though nothing here confirms it. Timings will come once there is a machine with SHA-NI to bench on.

🤖 Generated with Claude Code

PBKDF2's 999 chained HMACs per block now run on the x86 SHA extensions
(SHA-NI) where the processor has them, checked at run time, as they
already do on ARMv8: SHA1RNDS4 does four rounds, SHA1NEXTE gives the next
e from the a before them, and SHA1MSG1, a XOR and SHA1MSG2 extend the
schedule, with each iteration's padded block built in registers from the
previous digest. Without them the portable loop runs, as before.

No machine here has SHA-NI, so this was tested under Intel's Software
Development Emulator (Ice Lake and Goldmont models) on an i7-8700K: the
whole suite passes there and natively, and a wrong round function in the
last group fails four tests under the emulator. Its speed is not measured.

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: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T22:59:59.386797Z 13348ee 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 19ad69e 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