dcpsha3.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
  3. {******************************************************************************}
  4. {* A binary compatible implementation of SHA3 (224, 256, 384, 512) ************}
  5. {******************************************************************************}
  6. {* Copyright (C) 2016 Alexander Koblov ([email protected]) *}
  7. {* Permission is hereby granted, free of charge, to any person obtaining a *}
  8. {* copy of this software and associated documentation files (the "Software"), *}
  9. {* to deal in the Software without restriction, including without limitation *}
  10. {* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
  11. {* and/or sell copies of the Software, and to permit persons to whom the *}
  12. {* Software is furnished to do so, subject to the following conditions: *}
  13. {* *}
  14. {* The above copyright notice and this permission notice shall be included in *}
  15. {* all copies or substantial portions of the Software. *}
  16. {* *}
  17. {* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
  18. {* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
  19. {* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
  20. {* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
  21. {* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
  22. {* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
  23. {* DEALINGS IN THE SOFTWARE. *}
  24. {******************************************************************************}
  25. unit DCPsha3;
  26. {$mode objfpc}{$H+}
  27. interface
  28. uses
  29. Classes, SysUtils, DCPcrypt2, DCPconst, SHA3;
  30. type
  31. { TDCP_sha3base }
  32. TDCP_sha3base = class(TDCP_hash)
  33. protected
  34. FState: TSHA3State;
  35. public
  36. procedure Final(var Digest); override;
  37. procedure Update(const Buffer; Size: longword); override;
  38. end;
  39. { TDCP_sha3_224 }
  40. TDCP_sha3_224 = class(TDCP_sha3base)
  41. public
  42. class function GetAlgorithm: string; override;
  43. class function GetHashSize: integer; override;
  44. class function SelfTest: boolean; override;
  45. procedure Init; override;
  46. end;
  47. { TDCP_sha3_256 }
  48. TDCP_sha3_256 = class(TDCP_sha3base)
  49. public
  50. class function GetAlgorithm: string; override;
  51. class function GetHashSize: integer; override;
  52. class function SelfTest: boolean; override;
  53. procedure Init; override;
  54. end;
  55. { TDCP_sha3_384 }
  56. TDCP_sha3_384 = class(TDCP_sha3base)
  57. public
  58. class function GetAlgorithm: string; override;
  59. class function GetHashSize: integer; override;
  60. class function SelfTest: boolean; override;
  61. procedure Init; override;
  62. end;
  63. { TDCP_sha3_512 }
  64. TDCP_sha3_512 = class(TDCP_sha3base)
  65. public
  66. class function GetAlgorithm: string; override;
  67. class function GetHashSize: integer; override;
  68. class function SelfTest: boolean; override;
  69. procedure Init; override;
  70. end;
  71. implementation
  72. { TDCP_sha3base }
  73. procedure TDCP_sha3base.Final(var Digest);
  74. begin
  75. SHA3_FinalBit_LSB(FState, 0, 0, @Digest, FState.fixedOutputLength);
  76. end;
  77. procedure TDCP_sha3base.Update(const Buffer; Size: longword);
  78. begin
  79. SHA3_UpdateXL(FState, @Buffer, Size);
  80. end;
  81. { TDCP_sha3_224 }
  82. class function TDCP_sha3_224.GetAlgorithm: string;
  83. begin
  84. Result:= 'SHA3_224';
  85. end;
  86. class function TDCP_sha3_224.GetHashSize: integer;
  87. begin
  88. Result:= 224;
  89. end;
  90. class function TDCP_sha3_224.SelfTest: boolean;
  91. begin
  92. Result:= False; // TODO: SelfTest SHA3_224
  93. end;
  94. procedure TDCP_sha3_224.Init;
  95. begin
  96. SHA3_Init(FState, __SHA3_224);
  97. end;
  98. { TDCP_sha3_256 }
  99. class function TDCP_sha3_256.GetAlgorithm: string;
  100. begin
  101. Result:= 'SHA3_256';
  102. end;
  103. class function TDCP_sha3_256.GetHashSize: integer;
  104. begin
  105. Result:= 256;
  106. end;
  107. class function TDCP_sha3_256.SelfTest: boolean;
  108. begin
  109. Result:= False; // TODO: SelfTest SHA3_256
  110. end;
  111. procedure TDCP_sha3_256.Init;
  112. begin
  113. SHA3_Init(FState, __SHA3_256);
  114. end;
  115. { TDCP_sha3_384 }
  116. class function TDCP_sha3_384.GetAlgorithm: string;
  117. begin
  118. Result:= 'SHA3_384';
  119. end;
  120. class function TDCP_sha3_384.GetHashSize: integer;
  121. begin
  122. Result:= 384;
  123. end;
  124. class function TDCP_sha3_384.SelfTest: boolean;
  125. begin
  126. Result:= False; // TODO: SelfTest SHA3_384
  127. end;
  128. procedure TDCP_sha3_384.Init;
  129. begin
  130. SHA3_Init(FState, __SHA3_384);
  131. end;
  132. { TDCP_sha3_512 }
  133. class function TDCP_sha3_512.GetAlgorithm: string;
  134. begin
  135. Result:= 'SHA3_512';
  136. end;
  137. class function TDCP_sha3_512.GetHashSize: integer;
  138. begin
  139. Result:= 512;
  140. end;
  141. class function TDCP_sha3_512.SelfTest: boolean;
  142. begin
  143. Result:= False; // TODO: SelfTest SHA3_512
  144. end;
  145. procedure TDCP_sha3_512.Init;
  146. begin
  147. SHA3_Init(FState, __SHA3_512);
  148. end;
  149. end.