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