ChaCha20-Poly1305
A keystream from 20 rounds
ChaCha20 is Daniel J. Bernstein’s stream cipher (2008) in the variant of RFC 8439: from a 256-bit key, a 96-bit nonce and a 32-bit block counter it produces a keystream that is XORed with the plaintext, as in the one-time pad. Each 64-byte keystream block comes from a 4 × 4 grid of 32-bit words through 20 rounds made only of addition, bit rotation and XOR (ARX). Poly1305 adds a tag to the ciphertext: a polynomial computed modulo 2¹³⁰ − 5 with a one-time key that ChaCha20 derives from the same key and nonce. Together they form an AEAD — authenticated encryption with associated data, like AES-GCM. WebCrypto has no ChaCha20, and the toy has to show the state after every step, so it runs its own implementation.
Toy implementation — do not use in productionOur own ChaCha20 and Poly1305, to show the intermediate state. Checked step by step against the RFC 8439 vectors and compared with OpenSSL in tests. Poly1305 runs on BigInt, so it is not constant-time.
ChaCha20-Poly1305 step by step
The toy encrypts one keystream block: up to 64 B of UTF-8 (“ą” is 2 bytes).
The message is empty. Type at least one character.
Public but protected by the tag — a record number or a header, say. May be empty.
-27 B too long. In the toy the AAD is up to 32 B.
Drawing a key and a nonce…
The same nonce, the same Poly1305 key
ChaCha20-Poly1305 falls to the same mistake as AES-GCM: a repeated nonce. According to RFC 8439 (§4), two messages with the same key and nonce get the same keystream and the same one-time Poly1305 key. The first gives away the XOR of the plaintexts — how to read the messages from it is shown on the XOR and OTP and the AES-GCM stations. The second is worse: Poly1305 is secure only if the key (r, s) serves a single message, and from two tags under the same key it can be computed and used to forge the tag of any ciphertext. Do it in the toy below.
The same nonce twice
A random nonce is enough only if it is long enough. With q messages and random n-bit nonces, the chance that two of them repeat is about q² / 2ⁿ⁺¹ — the birthday paradox from the Hash functions station. For 96 bits that is about 2⁻³³ after 2³² messages, which is why NIST allows at most 2³² encryptions under one key for AES-GCM with random 96-bit IVs (SP 800-38D, §8.3); the same arithmetic applies to the 96-bit nonce of ChaCha20-Poly1305. XChaCha20 has a 192-bit nonce: at the same 2⁻³² risk it allows 2⁸⁰ messages (draft-irtf-cfrg-xchacha-03, §2.1).
The second attack — timing through the processor cache — does not work on ChaCha20. Fast software AES reads tables at addresses that depend on key bytes, and how long a read takes depends on whether the entry was in the cache. In 2005 Bernstein recovered a complete AES key this way from a server using OpenSSL, timing from another computer how it handled packets with known contents (Cache-timing attacks on AES), and Osvik, Shamir and Tromer recovered the key of an encrypted dm-crypt partition after 800 writes, in 65 ms (Cache Attacks and Countermeasures: the Case of AES). ChaCha20 uses only addition, XOR and rotations by fixed amounts, and the order in which it reads the state does not depend on the key (RFC 8439, §4) — compare the two in the toy’s “Break it” mode above. Poly1305 still has to be computed in constant time, which the toy’s BigInt version does not do.
Where ChaCha20-Poly1305 runs
- TLS 1.3RFC 9846 says implementations should support
TLS_CHACHA20_POLY1305_SHA256next to the mandatoryTLS_AES_128_GCM_SHA256. The nonce of each record is a 64-bit sequence number XORed with a static IV (§5.3), so it does not repeat under one key.Source (TLS 1.3) - WireGuardData packets are encrypted with ChaCha20-Poly1305 under a nonce made of 32 zero bits and a 64-bit counter that cannot be wound backward. Cookies are encrypted with XChaCha20-Poly1305 under a random 24-byte nonce.Source (WireGuard)
- OpenSSHThe cipher
[email protected]is first in the OpenSSH client’s default cipher list, ahead of AES-GCM and AES-CTR.Source (OpenSSH) - Cloudflare (2015)Cloudflare prefers ChaCha20-Poly1305 for browsers on devices without AES hardware. On a Galaxy Nexus phone, decrypting 1 MB took 13.2 ms, against 41.6 ms with AES-128-GCM.Source (Cloudflare (2015))
- RFC 8439The CFRG document describes ChaCha20, Poly1305 and their AEAD as a stable reference for IETF protocols. According to it, ChaCha20 in software is around three times as fast as AES on platforms without AES hardware support.Source (RFC 8439)
A library, and random nonces only with XChaCha20
Encrypt with a ready-made AEAD from a library, never your own implementation — the toy’s is not constant-time
and nobody has audited it. If the nonce is to be random, use XChaCha20-Poly1305 from libsodium (in .NET, for
example NSec, a libsodium wrapper): its 192-bit nonce can be drawn at random for every message
(libsodium documentation).
Store the nonce next to the ciphertext, do not truncate the tag (RFC 8439, §4 forbids it), and pass the context
as AAD. With the 96-bit ChaCha20-Poly1305, such as the ChaCha20Poly1305 class in .NET, take the nonce from a
counter that never repeats — like the record number in TLS 1.3.
import sodium from 'libsodium-wrappers';
await sodium.ready;
const key = sodium.crypto_aead_xchacha20poly1305_ietf_keygen();
// a fresh random nonce (24 B) for every message
const nonce = sodium.randombytes_buf(
sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES,
);
const ciphertext = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
message, context, null, nonce, key,
);
// store the nonce with the ciphertext; the tag is at its endvar aead = AeadAlgorithm.XChaCha20Poly1305; // NSec
using var key = Key.Create(aead);
// a fresh random nonce (24 B) for every message
byte[] nonce = RandomNumberGenerator.GetBytes(aead.NonceSize);
byte[] ciphertext = aead.Encrypt(key, nonce, context, plaintext);Do not use your own ChaCha20 or Poly1305 in production, and do not draw random 96-bit nonces for a key that will encrypt billions of messages. Do not keep the nonce counter only in memory: after a restart it starts from zero and repeats nonces, and then — as in “Break it” — the plaintexts and the Poly1305 key leak.
// ✕ a nonce counter kept only in memory: after a
// restart it counts from zero and repeats nonces
let sent = 0;
function nextNonce() {
const nonce = new Uint8Array(12);
new DataView(nonce.buffer).setUint32(8, sent++);
return nonce;
}