blob: da0d5e4150753cb6858c97bfab7a98b02196ed42 [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"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010011void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020012{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020013 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020014
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020015 mbedtls_mpi_init( &X );
16 mbedtls_mpi_init( &Y );
17 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020018
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020019 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
20 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +020021 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020022 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020023
24exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020025 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020026}
27/* END_CASE */
28
29/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010030void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
31 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +020032 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +000033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +000035 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +010036 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +020041 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +000042 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +010043 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +020044 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +000045 {
Paul Bakker33b43f12013-08-20 11:48:36 +020046 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +000047 }
48 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000049
Paul Bakkerbd51b262014-07-10 15:26:12 +020050exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +000052}
Paul Bakker33b43f12013-08-20 11:48:36 +020053/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000054
Paul Bakker33b43f12013-08-20 11:48:36 +020055/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010056void mbedtls_mpi_read_binary( uint8_t * buf, uint32_t input_len, int radix_A,
57 char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +000058{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +000060 unsigned char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +010061 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +000064
Paul Bakkere896fea2009-07-06 06:40:23 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf, input_len ) == 0 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +010067 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +020068 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +000069
Paul Bakkerbd51b262014-07-10 15:26:12 +020070exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +000072}
Paul Bakker33b43f12013-08-20 11:48:36 +020073/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000074
Paul Bakker33b43f12013-08-20 11:48:36 +020075/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010076void mbedtls_mpi_write_binary( int radix_X, char * input_X, uint8_t * input_A,
77 uint32_t input_A_len, int output_size,
78 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +000079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +000081 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +000082 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +000083
84 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +000087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +010089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +020091 if( buflen > (size_t) output_size )
92 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020095 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +000096 {
Paul Bakkere896fea2009-07-06 06:40:23 +000097
Azim Khanf1aaec92017-05-30 14:23:15 +010098 TEST_ASSERT( hexcmp( buf, input_A, buflen, input_A_len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +000099 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000100
Paul Bakkerbd51b262014-07-10 15:26:12 +0200101exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000103}
Paul Bakker33b43f12013-08-20 11:48:36 +0200104/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100107void mbedtls_mpi_read_file( int radix_X, char * input_file, uint8_t * input_A,
108 uint32_t input_A_len, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000111 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000112 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000113 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000114 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000115
116 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000119
Paul Bakker33b43f12013-08-20 11:48:36 +0200120 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200121 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000123 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000124 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000125
Paul Bakker33b43f12013-08-20 11:48:36 +0200126 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 buflen = mbedtls_mpi_size( &X );
129 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000130
Paul Bakkere896fea2009-07-06 06:40:23 +0000131
Azim Khanf1aaec92017-05-30 14:23:15 +0100132 TEST_ASSERT( hexcmp( buf, input_A, buflen, input_A_len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000133 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000134
Paul Bakkerbd51b262014-07-10 15:26:12 +0200135exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000137}
Paul Bakker33b43f12013-08-20 11:48:36 +0200138/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100141void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
142 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000145 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200146 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000151
Paul Bakker33b43f12013-08-20 11:48:36 +0200152 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000153 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200154 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000155 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200156 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000157
Paul Bakker33b43f12013-08-20 11:48:36 +0200158 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000159 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200160 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000161 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200162 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000165
Paul Bakkerbd51b262014-07-10 15:26:12 +0200166exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000168}
Paul Bakker33b43f12013-08-20 11:48:36 +0200169/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000170
Paul Bakker33b43f12013-08-20 11:48:36 +0200171/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100172void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000173{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_mpi X;
175 mbedtls_mpi_init( &X );
176 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
177 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000178
Paul Bakkerbd51b262014-07-10 15:26:12 +0200179exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000181}
Paul Bakker33b43f12013-08-20 11:48:36 +0200182/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000183
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100185void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
186 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_mpi X, Y;
189 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
192 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100193 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
194
195 if( result == 0 )
196 {
197 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
198 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000199
Paul Bakkerbd51b262014-07-10 15:26:12 +0200200exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000202}
Paul Bakker33b43f12013-08-20 11:48:36 +0200203/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000204
Paul Bakker33b43f12013-08-20 11:48:36 +0200205/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100206void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_mpi X;
209 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
212 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000213
Paul Bakkerbd51b262014-07-10 15:26:12 +0200214exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000216}
Paul Bakker33b43f12013-08-20 11:48:36 +0200217/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000218
Paul Bakker33b43f12013-08-20 11:48:36 +0200219/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100220void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_mpi X;
223 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200226 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000227
Paul Bakkerbd51b262014-07-10 15:26:12 +0200228exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000230}
Paul Bakker33b43f12013-08-20 11:48:36 +0200231/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000232
Paul Bakker33b43f12013-08-20 11:48:36 +0200233/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100234void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
235 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000236{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_mpi A, X, Y, Z;
238 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
241 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
242 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
243 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
244 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000245
Paul Bakkerbd51b262014-07-10 15:26:12 +0200246exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000248}
Paul Bakker33b43f12013-08-20 11:48:36 +0200249/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000250
Paul Bakker33b43f12013-08-20 11:48:36 +0200251/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_mpi X;
255 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
258 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000259
Paul Bakkerbd51b262014-07-10 15:26:12 +0200260exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000262}
Paul Bakker33b43f12013-08-20 11:48:36 +0200263/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100266void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
267 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000268{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_mpi X, Y;
270 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
273 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
274 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000275
Paul Bakkerbd51b262014-07-10 15:26:12 +0200276exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000278}
Paul Bakker33b43f12013-08-20 11:48:36 +0200279/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000280
Paul Bakker33b43f12013-08-20 11:48:36 +0200281/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100282void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
283 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_mpi X, Y;
286 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
289 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
290 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000291
Paul Bakkerbd51b262014-07-10 15:26:12 +0200292exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000294}
Paul Bakker33b43f12013-08-20 11:48:36 +0200295/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000296
Paul Bakker33b43f12013-08-20 11:48:36 +0200297/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298void mbedtls_mpi_copy( int input_X, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_mpi X, Y, A;
301 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
304 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_A ) == 0 );
305 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 );
306 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
307 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
308 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
309 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
310 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000311
Paul Bakkerbd51b262014-07-10 15:26:12 +0200312exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000314}
Paul Bakker33b43f12013-08-20 11:48:36 +0200315/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000316
Paul Bakker33b43f12013-08-20 11:48:36 +0200317/* BEGIN_CASE */
318void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_mpi X;
321 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
324 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
325 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000326
Paul Bakkerbd51b262014-07-10 15:26:12 +0200327exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000329}
Paul Bakker33b43f12013-08-20 11:48:36 +0200330/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000331
Paul Bakker33b43f12013-08-20 11:48:36 +0200332/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_mpi X;
336 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100339 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
341 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100342 TEST_ASSERT( X.n == (size_t) after );
343
Paul Bakkerbd51b262014-07-10 15:26:12 +0200344exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100346}
347/* END_CASE */
348
349/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100350void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
351 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_mpi X, Y, XX;
354 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100357 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100359 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
363 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
366 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100367
Paul Bakkerbd51b262014-07-10 15:26:12 +0200368exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100370}
371/* END_CASE */
372
373/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100374void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
375 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
380 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100383 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100385 Y.s = y_sign;
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
388 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
391 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
392 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
395 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
396 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100397
Paul Bakkerbd51b262014-07-10 15:26:12 +0200398exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
400 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100401}
402/* END_CASE */
403
404/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100405void mbedtls_mpi_swap( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000406{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_mpi X, Y, A;
408 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
411 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
412 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 );
413 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
414 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
415 mbedtls_mpi_swap( &X, &Y );
416 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
417 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000418
Paul Bakkerbd51b262014-07-10 15:26:12 +0200419exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000421}
Paul Bakker33b43f12013-08-20 11:48:36 +0200422/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000423
Paul Bakker33b43f12013-08-20 11:48:36 +0200424/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100425void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
426 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000427{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_mpi X, Y, Z, A;
429 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
432 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
433 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
434 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
435 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000436
Paul Bakkerbd51b262014-07-10 15:26:12 +0200437exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000439}
Paul Bakker33b43f12013-08-20 11:48:36 +0200440/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000441
Paul Bakker33b43f12013-08-20 11:48:36 +0200442/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100443void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
444 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100445{
446 mbedtls_mpi X, A;
447 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
448
Janos Follath044a86b2015-10-25 10:58:03 +0100449 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100450
451 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
452 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
453 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
454
455 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
456 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
457 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
458
459 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100460 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
461 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
462
463exit:
464 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
465}
466/* END_CASE */
467
468
469/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100470void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
471 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000472{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_mpi X, Y, Z, A;
474 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
477 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
478 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
479 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
480 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000481
Paul Bakkerbd51b262014-07-10 15:26:12 +0200482exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000484}
Paul Bakker33b43f12013-08-20 11:48:36 +0200485/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000486
Paul Bakker33b43f12013-08-20 11:48:36 +0200487/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100488void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
489 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000490{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_mpi X, Y, A;
492 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
495 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
496 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
497 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
498 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000499
Paul Bakkerbd51b262014-07-10 15:26:12 +0200500exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000502}
Paul Bakker33b43f12013-08-20 11:48:36 +0200503/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000504
Paul Bakker33b43f12013-08-20 11:48:36 +0200505/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100506void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
507 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000508{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_mpi X, Y, A;
510 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
513 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
514 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
515 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
516 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000517
Paul Bakkerbd51b262014-07-10 15:26:12 +0200518exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000520}
Paul Bakker33b43f12013-08-20 11:48:36 +0200521/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000522
Paul Bakker33b43f12013-08-20 11:48:36 +0200523/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100524void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
525 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_mpi X, Z, A;
528 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
531 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
532 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
533 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000534
Paul Bakkerbd51b262014-07-10 15:26:12 +0200535exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000537}
Paul Bakker33b43f12013-08-20 11:48:36 +0200538/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000539
Paul Bakker33b43f12013-08-20 11:48:36 +0200540/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100541void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
542 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000543{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_mpi X, Y, Z, A;
545 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
548 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
549 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
550 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
551 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000552
Paul Bakkerbd51b262014-07-10 15:26:12 +0200553exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000555}
Paul Bakker33b43f12013-08-20 11:48:36 +0200556/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000557
Paul Bakker33b43f12013-08-20 11:48:36 +0200558/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100559void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
560 char * input_Y, int radix_A, char * input_A,
561 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000562{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000564 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
568 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
569 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200572 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000573 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000575
Paul Bakkerbd51b262014-07-10 15:26:12 +0200576exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000578}
Paul Bakker33b43f12013-08-20 11:48:36 +0200579/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000580
Paul Bakker33b43f12013-08-20 11:48:36 +0200581/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100582void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
583 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000584{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_mpi X, Z, A;
586 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
589 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
590 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
591 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000592
Paul Bakkerbd51b262014-07-10 15:26:12 +0200593exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000595}
Paul Bakker33b43f12013-08-20 11:48:36 +0200596/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000597
Paul Bakker33b43f12013-08-20 11:48:36 +0200598/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100599void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
600 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_mpi X, Y, Z, A;
603 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
606 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
607 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
608 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
609 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000610
Paul Bakkerbd51b262014-07-10 15:26:12 +0200611exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000613}
Paul Bakker33b43f12013-08-20 11:48:36 +0200614/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000615
Paul Bakker33b43f12013-08-20 11:48:36 +0200616/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100617void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
618 int radix_A, char * input_A,
619 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_mpi X, Z, A;
622 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
625 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
626 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200627 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200629 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200631 else
632 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000633
Paul Bakkerbd51b262014-07-10 15:26:12 +0200634exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000636}
Paul Bakker33b43f12013-08-20 11:48:36 +0200637/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000638
Paul Bakker33b43f12013-08-20 11:48:36 +0200639/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100640void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
641 char * input_Y, int radix_A, char * input_A,
642 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000645 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
647 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
650 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
651 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
652 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
653 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200654 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000655 if( res == 0 )
656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
658 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000659 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000660
Paul Bakkerbd51b262014-07-10 15:26:12 +0200661exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
663 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000664}
Paul Bakker33b43f12013-08-20 11:48:36 +0200665/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000666
Paul Bakker33b43f12013-08-20 11:48:36 +0200667/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100668void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
669 int radix_A, char * input_A, int radix_B,
670 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000671{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000673 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
675 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
678 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
679 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
680 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200681 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000682 if( res == 0 )
683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
685 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000686 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000687
Paul Bakkerbd51b262014-07-10 15:26:12 +0200688exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
690 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000691}
Paul Bakker33b43f12013-08-20 11:48:36 +0200692/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000693
Paul Bakker33b43f12013-08-20 11:48:36 +0200694/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100695void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
696 char * input_Y, int radix_A, char * input_A,
697 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000698{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000700 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
704 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
705 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
706 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200707 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000708 if( res == 0 )
709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000711 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000712
Paul Bakkerbd51b262014-07-10 15:26:12 +0200713exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000715}
Paul Bakker33b43f12013-08-20 11:48:36 +0200716/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000717
Paul Bakker33b43f12013-08-20 11:48:36 +0200718/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100719void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
720 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000721{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000723 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_mpi_uint r;
725 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
728 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200729 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000730 if( res == 0 )
731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000733 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000734
Paul Bakkerbd51b262014-07-10 15:26:12 +0200735exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000737}
Paul Bakker33b43f12013-08-20 11:48:36 +0200738/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000739
Paul Bakker33b43f12013-08-20 11:48:36 +0200740/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100741void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
742 char * input_E, int radix_N, char * input_N,
743 int radix_RR, char * input_RR, int radix_X,
744 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000747 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
749 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
752 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
753 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
754 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000755
Paul Bakker33b43f12013-08-20 11:48:36 +0200756 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +0200760 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000761 if( res == 0 )
762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000764 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000765
Paul Bakkerbd51b262014-07-10 15:26:12 +0200766exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
768 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000769}
Paul Bakker33b43f12013-08-20 11:48:36 +0200770/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000771
Paul Bakker33b43f12013-08-20 11:48:36 +0200772/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100773void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
774 char * input_Y, int radix_A, char * input_A,
775 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000776{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000778 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
782 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
783 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
784 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200785 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000786 if( res == 0 )
787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000789 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000790
Paul Bakkerbd51b262014-07-10 15:26:12 +0200791exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000793}
Paul Bakker33b43f12013-08-20 11:48:36 +0200794/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +0100797void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000798{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000800 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
804 res = mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +0200805 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000806
Paul Bakkerbd51b262014-07-10 15:26:12 +0200807exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000809}
Paul Bakker33b43f12013-08-20 11:48:36 +0200810/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
813void mbedtls_mpi_gen_prime( int bits, int safe, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200814{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200816 int my_ret;
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 my_ret = mbedtls_mpi_gen_prime( &X, bits, safe, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200821 TEST_ASSERT( my_ret == ref_ret );
822
823 if( ref_ret == 0 )
824 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200825 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200826
827 TEST_ASSERT( actual_bits >= (size_t) bits );
828 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200831 if( safe )
832 {
Hanno Beckerd4d60572018-01-10 07:12:01 +0000833 /* X = ( X - 1 ) / 2 */
834 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200836 }
837 }
838
Paul Bakkerbd51b262014-07-10 15:26:12 +0200839exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200841}
842/* END_CASE */
843
Paul Bakker33b43f12013-08-20 11:48:36 +0200844/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100845void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
846 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000847{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_mpi X, A;
849 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
852 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
853 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
854 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000855
Paul Bakkerbd51b262014-07-10 15:26:12 +0200856exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000858}
Paul Bakker33b43f12013-08-20 11:48:36 +0200859/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000860
Paul Bakker33b43f12013-08-20 11:48:36 +0200861/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100862void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
863 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000864{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_mpi X, A;
866 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
869 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
870 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
871 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000872
Paul Bakkerbd51b262014-07-10 15:26:12 +0200873exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000875}
Paul Bakker33b43f12013-08-20 11:48:36 +0200876/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100879void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +0000880{
Andres AG93012e82016-09-09 09:10:28 +0100881 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000882}
Paul Bakker33b43f12013-08-20 11:48:36 +0200883/* END_CASE */