2 * Noise generation for PuTTY's cryptographic random number
12 static char seedpath[2*MAX_PATH+10] = "\0";
15 * Find the random seed file path and store it in `seedpath'.
17 static void get_seedpath(void) {
21 size = sizeof(seedpath);
23 if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS, &rkey)==ERROR_SUCCESS) {
24 int ret = RegQueryValueEx(rkey, "RandSeedFile",
25 0, &type, seedpath, &size);
26 if (ret != ERROR_SUCCESS || type != REG_SZ)
35 len = GetEnvironmentVariable("HOMEDRIVE", seedpath, sizeof(seedpath));
36 ret = GetEnvironmentVariable("HOMEPATH", seedpath+len,
37 sizeof(seedpath)-len);
38 if (ret == 0) { /* probably win95; store in \WINDOWS */
39 GetWindowsDirectory(seedpath, sizeof(seedpath));
40 len = strlen(seedpath);
43 strcpy(seedpath+len, "\\PUTTY.RND");
48 * This function is called once, at PuTTY startup, and will do some
49 * seriously silly things like listing directories and getting disk
50 * free space and a process snapshot.
53 void noise_get_heavy(void (*func) (void *, int)) {
56 WIN32_FIND_DATA finddata;
57 char winpath[MAX_PATH+3];
59 GetWindowsDirectory(winpath, sizeof(winpath));
60 strcat(winpath, "\\*");
61 srch = FindFirstFile(winpath, &finddata);
62 if (srch != INVALID_HANDLE_VALUE) {
64 func(&finddata, sizeof(finddata));
65 } while (FindNextFile(srch, &finddata));
72 seedf = CreateFile(seedpath, GENERIC_READ,
73 FILE_SHARE_READ | FILE_SHARE_WRITE,
74 NULL, OPEN_EXISTING, 0, NULL);
76 if (seedf != INVALID_HANDLE_VALUE) {
81 if (ReadFile(seedf, buf, sizeof(buf), &len, NULL) && len)
90 void random_save_seed(void) {
96 seedf = CreateFile(seedpath, GENERIC_WRITE, 0,
97 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
99 if (seedf != INVALID_HANDLE_VALUE) {
104 random_get_savedata(&data, &len);
105 WriteFile(seedf, data, len, &lenwritten, NULL);
111 * This function is called every time the random pool needs
112 * stirring, and will acquire the system time in all available
113 * forms and the battery status.
115 void noise_get_light(void (*func) (void *, int)) {
119 SYSTEM_POWER_STATUS pwrstat;
121 GetSystemTime(&systime);
122 func(&systime, sizeof(systime));
124 GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
125 func(&adjust, sizeof(adjust));
127 if (GetSystemPowerStatus(&pwrstat))
128 func(&pwrstat, sizeof(pwrstat));
132 * This function is called on every keypress or mouse move, and
133 * will add the current Windows time and performance monitor
134 * counter to the noise pool. It gets the scan code or mouse
135 * position passed in.
137 void noise_ultralight(DWORD data) {
139 LARGE_INTEGER perftime;
141 random_add_noise(&data, sizeof(DWORD));
143 wintime = GetTickCount();
144 random_add_noise(&wintime, sizeof(DWORD));
146 if (QueryPerformanceCounter(&perftime))
147 random_add_noise(&perftime, sizeof(perftime));