| 1 |
//! RDRAND backend for x86(-64) targets |
| 2 |
use crate::{util::slice_as_uninit, Error}; |
| 3 |
use core::mem::{size_of, MaybeUninit}; |
| 4 |
|
| 5 |
#[path = "../lazy.rs"] |
| 6 |
mod lazy; |
| 7 |
|
| 8 |
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))] |
| 9 |
compile_error!("`rdrand` backend can be enabled only for x86 and x86-64 targets!"); |
| 10 |
|
| 11 |
cfg_if! { |
| 12 |
if #[cfg(target_arch = "x86_64")] { |
| 13 |
use core::arch::x86_64 as arch; |
| 14 |
use arch::_rdrand64_step as rdrand_step; |
| 15 |
type Word = u64; |
| 16 |
} else if #[cfg(target_arch = "x86")] { |
| 17 |
use core::arch::x86 as arch; |
| 18 |
use arch::_rdrand32_step as rdrand_step; |
| 19 |
type Word = u32; |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
static RDRAND_GOOD: lazy::LazyBool = lazy::LazyBool::new(); |
| 24 |
|
| 25 |
// Recommendation from "Intel® Digital Random Number Generator (DRNG) Software |
| 26 |
// Implementation Guide" - Section 5.2.1 and "Intel® 64 and IA-32 Architectures |
| 27 |
// Software Developer’s Manual" - Volume 1 - Section 7.3.17.1. |
| 28 |
const RETRY_LIMIT: usize = 10; |
| 29 |
|
| 30 |
#[target_feature(enable = "rdrand")] |
| 31 |
unsafe fn rdrand() -> Option<Word> { |
| 32 |
for _ in 0..RETRY_LIMIT { |
| 33 |
let mut val = 0; |
| 34 |
if rdrand_step(&mut val) == 1 { |
| 35 |
return Some(val); |
| 36 |
} |
| 37 |
} |
| 38 |
None |
| 39 |
} |
| 40 |
|
| 41 |
// "rdrand" target feature requires "+rdrand" flag, see https://github.com/rust-lang/rust/issues/49653. |
| 42 |
#[cfg(all(target_env = "sgx", not(target_feature = "rdrand")))] |
| 43 |
compile_error!( |
| 44 |
"SGX targets require 'rdrand' target feature. Enable by using -C target-feature=+rdrand." |
| 45 |
); |
| 46 |
|
| 47 |
// Run a small self-test to make sure we aren't repeating values |
| 48 |
// Adapted from Linux's test in arch/x86/kernel/cpu/rdrand.c |
| 49 |
// Fails with probability < 2^(-90) on 32-bit systems |
| 50 |
#[target_feature(enable = "rdrand")] |
| 51 |
unsafe fn self_test() -> bool { |
| 52 |
// On AMD, RDRAND returns 0xFF...FF on failure, count it as a collision. |
| 53 |
let mut prev = !0; // TODO(MSRV 1.43): Move to usize::MAX |
| 54 |
let mut fails = 0; |
| 55 |
for _ in 0..8 { |
| 56 |
match rdrand() { |
| 57 |
Some(val) if val == prev => fails += 1, |
| 58 |
Some(val) => prev = val, |
| 59 |
None => return false, |
| 60 |
}; |
| 61 |
} |
| 62 |
fails <= 2 |
| 63 |
} |
| 64 |
|
| 65 |
fn is_rdrand_good() -> bool { |
| 66 |
#[cfg(not(target_feature = "rdrand"))] |
| 67 |
{ |
| 68 |
// SAFETY: All Rust x86 targets are new enough to have CPUID, and we |
| 69 |
// check that leaf 1 is supported before using it. |
| 70 |
let cpuid0 = unsafe { arch::__cpuid(0) }; |
| 71 |
if cpuid0.eax < 1 { |
| 72 |
return false; |
| 73 |
} |
| 74 |
let cpuid1 = unsafe { arch::__cpuid(1) }; |
| 75 |
|
| 76 |
let vendor_id = [ |
| 77 |
cpuid0.ebx.to_le_bytes(), |
| 78 |
cpuid0.edx.to_le_bytes(), |
| 79 |
cpuid0.ecx.to_le_bytes(), |
| 80 |
]; |
| 81 |
if vendor_id == [*b"Auth", *b"enti", *b"cAMD"] { |
| 82 |
let mut family = (cpuid1.eax >> 8) & 0xF; |
| 83 |
if family == 0xF { |
| 84 |
family += (cpuid1.eax >> 20) & 0xFF; |
| 85 |
} |
| 86 |
// AMD CPUs families before 17h (Zen) sometimes fail to set CF when |
| 87 |
// RDRAND fails after suspend. Don't use RDRAND on those families. |
| 88 |
// See https://bugzilla.redhat.com/show_bug.cgi?id=1150286 |
| 89 |
if family < 0x17 { |
| 90 |
return false; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
const RDRAND_FLAG: u32 = 1 << 30; |
| 95 |
if cpuid1.ecx & RDRAND_FLAG == 0 { |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// SAFETY: We have already checked that rdrand is available. |
| 101 |
unsafe { self_test() } |
| 102 |
} |
| 103 |
|
| 104 |
// TODO: make this function safe when we have feature(target_feature_11) |
| 105 |
#[target_feature(enable = "rdrand")] |
| 106 |
unsafe fn rdrand_exact(dest: &mut [MaybeUninit<u8>]) -> Option<()> { |
| 107 |
// We use chunks_exact_mut instead of chunks_mut as it allows almost all |
| 108 |
// calls to memcpy to be elided by the compiler. |
| 109 |
let mut chunks = dest.chunks_exact_mut(size_of::<Word>()); |
| 110 |
for chunk in chunks.by_ref() { |
| 111 |
let src = rdrand()?.to_ne_bytes(); |
| 112 |
chunk.copy_from_slice(slice_as_uninit(&src)); |
| 113 |
} |
| 114 |
|
| 115 |
let tail = chunks.into_remainder(); |
| 116 |
let n = tail.len(); |
| 117 |
if n > 0 { |
| 118 |
let src = rdrand()?.to_ne_bytes(); |
| 119 |
tail.copy_from_slice(slice_as_uninit(&src[..n])); |
| 120 |
} |
| 121 |
Some(()) |
| 122 |
} |
| 123 |
|
| 124 |
#[cfg(target_arch = "x86_64")] |
| 125 |
#[target_feature(enable = "rdrand")] |
| 126 |
unsafe fn rdrand_u32() -> Option<u32> { |
| 127 |
rdrand().map(crate::util::truncate) |
| 128 |
} |
| 129 |
|
| 130 |
#[cfg(target_arch = "x86_64")] |
| 131 |
#[target_feature(enable = "rdrand")] |
| 132 |
unsafe fn rdrand_u64() -> Option<u64> { |
| 133 |
rdrand() |
| 134 |
} |
| 135 |
|
| 136 |
#[cfg(target_arch = "x86")] |
| 137 |
#[target_feature(enable = "rdrand")] |
| 138 |
unsafe fn rdrand_u32() -> Option<u32> { |
| 139 |
rdrand() |
| 140 |
} |
| 141 |
|
| 142 |
#[cfg(target_arch = "x86")] |
| 143 |
#[target_feature(enable = "rdrand")] |
| 144 |
unsafe fn rdrand_u64() -> Option<u64> { |
| 145 |
let a = rdrand()?; |
| 146 |
let b = rdrand()?; |
| 147 |
Some((u64::from(a) << 32) | u64::from(b)) |
| 148 |
} |
| 149 |
|
| 150 |
#[inline] |
| 151 |
pub fn inner_u32() -> Result<u32, Error> { |
| 152 |
if !RDRAND_GOOD.unsync_init(is_rdrand_good) { |
| 153 |
return Err(Error::NO_RDRAND); |
| 154 |
} |
| 155 |
// SAFETY: After this point, we know rdrand is supported. |
| 156 |
unsafe { rdrand_u32() }.ok_or(Error::FAILED_RDRAND) |
| 157 |
} |
| 158 |
|
| 159 |
#[inline] |
| 160 |
pub fn inner_u64() -> Result<u64, Error> { |
| 161 |
if !RDRAND_GOOD.unsync_init(is_rdrand_good) { |
| 162 |
return Err(Error::NO_RDRAND); |
| 163 |
} |
| 164 |
// SAFETY: After this point, we know rdrand is supported. |
| 165 |
unsafe { rdrand_u64() }.ok_or(Error::FAILED_RDRAND) |
| 166 |
} |
| 167 |
|
| 168 |
#[inline] |
| 169 |
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
| 170 |
if !RDRAND_GOOD.unsync_init(is_rdrand_good) { |
| 171 |
return Err(Error::NO_RDRAND); |
| 172 |
} |
| 173 |
// SAFETY: After this point, we know rdrand is supported. |
| 174 |
unsafe { rdrand_exact(dest) }.ok_or(Error::FAILED_RDRAND) |
| 175 |
} |
| 176 |
|
| 177 |
impl Error { |
| 178 |
/// RDRAND instruction failed due to a hardware issue. |
| 179 |
pub(crate) const FAILED_RDRAND: Error = Self::new_internal(10); |
| 180 |
/// RDRAND instruction unsupported on this target. |
| 181 |
pub(crate) const NO_RDRAND: Error = Self::new_internal(11); |
| 182 |
} |