This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvilWorks.Crypto.HMAC_SHA1.pas
4018 lines (3564 loc) · 119 KB
/
EvilWorks.Crypto.HMAC_SHA1.pas
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*============================================================================================================
EvilLibrary by Vedran Vuk 2010-2012
Name: EvilWorks.Crypto.HMAC_SHA1
Description: HMAC_SHA1 hasher. Taken from "Fundamentals 4.00" chash.pas library version 4.15.
Original Copyright: Copyright © 1999-2011, David J Butler
Original Home page: http://fundementals.sourceforge.net
Original Forum: http://sourceforge.net/forum/forum.php?forum_id=2117
Original E-mail: fundamentalslib at gmail.com
File last change date: August 15th. 2012
File version: 0.0.1
Licence: Free as in beer.
===========================================================================================================*)
unit EvilWorks.Crypto.HMAC_SHA1;
{$EXTENDEDSYNTAX ON}
{$IOCHECKS ON}
{$LONGSTRINGS ON}
{$BOOLEVAL OFF}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$OPTIMIZATION ON}
{$INLINE ON}
{$HIGHCHARUNICODE OFF}
{ }
{ Windows platform }
{ }
{$IFDEF DOT_NET}
{$DEFINE WindowsPlatform}
{$ENDIF}
{$IFDEF OS_WIN32}
{$DEFINE WindowsPlatform}
{$ENDIF}
{$IFDEF OS_WIN64}
{$DEFINE WindowsPlatform}
{$ENDIF}
{ }
{ CPU type }
{ }
{$IFNDEF ManagedCode}
{$DEFINE NativeCode}
{$ENDIF}
{$IFDEF CPU386}
{$DEFINE INTEL386}
{$DEFINE CPU_INTEL386}
{$ENDIF}
{$IFDEF CPUX64}
{$DEFINE CPU_X86_64}
{$ENDIF}
{$IFDEF CPU86_64}
{$DEFINE CPU_X86_64}
{$ENDIF}
{$IFDEF CPU68K}
{$DEFINE CPU_68K}
{$ENDIF}
{$IFDEF CPUPPC}
{$DEFINE CPU_POWERPC}
{$ENDIF}
{$IFDEF CPUPPC64}
{$DEFINE CPU_POWERPC64}
{$ENDIF}
{$IFDEF CPUARM}
{$DEFINE CPU_ARM}
{$ENDIF}
{ }
{ Assembler style }
{ }
{$IFNDEF PurePascal}
{$IFNDEF ManagedCode}
{$IFDEF CPU_X86_64}
{$DEFINE ASMX86_64}
{$ENDIF}
{$IFDEF CPU_INTEL386}
{$DEFINE ASM386}
{$IFDEF DELPHI}{$IFDEF OS_WIN32}
{$DEFINE ASM386_DELPHI}
{$IFNDEF UseInline} {$DEFINE ASM386_DELPHI_INLINE_OFF} {$ENDIF}
{$ENDIF}{$ENDIF}
{$IFDEF FREEPASCAL2_UP}
{$DEFINE ASM386_FREEPASCAL}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{ }
{ Function inlining }
{ }
{$IFDEF SupportInline}
{$IFNDEF SupportInlineIsBuggy}
{$IFNDEF PurePascal}
{$DEFINE UseInline}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{ }
{ Standard compiler directives }
{ }
{$EXTENDEDSYNTAX ON}
{$IOCHECKS ON}
{$LONGSTRINGS ON}
{$BOOLEVAL OFF}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$IFDEF DEBUG}
{$ASSERTIONS ON}
{$DEBUGINFO ON}
{$OVERFLOWCHECKS ON}
{$RANGECHECKS ON}
{$WARNINGS ON}
{$HINTS ON}
{$ELSE}
{$ASSERTIONS OFF}
{$DEBUGINFO OFF}
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
{$WARNINGS OFF}
{$HINTS OFF}
{$ENDIF}
{$IFDEF CLR}
{$UNSAFECODE OFF}
{$ENDIF}
{$IFDEF DELPHI}
{$OPTIMIZATION ON}
{$ENDIF}
{$IFDEF DELPHI2005_UP}
{$INLINE ON}
{$ENDIF}
{$IFDEF DELPHI2009_UP}
{$HIGHCHARUNICODE OFF}
{$ENDIF}
{ }
{ Compiler warnings }
{ }
{$IFDEF DELPHI7}
{$WARN UNSAFE_CODE OFF}
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CAST OFF}
{$ENDIF}
{$IFDEF DELPHI2007}
{$IFNDEF DOT_NET}
{$WARN UNSAFE_CODE OFF}
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CAST OFF}
{$ENDIF}
{$ENDIF}
{$IFDEF DOT_NET}
{$WARN UNIT_PLATFORM OFF}
{$ENDIF}
{$IFNDEF DEBUG}
{$IFDEF DELPHI6_UP}
{$WARN SYMBOL_PLATFORM OFF}
{$WARN UNIT_PLATFORM OFF}
{$WARN UNIT_DEPRECATED OFF}
{$ENDIF}
{$ENDIF}
interface
uses
WinApi.Windows,
System.SysUtils,
EvilWorks.System.SysUtils;
{ }
{ Hash digests }
{ }
type
Word64 = packed record
case Integer of
0:
(Bytes: array [0 .. 7] of Byte);
1:
(Words: array [0 .. 3] of Word);
2:
(LongWords: array [0 .. 1] of LongWord);
end;
PWord64 = ^Word64;
T128BitDigest = record
case integer of
0:
(Int64s: array [0 .. 1] of Int64);
1:
(Longs: array [0 .. 3] of LongWord);
2:
(Words: array [0 .. 7] of Word);
3:
(Bytes: array [0 .. 15] of Byte);
end;
P128BitDigest = ^T128BitDigest;
T160BitDigest = record
case integer of
0:
(Longs: array [0 .. 4] of LongWord);
1:
(Words: array [0 .. 9] of Word);
2:
(Bytes: array [0 .. 19] of Byte);
end;
P160BitDigest = ^T160BitDigest;
T224BitDigest = record
case integer of
0:
(Longs: array [0 .. 6] of LongWord);
1:
(Words: array [0 .. 13] of Word);
2:
(Bytes: array [0 .. 27] of Byte);
end;
P224BitDigest = ^T224BitDigest;
T256BitDigest = record
case integer of
0:
(Longs: array [0 .. 7] of LongWord);
1:
(Words: array [0 .. 15] of Word);
2:
(Bytes: array [0 .. 31] of Byte);
end;
P256BitDigest = ^T256BitDigest;
T384BitDigest = record
case integer of
0:
(Word64s: array [0 .. 5] of Word64);
1:
(Longs: array [0 .. 11] of LongWord);
2:
(Words: array [0 .. 23] of Word);
3:
(Bytes: array [0 .. 47] of Byte);
end;
P384BitDigest = ^T384BitDigest;
T512BitDigest = record
case integer of
0:
(Word64s: array [0 .. 7] of Word64);
1:
(Longs: array [0 .. 15] of LongWord);
2:
(Words: array [0 .. 31] of Word);
3:
(Bytes: array [0 .. 63] of Byte);
end;
P512BitDigest = ^T512BitDigest;
T512BitBuf = array [0 .. 63] of Byte;
T1024BitBuf = array [0 .. 127] of Byte;
const
MaxHashDigestSize = Sizeof(T160BitDigest);
procedure DigestToHexBufA(const Digest; const Size: Integer; const Buf);
procedure DigestToHexBufW(const Digest; const Size: Integer; const Buf);
function DigestToHexA(const Digest; const Size: Integer): AnsiString;
function DigestToHexW(const Digest; const Size: Integer): WideString;
function Digest128Equal(const Digest1, Digest2: T128BitDigest): Boolean;
function Digest160Equal(const Digest1, Digest2: T160BitDigest): Boolean;
function Digest224Equal(const Digest1, Digest2: T224BitDigest): Boolean;
function Digest256Equal(const Digest1, Digest2: T256BitDigest): Boolean;
function Digest384Equal(const Digest1, Digest2: T384BitDigest): Boolean;
function Digest512Equal(const Digest1, Digest2: T512BitDigest): Boolean;
{ }
{ Hash errors }
{ }
const
hashNoError = 0;
hashInternalError = 1;
hashInvalidHashType = 2;
hashInvalidBuffer = 3;
hashInvalidBufferSize = 4;
hashInvalidDigest = 5;
hashInvalidKey = 6;
hashInvalidFileName = 7;
hashFileOpenError = 8;
hashFileSeekError = 9;
hashFileReadError = 10;
hashNotKeyedHashType = 11;
hashTooManyOpenHandles = 12;
hashInvalidHandle = 13;
hashMAX_ERROR = 13;
function GetHashErrorMessage(const ErrorCode: LongWord): PChar;
type
EHashError = class(Exception)
protected
FErrorCode: LongWord;
public
constructor Create(const ErrorCode: LongWord; const Msg: string = '');
property ErrorCode: LongWord read FErrorCode;
end;
{ }
{ Secure memory clear }
{ Used to clear keys and other sensitive data from memory }
{ }
procedure SecureClear(var Buf; const BufSize: Integer);
procedure SecureClear512(var Buf: T512BitBuf);
procedure SecureClear1024(var Buf: T1024BitBuf);
procedure SecureClearStrA(var S: AnsiString);
procedure SecureClearStrW(var S: WideString);
{ }
{ Checksum hashing }
{ }
function CalcChecksum32(const Buf; const BufSize: Integer): LongWord; overload;
function CalcChecksum32(const Buf: AnsiString): LongWord; overload;
{ }
{ XOR hashing }
{ }
function CalcXOR8(const Buf; const BufSize: Integer): Byte; overload;
function CalcXOR8(const Buf: AnsiString): Byte; overload;
function CalcXOR16(const Buf; const BufSize: Integer): Word; overload;
function CalcXOR16(const Buf: AnsiString): Word; overload;
function CalcXOR32(const Buf; const BufSize: Integer): LongWord; overload;
function CalcXOR32(const Buf: AnsiString): LongWord; overload;
{ }
{ CRC 16 hashing }
{ }
{ The theory behind CCITT V.41 CRCs: }
{ }
{ 1. Select the magnitude of the CRC to be used (typically 16 or 32 }
{ bits) and choose the polynomial to use. In the case of 16 bit }
{ CRCs, the CCITT polynomial is recommended and is }
{ }
{ 16 12 5 }
{ G(x) = x + x + x + 1 }
{ }
{ This polynomial traps 100% of 1 bit, 2 bit, odd numbers of bit }
{ errors, 100% of <= 16 bit burst errors and over 99% of all }
{ other errors. }
{ }
{ 2. The CRC is calculated as }
{ r }
{ D(x) = (M(x) * 2 ) mod G(x) }
{ }
{ This may be better described as : Add r bits (0 content) to }
{ the end of M(x). Divide this by G(x) and the remainder is the }
{ CRC. }
{ }
{ 3. Tag the CRC onto the end of M(x). }
{ }
{ 4. To check it, calculate the CRC of the new message D(x), using }
{ the same process as in 2. above. The newly calculated CRC }
{ should be zero. }
{ }
{ This effectively means that using CRCs, it is possible to calculate a }
{ series of bits to tag onto the data which makes the data an exact }
{ multiple of the polynomial. }
{ }
procedure CRC16Init(var CRC16: Word);
function CRC16Byte(const CRC16: Word; const Octet: Byte): Word;
function CRC16Buf(const CRC16: Word; const Buf; const BufSize: Integer): Word;
function CalcCRC16(const Buf; const BufSize: Integer): Word; overload;
function CalcCRC16(const Buf: AnsiString): Word; overload;
{ }
{ CRC 32 hashing }
{ }
procedure SetCRC32Poly(const Poly: LongWord);
procedure CRC32Init(var CRC32: LongWord);
function CRC32Byte(const CRC32: LongWord; const Octet: Byte): LongWord;
function CRC32Buf(const CRC32: LongWord; const Buf; const BufSize: Integer): LongWord;
function CRC32BufNoCase(const CRC32: LongWord; const Buf; const BufSize: Integer): LongWord;
function CalcCRC32(const Buf; const BufSize: Integer): LongWord; overload;
function CalcCRC32(const Buf: AnsiString): LongWord; overload;
{ }
{ Adler 32 hashing }
{ }
procedure Adler32Init(var Adler32: LongWord);
function Adler32Byte(const Adler32: LongWord; const Octet: Byte): LongWord;
function Adler32Buf(const Adler32: LongWord; const Buf; const BufSize: Integer): LongWord;
function CalcAdler32(const Buf; const BufSize: Integer): LongWord; overload;
function CalcAdler32(const Buf: AnsiString): LongWord; overload;
{ }
{ ELF hashing }
{ }
procedure ELFInit(var Digest: LongWord);
function ELFBuf(const Digest: LongWord; const Buf; const BufSize: Integer): LongWord;
function CalcELF(const Buf; const BufSize: Integer): LongWord; overload;
function CalcELF(const Buf: AnsiString): LongWord; overload;
{ }
{ ISBN checksum }
{ }
function IsValidISBN(const S: AnsiString): Boolean;
{ }
{ LUHN checksum }
{ }
{ The LUHN forumula (also known as mod-10) is used in major credit card }
{ account numbers for validity checking. }
{ }
function IsValidLUHN(const S: AnsiString): Boolean;
{ }
{ Knuth hash }
{ General purpose string hashing function proposed by Donald E Knuth in }
{ 'The Art of Computer Programming Vol 3'. }
{ }
function KnuthHashA(const S: AnsiString): LongWord;
function KnuthHashW(const S: WideString): LongWord;
{ }
{ MD5 hash }
{ }
{ MD5 is an Internet standard secure hashing function, that was }
{ developed by Professor Ronald L. Rivest in 1991. Subsequently it has }
{ been placed in the public domain. }
{ MD5 was developed to be more secure after MD4 was 'broken'. }
{ Den Boer and Bosselaers estimate that if a custom machine were to be }
{ built specifically to find collisions for MD5 (costing $10m in 1994) it }
{ would on average take 24 days to find a collision. }
{ }
procedure MD5InitDigest(var Digest: T128BitDigest);
procedure MD5Buf(var Digest: T128BitDigest; const Buf; const BufSize: Integer);
procedure MD5FinalBuf(var Digest: T128BitDigest; const Buf; const BufSize: Integer;
const TotalSize: Int64);
function CalcMD5(const Buf; const BufSize: Integer): T128BitDigest; overload;
function CalcMD5(const Buf: AnsiString): T128BitDigest; overload;
function MD5DigestToStrA(const Digest: T128BitDigest): AnsiString;
function MD5DigestToHexA(const Digest: T128BitDigest): AnsiString;
function MD5DigestToHexW(const Digest: T128BitDigest): WideString;
{ }
{ SHA1 Hashing }
{ }
{ Specification at http://www.itl.nist.gov/fipspubs/fip180-1.htm }
{ Also see RFC 3174. }
{ SHA1 was developed by NIST and is specified in the Secure Hash Standard }
{ (SHS, FIPS 180) and corrects an unpublished flaw the original SHA }
{ algorithm. }
{ SHA1 produces a 160-bit digest and is considered more secure than MD5. }
{ SHA1 has a similar design to the MD4-family of hash functions. }
{ }
procedure SHA1InitDigest(var Digest: T160BitDigest);
procedure SHA1Buf(var Digest: T160BitDigest; const Buf; const BufSize: Integer);
procedure SHA1FinalBuf(var Digest: T160BitDigest; const Buf; const BufSize: Integer;
const TotalSize: Int64);
function CalcSHA1(const Buf; const BufSize: Integer): T160BitDigest; overload;
function CalcSHA1(const Buf: AnsiString): T160BitDigest; overload;
function SHA1DigestToStrA(const Digest: T160BitDigest): AnsiString;
function SHA1DigestToHexA(const Digest: T160BitDigest): AnsiString;
function SHA1DigestToHexW(const Digest: T160BitDigest): WideString;
{ }
{ SHA224 Hashing }
{ }
{ 224 bit SHA-2 hash }
{ http://en.wikipedia.org/wiki/SHA-2 }
{ SHA-224 is based on SHA-256 }
{ }
procedure SHA224InitDigest(var Digest: T256BitDigest);
procedure SHA224Buf(var Digest: T256BitDigest; const Buf; const BufSize: Integer);
procedure SHA224FinalBuf(var Digest: T256BitDigest; const Buf; const BufSize: Integer; const TotalSize: Int64;
var OutDigest: T224BitDigest);
function CalcSHA224(const Buf; const BufSize: Integer): T224BitDigest; overload;
function CalcSHA224(const Buf: AnsiString): T224BitDigest; overload;
function SHA224DigestToStrA(const Digest: T224BitDigest): AnsiString;
function SHA224DigestToHexA(const Digest: T224BitDigest): AnsiString;
function SHA224DigestToHexW(const Digest: T224BitDigest): WideString;
{ }
{ SHA256 Hashing }
{ 256 bit SHA-2 hash }
{ }
procedure SHA256InitDigest(var Digest: T256BitDigest);
procedure SHA256Buf(var Digest: T256BitDigest; const Buf; const BufSize: Integer);
procedure SHA256FinalBuf(var Digest: T256BitDigest; const Buf; const BufSize: Integer; const TotalSize: Int64);
function CalcSHA256(const Buf; const BufSize: Integer): T256BitDigest; overload;
function CalcSHA256(const Buf: AnsiString): T256BitDigest; overload;
function SHA256DigestToStrA(const Digest: T256BitDigest): AnsiString;
function SHA256DigestToHexA(const Digest: T256BitDigest): AnsiString;
function SHA256DigestToHexW(const Digest: T256BitDigest): WideString;
{ }
{ SHA384 Hashing }
{ 384 bit SHA-2 hash }
{ SHA-384 is based on SHA-512 }
{ }
procedure SHA384InitDigest(var Digest: T512BitDigest);
procedure SHA384Buf(var Digest: T512BitDigest; const Buf; const BufSize: Integer);
procedure SHA384FinalBuf(var Digest: T512BitDigest; const Buf; const BufSize: Integer; const TotalSize: Int64; var OutDigest: T384BitDigest);
function CalcSHA384(const Buf; const BufSize: Integer): T384BitDigest; overload;
function CalcSHA384(const Buf: AnsiString): T384BitDigest; overload;
function SHA384DigestToStrA(const Digest: T384BitDigest): AnsiString;
function SHA384DigestToHexA(const Digest: T384BitDigest): AnsiString;
function SHA384DigestToHexW(const Digest: T384BitDigest): WideString;
{ }
{ SHA512 Hashing }
{ 512 bit SHA-2 hash }
{ }
procedure SHA512InitDigest(var Digest: T512BitDigest);
procedure SHA512Buf(var Digest: T512BitDigest; const Buf; const BufSize: Integer);
procedure SHA512FinalBuf(var Digest: T512BitDigest; const Buf; const BufSize: Integer; const TotalSize: Int64);
function CalcSHA512(const Buf; const BufSize: Integer): T512BitDigest; overload;
function CalcSHA512(const Buf: AnsiString): T512BitDigest; overload;
function SHA512DigestToStrA(const Digest: T512BitDigest): AnsiString;
function SHA512DigestToHexA(const Digest: T512BitDigest): AnsiString;
function SHA512DigestToHexW(const Digest: T512BitDigest): WideString;
{ }
{ HMAC-MD5 keyed hashing }
{ }
{ HMAC allows secure keyed hashing (hashing with a password). }
{ HMAC was designed to meet the requirements of the IPSEC working group in }
{ the IETF, and is now a standard. }
{ HMAC, are proven to be secure as long as the underlying hash function }
{ has some reasonable cryptographic strengths. }
{ See RFC 2104 for details on HMAC. }
{ }
procedure HMAC_MD5Init(const Key: Pointer; const KeySize: Integer;
var Digest: T128BitDigest; var K: T512BitBuf);
procedure HMAC_MD5Buf(var Digest: T128BitDigest; const Buf; const BufSize: Integer);
procedure HMAC_MD5FinalBuf(const K: T512BitBuf; var Digest: T128BitDigest;
const Buf; const BufSize: Integer; const TotalSize: Int64);
function CalcHMAC_MD5(const Key: Pointer; const KeySize: Integer;
const Buf; const BufSize: Integer): T128BitDigest; overload;
function CalcHMAC_MD5(const Key: AnsiString; const Buf; const BufSize: Integer): T128BitDigest; overload;
function CalcHMAC_MD5(const Key, Buf: AnsiString): T128BitDigest; overload;
{ }
{ HMAC-SHA1 keyed hashing }
{ }
procedure HMAC_SHA1Init(const Key: Pointer; const KeySize: Integer;
var Digest: T160BitDigest; var K: T512BitBuf);
procedure HMAC_SHA1Buf(var Digest: T160BitDigest; const Buf; const BufSize: Integer);
procedure HMAC_SHA1FinalBuf(const K: T512BitBuf; var Digest: T160BitDigest;
const Buf; const BufSize: Integer; const TotalSize: Int64);
function CalcHMAC_SHA1(const Key: Pointer; const KeySize: Integer;
const Buf; const BufSize: Integer): T160BitDigest; overload;
function CalcHMAC_SHA1(const Key: AnsiString; const Buf; const BufSize: Integer): T160BitDigest; overload;
function CalcHMAC_SHA1(const Key, Buf: AnsiString): T160BitDigest; overload;
{ }
{ HMAC-SHA256 keyed hashing }
{ }
procedure HMAC_SHA256Init(const Key: Pointer; const KeySize: Integer;
var Digest: T256BitDigest; var K: T512BitBuf);
procedure HMAC_SHA256Buf(var Digest: T256BitDigest; const Buf; const BufSize: Integer);
procedure HMAC_SHA256FinalBuf(const K: T512BitBuf; var Digest: T256BitDigest;
const Buf; const BufSize: Integer; const TotalSize: Int64);
function CalcHMAC_SHA256(const Key: Pointer; const KeySize: Integer;
const Buf; const BufSize: Integer): T256BitDigest; overload;
function CalcHMAC_SHA256(const Key: AnsiString; const Buf; const BufSize: Integer): T256BitDigest; overload;
function CalcHMAC_SHA256(const Key, Buf: AnsiString): T256BitDigest; overload;
{ }
{ HMAC-SHA512 keyed hashing }
{ }
procedure HMAC_SHA512Init(const Key: Pointer; const KeySize: Integer; var Digest: T512BitDigest; var K: T1024BitBuf);
procedure HMAC_SHA512Buf(var Digest: T512BitDigest; const Buf; const BufSize: Integer);
procedure HMAC_SHA512FinalBuf(const K: T1024BitBuf; var Digest: T512BitDigest; const Buf; const BufSize: Integer; const TotalSize: Int64);
function CalcHMAC_SHA512(const Key: Pointer; const KeySize: Integer;
const Buf; const BufSize: Integer): T512BitDigest; overload;
function CalcHMAC_SHA512(const Key: AnsiString; const Buf; const BufSize: Integer): T512BitDigest; overload;
function CalcHMAC_SHA512(const Key, Buf: AnsiString): T512BitDigest; overload;
{ }
{ Hash class wrappers }
{ }
type
{ AHash }
{ Base class for hash classes. }
AHash = class
protected
FDigest : Pointer;
FTotalSize: Int64;
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); virtual; abstract;
procedure ProcessBuf(const Buf; const BufSize: Integer); virtual; abstract;
procedure ProcessFinalBuf(const Buf; const BufSize: Integer; const TotalSize: Int64); virtual;
public
class function DigestSize: Integer; virtual; abstract;
class function BlockSize: Integer; virtual;
procedure Init(const Digest: Pointer; const Key: Pointer = nil;
const KeySize: Integer = 0); overload;
procedure Init(const Digest: Pointer; const Key: AnsiString = ''); overload;
procedure HashBuf(const Buf; const BufSize: Integer; const FinalBuf: Boolean);
procedure HashFile(const FileName: string; const Offset: Int64 = 0;
const MaxCount: Int64 = - 1);
end;
THashClass = class of AHash;
{ TChecksum32Hash }
TChecksum32Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
public
class function DigestSize: Integer; override;
end;
{ TXOR8Hash }
TXOR8Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
public
class function DigestSize: Integer; override;
end;
{ TXOR16Hash }
TXOR16Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
public
class function DigestSize: Integer; override;
end;
{ TXOR32Hash }
TXOR32Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
public
class function DigestSize: Integer; override;
end;
{ TCRC16Hash }
TCRC16Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
public
class function DigestSize: Integer; override;
end;
{ TCRC32Hash }
TCRC32Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
public
class function DigestSize: Integer; override;
end;
{ TAdler32Hash }
TAdler32Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
public
class function DigestSize: Integer; override;
end;
{ TELFHash }
TELFHash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
public
class function DigestSize: Integer; override;
end;
{ TMD5Hash }
TMD5Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
procedure ProcessFinalBuf(const Buf; const BufSize: Integer; const TotalSize: Int64); override;
public
class function DigestSize: Integer; override;
class function BlockSize: Integer; override;
end;
{ TSHA1Hash }
TSHA1Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
procedure ProcessFinalBuf(const Buf; const BufSize: Integer; const TotalSize: Int64); override;
public
class function DigestSize: Integer; override;
class function BlockSize: Integer; override;
end;
{ TSHA256Hash }
TSHA256Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
procedure ProcessFinalBuf(const Buf; const BufSize: Integer; const TotalSize: Int64); override;
public
class function DigestSize: Integer; override;
class function BlockSize: Integer; override;
end;
{ TSHA512Hash }
TSHA512Hash = class(AHash)
protected
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
procedure ProcessFinalBuf(const Buf; const BufSize: Integer; const TotalSize: Int64); override;
public
class function DigestSize: Integer; override;
class function BlockSize: Integer; override;
end;
{ THMAC_MD5Hash }
THMAC_MD5Hash = class(AHash)
protected
FKey: T512BitBuf;
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
procedure ProcessFinalBuf(const Buf; const BufSize: Integer; const TotalSize: Int64); override;
public
class function DigestSize: Integer; override;
class function BlockSize: Integer; override;
destructor Destroy; override;
end;
{ THMAC_SHA1Hash }
THMAC_SHA1Hash = class(AHash)
protected
FKey: T512BitBuf;
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
procedure ProcessFinalBuf(const Buf; const BufSize: Integer; const TotalSize: Int64); override;
public
class function DigestSize: Integer; override;
class function BlockSize: Integer; override;
destructor Destroy; override;
end;
{ THMAC_SHA256Hash }
THMAC_SHA256Hash = class(AHash)
protected
FKey: T512BitBuf;
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
procedure ProcessFinalBuf(const Buf; const BufSize: Integer; const TotalSize: Int64); override;
public
class function DigestSize: Integer; override;
class function BlockSize: Integer; override;
destructor Destroy; override;
end;
{ THMAC_SHA512Hash }
THMAC_SHA512Hash = class(AHash)
protected
FKey: T1024BitBuf;
procedure InitHash(const Digest: Pointer; const Key: Pointer; const KeySize: Integer); override;
procedure ProcessBuf(const Buf; const BufSize: Integer); override;
procedure ProcessFinalBuf(const Buf; const BufSize: Integer; const TotalSize: Int64); override;
public
class function DigestSize: Integer; override;
class function BlockSize: Integer; override;
destructor Destroy; override;
end;
{ }
{ THashType }
{ }
type
THashType = (
hashChecksum32, hashXOR8, hashXOR16, hashXOR32,
hashCRC16, hashCRC32,
hashAdler32,
hashELF,
hashMD5, hashSHA1, hashSHA256, hashSHA512,
hashHMAC_MD5, hashHMAC_SHA1, hashHMAC_SHA256, hashHMAC_SHA512);
{ }
{ GetHashClassByType }
{ }
function GetHashClassByType(const HashType: THashType): THashClass;
function GetDigestSize(const HashType: THashType): Integer;
{ }
{ CalculateHash }
{ }
procedure CalculateHash(const HashType: THashType;
const Buf; const BufSize: Integer; const Digest: Pointer;
const Key: Pointer = nil; const KeySize: Integer = 0); overload;
procedure CalculateHash(const HashType: THashType;
const Buf; const BufSize: Integer;
const Digest: Pointer; const Key: AnsiString = ''); overload;
procedure CalculateHash(const HashType: THashType;
const Buf: AnsiString; const Digest: Pointer;
const Key: AnsiString = ''); overload;
{ }
{ HashString }
{ }
{ HashString is a fast general purpose ASCII string hashing function. }
{ It returns a 32 bit value in the range 0 to Slots - 1. If Slots = 0 then }
{ the full 32 bit value is returned. }
{ If CaseSensitive = False then HashString will return the same hash value }
{ regardless of the case of the characters in the string. }
{ }
{ The implementation is based on CRC32. It uses up to 48 characters from }
{ the string (first 16 characters, last 16 characters and 16 characters }
{ uniformly sampled from the remaining characters) to calculate the hash }
{ value. }
{ }
function HashString(const StrBuf: Pointer; const StrLength: Integer;
const Slots: LongWord = 0; const CaseSensitive: Boolean = True): LongWord; overload;
function HashString(const S: AnsiString; const Slots: LongWord = 0;
const CaseSensitive: Boolean = True): LongWord; overload;
implementation
{ }
{ Hash errors }
{ }
const
hashErrorMessages: array [0 .. hashMAX_ERROR] of string = (
'',
'Internal error',
'Invalid hash type',
'Invalid buffer',
'Invalid buffer size',
'Invalid digest',
'Invalid key',
'Invalid file name',
'File open error',
'File seek error',
'File read error',
'Not a keyed hash type',
'Too many open handles',
'Invalid handle');
function GetHashErrorMessage(const ErrorCode: LongWord): PChar;
begin
if (ErrorCode = hashNoError) or (ErrorCode > hashMAX_ERROR) then
Result := nil
else
Result := PChar(hashErrorMessages[ErrorCode]);
end;
{ }
{ EHashError }
{ }
constructor EHashError.Create(const ErrorCode: LongWord; const Msg: string);
begin
FErrorCode := ErrorCode;
if (Msg = '') and (ErrorCode <= hashMAX_ERROR) then
inherited Create(hashErrorMessages[ErrorCode])
else
inherited Create(Msg);
end;
{ }
{ Secure memory clear }
{ }
procedure SecureClear(var Buf; const BufSize: Integer);
begin
if BufSize <= 0 then
exit;
FillChar(Buf, BufSize, #$00);
end;
procedure SecureClear512(var Buf: T512BitBuf);
begin
SecureClear(Buf, SizeOf(Buf));
end;
procedure SecureClear1024(var Buf: T1024BitBuf);
begin
SecureClear(Buf, SizeOf(Buf));
end;
procedure SecureClearStrA(var S: AnsiString);
var
L: Integer;
begin
L := Length(S);
if L = 0 then
exit;
SecureClear(S[1], L);
S := '';
end;
procedure SecureClearStrW(var S: WideString);
var
L: Integer;
begin
L := Length(S);
if L = 0 then
exit;
SecureClear(S[1], L * SizeOf(WideChar));
S := '';
end;
{ }
{ Checksum hashing }
{ }
{$IFDEF ASM386_DELPHI}
function CalcChecksum32(const Buf; const BufSize: Integer): LongWord;
asm
or eax, eax //eax = Buf
jz @fin
or edx, edx //edx = BufSize
jbe @finz
push esi
mov esi, eax
add esi, edx
xor eax, eax
xor ecx, ecx
@l1:
dec esi
mov cl, [esi]
add eax, ecx
dec edx
jnz @l1
pop esi
@fin:
ret
@finz:
xor eax, eax
end;
{$ELSE}
function CalcChecksum32(const Buf; const BufSize: Integer): LongWord;