blob: 7ca9d6bfddacf60d9bfb51414336e6ae2b56b566 [file] [log] [blame]
Joel Galenson4be0c6d2020-07-07 13:20:14 -07001//! Implementation for VxWorks
ThiƩbaud Weksteen9791b302021-03-03 16:30:20 +01002use crate::{util_libc::last_os_error, Error};
Jeff Vander Stoep1872b3e2024-02-01 19:43:14 +01003use core::{
4 mem::MaybeUninit,
5 sync::atomic::{AtomicBool, Ordering::Relaxed},
6};
Joel Galenson4be0c6d2020-07-07 13:20:14 -07007
Jeff Vander Stoep1872b3e2024-02-01 19:43:14 +01008pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
Joel Galenson4be0c6d2020-07-07 13:20:14 -07009 static RNG_INIT: AtomicBool = AtomicBool::new(false);
10 while !RNG_INIT.load(Relaxed) {
11 let ret = unsafe { libc::randSecure() };
12 if ret < 0 {
ThiƩbaud Weksteen9791b302021-03-03 16:30:20 +010013 return Err(Error::VXWORKS_RAND_SECURE);
Joel Galenson4be0c6d2020-07-07 13:20:14 -070014 } else if ret > 0 {
15 RNG_INIT.store(true, Relaxed);
16 break;
17 }
18 unsafe { libc::usleep(10) };
19 }
20
21 // Prevent overflow of i32
22 for chunk in dest.chunks_mut(i32::max_value() as usize) {
Jeff Vander Stoep1872b3e2024-02-01 19:43:14 +010023 let ret = unsafe { libc::randABytes(chunk.as_mut_ptr() as *mut u8, chunk.len() as i32) };
Joel Galenson4be0c6d2020-07-07 13:20:14 -070024 if ret != 0 {
25 return Err(last_os_error());
26 }
27 }
28 Ok(())
29}