mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-27 05:24:15 +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
bdc807dbe8
commit
390bbd08f7
@ -37,3 +37,11 @@ ecdh_exchange:MBEDTLS_ECP_DP_SECP192R1
|
||||
ECDH exchange #2
|
||||
depends_on:MBEDTLS_ECP_DP_SECP521R1_ENABLED
|
||||
ecdh_exchange:MBEDTLS_ECP_DP_SECP521R1
|
||||
|
||||
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,47 @@
|
||||
/* BEGIN_HEADER */
|
||||
#include "mbedtls/ecdh.h"
|
||||
|
||||
static int load_public_key( int grp_id, const char *point_str,
|
||||
mbedtls_ecp_keypair *ecp )
|
||||
{
|
||||
int ok = 0;
|
||||
unsigned char point_buf[MBEDTLS_ECP_MAX_BYTES];
|
||||
size_t point_len = unhexify( point_buf, point_str );
|
||||
|
||||
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ecp_point_read_binary( &ecp->grp,
|
||||
&ecp->Q,
|
||||
point_buf,
|
||||
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, const char *private_key_str,
|
||||
mbedtls_ecp_keypair *ecp,
|
||||
rnd_pseudo_info *rnd_info )
|
||||
{
|
||||
int ok = 0;
|
||||
unsigned char private_key_buf[MBEDTLS_ECP_MAX_BYTES];
|
||||
size_t private_key_len = unhexify( private_key_buf, private_key_str );
|
||||
|
||||
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
|
||||
TEST_ASSERT( mbedtls_mpi_read_binary( &ecp->d,
|
||||
private_key_buf,
|
||||
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 */
|
||||
|
||||
/* BEGIN_DEPENDENCIES
|
||||
@ -158,3 +200,64 @@ exit:
|
||||
mbedtls_ecdh_free( &cli );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void ecdh_exchange_calc_secret( int grp_id,
|
||||
char *our_private_key,
|
||||
char *their_point,
|
||||
int ours_first,
|
||||
char *expected_str )
|
||||
{
|
||||
rnd_pseudo_info rnd_info;
|
||||
unsigned char expected_buf[MBEDTLS_ECP_MAX_BYTES];
|
||||
size_t expected_len;
|
||||
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 );
|
||||
|
||||
expected_len = unhexify( expected_buf, expected_str );
|
||||
|
||||
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_buf, 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