A Gentle Introduction To End To End Encrypted Messaging

ETHCC 7 · 8 Jul 2024 · 19:08

Nick Molnar, CTO of Ephemera, the core contributors behind the XMTP messaging protocol, gives a speed run through 50 years of secure messaging for people who are not cryptographers. He starts with simple server-based messaging and its weaknesses, then explains end-to-end encryption, Diffie-Hellman key exchange, forward secrecy, ratcheting with hash functions and the challenge of group chats. He says XMTP's version built on MLS launched about two weeks earlier with forward secrecy, post-compromise security and key revocation. In Q&A he says changing group membership changes the secret, and that MLS needs trusted ordering rather than a trusted server.

Speakers

Topics

From ETHCC 7, hosted by Ethereum France.

Transcript

Hey everyone, I'm Nick Molnar. I'm the CTO of Ephemera, the core contributors behind the XMTP secure messaging protocol. Today I'm going to be giving you a speed run through the last 50 years of secure messaging. This is kind of the talk I wish I had heard before I joined XMTP. Like a lot of you in the audience, I'm not a cryptographer and I haven't spent my entire career working in secure messaging. But unlike most of you, I've had the incredible luck and privilege to get to work with some of the best secure messaging experts in the world and have them teach me what they know. You know, I've worked with people who have invented some of the cryptographic protocols that we're going to be talking about here.

Alex has a message and wants to get it to Bo. The easiest way to do it, and how I would start if I was doing this in a day is just send the message to a server. Server sends the message to Bo. Great. We're done. This is all super easy. Twitter DMs worked this way until pretty recently. Warpcast DMs work this way. It's more common than you'd think for the architecture to really be this simple and naive. So what could go wrong with this architecture?

Well, an employee with access to the server could just read all the messages. Law enforcement can go to whoever controls the server and force them to hand over the messages. And the operator of the server probably won't even be able to tell you that law enforcement made them give up all your messages. Or someone can just break into the server. They can uh you know get a backdoor onto the server, read all the messages, and the person who owns the server probably doesn't even know it. You really have to have a large threat detection org to be able to know that this has happened.

And so I would trust anything uh like this maybe to share some recipes, but if we're talking about sensitive economic data, you really need a better model and a better architecture. You might have heard of apps say that your messages are encrypted at rest. Uh that's nice. It just means that the server got your plain text messages, can read them, and then they encrypted it with a key that they control, and they decrypt it when they wanted to send it to someone else. Uh company insiders can get the key, probably the attackers can get the key. It's good for marketing, but really doesn't help much.

What you want is end-to-end encryption. So in this model, you have both Alex and Bo with the secret key. And Alex can encrypt the message with the key, send it to the server. The server just sees gibberish, it sends it to Bo, who has the same key and can decrypt the message. This should be familiar to a lot of you. Um, this would be a lot better of an architecture. But the problem is somehow Alex and Bo need to have the same key. And this is where things get a little bit more complicated.

So Alex could just write the key on a piece of paper, hand it to Bo. That works. It is totally secure, but it's just not very practical. You can imagine, oh, we're just gonna build a service. We'll just have some service that can pass these keys around. But now all the problems in the last slide apply to that service. And so you end up in roughly the same place. So what you really want is the keys to not have to leave either of Alex or Bo's devices and not go over the wire at all, or you're kind of in the same place as having a totally insecure messaging protocol. Enter these two guys, Diffie and Hellman.

So the way that most messaging protocols do this is you give everyone a private and a public key, just like you have with your Ethereum wallet, and you have a function that takes in my public key and your private key, or my private key and your public key, and it can get out the same result. So Alex and Bo can use Alex's secret key and Bo's public key, or Alex's public key and Bo's secret key. And no matter which way you're doing this, you are going to get the same output that you can use to encrypt a message.

And so as long as they can somehow get these keys to one another, you can tattoo it on your forehead, you can uh put it on a billboard. Any method of advertising your public keys is all you really need. And because they're public keys, you really don't have to worry about securing it. You can put it in a very public registry. This would be a big step up from the last architecture. So at this point, you imagine okay, both sides have a public and a private key, and you're able to send messages to one another.

This is a pretty good end-to-end encrypted system. Version one of XMTP, which we launched about two years ago, works something like this. PGP works something like this, but you have to ask a few more questions about the system to figure out how secure is it really? So, what happens if someone gets a hold of that secret key? Do they get all the messages, or do they just get a few of the messages? If someone gets a hold of that secret key, can they impersonate me or can they just read my messages? It'd be better if they could just read the messages but not send any new ones on my behalf.

And then if there is some attack, maybe someone steals your phone and puts malware on it. Is there a way to recover and actually get back to a good state where you can use the protocol? And then the last question is maybe the trickiest. What if there's more than two people in this conversation? How do you extend it so that you can have uh groups, maybe groups of hundreds or thousands of people? And this simple naive example I had before is not really great for any of these things. You have one key that if you lose it, you're really screwed.

And so we can start looking at some other security properties that might let us fix some of these problems. One you might have heard of is forward secrecy. So this is just the property that if there's a compromise here in the middle, that older messages from before the compromise are safe. So this very useful property. At least they can only steal new messages if someone breaks into my phone, but they can't get all the old ones. The other property is post-compromise security.

So if I get malware on my phone, how do I move back to a clean state where no one could read my messages? How does this self-heal after a compromise? If you have a system that offers both, the worst case scenario is that after a device gets compromised, the attacker should only be able to read a really small number of messages. So let's dig into how you might get forward secrecy. With the simple Diffie Hellman example before, every message is encrypted with the exact same secret. So you have key one, it encrypts message one, key one also encrypts message two.

And this runs into a problem if the key that was used for message three were to get compromised. It's actually going to compromise all the messages. And so you can go all the way back in time. So if someone gets hold of that key, they can only read a single message. But if you imagine WhatsApp or Telegram or Signal, uh, there's a lot of messages going back and forth. Trying to transmit that many keys is going to be really expensive and complicated.

And so in a perfect world, you'd have some way that you can not send a bunch of things over the wire, but still get a new key for every single message. This is where ratcheting comes in. If you've read Signals Marketing, they talk a lot about the double ratchet algorithm. This is one of those ratchets. So the basis of ratcheting is a hash function. You can go from A to B very easily, but going B to A is very, very hard. And you can use hash functions to go from one key to another.

So message one gets encrypted with a key. You hash that key to get the uh the decryption key for message two, and then you throw out the first key. And if you do that, then when someone compromises you on message three, they have no way to go back and get key two or key one, and you get forward secrecy out of this. So forward secrecy of those two properties, probably the easiest to get since this is pretty well understood. This is actually the way Signal does it. Uh it's a slightly fancier version of the same idea where you're taking in a chain key and a message decryption key, and you use the chain key to generate the next chain key.

Uh and so if you compromise the message key, you actually can't go forward either. But this is kind of an optimization, it's roughly the same idea as uh what I just described. So the next question is what about group chats? People really like group chats, and people are using more group chats than you think. Even if two people are talking, maybe they have a laptop and a tablet and a phone, and so those two people are actually in a group chat between maybe six devices. The easiest way to build a group chat protocol is to not really think about it and just do it on top of one-to-one messaging.

So you have everyone in the group talk to everyone else in the group through pairwise communication channels. This is what that looks like. Alex talks to Caro, Alex talks to Bo, Alex talks to Dan, and they all talk to each other. And as dumb as it sounds, this is really common in the world. If you use iMessage right now, it is built on top of pairwise communication channels. Where you use pairwise communication channels to send a key to everyone in the group once, and then you can reuse that key so you're not having to do it on every single message.

But it's the basically same idea. And it really doesn't scale. If you imagine that you have a group of a hundred members, now there's uh you know 9,999 pairwise communication channels that you're having to deal with. And so people have been looking for a better way for a long time because it really gets hard to do this level of security on a group bigger than 50 or 100 members without your phone turning red hot in your pocket. The thing we've been looking for is a nice way to let you securely get one key for an entire group.

Then you can send a message one time, use one key, no matter how many mess members are in the group. And you want to do this without losing forward secrecy or post compromise security. In the last few years, we've seen the emergence of a new protocol that promises just that. It's called messaging layer security or MLS, and it builds on top of all the concepts we've been talking about to deliver the first end-to-end encrypted messaging protocol specifically for groups. It's an IETF standard, it's being adopted by really big players like Google and Amazon, Cisco, Matrix.

And as of a few weeks ago, XMTP is running on MLS. So let's dig into how MLS actually works and gets you these properties. So the magic of MLS is that it turns your group into a big tree. Any member of group can derive that one green secret at the top of the tree by moving from their own little slot at the bottom all the way up. So if you're the person in position zero at the bottom left, you would use your secret key and the public key of the person in position one, and you do a Diffie Hellman there, and you get a secret key for the level above you in the tree.

And then you have the public key of the member to the right of you. And so you do another Diffie Hellman that generates the secret key for the rung above. And you could do this all the way up the tree. And so you can very efficiently in logarithmic complexity get the root secret that lives at the top of the tree, no matter where you are. This then can get fed into a ratchet, and you can generate message specific keys out of this root secret. The benefit of this is that instead of getting ON encryptions for pairwise channels, you only have to encrypt normal messages one time.

So every member of a thousand member group can read the same message because they can derive the same secret key. And this is much more efficient, especially for a decentralized messaging network like XMTP, where the costs of storing each message can be a lot higher. Having one copy as opposed to a thousand copies of every of every message just makes the economics of the protocol work a lot more smoothly. And so at Ephemera, we're using MLS for the next generation of the XMTP protocol. If you're building on groups with XMTP, this is using MLS under the hood.

The reason we chose this is pretty clear, it treats groups as a first-class citizen, not as a thing that gets bolted on. And it can scale to groups with thousands of members. Today we limit to 400, but we are continually working on getting those numbers up. It seems like MLS is not our bottleneck here, and so we can just keep increasing group sizes to get to thousands of members, which is really exciting for something that has the same security properties as Signal or WhatsApp. It's also just a really well studied protocol.

The devil's in the details with any of these cryptographic protocols. You'd see all the papers that have been written about MLS. This has been going on for years as part of an IETF standard. And people have formally verified the protocol, and so that's really powerful. And the project has great trajectory because it's been standardized. There's now many open source implementations in a bunch of different languages. So this is what we launched about two weeks ago on XMTP. It checks a lot of security boxes and privacy boxes.

We have forward secrecy, post-compromise security, key revocation. We have protections for people's private data. So when you look at our nodes, even with the most privileged access, like if I go into our database of one of our nodes, I don't know who's talking to whom, um, who is sending which message, who's receiving which message. Um we have message authentication so that the people in the group do know that this really came from the person I was expecting it to come from, and the messages are confidential. So if you have any questions, uh drop me a line on any XMTP compatible app like Converse or Coinbase Wallet.

My ENS name is nickmolnar.eth. Uh yeah, just shoot me a line on Converse, and if we have a few minutes, I can take some questions now. I think we have time just for one question. Hey, thanks. Great talk. Um, are there difficulties when you need to like add or remove someone from the group chat? Because maybe the the Merkle route changes?

Yeah, so it's actually a great feature of this is that adding or removing someone from the group changes the secret. And that's how when I kick you out of a group, you can't read any messages after that because that root secret is changed. And so it's really a uh feature of the protocol that it changes on uh membership changes. Uh yeah. Oh yeah. I suppose I know that this is some concursive problem too.

Uh that requires this so trust the servers. And do you think that we can use the MNS4 in the decentralized way in the P2P without using trust to setup in uh that we cancel the concursive problem? Thank thank you. So MLS doesn't require a trusted server. It does require trusted ordering. And so if you can get that ordering through a trustless system, I think there's a lot happening in the blockchain world that is about getting ordering out of a trustless system. Uh then you don't have to trust the server.

And so that's the big bet we're making. Thank you all.

Related talks