blob: 34710c430d2cf516a537211fcac0b56f73ab16a8 [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"
Janos Follath64eca052018-09-05 17:04:49 +01005
Chris Jonese64a46f2020-12-03 17:44:03 +00006#if MBEDTLS_MPI_MAX_BITS > 792
7#define MPI_MAX_BITS_LARGER_THAN_792
Chris Jones4592bd82020-12-03 14:24:33 +00008#endif
Janos Follath64eca052018-09-05 17:04:49 +01009
Gilles Peskinedffc7102021-06-10 15:34:15 +020010/* Check the validity of the sign bit in an MPI object. Reject representations
11 * that are not supported by the rest of the library and indicate a bug when
12 * constructing the value. */
13static int sign_is_valid( const mbedtls_mpi *X )
14{
15 if( X->s != 1 && X->s != -1 )
16 return( 0 ); // invalid sign bit, e.g. 0
17 if( mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 )
18 return( 0 ); // negative zero
19 return( 1 );
20}
21
Janos Follath64eca052018-09-05 17:04:49 +010022typedef struct mbedtls_test_mpi_random
23{
24 data_t *data;
25 size_t pos;
26 size_t chunk_len;
27} mbedtls_test_mpi_random;
28
29/*
30 * This function is called by the Miller-Rabin primality test each time it
31 * chooses a random witness. The witnesses (or non-witnesses as provided by the
32 * test) are stored in the data member of the state structure. Each number is in
33 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
34 */
35int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
36 unsigned char* buf,
37 size_t len )
38{
39 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
40
41 if( random == NULL || random->data->x == NULL || buf == NULL )
42 return( -1 );
43
44 if( random->pos + random->chunk_len > random->data->len
45 || random->chunk_len > len )
46 {
47 return( -1 );
48 }
49
50 memset( buf, 0, len );
51
52 /* The witness is written to the end of the buffer, since the buffer is
53 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
54 * Writing the witness to the start of the buffer would result in the
55 * buffer being 'witness 000...000', which would be treated as
56 * witness * 2^n for some n. */
57 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
58 random->chunk_len );
59
60 random->pos += random->chunk_len;
61
62 return( 0 );
63}
Gilles Peskine3cb1e292020-11-25 15:37:20 +010064
65/* Random generator that is told how many bytes to return. */
66static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
67{
68 size_t *bytes_left = state;
69 size_t i;
70 for( i = 0; i < len; i++ )
71 {
72 if( *bytes_left == 0 )
73 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
74 buf[i] = *bytes_left & 0xff;
75 --( *bytes_left );
76 }
77 return( 0 );
78}
79
Gilles Peskineeedefa52021-04-13 19:50:04 +020080/* Test whether bytes represents (in big-endian base 256) a number b that
81 * is significantly above a power of 2. That is, b must not have a long run
82 * of unset bits after the most significant bit.
83 *
84 * Let n be the bit-size of b, i.e. the integer such that 2^n <= b < 2^{n+1}.
85 * This function returns 1 if, when drawing a number between 0 and b,
86 * the probability that this number is at least 2^n is not negligible.
87 * This probability is (b - 2^n) / b and this function checks that this
88 * number is above some threshold A. The threshold value is heuristic and
89 * based on the needs of mpi_random_many().
Gilles Peskine02ac93a2021-03-29 22:02:55 +020090 */
91static int is_significantly_above_a_power_of_2( data_t *bytes )
92{
93 const uint8_t *p = bytes->x;
94 size_t len = bytes->len;
95 unsigned x;
Gilles Peskineeedefa52021-04-13 19:50:04 +020096
97 /* Skip leading null bytes */
Gilles Peskine02ac93a2021-03-29 22:02:55 +020098 while( len > 0 && p[0] == 0 )
99 {
100 ++p;
101 --len;
102 }
Gilles Peskineeedefa52021-04-13 19:50:04 +0200103 /* 0 is not significantly above a power of 2 */
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200104 if( len == 0 )
105 return( 0 );
Gilles Peskineeedefa52021-04-13 19:50:04 +0200106 /* Extract the (up to) 2 most significant bytes */
107 if( len == 1 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200108 x = p[0];
109 else
110 x = ( p[0] << 8 ) | p[1];
111
Gilles Peskineeedefa52021-04-13 19:50:04 +0200112 /* Shift the most significant bit of x to position 8 and mask it out */
113 while( ( x & 0xfe00 ) != 0 )
114 x >>= 1;
115 x &= 0x00ff;
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200116
Gilles Peskineeedefa52021-04-13 19:50:04 +0200117 /* At this point, x = floor((b - 2^n) / 2^(n-8)). b is significantly above
118 * a power of 2 iff x is significantly above 0 compared to 2^8.
119 * Testing x >= 2^4 amounts to picking A = 1/16 in the function
120 * description above. */
121 return( x >= 0x10 );
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200122}
123
Paul Bakker33b43f12013-08-20 11:48:36 +0200124/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +0000125
Paul Bakker33b43f12013-08-20 11:48:36 +0200126/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200128 * END_DEPENDENCIES
129 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000130
Hanno Beckerb48e1aa2018-12-18 23:25:01 +0000131/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100132void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200133{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200134 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200135
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200136 mbedtls_mpi_init( &X );
137 mbedtls_mpi_init( &Y );
138 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200139
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200140 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
141 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200142 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200143 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200144
145exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200146 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200147}
148/* END_CASE */
149
150/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100151void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
152 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200153 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000156 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100157 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000160
Janos Follath04dadb72019-03-06 12:29:37 +0000161 memset( str, '!', sizeof( str ) );
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200164 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000165 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200166 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100167 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200168 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000169 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200170 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000171 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000172 }
173 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000174
Paul Bakkerbd51b262014-07-10 15:26:12 +0200175exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000177}
Paul Bakker33b43f12013-08-20 11:48:36 +0200178/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000179
Paul Bakker33b43f12013-08-20 11:48:36 +0200180/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100181void mbedtls_mpi_read_binary( data_t * buf, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000182{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000184 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100185 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000188
Paul Bakkere896fea2009-07-06 06:40:23 +0000189
Azim Khand30ca132017-06-09 04:32:58 +0100190 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200191 TEST_ASSERT( sign_is_valid( &X ) );
Werner Lewisf65a3272022-07-07 11:38:44 +0100192 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdc47fe72022-08-01 13:55:41 +0100193 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000194
Paul Bakkerbd51b262014-07-10 15:26:12 +0200195exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000197}
Paul Bakker33b43f12013-08-20 11:48:36 +0200198/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000199
Paul Bakker33b43f12013-08-20 11:48:36 +0200200/* BEGIN_CASE */
Janos Follathf1d617d2022-07-21 09:29:32 +0100201void mbedtls_mpi_core_io_be( data_t *input, int nb_int, int nx_64_int, int iret,
202 int oret )
203{
204 #define BMAX 1024
205 unsigned char buf[BMAX];
206 #define XMAX BMAX / sizeof( mbedtls_mpi_uint )
207 mbedtls_mpi_uint X[XMAX];
208 size_t nx, nb;
209 int ret;
210
211 if( iret != 0 )
212 TEST_ASSERT( oret == 0 );
213
214 TEST_ASSERT( 0 <= nb_int );
215 nb = nb_int;
216 TEST_ASSERT( nb <= BMAX );
217
218 TEST_ASSERT( 0 <= nx_64_int );
219 nx = nx_64_int;
220 /* nx_64_int is the number of 64 bit limbs, if we have 32 bit limbs we need
221 * to double the number of limbs to have the same size. */
222 if( sizeof( mbedtls_mpi_uint ) == 4 )
223 nx *= 2;
224 TEST_ASSERT( nx <= XMAX );
225
226 ret = mbedtls_mpi_core_read_be( X, nx, input->x, input->len );
227 TEST_ASSERT( ret == iret );
228
229 if( iret == 0 )
230 {
231 ret = mbedtls_mpi_core_write_be( X, nx, buf, nb );
232 TEST_ASSERT( ret == oret );
233 }
234
235 if( ( iret == 0 ) && ( oret == 0 ) )
236 {
237 if( nb > input->len )
238 {
239 size_t leading_zeroes = nb - input->len;
240 TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
241 for( size_t i = 0; i < leading_zeroes; i++ )
242 TEST_ASSERT( buf[i] == 0 );
243 }
244 else
245 {
246 size_t leading_zeroes = input->len - nb;
247 TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
248 for( size_t i = 0; i < leading_zeroes; i++ )
249 TEST_ASSERT( input->x[i] == 0 );
250 }
251 }
252
253exit:
254 ;
255
256 #undef BMAX
257 #undef XMAX
258}
259/* END_CASE */
260
261/* BEGIN_CASE */
Janos Follath6ff35362022-07-21 15:27:21 +0100262void mbedtls_mpi_core_io_le( data_t *input, int nb_int, int nx_64_int, int iret,
263 int oret )
264{
265 #define BMAX 1024
266 unsigned char buf[BMAX];
267 #define XMAX BMAX / sizeof( mbedtls_mpi_uint )
268 mbedtls_mpi_uint X[XMAX];
269 size_t nx, nb;
270 int ret;
271
272 if( iret != 0 )
273 TEST_ASSERT( oret == 0 );
274
275 TEST_ASSERT( 0 <= nb_int );
276 nb = nb_int;
277 TEST_ASSERT( nb <= BMAX );
278
279 TEST_ASSERT( 0 <= nx_64_int );
280 nx = nx_64_int;
281 /* nx_64_int is the number of 64 bit limbs, if we have 32 bit limbs we need
282 * to double the number of limbs to have the same size. */
283 if( sizeof( mbedtls_mpi_uint ) == 4 )
284 nx *= 2;
285 TEST_ASSERT( nx <= XMAX );
286
287 ret = mbedtls_mpi_core_read_le( X, nx, input->x, input->len );
288 TEST_ASSERT( ret == iret );
289
290 if( iret == 0 )
291 {
292 ret = mbedtls_mpi_core_write_le( X, nx, buf, nb );
293 TEST_ASSERT( ret == oret );
294 }
295
296 if( ( iret == 0 ) && ( oret == 0 ) )
297 {
298 if( nb > input->len )
299 {
300 TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 );
301 for( size_t i = input->len; i < nb; i++ )
302 TEST_ASSERT( buf[i] == 0 );
303 }
304 else
305 {
306 TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 );
307 for( size_t i = nb; i < input->len; i++ )
308 TEST_ASSERT( input->x[i] == 0 );
309 }
310 }
311
312exit:
313 ;
314
315 #undef BMAX
316 #undef XMAX
317}
318/* END_CASE */
319
320/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100321void mbedtls_mpi_read_binary_le( data_t * buf, char * input_A )
Janos Follatha778a942019-02-13 10:28:28 +0000322{
323 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000324 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000325 size_t len;
326
327 mbedtls_mpi_init( &X );
328
329
330 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200331 TEST_ASSERT( sign_is_valid( &X ) );
Werner Lewisf65a3272022-07-07 11:38:44 +0100332 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdc47fe72022-08-01 13:55:41 +0100333 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000334
335exit:
336 mbedtls_mpi_free( &X );
337}
338/* END_CASE */
339
340/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100341void mbedtls_mpi_write_binary( char * input_X, data_t * input_A,
342 int output_size, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000343{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000345 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000346 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000347
348 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000351
Werner Lewis19b4cd82022-07-07 11:02:27 +0100352 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200355 if( buflen > (size_t) output_size )
356 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200359 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000360 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000361
Ronald Cron2dbba992020-06-10 11:42:32 +0200362 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
363 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000364 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000365
Paul Bakkerbd51b262014-07-10 15:26:12 +0200366exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000368}
Paul Bakker33b43f12013-08-20 11:48:36 +0200369/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000370
Janos Follathe344d0f2019-02-19 16:17:40 +0000371/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100372void mbedtls_mpi_write_binary_le( char * input_X, data_t * input_A,
373 int output_size, int result )
Janos Follathe344d0f2019-02-19 16:17:40 +0000374{
375 mbedtls_mpi X;
376 unsigned char buf[1000];
377 size_t buflen;
378
379 memset( buf, 0x00, 1000 );
380
381 mbedtls_mpi_init( &X );
382
Werner Lewis19b4cd82022-07-07 11:02:27 +0100383 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000384
385 buflen = mbedtls_mpi_size( &X );
386 if( buflen > (size_t) output_size )
387 buflen = (size_t) output_size;
388
389 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
390 if( result == 0)
391 {
392
Ronald Cron2dbba992020-06-10 11:42:32 +0200393 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
394 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000395 }
396
397exit:
398 mbedtls_mpi_free( &X );
399}
400/* END_CASE */
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Werner Lewisefda01f2022-07-06 13:03:36 +0100403void mbedtls_mpi_read_file( char * input_file, data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000406 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000407 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000408 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000409 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000410
411 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000414
Paul Bakker33b43f12013-08-20 11:48:36 +0200415 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200416 TEST_ASSERT( file != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100417 ret = mbedtls_mpi_read_file( &X, 16, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000418 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000419 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000420
Paul Bakker33b43f12013-08-20 11:48:36 +0200421 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000422 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200423 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 buflen = mbedtls_mpi_size( &X );
425 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000426
Paul Bakkere896fea2009-07-06 06:40:23 +0000427
Ronald Cron2dbba992020-06-10 11:42:32 +0200428 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
429 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000430 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000431
Paul Bakkerbd51b262014-07-10 15:26:12 +0200432exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000434}
Paul Bakker33b43f12013-08-20 11:48:36 +0200435/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Werner Lewisefda01f2022-07-06 13:03:36 +0100438void mbedtls_mpi_write_file( char * input_X, char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000439{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000441 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200442 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000445
Werner Lewis19b4cd82022-07-07 11:02:27 +0100446 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000447
Paul Bakker33b43f12013-08-20 11:48:36 +0200448 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000449 TEST_ASSERT( file_out != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100450 ret = mbedtls_mpi_write_file( NULL, &X, 16, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000451 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200452 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000453
Paul Bakker33b43f12013-08-20 11:48:36 +0200454 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000455 TEST_ASSERT( file_in != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100456 ret = mbedtls_mpi_read_file( &Y, 16, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000457 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200458 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000461
Paul Bakkerbd51b262014-07-10 15:26:12 +0200462exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000464}
Paul Bakker33b43f12013-08-20 11:48:36 +0200465/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000466
Paul Bakker33b43f12013-08-20 11:48:36 +0200467/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100468void mbedtls_mpi_get_bit( char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000469{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_mpi X;
471 mbedtls_mpi_init( &X );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100472 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000474
Paul Bakkerbd51b262014-07-10 15:26:12 +0200475exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000477}
Paul Bakker33b43f12013-08-20 11:48:36 +0200478/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000479
Paul Bakker33b43f12013-08-20 11:48:36 +0200480/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100481void mbedtls_mpi_set_bit( char * input_X, int pos, int val,
482 char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_mpi X, Y;
485 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000486
Werner Lewis19b4cd82022-07-07 11:02:27 +0100487 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
488 TEST_ASSERT( mbedtls_test_read_mpi( &Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100489 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
490
491 if( result == 0 )
492 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200493 TEST_ASSERT( sign_is_valid( &X ) );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100494 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
495 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000496
Paul Bakkerbd51b262014-07-10 15:26:12 +0200497exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000499}
Paul Bakker33b43f12013-08-20 11:48:36 +0200500/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000501
Paul Bakker33b43f12013-08-20 11:48:36 +0200502/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100503void mbedtls_mpi_lsb( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000504{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_mpi X;
506 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000507
Werner Lewis19b4cd82022-07-07 11:02:27 +0100508 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000510
Paul Bakkerbd51b262014-07-10 15:26:12 +0200511exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000513}
Paul Bakker33b43f12013-08-20 11:48:36 +0200514/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000515
Paul Bakker33b43f12013-08-20 11:48:36 +0200516/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100517void mbedtls_mpi_bitlen( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_mpi X;
520 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000521
Werner Lewis19b4cd82022-07-07 11:02:27 +0100522 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200523 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000524
Paul Bakkerbd51b262014-07-10 15:26:12 +0200525exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000527}
Paul Bakker33b43f12013-08-20 11:48:36 +0200528/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000529
Paul Bakker33b43f12013-08-20 11:48:36 +0200530/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100531void mbedtls_mpi_gcd( char * input_X, char * input_Y,
532 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_mpi A, X, Y, Z;
535 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000536
Werner Lewis19b4cd82022-07-07 11:02:27 +0100537 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
538 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
539 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200541 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000543
Paul Bakkerbd51b262014-07-10 15:26:12 +0200544exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000546}
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000548
Paul Bakker33b43f12013-08-20 11:48:36 +0200549/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_mpi X;
553 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
556 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000557
Paul Bakkerbd51b262014-07-10 15:26:12 +0200558exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000560}
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000562
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100564void mbedtls_mpi_cmp_mpi( char * input_X, char * input_Y,
565 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_mpi X, Y;
568 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000569
Werner Lewis19b4cd82022-07-07 11:02:27 +0100570 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
571 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000573
Paul Bakkerbd51b262014-07-10 15:26:12 +0200574exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000576}
Paul Bakker33b43f12013-08-20 11:48:36 +0200577/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000578
Paul Bakker33b43f12013-08-20 11:48:36 +0200579/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100580void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
581 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100582 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100583{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200584 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100585 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100586 mbedtls_mpi X, Y;
587 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
588
Werner Lewis19b4cd82022-07-07 11:02:27 +0100589 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
590 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100591
Gilles Peskine9018b112020-01-21 16:30:53 +0100592 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
593 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100594
Janos Follath0e5532d2019-10-11 14:21:53 +0100595 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100596 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100597 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100598
599exit:
600 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
601}
602/* END_CASE */
603
604/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100605void mbedtls_mpi_cmp_abs( char * input_X, char * input_Y,
606 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000607{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_mpi X, Y;
609 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000610
Werner Lewis19b4cd82022-07-07 11:02:27 +0100611 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
612 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000614
Paul Bakkerbd51b262014-07-10 15:26:12 +0200615exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000617}
Paul Bakker33b43f12013-08-20 11:48:36 +0200618/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000619
Paul Bakker33b43f12013-08-20 11:48:36 +0200620/* BEGIN_CASE */
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200621void mbedtls_mpi_copy( char *src_hex, char *dst_hex )
Paul Bakker367dae42009-06-28 21:50:27 +0000622{
Gilles Peskined0722f82021-06-10 23:00:33 +0200623 mbedtls_mpi src, dst, ref;
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200624 mbedtls_mpi_init( &src );
625 mbedtls_mpi_init( &dst );
Gilles Peskined0722f82021-06-10 23:00:33 +0200626 mbedtls_mpi_init( &ref );
Paul Bakker367dae42009-06-28 21:50:27 +0000627
Werner Lewis19b4cd82022-07-07 11:02:27 +0100628 TEST_ASSERT( mbedtls_test_read_mpi( &src, src_hex ) == 0 );
629 TEST_ASSERT( mbedtls_test_read_mpi( &ref, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200630
631 /* mbedtls_mpi_copy() */
Werner Lewis19b4cd82022-07-07 11:02:27 +0100632 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200633 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200634 TEST_ASSERT( sign_is_valid( &dst ) );
635 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000636
Gilles Peskined0722f82021-06-10 23:00:33 +0200637 /* mbedtls_mpi_safe_cond_assign(), assignment done */
638 mbedtls_mpi_free( &dst );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100639 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200640 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
641 TEST_ASSERT( sign_is_valid( &dst ) );
642 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
643
644 /* mbedtls_mpi_safe_cond_assign(), assignment not done */
645 mbedtls_mpi_free( &dst );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100646 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200647 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
648 TEST_ASSERT( sign_is_valid( &dst ) );
649 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
650
Paul Bakkerbd51b262014-07-10 15:26:12 +0200651exit:
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200652 mbedtls_mpi_free( &src );
653 mbedtls_mpi_free( &dst );
Gilles Peskined0722f82021-06-10 23:00:33 +0200654 mbedtls_mpi_free( &ref );
Gilles Peskine7428b452020-01-20 21:01:51 +0100655}
656/* END_CASE */
657
658/* BEGIN_CASE */
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200659void mpi_copy_self( char *input_X )
Gilles Peskine7428b452020-01-20 21:01:51 +0100660{
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200661 mbedtls_mpi X, A;
662 mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000664
Werner Lewis19b4cd82022-07-07 11:02:27 +0100665 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200667
Werner Lewis19b4cd82022-07-07 11:02:27 +0100668 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_X ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200669 TEST_ASSERT( sign_is_valid( &X ) );
670 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000671
Paul Bakkerbd51b262014-07-10 15:26:12 +0200672exit:
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200673 mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000675}
Paul Bakker33b43f12013-08-20 11:48:36 +0200676/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000677
Paul Bakker33b43f12013-08-20 11:48:36 +0200678/* BEGIN_CASE */
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200679void mbedtls_mpi_swap( char *X_hex, char *Y_hex )
680{
681 mbedtls_mpi X, Y, X0, Y0;
682 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
683 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
684
Werner Lewis19b4cd82022-07-07 11:02:27 +0100685 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
686 TEST_ASSERT( mbedtls_test_read_mpi( &Y0, Y_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200687
Gilles Peskined0722f82021-06-10 23:00:33 +0200688 /* mbedtls_mpi_swap() */
Werner Lewis19b4cd82022-07-07 11:02:27 +0100689 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
690 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200691 mbedtls_mpi_swap( &X, &Y );
692 TEST_ASSERT( sign_is_valid( &X ) );
693 TEST_ASSERT( sign_is_valid( &Y ) );
694 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
695 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
696
Gilles Peskined0722f82021-06-10 23:00:33 +0200697 /* mbedtls_mpi_safe_cond_swap(), swap done */
698 mbedtls_mpi_free( &X );
699 mbedtls_mpi_free( &Y );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100700 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
701 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200702 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
703 TEST_ASSERT( sign_is_valid( &X ) );
704 TEST_ASSERT( sign_is_valid( &Y ) );
705 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
706 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
707
708 /* mbedtls_mpi_safe_cond_swap(), swap not done */
709 mbedtls_mpi_free( &X );
710 mbedtls_mpi_free( &Y );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100711 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
712 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200713 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
714 TEST_ASSERT( sign_is_valid( &X ) );
715 TEST_ASSERT( sign_is_valid( &Y ) );
716 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
717 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
718
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200719exit:
720 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
721 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
722}
723/* END_CASE */
724
725/* BEGIN_CASE */
726void mpi_swap_self( char *X_hex )
727{
728 mbedtls_mpi X, X0;
729 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
730
Werner Lewis19b4cd82022-07-07 11:02:27 +0100731 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
732 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200733
734 mbedtls_mpi_swap( &X, &X );
735 TEST_ASSERT( sign_is_valid( &X ) );
736 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
737
738exit:
739 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
740}
741/* END_CASE */
742
743/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_mpi X;
747 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Gilles Peskinee1091752021-06-15 21:19:18 +0200750 if( used > 0 )
751 {
752 size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint );
753 TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 );
754 }
755 TEST_EQUAL( X.n, (size_t) before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Gilles Peskinee1091752021-06-15 21:19:18 +0200757 TEST_EQUAL( X.n, (size_t) after );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100758
Paul Bakkerbd51b262014-07-10 15:26:12 +0200759exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100761}
762/* END_CASE */
763
764/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100765void mbedtls_mpi_add_mpi( char * input_X, char * input_Y,
766 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_mpi X, Y, Z, A;
769 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000770
Werner Lewis19b4cd82022-07-07 11:02:27 +0100771 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
772 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
773 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200775 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000777
Gilles Peskine56f943a2020-07-23 01:18:11 +0200778 /* result == first operand */
779 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200780 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200781 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100782 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200783
784 /* result == second operand */
785 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200786 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200787 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
788
Paul Bakkerbd51b262014-07-10 15:26:12 +0200789exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000791}
Paul Bakker33b43f12013-08-20 11:48:36 +0200792/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000793
Paul Bakker33b43f12013-08-20 11:48:36 +0200794/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100795void mbedtls_mpi_add_mpi_inplace( char * input_X, char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100796{
797 mbedtls_mpi X, A;
798 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
799
Werner Lewis19b4cd82022-07-07 11:02:27 +0100800 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100801
Werner Lewis19b4cd82022-07-07 11:02:27 +0100802 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100803 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
804 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200805 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100806
Werner Lewis19b4cd82022-07-07 11:02:27 +0100807 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100808 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200809 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100810 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
811
Werner Lewis19b4cd82022-07-07 11:02:27 +0100812 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100813 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200814 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +0100815 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
816
817exit:
818 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
819}
820/* END_CASE */
821
822
823/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100824void mbedtls_mpi_add_abs( char * input_X, char * input_Y,
825 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000826{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_mpi X, Y, Z, A;
828 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000829
Werner Lewis19b4cd82022-07-07 11:02:27 +0100830 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
831 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
832 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200834 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000836
Gilles Peskine56f943a2020-07-23 01:18:11 +0200837 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200839 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100841 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200842
843 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200845 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000847
Paul Bakkerbd51b262014-07-10 15:26:12 +0200848exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +0200849 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000850}
Paul Bakker33b43f12013-08-20 11:48:36 +0200851/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000852
Paul Bakker33b43f12013-08-20 11:48:36 +0200853/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100854void mbedtls_mpi_add_int( char * input_X, int input_Y,
855 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000856{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 mbedtls_mpi X, Z, A;
858 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000859
Werner Lewis19b4cd82022-07-07 11:02:27 +0100860 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
861 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200863 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000865
Paul Bakkerbd51b262014-07-10 15:26:12 +0200866exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000868}
Paul Bakker33b43f12013-08-20 11:48:36 +0200869/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000870
Paul Bakker33b43f12013-08-20 11:48:36 +0200871/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100872void mbedtls_mpi_sub_mpi( char * input_X, char * input_Y,
873 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000874{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_mpi X, Y, Z, A;
876 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000877
Werner Lewis19b4cd82022-07-07 11:02:27 +0100878 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
879 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
880 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200882 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000884
Gilles Peskine56f943a2020-07-23 01:18:11 +0200885 /* result == first operand */
886 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200887 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200888 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100889 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200890
891 /* result == second operand */
892 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200893 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200894 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
895
Paul Bakkerbd51b262014-07-10 15:26:12 +0200896exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000898}
Paul Bakker33b43f12013-08-20 11:48:36 +0200899/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000900
Paul Bakker33b43f12013-08-20 11:48:36 +0200901/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100902void mbedtls_mpi_sub_abs( char * input_X, char * input_Y,
903 char * input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000906 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000908
Werner Lewis19b4cd82022-07-07 11:02:27 +0100909 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
910 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
911 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200914 TEST_ASSERT( res == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200915 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +0000916 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000918
Gilles Peskine56f943a2020-07-23 01:18:11 +0200919 /* result == first operand */
920 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200921 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200922 if( sub_result == 0 )
923 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100924 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200925
926 /* result == second operand */
927 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200928 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200929 if( sub_result == 0 )
930 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
931
Paul Bakkerbd51b262014-07-10 15:26:12 +0200932exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000934}
Paul Bakker33b43f12013-08-20 11:48:36 +0200935/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000936
Paul Bakker33b43f12013-08-20 11:48:36 +0200937/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100938void mbedtls_mpi_sub_int( char * input_X, int input_Y,
939 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000940{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_mpi X, Z, A;
942 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000943
Werner Lewis19b4cd82022-07-07 11:02:27 +0100944 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
945 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200947 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000949
Paul Bakkerbd51b262014-07-10 15:26:12 +0200950exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000952}
Paul Bakker33b43f12013-08-20 11:48:36 +0200953/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000954
Paul Bakker33b43f12013-08-20 11:48:36 +0200955/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100956void mbedtls_mpi_mul_mpi( char * input_X, char * input_Y,
957 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000958{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 mbedtls_mpi X, Y, Z, A;
960 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000961
Werner Lewis19b4cd82022-07-07 11:02:27 +0100962 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
963 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
964 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200966 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000968
Paul Bakkerbd51b262014-07-10 15:26:12 +0200969exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000971}
Paul Bakker33b43f12013-08-20 11:48:36 +0200972/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000973
Paul Bakker33b43f12013-08-20 11:48:36 +0200974/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100975void mbedtls_mpi_mul_int( char * input_X, int input_Y,
Werner Lewisefda01f2022-07-06 13:03:36 +0100976 char * input_A, char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000977{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_mpi X, Z, A;
979 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000980
Werner Lewis19b4cd82022-07-07 11:02:27 +0100981 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
982 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200984 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200985 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200987 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200989 else
990 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000991
Paul Bakkerbd51b262014-07-10 15:26:12 +0200992exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000994}
Paul Bakker33b43f12013-08-20 11:48:36 +0200995/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000996
Paul Bakker33b43f12013-08-20 11:48:36 +0200997/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100998void mbedtls_mpi_div_mpi( char * input_X, char * input_Y,
999 char * input_A, char * input_B,
1000 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001001{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001003 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1005 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001006
Werner Lewis19b4cd82022-07-07 11:02:27 +01001007 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1008 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1009 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1010 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001012 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001013 if( res == 0 )
1014 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001015 TEST_ASSERT( sign_is_valid( &Q ) );
1016 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1018 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001019 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001020
Paul Bakkerbd51b262014-07-10 15:26:12 +02001021exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1023 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001024}
Paul Bakker33b43f12013-08-20 11:48:36 +02001025/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001026
Paul Bakker33b43f12013-08-20 11:48:36 +02001027/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001028void mbedtls_mpi_div_int( char * input_X, int input_Y,
Werner Lewisefda01f2022-07-06 13:03:36 +01001029 char * input_A, char * input_B,
1030 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001033 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1035 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001036
Werner Lewis19b4cd82022-07-07 11:02:27 +01001037 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1038 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1039 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001041 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001042 if( res == 0 )
1043 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001044 TEST_ASSERT( sign_is_valid( &Q ) );
1045 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1047 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001048 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001049
Paul Bakkerbd51b262014-07-10 15:26:12 +02001050exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1052 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001053}
Paul Bakker33b43f12013-08-20 11:48:36 +02001054/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001055
Paul Bakker33b43f12013-08-20 11:48:36 +02001056/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001057void mbedtls_mpi_mod_mpi( char * input_X, char * input_Y,
1058 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001061 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001063
Werner Lewis19b4cd82022-07-07 11:02:27 +01001064 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1065 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1066 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001068 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001069 if( res == 0 )
1070 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001071 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001073 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001074
Paul Bakkerbd51b262014-07-10 15:26:12 +02001075exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001077}
Paul Bakker33b43f12013-08-20 11:48:36 +02001078/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001079
Paul Bakker33b43f12013-08-20 11:48:36 +02001080/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001081void mbedtls_mpi_mod_int( char * input_X, int input_Y,
Azim Khanf1aaec92017-05-30 14:23:15 +01001082 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001085 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 mbedtls_mpi_uint r;
1087 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001088
Werner Lewis19b4cd82022-07-07 11:02:27 +01001089 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001091 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001092 if( res == 0 )
1093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001095 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001096
Paul Bakkerbd51b262014-07-10 15:26:12 +02001097exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001099}
Paul Bakker33b43f12013-08-20 11:48:36 +02001100/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001101
Paul Bakker33b43f12013-08-20 11:48:36 +02001102/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001103void mbedtls_mpi_exp_mod( char * input_A, char * input_E,
1104 char * input_N, char * input_X,
1105 int exp_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001108 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1110 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001111
Werner Lewis19b4cd82022-07-07 11:02:27 +01001112 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1113 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
1114 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
1115 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001116
Gilles Peskine342f71b2021-06-09 18:31:35 +02001117 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001118 TEST_ASSERT( res == exp_result );
Gilles Peskine342f71b2021-06-09 18:31:35 +02001119 if( res == 0 )
1120 {
1121 TEST_ASSERT( sign_is_valid( &Z ) );
1122 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1123 }
1124
1125 /* Now test again with the speed-up parameter supplied as an output. */
1126 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001127 TEST_ASSERT( res == exp_result );
Gilles Peskine342f71b2021-06-09 18:31:35 +02001128 if( res == 0 )
1129 {
1130 TEST_ASSERT( sign_is_valid( &Z ) );
1131 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1132 }
1133
1134 /* Now test again with the speed-up parameter supplied in calculated form. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001136 TEST_ASSERT( res == exp_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001137 if( res == 0 )
1138 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001139 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001141 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001142
Paul Bakkerbd51b262014-07-10 15:26:12 +02001143exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1145 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001146}
Paul Bakker33b43f12013-08-20 11:48:36 +02001147/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001148
Paul Bakker33b43f12013-08-20 11:48:36 +02001149/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001150void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Werner Lewis9802d362022-07-07 11:37:24 +01001151 char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001152{
1153 mbedtls_mpi A, E, N, RR, Z;
1154 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1155 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1156
Chris Jonesaa850cd2020-12-03 11:35:41 +00001157 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001158 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001159 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001160 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001161
1162 /* Set E to 2^(E_bytes - 1) + 1 */
1163 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1164 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001165 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001166
1167 /* Set N to 2^(N_bytes - 1) + 1 */
1168 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1169 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001170 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1171
1172 if( strlen( input_RR ) )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001173 TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001174
Chris Jonesaa850cd2020-12-03 11:35:41 +00001175 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001176
1177exit:
1178 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1179 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1180}
1181/* END_CASE */
1182
1183/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001184void mbedtls_mpi_inv_mod( char * input_X, char * input_Y,
1185 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001188 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001190
Werner Lewis19b4cd82022-07-07 11:02:27 +01001191 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1192 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1193 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001195 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001196 if( res == 0 )
1197 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001198 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001200 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001201
Paul Bakkerbd51b262014-07-10 15:26:12 +02001202exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001204}
Paul Bakker33b43f12013-08-20 11:48:36 +02001205/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Werner Lewis9802d362022-07-07 11:37:24 +01001208void mbedtls_mpi_is_prime( char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001211 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001213
Werner Lewis19b4cd82022-07-07 11:02:27 +01001214 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001215 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001216 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001217
Paul Bakkerbd51b262014-07-10 15:26:12 +02001218exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001220}
Paul Bakker33b43f12013-08-20 11:48:36 +02001221/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001224void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001225 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001226{
1227 mbedtls_mpi X;
1228 int res;
1229 mbedtls_test_mpi_random rand;
1230
1231 mbedtls_mpi_init( &X );
1232 rand.data = witnesses;
1233 rand.pos = 0;
1234 rand.chunk_len = chunk_len;
1235
1236 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001237 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1238 mbedtls_test_mpi_miller_rabin_determinizer,
1239 &rand );
1240 TEST_ASSERT( res == 0 );
1241
1242 rand.data = witnesses;
1243 rand.pos = 0;
1244 rand.chunk_len = chunk_len;
1245
Janos Follatha0b67c22018-09-18 14:48:23 +01001246 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1247 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001248 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001249 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001250
1251exit:
1252 mbedtls_mpi_free( &X );
1253}
1254/* END_CASE */
1255
1256/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001257void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001258{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001260 int my_ret;
1261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001263
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001264 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1265 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001266 TEST_ASSERT( my_ret == ref_ret );
1267
1268 if( ref_ret == 0 )
1269 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001270 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001271
1272 TEST_ASSERT( actual_bits >= (size_t) bits );
1273 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001274 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001275
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001276 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1277 mbedtls_test_rnd_std_rand,
1278 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001279 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001280 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001281 /* X = ( X - 1 ) / 2 */
1282 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001283 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1284 mbedtls_test_rnd_std_rand,
1285 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001286 }
1287 }
1288
Paul Bakkerbd51b262014-07-10 15:26:12 +02001289exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001291}
1292/* END_CASE */
1293
Paul Bakker33b43f12013-08-20 11:48:36 +02001294/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001295void mbedtls_mpi_shift_l( char * input_X, int shift_X,
1296 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_mpi X, A;
1299 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001300
Werner Lewis19b4cd82022-07-07 11:02:27 +01001301 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1302 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001304 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001306
Paul Bakkerbd51b262014-07-10 15:26:12 +02001307exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001309}
Paul Bakker33b43f12013-08-20 11:48:36 +02001310/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001311
Paul Bakker33b43f12013-08-20 11:48:36 +02001312/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001313void mbedtls_mpi_shift_r( char * input_X, int shift_X,
1314 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_mpi X, A;
1317 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001318
Werner Lewis19b4cd82022-07-07 11:02:27 +01001319 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1320 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001322 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001324
Paul Bakkerbd51b262014-07-10 15:26:12 +02001325exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001327}
Paul Bakker33b43f12013-08-20 11:48:36 +02001328/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001329
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001330/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001331void mpi_fill_random( int wanted_bytes, int rng_bytes,
1332 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001333{
1334 mbedtls_mpi X;
1335 int ret;
1336 size_t bytes_left = rng_bytes;
1337 mbedtls_mpi_init( &X );
1338
Gilles Peskine422e8672021-04-02 00:02:27 +02001339 if( before != 0 )
1340 {
1341 /* Set X to sign(before) * 2^(|before|-1) */
1342 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1343 if( before < 0 )
1344 before = - before;
1345 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1346 }
1347
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001348 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1349 f_rng_bytes_left, &bytes_left );
1350 TEST_ASSERT( ret == expected_ret );
1351
1352 if( expected_ret == 0 )
1353 {
1354 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1355 * as a big-endian representation of the number. We know when
1356 * our RNG function returns null bytes, so we know how many
1357 * leading zero bytes the number has. */
1358 size_t leading_zeros = 0;
1359 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1360 leading_zeros = 1;
1361 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1362 (size_t) wanted_bytes );
1363 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001364 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001365 }
1366
1367exit:
1368 mbedtls_mpi_free( &X );
1369}
1370/* END_CASE */
1371
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001372/* BEGIN_CASE */
1373void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1374{
1375 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1376 * This function assumes that the value of bound is at least 2 and
1377 * that iterations is large enough that a one-in-2^iterations chance
1378 * effectively never occurs.
1379 */
1380
1381 mbedtls_mpi upper_bound;
1382 size_t n_bits;
1383 mbedtls_mpi result;
1384 size_t b;
1385 /* If upper_bound is small, stats[b] is the number of times the value b
1386 * has been generated. Otherwise stats[b] is the number of times a
1387 * value with bit b set has been generated. */
1388 size_t *stats = NULL;
1389 size_t stats_len;
1390 int full_stats;
1391 size_t i;
1392
1393 mbedtls_mpi_init( &upper_bound );
1394 mbedtls_mpi_init( &result );
1395
1396 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1397 bound_bytes->x, bound_bytes->len ) );
1398 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1399 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1400 * to be small enough that the probability of missing one value is
1401 * negligible given the number of iterations. It must be less than
1402 * 256 because some of the code below assumes that "small" values
1403 * fit in a byte. */
1404 if( n_bits <= 5 )
1405 {
1406 full_stats = 1;
1407 stats_len = bound_bytes->x[bound_bytes->len - 1];
1408 }
1409 else
1410 {
1411 full_stats = 0;
1412 stats_len = n_bits;
1413 }
1414 ASSERT_ALLOC( stats, stats_len );
1415
1416 for( i = 0; i < (size_t) iterations; i++ )
1417 {
1418 mbedtls_test_set_step( i );
1419 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1420 mbedtls_test_rnd_std_rand, NULL ) );
1421
Gilles Peskinedffc7102021-06-10 15:34:15 +02001422 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001423 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1424 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1425 if( full_stats )
1426 {
1427 uint8_t value;
1428 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1429 TEST_ASSERT( value < stats_len );
1430 ++stats[value];
1431 }
1432 else
1433 {
1434 for( b = 0; b < n_bits; b++ )
1435 stats[b] += mbedtls_mpi_get_bit( &result, b );
1436 }
1437 }
1438
1439 if( full_stats )
1440 {
Gilles Peskined463edf2021-04-13 20:45:05 +02001441 for( b = min; b < stats_len; b++ )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001442 {
1443 mbedtls_test_set_step( 1000000 + b );
1444 /* Assert that each value has been reached at least once.
1445 * This is almost guaranteed if the iteration count is large
1446 * enough. This is a very crude way of checking the distribution.
1447 */
1448 TEST_ASSERT( stats[b] > 0 );
1449 }
1450 }
1451 else
1452 {
Gilles Peskineceefe5d2021-06-02 21:24:04 +02001453 int statistically_safe_all_the_way =
1454 is_significantly_above_a_power_of_2( bound_bytes );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001455 for( b = 0; b < n_bits; b++ )
1456 {
1457 mbedtls_test_set_step( 1000000 + b );
1458 /* Assert that each bit has been set in at least one result and
1459 * clear in at least one result. Provided that iterations is not
1460 * too small, it would be extremely unlikely for this not to be
1461 * the case if the results are uniformly distributed.
1462 *
1463 * As an exception, the top bit may legitimately never be set
1464 * if bound is a power of 2 or only slightly above.
1465 */
Gilles Peskineceefe5d2021-06-02 21:24:04 +02001466 if( statistically_safe_all_the_way || b != n_bits - 1 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001467 {
1468 TEST_ASSERT( stats[b] > 0 );
1469 }
1470 TEST_ASSERT( stats[b] < (size_t) iterations );
1471 }
1472 }
1473
1474exit:
1475 mbedtls_mpi_free( &upper_bound );
1476 mbedtls_mpi_free( &result );
1477 mbedtls_free( stats );
1478}
1479/* END_CASE */
1480
Gilles Peskine1e918f42021-03-29 22:14:51 +02001481/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001482void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001483{
1484 mbedtls_mpi upper_bound;
1485 mbedtls_mpi result;
1486
1487 mbedtls_mpi_init( &upper_bound );
1488 mbedtls_mpi_init( &result );
1489
Gilles Peskine422e8672021-04-02 00:02:27 +02001490 if( before != 0 )
1491 {
1492 /* Set result to sign(before) * 2^(|before|-1) */
1493 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1494 if( before < 0 )
1495 before = - before;
1496 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1497 }
1498
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001499 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1500 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1501 bound_bytes->x, bound_bytes->len ) );
1502 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1503 mbedtls_test_rnd_std_rand, NULL ) );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001504 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001505 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1506 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1507
1508exit:
1509 mbedtls_mpi_free( &upper_bound );
1510 mbedtls_mpi_free( &result );
1511}
1512/* END_CASE */
1513
1514/* BEGIN_CASE */
Gilles Peskine1e918f42021-03-29 22:14:51 +02001515void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1516{
1517 mbedtls_mpi upper_bound;
1518 mbedtls_mpi result;
1519 int actual_ret;
1520
1521 mbedtls_mpi_init( &upper_bound );
1522 mbedtls_mpi_init( &result );
1523
1524 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1525 bound_bytes->x, bound_bytes->len ) );
1526 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
1527 mbedtls_test_rnd_std_rand, NULL );
1528 TEST_EQUAL( expected_ret, actual_ret );
1529
1530exit:
1531 mbedtls_mpi_free( &upper_bound );
1532 mbedtls_mpi_free( &result );
1533}
1534/* END_CASE */
1535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001537void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001538{
Andres AG93012e82016-09-09 09:10:28 +01001539 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001540}
Paul Bakker33b43f12013-08-20 11:48:36 +02001541/* END_CASE */