Skip to content

Commit d87d339

Browse files
authored
Document use of /dev/urandom vs /dev/random (#311)
Also, on platforms where the devices are the same, prefer using /dev/urandom. Documentation of the devices being the same: - macOS [`random(4)`](https://www.unix.com/man-page/mojave/4/urandom/) - DragonFly [`getrandom(2)`](https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom) - Haiku [implementation](https://github.com/haiku/haiku/blob/f40bacae87f37276be7d107a6b3d41ca97b4d82c/src/add-ons/kernel/bus_managers/random/driver.cpp#L217-L222) Signed-off-by: Joe Richey <[email protected]> Signed-off-by: Joe Richey <[email protected]>
1 parent 88d0d31 commit d87d339

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414
//! | ----------------- | ------------------ | --------------
1515
//! | Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call if available, otherwise [`/dev/urandom`][2] after successfully polling `/dev/random`
1616
//! | Windows | `*‑windows‑*` | [`BCryptGenRandom`]
17-
//! | macOS | `*‑apple‑darwin` | [`getentropy`][3] if available, otherwise [`/dev/random`][4] (identical to `/dev/urandom`)
17+
//! | macOS | `*‑apple‑darwin` | [`getentropy`][3] if available, otherwise [`/dev/urandom`][4] (identical to `/dev/random`)
1818
//! | iOS | `*‑apple‑ios` | [`SecRandomCopyBytes`]
1919
//! | FreeBSD | `*‑freebsd` | [`getrandom`][5] if available, otherwise [`kern.arandom`][6]
2020
//! | OpenBSD | `*‑openbsd` | [`getentropy`][7]
2121
//! | NetBSD | `*‑netbsd` | [`kern.arandom`][8]
22-
//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9] if available, otherwise [`/dev/random`][10]
22+
//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9] if available, otherwise [`/dev/urandom`][10] (identical to `/dev/random`)
2323
//! | Solaris, illumos | `*‑solaris`, `*‑illumos` | [`getrandom`][11] if available, otherwise [`/dev/random`][12]
2424
//! | Fuchsia OS | `*‑fuchsia` | [`cprng_draw`]
2525
//! | Redox | `*‑redox` | `/dev/urandom`
26-
//! | Haiku | `*‑haiku` | `/dev/random` (identical to `/dev/urandom`)
26+
//! | Haiku | `*‑haiku` | `/dev/urandom` (identical to `/dev/random`)
2727
//! | Hermit | `x86_64-*-hermit` | [`RDRAND`]
2828
//! | SGX | `x86_64‑*‑sgx` | [`RDRAND`]
2929
//! | VxWorks | `*‑wrs‑vxworks‑*` | `randABytes` after checking entropy pool initialization with `randSecure`
3030
//! | ESP-IDF | `*‑espidf` | [`esp_fill_random`]
31-
//! | Emscripten | `*‑emscripten` | `/dev/random` (identical to `/dev/urandom`)
31+
//! | Emscripten | `*‑emscripten` | `/dev/urandom` (identical to `/dev/random`)
3232
//! | WASI | `wasm32‑wasi` | [`random_get`]
3333
//! | Web Browser and Node.js | `wasm32‑*‑unknown` | [`Crypto.getRandomValues`] if available, then [`crypto.randomFillSync`] if on Node.js, see [WebAssembly support]
3434
//! | SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes`
@@ -150,7 +150,7 @@
150150
//! [1]: http://man7.org/linux/man-pages/man2/getrandom.2.html
151151
//! [2]: http://man7.org/linux/man-pages/man4/urandom.4.html
152152
//! [3]: https://www.unix.com/man-page/mojave/2/getentropy/
153-
//! [4]: https://www.unix.com/man-page/mojave/4/random/
153+
//! [4]: https://www.unix.com/man-page/mojave/4/urandom/
154154
//! [5]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable
155155
//! [6]: https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4
156156
//! [7]: https://man.openbsd.org/getentropy.2

src/use_file.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ use core::{
1818
sync::atomic::{AtomicUsize, Ordering::Relaxed},
1919
};
2020

21+
// We prefer using /dev/urandom and only use /dev/random if the OS
22+
// documentation indicates that /dev/urandom is insecure.
23+
// On Solaris/Illumos, see src/solaris_illumos.rs
24+
// On Dragonfly, Haiku, and macOS, the devices are identical.
25+
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
26+
const FILE_PATH: &str = "/dev/random\0";
2127
#[cfg(any(
28+
target_os = "android",
29+
target_os = "linux",
30+
target_os = "redox",
2231
target_os = "dragonfly",
2332
target_os = "emscripten",
2433
target_os = "haiku",
25-
target_os = "macos",
26-
target_os = "solaris",
27-
target_os = "illumos"
34+
target_os = "macos"
2835
))]
29-
const FILE_PATH: &str = "/dev/random\0";
30-
#[cfg(any(target_os = "android", target_os = "linux", target_os = "redox"))]
3136
const FILE_PATH: &str = "/dev/urandom\0";
3237

3338
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {

0 commit comments

Comments
 (0)