Skip to content

Fix ChaCha SIMD feed-forward carry (GH #1362) - #1363

Merged
noloader merged 1 commit into
weidai11:masterfrom
Coralesoft:fix/issue-1362-chacha-simd-carry
Jul 13, 2026
Merged

Fix ChaCha SIMD feed-forward carry (GH #1362)#1363
noloader merged 1 commit into
weidai11:masterfrom
Coralesoft:fix/issue-1362-chacha-simd-carry

Conversation

@Coralesoft

Copy link
Copy Markdown
Contributor

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.

MultiBlockSafe only 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.cpp but 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.c and 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 chacha passes with the fix and fails exactly the three new vectors without it.

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.
@noloader
noloader merged commit 60e2376 into weidai11:master Jul 13, 2026
@Coralesoft
Coralesoft deleted the fix/issue-1362-chacha-simd-carry branch July 16, 2026 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8.9.0 ChaCha ARMv8 NEON backend produces 1-bit incorrect keystream for a specific state

2 participants