Skip to content

Commit

Permalink
kem: Made rng input a mut ref; made Error assoc type impl Debug
Browse files Browse the repository at this point in the history
  • Loading branch information
rozbb committed Mar 28, 2024
1 parent dd3e525 commit d24de10
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions kem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@
#![forbid(unsafe_code)]
#![warn(missing_docs, unused_qualifications, missing_debug_implementations)]

use core::fmt::Debug;
use rand_core::CryptoRngCore;

/// A value that can be encapsulated to. Often, this will just be a public key. However, it can
/// also be a bundle of public keys, or it can include a sender's private key for authenticated
/// encapsulation.
pub trait Encapsulate<EK, SS> {
/// Encapsulation error
type Error;
type Error: Debug;

/// Encapsulates a fresh shared secret
fn encapsulate(&self, rng: impl rand_core::CryptoRngCore) -> Result<(EK, SS), Self::Error>;
fn encapsulate(&self, rng: &mut impl CryptoRngCore) -> Result<(EK, SS), Self::Error>;
}

/// A value that can be used to decapsulate an encapsulated key. Often, this will just be a secret
/// key. But, as with [`Encapsulate`], it can be a bundle of secret keys, or it can include a
/// sender's private key for authenticated encapsulation.
pub trait Decapsulate<EK, SS> {
/// Decapsulation error
type Error;
type Error: Debug;

/// Decapsulates the given encapsulated key
fn decapsulate(&self, encapsulated_key: &EK) -> Result<SS, Self::Error>;
Expand Down
2 changes: 1 addition & 1 deletion kem/tests/hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Encapsulate<EncappedKey, SharedSecret> for PublicKey {

fn encapsulate(
&self,
mut csprng: impl CryptoRngCore,
mut csprng: &mut impl CryptoRngCore,
) -> Result<(EncappedKey, SharedSecret), HpkeError> {
<X25519HkdfSha256 as KemTrait>::encap(&self.0, None, &mut csprng).map(|(ek, ss)| (ss, ek))
}
Expand Down
2 changes: 1 addition & 1 deletion kem/tests/saber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Encapsulate<SaberEncappedKey, SaberSharedSecret> for SaberPublicKey {

fn encapsulate(
&self,
_: impl CryptoRngCore,
_: &mut impl CryptoRngCore,
) -> Result<(SaberEncappedKey, SaberSharedSecret), ()> {
let (ss, ek) = encapsulate(&self.0);
Ok((ek, ss))
Expand Down
2 changes: 1 addition & 1 deletion kem/tests/x3dh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Encapsulate<EphemeralKey, SharedSecret> for EncapContext {

fn encapsulate(
&self,
_: impl CryptoRngCore,
_: &mut impl CryptoRngCore,
) -> Result<(EphemeralKey, SharedSecret), Self::Error> {
// Make a new ephemeral key. This will be the encapped key
let ek = EphemeralKey::default();
Expand Down

0 comments on commit d24de10

Please sign in to comment.