]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winnoise.c
More careful owner SID selection in the Pageant client code. This
[PuTTY.git] / windows / winnoise.c
1 /*
2  * Noise generation for PuTTY's cryptographic random number
3  * generator.
4  */
5
6 #include <stdio.h>
7
8 #include "putty.h"
9 #include "ssh.h"
10 #include "storage.h"
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
18 void noise_get_heavy(void (*func) (void *, int))
19 {
20     HANDLE srch;
21     WIN32_FIND_DATA finddata;
22     DWORD pid;
23     char winpath[MAX_PATH + 3];
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
35     pid = GetCurrentProcessId();
36     func(&pid, sizeof(pid));
37
38     read_random_seed(func);
39     /* Update the seed immediately, in case another instance uses it. */
40     random_save_seed();
41 }
42
43 void random_save_seed(void)
44 {
45     int len;
46     void *data;
47
48     if (random_active) {
49         random_get_savedata(&data, &len);
50         write_random_seed(data, len);
51         sfree(data);
52     }
53 }
54
55 /*
56  * This function is called every time the random pool needs
57  * stirring, and will acquire the system time in all available
58  * forms.
59  */
60 void noise_get_light(void (*func) (void *, int))
61 {
62     SYSTEMTIME systime;
63     DWORD adjust[2];
64     BOOL rubbish;
65
66     GetSystemTime(&systime);
67     func(&systime, sizeof(systime));
68
69     GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
70     func(&adjust, sizeof(adjust));
71 }
72
73 /*
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  */
79 void noise_regular(void)
80 {
81     HWND w;
82     DWORD z;
83     POINT pt;
84     MEMORYSTATUS memstat;
85     FILETIME times[4];
86
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));
95
96     GetCursorPos(&pt);
97     random_add_noise(&pt, sizeof(pt));
98
99     GlobalMemoryStatus(&memstat);
100     random_add_noise(&memstat, sizeof(memstat));
101
102     GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
103                    times + 3);
104     random_add_noise(&times, sizeof(times));
105     GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
106                     times + 3);
107     random_add_noise(&times, sizeof(times));
108 }
109
110 /*
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  */
116 void noise_ultralight(unsigned long data)
117 {
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 }