blob: 95fe99cec46702c5bc403eb3915174745c7c72b3 [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"
Janos Follath64eca052018-09-05 17:04:49 +01003
4typedef struct mbedtls_test_mpi_random
5{
6 data_t *data;
7 size_t pos;
8 size_t chunk_len;
9} mbedtls_test_mpi_random;
10
11/*
12 * This function is called by the Miller-Rabin primality test each time it
13 * chooses a random witness. The witnesses (or non-witnesses as provided by the
14 * test) are stored in the data member of the state structure. Each number is in
15 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
16 */
17int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
18 unsigned char* buf,
19 size_t len )
20{
21 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
22
23 if( random == NULL || random->data->x == NULL || buf == NULL )
24 return( -1 );
25
26 if( random->pos + random->chunk_len > random->data->len
27 || random->chunk_len > len )
28 {
29 return( -1 );
30 }
31
32 memset( buf, 0, len );
33
34 /* The witness is written to the end of the buffer, since the buffer is
35 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
36 * Writing the witness to the start of the buffer would result in the
37 * buffer being 'witness 000...000', which would be treated as
38 * witness * 2^n for some n. */
39 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
40 random->chunk_len );
41
42 random->pos += random->chunk_len;
43
44 return( 0 );
45}
Paul Bakker33b43f12013-08-20 11:48:36 +020046/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000047
Paul Bakker33b43f12013-08-20 11:48:36 +020048/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020050 * END_DEPENDENCIES
51 */
Paul Bakker5690efc2011-05-26 13:16:06 +000052
Paul Bakker33b43f12013-08-20 11:48:36 +020053/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010054void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020055{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020056 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020057
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020058 mbedtls_mpi_init( &X );
59 mbedtls_mpi_init( &Y );
60 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020061
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020062 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
63 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +020064 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020065 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020066
67exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020068 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020069}
70/* END_CASE */
71
72/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010073void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
74 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +020075 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +000076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +000078 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +010079 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +000080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +000082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +020084 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +000085 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +010086 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +020087 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +000088 {
Paul Bakker33b43f12013-08-20 11:48:36 +020089 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +000090 }
91 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000092
Paul Bakkerbd51b262014-07-10 15:26:12 +020093exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +000095}
Paul Bakker33b43f12013-08-20 11:48:36 +020096/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000097
Paul Bakker33b43f12013-08-20 11:48:36 +020098/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010099void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000102 unsigned char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100103 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000106
Paul Bakkere896fea2009-07-06 06:40:23 +0000107
Azim Khand30ca132017-06-09 04:32:58 +0100108 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100109 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200110 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000111
Paul Bakkerbd51b262014-07-10 15:26:12 +0200112exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000114}
Paul Bakker33b43f12013-08-20 11:48:36 +0200115/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000116
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100118void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100119 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100120 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000123 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000124 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000125
126 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200133 if( buflen > (size_t) output_size )
134 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200137 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000138 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000139
Azim Khand30ca132017-06-09 04:32:58 +0100140 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000141 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000142
Paul Bakkerbd51b262014-07-10 15:26:12 +0200143exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000145}
Paul Bakker33b43f12013-08-20 11:48:36 +0200146/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100149void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100150 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000153 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000154 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000155 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000156 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000157
158 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000161
Paul Bakker33b43f12013-08-20 11:48:36 +0200162 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200163 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000165 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000166 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000167
Paul Bakker33b43f12013-08-20 11:48:36 +0200168 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 buflen = mbedtls_mpi_size( &X );
171 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000172
Paul Bakkere896fea2009-07-06 06:40:23 +0000173
Azim Khand30ca132017-06-09 04:32:58 +0100174 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000175 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000176
Paul Bakkerbd51b262014-07-10 15:26:12 +0200177exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000179}
Paul Bakker33b43f12013-08-20 11:48:36 +0200180/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100183void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
184 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000187 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200188 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000193
Paul Bakker33b43f12013-08-20 11:48:36 +0200194 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000195 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200196 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000197 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200198 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000199
Paul Bakker33b43f12013-08-20 11:48:36 +0200200 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000201 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200202 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000203 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200204 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000207
Paul Bakkerbd51b262014-07-10 15:26:12 +0200208exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000210}
Paul Bakker33b43f12013-08-20 11:48:36 +0200211/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000212
Paul Bakker33b43f12013-08-20 11:48:36 +0200213/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100214void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_mpi X;
217 mbedtls_mpi_init( &X );
218 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
219 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000220
Paul Bakkerbd51b262014-07-10 15:26:12 +0200221exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000223}
Paul Bakker33b43f12013-08-20 11:48:36 +0200224/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000225
Paul Bakker33b43f12013-08-20 11:48:36 +0200226/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100227void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
228 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_mpi X, Y;
231 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
234 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100235 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
236
237 if( result == 0 )
238 {
239 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
240 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000241
Paul Bakkerbd51b262014-07-10 15:26:12 +0200242exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000244}
Paul Bakker33b43f12013-08-20 11:48:36 +0200245/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000246
Paul Bakker33b43f12013-08-20 11:48:36 +0200247/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100248void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000249{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_mpi X;
251 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
254 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000255
Paul Bakkerbd51b262014-07-10 15:26:12 +0200256exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000258}
Paul Bakker33b43f12013-08-20 11:48:36 +0200259/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000260
Paul Bakker33b43f12013-08-20 11:48:36 +0200261/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100262void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_mpi X;
265 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200268 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000269
Paul Bakkerbd51b262014-07-10 15:26:12 +0200270exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000272}
Paul Bakker33b43f12013-08-20 11:48:36 +0200273/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000274
Paul Bakker33b43f12013-08-20 11:48:36 +0200275/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100276void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
277 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_mpi A, X, Y, Z;
280 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
283 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
284 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
285 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
286 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000287
Paul Bakkerbd51b262014-07-10 15:26:12 +0200288exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000290}
Paul Bakker33b43f12013-08-20 11:48:36 +0200291/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000292
Paul Bakker33b43f12013-08-20 11:48:36 +0200293/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000295{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_mpi X;
297 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
300 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000301
Paul Bakkerbd51b262014-07-10 15:26:12 +0200302exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000304}
Paul Bakker33b43f12013-08-20 11:48:36 +0200305/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000306
Paul Bakker33b43f12013-08-20 11:48:36 +0200307/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100308void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
309 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000310{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_mpi X, Y;
312 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
315 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
316 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000317
Paul Bakkerbd51b262014-07-10 15:26:12 +0200318exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000320}
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000322
Paul Bakker33b43f12013-08-20 11:48:36 +0200323/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100324void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
325 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_mpi X, Y;
328 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +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_read_string( &Y, radix_Y, input_Y ) == 0 );
332 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000333
Paul Bakkerbd51b262014-07-10 15:26:12 +0200334exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000336}
Paul Bakker33b43f12013-08-20 11:48:36 +0200337/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000338
Paul Bakker33b43f12013-08-20 11:48:36 +0200339/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340void mbedtls_mpi_copy( int input_X, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000341{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_mpi X, Y, A;
343 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
346 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_A ) == 0 );
347 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 );
348 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
349 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
350 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
351 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
352 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000353
Paul Bakkerbd51b262014-07-10 15:26:12 +0200354exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000356}
Paul Bakker33b43f12013-08-20 11:48:36 +0200357/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000358
Paul Bakker33b43f12013-08-20 11:48:36 +0200359/* BEGIN_CASE */
360void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000361{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_mpi X;
363 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
366 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
367 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000368
Paul Bakkerbd51b262014-07-10 15:26:12 +0200369exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000371}
Paul Bakker33b43f12013-08-20 11:48:36 +0200372/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000373
Paul Bakker33b43f12013-08-20 11:48:36 +0200374/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_mpi X;
378 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100381 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
383 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100384 TEST_ASSERT( X.n == (size_t) after );
385
Paul Bakkerbd51b262014-07-10 15:26:12 +0200386exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100388}
389/* END_CASE */
390
391/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100392void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
393 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_mpi X, Y, XX;
396 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100399 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100401 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
405 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
408 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100409
Paul Bakkerbd51b262014-07-10 15:26:12 +0200410exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100412}
413/* END_CASE */
414
415/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100416void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
417 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
422 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100425 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100427 Y.s = y_sign;
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
430 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
433 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
434 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
437 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
438 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100439
Paul Bakkerbd51b262014-07-10 15:26:12 +0200440exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
442 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100443}
444/* END_CASE */
445
446/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100447void mbedtls_mpi_swap( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_mpi X, Y, A;
450 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
453 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
454 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 );
455 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
456 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
457 mbedtls_mpi_swap( &X, &Y );
458 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
459 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000460
Paul Bakkerbd51b262014-07-10 15:26:12 +0200461exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000463}
Paul Bakker33b43f12013-08-20 11:48:36 +0200464/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000465
Paul Bakker33b43f12013-08-20 11:48:36 +0200466/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100467void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
468 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000469{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_mpi X, Y, Z, A;
471 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
474 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
475 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
476 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
477 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000478
Paul Bakkerbd51b262014-07-10 15:26:12 +0200479exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000481}
Paul Bakker33b43f12013-08-20 11:48:36 +0200482/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000483
Paul Bakker33b43f12013-08-20 11:48:36 +0200484/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100485void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
486 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100487{
488 mbedtls_mpi X, A;
489 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
490
Janos Follath044a86b2015-10-25 10:58:03 +0100491 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100492
493 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
494 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
495 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
496
497 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
498 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
499 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
500
501 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100502 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
503 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
504
505exit:
506 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
507}
508/* END_CASE */
509
510
511/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100512void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
513 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000514{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_mpi X, Y, Z, A;
516 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
519 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
520 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
521 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
522 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000523
Paul Bakkerbd51b262014-07-10 15:26:12 +0200524exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000526}
Paul Bakker33b43f12013-08-20 11:48:36 +0200527/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000528
Paul Bakker33b43f12013-08-20 11:48:36 +0200529/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100530void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
531 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000532{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_mpi X, Y, A;
534 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
537 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
538 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
539 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
540 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000541
Paul Bakkerbd51b262014-07-10 15:26:12 +0200542exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000544}
Paul Bakker33b43f12013-08-20 11:48:36 +0200545/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000546
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100548void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
549 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_mpi X, Y, A;
552 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
555 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
556 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
557 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
558 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000559
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 ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000562}
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000564
Paul Bakker33b43f12013-08-20 11:48:36 +0200565/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100566void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
567 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_mpi X, Z, A;
570 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
573 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
574 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
575 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000576
Paul Bakkerbd51b262014-07-10 15:26:12 +0200577exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000579}
Paul Bakker33b43f12013-08-20 11:48:36 +0200580/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000581
Paul Bakker33b43f12013-08-20 11:48:36 +0200582/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100583void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
584 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_mpi X, Y, Z, A;
587 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
590 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
591 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
592 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
593 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000594
Paul Bakkerbd51b262014-07-10 15:26:12 +0200595exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000597}
Paul Bakker33b43f12013-08-20 11:48:36 +0200598/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000599
Paul Bakker33b43f12013-08-20 11:48:36 +0200600/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100601void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
602 char * input_Y, int radix_A, char * input_A,
603 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000606 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
610 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
611 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200614 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000615 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000617
Paul Bakkerbd51b262014-07-10 15:26:12 +0200618exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000620}
Paul Bakker33b43f12013-08-20 11:48:36 +0200621/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000622
Paul Bakker33b43f12013-08-20 11:48:36 +0200623/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100624void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
625 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_mpi X, Z, A;
628 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
631 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
632 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
633 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000634
Paul Bakkerbd51b262014-07-10 15:26:12 +0200635exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000637}
Paul Bakker33b43f12013-08-20 11:48:36 +0200638/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000639
Paul Bakker33b43f12013-08-20 11:48:36 +0200640/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100641void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
642 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_mpi X, Y, Z, A;
645 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
648 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
649 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
650 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
651 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000652
Paul Bakkerbd51b262014-07-10 15:26:12 +0200653exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000655}
Paul Bakker33b43f12013-08-20 11:48:36 +0200656/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000657
Paul Bakker33b43f12013-08-20 11:48:36 +0200658/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100659void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
660 int radix_A, char * input_A,
661 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000662{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_mpi X, Z, A;
664 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
667 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
668 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200669 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200671 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200673 else
674 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000675
Paul Bakkerbd51b262014-07-10 15:26:12 +0200676exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000678}
Paul Bakker33b43f12013-08-20 11:48:36 +0200679/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000680
Paul Bakker33b43f12013-08-20 11:48:36 +0200681/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100682void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
683 char * input_Y, int radix_A, char * input_A,
684 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000685{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000687 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
689 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
692 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
693 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
694 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
695 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200696 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000697 if( res == 0 )
698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
700 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000701 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000702
Paul Bakkerbd51b262014-07-10 15:26:12 +0200703exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
705 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000706}
Paul Bakker33b43f12013-08-20 11:48:36 +0200707/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000708
Paul Bakker33b43f12013-08-20 11:48:36 +0200709/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100710void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
711 int radix_A, char * input_A, int radix_B,
712 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000713{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000715 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
717 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
720 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
721 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
722 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200723 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000724 if( res == 0 )
725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
727 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000728 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000729
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( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
732 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000733}
Paul Bakker33b43f12013-08-20 11:48:36 +0200734/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000735
Paul Bakker33b43f12013-08-20 11:48:36 +0200736/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100737void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
738 char * input_Y, int radix_A, char * input_A,
739 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000742 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
746 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
747 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
748 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200749 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000750 if( res == 0 )
751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000753 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000754
Paul Bakkerbd51b262014-07-10 15:26:12 +0200755exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000757}
Paul Bakker33b43f12013-08-20 11:48:36 +0200758/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000759
Paul Bakker33b43f12013-08-20 11:48:36 +0200760/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100761void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
762 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000763{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000765 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_mpi_uint r;
767 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
770 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200771 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000772 if( res == 0 )
773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000775 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000776
Paul Bakkerbd51b262014-07-10 15:26:12 +0200777exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000779}
Paul Bakker33b43f12013-08-20 11:48:36 +0200780/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000781
Paul Bakker33b43f12013-08-20 11:48:36 +0200782/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100783void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
784 char * input_E, int radix_N, char * input_N,
785 int radix_RR, char * input_RR, int radix_X,
786 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000789 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
791 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
794 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
795 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
796 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000797
Paul Bakker33b43f12013-08-20 11:48:36 +0200798 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +0200802 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000803 if( res == 0 )
804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000806 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000807
Paul Bakkerbd51b262014-07-10 15:26:12 +0200808exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
810 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000811}
Paul Bakker33b43f12013-08-20 11:48:36 +0200812/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000813
Paul Bakker33b43f12013-08-20 11:48:36 +0200814/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100815void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
816 char * input_Y, int radix_A, char * input_A,
817 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000818{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000820 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
824 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
825 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
826 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200827 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000828 if( res == 0 )
829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000831 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000832
Paul Bakkerbd51b262014-07-10 15:26:12 +0200833exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000835}
Paul Bakker33b43f12013-08-20 11:48:36 +0200836/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +0100839void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000840{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000842 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +0100846 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +0200847 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000848
Paul Bakkerbd51b262014-07-10 15:26:12 +0200849exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000851}
Paul Bakker33b43f12013-08-20 11:48:36 +0200852/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +0100855void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Janos Follatha0b67c22018-09-18 14:48:23 +0100856 int chunk_len, int rounds, int div_result )
Janos Follath64eca052018-09-05 17:04:49 +0100857{
858 mbedtls_mpi X;
859 int res;
860 mbedtls_test_mpi_random rand;
861
862 mbedtls_mpi_init( &X );
863 rand.data = witnesses;
864 rand.pos = 0;
865 rand.chunk_len = chunk_len;
866
867 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +0100868 res = mbedtls_mpi_is_prime_ext( &X, rounds,
869 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +0100870 &rand );
871 TEST_ASSERT( res == div_result );
872
873exit:
874 mbedtls_mpi_free( &X );
875}
876/* END_CASE */
877
878/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +0100879void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200880{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200882 int my_ret;
883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200885
Janos Follatha3cb7eb2018-08-14 15:31:54 +0100886 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200887 TEST_ASSERT( my_ret == ref_ret );
888
889 if( ref_ret == 0 )
890 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200891 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200892
893 TEST_ASSERT( actual_bits >= (size_t) bits );
894 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
895
Janos Follatha0b67c22018-09-18 14:48:23 +0100896 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
897 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +0100898 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200899 {
Hanno Beckerd4d60572018-01-10 07:12:01 +0000900 /* X = ( X - 1 ) / 2 */
901 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +0100902 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
903 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200904 }
905 }
906
Paul Bakkerbd51b262014-07-10 15:26:12 +0200907exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200909}
910/* END_CASE */
911
Paul Bakker33b43f12013-08-20 11:48:36 +0200912/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100913void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
914 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_mpi X, A;
917 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
920 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
921 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
922 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000923
Paul Bakkerbd51b262014-07-10 15:26:12 +0200924exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000926}
Paul Bakker33b43f12013-08-20 11:48:36 +0200927/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000928
Paul Bakker33b43f12013-08-20 11:48:36 +0200929/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100930void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
931 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_mpi X, A;
934 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
937 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
938 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
939 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000940
Paul Bakkerbd51b262014-07-10 15:26:12 +0200941exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000943}
Paul Bakker33b43f12013-08-20 11:48:36 +0200944/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100947void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +0000948{
Andres AG93012e82016-09-09 09:10:28 +0100949 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000950}
Paul Bakker33b43f12013-08-20 11:48:36 +0200951/* END_CASE */