@@ -2,11 +2,12 @@ use num_bigint::traits::ModInverse;
2
2
use num_bigint:: Sign :: Plus ;
3
3
use num_bigint:: { BigInt , BigUint } ;
4
4
use num_traits:: { FromPrimitive , One } ;
5
- use rand:: { rngs:: ThreadRng , Rng } ;
5
+ use rand:: { rngs:: StdRng , Rng } ;
6
6
#[ cfg( feature = "serde" ) ]
7
7
use serde_crate:: { Deserialize , Serialize } ;
8
- use std :: ops:: Deref ;
8
+ use core :: ops:: Deref ;
9
9
use zeroize:: Zeroize ;
10
+ use alloc:: vec:: Vec ;
10
11
11
12
use crate :: algorithms:: { generate_multi_prime_key, generate_multi_prime_key_with_exp} ;
12
13
use crate :: errors:: { Error , Result } ;
@@ -247,6 +248,7 @@ impl RSAPublicKey {
247
248
/// let der_bytes = base64::decode(&der_encoded).expect("failed to decode base64 content");
248
249
/// let public_key = RSAPublicKey::from_pkcs1(&der_bytes).expect("failed to parse key");
249
250
/// ```
251
+ #[ cfg( feature = "std" ) ]
250
252
pub fn from_pkcs1 ( der : & [ u8 ] ) -> Result < RSAPublicKey > {
251
253
crate :: parse:: parse_public_key_pkcs1 ( der)
252
254
}
@@ -281,6 +283,7 @@ impl RSAPublicKey {
281
283
/// let der_bytes = base64::decode(&der_encoded).expect("failed to decode base64 content");
282
284
/// let public_key = RSAPublicKey::from_pkcs8(&der_bytes).expect("failed to parse key");
283
285
/// ```
286
+ #[ cfg( feature = "std" ) ]
284
287
pub fn from_pkcs8 ( der : & [ u8 ] ) -> Result < RSAPublicKey > {
285
288
crate :: parse:: parse_public_key_pkcs8 ( der)
286
289
}
@@ -405,6 +408,7 @@ impl RSAPrivateKey {
405
408
/// let der_bytes = base64::decode(&der_encoded).expect("failed to decode base64 content");
406
409
/// let private_key = RSAPrivateKey::from_pkcs1(&der_bytes).expect("failed to parse key");
407
410
/// ```
411
+ #[ cfg( feature = "std" ) ]
408
412
pub fn from_pkcs1 ( der : & [ u8 ] ) -> Result < RSAPrivateKey > {
409
413
crate :: parse:: parse_private_key_pkcs1 ( der)
410
414
}
@@ -445,6 +449,7 @@ impl RSAPrivateKey {
445
449
/// let der_bytes = base64::decode(&der_encoded).expect("failed to decode base64 content");
446
450
/// let private_key = RSAPrivateKey::from_pkcs8(&der_bytes).expect("failed to parse key");
447
451
/// ```
452
+ #[ cfg( feature = "std" ) ]
448
453
pub fn from_pkcs8 ( der : & [ u8 ] ) -> Result < RSAPrivateKey > {
449
454
crate :: parse:: parse_private_key_pkcs8 ( der)
450
455
}
@@ -554,10 +559,10 @@ impl RSAPrivateKey {
554
559
match padding {
555
560
// need to pass any Rng as the type arg, so the type checker is happy, it is not actually used for anything
556
561
PaddingScheme :: PKCS1v15Encrypt => {
557
- pkcs1v15:: decrypt :: < ThreadRng , _ > ( None , self , ciphertext)
562
+ pkcs1v15:: decrypt :: < StdRng , _ > ( None , self , ciphertext)
558
563
}
559
564
PaddingScheme :: OAEP { mut digest, label } => {
560
- oaep:: decrypt :: < ThreadRng , _ > ( None , self , ciphertext, & mut * digest, label)
565
+ oaep:: decrypt :: < StdRng , _ > ( None , self , ciphertext, & mut * digest, label)
561
566
}
562
567
_ => Err ( Error :: InvalidPaddingScheme ) ,
563
568
}
@@ -584,14 +589,15 @@ impl RSAPrivateKey {
584
589
/// Sign the given digest.
585
590
pub fn sign ( & self , padding : PaddingScheme , digest_in : & [ u8 ] ) -> Result < Vec < u8 > > {
586
591
match padding {
592
+ // need to pass any Rng as the type arg, so the type checker is happy, it is not actually used for anything
587
593
PaddingScheme :: PKCS1v15Sign { ref hash } => {
588
- pkcs1v15:: sign :: < ThreadRng , _ > ( None , self , hash. as_ref ( ) , digest_in)
594
+ pkcs1v15:: sign :: < StdRng , _ > ( None , self , hash. as_ref ( ) , digest_in)
589
595
}
590
596
PaddingScheme :: PSS {
591
597
mut salt_rng,
592
598
mut digest,
593
599
salt_len,
594
- } => pss:: sign :: < _ , ThreadRng , _ > (
600
+ } => pss:: sign :: < _ , StdRng , _ > (
595
601
& mut * salt_rng,
596
602
None ,
597
603
self ,
@@ -652,9 +658,10 @@ mod tests {
652
658
use super :: * ;
653
659
use crate :: internals;
654
660
661
+ use std:: time:: SystemTime ;
655
662
use digest:: { Digest , DynDigest } ;
656
663
use num_traits:: { FromPrimitive , ToPrimitive } ;
657
- use rand:: { distributions:: Alphanumeric , rngs:: ThreadRng , thread_rng } ;
664
+ use rand:: { distributions:: Alphanumeric , rngs:: StdRng , SeedableRng } ;
658
665
use sha1:: Sha1 ;
659
666
use sha2:: { Sha224 , Sha256 , Sha384 , Sha512 } ;
660
667
use sha3:: { Sha3_256 , Sha3_384 , Sha3_512 } ;
@@ -687,10 +694,11 @@ mod tests {
687
694
let pub_key: RSAPublicKey = private_key. clone ( ) . into ( ) ;
688
695
let m = BigUint :: from_u64 ( 42 ) . expect ( "invalid 42" ) ;
689
696
let c = internals:: encrypt ( & pub_key, & m) ;
690
- let m2 = internals:: decrypt :: < ThreadRng > ( None , & private_key, & c)
697
+ let m2 = internals:: decrypt :: < StdRng > ( None , & private_key, & c)
691
698
. expect ( "unable to decrypt without blinding" ) ;
692
699
assert_eq ! ( m, m2) ;
693
- let mut rng = thread_rng ( ) ;
700
+ let seed = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
701
+ let mut rng = StdRng :: seed_from_u64 ( seed. as_secs ( ) ) ;
694
702
let m3 = internals:: decrypt ( Some ( & mut rng) , & private_key, & c)
695
703
. expect ( "unable to decrypt with blinding" ) ;
696
704
assert_eq ! ( m, m3) ;
@@ -700,7 +708,8 @@ mod tests {
700
708
( $name: ident, $multi: expr, $size: expr) => {
701
709
#[ test]
702
710
fn $name( ) {
703
- let mut rng = thread_rng( ) ;
711
+ let seed = SystemTime :: now( ) . duration_since( SystemTime :: UNIX_EPOCH ) . unwrap( ) ;
712
+ let mut rng = StdRng :: seed_from_u64( seed. as_secs( ) ) ;
704
713
705
714
for _ in 0 ..10 {
706
715
let private_key = if $multi == 2 {
@@ -730,7 +739,8 @@ mod tests {
730
739
#[ test]
731
740
fn test_impossible_keys ( ) {
732
741
// make sure not infinite loops are hit here.
733
- let mut rng = thread_rng ( ) ;
742
+ let seed = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
743
+ let mut rng = StdRng :: seed_from_u64 ( seed. as_secs ( ) ) ;
734
744
for i in 0 ..32 {
735
745
let _ = RSAPrivateKey :: new ( & mut rng, i) . is_err ( ) ;
736
746
let _ = generate_multi_prime_key ( & mut rng, 3 , i) ;
@@ -902,7 +912,8 @@ mod tests {
902
912
}
903
913
904
914
fn do_test_encrypt_decrypt_oaep < D : ' static + Digest + DynDigest > ( prk : & RSAPrivateKey ) {
905
- let mut rng = thread_rng ( ) ;
915
+ let seed = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
916
+ let mut rng = StdRng :: seed_from_u64 ( seed. as_secs ( ) ) ;
906
917
907
918
let k = prk. size ( ) ;
908
919
@@ -913,7 +924,7 @@ mod tests {
913
924
}
914
925
let has_label: bool = rng. gen ( ) ;
915
926
let label: Option < String > = if has_label {
916
- Some ( rng. sample_iter ( & Alphanumeric ) . take ( 30 ) . collect ( ) )
927
+ Some ( rng. clone ( ) . sample_iter ( & Alphanumeric ) . take ( 30 ) . collect ( ) )
917
928
} else {
918
929
None
919
930
} ;
@@ -949,7 +960,8 @@ mod tests {
949
960
950
961
#[ test]
951
962
fn test_decrypt_oaep_invalid_hash ( ) {
952
- let mut rng = thread_rng ( ) ;
963
+ let seed = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
964
+ let mut rng = StdRng :: seed_from_u64 ( seed. as_secs ( ) ) ;
953
965
let priv_key = get_private_key ( ) ;
954
966
let pub_key: RSAPublicKey = ( & priv_key) . into ( ) ;
955
967
let ciphertext = pub_key
0 commit comments