Skip to content

Commit

Permalink
Refactor underlying primitives into separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyer committed Dec 23, 2021
1 parent ef3e892 commit f338f0b
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 1,030 deletions.
10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "xxxdh"
license = "MIT"
version = "0.4.0"
version = "0.5.0"
edition = "2021"
authors = ["Oleksandr Yermakov <olexander.yermakov@gmail.com>"]
categories = ["cryptography"]
Expand All @@ -12,11 +12,9 @@ description = "Pure Rust X3DH key exchange protocol implementation"

[dependencies]
aes-gcm = {version = "0.9.4", optional = true }
curve25519-dalek-ng = { version = "4.1.1", optional = true }
hkdf = "0.12.0"
cryptimitives = "0.5.0"
cryptraits = "0.2.0"
rand_core = "0.6.3"
schnorrkel = { version = "0.10.2", optional = true }
sha2 = "0.10.0"
thiserror = "1.0.30"
zeroize = "1.4.3"

Expand All @@ -25,4 +23,4 @@ default = ["x25519-ristretto", "hkdf-sha256", "aead-aes-gcm"]
aead-aes-gcm = ["aes-gcm"]
hkdf-sha256 = []
hkdf-sha512 = []
x25519-ristretto = ["curve25519-dalek-ng", "schnorrkel"]
x25519-ristretto = []
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,44 @@ Implementation is close to the [Signal Spec](https://signal.org/docs/specificati
## Usage

```rust
//! Basic example.

use cryptimitives::{aead, kdf::sha256, key::x25519_ristretto};
use cryptraits::{convert::ToVec, key::KeyPair, signature::Sign};
use rand_core::OsRng;
use xxxdh::{
aes_gcm, inmem, sha256, x25519_ristretto, IdentityKeyStorage, OnetimeKeyStorage, PreKeyStorage,
Protocol, Sign, SignatureStorage, ToVec,
inmem, IdentityKeyStorage, OnetimeKeyStorage, PreKeyStorage, Protocol, SignatureStorage,
};

fn main() {
// Instantiate Alice protocol.

let alice_identity = x25519_ristretto::IdentityKeyPair::generate_with(OsRng);
let alice_prekey = x25519_ristretto::PreKeyPair::generate_with(OsRng);
let alice_identity = x25519_ristretto::KeyPair::generate_with(OsRng);
let alice_prekey = x25519_ristretto::KeyPair::generate_with(OsRng);
let alice_signature = alice_identity.sign(&alice_prekey.to_public().to_vec());
let mut alice_protocol = Protocol::<
x25519_ristretto::IdentitySecretKey,
x25519_ristretto::SecretKey,
x25519_ristretto::EphemeralSecretKey,
x25519_ristretto::Signature,
inmem::Storage<_, _>,
sha256::Kdf,
aes_gcm::Aead,
aead::aes_gcm::Aes256Gcm,
>::new(alice_identity, alice_prekey, alice_signature, None);

// Instantiate Bob protocol.

let onetime_keypair = x25519_ristretto::OnetimeKeyPair::generate_with(OsRng);
let onetime_keypair = x25519_ristretto::KeyPair::generate_with(OsRng);

let bob_identity = x25519_ristretto::IdentityKeyPair::generate_with(OsRng);
let bob_prekey = x25519_ristretto::IdentityKeyPair::generate_with(OsRng);
let bob_identity = x25519_ristretto::KeyPair::generate_with(OsRng);
let bob_prekey = x25519_ristretto::KeyPair::generate_with(OsRng);
let bob_signature = bob_identity.sign(&bob_prekey.to_public().to_vec());
let mut bob_protocol = Protocol::<
x25519_ristretto::IdentitySecretKey,
x25519_ristretto::SecretKey,
x25519_ristretto::EphemeralSecretKey,
x25519_ristretto::Signature,
inmem::Storage<_, _>,
sha256::Kdf,
aes_gcm::Aead,
aead::aes_gcm::Aes256Gcm,
>::new(
bob_identity,
bob_prekey,
Expand Down Expand Up @@ -82,4 +85,5 @@ fn main() {
println!("Alice shared secret: {:?}", alice_sk);
println!("Bob shared secret: {:?}", bob_sk);
}

```
23 changes: 12 additions & 11 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
//! Basic example.
use cryptimitives::{aead, kdf::sha256, key::x25519_ristretto};
use cryptraits::{convert::ToVec, key::KeyPair, signature::Sign};
use rand_core::OsRng;
use xxxdh::{
aes_gcm, inmem, sha256, x25519_ristretto, IdentityKeyStorage, OnetimeKeyStorage, PreKeyStorage,
Protocol, Sign, SignatureStorage, ToVec,
inmem, IdentityKeyStorage, OnetimeKeyStorage, PreKeyStorage, Protocol, SignatureStorage,
};

fn main() {
// Instantiate Alice protocol.

let alice_identity = x25519_ristretto::IdentityKeyPair::generate_with(OsRng);
let alice_prekey = x25519_ristretto::PreKeyPair::generate_with(OsRng);
let alice_identity = x25519_ristretto::KeyPair::generate_with(OsRng);
let alice_prekey = x25519_ristretto::KeyPair::generate_with(OsRng);
let alice_signature = alice_identity.sign(&alice_prekey.to_public().to_vec());
let mut alice_protocol = Protocol::<
x25519_ristretto::IdentitySecretKey,
x25519_ristretto::SecretKey,
x25519_ristretto::EphemeralSecretKey,
x25519_ristretto::Signature,
inmem::Storage<_, _>,
sha256::Kdf,
aes_gcm::Aead,
aead::aes_gcm::Aes256Gcm,
>::new(alice_identity, alice_prekey, alice_signature, None);

// Instantiate Bob protocol.

let onetime_keypair = x25519_ristretto::OnetimeKeyPair::generate_with(OsRng);
let onetime_keypair = x25519_ristretto::KeyPair::generate_with(OsRng);

let bob_identity = x25519_ristretto::IdentityKeyPair::generate_with(OsRng);
let bob_prekey = x25519_ristretto::IdentityKeyPair::generate_with(OsRng);
let bob_identity = x25519_ristretto::KeyPair::generate_with(OsRng);
let bob_prekey = x25519_ristretto::KeyPair::generate_with(OsRng);
let bob_signature = bob_identity.sign(&bob_prekey.to_public().to_vec());
let mut bob_protocol = Protocol::<
x25519_ristretto::IdentitySecretKey,
x25519_ristretto::SecretKey,
x25519_ristretto::EphemeralSecretKey,
x25519_ristretto::Signature,
inmem::Storage<_, _>,
sha256::Kdf,
aes_gcm::Aead,
aead::aes_gcm::Aes256Gcm,
>::new(
bob_identity,
bob_prekey,
Expand Down
26 changes: 0 additions & 26 deletions src/aead/aes_gcm.rs

This file was deleted.

13 changes: 0 additions & 13 deletions src/aead/mod.rs

This file was deleted.

77 changes: 25 additions & 52 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
//! Crate custom errors.
use cryptimitives::errors::{AeadError, KdfError, KeyPairError, SignatureError};
use thiserror::Error;

/// Errors which may occur while processing keypairs.
///
/// This error may arise due to:
///
/// * Being given bytes with a length different to what was expected.
#[derive(Debug, Error)]
pub enum KeypairError {
#[error("being given bytes with a length different to what was expected")]
BytesLengthError,

#[error("underlying error: {0}")]
UnderlyingError(String),
}

/// Errors which may occur while processing signatures.
#[derive(Debug, Error, PartialEq)]
pub enum SignatureError {
/// A signature verification equation failed.
#[error("signature verification equation failed")]
EquationFalse,
}

/// X3DH protocol errors.
#[derive(Debug, Error)]
pub enum XxxDhError {
Expand All @@ -36,38 +15,26 @@ pub enum XxxDhError {
UnknownPrekey,

/// Error occurred in the underlying KDF function.
#[error(transparent)]
KdfError(#[from] KdfError),
#[error("{0:?}")]
KdfError(KdfError),

/// Error occurred in the underlying keypair.
#[error(transparent)]
KeypairError(#[from] KeypairError),
#[error("{0:?}")]
KeypairError(KeyPairError),

/// Error occurred in the underlying AEAD cipher.
#[error(transparent)]
AeadError(#[from] AeadError),
#[error("{0:?}")]
AeadError(AeadError),

/// Error occured in the underlying signature.
#[error(transparent)]
SignatureError(#[from] SignatureError),
#[error("{0:?}")]
SignatureError(SignatureError),

/// Storge related errors.
#[error(transparent)]
StorageError(#[from] StorageError),
}

/// Error which may occur while deriving keys.
#[derive(Debug, Error)]
pub enum KdfError {
#[error("invalid length")]
InvalidLength,
}

/// AEAD algorithm error.
#[derive(Debug, Error)]
#[error("AEAD error")]
pub struct AeadError;

/// Storage related errors
#[allow(dead_code)]
#[derive(Debug, Error)]
Expand All @@ -77,20 +44,26 @@ pub enum StorageError {
UnknownError,
}

/// `Result` specialized to this crate for convenience. Used for keypair related results.
pub type KeyResult<T> = Result<T, KeypairError>;
impl From<KdfError> for XxxDhError {
fn from(e: KdfError) -> Self {
Self::KdfError(e)
}
}

impl From<AeadError> for XxxDhError {
fn from(e: AeadError) -> Self {
Self::AeadError(e)
}
}

/// `Result` specialized to this crate for convenience. Used for signture related results.
pub type SignatureResult<T> = Result<T, SignatureError>;
impl From<SignatureError> for XxxDhError {
fn from(e: SignatureError) -> Self {
Self::SignatureError(e)
}
}

/// `Result` specialized to this crate for convenience. Used for protocol related results.
pub type XxxDhResult<T> = Result<T, XxxDhError>;

/// `Result` specialized to this crate for convenience. Used for kdf related results.
pub type KdfResult<T> = Result<T, KdfError>;

/// `Result` specialized to this crate for convenience. Used for AEAD related results.
pub type AeadResult<T> = Result<T, AeadError>;

/// `Result` specialized to this crate for convenience. Used for storage related results.
pub type StorageResult<T> = Result<T, StorageError>;
13 changes: 0 additions & 13 deletions src/kdf/mod.rs

This file was deleted.

16 changes: 0 additions & 16 deletions src/kdf/sha256.rs

This file was deleted.

16 changes: 0 additions & 16 deletions src/kdf/sha512.rs

This file was deleted.

10 changes: 0 additions & 10 deletions src/key_exchange.rs

This file was deleted.

Loading

0 comments on commit f338f0b

Please sign in to comment.