Browse Source

Merge upstream.

Herman Schoenfeld 6 years ago
parent
commit
f0a2deca34

+ 3 - 3
PIP/PIP-0029.md

@@ -48,7 +48,7 @@ Under this interpretation, the chain of account states (or a PASA-Chain) is used
 
 
 In essence, PascalCoin's Proof-of-Work is enough to secure millions of blockchains (1 for each account) at no extra financial or computational cost. An astonishing result, and the first of it's kind for any cryptocurrency. 
 In essence, PascalCoin's Proof-of-Work is enough to secure millions of blockchains (1 for each account) at no extra financial or computational cost. An astonishing result, and the first of it's kind for any cryptocurrency. 
 
 
-See [Embedded Chain Specification](###Embedded-Chain-Specification) for more details.
+See [Embedded Chain Specification](#embedded-chain-specification) for more details.
 
 
 ### PASA as a Decentralised Finite-State-Machine
 ### PASA as a Decentralised Finite-State-Machine
 
 
@@ -132,7 +132,7 @@ In order to prove an prior account state is valid, the user needs a copy of that
 
 
    TAccountProof = array of StateProof;
    TAccountProof = array of StateProof;
 
 
-   function IsValidAccountProof(Proof : TAccountProof) 
+   function IsValidAccountProof(Proof : TAccountProof) : Boolean;
    begin
    begin
       if (proof[Length(proof) - 1].AccountState = SafeBox.GetAccount(account.Number)) AND IsValidProofChain(Proof) then
       if (proof[Length(proof) - 1].AccountState = SafeBox.GetAccount(account.Number)) AND IsValidProofChain(Proof) then
          return true;
          return true;
@@ -149,7 +149,7 @@ In order to prove an prior account state is valid, the user needs a copy of that
      return true;    
      return true;    
    end;
    end;
    
    
-   fuction CalculateSeal(proofLink : TStateProof ) : TBytes[32]
+   function CalculateSeal(proofLink : TStateProof ) : TBytes[32]
    begin
    begin
       return RIPEMD160 ( SHA2_256 ( Array.Join( SerializeAccount ( proofLink.AccountState ) , proofLink.OPID  ) ) );
       return RIPEMD160 ( SHA2_256 ( Array.Join( SerializeAccount ( proofLink.AccountState ) , proofLink.OPID  ) ) );
    end
    end

+ 8 - 8
src/core/UPCCryptoLib4Pascal.pas

@@ -49,7 +49,7 @@ Uses SysUtils, UBaseTypes, UPCDataTypes,
   ClpIX9ECParameters,
   ClpIX9ECParameters,
   ClpIIESEngine,
   ClpIIESEngine,
   ClpIBaseKdfBytesGenerator,
   ClpIBaseKdfBytesGenerator,
-  ClpIIESWithCipherParameters,
+  ClpIIESParameterSpec,
   ClpIPascalCoinECIESKdfBytesGenerator,
   ClpIPascalCoinECIESKdfBytesGenerator,
   ClpIPascalCoinIESEngine
   ClpIPascalCoinIESEngine
   ;
   ;
@@ -73,7 +73,7 @@ Type
     class var FCurve_SECT283K1 : IX9ECParameters;
     class var FCurve_SECT283K1 : IX9ECParameters;
     class var FCurve_SECP521R1 : IX9ECParameters;
     class var FCurve_SECP521R1 : IX9ECParameters;
     class var FPascalCoinIESEngine : IPascalCoinIESEngine;
     class var FPascalCoinIESEngine : IPascalCoinIESEngine;
-    class var FPascalCoinIESWithCipherParameters : IIESWithCipherParameters;
+    class var FIESParameterSpec : IIESParameterSpec;
     class constructor TPCCryptoLib4Pascal();
     class constructor TPCCryptoLib4Pascal();
     class function GetCurveAndDomainParameters(const AEC_OpenSSL_NID : Word; var OCurve : IX9ECParameters; var ODomain : IECDomainParameters; ARaiseIfNotForPascal : Boolean = True ) : Boolean;
     class function GetCurveAndDomainParameters(const AEC_OpenSSL_NID : Word; var OCurve : IX9ECParameters; var ODomain : IECDomainParameters; ARaiseIfNotForPascal : Boolean = True ) : Boolean;
     class function GetDomainParameters(const AEC_OpenSSL_NID : Word) : IECDomainParameters;
     class function GetDomainParameters(const AEC_OpenSSL_NID : Word) : IECDomainParameters;
@@ -115,7 +115,7 @@ Uses
   ClpIAesEngine,
   ClpIAesEngine,
   ClpIBlockCipherModes,
   ClpIBlockCipherModes,
   ClpIBasicAgreement,
   ClpIBasicAgreement,
-  ClpIESWithCipherParameters,
+  ClpIESParameterSpec,
   ClpIECDHBasicAgreement,
   ClpIECDHBasicAgreement,
   ClpIMac,
   ClpIMac,
   ClpECDHBasicAgreement,
   ClpECDHBasicAgreement,
@@ -349,7 +349,7 @@ begin
 
 
     // Decryption
     // Decryption
     LCipherDecrypt := TIESCipher.Create(FPascalCoinIESEngine);
     LCipherDecrypt := TIESCipher.Create(FPascalCoinIESEngine);
-    LCipherDecrypt.Init(False, LPrivKeyParams, FPascalCoinIESWithCipherParameters, FRandom);
+    LCipherDecrypt.Init(False, LPrivKeyParams, FIESParameterSpec, FRandom);
     ADecryptedMessage := System.Copy(LCipherDecrypt.DoFinal(AEncryptedMessage));
     ADecryptedMessage := System.Copy(LCipherDecrypt.DoFinal(AEncryptedMessage));
     Result := True;
     Result := True;
   except
   except
@@ -376,7 +376,7 @@ begin
   LPubKeyParams := TECPublicKeyParameters.Create('ECDSA', LPoint, LDomain);
   LPubKeyParams := TECPublicKeyParameters.Create('ECDSA', LPoint, LDomain);
   // Encryption
   // Encryption
   LCipherEncrypt := TIESCipher.Create(FPascalCoinIESEngine);
   LCipherEncrypt := TIESCipher.Create(FPascalCoinIESEngine);
-  LCipherEncrypt.Init(True, LPubKeyParams, FPascalCoinIESWithCipherParameters, FRandom);
+  LCipherEncrypt.Init(True, LPubKeyParams, FIESParameterSpec, FRandom);
   AEncryptedMessage := LCipherEncrypt.DoFinal(AMessage);
   AEncryptedMessage := LCipherEncrypt.DoFinal(AMessage);
   Result := True;
   Result := True;
 end;
 end;
@@ -485,7 +485,7 @@ begin
 end;
 end;
 
 
 class constructor TPCCryptoLib4Pascal.TPCCryptoLib4Pascal;
 class constructor TPCCryptoLib4Pascal.TPCCryptoLib4Pascal;
-    function GetIESCipherParameters: IIESWithCipherParameters;
+    function GetIESParameterSpec: IIESParameterSpec;
     var
     var
       Derivation, Encoding, IVBytes: TBytes;
       Derivation, Encoding, IVBytes: TBytes;
       MacKeySizeInBits, CipherKeySizeInBits: Int32;
       MacKeySizeInBits, CipherKeySizeInBits: Int32;
@@ -512,7 +512,7 @@ class constructor TPCCryptoLib4Pascal.TPCCryptoLib4Pascal;
       // from a point or not in the EphemeralKeyPairGenerator
       // from a point or not in the EphemeralKeyPairGenerator
       UsePointCompression := True; // for compatibility
       UsePointCompression := True; // for compatibility
 
 
-      Result := TIESWithCipherParameters.Create(Derivation, Encoding,
+      Result := TIESParameterSpec.Create(Derivation, Encoding,
         MacKeySizeInBits, CipherKeySizeInBits, IVBytes, UsePointCompression);
         MacKeySizeInBits, CipherKeySizeInBits, IVBytes, UsePointCompression);
     end;
     end;
     function GetECIESPascalCoinCompatibilityEngine(): IPascalCoinIESEngine;
     function GetECIESPascalCoinCompatibilityEngine(): IPascalCoinIESEngine;
@@ -564,7 +564,7 @@ begin
   FDomain_SECP521R1 := TECDomainParameters.Create(FCurve_SECP521R1.Curve, FCurve_SECP521R1.G, FCurve_SECP521R1.N, FCurve_SECP521R1.H, FCurve_SECP521R1.GetSeed);
   FDomain_SECP521R1 := TECDomainParameters.Create(FCurve_SECP521R1.Curve, FCurve_SECP521R1.G, FCurve_SECP521R1.N, FCurve_SECP521R1.H, FCurve_SECP521R1.GetSeed);
   // Init ECIES
   // Init ECIES
   FPascalCoinIESEngine := GetECIESPascalCoinCompatibilityEngine;
   FPascalCoinIESEngine := GetECIESPascalCoinCompatibilityEngine;
-  FPascalCoinIESWithCipherParameters := GetIESCipherParameters;
+  FIESParameterSpec := GetIESParameterSpec;
 
 
 end;
 end;
 
 

+ 1 - 0
src/libraries/cryptolib4pascal/ClpCipherUtilities.pas

@@ -217,6 +217,7 @@ begin
   parts := TStringUtils.SplitString(algorithm, '/');
   parts := TStringUtils.SplitString(algorithm, '/');
 
 
   blockCipher := Nil;
   blockCipher := Nil;
+  streamCipher := Nil;
 
 
   algorithmName := parts[0];
   algorithmName := parts[0];
 
 

+ 31 - 0
src/libraries/cryptolib4pascal/ClpIAlgorithmParameterSpec.pas

@@ -0,0 +1,31 @@
+{ *********************************************************************************** }
+{ *                              CryptoLib Library                                  * }
+{ *                Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe                    * }
+{ *                 Github Repository <https://github.com/Xor-el>                   * }
+
+{ *  Distributed under the MIT software license, see the accompanying file LICENSE  * }
+{ *          or visit http://www.opensource.org/licenses/mit-license.php.           * }
+
+{ *                              Acknowledgements:                                  * }
+{ *                                                                                 * }
+{ *      Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring     * }
+{ *                           development of this library                           * }
+
+{ * ******************************************************************************* * }
+
+(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
+
+unit ClpIAlgorithmParameterSpec;
+
+{$I CryptoLib.inc}
+
+interface
+
+type
+  IAlgorithmParameterSpec = interface(IInterface)
+    ['{FBA69725-AEFF-4B99-92C0-1819E5DE2DA1}']
+  end;
+
+implementation
+
+end.

+ 1 - 1
src/libraries/cryptolib4pascal/ClpIDsaKeyParameters.pas

@@ -31,7 +31,7 @@ type
 
 
     function GetParameters: IDsaParameters;
     function GetParameters: IDsaParameters;
 
 
-    function Equals(const other: IDsaKeyParameters): Boolean;
+    function Equals(const other: IDsaKeyParameters): Boolean; overload;
     property parameters: IDsaParameters read GetParameters;
     property parameters: IDsaParameters read GetParameters;
 
 
   end;
   end;

+ 1 - 1
src/libraries/cryptolib4pascal/ClpIDsaPrivateKeyParameters.pas

@@ -31,7 +31,7 @@ type
 
 
     function GetX: TBigInteger;
     function GetX: TBigInteger;
 
 
-    function Equals(const other: IDsaPrivateKeyParameters): Boolean;
+    function Equals(const other: IDsaPrivateKeyParameters): Boolean; overload;
     property X: TBigInteger read GetX;
     property X: TBigInteger read GetX;
 
 
   end;
   end;

+ 1 - 1
src/libraries/cryptolib4pascal/ClpIDsaPublicKeyParameters.pas

@@ -28,7 +28,7 @@ type
 
 
     function GetY: TBigInteger;
     function GetY: TBigInteger;
 
 
-    function Equals(const other: IDsaPublicKeyParameters): Boolean;
+    function Equals(const other: IDsaPublicKeyParameters): Boolean; overload;
     property y: TBigInteger read GetY;
     property y: TBigInteger read GetY;
 
 
   end;
   end;

+ 2 - 0
src/libraries/cryptolib4pascal/ClpIECKeyParameters.pas

@@ -35,6 +35,8 @@ type
     function GetPublicKeyParamSet: IDerObjectIdentifier;
     function GetPublicKeyParamSet: IDerObjectIdentifier;
     function GetParameters: IECDomainParameters;
     function GetParameters: IECDomainParameters;
 
 
+    function Equals(const other: IECKeyParameters): Boolean; overload;
+
     property AlgorithmName: String read GetAlgorithmName;
     property AlgorithmName: String read GetAlgorithmName;
     property PublicKeyParamSet: IDerObjectIdentifier read GetPublicKeyParamSet;
     property PublicKeyParamSet: IDerObjectIdentifier read GetPublicKeyParamSet;
     property Parameters: IECDomainParameters read GetParameters;
     property Parameters: IECDomainParameters read GetParameters;

+ 1 - 0
src/libraries/cryptolib4pascal/ClpIECPrivateKeyParameters.pas

@@ -30,6 +30,7 @@ type
   IECPrivateKeyParameters = interface(IECKeyParameters)
   IECPrivateKeyParameters = interface(IECKeyParameters)
     ['{49066428-4021-4E3C-A9F5-AB2127289A67}']
     ['{49066428-4021-4E3C-A9F5-AB2127289A67}']
 
 
+    function Equals(const other: IECPrivateKeyParameters): Boolean; overload;
     function GetD: TBigInteger;
     function GetD: TBigInteger;
     property D: TBigInteger read GetD;
     property D: TBigInteger read GetD;
   end;
   end;

+ 1 - 1
src/libraries/cryptolib4pascal/ClpIECPublicKeyParameters.pas

@@ -30,8 +30,8 @@ type
   IECPublicKeyParameters = interface(IECKeyParameters)
   IECPublicKeyParameters = interface(IECKeyParameters)
     ['{4BABC163-847A-4FE2-AA16-5CD100F76124}']
     ['{4BABC163-847A-4FE2-AA16-5CD100F76124}']
 
 
+    function Equals(const other: IECPublicKeyParameters): Boolean; overload;
     function GetQ: IECPoint;
     function GetQ: IECPoint;
-
     property Q: IECPoint read GetQ;
     property Q: IECPoint read GetQ;
   end;
   end;
 
 

+ 21 - 21
src/libraries/cryptolib4pascal/ClpIESCipher.pas

@@ -28,7 +28,8 @@ uses
   ClpIECIESPublicKeyParser,
   ClpIECIESPublicKeyParser,
   ClpECIESPublicKeyParser,
   ClpECIESPublicKeyParser,
   ClpIAsymmetricKeyParameter,
   ClpIAsymmetricKeyParameter,
-  ClpIIESWithCipherParameters,
+  ClpIAlgorithmParameterSpec,
+  ClpIIESParameterSpec,
   ClpICipherParameters,
   ClpICipherParameters,
   ClpIParametersWithRandom,
   ClpIParametersWithRandom,
   ClpIECKeyParameters,
   ClpIECKeyParameters,
@@ -53,10 +54,10 @@ resourcestring
     'Must be Passed Recipient''s Public EC Key for Encryption';
     'Must be Passed Recipient''s Public EC Key for Encryption';
   SInvalidPrivateKey =
   SInvalidPrivateKey =
     'Must be Passed Recipient''s Private EC Key for Decryption';
     'Must be Passed Recipient''s Private EC Key for Decryption';
-  SIESCipherParameterNil = 'IES Cipher Parameters Cannot Be Nil';
+  SIAlgorithmParameterSpecNil = 'Parameter Spec Cannot Be Nil';
   SUnableToProcessBlock = 'Unable to Process Block. "%s"';
   SUnableToProcessBlock = 'Unable to Process Block. "%s"';
-  SIESCipherParameterError = 'IES Cipher Parameter Error';
-  SNonceInvalidLength = 'NONCE in IES Parameters Needs to be "%s" Bytes Long';
+  SIESParameterSpecError = 'Must be Passed IES Parameter Spec';
+  SNonceInvalidLength = 'Nonce in IES Parameters Needs to be "%s" Bytes Long';
 
 
 type
 type
   TIESCipher = class sealed(TInterfacedObject, IIESCipher)
   TIESCipher = class sealed(TInterfacedObject, IIESCipher)
@@ -67,7 +68,7 @@ type
     FEngine: IIESEngine;
     FEngine: IIESEngine;
     FForEncryption: Boolean;
     FForEncryption: Boolean;
     FBuffer: TMemoryStream;
     FBuffer: TMemoryStream;
-    FIESCipherParameters: IIESWithCipherParameters;
+    FEngineSpec: IIESParameterSpec;
     Fkey: IAsymmetricKeyParameter;
     Fkey: IAsymmetricKeyParameter;
     FRandom: ISecureRandom;
     FRandom: ISecureRandom;
 
 
@@ -75,8 +76,7 @@ type
 
 
   public
   public
     procedure Init(ForEncryption: Boolean; const Key: ICipherParameters;
     procedure Init(ForEncryption: Boolean; const Key: ICipherParameters;
-      const IESCipherParameters: IIESWithCipherParameters;
-      const Random: ISecureRandom);
+      const EngineSpec: IAlgorithmParameterSpec; const Random: ISecureRandom);
 
 
     procedure ProcessBytes(const input: TCryptoLibByteArray); overload;
     procedure ProcessBytes(const input: TCryptoLibByteArray); overload;
     procedure ProcessBytes(const input: TCryptoLibByteArray;
     procedure ProcessBytes(const input: TCryptoLibByteArray;
@@ -142,13 +142,13 @@ begin
   FBuffer.SetSize(Int64(0));
   FBuffer.SetSize(Int64(0));
 
 
   // Convert parameters for use in IESEngine
   // Convert parameters for use in IESEngine
-  params := TIESWithCipherParameters.Create(FIESCipherParameters.GetDerivationV,
-    FIESCipherParameters.GetEncodingV, FIESCipherParameters.MacKeySize,
-    FIESCipherParameters.CipherKeySize);
+  params := TIESWithCipherParameters.Create(FEngineSpec.GetDerivationV,
+    FEngineSpec.GetEncodingV, FEngineSpec.MacKeySize,
+    FEngineSpec.CipherKeySize);
 
 
-  if (FIESCipherParameters.Nonce <> Nil) then
+  if (FEngineSpec.Nonce <> Nil) then
   begin
   begin
-    params := TParametersWithIV.Create(params, FIESCipherParameters.Nonce);
+    params := TParametersWithIV.Create(params, FEngineSpec.Nonce);
   end;
   end;
   ecParams := (Fkey as IECKeyParameters).Parameters;
   ecParams := (Fkey as IECKeyParameters).Parameters;
 
 
@@ -159,7 +159,7 @@ begin
     gen.Init(TECKeyGenerationParameters.Create(ecParams, FRandom)
     gen.Init(TECKeyGenerationParameters.Create(ecParams, FRandom)
       as IECKeyGenerationParameters);
       as IECKeyGenerationParameters);
 
 
-    UsePointCompression := FIESCipherParameters.PointCompression;
+    UsePointCompression := FEngineSpec.PointCompression;
 
 
     kGen := TEphemeralKeyPairGenerator.Create(gen,
     kGen := TEphemeralKeyPairGenerator.Create(gen,
       TKeyEncoder.Create(UsePointCompression) as IKeyEncoder);
       TKeyEncoder.Create(UsePointCompression) as IKeyEncoder);
@@ -225,8 +225,7 @@ begin
 end;
 end;
 
 
 procedure TIESCipher.Init(ForEncryption: Boolean; const Key: ICipherParameters;
 procedure TIESCipher.Init(ForEncryption: Boolean; const Key: ICipherParameters;
-  const IESCipherParameters: IIESWithCipherParameters;
-  const Random: ISecureRandom);
+  const EngineSpec: IAlgorithmParameterSpec; const Random: ISecureRandom);
 var
 var
   LKey: ICipherParameters;
   LKey: ICipherParameters;
   Nonce: TCryptoLibByteArray;
   Nonce: TCryptoLibByteArray;
@@ -234,21 +233,22 @@ begin
 
 
   FForEncryption := ForEncryption;
   FForEncryption := ForEncryption;
 
 
-  if (IESCipherParameters = Nil) then
+  if (EngineSpec = Nil) then
   begin
   begin
-    raise EArgumentNilCryptoLibException.CreateRes(@SIESCipherParameterNil);
+    raise EArgumentNilCryptoLibException.CreateRes
+      (@SIAlgorithmParameterSpecNil);
   end
   end
-  else if (Supports(IESCipherParameters, IIESWithCipherParameters)) then
+  else if (Supports(EngineSpec, IIESParameterSpec)) then
   begin
   begin
-    FIESCipherParameters := IESCipherParameters as IIESWithCipherParameters;
+    FEngineSpec := EngineSpec as IIESParameterSpec;
   end
   end
   else
   else
   begin
   begin
     raise EInvalidParameterCryptoLibException.CreateRes
     raise EInvalidParameterCryptoLibException.CreateRes
-      (@SIESCipherParameterError);
+      (@SIESParameterSpecError);
   end;
   end;
 
 
-  Nonce := FIESCipherParameters.Nonce;
+  Nonce := FEngineSpec.Nonce;
 
 
   if ((FivLength <> 0) and ((Nonce = Nil) or (System.length(Nonce) <>
   if ((FivLength <> 0) and ((Nonce = Nil) or (System.length(Nonce) <>
     FivLength))) then
     FivLength))) then

+ 42 - 38
src/libraries/cryptolib4pascal/ClpIESEngine.pas

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

+ 205 - 0
src/libraries/cryptolib4pascal/ClpIESParameterSpec.pas

@@ -0,0 +1,205 @@
+{ *********************************************************************************** }
+{ *                              CryptoLib Library                                  * }
+{ *                Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe                    * }
+{ *                 Github Repository <https://github.com/Xor-el>                   * }
+
+{ *  Distributed under the MIT software license, see the accompanying file LICENSE  * }
+{ *          or visit http://www.opensource.org/licenses/mit-license.php.           * }
+
+{ *                              Acknowledgements:                                  * }
+{ *                                                                                 * }
+{ *      Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring     * }
+{ *                           development of this library                           * }
+
+{ * ******************************************************************************* * }
+
+(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
+
+unit ClpIESParameterSpec;
+
+{$I CryptoLib.inc}
+
+interface
+
+uses
+  ClpIIESParameterSpec,
+  ClpIAlgorithmParameterSpec,
+  ClpCryptoLibTypes;
+
+type
+
+  /// <summary>
+  /// Parameter spec for an integrated encryptor, as in IEEE P1363a
+  /// </summary>
+  TIESParameterSpec = class sealed(TInterfacedObject, IAlgorithmParameterSpec,
+    IIESParameterSpec)
+
+  strict private
+  var
+    Fderivation, Fencoding, FNonce: TCryptoLibByteArray;
+    FmacKeySize, FcipherKeySize: Int32;
+    FusePointCompression: Boolean;
+
+  strict private
+    function GetDerivationV: TCryptoLibByteArray; inline;
+    function GetEncodingV: TCryptoLibByteArray; inline;
+    function GetMacKeySize: Int32; inline;
+    function GetCipherKeySize: Int32; inline;
+    function GetNonce: TCryptoLibByteArray; inline;
+    function GetPointCompression: Boolean; inline;
+
+  public
+
+    /// <summary>
+    /// Set the IES engine parameters.
+    /// </summary>
+    /// <param name="derivation">
+    /// the optional derivation vector for the KDF.
+    /// </param>
+    /// <param name="encoding">
+    /// the optional encoding vector for the KDF.
+    /// </param>
+    /// <param name="macKeySize">
+    /// the key size (in bits) for the MAC.
+    /// </param>
+    /// <param name="CipherKeySize">
+    /// the key size (in bits) for the block cipher.
+    /// </param>
+    /// <param name="Nonce">
+    /// an IV to use initialising the block cipher.
+    /// </param>
+    constructor Create(const derivation, encoding: TCryptoLibByteArray;
+      MacKeySize, CipherKeySize: Int32;
+      const Nonce: TCryptoLibByteArray); overload;
+
+    /// <summary>
+    /// Set the IES engine parameters.
+    /// </summary>
+    /// <param name="derivation">
+    /// the optional derivation vector for the KDF.
+    /// </param>
+    /// <param name="encoding">
+    /// the optional encoding vector for the KDF.
+    /// </param>
+    /// <param name="macKeySize">
+    /// the key size (in bits) for the MAC.
+    /// </param>
+    /// <param name="CipherKeySize">
+    /// the key size (in bits) for the block cipher.
+    /// </param>
+    /// <param name="Nonce">
+    /// an IV to use initialising the block cipher.
+    /// </param>
+    /// <param name="UsePointCompression">
+    /// whether to use EC point compression or not (false by default)
+    /// </param>
+    constructor Create(const derivation, encoding: TCryptoLibByteArray;
+      MacKeySize: Int32; CipherKeySize: Int32 = -1;
+      const Nonce: TCryptoLibByteArray = Nil;
+      UsePointCompression: Boolean = False); overload;
+
+    /// <summary>
+    /// Returns the derivation vector.
+    /// </summary>
+    /// <value>
+    /// the derivation vector.
+    /// </value>
+    property DerivationV: TCryptoLibByteArray read GetDerivationV;
+
+    /// <summary>
+    /// Returns the encoding vector.
+    /// </summary>
+    /// <value>
+    /// the encoding vector.
+    /// </value>
+    property EncodingV: TCryptoLibByteArray read GetEncodingV;
+
+    /// <summary>
+    /// Return the key size in bits for the MAC used with the message
+    /// </summary>
+    /// <value>
+    /// the key size in bits for the MAC used with the message
+    /// </value>
+    property MacKeySize: Int32 read GetMacKeySize;
+
+    /// <summary>
+    /// Return the key size in bits for the block cipher used with the message
+    /// </summary>
+    /// <value>
+    /// the key size in bits for the block cipher used with the message
+    /// </value>
+    property CipherKeySize: Int32 read GetCipherKeySize;
+
+    /// <summary>
+    /// Return the Nonce (IV) value to be associated with message.
+    /// </summary>
+    /// <value>
+    /// block cipher IV for message.
+    /// </value>
+    property Nonce: TCryptoLibByteArray read GetNonce;
+
+    /// <summary>
+    /// Return the 'point compression' flag.
+    /// </summary>
+    /// <value>
+    /// the point compression flag
+    /// </value>
+    property PointCompression: Boolean read GetPointCompression;
+
+  end;
+
+implementation
+
+{ TIESParameterSpec }
+
+constructor TIESParameterSpec.Create(const derivation,
+  encoding: TCryptoLibByteArray; MacKeySize, CipherKeySize: Int32;
+  const Nonce: TCryptoLibByteArray);
+begin
+  Create(derivation, encoding, MacKeySize, CipherKeySize, Nonce, False);
+end;
+
+constructor TIESParameterSpec.Create(const derivation,
+  encoding: TCryptoLibByteArray; MacKeySize: Int32; CipherKeySize: Int32;
+  const Nonce: TCryptoLibByteArray; UsePointCompression: Boolean);
+begin
+  Inherited Create();
+  Fderivation := derivation;
+  Fencoding := encoding;
+  FmacKeySize := MacKeySize;
+  FcipherKeySize := CipherKeySize;
+  FNonce := System.Copy(Nonce);
+  FusePointCompression := UsePointCompression;
+end;
+
+function TIESParameterSpec.GetCipherKeySize: Int32;
+begin
+  result := FcipherKeySize;
+end;
+
+function TIESParameterSpec.GetDerivationV: TCryptoLibByteArray;
+begin
+  result := System.Copy(Fderivation);
+end;
+
+function TIESParameterSpec.GetEncodingV: TCryptoLibByteArray;
+begin
+  result := System.Copy(Fencoding);
+end;
+
+function TIESParameterSpec.GetMacKeySize: Int32;
+begin
+  result := FmacKeySize;
+end;
+
+function TIESParameterSpec.GetNonce: TCryptoLibByteArray;
+begin
+  result := System.Copy(FNonce);
+end;
+
+function TIESParameterSpec.GetPointCompression: Boolean;
+begin
+  result := FusePointCompression;
+end;
+
+end.

+ 1 - 94
src/libraries/cryptolib4pascal/ClpIESWithCipherParameters.pas

@@ -34,13 +34,9 @@ type
 
 
   strict private
   strict private
   var
   var
-    Fnonce: TCryptoLibByteArray;
     FcipherKeySize: Int32;
     FcipherKeySize: Int32;
-    FusePointCompression: Boolean;
 
 
     function GetCipherKeySize: Int32; inline;
     function GetCipherKeySize: Int32; inline;
-    function GetNonce: TCryptoLibByteArray; inline;
-    function GetPointCompression: Boolean; inline;
   public
   public
 
 
     /// <summary>
     /// <summary>
@@ -59,54 +55,7 @@ type
     /// the key size (in bits) for the block cipher.
     /// the key size (in bits) for the block cipher.
     /// </param>
     /// </param>
     constructor Create(const derivation, encoding: TCryptoLibByteArray;
     constructor Create(const derivation, encoding: TCryptoLibByteArray;
-      macKeySize, CipherKeySize: Int32); overload;
-
-    /// <summary>
-    /// Set the IES engine parameters.
-    /// </summary>
-    /// <param name="derivation">
-    /// the optional derivation vector for the KDF.
-    /// </param>
-    /// <param name="encoding">
-    /// the optional encoding vector for the KDF.
-    /// </param>
-    /// <param name="macKeySize">
-    /// the key size (in bits) for the MAC.
-    /// </param>
-    /// <param name="CipherKeySize">
-    /// the key size (in bits) for the block cipher.
-    /// </param>
-    /// <param name="nonce">
-    /// an IV to use initialising the block cipher.
-    /// </param>
-    constructor Create(const derivation, encoding: TCryptoLibByteArray;
-      macKeySize, CipherKeySize: Int32;
-      const nonce: TCryptoLibByteArray); overload;
-
-    /// <summary>
-    /// Set the IES engine parameters.
-    /// </summary>
-    /// <param name="derivation">
-    /// the optional derivation vector for the KDF.
-    /// </param>
-    /// <param name="encoding">
-    /// the optional encoding vector for the KDF.
-    /// </param>
-    /// <param name="macKeySize">
-    /// the key size (in bits) for the MAC.
-    /// </param>
-    /// <param name="CipherKeySize">
-    /// the key size (in bits) for the block cipher.
-    /// </param>
-    /// <param name="nonce">
-    /// an IV to use initialising the block cipher.
-    /// </param>
-    /// <param name="UsePointCompression">
-    /// whether to use EC point compression or not (false by default)
-    /// </param>
-    constructor Create(const derivation, encoding: TCryptoLibByteArray;
-      macKeySize, CipherKeySize: Int32; const nonce: TCryptoLibByteArray;
-      UsePointCompression: Boolean); overload;
+      macKeySize, CipherKeySize: Int32);
 
 
     /// <summary>
     /// <summary>
     /// Return the key size in bits for the block cipher used with the message
     /// Return the key size in bits for the block cipher used with the message
@@ -116,21 +65,6 @@ type
     /// </value>
     /// </value>
     property CipherKeySize: Int32 read GetCipherKeySize;
     property CipherKeySize: Int32 read GetCipherKeySize;
 
 
-    /// <summary>
-    /// Return the nonce (IV) value to be associated with message.
-    /// </summary>
-    /// <value>
-    /// block cipher IV for message.
-    /// </value>
-    property nonce: TCryptoLibByteArray read GetNonce;
-
-    /// <summary>
-    /// Return the 'point compression' flag.
-    /// </summary>
-    /// <value>
-    /// the point compression flag
-    /// </value>
-    property PointCompression: Boolean read GetPointCompression;
   end;
   end;
 
 
 implementation
 implementation
@@ -142,38 +76,11 @@ begin
   Result := FcipherKeySize;
   Result := FcipherKeySize;
 end;
 end;
 
 
-function TIESWithCipherParameters.GetNonce: TCryptoLibByteArray;
-begin
-  Result := System.Copy(Fnonce);
-end;
-
-function TIESWithCipherParameters.GetPointCompression: Boolean;
-begin
-  Result := FusePointCompression;
-end;
-
 constructor TIESWithCipherParameters.Create(const derivation,
 constructor TIESWithCipherParameters.Create(const derivation,
   encoding: TCryptoLibByteArray; macKeySize, CipherKeySize: Int32);
   encoding: TCryptoLibByteArray; macKeySize, CipherKeySize: Int32);
-begin
-  Create(derivation, encoding, macKeySize, CipherKeySize, Nil);
-end;
-
-constructor TIESWithCipherParameters.Create(const derivation,
-  encoding: TCryptoLibByteArray; macKeySize, CipherKeySize: Int32;
-  const nonce: TCryptoLibByteArray);
-begin
-  Create(derivation, encoding, macKeySize, CipherKeySize, nonce, false);
-end;
-
-constructor TIESWithCipherParameters.Create(const derivation,
-  encoding: TCryptoLibByteArray; macKeySize, CipherKeySize: Int32;
-  const nonce: TCryptoLibByteArray; UsePointCompression: Boolean);
 begin
 begin
   Inherited Create(derivation, encoding, macKeySize);
   Inherited Create(derivation, encoding, macKeySize);
-
   FcipherKeySize := CipherKeySize;
   FcipherKeySize := CipherKeySize;
-  Fnonce := System.Copy(nonce);
-  FusePointCompression := UsePointCompression;
 end;
 end;
 
 
 end.
 end.

+ 2 - 2
src/libraries/cryptolib4pascal/ClpIIESCipher.pas

@@ -24,7 +24,7 @@ interface
 uses
 uses
   ClpICipherParameters,
   ClpICipherParameters,
   ClpISecureRandom,
   ClpISecureRandom,
-  ClpIIESWithCipherParameters,
+  ClpIAlgorithmParameterSpec,
   ClpCryptoLibTypes;
   ClpCryptoLibTypes;
 
 
 type
 type
@@ -32,7 +32,7 @@ type
     ['{DD112FD3-844A-4EF0-B9B8-22AFAEFB0881}']
     ['{DD112FD3-844A-4EF0-B9B8-22AFAEFB0881}']
 
 
     procedure Init(ForEncryption: Boolean; const Key: ICipherParameters;
     procedure Init(ForEncryption: Boolean; const Key: ICipherParameters;
-      const engineSpec: IIESWithCipherParameters; const Random: ISecureRandom);
+      const engineSpec: IAlgorithmParameterSpec; const Random: ISecureRandom);
 
 
     procedure ProcessBytes(const input: TCryptoLibByteArray); overload;
     procedure ProcessBytes(const input: TCryptoLibByteArray); overload;
     procedure ProcessBytes(const input: TCryptoLibByteArray;
     procedure ProcessBytes(const input: TCryptoLibByteArray;

+ 90 - 0
src/libraries/cryptolib4pascal/ClpIIESParameterSpec.pas

@@ -0,0 +1,90 @@
+{ *********************************************************************************** }
+{ *                              CryptoLib Library                                  * }
+{ *                Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe                    * }
+{ *                 Github Repository <https://github.com/Xor-el>                   * }
+
+{ *  Distributed under the MIT software license, see the accompanying file LICENSE  * }
+{ *          or visit http://www.opensource.org/licenses/mit-license.php.           * }
+
+{ *                              Acknowledgements:                                  * }
+{ *                                                                                 * }
+{ *      Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring     * }
+{ *                           development of this library                           * }
+
+{ * ******************************************************************************* * }
+
+(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
+
+unit ClpIIESParameterSpec;
+
+{$I CryptoLib.inc}
+
+interface
+
+uses
+  ClpIAlgorithmParameterSpec,
+  ClpCryptoLibTypes;
+
+type
+  IIESParameterSpec = interface(IAlgorithmParameterSpec)
+    ['{F83CD14B-C049-4878-8D78-0214FD9D2B8A}']
+
+    /// <summary>
+    /// Returns the derivation vector.
+    /// </summary>
+    /// <value>
+    /// the derivation vector.
+    /// </value>
+    function GetDerivationV: TCryptoLibByteArray;
+    property DerivationV: TCryptoLibByteArray read GetDerivationV;
+
+    /// <summary>
+    /// Returns the encoding vector.
+    /// </summary>
+    /// <value>
+    /// the encoding vector.
+    /// </value>
+    function GetEncodingV: TCryptoLibByteArray;
+    property EncodingV: TCryptoLibByteArray read GetEncodingV;
+
+    /// <summary>
+    /// Return the key size in bits for the MAC used with the message
+    /// </summary>
+    /// <value>
+    /// the key size in bits for the MAC used with the message
+    /// </value>
+    function GetMacKeySize: Int32;
+    property MacKeySize: Int32 read GetMacKeySize;
+
+    /// <summary>
+    /// Return the key size in bits for the block cipher used with the message
+    /// </summary>
+    /// <value>
+    /// the key size in bits for the block cipher used with the message
+    /// </value>
+    function GetCipherKeySize: Int32;
+    property CipherKeySize: Int32 read GetCipherKeySize;
+
+    /// <summary>
+    /// Return the Nonce (IV) value to be associated with message.
+    /// </summary>
+    /// <value>
+    /// block cipher IV for message.
+    /// </value>
+    function GetNonce: TCryptoLibByteArray;
+    property Nonce: TCryptoLibByteArray read GetNonce;
+
+    /// <summary>
+    /// Return the 'point compression' flag.
+    /// </summary>
+    /// <value>
+    /// the point compression flag
+    /// </value>
+    function GetPointCompression: Boolean;
+    property PointCompression: Boolean read GetPointCompression;
+
+  end;
+
+implementation
+
+end.

+ 0 - 20
src/libraries/cryptolib4pascal/ClpIIESWithCipherParameters.pas

@@ -40,26 +40,6 @@ type
     /// </value>
     /// </value>
     property CipherKeySize: Int32 read GetCipherKeySize;
     property CipherKeySize: Int32 read GetCipherKeySize;
 
 
-    function GetNonce: TCryptoLibByteArray;
-
-    /// <summary>
-    /// Return the nonce (IV) value to be associated with message.
-    /// </summary>
-    /// <value>
-    /// block cipher IV for message.
-    /// </value>
-    property Nonce: TCryptoLibByteArray read GetNonce;
-
-    function GetPointCompression: Boolean;
-
-    /// <summary>
-    /// Return the 'point compression' flag.
-    /// </summary>
-    /// <value>
-    /// the point compression flag
-    /// </value>
-    property PointCompression: Boolean read GetPointCompression;
-
   end;
   end;
 
 
 implementation
 implementation

+ 7 - 23
src/libraries/cryptolib4pascal/ClpPascalCoinECIESKdfBytesGenerator.pas

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

+ 4 - 23
src/libraries/cryptolib4pascal/ClpPascalCoinIESEngine.pas

@@ -34,7 +34,6 @@ uses
   ClpIEphemeralKeyPair,
   ClpIEphemeralKeyPair,
   ClpKdfParameters,
   ClpKdfParameters,
   ClpIKdfParameters,
   ClpIKdfParameters,
-  ClpIIESWithCipherParameters,
   ClpIESEngine,
   ClpIESEngine,
   ClpArrayUtils,
   ClpArrayUtils,
   ClpBigInteger,
   ClpBigInteger,
@@ -98,7 +97,7 @@ implementation
 function TPascalCoinIESEngine.DecryptBlock(const in_enc: TCryptoLibByteArray;
 function TPascalCoinIESEngine.DecryptBlock(const in_enc: TCryptoLibByteArray;
   inOff, inLen: Int32): TCryptoLibByteArray;
   inOff, inLen: Int32): TCryptoLibByteArray;
 var
 var
-  K, K1, K2, T1, T2: TCryptoLibByteArray;
+  K1, K2, T1, T2: TCryptoLibByteArray;
   cp: ICipherParameters;
   cp: ICipherParameters;
 begin
 begin
   // Ensure that the length of the input is greater than the MAC in bytes
   // Ensure that the length of the input is greater than the MAC in bytes
@@ -117,16 +116,7 @@ begin
   begin
   begin
     // Block cipher mode.
     // 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);
     cp := TKeyParameter.Create(K1);
 
 
@@ -164,7 +154,7 @@ end;
 function TPascalCoinIESEngine.EncryptBlock(const &in: TCryptoLibByteArray;
 function TPascalCoinIESEngine.EncryptBlock(const &in: TCryptoLibByteArray;
   inOff, inLen: Int32): TCryptoLibByteArray;
   inOff, inLen: Int32): TCryptoLibByteArray;
 var
 var
-  C, K, K1, K2, T: TCryptoLibByteArray;
+  C, K1, K2, T: TCryptoLibByteArray;
   MessageToEncryptPadSize, CipherBlockSize, MessageToEncryptSize: Int32;
   MessageToEncryptPadSize, CipherBlockSize, MessageToEncryptSize: Int32;
 begin
 begin
   if (Fcipher = Nil) then
   if (Fcipher = Nil) then
@@ -176,16 +166,7 @@ begin
   begin
   begin
     // Block cipher mode.
     // 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 iv is provided use it to initialise the cipher
     if (FIV <> Nil) then
     if (FIV <> Nil) then