Stream Ciphers
Symmetric Encryption · Networking & Cryptography track
6 min read
In this module
Encrypting Byte by Byte
A stream cipher spins a pseudorandom stream of bytes (the keystream) out of a key and a nonce, then XORs that keystream against the plaintext. Unlike a block cipher, there are no fixed-size blocks — data is encrypted one byte (or bit) at a time as it arrives, like a tap.
keystream = generate(key, nonce) // pseudorandom byte streamciphertext[i] = plaintext[i] XOR keystream[i]Decryption is identical: generate the same keystream and XOR it with the ciphertext.
Stream Ciphers vs. Block Ciphers in CTR Mode
You may have noticed a block cipher in CTR mode looks suspiciously like a stream cipher. You'd be right. CTR mode essentially repurposes a block cipher as a keystream generator. Dedicated stream ciphers chase faster and simpler than block-cipher-based constructions can manage.
The practical split: dedicated stream ciphers are usually tuned for raw software speed, while AES in CTR mode rides on AES-NI hardware acceleration. Different roads, similar destinations.
A stream cipher generates a pseudorandom keystream from a key and nonce, then XORs it with the data. It processes data incrementally without needing to buffer full blocks. Dedicated stream ciphers like ChaCha20 can outperform AES in software when hardware AES instructions are not available.
RC4: A Cautionary Tale
RC4 was the most widely deployed stream cipher for decades. It powered SSL/TLS, WEP/WPA, and a long tail of other protocols. Its keystream comes from a 256-byte internal state that gets permuted with each output byte.
RC4 won hearts because it was tiny (the whole algorithm fits in a few lines of code) and screaming fast. Then, year by year, statistical biases turned up in its output. The first bytes of the keystream are especially lopsided, and those biases can be levered into plaintext recovery.
RC4 is now considered broken. TLS banned it outright in 2015 (RFC 7465), and TLS 1.3 doesn't define it at all. Its story is a recurring lesson: simple and fast aren't enough. The keystream must be genuinely indistinguishable from random, and "looks random to me" is not a security property.
ChaCha20: The Modern Stream Cipher
ChaCha20 is Daniel J. Bernstein's tighter follow-up to his earlier Salsa20 cipher. It operates on a 512-bit state laid out as a 4x4 matrix of 32-bit words. This is the IETF variant from RFC 8439, the one TLS uses; Bernstein's original splits the last row into a 64-bit counter and a 64-bit nonce instead:
State matrix (16 words of 32 bits each):[ constant constant constant constant ][ key key key key ][ key key key key ][ counter nonce nonce nonce ]The state is initialised with:
- 4 words of fixed constant values
- 8 words from the 256-bit key
- 1 word counter (block number)
- 3 words from the 96-bit nonce
ChaCha20 grinds through 20 rounds of quarter-round operations — each one a flurry of additions, XORs, and rotations on the state. The original state then gets added back to the result, and the 512-bit output block becomes the next slice of keystream.
Why ChaCha20 Matters
ChaCha20 has a few properties that matter in practice.
No hardware dependency. ChaCha20 flies in pure software using only additions, XORs, and rotations (ARX operations). On devices without AES hardware — embedded systems, older ARM chips — ChaCha20 leaves AES in the dust.
Constant-time by design: The ARX operations are inherently constant-time on every platform. AES software implementations that lean on lookup tables can leak through cache-timing attacks unless they're written with surgical care.
Widely deployed: Google rolled ChaCha20-Poly1305 into Chrome and Android. TLS 1.3 lists it as a should-implement cipher suite, next to the mandatory AES-128-GCM (RFC 8446, section 9.1). Servers commonly pick it for clients that lack fast AES hardware.
ChaCha20-Poly1305: Authenticated Stream Encryption
The same way AES-GCM bolts authentication onto AES-CTR, ChaCha20-Poly1305 bolts authentication onto ChaCha20. Poly1305 is a fast message authentication code (MAC) that produces a 128-bit tag certifying the ciphertext's integrity.
The combination provides the same security guarantees as AES-GCM:
- Confidentiality from ChaCha20 encryption
- Integrity and authenticity from Poly1305 authentication
Nonce Reuse: Still Fatal
Like CTR mode, stream ciphers fall apart spectacularly under nonce reuse. Encrypt two messages with the same key and nonce, and an attacker XORs the ciphertexts to lift the XOR of the plaintexts — enough to recover both messages with frequency analysis or a known crib.
C1 = P1 XOR keystream(key, nonce)C2 = P2 XOR keystream(key, nonce) // same nonce!
C1 XOR C2 = P1 XOR P2 // key cancels outHere's what an attacker does with that. The program encrypts two messages under the same key and nonce (with a toy keystream generator, not ChaCha20), then crib-drags: it slides a guessed fragment, " THE ", along C1 XOR C2 and prints every offset where the other message comes out as readable text.
"ODE I" looks like the middle of "CODE IS", so the next crib to try is something like "CODE IS" at offset 9. Each correct guess hands you the same stretch of the other message, and the two plaintexts unravel together.
Nonce management is the make-or-break detail in any system using stream ciphers or CTR mode. Use a counter that's guaranteed never to repeat, or pick random nonces with a cipher whose nonce space is wide enough that collisions are vanishingly unlikely (XChaCha20's 192-bit nonce is the comfortable choice).
Choosing Between Block and Stream Ciphers
In modern practice, the line between block and stream ciphers has blurred to the point of being mostly cosmetic. AES in CTR mode acts like a stream cipher (generating keystream, XORing it with plaintext). ChaCha20, a born-and-bred stream cipher, has become the go-to alternative to AES on platforms without hardware acceleration. The decision usually boils down to silicon: AES-GCM wins where AES-NI exists; ChaCha20-Poly1305 wins where it doesn't (think low-end phones, older Arm chips without the ARMv8 crypto extensions, and microcontrollers). Most current phones do have AES instructions, so the gap there has narrowed. Both deliver authenticated encryption. Both are well-vetted. Both are blessed by TLS 1.3. Knowing both gives you the agility to pick the right tool for the platform sitting in front of you.