2 * Noise generation for PuTTY's cryptographic random number
\r
10 #include "storage.h"
\r
13 * This function is called once, at PuTTY startup, and will do some
\r
14 * seriously silly things like listing directories and getting disk
\r
15 * free space and a process snapshot.
\r
18 void noise_get_heavy(void (*func) (void *, int))
\r
21 WIN32_FIND_DATA finddata;
\r
23 char winpath[MAX_PATH + 3];
\r
25 GetWindowsDirectory(winpath, sizeof(winpath));
\r
26 strcat(winpath, "\\*");
\r
27 srch = FindFirstFile(winpath, &finddata);
\r
28 if (srch != INVALID_HANDLE_VALUE) {
\r
30 func(&finddata, sizeof(finddata));
\r
31 } while (FindNextFile(srch, &finddata));
\r
35 pid = GetCurrentProcessId();
\r
36 func(&pid, sizeof(pid));
\r
38 read_random_seed(func);
\r
39 /* Update the seed immediately, in case another instance uses it. */
\r
43 void random_save_seed(void)
\r
48 if (random_active) {
\r
49 random_get_savedata(&data, &len);
\r
50 write_random_seed(data, len);
\r
56 * This function is called every time the random pool needs
\r
57 * stirring, and will acquire the system time in all available
\r
60 void noise_get_light(void (*func) (void *, int))
\r
66 GetSystemTime(&systime);
\r
67 func(&systime, sizeof(systime));
\r
69 GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
\r
70 func(&adjust, sizeof(adjust));
\r
74 * This function is called on a timer, and it will monitor
\r
75 * frequently changing quantities such as the state of physical and
\r
76 * virtual memory, the state of the process's message queue, which
\r
77 * window is in the foreground, which owns the clipboard, etc.
\r
79 void noise_regular(void)
\r
84 MEMORYSTATUS memstat;
\r
87 w = GetForegroundWindow();
\r
88 random_add_noise(&w, sizeof(w));
\r
90 random_add_noise(&w, sizeof(w));
\r
91 w = GetClipboardOwner();
\r
92 random_add_noise(&w, sizeof(w));
\r
93 z = GetQueueStatus(QS_ALLEVENTS);
\r
94 random_add_noise(&z, sizeof(z));
\r
97 random_add_noise(&pt, sizeof(pt));
\r
99 GlobalMemoryStatus(&memstat);
\r
100 random_add_noise(&memstat, sizeof(memstat));
\r
102 GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
\r
104 random_add_noise(×, sizeof(times));
\r
105 GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
\r
107 random_add_noise(×, sizeof(times));
\r
111 * This function is called on every keypress or mouse move, and
\r
112 * will add the current Windows time and performance monitor
\r
113 * counter to the noise pool. It gets the scan code or mouse
\r
114 * position passed in.
\r
116 void noise_ultralight(unsigned long data)
\r
119 LARGE_INTEGER perftime;
\r
121 random_add_noise(&data, sizeof(DWORD));
\r
123 wintime = GetTickCount();
\r
124 random_add_noise(&wintime, sizeof(DWORD));
\r
126 if (QueryPerformanceCounter(&perftime))
\r
127 random_add_noise(&perftime, sizeof(perftime));
\r