]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - noise.c
Update Makefile generation and ensure everything works with Borland 5.5
[PuTTY.git] / noise.c
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"
11 #include "storage.h"
12
13 /*
14  * GetSystemPowerStatus function.
15  */
16 typedef BOOL (WINAPI *gsps_t)(LPSYSTEM_POWER_STATUS);
17 static gsps_t gsps;
18
19 /*
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;
27     WIN32_FIND_DATA finddata;
28     char winpath[MAX_PATH+3];
29     HMODULE mod;
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
41     read_random_seed(func);
42
43     gsps = NULL;
44     mod = GetModuleHandle("KERNEL32");
45     if (mod) {
46         gsps = (gsps_t)GetProcAddress(mod, "GetSystemPowerStatus");
47     }
48 }
49
50 void random_save_seed(void) {
51     int len;
52     void *data;
53
54     random_get_savedata(&data, &len);
55     write_random_seed(data, len);
56 }
57
58 /*
59  * This function is called every time the random pool needs
60  * stirring, and will acquire the system time in all available
61  * forms and the battery status.
62  */
63 void noise_get_light(void (*func) (void *, int)) {
64     SYSTEMTIME systime;
65     DWORD adjust[2];
66     BOOL rubbish;
67     SYSTEM_POWER_STATUS pwrstat;
68
69     GetSystemTime(&systime);
70     func(&systime, sizeof(systime));
71
72     GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
73     func(&adjust, sizeof(adjust));
74
75     /*
76      * Call GetSystemPowerStatus if present.
77      */
78     if (gsps) {
79         if (gsps(&pwrstat))
80             func(&pwrstat, sizeof(pwrstat));
81     }
82 }
83
84 /*
85  * This function is called on every keypress or mouse move, and
86  * will add the current Windows time and performance monitor
87  * counter to the noise pool. It gets the scan code or mouse
88  * position passed in.
89  */
90 void noise_ultralight(DWORD data) {
91     DWORD wintime;
92     LARGE_INTEGER perftime;
93
94     random_add_noise(&data, sizeof(DWORD));
95
96     wintime = GetTickCount();
97     random_add_noise(&wintime, sizeof(DWORD));
98
99     if (QueryPerformanceCounter(&perftime))
100         random_add_noise(&perftime, sizeof(perftime));
101 }