13b6c3722Schristos /*
23b6c3722Schristos * util/random.c - thread safe random generator, which is reasonably secure.
33b6c3722Schristos *
43b6c3722Schristos * Copyright (c) 2007, NLnet Labs. All rights reserved.
53b6c3722Schristos *
63b6c3722Schristos * This software is open source.
73b6c3722Schristos *
83b6c3722Schristos * Redistribution and use in source and binary forms, with or without
93b6c3722Schristos * modification, are permitted provided that the following conditions
103b6c3722Schristos * are met:
113b6c3722Schristos *
123b6c3722Schristos * Redistributions of source code must retain the above copyright notice,
133b6c3722Schristos * this list of conditions and the following disclaimer.
143b6c3722Schristos *
153b6c3722Schristos * Redistributions in binary form must reproduce the above copyright notice,
163b6c3722Schristos * this list of conditions and the following disclaimer in the documentation
173b6c3722Schristos * and/or other materials provided with the distribution.
183b6c3722Schristos *
193b6c3722Schristos * Neither the name of the NLNET LABS nor the names of its contributors may
203b6c3722Schristos * be used to endorse or promote products derived from this software without
213b6c3722Schristos * specific prior written permission.
223b6c3722Schristos *
233b6c3722Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
243b6c3722Schristos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
253b6c3722Schristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
263b6c3722Schristos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
273b6c3722Schristos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
283b6c3722Schristos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
293b6c3722Schristos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
303b6c3722Schristos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
313b6c3722Schristos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
323b6c3722Schristos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
333b6c3722Schristos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
343b6c3722Schristos */
353b6c3722Schristos
363b6c3722Schristos /**
373b6c3722Schristos * \file
383b6c3722Schristos * Thread safe random functions. Similar to arc4random() with an explicit
393b6c3722Schristos * initialisation routine.
403b6c3722Schristos *
413b6c3722Schristos * The code in this file is based on arc4random from
423b6c3722Schristos * openssh-4.0p1/openbsd-compat/bsd-arc4random.c
433b6c3722Schristos * That code is also BSD licensed. Here is their statement:
443b6c3722Schristos *
453b6c3722Schristos * Copyright (c) 1996, David Mazieres <dm@uun.org>
463b6c3722Schristos * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
473b6c3722Schristos *
483b6c3722Schristos * Permission to use, copy, modify, and distribute this software for any
493b6c3722Schristos * purpose with or without fee is hereby granted, provided that the above
503b6c3722Schristos * copyright notice and this permission notice appear in all copies.
513b6c3722Schristos *
523b6c3722Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
533b6c3722Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
543b6c3722Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
553b6c3722Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
563b6c3722Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
573b6c3722Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
583b6c3722Schristos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
593b6c3722Schristos */
603b6c3722Schristos #include "config.h"
613b6c3722Schristos #include "util/random.h"
623b6c3722Schristos #include "util/log.h"
633b6c3722Schristos #include <time.h>
643b6c3722Schristos
653b6c3722Schristos #ifdef HAVE_NSS
663b6c3722Schristos /* nspr4 */
673b6c3722Schristos #include "prerror.h"
683b6c3722Schristos /* nss3 */
693b6c3722Schristos #include "secport.h"
703b6c3722Schristos #include "pk11pub.h"
713b6c3722Schristos #elif defined(HAVE_NETTLE)
723b6c3722Schristos #include "yarrow.h"
733b6c3722Schristos #endif
743b6c3722Schristos
753b6c3722Schristos /**
763b6c3722Schristos * Max random value. Similar to RAND_MAX, but more portable
773b6c3722Schristos * (mingw uses only 15 bits random).
783b6c3722Schristos */
793b6c3722Schristos #define MAX_VALUE 0x7fffffff
803b6c3722Schristos
81*d0eba39bSchristos #if defined(HAVE_SSL) || defined(HAVE_LIBBSD)
823b6c3722Schristos struct ub_randstate*
ub_initstate(struct ub_randstate * ATTR_UNUSED (from))8301049ae6Schristos ub_initstate(struct ub_randstate* ATTR_UNUSED(from))
843b6c3722Schristos {
853b6c3722Schristos struct ub_randstate* s = (struct ub_randstate*)malloc(1);
863b6c3722Schristos if(!s) {
873b6c3722Schristos log_err("malloc failure in random init");
883b6c3722Schristos return NULL;
893b6c3722Schristos }
903b6c3722Schristos return s;
913b6c3722Schristos }
923b6c3722Schristos
933b6c3722Schristos long int
ub_random(struct ub_randstate * ATTR_UNUSED (s))943b6c3722Schristos ub_random(struct ub_randstate* ATTR_UNUSED(s))
953b6c3722Schristos {
963b6c3722Schristos /* This relies on MAX_VALUE being 0x7fffffff. */
973b6c3722Schristos return (long)arc4random() & MAX_VALUE;
983b6c3722Schristos }
993b6c3722Schristos
1003b6c3722Schristos long int
ub_random_max(struct ub_randstate * state,long int x)1013b6c3722Schristos ub_random_max(struct ub_randstate* state, long int x)
1023b6c3722Schristos {
1033b6c3722Schristos (void)state;
1043b6c3722Schristos /* on OpenBSD, this does not need _seed(), or _stir() calls */
1053b6c3722Schristos return (long)arc4random_uniform((uint32_t)x);
1063b6c3722Schristos }
1073b6c3722Schristos
1083b6c3722Schristos #elif defined(HAVE_NSS)
1093b6c3722Schristos
1103b6c3722Schristos /* not much to remember for NSS since we use its pk11_random, placeholder */
1113b6c3722Schristos struct ub_randstate {
1123b6c3722Schristos int ready;
1133b6c3722Schristos };
1143b6c3722Schristos
ub_initstate(struct ub_randstate * ATTR_UNUSED (from))11501049ae6Schristos struct ub_randstate* ub_initstate(struct ub_randstate* ATTR_UNUSED(from))
1163b6c3722Schristos {
1173b6c3722Schristos struct ub_randstate* s = (struct ub_randstate*)calloc(1, sizeof(*s));
1183b6c3722Schristos if(!s) {
1193b6c3722Schristos log_err("malloc failure in random init");
1203b6c3722Schristos return NULL;
1213b6c3722Schristos }
1223b6c3722Schristos return s;
1233b6c3722Schristos }
1243b6c3722Schristos
ub_random(struct ub_randstate * ATTR_UNUSED (state))1253b6c3722Schristos long int ub_random(struct ub_randstate* ATTR_UNUSED(state))
1263b6c3722Schristos {
1273b6c3722Schristos long int x;
1283b6c3722Schristos /* random 31 bit value. */
1293b6c3722Schristos SECStatus s = PK11_GenerateRandom((unsigned char*)&x, (int)sizeof(x));
1303b6c3722Schristos if(s != SECSuccess) {
13101049ae6Schristos /* unbound needs secure randomness for randomized
13201049ae6Schristos * ID bits and port numbers in packets to upstream servers */
13301049ae6Schristos fatal_exit("PK11_GenerateRandom error: %s",
1343b6c3722Schristos PORT_ErrorToString(PORT_GetError()));
1353b6c3722Schristos }
1363b6c3722Schristos return x & MAX_VALUE;
1373b6c3722Schristos }
1383b6c3722Schristos
1393b6c3722Schristos #elif defined(HAVE_NETTLE)
1403b6c3722Schristos
1413b6c3722Schristos /**
1423b6c3722Schristos * libnettle implements a Yarrow-256 generator (SHA256 + AES),
1433b6c3722Schristos * and we have to ensure it is seeded before use.
1443b6c3722Schristos */
1453b6c3722Schristos struct ub_randstate {
1463b6c3722Schristos struct yarrow256_ctx ctx;
1473b6c3722Schristos int seeded;
1483b6c3722Schristos };
1493b6c3722Schristos
ub_initstate(struct ub_randstate * ATTR_UNUSED (from))15001049ae6Schristos struct ub_randstate* ub_initstate(struct ub_randstate* ATTR_UNUSED(from))
1513b6c3722Schristos {
1523b6c3722Schristos struct ub_randstate* s = (struct ub_randstate*)calloc(1, sizeof(*s));
1533b6c3722Schristos uint8_t buf[YARROW256_SEED_FILE_SIZE];
1543b6c3722Schristos if(!s) {
1553b6c3722Schristos log_err("malloc failure in random init");
1563b6c3722Schristos return NULL;
1573b6c3722Schristos }
1583b6c3722Schristos /* Setup Yarrow context */
1593b6c3722Schristos yarrow256_init(&s->ctx, 0, NULL);
1603b6c3722Schristos
1613b6c3722Schristos if(getentropy(buf, sizeof(buf)) != -1) {
1623b6c3722Schristos /* got entropy */
1633b6c3722Schristos yarrow256_seed(&s->ctx, YARROW256_SEED_FILE_SIZE, buf);
1643b6c3722Schristos s->seeded = yarrow256_is_seeded(&s->ctx);
1653b6c3722Schristos } else {
16601049ae6Schristos log_err("nettle random(yarrow) cannot initialize, "
16701049ae6Schristos "getentropy failed: %s", strerror(errno));
16801049ae6Schristos free(s);
16901049ae6Schristos return NULL;
1703b6c3722Schristos }
1713b6c3722Schristos
1723b6c3722Schristos return s;
1733b6c3722Schristos }
1743b6c3722Schristos
ub_random(struct ub_randstate * s)1753b6c3722Schristos long int ub_random(struct ub_randstate* s)
1763b6c3722Schristos {
1773b6c3722Schristos /* random 31 bit value. */
1783b6c3722Schristos long int x = 0;
1793b6c3722Schristos if (!s || !s->seeded) {
1803b6c3722Schristos log_err("Couldn't generate randomness, Yarrow-256 generator not yet seeded");
1813b6c3722Schristos } else {
1823b6c3722Schristos yarrow256_random(&s->ctx, sizeof(x), (uint8_t *)&x);
1833b6c3722Schristos }
1843b6c3722Schristos return x & MAX_VALUE;
1853b6c3722Schristos }
186*d0eba39bSchristos #endif /* HAVE_SSL or HAVE_LIBBSD or HAVE_NSS or HAVE_NETTLE */
1873b6c3722Schristos
1883b6c3722Schristos
189*d0eba39bSchristos #if defined(HAVE_NSS) || defined(HAVE_NETTLE) && !defined(HAVE_LIBBSD)
1903b6c3722Schristos long int
ub_random_max(struct ub_randstate * state,long int x)1913b6c3722Schristos ub_random_max(struct ub_randstate* state, long int x)
1923b6c3722Schristos {
1933b6c3722Schristos /* make sure we fetch in a range that is divisible by x. ignore
1943b6c3722Schristos * values from d .. MAX_VALUE, instead draw a new number */
1953b6c3722Schristos long int d = MAX_VALUE - (MAX_VALUE % x); /* d is divisible by x */
1963b6c3722Schristos long int v = ub_random(state);
1973b6c3722Schristos while(d <= v)
1983b6c3722Schristos v = ub_random(state);
1993b6c3722Schristos return (v % x);
2003b6c3722Schristos }
201*d0eba39bSchristos #endif /* HAVE_NSS or HAVE_NETTLE and !HAVE_LIBBSD */
2023b6c3722Schristos
2033b6c3722Schristos void
ub_randfree(struct ub_randstate * s)2043b6c3722Schristos ub_randfree(struct ub_randstate* s)
2053b6c3722Schristos {
2063b6c3722Schristos free(s);
2073b6c3722Schristos /* user app must do RAND_cleanup(); */
2083b6c3722Schristos }
209