mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-10 09:58:01 +00:00
320bf8479f
This tears out the entire previous random-pool system in sshrand.c. In its place is a system pretty close to Ferguson and Schneier's 'Fortuna' generator, with the main difference being that I use SHA-256 instead of AES for the generation side of the system (rationale given in comment). The PRNG implementation lives in sshprng.c, and defines a self- contained data type with no state stored outside the object, so you can instantiate however many of them you like. The old sshrand.c still exists, but in place of the previous random pool system, it's just become a client of sshprng.c, whose job is to hold a single global instance of the PRNG type, and manage its reference count, save file, noise-collection timers and similar administrative business. Advantages of this change include: - Fortuna is designed with a more varied threat model in mind than my old home-grown random pool. For example, after any request for random numbers, it automatically re-seeds itself, so that if the state of the PRNG should be leaked, it won't give enough information to find out what past outputs _were_. - The PRNG type can be instantiated with any hash function; the instance used by the main tools is based on SHA-256, an improvement on the old pool's use of SHA-1. - The new PRNG only uses the completely standard interface to the hash function API, instead of having to have privileged access to the internal SHA-1 block transform function. This will make it easier to revamp the hash code in general, and also it means that hardware-accelerated versions of SHA-256 will automatically be used for the PRNG as well as for everything else. - The new PRNG can be _tested_! Because it has an actual (if not quite explicit) specification for exactly what the output numbers _ought_ to be derived from the hashes of, I can (and have) put tests in cryptsuite that ensure the output really is being derived in the way I think it is. The old pool could have been returning any old nonsense and it would have been very hard to tell for sure.
143 lines
3.1 KiB
C
143 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);
|
|
}
|
|
|
|
void random_save_seed(void)
|
|
{
|
|
int len;
|
|
void *data;
|
|
|
|
if (random_active) {
|
|
random_get_savedata(&data, &len);
|
|
write_random_seed(data, len);
|
|
sfree(data);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* 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;
|
|
}
|