summaryrefslogtreecommitdiff
path: root/src/random.c
blob: 648ec9f8d34ba4fece3de38e69d89d4542af0c02 (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
#include "config.h"

#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "logerr.h"

#include "random.h"


/// A temporary buffer is used to avoid writing to `addr` while having an error
/// when calling `fclose(3)`;
int
urandom_bytes(const size_t n, uint8_t (*const addr)[]) {
	int rc = -1;

	uint8_t *temp = NULL;
	FILE *f = NULL;

	temp = malloc(n);
	if (temp == NULL) {
		logerr("malloc(...): %s\n", strerror(errno));
		goto out;
	}

	f = fopen("/dev/urandom", "r");
	if (f == NULL) {
		logerr("fopen(...): %s\n", strerror(errno));
		goto out;
	}

	const size_t read_count = fread(temp, 1, n, f);
	if (ferror(f)) {
		logerr("fread(...), n, f): %s\n", strerror(errno));
		goto out;
	}
	assert(read_count == n);

	if (fclose(f)) {
		logerr("fclose(...): %s\n", strerror(errno));
		goto out;
	}
	f = NULL;

	memcpy(addr, temp, n);
	rc = 0;
out:
	if (f != NULL) {
		if (fclose(f)) {
			logerr("fclose(...): %s\n", strerror(errno));
			rc = -1;
		}
	}
	if (temp != NULL) {
		free(temp);
	}
	return rc;
}