Skip to content

Commit

Permalink
Merge pull request #6182 from wernerlewis/ecp_set_zero_2.28
Browse files Browse the repository at this point in the history
[Backport 2.28] Add tests for mbedtls_ecp_set_zero
  • Loading branch information
daverodgman authored Aug 11, 2022
2 parents 3469f7a + 55a3285 commit a77287f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/suites/test_suite_ecp.data
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ ECP point muladd secp256r1 #2
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
ecp_muladd:MBEDTLS_ECP_DP_SECP256R1:"01":"04e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1ffffffff20e120e1e1e1e13a4e135157317b79d4ecf329fed4f9eb00dc67dbddae33faca8b6d8a0255b5ce":"01":"04e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e0e1ff20e1ffe120e1e1e173287170a761308491683e345cacaebb500c96e1a7bbd37772968b2c951f0579":"04fab65e09aa5dd948320f86246be1d3fc571e7f799d9005170ed5cc868b67598431a668f96aa9fd0b0eb15f0edf4c7fe1be2885eadcb57e3db4fdd093585d3fa6"

ECP point set zero
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
ecp_set_zero:MBEDTLS_ECP_DP_SECP256R1:"04e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e0e1ff20e1ffe120e1e1e173287170a761308491683e345cacaebb500c96e1a7bbd37772968b2c951f0579"

ECP test vectors Curve448 (RFC 7748 6.2, after decodeUCoordinate)
depends_on:MBEDTLS_ECP_DP_CURVE448_ENABLED
ecp_test_vec_x:MBEDTLS_ECP_DP_CURVE448:"eb7298a5c0d8c29a1dab27f1a6826300917389449741a974f5bac9d98dc298d46555bce8bae89eeed400584bb046cf75579f51d125498f98":"a01fc432e5807f17530d1288da125b0cd453d941726436c8bbd9c5222c3da7fa639ce03db8d23b274a0721a1aed5227de6e3b731ccf7089b":"ad997351b6106f36b0d1091b929c4c37213e0d2b97e85ebb20c127691d0dad8f1d8175b0723745e639a3cb7044290b99e0e2a0c27a6a301c":"0936f37bc6c1bd07ae3dec7ab5dc06a73ca13242fb343efc72b9d82730b445f3d4b0bd077162a46dcfec6f9b590bfcbcf520cdb029a8b73e":"9d874a5137509a449ad5853040241c5236395435c36424fd560b0cb62b281d285275a740ce32a22dd1740f4aa9161cec95ccc61a18f4ff07"
Expand Down
66 changes: 66 additions & 0 deletions tests/suites/test_suite_ecp.function
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,72 @@ exit:
}
/* END_CASE */

/* BEGIN_CASE */
void ecp_set_zero( int id, data_t * P_bin )
{
mbedtls_ecp_group grp;
mbedtls_ecp_point pt, zero_pt, nonzero_pt;

mbedtls_ecp_group_init( &grp );
mbedtls_ecp_point_init( &pt );
mbedtls_ecp_point_init( &zero_pt );
mbedtls_ecp_point_init( &nonzero_pt );

// Set zero and non-zero points for comparison
TEST_EQUAL( mbedtls_ecp_set_zero( &zero_pt ), 0 );
TEST_EQUAL( mbedtls_ecp_group_load( &grp, id ), 0 );
TEST_EQUAL( mbedtls_ecp_point_read_binary( &grp, &nonzero_pt,
P_bin->x, P_bin->len ), 0 );
TEST_EQUAL( mbedtls_ecp_is_zero( &zero_pt ), 1 );
TEST_EQUAL( mbedtls_ecp_is_zero( &nonzero_pt ), 0 );

// Test initialized point
TEST_EQUAL( mbedtls_ecp_set_zero( &pt ), 0 );
TEST_EQUAL( mbedtls_ecp_is_zero( &pt ), 1 );
TEST_EQUAL( mbedtls_ecp_point_cmp( &zero_pt, &pt ), 0 );
TEST_EQUAL( mbedtls_ecp_point_cmp( &nonzero_pt, &zero_pt ),
MBEDTLS_ERR_ECP_BAD_INPUT_DATA );

// Test zeroed point
TEST_EQUAL( mbedtls_ecp_set_zero( &pt ), 0 );
TEST_EQUAL( mbedtls_ecp_is_zero( &pt ), 1 );
TEST_EQUAL( mbedtls_ecp_point_cmp( &zero_pt, &pt ), 0 );
TEST_EQUAL( mbedtls_ecp_point_cmp( &nonzero_pt, &pt ),
MBEDTLS_ERR_ECP_BAD_INPUT_DATA );

// Set point to non-zero value
TEST_EQUAL( mbedtls_ecp_point_read_binary( &grp, &pt,
P_bin->x, P_bin->len ), 0 );
TEST_EQUAL( mbedtls_ecp_is_zero( &pt ), 0 );
TEST_EQUAL( mbedtls_ecp_point_cmp( &zero_pt, &pt ),
MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
TEST_EQUAL( mbedtls_ecp_point_cmp( &nonzero_pt, &pt), 0 );

// Test non-zero point
TEST_EQUAL( mbedtls_ecp_set_zero( &pt ), 0 );
TEST_EQUAL( mbedtls_ecp_is_zero( &pt ), 1 );
TEST_EQUAL( mbedtls_ecp_point_cmp( &zero_pt, &pt ), 0 );
TEST_EQUAL( mbedtls_ecp_point_cmp( &nonzero_pt, &pt ),
MBEDTLS_ERR_ECP_BAD_INPUT_DATA );

// Test freed non-zero point
TEST_EQUAL( mbedtls_ecp_point_read_binary( &grp, &pt,
P_bin->x, P_bin->len ), 0 );
mbedtls_ecp_point_free( &pt );
TEST_EQUAL( mbedtls_ecp_set_zero( &pt ), 0 );
TEST_EQUAL( mbedtls_ecp_is_zero( &pt ), 1 );
TEST_EQUAL( mbedtls_ecp_point_cmp( &zero_pt, &pt ), 0 );
TEST_EQUAL( mbedtls_ecp_point_cmp( &nonzero_pt, &pt),
MBEDTLS_ERR_ECP_BAD_INPUT_DATA );

exit:
mbedtls_ecp_group_free( &grp );
mbedtls_ecp_point_free( &pt );
mbedtls_ecp_point_free( &zero_pt );
mbedtls_ecp_point_free( &nonzero_pt );
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
void ecp_selftest( )
{
Expand Down

0 comments on commit a77287f

Please sign in to comment.