Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 28 Nov 1999 17:45:16 -0500
From:      Dan Moschuk <dan@FreeBSD.ORG>
To:        Mike Smith <msmith@FreeBSD.ORG>
Cc:        Warner Losh <imp@village.org>, cvs-committers@FreeBSD.ORG, cvs-all@FreeBSD.ORG
Subject:   Re: cvs commit: src/sys/i386/conf files.i386 src/sys/kern kern_fork.c src/sys/libkern arc4random.c src/sys/sys libkern.h
Message-ID:  <19991128174516.B11396@spirit.jaded.net>
In-Reply-To: <199911282015.MAA00314@mass.cdrom.com>; from msmith@FreeBSD.ORG on Sun, Nov 28, 1999 at 12:15:23PM -0800
References:  <199911281929.MAA86006@harmony.village.org> <199911282015.MAA00314@mass.cdrom.com>

next in thread | previous in thread | raw e-mail | index | archive | help

| > Reviewed, but not completely approved by imp....  The main problem
| > with this, like I said in other mail, is it not using the /dev/random
| > entropy pool for this.
| 
| I actually effectively vetoed this commit (on IRC) for it's failure to do 
| just that.  I expect Dan to pay close attention to the feedback and DTRT 
| (which should include using the libc arcfour code as well, if he's not 
| already).

This patch should address the concerns.  Note that I am not using arc4random()
from libc, it contains functions which we don't need inside the kernel.

Index: arc4random.c
===================================================================
RCS file: /home/ncvs/src/sys/libkern/arc4random.c,v
retrieving revision 1.1
diff -u -r1.1 arc4random.c
--- arc4random.c	1999/11/28 17:51:08	1.1
+++ arc4random.c	1999/11/28 22:38:22
@@ -11,12 +11,16 @@
  */
 
 #include <sys/libkern.h>
-#include <sys/time.h>
 
+#define	ARC4_MAXRUNS 64
+
 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];
 
+extern u_int read_random (void *, u_int);
+
 static __inline void
 arc4_swap(u_int8_t *a, u_int8_t *b)
 {
@@ -28,29 +32,38 @@
 }	
 
 /*
- * Initialize our S-box to its beginning defaults.
+ * Stir our S-box.
  */
 static void
-arc4_init(void)
+arc4_randomstir (void)
 {
-	struct timespec ts;
 	u_int8_t key[256];
-	int n;
+	int r, n;
 
-	for (n = 0; n < 256; n++)
-		arc4_sbox[n] = (u_int8_t) n;
+	r = read_random(key, sizeof(key));
+	for (n = r; n < sizeof(key); n++)
+		key[n] = key[n % r];
 
-	nanotime(&ts);
-	srandom(ts.tv_sec ^ ts.tv_nsec);
 	for (n = 0; n < 256; n++)
-		key[n] = random() % 256;
-
-	arc4_i = arc4_j = 0;
-	for (n = 0; n < 256; n++)
 	{
-		arc4_j = arc4_j + arc4_sbox[n] + key[n];
+		arc4_j = (arc4_j + arc4_sbox[n] + key[n]) % 256;
 		arc4_swap(&arc4_sbox[n], &arc4_sbox[arc4_j]);
 	}
+}
+
+/*
+ * Initialize our S-box to its beginning defaults.
+ */
+static void
+arc4_init(void)
+{
+	int n;
+
+	arc4_i = arc4_j = 0;
+	for (n = 0; n < 256; n++)
+		arc4_sbox[n] = (u_int8_t) n;
+
+	arc4_randomstir();
 	arc4_initialized = 1;
 }
 
@@ -79,11 +92,17 @@
 	/* Initialize array if needed. */
 	if (!arc4_initialized)
 		arc4_init();
+	if (arc4_numruns > ARC4_MAXRUNS)
+	{
+		arc4_randomstir();
+		arc4_numruns = 0;
+	}
 
 	ret = arc4_randbyte();
 	ret |= arc4_randbyte() << 8;
 	ret |= arc4_randbyte() << 16;
 	ret |= arc4_randbyte() << 24;
 
+	arc4_numruns++;
 	return ret;
 }

-- 
Dan Moschuk (TFreak!dan@freebsd.org)
"Cure for global warming: One giant heatsink and dual fans!"


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe cvs-all" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19991128174516.B11396>