Skip to content

Commit 552563b

Browse files
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.
1 parent 13cf3ec commit 552563b

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

tests/suites/test_suite_ecdh.data

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,11 @@ ecdh_restart:MBEDTLS_ECP_DP_SECP256R1:"C88F01F510D9AC3F70A292DAA2316DE544E9AAB8A
7979
ECDH exchange legacy context
8080
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
8181
ecdh_exchange_legacy:MBEDTLS_ECP_DP_SECP192R1
82+
83+
ECDH calc_secret: ours first, SECP256R1 (RFC 5903)
84+
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
85+
ecdh_exchange_calc_secret:MBEDTLS_ECP_DP_SECP256R1:"c6ef9c5d78ae012a011164acb397ce2088685d8f06bf9be0b283ab46476bee53":"04dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3":0:"d6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de"
86+
87+
ECDH calc_secret: theirs first, SECP256R1 (RFC 5903)
88+
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
89+
ecdh_exchange_calc_secret:MBEDTLS_ECP_DP_SECP256R1:"c6ef9c5d78ae012a011164acb397ce2088685d8f06bf9be0b283ab46476bee53":"04dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3":1:"d6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de"

tests/suites/test_suite_ecdh.function

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
/* BEGIN_HEADER */
22
#include "mbedtls/ecdh.h"
3+
4+
static int load_public_key( int grp_id, data_t *point,
5+
mbedtls_ecp_keypair *ecp )
6+
{
7+
int ok = 0;
8+
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
9+
TEST_ASSERT( mbedtls_ecp_point_read_binary( &ecp->grp,
10+
&ecp->Q,
11+
point->x,
12+
point->len ) == 0 );
13+
TEST_ASSERT( mbedtls_ecp_check_pubkey( &ecp->grp,
14+
&ecp->Q ) == 0 );
15+
ok = 1;
16+
exit:
17+
return( ok );
18+
}
19+
20+
static int load_private_key( int grp_id, data_t *private_key,
21+
mbedtls_ecp_keypair *ecp,
22+
rnd_pseudo_info *rnd_info )
23+
{
24+
int ok = 0;
25+
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
26+
TEST_ASSERT( mbedtls_mpi_read_binary( &ecp->d,
27+
private_key->x,
28+
private_key->len ) == 0 );
29+
TEST_ASSERT( mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) == 0 );
30+
/* Calculate the public key from the private key. */
31+
TEST_ASSERT( mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d,
32+
&ecp->grp.G,
33+
&rnd_pseudo_rand, rnd_info ) == 0 );
34+
ok = 1;
35+
exit:
36+
return( ok );
37+
}
38+
339
/* END_HEADER */
440

541
/* BEGIN_DEPENDENCIES
@@ -464,3 +500,60 @@ exit:
464500
mbedtls_ecdh_free( &cli );
465501
}
466502
/* END_CASE */
503+
504+
/* BEGIN_CASE */
505+
void ecdh_exchange_calc_secret( int grp_id,
506+
data_t *our_private_key,
507+
data_t *their_point,
508+
int ours_first,
509+
data_t *expected )
510+
{
511+
rnd_pseudo_info rnd_info;
512+
mbedtls_ecp_keypair our_key;
513+
mbedtls_ecp_keypair their_key;
514+
mbedtls_ecdh_context ecdh;
515+
unsigned char shared_secret[MBEDTLS_ECP_MAX_BYTES];
516+
size_t shared_secret_length = 0;
517+
518+
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
519+
mbedtls_ecdh_init( &ecdh );
520+
mbedtls_ecp_keypair_init( &our_key );
521+
mbedtls_ecp_keypair_init( &their_key );
522+
523+
if( ! load_private_key( grp_id, our_private_key, &our_key, &rnd_info ) )
524+
goto exit;
525+
if( ! load_public_key( grp_id, their_point, &their_key ) )
526+
goto exit;
527+
528+
/* Import the keys to the ECDH calculation. */
529+
if( ours_first )
530+
{
531+
TEST_ASSERT( mbedtls_ecdh_get_params(
532+
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
533+
TEST_ASSERT( mbedtls_ecdh_get_params(
534+
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
535+
}
536+
else
537+
{
538+
TEST_ASSERT( mbedtls_ecdh_get_params(
539+
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
540+
TEST_ASSERT( mbedtls_ecdh_get_params(
541+
&ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
542+
}
543+
544+
/* Perform the ECDH calculation. */
545+
TEST_ASSERT( mbedtls_ecdh_calc_secret(
546+
&ecdh,
547+
&shared_secret_length,
548+
shared_secret, sizeof( shared_secret ),
549+
&rnd_pseudo_rand, &rnd_info ) == 0 );
550+
TEST_ASSERT( shared_secret_length == expected->len );
551+
TEST_ASSERT( memcmp( expected->x, shared_secret,
552+
shared_secret_length ) == 0 );
553+
554+
exit:
555+
mbedtls_ecdh_free( &ecdh );
556+
mbedtls_ecp_keypair_free( &our_key );
557+
mbedtls_ecp_keypair_free( &their_key );
558+
}
559+
/* END_CASE */

0 commit comments

Comments
 (0)