Skip to content

Commit 0b1b71d

Browse files
Fix ecdh_get_params with mismatching group
If mbedtls_ecdh_get_params is called with keys belonging to different groups, make it return an error the second time, rather than silently interpret the first key as being on the second curve. This makes the non-regression test added by the previous commit pass.
1 parent c4dff06 commit 0b1b71d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

library/ecdh.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,21 @@ int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
442442
ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS ||
443443
side == MBEDTLS_ECDH_THEIRS );
444444

445-
if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
446-
return( ret );
445+
if( ctx->grp.id == MBEDTLS_ECP_DP_NONE )
446+
{
447+
/* This is the first call to get_params(). Set up the context
448+
* for use with the group. */
449+
if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 )
450+
return( ret );
451+
}
452+
else
453+
{
454+
/* This is not the first call to get_params(). Check that the
455+
* current key's group is the same as the context's, which was set
456+
* from the first key's group. */
457+
if( ctx->grp.id != key->grp.id )
458+
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
459+
}
447460

448461
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
449462
return( ecdh_get_params_internal( ctx, key, side ) );

0 commit comments

Comments
 (0)