Loose end from timing shakeup: sshrand.c is now a client of
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Sat, 27 Nov 2004 19:56:38 +0000 (19:56 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Sat, 27 Nov 2004 19:56:38 +0000 (19:56 +0000)
timing.c, and hence takes its own responsibility for calling
noise_regular() at regular intervals. Again, this means it will be
called consistently in _all_ the SSH-speaking tools, not just those
in which I remembered to call it!

git-svn-id: svn://svn.tartarus.org/sgt/putty@4913 cda61777-01e9-0310-a592-d414129be87e

Recipe
be_nossh.c
cmdgen.c
putty.h
ssh.c
sshrand.c
windows/winpgen.c

diff --git a/Recipe b/Recipe
index 18f5637..216ef96 100644 (file)
--- a/Recipe
+++ b/Recipe
@@ -239,7 +239,8 @@ pageant  : [G] winpgnt sshrsa sshpubk sshdes sshbn sshmd5 version tree234
 
 puttygen : [G] winpgen sshrsag sshdssg sshprime sshdes sshbn sshmd5 version
          + sshrand winnoise sshsha winstore misc winctrls sshrsa sshdss winmisc
-         + sshpubk sshaes sshsh512 import winutils puttygen.res tree234 LIBS
+         + sshpubk sshaes sshsh512 import winutils puttygen.res tree234
+        + notiming LIBS
 
 pterm    : [X] UXTERM uxmisc misc ldisc settings pty uxsel BE_NONE uxstore
          + signal CHARSET cmdline ptermm version
@@ -252,7 +253,7 @@ plink    : [U] uxplink uxcons NONSSH UXSSH BE_ALL logging UXMISC signal ux_x11
 
 puttygen : [U] cmdgen sshrsag sshdssg sshprime sshdes sshbn sshmd5 version
          + sshrand uxnoise sshsha misc sshrsa sshdss uxcons uxstore uxmisc
-         + sshpubk sshaes sshsh512 import puttygen.res tree234 uxgen
+         + sshpubk sshaes sshsh512 import puttygen.res tree234 uxgen notiming
 
 pscp     : [U] scp uxsftp uxcons UXSSH BE_SSH SFTP wildcard UXMISC
 psftp    : [U] psftp uxsftp uxcons UXSSH BE_SSH SFTP UXMISC
index 90ab998..18ba32a 100644 (file)
@@ -31,7 +31,3 @@ void random_destroy_seed(void)
 void noise_ultralight(unsigned long data)
 {
 }
-
-void noise_regular(void)
-{
-}
index 467bab6..42772aa 100644 (file)
--- a/cmdgen.c
+++ b/cmdgen.c
@@ -616,7 +616,7 @@ int main(int argc, char **argv)
        else
            strftime(default_comment, 30, "rsa-key-%Y%m%d", tm);
 
-       random_init();
+       random_ref();
        entropy = get_random_data(bits / 8);
        random_add_heavynoise(entropy, bits / 8);
        memset(entropy, 0, bits/8);
diff --git a/putty.h b/putty.h
index 4ad3124..d880dbe 100644 (file)
--- a/putty.h
+++ b/putty.h
@@ -742,10 +742,14 @@ void luni_send(void *, wchar_t * widebuf, int len, int interactive);
  */
 
 void random_add_noise(void *noise, int length);
-void random_init(void);
 int random_byte(void);
 void random_get_savedata(void **data, int *len);
 extern int random_active;
+/* The random number subsystem is activated if at least one other entity
+ * within the program expresses an interest in it. So each SSH session
+ * calls random_ref on startup and random_unref on shutdown. */
+void random_ref(void);
+void random_unref(void);
 
 /*
  * Exports from pinger.c.
diff --git a/ssh.c b/ssh.c
index 422897f..07d06f1 100644 (file)
--- a/ssh.c
+++ b/ssh.c
@@ -2576,8 +2576,6 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
 
     crBegin(ssh->do_ssh1_login_crstate);
 
-    random_init();
-
     if (!pktin)
        crWaitUntil(pktin);
 
@@ -4299,7 +4297,6 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
     s->csmac_tobe = s->scmac_tobe = NULL;
     s->cscomp_tobe = s->sccomp_tobe = NULL;
 
-    random_init();
     s->first_kex = 1;
 
     {
@@ -7145,6 +7142,8 @@ static const char *ssh_init(void *frontend_handle, void **backend_handle,
     if (p != NULL)
        return p;
 
+    random_ref();
+
     return NULL;
 }
 
@@ -7223,6 +7222,8 @@ static void ssh_free(void *handle)
     sfree(ssh);
     if (ssh->pinger)
        pinger_free(ssh->pinger);
+
+    random_unref();
 }
 
 /*
index a7b3dc4..43b8123 100644 (file)
--- 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,16 +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)
+{
+    if (random_active > 0 && now - next_noise_collection >= 0) {
+       noise_regular();
+       next_noise_collection =
+           schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool);
+    }
+}
+
+void random_ref(void)
 {
     if (!random_active) {
        memset(&pool, 0, sizeof(pool));    /* just to start with */
 
-       random_active = 1;
-
        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)
index 3f2be92..e80dbfe 100644 (file)
@@ -1445,7 +1445,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
             help_path = NULL;
     }
 
-    random_init();
+    random_ref();
     return DialogBox(hinst, MAKEINTRESOURCE(201), NULL,
                     MainDlgProc) != IDOK;
 }