diff options
Diffstat (limited to 'cipher/random.c')
-rw-r--r-- | cipher/random.c | 57 |
1 files changed, 44 insertions, 13 deletions
diff --git a/cipher/random.c b/cipher/random.c index e173a5279..32415bd0b 100644 --- a/cipher/random.c +++ b/cipher/random.c @@ -32,6 +32,18 @@ #include <assert.h> #include <errno.h> #include <string.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/stat.h> +#ifdef HAVE_GETHRTIME + #include <sys/times.h> +#endif +#ifdef HAVE_GETTIMEOFDAY + #include <sys/times.h> +#endif +#ifdef HAVE_GETRUSAGE + #include <sys/resource.h> +#endif #include "util.h" #include "rmd.h" #include "ttyio.h" @@ -83,9 +95,7 @@ static void read_pool( byte *buffer, size_t length, int level ); static void add_randomness( const void *buffer, size_t length, int source ); static void random_poll(void); static void read_random_source( byte *buffer, size_t length, int level ); -#ifndef HAVE_DEV_RANDOM static int gather_faked( byte *buffer, size_t *r_length, int level ); -#endif static void @@ -329,11 +339,39 @@ fast_random_poll() initialize(); initialized = 1; fnc = dynload_getfnc_fast_random_poll(); - if( !fnc ) - log_info("Ooops: No fast random poll function\n"); } - if( fnc ) + if( fnc ) { (*fnc)( add_randomness ); + return; + } + + /* fall back to the generic function */ + #if HAVE_GETHRTIME + { hrtime_t tv; + tv = gethrtime(); + add_randomness( &tv, sizeof(tv), 1 ); + } + #elif HAVE_GETTIMEOFDAY + { struct timeval tv; + if( gettimeofday( &tv, NULL ) ) + BUG(); + add_randomness( &tv.tv_sec, sizeof(tv.tv_sec), 1 ); + add_randomness( &tv.tv_usec, sizeof(tv.tv_usec), 1 ); + } + #else /* use times */ + { struct tms buf; + times( &buf ); + add_randomness( &buf, sizeof buf, 1 ); + } + #endif + #ifdef HAVE_GETRUSAGE + { struct rusage buf; + if( getrusage( RUSAGE_SELF, &buf ) ) + BUG(); + add_randomness( &buf, sizeof buf, 1 ); + memset( &buf, 0, sizeof buf ); + } + #endif } @@ -351,11 +389,7 @@ read_random_source( byte *buffer, size_t length, int level ) fnc = dynload_getfnc_gather_random(); if( !fnc ) { faked_rng = 1; - #ifndef HAVE_DEV_RANDOM fnc = gather_faked; - #else - BUG(); - #endif } } while( length ) { @@ -368,7 +402,6 @@ read_random_source( byte *buffer, size_t length, int level ) } -#ifndef HAVE_DEV_RANDOM static int gather_faked( byte *buffer, size_t *r_length, int level ) { @@ -378,7 +411,7 @@ gather_faked( byte *buffer, size_t *r_length, int level ) if( !initialized ) { log_info(_("WARNING: using insecure random number generator!!\n")); tty_printf(_("The random number generator is only a kludge to let\n" - "it compile - it is in no way a strong RNG!\n\n" + "it run - it is in no way a strong RNG!\n\n" "DON'T USE ANY DATA GENERATED BY THIS PROGRAM!!\n\n")); initialized=1; #ifdef HAVE_RAND @@ -398,5 +431,3 @@ gather_faked( byte *buffer, size_t *r_length, int level ) return 100; /* We really fake it ;-) */ } -#endif /* ! HAVE_DEV_RANDOM */ - |