blob: 715a839988e356e0ffdf72ba0dc7fa663b6a3b8d [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 */
Minos Galanakisaed832a2022-11-24 09:09:47 +000084void mpi_residue_setup( char * input_X, char * input_Y, int ret )
Minos Galanakisa17ad482022-11-16 16:29:15 +000085{
Minos Galanakisa17ad482022-11-16 16:29:15 +000086 mbedtls_mpi_uint *N = NULL;
87 mbedtls_mpi_uint *R = NULL;
Minos Galanakisaed832a2022-11-24 09:09:47 +000088 size_t n_limbs, r_limbs;
Minos Galanakisa17ad482022-11-16 16:29:15 +000089 mbedtls_mpi_mod_modulus m;
90 mbedtls_mpi_mod_residue r;
91
Minos Galanakisa17ad482022-11-16 16:29:15 +000092 mbedtls_mpi_mod_modulus_init( &m );
93
Minos Galanakisaed832a2022-11-24 09:09:47 +000094 /* Allocate the memory for intermediate data structures */
95 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_X ) );
96 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, input_Y ) );
97
Minos Galanakisa17ad482022-11-16 16:29:15 +000098 TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
99 MBEDTLS_MPI_MOD_EXT_REP_LE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
100
Minos Galanakisaed832a2022-11-24 09:09:47 +0000101 TEST_EQUAL( ret, mbedtls_mpi_mod_residue_setup( &r, &m, R , r_limbs ) );
Minos Galanakisa17ad482022-11-16 16:29:15 +0000102
103exit:
104 mbedtls_mpi_mod_modulus_free( &m );
105 mbedtls_free( N );
106 mbedtls_free( R );
Minos Galanakisa17ad482022-11-16 16:29:15 +0000107}
108/* END_CASE */
Minos Galanakisaed832a2022-11-24 09:09:47 +0000109
Minos Galanakisa17ad482022-11-16 16:29:15 +0000110/* BEGIN_CASE */
Minos Galanakis8f242702022-11-10 16:56:02 +0000111void mpi_mod_io_neg( )
112{
Minos Galanakis8f242702022-11-10 16:56:02 +0000113 mbedtls_mpi_uint *N = NULL;
114 mbedtls_mpi_uint *R = NULL;
115 mbedtls_mpi_uint *N2 = NULL;
116 mbedtls_mpi_uint *R2 = NULL;
117 unsigned char *r_buff = NULL;
118
119 size_t n_limbs, r_limbs, n2_limbs, r2_limbs;
120
121 mbedtls_mpi_mod_modulus m;
122 mbedtls_mpi_mod_residue r;
123 mbedtls_mpi_mod_modulus m2;
124 mbedtls_mpi_mod_residue rn = { NULL, 0 };
125
Minos Galanakis8b375452022-11-24 11:04:11 +0000126 const char *hex_residue_single = "01";
127 const char *hex_modulus_single = "fe";
128 const char *hex_residue_multi = "7ffffffffffffffffffffffffffffff0";
129 const char *hex_modulus_multi = "7ffffffffffffffffffffffffffffff1";
130
Minos Galanakis8f242702022-11-10 16:56:02 +0000131 const size_t buff_bytes = 1024;
132
133 /* Allocate the memory for intermediate data structures */
Minos Galanakis8b375452022-11-24 11:04:11 +0000134 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, hex_modulus_single ) );
135 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, hex_residue_single ) );
136 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N2, &n2_limbs, hex_modulus_multi ) );
137 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R2, &r2_limbs, hex_residue_multi ) );
Minos Galanakis8f242702022-11-10 16:56:02 +0000138
139 mbedtls_mpi_mod_modulus_init( &m );
140 mbedtls_mpi_mod_modulus_init( &m2 );
141
142 /* Allocate more than required space on buffer so we can test for input_r > mpi */
143 ASSERT_ALLOC( r_buff, buff_bytes );
144 memset( r_buff, 0x1, 1 );
145
146 TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
147 MBEDTLS_MPI_MOD_EXT_REP_LE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
148
149 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , n_limbs ) );
150
151 /* Pass for input_r < modulo */
152 TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
153
Minos Galanakis8b375452022-11-24 11:04:11 +0000154 /* Pass for input_r == modulo -1 */
Minos Galanakis8f242702022-11-10 16:56:02 +0000155 memset( r_buff, 0xfd, buff_bytes );
156 TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
157
158 /* modulo->p == NULL || residue->p == NULL ( m2 has not been set-up ) */
159 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m2, r_buff, 1 ) );
160 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &rn, &m, r_buff, 1 ) );
161 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_write( &r, &m2, r_buff, 1 ) );
162 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_write( &rn, &m, r_buff, 1 ) );
163
164 /* Fail for r_limbs < m->limbs */
165 r.limbs = m.limbs - 1;
166 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
167 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_write( &rn, &m, r_buff, 1 ) );
168 r.limbs = r_limbs;
169
170 /* Fail if input_r >= modulo m */
171 /* input_r = modulo */
172 memset( r_buff, 0xfe, buff_bytes );
173 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
174
175 /* input_r > modulo */
176 memset( r_buff, 0xff, buff_bytes );
177 TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_read( &r, &m, r_buff, 1 ) );
178
179 /* Data too large to fit */
180 TEST_EQUAL(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL, mbedtls_mpi_mod_read( &r, &m, r_buff, buff_bytes ) );
181
182 /* Read the two limbs input data into a larger modulus and residue */
183 TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m2, N2, n2_limbs,
184 MBEDTLS_MPI_MOD_EXT_REP_LE, MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
185 rn.p = R2;
186 rn.limbs = r2_limbs;
187 TEST_EQUAL(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL, mbedtls_mpi_mod_write( &rn, &m2, r_buff, 1 ) );
188
189exit:
190 mbedtls_mpi_mod_modulus_free( &m );
191 mbedtls_mpi_mod_modulus_free( &m2 );
192 mbedtls_free( N );
193 mbedtls_free( R );
194 mbedtls_free( N2 );
195 mbedtls_free( R2 );
196 mbedtls_free( r_buff );
Minos Galanakis8f242702022-11-10 16:56:02 +0000197}
198/* END_CASE */
199
200/* BEGIN_CASE */
201void mpi_mod_io( char * input_N, data_t * input_A, int iendian )
202{
203 mbedtls_mpi_uint *N = NULL;
204 mbedtls_mpi_uint *R = NULL;
205 unsigned char *r_buff = NULL;
206 mbedtls_mpi_mod_modulus m;
207 mbedtls_mpi_mod_residue r;
208 size_t n_limbs, n_bytes, a_bytes;
209
210 /* Read inputs */
211 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
212 n_bytes = n_limbs * sizeof( mbedtls_mpi_uint );
213 a_bytes = input_A->len * sizeof( char );
214
215 /* Allocate the memory for intermediate data structures */
216 ASSERT_ALLOC( R, n_bytes );
217 ASSERT_ALLOC( r_buff, a_bytes );
218
219 /* Test that input's size is not greater to modulo's */
220 TEST_LE_U(a_bytes, n_bytes );
221
222 /* Init Structures */
223 mbedtls_mpi_mod_modulus_init( &m );
224 TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, iendian,
225 MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
226
227 /* Enforcing p_limbs >= m->limbs */
228 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , n_limbs ) );
229
230 TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, input_A->x, input_A->len ) );
231
232 TEST_EQUAL( 0,mbedtls_mpi_mod_write( &r, &m, r_buff, a_bytes ) );
233
234 ASSERT_COMPARE( r_buff, a_bytes, input_A->x, a_bytes );
235exit:
236 mbedtls_mpi_mod_modulus_free( &m );
237 mbedtls_free( N );
238 mbedtls_free( R );
239 mbedtls_free( r_buff );
240}
241/* END_CASE */
Janos Follath5933f692022-11-02 14:35:17 +0000242/* END MERGE SLOT 7 */
243
244/* BEGIN MERGE SLOT 8 */
245
246/* END MERGE SLOT 8 */
247
248/* BEGIN MERGE SLOT 9 */
249
250/* END MERGE SLOT 9 */
251
252/* BEGIN MERGE SLOT 10 */
253
254/* END MERGE SLOT 10 */