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