From 5087792440a04c87767c9bf9a966c1832ac58643 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 22 Jan 2019 18:25:54 +0000 Subject: [PATCH] Label random-noise sources with an enum of ids. The upcoming PRNG revamp will want to tell noise sources apart, so that it can treat them all fairly. So I've added an extra parameter to noise_ultralight and random_add_noise, which takes values in an enumeration covering all the vague classes of entropy source I'm collecting. In this commit, though, it's simply ignored. --- be_nos_s.c | 2 +- be_nossh.c | 2 +- putty.h | 24 ++++++++++++++++++++++-- unix/gtkwin.c | 7 ++++--- unix/uxfdsock.c | 2 +- unix/uxnet.c | 8 ++++---- unix/uxnoise.c | 12 ++++++------ unix/uxpgnt.c | 2 +- unix/uxplink.c | 2 +- unix/uxpterm.c | 2 +- unix/uxsel.c | 2 +- windows/window.c | 6 +++--- windows/winhandl.c | 2 +- windows/winnet.c | 8 ++++---- windows/winnoise.c | 24 ++++++++++++------------ windows/winplink.c | 5 ++--- windows/winsftp.c | 3 +-- 17 files changed, 66 insertions(+), 47 deletions(-) diff --git a/be_nos_s.c b/be_nos_s.c index b3c61e7c..15906cfa 100644 --- a/be_nos_s.c +++ b/be_nos_s.c @@ -29,6 +29,6 @@ void random_destroy_seed(void) { } -void noise_ultralight(unsigned long data) +void noise_ultralight(NoiseSourceId id, unsigned long data) { } diff --git a/be_nossh.c b/be_nossh.c index daf15998..184f28dc 100644 --- a/be_nossh.c +++ b/be_nossh.c @@ -28,6 +28,6 @@ void random_destroy_seed(void) { } -void noise_ultralight(unsigned long data) +void noise_ultralight(NoiseSourceId id, unsigned long data) { } diff --git a/putty.h b/putty.h index 4d6a3c5b..3127c5de 100644 --- a/putty.h +++ b/putty.h @@ -1456,10 +1456,30 @@ FontSpec *fontspec_deserialise(BinarySource *src); /* * Exports from noise.c. */ +typedef enum NoiseSourceId { + NOISE_SOURCE_TIME, + NOISE_SOURCE_IOID, + NOISE_SOURCE_IOLEN, + NOISE_SOURCE_KEY, + NOISE_SOURCE_MOUSEBUTTON, + NOISE_SOURCE_MOUSEPOS, + NOISE_SOURCE_MEMINFO, + NOISE_SOURCE_STAT, + NOISE_SOURCE_RUSAGE, + NOISE_SOURCE_FGWINDOW, + NOISE_SOURCE_CAPTURE, + NOISE_SOURCE_CLIPBOARD, + NOISE_SOURCE_QUEUE, + NOISE_SOURCE_CURSORPOS, + NOISE_SOURCE_THREADTIME, + NOISE_SOURCE_PROCTIME, + NOISE_SOURCE_PERFCOUNT, + NOISE_MAX_SOURCES +} NoiseSourceId; void noise_get_heavy(void (*func) (void *, int)); void noise_get_light(void (*func) (void *, int)); void noise_regular(void); -void noise_ultralight(unsigned long data); +void noise_ultralight(NoiseSourceId id, unsigned long data); void random_save_seed(void); void random_destroy_seed(void); @@ -1673,7 +1693,7 @@ void luni_send(Ldisc *, const wchar_t * widebuf, int len, bool interactive); * Exports from sshrand.c. */ -void random_add_noise(void *noise, int length); +void random_add_noise(NoiseSourceId source, const void *noise, int length); void random_read(void *buf, size_t size); void random_get_savedata(void **data, int *len); extern int random_active; diff --git a/unix/gtkwin.c b/unix/gtkwin.c index 7822a922..1f7f2dcc 100644 --- a/unix/gtkwin.c +++ b/unix/gtkwin.c @@ -987,7 +987,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data) bool generated_something = false; char num_keypad_key = '\0'; - noise_ultralight(event->keyval); + noise_ultralight(NOISE_SOURCE_KEY, event->keyval); #ifdef OSX_META_KEY_CONFIG if (event->state & inst->system_mod_mask) @@ -2059,7 +2059,7 @@ static gboolean button_internal(GtkFrontend *inst, GdkEventButton *event) /* Remember the timestamp. */ inst->input_event_time = event->time; - noise_ultralight(event->button); + noise_ultralight(NOISE_SOURCE_MOUSEBUTTON, event->button); show_mouseptr(inst, true); @@ -2187,7 +2187,8 @@ gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data) /* Remember the timestamp. */ inst->input_event_time = event->time; - noise_ultralight(((uint32_t)event->x << 16) | (uint32_t)event->y); + noise_ultralight(NOISE_SOURCE_MOUSEPOS, + ((uint32_t)event->x << 16) | (uint32_t)event->y); show_mouseptr(inst, true); diff --git a/unix/uxfdsock.c b/unix/uxfdsock.c index 6b09e6db..06a5060f 100644 --- a/unix/uxfdsock.c +++ b/unix/uxfdsock.c @@ -167,7 +167,7 @@ static int fdsocket_try_send(FdSocket *fds) bufchain_prefix(&fds->pending_output_data, &data, &len); ret = write(fds->outfd, data, len); - noise_ultralight(ret); + noise_ultralight(NOISE_SOURCE_IOID, ret); if (ret < 0 && errno != EWOULDBLOCK) { if (!fds->pending_error) { fds->pending_error = errno; diff --git a/unix/uxnet.c b/unix/uxnet.c index 910d290e..231686be 100644 --- a/unix/uxnet.c +++ b/unix/uxnet.c @@ -1121,7 +1121,7 @@ void try_send(NetSocket *s) bufchain_prefix(&s->output_data, &data, &len); } nsent = send(s->s, data, len, urgentflag); - noise_ultralight(nsent); + noise_ultralight(NOISE_SOURCE_IOLEN, nsent); if (nsent <= 0) { err = (nsent < 0 ? errno : 0); if (err == EWOULDBLOCK) { @@ -1275,7 +1275,7 @@ static void net_select_result(int fd, int event) if (!s) return; /* boggle */ - noise_ultralight(event); + noise_ultralight(NOISE_SOURCE_IOID, fd); switch (event) { case 4: /* exceptional */ @@ -1287,7 +1287,7 @@ static void net_select_result(int fd, int event) * type==2 (urgent data). */ ret = recv(s->s, buf, sizeof(buf), MSG_OOB); - noise_ultralight(ret); + noise_ultralight(NOISE_SOURCE_IOLEN, ret); if (ret <= 0) { plug_closing(s->plug, ret == 0 ? "Internal networking trouble" : @@ -1370,7 +1370,7 @@ static void net_select_result(int fd, int event) atmark = true; ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0); - noise_ultralight(ret); + noise_ultralight(NOISE_SOURCE_IOLEN, ret); if (ret < 0) { if (errno == EWOULDBLOCK) { break; diff --git a/unix/uxnoise.c b/unix/uxnoise.c index bcc78273..9b239f77 100644 --- a/unix/uxnoise.c +++ b/unix/uxnoise.c @@ -121,16 +121,16 @@ void noise_regular(void) if ((fd = open("/proc/meminfo", O_RDONLY)) >= 0) { while ( (ret = read(fd, buf, sizeof(buf))) > 0) - random_add_noise(buf, ret); + random_add_noise(NOISE_SOURCE_MEMINFO, buf, ret); close(fd); } if ((fd = open("/proc/stat", O_RDONLY)) >= 0) { while ( (ret = read(fd, buf, sizeof(buf))) > 0) - random_add_noise(buf, ret); + random_add_noise(NOISE_SOURCE_STAT, buf, ret); close(fd); } getrusage(RUSAGE_SELF, &rusage); - random_add_noise(&rusage, sizeof(rusage)); + random_add_noise(NOISE_SOURCE_RUSAGE, &rusage, sizeof(rusage)); } /* @@ -138,10 +138,10 @@ void noise_regular(void) * will add the current time to the noise pool. It gets the scan * code or mouse position passed in, and adds that too. */ -void noise_ultralight(unsigned long data) +void noise_ultralight(NoiseSourceId id, unsigned long data) { struct timeval tv; gettimeofday(&tv, NULL); - random_add_noise(&tv, sizeof(tv)); - random_add_noise(&data, sizeof(data)); + random_add_noise(NOISE_SOURCE_TIME, &tv, sizeof(tv)); + random_add_noise(id, &data, sizeof(data)); } diff --git a/unix/uxpgnt.c b/unix/uxpgnt.c index 964542a2..14e4b51b 100644 --- a/unix/uxpgnt.c +++ b/unix/uxpgnt.c @@ -52,7 +52,7 @@ void uxsel_input_remove(uxsel_id *id) { } */ void random_save_seed(void) {} void random_destroy_seed(void) {} -void noise_ultralight(unsigned long data) {} +void noise_ultralight(NoiseSourceId id, unsigned long data) {} char *platform_default_s(const char *name) { return NULL; } bool platform_default_b(const char *name, bool def) { return def; } int platform_default_i(const char *name, int def) { return def; } diff --git a/unix/uxplink.c b/unix/uxplink.c index 385da47b..186f3a6a 100644 --- a/unix/uxplink.c +++ b/unix/uxplink.c @@ -958,7 +958,7 @@ int main(int argc, char **argv) if (backend_connected(backend)) { ret = read(STDIN_FILENO, buf, sizeof(buf)); - noise_ultralight(ret); + noise_ultralight(NOISE_SOURCE_IOLEN, ret); if (ret < 0) { perror("stdin: read"); exit(1); diff --git a/unix/uxpterm.c b/unix/uxpterm.c index 2352b971..054d56e5 100644 --- a/unix/uxpterm.c +++ b/unix/uxpterm.c @@ -15,7 +15,7 @@ const bool dup_check_launchable = false; /* no need to check host name const bool use_pty_argv = true; /* gtkwin.c will call this, and in pterm it's not needed */ -void noise_ultralight(unsigned long data) { } +void noise_ultralight(NoiseSourceId id, unsigned long data) { } const struct BackendVtable *select_backend(Conf *conf) { diff --git a/unix/uxsel.c b/unix/uxsel.c index cca4017c..d32eac63 100644 --- a/unix/uxsel.c +++ b/unix/uxsel.c @@ -115,7 +115,7 @@ void select_result(int fd, int event) { struct fd *fdstruct = find234(fds, &fd, uxsel_fd_findcmp); - noise_ultralight(fd); + noise_ultralight(NOISE_SOURCE_IOID, fd); /* * Apparently this can sometimes be NULL. Can't see how, but I diff --git a/windows/window.c b/windows/window.c index 0b432e88..d43afb8a 100644 --- a/windows/window.c +++ b/windows/window.c @@ -2696,7 +2696,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, * Add the mouse position and message time to the random * number noise. */ - noise_ultralight(lParam); + noise_ultralight(NOISE_SOURCE_MOUSEPOS, lParam); if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON) && GetCapture() == hwnd) { @@ -2724,7 +2724,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, last_mousemove = WM_NCMOUSEMOVE; } } - noise_ultralight(lParam); + noise_ultralight(NOISE_SOURCE_MOUSEPOS, lParam); break; case WM_IGNORE_CLIP: ignore_clip = wParam; /* don't panic on DESTROYCLIPBOARD */ @@ -3179,7 +3179,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, * Add the scan code and keypress timing to the random * number noise. */ - noise_ultralight(lParam); + noise_ultralight(NOISE_SOURCE_KEY, lParam); /* * We don't do TranslateMessage since it disassociates the diff --git a/windows/winhandl.c b/windows/winhandl.c index f106da7f..c2cc6a82 100644 --- a/windows/winhandl.c +++ b/windows/winhandl.c @@ -689,7 +689,7 @@ void handle_got_event(HANDLE event) h->u.o.sentdata(h, -h->u.o.writeerr); } else { bufchain_consume(&h->u.o.queued_data, h->u.o.lenwritten); - noise_ultralight(h->u.o.lenwritten); + noise_ultralight(NOISE_SOURCE_IOLEN, h->u.o.lenwritten); h->u.o.sentdata(h, bufchain_size(&h->u.o.queued_data)); handle_try_output(&h->u.o); } diff --git a/windows/winnet.c b/windows/winnet.c index 590b98e9..0d42c989 100644 --- a/windows/winnet.c +++ b/windows/winnet.c @@ -1387,7 +1387,7 @@ void try_send(NetSocket *s) bufchain_prefix(&s->output_data, &data, &len); } nsent = p_send(s->s, data, len, urgentflag); - noise_ultralight(nsent); + noise_ultralight(NOISE_SOURCE_IOLEN, nsent); if (nsent <= 0) { err = (nsent < 0 ? p_WSAGetLastError() : 0); if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) { @@ -1538,7 +1538,7 @@ void select_result(WPARAM wParam, LPARAM lParam) return; } - noise_ultralight(lParam); + noise_ultralight(NOISE_SOURCE_IOID, wParam); switch (WSAGETSELECTEVENT(lParam)) { case FD_CONNECT: @@ -1582,7 +1582,7 @@ void select_result(WPARAM wParam, LPARAM lParam) atmark = true; ret = p_recv(s->s, buf, sizeof(buf), 0); - noise_ultralight(ret); + noise_ultralight(NOISE_SOURCE_IOLEN, ret); if (ret < 0) { err = p_WSAGetLastError(); if (err == WSAEWOULDBLOCK) { @@ -1605,7 +1605,7 @@ void select_result(WPARAM wParam, LPARAM lParam) * end with type==2 (urgent data). */ ret = p_recv(s->s, buf, sizeof(buf), MSG_OOB); - noise_ultralight(ret); + noise_ultralight(NOISE_SOURCE_IOLEN, ret); if (ret <= 0) { int err = p_WSAGetLastError(); plug_closing(s->plug, winsock_error_string(err), err, 0); diff --git a/windows/winnoise.c b/windows/winnoise.c index 39c2049a..0c73321e 100644 --- a/windows/winnoise.c +++ b/windows/winnoise.c @@ -122,26 +122,26 @@ void noise_regular(void) FILETIME times[4]; w = GetForegroundWindow(); - random_add_noise(&w, sizeof(w)); + random_add_noise(NOISE_SOURCE_FGWINDOW, &w, sizeof(w)); w = GetCapture(); - random_add_noise(&w, sizeof(w)); + random_add_noise(NOISE_SOURCE_CAPTURE, &w, sizeof(w)); w = GetClipboardOwner(); - random_add_noise(&w, sizeof(w)); + random_add_noise(NOISE_SOURCE_CLIPBOARD, &w, sizeof(w)); z = GetQueueStatus(QS_ALLEVENTS); - random_add_noise(&z, sizeof(z)); + random_add_noise(NOISE_SOURCE_QUEUE, &z, sizeof(z)); GetCursorPos(&pt); - random_add_noise(&pt, sizeof(pt)); + random_add_noise(NOISE_SOURCE_CURSORPOS, &pt, sizeof(pt)); GlobalMemoryStatus(&memstat); - random_add_noise(&memstat, sizeof(memstat)); + random_add_noise(NOISE_SOURCE_MEMINFO, &memstat, sizeof(memstat)); GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2, times + 3); - random_add_noise(×, sizeof(times)); + random_add_noise(NOISE_SOURCE_THREADTIME, ×, sizeof(times)); GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2, times + 3); - random_add_noise(×, sizeof(times)); + random_add_noise(NOISE_SOURCE_PROCTIME, ×, sizeof(times)); } /* @@ -150,16 +150,16 @@ void noise_regular(void) * counter to the noise pool. It gets the scan code or mouse * position passed in. */ -void noise_ultralight(unsigned long data) +void noise_ultralight(NoiseSourceId id, unsigned long data) { DWORD wintime; LARGE_INTEGER perftime; - random_add_noise(&data, sizeof(DWORD)); + random_add_noise(id, &data, sizeof(DWORD)); wintime = GetTickCount(); - random_add_noise(&wintime, sizeof(DWORD)); + random_add_noise(NOISE_SOURCE_TIME, &wintime, sizeof(DWORD)); if (QueryPerformanceCounter(&perftime)) - random_add_noise(&perftime, sizeof(perftime)); + random_add_noise(NOISE_SOURCE_PERFCOUNT, &perftime, sizeof(perftime)); } diff --git a/windows/winplink.c b/windows/winplink.c index fa6f4c98..cbfdeaff 100644 --- a/windows/winplink.c +++ b/windows/winplink.c @@ -220,7 +220,7 @@ int stdin_gotdata(struct handle *h, void *data, int len) fprintf(stderr, "Unable to read from standard input: %s\n", buf); cleanup_exit(0); } - noise_ultralight(len); + noise_ultralight(NOISE_SOURCE_IOLEN, len); if (backend_connected(backend)) { if (len > 0) { return backend_send(backend, data, len); @@ -574,8 +574,7 @@ int main(int argc, char **argv) }; int e; - noise_ultralight(socket); - noise_ultralight(things.lNetworkEvents); + noise_ultralight(NOISE_SOURCE_IOID, socket); for (e = 0; e < lenof(eventtypes); e++) if (things.lNetworkEvents & eventtypes[e].mask) { diff --git a/windows/winsftp.c b/windows/winsftp.c index 27c03616..31683e5a 100644 --- a/windows/winsftp.c +++ b/windows/winsftp.c @@ -578,8 +578,7 @@ int do_eventsel_loop(HANDLE other_event) }; int e; - noise_ultralight(socket); - noise_ultralight(things.lNetworkEvents); + noise_ultralight(NOISE_SOURCE_IOID, socket); for (e = 0; e < lenof(eventtypes); e++) if (things.lNetworkEvents & eventtypes[e].mask) {