OSDN Git Service

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