# Chapter 42: Private messenger

> Ana wants a chat app in which not even the company that runs it can read what she says. The trunk’s server keeps everything and understands everything. What has to change?

IM Systems in Depth · https://im.liko.page/en/private-messenger/

Ana wants to switch chat apps. She has one requirement: what she says to Ben can be read only on their own devices. Nobody else can read it, including the company that runs the app.

The trunk’s server cannot do that. Since chapter 1, the server has read every message it passes on. Even with the connection encrypted with TLS, which protects it on the way, the server itself decrypts it, stores it and understands it. This chapter starts from v3 and asks: once the server cannot read messages, which of the trunk’s decisions must change?

## 1. What the product needs

First, what **end-to-end encryption** means. Each device makes its own pair of keys. The **public key** goes to the server, and anyone may fetch it. The **private key** stays on the device and never leaves it. A message encrypted with a public key is called **ciphertext**: it looks like random bytes, and only the matching private key can turn it back into the original. To send to Ben’s phone, Ana’s phone fetches that phone’s public key from the server and encrypts the message with it; the server just passes on bytes it cannot decrypt. The real protocol (the Signal protocol) also keeps changing keys, which we skip here. Remember one thing only: **a piece of ciphertext can be opened only by the device it was encrypted for.**

There is also a simpler kind of key: the same key both encrypts and decrypts, so whoever has it can open the message. Groups and photos will use it later.

“Encryption” comes in several levels, ordered by how much the server gives up:

| Level | What the server can see | Examples |
|---|---|---|
| Transport encryption only | All content; it keeps all history | The trunk; Telegram’s ordinary (cloud) chats |
| End-to-end, the server keeps encrypted history | Who talks to whom, and when; it cannot read the content, but keeps it | Matrix (an open chat protocol); Facebook Messenger (since 2023) |
| End-to-end, deleted once delivered | Who talks to whom, and when; content stays only while in transit | WhatsApp, Signal |
| Also hide who sends and who is in a group | Who a message is for; as little as possible about who sent it | Signal (section 8) |

This branch takes the third level: only devices can read the content, and the server keeps as little as it can. Section 8 covers the fourth.

## 2. Which of the trunk’s decisions read content

Each of the trunk’s decisions made sense when it was made, but once the server cannot read messages, six of them no longer hold:

| The trunk’s decision | What the server must do | With end-to-end encryption |
|---|---|---|
| History kept on the server forever; new devices pull it (chapters 6, 10, 16) | Store every message | Each copy is for the devices that existed then; a new device has no key and cannot open it |
| One message pushed to every device (chapter 16) | Store one, push many | Each device has its own key, so each needs its own copy |
| The server counts unread, skipping system messages and your own (chapter 14) | Know what each message is | It cannot tell which is which |
| Notification text, and a badge from the server’s unread count (chapter 17) | Read content, count unread | Neither is possible |
| Recall, with the server checking the time limit (chapter 18) | Know which message is a recall | A recall is ciphertext too; only clients can check |
| Photos in object storage; whoever has the address can see them (chapter 8) | Store files | They must be stored encrypted, with the key inside the message |

The first row is the easiest to get wrong. If the server keeps the ciphertext forever, can’t a new device pull it? No. A message Ana sent last year was encrypted separately for her phone and laptop of that time and for Ben’s two devices; the tablet she bought today had no keys then and cannot open any of those copies. **With end-to-end encryption, history kept on the server is useless to a new device.**

The trunk has no server-side search, but the workplace branch (chapter 39) does. A server that cannot read has nothing to index.

The next three sections change three things: how to send, what the server stores, and how history reaches a new device.

## 3. Sending: one copy per device

In the trunk, Ana uploads one copy and the server pushes it to every device (chapter 16). Now every device has a different key, so **Ana’s phone** encrypts a copy for each device:

1. Ask the server which devices Ben has and their public keys, and which devices Ana has besides this phone.
2. Encrypt one copy for each device, her own other devices included. (Otherwise her laptop would not see what she sent from her phone: chapter 16’s problem.)
3. Hand them to the server in one go. The server puts each into that device’s inbox (next section).

In a 1:1 chat where Ana has two devices and Ben has two: Ben’s 2 copies plus 1 for Ana’s laptop make **3 copies**. The trunk uploads 1.

WhatsApp’s [multi-device post](https://engineering.fb.com/2021/07/14/security/whatsapp-multi-device/) (2021) describes exactly this: each device has its own keys, and the sending client encrypts and transmits a message N times, to N devices. Besides the phone, up to four devices can be linked.

The device list must be right. Ana’s idea of Ben’s devices may be stale: Ben logged in on a new laptop yesterday. Signal’s server checks this. If the list does not match, it refuses the message with error code 409, described as “Incorrect set of devices supplied for recipient” ([MessageController](https://github.com/signalapp/Signal-Server/blob/a15d5cd/service/src/main/java/org/whispersystems/textsecuregcm/controllers/MessageController.java)). The reply lists the missing devices and the extra ones; the client adds and removes copies and sends again.

**Groups get expensive.** A group of 100 people with 3 devices each is 300 devices; minus the sending one, 299 copies. Encryption adds a key number, a counter and a check code to the 200-byte message, and each copy carries its device’s address: call it 300 bytes a copy, as side trip W4 does. One message uploads 89.7 KB; on chapter 8’s weak 2 Mbit/s uplink that takes 0.36 s, where the trunk uploads 200 bytes. The trunk’s 500-member hiking group means 750 devices, 749 copies, 224.7 KB and 0.9 s.

So groups use another method: **sender keys**. Each device has a key of its own for the group (the simpler kind from section 1: whoever has it can open the message). It is sent once to every device of every member, encrypted for each device as in a 1:1 chat. After that, each message is encrypted only once with it, handed to the server, and the server delivers it to every device in the group. They all hold the key already, so one copy is enough. WhatsApp’s [security whitepaper](https://www.whatsapp.com/security/WhatsApp-Security-Whitepaper.pdf) works this way; whenever a member leaves, all participants clear their Sender Key and start over. 299 copies become 1. The cost is distributing and replacing keys; the details are in [side trip W4: Group encryption](/en/group-encryption/).

**A photo cannot go out 300 times.** A 200 KB photo encrypted once per device is 299 × 200 KB = 59.8 MB, 239 s on the weak uplink. The whitepaper’s method: the phone makes a random key, encrypts the photo **once** and uploads it to storage. Then it sends an ordinary encrypted message carrying that key, the file’s address and its hash (a “fingerprint” of the content, used to check that the downloaded file was not swapped). The upload stays 200 KB, 0.8 s; only the small message is sent 299 times. Chapter 8’s “the address is the permission” problem goes away too: someone who gets the address but not the key cannot open the file.

## 4. Storing: an inbox per device, deleted once fetched

The trunk’s server writes each message into the conversation’s timeline and keeps it. Devices bring their seq and ask “what comes after this?” (chapter 6). Now each device’s copy is different, so what the server holds is ciphertext for specific devices. The simplest shape:

- **One inbox per device**, in order of arrival.
- A device fetches when it connects. Once a copy is in its local database it sends an ACK, and the server **deletes that copy**.
- If a device never comes to fetch, the copy is deleted after a while anyway.

This is store and forward, not an archive. In Signal’s server code, a delete is by recipient, device and message ID ([MessagesManager](https://github.com/signalapp/Signal-Server/blob/a15d5cd/service/src/main/java/org/whispersystems/textsecuregcm/storage/MessagesManager.java)). Its [privacy policy](https://signal.org/legal/) says the server queues end-to-end encrypted messages for delivery to devices that are temporarily offline, and that your message history is stored on your own devices. WhatsApp’s [privacy policy](https://www.whatsapp.com/legal/privacy-policy) says messages are deleted from its servers once delivered; a message that cannot be delivered is kept in encrypted form for up to 30 days, then deleted.

Among chapter 16’s other answers was “an inbox per device”, rejected then: every message is written once per device, 750 writes for one message in the hiking group. Here it comes back, because those 750 copies are 750 different ciphertexts anyway. They have to be written somewhere.

**How much is stored.** At v3, 400 million messages a day:

- **The trunk** keeps all history: 400 million × 200 bytes = 80 GB a day, 29.2 TB a year, 87.6 TB with three replicas, and still growing.
- **Inboxes**: each message reaches 10 devices on average (the series’ assumption since chapter 1: 1:1 chats reach few, groups many), so 4 billion copies of 300 bytes, 1.2 TB, **flow through** the server each day. What is held at any moment = flow × how long each copy stays. Assume copies stay 1 hour on average (online devices fetch at once; offline ones wait for a push to wake them): that is one hour’s flow, 50 GB. Also assume 1% of devices never come back, so their copies stay the full 30 days: 1.2 TB × 1% × 30 = 360 GB. Together 410 GB, 1.23 TB with three replicas, and **it stops growing**.

After one year, the inboxes hold about **1/70** of what the trunk holds (87.6 TB ÷ 1.23 TB = 71). Notice one more thing: 88% of those 1.23 TB are copies nobody fetches. The inboxes’ size is set mostly by “when do we give up”, not by the devices that behave normally.

**But writes do not go down.** At v3 peak the trunk writes 13,900 messages a second, plus chapter 11’s record update for each recipient (6.67 recipients per message on average, chapter 6’s assumption), 92,700 a second at two rows each: 199,300 rows a second in all, or 10 storage nodes at chapter 29’s 20,000 rows a second each. Inboxes take in 139,000 copies a second (13,900 × 10 devices) and delete 139,000 after their ACKs, and a delete is a write too: 278,000 a second, 13.9 nodes, about **1.4 times** as much. Storing less is paid for by writing more.

Inboxes also change the shape of sync. The server no longer keeps a timeline per conversation, so there is no “latest seq of each conversation” to sweep (chapter 6), and no conversation records on the server (chapters 10 and 11). A device that comes back asks only: “what is in my inbox?” The phone builds the conversation list itself from the messages it receives: chapter 10’s “built by the client” answer.

## 5. History: moved between your own devices

With no history on the server, Ana’s new tablet gets nothing at its first login. The fix is to **let her old device provide it**:

- WhatsApp: as soon as a new device is linked, the primary device packs the messages of recent chats into a bundle, encrypts it and transfers it; the key goes to the new device in an end-to-end encrypted message ([multi-device post](https://engineering.fb.com/2021/07/14/security/whatsapp-multi-device/)).
- Signal ([January 2025](https://signal.org/blog/a-synchronized-start-for-linked-devices/)): when linking a new Desktop or iPad, the user can choose to bring their chats along. The phone compresses them into one archive, encrypts it with a random one-time key and sends it to the new device through the server; the last 45 days of media can come too.

With chapter 16’s numbers, Ana sends and receives about 110,000 messages a year, 22 MB of text; the old phone packs them, encrypts them once and passes them to the tablet through the server. This assumes the old device is still there and still turns on.

**If every device is lost, the history is gone**, unless the user turned on an encrypted backup. Since 2021 WhatsApp can [encrypt backups end to end](https://engineering.fb.com/2021/09/10/security/whatsapp-e2ee-backups/), with the key protected by the user’s password or a 64-digit key. In 2025 Signal launched [secure backups](https://signal.org/blog/introducing-secure-backups/), first in its Android beta, unlocked by a 64-character recovery key that only the user holds. Forget the key and the backup cannot be opened; the server cannot help with that.

## 6. The rest moves to the phone

**Unread counts.** The phone counts them (as chapter 14 said). It has every message (it fetched them all from its inbox), so counting is easy. The hard part is keeping “read up to here” the same across devices: it becomes one more encrypted message, sent to your own other devices.

**Notifications.** The notification carries nothing; it only wakes the app, which fetches from its inbox, decrypts and then shows the message. Signal’s server code shows this ([APNSender](https://github.com/signalapp/Signal-Server/blob/a15d5cd/service/src/main/java/org/whispersystems/textsecuregcm/push/APNSender.java), [FcmSender](https://github.com/signalapp/Signal-Server/blob/a15d5cd/service/src/main/java/org/whispersystems/textsecuregcm/push/FcmSender.java)). An iPhone gets a notification with `mutable-content`, whose text is only a placeholder, `APN_Message`; the app’s notification extension replaces it with the real content on the phone. An Android phone gets an FCM data message with the key `newMessageAlert` and an empty value. Chapter 17 described another use of the same extension: the notification carries the ciphertext, and the extension decrypts it on the phone without a trip to the inbox. Either works; Signal puts no content in the notification at all. The badge is computed on the phone too.

**Recall and edit.** Chapter 18’s design: a recall is one more message in the conversation and travels to every device like any other. That barely changes. A recall is an encrypted message, “delete the message with msg_id X”, sent to every device. What changes is **who checks it**. Chapter 18 had the server check the time limit; now the server cannot tell that a message is a recall, so only the receiving phone can check “did the sender write it, and is it within the limit?” A modified client can ignore it.

**Search.** Only over the local database. Chapter 16 said search on a new device is incomplete; here it is the only search there is.

**Duplicates.** Chapter 4’s msg_id is inside the ciphertext, where the server cannot see it. The receiving phone removes duplicates by “sender + msg_id” after decrypting; chapter 7’s unique index in the local database already does that.

**Order.** Chapter 5’s seq is given by the server; a server that cannot read the content cannot vouch for the order either. Telegram’s secret chats put two counters inside the ciphertext, and the two clients count for themselves: a number lower than expected is a repeat and is dropped; a higher one means something is missing in between, so the client holds the message and asks the other side to resend the missing ones; if they cannot be filled, the secret chat is ended ([Sequence numbers in secret chats](https://core.telegram.org/api/end-to-end/seq_no)). It is chapter 5’s “fill the gap when a number is skipped”, moved to the phones.

## 7. The cost

- **Lose your devices and you lose your history**, unless you turned on an encrypted backup and remember its key.
- **A new device starts empty.** It has only what an old device gives it; without the old device at hand, it starts from today.
- **Each message costs more to send.** One copy per device, with sender keys in groups; device lists must always be right; about 1.4 times the writes.
- **The server cannot help.** No server-side search, no badge from the server, no content in notifications; moderation relies on users reporting messages and handing them over themselves.
- **Recall and edit depend on clients following the rules.**
- **Keys must be managed.** A new phone or a reinstalled app means new keys, and contacts need to see that the safety number changed (a number computed from the two people’s public keys: if they compare it in person, they know nobody swapped a key in between); and the device list must show that the user really added each device.

## 8. Other answers

- **The server keeps encrypted history.** Matrix keeps room history on the server, encrypted messages included. So that a new device can read old messages, a client can back up its decryption keys to the server, encrypted, and a new device opens them with a key only the user knows ([server-side key backups](https://spec.matrix.org/latest/client-server-api/#server-side-key-backups)). When Facebook Messenger moved to end-to-end encryption by default in 2023, it did not choose “deleted once delivered”. Its apps had always kept only a small local cache and relied on history on the server, so Meta built an encrypted storage system, Labyrinth: ciphertext stays on the server, and devices load it on demand ([post](https://engineering.fb.com/2023/12/06/security/building-end-to-end-security-for-messenger/)).
- **Two kinds of chat side by side.** Telegram’s ordinary chats use transport encryption only and live on the server, readable from any device. Its “secret chats” are end-to-end encrypted, exist only on the device that started them, and are not in the cloud. Its [FAQ](https://telegram.org/faq) says that restoring chat history on a new device has no elegant solution with end-to-end encryption.
- **Hide even who sent it.** The server must know who a message is for, but not necessarily who sent it. Signal’s [sealed sender](https://signal.org/blog/sealed-sender/) (2018) puts the sender inside the ciphertext. Its [private group system](https://signal.org/blog/signal-private-group-system/) (designed in 2019, the basis of its [new groups](https://signal.org/blog/group-calls/) since 2020) stores the member list encrypted on the server: the server can check “you are a member” without knowing who the members are.
- **The trunk: transport encryption only.** All six decisions from section 2 stay. Most chat products are at this level.

## 9. This chapter’s decision

**Decision card**

- Problem: With end-to-end encryption the server cannot read messages. Six of the trunk’s decisions read content: history on the server for new devices, one message pushed to every device, unread counted by the server, notification text, the server checking recall time limits, and photo addresses as permissions.
- Choice: The sender’s phone encrypts one copy for each device, its own other devices included; groups use sender keys, and a photo is encrypted once with its key inside the message. The server keeps one inbox per device and deletes each copy once fetched, keeping unfetched copies at most 30 days. Old devices pack history and pass it to new ones; unread counts, notification content, recall checks and search move to the phone.
- Cost: Lose your devices and you lose your history, unless there is an encrypted backup; a new device starts empty; one copy per device, about 1.4 times the writes; no server-side search or server badge, no content in notifications; recall relies on honest clients; keys and device lists must be managed.
- Revisit when: How group keys are distributed and replaced (W4); a product that needs server-side search or years of history (chapter 39); history on the server together with end-to-end encryption (Matrix, Messenger’s Labyrinth).
- Other answers: The server keeps encrypted history (Matrix, Messenger); two kinds of chat side by side (Telegram’s secret chats); hide even the sender and the group members (Signal); transport encryption only (the trunk).

That covers a product whose server cannot read. The next chapter is the opposite: customer service, where a message is read not only by the other person but by a second and a third agent at the company.
