ClpEd25519PhBlake2BSigner.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. { *********************************************************************************** }
  2. { * CryptoLib Library * }
  3. { * Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe * }
  4. { * Github Repository <https://github.com/Xor-el> * }
  5. { * Distributed under the MIT software license, see the accompanying file LICENSE * }
  6. { * or visit http://www.opensource.org/licenses/mit-license.php. * }
  7. { * Acknowledgements: * }
  8. { * * }
  9. { * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
  10. { * development of this library * }
  11. { * ******************************************************************************* * }
  12. (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
  13. unit ClpEd25519PhBlake2BSigner;
  14. {$I CryptoLib.inc}
  15. interface
  16. uses
  17. ClpIDigest,
  18. ClpIEd25519Blake2B,
  19. ClpEd25519Blake2B,
  20. ClpICipherParameters,
  21. ClpIEd25519PhBlake2BSigner,
  22. ClpIEd25519Blake2BPrivateKeyParameters,
  23. ClpIEd25519Blake2BPublicKeyParameters,
  24. ClpEd25519Blake2BPrivateKeyParameters,
  25. ClpCryptoLibTypes;
  26. resourcestring
  27. SNotInitializedForSigning =
  28. 'Ed25519PhBlake2BSigner not Initialised for Signature Generation.';
  29. SNotInitializedForVerifying =
  30. 'Ed25519PhBlake2BSigner not Initialised for Verification';
  31. SPreHashDigestFailed = 'PreHash Digest Failed';
  32. type
  33. TEd25519PhBlake2BSigner = class(TInterfacedObject, IEd25519PhBlake2BSigner)
  34. strict private
  35. var
  36. FPreHash: IDigest;
  37. FContext: TCryptoLibByteArray;
  38. FforSigning: Boolean;
  39. FEd25519Blake2BInstance: IEd25519Blake2B;
  40. FPrivateKey: IEd25519Blake2BPrivateKeyParameters;
  41. FPublicKey: IEd25519Blake2BPublicKeyParameters;
  42. strict protected
  43. function GetAlgorithmName: String; virtual;
  44. public
  45. constructor Create(const context: TCryptoLibByteArray);
  46. destructor Destroy(); override;
  47. procedure Init(forSigning: Boolean;
  48. const parameters: ICipherParameters); virtual;
  49. procedure Update(b: Byte); virtual;
  50. procedure BlockUpdate(const buf: TCryptoLibByteArray;
  51. off, len: Int32); virtual;
  52. function GenerateSignature(): TCryptoLibByteArray; virtual;
  53. function VerifySignature(const signature: TCryptoLibByteArray)
  54. : Boolean; virtual;
  55. procedure Reset(); virtual;
  56. property AlgorithmName: String read GetAlgorithmName;
  57. end;
  58. implementation
  59. { TEd25519PhBlake2BSigner }
  60. procedure TEd25519PhBlake2BSigner.BlockUpdate(const buf: TCryptoLibByteArray;
  61. off, len: Int32);
  62. begin
  63. FPreHash.BlockUpdate(buf, off, len);
  64. end;
  65. constructor TEd25519PhBlake2BSigner.Create(const context: TCryptoLibByteArray);
  66. begin
  67. Inherited Create();
  68. FContext := System.Copy(context);
  69. FEd25519Blake2BInstance := TEd25519Blake2B.Create();
  70. FPreHash := FEd25519Blake2BInstance.CreatePreHash();
  71. end;
  72. destructor TEd25519PhBlake2BSigner.Destroy;
  73. begin
  74. inherited Destroy;
  75. end;
  76. function TEd25519PhBlake2BSigner.GetAlgorithmName: String;
  77. begin
  78. Result := 'Ed25519PhBlake2B';
  79. end;
  80. procedure TEd25519PhBlake2BSigner.Init(forSigning: Boolean;
  81. const parameters: ICipherParameters);
  82. begin
  83. FforSigning := forSigning;
  84. if (forSigning) then
  85. begin
  86. // TODO Allow IAsymmetricCipherKeyPair to be an ICipherParameters?
  87. FPrivateKey := parameters as IEd25519Blake2BPrivateKeyParameters;
  88. FPublicKey := FPrivateKey.GeneratePublicKey();
  89. end
  90. else
  91. begin
  92. FPrivateKey := Nil;
  93. FPublicKey := parameters as IEd25519Blake2BPublicKeyParameters;
  94. end;
  95. Reset();
  96. end;
  97. procedure TEd25519PhBlake2BSigner.Reset;
  98. begin
  99. FPreHash.Reset();
  100. end;
  101. procedure TEd25519PhBlake2BSigner.Update(b: Byte);
  102. begin
  103. FPreHash.Update(b);
  104. end;
  105. function TEd25519PhBlake2BSigner.GenerateSignature: TCryptoLibByteArray;
  106. var
  107. signature, msg: TCryptoLibByteArray;
  108. begin
  109. if ((not FforSigning) or (FPrivateKey = Nil)) then
  110. begin
  111. raise EInvalidOperationCryptoLibException.CreateRes
  112. (@SNotInitializedForSigning);
  113. end;
  114. System.SetLength(msg, TEd25519Blake2B.PreHashSize);
  115. if ((TEd25519Blake2B.PreHashSize) <> (FPreHash.DoFinal(msg, 0))) then
  116. begin
  117. raise EInvalidOperationCryptoLibException.CreateRes(@SPreHashDigestFailed);
  118. end;
  119. System.SetLength(signature,
  120. TEd25519Blake2BPrivateKeyParameters.SignatureSize);
  121. FPrivateKey.Sign(TEd25519Blake2B.TEd25519Algorithm.Ed25519Ph, FPublicKey,
  122. FContext, msg, 0, TEd25519Blake2B.PreHashSize, signature, 0);
  123. Result := signature;
  124. end;
  125. function TEd25519PhBlake2BSigner.VerifySignature(const signature
  126. : TCryptoLibByteArray): Boolean;
  127. var
  128. pk: TCryptoLibByteArray;
  129. begin
  130. if ((FforSigning) or (FPublicKey = Nil)) then
  131. begin
  132. raise EInvalidOperationCryptoLibException.CreateRes
  133. (@SNotInitializedForVerifying);
  134. end;
  135. if (TEd25519Blake2B.SignatureSize <> System.Length(signature)) then
  136. begin
  137. Result := false;
  138. Exit;
  139. end;
  140. pk := FPublicKey.GetEncoded();
  141. Result := FEd25519Blake2BInstance.VerifyPrehash(signature, 0, pk, 0, FContext,
  142. FPreHash);
  143. end;
  144. end.