374330e2 |
1 | /* |
2 | * Noise generation for PuTTY's cryptographic random number |
3 | * generator. |
4 | */ |
5 | |
374330e2 |
6 | #include <stdio.h> |
7 | |
8 | #include "putty.h" |
9 | #include "ssh.h" |
d5859615 |
10 | #include "storage.h" |
374330e2 |
11 | |
12 | /* |
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. |
16 | */ |
17 | |
32874aea |
18 | void noise_get_heavy(void (*func) (void *, int)) |
19 | { |
374330e2 |
20 | HANDLE srch; |
374330e2 |
21 | WIN32_FIND_DATA finddata; |
a5c2148b |
22 | DWORD pid; |
32874aea |
23 | char winpath[MAX_PATH + 3]; |
374330e2 |
24 | |
25 | GetWindowsDirectory(winpath, sizeof(winpath)); |
26 | strcat(winpath, "\\*"); |
27 | srch = FindFirstFile(winpath, &finddata); |
28 | if (srch != INVALID_HANDLE_VALUE) { |
29 | do { |
30 | func(&finddata, sizeof(finddata)); |
31 | } while (FindNextFile(srch, &finddata)); |
32 | FindClose(srch); |
33 | } |
34 | |
a5c2148b |
35 | pid = GetCurrentProcessId(); |
36 | func(&pid, sizeof(pid)); |
37 | |
d5859615 |
38 | read_random_seed(func); |
e3ac3c05 |
39 | /* Update the seed immediately, in case another instance uses it. */ |
40 | random_save_seed(); |
374330e2 |
41 | } |
42 | |
32874aea |
43 | void random_save_seed(void) |
44 | { |
d5859615 |
45 | int len; |
46 | void *data; |
374330e2 |
47 | |
93b581bd |
48 | if (random_active) { |
49 | random_get_savedata(&data, &len); |
50 | write_random_seed(data, len); |
e3ac3c05 |
51 | sfree(data); |
93b581bd |
52 | } |
de3df031 |
53 | } |
54 | |
55 | /* |
374330e2 |
56 | * This function is called every time the random pool needs |
57 | * stirring, and will acquire the system time in all available |
a5c2148b |
58 | * forms. |
374330e2 |
59 | */ |
32874aea |
60 | void noise_get_light(void (*func) (void *, int)) |
61 | { |
374330e2 |
62 | SYSTEMTIME systime; |
63 | DWORD adjust[2]; |
64 | BOOL rubbish; |
374330e2 |
65 | |
66 | GetSystemTime(&systime); |
67 | func(&systime, sizeof(systime)); |
68 | |
69 | GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish); |
70 | func(&adjust, sizeof(adjust)); |
374330e2 |
71 | } |
72 | |
73 | /* |
7d6ee6ff |
74 | * This function is called on a timer, and it will monitor |
75 | * frequently changing quantities such as the state of physical and |
76 | * virtual memory, the state of the process's message queue, which |
77 | * window is in the foreground, which owns the clipboard, etc. |
78 | */ |
32874aea |
79 | void noise_regular(void) |
80 | { |
7d6ee6ff |
81 | HWND w; |
82 | DWORD z; |
83 | POINT pt; |
84 | MEMORYSTATUS memstat; |
85 | FILETIME times[4]; |
86 | |
32874aea |
87 | w = GetForegroundWindow(); |
88 | random_add_noise(&w, sizeof(w)); |
89 | w = GetCapture(); |
90 | random_add_noise(&w, sizeof(w)); |
91 | w = GetClipboardOwner(); |
92 | random_add_noise(&w, sizeof(w)); |
93 | z = GetQueueStatus(QS_ALLEVENTS); |
94 | random_add_noise(&z, sizeof(z)); |
7d6ee6ff |
95 | |
32874aea |
96 | GetCursorPos(&pt); |
97 | random_add_noise(&pt, sizeof(pt)); |
7d6ee6ff |
98 | |
32874aea |
99 | GlobalMemoryStatus(&memstat); |
100 | random_add_noise(&memstat, sizeof(memstat)); |
7d6ee6ff |
101 | |
32874aea |
102 | GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2, |
103 | times + 3); |
7d6ee6ff |
104 | random_add_noise(×, sizeof(times)); |
32874aea |
105 | GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2, |
106 | times + 3); |
7d6ee6ff |
107 | random_add_noise(×, sizeof(times)); |
108 | } |
109 | |
110 | /* |
374330e2 |
111 | * This function is called on every keypress or mouse move, and |
112 | * will add the current Windows time and performance monitor |
113 | * counter to the noise pool. It gets the scan code or mouse |
114 | * position passed in. |
115 | */ |
f7f27309 |
116 | void noise_ultralight(unsigned long data) |
32874aea |
117 | { |
374330e2 |
118 | DWORD wintime; |
119 | LARGE_INTEGER perftime; |
120 | |
121 | random_add_noise(&data, sizeof(DWORD)); |
122 | |
123 | wintime = GetTickCount(); |
124 | random_add_noise(&wintime, sizeof(DWORD)); |
125 | |
126 | if (QueryPerformanceCounter(&perftime)) |
127 | random_add_noise(&perftime, sizeof(perftime)); |
128 | } |