Skip to content

reject out-of-range digest_size in the generichash bindings - #980

Open
iefa-m wants to merge 1 commit into
pyca:mainfrom
iefa-m:blake2-digest-size-range
Open

iefa-m wants to merge 1 commit into
pyca:mainfrom
iefa-m:blake2-digest-size-range

Conversation

@iefa-m

@iefa-m iefa-m commented Nov 23, 2026

Copy link
Copy Markdown

Blake2State keeps whatever digest_size it is handed and _checkparams only checks the upper bound, so generichash_blake2b_final can hand libsodium an out-of-range output length. blake2b_final treats outlen == 0 or outlen > 64 as misuse and calls sodium_misuse(), so these abort the interpreter with SIGABRT instead of raising (#967, #964):

from nacl.bindings.crypto_generichash import Blake2State, generichash_blake2b_final
generichash_blake2b_final(Blake2State(0))    # exit 134
generichash_blake2b_final(Blake2State(65))   # exit 134

Blake2State(256) aborts the same way through the (uint8_t) cast in crypto_generichash_blake2b_final, and that 1..64 range is also what keeps ffi.buffer(_digest, state.digest_size) inside the 64-byte buffer the function allocates from crypto_generichash_BYTES_MAX.

Checking at the three points where a digest size enters the bindings means no caller can reach blake2b_final with a size it rejects, including a state built directly rather than through generichash_blake2b_init. The lower bound is 1 and not crypto_generichash_BYTES_MIN: libsodium's C API accepts 1 to 64, and the blake2 vectors in tests/data go down to a 1-byte digest, so enforcing 16 would break working callers. #886 covers that question separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant