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 = "trusty")] {
64        mod trusty;
65        pub use trusty::fill_bytes;
66    } else if #[cfg(target_os = "uefi")] {
67        mod uefi;
68        pub use uefi::fill_bytes;
69    } else if #[cfg(target_os = "vxworks")] {
70        mod vxworks;
71        pub use vxworks::fill_bytes;
72    } else if #[cfg(target_os = "wasi")] {
73        mod wasi;
74        pub use wasi::fill_bytes;
75    } else if #[cfg(target_os = "zkvm")] {
76        mod zkvm;
77        pub use zkvm::fill_bytes;
78    } else if #[cfg(any(
79        all(target_family = "wasm", target_os = "unknown"),
80        target_os = "xous",
81    ))] {
82        // FIXME: finally remove std support for wasm32-unknown-unknown
83        // FIXME: add random data generation to xous
84        mod unsupported;
85        pub use unsupported::{fill_bytes, hashmap_random_keys};
86    }
87}
88
89#[cfg(not(any(
90    target_os = "linux",
91    target_os = "android",
92    all(target_family = "wasm", target_os = "unknown"),
93    target_os = "xous",
94)))]
95pub fn hashmap_random_keys() -> (u64, u64) {
96    let mut buf = [0; 16];
97    fill_bytes(&mut buf);
98    let k1 = u64::from_ne_bytes(buf[..8].try_into().unwrap());
99    let k2 = u64::from_ne_bytes(buf[8..].try_into().unwrap());
100    (k1, k2)
101}