diff options
Diffstat (limited to 'cipher')
-rw-r--r-- | cipher/elgamal.c | 4 | ||||
-rw-r--r-- | cipher/primegen.c | 15 | ||||
-rw-r--r-- | cipher/rsa.c | 16 | ||||
-rw-r--r-- | cipher/rsa.h | 1 |
4 files changed, 25 insertions, 11 deletions
diff --git a/cipher/elgamal.c b/cipher/elgamal.c index a7450e068..63ec06f57 100644 --- a/cipher/elgamal.c +++ b/cipher/elgamal.c @@ -95,7 +95,7 @@ gen_k( MPI p ) if( DBG_CIPHER ) fputc('.', stderr); mpi_set_bytes( k, nbits, get_random_byte, 1 ); - mpi_set_bit( k, nbits-1 ); /* make sure it's high (needed?) */ + mpi_set_bit( k, nbits-1 ); /* make sure it's high (really needed?) */ if( mpi_cmp( k, p_1 ) >= 0 ) continue; /* is not smaller than (p-1) */ if( mpi_gcd( temp, k, p_1 ) ) @@ -136,7 +136,7 @@ elg_generate( ELG_public_key *pk, ELG_secret_key *sk, unsigned nbits ) fputc('.', stderr); mpi_set_bytes( x, nbits, get_random_byte, 1 ); /* fixme: should be 2 */ mpi_set_bit( x, nbits-1 ); /* make sure it's high (needed?) */ - } while( mpi_cmp( x, p ) >= 0 ); /* x must be samller than p */ + } while( mpi_cmp( x, p ) >= 0 ); /* x must be smaller than p */ y = mpi_alloc(nbits/BITS_PER_MPI_LIMB); mpi_powm( y, g, x, p ); diff --git a/cipher/primegen.c b/cipher/primegen.c index d69f09ac3..49ec8f659 100644 --- a/cipher/primegen.c +++ b/cipher/primegen.c @@ -98,8 +98,7 @@ gen_prime( unsigned nbits, int secret ) } if( x ) continue; /* found a multiple of a already known prime */ - if( DBG_CIPHER ) - fputc('.', stderr); + fputc('.', stderr); mpi_add_ui( prime, prime, step ); @@ -108,8 +107,7 @@ gen_prime( unsigned nbits, int secret ) mpi_powm( result, val_2, prime, prime ); if( mpi_cmp_ui(result, 2) ) continue; /* stepping (fermat test failed) */ - if( DBG_CIPHER ) - fputc('+', stderr); + fputc('+', stderr); /* perform stronger tests */ if( !is_not_prime(prime, nbits, 5, &count2 ) ) { @@ -120,8 +118,9 @@ gen_prime( unsigned nbits, int secret ) break; /* step loop, cont with a new prime */ } } + + fputc('\n', stderr); if( DBG_CIPHER ) { - fputc('\n', stderr); log_debug("performed %u simple and %u stronger tests\n", count1, count2 ); log_mpidump("found prime: ", prime ); @@ -134,8 +133,7 @@ gen_prime( unsigned nbits, int secret ) return prime; } } - if( DBG_CIPHER ) - fputc(':', stderr); /* restart with a new random value */ + fputc(':', stderr); /* restart with a new random value */ } } @@ -179,8 +177,7 @@ is_not_prime( MPI n, unsigned nbits, int steps, int *count ) if( j == k ) goto leave; } - if( DBG_CIPHER ) - fputc('+', stderr); + fputc('+', stderr); } rc = 0; /* may be a prime */ diff --git a/cipher/rsa.c b/cipher/rsa.c index a1f08457b..db82b48d7 100644 --- a/cipher/rsa.c +++ b/cipher/rsa.c @@ -157,6 +157,22 @@ rsa_generate( RSA_public_key *pk, RSA_secret_key *sk, unsigned nbits ) } +/**************** + * Test wether the secret key is valid. + * Returns: true if this is a valid key. + */ +int +rsa_check_secret_key( RSA_secret_key *sk ) +{ + int rc; + MPI temp = mpi_alloc( mpi_get_nlimbs(sk->p)*2 ); + + mpi_mul(temp, sk->p, sk->q ); + rc = mpi_cmp( temp, sk->n ); + mpi_free(temp); + return !rc; +} + /**************** diff --git a/cipher/rsa.h b/cipher/rsa.h index a9980d0bc..1b6d189bf 100644 --- a/cipher/rsa.h +++ b/cipher/rsa.h @@ -46,6 +46,7 @@ typedef struct { void rsa_free_public_key( RSA_public_key *pk ); void rsa_free_secret_key( RSA_secret_key *sk ); void rsa_generate( RSA_public_key *pk, RSA_secret_key *sk, unsigned nbits ); +int rsa_check_secret_key( RSA_secret_key *sk ); void rsa_public(MPI output, MPI input, RSA_public_key *skey ); void rsa_secret(MPI output, MPI input, RSA_secret_key *skey ); |