1 //! Implementation for iOS, tvOS, and watchOS where `getentropy` is unavailable.
2 use crate::Error;
3 use core::{ffi::c_void, mem::MaybeUninit};
4 
5 // libsystem contains the libc of Darwin, and every binary ends up linked against it either way. This
6 // makes it a more lightweight choice compared to `Security.framework`.
7 extern "C" {
8     // This RNG uses a thread-local CSPRNG to provide data, which is seeded by the operating system's root CSPRNG.
9     // Its the best option after `getentropy` on modern Darwin-based platforms that also avoids the
10     // high startup costs and linking of Security.framework.
11     //
12     // While its just an implementation detail, `Security.framework` just calls into this anyway.
CCRandomGenerateBytes(bytes: *mut c_void, size: usize) -> i3213     fn CCRandomGenerateBytes(bytes: *mut c_void, size: usize) -> i32;
14 }
15 
getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>16 pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
17     let ret = unsafe { CCRandomGenerateBytes(dest.as_mut_ptr() as *mut c_void, dest.len()) };
18     // kCCSuccess (from CommonCryptoError.h) is always zero.
19     if ret != 0 {
20         Err(Error::IOS_SEC_RANDOM)
21     } else {
22         Ok(())
23     }
24 }
25