End-to-End Encryption in Chat Apps: Signal Protocol Basics

How end-to-end encrypted messaging works: identity keys, prekeys, X3DH key agreement, the Double Ratchet, forward secrecy, safety numbers and group sender ke...

Published November 27, 2026

End-to-end encryption (E2EE) means only the sender and recipients can read messages; the server relays ciphertext it cannot decrypt. WhatsApp, Signal and others use the Signal Protocol, which adds two important properties on top of basic encryption: forward secrecy (stealing today's keys does not reveal past messages) and post-compromise security (the conversation heals after a key compromise). This article explains the moving parts without the heavy math.

The concept

Every device has a long-term identity key pair and uploads a bundle of public prekeys to the server. To start a conversation with someone who may be offline, the sender downloads their prekey bundle and runs X3DH (Extended Triple Diffie-Hellman), combining several Diffie-Hellman exchanges into a shared secret that only the two devices can compute. The server stores and hands out public keys but never sees private keys or the resulting secret.

From that shared secret, the Double Ratchet derives a fresh key for every single message. A symmetric ratchet advances a chain key with each message, and a Diffie-Hellman ratchet mixes in new key material whenever the conversation changes direction. Old keys are deleted, which gives forward secrecy, and new DH exchanges mean an attacker who steals a key loses access again after a few messages.

Starting a session with X3DH

AliceKey serverBobupload identity key + prekeysfetch Bob's prekey bundleX3DH → shared secretinitial message (ciphertext + Alice's keys)relay ciphertextX3DH, decrypt
Setting up an encrypted session with an offline recipient.

The Double Ratchet

  1. Shared root key from X3DH
  2. Each message: chain key → message key (symmetric ratchet)
  3. Reply with a new DH public key → new root and chain keys (DH ratchet)
  4. Delete used message keys immediately
  5. Compromise of one key exposes at most a few messages
The Double Ratchet derives a new key for every message.

Security properties

PropertyWhat it meansProvided by
ConfidentialityServer and network see only ciphertextAEAD encryption with per-message keys
AuthenticationYou are talking to the holder of that identity keyIdentity keys, safety numbers
Forward secrecyPast messages safe if current keys leakDeleting ratchet keys
Post-compromise securityFuture messages become safe againFresh DH ratchet steps
Asynchronous startStart chatting while recipient is offlinePrekeys and X3DH

What the server still knows

E2EE protects message content, not metadata. The server still sees who talks to whom, when, how often, IP addresses, and approximate message sizes unless additional protections such as sealed sender or padding are used. Backups are another weak point: cloud backups of chat history must be encrypted with a key the provider does not hold, otherwise they bypass E2EE entirely.

Practical examples

Safety number verification

Users compare a fingerprint derived from both identity keys to detect a man-in-the-middle.

Safety number with Bob:
37281 90124 55609 13377 82041
66210 09431 77125 40986 21553
(scan QR code in person, or compare over a trusted channel)

Conceptual message key derivation

Illustration of a symmetric ratchet step using HKDF; real implementations use libsignal.

import hmac, hashlib

def kdf_chain(chain_key: bytes) -> tuple[bytes, bytes]:
    message_key = hmac.new(chain_key, b"\x01", hashlib.sha256).digest()
    next_chain  = hmac.new(chain_key, b"\x02", hashlib.sha256).digest()
    return next_chain, message_key   # delete chain_key after this step

Use a vetted library

Never implement the protocol yourself for production.

# Official implementation (Rust core with Java, Swift and TypeScript bindings)
https://github.com/signalapp/libsignal

Common mistakes

  • Rolling your own cryptographic protocol instead of using libsignal or MLS implementations.
  • Encrypting messages but uploading plaintext backups to the cloud.
  • Putting decrypted message previews into push notification payloads.
  • Ignoring identity key changes; clients should warn users when a contact's key changes.
  • Assuming E2EE hides metadata such as who talks to whom.

Key takeaways

  • The server distributes public prekeys but never sees private keys or plaintext.
  • X3DH creates a shared secret even when the recipient is offline.
  • The Double Ratchet gives a new key per message, forward secrecy and self-healing.
  • Metadata, backups and push payloads need separate protection.

FAQ

How do group chats work with E2EE?

Signal-protocol apps use sender keys: each member shares a sender key with the group pairwise, then encrypts group messages once. The newer MLS standard (RFC 9420) is designed for efficient large-group E2EE.

Can a provider read messages if forced to?

Not past messages under correct E2EE. A provider could theoretically push a malicious app update or add a hidden device, which is why key-change warnings and transparency measures matter.

Does E2EE work across multiple devices?

Yes. Each device has its own identity keys, and senders encrypt for every device of every recipient.

More Architecture articles