Technology · 6 min read
How Encrypted Messaging Establishes and Replaces Shared Keys
A step-by-step account of prekeys, key agreement, message-key ratchets, forward security, and endpoint limits.

How Encrypted Messaging Establishes and Replaces Shared Keys
Starting a conversation without sharing a password
End-to-end encryption is a statement about where message content can be decrypted: ideally, only on the participants' devices. It is not a claim that the service knows nothing about an exchange, that a compromised phone is safe, or that every encrypted messenger uses the same protocol. To see what the protection does, it helps to follow the keys rather than the slogans.
Consider an asynchronous conversation. Alice wants to send Bob a message while Bob's phone is offline. They have never met and have not agreed on a password. The server can store public information and carry ciphertext, but Alice does not want the server to learn the message key. A modern protocol must establish a shared secret, authenticate the parties' long-term identities, work while one party is absent, and limit the damage if a key is exposed later.
Signal's published specifications provide a well-documented example. Its Extended Triple Diffie-Hellman protocol, or X3DH, was designed for the initial asynchronous agreement. The Double Ratchet algorithm then derives fresh keys as messages pass in both directions. Other services may use variants, newer post-quantum additions, or different trust models. The mechanisms below explain this protocol family, not every product carrying an encryption label.
Prekeys make an offline recipient reachable
Bob's device first creates several public-private key pairs. The public parts form a "prekey bundle" placed on the server: a long-term identity key, a signed prekey that changes periodically, and a supply of one-time prekeys. The private parts remain on Bob's device. A signature by the identity key lets a sender check that the signed prekey belongs to the same published identity.
When Alice begins the conversation, she downloads Bob's bundle and creates a fresh ephemeral key. Her device performs several Diffie-Hellman calculations that combine her private keys with Bob's public keys. Bob can later perform corresponding calculations using his private keys and Alice's public information. The mathematical results match even though neither device transmits the resulting shared secret.
The server sees the public bundle and can deliver Alice's initial ciphertext, but under the protocol's assumptions it cannot calculate the shared secret without private key material. If a one-time prekey is used, the server removes that public prekey after giving it to Alice, and Bob deletes its private partner after processing the message. X3DH's specification says this deletion helps protect the earlier shared secret against some later compromises.
Authentication is not automatic merely because the calculations work. A malicious server could try to substitute public keys during a first encounter. Signal therefore exposes a safety number derived from the participants' identity keys. Comparing that number through a trusted channel, or scanning a code in person, checks that both devices see the same identities. Most users do not perform this check for every contact, so ordinary use still relies on mechanisms that detect or announce identity-key changes.
The ratchets turn one secret into many disposable keys
Using the initial secret directly for a long conversation would create a large single point of failure. The Double Ratchet feeds that secret into a key-derivation function, a cryptographic construction that produces new key material. It maintains a root chain and separate sending and receiving chains. Each message advances a symmetric-key ratchet: the current chain key derives a unique message key and the next chain key, after which the device can delete the previous key.
This step has a one-way property. Given the current chain key, the device can derive future keys in that chain. Given a later chain key, an attacker should not be able to reverse the function and reconstruct deleted earlier message keys. The Signal specification calls the resulting property forward security. Its strength depends on secure primitives and actual deletion; a device that logs old keys or exposes plaintext defeats the intended boundary.
The second ratchet uses Diffie-Hellman public keys. When the direction of conversation changes, the receiving device notices a new ratchet public key and combines it with its current private key. That output updates the root chain and starts fresh sending or receiving chains. The device also generates a new ratchet key pair for its next turn. Alternating these operations introduces new secrets that were not derivable from the old state.
Messages can arrive late or out of order. A practical messenger therefore records limited "skipped" message keys so it can decrypt an earlier ciphertext after newer ones have arrived. Implementations cap that storage, because accepting an unlimited number of skipped keys would create memory and denial-of-service risks. The protocol is a state machine, not simply an encryption function applied independently to each text.
What compromise protection means
Forward security answers a bounded question: if an attacker obtains current key state, can the attacker derive keys that the devices already used and deleted? A properly implemented ratchet is designed to make that difficult. It cannot erase message previews from a lock screen, delete screenshots, or protect plaintext stored in a device backup.
The Diffie-Hellman ratchet also supports a form of recovery sometimes called post-compromise security. Suppose an attacker briefly copies one device's current state and then loses access. If the honest devices later exchange fresh Diffie-Hellman contributions unknown to the attacker, subsequent chains may become secure again. Recovery is conditional. An attacker who retains control of the device, intercepts identity changes, or reads messages after decryption remains inside the boundary.
X3DH's own security considerations are explicit about limits. Compromise of an identity private key permits impersonation. Reuse of prekeys affects which past sessions remain protected. The original protocol is not by itself a complete defense against future quantum computers. Signal has since published the PQXDH specification and added post-quantum material to key agreement, illustrating that deployed cryptography changes as threat models and primitives change.
There is also a difference between cryptographic deniability and ordinary denial. Some protocol transcripts are designed not to function as transferable digital signatures proving to a third party who authored a message. That property does not make a screenshot unpersuasive, prevent a recipient from forwarding text, or guarantee legal deniability.
Content secrecy leaves other information exposed
End-to-end encryption can conceal content from the transport service while leaving some metadata necessary to operate the system. A service may observe account creation, connection times, IP addresses, message sizes, delivery attempts, or membership changes, depending on its design. Signal has developed techniques such as sealed sender to reduce some server-visible information, but no network communication occurs without observable traffic somewhere.
Group messaging adds further choices. A service can encrypt a group message separately to each device, distribute sender keys, or use a group key-management protocol. Multiple linked devices require sessions for each endpoint. Removing a member must stop that device from receiving future keys; it cannot retract plaintext already received. Backups must either remain local, receive separate end-to-end encryption, or become a route around the main protocol.
Implementation quality is load-bearing. Random-number generation, operating-system storage, notification handling, contact discovery, key verification, and update delivery can introduce weaknesses outside the formal algorithm. Independent review and open specifications help analysts examine those boundaries, but source availability does not certify every compiled app or device state.
What is established is the sequence: authenticated public prekeys enable an offline initial agreement; key-derivation chains create disposable message keys; and Diffie-Hellman ratchets periodically add fresh secrets. These mechanisms sharply limit what a passive server or later key thief can learn under stated assumptions. What remains conditional is everything around them: the integrity of endpoints, verification behavior, metadata policy, backup design, and the continuing security of the cryptographic primitives.
Sources and review note
4 sources, none of them with a link, so treat them as reading, not proof.
- Moxie Marlinspike and Trevor Perrin, "The X3DH Key Agreement Protocol," Signal specification, revision 2016.
- Trevor Perrin and Moxie Marlinspike, "The Double Ratchet Algorithm," Signal specification, continuously revised.
- Cas Cremers et al., "A Comprehensive Symbolic Analysis of the Signal Protocol," IEEE European Symposium on Security and Privacy (2017).
- Signal, "The PQXDH Key Agreement Protocol," specification for post-quantum asynchronous key agreement.
The central uncertainty is implementation-specific: a protocol specification supports claims about intended key transitions, not a blanket security claim about every messenger, operating system, or backup configuration.
