-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SHA3.pas
3703 lines (3094 loc) · 133 KB
/
SHA3.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
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
SHA-3/Keccak calculation
This unit provides a mean of calculating SHA-3 hash of pretty much any data.
The only limiting factor is, that the data must consist of whole bytes, ie.
arbitrary bitstrings are not supported. SHA-3 is based on Keccak, so it is
provided in limited form too.
As SHA-3 is derived from Keccak-f[1600] (permutation width of 1600 bits,
64bit lanes/words), all hashes provided here are also based on this
permutation. To be more specific, everything is based on Keccak-p[1600,24],
a generalization of Keccak-f[1600] with 24 rounds per permutation (number
of rounds Nr = 12 + 2L, where L = log2(w), w is width of a keccak word, or,
in keccak terminology, a lane size).
Let's have following sponge function:
SPONGE[f,pad,r]
f - underlying sponge function (permutation function)
pad - padding rule or algorithm for input (padding to block boundary)
r - (bit)rate of the sponge function (defines size of input block)
Then we can define Keccak[c] as this:
Keccak[c] = SPONGE[Keccak-p[1600.24],pad10*1,1600-c]
c - capacity of the sponge function
pad10*1 (multi-rate padding) pretty much adds one 1 bit at the end of the
message, followed by none or more zeroes. The block is then closed by 1 bit.
Using all this, any of the implemented function can be described by
following formula:
Function(M) = Keccak[c](M||pad,d)
M - input message (data) that will be hashed
pad - optional padding bits beyond original pad10*1 padding (added
before the original padding bits)
d - number of bits in the output digest (hash)
Following twelve functions, or hash variants, are implemented and therefore
supported by this library:
Keccak[] SHA3-224
Keccak224 SHA3-256
Keccak256 SHA3-384
Keccak384 SHA3-512
Keccak512 SHAKE128
Keccak[c] SHAKE256
Each variant is implemeted in its own class. They all share the same
interface inherited from TBlockHash, but each provides some specialized
methods and properties.
Inheritance tree of all present class looks like this (star marks an
abstract class, these must not be directly instantiated):
*TKeccakHash --- TKeccak0Hash
|
|- *TKeccakDefHash --- *TKeccakFixHash --- TKeccak224Hash
| |- TKeccak256Hash
| |- TKeccak384Hash
| |- TKeccak512Hash
| |
| |- *TSHA3Hash ---- TSHA3_224Hash
| |- TSHA3_256Hash
| |- TSHA3_384Hash
| |- TSHA3_512Hash
|
|- *TKeccakVarHash --- TKeccakCHash
|
|- *TSHAKEHash --- TSHAKE128Hash
|- TSHAKE256Hash
Keccak[], in this library denoted as Keccak0, is a special case as it does
not produce any hash. Instead, it has public methods Permute and Squeeze
which can be used to produce output. It is intended to be used for other
purposes than hashing, for example as a base for a pseudo-random number
generator. It is defined as (note 576 is a default capacity for Keccak):
Keccak[](M) = Keccak[576](M,0)
Keccak224 trough Keccak512 are defined as:
Keccak224(M) = Keccak[448](M,224)
Keccak256(M) = Keccak[512](M,256)
Keccak384(M) = Keccak[768](M,384)
Keccak512(M) = Keccak[1024](M,512)
Keccak[c] is, again, a special case. Both capacity and hash length can be
selected independetly. Capacity can be anything from 8 to 1592. Hash bits
must be larger than zero, other than that they are limited only by available
memory. Thanks to this, Keccak[c] can be treated as an XOF - an
eXtendable-Output Function.
Defining this function from previous formula has no meaning, as it is fully
parametrized. It can be viewed as a direct implementation of this
generalization.
SHA-3 family of functions differ only slightly from the original Keccak.
They append few more bits to the message and sets capacity to double of the
hash length in bits. The functions are defined:
SHA3-224(M) = Keccak[448](M||01,224)
SHA3-256(M) = Keccak[512](M||01,256)
SHA3-384(M) = Keccak[768](M||01,384)
SHA3-512(M) = Keccak[1024](M||01,512)
SHAKE functions are extendable-output functions, meaning they produce
variable length hash, of which length can be selected before hashing.
They can be defined:
SHAKE128(M,d) = KECCAK[256](M||1111,d)
SHAKE256(M,d) = KECCAK[512](M||1111,d)
Version 1.2.1 (2020-07-13)
Last change 2024-03-05
©2015-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.SHA3
Dependencies:
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
BitOps - github.com/TheLazyTomcat/Lib.BitOps
HashBase - github.com/TheLazyTomcat/Lib.HashBase
Indirect dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
BasicUIM - github.com/TheLazyTomcat/Lib.BasicUIM
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StaticMemoryStream - github.com/TheLazyTomcat/Lib.StaticMemoryStream
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit SHA3;
{$IF defined(CPU64) or defined(CPU64BITS)}
{$DEFINE CPU64bit}
{$ELSEIF defined(CPU16)}
{$MESSAGE FATAL '16bit CPU not supported'}
{$ELSE}
{$DEFINE CPU32bit}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH DuplicateLocals+}
{$INLINE ON}
{$DEFINE CanInline}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ELSE}
{$IF CompilerVersion >= 17} // Delphi 2005+
{$DEFINE CanInline}
{$ELSE}
{$UNDEF CanInline}
{$IFEND}
{$ENDIF}
{$H+}
interface
uses
Classes,
AuxTypes, HashBase;
{===============================================================================
Common types and constants
===============================================================================}
{
Bytes in all Keccak, SHA-3 and SHAKE hashes are always ordered from the most
significant byte to the least significant byte (big endian).
Keccak/SHA-3/SHAKE does not differ in little and big endian forms, as it is
not a single quantity, therefore methods like SHA3_*ToLE or SHA3_*ToBE do
nothing and are present only for the sake of completeness.
}
type
// fixed length hashes
TKeccak224 = packed array[0..27] of UInt8; PKeccak224 = ^TKeccak224;
TKeccak256 = packed array[0..31] of UInt8; PKeccak256 = ^TKeccak256;
TKeccak384 = packed array[0..47] of UInt8; PKeccak384 = ^TKeccak384;
TKeccak512 = packed array[0..63] of UInt8; PKeccak512 = ^TKeccak512;
TSHA3_224 = type TKeccak224; PSHA3_224 = ^TSHA3_224;
TSHA3_256 = type TKeccak256; PSHA3_256 = ^TSHA3_256;
TSHA3_384 = type TKeccak384; PSHA3_384 = ^TSHA3_384;
TSHA3_512 = type TKeccak512; PSHA3_512 = ^TSHA3_512;
// variable length hashes
TKeccakVar = packed array of UInt8; // also used internally as a buffer
TKeccakC = type TKeccakVar; PKeccakC = ^TKeccakC;
TSHAKE128 = type TKeccakVar; PSHAKE128 = ^TSHAKE128;
TSHAKE256 = type TKeccakVar; PSHAKE256 = ^TSHAKE256;
TKeccakFunction = (fnKeccak0,fnKeccak224,fnKeccak256,fnKeccak384,fnKeccak512,fnKeccakC,
fnSHA3_224,fnSHA3_256,fnSHA3_384,fnSHA3_512,fnSHAKE128,fnSHAKE256);
TKeccak = record
HashFunction: TKeccakFunction;
HashBits: UInt32;
HashData: TKeccakVar;
end;
// some aliases
TSHA3Function = TKeccakFunction;
TSHA3 = TKeccak;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const
ZeroKeccak224: TKeccak224 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
ZeroKeccak256: TKeccak256 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
ZeroKeccak384: TKeccak384 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
ZeroKeccak512: TKeccak512 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
ZeroSHA3_224: TSHA3_224 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
ZeroSHA3_256: TSHA3_256 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
ZeroSHA3_384: TSHA3_384 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
ZeroSHA3_512: TSHA3_512 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
type
TKeccakWord = UInt64;
TKeccakSponge = packed array[0..4,0..4] of TKeccakWord;
TKeccakSpongeWordOverlay = packed array[0..24] of TKeccakWord;
TKeccakSpongeByteOverlay = packed array[0..Pred(25 * SizeOf(TKeccakWord))] of UInt8;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
type
ESHA3Exception = class(EHashException);
ESHA3IncompatibleClass = class(ESHA3Exception);
ESHA3IncompatibleFunction = class(ESHA3Exception);
ESHA3IncompatibleHashBits = class(ESHA3Exception);
ESHA3IncompatibleSize = class(ESHA3Exception);
ESHA3ProcessingError = class(ESHA3Exception);
ESHA3InvalidHashFunction = class(ESHA3Exception);
ESHA3InvalidHashBits = class(ESHA3Exception);
ESHA3InvalidSize = class(ESHA3Exception);
ESHA3InvalidCapacity = class(ESHA3Exception);
{-------------------------------------------------------------------------------
================================================================================
TKeccakHash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccakHash - class declaration
===============================================================================}
type
TKeccakHash = class(TBlockHash)
protected
fSponge: TKeccakSponge;
fHashBits: UInt32;
fCapacity: UInt32;
// getters, setters
Function GetBitrate: UInt32; virtual;
Function GetHashBuffer: TKeccakVar; virtual; // override in descendants
procedure SetHashBuffer(HashBuffer: TKeccakVar); virtual; // -//-
// endianness conversion
class Function HashBufferToLE(HashBuffer: TKeccakVar): TKeccakVar; virtual;
class Function HashBufferToBE(HashBuffer: TKeccakVar): TKeccakVar; virtual;
class Function HashBufferFromLE(HashBuffer: TKeccakVar): TKeccakVar; virtual;
class Function HashBufferFromBE(HashBuffer: TKeccakVar): TKeccakVar; virtual;
// hash function specific stuff
class Function CapacityFromHashBits(HashBits: UInt32): UInt32; virtual;
class Function PaddingByte: UInt8; virtual;
// processing
procedure Permute; virtual;
procedure Squeeze; overload; virtual;
procedure SqueezeTo(var Buffer; Size: TMemSize); virtual;
procedure ProcessFirst(const Block); override;
procedure ProcessBlock(const Block); override;
procedure ProcessLast; override;
// initialization
procedure InitHashBits(Value: UInt32); virtual; // must be called before Initialize indescendants
procedure Initialize; override;
public
class Function LaneSize: UInt32; virtual; // in bits, also width of the keccak word
class Function PermutationWidth: UInt32; virtual; // in bits
Function HashSize: TMemSize; reintroduce; // hides class functions
class Function HashEndianness: THashEndianness; override;
class Function HashFinalization: Boolean; override;
class Function HashFunction: TKeccakFunction; virtual; abstract;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak); overload; virtual; abstract;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TKeccak); reintroduce; overload; virtual;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property Sponge: TKeccakSponge read fSponge write fSponge;
property HashBits: UInt32 read fHashBits;
property Capacity: UInt32 read fCapacity;
property Bitrate: UInt32 read GetBitrate;
end;
TKeccakHashClass = class of TKeccakHash;
{-------------------------------------------------------------------------------
================================================================================
TKeccak0Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccak0Hash - class declaration
===============================================================================}
type
TKeccak0Hash = class(TKeccakHash)
protected
procedure Initialize; override;
public
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: TKeccak); overload; override;
procedure Permute; override;
procedure Squeeze(var Buffer; Size: TMemSize); overload; virtual;
end;
{-------------------------------------------------------------------------------
================================================================================
TKeccakDefHash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccakDefHash - class declaration
===============================================================================}
type
TKeccakDefHash = class(TKeccakHash)
protected
Function GetKeccak: TKeccak; virtual;
public
property Keccak: TKeccak read GetKeccak;
end;
{-------------------------------------------------------------------------------
================================================================================
TKeccakFixHash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccakFixHash - class declaration
===============================================================================}
type
TKeccakFixHash = class(TKeccakDefHash)
protected
class Function CapacityFromHashBits(HashBits: UInt32): UInt32; override;
public
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TKeccak); overload; override;
end;
{-------------------------------------------------------------------------------
================================================================================
TKeccak224Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccak224Hash - class declaration
===============================================================================}
type
TKeccak224Hash = class(TKeccakFixHash)
protected
fKeccak224: TKeccak224;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
procedure Initialize; override;
public
class Function Keccak224ToLE(Keccak224: TKeccak224): TKeccak224; virtual;
class Function Keccak224ToBE(Keccak224: TKeccak224): TKeccak224; virtual;
class Function Keccak224FromLE(Keccak224: TKeccak224): TKeccak224; virtual;
class Function Keccak224FromBE(Keccak224: TKeccak224): TKeccak224; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak224); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TKeccak224); overload; virtual;
property Keccak224: TKeccak224 read fKeccak224;
end;
{-------------------------------------------------------------------------------
================================================================================
TKeccak256Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccak256Hash - class declaration
===============================================================================}
type
TKeccak256Hash = class(TKeccakFixHash)
protected
fKeccak256: TKeccak256;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
procedure Initialize; override;
public
class Function Keccak256ToLE(Keccak256: TKeccak256): TKeccak256; virtual;
class Function Keccak256ToBE(Keccak256: TKeccak256): TKeccak256; virtual;
class Function Keccak256FromLE(Keccak256: TKeccak256): TKeccak256; virtual;
class Function Keccak256FromBE(Keccak256: TKeccak256): TKeccak256; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak256); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TKeccak256); overload; virtual;
property Keccak256: TKeccak256 read fKeccak256;
end;
{-------------------------------------------------------------------------------
================================================================================
TKeccak384Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccak384Hash - class declaration
===============================================================================}
type
TKeccak384Hash = class(TKeccakFixHash)
protected
fKeccak384: TKeccak384;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
procedure Initialize; override;
public
class Function Keccak384ToLE(Keccak384: TKeccak384): TKeccak384; virtual;
class Function Keccak384ToBE(Keccak384: TKeccak384): TKeccak384; virtual;
class Function Keccak384FromLE(Keccak384: TKeccak384): TKeccak384; virtual;
class Function Keccak384FromBE(Keccak384: TKeccak384): TKeccak384; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak384); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TKeccak384); overload; virtual;
property Keccak384: TKeccak384 read fKeccak384;
end;
{-------------------------------------------------------------------------------
================================================================================
TKeccak512Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccak512Hash - class declaration
===============================================================================}
type
TKeccak512Hash = class(TKeccakFixHash)
protected
fKeccak512: TKeccak512;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
procedure Initialize; override;
public
class Function Keccak512ToLE(Keccak512: TKeccak512): TKeccak512; virtual;
class Function Keccak512ToBE(Keccak512: TKeccak512): TKeccak512; virtual;
class Function Keccak512FromLE(Keccak512: TKeccak512): TKeccak512; virtual;
class Function Keccak512FromBE(Keccak512: TKeccak512): TKeccak512; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak512); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TKeccak512); overload; virtual;
property Keccak512: TKeccak512 read fKeccak512;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA3Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA3Hash - class declaration
===============================================================================}
type
TSHA3Hash = class(TKeccakFixHash)
protected
class Function CapacityFromHashBits(HashBits: UInt32): UInt32; override;
class Function PaddingByte: UInt8; override;
public
property SHA3: TSHA3 read GetKeccak;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA3_224Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA3_224Hash - class declaration
===============================================================================}
type
TSHA3_224Hash = class(TSHA3Hash)
protected
fSHA3_224: TSHA3_224;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
procedure Initialize; override;
public
class Function SHA3_224ToLE(SHA3_224: TSHA3_224): TSHA3_224; virtual;
class Function SHA3_224ToBE(SHA3_224: TSHA3_224): TSHA3_224; virtual;
class Function SHA3_224FromLE(SHA3_224: TSHA3_224): TSHA3_224; virtual;
class Function SHA3_224FromBE(SHA3_224: TSHA3_224): TSHA3_224; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSHA3); overload; override;
constructor CreateAndInitFrom(Hash: TSHA3_224); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHA3_224); overload; virtual;
property SHA3_224: TSHA3_224 read fSHA3_224;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA3_256Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA3_256Hash - class declaration
===============================================================================}
type
TSHA3_256Hash = class(TSHA3Hash)
protected
fSHA3_256: TSHA3_256;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
procedure Initialize; override;
public
class Function SHA3_256ToLE(SHA3_256: TSHA3_256): TSHA3_256; virtual;
class Function SHA3_256ToBE(SHA3_256: TSHA3_256): TSHA3_256; virtual;
class Function SHA3_256FromLE(SHA3_256: TSHA3_256): TSHA3_256; virtual;
class Function SHA3_256FromBE(SHA3_256: TSHA3_256): TSHA3_256; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSHA3); overload; override;
constructor CreateAndInitFrom(Hash: TSHA3_256); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHA3_256); overload; virtual;
property SHA3_256: TSHA3_256 read fSHA3_256;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA3_384Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA3_384Hash - class declaration
===============================================================================}
type
TSHA3_384Hash = class(TSHA3Hash)
protected
fSHA3_384: TSHA3_384;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
procedure Initialize; override;
public
class Function SHA3_384ToLE(SHA3_384: TSHA3_384): TSHA3_384; virtual;
class Function SHA3_384ToBE(SHA3_384: TSHA3_384): TSHA3_384; virtual;
class Function SHA3_384FromLE(SHA3_384: TSHA3_384): TSHA3_384; virtual;
class Function SHA3_384FromBE(SHA3_384: TSHA3_384): TSHA3_384; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSHA3); overload; override;
constructor CreateAndInitFrom(Hash: TSHA3_384); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHA3_384); overload; virtual;
property SHA3_384: TSHA3_384 read fSHA3_384;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHA3_512Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHA3_512Hash - class declaration
===============================================================================}
type
TSHA3_512Hash = class(TSHA3Hash)
protected
fSHA3_512: TSHA3_512;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
procedure Initialize; override;
public
class Function SHA3_512ToLE(SHA3_512: TSHA3_512): TSHA3_512; virtual;
class Function SHA3_512ToBE(SHA3_512: TSHA3_512): TSHA3_512; virtual;
class Function SHA3_512FromLE(SHA3_512: TSHA3_512): TSHA3_512; virtual;
class Function SHA3_512FromBE(SHA3_512: TSHA3_512): TSHA3_512; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSHA3); overload; override;
constructor CreateAndInitFrom(Hash: TSHA3_512); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHA3_512); overload; virtual;
property SHA3_512: TSHA3_512 read fSHA3_512;
end;
{-------------------------------------------------------------------------------
================================================================================
TKeccakVarHash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccakVarHash - class declaration
===============================================================================}
type
TKeccakVarHash = class(TKeccakDefHash)
protected
procedure SetHashBits(Value: UInt32); virtual; abstract;
class Function CapacityFromHashBits(HashBits: UInt32): UInt32; override;
public
procedure FromStringDef(const Str: String; const Default: TKeccak); overload; override;
property HashBits: UInt32 read fHashBits write SetHashBits;
end;
{-------------------------------------------------------------------------------
================================================================================
TKeccakCHash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccakCHash - class declaration
===============================================================================}
type
TKeccakCHash = class(TKeccakVarHash)
protected
fKeccakC: TKeccakC;
procedure SetHashBits(Value: UInt32); override;
procedure SetCapacity(Value: UInt32); virtual;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
Function GetKeccakC: TKeccakC; virtual;
procedure Initialize; override;
public
class Function KeccakCToLE(KeccakC: TKeccakC): TKeccakC; virtual;
class Function KeccakCToBE(KeccakC: TKeccakC): TKeccakC; virtual;
class Function KeccakCFromLE(KeccakC: TKeccakC): TKeccakC; virtual;
class Function KeccakCFromBE(KeccakC: TKeccakC): TKeccakC; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
class Function MaxCapacity: UInt32; virtual;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak); overload; override;
constructor CreateAndInitFrom(Hash: TKeccakC); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TKeccakC); overload; virtual;
property Capacity: UInt32 read fCapacity write SetCapacity;
property KeccakC: TKeccakC read GetKeccakC;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHAKEHash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHAKEHash - class declaration
===============================================================================}
type
TSHAKEHash = class(TKeccakVarHash)
protected
class Function PaddingByte: UInt8; override;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHAKE128Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHAKE128Hash - class declaration
===============================================================================}
type
TSHAKE128Hash = class(TSHAKEHash)
protected
fSHAKE128: TSHAKE128;
procedure SetHashBits(Value: UInt32); override;
class Function CapacityFromHashBits(HashBits: UInt32): UInt32; override;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
Function GetSHAKE128: TSHAKE128; virtual;
procedure Initialize; override;
public
class Function SHAKE128ToLE(SHAKE128: TSHAKE128): TSHAKE128; virtual;
class Function SHAKE128ToBE(SHAKE128: TSHAKE128): TSHAKE128; virtual;
class Function SHAKE128FromLE(SHAKE128: TSHAKE128): TSHAKE128; virtual;
class Function SHAKE128FromBE(SHAKE128: TSHAKE128): TSHAKE128; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak); overload; override;
constructor CreateAndInitFrom(Hash: TSHAKE128); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHAKE128); overload; virtual;
property HashBits: UInt32 read fHashBits write SetHashBits;
property SHAKE128: TSHAKE128 read GetSHAKE128;
end;
{-------------------------------------------------------------------------------
================================================================================
TSHAKE256Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TSHAKE256Hash - class declaration
===============================================================================}
type
TSHAKE256Hash = class(TSHAKEHash)
protected
fSHAKE256: TSHAKE256;
procedure SetHashBits(Value: UInt32); override;
class Function CapacityFromHashBits(HashBits: UInt32): UInt32; override;
Function GetHashBuffer: TKeccakVar; override;
procedure SetHashBuffer(HashBuffer: TKeccakVar); override;
Function GetSHAKE256: TSHAKE256; virtual;
procedure Initialize; override;
public
class Function SHAKE256ToLE(SHAKE256: TSHAKE256): TSHAKE256; virtual;
class Function SHAKE256ToBE(SHAKE256: TSHAKE256): TSHAKE256; virtual;
class Function SHAKE256FromLE(SHAKE256: TSHAKE256): TSHAKE256; virtual;
class Function SHAKE256FromBE(SHAKE256: TSHAKE256): TSHAKE256; virtual;
class Function HashName: String; override;
class Function HashFunction: TKeccakFunction; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TKeccak); overload; override;
constructor CreateAndInitFrom(Hash: TSHAKE256); overload; virtual;
procedure Init; override;
procedure FromStringDef(const Str: String; const Default: TSHAKE256); overload; virtual;
property HashBits: UInt32 read fHashBits write SetHashBits;
property SHAKE256: TSHAKE256 read GetSHAKE256;
end;
{===============================================================================
Auxiliary functions
===============================================================================}
Function ClassByFunction(HashFunction: TKeccakFunction): TKeccakHashClass;
Function CreateByFunction(HashFunction: TKeccakFunction): TKeccakHash;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function CreateFromByFunction(HashFunction: TKeccakFunction; Hash: TKeccakHash): TKeccakHash; overload;{$IFDEF CanInline} inline; {$ENDIF}
Function CreateFromByFunction(HashFunction: TKeccakFunction; Hash: TKeccak): TKeccakHash; overload;{$IF Defined(CanInline) and not Defined(FPC)} inline; {$IFEND}
Function CreateFromByFunction(Keccak: TKeccak): TKeccakHash; overload;{$IFDEF CanInline} inline; {$ENDIF}
{===============================================================================
Backward compatibility functions
===============================================================================}
{
For Keccak/SHA3/SHAKE, it is not enough to pass hash from previous step when
doing continuous hashing (BufferSHA3 > LastBufferSHA3). TKecakState type is
introduced for this purpose.
}
type
TKeccakState = record
HashFunction: TKeccakFunction;
HashBits: UInt32;
Sponge: TKeccakSponge;
end;
PKeccakState = ^TKeccakState;
TSHA3State = TKeccakState;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function GetBlockSize(HashFunction: TSHA3Function): UInt32;
Function InitialSHA3State(HashFunction: TSHA3Function; HashBits: UInt32 = 0): TSHA3State;
Function SHA3ToStr(SHA3: TSHA3): String;
Function StrToSHA3(HashFunction: TSHA3Function; Str: String): TSHA3;
Function TryStrToSHA3(HashFunction: TSHA3Function; const Str: String; out SHA3: TSHA3): Boolean;
Function StrToSHA3Def(HashFunction: TSHA3Function; const Str: String; Default: TSHA3): TSHA3;
Function CompareSHA3(A,B: TSHA3): Integer;
Function SameSHA3(A,B: TSHA3): Boolean;
Function BinaryCorrectSHA3(Hash: TSHA3): TSHA3;{$IFDEF CanInline} inline; {$ENDIF}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure BufferSHA3(var State: TSHA3State; const Buffer; Size: TMemSize); overload;
Function LastBufferSHA3(State: TSHA3State; const Buffer; Size: TMemSize): TSHA3;
Function BufferSHA3(HashFunction: TSHA3Function; const Buffer; Size: TMemSize; HashBits: UInt32 = 0): TSHA3; overload;
Function AnsiStringSHA3(HashFunction: TSHA3Function; const Str: AnsiString; HashBits: UInt32 = 0): TSHA3;
Function WideStringSHA3(HashFunction: TSHA3Function; const Str: WideString; HashBits: UInt32 = 0): TSHA3;
Function StringSHA3(HashFunction: TSHA3Function; const Str: String; HashBits: UInt32 = 0): TSHA3;
Function StreamSHA3(HashFunction: TSHA3Function; Stream: TStream; Count: Int64 = -1; HashBits: UInt32 = 0): TSHA3;
Function FileSHA3(HashFunction: TSHA3Function; const FileName: String; HashBits: UInt32 = 0): TSHA3;
//------------------------------------------------------------------------------
type
TSHA3Context = type Pointer;
Function SHA3_Init(HashFunction: TSHA3Function; HashBits: UInt32 = 0): TSHA3Context;
procedure SHA3_Update(Context: TSHA3Context; const Buffer; Size: TMemSize);
Function SHA3_Final(var Context: TSHA3Context; const Buffer; Size: TMemSize): TSHA3; overload;
Function SHA3_Final(var Context: TSHA3Context): TSHA3; overload;
Function SHA3_Hash(HashFunction: TSHA3Function; const Buffer; Size: TMemSize; HashBits: UInt32 = 0): TSHA3;
implementation
uses
SysUtils,
BitOps;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W4055:={$WARN 4055 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W4056:={$WARN 4056 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$DEFINE W6018:={$WARN 6018 OFF}} // unreachable code
{$ENDIF}
{===============================================================================
Auxiliary functions - implementation
===============================================================================}
{-------------------------------------------------------------------------------
Auxiliary functions - private functions
-------------------------------------------------------------------------------}
Function EndianSwap(Sponge: TKeccakSponge): TKeccakSponge; overload;
var
i: Integer;
begin
For i := Low(TKeccakSpongeWordOverlay) to High(TKeccakSpongeWordOverlay) do
TKeccakSpongeWordOverlay(Result)[i] := EndianSwap(TKeccakSpongeWordOverlay(Sponge)[i]);
end;
//------------------------------------------------------------------------------
{
BCF = Backward Compatibility Functions, yup ;-)
}
Function BCF_CreateByFunction(HashFunction: TKeccakFunction): TKeccakDefHash;
begin
If HashFunction <> fnKeccak0 then
Result := TKeccakDefHash(CreateByFunction(HashFunction))
else
raise ESHA3InvalidHashFunction.Create('BCF_CreateByFunction: Keccak0 not allowed here.');
end;
//------------------------------------------------------------------------------
Function BCF_CreateFromByFunction(HashFunction: TKeccakFunction; Hash: TKeccak): TKeccakDefHash; overload;{$IFDEF CanInline} inline; {$ENDIF}
begin
If HashFunction <> fnKeccak0 then
Result := TKeccakDefHash(CreateFromByFunction(HashFunction,Hash))
else
raise ESHA3InvalidHashFunction.Create('BCF_CreateFromByFunction: Keccak0 not allowed here.');
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function BCF_CreateFromByFunction(Keccak: TKeccak): TKeccakDefHash; overload;{$IFDEF CanInline} inline; {$ENDIF}
begin
Result := BCF_CreateFromByFunction(Keccak.HashFunction,Keccak);
end;
{-------------------------------------------------------------------------------
Auxiliary functions - public functions
-------------------------------------------------------------------------------}
Function ClassByFunction(HashFunction: TKeccakFunction): TKeccakHashClass;
begin
case HashFunction of
fnKeccak0: Result := TKeccak0Hash;
fnKeccak224: Result := TKeccak224Hash;
fnKeccak256: Result := TKeccak256Hash;
fnKeccak384: Result := TKeccak384Hash;
fnKeccak512: Result := TKeccak512Hash;
fnKeccakC: Result := TKeccakCHash;
fnSHA3_224: Result := TSHA3_224Hash;
fnSHA3_256: Result := TSHA3_256Hash;
fnSHA3_384: Result := TSHA3_384Hash;
fnSHA3_512: Result := TSHA3_512Hash;
fnSHAKE128: Result := TSHAKE128Hash;
fnSHAKE256: Result := TSHAKE256Hash;
else
raise ESHA3InvalidHashFunction.CreateFmt('ClassByFunction: Invalid hash function (%d)',[Ord(HashFunction)]);
end;
end;
//------------------------------------------------------------------------------
Function CreateByFunction(HashFunction: TKeccakFunction): TKeccakHash;
begin
Result := ClassByFunction(HashFunction).Create;
end;
//------------------------------------------------------------------------------
Function CreateFromByFunction(HashFunction: TKeccakFunction; Hash: TKeccakHash): TKeccakHash;
begin
Result := ClassByFunction(HashFunction).CreateAndInitFrom(Hash);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function CreateFromByFunction(HashFunction: TKeccakFunction; Hash: TKeccak): TKeccakHash;
begin
Result := ClassByFunction(HashFunction).CreateAndInitFrom(Hash);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function CreateFromByFunction(Keccak: TKeccak): TKeccakHash;
begin
Result := CreateFromByFunction(Keccak.HashFunction,Keccak);
end;
{-------------------------------------------------------------------------------
================================================================================
TKeccakHash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TKeccakHash - calculation constants
===============================================================================}
const
KECCAK_ROUND_CONSTS: array[0..23] of UInt64 = (
UInt64($0000000000000001), UInt64($0000000000008082), UInt64($800000000000808A),
UInt64($8000000080008000), UInt64($000000000000808B), UInt64($0000000080000001),
UInt64($8000000080008081), UInt64($8000000000008009), UInt64($000000000000008A),
UInt64($0000000000000088), UInt64($0000000080008009), UInt64($000000008000000A),
UInt64($000000008000808B), UInt64($800000000000008B), UInt64($8000000000008089),
UInt64($8000000000008003), UInt64($8000000000008002), UInt64($8000000000000080),
UInt64($000000000000800A), UInt64($800000008000000A), UInt64($8000000080008081),
UInt64($8000000000008080), UInt64($0000000080000001), UInt64($8000000080008008));
KECCAK_ROT_COEFS: array[0..4,0..4] of UInt8 = ( // first index is X, second Y
{X = 0} ( 0,36, 3,41,18),
{X = 1} ( 1,44,10,45, 2),
{X = 2} (62, 6,43,15,61),
{X = 3} (28,55,25,21,56),
{X = 4} (27,20,39, 8,14));
KECCAK_DEFAULT_CAPACITY = 9 * 8 * SizeOf(TKeccakWord); // 576 bits
{===============================================================================
TKeccakHash - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TKeccakHash - protected methods
-------------------------------------------------------------------------------}
Function TKeccakHash.GetBitrate: UInt32;
begin