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
12
use core:: num:: NonZeroU32 ;
13
- use core:: ptr;
13
+ use core:: { mem, ptr} ;
14
+
15
+ type GetRandomFn = unsafe extern "C" fn ( * mut u8 , libc:: size_t , libc:: c_uint ) -> libc:: ssize_t ;
14
16
15
17
fn kern_arnd ( buf : & mut [ u8 ] ) -> libc:: ssize_t {
16
18
static MIB : [ libc:: c_int ; 2 ] = [ libc:: CTL_KERN , libc:: KERN_ARND ] ;
@@ -34,7 +36,13 @@ fn kern_arnd(buf: &mut [u8]) -> libc::ssize_t {
34
36
}
35
37
36
38
pub fn getrandom_inner ( dest : & mut [ u8 ] ) -> Result < ( ) , Error > {
37
- fill_exact ( dest, kern_arnd)
39
+ static GETRANDOM : Weak = unsafe { Weak :: new ( "getrandom\0 " ) } ;
40
+ if let Some ( fptr) = GETRANDOM . ptr ( ) {
41
+ let func: GetRandomFn = unsafe { mem:: transmute ( fptr) } ;
42
+ fill_exact ( dest, |buf| unsafe { func ( buf. as_mut_ptr ( ) , buf. len ( ) , 0 ) } )
43
+ } else {
44
+ fill_exact ( dest, kern_arnd)
45
+ }
38
46
}
39
47
40
48
#[ inline( always) ]
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]
118
118
//! [18]: https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide
119
119
//! [19]: https://www.unix.com/man-page/mojave/2/getentropy/
120
120
//! [20]: https://www.unix.com/man-page/mojave/4/random/
121
+ //! [21]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable
121
122
122
123
#![ doc(
123
124
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