|
@@ -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;
|