2 * Noise generation for PuTTY's cryptographic random number
\r
12 #include <sys/time.h>
\r
13 #include <sys/resource.h>
\r
17 #include "storage.h"
\r
19 static int read_dev_urandom(char *buf, int len)
\r
24 fd = open("/dev/urandom", O_RDONLY);
\r
29 while (ngot < len) {
\r
30 ret = read(fd, buf+ngot, len-ngot);
\r
44 * This function is called once, at PuTTY startup. It will do some
\r
45 * slightly silly things such as fetching an entire process listing
\r
46 * and scanning /tmp, load the saved random seed from disk, and
\r
47 * also read 32 bytes out of /dev/urandom.
\r
50 void noise_get_heavy(void (*func) (void *, int))
\r
55 int got_dev_urandom = 0;
\r
57 if (read_dev_urandom(buf, 32)) {
\r
58 got_dev_urandom = 1;
\r
62 fp = popen("ps -axu 2>/dev/null", "r");
\r
64 while ( (ret = fread(buf, 1, sizeof(buf), fp)) > 0)
\r
67 } else if (!got_dev_urandom) {
\r
68 fprintf(stderr, "popen: %s\n"
\r
69 "Unable to access fallback entropy source\n", strerror(errno));
\r
73 fp = popen("ls -al /tmp 2>/dev/null", "r");
\r
75 while ( (ret = fread(buf, 1, sizeof(buf), fp)) > 0)
\r
78 } else if (!got_dev_urandom) {
\r
79 fprintf(stderr, "popen: %s\n"
\r
80 "Unable to access fallback entropy source\n", strerror(errno));
\r
84 read_random_seed(func);
\r
88 void random_save_seed(void)
\r
93 if (random_active) {
\r
94 random_get_savedata(&data, &len);
\r
95 write_random_seed(data, len);
\r
101 * This function is called every time the random pool needs
\r
102 * stirring, and will acquire the system time.
\r
104 void noise_get_light(void (*func) (void *, int))
\r
107 gettimeofday(&tv, NULL);
\r
108 func(&tv, sizeof(tv));
\r
112 * This function is called on a timer, and grabs as much changeable
\r
113 * system data as it can quickly get its hands on.
\r
115 void noise_regular(void)
\r
120 struct rusage rusage;
\r
122 if ((fd = open("/proc/meminfo", O_RDONLY)) >= 0) {
\r
123 while ( (ret = read(fd, buf, sizeof(buf))) > 0)
\r
124 random_add_noise(buf, ret);
\r
127 if ((fd = open("/proc/stat", O_RDONLY)) >= 0) {
\r
128 while ( (ret = read(fd, buf, sizeof(buf))) > 0)
\r
129 random_add_noise(buf, ret);
\r
132 getrusage(RUSAGE_SELF, &rusage);
\r
133 random_add_noise(&rusage, sizeof(rusage));
\r
137 * This function is called on every keypress or mouse move, and
\r
138 * will add the current time to the noise pool. It gets the scan
\r
139 * code or mouse position passed in, and adds that too.
\r
141 void noise_ultralight(unsigned long data)
\r
144 gettimeofday(&tv, NULL);
\r
145 random_add_noise(&tv, sizeof(tv));
\r
146 random_add_noise(&data, sizeof(data));
\r