#include "putty.h"
#include "ssh.h"
+#include <assert.h>
/* Collect environmental noise every 5 minutes */
#define NOISE_REGULAR_INTERVAL (5*60*TICKSPERSEC)
unsigned char incomingb[HASHINPUT];
int incomingpos;
+
+ int stir_pending;
};
static struct RandPool pool;
word32 digest[HASHSIZE / sizeof(word32)];
int i, j, k;
+ /*
+ * noise_get_light will call random_add_noise, which may call
+ * back to here. Prevent recursive stirs.
+ */
+ if (pool.stir_pending)
+ return;
+ pool.stir_pending = TRUE;
+
noise_get_light(random_add_noise);
SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb);
memcpy(pool.incoming, digest, sizeof(digest));
pool.poolpos = sizeof(pool.incoming);
+
+ pool.stir_pending = FALSE;
}
void random_add_noise(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);
if (!random_active) {
memset(&pool, 0, sizeof(pool)); /* just to start with */
+ random_active++;
+
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)
{
+ 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();