|
1 | 1 | /* BEGIN_HEADER */
|
2 | 2 | #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 | + |
3 | 39 | /* END_HEADER */
|
4 | 40 |
|
5 | 41 | /* BEGIN_DEPENDENCIES
|
@@ -464,3 +500,60 @@ exit:
|
464 | 500 | mbedtls_ecdh_free( &cli );
|
465 | 501 | }
|
466 | 502 | /* 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