Browse Source

some refactorings.

- some refactorings with respect to criticalsections
Ugochukwu Mmaduekwe 6 years ago
parent
commit
0c2dfb8e86

+ 3 - 14
CryptoLib/src/Asn1/ClpAsn1Objects.pas

@@ -8134,22 +8134,11 @@ procedure TDerBmpString.Encode(const derOut: TStream);
 var
 var
   c: TCryptoLibCharArray;
   c: TCryptoLibCharArray;
   b: TCryptoLibByteArray;
   b: TCryptoLibByteArray;
-  I, LowPoint, HighPoint: Int32;
+  I: Int32;
 begin
 begin
-  System.SetLength(c, System.length(Str));
 
 
-  // had to use this loop because somehow, StrPLCopy causes memory leak in FPC v3.0.5
-{$IFDEF DELPHIXE3_UP}
-  LowPoint := System.Low(Str);
-  HighPoint := System.High(Str);
-{$ELSE}
-  LowPoint := 1;
-  HighPoint := System.length(Str);
-{$ENDIF DELPHIXE3_UP}
-  for I := LowPoint to HighPoint do
-  begin
-    c[I - 1] := Str[I];
-  end;
+  c := TStringUtils.StringToCharArray(Str);
+
   System.SetLength(b, System.length(c) * 2);
   System.SetLength(b, System.length(c) * 2);
 
 
   I := 0;
   I := 0;

+ 10 - 20
CryptoLib/src/Asn1/X9/ClpX9ECParametersHolder.pas

@@ -31,21 +31,18 @@ type
     IX9ECParametersHolder)
     IX9ECParametersHolder)
 
 
   strict private
   strict private
+  var
+    FLock: TCriticalSection;
     Fparameters: IX9ECParameters;
     Fparameters: IX9ECParameters;
 
 
-    class var
-
-      FLock: TCriticalSection;
-
-    class procedure Boot(); static;
-    class constructor CreateX9ECParametersHolder();
-    class destructor DestroyX9ECParametersHolder();
-
   strict protected
   strict protected
+
     function GetParameters: IX9ECParameters; inline;
     function GetParameters: IX9ECParameters; inline;
     function CreateParameters(): IX9ECParameters; virtual; abstract;
     function CreateParameters(): IX9ECParameters; virtual; abstract;
 
 
   public
   public
+    constructor Create();
+    destructor Destroy; override;
     property Parameters: IX9ECParameters read GetParameters;
     property Parameters: IX9ECParameters read GetParameters;
 
 
   end;
   end;
@@ -54,27 +51,20 @@ implementation
 
 
 { TX9ECParametersHolder }
 { TX9ECParametersHolder }
 
 
-class procedure TX9ECParametersHolder.Boot;
+constructor TX9ECParametersHolder.Create;
 begin
 begin
-  if FLock = Nil then
-  begin
-    FLock := TCriticalSection.Create;
-  end;
+  Inherited Create();
+  FLock := TCriticalSection.Create;
 end;
 end;
 
 
-class constructor TX9ECParametersHolder.CreateX9ECParametersHolder;
-begin
-  TX9ECParametersHolder.Boot;
-end;
-
-class destructor TX9ECParametersHolder.DestroyX9ECParametersHolder;
+destructor TX9ECParametersHolder.Destroy;
 begin
 begin
   FLock.Free;
   FLock.Free;
+  inherited Destroy;
 end;
 end;
 
 
 function TX9ECParametersHolder.GetParameters: IX9ECParameters;
 function TX9ECParametersHolder.GetParameters: IX9ECParameters;
 begin
 begin
-
   FLock.Acquire;
   FLock.Acquire;
   try
   try
     if (Fparameters = Nil) then
     if (Fparameters = Nil) then

+ 7 - 23
CryptoLib/src/Crypto/Parameters/ClpECDomainParameters.pas

@@ -43,11 +43,8 @@ type
 
 
   strict private
   strict private
 
 
-    class var
-
-      FLock: TCriticalSection;
-
   var
   var
+    FLock: TCriticalSection;
     Fcurve: IECCurve;
     Fcurve: IECCurve;
     Fseed: TCryptoLibByteArray;
     Fseed: TCryptoLibByteArray;
     Fg: IECPoint;
     Fg: IECPoint;
@@ -60,10 +57,6 @@ type
     function GetHInv: TBigInteger; inline;
     function GetHInv: TBigInteger; inline;
     function GetSeed: TCryptoLibByteArray; inline;
     function GetSeed: TCryptoLibByteArray; inline;
 
 
-    class procedure Boot(); static;
-    class constructor CreateECDomainParameters();
-    class destructor DestroyECDomainParameters();
-
   public
   public
 
 
     class function Validate(const c: IECCurve; const q: IECPoint)
     class function Validate(const c: IECCurve; const q: IECPoint)
@@ -76,6 +69,8 @@ type
     constructor Create(const curve: IECCurve; const g: IECPoint;
     constructor Create(const curve: IECCurve; const g: IECPoint;
       const n, h: TBigInteger; const seed: TCryptoLibByteArray); overload;
       const n, h: TBigInteger; const seed: TCryptoLibByteArray); overload;
 
 
+    destructor Destroy; override;
+
     property curve: IECCurve read GetCurve;
     property curve: IECCurve read GetCurve;
     property g: IECPoint read GetG;
     property g: IECPoint read GetG;
     property n: TBigInteger read GetN;
     property n: TBigInteger read GetN;
@@ -130,7 +125,6 @@ end;
 
 
 function TECDomainParameters.GetHInv: TBigInteger;
 function TECDomainParameters.GetHInv: TBigInteger;
 begin
 begin
-
   FLock.Acquire;
   FLock.Acquire;
   try
   try
     if (not(FhInv.IsInitialized)) then
     if (not(FhInv.IsInitialized)) then
@@ -160,14 +154,6 @@ begin
   Create(curve, g, n, h, Nil);
   Create(curve, g, n, h, Nil);
 end;
 end;
 
 
-class procedure TECDomainParameters.Boot;
-begin
-  if FLock = Nil then
-  begin
-    FLock := TCriticalSection.Create;
-  end;
-end;
-
 constructor TECDomainParameters.Create(const curve: IECCurve; const g: IECPoint;
 constructor TECDomainParameters.Create(const curve: IECCurve; const g: IECPoint;
   const n, h: TBigInteger; const seed: TCryptoLibByteArray);
   const n, h: TBigInteger; const seed: TCryptoLibByteArray);
 begin
 begin
@@ -180,6 +166,8 @@ begin
     raise EArgumentNilCryptoLibException.CreateResFmt
     raise EArgumentNilCryptoLibException.CreateResFmt
       (@SBigIntegerNotInitialized, ['n']);
       (@SBigIntegerNotInitialized, ['n']);
 
 
+  FLock := TCriticalSection.Create;
+
   // we can't check for (not (h.IsInitialized)) here as h is optional in X9.62 as it is not required for ECDSA
   // we can't check for (not (h.IsInitialized)) here as h is optional in X9.62 as it is not required for ECDSA
 
 
   Fcurve := curve;
   Fcurve := curve;
@@ -192,14 +180,10 @@ begin
 
 
 end;
 end;
 
 
-class constructor TECDomainParameters.CreateECDomainParameters;
-begin
-  TECDomainParameters.Boot;
-end;
-
-class destructor TECDomainParameters.DestroyECDomainParameters;
+destructor TECDomainParameters.Destroy;
 begin
 begin
   FLock.Free;
   FLock.Free;
+  inherited Destroy;
 end;
 end;
 
 
 function TECDomainParameters.Equals(const other: IECDomainParameters): Boolean;
 function TECDomainParameters.Equals(const other: IECDomainParameters): Boolean;

+ 3 - 36
CryptoLib/src/Crypto/Prng/ClpCryptoApiRandomGenerator.pas

@@ -22,12 +22,11 @@ unit ClpCryptoApiRandomGenerator;
 interface
 interface
 
 
 uses
 uses
-  SyncObjs,
-  ClpCryptoLibTypes,
   ClpIRandomNumberGenerator,
   ClpIRandomNumberGenerator,
   ClpRandomNumberGenerator,
   ClpRandomNumberGenerator,
   ClpICryptoApiRandomGenerator,
   ClpICryptoApiRandomGenerator,
-  ClpIRandomGenerator;
+  ClpIRandomGenerator,
+  ClpCryptoLibTypes;
 
 
 resourcestring
 resourcestring
   SNegativeOffset = 'Start Offset Cannot be Negative, "Start"';
   SNegativeOffset = 'Start Offset Cannot be Negative, "Start"';
@@ -44,14 +43,6 @@ type
   var
   var
     FrndProv: IRandomNumberGenerator;
     FrndProv: IRandomNumberGenerator;
 
 
-    class var
-
-      FLock: TCriticalSection;
-
-    class procedure Boot(); static;
-    class constructor CreateCryptoApiRandomGenerator();
-    class destructor DestroyCryptoApiRandomGenerator();
-
   public
   public
     /// <summary>
     /// <summary>
     /// Uses TRandomNumberGenerator.CreateRNG() to Get randomness generator
     /// Uses TRandomNumberGenerator.CreateRNG() to Get randomness generator
@@ -90,14 +81,6 @@ begin
   // We don't care about the seed
   // We don't care about the seed
 end;
 end;
 
 
-class procedure TCryptoApiRandomGenerator.Boot;
-begin
-  if FLock = Nil then
-  begin
-    FLock := TCriticalSection.Create;
-  end;
-end;
-
 procedure TCryptoApiRandomGenerator.AddSeedMaterial
 procedure TCryptoApiRandomGenerator.AddSeedMaterial
   (const seed: TCryptoLibByteArray);
   (const seed: TCryptoLibByteArray);
 begin
 begin
@@ -110,16 +93,6 @@ 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());
@@ -127,13 +100,7 @@ end;
 
 
 procedure TCryptoApiRandomGenerator.NextBytes(const bytes: TCryptoLibByteArray);
 procedure TCryptoApiRandomGenerator.NextBytes(const bytes: TCryptoLibByteArray);
 begin
 begin
-
-  FLock.Acquire;
-  try
-    FrndProv.GetBytes(bytes);
-  finally
-    FLock.Release;
-  end;
+  FrndProv.GetBytes(bytes);
 end;
 end;
 
 
 procedure TCryptoApiRandomGenerator.NextBytes(const bytes: TCryptoLibByteArray;
 procedure TCryptoApiRandomGenerator.NextBytes(const bytes: TCryptoLibByteArray;

+ 5 - 25
CryptoLib/src/Crypto/Prng/ClpDigestRandomGenerator.pas

@@ -45,6 +45,7 @@ type
     CYCLE_COUNT = Int64(10);
     CYCLE_COUNT = Int64(10);
 
 
   var
   var
+    FLock: TCriticalSection;
     FstateCounter, FseedCounter: Int64;
     FstateCounter, FseedCounter: Int64;
     Fdigest: IDigest;
     Fdigest: IDigest;
     Fstate, Fseed: TCryptoLibByteArray;
     Fstate, Fseed: TCryptoLibByteArray;
@@ -55,17 +56,10 @@ type
     procedure DigestUpdate(const inSeed: TCryptoLibByteArray); inline;
     procedure DigestUpdate(const inSeed: TCryptoLibByteArray); inline;
     procedure DigestDoFinal(const result: TCryptoLibByteArray); inline;
     procedure DigestDoFinal(const result: TCryptoLibByteArray); inline;
 
 
-    class var
-
-      FLock: TCriticalSection;
-
-    class procedure Boot(); static;
-    class constructor CreateDigestRandomGenerator();
-    class destructor DestroyDigestRandomGenerator();
-
   public
   public
 
 
     constructor Create(const digest: IDigest);
     constructor Create(const digest: IDigest);
+    destructor Destroy; override;
     procedure AddSeedMaterial(const inSeed: TCryptoLibByteArray);
     procedure AddSeedMaterial(const inSeed: TCryptoLibByteArray);
       overload; inline;
       overload; inline;
     procedure AddSeedMaterial(rSeed: Int64); overload; inline;
     procedure AddSeedMaterial(rSeed: Int64); overload; inline;
@@ -102,7 +96,6 @@ end;
 
 
 procedure TDigestRandomGenerator.AddSeedMaterial(rSeed: Int64);
 procedure TDigestRandomGenerator.AddSeedMaterial(rSeed: Int64);
 begin
 begin
-
   FLock.Acquire;
   FLock.Acquire;
   try
   try
     DigestAddCounter(rSeed);
     DigestAddCounter(rSeed);
@@ -113,18 +106,9 @@ begin
   end;
   end;
 end;
 end;
 
 
-class procedure TDigestRandomGenerator.Boot;
-begin
-  if FLock = Nil then
-  begin
-    FLock := TCriticalSection.Create;
-  end;
-end;
-
 procedure TDigestRandomGenerator.AddSeedMaterial(const inSeed
 procedure TDigestRandomGenerator.AddSeedMaterial(const inSeed
   : TCryptoLibByteArray);
   : TCryptoLibByteArray);
 begin
 begin
-
   FLock.Acquire;
   FLock.Acquire;
   try
   try
     DigestUpdate(inSeed);
     DigestUpdate(inSeed);
@@ -138,6 +122,7 @@ end;
 constructor TDigestRandomGenerator.Create(const digest: IDigest);
 constructor TDigestRandomGenerator.Create(const digest: IDigest);
 begin
 begin
   Inherited Create();
   Inherited Create();
+  FLock := TCriticalSection.Create;
   Fdigest := digest;
   Fdigest := digest;
   System.SetLength(Fseed, digest.GetDigestSize);
   System.SetLength(Fseed, digest.GetDigestSize);
   FseedCounter := 1;
   FseedCounter := 1;
@@ -145,11 +130,6 @@ begin
   FstateCounter := 1;
   FstateCounter := 1;
 end;
 end;
 
 
-class constructor TDigestRandomGenerator.CreateDigestRandomGenerator;
-begin
-  TDigestRandomGenerator.Boot;
-end;
-
 procedure TDigestRandomGenerator.CycleSeed;
 procedure TDigestRandomGenerator.CycleSeed;
 begin
 begin
   DigestUpdate(Fseed);
   DigestUpdate(Fseed);
@@ -158,9 +138,10 @@ begin
   DigestDoFinal(Fseed);
   DigestDoFinal(Fseed);
 end;
 end;
 
 
-class destructor TDigestRandomGenerator.DestroyDigestRandomGenerator;
+destructor TDigestRandomGenerator.Destroy;
 begin
 begin
   FLock.Free;
   FLock.Free;
+  inherited Destroy;
 end;
 end;
 
 
 procedure TDigestRandomGenerator.GenerateState;
 procedure TDigestRandomGenerator.GenerateState;
@@ -188,7 +169,6 @@ var
   stateOff, endPoint: Int32;
   stateOff, endPoint: Int32;
   I: Int32;
   I: Int32;
 begin
 begin
-
   FLock.Acquire;
   FLock.Acquire;
   try
   try
     stateOff := 0;
     stateOff := 0;

+ 8 - 33
CryptoLib/src/Math/EC/ClpECC.pas

@@ -474,19 +474,10 @@ type
   /// </summary>
   /// </summary>
   TECCurve = class abstract(TInterfacedObject, IECCurve)
   TECCurve = class abstract(TInterfacedObject, IECCurve)
 
 
-  strict private
-
-    class procedure Boot(); static;
-    class constructor CreateECCurve();
-    class destructor DestroyECCurve();
-
   strict protected
   strict protected
 
 
-    class var
-
-      FLock: TCriticalSection;
-
   var
   var
+    FLock: TCriticalSection;
     Fm_field: IFiniteField;
     Fm_field: IFiniteField;
     Fm_order, Fm_cofactor: TBigInteger;
     Fm_order, Fm_cofactor: TBigInteger;
 
 
@@ -2496,14 +2487,6 @@ end;
 
 
 { TECCurve }
 { TECCurve }
 
 
-class procedure TECCurve.Boot;
-begin
-  if FLock = Nil then
-  begin
-    FLock := TCriticalSection.Create;
-  end;
-end;
-
 procedure TECCurve.CheckPoint(const point: IECPoint);
 procedure TECCurve.CheckPoint(const point: IECPoint);
 begin
 begin
   if ((point = Nil) or ((Self as IECCurve) <> point.curve)) then
   if ((point = Nil) or ((Self as IECCurve) <> point.curve)) then
@@ -2552,9 +2535,16 @@ end;
 constructor TECCurve.Create(const field: IFiniteField);
 constructor TECCurve.Create(const field: IFiniteField);
 begin
 begin
   inherited Create();
   inherited Create();
+  FLock := TCriticalSection.Create;
   Fm_field := field;
   Fm_field := field;
 end;
 end;
 
 
+destructor TECCurve.Destroy;
+begin
+  FLock.Free;
+  inherited Destroy;
+end;
+
 function TECCurve.CreateCacheSafeLookupTable(const points
 function TECCurve.CreateCacheSafeLookupTable(const points
   : TCryptoLibGenericArray<IECPoint>; off, len: Int32): IECLookupTable;
   : TCryptoLibGenericArray<IECPoint>; off, len: Int32): IECLookupTable;
 var
 var
@@ -2618,11 +2608,6 @@ begin
   result := TWNafL2RMultiplier.Create();
   result := TWNafL2RMultiplier.Create();
 end;
 end;
 
 
-class constructor TECCurve.CreateECCurve;
-begin
-  TECCurve.Boot;
-end;
-
 function TECCurve.CreatePoint(const x, y: TBigInteger): IECPoint;
 function TECCurve.CreatePoint(const x, y: TBigInteger): IECPoint;
 begin
 begin
   result := CreatePoint(x, y, false);
   result := CreatePoint(x, y, false);
@@ -2728,16 +2713,6 @@ begin
   result := P;
   result := P;
 end;
 end;
 
 
-destructor TECCurve.Destroy;
-begin
-  inherited Destroy;
-end;
-
-class destructor TECCurve.DestroyECCurve;
-begin
-  FLock.Free;
-end;
-
 function TECCurve.Equals(const other: IECCurve): Boolean;
 function TECCurve.Equals(const other: IECCurve): Boolean;
 begin
 begin
   if ((Self as IECCurve) = other) then
   if ((Self as IECCurve) = other) then

+ 15 - 0
CryptoLib/src/Utils/ClpStringUtils.pas

@@ -45,6 +45,9 @@ type
     class function LastIndexOf(const Input, SubString: string;
     class function LastIndexOf(const Input, SubString: string;
       StartIndex, Count: Int32; IgnoreCase: Boolean): Int32; overload; static;
       StartIndex, Count: Int32; IgnoreCase: Boolean): Int32; overload; static;
 
 
+    class function StringToCharArray(const S: String)
+      : TCryptoLibCharArray; static;
+
   end;
   end;
 
 
 implementation
 implementation
@@ -198,6 +201,18 @@ begin
   end;
   end;
 end;
 end;
 
 
+class function TStringUtils.StringToCharArray(const S: String)
+  : TCryptoLibCharArray;
+begin
+  Result := Nil;
+  if System.Length(S) > 0 then
+  begin
+    System.SetLength(Result, System.Length(S) + 1);
+    StrPLCopy(PChar(Result), S, System.Length(Result));
+    System.SetLength(Result, System.Length(S)); // to remove the null terminator
+  end;
+end;
+
 class function TStringUtils.BeginsWith(const Input, SubString: string;
 class function TStringUtils.BeginsWith(const Input, SubString: string;
   IgnoreCase: Boolean; Offset: Int32): Boolean;
   IgnoreCase: Boolean; Offset: Int32): Boolean;
 var
 var