Debian Codegraph @

components/kascrypt/Hashes/dcpmd5.pas

raw
1 {******************************************************************************}
2 {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
3 {******************************************************************************}
4 {* A binary compatible implementation of MD5 **********************************}
5 {******************************************************************************}
6 {* Copyright (c) 1999-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 DCPmd5;
26
27 {$MODE Delphi}
28
29 interface
30 uses
31 Classes, Sysutils, DCPcrypt2, DCPconst;
32
33 type
34 TDCP_md5= class(TDCP_hash)
35 protected
36 LenHi, LenLo: longword;
37 Index: DWord;
38 CurrentHash: array[0..3] of DWord;
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 {******************************************************************************}
55 {******************************************************************************}
56 implementation
57 {$R-}{$Q-}
58
59 {$macro on}{$define LRot32:=RolDWord}
60
61 procedure TDCP_md5.Compress;
62 var
63 Data: array[0..15] of dword;
64 A, B, C, D: dword;
65 begin
66 dcpFillChar(Data, SizeOf(Data), 0);
67 Move(HashBuffer,Data,Sizeof(Data));
68 A:= CurrentHash[0];
69 B:= CurrentHash[1];
70 C:= CurrentHash[2];
71 D:= CurrentHash[3];
72
73 A:= B + LRot32(A + (D xor (B and (C xor D))) + Data[ 0] + $d76aa478,7);
74 D:= A + LRot32(D + (C xor (A and (B xor C))) + Data[ 1] + $e8c7b756,12);
75 C:= D + LRot32(C + (B xor (D and (A xor B))) + Data[ 2] + $242070db,17);
76 B:= C + LRot32(B + (A xor (C and (D xor A))) + Data[ 3] + $c1bdceee,22);
77 A:= B + LRot32(A + (D xor (B and (C xor D))) + Data[ 4] + $f57c0faf,7);
78 D:= A + LRot32(D + (C xor (A and (B xor C))) + Data[ 5] + $4787c62a,12);
79 C:= D + LRot32(C + (B xor (D and (A xor B))) + Data[ 6] + $a8304613,17);
80 B:= C + LRot32(B + (A xor (C and (D xor A))) + Data[ 7] + $fd469501,22);
81 A:= B + LRot32(A + (D xor (B and (C xor D))) + Data[ 8] + $698098d8,7);
82 D:= A + LRot32(D + (C xor (A and (B xor C))) + Data[ 9] + $8b44f7af,12);
83 C:= D + LRot32(C + (B xor (D and (A xor B))) + Data[10] + $ffff5bb1,17);
84 B:= C + LRot32(B + (A xor (C and (D xor A))) + Data[11] + $895cd7be,22);
85 A:= B + LRot32(A + (D xor (B and (C xor D))) + Data[12] + $6b901122,7);
86 D:= A + LRot32(D + (C xor (A and (B xor C))) + Data[13] + $fd987193,12);
87 C:= D + LRot32(C + (B xor (D and (A xor B))) + Data[14] + $a679438e,17);
88 B:= C + LRot32(B + (A xor (C and (D xor A))) + Data[15] + $49b40821,22);
89
90 A:= B + LRot32(A + (C xor (D and (B xor C))) + Data[ 1] + $f61e2562,5);
91 D:= A + LRot32(D + (B xor (C and (A xor B))) + Data[ 6] + $c040b340,9);
92 C:= D + LRot32(C + (A xor (B and (D xor A))) + Data[11] + $265e5a51,14);
93 B:= C + LRot32(B + (D xor (A and (C xor D))) + Data[ 0] + $e9b6c7aa,20);
94 A:= B + LRot32(A + (C xor (D and (B xor C))) + Data[ 5] + $d62f105d,5);
95 D:= A + LRot32(D + (B xor (C and (A xor B))) + Data[10] + $02441453,9);
96 C:= D + LRot32(C + (A xor (B and (D xor A))) + Data[15] + $d8a1e681,14);
97 B:= C + LRot32(B + (D xor (A and (C xor D))) + Data[ 4] + $e7d3fbc8,20);
98 A:= B + LRot32(A + (C xor (D and (B xor C))) + Data[ 9] + $21e1cde6,5);
99 D:= A + LRot32(D + (B xor (C and (A xor B))) + Data[14] + $c33707d6,9);
100 C:= D + LRot32(C + (A xor (B and (D xor A))) + Data[ 3] + $f4d50d87,14);
101 B:= C + LRot32(B + (D xor (A and (C xor D))) + Data[ 8] + $455a14ed,20);
102 A:= B + LRot32(A + (C xor (D and (B xor C))) + Data[13] + $a9e3e905,5);
103 D:= A + LRot32(D + (B xor (C and (A xor B))) + Data[ 2] + $fcefa3f8,9);
104 C:= D + LRot32(C + (A xor (B and (D xor A))) + Data[ 7] + $676f02d9,14);
105 B:= C + LRot32(B + (D xor (A and (C xor D))) + Data[12] + $8d2a4c8a,20);
106
107 A:= B + LRot32(A + (B xor C xor D) + Data[ 5] + $fffa3942,4);
108 D:= A + LRot32(D + (A xor B xor C) + Data[ 8] + $8771f681,11);
109 C:= D + LRot32(C + (D xor A xor B) + Data[11] + $6d9d6122,16);
110 B:= C + LRot32(B + (C xor D xor A) + Data[14] + $fde5380c,23);
111 A:= B + LRot32(A + (B xor C xor D) + Data[ 1] + $a4beea44,4);
112 D:= A + LRot32(D + (A xor B xor C) + Data[ 4] + $4bdecfa9,11);
113 C:= D + LRot32(C + (D xor A xor B) + Data[ 7] + $f6bb4b60,16);
114 B:= C + LRot32(B + (C xor D xor A) + Data[10] + $bebfbc70,23);
115 A:= B + LRot32(A + (B xor C xor D) + Data[13] + $289b7ec6,4);
116 D:= A + LRot32(D + (A xor B xor C) + Data[ 0] + $eaa127fa,11);
117 C:= D + LRot32(C + (D xor A xor B) + Data[ 3] + $d4ef3085,16);
118 B:= C + LRot32(B + (C xor D xor A) + Data[ 6] + $04881d05,23);
119 A:= B + LRot32(A + (B xor C xor D) + Data[ 9] + $d9d4d039,4);
120 D:= A + LRot32(D + (A xor B xor C) + Data[12] + $e6db99e5,11);
121 C:= D + LRot32(C + (D xor A xor B) + Data[15] + $1fa27cf8,16);
122 B:= C + LRot32(B + (C xor D xor A) + Data[ 2] + $c4ac5665,23);
123
124 A:= B + LRot32(A + (C xor (B or (not D))) + Data[ 0] + $f4292244,6);
125 D:= A + LRot32(D + (B xor (A or (not C))) + Data[ 7] + $432aff97,10);
126 C:= D + LRot32(C + (A xor (D or (not B))) + Data[14] + $ab9423a7,15);
127 B:= C + LRot32(B + (D xor (C or (not A))) + Data[ 5] + $fc93a039,21);
128 A:= B + LRot32(A + (C xor (B or (not D))) + Data[12] + $655b59c3,6);
129 D:= A + LRot32(D + (B xor (A or (not C))) + Data[ 3] + $8f0ccc92,10);
130 C:= D + LRot32(C + (A xor (D or (not B))) + Data[10] + $ffeff47d,15);
131 B:= C + LRot32(B + (D xor (C or (not A))) + Data[ 1] + $85845dd1,21);
132 A:= B + LRot32(A + (C xor (B or (not D))) + Data[ 8] + $6fa87e4f,6);
133 D:= A + LRot32(D + (B xor (A or (not C))) + Data[15] + $fe2ce6e0,10);
134 C:= D + LRot32(C + (A xor (D or (not B))) + Data[ 6] + $a3014314,15);
135 B:= C + LRot32(B + (D xor (C or (not A))) + Data[13] + $4e0811a1,21);
136 A:= B + LRot32(A + (C xor (B or (not D))) + Data[ 4] + $f7537e82,6);
137 D:= A + LRot32(D + (B xor (A or (not C))) + Data[11] + $bd3af235,10);
138 C:= D + LRot32(C + (A xor (D or (not B))) + Data[ 2] + $2ad7d2bb,15);
139 B:= C + LRot32(B + (D xor (C or (not A))) + Data[ 9] + $eb86d391,21);
140
141 Inc(CurrentHash[0],A);
142 Inc(CurrentHash[1],B);
143 Inc(CurrentHash[2],C);
144 Inc(CurrentHash[3],D);
145 Index:= 0;
146 FillChar(HashBuffer,Sizeof(HashBuffer),0);
147 end;
148
149 class function TDCP_md5.GetHashSize: integer;
150 begin
151 Result:= 128;
152 end;
153
154 class function TDCP_md5.GetId: integer;
155 begin
156 Result:= DCP_md5;
157 end;
158
159 class function TDCP_md5.GetAlgorithm: string;
160 begin
161 Result:= 'MD5';
162 end;
163
164 class function TDCP_md5.SelfTest: boolean;
165 const
166 Test1Out: array[0..15] of byte=
167 ($90,$01,$50,$98,$3c,$d2,$4f,$b0,$d6,$96,$3f,$7d,$28,$e1,$7f,$72);
168 Test2Out: array[0..15] of byte=
169 ($c3,$fc,$d3,$d7,$61,$92,$e4,$00,$7d,$fb,$49,$6c,$ca,$67,$e1,$3b);
170 var
171 TestHash: TDCP_md5;
172 TestOut: array[0..19] of byte;
173 begin
174 dcpFillChar(TestOut, SizeOf(TestOut), 0);
175 TestHash:= TDCP_md5.Create(nil);
176 TestHash.Init;
177 TestHash.UpdateStr('abc');
178 TestHash.Final(TestOut);
179 Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
180 TestHash.Init;
181 TestHash.UpdateStr('abcdefghijklmnopqrstuvwxyz');
182 TestHash.Final(TestOut);
183 Result:= CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out)) and Result;
184 TestHash.Free;
185 end;
186
187 procedure TDCP_md5.Init;
188 begin
189 Burn;
190 CurrentHash[0]:= $67452301;
191 CurrentHash[1]:= $efcdab89;
192 CurrentHash[2]:= $98badcfe;
193 CurrentHash[3]:= $10325476;
194 fInitialized:= true;
195 end;
196
197 procedure TDCP_md5.Burn;
198 begin
199 LenHi:= 0; LenLo:= 0;
200 Index:= 0;
201 FillChar(HashBuffer,Sizeof(HashBuffer),0);
202 FillChar(CurrentHash,Sizeof(CurrentHash),0);
203 fInitialized:= false;
204 end;
205
206 procedure TDCP_md5.Update(const Buffer; Size: longword);
207 var
208 PBuf: ^byte;
209 begin
210 if not fInitialized then
211 raise EDCP_hash.Create('Hash not initialized');
212
213 Inc(LenHi,Size shr 29);
214 Inc(LenLo,Size*8);
215 if LenLo< (Size*8) then
216 Inc(LenHi);
217
218 PBuf:= @Buffer;
219 while Size> 0 do
220 begin
221 if (Sizeof(HashBuffer)-Index)<= DWord(Size) then
222 begin
223 Move(PBuf^,HashBuffer[Index],Sizeof(HashBuffer)-Index);
224 Dec(Size,Sizeof(HashBuffer)-Index);
225 Inc(PBuf,Sizeof(HashBuffer)-Index);
226 Compress;
227 end
228 else
229 begin
230 Move(PBuf^,HashBuffer[Index],Size);
231 Inc(Index,Size);
232 Size:= 0;
233 end;
234 end;
235 end;
236
237 procedure TDCP_md5.Final(var Digest);
238 begin
239 if not fInitialized then
240 raise EDCP_hash.Create('Hash not initialized');
241 HashBuffer[Index]:= $80;
242 if Index>= 56 then
243 Compress;
244 PDWord(@HashBuffer[56])^:= LenLo;
245 PDWord(@HashBuffer[60])^:= LenHi;
246 Compress;
247 Move(CurrentHash,Digest,Sizeof(CurrentHash));
248 Burn;
249 end;
250
251 end.