summaryrefslogtreecommitdiffstats
path: root/sys/libkern
diff options
context:
space:
mode:
Diffstat (limited to 'sys/libkern')
-rw-r--r--sys/libkern/arc4random.c43
1 files changed, 26 insertions, 17 deletions
diff --git a/sys/libkern/arc4random.c b/sys/libkern/arc4random.c
index 909d750..3a3227c 100644
--- a/sys/libkern/arc4random.c
+++ b/sys/libkern/arc4random.c
@@ -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
OpenPOWER on IntegriCloud