mbedtls/tests/suites/test_suite_bignum_mod.function
Minos Galanakis 771c47055f bignum_mod: Style changes
This patch addresses review comments with regards to style of
`mbedtls_mpi_mod_modulus_setup/free()`.

It also removes a test check which was triggering a use-after-free.

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
2022-10-27 12:36:24 +01:00

61 lines
1.7 KiB
C

/* BEGIN_HEADER */
#include "mbedtls/bignum.h"
#include "mbedtls/entropy.h"
#include "bignum_mod.h"
#include "constant_time_internal.h"
#include "test/constant_flow.h"
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_BIGNUM_C
* END_DEPENDENCIES
*/
/* BEGIN_CASE */
void mpi_mod_setup( int ext_rep, int int_rep, int iret )
{
#define MLIMBS 8
mbedtls_mpi_uint mp[MLIMBS];
mbedtls_mpi_mod_modulus m;
const size_t mp_size = sizeof(mbedtls_mpi_uint);
int ret;
memset( mp, 0xFF, mp_size );
mbedtls_mpi_mod_modulus_init( &m );
ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, ext_rep, int_rep );
TEST_EQUAL( ret, iret );
/* Only test if the constants have been set-up */
if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
{
/* Test that the consts have been calculated */
TEST_ASSERT( m.rep.mont.rr != NULL );
TEST_ASSERT( m.rep.mont.mm != 0 );
}
/* Address sanitiser should catch if we try to free mp */
mbedtls_mpi_mod_modulus_free( &m );
/* Make sure that the modulus doesn't have reference to mp anymore */
TEST_ASSERT( m.p != mp );
/* Only test if the constants have been set-up */
if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
{
/* Reuse the allocated buffer for a zeroization check */
memset( mp, 0x00, mp_size );
/* Verify the data and pointers allocated have been properly wiped */
TEST_ASSERT( m.rep.mont.rr == NULL );
TEST_ASSERT( m.rep.mont.mm == 0 );
}
exit:
/* It should be safe to call an mbedtls free several times */
mbedtls_mpi_mod_modulus_free( &m );
#undef MLIMBS
}
/* END_CASE */