X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=sshrand.c;h=43b81234240f8d9e7ca37d51c5e89276a1bbf7ca;hb=693bbf0ec6007120b91dafb4eaed1ae833eb59f7;hp=e2386669dac19f4cd2cc1095142f29c3c8512f58;hpb=4296e6b786b062f0754b1d0e1c062f22046c2d74;p=PuTTY.git diff --git a/sshrand.c b/sshrand.c index e2386669..43b81234 100644 --- a/sshrand.c +++ b/sshrand.c @@ -5,6 +5,9 @@ #include "putty.h" #include "ssh.h" +/* Collect environmental noise every 5 minutes */ +#define NOISE_REGULAR_INTERVAL (5*60*TICKSPERSEC) + void noise_get_heavy(void (*func) (void *, int)); void noise_get_light(void (*func) (void *, int)); @@ -41,6 +44,7 @@ struct RandPool { static struct RandPool pool; int random_active = 0; +long next_noise_collection; static void random_stir(void) { @@ -182,14 +186,33 @@ static void random_add_heavynoise_bitbybit(void *noise, int length) pool.poolpos = i; } -void random_init(void) +static void random_timer(void *ctx, long now) { - memset(&pool, 0, sizeof(pool)); /* just to start with */ + if (random_active > 0 && now - next_noise_collection >= 0) { + noise_regular(); + next_noise_collection = + schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool); + } +} - random_active = 1; +void random_ref(void) +{ + if (!random_active) { + memset(&pool, 0, sizeof(pool)); /* just to start with */ - noise_get_heavy(random_add_heavynoise_bitbybit); - random_stir(); + noise_get_heavy(random_add_heavynoise_bitbybit); + random_stir(); + + next_noise_collection = + schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool); + } + + random_active++; +} + +void random_unref(void) +{ + random_active--; } int random_byte(void) @@ -202,7 +225,7 @@ int random_byte(void) void random_get_savedata(void **data, int *len) { - void *buf = smalloc(POOLSIZE / 2); + void *buf = snewn(POOLSIZE / 2, char); random_stir(); memcpy(buf, pool.pool + pool.poolpos, POOLSIZE / 2); *len = POOLSIZE / 2;