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); |
17 | gsps_t gsps; |
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"); |
47 | debug(("got gsps=%p\n", gsps)); |
48 | } |
374330e2 |
49 | } |
50 | |
51 | void random_save_seed(void) { |
d5859615 |
52 | int len; |
53 | void *data; |
374330e2 |
54 | |
d5859615 |
55 | random_get_savedata(&data, &len); |
56 | write_random_seed(data, len); |
de3df031 |
57 | } |
58 | |
59 | /* |
374330e2 |
60 | * This function is called every time the random pool needs |
61 | * stirring, and will acquire the system time in all available |
62 | * forms and the battery status. |
63 | */ |
64 | void noise_get_light(void (*func) (void *, int)) { |
65 | SYSTEMTIME systime; |
66 | DWORD adjust[2]; |
67 | BOOL rubbish; |
68 | SYSTEM_POWER_STATUS pwrstat; |
69 | |
70 | GetSystemTime(&systime); |
71 | func(&systime, sizeof(systime)); |
72 | |
73 | GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish); |
74 | func(&adjust, sizeof(adjust)); |
75 | |
a4e14164 |
76 | /* |
77 | * Call GetSystemPowerStatus if present. |
78 | */ |
79 | if (gsps) { |
80 | if (gsps(&pwrstat)) |
81 | func(&pwrstat, sizeof(pwrstat)); |
82 | } |
374330e2 |
83 | } |
84 | |
85 | /* |
86 | * This function is called on every keypress or mouse move, and |
87 | * will add the current Windows time and performance monitor |
88 | * counter to the noise pool. It gets the scan code or mouse |
89 | * position passed in. |
90 | */ |
91 | void noise_ultralight(DWORD data) { |
92 | DWORD wintime; |
93 | LARGE_INTEGER perftime; |
94 | |
95 | random_add_noise(&data, sizeof(DWORD)); |
96 | |
97 | wintime = GetTickCount(); |
98 | random_add_noise(&wintime, sizeof(DWORD)); |
99 | |
100 | if (QueryPerformanceCounter(&perftime)) |
101 | random_add_noise(&perftime, sizeof(perftime)); |
102 | } |