Debian Codegraph @

components/kascrypt/Hashes/dcpmd4.pas

raw
1 {******************************************************************************}
2 {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
3 {******************************************************************************}
4 {* A binary compatible implementation of MD4 **********************************}
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 DCPmd4;
26
27 {$MODE Delphi}
28
29 interface
30 uses
31 Classes, Sysutils, DCPcrypt2, DCPconst;
32
33 type
34 TDCP_md4= 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 function LRot32(a, b: longword): longword;
60 begin
61 Result:= (a shl b) or (a shr (32-b));
62 end;
63
64 procedure TDCP_md4.Compress;
65 var
66 Data: array[0..15] of dword;
67 A, B, C, D: dword;
68 begin
69 dcpFillChar(Data, SizeOf(Data), 0);
70 Move(HashBuffer,Data,Sizeof(Data));
71 A:= CurrentHash[0];
72 B:= CurrentHash[1];
73 C:= CurrentHash[2];
74 D:= CurrentHash[3];
75
76 A:= LRot32(A + (D xor (B and (C xor D))) + Data[ 0],3);
77 D:= LRot32(D + (C xor (A and (B xor C))) + Data[ 1],7);
78 C:= LRot32(C + (B xor (D and (A xor B))) + Data[ 2],11);
79 B:= LRot32(B + (A xor (C and (D xor A))) + Data[ 3],19);
80 A:= LRot32(A + (D xor (B and (C xor D))) + Data[ 4],3);
81 D:= LRot32(D + (C xor (A and (B xor C))) + Data[ 5],7);
82 C:= LRot32(C + (B xor (D and (A xor B))) + Data[ 6],11);
83 B:= LRot32(B + (A xor (C and (D xor A))) + Data[ 7],19);
84 A:= LRot32(A + (D xor (B and (C xor D))) + Data[ 8],3);
85 D:= LRot32(D + (C xor (A and (B xor C))) + Data[ 9],7);
86 C:= LRot32(C + (B xor (D and (A xor B))) + Data[10],11);
87 B:= LRot32(B + (A xor (C and (D xor A))) + Data[11],19);
88 A:= LRot32(A + (D xor (B and (C xor D))) + Data[12],3);
89 D:= LRot32(D + (C xor (A and (B xor C))) + Data[13],7);
90 C:= LRot32(C + (B xor (D and (A xor B))) + Data[14],11);
91 B:= LRot32(B + (A xor (C and (D xor A))) + Data[15],19);
92
93 A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 0] + $5a827999,3);
94 D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 4] + $5a827999,5);
95 C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[ 8] + $5a827999,9);
96 B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[12] + $5a827999,13);
97 A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 1] + $5a827999,3);
98 D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 5] + $5a827999,5);
99 C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[ 9] + $5a827999,9);
100 B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[13] + $5a827999,13);
101 A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 2] + $5a827999,3);
102 D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 6] + $5a827999,5);
103 C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[10] + $5a827999,9);
104 B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[14] + $5a827999,13);
105 A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 3] + $5a827999,3);
106 D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 7] + $5a827999,5);
107 C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[11] + $5a827999,9);
108 B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[15] + $5a827999,13);
109
110 A:= LRot32(A + (B xor C xor D) + Data[ 0] + $6ed9eba1,3);
111 D:= LRot32(D + (A xor B xor C) + Data[ 8] + $6ed9eba1,9);
112 C:= LRot32(C + (D xor A xor B) + Data[ 4] + $6ed9eba1,11);
113 B:= LRot32(B + (C xor D xor A) + Data[12] + $6ed9eba1,15);
114 A:= LRot32(A + (B xor C xor D) + Data[ 2] + $6ed9eba1,3);
115 D:= LRot32(D + (A xor B xor C) + Data[10] + $6ed9eba1,9);
116 C:= LRot32(C + (D xor A xor B) + Data[ 6] + $6ed9eba1,11);
117 B:= LRot32(B + (C xor D xor A) + Data[14] + $6ed9eba1,15);
118 A:= LRot32(A + (B xor C xor D) + Data[ 1] + $6ed9eba1,3);
119 D:= LRot32(D + (A xor B xor C) + Data[ 9] + $6ed9eba1,9);
120 C:= LRot32(C + (D xor A xor B) + Data[ 5] + $6ed9eba1,11);
121 B:= LRot32(B + (C xor D xor A) + Data[13] + $6ed9eba1,15);
122 A:= LRot32(A + (B xor C xor D) + Data[ 3] + $6ed9eba1,3);
123 D:= LRot32(D + (A xor B xor C) + Data[11] + $6ed9eba1,9);
124 C:= LRot32(C + (D xor A xor B) + Data[ 7] + $6ed9eba1,11);
125 B:= LRot32(B + (C xor D xor A) + Data[15] + $6ed9eba1,15);
126
127 Inc(CurrentHash[0],A);
128 Inc(CurrentHash[1],B);
129 Inc(CurrentHash[2],C);
130 Inc(CurrentHash[3],D);
131 Index:= 0;
132 FillChar(HashBuffer,Sizeof(HashBuffer),0);
133 end;
134
135 class function TDCP_md4.GetHashSize: integer;
136 begin
137 Result:= 128;
138 end;
139
140 class function TDCP_md4.GetId: integer;
141 begin
142 Result:= DCP_md4;
143 end;
144
145 class function TDCP_md4.GetAlgorithm: string;
146 begin
147 Result:= 'MD4';
148 end;
149
150 class function TDCP_md4.SelfTest: boolean;
151 const
152 Test1Out: array[0..15] of byte=
153 ($a4,$48,$01,$7a,$af,$21,$d8,$52,$5f,$c1,$0a,$e8,$7a,$a6,$72,$9d);
154 Test2Out: array[0..15] of byte=
155 ($d7,$9e,$1c,$30,$8a,$a5,$bb,$cd,$ee,$a8,$ed,$63,$df,$41,$2d,$a9);
156 var
157 TestHash: TDCP_md4;
158 TestOut: array[0..19] of byte;
159 begin
160 dcpFillChar(TestOut, SizeOf(TestOut), 0);
161 TestHash:= TDCP_md4.Create(nil);
162 TestHash.Init;
163 TestHash.UpdateStr('abc');
164 TestHash.Final(TestOut);
165 Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
166 TestHash.Init;
167 TestHash.UpdateStr('abcdefghijklmnopqrstuvwxyz');
168 TestHash.Final(TestOut);
169 Result:= CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out)) and Result;
170 TestHash.Free;
171 end;
172
173 procedure TDCP_md4.Init;
174 begin
175 Burn;
176 CurrentHash[0]:= $67452301;
177 CurrentHash[1]:= $efcdab89;
178 CurrentHash[2]:= $98badcfe;
179 CurrentHash[3]:= $10325476;
180 fInitialized:= true;
181 end;
182
183 procedure TDCP_md4.Burn;
184 begin
185 LenHi:= 0; LenLo:= 0;
186 Index:= 0;
187 FillChar(HashBuffer,Sizeof(HashBuffer),0);
188 FillChar(CurrentHash,Sizeof(CurrentHash),0);
189 fInitialized:= false;
190 end;
191
192 procedure TDCP_md4.Update(const Buffer; Size: longword);
193 var
194 PBuf: ^byte;
195 begin
196 if not fInitialized then
197 raise EDCP_hash.Create('Hash not initialized');
198
199 Inc(LenHi,Size shr 29);
200 Inc(LenLo,Size*8);
201 if LenLo< (Size*8) then
202 Inc(LenHi);
203
204 PBuf:= @Buffer;
205 while Size> 0 do
206 begin
207 if (Sizeof(HashBuffer)-Index)<= DWord(Size) then
208 begin
209 Move(PBuf^,HashBuffer[Index],Sizeof(HashBuffer)-Index);
210 Dec(Size,Sizeof(HashBuffer)-Index);
211 Inc(PBuf,Sizeof(HashBuffer)-Index);
212 Compress;
213 end
214 else
215 begin
216 Move(PBuf^,HashBuffer[Index],Size);
217 Inc(Index,Size);
218 Size:= 0;
219 end;
220 end;
221 end;
222
223 procedure TDCP_md4.Final(var Digest);
224 begin
225 if not fInitialized then
226 raise EDCP_hash.Create('Hash not initialized');
227 HashBuffer[Index]:= $80;
228 if Index>= 56 then
229 Compress;
230 PDWord(@HashBuffer[56])^:= LenLo;
231 PDWord(@HashBuffer[60])^:= LenHi;
232 Compress;
233 Move(CurrentHash,Digest,Sizeof(CurrentHash));
234 Burn;
235 end;
236
237 end.