Browse Source

some OSRandom refactorings

Ugochukwu Mmaduekwe 6 years ago
parent
commit
9e1eade1a6
3 changed files with 53 additions and 13 deletions
  1. 2 1
      .travis.yml
  2. 8 2
      CryptoLib/src/Include/CryptoLib.inc
  3. 43 10
      CryptoLib/src/Utils/Randoms/ClpOSRandom.pas

+ 2 - 1
.travis.yml

@@ -70,7 +70,8 @@ script:
   - lazbuild $LAZ_OPT ./generics.collections-master/generics_collections.lpk  # Build generics.collections Package
   - lazbuild $LAZ_OPT ./generics.collections-master/generics_collections.lpk  # Build generics.collections Package
   - lazbuild $LAZ_OPT ./CryptoLib/src/Packages/FPC/CryptoLib4PascalPackage.lpk  # Build CryptoLib4Pascal Package
   - lazbuild $LAZ_OPT ./CryptoLib/src/Packages/FPC/CryptoLib4PascalPackage.lpk  # Build CryptoLib4Pascal Package
   - lazbuild $LAZ_OPT ./CryptoLib.Tests/FreePascal.Tests/CryptoLibConsole.Tests.lpi  # Build CryptoLib4Pascal Test Project
   - lazbuild $LAZ_OPT ./CryptoLib.Tests/FreePascal.Tests/CryptoLibConsole.Tests.lpi  # Build CryptoLib4Pascal Test Project
-  - travis_wait 120 $LAZ_ENV ./CryptoLib.Tests/FreePascal.Tests/bin/CryptoLib --all --format=plain --progress # Run CryptoLib4Pascal TestSuite with timeout of 120 mins
+  - travis_wait 120 $LAZ_ENV ./CryptoLib.Tests/FreePascal.Tests/bin/CryptoLib --format=plain --suite=TTestSecureRandom --progress  # Run TTestSecureRandom TestSuite with timeout of 120 mins
+ # - travis_wait 120 $LAZ_ENV ./CryptoLib.Tests/FreePascal.Tests/bin/CryptoLib --all --format=plain --progress # Run CryptoLib4Pascal TestSuite with timeout of 120 mins
  # - travis_wait 120 $LAZ_ENV ./CryptoLib.Tests/FreePascal.Tests/bin/CryptoLib --format=plain --suite=TTestMD5HMac --progress  # Run TTestMD5HMac TestSuite with timeout of 120 mins
  # - travis_wait 120 $LAZ_ENV ./CryptoLib.Tests/FreePascal.Tests/bin/CryptoLib --format=plain --suite=TTestMD5HMac --progress  # Run TTestMD5HMac TestSuite with timeout of 120 mins
 
 
 
 

+ 8 - 2
CryptoLib/src/Include/CryptoLib.inc

@@ -49,7 +49,6 @@
    {$DEFINE CRYPTOLIB_UNIX}
    {$DEFINE CRYPTOLIB_UNIX}
    {$IF DEFINED(BSD)}
    {$IF DEFINED(BSD)}
       {$IF DEFINED(DARWIN)}
       {$IF DEFINED(DARWIN)}
-         {$UNDEF CRYPTOLIB_UNIX}
          {$DEFINE CRYPTOLIB_APPLE}
          {$DEFINE CRYPTOLIB_APPLE}
          {$IF DEFINED(CRYPTOLIB_ARM) OR DEFINED(CRYPTOLIB_AARCH64)}
          {$IF DEFINED(CRYPTOLIB_ARM) OR DEFINED(CRYPTOLIB_AARCH64)}
             {$DEFINE CRYPTOLIB_IOS}
             {$DEFINE CRYPTOLIB_IOS}
@@ -57,7 +56,6 @@
             {$DEFINE CRYPTOLIB_MACOS}
             {$DEFINE CRYPTOLIB_MACOS}
          {$IFEND}
          {$IFEND}
       {$ELSEIF DEFINED(FREEBSD) OR DEFINED(NETBSD) OR DEFINED(OPENBSD) OR DEFINED(DRAGONFLY)}
       {$ELSEIF DEFINED(FREEBSD) OR DEFINED(NETBSD) OR DEFINED(OPENBSD) OR DEFINED(DRAGONFLY)}
-         {$UNDEF CRYPTOLIB_UNIX}
          {$DEFINE CRYPTOLIB_GENERIC_BSD}
          {$DEFINE CRYPTOLIB_GENERIC_BSD}
       {$IFEND}
       {$IFEND}
   {$ELSEIF DEFINED(LINUX)}
   {$ELSEIF DEFINED(LINUX)}
@@ -71,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}
+{$ENDIF}
+
 {$DEFINE USE_UNROLLED_VARIANT}
 {$DEFINE USE_UNROLLED_VARIANT}
 
 
 // Disable Overflow and RangeChecks.
 // Disable Overflow and RangeChecks.
@@ -150,6 +152,10 @@
    {$DEFINE CRYPTOLIB_LINUX}
    {$DEFINE CRYPTOLIB_LINUX}
 {$ENDIF}
 {$ENDIF}
 
 
+{$IFDEF  CRYPTOLIB_LINUX}
+   {$DEFINE CRYPTOLIB_HAS_GETRANDOM}
+{$ENDIF}
+
 {$IF DEFINED(CRYPTOLIB_APPLE) OR DEFINED(CRYPTOLIB_LINUX)}
 {$IF DEFINED(CRYPTOLIB_APPLE) OR DEFINED(CRYPTOLIB_LINUX)}
    {$DEFINE CRYPTOLIB_UNIX}
    {$DEFINE CRYPTOLIB_UNIX}
 {$ENDIF}
 {$ENDIF}

+ 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 OSX</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 OSX 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