blob: 00e830670175ad8ca18f571336bc76dfe9a3dc1d [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 */
Janos Follath5933f692022-11-02 14:35:17 +000057
58/* BEGIN MERGE SLOT 1 */
59
60/* END MERGE SLOT 1 */
61
62/* BEGIN MERGE SLOT 2 */
63
64/* END MERGE SLOT 2 */
65
66/* BEGIN MERGE SLOT 3 */
67
68/* END MERGE SLOT 3 */
69
70/* BEGIN MERGE SLOT 4 */
71
72/* END MERGE SLOT 4 */
73
74/* BEGIN MERGE SLOT 5 */
75
76/* END MERGE SLOT 5 */
77
78/* BEGIN MERGE SLOT 6 */
79
80/* END MERGE SLOT 6 */
81
82/* BEGIN MERGE SLOT 7 */
Minos Galanakis8f242702022-11-10 16:56:02 +000083/* BEGIN_CASE */
84void mpi_mod_io_neg( )
85{
86 #define IO_ZERO 0
87 #define IO_ONE 1
88 #define IO_MIN1 2
89 #define IO_MAX 3
90 #define IO_2LIMBS_MIN1 4
91 #define IO_2LIMBS 5
Janos Follath5933f692022-11-02 14:35:17 +000092
Minos Galanakis8f242702022-11-10 16:56:02 +000093 mbedtls_mpi_uint *N = NULL;
94 mbedtls_mpi_uint *R = NULL;
95 mbedtls_mpi_uint *N2 = NULL;
96 mbedtls_mpi_uint *R2 = NULL;
97 unsigned char *r_buff = NULL;
98
99 size_t n_limbs, r_limbs, n2_limbs, r2_limbs;
100
101 mbedtls_mpi_mod_modulus m;
102 mbedtls_mpi_mod_residue r;
103 mbedtls_mpi_mod_modulus m2;
104 mbedtls_mpi_mod_residue rn = { NULL, 0 };
105
106 const char * s_data[ 6 ] = { "00", "01", "fe", "ff",
107 "7ffffffffffffffff0" ,"7ffffffffffffffff1" };
108 const size_t buff_bytes = 1024;
109
110 /* Allocate the memory for intermediate data structures */
111 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, s_data[ IO_MIN1 ] ) );
112 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, s_data[ IO_ONE ] ) );
113 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N2, &n2_limbs, s_data[ IO_2LIMBS ] ) );
114 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R2, &r2_limbs, s_data[ IO_2LIMBS_MIN1 ] ) );
115
116 mbedtls_mpi_mod_modulus_init( &m );
117 mbedtls_mpi_mod_modulus_init( &m2 );
118
119 /* Allocate more than required space on buffer so we can test for input_r > mpi */
120 ASSERT_ALLOC( r_buff, buff_bytes );
121 memset( r_buff, 0x1, 1 );
122
123 TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
124 MBEDTLS_MPI_MOD_EXT_REP_LE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
125
126 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , n_limbs ) );
127
128 /* Pass for input_r < modulo */
129 TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
130
131 /* input_r == modulo -1 */
132 memset( r_buff, 0xfd, buff_bytes );
133 TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
134
135 /* modulo->p == NULL || residue->p == NULL ( m2 has not been set-up ) */
136 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m2, r_buff, 1 ) );
137 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &rn, &m, r_buff, 1 ) );
138 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_write( &r, &m2, r_buff, 1 ) );
139 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_write( &rn, &m, r_buff, 1 ) );
140
141 /* Fail for r_limbs < m->limbs */
142 r.limbs = m.limbs - 1;
143 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
144 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_write( &rn, &m, r_buff, 1 ) );
145 r.limbs = r_limbs;
146
147 /* Fail if input_r >= modulo m */
148 /* input_r = modulo */
149 memset( r_buff, 0xfe, buff_bytes );
150 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
151
152 /* input_r > modulo */
153 memset( r_buff, 0xff, buff_bytes );
154 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
155
156 /* Data too large to fit */
157 TEST_EQUAL(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL, mbedtls_mpi_mod_read( &r, &m, r_buff, buff_bytes ) );
158
159 /* Read the two limbs input data into a larger modulus and residue */
160 TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m2, N2, n2_limbs,
161 MBEDTLS_MPI_MOD_EXT_REP_LE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
162 rn.p = R2;
163 rn.limbs = r2_limbs;
164 TEST_EQUAL(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL, mbedtls_mpi_mod_write( &rn, &m2, r_buff, 1 ) );
165
166exit:
167 mbedtls_mpi_mod_modulus_free( &m );
168 mbedtls_mpi_mod_modulus_free( &m2 );
169 mbedtls_free( N );
170 mbedtls_free( R );
171 mbedtls_free( N2 );
172 mbedtls_free( R2 );
173 mbedtls_free( r_buff );
174
175 #undef IO_ZERO
176 #undef IO_ONE
177 #undef IO_MIN1
178 #undef IO_MAX
179 #undef IO_2LIMBS_MIN1
180 #undef IO_2LIMBS
181}
182/* END_CASE */
183
184/* BEGIN_CASE */
185void mpi_mod_io( char * input_N, data_t * input_A, int iendian )
186{
187 mbedtls_mpi_uint *N = NULL;
188 mbedtls_mpi_uint *R = NULL;
189 unsigned char *r_buff = NULL;
190 mbedtls_mpi_mod_modulus m;
191 mbedtls_mpi_mod_residue r;
192 size_t n_limbs, n_bytes, a_bytes;
193
194 /* Read inputs */
195 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
196 n_bytes = n_limbs * sizeof( mbedtls_mpi_uint );
197 a_bytes = input_A->len * sizeof( char );
198
199 /* Allocate the memory for intermediate data structures */
200 ASSERT_ALLOC( R, n_bytes );
201 ASSERT_ALLOC( r_buff, a_bytes );
202
203 /* Test that input's size is not greater to modulo's */
204 TEST_LE_U(a_bytes, n_bytes );
205
206 /* Init Structures */
207 mbedtls_mpi_mod_modulus_init( &m );
208 TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, iendian,
209 MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
210
211 /* Enforcing p_limbs >= m->limbs */
212 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , n_limbs ) );
213
214 TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, input_A->x, input_A->len ) );
215
216 TEST_EQUAL( 0,mbedtls_mpi_mod_write( &r, &m, r_buff, a_bytes ) );
217
218 ASSERT_COMPARE( r_buff, a_bytes, input_A->x, a_bytes );
219exit:
220 mbedtls_mpi_mod_modulus_free( &m );
221 mbedtls_free( N );
222 mbedtls_free( R );
223 mbedtls_free( r_buff );
224}
225/* END_CASE */
Janos Follath5933f692022-11-02 14:35:17 +0000226/* END MERGE SLOT 7 */
227
228/* BEGIN MERGE SLOT 8 */
229
230/* END MERGE SLOT 8 */
231
232/* BEGIN MERGE SLOT 9 */
233
234/* END MERGE SLOT 9 */
235
236/* BEGIN MERGE SLOT 10 */
237
238/* END MERGE SLOT 10 */