2 * Noise generation for PuTTY's cryptographic random number
13 * GetSystemPowerStatus function.
15 typedef BOOL(WINAPI * gsps_t) (LPSYSTEM_POWER_STATUS);
19 * This function is called once, at PuTTY startup, and will do some
20 * seriously silly things like listing directories and getting disk
21 * free space and a process snapshot.
24 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);
42 /* Update the seed immediately, in case another instance uses it. */
46 mod = GetModuleHandle("KERNEL32");
48 gsps = (gsps_t) GetProcAddress(mod, "GetSystemPowerStatus");
52 void random_save_seed(void)
58 random_get_savedata(&data, &len);
59 write_random_seed(data, len);
65 * This function is called every time the random pool needs
66 * stirring, and will acquire the system time in all available
67 * forms and the battery status.
69 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 a timer, and it will monitor
93 * frequently changing quantities such as the state of physical and
94 * virtual memory, the state of the process's message queue, which
95 * window is in the foreground, which owns the clipboard, etc.
97 void noise_regular(void)
102 MEMORYSTATUS memstat;
105 w = GetForegroundWindow();
106 random_add_noise(&w, sizeof(w));
108 random_add_noise(&w, sizeof(w));
109 w = GetClipboardOwner();
110 random_add_noise(&w, sizeof(w));
111 z = GetQueueStatus(QS_ALLEVENTS);
112 random_add_noise(&z, sizeof(z));
115 random_add_noise(&pt, sizeof(pt));
117 GlobalMemoryStatus(&memstat);
118 random_add_noise(&memstat, sizeof(memstat));
120 GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
122 random_add_noise(×, sizeof(times));
123 GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
125 random_add_noise(×, sizeof(times));
129 * This function is called on every keypress or mouse move, and
130 * will add the current Windows time and performance monitor
131 * counter to the noise pool. It gets the scan code or mouse
132 * position passed in.
134 void noise_ultralight(unsigned long data)
137 LARGE_INTEGER perftime;
139 random_add_noise(&data, sizeof(DWORD));
141 wintime = GetTickCount();
142 random_add_noise(&wintime, sizeof(DWORD));
144 if (QueryPerformanceCounter(&perftime))
145 random_add_noise(&perftime, sizeof(perftime));