In SSH, we now send terminal speeds to the server when requesting a pty
[u/mdw/putty] / noise.c
CommitLineData
374330e2 1/*
2 * Noise generation for PuTTY's cryptographic random number
3 * generator.
4 */
5
374330e2 6#include <stdio.h>
7
8#include "putty.h"
9#include "ssh.h"
d5859615 10#include "storage.h"
374330e2 11
12/*
13 * This function is called once, at PuTTY startup, and will do some
14 * seriously silly things like listing directories and getting disk
15 * free space and a process snapshot.
16 */
17
32874aea 18void noise_get_heavy(void (*func) (void *, int))
19{
374330e2 20 HANDLE srch;
374330e2 21 WIN32_FIND_DATA finddata;
32874aea 22 char winpath[MAX_PATH + 3];
374330e2 23
24 GetWindowsDirectory(winpath, sizeof(winpath));
25 strcat(winpath, "\\*");
26 srch = FindFirstFile(winpath, &finddata);
27 if (srch != INVALID_HANDLE_VALUE) {
28 do {
29 func(&finddata, sizeof(finddata));
30 } while (FindNextFile(srch, &finddata));
31 FindClose(srch);
32 }
33
d5859615 34 read_random_seed(func);
e3ac3c05 35 /* Update the seed immediately, in case another instance uses it. */
36 random_save_seed();
374330e2 37}
38
32874aea 39void random_save_seed(void)
40{
d5859615 41 int len;
42 void *data;
374330e2 43
93b581bd 44 if (random_active) {
45 random_get_savedata(&data, &len);
46 write_random_seed(data, len);
e3ac3c05 47 sfree(data);
93b581bd 48 }
de3df031 49}
50
51/*
374330e2 52 * This function is called every time the random pool needs
53 * stirring, and will acquire the system time in all available
54 * forms and the battery status.
55 */
32874aea 56void noise_get_light(void (*func) (void *, int))
57{
374330e2 58 SYSTEMTIME systime;
59 DWORD adjust[2];
60 BOOL rubbish;
61 SYSTEM_POWER_STATUS pwrstat;
62
63 GetSystemTime(&systime);
64 func(&systime, sizeof(systime));
65
66 GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
67 func(&adjust, sizeof(adjust));
374330e2 68}
69
70/*
7d6ee6ff 71 * This function is called on a timer, and it will monitor
72 * frequently changing quantities such as the state of physical and
73 * virtual memory, the state of the process's message queue, which
74 * window is in the foreground, which owns the clipboard, etc.
75 */
32874aea 76void noise_regular(void)
77{
7d6ee6ff 78 HWND w;
79 DWORD z;
80 POINT pt;
81 MEMORYSTATUS memstat;
82 FILETIME times[4];
83
32874aea 84 w = GetForegroundWindow();
85 random_add_noise(&w, sizeof(w));
86 w = GetCapture();
87 random_add_noise(&w, sizeof(w));
88 w = GetClipboardOwner();
89 random_add_noise(&w, sizeof(w));
90 z = GetQueueStatus(QS_ALLEVENTS);
91 random_add_noise(&z, sizeof(z));
7d6ee6ff 92
32874aea 93 GetCursorPos(&pt);
94 random_add_noise(&pt, sizeof(pt));
7d6ee6ff 95
32874aea 96 GlobalMemoryStatus(&memstat);
97 random_add_noise(&memstat, sizeof(memstat));
7d6ee6ff 98
32874aea 99 GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
100 times + 3);
7d6ee6ff 101 random_add_noise(&times, sizeof(times));
32874aea 102 GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
103 times + 3);
7d6ee6ff 104 random_add_noise(&times, sizeof(times));
105}
106
107/*
374330e2 108 * This function is called on every keypress or mouse move, and
109 * will add the current Windows time and performance monitor
110 * counter to the noise pool. It gets the scan code or mouse
111 * position passed in.
112 */
f7f27309 113void noise_ultralight(unsigned long data)
32874aea 114{
374330e2 115 DWORD wintime;
116 LARGE_INTEGER perftime;
117
118 random_add_noise(&data, sizeof(DWORD));
119
120 wintime = GetTickCount();
121 random_add_noise(&wintime, sizeof(DWORD));
122
123 if (QueryPerformanceCounter(&perftime))
124 random_add_noise(&perftime, sizeof(perftime));
125}