Lion Cipher Lab

Build ChaCha20-Poly1305 from Scratch (and Forge an Unauthenticated Stream Cipher)

Why it matters

ChaCha20-Poly1305 is one of the two authenticated ciphers that actually carries the modern internet: it is a mandatory-to-implement cipher suite in TLS 1.3, it secures WireGuard and SSH and QUIC, and it is the fast, constant-time, no-special-hardware alternative to AES-GCM on phones and embedded chips. But the reason it matters for a learner is the lesson underneath it. Encryption hides a message; it does NOT stop an attacker from changing it. A stream cipher like ChaCha20 produces a keystream that you XOR with your data -- which means an attacker who never learns your key can still flip any…

What you'll learn

Steps in this lab

  1. What an AEAD is, in one breath
  2. Why XOR keystream is malleable (the idea before the code)
  3. Set up and build the ChaCha20 quarter-round and block function
  4. Encrypt with ChaCha20 and validate the Section 2.4.2 vector
  5. Build the Poly1305 one-time MAC and validate Section 2.5.2
  6. Assemble the AEAD exactly as RFC 8439 Section 2.8 specifies
  7. BEFORE: forge a message against ChaCha20 ALONE
  8. AFTER: the AEAD rejects the exact same tampering
  9. Honest negatives: the sharp edges that cause real CVEs
  10. The judgment call: 'encrypted' is not 'safe'

Open the full interactive lab →