-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
gost3411-2012-ref.c
207 lines (172 loc) · 5.29 KB
/
gost3411-2012-ref.c
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
#include "gost3411-2012-ref.h"
#include "tables.h"
#ifdef WORDS_BIGENDIAN
static inline uint64_t bswap64(uint64_t x)
{
#ifdef __GNUC__
return __builtin_bswap64(x);
#else
return
((x & 0xFF00000000000000ULL) >> 56) |
((x & 0x00FF000000000000ULL) >> 40) |
((x & 0x0000FF0000000000ULL) >> 24) |
((x & 0x000000FF00000000ULL) >> 8) |
((x & 0x00000000FF000000ULL) << 8) |
((x & 0x0000000000FF0000ULL) << 24) |
((x & 0x000000000000FF00ULL) << 40) |
((x & 0x00000000000000FFULL) << 56)
;
#endif
}
#endif
static void XLPS(const union uint512_u* x, const union uint512_u* y, union uint512_u* data)
{
uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
int_fast8_t i;
r0 = x->QWORD[0] ^ y->QWORD[0];
r1 = x->QWORD[1] ^ y->QWORD[1];
r2 = x->QWORD[2] ^ y->QWORD[2];
r3 = x->QWORD[3] ^ y->QWORD[3];
r4 = x->QWORD[4] ^ y->QWORD[4];
r5 = x->QWORD[5] ^ y->QWORD[5];
r6 = x->QWORD[6] ^ y->QWORD[6];
r7 = x->QWORD[7] ^ y->QWORD[7];
#ifndef WORDS_BIGENDIAN
for (i=0; i<8; ++i) {
data->QWORD[i] = Ax[0][(uint8_t)(r0 >> (i << 3))];
data->QWORD[i] ^= Ax[1][(uint8_t)(r1 >> (i << 3))];
data->QWORD[i] ^= Ax[2][(uint8_t)(r2 >> (i << 3))];
data->QWORD[i] ^= Ax[3][(uint8_t)(r3 >> (i << 3))];
data->QWORD[i] ^= Ax[4][(uint8_t)(r4 >> (i << 3))];
data->QWORD[i] ^= Ax[5][(uint8_t)(r5 >> (i << 3))];
data->QWORD[i] ^= Ax[6][(uint8_t)(r6 >> (i << 3))];
data->QWORD[i] ^= Ax[7][(uint8_t)(r7 >> (i << 3))];
}
#else
for (i=7; i>=0; --i) {
data->QWORD[7-i] = Ax[0][(uint8_t)(r0 >> (i << 3))];
data->QWORD[7-i] ^= Ax[1][(uint8_t)(r1 >> (i << 3))];
data->QWORD[7-i] ^= Ax[2][(uint8_t)(r2 >> (i << 3))];
data->QWORD[7-i] ^= Ax[3][(uint8_t)(r3 >> (i << 3))];
data->QWORD[7-i] ^= Ax[4][(uint8_t)(r4 >> (i << 3))];
data->QWORD[7-i] ^= Ax[5][(uint8_t)(r5 >> (i << 3))];
data->QWORD[7-i] ^= Ax[6][(uint8_t)(r6 >> (i << 3))];
data->QWORD[7-i] ^= Ax[7][(uint8_t)(r7 >> (i << 3))];
}
#endif
}
static inline void X(const union uint512_u* x, const union uint512_u* y, union uint512_u* z)
{
z->QWORD[0] = x->QWORD[0] ^ y->QWORD[0];
z->QWORD[1] = x->QWORD[1] ^ y->QWORD[1];
z->QWORD[2] = x->QWORD[2] ^ y->QWORD[2];
z->QWORD[3] = x->QWORD[3] ^ y->QWORD[3];
z->QWORD[4] = x->QWORD[4] ^ y->QWORD[4];
z->QWORD[5] = x->QWORD[5] ^ y->QWORD[5];
z->QWORD[6] = x->QWORD[6] ^ y->QWORD[6];
z->QWORD[7] = x->QWORD[7] ^ y->QWORD[7];
}
static inline void add512(const union uint512_u* x, const union uint512_u* y, union uint512_u* r)
{
#ifndef WORDS_BIGENDIAN
uint_fast8_t i, CF;
CF = 0;
for (i=0; i<8; ++i) {
uint64_t a = x->QWORD[i];
uint64_t b = y->QWORD[i];
uint64_t sum = a + b + CF;
CF = ((sum < b) ? 1 : ((sum > b) ? 0 : CF));
r->QWORD[i] = sum;
}
#else
const unsigned char *xp, *yp;
unsigned char *rp;
unsigned int i;
int buf;
xp = (const unsigned char*)&x[0];
yp = (const unsigned char*)&y[0];
rp = (unsigned char*)&r[0];
buf = 0;
for (i=0; i<64; ++i) {
buf = xp[i] + yp[i] + (buf >> 8);
rp[i] = (uint8_t)buf;
}
#endif
}
static void g(union uint512_u* restrict h, const union uint512_u* restrict N, const unsigned char* restrict m)
{
ALIGN(16) union uint512_u Ki, data;
uint_fast8_t i;
XLPS(h, N, &data);
/* Starting E() */
Ki = data;
XLPS(&Ki, (const union uint512_u*)&m[0], &data);
for (i=0; i<11; ++i) {
XLPS(&Ki, &C[i], &Ki);
XLPS(&Ki, &data, &data);
}
XLPS(&Ki, &C[11], &Ki);
X(&Ki, &data, &data);
/* E() done */
X(&data, h, &data);
X(&data, (const union uint512_u*)&m[0], h);
}
static inline void stage2(GOST34112012Context* restrict CTX, const unsigned char* restrict data)
{
g(&CTX->h, &CTX->N, data);
add512(&CTX->N, &buffer512, &CTX->N);
add512(&CTX->Sigma, (const union uint512_u*)data, &CTX->Sigma);
}
void GOST34112012Update_ref(void* restrict ctx, const unsigned char* restrict data, size_t len)
{
size_t chunksize;
GOST34112012Context* CTX = (GOST34112012Context*)ctx;
while (len > 63 && CTX->bufsize == 0) {
stage2(CTX, data);
data += 64;
len -= 64;
}
while (len) {
chunksize = 64 - CTX->bufsize;
if (chunksize > len) {
chunksize = len;
}
memcpy(&CTX->buffer[CTX->bufsize], data, chunksize);
CTX->bufsize += chunksize;
len -= chunksize;
data += chunksize;
if (CTX->bufsize == 64) {
stage2(CTX, CTX->buffer);
CTX->bufsize = 0;
}
}
}
void GOST34112012Final_ref(void* restrict ctx, unsigned char* restrict digest)
{
GOST34112012Context* CTX = (GOST34112012Context*)ctx;
ALIGN(16) union uint512_u buf;
memset(&CTX->buffer[CTX->bufsize], 0, sizeof(CTX->buffer) - CTX->bufsize);
#ifndef WORDS_BIGENDIAN
buf.QWORD[0] = CTX->bufsize << 3;
#else
buf.QWORD[0] = bswap64(CTX->bufsize << 3);
#endif
memset(((char*)&buf) + sizeof(buf.QWORD[0]), 0, sizeof(buf) - sizeof(buf.QWORD[0]));
if (CTX->bufsize <= 63) {
CTX->buffer[CTX->bufsize] = 1;
memset(CTX->buffer + CTX->bufsize + 1, 0, sizeof(CTX->buffer) - CTX->bufsize + 1);
}
g(&CTX->h, &CTX->N, (const unsigned char*)&CTX->buffer);
add512(&CTX->N, &buf, &CTX->N);
add512(&CTX->Sigma, (const union uint512_u*)&CTX->buffer[0], &CTX->Sigma);
g(&CTX->h, &buffer0, (const unsigned char*)&CTX->N);
g(&CTX->h, &buffer0, (const unsigned char*)&CTX->Sigma);
memcpy(&CTX->hash, &CTX->h, sizeof(CTX->hash));
if (CTX->digest_size == 256) {
memcpy(digest, &(CTX->hash.QWORD[4]), 32);
}
else {
memcpy(digest, &(CTX->hash.QWORD[0]), 64);
}
memset(CTX, 0, sizeof(GOST34112012Context));
}