2000-06-25 08:38:58 +00:00
|
|
|
/*-
|
2000-07-07 09:03:59 +00:00
|
|
|
* Copyright (c) 2000 Mark R V Murray
|
2000-06-25 08:38:58 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-08-24 17:55:58 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2000-09-10 13:52:19 +00:00
|
|
|
#include <sys/kernel.h>
|
2001-03-28 09:17:56 +00:00
|
|
|
#include <sys/lock.h>
|
2000-10-20 07:58:15 +00:00
|
|
|
#include <sys/mutex.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
#include <sys/random.h>
|
2001-03-10 12:51:55 +00:00
|
|
|
#include <sys/sysctl.h>
|
2000-11-25 17:09:01 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
#include <crypto/rijndael/rijndael.h>
|
2002-07-15 13:58:35 +00:00
|
|
|
#include <crypto/sha2/sha2.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-10-14 10:59:56 +00:00
|
|
|
#include <dev/random/hash.h>
|
2001-03-10 12:51:55 +00:00
|
|
|
#include <dev/random/randomdev.h>
|
2000-10-14 10:59:56 +00:00
|
|
|
#include <dev/random/yarrow.h>
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-07-07 09:03:59 +00:00
|
|
|
/* #define DEBUG */
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
RANDOM_CHECK_UINT(gengateinterval, 4, 64);
|
|
|
|
RANDOM_CHECK_UINT(bins, 2, 16);
|
|
|
|
RANDOM_CHECK_UINT(fastthresh, BLOCKSIZE/4, BLOCKSIZE);
|
|
|
|
RANDOM_CHECK_UINT(slowthresh, BLOCKSIZE/4, BLOCKSIZE);
|
|
|
|
RANDOM_CHECK_UINT(slowoverthresh, 1, 5);
|
|
|
|
|
2002-01-10 00:09:21 +00:00
|
|
|
/* Structure holding the entropy state */
|
|
|
|
static struct random_state random_state;
|
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
SYSCTL_NODE(_kern_random, OID_AUTO, yarrow, CTLFLAG_RW, 0, "Yarrow Parameters");
|
|
|
|
SYSCTL_PROC(_kern_random_yarrow, OID_AUTO, gengateinterval,
|
|
|
|
CTLTYPE_INT|CTLFLAG_RW, &random_state.gengateinterval, 10,
|
|
|
|
random_check_uint_gengateinterval, "I", "Generator Gate Interval");
|
|
|
|
SYSCTL_PROC(_kern_random_yarrow, OID_AUTO, bins,
|
|
|
|
CTLTYPE_INT|CTLFLAG_RW, &random_state.bins, 10,
|
|
|
|
random_check_uint_bins, "I", "Execution time tuner");
|
|
|
|
SYSCTL_PROC(_kern_random_yarrow, OID_AUTO, fastthresh,
|
|
|
|
CTLTYPE_INT|CTLFLAG_RW, &random_state.pool[0].thresh, (3*BLOCKSIZE)/4,
|
|
|
|
random_check_uint_fastthresh, "I", "Fast reseed threshold");
|
|
|
|
SYSCTL_PROC(_kern_random_yarrow, OID_AUTO, slowthresh,
|
|
|
|
CTLTYPE_INT|CTLFLAG_RW, &random_state.pool[1].thresh, BLOCKSIZE,
|
|
|
|
random_check_uint_slowthresh, "I", "Slow reseed threshold");
|
|
|
|
SYSCTL_PROC(_kern_random_yarrow, OID_AUTO, slowoverthresh,
|
|
|
|
CTLTYPE_INT|CTLFLAG_RW, &random_state.slowoverthresh, 2,
|
|
|
|
random_check_uint_slowoverthresh, "I", "Slow over-threshold reseed");
|
2000-07-07 09:03:59 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
static void generator_gate(void);
|
|
|
|
static void reseed(u_int);
|
2000-09-10 13:52:19 +00:00
|
|
|
|
|
|
|
/* The reseed thread mutex */
|
2000-09-14 20:15:16 +00:00
|
|
|
static struct mtx random_reseed_mtx;
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
/* Process a single stochastic event off the harvest queue */
|
|
|
|
void
|
|
|
|
random_process_event(struct harvest *event)
|
2000-07-07 09:03:59 +00:00
|
|
|
{
|
2002-03-03 19:44:22 +00:00
|
|
|
u_int pl, overthreshhold[2];
|
2000-09-10 13:52:19 +00:00
|
|
|
struct source *source;
|
2002-03-03 19:44:22 +00:00
|
|
|
enum esource src;
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
/* Unpack the event into the appropriate source accumulator */
|
|
|
|
pl = random_state.which;
|
|
|
|
source = &random_state.pool[pl].source[event->source];
|
|
|
|
yarrow_hash_iterate(&random_state.pool[pl].hash, event->entropy,
|
|
|
|
sizeof(event->entropy));
|
|
|
|
yarrow_hash_iterate(&random_state.pool[pl].hash, &event->somecounter,
|
|
|
|
sizeof(event->somecounter));
|
|
|
|
source->frac += event->frac;
|
|
|
|
source->bits += event->bits + source->frac/1024;
|
|
|
|
source->frac %= 1024;
|
|
|
|
|
|
|
|
/* Count the over-threshold sources in each pool */
|
|
|
|
for (pl = 0; pl < 2; pl++) {
|
|
|
|
overthreshhold[pl] = 0;
|
2002-03-03 19:44:22 +00:00
|
|
|
for (src = RANDOM_START; src < ENTROPYSOURCE; src++) {
|
2001-03-10 12:51:55 +00:00
|
|
|
if (random_state.pool[pl].source[src].bits
|
|
|
|
> random_state.pool[pl].thresh)
|
|
|
|
overthreshhold[pl]++;
|
2000-09-10 13:52:19 +00:00
|
|
|
}
|
2001-03-10 12:51:55 +00:00
|
|
|
}
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
/* if any fast source over threshhold, reseed */
|
|
|
|
if (overthreshhold[FAST])
|
|
|
|
reseed(FAST);
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
/* if enough slow sources are over threshhold, reseed */
|
|
|
|
if (overthreshhold[SLOW] >= random_state.slowoverthresh)
|
|
|
|
reseed(SLOW);
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
/* Invert the fast/slow pool selector bit */
|
|
|
|
random_state.which = !random_state.which;
|
2000-07-07 09:03:59 +00:00
|
|
|
}
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
void
|
2000-07-07 09:03:59 +00:00
|
|
|
random_init(void)
|
2000-06-25 08:38:58 +00:00
|
|
|
{
|
2001-03-10 12:51:55 +00:00
|
|
|
int i;
|
2001-01-14 17:50:15 +00:00
|
|
|
|
2001-02-11 16:21:35 +00:00
|
|
|
/* Yarrow parameters. Do not adjust these unless you have
|
|
|
|
* have a very good clue about what they do!
|
|
|
|
*/
|
2000-07-07 09:03:59 +00:00
|
|
|
random_state.gengateinterval = 10;
|
|
|
|
random_state.bins = 10;
|
2001-03-10 12:51:55 +00:00
|
|
|
random_state.pool[0].thresh = (3*BLOCKSIZE)/4;
|
|
|
|
random_state.pool[1].thresh = BLOCKSIZE;
|
2000-07-07 09:03:59 +00:00
|
|
|
random_state.slowoverthresh = 2;
|
|
|
|
random_state.which = FAST;
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
/* Initialise the fast and slow entropy pools */
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
yarrow_hash_init(&random_state.pool[i].hash);
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
/* Clear the counter */
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
random_state.counter[i] = 0;
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
/* Set up a lock for the reseed process */
|
2002-04-04 21:03:38 +00:00
|
|
|
mtx_init(&random_reseed_mtx, "random reseed", NULL, MTX_DEF);
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-07 09:03:59 +00:00
|
|
|
random_deinit(void)
|
2000-06-25 08:38:58 +00:00
|
|
|
{
|
2000-09-10 13:52:19 +00:00
|
|
|
mtx_destroy(&random_reseed_mtx);
|
2000-07-07 09:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-03-10 12:51:55 +00:00
|
|
|
reseed(u_int fastslow)
|
2000-07-07 09:03:59 +00:00
|
|
|
{
|
2000-09-10 13:52:19 +00:00
|
|
|
/* Interrupt-context stack is a limited resource; make large
|
|
|
|
* structures static.
|
2000-07-23 11:08:16 +00:00
|
|
|
*/
|
2000-09-10 13:52:19 +00:00
|
|
|
static u_char v[TIMEBIN][KEYSIZE]; /* v[i] */
|
|
|
|
static struct yarrowhash context;
|
|
|
|
u_char hash[KEYSIZE]; /* h' */
|
|
|
|
u_char temp[KEYSIZE];
|
2002-03-03 19:44:22 +00:00
|
|
|
u_int i;
|
|
|
|
enum esource j;
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-07-07 09:03:59 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Reseed type %d\n", fastslow);
|
|
|
|
#endif
|
|
|
|
|
2000-09-10 13:52:19 +00:00
|
|
|
/* The reseed task must not be jumped on */
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock(&random_reseed_mtx);
|
2000-09-10 13:52:19 +00:00
|
|
|
|
2000-06-25 08:38:58 +00:00
|
|
|
/* 1. Hash the accumulated entropy into v[0] */
|
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
yarrow_hash_init(&context);
|
2000-09-10 13:52:19 +00:00
|
|
|
/* Feed the slow pool hash in if slow */
|
|
|
|
if (fastslow == SLOW)
|
|
|
|
yarrow_hash_iterate(&context,
|
2001-03-10 12:51:55 +00:00
|
|
|
&random_state.pool[SLOW].hash,
|
|
|
|
sizeof(struct yarrowhash));
|
2000-09-10 13:52:19 +00:00
|
|
|
yarrow_hash_iterate(&context,
|
|
|
|
&random_state.pool[FAST].hash, sizeof(struct yarrowhash));
|
2001-03-10 12:51:55 +00:00
|
|
|
yarrow_hash_finish(&context, v[0]);
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-07-23 11:08:16 +00:00
|
|
|
/* 2. Compute hash values for all v. _Supposed_ to be computationally
|
|
|
|
* intensive.
|
|
|
|
*/
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-07-07 09:03:59 +00:00
|
|
|
if (random_state.bins > TIMEBIN)
|
|
|
|
random_state.bins = TIMEBIN;
|
|
|
|
for (i = 1; i < random_state.bins; i++) {
|
2001-03-10 12:51:55 +00:00
|
|
|
yarrow_hash_init(&context);
|
|
|
|
/* v[i] #= h(v[i - 1]) */
|
2000-09-10 13:52:19 +00:00
|
|
|
yarrow_hash_iterate(&context, v[i - 1], KEYSIZE);
|
2000-07-07 09:03:59 +00:00
|
|
|
/* v[i] #= h(v[0]) */
|
2000-09-10 13:52:19 +00:00
|
|
|
yarrow_hash_iterate(&context, v[0], KEYSIZE);
|
2000-07-07 09:03:59 +00:00
|
|
|
/* v[i] #= h(i) */
|
2001-03-10 12:51:55 +00:00
|
|
|
yarrow_hash_iterate(&context, &i, sizeof(u_int));
|
2000-09-10 13:52:19 +00:00
|
|
|
/* Return the hashval */
|
|
|
|
yarrow_hash_finish(&context, v[i]);
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
|
|
|
|
2000-09-10 13:52:19 +00:00
|
|
|
/* 3. Compute a new key; h' is the identity function here;
|
|
|
|
* it is not being ignored!
|
|
|
|
*/
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
yarrow_hash_init(&context);
|
2000-09-10 13:52:19 +00:00
|
|
|
yarrow_hash_iterate(&context, &random_state.key, KEYSIZE);
|
|
|
|
for (i = 1; i < random_state.bins; i++)
|
|
|
|
yarrow_hash_iterate(&context, &v[i], KEYSIZE);
|
|
|
|
yarrow_hash_finish(&context, temp);
|
2001-03-10 12:51:55 +00:00
|
|
|
yarrow_encrypt_init(&random_state.key, temp);
|
2000-06-25 08:38:58 +00:00
|
|
|
|
|
|
|
/* 4. Recompute the counter */
|
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
random_state.counter[i] = 0;
|
|
|
|
yarrow_encrypt(&random_state.key, random_state.counter, temp);
|
|
|
|
memcpy(random_state.counter, temp, sizeof(random_state.counter));
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-07-07 09:03:59 +00:00
|
|
|
/* 5. Reset entropy estimate accumulators to zero */
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-07-07 09:03:59 +00:00
|
|
|
for (i = 0; i <= fastslow; i++) {
|
2002-03-03 19:44:22 +00:00
|
|
|
for (j = RANDOM_START; j < ENTROPYSOURCE; j++) {
|
2001-03-10 12:51:55 +00:00
|
|
|
random_state.pool[i].source[j].bits = 0;
|
|
|
|
random_state.pool[i].source[j].frac = 0;
|
2000-07-07 09:03:59 +00:00
|
|
|
}
|
|
|
|
}
|
2000-06-25 08:38:58 +00:00
|
|
|
|
|
|
|
/* 6. Wipe memory of intermediate values */
|
|
|
|
|
2000-09-10 13:52:19 +00:00
|
|
|
memset((void *)v, 0, sizeof(v));
|
|
|
|
memset((void *)temp, 0, sizeof(temp));
|
|
|
|
memset((void *)hash, 0, sizeof(hash));
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-09-10 13:52:19 +00:00
|
|
|
/* 7. Dump to seed file */
|
|
|
|
/* XXX Not done here yet */
|
|
|
|
|
|
|
|
/* Release the reseed mutex */
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock(&random_reseed_mtx);
|
2000-09-10 13:52:19 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Reseed finish\n");
|
|
|
|
#endif
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
/* Unblock the device if it was blocked due to being unseeded */
|
|
|
|
random_unblock();
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 13:58:35 +00:00
|
|
|
/* Internal function to return processed entropy from the PRNG */
|
2002-03-03 19:44:22 +00:00
|
|
|
int
|
|
|
|
read_random_real(void *buf, int count)
|
2000-06-25 08:38:58 +00:00
|
|
|
{
|
|
|
|
static int cur = 0;
|
|
|
|
static int gate = 1;
|
2001-03-28 06:27:42 +00:00
|
|
|
static u_char genval[KEYSIZE];
|
2002-12-12 17:38:45 +00:00
|
|
|
size_t tomove;
|
2002-03-03 19:44:22 +00:00
|
|
|
int i;
|
|
|
|
int retval;
|
2000-07-10 06:40:23 +00:00
|
|
|
|
|
|
|
/* The reseed task must not be jumped on */
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock(&random_reseed_mtx);
|
2000-06-25 08:38:58 +00:00
|
|
|
|
|
|
|
if (gate) {
|
|
|
|
generator_gate();
|
2000-07-07 09:03:59 +00:00
|
|
|
random_state.outputblocks = 0;
|
2000-06-25 08:38:58 +00:00
|
|
|
gate = 0;
|
|
|
|
}
|
2002-03-03 19:44:22 +00:00
|
|
|
if (count > 0 && (size_t)count >= sizeof(random_state.counter)) {
|
2000-06-25 08:38:58 +00:00
|
|
|
retval = 0;
|
2002-03-03 19:44:22 +00:00
|
|
|
for (i = 0; i < count; i += (int)sizeof(random_state.counter)) {
|
2001-03-10 12:51:55 +00:00
|
|
|
random_state.counter[0]++;
|
|
|
|
yarrow_encrypt(&random_state.key, random_state.counter,
|
2001-03-28 06:27:42 +00:00
|
|
|
genval);
|
2002-12-12 17:38:45 +00:00
|
|
|
tomove = min(count - i, sizeof(random_state.counter));
|
|
|
|
memcpy((char *)buf + i, genval, tomove);
|
2001-03-10 12:51:55 +00:00
|
|
|
if (++random_state.outputblocks >=
|
|
|
|
random_state.gengateinterval) {
|
2000-06-25 08:38:58 +00:00
|
|
|
generator_gate();
|
2000-07-07 09:03:59 +00:00
|
|
|
random_state.outputblocks = 0;
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
2002-12-12 17:38:45 +00:00
|
|
|
retval += (int)tomove;
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!cur) {
|
2001-03-10 12:51:55 +00:00
|
|
|
random_state.counter[0]++;
|
|
|
|
yarrow_encrypt(&random_state.key, random_state.counter,
|
2001-03-28 06:27:42 +00:00
|
|
|
genval);
|
2002-03-03 19:44:22 +00:00
|
|
|
memcpy(buf, genval, (size_t)count);
|
|
|
|
cur = (int)sizeof(random_state.counter) - count;
|
2001-03-10 12:51:55 +00:00
|
|
|
if (++random_state.outputblocks >=
|
|
|
|
random_state.gengateinterval) {
|
2000-06-25 08:38:58 +00:00
|
|
|
generator_gate();
|
2000-07-07 09:03:59 +00:00
|
|
|
random_state.outputblocks = 0;
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
|
|
|
retval = count;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
retval = cur < count ? cur : count;
|
2002-03-03 19:44:22 +00:00
|
|
|
memcpy(buf,
|
|
|
|
&genval[(int)sizeof(random_state.counter) - cur],
|
|
|
|
(size_t)retval);
|
2000-06-25 08:38:58 +00:00
|
|
|
cur -= retval;
|
|
|
|
}
|
|
|
|
}
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock(&random_reseed_mtx);
|
2000-06-25 08:38:58 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2000-07-07 09:03:59 +00:00
|
|
|
static void
|
2000-06-25 08:38:58 +00:00
|
|
|
generator_gate(void)
|
|
|
|
{
|
2001-03-10 12:51:55 +00:00
|
|
|
u_int i;
|
2000-09-10 13:52:19 +00:00
|
|
|
u_char temp[KEYSIZE];
|
2000-06-25 08:38:58 +00:00
|
|
|
|
2000-07-07 09:03:59 +00:00
|
|
|
#ifdef DEBUG
|
2000-07-10 06:40:23 +00:00
|
|
|
printf("Generator gate\n");
|
2000-07-07 09:03:59 +00:00
|
|
|
#endif
|
2000-07-10 06:40:23 +00:00
|
|
|
|
2000-07-07 09:03:59 +00:00
|
|
|
for (i = 0; i < KEYSIZE; i += sizeof(random_state.counter)) {
|
2001-03-10 12:51:55 +00:00
|
|
|
random_state.counter[0]++;
|
|
|
|
yarrow_encrypt(&random_state.key, random_state.counter,
|
|
|
|
&(temp[i]));
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
|
|
|
|
2001-03-10 12:51:55 +00:00
|
|
|
yarrow_encrypt_init(&random_state.key, temp);
|
2000-09-10 13:52:19 +00:00
|
|
|
memset((void *)temp, 0, KEYSIZE);
|
2000-07-10 06:40:23 +00:00
|
|
|
|
2000-09-10 13:52:19 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Generator gate finish\n");
|
|
|
|
#endif
|
2000-06-25 08:38:58 +00:00
|
|
|
}
|
2000-07-07 09:03:59 +00:00
|
|
|
|
2000-11-25 19:13:29 +00:00
|
|
|
/* Helper routine to perform explicit reseeds */
|
|
|
|
void
|
|
|
|
random_reseed(void)
|
|
|
|
{
|
2002-07-15 13:58:35 +00:00
|
|
|
reseed(SLOW);
|
2000-11-25 19:13:29 +00:00
|
|
|
}
|