File tree Expand file tree Collapse file tree 1 file changed +16
-6
lines changed Expand file tree Collapse file tree 1 file changed +16
-6
lines changed Original file line number Diff line number Diff line change @@ -29,18 +29,28 @@ fn rd_rand_entropy() -> [u8; 32] {
29
29
// Check if the CPU supports `RDRAND`.
30
30
if let Some ( rd_rand) = RdRand :: new ( ) {
31
31
for i in 0 ..4 {
32
- let value = loop {
33
- if let Some ( value) = rd_rand. get_u64 ( ) {
34
- break value;
35
- }
36
- } ;
37
- entropy[ i * 8 ..( i + 1 ) * 8 ] . copy_from_slice ( & value. to_ne_bytes ( ) ) ;
32
+ if let Some ( value) = get_random_64 ( rd_rand) {
33
+ entropy[ i * 8 ..( i + 1 ) * 8 ] . copy_from_slice ( & value. to_ne_bytes ( ) ) ;
34
+ }
38
35
}
39
36
}
40
37
41
38
entropy
42
39
}
43
40
41
+ /// Try to fetch a 64 bit random value with a retry count limit of 10.
42
+ ///
43
+ /// This function is a port of the C implementation provided in Intel's Software Developer's Manual, Volume 1, 7.3.17.1.
44
+ fn get_random_64 ( rd_rand : RdRand ) -> Option < u64 > {
45
+ const RETRY_LIMIT : u32 = 10 ;
46
+ for _ in 0 ..RETRY_LIMIT {
47
+ if let Some ( value) = rd_rand. get_u64 ( ) {
48
+ return Some ( value) ;
49
+ }
50
+ }
51
+ None
52
+ }
53
+
44
54
/// Gather entropy by reading the current time with the `RDTSC` instruction if it's available.
45
55
///
46
56
/// This function doesn't provide particulary good entropy, but it's better than nothing.
You can’t perform that action at this time.
0 commit comments