ChaCha20 is a software-friendly stream cipher. It generates a keystream from a 256-bit key, 12-byte nonce, and counter, then XORs that keystream with plaintext to produce equal-length ciphertext. section 4.5.3
Encryption follows a short sequence:
- Call the block function repeatedly, increasing the counter each time; each call produces 64 keystream bytes. section 4.5.3
- Truncate the keystream to the plaintext length and XOR the two byte sequences. section 4.5.3
- Decrypt by generating the same keystream and XORing it with the ciphertext. section 4.5.3
ChaCha20 is commonly paired with Poly1305 as ChaCha20-Poly1305: ChaCha20 encrypts and derives keys, while Poly1305 authenticates ciphertext and associated data. Never reuse a nonce with a key; doing so exposes relationships between plaintexts. section 4.5.3
Figure 4.18 ChaCha20 works by taking a symmetric key and a unique nonce. It then generates a keystream that is XORed with the plaintext (or ciphertext) to produce the ciphertext (or plaintext). The encryption is length-preserving as the ciphertext and the plaintext are of the same length.
Trace the key-and-nonce input, keystream generation, XOR operation, and length-preserving output. section 4.5.3
