bignum_mod: Adjusted input checking for mbedtls_mpi_mod_residue_setup()

This patch adjusts the logic of the size checking of the method,
and refactors the tests. Documentation has also been updated.

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
This commit is contained in:
Minos Galanakis 2022-11-24 09:09:47 +00:00 committed by Janos Follath
parent a17ad48e2d
commit aed832ac16
4 changed files with 39 additions and 37 deletions

View file

@ -39,7 +39,7 @@ int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
mbedtls_mpi_uint *p, mbedtls_mpi_uint *p,
size_t p_limbs ) size_t p_limbs )
{ {
if( p_limbs > m->limbs || !mbedtls_mpi_core_lt_ct( p, m->p, m->limbs ) ) if( p_limbs > m->limbs || !mbedtls_mpi_core_lt_ct( p, m->p, p_limbs ) )
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
r->limbs = m->limbs; r->limbs = m->limbs;

View file

@ -75,16 +75,17 @@ typedef struct {
/** Setup a residue structure. /** Setup a residue structure.
* *
* \param[out] r The address of residue to setup. The size is determined by * \param[out] r The address of residue to setup. The resulting structure's
* \p m. * size is determined by \p m.
* (In particular, it must have at least as many limbs as the
* modulus \p m.)
* \param[in] m The address of the modulus related to \p r. * \param[in] m The address of the modulus related to \p r.
* \param[in] p The address of the limb array storing the value of \p r. * \param[in] p The address of the limb array storing the value of \p r.
* The memory pointed to by \p p will be used by \p r and must * The memory pointed to by \p p will be used by \p r and must
* not be modified in any way until after * not be modified in any way until after
* mbedtls_mpi_mod_residue_release() is called. * mbedtls_mpi_mod_residue_release() is called. The data
* \param p_limbs The number of limbs of \p p. * pointed by p should be compatible (in terms of size/endianness)
* with the representation used in \p m.
* \param p_limbs The number of limbs of \p p. It must have at most as
* many limbs as the modulus \p m.)
* *
* \return \c 0 if successful. * \return \c 0 if successful.
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the

View file

@ -50,8 +50,29 @@ mpi_mod_setup:MBEDTLS_MPI_MOD_EXT_REP_LE:MBEDTLS_MPI_MOD_REP_MONTGOMERY:0
# END MERGE SLOT 6 # END MERGE SLOT 6
# BEGIN MERGE SLOT 7 # BEGIN MERGE SLOT 7
Test mbedtls_mpi_residue_setup Test mbedtls_mpi_residue_setup #1 m > r
mpi_residue_setup: mpi_residue_setup:"fe":"01":0
Test mbedtls_mpi_residue_setup #2 r == m - 1
mpi_residue_setup:"ff":"fe":0
Test mbedtls_mpi_residue_setup #3 m->limbs > r-> limbs && m > r
mpi_residue_setup:"000000000000000000000000000000007dfe5c6beb35a2d6":"fe":0
Test mbedtls_mpi_residue_setup #4 m->limbs > r-> limbs && m > r
mpi_residue_setup:"7ffffffffffffffffffffffffffffffffffffffffffffff1":"fe":0
Test mbedtls_mpi_residue_setup #5 m->limbs > r-> limbs && m > r
mpi_residue_setup:"7ffffffffffffffffffff000000000000000000000000000":"fe":-4
Test mbedtls_mpi_residue_setup #6 m->limbs < r-> limbs && m > r
mpi_residue_setup:"ff":"000000000000000000000000000000000000000000000001":-4
Test mbedtls_mpi_residue_setup #7 r == m
mpi_residue_setup:"fe":"fe":-4
Test mbedtls_mpi_residue_setup #8 r > m
mpi_residue_setup:"fe":"ff":-4
Test mbedtls_mpi_mod_io_neg Test mbedtls_mpi_mod_io_neg
mpi_mod_io_neg: mpi_mod_io_neg:

View file

@ -81,52 +81,32 @@ exit:
/* BEGIN MERGE SLOT 7 */ /* BEGIN MERGE SLOT 7 */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mpi_residue_setup( ) void mpi_residue_setup( char * input_X, char * input_Y, int ret )
{ {
#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 *N = NULL;
mbedtls_mpi_uint *R = NULL; mbedtls_mpi_uint *R = NULL;
mbedtls_mpi_uint *R_MAX = NULL; size_t n_limbs, r_limbs;
size_t n_limbs, r_limbs, r_max_limbs;
mbedtls_mpi_mod_modulus m; mbedtls_mpi_mod_modulus m;
mbedtls_mpi_mod_residue r; 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 ); mbedtls_mpi_mod_modulus_init( &m );
/* Allocate the memory for intermediate data structures */
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_X ) );
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, input_Y ) );
TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
MBEDTLS_MPI_MOD_EXT_REP_LE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); 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_EQUAL( ret, 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: exit:
mbedtls_mpi_mod_modulus_free( &m ); mbedtls_mpi_mod_modulus_free( &m );
mbedtls_free( N ); mbedtls_free( N );
mbedtls_free( R ); mbedtls_free( R );
mbedtls_free( R_MAX );
#undef RS_ONE
#undef RS_MAX_MIN1
#undef RS_MAX
} }
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mpi_mod_io_neg( ) void mpi_mod_io_neg( )
{ {