mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-29 22:24:16 +01:00
Add test case for ecdh_calc_secret
Add a test case for doing an ECDH calculation by calling mbedtls_ecdh_get_params on both keys, then mbedtls_ecdh_calc_secret.
This commit is contained in:
parent
4dc50bc06e
commit
6d9b762ee0
@ -79,3 +79,11 @@ ecdh_restart:MBEDTLS_ECP_DP_SECP256R1:"C88F01F510D9AC3F70A292DAA2316DE544E9AAB8A
|
|||||||
ECDH exchange legacy context
|
ECDH exchange legacy context
|
||||||
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
|
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
|
||||||
ecdh_exchange_legacy:MBEDTLS_ECP_DP_SECP192R1
|
ecdh_exchange_legacy:MBEDTLS_ECP_DP_SECP192R1
|
||||||
|
|
||||||
|
ECDH calc_secret: ours first, SECP256R1 (RFC 5903)
|
||||||
|
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
|
||||||
|
ecdh_exchange_calc_secret:MBEDTLS_ECP_DP_SECP256R1:"c6ef9c5d78ae012a011164acb397ce2088685d8f06bf9be0b283ab46476bee53":"04dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3":0:"d6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de"
|
||||||
|
|
||||||
|
ECDH calc_secret: theirs first, SECP256R1 (RFC 5903)
|
||||||
|
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
|
||||||
|
ecdh_exchange_calc_secret:MBEDTLS_ECP_DP_SECP256R1:"c6ef9c5d78ae012a011164acb397ce2088685d8f06bf9be0b283ab46476bee53":"04dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3":1:"d6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de"
|
||||||
|
@ -1,5 +1,41 @@
|
|||||||
/* BEGIN_HEADER */
|
/* BEGIN_HEADER */
|
||||||
#include "mbedtls/ecdh.h"
|
#include "mbedtls/ecdh.h"
|
||||||
|
|
||||||
|
static int load_public_key( int grp_id, data_t *point,
|
||||||
|
mbedtls_ecp_keypair *ecp )
|
||||||
|
{
|
||||||
|
int ok = 0;
|
||||||
|
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
|
||||||
|
TEST_ASSERT( mbedtls_ecp_point_read_binary( &ecp->grp,
|
||||||
|
&ecp->Q,
|
||||||
|
point->x,
|
||||||
|
point->len ) == 0 );
|
||||||
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &ecp->grp,
|
||||||
|
&ecp->Q ) == 0 );
|
||||||
|
ok = 1;
|
||||||
|
exit:
|
||||||
|
return( ok );
|
||||||
|
}
|
||||||
|
|
||||||
|
static int load_private_key( int grp_id, data_t *private_key,
|
||||||
|
mbedtls_ecp_keypair *ecp,
|
||||||
|
rnd_pseudo_info *rnd_info )
|
||||||
|
{
|
||||||
|
int ok = 0;
|
||||||
|
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
|
||||||
|
TEST_ASSERT( mbedtls_mpi_read_binary( &ecp->d,
|
||||||
|
private_key->x,
|
||||||
|
private_key->len ) == 0 );
|
||||||
|
TEST_ASSERT( mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) == 0 );
|
||||||
|
/* Calculate the public key from the private key. */
|
||||||
|
TEST_ASSERT( mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d,
|
||||||
|
&ecp->grp.G,
|
||||||
|
&rnd_pseudo_rand, rnd_info ) == 0 );
|
||||||
|
ok = 1;
|
||||||
|
exit:
|
||||||
|
return( ok );
|
||||||
|
}
|
||||||
|
|
||||||
/* END_HEADER */
|
/* END_HEADER */
|
||||||
|
|
||||||
/* BEGIN_DEPENDENCIES
|
/* BEGIN_DEPENDENCIES
|
||||||
@ -464,3 +500,60 @@ exit:
|
|||||||
mbedtls_ecdh_free( &cli );
|
mbedtls_ecdh_free( &cli );
|
||||||
}
|
}
|
||||||
/* END_CASE */
|
/* END_CASE */
|
||||||
|
|
||||||
|
/* BEGIN_CASE */
|
||||||
|
void ecdh_exchange_calc_secret( int grp_id,
|
||||||
|
data_t *our_private_key,
|
||||||
|
data_t *their_point,
|
||||||
|
int ours_first,
|
||||||
|
data_t *expected )
|
||||||
|
{
|
||||||
|
rnd_pseudo_info rnd_info;
|
||||||
|
mbedtls_ecp_keypair our_key;
|
||||||
|
mbedtls_ecp_keypair their_key;
|
||||||
|
mbedtls_ecdh_context ecdh;
|
||||||
|
unsigned char shared_secret[MBEDTLS_ECP_MAX_BYTES];
|
||||||
|
size_t shared_secret_length = 0;
|
||||||
|
|
||||||
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
||||||
|
mbedtls_ecdh_init( &ecdh );
|
||||||
|
mbedtls_ecp_keypair_init( &our_key );
|
||||||
|
mbedtls_ecp_keypair_init( &their_key );
|
||||||
|
|
||||||
|
if( ! load_private_key( grp_id, our_private_key, &our_key, &rnd_info ) )
|
||||||
|
goto exit;
|
||||||
|
if( ! load_public_key( grp_id, their_point, &their_key ) )
|
||||||
|
goto exit;
|
||||||
|
|
||||||
|
/* Import the keys to the ECDH calculation. */
|
||||||
|
if( ours_first )
|
||||||
|
{
|
||||||
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
||||||
|
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
|
||||||
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
||||||
|
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
||||||
|
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
|
||||||
|
TEST_ASSERT( mbedtls_ecdh_get_params(
|
||||||
|
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Perform the ECDH calculation. */
|
||||||
|
TEST_ASSERT( mbedtls_ecdh_calc_secret(
|
||||||
|
&ecdh,
|
||||||
|
&shared_secret_length,
|
||||||
|
shared_secret, sizeof( shared_secret ),
|
||||||
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
||||||
|
TEST_ASSERT( shared_secret_length == expected->len );
|
||||||
|
TEST_ASSERT( memcmp( expected->x, shared_secret,
|
||||||
|
shared_secret_length ) == 0 );
|
||||||
|
|
||||||
|
exit:
|
||||||
|
mbedtls_ecdh_free( &ecdh );
|
||||||
|
mbedtls_ecp_keypair_free( &our_key );
|
||||||
|
mbedtls_ecp_keypair_free( &their_key );
|
||||||
|
}
|
||||||
|
/* END_CASE */
|
||||||
|
Loading…
Reference in New Issue
Block a user