about summary refs log tree commit diff
path: root/library/std/src/sys/random/apple.rs
diff options
context:
space:
mode:
Diffstat (limited to 'library/std/src/sys/random/apple.rs')
-rw-r--r--library/std/src/sys/random/apple.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/library/std/src/sys/random/apple.rs b/library/std/src/sys/random/apple.rs
new file mode 100644
index 00000000000..417198c9d85
--- /dev/null
+++ b/library/std/src/sys/random/apple.rs
@@ -0,0 +1,15 @@
+//! Random data on Apple platforms.
+//!
+//! `CCRandomGenerateBytes` calls into `CCRandomCopyBytes` with `kCCRandomDefault`.
+//! `CCRandomCopyBytes` manages a CSPRNG which is seeded from the kernel's CSPRNG.
+//! We use `CCRandomGenerateBytes` instead of `SecCopyBytes` because it is accessible via
+//! `libSystem` (libc) while the other needs to link to `Security.framework`.
+//!
+//! Note that technically, `arc4random_buf` is available as well, but that calls
+//! into the same system service anyway, and `CCRandomGenerateBytes` has been
+//! proven to be App Store-compatible.
+
+pub fn fill_bytes(bytes: &mut [u8]) {
+    let ret = unsafe { libc::CCRandomGenerateBytes(bytes.as_mut_ptr().cast(), bytes.len()) };
+    assert_eq!(ret, libc::kCCSuccess, "failed to generate random data");
+}