blob: ffda605b9e0fae17e353b0e0d2e229636e9f4555 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/bignum.h"
Gilles Peskine2f780622020-11-25 15:37:20 +01003#include "mbedtls/entropy.h"
Janos Follath64eca052018-09-05 17:04:49 +01004
Chris Jones5dd1e262020-12-03 17:44:03 +00005#if MBEDTLS_MPI_MAX_BITS > 792
6#define MPI_MAX_BITS_LARGER_THAN_792
Chris Jonesce6fa8f2020-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 Peskine2f780622020-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,
264 mbedtls_mpi_fill_random( NULL, 42, rnd_std_rand,
265 NULL ) );
266 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
267 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000268
Hanno Beckerafb607b2018-12-11 14:27:08 +0000269 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
270 mbedtls_mpi_gcd( NULL, &X, &X ) );
271 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
272 mbedtls_mpi_gcd( &X, NULL, &X ) );
273 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
274 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000275
Hanno Beckerafb607b2018-12-11 14:27:08 +0000276 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
277 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
278 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
279 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
280 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000281 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000282
283exit:
284 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000285}
286/* END_CASE */
287
Paul Bakker33b43f12013-08-20 11:48:36 +0200288/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100289void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200290{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200291 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200292
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200293 mbedtls_mpi_init( &X );
294 mbedtls_mpi_init( &Y );
295 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200296
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200297 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
298 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200299 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200300 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200301
302exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200303 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200304}
305/* END_CASE */
306
307/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100308void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
309 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200310 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000313 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100314 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000317
Janos Follath276284f2019-03-06 12:29:37 +0000318 memset( str, '!', sizeof( str ) );
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200321 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000322 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100323 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200324 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000325 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200326 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath276284f2019-03-06 12:29:37 +0000327 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000328 }
329 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000330
Paul Bakkerbd51b262014-07-10 15:26:12 +0200331exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000333}
Paul Bakker33b43f12013-08-20 11:48:36 +0200334/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000335
Paul Bakker33b43f12013-08-20 11:48:36 +0200336/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100337void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000340 unsigned char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100341 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000344
Paul Bakkere896fea2009-07-06 06:40:23 +0000345
Azim Khand30ca132017-06-09 04:32:58 +0100346 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100347 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200348 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000349
Paul Bakkerbd51b262014-07-10 15:26:12 +0200350exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000352}
Paul Bakker33b43f12013-08-20 11:48:36 +0200353/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000354
Paul Bakker33b43f12013-08-20 11:48:36 +0200355/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100356void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100357 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100358 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000361 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000362 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000363
364 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200371 if( buflen > (size_t) output_size )
372 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200375 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000376 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000377
Azim Khand30ca132017-06-09 04:32:58 +0100378 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000379 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000380
Paul Bakkerbd51b262014-07-10 15:26:12 +0200381exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000383}
Paul Bakker33b43f12013-08-20 11:48:36 +0200384/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100387void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100388 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000391 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000392 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000393 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000394 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000395
396 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000399
Paul Bakker33b43f12013-08-20 11:48:36 +0200400 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200401 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000403 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000404 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000405
Paul Bakker33b43f12013-08-20 11:48:36 +0200406 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 buflen = mbedtls_mpi_size( &X );
409 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000410
Paul Bakkere896fea2009-07-06 06:40:23 +0000411
Azim Khand30ca132017-06-09 04:32:58 +0100412 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000413 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000414
Paul Bakkerbd51b262014-07-10 15:26:12 +0200415exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000417}
Paul Bakker33b43f12013-08-20 11:48:36 +0200418/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100421void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
422 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000423{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000425 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200426 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000431
Paul Bakker33b43f12013-08-20 11:48:36 +0200432 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000433 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200434 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000435 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200436 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000437
Paul Bakker33b43f12013-08-20 11:48:36 +0200438 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000439 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200440 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000441 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200442 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000445
Paul Bakkerbd51b262014-07-10 15:26:12 +0200446exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000448}
Paul Bakker33b43f12013-08-20 11:48:36 +0200449/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000450
Paul Bakker33b43f12013-08-20 11:48:36 +0200451/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100452void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_mpi X;
455 mbedtls_mpi_init( &X );
456 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
457 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000458
Paul Bakkerbd51b262014-07-10 15:26:12 +0200459exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000461}
Paul Bakker33b43f12013-08-20 11:48:36 +0200462/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000463
Paul Bakker33b43f12013-08-20 11:48:36 +0200464/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100465void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
466 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000467{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_mpi X, Y;
469 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
472 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100473 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
474
475 if( result == 0 )
476 {
477 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
478 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000479
Paul Bakkerbd51b262014-07-10 15:26:12 +0200480exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000482}
Paul Bakker33b43f12013-08-20 11:48:36 +0200483/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000484
Paul Bakker33b43f12013-08-20 11:48:36 +0200485/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100486void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_mpi X;
489 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
492 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000493
Paul Bakkerbd51b262014-07-10 15:26:12 +0200494exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000496}
Paul Bakker33b43f12013-08-20 11:48:36 +0200497/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000498
Paul Bakker33b43f12013-08-20 11:48:36 +0200499/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100500void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000501{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_mpi X;
503 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200506 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000507
Paul Bakkerbd51b262014-07-10 15:26:12 +0200508exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000510}
Paul Bakker33b43f12013-08-20 11:48:36 +0200511/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000512
Paul Bakker33b43f12013-08-20 11:48:36 +0200513/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100514void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
515 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_mpi A, X, Y, Z;
518 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
521 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
522 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
523 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
524 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000525
Paul Bakkerbd51b262014-07-10 15:26:12 +0200526exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000528}
Paul Bakker33b43f12013-08-20 11:48:36 +0200529/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000530
Paul Bakker33b43f12013-08-20 11:48:36 +0200531/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_mpi X;
535 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
538 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000539
Paul Bakkerbd51b262014-07-10 15:26:12 +0200540exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000542}
Paul Bakker33b43f12013-08-20 11:48:36 +0200543/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000544
Paul Bakker33b43f12013-08-20 11:48:36 +0200545/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100546void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
547 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_mpi X, Y;
550 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
553 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
554 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000555
Paul Bakkerbd51b262014-07-10 15:26:12 +0200556exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000558}
Paul Bakker33b43f12013-08-20 11:48:36 +0200559/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000560
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* BEGIN_CASE */
Janos Follath27d221a2019-10-14 09:21:49 +0100562void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
563 int size_Y, char * input_Y,
Janos Follath867a3ab2019-10-11 14:21:53 +0100564 int input_ret, int input_err )
Janos Follathe9ae6302019-09-11 16:07:14 +0100565{
Janos Follath867a3ab2019-10-11 14:21:53 +0100566 unsigned ret;
567 unsigned input_uret = input_ret;
Janos Follathe9ae6302019-09-11 16:07:14 +0100568 mbedtls_mpi X, Y;
569 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
570
Janos Follath27d221a2019-10-14 09:21:49 +0100571 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
572 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100573
Gilles Peskine1a30fbb2020-01-21 16:30:53 +0100574 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
575 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100576
Janos Follath867a3ab2019-10-11 14:21:53 +0100577 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follathe9ae6302019-09-11 16:07:14 +0100578 if( input_err == 0 )
Janos Follath867a3ab2019-10-11 14:21:53 +0100579 TEST_ASSERT( ret == input_uret );
Janos Follathe9ae6302019-09-11 16:07:14 +0100580
581exit:
582 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
583}
584/* END_CASE */
585
586/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100587void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
588 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_mpi X, Y;
591 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
594 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
595 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000596
Paul Bakkerbd51b262014-07-10 15:26:12 +0200597exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000599}
Paul Bakker33b43f12013-08-20 11:48:36 +0200600/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000601
Paul Bakker33b43f12013-08-20 11:48:36 +0200602/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100603void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000604{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100605 mbedtls_mpi X, Y;
606 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100609 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100612 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
613 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000614
Paul Bakkerbd51b262014-07-10 15:26:12 +0200615exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100616 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
617}
618/* END_CASE */
619
620/* BEGIN_CASE */
621void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
622{
623 mbedtls_mpi X, Y, X0;
624 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
625
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100626 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
627 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
628 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100629 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
630
631 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
632 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
633 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
634
635exit:
636 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000637}
Paul Bakker33b43f12013-08-20 11:48:36 +0200638/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000639
Paul Bakker33b43f12013-08-20 11:48:36 +0200640/* BEGIN_CASE */
641void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000642{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_mpi X;
644 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
647 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
648 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000649
Paul Bakkerbd51b262014-07-10 15:26:12 +0200650exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000652}
Paul Bakker33b43f12013-08-20 11:48:36 +0200653/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000654
Paul Bakker33b43f12013-08-20 11:48:36 +0200655/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100657{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_mpi X;
659 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100662 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
664 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100665 TEST_ASSERT( X.n == (size_t) after );
666
Paul Bakkerbd51b262014-07-10 15:26:12 +0200667exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100669}
670/* END_CASE */
671
672/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100673void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
674 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_mpi X, Y, XX;
677 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100680 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100682 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
686 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
689 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100690
Paul Bakkerbd51b262014-07-10 15:26:12 +0200691exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100693}
694/* END_CASE */
695
696/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100697void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
698 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
703 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100706 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100708 Y.s = y_sign;
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
711 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
714 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
715 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
718 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
719 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100720
Paul Bakkerbd51b262014-07-10 15:26:12 +0200721exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
723 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100724}
725/* END_CASE */
726
727/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100728void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000729{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100730 mbedtls_mpi X, Y;
731 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
734 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100735 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
736 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100739 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
740 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000741
Paul Bakkerbd51b262014-07-10 15:26:12 +0200742exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100743 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
744}
745/* END_CASE */
746
747/* BEGIN_CASE */
748void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
749{
750 mbedtls_mpi X, Y, X0, Y0;
751 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
752 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
753
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100754 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
755 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
756 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
757 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100758
759 mbedtls_mpi_swap( &X, &Y );
760 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
761 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
762
763exit:
764 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
765 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
766}
767/* END_CASE */
768
769/* BEGIN_CASE */
770void mpi_swap_self( data_t *input_X )
771{
772 mbedtls_mpi X, X0;
773 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
774
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100775 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
776 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100777
778 mbedtls_mpi_swap( &X, &X );
779 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
780
781exit:
782 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000783}
Paul Bakker33b43f12013-08-20 11:48:36 +0200784/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000785
Paul Bakker33b43f12013-08-20 11:48:36 +0200786/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100787void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
788 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000789{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_mpi X, Y, Z, A;
791 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
794 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
795 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
796 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
797 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000798
Paul Bakkerbd51b262014-07-10 15:26:12 +0200799exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000801}
Paul Bakker33b43f12013-08-20 11:48:36 +0200802/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000803
Paul Bakker33b43f12013-08-20 11:48:36 +0200804/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100805void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
806 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100807{
808 mbedtls_mpi X, A;
809 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
810
Janos Follath044a86b2015-10-25 10:58:03 +0100811 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100812
813 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
814 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
815 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
816
817 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
818 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
819 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
820
821 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100822 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
823 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
824
825exit:
826 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
827}
828/* END_CASE */
829
830
831/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100832void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
833 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000834{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_mpi X, Y, Z, A;
836 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
839 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
840 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
841 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
842 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000843
Paul Bakkerbd51b262014-07-10 15:26:12 +0200844exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000846}
Paul Bakker33b43f12013-08-20 11:48:36 +0200847/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000848
Paul Bakker33b43f12013-08-20 11:48:36 +0200849/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100850void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
851 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_mpi X, Y, A;
854 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
857 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
858 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
859 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
860 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000861
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( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000864}
Paul Bakker33b43f12013-08-20 11:48:36 +0200865/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000866
Paul Bakker33b43f12013-08-20 11:48:36 +0200867/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100868void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
869 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000870{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_mpi X, Y, A;
872 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
875 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
876 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
877 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
878 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000879
Paul Bakkerbd51b262014-07-10 15:26:12 +0200880exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000882}
Paul Bakker33b43f12013-08-20 11:48:36 +0200883/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000884
Paul Bakker33b43f12013-08-20 11:48:36 +0200885/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100886void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
887 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, Z, A;
890 mbedtls_mpi_init( &X ); 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( &A, radix_A, input_A ) == 0 );
894 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
895 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000896
Paul Bakkerbd51b262014-07-10 15:26:12 +0200897exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000899}
Paul Bakker33b43f12013-08-20 11:48:36 +0200900/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000901
Paul Bakker33b43f12013-08-20 11:48:36 +0200902/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100903void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
904 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_mpi X, Y, Z, A;
907 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
910 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
911 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
912 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
913 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000914
Paul Bakkerbd51b262014-07-10 15:26:12 +0200915exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000917}
Paul Bakker33b43f12013-08-20 11:48:36 +0200918/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000919
Paul Bakker33b43f12013-08-20 11:48:36 +0200920/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100921void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
922 char * input_Y, int radix_A, char * input_A,
923 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000926 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
930 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
931 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200934 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000935 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000937
Paul Bakkerbd51b262014-07-10 15:26:12 +0200938exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000940}
Paul Bakker33b43f12013-08-20 11:48:36 +0200941/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000942
Paul Bakker33b43f12013-08-20 11:48:36 +0200943/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100944void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
945 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000946{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_mpi X, Z, A;
948 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
951 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
952 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
953 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000954
Paul Bakkerbd51b262014-07-10 15:26:12 +0200955exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000957}
Paul Bakker33b43f12013-08-20 11:48:36 +0200958/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000959
Paul Bakker33b43f12013-08-20 11:48:36 +0200960/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100961void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
962 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000963{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_mpi X, Y, Z, A;
965 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
968 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
969 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
970 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
971 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000972
Paul Bakkerbd51b262014-07-10 15:26:12 +0200973exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000975}
Paul Bakker33b43f12013-08-20 11:48:36 +0200976/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000977
Paul Bakker33b43f12013-08-20 11:48:36 +0200978/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100979void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
980 int radix_A, char * input_A,
981 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000982{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_mpi X, Z, A;
984 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
987 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
988 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200989 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200991 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200993 else
994 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000995
Paul Bakkerbd51b262014-07-10 15:26:12 +0200996exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000998}
Paul Bakker33b43f12013-08-20 11:48:36 +0200999/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001000
Paul Bakker33b43f12013-08-20 11:48:36 +02001001/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001002void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1003 char * input_Y, int radix_A, char * input_A,
1004 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001005{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001007 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1009 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1012 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1013 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1014 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1015 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001016 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001017 if( res == 0 )
1018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1020 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001021 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001022
Paul Bakkerbd51b262014-07-10 15:26:12 +02001023exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1025 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001026}
Paul Bakker33b43f12013-08-20 11:48:36 +02001027/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001028
Paul Bakker33b43f12013-08-20 11:48:36 +02001029/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001030void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1031 int radix_A, char * input_A, int radix_B,
1032 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001035 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1037 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1040 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1041 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1042 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001043 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001044 if( res == 0 )
1045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1047 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001048 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001049
Paul Bakkerbd51b262014-07-10 15:26:12 +02001050exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1052 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001053}
Paul Bakker33b43f12013-08-20 11:48:36 +02001054/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001055
Paul Bakker33b43f12013-08-20 11:48:36 +02001056/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001057void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1058 char * input_Y, int radix_A, char * input_A,
1059 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001062 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1066 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1067 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1068 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001069 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001070 if( res == 0 )
1071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001073 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001074
Paul Bakkerbd51b262014-07-10 15:26:12 +02001075exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001077}
Paul Bakker33b43f12013-08-20 11:48:36 +02001078/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001079
Paul Bakker33b43f12013-08-20 11:48:36 +02001080/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001081void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1082 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001085 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 mbedtls_mpi_uint r;
1087 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1090 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001091 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001092 if( res == 0 )
1093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001095 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001096
Paul Bakkerbd51b262014-07-10 15:26:12 +02001097exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001099}
Paul Bakker33b43f12013-08-20 11:48:36 +02001100/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001101
Paul Bakker33b43f12013-08-20 11:48:36 +02001102/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001103void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1104 char * input_E, int radix_N, char * input_N,
1105 int radix_RR, char * input_RR, int radix_X,
1106 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001109 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1111 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1114 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1115 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1116 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001117
Paul Bakker33b43f12013-08-20 11:48:36 +02001118 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001122 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001123 if( res == 0 )
1124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001126 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001127
Paul Bakkerbd51b262014-07-10 15:26:12 +02001128exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1130 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
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 */
Chris Jones415c7be2020-12-02 10:41:50 +00001135void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesa18813e2020-12-03 11:35:41 +00001136 int radix_RR, char * input_RR, int exp_result )
Chris Jones415c7be2020-12-02 10:41:50 +00001137{
1138 mbedtls_mpi A, E, N, RR, Z;
1139 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1140 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1141
Chris Jonesa18813e2020-12-03 11:35:41 +00001142 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jones415c7be2020-12-02 10:41:50 +00001143 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001144 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001145 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001146
1147 /* Set E to 2^(E_bytes - 1) + 1 */
1148 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1149 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001150 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001151
1152 /* Set N to 2^(N_bytes - 1) + 1 */
1153 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1154 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001155 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1156
1157 if( strlen( input_RR ) )
1158 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
1159
Chris Jonesa18813e2020-12-03 11:35:41 +00001160 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jones415c7be2020-12-02 10:41:50 +00001161
1162exit:
1163 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1164 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1165}
1166/* END_CASE */
1167
1168/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001169void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1170 char * input_Y, int radix_A, char * input_A,
1171 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001174 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1178 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1179 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1180 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001181 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001182 if( res == 0 )
1183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001185 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001186
Paul Bakkerbd51b262014-07-10 15:26:12 +02001187exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001189}
Paul Bakker33b43f12013-08-20 11:48:36 +02001190/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001193void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001196 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001200 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001201 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001202
Paul Bakkerbd51b262014-07-10 15:26:12 +02001203exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001205}
Paul Bakker33b43f12013-08-20 11:48:36 +02001206/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001209void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001210 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001211{
1212 mbedtls_mpi X;
1213 int res;
1214 mbedtls_test_mpi_random rand;
1215
1216 mbedtls_mpi_init( &X );
1217 rand.data = witnesses;
1218 rand.pos = 0;
1219 rand.chunk_len = chunk_len;
1220
1221 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001222 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1223 mbedtls_test_mpi_miller_rabin_determinizer,
1224 &rand );
1225 TEST_ASSERT( res == 0 );
1226
1227 rand.data = witnesses;
1228 rand.pos = 0;
1229 rand.chunk_len = chunk_len;
1230
Janos Follatha0b67c22018-09-18 14:48:23 +01001231 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1232 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001233 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001234 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001235
1236exit:
1237 mbedtls_mpi_free( &X );
1238}
1239/* END_CASE */
1240
1241/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001242void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001245 int my_ret;
1246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001248
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001249 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001250 TEST_ASSERT( my_ret == ref_ret );
1251
1252 if( ref_ret == 0 )
1253 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001254 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001255
1256 TEST_ASSERT( actual_bits >= (size_t) bits );
1257 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1258
Janos Follatha0b67c22018-09-18 14:48:23 +01001259 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1260 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001261 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001262 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001263 /* X = ( X - 1 ) / 2 */
1264 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001265 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1266 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001267 }
1268 }
1269
Paul Bakkerbd51b262014-07-10 15:26:12 +02001270exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001272}
1273/* END_CASE */
1274
Paul Bakker33b43f12013-08-20 11:48:36 +02001275/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001276void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1277 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 mbedtls_mpi X, A;
1280 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1283 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1284 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1285 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001286
Paul Bakkerbd51b262014-07-10 15:26:12 +02001287exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001289}
Paul Bakker33b43f12013-08-20 11:48:36 +02001290/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001291
Paul Bakker33b43f12013-08-20 11:48:36 +02001292/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001293void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1294 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001295{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_mpi X, A;
1297 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1300 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1301 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1302 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001303
Paul Bakkerbd51b262014-07-10 15:26:12 +02001304exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001306}
Paul Bakker33b43f12013-08-20 11:48:36 +02001307/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001308
Gilles Peskine2f780622020-11-25 15:37:20 +01001309/* BEGIN_CASE */
1310void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1311{
1312 mbedtls_mpi X;
1313 int ret;
1314 size_t bytes_left = rng_bytes;
1315 mbedtls_mpi_init( &X );
1316
1317 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1318 f_rng_bytes_left, &bytes_left );
1319 TEST_ASSERT( ret == expected_ret );
1320
1321 if( expected_ret == 0 )
1322 {
1323 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1324 * as a big-endian representation of the number. We know when
1325 * our RNG function returns null bytes, so we know how many
1326 * leading zero bytes the number has. */
1327 size_t leading_zeros = 0;
1328 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1329 leading_zeros = 1;
1330 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1331 (size_t) wanted_bytes );
1332 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
1333 }
1334
1335exit:
1336 mbedtls_mpi_free( &X );
1337}
1338/* END_CASE */
1339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001341void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001342{
Andres AG93012e82016-09-09 09:10:28 +01001343 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001344}
Paul Bakker33b43f12013-08-20 11:48:36 +02001345/* END_CASE */