diff options
author | Werner Koch <[email protected]> | 1998-08-07 08:53:38 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 1998-08-07 08:53:38 +0000 |
commit | 6d21f2838dcfee933f4c430fba68ba8ada3abd50 (patch) | |
tree | ecf44aec40a55d3fd46fc6a6d96bb3afa7901029 /cipher | |
parent | add salted and iterated mode (diff) | |
download | gnupg-6d21f2838dcfee933f4c430fba68ba8ada3abd50.tar.gz gnupg-6d21f2838dcfee933f4c430fba68ba8ada3abd50.zip |
chnages done at the train
Diffstat (limited to 'cipher')
-rw-r--r-- | cipher/ChangeLog | 5 | ||||
-rw-r--r-- | cipher/dsa.c | 18 | ||||
-rw-r--r-- | cipher/elgamal.c | 22 | ||||
-rw-r--r-- | cipher/g10c.c | 6 | ||||
-rw-r--r-- | cipher/primegen.c | 16 | ||||
-rw-r--r-- | cipher/random.c | 38 | ||||
-rw-r--r-- | cipher/random.h | 1 |
7 files changed, 57 insertions, 49 deletions
diff --git a/cipher/ChangeLog b/cipher/ChangeLog index 528de20cd..cb6082e34 100644 --- a/cipher/ChangeLog +++ b/cipher/ChangeLog @@ -1,3 +1,8 @@ +Thu Aug 6 17:25:38 1998 Werner Koch,mobil,,, (wk@tobold) + + * random.c (get_random_byte): Removed and changed all callers + to use get_random_bits() + Mon Jul 27 10:30:22 1998 Werner Koch (wk@(none)) * cipher.c : Support for other blocksizes diff --git a/cipher/dsa.c b/cipher/dsa.c index 107ed71c2..accbca9e8 100644 --- a/cipher/dsa.c +++ b/cipher/dsa.c @@ -66,7 +66,17 @@ gen_k( MPI q ) for(;;) { if( DBG_CIPHER ) fputc('.', stderr); - mpi_set_bytes( k, nbits , get_random_byte, 1 ); + { char *p = get_random_bits( nbits, 1, 1 ); + mpi_set_buffer( k, p, (nbits+7)/8, 0 ); + m_free(p); + /* make sure that the number is of the exact lenght */ + if( mpi_test_bit( k, nbits-1 ) ) + mpi_set_highbit( k, nbits-1 ); + else { + mpi_set_highbit( k, nbits-1 ); + mpi_clear_bit( k, nbits-1 ); + } + } if( !(mpi_cmp( k, q ) < 0) ) /* check: k < q */ continue; /* no */ if( !(mpi_cmp_ui( k, 0 ) > 0) ) /* check: k > 0 */ @@ -92,7 +102,11 @@ test_keys( DSA_secret_key *sk, unsigned qbits ) pk.q = sk->q; pk.g = sk->g; pk.y = sk->y; - mpi_set_bytes( test, qbits, get_random_byte, 0 ); + /*mpi_set_bytes( test, qbits, get_random_byte, 0 );*/ + { char *p = get_random_bits( qbits, 0, 0 ); + mpi_set_buffer( test, p, (qbits+7)/8, 0 ); + m_free(p); + } sign( out1_a, out1_b, test, sk ); if( !verify( out1_a, out1_b, test, &pk ) ) diff --git a/cipher/elgamal.c b/cipher/elgamal.c index 9b9981da1..b330ccff0 100644 --- a/cipher/elgamal.c +++ b/cipher/elgamal.c @@ -60,7 +60,7 @@ static void test_keys( ELG_secret_key *sk, unsigned nbits ) { ELG_public_key pk; - MPI test = mpi_alloc( nbits / BITS_PER_MPI_LIMB ); + MPI test = mpi_alloc( 0 ); MPI out1_a = mpi_alloc( nbits / BITS_PER_MPI_LIMB ); MPI out1_b = mpi_alloc( nbits / BITS_PER_MPI_LIMB ); MPI out2 = mpi_alloc( nbits / BITS_PER_MPI_LIMB ); @@ -69,7 +69,11 @@ test_keys( ELG_secret_key *sk, unsigned nbits ) pk.g = sk->g; pk.y = sk->y; - mpi_set_bytes( test, nbits, get_random_byte, 0 ); + /*mpi_set_bytes( test, nbits, get_random_byte, 0 );*/ + { char *p = get_random_bits( nbits, 0, 0 ); + mpi_set_buffer( test, p, (nbits+7)/8, 0 ); + m_free(p); + } encrypt( out1_a, out1_b, test, &pk ); decrypt( out2, out1_a, out1_b, sk ); @@ -94,7 +98,7 @@ test_keys( ELG_secret_key *sk, unsigned nbits ) static MPI gen_k( MPI p ) { - MPI k = mpi_alloc_secure( mpi_get_nlimbs(p) ); + MPI k = mpi_alloc_secure( 0 ); MPI temp = mpi_alloc( mpi_get_nlimbs(p) ); MPI p_1 = mpi_copy(p); unsigned nbits = mpi_get_nbits(p); @@ -105,7 +109,17 @@ gen_k( MPI p ) for(;;) { if( DBG_CIPHER ) fputc('.', stderr); - mpi_set_bytes( k, nbits , get_random_byte, 1 ); + { char *p = get_random_bits( nbits, 1, 1 ); + mpi_set_buffer( k, p, (nbits+7)/8, 0 ); + m_free(p); + /* make sure that the number is of the exact lenght */ + if( mpi_test_bit( k, nbits-1 ) ) + mpi_set_highbit( k, nbits-1 ); + else { + mpi_set_highbit( k, nbits-1 ); + mpi_clear_bit( k, nbits-1 ); + } + } if( !(mpi_cmp( k, p_1 ) < 0) ) /* check: k < (p-1) */ continue; /* no */ if( !(mpi_cmp_ui( k, 0 ) > 0) ) /* check: k > 0 */ diff --git a/cipher/g10c.c b/cipher/g10c.c index 5bf0eb61f..d5f79bda5 100644 --- a/cipher/g10c.c +++ b/cipher/g10c.c @@ -35,10 +35,4 @@ g10c_generate_secret_prime( unsigned nbits ) return generate_secret_prime( nbits ); } -byte -g10c_get_random_byte( int level ) -{ - return get_random_byte( level ); -} - diff --git a/cipher/primegen.c b/cipher/primegen.c index 66d40dbfc..addc51fd5 100644 --- a/cipher/primegen.c +++ b/cipher/primegen.c @@ -293,7 +293,12 @@ gen_prime( unsigned nbits, int secret, int randomlevel ) int dotcount=0; /* generate a random number */ - mpi_set_bytes( prime, nbits, get_random_byte, randomlevel ); + /*mpi_set_bytes( prime, nbits, get_random_byte, randomlevel );*/ + { char *p = get_random_bits( nbits, randomlevel, secret ); + mpi_set_buffer( prime, p, (nbits+7)/8, 0 ); + m_free(p); + } + /* set high order bit to 1, set low order bit to 1 */ mpi_set_highbit( prime, nbits-1 ); mpi_set_bit( prime, 0 ); @@ -423,8 +428,13 @@ is_prime( MPI n, int steps, int *count ) mpi_set_ui( x, 2 ); } else { - mpi_set_bytes( x, nbits-1, get_random_byte, 0 ); - /* work around a bug in mpi_set_bytes */ + /*mpi_set_bytes( x, nbits-1, get_random_byte, 0 );*/ + { char *p = get_random_bits( nbits, 0, 0 ); + mpi_set_buffer( x, p, (nbits+7)/8, 0 ); + m_free(p); + } + /* make sure that the number is smaller than the prime + * and keep the randomness of the high bit */ if( mpi_test_bit( x, nbits-2 ) ) { mpi_set_highbit( x, nbits-2 ); /* clear all higher bits */ } diff --git a/cipher/random.c b/cipher/random.c index 75754cd01..29b82ee7c 100644 --- a/cipher/random.c +++ b/cipher/random.c @@ -47,15 +47,8 @@ #error weird size for an unsigned long #endif -struct cache { - int len; - int size; - byte *buffer; -}; - static int is_initialized; -static struct cache cache[3]; #define MASK_LEVEL(a) do {if( a > 2 ) a = 2; else if( a < 0 ) a = 0; } while(0) static char *rndpool; /* allocated size is POOLSIZE+BLOCKLEN */ static char *keypool; /* allocated size is POOLSIZE+BLOCKLEN */ @@ -113,38 +106,17 @@ quick_random_gen( int onoff ) void randomize_buffer( byte *buffer, size_t length, int level ) { - for( ; length; length-- ) - *buffer++ = get_random_byte(level); -} - - -byte -get_random_byte( int level ) -{ - MASK_LEVEL(level); - if( !cache[level].len ) { - if( !is_initialized ) - initialize(); - if( !cache[level].buffer ) { - cache[level].size = 100; - cache[level].buffer = level && secure_alloc? - m_alloc_secure( cache[level].size ) - : m_alloc( cache[level].size ); - } - read_pool(cache[level].buffer, cache[level].size, level ); - cache[level].len = cache[level].size; - } - - return cache[level].buffer[--cache[level].len]; + char *p = get_random_bits( length*8, level, m_is_secure(buffer) ); + memcpy( buffer, p, length ); + m_free(p); } /**************** * Return a pointer to a randomized buffer of level 0 and LENGTH bits - * caller must free the buffer. This function does not use the - * cache (will be removed in future). Note: The returned value is - * rounded up to bytes. + * caller must free the buffer. + * Note: The returned value is rounded up to bytes. */ byte * get_random_bits( size_t nbits, int level, int secure ) diff --git a/cipher/random.h b/cipher/random.h index d93e5b766..2ac50a7d4 100644 --- a/cipher/random.h +++ b/cipher/random.h @@ -26,7 +26,6 @@ void secure_random_alloc(void); int quick_random_gen( int onoff ); void randomize_buffer( byte *buffer, size_t length, int level ); -byte get_random_byte( int level ); byte *get_random_bits( size_t nbits, int level, int secure ); void add_randomness( const void *buffer, size_t length, int source ); |