diff --git a/tests/suites/test_suite_bignum_mod.data b/tests/suites/test_suite_bignum_mod.data index c76dad8f8..cb781403d 100644 --- a/tests/suites/test_suite_bignum_mod.data +++ b/tests/suites/test_suite_bignum_mod.data @@ -24,63 +24,3 @@ mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_LE:MBEDTLS_MPI_MOD_REP_OPT_RED:0 Test mbedtls_mpi_mod_setup #9 (Both representations valid) mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_LE:MBEDTLS_MPI_MOD_REP_MONTGOMERY:0 - -Base test mbedtls_mpi_mod_mpi #1 -mpi_mod_mpi:"3e8":"d":"c":0 - -Base test mbedtls_mpi_mod_mpi #2 (Divide by zero (null)) -mpi_mod_mpi:"3e8":"":"0":MBEDTLS_ERR_MPI_DIVISION_BY_ZERO - -Base test mbedtls_mpi_mod_mpi #2 (Divide by zero (1 limb)) -mpi_mod_mpi:"3e8":"0":"0":MBEDTLS_ERR_MPI_DIVISION_BY_ZERO - -Base test mbedtls_mpi_mod_mpi #3 -mpi_mod_mpi:"-3e8":"d":"1":0 - -Base test mbedtls_mpi_mod_mpi #4 (Negative modulo) -mpi_mod_mpi:"3e8":"-d":"-1":MBEDTLS_ERR_MPI_NEGATIVE_VALUE - -Base test mbedtls_mpi_mod_mpi #5 (Negative modulo) -mpi_mod_mpi:"-3e8":"-d":"-c":MBEDTLS_ERR_MPI_NEGATIVE_VALUE - -Test mbedtls_mpi_mod_mpi: 0 (null) % 1 -mpi_mod_mpi:"":"1":"":0 - -Test mbedtls_mpi_mod_mpi: 0 (null) % -1 -mpi_mod_mpi:"":"-1":"":MBEDTLS_ERR_MPI_NEGATIVE_VALUE - -Base test mbedtls_mpi_mod_int #1 -mpi_mod_int:"3e8":13:12:0 - -Base test mbedtls_mpi_mod_int #2 (Divide by zero) -mpi_mod_int:"3e8":0:0:MBEDTLS_ERR_MPI_DIVISION_BY_ZERO - -Base test mbedtls_mpi_mod_int #3 -mpi_mod_int:"-3e8":13:1:0 - -Base test mbedtls_mpi_mod_int #4 (Negative modulo) -mpi_mod_int:"3e8":-13:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE - -Base test mbedtls_mpi_mod_int #5 (Negative modulo) -mpi_mod_int:"-3e8":-13:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE - -Base test mbedtls_mpi_mod_int #6 (By 1) -mpi_mod_int:"3e8":1:0:0 - -Base test mbedtls_mpi_mod_int #7 (By 2) -mpi_mod_int:"3e9":2:1:0 - -Base test mbedtls_mpi_mod_int #8 (By 2) -mpi_mod_int:"3e8":2:0:0 - -Test mbedtls_mpi_mod_int: 0 (null) % 1 -mpi_mod_int:"":1:0:0 - -Test mbedtls_mpi_mod_int: 0 (null) % 2 -mpi_mod_int:"":2:0:0 - -Test mbedtls_mpi_mod_int: 0 (null) % -1 -mpi_mod_int:"":-1:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE - -Test mbedtls_mpi_mod_int: 0 (null) % -2 -mpi_mod_int:"":-2:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index 154362306..9f7320965 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -4,19 +4,6 @@ #include "bignum_mod.h" #include "constant_time_internal.h" #include "test/constant_flow.h" - -/* Check the validity of the sign bit in an MPI object. Reject representations - * that are not supported by the rest of the library and indicate a bug when - * constructing the value. */ -static int sign_is_valid( const mbedtls_mpi *X ) -{ - if( X->s != 1 && X->s != -1 ) - return( 0 ); // invalid sign bit, e.g. 0 - if( mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 ) - return( 0 ); // negative zero - return( 1 ); -} - /* END_HEADER */ /* BEGIN_DEPENDENCIES @@ -51,50 +38,3 @@ exit: #undef MLIMBS } /* END_CASE */ - - -/* BEGIN_CASE */ -void mpi_mod_mpi( char * input_X, char * input_Y, - char * input_A, int div_result ) -{ - mbedtls_mpi X, Y, A; - int res; - mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); - - TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); - TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); - TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); - res = mbedtls_mpi_mod_mpi( &X, &X, &Y ); - TEST_ASSERT( res == div_result ); - if( res == 0 ) - { - TEST_ASSERT( sign_is_valid( &X ) ); - TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); - } - -exit: - mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); -} -/* END_CASE */ - -/* BEGIN_CASE */ -void mpi_mod_int( char * input_X, int input_Y, - int input_A, int div_result ) -{ - mbedtls_mpi X; - int res; - mbedtls_mpi_uint r; - mbedtls_mpi_init( &X ); - - TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); - res = mbedtls_mpi_mod_int( &r, &X, input_Y ); - TEST_ASSERT( res == div_result ); - if( res == 0 ) - { - TEST_ASSERT( r == (mbedtls_mpi_uint) input_A ); - } - -exit: - mbedtls_mpi_free( &X ); -} -/* END_CASE */ diff --git a/tests/suites/test_suite_mpi.data b/tests/suites/test_suite_mpi.data index 93279a658..794031901 100644 --- a/tests/suites/test_suite_mpi.data +++ b/tests/suites/test_suite_mpi.data @@ -1258,6 +1258,66 @@ mpi_div_int:"00":0:"":"":MBEDTLS_ERR_MPI_DIVISION_BY_ZERO Test mbedtls_mpi_div_int: 0 (null) / 1 mpi_div_int:"":1:"":"":0 +Base test mbedtls_mpi_mod_mpi #1 +mpi_mod_mpi:"3e8":"d":"c":0 + +Base test mbedtls_mpi_mod_mpi #2 (Divide by zero (null)) +mpi_mod_mpi:"3e8":"":"0":MBEDTLS_ERR_MPI_DIVISION_BY_ZERO + +Base test mbedtls_mpi_mod_mpi #2 (Divide by zero (1 limb)) +mpi_mod_mpi:"3e8":"0":"0":MBEDTLS_ERR_MPI_DIVISION_BY_ZERO + +Base test mbedtls_mpi_mod_mpi #3 +mpi_mod_mpi:"-3e8":"d":"1":0 + +Base test mbedtls_mpi_mod_mpi #4 (Negative modulo) +mpi_mod_mpi:"3e8":"-d":"-1":MBEDTLS_ERR_MPI_NEGATIVE_VALUE + +Base test mbedtls_mpi_mod_mpi #5 (Negative modulo) +mpi_mod_mpi:"-3e8":"-d":"-c":MBEDTLS_ERR_MPI_NEGATIVE_VALUE + +Test mbedtls_mpi_mod_mpi: 0 (null) % 1 +mpi_mod_mpi:"":"1":"":0 + +Test mbedtls_mpi_mod_mpi: 0 (null) % -1 +mpi_mod_mpi:"":"-1":"":MBEDTLS_ERR_MPI_NEGATIVE_VALUE + +Base test mbedtls_mpi_mod_int #1 +mpi_mod_int:"3e8":13:12:0 + +Base test mbedtls_mpi_mod_int #2 (Divide by zero) +mpi_mod_int:"3e8":0:0:MBEDTLS_ERR_MPI_DIVISION_BY_ZERO + +Base test mbedtls_mpi_mod_int #3 +mpi_mod_int:"-3e8":13:1:0 + +Base test mbedtls_mpi_mod_int #4 (Negative modulo) +mpi_mod_int:"3e8":-13:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE + +Base test mbedtls_mpi_mod_int #5 (Negative modulo) +mpi_mod_int:"-3e8":-13:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE + +Base test mbedtls_mpi_mod_int #6 (By 1) +mpi_mod_int:"3e8":1:0:0 + +Base test mbedtls_mpi_mod_int #7 (By 2) +mpi_mod_int:"3e9":2:1:0 + +Base test mbedtls_mpi_mod_int #8 (By 2) +mpi_mod_int:"3e8":2:0:0 + +Test mbedtls_mpi_mod_int: 0 (null) % 1 +mpi_mod_int:"":1:0:0 + +Test mbedtls_mpi_mod_int: 0 (null) % 2 +mpi_mod_int:"":2:0:0 + +Test mbedtls_mpi_mod_int: 0 (null) % -1 +mpi_mod_int:"":-1:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE + +Test mbedtls_mpi_mod_int: 0 (null) % -2 +mpi_mod_int:"":-2:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE + Base test mbedtls_mpi_exp_mod #1 mpi_exp_mod:"17":"d":"1d":"18":0 diff --git a/tests/suites/test_suite_mpi.function b/tests/suites/test_suite_mpi.function index 4e82a4b47..db274100a 100644 --- a/tests/suites/test_suite_mpi.function +++ b/tests/suites/test_suite_mpi.function @@ -1244,6 +1244,52 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void mpi_mod_mpi( char * input_X, char * input_Y, + char * input_A, int div_result ) +{ + mbedtls_mpi X, Y, A; + int res; + mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); + + TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); + TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); + TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); + res = mbedtls_mpi_mod_mpi( &X, &X, &Y ); + TEST_ASSERT( res == div_result ); + if( res == 0 ) + { + TEST_ASSERT( sign_is_valid( &X ) ); + TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); + } + +exit: + mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void mpi_mod_int( char * input_X, int input_Y, + int input_A, int div_result ) +{ + mbedtls_mpi X; + int res; + mbedtls_mpi_uint r; + mbedtls_mpi_init( &X ); + + TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); + res = mbedtls_mpi_mod_int( &r, &X, input_Y ); + TEST_ASSERT( res == div_result ); + if( res == 0 ) + { + TEST_ASSERT( r == (mbedtls_mpi_uint) input_A ); + } + +exit: + mbedtls_mpi_free( &X ); +} +/* END_CASE */ + /* BEGIN_CASE */ void mpi_exp_mod( char * input_A, char * input_E, char * input_N, char * input_X,