From 462a875a30f86fac796a58b0d086f65c326573d2 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 16 Jan 2019 21:22:35 -0800 Subject: [PATCH] Add support for fuchsia's CPRNG source This is another attempt for ring to support fuchsia, by directly calling `zx_cprng_draw` to generate random byte strings. This avoids having to pull in an extra dependency (which #634 did). With this change, all the ring tests pass on fuchsia. I agree to license my contributions to each file under the terms given at the top of each file I changed. Closes #428 --- src/rand.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/rand.rs b/src/rand.rs index 4912b8d718..f140b78701 100644 --- a/src/rand.rs +++ b/src/rand.rs @@ -95,7 +95,13 @@ impl sealed::Sealed for SystemRandom {} #[cfg(all( feature = "use_heap", - not(any(target_os = "linux", target_os = "macos", target_os = "ios", windows)) + not(any( + target_os = "linux", + target_os = "macos", + target_os = "ios", + target_os = "fuchsia", + windows + )) ))] use self::urandom::fill as fill_impl; @@ -110,6 +116,10 @@ use self::sysrand_or_urandom::fill as fill_impl; #[cfg(any(target_os = "macos", target_os = "ios"))] use self::darwin::fill as fill_impl; + +#[cfg(any(target_os = "fuchsia"))] +use self::fuchsia::fill as fill_impl; + use crate::sealed; #[cfg(target_os = "linux")] @@ -188,7 +198,8 @@ mod sysrand { feature = "use_heap", any(target_os = "redox", unix), not(any(target_os = "macos", target_os = "ios")), - not(all(target_os = "linux", not(feature = "dev_urandom_fallback"))) + not(all(target_os = "linux", not(feature = "dev_urandom_fallback"))), + not(any(target_os = "fuchsia")), ))] mod urandom { use crate::error; @@ -279,6 +290,21 @@ mod darwin { } } +#[cfg(any(target_os = "fuchsia"))] +mod fuchsia { + use crate::error; + + pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> { + unsafe { zx_cprng_draw(dest.as_mut_ptr(), dest.len()); } + Ok(()) + } + + #[link(name = "zircon")] + extern "C" { + fn zx_cprng_draw(buffer: *mut u8, length: usize); + } +} + #[cfg(test)] mod tests { use crate::rand::{self, SecureRandom};