Rationalised host key storage. Also started code reorg: persistent-state
[u/mdw/putty] / 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 * This function is called once, at PuTTY startup, and will do some
15 * seriously silly things like listing directories and getting disk
16 * free space and a process snapshot.
17 */
18
19 void noise_get_heavy(void (*func) (void *, int)) {
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 }
36
37 void random_save_seed(void) {
38 int len;
39 void *data;
40
41 random_get_savedata(&data, &len);
42 write_random_seed(data, len);
43 }
44
45 /*
46 * This function is called every time the random pool needs
47 * stirring, and will acquire the system time in all available
48 * forms and the battery status.
49 */
50 void noise_get_light(void (*func) (void *, int)) {
51 SYSTEMTIME systime;
52 DWORD adjust[2];
53 BOOL rubbish;
54 SYSTEM_POWER_STATUS pwrstat;
55
56 GetSystemTime(&systime);
57 func(&systime, sizeof(systime));
58
59 GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
60 func(&adjust, sizeof(adjust));
61
62 #ifndef WIN32S_COMPAT
63 if (GetSystemPowerStatus(&pwrstat))
64 func(&pwrstat, sizeof(pwrstat));
65 #endif
66 }
67
68 /*
69 * This function is called on every keypress or mouse move, and
70 * will add the current Windows time and performance monitor
71 * counter to the noise pool. It gets the scan code or mouse
72 * position passed in.
73 */
74 void noise_ultralight(DWORD data) {
75 DWORD wintime;
76 LARGE_INTEGER perftime;
77
78 random_add_noise(&data, sizeof(DWORD));
79
80 wintime = GetTickCount();
81 random_add_noise(&wintime, sizeof(DWORD));
82
83 if (QueryPerformanceCounter(&perftime))
84 random_add_noise(&perftime, sizeof(perftime));
85 }