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