forked from gost-engine/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosthash2012.c
230 lines (182 loc) · 5.47 KB
/
gosthash2012.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* GOST R 34.11-2012 core functions.
*
* Copyright (c) 2013 Cryptocom LTD.
* This file is distributed under the same license as OpenSSL.
*
* Author: Alexey Degtyarev <[email protected]>
*
*/
#include "gosthash2012.h"
#if defined(_WIN32) || defined(_WINDOWS)
# define INLINE __inline
#else
# define INLINE inline
#endif
#define BSWAP64(x) \
(((x & 0xFF00000000000000ULL) >> 56) | \
((x & 0x00FF000000000000ULL) >> 40) | \
((x & 0x0000FF0000000000ULL) >> 24) | \
((x & 0x000000FF00000000ULL) >> 8) | \
((x & 0x00000000FF000000ULL) << 8) | \
((x & 0x0000000000FF0000ULL) << 24) | \
((x & 0x000000000000FF00ULL) << 40) | \
((x & 0x00000000000000FFULL) << 56))
/*
* Initialize gost2012 hash context structure
*/
void init_gost2012_hash_ctx(gost2012_hash_ctx * CTX,
const unsigned int digest_size)
{
memset(CTX, 0, sizeof(gost2012_hash_ctx));
CTX->digest_size = digest_size;
if (digest_size == 256)
memset(&CTX->h, 0x01, sizeof(uint512_u));
else
memset(&CTX->h, 0x00, sizeof(uint512_u));
}
static INLINE void pad(gost2012_hash_ctx * CTX)
{
unsigned char buf[64];
if (CTX->bufsize > 63)
return;
memset(&buf, 0x00, sizeof buf);
memcpy(&buf, CTX->buffer, CTX->bufsize);
buf[CTX->bufsize] = 0x01;
memcpy(CTX->buffer, &buf, sizeof buf);
}
static INLINE void add512(const union uint512_u *x,
const union uint512_u *y, union uint512_u *r)
{
#ifndef __GOST3411_BIG_ENDIAN__
unsigned int CF, OF;
unsigned int i;
CF = 0;
for (i = 0; i < 8; i++) {
r->QWORD[i] = x->QWORD[i] + y->QWORD[i];
if (r->QWORD[i] < y->QWORD[i] || r->QWORD[i] < x->QWORD[i])
OF = 1;
else
OF = 0;
r->QWORD[i] += CF;
CF = OF;
}
#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] = (unsigned char)buf & 0xFF;
}
#endif
}
static void g(union uint512_u *h, const union uint512_u *N,
const unsigned char *m)
{
#ifdef __GOST3411_HAS_SSE2__
__m128i xmm0, xmm2, xmm4, xmm6; /* XMMR0-quadruple */
__m128i xmm1, xmm3, xmm5, xmm7; /* XMMR1-quadruple */
unsigned int i;
LOAD(N, xmm0, xmm2, xmm4, xmm6);
XLPS128M(h, xmm0, xmm2, xmm4, xmm6);
LOAD(m, xmm1, xmm3, xmm5, xmm7);
XLPS128R(xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
for (i = 0; i < 11; i++)
ROUND128(i, xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
XLPS128M((&C[11]), xmm0, xmm2, xmm4, xmm6);
X128R(xmm0, xmm2, xmm4, xmm6, xmm1, xmm3, xmm5, xmm7);
X128M(h, xmm0, xmm2, xmm4, xmm6);
X128M(m, xmm0, xmm2, xmm4, xmm6);
UNLOAD(h, xmm0, xmm2, xmm4, xmm6);
/* Restore the Floating-point status on the CPU */
_mm_empty();
#else
union uint512_u Ki, data;
unsigned int i;
XLPS(h, N, (&data));
/* Starting E() */
Ki = data;
XLPS((&Ki), ((const union uint512_u *)&m[0]), (&data));
for (i = 0; i < 11; i++)
ROUND(i, (&Ki), (&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);
#endif
}
static INLINE void stage2(gost2012_hash_ctx * CTX, const unsigned char *data)
{
g(&(CTX->h), &(CTX->N), data);
add512(&(CTX->N), &buffer512, &(CTX->N));
add512(&(CTX->Sigma), (const union uint512_u *)data, &(CTX->Sigma));
}
static INLINE void stage3(gost2012_hash_ctx * CTX)
{
ALIGN(16) union uint512_u buf;
memset(&buf, 0x00, sizeof buf);
memcpy(&buf, &(CTX->buffer), CTX->bufsize);
memcpy(&(CTX->buffer), &buf, sizeof(uint512_u));
memset(&buf, 0x00, sizeof buf);
#ifndef __GOST3411_BIG_ENDIAN__
buf.QWORD[0] = CTX->bufsize << 3;
#else
buf.QWORD[0] = BSWAP64(CTX->bufsize << 3);
#endif
pad(CTX);
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(uint512_u));
}
/*
* Hash block of arbitrary length
*
*/
void gost2012_hash_block(gost2012_hash_ctx * CTX,
const unsigned char *data, size_t len)
{
size_t chunksize;
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;
}
}
}
/*
* Compute hash value from current state of ctx
* state of hash ctx becomes invalid and cannot be used for further
* hashing.
*/
void gost2012_finish_hash(gost2012_hash_ctx * CTX, unsigned char *digest)
{
stage3(CTX);
CTX->bufsize = 0;
if (CTX->digest_size == 256)
memcpy(digest, &(CTX->hash.QWORD[4]), 32);
else
memcpy(digest, &(CTX->hash.QWORD[0]), 64);
}