2 * Noise generation for PuTTY's cryptographic random number
10 #include <sys/resource.h>
16 static int read_dev_urandom(char *buf, int len)
21 fd = open("/dev/urandom", O_RDONLY);
27 ret = read(fd, buf+ngot, len-ngot);
39 * This function is called once, at PuTTY startup. It will do some
40 * slightly silly things such as fetching an entire process listing
41 * and scanning /tmp, load the saved random seed from disk, and
42 * also read 32 bytes out of /dev/urandom.
45 void noise_get_heavy(void (*func) (void *, int))
51 if (read_dev_urandom(buf, 32))
54 fp = popen("ps -axu 2>/dev/null", "r");
55 while ( (ret = fread(buf, 1, sizeof(buf), fp)) > 0)
59 fp = popen("ls -al /tmp 2>/dev/null", "r");
60 while ( (ret = fread(buf, 1, sizeof(buf), fp)) > 0)
64 read_random_seed(func);
68 void random_save_seed(void)
74 random_get_savedata(&data, &len);
75 write_random_seed(data, len);
81 * This function is called every time the random pool needs
82 * stirring, and will acquire the system time.
84 void noise_get_light(void (*func) (void *, int))
87 gettimeofday(&tv, NULL);
88 func(&tv, sizeof(tv));
92 * This function is called on a timer, and grabs as much changeable
93 * system data as it can quickly get its hands on.
95 void noise_regular(void)
100 struct rusage rusage;
102 if ((fd = open("/proc/meminfo", O_RDONLY)) >= 0) {
103 while ( (ret = read(fd, buf, sizeof(buf))) > 0)
104 random_add_noise(buf, ret);
107 if ((fd = open("/proc/stat", O_RDONLY)) >= 0) {
108 while ( (ret = read(fd, buf, sizeof(buf))) > 0)
109 random_add_noise(buf, ret);
112 getrusage(RUSAGE_SELF, &rusage);
113 random_add_noise(&rusage, sizeof(rusage));
117 * This function is called on every keypress or mouse move, and
118 * will add the current time to the noise pool. It gets the scan
119 * code or mouse position passed in, and adds that too.
121 void noise_ultralight(unsigned long data)
124 gettimeofday(&tv, NULL);
125 random_add_noise(&tv, sizeof(tv));
126 random_add_noise(&data, sizeof(data));