Skip to content

Commit

Permalink
Add OSX Implementation of SecRandomCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
PureAbstract committed Jun 8, 2012
1 parent becd8e3 commit 04a4249
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Classes/NSData+AES.m
Expand Up @@ -10,6 +10,10 @@
#import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonDigest.h>

#if !TARGET_OS_IPHONE
#import "OSX_SecRandom.h"
#endif

@implementation NSData (AES)
-(NSMutableData *)sha256
{
Expand Down
7 changes: 7 additions & 0 deletions OSX_Support/OSX_SecRandom.h
@@ -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
33 changes: 33 additions & 0 deletions OSX_Support/OSX_SecRandom.m
@@ -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
14 changes: 14 additions & 0 deletions PWStore.xcodeproj/project.pbxproj
Expand Up @@ -42,6 +42,7 @@
E3994FE1157AAA5E001CE6D3 /* PWItem+StringExport.m in Sources */ = {isa = PBXBuildFile; fileRef = E3994FE0157AAA5E001CE6D3 /* PWItem+StringExport.m */; };
E3995065157AB505001CE6D3 /* PWData+StringExport.m in Sources */ = {isa = PBXBuildFile; fileRef = E3995064157AB505001CE6D3 /* PWData+StringExport.m */; };
E3995284157CEAE7001CE6D3 /* libxml2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = E3995283157CEAE7001CE6D3 /* libxml2.dylib */; };
E39AA44D1582928B00A93BCE /* OSX_SecRandom.m in Sources */ = {isa = PBXBuildFile; fileRef = E39AA44C1582928B00A93BCE /* OSX_SecRandom.m */; };
E3C91235157D3ACC0055AAD6 /* SyncViewController+TestDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = E3C91234157D3ACC0055AAD6 /* SyncViewController+TestDriver.m */; };
E3C912F6157D4A040055AAD6 /* XmlDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = E3C912F5157D4A040055AAD6 /* XmlDocument.m */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -106,6 +107,8 @@
E3995063157AB505001CE6D3 /* PWData+StringExport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "PWData+StringExport.h"; sourceTree = "<group>"; };
E3995064157AB505001CE6D3 /* PWData+StringExport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "PWData+StringExport.m"; sourceTree = "<group>"; };
E3995283157CEAE7001CE6D3 /* libxml2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libxml2.dylib; path = usr/lib/libxml2.dylib; sourceTree = SDKROOT; };
E39AA44B1582928B00A93BCE /* OSX_SecRandom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OSX_SecRandom.h; path = OSX_Support/OSX_SecRandom.h; sourceTree = "<group>"; };
E39AA44C1582928B00A93BCE /* OSX_SecRandom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OSX_SecRandom.m; path = OSX_Support/OSX_SecRandom.m; sourceTree = "<group>"; };
E3C91233157D3ACC0055AAD6 /* SyncViewController+TestDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "SyncViewController+TestDriver.h"; sourceTree = "<group>"; };
E3C91234157D3ACC0055AAD6 /* SyncViewController+TestDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "SyncViewController+TestDriver.m"; sourceTree = "<group>"; };
E3C912F4157D4A040055AAD6 /* XmlDocument.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XmlDocument.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -164,6 +167,7 @@
29B97315FDCFA39411CA2CEA /* Other Sources */ = {
isa = PBXGroup;
children = (
E39AA44315828C5E00A93BCE /* OSX Support */,
28A0AAE50D9B0CCF005BE974 /* PWStore_Prefix.pch */,
29B97316FDCFA39411CA2CEA /* main.m */,
);
Expand Down Expand Up @@ -293,6 +297,15 @@
name = "User Interface";
sourceTree = "<group>";
};
E39AA44315828C5E00A93BCE /* OSX Support */ = {
isa = PBXGroup;
children = (
E39AA44B1582928B00A93BCE /* OSX_SecRandom.h */,
E39AA44C1582928B00A93BCE /* OSX_SecRandom.m */,
);
name = "OSX Support";
sourceTree = "<group>";
};
E3C9125E157D3C710055AAD6 /* Xml Parser */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -394,6 +407,7 @@
E3995065157AB505001CE6D3 /* PWData+StringExport.m in Sources */,
E3C91235157D3ACC0055AAD6 /* SyncViewController+TestDriver.m in Sources */,
E3C912F6157D4A040055AAD6 /* XmlDocument.m in Sources */,
E39AA44D1582928B00A93BCE /* OSX_SecRandom.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 04a4249

Please sign in to comment.