1999-10-20 23:25:17 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-10-20 23:25:17 +00:00
|
|
|
*/
|
2014-06-04 23:45:22 +00:00
|
|
|
|
2018-04-22 14:56:28 +02:00
|
|
|
/*
|
|
|
|
* Portions of isc_random_uniform():
|
2014-06-04 13:38:59 +05:30
|
|
|
*
|
|
|
|
* Copyright (c) 1996, David Mazieres <dm@uun.org>
|
|
|
|
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2005-04-27 04:57:32 +00:00
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
1999-10-20 23:25:17 +00:00
|
|
|
#include <stdlib.h>
|
2018-05-28 15:22:23 +02:00
|
|
|
#include <string.h>
|
2003-10-01 00:58:07 +00:00
|
|
|
#include <unistd.h>
|
1999-10-20 23:25:17 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/once.h>
|
2018-05-29 14:10:32 +02:00
|
|
|
#include <isc/platform.h>
|
2018-04-22 14:56:28 +02:00
|
|
|
#include <isc/random.h>
|
|
|
|
#include <isc/result.h>
|
2019-12-02 11:42:50 +01:00
|
|
|
#include <isc/thread.h>
|
2018-04-22 14:56:28 +02:00
|
|
|
#include <isc/types.h>
|
|
|
|
#include <isc/util.h>
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
#include "entropy_private.h"
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
/*
|
|
|
|
* The specific implementation for PRNG is included as a C file
|
|
|
|
* that has to provide a static variable named seed, and a function
|
|
|
|
* uint32_t next(void) that provides next random number.
|
|
|
|
*
|
|
|
|
* The implementation must be thread-safe.
|
|
|
|
*/
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
/*
|
|
|
|
* Two contestants have been considered: the xoroshiro family of the
|
|
|
|
* functions by Villa&Blackman, and PCG by O'Neill. After
|
|
|
|
* consideration, the xoshiro128starstar function has been chosen as
|
|
|
|
* the uint32_t random number provider because it is very fast and has
|
|
|
|
* good enough properties for our usage pattern.
|
|
|
|
*/
|
|
|
|
#include "xoshiro128starstar.c"
|
2014-06-11 21:42:36 +10:00
|
|
|
|
2019-12-02 11:42:50 +01:00
|
|
|
ISC_THREAD_LOCAL isc_once_t isc_random_once = ISC_ONCE_INIT;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_random_initialize(void)
|
|
|
|
{
|
|
|
|
int useed[4] = { 0, 0, 0, 1 };
|
2018-07-03 15:45:11 +02:00
|
|
|
#if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
2018-11-28 18:27:06 +00:00
|
|
|
/*
|
|
|
|
* Set a constant seed to help in problem reproduction should fuzzing
|
|
|
|
* find a crash or a hang. The seed array must be non-zero else
|
|
|
|
* xoshiro128starstar will generate an infinite series of zeroes.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
2018-08-24 11:21:27 +02:00
|
|
|
isc_entropy_get(useed, sizeof(useed));
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
2019-07-12 15:28:00 +02:00
|
|
|
memmove(seed, useed, sizeof(seed));
|
2018-05-28 15:22:23 +02:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
uint8_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_random8(void)
|
|
|
|
{
|
|
|
|
RUNTIME_CHECK(isc_once_do(&isc_random_once, isc_random_initialize) ==
|
|
|
|
ISC_R_SUCCESS);
|
2018-05-28 15:22:23 +02:00
|
|
|
return (next() & 0xff);
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
uint16_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_random16(void)
|
|
|
|
{
|
|
|
|
RUNTIME_CHECK(isc_once_do(&isc_random_once, isc_random_initialize) ==
|
|
|
|
ISC_R_SUCCESS);
|
2018-05-28 15:22:23 +02:00
|
|
|
return (next() & 0xffff);
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_random32(void)
|
|
|
|
{
|
|
|
|
RUNTIME_CHECK(isc_once_do(&isc_random_once, isc_random_initialize) ==
|
|
|
|
ISC_R_SUCCESS);
|
2018-05-28 15:22:23 +02:00
|
|
|
return (next());
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_random_buf(void *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
int i;
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t r;
|
2018-05-30 07:03:35 +02:00
|
|
|
|
2018-06-04 13:41:09 +02:00
|
|
|
REQUIRE(buf != NULL);
|
2018-04-22 14:56:28 +02:00
|
|
|
REQUIRE(buflen > 0);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
RUNTIME_CHECK(isc_once_do(&isc_random_once, isc_random_initialize) ==
|
|
|
|
ISC_R_SUCCESS);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-05-28 15:22:23 +02:00
|
|
|
for (i = 0; i + sizeof(r) <= buflen; i += sizeof(r)) {
|
|
|
|
r = next();
|
2018-04-17 08:29:14 -07:00
|
|
|
memmove((uint8_t *)buf + i, &r, sizeof(r));
|
2018-05-22 12:00:00 +02:00
|
|
|
}
|
2018-05-28 15:22:23 +02:00
|
|
|
r = next();
|
2018-04-17 08:29:14 -07:00
|
|
|
memmove((uint8_t *)buf + i, &r, buflen % sizeof(r));
|
2018-05-28 15:22:23 +02:00
|
|
|
return;
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_random_uniform(uint32_t upper_bound)
|
|
|
|
{
|
2018-04-22 14:56:28 +02:00
|
|
|
/* Copy of arc4random_uniform from OpenBSD */
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t r, min;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
RUNTIME_CHECK(isc_once_do(&isc_random_once, isc_random_initialize) ==
|
|
|
|
ISC_R_SUCCESS);
|
2018-05-28 15:22:23 +02:00
|
|
|
|
2018-04-22 14:56:28 +02:00
|
|
|
if (upper_bound < 2) {
|
2014-06-04 13:38:59 +05:30
|
|
|
return (0);
|
2018-04-22 14:56:28 +02:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2018-04-22 14:56:28 +02:00
|
|
|
#if (ULONG_MAX > 0xffffffffUL)
|
|
|
|
min = 0x100000000UL % upper_bound;
|
|
|
|
#else /* if (ULONG_MAX > 0xffffffffUL) */
|
|
|
|
/* Calculate (2**32 % upper_bound) avoiding 64-bit math */
|
|
|
|
if (upper_bound > 0x80000000) {
|
2020-02-12 13:59:18 +01:00
|
|
|
min = 1 + ~upper_bound; /* 2**32 - upper_bound */
|
2018-04-22 14:56:28 +02:00
|
|
|
} else {
|
|
|
|
/* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */
|
|
|
|
min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;
|
|
|
|
}
|
|
|
|
#endif /* if (ULONG_MAX > 0xffffffffUL) */
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* This could theoretically loop forever but each retry has
|
|
|
|
* p > 0.5 (worst case, usually far better) of selecting a
|
|
|
|
* number inside the range we need, so it should rarely need
|
|
|
|
* to re-roll.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
2018-05-28 15:22:23 +02:00
|
|
|
r = next();
|
2018-04-22 14:56:28 +02:00
|
|
|
if (r >= min) {
|
2014-06-04 13:38:59 +05:30
|
|
|
break;
|
2018-04-22 14:56:28 +02:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return (r % upper_bound);
|
|
|
|
}
|