The Double Ratchet
A key for every message
The Double Ratchet (Signal’s specification, revision 4 of 2025) encrypts every message under a different key and deletes it right after use. Message keys (MK) come from an HMAC chain: MK = HMAC(CK, 0x01), and the next chain key is CK = HMAC(CK, 0x02). HMAC only runs one way, so today’s CK does not lead back to yesterday’s keys. That is the first ratchet, the symmetric one. On its own it is not enough: whoever steals CK computes every later key. So every message carries the sender’s X25519 public key in its header. When a new one arrives, the receiver takes a step of the second ratchet, the Diffie–Hellman one: it computes ECDH with its private key, generates a new key pair and computes ECDH again. Both outputs go into the root key chain through HKDF — the old root key is the salt — and give a new receiving and sending chain. The parties take turns replacing their key pairs, like ping-pong (§2.3).
The header also holds the message’s number in its chain (N) and the length of the previous chain (PN). With them the receiver of a message that overtook others runs the chain ahead, stores the skipped keys in MKSKIPPED and opens the late messages afterwards (§2.6). Send a few messages as Alice and deliver them out of order; Bob’s reply shows the DH ratchet step. Every message is encrypted with AES-256-GCM under a key and nonce from HKDF(MK), with the header in the associated data. The receiver works on a copy of the state and stores it only after the tag checks out (§3.5) — which is why a replayed message is refused: its key is gone. The specification recommends SIV or AES-CBC with HMAC, which resist a key accidentally used twice. Here it is AES-GCM: §3.1 asks only for an AEAD and allows a nonce derived from the message key, and every message key encrypts exactly one message.
Alice and Bob: every message under a new key
The message must be 1 to 1024 bytes of UTF-8.
Alice and Bob are setting up a session…
Stealing the state at time t
The specification promises two things (§2.1). Forward secrecy: whoever steals the state today cannot read messages from before the theft, because their keys are deleted and the chains only run forward. Post-compromise security (“break-in recovery”): after a theft the ratchet heals once a DH output the attacker does not know goes into the computation. Check it below. The attacker records the whole conversation and, at a moment you choose, copies Alice’s whole state from her phone, ratchet private key included. She really tries to decrypt every recorded message with what she stole. She reads the rest of both current chains. She also reads Bob’s next chain, because Bob’s new public key meets Alice’s stolen private key in X25519. She is lost only at Alice’s new key pair: she cannot compute that X25519 output without either of the two private keys, and without it she knows no root key any more. With the theft right after B₃ (where the toy starts) the key of the delayed B₂ waits in MKSKIPPED, so the attacker reads a message sent before the theft — the risk §8.4 warns about.
Stealing Alice’s state at time t
Alice and Bob are talking, you are recording…
The healing works against an attacker who only listens (§8.2). Whoever stole the keys can also impersonate Alice, or sit between the parties and substitute her own ratchet keys — which is why a device suspected of compromise must be replaced. The second limit is a future quantum computer. The X25519 public keys travel in clear in the headers, and such a computer would compute the DH outputs from them. Whoever steals the state and records the conversation today, and gets one later, walks through every ratchet step: against her the classic Double Ratchet’s healing gives no protection (§8.11). That is why Signal runs a post-quantum ratchet on ML-KEM next to it and mixes the keys of both (ML-KEM station, hybrid schemes).
Where the Double Ratchet runs
- SignalSince October 2025 Signal has been rolling out the Triple Ratchet: the Double Ratchet and the post-quantum SPQR ratchet on ML-KEM run side by side, and the message key mixes keys from both. It is meant to protect against harvest-now-decrypt-later attacks.Source (Signal)
- WhatsAppWhatsApp’s end-to-end encryption is based on the Signal Protocol: Message Key = HMAC-SHA256(Chain Key, 0x01), Chain Key = HMAC-SHA256(Chain Key, 0x02), and with each message round trip a new Curve25519 ECDH output goes into HKDF together with the root key. A message key is 80 bytes: an AES-256 key, an HMAC-SHA256 key and an IV.Source (WhatsApp)
- Matrix — OlmOlm from the Matrix ecosystem is an implementation of the Double Ratchet: HKDF-SHA-256 with the info “OLM_RATCHET” for the root key, HMAC-SHA-256 over the bytes 0x01 and 0x02 for the chain, AES-256-CBC with HMAC-SHA-256 truncated to 64 bits for messages.Source (Matrix — Olm)
- XMPP — OMEMOEnd-to-end encryption in XMPP (XEP-0384) uses the Double Ratchet with X3DH: KDF_RK is HKDF-SHA-256 with the info “OMEMO Root Chain”, encryption is AES-256-CBC with HMAC-SHA-256. It recommends keeping up to 1000 skipped message keys per session.Source (XMPP — OMEMO)
A vetted library, keys deleted at once
Do not write the ratchet yourself — take a vetted implementation: in the Matrix ecosystem vodozemac (Olm and Megolm in pure Rust, audited by Least Authority), in Signal libsignal (Rust with Java, Swift and TypeScript bindings; its README states that use outside Signal is unsupported). If you build your own protocol, follow §3 and §7.2 of the specification: a separate info in every HKDF, the message key deleted right after use, the state stored only after a valid tag, the header in the associated data. Limit skipped keys — MAX_SKIP per chain and a per-session cap, e.g. 1000 — and delete them after a while or after a few ratchet steps (§8.4). Plan a new protocol with a post-quantum ratchet from the start, like the Triple Ratchet.
// KDF_CK (§7.2): two HMAC-SHA-256 under the chain key — 0x01 gives the message key, 0x02 the next chain key
async function kdfCk(chainKey) {
const key = await crypto.subtle.importKey(
'raw', chainKey, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
const step = async (c) => new Uint8Array(await crypto.subtle.sign('HMAC', key, Uint8Array.of(c)));
return { nextChainKey: await step(0x02), messageKey: await step(0x01) };
}Do not encrypt a whole conversation under one session key: its leak opens the whole history and the whole future. Do not keep message keys, old chain keys or skipped keys “just in case” — each of them is a message to read after a device is stolen. Do not advance the state before checking the tag: a replay or a forged header would burn the keys of genuine messages. Do not treat healing as protection from an active attacker — after stealing the keys she can substitute her own ratchet keys; comparing safety numbers and replacing the device are separate safeguards (§8.2).
// ✕ one key for the whole conversation: a leak = the whole history and the whole future
const sessionKey = await deriveSessionKey(sharedSecret);
// ✕ skipped keys without a limit or an expiry: a stash for a thief and a door to DoS
skipped.set(`${header.dh}:${header.n}`, messageKey);
// ✕ state stored before the tag is checked: a replay moves the chain
state = nextState;
const plaintext = await decrypt(messageKey, ciphertext);