Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions features/cryptocell/FEATURE_CRYPTOCELL310/ecdh_alt.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
}
else if ( grp->id == MBEDTLS_ECP_DP_CURVE25519 )
{
uint8_t temp_buf[CURVE_25519_KEY_SIZE] = {0};
cc_ecc_25519_comp_shared_params_t* ecdhParams = mbedtls_calloc( 1, sizeof(cc_ecc_25519_comp_shared_params_t) );
if ( ecdhParams == NULL )
{
Expand All @@ -211,18 +212,66 @@ int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
pHeap = ecdhParams;
heapSize = sizeof(cc_ecc_25519_comp_shared_params_t);

if( mbedtls_mpi_size( d ) != CURVE_25519_KEY_SIZE )
{
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, temp_buf,
mbedtls_mpi_size( d ) ) ) ;
ret = convert_CrysError_to_mbedtls_err(
CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes( ecdhParams->privKey,
CURVE_25519_KEY_SIZE,
(uint32_t*)temp_buf,
sizeof( temp_buf) ) );
if ( ret != 0 )
{
mbedtls_platform_zeroize( temp_buf, sizeof(temp_buf) );
goto cleanup;
}

MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, ecdhParams->privKey, mbedtls_mpi_size( d ) ) ) ;
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &Q->X, ecdhParams->pubKey, public_key_size ) );
if( public_key_size != CURVE_25519_KEY_SIZE )
{
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}

ret = convert_CrysError_to_mbedtls_err( CRYS_ECMONT_Scalarmult( secret, ( size_t* )&secret_size,
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &Q->X, temp_buf, public_key_size ) );
ret = convert_CrysError_to_mbedtls_err(
CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes( ecdhParams->pubKey,
CURVE_25519_KEY_SIZE,
(uint32_t*)temp_buf,
sizeof( temp_buf) ) );
if ( ret != 0 )
{
mbedtls_platform_zeroize( temp_buf, sizeof(temp_buf) );
goto cleanup;
}

if( secret_size != CURVE_25519_KEY_SIZE )
{
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}

ret = convert_CrysError_to_mbedtls_err( CRYS_ECMONT_Scalarmult( temp_buf, ( size_t* )&secret_size,
ecdhParams->privKey, CURVE_25519_KEY_SIZE ,
ecdhParams->pubKey, CURVE_25519_KEY_SIZE ,
&ecdhParams->kgTempData ) );
if ( ret != 0 )
{
goto cleanup;
}
ret = convert_CrysError_to_mbedtls_err(
CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes( secret,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this conversion go the other way? We have converted the private and public key to the MsbLsbBytes so that CRYS_ECMONT_Scalarmult() can operate on them, but I would expect the output temp_buf to still be in that encoding. If we now want to operate on it further, outside the Cryptocell world, I'd expect the temp_buf to be converted to the LswMswWords again here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is what mbedtls_mpi_read_binary() does in line 282.
Note that this operation was added after the test vector in our tests failed, so the output of this function is now as expected outside the Cryptocell world

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification 👍

secret_size,
(uint32_t*)temp_buf,
CURVE_25519_KEY_SIZE ) );
if ( ret != 0 )
{
mbedtls_platform_zeroize( temp_buf, sizeof(temp_buf) );
goto cleanup;
}
}
else
{
Expand Down