Browse Source

Improvements to PascalCoinIESEngine Encrypt.

Ugochukwu Mmaduekwe 7 years ago
parent
commit
dcf8ed16db

+ 3 - 2
CryptoLib.Samples/Delphi.Samples/UsageSamples.dpr

@@ -375,8 +375,9 @@ begin
     TUsageExamples.RecreatePublicKeyFromXAndYCoordByteArray;
     TUsageExamples.BinaryCompatiblePascalCoinAES256EncryptDecryptDemo('Pascal Rules', 'Pascal');
     TUsageExamples.BinaryCompatiblePascalCoinECIESEncryptDecryptDemo('Kowalski');
-   // TUsageExamples.BinaryCompatiblePascalCoinECIESDecryptExistingPayloadDemo('', '', '');
-    Readln;
+//  TUsageExamples.BinaryCompatiblePascalCoinECIESEncryptExistingPayloadDemo('', '', '');
+//  TUsageExamples.BinaryCompatiblePascalCoinECIESDecryptExistingPayloadDemo('', '', '');
+      Readln;
   except
     on E: Exception do
     begin

+ 2 - 0
CryptoLib.Samples/FreePascal.Samples/UsageSamples.lpr

@@ -17,6 +17,8 @@ begin
     TUsageExamples.RecreatePublicKeyFromXAndYCoordByteArray;
     TUsageExamples.BinaryCompatiblePascalCoinAES256EncryptDecryptDemo('Pascal Rules', 'Pascal');
     TUsageExamples.BinaryCompatiblePascalCoinECIESEncryptDecryptDemo('Kowalski');
+//  TUsageExamples.BinaryCompatiblePascalCoinECIESEncryptExistingPayloadDemo('', '', '');
+//  TUsageExamples.BinaryCompatiblePascalCoinECIESDecryptExistingPayloadDemo('', '', '');
     Readln;
   except
     on E: Exception do

+ 61 - 1
CryptoLib.Samples/src/UsageExamples.pas

@@ -151,6 +151,9 @@ type
     class procedure BinaryCompatiblePascalCoinECIESEncryptDecryptDemo
       (const input: string); static;
 
+    class procedure BinaryCompatiblePascalCoinECIESEncryptExistingPayloadDemo
+      (const PublicKeyInHex, PureMessageInHex, ACurveName: string); static;
+
     class procedure BinaryCompatiblePascalCoinECIESDecryptExistingPayloadDemo
       (const PrivateKeyInHex, EncryptedMessageInHex,
       ACurveName: string); static;
@@ -348,6 +351,63 @@ begin
 
 end;
 
+class procedure TUsageExamples.
+  BinaryCompatiblePascalCoinECIESEncryptExistingPayloadDemo
+  (const PublicKeyInHex, PureMessageInHex, ACurveName: string);
+
+const
+  MethodName = 'BinaryCompatiblePascalCoinECIESEncryptExistingPayloadDemo';
+var
+  PublicKeyBytes, PayloadToEncodeBytes, EncryptedCipherText: TBytes;
+  Lcurve: IX9ECParameters;
+  domain: IECDomainParameters;
+  RegeneratedPublicKey: IECPublicKeyParameters;
+begin
+
+  // Create From Existing Parameter Method
+  System.Assert(PublicKeyInHex <> '', 'PublicKeyInHex Cannot be Empty');
+  System.Assert(PureMessageInHex <> '', 'PureMessageInHex Cannot be Empty');
+  System.Assert(ACurveName <> '', 'ACurveName Cannot be Empty');
+
+  PublicKeyBytes := THex.Decode(PublicKeyInHex);
+  System.Assert(PublicKeyBytes <> Nil, 'PublicKeyBytes Cannot be Nil');
+
+  PayloadToEncodeBytes := THex.Decode(PureMessageInHex);
+  System.Assert(PayloadToEncodeBytes <> Nil,
+    'PayloadToDecodeBytes Cannot be Nil');
+
+  Lcurve := TSecNamedCurves.GetByName(ACurveName);
+  System.Assert(Lcurve <> Nil, 'Lcurve Cannot be Nil');
+
+  // Set Up Asymmetric Key Pair from known private key ByteArray
+
+  domain := TECDomainParameters.Create(Lcurve.Curve, Lcurve.G, Lcurve.N,
+    Lcurve.H, Lcurve.GetSeed);
+
+  RegeneratedPublicKey := TECPublicKeyParameters.Create('ECDSA',
+    Lcurve.Curve.DecodePoint(PublicKeyBytes), domain);
+
+  // Do Encryption Of Payload
+
+  EncryptedCipherText := TUsageExamples.ECIESPascalCoinEncrypt
+    (RegeneratedPublicKey, PayloadToEncodeBytes);
+  if EncryptedCipherText <> Nil then
+  begin
+
+    Writeln('ECIES PascalCoin Existing Payload Compatability Encrypt Was Successful '
+      + sLineBreak);
+
+    Writeln('Encrypted Payload Message Is "' +
+      THex.Encode(EncryptedCipherText) + '"');
+    Exit;
+
+  end;
+
+  Writeln('ECIES PascalCoin Existing Payload Compatability Encrypt Failed ' +
+    sLineBreak);
+
+end;
+
 class procedure TUsageExamples.
   BinaryCompatiblePascalCoinECIESDecryptExistingPayloadDemo
   (const PrivateKeyInHex, EncryptedMessageInHex, ACurveName: string);
@@ -408,7 +468,7 @@ begin
     Writeln('ECIES PascalCoin Existing Payload Compatability Decrypt Was Successful '
       + sLineBreak);
 
-    Writeln('Payload Message Is "' + TEncoding.UTF8.GetString
+    Writeln('Decrypted Payload Message Is "' + TEncoding.UTF8.GetString
       (DecryptedCipherText) + '"');
     Exit;
 

+ 38 - 3
CryptoLib/src/Crypto/Engines/ClpPascalCoinIESEngine.pas

@@ -39,6 +39,7 @@ uses
   ClpArrayUtils,
   ClpBigInteger,
   ClpBigIntegers,
+  ClpBitConverter,
   ClpCryptoLibTypes;
 
 resourcestring
@@ -152,7 +153,8 @@ end;
 function TPascalCoinIESEngine.EncryptBlock(&in: TCryptoLibByteArray;
   inOff, inLen: Int32): TCryptoLibByteArray;
 var
-  C, K, K1, K2, T: TCryptoLibByteArray;
+  C, K, K1, K2, T, tempHolder: TCryptoLibByteArray;
+  MessageToEncryptPadSize, CipherBlockSize, MessageToEncryptSize: Int32;
 begin
   if (Fcipher = Nil) then
   begin
@@ -196,15 +198,48 @@ begin
   Fmac.BlockUpdate(C, 0, System.Length(C));
 
   T := Fmac.DoFinal();
+  CipherBlockSize := Fcipher.GetBlockSize;
+  MessageToEncryptSize := inLen - inOff;
 
-  // Output the quadruple (SECURE_HEAD_SIZE,V,T,C).
-  // SECURE_HEAD_SIZE := Dummy Random Data for interpolability
+  if (MessageToEncryptSize mod CipherBlockSize) = 0 then
+  begin
+    MessageToEncryptPadSize := 0
+  end
+  else
+  begin
+    MessageToEncryptPadSize := CipherBlockSize -
+      (MessageToEncryptSize mod CipherBlockSize);
+  end;
+  // Output the quadruple (SECURE_HEAD_DETAILS,V,T,C).
+  // SECURE_HEAD_DETAILS :=
+  // [0] := Convert Byte(Length(V)) to a ByteArray,
+  // [1] := Convert Byte(Length(T)) to a ByteArray,
+  // [2] and [3] := Convert UInt16(MessageToEncryptSize) to a ByteArray,
+  // [4] and [5] := Convert UInt16(MessageToEncryptSize + MessageToEncryptPadSize) to a ByteArray,
   // V := Ephermeral Public Key
   // T := Authentication Message (MAC)
   // C := Encrypted Payload
+
   System.SetLength(Result, SECURE_HEAD_SIZE + System.Length(FV) +
     System.Length(T) + System.Length(C));
 
+  tempHolder := TBitConverter.GetBytes(Byte(System.Length(FV)));
+
+  System.Move(tempHolder[0], Result[0], System.SizeOf(Byte));
+
+  tempHolder := TBitConverter.GetBytes(Byte(System.Length(T)));
+
+  System.Move(tempHolder[0], Result[1], System.SizeOf(Byte));
+
+  tempHolder := TBitConverter.GetBytes(UInt16(MessageToEncryptSize));
+
+  System.Move(tempHolder[0], Result[2], System.SizeOf(UInt16));
+
+  tempHolder := TBitConverter.GetBytes
+    (UInt16(MessageToEncryptSize + MessageToEncryptPadSize));
+
+  System.Move(tempHolder[0], Result[4], System.SizeOf(UInt16));
+
   System.Move(FV[0], Result[SECURE_HEAD_SIZE], System.Length(FV) *
     System.SizeOf(Byte));
 

+ 2 - 2
CryptoLib/src/Math/EC/ClpECCurve.pas

@@ -829,7 +829,7 @@ begin
 
     $04: // uncompressed
       begin
-        if (System.Length(encoded) <> (2 * expectedLength + 1)) then
+        if (System.Length(encoded) <> ((2 * expectedLength) + 1)) then
         begin
           raise EArgumentCryptoLibException.CreateRes
             (@SIncorrectLengthUnCompressedEncoding);
@@ -844,7 +844,7 @@ begin
     $06, // hybrid
     $07: // hybrid
       begin
-        if (System.Length(encoded) <> (2 * expectedLength + 1)) then
+        if (System.Length(encoded) <> ((2 * expectedLength) + 1)) then
         begin
           raise EArgumentCryptoLibException.CreateRes
             (@SIncorrectLengthHybridEncoding);