Skip to content

Commit

Permalink
Add support for fuchsia's CPRNG source
Browse files Browse the repository at this point in the history
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
  • Loading branch information
erickt committed Jan 31, 2019
1 parent 899efbe commit 462a875
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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")]
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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};
Expand Down

0 comments on commit 462a875

Please sign in to comment.