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))
28 WIN32_FIND_DATA finddata;
29 char winpath[MAX_PATH + 3];
32 GetWindowsDirectory(winpath, sizeof(winpath));
33 strcat(winpath, "\\*");
34 srch = FindFirstFile(winpath, &finddata);
35 if (srch != INVALID_HANDLE_VALUE) {
37 func(&finddata, sizeof(finddata));
38 } while (FindNextFile(srch, &finddata));
42 read_random_seed(func);
43 /* Update the seed immediately, in case another instance uses it. */
47 mod = GetModuleHandle("KERNEL32");
49 gsps = (gsps_t) GetProcAddress(mod, "GetSystemPowerStatus");
53 void random_save_seed(void)
59 random_get_savedata(&data, &len);
60 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))
75 SYSTEM_POWER_STATUS pwrstat;
77 GetSystemTime(&systime);
78 func(&systime, sizeof(systime));
80 GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
81 func(&adjust, sizeof(adjust));
84 * Call GetSystemPowerStatus if present.
88 func(&pwrstat, sizeof(pwrstat));
93 * This function is called on a timer, and it will monitor
94 * frequently changing quantities such as the state of physical and
95 * virtual memory, the state of the process's message queue, which
96 * window is in the foreground, which owns the clipboard, etc.
98 void noise_regular(void)
103 MEMORYSTATUS memstat;
106 w = GetForegroundWindow();
107 random_add_noise(&w, sizeof(w));
109 random_add_noise(&w, sizeof(w));
110 w = GetClipboardOwner();
111 random_add_noise(&w, sizeof(w));
112 z = GetQueueStatus(QS_ALLEVENTS);
113 random_add_noise(&z, sizeof(z));
116 random_add_noise(&pt, sizeof(pt));
118 GlobalMemoryStatus(&memstat);
119 random_add_noise(&memstat, sizeof(memstat));
121 GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
123 random_add_noise(×, sizeof(times));
124 GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
126 random_add_noise(×, sizeof(times));
130 * This function is called on every keypress or mouse move, and
131 * will add the current Windows time and performance monitor
132 * counter to the noise pool. It gets the scan code or mouse
133 * position passed in.
135 void noise_ultralight(unsigned long data)
138 LARGE_INTEGER perftime;
140 random_add_noise(&data, sizeof(DWORD));
142 wintime = GetTickCount();
143 random_add_noise(&wintime, sizeof(DWORD));
145 if (QueryPerformanceCounter(&perftime))
146 random_add_noise(&perftime, sizeof(perftime));