|
7 | 7 | // except according to those terms.
|
8 | 8 |
|
9 | 9 | //! Implementation for SGX using RDRAND instruction
|
10 |
| -#[cfg(not(target_feature = "rdrand"))] |
11 |
| -use crate::util::LazyBool; |
12 | 10 | use crate::Error;
|
13 |
| -use core::arch::x86_64::_rdrand64_step; |
14 | 11 | use core::mem;
|
15 | 12 |
|
| 13 | +cfg_if! { |
| 14 | + if #[cfg(target_arch = "x86_64")] { |
| 15 | + use core::arch::x86_64 as arch; |
| 16 | + use arch::_rdrand64_step as rdrand_step; |
| 17 | + } else if #[cfg(target_arch = "x86")] { |
| 18 | + use core::arch::x86 as arch; |
| 19 | + use arch::_rdrand32_step as rdrand_step; |
| 20 | + } |
| 21 | +} |
| 22 | + |
16 | 23 | // Recommendation from "Intel® Digital Random Number Generator (DRNG) Software
|
17 | 24 | // Implementation Guide" - Section 5.2.1 and "Intel® 64 and IA-32 Architectures
|
18 | 25 | // Software Developer’s Manual" - Volume 1 - Section 7.3.17.1.
|
19 | 26 | const RETRY_LIMIT: usize = 10;
|
20 |
| -const WORD_SIZE: usize = mem::size_of::<u64>(); |
| 27 | +const WORD_SIZE: usize = mem::size_of::<usize>(); |
21 | 28 |
|
22 | 29 | #[target_feature(enable = "rdrand")]
|
23 | 30 | unsafe fn rdrand() -> Result<[u8; WORD_SIZE], Error> {
|
24 | 31 | for _ in 0..RETRY_LIMIT {
|
25 | 32 | let mut el = mem::zeroed();
|
26 |
| - if _rdrand64_step(&mut el) == 1 { |
| 33 | + if rdrand_step(&mut el) == 1 { |
27 | 34 | // AMD CPUs from families 14h to 16h (pre Ryzen) sometimes fail to
|
28 | 35 | // set CF on bogus random data, so we check these values explicitly.
|
29 | 36 | // See https://github.com/systemd/systemd/issues/11810#issuecomment-489727505
|
@@ -53,11 +60,13 @@ fn is_rdrand_supported() -> bool {
|
53 | 60 | // https://github.com/rust-lang-nursery/stdsimd/issues/464
|
54 | 61 | #[cfg(not(target_feature = "rdrand"))]
|
55 | 62 | fn is_rdrand_supported() -> bool {
|
56 |
| - use core::arch::x86_64::__cpuid; |
57 |
| - // SAFETY: All x86_64 CPUs support CPUID leaf 1 |
| 63 | + use crate::util::LazyBool; |
| 64 | + |
| 65 | + // SAFETY: All Rust x86 targets are new enough to have CPUID, and if CPUID |
| 66 | + // is supported, CPUID leaf 1 is always supported. |
58 | 67 | const FLAG: u32 = 1 << 30;
|
59 | 68 | static HAS_RDRAND: LazyBool = LazyBool::new();
|
60 |
| - HAS_RDRAND.unsync_init(|| unsafe { (__cpuid(1).ecx & FLAG) != 0 }) |
| 69 | + HAS_RDRAND.unsync_init(|| unsafe { (arch::__cpuid(1).ecx & FLAG) != 0 }) |
61 | 70 | }
|
62 | 71 |
|
63 | 72 | pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
|
|
0 commit comments