From 7ba7b3aded7f51051213704edc6c6e710cdee9aa Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Wed, 19 Oct 2022 17:22:15 +0200 Subject: [PATCH 1/2] Update tests to use mbedtls_test_read_mpi_core In conditional assign and swap tests use the mbedtls_test_read_mpi_core function for reading MPIs. Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_core.function | 54 +++++--------- .../suites/test_suite_bignum_mod_raw.function | 70 ++++++------------- 2 files changed, 39 insertions(+), 85 deletions(-) diff --git a/tests/suites/test_suite_bignum_core.function b/tests/suites/test_suite_bignum_core.function index f50fd07e41..021b7b31cf 100644 --- a/tests/suites/test_suite_bignum_core.function +++ b/tests/suites/test_suite_bignum_core.function @@ -345,14 +345,18 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void mpi_core_cond_assign( data_t * input_X, - data_t * input_Y, +void mpi_core_cond_assign( char * input_X, + char * input_Y, int input_bytes ) { mbedtls_mpi_uint *X = NULL; mbedtls_mpi_uint *Y = NULL; - size_t limbs_X = CHARS_TO_LIMBS( input_X->len ); - size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); + size_t limbs_X; + size_t limbs_Y; + + TEST_EQUAL( mbedtls_test_read_mpi_core( &X, &limbs_X, input_X ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi_core( &Y, &limbs_Y, input_Y ), 0 ); + size_t limbs = limbs_X; size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); @@ -361,24 +365,12 @@ void mpi_core_cond_assign( data_t * input_X, TEST_EQUAL( limbs_X, limbs_Y ); TEST_ASSERT( copy_limbs <= limbs ); - ASSERT_ALLOC( X, limbs ); - ASSERT_ALLOC( Y, limbs ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( X, limbs, input_X->x, input_X->len ) - == 0 ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( Y, limbs, input_Y->x, input_Y->len ) - == 0 ); - /* condition is false */ TEST_CF_SECRET( X, bytes ); TEST_CF_SECRET( Y, bytes ); mbedtls_mpi_core_cond_assign( X, Y, copy_limbs, 0 ); - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); /* condition is true */ @@ -387,9 +379,6 @@ void mpi_core_cond_assign( data_t * input_X, mbedtls_mpi_core_cond_assign( X, Y, copy_limbs, 1 ); - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - /* Check if the given length is copied even it is smaller than the length of the given MPIs. */ if( copy_limbs < limbs ) @@ -407,16 +396,20 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void mpi_core_cond_swap( data_t * input_X, - data_t * input_Y, +void mpi_core_cond_swap( char * input_X, + char * input_Y, int input_bytes ) { mbedtls_mpi_uint *tmp_X = NULL; mbedtls_mpi_uint *tmp_Y = NULL; mbedtls_mpi_uint *X = NULL; mbedtls_mpi_uint *Y = NULL; - size_t limbs_X = CHARS_TO_LIMBS( input_X->len ); - size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); + size_t limbs_X; + size_t limbs_Y; + + TEST_EQUAL( mbedtls_test_read_mpi_core( &tmp_X, &limbs_X, input_X ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi_core( &tmp_Y, &limbs_Y, input_Y ), 0 ); + size_t limbs = limbs_X; size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); @@ -425,18 +418,9 @@ void mpi_core_cond_swap( data_t * input_X, TEST_EQUAL( limbs_X, limbs_Y ); TEST_ASSERT( copy_limbs <= limbs ); - ASSERT_ALLOC( tmp_X, limbs ); - ASSERT_ALLOC( tmp_Y, limbs ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_X, limbs, - input_X->x, input_X->len ) - == 0 ); ASSERT_ALLOC( X, limbs ); memcpy( X, tmp_X, bytes ); - TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_Y, limbs, - input_Y->x, input_Y->len ) - == 0 ); ASSERT_ALLOC( Y, limbs ); memcpy( Y, tmp_Y, bytes ); @@ -446,9 +430,6 @@ void mpi_core_cond_swap( data_t * input_X, mbedtls_mpi_core_cond_swap( X, Y, copy_limbs, 0 ); - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - ASSERT_COMPARE( X, bytes, tmp_X, bytes ); ASSERT_COMPARE( Y, bytes, tmp_Y, bytes ); @@ -458,9 +439,6 @@ void mpi_core_cond_swap( data_t * input_X, mbedtls_mpi_core_cond_swap( X, Y, copy_limbs, 1 ); - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - /* Check if the given length is copied even it is smaller than the length of the given MPIs. */ if( copy_limbs < limbs ) diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 8ac1ef4977..556cca07f0 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -110,16 +110,20 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void mpi_mod_raw_cond_assign( data_t * input_X, - data_t * input_Y, +void mpi_mod_raw_cond_assign( char * input_X, + char * input_Y, int input_bytes ) { mbedtls_mpi_uint *X = NULL; mbedtls_mpi_uint *Y = NULL; mbedtls_mpi_uint *buff_m = NULL; mbedtls_mpi_mod_modulus m; - size_t limbs_X = CHARS_TO_LIMBS( input_X->len ); - size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); + size_t limbs_X; + size_t limbs_Y; + + TEST_EQUAL( mbedtls_test_read_mpi_core( &X, &limbs_X, input_X ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi_core( &Y, &limbs_Y, input_Y ), 0 ); + size_t limbs = limbs_X; size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); @@ -130,24 +134,12 @@ void mpi_mod_raw_cond_assign( data_t * input_X, TEST_EQUAL( limbs_X, limbs_Y ); TEST_ASSERT( copy_limbs <= limbs ); - ASSERT_ALLOC( X, limbs ); - ASSERT_ALLOC( Y, limbs ); - - ASSERT_ALLOC( buff_m, limbs ); - memset( buff_m, 0xFF, copy_bytes ); - TEST_ASSERT( mbedtls_mpi_mod_modulus_setup( + ASSERT_ALLOC( buff_m, copy_limbs ); + memset( buff_m, 0xFF, copy_limbs ); + TEST_EQUAL( mbedtls_mpi_mod_modulus_setup( &m, buff_m, copy_limbs, MBEDTLS_MPI_MOD_EXT_REP_BE, - MBEDTLS_MPI_MOD_REP_MONTGOMERY ) - == 0 ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( X, limbs, - input_X->x, input_X->len ) - == 0 ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( Y, limbs, - input_Y->x, input_Y->len ) - == 0 ); + MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 ); /* condition is false */ TEST_CF_SECRET( X, bytes ); @@ -155,9 +147,6 @@ void mpi_mod_raw_cond_assign( data_t * input_X, mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 0 ); - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); /* condition is true */ @@ -166,9 +155,6 @@ void mpi_mod_raw_cond_assign( data_t * input_X, mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 1 ); - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - /* Check if the given length is copied even it is smaller than the length of the given MPIs. */ if( copy_limbs len ); - size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); + size_t limbs_X; + size_t limbs_Y; + + TEST_EQUAL( mbedtls_test_read_mpi_core( &tmp_X, &limbs_X, input_X ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi_core( &tmp_Y, &limbs_Y, input_Y ), 0 ); + size_t limbs = limbs_X; size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); @@ -211,24 +201,16 @@ void mpi_mod_raw_cond_swap( data_t * input_X, TEST_EQUAL( limbs_X, limbs_Y ); TEST_ASSERT( copy_limbs <= limbs ); - ASSERT_ALLOC( tmp_X, limbs ); - ASSERT_ALLOC( tmp_Y, limbs ); - ASSERT_ALLOC( buff_m, copy_limbs ); - memset( buff_m, 0xFF, copy_bytes ); - TEST_ASSERT( mbedtls_mpi_mod_modulus_setup( + memset( buff_m, 0xFF, copy_limbs ); + TEST_EQUAL( mbedtls_mpi_mod_modulus_setup( &m, buff_m, copy_limbs, MBEDTLS_MPI_MOD_EXT_REP_BE, - MBEDTLS_MPI_MOD_REP_MONTGOMERY ) - == 0 ); + MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 ); - TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_X, limbs, input_X->x, input_X->len ) - == 0 ); ASSERT_ALLOC( X, limbs ); memcpy( X, tmp_X, bytes ); - TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_Y, limbs, input_Y->x, input_Y->len ) - == 0 ); ASSERT_ALLOC( Y, bytes ); memcpy( Y, tmp_Y, bytes ); @@ -238,9 +220,6 @@ void mpi_mod_raw_cond_swap( data_t * input_X, mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 0 ); - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - ASSERT_COMPARE( X, bytes, tmp_X, bytes ); ASSERT_COMPARE( Y, bytes, tmp_Y, bytes ); @@ -250,9 +229,6 @@ void mpi_mod_raw_cond_swap( data_t * input_X, mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 1 ); - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - /* Check if the given length is copied even it is smaller than the length of the given MPIs. */ if( copy_limbs < limbs ) From a8cf998bc9569ce29dae28c3387a44882d226cdc Mon Sep 17 00:00:00 2001 From: Gabor Mezei Date: Thu, 20 Oct 2022 12:27:36 +0200 Subject: [PATCH 2/2] Let the allocated memory visible for the memory sanitizer Signed-off-by: Gabor Mezei --- tests/suites/test_suite_bignum_core.function | 15 +++++++++++++++ tests/suites/test_suite_bignum_mod_raw.function | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tests/suites/test_suite_bignum_core.function b/tests/suites/test_suite_bignum_core.function index 021b7b31cf..612a7c6bd4 100644 --- a/tests/suites/test_suite_bignum_core.function +++ b/tests/suites/test_suite_bignum_core.function @@ -371,6 +371,9 @@ void mpi_core_cond_assign( char * input_X, mbedtls_mpi_core_cond_assign( X, Y, copy_limbs, 0 ); + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); /* condition is true */ @@ -379,10 +382,16 @@ void mpi_core_cond_assign( char * input_X, mbedtls_mpi_core_cond_assign( X, Y, copy_limbs, 1 ); + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + /* Check if the given length is copied even it is smaller than the length of the given MPIs. */ if( copy_limbs < limbs ) { + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + ASSERT_COMPARE( X, copy_bytes, Y, copy_bytes ); TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); } @@ -430,6 +439,9 @@ void mpi_core_cond_swap( char * input_X, mbedtls_mpi_core_cond_swap( X, Y, copy_limbs, 0 ); + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + ASSERT_COMPARE( X, bytes, tmp_X, bytes ); ASSERT_COMPARE( Y, bytes, tmp_Y, bytes ); @@ -439,6 +451,9 @@ void mpi_core_cond_swap( char * input_X, mbedtls_mpi_core_cond_swap( X, Y, copy_limbs, 1 ); + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + /* Check if the given length is copied even it is smaller than the length of the given MPIs. */ if( copy_limbs < limbs ) diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 556cca07f0..4b906751f2 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -147,6 +147,9 @@ void mpi_mod_raw_cond_assign( char * input_X, mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 0 ); + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); /* condition is true */ @@ -155,6 +158,9 @@ void mpi_mod_raw_cond_assign( char * input_X, mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 1 ); + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + /* Check if the given length is copied even it is smaller than the length of the given MPIs. */ if( copy_limbs