blob: 1709677527db7251e10d0587beb2db302d589578 [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
9typedef struct mbedtls_test_mpi_random
10{
11 data_t *data;
12 size_t pos;
13 size_t chunk_len;
14} mbedtls_test_mpi_random;
15
16/*
17 * This function is called by the Miller-Rabin primality test each time it
18 * chooses a random witness. The witnesses (or non-witnesses as provided by the
19 * test) are stored in the data member of the state structure. Each number is in
20 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
21 */
22int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
23 unsigned char* buf,
24 size_t len )
25{
26 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
27
28 if( random == NULL || random->data->x == NULL || buf == NULL )
29 return( -1 );
30
31 if( random->pos + random->chunk_len > random->data->len
32 || random->chunk_len > len )
33 {
34 return( -1 );
35 }
36
37 memset( buf, 0, len );
38
39 /* The witness is written to the end of the buffer, since the buffer is
40 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
41 * Writing the witness to the start of the buffer would result in the
42 * buffer being 'witness 000...000', which would be treated as
43 * witness * 2^n for some n. */
44 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
45 random->chunk_len );
46
47 random->pos += random->chunk_len;
48
49 return( 0 );
50}
Gilles Peskine3cb1e292020-11-25 15:37:20 +010051
52/* Random generator that is told how many bytes to return. */
53static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
54{
55 size_t *bytes_left = state;
56 size_t i;
57 for( i = 0; i < len; i++ )
58 {
59 if( *bytes_left == 0 )
60 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
61 buf[i] = *bytes_left & 0xff;
62 --( *bytes_left );
63 }
64 return( 0 );
65}
66
Gilles Peskineeedefa52021-04-13 19:50:04 +020067/* Test whether bytes represents (in big-endian base 256) a number b that
68 * is significantly above a power of 2. That is, b must not have a long run
69 * of unset bits after the most significant bit.
70 *
71 * Let n be the bit-size of b, i.e. the integer such that 2^n <= b < 2^{n+1}.
72 * This function returns 1 if, when drawing a number between 0 and b,
73 * the probability that this number is at least 2^n is not negligible.
74 * This probability is (b - 2^n) / b and this function checks that this
75 * number is above some threshold A. The threshold value is heuristic and
76 * based on the needs of mpi_random_many().
Gilles Peskine02ac93a2021-03-29 22:02:55 +020077 */
78static int is_significantly_above_a_power_of_2( data_t *bytes )
79{
80 const uint8_t *p = bytes->x;
81 size_t len = bytes->len;
82 unsigned x;
Gilles Peskineeedefa52021-04-13 19:50:04 +020083
84 /* Skip leading null bytes */
Gilles Peskine02ac93a2021-03-29 22:02:55 +020085 while( len > 0 && p[0] == 0 )
86 {
87 ++p;
88 --len;
89 }
Gilles Peskineeedefa52021-04-13 19:50:04 +020090 /* 0 is not significantly above a power of 2 */
Gilles Peskine02ac93a2021-03-29 22:02:55 +020091 if( len == 0 )
92 return( 0 );
Gilles Peskineeedefa52021-04-13 19:50:04 +020093 /* Extract the (up to) 2 most significant bytes */
94 if( len == 1 )
Gilles Peskine02ac93a2021-03-29 22:02:55 +020095 x = p[0];
96 else
97 x = ( p[0] << 8 ) | p[1];
98
Gilles Peskineeedefa52021-04-13 19:50:04 +020099 /* Shift the most significant bit of x to position 8 and mask it out */
100 while( ( x & 0xfe00 ) != 0 )
101 x >>= 1;
102 x &= 0x00ff;
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200103
Gilles Peskineeedefa52021-04-13 19:50:04 +0200104 /* At this point, x = floor((b - 2^n) / 2^(n-8)). b is significantly above
105 * a power of 2 iff x is significantly above 0 compared to 2^8.
106 * Testing x >= 2^4 amounts to picking A = 1/16 in the function
107 * description above. */
108 return( x >= 0x10 );
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200109}
110
Paul Bakker33b43f12013-08-20 11:48:36 +0200111/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +0000112
Paul Bakker33b43f12013-08-20 11:48:36 +0200113/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200115 * END_DEPENDENCIES
116 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000117
Hanno Beckerb48e1aa2018-12-18 23:25:01 +0000118/* BEGIN_CASE */
119void mpi_valid_param( )
120{
121 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
122}
123/* END_CASE */
124
Hanno Beckerafb607b2018-12-11 14:27:08 +0000125/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
126void mpi_invalid_param( )
127{
128 mbedtls_mpi X;
129 const char *s_in = "00101000101010";
130 char s_out[16] = { 0 };
131 unsigned char u_out[16] = { 0 };
132 unsigned char u_in[16] = { 0 };
133 size_t olen;
134 mbedtls_mpi_uint mpi_uint;
135
136 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000137
Hanno Beckerafb607b2018-12-11 14:27:08 +0000138 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
139 mbedtls_mpi_grow( NULL, 42 ) );
140 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
141 mbedtls_mpi_copy( NULL, &X ) );
142 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
143 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000144
Hanno Beckerafb607b2018-12-11 14:27:08 +0000145 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
146 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000147
Hanno Beckerafb607b2018-12-11 14:27:08 +0000148 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
149 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
150 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
151 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000152
Hanno Beckerafb607b2018-12-11 14:27:08 +0000153 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
154 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
156 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000157
Hanno Beckerafb607b2018-12-11 14:27:08 +0000158 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
159 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000160
Hanno Beckerafb607b2018-12-11 14:27:08 +0000161 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
162 mbedtls_mpi_get_bit( NULL, 42 ) );
163 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
164 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000165
Hanno Beckerafb607b2018-12-11 14:27:08 +0000166 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
167 mbedtls_mpi_read_string( NULL, 2, s_in ) );
168 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
169 mbedtls_mpi_read_string( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000170
Hanno Beckerafb607b2018-12-11 14:27:08 +0000171 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
172 mbedtls_mpi_write_string( NULL, 2,
173 s_out, sizeof( s_out ),
174 &olen ) );
175 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
176 mbedtls_mpi_write_string( &X, 2,
177 NULL, sizeof( s_out ),
178 &olen ) );
179 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
180 mbedtls_mpi_write_string( &X, 2,
181 s_out, sizeof( s_out ),
182 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000183
Hanno Beckerafb607b2018-12-11 14:27:08 +0000184#if defined(MBEDTLS_FS_IO)
185 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
186 mbedtls_mpi_read_file( NULL, 2, stdin ) );
187 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
188 mbedtls_mpi_read_file( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000189
Hanno Beckerafb607b2018-12-11 14:27:08 +0000190 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
191 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
192#endif /* MBEDTLS_FS_IO */
193
194 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
195 mbedtls_mpi_read_binary( NULL, u_in,
196 sizeof( u_in ) ) );
197 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
198 mbedtls_mpi_read_binary( &X, NULL,
199 sizeof( u_in ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000200
Hanno Beckerafb607b2018-12-11 14:27:08 +0000201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
202 mbedtls_mpi_write_binary( NULL, u_out,
203 sizeof( u_out ) ) );
204 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
205 mbedtls_mpi_write_binary( &X, NULL,
206 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000207
Hanno Beckerafb607b2018-12-11 14:27:08 +0000208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
209 mbedtls_mpi_shift_l( NULL, 42 ) );
210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
211 mbedtls_mpi_shift_r( NULL, 42 ) );
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_cmp_abs( NULL, &X ) );
215 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
216 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000217
Hanno Beckerafb607b2018-12-11 14:27:08 +0000218 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
219 mbedtls_mpi_cmp_mpi( NULL, &X ) );
220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
221 mbedtls_mpi_cmp_mpi( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000222
Hanno Beckerafb607b2018-12-11 14:27:08 +0000223 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
224 mbedtls_mpi_cmp_int( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000225
Hanno Beckerafb607b2018-12-11 14:27:08 +0000226 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
227 mbedtls_mpi_add_abs( NULL, &X, &X ) );
228 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
229 mbedtls_mpi_add_abs( &X, NULL, &X ) );
230 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
231 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000232
Hanno Beckerafb607b2018-12-11 14:27:08 +0000233 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
234 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
235 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
236 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
237 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
238 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000239
Hanno Beckerafb607b2018-12-11 14:27:08 +0000240 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
241 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
243 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
244 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
245 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000246
Hanno Beckerafb607b2018-12-11 14:27:08 +0000247 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
248 mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
249 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
250 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
251 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
252 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000253
Hanno Beckerafb607b2018-12-11 14:27:08 +0000254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
255 mbedtls_mpi_add_int( NULL, &X, 42 ) );
256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_add_int( &X, NULL, 42 ) );
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_int( NULL, &X, 42 ) );
261 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
262 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000263
Hanno Beckerafb607b2018-12-11 14:27:08 +0000264 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
265 mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
266 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
267 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
269 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
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_mul_int( NULL, &X, 42 ) );
273 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
274 mbedtls_mpi_mul_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_div_mpi( &X, &X, NULL, &X ) );
278 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
279 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000280
Hanno Beckerafb607b2018-12-11 14:27:08 +0000281 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
282 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000283
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000284 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
285
Hanno Beckerafb607b2018-12-11 14:27:08 +0000286 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
287 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
288 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
289 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
291 mbedtls_mpi_mod_mpi( &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_mod_int( NULL, &X, 42 ) );
295 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
296 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000297
Hanno Beckerafb607b2018-12-11 14:27:08 +0000298 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
299 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
300 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
301 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
302 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
303 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
304 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
305 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000306
Hanno Beckerafb607b2018-12-11 14:27:08 +0000307 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200308 mbedtls_mpi_fill_random( NULL, 42,
309 mbedtls_test_rnd_std_rand,
Hanno Beckerafb607b2018-12-11 14:27:08 +0000310 NULL ) );
311 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
312 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000313
Hanno Beckerafb607b2018-12-11 14:27:08 +0000314 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
315 mbedtls_mpi_gcd( NULL, &X, &X ) );
316 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
317 mbedtls_mpi_gcd( &X, NULL, &X ) );
318 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
319 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000320
Hanno Beckerafb607b2018-12-11 14:27:08 +0000321 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
322 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
323 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
324 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
325 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000326 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000327
328exit:
329 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000330}
331/* END_CASE */
332
Paul Bakker33b43f12013-08-20 11:48:36 +0200333/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100334void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200335{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200336 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200337
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200338 mbedtls_mpi_init( &X );
339 mbedtls_mpi_init( &Y );
340 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200341
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200342 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
343 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200344 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200345 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200346
347exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200348 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200349}
350/* END_CASE */
351
352/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100353void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
354 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200355 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000356{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000358 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100359 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000362
Janos Follath04dadb72019-03-06 12:29:37 +0000363 memset( str, '!', sizeof( str ) );
364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200366 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000367 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100368 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200369 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000370 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200371 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000372 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000373 }
374 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000375
Paul Bakkerbd51b262014-07-10 15:26:12 +0200376exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000378}
Paul Bakker33b43f12013-08-20 11:48:36 +0200379/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000380
Paul Bakker33b43f12013-08-20 11:48:36 +0200381/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100382void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000383{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000385 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100386 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000389
Paul Bakkere896fea2009-07-06 06:40:23 +0000390
Azim Khand30ca132017-06-09 04:32:58 +0100391 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000392 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200393 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000394
Paul Bakkerbd51b262014-07-10 15:26:12 +0200395exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000397}
Paul Bakker33b43f12013-08-20 11:48:36 +0200398/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000399
Paul Bakker33b43f12013-08-20 11:48:36 +0200400/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000401void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
402{
403 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000404 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000405 size_t len;
406
407 mbedtls_mpi_init( &X );
408
409
410 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000411 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000412 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
413
414exit:
415 mbedtls_mpi_free( &X );
416}
417/* END_CASE */
418
419/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100420void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100421 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100422 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000423{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000425 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000426 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000427
428 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200435 if( buflen > (size_t) output_size )
436 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200439 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000440 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000441
Ronald Cron2dbba992020-06-10 11:42:32 +0200442 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
443 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000444 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000445
Paul Bakkerbd51b262014-07-10 15:26:12 +0200446exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000448}
Paul Bakker33b43f12013-08-20 11:48:36 +0200449/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000450
Janos Follathe344d0f2019-02-19 16:17:40 +0000451/* BEGIN_CASE */
452void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
453 data_t * input_A, int output_size,
454 int result )
455{
456 mbedtls_mpi X;
457 unsigned char buf[1000];
458 size_t buflen;
459
460 memset( buf, 0x00, 1000 );
461
462 mbedtls_mpi_init( &X );
463
464 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
465
466 buflen = mbedtls_mpi_size( &X );
467 if( buflen > (size_t) output_size )
468 buflen = (size_t) output_size;
469
470 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
471 if( result == 0)
472 {
473
Ronald Cron2dbba992020-06-10 11:42:32 +0200474 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
475 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000476 }
477
478exit:
479 mbedtls_mpi_free( &X );
480}
481/* END_CASE */
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100484void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100485 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000488 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000489 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000490 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000491 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000492
493 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000496
Paul Bakker33b43f12013-08-20 11:48:36 +0200497 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200498 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000500 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000501 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000502
Paul Bakker33b43f12013-08-20 11:48:36 +0200503 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 buflen = mbedtls_mpi_size( &X );
506 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000507
Paul Bakkere896fea2009-07-06 06:40:23 +0000508
Ronald Cron2dbba992020-06-10 11:42:32 +0200509 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
510 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000511 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000512
Paul Bakkerbd51b262014-07-10 15:26:12 +0200513exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000515}
Paul Bakker33b43f12013-08-20 11:48:36 +0200516/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100519void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
520 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000523 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200524 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000529
Paul Bakker33b43f12013-08-20 11:48:36 +0200530 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000531 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200532 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000533 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200534 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000535
Paul Bakker33b43f12013-08-20 11:48:36 +0200536 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000537 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200538 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000539 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200540 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000543
Paul Bakkerbd51b262014-07-10 15:26:12 +0200544exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000546}
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000548
Paul Bakker33b43f12013-08-20 11:48:36 +0200549/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100550void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_mpi X;
553 mbedtls_mpi_init( &X );
554 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
555 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000556
Paul Bakkerbd51b262014-07-10 15:26:12 +0200557exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000559}
Paul Bakker33b43f12013-08-20 11:48:36 +0200560/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000561
Paul Bakker33b43f12013-08-20 11:48:36 +0200562/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100563void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
564 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_mpi X, Y;
567 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
570 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100571 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
572
573 if( result == 0 )
574 {
575 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
576 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000577
Paul Bakkerbd51b262014-07-10 15:26:12 +0200578exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000580}
Paul Bakker33b43f12013-08-20 11:48:36 +0200581/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000582
Paul Bakker33b43f12013-08-20 11:48:36 +0200583/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100584void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_mpi X;
587 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
590 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000591
Paul Bakkerbd51b262014-07-10 15:26:12 +0200592exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000594}
Paul Bakker33b43f12013-08-20 11:48:36 +0200595/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000596
Paul Bakker33b43f12013-08-20 11:48:36 +0200597/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100598void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_mpi X;
601 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200604 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000605
Paul Bakkerbd51b262014-07-10 15:26:12 +0200606exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000608}
Paul Bakker33b43f12013-08-20 11:48:36 +0200609/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000610
Paul Bakker33b43f12013-08-20 11:48:36 +0200611/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100612void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
613 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000614{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_mpi A, X, Y, Z;
616 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
619 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
620 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
621 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
622 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000623
Paul Bakkerbd51b262014-07-10 15:26:12 +0200624exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000626}
Paul Bakker33b43f12013-08-20 11:48:36 +0200627/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000628
Paul Bakker33b43f12013-08-20 11:48:36 +0200629/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_mpi X;
633 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
636 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000637
Paul Bakkerbd51b262014-07-10 15:26:12 +0200638exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000640}
Paul Bakker33b43f12013-08-20 11:48:36 +0200641/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000642
Paul Bakker33b43f12013-08-20 11:48:36 +0200643/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100644void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
645 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_mpi X, Y;
648 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
651 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
652 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000653
Paul Bakkerbd51b262014-07-10 15:26:12 +0200654exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000656}
Paul Bakker33b43f12013-08-20 11:48:36 +0200657/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000658
Paul Bakker33b43f12013-08-20 11:48:36 +0200659/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100660void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
661 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100662 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100663{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200664 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100665 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100666 mbedtls_mpi X, Y;
667 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
668
Janos Follathb7e1b492019-10-14 09:21:49 +0100669 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
670 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100671
Gilles Peskine9018b112020-01-21 16:30:53 +0100672 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
673 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100674
Janos Follath0e5532d2019-10-11 14:21:53 +0100675 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100676 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100677 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100678
679exit:
680 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
681}
682/* END_CASE */
683
684/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100685void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
686 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000687{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_mpi X, Y;
689 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
692 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
693 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000694
Paul Bakkerbd51b262014-07-10 15:26:12 +0200695exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000697}
Paul Bakker33b43f12013-08-20 11:48:36 +0200698/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000699
Paul Bakker33b43f12013-08-20 11:48:36 +0200700/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100701void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000702{
Gilles Peskine7428b452020-01-20 21:01:51 +0100703 mbedtls_mpi X, Y;
704 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100707 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100710 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
711 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000712
Paul Bakkerbd51b262014-07-10 15:26:12 +0200713exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100714 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
715}
716/* END_CASE */
717
718/* BEGIN_CASE */
719void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
720{
721 mbedtls_mpi X, Y, X0;
722 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
723
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100724 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
725 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
726 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100727 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
728
729 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
730 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
731 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
732
733exit:
734 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000735}
Paul Bakker33b43f12013-08-20 11:48:36 +0200736/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000737
Paul Bakker33b43f12013-08-20 11:48:36 +0200738/* BEGIN_CASE */
739void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_mpi X;
742 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
745 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
746 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000747
Paul Bakkerbd51b262014-07-10 15:26:12 +0200748exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000750}
Paul Bakker33b43f12013-08-20 11:48:36 +0200751/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000752
Paul Bakker33b43f12013-08-20 11:48:36 +0200753/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100755{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_mpi X;
757 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100760 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
762 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100763 TEST_ASSERT( X.n == (size_t) after );
764
Paul Bakkerbd51b262014-07-10 15:26:12 +0200765exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100767}
768/* END_CASE */
769
770/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100771void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
772 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_mpi X, Y, XX;
775 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100778 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100780 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
784 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
787 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100788
Paul Bakkerbd51b262014-07-10 15:26:12 +0200789exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100791}
792/* END_CASE */
793
794/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100795void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
796 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100797{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
801 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100804 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100806 Y.s = y_sign;
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
809 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
812 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
813 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
816 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
817 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100818
Paul Bakkerbd51b262014-07-10 15:26:12 +0200819exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
821 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100822}
823/* END_CASE */
824
825/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100826void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000827{
Gilles Peskine7428b452020-01-20 21:01:51 +0100828 mbedtls_mpi X, Y;
829 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
832 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100833 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
834 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine7428b452020-01-20 21:01:51 +0100837 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
838 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000839
Paul Bakkerbd51b262014-07-10 15:26:12 +0200840exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100841 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
842}
843/* END_CASE */
844
845/* BEGIN_CASE */
846void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
847{
848 mbedtls_mpi X, Y, X0, Y0;
849 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
850 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
851
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100852 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
853 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
854 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
855 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100856
857 mbedtls_mpi_swap( &X, &Y );
858 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
859 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
860
861exit:
862 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
863 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
864}
865/* END_CASE */
866
867/* BEGIN_CASE */
868void mpi_swap_self( data_t *input_X )
869{
870 mbedtls_mpi X, X0;
871 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
872
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100873 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
874 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100875
876 mbedtls_mpi_swap( &X, &X );
877 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
878
879exit:
880 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000881}
Paul Bakker33b43f12013-08-20 11:48:36 +0200882/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000883
Paul Bakker33b43f12013-08-20 11:48:36 +0200884/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100885void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
886 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000887{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_mpi X, Y, Z, A;
889 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
892 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
893 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
894 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
895 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000896
Gilles Peskine56f943a2020-07-23 01:18:11 +0200897 /* result == first operand */
898 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
899 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
900 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
901
902 /* result == second operand */
903 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
904 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
905
Paul Bakkerbd51b262014-07-10 15:26:12 +0200906exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000908}
Paul Bakker33b43f12013-08-20 11:48:36 +0200909/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000910
Paul Bakker33b43f12013-08-20 11:48:36 +0200911/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100912void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
913 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100914{
915 mbedtls_mpi X, A;
916 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
917
Janos Follath044a86b2015-10-25 10:58:03 +0100918 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100919
920 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
921 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
922 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
923
924 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
925 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
926 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
927
928 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100929 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
930 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
931
932exit:
933 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
934}
935/* END_CASE */
936
937
938/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100939void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
940 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000941{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_mpi X, Y, Z, A;
943 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
946 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
947 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
948 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
949 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000950
Gilles Peskine56f943a2020-07-23 01:18:11 +0200951 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
953 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200955
956 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
958 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000959
Paul Bakkerbd51b262014-07-10 15:26:12 +0200960exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +0200961 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000962}
Paul Bakker33b43f12013-08-20 11:48:36 +0200963/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000964
Paul Bakker33b43f12013-08-20 11:48:36 +0200965/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100966void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
967 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_mpi X, Z, A;
970 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
973 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
974 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
975 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000976
Paul Bakkerbd51b262014-07-10 15:26:12 +0200977exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000979}
Paul Bakker33b43f12013-08-20 11:48:36 +0200980/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000981
Paul Bakker33b43f12013-08-20 11:48:36 +0200982/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100983void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
984 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000985{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_mpi X, Y, Z, A;
987 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
990 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
991 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
992 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
993 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000994
Gilles Peskine56f943a2020-07-23 01:18:11 +0200995 /* result == first operand */
996 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
997 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
998 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
999
1000 /* result == second operand */
1001 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
1002 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1003
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( &Y ); 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_abs( int radix_X, char * input_X, int radix_Y,
1011 char * input_Y, int radix_A, char * input_A,
1012 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001015 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1019 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1020 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001023 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001024 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001026
Gilles Peskine56f943a2020-07-23 01:18:11 +02001027 /* result == first operand */
1028 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
1029 if( sub_result == 0 )
1030 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
1031 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1032
1033 /* result == second operand */
1034 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
1035 if( sub_result == 0 )
1036 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1037
Paul Bakkerbd51b262014-07-10 15:26:12 +02001038exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001040}
Paul Bakker33b43f12013-08-20 11:48:36 +02001041/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001042
Paul Bakker33b43f12013-08-20 11:48:36 +02001043/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001044void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
1045 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_mpi X, Z, A;
1048 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1051 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1052 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
1053 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001054
Paul Bakkerbd51b262014-07-10 15:26:12 +02001055exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001057}
Paul Bakker33b43f12013-08-20 11:48:36 +02001058/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001059
Paul Bakker33b43f12013-08-20 11:48:36 +02001060/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001061void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
1062 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_mpi X, Y, Z, A;
1065 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1068 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1069 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1070 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
1071 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001072
Paul Bakkerbd51b262014-07-10 15:26:12 +02001073exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001075}
Paul Bakker33b43f12013-08-20 11:48:36 +02001076/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001077
Paul Bakker33b43f12013-08-20 11:48:36 +02001078/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001079void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1080 int radix_A, char * input_A,
1081 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001082{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_mpi X, Z, A;
1084 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1087 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1088 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001089 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001091 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001093 else
1094 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001095
Paul Bakkerbd51b262014-07-10 15:26:12 +02001096exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001098}
Paul Bakker33b43f12013-08-20 11:48:36 +02001099/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001100
Paul Bakker33b43f12013-08-20 11:48:36 +02001101/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001102void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1103 char * input_Y, int radix_A, char * input_A,
1104 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001105{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001107 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1109 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1112 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1113 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1114 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1115 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001116 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001117 if( res == 0 )
1118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1120 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001121 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001122
Paul Bakkerbd51b262014-07-10 15:26:12 +02001123exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1125 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001126}
Paul Bakker33b43f12013-08-20 11:48:36 +02001127/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001128
Paul Bakker33b43f12013-08-20 11:48:36 +02001129/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001130void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1131 int radix_A, char * input_A, int radix_B,
1132 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001135 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1137 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1140 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1141 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1142 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001143 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001144 if( res == 0 )
1145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1147 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001148 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001149
Paul Bakkerbd51b262014-07-10 15:26:12 +02001150exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1152 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001153}
Paul Bakker33b43f12013-08-20 11:48:36 +02001154/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001155
Paul Bakker33b43f12013-08-20 11:48:36 +02001156/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001157void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1158 char * input_Y, int radix_A, char * input_A,
1159 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001162 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1166 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1167 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1168 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001169 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001170 if( res == 0 )
1171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001173 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001174
Paul Bakkerbd51b262014-07-10 15:26:12 +02001175exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001177}
Paul Bakker33b43f12013-08-20 11:48:36 +02001178/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001179
Paul Bakker33b43f12013-08-20 11:48:36 +02001180/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001181void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1182 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001185 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 mbedtls_mpi_uint r;
1187 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1190 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001191 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001192 if( res == 0 )
1193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001195 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001196
Paul Bakkerbd51b262014-07-10 15:26:12 +02001197exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001199}
Paul Bakker33b43f12013-08-20 11:48:36 +02001200/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001201
Paul Bakker33b43f12013-08-20 11:48:36 +02001202/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001203void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1204 char * input_E, int radix_N, char * input_N,
1205 int radix_RR, char * input_RR, int radix_X,
1206 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001209 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1211 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1214 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1215 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1216 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001217
Paul Bakker33b43f12013-08-20 11:48:36 +02001218 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001222 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001223 if( res == 0 )
1224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001226 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001227
Paul Bakkerbd51b262014-07-10 15:26:12 +02001228exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1230 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001231}
Paul Bakker33b43f12013-08-20 11:48:36 +02001232/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001233
Paul Bakker33b43f12013-08-20 11:48:36 +02001234/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001235void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesaa850cd2020-12-03 11:35:41 +00001236 int radix_RR, char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001237{
1238 mbedtls_mpi A, E, N, RR, Z;
1239 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1240 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1241
Chris Jonesaa850cd2020-12-03 11:35:41 +00001242 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001243 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001244 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001245 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001246
1247 /* Set E to 2^(E_bytes - 1) + 1 */
1248 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1249 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001250 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001251
1252 /* Set N to 2^(N_bytes - 1) + 1 */
1253 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1254 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001255 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1256
1257 if( strlen( input_RR ) )
1258 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
1259
Chris Jonesaa850cd2020-12-03 11:35:41 +00001260 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001261
1262exit:
1263 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1264 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1265}
1266/* END_CASE */
1267
1268/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001269void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1270 char * input_Y, int radix_A, char * input_A,
1271 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001274 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1278 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1279 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1280 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001281 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001282 if( res == 0 )
1283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001285 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001286
Paul Bakkerbd51b262014-07-10 15:26:12 +02001287exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001289}
Paul Bakker33b43f12013-08-20 11:48:36 +02001290/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001293void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001296 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001300 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001301 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001302
Paul Bakkerbd51b262014-07-10 15:26:12 +02001303exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001305}
Paul Bakker33b43f12013-08-20 11:48:36 +02001306/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001309void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001310 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001311{
1312 mbedtls_mpi X;
1313 int res;
1314 mbedtls_test_mpi_random rand;
1315
1316 mbedtls_mpi_init( &X );
1317 rand.data = witnesses;
1318 rand.pos = 0;
1319 rand.chunk_len = chunk_len;
1320
1321 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001322 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1323 mbedtls_test_mpi_miller_rabin_determinizer,
1324 &rand );
1325 TEST_ASSERT( res == 0 );
1326
1327 rand.data = witnesses;
1328 rand.pos = 0;
1329 rand.chunk_len = chunk_len;
1330
Janos Follatha0b67c22018-09-18 14:48:23 +01001331 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1332 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001333 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001334 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001335
1336exit:
1337 mbedtls_mpi_free( &X );
1338}
1339/* END_CASE */
1340
1341/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001342void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001343{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001345 int my_ret;
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001348
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001349 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1350 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001351 TEST_ASSERT( my_ret == ref_ret );
1352
1353 if( ref_ret == 0 )
1354 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001355 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001356
1357 TEST_ASSERT( actual_bits >= (size_t) bits );
1358 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1359
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001360 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1361 mbedtls_test_rnd_std_rand,
1362 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001363 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001364 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001365 /* X = ( X - 1 ) / 2 */
1366 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001367 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1368 mbedtls_test_rnd_std_rand,
1369 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001370 }
1371 }
1372
Paul Bakkerbd51b262014-07-10 15:26:12 +02001373exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001375}
1376/* END_CASE */
1377
Paul Bakker33b43f12013-08-20 11:48:36 +02001378/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001379void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1380 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 mbedtls_mpi X, A;
1383 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1386 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1387 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1388 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001389
Paul Bakkerbd51b262014-07-10 15:26:12 +02001390exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001392}
Paul Bakker33b43f12013-08-20 11:48:36 +02001393/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001394
Paul Bakker33b43f12013-08-20 11:48:36 +02001395/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001396void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1397 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_mpi X, A;
1400 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1403 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1404 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1405 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001406
Paul Bakkerbd51b262014-07-10 15:26:12 +02001407exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001409}
Paul Bakker33b43f12013-08-20 11:48:36 +02001410/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001411
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001412/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001413void mpi_fill_random( int wanted_bytes, int rng_bytes,
1414 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001415{
1416 mbedtls_mpi X;
1417 int ret;
1418 size_t bytes_left = rng_bytes;
1419 mbedtls_mpi_init( &X );
1420
Gilles Peskine422e8672021-04-02 00:02:27 +02001421 if( before != 0 )
1422 {
1423 /* Set X to sign(before) * 2^(|before|-1) */
1424 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1425 if( before < 0 )
1426 before = - before;
1427 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1428 }
1429
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001430 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1431 f_rng_bytes_left, &bytes_left );
1432 TEST_ASSERT( ret == expected_ret );
1433
1434 if( expected_ret == 0 )
1435 {
1436 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1437 * as a big-endian representation of the number. We know when
1438 * our RNG function returns null bytes, so we know how many
1439 * leading zero bytes the number has. */
1440 size_t leading_zeros = 0;
1441 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1442 leading_zeros = 1;
1443 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1444 (size_t) wanted_bytes );
1445 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
1446 }
1447
1448exit:
1449 mbedtls_mpi_free( &X );
1450}
1451/* END_CASE */
1452
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001453/* BEGIN_CASE */
1454void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1455{
1456 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1457 * This function assumes that the value of bound is at least 2 and
1458 * that iterations is large enough that a one-in-2^iterations chance
1459 * effectively never occurs.
1460 */
1461
1462 mbedtls_mpi upper_bound;
1463 size_t n_bits;
1464 mbedtls_mpi result;
1465 size_t b;
1466 /* If upper_bound is small, stats[b] is the number of times the value b
1467 * has been generated. Otherwise stats[b] is the number of times a
1468 * value with bit b set has been generated. */
1469 size_t *stats = NULL;
1470 size_t stats_len;
1471 int full_stats;
1472 size_t i;
1473
1474 mbedtls_mpi_init( &upper_bound );
1475 mbedtls_mpi_init( &result );
1476
1477 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1478 bound_bytes->x, bound_bytes->len ) );
1479 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1480 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1481 * to be small enough that the probability of missing one value is
1482 * negligible given the number of iterations. It must be less than
1483 * 256 because some of the code below assumes that "small" values
1484 * fit in a byte. */
1485 if( n_bits <= 5 )
1486 {
1487 full_stats = 1;
1488 stats_len = bound_bytes->x[bound_bytes->len - 1];
1489 }
1490 else
1491 {
1492 full_stats = 0;
1493 stats_len = n_bits;
1494 }
1495 ASSERT_ALLOC( stats, stats_len );
1496
1497 for( i = 0; i < (size_t) iterations; i++ )
1498 {
1499 mbedtls_test_set_step( i );
1500 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1501 mbedtls_test_rnd_std_rand, NULL ) );
1502
1503 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1504 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1505 if( full_stats )
1506 {
1507 uint8_t value;
1508 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1509 TEST_ASSERT( value < stats_len );
1510 ++stats[value];
1511 }
1512 else
1513 {
1514 for( b = 0; b < n_bits; b++ )
1515 stats[b] += mbedtls_mpi_get_bit( &result, b );
1516 }
1517 }
1518
1519 if( full_stats )
1520 {
1521 for( b = 1; b < stats_len; b++ )
1522 {
1523 mbedtls_test_set_step( 1000000 + b );
1524 /* Assert that each value has been reached at least once.
1525 * This is almost guaranteed if the iteration count is large
1526 * enough. This is a very crude way of checking the distribution.
1527 */
1528 TEST_ASSERT( stats[b] > 0 );
1529 }
1530 }
1531 else
1532 {
1533 for( b = 0; b < n_bits; b++ )
1534 {
1535 mbedtls_test_set_step( 1000000 + b );
1536 /* Assert that each bit has been set in at least one result and
1537 * clear in at least one result. Provided that iterations is not
1538 * too small, it would be extremely unlikely for this not to be
1539 * the case if the results are uniformly distributed.
1540 *
1541 * As an exception, the top bit may legitimately never be set
1542 * if bound is a power of 2 or only slightly above.
1543 */
1544 if( b != n_bits - 1 ||
1545 is_significantly_above_a_power_of_2( bound_bytes ) )
1546 {
1547 TEST_ASSERT( stats[b] > 0 );
1548 }
1549 TEST_ASSERT( stats[b] < (size_t) iterations );
1550 }
1551 }
1552
1553exit:
1554 mbedtls_mpi_free( &upper_bound );
1555 mbedtls_mpi_free( &result );
1556 mbedtls_free( stats );
1557}
1558/* END_CASE */
1559
Gilles Peskine1e918f42021-03-29 22:14:51 +02001560/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001561void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001562{
1563 mbedtls_mpi upper_bound;
1564 mbedtls_mpi result;
1565
1566 mbedtls_mpi_init( &upper_bound );
1567 mbedtls_mpi_init( &result );
1568
Gilles Peskine422e8672021-04-02 00:02:27 +02001569 if( before != 0 )
1570 {
1571 /* Set result to sign(before) * 2^(|before|-1) */
1572 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1573 if( before < 0 )
1574 before = - before;
1575 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1576 }
1577
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001578 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1579 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1580 bound_bytes->x, bound_bytes->len ) );
1581 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1582 mbedtls_test_rnd_std_rand, NULL ) );
1583 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1584 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1585
1586exit:
1587 mbedtls_mpi_free( &upper_bound );
1588 mbedtls_mpi_free( &result );
1589}
1590/* END_CASE */
1591
1592/* BEGIN_CASE */
Gilles Peskine1e918f42021-03-29 22:14:51 +02001593void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1594{
1595 mbedtls_mpi upper_bound;
1596 mbedtls_mpi result;
1597 int actual_ret;
1598
1599 mbedtls_mpi_init( &upper_bound );
1600 mbedtls_mpi_init( &result );
1601
1602 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1603 bound_bytes->x, bound_bytes->len ) );
1604 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
1605 mbedtls_test_rnd_std_rand, NULL );
1606 TEST_EQUAL( expected_ret, actual_ret );
1607
1608exit:
1609 mbedtls_mpi_free( &upper_bound );
1610 mbedtls_mpi_free( &result );
1611}
1612/* END_CASE */
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001615void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001616{
Andres AG93012e82016-09-09 09:10:28 +01001617 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001618}
Paul Bakker33b43f12013-08-20 11:48:36 +02001619/* END_CASE */