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 24, 2019
1 parent abbb616 commit 18f844b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ libc = { version = "0.2.45" }
[target.'cfg(any(target_os = "redox", all(unix, not(any(target_os = "macos", target_os = "ios")))))'.dependencies]
lazy_static = "1.2"

[target.'cfg(target_os = "fuchsia")'.dependencies]
fuchsia-cprng = "0.1.0"

# Keep this in sync with `[dependencies]` in pregenerate_asm/Cargo.toml.
[build-dependencies]
cc = "1.0.26"
Expand Down
25 changes: 23 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,16 @@ mod darwin {
}
}

#[cfg(any(target_os = "fuchsia"))]
mod fuchsia {
use crate::error;

pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> {
fuchsia_cprng::cprng_draw(dest);
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::rand::{self, SecureRandom};
Expand Down

0 comments on commit 18f844b

Please sign in to comment.