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