/* rand-dummy.c - INSECURE dummy random device * Copyright (C) 1998 Free Software Foundation, Inc. * * This file is part of GNUPG. * * GNUPG is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * GNUPG is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include #include #include #include #include #include #include #include #ifdef HAVE_GETHRTIME #include #endif #ifdef HAVE_GETTIMEOFDAY #include #endif #ifdef HAVE_GETRUSAGE #include #endif #include #include #ifdef __MINGW32__ #include #endif #include #include "util.h" #include "ttyio.h" #include "i18n.h" #include "rand-internal.h" #ifdef USE_RAND_DUMMY /* a dummy random file so we can do some tests */ #ifndef RAND_MAX /* for SunOS */ #define RAND_MAX 32767 #endif #if __GNUC__ #warning Using the insecure dummy random device #endif void random_poll() { char buf[POOLSIZE/5]; read_random_source( buf, POOLSIZE/5, 1 ); /* read dummy data */ add_randomness( buf, POOLSIZE/5, 2); memset( buf, 0, POOLSIZE/5); } void fast_random_poll() { #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 */ { #ifndef __MINGW32__ struct tms buf; times( &buf ); add_randomness( &buf, sizeof buf, 1 ); #endif } #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 } void read_random_source( byte *buffer, size_t length, int level ) { static int initialized=0; 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" "DON'T USE ANY DATA GENERATED BY THIS PROGRAM!!\n\n")); initialized=1; #ifdef HAVE_RAND srand(make_timestamp()*getpid()); #else srandom(make_timestamp()*getpid()); #endif } #ifdef HAVE_RAND while( length-- ) *buffer++ = ((unsigned)(1 + (int) (256.0*rand()/(RAND_MAX+1.0)))-1); #else while( length-- ) *buffer++ = ((unsigned)(1 + (int) (256.0*random()/(RAND_MAX+1.0)))-1); #endif } #endif /* USE_RAND_DUMMY */