blob: ad89bdf3e7a57b0abf76e87a1957cc50b1618b15 [file] [log] [blame]
Werner Lewis0c6ea122022-09-30 13:02:16 +01001/* BEGIN_HEADER */
2#include "mbedtls/bignum.h"
3#include "mbedtls/entropy.h"
4#include "bignum_mod.h"
5#include "constant_time_internal.h"
6#include "test/constant_flow.h"
Werner Lewis0c6ea122022-09-30 13:02:16 +01007/* END_HEADER */
8
9/* BEGIN_DEPENDENCIES
10 * depends_on:MBEDTLS_BIGNUM_C
11 * END_DEPENDENCIES
12 */
13
14/* BEGIN_CASE */
15void mpi_mod_setup( int ext_rep, int int_rep, int iret )
16{
17 #define MLIMBS 8
18 mbedtls_mpi_uint mp[MLIMBS];
19 mbedtls_mpi_mod_modulus m;
20 int ret;
21
Minos Galanakis4d4c98b2022-10-27 15:58:02 +010022 memset( mp, 0xFF, sizeof(mp) );
Werner Lewis0c6ea122022-09-30 13:02:16 +010023
24 mbedtls_mpi_mod_modulus_init( &m );
25 ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, ext_rep, int_rep );
26 TEST_EQUAL( ret, iret );
27
Minos Galanakisdd365a52022-10-19 01:48:32 +010028 /* Only test if the constants have been set-up */
29 if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
30 {
31 /* Test that the consts have been calculated */
32 TEST_ASSERT( m.rep.mont.rr != NULL );
33 TEST_ASSERT( m.rep.mont.mm != 0 );
34
Minos Galanakisdd365a52022-10-19 01:48:32 +010035 }
36
Werner Lewis0c6ea122022-09-30 13:02:16 +010037 /* Address sanitiser should catch if we try to free mp */
38 mbedtls_mpi_mod_modulus_free( &m );
39
40 /* Make sure that the modulus doesn't have reference to mp anymore */
41 TEST_ASSERT( m.p != mp );
42
Minos Galanakisdd365a52022-10-19 01:48:32 +010043 /* Only test if the constants have been set-up */
44 if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
45 {
Minos Galanakisdd365a52022-10-19 01:48:32 +010046 /* Verify the data and pointers allocated have been properly wiped */
47 TEST_ASSERT( m.rep.mont.rr == NULL );
48 TEST_ASSERT( m.rep.mont.mm == 0 );
Minos Galanakisdd365a52022-10-19 01:48:32 +010049 }
Werner Lewis0c6ea122022-09-30 13:02:16 +010050exit:
51 /* It should be safe to call an mbedtls free several times */
52 mbedtls_mpi_mod_modulus_free( &m );
53
54 #undef MLIMBS
55}
56/* END_CASE */