Debian Codegraph @

components/kascrypt/Hashes/dcptiger.pas

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