blob: a6d9effc2541b3473c878ed16562d18d88b94ac1 [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"
Gilles Peskine2f780622020-11-25 15:37:20 +01003#include "mbedtls/entropy.h"
Janos Follath64eca052018-09-05 17:04:49 +01004
5typedef struct mbedtls_test_mpi_random
6{
7 data_t *data;
8 size_t pos;
9 size_t chunk_len;
10} mbedtls_test_mpi_random;
11
12/*
13 * This function is called by the Miller-Rabin primality test each time it
14 * chooses a random witness. The witnesses (or non-witnesses as provided by the
15 * test) are stored in the data member of the state structure. Each number is in
16 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
17 */
18int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
19 unsigned char* buf,
20 size_t len )
21{
22 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
23
24 if( random == NULL || random->data->x == NULL || buf == NULL )
25 return( -1 );
26
27 if( random->pos + random->chunk_len > random->data->len
28 || random->chunk_len > len )
29 {
30 return( -1 );
31 }
32
33 memset( buf, 0, len );
34
35 /* The witness is written to the end of the buffer, since the buffer is
36 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
37 * Writing the witness to the start of the buffer would result in the
38 * buffer being 'witness 000...000', which would be treated as
39 * witness * 2^n for some n. */
40 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
41 random->chunk_len );
42
43 random->pos += random->chunk_len;
44
45 return( 0 );
46}
Gilles Peskine2f780622020-11-25 15:37:20 +010047
48/* Random generator that is told how many bytes to return. */
49static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
50{
51 size_t *bytes_left = state;
52 size_t i;
53 for( i = 0; i < len; i++ )
54 {
55 if( *bytes_left == 0 )
56 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
57 buf[i] = *bytes_left & 0xff;
58 --( *bytes_left );
59 }
60 return( 0 );
61}
62
Paul Bakker33b43f12013-08-20 11:48:36 +020063/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000064
Paul Bakker33b43f12013-08-20 11:48:36 +020065/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020067 * END_DEPENDENCIES
68 */
Paul Bakker5690efc2011-05-26 13:16:06 +000069
Hanno Beckerb48e1aa2018-12-18 23:25:01 +000070/* BEGIN_CASE */
71void mpi_valid_param( )
72{
73 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
74}
75/* END_CASE */
76
Hanno Beckerafb607b2018-12-11 14:27:08 +000077/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
78void mpi_invalid_param( )
79{
80 mbedtls_mpi X;
81 const char *s_in = "00101000101010";
82 char s_out[16] = { 0 };
83 unsigned char u_out[16] = { 0 };
84 unsigned char u_in[16] = { 0 };
85 size_t olen;
86 mbedtls_mpi_uint mpi_uint;
87
88 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000089
Hanno Beckerafb607b2018-12-11 14:27:08 +000090 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
91 mbedtls_mpi_grow( NULL, 42 ) );
92 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
93 mbedtls_mpi_copy( NULL, &X ) );
94 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
95 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000096
Hanno Beckerafb607b2018-12-11 14:27:08 +000097 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
98 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000099
Hanno Beckerafb607b2018-12-11 14:27:08 +0000100 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
101 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
102 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
103 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000104
Hanno Beckerafb607b2018-12-11 14:27:08 +0000105 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
106 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
107 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
108 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000109
Hanno Beckerafb607b2018-12-11 14:27:08 +0000110 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
111 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000112
Hanno Beckerafb607b2018-12-11 14:27:08 +0000113 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
114 mbedtls_mpi_get_bit( NULL, 42 ) );
115 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
116 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000117
Hanno Beckerafb607b2018-12-11 14:27:08 +0000118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
119 mbedtls_mpi_read_string( NULL, 2, s_in ) );
120 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
121 mbedtls_mpi_read_string( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000122
Hanno Beckerafb607b2018-12-11 14:27:08 +0000123 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
124 mbedtls_mpi_write_string( NULL, 2,
125 s_out, sizeof( s_out ),
126 &olen ) );
127 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
128 mbedtls_mpi_write_string( &X, 2,
129 NULL, sizeof( s_out ),
130 &olen ) );
131 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
132 mbedtls_mpi_write_string( &X, 2,
133 s_out, sizeof( s_out ),
134 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000135
Hanno Beckerafb607b2018-12-11 14:27:08 +0000136#if defined(MBEDTLS_FS_IO)
137 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
138 mbedtls_mpi_read_file( NULL, 2, stdin ) );
139 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
140 mbedtls_mpi_read_file( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000141
Hanno Beckerafb607b2018-12-11 14:27:08 +0000142 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
143 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
144#endif /* MBEDTLS_FS_IO */
145
146 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
147 mbedtls_mpi_read_binary( NULL, u_in,
148 sizeof( u_in ) ) );
149 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
150 mbedtls_mpi_read_binary( &X, NULL,
151 sizeof( u_in ) ) );
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_write_binary( NULL, u_out,
155 sizeof( u_out ) ) );
156 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
157 mbedtls_mpi_write_binary( &X, NULL,
158 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000159
Hanno Beckerafb607b2018-12-11 14:27:08 +0000160 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
161 mbedtls_mpi_shift_l( NULL, 42 ) );
162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
163 mbedtls_mpi_shift_r( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000164
Hanno Beckerafb607b2018-12-11 14:27:08 +0000165 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
166 mbedtls_mpi_cmp_abs( NULL, &X ) );
167 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
168 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000169
Hanno Beckerafb607b2018-12-11 14:27:08 +0000170 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
171 mbedtls_mpi_cmp_mpi( NULL, &X ) );
172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
173 mbedtls_mpi_cmp_mpi( &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_cmp_int( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000177
Hanno Beckerafb607b2018-12-11 14:27:08 +0000178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
179 mbedtls_mpi_add_abs( NULL, &X, &X ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
181 mbedtls_mpi_add_abs( &X, NULL, &X ) );
182 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
183 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000184
Hanno Beckerafb607b2018-12-11 14:27:08 +0000185 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
186 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
187 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
188 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
189 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
190 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000191
Hanno Beckerafb607b2018-12-11 14:27:08 +0000192 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
193 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
194 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
195 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
197 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
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_sub_mpi( NULL, &X, &X ) );
201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
202 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
203 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
204 mbedtls_mpi_sub_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_add_int( NULL, &X, 42 ) );
208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
209 mbedtls_mpi_add_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_sub_int( NULL, &X, 42 ) );
213 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
214 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
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_mul_mpi( NULL, &X, &X ) );
218 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
219 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
221 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000222
Hanno Beckerafb607b2018-12-11 14:27:08 +0000223 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
224 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
225 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
226 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
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_div_mpi( &X, &X, NULL, &X ) );
230 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
231 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
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_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000235
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000236 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
237
Hanno Beckerafb607b2018-12-11 14:27:08 +0000238 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
239 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
240 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
241 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
243 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000244
Hanno Beckerafb607b2018-12-11 14:27:08 +0000245 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
246 mbedtls_mpi_mod_int( NULL, &X, 42 ) );
247 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
248 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000249
Hanno Beckerafb607b2018-12-11 14:27:08 +0000250 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
251 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
252 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
253 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
255 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000258
Hanno Beckerafb607b2018-12-11 14:27:08 +0000259 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
260 mbedtls_mpi_fill_random( NULL, 42, rnd_std_rand,
261 NULL ) );
262 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
263 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000264
Hanno Beckerafb607b2018-12-11 14:27:08 +0000265 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
266 mbedtls_mpi_gcd( NULL, &X, &X ) );
267 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
268 mbedtls_mpi_gcd( &X, NULL, &X ) );
269 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
270 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000271
Hanno Beckerafb607b2018-12-11 14:27:08 +0000272 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
273 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
274 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
275 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
276 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000277 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000278
279exit:
280 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000281}
282/* END_CASE */
283
Paul Bakker33b43f12013-08-20 11:48:36 +0200284/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100285void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200286{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200287 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200288
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200289 mbedtls_mpi_init( &X );
290 mbedtls_mpi_init( &Y );
291 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200292
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200293 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
294 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200295 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200296 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200297
298exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200299 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200300}
301/* END_CASE */
302
303/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100304void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
305 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200306 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000307{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000309 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100310 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000313
Janos Follath276284f2019-03-06 12:29:37 +0000314 memset( str, '!', sizeof( str ) );
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200317 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000318 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100319 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200320 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000321 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200322 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath276284f2019-03-06 12:29:37 +0000323 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000324 }
325 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000326
Paul Bakkerbd51b262014-07-10 15:26:12 +0200327exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000329}
Paul Bakker33b43f12013-08-20 11:48:36 +0200330/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000331
Paul Bakker33b43f12013-08-20 11:48:36 +0200332/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100333void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000336 unsigned char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100337 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000340
Paul Bakkere896fea2009-07-06 06:40:23 +0000341
Azim Khand30ca132017-06-09 04:32:58 +0100342 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100343 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200344 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000345
Paul Bakkerbd51b262014-07-10 15:26:12 +0200346exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000348}
Paul Bakker33b43f12013-08-20 11:48:36 +0200349/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000350
Paul Bakker33b43f12013-08-20 11:48:36 +0200351/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100352void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100353 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100354 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000355{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000357 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000358 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000359
360 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200367 if( buflen > (size_t) output_size )
368 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200371 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000372 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000373
Azim Khand30ca132017-06-09 04:32:58 +0100374 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000375 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000376
Paul Bakkerbd51b262014-07-10 15:26:12 +0200377exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000379}
Paul Bakker33b43f12013-08-20 11:48:36 +0200380/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100383void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100384 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000387 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000388 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000389 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000390 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000391
392 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000395
Paul Bakker33b43f12013-08-20 11:48:36 +0200396 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200397 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000399 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000400 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000401
Paul Bakker33b43f12013-08-20 11:48:36 +0200402 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 buflen = mbedtls_mpi_size( &X );
405 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000406
Paul Bakkere896fea2009-07-06 06:40:23 +0000407
Azim Khand30ca132017-06-09 04:32:58 +0100408 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000409 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000410
Paul Bakkerbd51b262014-07-10 15:26:12 +0200411exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000413}
Paul Bakker33b43f12013-08-20 11:48:36 +0200414/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100417void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
418 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000419{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000421 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200422 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000427
Paul Bakker33b43f12013-08-20 11:48:36 +0200428 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000429 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200430 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000431 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200432 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000433
Paul Bakker33b43f12013-08-20 11:48:36 +0200434 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000435 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200436 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000437 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200438 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000441
Paul Bakkerbd51b262014-07-10 15:26:12 +0200442exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000444}
Paul Bakker33b43f12013-08-20 11:48:36 +0200445/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000446
Paul Bakker33b43f12013-08-20 11:48:36 +0200447/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100448void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_mpi X;
451 mbedtls_mpi_init( &X );
452 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
453 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000454
Paul Bakkerbd51b262014-07-10 15:26:12 +0200455exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000457}
Paul Bakker33b43f12013-08-20 11:48:36 +0200458/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000459
Paul Bakker33b43f12013-08-20 11:48:36 +0200460/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100461void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
462 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_mpi X, Y;
465 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
468 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100469 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
470
471 if( result == 0 )
472 {
473 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
474 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000475
Paul Bakkerbd51b262014-07-10 15:26:12 +0200476exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000478}
Paul Bakker33b43f12013-08-20 11:48:36 +0200479/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000480
Paul Bakker33b43f12013-08-20 11:48:36 +0200481/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100482void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_mpi X;
485 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
488 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000489
Paul Bakkerbd51b262014-07-10 15:26:12 +0200490exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000492}
Paul Bakker33b43f12013-08-20 11:48:36 +0200493/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000494
Paul Bakker33b43f12013-08-20 11:48:36 +0200495/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100496void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000497{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_mpi X;
499 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200502 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000503
Paul Bakkerbd51b262014-07-10 15:26:12 +0200504exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000506}
Paul Bakker33b43f12013-08-20 11:48:36 +0200507/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000508
Paul Bakker33b43f12013-08-20 11:48:36 +0200509/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100510void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
511 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000512{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_mpi A, X, Y, Z;
514 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
517 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
518 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
519 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
520 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000521
Paul Bakkerbd51b262014-07-10 15:26:12 +0200522exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000524}
Paul Bakker33b43f12013-08-20 11:48:36 +0200525/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000526
Paul Bakker33b43f12013-08-20 11:48:36 +0200527/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000529{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_mpi X;
531 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
534 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000535
Paul Bakkerbd51b262014-07-10 15:26:12 +0200536exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000538}
Paul Bakker33b43f12013-08-20 11:48:36 +0200539/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000540
Paul Bakker33b43f12013-08-20 11:48:36 +0200541/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100542void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
543 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_mpi X, Y;
546 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
549 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
550 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
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( &X ); mbedtls_mpi_free( &Y );
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 */
Janos Follath27d221a2019-10-14 09:21:49 +0100558void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
559 int size_Y, char * input_Y,
Janos Follath867a3ab2019-10-11 14:21:53 +0100560 int input_ret, int input_err )
Janos Follathe9ae6302019-09-11 16:07:14 +0100561{
Janos Follath867a3ab2019-10-11 14:21:53 +0100562 unsigned ret;
563 unsigned input_uret = input_ret;
Janos Follathe9ae6302019-09-11 16:07:14 +0100564 mbedtls_mpi X, Y;
565 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
566
Janos Follath27d221a2019-10-14 09:21:49 +0100567 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
568 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100569
Gilles Peskine1a30fbb2020-01-21 16:30:53 +0100570 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
571 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100572
Janos Follath867a3ab2019-10-11 14:21:53 +0100573 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follathe9ae6302019-09-11 16:07:14 +0100574 if( input_err == 0 )
Janos Follath867a3ab2019-10-11 14:21:53 +0100575 TEST_ASSERT( ret == input_uret );
Janos Follathe9ae6302019-09-11 16:07:14 +0100576
577exit:
578 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
579}
580/* END_CASE */
581
582/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100583void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
584 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_mpi X, Y;
587 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
590 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
591 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000592
Paul Bakkerbd51b262014-07-10 15:26:12 +0200593exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000595}
Paul Bakker33b43f12013-08-20 11:48:36 +0200596/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000597
Paul Bakker33b43f12013-08-20 11:48:36 +0200598/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100599void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000600{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100601 mbedtls_mpi X, Y;
602 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100605 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100608 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
609 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000610
Paul Bakkerbd51b262014-07-10 15:26:12 +0200611exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100612 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
613}
614/* END_CASE */
615
616/* BEGIN_CASE */
617void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
618{
619 mbedtls_mpi X, Y, X0;
620 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
621
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100622 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
623 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
624 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100625 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
626
627 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
628 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
629 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
630
631exit:
632 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000633}
Paul Bakker33b43f12013-08-20 11:48:36 +0200634/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000635
Paul Bakker33b43f12013-08-20 11:48:36 +0200636/* BEGIN_CASE */
637void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_mpi X;
640 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
643 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
644 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000645
Paul Bakkerbd51b262014-07-10 15:26:12 +0200646exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000648}
Paul Bakker33b43f12013-08-20 11:48:36 +0200649/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000650
Paul Bakker33b43f12013-08-20 11:48:36 +0200651/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_mpi X;
655 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100658 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
660 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100661 TEST_ASSERT( X.n == (size_t) after );
662
Paul Bakkerbd51b262014-07-10 15:26:12 +0200663exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100665}
666/* END_CASE */
667
668/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100669void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
670 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100671{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_mpi X, Y, XX;
673 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100676 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100678 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
682 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
685 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100686
Paul Bakkerbd51b262014-07-10 15:26:12 +0200687exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100689}
690/* END_CASE */
691
692/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100693void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
694 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100695{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
699 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100702 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100704 Y.s = y_sign;
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
707 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
710 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
711 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
714 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
715 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100716
Paul Bakkerbd51b262014-07-10 15:26:12 +0200717exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
719 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100720}
721/* END_CASE */
722
723/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100724void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000725{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100726 mbedtls_mpi X, Y;
727 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
730 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100731 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
732 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100735 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
736 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000737
Paul Bakkerbd51b262014-07-10 15:26:12 +0200738exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100739 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
740}
741/* END_CASE */
742
743/* BEGIN_CASE */
744void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
745{
746 mbedtls_mpi X, Y, X0, Y0;
747 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
748 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
749
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100750 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
751 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
752 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
753 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100754
755 mbedtls_mpi_swap( &X, &Y );
756 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
757 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
758
759exit:
760 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
761 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
762}
763/* END_CASE */
764
765/* BEGIN_CASE */
766void mpi_swap_self( data_t *input_X )
767{
768 mbedtls_mpi X, X0;
769 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
770
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100771 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
772 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100773
774 mbedtls_mpi_swap( &X, &X );
775 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
776
777exit:
778 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000779}
Paul Bakker33b43f12013-08-20 11:48:36 +0200780/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000781
Paul Bakker33b43f12013-08-20 11:48:36 +0200782/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100783void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
784 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000785{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_mpi X, Y, Z, A;
787 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
790 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
791 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
792 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
793 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000794
Paul Bakkerbd51b262014-07-10 15:26:12 +0200795exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000797}
Paul Bakker33b43f12013-08-20 11:48:36 +0200798/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000799
Paul Bakker33b43f12013-08-20 11:48:36 +0200800/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100801void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
802 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100803{
804 mbedtls_mpi X, A;
805 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
806
Janos Follath044a86b2015-10-25 10:58:03 +0100807 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100808
809 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
810 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
811 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
812
813 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
814 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
815 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
816
817 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100818 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
819 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
820
821exit:
822 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
823}
824/* END_CASE */
825
826
827/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100828void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
829 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000830{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_mpi X, Y, Z, A;
832 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
835 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
836 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
837 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
838 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000839
Paul Bakkerbd51b262014-07-10 15:26:12 +0200840exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000842}
Paul Bakker33b43f12013-08-20 11:48:36 +0200843/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000844
Paul Bakker33b43f12013-08-20 11:48:36 +0200845/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100846void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
847 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_mpi X, Y, A;
850 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
853 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
854 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
855 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
856 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000857
Paul Bakkerbd51b262014-07-10 15:26:12 +0200858exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000860}
Paul Bakker33b43f12013-08-20 11:48:36 +0200861/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000862
Paul Bakker33b43f12013-08-20 11:48:36 +0200863/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100864void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
865 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000866{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_mpi X, Y, A;
868 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
871 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
872 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
873 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
874 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000875
Paul Bakkerbd51b262014-07-10 15:26:12 +0200876exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000878}
Paul Bakker33b43f12013-08-20 11:48:36 +0200879/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000880
Paul Bakker33b43f12013-08-20 11:48:36 +0200881/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100882void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
883 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000884{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_mpi X, Z, A;
886 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
889 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
890 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
891 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000892
Paul Bakkerbd51b262014-07-10 15:26:12 +0200893exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000895}
Paul Bakker33b43f12013-08-20 11:48:36 +0200896/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000897
Paul Bakker33b43f12013-08-20 11:48:36 +0200898/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100899void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
900 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000901{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_mpi X, Y, Z, A;
903 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
906 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
907 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
908 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
909 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000910
Paul Bakkerbd51b262014-07-10 15:26:12 +0200911exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000913}
Paul Bakker33b43f12013-08-20 11:48:36 +0200914/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000915
Paul Bakker33b43f12013-08-20 11:48:36 +0200916/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100917void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
918 char * input_Y, int radix_A, char * input_A,
919 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000922 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
926 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
927 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200930 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000931 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000933
Paul Bakkerbd51b262014-07-10 15:26:12 +0200934exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000936}
Paul Bakker33b43f12013-08-20 11:48:36 +0200937/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000938
Paul Bakker33b43f12013-08-20 11:48:36 +0200939/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100940void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
941 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_mpi X, Z, A;
944 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
947 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
948 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
949 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000950
Paul Bakkerbd51b262014-07-10 15:26:12 +0200951exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000953}
Paul Bakker33b43f12013-08-20 11:48:36 +0200954/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000955
Paul Bakker33b43f12013-08-20 11:48:36 +0200956/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100957void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
958 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000959{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_mpi X, Y, Z, A;
961 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
964 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
965 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
966 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
967 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000968
Paul Bakkerbd51b262014-07-10 15:26:12 +0200969exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000971}
Paul Bakker33b43f12013-08-20 11:48:36 +0200972/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000973
Paul Bakker33b43f12013-08-20 11:48:36 +0200974/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100975void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
976 int radix_A, char * input_A,
977 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000978{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_mpi X, Z, A;
980 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
983 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
984 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200985 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200987 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200989 else
990 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000991
Paul Bakkerbd51b262014-07-10 15:26:12 +0200992exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000994}
Paul Bakker33b43f12013-08-20 11:48:36 +0200995/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000996
Paul Bakker33b43f12013-08-20 11:48:36 +0200997/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100998void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
999 char * input_Y, int radix_A, char * input_A,
1000 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001001{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001003 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1005 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1008 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1009 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1010 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1011 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001012 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001013 if( res == 0 )
1014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1016 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 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( &Q ); mbedtls_mpi_free( &R );
1021 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001022}
Paul Bakker33b43f12013-08-20 11:48:36 +02001023/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001024
Paul Bakker33b43f12013-08-20 11:48:36 +02001025/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001026void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1027 int radix_A, char * input_A, int radix_B,
1028 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001031 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1033 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1036 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1037 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1038 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001039 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001040 if( res == 0 )
1041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1043 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001044 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001045
Paul Bakkerbd51b262014-07-10 15:26:12 +02001046exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1048 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001049}
Paul Bakker33b43f12013-08-20 11:48:36 +02001050/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001051
Paul Bakker33b43f12013-08-20 11:48:36 +02001052/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001053void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1054 char * input_Y, int radix_A, char * input_A,
1055 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001058 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1062 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1063 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1064 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001065 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001066 if( res == 0 )
1067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001069 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001070
Paul Bakkerbd51b262014-07-10 15:26:12 +02001071exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001073}
Paul Bakker33b43f12013-08-20 11:48:36 +02001074/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001075
Paul Bakker33b43f12013-08-20 11:48:36 +02001076/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001077void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1078 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001081 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_mpi_uint r;
1083 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1086 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001087 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001088 if( res == 0 )
1089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001091 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001092
Paul Bakkerbd51b262014-07-10 15:26:12 +02001093exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001095}
Paul Bakker33b43f12013-08-20 11:48:36 +02001096/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001097
Paul Bakker33b43f12013-08-20 11:48:36 +02001098/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001099void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1100 char * input_E, int radix_N, char * input_N,
1101 int radix_RR, char * input_RR, int radix_X,
1102 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001105 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1107 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); 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( &A, radix_A, input_A ) == 0 );
1110 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1111 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1112 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001113
Paul Bakker33b43f12013-08-20 11:48:36 +02001114 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001118 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001119 if( res == 0 )
1120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001122 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001123
Paul Bakkerbd51b262014-07-10 15:26:12 +02001124exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1126 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001127}
Paul Bakker33b43f12013-08-20 11:48:36 +02001128/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001129
Paul Bakker33b43f12013-08-20 11:48:36 +02001130/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001131void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1132 char * input_Y, int radix_A, char * input_A,
1133 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001136 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1140 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1141 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1142 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001143 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001144 if( res == 0 )
1145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001147 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001148
Paul Bakkerbd51b262014-07-10 15:26:12 +02001149exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001151}
Paul Bakker33b43f12013-08-20 11:48:36 +02001152/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001155void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001158 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001162 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001163 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001164
Paul Bakkerbd51b262014-07-10 15:26:12 +02001165exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001167}
Paul Bakker33b43f12013-08-20 11:48:36 +02001168/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001171void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001172 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001173{
1174 mbedtls_mpi X;
1175 int res;
1176 mbedtls_test_mpi_random rand;
1177
1178 mbedtls_mpi_init( &X );
1179 rand.data = witnesses;
1180 rand.pos = 0;
1181 rand.chunk_len = chunk_len;
1182
1183 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001184 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1185 mbedtls_test_mpi_miller_rabin_determinizer,
1186 &rand );
1187 TEST_ASSERT( res == 0 );
1188
1189 rand.data = witnesses;
1190 rand.pos = 0;
1191 rand.chunk_len = chunk_len;
1192
Janos Follatha0b67c22018-09-18 14:48:23 +01001193 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1194 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001195 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001196 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001197
1198exit:
1199 mbedtls_mpi_free( &X );
1200}
1201/* END_CASE */
1202
1203/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001204void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001207 int my_ret;
1208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001210
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001211 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001212 TEST_ASSERT( my_ret == ref_ret );
1213
1214 if( ref_ret == 0 )
1215 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001216 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001217
1218 TEST_ASSERT( actual_bits >= (size_t) bits );
1219 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1220
Janos Follatha0b67c22018-09-18 14:48:23 +01001221 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1222 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001223 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001224 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001225 /* X = ( X - 1 ) / 2 */
1226 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001227 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1228 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001229 }
1230 }
1231
Paul Bakkerbd51b262014-07-10 15:26:12 +02001232exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001234}
1235/* END_CASE */
1236
Paul Bakker33b43f12013-08-20 11:48:36 +02001237/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001238void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1239 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_mpi X, A;
1242 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1245 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1246 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1247 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001248
Paul Bakkerbd51b262014-07-10 15:26:12 +02001249exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001251}
Paul Bakker33b43f12013-08-20 11:48:36 +02001252/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001253
Paul Bakker33b43f12013-08-20 11:48:36 +02001254/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001255void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1256 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_mpi X, A;
1259 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1262 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1263 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1264 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001265
Paul Bakkerbd51b262014-07-10 15:26:12 +02001266exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001268}
Paul Bakker33b43f12013-08-20 11:48:36 +02001269/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001270
Gilles Peskine2f780622020-11-25 15:37:20 +01001271/* BEGIN_CASE */
1272void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1273{
1274 mbedtls_mpi X;
1275 int ret;
1276 size_t bytes_left = rng_bytes;
1277 mbedtls_mpi_init( &X );
1278
1279 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1280 f_rng_bytes_left, &bytes_left );
1281 TEST_ASSERT( ret == expected_ret );
1282
1283 if( expected_ret == 0 )
1284 {
1285 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1286 * as a big-endian representation of the number. We know when
1287 * our RNG function returns null bytes, so we know how many
1288 * leading zero bytes the number has. */
1289 size_t leading_zeros = 0;
1290 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1291 leading_zeros = 1;
1292 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1293 (size_t) wanted_bytes );
1294 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
1295 }
1296
1297exit:
1298 mbedtls_mpi_free( &X );
1299}
1300/* END_CASE */
1301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001303void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001304{
Andres AG93012e82016-09-09 09:10:28 +01001305 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001306}
Paul Bakker33b43f12013-08-20 11:48:36 +02001307/* END_CASE */