Fix ChaCha SIMD feed-forward carry (GH #1362) - #1363
Merged
noloader merged 1 commit intoJul 13, 2026
Conversation
The NEON, SSE2 and Altivec backends applied the per-block counter offset with a 64-bit add after the rounds. A carry from word 12 could therefore alter word 13 of the keystream. Precompute each block's initial state, then use 32-bit feed-forward adds. Add regression vectors covering ChaCha8/12/20 and counter offsets 1, 2 and 3. This matches the earlier AVX2 fix in GH weidai11#1069.
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.
Fixes #1362.
The NEON, SSE2 and Altivec ChaCha backends apply the per-block counter offset with a 64-bit add during the final feed-forward. If word 12 wraps, the carry alters word 13 of the keystream.
MultiBlockSafeonly prevents the input block counter from wrapping. It does not prevent a carry from the completed word 12 value during the feed-forward.This is the same issue fixed for AVX2 in GH #1069. That change corrected
chacha_avx.cppbut left the other three backends unchanged.The fix precomputes each block's initial counter state, then uses 32-bit adds for the feed-forward. It also adds three regression vectors covering ChaCha8, ChaCha12 and ChaCha20 with counter offsets 1, 2 and 3.
The affected code is used by ChaCha, ChaChaTLS, XChaCha20 and ChaCha20Poly1305.
Verification
The output was compared with Bernstein's
chacha-ref.cand OpenSSL 3.5.6. The fixed code matches both. The unpatched SSE2, NEON and Altivec backends fail the triggering cases.SSE2 and AVX2 were tested natively. NEON and Altivec were tested under QEMU. The new vectors fail on an unpatched SSE2 build and pass with the fix. AVX2 passes both because it was corrected in GH #1069.
cryptest tv chachapasses with the fix and fails exactly the three new vectors without it.