2
0
Ugochukwu Mmaduekwe 7 жил өмнө
parent
commit
85aea1a965

+ 9 - 6
CryptoLib.Tests/src/Asn1/TagTests.pas

@@ -99,7 +99,7 @@ var
   app: IDerApplicationSpecific;
   app: IDerApplicationSpecific;
   tagged: IAsn1TaggedObject;
   tagged: IAsn1TaggedObject;
   sr: ISecureRandom;
   sr: ISecureRandom;
-  TestTag, I: Int32;
+  LTestTag, I: Int32;
 begin
 begin
 
 
   aIn := TAsn1InputStream.Create(FlongTagged);
   aIn := TAsn1InputStream.Create(FlongTagged);
@@ -172,19 +172,22 @@ begin
 
 
   sr := TSecureRandom.Create();
   sr := TSecureRandom.Create();
 
 
-  for I := 0 to System.Pred(100) do
+  I := 0;
+  while I < 100 do
   begin
   begin
-    TestTag := TBits.Asr32(sr.NextInt32() and System.High(Int32), sr.Next(26));
-    app := TDerApplicationSpecific.Create(TestTag, TBytes.Create(1));
+    LTestTag := TBits.Asr32(sr.NextInt32() and System.High(Int32), sr.Next(26));
+    app := TDerApplicationSpecific.Create(LTestTag, TBytes.Create(1));
     app := TAsn1Object.FromByteArray(app.GetEncoded())
     app := TAsn1Object.FromByteArray(app.GetEncoded())
       as IDerApplicationSpecific;
       as IDerApplicationSpecific;
 
 
-    if (app.ApplicationTag <> TestTag) then
+    if (app.ApplicationTag <> LTestTag) then
     begin
     begin
       Fail(Format
       Fail(Format
         ('incorrect tag number read on recode (random test value: " %d ")',
         ('incorrect tag number read on recode (random test value: " %d ")',
-        [TestTag]));
+        [LTestTag]));
     end;
     end;
+
+    System.Inc(I);
   end;
   end;
 end;
 end;
 
 

+ 19 - 0
CryptoLib/src/Security/ClpParameterUtilities.pas

@@ -26,8 +26,11 @@ uses
   Generics.Collections,
   Generics.Collections,
   ClpKeyParameter,
   ClpKeyParameter,
   ClpIKeyParameter,
   ClpIKeyParameter,
+  ClpICipherParameters,
+  ClpISecureRandom,
   ClpIDerObjectIdentifier,
   ClpIDerObjectIdentifier,
   ClpNistObjectIdentifiers,
   ClpNistObjectIdentifiers,
+  ClpParametersWithRandom,
   ClpCryptoLibTypes;
   ClpCryptoLibTypes;
 
 
 resourcestring
 resourcestring
@@ -71,6 +74,9 @@ type
       const keyBytes: TCryptoLibByteArray; offset, length: Int32)
       const keyBytes: TCryptoLibByteArray; offset, length: Int32)
       : IKeyParameter; overload; static;
       : IKeyParameter; overload; static;
 
 
+    class function WithRandom(const cp: ICipherParameters;
+      const random: ISecureRandom): ICipherParameters; static; inline;
+
     class procedure Boot(); static;
     class procedure Boot(); static;
 
 
   end;
   end;
@@ -137,6 +143,19 @@ begin
   Falgorithms.TryGetValue(UpperCase(algorithm), result);
   Falgorithms.TryGetValue(UpperCase(algorithm), result);
 end;
 end;
 
 
+class function TParameterUtilities.WithRandom(const cp: ICipherParameters;
+  const random: ISecureRandom): ICipherParameters;
+var
+  Lcp: ICipherParameters;
+begin
+  Lcp := cp;
+  if (random <> Nil) then
+  begin
+    Lcp := TParametersWithRandom.Create(Lcp, random);
+  end;
+  result := Lcp;
+end;
+
 class function TParameterUtilities.CreateKeyParameter(const algorithm: String;
 class function TParameterUtilities.CreateKeyParameter(const algorithm: String;
   const keyBytes: TCryptoLibByteArray): IKeyParameter;
   const keyBytes: TCryptoLibByteArray): IKeyParameter;
 begin
 begin

+ 27 - 0
CryptoLib/src/Security/ClpSignerUtilities.pas

@@ -43,11 +43,14 @@ uses
   ClpOiwObjectIdentifiers,
   ClpOiwObjectIdentifiers,
   ClpNistObjectIdentifiers,
   ClpNistObjectIdentifiers,
   ClpCryptoProObjectIdentifiers,
   ClpCryptoProObjectIdentifiers,
+  ClpParameterUtilities,
+  ClpIAsymmetricKeyParameter,
   ClpDsaSigner,
   ClpDsaSigner,
   ClpIDsaSigner,
   ClpIDsaSigner,
   ClpECDsaSigner,
   ClpECDsaSigner,
   ClpIECDsaSigner,
   ClpIECDsaSigner,
   ClpISigner,
   ClpISigner,
+  ClpISecureRandom,
   ClpIDerObjectIdentifier,
   ClpIDerObjectIdentifier,
   ClpCryptoLibTypes;
   ClpCryptoLibTypes;
 
 
@@ -95,6 +98,14 @@ type
 
 
     class function GetSigner(algorithm: String): ISigner; overload; static;
     class function GetSigner(algorithm: String): ISigner; overload; static;
 
 
+    class function InitSigner(const algorithm: String; forSigning: Boolean;
+      const privateKey: IAsymmetricKeyParameter; const random: ISecureRandom)
+      : ISigner; overload; static; inline;
+
+    class function InitSigner(const algorithmOid: IDerObjectIdentifier;
+      forSigning: Boolean; const privateKey: IAsymmetricKeyParameter;
+      const random: ISecureRandom): ISigner; overload; static; inline;
+
     class property Algorithms: TCryptoLibStringArray read GetAlgorithms;
     class property Algorithms: TCryptoLibStringArray read GetAlgorithms;
 
 
   end;
   end;
@@ -836,4 +847,20 @@ begin
 
 
 end;
 end;
 
 
+class function TSignerUtilities.InitSigner(const algorithm: String;
+  forSigning: Boolean; const privateKey: IAsymmetricKeyParameter;
+  const random: ISecureRandom): ISigner;
+begin
+  Result := TSignerUtilities.GetSigner(algorithm);
+  Result.Init(forSigning, TParameterUtilities.WithRandom(privateKey, random));
+end;
+
+class function TSignerUtilities.InitSigner(const algorithmOid
+  : IDerObjectIdentifier; forSigning: Boolean;
+  const privateKey: IAsymmetricKeyParameter;
+  const random: ISecureRandom): ISigner;
+begin
+  Result := InitSigner(algorithmOid.id, forSigning, privateKey, random);
+end;
+
 end.
 end.

+ 17 - 0
CryptoLib/src/Utils/ClpArrayUtils.pas

@@ -53,6 +53,9 @@ type
     class function AreEqual(const A, B: TCryptoLibInt32Array): Boolean;
     class function AreEqual(const A, B: TCryptoLibInt32Array): Boolean;
       overload; static;
       overload; static;
 
 
+    class function AreAllZeroes(const buf: TCryptoLibByteArray; off, len: Int32)
+      : Boolean; static;
+
     class function GetArrayHashCode(const data: TCryptoLibByteArray): Int32;
     class function GetArrayHashCode(const data: TCryptoLibByteArray): Int32;
       overload; static;
       overload; static;
 
 
@@ -163,6 +166,20 @@ begin
   Result := CompareMem(A, B, System.Length(A) * System.SizeOf(Byte));
   Result := CompareMem(A, B, System.Length(A) * System.SizeOf(Byte));
 end;
 end;
 
 
+class function TArrayUtils.AreAllZeroes(const buf: TCryptoLibByteArray;
+  off, len: Int32): Boolean;
+var
+  bits: UInt32;
+  i: Int32;
+begin
+  bits := 0;
+  for i := 0 to System.Pred(len) do
+  begin
+    bits := bits or (buf[off + i]);
+  end;
+  Result := bits = 0;
+end;
+
 class function TArrayUtils.AreEqual(const A, B: TCryptoLibInt32Array): Boolean;
 class function TArrayUtils.AreEqual(const A, B: TCryptoLibInt32Array): Boolean;
 begin
 begin
   if System.Length(A) <> System.Length(B) then
   if System.Length(A) <> System.Length(B) then

+ 35 - 3
CryptoLib/src/Utils/IO/ClpStreams.pas

@@ -67,10 +67,16 @@ type
     /// </returns>
     /// </returns>
     /// <exception cref="EStreamOverflowCryptoLibException" />
     /// <exception cref="EStreamOverflowCryptoLibException" />
     class function PipeAllLimited(const inStr: TStream; limit: Int64;
     class function PipeAllLimited(const inStr: TStream; limit: Int64;
-      const outStr: TStream): Int64;
+      const outStr: TStream): Int64; static;
 
 
-    class procedure WriteBufTo(const buf: TMemoryStream;
-      const output: TStream); inline;
+    class procedure WriteBufTo(const buf: TMemoryStream; const output: TStream);
+      overload; static; inline;
+
+    class function WriteBufTo(const buf: TMemoryStream;
+      const output: TCryptoLibByteArray; offset: Int32): Int32; overload;
+      static; inline;
+
+    class procedure WriteZeroes(const outStr: TStream; count: Int64); static;
 
 
   end;
   end;
 
 
@@ -187,6 +193,32 @@ begin
   Result := totalRead;
   Result := totalRead;
 end;
 end;
 
 
+class function TStreams.WriteBufTo(const buf: TMemoryStream;
+  const output: TCryptoLibByteArray; offset: Int32): Int32;
+var
+  bytes: TCryptoLibByteArray;
+begin
+  buf.Position := 0;
+  System.SetLength(bytes, buf.Size);
+  buf.Read(bytes[0], buf.Size);
+  System.Move(bytes[0], output[offset], System.Length(bytes) *
+    System.SizeOf(Byte));
+  Result := System.Length(bytes);
+end;
+
+class procedure TStreams.WriteZeroes(const outStr: TStream; count: Int64);
+var
+  zeroes: TCryptoLibByteArray;
+begin
+  System.SetLength(zeroes, BufferSize);
+  while (count > BufferSize) do
+  begin
+    outStr.Write(zeroes[0], BufferSize);
+    count := count - BufferSize;
+  end;
+  outStr.Write(zeroes[0], Int32(count));
+end;
+
 class function TStreams.ReadFully(const inStr: TStream;
 class function TStreams.ReadFully(const inStr: TStream;
   var buf: TCryptoLibByteArray): Int32;
   var buf: TCryptoLibByteArray): Int32;
 begin
 begin