Skip to content

Commit d44577d

Browse files
Add support for x86_64-fortanix-unknown-sgx target
1 parent 10214c7 commit d44577d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ libc = { version = "0.2.45" }
298298
[target.'cfg(any(target_os = "redox", all(unix, not(any(target_os = "macos", target_os = "ios")))))'.dependencies]
299299
lazy_static = "1.2"
300300

301+
[target.'cfg(target_env = "sgx")'.dependencies]
302+
rand_core = "0.3.0"
303+
rdrand = "0.4.0"
304+
301305
# Keep this in sync with `[dependencies]` in pregenerate_asm/Cargo.toml.
302306
[build-dependencies]
303307
cc = "1.0.26"

src/rand.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ pub trait SecureRandom: sealed::Sealed {
6060
/// On Windows, `fill` is implemented using the platform's API for secure
6161
/// random number generation.
6262
///
63+
/// On SGX, `fill()` is implemented using the `RDRAND` instruction.
64+
///
6365
/// Otherwise, `fill()` is implemented by reading from `/dev/urandom`. (This is
6466
/// something that should be improved for any platform that adds something
6567
/// better.)
@@ -95,8 +97,7 @@ impl sealed::Sealed for SystemRandom {}
9597

9698
#[cfg(all(
9799
feature = "use_heap",
98-
not(any(target_os = "linux", target_os = "macos", target_os = "ios", windows))
99-
))]
100+
not(any(target_os = "linux", target_os = "macos", target_os = "ios", windows, target_env = "sgx"))))]
100101
use self::urandom::fill as fill_impl;
101102

102103
#[cfg(any(
@@ -110,6 +111,10 @@ use self::sysrand_or_urandom::fill as fill_impl;
110111

111112
#[cfg(any(target_os = "macos", target_os = "ios"))]
112113
use self::darwin::fill as fill_impl;
114+
115+
#[cfg(target_env = "sgx")]
116+
use self::rdrandom::fill as fill_impl;
117+
113118
use crate::sealed;
114119

115120
#[cfg(target_os = "linux")]
@@ -279,6 +284,19 @@ mod darwin {
279284
}
280285
}
281286

287+
#[cfg(target_env = "sgx")]
288+
mod rdrandom {
289+
use crate::error;
290+
use rdrand::RdRand;
291+
use rand_core::RngCore;
292+
293+
pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> {
294+
let mut rng = RdRand::new().map_err(|_| error::Unspecified)?;
295+
rng.try_fill_bytes(dest).map_err(|_| error::Unspecified)?;
296+
Ok(())
297+
}
298+
}
299+
282300
#[cfg(test)]
283301
mod tests {
284302
use crate::rand::{self, SecureRandom};

0 commit comments

Comments
 (0)