diff --git a/dhkem/src/arithmetic.rs b/dhkem/src/arithmetic.rs index ef24cb9..a639024 100644 --- a/dhkem/src/arithmetic.rs +++ b/dhkem/src/arithmetic.rs @@ -1,5 +1,5 @@ -use crate::{Decapsulator, DhKem, EncapsulatedKey, Encapsulator, SharedSecret}; -use elliptic_curve::ecdh::{EphemeralSecret, SharedSecret as EcdhSecret}; +use crate::{Decapsulator, DhKem, Encapsulator}; +use elliptic_curve::ecdh::{EphemeralSecret, SharedSecret}; use elliptic_curve::{CurveArithmetic, PublicKey}; use kem::{Decapsulate, Encapsulate}; use rand_core::CryptoRngCore; @@ -7,8 +7,7 @@ use std::marker::PhantomData; pub struct ArithmeticKem(PhantomData); -impl Encapsulate>, SharedSecret>> - for Encapsulator> +impl Encapsulate, SharedSecret> for Encapsulator> where C: CurveArithmetic, { @@ -17,30 +16,26 @@ where fn encapsulate( &self, rng: &mut impl CryptoRngCore, - ) -> Result<(EncapsulatedKey>, SharedSecret>), Self::Error> { + ) -> Result<(PublicKey, SharedSecret), Self::Error> { // ECDH encapsulation involves creating a new ephemeral key pair and then doing DH let sk = EphemeralSecret::random(rng); let pk = sk.public_key(); let ss = sk.diffie_hellman(&self.0); - Ok((EncapsulatedKey(pk), SharedSecret(ss))) + Ok((pk, ss)) } } -impl Decapsulate>, SharedSecret>> - for Decapsulator> +impl Decapsulate, SharedSecret> for Decapsulator> where C: CurveArithmetic, { type Error = (); - fn decapsulate( - &self, - encapsulated_key: &EncapsulatedKey>, - ) -> Result>, Self::Error> { - let ss = self.0.diffie_hellman(&encapsulated_key.0); + fn decapsulate(&self, encapsulated_key: &PublicKey) -> Result, Self::Error> { + let ss = self.0.diffie_hellman(&encapsulated_key); - Ok(SharedSecret(ss)) + Ok(ss) } } @@ -50,8 +45,8 @@ where { type DecapsulatingKey = Decapsulator>; type EncapsulatingKey = Encapsulator>; - type EncapsulatedKey = EncapsulatedKey>; - type SharedSecret = SharedSecret>; + type EncapsulatedKey = PublicKey; + type SharedSecret = SharedSecret; fn random_keypair( rng: &mut impl CryptoRngCore, @@ -64,11 +59,11 @@ where } #[cfg(test)] -impl crate::SecretBytes for SharedSecret> +impl crate::SecretBytes for SharedSecret where C: CurveArithmetic, { fn as_slice(&self) -> &[u8] { - self.0.raw_secret_bytes().as_slice() + self.raw_secret_bytes().as_slice() } } diff --git a/dhkem/src/lib.rs b/dhkem/src/lib.rs index d5659e4..97b0c54 100644 --- a/dhkem/src/lib.rs +++ b/dhkem/src/lib.rs @@ -5,10 +5,6 @@ use rand_core::CryptoRngCore; pub struct Encapsulator(X); /// Newtype for a piece of data that may be decapsulated pub struct Decapsulator(X); -/// Newtype for a shared secret -pub struct SharedSecret(X); -/// Newtype for an encapsulated key -pub struct EncapsulatedKey(X); #[cfg(test)] pub trait SecretBytes { diff --git a/dhkem/src/x25519_kem.rs b/dhkem/src/x25519_kem.rs index 665c5d6..d4ff410 100644 --- a/dhkem/src/x25519_kem.rs +++ b/dhkem/src/x25519_kem.rs @@ -1,48 +1,41 @@ -use crate::{Decapsulator, DhKem, EncapsulatedKey, Encapsulator, SharedSecret}; +use crate::{Decapsulator, DhKem, Encapsulator}; use kem::{Decapsulate, Encapsulate}; use rand_core::CryptoRngCore; -use x25519::{PublicKey, ReusableSecret, SharedSecret as X25519Secret}; +use x25519::{PublicKey, ReusableSecret, SharedSecret}; pub struct X25519; -impl Encapsulate, SharedSecret> - for Encapsulator -{ +impl Encapsulate for Encapsulator { type Error = (); fn encapsulate( &self, rng: &mut impl CryptoRngCore, - ) -> Result<(EncapsulatedKey, SharedSecret), Self::Error> { + ) -> Result<(PublicKey, SharedSecret), Self::Error> { // ECDH encapsulation involves creating a new ephemeral key pair and then doing DH let sk = ReusableSecret::random_from_rng(rng); let pk = PublicKey::from(&sk); let ss = sk.diffie_hellman(&self.0); - Ok((EncapsulatedKey(pk), SharedSecret(ss))) + Ok((pk, ss)) } } -impl Decapsulate, SharedSecret> - for Decapsulator -{ +impl Decapsulate for Decapsulator { type Error = (); - fn decapsulate( - &self, - encapsulated_key: &EncapsulatedKey, - ) -> Result, Self::Error> { - let ss = self.0.diffie_hellman(&encapsulated_key.0); + fn decapsulate(&self, encapsulated_key: &PublicKey) -> Result { + let ss = self.0.diffie_hellman(&encapsulated_key); - Ok(SharedSecret(ss)) + Ok(ss) } } impl DhKem for X25519 { type DecapsulatingKey = Decapsulator; type EncapsulatingKey = Encapsulator; - type EncapsulatedKey = EncapsulatedKey; - type SharedSecret = SharedSecret; + type EncapsulatedKey = PublicKey; + type SharedSecret = SharedSecret; fn random_keypair( rng: &mut impl CryptoRngCore, @@ -55,8 +48,8 @@ impl DhKem for X25519 { } #[cfg(test)] -impl crate::SecretBytes for SharedSecret { +impl crate::SecretBytes for SharedSecret { fn as_slice(&self) -> &[u8] { - self.0.as_bytes().as_slice() + self.as_bytes().as_slice() } }