1
0
Fork 0
mirror of git://git.gnupg.org/gnupg.git synced 2025-07-02 22:46:30 +02:00

chnages done at the train

This commit is contained in:
Werner Koch 1998-08-07 08:53:38 +00:00
parent 48a041279d
commit 6d21f2838d
19 changed files with 167 additions and 126 deletions

View file

@ -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

View file

@ -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 ) )

View file

@ -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 */

View file

@ -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 );
}

View file

@ -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 */
}

View file

@ -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 )

View file

@ -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 );