Reject oversized digest_size in generichash_blake2b_final() - #965
Open
akashchamp wants to merge 1 commit into
Open
akashchamp wants to merge 1 commit into
akashchamp wants to merge 1 commit into
Conversation
generichash_blake2b_final() trusted state.digest_size without validating it against crypto_generichash_BYTES_MAX before passing it to libsodium. A Blake2State constructed directly (bypassing the parameter validation performed by generichash_blake2b_init()) with a digest_size greater than 64 caused libsodium's blake2b_final() to call sodium_misuse(), aborting the process with SIGABRT instead of raising a Python exception. Add the same upper-bound check already used elsewhere in this module (_checkparams) so the invalid state now raises a ValueError. Fixes pyca#964 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
nacl.bindings.crypto_generichash.generichash_blake2b_final()aborts theprocess with
SIGABRTwhen called on aBlake2Statewhosedigest_sizeexceeds
crypto_generichash_BYTES_MAX(64).Blake2Statecan be constructed directly with an arbitrarydigest_size,bypassing the parameter validation that
generichash_blake2b_init()performs.
generichash_blake2b_final()then passes that uncheckeddigest_sizestraight through to libsodium'scrypto_generichash_blake2b_final(),which ultimately calls
blake2b_final(). That function callssodium_misuse()(which aborts by default) whenoutlen > BLAKE2B_OUTBYTES,crashing the interpreter instead of raising a Python exception.
Fixes #964.
Root cause
generichash_blake2b_final()insrc/nacl/bindings/crypto_generichash.pyhad no equivalent guard, even though the sibling functions
(
generichash_blake2b_salt_personal()andgenerichash_blake2b_init())both validate
digest_sizevia the module's_checkparams()helper beforecalling into libsodium.
Fix
Add the same
digest_size <= crypto_generichash_BYTES_MAXcheck alreadyused by
_checkparams()togenerichash_blake2b_final(), so anout-of-range
digest_sizenow raisesnacl.exceptions.ValueErrorinsteadof aborting the process.
Testing
test_generichash_blake2b_final_rejects_oversized_directly_constructed_statein
tests/test_generichash.py, which constructs aBlake2Statewithdigest_size = crypto_generichash_BYTES_MAX + 1and assertsgenerichash_blake2b_final()raisesValueErrorinstead of crashing.pytest tests/ -n auto --dist=worksteal→ 4662 passed, 10 skipped (skips are pre-existing/environment-only,
e.g. "Requires minimal build of libsodium"), unchanged from before this
change.
ruff check src/ tests/andruff format --check src/ tests/→ clean.mypy→Success: no issues found in 49 source files.Manual verification
Before the fix, the reproducer from the issue crashed the interpreter:
After the fix, the same call raises a normal Python exception:
I also confirmed the normal
generichash_blake2b_init()/generichash_blake2b_update()/generichash_blake2b_final()path isunaffected and still matches the one-shot
nacl.hash.blake2b()output fora
digest_size=64hash ofb"hello world".Scope
This is a minimal, targeted fix: one added bounds check plus one
regression test, no unrelated refactors or dependency changes.
🤖 Generated with Claude Code
Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com