@@ -201,6 +201,7 @@ int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
201201 }
202202 else if ( grp -> id == MBEDTLS_ECP_DP_CURVE25519 )
203203 {
204+ uint8_t temp_buf [CURVE_25519_KEY_SIZE ] = {0 };
204205 cc_ecc_25519_comp_shared_params_t * ecdhParams = mbedtls_calloc ( 1 , sizeof (cc_ecc_25519_comp_shared_params_t ) );
205206 if ( ecdhParams == NULL )
206207 {
@@ -211,18 +212,66 @@ int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
211212 pHeap = ecdhParams ;
212213 heapSize = sizeof (cc_ecc_25519_comp_shared_params_t );
213214
215+ if ( mbedtls_mpi_size ( d ) != CURVE_25519_KEY_SIZE )
216+ {
217+ ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA ;
218+ goto cleanup ;
219+ }
220+ MBEDTLS_MPI_CHK ( mbedtls_mpi_write_binary ( d , temp_buf ,
221+ mbedtls_mpi_size ( d ) ) ) ;
222+ ret = convert_CrysError_to_mbedtls_err (
223+ CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes ( ecdhParams -> privKey ,
224+ CURVE_25519_KEY_SIZE ,
225+ (uint32_t * )temp_buf ,
226+ sizeof ( temp_buf ) ) );
227+ if ( ret != 0 )
228+ {
229+ mbedtls_platform_zeroize ( temp_buf , sizeof (temp_buf ) );
230+ goto cleanup ;
231+ }
214232
215- MBEDTLS_MPI_CHK ( mbedtls_mpi_write_binary ( d , ecdhParams -> privKey , mbedtls_mpi_size ( d ) ) ) ;
216- MBEDTLS_MPI_CHK ( mbedtls_mpi_write_binary ( & Q -> X , ecdhParams -> pubKey , public_key_size ) );
233+ if ( public_key_size != CURVE_25519_KEY_SIZE )
234+ {
235+ ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA ;
236+ goto cleanup ;
237+ }
217238
218- ret = convert_CrysError_to_mbedtls_err ( CRYS_ECMONT_Scalarmult ( secret , ( size_t * )& secret_size ,
239+ MBEDTLS_MPI_CHK ( mbedtls_mpi_write_binary ( & Q -> X , temp_buf , public_key_size ) );
240+ ret = convert_CrysError_to_mbedtls_err (
241+ CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes ( ecdhParams -> pubKey ,
242+ CURVE_25519_KEY_SIZE ,
243+ (uint32_t * )temp_buf ,
244+ sizeof ( temp_buf ) ) );
245+ if ( ret != 0 )
246+ {
247+ mbedtls_platform_zeroize ( temp_buf , sizeof (temp_buf ) );
248+ goto cleanup ;
249+ }
250+
251+ if ( secret_size != CURVE_25519_KEY_SIZE )
252+ {
253+ ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA ;
254+ goto cleanup ;
255+ }
256+
257+ ret = convert_CrysError_to_mbedtls_err ( CRYS_ECMONT_Scalarmult ( temp_buf , ( size_t * )& secret_size ,
219258 ecdhParams -> privKey , CURVE_25519_KEY_SIZE ,
220259 ecdhParams -> pubKey , CURVE_25519_KEY_SIZE ,
221260 & ecdhParams -> kgTempData ) );
222261 if ( ret != 0 )
223262 {
224263 goto cleanup ;
225264 }
265+ ret = convert_CrysError_to_mbedtls_err (
266+ CRYS_COMMON_ConvertLswMswWordsToMsbLsbBytes ( secret ,
267+ secret_size ,
268+ (uint32_t * )temp_buf ,
269+ CURVE_25519_KEY_SIZE ) );
270+ if ( ret != 0 )
271+ {
272+ mbedtls_platform_zeroize ( temp_buf , sizeof (temp_buf ) );
273+ goto cleanup ;
274+ }
226275 }
227276 else
228277 {
0 commit comments