@@ -22,7 +22,7 @@ use crate::ctypes::{size_t, ssize_t};
2222
2323use axerrno:: LinuxError ;
2424
25- #[ cfg( all( target_arch = "x86_64" , feature = "random-hw" ) ) ]
25+ #[ cfg( all( target_arch = "x86_64" , feature = "random-hw" , not ( feature = "rng" ) ) ) ]
2626use core:: arch:: x86_64:: __cpuid;
2727
2828static SEED : AtomicU64 = AtomicU64 :: new ( 0xae_f3 ) ;
@@ -53,7 +53,7 @@ fn srand_lcg(seed: u64) {
5353}
5454
5555/// Checking if the CPU core is compatible with hardware random number instructions.
56- #[ cfg( feature = "random-hw" ) ]
56+ #[ cfg( all ( feature = "random-hw" , not ( feature = "rng" ) ) ) ]
5757fn has_rdrand ( ) -> bool {
5858 #[ cfg( target_arch = "x86_64" ) ]
5959 {
@@ -77,7 +77,7 @@ fn has_rdrand() -> bool {
7777}
7878
7979/// Return 64-bit unsigned random interger using cpu instruction
80- #[ cfg( feature = "random-hw" ) ]
80+ #[ cfg( all ( feature = "random-hw" , not ( feature = "rng" ) ) ) ]
8181fn random_hw ( ) -> u64 {
8282 let mut _random: u64 ;
8383
@@ -118,33 +118,38 @@ pub unsafe extern "C" fn sys_srand(_seed: c_uint) {
118118/// Returns a 32-bit unsigned random integer
119119#[ no_mangle]
120120pub unsafe extern "C" fn sys_rand ( ) -> c_int {
121- #[ cfg( feature = "random-hw" ) ]
121+ #[ cfg( all ( feature = "rng" , not ( feature = " random-hw") ) ) ]
122122 {
123- match has_rdrand ( ) {
124- true => ( random_hw ( ) >> 33 ) as c_int ,
125- false => rand_lcg32 ( ) as c_int ,
126- }
123+ use ruxrand:: random;
124+ return random :: < c_int > ( ) ;
127125 }
128- #[ cfg( not ( feature = "random-hw" ) ) ]
126+ #[ cfg( all ( feature = "random-hw" , not ( feature = "rng" ) ) ) ]
129127 {
130- rand_lcg32 ( ) as c_int
128+ match has_rdrand ( ) {
129+ true => return ( random_hw ( ) >> 33 ) as c_int ,
130+ false => return rand_lcg32 ( ) as c_int ,
131+ }
131132 }
133+ rand_lcg32 ( ) as c_int
132134}
133135
134136/// Returns a 64-bit unsigned random integer
135137#[ no_mangle]
136138pub unsafe extern "C" fn sys_random ( ) -> c_long {
137- #[ cfg( feature = "random-hw" ) ]
139+ #[ cfg( all ( feature = "rng" , not ( feature = " random-hw") ) ) ]
138140 {
139- match has_rdrand ( ) {
140- true => ( random_hw ( ) >> 1 ) as c_long ,
141- false => random_lcg64 ( ) as c_long ,
142- }
141+ use ruxrand:: random;
142+ return random :: < c_long > ( ) ;
143143 }
144- #[ cfg( not ( feature = "random-hw" ) ) ]
144+ #[ cfg( all ( feature = "random-hw" , not ( feature = "rng" ) ) ) ]
145145 {
146- random_lcg64 ( ) as c_long
146+ match has_rdrand ( ) {
147+ true => return ( random_hw ( ) >> 1 ) as c_long ,
148+ false => return random_lcg64 ( ) as c_long ,
149+ }
147150 }
151+
152+ random_lcg64 ( ) as c_long
148153}
149154
150155/// Fills the buffer pointed to by buf with up to buflen random bytes.
@@ -159,15 +164,29 @@ pub unsafe extern "C" fn sys_getrandom(buf: *mut c_void, buflen: size_t, flags:
159164 if flags != 0 {
160165 warn!( "flags are not implemented yet, flags: {flags}, ignored" ) ;
161166 }
162- // fill the buffer 8 bytes at a time first, then fill the remaining bytes
163- let buflen_mod = buflen % ( core:: mem:: size_of:: <i64 >( ) / core:: mem:: size_of:: <u8 >( ) ) ;
164- let buflen_div = buflen / ( core:: mem:: size_of:: <i64 >( ) / core:: mem:: size_of:: <u8 >( ) ) ;
165- for i in 0 ..buflen_div {
166- * ( ( buf as * mut u8 as * mut i64 ) . add( i) ) = sys_random( ) as i64 ;
167+ #[ cfg( feature = "rng" ) ]
168+ {
169+ use ruxrand:: request_entropy;
170+ let slice: & mut [ u8 ] =
171+ unsafe { core:: slice:: from_raw_parts_mut( buf as * mut u8 , buflen) } ;
172+ request_entropy( slice) . map_err( |e| {
173+ warn!( "Failed to get random bytes: {e:?}" ) ;
174+ LinuxError :: EIO
175+ } ) ?;
176+ Ok ( buflen as ssize_t)
167177 }
168- for i in 0 ..buflen_mod {
169- * ( ( buf as * mut u8 ) . add( buflen - buflen_mod + i) ) = sys_rand( ) as u8 ;
178+ #[ cfg( not( feature = "rng" ) ) ]
179+ {
180+ // fill the buffer 8 bytes at a time first, then fill the remaining bytes
181+ let buflen_mod = buflen % ( core:: mem:: size_of:: <i64 >( ) / core:: mem:: size_of:: <u8 >( ) ) ;
182+ let buflen_div = buflen / ( core:: mem:: size_of:: <i64 >( ) / core:: mem:: size_of:: <u8 >( ) ) ;
183+ for i in 0 ..buflen_div {
184+ * ( ( buf as * mut u8 as * mut i64 ) . add( i) ) = sys_random( ) as i64 ;
185+ }
186+ for i in 0 ..buflen_mod {
187+ * ( ( buf as * mut u8 ) . add( buflen - buflen_mod + i) ) = sys_rand( ) as u8 ;
188+ }
189+ Ok ( buflen as ssize_t)
170190 }
171- Ok ( buflen as ssize_t)
172191 } )
173192}
0 commit comments