-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MD6.pas
3317 lines (2854 loc) · 114 KB
/
MD6.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/.
-------------------------------------------------------------------------------}
{===============================================================================
MD6 calculation
Provides classes and functions for calculation of MD6 hash. All hash widths
that are a multiple of 8 are supported (meaning only whole bytes, arbitrary
bitstreams are not supported). Also, all mandatory and optional hash inputs
can be varied (key, mode control, number of rounds).
Default implementation provided here (class TMD6Hash and its descendants)
is only single-threaded, but as the MD6 can be heavily parallelized, an
attemp was made to provide MD6 hashing that can use multiple execution
paths.
This is done in the form of TMD6HashParallel class (note that this class
is NOT a descendant of THashBase, but rather a standalone class).
The used code is purely experimental and probably bugged, but it offers a
full freedom in hash input values (there is a limiting factor that
protects against excessive memory use for combination of long messages
and low mode control - property MaxSequentialNodes - but it can be
changed as needed).
The only strict limit is, that the entire message must be available at
the start of processing, so only hashing of memory buffer or file is
provided.
WARNING - The parallel code is not fully tested, use it at your own
risk. Also, performance gain and scaling is uncertain, as
I was not able to test the code on more than dual-core CPU.
Version 1.1 (2023-01-13)
Last change 2024-09-09
©2022-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.MD6
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
BitOps - github.com/TheLazyTomcat/Lib.BitOps
CrossSyncObjs - github.com/TheLazyTomcat/Lib.CrossSyncObjs
HashBase - github.com/TheLazyTomcat/Lib.HashBase
InterlockedOps - github.com/TheLazyTomcat/Lib.InterlockedOps
StaticMemoryStream - github.com/TheLazyTomcat/Lib.StaticMemoryStream
StrRect - github.com/TheLazyTomcat/Lib.StrRect
Indirect dependencies:
AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
BasicUIM - github.com/TheLazyTomcat/Lib.BasicUIM
BinaryStreamingLite - github.com/TheLazyTomcat/Lib.BinaryStreamingLite
BitVector - github.com/TheLazyTomcat/Lib.BitVector
LinSyncObjs - github.com/TheLazyTomcat/Lib.LinSyncObjs
NamedSharedItems - github.com/TheLazyTomcat/Lib.NamedSharedItems
SHA1 - github.com/TheLazyTomcat/Lib.SHA1
SharedMemoryStream - github.com/TheLazyTomcat/Lib.SharedMemoryStream
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
SimpleFutex - github.com/TheLazyTomcat/Lib.SimpleFutex
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
WinSyncObjs - github.com/TheLazyTomcat/Lib.WinSyncObjs
===============================================================================}
unit MD6;
{$IF defined(CPU64) or defined(CPU64BITS)}
{$DEFINE CPU64bit}
{$ELSEIF defined(CPU16)}
{$MESSAGE FATAL 'Unsupported CPU.'}
{$ELSE}
{$DEFINE CPU32bit}
{$IFEND}
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$ELSEIF Defined(LINUX) and Defined(FPC)}
{$DEFINE Linux}
{$ELSE}
{$MESSAGE FATAL 'Unsupported operating system.'}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH DuplicateLocals+}
{$MODESWITCH ClassicProcVars+}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
interface
uses
SysUtils, Classes,
AuxTypes, AuxClasses, HashBase, CrossSyncObjs;
{===============================================================================
Library-specific exceptions
===============================================================================}
type
EMD6Exception = class(EHASHException);
EMD6InvalidValue = class(EMD6Exception);
EMD6InvalidState = class(EMD6Exception);
EMD6InvalidHashLength = class(EMD6Exception);
EMD6IncompatibleClass = class(EMD6Exception);
EMD6SizeMismatch = class(EMD6Exception);
EMD6ProcessingError = class(EMD6Exception);
EMD6OperationNotAllowed = class(EMD6Exception);
EMD6ParallelInternal = class(EMD6Exception);
{===============================================================================
Common types and constants
===============================================================================}
{
Bytes in all MD6 hashes are always ordered from the most significant byte to
the least significant byte (big endian).
MD6 does not differ in little and big endian form, as it is not a single
quantity, therefore methods like MD6ToLE or MD6ToBE do nothing and are
present only for the sake of completeness.
}
type
TMD6 = packed array of UInt8;
TMD6_224 = packed array[0..27] of UInt8; PMD6_224 = ^TMD6_224;
TMD6_256 = packed array[0..31] of UInt8; PMD6_256 = ^TMD6_256;
TMD6_384 = packed array[0..47] of UInt8; PMD6_384 = ^TMD6_384;
TMD6_512 = packed array[0..63] of UInt8; PMD6_512 = ^TMD6_512;
TMD6Key = packed array of UInt8;
//------------------------------------------------------------------------------
const
ZeroMD6_224: TMD6_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);
ZeroMD6_256: TMD6_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);
ZeroMD6_384: TMD6_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);
ZeroMD6_512: TMD6_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);
MD6_BITS_DEFAULT = 512;
{===============================================================================
--------------------------------------------------------------------------------
TMD6Hash
--------------------------------------------------------------------------------
===============================================================================}
type
TMD6Word = UInt64;
TMD6ProcessingBlock = array of TMD6Word;
TMD6ProcessingNode = record
Index: Int64;
Bytes: TMemSize; // number of bytes already stored in block
Block: TMD6ProcessingBlock;
end;
TMD6ProcessingState = record
Levels: array of TMD6ProcessingNode;
end;
{===============================================================================
TMD6Hash - class declaration
===============================================================================}
type
TMD6Hash = class(TBlockHash)
protected
fMD6: TMD6;
fHashBits: Integer; // whole bytes only (div 8)
fKey: TMD6Key; // max length 64
fRounds: Integer; // default is 40 + (fHashBits / 4)
fRoundsDef: Boolean; // indicates if rounds were set explicitly (false) or implicitly (true)
fModeControl: Integer; // >= 0 (explicitly limited to 255)
fState: TMD6ProcessingState;
fProcessing: Boolean;
// getters setters
Function GetMD6: TMD6; virtual;
procedure SetMD6(Value: TMD6); virtual; // not used as a setter in any property
procedure SetHashBits(Value: Integer); virtual;
Function GetKey: TMD6Key; virtual;
procedure PutKey(Value: TMD6Key); virtual;
procedure SetRoundsDefault; virtual;
procedure SetRounds(Value: Integer); virtual;
procedure SetModeControl(Value: Integer); virtual;
// main processing
procedure AddTreeLevel; virtual;
procedure ProcessTreeNode(Level: Integer; const Data); virtual;
procedure ProcessTreeNodeFinal(Level: Integer; PadBytes: TMemSize); virtual;
// block hash processing
procedure ProcessBlock(const Block); override;
procedure ProcessFirst(const Block); override;
procedure ProcessLast; override;
// init/final
procedure Initialize; override;
public
class Function MD6ToLE(MD6: TMD6): TMD6; virtual;
class Function MD6ToBE(MD6: TMD6): TMD6; virtual;
class Function MD6FromLE(MD6: TMD6): TMD6; virtual;
class Function MD6FromBE(MD6: TMD6): TMD6; virtual;
Function HashSize: TMemSize; reintroduce;
class Function HashName: String; override;
class Function HashEndianness: THashEndianness; override;
class Function HashFinalization: Boolean; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TMD6); overload; virtual;
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: TMD6); reintroduce; overload; virtual;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure SetKey(const Key; Size: TMemSize); overload; virtual;
{
The Key parameter is converted to UTF8 encoding and this new string is
then used for the actual key. If it is longer than 64 bytes, it is
truncated.
}
procedure SetKey(const Key: String); overload; virtual;
property MD6: TMD6 read GetMD6;
property HashBits: Integer read fHashBits write SetHashBits;
property Key: TMD6Key read GetKey write PutKey;
property Rounds: Integer read fRounds write SetRounds;
property ModeControl: Integer read fModeControl write SetModeControl;
end;
{===============================================================================
--------------------------------------------------------------------------------
TMD6DefHash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TMD6DefHash - class declaration
===============================================================================}
type
TMD6DefHash = class(TMD6Hash)
protected
procedure SetMD6(Value: TMD6); override;
procedure SetHashBits(Value: Integer); override;
procedure PutKey(Value: TMD6Key); override;
procedure SetRounds(Value: Integer); override;
procedure SetModeControl(Value: Integer); override;
public
procedure SetKey(const Key; Size: TMemSize); overload; override;
procedure SetKey(const Key: String); overload; override;
// the properties are made read-only
property HashBits: Integer read fHashBits;
property Key: TMD6Key read GetKey;
property Rounds: Integer read fRounds;
property ModeControl: Integer read fModeControl;
end;
{===============================================================================
--------------------------------------------------------------------------------
TMD6_224Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TMD6_224Hash - class declaration
===============================================================================}
type
TMD6_224Hash = class(TMD6DefHash)
protected
Function GetMD6_224: TMD6_224; virtual;
procedure Initialize; override;
public
class Function MD6_224ToLE(MD6: TMD6_224): TMD6_224; virtual;
class Function MD6_224ToBE(MD6: TMD6_224): TMD6_224; virtual;
class Function MD6_224FromLE(MD6: TMD6_224): TMD6_224; virtual;
class Function MD6_224FromBE(MD6: TMD6_224): TMD6_224; virtual;
class Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TMD6); overload; override;
constructor CreateAndInitFrom(Hash: TMD6_224); overload; virtual;
procedure FromStringDef(const Str: String; const Default: TMD6_224); overload; virtual;
property MD6_224: TMD6_224 read GetMD6_224;
end;
{===============================================================================
--------------------------------------------------------------------------------
TMD6_256Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TMD6_256Hash - class declaration
===============================================================================}
type
TMD6_256Hash = class(TMD6DefHash)
protected
Function GetMD6_256: TMD6_256; virtual;
procedure Initialize; override;
public
class Function MD6_256ToLE(MD6: TMD6_256): TMD6_256; virtual;
class Function MD6_256ToBE(MD6: TMD6_256): TMD6_256; virtual;
class Function MD6_256FromLE(MD6: TMD6_256): TMD6_256; virtual;
class Function MD6_256FromBE(MD6: TMD6_256): TMD6_256; virtual;
class Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TMD6); overload; override;
constructor CreateAndInitFrom(Hash: TMD6_256); overload; virtual;
procedure FromStringDef(const Str: String; const Default: TMD6_256); overload; virtual;
property MD6_256: TMD6_256 read GetMD6_256;
end;
{===============================================================================
--------------------------------------------------------------------------------
TMD6_384Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TMD6_384Hash - class declaration
===============================================================================}
type
TMD6_384Hash = class(TMD6DefHash)
protected
Function GetMD6_384: TMD6_384; virtual;
procedure Initialize; override;
public
class Function MD6_384ToLE(MD6: TMD6_384): TMD6_384; virtual;
class Function MD6_384ToBE(MD6: TMD6_384): TMD6_384; virtual;
class Function MD6_384FromLE(MD6: TMD6_384): TMD6_384; virtual;
class Function MD6_384FromBE(MD6: TMD6_384): TMD6_384; virtual;
class Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TMD6); overload; override;
constructor CreateAndInitFrom(Hash: TMD6_384); overload; virtual;
procedure FromStringDef(const Str: String; const Default: TMD6_384); overload; virtual;
property MD6_384: TMD6_384 read GetMD6_384;
end;
{===============================================================================
--------------------------------------------------------------------------------
TMD6_512Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TMD6_512Hash - class declaration
===============================================================================}
type
TMD6_512Hash = class(TMD6DefHash)
protected
Function GetMD6_512: TMD6_512; virtual;
procedure Initialize; override;
public
class Function MD6_512ToLE(MD6: TMD6_512): TMD6_512; virtual;
class Function MD6_512ToBE(MD6: TMD6_512): TMD6_512; virtual;
class Function MD6_512FromLE(MD6: TMD6_512): TMD6_512; virtual;
class Function MD6_512FromBE(MD6: TMD6_512): TMD6_512; virtual;
class Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TMD6); overload; override;
constructor CreateAndInitFrom(Hash: TMD6_512); overload; virtual;
procedure FromStringDef(const Str: String; const Default: TMD6_512); overload; virtual;
property MD6_512: TMD6_512 read GetMD6_512;
end;
{===============================================================================
--------------------------------------------------------------------------------
TMD6HashParallel
--------------------------------------------------------------------------------
===============================================================================}
type
TMD6ThreadFunction = procedure(Param: Pointer);
TMD6ThreadStartCallback = procedure(Sender: TObject; ThreadFunction: TMD6ThreadFunction; Param: Pointer);
TMD6ThreadStartEvent = procedure(Sender: TObject; ThreadFunction: TMD6ThreadFunction; Param: Pointer) of object;
// internals
type
TMD6ProcessingMode = (tmBuffer,tmFile);
PMD6CommonProcessingNode = ^TMD6CommonProcessingNode;
TMD6CommonProcessingNode = record
IsLastNode: Boolean;
ParentNodePtr: PMD6CommonProcessingNode;
ParentNodePutIndex: Integer;
Counter: Integer;
LevelIndex: Integer;
Index: Int64;
Bytes: TMemSize;
Block: TMD6ProcessingBlock;
end;
TMD6CommonProcessingLevel = record
Index: Integer;
Nodes: array of TMD6CommonProcessingNode;
end;
TMD6CommonProcessingState = record
Levels: array of TMD6CommonProcessingLevel;
end;
TMD6ThreadTask = record
CurrentOffset: Int64;
State: TMD6ProcessingState;
end;
{===============================================================================
TMD6HashParallel - class declaration
===============================================================================}
type
TMD6HashParallel = class(TCustomObject)
protected
fMD6: TMD6;
{
Some of the following fields are accessed from (multiple) working threads.
Data integrity protection mechanisms are as follows:
R - read-only field, immutable during processing, does not need to be
thread-protected
I - accessed using only interlocked functions
C - combined access
}
fHashSettings: record
{R} HashBits: Integer;
{R} Key: TMD6Key;
{R} Rounds: Integer;
RoundsDef: Boolean;
{R} ModeControl: Integer;
MaxThreads: Integer;
MaxSeqNodes: Int64;
end;
fInputMessage: record
{R} ProcessingMode: TMD6ProcessingMode;
{R} Buffer: Pointer;
{R} FileName: String;
{R} Size: UInt64;
end;
fProcessingSettings: record
{R} Sequential: Boolean;
{R} ThreadCount: Integer;
{R} ThreadLevelCount: Integer;
end;
fTasksSettings: record
{R} ChunksPerTask: UInt64;
{R} BytesPerTask: UInt64;
end;
fProcessingVariables: record
{I} StopCounter: Integer;
{I} CurrentOffset: UInt64;
{I} Terminated: Boolean;
end;
fCommonTree: record
{C} State: TMD6CommonProcessingState;
end;
fProgressTracking: record
{I} CurrentIntProgress: UInt64;
end;
fProgUpdInterval: UInt32;
fDoneEvent: TEvent;
fProcessing: Boolean;
fLastProgress: Double;
// events and callbacks
fOnThreadStartCallback: TMD6ThreadStartCallback;
fOnThreadStartEvent: TMD6ThreadStartEvent;
fOnProgressCallback: TFloatCallback;
fOnProgressEvent: TFloatEvent;
// getters setters
Function GetMD6: TMD6; virtual;
procedure SetHashBits(Value: Integer); virtual;
Function GetKey: TMD6Key; virtual;
procedure PutKey(Value: TMD6Key); virtual;
procedure SetRoundsDefault; virtual;
procedure SetRounds(Value: Integer); virtual;
procedure SetModeControl(Value: Integer); virtual;
procedure SetMaxThreads(Value: Integer); virtual;
procedure SetMaxSeqNodes(Value: Int64); virtual;
// parallel processing - all following methods are called from within the worker threads
Function Thread_GetTask(var ThreadTask: TMD6ThreadTask): Boolean; virtual;
procedure Thread_ProcessThreadNode(var ThreadTask: TMD6ThreadTask; Level: Integer; const Data); virtual;
procedure Thread_ProcessThreadNodeFinal(var ThreadTask: TMD6ThreadTask; Level: Integer; PadBytes: TMemSize); virtual;
procedure Thread_CommonNodesEntry(const ThreadTask: TMD6ThreadTask); virtual;
procedure Thread_ProcessCommonNode(NodePtr: PMD6CommonProcessingNode; PutIndex: Integer; const Data); virtual;
procedure Thread_Execute; virtual;
// preparation and execution of parallel processing
Function ParallelPossible(DataSize: UInt64): Boolean; virtual;
procedure ParallelPrepare; virtual;
Function ParallelExecute(Memory: Pointer; Size: TMemSize): Integer; overload; virtual;
Function ParallelExecute(const FileName: String; Size: Int64): Integer; overload; virtual;
Function ParallelExecute: Integer; overload; virtual;
procedure DoThreadStart; virtual;
// object init/final
procedure Initialize; virtual;
// others
procedure ProgressHandler(Sender: TObject; Progress: Double); virtual;
procedure DoProgress; virtual;
public
class Function ProcessorCount: Integer; virtual;
constructor Create;
procedure SetKey(const Key; Size: TMemSize); overload; virtual;
procedure SetKey(const Key: String); overload; virtual;
Function HashMemory(Memory: Pointer; Size: TMemSize): Integer; virtual;
Function HashBuffer(const Buffer; Size: TMemSize): Integer; virtual;
Function HashFile(const FileName: String): Integer; virtual;
property MD6: TMD6 read GetMD6;
property HashBits: Integer read fHashSettings.HashBits write SetHashBits;
property Key: TMD6Key read GetKey write PutKey;
property Rounds: Integer read fHashSettings.Rounds write SetRounds;
property ModeControl: Integer read fHashSettings.ModeControl write SetModeControl;
property MaxThreads: Integer read fHashSettings.MaxThreads write SetMaxThreads;
property MaxSequentialNodes: Int64 read fHashSettings.MaxSeqNodes write SetMaxSeqNodes;
property ProgressUpdateInterval: UInt32 read fProgUpdInterval write fProgUpdInterval;
property OnThreadStartCallback: TMD6ThreadStartCallback read fOnThreadStartCallback write fOnThreadStartCallback;
property OnThreadStartEvent: TMD6ThreadStartEvent read fOnThreadStartEvent write fOnThreadStartEvent;
property OnThreadStart: TMD6ThreadStartEvent read fOnThreadStartEvent write fOnThreadStartEvent;
property OnProgressCallback: TFloatCallback read fOnProgressCallback write fOnProgressCallback;
property OnProgressEvent: TFloatEvent read fOnProgressEvent write fOnProgressEvent;
property OnProgress: TFloatEvent read fOnProgressEvent write fOnProgressEvent;
end;
{===============================================================================
--------------------------------------------------------------------------------
Standalone functions
--------------------------------------------------------------------------------
===============================================================================}
{
Note that there is, for the sake of simplicity, no function implemented for
fixed-length hash types (eg. TMD6_224), only for variant-length type TMD6.
If you want to pass a fixed type, or convert variant-length result to fixed
type, use following conversion functions to do so.
}
Function MD6ToMD6_224(Hash: TMD6): TMD6_224;
Function MD6ToMD6_256(Hash: TMD6): TMD6_256;
Function MD6ToMD6_384(Hash: TMD6): TMD6_384;
Function MD6ToMD6_512(Hash: TMD6): TMD6_512;
Function MD6_224ToMD6(Hash: TMD6_224): TMD6;
Function MD6_256ToMD6(Hash: TMD6_256): TMD6;
Function MD6_384ToMD6(Hash: TMD6_384): TMD6;
Function MD6_512ToMD6(Hash: TMD6_512): TMD6;
Function IsCompatibleMD6_224(Hash: TMD6): Boolean;
Function IsCompatibleMD6_256(Hash: TMD6): Boolean;
Function IsCompatibleMD6_384(Hash: TMD6): Boolean;
Function IsCompatibleMD6_512(Hash: TMD6): Boolean;
//------------------------------------------------------------------------------
Function MD6ToStr(MD6: TMD6): String;
Function StrToMD6(Str: String): TMD6;
Function TryStrToMD6(const Str: String; out MD6: TMD6): Boolean;
Function StrToMD6Def(const Str: String; Default: TMD6): TMD6;
Function CompareMD6(A,B: TMD6): Integer;
Function SameMD6(A,B: TMD6): Boolean;
Function BinaryCorrectMD6(Hash: TMD6): TMD6;
//------------------------------------------------------------------------------
{
For MD6, it is not enough to pass hash from previous step when doing
continuous hashing (BufferMD6 > LastBufferMD6). TDM6State type is introduced
for this purpose.
}
type
TMD6State = type Pointer;
TMD6Settings = record
HashBits: Integer;
Rounds: Integer;
ModeControl: Integer;
Key: TMD6Key;
end;
Function MD6Settings(HashBits,Rounds,ModeControl: Integer; Key: TMD6Key): TMD6Settings; overload;
Function MD6Settings(HashBits,Rounds,ModeControl: Integer; const Key; KeySize: TMemSize): TMD6Settings; overload;
Function MD6Settings(HashBits,Rounds,ModeControl: Integer; const Key: String): TMD6Settings; overload;
Function InitialMD6(Settings: TMD6Settings): TMD6State; overload;
Function InitialMD6(HashBits: Integer = MD6_BITS_DEFAULT): TMD6State; overload;
procedure BufferMD6(State: TMD6State; const Buffer; Size: TMemSize); overload;
Function LastBufferMD6(var State: TMD6State; const Buffer; Size: TMemSize): TMD6;
//------------------------------------------------------------------------------
Function BufferMD6(const Buffer; Size: TMemSize; Settings: TMD6Settings): TMD6; overload;
Function BufferMD6(const Buffer; Size: TMemSize; HashBits: Integer = MD6_BITS_DEFAULT): TMD6; overload;
Function AnsiStringMD6(const Str: AnsiString; Settings: TMD6Settings): TMD6; overload;
Function AnsiStringMD6(const Str: AnsiString; HashBits: Integer = MD6_BITS_DEFAULT): TMD6; overload;
Function WideStringMD6(const Str: WideString; Settings: TMD6Settings): TMD6; overload;
Function WideStringMD6(const Str: WideString; HashBits: Integer = MD6_BITS_DEFAULT): TMD6; overload;
Function StringMD6(const Str: String; Settings: TMD6Settings): TMD6; overload;
Function StringMD6(const Str: String; HashBits: Integer = MD6_BITS_DEFAULT): TMD6; overload;
Function StreamMD6(Stream: TStream; Count: Int64; Settings: TMD6Settings): TMD6; overload;
Function StreamMD6(Stream: TStream; Count: Int64 = -1; HashBits: Integer = MD6_BITS_DEFAULT): TMD6; overload;
Function FileMD6(const FileName: String; Settings: TMD6Settings): TMD6; overload;
Function FileMD6(const FileName: String; HashBits: Integer = MD6_BITS_DEFAULT): TMD6; overload;
//------------------------------------------------------------------------------
type
TMD6Context = type Pointer;
Function MD6_Init(Settings: TMD6Settings): TMD6Context; overload;
Function MD6_Init(HashBits: Integer = MD6_BITS_DEFAULT): TMD6Context; overload;
procedure MD6_Update(Context: TMD6Context; const Buffer; Size: TMemSize);
Function MD6_Final(var Context: TMD6Context; const Buffer; Size: TMemSize): TMD6; overload;
Function MD6_Final(var Context: TMD6Context): TMD6; overload;
Function MD6_Hash(const Buffer; Size: TMemSize; HashBits: Integer = MD6_BITS_DEFAULT): TMD6;
implementation
uses
{$IFDEF Windows}Windows,{$ELSE}BaseUnix,{$ENDIF} Math,
StrRect, BitOps, InterlockedOps, StaticMemoryStream;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$ENDIF}
{===============================================================================
General utilities
===============================================================================}
{$IFDEF Windows}
procedure GetNativeSystemInfo(lpSystemInfo: PSystemInfo); stdcall; external kernel32;
{$ELSE}
const
_SC_NPROCESSORS_ONLN = 84;
Function sysconf(name: cInt): cLong; cdecl; external;
{$ENDIF}
//------------------------------------------------------------------------------
{$IF not Declared(Ceil64)}
Function Ceil64(x: Extended): Int64;
begin
Result := Trunc(x);
If Frac(x) > 0 then
Result := Result + 1;
end;
{$IFEND}
{===============================================================================
--------------------------------------------------------------------------------
TMD6Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TMD6Hash - Implementation constants
===============================================================================}
const
MD6_BITS_MAX = 512;
MD6_MODE_DEFAULT = 64;
MD6_KEY_MAXLEN = 64; // in bytes
MD6_CHUNK_SIZE = 128; // bytes (16 words)
MD6_CHUNK_LEN = 16; // words
MD6_BLOCK_LEN = 89; // words
MD6_BLOCK_KEYSTART = 15;
MD6_BLOCK_KEYEND = 22;
MD6_BLOCK_DATASTART = 25;
MD6_BLOCK_CAPACITY = 512; // bytes (64 words)
MD6_BLOCK_IDX_U = 23; // position of unique node ID word
MD6_BLOCK_IDX_V = 24; // position of control word
// fractional part of sqrt(6)
MD6_VEC_Q: array[0..14] of TMD6Word = (
TMD6Word($7311c2812425cfa0), TMD6Word($6432286434aac8e7),
TMD6Word($b60450e9ef68b7c1), TMD6Word($e8fb23908d9f06f1),
TMD6Word($dd2e76cba691e5bf), TMD6Word($0cd0d63b2c30bc41),
TMD6Word($1f8ccf6823058f8a), TMD6Word($54e5ed5b88e3775d),
TMD6Word($4ad12aae0a6d6031), TMD6Word($3e7f16bb88222e0d),
TMD6Word($8af8671d3fb50c2c), TMD6Word($995ad1178bd25c31),
TMD6Word($c878c1dd04c4b633), TMD6Word($3b72066c7a1552ac),
TMD6Word($0d6f3522631effcb));
MD6_S_0 = TMD6Word($0123456789abcdef);
MD6_S_MASK = TMD6Word($7311c2812425cfa0);
(*
--------------------------------------------------------------------------------
Following constants are not used anywhere in the code, they are instead
directly expanded into numerals where needed or calculated on-the-fly.
--------------------------------------------------------------------------------
{
Round constants MD6_S can be calculated like this:
MD6_S[0] := MD6_S_0;
For i := 1 to High(MD6_S) do
MD6_S[i] := ROL(MD6_S[i - 1],1) xor (MD6_S[i - 1] and MD6_S_STAR)
}
MD6_S: array[0..167] of TMD6Word = (
TMD6Word($0123456789ABCDEF), TMD6Word($0347CACE1376567E),
TMD6Word($058E571C26C8EADC), TMD6Word($0A1CEC3869911F38),
TMD6Word($16291870F3233150), TMD6Word($3E5330E1C66763A0),
TMD6Word($4EB7614288EB84E0), TMD6Word($DF7F828511F68D60),
TMD6Word($EDEE878B23C997E1), TMD6Word($BADD8D976792A863),
TMD6Word($47AA9BAFEB25D8E7), TMD6Word($CC55B5DEF66E796E),
TMD6Word($D8BAEB3DC8F8BBFD), TMD6Word($E165147A91D1FC5B),
TMD6Word($A3CB28F523A234B7), TMD6Word($6497516B67646DCF),
TMD6Word($A93FE2D7EAEC961E), TMD6Word($736E072EF5FDAA3D),
TMD6Word($95DC0C5DCFDEDE5A), TMD6Word($3AA818BA9BB972B5),
TMD6Word($475031F53753A7CA), TMD6Word($CDB0636B4AA6C814),
TMD6Word($DA7084D795695829), TMD6Word($E6F1892E2EF3F873),
TMD6Word($AFF2925C79C638C7), TMD6Word($7CF5A6B8D388790F),
TMD6Word($89FACFF1A710BB1E), TMD6Word($12E55D626A21FD3D),
TMD6Word($37CBFAC4F462375A), TMD6Word($5C963709CCE469B4),
TMD6Word($E93C6C129DEC9AC8), TMD6Word($B36898253FFDBF11),
TMD6Word($55D1B04B5BDEF123), TMD6Word($FAB2E097B7B92366),
TMD6Word($877501AE4B5345ED), TMD6Word($0DFB03DC96A7CE7B),
TMD6Word($1AE70539296A52D6), TMD6Word($27CF0A7372F4E72C),
TMD6Word($6C9F16E7C5CD0978), TMD6Word($B92F2F4E8F9F1BD0),
TMD6Word($435F5C9D1B3B3C21), TMD6Word($C5AFF9BB36577462),
TMD6Word($CA5E33F748ABACE5), TMD6Word($D6AC656F9176D56B),
TMD6Word($FF588ADE22C96FF7), TMD6Word($8DA1973C6593904F),
TMD6Word($1A42AC78EF26A09F), TMD6Word($2685D8F1FA69C1BE),
TMD6Word($6F0A7162D4F242DC), TMD6Word($BD14A2C5ADC4C738),
TMD6Word($4B39C70A7F8D4951), TMD6Word($D5624C14DB1FDBA2),
TMD6Word($FBC4D829B63A7CE5), TMD6Word($848970524854B56B),
TMD6Word($0913A0A490ADEFF7), TMD6Word($1336C1C9217E104E),
TMD6Word($357D431362D8209C), TMD6Word($5BEBC427E5B041B8),
TMD6Word($E4D6484EEF40C2D0), TMD6Word($A9BCD09DFA814721),
TMD6Word($726961BAD503C963), TMD6Word($96D383F5AE065BE6),
TMD6Word($3FB6856A7808FC6D), TMD6Word($4C7D8AD4D01134FA),
TMD6Word($D8EA9729A0236D54), TMD6Word($E1D5AC52606797A9),
TMD6Word($A2BAD8A4E0EAA8F3), TMD6Word($676571C9E1F5D947),
TMD6Word($ADCBA312E3CE7B8E), TMD6Word($7A96C425E798BC9D),
TMD6Word($873D484AEB31F5BA), TMD6Word($0D6BD095F6422ED5),
TMD6Word($1BD661AAC884532A), TMD6Word($24BC83D5910CE574),
TMD6Word($6969852A221D0FC8), TMD6Word($B3D28A54643F1010),
TMD6Word($54B596A8EC5B2021), TMD6Word($F97AAFD1FCB74062),
TMD6Word($83E5DD22DD4BC0E5), TMD6Word($04CA7A45BE96416B),
TMD6Word($0994B68A5928C3F6), TMD6Word($1239EF94B271444C),
TMD6Word($36621DA944C3CC98), TMD6Word($5EC43BD38D8655B0),
TMD6Word($EF8875261F08EEC0), TMD6Word($BC10AA4C3A111301),
TMD6Word($4831D69854232503), TMD6Word($D0726FB0AC674F06),
TMD6Word($F0F49DE17CEBD10D), TMD6Word($91F9BB43DDF6631B),
TMD6Word($32E2F486BFC88537), TMD6Word($57C5298D5B918F4E),
TMD6Word($FC8B539BB722919C), TMD6Word($8917E5B64A65A2B9),
TMD6Word($133E0BEC94EEC7D3), TMD6Word($356C15592DF94826),
TMD6Word($5BD82AB37FD3D86C), TMD6Word($E4A057E7DBA678F8),
TMD6Word($A940ED4EB768B951), TMD6Word($73811A9D4AF1FBA3),
TMD6Word($940337BB95C23CE6), TMD6Word($38076DF62F84756D),
TMD6Word($400F9B6C7B0CAFFA), TMD6Word($C01EB4D8D61DD054),
TMD6Word($C02DE931A83E60A9), TMD6Word($C05A1262705881F3),
TMD6Word($C0A426C4C0B18247), TMD6Word($C1484F098142868F),
TMD6Word($C390DC1202858B9F), TMD6Word($C4317824050E9CBF),
TMD6Word($C873B0480E19B5DF), TMD6Word($D0F6E0901832EE3F),
TMD6Word($F1FD01A03045125F), TMD6Word($92EB03C0408F26BF),
TMD6Word($37D70500811B4BDF), TMD6Word($5CBF0A010237DC3E),
TMD6Word($E96F1603044A745C), TMD6Word($B3DF2E070C94ACB9),
TMD6Word($54AF5E0F1D2DD5D3), TMD6Word($F95FFE1F3E7E6E26),
TMD6Word($83AE3E3F58D8926D), TMD6Word($045C7E7FB1B1A6FB),
TMD6Word($08A8BEFE4342CB56), TMD6Word($1151FF7C86855DAC),
TMD6Word($33B23CF9090FF6F8), TMD6Word($54747973121A2B50),
TMD6Word($F8F8B2E724345DA0), TMD6Word($81E1E74F6C4CF6E1),
TMD6Word($02C20C9FFC9D2B63), TMD6Word($078419BEDD3F5DE6),
TMD6Word($0C0833FDBE5BF66C), TMD6Word($1810657A58B62AF8),
TMD6Word($20308AF4B1485F50), TMD6Word($607197694290F1A0),
TMD6Word($A0F2ACD3852122E0), TMD6Word($61F5D9260E634761),
TMD6Word($A2FA724C18E7C9E2), TMD6Word($67E4A69831EA5A65),
TMD6Word($ACC9CFB043F4FEEA), TMD6Word($79925DE087CD3375),
TMD6Word($8234FB410B9F65CA), TMD6Word($06793483173B8E15),
TMD6Word($0EE369872A56922A), TMD6Word($1FC7938F74A9A674),
TMD6Word($2C8EA59FCD72CAC8), TMD6Word($791DCBBE9EC55F10),
TMD6Word($832A55FD398FF120), TMD6Word($0554EB7B531A2361),
TMD6Word($0BB914F7A63445E2), TMD6Word($1463296E684CCE64),
TMD6Word($38C752DCF09D52E8), TMD6Word($418FE739C13FE770),
TMD6Word($C21E0C72825A09C0), TMD6Word($C62C18E504B41A01),
TMD6Word($CE58314B0D4C3E03), TMD6Word($DEA062971E9C7207),
TMD6Word($EF4087AF393CA60F), TMD6Word($BD818DDF525DCA1F),
TMD6Word($4A029B3FA4BE5E3F), TMD6Word($D605B47E6D58F25E),
TMD6Word($FE0AE8FCFEB126BD), TMD6Word($8E151179D9434BDB),
TMD6Word($1E3B22F2B287DC37), TMD6Word($2E674765450A744E),
TMD6Word($7ECFCCCB8E14AC9C), TMD6Word($8F9E5916182DD5B8),
TMD6Word($1C2CF22C307E6ED1), TMD6Word($2859265840D89322));
// tap positions
MD6_TAP: array[0..4] of Integer = (17,18,21,31,67);
// right shifts
MD6_SHIFT_R: array[0..15] of Integer = (
10, 5, 13, 10, 11, 12, 2, 7, 14, 15, 7, 13, 11, 7, 6, 12);
// left shifts
MD6_SHIFT_L: array[0..15] of Integer = (
11, 24, 9, 16, 15, 9, 27, 15, 6, 2, 29, 8, 15, 5, 31, 9);
*)
{===============================================================================
TMD6Hash - utility functions
===============================================================================}
Function GetControlWord(Rnds, ModeCtrl, PadBits, KeyLen, HashBits: Integer; Final: Boolean): TMD6Word;
begin
Result := 0 or
(TMD6Word(Rnds and $FFF) shl 48) or
(TMD6Word(ModeCtrl and $FF) shl 40) or
(TMD6Word(Integer(IfThen(Final,1,0)) and $F) shl 36) or
(TMD6Word(PadBits and $FFFF) shl 20) or
(TMD6Word(KeyLen and $FF) shl 12) or
TMD6Word(HashBits and $FFF);
end;
//------------------------------------------------------------------------------
Function GetUniqueNodeIDWord(LevelNumber: Integer; NodeIndex: Int64): TMD6Word;
begin
Result := (TMD6Word(LevelNumber and $FF) shl 56) or (NodeIndex and $00FFFFFFFFFFFFFF);
end;
//------------------------------------------------------------------------------
procedure InitializeBlock(out Block: TMD6ProcessingBlock; Rounds: Integer; Key: TMD6Key);
var
i: Integer;
begin
Block := nil;
SetLength(Block,MD6_BLOCK_LEN + (Rounds * MD6_CHUNK_LEN));
For i := Low(MD6_VEC_Q) to High(MD6_VEC_Q) do
Block[i] := MD6_VEC_Q[i];
// prepare key
If Length(Key) > 0 then
begin
Move(Key[0],Block[MD6_BLOCK_KEYSTART],Length(Key));
{$IFNDEF ENDIAN_BIG}
For i := MD6_BLOCK_KEYSTART to MD6_BLOCK_KEYEND do
EndianSwapValue(Block[i]);
{$ENDIF}
end;
end;
//------------------------------------------------------------------------------
procedure CompressBlock(var Block: TMD6ProcessingBlock);
var
i: Integer;
RoundConst: TMD6Word;
x: TMD6Word;
begin
// block is assumed to be completely prepared, only do endianness corrections
{$IFNDEF ENDIAN_BIG}
For i := MD6_BLOCK_DATASTART to Pred(MD6_BLOCK_LEN) do
EndianSwapValue(Block[i]);
{$ENDIF}
// main calculation...
RoundConst := MD6_S_0;
i := MD6_BLOCK_LEN;
while i <= (Length(Block) - MD6_CHUNK_LEN) do
begin
// unrolled round (16 steps)...
// step 0
x := RoundConst xor Block[i - 89] xor Block[i - 17];
x := x xor (Block[i - 18] and Block[i - 21]);
x := x xor (Block[i - 31] and Block[i - 67]);
x := x xor (x shr 10);
Block[i] := x xor (x shl 11);
// step 1
x := RoundConst xor Block[i - 88] xor Block[i - 16];
x := x xor (Block[i - 17] and Block[i - 20]);
x := x xor (Block[i - 30] and Block[i - 66]);
x := x xor (x shr 5);
Block[i + 1] := x xor (x shl 24);
// step 2
x := RoundConst xor Block[i - 87] xor Block[i - 15];
x := x xor (Block[i - 16] and Block[i - 19]);
x := x xor (Block[i - 29] and Block[i - 65]);
x := x xor (x shr 13);
Block[i + 2] := x xor (x shl 9);
// step 3
x := RoundConst xor Block[i - 86] xor Block[i - 14];
x := x xor (Block[i - 15] and Block[i - 18]);
x := x xor (Block[i - 28] and Block[i - 64]);
x := x xor (x shr 10);
Block[i + 3] := x xor (x shl 16);
// step 4
x := RoundConst xor Block[i - 85] xor Block[i - 13];
x := x xor (Block[i - 14] and Block[i - 17]);
x := x xor (Block[i - 27] and Block[i - 63]);
x := x xor (x shr 11);
Block[i + 4] := x xor (x shl 15);
// step 5
x := RoundConst xor Block[i - 84] xor Block[i - 12];
x := x xor (Block[i - 13] and Block[i - 16]);
x := x xor (Block[i - 26] and Block[i - 62]);
x := x xor (x shr 12);
Block[i + 5] := x xor (x shl 9);
// step 6
x := RoundConst xor Block[i - 83] xor Block[i - 11];
x := x xor (Block[i - 12] and Block[i - 15]);
x := x xor (Block[i - 25] and Block[i - 61]);
x := x xor (x shr 2);
Block[i + 6] := x xor (x shl 27);
// step 7
x := RoundConst xor Block[i - 82] xor Block[i - 10];
x := x xor (Block[i - 11] and Block[i - 14]);
x := x xor (Block[i - 24] and Block[i - 60]);
x := x xor (x shr 7);
Block[i + 7] := x xor (x shl 15);
// step 8
x := RoundConst xor Block[i - 81] xor Block[i - 9];
x := x xor (Block[i - 10] and Block[i - 13]);
x := x xor (Block[i - 23] and Block[i - 59]);
x := x xor (x shr 14);
Block[i + 8] := x xor (x shl 6);
// step 9
x := RoundConst xor Block[i - 80] xor Block[i - 8];
x := x xor (Block[i - 9] and Block[i - 12]);
x := x xor (Block[i - 22] and Block[i - 58]);
x := x xor (x shr 15);
Block[i + 9] := x xor (x shl 2);
// step 10
x := RoundConst xor Block[i - 79] xor Block[i - 7];
x := x xor (Block[i - 8] and Block[i - 11]);
x := x xor (Block[i - 21] and Block[i - 57]);
x := x xor (x shr 7);
Block[i + 10] := x xor (x shl 29);
// step 11
x := RoundConst xor Block[i - 78] xor Block[i - 6];
x := x xor (Block[i - 7] and Block[i - 10]);
x := x xor (Block[i - 20] and Block[i - 56]);
x := x xor (x shr 13);
Block[i + 11] := x xor (x shl 8);
// step 12
x := RoundConst xor Block[i - 77] xor Block[i - 5];
x := x xor (Block[i - 6] and Block[i - 9]);
x := x xor (Block[i - 19] and Block[i - 55]);
x := x xor (x shr 11);
Block[i + 12] := x xor (x shl 15);
// step 13
x := RoundConst xor Block[i - 76] xor Block[i - 4];
x := x xor (Block[i - 5] and Block[i - 8]);
x := x xor (Block[i - 18] and Block[i - 54]);
x := x xor (x shr 7);
Block[i + 13] := x xor (x shl 5);
// step 14
x := RoundConst xor Block[i - 75] xor Block[i - 3];
x := x xor (Block[i - 4] and Block[i - 7]);
x := x xor (Block[i - 17] and Block[i - 53]);
x := x xor (x shr 6);
Block[i + 14] := x xor (x shl 31);
// step 15
x := RoundConst xor Block[i - 74] xor Block[i - 2];
x := x xor (Block[i - 3] and Block[i - 6]);
x := x xor (Block[i - 16] and Block[i - 52]);
x := x xor (x shr 12);
Block[i + 15] := x xor (x shl 9);
// recalculate round constant
RoundConst := ROL(RoundConst,1) xor (RoundConst and MD6_S_MASK);
// increment index by number of steps taken
Inc(i,16);
end;
// endianness corection for chaining variable (last 1024 bits, 16 words, one chunk)
{$IFNDEF ENDIAN_BIG}
For i := (Length(Block) - MD6_CHUNK_LEN) to High(Block) do
EndianSwapValue(Block[i]);