diff options
author | Pawel Krawczyk <pawel.krawczyk@hush.com> | 2014-11-26 12:03:20 +0000 |
---|---|---|
committer | Pawel Krawczyk <pawel.krawczyk@hush.com> | 2014-11-26 12:03:20 +0000 |
commit | dd76c5b7ec14656f6839dd2a38f1e96ff21b9819 (patch) | |
tree | 50d82bf0d15cd7d1e2b69c504cdc2f11e1adaeda | |
parent | cedd99f3c6f690bf4474bf3c6ce5f49ffc3a94ab (diff) | |
download | pam_tacplus-dd76c5b7ec14656f6839dd2a38f1e96ff21b9819.tar.gz pam_tacplus-dd76c5b7ec14656f6839dd2a38f1e96ff21b9819.zip |
rewrite the PRNG logic to use urandom on srand() only and then return
random() on subsequent calls
-rw-r--r-- | libtac/lib/magic.c | 43 |
1 files changed, 15 insertions, 28 deletions
diff --git a/libtac/lib/magic.c b/libtac/lib/magic.c index 9785314..faf70f0 100644 --- a/libtac/lib/magic.c +++ b/libtac/lib/magic.c @@ -28,8 +28,7 @@ #include "magic.h" -static int rfd = -1; /* fd for /dev/urandom */ -static int magic_inited = 0; +static int magic_initialised = 0; /* * magic_init - Initialize the magic number generator. @@ -45,26 +44,24 @@ magic_init() long seed; struct timeval t; - if (magic_inited) + if (magic_initialised) return; - magic_inited = 1; - - /* - try using /dev/urandom - also check that it's a character device - If it doesn't exist, fallback to other method - */ - + // try to initialise seed from urandom if (!lstat("/dev/urandom", &statbuf) && S_ISCHR(statbuf.st_mode)) { - rfd = open("/dev/urandom", O_RDONLY); - if (rfd >= 0) - return; - } + int rfd = open("/dev/urandom", O_RDONLY); + if(rfd > 0) { + int nb_read = read(rfd, &seed, sizeof(seed)); + } + } + // add the deterministic data in case urandom failed gettimeofday(&t, NULL); - seed = gethostid() ^ t.tv_sec ^ t.tv_usec ^ getpid(); + seed ^= gethostid() ^ t.tv_sec ^ t.tv_usec ^ getpid(); + + // finally seed the PRNG srandom(seed); + magic_initialised = 1; } /* @@ -73,19 +70,9 @@ magic_init() u_int32_t magic() { - magic_init(); - - if(rfd > -1) { - u_int32_t ret; - int nb_read = read(rfd, &ret, sizeof(ret)); - close(rfd); + if(!magic_initialised) + magic_init(); - if (nb_read < sizeof(ret)) { - /* on read() error fallback to other method */ - return (u_int32_t)random(); - } - return ret; - } return (u_int32_t)random(); } |