End-to-End Encryption Saves Lives. You Can Start Saving Lives In Ruby, Too

COSCUP 2025 · 9 Aug 2025 · 30:20

Ryo Kajiwara makes the case for end-to-end encryption as a life-saving tool and shows how Ruby developers can use it through Messaging Layer Security (MLS, RFC 9420). He argues that banning encryption harms vulnerable people without stopping criminals, then explains public key cryptography, HPKE, forward secrecy, post-compromise security and how MLS uses ratchet and secret trees to scale group key agreement. He also covers remaining work on OpenSSL support, post-quantum drafts and running in Ruby WASM.

Speakers

Topics

From COSCUP 2025, hosted by COSCUP.

Transcript

Yeah, this is uh Yeah. Maybe you can hear. >> Yeah. >> Nation. >> Okay. >> Yeah. So, uh, so I'm hi, I'm Rio Kazar and I'm I'll be talking about end to end encryption and it saves lives. So you can start saving lives with Ruby too. So uh slides are available at this URL and for this QR

code and uh if uh yeah in in the in the morning session like people said like h said like nobody looks at speaker deck but like my there's a lot of text and lots of diagrams. So there's total of 94 slides I believe. So uh please uh h have it at your have it on your phone or on your computer to look uh to watch. Uh I'll be starting in about 20 seconds and this is a slightly updated version from the Ruby Kai talk and like I've added lots uh lot more uh explanations because the Ruby Kai version was very rushed and

so let's start. Hi. Uh I am from Matsyama and I flew from Mat Matsyamaj to Tonan TSA and to to find uh uh this uh like uh advertisement of Dogo onen which is in Matsyama in in Taipei. This is this is in uh this is in Shim Simbe and uh that was where I stayed yesterday. And so I do a lot of stuff. If anything catches your interest, uh let's talk and I do more stuff that is more relevant to this talk. I am a freelance web developer focused on digital identity and security. And I've uh I've worked and I've been working on uh writing,

editing and implementing stand uh standards such uh such as uh HTTPS and local network web of things working group in the W3C and uh OOTH the messaging layer security working groups of the IETF. and I've worked as an uh officer of inter internet society Japan chapter of uh until 2023 and the uh there's a usual disclaimer about talking about cryptography cryptographic API is very easy to misuse and operational security is also difficult because uh even if the protocol is secure if if m if you misuse it uh there will be security breaches you uh you've seen it in the news and I've done my research about this topic but I don't consider myself a cryptography expert which means I am not

DJB And uh also I don't have a PhD or master's degree in this field. So yeah uh if if you're not sure, please have your system audited by a security expert before going to production. And speaking about production readiness, the MLS implementation I'm talking about today is not uh not production ready at all. It's a it's very much a prototype and it passes most test vectors but lacks validation on error cases and client implementation is underway. I was trying to get this ready for this talk but uh yeah uh I I'll hopefully release 1.0 by the next ETF which is uh in uh November and yeah uh first I'll talk about the motivation getting in stock. So I happen

to be known as the SMTP or Yamero guy which means uh stop using SMTP more like SMTP is dead because uh I have two problems with SMTP. one that is it it is uh it lacks strong identity. You are you are able to spoof identity easily on SMTP and also SMTP lacks end to end encryption. Uh which means the people who run the services cannot read your messages if you have end toend encryption. Uh line Facebook messenger signal WhatsApp those app apps you use for messaging they all have their own each uh own version of end to end encryption. And uh to talk about why it's important, we have to mention some international affairs. So

bear with me for a while. So uh end to end encryption is more important than ever because the there is increasing tension in international affairs and also uh per powerful people even in de democratic countries want uh want to control the internet and your uh uh your communications. So end to encryption provides a safe method to communicate under oppression or in war zones which mean uh which means it saves lives but uh you might have heard that and end to encryption also helps criminals. Uh that's the reasoning that uh authorities target end to encryption by like uh but even if you ban end to encryption criminals will use it anyways because

they are bad people. Banning into an encryption disproportionately harms vulnerable people. And this is an example that uh the internet society uh published a blog blog post titled encryption is a preventive preventative tool that protects children. Uh which explains how encryption is beneficial for children which uh who can be considered vulnerable on the internet otherwise. Uh you might have heard that HTTPS or TLS also helps criminals. But if you ban HTTPS, you are not secure. Like everyone is not secure on the internet. So let's go back to talking tech. uh messaging layer security is an inter interoperable

way of doing end to end encryption and uh specifically it provides key exchange and there are two two major documents in MLS which is one one is the protocol RFC 9420 and the architecture document RFC 9750 which got an RFC number very recently. Uh today's topic is the implementation of the protocol and we kind of have our RFC 9420 on Ruby and so you can say that MLS brings interoperability in secure messaging. We could say that SMTP is MLS is essentially SMTP o [laughter] and and uh I tried to uh call my gem MLS but MLS gem already exists for a defunct

website. uh and it so I the name is uh uh the name of the gem is called mellos which is taken taken from novel that's in uh most Japanese middle school textbooks [laughter] so let's talk about the protocol uh this part will be the major update from the ruby kagi talk so uh you might have heard of symmetric and asymmetric crypto and symmetric crypto is the one that uses same key for both encryption and decryption and asymmetric crypto is uh also known This public key cryptography uh it works like this.

Alice keeps a pair of keys which is public key and a private key and she uh publishes a public key on the internet. Then Bob encrypts the plain text which uh which he wants to send to Alice uh using Alice's public key. Then Alice can decrypt the cipher text uh the encrypted text with her private key and to to do this encrypted publicly decrypt with private key thing. Um you you might have heard this in TLS but there's a uh there's a formalized way to do this called HPKE. Uh you might have heard this from uh before because I've talked about uh HPKE in Ruby at Ruby comp Taiwan the last time 2023. So this is a combination of key encapsulation

mechanism which is a asymmetric crypto and a key derivation function which is essentially a hash and authenticated encryption with a associated data AEA which is a symmetric crypto and this is already available in Ruby as an HPKE gem. So uh there are two major characteristics you want out of secure messaging. One is forward secrecy which means messages sent at a certain point in time are are secure in the face of later compromise of a group member which means uh it is secure against the harvest uh plots of uh uh cipher text now then decrypt later if you with a higher compensation of power and the other is a post compromised security

which means messages are secure even if a group member is compromised at some point in the past. So each member updates their key frequently so that the group secrets are not always encrypted with the same private key all the time. So forward secrecy uh provides security before the compromise. Then the post compromised security uh provides security after the fact the compromise took place. Uh to achieve this in two person it is uh kind of easy. uh you first get uh uh both uh two parties start with the uh same common secret chain key zero and to send message one uh you uh use a chain key zero and you take the hmach shot 256

of that and you the next chain key will be uh produced using the hmach shot 256 of a diff using a different uh 0x 01 and 0x02. So the the message key and chain key won't won't be equal and but doing this with three plus persons is difficult uh meaning uh which means it is uh very difficult to uh agree on a same secret for three plus persons. If you want to extend a two two plus uh two person method to for everyone the number of edges in an endnode complete graph is o n squared which is which is problematic. So uh MLS uses your favorite data structure that turns O into O log N which is trees. Uh to do

trees with arrays uh MLS uses a complete balance tree. So uh this can be described as a flat array like this. So uh the the tree on the right will be written as zero uh 0 1 2 3 uh up to nine. The the 10 is missing so it's a nil. Then then the next is an 11. Then then the rest is nil. So the leaf nodes are always even numbered 0 2 4 68 and the parent nodes which have a a child will be always odd odd index and we define operations such as left of an index right of an index based on the characteristics and and these algorithms are all defined in the appendix of the RSC 9420 and it comes

with a serialization and d serialization format so it's pretty convenient. Um so to agree upon a common secret there are three parts to MLS and this is a diagram from the RFC but it's kind of confusing to be honest because uh it hides the fact that there are actually three parts to it. So the tree uh there are two trees uh one is a one's called ratchet tree on the left and one's called uh the secret tree on right and users in the group uh group will be assigned a leaf in the r uh both the ratchet tree and the secret tree. So you will be uh if uh the first user will be uh uh index zero on both trees. Second user will be index two index four and

and so on. So what you want to do is you want to agree with other users in the group upon a common secret which is called the epoch secret of n which is the n is the uh epoch number or generation number. So for the first part is secret tree. If you have a common secret which is the epoch secret, you can der uh the group can derive the same symmetric keys for encrypting messages created using the secret tree. uh to create the secret uh you go go down from the root and uh produce secrets from uh in in the tree left right you you take the hazard of uh common secret then uh uh create secrets down to the leaf

then uh the actual ratchet ratcheting part t uh looks like this which looks very daunting but it's essentially boils down to this you derive a key and the nons from the same ratchet secret of the node Then uh the next ratchet secret will be uh uh produced use uh from the uh tree uh ratchet secret of the last generation or the last message. Then uh this uh compare this with a two uh two-party method of hash ratcheting. Uh it looks pretty similar. And uh the next part is called the key schedule. uh with uh with the last epoch secret of the last generation and the common commit secret the the group can agree upon the next epoch secret uh uh

commit secret and epoch secret n is uh used to create the epoch secret n plus one. So if we assume we can get the commit secret for every member every uh we can assume that the group secret state is consistent among all the group members. Uh this part is not nothing much different. It's uh it's a hash uh of uh these two strings uh connected together. It's essentially like that. But the uh the hardest part is called a treekm which is agreeing upon the commit secret.

So uh we said that uh users are assigned a leaf node and users have their each leaf key pair the uh public and private key pair. So uh if when a user wants to update the group secret the user will f first create uh create an update path. So how the update path is created is the user generates a random path secret at their leaf and then then the par the parents path path secret uh the parent of zero is one. So one's pass secret is calculated using the child child's path secret zero which take a hash of that then calculate that up to the root 137.

Then the commit secret is calculated from the root path secret the root uh seven's pass secret. So it looks like this. Pass secret zero is a random value. Pass secret one is the hash of the path secret. Uh pass zero password is three is created using pass secret one. Then the commit secret uh of the entire tree uh is created using uh the roots path secret which is password is seven. So when zero the user zero creates its update path which is a yellow we will uh we want to find the co-path nodes which are uh which are described in green uh along the update path. So uh copath node of zero is two one is five and three is

seven. Then if all parent nodes in the tree are populated like this, we uh we will encrypt the path secret of of the uh to the key of its copath. Let's say uh one's uh one's path secret is uh uh encrypted using two uh three will be encrypted using five, seven will be encrypted using 11. And the public keys for parent nodes are der uh uh with the keys to encrypt are derived from path secrets. So we can see that this is possible in a two-leaf tree. Let's say zero creates an update path. Node one has a path secret and it is used to create the uh commit secret and the path secret of one is

encrypted to two's user two's public key and node two knows uh the private key to node two which means uh uh so user at node two can decrypt one's uh path secret. know user two can create the the commit secret and uh zero and two knows the common commit secret but what happens if there are blanks in the tree and there uh there will be blanks in the tree so what happen uh what we'll do is we calculate the resolution of the copath node to figure out which node keys are available uh we uh we'll describe that later and when we we encrypt the path secret of update

path node to each key of that resolution So to calculate the resolution uh the the resolution of the node indicated on the right is three uh will become 32 in that order. Uh the indicated node in purple has unmerged leaves uh which will come out later. Uh so its resolution is itself index three and the list of emerged leaves which is two. Uh so uh in this case uh the resolution of the node node at the root will become 32 914 in that order. Uh so uh resolution uh for the root was calculated using the resolution from the left which is 32.

Then uh the right has a blank so it goes down uh and the left is nine. The uh go down and uh the resolution is 14 go at the end. So resolution is used to figure out the set of keys that are necessary to encrypt to every node under that node which which means uh to encrypt is a user 0 2 6 8 10 and 14 on that tree you you need to have the keys of 3 2 9 and 14. So actually what you do is you calculate the path secrets for each node on update path 1 3 and 7 then encrypt the path secrets to each key on the resolution of the copass node which is in this case

2511 and if there are blanks uh you figure out like uh I've described before so uh encrypting the commit secret itself and sending to everyone like uh you can encrypt the commit secret to two 4 6 8 10 12 14's uh public keys and send it to everyone. But that is O where uh where N is number of members but encrypting the commit secret along the co-path reduces the number of encryptions needed to O login where in this case it's 2 five and 11.

So it significantly reduces the number of encryptions needed. So to summarize this, the secret tree the part of part on the right gives you the per message sender keys based on the epoch secret and the key schedule gives you the next epoch secret based on the current epoch secret and commit secret. Then uh to agree upon the commit secret uh uh you use tree km and you get the commit secret uh uh for each member to send act uh to uh to encrypt actual messages. Uh you uh you will create a framed content you uh you uh sign it with and get an authenticated content.

Then uh you wrap it inside a private message which uh which will take the authenticated content and the secret tree and and uh encrypt it to the uh using this uh sender sender key and to create a group uh and to use the group uh group is updated through messages called proposals and commits. A proposals will add and remove users from the group. uh notifies an update of a user's leaf key and injects pre pre-shared keys and the commits will fix those informations and advances group epoch into the next generation and the updated p of the the updated path use create created before are uh conveyed in the commit message

and so group will be created uh by adding a user's leaf node into a zero node node group then starts with a random epoch secret like this uh this is from a work in prog progress version of the uh MLS implementation. So uh it will create the first ratchet tree with uh a single node leaf index of your user will be zero. Then uh then uh you start with a random epoch secret. Then uh use uh the users who want to join to that group will publish their uh identity using a key package uh which is uh you like you will publish your public key and then you'll uh they will receive a welcome message that has a group state

encrypted to that public key. So uh so when when a user create a creates the initial group and to add user b a sends an ad proposal adding b then commits that ad proposal then the a will send a welcome message to introduce b into the group uh which has the encrypted group state of uh uh the group. Then uh you will do the same for adding c. Uh what happens to the tree is when you when you add uh when you add a user you added a leaf node from the key package to the leftmost empty node.

Let's say uh in this case uh 10 is empty. So 10 uh you will add the leaf node to 10. Then uh you will add the leaf index of the 10 to uh the intermediate nodes unmerged list. So uh 9 11 and seven will have 10 as unmerged leaves. uh to update the key for let's say user B the B user B will send an update proposal notifying the group of its uh keys update then you uh the let's say another user commits that update to uh fix that to the uh group state so what happens is when you you will replace the leaf node with an updated uh leaf node then uh you will blank all the

nodes in on a dark path up to the root let's say a four up four updates its keys then five three and seven will be blanked. uh then uh to remove a user let's say Z sends a removal remove com commit to remove B and commits it and uh all uh uh all the group members will be notified of the uh removal of B but B cannot decrypt the path secret of the new commits update path because B uh B will not uh B will uh the the groups uh new state will not be uh

uh encrypted to B's public key. So B will not know the next epoch secret. So B cannot advance to the next generation. So what happens is uh when removing uh you will blank the specified node let's say number eight and the uh then uh blank all the nodes uh let's say 9 11 and seven but uh but in this case the uh right half of the tree is fully empty. So the next route will be three and the tree will be 0 to six. So uh the uh the intermediate parent nodes will be filled during the processing of a commit. Uh and uh when processing a commit the update path inside the commit will be merged into ratchetry and uh the parent nodes are created based on the

update path. So there are lots of stuff that I'm uh omitting here like injection of pre-shared keys and transcript hashes and signing of verification of messages and but it's already getting too long for uh uh explaining this protocol. So uh I might do it in this uh a different talk or a different blog post later. So I'll uh I'll explain about like uh ongoing and future work about this uh implementation and uh encryption in itself. Uh uh like cryptography in Ruby. uh the uh current implementation state is like it passes most test vectors and uh I've added uh some updates to the uh implementation like passing messages uh I don't know parsing messages now using

uh uses a string IO instead of using two strings and move this uh move the group uh all the group state into a group object but it needs a user friendly client API to use it because uh you don't uh you don't want to like uh create a frame content authenticated content wrap it in an app app methods that is that is too difficult for users to use. So so it uh needs user friendly API and I want to test it against other uh other languages implementations and it kind of looks like this right now.

You will uh create a client. You uh create a key package of that client and you create a leaf node and you create uh create a group with that leaf node and you uh you will add uh group uh user two's group uh leaf leaf node with an ad proposal. Then uh you will commit it and group two will receive a welcome and it's uh I'm currently working on this. So uh there is like uh some uh stuff that you can contribute to like uh there is very heavy refactoring uh needed and uh I'm currently wrapping uh raw values that are in the test uh test suit into the classes. Uh one one is a group

that's being worked on and now uh but the ratchet tree is now represented as raw array like uh I described. So that needs some heavy refactoring and also uh you can help with interoperability testing like uh learning learning other implementations and create test scenarios and there's already autom automated interop suit in other languages implementation that uses gRPC. So in uh uh adding that to Ruby will be very helpful. Uh so there are some other works on uh Ruby's crypto crypto ecosystem like uh HPK using OpenSSL's API. I've talked about this in the last Ruby of Taiwan and uh I'm I'm I'm thinking it uh it kind might might not

might might not be a good idea because the protocols needs the whole ciphers too not only uh the functions and you want to access constants such as hash and key length like this. Uh so to get the sender data key and data nons uh you you uh you you need to know how long the hash length is how long the key length and the nons length with uh which you cannot directly access through uh open SSL's API and uh this uh this might not be a missing feature from OpenSSL gem but uh I found it difficult to uh uh have a consistent API through uh the normal ECs and adverse curves such as X25519 and X4 4A like uh this is uh you if you want to check a check if the

private key and a public care public key corresponds actually makes a pair you have there's a uh uh there's a different API for Edward's curve and a normal EC and uh uh also getting uh getting uh the key pair as a uncreo point representation form which is required in the uh RFC that that uh you have you have to use a different API for uh Edward's curve and EC and also uh OpenSSL 3.350 ships with the postquantum cryptography like and there's an internet draft that tries to add ML cm into MLS so that that might that also might be another another place to work on and uh you might be thinking MLS is

into an encryption so it's a client size thing it's it's a Ruby Ruby is a serverside lang Ruby is Rails right no Ruby is not just Rails we have Ruby wom >> [laughter] >> So to uh to work with Ruby Wom uh browser uses web crypto API but uh actually ruby wom is opensl compiling wom but it's uh it's more performant to do through web crypto API. So I I want to have a unified API wrapper that works on desktop browser and embedded like uh embedded TLS which uh which has uh which is already supported in picor ruby. So I I have a very quick example of calling web crypto APIs random number generator like this but uh I want to have a more cleaner API. So I'm currently working on that.

So to end this talk let's answer the question in the description of the talk. Uh why do you need end to encryption in Ruby? Because one Ruby needs to stay relevant. Uh I've I've talked about the same thing uh in the last Ruby conf uh Taiwan talk. Uh if you don't have modern cryptography in Ruby, uh people would just use Python, Go, Rust, whatever the cool kids use these days, but Python doesn't have an MLS implementation. So it's a win for Ruby. And two, Ruby need needs freedom on the internet and the free internet needs Ruby. And so the major takeaways from this talk, we h we have interoperable ways of doing

end toend encryption and we kind of have this in Ruby too. And for uh for your daily uses uh please use secure messaging in your daily life. And I would like to give some shout outs to the messaging layer security working group. Uh congratulations for them to uh uh get uh getting their uh MLS architecture document as an RSC and uh also there shout outs to the protocol implementators uh in Ruby. the list is growing and also uh thanks for h uh thanks to the Ruby Comp Taiwan organizers for having me here here.

So if you have questions, comments uh uh you can find me on Twitter at s01 or fettyverse s01 at ruby.social or find me in the venue and and at drinkups. Uh I was planning to be at the official cold party, but I'm uh I'm kind of feeling not not so good today. So, uh, I I I am not I will not be at the code party today. Sorry. And that's the end of my talk. Thank Thank you very much.

Related talks