blob: 6f5abf3b83eb6c17009c1724280925f2758da294 [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 Follathaaa3f222019-10-14 09:21:49 +0100336void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
337 int size_Y, char * input_Y,
Janos Follathc3b376e2019-10-11 14:21:53 +0100338 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
Janos Follathaaa3f222019-10-14 09:21:49 +0100345 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
346 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath883801d2019-09-11 16:07:14 +0100347
Gilles Peskine16ba09c2020-01-21 16:30:53 +0100348 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
349 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath883801d2019-09-11 16:07:14 +0100350
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 */
Gilles Peskine84b8e252020-01-20 21:01:51 +0100377void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000378{
Gilles Peskine84b8e252020-01-20 21:01:51 +0100379 mbedtls_mpi X, Y;
380 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
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 );
Gilles Peskine84b8e252020-01-20 21:01:51 +0100383 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine84b8e252020-01-20 21:01:51 +0100386 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
387 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000388
Paul Bakkerbd51b262014-07-10 15:26:12 +0200389exit:
Gilles Peskine84b8e252020-01-20 21:01:51 +0100390 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
391}
392/* END_CASE */
393
394/* BEGIN_CASE */
395void mbedtls_mpi_copy_binary( char *input_X, char *input_Y )
396{
397 mbedtls_mpi X, Y, X0;
398 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
399
400 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
401 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
402 TEST_ASSERT( mbedtls_mpi_read_string( &X0, 16, input_X ) == 0 );
403 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
404
405 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
406 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
407 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
408
409exit:
410 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000411}
Paul Bakker33b43f12013-08-20 11:48:36 +0200412/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000413
Paul Bakker33b43f12013-08-20 11:48:36 +0200414/* BEGIN_CASE */
415void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000416{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_mpi X;
418 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
421 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
422 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000423
Paul Bakkerbd51b262014-07-10 15:26:12 +0200424exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000426}
Paul Bakker33b43f12013-08-20 11:48:36 +0200427/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000428
Paul Bakker33b43f12013-08-20 11:48:36 +0200429/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100431{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_mpi X;
433 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100436 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
438 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100439 TEST_ASSERT( X.n == (size_t) after );
440
Paul Bakkerbd51b262014-07-10 15:26:12 +0200441exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100443}
444/* END_CASE */
445
446/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447void mbedtls_mpi_safe_cond_assign( int x_sign, char *x_str,
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100448 int y_sign, char *y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_mpi X, Y, XX;
451 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100454 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100456 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
460 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
463 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100464
Paul Bakkerbd51b262014-07-10 15:26:12 +0200465exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100467}
468/* END_CASE */
469
470/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471void mbedtls_mpi_safe_cond_swap( int x_sign, char *x_str,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100472 int y_sign, char *y_str )
473{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
477 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100480 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100482 Y.s = y_sign;
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
485 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
488 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
489 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
492 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
493 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100494
Paul Bakkerbd51b262014-07-10 15:26:12 +0200495exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
497 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100498}
499/* END_CASE */
500
501/* BEGIN_CASE */
Gilles Peskine84b8e252020-01-20 21:01:51 +0100502void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000503{
Gilles Peskine84b8e252020-01-20 21:01:51 +0100504 mbedtls_mpi X, Y;
505 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
508 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine84b8e252020-01-20 21:01:51 +0100509 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
510 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine84b8e252020-01-20 21:01:51 +0100513 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
514 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000515
Paul Bakkerbd51b262014-07-10 15:26:12 +0200516exit:
Gilles Peskine84b8e252020-01-20 21:01:51 +0100517 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
518}
519/* END_CASE */
520
521/* BEGIN_CASE */
522void mbedtls_mpi_swap_binary( char *input_X, char *input_Y )
523{
524 mbedtls_mpi X, Y, X0, Y0;
525 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
526 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
527
528 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
529 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
530 TEST_ASSERT( mbedtls_mpi_read_string( &X0, 16, input_X ) == 0 );
531 TEST_ASSERT( mbedtls_mpi_read_string( &Y0, 16, input_Y ) == 0 );
532
533 mbedtls_mpi_swap( &X, &Y );
534 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
535 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
536
537exit:
538 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
539 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
540}
541/* END_CASE */
542
543/* BEGIN_CASE */
544void mpi_swap_self( char *input_X )
545{
546 mbedtls_mpi X, X0;
547 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
548
549 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
550 TEST_ASSERT( mbedtls_mpi_read_string( &X0, 16, input_X ) == 0 );
551
552 mbedtls_mpi_swap( &X, &X );
553 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
554
555exit:
556 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000557}
Paul Bakker33b43f12013-08-20 11:48:36 +0200558/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000559
Paul Bakker33b43f12013-08-20 11:48:36 +0200560/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561void mbedtls_mpi_add_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200562 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000563{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_mpi X, Y, Z, A;
565 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
568 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
569 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
570 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
571 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000572
Paul Bakkerbd51b262014-07-10 15:26:12 +0200573exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000575}
Paul Bakker33b43f12013-08-20 11:48:36 +0200576/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000577
Paul Bakker33b43f12013-08-20 11:48:36 +0200578/* BEGIN_CASE */
Janos Follath044a86b2015-10-25 10:58:03 +0100579void mbedtls_mpi_add_mpi_inplace( int radix_X, char *input_X, int radix_A, char *input_A )
580{
581 mbedtls_mpi X, A;
582 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
583
Janos Follath044a86b2015-10-25 10:58:03 +0100584 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100585
586 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
587 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
588 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
589
590 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
591 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
592 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
593
594 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100595 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
596 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
597
598exit:
599 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
600}
601/* END_CASE */
602
603
604/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605void mbedtls_mpi_add_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200606 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000607{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_mpi X, Y, Z, A;
609 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
612 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
613 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
614 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
615 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000616
Paul Bakkerbd51b262014-07-10 15:26:12 +0200617exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000619}
Paul Bakker33b43f12013-08-20 11:48:36 +0200620/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000621
Paul Bakker33b43f12013-08-20 11:48:36 +0200622/* BEGIN_CASE */
623void mpi_add_abs_add_first( int radix_X, char *input_X, int radix_Y,
624 char *input_Y, int radix_A, char *input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_mpi X, Y, A;
627 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
630 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
631 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
632 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
633 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000634
Paul Bakkerbd51b262014-07-10 15:26:12 +0200635exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000637}
Paul Bakker33b43f12013-08-20 11:48:36 +0200638/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000639
Paul Bakker33b43f12013-08-20 11:48:36 +0200640/* BEGIN_CASE */
641void mpi_add_abs_add_second( int radix_X, char *input_X, int radix_Y,
642 char *input_Y, int radix_A, char *input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_mpi X, Y, A;
645 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
648 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
649 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
650 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
651 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &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( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000655}
Paul Bakker33b43f12013-08-20 11:48:36 +0200656/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000657
Paul Bakker33b43f12013-08-20 11:48:36 +0200658/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659void mbedtls_mpi_add_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_add_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_sub_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_sub_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_sub_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200695 int radix_A, char *input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000696{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000698 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
702 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
703 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200706 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000707 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 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( &Y ); 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_sub_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200717 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_mpi X, Z, A;
720 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
723 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
724 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
725 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000726
Paul Bakkerbd51b262014-07-10 15:26:12 +0200727exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000729}
Paul Bakker33b43f12013-08-20 11:48:36 +0200730/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000731
Paul Bakker33b43f12013-08-20 11:48:36 +0200732/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733void mbedtls_mpi_mul_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200734 int radix_A, char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_mpi X, Y, Z, A;
737 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
740 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
741 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
742 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
743 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000744
Paul Bakkerbd51b262014-07-10 15:26:12 +0200745exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000747}
Paul Bakker33b43f12013-08-20 11:48:36 +0200748/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000749
Paul Bakker33b43f12013-08-20 11:48:36 +0200750/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751void mbedtls_mpi_mul_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200752 char *input_A, char *result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_mpi X, Z, A;
755 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
758 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
759 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200760 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200762 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200764 else
765 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000766
Paul Bakkerbd51b262014-07-10 15:26:12 +0200767exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000769}
Paul Bakker33b43f12013-08-20 11:48:36 +0200770/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000771
Paul Bakker33b43f12013-08-20 11:48:36 +0200772/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773void mbedtls_mpi_div_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200774 int radix_A, char *input_A, int radix_B, char *input_B,
775 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000776{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000778 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
780 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
783 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
784 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
785 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
786 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200787 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000788 if( res == 0 )
789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
791 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000792 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000793
Paul Bakkerbd51b262014-07-10 15:26:12 +0200794exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
796 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000797}
Paul Bakker33b43f12013-08-20 11:48:36 +0200798/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000799
Paul Bakker33b43f12013-08-20 11:48:36 +0200800/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801void mbedtls_mpi_div_int( int radix_X, char *input_X, int input_Y, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200802 char *input_A, int radix_B, char *input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000803{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000805 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
807 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
810 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
811 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
812 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200813 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000814 if( res == 0 )
815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
817 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000818 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000819
Paul Bakkerbd51b262014-07-10 15:26:12 +0200820exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
822 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000823}
Paul Bakker33b43f12013-08-20 11:48:36 +0200824/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000825
Paul Bakker33b43f12013-08-20 11:48:36 +0200826/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827void mbedtls_mpi_mod_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200828 int radix_A, char *input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000831 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
835 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
836 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
837 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200838 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000839 if( res == 0 )
840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000842 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000843
Paul Bakkerbd51b262014-07-10 15:26:12 +0200844exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000846}
Paul Bakker33b43f12013-08-20 11:48:36 +0200847/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000848
Paul Bakker33b43f12013-08-20 11:48:36 +0200849/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850void mbedtls_mpi_mod_int( int radix_X, char *input_X, int input_Y, int input_A,
Paul Bakker33b43f12013-08-20 11:48:36 +0200851 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000854 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_mpi_uint r;
856 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
859 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200860 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000861 if( res == 0 )
862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000864 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000865
Paul Bakkerbd51b262014-07-10 15:26:12 +0200866exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000868}
Paul Bakker33b43f12013-08-20 11:48:36 +0200869/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000870
Paul Bakker33b43f12013-08-20 11:48:36 +0200871/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872void mbedtls_mpi_exp_mod( int radix_A, char *input_A, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200873 int radix_N, char *input_N, int radix_RR, char *input_RR,
874 int radix_X, char *input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000875{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000877 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
879 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
882 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
883 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
884 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000885
Paul Bakker33b43f12013-08-20 11:48:36 +0200886 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +0200890 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000891 if( res == 0 )
892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000894 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000895
Paul Bakkerbd51b262014-07-10 15:26:12 +0200896exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
898 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000899}
Paul Bakker33b43f12013-08-20 11:48:36 +0200900/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000901
Paul Bakker33b43f12013-08-20 11:48:36 +0200902/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903void mbedtls_mpi_inv_mod( int radix_X, char *input_X, int radix_Y, char *input_Y,
Paul Bakker33b43f12013-08-20 11:48:36 +0200904 int radix_A, char *input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000907 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
911 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
912 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
913 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200914 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000915 if( res == 0 )
916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000918 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000919
Paul Bakkerbd51b262014-07-10 15:26:12 +0200920exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000922}
Paul Bakker33b43f12013-08-20 11:48:36 +0200923/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
926void mbedtls_mpi_is_prime( int radix_X, char *input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000927{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000929 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
933 res = mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +0200934 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000935
Paul Bakkerbd51b262014-07-10 15:26:12 +0200936exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000938}
Paul Bakker33b43f12013-08-20 11:48:36 +0200939/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath0b741612018-09-05 17:04:49 +0100942void mbedtls_mpi_is_prime_det( char *input_X, char *witnesses,
943 int chunk_len, int div_result )
944{
945 mbedtls_mpi X;
946 int res;
947 mbedtls_test_mpi_random rand;
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100948 uint8_t *witness_buf = NULL;
949 uint8_t *input_buf = NULL;
Janos Follath0b741612018-09-05 17:04:49 +0100950 size_t witness_len;
951 size_t input_len;
952
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100953 witness_buf = unhexify_alloc( witnesses, &witness_len );
954 input_buf = unhexify_alloc( input_X, &input_len );
Janos Follath0b741612018-09-05 17:04:49 +0100955
956 mbedtls_mpi_init( &X );
957 rand.data = witness_buf;
958 rand.data_len = witness_len;
959 rand.pos = 0;
960 rand.chunk_len = chunk_len;
961
962 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_buf, input_len ) == 0 );
963 res = mbedtls_mpi_is_prime( &X, mbedtls_test_mpi_miller_rabin_determinizer,
964 &rand );
965 TEST_ASSERT( res == div_result );
966
967exit:
968 mbedtls_mpi_free( &X );
Gilles Peskine0eaa6d52018-11-05 16:37:06 +0100969 mbedtls_free( witness_buf );
970 mbedtls_free( input_buf );
Janos Follath0b741612018-09-05 17:04:49 +0100971}
972/* END_CASE */
973
974/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975void mbedtls_mpi_gen_prime( int bits, int safe, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200976{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200978 int my_ret;
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 my_ret = mbedtls_mpi_gen_prime( &X, bits, safe, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200983 TEST_ASSERT( my_ret == ref_ret );
984
985 if( ref_ret == 0 )
986 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200987 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200988
989 TEST_ASSERT( actual_bits >= (size_t) bits );
990 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200993 if( safe )
994 {
Hanno Beckerd4d60572018-01-10 07:12:01 +0000995 /* X = ( X - 1 ) / 2 */
996 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +0200998 }
999 }
1000
Paul Bakkerbd51b262014-07-10 15:26:12 +02001001exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001003}
1004/* END_CASE */
1005
Paul Bakker33b43f12013-08-20 11:48:36 +02001006/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007void mbedtls_mpi_shift_l( int radix_X, char *input_X, int shift_X, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +02001008 char *input_A)
Paul Bakker367dae42009-06-28 21:50:27 +00001009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_mpi X, A;
1011 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1014 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1015 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1016 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001017
Paul Bakkerbd51b262014-07-10 15:26:12 +02001018exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001020}
Paul Bakker33b43f12013-08-20 11:48:36 +02001021/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001022
Paul Bakker33b43f12013-08-20 11:48:36 +02001023/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024void mbedtls_mpi_shift_r( int radix_X, char *input_X, int shift_X, int radix_A,
Paul Bakker33b43f12013-08-20 11:48:36 +02001025 char *input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_mpi X, A;
1028 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1031 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1032 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1033 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001034
Paul Bakkerbd51b262014-07-10 15:26:12 +02001035exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001037}
Paul Bakker33b43f12013-08-20 11:48:36 +02001038/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +02001041void mpi_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +00001042{
Andres AG93012e82016-09-09 09:10:28 +01001043 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001044}
Paul Bakker33b43f12013-08-20 11:48:36 +02001045/* END_CASE */