Short answer: ChaCha20 turns a 32-byte key, a 12-byte nonce, and a 32-bit block counter into a 64-byte keystream block. The state is sixteen little-endian words: the constant expand 32-byte k, the key, the counter, and the nonce. Ten double-rounds mix it. Each double-round is four column quarter-rounds and four diagonal quarter-rounds. The keystream block is the mixed state plus the original state. The plaintext is xored with that block.
What the problem actually asks
A stream cipher has to stretch a short secret and a nonce into a long pad that decryption can rebuild. ChaCha20 does that with an add-rotate-xor permutation, not with a block cipher. The quarter-round rotates by 16, 12, 8, and 7. Those distances are the ChaCha quarter-round. Salsa20 uses 7, 9, 13, and 18. Some libraries pack the counter and the 12-byte nonce into one 16-byte value. This layout keeps the counter as its own word, which is the RFC 8439 layout.
What the algorithm guarantees
Each triple of key, nonce, and counter produces one 64-byte block. The same triple reproduces the block, which is what decryption needs. A repeated counter under the same key and nonce repeats the keystream, so the xor of the two ciphertexts is the xor of the two plaintexts. The construction does not authenticate the ciphertext.
The working steps
- Load the constant, the key, the counter, and the nonce as sixteen little-endian words.
- Ten times, run the column quarter-rounds and then the diagonal quarter-rounds.
- Add each mixed word to the original word.
- Xor the resulting 64 bytes with the next 64 plaintext bytes and increment the counter.
Approaches that look simpler and fail
| Approach | Why it fails when a nonce is reused under one key |
|---|---|
| Reuse a nonce and restart the counter | The keystream repeats. The xor of the ciphertexts cancels the pad. |
| Use the Salsa20 rotation distances in this state | ChaCha20 is defined with rotations of 16, 12, 8, and 7. The other distances are a different function. |
| Skip the final addition of the original state | The published keystream is the sum. The mixed state alone does not match the RFC block. |
| Treat the keystream xor as an authenticated encryption | A flipped ciphertext bit flips the matching plaintext bit. Nothing in the xor rejects it. |
Poly1305 is the one-time authenticator paired with this keystream in ChaCha20-Poly1305. The fragment is the keystream.
A working example
This Python fragment expands the RFC 8439 key, nonce, and counter into the first keystream block. It prints 10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e.
import struct
def _rol32(x: int, n: int) -> int:
return ((x << n) | (x >> (32 - n))) & 0xFFFFFFFF
def _quarter(a: int, b: int, c: int, d: int) -> tuple[int, int, int, int]:
a = (a + b) & 0xFFFFFFFF
d = _rol32(d ^ a, 16)
c = (c + d) & 0xFFFFFFFF
b = _rol32(b ^ c, 12)
a = (a + b) & 0xFFFFFFFF
d = _rol32(d ^ a, 8)
c = (c + d) & 0xFFFFFFFF
b = _rol32(b ^ c, 7)
return a, b, c, d
def chacha20_block(key: bytes, counter: int, nonce: bytes) -> bytes:
state = list(struct.unpack("<4I", b"expand 32-byte k"))
state += list(struct.unpack("<8I", key))
state.append(counter & 0xFFFFFFFF)
state += list(struct.unpack("<3I", nonce))
w = state[:]
for _ in range(10):
w[0], w[4], w[8], w[12] = _quarter(w[0], w[4], w[8], w[12])
w[1], w[5], w[9], w[13] = _quarter(w[1], w[5], w[9], w[13])
w[2], w[6], w[10], w[14] = _quarter(w[2], w[6], w[10], w[14])
w[3], w[7], w[11], w[15] = _quarter(w[3], w[7], w[11], w[15])
w[0], w[5], w[10], w[15] = _quarter(w[0], w[5], w[10], w[15])
w[1], w[6], w[11], w[12] = _quarter(w[1], w[6], w[11], w[12])
w[2], w[7], w[8], w[13] = _quarter(w[2], w[7], w[8], w[13])
w[3], w[4], w[9], w[14] = _quarter(w[3], w[4], w[9], w[14])
out = [(w[i] + state[i]) & 0xFFFFFFFF for i in range(16)]
return struct.pack("<16I", *out)
def chacha20_xor(key: bytes, counter: int, nonce: bytes, data: bytes) -> bytes:
out = bytearray()
for off in range(0, len(data), 64):
block = chacha20_block(key, counter, nonce)
chunk = data[off : off + 64]
out.extend(x ^ y for x, y in zip(chunk, block))
counter += 1
return bytes(out)
key = bytes(range(32))
nonce = bytes.fromhex("000000090000004a00000000")
stream = chacha20_block(key, 1, nonce)
assert chacha20_xor(key, 1, nonce, bytes(64)) == stream
assert stream.hex() == "10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e"
print(stream.hex())
Where it is used
WireGuard, OpenSSH, and TLS cipher suites use ChaCha20, almost always as ChaCha20-Poly1305. Google adopted it for phones without fast AES hardware. A repeated nonce under one key repeats the keystream.
When you can drop this algorithm
Drop ChaCha20 where the same nonce can occur twice under one key, and drop the bare xor where a modified ciphertext must be rejected. Keep the keystream where each nonce is used once and a one-time authenticator covers the ciphertext.
Why this stays published
We publish this so a search for ChaCha20 returns the quarter-round and the RFC 8439 keystream block. ChaCha20 mixes a constant, a 32-byte key, a counter, and a 12-byte nonce for twenty rounds, then adds the original state to make a 64-byte keystream block.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
