Skip to content

Commit

Permalink
ios: Fix Error checking for SecRandomCopyBytes (#244)
Browse files Browse the repository at this point in the history
Apple's documentation for SecRandomCopyBytes says that errSecSuccess is
returned on success, and all other values indicate failure.
  https://developer.apple.com/documentation/security/1399291-secrandomcopybytes

The SecBase.h header also clearly establishes that `errSecSuccess = 0`:
  https://opensource.apple.com/source/Security/Security-55471/sec/Security/SecBase.h.auto.html

Fixes #243

Signed-off-by: Joe Richey <joerichey@google.com>
  • Loading branch information
josephlr authored Jan 21, 2022
1 parent e6e7dd6 commit 21e03e8
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ extern "C" {
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
// Apple's documentation guarantees kSecRandomDefault is a synonym for NULL.
let ret = unsafe { SecRandomCopyBytes(null(), dest.len(), dest.as_mut_ptr()) };
if ret == -1 {
// errSecSuccess (from SecBase.h) is always zero.
if ret != 0 {
Err(Error::IOS_SEC_RANDOM)
} else {
Ok(())
Expand Down

0 comments on commit 21e03e8

Please sign in to comment.