blob: d9a44a6e0bcc294ef72cb294eaa164b6810af697 [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 Follath64eca052018-09-05 17:04:49 +01004
Chris Jonese64a46f2020-12-03 17:44:03 +00005#if MBEDTLS_MPI_MAX_BITS > 792
6#define MPI_MAX_BITS_LARGER_THAN_792
Chris Jones4592bd82020-12-03 14:24:33 +00007#endif
Janos Follath64eca052018-09-05 17:04:49 +01008
9typedef struct mbedtls_test_mpi_random
10{
11 data_t *data;
12 size_t pos;
13 size_t chunk_len;
14} mbedtls_test_mpi_random;
15
16/*
17 * This function is called by the Miller-Rabin primality test each time it
18 * chooses a random witness. The witnesses (or non-witnesses as provided by the
19 * test) are stored in the data member of the state structure. Each number is in
20 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
21 */
22int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
23 unsigned char* buf,
24 size_t len )
25{
26 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
27
28 if( random == NULL || random->data->x == NULL || buf == NULL )
29 return( -1 );
30
31 if( random->pos + random->chunk_len > random->data->len
32 || random->chunk_len > len )
33 {
34 return( -1 );
35 }
36
37 memset( buf, 0, len );
38
39 /* The witness is written to the end of the buffer, since the buffer is
40 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
41 * Writing the witness to the start of the buffer would result in the
42 * buffer being 'witness 000...000', which would be treated as
43 * witness * 2^n for some n. */
44 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
45 random->chunk_len );
46
47 random->pos += random->chunk_len;
48
49 return( 0 );
50}
Gilles Peskine3cb1e292020-11-25 15:37:20 +010051
52/* Random generator that is told how many bytes to return. */
53static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
54{
55 size_t *bytes_left = state;
56 size_t i;
57 for( i = 0; i < len; i++ )
58 {
59 if( *bytes_left == 0 )
60 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
61 buf[i] = *bytes_left & 0xff;
62 --( *bytes_left );
63 }
64 return( 0 );
65}
66
Paul Bakker33b43f12013-08-20 11:48:36 +020067/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000068
Paul Bakker33b43f12013-08-20 11:48:36 +020069/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020071 * END_DEPENDENCIES
72 */
Paul Bakker5690efc2011-05-26 13:16:06 +000073
Hanno Beckerb48e1aa2018-12-18 23:25:01 +000074/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010075void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020076{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020077 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020078
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020079 mbedtls_mpi_init( &X );
80 mbedtls_mpi_init( &Y );
81 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020082
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020083 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
84 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +020085 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020086 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020087
88exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020089 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020090}
91/* END_CASE */
92
93/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010094void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
95 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +020096 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +000097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +000099 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100100 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000103
Janos Follath04dadb72019-03-06 12:29:37 +0000104 memset( str, '!', sizeof( str ) );
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200107 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000108 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100109 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200110 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000111 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200112 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000113 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000114 }
115 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000116
Paul Bakkerbd51b262014-07-10 15:26:12 +0200117exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000119}
Paul Bakker33b43f12013-08-20 11:48:36 +0200120/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000121
Paul Bakker33b43f12013-08-20 11:48:36 +0200122/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100123void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000126 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100127 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000130
Paul Bakkere896fea2009-07-06 06:40:23 +0000131
Azim Khand30ca132017-06-09 04:32:58 +0100132 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000133 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200134 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000135
Paul Bakkerbd51b262014-07-10 15:26:12 +0200136exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000138}
Paul Bakker33b43f12013-08-20 11:48:36 +0200139/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000140
Paul Bakker33b43f12013-08-20 11:48:36 +0200141/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000142void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
143{
144 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000145 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000146 size_t len;
147
148 mbedtls_mpi_init( &X );
149
150
151 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000152 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000153 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
154
155exit:
156 mbedtls_mpi_free( &X );
157}
158/* END_CASE */
159
160/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100161void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100162 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100163 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000166 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000167 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000168
169 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200176 if( buflen > (size_t) output_size )
177 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200180 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000181 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000182
Ronald Cron2dbba992020-06-10 11:42:32 +0200183 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
184 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000185 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000186
Paul Bakkerbd51b262014-07-10 15:26:12 +0200187exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000189}
Paul Bakker33b43f12013-08-20 11:48:36 +0200190/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000191
Janos Follathe344d0f2019-02-19 16:17:40 +0000192/* BEGIN_CASE */
193void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
194 data_t * input_A, int output_size,
195 int result )
196{
197 mbedtls_mpi X;
198 unsigned char buf[1000];
199 size_t buflen;
200
201 memset( buf, 0x00, 1000 );
202
203 mbedtls_mpi_init( &X );
204
205 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
206
207 buflen = mbedtls_mpi_size( &X );
208 if( buflen > (size_t) output_size )
209 buflen = (size_t) output_size;
210
211 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
212 if( result == 0)
213 {
214
Ronald Cron2dbba992020-06-10 11:42:32 +0200215 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
216 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000217 }
218
219exit:
220 mbedtls_mpi_free( &X );
221}
222/* END_CASE */
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100225void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100226 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000229 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000230 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000231 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000232 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000233
234 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000237
Paul Bakker33b43f12013-08-20 11:48:36 +0200238 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200239 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000241 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000242 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000243
Paul Bakker33b43f12013-08-20 11:48:36 +0200244 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 buflen = mbedtls_mpi_size( &X );
247 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000248
Paul Bakkere896fea2009-07-06 06:40:23 +0000249
Ronald Cron2dbba992020-06-10 11:42:32 +0200250 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
251 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000252 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000253
Paul Bakkerbd51b262014-07-10 15:26:12 +0200254exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000256}
Paul Bakker33b43f12013-08-20 11:48:36 +0200257/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100260void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
261 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000264 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200265 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000270
Paul Bakker33b43f12013-08-20 11:48:36 +0200271 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000272 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200273 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000274 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200275 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000276
Paul Bakker33b43f12013-08-20 11:48:36 +0200277 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000278 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200279 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000280 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200281 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000284
Paul Bakkerbd51b262014-07-10 15:26:12 +0200285exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000287}
Paul Bakker33b43f12013-08-20 11:48:36 +0200288/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000289
Paul Bakker33b43f12013-08-20 11:48:36 +0200290/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100291void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000292{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_mpi X;
294 mbedtls_mpi_init( &X );
295 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
296 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000297
Paul Bakkerbd51b262014-07-10 15:26:12 +0200298exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000300}
Paul Bakker33b43f12013-08-20 11:48:36 +0200301/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000302
Paul Bakker33b43f12013-08-20 11:48:36 +0200303/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100304void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
305 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_mpi X, Y;
308 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
311 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100312 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
313
314 if( result == 0 )
315 {
316 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
317 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000318
Paul Bakkerbd51b262014-07-10 15:26:12 +0200319exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000321}
Paul Bakker33b43f12013-08-20 11:48:36 +0200322/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000323
Paul Bakker33b43f12013-08-20 11:48:36 +0200324/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100325void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_mpi X;
328 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
331 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000332
Paul Bakkerbd51b262014-07-10 15:26:12 +0200333exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000335}
Paul Bakker33b43f12013-08-20 11:48:36 +0200336/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000337
Paul Bakker33b43f12013-08-20 11:48:36 +0200338/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100339void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_mpi X;
342 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200345 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000346
Paul Bakkerbd51b262014-07-10 15:26:12 +0200347exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000349}
Paul Bakker33b43f12013-08-20 11:48:36 +0200350/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000351
Paul Bakker33b43f12013-08-20 11:48:36 +0200352/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100353void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
354 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000355{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_mpi A, X, Y, Z;
357 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
360 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
361 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
362 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
363 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000364
Paul Bakkerbd51b262014-07-10 15:26:12 +0200365exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000367}
Paul Bakker33b43f12013-08-20 11:48:36 +0200368/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000369
Paul Bakker33b43f12013-08-20 11:48:36 +0200370/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_mpi X;
374 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
377 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000378
Paul Bakkerbd51b262014-07-10 15:26:12 +0200379exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000381}
Paul Bakker33b43f12013-08-20 11:48:36 +0200382/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000383
Paul Bakker33b43f12013-08-20 11:48:36 +0200384/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100385void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
386 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_mpi X, Y;
389 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
392 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
393 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000394
Paul Bakkerbd51b262014-07-10 15:26:12 +0200395exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000397}
Paul Bakker33b43f12013-08-20 11:48:36 +0200398/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000399
Paul Bakker33b43f12013-08-20 11:48:36 +0200400/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100401void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
402 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100403 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100404{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200405 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100406 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100407 mbedtls_mpi X, Y;
408 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
409
Janos Follathb7e1b492019-10-14 09:21:49 +0100410 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
411 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100412
Gilles Peskine9018b112020-01-21 16:30:53 +0100413 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
414 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100415
Janos Follath0e5532d2019-10-11 14:21:53 +0100416 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100417 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100418 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100419
420exit:
421 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
422}
423/* END_CASE */
424
425/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100426void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
427 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000428{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_mpi X, Y;
430 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
433 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
434 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000435
Paul Bakkerbd51b262014-07-10 15:26:12 +0200436exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000438}
Paul Bakker33b43f12013-08-20 11:48:36 +0200439/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000440
Paul Bakker33b43f12013-08-20 11:48:36 +0200441/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100442void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000443{
Gilles Peskine7428b452020-01-20 21:01:51 +0100444 mbedtls_mpi X, Y;
445 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100448 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100451 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
452 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000453
Paul Bakkerbd51b262014-07-10 15:26:12 +0200454exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100455 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
456}
457/* END_CASE */
458
459/* BEGIN_CASE */
460void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
461{
462 mbedtls_mpi X, Y, X0;
463 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
464
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100465 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
466 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
467 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100468 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
469
470 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
471 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
472 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
473
474exit:
475 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000476}
Paul Bakker33b43f12013-08-20 11:48:36 +0200477/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000478
Paul Bakker33b43f12013-08-20 11:48:36 +0200479/* BEGIN_CASE */
480void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000481{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_mpi X;
483 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
486 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
487 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000488
Paul Bakkerbd51b262014-07-10 15:26:12 +0200489exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000491}
Paul Bakker33b43f12013-08-20 11:48:36 +0200492/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000493
Paul Bakker33b43f12013-08-20 11:48:36 +0200494/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_mpi X;
498 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100501 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
503 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100504 TEST_ASSERT( X.n == (size_t) after );
505
Paul Bakkerbd51b262014-07-10 15:26:12 +0200506exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100508}
509/* END_CASE */
510
511/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100512void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
513 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100514{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_mpi X, Y, XX;
516 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100519 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100521 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
525 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
528 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100529
Paul Bakkerbd51b262014-07-10 15:26:12 +0200530exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100532}
533/* END_CASE */
534
535/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100536void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
537 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
542 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100545 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100547 Y.s = y_sign;
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
550 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
553 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
554 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
557 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
558 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100559
Paul Bakkerbd51b262014-07-10 15:26:12 +0200560exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
562 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100563}
564/* END_CASE */
565
566/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100567void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000568{
Gilles Peskine7428b452020-01-20 21:01:51 +0100569 mbedtls_mpi X, Y;
570 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
573 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100574 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
575 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine7428b452020-01-20 21:01:51 +0100578 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
579 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000580
Paul Bakkerbd51b262014-07-10 15:26:12 +0200581exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100582 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
583}
584/* END_CASE */
585
586/* BEGIN_CASE */
587void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
588{
589 mbedtls_mpi X, Y, X0, Y0;
590 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
591 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
592
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100593 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
594 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
595 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
596 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100597
598 mbedtls_mpi_swap( &X, &Y );
599 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
600 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
601
602exit:
603 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
604 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
605}
606/* END_CASE */
607
608/* BEGIN_CASE */
609void mpi_swap_self( data_t *input_X )
610{
611 mbedtls_mpi X, X0;
612 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
613
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100614 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
615 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100616
617 mbedtls_mpi_swap( &X, &X );
618 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
619
620exit:
621 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000622}
Paul Bakker33b43f12013-08-20 11:48:36 +0200623/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000624
Paul Bakker33b43f12013-08-20 11:48:36 +0200625/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100626void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
627 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000628{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_mpi X, Y, Z, A;
630 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
633 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
634 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
635 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
636 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000637
Gilles Peskine56f943a2020-07-23 01:18:11 +0200638 /* result == first operand */
639 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
640 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
641 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
642
643 /* result == second operand */
644 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
645 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
646
Paul Bakkerbd51b262014-07-10 15:26:12 +0200647exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000649}
Paul Bakker33b43f12013-08-20 11:48:36 +0200650/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000651
Paul Bakker33b43f12013-08-20 11:48:36 +0200652/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100653void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
654 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100655{
656 mbedtls_mpi X, A;
657 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
658
Janos Follath044a86b2015-10-25 10:58:03 +0100659 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100660
661 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
662 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
663 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
664
665 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
666 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
667 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
668
669 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100670 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
671 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
672
673exit:
674 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
675}
676/* END_CASE */
677
678
679/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100680void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
681 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000682{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_mpi X, Y, Z, A;
684 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
687 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
688 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
689 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
690 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000691
Gilles Peskine56f943a2020-07-23 01:18:11 +0200692 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
694 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200696
697 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
699 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000700
Paul Bakkerbd51b262014-07-10 15:26:12 +0200701exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +0200702 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000703}
Paul Bakker33b43f12013-08-20 11:48:36 +0200704/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000705
Paul Bakker33b43f12013-08-20 11:48:36 +0200706/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100707void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
708 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000709{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_mpi X, Z, A;
711 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
714 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
715 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
716 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000717
Paul Bakkerbd51b262014-07-10 15:26:12 +0200718exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000720}
Paul Bakker33b43f12013-08-20 11:48:36 +0200721/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000722
Paul Bakker33b43f12013-08-20 11:48:36 +0200723/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100724void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
725 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_mpi X, Y, Z, A;
728 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
731 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
732 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
733 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
734 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000735
Gilles Peskine56f943a2020-07-23 01:18:11 +0200736 /* result == first operand */
737 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
738 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
739 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
740
741 /* result == second operand */
742 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
743 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
744
Paul Bakkerbd51b262014-07-10 15:26:12 +0200745exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000747}
Paul Bakker33b43f12013-08-20 11:48:36 +0200748/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000749
Paul Bakker33b43f12013-08-20 11:48:36 +0200750/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100751void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
752 char * input_Y, int radix_A, char * input_A,
753 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000754{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000756 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
760 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
761 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200764 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000765 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000767
Gilles Peskine56f943a2020-07-23 01:18:11 +0200768 /* result == first operand */
769 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
770 if( sub_result == 0 )
771 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
772 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
773
774 /* result == second operand */
775 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
776 if( sub_result == 0 )
777 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
778
Paul Bakkerbd51b262014-07-10 15:26:12 +0200779exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000781}
Paul Bakker33b43f12013-08-20 11:48:36 +0200782/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000783
Paul Bakker33b43f12013-08-20 11:48:36 +0200784/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100785void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
786 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_mpi X, Z, A;
789 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
792 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
793 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
794 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000795
Paul Bakkerbd51b262014-07-10 15:26:12 +0200796exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000798}
Paul Bakker33b43f12013-08-20 11:48:36 +0200799/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000800
Paul Bakker33b43f12013-08-20 11:48:36 +0200801/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100802void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
803 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000804{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_mpi X, Y, Z, A;
806 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
809 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
810 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
811 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
812 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000813
Paul Bakkerbd51b262014-07-10 15:26:12 +0200814exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000816}
Paul Bakker33b43f12013-08-20 11:48:36 +0200817/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000818
Paul Bakker33b43f12013-08-20 11:48:36 +0200819/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100820void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
821 int radix_A, char * input_A,
822 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000823{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_mpi X, Z, A;
825 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
828 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
829 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200830 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200832 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200834 else
835 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000836
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( &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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100843void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
844 char * input_Y, int radix_A, char * input_A,
845 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000848 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
850 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
853 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
854 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
855 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
856 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200857 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000858 if( res == 0 )
859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
861 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000862 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000863
Paul Bakkerbd51b262014-07-10 15:26:12 +0200864exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
866 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000867}
Paul Bakker33b43f12013-08-20 11:48:36 +0200868/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000869
Paul Bakker33b43f12013-08-20 11:48:36 +0200870/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100871void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
872 int radix_A, char * input_A, int radix_B,
873 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000874{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000876 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
878 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
881 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
882 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
883 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200884 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000885 if( res == 0 )
886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
888 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000889 }
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( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
893 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000894}
Paul Bakker33b43f12013-08-20 11:48:36 +0200895/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000896
Paul Bakker33b43f12013-08-20 11:48:36 +0200897/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100898void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
899 char * input_Y, int radix_A, char * input_A,
900 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000901{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000903 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
907 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
908 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
909 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200910 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000911 if( res == 0 )
912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000914 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000915
Paul Bakkerbd51b262014-07-10 15:26:12 +0200916exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000918}
Paul Bakker33b43f12013-08-20 11:48:36 +0200919/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000920
Paul Bakker33b43f12013-08-20 11:48:36 +0200921/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100922void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
923 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000926 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 mbedtls_mpi_uint r;
928 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
931 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200932 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000933 if( res == 0 )
934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000936 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000937
Paul Bakkerbd51b262014-07-10 15:26:12 +0200938exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000940}
Paul Bakker33b43f12013-08-20 11:48:36 +0200941/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000942
Paul Bakker33b43f12013-08-20 11:48:36 +0200943/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100944void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
945 char * input_E, int radix_N, char * input_N,
946 int radix_RR, char * input_RR, int radix_X,
947 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000948{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000950 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
952 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
955 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
956 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
957 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000958
Paul Bakker33b43f12013-08-20 11:48:36 +0200959 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +0200963 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000964 if( res == 0 )
965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000967 }
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( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
971 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000972}
Paul Bakker33b43f12013-08-20 11:48:36 +0200973/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000974
Paul Bakker33b43f12013-08-20 11:48:36 +0200975/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +0000976void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesaa850cd2020-12-03 11:35:41 +0000977 int radix_RR, char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +0000978{
979 mbedtls_mpi A, E, N, RR, Z;
980 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
981 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
982
Chris Jonesaa850cd2020-12-03 11:35:41 +0000983 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +0000984 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +0000985 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +0000986 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +0000987
988 /* Set E to 2^(E_bytes - 1) + 1 */
989 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
990 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +0000991 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +0000992
993 /* Set N to 2^(N_bytes - 1) + 1 */
994 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
995 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +0000996 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
997
998 if( strlen( input_RR ) )
999 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
1000
Chris Jonesaa850cd2020-12-03 11:35:41 +00001001 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001002
1003exit:
1004 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1005 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1006}
1007/* END_CASE */
1008
1009/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001010void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1011 char * input_Y, int radix_A, char * input_A,
1012 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001015 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1019 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1020 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1021 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001022 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001023 if( res == 0 )
1024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001026 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001027
Paul Bakkerbd51b262014-07-10 15:26:12 +02001028exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001030}
Paul Bakker33b43f12013-08-20 11:48:36 +02001031/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001034void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001035{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001037 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001041 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001042 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001043
Paul Bakkerbd51b262014-07-10 15:26:12 +02001044exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001046}
Paul Bakker33b43f12013-08-20 11:48:36 +02001047/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001050void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001051 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001052{
1053 mbedtls_mpi X;
1054 int res;
1055 mbedtls_test_mpi_random rand;
1056
1057 mbedtls_mpi_init( &X );
1058 rand.data = witnesses;
1059 rand.pos = 0;
1060 rand.chunk_len = chunk_len;
1061
1062 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001063 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1064 mbedtls_test_mpi_miller_rabin_determinizer,
1065 &rand );
1066 TEST_ASSERT( res == 0 );
1067
1068 rand.data = witnesses;
1069 rand.pos = 0;
1070 rand.chunk_len = chunk_len;
1071
Janos Follatha0b67c22018-09-18 14:48:23 +01001072 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1073 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001074 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001075 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001076
1077exit:
1078 mbedtls_mpi_free( &X );
1079}
1080/* END_CASE */
1081
1082/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001083void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001086 int my_ret;
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001089
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001090 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1091 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001092 TEST_ASSERT( my_ret == ref_ret );
1093
1094 if( ref_ret == 0 )
1095 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001096 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001097
1098 TEST_ASSERT( actual_bits >= (size_t) bits );
1099 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1100
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001101 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1102 mbedtls_test_rnd_std_rand,
1103 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001104 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001105 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001106 /* X = ( X - 1 ) / 2 */
1107 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001108 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1109 mbedtls_test_rnd_std_rand,
1110 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001111 }
1112 }
1113
Paul Bakkerbd51b262014-07-10 15:26:12 +02001114exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001116}
1117/* END_CASE */
1118
Paul Bakker33b43f12013-08-20 11:48:36 +02001119/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001120void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1121 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 mbedtls_mpi X, A;
1124 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1127 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1128 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1129 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001130
Paul Bakkerbd51b262014-07-10 15:26:12 +02001131exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001133}
Paul Bakker33b43f12013-08-20 11:48:36 +02001134/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001135
Paul Bakker33b43f12013-08-20 11:48:36 +02001136/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001137void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1138 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_mpi X, A;
1141 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1144 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1145 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1146 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001147
Paul Bakkerbd51b262014-07-10 15:26:12 +02001148exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001150}
Paul Bakker33b43f12013-08-20 11:48:36 +02001151/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001152
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001153/* BEGIN_CASE */
1154void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1155{
1156 mbedtls_mpi X;
1157 int ret;
1158 size_t bytes_left = rng_bytes;
1159 mbedtls_mpi_init( &X );
1160
1161 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1162 f_rng_bytes_left, &bytes_left );
1163 TEST_ASSERT( ret == expected_ret );
1164
1165 if( expected_ret == 0 )
1166 {
1167 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1168 * as a big-endian representation of the number. We know when
1169 * our RNG function returns null bytes, so we know how many
1170 * leading zero bytes the number has. */
1171 size_t leading_zeros = 0;
1172 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1173 leading_zeros = 1;
1174 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1175 (size_t) wanted_bytes );
1176 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
1177 }
1178
1179exit:
1180 mbedtls_mpi_free( &X );
1181}
1182/* END_CASE */
1183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001185void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001186{
Andres AG93012e82016-09-09 09:10:28 +01001187 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001188}
Paul Bakker33b43f12013-08-20 11:48:36 +02001189/* END_CASE */