| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
/* |
|
2
|
|
|
|
|
|
|
* Argon2 reference source code package - reference C implementations |
|
3
|
|
|
|
|
|
|
* |
|
4
|
|
|
|
|
|
|
* Copyright 2015 |
|
5
|
|
|
|
|
|
|
* Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves |
|
6
|
|
|
|
|
|
|
* |
|
7
|
|
|
|
|
|
|
* You may use this work under the terms of a Creative Commons CC0 1.0 |
|
8
|
|
|
|
|
|
|
* License/Waiver or the Apache Public License 2.0, at your option. The terms of |
|
9
|
|
|
|
|
|
|
* these licenses can be found at: |
|
10
|
|
|
|
|
|
|
* |
|
11
|
|
|
|
|
|
|
* - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0 |
|
12
|
|
|
|
|
|
|
* - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
|
|
|
|
* |
|
14
|
|
|
|
|
|
|
* You should have received a copy of both of these licenses along with this |
|
15
|
|
|
|
|
|
|
* software. If not, they may be obtained at the above URLs. |
|
16
|
|
|
|
|
|
|
*/ |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#include |
|
19
|
|
|
|
|
|
|
#include |
|
20
|
|
|
|
|
|
|
#include |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
#include "blake2.h" |
|
23
|
|
|
|
|
|
|
#include "blake2-impl.h" |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
static const uint64_t blake2b_IV[8] = { |
|
26
|
|
|
|
|
|
|
UINT64_C(0x6a09e667f3bcc908), UINT64_C(0xbb67ae8584caa73b), |
|
27
|
|
|
|
|
|
|
UINT64_C(0x3c6ef372fe94f82b), UINT64_C(0xa54ff53a5f1d36f1), |
|
28
|
|
|
|
|
|
|
UINT64_C(0x510e527fade682d1), UINT64_C(0x9b05688c2b3e6c1f), |
|
29
|
|
|
|
|
|
|
UINT64_C(0x1f83d9abfb41bd6b), UINT64_C(0x5be0cd19137e2179)}; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
static const unsigned int blake2b_sigma[12][16] = { |
|
32
|
|
|
|
|
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, |
|
33
|
|
|
|
|
|
|
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3}, |
|
34
|
|
|
|
|
|
|
{11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4}, |
|
35
|
|
|
|
|
|
|
{7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8}, |
|
36
|
|
|
|
|
|
|
{9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13}, |
|
37
|
|
|
|
|
|
|
{2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9}, |
|
38
|
|
|
|
|
|
|
{12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11}, |
|
39
|
|
|
|
|
|
|
{13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10}, |
|
40
|
|
|
|
|
|
|
{6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5}, |
|
41
|
|
|
|
|
|
|
{10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0}, |
|
42
|
|
|
|
|
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, |
|
43
|
|
|
|
|
|
|
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3}, |
|
44
|
|
|
|
|
|
|
}; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
static BLAKE2_INLINE void blake2b_set_lastnode(blake2b_state *S) { |
|
47
|
0
|
|
|
|
|
|
S->f[1] = (uint64_t)-1; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
static BLAKE2_INLINE void blake2b_set_lastblock(blake2b_state *S) { |
|
51
|
1978
|
|
|
|
|
|
if (S->last_node) { |
|
52
|
|
|
|
|
|
|
blake2b_set_lastnode(S); |
|
53
|
|
|
|
|
|
|
} |
|
54
|
1978
|
|
|
|
|
|
S->f[0] = (uint64_t)-1; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
static BLAKE2_INLINE void blake2b_increment_counter(blake2b_state *S, |
|
58
|
|
|
|
|
|
|
uint64_t inc) { |
|
59
|
2202
|
|
|
|
|
|
S->t[0] += inc; |
|
60
|
2202
|
|
|
|
|
|
S->t[1] += (S->t[0] < inc); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
static BLAKE2_INLINE void blake2b_invalidate_state(blake2b_state *S) { |
|
64
|
0
|
|
|
|
|
|
clear_internal_memory(S, sizeof(*S)); /* wipe */ |
|
65
|
|
|
|
|
|
|
blake2b_set_lastblock(S); /* invalidate for further use */ |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
|
|
68
|
1978
|
|
|
|
|
|
static BLAKE2_INLINE void blake2b_init0(blake2b_state *S) { |
|
69
|
|
|
|
|
|
|
memset(S, 0, sizeof(*S)); |
|
70
|
1978
|
|
|
|
|
|
memcpy(S->h, blake2b_IV, sizeof(S->h)); |
|
71
|
1978
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
1978
|
|
|
|
|
|
int blake2b_init_param(blake2b_state *S, const blake2b_param *P) { |
|
74
|
|
|
|
|
|
|
const unsigned char *p = (const unsigned char *)P; |
|
75
|
|
|
|
|
|
|
unsigned int i; |
|
76
|
|
|
|
|
|
|
|
|
77
|
1978
|
50
|
|
|
|
|
if (NULL == P || NULL == S) { |
|
78
|
|
|
|
|
|
|
return -1; |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
1978
|
|
|
|
|
|
blake2b_init0(S); |
|
82
|
|
|
|
|
|
|
/* IV XOR Parameter Block */ |
|
83
|
17802
|
100
|
|
|
|
|
for (i = 0; i < 8; ++i) { |
|
84
|
15824
|
|
|
|
|
|
S->h[i] ^= load64(&p[i * sizeof(S->h[i])]); |
|
85
|
|
|
|
|
|
|
} |
|
86
|
1978
|
|
|
|
|
|
S->outlen = P->digest_length; |
|
87
|
1978
|
|
|
|
|
|
return 0; |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
/* Sequential blake2b initialization */ |
|
91
|
1978
|
|
|
|
|
|
int blake2b_init(blake2b_state *S, size_t outlen) { |
|
92
|
|
|
|
|
|
|
blake2b_param P; |
|
93
|
|
|
|
|
|
|
|
|
94
|
1978
|
50
|
|
|
|
|
if (S == NULL) { |
|
95
|
|
|
|
|
|
|
return -1; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
1978
|
50
|
|
|
|
|
if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) { |
|
99
|
|
|
|
|
|
|
blake2b_invalidate_state(S); |
|
100
|
0
|
|
|
|
|
|
return -1; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
/* Setup Parameter Block for unkeyed BLAKE2 */ |
|
104
|
1978
|
|
|
|
|
|
P.digest_length = (uint8_t)outlen; |
|
105
|
1978
|
|
|
|
|
|
P.key_length = 0; |
|
106
|
1978
|
|
|
|
|
|
P.fanout = 1; |
|
107
|
1978
|
|
|
|
|
|
P.depth = 1; |
|
108
|
1978
|
|
|
|
|
|
P.leaf_length = 0; |
|
109
|
1978
|
|
|
|
|
|
P.node_offset = 0; |
|
110
|
1978
|
|
|
|
|
|
P.node_depth = 0; |
|
111
|
1978
|
|
|
|
|
|
P.inner_length = 0; |
|
112
|
|
|
|
|
|
|
memset(P.reserved, 0, sizeof(P.reserved)); |
|
113
|
|
|
|
|
|
|
memset(P.salt, 0, sizeof(P.salt)); |
|
114
|
|
|
|
|
|
|
memset(P.personal, 0, sizeof(P.personal)); |
|
115
|
|
|
|
|
|
|
|
|
116
|
1978
|
|
|
|
|
|
return blake2b_init_param(S, &P); |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key, |
|
120
|
|
|
|
|
|
|
size_t keylen) { |
|
121
|
|
|
|
|
|
|
blake2b_param P; |
|
122
|
|
|
|
|
|
|
|
|
123
|
0
|
0
|
|
|
|
|
if (S == NULL) { |
|
124
|
|
|
|
|
|
|
return -1; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
0
|
0
|
|
|
|
|
if ((outlen == 0) || (outlen > BLAKE2B_OUTBYTES)) { |
|
128
|
|
|
|
|
|
|
blake2b_invalidate_state(S); |
|
129
|
0
|
|
|
|
|
|
return -1; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
0
|
0
|
|
|
|
|
if ((key == 0) || (keylen == 0) || (keylen > BLAKE2B_KEYBYTES)) { |
|
|
|
0
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
blake2b_invalidate_state(S); |
|
134
|
0
|
|
|
|
|
|
return -1; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
/* Setup Parameter Block for keyed BLAKE2 */ |
|
138
|
0
|
|
|
|
|
|
P.digest_length = (uint8_t)outlen; |
|
139
|
0
|
|
|
|
|
|
P.key_length = (uint8_t)keylen; |
|
140
|
0
|
|
|
|
|
|
P.fanout = 1; |
|
141
|
0
|
|
|
|
|
|
P.depth = 1; |
|
142
|
0
|
|
|
|
|
|
P.leaf_length = 0; |
|
143
|
0
|
|
|
|
|
|
P.node_offset = 0; |
|
144
|
0
|
|
|
|
|
|
P.node_depth = 0; |
|
145
|
0
|
|
|
|
|
|
P.inner_length = 0; |
|
146
|
|
|
|
|
|
|
memset(P.reserved, 0, sizeof(P.reserved)); |
|
147
|
|
|
|
|
|
|
memset(P.salt, 0, sizeof(P.salt)); |
|
148
|
|
|
|
|
|
|
memset(P.personal, 0, sizeof(P.personal)); |
|
149
|
|
|
|
|
|
|
|
|
150
|
0
|
0
|
|
|
|
|
if (blake2b_init_param(S, &P) < 0) { |
|
151
|
|
|
|
|
|
|
blake2b_invalidate_state(S); |
|
152
|
0
|
|
|
|
|
|
return -1; |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
{ |
|
156
|
|
|
|
|
|
|
uint8_t block[BLAKE2B_BLOCKBYTES]; |
|
157
|
|
|
|
|
|
|
memset(block, 0, BLAKE2B_BLOCKBYTES); |
|
158
|
|
|
|
|
|
|
memcpy(block, key, keylen); |
|
159
|
0
|
|
|
|
|
|
blake2b_update(S, block, BLAKE2B_BLOCKBYTES); |
|
160
|
|
|
|
|
|
|
/* Burn the key from stack */ |
|
161
|
0
|
|
|
|
|
|
clear_internal_memory(block, BLAKE2B_BLOCKBYTES); |
|
162
|
|
|
|
|
|
|
} |
|
163
|
0
|
|
|
|
|
|
return 0; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
2202
|
|
|
|
|
|
static void blake2b_compress(blake2b_state *S, const uint8_t *block) { |
|
167
|
|
|
|
|
|
|
uint64_t m[16]; |
|
168
|
|
|
|
|
|
|
uint64_t v[16]; |
|
169
|
|
|
|
|
|
|
unsigned int i, r; |
|
170
|
|
|
|
|
|
|
|
|
171
|
37434
|
100
|
|
|
|
|
for (i = 0; i < 16; ++i) { |
|
172
|
35232
|
|
|
|
|
|
m[i] = load64(block + i * sizeof(m[i])); |
|
173
|
|
|
|
|
|
|
} |
|
174
|
|
|
|
|
|
|
|
|
175
|
19818
|
100
|
|
|
|
|
for (i = 0; i < 8; ++i) { |
|
176
|
17616
|
|
|
|
|
|
v[i] = S->h[i]; |
|
177
|
|
|
|
|
|
|
} |
|
178
|
|
|
|
|
|
|
|
|
179
|
2202
|
|
|
|
|
|
v[8] = blake2b_IV[0]; |
|
180
|
2202
|
|
|
|
|
|
v[9] = blake2b_IV[1]; |
|
181
|
2202
|
|
|
|
|
|
v[10] = blake2b_IV[2]; |
|
182
|
2202
|
|
|
|
|
|
v[11] = blake2b_IV[3]; |
|
183
|
2202
|
|
|
|
|
|
v[12] = blake2b_IV[4] ^ S->t[0]; |
|
184
|
2202
|
|
|
|
|
|
v[13] = blake2b_IV[5] ^ S->t[1]; |
|
185
|
2202
|
|
|
|
|
|
v[14] = blake2b_IV[6] ^ S->f[0]; |
|
186
|
2202
|
|
|
|
|
|
v[15] = blake2b_IV[7] ^ S->f[1]; |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
#define G(r, i, a, b, c, d) \ |
|
189
|
|
|
|
|
|
|
do { \ |
|
190
|
|
|
|
|
|
|
a = a + b + m[blake2b_sigma[r][2 * i + 0]]; \ |
|
191
|
|
|
|
|
|
|
d = rotr64(d ^ a, 32); \ |
|
192
|
|
|
|
|
|
|
c = c + d; \ |
|
193
|
|
|
|
|
|
|
b = rotr64(b ^ c, 24); \ |
|
194
|
|
|
|
|
|
|
a = a + b + m[blake2b_sigma[r][2 * i + 1]]; \ |
|
195
|
|
|
|
|
|
|
d = rotr64(d ^ a, 16); \ |
|
196
|
|
|
|
|
|
|
c = c + d; \ |
|
197
|
|
|
|
|
|
|
b = rotr64(b ^ c, 63); \ |
|
198
|
|
|
|
|
|
|
} while ((void)0, 0) |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
#define ROUND(r) \ |
|
201
|
|
|
|
|
|
|
do { \ |
|
202
|
|
|
|
|
|
|
G(r, 0, v[0], v[4], v[8], v[12]); \ |
|
203
|
|
|
|
|
|
|
G(r, 1, v[1], v[5], v[9], v[13]); \ |
|
204
|
|
|
|
|
|
|
G(r, 2, v[2], v[6], v[10], v[14]); \ |
|
205
|
|
|
|
|
|
|
G(r, 3, v[3], v[7], v[11], v[15]); \ |
|
206
|
|
|
|
|
|
|
G(r, 4, v[0], v[5], v[10], v[15]); \ |
|
207
|
|
|
|
|
|
|
G(r, 5, v[1], v[6], v[11], v[12]); \ |
|
208
|
|
|
|
|
|
|
G(r, 6, v[2], v[7], v[8], v[13]); \ |
|
209
|
|
|
|
|
|
|
G(r, 7, v[3], v[4], v[9], v[14]); \ |
|
210
|
|
|
|
|
|
|
} while ((void)0, 0) |
|
211
|
|
|
|
|
|
|
|
|
212
|
28626
|
100
|
|
|
|
|
for (r = 0; r < 12; ++r) { |
|
213
|
871992
|
|
|
|
|
|
ROUND(r); |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
|
|
216
|
19818
|
100
|
|
|
|
|
for (i = 0; i < 8; ++i) { |
|
217
|
17616
|
|
|
|
|
|
S->h[i] = S->h[i] ^ v[i] ^ v[i + 8]; |
|
218
|
|
|
|
|
|
|
} |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
#undef G |
|
221
|
|
|
|
|
|
|
#undef ROUND |
|
222
|
2202
|
|
|
|
|
|
} |
|
223
|
|
|
|
|
|
|
|
|
224
|
2376
|
|
|
|
|
|
int blake2b_update(blake2b_state *S, const void *in, size_t inlen) { |
|
225
|
|
|
|
|
|
|
const uint8_t *pin = (const uint8_t *)in; |
|
226
|
|
|
|
|
|
|
|
|
227
|
2376
|
50
|
|
|
|
|
if (inlen == 0) { |
|
228
|
|
|
|
|
|
|
return 0; |
|
229
|
|
|
|
|
|
|
} |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
/* Sanity check */ |
|
232
|
2376
|
50
|
|
|
|
|
if (S == NULL || in == NULL) { |
|
233
|
|
|
|
|
|
|
return -1; |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
/* Is this a reused state? */ |
|
237
|
2376
|
50
|
|
|
|
|
if (S->f[0] != 0) { |
|
238
|
|
|
|
|
|
|
return -1; |
|
239
|
|
|
|
|
|
|
} |
|
240
|
|
|
|
|
|
|
|
|
241
|
2376
|
100
|
|
|
|
|
if (S->buflen + inlen > BLAKE2B_BLOCKBYTES) { |
|
242
|
|
|
|
|
|
|
/* Complete current block */ |
|
243
|
|
|
|
|
|
|
size_t left = S->buflen; |
|
244
|
28
|
|
|
|
|
|
size_t fill = BLAKE2B_BLOCKBYTES - left; |
|
245
|
28
|
|
|
|
|
|
memcpy(&S->buf[left], pin, fill); |
|
246
|
|
|
|
|
|
|
blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES); |
|
247
|
28
|
|
|
|
|
|
blake2b_compress(S, S->buf); |
|
248
|
28
|
|
|
|
|
|
S->buflen = 0; |
|
249
|
28
|
|
|
|
|
|
inlen -= fill; |
|
250
|
28
|
|
|
|
|
|
pin += fill; |
|
251
|
|
|
|
|
|
|
/* Avoid buffer copies when possible */ |
|
252
|
224
|
100
|
|
|
|
|
while (inlen > BLAKE2B_BLOCKBYTES) { |
|
253
|
|
|
|
|
|
|
blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES); |
|
254
|
196
|
|
|
|
|
|
blake2b_compress(S, pin); |
|
255
|
196
|
|
|
|
|
|
inlen -= BLAKE2B_BLOCKBYTES; |
|
256
|
196
|
|
|
|
|
|
pin += BLAKE2B_BLOCKBYTES; |
|
257
|
|
|
|
|
|
|
} |
|
258
|
|
|
|
|
|
|
} |
|
259
|
2376
|
|
|
|
|
|
memcpy(&S->buf[S->buflen], pin, inlen); |
|
260
|
2376
|
|
|
|
|
|
S->buflen += (unsigned int)inlen; |
|
261
|
2376
|
|
|
|
|
|
return 0; |
|
262
|
|
|
|
|
|
|
} |
|
263
|
|
|
|
|
|
|
|
|
264
|
1978
|
|
|
|
|
|
int blake2b_final(blake2b_state *S, void *out, size_t outlen) { |
|
265
|
1978
|
|
|
|
|
|
uint8_t buffer[BLAKE2B_OUTBYTES] = {0}; |
|
266
|
|
|
|
|
|
|
unsigned int i; |
|
267
|
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
/* Sanity checks */ |
|
269
|
1978
|
50
|
|
|
|
|
if (S == NULL || out == NULL || outlen < S->outlen) { |
|
|
|
50
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
return -1; |
|
271
|
|
|
|
|
|
|
} |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
/* Is this a reused state? */ |
|
274
|
1978
|
50
|
|
|
|
|
if (S->f[0] != 0) { |
|
275
|
|
|
|
|
|
|
return -1; |
|
276
|
|
|
|
|
|
|
} |
|
277
|
|
|
|
|
|
|
|
|
278
|
1978
|
|
|
|
|
|
blake2b_increment_counter(S, S->buflen); |
|
279
|
|
|
|
|
|
|
blake2b_set_lastblock(S); |
|
280
|
1978
|
|
|
|
|
|
memset(&S->buf[S->buflen], 0, BLAKE2B_BLOCKBYTES - S->buflen); /* Padding */ |
|
281
|
1978
|
|
|
|
|
|
blake2b_compress(S, S->buf); |
|
282
|
|
|
|
|
|
|
|
|
283
|
17802
|
100
|
|
|
|
|
for (i = 0; i < 8; ++i) { /* Output full hash to temp buffer */ |
|
284
|
15824
|
|
|
|
|
|
store64(buffer + sizeof(S->h[i]) * i, S->h[i]); |
|
285
|
|
|
|
|
|
|
} |
|
286
|
|
|
|
|
|
|
|
|
287
|
1978
|
|
|
|
|
|
memcpy(out, buffer, S->outlen); |
|
288
|
1978
|
|
|
|
|
|
clear_internal_memory(buffer, sizeof(buffer)); |
|
289
|
1978
|
|
|
|
|
|
clear_internal_memory(S->buf, sizeof(S->buf)); |
|
290
|
1978
|
|
|
|
|
|
clear_internal_memory(S->h, sizeof(S->h)); |
|
291
|
1978
|
|
|
|
|
|
return 0; |
|
292
|
|
|
|
|
|
|
} |
|
293
|
|
|
|
|
|
|
|
|
294
|
1860
|
|
|
|
|
|
int blake2b(void *out, size_t outlen, const void *in, size_t inlen, |
|
295
|
|
|
|
|
|
|
const void *key, size_t keylen) { |
|
296
|
|
|
|
|
|
|
blake2b_state S; |
|
297
|
|
|
|
|
|
|
int ret = -1; |
|
298
|
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
/* Verify parameters */ |
|
300
|
1860
|
50
|
|
|
|
|
if (NULL == in && inlen > 0) { |
|
301
|
|
|
|
|
|
|
goto fail; |
|
302
|
|
|
|
|
|
|
} |
|
303
|
|
|
|
|
|
|
|
|
304
|
1860
|
50
|
|
|
|
|
if (NULL == out || outlen == 0 || outlen > BLAKE2B_OUTBYTES) { |
|
|
|
50
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
goto fail; |
|
306
|
|
|
|
|
|
|
} |
|
307
|
|
|
|
|
|
|
|
|
308
|
1860
|
50
|
|
|
|
|
if ((NULL == key && keylen > 0) || keylen > BLAKE2B_KEYBYTES) { |
|
|
|
50
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
goto fail; |
|
310
|
|
|
|
|
|
|
} |
|
311
|
|
|
|
|
|
|
|
|
312
|
1860
|
50
|
|
|
|
|
if (keylen > 0) { |
|
313
|
0
|
0
|
|
|
|
|
if (blake2b_init_key(&S, outlen, key, keylen) < 0) { |
|
314
|
|
|
|
|
|
|
goto fail; |
|
315
|
|
|
|
|
|
|
} |
|
316
|
|
|
|
|
|
|
} else { |
|
317
|
1860
|
50
|
|
|
|
|
if (blake2b_init(&S, outlen) < 0) { |
|
318
|
|
|
|
|
|
|
goto fail; |
|
319
|
|
|
|
|
|
|
} |
|
320
|
|
|
|
|
|
|
} |
|
321
|
|
|
|
|
|
|
|
|
322
|
1860
|
50
|
|
|
|
|
if (blake2b_update(&S, in, inlen) < 0) { |
|
323
|
|
|
|
|
|
|
goto fail; |
|
324
|
|
|
|
|
|
|
} |
|
325
|
1860
|
|
|
|
|
|
ret = blake2b_final(&S, out, outlen); |
|
326
|
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
fail: |
|
328
|
1860
|
|
|
|
|
|
clear_internal_memory(&S, sizeof(S)); |
|
329
|
1860
|
|
|
|
|
|
return ret; |
|
330
|
|
|
|
|
|
|
} |
|
331
|
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
/* Argon2 Team - Begin Code */ |
|
333
|
90
|
|
|
|
|
|
int blake2b_long(void *pout, size_t outlen, const void *in, size_t inlen) { |
|
334
|
|
|
|
|
|
|
uint8_t *out = (uint8_t *)pout; |
|
335
|
|
|
|
|
|
|
blake2b_state blake_state; |
|
336
|
90
|
|
|
|
|
|
uint8_t outlen_bytes[sizeof(uint32_t)] = {0}; |
|
337
|
|
|
|
|
|
|
int ret = -1; |
|
338
|
|
|
|
|
|
|
|
|
339
|
90
|
50
|
|
|
|
|
if (outlen > UINT32_MAX) { |
|
340
|
|
|
|
|
|
|
goto fail; |
|
341
|
|
|
|
|
|
|
} |
|
342
|
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
/* Ensure little-endian byte order! */ |
|
344
|
90
|
|
|
|
|
|
store32(outlen_bytes, (uint32_t)outlen); |
|
345
|
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
#define TRY(statement) \ |
|
347
|
|
|
|
|
|
|
do { \ |
|
348
|
|
|
|
|
|
|
ret = statement; \ |
|
349
|
|
|
|
|
|
|
if (ret < 0) { \ |
|
350
|
|
|
|
|
|
|
goto fail; \ |
|
351
|
|
|
|
|
|
|
} \ |
|
352
|
|
|
|
|
|
|
} while ((void)0, 0) |
|
353
|
|
|
|
|
|
|
|
|
354
|
90
|
100
|
|
|
|
|
if (outlen <= BLAKE2B_OUTBYTES) { |
|
355
|
28
|
50
|
|
|
|
|
TRY(blake2b_init(&blake_state, outlen)); |
|
356
|
28
|
50
|
|
|
|
|
TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes))); |
|
357
|
28
|
50
|
|
|
|
|
TRY(blake2b_update(&blake_state, in, inlen)); |
|
358
|
28
|
|
|
|
|
|
TRY(blake2b_final(&blake_state, out, outlen)); |
|
359
|
|
|
|
|
|
|
} else { |
|
360
|
|
|
|
|
|
|
uint32_t toproduce; |
|
361
|
|
|
|
|
|
|
uint8_t out_buffer[BLAKE2B_OUTBYTES]; |
|
362
|
|
|
|
|
|
|
uint8_t in_buffer[BLAKE2B_OUTBYTES]; |
|
363
|
62
|
50
|
|
|
|
|
TRY(blake2b_init(&blake_state, BLAKE2B_OUTBYTES)); |
|
364
|
62
|
50
|
|
|
|
|
TRY(blake2b_update(&blake_state, outlen_bytes, sizeof(outlen_bytes))); |
|
365
|
62
|
50
|
|
|
|
|
TRY(blake2b_update(&blake_state, in, inlen)); |
|
366
|
62
|
50
|
|
|
|
|
TRY(blake2b_final(&blake_state, out_buffer, BLAKE2B_OUTBYTES)); |
|
367
|
|
|
|
|
|
|
memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2); |
|
368
|
62
|
|
|
|
|
|
out += BLAKE2B_OUTBYTES / 2; |
|
369
|
62
|
|
|
|
|
|
toproduce = (uint32_t)outlen - BLAKE2B_OUTBYTES / 2; |
|
370
|
|
|
|
|
|
|
|
|
371
|
1860
|
100
|
|
|
|
|
while (toproduce > BLAKE2B_OUTBYTES) { |
|
372
|
|
|
|
|
|
|
memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES); |
|
373
|
1798
|
50
|
|
|
|
|
TRY(blake2b(out_buffer, BLAKE2B_OUTBYTES, in_buffer, |
|
374
|
|
|
|
|
|
|
BLAKE2B_OUTBYTES, NULL, 0)); |
|
375
|
|
|
|
|
|
|
memcpy(out, out_buffer, BLAKE2B_OUTBYTES / 2); |
|
376
|
1798
|
|
|
|
|
|
out += BLAKE2B_OUTBYTES / 2; |
|
377
|
1798
|
|
|
|
|
|
toproduce -= BLAKE2B_OUTBYTES / 2; |
|
378
|
|
|
|
|
|
|
} |
|
379
|
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
memcpy(in_buffer, out_buffer, BLAKE2B_OUTBYTES); |
|
381
|
62
|
50
|
|
|
|
|
TRY(blake2b(out_buffer, toproduce, in_buffer, BLAKE2B_OUTBYTES, NULL, |
|
382
|
|
|
|
|
|
|
0)); |
|
383
|
|
|
|
|
|
|
memcpy(out, out_buffer, toproduce); |
|
384
|
|
|
|
|
|
|
} |
|
385
|
|
|
|
|
|
|
fail: |
|
386
|
90
|
|
|
|
|
|
clear_internal_memory(&blake_state, sizeof(blake_state)); |
|
387
|
90
|
|
|
|
|
|
return ret; |
|
388
|
|
|
|
|
|
|
#undef TRY |
|
389
|
|
|
|
|
|
|
} |
|
390
|
|
|
|
|
|
|
/* Argon2 Team - End Code */ |