blob: de66fdd6a694a9e9270c1f4179259e91ed022b94 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/bignum.h"
Gilles Peskine3cb1e292020-11-25 15:37:20 +01003#include "mbedtls/entropy.h"
Janos Follathf1d617d2022-07-21 09:29:32 +01004#include "bignum_core.h"
Gabor Mezei23a1ce92022-08-02 11:54:44 +02005#include "bignum_mod.h"
6#include "bignum_mod_raw.h"
Janos Follath23bdeca2022-07-22 18:24:06 +01007#include "constant_time_internal.h"
8#include "test/constant_flow.h"
Janos Follath64eca052018-09-05 17:04:49 +01009
Chris Jonese64a46f2020-12-03 17:44:03 +000010#if MBEDTLS_MPI_MAX_BITS > 792
11#define MPI_MAX_BITS_LARGER_THAN_792
Chris Jones4592bd82020-12-03 14:24:33 +000012#endif
Janos Follath64eca052018-09-05 17:04:49 +010013
Gilles Peskinedffc7102021-06-10 15:34:15 +020014/* Check the validity of the sign bit in an MPI object. Reject representations
15 * that are not supported by the rest of the library and indicate a bug when
16 * constructing the value. */
17static int sign_is_valid( const mbedtls_mpi *X )
18{
19 if( X->s != 1 && X->s != -1 )
20 return( 0 ); // invalid sign bit, e.g. 0
21 if( mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 )
22 return( 0 ); // negative zero
23 return( 1 );
24}
25
Janos Follath64eca052018-09-05 17:04:49 +010026typedef struct mbedtls_test_mpi_random
27{
28 data_t *data;
29 size_t pos;
30 size_t chunk_len;
31} mbedtls_test_mpi_random;
32
33/*
34 * This function is called by the Miller-Rabin primality test each time it
35 * chooses a random witness. The witnesses (or non-witnesses as provided by the
36 * test) are stored in the data member of the state structure. Each number is in
37 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
38 */
39int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
40 unsigned char* buf,
41 size_t len )
42{
43 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
44
45 if( random == NULL || random->data->x == NULL || buf == NULL )
46 return( -1 );
47
48 if( random->pos + random->chunk_len > random->data->len
49 || random->chunk_len > len )
50 {
51 return( -1 );
52 }
53
54 memset( buf, 0, len );
55
56 /* The witness is written to the end of the buffer, since the buffer is
57 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
58 * Writing the witness to the start of the buffer would result in the
59 * buffer being 'witness 000...000', which would be treated as
60 * witness * 2^n for some n. */
61 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
62 random->chunk_len );
63
64 random->pos += random->chunk_len;
65
66 return( 0 );
67}
Gilles Peskine3cb1e292020-11-25 15:37:20 +010068
69/* Random generator that is told how many bytes to return. */
70static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
71{
72 size_t *bytes_left = state;
73 size_t i;
74 for( i = 0; i < len; i++ )
75 {
76 if( *bytes_left == 0 )
77 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
78 buf[i] = *bytes_left & 0xff;
79 --( *bytes_left );
80 }
81 return( 0 );
82}
83
Gilles Peskineeedefa52021-04-13 19:50:04 +020084/* Test whether bytes represents (in big-endian base 256) a number b that
85 * is significantly above a power of 2. That is, b must not have a long run
86 * of unset bits after the most significant bit.
87 *
88 * Let n be the bit-size of b, i.e. the integer such that 2^n <= b < 2^{n+1}.
89 * This function returns 1 if, when drawing a number between 0 and b,
90 * the probability that this number is at least 2^n is not negligible.
91 * This probability is (b - 2^n) / b and this function checks that this
92 * number is above some threshold A. The threshold value is heuristic and
93 * based on the needs of mpi_random_many().
Gilles Peskine02ac93a2021-03-29 22:02:55 +020094 */
95static int is_significantly_above_a_power_of_2( data_t *bytes )
96{
97 const uint8_t *p = bytes->x;
98 size_t len = bytes->len;
99 unsigned x;
Gilles Peskineeedefa52021-04-13 19:50:04 +0200100
101 /* Skip leading null bytes */
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200102 while( len > 0 && p[0] == 0 )
103 {
104 ++p;
105 --len;
106 }
Gilles Peskineeedefa52021-04-13 19:50:04 +0200107 /* 0 is not significantly above a power of 2 */
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200108 if( len == 0 )
109 return( 0 );
Gilles Peskineeedefa52021-04-13 19:50:04 +0200110 /* Extract the (up to) 2 most significant bytes */
111 if( len == 1 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200112 x = p[0];
113 else
114 x = ( p[0] << 8 ) | p[1];
115
Gilles Peskineeedefa52021-04-13 19:50:04 +0200116 /* Shift the most significant bit of x to position 8 and mask it out */
117 while( ( x & 0xfe00 ) != 0 )
118 x >>= 1;
119 x &= 0x00ff;
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200120
Gilles Peskineeedefa52021-04-13 19:50:04 +0200121 /* At this point, x = floor((b - 2^n) / 2^(n-8)). b is significantly above
122 * a power of 2 iff x is significantly above 0 compared to 2^8.
123 * Testing x >= 2^4 amounts to picking A = 1/16 in the function
124 * description above. */
125 return( x >= 0x10 );
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200126}
127
Paul Bakker33b43f12013-08-20 11:48:36 +0200128/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +0000129
Paul Bakker33b43f12013-08-20 11:48:36 +0200130/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200132 * END_DEPENDENCIES
133 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000134
Hanno Beckerb48e1aa2018-12-18 23:25:01 +0000135/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100136void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200137{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200138 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200139
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200140 mbedtls_mpi_init( &X );
141 mbedtls_mpi_init( &Y );
142 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200143
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200144 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
145 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200146 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200147 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200148
149exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200150 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200151}
152/* END_CASE */
153
154/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100155void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
156 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200157 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000160 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100161 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000164
Janos Follath04dadb72019-03-06 12:29:37 +0000165 memset( str, '!', sizeof( str ) );
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200168 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000169 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200170 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100171 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200172 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000173 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200174 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000175 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000176 }
177 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000178
Paul Bakkerbd51b262014-07-10 15:26:12 +0200179exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000181}
Paul Bakker33b43f12013-08-20 11:48:36 +0200182/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000183
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100185void mbedtls_mpi_read_binary( data_t * buf, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000188 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100189 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000192
Paul Bakkere896fea2009-07-06 06:40:23 +0000193
Azim Khand30ca132017-06-09 04:32:58 +0100194 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200195 TEST_ASSERT( sign_is_valid( &X ) );
Werner Lewisf65a3272022-07-07 11:38:44 +0100196 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdc47fe72022-08-01 13:55:41 +0100197 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000198
Paul Bakkerbd51b262014-07-10 15:26:12 +0200199exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000201}
Paul Bakker33b43f12013-08-20 11:48:36 +0200202/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000203
Paul Bakker33b43f12013-08-20 11:48:36 +0200204/* BEGIN_CASE */
Janos Follath91dc67d2022-07-22 14:24:58 +0100205void mbedtls_mpi_core_io_null()
206{
207 mbedtls_mpi_uint X = 0;
208 int ret;
209
210 ret = mbedtls_mpi_core_read_be( &X, 1, NULL, 0 );
211 TEST_ASSERT( ret == 0 );
212 ret = mbedtls_mpi_core_write_be( &X, 1, NULL, 0 );
213 TEST_ASSERT( ret == 0 );
214
215 ret = mbedtls_mpi_core_read_be( NULL, 0, NULL, 0 );
216 TEST_ASSERT( ret == 0 );
217 ret = mbedtls_mpi_core_write_be( NULL, 0, NULL, 0 );
218 TEST_ASSERT( ret == 0 );
219
220 ret = mbedtls_mpi_core_read_le( &X, 1, NULL, 0 );
221 TEST_ASSERT( ret == 0 );
222 ret = mbedtls_mpi_core_write_le( &X, 1, NULL, 0 );
223 TEST_ASSERT( ret == 0 );
224
225 ret = mbedtls_mpi_core_read_le( NULL, 0, NULL, 0 );
226 TEST_ASSERT( ret == 0 );
227 ret = mbedtls_mpi_core_write_le( NULL, 0, NULL, 0 );
228 TEST_ASSERT( ret == 0 );
229
230exit:
231 ;
232}
233/* END_CASE */
234
235/* BEGIN_CASE */
Janos Follathf1d617d2022-07-21 09:29:32 +0100236void mbedtls_mpi_core_io_be( data_t *input, int nb_int, int nx_64_int, int iret,
237 int oret )
238{
239 #define BMAX 1024
240 unsigned char buf[BMAX];
241 #define XMAX BMAX / sizeof( mbedtls_mpi_uint )
242 mbedtls_mpi_uint X[XMAX];
243 size_t nx, nb;
244 int ret;
245
246 if( iret != 0 )
247 TEST_ASSERT( oret == 0 );
248
249 TEST_ASSERT( 0 <= nb_int );
250 nb = nb_int;
251 TEST_ASSERT( nb <= BMAX );
252
253 TEST_ASSERT( 0 <= nx_64_int );
254 nx = nx_64_int;
255 /* nx_64_int is the number of 64 bit limbs, if we have 32 bit limbs we need
256 * to double the number of limbs to have the same size. */
257 if( sizeof( mbedtls_mpi_uint ) == 4 )
258 nx *= 2;
259 TEST_ASSERT( nx <= XMAX );
260
261 ret = mbedtls_mpi_core_read_be( X, nx, input->x, input->len );
262 TEST_ASSERT( ret == iret );
263
264 if( iret == 0 )
265 {
266 ret = mbedtls_mpi_core_write_be( X, nx, buf, nb );
267 TEST_ASSERT( ret == oret );
268 }
269
270 if( ( iret == 0 ) && ( oret == 0 ) )
271 {
272 if( nb > input->len )
273 {
274 size_t leading_zeroes = nb - input->len;
275 TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
276 for( size_t i = 0; i < leading_zeroes; i++ )
277 TEST_ASSERT( buf[i] == 0 );
278 }
279 else
280 {
281 size_t leading_zeroes = input->len - nb;
282 TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
283 for( size_t i = 0; i < leading_zeroes; i++ )
284 TEST_ASSERT( input->x[i] == 0 );
285 }
286 }
287
288exit:
289 ;
290
291 #undef BMAX
292 #undef XMAX
293}
294/* END_CASE */
295
296/* BEGIN_CASE */
Janos Follath6ff35362022-07-21 15:27:21 +0100297void mbedtls_mpi_core_io_le( data_t *input, int nb_int, int nx_64_int, int iret,
298 int oret )
299{
300 #define BMAX 1024
301 unsigned char buf[BMAX];
302 #define XMAX BMAX / sizeof( mbedtls_mpi_uint )
303 mbedtls_mpi_uint X[XMAX];
304 size_t nx, nb;
305 int ret;
306
307 if( iret != 0 )
308 TEST_ASSERT( oret == 0 );
309
310 TEST_ASSERT( 0 <= nb_int );
311 nb = nb_int;
312 TEST_ASSERT( nb <= BMAX );
313
314 TEST_ASSERT( 0 <= nx_64_int );
315 nx = nx_64_int;
316 /* nx_64_int is the number of 64 bit limbs, if we have 32 bit limbs we need
317 * to double the number of limbs to have the same size. */
318 if( sizeof( mbedtls_mpi_uint ) == 4 )
319 nx *= 2;
320 TEST_ASSERT( nx <= XMAX );
321
322 ret = mbedtls_mpi_core_read_le( X, nx, input->x, input->len );
323 TEST_ASSERT( ret == iret );
324
325 if( iret == 0 )
326 {
327 ret = mbedtls_mpi_core_write_le( X, nx, buf, nb );
328 TEST_ASSERT( ret == oret );
329 }
330
331 if( ( iret == 0 ) && ( oret == 0 ) )
332 {
333 if( nb > input->len )
334 {
335 TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 );
336 for( size_t i = input->len; i < nb; i++ )
337 TEST_ASSERT( buf[i] == 0 );
338 }
339 else
340 {
341 TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 );
342 for( size_t i = nb; i < input->len; i++ )
343 TEST_ASSERT( input->x[i] == 0 );
344 }
345 }
346
347exit:
348 ;
349
350 #undef BMAX
351 #undef XMAX
352}
353/* END_CASE */
354
355/* BEGIN_CASE */
Gabor Mezei23a1ce92022-08-02 11:54:44 +0200356void mbedtls_mpi_mod_raw_io( data_t *input, int nb_int, int nx_64_int,
357 int iendian, int iret, int oret )
358{
359 #define BMAX 1024
360 unsigned char buf[BMAX];
361 #define XMAX BMAX / sizeof( mbedtls_mpi_uint )
362 mbedtls_mpi_uint X[XMAX];
363 mbedtls_mpi_uint init[XMAX];
364 mbedtls_mpi_mod_modulus m;
365 size_t nx, nb;
366 int ret;
367 int endian;
368
369 if( iret != 0 )
370 TEST_ASSERT( oret == 0 );
371
372 TEST_ASSERT( 0 <= nb_int );
373 nb = nb_int;
374 TEST_ASSERT( nb <= BMAX );
375
376 TEST_ASSERT( 0 <= nx_64_int );
377 nx = nx_64_int;
378 /* nx_64_int is the number of 64 bit limbs, if we have 32 bit limbs we need
379 * to double the number of limbs to have the same size. */
380 if( sizeof( mbedtls_mpi_uint ) == 4 )
381 nx *= 2;
382 TEST_ASSERT( nx <= XMAX );
383
384 if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID )
385 endian = MBEDTLS_MPI_MOD_EXT_REP_LE;
386 else
387 endian = iendian;
388
389 mbedtls_mpi_mod_modulus_init( &m );
390 TEST_ASSERT( memset( init, 0xFF, sizeof( init ) ) );
391
392 ret = mbedtls_mpi_mod_modulus_setup( &m, init, nx, endian,
393 MBEDTLS_MPI_MOD_REP_MONTGOMERY );
394 TEST_ASSERT( ret == 0 );
395
396 if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && iret != 0 )
397 m.ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
398
399 ret = mbedtls_mpi_mod_raw_read( X, &m, input->x, input->len );
400 TEST_ASSERT( ret == iret );
401
402 if( iret == 0 )
403 {
404 if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && oret != 0 )
405 m.ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
406
407 ret = mbedtls_mpi_mod_raw_write( X, &m, buf, nb );
408 TEST_ASSERT( ret == oret );
409 }
410
411 if( ( iret == 0 ) && ( oret == 0 ) )
412 {
413 if( nb > input->len )
414 {
415 if( endian == MBEDTLS_MPI_MOD_EXT_REP_BE )
416 {
417 size_t leading_zeroes = nb - input->len;
418 TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
419 for( size_t i = 0; i < leading_zeroes; i++ )
420 TEST_ASSERT( buf[i] == 0 );
421 }
422 else
423 {
424 TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 );
425 for( size_t i = input->len; i < nb; i++ )
426 TEST_ASSERT( buf[i] == 0 );
427 }
428 }
429 else
430 {
431 if( endian == MBEDTLS_MPI_MOD_EXT_REP_BE )
432 {
433 size_t leading_zeroes = input->len - nb;
434 TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
435 for( size_t i = 0; i < leading_zeroes; i++ )
436 TEST_ASSERT( input->x[i] == 0 );
437 }
438 else
439 {
440 TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 );
441 for( size_t i = nb; i < input->len; i++ )
442 TEST_ASSERT( input->x[i] == 0 );
443 }
444 }
445 }
446
447exit:
448 mbedtls_mpi_mod_modulus_free( &m );
449
450 #undef BMAX
451 #undef XMAX
452}
453/* END_CASE */
454
455/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100456void mbedtls_mpi_read_binary_le( data_t * buf, char * input_A )
Janos Follatha778a942019-02-13 10:28:28 +0000457{
458 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000459 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000460 size_t len;
461
462 mbedtls_mpi_init( &X );
463
464
465 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200466 TEST_ASSERT( sign_is_valid( &X ) );
Werner Lewisf65a3272022-07-07 11:38:44 +0100467 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdc47fe72022-08-01 13:55:41 +0100468 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000469
470exit:
471 mbedtls_mpi_free( &X );
472}
473/* END_CASE */
474
475/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100476void mbedtls_mpi_write_binary( char * input_X, data_t * input_A,
477 int output_size, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000478{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000480 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000481 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000482
483 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000486
Werner Lewis19b4cd82022-07-07 11:02:27 +0100487 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200490 if( buflen > (size_t) output_size )
491 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200494 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000495 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000496
Ronald Cron2dbba992020-06-10 11:42:32 +0200497 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
498 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000499 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000500
Paul Bakkerbd51b262014-07-10 15:26:12 +0200501exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000503}
Paul Bakker33b43f12013-08-20 11:48:36 +0200504/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000505
Janos Follathe344d0f2019-02-19 16:17:40 +0000506/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100507void mbedtls_mpi_write_binary_le( char * input_X, data_t * input_A,
508 int output_size, int result )
Janos Follathe344d0f2019-02-19 16:17:40 +0000509{
510 mbedtls_mpi X;
511 unsigned char buf[1000];
512 size_t buflen;
513
514 memset( buf, 0x00, 1000 );
515
516 mbedtls_mpi_init( &X );
517
Werner Lewis19b4cd82022-07-07 11:02:27 +0100518 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000519
520 buflen = mbedtls_mpi_size( &X );
521 if( buflen > (size_t) output_size )
522 buflen = (size_t) output_size;
523
524 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
525 if( result == 0)
526 {
527
Ronald Cron2dbba992020-06-10 11:42:32 +0200528 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
529 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000530 }
531
532exit:
533 mbedtls_mpi_free( &X );
534}
535/* END_CASE */
536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Werner Lewisefda01f2022-07-06 13:03:36 +0100538void mbedtls_mpi_read_file( char * input_file, data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000541 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000542 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000543 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000544 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000545
546 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000549
Paul Bakker33b43f12013-08-20 11:48:36 +0200550 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200551 TEST_ASSERT( file != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100552 ret = mbedtls_mpi_read_file( &X, 16, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000553 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000554 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000555
Paul Bakker33b43f12013-08-20 11:48:36 +0200556 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000557 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200558 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 buflen = mbedtls_mpi_size( &X );
560 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000561
Paul Bakkere896fea2009-07-06 06:40:23 +0000562
Ronald Cron2dbba992020-06-10 11:42:32 +0200563 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
564 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000565 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000566
Paul Bakkerbd51b262014-07-10 15:26:12 +0200567exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000569}
Paul Bakker33b43f12013-08-20 11:48:36 +0200570/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Werner Lewisefda01f2022-07-06 13:03:36 +0100573void mbedtls_mpi_write_file( char * input_X, char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000574{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000576 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200577 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000580
Werner Lewis19b4cd82022-07-07 11:02:27 +0100581 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000582
Paul Bakker33b43f12013-08-20 11:48:36 +0200583 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000584 TEST_ASSERT( file_out != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100585 ret = mbedtls_mpi_write_file( NULL, &X, 16, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000586 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200587 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000588
Paul Bakker33b43f12013-08-20 11:48:36 +0200589 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000590 TEST_ASSERT( file_in != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100591 ret = mbedtls_mpi_read_file( &Y, 16, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000592 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200593 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000596
Paul Bakkerbd51b262014-07-10 15:26:12 +0200597exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000599}
Paul Bakker33b43f12013-08-20 11:48:36 +0200600/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000601
Paul Bakker33b43f12013-08-20 11:48:36 +0200602/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100603void mbedtls_mpi_get_bit( char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_mpi X;
606 mbedtls_mpi_init( &X );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100607 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000609
Paul Bakkerbd51b262014-07-10 15:26:12 +0200610exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000612}
Paul Bakker33b43f12013-08-20 11:48:36 +0200613/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000614
Paul Bakker33b43f12013-08-20 11:48:36 +0200615/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100616void mbedtls_mpi_set_bit( char * input_X, int pos, int val,
617 char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_mpi X, Y;
620 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000621
Werner Lewis19b4cd82022-07-07 11:02:27 +0100622 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
623 TEST_ASSERT( mbedtls_test_read_mpi( &Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100624 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
625
626 if( result == 0 )
627 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200628 TEST_ASSERT( sign_is_valid( &X ) );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100629 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
630 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000631
Paul Bakkerbd51b262014-07-10 15:26:12 +0200632exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000634}
Paul Bakker33b43f12013-08-20 11:48:36 +0200635/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000636
Paul Bakker33b43f12013-08-20 11:48:36 +0200637/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100638void mbedtls_mpi_lsb( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_mpi X;
641 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000642
Werner Lewis19b4cd82022-07-07 11:02:27 +0100643 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000645
Paul Bakkerbd51b262014-07-10 15:26:12 +0200646exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000648}
Paul Bakker33b43f12013-08-20 11:48:36 +0200649/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000650
Paul Bakker33b43f12013-08-20 11:48:36 +0200651/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100652void mbedtls_mpi_bitlen( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_mpi X;
655 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000656
Werner Lewis19b4cd82022-07-07 11:02:27 +0100657 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200658 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000659
Paul Bakkerbd51b262014-07-10 15:26:12 +0200660exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000662}
Paul Bakker33b43f12013-08-20 11:48:36 +0200663/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000664
Paul Bakker33b43f12013-08-20 11:48:36 +0200665/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100666void mbedtls_mpi_gcd( char * input_X, char * input_Y,
667 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000668{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_mpi A, X, Y, Z;
670 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000671
Werner Lewis19b4cd82022-07-07 11:02:27 +0100672 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
673 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
674 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200676 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000678
Paul Bakkerbd51b262014-07-10 15:26:12 +0200679exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000681}
Paul Bakker33b43f12013-08-20 11:48:36 +0200682/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000683
Paul Bakker33b43f12013-08-20 11:48:36 +0200684/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_mpi X;
688 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
691 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000692
Paul Bakkerbd51b262014-07-10 15:26:12 +0200693exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000695}
Paul Bakker33b43f12013-08-20 11:48:36 +0200696/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000697
Paul Bakker33b43f12013-08-20 11:48:36 +0200698/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100699void mbedtls_mpi_cmp_mpi( char * input_X, char * input_Y,
700 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000701{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_mpi X, Y;
703 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000704
Werner Lewis19b4cd82022-07-07 11:02:27 +0100705 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
706 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000708
Paul Bakkerbd51b262014-07-10 15:26:12 +0200709exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000711}
Paul Bakker33b43f12013-08-20 11:48:36 +0200712/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000713
Paul Bakker33b43f12013-08-20 11:48:36 +0200714/* BEGIN_CASE */
Janos Follath23bdeca2022-07-22 18:24:06 +0100715void mbedtls_mpi_core_lt_ct( data_t * input_X, data_t * input_Y, int input_ret )
716{
717 #define MAX_LEN 64
718 mbedtls_mpi_uint X[MAX_LEN];
719 mbedtls_mpi_uint Y[MAX_LEN];
720 unsigned exp_ret = input_ret;
721 unsigned ret;
722 size_t len = CHARS_TO_LIMBS(
723 input_X->len > input_Y->len ? input_X->len : input_Y->len );
724
725 TEST_ASSERT( len <= MAX_LEN );
726
727 TEST_ASSERT( mbedtls_mpi_core_read_be( X, len, input_X->x, input_X->len )
728 == 0 );
729 TEST_ASSERT( mbedtls_mpi_core_read_be( Y, len, input_Y->x, input_Y->len )
730 == 0 );
731
732 TEST_CF_SECRET( X, len * sizeof( mbedtls_mpi_uint ) );
733 TEST_CF_SECRET( Y, len * sizeof( mbedtls_mpi_uint ) );
734
735 ret = mbedtls_mpi_core_lt_ct( X, Y, len );
736
737 TEST_CF_PUBLIC( X, len * sizeof( mbedtls_mpi_uint ) );
738 TEST_CF_PUBLIC( Y, len * sizeof( mbedtls_mpi_uint ) );
739 TEST_CF_PUBLIC( &ret, sizeof( ret ) );
740
741 TEST_ASSERT( ret == exp_ret );
742
743exit:
744 ;
745
746 #undef MAX_LEN
747}
748/* END_CASE */
749
750/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100751void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
752 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100753 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100754{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200755 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100756 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100757 mbedtls_mpi X, Y;
758 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
759
Werner Lewis19b4cd82022-07-07 11:02:27 +0100760 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
761 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100762
Gilles Peskine9018b112020-01-21 16:30:53 +0100763 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
764 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100765
Janos Follath0e5532d2019-10-11 14:21:53 +0100766 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100767 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100768 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100769
770exit:
771 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
772}
773/* END_CASE */
774
775/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100776void mbedtls_mpi_cmp_abs( char * input_X, char * input_Y,
777 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000778{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_mpi X, Y;
780 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000781
Werner Lewis19b4cd82022-07-07 11:02:27 +0100782 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
783 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000785
Paul Bakkerbd51b262014-07-10 15:26:12 +0200786exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000788}
Paul Bakker33b43f12013-08-20 11:48:36 +0200789/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000790
Paul Bakker33b43f12013-08-20 11:48:36 +0200791/* BEGIN_CASE */
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200792void mbedtls_mpi_copy( char *src_hex, char *dst_hex )
Paul Bakker367dae42009-06-28 21:50:27 +0000793{
Gilles Peskined0722f82021-06-10 23:00:33 +0200794 mbedtls_mpi src, dst, ref;
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200795 mbedtls_mpi_init( &src );
796 mbedtls_mpi_init( &dst );
Gilles Peskined0722f82021-06-10 23:00:33 +0200797 mbedtls_mpi_init( &ref );
Paul Bakker367dae42009-06-28 21:50:27 +0000798
Werner Lewis19b4cd82022-07-07 11:02:27 +0100799 TEST_ASSERT( mbedtls_test_read_mpi( &src, src_hex ) == 0 );
800 TEST_ASSERT( mbedtls_test_read_mpi( &ref, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200801
802 /* mbedtls_mpi_copy() */
Werner Lewis19b4cd82022-07-07 11:02:27 +0100803 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200804 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200805 TEST_ASSERT( sign_is_valid( &dst ) );
806 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000807
Gilles Peskined0722f82021-06-10 23:00:33 +0200808 /* mbedtls_mpi_safe_cond_assign(), assignment done */
809 mbedtls_mpi_free( &dst );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100810 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200811 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
812 TEST_ASSERT( sign_is_valid( &dst ) );
813 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
814
815 /* mbedtls_mpi_safe_cond_assign(), assignment not done */
816 mbedtls_mpi_free( &dst );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100817 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200818 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
819 TEST_ASSERT( sign_is_valid( &dst ) );
820 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
821
Paul Bakkerbd51b262014-07-10 15:26:12 +0200822exit:
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200823 mbedtls_mpi_free( &src );
824 mbedtls_mpi_free( &dst );
Gilles Peskined0722f82021-06-10 23:00:33 +0200825 mbedtls_mpi_free( &ref );
Gilles Peskine7428b452020-01-20 21:01:51 +0100826}
827/* END_CASE */
828
829/* BEGIN_CASE */
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200830void mpi_copy_self( char *input_X )
Gilles Peskine7428b452020-01-20 21:01:51 +0100831{
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200832 mbedtls_mpi X, A;
833 mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000835
Werner Lewis19b4cd82022-07-07 11:02:27 +0100836 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200838
Werner Lewis19b4cd82022-07-07 11:02:27 +0100839 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_X ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200840 TEST_ASSERT( sign_is_valid( &X ) );
841 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000842
Paul Bakkerbd51b262014-07-10 15:26:12 +0200843exit:
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200844 mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000846}
Paul Bakker33b43f12013-08-20 11:48:36 +0200847/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000848
Paul Bakker33b43f12013-08-20 11:48:36 +0200849/* BEGIN_CASE */
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200850void mbedtls_mpi_swap( char *X_hex, char *Y_hex )
851{
852 mbedtls_mpi X, Y, X0, Y0;
853 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
854 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
855
Werner Lewis19b4cd82022-07-07 11:02:27 +0100856 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
857 TEST_ASSERT( mbedtls_test_read_mpi( &Y0, Y_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200858
Gilles Peskined0722f82021-06-10 23:00:33 +0200859 /* mbedtls_mpi_swap() */
Werner Lewis19b4cd82022-07-07 11:02:27 +0100860 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
861 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200862 mbedtls_mpi_swap( &X, &Y );
863 TEST_ASSERT( sign_is_valid( &X ) );
864 TEST_ASSERT( sign_is_valid( &Y ) );
865 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
866 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
867
Gilles Peskined0722f82021-06-10 23:00:33 +0200868 /* mbedtls_mpi_safe_cond_swap(), swap done */
869 mbedtls_mpi_free( &X );
870 mbedtls_mpi_free( &Y );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100871 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
872 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200873 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
874 TEST_ASSERT( sign_is_valid( &X ) );
875 TEST_ASSERT( sign_is_valid( &Y ) );
876 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
877 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
878
879 /* mbedtls_mpi_safe_cond_swap(), swap not done */
880 mbedtls_mpi_free( &X );
881 mbedtls_mpi_free( &Y );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100882 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
883 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200884 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
885 TEST_ASSERT( sign_is_valid( &X ) );
886 TEST_ASSERT( sign_is_valid( &Y ) );
887 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
888 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
889
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200890exit:
891 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
892 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
893}
894/* END_CASE */
895
896/* BEGIN_CASE */
897void mpi_swap_self( char *X_hex )
898{
899 mbedtls_mpi X, X0;
900 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
901
Werner Lewis19b4cd82022-07-07 11:02:27 +0100902 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
903 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200904
905 mbedtls_mpi_swap( &X, &X );
906 TEST_ASSERT( sign_is_valid( &X ) );
907 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
908
909exit:
910 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
911}
912/* END_CASE */
913
914/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100916{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_mpi X;
918 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Gilles Peskinee1091752021-06-15 21:19:18 +0200921 if( used > 0 )
922 {
923 size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint );
924 TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 );
925 }
926 TEST_EQUAL( X.n, (size_t) before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Gilles Peskinee1091752021-06-15 21:19:18 +0200928 TEST_EQUAL( X.n, (size_t) after );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100929
Paul Bakkerbd51b262014-07-10 15:26:12 +0200930exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100932}
933/* END_CASE */
934
935/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100936void mbedtls_mpi_add_mpi( char * input_X, char * input_Y,
937 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_mpi X, Y, Z, A;
940 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000941
Werner Lewis19b4cd82022-07-07 11:02:27 +0100942 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
943 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
944 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200946 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000948
Gilles Peskine56f943a2020-07-23 01:18:11 +0200949 /* result == first operand */
950 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200951 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200952 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100953 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200954
955 /* result == second operand */
956 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200957 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200958 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
959
Paul Bakkerbd51b262014-07-10 15:26:12 +0200960exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000962}
Paul Bakker33b43f12013-08-20 11:48:36 +0200963/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000964
Paul Bakker33b43f12013-08-20 11:48:36 +0200965/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100966void mbedtls_mpi_add_mpi_inplace( char * input_X, char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100967{
968 mbedtls_mpi X, A;
969 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
970
Werner Lewis19b4cd82022-07-07 11:02:27 +0100971 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100972
Werner Lewis19b4cd82022-07-07 11:02:27 +0100973 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100974 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
975 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200976 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100977
Werner Lewis19b4cd82022-07-07 11:02:27 +0100978 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100979 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200980 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100981 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
982
Werner Lewis19b4cd82022-07-07 11:02:27 +0100983 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100984 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200985 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +0100986 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
987
988exit:
989 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
990}
991/* END_CASE */
992
993
994/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100995void mbedtls_mpi_add_abs( char * input_X, char * input_Y,
996 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000997{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_mpi X, Y, Z, A;
999 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001000
Werner Lewis19b4cd82022-07-07 11:02:27 +01001001 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1002 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1003 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001005 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001007
Gilles Peskine56f943a2020-07-23 01:18:11 +02001008 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001010 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001012 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001013
1014 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001016 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001018
Paul Bakkerbd51b262014-07-10 15:26:12 +02001019exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +02001020 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +00001021}
Paul Bakker33b43f12013-08-20 11:48:36 +02001022/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +00001023
Paul Bakker33b43f12013-08-20 11:48:36 +02001024/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001025void mbedtls_mpi_add_int( char * input_X, int input_Y,
1026 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001027{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_mpi X, Z, A;
1029 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001030
Werner Lewis19b4cd82022-07-07 11:02:27 +01001031 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1032 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001034 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001036
Paul Bakkerbd51b262014-07-10 15:26:12 +02001037exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001039}
Paul Bakker33b43f12013-08-20 11:48:36 +02001040/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001041
Paul Bakker33b43f12013-08-20 11:48:36 +02001042/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001043void mbedtls_mpi_sub_mpi( char * input_X, char * input_Y,
1044 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_mpi X, Y, Z, A;
1047 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001048
Werner Lewis19b4cd82022-07-07 11:02:27 +01001049 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1050 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1051 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001053 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001055
Gilles Peskine56f943a2020-07-23 01:18:11 +02001056 /* result == first operand */
1057 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001058 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001059 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001060 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001061
1062 /* result == second operand */
1063 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001064 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001065 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1066
Paul Bakkerbd51b262014-07-10 15:26:12 +02001067exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001069}
Paul Bakker33b43f12013-08-20 11:48:36 +02001070/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001071
Paul Bakker33b43f12013-08-20 11:48:36 +02001072/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001073void mbedtls_mpi_sub_abs( char * input_X, char * input_Y,
1074 char * input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001077 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001079
Werner Lewis19b4cd82022-07-07 11:02:27 +01001080 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1081 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1082 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001085 TEST_ASSERT( res == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001086 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +00001087 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001089
Gilles Peskine56f943a2020-07-23 01:18:11 +02001090 /* result == first operand */
1091 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001092 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001093 if( sub_result == 0 )
1094 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +01001095 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001096
1097 /* result == second operand */
1098 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001099 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001100 if( sub_result == 0 )
1101 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1102
Paul Bakkerbd51b262014-07-10 15:26:12 +02001103exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001105}
Paul Bakker33b43f12013-08-20 11:48:36 +02001106/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001107
Paul Bakker33b43f12013-08-20 11:48:36 +02001108/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001109void mbedtls_mpi_sub_int( char * input_X, int input_Y,
1110 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_mpi X, Z, A;
1113 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001114
Werner Lewis19b4cd82022-07-07 11:02:27 +01001115 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1116 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001118 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001120
Paul Bakkerbd51b262014-07-10 15:26:12 +02001121exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001123}
Paul Bakker33b43f12013-08-20 11:48:36 +02001124/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001125
Paul Bakker33b43f12013-08-20 11:48:36 +02001126/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001127void mbedtls_mpi_mul_mpi( char * input_X, char * input_Y,
1128 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_mpi X, Y, Z, A;
1131 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001132
Werner Lewis19b4cd82022-07-07 11:02:27 +01001133 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1134 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1135 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001137 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001139
Paul Bakkerbd51b262014-07-10 15:26:12 +02001140exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001142}
Paul Bakker33b43f12013-08-20 11:48:36 +02001143/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001144
Paul Bakker33b43f12013-08-20 11:48:36 +02001145/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001146void mbedtls_mpi_mul_int( char * input_X, int input_Y,
Werner Lewisefda01f2022-07-06 13:03:36 +01001147 char * input_A, char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001148{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 mbedtls_mpi X, Z, A;
1150 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001151
Werner Lewis19b4cd82022-07-07 11:02:27 +01001152 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1153 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001155 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001156 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001158 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001160 else
1161 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001162
Paul Bakkerbd51b262014-07-10 15:26:12 +02001163exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001165}
Paul Bakker33b43f12013-08-20 11:48:36 +02001166/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001167
Paul Bakker33b43f12013-08-20 11:48:36 +02001168/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001169void mbedtls_mpi_div_mpi( char * input_X, char * input_Y,
1170 char * input_A, char * input_B,
1171 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001174 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1176 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001177
Werner Lewis19b4cd82022-07-07 11:02:27 +01001178 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1179 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1180 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1181 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001183 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001184 if( res == 0 )
1185 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001186 TEST_ASSERT( sign_is_valid( &Q ) );
1187 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1189 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001190 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001191
Paul Bakkerbd51b262014-07-10 15:26:12 +02001192exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1194 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001195}
Paul Bakker33b43f12013-08-20 11:48:36 +02001196/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001197
Paul Bakker33b43f12013-08-20 11:48:36 +02001198/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001199void mbedtls_mpi_div_int( char * input_X, int input_Y,
Werner Lewisefda01f2022-07-06 13:03:36 +01001200 char * input_A, char * input_B,
1201 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001204 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1206 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001207
Werner Lewis19b4cd82022-07-07 11:02:27 +01001208 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1209 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1210 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001212 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001213 if( res == 0 )
1214 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001215 TEST_ASSERT( sign_is_valid( &Q ) );
1216 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1218 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001219 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001220
Paul Bakkerbd51b262014-07-10 15:26:12 +02001221exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1223 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001224}
Paul Bakker33b43f12013-08-20 11:48:36 +02001225/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001226
Paul Bakker33b43f12013-08-20 11:48:36 +02001227/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001228void mbedtls_mpi_mod_mpi( char * input_X, char * input_Y,
1229 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001230{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001232 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001234
Werner Lewis19b4cd82022-07-07 11:02:27 +01001235 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1236 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1237 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001239 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001240 if( res == 0 )
1241 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001242 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001244 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001245
Paul Bakkerbd51b262014-07-10 15:26:12 +02001246exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001248}
Paul Bakker33b43f12013-08-20 11:48:36 +02001249/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001250
Paul Bakker33b43f12013-08-20 11:48:36 +02001251/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001252void mbedtls_mpi_mod_int( char * input_X, int input_Y,
Azim Khanf1aaec92017-05-30 14:23:15 +01001253 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001254{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001256 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_mpi_uint r;
1258 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001259
Werner Lewis19b4cd82022-07-07 11:02:27 +01001260 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001262 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001263 if( res == 0 )
1264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001266 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001267
Paul Bakkerbd51b262014-07-10 15:26:12 +02001268exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001270}
Paul Bakker33b43f12013-08-20 11:48:36 +02001271/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001272
Paul Bakker33b43f12013-08-20 11:48:36 +02001273/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001274void mbedtls_mpi_exp_mod( char * input_A, char * input_E,
1275 char * input_N, char * input_X,
1276 int exp_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001277{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001279 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1281 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001282
Werner Lewis19b4cd82022-07-07 11:02:27 +01001283 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1284 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
1285 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
1286 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001287
Gilles Peskine342f71b2021-06-09 18:31:35 +02001288 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001289 TEST_ASSERT( res == exp_result );
Gilles Peskine342f71b2021-06-09 18:31:35 +02001290 if( res == 0 )
1291 {
1292 TEST_ASSERT( sign_is_valid( &Z ) );
1293 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1294 }
1295
1296 /* Now test again with the speed-up parameter supplied as an output. */
1297 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001298 TEST_ASSERT( res == exp_result );
Gilles Peskine342f71b2021-06-09 18:31:35 +02001299 if( res == 0 )
1300 {
1301 TEST_ASSERT( sign_is_valid( &Z ) );
1302 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1303 }
1304
1305 /* Now test again with the speed-up parameter supplied in calculated form. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001307 TEST_ASSERT( res == exp_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001308 if( res == 0 )
1309 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001310 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001312 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001313
Paul Bakkerbd51b262014-07-10 15:26:12 +02001314exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1316 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001317}
Paul Bakker33b43f12013-08-20 11:48:36 +02001318/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001319
Paul Bakker33b43f12013-08-20 11:48:36 +02001320/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001321void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Werner Lewis9802d362022-07-07 11:37:24 +01001322 char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001323{
1324 mbedtls_mpi A, E, N, RR, Z;
1325 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1326 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1327
Chris Jonesaa850cd2020-12-03 11:35:41 +00001328 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001329 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001330 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001331 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001332
1333 /* Set E to 2^(E_bytes - 1) + 1 */
1334 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1335 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001336 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001337
1338 /* Set N to 2^(N_bytes - 1) + 1 */
1339 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1340 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001341 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1342
1343 if( strlen( input_RR ) )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001344 TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001345
Chris Jonesaa850cd2020-12-03 11:35:41 +00001346 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001347
1348exit:
1349 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1350 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1351}
1352/* END_CASE */
1353
1354/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001355void mbedtls_mpi_inv_mod( char * input_X, char * input_Y,
1356 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001359 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001361
Werner Lewis19b4cd82022-07-07 11:02:27 +01001362 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1363 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1364 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001366 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001367 if( res == 0 )
1368 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001369 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001371 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001372
Paul Bakkerbd51b262014-07-10 15:26:12 +02001373exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001375}
Paul Bakker33b43f12013-08-20 11:48:36 +02001376/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Werner Lewis9802d362022-07-07 11:37:24 +01001379void mbedtls_mpi_is_prime( char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001382 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001384
Werner Lewis19b4cd82022-07-07 11:02:27 +01001385 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001386 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001387 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001388
Paul Bakkerbd51b262014-07-10 15:26:12 +02001389exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001391}
Paul Bakker33b43f12013-08-20 11:48:36 +02001392/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001395void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001396 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001397{
1398 mbedtls_mpi X;
1399 int res;
1400 mbedtls_test_mpi_random rand;
1401
1402 mbedtls_mpi_init( &X );
1403 rand.data = witnesses;
1404 rand.pos = 0;
1405 rand.chunk_len = chunk_len;
1406
1407 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001408 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1409 mbedtls_test_mpi_miller_rabin_determinizer,
1410 &rand );
1411 TEST_ASSERT( res == 0 );
1412
1413 rand.data = witnesses;
1414 rand.pos = 0;
1415 rand.chunk_len = chunk_len;
1416
Janos Follatha0b67c22018-09-18 14:48:23 +01001417 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1418 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001419 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001420 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001421
1422exit:
1423 mbedtls_mpi_free( &X );
1424}
1425/* END_CASE */
1426
1427/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001428void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001429{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001431 int my_ret;
1432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001434
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001435 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1436 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001437 TEST_ASSERT( my_ret == ref_ret );
1438
1439 if( ref_ret == 0 )
1440 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001441 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001442
1443 TEST_ASSERT( actual_bits >= (size_t) bits );
1444 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001445 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001446
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001447 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1448 mbedtls_test_rnd_std_rand,
1449 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001450 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001451 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001452 /* X = ( X - 1 ) / 2 */
1453 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001454 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1455 mbedtls_test_rnd_std_rand,
1456 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001457 }
1458 }
1459
Paul Bakkerbd51b262014-07-10 15:26:12 +02001460exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001462}
1463/* END_CASE */
1464
Paul Bakker33b43f12013-08-20 11:48:36 +02001465/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001466void mbedtls_mpi_shift_l( char * input_X, int shift_X,
1467 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001468{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 mbedtls_mpi X, A;
1470 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001471
Werner Lewis19b4cd82022-07-07 11:02:27 +01001472 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1473 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001475 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001477
Paul Bakkerbd51b262014-07-10 15:26:12 +02001478exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001480}
Paul Bakker33b43f12013-08-20 11:48:36 +02001481/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001482
Paul Bakker33b43f12013-08-20 11:48:36 +02001483/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001484void mbedtls_mpi_shift_r( char * input_X, int shift_X,
1485 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 mbedtls_mpi X, A;
1488 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001489
Werner Lewis19b4cd82022-07-07 11:02:27 +01001490 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1491 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001493 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001495
Paul Bakkerbd51b262014-07-10 15:26:12 +02001496exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001498}
Paul Bakker33b43f12013-08-20 11:48:36 +02001499/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001500
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001501/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001502void mpi_fill_random( int wanted_bytes, int rng_bytes,
1503 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001504{
1505 mbedtls_mpi X;
1506 int ret;
1507 size_t bytes_left = rng_bytes;
1508 mbedtls_mpi_init( &X );
1509
Gilles Peskine422e8672021-04-02 00:02:27 +02001510 if( before != 0 )
1511 {
1512 /* Set X to sign(before) * 2^(|before|-1) */
1513 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1514 if( before < 0 )
1515 before = - before;
1516 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1517 }
1518
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001519 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1520 f_rng_bytes_left, &bytes_left );
1521 TEST_ASSERT( ret == expected_ret );
1522
1523 if( expected_ret == 0 )
1524 {
1525 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1526 * as a big-endian representation of the number. We know when
1527 * our RNG function returns null bytes, so we know how many
1528 * leading zero bytes the number has. */
1529 size_t leading_zeros = 0;
1530 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1531 leading_zeros = 1;
1532 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1533 (size_t) wanted_bytes );
1534 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001535 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001536 }
1537
1538exit:
1539 mbedtls_mpi_free( &X );
1540}
1541/* END_CASE */
1542
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001543/* BEGIN_CASE */
1544void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1545{
1546 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1547 * This function assumes that the value of bound is at least 2 and
1548 * that iterations is large enough that a one-in-2^iterations chance
1549 * effectively never occurs.
1550 */
1551
1552 mbedtls_mpi upper_bound;
1553 size_t n_bits;
1554 mbedtls_mpi result;
1555 size_t b;
1556 /* If upper_bound is small, stats[b] is the number of times the value b
1557 * has been generated. Otherwise stats[b] is the number of times a
1558 * value with bit b set has been generated. */
1559 size_t *stats = NULL;
1560 size_t stats_len;
1561 int full_stats;
1562 size_t i;
1563
1564 mbedtls_mpi_init( &upper_bound );
1565 mbedtls_mpi_init( &result );
1566
1567 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1568 bound_bytes->x, bound_bytes->len ) );
1569 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1570 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1571 * to be small enough that the probability of missing one value is
1572 * negligible given the number of iterations. It must be less than
1573 * 256 because some of the code below assumes that "small" values
1574 * fit in a byte. */
1575 if( n_bits <= 5 )
1576 {
1577 full_stats = 1;
1578 stats_len = bound_bytes->x[bound_bytes->len - 1];
1579 }
1580 else
1581 {
1582 full_stats = 0;
1583 stats_len = n_bits;
1584 }
1585 ASSERT_ALLOC( stats, stats_len );
1586
1587 for( i = 0; i < (size_t) iterations; i++ )
1588 {
1589 mbedtls_test_set_step( i );
1590 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1591 mbedtls_test_rnd_std_rand, NULL ) );
1592
Gilles Peskinedffc7102021-06-10 15:34:15 +02001593 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001594 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1595 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1596 if( full_stats )
1597 {
1598 uint8_t value;
1599 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1600 TEST_ASSERT( value < stats_len );
1601 ++stats[value];
1602 }
1603 else
1604 {
1605 for( b = 0; b < n_bits; b++ )
1606 stats[b] += mbedtls_mpi_get_bit( &result, b );
1607 }
1608 }
1609
1610 if( full_stats )
1611 {
Gilles Peskined463edf2021-04-13 20:45:05 +02001612 for( b = min; b < stats_len; b++ )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001613 {
1614 mbedtls_test_set_step( 1000000 + b );
1615 /* Assert that each value has been reached at least once.
1616 * This is almost guaranteed if the iteration count is large
1617 * enough. This is a very crude way of checking the distribution.
1618 */
1619 TEST_ASSERT( stats[b] > 0 );
1620 }
1621 }
1622 else
1623 {
Gilles Peskineceefe5d2021-06-02 21:24:04 +02001624 int statistically_safe_all_the_way =
1625 is_significantly_above_a_power_of_2( bound_bytes );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001626 for( b = 0; b < n_bits; b++ )
1627 {
1628 mbedtls_test_set_step( 1000000 + b );
1629 /* Assert that each bit has been set in at least one result and
1630 * clear in at least one result. Provided that iterations is not
1631 * too small, it would be extremely unlikely for this not to be
1632 * the case if the results are uniformly distributed.
1633 *
1634 * As an exception, the top bit may legitimately never be set
1635 * if bound is a power of 2 or only slightly above.
1636 */
Gilles Peskineceefe5d2021-06-02 21:24:04 +02001637 if( statistically_safe_all_the_way || b != n_bits - 1 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001638 {
1639 TEST_ASSERT( stats[b] > 0 );
1640 }
1641 TEST_ASSERT( stats[b] < (size_t) iterations );
1642 }
1643 }
1644
1645exit:
1646 mbedtls_mpi_free( &upper_bound );
1647 mbedtls_mpi_free( &result );
1648 mbedtls_free( stats );
1649}
1650/* END_CASE */
1651
Gilles Peskine1e918f42021-03-29 22:14:51 +02001652/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001653void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001654{
1655 mbedtls_mpi upper_bound;
1656 mbedtls_mpi result;
1657
1658 mbedtls_mpi_init( &upper_bound );
1659 mbedtls_mpi_init( &result );
1660
Gilles Peskine422e8672021-04-02 00:02:27 +02001661 if( before != 0 )
1662 {
1663 /* Set result to sign(before) * 2^(|before|-1) */
1664 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1665 if( before < 0 )
1666 before = - before;
1667 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1668 }
1669
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001670 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1671 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1672 bound_bytes->x, bound_bytes->len ) );
1673 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1674 mbedtls_test_rnd_std_rand, NULL ) );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001675 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001676 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1677 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1678
1679exit:
1680 mbedtls_mpi_free( &upper_bound );
1681 mbedtls_mpi_free( &result );
1682}
1683/* END_CASE */
1684
1685/* BEGIN_CASE */
Gilles Peskine1e918f42021-03-29 22:14:51 +02001686void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1687{
1688 mbedtls_mpi upper_bound;
1689 mbedtls_mpi result;
1690 int actual_ret;
1691
1692 mbedtls_mpi_init( &upper_bound );
1693 mbedtls_mpi_init( &result );
1694
1695 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1696 bound_bytes->x, bound_bytes->len ) );
1697 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
1698 mbedtls_test_rnd_std_rand, NULL );
1699 TEST_EQUAL( expected_ret, actual_ret );
1700
1701exit:
1702 mbedtls_mpi_free( &upper_bound );
1703 mbedtls_mpi_free( &result );
1704}
1705/* END_CASE */
1706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001708void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001709{
Andres AG93012e82016-09-09 09:10:28 +01001710 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001711}
Paul Bakker33b43f12013-08-20 11:48:36 +02001712/* END_CASE */