std/sys/random/
mod.rs

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