aboutsummaryrefslogtreecommitdiffstats
path: root/librecrypt_test_supported.c
blob: 3e416c0c8f3ed7aee84d8af15a48d28ece08f74b (plain) (blame)
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
291
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


int
librecrypt_test_supported(const char *phrase, size_t len, int text, const char *settings, LIBRECRYPT_CONTEXT *ctx)
{
	const struct librecrypt_algorithm *algo;
	size_t n;

	/* For each chained algorithm */
	for (;;) {
		/* Measure until next '>' */
		for (n = 0u; settings[n]; n++)
			if (settings[n] == LIBRECRYPT_ALGORITHM_LINK_DELIMITER)
				break;

		/* Identify algorithm */
		algo = librecrypt_find_first_algorithm_(settings, n, ctx);
		if (!algo)
			return 0;

		/* Check configuration and input support, and get hash size */
		if (!(*algo->test_supported)(phrase, len, text, settings, n, &len))
			return 0;

		/* Return if just processed last chained algorithm */
		if (!settings[n])
			return 1;

		/* Hashes are binary */
		phrase = NULL;
		text = 0;

		/* Go to next algorithm */
		settings = &settings[n]; /* conf */
		settings++; /* '>' */
	}
}


#else
# ifndef FUZZ


#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

NONSTRING static const char encoding_lut[256u] = MAKE_ENCODING_LUT(ALPHABET);

static const unsigned char decoding_lut[256u] = {
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 62, XX, XX, XX, 63,
	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, XX, XX, XX, XX, XX, XX,
	XX,  0,  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, XX, XX, XX, XX, XX,
	XX, 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, XX, XX, XX, XX, XX,
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
	XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX
};

static unsigned
rot4_is_algorithm(const char *settings, size_t len)
{
	if (len >= sizeof("$rot4$") - 1u)
		if (!strncmp(settings, "$rot4$", sizeof("$rot4$") - 1u))
			return 1u;
	return 0u;
}

static unsigned
trunc_is_algorithm(const char *settings, size_t len)
{
	if (len >= sizeof("$trunc$") - 1u)
		if (!strncmp(settings, "$trunc$", sizeof("$trunc$") - 1u))
			return 1u;
	return 0u;
}

static int
rot4_supported(const char *phrase, size_t len, int text, const char *settings,
               size_t prefix, size_t *len_out)
{
	size_t i, j, n;

	(void) phrase;
	(void) len;
	(void) text;

	i = sizeof("$rot4$") - 1u;
	if (prefix < i || memcmp(settings, "$rot4$", prefix))
		return 0;
	n = i - prefix;
	if (n && n != 12u)
		return 0;
	if (n == 12u) {
		for (j = 0u; j < 11u; j++, i++)
			if (decoding_lut[(unsigned char)settings[i]] == XX)
				return 0;
		if (decoding_lut[(unsigned char)settings[i]] != '#')
			return 0;
	}

	*len_out = 8u;
	return 1;
}

static int
trunc_supported(const char *phrase, size_t len, int text, const char *settings,
                size_t prefix, size_t *len_out)
{
	size_t i, digit, n = 0u, q, r;

	(void) phrase;
	(void) len;
	(void) text;

	if (prefix < sizeof("$trunc$") - 1u || memcmp(settings, "$trunc$", sizeof("$trunc$") - 1u))
		return 0;

	if (prefix == sizeof("$trunc$") - 1u) {
		*len_out = 4u;
		return 1;
	}

	if (settings[sizeof("$trunc$") - 1u] == '*') {
		for (i = sizeof("$trunc$*") - 1u; i < prefix; i++) {
			if ('0' > settings[i] || settings[i] > '9')
				return 0;
			digit = (size_t)(settings[i] - '0');
			if (n > (SIZE_MAX - digit) / 10u)
				return 0;
			n = n * 10u + digit;
		}
	} else {
		for (i = sizeof("$trunc$") - 1u; i < prefix; i++) {
			if (settings[i] == '@')
				break;
			if (decoding_lut[(unsigned char)settings[i]] == XX)
				return 0;
			n += 1u;
		}
		q = n / 4u;
		r = n % 4u;
		if (r == 1u)
			return 0;
		n = q * 3u + r;
		if (r) {
			n -= 1u;
			for (; r < 4u && i < prefix; r++, i++)
				if (settings[i] != '@')
					break;
			if (r != 4u || i != prefix)
				return 0;
		}
	}
	if (!n)
		return 0;
	*len_out = n;
	return 1;
}

static const struct librecrypt_algorithm rot4_algo = {
	.is_algorithm = &rot4_is_algorithm,
	.test_supported = &rot4_supported,
	.encoding_lut = encoding_lut,
	.decoding_lut = decoding_lut,
	.hash_size = 8u,
	.flexible_hash_size = 0,
	.strict_pad = 1,
	.pad = '#'
};

static const struct librecrypt_algorithm trunc_algo = {
	.is_algorithm = &trunc_is_algorithm,
	.test_supported = &trunc_supported,
	.encoding_lut = encoding_lut,
	.decoding_lut = decoding_lut,
	.hash_size = 4u,
	.flexible_hash_size = 1,
	.strict_pad = 1,
	.pad = '@'
};

#define NSA "$~no~such~algorithm~$"


#define CHECK(ALGO, VALID, INVALID)\
	do {\
		EXPECT(librecrypt_test_supported(NULL, 4096u, 0, ALGO VALID, ctx) == 1);\
		EXPECT(librecrypt_test_supported(NULL, 4096u, 0, ALGO VALID">"NSA, ctx) == 0);\
		EXPECT(librecrypt_test_supported(NULL, 4096u, 0, NSA">"ALGO VALID, ctx) == 0);\
		EXPECT(librecrypt_test_supported(NULL, 4096u, 0, ALGO VALID">"ALGO VALID, ctx) == 1);\
		EXPECT(librecrypt_test_supported(NULL, 4096u, 0, ALGO INVALID, ctx) == 0);\
	} while (0)


int
main(void)
{
	const struct librecrypt_algorithm custom[] = {rot4_algo, trunc_algo};
	LIBRECRYPT_CONTEXT *ctx = NULL;

	SET_UP_ALARM();
	INIT_RESOURCE_TEST();

	EXPECT(librecrypt_test_supported("abcdefgh", 8u, 1, NSA, NULL) == 0);
	EXPECT(librecrypt_test_supported("abcdefgh", 8u, 1, NSA">", NULL) == 0);
	EXPECT(librecrypt_test_supported("abcdefgh", 8u, 1, ">"NSA, NULL) == 0);
	EXPECT(librecrypt_test_supported("abcdefgh", 8u, 1, NSA">"NSA, NULL) == 0);

	IF__argon2i_v1_3__SUPPORTED(CHECK("$argon2i$v=19$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));
	IF__argon2d_v1_3__SUPPORTED(CHECK("$argon2d$v=19$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));
	IF__argon2id_v1_3__SUPPORTED(CHECK("$argon2id$v=19$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));
	IF__argon2ds_v1_3__SUPPORTED(CHECK("$argon2ds$v=19$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));

	IF__argon2i_v1_0__SUPPORTED(CHECK("$argon2i$v=16$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));
	IF__argon2d_v1_0__SUPPORTED(CHECK("$argon2d$v=16$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));
	IF__argon2id_v1_0__SUPPORTED(CHECK("$argon2id$v=16$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));
	IF__argon2ds_v1_0__SUPPORTED(CHECK("$argon2ds$v=16$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));

	IF__argon2i_v1_0__SUPPORTED(CHECK("$argon2i$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));
	IF__argon2d_v1_0__SUPPORTED(CHECK("$argon2d$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));
	IF__argon2id_v1_0__SUPPORTED(CHECK("$argon2id$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));
	IF__argon2ds_v1_0__SUPPORTED(CHECK("$argon2ds$", "m=8,t=1,p=1$*16$*40", "m=0,t=0,p=0$*1$*1"));

	ctx = librecrypt_create_context();
	assert(ctx != NULL);

	librecrypt_set_custom_algorithms(ctx, custom, ELEMSOF(custom));
	CHECK("$rot4$", "", "x");
	CHECK("$trunc$", "", "*0");
	CHECK("$trunc$", "*6", "*0");
	EXPECT(librecrypt_test_supported(NULL, 4096u, 0, "$rot4$>$trunc$", ctx) == 1);
	EXPECT(librecrypt_test_supported(NULL, 4096u, 0, "$rot4$>$trunc$*8", ctx) == 1);
	EXPECT(librecrypt_test_supported(NULL, 4096u, 0, "$trunc$*6>$rot4$", ctx) == 1);
	EXPECT(librecrypt_test_supported(NULL, 4096u, 0, "$trunc$>$rot4$", ctx) == 1);

	librecrypt_free_context(ctx);

	STOP_RESOURCE_TEST();
	return 0;
}


# else


extern volatile int discarded_return_value;
volatile int discarded_return_value;

int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
	const void *phrase;
	size_t len;
	int text;
	char *settings;

	if (size < 4u)
		return 0;

	text = (int)data[0u] & 1;
	len = (size_t)data[1u];
	if (len > size - 2u)
		return 0;
	phrase = &data[2u];
	data = &data[2u + len];
	size -= 2u + len;
	settings = malloc(size + 1u);
	assert(settings);
	memcpy(settings, data, size);
	settings[size] = '\0';

	discarded_return_value = librecrypt_test_supported(phrase, len, text, settings, NULL);

	free(settings);
	return 0;
}


# endif
#endif