c5966a712c885fa9fcb088187323795bc76f935a
[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 HANDLE srch;
27 WIN32_FIND_DATA finddata;
28 char winpath[MAX_PATH+3];
29 HMODULE mod;
30
31 GetWindowsDirectory(winpath, sizeof(winpath));
32 strcat(winpath, "\\*");
33 srch = FindFirstFile(winpath, &finddata);
34 if (srch != INVALID_HANDLE_VALUE) {
35 do {
36 func(&finddata, sizeof(finddata));
37 } while (FindNextFile(srch, &finddata));
38 FindClose(srch);
39 }
40
41 read_random_seed(func);
42
43 gsps = NULL;
44 mod = GetModuleHandle("KERNEL32");
45 if (mod) {
46 gsps = (gsps_t)GetProcAddress(mod, "GetSystemPowerStatus");
47 }
48 }
49
50 void random_save_seed(void) {
51 int len;
52 void *data;
53
54 random_get_savedata(&data, &len);
55 write_random_seed(data, len);
56 }
57
58 /*
59 * This function is called every time the random pool needs
60 * stirring, and will acquire the system time in all available
61 * forms and the battery status.
62 */
63 void noise_get_light(void (*func) (void *, int)) {
64 SYSTEMTIME systime;
65 DWORD adjust[2];
66 BOOL rubbish;
67 SYSTEM_POWER_STATUS pwrstat;
68
69 GetSystemTime(&systime);
70 func(&systime, sizeof(systime));
71
72 GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
73 func(&adjust, sizeof(adjust));
74
75 /*
76 * Call GetSystemPowerStatus if present.
77 */
78 if (gsps) {
79 if (gsps(&pwrstat))
80 func(&pwrstat, sizeof(pwrstat));
81 }
82 }
83
84 /*
85 * This function is called on a timer, and it will monitor
86 * frequently changing quantities such as the state of physical and
87 * virtual memory, the state of the process's message queue, which
88 * window is in the foreground, which owns the clipboard, etc.
89 */
90 void noise_regular(void) {
91 HWND w;
92 DWORD z;
93 POINT pt;
94 MEMORYSTATUS memstat;
95 FILETIME times[4];
96
97 w = GetForegroundWindow(); random_add_noise(&w, sizeof(w));
98 w = GetCapture(); random_add_noise(&w, sizeof(w));
99 w = GetClipboardOwner(); random_add_noise(&w, sizeof(w));
100 z = GetQueueStatus(QS_ALLEVENTS); random_add_noise(&z, sizeof(z));
101
102 GetCursorPos(&pt); random_add_noise(&pt, sizeof(pt));
103
104 GlobalMemoryStatus(&memstat); random_add_noise(&memstat, sizeof(memstat));
105
106 GetThreadTimes(GetCurrentThread(), times, times+1, times+2, times+3);
107 random_add_noise(&times, sizeof(times));
108 GetProcessTimes(GetCurrentProcess(), times, times+1, times+2, times+3);
109 random_add_noise(&times, sizeof(times));
110 }
111
112 /*
113 * This function is called on every keypress or mouse move, and
114 * will add the current Windows time and performance monitor
115 * counter to the noise pool. It gets the scan code or mouse
116 * position passed in.
117 */
118 void noise_ultralight(DWORD data) {
119 DWORD wintime;
120 LARGE_INTEGER perftime;
121
122 random_add_noise(&data, sizeof(DWORD));
123
124 wintime = GetTickCount();
125 random_add_noise(&wintime, sizeof(DWORD));
126
127 if (QueryPerformanceCounter(&perftime))
128 random_add_noise(&perftime, sizeof(perftime));
129 }