374330e2 |
1 | /* |
2 | * Noise generation for PuTTY's cryptographic random number |
3 | * generator. |
4 | */ |
5 | |
6 | #include <windows.h> |
7 | #include <stdio.h> |
8 | |
9 | #include "putty.h" |
10 | #include "ssh.h" |
d5859615 |
11 | #include "storage.h" |
374330e2 |
12 | |
13 | /* |
a4e14164 |
14 | * GetSystemPowerStatus function. |
15 | */ |
16 | typedef BOOL (WINAPI *gsps_t)(LPSYSTEM_POWER_STATUS); |
75cab814 |
17 | static gsps_t gsps; |
a4e14164 |
18 | |
19 | /* |
374330e2 |
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. |
23 | */ |
24 | |
25 | void noise_get_heavy(void (*func) (void *, int)) { |
26 | HANDLE srch; |
374330e2 |
27 | WIN32_FIND_DATA finddata; |
28 | char winpath[MAX_PATH+3]; |
a4e14164 |
29 | HMODULE mod; |
374330e2 |
30 | |
31 | GetWindowsDirectory(winpath, sizeof(winpath)); |
32 | strcat(winpath, "\\*"); |
33 | srch = FindFirstFile(winpath, &finddata); |
34 | if (srch != INVALID_HANDLE_VALUE) { |
35 | do { |
36 | func(&finddata, sizeof(finddata)); |
37 | } while (FindNextFile(srch, &finddata)); |
38 | FindClose(srch); |
39 | } |
40 | |
d5859615 |
41 | read_random_seed(func); |
a4e14164 |
42 | |
43 | gsps = NULL; |
44 | mod = GetModuleHandle("KERNEL32"); |
45 | if (mod) { |
46 | gsps = (gsps_t)GetProcAddress(mod, "GetSystemPowerStatus"); |
a4e14164 |
47 | } |
374330e2 |
48 | } |
49 | |
50 | void random_save_seed(void) { |
d5859615 |
51 | int len; |
52 | void *data; |
374330e2 |
53 | |
d5859615 |
54 | random_get_savedata(&data, &len); |
55 | write_random_seed(data, len); |
de3df031 |
56 | } |
57 | |
58 | /* |
374330e2 |
59 | * This function is called every time the random pool needs |
60 | * stirring, and will acquire the system time in all available |
61 | * forms and the battery status. |
62 | */ |
63 | void noise_get_light(void (*func) (void *, int)) { |
64 | SYSTEMTIME systime; |
65 | DWORD adjust[2]; |
66 | BOOL rubbish; |
67 | SYSTEM_POWER_STATUS pwrstat; |
68 | |
69 | GetSystemTime(&systime); |
70 | func(&systime, sizeof(systime)); |
71 | |
72 | GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish); |
73 | func(&adjust, sizeof(adjust)); |
74 | |
a4e14164 |
75 | /* |
76 | * Call GetSystemPowerStatus if present. |
77 | */ |
78 | if (gsps) { |
79 | if (gsps(&pwrstat)) |
80 | func(&pwrstat, sizeof(pwrstat)); |
81 | } |
374330e2 |
82 | } |
83 | |
84 | /* |
85 | * This function is called on every keypress or mouse move, and |
86 | * will add the current Windows time and performance monitor |
87 | * counter to the noise pool. It gets the scan code or mouse |
88 | * position passed in. |
89 | */ |
90 | void noise_ultralight(DWORD data) { |
91 | DWORD wintime; |
92 | LARGE_INTEGER perftime; |
93 | |
94 | random_add_noise(&data, sizeof(DWORD)); |
95 | |
96 | wintime = GetTickCount(); |
97 | random_add_noise(&wintime, sizeof(DWORD)); |
98 | |
99 | if (QueryPerformanceCounter(&perftime)) |
100 | random_add_noise(&perftime, sizeof(perftime)); |
101 | } |