summaryrefslogtreecommitdiff
path: root/lib/nettle/sysrng-netbsd.c
blob: 15bce72438ee92d53be8cee216e776e1a05ef65a (plain)
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
/*
 * Copyright (C) 2010-2016 Free Software Foundation, Inc.
 * Copyright (C) 2015-2016 Red Hat, Inc.
 *
 * Author: Nia Alarie
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 *
 */

/*
 * The *BSD sysctl-based system random generator.
 * Used on NetBSD.
 */

#include "gnutls_int.h"
#include "errors.h"
#include <locks.h>
#include <num.h>
#include <errno.h>
#include <rnd-common.h>

#include <sys/sysctl.h>

#define	ARRAY_SIZE(A)	(sizeof(A)/sizeof((A)[0]))

get_entropy_func _rnd_get_system_entropy = NULL;

static int _rnd_get_system_entropy_sysctl(void *_rnd, size_t size)
{
	static int name[] = { CTL_KERN, KERN_ARND };
	size_t count, req;
	unsigned char *p;

	p = _rnd;
	while (size) {
		req = size < 32 ? size : 32;
		count = req;

		if (sysctl(name, ARRAY_SIZE(name), p, &count, NULL, 0) == -1) {
			return GNUTLS_E_RANDOM_DEVICE_ERROR;
		}

		if (count != req) {
			return GNUTLS_E_RANDOM_DEVICE_ERROR;	/* Can't happen. */
		}

		p += count;
		size -= count;
	}

	return 0;
}

int _rnd_system_entropy_init(void)
{
	_rnd_get_system_entropy = _rnd_get_system_entropy_sysctl;
	return 0;
}

void _rnd_system_entropy_deinit(void)
{
	return;
}