-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
HlpRadioGatun32.pas
212 lines (173 loc) · 4.27 KB
/
HlpRadioGatun32.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
unit HlpRadioGatun32;
{$I ..\Include\HashLib.inc}
interface
uses
HlpHashLibTypes,
{$IFDEF DELPHI}
HlpHashBuffer,
HlpHash,
{$ENDIF DELPHI}
HlpBits,
HlpConverters,
HlpIHash,
HlpIHashInfo,
HlpArrayUtils,
HlpHashCryptoNotBuildIn;
type
TRadioGatun32 = class sealed(TBlockHash, ICryptoNotBuildIn, ITransformBlock)
strict private
var
FMill: THashLibUInt32Array;
FBelt: THashLibMatrixUInt32Array;
procedure RoundFunction();
strict protected
procedure Finish(); override;
function GetResult(): THashLibByteArray; override;
procedure TransformBlock(AData: PByte; ADataLength: Int32;
AIndex: Int32); override;
public
constructor Create();
procedure Initialize(); override;
function Clone(): IHash; override;
end;
implementation
{ TRadioGatun32 }
function TRadioGatun32.Clone(): IHash;
var
LHashInstance: TRadioGatun32;
begin
LHashInstance := TRadioGatun32.Create();
LHashInstance.FMill := System.Copy(FMill);
LHashInstance.FBelt := TArrayUtils.Clone(FBelt);
LHashInstance.FBuffer := FBuffer.Clone();
LHashInstance.FProcessedBytesCount := FProcessedBytesCount;
result := LHashInstance as IHash;
result.BufferSize := BufferSize;
end;
constructor TRadioGatun32.Create;
var
LIdx: Int32;
begin
Inherited Create(32, 12);
System.SetLength(FMill, 19);
System.SetLength(FBelt, 13);
LIdx := 0;
while LIdx < 13 do
begin
System.SetLength(FBelt[LIdx], 3);
System.Inc(LIdx);
end;
end;
procedure TRadioGatun32.Finish;
var
LPaddingSize, LIdx: Int32;
LPad: THashLibByteArray;
begin
LPaddingSize := 12 - ((Int32(FProcessedBytesCount)) mod 12);
System.SetLength(LPad, LPaddingSize);
LPad[0] := $01;
TransformBytes(LPad, 0, LPaddingSize);
LIdx := 0;
while LIdx < 16 do
begin
RoundFunction();
System.Inc(LIdx);
end;
end;
function TRadioGatun32.GetResult: THashLibByteArray;
var
LBuffer: THashLibUInt32Array;
LIdx: Int32;
begin
System.SetLength(LBuffer, 8);
System.SetLength(result, System.Length(LBuffer) * System.SizeOf(UInt32));
LIdx := 0;
while LIdx < 4 do
begin
RoundFunction();
System.Move(FMill[1], LBuffer[LIdx * 2], 2 * System.SizeOf(UInt32));
System.Inc(LIdx);
end;
TConverters.le32_copy(PCardinal(LBuffer), 0, PByte(result), 0,
System.Length(result));
end;
procedure TRadioGatun32.Initialize;
begin
TArrayUtils.ZeroFill(FMill);
TArrayUtils.ZeroFill(FBelt);
Inherited Initialize();
end;
procedure TRadioGatun32.RoundFunction;
var
LQ: THashLibUInt32Array;
LA: array [0 .. 18] of UInt32;
LIdx: Int32;
begin
LQ := FBelt[12];
LIdx := 12;
while LIdx > 0 do
begin
FBelt[LIdx] := FBelt[LIdx - 1];
System.Dec(LIdx);
end;
FBelt[0] := LQ;
LIdx := 0;
while LIdx < 12 do
begin
FBelt[LIdx + 1][LIdx mod 3] := FBelt[LIdx + 1][LIdx mod 3] xor FMill
[LIdx + 1];
System.Inc(LIdx);
end;
LIdx := 0;
while LIdx < 19 do
begin
LA[LIdx] := FMill[LIdx] xor (FMill[(LIdx + 1) mod 19] or
not FMill[(LIdx + 2) mod 19]);
System.Inc(LIdx);
end;
LIdx := 0;
while LIdx < 19 do
begin
FMill[LIdx] := TBits.RotateRight32(LA[(7 * LIdx) mod 19],
(LIdx * (LIdx + 1)) shr 1);
System.Inc(LIdx);
end;
LIdx := 0;
while LIdx < 19 do
begin
LA[LIdx] := FMill[LIdx] xor FMill[(LIdx + 1) mod 19] xor FMill
[(LIdx + 4) mod 19];
System.Inc(LIdx);
end;
LA[0] := LA[0] xor 1;
LIdx := 0;
while LIdx < 19 do
begin
FMill[LIdx] := LA[LIdx];
System.Inc(LIdx);
end;
LIdx := 0;
while LIdx < 3 do
begin
FMill[LIdx + 13] := FMill[LIdx + 13] xor LQ[LIdx];
System.Inc(LIdx);
end;
end;
procedure TRadioGatun32.TransformBlock(AData: PByte; ADataLength: Int32;
AIndex: Int32);
var
LData: array [0 .. 2] of UInt32;
LIdx: Int32;
begin
TConverters.le32_copy(AData, AIndex, @(LData[0]), 0, ADataLength);
LIdx := 0;
while LIdx < 3 do
begin
FMill[LIdx + 16] := FMill[LIdx + 16] xor LData[LIdx];
FBelt[0][LIdx] := FBelt[0][LIdx] xor LData[LIdx];
System.Inc(LIdx);
end;
RoundFunction();
System.FillChar(LData, System.SizeOf(LData), UInt32(0));
end;
end.