Commit 7682be97 authored by Matthew Hodgson's avatar Matthew Hodgson
Browse files

switch RST to MD, thanks to @aaronraimist. fixes spec docs in gitlab

parent 214908ac
Contributing code to libolm
===========================
# Contributing code to libolm
To contribute code to this library, the preferred way is to clone the git
repository, create a git patch series (for example via ``git
......@@ -8,18 +7,16 @@ format-patch --stdout origin/master``), and send this by email to
Naturally, you must be willing to license your contributions under the same
license as the project itself - in this case, Apache Software License v2 (see
`<LICENSE>`_).
[LICENSE](LICENSE)).
Sign off
--------
## Sign off
In order to have a concrete record that your contribution is intentional and
you agree to license it under the same terms as the project's license, we've
adopted the same lightweight approach that the
`Linux Kernel <https://www.kernel.org/doc/Documentation/SubmittingPatches>`_,
`Docker <https://github.com/docker/docker/blob/master/CONTRIBUTING.md>`_,
and many other projects use: the DCO
(`Developer Certificate of Origin <http://developercertificate.org/>`_).
[Linux Kernel](https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin),
[Docker](https://github.com/docker/docker/blob/master/CONTRIBUTING.md),
and many other projects use: the DCO ([Developer Certificate of Origin](http://developercertificate.org/)).
This is a simple declaration that you wrote the contribution or otherwise have
the right to contribute it to Matrix::
......
Olm
===
# Olm
An implementation of the Double Ratchet cryptographic ratchet described by
https://whispersystems.org/docs/specifications/doubleratchet/, written in C and
C++11 and exposed as a C API.
The specification of the Olm ratchet can be found in `<docs/olm.rst>`_.
The specification of the Olm ratchet can be found in [docs/olm.md](docs/olm.md).
This library also includes an implementation of the Megolm cryptographic
ratchet, as specified in `<docs/megolm.rst>`_.
ratchet, as specified in [docs/megolm.md](docs/megolm.md).
Building
--------
## Building
To build olm as a shared library run either:
.. code:: bash
cmake . -Bbuild
cmake --build build
```bash
cmake . -Bbuild
cmake --build build
```
or:
.. code:: bash
make
```bash
make
```
Using cmake is the preferred method for building the shared library; the
Makefile may be removed in the future.
To run the tests when using cmake, run:
.. code:: bash
cd build/tests
ctest .
```bash
cd build/tests
ctest .
```
To run the tests when using make, run:
.. code:: bash
make test
```bash
make test
```
To build the JavaScript bindings, install emscripten from http://kripken.github.io/emscripten-site/ and then run:
.. code:: bash
make js
```bash
make js
```
Note that if you run emscripten in a docker container, you need to pass through
the EMCC_CLOSURE_ARGS environment variable.
To build the android project for Android bindings, run:
.. code:: bash
cd android
./gradlew clean assembleRelease
```bash
cd android
./gradlew clean assembleRelease
```
To build the Xcode workspace for Objective-C bindings, run:
.. code:: bash
cd xcode
pod install
open OLMKit.xcworkspace
```bash
cd xcode
pod install
open OLMKit.xcworkspace
```
To build the Python bindings, first build olm as a shared library as above, and
then run:
.. code:: bash
cd python
make
```bash
cd python
make
```
to make both the Python 2 and Python 3 bindings. To make only one version, use
``make olm-python2`` or ``make olm-python3`` instead of just ``make``.
......@@ -80,27 +77,25 @@ to make both the Python 2 and Python 3 bindings. To make only one version, use
To build olm as a static library (which still needs libstdc++ dynamically) run
either:
.. code:: bash
cmake . -Bbuild -DBUILD_SHARED_LIBS=NO
cmake --build build
```bash
cmake . -Bbuild -DBUILD_SHARED_LIBS=NO
cmake --build build
```
or
.. code:: bash
make static
```bash
make static
```
The library can also be used as a dependency with CMake using:
.. code:: cmake
find_package(Olm::Olm REQUIRED)
target_link_libraries(my_exe Olm::Olm)
```cmake
find_package(Olm::Olm REQUIRED)
target_link_libraries(my_exe Olm::Olm)
```
Release process
---------------
## Release process
First: bump version numbers in ``common.mk``, ``CMakeLists.txt``,
``javascript/package.json``, ``python/olm/__version__.py``, ``OLMKit.podspec``,
......@@ -113,34 +108,32 @@ git.
It's probably sensible to do the above on a release branch (``release-vx.y.z``
by convention), and merge back to master once the release is complete.
.. code:: bash
```bash
make clean
make clean
# build and test C library
make test
# build and test C library
make test
# build and test JS wrapper
make js
(cd javascript && npm run test)
npm pack javascript
# build and test JS wrapper
make js
(cd javascript && npm run test)
npm pack javascript
VERSION=x.y.z
scp olm-$VERSION.tgz packages@ares.matrix.org:packages/npm/olm/
git tag $VERSION -s
git push --tags
VERSION=x.y.z
scp olm-$VERSION.tgz packages@ares.matrix.org:packages/npm/olm/
git tag $VERSION -s
git push --tags
# OLMKit CocoaPod release
# Make sure the version OLMKit.podspec is the same as the git tag
# (this must be checked before git tagging)
pod spec lint OLMKit.podspec --use-libraries --allow-warnings
pod trunk push OLMKit.podspec --use-libraries --allow-warnings
# Check the pod has been successully published with:
pod search OLMKit
```
# OLMKit CocoaPod release
# Make sure the version OLMKit.podspec is the same as the git tag
# (this must be checked before git tagging)
pod spec lint OLMKit.podspec --use-libraries --allow-warnings
pod trunk push OLMKit.podspec --use-libraries --allow-warnings
# Check the pod has been successully published with:
pod search OLMKit
Design
------
## Design
Olm is designed to be easy port to different platforms and to be easy
to write bindings for.
......@@ -150,46 +143,40 @@ API. As development has progressed, it has become clear that C++ gives little
advantage, and new functionality is being added in C, with C++ parts being
rewritten as the need ariases.
Error Handling
~~~~~~~~~~~~~~
### Error Handling
All C functions in the API for olm return ``olm_error()`` on error.
This makes it easy to check for error conditions within the language bindings.
Random Numbers
~~~~~~~~~~~~~~
### Random Numbers
Olm doesn't generate random numbers itself. Instead the caller must
provide the random data. This makes it easier to port the library to different
platforms since the caller can use whatever cryptographic random number
generator their platform provides.
Memory
~~~~~~
### Memory
Olm avoids calling malloc or allocating memory on the heap itself.
Instead the library calculates how much memory will be needed to hold the
output and the caller supplies a buffer of the appropriate size.
Output Encoding
~~~~~~~~~~~~~~~
### Output Encoding
Binary output is encoded as base64 so that languages that prefer unicode
strings will find it easier to handle the output.
Dependencies
~~~~~~~~~~~~
### Dependencies
Olm uses pure C implementations of the cryptographic primitives used by
the ratchet. While this decreases the performance it makes it much easier
to compile the library for different architectures.
Contributing
------------
Please see `<CONTRIBUTING.rst>`_ when making contributions to the library.
## Contributing
Security assessment
-------------------
Please see [CONTRIBUTING.md](CONTRIBUTING.md) when making contributions to the library.
## Security assessment
Olm 1.3.0 was independently assessed by NCC Group's Cryptography Services
Practive in September 2016 to check for security issues: you can read all
......@@ -197,18 +184,16 @@ about it at
https://www.nccgroup.trust/us/our-research/matrix-olm-cryptographic-review/
and https://matrix.org/blog/2016/11/21/matrixs-olm-end-to-end-encryption-security-assessment-released-and-implemented-cross-platform-on-riot-at-last/
Bug reports
-----------
## Bug reports
Please file bug reports at https://github.com/matrix-org/olm/issues
What's an olm?
--------------
## What's an olm?
It's a really cool species of European troglodytic salamander.
http://www.postojnska-jama.eu/en/come-and-visit-us/vivarium-proteus/
Legal Notice
------------
## Legal Notice
The software may be subject to the U.S. export control laws and regulations
and by downloading the software the user certifies that he/she/it is
......
.. 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/LICENSE-2.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 AES-based 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
peer-to-peer encryption systems such as `Olm`_.
peer-to-peer 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,55 +28,55 @@ channels. Provided that peer-to-peer 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 cryptographically-secure random data, and
advanced as follows:
.. math::
\begin{align}
R_{i,0} &=
\begin{cases}
H_0\left(R_{2^24(n-1),0}\right) &\text{if }\exists n | i = 2^24n\\
R_{i-1,0} &\text{otherwise}
\end{cases}\\
R_{i,1} &=
\begin{cases}
H_1\left(R_{2^24(n-1),0}\right) &\text{if }\exists n | i = 2^24n\\
H_1\left(R_{2^16(m-1),1}\right) &\text{if }\exists m | i = 2^16m\\
R_{i-1,1} &\text{otherwise}
\end{cases}\\
R_{i,2} &=
\begin{cases}
H_2\left(R_{2^24(n-1),0}\right) &\text{if }\exists n | i = 2^24n\\
H_2\left(R_{2^16(m-1),1}\right) &\text{if }\exists m | i = 2^16m\\
H_2\left(R_{2^8(p-1),2}\right) &\text{if }\exists p | i = 2^8p\\
R_{i-1,2} &\text{otherwise}
\end{cases}\\
R_{i,3} &=
\begin{cases}
H_3\left(R_{2^24(n-1),0}\right) &\text{if }\exists n | i = 2^24n\\
H_3\left(R_{2^16(m-1),1}\right) &\text{if }\exists m | i = 2^16m\\
H_3\left(R_{2^8(p-1),2}\right) &\text{if }\exists p | i = 2^8p\\
H_3\left(R_{i-1,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
arbitrary amount forwards while needing at most 1020 hash computations. A
```math
\begin{aligned}
R_{i,0} &=
\begin{cases}
H_0\left(R_{2^24(n-1),0}\right) &\text{if }\exists n | i = 2^24n\\
R_{i-1,0} &\text{otherwise}
\end{cases}\\
R_{i,1} &=
\begin{cases}
H_1\left(R_{2^24(n-1),0}\right) &\text{if }\exists n | i = 2^24n\\
H_1\left(R_{2^16(m-1),1}\right) &\text{if }\exists m | i = 2^16m\\
R_{i-1,1} &\text{otherwise}
\end{cases}\\
R_{i,2} &=
\begin{cases}
H_2\left(R_{2^24(n-1),0}\right) &\text{if }\exists n | i = 2^24n\\
H_2\left(R_{2^16(m-1),1}\right) &\text{if }\exists m | i = 2^16m\\
H_2\left(R_{2^8(p-1),2}\right) &\text{if }\exists p | i = 2^8p\\
R_{i-1,2} &\text{otherwise}
\end{cases}\\
R_{i,3} &=
\begin{cases}
H_3\left(R_{2^24(n-1),0}\right) &\text{if }\exists n | i = 2^24n\\
H_3\left(R_{2^16(m-1),1}\right) &\text{if }\exists m | i = 2^16m\\
H_3\left(R_{2^8(p-1),2}\right) &\text{if }\exists p | i = 2^8p\\
H_3\left(R_{i-1,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
reversing the hash function.
......@@ -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 256-bit 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 256-bit 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
cryptographically-secure 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 `Session-sharing format`_. It is then
session data is formatted as described in [Session-sharing format](#Session-sharing-format). It is then
shared with other participants in the conversation via a secure peer-to-peer
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 AES-256_ in CBC_ mode with `PKCS#7`_ padding and
This version of Megolm uses AES-256_ in CBC_ mode with [PKCS#7][] padding and
HMAC-SHA-256_ (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 `HMAC-based key
derivation function`_ using using `SHA-256`_ as the hash function
(`HKDF-SHA-256`_) 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 plain-text is encrypted with AES-256, using the key :math:`AES\_KEY_{i}`
and the IV :math:`AES\_IV_{i}` to give the cipher-text, :math:`X_{i}`.
The ratchet index :math:`i`, and the cipher-text :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 [HMAC-based key
derivation function][] using using [SHA-256][] as the hash function
([HKDF-SHA-256][]) with a salt value of $`salt`$, input key material of
$`IKM`$, context string $`info`$, and output keying material length of
$`L`$ bytes.
The plain-text is encrypted with AES-256, using the key $`AES\_KEY_{i}`$
and the IV $`AES\_IV_{i}`$ to give the cipher-text, $`X_{i}`$.
The ratchet index $`i`$, and the cipher-text $`X_{i}`$, are then packed
into a message as described in [Message format](#message-format). Then the entire message
(including the version bytes and all payload bytes) are passed through
HMAC-SHA-256. 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 HMAC-SHA-256_ of ``T``, using ``A`` as the
described in [The Megolm ratchet algorithm](#the-megolm-ratchet-algorithm), 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 HMAC-SHA-256 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](#partial-forward-secrecy)). 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
Session-sharing format
~~~~~~~~~~~~~~~~~~~~~~
### Session-sharing format
The Megolm key-sharing 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
big-endian 32-bit 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
big-endian 32-bit 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 64-byte 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::
+---+------------------------------------+-----------+------------------+