| 1 | /* |
| 2 | * Noise generation for PuTTY's cryptographic random number |
| 3 | * generator. |
| 4 | */ |
| 5 | |
| 6 | #include <Processes.h> |
| 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 | |
| 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 | |
| 36 | void noise_get_heavy(void (*func) (void *, int)) |
| 37 | { |
| 38 | |
| 39 | noise_get_light(func); |
| 40 | noise_get_processes(func); |
| 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 | /* |
| 71 | * This function is called on every keypress or mouse move, and |
| 72 | * will add the current time to the noise pool. It gets the scan |
| 73 | * code or mouse position passed in, and adds that too. |
| 74 | */ |
| 75 | void noise_ultralight(unsigned long data) |
| 76 | { |
| 77 | UnsignedWide utc; |
| 78 | |
| 79 | Microseconds(&utc); |
| 80 | random_add_noise(&utc, sizeof(utc)); |
| 81 | random_add_noise(&data, sizeof(data)); |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Local Variables: |
| 86 | * c-file-style: "simon" |
| 87 | * End: |
| 88 | */ |