File: dcphaval.pas

package info (click to toggle)
doublecmd 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,380 kB
  • sloc: pascal: 368,788; ansic: 6,001; sh: 769; makefile: 196; python: 52; xml: 8
file content (503 lines) | stat: -rw-r--r-- 15,734 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of Haval ********************************}
{******************************************************************************}
{* Copyright (c) 1999-2002 David Barton                                       *}
{* Permission is hereby granted, free of charge, to any person obtaining a    *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation  *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense,   *}
{* and/or sell copies of the Software, and to permit persons to whom the      *}
{* Software is furnished to do so, subject to the following conditions:       *}
{*                                                                            *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software.                        *}
{*                                                                            *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        *}
{* DEALINGS IN THE SOFTWARE.                                                  *}
{******************************************************************************}
unit DCPhaval;

{$MODE Delphi}

interface
uses
  Classes, Sysutils, DCPcrypt2, DCPconst;

type
  TDCP_haval= class(TDCP_hash)
  protected
    LenHi, LenLo: longword;
    Index: DWord;
    CurrentHash: array[0..7] of DWord;
    HashBuffer: array[0..127] of byte;
    procedure Compress;
  public
    class function GetId: integer; override;
    class function GetAlgorithm: string; override;
    class function GetHashSize: integer; override;
    class function SelfTest: boolean; override;
    procedure Init; override;
    procedure Burn; override;
    procedure Update(const Buffer; Size: longword); override;
    procedure Final(var Digest); override;
  end;

{ Choose how many passes (previous versions of DCPcrypt uses 5 passes) }
{ ONLY UNCOMMENT ONE! }
//{$DEFINE PASS3}
//{$DEFINE PASS4}
{$DEFINE PASS5}

{ Choose digest length (previous versions of DCPcrypt uses 256bits) }
{ ONLY UNCOMMENT ONE! }
//{$DEFINE DIGEST128}
//{$DEFINE DIGEST160}
//{$DEFINE DIGEST192}
//{$DEFINE DIGEST224}
{$DEFINE DIGEST256}


{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}

procedure TDCP_haval.Compress;
var
  t7, t6, t5, t4, t3, t2, t1, t0: DWord;
  W: array[0..31] of DWord;
  Temp: dword;
begin
  dcpFillChar(W, SizeOf(W), 0);
  t0:= CurrentHash[0];
  t1:= CurrentHash[1];
  t2:= CurrentHash[2];
  t3:= CurrentHash[3];
  t4:= CurrentHash[4];
  t5:= CurrentHash[5];
  t6:= CurrentHash[6];
  t7:= CurrentHash[7];
  Move(HashBuffer,W,Sizeof(W));

{$IFDEF PASS3}
  {$INCLUDE DCPhaval3.inc}
{$ELSE}
  {$IFDEF PASS4}
    {$INCLUDE DCPhaval4.inc}
  {$ELSE}
    {$INCLUDE DCPhaval5.inc}
  {$ENDIF}
{$ENDIF}

  Inc(CurrentHash[0],t0);
  Inc(CurrentHash[1],t1);
  Inc(CurrentHash[2],t2);
  Inc(CurrentHash[3],t3);
  Inc(CurrentHash[4],t4);
  Inc(CurrentHash[5],t5);
  Inc(CurrentHash[6],t6);
  Inc(CurrentHash[7],t7);
  FillChar(W,Sizeof(W),0);
  Index:= 0;
  FillChar(HashBuffer,Sizeof(HashBuffer),0);
end;

class function TDCP_haval.GetHashSize: integer;
begin
{$IFDEF DIGEST128}
  Result:= 128;
{$ELSE}
  {$IFDEF DIGEST160}
    Result:= 160;
  {$ELSE}
    {$IFDEF DIGEST192}
      Result:= 192;
    {$ELSE}
      {$IFDEF DIGEST224}
        Result:= 224;
      {$ELSE}
        Result:= 256;
      {$ENDIF}
    {$ENDIF}
  {$ENDIF}
{$ENDIF}
end;

class function TDCP_haval.GetId: integer;
begin
  Result:= DCP_haval;
end;

class function TDCP_haval.GetAlgorithm: string;
begin
  Result:= 'Haval (';
{$IFDEF DIGEST128}
  Result:= Result+'128bit, ';
{$ELSE}
  {$IFDEF DIGEST160}
    Result:= Result+'160bit, ';
  {$ELSE}
    {$IFDEF DIGEST192}
      Result:= Result+'192bit, ';
    {$ELSE}
      {$IFDEF DIGEST224}
        Result:= Result+'224bit, ';
      {$ELSE}
        Result:= Result+'256bit, ';
      {$ENDIF}
    {$ENDIF}
  {$ENDIF}
{$ENDIF}
{$IFDEF PASS3}
  Result:= Result+'3 passes)';
{$ELSE}
  {$IFDEF PASS4}
    Result:= Result+'4 passes)';
  {$ELSE}
    Result:= Result+'5 passes)';
  {$ENDIF}
{$ENDIF}
end;

class function TDCP_haval.SelfTest: boolean;
{$IFDEF PASS3}
  {$IFDEF DIGEST128}
    const
      Test1Out: array[0..15] of byte=
        ($1B,$DC,$55,$6B,$29,$AD,$02,$EC,$09,$AF,$8C,$66,$47,$7F,$2A,$87);
    var
      TestHash: TDCP_haval;
      TestOut: array[0..15] of byte;
    begin
      TestHash:= TDCP_haval.Create(nil);
      TestHash.Init;
      TestHash.UpdateStr('');
      TestHash.Final(TestOut);
      Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
      TestHash.Free;
  {$ELSE}
    {$IFDEF DIGEST160}
      const
        Test1Out: array[0..19] of byte=
          ($5E,$16,$10,$FC,$ED,$1D,$3A,$DB,$0B,$B1,
           $8E,$92,$AC,$2B,$11,$F0,$BD,$99,$D8,$ED);
      var
        TestHash: TDCP_haval;
        TestOut: array[0..19] of byte;
      begin
        TestHash:= TDCP_haval.Create(nil);
        TestHash.Init;
        TestHash.UpdateStr('a');
        TestHash.Final(TestOut);
        Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
        TestHash.Free;
    {$ELSE}
      begin
        Result:= true;
    {$ENDIF}
  {$ENDIF}
{$ELSE}
  {$IFDEF PASS4}
    {$IFDEF DIGEST192}
      const
        Test1Out: array[0..23] of byte=
          ($74,$AA,$31,$18,$2F,$F0,$9B,$CC,$E4,$53,$A7,$F7,
           $1B,$5A,$7C,$5E,$80,$87,$2F,$A9,$0C,$D9,$3A,$E4);
      var
        TestHash: TDCP_haval;
        TestOut: array[0..23] of byte;
      begin
        TestHash:= TDCP_haval.Create(nil);
        TestHash.Init;
        TestHash.UpdateStr('HAVAL');
        TestHash.Final(TestOut);
        Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
        TestHash.Free;
    {$ELSE}
      {$IFDEF DIGEST224}
        const
          Test1Out: array[0..27] of byte=
            ($14,$4C,$B2,$DE,$11,$F0,$5D,$F7,$C3,$56,$28,$2A,$3B,$48,
             $57,$96,$DA,$65,$3F,$6B,$70,$28,$68,$C7,$DC,$F4,$AE,$76);
        var
          TestHash: TDCP_haval;
          TestOut: array[0..27] of byte;
        begin
          TestHash:= TDCP_haval.Create(nil);
          TestHash.Init;
          TestHash.UpdateStr('0123456789');
          TestHash.Final(TestOut);
          Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
          TestHash.Free;
      {$ELSE}
        begin
          Result:= true;
      {$ENDIF}
    {$ENDIF}
  {$ELSE}
    {$IFDEF DIGEST256}
      const
        Test1Out: array[0..31] of byte=
          ($1A,$1D,$C8,$09,$9B,$DA,$A7,$F3,$5B,$4D,$A4,$E8,$05,$F1,$A2,$8F,
           $EE,$90,$9D,$8D,$EE,$92,$01,$98,$18,$5C,$BC,$AE,$D8,$A1,$0A,$8D);
        Test2Out: array[0..31] of byte=
          ($C5,$64,$7F,$C6,$C1,$87,$7F,$FF,$96,$74,$2F,$27,$E9,$26,$6B,$68,
           $74,$89,$4F,$41,$A0,$8F,$59,$13,$03,$3D,$9D,$53,$2A,$ED,$DB,$39);
      var
        TestHash: TDCP_haval;
        TestOut: array[0..31] of byte;
      begin
        dcpFillChar(TestOut, SizeOf(TestOut), 0);
        TestHash:= TDCP_haval.Create(nil);
        TestHash.Init;
        TestHash.UpdateStr('abcdefghijklmnopqrstuvwxyz');
        TestHash.Final(TestOut);
        Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
        TestHash.Init;
        TestHash.UpdateStr('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
        TestHash.Final(TestOut);
        Result:= CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out)) and Result;
        TestHash.Free;
    {$ELSE}
      begin
        Result:= true;
    {$ENDIF}
  {$ENDIF}
{$ENDIF}
end;

procedure TDCP_haval.Init;
begin
  Burn;
  CurrentHash[0]:= $243F6A88;
  CurrentHash[1]:= $85A308D3;
  CurrentHash[2]:= $13198A2E;
  CurrentHash[3]:= $03707344;
  CurrentHash[4]:= $A4093822;
  CurrentHash[5]:= $299F31D0;
  CurrentHash[6]:= $082EFA98;
  CurrentHash[7]:= $EC4E6C89;
  fInitialized:= true;
end;

procedure TDCP_haval.Burn;
begin
  LenHi:= 0; LenLo:= 0;
  Index:= 0;
  FillChar(HashBuffer,Sizeof(HashBuffer),0);
  FillChar(CurrentHash,Sizeof(CurrentHash),0);
  fInitialized:= false;
end;

procedure TDCP_haval.Update(const Buffer; Size: longword);
var
  PBuf: ^byte;
begin
  if not fInitialized then
    raise EDCP_hash.Create('Hash not initialized');

  Inc(LenHi,Size shr 29);
  Inc(LenLo,Size*8);
  if LenLo< (Size*8) then
    Inc(LenHi);

  PBuf:= @Buffer;
  while Size> 0 do
  begin
    if (Sizeof(HashBuffer)-Index)<= DWord(Size) then
    begin
      Move(PBuf^,HashBuffer[Index],Sizeof(HashBuffer)-Index);
      Dec(Size,Sizeof(HashBuffer)-Index);
      Inc(PBuf,Sizeof(HashBuffer)-Index);
      Compress;
    end
    else
    begin
      Move(PBuf^,HashBuffer[Index],Size);
      Inc(Index,Size);
      Size:= 0;
    end;
  end;
end;

procedure TDCP_haval.Final(var Digest);
{$IFNDEF DIGEST256}
  {$IFNDEF DIGEST224}
    var
      temp: dword;
  {$ENDIF}
{$ENDIF}
begin
  if not fInitialized then
    raise EDCP_hash.Create('Hash not initialized');
  HashBuffer[Index]:= $80;
  if Index>= 118 then
    Compress;
{$IFDEF PASS3}
  {$IFDEF DIGEST128}
    HashBuffer[118]:= ((128 and 3) shl 6) or (3 shl 3) or 1;
    HashBuffer[119]:= (128 shr 2) and $FF;
  {$ELSE}
    {$IFDEF DIGEST160}
      HashBuffer[118]:= ((160 and 3) shl 6) or (3 shl 3) or 1;
      HashBuffer[119]:= (160 shr 2) and $FF;
    {$ELSE}
      {$IFDEF DIGEST192}
        HashBuffer[118]:= ((192 and 3) shl 6) or (3 shl 3) or 1;
        HashBuffer[119]:= (192 shr 2) and $FF;
      {$ELSE}
        {$IFDEF DIGEST224}
          HashBuffer[118]:= ((224 and 3) shl 6) or (3 shl 3) or 1;
          HashBuffer[119]:= (224 shr 2) and $FF;
        {$ELSE}
          HashBuffer[118]:= ((256 and 3) shl 6) or (3 shl 3) or 1;
          HashBuffer[119]:= (256 shr 2) and $FF;
        {$ENDIF}
      {$ENDIF}
    {$ENDIF}
  {$ENDIF}
{$ELSE}
  {$IFDEF PASS4}
    {$IFDEF DIGEST128}
      HashBuffer[118]:= ((128 and 3) shl 6) or (4 shl 3) or 1;
      HashBuffer[119]:= (128 shr 2) and $FF;
    {$ELSE}
      {$IFDEF DIGEST160}
        HashBuffer[118]:= ((160 and 3) shl 6) or (4 shl 3) or 1;
        HashBuffer[119]:= (160 shr 2) and $FF;
      {$ELSE}
        {$IFDEF DIGEST192}
          HashBuffer[118]:= ((192 and 3) shl 6) or (4 shl 3) or 1;
          HashBuffer[119]:= (192 shr 2) and $FF;
        {$ELSE}
          {$IFDEF DIGEST224}
            HashBuffer[118]:= ((224 and 3) shl 6) or (4 shl 3) or 1;
            HashBuffer[119]:= (224 shr 2) and $FF;
          {$ELSE}
            HashBuffer[118]:= ((256 and 3) shl 6) or (4 shl 3) or 1;
            HashBuffer[119]:= (256 shr 2) and $FF;
          {$ENDIF}
        {$ENDIF}
      {$ENDIF}
    {$ENDIF}
  {$ELSE}
    {$IFDEF DIGEST128}
      HashBuffer[118]:= ((128 and 3) shl 6) or (5 shl 3) or 1;
      HashBuffer[119]:= (2128 shr 2) and $FF;
    {$ELSE}
      {$IFDEF DIGEST160}
        HashBuffer[118]:= ((160 and 3) shl 6) or (5 shl 3) or 1;
        HashBuffer[119]:= (160 shr 2) and $FF;
      {$ELSE}
        {$IFDEF DIGEST192}
          HashBuffer[118]:= ((192 and 3) shl 6) or (5 shl 3) or 1;
          HashBuffer[119]:= (192 shr 2) and $FF;
        {$ELSE}
          {$IFDEF DIGEST224}
            HashBuffer[118]:= ((224 and 3) shl 6) or (5 shl 3) or 1;
            HashBuffer[119]:= (224 shr 2) and $FF;
          {$ELSE}
            HashBuffer[118]:= ((256 and 3) shl 6) or (5 shl 3) or 1;
            HashBuffer[119]:= (256 shr 2) and $FF;
          {$ENDIF}
        {$ENDIF}
      {$ENDIF}
    {$ENDIF}
  {$ENDIF}
{$ENDIF}
  PDWord(@HashBuffer[120])^:= LenLo;
  PDWord(@HashBuffer[124])^:= LenHi;
  Compress;
{$IFDEF DIGEST128}
  temp:= (CurrentHash[7] and $000000FF) or
         (CurrentHash[6] and $FF000000) or
         (CurrentHash[5] and $00FF0000) or
         (CurrentHash[4] and $0000FF00);
  Inc(CurrentHash[0],(temp shr 8) or (temp shl 24));
  temp:= (CurrentHash[7] and $0000FF00) or
         (CurrentHash[6] and $000000FF) or
         (CurrentHash[5] and $FF000000) or
         (CurrentHash[4] and $00FF0000);
  Inc(CurrentHash[1],(temp shr 16) or (temp shl 16));
  temp:= (CurrentHash[7] and $00FF0000) or
         (CurrentHash[6] and $0000FF00) or
         (CurrentHash[5] and $000000FF) or
         (CurrentHash[4] and $FF000000);
  Inc(CurrentHash[2],(temp shr 24) or (temp shl 8));
  temp:= (CurrentHash[7] and $FF000000) or
         (CurrentHash[6] and $00FF0000) or
         (CurrentHash[5] and $0000FF00) or
         (CurrentHash[4] and $000000FF);
  Inc(CurrentHash[3],temp);
  Move(CurrentHash,Digest,128 div 8);
{$ELSE}
  {$IFDEF DIGEST160}
    temp:= (CurrentHash[7] and $3F) or
           (CurrentHash[6] and ($7F shl 25)) or
           (CurrentHash[5] and ($3F shl 19));
    Inc(CurrentHash[0],(temp shr 19) or (temp shl 13));
    temp:= (CurrentHash[7] and ($3F shl 6)) or
           (CurrentHash[6] and $3F) or
           (CurrentHash[5] and ($7F shl 25));
    Inc(CurrentHash[1],(temp shr 25) or (temp shl 7));
    temp:= (CurrentHash[7] and ($7F shl 12)) or
           (CurrentHash[6] and ($3F shl 6)) or
           (CurrentHash[5] and $3F);
    Inc(CurrentHash[2],temp);
    temp:= (CurrentHash[7] and ($3F shl 19)) or
           (CurrentHash[6] and ($7F shl 12)) or
           (CurrentHash[5] and ($3F shl 6));
    Inc(CurrentHash[3],temp shr 6);
    temp:= (CurrentHash[7] and ($7F shl 25)) or
           (CurrentHash[6] and ($3F shl 19)) or
           (CurrentHash[5] and ($7F shl 12));
    Inc(CurrentHash[4],temp shr 12);
    Move(CurrentHash,Digest,160 div 8);
  {$ELSE}
    {$IFDEF DIGEST192}
      temp:= (CurrentHash[7] and $1F) or
             (CurrentHash[6] and ($3F shl 26));
      Inc(CurrentHash[0],(temp shr 26) or (temp shl 6));
      temp:= (CurrentHash[7] and ($1F shl 5)) or
             (CurrentHash[6] and $1F);
      Inc(CurrentHash[1],temp);
      temp:= (CurrentHash[7] and ($3F shl 10)) or
             (CurrentHash[6] and ($1F shl 5));
      Inc(CurrentHash[2],temp shr 5);
      temp:= (CurrentHash[7] and ($1F shl 16)) or
             (CurrentHash[6] and ($3F shl 10));
      Inc(CurrentHash[3],temp shr 10);
      temp:= (CurrentHash[7] and ($1F shl 21)) or
             (CurrentHash[6] and ($1F shl 16));
      Inc(CurrentHash[4],temp shr 16);
      temp:= (CurrentHash[7] and ($3F shl 26)) or
             (CurrentHash[6] and ($1F shl 21));
      Inc(CurrentHash[5],temp shr 21);
      Move(CurrentHash,Digest,192 div 8);
    {$ELSE}
      {$IFDEF DIGEST224}
        Inc(CurrentHash[0],(CurrentHash[7] shr 27) and $1F);
        Inc(CurrentHash[1],(CurrentHash[7] shr 22) and $1F);
        Inc(CurrentHash[2],(CurrentHash[7] shr 18) and $F);
        Inc(CurrentHash[3],(CurrentHash[7] shr 13) and $1F);
        Inc(CurrentHash[4],(CurrentHash[7] shr 9) and $F);
        Inc(CurrentHash[5],(CurrentHash[7] shr 4) and $1F);
        Inc(CurrentHash[6],CurrentHash[7] and $F);
        Move(CurrentHash,Digest,224 div 8);
      {$ELSE}
        Move(CurrentHash,Digest,256 div 8);
      {$ENDIF}
    {$ENDIF}
  {$ENDIF}
{$ENDIF}
  Burn;
end;

end.