blob: 67894e654327d9e3408cf6c264e944b24c915a30 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200298 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000299 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100300 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200301 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000302 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200303 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000304 }
305 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000306
Paul Bakkerbd51b262014-07-10 15:26:12 +0200307exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000309}
Paul Bakker33b43f12013-08-20 11:48:36 +0200310/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000311
Paul Bakker33b43f12013-08-20 11:48:36 +0200312/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100313void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000316 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100317 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000320
Paul Bakkere896fea2009-07-06 06:40:23 +0000321
Azim Khand30ca132017-06-09 04:32:58 +0100322 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000323 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200324 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000325
Paul Bakkerbd51b262014-07-10 15:26:12 +0200326exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000328}
Paul Bakker33b43f12013-08-20 11:48:36 +0200329/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000330
Paul Bakker33b43f12013-08-20 11:48:36 +0200331/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000332void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
333{
334 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000335 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000336 size_t len;
337
338 mbedtls_mpi_init( &X );
339
340
341 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000342 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000343 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
344
345exit:
346 mbedtls_mpi_free( &X );
347}
348/* END_CASE */
349
350/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100351void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100352 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100353 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000356 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000357 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000358
359 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200366 if( buflen > (size_t) output_size )
367 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200370 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000371 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000372
Azim Khand30ca132017-06-09 04:32:58 +0100373 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000374 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000375
Paul Bakkerbd51b262014-07-10 15:26:12 +0200376exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000378}
Paul Bakker33b43f12013-08-20 11:48:36 +0200379/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000380
Janos Follathe344d0f2019-02-19 16:17:40 +0000381/* BEGIN_CASE */
382void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
383 data_t * input_A, int output_size,
384 int result )
385{
386 mbedtls_mpi X;
387 unsigned char buf[1000];
388 size_t buflen;
389
390 memset( buf, 0x00, 1000 );
391
392 mbedtls_mpi_init( &X );
393
394 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
395
396 buflen = mbedtls_mpi_size( &X );
397 if( buflen > (size_t) output_size )
398 buflen = (size_t) output_size;
399
400 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
401 if( result == 0)
402 {
403
404 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
405 }
406
407exit:
408 mbedtls_mpi_free( &X );
409}
410/* END_CASE */
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100413void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100414 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000417 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000418 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000419 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000420 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000421
422 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000425
Paul Bakker33b43f12013-08-20 11:48:36 +0200426 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200427 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000429 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000430 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000431
Paul Bakker33b43f12013-08-20 11:48:36 +0200432 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 buflen = mbedtls_mpi_size( &X );
435 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000436
Paul Bakkere896fea2009-07-06 06:40:23 +0000437
Azim Khand30ca132017-06-09 04:32:58 +0100438 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000439 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000440
Paul Bakkerbd51b262014-07-10 15:26:12 +0200441exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000443}
Paul Bakker33b43f12013-08-20 11:48:36 +0200444/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100447void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
448 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000451 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200452 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000457
Paul Bakker33b43f12013-08-20 11:48:36 +0200458 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000459 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200460 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000461 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200462 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000463
Paul Bakker33b43f12013-08-20 11:48:36 +0200464 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000465 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200466 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000467 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200468 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000471
Paul Bakkerbd51b262014-07-10 15:26:12 +0200472exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000474}
Paul Bakker33b43f12013-08-20 11:48:36 +0200475/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000476
Paul Bakker33b43f12013-08-20 11:48:36 +0200477/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100478void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000479{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_mpi X;
481 mbedtls_mpi_init( &X );
482 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
483 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000484
Paul Bakkerbd51b262014-07-10 15:26:12 +0200485exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000487}
Paul Bakker33b43f12013-08-20 11:48:36 +0200488/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000489
Paul Bakker33b43f12013-08-20 11:48:36 +0200490/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100491void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
492 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000493{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_mpi X, Y;
495 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
498 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100499 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
500
501 if( result == 0 )
502 {
503 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
504 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000505
Paul Bakkerbd51b262014-07-10 15:26:12 +0200506exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000508}
Paul Bakker33b43f12013-08-20 11:48:36 +0200509/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000510
Paul Bakker33b43f12013-08-20 11:48:36 +0200511/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100512void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_mpi X;
515 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
518 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000519
Paul Bakkerbd51b262014-07-10 15:26:12 +0200520exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000522}
Paul Bakker33b43f12013-08-20 11:48:36 +0200523/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000524
Paul Bakker33b43f12013-08-20 11:48:36 +0200525/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100526void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000527{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_mpi X;
529 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200532 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000533
Paul Bakkerbd51b262014-07-10 15:26:12 +0200534exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000536}
Paul Bakker33b43f12013-08-20 11:48:36 +0200537/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000538
Paul Bakker33b43f12013-08-20 11:48:36 +0200539/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100540void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
541 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_mpi A, X, Y, Z;
544 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
547 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
548 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
549 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
550 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000551
Paul Bakkerbd51b262014-07-10 15:26:12 +0200552exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000554}
Paul Bakker33b43f12013-08-20 11:48:36 +0200555/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000556
Paul Bakker33b43f12013-08-20 11:48:36 +0200557/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000559{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_mpi X;
561 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
564 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000565
Paul Bakkerbd51b262014-07-10 15:26:12 +0200566exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000568}
Paul Bakker33b43f12013-08-20 11:48:36 +0200569/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000570
Paul Bakker33b43f12013-08-20 11:48:36 +0200571/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100572void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
573 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000574{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_mpi X, Y;
576 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
579 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
580 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000581
Paul Bakkerbd51b262014-07-10 15:26:12 +0200582exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000584}
Paul Bakker33b43f12013-08-20 11:48:36 +0200585/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000586
Paul Bakker33b43f12013-08-20 11:48:36 +0200587/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100588void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
589 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000590{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_mpi X, Y;
592 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
595 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
596 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000597
Paul Bakkerbd51b262014-07-10 15:26:12 +0200598exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000600}
Paul Bakker33b43f12013-08-20 11:48:36 +0200601/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000602
Paul Bakker33b43f12013-08-20 11:48:36 +0200603/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604void mbedtls_mpi_copy( int input_X, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_mpi X, Y, A;
607 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
610 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_A ) == 0 );
611 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 );
612 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
613 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
614 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
615 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
616 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000617
Paul Bakkerbd51b262014-07-10 15:26:12 +0200618exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000620}
Paul Bakker33b43f12013-08-20 11:48:36 +0200621/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000622
Paul Bakker33b43f12013-08-20 11:48:36 +0200623/* BEGIN_CASE */
624void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_mpi X;
627 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
630 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
631 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000632
Paul Bakkerbd51b262014-07-10 15:26:12 +0200633exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000635}
Paul Bakker33b43f12013-08-20 11:48:36 +0200636/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000637
Paul Bakker33b43f12013-08-20 11:48:36 +0200638/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_mpi X;
642 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100645 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
647 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100648 TEST_ASSERT( X.n == (size_t) after );
649
Paul Bakkerbd51b262014-07-10 15:26:12 +0200650exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100652}
653/* END_CASE */
654
655/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100656void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
657 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100658{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_mpi X, Y, XX;
660 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100663 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100665 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
669 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
672 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100673
Paul Bakkerbd51b262014-07-10 15:26:12 +0200674exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100676}
677/* END_CASE */
678
679/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100680void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
681 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100682{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
686 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100689 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100691 Y.s = y_sign;
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
694 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
697 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
698 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
701 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
702 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100703
Paul Bakkerbd51b262014-07-10 15:26:12 +0200704exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
706 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100707}
708/* END_CASE */
709
710/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100711void mbedtls_mpi_swap( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000712{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_mpi X, Y, A;
714 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
717 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
718 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 );
719 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
720 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
721 mbedtls_mpi_swap( &X, &Y );
722 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
723 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000724
Paul Bakkerbd51b262014-07-10 15:26:12 +0200725exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000727}
Paul Bakker33b43f12013-08-20 11:48:36 +0200728/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000729
Paul Bakker33b43f12013-08-20 11:48:36 +0200730/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100731void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
732 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000733{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_mpi X, Y, Z, A;
735 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
738 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
739 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
740 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
741 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000742
Paul Bakkerbd51b262014-07-10 15:26:12 +0200743exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000745}
Paul Bakker33b43f12013-08-20 11:48:36 +0200746/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000747
Paul Bakker33b43f12013-08-20 11:48:36 +0200748/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100749void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
750 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100751{
752 mbedtls_mpi X, A;
753 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
754
Janos Follath044a86b2015-10-25 10:58:03 +0100755 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100756
757 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
758 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
759 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
760
761 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
762 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
763 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
764
765 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100766 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
767 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
768
769exit:
770 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
771}
772/* END_CASE */
773
774
775/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100776void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
777 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000778{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_mpi X, Y, Z, A;
780 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
783 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
784 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
785 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
786 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000787
Paul Bakkerbd51b262014-07-10 15:26:12 +0200788exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000790}
Paul Bakker33b43f12013-08-20 11:48:36 +0200791/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000792
Paul Bakker33b43f12013-08-20 11:48:36 +0200793/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100794void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
795 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000796{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_mpi X, Y, A;
798 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
801 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
802 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
803 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
804 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000805
Paul Bakkerbd51b262014-07-10 15:26:12 +0200806exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000808}
Paul Bakker33b43f12013-08-20 11:48:36 +0200809/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000810
Paul Bakker33b43f12013-08-20 11:48:36 +0200811/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100812void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
813 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000814{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_mpi X, Y, A;
816 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
819 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
820 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
821 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
822 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000823
Paul Bakkerbd51b262014-07-10 15:26:12 +0200824exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000826}
Paul Bakker33b43f12013-08-20 11:48:36 +0200827/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000828
Paul Bakker33b43f12013-08-20 11:48:36 +0200829/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100830void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
831 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_mpi X, Z, A;
834 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
837 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
838 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
839 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000840
Paul Bakkerbd51b262014-07-10 15:26:12 +0200841exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000843}
Paul Bakker33b43f12013-08-20 11:48:36 +0200844/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000845
Paul Bakker33b43f12013-08-20 11:48:36 +0200846/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100847void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
848 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000849{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_mpi X, Y, Z, A;
851 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
854 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
855 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
856 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
857 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000858
Paul Bakkerbd51b262014-07-10 15:26:12 +0200859exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000861}
Paul Bakker33b43f12013-08-20 11:48:36 +0200862/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000863
Paul Bakker33b43f12013-08-20 11:48:36 +0200864/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100865void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
866 char * input_Y, int radix_A, char * input_A,
867 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000868{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000870 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
874 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
875 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200878 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000879 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000881
Paul Bakkerbd51b262014-07-10 15:26:12 +0200882exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000884}
Paul Bakker33b43f12013-08-20 11:48:36 +0200885/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000886
Paul Bakker33b43f12013-08-20 11:48:36 +0200887/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100888void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
889 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_mpi X, Z, A;
892 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
895 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
896 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
897 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000898
Paul Bakkerbd51b262014-07-10 15:26:12 +0200899exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000901}
Paul Bakker33b43f12013-08-20 11:48:36 +0200902/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000903
Paul Bakker33b43f12013-08-20 11:48:36 +0200904/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100905void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
906 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_mpi X, Y, Z, A;
909 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
912 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
913 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
914 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
915 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000916
Paul Bakkerbd51b262014-07-10 15:26:12 +0200917exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000919}
Paul Bakker33b43f12013-08-20 11:48:36 +0200920/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000921
Paul Bakker33b43f12013-08-20 11:48:36 +0200922/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100923void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
924 int radix_A, char * input_A,
925 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000926{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 mbedtls_mpi X, Z, A;
928 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
931 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
932 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200933 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200935 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200937 else
938 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000939
Paul Bakkerbd51b262014-07-10 15:26:12 +0200940exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000942}
Paul Bakker33b43f12013-08-20 11:48:36 +0200943/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000944
Paul Bakker33b43f12013-08-20 11:48:36 +0200945/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100946void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
947 char * input_Y, int radix_A, char * input_A,
948 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000949{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000951 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
953 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
956 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
957 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
958 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
959 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200960 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000961 if( res == 0 )
962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
964 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000965 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000966
Paul Bakkerbd51b262014-07-10 15:26:12 +0200967exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
969 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000970}
Paul Bakker33b43f12013-08-20 11:48:36 +0200971/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000972
Paul Bakker33b43f12013-08-20 11:48:36 +0200973/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100974void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
975 int radix_A, char * input_A, int radix_B,
976 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000977{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000979 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
981 mbedtls_mpi_init( &B );
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_read_string( &B, radix_B, input_B ) == 0 );
986 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200987 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000988 if( res == 0 )
989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
991 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000992 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000993
Paul Bakkerbd51b262014-07-10 15:26:12 +0200994exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
996 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000997}
Paul Bakker33b43f12013-08-20 11:48:36 +0200998/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000999
Paul Bakker33b43f12013-08-20 11:48:36 +02001000/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001001void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1002 char * input_Y, int radix_A, char * input_A,
1003 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001004{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001006 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1010 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1011 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1012 res = mbedtls_mpi_mod_mpi( &X, &X, &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( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
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 ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
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_mod_int( int radix_X, char * input_X, int input_Y,
1026 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001027{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001029 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_mpi_uint r;
1031 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1034 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001035 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001036 if( res == 0 )
1037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001039 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001040
Paul Bakkerbd51b262014-07-10 15:26:12 +02001041exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001043}
Paul Bakker33b43f12013-08-20 11:48:36 +02001044/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001045
Paul Bakker33b43f12013-08-20 11:48:36 +02001046/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001047void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1048 char * input_E, int radix_N, char * input_N,
1049 int radix_RR, char * input_RR, int radix_X,
1050 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001053 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1055 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1058 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1059 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1060 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001061
Paul Bakker33b43f12013-08-20 11:48:36 +02001062 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001066 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001067 if( res == 0 )
1068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001070 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001071
Paul Bakkerbd51b262014-07-10 15:26:12 +02001072exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1074 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001075}
Paul Bakker33b43f12013-08-20 11:48:36 +02001076/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001077
Paul Bakker33b43f12013-08-20 11:48:36 +02001078/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001079void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1080 char * input_Y, int radix_A, char * input_A,
1081 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001082{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001084 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
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 );
1088 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1089 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1090 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001091 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001092 if( res == 0 )
1093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001095 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001096
Paul Bakkerbd51b262014-07-10 15:26:12 +02001097exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001099}
Paul Bakker33b43f12013-08-20 11:48:36 +02001100/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001103void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001106 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001110 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001111 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001112
Paul Bakkerbd51b262014-07-10 15:26:12 +02001113exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001115}
Paul Bakker33b43f12013-08-20 11:48:36 +02001116/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001119void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001120 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001121{
1122 mbedtls_mpi X;
1123 int res;
1124 mbedtls_test_mpi_random rand;
1125
1126 mbedtls_mpi_init( &X );
1127 rand.data = witnesses;
1128 rand.pos = 0;
1129 rand.chunk_len = chunk_len;
1130
1131 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001132 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1133 mbedtls_test_mpi_miller_rabin_determinizer,
1134 &rand );
1135 TEST_ASSERT( res == 0 );
1136
1137 rand.data = witnesses;
1138 rand.pos = 0;
1139 rand.chunk_len = chunk_len;
1140
Janos Follatha0b67c22018-09-18 14:48:23 +01001141 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1142 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001143 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001144 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001145
1146exit:
1147 mbedtls_mpi_free( &X );
1148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001152void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001155 int my_ret;
1156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001158
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001159 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001160 TEST_ASSERT( my_ret == ref_ret );
1161
1162 if( ref_ret == 0 )
1163 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001164 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001165
1166 TEST_ASSERT( actual_bits >= (size_t) bits );
1167 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1168
Janos Follatha0b67c22018-09-18 14:48:23 +01001169 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1170 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001171 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001172 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001173 /* X = ( X - 1 ) / 2 */
1174 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001175 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1176 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001177 }
1178 }
1179
Paul Bakkerbd51b262014-07-10 15:26:12 +02001180exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001182}
1183/* END_CASE */
1184
Paul Bakker33b43f12013-08-20 11:48:36 +02001185/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001186void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1187 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_mpi X, A;
1190 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1193 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1194 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1195 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001196
Paul Bakkerbd51b262014-07-10 15:26:12 +02001197exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001199}
Paul Bakker33b43f12013-08-20 11:48:36 +02001200/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001201
Paul Bakker33b43f12013-08-20 11:48:36 +02001202/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001203void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1204 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_mpi X, A;
1207 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1210 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1211 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1212 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001213
Paul Bakkerbd51b262014-07-10 15:26:12 +02001214exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001216}
Paul Bakker33b43f12013-08-20 11:48:36 +02001217/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001220void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001221{
Andres AG93012e82016-09-09 09:10:28 +01001222 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001223}
Paul Bakker33b43f12013-08-20 11:48:36 +02001224/* END_CASE */