From owner-cvs-all Sun Nov 28 14:43:35 1999 Delivered-To: cvs-all@freebsd.org Received: from spirit.jaded.net (dialin924.toronto.globalserve.net [209.90.133.161]) by hub.freebsd.org (Postfix) with ESMTP id 06DFC15394; Sun, 28 Nov 1999 14:43:19 -0800 (PST) (envelope-from dan@spirit.jaded.net) Received: (from dan@localhost) by spirit.jaded.net (8.9.3/8.9.3) id RAA58946; Sun, 28 Nov 1999 17:45:16 -0500 (EST) Date: Sun, 28 Nov 1999 17:45:16 -0500 From: Dan Moschuk To: Mike Smith Cc: Warner Losh , 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> References: <199911281929.MAA86006@harmony.village.org> <199911282015.MAA00314@mass.cdrom.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <199911282015.MAA00314@mass.cdrom.com>; from msmith@FreeBSD.ORG on Sun, Nov 28, 1999 at 12:15:23PM -0800 Sender: owner-cvs-all@FreeBSD.ORG Precedence: bulk | > 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 -#include +#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