-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhash.c
178 lines (138 loc) · 3.81 KB
/
dhash.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
#include "core.h"
#include <string.h>
#ifdef D_STREEBOG
#include <immintrin.h>
#if defined(__GNUC__)
#include <x86intrin.h>
#endif
#include "gost3411-2012-const.h"
#include "gost3411-2012-precalc.h"
#include "gost3411-2012-sse41.h"
static inline void
pad(GOST34112012Context *CTX)
{
if (CTX->bufsize > 63)
return;
memset(CTX->buffer + CTX->bufsize,
0x00, sizeof(CTX->buffer) - CTX->bufsize);
CTX->buffer[CTX->bufsize] = 0x01;
}
static inline void
add512(u512_t* x, const u512_t* y)
{
unsigned long cf = 0;
unsigned int i;
unsigned long long tmp;
for (i = 0; i < 8; i++)
{
tmp = x->QWORD[i] + y->QWORD[i] + cf;
if(tmp!=x->QWORD[i])
cf = (tmp < x->QWORD[i] )?1:0;
x->QWORD[i] = tmp;
}
}
static inline void g(u512_t *h, const u512_t *N, const unsigned char *m) {
__m128i xmm0, xmm1, xmm2, xmm3;
__m128i xmm4, xmm5, xmm6, xmm7;
register unsigned long long r0, r1;
//key
LOAD_XMM(h, xmm0, xmm1, xmm2, xmm3);
//into_xor_m ( n )
XOR_XMM((*N), xmm0, xmm1, xmm2, xmm3);
LPS(xmm0, xmm1, xmm2, xmm3);
//buffer
LOADU_XMM(m, xmm4, xmm5, xmm6, xmm7);
for (int i = 0; i < 12; i++) {
//buffer.into_xor(key)
XOR_XMM2(xmm4, xmm5, xmm6, xmm7, xmm0, xmm1, xmm2, xmm3);
//buffer.lps()
LPS(xmm4, xmm5, xmm6, xmm7);
//key.into_xor( c[i] )
XOR_XMM(C[i], xmm0, xmm1, xmm2, xmm3);
//key.lps()
LPS(xmm0, xmm1, xmm2, xmm3);
}
//key.xor_r( buffer )
XOR_XMM2(xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7 );
//use ymm2 ymm3 as buffer is not need anymore
LOADU_XMM(m, xmm4, xmm5, xmm6, xmm7);
//key.xor_m ( m )
XOR_XMM2(xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7);
//key.xor_m ( h )
XOR_XMM( (*h), xmm0, xmm1, xmm2, xmm3);
STORE_XMM(h, xmm0, xmm1, xmm2, xmm3);
}
static inline void
stage2(GOST34112012Context *CTX, const unsigned char *data)
{
g(&(CTX->h), &(CTX->N), data );
add512(&(CTX->N), &buffer512);
add512(&(CTX->Sigma), (const u512_t *)data);
}
static void
stage3(GOST34112012Context *CTX)
{
ALIGNED u512_t buf = {{ 0 }};
buf.QWORD[0] = CTX->bufsize << 3;
pad(CTX);
g(&(CTX->h), &(CTX->N), (const unsigned char *) &(CTX->buffer));
add512(&(CTX->N), &buf);
add512(&(CTX->Sigma), (const u512_t *) &CTX->buffer[0]);
g(&(CTX->h), &buffer0, (const unsigned char *) &(CTX->N));
g(&(CTX->h), &buffer0, (const unsigned char *) &(CTX->Sigma));
}
void
GOST34112012Cleanup(GOST34112012Context *CTX)
{
memset(CTX, 0x00, sizeof (GOST34112012Context));
}
void
GOST34112012Init(GOST34112012Context *CTX, const unsigned int digest_size)
{
unsigned int i;
memset(CTX, 0x00, sizeof(GOST34112012Context));
CTX->digest_size = digest_size;
if (digest_size == 32) {
memset(&(CTX->h), 0x01, 64 );
}
}
void
GOST34112012Update(GOST34112012Context *CTX, const unsigned char *data, size_t len)
{
size_t chunksize;
if (CTX->bufsize) {
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;
}
}
while (len > 63)
{
stage2(CTX, data);
data += 64;
len -= 64;
}
if (len) {
memcpy(&CTX->buffer, data, len);
}
CTX->bufsize = len;
}
void
GOST34112012Final(GOST34112012Context *CTX, unsigned char *digest)
{
stage3(CTX);
CTX->bufsize = 0;
if (CTX->digest_size == 32)
memcpy(digest, &(CTX->h.QWORD[4]), 32);
else
memcpy(digest, &(CTX->h.QWORD[0]), 64);
}
#endif