diff options
Diffstat (limited to 'cipher/random.c')
-rw-r--r-- | cipher/random.c | 65 |
1 files changed, 39 insertions, 26 deletions
diff --git a/cipher/random.c b/cipher/random.c index e0d04a47..9e2878bf 100644 --- a/cipher/random.c +++ b/cipher/random.c @@ -1,5 +1,6 @@ /* random.c - random number generator - * Copyright (C) 1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. + * Copyright (C) 1998, 2000, 2001, 2002, 2003, + * 2004 Free Software Foundation, Inc. * * This file is part of Libgcrypt. * @@ -135,22 +136,33 @@ static void *progress_cb_data; /* Note, we assume that this function is used before any concurrent access happens. */ static void -initialize(void) +initialize_basics(void) { + static int initialized; int err; - err = ath_mutex_init (&pool_lock); - if (err) - log_fatal ("failed to create the pool lock: %s\n", strerror (err) ); + if (!initialized) + { + initialized = 1; + err = ath_mutex_init (&pool_lock); + if (err) + log_fatal ("failed to create the pool lock: %s\n", strerror (err) ); + + err = ath_mutex_init (&nonce_buffer_lock); + if (err) + log_fatal ("failed to create the nonce buffer lock: %s\n", + strerror (err) ); + } +} - err = ath_mutex_init (&nonce_buffer_lock); - if (err) - log_fatal ("failed to create the nonce buffer lock: %s\n", - strerror (err) ); - + +static void +initialize(void) +{ + initialize_basics (); /* The data buffer is allocated somewhat larger, so that we can use - this extra space (which is allocated in secure memory) as a - temporary hash buffer */ + this extra space (which is allocated in secure memory) as a + temporary hash buffer */ rndpool = secure_alloc ? gcry_xcalloc_secure(1,POOLSIZE+BLOCKLEN) : gcry_xcalloc(1,POOLSIZE+BLOCKLEN); keypool = secure_alloc ? gcry_xcalloc_secure(1,POOLSIZE+BLOCKLEN) @@ -180,14 +192,16 @@ _gcry_random_progress (const char *what, int printchar, int current, int total) } -/* Initialize this random subsystem. This function merely calls the - initialize and does not do anything more. Doing this is not really - required but when running in a threaded environment we might get a - race condition otherwise. */ +/* Initialize this random subsystem. If FULL is false, this function + merely calls the initialize and does not do anything more. Doing + this is not really required but when running in a threaded + environment we might get a race condition otherwise. */ void -_gcry_random_initialize () +_gcry_random_initialize (int full) { - if (!is_initialized) + if (!full) + initialize_basics (); + else if (!is_initialized) initialize (); } @@ -974,20 +988,19 @@ do_fast_random_poll (void) /* The fast random pool function as called at some places in libgcrypt. This is merely a wrapper to make sure that this module - is initalized and to look the pool. */ + is initalized and to look the pool. Note, that this function is a + NOP unless a random function has been used or _gcry_initialize (1) + has been used. We use this hack so that the internal use of this + function in cipher_open and md_open won't start filling up the + radnom pool, even if no random will be required by the process. */ void _gcry_fast_random_poll (void) { int err; - /* We have to make sure that the intialization is done because this - gatherer might be called before any other functions and it is not - sufficient to initialize it within do_fast_random_pool because we - want to use the mutex here. FIXME: Whe should initialize the - mutex using a global constructor independent from the - initialization of the pool. */ if (!is_initialized) - initialize (); + return; + err = ath_mutex_lock (&pool_lock); if (err) log_fatal ("failed to acquire the pool lock: %s\n", strerror (err)); |