blob: 02cd1a9cae5a11439f9f9c460decac0c91c283be [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 Beckerb48e1aa2018-12-18 23:25:01 +000053/* BEGIN_CASE */
54void mpi_valid_param( )
55{
56 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
57}
58/* END_CASE */
59
Hanno Beckerafb607b2018-12-11 14:27:08 +000060/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
61void mpi_invalid_param( )
62{
63 mbedtls_mpi X;
64 const char *s_in = "00101000101010";
65 char s_out[16] = { 0 };
66 unsigned char u_out[16] = { 0 };
67 unsigned char u_in[16] = { 0 };
68 size_t olen;
69 mbedtls_mpi_uint mpi_uint;
70
71 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000072
Hanno Beckerafb607b2018-12-11 14:27:08 +000073 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
74 mbedtls_mpi_grow( NULL, 42 ) );
75 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
76 mbedtls_mpi_copy( NULL, &X ) );
77 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
78 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000079
Hanno Beckerafb607b2018-12-11 14:27:08 +000080 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
81 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000082
Hanno Beckerafb607b2018-12-11 14:27:08 +000083 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
84 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
85 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
86 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +000087
Hanno Beckerafb607b2018-12-11 14:27:08 +000088 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
89 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
90 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
91 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +000092
Hanno Beckerafb607b2018-12-11 14:27:08 +000093 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
94 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +000095
Hanno Beckerafb607b2018-12-11 14:27:08 +000096 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
97 mbedtls_mpi_get_bit( NULL, 42 ) );
98 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
99 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000100
Hanno Beckerafb607b2018-12-11 14:27:08 +0000101 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
102 mbedtls_mpi_read_string( NULL, 2, s_in ) );
103 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
104 mbedtls_mpi_read_string( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000105
Hanno Beckerafb607b2018-12-11 14:27:08 +0000106 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
107 mbedtls_mpi_write_string( NULL, 2,
108 s_out, sizeof( s_out ),
109 &olen ) );
110 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
111 mbedtls_mpi_write_string( &X, 2,
112 NULL, sizeof( s_out ),
113 &olen ) );
114 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
115 mbedtls_mpi_write_string( &X, 2,
116 s_out, sizeof( s_out ),
117 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000118
Hanno Beckerafb607b2018-12-11 14:27:08 +0000119#if defined(MBEDTLS_FS_IO)
120 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
121 mbedtls_mpi_read_file( NULL, 2, stdin ) );
122 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
123 mbedtls_mpi_read_file( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000124
Hanno Beckerafb607b2018-12-11 14:27:08 +0000125 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
126 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
127#endif /* MBEDTLS_FS_IO */
128
129 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
130 mbedtls_mpi_read_binary( NULL, u_in,
131 sizeof( u_in ) ) );
132 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
133 mbedtls_mpi_read_binary( &X, NULL,
134 sizeof( u_in ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000135
Hanno Beckerafb607b2018-12-11 14:27:08 +0000136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
137 mbedtls_mpi_write_binary( NULL, u_out,
138 sizeof( u_out ) ) );
139 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
140 mbedtls_mpi_write_binary( &X, NULL,
141 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000142
Hanno Beckerafb607b2018-12-11 14:27:08 +0000143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
144 mbedtls_mpi_shift_l( NULL, 42 ) );
145 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
146 mbedtls_mpi_shift_r( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000147
Hanno Beckerafb607b2018-12-11 14:27:08 +0000148 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
149 mbedtls_mpi_cmp_abs( NULL, &X ) );
150 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
151 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000152
Hanno Beckerafb607b2018-12-11 14:27:08 +0000153 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
154 mbedtls_mpi_cmp_mpi( NULL, &X ) );
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
156 mbedtls_mpi_cmp_mpi( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000157
Hanno Beckerafb607b2018-12-11 14:27:08 +0000158 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
159 mbedtls_mpi_cmp_int( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000160
Hanno Beckerafb607b2018-12-11 14:27:08 +0000161 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
162 mbedtls_mpi_add_abs( NULL, &X, &X ) );
163 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
164 mbedtls_mpi_add_abs( &X, NULL, &X ) );
165 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
166 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000167
Hanno Beckerafb607b2018-12-11 14:27:08 +0000168 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
169 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
170 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
171 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
173 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000174
Hanno Beckerafb607b2018-12-11 14:27:08 +0000175 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
176 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
177 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
178 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
179 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
180 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000181
Hanno Beckerafb607b2018-12-11 14:27:08 +0000182 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
183 mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
184 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
185 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
186 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
187 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000188
Hanno Beckerafb607b2018-12-11 14:27:08 +0000189 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
190 mbedtls_mpi_add_int( NULL, &X, 42 ) );
191 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
192 mbedtls_mpi_add_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000193
Hanno Beckerafb607b2018-12-11 14:27:08 +0000194 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
195 mbedtls_mpi_sub_int( NULL, &X, 42 ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
197 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000198
Hanno Beckerafb607b2018-12-11 14:27:08 +0000199 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
200 mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
202 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
203 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
204 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000205
Hanno Beckerafb607b2018-12-11 14:27:08 +0000206 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
207 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
209 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000210
Hanno Beckerafb607b2018-12-11 14:27:08 +0000211 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
212 mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) );
213 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
214 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000215
Hanno Beckerafb607b2018-12-11 14:27:08 +0000216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
217 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000218
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000219 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
220
Hanno Beckerafb607b2018-12-11 14:27:08 +0000221 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
222 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
223 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
224 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
225 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
226 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000227
Hanno Beckerafb607b2018-12-11 14:27:08 +0000228 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
229 mbedtls_mpi_mod_int( NULL, &X, 42 ) );
230 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
231 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000232
Hanno Beckerafb607b2018-12-11 14:27:08 +0000233 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
234 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
235 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
236 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
237 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
238 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
239 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
240 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000241
Hanno Beckerafb607b2018-12-11 14:27:08 +0000242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Ronald Cron351f0ee2020-06-10 12:12:18 +0200243 mbedtls_mpi_fill_random( NULL, 42, mbedtls_test_rnd_std_rand,
Hanno Beckerafb607b2018-12-11 14:27:08 +0000244 NULL ) );
245 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
246 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000247
Hanno Beckerafb607b2018-12-11 14:27:08 +0000248 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
249 mbedtls_mpi_gcd( NULL, &X, &X ) );
250 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
251 mbedtls_mpi_gcd( &X, NULL, &X ) );
252 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
253 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000254
Hanno Beckerafb607b2018-12-11 14:27:08 +0000255 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
256 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
257 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
258 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
259 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000260 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000261
262exit:
263 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000264}
265/* END_CASE */
266
Paul Bakker33b43f12013-08-20 11:48:36 +0200267/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100268void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200269{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200270 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200271
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200272 mbedtls_mpi_init( &X );
273 mbedtls_mpi_init( &Y );
274 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200275
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200276 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
277 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200278 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200279 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200280
281exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200282 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200283}
284/* END_CASE */
285
286/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100287void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
288 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200289 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000292 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100293 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000296
Janos Follath04dadb72019-03-06 12:29:37 +0000297 memset( str, '!', sizeof( str ) );
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200300 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000301 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100302 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200303 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000304 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200305 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000306 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000307 }
308 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000309
Paul Bakkerbd51b262014-07-10 15:26:12 +0200310exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000312}
Paul Bakker33b43f12013-08-20 11:48:36 +0200313/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000314
Paul Bakker33b43f12013-08-20 11:48:36 +0200315/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100316void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000319 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100320 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000323
Paul Bakkere896fea2009-07-06 06:40:23 +0000324
Azim Khand30ca132017-06-09 04:32:58 +0100325 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000326 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200327 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000328
Paul Bakkerbd51b262014-07-10 15:26:12 +0200329exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000331}
Paul Bakker33b43f12013-08-20 11:48:36 +0200332/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000333
Paul Bakker33b43f12013-08-20 11:48:36 +0200334/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000335void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
336{
337 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000338 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000339 size_t len;
340
341 mbedtls_mpi_init( &X );
342
343
344 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000345 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000346 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
347
348exit:
349 mbedtls_mpi_free( &X );
350}
351/* END_CASE */
352
353/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100354void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100355 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100356 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000359 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000360 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000361
362 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200369 if( buflen > (size_t) output_size )
370 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200373 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000374 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000375
Ronald Cron2dbba992020-06-10 11:42:32 +0200376 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
377 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000378 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000379
Paul Bakkerbd51b262014-07-10 15:26:12 +0200380exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000382}
Paul Bakker33b43f12013-08-20 11:48:36 +0200383/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000384
Janos Follathe344d0f2019-02-19 16:17:40 +0000385/* BEGIN_CASE */
386void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
387 data_t * input_A, int output_size,
388 int result )
389{
390 mbedtls_mpi X;
391 unsigned char buf[1000];
392 size_t buflen;
393
394 memset( buf, 0x00, 1000 );
395
396 mbedtls_mpi_init( &X );
397
398 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
399
400 buflen = mbedtls_mpi_size( &X );
401 if( buflen > (size_t) output_size )
402 buflen = (size_t) output_size;
403
404 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
405 if( result == 0)
406 {
407
Ronald Cron2dbba992020-06-10 11:42:32 +0200408 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
409 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000410 }
411
412exit:
413 mbedtls_mpi_free( &X );
414}
415/* END_CASE */
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100418void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100419 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000420{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000422 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000423 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000424 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000425 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000426
427 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000430
Paul Bakker33b43f12013-08-20 11:48:36 +0200431 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200432 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000434 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000435 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000436
Paul Bakker33b43f12013-08-20 11:48:36 +0200437 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 buflen = mbedtls_mpi_size( &X );
440 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000441
Paul Bakkere896fea2009-07-06 06:40:23 +0000442
Ronald Cron2dbba992020-06-10 11:42:32 +0200443 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
444 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000445 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000446
Paul Bakkerbd51b262014-07-10 15:26:12 +0200447exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000449}
Paul Bakker33b43f12013-08-20 11:48:36 +0200450/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100453void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
454 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000455{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000457 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200458 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000463
Paul Bakker33b43f12013-08-20 11:48:36 +0200464 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000465 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200466 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000467 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200468 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000469
Paul Bakker33b43f12013-08-20 11:48:36 +0200470 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000471 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200472 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000473 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200474 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
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 ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000480}
Paul Bakker33b43f12013-08-20 11:48:36 +0200481/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000482
Paul Bakker33b43f12013-08-20 11:48:36 +0200483/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100484void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_mpi X;
487 mbedtls_mpi_init( &X );
488 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
489 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000490
Paul Bakkerbd51b262014-07-10 15:26:12 +0200491exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000493}
Paul Bakker33b43f12013-08-20 11:48:36 +0200494/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000495
Paul Bakker33b43f12013-08-20 11:48:36 +0200496/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100497void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
498 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000499{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_mpi X, Y;
501 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
504 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100505 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
506
507 if( result == 0 )
508 {
509 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
510 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000511
Paul Bakkerbd51b262014-07-10 15:26:12 +0200512exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000514}
Paul Bakker33b43f12013-08-20 11:48:36 +0200515/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000516
Paul Bakker33b43f12013-08-20 11:48:36 +0200517/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100518void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_mpi X;
521 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
524 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
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 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000528}
Paul Bakker33b43f12013-08-20 11:48:36 +0200529/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000530
Paul Bakker33b43f12013-08-20 11:48:36 +0200531/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100532void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_mpi X;
535 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200538 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000539
Paul Bakkerbd51b262014-07-10 15:26:12 +0200540exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000542}
Paul Bakker33b43f12013-08-20 11:48:36 +0200543/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000544
Paul Bakker33b43f12013-08-20 11:48:36 +0200545/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100546void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
547 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_mpi A, X, Y, Z;
550 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
553 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
554 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
555 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
556 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000557
Paul Bakkerbd51b262014-07-10 15:26:12 +0200558exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000560}
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000562
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_mpi X;
567 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
570 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000571
Paul Bakkerbd51b262014-07-10 15:26:12 +0200572exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000574}
Paul Bakker33b43f12013-08-20 11:48:36 +0200575/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000576
Paul Bakker33b43f12013-08-20 11:48:36 +0200577/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100578void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
579 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000580{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_mpi X, Y;
582 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
585 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
586 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000587
Paul Bakkerbd51b262014-07-10 15:26:12 +0200588exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000590}
Paul Bakker33b43f12013-08-20 11:48:36 +0200591/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000592
Paul Bakker33b43f12013-08-20 11:48:36 +0200593/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100594void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
595 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100596 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100597{
Janos Follath0e5532d2019-10-11 14:21:53 +0100598 unsigned ret;
599 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100600 mbedtls_mpi X, Y;
601 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
602
Janos Follathb7e1b492019-10-14 09:21:49 +0100603 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
604 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100605
Gilles Peskine9018b112020-01-21 16:30:53 +0100606 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
607 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100608
Janos Follath0e5532d2019-10-11 14:21:53 +0100609 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100610 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100611 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100612
613exit:
614 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
615}
616/* END_CASE */
617
618/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100619void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
620 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000621{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_mpi X, Y;
623 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
626 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
627 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000628
Paul Bakkerbd51b262014-07-10 15:26:12 +0200629exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000631}
Paul Bakker33b43f12013-08-20 11:48:36 +0200632/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000633
Paul Bakker33b43f12013-08-20 11:48:36 +0200634/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100635void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000636{
Gilles Peskine7428b452020-01-20 21:01:51 +0100637 mbedtls_mpi X, Y;
638 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100641 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100644 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
645 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000646
Paul Bakkerbd51b262014-07-10 15:26:12 +0200647exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100648 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
649}
650/* END_CASE */
651
652/* BEGIN_CASE */
653void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
654{
655 mbedtls_mpi X, Y, X0;
656 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
657
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100658 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
659 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
660 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100661 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
662
663 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
664 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
665 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
666
667exit:
668 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000669}
Paul Bakker33b43f12013-08-20 11:48:36 +0200670/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000671
Paul Bakker33b43f12013-08-20 11:48:36 +0200672/* BEGIN_CASE */
673void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_mpi X;
676 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
679 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
680 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000681
Paul Bakkerbd51b262014-07-10 15:26:12 +0200682exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000684}
Paul Bakker33b43f12013-08-20 11:48:36 +0200685/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000686
Paul Bakker33b43f12013-08-20 11:48:36 +0200687/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_mpi X;
691 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100694 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
696 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100697 TEST_ASSERT( X.n == (size_t) after );
698
Paul Bakkerbd51b262014-07-10 15:26:12 +0200699exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100701}
702/* END_CASE */
703
704/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100705void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
706 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100707{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_mpi X, Y, XX;
709 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100712 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100714 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
718 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
721 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100722
Paul Bakkerbd51b262014-07-10 15:26:12 +0200723exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100725}
726/* END_CASE */
727
728/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100729void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
730 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
735 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100738 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100740 Y.s = y_sign;
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
743 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
746 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
747 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
750 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
751 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100752
Paul Bakkerbd51b262014-07-10 15:26:12 +0200753exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
755 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100756}
757/* END_CASE */
758
759/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100760void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000761{
Gilles Peskine7428b452020-01-20 21:01:51 +0100762 mbedtls_mpi X, Y;
763 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
766 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100767 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
768 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine7428b452020-01-20 21:01:51 +0100771 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
772 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000773
Paul Bakkerbd51b262014-07-10 15:26:12 +0200774exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100775 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
776}
777/* END_CASE */
778
779/* BEGIN_CASE */
780void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
781{
782 mbedtls_mpi X, Y, X0, Y0;
783 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
784 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
785
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100786 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
787 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
788 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
789 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100790
791 mbedtls_mpi_swap( &X, &Y );
792 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
793 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
794
795exit:
796 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
797 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
798}
799/* END_CASE */
800
801/* BEGIN_CASE */
802void mpi_swap_self( data_t *input_X )
803{
804 mbedtls_mpi X, X0;
805 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
806
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100807 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
808 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100809
810 mbedtls_mpi_swap( &X, &X );
811 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
812
813exit:
814 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000815}
Paul Bakker33b43f12013-08-20 11:48:36 +0200816/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000817
Paul Bakker33b43f12013-08-20 11:48:36 +0200818/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100819void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
820 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000821{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_mpi X, Y, Z, A;
823 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
826 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
827 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
828 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
829 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000830
Paul Bakkerbd51b262014-07-10 15:26:12 +0200831exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000833}
Paul Bakker33b43f12013-08-20 11:48:36 +0200834/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000835
Paul Bakker33b43f12013-08-20 11:48:36 +0200836/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100837void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
838 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100839{
840 mbedtls_mpi X, A;
841 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
842
Janos Follath044a86b2015-10-25 10:58:03 +0100843 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100844
845 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
846 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
847 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
848
849 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
850 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
851 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
852
853 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100854 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
855 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
856
857exit:
858 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
859}
860/* END_CASE */
861
862
863/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100864void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
865 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000866{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_mpi X, Y, Z, A;
868 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
871 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
872 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
873 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
874 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000875
Paul Bakkerbd51b262014-07-10 15:26:12 +0200876exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000878}
Paul Bakker33b43f12013-08-20 11:48:36 +0200879/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000880
Paul Bakker33b43f12013-08-20 11:48:36 +0200881/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100882void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
883 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000884{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_mpi X, Y, A;
886 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
889 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
890 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
891 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
892 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000893
Paul Bakkerbd51b262014-07-10 15:26:12 +0200894exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000896}
Paul Bakker33b43f12013-08-20 11:48:36 +0200897/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000898
Paul Bakker33b43f12013-08-20 11:48:36 +0200899/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100900void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
901 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000902{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_mpi X, Y, A;
904 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
907 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
908 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
909 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
910 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000911
Paul Bakkerbd51b262014-07-10 15:26:12 +0200912exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000914}
Paul Bakker33b43f12013-08-20 11:48:36 +0200915/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000916
Paul Bakker33b43f12013-08-20 11:48:36 +0200917/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100918void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
919 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_mpi X, Z, A;
922 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
925 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
926 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
927 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000928
Paul Bakkerbd51b262014-07-10 15:26:12 +0200929exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000931}
Paul Bakker33b43f12013-08-20 11:48:36 +0200932/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000933
Paul Bakker33b43f12013-08-20 11:48:36 +0200934/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100935void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
936 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 mbedtls_mpi X, Y, Z, A;
939 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
942 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
943 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
944 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
945 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000946
Paul Bakkerbd51b262014-07-10 15:26:12 +0200947exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000949}
Paul Bakker33b43f12013-08-20 11:48:36 +0200950/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000951
Paul Bakker33b43f12013-08-20 11:48:36 +0200952/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100953void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
954 char * input_Y, int radix_A, char * input_A,
955 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000956{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000958 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
962 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
963 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200966 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000967 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000969
Paul Bakkerbd51b262014-07-10 15:26:12 +0200970exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000972}
Paul Bakker33b43f12013-08-20 11:48:36 +0200973/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000974
Paul Bakker33b43f12013-08-20 11:48:36 +0200975/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100976void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
977 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000978{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_mpi X, Z, A;
980 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
983 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
984 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
985 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000986
Paul Bakkerbd51b262014-07-10 15:26:12 +0200987exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000989}
Paul Bakker33b43f12013-08-20 11:48:36 +0200990/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000991
Paul Bakker33b43f12013-08-20 11:48:36 +0200992/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100993void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
994 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000995{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_mpi X, Y, Z, A;
997 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1000 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1001 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1002 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
1003 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001004
Paul Bakkerbd51b262014-07-10 15:26:12 +02001005exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001007}
Paul Bakker33b43f12013-08-20 11:48:36 +02001008/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001009
Paul Bakker33b43f12013-08-20 11:48:36 +02001010/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001011void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1012 int radix_A, char * input_A,
1013 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_mpi X, Z, A;
1016 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1019 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1020 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001021 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001023 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001025 else
1026 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001027
Paul Bakkerbd51b262014-07-10 15:26:12 +02001028exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001030}
Paul Bakker33b43f12013-08-20 11:48:36 +02001031/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001032
Paul Bakker33b43f12013-08-20 11:48:36 +02001033/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001034void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1035 char * input_Y, int radix_A, char * input_A,
1036 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001037{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001039 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1041 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1044 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1045 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1046 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1047 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001048 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001049 if( res == 0 )
1050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1052 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001053 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001054
Paul Bakkerbd51b262014-07-10 15:26:12 +02001055exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1057 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001058}
Paul Bakker33b43f12013-08-20 11:48:36 +02001059/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001060
Paul Bakker33b43f12013-08-20 11:48:36 +02001061/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001062void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1063 int radix_A, char * input_A, int radix_B,
1064 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001067 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1069 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1072 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1073 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1074 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001075 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001076 if( res == 0 )
1077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1079 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001080 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001081
Paul Bakkerbd51b262014-07-10 15:26:12 +02001082exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1084 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001085}
Paul Bakker33b43f12013-08-20 11:48:36 +02001086/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001087
Paul Bakker33b43f12013-08-20 11:48:36 +02001088/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001089void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1090 char * input_Y, int radix_A, char * input_A,
1091 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001094 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1098 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1099 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1100 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001101 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001102 if( res == 0 )
1103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001105 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001106
Paul Bakkerbd51b262014-07-10 15:26:12 +02001107exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001109}
Paul Bakker33b43f12013-08-20 11:48:36 +02001110/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001111
Paul Bakker33b43f12013-08-20 11:48:36 +02001112/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001113void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1114 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001117 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 mbedtls_mpi_uint r;
1119 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1122 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001123 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001124 if( res == 0 )
1125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001127 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001128
Paul Bakkerbd51b262014-07-10 15:26:12 +02001129exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001131}
Paul Bakker33b43f12013-08-20 11:48:36 +02001132/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001133
Paul Bakker33b43f12013-08-20 11:48:36 +02001134/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001135void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1136 char * input_E, int radix_N, char * input_N,
1137 int radix_RR, char * input_RR, int radix_X,
1138 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001141 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1143 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1146 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1147 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1148 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001149
Paul Bakker33b43f12013-08-20 11:48:36 +02001150 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001154 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001155 if( res == 0 )
1156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001158 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001159
Paul Bakkerbd51b262014-07-10 15:26:12 +02001160exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1162 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001163}
Paul Bakker33b43f12013-08-20 11:48:36 +02001164/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001165
Paul Bakker33b43f12013-08-20 11:48:36 +02001166/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001167void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1168 char * input_Y, int radix_A, char * input_A,
1169 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001172 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1176 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1177 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1178 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001179 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001180 if( res == 0 )
1181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001183 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001184
Paul Bakkerbd51b262014-07-10 15:26:12 +02001185exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001187}
Paul Bakker33b43f12013-08-20 11:48:36 +02001188/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001191void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001194 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001198 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001199 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001200
Paul Bakkerbd51b262014-07-10 15:26:12 +02001201exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001203}
Paul Bakker33b43f12013-08-20 11:48:36 +02001204/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001207void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001208 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001209{
1210 mbedtls_mpi X;
1211 int res;
1212 mbedtls_test_mpi_random rand;
1213
1214 mbedtls_mpi_init( &X );
1215 rand.data = witnesses;
1216 rand.pos = 0;
1217 rand.chunk_len = chunk_len;
1218
1219 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001220 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1221 mbedtls_test_mpi_miller_rabin_determinizer,
1222 &rand );
1223 TEST_ASSERT( res == 0 );
1224
1225 rand.data = witnesses;
1226 rand.pos = 0;
1227 rand.chunk_len = chunk_len;
1228
Janos Follatha0b67c22018-09-18 14:48:23 +01001229 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1230 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001231 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001232 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001233
1234exit:
1235 mbedtls_mpi_free( &X );
1236}
1237/* END_CASE */
1238
1239/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001240void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001243 int my_ret;
1244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001246
Ronald Cron351f0ee2020-06-10 12:12:18 +02001247 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001248 TEST_ASSERT( my_ret == ref_ret );
1249
1250 if( ref_ret == 0 )
1251 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001252 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001253
1254 TEST_ASSERT( actual_bits >= (size_t) bits );
1255 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1256
Ronald Cron351f0ee2020-06-10 12:12:18 +02001257 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL )
Janos Follatha0b67c22018-09-18 14:48:23 +01001258 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001259 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001260 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001261 /* X = ( X - 1 ) / 2 */
1262 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001263 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL )
Janos Follatha0b67c22018-09-18 14:48:23 +01001264 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001265 }
1266 }
1267
Paul Bakkerbd51b262014-07-10 15:26:12 +02001268exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001270}
1271/* END_CASE */
1272
Paul Bakker33b43f12013-08-20 11:48:36 +02001273/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001274void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1275 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001276{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 mbedtls_mpi X, A;
1278 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1281 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1282 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1283 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001284
Paul Bakkerbd51b262014-07-10 15:26:12 +02001285exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001287}
Paul Bakker33b43f12013-08-20 11:48:36 +02001288/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001289
Paul Bakker33b43f12013-08-20 11:48:36 +02001290/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001291void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1292 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_mpi X, A;
1295 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1298 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1299 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1300 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001301
Paul Bakkerbd51b262014-07-10 15:26:12 +02001302exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001304}
Paul Bakker33b43f12013-08-20 11:48:36 +02001305/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001308void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001309{
Andres AG93012e82016-09-09 09:10:28 +01001310 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001311}
Paul Bakker33b43f12013-08-20 11:48:36 +02001312/* END_CASE */