Gainsley has chats: between friends, in groups, and inside challenges. For a while they were encrypted with a scheme I'd put together myself on top of a well-known crypto library. Every primitive was solid, but the protocol, the way the pieces fit together, was mine. And homemade protocols are where encryption usually breaks.
So I replaced it with MLS (Messaging Layer Security, RFC 9420), the IETF standard that Discord uses for call encryption and that Google's RCS messaging is adopting, through the open-source Rust library OpenMLS.
End-to-end encryption, the short version: the server delivers messages it can't read.
The goal, in one sentence
Nobody but the people in a chat can read it: not the server, not me as the operator, not a database backup, not the push notification service.
What MLS gives you
The core idea is a group with a shared secret that keeps changing. Every member has their own keys, and together they agree on a group secret for the current epoch. Whenever someone joins or leaves, the group moves to a new epoch with a new secret, so:
- someone who leaves can't read what's sent after they're gone, and
- someone who joins can't read what was sent before they arrived.
It also scales. In a naive design you encrypt every message once per recipient. MLS uses a tree of keys, so changing the group costs roughly log n work instead of n.
Every device is a member
The first design decision wasn't about cryptography at all: what is a "member"? In Gainsley it's a device, not a person. Your phone and the browser you linked are two members of every chat you're in. A message is encrypted once for the group and reaches all of your devices and everyone else's.
That raises the obvious question: if the server tells your phone "Ben has a new device, add it to the chat", how do you know that's really Ben's device and not the server adding itself to read along?
Trust is the real problem
Each person has one identity key, created on their first phone, and it signs the list of their devices. Your phone only accepts a device into a chat if its owner's identity signed it. Contacts' identity keys are remembered the first time you see them, and if one changes you get a "security code changed" line in the chat, the same idea as WhatsApp. You can also compare codes in person.
The server in this setup is only a delivery service: it stores encrypted messages, keeps them in order, and enforces app rules like "only friends can chat". It never sees the text.
What the server still knows
End-to-end encryption hides content, not metadata. The server still has to know who is in which chat, when messages are sent and roughly how big they are, otherwise it can't deliver anything. Being honest about that line was part of the design: text, reactions, edits and replies are all encrypted, and the delivery facts are not.
Keeping keys out of JavaScript
The MLS code is a small Rust crate around OpenMLS. On Android it's called from native code, and the key that protects the local state is wrapped by the Android Keystore. In the browser the same Rust core runs as WebAssembly. JavaScript never holds a private key.
What I took from it
- Don't write the cryptography, and be very careful writing the protocol. Using an audited library for the primitives isn't enough if you invent how they fit together.
- Most of the work is trust decisions, not encryption. Who is allowed to add a device? What happens when a key changes? What does a device do when it missed too much? None of that is in the RFC's examples.
- Assume the server is the attacker when you review it. Going through every flow asking "what if the server lies here?" found more problems than any other approach I tried.
- Write down what you're not protecting. Metadata, a stolen unlocked phone, a copied browser profile. A clear threat model makes the rest of the decisions much easier.
