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
;
22 char winpath
[MAX_PATH
+ 3];
24 GetWindowsDirectory(winpath
, sizeof(winpath
));
25 strcat(winpath
, "\\*");
26 srch
= FindFirstFile(winpath
, &finddata
);
27 if (srch
!= INVALID_HANDLE_VALUE
) {
29 func(&finddata
, sizeof(finddata
));
30 } while (FindNextFile(srch
, &finddata
));
34 read_random_seed(func
);
35 /* Update the seed immediately, in case another instance uses it. */
39 void random_save_seed(void)
45 random_get_savedata(&data
, &len
);
46 write_random_seed(data
, len
);
52 * This function is called every time the random pool needs
53 * stirring, and will acquire the system time in all available
54 * forms and the battery status.
56 void noise_get_light(void (*func
) (void *, int))
61 SYSTEM_POWER_STATUS pwrstat
;
63 GetSystemTime(&systime
);
64 func(&systime
, sizeof(systime
));
66 GetSystemTimeAdjustment(&adjust
[0], &adjust
[1], &rubbish
);
67 func(&adjust
, sizeof(adjust
));
71 * This function is called on a timer, and it will monitor
72 * frequently changing quantities such as the state of physical and
73 * virtual memory, the state of the process's message queue, which
74 * window is in the foreground, which owns the clipboard, etc.
76 void noise_regular(void)
84 w
= GetForegroundWindow();
85 random_add_noise(&w
, sizeof(w
));
87 random_add_noise(&w
, sizeof(w
));
88 w
= GetClipboardOwner();
89 random_add_noise(&w
, sizeof(w
));
90 z
= GetQueueStatus(QS_ALLEVENTS
);
91 random_add_noise(&z
, sizeof(z
));
94 random_add_noise(&pt
, sizeof(pt
));
96 GlobalMemoryStatus(&memstat
);
97 random_add_noise(&memstat
, sizeof(memstat
));
99 GetThreadTimes(GetCurrentThread(), times
, times
+ 1, times
+ 2,
101 random_add_noise(×
, sizeof(times
));
102 GetProcessTimes(GetCurrentProcess(), times
, times
+ 1, times
+ 2,
104 random_add_noise(×
, sizeof(times
));
108 * This function is called on every keypress or mouse move, and
109 * will add the current Windows time and performance monitor
110 * counter to the noise pool. It gets the scan code or mouse
111 * position passed in.
113 void noise_ultralight(unsigned long data
)
116 LARGE_INTEGER perftime
;
118 random_add_noise(&data
, sizeof(DWORD
));
120 wintime
= GetTickCount();
121 random_add_noise(&wintime
, sizeof(DWORD
));
123 if (QueryPerformanceCounter(&perftime
))
124 random_add_noise(&perftime
, sizeof(perftime
));