X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=sshrand.c;h=ead39a9bdad917ec144faddd1ee4df1ec2cd22fd;hb=32adc1a79d0de4219c877f6be80b46984319c1c9;hp=26fcfe5245200ac9bd7cd48a04373b855bd5e0f5;hpb=31133eb0775df1a9506e967462c8cc09463bb79a;p=PuTTY.git diff --git a/sshrand.c b/sshrand.c index 26fcfe52..ead39a9b 100644 --- a/sshrand.c +++ b/sshrand.c @@ -4,6 +4,7 @@ #include "putty.h" #include "ssh.h" +#include /* Collect environmental noise every 5 minutes */ #define NOISE_REGULAR_INTERVAL (5*60*TICKSPERSEC) @@ -48,6 +49,10 @@ static struct RandPool pool; int random_active = 0; long next_noise_collection; +#ifdef RANDOM_DIAGNOSTICS +int random_diagnostics = 0; +#endif + static void random_stir(void) { word32 block[HASHINPUT / sizeof(word32)]; @@ -64,6 +69,30 @@ static void random_stir(void) noise_get_light(random_add_noise); +#ifdef RANDOM_DIAGNOSTICS + { + int p, q; + printf("random stir starting\npool:\n"); + for (p = 0; p < POOLSIZE; p += HASHSIZE) { + printf(" "); + for (q = 0; q < HASHSIZE; q += 4) { + printf(" %08x", *(word32 *)(pool.pool + p + q)); + } + printf("\n"); + } + printf("incoming:\n "); + for (q = 0; q < HASHSIZE; q += 4) { + printf(" %08x", *(word32 *)(pool.incoming + q)); + } + printf("\nincomingb:\n "); + for (q = 0; q < HASHINPUT; q += 4) { + printf(" %08x", *(word32 *)(pool.incomingb + q)); + } + printf("\n"); + random_diagnostics++; + } +#endif + SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb); pool.incomingpos = 0; @@ -115,6 +144,29 @@ static void random_stir(void) for (k = 0; k < sizeof(digest) / sizeof(*digest); k++) ((word32 *) (pool.pool + j))[k] = digest[k]; } + +#ifdef RANDOM_DIAGNOSTICS + if (i == 0) { + int p, q; + printf("random stir midpoint\npool:\n"); + for (p = 0; p < POOLSIZE; p += HASHSIZE) { + printf(" "); + for (q = 0; q < HASHSIZE; q += 4) { + printf(" %08x", *(word32 *)(pool.pool + p + q)); + } + printf("\n"); + } + printf("incoming:\n "); + for (q = 0; q < HASHSIZE; q += 4) { + printf(" %08x", *(word32 *)(pool.incoming + q)); + } + printf("\nincomingb:\n "); + for (q = 0; q < HASHINPUT; q += 4) { + printf(" %08x", *(word32 *)(pool.incomingb + q)); + } + printf("\n"); + } +#endif } /* @@ -127,6 +179,30 @@ static void random_stir(void) pool.poolpos = sizeof(pool.incoming); pool.stir_pending = FALSE; + +#ifdef RANDOM_DIAGNOSTICS + { + int p, q; + printf("random stir done\npool:\n"); + for (p = 0; p < POOLSIZE; p += HASHSIZE) { + printf(" "); + for (q = 0; q < HASHSIZE; q += 4) { + printf(" %08x", *(word32 *)(pool.pool + p + q)); + } + printf("\n"); + } + printf("incoming:\n "); + for (q = 0; q < HASHSIZE; q += 4) { + printf(" %08x", *(word32 *)(pool.incoming + q)); + } + printf("\nincomingb:\n "); + for (q = 0; q < HASHINPUT; q += 4) { + printf(" %08x", *(word32 *)(pool.incomingb + q)); + } + printf("\n"); + random_diagnostics--; + } +#endif } void random_add_noise(void *noise, int length) @@ -198,9 +274,9 @@ static void random_add_heavynoise_bitbybit(void *noise, int length) pool.poolpos = i; } -static void random_timer(void *ctx, long now) +static void random_timer(void *ctx, unsigned long now) { - if (random_active > 0 && now - next_noise_collection >= 0) { + if (random_active > 0 && now == next_noise_collection) { noise_regular(); next_noise_collection = schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool); @@ -218,17 +294,23 @@ void random_ref(void) next_noise_collection = schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool); } - random_active++; } void random_unref(void) { + assert(random_active > 0); + if (random_active == 1) { + random_save_seed(); + expire_timer_context(&pool); + } random_active--; } int random_byte(void) { + assert(random_active); + if (pool.poolpos >= POOLSIZE) random_stir();