blob: e54aaffe63b20388e81d6afd47ac8665b9261222 [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 Cron6c5bd7f2020-06-10 14:08:26 +0200243 mbedtls_mpi_fill_random( NULL, 42,
244 mbedtls_test_rnd_std_rand,
Hanno Beckerafb607b2018-12-11 14:27:08 +0000245 NULL ) );
246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
247 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000248
Hanno Beckerafb607b2018-12-11 14:27:08 +0000249 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
250 mbedtls_mpi_gcd( NULL, &X, &X ) );
251 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
252 mbedtls_mpi_gcd( &X, NULL, &X ) );
253 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
254 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000255
Hanno Beckerafb607b2018-12-11 14:27:08 +0000256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
259 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000261 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000262
263exit:
264 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000265}
266/* END_CASE */
267
Paul Bakker33b43f12013-08-20 11:48:36 +0200268/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100269void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200270{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200271 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200272
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200273 mbedtls_mpi_init( &X );
274 mbedtls_mpi_init( &Y );
275 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200276
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200277 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
278 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200279 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200280 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200281
282exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200283 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200284}
285/* END_CASE */
286
287/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100288void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
289 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200290 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000293 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100294 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000297
Janos Follath04dadb72019-03-06 12:29:37 +0000298 memset( str, '!', sizeof( str ) );
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200301 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000302 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100303 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200304 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000305 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200306 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000307 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000308 }
309 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000310
Paul Bakkerbd51b262014-07-10 15:26:12 +0200311exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000313}
Paul Bakker33b43f12013-08-20 11:48:36 +0200314/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000315
Paul Bakker33b43f12013-08-20 11:48:36 +0200316/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100317void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000320 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100321 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000324
Paul Bakkere896fea2009-07-06 06:40:23 +0000325
Azim Khand30ca132017-06-09 04:32:58 +0100326 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000327 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200328 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000329
Paul Bakkerbd51b262014-07-10 15:26:12 +0200330exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000332}
Paul Bakker33b43f12013-08-20 11:48:36 +0200333/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000334
Paul Bakker33b43f12013-08-20 11:48:36 +0200335/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000336void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
337{
338 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000339 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000340 size_t len;
341
342 mbedtls_mpi_init( &X );
343
344
345 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000346 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000347 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
348
349exit:
350 mbedtls_mpi_free( &X );
351}
352/* END_CASE */
353
354/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100355void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100356 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100357 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000358{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000360 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000361 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000362
363 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200370 if( buflen > (size_t) output_size )
371 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200374 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000375 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000376
Ronald Cron2dbba992020-06-10 11:42:32 +0200377 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
378 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000379 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000380
Paul Bakkerbd51b262014-07-10 15:26:12 +0200381exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000383}
Paul Bakker33b43f12013-08-20 11:48:36 +0200384/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000385
Janos Follathe344d0f2019-02-19 16:17:40 +0000386/* BEGIN_CASE */
387void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
388 data_t * input_A, int output_size,
389 int result )
390{
391 mbedtls_mpi X;
392 unsigned char buf[1000];
393 size_t buflen;
394
395 memset( buf, 0x00, 1000 );
396
397 mbedtls_mpi_init( &X );
398
399 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
400
401 buflen = mbedtls_mpi_size( &X );
402 if( buflen > (size_t) output_size )
403 buflen = (size_t) output_size;
404
405 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
406 if( result == 0)
407 {
408
Ronald Cron2dbba992020-06-10 11:42:32 +0200409 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
410 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000411 }
412
413exit:
414 mbedtls_mpi_free( &X );
415}
416/* END_CASE */
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100419void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100420 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000421{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000423 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000424 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000425 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000426 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000427
428 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000431
Paul Bakker33b43f12013-08-20 11:48:36 +0200432 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200433 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000435 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000436 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000437
Paul Bakker33b43f12013-08-20 11:48:36 +0200438 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 buflen = mbedtls_mpi_size( &X );
441 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000442
Paul Bakkere896fea2009-07-06 06:40:23 +0000443
Ronald Cron2dbba992020-06-10 11:42:32 +0200444 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
445 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000446 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000447
Paul Bakkerbd51b262014-07-10 15:26:12 +0200448exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000450}
Paul Bakker33b43f12013-08-20 11:48:36 +0200451/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100454void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
455 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000458 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200459 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000464
Paul Bakker33b43f12013-08-20 11:48:36 +0200465 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000466 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200467 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000468 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200469 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000470
Paul Bakker33b43f12013-08-20 11:48:36 +0200471 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000472 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200473 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000474 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200475 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000478
Paul Bakkerbd51b262014-07-10 15:26:12 +0200479exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000481}
Paul Bakker33b43f12013-08-20 11:48:36 +0200482/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000483
Paul Bakker33b43f12013-08-20 11:48:36 +0200484/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100485void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_mpi X;
488 mbedtls_mpi_init( &X );
489 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
490 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000491
Paul Bakkerbd51b262014-07-10 15:26:12 +0200492exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000494}
Paul Bakker33b43f12013-08-20 11:48:36 +0200495/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000496
Paul Bakker33b43f12013-08-20 11:48:36 +0200497/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100498void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
499 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000500{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_mpi X, Y;
502 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
505 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100506 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
507
508 if( result == 0 )
509 {
510 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
511 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000512
Paul Bakkerbd51b262014-07-10 15:26:12 +0200513exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000515}
Paul Bakker33b43f12013-08-20 11:48:36 +0200516/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000517
Paul Bakker33b43f12013-08-20 11:48:36 +0200518/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100519void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000520{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_mpi X;
522 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
525 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000526
Paul Bakkerbd51b262014-07-10 15:26:12 +0200527exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000529}
Paul Bakker33b43f12013-08-20 11:48:36 +0200530/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000531
Paul Bakker33b43f12013-08-20 11:48:36 +0200532/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100533void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_mpi X;
536 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200539 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000540
Paul Bakkerbd51b262014-07-10 15:26:12 +0200541exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000543}
Paul Bakker33b43f12013-08-20 11:48:36 +0200544/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000545
Paul Bakker33b43f12013-08-20 11:48:36 +0200546/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100547void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
548 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_mpi A, X, Y, Z;
551 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
554 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
555 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
556 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
557 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000558
Paul Bakkerbd51b262014-07-10 15:26:12 +0200559exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000561}
Paul Bakker33b43f12013-08-20 11:48:36 +0200562/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000563
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_mpi X;
568 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
571 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000572
Paul Bakkerbd51b262014-07-10 15:26:12 +0200573exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000575}
Paul Bakker33b43f12013-08-20 11:48:36 +0200576/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000577
Paul Bakker33b43f12013-08-20 11:48:36 +0200578/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100579void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
580 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000581{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_mpi X, Y;
583 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
586 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
587 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000588
Paul Bakkerbd51b262014-07-10 15:26:12 +0200589exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000591}
Paul Bakker33b43f12013-08-20 11:48:36 +0200592/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000593
Paul Bakker33b43f12013-08-20 11:48:36 +0200594/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100595void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
596 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100597 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100598{
Janos Follath0e5532d2019-10-11 14:21:53 +0100599 unsigned ret;
600 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100601 mbedtls_mpi X, Y;
602 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
603
Janos Follathb7e1b492019-10-14 09:21:49 +0100604 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
605 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100606
Gilles Peskine9018b112020-01-21 16:30:53 +0100607 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
608 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100609
Janos Follath0e5532d2019-10-11 14:21:53 +0100610 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100611 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100612 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100613
614exit:
615 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
616}
617/* END_CASE */
618
619/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100620void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
621 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_mpi X, Y;
624 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
627 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
628 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000629
Paul Bakkerbd51b262014-07-10 15:26:12 +0200630exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000632}
Paul Bakker33b43f12013-08-20 11:48:36 +0200633/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000634
Paul Bakker33b43f12013-08-20 11:48:36 +0200635/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100636void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000637{
Gilles Peskine7428b452020-01-20 21:01:51 +0100638 mbedtls_mpi X, Y;
639 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100642 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100645 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
646 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000647
Paul Bakkerbd51b262014-07-10 15:26:12 +0200648exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100649 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
650}
651/* END_CASE */
652
653/* BEGIN_CASE */
654void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
655{
656 mbedtls_mpi X, Y, X0;
657 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
658
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100659 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
660 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
661 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100662 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
663
664 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
665 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
666 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
667
668exit:
669 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000670}
Paul Bakker33b43f12013-08-20 11:48:36 +0200671/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000672
Paul Bakker33b43f12013-08-20 11:48:36 +0200673/* BEGIN_CASE */
674void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_mpi X;
677 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
680 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
681 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000682
Paul Bakkerbd51b262014-07-10 15:26:12 +0200683exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000685}
Paul Bakker33b43f12013-08-20 11:48:36 +0200686/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000687
Paul Bakker33b43f12013-08-20 11:48:36 +0200688/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_mpi X;
692 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100695 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
697 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100698 TEST_ASSERT( X.n == (size_t) after );
699
Paul Bakkerbd51b262014-07-10 15:26:12 +0200700exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100702}
703/* END_CASE */
704
705/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100706void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
707 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_mpi X, Y, XX;
710 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100713 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100715 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
719 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
722 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100723
Paul Bakkerbd51b262014-07-10 15:26:12 +0200724exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100726}
727/* END_CASE */
728
729/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100730void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
731 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
736 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100739 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100741 Y.s = y_sign;
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
744 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
747 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
748 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
751 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
752 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100753
Paul Bakkerbd51b262014-07-10 15:26:12 +0200754exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
756 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100757}
758/* END_CASE */
759
760/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100761void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000762{
Gilles Peskine7428b452020-01-20 21:01:51 +0100763 mbedtls_mpi X, Y;
764 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
767 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100768 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
769 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine7428b452020-01-20 21:01:51 +0100772 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
773 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000774
Paul Bakkerbd51b262014-07-10 15:26:12 +0200775exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100776 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
777}
778/* END_CASE */
779
780/* BEGIN_CASE */
781void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
782{
783 mbedtls_mpi X, Y, X0, Y0;
784 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
785 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
786
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100787 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
788 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
789 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
790 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100791
792 mbedtls_mpi_swap( &X, &Y );
793 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
794 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
795
796exit:
797 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
798 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
799}
800/* END_CASE */
801
802/* BEGIN_CASE */
803void mpi_swap_self( data_t *input_X )
804{
805 mbedtls_mpi X, X0;
806 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
807
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100808 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
809 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100810
811 mbedtls_mpi_swap( &X, &X );
812 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
813
814exit:
815 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000816}
Paul Bakker33b43f12013-08-20 11:48:36 +0200817/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000818
Paul Bakker33b43f12013-08-20 11:48:36 +0200819/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100820void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
821 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000822{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_mpi X, Y, Z, A;
824 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
827 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
828 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
829 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
830 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000831
Paul Bakkerbd51b262014-07-10 15:26:12 +0200832exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000834}
Paul Bakker33b43f12013-08-20 11:48:36 +0200835/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000836
Paul Bakker33b43f12013-08-20 11:48:36 +0200837/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100838void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
839 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100840{
841 mbedtls_mpi X, A;
842 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
843
Janos Follath044a86b2015-10-25 10:58:03 +0100844 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100845
846 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
847 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
848 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
849
850 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
851 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
852 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
853
854 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100855 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
856 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
857
858exit:
859 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
860}
861/* END_CASE */
862
863
864/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100865void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
866 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000867{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_mpi X, Y, Z, A;
869 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
872 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
873 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
874 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
875 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000876
Paul Bakkerbd51b262014-07-10 15:26:12 +0200877exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000879}
Paul Bakker33b43f12013-08-20 11:48:36 +0200880/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000881
Paul Bakker33b43f12013-08-20 11:48:36 +0200882/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100883void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
884 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000885{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_mpi X, Y, A;
887 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
890 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
891 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
892 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
893 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000894
Paul Bakkerbd51b262014-07-10 15:26:12 +0200895exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000897}
Paul Bakker33b43f12013-08-20 11:48:36 +0200898/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000899
Paul Bakker33b43f12013-08-20 11:48:36 +0200900/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100901void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
902 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_mpi X, Y, A;
905 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
908 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
909 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
910 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
911 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000912
Paul Bakkerbd51b262014-07-10 15:26:12 +0200913exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000915}
Paul Bakker33b43f12013-08-20 11:48:36 +0200916/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000917
Paul Bakker33b43f12013-08-20 11:48:36 +0200918/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100919void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
920 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 mbedtls_mpi X, Z, A;
923 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
926 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
927 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
928 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000929
Paul Bakkerbd51b262014-07-10 15:26:12 +0200930exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000932}
Paul Bakker33b43f12013-08-20 11:48:36 +0200933/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000934
Paul Bakker33b43f12013-08-20 11:48:36 +0200935/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100936void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
937 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_mpi X, Y, Z, A;
940 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
943 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
944 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
945 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
946 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000947
Paul Bakkerbd51b262014-07-10 15:26:12 +0200948exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000950}
Paul Bakker33b43f12013-08-20 11:48:36 +0200951/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000952
Paul Bakker33b43f12013-08-20 11:48:36 +0200953/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100954void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
955 char * input_Y, int radix_A, char * input_A,
956 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000959 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
963 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
964 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200967 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000968 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000970
Paul Bakkerbd51b262014-07-10 15:26:12 +0200971exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000973}
Paul Bakker33b43f12013-08-20 11:48:36 +0200974/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000975
Paul Bakker33b43f12013-08-20 11:48:36 +0200976/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100977void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
978 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000979{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_mpi X, Z, A;
981 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
984 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
985 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
986 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000987
Paul Bakkerbd51b262014-07-10 15:26:12 +0200988exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000990}
Paul Bakker33b43f12013-08-20 11:48:36 +0200991/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000992
Paul Bakker33b43f12013-08-20 11:48:36 +0200993/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100994void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
995 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000996{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_mpi X, Y, Z, A;
998 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1001 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1002 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1003 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
1004 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001005
Paul Bakkerbd51b262014-07-10 15:26:12 +02001006exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001008}
Paul Bakker33b43f12013-08-20 11:48:36 +02001009/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001010
Paul Bakker33b43f12013-08-20 11:48:36 +02001011/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001012void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1013 int radix_A, char * input_A,
1014 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_mpi X, Z, A;
1017 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1020 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1021 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001022 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001024 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001026 else
1027 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001028
Paul Bakkerbd51b262014-07-10 15:26:12 +02001029exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001031}
Paul Bakker33b43f12013-08-20 11:48:36 +02001032/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001033
Paul Bakker33b43f12013-08-20 11:48:36 +02001034/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001035void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1036 char * input_Y, int radix_A, char * input_A,
1037 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001038{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001040 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1042 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1045 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1046 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1047 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1048 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001049 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001050 if( res == 0 )
1051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1053 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001054 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001055
Paul Bakkerbd51b262014-07-10 15:26:12 +02001056exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1058 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
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
Paul Bakker33b43f12013-08-20 11:48:36 +02001062/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001063void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1064 int radix_A, char * input_A, int radix_B,
1065 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001068 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1070 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1073 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1074 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1075 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001076 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001077 if( res == 0 )
1078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1080 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001081 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001082
Paul Bakkerbd51b262014-07-10 15:26:12 +02001083exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1085 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001086}
Paul Bakker33b43f12013-08-20 11:48:36 +02001087/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001088
Paul Bakker33b43f12013-08-20 11:48:36 +02001089/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001090void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1091 char * input_Y, int radix_A, char * input_A,
1092 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001095 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1099 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1100 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1101 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001102 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001103 if( res == 0 )
1104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001106 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001107
Paul Bakkerbd51b262014-07-10 15:26:12 +02001108exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001110}
Paul Bakker33b43f12013-08-20 11:48:36 +02001111/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001112
Paul Bakker33b43f12013-08-20 11:48:36 +02001113/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001114void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1115 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001118 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_mpi_uint r;
1120 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1123 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001124 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001125 if( res == 0 )
1126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001128 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001129
Paul Bakkerbd51b262014-07-10 15:26:12 +02001130exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001132}
Paul Bakker33b43f12013-08-20 11:48:36 +02001133/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001134
Paul Bakker33b43f12013-08-20 11:48:36 +02001135/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001136void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1137 char * input_E, int radix_N, char * input_N,
1138 int radix_RR, char * input_RR, int radix_X,
1139 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001142 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1144 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1147 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1148 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1149 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001150
Paul Bakker33b43f12013-08-20 11:48:36 +02001151 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001155 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001156 if( res == 0 )
1157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001159 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001160
Paul Bakkerbd51b262014-07-10 15:26:12 +02001161exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1163 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001164}
Paul Bakker33b43f12013-08-20 11:48:36 +02001165/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001166
Paul Bakker33b43f12013-08-20 11:48:36 +02001167/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001168void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1169 char * input_Y, int radix_A, char * input_A,
1170 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001173 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1177 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1178 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1179 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001180 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001181 if( res == 0 )
1182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001184 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001185
Paul Bakkerbd51b262014-07-10 15:26:12 +02001186exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001188}
Paul Bakker33b43f12013-08-20 11:48:36 +02001189/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001192void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001195 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001199 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001200 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001201
Paul Bakkerbd51b262014-07-10 15:26:12 +02001202exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001204}
Paul Bakker33b43f12013-08-20 11:48:36 +02001205/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001208void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001209 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001210{
1211 mbedtls_mpi X;
1212 int res;
1213 mbedtls_test_mpi_random rand;
1214
1215 mbedtls_mpi_init( &X );
1216 rand.data = witnesses;
1217 rand.pos = 0;
1218 rand.chunk_len = chunk_len;
1219
1220 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001221 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1222 mbedtls_test_mpi_miller_rabin_determinizer,
1223 &rand );
1224 TEST_ASSERT( res == 0 );
1225
1226 rand.data = witnesses;
1227 rand.pos = 0;
1228 rand.chunk_len = chunk_len;
1229
Janos Follatha0b67c22018-09-18 14:48:23 +01001230 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1231 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001232 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001233 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001234
1235exit:
1236 mbedtls_mpi_free( &X );
1237}
1238/* END_CASE */
1239
1240/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001241void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001244 int my_ret;
1245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001247
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001248 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1249 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001250 TEST_ASSERT( my_ret == ref_ret );
1251
1252 if( ref_ret == 0 )
1253 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001254 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001255
1256 TEST_ASSERT( actual_bits >= (size_t) bits );
1257 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1258
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001259 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1260 mbedtls_test_rnd_std_rand,
1261 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001262 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001263 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001264 /* X = ( X - 1 ) / 2 */
1265 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001266 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1267 mbedtls_test_rnd_std_rand,
1268 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001269 }
1270 }
1271
Paul Bakkerbd51b262014-07-10 15:26:12 +02001272exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001274}
1275/* END_CASE */
1276
Paul Bakker33b43f12013-08-20 11:48:36 +02001277/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001278void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1279 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 mbedtls_mpi X, A;
1282 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1285 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1286 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1287 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001288
Paul Bakkerbd51b262014-07-10 15:26:12 +02001289exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001291}
Paul Bakker33b43f12013-08-20 11:48:36 +02001292/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001293
Paul Bakker33b43f12013-08-20 11:48:36 +02001294/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001295void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1296 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_mpi X, A;
1299 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1302 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1303 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1304 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001305
Paul Bakkerbd51b262014-07-10 15:26:12 +02001306exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001308}
Paul Bakker33b43f12013-08-20 11:48:36 +02001309/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001312void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001313{
Andres AG93012e82016-09-09 09:10:28 +01001314 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001315}
Paul Bakker33b43f12013-08-20 11:48:36 +02001316/* END_CASE */