Fix odd bitlength RSA key generation

Fix issue that caused a hang up when generating RSA keys of odd
bitlength.
This commit is contained in:
Janos Follath 2016-02-23 14:42:48 +00:00 committed by Simon Butcher
parent 4dfecabb97
commit d61fc6881a
2 changed files with 9 additions and 6 deletions

View file

@ -7,6 +7,7 @@ Bugfix
arguments where the same (in-place doubling). Found and fixed by Janos
Follath. #309
* Fix issue in Makefile that prevented building using armar. #386
* Fix issue that caused a hang up when generating RSA keys of odd bitlength
Changes
* On ARM platforms, when compiling with -O0 with GCC, Clang or armcc5,

View file

@ -97,7 +97,8 @@ int rsa_gen_key( rsa_context *ctx,
if( f_rng == NULL || nbits < 128 || exponent < 3 )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
mpi_init( &P1 ); mpi_init( &Q1 );
mpi_init( &H ); mpi_init( &G );
/*
* find primes P and Q with Q < P so that:
@ -107,14 +108,15 @@ int rsa_gen_key( rsa_context *ctx,
do
{
MPI_CHK( mpi_gen_prime( &ctx->P, ( nbits + 1 ) >> 1, 0,
MPI_CHK( mpi_gen_prime( &ctx->P, nbits >> 1, 0,
f_rng, p_rng ) );
MPI_CHK( mpi_gen_prime( &ctx->Q, ( nbits + 1 ) >> 1, 0,
if( nbits % 2 )
MPI_CHK( mpi_gen_prime( &ctx->Q, ( nbits >> 1 ) + 1, 0,
f_rng, p_rng ) );
else
MPI_CHK( mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
f_rng, p_rng ) );
if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
mpi_swap( &ctx->P, &ctx->Q );
if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
continue;