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");
47 debug(("got gsps=%p\n", gsps));
51 void random_save_seed(void) {
55 random_get_savedata(&data, &len);
56 write_random_seed(data, len);
60 * This function is called every time the random pool needs
61 * stirring, and will acquire the system time in all available
62 * forms and the battery status.
64 void noise_get_light(void (*func) (void *, int)) {
68 SYSTEM_POWER_STATUS pwrstat;
70 GetSystemTime(&systime);
71 func(&systime, sizeof(systime));
73 GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
74 func(&adjust, sizeof(adjust));
77 * Call GetSystemPowerStatus if present.
81 func(&pwrstat, sizeof(pwrstat));
86 * This function is called on every keypress or mouse move, and
87 * will add the current Windows time and performance monitor
88 * counter to the noise pool. It gets the scan code or mouse
91 void noise_ultralight(DWORD data) {
93 LARGE_INTEGER perftime;
95 random_add_noise(&data, sizeof(DWORD));
97 wintime = GetTickCount();
98 random_add_noise(&wintime, sizeof(DWORD));
100 if (QueryPerformanceCounter(&perftime))
101 random_add_noise(&perftime, sizeof(perftime));