Browse Source

code cleanup

Ugochukwu Mmaduekwe 6 years ago
parent
commit
0bea0d697d

+ 42 - 38
CryptoLib/src/Crypto/Engines/ClpIESEngine.pas

@@ -72,6 +72,9 @@ type
 
     procedure ExtractParams(const params: ICipherParameters); inline;
 
+    function SimilarMacCompute(const ArgOne, ArgTwo: TCryptoLibByteArray)
+      : TCryptoLibByteArray; inline;
+
   strict protected
 
   var
@@ -91,6 +94,8 @@ type
     function EncryptBlock(const &in: TCryptoLibByteArray; inOff, inLen: Int32)
       : TCryptoLibByteArray; virtual;
 
+    procedure SetupBlockCipherAndMacKeyBytes(out K1,
+      K2: TCryptoLibByteArray); inline;
     function DecryptBlock(const in_enc: TCryptoLibByteArray;
       inOff, inLen: Int32): TCryptoLibByteArray; virtual;
 
@@ -208,6 +213,37 @@ begin
   end;
 end;
 
+function TIESEngine.SimilarMacCompute(const ArgOne, ArgTwo: TCryptoLibByteArray)
+  : TCryptoLibByteArray;
+begin
+  if (ArgOne <> Nil) then
+  begin
+    Fmac.BlockUpdate(ArgOne, 0, System.Length(ArgOne));
+  end;
+  if (System.Length(FV) <> 0) then
+  begin
+    Fmac.BlockUpdate(ArgTwo, 0, System.Length(ArgTwo));
+  end;
+  Result := Fmac.DoFinal;
+end;
+
+procedure TIESEngine.SetupBlockCipherAndMacKeyBytes(out K1,
+  K2: TCryptoLibByteArray);
+var
+  K: TCryptoLibByteArray;
+begin
+  System.SetLength(K1, (Fparam as IIESWithCipherParameters)
+    .CipherKeySize div 8);
+  System.SetLength(K2, Fparam.MacKeySize div 8);
+  System.SetLength(K, System.Length(K1) + System.Length(K2));
+
+  Fkdf.GenerateBytes(K, 0, System.Length(K));
+
+  System.Move(K[0], K1[0], System.Length(K1) * System.SizeOf(Byte));
+  System.Move(K[System.Length(K1)], K2[0], System.Length(K2) *
+    System.SizeOf(Byte));
+end;
+
 constructor TIESEngine.Create(const agree: IBasicAgreement;
   const kdf: IDerivationFunction; const mac: IMac);
 begin
@@ -291,16 +327,7 @@ begin
   begin
     // Block cipher mode.
 
-    System.SetLength(K1, (Fparam as IIESWithCipherParameters)
-      .CipherKeySize div 8);
-    System.SetLength(K2, Fparam.MacKeySize div 8);
-    System.SetLength(K, System.Length(K1) + System.Length(K2));
-
-    Fkdf.GenerateBytes(K, 0, System.Length(K));
-
-    System.Move(K[0], K1[0], System.Length(K1) * System.SizeOf(Byte));
-    System.Move(K[System.Length(K1)], K2[0], System.Length(K2) *
-      System.SizeOf(Byte));
+    SetupBlockCipherAndMacKeyBytes(K1, K2);
 
     cp := TKeyParameter.Create(K1);
 
@@ -338,15 +365,8 @@ begin
 
   Fmac.BlockUpdate(in_enc, inOff + System.Length(FV), inLen - System.Length(FV)
     - System.Length(T2));
-  if (p2 <> Nil) then
-  begin
-    Fmac.BlockUpdate(p2, 0, System.Length(p2));
-  end;
-  if (System.Length(FV) <> 0) then
-  begin
-    Fmac.BlockUpdate(L2, 0, System.Length(L2));
-  end;
-  T2 := Fmac.DoFinal();
+
+  T2 := SimilarMacCompute(p2, L2);
 
   if (not TArrayUtils.ConstantTimeAreEqual(T1, T2)) then
   begin
@@ -416,16 +436,7 @@ begin
   begin
     // Block cipher mode.
 
-    System.SetLength(K1, (Fparam as IIESWithCipherParameters)
-      .CipherKeySize div 8);
-    System.SetLength(K2, Fparam.MacKeySize div 8);
-    System.SetLength(K, System.Length(K1) + System.Length(K2));
-
-    Fkdf.GenerateBytes(K, 0, System.Length(K));
-
-    System.Move(K[0], K1[0], System.Length(K1) * System.SizeOf(Byte));
-    System.Move(K[System.Length(K1)], K2[0], System.Length(K2) *
-      System.SizeOf(Byte));
+    SetupBlockCipherAndMacKeyBytes(K1, K2);
 
     // If iv is provided use it to initialise the cipher
     if (FIV <> Nil) then
@@ -457,15 +468,8 @@ begin
 
   Fmac.Init((TKeyParameter.Create(K2) as IKeyParameter) as ICipherParameters);
   Fmac.BlockUpdate(C, 0, System.Length(C));
-  if (p2 <> Nil) then
-  begin
-    Fmac.BlockUpdate(p2, 0, System.Length(p2));
-  end;
-  if (System.Length(FV) <> 0) then
-  begin
-    Fmac.BlockUpdate(L2, 0, System.Length(L2));
-  end;
-  T := Fmac.DoFinal;
+
+  T := SimilarMacCompute(p2, L2);
 
   // Output the triple (V,C,T).
   // V := Ephermeral Public Key

+ 4 - 22
CryptoLib/src/Crypto/Engines/ClpPascalCoinIESEngine.pas

@@ -98,7 +98,7 @@ implementation
 function TPascalCoinIESEngine.DecryptBlock(const in_enc: TCryptoLibByteArray;
   inOff, inLen: Int32): TCryptoLibByteArray;
 var
-  K, K1, K2, T1, T2: TCryptoLibByteArray;
+  K1, K2, T1, T2: TCryptoLibByteArray;
   cp: ICipherParameters;
 begin
   // Ensure that the length of the input is greater than the MAC in bytes
@@ -117,16 +117,7 @@ begin
   begin
     // Block cipher mode.
 
-    System.SetLength(K1, (Fparam as IIESWithCipherParameters)
-      .CipherKeySize div 8);
-    System.SetLength(K2, Fparam.MacKeySize div 8);
-    System.SetLength(K, System.Length(K1) + System.Length(K2));
-
-    Fkdf.GenerateBytes(K, 0, System.Length(K));
-
-    System.Move(K[0], K1[0], System.Length(K1) * System.SizeOf(Byte));
-    System.Move(K[System.Length(K1)], K2[0], System.Length(K2) *
-      System.SizeOf(Byte));
+    SetupBlockCipherAndMacKeyBytes(K1, K2);
 
     cp := TKeyParameter.Create(K1);
 
@@ -164,7 +155,7 @@ end;
 function TPascalCoinIESEngine.EncryptBlock(const &in: TCryptoLibByteArray;
   inOff, inLen: Int32): TCryptoLibByteArray;
 var
-  C, K, K1, K2, T: TCryptoLibByteArray;
+  C, K1, K2, T: TCryptoLibByteArray;
   MessageToEncryptPadSize, CipherBlockSize, MessageToEncryptSize: Int32;
 begin
   if (Fcipher = Nil) then
@@ -176,16 +167,7 @@ begin
   begin
     // Block cipher mode.
 
-    System.SetLength(K1, (Fparam as IIESWithCipherParameters)
-      .CipherKeySize div 8);
-    System.SetLength(K2, Fparam.MacKeySize div 8);
-    System.SetLength(K, System.Length(K1) + System.Length(K2));
-
-    Fkdf.GenerateBytes(K, 0, System.Length(K));
-
-    System.Move(K[0], K1[0], System.Length(K1) * System.SizeOf(Byte));
-    System.Move(K[System.Length(K1)], K2[0], System.Length(K2) *
-      System.SizeOf(Byte));
+    SetupBlockCipherAndMacKeyBytes(K1, K2);
 
     // If iv is provided use it to initialise the cipher
     if (FIV <> Nil) then

+ 7 - 23
CryptoLib/src/Crypto/Generators/ClpPascalCoinECIESKdfBytesGenerator.pas

@@ -46,9 +46,6 @@ type
   TPascalCoinECIESKdfBytesGenerator = class(TBaseKdfBytesGenerator,
     IPascalCoinECIESKdfBytesGenerator)
 
-  strict protected
-    function GetDigest(): IDigest; override;
-
   public
 
     /// <summary>
@@ -61,11 +58,6 @@ type
 
     procedure Init(const parameters: IDerivationParameters); override;
 
-    /// <summary>
-    /// return the underlying digest.
-    /// </summary>
-    property digest: IDigest read GetDigest;
-
     /// <summary>
     /// fill len bytes of the output buffer with bytes generated from the
     /// derivation function.
@@ -94,7 +86,6 @@ function TPascalCoinECIESKdfBytesGenerator.GenerateBytes
   (const output: TCryptoLibByteArray; outOff, length: Int32): Int32;
 var
   outLen: Int32;
-  oBytes: Int64;
   temp: TCryptoLibByteArray;
 begin
   if ((System.length(output) - length) < outOff) then
@@ -102,32 +93,26 @@ begin
     raise EDataLengthCryptoLibException.CreateRes(@SOutputBufferTooSmall);
   end;
 
-  oBytes := length;
-  outLen := Fdigest.GetDigestSize;
+  outLen := digest.GetDigestSize;
 
-  if (oBytes > outLen) then
+  if (length > outLen) then
   begin
     raise EDataLengthCryptoLibException.CreateRes
       (@SHashCannotNotProduceSufficientData);
   end;
 
-  System.SetLength(temp, Fdigest.GetDigestSize);
-  Fdigest.BlockUpdate(Fshared, 0, System.length(Fshared));
-  Fdigest.DoFinal(temp, 0);
+  System.SetLength(temp, digest.GetDigestSize);
+  digest.BlockUpdate(Fshared, 0, System.length(Fshared));
+  digest.DoFinal(temp, 0);
 
   System.Move(temp[0], output[outOff], length * System.SizeOf(Byte));
 
-  Fdigest.Reset();
+  digest.Reset();
 
-  result := oBytes;
+  result := length;
 
 end;
 
-function TPascalCoinECIESKdfBytesGenerator.GetDigest: IDigest;
-begin
-  result := Fdigest;
-end;
-
 procedure TPascalCoinECIESKdfBytesGenerator.Init(const parameters
   : IDerivationParameters);
 var
@@ -139,7 +124,6 @@ begin
   if Supports(Lparameters, IKdfParameters, p1) then
   begin
     Fshared := p1.GetSharedSecret();
-    Fiv := p1.GetIV();
   end
   else
   begin