summaryrefslogtreecommitdiff
path: root/src/hash.c
blob: 18ae1f893bf5202f30001114c9bb894066165f99 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "config.h"

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <pthread.h>

#include <siphash.h>

#include "logerr.h"
#include "random.h"
#include "hash.h"


// FIXME: verify that this is the correct type of mutex for the use case
static pthread_mutex_t
SEED_INIT_MUTEX = PTHREAD_MUTEX_INITIALIZER;

#define PRIV_SEED_LENGTH 16

struct Seed {
	bool initialized;
	uint8_t bytes[PRIV_SEED_LENGTH];
};

static struct Seed
SEED = {
	.initialized = false,
	.bytes = { 0 },
};


int
hash_init(void) {
	int rc = 0;

	int ret = 0;
	struct Seed seed = { 0 };

	if (SEED.initialized) {
		goto out;
	}

	if (urandom_bytes(sizeof(seed.bytes), &seed.bytes)) {
		logerr("urandom_bytes(sizeof(seed.bytes), &seed.bytes);\n");
		rc = -1;
		goto out;
	}

	ret = pthread_mutex_lock(&SEED_INIT_MUTEX);
	if (ret) {
		logerr(
			"pthread_mutex_lock(&SEED_INIT_MUTEX): %s;\n",
			strerror(ret)
		);
		rc = -1;
		goto out;
	}

	/*
	   In case where the seed was initialized by another thread while the
	   current was enterinig hash_init() and locking the mutex.  The
	   previous check is only a best-effort one to avoid all threads that
	   start at the same time to serially acquire the mutex and do nothing
	   with.  In order to sometimes avoid the extra overhead, the previous
	   check exists, but only the following check that has any correctness
	   guarantee, and instead of being best-effort it blocks others so it
	   can make the correct decision.
	*/
	if (SEED.initialized) {
		goto out;
	}

	// FIXME: memcpy?
	const uint8_t length = sizeof(SEED.bytes) / sizeof(SEED.bytes[0]);
	for (uint8_t i = 0; i < length; i++) {
		SEED.bytes[i] = seed.bytes[i];
	}

	SEED.initialized = true;

out:
	// FIXME: verify that unlocking an unlocked mutex is fine
	ret = pthread_mutex_unlock(&SEED_INIT_MUTEX);
	if (ret) {
		logerr(
			"pthread_mutex_unlock(&SEED_INIT_MUTEX): %s\n",
			strerror(ret)
		);
		rc = -1;
	}
	return rc;
}

/**
 * @tags infallible
*/
void
hash(const size_t len, const void *const in, uint8_t out[OUTPUT_LENGTH]) {
	assert(SEED.initialized && "Missing call to hash_init() before using hash().");
	siphash(in, len, SEED.bytes, out, 16);
	return;
}


#ifdef TEST
#include "testing.h"
static void *
hash_init_thread(void *data) {
	int *ret = (int *)data;
	*ret = hash_init();
	pthread_exit(NULL);
	return NULL;
}

static int
test_hash_init(void) {
	int rc = 0;

	test_start("hash_init()");

	const uint8_t length = sizeof(SEED.bytes) / sizeof(SEED.bytes[0]);

	assert(length == 16);
	for (uint8_t i = 0; i < length; i++) {
		assert(SEED.bytes[i] == 0);
	}

	pthread_mutex_t test_mutex = PTHREAD_MUTEX_INITIALIZER;
	int ret;

	{
		testing("when is initialized we do nothing");

		SEED.initialized = true;
		assert(hash_init() == 0);

		for (uint8_t i = 0; i < length; i++) {
			assert(SEED.bytes[i] == 0);
		}

		test_ok();
	}

	{
		testing("do nothing when initialized on the second check");

		/*
		   0. start with an unitialized seed
		   1. grab the lock
		   2. start the other thread
		   3. set SEED_INITIALIZED
		   4. release the lock
		   5. join the other thread
		   6. assert HASH_SEED stilll empty
		*/

		SEED.initialized = false;                                   // 0
		ret = pthread_mutex_lock(&test_mutex);                      // 1
		if (ret) {
			logerr(
				"pthread_mutex_lock(&test_mutex): %s;\n",
				strerror(ret)
			);
			rc = -1;
			goto out;
		}

		pthread_t t;
		int t_rc = -1;
		ret = pthread_create(&t, NULL, hash_init_thread, &t_rc);    // 2
		if (ret) {
			logerr("pthread_create(%s, %s, %s, %s): %s\n",
				"&t",
				"NULL",
				"hash_init",
				"&t_rc",
				strerror(ret));
			rc = -1;
			goto out;
		}

		SEED.initialized = true;                                    // 3
		ret = pthread_mutex_unlock(&test_mutex);                    // 4
		if (ret) {
			logerr(
				"pthread_mutex_unlock(&test_mutex): %s\n",
				strerror(ret)
			);
			rc = -1;
			goto out;
		}

		ret = pthread_join(t, NULL);                                // 5
		if (ret) {
			logerr("pthread_join(t, &rc): %s\n", strerror(ret));
			rc = -1;
			goto out;
		}

		if (t_rc) {
			logerr("hash_init() in pthread_t\n");
			rc = -1;
			goto out;
		}

		for (size_t i = 0; i < length; i++) {
			assert(SEED.bytes[i] == 0);                          // 6
		}

		test_ok();
	}

	{
		testing("competing threads can init the seed besides us");

		/*
		   0. start with an unitialized seed
		   1. grab the lock
		   2. start the other 2 threads
		   3. release the lock
		   4. join the other threads
		   5. assert(SEED_INITIALIZED == true);
		*/

		SEED.initialized = false;                                   // 0
		ret = pthread_mutex_lock(&test_mutex);                      // 1
		if (ret) {
			logerr(
				"pthread_mutex_lock(&SEED_INIT_MUTEX): %s;\n",
				strerror(ret)
			);
			rc = -1;
			goto out;
		}

		pthread_t t1;
		int t1_rc = -1;
		ret = pthread_create(&t1, NULL, hash_init_thread, &t1_rc);  // 2
		if (ret) {
			logerr("pthread_create(%s, %s, %s, %s): %s\n",
				"&t1",
				"NULL",
				"hash_init",
				"&t1_rc",
				strerror(ret));
			rc = -1;
			goto out;
		}

		pthread_t t2;
		int t2_rc = -1;
		ret = pthread_create(&t2, NULL, hash_init_thread, &t2_rc);  // 2
		if (ret) {
			logerr("pthread_create(%s, %s, %s, %s): %s\n",
				"&t2",
				"NULL",
				"hash_init",
				"&t2_rc",
				strerror(ret));
			rc = -1;
			goto out;
		}

		ret = pthread_mutex_unlock(&test_mutex);                    // 3
		if (ret) {
			logerr(
				"pthread_mutex_unlock(&test_mutex): %s\n",
				strerror(ret)
			);
			rc = -1;
			goto out;
		}

		ret = pthread_join(t1, NULL);                               // 4
		if (ret) {
			logerr("pthread_join(t1, &rc): %s\n", strerror(ret));
			rc = -1;
			goto out;
		}

		if (t1_rc) {
			logerr("hash_init() in pthread_t t1\n");
			rc = -1;
			goto out;
		}

		ret = pthread_join(t2, NULL);                               // 4
		if (ret) {
			logerr("pthread_join(t2, &rc): %s\n", strerror(ret));
			rc = -1;
			goto out;
		}

		if (t2_rc) {
			logerr("hash_init() in pthread_t t2\n");
			rc = -1;
			goto out;
		}

		assert(SEED.initialized == true);                           // 5

		test_ok();
	}

out:
	ret = pthread_mutex_unlock(&test_mutex);
	if (ret) {
		logerr(
			"pthread_mutex_unlock(&test_mutex): %s\n",
			strerror(ret)
		);
		rc = -1;
	}
	return rc;
}

static void
test_hash(void) {
	test_start("hash()");

	{
		testing("we get the same hash given the same input");

		uint8_t out1[OUTPUT_LENGTH] = { 0 };
		uint8_t out2[OUTPUT_LENGTH] = { 0 };

		const char *const input_str = "the input";
		const uint8_t *const input = (uint8_t *)input_str;
		const size_t input_length = strlen(input_str);

		hash(input_length, input, out1);
		hash(input_length, input, out2);

		for (int i = 0; i < OUTPUT_LENGTH; i++) {
			assert(out1[i] == out2[i]);
		}

		test_ok();
	}

	{
		testing("hashing twice overwrites whatever was in `out`");

		uint8_t out1[OUTPUT_LENGTH];
		uint8_t out2[OUTPUT_LENGTH];

		const char *const input_str = "another input";
		const uint8_t *const input = (uint8_t *)input_str;
		const size_t input_length = strlen(input_str);

		hash(6, (uint8_t *)"before", out1);
		hash(input_length, input,    out1);
		hash(input_length, input,    out2);

		for (int i = 0; i < OUTPUT_LENGTH; i++) {
			assert(out1[i] == out2[i]);
		}

		test_ok();
	}

	return;
}

int
main(void) {
	int rc = 0;

	if (test_hash_init()) {
		logerr("test_hash_init();\n");
		rc = -1;
		goto out;
	}

	test_hash();

out:
	return !!rc;
}
#endif