blob: bf3ddfe0ed5baf43854a107b523334eef069fdaf [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 Follath0b741612018-09-05 17:04:49 +01003
4typedef struct mbedtls_test_mpi_random
5{
6 uint8_t *data;
7 uint32_t data_len;
8 size_t pos;
9 size_t chunk_len;
10} mbedtls_test_mpi_random;
11
12/*
13 * This function is called by the Miller-Rabin primality test each time it
14 * chooses a random witness. The witnesses (or non-witnesses as provided by the
15 * test) are stored in the data member of the state structure. Each number is in
16 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
17 */
18int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
19 unsigned char* buf,
20 size_t len )
21{
22 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
23
24 if( random == NULL || random->data == NULL || buf == NULL )
25 return( -1 );
26
27 if( random->pos + random->chunk_len > random->data_len
28 || random->chunk_len > len )
29 {
30 return( -1 );
31 }
32
33 memset( buf, 0, len );
34
35 /* The witness is written to the end of the buffer, since the buffer is
36 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
37 * Writing the witness to the start of the buffer would result in the
38 * buffer being 'witness 000...000', which would be treated as
39 * witness * 2^n for some n. */
40 memcpy( buf + len - random->chunk_len, &random->data[random->pos],
41 random->chunk_len );
42
43 random->pos += random->chunk_len;
44
45 return( 0 );
46}
Paul Bakker33b43f12013-08-20 11:48:36 +020047/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000048
Paul Bakker33b43f12013-08-20 11:48:36 +020049/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020051 * END_DEPENDENCIES
52 */
Paul Bakker5690efc2011-05-26 13:16:06 +000053
Paul Bakker33b43f12013-08-20 11:48:36 +020054/* BEGIN_CASE */
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020055void mpi_null( )
56{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020057 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020058
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020059 mbedtls_mpi_init( &X );
60 mbedtls_mpi_init( &Y );
61 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020062
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020063 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
64 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +020065 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020066 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020067
68exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020069 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020070}
71/* END_CASE */
72
73/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020074void mpi_read_write_string( int radix_X, char *input_X, int radix_A,
75 char *input_A, int output_size, int result_read,
76 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +000077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +000079 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +010080 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +000083
Janos Follathf56da142019-03-06 12:29:37 +000084 memset( str, '!', sizeof( str ) );
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +020087 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +000088 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +010089 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +020090 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +000091 {
Paul Bakker33b43f12013-08-20 11:48:36 +020092 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follathf56da142019-03-06 12:29:37 +000093 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +000094 }
95 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000096
Paul Bakkerbd51b262014-07-10 15:26:12 +020097exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +000099}
Paul Bakker33b43f12013-08-20 11:48:36 +0200100/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000101
Paul Bakker33b43f12013-08-20 11:48:36 +0200102/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103void mbedtls_mpi_read_binary( char *input_X, int radix_A, char *input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000106 unsigned char str[1000];
107 unsigned char buf[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100108 size_t len;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000109 size_t input_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000112
Paul Bakker33b43f12013-08-20 11:48:36 +0200113 input_len = unhexify( buf, input_X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf, input_len ) == 0 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100116 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200117 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000118
Paul Bakkerbd51b262014-07-10 15:26:12 +0200119exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000121}
Paul Bakker33b43f12013-08-20 11:48:36 +0200122/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000123
Paul Bakker33b43f12013-08-20 11:48:36 +0200124/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125void mbedtls_mpi_write_binary( int radix_X, char *input_X, char *input_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200126 int output_size, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000129 unsigned char str[1000];
130 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000131 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000132
133 memset( buf, 0x00, 1000 );
134 memset( str, 0x00, 1000 );
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200141 if( buflen > (size_t) output_size )
142 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200145 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000146 {
147 hexify( str, buf, buflen );
Paul Bakkere896fea2009-07-06 06:40:23 +0000148
Paul Bakker33b43f12013-08-20 11:48:36 +0200149 TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000150 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000151
Paul Bakkerbd51b262014-07-10 15:26:12 +0200152exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000154}
Paul Bakker33b43f12013-08-20 11:48:36 +0200155/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
158void mbedtls_mpi_read_file( int radix_X, char *input_file, char *input_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200159 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000162 unsigned char str[1000];
163 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000164 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000165 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000166 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000167
168 memset( buf, 0x00, 1000 );
169 memset( str, 0x00, 1000 );
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000172
Paul Bakker33b43f12013-08-20 11:48:36 +0200173 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200174 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000176 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000177 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000178
Paul Bakker33b43f12013-08-20 11:48:36 +0200179 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 buflen = mbedtls_mpi_size( &X );
182 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000183
Paul Bakkerba48cb22009-07-12 11:01:32 +0000184 hexify( str, buf, buflen );
Paul Bakkere896fea2009-07-06 06:40:23 +0000185
Paul Bakker33b43f12013-08-20 11:48:36 +0200186 TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000187 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000188
Paul Bakkerbd51b262014-07-10 15:26:12 +0200189exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000191}
Paul Bakker33b43f12013-08-20 11:48:36 +0200192/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
195void mbedtls_mpi_write_file( int radix_X, char *input_X, int output_radix,
Paul Bakker33b43f12013-08-20 11:48:36 +0200196 char *output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000197{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000199 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200200 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000205
Paul Bakker33b43f12013-08-20 11:48:36 +0200206 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000207 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200208 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000209 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200210 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000211
Paul Bakker33b43f12013-08-20 11:48:36 +0200212 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000213 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200214 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000215 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200216 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000219
Paul Bakkerbd51b262014-07-10 15:26:12 +0200220exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000222}
Paul Bakker33b43f12013-08-20 11:48:36 +0200223/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000224
Paul Bakker33b43f12013-08-20 11:48:36 +0200225/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226void mbedtls_mpi_get_bit( int radix_X, char *input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_mpi X;
229 mbedtls_mpi_init( &X );
230 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
231 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000232
Paul Bakkerbd51b262014-07-10 15:26:12 +0200233exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000235}
Paul Bakker33b43f12013-08-20 11:48:36 +0200236/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000237
Paul Bakker33b43f12013-08-20 11:48:36 +0200238/* BEGIN_CASE */
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100239void mbedtls_mpi_set_bit( int radix_X, char *input_X, int pos, int val,
240 int radix_Y, char *output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_mpi X, Y;
243 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
246 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100247 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
248
249 if( result == 0 )
250 {
251 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
252 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000253
Paul Bakkerbd51b262014-07-10 15:26:12 +0200254exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000256}
Paul Bakker33b43f12013-08-20 11:48:36 +0200257/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000258
Paul Bakker33b43f12013-08-20 11:48:36 +0200259/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260void mbedtls_mpi_lsb( int radix_X, char *input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_mpi X;
263 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
266 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000267
Paul Bakkerbd51b262014-07-10 15:26:12 +0200268exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000270}
Paul Bakker33b43f12013-08-20 11:48:36 +0200271/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000272
Paul Bakker33b43f12013-08-20 11:48:36 +0200273/* BEGIN_CASE */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200274void mbedtls_mpi_bitlen( int radix_X, char *input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_mpi X;
277 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200280 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000281
Paul Bakkerbd51b262014-07-10 15:26:12 +0200282exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000284}
Paul Bakker33b43f12013-08-20 11:48:36 +0200285/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000286
Paul Bakker33b43f12013-08-20 11:48:36 +0200287/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288void mbedtls_mpi_gcd( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200289 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_mpi A, X, Y, Z;
292 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
295 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
296 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
297 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
298 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000299
Paul Bakkerbd51b262014-07-10 15:26:12 +0200300exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000302}
Paul Bakker33b43f12013-08-20 11:48:36 +0200303/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000304
Paul Bakker33b43f12013-08-20 11:48:36 +0200305/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000307{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_mpi X;
309 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
312 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000313
Paul Bakkerbd51b262014-07-10 15:26:12 +0200314exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000316}
Paul Bakker33b43f12013-08-20 11:48:36 +0200317/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000318
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320void mbedtls_mpi_cmp_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200321 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000322{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_mpi X, Y;
324 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
327 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
328 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000329
Paul Bakkerbd51b262014-07-10 15:26:12 +0200330exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000332}
Paul Bakker33b43f12013-08-20 11:48:36 +0200333/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000334
Paul Bakker33b43f12013-08-20 11:48:36 +0200335/* BEGIN_CASE */
Janos Follathc3b376e2019-10-11 14:21:53 +0100336void mbedtls_mpi_lt_mpi_ct( int size_X, int radix_X, char * input_X,
337 int size_Y, int radix_Y, char * input_Y,
338 int input_ret, int input_err )
Janos Follath883801d2019-09-11 16:07:14 +0100339{
Janos Follathc3b376e2019-10-11 14:21:53 +0100340 unsigned ret;
341 unsigned input_uret = input_ret;
Janos Follath883801d2019-09-11 16:07:14 +0100342 mbedtls_mpi X, Y;
343 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
344
345 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
346 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
347
348 mbedtls_mpi_grow( &X, size_X );
349 mbedtls_mpi_grow( &Y, size_Y );
350
Janos Follathc3b376e2019-10-11 14:21:53 +0100351 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath883801d2019-09-11 16:07:14 +0100352 if( input_err == 0 )
Janos Follathc3b376e2019-10-11 14:21:53 +0100353 TEST_ASSERT( ret == input_uret );
Janos Follath883801d2019-09-11 16:07:14 +0100354
355exit:
356 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
357}
358/* END_CASE */
359
360/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361void mbedtls_mpi_cmp_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200362 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_mpi X, Y;
365 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
368 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
369 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000370
Paul Bakkerbd51b262014-07-10 15:26:12 +0200371exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000373}
Paul Bakker33b43f12013-08-20 11:48:36 +0200374/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000375
Paul Bakker33b43f12013-08-20 11:48:36 +0200376/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377void mbedtls_mpi_copy( int input_X, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000378{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_mpi X, Y, A;
380 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
383 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_A ) == 0 );
384 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 );
385 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
386 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
387 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
388 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
389 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000390
Paul Bakkerbd51b262014-07-10 15:26:12 +0200391exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000393}
Paul Bakker33b43f12013-08-20 11:48:36 +0200394/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000395
Paul Bakker33b43f12013-08-20 11:48:36 +0200396/* BEGIN_CASE */
397void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_mpi X;
400 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
403 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
404 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000405
Paul Bakkerbd51b262014-07-10 15:26:12 +0200406exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000408}
Paul Bakker33b43f12013-08-20 11:48:36 +0200409/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000410
Paul Bakker33b43f12013-08-20 11:48:36 +0200411/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100413{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_mpi X;
415 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100418 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
420 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100421 TEST_ASSERT( X.n == (size_t) after );
422
Paul Bakkerbd51b262014-07-10 15:26:12 +0200423exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100425}
426/* END_CASE */
427
428/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429void mbedtls_mpi_safe_cond_assign( int x_sign, char *x_str,
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100430 int y_sign, char *y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100431{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_mpi X, Y, XX;
433 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100436 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100438 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
442 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
445 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100446
Paul Bakkerbd51b262014-07-10 15:26:12 +0200447exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100449}
450/* END_CASE */
451
452/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453void mbedtls_mpi_safe_cond_swap( int x_sign, char *x_str,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100454 int y_sign, char *y_str )
455{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
459 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100462 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100464 Y.s = y_sign;
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
467 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
470 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
471 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
474 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
475 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100476
Paul Bakkerbd51b262014-07-10 15:26:12 +0200477exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
479 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100480}
481/* END_CASE */
482
483/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484void mbedtls_mpi_swap( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_mpi X, Y, A;
487 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
490 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
491 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 );
492 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
493 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
494 mbedtls_mpi_swap( &X, &Y );
495 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
496 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000497
Paul Bakkerbd51b262014-07-10 15:26:12 +0200498exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000500}
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000502
Paul Bakker33b43f12013-08-20 11:48:36 +0200503/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504void mbedtls_mpi_add_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200505 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_mpi X, Y, Z, A;
508 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
511 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
512 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
513 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
514 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000515
Paul Bakkerbd51b262014-07-10 15:26:12 +0200516exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000518}
Paul Bakker33b43f12013-08-20 11:48:36 +0200519/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000520
Paul Bakker33b43f12013-08-20 11:48:36 +0200521/* BEGIN_CASE */
Janos Follath044a86b2015-10-25 10:58:03 +0100522void mbedtls_mpi_add_mpi_inplace( int radix_X, char *input_X, int radix_A, char *input_A )
523{
524 mbedtls_mpi X, A;
525 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
526
Janos Follath044a86b2015-10-25 10:58:03 +0100527 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100528
529 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
530 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
531 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
532
533 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
534 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
535 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
536
537 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100538 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
539 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
540
541exit:
542 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
543}
544/* END_CASE */
545
546
547/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548void mbedtls_mpi_add_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200549 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_mpi X, Y, Z, A;
552 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +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( &Z, &X, &Y ) == 0 );
558 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &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( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000562}
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000564
Paul Bakker33b43f12013-08-20 11:48:36 +0200565/* BEGIN_CASE */
566void mpi_add_abs_add_first( int radix_X, char *input_X, int radix_Y,
567 char *input_Y, int radix_A, char *input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_mpi X, Y, A;
570 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +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( &Y, radix_Y, input_Y ) == 0 );
574 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
575 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
576 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000577
Paul Bakkerbd51b262014-07-10 15:26:12 +0200578exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000580}
Paul Bakker33b43f12013-08-20 11:48:36 +0200581/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000582
Paul Bakker33b43f12013-08-20 11:48:36 +0200583/* BEGIN_CASE */
584void mpi_add_abs_add_second( int radix_X, char *input_X, int radix_Y,
585 char *input_Y, int radix_A, char *input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_mpi X, Y, A;
588 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
591 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
592 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
593 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
594 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000595
Paul Bakkerbd51b262014-07-10 15:26:12 +0200596exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000598}
Paul Bakker33b43f12013-08-20 11:48:36 +0200599/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000600
Paul Bakker33b43f12013-08-20 11:48:36 +0200601/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602void mbedtls_mpi_add_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200603 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_mpi X, Z, A;
606 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
609 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
610 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
611 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000612
Paul Bakkerbd51b262014-07-10 15:26:12 +0200613exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000615}
Paul Bakker33b43f12013-08-20 11:48:36 +0200616/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000617
Paul Bakker33b43f12013-08-20 11:48:36 +0200618/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619void mbedtls_mpi_sub_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200620 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000621{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_mpi X, Y, Z, A;
623 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
626 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
627 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
628 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
629 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000630
Paul Bakkerbd51b262014-07-10 15:26:12 +0200631exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000633}
Paul Bakker33b43f12013-08-20 11:48:36 +0200634/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000635
Paul Bakker33b43f12013-08-20 11:48:36 +0200636/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637void mbedtls_mpi_sub_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200638 int radix_A, char *input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000641 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
645 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
646 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200649 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000650 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659void mbedtls_mpi_sub_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200660 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000661{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_mpi X, Z, A;
663 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
666 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
667 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
668 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000669
Paul Bakkerbd51b262014-07-10 15:26:12 +0200670exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000672}
Paul Bakker33b43f12013-08-20 11:48:36 +0200673/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000674
Paul Bakker33b43f12013-08-20 11:48:36 +0200675/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676void mbedtls_mpi_mul_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200677 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_mpi X, Y, Z, A;
680 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
683 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
684 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
685 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
686 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
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( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000690}
Paul Bakker33b43f12013-08-20 11:48:36 +0200691/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000692
Paul Bakker33b43f12013-08-20 11:48:36 +0200693/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694void mbedtls_mpi_mul_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200695 char *input_A, char *result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000696{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_mpi X, Z, A;
698 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
701 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
702 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200703 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200705 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200707 else
708 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000709
Paul Bakkerbd51b262014-07-10 15:26:12 +0200710exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000712}
Paul Bakker33b43f12013-08-20 11:48:36 +0200713/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000714
Paul Bakker33b43f12013-08-20 11:48:36 +0200715/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716void mbedtls_mpi_div_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200717 int radix_A, char *input_A, int radix_B, char *input_B,
718 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000721 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
723 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
726 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
727 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
728 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
729 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200730 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000731 if( res == 0 )
732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
734 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000735 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000736
Paul Bakkerbd51b262014-07-10 15:26:12 +0200737exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
739 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000740}
Paul Bakker33b43f12013-08-20 11:48:36 +0200741/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000742
Paul Bakker33b43f12013-08-20 11:48:36 +0200743/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744void mbedtls_mpi_div_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200745 char *input_A, int radix_B, char *input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000746{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000748 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
750 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
753 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
754 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
755 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200756 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000757 if( res == 0 )
758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
760 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000761 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000762
Paul Bakkerbd51b262014-07-10 15:26:12 +0200763exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
765 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000766}
Paul Bakker33b43f12013-08-20 11:48:36 +0200767/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000768
Paul Bakker33b43f12013-08-20 11:48:36 +0200769/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770void mbedtls_mpi_mod_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200771 int radix_A, char *input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000772{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000774 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
778 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
779 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
780 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200781 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000782 if( res == 0 )
783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000785 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000786
Paul Bakkerbd51b262014-07-10 15:26:12 +0200787exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000789}
Paul Bakker33b43f12013-08-20 11:48:36 +0200790/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000791
Paul Bakker33b43f12013-08-20 11:48:36 +0200792/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793void mbedtls_mpi_mod_int( int radix_X, char *input_X, int input_Y, int input_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200794 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000795{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000797 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_mpi_uint r;
799 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
802 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200803 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000804 if( res == 0 )
805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000807 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000808
Paul Bakkerbd51b262014-07-10 15:26:12 +0200809exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815void mbedtls_mpi_exp_mod( int radix_A, char *input_A, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200816 int radix_N, char *input_N, int radix_RR, char *input_RR,
817 int radix_X, char *input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000818{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000820 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
822 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
825 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
826 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
827 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000828
Paul Bakker33b43f12013-08-20 11:48:36 +0200829 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +0200833 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000834 if( res == 0 )
835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000837 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000838
Paul Bakkerbd51b262014-07-10 15:26:12 +0200839exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
841 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000842}
Paul Bakker33b43f12013-08-20 11:48:36 +0200843/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000844
Paul Bakker33b43f12013-08-20 11:48:36 +0200845/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846void mbedtls_mpi_inv_mod( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200847 int radix_A, char *input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000850 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
854 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
855 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
856 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200857 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000858 if( res == 0 )
859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000861 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000862
Paul Bakkerbd51b262014-07-10 15:26:12 +0200863exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000865}
Paul Bakker33b43f12013-08-20 11:48:36 +0200866/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
869void mbedtls_mpi_is_prime( int radix_X, char *input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000870{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000872 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
876 res = mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +0200877 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000878
Paul Bakkerbd51b262014-07-10 15:26:12 +0200879exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000881}
Paul Bakker33b43f12013-08-20 11:48:36 +0200882/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath0b741612018-09-05 17:04:49 +0100885void mbedtls_mpi_is_prime_det( char *input_X, char *witnesses,
886 int chunk_len, int div_result )
887{
888 mbedtls_mpi X;
889 int res;
890 mbedtls_test_mpi_random rand;
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100891 uint8_t *witness_buf = NULL;
892 uint8_t *input_buf = NULL;
Janos Follath0b741612018-09-05 17:04:49 +0100893 size_t witness_len;
894 size_t input_len;
895
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100896 witness_buf = unhexify_alloc( witnesses, &witness_len );
897 input_buf = unhexify_alloc( input_X, &input_len );
Janos Follath0b741612018-09-05 17:04:49 +0100898
899 mbedtls_mpi_init( &X );
900 rand.data = witness_buf;
901 rand.data_len = witness_len;
902 rand.pos = 0;
903 rand.chunk_len = chunk_len;
904
905 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_buf, input_len ) == 0 );
906 res = mbedtls_mpi_is_prime( &X, mbedtls_test_mpi_miller_rabin_determinizer,
907 &rand );
908 TEST_ASSERT( res == div_result );
909
910exit:
911 mbedtls_mpi_free( &X );
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100912 mbedtls_free( witness_buf );
913 mbedtls_free( input_buf );
Janos Follath0b741612018-09-05 17:04:49 +0100914}
915/* END_CASE */
916
917/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918void mbedtls_mpi_gen_prime( int bits, int safe, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200919{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200921 int my_ret;
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 my_ret = mbedtls_mpi_gen_prime( &X, bits, safe, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200926 TEST_ASSERT( my_ret == ref_ret );
927
928 if( ref_ret == 0 )
929 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200930 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200931
932 TEST_ASSERT( actual_bits >= (size_t) bits );
933 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200936 if( safe )
937 {
Hanno Beckerd4d60572018-01-10 07:12:01 +0000938 /* X = ( X - 1 ) / 2 */
939 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200941 }
942 }
943
Paul Bakkerbd51b262014-07-10 15:26:12 +0200944exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200946}
947/* END_CASE */
948
Paul Bakker33b43f12013-08-20 11:48:36 +0200949/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950void mbedtls_mpi_shift_l( int radix_X, char *input_X, int shift_X, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200951 char *input_A)
Paul Bakker367dae42009-06-28 21:50:27 +0000952{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_mpi X, A;
954 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
957 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
958 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
959 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000960
Paul Bakkerbd51b262014-07-10 15:26:12 +0200961exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000963}
Paul Bakker33b43f12013-08-20 11:48:36 +0200964/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000965
Paul Bakker33b43f12013-08-20 11:48:36 +0200966/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967void mbedtls_mpi_shift_r( int radix_X, char *input_X, int shift_X, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200968 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000969{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_mpi X, A;
971 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
974 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
975 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
976 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000977
Paul Bakkerbd51b262014-07-10 15:26:12 +0200978exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000980}
Paul Bakker33b43f12013-08-20 11:48:36 +0200981/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200984void mpi_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +0000985{
Andres AG93012e82016-09-09 09:10:28 +0100986 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000987}
Paul Bakker33b43f12013-08-20 11:48:36 +0200988/* END_CASE */