Skip to content

Commit

Permalink
Added a dedicated marker trait for supported RNG sources
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlepisto committed Dec 17, 2024
1 parent e023cf5 commit 22b0bf2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
7 changes: 3 additions & 4 deletions src/handshakestate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use arrayvec::ArrayVec;
use rand_core::{CryptoRng, RngCore};
use zeroize::Zeroize;

use crate::bytearray::ByteArray;
Expand All @@ -8,7 +7,7 @@ use crate::constants::{MAX_PSKS, PSK_LEN};
use crate::error::{HandshakeError, HandshakeResult};
use crate::handshakepattern::{HandshakePattern, Token};
use crate::symmetricstate::SymmetricState;
use crate::traits::{Cipher, Hash};
use crate::traits::{Cipher, Hash, Rng};
use crate::KeyPair;

pub mod dual_layer;
Expand All @@ -32,7 +31,7 @@ pub(crate) struct HandshakeInternals<'a, C, H, RNG, K, P, EK, EP>
where
C: Cipher,
H: Hash,
RNG: RngCore + CryptoRng,
RNG: Rng,
K: ByteArray,
P: ByteArray,
EK: ByteArray,
Expand All @@ -56,7 +55,7 @@ impl<'a, C, H, RNG, K, P, EK, EP> HandshakeInternals<'a, C, H, RNG, K, P, EK, EP
where
C: Cipher,
H: Hash,
RNG: RngCore + CryptoRng,
RNG: Rng,
K: ByteArray,
P: ByteArray,
EK: ByteArray,
Expand Down
11 changes: 5 additions & 6 deletions src/handshakestate/nq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use core::fmt::Write;

use arrayvec::{ArrayString, ArrayVec};
use rand_core::{CryptoRng, RngCore};

use super::HandshakeInternals;
use crate::bytearray::ByteArray;
Expand All @@ -12,7 +11,7 @@ use crate::error::{HandshakeError, HandshakeResult};
use crate::handshakepattern::{HandshakePattern, Token};
use crate::handshakestate::HandshakeStatus;
use crate::symmetricstate::SymmetricState;
use crate::traits::{Cipher, Dh, Handshaker, HandshakerInternal, Hash};
use crate::traits::{Cipher, Dh, Handshaker, HandshakerInternal, Hash, Rng};
use crate::KeyPair;

/// Non-post-quantum Noise handshake
Expand All @@ -21,7 +20,7 @@ where
DH: Dh,
C: Cipher,
H: Hash,
RNG: RngCore + CryptoRng,
RNG: Rng,
{
// Internal, we can live with this
#[allow(clippy::type_complexity)]
Expand All @@ -34,7 +33,7 @@ where
DH: Dh,
CIPHER: Cipher,
HASH: Hash,
RNG: RngCore + CryptoRng,
RNG: Rng,
{
/// Initialize new non-post-quantum handshake
///
Expand Down Expand Up @@ -211,7 +210,7 @@ where
DH: Dh,
C: Cipher,
H: Hash,
RNG: RngCore + CryptoRng,
RNG: Rng,
{
fn status(&self) -> HandshakeStatus {
self.internals.status()
Expand Down Expand Up @@ -397,7 +396,7 @@ where
DH: Dh,
C: Cipher,
H: Hash,
RNG: RngCore + CryptoRng,
RNG: Rng,
{
type E = DH::PubKey;
type S = DH::PubKey;
Expand Down
11 changes: 5 additions & 6 deletions src/handshakestate/pq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use core::fmt::Write;

use arrayvec::{ArrayString, ArrayVec};
use rand_core::{CryptoRng, RngCore};

use super::HandshakeInternals;
use crate::bytearray::ByteArray;
Expand All @@ -13,7 +12,7 @@ use crate::error::{HandshakeError, HandshakeResult};
use crate::handshakepattern::{HandshakePattern, Token};
use crate::handshakestate::HandshakeStatus;
use crate::symmetricstate::SymmetricState;
use crate::traits::{Cipher, Handshaker, HandshakerInternal, Hash, Kem};
use crate::traits::{Cipher, Handshaker, HandshakerInternal, Hash, Kem, Rng};
use crate::KeyPair;

/// Post-quantum Noise handshake
Expand All @@ -23,7 +22,7 @@ where
SKEM: Kem,
C: Cipher,
H: Hash,
RNG: RngCore + CryptoRng,
RNG: Rng,
{
// Internal, we can live with this
#[allow(clippy::type_complexity)]
Expand All @@ -45,7 +44,7 @@ where
SKEM: Kem,
CIPHER: Cipher,
HASH: Hash,
RNG: RngCore + CryptoRng,
RNG: Rng,
{
/// Initialize new post-quantum handshake
///
Expand Down Expand Up @@ -190,7 +189,7 @@ where
SKEM: Kem,
C: Cipher,
H: Hash,
RNG: RngCore + CryptoRng,
RNG: Rng,
{
fn status(&self) -> HandshakeStatus {
self.internals.status()
Expand Down Expand Up @@ -434,7 +433,7 @@ where
SKEM: Kem,
C: Cipher,
H: Hash,
RNG: RngCore + CryptoRng,
RNG: Rng,
{
type E = EKEM::PubKey;
type S = SKEM::PubKey;
Expand Down
25 changes: 14 additions & 11 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Common traits used throughout the crate
use arrayvec::ArrayString;
use rand_core::{CryptoRng, RngCore};
pub use rand_core::{CryptoRng, RngCore};
use zeroize::Zeroize;

use crate::bytearray::ByteArray;
Expand All @@ -19,6 +19,16 @@ pub trait CryptoComponent {
fn name() -> &'static str;
}

/// Common trait for compatible RNG sources
///
/// Automatically implemented for all types that implement:
/// * [`RngCore`]
/// * [`CryptoRng`]
pub trait Rng: RngCore + CryptoRng {}

/// Automatic implementation for all supported types
impl<T: RngCore + CryptoRng> Rng for T {}

/// Common trait for all Diffie-Hellman algorithms
pub trait Dh: CryptoComponent {
/// Private key type
Expand All @@ -29,9 +39,7 @@ pub trait Dh: CryptoComponent {
type Output: ByteArray;

/// Generate a keypair
fn genkey<R: RngCore + CryptoRng>(
rng: &mut R,
) -> DhResult<KeyPair<Self::PubKey, Self::PrivateKey>>;
fn genkey<R: Rng>(rng: &mut R) -> DhResult<KeyPair<Self::PubKey, Self::PrivateKey>>;

/// Extract public key from given private key
fn pubkey(k: &Self::PrivateKey) -> Self::PubKey;
Expand All @@ -52,15 +60,10 @@ pub trait Kem: CryptoComponent {
type Ss: ByteArray;

/// Generate a keypair
fn genkey<R: RngCore + CryptoRng>(
rng: &mut R,
) -> KemResult<KeyPair<Self::PubKey, Self::SecretKey>>;
fn genkey<R: Rng>(rng: &mut R) -> KemResult<KeyPair<Self::PubKey, Self::SecretKey>>;

/// Encapsulate a public key and return the ciphertext and shared secret
fn encapsulate<R: RngCore + CryptoRng>(
pk: &[u8],
rng: &mut R,
) -> KemResult<(Self::Ct, Self::Ss)>;
fn encapsulate<R: Rng>(pk: &[u8], rng: &mut R) -> KemResult<(Self::Ct, Self::Ss)>;

/// Decapsulate ciphertext with secret key and return the shared secret
fn decapsulate(ct: &[u8], sk: &[u8]) -> KemResult<Self::Ss>;
Expand Down

0 comments on commit 22b0bf2

Please sign in to comment.