[php-src] PR #21174: Optimize hash_pbkdf2() by reusing HMAC contexts
| From: | gregPDN | Date: | Mon, 09 Feb 2026 03:04:22 +0000 |
| Subject: | [php-src] PR #21174: Optimize hash_pbkdf2() by reusing HMAC contexts | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-34863@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/21174
Author: gregPDN
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