blob: d5bb6a7b9ca139dd70f13ac1c4d781a0e071415d [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 Follath64eca052018-09-05 17:04:49 +01003
4typedef struct mbedtls_test_mpi_random
5{
6 data_t *data;
7 size_t pos;
8 size_t chunk_len;
9} mbedtls_test_mpi_random;
10
11/*
12 * This function is called by the Miller-Rabin primality test each time it
13 * chooses a random witness. The witnesses (or non-witnesses as provided by the
14 * test) are stored in the data member of the state structure. Each number is in
15 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
16 */
17int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
18 unsigned char* buf,
19 size_t len )
20{
21 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
22
23 if( random == NULL || random->data->x == NULL || buf == NULL )
24 return( -1 );
25
26 if( random->pos + random->chunk_len > random->data->len
27 || random->chunk_len > len )
28 {
29 return( -1 );
30 }
31
32 memset( buf, 0, len );
33
34 /* The witness is written to the end of the buffer, since the buffer is
35 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
36 * Writing the witness to the start of the buffer would result in the
37 * buffer being 'witness 000...000', which would be treated as
38 * witness * 2^n for some n. */
39 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
40 random->chunk_len );
41
42 random->pos += random->chunk_len;
43
44 return( 0 );
45}
Paul Bakker33b43f12013-08-20 11:48:36 +020046/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000047
Paul Bakker33b43f12013-08-20 11:48:36 +020048/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020050 * END_DEPENDENCIES
51 */
Paul Bakker5690efc2011-05-26 13:16:06 +000052
Hanno Beckerafb607b2018-12-11 14:27:08 +000053/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
54void mpi_invalid_param( )
55{
56 mbedtls_mpi X;
57 const char *s_in = "00101000101010";
58 char s_out[16] = { 0 };
59 unsigned char u_out[16] = { 0 };
60 unsigned char u_in[16] = { 0 };
61 size_t olen;
62 mbedtls_mpi_uint mpi_uint;
63
64 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000065 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
66
Hanno Beckerafb607b2018-12-11 14:27:08 +000067 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
68 mbedtls_mpi_grow( NULL, 42 ) );
69 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
70 mbedtls_mpi_copy( NULL, &X ) );
71 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
72 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000073
Hanno Beckerafb607b2018-12-11 14:27:08 +000074 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
75 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000076
Hanno Beckerafb607b2018-12-11 14:27:08 +000077 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
78 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
79 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
80 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +000081
Hanno Beckerafb607b2018-12-11 14:27:08 +000082 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
83 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
84 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
85 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +000086
Hanno Beckerafb607b2018-12-11 14:27:08 +000087 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
88 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +000089
Hanno Beckerafb607b2018-12-11 14:27:08 +000090 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
91 mbedtls_mpi_get_bit( NULL, 42 ) );
92 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
93 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +000094
Hanno Beckerafb607b2018-12-11 14:27:08 +000095 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
96 mbedtls_mpi_read_string( NULL, 2, s_in ) );
97 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
98 mbedtls_mpi_read_string( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000099
Hanno Beckerafb607b2018-12-11 14:27:08 +0000100 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
101 mbedtls_mpi_write_string( NULL, 2,
102 s_out, sizeof( s_out ),
103 &olen ) );
104 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
105 mbedtls_mpi_write_string( &X, 2,
106 NULL, sizeof( s_out ),
107 &olen ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
109 mbedtls_mpi_write_string( &X, 2,
110 s_out, sizeof( s_out ),
111 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000112
Hanno Beckerafb607b2018-12-11 14:27:08 +0000113#if defined(MBEDTLS_FS_IO)
114 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
115 mbedtls_mpi_read_file( NULL, 2, stdin ) );
116 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
117 mbedtls_mpi_read_file( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000118
Hanno Beckerafb607b2018-12-11 14:27:08 +0000119 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
120 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
121#endif /* MBEDTLS_FS_IO */
122
123 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
124 mbedtls_mpi_read_binary( NULL, u_in,
125 sizeof( u_in ) ) );
126 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
127 mbedtls_mpi_read_binary( &X, NULL,
128 sizeof( u_in ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000129
Hanno Beckerafb607b2018-12-11 14:27:08 +0000130 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
131 mbedtls_mpi_write_binary( NULL, u_out,
132 sizeof( u_out ) ) );
133 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
134 mbedtls_mpi_write_binary( &X, NULL,
135 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000136
Hanno Beckerafb607b2018-12-11 14:27:08 +0000137 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
138 mbedtls_mpi_shift_l( NULL, 42 ) );
139 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
140 mbedtls_mpi_shift_r( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000141
Hanno Beckerafb607b2018-12-11 14:27:08 +0000142 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
143 mbedtls_mpi_cmp_abs( NULL, &X ) );
144 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
145 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000146
Hanno Beckerafb607b2018-12-11 14:27:08 +0000147 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
148 mbedtls_mpi_cmp_mpi( NULL, &X ) );
149 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
150 mbedtls_mpi_cmp_mpi( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000151
Hanno Beckerafb607b2018-12-11 14:27:08 +0000152 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
153 mbedtls_mpi_cmp_int( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000154
Hanno Beckerafb607b2018-12-11 14:27:08 +0000155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
156 mbedtls_mpi_add_abs( NULL, &X, &X ) );
157 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
158 mbedtls_mpi_add_abs( &X, NULL, &X ) );
159 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
160 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000161
Hanno Beckerafb607b2018-12-11 14:27:08 +0000162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
163 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
164 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
165 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
166 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
167 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000168
Hanno Beckerafb607b2018-12-11 14:27:08 +0000169 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
170 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
171 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
172 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
173 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
174 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000175
Hanno Beckerafb607b2018-12-11 14:27:08 +0000176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
177 mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
179 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
181 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000182
Hanno Beckerafb607b2018-12-11 14:27:08 +0000183 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
184 mbedtls_mpi_add_int( NULL, &X, 42 ) );
185 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
186 mbedtls_mpi_add_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000187
Hanno Beckerafb607b2018-12-11 14:27:08 +0000188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
189 mbedtls_mpi_sub_int( NULL, &X, 42 ) );
190 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
191 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000192
Hanno Beckerafb607b2018-12-11 14:27:08 +0000193 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
194 mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
195 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
196 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
197 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
198 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000199
Hanno Beckerafb607b2018-12-11 14:27:08 +0000200 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
201 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
202 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
203 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000204
Hanno Beckerafb607b2018-12-11 14:27:08 +0000205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
206 mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) );
207 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
208 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000209
Hanno Beckerafb607b2018-12-11 14:27:08 +0000210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
211 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000212
Hanno Beckerafb607b2018-12-11 14:27:08 +0000213 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
214 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
215 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
216 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
217 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
218 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000219
Hanno Beckerafb607b2018-12-11 14:27:08 +0000220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
221 mbedtls_mpi_mod_int( NULL, &X, 42 ) );
222 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
223 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000224
Hanno Beckerafb607b2018-12-11 14:27:08 +0000225 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
226 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
228 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
229 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
230 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
231 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
232 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000233
Hanno Beckerafb607b2018-12-11 14:27:08 +0000234 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
235 mbedtls_mpi_fill_random( NULL, 42, rnd_std_rand,
236 NULL ) );
237 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
238 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000239
Hanno Beckerafb607b2018-12-11 14:27:08 +0000240 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
241 mbedtls_mpi_gcd( NULL, &X, &X ) );
242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
243 mbedtls_mpi_gcd( &X, NULL, &X ) );
244 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
245 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000246
Hanno Beckerafb607b2018-12-11 14:27:08 +0000247 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
248 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
249 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
250 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
251 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000252 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000253
Hanno Becker56b661c2018-12-12 13:37:25 +0000254 mbedtls_mpi_free( NULL );
255
Hanno Beckerafb607b2018-12-11 14:27:08 +0000256exit:
257 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000258}
259/* END_CASE */
260
Paul Bakker33b43f12013-08-20 11:48:36 +0200261/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100262void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200263{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200264 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200265
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200266 mbedtls_mpi_init( &X );
267 mbedtls_mpi_init( &Y );
268 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200269
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200270 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
271 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200272 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200273 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200274
275exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200276 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200277}
278/* END_CASE */
279
280/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100281void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
282 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200283 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000286 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100287 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200292 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000293 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100294 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200295 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000296 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200297 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000298 }
299 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000300
Paul Bakkerbd51b262014-07-10 15:26:12 +0200301exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000303}
Paul Bakker33b43f12013-08-20 11:48:36 +0200304/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000305
Paul Bakker33b43f12013-08-20 11:48:36 +0200306/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100307void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000308{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000310 unsigned char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100311 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000314
Paul Bakkere896fea2009-07-06 06:40:23 +0000315
Azim Khand30ca132017-06-09 04:32:58 +0100316 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100317 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200318 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000319
Paul Bakkerbd51b262014-07-10 15:26:12 +0200320exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000322}
Paul Bakker33b43f12013-08-20 11:48:36 +0200323/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000324
Paul Bakker33b43f12013-08-20 11:48:36 +0200325/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100326void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100327 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100328 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000331 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000332 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000333
334 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200341 if( buflen > (size_t) output_size )
342 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200345 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000346 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000347
Azim Khand30ca132017-06-09 04:32:58 +0100348 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000349 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000350
Paul Bakkerbd51b262014-07-10 15:26:12 +0200351exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000353}
Paul Bakker33b43f12013-08-20 11:48:36 +0200354/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100357void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100358 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000361 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000362 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000363 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000364 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000365
366 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000369
Paul Bakker33b43f12013-08-20 11:48:36 +0200370 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200371 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000373 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000374 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000375
Paul Bakker33b43f12013-08-20 11:48:36 +0200376 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 buflen = mbedtls_mpi_size( &X );
379 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000380
Paul Bakkere896fea2009-07-06 06:40:23 +0000381
Azim Khand30ca132017-06-09 04:32:58 +0100382 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000383 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000384
Paul Bakkerbd51b262014-07-10 15:26:12 +0200385exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000387}
Paul Bakker33b43f12013-08-20 11:48:36 +0200388/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100391void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
392 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000395 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200396 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000401
Paul Bakker33b43f12013-08-20 11:48:36 +0200402 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000403 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200404 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000405 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200406 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000407
Paul Bakker33b43f12013-08-20 11:48:36 +0200408 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000409 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200410 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000411 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200412 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000415
Paul Bakkerbd51b262014-07-10 15:26:12 +0200416exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000418}
Paul Bakker33b43f12013-08-20 11:48:36 +0200419/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000420
Paul Bakker33b43f12013-08-20 11:48:36 +0200421/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100422void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000423{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_mpi X;
425 mbedtls_mpi_init( &X );
426 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
427 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000428
Paul Bakkerbd51b262014-07-10 15:26:12 +0200429exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000431}
Paul Bakker33b43f12013-08-20 11:48:36 +0200432/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000433
Paul Bakker33b43f12013-08-20 11:48:36 +0200434/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100435void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
436 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000437{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_mpi X, Y;
439 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
442 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100443 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
444
445 if( result == 0 )
446 {
447 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
448 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000449
Paul Bakkerbd51b262014-07-10 15:26:12 +0200450exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000452}
Paul Bakker33b43f12013-08-20 11:48:36 +0200453/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000454
Paul Bakker33b43f12013-08-20 11:48:36 +0200455/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100456void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000457{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_mpi X;
459 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
462 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000463
Paul Bakkerbd51b262014-07-10 15:26:12 +0200464exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000466}
Paul Bakker33b43f12013-08-20 11:48:36 +0200467/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000468
Paul Bakker33b43f12013-08-20 11:48:36 +0200469/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100470void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_mpi X;
473 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200476 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000477
Paul Bakkerbd51b262014-07-10 15:26:12 +0200478exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000480}
Paul Bakker33b43f12013-08-20 11:48:36 +0200481/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000482
Paul Bakker33b43f12013-08-20 11:48:36 +0200483/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100484void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
485 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_mpi A, X, Y, Z;
488 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
491 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
492 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
493 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
494 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000495
Paul Bakkerbd51b262014-07-10 15:26:12 +0200496exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000498}
Paul Bakker33b43f12013-08-20 11:48:36 +0200499/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000500
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_mpi X;
505 mbedtls_mpi_init( &X );
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_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000509
Paul Bakkerbd51b262014-07-10 15:26:12 +0200510exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000512}
Paul Bakker33b43f12013-08-20 11:48:36 +0200513/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000514
Paul Bakker33b43f12013-08-20 11:48:36 +0200515/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100516void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
517 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_mpi X, Y;
520 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
523 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
524 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000525
Paul Bakkerbd51b262014-07-10 15:26:12 +0200526exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000528}
Paul Bakker33b43f12013-08-20 11:48:36 +0200529/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000530
Paul Bakker33b43f12013-08-20 11:48:36 +0200531/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100532void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
533 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_mpi X, Y;
536 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
539 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
540 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000541
Paul Bakkerbd51b262014-07-10 15:26:12 +0200542exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000544}
Paul Bakker33b43f12013-08-20 11:48:36 +0200545/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000546
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548void mbedtls_mpi_copy( int input_X, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_mpi X, Y, A;
551 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
554 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_A ) == 0 );
555 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 );
556 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
557 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
558 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
559 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
560 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000561
Paul Bakkerbd51b262014-07-10 15:26:12 +0200562exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000564}
Paul Bakker33b43f12013-08-20 11:48:36 +0200565/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000566
Paul Bakker33b43f12013-08-20 11:48:36 +0200567/* BEGIN_CASE */
568void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_mpi X;
571 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
574 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
575 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000576
Paul Bakkerbd51b262014-07-10 15:26:12 +0200577exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000579}
Paul Bakker33b43f12013-08-20 11:48:36 +0200580/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000581
Paul Bakker33b43f12013-08-20 11:48:36 +0200582/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100584{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_mpi X;
586 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100589 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
591 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100592 TEST_ASSERT( X.n == (size_t) after );
593
Paul Bakkerbd51b262014-07-10 15:26:12 +0200594exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100596}
597/* END_CASE */
598
599/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100600void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
601 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_mpi X, Y, XX;
604 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100607 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100609 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
613 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
616 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100617
Paul Bakkerbd51b262014-07-10 15:26:12 +0200618exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100620}
621/* END_CASE */
622
623/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100624void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
625 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
630 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100633 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100635 Y.s = y_sign;
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
638 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
641 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
642 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
645 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
646 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100647
Paul Bakkerbd51b262014-07-10 15:26:12 +0200648exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
650 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100651}
652/* END_CASE */
653
654/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100655void mbedtls_mpi_swap( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_mpi X, Y, A;
658 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
661 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
662 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 );
663 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
664 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
665 mbedtls_mpi_swap( &X, &Y );
666 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
667 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000668
Paul Bakkerbd51b262014-07-10 15:26:12 +0200669exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000671}
Paul Bakker33b43f12013-08-20 11:48:36 +0200672/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000673
Paul Bakker33b43f12013-08-20 11:48:36 +0200674/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100675void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
676 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_mpi X, Y, Z, A;
679 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
682 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
683 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
684 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
685 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000686
Paul Bakkerbd51b262014-07-10 15:26:12 +0200687exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000689}
Paul Bakker33b43f12013-08-20 11:48:36 +0200690/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000691
Paul Bakker33b43f12013-08-20 11:48:36 +0200692/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100693void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
694 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100695{
696 mbedtls_mpi X, A;
697 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
698
Janos Follath044a86b2015-10-25 10:58:03 +0100699 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100700
701 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
702 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
703 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
704
705 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
706 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
707 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
708
709 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100710 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
711 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
712
713exit:
714 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
715}
716/* END_CASE */
717
718
719/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100720void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
721 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000722{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_mpi X, Y, Z, A;
724 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
727 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
728 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
729 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
730 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000731
Paul Bakkerbd51b262014-07-10 15:26:12 +0200732exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000734}
Paul Bakker33b43f12013-08-20 11:48:36 +0200735/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000736
Paul Bakker33b43f12013-08-20 11:48:36 +0200737/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100738void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
739 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_mpi X, Y, A;
742 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
745 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
746 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
747 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
748 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000749
Paul Bakkerbd51b262014-07-10 15:26:12 +0200750exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000752}
Paul Bakker33b43f12013-08-20 11:48:36 +0200753/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000754
Paul Bakker33b43f12013-08-20 11:48:36 +0200755/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100756void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
757 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_mpi X, Y, A;
760 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
763 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
764 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
765 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
766 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000767
Paul Bakkerbd51b262014-07-10 15:26:12 +0200768exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000770}
Paul Bakker33b43f12013-08-20 11:48:36 +0200771/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000772
Paul Bakker33b43f12013-08-20 11:48:36 +0200773/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100774void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
775 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000776{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_mpi X, Z, A;
778 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
781 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
782 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
783 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000784
Paul Bakkerbd51b262014-07-10 15:26:12 +0200785exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000787}
Paul Bakker33b43f12013-08-20 11:48:36 +0200788/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000789
Paul Bakker33b43f12013-08-20 11:48:36 +0200790/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100791void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
792 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000793{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_mpi X, Y, Z, A;
795 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
798 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
799 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
800 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &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( &Y ); 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_sub_abs( int radix_X, char * input_X, int radix_Y,
810 char * input_Y, int radix_A, char * input_A,
811 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000812{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000814 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
818 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
819 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200822 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000823 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000825
Paul Bakkerbd51b262014-07-10 15:26:12 +0200826exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000828}
Paul Bakker33b43f12013-08-20 11:48:36 +0200829/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000830
Paul Bakker33b43f12013-08-20 11:48:36 +0200831/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100832void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
833 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000834{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_mpi X, Z, A;
836 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
839 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
840 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
841 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000842
Paul Bakkerbd51b262014-07-10 15:26:12 +0200843exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000845}
Paul Bakker33b43f12013-08-20 11:48:36 +0200846/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000847
Paul Bakker33b43f12013-08-20 11:48:36 +0200848/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100849void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
850 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_mpi X, Y, Z, A;
853 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
856 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
857 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
858 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
859 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000860
Paul Bakkerbd51b262014-07-10 15:26:12 +0200861exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000863}
Paul Bakker33b43f12013-08-20 11:48:36 +0200864/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000865
Paul Bakker33b43f12013-08-20 11:48:36 +0200866/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100867void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
868 int radix_A, char * input_A,
869 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000870{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_mpi X, Z, A;
872 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
875 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
876 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200877 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200879 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200881 else
882 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000883
Paul Bakkerbd51b262014-07-10 15:26:12 +0200884exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000886}
Paul Bakker33b43f12013-08-20 11:48:36 +0200887/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000888
Paul Bakker33b43f12013-08-20 11:48:36 +0200889/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100890void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
891 char * input_Y, int radix_A, char * input_A,
892 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000895 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
897 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
900 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
901 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
902 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
903 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200904 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000905 if( res == 0 )
906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
908 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000909 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000910
Paul Bakkerbd51b262014-07-10 15:26:12 +0200911exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
913 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000914}
Paul Bakker33b43f12013-08-20 11:48:36 +0200915/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000916
Paul Bakker33b43f12013-08-20 11:48:36 +0200917/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100918void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
919 int radix_A, char * input_A, int radix_B,
920 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000923 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
925 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
928 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
929 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
930 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200931 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000932 if( res == 0 )
933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
935 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000936 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000937
Paul Bakkerbd51b262014-07-10 15:26:12 +0200938exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
940 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000941}
Paul Bakker33b43f12013-08-20 11:48:36 +0200942/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000943
Paul Bakker33b43f12013-08-20 11:48:36 +0200944/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100945void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
946 char * input_Y, int radix_A, char * input_A,
947 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000948{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000950 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
954 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
955 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
956 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200957 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000958 if( res == 0 )
959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000961 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000962
Paul Bakkerbd51b262014-07-10 15:26:12 +0200963exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000965}
Paul Bakker33b43f12013-08-20 11:48:36 +0200966/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000967
Paul Bakker33b43f12013-08-20 11:48:36 +0200968/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100969void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
970 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000971{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000973 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_mpi_uint r;
975 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
978 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200979 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000980 if( res == 0 )
981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000983 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000984
Paul Bakkerbd51b262014-07-10 15:26:12 +0200985exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000987}
Paul Bakker33b43f12013-08-20 11:48:36 +0200988/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000989
Paul Bakker33b43f12013-08-20 11:48:36 +0200990/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100991void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
992 char * input_E, int radix_N, char * input_N,
993 int radix_RR, char * input_RR, int radix_X,
994 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000995{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000997 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
999 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1002 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1003 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1004 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001005
Paul Bakker33b43f12013-08-20 11:48:36 +02001006 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001010 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001011 if( res == 0 )
1012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001014 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001015
Paul Bakkerbd51b262014-07-10 15:26:12 +02001016exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1018 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001019}
Paul Bakker33b43f12013-08-20 11:48:36 +02001020/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001021
Paul Bakker33b43f12013-08-20 11:48:36 +02001022/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001023void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1024 char * input_Y, int radix_A, char * input_A,
1025 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001028 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1032 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1033 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1034 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001035 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001036 if( res == 0 )
1037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001039 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001040
Paul Bakkerbd51b262014-07-10 15:26:12 +02001041exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001043}
Paul Bakker33b43f12013-08-20 11:48:36 +02001044/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001047void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001050 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001054 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001055 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001056
Paul Bakkerbd51b262014-07-10 15:26:12 +02001057exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001059}
Paul Bakker33b43f12013-08-20 11:48:36 +02001060/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001063void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001064 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001065{
1066 mbedtls_mpi X;
1067 int res;
1068 mbedtls_test_mpi_random rand;
1069
1070 mbedtls_mpi_init( &X );
1071 rand.data = witnesses;
1072 rand.pos = 0;
1073 rand.chunk_len = chunk_len;
1074
1075 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001076 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1077 mbedtls_test_mpi_miller_rabin_determinizer,
1078 &rand );
1079 TEST_ASSERT( res == 0 );
1080
1081 rand.data = witnesses;
1082 rand.pos = 0;
1083 rand.chunk_len = chunk_len;
1084
Janos Follatha0b67c22018-09-18 14:48:23 +01001085 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1086 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001087 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001088 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001089
1090exit:
1091 mbedtls_mpi_free( &X );
1092}
1093/* END_CASE */
1094
1095/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001096void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001099 int my_ret;
1100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001102
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001103 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001104 TEST_ASSERT( my_ret == ref_ret );
1105
1106 if( ref_ret == 0 )
1107 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001108 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001109
1110 TEST_ASSERT( actual_bits >= (size_t) bits );
1111 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1112
Janos Follatha0b67c22018-09-18 14:48:23 +01001113 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1114 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001115 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001116 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001117 /* X = ( X - 1 ) / 2 */
1118 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001119 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1120 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001121 }
1122 }
1123
Paul Bakkerbd51b262014-07-10 15:26:12 +02001124exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001126}
1127/* END_CASE */
1128
Paul Bakker33b43f12013-08-20 11:48:36 +02001129/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001130void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1131 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 mbedtls_mpi X, A;
1134 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1137 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1138 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1139 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001140
Paul Bakkerbd51b262014-07-10 15:26:12 +02001141exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001143}
Paul Bakker33b43f12013-08-20 11:48:36 +02001144/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001145
Paul Bakker33b43f12013-08-20 11:48:36 +02001146/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001147void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1148 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001149{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 mbedtls_mpi X, A;
1151 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1154 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1155 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1156 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001157
Paul Bakkerbd51b262014-07-10 15:26:12 +02001158exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001160}
Paul Bakker33b43f12013-08-20 11:48:36 +02001161/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001164void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001165{
Andres AG93012e82016-09-09 09:10:28 +01001166 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001167}
Paul Bakker33b43f12013-08-20 11:48:36 +02001168/* END_CASE */