From 55a3285fafd424d40223358ba4ab0a25b2da2fb4 Mon Sep 17 00:00:00 2001 From: Werner Lewis Date: Mon, 8 Aug 2022 11:53:45 +0100 Subject: [PATCH] Add test case for mbedtls_ecp_set_zero Tests function with various ECP point conditions, covering freshly initialized, zeroed, non-zero, and freed points. Signed-off-by: Werner Lewis --- tests/suites/test_suite_ecp.data | 4 ++ tests/suites/test_suite_ecp.function | 66 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/tests/suites/test_suite_ecp.data b/tests/suites/test_suite_ecp.data index dd466037d9a..5277bc3cf07 100644 --- a/tests/suites/test_suite_ecp.data +++ b/tests/suites/test_suite_ecp.data @@ -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" diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index c5754249ece..ae966a25f1e 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -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( ) {