blob: 79f5134a9080c1f261f1c8f03e830c0065feea0a [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"
Tom Cosgrove62b20482022-12-01 14:27:37 +00007
8#define TEST_COMPARE_MPI_RESIDUES( a, b ) \
9 ASSERT_COMPARE( (a).p, (a).limbs * sizeof(mbedtls_mpi_uint), \
10 (b).p, (b).limbs * sizeof(mbedtls_mpi_uint) )
11
Tom Cosgrovef51f9722022-12-05 15:47:40 +000012static int test_read_modulus( mbedtls_mpi_mod_modulus *m,
13 mbedtls_mpi_mod_rep_selector int_rep,
14 char *input )
Tom Cosgrove62b20482022-12-01 14:27:37 +000015{
16 mbedtls_mpi_uint *p = NULL;
17 size_t limbs;
18
19 int ret = mbedtls_test_read_mpi_core( &p, &limbs, input );
20 if( ret != 0 )
21 return( ret );
22
23 return( mbedtls_mpi_mod_modulus_setup( m, p, limbs, int_rep ) );
24}
25
Tom Cosgrovef51f9722022-12-05 15:47:40 +000026static int test_read_residue( mbedtls_mpi_mod_residue *r,
27 const mbedtls_mpi_mod_modulus *m,
28 char *input,
29 int skip_limbs_and_value_checks )
Tom Cosgrove62b20482022-12-01 14:27:37 +000030{
31 mbedtls_mpi_uint *p = NULL;
32 size_t limbs;
33
34 int ret = mbedtls_test_read_mpi_core( &p, &limbs, input );
35 if( ret != 0 )
36 return( ret );
37
38 if( skip_limbs_and_value_checks )
39 {
40 r->p = p;
41 r->limbs = limbs;
42 return( 0 );
43 }
44
45 /* mbedtls_mpi_mod_residue_setup() checks limbs, and that value < m */
46 return( mbedtls_mpi_mod_residue_setup( r, m, p, limbs ) );
47}
Werner Lewis0c6ea122022-09-30 13:02:16 +010048/* END_HEADER */
49
50/* BEGIN_DEPENDENCIES
51 * depends_on:MBEDTLS_BIGNUM_C
52 * END_DEPENDENCIES
53 */
54
55/* BEGIN_CASE */
Janos Follath91295d22022-11-24 18:20:26 +000056void mpi_mod_setup( int int_rep, int iret )
Werner Lewis0c6ea122022-09-30 13:02:16 +010057{
58 #define MLIMBS 8
59 mbedtls_mpi_uint mp[MLIMBS];
60 mbedtls_mpi_mod_modulus m;
61 int ret;
62
Minos Galanakis4d4c98b2022-10-27 15:58:02 +010063 memset( mp, 0xFF, sizeof(mp) );
Werner Lewis0c6ea122022-09-30 13:02:16 +010064
65 mbedtls_mpi_mod_modulus_init( &m );
Janos Follath91295d22022-11-24 18:20:26 +000066 ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, int_rep );
Werner Lewis0c6ea122022-09-30 13:02:16 +010067 TEST_EQUAL( ret, iret );
68
Minos Galanakisdd365a52022-10-19 01:48:32 +010069 /* Only test if the constants have been set-up */
70 if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
71 {
72 /* Test that the consts have been calculated */
73 TEST_ASSERT( m.rep.mont.rr != NULL );
74 TEST_ASSERT( m.rep.mont.mm != 0 );
75
Minos Galanakisdd365a52022-10-19 01:48:32 +010076 }
77
Werner Lewis0c6ea122022-09-30 13:02:16 +010078 /* Address sanitiser should catch if we try to free mp */
79 mbedtls_mpi_mod_modulus_free( &m );
80
81 /* Make sure that the modulus doesn't have reference to mp anymore */
82 TEST_ASSERT( m.p != mp );
83
Minos Galanakisdd365a52022-10-19 01:48:32 +010084 /* Only test if the constants have been set-up */
85 if ( ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
86 {
Minos Galanakisdd365a52022-10-19 01:48:32 +010087 /* Verify the data and pointers allocated have been properly wiped */
88 TEST_ASSERT( m.rep.mont.rr == NULL );
89 TEST_ASSERT( m.rep.mont.mm == 0 );
Minos Galanakisdd365a52022-10-19 01:48:32 +010090 }
Werner Lewis0c6ea122022-09-30 13:02:16 +010091exit:
92 /* It should be safe to call an mbedtls free several times */
93 mbedtls_mpi_mod_modulus_free( &m );
94
95 #undef MLIMBS
96}
97/* END_CASE */
Janos Follath5933f692022-11-02 14:35:17 +000098
99/* BEGIN MERGE SLOT 1 */
100
101/* END MERGE SLOT 1 */
102
103/* BEGIN MERGE SLOT 2 */
104
105/* END MERGE SLOT 2 */
106
107/* BEGIN MERGE SLOT 3 */
Tom Cosgrove62b20482022-12-01 14:27:37 +0000108/* BEGIN_CASE */
109void mpi_mod_sub( char * input_N,
110 char * input_A, char * input_B,
Tom Cosgrove7f4d15e2022-12-15 10:55:15 +0000111 char * input_D, int expected_ret )
Tom Cosgrove62b20482022-12-01 14:27:37 +0000112{
113 mbedtls_mpi_mod_residue a = { NULL, 0 };
114 mbedtls_mpi_mod_residue b = { NULL, 0 };
115 mbedtls_mpi_mod_residue d = { NULL, 0 };
116 mbedtls_mpi_mod_residue x = { NULL, 0 };
117 mbedtls_mpi_uint *X_raw = NULL;
Janos Follath5933f692022-11-02 14:35:17 +0000118
Tom Cosgrove62b20482022-12-01 14:27:37 +0000119 mbedtls_mpi_mod_modulus m;
120 mbedtls_mpi_mod_modulus_init( &m );
121
122 TEST_EQUAL( 0,
123 test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ) );
124
125 /* test_read_residue() normally checks that inputs have the same number of
126 * limbs as the modulus. For negative testing we can ask it to skip this
127 * with a non-zero final parameter. */
Tom Cosgrove7f4d15e2022-12-15 10:55:15 +0000128 TEST_EQUAL( 0, test_read_residue( &a, &m, input_A, expected_ret != 0 ) );
129 TEST_EQUAL( 0, test_read_residue( &b, &m, input_B, expected_ret != 0 ) );
130 TEST_EQUAL( 0, test_read_residue( &d, &m, input_D, expected_ret != 0 ) );
Tom Cosgrove62b20482022-12-01 14:27:37 +0000131
132 size_t limbs = m.limbs;
133 size_t bytes = limbs * sizeof( *X_raw );
134
Tom Cosgrove7f4d15e2022-12-15 10:55:15 +0000135 if( expected_ret == 0 )
Tom Cosgrove62b20482022-12-01 14:27:37 +0000136 {
Tom Cosgrove7f4d15e2022-12-15 10:55:15 +0000137 /* Negative test with too many limbs in output */
138 ASSERT_ALLOC( X_raw, limbs + 1 );
Tom Cosgrove62b20482022-12-01 14:27:37 +0000139
Tom Cosgrove62b20482022-12-01 14:27:37 +0000140 x.p = X_raw;
141 x.limbs = limbs + 1;
142 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
143 mbedtls_mpi_mod_sub( &x, &a, &b, &m ) );
144
Tom Cosgrove7f4d15e2022-12-15 10:55:15 +0000145 mbedtls_free( X_raw );
146 X_raw = NULL;
147
148 /* Negative test with too few limbs in output */
Tom Cosgrove62b20482022-12-01 14:27:37 +0000149 if( limbs > 1 )
150 {
Tom Cosgrove7f4d15e2022-12-15 10:55:15 +0000151 ASSERT_ALLOC( X_raw, limbs - 1 );
152
Tom Cosgrove62b20482022-12-01 14:27:37 +0000153 x.p = X_raw;
154 x.limbs = limbs - 1;
155 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
156 mbedtls_mpi_mod_sub( &x, &a, &b, &m ) );
Tom Cosgrove7f4d15e2022-12-15 10:55:15 +0000157
158 mbedtls_free( X_raw );
159 X_raw = NULL;
Tom Cosgrove62b20482022-12-01 14:27:37 +0000160 }
161
162 /* Negative testing with too many/too few limbs in a and b is covered by
Tom Cosgrove7f4d15e2022-12-15 10:55:15 +0000163 * manually-written test cases with expected_ret != 0. */
Tom Cosgrove62b20482022-12-01 14:27:37 +0000164 }
165
Tom Cosgrove7f4d15e2022-12-15 10:55:15 +0000166 ASSERT_ALLOC( X_raw, limbs );
167
Tom Cosgrove62b20482022-12-01 14:27:37 +0000168 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &x, &m, X_raw, limbs ) );
169
170 /* a - b => Correct result, or expected error */
Tom Cosgrove7f4d15e2022-12-15 10:55:15 +0000171 TEST_EQUAL( expected_ret, mbedtls_mpi_mod_sub( &x, &a, &b, &m ) );
172 if( expected_ret != 0 )
Tom Cosgrove62b20482022-12-01 14:27:37 +0000173 goto exit;
174
175 TEST_COMPARE_MPI_RESIDUES( x, d );
176
177 /* a - b: alias x to a => Correct result */
178 memcpy( x.p, a.p, bytes );
179 TEST_EQUAL( 0, mbedtls_mpi_mod_sub( &x, &x, &b, &m ) );
180 TEST_COMPARE_MPI_RESIDUES( x, d );
181
182 /* a - b: alias x to b => Correct result */
183 memcpy( x.p, b.p, bytes );
184 TEST_EQUAL( 0, mbedtls_mpi_mod_sub( &x, &a, &x, &m ) );
185 TEST_COMPARE_MPI_RESIDUES( x, d );
186
187 if ( memcmp( a.p, b.p, bytes ) == 0 )
188 {
189 /* a == b: alias a and b */
190
191 /* a - a => Correct result */
192 TEST_EQUAL( 0, mbedtls_mpi_mod_sub( &x, &a, &a, &m ) );
193 TEST_COMPARE_MPI_RESIDUES( x, d );
194
195 /* a - a: x, a, b all aliased together => Correct result */
196 memcpy( x.p, a.p, bytes );
197 TEST_EQUAL( 0, mbedtls_mpi_mod_sub( &x, &x, &x, &m ) );
198 TEST_COMPARE_MPI_RESIDUES( x, d );
199 }
200
201exit:
202 mbedtls_free( (void *)m.p ); /* mbedtls_mpi_mod_modulus_free() sets m.p = NULL */
203 mbedtls_mpi_mod_modulus_free( &m );
204
205 mbedtls_free( a.p );
206 mbedtls_free( b.p );
207 mbedtls_free( d.p );
208 mbedtls_free( X_raw );
209}
210/* END_CASE */
Tom Cosgrovedc197592022-12-15 16:59:40 +0000211
212/* BEGIN_CASE */
213void mpi_mod_inv_mont( char * input_N,
214 char * input_A, char * input_I,
215 int expected_ret )
216{
217 mbedtls_mpi_mod_residue a = { NULL, 0 }; /* argument */
218 mbedtls_mpi_mod_residue i = { NULL, 0 }; /* expected inverse wrt N */
219 mbedtls_mpi_mod_residue x = { NULL, 0 }; /* output */
220 mbedtls_mpi_uint *X_raw = NULL;
221
222 mbedtls_mpi_mod_modulus N;
223 mbedtls_mpi_mod_modulus_init( &N );
224
225 TEST_EQUAL( 0,
226 test_read_modulus( &N, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ) );
227
228 /* test_read_residue() normally checks that inputs have the same number of
229 * limbs as the modulus. For negative testing we can ask it to skip this
230 * with a non-zero final parameter. */
231 TEST_EQUAL( 0, test_read_residue( &a, &N, input_A, expected_ret != 0 ) );
232 TEST_EQUAL( 0, test_read_residue( &i, &N, input_I, expected_ret != 0 ) );
233
234 size_t limbs = N.limbs;
235 size_t bytes = limbs * sizeof( *X_raw );
236
237 ASSERT_ALLOC( X_raw, limbs );
238
239 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &x, &N, X_raw, limbs ) );
240
241 TEST_EQUAL( expected_ret, mbedtls_mpi_mod_inv( &x, &a, &N ) );
242 if( expected_ret == 0 )
243 {
244 TEST_COMPARE_MPI_RESIDUES( x, i );
245
246 /* a^-1: alias x to a => Correct result */
247 memcpy( x.p, a.p, bytes );
248 TEST_EQUAL( 0, mbedtls_mpi_mod_inv( &x, &x, &N ) );
249 TEST_COMPARE_MPI_RESIDUES( x, i );
250 }
251
252exit:
253 mbedtls_free( (void *)N.p ); /* mbedtls_mpi_mod_modulus_free() sets N.p = NULL */
254 mbedtls_mpi_mod_modulus_free( &N );
255
256 mbedtls_free( a.p );
257 mbedtls_free( i.p );
258 mbedtls_free( X_raw );
259}
260/* END_CASE */
261
262/* BEGIN_CASE */
263void mpi_mod_inv_non_mont( char * input_N,
264 char * input_A, char * input_I,
265 int expected_ret )
266{
267 mbedtls_mpi_mod_residue a = { NULL, 0 }; /* argument */
268 mbedtls_mpi_mod_residue i = { NULL, 0 }; /* expected inverse wrt N */
269 mbedtls_mpi_mod_residue x = { NULL, 0 }; /* output */
270 mbedtls_mpi_uint *X_raw = NULL;
271
272 mbedtls_mpi_mod_modulus N;
273 mbedtls_mpi_mod_modulus_init( &N );
274
275 TEST_EQUAL( 0,
276 test_read_modulus( &N, MBEDTLS_MPI_MOD_REP_OPT_RED, input_N ) );
277
278 /* test_read_residue() normally checks that inputs have the same number of
279 * limbs as the modulus. For negative testing we can ask it to skip this
280 * with a non-zero final parameter. */
281 TEST_EQUAL( 0, test_read_residue( &a, &N, input_A, expected_ret != 0 ) );
282 TEST_EQUAL( 0, test_read_residue( &i, &N, input_I, expected_ret != 0 ) );
283
284 size_t limbs = N.limbs;
285 size_t bytes = limbs * sizeof( *X_raw );
286
287 ASSERT_ALLOC( X_raw, limbs );
288
289 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &x, &N, X_raw, limbs ) );
290
291 TEST_EQUAL( expected_ret, mbedtls_mpi_mod_inv( &x, &a, &N ) );
292 if( expected_ret == 0 )
293 {
294 TEST_COMPARE_MPI_RESIDUES( x, i );
295
296 /* a^-1: alias x to a => Correct result */
297 memcpy( x.p, a.p, bytes );
298 TEST_EQUAL( 0, mbedtls_mpi_mod_inv( &x, &x, &N ) );
299 TEST_COMPARE_MPI_RESIDUES( x, i );
300 }
301
302exit:
303 mbedtls_free( (void *)N.p ); /* mbedtls_mpi_mod_modulus_free() sets N.p = NULL */
304 mbedtls_mpi_mod_modulus_free( &N );
305
306 mbedtls_free( a.p );
307 mbedtls_free( i.p );
308 mbedtls_free( X_raw );
309}
310/* END_CASE */
Janos Follath5933f692022-11-02 14:35:17 +0000311/* END MERGE SLOT 3 */
312
313/* BEGIN MERGE SLOT 4 */
314
315/* END MERGE SLOT 4 */
316
317/* BEGIN MERGE SLOT 5 */
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000318/* BEGIN_CASE */
319void mpi_mod_add( char * input_N,
320 char * input_A, char * input_B,
Werner Lewis25690a92022-12-13 17:17:34 +0000321 char * input_S, int expected_ret )
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000322{
323 mbedtls_mpi_mod_residue a = { NULL, 0 };
324 mbedtls_mpi_mod_residue b = { NULL, 0 };
325 mbedtls_mpi_mod_residue s = { NULL, 0 };
326 mbedtls_mpi_mod_residue x = { NULL, 0 };
327 mbedtls_mpi_uint *X_raw = NULL;
Janos Follath5933f692022-11-02 14:35:17 +0000328
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000329 mbedtls_mpi_mod_modulus m;
330 mbedtls_mpi_mod_modulus_init( &m );
331
332 TEST_EQUAL( 0,
333 test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ) );
334
335 /* test_read_residue() normally checks that inputs have the same number of
336 * limbs as the modulus. For negative testing we can ask it to skip this
337 * with a non-zero final parameter. */
Werner Lewis25690a92022-12-13 17:17:34 +0000338 TEST_EQUAL( 0, test_read_residue( &a, &m, input_A, expected_ret != 0 ) );
339 TEST_EQUAL( 0, test_read_residue( &b, &m, input_B, expected_ret != 0 ) );
340 TEST_EQUAL( 0, test_read_residue( &s, &m, input_S, expected_ret != 0 ) );
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000341
342 size_t limbs = m.limbs;
343 size_t bytes = limbs * sizeof( *X_raw );
344
Werner Lewis25690a92022-12-13 17:17:34 +0000345 if( expected_ret == 0 )
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000346 {
347 /* Negative test with too many limbs in output */
Werner Lewis79341a42022-12-13 17:19:01 +0000348 ASSERT_ALLOC( X_raw, limbs + 1 );
349
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000350 x.p = X_raw;
351 x.limbs = limbs + 1;
352 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
353 mbedtls_mpi_mod_add( &x, &a, &b, &m ) );
354
Werner Lewis79341a42022-12-13 17:19:01 +0000355 mbedtls_free( X_raw );
356 X_raw = NULL;
357
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000358 /* Negative test with too few limbs in output */
359 if( limbs > 1 )
360 {
Werner Lewis79341a42022-12-13 17:19:01 +0000361 ASSERT_ALLOC( X_raw, limbs - 1 );
362
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000363 x.p = X_raw;
364 x.limbs = limbs - 1;
365 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
366 mbedtls_mpi_mod_add( &x, &a, &b, &m ) );
Werner Lewis79341a42022-12-13 17:19:01 +0000367
368 mbedtls_free( X_raw );
369 X_raw = NULL;
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000370 }
371
372 /* Negative testing with too many/too few limbs in a and b is covered by
373 * manually-written test cases with oret != 0. */
374 }
375
Werner Lewis79341a42022-12-13 17:19:01 +0000376 /* Allocate correct number of limbs for X_raw */
377 ASSERT_ALLOC( X_raw, limbs );
378
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000379 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &x, &m, X_raw, limbs ) );
380
381 /* A + B => Correct result or expected error */
Werner Lewis25690a92022-12-13 17:17:34 +0000382 TEST_EQUAL( expected_ret, mbedtls_mpi_mod_add( &x, &a, &b, &m ) );
383 if( expected_ret != 0 )
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000384 goto exit;
385
386 TEST_COMPARE_MPI_RESIDUES( x, s );
387
388 /* a + b: alias x to a => Correct result */
389 memcpy( x.p, a.p, bytes );
390 TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &x, &b, &m ) );
391 TEST_COMPARE_MPI_RESIDUES( x, s );
392
393 /* a + b: alias x to b => Correct result */
394 memcpy( x.p, b.p, bytes );
395 TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &a, &x, &m ) );
396 TEST_COMPARE_MPI_RESIDUES( x, s );
397
398 if ( memcmp( a.p, b.p, bytes ) == 0 )
399 {
400 /* a == b: alias a and b */
401
402 /* a + a => Correct result */
403 TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &a, &a, &m ) );
404 TEST_COMPARE_MPI_RESIDUES( x, s );
405
406 /* a + a: x, a, b all aliased together => Correct result */
407 memcpy( x.p, a.p, bytes );
408 TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &x, &x, &m ) );
409 TEST_COMPARE_MPI_RESIDUES( x, s );
410 }
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000411
412exit:
413 mbedtls_free( (void *)m.p ); /* mbedtls_mpi_mod_modulus_free() sets m.p = NULL */
414 mbedtls_mpi_mod_modulus_free( &m );
415
416 mbedtls_free( a.p );
417 mbedtls_free( b.p );
418 mbedtls_free( s.p );
419 mbedtls_free( X_raw );
420}
421/* END_CASE */
Janos Follath5933f692022-11-02 14:35:17 +0000422/* END MERGE SLOT 5 */
423
424/* BEGIN MERGE SLOT 6 */
425
426/* END MERGE SLOT 6 */
427
428/* BEGIN MERGE SLOT 7 */
Minos Galanakis8f242702022-11-10 16:56:02 +0000429/* BEGIN_CASE */
Janos Follath91f3abd2022-11-26 11:47:14 +0000430void mpi_residue_setup( char * input_N, char * input_R, int ret )
Minos Galanakisa17ad482022-11-16 16:29:15 +0000431{
Minos Galanakisa17ad482022-11-16 16:29:15 +0000432 mbedtls_mpi_uint *N = NULL;
433 mbedtls_mpi_uint *R = NULL;
Minos Galanakisaed832a2022-11-24 09:09:47 +0000434 size_t n_limbs, r_limbs;
Minos Galanakisa17ad482022-11-16 16:29:15 +0000435 mbedtls_mpi_mod_modulus m;
436 mbedtls_mpi_mod_residue r;
437
Minos Galanakisa17ad482022-11-16 16:29:15 +0000438 mbedtls_mpi_mod_modulus_init( &m );
439
Minos Galanakisaed832a2022-11-24 09:09:47 +0000440 /* Allocate the memory for intermediate data structures */
Janos Follath91f3abd2022-11-26 11:47:14 +0000441 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
442 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &R, &r_limbs, input_R ) );
Minos Galanakisaed832a2022-11-24 09:09:47 +0000443
Minos Galanakisa17ad482022-11-16 16:29:15 +0000444 TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
Janos Follath91295d22022-11-24 18:20:26 +0000445 MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
Minos Galanakisa17ad482022-11-16 16:29:15 +0000446
Minos Galanakisaed832a2022-11-24 09:09:47 +0000447 TEST_EQUAL( ret, mbedtls_mpi_mod_residue_setup( &r, &m, R , r_limbs ) );
Minos Galanakisa17ad482022-11-16 16:29:15 +0000448
Janos Follath91f3abd2022-11-26 11:47:14 +0000449 if ( ret == 0 )
450 {
451 TEST_EQUAL( r.limbs, r_limbs );
452 TEST_ASSERT( r.p == R );
453 }
454
Minos Galanakisa17ad482022-11-16 16:29:15 +0000455exit:
456 mbedtls_mpi_mod_modulus_free( &m );
457 mbedtls_free( N );
458 mbedtls_free( R );
Minos Galanakisa17ad482022-11-16 16:29:15 +0000459}
460/* END_CASE */
Minos Galanakisaed832a2022-11-24 09:09:47 +0000461
Minos Galanakisa17ad482022-11-16 16:29:15 +0000462/* BEGIN_CASE */
Janos Follath339b4392022-11-26 12:20:41 +0000463void mpi_mod_io_neg( char * input_N, data_t * buf, int ret )
Minos Galanakis8f242702022-11-10 16:56:02 +0000464{
Minos Galanakis8f242702022-11-10 16:56:02 +0000465 mbedtls_mpi_uint *N = NULL;
466 mbedtls_mpi_uint *R = NULL;
Minos Galanakis8f242702022-11-10 16:56:02 +0000467
468 mbedtls_mpi_mod_modulus m;
Janos Follathe7190a22022-11-26 18:46:54 +0000469 mbedtls_mpi_mod_residue r = { NULL, 0 };
Minos Galanakis96070a52022-11-25 19:32:10 +0000470 mbedtls_mpi_mod_ext_rep endian = MBEDTLS_MPI_MOD_EXT_REP_LE;
Minos Galanakis8f242702022-11-10 16:56:02 +0000471
Janos Follath799eaee2022-11-25 15:57:04 +0000472 mbedtls_mpi_mod_modulus_init( &m );
Janos Follath799eaee2022-11-25 15:57:04 +0000473
Janos Follath339b4392022-11-26 12:20:41 +0000474 size_t n_limbs;
Minos Galanakis96070a52022-11-25 19:32:10 +0000475 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
Janos Follath339b4392022-11-26 12:20:41 +0000476 size_t r_limbs = n_limbs;
477 ASSERT_ALLOC( R, r_limbs );
Minos Galanakis8f242702022-11-10 16:56:02 +0000478
Janos Follathe7190a22022-11-26 18:46:54 +0000479 /* modulus->p == NULL || residue->p == NULL ( m has not been set-up ) */
Janos Follath3e3fc912022-11-24 18:02:46 +0000480 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Janos Follath566c91d2022-11-26 12:05:50 +0000481 mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
Minos Galanakis8f242702022-11-10 16:56:02 +0000482
Minos Galanakis96070a52022-11-25 19:32:10 +0000483 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Janos Follath566c91d2022-11-26 12:05:50 +0000484 mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );
Minos Galanakis8f242702022-11-10 16:56:02 +0000485
Janos Follathe7190a22022-11-26 18:46:54 +0000486 /* Set up modulus and test with residue->p == NULL */
Minos Galanakis96070a52022-11-25 19:32:10 +0000487 TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
488 MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
Minos Galanakis96070a52022-11-25 19:32:10 +0000489
Minos Galanakis96070a52022-11-25 19:32:10 +0000490 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Janos Follathe7190a22022-11-26 18:46:54 +0000491 mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
Minos Galanakis96070a52022-11-25 19:32:10 +0000492 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Janos Follathe7190a22022-11-26 18:46:54 +0000493 mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );
494
495 /* Do the rest of the tests with a residue set up with the input data */
496 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R , r_limbs ) );
Minos Galanakis96070a52022-11-25 19:32:10 +0000497
Janos Follathd7bb3522022-11-26 14:59:27 +0000498 /* Fail for r_limbs < m->limbs */
499 r.limbs--;
500 TEST_ASSERT( r.limbs < m.limbs );
Minos Galanakis96070a52022-11-25 19:32:10 +0000501 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Janos Follath566c91d2022-11-26 12:05:50 +0000502 mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
Minos Galanakis96070a52022-11-25 19:32:10 +0000503 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Janos Follath566c91d2022-11-26 12:05:50 +0000504 mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );
Janos Follathd7bb3522022-11-26 14:59:27 +0000505 r.limbs++;
506
507 /* Fail for r_limbs > m->limbs */
508 m.limbs--;
509 TEST_ASSERT( r.limbs > m.limbs );
510 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
511 mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
512 TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
513 mbedtls_mpi_mod_write( &r, &m, buf->x, buf->len, endian ) );
514 m.limbs++;
Minos Galanakis96070a52022-11-25 19:32:10 +0000515
516 /* Test the read */
Janos Follath566c91d2022-11-26 12:05:50 +0000517 TEST_EQUAL( ret, mbedtls_mpi_mod_read( &r, &m, buf->x, buf->len, endian ) );
Minos Galanakis96070a52022-11-25 19:32:10 +0000518
519 /* Test write overflow only when the representation is large and read is successful */
Janos Follath6ef582f2022-11-26 14:19:02 +0000520 if ( r.limbs > 1 && ret == 0 )
Minos Galanakis96070a52022-11-25 19:32:10 +0000521 TEST_EQUAL( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL,
Janos Follath566c91d2022-11-26 12:05:50 +0000522 mbedtls_mpi_mod_write( &r, &m, buf->x, 1, endian ) );
Janos Follathd7bb3522022-11-26 14:59:27 +0000523
Minos Galanakis8f242702022-11-10 16:56:02 +0000524exit:
Janos Follathd7bb3522022-11-26 14:59:27 +0000525 mbedtls_mpi_mod_residue_release( &r );
Minos Galanakis8f242702022-11-10 16:56:02 +0000526 mbedtls_mpi_mod_modulus_free( &m );
Minos Galanakis8f242702022-11-10 16:56:02 +0000527 mbedtls_free( N );
528 mbedtls_free( R );
Minos Galanakis8f242702022-11-10 16:56:02 +0000529}
530/* END_CASE */
531
532/* BEGIN_CASE */
Janos Follath3e3fc912022-11-24 18:02:46 +0000533void mpi_mod_io( char * input_N, data_t * input_A, int endian )
Minos Galanakis8f242702022-11-10 16:56:02 +0000534{
535 mbedtls_mpi_uint *N = NULL;
536 mbedtls_mpi_uint *R = NULL;
Janos Follath8dfc8c42022-11-26 15:39:02 +0000537 mbedtls_mpi_uint *R_COPY = NULL;
Janos Follath0020df92022-11-26 17:23:16 +0000538 unsigned char *obuf = NULL;
539 unsigned char *ref_buf = NULL;
Minos Galanakis8f242702022-11-10 16:56:02 +0000540 mbedtls_mpi_mod_modulus m;
541 mbedtls_mpi_mod_residue r;
Janos Follath8dfc8c42022-11-26 15:39:02 +0000542 mbedtls_mpi_mod_residue r_copy;
Minos Galanakis8f242702022-11-10 16:56:02 +0000543 size_t n_limbs, n_bytes, a_bytes;
544
Janos Follath799eaee2022-11-25 15:57:04 +0000545 mbedtls_mpi_mod_modulus_init( &m );
546
Minos Galanakis8f242702022-11-10 16:56:02 +0000547 /* Read inputs */
548 TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) );
549 n_bytes = n_limbs * sizeof( mbedtls_mpi_uint );
Janos Follath6ef582f2022-11-26 14:19:02 +0000550 a_bytes = input_A->len;
Minos Galanakis8f242702022-11-10 16:56:02 +0000551
552 /* Allocate the memory for intermediate data structures */
553 ASSERT_ALLOC( R, n_bytes );
Janos Follath8dfc8c42022-11-26 15:39:02 +0000554 ASSERT_ALLOC( R_COPY, n_bytes );
Minos Galanakis8f242702022-11-10 16:56:02 +0000555
556 /* Test that input's size is not greater to modulo's */
Janos Follath6ef582f2022-11-26 14:19:02 +0000557 TEST_LE_U( a_bytes, n_bytes );
Minos Galanakis8f242702022-11-10 16:56:02 +0000558
559 /* Init Structures */
Janos Follath91295d22022-11-24 18:20:26 +0000560 TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs,
Minos Galanakis8f242702022-11-10 16:56:02 +0000561 MBEDTLS_MPI_MOD_REP_MONTGOMERY ) );
562
563 /* Enforcing p_limbs >= m->limbs */
Janos Follath3e3fc912022-11-24 18:02:46 +0000564 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r, &m, R, n_limbs ) );
Minos Galanakis8f242702022-11-10 16:56:02 +0000565
Janos Follath3e3fc912022-11-24 18:02:46 +0000566 TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r, &m, input_A->x, input_A->len,
567 endian ) );
Minos Galanakis8f242702022-11-10 16:56:02 +0000568
Janos Follath0020df92022-11-26 17:23:16 +0000569 /* Read a copy for checking that writing didn't change the value of r */
570 TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &r_copy, &m,
571 R_COPY, n_limbs ) );
Janos Follath8dfc8c42022-11-26 15:39:02 +0000572 TEST_EQUAL( 0, mbedtls_mpi_mod_read( &r_copy, &m, input_A->x, input_A->len,
573 endian ) );
Janos Follath8dfc8c42022-11-26 15:39:02 +0000574
Janos Follath0020df92022-11-26 17:23:16 +0000575 /* Get number of bytes without leading zeroes */
576 size_t a_bytes_trimmed = a_bytes;
577 while( a_bytes_trimmed > 0 )
578 {
579 unsigned char* r_byte_array = (unsigned char*) r.p;
580 if( r_byte_array[--a_bytes_trimmed] != 0 )
581 break;
582 }
583 a_bytes_trimmed++;
584
585 /* Test write with three output buffer sizes: tight, same as input and
586 * longer than the input */
587 size_t obuf_sizes[3];
588 const size_t obuf_sizes_len = sizeof( obuf_sizes ) / sizeof( obuf_sizes[0] );
589 obuf_sizes[0] = a_bytes_trimmed;
590 obuf_sizes[1] = a_bytes;
591 obuf_sizes[2] = a_bytes + 8;
592
593 for( size_t i = 0; i < obuf_sizes_len; i++ )
594 {
595 ASSERT_ALLOC( obuf, obuf_sizes[i] );
596 TEST_EQUAL( 0, mbedtls_mpi_mod_write( &r, &m, obuf, obuf_sizes[i], endian ) );
597
598 /* Make sure that writing didn't corrupt the value of r */
599 ASSERT_COMPARE( r.p, r.limbs, r_copy.p, r_copy.limbs );
600
601 /* Set up reference output for checking the result */
602 ASSERT_ALLOC( ref_buf, obuf_sizes[i] );
603 switch( endian )
604 {
605 case MBEDTLS_MPI_MOD_EXT_REP_LE:
606 memcpy( ref_buf, input_A->x, a_bytes_trimmed );
607 break;
608 case MBEDTLS_MPI_MOD_EXT_REP_BE:
609 {
610 size_t a_offset = input_A->len - a_bytes_trimmed;
611 size_t ref_offset = obuf_sizes[i] - a_bytes_trimmed;
612 memcpy( ref_buf + ref_offset, input_A->x + a_offset,
613 a_bytes_trimmed );
614 }
615 break;
616 default:
617 TEST_ASSERT( 0 );
618 }
619
620 /* Check the result */
621 ASSERT_COMPARE( obuf, obuf_sizes[i], ref_buf, obuf_sizes[i] );
622
623 mbedtls_free( ref_buf );
624 ref_buf = NULL;
625 mbedtls_free( obuf );
626 obuf = NULL;
627 }
628
Minos Galanakis8f242702022-11-10 16:56:02 +0000629exit:
630 mbedtls_mpi_mod_modulus_free( &m );
631 mbedtls_free( N );
632 mbedtls_free( R );
Janos Follath8dfc8c42022-11-26 15:39:02 +0000633 mbedtls_free( R_COPY );
Janos Follath0020df92022-11-26 17:23:16 +0000634 mbedtls_free( obuf );
Minos Galanakis8f242702022-11-10 16:56:02 +0000635}
636/* END_CASE */
Janos Follath5933f692022-11-02 14:35:17 +0000637/* END MERGE SLOT 7 */
638
639/* BEGIN MERGE SLOT 8 */
640
641/* END MERGE SLOT 8 */
642
643/* BEGIN MERGE SLOT 9 */
644
645/* END MERGE SLOT 9 */
646
647/* BEGIN MERGE SLOT 10 */
648
649/* END MERGE SLOT 10 */