Skip to content

Commit

Permalink
Prevent memory leak in ecp_check_pubkey_x25519()
Browse files Browse the repository at this point in the history
Signed-off-by: Janos Follath <[email protected]>
  • Loading branch information
yanesca committed Jun 25, 2021
1 parent 520f0a0 commit b4c676e
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions library/ecp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2999,18 +2999,30 @@ static int ecp_check_pubkey_x25519( const mbedtls_mpi *X, const mbedtls_mpi *P )
/* Check against the known bad values that are less than P in the
* following list: https://cr.yp.to/ecdh.html#validate */
if( mbedtls_mpi_cmp_int( &XmP, 1 ) <= 0 ) /* takes care of 0 and 1 */
return( MBEDTLS_ERR_ECP_INVALID_KEY );
{
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
goto cleanup;
}

if( mbedtls_mpi_cmp_mpi( &XmP, &ecp_x25519_bad_point_1 ) == 0 )
return( MBEDTLS_ERR_ECP_INVALID_KEY );
{
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
goto cleanup;
}

if( mbedtls_mpi_cmp_mpi( &XmP, &ecp_x25519_bad_point_2 ) == 0 )
return( MBEDTLS_ERR_ECP_INVALID_KEY );
{
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
goto cleanup;
}

/* Final check: check if XmP + 1 is P (final because it changes XmP!) */
MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &XmP, &XmP, 1 ) );
if( mbedtls_mpi_cmp_mpi( &XmP, P ) == 0 )
return( MBEDTLS_ERR_ECP_INVALID_KEY );
{
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
goto cleanup;
}

ret = 0;

Expand Down

0 comments on commit b4c676e

Please sign in to comment.