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
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST
size_t
librecrypt_settings_prefix(const char *hash, size_t *hashsize_out, LIBRECRYPT_CONTEXT *ctx)
{
size_t i, len, ret = 0u;
size_t last_offset = 0u;
const struct librecrypt_algorithm *algo;
uintmax_t hashsize;
/* Find last algorithm, and beginning of result */
for (i = 0u; hash[i]; i++) {
if (hash[i] == LIBRECRYPT_HASH_COMPOSITION_DELIMITER)
ret = i + 1u;
else if (hash[i] == LIBRECRYPT_ALGORITHM_LINK_DELIMITER)
last_offset = ret = i + 1u;
}
/* and get `strlen(hash)` */
len = i;
/* Special case for bsdicrypt */
if (ret == last_offset && ret < len && hash[ret] == '_')
ret += 1u;
/* Return if hash size is not required */
if (!hashsize_out)
goto out;
/* Return 0 as hash size if default is specified */
if (ret == i)
goto zero;
/* Return 0 as hash size if algorithm cannot be identified or has fixed hash size */
algo = librecrypt_find_first_algorithm_(&hash[last_offset], len - last_offset, ctx);
if (!algo)
goto zero;
if (!algo->flexible_hash_size)
goto zero;
/* Get the hash size */
if (!librecrypt_scan_settings(&hash[ret], len - ret, "%^b",
&hashsize, (uintmax_t)1u, (uintmax_t)SIZE_MAX,
algo->decoding_lut, algo->pad, algo->strict_pad))
goto zero;
*hashsize_out = (size_t)hashsize;
goto out;
zero:
*hashsize_out = 0u;
out:
return ret;
}
#else
# ifndef FUZZ
#define CHECK_NULL(PREFIX, SUFFIX)\
do {\
EXPECT(librecrypt_settings_prefix(PREFIX SUFFIX, NULL, ctx) == sizeof(PREFIX) - 1u);\
EXPECT(librecrypt_settings_prefix(PREFIX, NULL, ctx) == sizeof(PREFIX) - 1u); \
} while (0)
#define CHECK_ZERO(PREFIX, SUFFIX)\
do {\
size_t hashsize = 99999u;\
EXPECT(librecrypt_settings_prefix(PREFIX SUFFIX, &hashsize, ctx) == sizeof(PREFIX) - 1u);\
EXPECT(hashsize == 0u);\
} while (0)
#define CHECK_HASH(PREFIX, SUFFIX, HASH)\
do {\
size_t hashsize = 99999u;\
EXPECT(librecrypt_settings_prefix(PREFIX SUFFIX, &hashsize, ctx) == sizeof(PREFIX) - 1u);\
EXPECT(hashsize == HASH##u);\
} while (0)
static unsigned
dumdum_is_algorithm(const char *settings, size_t len)
{
(void) settings;
(void) len;
return 1u;
}
static struct librecrypt_algorithm dumdum = {
.is_algorithm = &dumdum_is_algorithm,
.flexible_hash_size = 0
};
int
main(void)
{
LIBRECRYPT_CONTEXT *ctx = NULL;
SET_UP_ALARM();
INIT_RESOURCE_TEST();
/* Simple cases */
CHECK_NULL("", "result");
CHECK_NULL(">", "double-des result");
CHECK_NULL("$", "x");
CHECK_NULL("y$", "x");
CHECK_NULL("y>", "x");
CHECK_NULL("a$b$c>d$e$", "x");
CHECK_NULL("a$b$c>", "x");
/* Discard asterisk-notation */
CHECK_NULL("", "*10");
CHECK_NULL(">", "*11");
CHECK_NULL("$", "*12");
CHECK_NULL("y$", "*13");
CHECK_NULL("y>", "*012");
CHECK_NULL("a$b$c>d$e$", "*1");
CHECK_NULL("a$b$c>", "*2");
CHECK_NULL("a$b$c>*10$", "*2");
/* Special case: bsdicrypt */
CHECK_NULL("_", "res");
CHECK_NULL("_", "");
CHECK_NULL("$x$*10>_", "_");
/* Check without hash */
CHECK_ZERO("a$b$", "");
CHECK_ZERO("a$b$>", "");
CHECK_ZERO("a$b$x>", "");
CHECK_ZERO("a$b$*10>", "");
/* Check without invalid algorithm */
CHECK_ZERO("$~no~such~algorithm~$", "hash");
/* Check without hash and hashlen */
#if defined(SUPPORT_ARGON2I)
CHECK_HASH("$argon2i$m=8,t=1,p=1$*99$", "*100", 100);
CHECK_HASH("x$*99>$argon2i$m=8,t=1,p=1$*99$", "*100", 100);
CHECK_HASH("$argon2i$m=8,t=1,p=1$*99$", "NineByteHash", 9);
CHECK_HASH("x$*99>$argon2i$m=8,t=1,p=1$*99$", "NineByteHash", 9);
#endif
#if defined(SUPPORT_ARGON2ID)
CHECK_HASH("$argon2id$m=8,t=1,p=1$*99$", "*100", 100);
CHECK_HASH("x$*99>$argon2id$m=8,t=1,p=1$*99$", "*100", 100);
CHECK_HASH("$argon2id$m=8,t=1,p=1$*99$", "NineByteHash", 9);
CHECK_HASH("x$*99>$argon2id$m=8,t=1,p=1$*99$", "NineByteHash", 9);
#endif
#if defined(SUPPORT_ARGON2D)
CHECK_HASH("$argon2d$m=8,t=1,p=1$*99$", "*100", 100);
CHECK_HASH("x$*99>$argon2d$m=8,t=1,p=1$*99$", "*100", 100);
CHECK_HASH("$argon2d$m=8,t=1,p=1$*99$", "NineByteHash", 9);
CHECK_HASH("x$*99>$argon2d$m=8,t=1,p=1$*99$", "NineByteHash", 9);
#endif
#if defined(SUPPORT_ARGON2DS)
CHECK_HASH("$argon2ds$m=8,t=1,p=1$*99$", "*100", 100);
CHECK_HASH("x$*99>$argon2ds$m=8,t=1,p=1$*99$", "*100", 100);
CHECK_HASH("$argon2ds$m=8,t=1,p=1$*99$", "NineByteHash", 9);
CHECK_HASH("x$*99>$argon2ds$m=8,t=1,p=1$*99$", "NineByteHash", 9);
#endif
/* Check without invalid hash */
IF__argon2i__SUPPORTED(CHECK_ZERO("$argon2i$m=8,t=1,p=1$*99$", "#");)
IF__argon2d__SUPPORTED(CHECK_ZERO("$argon2d$m=8,t=1,p=1$*99$", "#");)
IF__argon2id__SUPPORTED(CHECK_ZERO("$argon2id$m=8,t=1,p=1$*99$", "#");)
IF__argon2ds__SUPPORTED(CHECK_ZERO("$argon2ds$m=8,t=1,p=1$*99$", "#");)
/* Test without flexible hash size (but with context I guess) */
ctx = librecrypt_create_context();
assert(ctx != NULL);
librecrypt_set_custom_algorithms(ctx, &dumdum, 1u);
CHECK_ZERO("$dumdum$", "abcd");
librecrypt_free_context(ctx);
STOP_RESOURCE_TEST();
return 0;
}
# else
int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
char *hash;
size_t r;
hash = malloc(size + 1u);
assert(hash);
memcpy(hash, data, size);
hash[size] = '\0';
r = librecrypt_settings_prefix(hash, &(size_t){0u}, NULL);
EXPECT(librecrypt_settings_prefix(hash, NULL, NULL) == r);
free(hash);
return 0;
}
# endif
#endif
|