blob: bebca5a0a93ca472f9f1b9ebc3cc1732ca749ec9 [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,
243 mbedtls_mpi_fill_random( NULL, 42, rnd_std_rand,
244 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 Follath276284f2019-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 Follath276284f2019-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;
Paul Bakkere896fea2009-07-06 06:40:23 +0000319 unsigned 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 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100326 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) 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 */
Azim Khand30ca132017-06-09 04:32:58 +0100335void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100336 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100337 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000340 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000341 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000342
343 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200350 if( buflen > (size_t) output_size )
351 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200354 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000355 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000356
Azim Khand30ca132017-06-09 04:32:58 +0100357 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000358 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000359
Paul Bakkerbd51b262014-07-10 15:26:12 +0200360exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000362}
Paul Bakker33b43f12013-08-20 11:48:36 +0200363/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100366void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100367 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000370 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000371 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000372 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000373 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000374
375 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000378
Paul Bakker33b43f12013-08-20 11:48:36 +0200379 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200380 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000382 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000383 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000384
Paul Bakker33b43f12013-08-20 11:48:36 +0200385 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 buflen = mbedtls_mpi_size( &X );
388 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000389
Paul Bakkere896fea2009-07-06 06:40:23 +0000390
Azim Khand30ca132017-06-09 04:32:58 +0100391 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000392 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000393
Paul Bakkerbd51b262014-07-10 15:26:12 +0200394exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000396}
Paul Bakker33b43f12013-08-20 11:48:36 +0200397/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100400void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
401 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000404 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200405 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000410
Paul Bakker33b43f12013-08-20 11:48:36 +0200411 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000412 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200413 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000414 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200415 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000416
Paul Bakker33b43f12013-08-20 11:48:36 +0200417 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000418 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200419 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000420 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200421 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000424
Paul Bakkerbd51b262014-07-10 15:26:12 +0200425exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000427}
Paul Bakker33b43f12013-08-20 11:48:36 +0200428/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000429
Paul Bakker33b43f12013-08-20 11:48:36 +0200430/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100431void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000432{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_mpi X;
434 mbedtls_mpi_init( &X );
435 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
436 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000437
Paul Bakkerbd51b262014-07-10 15:26:12 +0200438exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000440}
Paul Bakker33b43f12013-08-20 11:48:36 +0200441/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000442
Paul Bakker33b43f12013-08-20 11:48:36 +0200443/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100444void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
445 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000446{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_mpi X, Y;
448 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
451 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100452 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
453
454 if( result == 0 )
455 {
456 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
457 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000458
Paul Bakkerbd51b262014-07-10 15:26:12 +0200459exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000461}
Paul Bakker33b43f12013-08-20 11:48:36 +0200462/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000463
Paul Bakker33b43f12013-08-20 11:48:36 +0200464/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100465void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000466{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_mpi X;
468 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
471 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000472
Paul Bakkerbd51b262014-07-10 15:26:12 +0200473exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000475}
Paul Bakker33b43f12013-08-20 11:48:36 +0200476/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000477
Paul Bakker33b43f12013-08-20 11:48:36 +0200478/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100479void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000480{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_mpi X;
482 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200485 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000486
Paul Bakkerbd51b262014-07-10 15:26:12 +0200487exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000489}
Paul Bakker33b43f12013-08-20 11:48:36 +0200490/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000491
Paul Bakker33b43f12013-08-20 11:48:36 +0200492/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100493void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
494 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000495{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_mpi A, X, Y, Z;
497 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
500 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
501 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
502 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
503 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000504
Paul Bakkerbd51b262014-07-10 15:26:12 +0200505exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000507}
Paul Bakker33b43f12013-08-20 11:48:36 +0200508/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000509
Paul Bakker33b43f12013-08-20 11:48:36 +0200510/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000512{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_mpi X;
514 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
517 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000518
Paul Bakkerbd51b262014-07-10 15:26:12 +0200519exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000521}
Paul Bakker33b43f12013-08-20 11:48:36 +0200522/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000523
Paul Bakker33b43f12013-08-20 11:48:36 +0200524/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100525void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
526 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000527{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_mpi X, Y;
529 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
532 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
533 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000534
Paul Bakkerbd51b262014-07-10 15:26:12 +0200535exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000537}
Paul Bakker33b43f12013-08-20 11:48:36 +0200538/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000539
Paul Bakker33b43f12013-08-20 11:48:36 +0200540/* BEGIN_CASE */
Janos Follath27d221a2019-10-14 09:21:49 +0100541void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
542 int size_Y, char * input_Y,
Janos Follath867a3ab2019-10-11 14:21:53 +0100543 int input_ret, int input_err )
Janos Follathe9ae6302019-09-11 16:07:14 +0100544{
Janos Follath867a3ab2019-10-11 14:21:53 +0100545 unsigned ret;
546 unsigned input_uret = input_ret;
Janos Follathe9ae6302019-09-11 16:07:14 +0100547 mbedtls_mpi X, Y;
548 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
549
Janos Follath27d221a2019-10-14 09:21:49 +0100550 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
551 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100552
Gilles Peskine1a30fbb2020-01-21 16:30:53 +0100553 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
554 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100555
Janos Follath867a3ab2019-10-11 14:21:53 +0100556 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follathe9ae6302019-09-11 16:07:14 +0100557 if( input_err == 0 )
Janos Follath867a3ab2019-10-11 14:21:53 +0100558 TEST_ASSERT( ret == input_uret );
Janos Follathe9ae6302019-09-11 16:07:14 +0100559
560exit:
561 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
562}
563/* END_CASE */
564
565/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100566void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
567 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_mpi X, Y;
570 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
573 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
574 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000575
Paul Bakkerbd51b262014-07-10 15:26:12 +0200576exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000578}
Paul Bakker33b43f12013-08-20 11:48:36 +0200579/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000580
Paul Bakker33b43f12013-08-20 11:48:36 +0200581/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582void mbedtls_mpi_copy( int input_X, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000583{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_mpi X, Y, A;
585 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
588 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_A ) == 0 );
589 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 );
590 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
591 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
592 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
593 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
594 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000595
Paul Bakkerbd51b262014-07-10 15:26:12 +0200596exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000598}
Paul Bakker33b43f12013-08-20 11:48:36 +0200599/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000600
Paul Bakker33b43f12013-08-20 11:48:36 +0200601/* BEGIN_CASE */
602void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000603{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_mpi X;
605 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
608 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
609 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000610
Paul Bakkerbd51b262014-07-10 15:26:12 +0200611exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000613}
Paul Bakker33b43f12013-08-20 11:48:36 +0200614/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000615
Paul Bakker33b43f12013-08-20 11:48:36 +0200616/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_mpi X;
620 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100623 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
625 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100626 TEST_ASSERT( X.n == (size_t) after );
627
Paul Bakkerbd51b262014-07-10 15:26:12 +0200628exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100630}
631/* END_CASE */
632
633/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100634void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
635 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_mpi X, Y, XX;
638 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100641 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100643 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
647 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
650 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100651
Paul Bakkerbd51b262014-07-10 15:26:12 +0200652exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100654}
655/* END_CASE */
656
657/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100658void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
659 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100660{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
664 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100667 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100669 Y.s = y_sign;
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
672 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
675 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
676 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
679 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
680 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100681
Paul Bakkerbd51b262014-07-10 15:26:12 +0200682exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
684 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100685}
686/* END_CASE */
687
688/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100689void mbedtls_mpi_swap( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_mpi X, Y, A;
692 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
695 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
696 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 );
697 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
698 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
699 mbedtls_mpi_swap( &X, &Y );
700 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
701 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000702
Paul Bakkerbd51b262014-07-10 15:26:12 +0200703exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000705}
Paul Bakker33b43f12013-08-20 11:48:36 +0200706/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000707
Paul Bakker33b43f12013-08-20 11:48:36 +0200708/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100709void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
710 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000711{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_mpi X, Y, Z, A;
713 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
716 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
717 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
718 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
719 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000720
Paul Bakkerbd51b262014-07-10 15:26:12 +0200721exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000723}
Paul Bakker33b43f12013-08-20 11:48:36 +0200724/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000725
Paul Bakker33b43f12013-08-20 11:48:36 +0200726/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100727void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
728 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100729{
730 mbedtls_mpi X, A;
731 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
732
Janos Follath044a86b2015-10-25 10:58:03 +0100733 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100734
735 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
736 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
737 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
738
739 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
740 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
741 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
742
743 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100744 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
745 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
746
747exit:
748 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
749}
750/* END_CASE */
751
752
753/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100754void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
755 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_mpi X, Y, Z, A;
758 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
761 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
762 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
763 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
764 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000765
Paul Bakkerbd51b262014-07-10 15:26:12 +0200766exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000768}
Paul Bakker33b43f12013-08-20 11:48:36 +0200769/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000770
Paul Bakker33b43f12013-08-20 11:48:36 +0200771/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100772void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
773 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000774{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_mpi X, Y, A;
776 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
779 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
780 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
781 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
782 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000783
Paul Bakkerbd51b262014-07-10 15:26:12 +0200784exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000786}
Paul Bakker33b43f12013-08-20 11:48:36 +0200787/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000788
Paul Bakker33b43f12013-08-20 11:48:36 +0200789/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100790void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
791 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000792{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_mpi X, Y, A;
794 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
797 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
798 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
799 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
800 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000801
Paul Bakkerbd51b262014-07-10 15:26:12 +0200802exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000804}
Paul Bakker33b43f12013-08-20 11:48:36 +0200805/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000806
Paul Bakker33b43f12013-08-20 11:48:36 +0200807/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100808void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
809 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000810{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_mpi X, Z, A;
812 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
815 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
816 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
817 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000818
Paul Bakkerbd51b262014-07-10 15:26:12 +0200819exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000821}
Paul Bakker33b43f12013-08-20 11:48:36 +0200822/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000823
Paul Bakker33b43f12013-08-20 11:48:36 +0200824/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100825void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
826 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000827{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_mpi X, Y, Z, A;
829 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
832 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
833 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
834 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
835 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000836
Paul Bakkerbd51b262014-07-10 15:26:12 +0200837exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000839}
Paul Bakker33b43f12013-08-20 11:48:36 +0200840/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000841
Paul Bakker33b43f12013-08-20 11:48:36 +0200842/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100843void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
844 char * input_Y, int radix_A, char * input_A,
845 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000848 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
852 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
853 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200856 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000857 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000859
Paul Bakkerbd51b262014-07-10 15:26:12 +0200860exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000862}
Paul Bakker33b43f12013-08-20 11:48:36 +0200863/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000864
Paul Bakker33b43f12013-08-20 11:48:36 +0200865/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100866void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
867 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000868{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_mpi X, Z, A;
870 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
873 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
874 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_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( &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 mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
884 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000885{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_mpi X, Y, Z, A;
887 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +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_mul_mpi( &Z, &X, &Y ) == 0 );
893 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &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( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000897}
Paul Bakker33b43f12013-08-20 11:48:36 +0200898/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000899
Paul Bakker33b43f12013-08-20 11:48:36 +0200900/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100901void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
902 int radix_A, char * input_A,
903 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_mpi X, Z, A;
906 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
909 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
910 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200911 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200913 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200915 else
916 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000917
Paul Bakkerbd51b262014-07-10 15:26:12 +0200918exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000920}
Paul Bakker33b43f12013-08-20 11:48:36 +0200921/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000922
Paul Bakker33b43f12013-08-20 11:48:36 +0200923/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100924void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
925 char * input_Y, int radix_A, char * input_A,
926 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000927{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000929 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
931 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
934 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
935 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
936 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
937 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200938 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000939 if( res == 0 )
940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
942 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000943 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000944
Paul Bakkerbd51b262014-07-10 15:26:12 +0200945exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
947 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000948}
Paul Bakker33b43f12013-08-20 11:48:36 +0200949/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000950
Paul Bakker33b43f12013-08-20 11:48:36 +0200951/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100952void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
953 int radix_A, char * input_A, int radix_B,
954 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000957 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
959 mbedtls_mpi_init( &B );
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( &A, radix_A, input_A ) == 0 );
963 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
964 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200965 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000966 if( res == 0 )
967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
969 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000970 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000971
Paul Bakkerbd51b262014-07-10 15:26:12 +0200972exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
974 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000975}
Paul Bakker33b43f12013-08-20 11:48:36 +0200976/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000977
Paul Bakker33b43f12013-08-20 11:48:36 +0200978/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100979void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
980 char * input_Y, int radix_A, char * input_A,
981 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000982{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000984 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
988 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
989 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
990 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200991 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000992 if( res == 0 )
993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000995 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000996
Paul Bakkerbd51b262014-07-10 15:26:12 +0200997exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000999}
Paul Bakker33b43f12013-08-20 11:48:36 +02001000/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001001
Paul Bakker33b43f12013-08-20 11:48:36 +02001002/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001003void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1004 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001005{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001007 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_mpi_uint r;
1009 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1012 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001013 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001014 if( res == 0 )
1015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001017 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001018
Paul Bakkerbd51b262014-07-10 15:26:12 +02001019exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001021}
Paul Bakker33b43f12013-08-20 11:48:36 +02001022/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001023
Paul Bakker33b43f12013-08-20 11:48:36 +02001024/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001025void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1026 char * input_E, int radix_N, char * input_N,
1027 int radix_RR, char * input_RR, int radix_X,
1028 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001031 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1033 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1036 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1037 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1038 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001039
Paul Bakker33b43f12013-08-20 11:48:36 +02001040 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001044 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001045 if( res == 0 )
1046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001048 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001049
Paul Bakkerbd51b262014-07-10 15:26:12 +02001050exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1052 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001053}
Paul Bakker33b43f12013-08-20 11:48:36 +02001054/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001055
Paul Bakker33b43f12013-08-20 11:48:36 +02001056/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001057void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1058 char * input_Y, int radix_A, char * input_A,
1059 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001062 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1066 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1067 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1068 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001069 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001070 if( res == 0 )
1071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001073 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001074
Paul Bakkerbd51b262014-07-10 15:26:12 +02001075exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001077}
Paul Bakker33b43f12013-08-20 11:48:36 +02001078/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001081void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001082{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001084 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001088 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001089 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001090
Paul Bakkerbd51b262014-07-10 15:26:12 +02001091exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001093}
Paul Bakker33b43f12013-08-20 11:48:36 +02001094/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001097void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001098 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001099{
1100 mbedtls_mpi X;
1101 int res;
1102 mbedtls_test_mpi_random rand;
1103
1104 mbedtls_mpi_init( &X );
1105 rand.data = witnesses;
1106 rand.pos = 0;
1107 rand.chunk_len = chunk_len;
1108
1109 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001110 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1111 mbedtls_test_mpi_miller_rabin_determinizer,
1112 &rand );
1113 TEST_ASSERT( res == 0 );
1114
1115 rand.data = witnesses;
1116 rand.pos = 0;
1117 rand.chunk_len = chunk_len;
1118
Janos Follatha0b67c22018-09-18 14:48:23 +01001119 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1120 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001121 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001122 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001123
1124exit:
1125 mbedtls_mpi_free( &X );
1126}
1127/* END_CASE */
1128
1129/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001130void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001133 int my_ret;
1134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001136
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001137 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001138 TEST_ASSERT( my_ret == ref_ret );
1139
1140 if( ref_ret == 0 )
1141 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001142 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001143
1144 TEST_ASSERT( actual_bits >= (size_t) bits );
1145 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1146
Janos Follatha0b67c22018-09-18 14:48:23 +01001147 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1148 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001149 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001150 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001151 /* X = ( X - 1 ) / 2 */
1152 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001153 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1154 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001155 }
1156 }
1157
Paul Bakkerbd51b262014-07-10 15:26:12 +02001158exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001160}
1161/* END_CASE */
1162
Paul Bakker33b43f12013-08-20 11:48:36 +02001163/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001164void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1165 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001166{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 mbedtls_mpi X, A;
1168 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1171 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1172 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1173 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001174
Paul Bakkerbd51b262014-07-10 15:26:12 +02001175exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001177}
Paul Bakker33b43f12013-08-20 11:48:36 +02001178/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001179
Paul Bakker33b43f12013-08-20 11:48:36 +02001180/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001181void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1182 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 mbedtls_mpi X, A;
1185 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1188 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1189 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1190 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001191
Paul Bakkerbd51b262014-07-10 15:26:12 +02001192exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001194}
Paul Bakker33b43f12013-08-20 11:48:36 +02001195/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001198void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001199{
Andres AG93012e82016-09-09 09:10:28 +01001200 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001201}
Paul Bakker33b43f12013-08-20 11:48:36 +02001202/* END_CASE */