Use arc4random(3) on OpenBSD and NetBSD
diff --git a/src/os.rs b/src/os.rs
index 4cfad6b..9a06958 100644
--- a/src/os.rs
+++ b/src/os.rs
@@ -54,7 +54,9 @@
#[cfg(all(unix, not(target_os = "ios"),
not(target_os = "nacl"),
- not(target_os = "freebsd")))]
+ not(target_os = "freebsd"),
+ not(target_os = "netbsd"),
+ not(target_os = "openbsd")))]
mod imp {
extern crate libc;
@@ -289,6 +291,45 @@
}
}
+#[cfg(target_os = "openbsd")]
+mod imp {
+ extern crate libc;
+
+ use std::io;
+ use Rng;
+
+ use super::{next_u32, next_u64};
+
+ pub struct OsRng;
+
+ impl OsRng {
+ pub fn new() -> io::Result<OsRng> {
+ Ok(OsRng)
+ }
+ }
+
+ impl Rng for OsRng {
+ fn next_u32(&mut self) -> u32 {
+ next_u32(&mut |v| self.fill_bytes(v))
+ }
+ fn next_u64(&mut self) -> u64 {
+ next_u64(&mut |v| self.fill_bytes(v))
+ }
+ fn fill_bytes(&mut self, v: &mut [u8]) {
+ // getentropy(2) permits a maximum buffer size of 256 bytes
+ for s in v.chunks_mut(256) {
+ let ret = unsafe {
+ libc::getentropy(s.as_mut_ptr() as *mut libc::c_void, s.len())
+ };
+ if ret == -1 {
+ let err = io::Error::last_os_error();
+ panic!("getentropy failed: {}", err);
+ }
+ }
+ }
+ }
+}
+
#[cfg(windows)]
mod imp {
use std::io;