Browse Source

add support for Mac OS X versions less than 10.7

Ugochukwu Mmaduekwe 6 years ago
parent
commit
52232ebbb2
3 changed files with 53 additions and 12 deletions
  1. 9 1
      CryptoLib/src/Include/CryptoLib.inc
  2. 43 10
      CryptoLib/src/Utils/Randoms/ClpOSRandom.pas
  3. 1 1
      README.md

+ 9 - 1
CryptoLib/src/Include/CryptoLib.inc

@@ -69,6 +69,10 @@
    {$MESSAGE ERROR 'UNSUPPORTED TARGET.'}
    {$MESSAGE ERROR 'UNSUPPORTED TARGET.'}
 {$ENDIF}
 {$ENDIF}
 
 
+{$IF DEFINED(CRYPTOLIB_LINUX) OR DEFINED(CRYPTOLIB_SOLARIS)}
+   {$DEFINE CRYPTOLIB_HAS_GETRANDOM}
+{$IFEND}
+
 {$DEFINE USE_UNROLLED_VARIANT}
 {$DEFINE USE_UNROLLED_VARIANT}
 
 
 // Disable Overflow and RangeChecks.
 // Disable Overflow and RangeChecks.
@@ -146,11 +150,15 @@
 
 
 {$IF DEFINED(LINUX) OR DEFINED(ANDROID)}
 {$IF DEFINED(LINUX) OR DEFINED(ANDROID)}
    {$DEFINE CRYPTOLIB_LINUX}
    {$DEFINE CRYPTOLIB_LINUX}
+{$IFEND}
+
+{$IFDEF CRYPTOLIB_LINUX}
+   {$DEFINE CRYPTOLIB_HAS_GETRANDOM}
 {$ENDIF}
 {$ENDIF}
 
 
 {$IF DEFINED(CRYPTOLIB_APPLE) OR DEFINED(CRYPTOLIB_LINUX)}
 {$IF DEFINED(CRYPTOLIB_APPLE) OR DEFINED(CRYPTOLIB_LINUX)}
    {$DEFINE CRYPTOLIB_UNIX}
    {$DEFINE CRYPTOLIB_UNIX}
-{$ENDIF}
+{$IFEND}
 
 
 {$DEFINE USE_UNROLLED_VARIANT}
 {$DEFINE USE_UNROLLED_VARIANT}
 
 

+ 43 - 10
CryptoLib/src/Utils/Randoms/ClpOSRandom.pas

@@ -28,27 +28,34 @@ uses
 {$IFDEF CRYPTOLIB_APPLE}
 {$IFDEF CRYPTOLIB_APPLE}
 {$IFDEF FPC}
 {$IFDEF FPC}
 {$LINKFRAMEWORK Security}
 {$LINKFRAMEWORK Security}
+{$IFDEF CRYPTOLIB_MACOS}
+  CocoaAll,
+{$ENDIF} // ENDIF CRYPTOLIB_MACOS
 {$ELSE}
 {$ELSE}
   // Macapi.Dispatch, or
   // Macapi.Dispatch, or
   Macapi.ObjCRuntime,
   Macapi.ObjCRuntime,
-{$IF DEFINED(CRYPTOLIB_IOS)}
+{$IFDEF CRYPTOLIB_IOS}
   iOSapi.Foundation,
   iOSapi.Foundation,
-{$ELSEIF DEFINED(CRYPTOLIB_MACOS)}
+{$ENDIF} // ENDIF CRYPTOLIB_IOS
+{$IFDEF CRYPTOLIB_MACOS}
+  Macapi.AppKit,
   Macapi.Foundation,
   Macapi.Foundation,
-{$ELSE}
-{$MESSAGE ERROR 'UNSUPPORTED TARGET.'}
-{$IFEND} // ENDIF CRYPTOLIB_MACOS
+{$ENDIF} // ENDIF CRYPTOLIB_MACOS
 {$ENDIF}  // ENDIF FPC
 {$ENDIF}  // ENDIF FPC
 {$ENDIF}   // ENDIF CRYPTOLIB_APPLE
 {$ENDIF}   // ENDIF CRYPTOLIB_APPLE
 {$IFDEF CRYPTOLIB_UNIX}
 {$IFDEF CRYPTOLIB_UNIX}
   Classes,
   Classes,
 {$IFDEF FPC}
 {$IFDEF FPC}
   BaseUnix,
   BaseUnix,
+{$IFDEF CRYPTOLIB_HAS_GETRANDOM}
   dl,
   dl,
+{$ENDIF}
 {$ELSE}
 {$ELSE}
   Posix.Errno,
   Posix.Errno,
+{$IFDEF CRYPTOLIB_HAS_GETRANDOM}
   Posix.Dlfcn,
   Posix.Dlfcn,
 {$ENDIF}
 {$ENDIF}
+{$ENDIF}
 {$ENDIF}  // ENDIF CRYPTOLIB_UNIX
 {$ENDIF}  // ENDIF CRYPTOLIB_UNIX
 {$IFDEF CRYPTOLIB_PUREBSD}
 {$IFDEF CRYPTOLIB_PUREBSD}
   // PureBSD (NetBSD, FreeBSD, OpenBSD)
   // PureBSD (NetBSD, FreeBSD, OpenBSD)
@@ -122,7 +129,13 @@ type
   /// BCryptGenRandom</see> for <b>Vista</b> Upwards</description>
   /// BCryptGenRandom</see> for <b>Vista</b> Upwards</description>
   /// </item>
   /// </item>
   /// <item>
   /// <item>
-  /// <term>macOS, iOS</term>
+  /// <term>Mac OS X</term>
+  /// <description><see href="https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc">
+  /// SecRandomCopyBytes</see> for <b>10.7+,</b> ( /dev/urandom
+  /// or /dev/random) (which ever is available) for &lt; <b>10.7</b><br /></description>
+  /// </item>
+  /// <item>
+  /// <term>iOS</term>
   /// <description><see href="https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc">
   /// <description><see href="https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc">
   /// SecRandomCopyBytes</see><br /></description>
   /// SecRandomCopyBytes</see><br /></description>
   /// </item>
   /// </item>
@@ -156,9 +169,15 @@ type
 
 
 {$IFDEF CRYPTOLIB_UNIX}
 {$IFDEF CRYPTOLIB_UNIX}
   const
   const
-    GRND_DEFAULT: Int32 = $0000;
     EINTR = {$IFDEF FPC}ESysEINTR {$ELSE}Posix.Errno.EINTR{$ENDIF};
     EINTR = {$IFDEF FPC}ESysEINTR {$ELSE}Posix.Errno.EINTR{$ENDIF};
 
 
+    class function ErrorNo: Int32; static; inline;
+
+{$IFDEF CRYPTOLIB_HAS_GETRANDOM}
+
+  const
+    GRND_DEFAULT: Int32 = $0000;
+
 {$IFDEF CRYPTOLIB_LINUX}
 {$IFDEF CRYPTOLIB_LINUX}
     LIBC_SO = 'libc.so.6';
     LIBC_SO = 'libc.so.6';
 {$ENDIF}
 {$ENDIF}
@@ -175,8 +194,6 @@ type
     FIsGetRandomSupportedOnOS: Boolean;
     FIsGetRandomSupportedOnOS: Boolean;
     FGetRandom: TGetRandom;
     FGetRandom: TGetRandom;
 
 
-    class function ErrorNo: Int32; static; inline;
-
     class function GetIsGetRandomSupportedOnOS(): Boolean; static; inline;
     class function GetIsGetRandomSupportedOnOS(): Boolean; static; inline;
 
 
     class function IsGetRandomAvailable(): Boolean; static;
     class function IsGetRandomAvailable(): Boolean; static;
@@ -184,6 +201,7 @@ type
     class property IsGetRandomSupportedOnOS: Boolean
     class property IsGetRandomSupportedOnOS: Boolean
       read GetIsGetRandomSupportedOnOS;
       read GetIsGetRandomSupportedOnOS;
 
 
+{$ENDIF}
 {$ENDIF}
 {$ENDIF}
     // ================================================================//
     // ================================================================//
 
 
@@ -328,7 +346,7 @@ begin
 {$IFDEF CRYPTOLIB_MSWINDOWS}
 {$IFDEF CRYPTOLIB_MSWINDOWS}
   FIsCngBCryptGenRandomSupportedOnOS := IsCngBCryptGenRandomAvailable();
   FIsCngBCryptGenRandomSupportedOnOS := IsCngBCryptGenRandomAvailable();
 {$ENDIF}
 {$ENDIF}
-{$IFDEF CRYPTOLIB_UNIX}
+{$IFDEF CRYPTOLIB_HAS_GETRANDOM}
   FIsGetRandomSupportedOnOS := IsGetRandomAvailable();
   FIsGetRandomSupportedOnOS := IsGetRandomAvailable();
 {$ENDIF}
 {$ENDIF}
 end;
 end;
@@ -451,7 +469,20 @@ class function TOSRandom.GenRandomBytesApple(len: Int32; data: PByte): Int32;
   end;
   end;
 
 
 begin
 begin
+{$IF DEFINED(CRYPTOLIB_MACOS)}
+  // >= (Mac OS X 10.7+)
+  if NSAppKitVersionNumber >= 1138 then // NSAppKitVersionNumber10_7
+  begin
+    result := SecRandomCopyBytes(kSecRandomDefault, LongWord(len), data);
+  end
+  else
+  begin
+    // fallback for when SecRandomCopyBytes API is not available
+    result := dev_random_device_read(len, data);
+  end;
+{$ELSE}
   result := SecRandomCopyBytes(kSecRandomDefault, LongWord(len), data);
   result := SecRandomCopyBytes(kSecRandomDefault, LongWord(len), data);
+{$IFEND}
 end;
 end;
 
 
 {$ENDIF}
 {$ENDIF}
@@ -520,6 +551,8 @@ class function TOSRandom.ErrorNo: Int32;
 begin
 begin
   result := Errno;
   result := Errno;
 end;
 end;
+{$ENDIF}
+{$IFDEF CRYPTOLIB_HAS_GETRANDOM}
 
 
 class function TOSRandom.GetIsGetRandomSupportedOnOS(): Boolean;
 class function TOSRandom.GetIsGetRandomSupportedOnOS(): Boolean;
 begin
 begin

+ 1 - 1
README.md

@@ -180,7 +180,7 @@ Available Algorithms
 
 
 * `Linux (Including Android and Raspberry PI)`
 * `Linux (Including Android and Raspberry PI)`
 
 
-* `macOS 10.7+`
+* `Mac OS X`
 
 
 * `iOS 2.0+`
 * `iOS 2.0+`