blob: 2e0377a703c054fdd3ef1231bf3daada396232e1 [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;
Minos Galanakisdd365a52022-10-19 01:48:32 +010020 const size_t mp_size = sizeof(mbedtls_mpi_uint);
Werner Lewis0c6ea122022-09-30 13:02:16 +010021 int ret;
22
Minos Galanakisdd365a52022-10-19 01:48:32 +010023 memset( mp, 0xFF, mp_size );
Werner Lewis0c6ea122022-09-30 13:02:16 +010024
25 mbedtls_mpi_mod_modulus_init( &m );
26 ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, ext_rep, int_rep );
27 TEST_EQUAL( ret, iret );
28
Minos Galanakisdd365a52022-10-19 01:48:32 +010029 /* Only test if the constants have been set-up */
30 if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
31 {
32 /* Test that the consts have been calculated */
33 TEST_ASSERT( m.rep.mont.rr != NULL );
34 TEST_ASSERT( m.rep.mont.mm != 0 );
35
Minos Galanakisdd365a52022-10-19 01:48:32 +010036 }
37
Werner Lewis0c6ea122022-09-30 13:02:16 +010038 /* Address sanitiser should catch if we try to free mp */
39 mbedtls_mpi_mod_modulus_free( &m );
40
41 /* Make sure that the modulus doesn't have reference to mp anymore */
42 TEST_ASSERT( m.p != mp );
43
Minos Galanakisdd365a52022-10-19 01:48:32 +010044 /* Only test if the constants have been set-up */
45 if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
46 {
47 /* Reuse the allocated buffer for a zeroization check */
48 memset( mp, 0x00, mp_size );
49
50 /* Verify the data and pointers allocated have been properly wiped */
51 TEST_ASSERT( m.rep.mont.rr == NULL );
52 TEST_ASSERT( m.rep.mont.mm == 0 );
53
Minos Galanakisdd365a52022-10-19 01:48:32 +010054 }
Werner Lewis0c6ea122022-09-30 13:02:16 +010055exit:
56 /* It should be safe to call an mbedtls free several times */
57 mbedtls_mpi_mod_modulus_free( &m );
58
59 #undef MLIMBS
60}
61/* END_CASE */