E2EE Messaging: X3DH & the Double Ratchet
How X3DH pre-key bundles establish offline sessions and how the Double Ratchet combines symmetric KDF chains with DH steps for self-healing security.
In this lesson (3 bite-sized sections)
Asynchronous session setup with X3DH
TLS assumes both peers are online at the same instant to trade ephemeral keys. Messaging apps cannot assume that: Alice often texts Bob while Bob's phone is in airplane mode. The Signal Protocol solves asynchronous key agreement with X3DH (Extended Triple Diffie-Hellman). Before going offline, Bob uploads a public pre-key bundle to the untrusted relay server containing three things: his long-term Identity Key (IK), a periodically rotated Signed Pre-Key (SPK) signed by IK, and a batch of single-use One-Time Pre-Keys (OPKs).
When Alice writes to Bob, she fetches Bob's bundle, verifies the signature on SPK, and runs three or four DH exchanges combining her Identity Key and a fresh ephemeral key with Bob's IK, SPK, and one popped OPK (the server deletes that OPK immediately so no second caller reuses it). Alice derives the initial root key, encrypts her first message, and sends it to the relay. Even if the server is malicious, it never sees any private key and cannot decrypt the message; the only power it has is omitting an OPK, which degrades initial forward secrecy slightly until Bob replies.
X3DH pre-key server and Double Ratchet message relay: an architecture diagram of 4 components and 4 connections. Full description follows.
- Alice device (IK_A, ratchet state): client.
- Pre-key bundle store (IK_B, SPK_B, OPK_B): database.
- Untrusted message relay (ciphertext only): service.
- Bob device (deletes used OPK_B): client.
- Bob device (deletes used OPK_B) sends asynchronously to Pre-key bundle store (IK_B, SPK_B, OPK_B) (publishes public pre-key bundle).
- Alice device (IK_A, ratchet state) calls synchronously Pre-key bundle store (IK_B, SPK_B, OPK_B) (fetches bundle + pops 1 OPK).
- Alice device (IK_A, ratchet state) sends asynchronously to Untrusted message relay (ciphertext only) (X3DH header + ratcheted AEAD msg).
- Untrusted message relay (ciphertext only) sends asynchronously to Bob device (deletes used OPK_B) (delivers ciphertext when online).
Two ratchets: Forward Secrecy and Post-Compromise Security
Once X3DH establishes a shared Root Key, the Double Ratchet protects every message in an ongoing conversation by combining two ratchets:
- Symmetric KDF chain ratchet (every message): Each sent message advances a chain key via HMAC/HKDF into
(next_chain_key, message_key), then immediately deletes the old chain key and message key. If an attacker steals Bob's phone state today, they cannot roll the one-way KDF backward to read older messages (Forward Secrecy). - Diffie-Hellman ratchet (every turn change): Every message header carries the sender's fresh ephemeral DH public key. Whenever the conversation ping-pongs from Alice to Bob and back, both sides run a new X25519 DH step and mix the fresh shared secret into the Root Key. If an attacker stole Alice's entire ratchet state at message 10 (passive compromise), one unanswered turn exchange injects fresh randomness the attacker does not know, locking them out of message 12 onward. That self-healing property is Post-Compromise Security (PCS), also called Future Secrecy.

Out-of-order delivery and skipped message keys
Mobile networks drop, duplicate, and reorder packets constantly. If message 5 arrives before message 3, Bob cannot simply advance his chain key past 3 and throw MK3 away, or message 3 becomes permanently unreadable. The Double Ratchet solves this by caching skipped message keys in a bounded local dictionary keyed by (sender_dh_pub, msg_number). Bob advances the chain to 5, stores MK3 and MK4, decrypts 5 immediately, and deletes MK3 the instant message 3 finally arrives. Bounding the maximum skip count (e.g., 1,000 keys) and expiring old skipped keys prevents a malicious sender from sending msg_number = 999,999,999 to trigger a memory or CPU DoS on Bob's phone.
Match each Signal Protocol key to the guarantee or lifecycle role it serves.
Sort 4 more items to check
Recap: X3DH establishes authenticated E2EE sessions while recipients are offline using IK, SPK, and single-use OPKs; the Double Ratchet pairs per-message KDF steps (Forward Secrecy) with per-reply DH steps (Post-Compromise Security), bounded by a MAX_SKIP cache for reordered packets.
Sources: Signal X3DH Specification · Signal Double Ratchet Algorithm · Signal PQXDH Extension
Apply
Your turn
The task this lesson builds to.
Architect the end-to-end encrypted 1:1 messaging protocol and relay backend for a consumer chat app where recipients are frequently offline and cellular networks regularly reorder packets.
Think about
- What exact keys does the server store per user so senders can start E2EE sessions offline?
- How do symmetric chain ratchets and DH ratchets separate Forward Secrecy from Post-Compromise Security?
- How does the client handle out-of-order messages without opening a memory or CPU DoS vector?
Draft your design answer here Save your draft, compare against the model answer, and track progress.
Practice
Make it stick
A second problem on the same idea, so it survives past today.
Investigate an incident where an attacker compromised a chat relay server for 48 hours, stripped all One-Time Pre-Keys from served X3DH bundles, and logged every encrypted ciphertext. Determine what the attacker can and cannot decrypt, and specify how to upgrade the protocol against future harvest-now-decrypt-later quantum threats.
Think about
- What exact property is weakened when the server omits OPKs from X3DH bundles?
- Can the relay decrypt messages if it never obtained Bob's SPK private key?
- How does PQXDH (hybrid X25519 + ML-KEM) close the harvest-now-decrypt-later window?
Draft your design answer here Save your draft, compare against the model answer, and track progress.