Optimize hash_pbkdf2() by reusing HMAC contexts #21174
Closed
+285
−26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #9604.
This optimizes
hash_pbkdf2()inext/hashby avoiding repeated HMAC key-block hashing work across PBKDF2 rounds.What changed
php_hash_hmac_round_with_copy).ext/hash.PHP_SHA256Final32FromContext()for fixed-size inner/outer follow-up rounds.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)hrtime(true)), median reported.Environment:
masterbuilt with--disable-all --enable-cliResult summary on this machine:
135.47 ms28.28 ms(sample medians ranged27.68–28.46 ms)~4.79x