d4af3006 |
1 | /* |
2 | * Noise generation for PuTTY's cryptographic random number |
3 | * generator. |
4 | */ |
5 | |
62cd4f18 |
6 | #include <Processes.h> |
d4af3006 |
7 | #include <Types.h> |
8 | #include <Timer.h> |
9 | |
10 | #include "putty.h" |
11 | #include "ssh.h" |
12 | #include "storage.h" |
13 | |
14 | /* |
15 | * This function is called once, at PuTTY startup, and will do some |
16 | * seriously silly things like listing directories and getting disk |
17 | * free space and a process snapshot. |
18 | */ |
19 | |
62cd4f18 |
20 | static void noise_get_processes(void (*func) (void *, int)) |
21 | { |
22 | ProcessSerialNumber psn = {0, kNoProcess}; |
23 | ProcessInfoRec info; |
24 | |
25 | for (;;) { |
26 | GetNextProcess(&psn); |
27 | if (psn.highLongOfPSN == 0 && psn.lowLongOfPSN == kNoProcess) return; |
28 | info.processInfoLength = sizeof(info); |
29 | info.processName = NULL; |
30 | info.processAppSpec = NULL; |
31 | GetProcessInformation(&psn, &info); |
32 | func(&info, sizeof(info)); |
33 | } |
34 | } |
35 | |
d4af3006 |
36 | void noise_get_heavy(void (*func) (void *, int)) |
37 | { |
38 | |
62cd4f18 |
39 | noise_get_light(func); |
40 | noise_get_processes(func); |
d4af3006 |
41 | read_random_seed(func); |
42 | /* Update the seed immediately, in case another instance uses it. */ |
43 | random_save_seed(); |
44 | } |
45 | |
46 | void random_save_seed(void) |
47 | { |
48 | int len; |
49 | void *data; |
50 | |
51 | if (random_active) { |
52 | random_get_savedata(&data, &len); |
53 | write_random_seed(data, len); |
54 | sfree(data); |
55 | } |
56 | } |
57 | |
58 | /* |
59 | * This function is called every time the random pool needs |
60 | * stirring, and will acquire the system time. |
61 | */ |
62 | void noise_get_light(void (*func) (void *, int)) |
63 | { |
64 | UnsignedWide utc; |
65 | |
66 | Microseconds(&utc); |
67 | func(&utc, sizeof(utc)); |
68 | } |
69 | |
70 | /* |
e479b152 |
71 | * This function is called on a timer, and grabs as much changeable |
72 | * system data as it can quickly get its hands on. |
73 | */ |
74 | void noise_regular(void) |
75 | { |
76 | /* XXX */ |
77 | } |
78 | |
79 | /* |
d4af3006 |
80 | * This function is called on every keypress or mouse move, and |
81 | * will add the current time to the noise pool. It gets the scan |
82 | * code or mouse position passed in, and adds that too. |
83 | */ |
84 | void noise_ultralight(unsigned long data) |
85 | { |
86 | UnsignedWide utc; |
87 | |
88 | Microseconds(&utc); |
89 | random_add_noise(&utc, sizeof(utc)); |
90 | random_add_noise(&data, sizeof(data)); |
91 | } |
92 | |
93 | /* |
94 | * Local Variables: |
95 | * c-file-style: "simon" |
96 | * End: |
97 | */ |