]> git.proxmox.com Git - rustc.git/blame - vendor/getrandom/src/ios.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / vendor / getrandom / src / ios.rs
CommitLineData
f20569fa
XL
1// 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 iOS
10use crate::Error;
11use core::{ffi::c_void, ptr::null};
12
13#[link(name = "Security", kind = "framework")]
14extern "C" {
15 fn SecRandomCopyBytes(rnd: *const c_void, count: usize, bytes: *mut u8) -> i32;
16}
17
18pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
19 // Apple's documentation guarantees kSecRandomDefault is a synonym for NULL.
20 let ret = unsafe { SecRandomCopyBytes(null(), dest.len(), dest.as_mut_ptr()) };
21 if ret == -1 {
22 Err(Error::IOS_SEC_RANDOM)
23 } else {
24 Ok(())
25 }
26}