File tree 2 files changed +13
-4
lines changed
2 files changed +13
-4
lines changed Original file line number Diff line number Diff line change 7
7
// except according to those terms.
8
8
9
9
//! Implementation for FreeBSD
10
- use crate :: util_libc:: fill_exact;
10
+ use crate :: util_libc:: { fill_exact, Weak } ;
11
11
use crate :: Error ;
12
- use core:: ptr;
12
+ use core:: { mem, ptr} ;
13
+
14
+ type GetRandomFn = unsafe extern "C" fn ( * mut u8 , libc:: size_t , libc:: c_uint ) -> libc:: ssize_t ;
13
15
14
16
fn kern_arnd ( buf : & mut [ u8 ] ) -> libc:: ssize_t {
15
17
static MIB : [ libc:: c_int ; 2 ] = [ libc:: CTL_KERN , libc:: KERN_ARND ] ;
@@ -33,5 +35,11 @@ fn kern_arnd(buf: &mut [u8]) -> libc::ssize_t {
33
35
}
34
36
35
37
pub fn getrandom_inner ( dest : & mut [ u8 ] ) -> Result < ( ) , Error > {
36
- fill_exact ( dest, kern_arnd)
38
+ static GETRANDOM : Weak = unsafe { Weak :: new ( "getrandom\0 " ) } ;
39
+ if let Some ( fptr) = GETRANDOM . ptr ( ) {
40
+ let func: GetRandomFn = unsafe { mem:: transmute ( fptr) } ;
41
+ fill_exact ( dest, |buf| unsafe { func ( buf. as_mut_ptr ( ) , buf. len ( ) , 0 ) } )
42
+ } else {
43
+ fill_exact ( dest, kern_arnd)
44
+ }
37
45
}
Original file line number Diff line number Diff line change 16
16
//! | Windows | [`RtlGenRandom`][3]
17
17
//! | macOS | [`getentropy()`][19] if available, otherise [`/dev/random`][20] (identical to `/dev/urandom`)
18
18
//! | iOS | [`SecRandomCopyBytes`][4]
19
- //! | FreeBSD | [`kern.arandom`][5]
19
+ //! | FreeBSD | [`getrandom()`][21] if available, otherise [` kern.arandom`][5]
20
20
//! | OpenBSD | [`getentropy`][6]
21
21
//! | NetBSD | [`/dev/urandom`][7] after reading from `/dev/random` once
22
22
//! | Dragonfly BSD | [`/dev/random`][8]
101
101
//! [18]: https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide
102
102
//! [19]: https://www.unix.com/man-page/mojave/2/getentropy/
103
103
//! [20]: https://www.unix.com/man-page/mojave/4/random/
104
+ //! [21]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable
104
105
105
106
#![ doc(
106
107
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" ,
You can’t perform that action at this time.
0 commit comments