Repository navigation
ChaCha7539Engine: allow the last counter block and refuse all keystream after it - #2571
Open
AlexLaroche wants to merge 2 commits into
Open
AlexLaroche wants to merge 2 commits into
AlexLaroche wants to merge 2 commits into
Conversation
Contributor
Author
Contribution statementUse of generative AI. I used Claude Code (Anthropic's coding agent, model Claude Opus) to help with this pull request. It helped find and reproduce the issue, write the change and its regression test, run the existing tests, and write this description. I chose what to submit and I am responsible for it. Review. I have reviewed every line of this change, including the tests and the release note, and I understand what it does and why. I checked that the new test fails on Representations under CONTRIBUTING.md. By submitting this pull request I make the representations in the project's contributing guidelines, including the following:
|
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
ChaCha7539Engine(RFC 8439, 32-bit block counter) mishandles the end of the counter range in three ways:Salsa20Engine.processBytes/returnBytecalladvanceCounter()as soon as a block's last byte is used, andChaCha7539Engine.advanceCounter()throws when the counter wraps. So a call that ends exactly on the last byte of block0xFFFFFFFF, which RFC 8439 allows, throws even though all its output was valid. Starting at0xFFFFFFFF, 1 and 63 bytes work but 64 bytes throw. Starting at0xFFFFFFFE, 127 bytes work but 128 throw.engineState[12]has already wrapped to 0 and the buffer still holds block0xFFFFFFFF's keystream. A caller that catches the exception and keeps encrypting gets block0xFFFFFFFF's keystream a second time, then block 1, 2, … again.These are all reachable through the public
SkippingStreamCipherAPI (seekTo/skip). From counter 0 they only matter at the 256 GiB limit, whichChaCha20Poly1305'sDATA_LIMIT = (2^32 - 1) * 64also assumes is fully usable. Checked onmain(e03fee0) and bcprov 1.87, with the Wycheproofchacha20_counter_test.jsonvectors (C2SP/wycheproof, ChaCha20-IETF group, tcIds 29–42).Fix (
ChaCha7539Engineonly)counterExhaustedflag records that the counter has moved past0xFFFFFFFF. With it set,getCounter()returns2^32instead of the wrapped 0, sogetPosition()reports2^38, the end of the stream.processBytes,returnByteandskipcheck the request against the bytes left (2^38 - getPosition()) before doing anything. A request that would go past the end throws the sameIllegalStateException("attempt to increase counter past 2^32.")as before, but leaves the output and the position untouched.advanceCounter/retreatCounter(both forms),getCounterandresetCountergo through onesetCounter(long). The counter can now reach exactly2^32, the exhausted state, but never pass it. Retreating from the end clears the flag, soskip(-n)from the end works.reset(),init()andseekTo()clear it too.seekTo(2^38)(exactly the end of the stream) is now accepted. Anything past it is still refused.XChaCha20Engineinherits the change. No public API changes.Tests
New
ChaCha7539CounterLimitTest(TestCase, registered incrypto.test.AllTests), public API only. Expected output is the Wycheproof keystream for tcIds 35 and 39 (all-zero key, nonce and message). It covers:processBytesandreturnByte;seekToto the end, andskip/seekTopast it;skipbackwards from the end;resetrestarting at block 0.All 9 fail on
main(8 with the earlyIllegalStateException, 1 because output was written before the refusal). All pass with the change.ChaChaTest,XChaCha20Test,ChaCha20Poly1305Test,XChaCha20Poly1305Test,Salsa20Test,XSalsa20Test,StreamCipherOverlapTest,SymmetricConstraintsTestandBigSkippingCipherTest.testSkipFromNonZeroCounterCarry, the onlyBigSkippingCipherTestcase usingChaCha7539Engine, all pass. The rest ofBigSkippingCipherTest(AES-CTR, Salsa20, 64-bit ChaCha) doesn't touch this class, and I stopped it after 35 minutes.:core:compileJava :core:compileTestJava :core:checkstyleMainpass on JDK 25. The change uses no post-1.4 language features or APIs. I didn't run theprovortlssuites.Release note added to
docs/releasenotes.md(1.88, Defects Fixed).The same defect in bc-csharp is bcgit/bc-csharp#757.