Skip to content

Commit 22b0bf2

Browse files
author
Joni
committed
Added a dedicated marker trait for supported RNG sources
1 parent e023cf5 commit 22b0bf2

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

src/handshakestate/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use arrayvec::ArrayVec;
2-
use rand_core::{CryptoRng, RngCore};
32
use zeroize::Zeroize;
43

54
use crate::bytearray::ByteArray;
@@ -8,7 +7,7 @@ use crate::constants::{MAX_PSKS, PSK_LEN};
87
use crate::error::{HandshakeError, HandshakeResult};
98
use crate::handshakepattern::{HandshakePattern, Token};
109
use crate::symmetricstate::SymmetricState;
11-
use crate::traits::{Cipher, Hash};
10+
use crate::traits::{Cipher, Hash, Rng};
1211
use crate::KeyPair;
1312

1413
pub mod dual_layer;
@@ -32,7 +31,7 @@ pub(crate) struct HandshakeInternals<'a, C, H, RNG, K, P, EK, EP>
3231
where
3332
C: Cipher,
3433
H: Hash,
35-
RNG: RngCore + CryptoRng,
34+
RNG: Rng,
3635
K: ByteArray,
3736
P: ByteArray,
3837
EK: ByteArray,
@@ -56,7 +55,7 @@ impl<'a, C, H, RNG, K, P, EK, EP> HandshakeInternals<'a, C, H, RNG, K, P, EK, EP
5655
where
5756
C: Cipher,
5857
H: Hash,
59-
RNG: RngCore + CryptoRng,
58+
RNG: Rng,
6059
K: ByteArray,
6160
P: ByteArray,
6261
EK: ByteArray,

src/handshakestate/nq.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use core::fmt::Write;
44

55
use arrayvec::{ArrayString, ArrayVec};
6-
use rand_core::{CryptoRng, RngCore};
76

87
use super::HandshakeInternals;
98
use crate::bytearray::ByteArray;
@@ -12,7 +11,7 @@ use crate::error::{HandshakeError, HandshakeResult};
1211
use crate::handshakepattern::{HandshakePattern, Token};
1312
use crate::handshakestate::HandshakeStatus;
1413
use crate::symmetricstate::SymmetricState;
15-
use crate::traits::{Cipher, Dh, Handshaker, HandshakerInternal, Hash};
14+
use crate::traits::{Cipher, Dh, Handshaker, HandshakerInternal, Hash, Rng};
1615
use crate::KeyPair;
1716

1817
/// Non-post-quantum Noise handshake
@@ -21,7 +20,7 @@ where
2120
DH: Dh,
2221
C: Cipher,
2322
H: Hash,
24-
RNG: RngCore + CryptoRng,
23+
RNG: Rng,
2524
{
2625
// Internal, we can live with this
2726
#[allow(clippy::type_complexity)]
@@ -34,7 +33,7 @@ where
3433
DH: Dh,
3534
CIPHER: Cipher,
3635
HASH: Hash,
37-
RNG: RngCore + CryptoRng,
36+
RNG: Rng,
3837
{
3938
/// Initialize new non-post-quantum handshake
4039
///
@@ -211,7 +210,7 @@ where
211210
DH: Dh,
212211
C: Cipher,
213212
H: Hash,
214-
RNG: RngCore + CryptoRng,
213+
RNG: Rng,
215214
{
216215
fn status(&self) -> HandshakeStatus {
217216
self.internals.status()
@@ -397,7 +396,7 @@ where
397396
DH: Dh,
398397
C: Cipher,
399398
H: Hash,
400-
RNG: RngCore + CryptoRng,
399+
RNG: Rng,
401400
{
402401
type E = DH::PubKey;
403402
type S = DH::PubKey;

src/handshakestate/pq.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use core::fmt::Write;
44

55
use arrayvec::{ArrayString, ArrayVec};
6-
use rand_core::{CryptoRng, RngCore};
76

87
use super::HandshakeInternals;
98
use crate::bytearray::ByteArray;
@@ -13,7 +12,7 @@ use crate::error::{HandshakeError, HandshakeResult};
1312
use crate::handshakepattern::{HandshakePattern, Token};
1413
use crate::handshakestate::HandshakeStatus;
1514
use crate::symmetricstate::SymmetricState;
16-
use crate::traits::{Cipher, Handshaker, HandshakerInternal, Hash, Kem};
15+
use crate::traits::{Cipher, Handshaker, HandshakerInternal, Hash, Kem, Rng};
1716
use crate::KeyPair;
1817

1918
/// Post-quantum Noise handshake
@@ -23,7 +22,7 @@ where
2322
SKEM: Kem,
2423
C: Cipher,
2524
H: Hash,
26-
RNG: RngCore + CryptoRng,
25+
RNG: Rng,
2726
{
2827
// Internal, we can live with this
2928
#[allow(clippy::type_complexity)]
@@ -45,7 +44,7 @@ where
4544
SKEM: Kem,
4645
CIPHER: Cipher,
4746
HASH: Hash,
48-
RNG: RngCore + CryptoRng,
47+
RNG: Rng,
4948
{
5049
/// Initialize new post-quantum handshake
5150
///
@@ -190,7 +189,7 @@ where
190189
SKEM: Kem,
191190
C: Cipher,
192191
H: Hash,
193-
RNG: RngCore + CryptoRng,
192+
RNG: Rng,
194193
{
195194
fn status(&self) -> HandshakeStatus {
196195
self.internals.status()
@@ -434,7 +433,7 @@ where
434433
SKEM: Kem,
435434
C: Cipher,
436435
H: Hash,
437-
RNG: RngCore + CryptoRng,
436+
RNG: Rng,
438437
{
439438
type E = EKEM::PubKey;
440439
type S = SKEM::PubKey;

src/traits.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Common traits used throughout the crate
22
33
use arrayvec::ArrayString;
4-
use rand_core::{CryptoRng, RngCore};
4+
pub use rand_core::{CryptoRng, RngCore};
55
use zeroize::Zeroize;
66

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

22+
/// Common trait for compatible RNG sources
23+
///
24+
/// Automatically implemented for all types that implement:
25+
/// * [`RngCore`]
26+
/// * [`CryptoRng`]
27+
pub trait Rng: RngCore + CryptoRng {}
28+
29+
/// Automatic implementation for all supported types
30+
impl<T: RngCore + CryptoRng> Rng for T {}
31+
2232
/// Common trait for all Diffie-Hellman algorithms
2333
pub trait Dh: CryptoComponent {
2434
/// Private key type
@@ -29,9 +39,7 @@ pub trait Dh: CryptoComponent {
2939
type Output: ByteArray;
3040

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

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

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

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

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

0 commit comments

Comments
 (0)