xref: /openbsd-src/sbin/unwind/libunbound/util/random.c (revision d32eb43c98fe133df5eab7dc88533129ee17734e)
1ae8c6e27Sflorian /*
2ae8c6e27Sflorian  * util/random.c - thread safe random generator, which is reasonably secure.
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * This software is open source.
7ae8c6e27Sflorian  *
8ae8c6e27Sflorian  * Redistribution and use in source and binary forms, with or without
9ae8c6e27Sflorian  * modification, are permitted provided that the following conditions
10ae8c6e27Sflorian  * are met:
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Redistributions of source code must retain the above copyright notice,
13ae8c6e27Sflorian  * this list of conditions and the following disclaimer.
14ae8c6e27Sflorian  *
15ae8c6e27Sflorian  * Redistributions in binary form must reproduce the above copyright notice,
16ae8c6e27Sflorian  * this list of conditions and the following disclaimer in the documentation
17ae8c6e27Sflorian  * and/or other materials provided with the distribution.
18ae8c6e27Sflorian  *
19ae8c6e27Sflorian  * Neither the name of the NLNET LABS nor the names of its contributors may
20ae8c6e27Sflorian  * be used to endorse or promote products derived from this software without
21ae8c6e27Sflorian  * specific prior written permission.
22ae8c6e27Sflorian  *
23ae8c6e27Sflorian  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24ae8c6e27Sflorian  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25ae8c6e27Sflorian  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26ae8c6e27Sflorian  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27ae8c6e27Sflorian  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28ae8c6e27Sflorian  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29ae8c6e27Sflorian  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30ae8c6e27Sflorian  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31ae8c6e27Sflorian  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32ae8c6e27Sflorian  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33ae8c6e27Sflorian  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ae8c6e27Sflorian  */
35ae8c6e27Sflorian 
36ae8c6e27Sflorian /**
37ae8c6e27Sflorian  * \file
38ae8c6e27Sflorian  * Thread safe random functions. Similar to arc4random() with an explicit
39ae8c6e27Sflorian  * initialisation routine.
40ae8c6e27Sflorian  *
41ae8c6e27Sflorian  * The code in this file is based on arc4random from
42ae8c6e27Sflorian  * openssh-4.0p1/openbsd-compat/bsd-arc4random.c
43ae8c6e27Sflorian  * That code is also BSD licensed. Here is their statement:
44ae8c6e27Sflorian  *
45ae8c6e27Sflorian  * Copyright (c) 1996, David Mazieres <dm@uun.org>
46ae8c6e27Sflorian  * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
47ae8c6e27Sflorian  *
48ae8c6e27Sflorian  * Permission to use, copy, modify, and distribute this software for any
49ae8c6e27Sflorian  * purpose with or without fee is hereby granted, provided that the above
50ae8c6e27Sflorian  * copyright notice and this permission notice appear in all copies.
51ae8c6e27Sflorian  *
52ae8c6e27Sflorian  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
53ae8c6e27Sflorian  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
54ae8c6e27Sflorian  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
55ae8c6e27Sflorian  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
56ae8c6e27Sflorian  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
57ae8c6e27Sflorian  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
58ae8c6e27Sflorian  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
59ae8c6e27Sflorian  */
60ae8c6e27Sflorian #include "config.h"
61ae8c6e27Sflorian #include "util/random.h"
62ae8c6e27Sflorian #include "util/log.h"
63ae8c6e27Sflorian #include <time.h>
64ae8c6e27Sflorian 
65ae8c6e27Sflorian #ifdef HAVE_NSS
66ae8c6e27Sflorian /* nspr4 */
67ae8c6e27Sflorian #include "prerror.h"
68ae8c6e27Sflorian /* nss3 */
69ae8c6e27Sflorian #include "secport.h"
70ae8c6e27Sflorian #include "pk11pub.h"
71ae8c6e27Sflorian #elif defined(HAVE_NETTLE)
72ae8c6e27Sflorian #include "yarrow.h"
73ae8c6e27Sflorian #endif
74ae8c6e27Sflorian 
75ae8c6e27Sflorian /**
76ae8c6e27Sflorian  * Max random value.  Similar to RAND_MAX, but more portable
77ae8c6e27Sflorian  * (mingw uses only 15 bits random).
78ae8c6e27Sflorian  */
79ae8c6e27Sflorian #define MAX_VALUE 0x7fffffff
80ae8c6e27Sflorian 
81*d32eb43cSflorian #if defined(HAVE_SSL) || defined(HAVE_LIBBSD)
82ae8c6e27Sflorian struct ub_randstate*
ub_initstate(struct ub_randstate * ATTR_UNUSED (from))8357403691Sflorian ub_initstate(struct ub_randstate* ATTR_UNUSED(from))
84ae8c6e27Sflorian {
85ae8c6e27Sflorian 	struct ub_randstate* s = (struct ub_randstate*)malloc(1);
86ae8c6e27Sflorian 	if(!s) {
87ae8c6e27Sflorian 		log_err("malloc failure in random init");
88ae8c6e27Sflorian 		return NULL;
89ae8c6e27Sflorian 	}
90ae8c6e27Sflorian 	return s;
91ae8c6e27Sflorian }
92ae8c6e27Sflorian 
93ae8c6e27Sflorian long int
ub_random(struct ub_randstate * ATTR_UNUSED (s))94ae8c6e27Sflorian ub_random(struct ub_randstate* ATTR_UNUSED(s))
95ae8c6e27Sflorian {
96ae8c6e27Sflorian 	/* This relies on MAX_VALUE being 0x7fffffff. */
97ae8c6e27Sflorian 	return (long)arc4random() & MAX_VALUE;
98ae8c6e27Sflorian }
99ae8c6e27Sflorian 
100ae8c6e27Sflorian long int
ub_random_max(struct ub_randstate * state,long int x)101ae8c6e27Sflorian ub_random_max(struct ub_randstate* state, long int x)
102ae8c6e27Sflorian {
103ae8c6e27Sflorian 	(void)state;
104ae8c6e27Sflorian 	/* on OpenBSD, this does not need _seed(), or _stir() calls */
105ae8c6e27Sflorian 	return (long)arc4random_uniform((uint32_t)x);
106ae8c6e27Sflorian }
107ae8c6e27Sflorian 
108ae8c6e27Sflorian #elif defined(HAVE_NSS)
109ae8c6e27Sflorian 
110ae8c6e27Sflorian /* not much to remember for NSS since we use its pk11_random, placeholder */
111ae8c6e27Sflorian struct ub_randstate {
112ae8c6e27Sflorian 	int ready;
113ae8c6e27Sflorian };
114ae8c6e27Sflorian 
ub_initstate(struct ub_randstate * ATTR_UNUSED (from))11557403691Sflorian struct ub_randstate* ub_initstate(struct ub_randstate* ATTR_UNUSED(from))
116ae8c6e27Sflorian {
117ae8c6e27Sflorian 	struct ub_randstate* s = (struct ub_randstate*)calloc(1, sizeof(*s));
118ae8c6e27Sflorian 	if(!s) {
119ae8c6e27Sflorian 		log_err("malloc failure in random init");
120ae8c6e27Sflorian 		return NULL;
121ae8c6e27Sflorian 	}
122ae8c6e27Sflorian 	return s;
123ae8c6e27Sflorian }
124ae8c6e27Sflorian 
ub_random(struct ub_randstate * ATTR_UNUSED (state))125ae8c6e27Sflorian long int ub_random(struct ub_randstate* ATTR_UNUSED(state))
126ae8c6e27Sflorian {
127ae8c6e27Sflorian 	long int x;
128ae8c6e27Sflorian 	/* random 31 bit value. */
129ae8c6e27Sflorian 	SECStatus s = PK11_GenerateRandom((unsigned char*)&x, (int)sizeof(x));
130ae8c6e27Sflorian 	if(s != SECSuccess) {
13157403691Sflorian 		/* unbound needs secure randomness for randomized
13257403691Sflorian 		 * ID bits and port numbers in packets to upstream servers */
13357403691Sflorian 		fatal_exit("PK11_GenerateRandom error: %s",
134ae8c6e27Sflorian 			PORT_ErrorToString(PORT_GetError()));
135ae8c6e27Sflorian 	}
136ae8c6e27Sflorian 	return x & MAX_VALUE;
137ae8c6e27Sflorian }
138ae8c6e27Sflorian 
139ae8c6e27Sflorian #elif defined(HAVE_NETTLE)
140ae8c6e27Sflorian 
141ae8c6e27Sflorian /**
142ae8c6e27Sflorian  * libnettle implements a Yarrow-256 generator (SHA256 + AES),
143ae8c6e27Sflorian  * and we have to ensure it is seeded before use.
144ae8c6e27Sflorian  */
145ae8c6e27Sflorian struct ub_randstate {
146ae8c6e27Sflorian 	struct yarrow256_ctx ctx;
147ae8c6e27Sflorian 	int seeded;
148ae8c6e27Sflorian };
149ae8c6e27Sflorian 
ub_initstate(struct ub_randstate * ATTR_UNUSED (from))15057403691Sflorian struct ub_randstate* ub_initstate(struct ub_randstate* ATTR_UNUSED(from))
151ae8c6e27Sflorian {
152ae8c6e27Sflorian 	struct ub_randstate* s = (struct ub_randstate*)calloc(1, sizeof(*s));
153ae8c6e27Sflorian 	uint8_t buf[YARROW256_SEED_FILE_SIZE];
154ae8c6e27Sflorian 	if(!s) {
155ae8c6e27Sflorian 		log_err("malloc failure in random init");
156ae8c6e27Sflorian 		return NULL;
157ae8c6e27Sflorian 	}
158ae8c6e27Sflorian 	/* Setup Yarrow context */
159ae8c6e27Sflorian 	yarrow256_init(&s->ctx, 0, NULL);
160ae8c6e27Sflorian 
161ae8c6e27Sflorian 	if(getentropy(buf, sizeof(buf)) != -1) {
162ae8c6e27Sflorian 		/* got entropy */
163ae8c6e27Sflorian 		yarrow256_seed(&s->ctx, YARROW256_SEED_FILE_SIZE, buf);
164ae8c6e27Sflorian 		s->seeded = yarrow256_is_seeded(&s->ctx);
165ae8c6e27Sflorian 	} else {
16657403691Sflorian 		log_err("nettle random(yarrow) cannot initialize, "
16757403691Sflorian 			"getentropy failed: %s", strerror(errno));
16857403691Sflorian 		free(s);
16957403691Sflorian 		return NULL;
170ae8c6e27Sflorian 	}
171ae8c6e27Sflorian 
172ae8c6e27Sflorian 	return s;
173ae8c6e27Sflorian }
174ae8c6e27Sflorian 
ub_random(struct ub_randstate * s)175ae8c6e27Sflorian long int ub_random(struct ub_randstate* s)
176ae8c6e27Sflorian {
177ae8c6e27Sflorian 	/* random 31 bit value. */
178ae8c6e27Sflorian 	long int x = 0;
179ae8c6e27Sflorian 	if (!s || !s->seeded) {
180ae8c6e27Sflorian 		log_err("Couldn't generate randomness, Yarrow-256 generator not yet seeded");
181ae8c6e27Sflorian 	} else {
182ae8c6e27Sflorian 		yarrow256_random(&s->ctx, sizeof(x), (uint8_t *)&x);
183ae8c6e27Sflorian 	}
184ae8c6e27Sflorian 	return x & MAX_VALUE;
185ae8c6e27Sflorian }
186*d32eb43cSflorian #endif /* HAVE_SSL or HAVE_LIBBSD or HAVE_NSS or HAVE_NETTLE */
187ae8c6e27Sflorian 
188ae8c6e27Sflorian 
189*d32eb43cSflorian #if defined(HAVE_NSS) || defined(HAVE_NETTLE) && !defined(HAVE_LIBBSD)
190ae8c6e27Sflorian long int
ub_random_max(struct ub_randstate * state,long int x)191ae8c6e27Sflorian ub_random_max(struct ub_randstate* state, long int x)
192ae8c6e27Sflorian {
193ae8c6e27Sflorian 	/* make sure we fetch in a range that is divisible by x. ignore
194ae8c6e27Sflorian 	 * values from d .. MAX_VALUE, instead draw a new number */
195ae8c6e27Sflorian 	long int d = MAX_VALUE - (MAX_VALUE % x); /* d is divisible by x */
196ae8c6e27Sflorian 	long int v = ub_random(state);
197ae8c6e27Sflorian 	while(d <= v)
198ae8c6e27Sflorian 		v = ub_random(state);
199ae8c6e27Sflorian 	return (v % x);
200ae8c6e27Sflorian }
201*d32eb43cSflorian #endif /* HAVE_NSS or HAVE_NETTLE and !HAVE_LIBBSD */
202ae8c6e27Sflorian 
203ae8c6e27Sflorian void
ub_randfree(struct ub_randstate * s)204ae8c6e27Sflorian ub_randfree(struct ub_randstate* s)
205ae8c6e27Sflorian {
206ae8c6e27Sflorian 	free(s);
207ae8c6e27Sflorian 	/* user app must do RAND_cleanup(); */
208ae8c6e27Sflorian }
209