std/sys/random/
apple.rs

1//! Random data on Apple platforms.
2//!
3//! `CCRandomGenerateBytes` calls into `CCRandomCopyBytes` with `kCCRandomDefault`.
4//! `CCRandomCopyBytes` manages a CSPRNG which is seeded from the kernel's CSPRNG.
5//! We use `CCRandomGenerateBytes` instead of `SecCopyBytes` because it is accessible via
6//! `libSystem` (libc) while the other needs to link to `Security.framework`.
7//!
8//! Note that technically, `arc4random_buf` is available as well, but that calls
9//! into the same system service anyway, and `CCRandomGenerateBytes` has been
10//! proven to be App Store-compatible.
11
12pub fn fill_bytes(bytes: &mut [u8]) {
13    let ret = unsafe { libc::CCRandomGenerateBytes(bytes.as_mut_ptr().cast(), bytes.len()) };
14    assert_eq!(ret, libc::kCCSuccess, "failed to generate random data");
15}