diff --git a/src/handshakestate/mod.rs b/src/handshakestate/mod.rs index 3da0abf..f08986f 100644 --- a/src/handshakestate/mod.rs +++ b/src/handshakestate/mod.rs @@ -1,5 +1,4 @@ use arrayvec::ArrayVec; -use rand_core::{CryptoRng, RngCore}; use zeroize::Zeroize; use crate::bytearray::ByteArray; @@ -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; @@ -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, @@ -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, diff --git a/src/handshakestate/nq.rs b/src/handshakestate/nq.rs index abd10af..c3799e2 100644 --- a/src/handshakestate/nq.rs +++ b/src/handshakestate/nq.rs @@ -3,7 +3,6 @@ use core::fmt::Write; use arrayvec::{ArrayString, ArrayVec}; -use rand_core::{CryptoRng, RngCore}; use super::HandshakeInternals; use crate::bytearray::ByteArray; @@ -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 @@ -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)] @@ -34,7 +33,7 @@ where DH: Dh, CIPHER: Cipher, HASH: Hash, - RNG: RngCore + CryptoRng, + RNG: Rng, { /// Initialize new non-post-quantum handshake /// @@ -211,7 +210,7 @@ where DH: Dh, C: Cipher, H: Hash, - RNG: RngCore + CryptoRng, + RNG: Rng, { fn status(&self) -> HandshakeStatus { self.internals.status() @@ -397,7 +396,7 @@ where DH: Dh, C: Cipher, H: Hash, - RNG: RngCore + CryptoRng, + RNG: Rng, { type E = DH::PubKey; type S = DH::PubKey; diff --git a/src/handshakestate/pq.rs b/src/handshakestate/pq.rs index be2aca8..fa625e7 100644 --- a/src/handshakestate/pq.rs +++ b/src/handshakestate/pq.rs @@ -3,7 +3,6 @@ use core::fmt::Write; use arrayvec::{ArrayString, ArrayVec}; -use rand_core::{CryptoRng, RngCore}; use super::HandshakeInternals; use crate::bytearray::ByteArray; @@ -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 @@ -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)] @@ -45,7 +44,7 @@ where SKEM: Kem, CIPHER: Cipher, HASH: Hash, - RNG: RngCore + CryptoRng, + RNG: Rng, { /// Initialize new post-quantum handshake /// @@ -190,7 +189,7 @@ where SKEM: Kem, C: Cipher, H: Hash, - RNG: RngCore + CryptoRng, + RNG: Rng, { fn status(&self) -> HandshakeStatus { self.internals.status() @@ -434,7 +433,7 @@ where SKEM: Kem, C: Cipher, H: Hash, - RNG: RngCore + CryptoRng, + RNG: Rng, { type E = EKEM::PubKey; type S = SKEM::PubKey; diff --git a/src/traits.rs b/src/traits.rs index 0b859ff..011a306 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -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; @@ -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 Rng for T {} + /// Common trait for all Diffie-Hellman algorithms pub trait Dh: CryptoComponent { /// Private key type @@ -29,9 +39,7 @@ pub trait Dh: CryptoComponent { type Output: ByteArray; /// Generate a keypair - fn genkey( - rng: &mut R, - ) -> DhResult>; + fn genkey(rng: &mut R) -> DhResult>; /// Extract public key from given private key fn pubkey(k: &Self::PrivateKey) -> Self::PubKey; @@ -52,15 +60,10 @@ pub trait Kem: CryptoComponent { type Ss: ByteArray; /// Generate a keypair - fn genkey( - rng: &mut R, - ) -> KemResult>; + fn genkey(rng: &mut R) -> KemResult>; /// Encapsulate a public key and return the ciphertext and shared secret - fn encapsulate( - pk: &[u8], - rng: &mut R, - ) -> KemResult<(Self::Ct, Self::Ss)>; + fn encapsulate(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;