Address issues found by coverity

1) The MPI test for prime generation missed a return value
   check for a call to `mpi_shift_r`. This is neither
   critical nor new but should be fixed.

2) The RSA keygeneration example program contained code
   initializing an RSA context after a potentially failing
   call to CTR DRBG initialization, leaving the corresponding
   RSA context free call in the cleanup section orphaned.
   The commit fixes this by moving the initializtion of the
   RSA context prior to the first potentially failing call.
This commit is contained in:
Hanno Becker 2018-01-10 11:24:43 +00:00
parent 465c8b7827
commit 175668a8fd
2 changed files with 3 additions and 2 deletions

View File

@ -74,6 +74,7 @@ int main( void )
fflush( stdout ); fflush( stdout );
entropy_init( &entropy ); entropy_init( &entropy );
rsa_init( &rsa, RSA_PKCS_V15, 0 );
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 ) strlen( pers ) ) ) != 0 )
@ -85,7 +86,6 @@ int main( void )
polarssl_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE ); polarssl_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
fflush( stdout ); fflush( stdout );
rsa_init( &rsa, RSA_PKCS_V15, 0 );
if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE, if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
EXPONENT ) ) != 0 ) EXPONENT ) ) != 0 )

View File

@ -823,7 +823,8 @@ void mpi_gen_prime( int bits, int safe, int ref_ret )
TEST_ASSERT( mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 ); TEST_ASSERT( mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
if( safe ) if( safe )
{ {
mpi_shift_r( &X, 1 ); /* X = ( X - 1 ) / 2 */ /* X = ( X - 1 ) / 2 */
TEST_ASSERT( mpi_shift_r( &X, 1 ) == 0 );
TEST_ASSERT( mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 ); TEST_ASSERT( mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
} }
} }