Skip to content

Commit

Permalink
add documentation around the DhKem trait and the crate itself
Browse files Browse the repository at this point in the history
  • Loading branch information
incertia committed Apr 18, 2024
1 parent b2d8b9f commit 34ef255
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion dhkem/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
//! # Diffie-Hellman (DH) based Key Encapsulation Mechanisms (KEM)
//!
//! This crate provides a KEM interface for DH protocols as specified in
//! [RFC9180](https://datatracker.ietf.org/doc/html/rfc9180#name-dh-based-kem-dhkem)
//! without the shared secret extraction process. In particular, `Encaps(pk)` in the
//! RFC returns the encapsulated key and an extracted shared secret, while our
//! implementation leaves the extraction process up to the user. This type of KEM
//! construction is currently being used in HPKE, as per the RFC, and in the current
//! draft of the [TLS KEM
//! combiner](https://datatracker.ietf.org/doc/html/draft-ietf-tls-hybrid-design-10).

use kem::{Decapsulate, Encapsulate};
use rand_core::CryptoRngCore;

Expand All @@ -11,23 +22,36 @@ pub trait SecretBytes {
fn as_slice(&self) -> &[u8];
}

/// This is a trait that all KEM models should implement, and should probably be
/// promoted to the kem crate itself. It specifies the types of encapsulating and
/// decapsulating keys created by key generation, the shared secret type, and the
/// encapsulated key type
pub trait DhKem {
/// The type that will implement [`Decapsulate`]
type DecapsulatingKey: Decapsulate<Self::EncapsulatedKey, Self::SharedSecret>;

/// The type that will implement [`Encapsulate`]
type EncapsulatingKey: Encapsulate<Self::EncapsulatedKey, Self::SharedSecret>;

/// The type of the encapsulated key
type EncapsulatedKey;

#[cfg(not(test))]
/// The type of the shared secret
type SharedSecret;

#[cfg(test)]
type SharedSecret: SecretBytes;

/// Generates a new (decapsulating key, encapsulating key) keypair for the KEM
/// model
fn random_keypair(
rng: &mut impl CryptoRngCore,
) -> (Self::DecapsulatingKey, Self::EncapsulatingKey);
}

#[cfg(feature = "arithmetic")]
mod arithmetic;
pub mod arithmetic;

#[cfg(feature = "x25519")]
mod x25519_kem;
Expand Down

0 comments on commit 34ef255

Please sign in to comment.