Skip to content

Track ChaCha20 32-bit counter and error on overflow#14977

Draft
reaperhulk wants to merge 2 commits into
pyca:mainfrom
reaperhulk:fix-chacha
Draft

Track ChaCha20 32-bit counter and error on overflow#14977
reaperhulk wants to merge 2 commits into
pyca:mainfrom
reaperhulk:fix-chacha

Conversation

@reaperhulk
Copy link
Copy Markdown
Member

ChaCha20 now extracts the 32-bit little-endian block counter from the first 4 bytes of the 128-bit nonce and tracks the number of bytes processed. Encrypting or decrypting more than (2**32 - counter) * 64 bytes would overflow that counter, at which point the underlying implementation silently diverges from RFC 7539. We now raise ValueError instead.

Setting the counter portion of the nonce to zero allows encrypting up to 256 GiB with a given nonce. The docs are updated accordingly and no longer suggest randomizing the full value.

The counter-overflow.txt vectors (and their generator/verifier docs) captured the old carry behavior, which can no longer be reproduced, so they are removed in favor of explicit overflow tests.

ChaCha20 now extracts the 32-bit little-endian block counter from the
first 4 bytes of the 128-bit nonce and tracks the number of bytes
processed. Encrypting or decrypting more than (2**32 - counter) * 64
bytes would overflow that counter, at which point the underlying
implementation silently diverges from RFC 7539. We now raise ValueError
instead.

Setting the counter portion of the nonce to zero allows encrypting up to
256 GiB with a given nonce. The docs are updated accordingly and no
longer suggest randomizing the full value.

The counter-overflow.txt vectors (and their generator/verifier docs)
captured the old carry behavior, which can no longer be reproduced, so
they are removed in favor of explicit overflow tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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