Skip to content

Commit 1438c12

Browse files
authored
Add OpenBSD support (Absolucy#40)
1 parent 8a87ac8 commit 1438c12

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ _Listed in order of priority_
7373
we will attempt to source as much entropy as possible via our [`rdseed_entropy`](entropy::rdseed_entropy) function
7474
* Linux and Android will attempt to use the [`getrandom`](https://man7.org/linux/man-pages/man2/getrandom.2.html) syscall.
7575
* macOS and iOS (Darwin-based systems) will use Security.framework's [`SecRandomCopyBytes`](https://developer.apple.com/documentation/security/1399291-secrandomcopybytes).
76+
* OpenBSD will attempt to use the [`arc4random_buf`](https://man.openbsd.org/arc4random.3) function.
7677
* Windows
7778
* If we're targeting UWP, then the [`BCryptGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom) is used with system-preferred RNG (`BCRYPT_USE_SYSTEM_PREFERRED_RNG`).
7879
* Otherwise, we'll use [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom).

src/entropy.rs

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub use darwin::entropy as system;
88
not(feature = "getrandom")
99
))]
1010
pub use linux::entropy as system;
11+
#[cfg(all(target_os = "openbsd", not(feature = "getrandom")))]
12+
pub use openbsd::entropy as system;
1113
#[cfg(all(windows, not(target_vendor = "uwp"), not(feature = "getrandom")))]
1214
pub use windows::entropy as system;
1315
#[cfg(all(windows, target_vendor = "uwp", not(feature = "getrandom")))]
@@ -32,6 +34,10 @@ pub mod windows_uwp;
3234
/// An entropy generator for Windows, using WinAPI's `RtlGenRandom` function.
3335
pub mod windows;
3436

37+
#[cfg(all(target_os = "openbsd", not(feature = "getrandom")))]
38+
/// An entropy generator for OpenBSD, using libc's `arc4random_buf` function.
39+
pub mod openbsd;
40+
3541
#[cfg(feature = "getrandom")]
3642
/// Pull in system entropy using the [`getrandom`](https://crates.io/crates/getrandom) crate.
3743
/// Uses backup entropy (rdseed and system time) if it fails.
@@ -47,6 +53,7 @@ pub fn system(out: &mut [u8]) {
4753
feature = "getrandom",
4854
target_os = "linux",
4955
target_os = "android",
56+
target_os = "openbsd",
5057
target_vendor = "apple",
5158
windows
5259
)))]

src/entropy/openbsd.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use core::ffi::c_void;
2+
3+
extern "C" {
4+
fn arc4random_buf(buf: *mut c_void, nbytes: usize);
5+
}
6+
7+
/// Obtain a series of random bytes.
8+
pub fn entropy(out: &mut [u8]) -> bool {
9+
unsafe {
10+
arc4random_buf(out.as_mut_ptr() as *mut c_void, out.len());
11+
true
12+
}
13+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
//! we will attempt to source as much entropy as possible via our [`rdseed_entropy`](entropy::rdseed_entropy) function
7979
//! * Linux and Android will attempt to use the [`getrandom`](https://man7.org/linux/man-pages/man2/getrandom.2.html) syscall.
8080
//! * macOS and iOS (Darwin-based systems) will use Security.framework's [`SecRandomCopyBytes`](https://developer.apple.com/documentation/security/1399291-secrandomcopybytes).
81+
//! * OpenBSD will attempt to use the [`arc4random_buf`](https://man.openbsd.org/arc4random.3) function.
8182
//! * Windows
8283
//! * If we're targeting UWP, then the [`BCryptGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom) is used with system-preferred RNG (`BCRYPT_USE_SYSTEM_PREFERRED_RNG`).
8384
//! * Otherwise, we'll use [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom).

0 commit comments

Comments
 (0)