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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/* See LICENSE file for copyright and license details. */
#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
# pragma clang diagnostic ignored "-Wpadded"
# pragma clang diagnostic ignored "-Wcomma"
# pragma clang diagnostic ignored "-Wcovered-switch-default"
#endif
#if defined(SUPPORT_SHA0) || defined(SUPPORT_SHA1)
# define LIBHASHSUM_INCLUDE_LIBSHA1_STATE
#endif
#if defined(SUPPORT_SHA2)
# define LIBHASHSUM_INCLUDE_LIBSHA2_STATE
#endif
#if defined(SUPPORT_KECCAK) || defined(SUPPORT_SHA3) || defined(SUPPORT_SHAKE) || defined(SUPPORT_RAWSHAKE)
# define LIBHASHSUM_INCLUDE_LIBKECCAK_STATE
#endif
#if defined(SUPPORT_BLAKE224) || defined(SUPPORT_BLAKE256)
# define SUPPORT_BLAKES
#endif
#if defined(SUPPORT_BLAKE384) || defined(SUPPORT_BLAKE512)
# define SUPPORT_BLAKEB
#endif
#if defined(SUPPORT_BLAKES) || defined(SUPPORT_BLAKEB)
# define SUPPORT_BLAKE
#endif
#if defined(SUPPORT_BLAKE)
# define LIBHASHSUM_INCLUDE_LIBBLAKE_STATE
#endif
#include "libhashsum.h"
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#if defined(__GNUC__)
__attribute__((__const__, __visibility__("hidden")))
#endif
uint8_t libhashsum_reverse_byte__(uint8_t);
#ifdef LIBHASHSUM_INCLUDE_LIBKECCAK_STATE
#if defined(__GNUC__)
__attribute__((__visibility__("hidden")))
#endif
int libhashsum_init_keccak__(struct libhashsum_hasher *this, size_t hashbits, void *spec, size_t squeezes, const char *suffix);
#endif
#define KECCAKN(N) 1600 - 2 * (N), 2 * (N), (N), 1
#ifdef TEST
struct testcase {
size_t input_repeat;
unsigned extra_bits;
const char *input;
const char *output;
};
static void
hex(char *restrict out, const unsigned char *restrict in, size_t n)
{
for (; n--; in++) {
*out++ = "0123456789abcdef"[(*in >> 4) & 15];
*out++ = "0123456789abcdef"[(*in >> 0) & 15];
}
*out = '\0';
}
static char *
escape(const char *s, size_t n)
{
char *ret, *p;
ret = malloc(n * 4U + 1U);
if (!ret) {
perror("malloc");
exit(2);
}
for (p = ret; n--; s++) {
if (*s == '"' || *s == '\\') {
*p++ = '\\';
*p++ = *s;
} else if (*s == '\n') {
*p++ = '\\';
*p++ = 'n';
} else if (*s < ' ' || *s >= 127) {
*p++ = '\\';
*p++ = 'x';
*p++ = "0123456789abcdef"[(*s >> 4) & 15];
*p++ = "0123456789abcdef"[(*s >> 0) & 15];
} else {
*p++ = *s;
}
}
*p = '\0';
return ret;
}
static int
run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_size,
struct testcase *testcases, size_t ntestcases, char hexsum[])
{
struct libhashsum_hasher hasher, hasher2;
unsigned char custom_hashbuffer[2048];
unsigned char custom_hashbuffer2[sizeof(custom_hashbuffer)];
int use_custom_hashbuffer = 0;
char *input, *p, bitstr[sizeof(" + b\"1234567\"")];
unsigned char extra_bit;
int ok = 1, caseok;
size_t i, j, input_string_len, input_total_len, input_size;
size_t bits;
for (i = 0; i < ntestcases; i++, use_custom_hashbuffer ^= 1) {
if (libhashsum_init_hasher(&hasher, algorithm)) {
perror("libhashsum_init_hasher");
return 2;
}
if (hasher.algorithm != algorithm) {
fprintf(stderr, "libhashsum_init_hasher returned unexpected value in .algorithm\n");
return 2;
}
if (!hasher.algorithm_string) {
fprintf(stderr, "libhashsum_init_hasher returned NULL pointer in .algorithm_string\n");
return 2;
}
if (strcmp(hasher.algorithm_string, name)) {
fprintf(stderr, "libhashsum_init_hasher returned unexpected value in .algorithm_string\n");
return 2;
}
if (hasher.hash_size != hash_size) {
fprintf(stderr, "libhashsum_init_hasher returned unexpected value in .hash_size\n");
return 2;
}
if (hasher.hash_output) {
fprintf(stderr, "libhashsum_init_hasher returned non-NULL pointer in .hash_output\n");
return 2;
}
if (!hasher.process) {
fprintf(stderr, "libhashsum_init_hasher returned NULL pointer in .process\n");
return 2;
}
if (!hasher.finalise_const) {
fprintf(stderr, "libhashsum_init_hasher returned NULL pointer in .finalise_const\n");
return 2;
}
if (!hasher.finalise) {
fprintf(stderr, "libhashsum_init_hasher returned NULL pointer in .finalise\n");
return 2;
}
if (hasher.stretch) {
fprintf(stderr, "libhashsum_init_hasher returned non-NULL pointer in .stretch\n");
return 2;
}
input_string_len = strlen(testcases[i].input);
bits = testcases[i].extra_bits;
if (bits) {
if (input_string_len < (bits + 7U) / 8U)
input_string_len = (bits + 7U) / 8U;
bits %= 8U;
if (bits)
input_string_len -= 1U;
}
input_total_len = testcases[i].input_repeat * input_string_len;
input_size = input_total_len + hasher.input_block_size + 1U;
p = input = malloc(input_size);
if (!input) {
perror("malloc");
return 2;
}
for (j = 0; j < testcases[i].input_repeat; j++) {
memcpy(p, testcases[i].input, input_string_len);
p = &p[input_string_len];
}
if (bits)
*p = testcases[i].input[input_string_len];
hasher.hash_output = use_custom_hashbuffer ? custom_hashbuffer : NULL;
if (hasher.finalise_const(&hasher, input, input_total_len, (unsigned)bits)) {
perror("hasher.finalise_const");
return 2;
}
if (!hasher.hash_output) {
fprintf(stderr, "hasher.finalise_const did not set hasher.hash_output\n");
return 2;
}
#ifndef NO_CUSTOM_HASH_BUFFER_SUPPORT
if (use_custom_hashbuffer && hasher.hash_output != custom_hashbuffer) {
fprintf(stderr, "hasher.finalise_const did keep custom hash buffer\n");
return 2;
}
#endif
hex(hexsum, hasher.hash_output, hasher.hash_size);
ok &= caseok = !testcases[i].output || !strcmp(hexsum, testcases[i].output);
if (libhashsum_init_hasher(&hasher2, algorithm)) {
perror("libhashsum_init_hasher");
return 2;
}
hasher2.hash_output = use_custom_hashbuffer ? custom_hashbuffer2 : NULL;
if (hasher2.finalise(&hasher2, input, input_total_len, (unsigned)bits, input_size)) {
perror("hasher.finalise");
return 2;
}
if (!hasher2.hash_output) {
fprintf(stderr, "hasher.finalise did not set hasher.hash_output\n");
return 2;
}
#ifndef NO_CUSTOM_HASH_BUFFER_SUPPORT
if (use_custom_hashbuffer && hasher2.hash_output != custom_hashbuffer2) {
fprintf(stderr, "hasher.finalise did keep custom hash buffer\n");
return 2;
}
#endif
if (memcmp(hasher2.hash_output, hasher.hash_output, hasher.hash_size)) {
fprintf(stderr, "hasher.finalise and hasher.finalise_const produced different hashes\n");
return 2;
}
free(input);
input = escape(testcases[i].input, input_string_len);
bitstr[0] = '\0';
if (bits) {
extra_bit = (unsigned char)testcases[i].input[input_string_len];
p = bitstr;
*p++ = ' ';
*p++ = '+';
*p++ = ' ';
*p++ = 'b';
*p++ = '"';
while (bits--) {
*p++ = "01"[extra_bit & 1U];
extra_bit >>= 1;
}
*p++ = '"';
*p = '\0';
}
if (testcases[i].input_repeat == 1)
fprintf(stderr, "[\033[1;%s\033[m] %s(\"%s\"%s) = %s\n",
caseok ? "32mPASS" : "31mFAIL", name, input, bitstr, hexsum);
else if (!testcases[i].input_repeat && *bitstr)
fprintf(stderr, "[\033[1;%s\033[m] %s(%s) = %s\n",
caseok ? "32mPASS" : "31mFAIL", name, &bitstr[3], hexsum);
else if (!testcases[i].input_repeat)
fprintf(stderr, "[\033[1;%s\033[m] %s(\"\") = %s\n",
caseok ? "32mPASS" : "31mFAIL", name, hexsum);
else
fprintf(stderr, "[\033[1;%s\033[m] %s(%zu * \"%s\"%s) = %s\n",
caseok ? "32mPASS" : "31mFAIL", name, testcases[i].input_repeat, input, bitstr, hexsum);
free(input);
if (hasher.destroy)
hasher.destroy(&hasher);
}
return !ok;
}
# define TEST_MAIN(NAME, ID)\
char hexsum[LIBHASHSUM_##ID##_HASH_SIZE * 2 + 1];\
return run_tests(NAME, LIBHASHSUM_##ID, LIBHASHSUM_##ID##_HASH_SIZE,\
testcases, sizeof(testcases) / sizeof(*testcases), hexsum)
# define ASSERT(ASSERTION)\
do {\
if ((ASSERTION))\
break;\
fprintf(stderr, "assertion `%s` at line %i failed\n", #ASSERTION, __LINE__);\
exit(2);\
} while (0)
#endif
#ifdef TEST_UNSUPPORTED
# define TEST_MAIN(NAME, ID)\
struct libhashsum_hasher hasher;\
if (!libhashsum_init_hasher(&hasher, LIBHASHSUM_##ID)) {\
fprintf(stderr, "expected libhashsum_init_hasher to fail, but it returned successfully\n");\
return 2;\
}\
if (errno != ENOSYS) {\
perror("expected libhashsum_init_hasher to set errno to ENOSYS, but got");\
return 2;\
}\
return 0;
#endif
|