diff options
Diffstat (limited to 'cipher/rndw32.c')
-rw-r--r-- | cipher/rndw32.c | 231 |
1 files changed, 13 insertions, 218 deletions
diff --git a/cipher/rndw32.c b/cipher/rndw32.c index 21d0af6a..3db42e46 100644 --- a/cipher/rndw32.c +++ b/cipher/rndw32.c @@ -57,220 +57,11 @@ #include "types.h" #include "g10lib.h" +#include "rand-internal.h" -/* We do not use the netropy DLL anymore because a standalone program is - * easier to maintain and */ -/*#define USE_ENTROPY_DLL*/ static int debug_me; -#ifdef USE_ENTROPY_DLL - -#define WIN32_SLOW_SEEDER 0 -#define WIN32_FAST_SEEDER 1 - -#define PCP_SUCCESS 0 -#define PCP_NULL_POINTER 1 -#define PCP_SEEDER_FAILED 2 -#define PCP_SEEDER_NO_MEM 3 -#define PCP_SEEDER_TOO_SMALL 4 -#define PCP_DLL_LOAD_FAILED 5 -#define PCP_UNKNOWN_PLATFORM 6 -#define PCP_ERROR_VERSION 7 -#define PCP_DLL_FUNC 8 -#define PCP_UNKNOWN_SEEDER_TYPE 9 - - -/**************** - * We sometimes get a SEEDER_TOO_SMALL error, in which case we increment - * the internal buffer by SEEDER_INC_CHUNK until we reach MAX_SEEDER_SIZE - * MAX_SEEDER_SIZE is used as an arbitrary limit to protect against - * bugs in Winseed. - */ -#define MAX_SEEDER_SIZE 500000 -#define SEEDER_INC_CHUNK 50000 - - -typedef void *WIN32_SEEDER; - -static WIN32_SEEDER (WINAPI *create_instance)( byte type, unsigned int *reason); -static void (WINAPI *delete_instance)( WIN32_SEEDER that ); -static unsigned int (WINAPI *get_internal_seed_size)( WIN32_SEEDER that ); -static void (WINAPI *set_internal_seed_size)( WIN32_SEEDER that, - unsigned int new_size); -static unsigned int (WINAPI *get_expected_seed_size)( WIN32_SEEDER that); -static unsigned int (WINAPI *get_seed)( WIN32_SEEDER that, byte *buffer, - unsigned int *desired_length); - -static WIN32_SEEDER slow_seeder, fast_seeder; -static byte *entropy_buffer; -static size_t entropy_buffer_size; - -/**************** - * Load and initialize the winseed DLL - * NOTE: winseed is not part of the GnuPG distribution. It should be available - * at the GNU crypto FTP server site. - * We do not load the DLL on demand to have a better control over the - * location of the library. - */ -static void -load_and_init_winseed( void ) -{ - HANDLE hInstance; - void *addr; - unsigned int reason = 0; - unsigned int n1, n2; - const char *dllname; - - dllname = read_w32_registry_string( "HKEY_LOCAL_MACHINE", - "Software\\GNU\\GnuPG", - "EntropyDLL" ); - if( !dllname ) - dllname = "c:/gnupg/entropy.dll"; - - hInstance = LoadLibrary( dllname ); - if( !hInstance ) - goto failure; - if( !(addr = GetProcAddress( hInstance, "WS_create_instance" )) ) - goto failure; - create_instance = addr; - if( !(addr = GetProcAddress( hInstance, "WS_delete_instance" )) ) - goto failure; - delete_instance = addr; - if( !(addr = GetProcAddress( hInstance, "WS_get_internal_seed_size" )) ) - goto failure; - get_internal_seed_size = addr; - if( !(addr = GetProcAddress( hInstance, "WS_set_internal_seed_size" )) ) - goto failure; - set_internal_seed_size = addr; - if( !(addr = GetProcAddress( hInstance, "WS_get_expected_seed_size" )) ) - goto failure; - get_expected_seed_size = addr; - if( !(addr = GetProcAddress( hInstance, "WS_get_seed" )) ) - goto failure; - get_seed = addr; - - /* we have all the functions - init the system */ - slow_seeder = create_instance( WIN32_SLOW_SEEDER, &reason); - if( !slow_seeder ) { - log_fatal("error creating winseed slow seeder: rc=%u\n", reason ); - goto failure; - } - fast_seeder = create_instance( WIN32_FAST_SEEDER, &reason); - if( !fast_seeder ) { - log_fatal("error creating winseed fast seeder: rc=%u\n", reason ); - goto failure; - } - n1 = get_internal_seed_size( slow_seeder ); - /*log_info("slow buffer size=%u\n", n1);*/ - n2 = get_internal_seed_size( fast_seeder ); - /*log_info("fast buffer size=%u\n", n2);*/ - - entropy_buffer_size = n1 > n2? n1: n2; - entropy_buffer = gcry_xmalloc( entropy_buffer_size ); - /*log_info("using a buffer of size=%u\n", entropy_buffer_size );*/ - - return; - - failure: - log_fatal("error loading winseed DLL `%s'\n", dllname ); -} - - - - - -/* Note: we always use the highest level. - * TO boost the performance we may want to add some - * additional code for level 1 - */ -int -rndw32_gather_random( void (*add)(const void*, size_t, int), int requester, - size_t length, int level ) -{ - unsigned int result; - unsigned int nbytes; - - if( !level ) - return 0; - - if( !slow_seeder ) - load_and_init_winseed(); - - /* Our estimation on how much entropy we should use is very vague. - * Winseed delivers some amount of entropy on each slow poll and - * we add it to our random pool. Depending on the required quality - * level we adjust the requested length so that for higher quality - * we make sure to add more entropy to our pool. However, as we don't - * like to waste any entropy collected by winseed, we always add - * at least everything we got from winseed. - */ - if( level > 1 ) - length *= 100; - else if( level > 0 ) - length *= 10; - - for(;;) { - nbytes = entropy_buffer_size; - result = get_seed( slow_seeder, entropy_buffer, &nbytes); - if( result == PCP_SEEDER_TOO_SMALL ) { - unsigned int n1 = get_internal_seed_size( slow_seeder ); - - if( n1 > MAX_SEEDER_SIZE ) { - log_fatal("rndw32: internal seeder problem (size=%u)\n", - n1); - return -1; /* actually never reached */ - } - n1 += SEEDER_INC_CHUNK; - set_internal_seed_size( slow_seeder, n1 ); - if( n1 > entropy_buffer_size ) { - entropy_buffer_size = n1; - entropy_buffer = gcry_realloc( entropy_buffer, - entropy_buffer_size ); - } - continue; - } - - - if( result ) { - log_fatal("rndw32: get_seed(slow) failed: rc=%u\n", result); - return -1; /* actually never reached */ - } - /*log_info("rndw32: slow poll level %d, need %u, got %u\n", - level, (unsigned int)length, (unsigned int)nbytes );*/ - (*add)( entropy_buffer, nbytes, requester ); - if( length <= nbytes ) - return 0; /* okay */ - length -= nbytes; - } -} - -int -rndw32_gather_random_fast( void (*add)(const void*, size_t, int), int requester ) -{ - unsigned int result; - unsigned int nbytes; - - if( !fast_seeder ) - load_and_init_winseed(); - - /* winseed delivers a constant ammount of entropy for a fast - * poll. We can simply use this and add it to the pool; no need - * a loop like it is used in the slow poll */ - nbytes = entropy_buffer_size; - result = get_seed( fast_seeder, entropy_buffer, &nbytes); - if( result ) { - log_fatal("rndw32: get_seed(fast) failed: rc=%u\n", result); - return -1; /* actually never reached */ - } - /*log_info("rndw32: fast poll got %u\n", (unsigned int)nbytes );*/ - (*add)( entropy_buffer, nbytes, requester ); - return 0; -} - -#else /* !USE_ENTROPY_DLL */ -/* This is the new code which does not require the entropy.dll */ - /* * Definitions which are missing from the current GNU Windows32Api */ @@ -291,6 +82,10 @@ rndw32_gather_random_fast( void (*add)(const void*, size_t, int), int requester #define VER_PLATFORM_WIN32_WINDOWS 1 #endif +/* This used to be (6*8+5*4+8*2), but Peter Gutmann figured a larger + value in a newer release. So we use a far larger value. */ +#define SIZEOF_DISK_PERFORMANCE_STRUCT 256 + typedef struct { DWORD dwSize; @@ -431,7 +226,7 @@ slow_gatherer_windows95( void (*add)(const void*, size_t, int), int requester ) || !pThread32First || !pThread32Next || !pHeap32ListFirst || !pHeap32ListNext || !pHeap32First || !pHeap32Next ) { - log_fatal ( "rndw32: failed to get a toolhep function\n" ); + log_fatal ( "rndw32: failed to get a toolhelp function\n" ); } } @@ -701,9 +496,10 @@ slow_gatherer_windowsNT( void (*add)(const void*, size_t, int), int requester ) } -static int -gather_random( void (*add)(const void*, size_t, int), int requester, - size_t length, int level ) +int +_gcry_rndw32_gather_random( void (*add)(const void*, size_t, int), + int requester, + size_t length, int level ) { static int is_initialized; static int is_windowsNT, has_toolhelp; @@ -752,8 +548,9 @@ gather_random( void (*add)(const void*, size_t, int), int requester, } -static int -gather_random_fast( void (*add)(const void*, size_t, int), int requester ) +int +_gcry_rndw32_gather_random_fast (void (*add)(const void*, size_t, int), + int requester ) { static int addedFixedItems = 0; @@ -883,5 +680,3 @@ gather_random_fast( void (*add)(const void*, size_t, int), int requester ) return 0; } - -#endif /* !USE_ENTROPY_DLL */ |