|
9 | 9 | //! Implementation for SGX using RDRAND instruction |
10 | 10 | use crate::Error; |
11 | 11 | use core::mem; |
12 | | -use core::arch::x86_64::_rdrand64_step; |
| 12 | +use core::arch::x86_64::{__cpuid, _rdrand64_step}; |
13 | 13 | use core::num::NonZeroU32; |
14 | | - |
15 | | -#[cfg(not(target_feature = "rdrand"))] |
16 | | -compile_error!("enable rdrand target feature!"); |
| 14 | +use lazy_static::lazy_static; |
17 | 15 |
|
18 | 16 | // Recommendation from "Intel® Digital Random Number Generator (DRNG) Software |
19 | 17 | // Implementation Guide" - Section 5.2.1 and "Intel® 64 and IA-32 Architectures |
20 | 18 | // Software Developer’s Manual" - Volume 1 - Section 7.3.17.1. |
21 | 19 | const RETRY_LIMIT: usize = 10; |
22 | 20 | const WORD_SIZE: usize = mem::size_of::<u64>(); |
23 | 21 |
|
24 | | -fn rdrand() -> Result<[u8; WORD_SIZE], Error> { |
| 22 | +#[target_feature(enable = "rdrand")] |
| 23 | +unsafe fn rdrand() -> Result<[u8; WORD_SIZE], Error> { |
25 | 24 | for _ in 0..RETRY_LIMIT { |
26 | | - unsafe { |
27 | | - // SAFETY: we've checked RDRAND support, and u64 can have any value. |
28 | | - let mut el = mem::uninitialized(); |
29 | | - if _rdrand64_step(&mut el) == 1 { |
30 | | - return Ok(el.to_ne_bytes()); |
31 | | - } |
32 | | - }; |
| 25 | + let mut el = mem::uninitialized(); |
| 26 | + if _rdrand64_step(&mut el) == 1 { |
| 27 | + return Ok(el.to_ne_bytes()); |
| 28 | + } |
33 | 29 | } |
34 | 30 | error!("RDRAND failed, CPU issue likely"); |
35 | 31 | Err(Error::UNKNOWN) |
36 | 32 | } |
37 | 33 |
|
| 34 | +// "rdrand" target feature requires "+rdrnd" flag, see https://github.com/rust-lang/rust/issues/49653. |
| 35 | +#[cfg(all(target_env = "sgx", not(target_feature = "rdrand")))] |
| 36 | +compile_error!( |
| 37 | + "SGX targets require 'rdrand' target feature. Enable by using -C target-feature=+rdrnd." |
| 38 | +); |
| 39 | + |
| 40 | +// TODO use is_x86_feature_detected!("rdrand") when that works in core. See: |
| 41 | +// https://github.com/rust-lang-nursery/stdsimd/issues/464 |
| 42 | +fn is_rdrand_supported() -> bool { |
| 43 | + if cfg!(target_feature = "rdrand") { |
| 44 | + true |
| 45 | + } else { |
| 46 | + // SAFETY: All x86_64 CPUs support CPUID leaf 1 |
| 47 | + const FLAG: u32 = 1 << 30; |
| 48 | + lazy_static! { |
| 49 | + static ref HAS_RDRAND: bool = unsafe { __cpuid(1).ecx & FLAG != 0 }; |
| 50 | + } |
| 51 | + *HAS_RDRAND |
| 52 | + } |
| 53 | +} |
| 54 | + |
38 | 55 | pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { |
| 56 | + if !is_rdrand_supported() { |
| 57 | + return Err(Error::UNAVAILABLE); |
| 58 | + } |
| 59 | + |
| 60 | + // SAFETY: After this point, rdrand is supported, so calling the rdrand |
| 61 | + // functions is not undefined behavior. |
| 62 | + unsafe { rdrand_exact(dest) } |
| 63 | +} |
| 64 | + |
| 65 | +#[target_feature(enable = "rdrand")] |
| 66 | +unsafe fn rdrand_exact(dest: &mut [u8]) -> Result<(), Error> { |
39 | 67 | // We use chunks_exact_mut instead of chunks_mut as it allows almost all |
40 | 68 | // calls to memcpy to be elided by the compiler. |
41 | 69 | let mut chunks = dest.chunks_exact_mut(WORD_SIZE); |
|
0 commit comments