2 * Noise generation for PuTTY's cryptographic random number
14 * GetSystemPowerStatus function.
16 typedef BOOL (WINAPI
*gsps_t
)(LPSYSTEM_POWER_STATUS
);
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.
25 void noise_get_heavy(void (*func
) (void *, int)) {
27 WIN32_FIND_DATA finddata
;
28 char winpath
[MAX_PATH
+3];
31 GetWindowsDirectory(winpath
, sizeof(winpath
));
32 strcat(winpath
, "\\*");
33 srch
= FindFirstFile(winpath
, &finddata
);
34 if (srch
!= INVALID_HANDLE_VALUE
) {
36 func(&finddata
, sizeof(finddata
));
37 } while (FindNextFile(srch
, &finddata
));
41 read_random_seed(func
);
44 mod
= GetModuleHandle("KERNEL32");
46 gsps
= (gsps_t
)GetProcAddress(mod
, "GetSystemPowerStatus");
50 void random_save_seed(void) {
54 random_get_savedata(&data
, &len
);
55 write_random_seed(data
, len
);
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.
63 void noise_get_light(void (*func
) (void *, int)) {
67 SYSTEM_POWER_STATUS pwrstat
;
69 GetSystemTime(&systime
);
70 func(&systime
, sizeof(systime
));
72 GetSystemTimeAdjustment(&adjust
[0], &adjust
[1], &rubbish
);
73 func(&adjust
, sizeof(adjust
));
76 * Call GetSystemPowerStatus if present.
80 func(&pwrstat
, sizeof(pwrstat
));
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.
90 void noise_regular(void) {
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
));
102 GetCursorPos(&pt
); random_add_noise(&pt
, sizeof(pt
));
104 GlobalMemoryStatus(&memstat
); random_add_noise(&memstat
, sizeof(memstat
));
106 GetThreadTimes(GetCurrentThread(), times
, times
+1, times
+2, times
+3);
107 random_add_noise(×
, sizeof(times
));
108 GetProcessTimes(GetCurrentProcess(), times
, times
+1, times
+2, times
+3);
109 random_add_noise(×
, sizeof(times
));
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.
118 void noise_ultralight(DWORD data
) {
120 LARGE_INTEGER perftime
;
122 random_add_noise(&data
, sizeof(DWORD
));
124 wintime
= GetTickCount();
125 random_add_noise(&wintime
, sizeof(DWORD
));
127 if (QueryPerformanceCounter(&perftime
))
128 random_add_noise(&perftime
, sizeof(perftime
));