blob: 1e4ebae85500d653f12b2b93ba22a9b5cb71536c [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
Janos Follath64eca052018-09-05 17:04:49 +01008
9typedef struct mbedtls_test_mpi_random
10{
11 data_t *data;
12 size_t pos;
13 size_t chunk_len;
14} mbedtls_test_mpi_random;
15
16/*
17 * This function is called by the Miller-Rabin primality test each time it
18 * chooses a random witness. The witnesses (or non-witnesses as provided by the
19 * test) are stored in the data member of the state structure. Each number is in
20 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
21 */
22int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
23 unsigned char* buf,
24 size_t len )
25{
26 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
27
28 if( random == NULL || random->data->x == NULL || buf == NULL )
29 return( -1 );
30
31 if( random->pos + random->chunk_len > random->data->len
32 || random->chunk_len > len )
33 {
34 return( -1 );
35 }
36
37 memset( buf, 0, len );
38
39 /* The witness is written to the end of the buffer, since the buffer is
40 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
41 * Writing the witness to the start of the buffer would result in the
42 * buffer being 'witness 000...000', which would be treated as
43 * witness * 2^n for some n. */
44 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
45 random->chunk_len );
46
47 random->pos += random->chunk_len;
48
49 return( 0 );
50}
Gilles 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
Gilles Peskineb8e15342021-06-10 23:18:39 +0200368 TEST_ASSERT( mbedtls_test_read_mpi( &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
Ronald Cron9fde3532020-06-10 11:42:32 +0200378 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
379 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000380 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000381
Paul Bakkerbd51b262014-07-10 15:26:12 +0200382exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000384}
Paul Bakker33b43f12013-08-20 11:48:36 +0200385/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100388void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100389 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000392 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000393 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000394 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000395 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000396
397 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000400
Paul Bakker33b43f12013-08-20 11:48:36 +0200401 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200402 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000404 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000405 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000406
Paul Bakker33b43f12013-08-20 11:48:36 +0200407 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 buflen = mbedtls_mpi_size( &X );
410 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000411
Paul Bakkere896fea2009-07-06 06:40:23 +0000412
Ronald Cron9fde3532020-06-10 11:42:32 +0200413 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
414 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000415 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000416
Paul Bakkerbd51b262014-07-10 15:26:12 +0200417exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000419}
Paul Bakker33b43f12013-08-20 11:48:36 +0200420/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100423void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
424 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000425{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000427 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200428 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000431
Gilles Peskineb8e15342021-06-10 23:18:39 +0200432 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000433
Paul Bakker33b43f12013-08-20 11:48:36 +0200434 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000435 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200436 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000437 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200438 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000439
Paul Bakker33b43f12013-08-20 11:48:36 +0200440 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000441 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200442 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000443 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200444 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000447
Paul Bakkerbd51b262014-07-10 15:26:12 +0200448exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000450}
Paul Bakker33b43f12013-08-20 11:48:36 +0200451/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000452
Paul Bakker33b43f12013-08-20 11:48:36 +0200453/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100454void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000455{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_mpi X;
457 mbedtls_mpi_init( &X );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200458 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000460
Paul Bakkerbd51b262014-07-10 15:26:12 +0200461exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000463}
Paul Bakker33b43f12013-08-20 11:48:36 +0200464/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000465
Paul Bakker33b43f12013-08-20 11:48:36 +0200466/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100467void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
468 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000469{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_mpi X, Y;
471 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000472
Gilles Peskineb8e15342021-06-10 23:18:39 +0200473 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
474 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100475 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
476
477 if( result == 0 )
478 {
479 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
480 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000481
Paul Bakkerbd51b262014-07-10 15:26:12 +0200482exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000484}
Paul Bakker33b43f12013-08-20 11:48:36 +0200485/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000486
Paul Bakker33b43f12013-08-20 11:48:36 +0200487/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100488void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000489{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_mpi X;
491 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000492
Gilles Peskineb8e15342021-06-10 23:18:39 +0200493 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000495
Paul Bakkerbd51b262014-07-10 15:26:12 +0200496exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000498}
Paul Bakker33b43f12013-08-20 11:48:36 +0200499/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000500
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100502void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_mpi X;
505 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000506
Gilles Peskineb8e15342021-06-10 23:18:39 +0200507 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200508 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000509
Paul Bakkerbd51b262014-07-10 15:26:12 +0200510exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000512}
Paul Bakker33b43f12013-08-20 11:48:36 +0200513/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000514
Paul Bakker33b43f12013-08-20 11:48:36 +0200515/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100516void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
517 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_mpi A, X, Y, Z;
520 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000521
Gilles Peskineb8e15342021-06-10 23:18:39 +0200522 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
523 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
524 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
526 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000527
Paul Bakkerbd51b262014-07-10 15:26:12 +0200528exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000530}
Paul Bakker33b43f12013-08-20 11:48:36 +0200531/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000532
Paul Bakker33b43f12013-08-20 11:48:36 +0200533/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000535{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_mpi X;
537 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
540 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000541
Paul Bakkerbd51b262014-07-10 15:26:12 +0200542exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000544}
Paul Bakker33b43f12013-08-20 11:48:36 +0200545/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000546
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100548void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
549 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_mpi X, Y;
552 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000553
Gilles Peskineb8e15342021-06-10 23:18:39 +0200554 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
555 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000557
Paul Bakkerbd51b262014-07-10 15:26:12 +0200558exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000560}
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000562
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* BEGIN_CASE */
Janos Follath27d221a2019-10-14 09:21:49 +0100564void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
565 int size_Y, char * input_Y,
Janos Follath867a3ab2019-10-11 14:21:53 +0100566 int input_ret, int input_err )
Janos Follathe9ae6302019-09-11 16:07:14 +0100567{
Gilles Peskine319ecf32020-09-02 15:18:07 +0200568 unsigned ret = -1;
Janos Follath867a3ab2019-10-11 14:21:53 +0100569 unsigned input_uret = input_ret;
Janos Follathe9ae6302019-09-11 16:07:14 +0100570 mbedtls_mpi X, Y;
571 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
572
Gilles Peskineb8e15342021-06-10 23:18:39 +0200573 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 );
574 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, input_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100575
Gilles Peskine1a30fbb2020-01-21 16:30:53 +0100576 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
577 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100578
Janos Follath867a3ab2019-10-11 14:21:53 +0100579 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follathe9ae6302019-09-11 16:07:14 +0100580 if( input_err == 0 )
Janos Follath867a3ab2019-10-11 14:21:53 +0100581 TEST_ASSERT( ret == input_uret );
Janos Follathe9ae6302019-09-11 16:07:14 +0100582
583exit:
584 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
585}
586/* END_CASE */
587
588/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100589void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
590 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000591{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_mpi X, Y;
593 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000594
Gilles Peskineb8e15342021-06-10 23:18:39 +0200595 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
596 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000598
Paul Bakkerbd51b262014-07-10 15:26:12 +0200599exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000601}
Paul Bakker33b43f12013-08-20 11:48:36 +0200602/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000603
Paul Bakker33b43f12013-08-20 11:48:36 +0200604/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100605void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000606{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100607 mbedtls_mpi X, Y;
608 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100611 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100614 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
615 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000616
Paul Bakkerbd51b262014-07-10 15:26:12 +0200617exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100618 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
619}
620/* END_CASE */
621
622/* BEGIN_CASE */
623void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
624{
625 mbedtls_mpi X, Y, X0;
626 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
627
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100628 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
629 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
630 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100631 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
632
633 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
634 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
635 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
636
637exit:
638 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000639}
Paul Bakker33b43f12013-08-20 11:48:36 +0200640/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000641
Paul Bakker33b43f12013-08-20 11:48:36 +0200642/* BEGIN_CASE */
643void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_mpi X;
646 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
649 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
650 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000651
Paul Bakkerbd51b262014-07-10 15:26:12 +0200652exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000654}
Paul Bakker33b43f12013-08-20 11:48:36 +0200655/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000656
Paul Bakker33b43f12013-08-20 11:48:36 +0200657/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_mpi X;
661 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100664 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
666 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100667 TEST_ASSERT( X.n == (size_t) after );
668
Paul Bakkerbd51b262014-07-10 15:26:12 +0200669exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100671}
672/* END_CASE */
673
674/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100675void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
676 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_mpi X, Y, XX;
679 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100680
Gilles Peskineb8e15342021-06-10 23:18:39 +0200681 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100682 X.s = x_sign;
Gilles Peskineb8e15342021-06-10 23:18:39 +0200683 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100684 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
688 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
691 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100692
Paul Bakkerbd51b262014-07-10 15:26:12 +0200693exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100695}
696/* END_CASE */
697
698/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100699void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
700 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100701{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
705 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100706
Gilles Peskineb8e15342021-06-10 23:18:39 +0200707 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100708 X.s = x_sign;
Gilles Peskineb8e15342021-06-10 23:18:39 +0200709 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100710 Y.s = y_sign;
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
713 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
716 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
717 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
720 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
721 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100722
Paul Bakkerbd51b262014-07-10 15:26:12 +0200723exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
725 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100726}
727/* END_CASE */
728
729/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100730void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000731{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100732 mbedtls_mpi X, Y;
733 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
736 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100737 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
738 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100741 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
742 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000743
Paul Bakkerbd51b262014-07-10 15:26:12 +0200744exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100745 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
746}
747/* END_CASE */
748
749/* BEGIN_CASE */
750void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
751{
752 mbedtls_mpi X, Y, X0, Y0;
753 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
754 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
755
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100756 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
757 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
758 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
759 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100760
761 mbedtls_mpi_swap( &X, &Y );
762 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
763 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
764
765exit:
766 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
767 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
768}
769/* END_CASE */
770
771/* BEGIN_CASE */
772void mpi_swap_self( data_t *input_X )
773{
774 mbedtls_mpi X, X0;
775 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
776
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100777 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
778 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100779
780 mbedtls_mpi_swap( &X, &X );
781 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
782
783exit:
784 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000785}
Paul Bakker33b43f12013-08-20 11:48:36 +0200786/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000787
Paul Bakker33b43f12013-08-20 11:48:36 +0200788/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100789void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
790 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_mpi X, Y, Z, A;
793 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000794
Gilles Peskineb8e15342021-06-10 23:18:39 +0200795 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
796 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
797 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
799 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000800
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200801 /* result == first operand */
802 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
803 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200804 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200805
806 /* result == second operand */
807 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
808 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
809
Paul Bakkerbd51b262014-07-10 15:26:12 +0200810exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000812}
Paul Bakker33b43f12013-08-20 11:48:36 +0200813/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000814
Paul Bakker33b43f12013-08-20 11:48:36 +0200815/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100816void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
817 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100818{
819 mbedtls_mpi X, A;
820 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
821
Gilles Peskineb8e15342021-06-10 23:18:39 +0200822 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100823
Gilles Peskineb8e15342021-06-10 23:18:39 +0200824 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100825 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
826 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
827
Gilles Peskineb8e15342021-06-10 23:18:39 +0200828 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100829 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
830 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
831
Gilles Peskineb8e15342021-06-10 23:18:39 +0200832 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100833 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
834 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
835
836exit:
837 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
838}
839/* END_CASE */
840
841
842/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100843void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
844 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000845{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_mpi X, Y, Z, A;
847 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000848
Gilles Peskineb8e15342021-06-10 23:18:39 +0200849 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
850 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
851 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
853 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000854
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200855 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
857 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200858 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200859
860 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
862 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000863
Paul Bakkerbd51b262014-07-10 15:26:12 +0200864exit:
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200865 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000866}
Paul Bakker33b43f12013-08-20 11:48:36 +0200867/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000868
Paul Bakker33b43f12013-08-20 11:48:36 +0200869/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100870void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
871 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000872{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_mpi X, Z, A;
874 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000875
Gilles Peskineb8e15342021-06-10 23:18:39 +0200876 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
877 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
879 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000880
Paul Bakkerbd51b262014-07-10 15:26:12 +0200881exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000883}
Paul Bakker33b43f12013-08-20 11:48:36 +0200884/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000885
Paul Bakker33b43f12013-08-20 11:48:36 +0200886/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100887void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
888 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_mpi X, Y, Z, A;
891 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000892
Gilles Peskineb8e15342021-06-10 23:18:39 +0200893 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
894 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
895 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
897 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000898
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200899 /* result == first operand */
900 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
901 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200902 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200903
904 /* result == second operand */
905 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
906 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
907
Paul Bakkerbd51b262014-07-10 15:26:12 +0200908exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000910}
Paul Bakker33b43f12013-08-20 11:48:36 +0200911/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000912
Paul Bakker33b43f12013-08-20 11:48:36 +0200913/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100914void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
915 char * input_Y, int radix_A, char * input_A,
916 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000919 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000921
Gilles Peskineb8e15342021-06-10 23:18:39 +0200922 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
923 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
924 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200927 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000928 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000930
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200931 /* result == first operand */
932 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
933 if( sub_result == 0 )
934 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200935 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200936
937 /* result == second operand */
938 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
939 if( sub_result == 0 )
940 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
941
Paul Bakkerbd51b262014-07-10 15:26:12 +0200942exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000944}
Paul Bakker33b43f12013-08-20 11:48:36 +0200945/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000946
Paul Bakker33b43f12013-08-20 11:48:36 +0200947/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100948void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
949 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000950{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_mpi X, Z, A;
952 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000953
Gilles Peskineb8e15342021-06-10 23:18:39 +0200954 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
955 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
957 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000958
Paul Bakkerbd51b262014-07-10 15:26:12 +0200959exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000961}
Paul Bakker33b43f12013-08-20 11:48:36 +0200962/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000963
Paul Bakker33b43f12013-08-20 11:48:36 +0200964/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100965void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
966 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000967{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_mpi X, Y, Z, A;
969 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000970
Gilles Peskineb8e15342021-06-10 23:18:39 +0200971 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
972 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
973 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
975 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000976
Paul Bakkerbd51b262014-07-10 15:26:12 +0200977exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000979}
Paul Bakker33b43f12013-08-20 11:48:36 +0200980/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000981
Paul Bakker33b43f12013-08-20 11:48:36 +0200982/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100983void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
984 int radix_A, char * input_A,
985 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 mbedtls_mpi X, Z, A;
988 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000989
Gilles Peskineb8e15342021-06-10 23:18:39 +0200990 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
991 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200993 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200995 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200997 else
998 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000999
Paul Bakkerbd51b262014-07-10 15:26:12 +02001000exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001002}
Paul Bakker33b43f12013-08-20 11:48:36 +02001003/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001004
Paul Bakker33b43f12013-08-20 11:48:36 +02001005/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001006void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1007 char * input_Y, int radix_A, char * input_A,
1008 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001011 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1013 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001014
Gilles Peskineb8e15342021-06-10 23:18:39 +02001015 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1016 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1017 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1018 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001020 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001021 if( res == 0 )
1022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1024 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001025 }
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( &Q ); mbedtls_mpi_free( &R );
1029 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001030}
Paul Bakker33b43f12013-08-20 11:48:36 +02001031/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001032
Paul Bakker33b43f12013-08-20 11:48:36 +02001033/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001034void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1035 int radix_A, char * input_A, int radix_B,
1036 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001037{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001039 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1041 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001042
Gilles Peskineb8e15342021-06-10 23:18:39 +02001043 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1044 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1045 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001047 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001048 if( res == 0 )
1049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1051 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001052 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001053
Paul Bakkerbd51b262014-07-10 15:26:12 +02001054exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1056 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001057}
Paul Bakker33b43f12013-08-20 11:48:36 +02001058/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001059
Paul Bakker33b43f12013-08-20 11:48:36 +02001060/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001061void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1062 char * input_Y, int radix_A, char * input_A,
1063 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001066 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001068
Gilles Peskineb8e15342021-06-10 23:18:39 +02001069 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1070 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1071 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001073 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001074 if( res == 0 )
1075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001077 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001078
Paul Bakkerbd51b262014-07-10 15:26:12 +02001079exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001081}
Paul Bakker33b43f12013-08-20 11:48:36 +02001082/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001083
Paul Bakker33b43f12013-08-20 11:48:36 +02001084/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001085void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1086 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001089 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_mpi_uint r;
1091 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001092
Gilles Peskineb8e15342021-06-10 23:18:39 +02001093 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001095 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001096 if( res == 0 )
1097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001099 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001100
Paul Bakkerbd51b262014-07-10 15:26:12 +02001101exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001103}
Paul Bakker33b43f12013-08-20 11:48:36 +02001104/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001105
Paul Bakker33b43f12013-08-20 11:48:36 +02001106/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001107void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1108 char * input_E, int radix_N, char * input_N,
1109 int radix_RR, char * input_RR, int radix_X,
1110 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001113 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1115 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001116
Gilles Peskineb8e15342021-06-10 23:18:39 +02001117 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1118 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
1119 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
1120 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001121
Paul Bakker33b43f12013-08-20 11:48:36 +02001122 if( strlen( input_RR ) )
Gilles Peskineb8e15342021-06-10 23:18:39 +02001123 TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001126 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001127 if( res == 0 )
1128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001130 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001131
Paul Bakkerbd51b262014-07-10 15:26:12 +02001132exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1134 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001135}
Paul Bakker33b43f12013-08-20 11:48:36 +02001136/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001137
Paul Bakker33b43f12013-08-20 11:48:36 +02001138/* BEGIN_CASE */
Chris Jones415c7be2020-12-02 10:41:50 +00001139void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesa18813e2020-12-03 11:35:41 +00001140 int radix_RR, char * input_RR, int exp_result )
Chris Jones415c7be2020-12-02 10:41:50 +00001141{
1142 mbedtls_mpi A, E, N, RR, Z;
1143 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1144 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1145
Chris Jonesa18813e2020-12-03 11:35:41 +00001146 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jones415c7be2020-12-02 10:41:50 +00001147 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001148 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001149 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001150
1151 /* Set E to 2^(E_bytes - 1) + 1 */
1152 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1153 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001154 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001155
1156 /* Set N to 2^(N_bytes - 1) + 1 */
1157 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1158 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001159 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1160
1161 if( strlen( input_RR ) )
Gilles Peskineb8e15342021-06-10 23:18:39 +02001162 TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001163
Chris Jonesa18813e2020-12-03 11:35:41 +00001164 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jones415c7be2020-12-02 10:41:50 +00001165
1166exit:
1167 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1168 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1169}
1170/* END_CASE */
1171
1172/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001173void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1174 char * input_Y, int radix_A, char * input_A,
1175 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001178 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001180
Gilles Peskineb8e15342021-06-10 23:18:39 +02001181 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1182 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1183 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001185 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001186 if( res == 0 )
1187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001189 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001190
Paul Bakkerbd51b262014-07-10 15:26:12 +02001191exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001193}
Paul Bakker33b43f12013-08-20 11:48:36 +02001194/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001197void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001200 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001202
Gilles Peskineb8e15342021-06-10 23:18:39 +02001203 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001204 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001205 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001206
Paul Bakkerbd51b262014-07-10 15:26:12 +02001207exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001209}
Paul Bakker33b43f12013-08-20 11:48:36 +02001210/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001213void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001214 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001215{
1216 mbedtls_mpi X;
1217 int res;
1218 mbedtls_test_mpi_random rand;
1219
1220 mbedtls_mpi_init( &X );
1221 rand.data = witnesses;
1222 rand.pos = 0;
1223 rand.chunk_len = chunk_len;
1224
1225 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001226 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1227 mbedtls_test_mpi_miller_rabin_determinizer,
1228 &rand );
1229 TEST_ASSERT( res == 0 );
1230
1231 rand.data = witnesses;
1232 rand.pos = 0;
1233 rand.chunk_len = chunk_len;
1234
Janos Follatha0b67c22018-09-18 14:48:23 +01001235 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1236 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001237 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001238 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001239
1240exit:
1241 mbedtls_mpi_free( &X );
1242}
1243/* END_CASE */
1244
1245/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001246void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001247{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001249 int my_ret;
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001252
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001253 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001254 TEST_ASSERT( my_ret == ref_ret );
1255
1256 if( ref_ret == 0 )
1257 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001258 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001259
1260 TEST_ASSERT( actual_bits >= (size_t) bits );
1261 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1262
Janos Follatha0b67c22018-09-18 14:48:23 +01001263 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1264 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001265 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001266 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001267 /* X = ( X - 1 ) / 2 */
1268 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001269 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1270 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001271 }
1272 }
1273
Paul Bakkerbd51b262014-07-10 15:26:12 +02001274exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001276}
1277/* END_CASE */
1278
Paul Bakker33b43f12013-08-20 11:48:36 +02001279/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001280void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1281 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001282{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_mpi X, A;
1284 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001285
Gilles Peskineb8e15342021-06-10 23:18:39 +02001286 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1287 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1289 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001290
Paul Bakkerbd51b262014-07-10 15:26:12 +02001291exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001293}
Paul Bakker33b43f12013-08-20 11:48:36 +02001294/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001295
Paul Bakker33b43f12013-08-20 11:48:36 +02001296/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001297void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1298 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 mbedtls_mpi X, A;
1301 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001302
Gilles Peskineb8e15342021-06-10 23:18:39 +02001303 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1304 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1306 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001307
Paul Bakkerbd51b262014-07-10 15:26:12 +02001308exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001310}
Paul Bakker33b43f12013-08-20 11:48:36 +02001311/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001312
Gilles Peskine2f780622020-11-25 15:37:20 +01001313/* BEGIN_CASE */
1314void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1315{
1316 mbedtls_mpi X;
1317 int ret;
1318 size_t bytes_left = rng_bytes;
1319 mbedtls_mpi_init( &X );
1320
1321 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1322 f_rng_bytes_left, &bytes_left );
1323 TEST_ASSERT( ret == expected_ret );
1324
1325 if( expected_ret == 0 )
1326 {
1327 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1328 * as a big-endian representation of the number. We know when
1329 * our RNG function returns null bytes, so we know how many
1330 * leading zero bytes the number has. */
1331 size_t leading_zeros = 0;
1332 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1333 leading_zeros = 1;
1334 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1335 (size_t) wanted_bytes );
1336 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
1337 }
1338
1339exit:
1340 mbedtls_mpi_free( &X );
1341}
1342/* END_CASE */
1343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001345void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001346{
Andres AG93012e82016-09-09 09:10:28 +01001347 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001348}
Paul Bakker33b43f12013-08-20 11:48:36 +02001349/* END_CASE */