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 every keypress or mouse move, and
86 * will add the current Windows time and performance monitor
87 * counter to the noise pool. It gets the scan code or mouse
90 void noise_ultralight(DWORD data) {
92 LARGE_INTEGER perftime;
94 random_add_noise(&data, sizeof(DWORD));
96 wintime = GetTickCount();
97 random_add_noise(&wintime, sizeof(DWORD));
99 if (QueryPerformanceCounter(&perftime))
100 random_add_noise(&perftime, sizeof(perftime));