Skip to content
This repository was archived by the owner on May 4, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ The following primitives are covered:
- Cryptographically secure, deterministic RNG

- Asymmetric Signer
- Normal signer
- Normal signer

- Diffie-Hellman

- Key Encapsulation Mechanism
25 changes: 25 additions & 0 deletions src/dh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::rng::{SecKeyGen, PubKeyGen};
use std::error::Error;

#[derive(Debug, Eq, PartialEq, Clone)]
/// Information about a Diffie-Hellman group
pub struct DiffieHellmanInfo {
/// The name
pub name: &'static str,

/// The secret key length
pub sec_key_len: usize,
/// The public key length
pub pub_key_len: usize,
/// The shared secret length
pub ss_len: usize,
}

/// A Diffie-Hellman interface
pub trait DiffieHellman: SecKeyGen + PubKeyGen {
fn info(&self) -> DiffieHellmanInfo;

/// Combine `pub_key` with a `sec_key` and store the shared secret to `buf`
fn dh(&self, buf: &mut [u8], pub_key: &[u8], sec_key: &[u8])
-> Result<(), Box<dyn Error + 'static>>;
}
29 changes: 29 additions & 0 deletions src/kem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::rng::{SecKeyGen, PubKeyGen};

/// Information about a Key Encapsulation Mechanism implementation
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct KEMInfo {
pub name: 'static str,

/// Length of public key
pub pub_key_len: usize,
/// Length of private key
pub sec_key_len: usize,
/// Length of the encapsulated secret
pub ctext_len: usize,
/// Length of the shared secred
pub ss_len: usize,
}

pub trait KEM: SecKeyGen + PubKeyGen {
/// Returns information about the Key Encapsulation Mechanism
fn info(&self) -> KEMInfo;

/// Encapsulates to `pub_key`, stores ciphertext to `ctext` and shared secret to `ss`
fn encapsulate(&self, ctext: &mut[u8], ss: &mut[u8], pub_key: &[u8], rng: &mut SecureRng)
-> Result<(), Box<dyn Error + 'static>>;

/// Decrypts the encapsulated `ctext` into `ss` using `sec_key`
fn decapsulate(&self, ss: &mut[u8], ctext: &[u8], sec_key: &[u8])
-> Result<(), Box<dyn Error + 'static>>;
}
4 changes: 2 additions & 2 deletions src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ pub trait DeterministicRng: SecureRng {
/// A algorithm specific key generator to generate a (secret) key
pub trait SecKeyGen {
/// Generates a new key into `buf` using `rng` and returns the length of the secret key
fn new_sec_key(&self, buf: &mut[u8], rng: &mut SecureRng)
fn new_sec_key(&self, buf: &mut[u8], rng: &mut dyn SecureRng)
-> Result<usize, Box<dyn Error + 'static>>;
}

/// A algorithm specific trait to compute a public key from a secret key
pub trait PubKeyGen {
/// Computes the public key from `sec_key` into `buf` and returns the length of the public key
fn get_pub_key(&self, buf: &mut[u8], sec_key: &[u8]) -> Result<usize, Box<dyn Error + 'static>>;
}
}