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
|
|
|
|
2014-06-04 13:38:59 +05:30
|
|
|
/*%
|
|
|
|
* ChaCha based random number generator derived from OpenBSD.
|
|
|
|
*
|
|
|
|
* The original copyright follows:
|
|
|
|
* Copyright (c) 1996, David Mazieres <dm@uun.org>
|
|
|
|
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
|
|
|
|
* Copyright (c) 2013, Markus Friedl <markus@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
|
|
|
|
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1999-10-20 23:25:17 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2000-12-26 21:02:14 +00:00
|
|
|
#include <time.h> /* Required for time(). */
|
2003-10-01 00:58:07 +00:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1999-10-20 23:25:17 +00:00
|
|
|
|
2014-06-04 13:38:59 +05:30
|
|
|
#include <isc/magic.h>
|
1999-10-20 23:25:17 +00:00
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/once.h>
|
2014-06-04 13:38:59 +05:30
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/entropy.h>
|
1999-10-20 23:25:17 +00:00
|
|
|
#include <isc/random.h>
|
2017-09-19 16:16:45 +05:30
|
|
|
#include <isc/safe.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1999-10-20 23:25:17 +00:00
|
|
|
|
2014-06-04 13:38:59 +05:30
|
|
|
#define RNG_MAGIC ISC_MAGIC('R', 'N', 'G', 'x')
|
|
|
|
#define VALID_RNG(r) ISC_MAGIC_VALID(r, RNG_MAGIC)
|
|
|
|
|
|
|
|
#define KEYSTREAM_ONLY
|
|
|
|
#include "chacha_private.h"
|
|
|
|
|
2014-06-10 12:38:32 +10:00
|
|
|
#define CHACHA_KEYSIZE 32U
|
|
|
|
#define CHACHA_IVSIZE 8U
|
2014-06-04 13:38:59 +05:30
|
|
|
#define CHACHA_BLOCKSIZE 64
|
|
|
|
#define CHACHA_BUFFERSIZE (16 * CHACHA_BLOCKSIZE)
|
2017-11-06 10:44:37 -08:00
|
|
|
#define CHACHA_MAXHAVE (CHACHA_BUFFERSIZE - CHACHA_KEYSIZE - CHACHA_IVSIZE)
|
|
|
|
/*
|
|
|
|
* Derived from OpenBSD's implementation. The rationale is not clear,
|
|
|
|
* but should be conservative enough in safety, and reasonably large for
|
|
|
|
* efficiency.
|
|
|
|
*/
|
|
|
|
#define CHACHA_MAXLENGTH 1600000
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
/* ChaCha RNG state */
|
|
|
|
struct isc_rng {
|
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t *mctx;
|
|
|
|
chacha_ctx cpctx;
|
|
|
|
isc_uint8_t buffer[CHACHA_BUFFERSIZE];
|
|
|
|
size_t have;
|
|
|
|
unsigned int references;
|
|
|
|
int count;
|
|
|
|
isc_entropy_t *entropy; /*%< entropy source */
|
|
|
|
isc_mutex_t lock;
|
|
|
|
};
|
|
|
|
|
1999-10-20 23:25:17 +00:00
|
|
|
static isc_once_t once = ISC_ONCE_INIT;
|
|
|
|
|
|
|
|
static void
|
2014-06-04 13:38:59 +05:30
|
|
|
initialize_rand(void) {
|
2002-12-04 01:19:28 +00:00
|
|
|
#ifndef HAVE_ARC4RANDOM
|
2003-08-05 00:08:30 +00:00
|
|
|
unsigned int pid = getpid();
|
2009-07-01 23:47:36 +00:00
|
|
|
|
2003-08-05 00:08:30 +00:00
|
|
|
/*
|
|
|
|
* The low bits of pid generally change faster.
|
|
|
|
* Xor them with the high bits of time which change slowly.
|
|
|
|
*/
|
|
|
|
pid = ((pid << 16) & 0xffff0000) | ((pid >> 16) & 0xffff);
|
|
|
|
|
2013-12-04 12:47:23 +11:00
|
|
|
srand((unsigned)time(NULL) ^ pid);
|
2002-12-04 01:19:28 +00:00
|
|
|
#endif
|
1999-10-20 23:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-06-04 13:38:59 +05:30
|
|
|
initialize(void) {
|
1999-10-20 23:25:17 +00:00
|
|
|
RUNTIME_CHECK(isc_once_do(&once, initialize_rand) == ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-04 13:38:59 +05:30
|
|
|
isc_random_seed(isc_uint32_t seed) {
|
1999-10-20 23:25:17 +00:00
|
|
|
initialize();
|
|
|
|
|
2002-12-04 01:19:28 +00:00
|
|
|
#ifndef HAVE_ARC4RANDOM
|
1999-10-20 23:25:17 +00:00
|
|
|
srand(seed);
|
2016-11-01 14:00:16 -07:00
|
|
|
#elif defined(HAVE_ARC4RANDOM_STIR)
|
|
|
|
/* Formally not necessary... */
|
|
|
|
UNUSED(seed);
|
|
|
|
arc4random_stir();
|
2014-05-07 08:58:25 -07:00
|
|
|
#elif defined(HAVE_ARC4RANDOM_ADDRANDOM)
|
2002-12-04 01:19:28 +00:00
|
|
|
arc4random_addrandom((u_char *) &seed, sizeof(isc_uint32_t));
|
2016-11-01 22:34:33 -07:00
|
|
|
#else
|
|
|
|
/*
|
2016-11-08 09:06:23 -08:00
|
|
|
* If arc4random() is available and no corresponding seeding
|
2016-11-02 23:46:39 +00:00
|
|
|
* function arc4random_addrandom() is available, no seeding is
|
|
|
|
* done on such platforms (e.g., OpenBSD 5.5). This is because
|
|
|
|
* the OS itself is supposed to seed the RNG and it is assumed
|
|
|
|
* that no explicit seeding is required.
|
|
|
|
*/
|
2016-11-01 22:34:33 -07:00
|
|
|
UNUSED(seed);
|
2002-12-04 01:19:28 +00:00
|
|
|
#endif
|
1999-10-20 23:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-04 13:38:59 +05:30
|
|
|
isc_random_get(isc_uint32_t *val) {
|
1999-10-20 23:25:17 +00:00
|
|
|
REQUIRE(val != NULL);
|
|
|
|
|
|
|
|
initialize();
|
|
|
|
|
2002-12-04 01:19:28 +00:00
|
|
|
#ifndef HAVE_ARC4RANDOM
|
2002-12-05 04:36:26 +00:00
|
|
|
/*
|
|
|
|
* rand()'s lower bits are not random.
|
|
|
|
* rand()'s upper bit is zero.
|
|
|
|
*/
|
2009-07-16 05:52:46 +00:00
|
|
|
#if RAND_MAX >= 0xfffff
|
|
|
|
/* We have at least 20 bits. Use lower 16 excluding lower most 4 */
|
2017-12-06 21:00:14 +11:00
|
|
|
*val = ((((unsigned int)rand()) & 0xffff0) >> 4) |
|
|
|
|
((((unsigned int)rand()) & 0xffff0) << 12);
|
2009-07-16 05:52:46 +00:00
|
|
|
#elif RAND_MAX >= 0x7fff
|
|
|
|
/* We have at least 15 bits. Use lower 10/11 excluding lower most 4 */
|
|
|
|
*val = ((rand() >> 4) & 0x000007ff) | ((rand() << 7) & 0x003ff800) |
|
|
|
|
((rand() << 18) & 0xffc00000);
|
|
|
|
#else
|
|
|
|
#error RAND_MAX is too small
|
|
|
|
#endif
|
2002-12-04 01:19:28 +00:00
|
|
|
#else
|
|
|
|
*val = arc4random();
|
|
|
|
#endif
|
2000-09-06 02:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_uint32_t
|
2000-09-26 17:23:19 +00:00
|
|
|
isc_random_jitter(isc_uint32_t max, isc_uint32_t jitter) {
|
2009-07-16 05:52:46 +00:00
|
|
|
isc_uint32_t rnd;
|
|
|
|
|
2009-06-30 02:53:46 +00:00
|
|
|
REQUIRE(jitter < max || (jitter == 0 && max == 0));
|
2009-07-16 05:52:46 +00:00
|
|
|
|
2000-09-08 00:06:39 +00:00
|
|
|
if (jitter == 0)
|
2000-09-26 17:23:19 +00:00
|
|
|
return (max);
|
2009-07-16 05:52:46 +00:00
|
|
|
|
|
|
|
isc_random_get(&rnd);
|
|
|
|
return (max - rnd % jitter);
|
1999-10-20 23:25:17 +00:00
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
static void
|
|
|
|
chacha_reinit(isc_rng_t *rng, isc_uint8_t *buffer, size_t n) {
|
|
|
|
REQUIRE(rng != NULL);
|
|
|
|
|
|
|
|
if (n < CHACHA_KEYSIZE + CHACHA_IVSIZE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
chacha_keysetup(&rng->cpctx, buffer, CHACHA_KEYSIZE * 8, 0);
|
|
|
|
chacha_ivsetup(&rng->cpctx, buffer + CHACHA_KEYSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_rng_create(isc_mem_t *mctx, isc_entropy_t *entropy, isc_rng_t **rngp) {
|
|
|
|
union {
|
|
|
|
unsigned char rnd[128];
|
|
|
|
isc_uint32_t rnd32[32];
|
|
|
|
} rnd;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_rng_t *rng;
|
|
|
|
|
|
|
|
REQUIRE(mctx != NULL);
|
|
|
|
REQUIRE(rngp != NULL && *rngp == NULL);
|
|
|
|
|
|
|
|
if (entropy != NULL) {
|
|
|
|
/*
|
|
|
|
* We accept any quality of random data to avoid blocking.
|
|
|
|
*/
|
|
|
|
result = isc_entropy_getdata(entropy, rnd.rnd,
|
|
|
|
sizeof(rnd), NULL, 0);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 32; i++)
|
|
|
|
isc_random_get(&rnd.rnd32[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
rng = isc_mem_get(mctx, sizeof(*rng));
|
|
|
|
if (rng == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
chacha_reinit(rng, rnd.rnd, sizeof(rnd.rnd));
|
|
|
|
|
|
|
|
rng->have = 0;
|
|
|
|
memset(rng->buffer, 0, CHACHA_BUFFERSIZE);
|
|
|
|
|
|
|
|
/* Create lock */
|
|
|
|
result = isc_mutex_init(&rng->lock);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
isc_mem_put(mctx, rng, sizeof(*rng));
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach to memory context */
|
|
|
|
rng->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &rng->mctx);
|
|
|
|
|
|
|
|
/* Local non-algorithm initializations. */
|
|
|
|
rng->count = 0;
|
|
|
|
rng->entropy = entropy; /* don't have to attach */
|
|
|
|
rng->references = 1;
|
|
|
|
rng->magic = RNG_MAGIC;
|
|
|
|
|
|
|
|
*rngp = rng;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_rng_attach(isc_rng_t *source, isc_rng_t **targetp) {
|
2014-06-11 21:42:36 +10:00
|
|
|
|
2014-06-04 13:38:59 +05:30
|
|
|
REQUIRE(VALID_RNG(source));
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
|
|
|
|
|
|
|
LOCK(&source->lock);
|
|
|
|
source->references++;
|
|
|
|
UNLOCK(&source->lock);
|
|
|
|
|
|
|
|
*targetp = (isc_rng_t *)source;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-06-11 22:57:46 +10:00
|
|
|
destroy(isc_rng_t *rng) {
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
REQUIRE(VALID_RNG(rng));
|
|
|
|
|
|
|
|
rng->magic = 0;
|
2014-06-11 22:57:46 +10:00
|
|
|
isc_mutex_destroy(&rng->lock);
|
2014-06-04 13:38:59 +05:30
|
|
|
isc_mem_putanddetach(&rng->mctx, rng, sizeof(isc_rng_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_rng_detach(isc_rng_t **rngp) {
|
2014-06-11 21:42:36 +10:00
|
|
|
isc_rng_t *rng;
|
2014-06-04 13:38:59 +05:30
|
|
|
isc_boolean_t dest = ISC_FALSE;
|
|
|
|
|
2014-06-11 21:42:36 +10:00
|
|
|
REQUIRE(rngp != NULL && VALID_RNG(*rngp));
|
|
|
|
|
|
|
|
rng = *rngp;
|
|
|
|
*rngp = NULL;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
LOCK(&rng->lock);
|
|
|
|
|
|
|
|
INSIST(rng->references > 0);
|
|
|
|
rng->references--;
|
|
|
|
if (rng->references == 0)
|
|
|
|
dest = ISC_TRUE;
|
2014-06-11 21:42:36 +10:00
|
|
|
UNLOCK(&rng->lock);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
if (dest)
|
2014-06-11 22:57:46 +10:00
|
|
|
destroy(rng);
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chacha_rekey(isc_rng_t *rng, u_char *dat, size_t datlen) {
|
|
|
|
REQUIRE(VALID_RNG(rng));
|
|
|
|
|
|
|
|
#ifndef KEYSTREAM_ONLY
|
|
|
|
memset(rng->buffer, 0, CHACHA_BUFFERSIZE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Fill buffer with the keystream. */
|
|
|
|
chacha_encrypt_bytes(&rng->cpctx, rng->buffer, rng->buffer,
|
|
|
|
CHACHA_BUFFERSIZE);
|
|
|
|
|
|
|
|
/* Mix in optional user provided data. */
|
|
|
|
if (dat != NULL) {
|
|
|
|
size_t i, m;
|
|
|
|
|
|
|
|
m = ISC_MIN(datlen, CHACHA_KEYSIZE + CHACHA_IVSIZE);
|
|
|
|
for (i = 0; i < m; i++)
|
|
|
|
rng->buffer[i] ^= dat[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Immediately reinit for backtracking resistance. */
|
|
|
|
chacha_reinit(rng, rng->buffer,
|
|
|
|
CHACHA_KEYSIZE + CHACHA_IVSIZE);
|
|
|
|
memset(rng->buffer, 0, CHACHA_KEYSIZE + CHACHA_IVSIZE);
|
2017-11-06 10:44:37 -08:00
|
|
|
rng->have = CHACHA_MAXHAVE;
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
2017-11-06 10:44:37 -08:00
|
|
|
static void
|
|
|
|
chacha_getbytes(isc_rng_t *rng, isc_uint8_t *output, size_t length) {
|
2014-06-04 13:38:59 +05:30
|
|
|
REQUIRE(VALID_RNG(rng));
|
|
|
|
|
2017-11-06 10:44:37 -08:00
|
|
|
while (ISC_UNLIKELY(length > CHACHA_MAXHAVE)) {
|
2014-06-04 13:38:59 +05:30
|
|
|
chacha_rekey(rng, NULL, 0);
|
2017-11-06 10:44:37 -08:00
|
|
|
memmove(output, rng->buffer + CHACHA_BUFFERSIZE - rng->have,
|
|
|
|
CHACHA_MAXHAVE);
|
|
|
|
output += CHACHA_MAXHAVE;
|
|
|
|
length -= CHACHA_MAXHAVE;
|
|
|
|
rng->have = 0;
|
|
|
|
}
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2017-11-06 10:44:37 -08:00
|
|
|
if (rng->have < length)
|
|
|
|
chacha_rekey(rng, NULL, 0);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
2017-11-06 10:44:37 -08:00
|
|
|
memmove(output, rng->buffer + CHACHA_BUFFERSIZE - rng->have, length);
|
|
|
|
/* Clear the copied region. */
|
|
|
|
memset(rng->buffer + CHACHA_BUFFERSIZE - rng->have, 0, length);
|
|
|
|
rng->have -= length;
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
chacha_stir(isc_rng_t *rng) {
|
|
|
|
union {
|
|
|
|
unsigned char rnd[128];
|
|
|
|
isc_uint32_t rnd32[32];
|
|
|
|
} rnd;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(VALID_RNG(rng));
|
|
|
|
|
|
|
|
if (rng->entropy != NULL) {
|
|
|
|
/*
|
|
|
|
* We accept any quality of random data to avoid blocking.
|
|
|
|
*/
|
|
|
|
result = isc_entropy_getdata(rng->entropy, rnd.rnd,
|
|
|
|
sizeof(rnd), NULL, 0);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 32; i++)
|
|
|
|
isc_random_get(&rnd.rnd32[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
chacha_rekey(rng, rnd.rnd, sizeof(rnd.rnd));
|
|
|
|
|
2017-09-19 16:16:45 +05:30
|
|
|
isc_safe_memwipe(rnd.rnd, sizeof(rnd.rnd));
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
/* Invalidate the buffer too. */
|
|
|
|
rng->have = 0;
|
|
|
|
memset(rng->buffer, 0, CHACHA_BUFFERSIZE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Derived from OpenBSD's implementation. The rationale is not clear,
|
|
|
|
* but should be conservative enough in safety, and reasonably large
|
|
|
|
* for efficiency.
|
|
|
|
*/
|
2017-11-06 10:44:37 -08:00
|
|
|
rng->count = CHACHA_MAXLENGTH;
|
2014-06-04 13:38:59 +05:30
|
|
|
}
|
|
|
|
|
2017-11-06 10:44:37 -08:00
|
|
|
void
|
|
|
|
isc_rng_randombytes(isc_rng_t *rng, void *output, size_t length) {
|
|
|
|
isc_uint8_t *ptr = output;
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
REQUIRE(VALID_RNG(rng));
|
2017-11-06 10:44:37 -08:00
|
|
|
REQUIRE(output != NULL && length > 0);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
LOCK(&rng->lock);
|
|
|
|
|
2017-11-06 10:44:37 -08:00
|
|
|
while (ISC_UNLIKELY(length > CHACHA_MAXLENGTH)) {
|
|
|
|
chacha_stir(rng);
|
|
|
|
chacha_getbytes(rng, ptr, CHACHA_MAXLENGTH);
|
|
|
|
ptr += CHACHA_MAXLENGTH;
|
|
|
|
length -= CHACHA_MAXLENGTH;
|
|
|
|
rng->count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rng->count -= length;
|
2014-06-04 13:38:59 +05:30
|
|
|
if (rng->count <= 0)
|
|
|
|
chacha_stir(rng);
|
2017-11-06 10:44:37 -08:00
|
|
|
|
|
|
|
chacha_getbytes(rng, ptr, length);
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
UNLOCK(&rng->lock);
|
2017-11-06 10:44:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_uint16_t
|
|
|
|
isc_rng_random(isc_rng_t *rng) {
|
|
|
|
isc_uint16_t result;
|
|
|
|
|
|
|
|
isc_rng_randombytes(rng, &result, sizeof(result));
|
2014-06-04 13:38:59 +05:30
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_uint16_t
|
|
|
|
isc_rng_uniformrandom(isc_rng_t *rng, isc_uint16_t upper_bound) {
|
|
|
|
isc_uint16_t min, r;
|
|
|
|
|
|
|
|
REQUIRE(VALID_RNG(rng));
|
|
|
|
|
|
|
|
if (upper_bound < 2)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure the range of random numbers [min, 0xffff] be a multiple of
|
|
|
|
* upper_bound and contain at least a half of the 16 bit range.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (upper_bound > 0x8000)
|
|
|
|
min = 1 + ~upper_bound; /* 0x8000 - upper_bound */
|
|
|
|
else
|
|
|
|
min = (isc_uint16_t)(0x10000 % (isc_uint32_t)upper_bound);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 (;;) {
|
2017-11-06 10:44:37 -08:00
|
|
|
isc_rng_randombytes(rng, &r, sizeof(r));
|
2014-06-04 13:38:59 +05:30
|
|
|
if (r >= min)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (r % upper_bound);
|
|
|
|
}
|