From 04a42492f11313a391bfef8339ad76f263d02b9c Mon Sep 17 00:00:00 2001 From: Andy Sawyer Date: Fri, 8 Jun 2012 21:19:23 +0100 Subject: [PATCH] Add OSX Implementation of SecRandomCopy --- Classes/NSData+AES.m | 4 ++++ OSX_Support/OSX_SecRandom.h | 7 +++++++ OSX_Support/OSX_SecRandom.m | 33 +++++++++++++++++++++++++++++++ PWStore.xcodeproj/project.pbxproj | 14 +++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 OSX_Support/OSX_SecRandom.h create mode 100644 OSX_Support/OSX_SecRandom.m diff --git a/Classes/NSData+AES.m b/Classes/NSData+AES.m index 4a1a18e..6a55ad8 100644 --- a/Classes/NSData+AES.m +++ b/Classes/NSData+AES.m @@ -10,6 +10,10 @@ #import #import +#if !TARGET_OS_IPHONE +#import "OSX_SecRandom.h" +#endif + @implementation NSData (AES) -(NSMutableData *)sha256 { diff --git a/OSX_Support/OSX_SecRandom.h b/OSX_Support/OSX_SecRandom.h new file mode 100644 index 0000000..538b361 --- /dev/null +++ b/OSX_Support/OSX_SecRandom.h @@ -0,0 +1,7 @@ +#if !TARGET_OS_IPHONE +#import +#import +typedef const struct __SecRandom * SecRandomRef; +extern const SecRandomRef kSecRandomDefault; +int SecRandomCopyBytes( SecRandomRef rnd, size_t count, uint8_t *bytes ); +#endif diff --git a/OSX_Support/OSX_SecRandom.m b/OSX_Support/OSX_SecRandom.m new file mode 100644 index 0000000..571f39f --- /dev/null +++ b/OSX_Support/OSX_SecRandom.m @@ -0,0 +1,33 @@ +#if !TARGET_OS_IPHONE + +#import +#import +#import +#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 diff --git a/PWStore.xcodeproj/project.pbxproj b/PWStore.xcodeproj/project.pbxproj index 67bcf49..82e34ea 100755 --- a/PWStore.xcodeproj/project.pbxproj +++ b/PWStore.xcodeproj/project.pbxproj @@ -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 */ @@ -106,6 +107,8 @@ E3995063157AB505001CE6D3 /* PWData+StringExport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "PWData+StringExport.h"; sourceTree = ""; }; E3995064157AB505001CE6D3 /* PWData+StringExport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "PWData+StringExport.m"; sourceTree = ""; }; 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 = ""; }; + E39AA44C1582928B00A93BCE /* OSX_SecRandom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OSX_SecRandom.m; path = OSX_Support/OSX_SecRandom.m; sourceTree = ""; }; E3C91233157D3ACC0055AAD6 /* SyncViewController+TestDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "SyncViewController+TestDriver.h"; sourceTree = ""; }; E3C91234157D3ACC0055AAD6 /* SyncViewController+TestDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "SyncViewController+TestDriver.m"; sourceTree = ""; }; E3C912F4157D4A040055AAD6 /* XmlDocument.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XmlDocument.h; sourceTree = ""; }; @@ -164,6 +167,7 @@ 29B97315FDCFA39411CA2CEA /* Other Sources */ = { isa = PBXGroup; children = ( + E39AA44315828C5E00A93BCE /* OSX Support */, 28A0AAE50D9B0CCF005BE974 /* PWStore_Prefix.pch */, 29B97316FDCFA39411CA2CEA /* main.m */, ); @@ -293,6 +297,15 @@ name = "User Interface"; sourceTree = ""; }; + E39AA44315828C5E00A93BCE /* OSX Support */ = { + isa = PBXGroup; + children = ( + E39AA44B1582928B00A93BCE /* OSX_SecRandom.h */, + E39AA44C1582928B00A93BCE /* OSX_SecRandom.m */, + ); + name = "OSX Support"; + sourceTree = ""; + }; E3C9125E157D3C710055AAD6 /* Xml Parser */ = { isa = PBXGroup; children = ( @@ -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; };