Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kem trait impls when ECDH is supported #1556

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rust-version = "1.73"
base16ct = "0.2"
crypto-bigint = { version = "=0.6.0-pre.12", default-features = false, features = ["rand_core", "hybrid-array", "zeroize"] }
hybrid-array = { version = "0.2.0-rc.8", default-features = false, features = ["zeroize"] }
kem = { version = "=0.3.0-pre.0", optional = true }
rand_core = { version = "0.6.4", default-features = false }
subtle = { version = "2", default-features = false }
zeroize = { version = "1.7", default-features = false }
Expand Down Expand Up @@ -64,6 +65,7 @@ bits = ["arithmetic", "ff/bits", "dep:tap"]
dev = ["arithmetic", "dep:hex-literal", "pem", "pkcs8"]
hash2curve = ["arithmetic", "digest"]
ecdh = ["arithmetic", "digest", "dep:hkdf"]
ecdh-kem = ["ecdh", "kem"]
group = ["dep:group", "ff"]
hazmat = []
jwk = ["dep:base64ct", "dep:serde_json", "alloc", "serde", "zeroize/alloc"]
Expand Down
53 changes: 53 additions & 0 deletions elliptic-curve/src/ecdh_kem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! # ECDH as a KEM
//!
//! This module turns the existing ECDH implementation into a suitable KEM by
//! modeling encapsulation of `g^x` as the generation of another ephemeral secret
//! `y`, computing the encapsulated ciphertext as `g^y`, and computing the shared
//! secret `g^xy`. Decapsulation of `x` is modelled as computing the shared secret
//! `g^xy` from `g^y`.
//!
//! # ECDH-KEM Usage
//!
//! ECDH-KEM allows for an unauthenticated key agreement protocol as follows
//!
//! 1. The client generates an [`EphemeralSecret`] value
//! 2. The client sends the corresponding [`PublicKey`] for their secret
//! 3. The server runs [`encapsulate`](Encapsulate::encapsulate) on the given
//! [`PublicKey`] and holds on to the resulting [`SharedSecret`]
//! 4. The client runs [`decapsulate`](Decapsulate::decapsulate) on the
//! "encapsulated" [`PublicKey`] returned by the server and uses the resulting
//! [`SharedSecret`]

use crate::ecdh::{EphemeralSecret, SharedSecret};
use crate::{CurveArithmetic, PublicKey};
use kem::{Decapsulate, Encapsulate};

impl<C> Decapsulate<PublicKey<C>, SharedSecret<C>> for EphemeralSecret<C>
where
C: CurveArithmetic,
{
type Error = ();

fn decapsulate(&self, encapsulated_key: &PublicKey<C>) -> Result<SharedSecret<C>, Self::Error> {
Ok(self.diffie_hellman(encapsulated_key))
}
}

impl<C> Encapsulate<PublicKey<C>, SharedSecret<C>> for EphemeralSecret<C>
where
C: CurveArithmetic,
{
type Error = ();

fn encapsulate(
&self,
rng: &mut impl rand_core::CryptoRngCore,
) -> Result<(PublicKey<C>, SharedSecret<C>), Self::Error> {
// generate another ephemeral ecdh secret
let secret = EphemeralSecret::<C>::random(rng);
let pk = secret.public_key();
let ss = self.diffie_hellman(&pk);

Ok((pk, ss))
}
}
2 changes: 2 additions & 0 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub mod scalar;
pub mod dev;
#[cfg(feature = "ecdh")]
pub mod ecdh;
#[cfg(feature = "ecdh-kem")]
pub mod ecdh_kem;
#[cfg(feature = "hash2curve")]
pub mod hash2curve;
#[cfg(feature = "arithmetic")]
Expand Down
Loading