From f8c380c195a46b048d03ec5d0192fc5451dcac87 Mon Sep 17 00:00:00 2001 From: Philip Homburg Date: Tue, 19 Jul 2005 12:16:55 +0000 Subject: [PATCH] Small update to random number generator --- drivers/memory/memory.c | 8 ++--- drivers/memory/random.c | 76 +++++++++++++++++++++++------------------ drivers/memory/random.h | 2 +- etc/usr/rc | 20 +++++++++++ 4 files changed, 65 insertions(+), 41 deletions(-) diff --git a/drivers/memory/memory.c b/drivers/memory/memory.c index 90cd512bd..6ffd44baa 100644 --- a/drivers/memory/memory.c +++ b/drivers/memory/memory.c @@ -27,7 +27,7 @@ #include "random.h" #define NR_DEVS 7 /* number of minor devices */ -#define KRANDOM_PERIOD 10 /* ticks between krandom calls */ +#define KRANDOM_PERIOD 1 /* ticks between krandom calls */ PRIVATE struct device m_geom[NR_DEVS]; /* base and size of each device */ PRIVATE int m_seg[NR_DEVS]; /* segment index of each device */ @@ -183,12 +183,8 @@ unsigned nr_req; /* length of request vector */ /* Random number generator. Character instead of block device. */ case RANDOM_DEV: - if (opcode == DEV_GATHER) - { - s= random_reseed(); - if (s < 0) + if (opcode == DEV_GATHER && !random_isseeded()) return(EAGAIN); - } left = count; while (left > 0) { chunk = (left > RANDOM_BUF_SIZE) ? RANDOM_BUF_SIZE : left; diff --git a/drivers/memory/random.c b/drivers/memory/random.c index f58236ea6..f49320480 100644 --- a/drivers/memory/random.c +++ b/drivers/memory/random.c @@ -33,6 +33,7 @@ PRIVATE u32_t reseed_count; FORWARD _PROTOTYPE( void add_sample, (int source, unsigned long sample) ); FORWARD _PROTOTYPE( void data_block, (rd_keyinstance *keyp, void *data) ); +FORWARD _PROTOTYPE( void reseed, (void) ); PUBLIC void random_init() { @@ -54,42 +55,11 @@ PUBLIC void random_init() reseed_count= 0; } -PUBLIC int random_reseed() +PUBLIC int random_isseeded() { - int i; - SHA256_CTX ctx; - u8_t digest[SHA256_DIGEST_LENGTH]; - - if (samples >= MIN_SAMPLES) - { - reseed_count++; - printf("random_reseed: round %d, samples = %d\n", - reseed_count, samples); - SHA256_Init(&ctx); - if (got_seeded) - SHA256_Update(&ctx, random_key, sizeof(random_key)); - SHA256_Final(digest, &pool_ctx[0]); - SHA256_Update(&ctx, digest, sizeof(digest)); - SHA256_Init(&pool_ctx[0]); - for (i= 1; i/dev/random + # overwrite $RANDOM_FILE. We don't want to use this data again + dd if=/dev/random of=$RANDOM_FILE bs=1024 count=1 + fi + # Start servers. ifs="$IFS"; IFS=, for server in `sysenv servers`; do daemonize $server; done @@ -106,4 +116,14 @@ prompt to investigate, otherwise just wait until an address is received..." # Run the daily cleanup on systems that are not on at night. test -f /usr/etc/daily && sh /usr/etc/daily boot & + +;; +stop|down) + # Save random data. + if dd if=/dev/random of=$RANDOM_FILE.new bs=1024 count=1 2>/dev/null + then + mv $RANDOM_FILE.new $RANDOM_FILE + else + echo 'Failed to save random data.' + fi esac -- 2.44.0