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
);
45 mod
= GetModuleHandle("KERNEL32");
47 gsps
= (gsps_t
) GetProcAddress(mod
, "GetSystemPowerStatus");
51 void random_save_seed(void)
56 random_get_savedata(&data
, &len
);
57 write_random_seed(data
, len
);
61 * This function is called every time the random pool needs
62 * stirring, and will acquire the system time in all available
63 * forms and the battery status.
65 void noise_get_light(void (*func
) (void *, int))
70 SYSTEM_POWER_STATUS pwrstat
;
72 GetSystemTime(&systime
);
73 func(&systime
, sizeof(systime
));
75 GetSystemTimeAdjustment(&adjust
[0], &adjust
[1], &rubbish
);
76 func(&adjust
, sizeof(adjust
));
79 * Call GetSystemPowerStatus if present.
83 func(&pwrstat
, sizeof(pwrstat
));
88 * This function is called on a timer, and it will monitor
89 * frequently changing quantities such as the state of physical and
90 * virtual memory, the state of the process's message queue, which
91 * window is in the foreground, which owns the clipboard, etc.
93 void noise_regular(void)
101 w
= GetForegroundWindow();
102 random_add_noise(&w
, sizeof(w
));
104 random_add_noise(&w
, sizeof(w
));
105 w
= GetClipboardOwner();
106 random_add_noise(&w
, sizeof(w
));
107 z
= GetQueueStatus(QS_ALLEVENTS
);
108 random_add_noise(&z
, sizeof(z
));
111 random_add_noise(&pt
, sizeof(pt
));
113 GlobalMemoryStatus(&memstat
);
114 random_add_noise(&memstat
, sizeof(memstat
));
116 GetThreadTimes(GetCurrentThread(), times
, times
+ 1, times
+ 2,
118 random_add_noise(×
, sizeof(times
));
119 GetProcessTimes(GetCurrentProcess(), times
, times
+ 1, times
+ 2,
121 random_add_noise(×
, sizeof(times
));
125 * This function is called on every keypress or mouse move, and
126 * will add the current Windows time and performance monitor
127 * counter to the noise pool. It gets the scan code or mouse
128 * position passed in.
130 void noise_ultralight(DWORD data
)
133 LARGE_INTEGER perftime
;
135 random_add_noise(&data
, sizeof(DWORD
));
137 wintime
= GetTickCount();
138 random_add_noise(&wintime
, sizeof(DWORD
));
140 if (QueryPerformanceCounter(&perftime
))
141 random_add_noise(&perftime
, sizeof(perftime
));