diff options
author | Paul R. Eggert <eggert@cs.ucla.edu> | 2010-07-20 09:50:37 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2010-07-20 09:51:12 -0700 |
commit | c1d8e6e458c2f9deb3013538d6db8da8f7f4e9f1 (patch) | |
tree | 8b541824476fa9f641b11ab89d89a3ebf3743aac /gl/lib/randread.c | |
parent | e5444fa2a7a2ad4246e7a5e961d5c4aad2aecbe9 (diff) | |
download | coreutils-c1d8e6e458c2f9deb3013538d6db8da8f7f4e9f1.tar.xz |
randread: use /dev/urandom to seed, not just getpid etc
* gl/lib/rand-isaac.c (isaac_seed_start): New arg SEEDED.
(isaac_seed): New args FD and BYTES_BOUND. Read from FD if possible.
Don't bother with low-quality sources if FD has enough bytes.
* gl/lib/rand-isaac.h: New size_t arg for isaac_seed.
* gl/lib/randread.c: Include fcntl.h, unistd.h.
(NAME_OF_NONCE_DEVICE): New #define.
(nonce_device): New static var.
(randread_new): Use nonce device if available.
Diffstat (limited to 'gl/lib/randread.c')
-rw-r--r-- | gl/lib/randread.c | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/gl/lib/randread.c b/gl/lib/randread.c index 94b9928b2..a681c8dae 100644 --- a/gl/lib/randread.c +++ b/gl/lib/randread.c @@ -24,12 +24,14 @@ #include <errno.h> #include <error.h> #include <exitfail.h> +#include <fcntl.h> #include <quotearg.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include "gettext.h" #define _(msgid) gettext (msgid) @@ -60,6 +62,10 @@ # define ALIGNED_POINTER(ptr, type) ((size_t) (ptr) % alignof (type) == 0) #endif +#ifndef NAME_OF_NONCE_DEVICE +#define NAME_OF_NONCE_DEVICE "/dev/urandom" +#endif + /* The maximum buffer size used for reads of random data. Using the value 2 * ISAAC_BYTES makes this the largest power of two that would not otherwise cause struct randread_source to grow. */ @@ -164,8 +170,11 @@ randread_new (char const *name, size_t bytes_bound) setvbuf (source, s->buf.c, _IOFBF, MIN (sizeof s->buf.c, bytes_bound)); else { + int nonce_device = open (NAME_OF_NONCE_DEVICE, O_RDONLY | O_BINARY); s->buf.isaac.buffered = 0; - isaac_seed (&s->buf.isaac.state); + isaac_seed (&s->buf.isaac.state, nonce_device, bytes_bound); + if (0 <= nonce_device) + close (nonce_device); } return s; |