dcptiger.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
  3. {******************************************************************************}
  4. {* A binary compatible implementation of Tiger ********************************}
  5. {******************************************************************************}
  6. {* Copyright (c) 2002 David Barton *}
  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 DCPtiger;
  26. {$MODE Delphi}
  27. interface
  28. uses
  29. Classes, Sysutils, DCPcrypt2, DCPconst;
  30. type
  31. TDCP_tiger= class(TDCP_hash)
  32. protected
  33. Len: int64;
  34. Index: DWord;
  35. CurrentHash: array[0..2] of int64;
  36. HashBuffer: array[0..63] of byte;
  37. procedure Compress;
  38. public
  39. class function GetId: integer; override;
  40. class function GetAlgorithm: string; override;
  41. class function GetHashSize: integer; override;
  42. class function SelfTest: boolean; override;
  43. procedure Init; override;
  44. procedure Burn; override;
  45. procedure Update(const Buffer; Size: longword); override;
  46. procedure Final(var Digest); override;
  47. end;
  48. {******************************************************************************}
  49. {******************************************************************************}
  50. implementation
  51. {$R-}{$Q-}
  52. {$INCLUDE dcptiger.inc}
  53. procedure TDCP_tiger.Compress;
  54. var
  55. a, b, c, aa, bb, cc: int64;
  56. x: array[0..7] of int64;
  57. begin
  58. dcpFillChar(x, SizeOf(x), 0);
  59. a:= CurrentHash[0]; aa:= a;
  60. b:= CurrentHash[1]; bb:= b;
  61. c:= CurrentHash[2]; cc:= c;
  62. Move(HashBuffer,x,Sizeof(x));
  63. c:= c xor x[0];
  64. a:= a - (t1[c and $FF] xor t2[(c shr 16) and $FF] xor t3[(c shr 32) and $FF] xor t4[(c shr 48) and $FF]);
  65. b:= b + (t4[(c shr 8) and $FF] xor t3[(c shr 24) and $FF] xor t2[(c shr 40) and $FF] xor t1[(c shr 56) and $FF]);
  66. b:= b * 5;
  67. a:= a xor x[1];
  68. b:= b - (t1[a and $FF] xor t2[(a shr 16) and $FF] xor t3[(a shr 32) and $FF] xor t4[(a shr 48) and $FF]);
  69. c:= c + (t4[(a shr 8) and $FF] xor t3[(a shr 24) and $FF] xor t2[(a shr 40) and $FF] xor t1[(a shr 56) and $FF]);
  70. c:= c * 5;
  71. b:= b xor x[2];
  72. c:= c - (t1[b and $FF] xor t2[(b shr 16) and $FF] xor t3[(b shr 32) and $FF] xor t4[(b shr 48) and $FF]);
  73. a:= a + (t4[(b shr 8) and $FF] xor t3[(b shr 24) and $FF] xor t2[(b shr 40) and $FF] xor t1[(b shr 56) and $FF]);
  74. a:= a * 5;
  75. c:= c xor x[3];
  76. a:= a - (t1[c and $FF] xor t2[(c shr 16) and $FF] xor t3[(c shr 32) and $FF] xor t4[(c shr 48) and $FF]);
  77. b:= b + (t4[(c shr 8) and $FF] xor t3[(c shr 24) and $FF] xor t2[(c shr 40) and $FF] xor t1[(c shr 56) and $FF]);
  78. b:= b * 5;
  79. a:= a xor x[4];
  80. b:= b - (t1[a and $FF] xor t2[(a shr 16) and $FF] xor t3[(a shr 32) and $FF] xor t4[(a shr 48) and $FF]);
  81. c:= c + (t4[(a shr 8) and $FF] xor t3[(a shr 24) and $FF] xor t2[(a shr 40) and $FF] xor t1[(a shr 56) and $FF]);
  82. c:= c * 5;
  83. b:= b xor x[5];
  84. c:= c - (t1[b and $FF] xor t2[(b shr 16) and $FF] xor t3[(b shr 32) and $FF] xor t4[(b shr 48) and $FF]);
  85. a:= a + (t4[(b shr 8) and $FF] xor t3[(b shr 24) and $FF] xor t2[(b shr 40) and $FF] xor t1[(b shr 56) and $FF]);
  86. a:= a * 5;
  87. c:= c xor x[6];
  88. a:= a - (t1[c and $FF] xor t2[(c shr 16) and $FF] xor t3[(c shr 32) and $FF] xor t4[(c shr 48) and $FF]);
  89. b:= b + (t4[(c shr 8) and $FF] xor t3[(c shr 24) and $FF] xor t2[(c shr 40) and $FF] xor t1[(c shr 56) and $FF]);
  90. b:= b * 5;
  91. a:= a xor x[7];
  92. b:= b - (t1[a and $FF] xor t2[(a shr 16) and $FF] xor t3[(a shr 32) and $FF] xor t4[(a shr 48) and $FF]);
  93. c:= c + (t4[(a shr 8) and $FF] xor t3[(a shr 24) and $FF] xor t2[(a shr 40) and $FF] xor t1[(a shr 56) and $FF]);
  94. c:= c * 5;
  95. x[0]:= x[0] - (x[7] xor $A5A5A5A5A5A5A5A5);
  96. x[1]:= x[1] xor x[0];
  97. x[2]:= x[2] + x[1];
  98. x[3]:= x[3] - (x[2] xor ((not x[1]) shl 19));
  99. x[4]:= x[4] xor x[3];
  100. x[5]:= x[5] + x[4];
  101. x[6]:= x[6] - (x[5] xor ((not x[4]) shr 23));
  102. x[7]:= x[7] xor x[6];
  103. x[0]:= x[0] + x[7];
  104. x[1]:= x[1] - (x[0] xor ((not x[7]) shl 19));
  105. x[2]:= x[2] xor x[1];
  106. x[3]:= x[3] + x[2];
  107. x[4]:= x[4] - (x[3] xor ((not x[2]) shr 23));
  108. x[5]:= x[5] xor x[4];
  109. x[6]:= x[6] + x[5];
  110. x[7]:= x[7] - (x[6] xor $0123456789ABCDEF);
  111. b:= b xor x[0];
  112. c:= c - (t1[b and $FF] xor t2[(b shr 16) and $FF] xor t3[(b shr 32) and $FF] xor t4[(b shr 48) and $FF]);
  113. a:= a + (t4[(b shr 8) and $FF] xor t3[(b shr 24) and $FF] xor t2[(b shr 40) and $FF] xor t1[(b shr 56) and $FF]);
  114. a:= a * 7;
  115. c:= c xor x[1];
  116. a:= a - (t1[c and $FF] xor t2[(c shr 16) and $FF] xor t3[(c shr 32) and $FF] xor t4[(c shr 48) and $FF]);
  117. b:= b + (t4[(c shr 8) and $FF] xor t3[(c shr 24) and $FF] xor t2[(c shr 40) and $FF] xor t1[(c shr 56) and $FF]);
  118. b:= b * 7;
  119. a:= a xor x[2];
  120. b:= b - (t1[a and $FF] xor t2[(a shr 16) and $FF] xor t3[(a shr 32) and $FF] xor t4[(a shr 48) and $FF]);
  121. c:= c + (t4[(a shr 8) and $FF] xor t3[(a shr 24) and $FF] xor t2[(a shr 40) and $FF] xor t1[(a shr 56) and $FF]);
  122. c:= c * 7;
  123. b:= b xor x[3];
  124. c:= c - (t1[b and $FF] xor t2[(b shr 16) and $FF] xor t3[(b shr 32) and $FF] xor t4[(b shr 48) and $FF]);
  125. a:= a + (t4[(b shr 8) and $FF] xor t3[(b shr 24) and $FF] xor t2[(b shr 40) and $FF] xor t1[(b shr 56) and $FF]);
  126. a:= a * 7;
  127. c:= c xor x[4];
  128. a:= a - (t1[c and $FF] xor t2[(c shr 16) and $FF] xor t3[(c shr 32) and $FF] xor t4[(c shr 48) and $FF]);
  129. b:= b + (t4[(c shr 8) and $FF] xor t3[(c shr 24) and $FF] xor t2[(c shr 40) and $FF] xor t1[(c shr 56) and $FF]);
  130. b:= b * 7;
  131. a:= a xor x[5];
  132. b:= b - (t1[a and $FF] xor t2[(a shr 16) and $FF] xor t3[(a shr 32) and $FF] xor t4[(a shr 48) and $FF]);
  133. c:= c + (t4[(a shr 8) and $FF] xor t3[(a shr 24) and $FF] xor t2[(a shr 40) and $FF] xor t1[(a shr 56) and $FF]);
  134. c:= c * 7;
  135. b:= b xor x[6];
  136. c:= c - (t1[b and $FF] xor t2[(b shr 16) and $FF] xor t3[(b shr 32) and $FF] xor t4[(b shr 48) and $FF]);
  137. a:= a + (t4[(b shr 8) and $FF] xor t3[(b shr 24) and $FF] xor t2[(b shr 40) and $FF] xor t1[(b shr 56) and $FF]);
  138. a:= a * 7;
  139. c:= c xor x[7];
  140. a:= a - (t1[c and $FF] xor t2[(c shr 16) and $FF] xor t3[(c shr 32) and $FF] xor t4[(c shr 48) and $FF]);
  141. b:= b + (t4[(c shr 8) and $FF] xor t3[(c shr 24) and $FF] xor t2[(c shr 40) and $FF] xor t1[(c shr 56) and $FF]);
  142. b:= b * 7;
  143. x[0]:= x[0] - (x[7] xor $A5A5A5A5A5A5A5A5);
  144. x[1]:= x[1] xor x[0];
  145. x[2]:= x[2] + x[1];
  146. x[3]:= x[3] - (x[2] xor ((not x[1]) shl 19));
  147. x[4]:= x[4] xor x[3];
  148. x[5]:= x[5] + x[4];
  149. x[6]:= x[6] - (x[5] xor ((not x[4]) shr 23));
  150. x[7]:= x[7] xor x[6];
  151. x[0]:= x[0] + x[7];
  152. x[1]:= x[1] - (x[0] xor ((not x[7]) shl 19));
  153. x[2]:= x[2] xor x[1];
  154. x[3]:= x[3] + x[2];
  155. x[4]:= x[4] - (x[3] xor ((not x[2]) shr 23));
  156. x[5]:= x[5] xor x[4];
  157. x[6]:= x[6] + x[5];
  158. x[7]:= x[7] - (x[6] xor $0123456789ABCDEF);
  159. a:= a xor x[0];
  160. b:= b - (t1[a and $FF] xor t2[(a shr 16) and $FF] xor t3[(a shr 32) and $FF] xor t4[(a shr 48) and $FF]);
  161. c:= c + (t4[(a shr 8) and $FF] xor t3[(a shr 24) and $FF] xor t2[(a shr 40) and $FF] xor t1[(a shr 56) and $FF]);
  162. c:= c * 9;
  163. b:= b xor x[1];
  164. c:= c - (t1[b and $FF] xor t2[(b shr 16) and $FF] xor t3[(b shr 32) and $FF] xor t4[(b shr 48) and $FF]);
  165. a:= a + (t4[(b shr 8) and $FF] xor t3[(b shr 24) and $FF] xor t2[(b shr 40) and $FF] xor t1[(b shr 56) and $FF]);
  166. a:= a * 9;
  167. c:= c xor x[2];
  168. a:= a - (t1[c and $FF] xor t2[(c shr 16) and $FF] xor t3[(c shr 32) and $FF] xor t4[(c shr 48) and $FF]);
  169. b:= b + (t4[(c shr 8) and $FF] xor t3[(c shr 24) and $FF] xor t2[(c shr 40) and $FF] xor t1[(c shr 56) and $FF]);
  170. b:= b * 9;
  171. a:= a xor x[3];
  172. b:= b - (t1[a and $FF] xor t2[(a shr 16) and $FF] xor t3[(a shr 32) and $FF] xor t4[(a shr 48) and $FF]);
  173. c:= c + (t4[(a shr 8) and $FF] xor t3[(a shr 24) and $FF] xor t2[(a shr 40) and $FF] xor t1[(a shr 56) and $FF]);
  174. c:= c * 9;
  175. b:= b xor x[4];
  176. c:= c - (t1[b and $FF] xor t2[(b shr 16) and $FF] xor t3[(b shr 32) and $FF] xor t4[(b shr 48) and $FF]);
  177. a:= a + (t4[(b shr 8) and $FF] xor t3[(b shr 24) and $FF] xor t2[(b shr 40) and $FF] xor t1[(b shr 56) and $FF]);
  178. a:= a * 9;
  179. c:= c xor x[5];
  180. a:= a - (t1[c and $FF] xor t2[(c shr 16) and $FF] xor t3[(c shr 32) and $FF] xor t4[(c shr 48) and $FF]);
  181. b:= b + (t4[(c shr 8) and $FF] xor t3[(c shr 24) and $FF] xor t2[(c shr 40) and $FF] xor t1[(c shr 56) and $FF]);
  182. b:= b * 9;
  183. a:= a xor x[6];
  184. b:= b - (t1[a and $FF] xor t2[(a shr 16) and $FF] xor t3[(a shr 32) and $FF] xor t4[(a shr 48) and $FF]);
  185. c:= c + (t4[(a shr 8) and $FF] xor t3[(a shr 24) and $FF] xor t2[(a shr 40) and $FF] xor t1[(a shr 56) and $FF]);
  186. c:= c * 9;
  187. b:= b xor x[7];
  188. c:= c - (t1[b and $FF] xor t2[(b shr 16) and $FF] xor t3[(b shr 32) and $FF] xor t4[(b shr 48) and $FF]);
  189. a:= a + (t4[(b shr 8) and $FF] xor t3[(b shr 24) and $FF] xor t2[(b shr 40) and $FF] xor t1[(b shr 56) and $FF]);
  190. a:= a * 9;
  191. CurrentHash[0]:= a xor aa;
  192. CurrentHash[1]:= b - bb;
  193. CurrentHash[2]:= c + cc;
  194. Index:= 0;
  195. FillChar(HashBuffer,Sizeof(HashBuffer),0);
  196. end;
  197. class function TDCP_tiger.GetHashSize: integer;
  198. begin
  199. Result:= 192;
  200. end;
  201. class function TDCP_tiger.GetId: integer;
  202. begin
  203. Result:= DCP_tiger;
  204. end;
  205. class function TDCP_tiger.GetAlgorithm: string;
  206. begin
  207. Result:= 'Tiger';
  208. end;
  209. class function TDCP_tiger.SelfTest: boolean;
  210. const
  211. Test1Out: array[0..2] of int64=
  212. ($87FB2A9083851CF7,$470D2CF810E6DF9E,$B586445034A5A386);
  213. Test2Out: array[0..2] of int64=
  214. ($0C410A042968868A,$1671DA5A3FD29A72,$5EC1E457D3CDB303);
  215. var
  216. TestHash: TDCP_tiger;
  217. TestOut: array[0..2] of int64;
  218. begin
  219. dcpFillChar(TestOut, SizeOf(TestOut), 0);
  220. TestHash:= TDCP_tiger.Create(nil);
  221. TestHash.Init;
  222. TestHash.UpdateStr('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-');
  223. TestHash.Final(TestOut);
  224. Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
  225. TestHash.Init;
  226. TestHash.UpdateStr('Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham');
  227. TestHash.Final(TestOut);
  228. Result:= CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out)) and Result;
  229. TestHash.Free;
  230. end;
  231. procedure TDCP_tiger.Init;
  232. begin
  233. Burn;
  234. fInitialized:= true;
  235. CurrentHash[0]:= $0123456789ABCDEF;
  236. CurrentHash[1]:= $FEDCBA9876543210;
  237. CurrentHash[2]:= $F096A5B4C3B2E187;
  238. end;
  239. procedure TDCP_tiger.Burn;
  240. begin
  241. Len:= 0;
  242. Index:= 0;
  243. FillChar(HashBuffer,Sizeof(HashBuffer),0);
  244. FillChar(CurrentHash,Sizeof(CurrentHash),0);
  245. fInitialized:= false;
  246. end;
  247. procedure TDCP_tiger.Update(const Buffer; Size: longword);
  248. var
  249. PBuf: ^byte;
  250. begin
  251. if not fInitialized then
  252. raise EDCP_hash.Create('Hash not initialized');
  253. Inc(Len,Size*8);
  254. PBuf:= @Buffer;
  255. while Size> 0 do
  256. begin
  257. if (Sizeof(HashBuffer)-Index)<= DWord(Size) then
  258. begin
  259. Move(PBuf^,HashBuffer[Index],Sizeof(HashBuffer)-Index);
  260. Dec(Size,Sizeof(HashBuffer)-Index);
  261. Inc(PBuf,Sizeof(HashBuffer)-Index);
  262. Compress;
  263. end
  264. else
  265. begin
  266. Move(PBuf^,HashBuffer[Index],Size);
  267. Inc(Index,Size);
  268. Size:= 0;
  269. end;
  270. end;
  271. end;
  272. procedure TDCP_tiger.Final(var Digest);
  273. begin
  274. if not fInitialized then
  275. raise EDCP_hash.Create('Hash not initialized');
  276. HashBuffer[Index]:= $01;
  277. if Index>= 56 then
  278. Compress;
  279. Pint64(@HashBuffer[56])^:= Len;
  280. Compress;
  281. Move(CurrentHash,Digest,Sizeof(CurrentHash));
  282. Burn;
  283. end;
  284. end.