Skip to content

Commit 138d903

Browse files
committed
ios: Don't use SecRandom type
The type of the `rnd` parameter is SecRandomRef which is an alias for an "Opaque Pointer". This is better represented in Rust as a c_void pointer. We also know that kSecRandomDefault is NULL, so we can simplify the code. https://developer.apple.com/documentation/security/1399291-secrandomcopybytes https://developer.apple.com/documentation/security/secrandomref https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/using_imported_c_functions_in_swift
1 parent afcfcd1 commit 138d903

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

src/ios.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,16 @@
88

99
//! Implementation for iOS
1010
use crate::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]);
11+
use core::{ffi::c_void, ptr::null};
1612

1713
#[link(name = "Security", kind = "framework")]
1814
extern "C" {
19-
static kSecRandomDefault: *const SecRandom;
20-
21-
fn SecRandomCopyBytes(rnd: *const SecRandom, count: usize, bytes: *mut u8) -> i32;
15+
fn SecRandomCopyBytes(rnd: *const c_void, count: usize, bytes: *mut u8) -> i32;
2216
}
2317

2418
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
25-
let ret = unsafe { SecRandomCopyBytes(kSecRandomDefault, dest.len(), dest.as_mut_ptr()) };
19+
// Apple's documentation guarantees kSecRandomDefault is a synonym for NULL.
20+
let ret = unsafe { SecRandomCopyBytes(null(), dest.len(), dest.as_mut_ptr()) };
2621
if ret == -1 {
2722
Err(Error::IOS_SEC_RANDOM)
2823
} else {

0 commit comments

Comments
 (0)