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