blob: 8b8eef2873483b9fac82d6f879ce3bedeeee99ea [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/bignum.h"
Gilles Peskine3cb1e292020-11-25 15:37:20 +01003#include "mbedtls/entropy.h"
Janos Follath64eca052018-09-05 17:04:49 +01004
Chris Jonese64a46f2020-12-03 17:44:03 +00005#if MBEDTLS_MPI_MAX_BITS > 792
6#define MPI_MAX_BITS_LARGER_THAN_792
Chris Jones4592bd82020-12-03 14:24:33 +00007#endif
Janos Follath64eca052018-09-05 17:04:49 +01008
Gilles Peskineb53b2182021-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 Peskine3cb1e292020-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
Gilles Peskine3b056152021-04-13 19:50:04 +020079/* Test whether bytes represents (in big-endian base 256) a number b that
80 * is significantly above a power of 2. That is, b must not have a long run
81 * of unset bits after the most significant bit.
82 *
83 * Let n be the bit-size of b, i.e. the integer such that 2^n <= b < 2^{n+1}.
84 * This function returns 1 if, when drawing a number between 0 and b,
85 * the probability that this number is at least 2^n is not negligible.
86 * This probability is (b - 2^n) / b and this function checks that this
87 * number is above some threshold A. The threshold value is heuristic and
88 * based on the needs of mpi_random_many().
Gilles Peskine4699fa42021-03-29 22:02:55 +020089 */
90static int is_significantly_above_a_power_of_2( data_t *bytes )
91{
92 const uint8_t *p = bytes->x;
93 size_t len = bytes->len;
94 unsigned x;
Gilles Peskine3b056152021-04-13 19:50:04 +020095
96 /* Skip leading null bytes */
Gilles Peskine4699fa42021-03-29 22:02:55 +020097 while( len > 0 && p[0] == 0 )
98 {
99 ++p;
100 --len;
101 }
Gilles Peskine3b056152021-04-13 19:50:04 +0200102 /* 0 is not significantly above a power of 2 */
Gilles Peskine4699fa42021-03-29 22:02:55 +0200103 if( len == 0 )
104 return( 0 );
Gilles Peskine3b056152021-04-13 19:50:04 +0200105 /* Extract the (up to) 2 most significant bytes */
106 if( len == 1 )
Gilles Peskine4699fa42021-03-29 22:02:55 +0200107 x = p[0];
108 else
109 x = ( p[0] << 8 ) | p[1];
110
Gilles Peskine3b056152021-04-13 19:50:04 +0200111 /* Shift the most significant bit of x to position 8 and mask it out */
112 while( ( x & 0xfe00 ) != 0 )
113 x >>= 1;
114 x &= 0x00ff;
Gilles Peskine4699fa42021-03-29 22:02:55 +0200115
Gilles Peskine3b056152021-04-13 19:50:04 +0200116 /* At this point, x = floor((b - 2^n) / 2^(n-8)). b is significantly above
117 * a power of 2 iff x is significantly above 0 compared to 2^8.
118 * Testing x >= 2^4 amounts to picking A = 1/16 in the function
119 * description above. */
120 return( x >= 0x10 );
Gilles Peskine4699fa42021-03-29 22:02:55 +0200121}
122
Paul Bakker33b43f12013-08-20 11:48:36 +0200123/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +0000124
Paul Bakker33b43f12013-08-20 11:48:36 +0200125/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200127 * END_DEPENDENCIES
128 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000129
Hanno Beckerb48e1aa2018-12-18 23:25:01 +0000130/* BEGIN_CASE */
131void mpi_valid_param( )
132{
133 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
134}
135/* END_CASE */
136
Hanno Beckerafb607b2018-12-11 14:27:08 +0000137/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
138void mpi_invalid_param( )
139{
140 mbedtls_mpi X;
141 const char *s_in = "00101000101010";
142 char s_out[16] = { 0 };
143 unsigned char u_out[16] = { 0 };
144 unsigned char u_in[16] = { 0 };
145 size_t olen;
146 mbedtls_mpi_uint mpi_uint;
147
148 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000149
Hanno Beckerafb607b2018-12-11 14:27:08 +0000150 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
151 mbedtls_mpi_grow( NULL, 42 ) );
152 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
153 mbedtls_mpi_copy( NULL, &X ) );
154 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
155 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000156
Hanno Beckerafb607b2018-12-11 14:27:08 +0000157 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
158 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000159
Hanno Beckerafb607b2018-12-11 14:27:08 +0000160 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
161 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
163 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000164
Hanno Beckerafb607b2018-12-11 14:27:08 +0000165 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
166 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
167 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
168 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000169
Hanno Beckerafb607b2018-12-11 14:27:08 +0000170 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
171 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000172
Hanno Beckerafb607b2018-12-11 14:27:08 +0000173 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
174 mbedtls_mpi_get_bit( NULL, 42 ) );
175 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
176 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000177
Hanno Beckerafb607b2018-12-11 14:27:08 +0000178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
179 mbedtls_mpi_read_string( NULL, 2, s_in ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
181 mbedtls_mpi_read_string( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000182
Hanno Beckerafb607b2018-12-11 14:27:08 +0000183 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
184 mbedtls_mpi_write_string( NULL, 2,
185 s_out, sizeof( s_out ),
186 &olen ) );
187 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
188 mbedtls_mpi_write_string( &X, 2,
189 NULL, sizeof( s_out ),
190 &olen ) );
191 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
192 mbedtls_mpi_write_string( &X, 2,
193 s_out, sizeof( s_out ),
194 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000195
Hanno Beckerafb607b2018-12-11 14:27:08 +0000196#if defined(MBEDTLS_FS_IO)
197 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
198 mbedtls_mpi_read_file( NULL, 2, stdin ) );
199 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
200 mbedtls_mpi_read_file( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000201
Hanno Beckerafb607b2018-12-11 14:27:08 +0000202 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
203 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
204#endif /* MBEDTLS_FS_IO */
205
206 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
207 mbedtls_mpi_read_binary( NULL, u_in,
208 sizeof( u_in ) ) );
209 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
210 mbedtls_mpi_read_binary( &X, NULL,
211 sizeof( u_in ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000212
Hanno Beckerafb607b2018-12-11 14:27:08 +0000213 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
214 mbedtls_mpi_write_binary( NULL, u_out,
215 sizeof( u_out ) ) );
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
217 mbedtls_mpi_write_binary( &X, NULL,
218 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000219
Hanno Beckerafb607b2018-12-11 14:27:08 +0000220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
221 mbedtls_mpi_shift_l( NULL, 42 ) );
222 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
223 mbedtls_mpi_shift_r( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000224
Hanno Beckerafb607b2018-12-11 14:27:08 +0000225 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
226 mbedtls_mpi_cmp_abs( NULL, &X ) );
227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
228 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000229
Hanno Beckerafb607b2018-12-11 14:27:08 +0000230 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
231 mbedtls_mpi_cmp_mpi( NULL, &X ) );
232 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
233 mbedtls_mpi_cmp_mpi( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000234
Hanno Beckerafb607b2018-12-11 14:27:08 +0000235 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
236 mbedtls_mpi_cmp_int( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000237
Hanno Beckerafb607b2018-12-11 14:27:08 +0000238 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
239 mbedtls_mpi_add_abs( NULL, &X, &X ) );
240 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
241 mbedtls_mpi_add_abs( &X, NULL, &X ) );
242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
243 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000244
Hanno Beckerafb607b2018-12-11 14:27:08 +0000245 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
246 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
247 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
248 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
249 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
250 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000251
Hanno Beckerafb607b2018-12-11 14:27:08 +0000252 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
253 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
255 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000258
Hanno Beckerafb607b2018-12-11 14:27:08 +0000259 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
260 mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
261 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
262 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
264 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
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_add_int( NULL, &X, 42 ) );
268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
269 mbedtls_mpi_add_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000270
Hanno Beckerafb607b2018-12-11 14:27:08 +0000271 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
272 mbedtls_mpi_sub_int( NULL, &X, 42 ) );
273 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
274 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000275
Hanno Beckerafb607b2018-12-11 14:27:08 +0000276 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
277 mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
278 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
279 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
280 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
281 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000282
Hanno Beckerafb607b2018-12-11 14:27:08 +0000283 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
284 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
285 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
286 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
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_div_mpi( &X, &X, NULL, &X ) );
290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
291 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000292
Hanno Beckerafb607b2018-12-11 14:27:08 +0000293 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
294 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000295
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000296 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
297
Hanno Beckerafb607b2018-12-11 14:27:08 +0000298 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
299 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
300 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
301 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
302 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
303 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000304
Hanno Beckerafb607b2018-12-11 14:27:08 +0000305 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
306 mbedtls_mpi_mod_int( NULL, &X, 42 ) );
307 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
308 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000309
Hanno Beckerafb607b2018-12-11 14:27:08 +0000310 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
311 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
312 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
313 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
314 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
315 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
316 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
317 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000318
Hanno Beckerafb607b2018-12-11 14:27:08 +0000319 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200320 mbedtls_mpi_fill_random( NULL, 42,
321 mbedtls_test_rnd_std_rand,
Hanno Beckerafb607b2018-12-11 14:27:08 +0000322 NULL ) );
323 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
324 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000325
Hanno Beckerafb607b2018-12-11 14:27:08 +0000326 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
327 mbedtls_mpi_gcd( NULL, &X, &X ) );
328 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
329 mbedtls_mpi_gcd( &X, NULL, &X ) );
330 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
331 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000332
Hanno Beckerafb607b2018-12-11 14:27:08 +0000333 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
334 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
335 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
336 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
337 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000338 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000339
340exit:
341 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000342}
343/* END_CASE */
344
Paul Bakker33b43f12013-08-20 11:48:36 +0200345/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100346void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200347{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200348 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200349
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200350 mbedtls_mpi_init( &X );
351 mbedtls_mpi_init( &Y );
352 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200353
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200354 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
355 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200356 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200357 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200358
359exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200360 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200361}
362/* END_CASE */
363
364/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100365void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
366 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200367 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000370 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100371 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000374
Janos Follath04dadb72019-03-06 12:29:37 +0000375 memset( str, '!', sizeof( str ) );
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200378 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000379 {
Gilles Peskineb53b2182021-06-10 15:34:15 +0200380 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100381 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200382 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000383 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200384 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000385 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000386 }
387 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000388
Paul Bakkerbd51b262014-07-10 15:26:12 +0200389exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000391}
Paul Bakker33b43f12013-08-20 11:48:36 +0200392/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000393
Paul Bakker33b43f12013-08-20 11:48:36 +0200394/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100395void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000398 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100399 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000402
Paul Bakkere896fea2009-07-06 06:40:23 +0000403
Azim Khand30ca132017-06-09 04:32:58 +0100404 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200405 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follathe5670f22019-02-25 16:11:58 +0000406 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200407 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000408
Paul Bakkerbd51b262014-07-10 15:26:12 +0200409exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000411}
Paul Bakker33b43f12013-08-20 11:48:36 +0200412/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000413
Paul Bakker33b43f12013-08-20 11:48:36 +0200414/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000415void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
416{
417 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000418 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000419 size_t len;
420
421 mbedtls_mpi_init( &X );
422
423
424 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200425 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follathe5670f22019-02-25 16:11:58 +0000426 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000427 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
428
429exit:
430 mbedtls_mpi_free( &X );
431}
432/* END_CASE */
433
434/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100435void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100436 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100437 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000440 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000441 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000442
443 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000446
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200447 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200450 if( buflen > (size_t) output_size )
451 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200454 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000455 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000456
Ronald Cron2dbba992020-06-10 11:42:32 +0200457 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
458 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000459 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000460
Paul Bakkerbd51b262014-07-10 15:26:12 +0200461exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000463}
Paul Bakker33b43f12013-08-20 11:48:36 +0200464/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000465
Janos Follathe344d0f2019-02-19 16:17:40 +0000466/* BEGIN_CASE */
467void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
468 data_t * input_A, int output_size,
469 int result )
470{
471 mbedtls_mpi X;
472 unsigned char buf[1000];
473 size_t buflen;
474
475 memset( buf, 0x00, 1000 );
476
477 mbedtls_mpi_init( &X );
478
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200479 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000480
481 buflen = mbedtls_mpi_size( &X );
482 if( buflen > (size_t) output_size )
483 buflen = (size_t) output_size;
484
485 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
486 if( result == 0)
487 {
488
Ronald Cron2dbba992020-06-10 11:42:32 +0200489 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
490 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000491 }
492
493exit:
494 mbedtls_mpi_free( &X );
495}
496/* END_CASE */
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100499void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100500 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000501{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000503 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000504 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000505 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000506 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000507
508 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000511
Paul Bakker33b43f12013-08-20 11:48:36 +0200512 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200513 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000515 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000516 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000517
Paul Bakker33b43f12013-08-20 11:48:36 +0200518 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000519 {
Gilles Peskineb53b2182021-06-10 15:34:15 +0200520 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 buflen = mbedtls_mpi_size( &X );
522 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000523
Paul Bakkere896fea2009-07-06 06:40:23 +0000524
Ronald Cron2dbba992020-06-10 11:42:32 +0200525 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
526 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000527 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000528
Paul Bakkerbd51b262014-07-10 15:26:12 +0200529exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000531}
Paul Bakker33b43f12013-08-20 11:48:36 +0200532/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100535void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
536 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000539 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200540 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000543
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200544 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000545
Paul Bakker33b43f12013-08-20 11:48:36 +0200546 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000547 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200548 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000549 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200550 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000551
Paul Bakker33b43f12013-08-20 11:48:36 +0200552 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000553 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200554 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000555 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200556 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000559
Paul Bakkerbd51b262014-07-10 15:26:12 +0200560exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000562}
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000564
Paul Bakker33b43f12013-08-20 11:48:36 +0200565/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100566void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi X;
569 mbedtls_mpi_init( &X );
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200570 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000572
Paul Bakkerbd51b262014-07-10 15:26:12 +0200573exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000575}
Paul Bakker33b43f12013-08-20 11:48:36 +0200576/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000577
Paul Bakker33b43f12013-08-20 11:48:36 +0200578/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100579void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
580 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000581{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_mpi X, Y;
583 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000584
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200585 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
586 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100587 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
588
589 if( result == 0 )
590 {
Gilles Peskineb53b2182021-06-10 15:34:15 +0200591 TEST_ASSERT( sign_is_valid( &X ) );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100592 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
593 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000594
Paul Bakkerbd51b262014-07-10 15:26:12 +0200595exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000597}
Paul Bakker33b43f12013-08-20 11:48:36 +0200598/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000599
Paul Bakker33b43f12013-08-20 11:48:36 +0200600/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100601void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_mpi X;
604 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000605
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200606 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000608
Paul Bakkerbd51b262014-07-10 15:26:12 +0200609exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000611}
Paul Bakker33b43f12013-08-20 11:48:36 +0200612/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000613
Paul Bakker33b43f12013-08-20 11:48:36 +0200614/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100615void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000616{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_mpi X;
618 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000619
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200620 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200621 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000622
Paul Bakkerbd51b262014-07-10 15:26:12 +0200623exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000625}
Paul Bakker33b43f12013-08-20 11:48:36 +0200626/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000627
Paul Bakker33b43f12013-08-20 11:48:36 +0200628/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100629void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
630 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_mpi A, X, Y, Z;
633 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000634
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200635 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
636 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
637 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200639 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000641
Paul Bakkerbd51b262014-07-10 15:26:12 +0200642exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000644}
Paul Bakker33b43f12013-08-20 11:48:36 +0200645/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000646
Paul Bakker33b43f12013-08-20 11:48:36 +0200647/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000649{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_mpi X;
651 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
654 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000655
Paul Bakkerbd51b262014-07-10 15:26:12 +0200656exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000658}
Paul Bakker33b43f12013-08-20 11:48:36 +0200659/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000660
Paul Bakker33b43f12013-08-20 11:48:36 +0200661/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100662void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
663 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000664{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_mpi X, Y;
666 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000667
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200668 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
669 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000671
Paul Bakkerbd51b262014-07-10 15:26:12 +0200672exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000674}
Paul Bakker33b43f12013-08-20 11:48:36 +0200675/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000676
Paul Bakker33b43f12013-08-20 11:48:36 +0200677/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100678void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
679 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100680 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100681{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200682 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100683 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100684 mbedtls_mpi X, Y;
685 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
686
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200687 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 );
688 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100689
Gilles Peskine9018b112020-01-21 16:30:53 +0100690 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
691 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100692
Janos Follath0e5532d2019-10-11 14:21:53 +0100693 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100694 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100695 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100696
697exit:
698 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
699}
700/* END_CASE */
701
702/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100703void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
704 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_mpi X, Y;
707 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000708
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200709 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
710 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000712
Paul Bakkerbd51b262014-07-10 15:26:12 +0200713exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000715}
Paul Bakker33b43f12013-08-20 11:48:36 +0200716/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000717
Paul Bakker33b43f12013-08-20 11:48:36 +0200718/* BEGIN_CASE */
Gilles Peskine77f55c92021-06-10 15:17:30 +0200719void mbedtls_mpi_copy( char *src_hex, char *dst_hex )
Paul Bakker367dae42009-06-28 21:50:27 +0000720{
Gilles Peskine77f55c92021-06-10 15:17:30 +0200721 mbedtls_mpi src, dst;
722 mbedtls_mpi_init( &src );
723 mbedtls_mpi_init( &dst );
Paul Bakker367dae42009-06-28 21:50:27 +0000724
Gilles Peskine77f55c92021-06-10 15:17:30 +0200725 TEST_ASSERT( mbedtls_test_read_mpi( &src, 16, src_hex ) == 0 );
726 TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100727
Gilles Peskine77f55c92021-06-10 15:17:30 +0200728 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
729
730 TEST_ASSERT( sign_is_valid( &dst ) );
731 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000732
Paul Bakkerbd51b262014-07-10 15:26:12 +0200733exit:
Gilles Peskine77f55c92021-06-10 15:17:30 +0200734 mbedtls_mpi_free( &src );
735 mbedtls_mpi_free( &dst );
Gilles Peskine7428b452020-01-20 21:01:51 +0100736}
737/* END_CASE */
738
739/* BEGIN_CASE */
Gilles Peskine77f55c92021-06-10 15:17:30 +0200740void mpi_copy_self( char *input_X )
Gilles Peskine7428b452020-01-20 21:01:51 +0100741{
Gilles Peskine77f55c92021-06-10 15:17:30 +0200742 mbedtls_mpi X, A;
743 mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000745
Gilles Peskine77f55c92021-06-10 15:17:30 +0200746 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
Gilles Peskine77f55c92021-06-10 15:17:30 +0200748
749 TEST_ASSERT( mbedtls_test_read_mpi( &A, 16, input_X ) == 0 );
750 TEST_ASSERT( sign_is_valid( &X ) );
751 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000752
Paul Bakkerbd51b262014-07-10 15:26:12 +0200753exit:
Gilles Peskine77f55c92021-06-10 15:17:30 +0200754 mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000756}
Paul Bakker33b43f12013-08-20 11:48:36 +0200757/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000758
Paul Bakker33b43f12013-08-20 11:48:36 +0200759/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_mpi X;
763 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100766 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
768 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100769 TEST_ASSERT( X.n == (size_t) after );
770
Paul Bakkerbd51b262014-07-10 15:26:12 +0200771exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100773}
774/* END_CASE */
775
776/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100777void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
778 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_mpi X, Y, XX;
781 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100782
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200783 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100784 X.s = x_sign;
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200785 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100786 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200790 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200794 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100796
Paul Bakkerbd51b262014-07-10 15:26:12 +0200797exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100799}
800/* END_CASE */
801
802/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100803void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
804 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100805{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
809 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100810
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200811 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100812 X.s = x_sign;
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200813 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100814 Y.s = y_sign;
815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
817 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200820 TEST_ASSERT( sign_is_valid( &X ) );
821 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
823 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200826 TEST_ASSERT( sign_is_valid( &X ) );
827 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
829 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100830
Paul Bakkerbd51b262014-07-10 15:26:12 +0200831exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
833 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100834}
835/* END_CASE */
836
837/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100838void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000839{
Gilles Peskine7428b452020-01-20 21:01:51 +0100840 mbedtls_mpi X, Y;
841 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
844 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100845 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
846 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_mpi_swap( &X, &Y );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200849 TEST_ASSERT( sign_is_valid( &X ) );
850 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine7428b452020-01-20 21:01:51 +0100851 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
852 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000853
Paul Bakkerbd51b262014-07-10 15:26:12 +0200854exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100855 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
856}
857/* END_CASE */
858
859/* BEGIN_CASE */
860void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
861{
862 mbedtls_mpi X, Y, X0, Y0;
863 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
864 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
865
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100866 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
867 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
868 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
869 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100870
871 mbedtls_mpi_swap( &X, &Y );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200872 TEST_ASSERT( sign_is_valid( &X ) );
873 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine7428b452020-01-20 21:01:51 +0100874 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
875 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
876
877exit:
878 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
879 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
880}
881/* END_CASE */
882
883/* BEGIN_CASE */
884void mpi_swap_self( data_t *input_X )
885{
886 mbedtls_mpi X, X0;
887 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
888
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100889 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
890 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100891
892 mbedtls_mpi_swap( &X, &X );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200893 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine7428b452020-01-20 21:01:51 +0100894 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
895
896exit:
897 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000898}
Paul Bakker33b43f12013-08-20 11:48:36 +0200899/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000900
Paul Bakker33b43f12013-08-20 11:48:36 +0200901/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100902void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
903 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_mpi X, Y, Z, A;
906 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000907
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200908 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
909 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
910 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200912 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000914
Gilles Peskine56f943a2020-07-23 01:18:11 +0200915 /* result == first operand */
916 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200917 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200918 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200919 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200920
921 /* result == second operand */
922 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200923 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200924 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
925
Paul Bakkerbd51b262014-07-10 15:26:12 +0200926exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000928}
Paul Bakker33b43f12013-08-20 11:48:36 +0200929/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000930
Paul Bakker33b43f12013-08-20 11:48:36 +0200931/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100932void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
933 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100934{
935 mbedtls_mpi X, A;
936 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
937
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200938 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100939
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200940 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100941 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
942 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200943 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100944
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200945 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100946 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200947 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100948 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
949
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200950 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100951 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200952 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +0100953 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
954
955exit:
956 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
957}
958/* END_CASE */
959
960
961/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100962void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
963 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000964{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_mpi X, Y, Z, A;
966 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000967
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200968 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
969 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
970 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200972 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000974
Gilles Peskine56f943a2020-07-23 01:18:11 +0200975 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200977 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200979 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200980
981 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200983 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000985
Paul Bakkerbd51b262014-07-10 15:26:12 +0200986exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +0200987 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000988}
Paul Bakker33b43f12013-08-20 11:48:36 +0200989/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000990
Paul Bakker33b43f12013-08-20 11:48:36 +0200991/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100992void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
993 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000994{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_mpi X, Z, A;
996 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000997
Gilles Peskinea0f4e102021-06-10 23:18:39 +0200998 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
999 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001001 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001003
Paul Bakkerbd51b262014-07-10 15:26:12 +02001004exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001006}
Paul Bakker33b43f12013-08-20 11:48:36 +02001007/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001008
Paul Bakker33b43f12013-08-20 11:48:36 +02001009/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001010void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
1011 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_mpi X, Y, Z, A;
1014 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001015
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001016 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1017 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1018 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001020 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001022
Gilles Peskine56f943a2020-07-23 01:18:11 +02001023 /* result == first operand */
1024 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001025 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001026 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001027 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001028
1029 /* result == second operand */
1030 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001031 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001032 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1033
Paul Bakkerbd51b262014-07-10 15:26:12 +02001034exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001036}
Paul Bakker33b43f12013-08-20 11:48:36 +02001037/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001038
Paul Bakker33b43f12013-08-20 11:48:36 +02001039/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001040void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
1041 char * input_Y, int radix_A, char * input_A,
1042 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001045 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001047
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001048 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1049 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1050 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001053 TEST_ASSERT( res == sub_result );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001054 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +00001055 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001057
Gilles Peskine56f943a2020-07-23 01:18:11 +02001058 /* result == first operand */
1059 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001060 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001061 if( sub_result == 0 )
1062 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001063 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001064
1065 /* result == second operand */
1066 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001067 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001068 if( sub_result == 0 )
1069 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1070
Paul Bakkerbd51b262014-07-10 15:26:12 +02001071exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001073}
Paul Bakker33b43f12013-08-20 11:48:36 +02001074/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001075
Paul Bakker33b43f12013-08-20 11:48:36 +02001076/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001077void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
1078 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_mpi X, Z, A;
1081 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001082
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001083 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1084 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001086 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001088
Paul Bakkerbd51b262014-07-10 15:26:12 +02001089exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001091}
Paul Bakker33b43f12013-08-20 11:48:36 +02001092/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001093
Paul Bakker33b43f12013-08-20 11:48:36 +02001094/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001095void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
1096 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi X, Y, Z, A;
1099 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001100
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001101 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1102 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1103 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001105 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001107
Paul Bakkerbd51b262014-07-10 15:26:12 +02001108exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001110}
Paul Bakker33b43f12013-08-20 11:48:36 +02001111/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001112
Paul Bakker33b43f12013-08-20 11:48:36 +02001113/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001114void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1115 int radix_A, char * input_A,
1116 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 mbedtls_mpi X, Z, A;
1119 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001120
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001121 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1122 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001124 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001125 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001127 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001129 else
1130 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001131
Paul Bakkerbd51b262014-07-10 15:26:12 +02001132exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001134}
Paul Bakker33b43f12013-08-20 11:48:36 +02001135/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001136
Paul Bakker33b43f12013-08-20 11:48:36 +02001137/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001138void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1139 char * input_Y, int radix_A, char * input_A,
1140 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001143 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1145 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001146
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001147 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1148 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1149 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1150 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001152 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001153 if( res == 0 )
1154 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001155 TEST_ASSERT( sign_is_valid( &Q ) );
1156 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1158 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001159 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001160
Paul Bakkerbd51b262014-07-10 15:26:12 +02001161exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1163 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001164}
Paul Bakker33b43f12013-08-20 11:48:36 +02001165/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001166
Paul Bakker33b43f12013-08-20 11:48:36 +02001167/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001168void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1169 int radix_A, char * input_A, int radix_B,
1170 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001173 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1175 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001176
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001177 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1178 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1179 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001181 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001182 if( res == 0 )
1183 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001184 TEST_ASSERT( sign_is_valid( &Q ) );
1185 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1187 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001188 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001189
Paul Bakkerbd51b262014-07-10 15:26:12 +02001190exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1192 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001193}
Paul Bakker33b43f12013-08-20 11:48:36 +02001194/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001195
Paul Bakker33b43f12013-08-20 11:48:36 +02001196/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001197void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1198 char * input_Y, int radix_A, char * input_A,
1199 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001202 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001204
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001205 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1206 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1207 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001209 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001210 if( res == 0 )
1211 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001212 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001214 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001215
Paul Bakkerbd51b262014-07-10 15:26:12 +02001216exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001218}
Paul Bakker33b43f12013-08-20 11:48:36 +02001219/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001220
Paul Bakker33b43f12013-08-20 11:48:36 +02001221/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001222void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1223 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001224{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001226 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_mpi_uint r;
1228 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001229
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001230 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001232 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001233 if( res == 0 )
1234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001236 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001237
Paul Bakkerbd51b262014-07-10 15:26:12 +02001238exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001240}
Paul Bakker33b43f12013-08-20 11:48:36 +02001241/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001242
Paul Bakker33b43f12013-08-20 11:48:36 +02001243/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001244void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1245 char * input_E, int radix_N, char * input_N,
1246 int radix_RR, char * input_RR, int radix_X,
1247 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001250 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1252 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001253
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001254 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1255 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
1256 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
1257 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001258
Paul Bakker33b43f12013-08-20 11:48:36 +02001259 if( strlen( input_RR ) )
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001260 TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001263 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001264 if( res == 0 )
1265 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001266 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001268 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001269
Paul Bakkerbd51b262014-07-10 15:26:12 +02001270exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1272 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001273}
Paul Bakker33b43f12013-08-20 11:48:36 +02001274/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001275
Paul Bakker33b43f12013-08-20 11:48:36 +02001276/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001277void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesaa850cd2020-12-03 11:35:41 +00001278 int radix_RR, char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001279{
1280 mbedtls_mpi A, E, N, RR, Z;
1281 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1282 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1283
Chris Jonesaa850cd2020-12-03 11:35:41 +00001284 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001285 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001286 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001287 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001288
1289 /* Set E to 2^(E_bytes - 1) + 1 */
1290 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1291 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001292 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001293
1294 /* Set N to 2^(N_bytes - 1) + 1 */
1295 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1296 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001297 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1298
1299 if( strlen( input_RR ) )
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001300 TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001301
Chris Jonesaa850cd2020-12-03 11:35:41 +00001302 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001303
1304exit:
1305 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1306 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1307}
1308/* END_CASE */
1309
1310/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001311void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1312 char * input_Y, int radix_A, char * input_A,
1313 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001316 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001318
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001319 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1320 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1321 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001323 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001324 if( res == 0 )
1325 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001326 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001328 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001329
Paul Bakkerbd51b262014-07-10 15:26:12 +02001330exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001332}
Paul Bakker33b43f12013-08-20 11:48:36 +02001333/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001336void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001339 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001341
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001342 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001343 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001344 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001345
Paul Bakkerbd51b262014-07-10 15:26:12 +02001346exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001348}
Paul Bakker33b43f12013-08-20 11:48:36 +02001349/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001352void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001353 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001354{
1355 mbedtls_mpi X;
1356 int res;
1357 mbedtls_test_mpi_random rand;
1358
1359 mbedtls_mpi_init( &X );
1360 rand.data = witnesses;
1361 rand.pos = 0;
1362 rand.chunk_len = chunk_len;
1363
1364 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001365 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1366 mbedtls_test_mpi_miller_rabin_determinizer,
1367 &rand );
1368 TEST_ASSERT( res == 0 );
1369
1370 rand.data = witnesses;
1371 rand.pos = 0;
1372 rand.chunk_len = chunk_len;
1373
Janos Follatha0b67c22018-09-18 14:48:23 +01001374 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1375 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001376 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001377 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001378
1379exit:
1380 mbedtls_mpi_free( &X );
1381}
1382/* END_CASE */
1383
1384/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001385void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001388 int my_ret;
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001391
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001392 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1393 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001394 TEST_ASSERT( my_ret == ref_ret );
1395
1396 if( ref_ret == 0 )
1397 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001398 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001399
1400 TEST_ASSERT( actual_bits >= (size_t) bits );
1401 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001402 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001403
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001404 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1405 mbedtls_test_rnd_std_rand,
1406 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001407 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001408 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001409 /* X = ( X - 1 ) / 2 */
1410 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001411 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1412 mbedtls_test_rnd_std_rand,
1413 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001414 }
1415 }
1416
Paul Bakkerbd51b262014-07-10 15:26:12 +02001417exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001419}
1420/* END_CASE */
1421
Paul Bakker33b43f12013-08-20 11:48:36 +02001422/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001423void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1424 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001425{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 mbedtls_mpi X, A;
1427 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001428
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001429 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1430 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001432 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001434
Paul Bakkerbd51b262014-07-10 15:26:12 +02001435exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001437}
Paul Bakker33b43f12013-08-20 11:48:36 +02001438/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001439
Paul Bakker33b43f12013-08-20 11:48:36 +02001440/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001441void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1442 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001443{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 mbedtls_mpi X, A;
1445 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001446
Gilles Peskinea0f4e102021-06-10 23:18:39 +02001447 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1448 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001450 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001452
Paul Bakkerbd51b262014-07-10 15:26:12 +02001453exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001455}
Paul Bakker33b43f12013-08-20 11:48:36 +02001456/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001457
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001458/* BEGIN_CASE */
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001459void mpi_fill_random( int wanted_bytes, int rng_bytes,
1460 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001461{
1462 mbedtls_mpi X;
1463 int ret;
1464 size_t bytes_left = rng_bytes;
1465 mbedtls_mpi_init( &X );
1466
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001467 if( before != 0 )
1468 {
1469 /* Set X to sign(before) * 2^(|before|-1) */
1470 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1471 if( before < 0 )
1472 before = - before;
1473 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1474 }
1475
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001476 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1477 f_rng_bytes_left, &bytes_left );
1478 TEST_ASSERT( ret == expected_ret );
1479
1480 if( expected_ret == 0 )
1481 {
1482 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1483 * as a big-endian representation of the number. We know when
1484 * our RNG function returns null bytes, so we know how many
1485 * leading zero bytes the number has. */
1486 size_t leading_zeros = 0;
1487 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1488 leading_zeros = 1;
1489 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1490 (size_t) wanted_bytes );
1491 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001492 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001493 }
1494
1495exit:
1496 mbedtls_mpi_free( &X );
1497}
1498/* END_CASE */
1499
Gilles Peskine4699fa42021-03-29 22:02:55 +02001500/* BEGIN_CASE */
1501void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1502{
1503 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1504 * This function assumes that the value of bound is at least 2 and
1505 * that iterations is large enough that a one-in-2^iterations chance
1506 * effectively never occurs.
1507 */
1508
1509 mbedtls_mpi upper_bound;
1510 size_t n_bits;
1511 mbedtls_mpi result;
1512 size_t b;
1513 /* If upper_bound is small, stats[b] is the number of times the value b
1514 * has been generated. Otherwise stats[b] is the number of times a
1515 * value with bit b set has been generated. */
1516 size_t *stats = NULL;
1517 size_t stats_len;
1518 int full_stats;
1519 size_t i;
1520
1521 mbedtls_mpi_init( &upper_bound );
1522 mbedtls_mpi_init( &result );
1523
1524 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1525 bound_bytes->x, bound_bytes->len ) );
1526 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1527 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1528 * to be small enough that the probability of missing one value is
1529 * negligible given the number of iterations. It must be less than
1530 * 256 because some of the code below assumes that "small" values
1531 * fit in a byte. */
1532 if( n_bits <= 5 )
1533 {
1534 full_stats = 1;
1535 stats_len = bound_bytes->x[bound_bytes->len - 1];
1536 }
1537 else
1538 {
1539 full_stats = 0;
1540 stats_len = n_bits;
1541 }
1542 ASSERT_ALLOC( stats, stats_len );
1543
1544 for( i = 0; i < (size_t) iterations; i++ )
1545 {
1546 mbedtls_test_set_step( i );
1547 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1548 mbedtls_test_rnd_std_rand, NULL ) );
1549
Gilles Peskineb53b2182021-06-10 15:34:15 +02001550 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine4699fa42021-03-29 22:02:55 +02001551 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1552 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1553 if( full_stats )
1554 {
1555 uint8_t value;
1556 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1557 TEST_ASSERT( value < stats_len );
1558 ++stats[value];
1559 }
1560 else
1561 {
1562 for( b = 0; b < n_bits; b++ )
1563 stats[b] += mbedtls_mpi_get_bit( &result, b );
1564 }
1565 }
1566
1567 if( full_stats )
1568 {
Gilles Peskinec520d7a2021-04-13 20:45:05 +02001569 for( b = min; b < stats_len; b++ )
Gilles Peskine4699fa42021-03-29 22:02:55 +02001570 {
1571 mbedtls_test_set_step( 1000000 + b );
1572 /* Assert that each value has been reached at least once.
1573 * This is almost guaranteed if the iteration count is large
1574 * enough. This is a very crude way of checking the distribution.
1575 */
1576 TEST_ASSERT( stats[b] > 0 );
1577 }
1578 }
1579 else
1580 {
Gilles Peskinee4f937f2021-06-02 21:24:04 +02001581 int statistically_safe_all_the_way =
1582 is_significantly_above_a_power_of_2( bound_bytes );
Gilles Peskine4699fa42021-03-29 22:02:55 +02001583 for( b = 0; b < n_bits; b++ )
1584 {
1585 mbedtls_test_set_step( 1000000 + b );
1586 /* Assert that each bit has been set in at least one result and
1587 * clear in at least one result. Provided that iterations is not
1588 * too small, it would be extremely unlikely for this not to be
1589 * the case if the results are uniformly distributed.
1590 *
1591 * As an exception, the top bit may legitimately never be set
1592 * if bound is a power of 2 or only slightly above.
1593 */
Gilles Peskinee4f937f2021-06-02 21:24:04 +02001594 if( statistically_safe_all_the_way || b != n_bits - 1 )
Gilles Peskine4699fa42021-03-29 22:02:55 +02001595 {
1596 TEST_ASSERT( stats[b] > 0 );
1597 }
1598 TEST_ASSERT( stats[b] < (size_t) iterations );
1599 }
1600 }
1601
1602exit:
1603 mbedtls_mpi_free( &upper_bound );
1604 mbedtls_mpi_free( &result );
1605 mbedtls_free( stats );
1606}
1607/* END_CASE */
1608
Gilles Peskine9312ba52021-03-29 22:14:51 +02001609/* BEGIN_CASE */
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001610void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine8f454702021-04-01 15:57:18 +02001611{
1612 mbedtls_mpi upper_bound;
1613 mbedtls_mpi result;
1614
1615 mbedtls_mpi_init( &upper_bound );
1616 mbedtls_mpi_init( &result );
1617
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001618 if( before != 0 )
1619 {
1620 /* Set result to sign(before) * 2^(|before|-1) */
1621 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1622 if( before < 0 )
1623 before = - before;
1624 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1625 }
1626
Gilles Peskine8f454702021-04-01 15:57:18 +02001627 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1628 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1629 bound_bytes->x, bound_bytes->len ) );
1630 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1631 mbedtls_test_rnd_std_rand, NULL ) );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001632 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine8f454702021-04-01 15:57:18 +02001633 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1634 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1635
1636exit:
1637 mbedtls_mpi_free( &upper_bound );
1638 mbedtls_mpi_free( &result );
1639}
1640/* END_CASE */
1641
1642/* BEGIN_CASE */
Gilles Peskine9312ba52021-03-29 22:14:51 +02001643void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1644{
1645 mbedtls_mpi upper_bound;
1646 mbedtls_mpi result;
1647 int actual_ret;
1648
1649 mbedtls_mpi_init( &upper_bound );
1650 mbedtls_mpi_init( &result );
1651
1652 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1653 bound_bytes->x, bound_bytes->len ) );
1654 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
1655 mbedtls_test_rnd_std_rand, NULL );
1656 TEST_EQUAL( expected_ret, actual_ret );
1657
1658exit:
1659 mbedtls_mpi_free( &upper_bound );
1660 mbedtls_mpi_free( &result );
1661}
1662/* END_CASE */
1663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001665void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001666{
Andres AG93012e82016-09-09 09:10:28 +01001667 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001668}
Paul Bakker33b43f12013-08-20 11:48:36 +02001669/* END_CASE */