Skip to content

Commit abd3819

Browse files
Add support for x86_64-fortanix-unknown-sgx target
1 parent aa562a1 commit abd3819

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ libc = { version = "0.2.34" }
286286
[target.'cfg(any(target_os = "redox", all(unix, not(any(target_os = "macos", target_os = "ios")))))'.dependencies]
287287
lazy_static = "1.2"
288288

289+
[target.'cfg(target_env = "sgx")'.dependencies]
290+
rdrand = "0.3.0"
291+
289292
# Keep this in sync with `[dependencies]` in pregenerate_asm/Cargo.toml.
290293
[build-dependencies]
291294
# we do not use the gcc parallel feature because we do the

src/rand.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl SecureRandom for SystemRandom {
9393

9494
impl private::Sealed for SystemRandom {}
9595

96-
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "ios", windows)))]
96+
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "ios", windows, target_env = "sgx")))]
9797
use self::urandom::fill as fill_impl;
9898

9999
#[cfg(any(
@@ -107,6 +107,9 @@ use self::sysrand_or_urandom::fill as fill_impl;
107107

108108
#[cfg(any(target_os = "macos", target_os = "ios"))]
109109
use self::darwin::fill as fill_impl;
110+
111+
#[cfg(target_env = "sgx")]
112+
use self::rdrandom::fill as fill_impl;
110113
use crate::private;
111114

112115
#[cfg(target_os = "linux")]
@@ -275,6 +278,24 @@ mod darwin {
275278
}
276279
}
277280

281+
#[cfg(target_env = "sgx")]
282+
mod rdrandom {
283+
use rdrand::RdRand;
284+
use crate::error;
285+
use crate::endian::LittleEndian;
286+
287+
pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> {
288+
let rng = RdRand::new().map_err(|_| error::Unspecified)?;
289+
for dst_chunk in dest.chunks_mut(8) {
290+
let src_num = rng.try_next_u64().ok_or(error::Unspecified)?;
291+
let temp = LittleEndian::from(src_num);
292+
let src_chunk = temp.as_ref();
293+
dst_chunk.copy_from_slice(&src_chunk[..dst_chunk.len()]);
294+
}
295+
Ok(())
296+
}
297+
}
298+
278299
#[cfg(test)]
279300
mod tests {
280301
use crate::rand::{self, SecureRandom};

0 commit comments

Comments
 (0)