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