blob: 095c8eed7dff8c3d6159b9e05b1fb90bd659e114 [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 Peskine4cbb1c92021-06-10 23:00:33 +0200624 mbedtls_mpi src, dst, ref;
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200625 mbedtls_mpi_init( &src );
626 mbedtls_mpi_init( &dst );
Gilles Peskine4cbb1c92021-06-10 23:00:33 +0200627 mbedtls_mpi_init( &ref );
Paul Bakker367dae42009-06-28 21:50:27 +0000628
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200629 TEST_ASSERT( mbedtls_test_read_mpi( &src, 16, src_hex ) == 0 );
Gilles Peskine4cbb1c92021-06-10 23:00:33 +0200630 TEST_ASSERT( mbedtls_test_read_mpi( &ref, 16, dst_hex ) == 0 );
631
632 /* mbedtls_mpi_copy() */
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200633 TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200634 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200635 TEST_ASSERT( sign_is_valid( &dst ) );
636 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000637
Gilles Peskine4cbb1c92021-06-10 23:00:33 +0200638 /* mbedtls_mpi_safe_cond_assign(), assignment done */
639 mbedtls_mpi_free( &dst );
640 TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
641 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
642 TEST_ASSERT( sign_is_valid( &dst ) );
643 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
644
645 /* mbedtls_mpi_safe_cond_assign(), assignment not done */
646 mbedtls_mpi_free( &dst );
647 TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
648 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
649 TEST_ASSERT( sign_is_valid( &dst ) );
650 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
651
Paul Bakkerbd51b262014-07-10 15:26:12 +0200652exit:
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200653 mbedtls_mpi_free( &src );
654 mbedtls_mpi_free( &dst );
Gilles Peskine4cbb1c92021-06-10 23:00:33 +0200655 mbedtls_mpi_free( &ref );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100656}
657/* END_CASE */
658
659/* BEGIN_CASE */
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200660void mpi_copy_self( char *input_X )
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100661{
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200662 mbedtls_mpi X, A;
663 mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000665
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200666 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200668
669 TEST_ASSERT( mbedtls_test_read_mpi( &A, 16, input_X ) == 0 );
670 TEST_ASSERT( sign_is_valid( &X ) );
671 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000672
Paul Bakkerbd51b262014-07-10 15:26:12 +0200673exit:
Gilles Peskine8e1aa662021-06-10 15:17:30 +0200674 mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000676}
Paul Bakker33b43f12013-08-20 11:48:36 +0200677/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000678
Paul Bakker33b43f12013-08-20 11:48:36 +0200679/* BEGIN_CASE */
Gilles Peskine1e914262021-06-10 22:29:57 +0200680void mbedtls_mpi_swap( char *X_hex, char *Y_hex )
681{
682 mbedtls_mpi X, Y, X0, Y0;
683 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
684 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
685
Gilles Peskine1e914262021-06-10 22:29:57 +0200686 TEST_ASSERT( mbedtls_test_read_mpi( &X0, 16, X_hex ) == 0 );
687 TEST_ASSERT( mbedtls_test_read_mpi( &Y0, 16, Y_hex ) == 0 );
688
Gilles Peskine4cbb1c92021-06-10 23:00:33 +0200689 /* mbedtls_mpi_swap() */
690 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
691 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 );
Gilles Peskine1e914262021-06-10 22:29:57 +0200692 mbedtls_mpi_swap( &X, &Y );
693 TEST_ASSERT( sign_is_valid( &X ) );
694 TEST_ASSERT( sign_is_valid( &Y ) );
695 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
696 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
697
Gilles Peskine4cbb1c92021-06-10 23:00:33 +0200698 /* mbedtls_mpi_safe_cond_swap(), swap done */
699 mbedtls_mpi_free( &X );
700 mbedtls_mpi_free( &Y );
701 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
702 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 );
703 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
704 TEST_ASSERT( sign_is_valid( &X ) );
705 TEST_ASSERT( sign_is_valid( &Y ) );
706 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
707 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
708
709 /* mbedtls_mpi_safe_cond_swap(), swap not done */
710 mbedtls_mpi_free( &X );
711 mbedtls_mpi_free( &Y );
712 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
713 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 );
714 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
715 TEST_ASSERT( sign_is_valid( &X ) );
716 TEST_ASSERT( sign_is_valid( &Y ) );
717 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
718 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
719
Gilles Peskine1e914262021-06-10 22:29:57 +0200720exit:
721 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
722 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
723}
724/* END_CASE */
725
726/* BEGIN_CASE */
727void mpi_swap_self( char *X_hex )
728{
729 mbedtls_mpi X, X0;
730 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
731
732 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
733 TEST_ASSERT( mbedtls_test_read_mpi( &X0, 16, X_hex ) == 0 );
734
735 mbedtls_mpi_swap( &X, &X );
736 TEST_ASSERT( sign_is_valid( &X ) );
737 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
738
739exit:
740 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
741}
742/* END_CASE */
743
744/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100746{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_mpi X;
748 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Gilles Peskine266275e2021-06-15 21:19:18 +0200751 if( used > 0 )
752 {
753 size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint );
754 TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 );
755 }
756 TEST_EQUAL( X.n, (size_t) before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Gilles Peskine266275e2021-06-15 21:19:18 +0200758 TEST_EQUAL( X.n, (size_t) after );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100759
Paul Bakkerbd51b262014-07-10 15:26:12 +0200760exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100762}
763/* END_CASE */
764
765/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100766void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
767 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000768{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_mpi X, Y, Z, A;
770 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000771
Gilles Peskineb8e15342021-06-10 23:18:39 +0200772 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
773 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
774 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200776 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000778
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200779 /* result == first operand */
780 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200781 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200782 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200783 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200784
785 /* result == second operand */
786 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200787 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200788 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
789
Paul Bakkerbd51b262014-07-10 15:26:12 +0200790exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000792}
Paul Bakker33b43f12013-08-20 11:48:36 +0200793/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000794
Paul Bakker33b43f12013-08-20 11:48:36 +0200795/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100796void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
797 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100798{
799 mbedtls_mpi X, A;
800 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
801
Gilles Peskineb8e15342021-06-10 23:18:39 +0200802 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100803
Gilles Peskineb8e15342021-06-10 23:18:39 +0200804 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100805 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
806 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200807 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100808
Gilles Peskineb8e15342021-06-10 23:18:39 +0200809 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100810 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200811 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100812 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
813
Gilles Peskineb8e15342021-06-10 23:18:39 +0200814 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100815 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200816 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +0100817 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
818
819exit:
820 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
821}
822/* END_CASE */
823
824
825/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100826void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
827 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000828{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_mpi X, Y, Z, A;
830 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000831
Gilles Peskineb8e15342021-06-10 23:18:39 +0200832 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
833 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
834 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200836 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000838
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200839 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200841 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200843 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200844
845 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200847 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000849
Paul Bakkerbd51b262014-07-10 15:26:12 +0200850exit:
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200851 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000852}
Paul Bakker33b43f12013-08-20 11:48:36 +0200853/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000854
Paul Bakker33b43f12013-08-20 11:48:36 +0200855/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100856void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
857 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000858{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_mpi X, Z, A;
860 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000861
Gilles Peskineb8e15342021-06-10 23:18:39 +0200862 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
863 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200865 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000867
Paul Bakkerbd51b262014-07-10 15:26:12 +0200868exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000870}
Paul Bakker33b43f12013-08-20 11:48:36 +0200871/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000872
Paul Bakker33b43f12013-08-20 11:48:36 +0200873/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100874void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
875 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000876{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_mpi X, Y, Z, A;
878 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000879
Gilles Peskineb8e15342021-06-10 23:18:39 +0200880 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
881 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
882 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200884 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000886
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200887 /* result == first operand */
888 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200889 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200890 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200891 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200892
893 /* result == second operand */
894 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200895 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200896 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
897
Paul Bakkerbd51b262014-07-10 15:26:12 +0200898exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000900}
Paul Bakker33b43f12013-08-20 11:48:36 +0200901/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000902
Paul Bakker33b43f12013-08-20 11:48:36 +0200903/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100904void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
905 char * input_Y, int radix_A, char * input_A,
906 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000909 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000911
Gilles Peskineb8e15342021-06-10 23:18:39 +0200912 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
913 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
914 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200917 TEST_ASSERT( res == sub_result );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200918 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +0000919 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000921
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200922 /* result == first operand */
923 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200924 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200925 if( sub_result == 0 )
926 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200927 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200928
929 /* result == second operand */
930 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200931 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200932 if( sub_result == 0 )
933 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
934
Paul Bakkerbd51b262014-07-10 15:26:12 +0200935exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000937}
Paul Bakker33b43f12013-08-20 11:48:36 +0200938/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000939
Paul Bakker33b43f12013-08-20 11:48:36 +0200940/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100941void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
942 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000943{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_mpi X, Z, A;
945 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000946
Gilles Peskineb8e15342021-06-10 23:18:39 +0200947 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
948 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200950 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000952
Paul Bakkerbd51b262014-07-10 15:26:12 +0200953exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000955}
Paul Bakker33b43f12013-08-20 11:48:36 +0200956/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000957
Paul Bakker33b43f12013-08-20 11:48:36 +0200958/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100959void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
960 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_mpi X, Y, Z, A;
963 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000964
Gilles Peskineb8e15342021-06-10 23:18:39 +0200965 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
966 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
967 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200969 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000971
Paul Bakkerbd51b262014-07-10 15:26:12 +0200972exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000974}
Paul Bakker33b43f12013-08-20 11:48:36 +0200975/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000976
Paul Bakker33b43f12013-08-20 11:48:36 +0200977/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100978void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
979 int radix_A, char * input_A,
980 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 mbedtls_mpi X, Z, A;
983 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000984
Gilles Peskineb8e15342021-06-10 23:18:39 +0200985 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
986 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200988 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200989 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200991 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200993 else
994 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000995
Paul Bakkerbd51b262014-07-10 15:26:12 +0200996exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000998}
Paul Bakker33b43f12013-08-20 11:48:36 +0200999/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001000
Paul Bakker33b43f12013-08-20 11:48:36 +02001001/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001002void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1003 char * input_Y, int radix_A, char * input_A,
1004 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001005{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001007 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1009 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001010
Gilles Peskineb8e15342021-06-10 23:18:39 +02001011 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1012 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1013 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1014 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001016 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001017 if( res == 0 )
1018 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001019 TEST_ASSERT( sign_is_valid( &Q ) );
1020 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1022 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001023 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001024
Paul Bakkerbd51b262014-07-10 15:26:12 +02001025exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1027 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001028}
Paul Bakker33b43f12013-08-20 11:48:36 +02001029/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001030
Paul Bakker33b43f12013-08-20 11:48:36 +02001031/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001032void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1033 int radix_A, char * input_A, int radix_B,
1034 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001035{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001037 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1039 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001040
Gilles Peskineb8e15342021-06-10 23:18:39 +02001041 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1042 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1043 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001045 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001046 if( res == 0 )
1047 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001048 TEST_ASSERT( sign_is_valid( &Q ) );
1049 TEST_ASSERT( sign_is_valid( &R ) );
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 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001076 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001078 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001079
Paul Bakkerbd51b262014-07-10 15:26:12 +02001080exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001082}
Paul Bakker33b43f12013-08-20 11:48:36 +02001083/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001084
Paul Bakker33b43f12013-08-20 11:48:36 +02001085/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001086void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1087 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001090 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_mpi_uint r;
1092 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001093
Gilles Peskineb8e15342021-06-10 23:18:39 +02001094 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001096 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001097 if( res == 0 )
1098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001100 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001101
Paul Bakkerbd51b262014-07-10 15:26:12 +02001102exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001104}
Paul Bakker33b43f12013-08-20 11:48:36 +02001105/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001106
Paul Bakker33b43f12013-08-20 11:48:36 +02001107/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001108void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1109 char * input_E, int radix_N, char * input_N,
Gilles Peskinecca6bb92021-06-09 18:28:35 +02001110 int radix_X, 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
Gilles Peskineefc3fd42021-06-09 18:31:35 +02001122 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
1123 TEST_ASSERT( res == div_result );
1124 if( res == 0 )
1125 {
1126 TEST_ASSERT( sign_is_valid( &Z ) );
1127 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1128 }
1129
1130 /* Now test again with the speed-up parameter supplied as an output. */
1131 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
1132 TEST_ASSERT( res == div_result );
1133 if( res == 0 )
1134 {
1135 TEST_ASSERT( sign_is_valid( &Z ) );
1136 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1137 }
1138
1139 /* Now test again with the speed-up parameter supplied in calculated form. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001141 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001142 if( res == 0 )
1143 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001144 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001146 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001147
Paul Bakkerbd51b262014-07-10 15:26:12 +02001148exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1150 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001151}
Paul Bakker33b43f12013-08-20 11:48:36 +02001152/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001153
Paul Bakker33b43f12013-08-20 11:48:36 +02001154/* BEGIN_CASE */
Chris Jones415c7be2020-12-02 10:41:50 +00001155void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesa18813e2020-12-03 11:35:41 +00001156 int radix_RR, char * input_RR, int exp_result )
Chris Jones415c7be2020-12-02 10:41:50 +00001157{
1158 mbedtls_mpi A, E, N, RR, Z;
1159 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1160 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1161
Chris Jonesa18813e2020-12-03 11:35:41 +00001162 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jones415c7be2020-12-02 10:41:50 +00001163 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001164 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001165 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001166
1167 /* Set E to 2^(E_bytes - 1) + 1 */
1168 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1169 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001170 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001171
1172 /* Set N to 2^(N_bytes - 1) + 1 */
1173 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1174 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001175 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1176
1177 if( strlen( input_RR ) )
Gilles Peskineb8e15342021-06-10 23:18:39 +02001178 TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001179
Chris Jonesa18813e2020-12-03 11:35:41 +00001180 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jones415c7be2020-12-02 10:41:50 +00001181
1182exit:
1183 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1184 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1185}
1186/* END_CASE */
1187
1188/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001189void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1190 char * input_Y, int radix_A, char * input_A,
1191 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001194 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001196
Gilles Peskineb8e15342021-06-10 23:18:39 +02001197 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1198 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1199 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001201 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001202 if( res == 0 )
1203 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001204 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001206 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001207
Paul Bakkerbd51b262014-07-10 15:26:12 +02001208exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001210}
Paul Bakker33b43f12013-08-20 11:48:36 +02001211/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001214void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001217 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001219
Gilles Peskineb8e15342021-06-10 23:18:39 +02001220 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001221 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001222 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001223
Paul Bakkerbd51b262014-07-10 15:26:12 +02001224exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001226}
Paul Bakker33b43f12013-08-20 11:48:36 +02001227/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001230void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001231 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001232{
1233 mbedtls_mpi X;
1234 int res;
1235 mbedtls_test_mpi_random rand;
1236
1237 mbedtls_mpi_init( &X );
1238 rand.data = witnesses;
1239 rand.pos = 0;
1240 rand.chunk_len = chunk_len;
1241
1242 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001243 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1244 mbedtls_test_mpi_miller_rabin_determinizer,
1245 &rand );
1246 TEST_ASSERT( res == 0 );
1247
1248 rand.data = witnesses;
1249 rand.pos = 0;
1250 rand.chunk_len = chunk_len;
1251
Janos Follatha0b67c22018-09-18 14:48:23 +01001252 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1253 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001254 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001255 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001256
1257exit:
1258 mbedtls_mpi_free( &X );
1259}
1260/* END_CASE */
1261
1262/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001263void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001264{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001266 int my_ret;
1267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001269
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001270 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001271 TEST_ASSERT( my_ret == ref_ret );
1272
1273 if( ref_ret == 0 )
1274 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001275 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001276
1277 TEST_ASSERT( actual_bits >= (size_t) bits );
1278 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001279 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001280
Janos Follatha0b67c22018-09-18 14:48:23 +01001281 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1282 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001283 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001284 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001285 /* X = ( X - 1 ) / 2 */
1286 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001287 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1288 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001289 }
1290 }
1291
Paul Bakkerbd51b262014-07-10 15:26:12 +02001292exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001294}
1295/* END_CASE */
1296
Paul Bakker33b43f12013-08-20 11:48:36 +02001297/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001298void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1299 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_mpi X, A;
1302 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001303
Gilles Peskineb8e15342021-06-10 23:18:39 +02001304 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1305 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001307 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001309
Paul Bakkerbd51b262014-07-10 15:26:12 +02001310exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001312}
Paul Bakker33b43f12013-08-20 11:48:36 +02001313/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001314
Paul Bakker33b43f12013-08-20 11:48:36 +02001315/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001316void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1317 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 mbedtls_mpi X, A;
1320 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001321
Gilles Peskineb8e15342021-06-10 23:18:39 +02001322 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1323 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001325 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001327
Paul Bakkerbd51b262014-07-10 15:26:12 +02001328exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001330}
Paul Bakker33b43f12013-08-20 11:48:36 +02001331/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001332
Gilles Peskine2f780622020-11-25 15:37:20 +01001333/* BEGIN_CASE */
1334void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1335{
1336 mbedtls_mpi X;
1337 int ret;
1338 size_t bytes_left = rng_bytes;
1339 mbedtls_mpi_init( &X );
1340
1341 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1342 f_rng_bytes_left, &bytes_left );
1343 TEST_ASSERT( ret == expected_ret );
1344
1345 if( expected_ret == 0 )
1346 {
1347 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1348 * as a big-endian representation of the number. We know when
1349 * our RNG function returns null bytes, so we know how many
1350 * leading zero bytes the number has. */
1351 size_t leading_zeros = 0;
1352 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1353 leading_zeros = 1;
1354 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1355 (size_t) wanted_bytes );
1356 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001357 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2f780622020-11-25 15:37:20 +01001358 }
1359
1360exit:
1361 mbedtls_mpi_free( &X );
1362}
1363/* END_CASE */
1364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001366void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001367{
Andres AG93012e82016-09-09 09:10:28 +01001368 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001369}
Paul Bakker33b43f12013-08-20 11:48:36 +02001370/* END_CASE */