blob: 1a047ccd6bbefd639063b4feae8765f77474023f [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 Follath883801d2019-09-11 16:07:14 +0100336void mbedtls_mpi_cmp_mpi_ct( int size_X, int radix_X, char * input_X, int size_Y,
337 int radix_Y, char * input_Y, int input_ret, int input_err )
338{
339 int ret;
340 mbedtls_mpi X, Y;
341 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
342
343 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
344 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
345
346 mbedtls_mpi_grow( &X, size_X );
347 mbedtls_mpi_grow( &Y, size_Y );
348
349 TEST_ASSERT( mbedtls_mpi_cmp_mpi_ct( &X, &Y, &ret ) == input_err );
350 if( input_err == 0 )
351 TEST_ASSERT( ret == input_ret );
352
353exit:
354 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
355}
356/* END_CASE */
357
358/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359void mbedtls_mpi_cmp_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200360 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000361{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_mpi X, Y;
363 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
366 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
367 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000368
Paul Bakkerbd51b262014-07-10 15:26:12 +0200369exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000371}
Paul Bakker33b43f12013-08-20 11:48:36 +0200372/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000373
Paul Bakker33b43f12013-08-20 11:48:36 +0200374/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375void mbedtls_mpi_copy( int input_X, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_mpi X, Y, A;
378 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
381 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_A ) == 0 );
382 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 );
383 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
384 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
385 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
386 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
387 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000388
Paul Bakkerbd51b262014-07-10 15:26:12 +0200389exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000391}
Paul Bakker33b43f12013-08-20 11:48:36 +0200392/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000393
Paul Bakker33b43f12013-08-20 11:48:36 +0200394/* BEGIN_CASE */
395void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_mpi X;
398 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
401 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
402 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000403
Paul Bakkerbd51b262014-07-10 15:26:12 +0200404exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000406}
Paul Bakker33b43f12013-08-20 11:48:36 +0200407/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000408
Paul Bakker33b43f12013-08-20 11:48:36 +0200409/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100411{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_mpi X;
413 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100416 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
418 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100419 TEST_ASSERT( X.n == (size_t) after );
420
Paul Bakkerbd51b262014-07-10 15:26:12 +0200421exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100423}
424/* END_CASE */
425
426/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427void mbedtls_mpi_safe_cond_assign( int x_sign, char *x_str,
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100428 int y_sign, char *y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100429{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_mpi X, Y, XX;
431 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100434 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100436 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
440 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
443 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100444
Paul Bakkerbd51b262014-07-10 15:26:12 +0200445exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100447}
448/* END_CASE */
449
450/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451void mbedtls_mpi_safe_cond_swap( int x_sign, char *x_str,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100452 int y_sign, char *y_str )
453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
457 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100460 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100462 Y.s = y_sign;
463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
465 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
468 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
469 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
472 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
473 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100474
Paul Bakkerbd51b262014-07-10 15:26:12 +0200475exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
477 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100478}
479/* END_CASE */
480
481/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482void mbedtls_mpi_swap( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_mpi X, Y, A;
485 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
488 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
489 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 );
490 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
491 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
492 mbedtls_mpi_swap( &X, &Y );
493 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
494 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000495
Paul Bakkerbd51b262014-07-10 15:26:12 +0200496exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000498}
Paul Bakker33b43f12013-08-20 11:48:36 +0200499/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000500
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502void mbedtls_mpi_add_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200503 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000504{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_mpi X, Y, Z, A;
506 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
509 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
510 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
511 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
512 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000513
Paul Bakkerbd51b262014-07-10 15:26:12 +0200514exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000516}
Paul Bakker33b43f12013-08-20 11:48:36 +0200517/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000518
Paul Bakker33b43f12013-08-20 11:48:36 +0200519/* BEGIN_CASE */
Janos Follath044a86b2015-10-25 10:58:03 +0100520void mbedtls_mpi_add_mpi_inplace( int radix_X, char *input_X, int radix_A, char *input_A )
521{
522 mbedtls_mpi X, A;
523 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
524
Janos Follath044a86b2015-10-25 10:58:03 +0100525 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100526
527 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
528 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
529 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
530
531 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
532 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
533 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
534
535 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100536 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
537 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
538
539exit:
540 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
541}
542/* END_CASE */
543
544
545/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546void mbedtls_mpi_add_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200547 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_mpi X, Y, Z, A;
550 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
553 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
554 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
555 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
556 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000557
Paul Bakkerbd51b262014-07-10 15:26:12 +0200558exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000560}
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000562
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* BEGIN_CASE */
564void mpi_add_abs_add_first( int radix_X, char *input_X, int radix_Y,
565 char *input_Y, int radix_A, char *input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_mpi X, Y, A;
568 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
571 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
572 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
573 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
574 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &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( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000578}
Paul Bakker33b43f12013-08-20 11:48:36 +0200579/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000580
Paul Bakker33b43f12013-08-20 11:48:36 +0200581/* BEGIN_CASE */
582void mpi_add_abs_add_second( int radix_X, char *input_X, int radix_Y,
583 char *input_Y, int radix_A, char *input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000584{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_mpi X, Y, A;
586 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +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( &Y, radix_Y, input_Y ) == 0 );
590 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
591 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
592 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000593
Paul Bakkerbd51b262014-07-10 15:26:12 +0200594exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000596}
Paul Bakker33b43f12013-08-20 11:48:36 +0200597/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000598
Paul Bakker33b43f12013-08-20 11:48:36 +0200599/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600void mbedtls_mpi_add_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200601 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_mpi X, Z, A;
604 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
607 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
608 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_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( &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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617void mbedtls_mpi_sub_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200618 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_mpi X, Y, Z, A;
621 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
624 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
625 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
626 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
627 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000628
Paul Bakkerbd51b262014-07-10 15:26:12 +0200629exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000631}
Paul Bakker33b43f12013-08-20 11:48:36 +0200632/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000633
Paul Bakker33b43f12013-08-20 11:48:36 +0200634/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635void mbedtls_mpi_sub_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200636 int radix_A, char *input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000639 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
643 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
644 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200647 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000648 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000650
Paul Bakkerbd51b262014-07-10 15:26:12 +0200651exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000653}
Paul Bakker33b43f12013-08-20 11:48:36 +0200654/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000655
Paul Bakker33b43f12013-08-20 11:48:36 +0200656/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657void mbedtls_mpi_sub_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200658 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_mpi X, Z, A;
661 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
664 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
665 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
666 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000667
Paul Bakkerbd51b262014-07-10 15:26:12 +0200668exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000670}
Paul Bakker33b43f12013-08-20 11:48:36 +0200671/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000672
Paul Bakker33b43f12013-08-20 11:48:36 +0200673/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674void mbedtls_mpi_mul_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200675 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000676{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_mpi X, Y, Z, A;
678 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
681 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
682 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
683 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
684 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000685
Paul Bakkerbd51b262014-07-10 15:26:12 +0200686exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000688}
Paul Bakker33b43f12013-08-20 11:48:36 +0200689/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000690
Paul Bakker33b43f12013-08-20 11:48:36 +0200691/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692void mbedtls_mpi_mul_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200693 char *input_A, char *result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_mpi X, Z, A;
696 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
699 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
700 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200701 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200703 else 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
706 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000707
Paul Bakkerbd51b262014-07-10 15:26:12 +0200708exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000710}
Paul Bakker33b43f12013-08-20 11:48:36 +0200711/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000712
Paul Bakker33b43f12013-08-20 11:48:36 +0200713/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714void mbedtls_mpi_div_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200715 int radix_A, char *input_A, int radix_B, char *input_B,
716 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000717{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000719 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
721 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
724 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
725 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
726 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
727 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200728 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000729 if( res == 0 )
730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
732 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
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 ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
737 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000738}
Paul Bakker33b43f12013-08-20 11:48:36 +0200739/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000740
Paul Bakker33b43f12013-08-20 11:48:36 +0200741/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742void mbedtls_mpi_div_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200743 char *input_A, int radix_B, char *input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000746 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
748 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
751 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
752 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
753 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200754 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000755 if( res == 0 )
756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
758 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000759 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000760
Paul Bakkerbd51b262014-07-10 15:26:12 +0200761exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
763 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000764}
Paul Bakker33b43f12013-08-20 11:48:36 +0200765/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000766
Paul Bakker33b43f12013-08-20 11:48:36 +0200767/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768void mbedtls_mpi_mod_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200769 int radix_A, char *input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000770{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000772 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
776 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
777 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
778 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200779 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000780 if( res == 0 )
781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000783 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000784
Paul Bakkerbd51b262014-07-10 15:26:12 +0200785exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000787}
Paul Bakker33b43f12013-08-20 11:48:36 +0200788/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000789
Paul Bakker33b43f12013-08-20 11:48:36 +0200790/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791void mbedtls_mpi_mod_int( int radix_X, char *input_X, int input_Y, int input_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200792 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000793{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000795 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_mpi_uint r;
797 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
800 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200801 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000802 if( res == 0 )
803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000805 }
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
Paul Bakker33b43f12013-08-20 11:48:36 +0200812/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813void mbedtls_mpi_exp_mod( int radix_A, char *input_A, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200814 int radix_N, char *input_N, int radix_RR, char *input_RR,
815 int radix_X, char *input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000816{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000818 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
820 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
823 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
824 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
825 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000826
Paul Bakker33b43f12013-08-20 11:48:36 +0200827 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +0200831 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000832 if( res == 0 )
833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000835 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000836
Paul Bakkerbd51b262014-07-10 15:26:12 +0200837exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
839 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000840}
Paul Bakker33b43f12013-08-20 11:48:36 +0200841/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000842
Paul Bakker33b43f12013-08-20 11:48:36 +0200843/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844void mbedtls_mpi_inv_mod( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200845 int radix_A, char *input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000848 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); 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( &Y, radix_Y, input_Y ) == 0 );
853 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
854 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200855 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000856 if( res == 0 )
857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000859 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000860
Paul Bakkerbd51b262014-07-10 15:26:12 +0200861exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000863}
Paul Bakker33b43f12013-08-20 11:48:36 +0200864/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
867void mbedtls_mpi_is_prime( int radix_X, char *input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000868{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000870 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
874 res = mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +0200875 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000876
Paul Bakkerbd51b262014-07-10 15:26:12 +0200877exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000879}
Paul Bakker33b43f12013-08-20 11:48:36 +0200880/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath0b741612018-09-05 17:04:49 +0100883void mbedtls_mpi_is_prime_det( char *input_X, char *witnesses,
884 int chunk_len, int div_result )
885{
886 mbedtls_mpi X;
887 int res;
888 mbedtls_test_mpi_random rand;
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100889 uint8_t *witness_buf = NULL;
890 uint8_t *input_buf = NULL;
Janos Follath0b741612018-09-05 17:04:49 +0100891 size_t witness_len;
892 size_t input_len;
893
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100894 witness_buf = unhexify_alloc( witnesses, &witness_len );
895 input_buf = unhexify_alloc( input_X, &input_len );
Janos Follath0b741612018-09-05 17:04:49 +0100896
897 mbedtls_mpi_init( &X );
898 rand.data = witness_buf;
899 rand.data_len = witness_len;
900 rand.pos = 0;
901 rand.chunk_len = chunk_len;
902
903 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_buf, input_len ) == 0 );
904 res = mbedtls_mpi_is_prime( &X, mbedtls_test_mpi_miller_rabin_determinizer,
905 &rand );
906 TEST_ASSERT( res == div_result );
907
908exit:
909 mbedtls_mpi_free( &X );
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100910 mbedtls_free( witness_buf );
911 mbedtls_free( input_buf );
Janos Follath0b741612018-09-05 17:04:49 +0100912}
913/* END_CASE */
914
915/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916void mbedtls_mpi_gen_prime( int bits, int safe, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200919 int my_ret;
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 my_ret = mbedtls_mpi_gen_prime( &X, bits, safe, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200924 TEST_ASSERT( my_ret == ref_ret );
925
926 if( ref_ret == 0 )
927 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200928 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200929
930 TEST_ASSERT( actual_bits >= (size_t) bits );
931 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200934 if( safe )
935 {
Hanno Beckerd4d60572018-01-10 07:12:01 +0000936 /* X = ( X - 1 ) / 2 */
937 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200939 }
940 }
941
Paul Bakkerbd51b262014-07-10 15:26:12 +0200942exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200944}
945/* END_CASE */
946
Paul Bakker33b43f12013-08-20 11:48:36 +0200947/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948void mbedtls_mpi_shift_l( int radix_X, char *input_X, int shift_X, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200949 char *input_A)
Paul Bakker367dae42009-06-28 21:50:27 +0000950{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_mpi X, A;
952 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
955 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
956 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
957 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000958
Paul Bakkerbd51b262014-07-10 15:26:12 +0200959exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000961}
Paul Bakker33b43f12013-08-20 11:48:36 +0200962/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000963
Paul Bakker33b43f12013-08-20 11:48:36 +0200964/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965void mbedtls_mpi_shift_r( int radix_X, char *input_X, int shift_X, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200966 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000967{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_mpi X, A;
969 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
972 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
973 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
974 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000975
Paul Bakkerbd51b262014-07-10 15:26:12 +0200976exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000978}
Paul Bakker33b43f12013-08-20 11:48:36 +0200979/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200982void mpi_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +0000983{
Andres AG93012e82016-09-09 09:10:28 +0100984 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000985}
Paul Bakker33b43f12013-08-20 11:48:36 +0200986/* END_CASE */