Lock down arc4random so it can be safely called w/o Giant.

Minor code reorganization was required, but the only functional
change was that the first 1024 bytes of output are thrown out
after each reseed, rather than just the initial seed.
This commit is contained in:
Mike Silbersack 2003-08-15 06:34:47 +00:00
parent 92aa3b6885
commit 2f823fa326
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=118938

View File

@ -12,8 +12,12 @@
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/random.h>
#include <sys/libkern.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/time.h>
#define ARC4_RESEED_BYTES 65536
@ -21,10 +25,10 @@ __FBSDID("$FreeBSD$");
#define ARC4_KEYBYTES (256 / 8)
static u_int8_t arc4_i, arc4_j;
static int arc4_initialized = 0;
static int arc4_numruns = 0;
static u_int8_t arc4_sbox[256];
static time_t arc4_t_reseed;
static struct mtx arc4_mtx;
static u_int8_t arc4_randbyte(void);
@ -53,6 +57,8 @@ arc4_randomstir (void)
* device is not loaded -- MarkM.
*/
r = read_random(key, ARC4_KEYBYTES);
getmicrouptime(&tv_now);
mtx_lock(&arc4_mtx);
/* If r == 0 || -1, just use what was on the stack. */
if (r > 0) {
for (n = r; n < sizeof(key); n++)
@ -65,9 +71,17 @@ arc4_randomstir (void)
}
/* Reset for next reseed cycle. */
getmicrouptime(&tv_now);
arc4_t_reseed = tv_now.tv_sec + ARC4_RESEED_SECONDS;
arc4_numruns = 0;
/*
* Throw away the first N words of output, as suggested in the
* paper "Weaknesses in the Key Scheduling Algorithm of RC4"
* by Fluher, Mantin, and Shamir. (N = 256 in our case.)
*/
for (n = 0; n < 256*4; n++)
arc4_randbyte();
mtx_unlock(&arc4_mtx);
}
/*
@ -78,22 +92,16 @@ arc4_init(void)
{
int n;
mtx_init(&arc4_mtx, "arc4_mtx", NULL, MTX_DEF);
arc4_i = arc4_j = 0;
for (n = 0; n < 256; n++)
arc4_sbox[n] = (u_int8_t) n;
arc4_randomstir();
arc4_initialized = 1;
/*
* Throw away the first N words of output, as suggested in the
* paper "Weaknesses in the Key Scheduling Algorithm of RC4"
* by Fluher, Mantin, and Shamir. (N = 256 in our case.)
*/
for (n = 0; n < 256*4; n++)
arc4_randbyte();
arc4_t_reseed = 0;
}
SYSINIT(arc4_init, SI_SUB_LOCK, SI_ORDER_ANY, arc4_init, NULL);
/*
* Generate a random byte.
*/
@ -111,26 +119,27 @@ arc4_randbyte(void)
return arc4_sbox[arc4_t];
}
/*
* MPSAFE
*/
void
arc4rand(void *ptr, u_int len, int reseed)
{
u_char *p;
struct timeval tv;
/* Initialize array if needed. */
if (!arc4_initialized)
arc4_init();
getmicrouptime(&tv);
arc4_numruns += len;
if (reseed ||
(arc4_numruns > ARC4_RESEED_BYTES) ||
(tv.tv_sec > arc4_t_reseed))
arc4_randomstir();
mtx_lock(&arc4_mtx);
arc4_numruns += len;
p = ptr;
while (len--)
*p++ = arc4_randbyte();
mtx_unlock(&arc4_mtx);
}
uint32_t