blob: cdc764fb47088290b5e61e87e2f480b214c54e76 [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
Gilles Peskine8854c5d2021-06-10 15:34:15 +02009/* Check the validity of the sign bit in an MPI object. Reject representations
10 * that are not supported by the rest of the library and indicate a bug when
11 * constructing the value. */
12static int sign_is_valid( const mbedtls_mpi *X )
13{
14 if( X->s != 1 && X->s != -1 )
15 return( 0 ); // invalid sign bit, e.g. 0
16 if( mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 )
17 return( 0 ); // negative zero
18 return( 1 );
19}
20
Janos Follath64eca052018-09-05 17:04:49 +010021typedef struct mbedtls_test_mpi_random
22{
23 data_t *data;
24 size_t pos;
25 size_t chunk_len;
26} mbedtls_test_mpi_random;
27
28/*
29 * This function is called by the Miller-Rabin primality test each time it
30 * chooses a random witness. The witnesses (or non-witnesses as provided by the
31 * test) are stored in the data member of the state structure. Each number is in
32 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
33 */
34int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
35 unsigned char* buf,
36 size_t len )
37{
38 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
39
40 if( random == NULL || random->data->x == NULL || buf == NULL )
41 return( -1 );
42
43 if( random->pos + random->chunk_len > random->data->len
44 || random->chunk_len > len )
45 {
46 return( -1 );
47 }
48
49 memset( buf, 0, len );
50
51 /* The witness is written to the end of the buffer, since the buffer is
52 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
53 * Writing the witness to the start of the buffer would result in the
54 * buffer being 'witness 000...000', which would be treated as
55 * witness * 2^n for some n. */
56 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
57 random->chunk_len );
58
59 random->pos += random->chunk_len;
60
61 return( 0 );
62}
Gilles Peskine2f780622020-11-25 15:37:20 +010063
64/* Random generator that is told how many bytes to return. */
65static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
66{
67 size_t *bytes_left = state;
68 size_t i;
69 for( i = 0; i < len; i++ )
70 {
71 if( *bytes_left == 0 )
72 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
73 buf[i] = *bytes_left & 0xff;
74 --( *bytes_left );
75 }
76 return( 0 );
77}
78
Paul Bakker33b43f12013-08-20 11:48:36 +020079/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000080
Paul Bakker33b43f12013-08-20 11:48:36 +020081/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020083 * END_DEPENDENCIES
84 */
Paul Bakker5690efc2011-05-26 13:16:06 +000085
Hanno Beckerb48e1aa2018-12-18 23:25:01 +000086/* BEGIN_CASE */
87void mpi_valid_param( )
88{
89 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
90}
91/* END_CASE */
92
Hanno Beckerafb607b2018-12-11 14:27:08 +000093/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
94void mpi_invalid_param( )
95{
96 mbedtls_mpi X;
97 const char *s_in = "00101000101010";
98 char s_out[16] = { 0 };
99 unsigned char u_out[16] = { 0 };
100 unsigned char u_in[16] = { 0 };
101 size_t olen;
102 mbedtls_mpi_uint mpi_uint;
103
104 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000105
Hanno Beckerafb607b2018-12-11 14:27:08 +0000106 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
107 mbedtls_mpi_grow( NULL, 42 ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
109 mbedtls_mpi_copy( NULL, &X ) );
110 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
111 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000112
Hanno Beckerafb607b2018-12-11 14:27:08 +0000113 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
114 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000115
Hanno Beckerafb607b2018-12-11 14:27:08 +0000116 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
117 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
119 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000120
Hanno Beckerafb607b2018-12-11 14:27:08 +0000121 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
122 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
123 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
124 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000125
Hanno Beckerafb607b2018-12-11 14:27:08 +0000126 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
127 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000128
Hanno Beckerafb607b2018-12-11 14:27:08 +0000129 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
130 mbedtls_mpi_get_bit( NULL, 42 ) );
131 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
132 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000133
Hanno Beckerafb607b2018-12-11 14:27:08 +0000134 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
135 mbedtls_mpi_read_string( NULL, 2, s_in ) );
136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
137 mbedtls_mpi_read_string( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000138
Hanno Beckerafb607b2018-12-11 14:27:08 +0000139 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
140 mbedtls_mpi_write_string( NULL, 2,
141 s_out, sizeof( s_out ),
142 &olen ) );
143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
144 mbedtls_mpi_write_string( &X, 2,
145 NULL, sizeof( s_out ),
146 &olen ) );
147 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
148 mbedtls_mpi_write_string( &X, 2,
149 s_out, sizeof( s_out ),
150 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000151
Hanno Beckerafb607b2018-12-11 14:27:08 +0000152#if defined(MBEDTLS_FS_IO)
153 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
154 mbedtls_mpi_read_file( NULL, 2, stdin ) );
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
156 mbedtls_mpi_read_file( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000157
Hanno Beckerafb607b2018-12-11 14:27:08 +0000158 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
159 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
160#endif /* MBEDTLS_FS_IO */
161
162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
163 mbedtls_mpi_read_binary( NULL, u_in,
164 sizeof( u_in ) ) );
165 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
166 mbedtls_mpi_read_binary( &X, NULL,
167 sizeof( u_in ) ) );
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_write_binary( NULL, u_out,
171 sizeof( u_out ) ) );
172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
173 mbedtls_mpi_write_binary( &X, NULL,
174 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000175
Hanno Beckerafb607b2018-12-11 14:27:08 +0000176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
177 mbedtls_mpi_shift_l( NULL, 42 ) );
178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
179 mbedtls_mpi_shift_r( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000180
Hanno Beckerafb607b2018-12-11 14:27:08 +0000181 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
182 mbedtls_mpi_cmp_abs( NULL, &X ) );
183 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
184 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000185
Hanno Beckerafb607b2018-12-11 14:27:08 +0000186 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
187 mbedtls_mpi_cmp_mpi( NULL, &X ) );
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
189 mbedtls_mpi_cmp_mpi( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000190
Hanno Beckerafb607b2018-12-11 14:27:08 +0000191 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
192 mbedtls_mpi_cmp_int( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000193
Hanno Beckerafb607b2018-12-11 14:27:08 +0000194 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
195 mbedtls_mpi_add_abs( NULL, &X, &X ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
197 mbedtls_mpi_add_abs( &X, NULL, &X ) );
198 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
199 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000200
Hanno Beckerafb607b2018-12-11 14:27:08 +0000201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
202 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
203 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
204 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
206 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000207
Hanno Beckerafb607b2018-12-11 14:27:08 +0000208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
209 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
211 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
212 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
213 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
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_mpi( NULL, &X, &X ) );
217 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
218 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
219 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
220 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000221
Hanno Beckerafb607b2018-12-11 14:27:08 +0000222 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
223 mbedtls_mpi_add_int( NULL, &X, 42 ) );
224 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
225 mbedtls_mpi_add_int( &X, NULL, 42 ) );
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_sub_int( NULL, &X, 42 ) );
229 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
230 mbedtls_mpi_sub_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_mul_mpi( NULL, &X, &X ) );
234 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
235 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
236 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
237 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000238
Hanno Beckerafb607b2018-12-11 14:27:08 +0000239 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
240 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
241 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
242 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000243
Hanno Beckerafb607b2018-12-11 14:27:08 +0000244 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
245 mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) );
246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
247 mbedtls_mpi_div_mpi( &X, &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_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000251
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000252 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
253
Hanno Beckerafb607b2018-12-11 14:27:08 +0000254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
255 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
259 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000260
Hanno Beckerafb607b2018-12-11 14:27:08 +0000261 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
262 mbedtls_mpi_mod_int( NULL, &X, 42 ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
264 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000265
Hanno Beckerafb607b2018-12-11 14:27:08 +0000266 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
267 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
269 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
271 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
272 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
273 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000274
Hanno Beckerafb607b2018-12-11 14:27:08 +0000275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
276 mbedtls_mpi_fill_random( NULL, 42, rnd_std_rand,
277 NULL ) );
278 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
279 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000280
Hanno Beckerafb607b2018-12-11 14:27:08 +0000281 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
282 mbedtls_mpi_gcd( NULL, &X, &X ) );
283 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
284 mbedtls_mpi_gcd( &X, NULL, &X ) );
285 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
286 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000287
Hanno Beckerafb607b2018-12-11 14:27:08 +0000288 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
289 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
291 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
292 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000293 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000294
295exit:
296 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000297}
298/* END_CASE */
299
Paul Bakker33b43f12013-08-20 11:48:36 +0200300/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100301void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200302{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200303 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200304
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200305 mbedtls_mpi_init( &X );
306 mbedtls_mpi_init( &Y );
307 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200308
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200309 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
310 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200311 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200312 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200313
314exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200315 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200316}
317/* END_CASE */
318
319/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100320void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
321 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200322 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000323{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000325 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100326 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000329
Janos Follath276284f2019-03-06 12:29:37 +0000330 memset( str, '!', sizeof( str ) );
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200333 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000334 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200335 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100336 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200337 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000338 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200339 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath276284f2019-03-06 12:29:37 +0000340 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000341 }
342 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000343
Paul Bakkerbd51b262014-07-10 15:26:12 +0200344exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000346}
Paul Bakker33b43f12013-08-20 11:48:36 +0200347/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000348
Paul Bakker33b43f12013-08-20 11:48:36 +0200349/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100350void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000353 unsigned char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100354 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000357
Paul Bakkere896fea2009-07-06 06:40:23 +0000358
Azim Khand30ca132017-06-09 04:32:58 +0100359 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200360 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100361 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200362 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000363
Paul Bakkerbd51b262014-07-10 15:26:12 +0200364exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000366}
Paul Bakker33b43f12013-08-20 11:48:36 +0200367/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000368
Paul Bakker33b43f12013-08-20 11:48:36 +0200369/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100370void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100371 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100372 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000375 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000376 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000377
378 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000381
Gilles Peskineb8e15342021-06-10 23:18:39 +0200382 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200385 if( buflen > (size_t) output_size )
386 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200389 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000390 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000391
Ronald Cron9fde3532020-06-10 11:42:32 +0200392 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
393 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000394 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000395
Paul Bakkerbd51b262014-07-10 15:26:12 +0200396exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000398}
Paul Bakker33b43f12013-08-20 11:48:36 +0200399/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100402void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100403 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000406 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000407 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000408 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000409 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000410
411 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000414
Paul Bakker33b43f12013-08-20 11:48:36 +0200415 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200416 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000418 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000419 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000420
Paul Bakker33b43f12013-08-20 11:48:36 +0200421 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000422 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200423 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 buflen = mbedtls_mpi_size( &X );
425 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000426
Paul Bakkere896fea2009-07-06 06:40:23 +0000427
Ronald Cron9fde3532020-06-10 11:42:32 +0200428 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
429 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000430 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000431
Paul Bakkerbd51b262014-07-10 15:26:12 +0200432exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000434}
Paul Bakker33b43f12013-08-20 11:48:36 +0200435/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100438void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
439 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000440{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000442 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200443 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000446
Gilles Peskineb8e15342021-06-10 23:18:39 +0200447 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000448
Paul Bakker33b43f12013-08-20 11:48:36 +0200449 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000450 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200451 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000452 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200453 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000454
Paul Bakker33b43f12013-08-20 11:48:36 +0200455 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000456 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200457 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000458 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200459 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000462
Paul Bakkerbd51b262014-07-10 15:26:12 +0200463exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000465}
Paul Bakker33b43f12013-08-20 11:48:36 +0200466/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000467
Paul Bakker33b43f12013-08-20 11:48:36 +0200468/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100469void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_mpi X;
472 mbedtls_mpi_init( &X );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200473 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000475
Paul Bakkerbd51b262014-07-10 15:26:12 +0200476exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000478}
Paul Bakker33b43f12013-08-20 11:48:36 +0200479/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000480
Paul Bakker33b43f12013-08-20 11:48:36 +0200481/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100482void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
483 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_mpi X, Y;
486 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000487
Gilles Peskineb8e15342021-06-10 23:18:39 +0200488 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
489 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100490 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
491
492 if( result == 0 )
493 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200494 TEST_ASSERT( sign_is_valid( &X ) );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100495 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
496 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000497
Paul Bakkerbd51b262014-07-10 15:26:12 +0200498exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000500}
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000502
Paul Bakker33b43f12013-08-20 11:48:36 +0200503/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100504void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000505{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_mpi X;
507 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000508
Gilles Peskineb8e15342021-06-10 23:18:39 +0200509 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000511
Paul Bakkerbd51b262014-07-10 15:26:12 +0200512exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000514}
Paul Bakker33b43f12013-08-20 11:48:36 +0200515/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000516
Paul Bakker33b43f12013-08-20 11:48:36 +0200517/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100518void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_mpi X;
521 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000522
Gilles Peskineb8e15342021-06-10 23:18:39 +0200523 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200524 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
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( &X );
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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100532void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
533 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_mpi A, X, Y, Z;
536 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000537
Gilles Peskineb8e15342021-06-10 23:18:39 +0200538 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
539 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
540 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200542 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000544
Paul Bakkerbd51b262014-07-10 15:26:12 +0200545exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000547}
Paul Bakker33b43f12013-08-20 11:48:36 +0200548/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000549
Paul Bakker33b43f12013-08-20 11:48:36 +0200550/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_mpi X;
554 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
557 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000558
Paul Bakkerbd51b262014-07-10 15:26:12 +0200559exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000561}
Paul Bakker33b43f12013-08-20 11:48:36 +0200562/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000563
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100565void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
566 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi X, Y;
569 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000570
Gilles Peskineb8e15342021-06-10 23:18:39 +0200571 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
572 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000574
Paul Bakkerbd51b262014-07-10 15:26:12 +0200575exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000577}
Paul Bakker33b43f12013-08-20 11:48:36 +0200578/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000579
Paul Bakker33b43f12013-08-20 11:48:36 +0200580/* BEGIN_CASE */
Janos Follath27d221a2019-10-14 09:21:49 +0100581void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
582 int size_Y, char * input_Y,
Janos Follath867a3ab2019-10-11 14:21:53 +0100583 int input_ret, int input_err )
Janos Follathe9ae6302019-09-11 16:07:14 +0100584{
Gilles Peskine319ecf32020-09-02 15:18:07 +0200585 unsigned ret = -1;
Janos Follath867a3ab2019-10-11 14:21:53 +0100586 unsigned input_uret = input_ret;
Janos Follathe9ae6302019-09-11 16:07:14 +0100587 mbedtls_mpi X, Y;
588 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
589
Gilles Peskineb8e15342021-06-10 23:18:39 +0200590 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 );
591 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, input_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100592
Gilles Peskine1a30fbb2020-01-21 16:30:53 +0100593 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
594 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100595
Janos Follath867a3ab2019-10-11 14:21:53 +0100596 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follathe9ae6302019-09-11 16:07:14 +0100597 if( input_err == 0 )
Janos Follath867a3ab2019-10-11 14:21:53 +0100598 TEST_ASSERT( ret == input_uret );
Janos Follathe9ae6302019-09-11 16:07:14 +0100599
600exit:
601 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
602}
603/* END_CASE */
604
605/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100606void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
607 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_mpi X, Y;
610 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000611
Gilles Peskineb8e15342021-06-10 23:18:39 +0200612 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
613 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000615
Paul Bakkerbd51b262014-07-10 15:26:12 +0200616exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000618}
Paul Bakker33b43f12013-08-20 11:48:36 +0200619/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000620
Paul Bakker33b43f12013-08-20 11:48:36 +0200621/* BEGIN_CASE */
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200622void mbedtls_mpi_copy( char *src_hex, char *dst_hex )
Paul Bakker367dae42009-06-28 21:50:27 +0000623{
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200624 mbedtls_mpi src, dst;
625 mbedtls_mpi_init( &src );
626 mbedtls_mpi_init( &dst );
Paul Bakker367dae42009-06-28 21:50:27 +0000627
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200628 TEST_ASSERT( mbedtls_test_read_mpi( &src, 16, src_hex ) == 0 );
629 TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100630
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200631 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
632
633 TEST_ASSERT( sign_is_valid( &dst ) );
634 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000635
Paul Bakkerbd51b262014-07-10 15:26:12 +0200636exit:
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200637 mbedtls_mpi_free( &src );
638 mbedtls_mpi_free( &dst );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100639}
640/* END_CASE */
641
642/* BEGIN_CASE */
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200643void mpi_copy_self( char *input_X )
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100644{
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200645 mbedtls_mpi X, A;
646 mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000648
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200649 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200651
652 TEST_ASSERT( mbedtls_test_read_mpi( &A, 16, input_X ) == 0 );
653 TEST_ASSERT( sign_is_valid( &X ) );
654 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000655
Paul Bakkerbd51b262014-07-10 15:26:12 +0200656exit:
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200657 mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000659}
Paul Bakker33b43f12013-08-20 11:48:36 +0200660/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000661
Paul Bakker33b43f12013-08-20 11:48:36 +0200662/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100664{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_mpi X;
666 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100669 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
671 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100672 TEST_ASSERT( X.n == (size_t) after );
673
Paul Bakkerbd51b262014-07-10 15:26:12 +0200674exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100676}
677/* END_CASE */
678
679/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100680void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
681 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100682{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_mpi X, Y, XX;
684 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100685
Gilles Peskineb8e15342021-06-10 23:18:39 +0200686 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100687 X.s = x_sign;
Gilles Peskineb8e15342021-06-10 23:18:39 +0200688 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100689 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200693 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200697 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100699
Paul Bakkerbd51b262014-07-10 15:26:12 +0200700exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100702}
703/* END_CASE */
704
705/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100706void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
707 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
712 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100713
Gilles Peskineb8e15342021-06-10 23:18:39 +0200714 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100715 X.s = x_sign;
Gilles Peskineb8e15342021-06-10 23:18:39 +0200716 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100717 Y.s = y_sign;
718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
720 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200723 TEST_ASSERT( sign_is_valid( &X ) );
724 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
726 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200729 TEST_ASSERT( sign_is_valid( &X ) );
730 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
732 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100733
Paul Bakkerbd51b262014-07-10 15:26:12 +0200734exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
736 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100737}
738/* END_CASE */
739
740/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100741void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000742{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100743 mbedtls_mpi X, Y;
744 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
747 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100748 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
749 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200752 TEST_ASSERT( sign_is_valid( &X ) );
753 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100754 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
755 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000756
Paul Bakkerbd51b262014-07-10 15:26:12 +0200757exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100758 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
759}
760/* END_CASE */
761
762/* BEGIN_CASE */
763void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
764{
765 mbedtls_mpi X, Y, X0, Y0;
766 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
767 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
768
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100769 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
770 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
771 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
772 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100773
774 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200775 TEST_ASSERT( sign_is_valid( &X ) );
776 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100777 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
778 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
779
780exit:
781 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
782 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
783}
784/* END_CASE */
785
786/* BEGIN_CASE */
787void mpi_swap_self( data_t *input_X )
788{
789 mbedtls_mpi X, X0;
790 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
791
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100792 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
793 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100794
795 mbedtls_mpi_swap( &X, &X );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200796 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100797 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
798
799exit:
800 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
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( int radix_X, char * input_X, int radix_Y,
806 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000807{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_mpi X, Y, Z, A;
809 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000810
Gilles Peskineb8e15342021-06-10 23:18:39 +0200811 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
812 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
813 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200815 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000817
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200818 /* result == first operand */
819 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200820 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200821 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200822 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200823
824 /* result == second operand */
825 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200826 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200827 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
828
Paul Bakkerbd51b262014-07-10 15:26:12 +0200829exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000831}
Paul Bakker33b43f12013-08-20 11:48:36 +0200832/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000833
Paul Bakker33b43f12013-08-20 11:48:36 +0200834/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100835void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
836 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100837{
838 mbedtls_mpi X, A;
839 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
840
Gilles Peskineb8e15342021-06-10 23:18:39 +0200841 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100842
Gilles Peskineb8e15342021-06-10 23:18:39 +0200843 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100844 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
845 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200846 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100847
Gilles Peskineb8e15342021-06-10 23:18:39 +0200848 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100849 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200850 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100851 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
852
Gilles Peskineb8e15342021-06-10 23:18:39 +0200853 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100854 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200855 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +0100856 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
857
858exit:
859 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
860}
861/* END_CASE */
862
863
864/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100865void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
866 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000867{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_mpi X, Y, Z, A;
869 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000870
Gilles Peskineb8e15342021-06-10 23:18:39 +0200871 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
872 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
873 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200875 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000877
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200878 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200880 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200882 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200883
884 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200886 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000888
Paul Bakkerbd51b262014-07-10 15:26:12 +0200889exit:
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200890 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000891}
Paul Bakker33b43f12013-08-20 11:48:36 +0200892/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000893
Paul Bakker33b43f12013-08-20 11:48:36 +0200894/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100895void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
896 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_mpi X, Z, A;
899 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000900
Gilles Peskineb8e15342021-06-10 23:18:39 +0200901 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
902 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200904 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000906
Paul Bakkerbd51b262014-07-10 15:26:12 +0200907exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000909}
Paul Bakker33b43f12013-08-20 11:48:36 +0200910/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000911
Paul Bakker33b43f12013-08-20 11:48:36 +0200912/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100913void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
914 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_mpi X, Y, Z, A;
917 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000918
Gilles Peskineb8e15342021-06-10 23:18:39 +0200919 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
920 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
921 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200923 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000925
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200926 /* result == first operand */
927 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200928 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200929 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200930 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200931
932 /* result == second operand */
933 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200934 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200935 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
936
Paul Bakkerbd51b262014-07-10 15:26:12 +0200937exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000939}
Paul Bakker33b43f12013-08-20 11:48:36 +0200940/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000941
Paul Bakker33b43f12013-08-20 11:48:36 +0200942/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100943void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
944 char * input_Y, int radix_A, char * input_A,
945 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000946{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000948 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000950
Gilles Peskineb8e15342021-06-10 23:18:39 +0200951 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
952 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
953 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200956 TEST_ASSERT( res == sub_result );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200957 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +0000958 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000960
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200961 /* result == first operand */
962 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200963 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200964 if( sub_result == 0 )
965 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200966 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200967
968 /* result == second operand */
969 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200970 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200971 if( sub_result == 0 )
972 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
973
Paul Bakkerbd51b262014-07-10 15:26:12 +0200974exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000976}
Paul Bakker33b43f12013-08-20 11:48:36 +0200977/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000978
Paul Bakker33b43f12013-08-20 11:48:36 +0200979/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100980void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
981 int radix_A, char * input_A )
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
Gilles Peskineb8e15342021-06-10 23:18:39 +0200986 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
987 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200989 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000991
Paul Bakkerbd51b262014-07-10 15:26:12 +0200992exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000994}
Paul Bakker33b43f12013-08-20 11:48:36 +0200995/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000996
Paul Bakker33b43f12013-08-20 11:48:36 +0200997/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100998void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
999 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001000{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_mpi X, Y, Z, A;
1002 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001003
Gilles Peskineb8e15342021-06-10 23:18:39 +02001004 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1005 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1006 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001008 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001010
Paul Bakkerbd51b262014-07-10 15:26:12 +02001011exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001013}
Paul Bakker33b43f12013-08-20 11:48:36 +02001014/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001015
Paul Bakker33b43f12013-08-20 11:48:36 +02001016/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001017void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1018 int radix_A, char * input_A,
1019 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_mpi X, Z, A;
1022 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001023
Gilles Peskineb8e15342021-06-10 23:18:39 +02001024 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1025 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001027 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001028 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001030 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001032 else
1033 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001034
Paul Bakkerbd51b262014-07-10 15:26:12 +02001035exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001037}
Paul Bakker33b43f12013-08-20 11:48:36 +02001038/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001039
Paul Bakker33b43f12013-08-20 11:48:36 +02001040/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001041void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1042 char * input_Y, int radix_A, char * input_A,
1043 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001044{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001046 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1048 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001049
Gilles Peskineb8e15342021-06-10 23:18:39 +02001050 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1051 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1052 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1053 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001055 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001056 if( res == 0 )
1057 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001058 TEST_ASSERT( sign_is_valid( &Q ) );
1059 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1061 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001062 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001063
Paul Bakkerbd51b262014-07-10 15:26:12 +02001064exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1066 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001067}
Paul Bakker33b43f12013-08-20 11:48:36 +02001068/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001069
Paul Bakker33b43f12013-08-20 11:48:36 +02001070/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001071void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1072 int radix_A, char * input_A, int radix_B,
1073 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001076 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1078 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001079
Gilles Peskineb8e15342021-06-10 23:18:39 +02001080 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1081 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1082 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001084 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001085 if( res == 0 )
1086 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001087 TEST_ASSERT( sign_is_valid( &Q ) );
1088 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1090 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001091 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001092
Paul Bakkerbd51b262014-07-10 15:26:12 +02001093exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1095 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001096}
Paul Bakker33b43f12013-08-20 11:48:36 +02001097/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001098
Paul Bakker33b43f12013-08-20 11:48:36 +02001099/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001100void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1101 char * input_Y, int radix_A, char * input_A,
1102 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001105 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001107
Gilles Peskineb8e15342021-06-10 23:18:39 +02001108 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1109 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1110 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001112 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001113 if( res == 0 )
1114 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001115 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001117 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001118
Paul Bakkerbd51b262014-07-10 15:26:12 +02001119exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001121}
Paul Bakker33b43f12013-08-20 11:48:36 +02001122/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001123
Paul Bakker33b43f12013-08-20 11:48:36 +02001124/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001125void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1126 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001129 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_mpi_uint r;
1131 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001132
Gilles Peskineb8e15342021-06-10 23:18:39 +02001133 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001135 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001136 if( res == 0 )
1137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001139 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001140
Paul Bakkerbd51b262014-07-10 15:26:12 +02001141exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001143}
Paul Bakker33b43f12013-08-20 11:48:36 +02001144/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001145
Paul Bakker33b43f12013-08-20 11:48:36 +02001146/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001147void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1148 char * input_E, int radix_N, char * input_N,
1149 int radix_RR, char * input_RR, int radix_X,
1150 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001153 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1155 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001156
Gilles Peskineb8e15342021-06-10 23:18:39 +02001157 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1158 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
1159 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
1160 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001161
Paul Bakker33b43f12013-08-20 11:48:36 +02001162 if( strlen( input_RR ) )
Gilles Peskineb8e15342021-06-10 23:18:39 +02001163 TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001166 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001167 if( res == 0 )
1168 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001169 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001171 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001172
Paul Bakkerbd51b262014-07-10 15:26:12 +02001173exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1175 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001176}
Paul Bakker33b43f12013-08-20 11:48:36 +02001177/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001178
Paul Bakker33b43f12013-08-20 11:48:36 +02001179/* BEGIN_CASE */
Chris Jones415c7be2020-12-02 10:41:50 +00001180void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesa18813e2020-12-03 11:35:41 +00001181 int radix_RR, char * input_RR, int exp_result )
Chris Jones415c7be2020-12-02 10:41:50 +00001182{
1183 mbedtls_mpi A, E, N, RR, Z;
1184 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1185 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1186
Chris Jonesa18813e2020-12-03 11:35:41 +00001187 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jones415c7be2020-12-02 10:41:50 +00001188 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001189 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001190 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001191
1192 /* Set E to 2^(E_bytes - 1) + 1 */
1193 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1194 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001195 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001196
1197 /* Set N to 2^(N_bytes - 1) + 1 */
1198 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1199 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001200 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1201
1202 if( strlen( input_RR ) )
Gilles Peskineb8e15342021-06-10 23:18:39 +02001203 TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001204
Chris Jonesa18813e2020-12-03 11:35:41 +00001205 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jones415c7be2020-12-02 10:41:50 +00001206
1207exit:
1208 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1209 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1210}
1211/* END_CASE */
1212
1213/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001214void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1215 char * input_Y, int radix_A, char * input_A,
1216 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001217{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001219 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001221
Gilles Peskineb8e15342021-06-10 23:18:39 +02001222 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1223 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1224 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001226 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001227 if( res == 0 )
1228 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001229 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001231 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001232
Paul Bakkerbd51b262014-07-10 15:26:12 +02001233exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001235}
Paul Bakker33b43f12013-08-20 11:48:36 +02001236/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001239void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001242 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001244
Gilles Peskineb8e15342021-06-10 23:18:39 +02001245 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001246 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001247 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001248
Paul Bakkerbd51b262014-07-10 15:26:12 +02001249exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001251}
Paul Bakker33b43f12013-08-20 11:48:36 +02001252/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001255void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001256 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001257{
1258 mbedtls_mpi X;
1259 int res;
1260 mbedtls_test_mpi_random rand;
1261
1262 mbedtls_mpi_init( &X );
1263 rand.data = witnesses;
1264 rand.pos = 0;
1265 rand.chunk_len = chunk_len;
1266
1267 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001268 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1269 mbedtls_test_mpi_miller_rabin_determinizer,
1270 &rand );
1271 TEST_ASSERT( res == 0 );
1272
1273 rand.data = witnesses;
1274 rand.pos = 0;
1275 rand.chunk_len = chunk_len;
1276
Janos Follatha0b67c22018-09-18 14:48:23 +01001277 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1278 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001279 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001280 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001281
1282exit:
1283 mbedtls_mpi_free( &X );
1284}
1285/* END_CASE */
1286
1287/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001288void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001289{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001291 int my_ret;
1292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001294
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001295 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001296 TEST_ASSERT( my_ret == ref_ret );
1297
1298 if( ref_ret == 0 )
1299 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001300 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001301
1302 TEST_ASSERT( actual_bits >= (size_t) bits );
1303 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001304 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001305
Janos Follatha0b67c22018-09-18 14:48:23 +01001306 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1307 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001308 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001309 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001310 /* X = ( X - 1 ) / 2 */
1311 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001312 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1313 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001314 }
1315 }
1316
Paul Bakkerbd51b262014-07-10 15:26:12 +02001317exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001319}
1320/* END_CASE */
1321
Paul Bakker33b43f12013-08-20 11:48:36 +02001322/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001323void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1324 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001325{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_mpi X, A;
1327 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001328
Gilles Peskineb8e15342021-06-10 23:18:39 +02001329 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1330 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001332 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001334
Paul Bakkerbd51b262014-07-10 15:26:12 +02001335exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001337}
Paul Bakker33b43f12013-08-20 11:48:36 +02001338/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001339
Paul Bakker33b43f12013-08-20 11:48:36 +02001340/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001341void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1342 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001343{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 mbedtls_mpi X, A;
1345 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001346
Gilles Peskineb8e15342021-06-10 23:18:39 +02001347 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1348 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001350 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001352
Paul Bakkerbd51b262014-07-10 15:26:12 +02001353exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001355}
Paul Bakker33b43f12013-08-20 11:48:36 +02001356/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001357
Gilles Peskine2f780622020-11-25 15:37:20 +01001358/* BEGIN_CASE */
1359void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1360{
1361 mbedtls_mpi X;
1362 int ret;
1363 size_t bytes_left = rng_bytes;
1364 mbedtls_mpi_init( &X );
1365
1366 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1367 f_rng_bytes_left, &bytes_left );
1368 TEST_ASSERT( ret == expected_ret );
1369
1370 if( expected_ret == 0 )
1371 {
1372 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1373 * as a big-endian representation of the number. We know when
1374 * our RNG function returns null bytes, so we know how many
1375 * leading zero bytes the number has. */
1376 size_t leading_zeros = 0;
1377 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1378 leading_zeros = 1;
1379 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1380 (size_t) wanted_bytes );
1381 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001382 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2f780622020-11-25 15:37:20 +01001383 }
1384
1385exit:
1386 mbedtls_mpi_free( &X );
1387}
1388/* END_CASE */
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001391void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001392{
Andres AG93012e82016-09-09 09:10:28 +01001393 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001394}
Paul Bakker33b43f12013-08-20 11:48:36 +02001395/* END_CASE */