Rationalised host key storage. Also started code reorg: persistent-state
[u/mdw/putty] / noise.c
CommitLineData
374330e2 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"
d5859615 11#include "storage.h"
374330e2 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
19void noise_get_heavy(void (*func) (void *, int)) {
20 HANDLE srch;
374330e2 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
d5859615 34 read_random_seed(func);
374330e2 35}
36
37void random_save_seed(void) {
d5859615 38 int len;
39 void *data;
374330e2 40
d5859615 41 random_get_savedata(&data, &len);
42 write_random_seed(data, len);
de3df031 43}
44
45/*
374330e2 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 */
50void 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
676c0556 62#ifndef WIN32S_COMPAT
374330e2 63 if (GetSystemPowerStatus(&pwrstat))
64 func(&pwrstat, sizeof(pwrstat));
676c0556 65#endif
374330e2 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 */
74void 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}