374330e2 |
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" |
d5859615 |
11 | #include "storage.h" |
374330e2 |
12 | |
13 | /* |
a4e14164 |
14 | * GetSystemPowerStatus function. |
15 | */ |
32874aea |
16 | typedef BOOL(WINAPI * gsps_t) (LPSYSTEM_POWER_STATUS); |
75cab814 |
17 | static gsps_t gsps; |
a4e14164 |
18 | |
19 | /* |
374330e2 |
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 | |
32874aea |
25 | void noise_get_heavy(void (*func) (void *, int)) |
26 | { |
374330e2 |
27 | HANDLE srch; |
374330e2 |
28 | WIN32_FIND_DATA finddata; |
32874aea |
29 | char winpath[MAX_PATH + 3]; |
a4e14164 |
30 | HMODULE mod; |
374330e2 |
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 | |
d5859615 |
42 | read_random_seed(func); |
e3ac3c05 |
43 | /* Update the seed immediately, in case another instance uses it. */ |
44 | random_save_seed(); |
a4e14164 |
45 | |
46 | gsps = NULL; |
47 | mod = GetModuleHandle("KERNEL32"); |
48 | if (mod) { |
32874aea |
49 | gsps = (gsps_t) GetProcAddress(mod, "GetSystemPowerStatus"); |
a4e14164 |
50 | } |
374330e2 |
51 | } |
52 | |
32874aea |
53 | void random_save_seed(void) |
54 | { |
d5859615 |
55 | int len; |
56 | void *data; |
374330e2 |
57 | |
93b581bd |
58 | if (random_active) { |
59 | random_get_savedata(&data, &len); |
60 | write_random_seed(data, len); |
e3ac3c05 |
61 | sfree(data); |
93b581bd |
62 | } |
de3df031 |
63 | } |
64 | |
65 | /* |
374330e2 |
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 | */ |
32874aea |
70 | void noise_get_light(void (*func) (void *, int)) |
71 | { |
374330e2 |
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 | |
a4e14164 |
83 | /* |
84 | * Call GetSystemPowerStatus if present. |
85 | */ |
86 | if (gsps) { |
32874aea |
87 | if (gsps(&pwrstat)) |
88 | func(&pwrstat, sizeof(pwrstat)); |
a4e14164 |
89 | } |
374330e2 |
90 | } |
91 | |
92 | /* |
7d6ee6ff |
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 | */ |
32874aea |
98 | void noise_regular(void) |
99 | { |
7d6ee6ff |
100 | HWND w; |
101 | DWORD z; |
102 | POINT pt; |
103 | MEMORYSTATUS memstat; |
104 | FILETIME times[4]; |
105 | |
32874aea |
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)); |
7d6ee6ff |
114 | |
32874aea |
115 | GetCursorPos(&pt); |
116 | random_add_noise(&pt, sizeof(pt)); |
7d6ee6ff |
117 | |
32874aea |
118 | GlobalMemoryStatus(&memstat); |
119 | random_add_noise(&memstat, sizeof(memstat)); |
7d6ee6ff |
120 | |
32874aea |
121 | GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2, |
122 | times + 3); |
7d6ee6ff |
123 | random_add_noise(×, sizeof(times)); |
32874aea |
124 | GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2, |
125 | times + 3); |
7d6ee6ff |
126 | random_add_noise(×, sizeof(times)); |
127 | } |
128 | |
129 | /* |
374330e2 |
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 | */ |
f7f27309 |
135 | void noise_ultralight(unsigned long data) |
32874aea |
136 | { |
374330e2 |
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 | } |