blob: c3e9572f500148cd25625249447f73d70a7a9ecb [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 */
Werner Lewis9802d362022-07-07 11:37:24 +0100262void mbedtls_mpi_read_binary_le( data_t * buf, char * input_A )
Janos Follatha778a942019-02-13 10:28:28 +0000263{
264 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000265 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000266 size_t len;
267
268 mbedtls_mpi_init( &X );
269
270
271 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200272 TEST_ASSERT( sign_is_valid( &X ) );
Werner Lewisf65a3272022-07-07 11:38:44 +0100273 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdc47fe72022-08-01 13:55:41 +0100274 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000275
276exit:
277 mbedtls_mpi_free( &X );
278}
279/* END_CASE */
280
281/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100282void mbedtls_mpi_write_binary( char * input_X, data_t * input_A,
283 int output_size, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000286 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000287 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000288
289 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000292
Werner Lewis19b4cd82022-07-07 11:02:27 +0100293 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200296 if( buflen > (size_t) output_size )
297 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200300 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000301 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000302
Ronald Cron2dbba992020-06-10 11:42:32 +0200303 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
304 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000305 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000306
Paul Bakkerbd51b262014-07-10 15:26:12 +0200307exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000309}
Paul Bakker33b43f12013-08-20 11:48:36 +0200310/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000311
Janos Follathe344d0f2019-02-19 16:17:40 +0000312/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100313void mbedtls_mpi_write_binary_le( char * input_X, data_t * input_A,
314 int output_size, int result )
Janos Follathe344d0f2019-02-19 16:17:40 +0000315{
316 mbedtls_mpi X;
317 unsigned char buf[1000];
318 size_t buflen;
319
320 memset( buf, 0x00, 1000 );
321
322 mbedtls_mpi_init( &X );
323
Werner Lewis19b4cd82022-07-07 11:02:27 +0100324 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000325
326 buflen = mbedtls_mpi_size( &X );
327 if( buflen > (size_t) output_size )
328 buflen = (size_t) output_size;
329
330 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
331 if( result == 0)
332 {
333
Ronald Cron2dbba992020-06-10 11:42:32 +0200334 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
335 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000336 }
337
338exit:
339 mbedtls_mpi_free( &X );
340}
341/* END_CASE */
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Werner Lewisefda01f2022-07-06 13:03:36 +0100344void mbedtls_mpi_read_file( char * input_file, data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000345{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000347 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000348 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000349 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000350 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000351
352 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000355
Paul Bakker33b43f12013-08-20 11:48:36 +0200356 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200357 TEST_ASSERT( file != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100358 ret = mbedtls_mpi_read_file( &X, 16, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000359 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000360 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000361
Paul Bakker33b43f12013-08-20 11:48:36 +0200362 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000363 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200364 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 buflen = mbedtls_mpi_size( &X );
366 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000367
Paul Bakkere896fea2009-07-06 06:40:23 +0000368
Ronald Cron2dbba992020-06-10 11:42:32 +0200369 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
370 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000371 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000372
Paul Bakkerbd51b262014-07-10 15:26:12 +0200373exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000375}
Paul Bakker33b43f12013-08-20 11:48:36 +0200376/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Werner Lewisefda01f2022-07-06 13:03:36 +0100379void mbedtls_mpi_write_file( char * input_X, char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000380{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000382 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200383 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000386
Werner Lewis19b4cd82022-07-07 11:02:27 +0100387 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000388
Paul Bakker33b43f12013-08-20 11:48:36 +0200389 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000390 TEST_ASSERT( file_out != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100391 ret = mbedtls_mpi_write_file( NULL, &X, 16, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000392 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200393 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000394
Paul Bakker33b43f12013-08-20 11:48:36 +0200395 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000396 TEST_ASSERT( file_in != NULL );
Werner Lewisf65a3272022-07-07 11:38:44 +0100397 ret = mbedtls_mpi_read_file( &Y, 16, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000398 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200399 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000402
Paul Bakkerbd51b262014-07-10 15:26:12 +0200403exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000405}
Paul Bakker33b43f12013-08-20 11:48:36 +0200406/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000407
Paul Bakker33b43f12013-08-20 11:48:36 +0200408/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100409void mbedtls_mpi_get_bit( char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000410{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_mpi X;
412 mbedtls_mpi_init( &X );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100413 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000415
Paul Bakkerbd51b262014-07-10 15:26:12 +0200416exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000418}
Paul Bakker33b43f12013-08-20 11:48:36 +0200419/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000420
Paul Bakker33b43f12013-08-20 11:48:36 +0200421/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100422void mbedtls_mpi_set_bit( char * input_X, int pos, int val,
423 char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000424{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_mpi X, Y;
426 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000427
Werner Lewis19b4cd82022-07-07 11:02:27 +0100428 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
429 TEST_ASSERT( mbedtls_test_read_mpi( &Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100430 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
431
432 if( result == 0 )
433 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200434 TEST_ASSERT( sign_is_valid( &X ) );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100435 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
436 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000437
Paul Bakkerbd51b262014-07-10 15:26:12 +0200438exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000440}
Paul Bakker33b43f12013-08-20 11:48:36 +0200441/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000442
Paul Bakker33b43f12013-08-20 11:48:36 +0200443/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100444void mbedtls_mpi_lsb( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_mpi X;
447 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000448
Werner Lewis19b4cd82022-07-07 11:02:27 +0100449 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000451
Paul Bakkerbd51b262014-07-10 15:26:12 +0200452exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000454}
Paul Bakker33b43f12013-08-20 11:48:36 +0200455/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000456
Paul Bakker33b43f12013-08-20 11:48:36 +0200457/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100458void mbedtls_mpi_bitlen( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000459{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_mpi X;
461 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000462
Werner Lewis19b4cd82022-07-07 11:02:27 +0100463 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200464 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000465
Paul Bakkerbd51b262014-07-10 15:26:12 +0200466exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000468}
Paul Bakker33b43f12013-08-20 11:48:36 +0200469/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000470
Paul Bakker33b43f12013-08-20 11:48:36 +0200471/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100472void mbedtls_mpi_gcd( char * input_X, char * input_Y,
473 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000474{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_mpi A, X, Y, Z;
476 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000477
Werner Lewis19b4cd82022-07-07 11:02:27 +0100478 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
479 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
480 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200482 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000484
Paul Bakkerbd51b262014-07-10 15:26:12 +0200485exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000487}
Paul Bakker33b43f12013-08-20 11:48:36 +0200488/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000489
Paul Bakker33b43f12013-08-20 11:48:36 +0200490/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000492{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_mpi X;
494 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
497 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000498
Paul Bakkerbd51b262014-07-10 15:26:12 +0200499exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000501}
Paul Bakker33b43f12013-08-20 11:48:36 +0200502/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000503
Paul Bakker33b43f12013-08-20 11:48:36 +0200504/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100505void mbedtls_mpi_cmp_mpi( char * input_X, char * input_Y,
506 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_mpi X, Y;
509 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000510
Werner Lewis19b4cd82022-07-07 11:02:27 +0100511 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
512 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000514
Paul Bakkerbd51b262014-07-10 15:26:12 +0200515exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000517}
Paul Bakker33b43f12013-08-20 11:48:36 +0200518/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000519
Paul Bakker33b43f12013-08-20 11:48:36 +0200520/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100521void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
522 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100523 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100524{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200525 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100526 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100527 mbedtls_mpi X, Y;
528 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
529
Werner Lewis19b4cd82022-07-07 11:02:27 +0100530 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
531 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100532
Gilles Peskine9018b112020-01-21 16:30:53 +0100533 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
534 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100535
Janos Follath0e5532d2019-10-11 14:21:53 +0100536 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100537 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100538 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100539
540exit:
541 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
542}
543/* END_CASE */
544
545/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100546void mbedtls_mpi_cmp_abs( char * input_X, char * input_Y,
547 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_mpi X, Y;
550 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000551
Werner Lewis19b4cd82022-07-07 11:02:27 +0100552 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
553 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000555
Paul Bakkerbd51b262014-07-10 15:26:12 +0200556exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000558}
Paul Bakker33b43f12013-08-20 11:48:36 +0200559/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000560
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* BEGIN_CASE */
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200562void mbedtls_mpi_copy( char *src_hex, char *dst_hex )
Paul Bakker367dae42009-06-28 21:50:27 +0000563{
Gilles Peskined0722f82021-06-10 23:00:33 +0200564 mbedtls_mpi src, dst, ref;
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200565 mbedtls_mpi_init( &src );
566 mbedtls_mpi_init( &dst );
Gilles Peskined0722f82021-06-10 23:00:33 +0200567 mbedtls_mpi_init( &ref );
Paul Bakker367dae42009-06-28 21:50:27 +0000568
Werner Lewis19b4cd82022-07-07 11:02:27 +0100569 TEST_ASSERT( mbedtls_test_read_mpi( &src, src_hex ) == 0 );
570 TEST_ASSERT( mbedtls_test_read_mpi( &ref, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200571
572 /* mbedtls_mpi_copy() */
Werner Lewis19b4cd82022-07-07 11:02:27 +0100573 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200574 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200575 TEST_ASSERT( sign_is_valid( &dst ) );
576 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000577
Gilles Peskined0722f82021-06-10 23:00:33 +0200578 /* mbedtls_mpi_safe_cond_assign(), assignment done */
579 mbedtls_mpi_free( &dst );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100580 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200581 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
582 TEST_ASSERT( sign_is_valid( &dst ) );
583 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
584
585 /* mbedtls_mpi_safe_cond_assign(), assignment not done */
586 mbedtls_mpi_free( &dst );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100587 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200588 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
589 TEST_ASSERT( sign_is_valid( &dst ) );
590 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
591
Paul Bakkerbd51b262014-07-10 15:26:12 +0200592exit:
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200593 mbedtls_mpi_free( &src );
594 mbedtls_mpi_free( &dst );
Gilles Peskined0722f82021-06-10 23:00:33 +0200595 mbedtls_mpi_free( &ref );
Gilles Peskine7428b452020-01-20 21:01:51 +0100596}
597/* END_CASE */
598
599/* BEGIN_CASE */
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200600void mpi_copy_self( char *input_X )
Gilles Peskine7428b452020-01-20 21:01:51 +0100601{
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200602 mbedtls_mpi X, A;
603 mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000605
Werner Lewis19b4cd82022-07-07 11:02:27 +0100606 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200608
Werner Lewis19b4cd82022-07-07 11:02:27 +0100609 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_X ) == 0 );
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200610 TEST_ASSERT( sign_is_valid( &X ) );
611 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000612
Paul Bakkerbd51b262014-07-10 15:26:12 +0200613exit:
Gilles Peskine90ec8e82021-06-10 15:17:30 +0200614 mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000616}
Paul Bakker33b43f12013-08-20 11:48:36 +0200617/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000618
Paul Bakker33b43f12013-08-20 11:48:36 +0200619/* BEGIN_CASE */
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200620void mbedtls_mpi_swap( char *X_hex, char *Y_hex )
621{
622 mbedtls_mpi X, Y, X0, Y0;
623 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
624 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
625
Werner Lewis19b4cd82022-07-07 11:02:27 +0100626 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
627 TEST_ASSERT( mbedtls_test_read_mpi( &Y0, Y_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200628
Gilles Peskined0722f82021-06-10 23:00:33 +0200629 /* mbedtls_mpi_swap() */
Werner Lewis19b4cd82022-07-07 11:02:27 +0100630 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
631 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200632 mbedtls_mpi_swap( &X, &Y );
633 TEST_ASSERT( sign_is_valid( &X ) );
634 TEST_ASSERT( sign_is_valid( &Y ) );
635 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
636 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
637
Gilles Peskined0722f82021-06-10 23:00:33 +0200638 /* mbedtls_mpi_safe_cond_swap(), swap done */
639 mbedtls_mpi_free( &X );
640 mbedtls_mpi_free( &Y );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100641 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
642 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200643 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
644 TEST_ASSERT( sign_is_valid( &X ) );
645 TEST_ASSERT( sign_is_valid( &Y ) );
646 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
647 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
648
649 /* mbedtls_mpi_safe_cond_swap(), swap not done */
650 mbedtls_mpi_free( &X );
651 mbedtls_mpi_free( &Y );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100652 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
653 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined0722f82021-06-10 23:00:33 +0200654 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
655 TEST_ASSERT( sign_is_valid( &X ) );
656 TEST_ASSERT( sign_is_valid( &Y ) );
657 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
658 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
659
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200660exit:
661 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
662 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
663}
664/* END_CASE */
665
666/* BEGIN_CASE */
667void mpi_swap_self( char *X_hex )
668{
669 mbedtls_mpi X, X0;
670 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
671
Werner Lewis19b4cd82022-07-07 11:02:27 +0100672 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
673 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
Gilles Peskinefc1eeef2021-06-10 22:29:57 +0200674
675 mbedtls_mpi_swap( &X, &X );
676 TEST_ASSERT( sign_is_valid( &X ) );
677 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
678
679exit:
680 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
681}
682/* END_CASE */
683
684/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_mpi X;
688 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Gilles Peskinee1091752021-06-15 21:19:18 +0200691 if( used > 0 )
692 {
693 size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint );
694 TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 );
695 }
696 TEST_EQUAL( X.n, (size_t) before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Gilles Peskinee1091752021-06-15 21:19:18 +0200698 TEST_EQUAL( X.n, (size_t) after );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100699
Paul Bakkerbd51b262014-07-10 15:26:12 +0200700exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100702}
703/* END_CASE */
704
705/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100706void mbedtls_mpi_add_mpi( char * input_X, char * input_Y,
707 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_mpi X, Y, Z, A;
710 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000711
Werner Lewis19b4cd82022-07-07 11:02:27 +0100712 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
713 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
714 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200716 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000718
Gilles Peskine56f943a2020-07-23 01:18:11 +0200719 /* result == first operand */
720 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200721 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200722 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100723 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200724
725 /* result == second operand */
726 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200727 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200728 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
729
Paul Bakkerbd51b262014-07-10 15:26:12 +0200730exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000732}
Paul Bakker33b43f12013-08-20 11:48:36 +0200733/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000734
Paul Bakker33b43f12013-08-20 11:48:36 +0200735/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100736void mbedtls_mpi_add_mpi_inplace( char * input_X, char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100737{
738 mbedtls_mpi X, A;
739 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
740
Werner Lewis19b4cd82022-07-07 11:02:27 +0100741 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100742
Werner Lewis19b4cd82022-07-07 11:02:27 +0100743 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100744 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
745 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200746 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100747
Werner Lewis19b4cd82022-07-07 11:02:27 +0100748 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100749 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200750 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100751 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
752
Werner Lewis19b4cd82022-07-07 11:02:27 +0100753 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100754 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200755 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +0100756 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
757
758exit:
759 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
760}
761/* END_CASE */
762
763
764/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100765void mbedtls_mpi_add_abs( 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_abs( &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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200780 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200786 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000788
Paul Bakkerbd51b262014-07-10 15:26:12 +0200789exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +0200790 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000791}
Paul Bakker33b43f12013-08-20 11:48:36 +0200792/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000793
Paul Bakker33b43f12013-08-20 11:48:36 +0200794/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100795void mbedtls_mpi_add_int( char * input_X, int input_Y,
796 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000797{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_mpi X, Z, A;
799 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000800
Werner Lewis19b4cd82022-07-07 11:02:27 +0100801 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
802 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200804 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000806
Paul Bakkerbd51b262014-07-10 15:26:12 +0200807exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000809}
Paul Bakker33b43f12013-08-20 11:48:36 +0200810/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000811
Paul Bakker33b43f12013-08-20 11:48:36 +0200812/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100813void mbedtls_mpi_sub_mpi( char * input_X, char * input_Y,
814 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000815{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_mpi X, Y, Z, A;
817 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000818
Werner Lewis19b4cd82022-07-07 11:02:27 +0100819 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
820 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
821 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200823 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000825
Gilles Peskine56f943a2020-07-23 01:18:11 +0200826 /* result == first operand */
827 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200828 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200829 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100830 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200831
832 /* result == second operand */
833 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200834 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200835 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
836
Paul Bakkerbd51b262014-07-10 15:26:12 +0200837exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000839}
Paul Bakker33b43f12013-08-20 11:48:36 +0200840/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000841
Paul Bakker33b43f12013-08-20 11:48:36 +0200842/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100843void mbedtls_mpi_sub_abs( char * input_X, char * input_Y,
844 char * input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000845{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000847 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000849
Werner Lewis19b4cd82022-07-07 11:02:27 +0100850 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
851 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
852 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200855 TEST_ASSERT( res == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200856 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +0000857 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000859
Gilles Peskine56f943a2020-07-23 01:18:11 +0200860 /* result == first operand */
861 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200862 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200863 if( sub_result == 0 )
864 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis19b4cd82022-07-07 11:02:27 +0100865 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200866
867 /* result == second operand */
868 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200869 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200870 if( sub_result == 0 )
871 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
872
Paul Bakkerbd51b262014-07-10 15:26:12 +0200873exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000875}
Paul Bakker33b43f12013-08-20 11:48:36 +0200876/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000877
Paul Bakker33b43f12013-08-20 11:48:36 +0200878/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100879void mbedtls_mpi_sub_int( char * input_X, int input_Y,
880 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000881{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_mpi X, Z, A;
883 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000884
Werner Lewis19b4cd82022-07-07 11:02:27 +0100885 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
886 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200888 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000890
Paul Bakkerbd51b262014-07-10 15:26:12 +0200891exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000893}
Paul Bakker33b43f12013-08-20 11:48:36 +0200894/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000895
Paul Bakker33b43f12013-08-20 11:48:36 +0200896/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100897void mbedtls_mpi_mul_mpi( char * input_X, char * input_Y,
898 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_mpi X, Y, Z, A;
901 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000902
Werner Lewis19b4cd82022-07-07 11:02:27 +0100903 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
904 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
905 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200907 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000909
Paul Bakkerbd51b262014-07-10 15:26:12 +0200910exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000912}
Paul Bakker33b43f12013-08-20 11:48:36 +0200913/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000914
Paul Bakker33b43f12013-08-20 11:48:36 +0200915/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100916void mbedtls_mpi_mul_int( char * input_X, int input_Y,
Werner Lewisefda01f2022-07-06 13:03:36 +0100917 char * input_A, char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000918{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_mpi X, Z, A;
920 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000921
Werner Lewis19b4cd82022-07-07 11:02:27 +0100922 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
923 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +0200925 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200926 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200928 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200930 else
931 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000932
Paul Bakkerbd51b262014-07-10 15:26:12 +0200933exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000935}
Paul Bakker33b43f12013-08-20 11:48:36 +0200936/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000937
Paul Bakker33b43f12013-08-20 11:48:36 +0200938/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100939void mbedtls_mpi_div_mpi( char * input_X, char * input_Y,
940 char * input_A, char * input_B,
941 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000944 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
946 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000947
Werner Lewis19b4cd82022-07-07 11:02:27 +0100948 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
949 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
950 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
951 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200953 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000954 if( res == 0 )
955 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200956 TEST_ASSERT( sign_is_valid( &Q ) );
957 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
959 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000960 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000961
Paul Bakkerbd51b262014-07-10 15:26:12 +0200962exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
964 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000965}
Paul Bakker33b43f12013-08-20 11:48:36 +0200966/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000967
Paul Bakker33b43f12013-08-20 11:48:36 +0200968/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100969void mbedtls_mpi_div_int( char * input_X, int input_Y,
Werner Lewisefda01f2022-07-06 13:03:36 +0100970 char * input_A, char * input_B,
971 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000974 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
976 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000977
Werner Lewis19b4cd82022-07-07 11:02:27 +0100978 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
979 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
980 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200982 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000983 if( res == 0 )
984 {
Gilles Peskinedffc7102021-06-10 15:34:15 +0200985 TEST_ASSERT( sign_is_valid( &Q ) );
986 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
988 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000989 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000990
Paul Bakkerbd51b262014-07-10 15:26:12 +0200991exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
993 mbedtls_mpi_free( &B );
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_mod_mpi( char * input_X, char * input_Y,
999 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001000{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001002 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001004
Werner Lewis19b4cd82022-07-07 11:02:27 +01001005 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1006 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1007 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001009 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001010 if( res == 0 )
1011 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001012 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001014 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001015
Paul Bakkerbd51b262014-07-10 15:26:12 +02001016exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001018}
Paul Bakker33b43f12013-08-20 11:48:36 +02001019/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001020
Paul Bakker33b43f12013-08-20 11:48:36 +02001021/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001022void mbedtls_mpi_mod_int( char * input_X, int input_Y,
Azim Khanf1aaec92017-05-30 14:23:15 +01001023 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001026 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_mpi_uint r;
1028 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001029
Werner Lewis19b4cd82022-07-07 11:02:27 +01001030 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001032 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001033 if( res == 0 )
1034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001036 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001037
Paul Bakkerbd51b262014-07-10 15:26:12 +02001038exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001040}
Paul Bakker33b43f12013-08-20 11:48:36 +02001041/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001042
Paul Bakker33b43f12013-08-20 11:48:36 +02001043/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001044void mbedtls_mpi_exp_mod( char * input_A, char * input_E,
1045 char * input_N, char * input_X,
1046 int exp_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001049 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1051 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001052
Werner Lewis19b4cd82022-07-07 11:02:27 +01001053 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1054 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
1055 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
1056 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001057
Gilles Peskine342f71b2021-06-09 18:31:35 +02001058 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001059 TEST_ASSERT( res == exp_result );
Gilles Peskine342f71b2021-06-09 18:31:35 +02001060 if( res == 0 )
1061 {
1062 TEST_ASSERT( sign_is_valid( &Z ) );
1063 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1064 }
1065
1066 /* Now test again with the speed-up parameter supplied as an output. */
1067 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001068 TEST_ASSERT( res == exp_result );
Gilles Peskine342f71b2021-06-09 18:31:35 +02001069 if( res == 0 )
1070 {
1071 TEST_ASSERT( sign_is_valid( &Z ) );
1072 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1073 }
1074
1075 /* Now test again with the speed-up parameter supplied in calculated form. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine722c62c2021-06-15 21:55:05 +02001077 TEST_ASSERT( res == exp_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001078 if( res == 0 )
1079 {
Gilles Peskinedffc7102021-06-10 15:34:15 +02001080 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001082 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001083
Paul Bakkerbd51b262014-07-10 15:26:12 +02001084exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1086 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001087}
Paul Bakker33b43f12013-08-20 11:48:36 +02001088/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001089
Paul Bakker33b43f12013-08-20 11:48:36 +02001090/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001091void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Werner Lewis9802d362022-07-07 11:37:24 +01001092 char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001093{
1094 mbedtls_mpi A, E, N, RR, Z;
1095 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1096 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1097
Chris Jonesaa850cd2020-12-03 11:35:41 +00001098 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001099 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001100 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001101 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001102
1103 /* Set E to 2^(E_bytes - 1) + 1 */
1104 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1105 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001106 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001107
1108 /* Set N to 2^(N_bytes - 1) + 1 */
1109 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1110 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001111 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1112
1113 if( strlen( input_RR ) )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001114 TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001115
Chris Jonesaa850cd2020-12-03 11:35:41 +00001116 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001117
1118exit:
1119 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1120 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1121}
1122/* END_CASE */
1123
1124/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +01001125void mbedtls_mpi_inv_mod( char * input_X, char * input_Y,
1126 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001129 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001131
Werner Lewis19b4cd82022-07-07 11:02:27 +01001132 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1133 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1134 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001136 TEST_ASSERT( res == div_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, &A ) == 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( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001145}
Paul Bakker33b43f12013-08-20 11:48:36 +02001146/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Werner Lewis9802d362022-07-07 11:37:24 +01001149void mbedtls_mpi_is_prime( char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001152 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001154
Werner Lewis19b4cd82022-07-07 11:02:27 +01001155 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001156 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001157 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001158
Paul Bakkerbd51b262014-07-10 15:26:12 +02001159exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001161}
Paul Bakker33b43f12013-08-20 11:48:36 +02001162/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001165void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001166 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001167{
1168 mbedtls_mpi X;
1169 int res;
1170 mbedtls_test_mpi_random rand;
1171
1172 mbedtls_mpi_init( &X );
1173 rand.data = witnesses;
1174 rand.pos = 0;
1175 rand.chunk_len = chunk_len;
1176
1177 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001178 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1179 mbedtls_test_mpi_miller_rabin_determinizer,
1180 &rand );
1181 TEST_ASSERT( res == 0 );
1182
1183 rand.data = witnesses;
1184 rand.pos = 0;
1185 rand.chunk_len = chunk_len;
1186
Janos Follatha0b67c22018-09-18 14:48:23 +01001187 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1188 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001189 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001190 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001191
1192exit:
1193 mbedtls_mpi_free( &X );
1194}
1195/* END_CASE */
1196
1197/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001198void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001201 int my_ret;
1202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001204
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001205 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1206 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001207 TEST_ASSERT( my_ret == ref_ret );
1208
1209 if( ref_ret == 0 )
1210 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001211 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001212
1213 TEST_ASSERT( actual_bits >= (size_t) bits );
1214 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001215 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001216
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001217 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1218 mbedtls_test_rnd_std_rand,
1219 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001220 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001221 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001222 /* X = ( X - 1 ) / 2 */
1223 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001224 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1225 mbedtls_test_rnd_std_rand,
1226 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001227 }
1228 }
1229
Paul Bakkerbd51b262014-07-10 15:26:12 +02001230exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001232}
1233/* END_CASE */
1234
Paul Bakker33b43f12013-08-20 11:48:36 +02001235/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001236void mbedtls_mpi_shift_l( char * input_X, int shift_X,
1237 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 mbedtls_mpi X, A;
1240 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001241
Werner Lewis19b4cd82022-07-07 11:02:27 +01001242 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1243 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001245 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001247
Paul Bakkerbd51b262014-07-10 15:26:12 +02001248exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001250}
Paul Bakker33b43f12013-08-20 11:48:36 +02001251/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001252
Paul Bakker33b43f12013-08-20 11:48:36 +02001253/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001254void mbedtls_mpi_shift_r( char * input_X, int shift_X,
1255 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001256{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_mpi X, A;
1258 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
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 );
1261 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001263 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001265
Paul Bakkerbd51b262014-07-10 15:26:12 +02001266exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001268}
Paul Bakker33b43f12013-08-20 11:48:36 +02001269/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001270
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001271/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001272void mpi_fill_random( int wanted_bytes, int rng_bytes,
1273 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001274{
1275 mbedtls_mpi X;
1276 int ret;
1277 size_t bytes_left = rng_bytes;
1278 mbedtls_mpi_init( &X );
1279
Gilles Peskine422e8672021-04-02 00:02:27 +02001280 if( before != 0 )
1281 {
1282 /* Set X to sign(before) * 2^(|before|-1) */
1283 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1284 if( before < 0 )
1285 before = - before;
1286 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1287 }
1288
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001289 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1290 f_rng_bytes_left, &bytes_left );
1291 TEST_ASSERT( ret == expected_ret );
1292
1293 if( expected_ret == 0 )
1294 {
1295 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1296 * as a big-endian representation of the number. We know when
1297 * our RNG function returns null bytes, so we know how many
1298 * leading zero bytes the number has. */
1299 size_t leading_zeros = 0;
1300 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1301 leading_zeros = 1;
1302 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1303 (size_t) wanted_bytes );
1304 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001305 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001306 }
1307
1308exit:
1309 mbedtls_mpi_free( &X );
1310}
1311/* END_CASE */
1312
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001313/* BEGIN_CASE */
1314void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1315{
1316 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1317 * This function assumes that the value of bound is at least 2 and
1318 * that iterations is large enough that a one-in-2^iterations chance
1319 * effectively never occurs.
1320 */
1321
1322 mbedtls_mpi upper_bound;
1323 size_t n_bits;
1324 mbedtls_mpi result;
1325 size_t b;
1326 /* If upper_bound is small, stats[b] is the number of times the value b
1327 * has been generated. Otherwise stats[b] is the number of times a
1328 * value with bit b set has been generated. */
1329 size_t *stats = NULL;
1330 size_t stats_len;
1331 int full_stats;
1332 size_t i;
1333
1334 mbedtls_mpi_init( &upper_bound );
1335 mbedtls_mpi_init( &result );
1336
1337 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1338 bound_bytes->x, bound_bytes->len ) );
1339 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1340 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1341 * to be small enough that the probability of missing one value is
1342 * negligible given the number of iterations. It must be less than
1343 * 256 because some of the code below assumes that "small" values
1344 * fit in a byte. */
1345 if( n_bits <= 5 )
1346 {
1347 full_stats = 1;
1348 stats_len = bound_bytes->x[bound_bytes->len - 1];
1349 }
1350 else
1351 {
1352 full_stats = 0;
1353 stats_len = n_bits;
1354 }
1355 ASSERT_ALLOC( stats, stats_len );
1356
1357 for( i = 0; i < (size_t) iterations; i++ )
1358 {
1359 mbedtls_test_set_step( i );
1360 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1361 mbedtls_test_rnd_std_rand, NULL ) );
1362
Gilles Peskinedffc7102021-06-10 15:34:15 +02001363 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001364 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1365 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1366 if( full_stats )
1367 {
1368 uint8_t value;
1369 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1370 TEST_ASSERT( value < stats_len );
1371 ++stats[value];
1372 }
1373 else
1374 {
1375 for( b = 0; b < n_bits; b++ )
1376 stats[b] += mbedtls_mpi_get_bit( &result, b );
1377 }
1378 }
1379
1380 if( full_stats )
1381 {
Gilles Peskined463edf2021-04-13 20:45:05 +02001382 for( b = min; b < stats_len; b++ )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001383 {
1384 mbedtls_test_set_step( 1000000 + b );
1385 /* Assert that each value has been reached at least once.
1386 * This is almost guaranteed if the iteration count is large
1387 * enough. This is a very crude way of checking the distribution.
1388 */
1389 TEST_ASSERT( stats[b] > 0 );
1390 }
1391 }
1392 else
1393 {
Gilles Peskineceefe5d2021-06-02 21:24:04 +02001394 int statistically_safe_all_the_way =
1395 is_significantly_above_a_power_of_2( bound_bytes );
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001396 for( b = 0; b < n_bits; b++ )
1397 {
1398 mbedtls_test_set_step( 1000000 + b );
1399 /* Assert that each bit has been set in at least one result and
1400 * clear in at least one result. Provided that iterations is not
1401 * too small, it would be extremely unlikely for this not to be
1402 * the case if the results are uniformly distributed.
1403 *
1404 * As an exception, the top bit may legitimately never be set
1405 * if bound is a power of 2 or only slightly above.
1406 */
Gilles Peskineceefe5d2021-06-02 21:24:04 +02001407 if( statistically_safe_all_the_way || b != n_bits - 1 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001408 {
1409 TEST_ASSERT( stats[b] > 0 );
1410 }
1411 TEST_ASSERT( stats[b] < (size_t) iterations );
1412 }
1413 }
1414
1415exit:
1416 mbedtls_mpi_free( &upper_bound );
1417 mbedtls_mpi_free( &result );
1418 mbedtls_free( stats );
1419}
1420/* END_CASE */
1421
Gilles Peskine1e918f42021-03-29 22:14:51 +02001422/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001423void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001424{
1425 mbedtls_mpi upper_bound;
1426 mbedtls_mpi result;
1427
1428 mbedtls_mpi_init( &upper_bound );
1429 mbedtls_mpi_init( &result );
1430
Gilles Peskine422e8672021-04-02 00:02:27 +02001431 if( before != 0 )
1432 {
1433 /* Set result to sign(before) * 2^(|before|-1) */
1434 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1435 if( before < 0 )
1436 before = - before;
1437 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1438 }
1439
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001440 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1441 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1442 bound_bytes->x, bound_bytes->len ) );
1443 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1444 mbedtls_test_rnd_std_rand, NULL ) );
Gilles Peskinedffc7102021-06-10 15:34:15 +02001445 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001446 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1447 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1448
1449exit:
1450 mbedtls_mpi_free( &upper_bound );
1451 mbedtls_mpi_free( &result );
1452}
1453/* END_CASE */
1454
1455/* BEGIN_CASE */
Gilles Peskine1e918f42021-03-29 22:14:51 +02001456void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1457{
1458 mbedtls_mpi upper_bound;
1459 mbedtls_mpi result;
1460 int actual_ret;
1461
1462 mbedtls_mpi_init( &upper_bound );
1463 mbedtls_mpi_init( &result );
1464
1465 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1466 bound_bytes->x, bound_bytes->len ) );
1467 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
1468 mbedtls_test_rnd_std_rand, NULL );
1469 TEST_EQUAL( expected_ret, actual_ret );
1470
1471exit:
1472 mbedtls_mpi_free( &upper_bound );
1473 mbedtls_mpi_free( &result );
1474}
1475/* END_CASE */
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001478void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001479{
Andres AG93012e82016-09-09 09:10:28 +01001480 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001481}
Paul Bakker33b43f12013-08-20 11:48:36 +02001482/* END_CASE */