-
Notifications
You must be signed in to change notification settings - Fork 0
/
chash.c
169 lines (131 loc) · 3.64 KB
/
chash.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
#include "core.h"
#include <string.h>
#ifdef C_STREEBOG
#include <immintrin.h>
#if defined(__GNUC__)
#include <x86intrin.h>
#endif
#include "gost3411-2012-const.h"
#include "gost3411-2012-precalc.h"
#include "gost3411-2012-avx.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 char cf = 0;
unsigned int i;
for (i = 0; i < 8; i++)
{
cf = _addcarry_u64(cf, x->QWORD[i], y->QWORD[i], &(x->QWORD[i]));
}
}
static inline void g(u512_t *h, const u512_t *N, const unsigned char *m) {
__m256i ymm0, ymm1;
__m256i ymm2, ymm3;
register unsigned long long r0, r1, r2, r3;
//key
LOAD_YMM(h, ymm0, ymm1);
//into_xor_m ( n )
XOR_YMM((*N), ymm0, ymm1);
YMM_LPS(ymm0, ymm1);
//buffer
LOADU_YMM(m, ymm2, ymm3);
for (int i = 0; i < 12; i++) {
XOR_YMM2(ymm2, ymm3, ymm0, ymm1);
//buffer.lps()
YMM_LPS(ymm2, ymm3);
//key.into_xor( c[i] )
XOR_YMM(C[i], ymm0, ymm1);
//key.lps()
YMM_LPS(ymm0, ymm1);
}
//key.xor_r( buffer )
XOR_YMM2(ymm0, ymm1, ymm2, ymm3 );
//use ymm2 ymm3 as buffer is not need anymore
LOADU_YMM(m, ymm2, ymm3);
//key.xor_m ( m )
XOR_YMM2(ymm0, ymm1, ymm2, ymm3);
//key.xor_m ( h )
XOR_YMM( (*h), ymm0, ymm1);
STORE_YMM(h, ymm0, ymm1);
}
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;
}
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