Skip to content

Conversation

@gregPDN
Copy link

@gregPDN gregPDN commented Feb 9, 2026

Fixes #9604.

This optimizes hash_pbkdf2() in ext/hash by avoiding repeated HMAC key-block hashing work across PBKDF2 rounds.

What changed

  • Reuse precomputed HMAC base contexts in PBKDF2 loops (ipad/opad pre-hash once, copy per round).
  • Add a generic copy-based HMAC helper for PBKDF2 (php_hash_hmac_round_with_copy).
  • Add SHA-256 PBKDF2 fast path in ext/hash.
  • Add SHA-256 helper PHP_SHA256Final32FromContext() for fixed-size inner/outer follow-up rounds.
  • Add regression/vector test: ext/hash/tests/gh9604.phpt.

Functional behavior

No public API changes. hash_pbkdf2() input validation and output semantics are preserved.

Validation

  • make test TESTS="ext/hash/tests/hash_pbkdf2_basic.phpt ext/hash/tests/hash_pbkdf2_error.phpt ext/hash/tests/gh9604.phpt"
  • make test TESTS="ext/hash/tests"

Benchmark

Benchmark script (CLI) used:

  • hash_pbkdf2('sha256', 'password', 'salt', 200000, 64, false)
  • 9 timed runs per sample (hrtime(true)), median reported.

Environment:

  • macOS 15.x arm64
  • php-src master built with --disable-all --enable-cli

Result summary on this machine:

  • Before optimization: median 135.47 ms
  • After optimization: median of 12 samples 28.28 ms (sample medians ranged 27.68–28.46 ms)
  • Speedup: ~4.79x

Fixes php#9604.

- Reuse precomputed HMAC inner/outer contexts in PBKDF2 loops

- Add SHA-256 PBKDF2 fast path and SHA-256 context helper

- Add phpGH-9604 regression vectors
@gregharmon
Copy link

Superseded by #21175 (same patchset from the correct account). Please review #21175.

@gregharmon
Copy link

Superseded by #21175 (opened from my main account). Please ignore this one and review #21175.

@TimWolla TimWolla closed this Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hash_pbkdf2() wastes time by repeatedly hashing the HMAC key blocks

3 participants