Deep dives Chapter 6 of 9

Encryption and real deletion

How keys for each user protect message text and personal data, and how account deletion destroys those keys.

5 min Checked against its records on 2026-12-01 Source file

Chatto encrypts message text and personal data with keys that belong to one user. The server holds these keys and decrypts the data for readers who are permitted to see it.

When an account is deleted, Chatto destroys the keys of that user. The encrypted bytes stay in the log, but no one can read them. This method is named crypto-shredding (ADR-007).

The log of facts is append-only, and the messages of one user are in many rooms. A search for each message would be slow, and copies could stay in a backup or on a replica (ADR-007).

One small key is easy to find and to destroy. Without the key, the encrypted data is permanently unreadable. The result is the same as deletion, and it also applies to each copy of the data.

Two levels of keys, and where Chatto keeps them

Chatto uses two types of key (ADR-007):

  • A data encryption key (DEK) encrypts the data. Each user has one DEK for message text and a different DEK for personal data.
  • A key encryption key (KEK) encrypts the DEKs. This operation is named wrapping. Each user has their own KEK.
KEK of the user      in ENCRYPTION_KEYS
  wraps
DEKs of the user     wrapped records in RUNTIME_STATE
  encrypt
message text, login, display name, verified email

Both stores are key-value buckets: a JetStream store that keeps the latest value for each key. The log records only that a DEK exists and which KEK wraps it. It never contains key material.

Only the key management service (KMS) of Chatto touches a KEK. It is a small interface with four operations: create a key, wrap a DEK, unwrap a DEK, and shred a key. It names each key by an opaque reference, not by a user ID. The default KMS runs in the server process and keeps the KEKs in ENCRYPTION_KEYS.

A shared key for all users would not work. The deletion of one account would then affect the data of all other users (FDR-018).

The chatto backup command does not copy ENCRYPTION_KEYS (ADR-007). A backup contains the encrypted data and the wrapped DEKs, but not the KEKs that unwrap them.

Thus a backup from last month cannot bring back the messages of a user who deleted their account last week. The key is not in the archive.

This rule has a cost. If the server loses a KEK by accident, the data of that user is lost. The operator must protect this bucket as critical data. The backup command has an --include-keys flag for an archive that must contain the keys. Such an archive is sensitive.

What the ciphertext is bound to

Chatto encrypts with XChaCha20-Poly1305, a cipher that also detects each change to the encrypted data. Such a cipher can also authenticate extra data that is not secret.

For a message body, this extra data is the message event ID, the body event ID, the room ID, the author ID, and the number of the DEK (ADR-007). If someone copies the ciphertext into a different message or a different room, decryption fails.

Personal data uses the same method. The log stores the login, the display name, and the verified email of a user only in encrypted form. A projection keeps the encrypted values and decrypts a display name only while it answers a read.

The basic operations are in a shared module, pkg/datacrypto: key generation, encryption, decryption, and key wrapping (ADR-060). The module does not know about users or messages. Chatto supplies the extra data and owns the key hierarchy.

Deleting an account

Account deletion cannot be undone, so the user must type a confirmation first (FDR-018). Then the server does these steps (ADR-007):

  1. It appends a UserKeyShreddingRequestedEvent fact for the user. If the server cannot store this fact, the deletion stops and nothing is destroyed.
  2. All projections treat this fact as the privacy boundary. The profile, the login data, the search data, and the messages of the user become unavailable immediately.
  3. The server shreds each KEK of the user. Only then does it delete the wrapped DEK records.
  4. It appends a UserKeyShreddedEvent fact, which records that the keys are gone.

The request is durable. A background worker reads the request facts from the log and tries again after a crash, on any server process. The order in step 3 makes a repeat safe: while a DEK record exists, the worker can find the KEK that still wraps it.

One limit applies when more than one server process runs. Each process keeps the keys that it unwrapped in memory, for 5 minutes at most, so that it does not read the key store for each request. A process that applied the request returns no key, also from its memory. A process that has not yet applied the request can still use a key from its memory until the 5 minutes end (ADR-007).

Other users see the result live. Their apps get a UserAccountDeletedEvent and discard the profile and the loaded message text of that user (FDR-018). The messages become placeholders.

What the user key does not cover

Attachments are not encrypted with the key of the user. The same is true for files derived from them, such as thumbnails. Key destruction does not affect them. Account deletion records an AssetDeletedEvent for each such file and removes the stored bytes explicitly (ADR-007).

The deletion of one message destroys no key. Chatto stores message text in a private body fact, separate from the public fact that a message was posted. A delete appends a public retraction, and a durable job then erases the body from the log (FDR-004).

Projection snapshots are a cache of derived state on disk. Chatto encrypts them with a key derived from the server secret, not with user keys. For this reason, a snapshot must not contain decrypted personal data or plain message text (ADR-050). Sensitive fields stay encrypted with the user keys inside the snapshot, so crypto-shredding also applies there.

Read the code

The records behind this chapter

Esc