]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winnoise.c
Now that we have Subversion's file renaming ability, it's time at
[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     char winpath[MAX_PATH + 3];
23
24     GetWindowsDirectory(winpath, sizeof(winpath));
25     strcat(winpath, "\\*");
26     srch = FindFirstFile(winpath, &finddata);
27     if (srch != INVALID_HANDLE_VALUE) {
28         do {
29             func(&finddata, sizeof(finddata));
30         } while (FindNextFile(srch, &finddata));
31         FindClose(srch);
32     }
33
34     read_random_seed(func);
35     /* Update the seed immediately, in case another instance uses it. */
36     random_save_seed();
37 }
38
39 void random_save_seed(void)
40 {
41     int len;
42     void *data;
43
44     if (random_active) {
45         random_get_savedata(&data, &len);
46         write_random_seed(data, len);
47         sfree(data);
48     }
49 }
50
51 /*
52  * This function is called every time the random pool needs
53  * stirring, and will acquire the system time in all available
54  * forms and the battery status.
55  */
56 void noise_get_light(void (*func) (void *, int))
57 {
58     SYSTEMTIME systime;
59     DWORD adjust[2];
60     BOOL rubbish;
61
62     GetSystemTime(&systime);
63     func(&systime, sizeof(systime));
64
65     GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
66     func(&adjust, sizeof(adjust));
67 }
68
69 /*
70  * This function is called on a timer, and it will monitor
71  * frequently changing quantities such as the state of physical and
72  * virtual memory, the state of the process's message queue, which
73  * window is in the foreground, which owns the clipboard, etc.
74  */
75 void noise_regular(void)
76 {
77     HWND w;
78     DWORD z;
79     POINT pt;
80     MEMORYSTATUS memstat;
81     FILETIME times[4];
82
83     w = GetForegroundWindow();
84     random_add_noise(&w, sizeof(w));
85     w = GetCapture();
86     random_add_noise(&w, sizeof(w));
87     w = GetClipboardOwner();
88     random_add_noise(&w, sizeof(w));
89     z = GetQueueStatus(QS_ALLEVENTS);
90     random_add_noise(&z, sizeof(z));
91
92     GetCursorPos(&pt);
93     random_add_noise(&pt, sizeof(pt));
94
95     GlobalMemoryStatus(&memstat);
96     random_add_noise(&memstat, sizeof(memstat));
97
98     GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
99                    times + 3);
100     random_add_noise(&times, sizeof(times));
101     GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
102                     times + 3);
103     random_add_noise(&times, sizeof(times));
104 }
105
106 /*
107  * This function is called on every keypress or mouse move, and
108  * will add the current Windows time and performance monitor
109  * counter to the noise pool. It gets the scan code or mouse
110  * position passed in.
111  */
112 void noise_ultralight(unsigned long data)
113 {
114     DWORD wintime;
115     LARGE_INTEGER perftime;
116
117     random_add_noise(&data, sizeof(DWORD));
118
119     wintime = GetTickCount();
120     random_add_noise(&wintime, sizeof(DWORD));
121
122     if (QueryPerformanceCounter(&perftime))
123         random_add_noise(&perftime, sizeof(perftime));
124 }