aboutsummaryrefslogtreecommitdiffstats
path: root/librecrypt_find_first_algorithm_.c
blob: a339d93a16a34f5144188069eebbc4d11f1eea47 (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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


const struct librecrypt_algorithm *
librecrypt_find_first_algorithm_(const char *settings, size_t len, LIBRECRYPT_CONTEXT *ctx)
{
	unsigned r, priority = 0;
	const struct librecrypt_algorithm *algo, *found = NULL;
	size_t i;

	/* Search application provided hash functions first;
	 * they should have priority of they report the same
	 * priority as library-provided hash functions */
	if (!ctx)
		goto library_provided;
	for (i = 0u; i < ctx->nalgos; i++) {
		algo = &ctx->algos[i];
		r = (*algo->is_algorithm)(settings, len);
		if (r > priority) {
			priority = r;
			found = algo;
		}
	}

library_provided:
	for (i = 0u;; i++) {
		/* Get next algorithm in the list */
		algo = &librecrypt_algorithms_[i];
		if (IS_END_OF_ALGORITHMS(algo))
			break;

		/* Get match-priority, bigger is more prioritised,
		 * 0 is non-match, so we save if we get a bigger
		 * value then our current best (we start at 0
		 * (no match found)) */
		r = (*algo->is_algorithm)(settings, len);
		if (r > priority) {
			priority = r;
			found = algo;
		}
	}

	/* NULL if all `is_algorithm` returned 0 (including if the
	 * last was completly empty), otherwise the first one
	 * (actually an arbitrary one) that returned the greated
	 * match-priority (which doesn't necessarily mean it's better,
	 * which is why we don't just break at the first match
	 * (`librecrypt_algorithms_` is ordered by how good they are)) */
	return found;
}


#else


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


#define CHECK(ALGO)\
	do {\
		algo = librecrypt_find_first_algorithm_(ALGO, sizeof(ALGO) - 1u, NULL);\
		EXPECT(algo != NULL);\
		EXPECT((*algo->is_algorithm)(ALGO, sizeof(ALGO) - 1u) > 0u);\
		EXPECT(librecrypt_find_first_algorithm_(ALGO">"NSA, sizeof(ALGO">"NSA) - 1u, NULL) == algo);\
	} while (0)


static unsigned prio1;
static unsigned prio2;

static unsigned
is_algorithm_x1(const char *settings, size_t len)
{
	if (len && settings[0u] == 'x')
		return prio1;
	return 0;
}

static unsigned
is_algorithm_x2(const char *settings, size_t len)
{
	if (len && settings[0u] == 'x')
		return prio2;
	return 0;
}

static unsigned
is_algorithm_y(const char *settings, size_t len)
{
	if (len && settings[0u] == 'y')
		return prio2;
	return 0;
}

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



int
main(void)
{
	const struct librecrypt_algorithm *algo;
	struct librecrypt_algorithm custom[2];
	LIBRECRYPT_CONTEXT *ctx;

	SET_UP_ALARM();
	INIT_RESOURCE_TEST();

	EXPECT(librecrypt_find_first_algorithm_(NSA, sizeof(NSA) - 1u, NULL) == NULL);
	EXPECT(librecrypt_find_first_algorithm_(NSA">", sizeof(NSA">") - 1u, NULL) == NULL);

	IF__argon2i__SUPPORTED(CHECK("$argon2i$"));
	IF__argon2d__SUPPORTED(CHECK("$argon2d$"));
	IF__argon2id__SUPPORTED(CHECK("$argon2id$"));
	IF__argon2ds__SUPPORTED(CHECK("$argon2ds$"));

	ctx = librecrypt_create_context();
	assert(ctx != NULL);
	librecrypt_set_custom_algorithms(ctx, custom, ELEMSOF(custom));

	prio1 = 1u;
	prio2 = 2u;
	custom[0].is_algorithm = &is_algorithm_x1;
	custom[1].is_algorithm = &is_algorithm_y;
	EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);
	EXPECT(librecrypt_find_first_algorithm_("y", 1u, ctx) == &custom[1u]);

	prio1 = 1u;
	prio2 = 1u;
	custom[0].is_algorithm = &is_algorithm_x1;
	custom[1].is_algorithm = &is_algorithm_y;
	EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);
	EXPECT(librecrypt_find_first_algorithm_("y", 1u, ctx) == &custom[1u]);

	prio1 = 1u;
	prio2 = 1u;
	custom[0].is_algorithm = &is_algorithm_x1;
	custom[1].is_algorithm = &is_algorithm_x2;
	EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);
	EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);

	prio1 = 2u;
	prio2 = 1u;
	custom[0].is_algorithm = &is_algorithm_x1;
	custom[1].is_algorithm = &is_algorithm_x2;
	EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);
	EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[0u]);

	prio1 = 1u;
	prio2 = 2u;
	custom[0].is_algorithm = &is_algorithm_x1;
	custom[1].is_algorithm = &is_algorithm_x2;
	EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[1u]);
	EXPECT(librecrypt_find_first_algorithm_("x", 1u, ctx) == &custom[1u]);

	librecrypt_set_custom_algorithms(ctx, custom, 1u);
	custom[0].is_algorithm = &is_algorithm_argon2;
	prio1 = UINT_MAX;
	EXPECT(librecrypt_find_first_algorithm_("$argon2id$", sizeof("$argon2id$") - 1u, ctx) == &custom[0u]);
	prio1 = 1u;
	EXPECT(librecrypt_find_first_algorithm_("$argon2id$", sizeof("$argon2id$") - 1u, ctx) == &custom[0u]);
	prio1 = 0u;
	EXPECT(librecrypt_find_first_algorithm_("$argon2id$", sizeof("$argon2id$") - 1u, ctx) != &custom[0u]);

	librecrypt_free_context(ctx);

	STOP_RESOURCE_TEST();
	return 0;
}


#endif