-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathHlpSHA1Dispatch.pas
More file actions
216 lines (179 loc) · 5.94 KB
/
HlpSHA1Dispatch.pas
File metadata and controls
216 lines (179 loc) · 5.94 KB
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
unit HlpSHA1Dispatch;
{$I ..\Include\HashLib.inc}
interface
type
TSHA1CompressProc = procedure(AState, AData: Pointer; ANumBlocks: UInt32);
var
SHA1_Compress: TSHA1CompressProc;
const
// K constants replicated 4x for SIMD, followed by BSWAP32 mask.
// Layout: K_00_19 (16B) at 0, K_20_39 at 16, K_40_59 at 32,
// K_60_79 at 48, BSWAP32 mask at 64.
K_SHA1: array [0 .. 19] of UInt32 = (
$5A827999, $5A827999, $5A827999, $5A827999,
$6ED9EBA1, $6ED9EBA1, $6ED9EBA1, $6ED9EBA1,
$8F1BBCDC, $8F1BBCDC, $8F1BBCDC, $8F1BBCDC,
$CA62C1D6, $CA62C1D6, $CA62C1D6, $CA62C1D6,
$00010203, $04050607, $08090A0B, $0C0D0E0F
);
implementation
uses
HlpBits,
HlpConverters,
HlpCpuFeatures,
HlpSimdLevels;
// =============================================================================
// Scalar fallback implementation
// =============================================================================
procedure SHA1_Compress_Scalar(AState, AData: Pointer; ANumBlocks: UInt32);
var
LPState: PCardinal;
LPData: PByte;
LA, LB, LC, LD, LE, LT: UInt32;
LW: array [0 .. 79] of UInt32;
LRound: Int32;
begin
LPState := PCardinal(AState);
LPData := PByte(AData);
while ANumBlocks > 0 do
begin
TConverters.be32_copy(LPData, 0, @LW[0], 0, 64);
for LRound := 16 to 79 do
begin
LT := LW[LRound - 3] xor LW[LRound - 8] xor LW[LRound - 14]
xor LW[LRound - 16];
LW[LRound] := TBits.RotateLeft32(LT, 1);
end;
LA := LPState[0]; LB := LPState[1]; LC := LPState[2];
LD := LPState[3]; LE := LPState[4];
for LRound := 0 to 19 do
begin
LT := TBits.RotateLeft32(LA, 5) + (LD xor (LB and (LC xor LD)))
+ LE + $5A827999 + LW[LRound];
LE := LD; LD := LC; LC := TBits.RotateLeft32(LB, 30);
LB := LA; LA := LT;
end;
for LRound := 20 to 39 do
begin
LT := TBits.RotateLeft32(LA, 5) + (LB xor LC xor LD)
+ LE + $6ED9EBA1 + LW[LRound];
LE := LD; LD := LC; LC := TBits.RotateLeft32(LB, 30);
LB := LA; LA := LT;
end;
for LRound := 40 to 59 do
begin
LT := TBits.RotateLeft32(LA, 5) +
((LB and LC) or (LD and (LB or LC)))
+ LE + $8F1BBCDC + LW[LRound];
LE := LD; LD := LC; LC := TBits.RotateLeft32(LB, 30);
LB := LA; LA := LT;
end;
for LRound := 60 to 79 do
begin
LT := TBits.RotateLeft32(LA, 5) + (LB xor LC xor LD)
+ LE + $CA62C1D6 + LW[LRound];
LE := LD; LD := LC; LC := TBits.RotateLeft32(LB, 30);
LB := LA; LA := LT;
end;
LPState[0] := LPState[0] + LA; LPState[1] := LPState[1] + LB;
LPState[2] := LPState[2] + LC; LPState[3] := LPState[3] + LD;
LPState[4] := LPState[4] + LE;
System.FillChar(LW, System.SizeOf(LW), 0);
System.Inc(LPData, 64);
System.Dec(ANumBlocks);
end;
end;
// =============================================================================
// SIMD implementations: SSE2 / SSSE3 (IA-32); ShaNi, SSE2, SSSE3, AVX2 (x86-64)
// =============================================================================
{$IFDEF HASHLIB_I386_ASM}
procedure SHA1_Compress_Sse2(AState, AData: Pointer; ANumBlocks: UInt32);
{$I ..\Include\Simd\Common\SimdProc3Begin_i386.inc}
{$I ..\Include\Simd\SHA1\SHA1CompressSse2_i386.inc}
end;
procedure SHA1_Compress_Ssse3(AState, AData: Pointer; ANumBlocks: UInt32;
AConstants: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_i386.inc}
{$I ..\Include\Simd\SHA1\SHA1CompressSsse3_i386.inc}
end;
procedure SHA1_Compress_Ssse3_Wrap(AState, AData: Pointer; ANumBlocks: UInt32);
begin
SHA1_Compress_Ssse3(AState, AData, ANumBlocks, @K_SHA1);
end;
{$ENDIF HASHLIB_I386_ASM}
{$IFDEF HASHLIB_X86_64_ASM}
procedure SHA1_Compress_ShaNi(AState, AData: Pointer; ANumBlocks: UInt32;
AConstants: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_x86_64.inc}
{$I ..\Include\Simd\SHA1\SHA1CompressShaNi_x86_64.inc}
end;
procedure SHA1_Compress_ShaNi_Wrap(AState, AData: Pointer; ANumBlocks: UInt32);
begin
SHA1_Compress_ShaNi(AState, AData, ANumBlocks, @K_SHA1);
end;
procedure SHA1_Compress_Sse2(AState, AData: Pointer; ANumBlocks: UInt32);
{$I ..\Include\Simd\Common\SimdProc3Begin_x86_64.inc}
{$I ..\Include\Simd\SHA1\SHA1CompressSse2_x86_64.inc}
end;
procedure SHA1_Compress_Ssse3(AState, AData: Pointer; ANumBlocks: UInt32;
AConstants: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_x86_64.inc}
{$I ..\Include\Simd\SHA1\SHA1CompressSsse3_x86_64.inc}
end;
procedure SHA1_Compress_Ssse3_Wrap(AState, AData: Pointer; ANumBlocks: UInt32);
begin
SHA1_Compress_Ssse3(AState, AData, ANumBlocks, @K_SHA1);
end;
procedure SHA1_Compress_Avx2(AState, AData: Pointer; ANumBlocks: UInt32;
AConstants: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_x86_64.inc}
{$I ..\Include\Simd\SHA1\SHA1CompressAvx2_x86_64.inc}
end;
procedure SHA1_Compress_Avx2_Wrap(AState, AData: Pointer; ANumBlocks: UInt32);
begin
SHA1_Compress_Avx2(AState, AData, ANumBlocks, @K_SHA1);
end;
{$ENDIF HASHLIB_X86_64_ASM}
// =============================================================================
// Dispatch initialization
// =============================================================================
procedure InitDispatch();
begin
SHA1_Compress := @SHA1_Compress_Scalar;
{$IFDEF HASHLIB_I386_ASM}
case TCpuFeatures.X86.GetSimdLevel() of
TX86SimdLevel.SSSE3:
begin
SHA1_Compress := @SHA1_Compress_Ssse3_Wrap;
end;
TX86SimdLevel.SSE2:
begin
SHA1_Compress := @SHA1_Compress_Sse2;
end;
end;
{$ENDIF}
{$IFDEF HASHLIB_X86_64_ASM}
if TCpuFeatures.X86.HasSHANI() then
begin
SHA1_Compress := @SHA1_Compress_ShaNi_Wrap;
Exit;
end;
case TCpuFeatures.X86.GetSimdLevel() of
TX86SimdLevel.AVX2:
begin
SHA1_Compress := @SHA1_Compress_Avx2_Wrap;
end;
TX86SimdLevel.SSSE3:
begin
SHA1_Compress := @SHA1_Compress_Ssse3_Wrap;
end;
TX86SimdLevel.SSE2:
begin
SHA1_Compress := @SHA1_Compress_Sse2;
end;
end;
{$ENDIF}
end;
initialization
InitDispatch();
end.