Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
matrixorg
Olm
Commits
e273189a
Commit
e273189a
authored
May 01, 2019
by
Aaron Raimist
Committed by
Hubert Chathi
May 14, 2019
Browse files
Convert megolm.rst to markdown
Signedoffby:
Aaron Raimist
<
aaron@raim.ist
>
parent
0757e6df
Changes
1
Hide whitespace changes
Inline
Sidebyside
docs/megolm.
rst
→
docs/megolm.
md
View file @
e273189a
.. Copyright 2016 OpenMarket Ltd
..
.. Licensed under the Apache License, Version 2.0 (the "License");
.. you may not use this file except in compliance with the License.
.. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Megolm group ratchet
====================
# Megolm group ratchet
An AESbased cryptographic ratchet intended for group communications.
.. contents::
Background

## Background
The Megolm ratchet is intended for encrypted messaging applications where there
may be a large number of recipients of each message, thus precluding the use of
peertopeer encryption systems such as
`
Olm
`_
.
peertopeer encryption systems such as
[
Olm
][]
.
It also allows a recipient to decrypt received messages multiple times. For
instance, in client/server applications, a copy of the ciphertext can be stored
on the (untrusted) server, while the client need only store the session keys.
Overview

## Overview
Each participant in a conversation uses their own outbound session for
encrypting messages. A session consists of a ratchet and an
`
Ed25519
`_
keypair.
encrypting messages. A session consists of a ratchet and an
[
Ed25519
][]
keypair.
Secrecy is provided by the ratchet, which can be wound forwards but not
backwards, and is used to derive a distinct message key for each message.
...
...
@@ 48,54 +28,54 @@ channels. Provided that peertopeer channel provides authenticity of the
messages to the participants and deniability of the messages to third parties,
the Megolm session will inherit those properties.
The Megolm ratchet algorithm

## The Megolm ratchet algorithm
The Megolm ratchet
:math:
`R_i` consists of four parts,
:math:
`R_{i,j}` for
:math:
`j \in {0,1,2,3}`. The length of each part depends on the hash function
The Megolm ratchet
$
`R_i`
$
consists of four parts,
$
`R_{i,j}`
$
for
$
`j \in {0,1,2,3}`
$
. The length of each part depends on the hash function
in use (256 bits for this version of Megolm).
The ratchet is initialised with cryptographicallysecure random data, and
advanced as follows:
.. math::
\begin{align}
R_{i,0} &=
\begin{cases}
H_0\left(R_{2^24(n1),0}\right) &\text{if }\exists n  i = 2^24n\\
R_{i1,0} &\text{otherwise}
\end{cases}\\
R_{i,1} &=
\begin{cases}
H_1\left(R_{2^24(n1),0}\right) &\text{if }\exists n  i = 2^24n\\
H_1\left(R_{2^16(m1),1}\right) &\text{if }\exists m  i = 2^16m\\
R_{i1,1} &\text{otherwise}
\end{cases}\\
R_{i,2} &=
\begin{cases}
H_2\left(R_{2^24(n1),0}\right) &\text{if }\exists n  i = 2^24n\\
H_2\left(R_{2^16(m1),1}\right) &\text{if }\exists m  i = 2^16m\\
H_2\left(R_{2^8(p1),2}\right) &\text{if }\exists p  i = 2^8p\\
R_{i1,2} &\text{otherwise}
\end{cases}\\
R_{i,3} &=
\begin{cases}
H_3\left(R_{2^24(n1),0}\right) &\text{if }\exists n  i = 2^24n\\
H_3\left(R_{2^16(m1),1}\right) &\text{if }\exists m  i = 2^16m\\
H_3\left(R_{2^8(p1),2}\right) &\text{if }\exists p  i = 2^8p\\
H_3\left(R_{i1,3}\right) &\text{otherwise}
\end{cases}
\end{align}
where :math:`H_0`, :math:`H_1`, :math:`H_2`, and :math:`H_3` are different hash
functions. In summary: every :math:`2^8` iterations, :math:`R_{i,3}` is
reseeded from :math:`R_{i,2}`. Every :math:`2^16` iterations, :math:`R_{i,2}`
and :math:`R_{i,3}` are reseeded from :math:`R_{i,1}`. Every :math:`2^24`
iterations, :math:`R_{i,1}`, :math:`R_{i,2}` and :math:`R_{i,3}` are reseeded
from :math:`R_{i,0}`.
The complete ratchet value, :math:`R_{i}`, is hashed to generate the keys used
to encrypt each message. This scheme allows the ratchet to be advanced an
```
math
\begin{aligned}
R_{i,0} &=
\begin{cases}
H_0\left(R_{2^24(n1),0}\right) &\text{if }\exists n  i = 2^24n\\
R_{i1,0} &\text{otherwise}
\end{cases}\\
R_{i,1} &=
\begin{cases}
H_1\left(R_{2^24(n1),0}\right) &\text{if }\exists n  i = 2^24n\\
H_1\left(R_{2^16(m1),1}\right) &\text{if }\exists m  i = 2^16m\\
R_{i1,1} &\text{otherwise}
\end{cases}\\
R_{i,2} &=
\begin{cases}
H_2\left(R_{2^24(n1),0}\right) &\text{if }\exists n  i = 2^24n\\
H_2\left(R_{2^16(m1),1}\right) &\text{if }\exists m  i = 2^16m\\
H_2\left(R_{2^8(p1),2}\right) &\text{if }\exists p  i = 2^8p\\
R_{i1,2} &\text{otherwise}
\end{cases}\\
R_{i,3} &=
\begin{cases}
H_3\left(R_{2^24(n1),0}\right) &\text{if }\exists n  i = 2^24n\\
H_3\left(R_{2^16(m1),1}\right) &\text{if }\exists m  i = 2^16m\\
H_3\left(R_{2^8(p1),2}\right) &\text{if }\exists p  i = 2^8p\\
H_3\left(R_{i1,3}\right) &\text{otherwise}
\end{cases}
\end{aligned}
```
where $
`H_0`
$, $
`H_1`
$, $
`H_2`
$, and $
`H_3`
$ are different hash
functions. In summary: every $
`2^8`
$ iterations, $
`R_{i,3}`
$ is
reseeded from $
`R_{i,2}`
$. Every $
`2^16`
$ iterations, $
`R_{i,2}`
$
and $
`R_{i,3}`
$ are reseeded from $
`R_{i,1}`
$. Every $
`2^24`
$
iterations, $
`R_{i,1}`
$, $
`R_{i,2}`
$ and $
`R_{i,3}`
$ are reseeded
from $
`R_{i,0}`
$.
The complete ratchet value, $
`R_{i}`
$, is hashed to generate the keys used
to encrypt each message. This scheme allows the ratchet to be advanced an
arbitrary amount forwards while needing at most 1023 hash computations. A
client can decrypt chat history onwards from the earliest value of the ratchet
it is aware of, but cannot decrypt history from before that point without
...
...
@@ 106,92 +86,88 @@ another from a point in the conversation onwards by giving a copy of the
ratchet at that point in the conversation.
The Megolm protocol

## The Megolm protocol
Session setup
~~~~~~~~~~~~~
### Session setup
Each participant in a conversation generates their own Megolm session. A
session consists of three parts:
* a 32 bit counter,
:math:
`i`.
* an
`
Ed25519
`_
keypair,
:math:
`K`.
* a ratchet,
:math:
`R_i`, which consists of four 256bit values,
:math:
`R_{i,j}` for
:math:
`j \in {0,1,2,3}`.
*
a 32 bit counter,
$
`i`
$
.
*
an
[
Ed25519
][]
keypair,
$
`K`
$
.
*
a ratchet,
$
`R_i`
$
, which consists of four 256bit values,
$
`R_{i,j}`
$
for
$
`j \in {0,1,2,3}`
$
.
The counter
:math:
`i` is initialised to
:math:
`0`. A new Ed25519 keypair is
generated for
:math:
`K`. The ratchet is simply initialised with 1024 bits of
The counter
$
`i`
$
is initialised to
$
`0`
$
. A new Ed25519 keypair is
generated for
$
`K`
$
. The ratchet is simply initialised with 1024 bits of
cryptographicallysecure random data.
A single participant may use multiple sessions over the lifetime of a
conversation. The public part of
:math:
`K` is used as an identifier to
conversation. The public part of
$
`K`
$
is used as an identifier to
discriminate between sessions.
Sharing session data
~~~~~~~~~~~~~~~~~~~~
### Sharing session data
To allow other participants in the conversation to decrypt messages, the
session data is formatted as described in
`
Sessionsharing format
`_
. It is then
session data is formatted as described in
[
Sessionsharing format
](
#Sessionsharingformat
)
. It is then
shared with other participants in the conversation via a secure peertopeer
channel (such as that provided by
`
Olm
`_
).
channel (such as that provided by
[
Olm
][]
).
When the session data is received from other participants, the recipient first
checks that the signature matches the public key. They then store their own
copy of the counter, ratchet, and public key.
Message encryption
~~~~~~~~~~~~~~~~~~
### Message encryption
This version of Megolm uses AES256_ in CBC_ mode with
`
PKCS#7
`_
padding and
This version of Megolm uses AES256_ in CBC_ mode with
[
PKCS#7
][]
padding and
HMACSHA256_ (truncated to 64 bits). The 256 bit AES key, 256 bit HMAC key,
and 128 bit AES IV are derived from the megolm ratchet
:math:
`R_i`:
..
math
::
\begin{align
}
AES\_KEY_{i}\;\parallel\;HMAC\_KEY_{i}\;\parallel\;AES\_IV_{i}
&= HKDF\left(0,\,R_{i},\text{"MEGOLM\_KEYS"},\,80\right) \\
\end{align}
where
:math:
`\parallel` represents string splitting, and
:math:
`HKDF\left(salt,\,IKM,\,info,\,L\right)` refers to the
`
HMACbased key
derivation function
`_
using using
`
SHA256
`_
as the hash function
(
`
HKDFSHA256
`_
) with a salt value of
:math:
`salt`, input key material of
:math:
`IKM`, context string
:math:
`info`, and output keying material length of
:math:
`L` bytes.
The plaintext is encrypted with AES256, using the key
:math:
`AES\_KEY_{i}`
and the IV
:math:
`AES\_IV_{i}` to give the ciphertext,
:math:
`X_{i}`.
The ratchet index
:math:
`i`, and the ciphertext
:math:
`X_{i}`, are then packed
into a message as described in
`
Message format
`_
. Then the entire message
and 128 bit AES IV are derived from the megolm ratchet
$
`R_i`
$
:
```
math
\begin{aligned}
AES\_KEY_{i}\;\parallel\;HMAC\_KEY_{i}\;\parallel\;AES\_IV_{i
}
&= HKDF\left(0,\,R_{i},\text{"MEGOLM\_KEYS"},\,80\right) \\
\end{aligned}
```
where
$
`\parallel`
$
represents string splitting, and
$
`HKDF\left(salt,\,IKM,\,info,\,L\right)`
$
refers to the
[
HMACbased key
derivation function
][]
using using
[
SHA256
][]
as the hash function
(
[
HKDFSHA256
][]
) with a salt value of
$
`salt`
$
, input key material of
$
`IKM`
$
, context string
$
`info`
$
, and output keying material length of
$
`L`
$
bytes.
The plaintext is encrypted with AES256, using the key
$
`AES\_KEY_{i}`
$
and the IV
$
`AES\_IV_{i}`
$
to give the ciphertext,
$
`X_{i}`
$
.
The ratchet index
$
`i`
$
, and the ciphertext
$
`X_{i}`
$
, are then packed
into a message as described in
[
Message format
](
#messageformat
)
. Then the entire message
(including the version bytes and all payload bytes) are passed through
HMACSHA256. The first 8 bytes of the MAC are appended to the message.
Finally, the authenticated message is signed using the Ed25519 keypair; the 64
byte signature is appended to the message.
The complete signed message, together with the public part of
:math:
`K` (acting
The complete signed message, together with the public part of
$
`K`
$
(acting
as a session identifier), can then be sent over an insecure channel. The
message can then be authenticated and decrypted only by recipients who have
received the session data.
Advancing the ratchet
~~~~~~~~~~~~~~~~~~~~~
### Advancing the ratchet
After each message is encrypted, the ratchet is advanced. This is done as
described in `The Megolm ratchet algorithm`_, using the following definitions:
.. math::
\begin{align}
H_0(A) &\equiv HMAC(A,\text{"\textbackslash x00"}) \\
H_1(A) &\equiv HMAC(A,\text{"\textbackslash x01"}) \\
H_2(A) &\equiv HMAC(A,\text{"\textbackslash x02"}) \\
H_3(A) &\equiv HMAC(A,\text{"\textbackslash x03"}) \\
\end{align}
where :math:`HMAC(A, T)` is the HMACSHA256_ of ``T``, using ``A`` as the
described in
[
The Megolm ratchet algorithm
](
#themegolmratchetalgorithm
)
, using the following definitions:
```
math
\begin{aligned}
H_0(A) &\equiv HMAC(A,\text{"\x00"}) \\
H_1(A) &\equiv HMAC(A,\text{"\x01"}) \\
H_2(A) &\equiv HMAC(A,\text{"\x02"}) \\
H_3(A) &\equiv HMAC(A,\text{"\x03"}) \\
\end{aligned}
```
where $
`HMAC(A, T)`
$ is the HMACSHA256 of
``T``
, using
``A``
as the
key.
For outbound sessions, the updated ratchet and counter are stored in the
...
...
@@ 199,59 +175,54 @@ session.
In order to maintain the ability to decrypt conversation history, inbound
sessions should store a copy of their earliest known ratchet value (unless they
explicitly want to drop the ability to decrypt that history  see
`
Partial
Forward Secrecy
`_\
). They may also choose to cache calculated ratchet values,
explicitly want to drop the ability to decrypt that history  see
[
Partial
Forward Secrecy
](
#partialforwardsecrecy
)
). They may also choose to cache calculated ratchet values,
but the decision of which ratchet states to cache is left to the application.
Data exchange formats

## Data exchange formats
Sessionsharing format
~~~~~~~~~~~~~~~~~~~~~~
### Sessionsharing format
The Megolm keysharing format is as follows:
.. code::
+++++++++
 V  i  R(i,0)  R(i,1)  R(i,2)  R(i,3)  Kpub  Signature 
+++++++++
0 1 5 37 69 101 133 165 229 bytes
```
+++++++++
 V  i  R(i,0)  R(i,1)  R(i,2)  R(i,3)  Kpub  Signature 
+++++++++
0 1 5 37 69 101 133 165 229 bytes
```
The version byte,
``V``
, is
``"\x02"``
.
This is followed by the ratchet index,
:math:
`i`, which is encoded as a
bigendian 32bit integer; the ratchet values
:math:
`R_{i,j}`; and the public
part of the Ed25519 keypair
:math:
`K`.
This is followed by the ratchet index,
$
`i`
$
, which is encoded as a
bigendian 32bit integer; the ratchet values
$
`R_{i,j}`
$
; and the public
part of the Ed25519 keypair
$
`K`
$
.
The data is then signed using the Ed25519 keypair, and the 64byte signature is
appended.
Message format
~~~~~~~~~~~~~~
### Message format
Megolm messages consist of a one byte version, followed by a variable length
payload, a fixed length message authentication code, and a fixed length
signature.
.. code::
+++++
 V  Payload Bytes  MAC Bytes  Signature Bytes 
+++++
0 1 N N+8 N+72 bytes
```
+++++
 V  Payload Bytes  MAC Bytes  Signature Bytes 
+++++
0 1 N N+8 N+72 bytes
```
The version byte,
``V``
, is
``"\x03"``
.
The payload uses a format based on the
`
Protocol Buffers encoding
`_
. It
The payload uses a format based on the
[
Protocol Buffers encoding
][]
. It
consists of the following keyvalue pairs:
============= ===== ======== ================================================
Name Tag Type Meaning
============= ===== ======== ================================================
MessageIndex 0x08 Integer The index of the ratchet, :math:`i`
CipherText 0x12 String The ciphertext, :math:`X_{i}`, of the message
============= ===== ======== ================================================
**Name**

**Tag**

**Type**

**Meaning**
::::::::
MessageIndex0x08IntegerThe index of the ratchet, i
CipherText0x12StringThe ciphertext, Xi, of the message
Within the payload, integers are encoded using a variable length encoding. Each
integer is encoded as a sequence of bytes with the high bit set followed by a
...
...
@@ 271,11 +242,9 @@ The length of the signature is determined by the signing algorithm being used
(64 bytes in this version of the protocol). The signature covers all of the
bytes preceding the signature.
Limitations

## Limitations
Message Replays

### Message Replays
A message can be decrypted successfully multiple times. This means that an
attacker can resend a copy of an old message, and the recipient will treat it
...
...
@@ 285,8 +254,7 @@ To mitigate this it is recommended that applications track the ratchet indices
they have received and that they reject messages with a ratchet index that
they have already decrypted.
Lack of Transcript Consistency
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### Lack of Transcript Consistency
In a group conversation, there is no guarantee that all recipients have
received the same messages. For example, if Alice is in a conversation with Bob
...
...
@@ 297,8 +265,7 @@ Solving this is, in general, a hard problem, particularly in a protocol which
does not guarantee inorder message delivery. For now it remains the subject of
future research.
Lack of Backward Secrecy
~~~~~~~~~~~~~~~~~~~~~~~~
### Lack of Backward Secrecy
Once the key to a Megolm session is compromised, the attacker can decrypt any
future messages sent via that session.
...
...
@@ 307,11 +274,10 @@ In order to mitigate this, the application should ensure that Megolm sessions
are not used indefinitely. Instead it should periodically start a new session,
with new keys shared over a secure channel.
..
TODO: Can we recommend sensible lifetimes for Megolm sessions? Probably
depends how paranoid we're feeling, but some guidelines might be useful.
<!
TODO: Can we recommend sensible lifetimes for Megolm sessions? Probably
depends how paranoid we're feeling, but some guidelines might be useful.
>
Partial Forward Secrecy
~~~~~~~~~~~~~~~~~~~~~~~
### Partial Forward Secrecy
Each recipient maintains a record of the ratchet value which allows them to
decrypt any messages sent in the session after the corresponding point in the
...
...
@@ 322,8 +288,7 @@ To mitigate this issue, the application should offer the user the option to
discard historical conversations, by winding forward any stored ratchet values,
or discarding sessions altogether.
Dependency on secure channel for key exchange
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### Dependency on secure channel for key exchange
The design of the Megolm ratchet relies on the availability of a secure
peertopeer channel for the exchange of session keys. Any vulnerabilities in
...
...
@@ 343,20 +308,18 @@ the Megolm session as a forgery.
A second example: if the peertopeer channel is vulnerable to a replay
attack, this can be extended to entire Megolm sessions.
License

The Megolm specification (this document) is licensed under the `Apache License,
Version 2.0 <http://www.apache.org/licenses/LICENSE2.0>`_.
.. _`Ed25519`: http://ed25519.cr.yp.to/
.. _`HMACbased key derivation function`: https://tools.ietf.org/html/rfc5869
.. _`HKDFSHA256`: https://tools.ietf.org/html/rfc5869
.. _`HMACSHA256`: https://tools.ietf.org/html/rfc2104
.. _`SHA256`: https://tools.ietf.org/html/rfc6234
.. _`AES256`: http://csrc.nist.gov/publications/fips/fips197/fips197.pdf
.. _`CBC`: http://csrc.nist.gov/publications/nistpubs/80038a/sp80038a.pdf
.. _`PKCS#7`: https://tools.ietf.org/html/rfc2315
.. _`Olm`: ./olm.html
.. _`Protocol Buffers encoding`: https://developers.google.com/protocolbuffers/docs/encoding
## License
The Megolm specification (this document) is licensed under the Apache License,
Version 2.0 http://www.apache.org/licenses/LICENSE2.0.
[
Ed25519
]:
http://ed25519.cr.yp.to/
[
HMACbased key derivation function
]:
https://tools.ietf.org/html/rfc5869
[
HKDFSHA256
]:
https://tools.ietf.org/html/rfc5869
[
HMACSHA256
]:
https://tools.ietf.org/html/rfc2104
[
SHA256
]:
https://tools.ietf.org/html/rfc6234
[
AES256
]:
http://csrc.nist.gov/publications/fips/fips197/fips197.pdf
[
CBC
]:
http://csrc.nist.gov/publications/nistpubs/80038a/sp80038a.pdf
[
PKCS#7
]:
https://tools.ietf.org/html/rfc2315
[
Olm
]:
https://gitlab.matrix.org/matrixorg/olm/blob/master/docs/olm.md
[
Protocol Buffers encoding
]:
https://developers.google.com/protocolbuffers/docs/encoding
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment