Skip to content

Commit

Permalink
Fixed memory leak in ecdsa_sign() / ecdsa_verify() in case of error
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Bakker committed Jul 26, 2013
1 parent 1e6a175 commit cca998a
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions library/ecdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ int ecdsa_sign( const ecp_group *grp, mpi *r, mpi *s,
MPI_CHK( mpi_copy( r, &R.X ) );

if( key_tries++ > 10 )
return( POLARSSL_ERR_ECP_GENERIC );
{
ret = POLARSSL_ERR_ECP_GENERIC;
goto cleanup;
}
}
while( mpi_cmp_int( r, 0 ) == 0 );

Expand All @@ -94,7 +97,10 @@ int ecdsa_sign( const ecp_group *grp, mpi *r, mpi *s,
MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );

if( sign_tries++ > 10 )
return( POLARSSL_ERR_ECP_GENERIC );
{
ret = POLARSSL_ERR_ECP_GENERIC;
goto cleanup;
}
}
while( mpi_cmp_int( s, 0 ) == 0 );

Expand Down Expand Up @@ -127,7 +133,8 @@ int ecdsa_verify( const ecp_group *grp,
if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
{
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}

/*
Expand Down Expand Up @@ -159,13 +166,19 @@ int ecdsa_verify( const ecp_group *grp,
MPI_CHK( ecp_add( grp, &R, &R, &P ) );

if( ecp_is_zero( &R ) )
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
{
ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}

/*
* Step 6: check that xR == r
*/
if( mpi_cmp_mpi( &R.X, r ) != 0 )
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
{
ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}

cleanup:
ecp_point_free( &R ); ecp_point_free( &P );
Expand Down

0 comments on commit cca998a

Please sign in to comment.