Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OSX Implementation of SecRandomCopy
- Loading branch information
1 parent
becd8e3
commit 04a4249
Showing
4 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#if !TARGET_OS_IPHONE | ||
#import <stdint.h> | ||
#import <sys/types.h> | ||
typedef const struct __SecRandom * SecRandomRef; | ||
extern const SecRandomRef kSecRandomDefault; | ||
int SecRandomCopyBytes( SecRandomRef rnd, size_t count, uint8_t *bytes ); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#if !TARGET_OS_IPHONE | ||
|
||
#import <unistd.h> | ||
#import <errno.h> | ||
#import <sys/fcntl.h> | ||
#import "OSX_SecRandom.h" | ||
|
||
const SecRandomRef kSecRandomDefault = NULL; | ||
|
||
int SecRandomCopyBytes( SecRandomRef rnd, size_t count, uint8_t *bytes ) | ||
{ | ||
if( count == 0 ) | ||
return 0; | ||
|
||
int fd = open("/dev/urandom",O_RDONLY); | ||
if( fd < 0 ) { | ||
return -1; | ||
} | ||
|
||
size_t bytesRead; | ||
uint8_t *p = bytes; | ||
|
||
do { | ||
bytesRead = read( fd, p, count - ( p -bytes ) ); | ||
if( bytesRead > 0 ) { | ||
p += bytesRead; | ||
} | ||
} while( bytesRead > 0 || ( bytesRead < 0 && errno == EINTR ) ); | ||
close( fd ); | ||
|
||
return bytesRead < 0 ? -1 : 0; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters