Skip to content

Commit

Permalink
Loose end from timing shakeup: sshrand.c is now a client of
Browse files Browse the repository at this point in the history
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!

[originally from svn r4913]
  • Loading branch information
sgtatham committed Nov 27, 2004
1 parent 9fc6731 commit 8c69ba0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
5 changes: 3 additions & 2 deletions Recipe
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions be_nossh.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,3 @@ void random_destroy_seed(void)
void noise_ultralight(unsigned long data)
{
}

void noise_regular(void)
{
}
2 changes: 1 addition & 1 deletion cmdgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion putty.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

{
Expand Down Expand Up @@ -7145,6 +7142,8 @@ static const char *ssh_init(void *frontend_handle, void **backend_handle,
if (p != NULL)
return p;

random_ref();

return NULL;
}

Expand Down Expand Up @@ -7223,6 +7222,8 @@ static void ssh_free(void *handle)
sfree(ssh);
if (ssh->pinger)
pinger_free(ssh->pinger);

random_unref();
}

/*
Expand Down
27 changes: 24 additions & 3 deletions sshrand.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -41,6 +44,7 @@ struct RandPool {

static struct RandPool pool;
int random_active = 0;
long next_noise_collection;

static void random_stir(void)
{
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion windows/winpgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 8c69ba0

Please sign in to comment.