blob: 8716f679a355c2976d3f7369b35321c11da974e2 [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);
21 mbedtls_mpi_uint * rr;
Werner Lewis0c6ea122022-09-30 13:02:16 +010022 int ret;
23
Minos Galanakisdd365a52022-10-19 01:48:32 +010024 memset( mp, 0xFF, mp_size );
Werner Lewis0c6ea122022-09-30 13:02:16 +010025
26 mbedtls_mpi_mod_modulus_init( &m );
27 ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, ext_rep, int_rep );
28 TEST_EQUAL( ret, iret );
29
Minos Galanakisdd365a52022-10-19 01:48:32 +010030 /* Only test if the constants have been set-up */
31 if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
32 {
33 /* Test that the consts have been calculated */
34 TEST_ASSERT( m.rep.mont.rr != NULL );
35 TEST_ASSERT( m.rep.mont.mm != 0 );
36
37 /* Keep a copy of the memory location used to store Montgomery const */
38 rr = (mbedtls_mpi_uint *)m.rep.mont.rr;
39 }
40
Werner Lewis0c6ea122022-09-30 13:02:16 +010041 /* Address sanitiser should catch if we try to free mp */
42 mbedtls_mpi_mod_modulus_free( &m );
43
44 /* Make sure that the modulus doesn't have reference to mp anymore */
45 TEST_ASSERT( m.p != mp );
46
Minos Galanakisdd365a52022-10-19 01:48:32 +010047 /* Only test if the constants have been set-up */
48 if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
49 {
50 /* Reuse the allocated buffer for a zeroization check */
51 memset( mp, 0x00, mp_size );
52
53 /* Verify the data and pointers allocated have been properly wiped */
54 TEST_ASSERT( m.rep.mont.rr == NULL );
55 TEST_ASSERT( m.rep.mont.mm == 0 );
56
57 /* mbedtls_mpi_mod_modulus_free() has set the
58 * (mbedtls_mpi_uint *)m.rep.mont.rr -> NULL.
59 * Verify that the actual data have been zeroed */
60 ASSERT_COMPARE( rr, mp_size, &mp, mp_size );
61 }
Werner Lewis0c6ea122022-09-30 13:02:16 +010062exit:
63 /* It should be safe to call an mbedtls free several times */
64 mbedtls_mpi_mod_modulus_free( &m );
65
66 #undef MLIMBS
67}
68/* END_CASE */