dcpblake2.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
  3. {******************************************************************************}
  4. {* A binary compatible implementation of BLAKE2S, BLAKE2SP, BLAKE2B, BLAKE2BP *}
  5. {******************************************************************************}
  6. {* Copyright (C) 2014-2018 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 DCPblake2;
  26. {$mode delphi}
  27. interface
  28. uses
  29. Classes, SysUtils, CTypes, DCPcrypt2, DCPconst, DCblake2, Hash;
  30. type
  31. { TDCP_blake2s }
  32. TDCP_blake2s = class(TDCP_hash)
  33. protected
  34. S: blake2s_state;
  35. public
  36. class function GetId: integer; override;
  37. class function GetAlgorithm: string; override;
  38. class function GetHashSize: integer; override;
  39. class function SelfTest: boolean; override;
  40. procedure Init; override;
  41. procedure Burn; override;
  42. procedure Update(const Buffer; Size: longword); override;
  43. procedure Final(var Digest); override;
  44. end;
  45. { TDCP_blake2sp }
  46. TDCP_blake2sp = class(TDCP_hash)
  47. protected
  48. S: blake2sp_state;
  49. public
  50. class function GetId: integer; override;
  51. class function GetAlgorithm: string; override;
  52. class function GetHashSize: integer; override;
  53. class function SelfTest: boolean; override;
  54. procedure Init; override;
  55. procedure Burn; override;
  56. procedure Update(const Buffer; Size: longword); override;
  57. procedure Final(var Digest); override;
  58. end;
  59. { TDCP_blake2b }
  60. TDCP_blake2b = class(TDCP_hash)
  61. protected
  62. S: blake2b_state;
  63. public
  64. class function GetId: integer; override;
  65. class function GetAlgorithm: string; override;
  66. class function GetHashSize: integer; override;
  67. class function SelfTest: boolean; override;
  68. procedure Init; override;
  69. procedure Burn; override;
  70. procedure Update(const Buffer; Size: longword); override;
  71. procedure Final(var Digest); override;
  72. end;
  73. { TDCP_blake2bp }
  74. TDCP_blake2bp = class(TDCP_hash)
  75. protected
  76. S: blake2bp_state;
  77. public
  78. class function GetId: integer; override;
  79. class function GetAlgorithm: string; override;
  80. class function GetHashSize: integer; override;
  81. class function SelfTest: boolean; override;
  82. procedure Init; override;
  83. procedure Burn; override;
  84. procedure Update(const Buffer; Size: longword); override;
  85. procedure Final(var Digest); override;
  86. end;
  87. implementation
  88. { TDCP_blake2s }
  89. class function TDCP_blake2s.GetId: integer;
  90. begin
  91. Result:= DCP_blake2s;
  92. end;
  93. class function TDCP_blake2s.GetAlgorithm: string;
  94. begin
  95. Result:= 'BLAKE2S';
  96. end;
  97. class function TDCP_blake2s.GetHashSize: integer;
  98. begin
  99. Result:= 256;
  100. end;
  101. class function TDCP_blake2s.SelfTest: boolean;
  102. const
  103. Test1Out: array[0..31] of byte=
  104. ($50, $8c, $5e, $8c, $32, $7c, $14, $e2, $e1, $a7, $2b, $a3, $4e, $eb, $45, $2f,
  105. $37, $45, $8b, $20, $9e, $d6, $3a, $29, $4d, $99, $9b, $4c, $86, $67, $59, $82);
  106. Test2Out: array[0..31] of byte=
  107. ($6f, $4d, $f5, $11, $6a, $6f, $33, $2e, $da, $b1, $d9, $e1, $0e, $e8, $7d, $f6,
  108. $55, $7b, $ea, $b6, $25, $9d, $76, $63, $f3, $bc, $d5, $72, $2c, $13, $f1, $89 );
  109. var
  110. TestHash: TDCP_blake2s;
  111. TestOut: array[0..31] of byte;
  112. begin
  113. dcpFillChar(TestOut, SizeOf(TestOut), 0);
  114. TestHash:= TDCP_blake2s.Create(nil);
  115. TestHash.Init;
  116. TestHash.UpdateStr('abc');
  117. TestHash.Final(TestOut);
  118. Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
  119. TestHash.Init;
  120. TestHash.UpdateStr('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
  121. TestHash.Final(TestOut);
  122. Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
  123. TestHash.Free;
  124. end;
  125. procedure TDCP_blake2s.Init;
  126. begin
  127. if blake2s_init( @S, BLAKE2S_OUTBYTES ) < 0 then
  128. raise EDCP_hash.Create('blake2s_init');
  129. fInitialized:= true;
  130. end;
  131. procedure TDCP_blake2s.Burn;
  132. begin
  133. fInitialized:= false;
  134. end;
  135. procedure TDCP_blake2s.Update(const Buffer; Size: longword);
  136. begin
  137. if blake2s_update(@S, @Buffer, Size) < 0 then
  138. raise EDCP_hash.Create('blake2s_update');
  139. end;
  140. procedure TDCP_blake2s.Final(var Digest);
  141. var
  142. Hash: array[0..Pred(BLAKE2S_OUTBYTES)] of cuint8;
  143. begin
  144. if not fInitialized then
  145. raise EDCP_hash.Create('Hash not initialized');
  146. if blake2s_final(@S, Hash, SizeOf(Hash)) < 0 then
  147. raise EDCP_hash.Create('blake2s_final');
  148. Move(Hash, Digest, Sizeof(Hash));
  149. Burn;
  150. end;
  151. { TDCP_blake2sp }
  152. class function TDCP_blake2sp.GetId: integer;
  153. begin
  154. Result:= DCP_blake2sp;
  155. end;
  156. class function TDCP_blake2sp.GetAlgorithm: string;
  157. begin
  158. Result:= 'BLAKE2SP';
  159. end;
  160. class function TDCP_blake2sp.GetHashSize: integer;
  161. begin
  162. Result:= 256;
  163. end;
  164. class function TDCP_blake2sp.SelfTest: boolean;
  165. const
  166. Test1Out: array[0..31] of byte=
  167. ($70, $f7, $5b, $58, $f1, $fe, $ca, $b8, $21, $db, $43, $c8, $8a, $d8, $4e, $dd,
  168. $e5, $a5, $26, $00, $61, $6c, $d2, $25, $17, $b7, $bb, $14, $d4, $40, $a7, $d5);
  169. Test2Out: array[0..31] of byte=
  170. ($3d, $10, $7e, $42, $f1, $7c, $13, $c8, $2b, $43, $6e, $bb, $65, $1a, $48, $de,
  171. $f6, $7e, $77, $72, $fa, $06, $f4, $73, $8e, $e9, $68, $c7, $f4, $d8, $b4, $8b);
  172. var
  173. TestHash: TDCP_blake2sp;
  174. TestOut: array[0..31] of byte;
  175. begin
  176. dcpFillChar(TestOut, SizeOf(TestOut), 0);
  177. TestHash:= TDCP_blake2sp.Create(nil);
  178. TestHash.Init;
  179. TestHash.UpdateStr('abc');
  180. TestHash.Final(TestOut);
  181. Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
  182. TestHash.Init;
  183. TestHash.UpdateStr('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
  184. TestHash.Final(TestOut);
  185. Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
  186. TestHash.Free;
  187. end;
  188. procedure TDCP_blake2sp.Init;
  189. begin
  190. if blake2sp_init( @S, BLAKE2S_OUTBYTES ) < 0 then
  191. raise EDCP_hash.Create('blake2sp_init');
  192. fInitialized:= true;
  193. end;
  194. procedure TDCP_blake2sp.Burn;
  195. begin
  196. fInitialized:= false;
  197. end;
  198. procedure TDCP_blake2sp.Update(const Buffer; Size: longword);
  199. begin
  200. if blake2sp_update(@S, @Buffer, Size) < 0 then
  201. raise EDCP_hash.Create('blake2sp_update');
  202. end;
  203. procedure TDCP_blake2sp.Final(var Digest);
  204. var
  205. Hash: array[0..Pred(BLAKE2S_OUTBYTES)] of cuint8;
  206. begin
  207. if not fInitialized then
  208. raise EDCP_hash.Create('Hash not initialized');
  209. if blake2sp_final(@S, Hash, SizeOf(Hash)) < 0 then
  210. raise EDCP_hash.Create('blake2sp_final');
  211. Move(Hash, Digest, Sizeof(Hash));
  212. Burn;
  213. end;
  214. { TDCP_blake2b }
  215. class function TDCP_blake2b.GetId: integer;
  216. begin
  217. Result:= DCP_blake2b;
  218. end;
  219. class function TDCP_blake2b.GetAlgorithm: string;
  220. begin
  221. Result:= 'BLAKE2B';
  222. end;
  223. class function TDCP_blake2b.GetHashSize: integer;
  224. begin
  225. Result:= 512;
  226. end;
  227. class function TDCP_blake2b.SelfTest: boolean;
  228. const
  229. Test1Out: array[0..63] of byte =
  230. ($ba, $80, $a5, $3f, $98, $1c, $4d, $0d, $6a, $27, $97, $b6, $9f, $12, $f6, $e9,
  231. $4c, $21, $2f, $14, $68, $5a, $c4, $b7, $4b, $12, $bb, $6f, $db, $ff, $a2, $d1,
  232. $7d, $87, $c5, $39, $2a, $ab, $79, $2d, $c2, $52, $d5, $de, $45, $33, $cc, $95,
  233. $18, $d3, $8a, $a8, $db, $f1, $92, $5a, $b9, $23, $86, $ed, $d4, $00, $99, $23);
  234. Test2Out: array[0..63] of byte =
  235. ($72, $85, $ff, $3e, $8b, $d7, $68, $d6, $9b, $e6, $2b, $3b, $f1, $87, $65, $a3,
  236. $25, $91, $7f, $a9, $74, $4a, $c2, $f5, $82, $a2, $08, $50, $bc, $2b, $11, $41,
  237. $ed, $1b, $3e, $45, $28, $59, $5a, $cc, $90, $77, $2b, $df, $2d, $37, $dc, $8a,
  238. $47, $13, $0b, $44, $f3, $3a, $02, $e8, $73, $0e, $5a, $d8, $e1, $66, $e8, $88);
  239. var
  240. TestHash: TDCP_blake2b;
  241. TestOut: array[0..63] of byte;
  242. begin
  243. dcpFillChar(TestOut, SizeOf(TestOut), 0);
  244. TestHash:= TDCP_blake2b.Create(nil);
  245. TestHash.Init;
  246. TestHash.UpdateStr('abc');
  247. TestHash.Final(TestOut);
  248. Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
  249. TestHash.Init;
  250. TestHash.UpdateStr('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
  251. TestHash.Final(TestOut);
  252. Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
  253. TestHash.Free;
  254. end;
  255. procedure TDCP_blake2b.Init;
  256. begin
  257. if blake2b_init( @S, BLAKE2B_OUTBYTES ) < 0 then
  258. raise EDCP_hash.Create('blake2b_init');
  259. fInitialized:= true;
  260. end;
  261. procedure TDCP_blake2b.Burn;
  262. begin
  263. fInitialized:= false;
  264. end;
  265. procedure TDCP_blake2b.Update(const Buffer; Size: longword);
  266. begin
  267. if blake2b_update(@S, @Buffer, Size) < 0 then
  268. raise EDCP_hash.Create('blake2b_update');
  269. end;
  270. procedure TDCP_blake2b.Final(var Digest);
  271. var
  272. Hash: array[0..Pred(BLAKE2B_OUTBYTES)] of cuint8;
  273. begin
  274. if not fInitialized then
  275. raise EDCP_hash.Create('Hash not initialized');
  276. if blake2b_final(@S, Hash, SizeOf(Hash)) < 0 then
  277. raise EDCP_hash.Create('blake2b_final');
  278. Move(Hash, Digest, Sizeof(Hash));
  279. Burn;
  280. end;
  281. { TDCP_blake2bp }
  282. class function TDCP_blake2bp.GetId: integer;
  283. begin
  284. Result:= DCP_blake2bp;
  285. end;
  286. class function TDCP_blake2bp.GetAlgorithm: string;
  287. begin
  288. Result:= 'BLAKE2BP';
  289. end;
  290. class function TDCP_blake2bp.GetHashSize: integer;
  291. begin
  292. Result:= 512;
  293. end;
  294. class function TDCP_blake2bp.SelfTest: boolean;
  295. const
  296. Test1Out: array[0..63] of byte =
  297. ($b9, $1a, $6b, $66, $ae, $87, $52, $6c, $40, $0b, $0a, $8b, $53, $77, $4d, $c6,
  298. $52, $84, $ad, $8f, $65, $75, $f8, $14, $8f, $f9, $3d, $ff, $94, $3a, $6e, $cd,
  299. $83, $62, $13, $0f, $22, $d6, $da, $e6, $33, $aa, $0f, $91, $df, $4a, $c8, $9a,
  300. $af, $f3, $1d, $0f, $1b, $92, $3c, $89, $8e, $82, $02, $5d, $ed, $bd, $ad, $6e);
  301. Test2Out: array[0..63] of byte =
  302. ($c5, $a0, $34, $1e, $eb, $b6, $15, $50, $3e, $22, $93, $30, $e0, $6a, $3d, $ce,
  303. $88, $05, $b4, $34, $ca, $75, $8e, $89, $9e, $72, $ac, $40, $ba, $c3, $6e, $63,
  304. $7b, $70, $09, $8a, $24, $ae, $5c, $3c, $4d, $39, $a1, $83, $a4, $3e, $b9, $74,
  305. $82, $3e, $3d, $db, $5b, $09, $e0, $7a, $d1, $e5, $26, $e9, $05, $f6, $5b, $c4);
  306. var
  307. TestHash: TDCP_blake2bp;
  308. TestOut: array[0..63] of byte;
  309. begin
  310. dcpFillChar(TestOut, SizeOf(TestOut), 0);
  311. TestHash:= TDCP_blake2bp.Create(nil);
  312. TestHash.Init;
  313. TestHash.UpdateStr('abc');
  314. TestHash.Final(TestOut);
  315. Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
  316. TestHash.Init;
  317. TestHash.UpdateStr('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
  318. TestHash.Final(TestOut);
  319. Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
  320. TestHash.Free;
  321. end;
  322. procedure TDCP_blake2bp.Init;
  323. begin
  324. if blake2bp_init( @S, BLAKE2B_OUTBYTES ) < 0 then
  325. raise EDCP_hash.Create('blake2bp_init');
  326. fInitialized:= true;
  327. end;
  328. procedure TDCP_blake2bp.Burn;
  329. begin
  330. fInitialized:= false;
  331. end;
  332. procedure TDCP_blake2bp.Update(const Buffer; Size: longword);
  333. begin
  334. if blake2bp_update(@S, @Buffer, Size) < 0 then
  335. raise EDCP_hash.Create('blake2bp_update');
  336. end;
  337. procedure TDCP_blake2bp.Final(var Digest);
  338. var
  339. Hash: array[0..Pred(BLAKE2B_OUTBYTES)] of cuint8;
  340. begin
  341. if not fInitialized then
  342. raise EDCP_hash.Create('Hash not initialized');
  343. if blake2bp_final(@S, Hash, SizeOf(Hash)) < 0 then
  344. raise EDCP_hash.Create('blake2bp_final');
  345. Move(Hash, Digest, Sizeof(Hash));
  346. Burn;
  347. end;
  348. end.