1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

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.
This commit is contained in:
Simon Tatham 2019-01-22 18:25:54 +00:00
parent 628e794832
commit 5087792440
17 changed files with 66 additions and 47 deletions

View File

@ -29,6 +29,6 @@ void random_destroy_seed(void)
{
}
void noise_ultralight(unsigned long data)
void noise_ultralight(NoiseSourceId id, unsigned long data)
{
}

View File

@ -28,6 +28,6 @@ void random_destroy_seed(void)
{
}
void noise_ultralight(unsigned long data)
void noise_ultralight(NoiseSourceId id, unsigned long data)
{
}

24
putty.h
View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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));
}

View File

@ -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; }

View File

@ -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);

View File

@ -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)
{

View File

@ -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

View File

@ -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

View File

@ -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);
}

View File

@ -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);

View File

@ -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(&times, sizeof(times));
random_add_noise(NOISE_SOURCE_THREADTIME, &times, sizeof(times));
GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
times + 3);
random_add_noise(&times, sizeof(times));
random_add_noise(NOISE_SOURCE_PROCTIME, &times, 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));
}

View File

@ -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) {

View File

@ -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) {