blob: bdc1d9e2f69ab8ef99888c76ca82abd1b6406e84 [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
8
Janos Follath64eca052018-09-05 17:04:49 +01009typedef 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
Paul Bakkerbd51b262014-07-10 15:26:12 +0200853exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000855}
Paul Bakker33b43f12013-08-20 11:48:36 +0200856/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000857
Paul Bakker33b43f12013-08-20 11:48:36 +0200858/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100859void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
860 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100861{
862 mbedtls_mpi X, A;
863 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
864
Janos Follath044a86b2015-10-25 10:58:03 +0100865 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100866
867 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
868 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
869 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
870
871 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
872 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
873 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
874
875 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100876 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
877 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
878
879exit:
880 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
881}
882/* END_CASE */
883
884
885/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100886void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
887 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_mpi X, Y, Z, A;
890 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
893 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
894 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
895 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
896 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000897
Paul Bakkerbd51b262014-07-10 15:26:12 +0200898exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000900}
Paul Bakker33b43f12013-08-20 11:48:36 +0200901/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000902
Paul Bakker33b43f12013-08-20 11:48:36 +0200903/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100904void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
905 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000906{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_mpi X, Y, A;
908 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
911 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
912 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
913 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
914 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000915
Paul Bakkerbd51b262014-07-10 15:26:12 +0200916exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 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 mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
923 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_mpi X, Y, A;
926 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +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( &Y, radix_Y, input_Y ) == 0 );
930 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
931 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
932 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &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( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000936}
Paul Bakker33b43f12013-08-20 11:48:36 +0200937/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000938
Paul Bakker33b43f12013-08-20 11:48:36 +0200939/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100940void mbedtls_mpi_add_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_add_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_sub_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_sub_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_sub_abs( int radix_X, char * input_X, int radix_Y,
976 char * input_Y, int radix_A, char * input_A,
977 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000978{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000980 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
984 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
985 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200988 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000989 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 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( &Y ); 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_sub_int( int radix_X, char * input_X, int input_Y,
999 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001000{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_mpi X, Z, A;
1002 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1005 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1006 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
1007 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001008
Paul Bakkerbd51b262014-07-10 15:26:12 +02001009exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001011}
Paul Bakker33b43f12013-08-20 11:48:36 +02001012/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001013
Paul Bakker33b43f12013-08-20 11:48:36 +02001014/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001015void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
1016 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001017{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_mpi X, Y, Z, A;
1019 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1022 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1023 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1024 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
1025 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001026
Paul Bakkerbd51b262014-07-10 15:26:12 +02001027exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001029}
Paul Bakker33b43f12013-08-20 11:48:36 +02001030/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001031
Paul Bakker33b43f12013-08-20 11:48:36 +02001032/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001033void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1034 int radix_A, char * input_A,
1035 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001036{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 mbedtls_mpi X, Z, A;
1038 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1041 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1042 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001043 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001045 else 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
1048 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001049
Paul Bakkerbd51b262014-07-10 15:26:12 +02001050exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001052}
Paul Bakker33b43f12013-08-20 11:48:36 +02001053/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001054
Paul Bakker33b43f12013-08-20 11:48:36 +02001055/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001056void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1057 char * input_Y, int radix_A, char * input_A,
1058 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001061 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1063 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1066 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1067 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1068 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1069 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001070 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001071 if( res == 0 )
1072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1074 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001075 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001076
Paul Bakkerbd51b262014-07-10 15:26:12 +02001077exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1079 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001080}
Paul Bakker33b43f12013-08-20 11:48:36 +02001081/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001082
Paul Bakker33b43f12013-08-20 11:48:36 +02001083/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001084void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1085 int radix_A, char * input_A, int radix_B,
1086 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001089 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1091 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1094 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1095 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1096 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001097 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001098 if( res == 0 )
1099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1101 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001102 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001103
Paul Bakkerbd51b262014-07-10 15:26:12 +02001104exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1106 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001107}
Paul Bakker33b43f12013-08-20 11:48:36 +02001108/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001109
Paul Bakker33b43f12013-08-20 11:48:36 +02001110/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001111void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1112 char * input_Y, int radix_A, char * input_A,
1113 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001114{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001116 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1120 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1121 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1122 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001123 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001124 if( res == 0 )
1125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001127 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001128
Paul Bakkerbd51b262014-07-10 15:26:12 +02001129exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001131}
Paul Bakker33b43f12013-08-20 11:48:36 +02001132/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001133
Paul Bakker33b43f12013-08-20 11:48:36 +02001134/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001135void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1136 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001139 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_mpi_uint r;
1141 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1144 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001145 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001146 if( res == 0 )
1147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001149 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001150
Paul Bakkerbd51b262014-07-10 15:26:12 +02001151exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001153}
Paul Bakker33b43f12013-08-20 11:48:36 +02001154/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001155
Paul Bakker33b43f12013-08-20 11:48:36 +02001156/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001157void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1158 char * input_E, int radix_N, char * input_N,
1159 int radix_RR, char * input_RR, int radix_X,
1160 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001161{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001163 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1165 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1168 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1169 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1170 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001171
Paul Bakker33b43f12013-08-20 11:48:36 +02001172 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001176 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001177 if( res == 0 )
1178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001180 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001181
Paul Bakkerbd51b262014-07-10 15:26:12 +02001182exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1184 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001185}
Paul Bakker33b43f12013-08-20 11:48:36 +02001186/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001187
Paul Bakker33b43f12013-08-20 11:48:36 +02001188/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001189void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesaa850cd2020-12-03 11:35:41 +00001190 int radix_RR, char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001191{
1192 mbedtls_mpi A, E, N, RR, Z;
1193 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1194 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1195
Chris Jonesaa850cd2020-12-03 11:35:41 +00001196 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001197 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001198 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001199 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001200
1201 /* Set E to 2^(E_bytes - 1) + 1 */
1202 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1203 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001204 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001205
1206 /* Set N to 2^(N_bytes - 1) + 1 */
1207 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1208 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001209 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1210
1211 if( strlen( input_RR ) )
1212 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
1213
Chris Jonesaa850cd2020-12-03 11:35:41 +00001214 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001215
1216exit:
1217 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1218 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1219}
1220/* END_CASE */
1221
1222/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001223void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1224 char * input_Y, int radix_A, char * input_A,
1225 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001228 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1232 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1233 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1234 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001235 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001236 if( res == 0 )
1237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001239 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001240
Paul Bakkerbd51b262014-07-10 15:26:12 +02001241exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001243}
Paul Bakker33b43f12013-08-20 11:48:36 +02001244/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001247void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001250 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001254 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001255 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001256
Paul Bakkerbd51b262014-07-10 15:26:12 +02001257exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001259}
Paul Bakker33b43f12013-08-20 11:48:36 +02001260/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001263void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001264 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001265{
1266 mbedtls_mpi X;
1267 int res;
1268 mbedtls_test_mpi_random rand;
1269
1270 mbedtls_mpi_init( &X );
1271 rand.data = witnesses;
1272 rand.pos = 0;
1273 rand.chunk_len = chunk_len;
1274
1275 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001276 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1277 mbedtls_test_mpi_miller_rabin_determinizer,
1278 &rand );
1279 TEST_ASSERT( res == 0 );
1280
1281 rand.data = witnesses;
1282 rand.pos = 0;
1283 rand.chunk_len = chunk_len;
1284
Janos Follatha0b67c22018-09-18 14:48:23 +01001285 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1286 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001287 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001288 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001289
1290exit:
1291 mbedtls_mpi_free( &X );
1292}
1293/* END_CASE */
1294
1295/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001296void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001299 int my_ret;
1300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001302
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001303 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1304 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001305 TEST_ASSERT( my_ret == ref_ret );
1306
1307 if( ref_ret == 0 )
1308 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001309 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001310
1311 TEST_ASSERT( actual_bits >= (size_t) bits );
1312 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1313
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001314 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1315 mbedtls_test_rnd_std_rand,
1316 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001317 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001318 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001319 /* X = ( X - 1 ) / 2 */
1320 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001321 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1322 mbedtls_test_rnd_std_rand,
1323 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001324 }
1325 }
1326
Paul Bakkerbd51b262014-07-10 15:26:12 +02001327exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001329}
1330/* END_CASE */
1331
Paul Bakker33b43f12013-08-20 11:48:36 +02001332/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001333void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1334 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001335{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 mbedtls_mpi X, A;
1337 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1340 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1341 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1342 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001343
Paul Bakkerbd51b262014-07-10 15:26:12 +02001344exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001346}
Paul Bakker33b43f12013-08-20 11:48:36 +02001347/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001348
Paul Bakker33b43f12013-08-20 11:48:36 +02001349/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001350void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1351 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 mbedtls_mpi X, A;
1354 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1357 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1358 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1359 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001360
Paul Bakkerbd51b262014-07-10 15:26:12 +02001361exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001363}
Paul Bakker33b43f12013-08-20 11:48:36 +02001364/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001365
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001366/* BEGIN_CASE */
1367void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1368{
1369 mbedtls_mpi X;
1370 int ret;
1371 size_t bytes_left = rng_bytes;
1372 mbedtls_mpi_init( &X );
1373
1374 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1375 f_rng_bytes_left, &bytes_left );
1376 TEST_ASSERT( ret == expected_ret );
1377
1378 if( expected_ret == 0 )
1379 {
1380 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1381 * as a big-endian representation of the number. We know when
1382 * our RNG function returns null bytes, so we know how many
1383 * leading zero bytes the number has. */
1384 size_t leading_zeros = 0;
1385 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1386 leading_zeros = 1;
1387 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1388 (size_t) wanted_bytes );
1389 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
1390 }
1391
1392exit:
1393 mbedtls_mpi_free( &X );
1394}
1395/* END_CASE */
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001398void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001399{
Andres AG93012e82016-09-09 09:10:28 +01001400 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001401}
Paul Bakker33b43f12013-08-20 11:48:36 +02001402/* END_CASE */