blob: 8c20258c592ab9d8d75c610c0071239a67b66fa8 [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"
Gilles Peskine3cb1e292020-11-25 15:37:20 +01003#include "mbedtls/entropy.h"
Janos Follath64eca052018-09-05 17:04:49 +01004
Chris Jonese64a46f2020-12-03 17:44:03 +00005#if MBEDTLS_MPI_MAX_BITS > 792
6#define MPI_MAX_BITS_LARGER_THAN_792
Chris Jones4592bd82020-12-03 14:24:33 +00007#endif
Janos Follath64eca052018-09-05 17:04:49 +01008
9typedef struct mbedtls_test_mpi_random
10{
11 data_t *data;
12 size_t pos;
13 size_t chunk_len;
14} mbedtls_test_mpi_random;
15
16/*
17 * This function is called by the Miller-Rabin primality test each time it
18 * chooses a random witness. The witnesses (or non-witnesses as provided by the
19 * test) are stored in the data member of the state structure. Each number is in
20 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
21 */
22int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
23 unsigned char* buf,
24 size_t len )
25{
26 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
27
28 if( random == NULL || random->data->x == NULL || buf == NULL )
29 return( -1 );
30
31 if( random->pos + random->chunk_len > random->data->len
32 || random->chunk_len > len )
33 {
34 return( -1 );
35 }
36
37 memset( buf, 0, len );
38
39 /* The witness is written to the end of the buffer, since the buffer is
40 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
41 * Writing the witness to the start of the buffer would result in the
42 * buffer being 'witness 000...000', which would be treated as
43 * witness * 2^n for some n. */
44 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
45 random->chunk_len );
46
47 random->pos += random->chunk_len;
48
49 return( 0 );
50}
Gilles Peskine3cb1e292020-11-25 15:37:20 +010051
52/* Random generator that is told how many bytes to return. */
53static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
54{
55 size_t *bytes_left = state;
56 size_t i;
57 for( i = 0; i < len; i++ )
58 {
59 if( *bytes_left == 0 )
60 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
61 buf[i] = *bytes_left & 0xff;
62 --( *bytes_left );
63 }
64 return( 0 );
65}
66
Paul Bakker33b43f12013-08-20 11:48:36 +020067/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000068
Paul Bakker33b43f12013-08-20 11:48:36 +020069/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020071 * END_DEPENDENCIES
72 */
Paul Bakker5690efc2011-05-26 13:16:06 +000073
Hanno Beckerb48e1aa2018-12-18 23:25:01 +000074/* BEGIN_CASE */
75void mpi_valid_param( )
76{
77 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
78}
79/* END_CASE */
80
Paul Bakker33b43f12013-08-20 11:48:36 +020081/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010082void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020083{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020084 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020085
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020086 mbedtls_mpi_init( &X );
87 mbedtls_mpi_init( &Y );
88 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020089
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020090 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
91 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +020092 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020093 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020094
95exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +020096 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +020097}
98/* END_CASE */
99
100/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100101void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
102 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200103 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000106 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100107 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000110
Janos Follath04dadb72019-03-06 12:29:37 +0000111 memset( str, '!', sizeof( str ) );
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200114 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000115 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100116 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200117 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000118 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200119 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000120 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000121 }
122 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000123
Paul Bakkerbd51b262014-07-10 15:26:12 +0200124exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000126}
Paul Bakker33b43f12013-08-20 11:48:36 +0200127/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000128
Paul Bakker33b43f12013-08-20 11:48:36 +0200129/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100130void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000133 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100134 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000137
Paul Bakkere896fea2009-07-06 06:40:23 +0000138
Azim Khand30ca132017-06-09 04:32:58 +0100139 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000140 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200141 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000142
Paul Bakkerbd51b262014-07-10 15:26:12 +0200143exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000145}
Paul Bakker33b43f12013-08-20 11:48:36 +0200146/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000147
Paul Bakker33b43f12013-08-20 11:48:36 +0200148/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000149void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
150{
151 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000152 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000153 size_t len;
154
155 mbedtls_mpi_init( &X );
156
157
158 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000159 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000160 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
161
162exit:
163 mbedtls_mpi_free( &X );
164}
165/* END_CASE */
166
167/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100168void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100169 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100170 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000173 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000174 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000175
176 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200183 if( buflen > (size_t) output_size )
184 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200187 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000188 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000189
Ronald Cron2dbba992020-06-10 11:42:32 +0200190 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
191 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000192 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000193
Paul Bakkerbd51b262014-07-10 15:26:12 +0200194exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000196}
Paul Bakker33b43f12013-08-20 11:48:36 +0200197/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000198
Janos Follathe344d0f2019-02-19 16:17:40 +0000199/* BEGIN_CASE */
200void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
201 data_t * input_A, int output_size,
202 int result )
203{
204 mbedtls_mpi X;
205 unsigned char buf[1000];
206 size_t buflen;
207
208 memset( buf, 0x00, 1000 );
209
210 mbedtls_mpi_init( &X );
211
212 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
213
214 buflen = mbedtls_mpi_size( &X );
215 if( buflen > (size_t) output_size )
216 buflen = (size_t) output_size;
217
218 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
219 if( result == 0)
220 {
221
Ronald Cron2dbba992020-06-10 11:42:32 +0200222 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
223 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000224 }
225
226exit:
227 mbedtls_mpi_free( &X );
228}
229/* END_CASE */
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100232void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100233 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000236 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000237 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000238 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000239 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000240
241 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000244
Paul Bakker33b43f12013-08-20 11:48:36 +0200245 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200246 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000248 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000249 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000250
Paul Bakker33b43f12013-08-20 11:48:36 +0200251 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 buflen = mbedtls_mpi_size( &X );
254 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000255
Paul Bakkere896fea2009-07-06 06:40:23 +0000256
Ronald Cron2dbba992020-06-10 11:42:32 +0200257 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
258 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000259 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000260
Paul Bakkerbd51b262014-07-10 15:26:12 +0200261exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000263}
Paul Bakker33b43f12013-08-20 11:48:36 +0200264/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100267void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
268 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000271 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200272 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000277
Paul Bakker33b43f12013-08-20 11:48:36 +0200278 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000279 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200280 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000281 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200282 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000283
Paul Bakker33b43f12013-08-20 11:48:36 +0200284 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000285 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200286 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000287 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200288 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000291
Paul Bakkerbd51b262014-07-10 15:26:12 +0200292exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000294}
Paul Bakker33b43f12013-08-20 11:48:36 +0200295/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000296
Paul Bakker33b43f12013-08-20 11:48:36 +0200297/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100298void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_mpi X;
301 mbedtls_mpi_init( &X );
302 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
303 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000304
Paul Bakkerbd51b262014-07-10 15:26:12 +0200305exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000307}
Paul Bakker33b43f12013-08-20 11:48:36 +0200308/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000309
Paul Bakker33b43f12013-08-20 11:48:36 +0200310/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100311void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
312 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_mpi X, Y;
315 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
318 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100319 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
320
321 if( result == 0 )
322 {
323 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
324 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000325
Paul Bakkerbd51b262014-07-10 15:26:12 +0200326exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000328}
Paul Bakker33b43f12013-08-20 11:48:36 +0200329/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000330
Paul Bakker33b43f12013-08-20 11:48:36 +0200331/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100332void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_mpi X;
335 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
338 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000339
Paul Bakkerbd51b262014-07-10 15:26:12 +0200340exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000342}
Paul Bakker33b43f12013-08-20 11:48:36 +0200343/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000344
Paul Bakker33b43f12013-08-20 11:48:36 +0200345/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100346void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_mpi X;
349 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200352 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000353
Paul Bakkerbd51b262014-07-10 15:26:12 +0200354exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000356}
Paul Bakker33b43f12013-08-20 11:48:36 +0200357/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000358
Paul Bakker33b43f12013-08-20 11:48:36 +0200359/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100360void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
361 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000362{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_mpi A, X, Y, Z;
364 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
367 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
368 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
369 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
370 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000371
Paul Bakkerbd51b262014-07-10 15:26:12 +0200372exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000374}
Paul Bakker33b43f12013-08-20 11:48:36 +0200375/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000376
Paul Bakker33b43f12013-08-20 11:48:36 +0200377/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_mpi X;
381 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
384 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000385
Paul Bakkerbd51b262014-07-10 15:26:12 +0200386exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000388}
Paul Bakker33b43f12013-08-20 11:48:36 +0200389/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000390
Paul Bakker33b43f12013-08-20 11:48:36 +0200391/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100392void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
393 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_mpi X, Y;
396 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
399 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
400 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000401
Paul Bakkerbd51b262014-07-10 15:26:12 +0200402exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000404}
Paul Bakker33b43f12013-08-20 11:48:36 +0200405/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000406
Paul Bakker33b43f12013-08-20 11:48:36 +0200407/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100408void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
409 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100410 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100411{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200412 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100413 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100414 mbedtls_mpi X, Y;
415 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
416
Janos Follathb7e1b492019-10-14 09:21:49 +0100417 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
418 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100419
Gilles Peskine9018b112020-01-21 16:30:53 +0100420 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
421 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100422
Janos Follath0e5532d2019-10-11 14:21:53 +0100423 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100424 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100425 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100426
427exit:
428 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
429}
430/* END_CASE */
431
432/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100433void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
434 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000435{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_mpi X, Y;
437 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
440 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
441 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000442
Paul Bakkerbd51b262014-07-10 15:26:12 +0200443exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000445}
Paul Bakker33b43f12013-08-20 11:48:36 +0200446/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000447
Paul Bakker33b43f12013-08-20 11:48:36 +0200448/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100449void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000450{
Gilles Peskine7428b452020-01-20 21:01:51 +0100451 mbedtls_mpi X, Y;
452 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100455 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100458 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
459 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000460
Paul Bakkerbd51b262014-07-10 15:26:12 +0200461exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100462 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
463}
464/* END_CASE */
465
466/* BEGIN_CASE */
467void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
468{
469 mbedtls_mpi X, Y, X0;
470 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
471
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100472 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
473 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
474 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100475 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
476
477 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
478 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
479 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
480
481exit:
482 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000483}
Paul Bakker33b43f12013-08-20 11:48:36 +0200484/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000485
Paul Bakker33b43f12013-08-20 11:48:36 +0200486/* BEGIN_CASE */
487void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000488{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_mpi X;
490 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
493 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
494 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 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 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000498}
Paul Bakker33b43f12013-08-20 11:48:36 +0200499/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000500
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_mpi X;
505 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100508 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
510 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100511 TEST_ASSERT( X.n == (size_t) after );
512
Paul Bakkerbd51b262014-07-10 15:26:12 +0200513exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100515}
516/* END_CASE */
517
518/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100519void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
520 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_mpi X, Y, XX;
523 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100526 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100528 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
532 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
535 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100536
Paul Bakkerbd51b262014-07-10 15:26:12 +0200537exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100539}
540/* END_CASE */
541
542/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100543void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
544 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
549 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100552 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100554 Y.s = y_sign;
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
557 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
560 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
561 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
564 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
565 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100566
Paul Bakkerbd51b262014-07-10 15:26:12 +0200567exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
569 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100570}
571/* END_CASE */
572
573/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100574void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000575{
Gilles Peskine7428b452020-01-20 21:01:51 +0100576 mbedtls_mpi X, Y;
577 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
580 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100581 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
582 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine7428b452020-01-20 21:01:51 +0100585 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
586 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000587
Paul Bakkerbd51b262014-07-10 15:26:12 +0200588exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100589 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
590}
591/* END_CASE */
592
593/* BEGIN_CASE */
594void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
595{
596 mbedtls_mpi X, Y, X0, Y0;
597 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
598 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
599
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100600 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
601 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
602 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
603 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100604
605 mbedtls_mpi_swap( &X, &Y );
606 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
607 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
608
609exit:
610 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
611 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
612}
613/* END_CASE */
614
615/* BEGIN_CASE */
616void mpi_swap_self( data_t *input_X )
617{
618 mbedtls_mpi X, X0;
619 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
620
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100621 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
622 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100623
624 mbedtls_mpi_swap( &X, &X );
625 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
626
627exit:
628 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000629}
Paul Bakker33b43f12013-08-20 11:48:36 +0200630/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000631
Paul Bakker33b43f12013-08-20 11:48:36 +0200632/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100633void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
634 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_mpi X, Y, Z, A;
637 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
640 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
641 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
642 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
643 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000644
Gilles Peskine56f943a2020-07-23 01:18:11 +0200645 /* result == first operand */
646 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
647 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
648 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
649
650 /* result == second operand */
651 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
652 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
653
Paul Bakkerbd51b262014-07-10 15:26:12 +0200654exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000656}
Paul Bakker33b43f12013-08-20 11:48:36 +0200657/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000658
Paul Bakker33b43f12013-08-20 11:48:36 +0200659/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100660void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
661 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100662{
663 mbedtls_mpi X, A;
664 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
665
Janos Follath044a86b2015-10-25 10:58:03 +0100666 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100667
668 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
669 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
670 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
671
672 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
673 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
674 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
675
676 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100677 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
678 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
679
680exit:
681 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
682}
683/* END_CASE */
684
685
686/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100687void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
688 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_mpi X, Y, Z, A;
691 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
694 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
695 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
696 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
697 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000698
Gilles Peskine56f943a2020-07-23 01:18:11 +0200699 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
701 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200703
704 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
706 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000707
Paul Bakkerbd51b262014-07-10 15:26:12 +0200708exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +0200709 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000710}
Paul Bakker33b43f12013-08-20 11:48:36 +0200711/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000712
Paul Bakker33b43f12013-08-20 11:48:36 +0200713/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100714void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
715 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_mpi X, Z, A;
718 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
721 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
722 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
723 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000724
Paul Bakkerbd51b262014-07-10 15:26:12 +0200725exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000727}
Paul Bakker33b43f12013-08-20 11:48:36 +0200728/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000729
Paul Bakker33b43f12013-08-20 11:48:36 +0200730/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100731void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
732 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000733{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_mpi X, Y, Z, A;
735 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
738 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
739 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
740 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
741 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000742
Gilles Peskine56f943a2020-07-23 01:18:11 +0200743 /* result == first operand */
744 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
745 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
746 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
747
748 /* result == second operand */
749 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
750 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
751
Paul Bakkerbd51b262014-07-10 15:26:12 +0200752exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000754}
Paul Bakker33b43f12013-08-20 11:48:36 +0200755/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000756
Paul Bakker33b43f12013-08-20 11:48:36 +0200757/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100758void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
759 char * input_Y, int radix_A, char * input_A,
760 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000763 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
767 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
768 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200771 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000772 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000774
Gilles Peskine56f943a2020-07-23 01:18:11 +0200775 /* result == first operand */
776 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
777 if( sub_result == 0 )
778 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
779 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
780
781 /* result == second operand */
782 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
783 if( sub_result == 0 )
784 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
785
Paul Bakkerbd51b262014-07-10 15:26:12 +0200786exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000788}
Paul Bakker33b43f12013-08-20 11:48:36 +0200789/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000790
Paul Bakker33b43f12013-08-20 11:48:36 +0200791/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100792void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
793 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000794{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_mpi X, Z, A;
796 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
799 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
800 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
801 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000802
Paul Bakkerbd51b262014-07-10 15:26:12 +0200803exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000805}
Paul Bakker33b43f12013-08-20 11:48:36 +0200806/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000807
Paul Bakker33b43f12013-08-20 11:48:36 +0200808/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100809void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
810 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000811{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_mpi X, Y, Z, A;
813 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
816 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
817 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
818 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
819 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000820
Paul Bakkerbd51b262014-07-10 15:26:12 +0200821exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100827void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
828 int radix_A, char * input_A,
829 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000830{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_mpi X, Z, A;
832 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); 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( &A, radix_A, input_A ) == 0 );
836 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200837 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200839 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200841 else
842 TEST_ASSERT( "unknown operator" == 0 );
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( &Z ); 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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100850void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
851 char * input_Y, int radix_A, char * input_A,
852 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000853{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000855 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
857 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
860 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
861 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
862 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
863 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200864 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000865 if( res == 0 )
866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
868 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000869 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000870
Paul Bakkerbd51b262014-07-10 15:26:12 +0200871exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
873 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000874}
Paul Bakker33b43f12013-08-20 11:48:36 +0200875/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000876
Paul Bakker33b43f12013-08-20 11:48:36 +0200877/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100878void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
879 int radix_A, char * input_A, int radix_B,
880 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000881{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000883 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
885 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
888 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
889 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
890 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200891 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000892 if( res == 0 )
893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
895 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000896 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000897
Paul Bakkerbd51b262014-07-10 15:26:12 +0200898exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
900 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000901}
Paul Bakker33b43f12013-08-20 11:48:36 +0200902/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000903
Paul Bakker33b43f12013-08-20 11:48:36 +0200904/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100905void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
906 char * input_Y, int radix_A, char * input_A,
907 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000910 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
914 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
915 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
916 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200917 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000918 if( res == 0 )
919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000921 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000922
Paul Bakkerbd51b262014-07-10 15:26:12 +0200923exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000925}
Paul Bakker33b43f12013-08-20 11:48:36 +0200926/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000927
Paul Bakker33b43f12013-08-20 11:48:36 +0200928/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100929void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
930 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000931{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000933 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 mbedtls_mpi_uint r;
935 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
938 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200939 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000940 if( res == 0 )
941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000943 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000944
Paul Bakkerbd51b262014-07-10 15:26:12 +0200945exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000947}
Paul Bakker33b43f12013-08-20 11:48:36 +0200948/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000949
Paul Bakker33b43f12013-08-20 11:48:36 +0200950/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100951void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
952 char * input_E, int radix_N, char * input_N,
953 int radix_RR, char * input_RR, int radix_X,
954 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000957 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
959 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
962 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
963 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
964 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000965
Paul Bakker33b43f12013-08-20 11:48:36 +0200966 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +0200970 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000971 if( res == 0 )
972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000974 }
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( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
978 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000979}
Paul Bakker33b43f12013-08-20 11:48:36 +0200980/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000981
Paul Bakker33b43f12013-08-20 11:48:36 +0200982/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +0000983void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesaa850cd2020-12-03 11:35:41 +0000984 int radix_RR, char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +0000985{
986 mbedtls_mpi A, E, N, RR, Z;
987 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
988 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
989
Chris Jonesaa850cd2020-12-03 11:35:41 +0000990 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +0000991 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +0000992 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +0000993 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +0000994
995 /* Set E to 2^(E_bytes - 1) + 1 */
996 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
997 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +0000998 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +0000999
1000 /* Set N to 2^(N_bytes - 1) + 1 */
1001 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1002 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001003 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1004
1005 if( strlen( input_RR ) )
1006 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
1007
Chris Jonesaa850cd2020-12-03 11:35:41 +00001008 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001009
1010exit:
1011 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1012 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1013}
1014/* END_CASE */
1015
1016/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001017void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1018 char * input_Y, int radix_A, char * input_A,
1019 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001022 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1026 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1027 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1028 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001029 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001030 if( res == 0 )
1031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001033 }
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( &Y ); mbedtls_mpi_free( &Z ); 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_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001041void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001044 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001048 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001049 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001050
Paul Bakkerbd51b262014-07-10 15:26:12 +02001051exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001053}
Paul Bakker33b43f12013-08-20 11:48:36 +02001054/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001057void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001058 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001059{
1060 mbedtls_mpi X;
1061 int res;
1062 mbedtls_test_mpi_random rand;
1063
1064 mbedtls_mpi_init( &X );
1065 rand.data = witnesses;
1066 rand.pos = 0;
1067 rand.chunk_len = chunk_len;
1068
1069 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001070 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1071 mbedtls_test_mpi_miller_rabin_determinizer,
1072 &rand );
1073 TEST_ASSERT( res == 0 );
1074
1075 rand.data = witnesses;
1076 rand.pos = 0;
1077 rand.chunk_len = chunk_len;
1078
Janos Follatha0b67c22018-09-18 14:48:23 +01001079 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1080 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001081 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001082 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001083
1084exit:
1085 mbedtls_mpi_free( &X );
1086}
1087/* END_CASE */
1088
1089/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001090void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001093 int my_ret;
1094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001096
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001097 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1098 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001099 TEST_ASSERT( my_ret == ref_ret );
1100
1101 if( ref_ret == 0 )
1102 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001103 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001104
1105 TEST_ASSERT( actual_bits >= (size_t) bits );
1106 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1107
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001108 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1109 mbedtls_test_rnd_std_rand,
1110 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001111 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001112 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001113 /* X = ( X - 1 ) / 2 */
1114 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001115 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1116 mbedtls_test_rnd_std_rand,
1117 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001118 }
1119 }
1120
Paul Bakkerbd51b262014-07-10 15:26:12 +02001121exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001123}
1124/* END_CASE */
1125
Paul Bakker33b43f12013-08-20 11:48:36 +02001126/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001127void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1128 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_mpi X, A;
1131 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1134 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1135 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1136 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001137
Paul Bakkerbd51b262014-07-10 15:26:12 +02001138exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001140}
Paul Bakker33b43f12013-08-20 11:48:36 +02001141/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001142
Paul Bakker33b43f12013-08-20 11:48:36 +02001143/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001144void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1145 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 mbedtls_mpi X, A;
1148 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1151 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1152 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1153 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001154
Paul Bakkerbd51b262014-07-10 15:26:12 +02001155exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001157}
Paul Bakker33b43f12013-08-20 11:48:36 +02001158/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001159
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001160/* BEGIN_CASE */
1161void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1162{
1163 mbedtls_mpi X;
1164 int ret;
1165 size_t bytes_left = rng_bytes;
1166 mbedtls_mpi_init( &X );
1167
1168 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1169 f_rng_bytes_left, &bytes_left );
1170 TEST_ASSERT( ret == expected_ret );
1171
1172 if( expected_ret == 0 )
1173 {
1174 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1175 * as a big-endian representation of the number. We know when
1176 * our RNG function returns null bytes, so we know how many
1177 * leading zero bytes the number has. */
1178 size_t leading_zeros = 0;
1179 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1180 leading_zeros = 1;
1181 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1182 (size_t) wanted_bytes );
1183 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
1184 }
1185
1186exit:
1187 mbedtls_mpi_free( &X );
1188}
1189/* END_CASE */
1190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001192void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001193{
Andres AG93012e82016-09-09 09:10:28 +01001194 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001195}
Paul Bakker33b43f12013-08-20 11:48:36 +02001196/* END_CASE */