-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathHlpSHA2_256Dispatch.pas
More file actions
226 lines (187 loc) · 6.79 KB
/
HlpSHA2_256Dispatch.pas
File metadata and controls
226 lines (187 loc) · 6.79 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
217
218
219
220
221
222
223
224
225
226
unit HlpSHA2_256Dispatch;
{$I ..\Include\HashLib.inc}
interface
type
TSHA256CompressProc = procedure(AState, AData: Pointer; ANumBlocks: UInt32);
var
SHA256_Compress: TSHA256CompressProc;
const
// K256 round constants (64 UInt32 = 256 bytes)
// followed by BSWAP32 mask for pshufb (4 UInt32 = 16 bytes) at offset 256
K256: array [0 .. 67] of UInt32 = (
$428A2F98, $71374491, $B5C0FBCF, $E9B5DBA5,
$3956C25B, $59F111F1, $923F82A4, $AB1C5ED5,
$D807AA98, $12835B01, $243185BE, $550C7DC3,
$72BE5D74, $80DEB1FE, $9BDC06A7, $C19BF174,
$E49B69C1, $EFBE4786, $0FC19DC6, $240CA1CC,
$2DE92C6F, $4A7484AA, $5CB0A9DC, $76F988DA,
$983E5152, $A831C66D, $B00327C8, $BF597FC7,
$C6E00BF3, $D5A79147, $06CA6351, $14292967,
$27B70A85, $2E1B2138, $4D2C6DFC, $53380D13,
$650A7354, $766A0ABB, $81C2C92E, $92722C85,
$A2BFE8A1, $A81A664B, $C24B8B70, $C76C51A3,
$D192E819, $D6990624, $F40E3585, $106AA070,
$19A4C116, $1E376C08, $2748774C, $34B0BCB5,
$391C0CB3, $4ED8AA4A, $5B9CCA4F, $682E6FF3,
$748F82EE, $78A5636F, $84C87814, $8CC70208,
$90BEFFFA, $A4506CEB, $BEF9A3F7, $C67178F2,
// BSWAP32 mask at offset 256: reverses bytes within each dword
$00010203, $04050607, $08090A0B, $0C0D0E0F
);
implementation
uses
HlpBits,
HlpConverters,
HlpCpuFeatures,
HlpSimdLevels;
// =============================================================================
// Scalar fallback implementation
// =============================================================================
procedure SHA256_Compress_Scalar(AState, AData: Pointer; ANumBlocks: UInt32);
var
LPState: PCardinal;
LPData: PByte;
LA, LB, LC, LD, LE, LF, LG, LH, LT1, LT2: UInt32;
LW: array [0 .. 63] 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 63 do
begin
LT1 := LW[LRound - 2];
LT2 := LW[LRound - 15];
LW[LRound] := (TBits.RotateRight32(LT1, 17) xor TBits.RotateRight32(LT1, 19)
xor (LT1 shr 10)) + LW[LRound - 7] +
(TBits.RotateRight32(LT2, 7) xor TBits.RotateRight32(LT2, 18)
xor (LT2 shr 3)) + LW[LRound - 16];
end;
LA := LPState[0]; LB := LPState[1]; LC := LPState[2]; LD := LPState[3];
LE := LPState[4]; LF := LPState[5]; LG := LPState[6]; LH := LPState[7];
for LRound := 0 to 63 do
begin
LT1 := LH + (TBits.RotateRight32(LE, 6) xor TBits.RotateRight32(LE, 11)
xor TBits.RotateRight32(LE, 25)) + ((LE and LF) xor (not LE and LG))
+ K256[LRound] + LW[LRound];
LT2 := (TBits.RotateRight32(LA, 2) xor TBits.RotateRight32(LA, 13)
xor TBits.RotateRight32(LA, 22)) +
((LA and LB) xor (LA and LC) xor (LB and LC));
LH := LG; LG := LF; LF := LE; LE := LD + LT1;
LD := LC; LC := LB; LB := LA; LA := LT1 + LT2;
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; LPState[5] := LPState[5] + LF;
LPState[6] := LPState[6] + LG; LPState[7] := LPState[7] + LH;
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 SHA256_Compress_Sse2(AState, AData: Pointer; ANumBlocks: UInt32;
AConstants: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_i386.inc}
{$I ..\Include\Simd\SHA256\SHA256CompressSse2_i386.inc}
end;
procedure SHA256_Compress_Ssse3(AState, AData: Pointer; ANumBlocks: UInt32;
AConstants: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_i386.inc}
{$I ..\Include\Simd\SHA256\SHA256CompressSsse3_i386.inc}
end;
{$ENDIF HASHLIB_I386_ASM}
{$IFDEF HASHLIB_X86_64_ASM}
procedure SHA256_Compress_ShaNi(AState, AData: Pointer; ANumBlocks: UInt32;
AConstants: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_x86_64.inc}
{$I ..\Include\Simd\SHA256\SHA256CompressShaNi_x86_64.inc}
end;
procedure SHA256_Compress_ShaNi_Wrap(AState, AData: Pointer; ANumBlocks: UInt32);
begin
SHA256_Compress_ShaNi(AState, AData, ANumBlocks, @K256);
end;
procedure SHA256_Compress_Sse2(AState, AData: Pointer; ANumBlocks: UInt32;
AConstants: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_x86_64.inc}
{$I ..\Include\Simd\SHA256\SHA256CompressSse2_x86_64.inc}
end;
procedure SHA256_Compress_Ssse3(AState, AData: Pointer; ANumBlocks: UInt32;
AConstants: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_x86_64.inc}
{$I ..\Include\Simd\SHA256\SHA256CompressSsse3_x86_64.inc}
end;
procedure SHA256_Compress_Ssse3_Wrap(AState, AData: Pointer; ANumBlocks: UInt32);
begin
SHA256_Compress_Ssse3(AState, AData, ANumBlocks, @K256);
end;
procedure SHA256_Compress_Avx2(AState, AData: Pointer; ANumBlocks: UInt32;
AConstants: Pointer);
{$I ..\Include\Simd\Common\SimdProc4Begin_x86_64.inc}
{$I ..\Include\Simd\SHA256\SHA256CompressAvx2_x86_64.inc}
end;
procedure SHA256_Compress_Avx2_Wrap(AState, AData: Pointer; ANumBlocks: UInt32);
begin
SHA256_Compress_Avx2(AState, AData, ANumBlocks, @K256);
end;
{$ENDIF HASHLIB_X86_64_ASM}
{$IFDEF HASHLIB_X86_SIMD}
procedure SHA256_Compress_Sse2_Wrap(AState, AData: Pointer; ANumBlocks: UInt32);
begin
SHA256_Compress_Sse2(AState, AData, ANumBlocks, @K256);
end;
{$IFDEF HASHLIB_I386_ASM}
procedure SHA256_Compress_Ssse3_Wrap(AState, AData: Pointer; ANumBlocks: UInt32);
begin
SHA256_Compress_Ssse3(AState, AData, ANumBlocks, @K256);
end;
{$ENDIF HASHLIB_I386_ASM}
{$ENDIF HASHLIB_X86_SIMD}
// =============================================================================
// Dispatch initialization
// =============================================================================
procedure InitDispatch();
begin
SHA256_Compress := @SHA256_Compress_Scalar;
{$IFDEF HASHLIB_I386_ASM}
case TCpuFeatures.X86.GetSimdLevel() of
TX86SimdLevel.SSSE3:
begin
SHA256_Compress := @SHA256_Compress_Ssse3_Wrap;
end;
TX86SimdLevel.SSE2:
begin
SHA256_Compress := @SHA256_Compress_Sse2_Wrap;
end;
end;
{$ENDIF}
{$IFDEF HASHLIB_X86_64_ASM}
if TCpuFeatures.X86.HasSHANI() then
begin
SHA256_Compress := @SHA256_Compress_ShaNi_Wrap;
Exit;
end;
case TCpuFeatures.X86.GetSimdLevel() of
TX86SimdLevel.AVX2:
begin
SHA256_Compress := @SHA256_Compress_Avx2_Wrap;
end;
TX86SimdLevel.SSSE3:
begin
SHA256_Compress := @SHA256_Compress_Ssse3_Wrap;
end;
TX86SimdLevel.SSE2:
begin
SHA256_Compress := @SHA256_Compress_Sse2_Wrap;
end;
end;
{$ENDIF}
end;
initialization
InitDispatch();
end.