2 * Noise generation for PuTTY's cryptographic random number
21 * GetSystemPowerStatus function.
23 typedef BOOL (WINAPI *gsps_t)(LPSYSTEM_POWER_STATUS);
27 * This function is called once, at PuTTY startup, and will do some
28 * seriously silly things like listing directories and getting disk
29 * free space and a process snapshot.
32 void noise_get_heavy(void (*func) (void *, int)) {
34 WIN32_FIND_DATA finddata;
35 char winpath[MAX_PATH+3];
38 GetWindowsDirectory(winpath, sizeof(winpath));
39 strcat(winpath, "\\*");
40 srch = FindFirstFile(winpath, &finddata);
41 if (srch != INVALID_HANDLE_VALUE) {
43 func(&finddata, sizeof(finddata));
44 } while (FindNextFile(srch, &finddata));
48 read_random_seed(func);
51 mod = GetModuleHandle("KERNEL32");
53 gsps = (gsps_t)GetProcAddress(mod, "GetSystemPowerStatus");
57 void random_save_seed(void) {
61 random_get_savedata(&data, &len);
62 write_random_seed(data, len);
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.
70 void noise_get_light(void (*func) (void *, int)) {
74 SYSTEM_POWER_STATUS pwrstat;
76 GetSystemTime(&systime);
77 func(&systime, sizeof(systime));
79 GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
80 func(&adjust, sizeof(adjust));
83 * Call GetSystemPowerStatus if present.
87 func(&pwrstat, sizeof(pwrstat));
92 * This function is called on every keypress or mouse move, and
93 * will add the current Windows time and performance monitor
94 * counter to the noise pool. It gets the scan code or mouse
97 void noise_ultralight(DWORD data) {
99 LARGE_INTEGER perftime;
101 random_add_noise(&data, sizeof(DWORD));
103 wintime = GetTickCount();
104 random_add_noise(&wintime, sizeof(DWORD));
106 if (QueryPerformanceCounter(&perftime))
107 random_add_noise(&perftime, sizeof(perftime));