Skip to content

Stop ChaCha20 block counter wrap and carry the XChaCha20 counter - #12194

Open
aidangarske wants to merge 2 commits into
wolfSSL:masterfrom
aidangarske:chacha-counter-wrap-12182
Open

aidangarske wants to merge 2 commits into
wolfSSL:masterfrom
aidangarske:chacha-counter-wrap-12182

Conversation

@aidangarske

Copy link
Copy Markdown
Member

F-16888 - ChaCha20 block counter wraps and reuses keystream

  • wc_Chacha_Process let the 32-bit block counter wrap from 0xFFFFFFFF to 0, so later blocks reused keystream under the same key and nonce.
  • This happened on every backend (C, x86, ARM, RISC-V), in a single call and across calls.
  • wc_Chacha_Process now returns CHACHA_POLY_OVERFLOW, without writing any output, when a call needs keystream past block 0xFFFFFFFF of the current IV.
  • XChaCha20 now carries its counter into the next state word, so it works as a 64-bit counter as libsodium and Wycheproof expect. Past 2^64 it is refused.
  • Used blocks are tracked in the C wrapper, so the check does not depend on how each assembly backend handles a partial block.
  • ChaCha gains two bitfields next to keySet and a word32 at the end of the struct, so sizeof(ChaCha) grows by 4 bytes.
  • Callers that ran past 256 GiB under one IV, including through EVP_chacha20, now get an error instead of repeated keystream.

reported in #12182.

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4-crypto-only

  • FLASH: .text +192 B (+0.1%, 184,016 B / 262,144 B, total: 70% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +192 B (+0.1%, 199,012 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +192 B (+0.0%, 808,460 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +192 B (+0.1%, 317,168 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +192 B (+0.1%, 347,688 B / 1,048,576 B, total: 33% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +192 B (+0.1%, 253,895 B / 262,144 B, total: 97% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +192 B (+0.1%, 318,000 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +192 B (+0.1%, 253,895 B / 262,144 B, total: 97% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #12194

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/chacha.h, tests/api/test_chacha.h
Coverage: 3 of 3 in-scope changed file(s) opened by the reviewer

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Max

Comment thread tests/api/test_chacha.c Resolved

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #12194

Scan targets checked: wolfssl-src, wolfssl-bugs
Unchanged since last review (not re-run): wolfcrypt-src, wolfcrypt-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/chacha.h, tests/api/test_chacha.h
Coverage: 1 of 1 in-scope changed file(s) opened by the reviewer

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review November 19, 2026 20:37

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

@aidangarske aidangarske removed their assignment Nov 20, 2026
Comment thread wolfcrypt/src/chacha.c
blocks = (need / CHACHA_CHUNK_BYTES) +
(((need % CHACHA_CHUNK_BYTES) != 0) ? 1U : 0U);
/* An unused counter of 0 leaves 2^32 blocks, more than one call needs. */
avail = ctx->cntExhausted ? 0U : (0U - ctx->nextBlock);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hesitant to add in the extra tracking for if the counter is wrapping on ChaCha20 for these reasons.

  1. Some other libraries handle it by doing a carry over into the next word32. Chacha20 initially had a 64bit counter, but was reduced to a 32bit counter for AEAD purposes. So some libraries still just handle it as a 64bit counter which moves the warp around case out farther.
  2. None of the RFC's, as far as I know, explicitly state what behavior should happen here. Only that applications must not reuse the same nonce/counter value and that there is a limit on the amount of data that can be encrypted. This is similar to AES modes where IV's must not be reused. Right now having the counter wrap is similar to how the counter is handled with AES in wolfSSL.

For XChaCha20 the change to carry over in this PR with the counter is good.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you maybe separate out the XChaCha20 and ChaCha20 counter changes? The XChaCha I think should go in but would like to discuss the ChaCha20 one.

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.

4 participants