Skip to main content

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(target_os = "aix", target_os = "hurd", target_os = "nto", target_os = "qnx") => {
53        mod unix_legacy;
54        pub use unix_legacy::fill_bytes;
55    }
56    target_os = "redox" => {
57        mod redox;
58        pub use redox::fill_bytes;
59    }
60    target_os = "motor" => {
61        mod motor;
62        pub use motor::fill_bytes;
63    }
64    all(target_vendor = "fortanix", target_env = "sgx") => {
65        mod sgx;
66        pub use sgx::fill_bytes;
67    }
68    target_os = "solid_asp3" => {
69        mod solid;
70        pub use solid::fill_bytes;
71    }
72    target_os = "teeos" => {
73        mod teeos;
74        pub use teeos::fill_bytes;
75    }
76    target_os = "trusty" => {
77        mod trusty;
78        pub use trusty::fill_bytes;
79    }
80    target_os = "uefi" => {
81        mod uefi;
82        pub use uefi::fill_bytes;
83    }
84    target_os = "vxworks" => {
85        mod vxworks;
86        pub use vxworks::fill_bytes;
87    }
88    all(target_os = "wasi", target_env = "p1") => {
89        mod wasip1;
90        pub use wasip1::fill_bytes;
91    }
92    all(target_os = "wasi", any(target_env = "p2", target_env = "p3")) => {
93        mod wasi;
94        pub use wasi::{fill_bytes, hashmap_random_keys};
95    }
96    target_os = "zkvm" => {
97        mod zkvm;
98        pub use zkvm::fill_bytes;
99    }
100    any(
101        all(target_family = "wasm", target_os = "unknown"),
102        target_os = "xous",
103        target_os = "vexos",
104        target_os = "l4re",
105    ) => {
106        // FIXME: finally remove std support for wasm32-unknown-unknown
107        // FIXME: add random data generation to xous
108        mod unsupported;
109        pub use unsupported::{fill_bytes, hashmap_random_keys};
110    }
111    _ => {}
112}
113
114#[cfg(not(any(
115    target_os = "linux",
116    target_os = "android",
117    all(target_family = "wasm", target_os = "unknown"),
118    all(target_os = "wasi", not(target_env = "p1")),
119    target_os = "xous",
120    target_os = "vexos",
121    target_os = "l4re",
122)))]
123pub fn hashmap_random_keys() -> (u64, u64) {
124    let mut buf = [0; 16];
125    fill_bytes(&mut buf);
126    let k1 = u64::from_ne_bytes(buf[..8].try_into().unwrap());
127    let k2 = u64::from_ne_bytes(buf[8..].try_into().unwrap());
128    (k1, k2)
129}