double-ratchet-rs/README.md

118 lines
4.8 KiB
Markdown
Raw Normal View History

2021-05-13 15:49:11 +00:00
# double-ratchet-2
2021-05-13 18:23:43 +00:00
[![Crate](https://img.shields.io/crates/v/double-ratchet-2)](https://crates.io/crates/double-ratchet-2)
[![License](https://img.shields.io/github/license/Decentrailzed-Communication-System/double-ratchet-2)](https://github.com/Decentrailzed-Communication-System/double-ratchet-2/blob/main/LICENSE)
[![Actions](https://img.shields.io/github/workflow/status/Decentrailzed-Communication-System/double-ratchet-2/Rust)](https://github.com/Decentrailzed-Communication-System/double-ratchet-2/actions)
2021-05-13 16:00:35 +00:00
2021-05-13 15:49:11 +00:00
Implementation of the double ratchet system/encryption as specified by [Signal][1].
The implementation follows the cryptographic recommendations provided by [Signal][2].
The AEAD Algorithm uses a constant Nonce. This might be changed in the future.
## Example Usage:
### Standard:
```rust
use double_ratchet_2::ratchet::Ratchet;
let sk = [1; 32]; // Initial Key created by a symmetric key agreement protocol
let (mut bob_ratchet, public_key) = Ratchet::init_bob(sk); // Creating Bobs Ratchet (returns Bobs PublicKey)
let mut alice_ratchet = Ratchet::init_alice(sk, public_key); // Creating Alice Ratchet with Bobs PublicKey
let data = b"Hello World".to_vec(); // Data to be encrypted
2021-05-14 16:22:18 +00:00
let (header, encrypted, nonce) = alice_ratchet.ratchet_encrypt(&data); // Encrypting message with Alice Ratchet (Alice always needs to send the first message)
let decrypted = bob_ratchet.ratchet_decrypt(&header, &encrypted, &nonce); // Decrypt message with Bobs Ratchet
2021-05-13 15:49:11 +00:00
assert_eq!(data, decrypted)
```
### With lost message:
```rust
let sk = [1; 32]; // Initial Key created by a symmetric key agreement protocol
let (mut bob_ratchet, public_key) = Ratchet::init_bob(sk); // Creating Bobs Ratchet (returns Bobs PublicKey)
let mut alice_ratchet = Ratchet::init_alice(sk, public_key); // Creating Alice Ratchet with Bobs PublicKey
let data = b"Hello World".to_vec(); // Data to be encrypted
2021-05-14 16:22:18 +00:00
let (header1, encrypted1, nonce1) = alice_ratchet.ratchet_encrypt(&data); // Lost message
let (header2, encrypted2, nonce2) = alice_ratchet.ratchet_encrypt(&data); // Successful message
2021-05-13 15:49:11 +00:00
2021-05-14 16:22:18 +00:00
let decrypted2 = bob_ratchet.ratchet_decrypt(&header2, &encrypted2, &nonce2); // Decrypting second message first
let decrypted1 = bob_ratchet.ratchet_decrypt(&header1, &encrypted1, &nonce1); // Decrypting latter message
2021-05-13 15:49:11 +00:00
let comp = decrypted1 == data && decrypted2 == data;
assert!(comp);
```
### Encryption before recieving inital message
```rust
use double_ratchet_2::ratchet::Ratchet;
let sk = [1; 32];
let (mut bob_ratchet, _) = Ratchet::init_bob(sk);
let data = b"Hello World".to_vec();
2021-05-14 16:22:18 +00:00
let (_, _, _) = bob_ratchet.ratchet_encrypt(&data);
2021-05-13 15:49:11 +00:00
```
### Encryption after recieving initial message
However bob can (of course) also encrypt messages. This is possible, after decrypting the first message from alice.
```rust
use double_ratchet_2::ratchet::Ratchet;
let sk = [1; 32];
let (mut bob_ratchet, public_key) = Ratchet::init_bob(sk);
let mut alice_ratchet = Ratchet::init_alice(sk, public_key);
let data = b"Hello World".to_vec();
2021-05-14 16:22:18 +00:00
let (header1, encrypted1, nonce1) = alice_ratchet.ratchet_encrypt(&data);
let _decrypted1 = bob_ratchet.ratchet_decrypt(&header1, &encrypted1, &nonce1);
2021-05-13 15:49:11 +00:00
2021-05-14 16:22:18 +00:00
let (header2, encrypted2, nonce2) = bob_ratchet.ratchet_encrypt(&data);
let decrypted2 = alice_ratchet.ratchet_decrypt(&header2, &encrypted2, &nonce2);
2021-05-13 15:49:11 +00:00
assert_eq!(data, decrypted2);
```
### Constructing and Deconstructing Headers
```rust
let header_bytes: Vec<u8> = header.clone().into();
let header_const = Header::from(header_bytes);
assert_eq!(header, header_const);
```
2021-05-14 16:22:18 +00:00
## Example Ratchet with encrypted headers
```rust
use double_ratchet_2::ratchet::RatchetEncHeader;
let sk = [0; 32];
let shared_hka = [1; 32];
let shared_nhkb = [2; 32];
let (mut bob_ratchet, public_key) = RatchetEncHeader::init_bob(sk, shared_hka, shared_nhkb);
let mut alice_ratchet = RatchetEncHeader::init_alice(sk, public_key, shared_hka, shared_nhkb);
let data = b"Hello World".to_vec();
let (header, encrypted, nonce) = alice_ratchet.ratchet_encrypt(&data);
let decrypted = bob_ratchet.ratchet_decrypt(&header, &encrypted, &nonce);
assert_eq!(data, decrypted)
```
2021-05-13 15:49:11 +00:00
## Features
Currently the crate only supports one feature: ring. If feature is enabled the crate switches
to ring-compat and uses ring as backend for Sha512 Hashing. May result in slightly better performance.
TODO:
- [x] Standard Double Ratchet
2021-05-14 16:22:18 +00:00
- [x] [Double Ratchet with encrypted headers][3]
2021-05-13 15:49:11 +00:00
[1]: https://signal.org/docs/specifications/doubleratchet/
[2]: https://signal.org/docs/specifications/doubleratchet/#recommended-cryptographic-algorithms
[3]: https://signal.org/docs/specifications/doubleratchet/#double-ratchet-with-header-encryption
2021-05-13 18:23:43 +00:00
License: MIT