1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00
putty-source/unix/uxnoise.c
Simon Tatham 5d718ef64b Whitespace rationalisation of entire code base.
The number of people has been steadily increasing who read our source
code with an editor that thinks tab stops are 4 spaces apart, as
opposed to the traditional tty-derived 8 that the PuTTY code expects.

So I've been wondering for ages about just fixing it, and switching to
a spaces-only policy throughout the code. And I recently found out
about 'git blame -w', which should make this change not too disruptive
for the purposes of source-control archaeology; so perhaps now is the
time.

While I'm at it, I've also taken the opportunity to remove all the
trailing spaces from source lines (on the basis that git dislikes
them, and is the only thing that seems to have a strong opinion one
way or the other).
    
Apologies to anyone downstream of this code who has complicated patch
sets to rebase past this change. I don't intend it to be needed again.
2019-09-08 20:29:21 +01:00

131 lines
3.1 KiB
C

/*
* Noise generation for PuTTY's cryptographic random number
* generator.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "putty.h"
#include "ssh.h"
#include "storage.h"
static bool read_dev_urandom(char *buf, int len)
{
int fd;
int ngot, ret;
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
return false;
ngot = 0;
while (ngot < len) {
ret = read(fd, buf+ngot, len-ngot);
if (ret < 0) {
close(fd);
return false;
}
ngot += ret;
}
close(fd);
return true;
}
/*
* This function is called once, at PuTTY startup. It will do some
* slightly silly things such as fetching an entire process listing
* and scanning /tmp, load the saved random seed from disk, and
* also read 32 bytes out of /dev/urandom.
*/
void noise_get_heavy(void (*func) (void *, int))
{
char buf[512];
FILE *fp;
int ret;
bool got_dev_urandom = false;
if (read_dev_urandom(buf, 32)) {
got_dev_urandom = true;
func(buf, 32);
}
fp = popen("ps -axu 2>/dev/null", "r");
if (fp) {
while ( (ret = fread(buf, 1, sizeof(buf), fp)) > 0)
func(buf, ret);
pclose(fp);
} else if (!got_dev_urandom) {
fprintf(stderr, "popen: %s\n"
"Unable to access fallback entropy source\n", strerror(errno));
exit(1);
}
fp = popen("ls -al /tmp 2>/dev/null", "r");
if (fp) {
while ( (ret = fread(buf, 1, sizeof(buf), fp)) > 0)
func(buf, ret);
pclose(fp);
} else if (!got_dev_urandom) {
fprintf(stderr, "popen: %s\n"
"Unable to access fallback entropy source\n", strerror(errno));
exit(1);
}
read_random_seed(func);
}
/*
* This function is called on a timer, and grabs as much changeable
* system data as it can quickly get its hands on.
*/
void noise_regular(void)
{
int fd;
int ret;
char buf[512];
struct rusage rusage;
if ((fd = open("/proc/meminfo", O_RDONLY)) >= 0) {
while ( (ret = read(fd, buf, sizeof(buf))) > 0)
random_add_noise(NOISE_SOURCE_MEMINFO, buf, ret);
close(fd);
}
if ((fd = open("/proc/stat", O_RDONLY)) >= 0) {
while ( (ret = read(fd, buf, sizeof(buf))) > 0)
random_add_noise(NOISE_SOURCE_STAT, buf, ret);
close(fd);
}
getrusage(RUSAGE_SELF, &rusage);
random_add_noise(NOISE_SOURCE_RUSAGE, &rusage, sizeof(rusage));
}
/*
* This function is called on every keypress or mouse move, and
* will add the current time to the noise pool. It gets the scan
* code or mouse position passed in, and adds that too.
*/
void noise_ultralight(NoiseSourceId id, unsigned long data)
{
struct timeval tv;
gettimeofday(&tv, NULL);
random_add_noise(NOISE_SOURCE_TIME, &tv, sizeof(tv));
random_add_noise(id, &data, sizeof(data));
}
uint64_t prng_reseed_time_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}