From eb726fa0595b1ffb2d36d62bc0cbea257c48c10f Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 28 Jun 2019 15:35:41 -0700 Subject: [PATCH 1/4] Move macOS/iOS implementaiton to ios.rs --- src/{macos.rs => ios.rs} | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/{macos.rs => ios.rs} (97%) diff --git a/src/macos.rs b/src/ios.rs similarity index 97% rename from src/macos.rs rename to src/ios.rs index 24157f9f..ed7f2cf1 100644 --- a/src/macos.rs +++ b/src/ios.rs @@ -6,7 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Implementation for MacOS / iOS +//! Implementation for iOS extern crate std; use crate::Error; diff --git a/src/lib.rs b/src/lib.rs index 063dffc9..5d1b40df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -181,7 +181,7 @@ mod_use!(cfg(target_os = "freebsd"), freebsd); mod_use!(cfg(target_os = "fuchsia"), fuchsia); mod_use!(cfg(target_os = "haiku"), use_file); mod_use!(cfg(target_os = "illumos"), solaris_illumos); -mod_use!(cfg(target_os = "ios"), macos); +mod_use!(cfg(target_os = "ios"), ios); mod_use!(cfg(target_os = "linux"), linux_android); mod_use!(cfg(target_os = "macos"), macos); mod_use!(cfg(target_os = "netbsd"), use_file); From 5cce845a6e8b393104b32f8644f6e3db50d1578a Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 28 Jun 2019 15:52:24 -0700 Subject: [PATCH 2/4] Add macOS implementation that uses getentropy() --- src/macos.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/macos.rs diff --git a/src/macos.rs b/src/macos.rs new file mode 100644 index 00000000..29078a86 --- /dev/null +++ b/src/macos.rs @@ -0,0 +1,49 @@ +// Copyright 2019 Developers of the Rand project. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Implementation for macOS +extern crate std; + +use crate::{use_file, Error}; +use core::mem; +use core::num::NonZeroU32; +use lazy_static::lazy_static; +use std::io; + +type GetEntropyFn = unsafe extern "C" fn(*mut u8, libc::size_t) -> libc::c_int; + +fn fetch_getentropy() -> Option { + let name = "getentropy\0"; + let addr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr() as *const _) }; + unsafe { mem::transmute(addr) } +} + +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { + lazy_static! { + static ref GETENTROPY_FUNC: Option = fetch_getentropy(); + } + if let Some(fptr) = *GETENTROPY_FUNC { + for chunk in dest.chunks_mut(256) { + let ret = unsafe { fptr(chunk.as_mut_ptr(), chunk.len()) }; + if ret != 0 { + error!("getentropy syscall failed with ret={}", ret); + return Err(io::Error::last_os_error().into()); + } + } + Ok(()) + } else { + // We fallback to reading from /dev/random instead of SecRandomCopyBytes + // to avoid high startup costs and linking the Security framework. + use_file::getrandom_inner(dest) + } +} + +#[inline(always)] +pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> { + None +} From afb9dc671338a45c48d622ab5ee0ddc45f690463 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 28 Jun 2019 15:53:52 -0700 Subject: [PATCH 3/4] Support macOS fallback to /dev/random --- src/lib.rs | 1 + src/use_file.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 5d1b40df..2bd50ce0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -167,6 +167,7 @@ mod error_impls; #[cfg(any( target_os = "android", target_os = "linux", + target_os = "macos", target_os = "solaris", target_os = "illumos", ))] diff --git a/src/use_file.rs b/src/use_file.rs index dc2ed8ff..d7c18a8a 100644 --- a/src/use_file.rs +++ b/src/use_file.rs @@ -25,6 +25,7 @@ const FILE_PATH: &str = "/dev/urandom"; target_os = "dragonfly", target_os = "emscripten", target_os = "haiku", + target_os = "macos", target_os = "solaris", target_os = "illumos" ))] From e4c7b18f6a7ea0d0d75f845a6dbe0ad191097999 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 28 Jun 2019 17:33:06 -0700 Subject: [PATCH 4/4] Update macOS docs --- src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2bd50ce0..9d3e583b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,8 @@ //! |------------------|--------------------------------------------------------- //! | Linux, Android | [`getrandom`][1] system call if available, otherwise [`/dev/urandom`][2] after reading from `/dev/random` once //! | Windows | [`RtlGenRandom`][3] -//! | macOS, iOS | [`SecRandomCopyBytes`][4] +//! | macOS | [`getentropy()`][19] if available, otherise [`/dev/random`][20] (identical to `/dev/urandom`) +//! | iOS | [`SecRandomCopyBytes`][4] //! | FreeBSD | [`kern.arandom`][5] //! | OpenBSD, Bitrig | [`getentropy`][6] //! | NetBSD | [`/dev/urandom`][7] after reading from `/dev/random` once @@ -115,6 +116,8 @@ //! [16]: #support-for-webassembly-and-amsjs //! [17]: https://github.com/CraneStation/wasmtime/blob/master/docs/WASI-api.md#__wasi_random_get //! [18]: https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide +//! [19]: https://www.unix.com/man-page/mojave/2/getentropy/ +//! [20]: https://www.unix.com/man-page/mojave/4/random/ #![doc( html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",