Browse Source

add lock in CryptoApiRandomGenerator NextBytes

Ugochukwu Mmaduekwe 7 years ago
parent
commit
f1b3d38f1f

+ 2 - 2
CryptoLib/src/Asn1/ClpDerObjectIdentifier.pas

@@ -69,8 +69,6 @@ type
     class constructor CreateDerObjectIdentifier();
     class constructor CreateDerObjectIdentifier();
     class destructor DestroyDerObjectIdentifier();
     class destructor DestroyDerObjectIdentifier();
 
 
-    class procedure Boot();
-
     constructor Create(const oid: IDerObjectIdentifier;
     constructor Create(const oid: IDerObjectIdentifier;
       const branchID: String); overload;
       const branchID: String); overload;
     constructor Create(const bytes: TCryptoLibByteArray); overload;
     constructor Create(const bytes: TCryptoLibByteArray); overload;
@@ -123,6 +121,8 @@ type
     class function FromOctetString(const enc: TCryptoLibByteArray)
     class function FromOctetString(const enc: TCryptoLibByteArray)
       : IDerObjectIdentifier; static;
       : IDerObjectIdentifier; static;
 
 
+    class procedure Boot(); static;
+
     constructor Create(const identifier: String); overload;
     constructor Create(const identifier: String); overload;
 
 
     property ID: String read GetID;
     property ID: String read GetID;

+ 38 - 1
CryptoLib/src/Crypto/Prng/ClpCryptoApiRandomGenerator.pas

@@ -22,6 +22,7 @@ unit ClpCryptoApiRandomGenerator;
 interface
 interface
 
 
 uses
 uses
+  SyncObjs,
   ClpCryptoLibTypes,
   ClpCryptoLibTypes,
   ClpIRandomNumberGenerator,
   ClpIRandomNumberGenerator,
   ClpRandomNumberGenerator,
   ClpRandomNumberGenerator,
@@ -40,8 +41,17 @@ type
     ICryptoApiRandomGenerator, IRandomGenerator)
     ICryptoApiRandomGenerator, IRandomGenerator)
 
 
   strict private
   strict private
+  var
     FrndProv: IRandomNumberGenerator;
     FrndProv: IRandomNumberGenerator;
 
 
+  class var
+
+    FLock: TCriticalSection;
+    FIsBooted: Boolean;
+
+    class constructor CreateCryptoApiRandomGenerator();
+    class destructor DestroyCryptoApiRandomGenerator();
+
   public
   public
     /// <summary>
     /// <summary>
     /// Uses TRandomNumberGenerator.CreateRNG() to Get randomness generator
     /// Uses TRandomNumberGenerator.CreateRNG() to Get randomness generator
@@ -69,6 +79,8 @@ type
     procedure NextBytes(const bytes: TCryptoLibByteArray; start, len: Int32);
     procedure NextBytes(const bytes: TCryptoLibByteArray; start, len: Int32);
       overload; virtual;
       overload; virtual;
 
 
+    class procedure Boot(); static;
+
   end;
   end;
 
 
 implementation
 implementation
@@ -80,6 +92,16 @@ begin
   // We don't care about the seed
   // We don't care about the seed
 end;
 end;
 
 
+class procedure TCryptoApiRandomGenerator.Boot;
+begin
+  if not FIsBooted then
+  begin
+    FLock := TCriticalSection.Create;
+
+    FIsBooted := True;
+  end;
+end;
+
 procedure TCryptoApiRandomGenerator.AddSeedMaterial
 procedure TCryptoApiRandomGenerator.AddSeedMaterial
   (const seed: TCryptoLibByteArray);
   (const seed: TCryptoLibByteArray);
 begin
 begin
@@ -92,6 +114,16 @@ begin
   FrndProv := rng;
   FrndProv := rng;
 end;
 end;
 
 
+class constructor TCryptoApiRandomGenerator.CreateCryptoApiRandomGenerator;
+begin
+  TCryptoApiRandomGenerator.Boot;
+end;
+
+class destructor TCryptoApiRandomGenerator.DestroyCryptoApiRandomGenerator;
+begin
+  FLock.Free;
+end;
+
 constructor TCryptoApiRandomGenerator.Create;
 constructor TCryptoApiRandomGenerator.Create;
 begin
 begin
   Create(TRandomNumberGenerator.CreateRNG());
   Create(TRandomNumberGenerator.CreateRNG());
@@ -99,7 +131,12 @@ end;
 
 
 procedure TCryptoApiRandomGenerator.NextBytes(const bytes: TCryptoLibByteArray);
 procedure TCryptoApiRandomGenerator.NextBytes(const bytes: TCryptoLibByteArray);
 begin
 begin
-  FrndProv.GetBytes(bytes);
+  FLock.Acquire;
+  try
+    FrndProv.GetBytes(bytes);
+  finally
+    FLock.Release;
+  end;
 end;
 end;
 
 
 procedure TCryptoApiRandomGenerator.NextBytes(const bytes: TCryptoLibByteArray;
 procedure TCryptoApiRandomGenerator.NextBytes(const bytes: TCryptoLibByteArray;