| Joel Galenson | 4be0c6d | 2020-07-07 13:20:14 -0700 | [diff] [blame] | 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 |
| 10 | use crate::{error::SEC_RANDOM_FAILED, Error}; |
| 11 | |
| 12 | // TODO: Make extern once extern_types feature is stabilized. See: |
| 13 | // https://github.com/rust-lang/rust/issues/43467 |
| 14 | #[repr(C)] |
| 15 | struct SecRandom([u8; 0]); |
| 16 | |
| 17 | #[link(name = "Security", kind = "framework")] |
| 18 | extern "C" { |
| 19 | static kSecRandomDefault: *const SecRandom; |
| 20 | |
| 21 | fn SecRandomCopyBytes(rnd: *const SecRandom, count: usize, bytes: *mut u8) -> i32; |
| 22 | } |
| 23 | |
| 24 | pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { |
| 25 | let ret = unsafe { SecRandomCopyBytes(kSecRandomDefault, dest.len(), dest.as_mut_ptr()) }; |
| 26 | if ret == -1 { |
| 27 | Err(SEC_RANDOM_FAILED) |
| 28 | } else { |
| 29 | Ok(()) |
| 30 | } |
| 31 | } |