From a17ad48e2d139d171dfe3bb59703b41eb869b18b Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Wed, 16 Nov 2022 16:29:15 +0000 Subject: [PATCH] bignum_mod: Fixed an issue with input checking in `mpi_mod_residue_setup` This patch is inverting the input type checking logic in the method, in order to ensure that residue < modulus. Signed-off-by: Minos Galanakis --- library/bignum_mod.c | 2 +- tests/suites/test_suite_bignum_mod.data | 4 +- tests/suites/test_suite_bignum_mod.function | 47 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/library/bignum_mod.c b/library/bignum_mod.c index a4ed32b6a..6c13b4b22 100644 --- a/library/bignum_mod.c +++ b/library/bignum_mod.c @@ -39,7 +39,7 @@ int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r, mbedtls_mpi_uint *p, size_t p_limbs ) { - if( p_limbs < m->limbs || !mbedtls_mpi_core_lt_ct( m->p, p, p_limbs ) ) + if( p_limbs > m->limbs || !mbedtls_mpi_core_lt_ct( p, m->p, m->limbs ) ) return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); r->limbs = m->limbs; diff --git a/tests/suites/test_suite_bignum_mod.data b/tests/suites/test_suite_bignum_mod.data index 6b25c49ee..5edb283ae 100644 --- a/tests/suites/test_suite_bignum_mod.data +++ b/tests/suites/test_suite_bignum_mod.data @@ -50,6 +50,9 @@ mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_LE:MBEDTLS_MPI_MOD_REP_MONTGOMERY:0 # END MERGE SLOT 6 # BEGIN MERGE SLOT 7 +Test mbedtls_mpi_residue_setup +mpi_residue_setup: + Test mbedtls_mpi_mod_io_neg mpi_mod_io_neg: @@ -280,4 +283,3 @@ mpi_mod_io:"ffcece570f2f991013f26dd5b03c4c5b65f97be5905f36cb4664f2c78ff80aa8135a Test mbedtls_mpi_mod_io #76 N: "32292747613635961694771916499883650667878589411552643628627186850993060141490368296439843252993342320145797691611646027435006878234727648863911408777308953382400333083852585109256846643097239747078406546553406955958288616728627292699264194880486908744773379992784153004816057528456043920098334713005039494478693892693017304730883448003944721685094014669042959451482141781404822386404101555113742346277194830729517252154824958327000717338180410404929239489607893939166712107274943411892079802406181464789204374234653633818543559183821503846194953493439237710780169796543565449952151334229364816621060143650318299210551" A: "136". mpi_mod_io:"ffcece570f2f991013f26dd5b03c4c5b65f97be5905f36cb4664f2c78ff80aa8135a4aaf57ccb8a0aca2f394909a74cef1ef6758a64d11e2c149c393659d124bfc94196f0ce88f7d7d567efa5a649e2deefaa6e10fdc3deac60d606bf63fc540ac95294347031aefd73d6a9ee10188aaeb7a90d920894553cb196881691cadc51808715a07e8b24fcb1a63df047c7cdf084dd177ba368c806f3d51ddb5d3898c863e687ecaf7d649a57a46264a582f94d3c8f2edaf59f77a7f6bdaf83c991e8f06abe220ec8507386fce8c3da84c6c3903ab8f3ad4630a204196a7dbcbd9bcca4e40ec5cc5c09938d49f5e1e6181db8896f33bb12e6ef73f12ec5c5ea7a8a337":"27d6b9ef85b7ecf293f6cb59a3b9ac5d72d1eb19c3fb6b7e8a1332ea2cfe352bb9cdd5f3cf35594a16fbe34fd726d96c372c27e174bda39d4a9e157d6de2fc7976af965c63ecd8c94e852ea3e68dcaf1c2752567a36f9dc5659dc9e2c1476c3a24a3a439b87c6e4c4dafdce56b1712b9e39d685bf21ccf87b2cf796c92e49b8d35aba846a87d2ad7a1dedc13c52a152336d4e9a386a2d275ae41ea5bb375d1a346317a822bdec9d1bcda63cb512a854c4fea437dc5f141732eada619ce2561cfa542524aa897d1a9de7a687f4875a679289cb5c4bfecc6d4deb64ea818eb86aff4be4ceac23aab95a3b45a41c6f2c82938db561fb15d3418a5488e1a2a5f41b6":MBEDTLS_MPI_MOD_EXT_REP_LE - diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index 00e830670..5a2d000ca 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -81,6 +81,53 @@ exit: /* BEGIN MERGE SLOT 7 */ /* BEGIN_CASE */ +void mpi_residue_setup( ) +{ + #define RS_ONE 0 + #define RS_MAX_MIN1 1 + #define RS_MAX 2 + const char * s_data[ 3 ] = { "01", "fe", "ff" }; + + mbedtls_mpi_uint *N = NULL; + mbedtls_mpi_uint *R = NULL; + mbedtls_mpi_uint *R_MAX = NULL; + size_t n_limbs, r_limbs, r_max_limbs; + mbedtls_mpi_mod_modulus m; + mbedtls_mpi_mod_residue r; + + /* Allocate the memory for intermediate data structures */ + TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, s_data[ RS_MAX_MIN1 ] ) ); + TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, s_data[ RS_ONE ] ) ); + TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R_MAX, &r_max_limbs, s_data[ RS_MAX ] ) ); + + mbedtls_mpi_mod_modulus_init( &m ); + + TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, + MBEDTLS_MPI_MOD_EXT_REP_LE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); + + TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , r_limbs ) ); + + /* Test for r-> limbs > m-> limbs */ + TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_residue_setup( &r, &m, R , r_limbs + 1 ) ); + + /* Test for r-> p > m-> p */ + TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_residue_setup( &r, &m, R_MAX , r_max_limbs ) ); + + /* Test for r-> p == m-> p */ + TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_residue_setup( &r, &m, N , r_max_limbs ) ); + +exit: + mbedtls_mpi_mod_modulus_free( &m ); + mbedtls_free( N ); + mbedtls_free( R ); + mbedtls_free( R_MAX ); + + #undef RS_ONE + #undef RS_MAX_MIN1 + #undef RS_MAX +} +/* END_CASE */ +/* BEGIN_CASE */ void mpi_mod_io_neg( ) { #define IO_ZERO 0