| 1 |
//! Implementation for VxWorks |
| 2 |
use crate::Error; |
| 3 |
use core::{ |
| 4 |
cmp::Ordering::{Equal, Greater, Less}, |
| 5 |
mem::MaybeUninit, |
| 6 |
sync::atomic::{AtomicBool, Ordering::Relaxed}, |
| 7 |
}; |
| 8 |
|
| 9 |
#[path = "../util_libc.rs"] |
| 10 |
mod util_libc; |
| 11 |
|
| 12 |
pub use crate::util::{inner_u32, inner_u64}; |
| 13 |
|
| 14 |
static RNG_INIT: AtomicBool = AtomicBool::new(false); |
| 15 |
|
| 16 |
#[cold] |
| 17 |
fn init() -> Result<(), Error> { |
| 18 |
let ret = unsafe { libc::randSecure() }; |
| 19 |
match ret.cmp(&0) { |
| 20 |
Greater => RNG_INIT.store(true, Relaxed), |
| 21 |
Equal => unsafe { |
| 22 |
libc::usleep(10); |
| 23 |
}, |
| 24 |
Less => return Err(Error::VXWORKS_RAND_SECURE), |
| 25 |
} |
| 26 |
Ok(()) |
| 27 |
} |
| 28 |
|
| 29 |
#[inline] |
| 30 |
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
| 31 |
while !RNG_INIT.load(Relaxed) { |
| 32 |
init()?; |
| 33 |
} |
| 34 |
|
| 35 |
// Prevent overflow of i32 |
| 36 |
let chunk_size = usize::try_from(i32::MAX).expect("VxWorks does not support 16-bit targets"); |
| 37 |
for chunk in dest.chunks_mut(chunk_size) { |
| 38 |
let chunk_len: libc::c_int = chunk |
| 39 |
.len() |
| 40 |
.try_into() |
| 41 |
.expect("chunk size is bounded by i32::MAX"); |
| 42 |
let p: *mut libc::c_uchar = chunk.as_mut_ptr().cast(); |
| 43 |
let ret = unsafe { libc::randABytes(p, chunk_len) }; |
| 44 |
if ret != 0 { |
| 45 |
return Err(util_libc::last_os_error()); |
| 46 |
} |
| 47 |
} |
| 48 |
Ok(()) |
| 49 |
} |
| 50 |
|
| 51 |
impl Error { |
| 52 |
/// On VxWorks, call to `randSecure` failed (random number generator is not yet initialized). |
| 53 |
pub(crate) const VXWORKS_RAND_SECURE: Error = Self::new_internal(10); |
| 54 |
} |