Find a file
dependabot[bot] d3004b90e7
Bump sha2 from 0.10.2 to 0.10.5
Bumps [sha2](https://github.com/RustCrypto/hashes) from 0.10.2 to 0.10.5.
- [Release notes](https://github.com/RustCrypto/hashes/releases)
- [Commits](https://github.com/RustCrypto/hashes/compare/sha2-v0.10.2...sha2-v0.10.5)

---
updated-dependencies:
- dependency-name: sha2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-09-05 15:54:43 +00:00
.github Improved github actions 2022-03-23 17:32:09 +01:00
benches Changed curve and added bench 2021-05-24 18:04:57 +02:00
src Updated dependencies. Removed useless zeroize. 2022-07-11 16:23:42 +02:00
tests Updated dependencies. Removed useless zeroize. 2022-07-11 16:23:42 +02:00
.gitignore Updated dependencies. Removed useless zeroize. 2022-07-11 16:23:42 +02:00
Cargo.lock Bump sha2 from 0.10.2 to 0.10.5 2022-09-05 15:54:43 +00:00
Cargo.toml Bumped version numbe2 2022-07-11 16:26:24 +02:00
deny.toml Added deny for rust security inspection and added rust-sec workflow 2021-09-05 23:11:38 +02:00
LICENSE Create LICENSE 2021-05-13 17:50:43 +02:00
README.md Added warning for using P-256 2022-03-23 17:00:52 +01:00
README.tpl Added Badges 2021-06-18 12:16:01 +02:00

Crate License Coverage Status Workflow Status Maintenance

double-ratchet-2

Implementation of the double ratchet system/encryption as specified by Signal.

WARNING! This implementation uses P-256 NOT Curve25519 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.

Example Usage:

Standard:

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
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)

With lost message:


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
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);

Encryption before recieving inital message

use double_ratchet_2::ratchet::Ratchet;
let sk = [1; 32];
let ad = b"Associated Data";
let (mut bob_ratchet, _) = Ratchet::init_bob(sk);
let data = b"Hello World".to_vec();

let (_, _, _) = bob_ratchet.ratchet_encrypt(&data, ad);

Encryption after recieving initial message

However bob can (of course) also encrypt messages. This is possible, after decrypting the first message from alice.

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();
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);

Constructing and Deconstructing Headers

let header_bytes: Vec<u8> = header.clone().into();
let header_const = Header::from(header_bytes);
assert_eq!(header, header_const);

Example Ratchet with encrypted headers

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 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)

Export / Import Ratchet with encrypted headers

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.

let (bob_ratchet, public_key) = RatchetEncHeader::init_bob(sk, shared_hka, shared_nhkb);
let ex_ratchet = bob_ratchet.export();
let im_ratchet = RatchetEncHeader::import(&ex_ratchet);
assert_eq!(im_ratchet, bob_ratchet)

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:

Current version: 0.4.0

License: MIT