std/sys/random/
mod.rs

1cfg_if::cfg_if! {
2    // Tier 1
3    if #[cfg(any(target_os = "linux", target_os = "android"))] {
4        mod linux;
5        pub use linux::{fill_bytes, hashmap_random_keys};
6    } else if #[cfg(target_os = "windows")] {
7        mod windows;
8        pub use windows::fill_bytes;
9    } else if #[cfg(target_vendor = "apple")] {
10        mod apple;
11        pub use apple::fill_bytes;
12    // Others, in alphabetical ordering.
13    } else if #[cfg(any(
14        target_os = "dragonfly",
15        target_os = "freebsd",
16        target_os = "haiku",
17        target_os = "illumos",
18        target_os = "netbsd",
19        target_os = "openbsd",
20        target_os = "rtems",
21        target_os = "solaris",
22        target_os = "vita",
23    ))] {
24        mod arc4random;
25        pub use arc4random::fill_bytes;
26    } else if #[cfg(target_os = "emscripten")] {
27        mod getentropy;
28        pub use getentropy::fill_bytes;
29    } else if #[cfg(target_os = "espidf")] {
30        mod espidf;
31        pub use espidf::fill_bytes;
32    } else if #[cfg(target_os = "fuchsia")] {
33        mod fuchsia;
34        pub use fuchsia::fill_bytes;
35    } else if #[cfg(target_os = "hermit")] {
36        mod hermit;
37        pub use hermit::fill_bytes;
38    } else if #[cfg(target_os = "horizon")] {
39        // FIXME: add arc4random_buf to shim-3ds
40        mod horizon;
41        pub use horizon::fill_bytes;
42    } else if #[cfg(any(
43        target_os = "aix",
44        target_os = "hurd",
45        target_os = "l4re",
46        target_os = "nto",
47        target_os = "nuttx",
48    ))] {
49        mod unix_legacy;
50        pub use unix_legacy::fill_bytes;
51    } else if #[cfg(target_os = "redox")] {
52        mod redox;
53        pub use redox::fill_bytes;
54    } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
55        mod sgx;
56        pub use sgx::fill_bytes;
57    } else if #[cfg(target_os = "solid_asp3")] {
58        mod solid;
59        pub use solid::fill_bytes;
60    } else if #[cfg(target_os = "teeos")] {
61        mod teeos;
62        pub use teeos::fill_bytes;
63    } else if #[cfg(target_os = "uefi")] {
64        mod uefi;
65        pub use uefi::fill_bytes;
66    } else if #[cfg(target_os = "vxworks")] {
67        mod vxworks;
68        pub use vxworks::fill_bytes;
69    } else if #[cfg(target_os = "wasi")] {
70        mod wasi;
71        pub use wasi::fill_bytes;
72    } else if #[cfg(target_os = "zkvm")] {
73        mod zkvm;
74        pub use zkvm::fill_bytes;
75    } else if #[cfg(any(
76        all(target_family = "wasm", target_os = "unknown"),
77        target_os = "xous",
78    ))] {
79        // FIXME: finally remove std support for wasm32-unknown-unknown
80        // FIXME: add random data generation to xous
81        mod unsupported;
82        pub use unsupported::{fill_bytes, hashmap_random_keys};
83    }
84}
85
86#[cfg(not(any(
87    target_os = "linux",
88    target_os = "android",
89    all(target_family = "wasm", target_os = "unknown"),
90    target_os = "xous",
91)))]
92pub fn hashmap_random_keys() -> (u64, u64) {
93    let mut buf = [0; 16];
94    fill_bytes(&mut buf);
95    let k1 = u64::from_ne_bytes(buf[..8].try_into().unwrap());
96    let k2 = u64::from_ne_bytes(buf[8..].try_into().unwrap());
97    (k1, k2)
98}