The Double Ratchet Algorithm implementation in Go
Go to file
Andrea Maria Piana 1e33002ac8
[do-not-merge] Bug/allow returning an error in dh (#11)
* Remove encryted header version

* Return error from DH & Encrypt

* Use slices instead of fixed array for keys

In order to support multiple types of curves, this commits changes the
type of `Key` from `[32]byte` to `[]byte`. This is useful as most of the
eliptic keys have a size of a compressed key of `33 bytes` instead of
`32 bytes`, which the default implemetation uses (curve25519).
2019-10-31 16:13:07 +01:00
.gitignore Deleted and removed glide.lock 2017-06-12 22:53:38 +07:00
.golangci.yml Add makefile with lint support (#4) 2018-08-21 11:18:46 +02:00
.travis.yml Added badges and coverall support 2017-06-12 23:31:45 +07:00
LICENSE Initial commit 2017-06-10 16:17:02 +07:00
Makefile Add makefile with lint support (#4) 2018-08-21 11:18:46 +02:00
README.md [do-not-merge] Bug/allow returning an error in dh (#11) 2019-10-31 16:13:07 +01:00
chains.go Wrote tests for kdfChain and kdfRootChain 2017-06-19 11:50:06 +07:00
chains_test.go Add makefile with lint support (#4) 2018-08-21 11:18:46 +02:00
crypto.go [do-not-merge] Bug/allow returning an error in dh (#11) 2019-10-31 16:13:07 +01:00
crypto_test.go Add makefile with lint support (#4) 2018-08-21 11:18:46 +02:00
default_crypto.go [do-not-merge] Bug/allow returning an error in dh (#11) 2019-10-31 16:13:07 +01:00
default_crypto_test.go [do-not-merge] Bug/allow returning an error in dh (#11) 2019-10-31 16:13:07 +01:00
glide.yaml Update references to tiabc 2019-09-10 10:14:35 +02:00
keys_storage.go [do-not-merge] Bug/allow returning an error in dh (#11) 2019-10-31 16:13:07 +01:00
keys_storage_test.go [do-not-merge] Bug/allow returning an error in dh (#11) 2019-10-31 16:13:07 +01:00
message.go [do-not-merge] Bug/allow returning an error in dh (#11) 2019-10-31 16:13:07 +01:00
message_test.go Add makefile with lint support (#4) 2018-08-21 11:18:46 +02:00
options.go Change handling of skipped/deleted keys 2018-11-01 15:15:23 +01:00
options_test.go Add makefile with lint support (#4) 2018-08-21 11:18:46 +02:00
session.go [do-not-merge] Bug/allow returning an error in dh (#11) 2019-10-31 16:13:07 +01:00
session_storage.go Allow to store and load sessions (#3) 2018-08-21 11:12:32 +02:00
session_test.go Don't delete key without explicit request 2019-02-13 16:28:48 +01:00
state.go [do-not-merge] Bug/allow returning an error in dh (#11) 2019-10-31 16:13:07 +01:00
state_test.go [do-not-merge] Bug/allow returning an error in dh (#11) 2019-10-31 16:13:07 +01:00

README.md

doubleratchet

Go Report Card Build Status Coverage Status GoDoc

The Double Ratchet Algorithm is used by two parties to exchange encrypted messages based on a shared secret key. Typically the parties will use some key agreement protocol (such as X3DH) to agree on the shared secret key. Following this, the parties will use the Double Ratchet to send and receive encrypted messages.

The parties derive new keys for every Double Ratchet message so that earlier keys cannot be calculated from later ones. The parties also send Diffie-Hellman public values attached to their messages. The results of Diffie-Hellman calculations are mixed into the derived keys so that later keys cannot be calculated from earlier ones. These properties gives some protection to earlier or later encrypted messages in case of a compromise of a party's keys.

Project status

The library is in beta version and ready for integration into production projects with care. Let me know if you face any problems or have any questions or suggestions.

Implementation notes

The Double Ratchet logic

  1. No more than 1000 messages can be skipped in a single chain.
  2. Skipped messages from a single ratchet step are deleted after 100 ratchet steps.
  3. Both parties' sending and receiving chains are initialized with the shared key so that both of them could message each other from the very beginning.

Cryptographic primitives

  1. GENERATE_DH(): Curve25519
  2. KDF_RK(rk, dh_out): HKDF with SHA-256
  3. KDF_CK(ck): HMAC with SHA-256 and constant inputs
  4. ENCRYPT(mk, pt, associated_data): AES-256-CTR with HMAC-SHA-256 and IV derived alongside an encryption key

Installation

go get github.com/status-im/doubleratchet

then cd into the project directory and install dependencies:

glide up

If glide is not installed, install it.

Usage

Basic usage example

package main

import (
	"fmt"
	"log"

	"github.com/status-im/doubleratchet"
)

func main() {
	// The shared key both parties have already agreed upon before the communication.
	sk := [32]byte{
		0xeb, 0x8, 0x10, 0x7c, 0x33, 0x54, 0x0, 0x20,
		0xe9, 0x4f, 0x6c, 0x84, 0xe4, 0x39, 0x50, 0x5a,
		0x2f, 0x60, 0xbe, 0x81, 0xa, 0x78, 0x8b, 0xeb,
		0x1e, 0x2c, 0x9, 0x8d, 0x4b, 0x4d, 0xc1, 0x40,
	}

	// Diffie-Hellman key pair generated by one of the parties during key exchange or
	// by any other means. The public key MUST be sent to another party for initialization
	// before the communication begins.
	keyPair, err := doubleratchet.DefaultCrypto{}.GenerateDH()
	if err != nil {
		log.Fatal(err)
	}

	// Bob MUST be created with the shared secret and a DH key pair.
	bob, err := doubleratchet.New([]byte("bob-session-id"), sk, keyPair, nil)
	if err != nil {
		log.Fatal(err)
	}

	// Alice MUST be created with the shared secret and Bob's public key.
	alice, err := doubleratchet.NewWithRemoteKey([]byte("alice-session-id"), sk, keyPair.PublicKey(), nil)
	if err != nil {
		log.Fatal(err)
	}

	// Alice can now encrypt messages under the Double Ratchet session.
	m, err := alice.RatchetEncrypt([]byte("Hi Bob!"), nil)

	if err != nil {
		log.Fatal(err)
	}

	// Which Bob can decrypt.
	plaintext, err := bob.RatchetDecrypt(m, nil)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(plaintext))
}

Options

Additional options can be passed to constructors to customize the algorithm behavior:

doubleratchet.New(
    sk, keyPair,
    
    // Your own cryptography supplement implementing doubleratchet.Crypto.
    WithCrypto(c),
    
    // Custom storage for skipped keys implementing doubleratchet.KeysStorage.
    WithKeysStorage(ks),
    
    // The maximum number of skipped keys. Error will be raised in an attempt to store more keys
    // in a single chain while decrypting.
    WithMaxSkip(1200),
    
    // The number of Diffie-Hellman ratchet steps skipped keys will be stored.
    WithMaxKeep(90),
)

License

MIT