@@ -12,11 +12,11 @@ use mbedtls_sys::types::size_t;
1212use rand:: { Rng , XorShiftRng } ;
1313
1414/// Not cryptographically secure!!! Use for testing only!!!
15- pub struct TestRandom ( XorShiftRng ) ;
15+ pub struct TestInsecureRandom ( XorShiftRng ) ;
1616
17- impl crate :: mbedtls:: rng:: RngCallbackMut for TestRandom {
17+ impl crate :: mbedtls:: rng:: RngCallbackMut for TestInsecureRandom {
1818 unsafe extern "C" fn call_mut ( p_rng : * mut c_void , data : * mut c_uchar , len : size_t ) -> c_int {
19- ( * ( p_rng as * mut TestRandom ) )
19+ ( * ( p_rng as * mut TestInsecureRandom ) )
2020 . 0
2121 . fill_bytes ( core:: slice:: from_raw_parts_mut ( data, len) ) ;
2222 0
@@ -27,9 +27,9 @@ impl crate::mbedtls::rng::RngCallbackMut for TestRandom {
2727 }
2828}
2929
30- impl crate :: mbedtls:: rng:: RngCallback for TestRandom {
30+ impl crate :: mbedtls:: rng:: RngCallback for TestInsecureRandom {
3131 unsafe extern "C" fn call ( p_rng : * mut c_void , data : * mut c_uchar , len : size_t ) -> c_int {
32- ( * ( p_rng as * mut TestRandom ) )
32+ ( * ( p_rng as * mut TestInsecureRandom ) )
3333 . 0
3434 . fill_bytes ( core:: slice:: from_raw_parts_mut ( data, len) ) ;
3535 0
@@ -40,7 +40,36 @@ impl crate::mbedtls::rng::RngCallback for TestRandom {
4040 }
4141}
4242
43+ cfg_if:: cfg_if! {
44+ if #[ cfg( any( feature = "rdrand" , target_env = "sgx" , feature = "std" ) ) ]
45+ {
46+ pub type TestRandom = crate :: mbedtls:: rng:: CtrDrbg ;
47+ } else {
48+ pub type TestRandom = TestInsecureRandom ;
49+ }
50+ }
51+
4352/// Not cryptographically secure!!! Use for testing only!!!
4453pub fn test_rng ( ) -> TestRandom {
45- TestRandom ( XorShiftRng :: new_unseeded ( ) )
54+ cfg_if:: cfg_if! {
55+ if #[ cfg( any( feature = "rdrand" , target_env = "sgx" , feature = "std" ) ) ]
56+ {
57+ #[ cfg( feature = "std" ) ]
58+ use std:: sync:: Arc ;
59+ #[ cfg( not( feature = "std" ) ) ]
60+ extern crate alloc as rust_alloc;
61+ #[ cfg( not( feature = "std" ) ) ]
62+ use rust_alloc:: sync:: Arc ;
63+
64+ let entropy = Arc :: new( super :: entropy:: entropy_new( ) ) ;
65+ TestRandom :: new( entropy, None ) . unwrap( )
66+ } else {
67+ test_deterministic_rng( )
68+ }
69+ }
70+ }
71+
72+ /// Not cryptographically secure!!! Use for testing only!!!
73+ pub fn test_deterministic_rng ( ) -> TestInsecureRandom {
74+ TestInsecureRandom ( XorShiftRng :: new_unseeded ( ) )
4675}
0 commit comments