-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gost3411-2012-sse41.cpp
317 lines (253 loc) · 9.17 KB
/
gost3411-2012-sse41.cpp
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
#pragma GCC target ("sse4.1,mmx")
#ifndef __MMX__
#define __MMX__ 1
#endif
#ifndef __SSE__
#define __SSE__ 1
#endif
#ifndef __SSE2__
#define __SSE2__ 1
#endif
#ifndef __SSE3__
#define __SSE3__ 1
#endif
#ifndef __SSSE3__
#define __SSSE3__ 1
#endif
#ifndef __SSE4_1__
#define __SSE4_1__ 1
#endif
#include <cstring>
#include <mmintrin.h>
#include <emmintrin.h>
#include <smmintrin.h>
#include "tables.h"
#include "gost3411-2012-sse41.h"
#if defined(__GNUC_PREREQ) && __GNUC_PREREQ(4,8)
# define TARGET_SSE4_1
#else
# define TARGET_SSE4_1 __attribute__((target("sse4.1")))
#endif
static inline void add512(const union uint512_u* x, const union uint512_u* y, union uint512_u* r)
{
uint i, CF;
CF = 0;
for (i=0; i<8; ++i) {
quint64 a = x->QWORD[i];
quint64 b = y->QWORD[i];
quint64 sum = a + b + CF;
CF = ((sum < b) ? 1 : ((sum > b) ? 0 : CF));
r->QWORD[i] = sum;
}
}
#if __i386__
template<unsigned int row>
TARGET_SSE4_1 static inline __m128i extract(__m128i xmm0, __m128i xmm1, __m128i xmm2, __m128i xmm3)
{
__m64 mm0, mm1;
mm0 = _mm_cvtsi64_m64(Ax[0][static_cast<quint8>(_mm_extract_epi8(xmm0, row + 0))]);
mm0 = _mm_xor_64(mm0, Ax[1][static_cast<quint8>(_mm_extract_epi8(xmm0, row + 8))]);
mm0 = _mm_xor_64(mm0, Ax[2][static_cast<quint8>(_mm_extract_epi8(xmm1, row + 0))]);
mm0 = _mm_xor_64(mm0, Ax[3][static_cast<quint8>(_mm_extract_epi8(xmm1, row + 8))]);
mm0 = _mm_xor_64(mm0, Ax[4][static_cast<quint8>(_mm_extract_epi8(xmm2, row + 0))]);
mm0 = _mm_xor_64(mm0, Ax[5][static_cast<quint8>(_mm_extract_epi8(xmm2, row + 8))]);
mm0 = _mm_xor_64(mm0, Ax[6][static_cast<quint8>(_mm_extract_epi8(xmm3, row + 0))]);
mm0 = _mm_xor_64(mm0, Ax[7][static_cast<quint8>(_mm_extract_epi8(xmm3, row + 8))]);
mm1 = _mm_cvtsi64_m64(Ax[0][static_cast<quint8>(_mm_extract_epi8(xmm0, row + 1))]);
mm1 = _mm_xor_64(mm1, Ax[1][static_cast<quint8>(_mm_extract_epi8(xmm0, row + 9))]);
mm1 = _mm_xor_64(mm1, Ax[2][static_cast<quint8>(_mm_extract_epi8(xmm1, row + 1))]);
mm1 = _mm_xor_64(mm1, Ax[3][static_cast<quint8>(_mm_extract_epi8(xmm1, row + 9))]);
mm1 = _mm_xor_64(mm1, Ax[4][static_cast<quint8>(_mm_extract_epi8(xmm2, row + 1))]);
mm1 = _mm_xor_64(mm1, Ax[5][static_cast<quint8>(_mm_extract_epi8(xmm2, row + 9))]);
mm1 = _mm_xor_64(mm1, Ax[6][static_cast<quint8>(_mm_extract_epi8(xmm3, row + 1))]);
mm1 = _mm_xor_64(mm1, Ax[7][static_cast<quint8>(_mm_extract_epi8(xmm3, row + 9))]);
return _mm_set_epi64(mm1, mm0);
}
#else
template<unsigned int row>
TARGET_SSE4_1 static inline __m128i extract(__m128i xmm0, __m128i xmm1, __m128i xmm2, __m128i xmm3)
{
qint64 r0, r1;
r0 = Ax[0][static_cast<quint8>(_mm_extract_epi8(xmm0, row + 0))];
r0 ^= Ax[1][static_cast<quint8>(_mm_extract_epi8(xmm0, row + 8))];
r0 ^= Ax[2][static_cast<quint8>(_mm_extract_epi8(xmm1, row + 0))];
r0 ^= Ax[3][static_cast<quint8>(_mm_extract_epi8(xmm1, row + 8))];
r0 ^= Ax[4][static_cast<quint8>(_mm_extract_epi8(xmm2, row + 0))];
r0 ^= Ax[5][static_cast<quint8>(_mm_extract_epi8(xmm2, row + 8))];
r0 ^= Ax[6][static_cast<quint8>(_mm_extract_epi8(xmm3, row + 0))];
r0 ^= Ax[7][static_cast<quint8>(_mm_extract_epi8(xmm3, row + 8))];
r1 = Ax[0][static_cast<quint8>(_mm_extract_epi8(xmm0, row + 1))];
r1 ^= Ax[1][static_cast<quint8>(_mm_extract_epi8(xmm0, row + 9))];
r1 ^= Ax[2][static_cast<quint8>(_mm_extract_epi8(xmm1, row + 1))];
r1 ^= Ax[3][static_cast<quint8>(_mm_extract_epi8(xmm1, row + 9))];
r1 ^= Ax[4][static_cast<quint8>(_mm_extract_epi8(xmm2, row + 1))];
r1 ^= Ax[5][static_cast<quint8>(_mm_extract_epi8(xmm2, row + 9))];
r1 ^= Ax[6][static_cast<quint8>(_mm_extract_epi8(xmm3, row + 1))];
r1 ^= Ax[7][static_cast<quint8>(_mm_extract_epi8(xmm3, row + 9))];
return _mm_insert_epi64(_mm_cvtsi64_si128(r0), r1, 1);
}
#endif
TARGET_SSE4_1 static inline void g(union uint512_u* h, const union uint512_u* N, const union uint512_u* m)
{
__m128i xmm0, xmm2, xmm4, xmm6;
__m128i xmm1, xmm3, xmm5, xmm7;
__m128i tmm0, tmm1, tmm2, tmm3;
uint i;
const __m128i *pN = reinterpret_cast<const __m128i*>(N);
const __m128i *pM = reinterpret_cast<const __m128i*>(m);
__m128i *pH = reinterpret_cast<__m128i*>(h);
const __m128i* p;
xmm0 = _mm_load_si128(&pN[0]);
xmm2 = _mm_load_si128(&pN[1]);
xmm4 = _mm_load_si128(&pN[2]);
xmm6 = _mm_load_si128(&pN[3]);
/* XLPS128M(h, xmm0, xmm2, xmm4, xmm6); */
xmm0 = _mm_xor_si128(xmm0, _mm_load_si128(&pH[0]));
xmm2 = _mm_xor_si128(xmm2, _mm_load_si128(&pH[1]));
xmm4 = _mm_xor_si128(xmm4, _mm_load_si128(&pH[2]));
xmm6 = _mm_xor_si128(xmm6, _mm_load_si128(&pH[3]));
tmm0 = extract<0>(xmm0, xmm2, xmm4, xmm6);
tmm1 = extract<2>(xmm0, xmm2, xmm4, xmm6);
tmm2 = extract<4>(xmm0, xmm2, xmm4, xmm6);
tmm3 = extract<6>(xmm0, xmm2, xmm4, xmm6);
xmm0 = tmm0;
xmm2 = tmm1;
xmm4 = tmm2;
xmm6 = tmm3;
/**/
xmm1 = _mm_load_si128(&pM[0]);
xmm3 = _mm_load_si128(&pM[1]);
xmm5 = _mm_load_si128(&pM[2]);
xmm7 = _mm_load_si128(&pM[3]);
/* XLPS128R */
xmm1 = _mm_xor_si128(xmm1, xmm0);
xmm3 = _mm_xor_si128(xmm3, xmm2);
xmm5 = _mm_xor_si128(xmm5, xmm4);
xmm7 = _mm_xor_si128(xmm7, xmm6);
tmm0 = extract<0>(xmm1, xmm3, xmm5, xmm7);
tmm1 = extract<2>(xmm1, xmm3, xmm5, xmm7);
tmm2 = extract<4>(xmm1, xmm3, xmm5, xmm7);
tmm3 = extract<6>(xmm1, xmm3, xmm5, xmm7);
xmm1 = tmm0;
xmm3 = tmm1;
xmm5 = tmm2;
xmm7 = tmm3;
/* end of XLPS128R */
for (i=0; i<11; ++i) {
/* XLPS128M(&C[i], xmm0, xmm2, xmm4, xmm6); */
p = reinterpret_cast<const __m128i*>(&C[i]);
xmm0 = _mm_xor_si128(xmm0, _mm_load_si128(&p[0]));
xmm2 = _mm_xor_si128(xmm2, _mm_load_si128(&p[1]));
xmm4 = _mm_xor_si128(xmm4, _mm_load_si128(&p[2]));
xmm6 = _mm_xor_si128(xmm6, _mm_load_si128(&p[3]));
tmm0 = extract<0>(xmm0, xmm2, xmm4, xmm6);
tmm1 = extract<2>(xmm0, xmm2, xmm4, xmm6);
tmm2 = extract<4>(xmm0, xmm2, xmm4, xmm6);
tmm3 = extract<6>(xmm0, xmm2, xmm4, xmm6);
xmm0 = tmm0;
xmm2 = tmm1;
xmm4 = tmm2;
xmm6 = tmm3;
/**/
/* XLPS128R */
xmm1 = _mm_xor_si128(xmm1, xmm0);
xmm3 = _mm_xor_si128(xmm3, xmm2);
xmm5 = _mm_xor_si128(xmm5, xmm4);
xmm7 = _mm_xor_si128(xmm7, xmm6);
tmm0 = extract<0>(xmm1, xmm3, xmm5, xmm7);
tmm1 = extract<2>(xmm1, xmm3, xmm5, xmm7);
tmm2 = extract<4>(xmm1, xmm3, xmm5, xmm7);
tmm3 = extract<6>(xmm1, xmm3, xmm5, xmm7);
xmm1 = tmm0;
xmm3 = tmm1;
xmm5 = tmm2;
xmm7 = tmm3;
/* end of XLPS128R */
}
/*XLPS128M(&C[11], xmm0, xmm2, xmm4, xmm6);*/
p = reinterpret_cast<const __m128i*>(&C[11]);
xmm0 = _mm_xor_si128(xmm0, _mm_load_si128(&p[0]));
xmm2 = _mm_xor_si128(xmm2, _mm_load_si128(&p[1]));
xmm4 = _mm_xor_si128(xmm4, _mm_load_si128(&p[2]));
xmm6 = _mm_xor_si128(xmm6, _mm_load_si128(&p[3]));
tmm0 = extract<0>(xmm0, xmm2, xmm4, xmm6);
tmm1 = extract<2>(xmm0, xmm2, xmm4, xmm6);
tmm2 = extract<4>(xmm0, xmm2, xmm4, xmm6);
tmm3 = extract<6>(xmm0, xmm2, xmm4, xmm6);
xmm0 = tmm0;
xmm2 = tmm1;
xmm4 = tmm2;
xmm6 = tmm3;
xmm0 = _mm_xor_si128(xmm0, xmm1);
xmm2 = _mm_xor_si128(xmm2, xmm3);
xmm4 = _mm_xor_si128(xmm4, xmm5);
xmm6 = _mm_xor_si128(xmm6, xmm7);
xmm0 = _mm_xor_si128(xmm0, _mm_load_si128(&pH[0]));
xmm2 = _mm_xor_si128(xmm2, _mm_load_si128(&pH[1]));
xmm4 = _mm_xor_si128(xmm4, _mm_load_si128(&pH[2]));
xmm6 = _mm_xor_si128(xmm6, _mm_load_si128(&pH[3]));
xmm0 = _mm_xor_si128(xmm0, _mm_load_si128(&pM[0]));
xmm2 = _mm_xor_si128(xmm2, _mm_load_si128(&pM[1]));
xmm4 = _mm_xor_si128(xmm4, _mm_load_si128(&pM[2]));
xmm6 = _mm_xor_si128(xmm6, _mm_load_si128(&pM[3]));
_mm_store_si128(&pH[0], xmm0);
_mm_store_si128(&pH[1], xmm2);
_mm_store_si128(&pH[2], xmm4);
_mm_store_si128(&pH[3], xmm6);
}
static inline void stage2(GOST34112012Context* CTX, const union uint512_u* data)
{
g(&CTX->h, &CTX->N, data);
add512(&CTX->N, &buffer512, &CTX->N);
add512(&CTX->Sigma, data, &CTX->Sigma);
}
void GOST34112012Update_sse41(GOST34112012Context* ctx, const unsigned char* data, std::size_t len)
{
std::size_t chunksize;
const union uint512_u* d = reinterpret_cast<const union uint512_u*>(data);
while (len > 63 && ctx->bufsize == 0) {
stage2(ctx, d);
data += 64;
len -= 64;
}
while (len) {
chunksize = 64 - ctx->bufsize;
if (chunksize > len) {
chunksize = len;
}
std::memcpy(&ctx->buffer.BYTE[ctx->bufsize], data, chunksize);
ctx->bufsize += chunksize;
len -= chunksize;
data += chunksize;
if (ctx->bufsize == 64) {
stage2(ctx, &ctx->buffer);
ctx->bufsize = 0;
}
}
_mm_empty();
}
void GOST34112012Final_sse41(GOST34112012Context* ctx, unsigned char* digest)
{
Q_DECL_ALIGN(16) union uint512_u buf;
std::memset(&ctx->buffer.BYTE[ctx->bufsize], 0, sizeof(ctx->buffer) - ctx->bufsize);
buf.QWORD[0] = ctx->bufsize << 3;
std::memset(buf.BYTE + sizeof(buf.QWORD[0]), 0, sizeof(buf) - sizeof(buf.QWORD[0]));
if (ctx->bufsize <= 63) {
ctx->buffer.BYTE[ctx->bufsize] = 1;
std::memset(ctx->buffer.BYTE + ctx->bufsize + 1, 0, sizeof(ctx->buffer) - ctx->bufsize + 1);
}
g(&ctx->h, &ctx->N, &ctx->buffer);
add512(&ctx->N, &buf, &ctx->N);
add512(&ctx->Sigma, &ctx->buffer, &ctx->Sigma);
g(&ctx->h, &buffer0, &ctx->N);
g(&ctx->h, &buffer0, &ctx->Sigma);
std::memcpy(&ctx->hash, &ctx->h, sizeof(ctx->hash));
if (ctx->digest_size == 256) {
std::memcpy(digest, &(ctx->hash.QWORD[4]), 32);
}
else {
std::memcpy(digest, &(ctx->hash.QWORD[0]), 64);
}
std::memset(ctx, 0, sizeof(GOST34112012Context));
_mm_empty();
}