From 025599ec999dd8454f2a8fdd11c08329ac608571 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 3 Jun 2018 14:30:28 +0100 Subject: [PATCH] Unix PuTTYgen: switch to /dev/urandom by default. The general wisdom these days - in particular as given by the Linux urandom(4) man page - seems to be that there's no need to use the blocking /dev/random any more unless you're running at very early boot time when the system random pool is at serious risk of not having any entropy in it at all. In case of non-Linux systems that don't think /dev/urandom is a standard name, I fall back to /dev/random if /dev/urandom can't be found. --- unix/uxgen.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/unix/uxgen.c b/unix/uxgen.c index f593a960..ed14188d 100644 --- a/unix/uxgen.c +++ b/unix/uxgen.c @@ -16,8 +16,26 @@ char *get_random_data(int len, const char *device) int fd; int ngot, ret; - if (!device) - device = "/dev/random"; + if (!device) { + static const char *const default_devices[] = { + "/dev/urandom", "/dev/random" + }; + size_t i; + + for (i = 0; i < lenof(default_devices); i++) { + if (access(default_devices[i], R_OK) == 0) { + device = default_devices[i]; + break; + } + } + + if (!device) { + sfree(buf); + fprintf(stderr, "puttygen: cannot find a readable " + "random number source; use --random-device\n"); + return NULL; + } + } fd = open(device, O_RDONLY); if (fd < 0) {