Olm: A Cryptographic Ratchet
An implementation of the double cryptographic ratchet described by https://whispersystems.org/docs/specifications/doubleratchet/.
This document uses ∥ to represent string concatenation. When ∥ appears on the right hand side of an = it means that the inputs are concatenated. When ∥ appears on the left hand side of an = it means that the output is split.
When this document uses ECDH(KA, KB) it means that each party computes a Diffie-Hellman agreement using their private key and the remote party's public key. So party A computes ECDH(KpublicB, KprivateA) and party B computes ECDH(KpublicA, KprivateB).
Where this document uses HKDF(salt, IKM, info, L) it refers to the HMAC-based key derivation function with a salt value of salt, input key material of IKM, context string info, and output keying material length of L bytes.
The Olm Algorithm
The setup takes four Curve25519 inputs: Identity keys for Alice and Bob, IA and IB, and one-time keys for Alice and Bob, EA and EB. A shared secret, S, is generated using Triple Diffie-Hellman. The initial 256 bit root key, R0, and 256 bit chain key, C0, 0, are derived from the shared secret using an HMAC-based Key Derivation Function using SHA-256 as the hash function (HKDF-SHA-256) with default salt and "OLM_ROOT" as the info.
Advancing the root key
Advancing a root key takes the previous root key, Ri − 1, and two Curve25519 inputs: the previous ratchet key, Ti − 1, and the current ratchet key Ti. The even ratchet keys are generated by Alice. The odd ratchet keys are generated by Bob. A shared secret is generated using Diffie-Hellman on the ratchet keys. The next root key, Ri, and chain key, Ci, 0, are derived from the shared secret using HKDF-SHA-256 using Ri − 1 as the salt and "OLM_RATCHET" as the info.
Advancing the chain key
Advancing a chain key takes the previous chain key, Ci, j − i. The next chain key, Ci, j, is the HMAC-SHA-256 of "\x02" using the previous chain key as the key.
Creating a message key
Creating a message key takes the current chain key, Ci, j. The message key, Mi, j, is the HMAC-SHA-256 of "\x01" using the current chain key as the key. The message keys where i is even are used by Alice to encrypt messages. The message keys where i is odd are used by Bob to encrypt messages.
The Olm Protocol
Creating an outbound session
Bob publishes the public parts of his identity key, IB, and some single-use one-time keys EB.
Alice downloads Bob's identity key, IB, and a one-time key, EB. She generates a new single-use key, EA, and computes a root key, R0, and a chain key C0, 0. She also generates a new ratchet key T0.
Sending the first pre-key messages
Alice computes a message key, M0, j, and a new chain key, C0, j + 1, using the current chain key. She replaces the current chain key with the new one.
Alice encrypts her plain-text with the message key, M0, j, using an authenticated encryption scheme (see below) to get a cipher-text, X0, j.
- She then sends the following to Bob:
- The public part of her identity key, IA
- The public part of her single-use key, EA
- The public part of Bob's single-use key, EB
- The current chain index, j
- The public part of her ratchet key, T0
- The cipher-text, X0, j
Alice will continue to send pre-key messages until she receives a message from Bob.
Creating an inbound session from a pre-key message
Bob receives a pre-key message as above.
Bob looks up the private part of his single-use key, EB. He can now compute the root key, R0, and the chain key, C0, 0, from IA, EA, IB, and EB.
Bob then advances the chain key j times, to compute the chain key used by the message, C0, j. He now creates the message key, M0, j, and attempts to decrypt the cipher-text, X0, j. If the cipher-text's authentication is correct then Bob can discard the private part of his single-use one-time key, EB.
Bob stores Alice's initial ratchet key, T0, until he wants to send a message.
Sending normal messages
Once a message has been received from the other side, a session is considered established, and a more compact form is used.
To send a message, the user checks if they have a sender chain key, Ci, j. Alice uses chain keys where i is even. Bob uses chain keys where i is odd. If the chain key doesn't exist then a new ratchet key Ti is generated and a new root key Ri and chain key Ci, 0 are computed using Ri − 1, Ti − 1 and Ti.
A message key, Mi, j is computed from the current chain key, Ci, j, and the chain key is replaced with the next chain key, Ci, j + 1. The plain-text is encrypted with Mi, j, using an authenticated encryption scheme (see below) to get a cipher-text, Xi, j.
- The user then sends the following to the recipient:
- The current chain index, j
- The public part of the current ratchet key, Ti
- The cipher-text, Xi, j
The user receives a message as above with the sender's current chain index, j, the sender's ratchet key, Ti, and the cipher-text, Xi, j.
The user checks if they have a receiver chain with the correct i by comparing the ratchet key, Ti. If the chain doesn't exist then they compute a new root key, Ri, and a new receiver chain, with chain key Ci, 0, using Ri − 1, Ti − 1 and Ti.
If the j of the message is less than the current chain index on the receiver then the message may only be decrypted if the receiver has stored a copy of the message key Mi, j. Otherwise the receiver computes the chain key, Ci, j. The receiver computes the message key, Mi, j, from the chain key and attempts to decrypt the cipher-text, Xi, j.
If the decryption succeeds the receiver updates the chain key for Ti with Ci, j + 1 and stores the message keys that were skipped in the process so that they can decode out of order messages. If the receiver created a new receiver chain then they discard their current sender chain so that they will create a new chain when they next send a message.
The Olm Message Format
Olm uses two types of messages. The underlying transport protocol must provide a means for recipients to distinguish between them.
Olm messages start with a one byte version followed by a variable length payload followed by a fixed length message authentication code.
+--------------+------------------------------------+-----------+ | Version Byte | Payload Bytes | MAC Bytes | +--------------+------------------------------------+-----------+
The version byte is "\x03".
The payload consists of key-value pairs where the keys are integers and the values are integers and strings. The keys are encoded as a variable length integer tag where the 3 lowest bits indicates the type of the value: 0 for integers, 2 for strings. If the value is an integer then the tag is followed by the value encoded as a variable length integer. If the value is a string then the tag is followed by the length of the string encoded as a variable length integer followed by the string itself.
Olm uses a variable length encoding for integers. Each integer is encoded as a sequence of bytes with the high bit set followed by a byte with the high bit clear. The seven low bits of each byte store the bits of the integer. The least significant bits are stored in the first byte.
|Ratchet-Key||0x0A||String||The public part of the ratchet key, Ti, of the message|
|Chain-Index||0x10||Integer||The chain index, j, of the message|
|Cipher-Text||0x22||String||The cipher-text, Xi, j, of the message|
The length of the MAC is determined by the authenticated encryption algorithm being used. (Olm version 1 uses HMAC-SHA-256, truncated to 8 bytes). The MAC protects all of the bytes preceding the MAC.
Olm pre-key messages start with a one byte version followed by a variable length payload.
+--------------+------------------------------------+ | Version Byte | Payload Bytes | +--------------+------------------------------------+
The version byte is "\x03".
The payload uses the same key-value format as for normal messages.
|One-Time-Key||0x0A||String||The public part of Bob's single-use key, Eb.|
|Base-Key||0x12||String||The public part of Alice's single-use key, Ea.|
|Identity-Key||0x1A||String||The public part of Alice's identity key, Ia.|
|Message||0x22||String||An embedded Olm message with its own version and MAC.|
Olm Authenticated Encryption
Version 1 of Olm uses AES-256 in CBC mode with PKCS#7 padding for encryption and HMAC-SHA-256 (truncated to 64 bits) for authentication. The 256 bit AES key, 256 bit HMAC key, and 128 bit AES IV are derived from the message key using HKDF-SHA-256 using the default salt and an info of "OLM_KEYS".
The plain-text is encrypted with AES-256, using the key AES_KEYi, j and the IV AES_IVi, j to give the cipher-text, Xi, j.
Then the entire message (including the Version Byte and all Payload Bytes) are passed through HMAC-SHA-256. The first 8 bytes of the MAC are appended to the message.
Message authentication concerns
To avoid unknown key-share attacks, the application must include identifying data for the sending and receiving user in the plain-text of (at least) the pre-key messages. Such data could be a user ID, a telephone number; alternatively it could be the public part of a keypair which the relevant user has proven ownership of.
Alice publishes her public Curve25519 identity key, IA. Eve publishes the same identity key, claiming it as her own. Bob downloads Eve's keys, and associates IA with Eve. Alice sends a message to Bob; Eve intercepts it before forwarding it to Bob. Bob believes the message came from Eve rather than Alice.
This is prevented if Alice includes her user ID in the plain-text of the pre-key message, so that Bob can see that the message was sent by Alice originally.
Bob publishes his public Curve25519 identity key, IB. Eve publishes the same identity key, claiming it as her own. Alice downloads Eve's keys, and associates IB with Eve. Alice sends a message to Eve; Eve cannot decrypt it, but forwards it to Bob. Bob believes the Alice sent the message to him, wheras Alice intended it to go to Eve.
This is prevented by Alice including the user ID of the intended recpient (Eve) in the plain-text of the pre-key message. Bob can now tell that the message was meant for Eve rather than him.
The Olm specification (this document) is hereby placed in the public domain.
Can be sent to mark at matrix.org.
The ratchet that Olm implements was designed by Trevor Perrin and Moxie Marlinspike - details at https://whispersystems.org/docs/specifications/doubleratchet/. Olm is an entirely new implementation written by the Matrix.org team.