1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 18:07:59 +00:00
putty-source/unix/uxgen.c
Simon Tatham 435b29da88 cmdgen: option to specify the random number device to use.
E.g. you might pass '--random-device=/dev/urandom'.

Mostly because I got sick of waiting for /dev/random to finish
blocking while I was trying to generate throwaway keys for testing bug
fixes in cmdgen itself. But it might also be useful on systems that
call their random device by a different name that we haven't
encountered.

(Since cmdgen also reads the saved PuTTY random seed file, setting
this option to /dev/zero will not render key generation deterministic.
It's tempting to provide _some_ way to do that, for testing purposes
and clearly marked as dangerous of course, but I think it would take
more faff than this.)
2016-03-30 08:30:40 +01:00

47 lines
826 B
C

/*
* uxgen.c: Unix implementation of get_heavy_noise() from cmdgen.c.
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "putty.h"
char *get_random_data(int len, const char *device)
{
char *buf = snewn(len, char);
int fd;
int ngot, ret;
if (!device)
device = "/dev/random";
fd = open(device, O_RDONLY);
if (fd < 0) {
sfree(buf);
fprintf(stderr, "puttygen: %s: open: %s\n",
device, strerror(errno));
return NULL;
}
ngot = 0;
while (ngot < len) {
ret = read(fd, buf+ngot, len-ngot);
if (ret < 0) {
close(fd);
sfree(buf);
fprintf(stderr, "puttygen: %s: read: %s\n",
device, strerror(errno));
return NULL;
}
ngot += ret;
}
close(fd);
return buf;
}