blob: c5bb5a678afedac43f223881138da0013dd46211 [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 Peskine3cb1e292020-11-25 15:37:20 +01003#include "mbedtls/entropy.h"
Janos Follath64eca052018-09-05 17:04:49 +01004
Chris Jonese64a46f2020-12-03 17:44:03 +00005#if MBEDTLS_MPI_MAX_BITS > 792
6#define MPI_MAX_BITS_LARGER_THAN_792
Chris Jones4592bd82020-12-03 14:24:33 +00007#endif
Janos Follath64eca052018-09-05 17:04:49 +01008
9typedef struct mbedtls_test_mpi_random
10{
11 data_t *data;
12 size_t pos;
13 size_t chunk_len;
14} mbedtls_test_mpi_random;
15
16/*
17 * This function is called by the Miller-Rabin primality test each time it
18 * chooses a random witness. The witnesses (or non-witnesses as provided by the
19 * test) are stored in the data member of the state structure. Each number is in
20 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
21 */
22int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
23 unsigned char* buf,
24 size_t len )
25{
26 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
27
28 if( random == NULL || random->data->x == NULL || buf == NULL )
29 return( -1 );
30
31 if( random->pos + random->chunk_len > random->data->len
32 || random->chunk_len > len )
33 {
34 return( -1 );
35 }
36
37 memset( buf, 0, len );
38
39 /* The witness is written to the end of the buffer, since the buffer is
40 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
41 * Writing the witness to the start of the buffer would result in the
42 * buffer being 'witness 000...000', which would be treated as
43 * witness * 2^n for some n. */
44 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
45 random->chunk_len );
46
47 random->pos += random->chunk_len;
48
49 return( 0 );
50}
Gilles Peskine3cb1e292020-11-25 15:37:20 +010051
52/* Random generator that is told how many bytes to return. */
53static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
54{
55 size_t *bytes_left = state;
56 size_t i;
57 for( i = 0; i < len; i++ )
58 {
59 if( *bytes_left == 0 )
60 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
61 buf[i] = *bytes_left & 0xff;
62 --( *bytes_left );
63 }
64 return( 0 );
65}
66
Paul Bakker33b43f12013-08-20 11:48:36 +020067/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000068
Paul Bakker33b43f12013-08-20 11:48:36 +020069/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020071 * END_DEPENDENCIES
72 */
Paul Bakker5690efc2011-05-26 13:16:06 +000073
Hanno Beckerb48e1aa2018-12-18 23:25:01 +000074/* BEGIN_CASE */
75void mpi_valid_param( )
76{
77 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
78}
79/* END_CASE */
80
Hanno Beckerafb607b2018-12-11 14:27:08 +000081/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
82void mpi_invalid_param( )
83{
84 mbedtls_mpi X;
85 const char *s_in = "00101000101010";
86 char s_out[16] = { 0 };
87 unsigned char u_out[16] = { 0 };
88 unsigned char u_in[16] = { 0 };
89 size_t olen;
90 mbedtls_mpi_uint mpi_uint;
91
92 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000093
Hanno Beckerafb607b2018-12-11 14:27:08 +000094 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
95 mbedtls_mpi_grow( NULL, 42 ) );
96 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
97 mbedtls_mpi_copy( NULL, &X ) );
98 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
99 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000100
Hanno Beckerafb607b2018-12-11 14:27:08 +0000101 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
102 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000103
Hanno Beckerafb607b2018-12-11 14:27:08 +0000104 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
105 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
106 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
107 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000108
Hanno Beckerafb607b2018-12-11 14:27:08 +0000109 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
110 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
111 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
112 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000113
Hanno Beckerafb607b2018-12-11 14:27:08 +0000114 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
115 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000116
Hanno Beckerafb607b2018-12-11 14:27:08 +0000117 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
118 mbedtls_mpi_get_bit( NULL, 42 ) );
119 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
120 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000121
Hanno Beckerafb607b2018-12-11 14:27:08 +0000122 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
123 mbedtls_mpi_read_string( NULL, 2, s_in ) );
124 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
125 mbedtls_mpi_read_string( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000126
Hanno Beckerafb607b2018-12-11 14:27:08 +0000127 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
128 mbedtls_mpi_write_string( NULL, 2,
129 s_out, sizeof( s_out ),
130 &olen ) );
131 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
132 mbedtls_mpi_write_string( &X, 2,
133 NULL, sizeof( s_out ),
134 &olen ) );
135 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
136 mbedtls_mpi_write_string( &X, 2,
137 s_out, sizeof( s_out ),
138 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000139
Hanno Beckerafb607b2018-12-11 14:27:08 +0000140#if defined(MBEDTLS_FS_IO)
141 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
142 mbedtls_mpi_read_file( NULL, 2, stdin ) );
143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
144 mbedtls_mpi_read_file( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000145
Hanno Beckerafb607b2018-12-11 14:27:08 +0000146 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
147 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
148#endif /* MBEDTLS_FS_IO */
149
150 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
151 mbedtls_mpi_read_binary( NULL, u_in,
152 sizeof( u_in ) ) );
153 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
154 mbedtls_mpi_read_binary( &X, NULL,
155 sizeof( u_in ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000156
Hanno Beckerafb607b2018-12-11 14:27:08 +0000157 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
158 mbedtls_mpi_write_binary( NULL, u_out,
159 sizeof( u_out ) ) );
160 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
161 mbedtls_mpi_write_binary( &X, NULL,
162 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000163
Hanno Beckerafb607b2018-12-11 14:27:08 +0000164 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
165 mbedtls_mpi_shift_l( NULL, 42 ) );
166 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
167 mbedtls_mpi_shift_r( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000168
Hanno Beckerafb607b2018-12-11 14:27:08 +0000169 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
170 mbedtls_mpi_cmp_abs( NULL, &X ) );
171 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
172 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000173
Hanno Beckerafb607b2018-12-11 14:27:08 +0000174 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
175 mbedtls_mpi_cmp_mpi( NULL, &X ) );
176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
177 mbedtls_mpi_cmp_mpi( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000178
Hanno Beckerafb607b2018-12-11 14:27:08 +0000179 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
180 mbedtls_mpi_cmp_int( NULL, 42 ) );
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_add_abs( NULL, &X, &X ) );
184 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
185 mbedtls_mpi_add_abs( &X, NULL, &X ) );
186 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
187 mbedtls_mpi_add_abs( &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_sub_abs( NULL, &X, &X ) );
191 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
192 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
193 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
194 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000195
Hanno Beckerafb607b2018-12-11 14:27:08 +0000196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
197 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
198 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
199 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
200 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
201 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000202
Hanno Beckerafb607b2018-12-11 14:27:08 +0000203 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
204 mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
206 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
207 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
208 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000209
Hanno Beckerafb607b2018-12-11 14:27:08 +0000210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
211 mbedtls_mpi_add_int( NULL, &X, 42 ) );
212 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
213 mbedtls_mpi_add_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000214
Hanno Beckerafb607b2018-12-11 14:27:08 +0000215 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
216 mbedtls_mpi_sub_int( NULL, &X, 42 ) );
217 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
218 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000219
Hanno Beckerafb607b2018-12-11 14:27:08 +0000220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
221 mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
222 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
223 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
224 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
225 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000226
Hanno Beckerafb607b2018-12-11 14:27:08 +0000227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
228 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
229 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
230 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000231
Hanno Beckerafb607b2018-12-11 14:27:08 +0000232 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
233 mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) );
234 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
235 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000236
Hanno Beckerafb607b2018-12-11 14:27:08 +0000237 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
238 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000239
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000240 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
241
Hanno Beckerafb607b2018-12-11 14:27:08 +0000242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
243 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
244 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
245 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
247 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000248
Hanno Beckerafb607b2018-12-11 14:27:08 +0000249 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
250 mbedtls_mpi_mod_int( NULL, &X, 42 ) );
251 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
252 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000253
Hanno Beckerafb607b2018-12-11 14:27:08 +0000254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
255 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
259 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
261 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000262
Hanno Beckerafb607b2018-12-11 14:27:08 +0000263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200264 mbedtls_mpi_fill_random( NULL, 42,
265 mbedtls_test_rnd_std_rand,
Hanno Beckerafb607b2018-12-11 14:27:08 +0000266 NULL ) );
267 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
268 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000269
Hanno Beckerafb607b2018-12-11 14:27:08 +0000270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
271 mbedtls_mpi_gcd( NULL, &X, &X ) );
272 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
273 mbedtls_mpi_gcd( &X, NULL, &X ) );
274 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
275 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000276
Hanno Beckerafb607b2018-12-11 14:27:08 +0000277 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
278 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
279 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
280 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
281 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000282 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000283
284exit:
285 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000286}
287/* END_CASE */
288
Paul Bakker33b43f12013-08-20 11:48:36 +0200289/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100290void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200291{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200292 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200293
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200294 mbedtls_mpi_init( &X );
295 mbedtls_mpi_init( &Y );
296 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200297
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200298 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
299 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200300 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200301 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200302
303exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200304 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200305}
306/* END_CASE */
307
308/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100309void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
310 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200311 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000314 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100315 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000318
Janos Follath04dadb72019-03-06 12:29:37 +0000319 memset( str, '!', sizeof( str ) );
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200322 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000323 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100324 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200325 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000326 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200327 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000328 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000329 }
330 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000331
Paul Bakkerbd51b262014-07-10 15:26:12 +0200332exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000334}
Paul Bakker33b43f12013-08-20 11:48:36 +0200335/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000336
Paul Bakker33b43f12013-08-20 11:48:36 +0200337/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100338void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000339{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000341 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100342 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000345
Paul Bakkere896fea2009-07-06 06:40:23 +0000346
Azim Khand30ca132017-06-09 04:32:58 +0100347 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000348 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200349 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000350
Paul Bakkerbd51b262014-07-10 15:26:12 +0200351exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000353}
Paul Bakker33b43f12013-08-20 11:48:36 +0200354/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000355
Paul Bakker33b43f12013-08-20 11:48:36 +0200356/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000357void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
358{
359 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000360 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000361 size_t len;
362
363 mbedtls_mpi_init( &X );
364
365
366 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000367 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000368 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
369
370exit:
371 mbedtls_mpi_free( &X );
372}
373/* END_CASE */
374
375/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100376void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100377 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100378 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000381 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000382 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000383
384 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200391 if( buflen > (size_t) output_size )
392 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200395 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000396 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000397
Ronald Cron2dbba992020-06-10 11:42:32 +0200398 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
399 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000400 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000401
Paul Bakkerbd51b262014-07-10 15:26:12 +0200402exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000404}
Paul Bakker33b43f12013-08-20 11:48:36 +0200405/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000406
Janos Follathe344d0f2019-02-19 16:17:40 +0000407/* BEGIN_CASE */
408void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
409 data_t * input_A, int output_size,
410 int result )
411{
412 mbedtls_mpi X;
413 unsigned char buf[1000];
414 size_t buflen;
415
416 memset( buf, 0x00, 1000 );
417
418 mbedtls_mpi_init( &X );
419
420 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
421
422 buflen = mbedtls_mpi_size( &X );
423 if( buflen > (size_t) output_size )
424 buflen = (size_t) output_size;
425
426 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
427 if( result == 0)
428 {
429
Ronald Cron2dbba992020-06-10 11:42:32 +0200430 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
431 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000432 }
433
434exit:
435 mbedtls_mpi_free( &X );
436}
437/* END_CASE */
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100440void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100441 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000444 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000445 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000446 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000447 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000448
449 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000452
Paul Bakker33b43f12013-08-20 11:48:36 +0200453 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200454 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000456 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000457 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000458
Paul Bakker33b43f12013-08-20 11:48:36 +0200459 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 buflen = mbedtls_mpi_size( &X );
462 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000463
Paul Bakkere896fea2009-07-06 06:40:23 +0000464
Ronald Cron2dbba992020-06-10 11:42:32 +0200465 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
466 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000467 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000468
Paul Bakkerbd51b262014-07-10 15:26:12 +0200469exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000471}
Paul Bakker33b43f12013-08-20 11:48:36 +0200472/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100475void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
476 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000477{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000479 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200480 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000485
Paul Bakker33b43f12013-08-20 11:48:36 +0200486 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000487 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200488 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000489 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200490 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000491
Paul Bakker33b43f12013-08-20 11:48:36 +0200492 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000493 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200494 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000495 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200496 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000499
Paul Bakkerbd51b262014-07-10 15:26:12 +0200500exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000502}
Paul Bakker33b43f12013-08-20 11:48:36 +0200503/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000504
Paul Bakker33b43f12013-08-20 11:48:36 +0200505/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100506void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_mpi X;
509 mbedtls_mpi_init( &X );
510 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
511 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000512
Paul Bakkerbd51b262014-07-10 15:26:12 +0200513exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000515}
Paul Bakker33b43f12013-08-20 11:48:36 +0200516/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000517
Paul Bakker33b43f12013-08-20 11:48:36 +0200518/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100519void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
520 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_mpi X, Y;
523 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
526 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100527 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
528
529 if( result == 0 )
530 {
531 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
532 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000533
Paul Bakkerbd51b262014-07-10 15:26:12 +0200534exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000536}
Paul Bakker33b43f12013-08-20 11:48:36 +0200537/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000538
Paul Bakker33b43f12013-08-20 11:48:36 +0200539/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100540void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000541{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_mpi X;
543 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
546 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000547
Paul Bakkerbd51b262014-07-10 15:26:12 +0200548exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000550}
Paul Bakker33b43f12013-08-20 11:48:36 +0200551/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000552
Paul Bakker33b43f12013-08-20 11:48:36 +0200553/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100554void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_mpi X;
557 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200560 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000561
Paul Bakkerbd51b262014-07-10 15:26:12 +0200562exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000564}
Paul Bakker33b43f12013-08-20 11:48:36 +0200565/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000566
Paul Bakker33b43f12013-08-20 11:48:36 +0200567/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100568void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
569 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000570{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_mpi A, X, Y, Z;
572 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
575 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
576 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
577 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
578 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000579
Paul Bakkerbd51b262014-07-10 15:26:12 +0200580exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000582}
Paul Bakker33b43f12013-08-20 11:48:36 +0200583/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000584
Paul Bakker33b43f12013-08-20 11:48:36 +0200585/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000587{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_mpi X;
589 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
592 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000593
Paul Bakkerbd51b262014-07-10 15:26:12 +0200594exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000596}
Paul Bakker33b43f12013-08-20 11:48:36 +0200597/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000598
Paul Bakker33b43f12013-08-20 11:48:36 +0200599/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100600void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
601 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_mpi X, Y;
604 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
607 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
608 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000609
Paul Bakkerbd51b262014-07-10 15:26:12 +0200610exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000612}
Paul Bakker33b43f12013-08-20 11:48:36 +0200613/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000614
Paul Bakker33b43f12013-08-20 11:48:36 +0200615/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100616void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
617 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100618 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100619{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200620 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100621 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100622 mbedtls_mpi X, Y;
623 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
624
Janos Follathb7e1b492019-10-14 09:21:49 +0100625 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
626 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100627
Gilles Peskine9018b112020-01-21 16:30:53 +0100628 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
629 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100630
Janos Follath0e5532d2019-10-11 14:21:53 +0100631 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100632 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100633 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100634
635exit:
636 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
637}
638/* END_CASE */
639
640/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100641void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
642 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_mpi X, Y;
645 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
648 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
649 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000650
Paul Bakkerbd51b262014-07-10 15:26:12 +0200651exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000653}
Paul Bakker33b43f12013-08-20 11:48:36 +0200654/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000655
Paul Bakker33b43f12013-08-20 11:48:36 +0200656/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100657void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000658{
Gilles Peskine7428b452020-01-20 21:01:51 +0100659 mbedtls_mpi X, Y;
660 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100663 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100666 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
667 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000668
Paul Bakkerbd51b262014-07-10 15:26:12 +0200669exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100670 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
671}
672/* END_CASE */
673
674/* BEGIN_CASE */
675void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
676{
677 mbedtls_mpi X, Y, X0;
678 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
679
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100680 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
681 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
682 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100683 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
684
685 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
686 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
687 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
688
689exit:
690 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000691}
Paul Bakker33b43f12013-08-20 11:48:36 +0200692/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000693
Paul Bakker33b43f12013-08-20 11:48:36 +0200694/* BEGIN_CASE */
695void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000696{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_mpi X;
698 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
701 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
702 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000703
Paul Bakkerbd51b262014-07-10 15:26:12 +0200704exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000706}
Paul Bakker33b43f12013-08-20 11:48:36 +0200707/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000708
Paul Bakker33b43f12013-08-20 11:48:36 +0200709/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100711{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_mpi X;
713 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100716 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
718 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100719 TEST_ASSERT( X.n == (size_t) after );
720
Paul Bakkerbd51b262014-07-10 15:26:12 +0200721exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100723}
724/* END_CASE */
725
726/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100727void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
728 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100729{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_mpi X, Y, XX;
731 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100734 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100736 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
740 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
743 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100744
Paul Bakkerbd51b262014-07-10 15:26:12 +0200745exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100747}
748/* END_CASE */
749
750/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100751void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
752 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
757 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100760 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100762 Y.s = y_sign;
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
765 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
768 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
769 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
772 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
773 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100774
Paul Bakkerbd51b262014-07-10 15:26:12 +0200775exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
777 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100778}
779/* END_CASE */
780
781/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100782void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000783{
Gilles Peskine7428b452020-01-20 21:01:51 +0100784 mbedtls_mpi X, Y;
785 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
788 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100789 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
790 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine7428b452020-01-20 21:01:51 +0100793 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
794 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000795
Paul Bakkerbd51b262014-07-10 15:26:12 +0200796exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100797 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
798}
799/* END_CASE */
800
801/* BEGIN_CASE */
802void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
803{
804 mbedtls_mpi X, Y, X0, Y0;
805 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
806 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
807
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100808 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
809 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
810 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
811 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100812
813 mbedtls_mpi_swap( &X, &Y );
814 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
815 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
816
817exit:
818 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
819 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
820}
821/* END_CASE */
822
823/* BEGIN_CASE */
824void mpi_swap_self( data_t *input_X )
825{
826 mbedtls_mpi X, X0;
827 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
828
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100829 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
830 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100831
832 mbedtls_mpi_swap( &X, &X );
833 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
834
835exit:
836 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000837}
Paul Bakker33b43f12013-08-20 11:48:36 +0200838/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000839
Paul Bakker33b43f12013-08-20 11:48:36 +0200840/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100841void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
842 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_mpi X, Y, Z, A;
845 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
848 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
849 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
850 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
851 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000852
Gilles Peskine56f943a2020-07-23 01:18:11 +0200853 /* result == first operand */
854 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
855 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
856 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
857
858 /* result == second operand */
859 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
860 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
861
Paul Bakkerbd51b262014-07-10 15:26:12 +0200862exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000864}
Paul Bakker33b43f12013-08-20 11:48:36 +0200865/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000866
Paul Bakker33b43f12013-08-20 11:48:36 +0200867/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100868void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
869 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100870{
871 mbedtls_mpi X, A;
872 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
873
Janos Follath044a86b2015-10-25 10:58:03 +0100874 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100875
876 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
877 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
878 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
879
880 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
881 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
882 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
883
884 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100885 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
886 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
887
888exit:
889 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
890}
891/* END_CASE */
892
893
894/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100895void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
896 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_mpi X, Y, Z, A;
899 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
902 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
903 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
904 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
905 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000906
Gilles Peskine56f943a2020-07-23 01:18:11 +0200907 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
909 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200911
912 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
914 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000915
Paul Bakkerbd51b262014-07-10 15:26:12 +0200916exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +0200917 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000918}
Paul Bakker33b43f12013-08-20 11:48:36 +0200919/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000920
Paul Bakker33b43f12013-08-20 11:48:36 +0200921/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100922void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
923 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_mpi X, Z, A;
926 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
929 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
930 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
931 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000932
Paul Bakkerbd51b262014-07-10 15:26:12 +0200933exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000935}
Paul Bakker33b43f12013-08-20 11:48:36 +0200936/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000937
Paul Bakker33b43f12013-08-20 11:48:36 +0200938/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100939void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
940 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000941{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_mpi X, Y, Z, A;
943 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
946 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
947 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
948 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
949 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000950
Gilles Peskine56f943a2020-07-23 01:18:11 +0200951 /* result == first operand */
952 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
953 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
954 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
955
956 /* result == second operand */
957 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
958 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
959
Paul Bakkerbd51b262014-07-10 15:26:12 +0200960exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000962}
Paul Bakker33b43f12013-08-20 11:48:36 +0200963/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000964
Paul Bakker33b43f12013-08-20 11:48:36 +0200965/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100966void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
967 char * input_Y, int radix_A, char * input_A,
968 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000969{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000971 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
975 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
976 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200979 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000980 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000982
Gilles Peskine56f943a2020-07-23 01:18:11 +0200983 /* result == first operand */
984 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
985 if( sub_result == 0 )
986 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
987 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
988
989 /* result == second operand */
990 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
991 if( sub_result == 0 )
992 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
993
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( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000996}
Paul Bakker33b43f12013-08-20 11:48:36 +0200997/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000998
Paul Bakker33b43f12013-08-20 11:48:36 +0200999/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001000void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
1001 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001002{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_mpi X, Z, A;
1004 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1007 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1008 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
1009 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001010
Paul Bakkerbd51b262014-07-10 15:26:12 +02001011exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001013}
Paul Bakker33b43f12013-08-20 11:48:36 +02001014/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001015
Paul Bakker33b43f12013-08-20 11:48:36 +02001016/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001017void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
1018 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 mbedtls_mpi X, Y, Z, A;
1021 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1024 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1025 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1026 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
1027 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001028
Paul Bakkerbd51b262014-07-10 15:26:12 +02001029exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001031}
Paul Bakker33b43f12013-08-20 11:48:36 +02001032/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001033
Paul Bakker33b43f12013-08-20 11:48:36 +02001034/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001035void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1036 int radix_A, char * input_A,
1037 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001038{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_mpi X, Z, A;
1040 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1043 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1044 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001045 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001047 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001049 else
1050 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001051
Paul Bakkerbd51b262014-07-10 15:26:12 +02001052exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001054}
Paul Bakker33b43f12013-08-20 11:48:36 +02001055/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001056
Paul Bakker33b43f12013-08-20 11:48:36 +02001057/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001058void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1059 char * input_Y, int radix_A, char * input_A,
1060 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001063 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1065 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1068 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1069 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1070 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1071 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001072 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001073 if( res == 0 )
1074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1076 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001077 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001078
Paul Bakkerbd51b262014-07-10 15:26:12 +02001079exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1081 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001082}
Paul Bakker33b43f12013-08-20 11:48:36 +02001083/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001084
Paul Bakker33b43f12013-08-20 11:48:36 +02001085/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001086void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1087 int radix_A, char * input_A, int radix_B,
1088 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001091 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1093 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1096 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1097 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1098 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001099 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001100 if( res == 0 )
1101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1103 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001104 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001105
Paul Bakkerbd51b262014-07-10 15:26:12 +02001106exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1108 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001109}
Paul Bakker33b43f12013-08-20 11:48:36 +02001110/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001111
Paul Bakker33b43f12013-08-20 11:48:36 +02001112/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001113void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1114 char * input_Y, int radix_A, char * input_A,
1115 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001118 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1122 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1123 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1124 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001125 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001126 if( res == 0 )
1127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001129 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001130
Paul Bakkerbd51b262014-07-10 15:26:12 +02001131exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001133}
Paul Bakker33b43f12013-08-20 11:48:36 +02001134/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001135
Paul Bakker33b43f12013-08-20 11:48:36 +02001136/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001137void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1138 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001141 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 mbedtls_mpi_uint r;
1143 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1146 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001147 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001148 if( res == 0 )
1149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001151 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001152
Paul Bakkerbd51b262014-07-10 15:26:12 +02001153exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001155}
Paul Bakker33b43f12013-08-20 11:48:36 +02001156/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001157
Paul Bakker33b43f12013-08-20 11:48:36 +02001158/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001159void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1160 char * input_E, int radix_N, char * input_N,
1161 int radix_RR, char * input_RR, int radix_X,
1162 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001165 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1167 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1170 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1171 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1172 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001173
Paul Bakker33b43f12013-08-20 11:48:36 +02001174 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001178 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001179 if( res == 0 )
1180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001182 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001183
Paul Bakkerbd51b262014-07-10 15:26:12 +02001184exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1186 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001187}
Paul Bakker33b43f12013-08-20 11:48:36 +02001188/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001189
Paul Bakker33b43f12013-08-20 11:48:36 +02001190/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001191void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesaa850cd2020-12-03 11:35:41 +00001192 int radix_RR, char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001193{
1194 mbedtls_mpi A, E, N, RR, Z;
1195 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1196 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1197
Chris Jonesaa850cd2020-12-03 11:35:41 +00001198 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001199 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001200 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001201 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001202
1203 /* Set E to 2^(E_bytes - 1) + 1 */
1204 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1205 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001206 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001207
1208 /* Set N to 2^(N_bytes - 1) + 1 */
1209 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1210 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001211 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1212
1213 if( strlen( input_RR ) )
1214 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
1215
Chris Jonesaa850cd2020-12-03 11:35:41 +00001216 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001217
1218exit:
1219 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1220 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1221}
1222/* END_CASE */
1223
1224/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001225void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1226 char * input_Y, int radix_A, char * input_A,
1227 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001228{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001230 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1234 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1235 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1236 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001237 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001238 if( res == 0 )
1239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001241 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001242
Paul Bakkerbd51b262014-07-10 15:26:12 +02001243exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001245}
Paul Bakker33b43f12013-08-20 11:48:36 +02001246/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001249void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001250{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001252 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001256 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001257 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001258
Paul Bakkerbd51b262014-07-10 15:26:12 +02001259exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001261}
Paul Bakker33b43f12013-08-20 11:48:36 +02001262/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001265void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001266 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001267{
1268 mbedtls_mpi X;
1269 int res;
1270 mbedtls_test_mpi_random rand;
1271
1272 mbedtls_mpi_init( &X );
1273 rand.data = witnesses;
1274 rand.pos = 0;
1275 rand.chunk_len = chunk_len;
1276
1277 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001278 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1279 mbedtls_test_mpi_miller_rabin_determinizer,
1280 &rand );
1281 TEST_ASSERT( res == 0 );
1282
1283 rand.data = witnesses;
1284 rand.pos = 0;
1285 rand.chunk_len = chunk_len;
1286
Janos Follatha0b67c22018-09-18 14:48:23 +01001287 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1288 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001289 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001290 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001291
1292exit:
1293 mbedtls_mpi_free( &X );
1294}
1295/* END_CASE */
1296
1297/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001298void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001301 int my_ret;
1302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001304
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001305 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1306 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001307 TEST_ASSERT( my_ret == ref_ret );
1308
1309 if( ref_ret == 0 )
1310 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001311 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001312
1313 TEST_ASSERT( actual_bits >= (size_t) bits );
1314 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1315
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001316 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1317 mbedtls_test_rnd_std_rand,
1318 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001319 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001320 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001321 /* X = ( X - 1 ) / 2 */
1322 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001323 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1324 mbedtls_test_rnd_std_rand,
1325 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001326 }
1327 }
1328
Paul Bakkerbd51b262014-07-10 15:26:12 +02001329exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001331}
1332/* END_CASE */
1333
Paul Bakker33b43f12013-08-20 11:48:36 +02001334/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001335void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1336 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 mbedtls_mpi X, A;
1339 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1342 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1343 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1344 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001345
Paul Bakkerbd51b262014-07-10 15:26:12 +02001346exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001348}
Paul Bakker33b43f12013-08-20 11:48:36 +02001349/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001350
Paul Bakker33b43f12013-08-20 11:48:36 +02001351/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001352void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1353 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 mbedtls_mpi X, A;
1356 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1359 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1360 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1361 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001362
Paul Bakkerbd51b262014-07-10 15:26:12 +02001363exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001365}
Paul Bakker33b43f12013-08-20 11:48:36 +02001366/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001367
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001368/* BEGIN_CASE */
1369void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1370{
1371 mbedtls_mpi X;
1372 int ret;
1373 size_t bytes_left = rng_bytes;
1374 mbedtls_mpi_init( &X );
1375
1376 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1377 f_rng_bytes_left, &bytes_left );
1378 TEST_ASSERT( ret == expected_ret );
1379
1380 if( expected_ret == 0 )
1381 {
1382 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1383 * as a big-endian representation of the number. We know when
1384 * our RNG function returns null bytes, so we know how many
1385 * leading zero bytes the number has. */
1386 size_t leading_zeros = 0;
1387 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1388 leading_zeros = 1;
1389 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1390 (size_t) wanted_bytes );
1391 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
1392 }
1393
1394exit:
1395 mbedtls_mpi_free( &X );
1396}
1397/* END_CASE */
1398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001400void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001401{
Andres AG93012e82016-09-09 09:10:28 +01001402 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001403}
Paul Bakker33b43f12013-08-20 11:48:36 +02001404/* END_CASE */