Crates.io | double-ratchet-2 |
lib.rs | double-ratchet-2 |
version | 0.4.0-pre.2 |
source | src |
created_at | 2021-05-13 15:56:33.361842 |
updated_at | 2022-07-20 10:11:54.909701 |
description | Implementation of Double Ratchet as specified by Signal. |
homepage | https://github.com/Dione-Software/double-ratchet-2 |
repository | https://github.com/Dione-Software/double-ratchet-2 |
max_upload_size | |
id | 396980 |
size | 72,859 |
Implementation of the double ratchet system/encryption as specified by Signal.
The implementation follows the cryptographic recommendations provided by Signal. The AEAD Algorithm uses a constant Nonce. This might be changed in the future.
use double_ratchet_2::ratchet::Ratchet;
use x25519_dalek::StaticSecret;
let sk = [1; 32]; // Initial Key created by a symmetric key agreement protocol
let (mut bob_ratchet, public_key) = Ratchet::<StaticSecret>::init_bob(sk); // Creating Bobs Ratchet (returns Bobs PublicKey)
let mut alice_ratchet = Ratchet::<StaticSecret>::init_alice(sk, public_key); // Creating Alice Ratchet with Bobs PublicKey
let data = b"Hello World".to_vec(); // Data to be encrypted
let ad = b"Associated Data"; // Associated Data
let (header, encrypted, nonce) = alice_ratchet.ratchet_encrypt(&data, ad); // Encrypting message with Alice Ratchet (Alice always needs to send the first message)
let decrypted = bob_ratchet.ratchet_decrypt(&header, &encrypted, &nonce, ad); // Decrypt message with Bobs Ratchet
assert_eq!(data, decrypted)
use x25519_dalek::StaticSecret;
let sk = [1; 32]; // Initial Key created by a symmetric key agreement protocol
let (mut bob_ratchet, public_key) = Ratchet::<StaticSecret>::init_bob(sk); // Creating Bobs Ratchet (returns Bobs PublicKey)
let mut alice_ratchet = Ratchet::<StaticSecret>::init_alice(sk, public_key); // Creating Alice Ratchet with Bobs PublicKey
let data = b"Hello World".to_vec(); // Data to be encrypted
let ad = b"Associated Data"; // Associated Data
let (header1, encrypted1, nonce1) = alice_ratchet.ratchet_encrypt(&data, ad); // Lost message
let (header2, encrypted2, nonce2) = alice_ratchet.ratchet_encrypt(&data, ad); // Successful message
let decrypted2 = bob_ratchet.ratchet_decrypt(&header2, &encrypted2, &nonce2, ad); // Decrypting second message first
let decrypted1 = bob_ratchet.ratchet_decrypt(&header1, &encrypted1, &nonce1, ad); // Decrypting latter message
let comp = decrypted1 == data && decrypted2 == data;
assert!(comp);
use double_ratchet_2::ratchet::Ratchet;
use x25519_dalek::StaticSecret;
let sk = [1; 32];
let ad = b"Associated Data";
let (mut bob_ratchet, _) = Ratchet::<StaticSecret>::init_bob(sk);
let data = b"Hello World".to_vec();
let (_, _, _) = bob_ratchet.ratchet_encrypt(&data, ad);
However bob can (of course) also encrypt messages. This is possible, after decrypting the first message from alice.
use double_ratchet_2::ratchet::Ratchet;
use x25519_dalek::StaticSecret;
let sk = [1; 32];
let (mut bob_ratchet, public_key) = Ratchet::<StaticSecret>::init_bob(sk);
let mut alice_ratchet = Ratchet::<StaticSecret>::init_alice(sk, public_key);
let data = b"Hello World".to_vec();
let ad = b"Associated Data";
let (header1, encrypted1, nonce1) = alice_ratchet.ratchet_encrypt(&data, ad);
let _decrypted1 = bob_ratchet.ratchet_decrypt(&header1, &encrypted1, &nonce1, ad);
let (header2, encrypted2, nonce2) = bob_ratchet.ratchet_encrypt(&data, ad);
let decrypted2 = alice_ratchet.ratchet_decrypt(&header2, &encrypted2, &nonce2, ad);
assert_eq!(data, decrypted2);
let header_bytes: Vec<u8> = header.concat(b"");
let header_const = Header::<x25519_dalek::PublicKey>::from(header_bytes.as_slice());
assert_eq!(header, header_const);
use double_ratchet_2::ratchet::RatchetEncHeader;
use x25519_dalek::StaticSecret;
let sk = [0; 32];
let shared_hka = [1; 32];
let shared_nhkb = [2; 32];
let (mut bob_ratchet, public_key) = RatchetEncHeader::<StaticSecret>::init_bob(sk, shared_hka, shared_nhkb);
let mut alice_ratchet = RatchetEncHeader::<StaticSecret>::init_alice(sk, public_key, shared_hka, shared_nhkb);
let data = b"Hello World".to_vec();
let ad = b"Associated Data";
let (header, encrypted, nonce) = alice_ratchet.ratchet_encrypt(&data, ad);
let decrypted = bob_ratchet.ratchet_decrypt(&header, &encrypted, &nonce, ad);
assert_eq!(data, decrypted)
This ratchet implements import and export functionality. This works over a bincode backend and maybe useful for saving Ratchets to and loading from a file.
use x25519_dalek::StaticSecret;
let (bob_ratchet, public_key) = RatchetEncHeader::<StaticSecret>::init_bob(sk, shared_hka, shared_nhkb);
let ex_ratchet = bob_ratchet.export();
let im_ratchet = RatchetEncHeader::<StaticSecret>::import(&ex_ratchet).unwrap();
assert_eq!(im_ratchet.export(), bob_ratchet.export())
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:
Current version: 0.4.0-pre.1
License: MIT