blob: a2fe52ada8513ead53dc43f3adb5a74f8568c303 [file] [log] [blame]
Joel Galenson4be0c6d2020-07-07 13:20:14 -07001// Copyright 2018 Developers of the Rand project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Implementation for VxWorks
10use crate::error::{Error, RAND_SECURE_FATAL};
11use crate::util_libc::last_os_error;
12use core::sync::atomic::{AtomicBool, Ordering::Relaxed};
13
14pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
15 static RNG_INIT: AtomicBool = AtomicBool::new(false);
16 while !RNG_INIT.load(Relaxed) {
17 let ret = unsafe { libc::randSecure() };
18 if ret < 0 {
19 return Err(RAND_SECURE_FATAL);
20 } else if ret > 0 {
21 RNG_INIT.store(true, Relaxed);
22 break;
23 }
24 unsafe { libc::usleep(10) };
25 }
26
27 // Prevent overflow of i32
28 for chunk in dest.chunks_mut(i32::max_value() as usize) {
29 let ret = unsafe { libc::randABytes(chunk.as_mut_ptr(), chunk.len() as i32) };
30 if ret != 0 {
31 return Err(last_os_error());
32 }
33 }
34 Ok(())
35}