]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - noise.c
Add help support to Pageant. (For the primary - non-context - help,
[PuTTY.git] / 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
44     gsps = NULL;
45     mod = GetModuleHandle("KERNEL32");
46     if (mod) {
47         gsps = (gsps_t) GetProcAddress(mod, "GetSystemPowerStatus");
48     }
49 }
50
51 void random_save_seed(void)
52 {
53     int len;
54     void *data;
55
56     random_get_savedata(&data, &len);
57     write_random_seed(data, len);
58 }
59
60 /*
61  * This function is called every time the random pool needs
62  * stirring, and will acquire the system time in all available
63  * forms and the battery status.
64  */
65 void noise_get_light(void (*func) (void *, int))
66 {
67     SYSTEMTIME systime;
68     DWORD adjust[2];
69     BOOL rubbish;
70     SYSTEM_POWER_STATUS pwrstat;
71
72     GetSystemTime(&systime);
73     func(&systime, sizeof(systime));
74
75     GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
76     func(&adjust, sizeof(adjust));
77
78     /*
79      * Call GetSystemPowerStatus if present.
80      */
81     if (gsps) {
82         if (gsps(&pwrstat))
83             func(&pwrstat, sizeof(pwrstat));
84     }
85 }
86
87 /*
88  * This function is called on a timer, and it will monitor
89  * frequently changing quantities such as the state of physical and
90  * virtual memory, the state of the process's message queue, which
91  * window is in the foreground, which owns the clipboard, etc.
92  */
93 void noise_regular(void)
94 {
95     HWND w;
96     DWORD z;
97     POINT pt;
98     MEMORYSTATUS memstat;
99     FILETIME times[4];
100
101     w = GetForegroundWindow();
102     random_add_noise(&w, sizeof(w));
103     w = GetCapture();
104     random_add_noise(&w, sizeof(w));
105     w = GetClipboardOwner();
106     random_add_noise(&w, sizeof(w));
107     z = GetQueueStatus(QS_ALLEVENTS);
108     random_add_noise(&z, sizeof(z));
109
110     GetCursorPos(&pt);
111     random_add_noise(&pt, sizeof(pt));
112
113     GlobalMemoryStatus(&memstat);
114     random_add_noise(&memstat, sizeof(memstat));
115
116     GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
117                    times + 3);
118     random_add_noise(&times, sizeof(times));
119     GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
120                     times + 3);
121     random_add_noise(&times, sizeof(times));
122 }
123
124 /*
125  * This function is called on every keypress or mouse move, and
126  * will add the current Windows time and performance monitor
127  * counter to the noise pool. It gets the scan code or mouse
128  * position passed in.
129  */
130 void noise_ultralight(DWORD data)
131 {
132     DWORD wintime;
133     LARGE_INTEGER perftime;
134
135     random_add_noise(&data, sizeof(DWORD));
136
137     wintime = GetTickCount();
138     random_add_noise(&wintime, sizeof(DWORD));
139
140     if (QueryPerformanceCounter(&perftime))
141         random_add_noise(&perftime, sizeof(perftime));
142 }