blob: a53d0cb9a5e092bd2f1ea0180b0a9e885f7f6e78 [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 */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100395void mbedtls_mpi_read_binary( data_t * buf, 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 ) );
Werner Lewis3e005f32022-07-07 11:38:44 +0100406 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdf336842022-08-01 13:55:41 +0100407 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 */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100415void mbedtls_mpi_read_binary_le( data_t * buf, char * input_A )
Janos Follatha778a942019-02-13 10:28:28 +0000416{
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 ) );
Werner Lewis3e005f32022-07-07 11:38:44 +0100426 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdf336842022-08-01 13:55:41 +0100427 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000428
429exit:
430 mbedtls_mpi_free( &X );
431}
432/* END_CASE */
433
434/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100435void mbedtls_mpi_write_binary( char * input_X, data_t * input_A,
436 int output_size, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000437{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000439 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000440 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000441
442 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000445
Werner Lewis24b60782022-07-07 15:08:17 +0100446 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200449 if( buflen > (size_t) output_size )
450 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200453 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000454 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000455
Ronald Cron2dbba992020-06-10 11:42:32 +0200456 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
457 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000458 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000459
Paul Bakkerbd51b262014-07-10 15:26:12 +0200460exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000462}
Paul Bakker33b43f12013-08-20 11:48:36 +0200463/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000464
Janos Follathe344d0f2019-02-19 16:17:40 +0000465/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100466void mbedtls_mpi_write_binary_le( char * input_X, data_t * input_A,
467 int output_size, int result )
Janos Follathe344d0f2019-02-19 16:17:40 +0000468{
469 mbedtls_mpi X;
470 unsigned char buf[1000];
471 size_t buflen;
472
473 memset( buf, 0x00, 1000 );
474
475 mbedtls_mpi_init( &X );
476
Werner Lewis24b60782022-07-07 15:08:17 +0100477 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000478
479 buflen = mbedtls_mpi_size( &X );
480 if( buflen > (size_t) output_size )
481 buflen = (size_t) output_size;
482
483 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
484 if( result == 0)
485 {
486
Ronald Cron2dbba992020-06-10 11:42:32 +0200487 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
488 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000489 }
490
491exit:
492 mbedtls_mpi_free( &X );
493}
494/* END_CASE */
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Werner Lewis3d52e442022-07-06 13:03:36 +0100497void mbedtls_mpi_read_file( char * input_file, data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000498{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000500 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000501 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000502 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000503 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000504
505 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000508
Paul Bakker33b43f12013-08-20 11:48:36 +0200509 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200510 TEST_ASSERT( file != NULL );
Werner Lewis3e005f32022-07-07 11:38:44 +0100511 ret = mbedtls_mpi_read_file( &X, 16, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000512 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000513 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000514
Paul Bakker33b43f12013-08-20 11:48:36 +0200515 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000516 {
Gilles Peskineb53b2182021-06-10 15:34:15 +0200517 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 buflen = mbedtls_mpi_size( &X );
519 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000520
Paul Bakkere896fea2009-07-06 06:40:23 +0000521
Ronald Cron2dbba992020-06-10 11:42:32 +0200522 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
523 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000524 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000525
Paul Bakkerbd51b262014-07-10 15:26:12 +0200526exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000528}
Paul Bakker33b43f12013-08-20 11:48:36 +0200529/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Werner Lewis3d52e442022-07-06 13:03:36 +0100532void mbedtls_mpi_write_file( char * input_X, char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000535 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200536 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000539
Werner Lewis24b60782022-07-07 15:08:17 +0100540 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000541
Paul Bakker33b43f12013-08-20 11:48:36 +0200542 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000543 TEST_ASSERT( file_out != NULL );
Werner Lewis3e005f32022-07-07 11:38:44 +0100544 ret = mbedtls_mpi_write_file( NULL, &X, 16, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000545 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200546 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000547
Paul Bakker33b43f12013-08-20 11:48:36 +0200548 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000549 TEST_ASSERT( file_in != NULL );
Werner Lewis3e005f32022-07-07 11:38:44 +0100550 ret = mbedtls_mpi_read_file( &Y, 16, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000551 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200552 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000555
Paul Bakkerbd51b262014-07-10 15:26:12 +0200556exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000558}
Paul Bakker33b43f12013-08-20 11:48:36 +0200559/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000560
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100562void mbedtls_mpi_get_bit( char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000563{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_mpi X;
565 mbedtls_mpi_init( &X );
Werner Lewis24b60782022-07-07 15:08:17 +0100566 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000568
Paul Bakkerbd51b262014-07-10 15:26:12 +0200569exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000571}
Paul Bakker33b43f12013-08-20 11:48:36 +0200572/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000573
Paul Bakker33b43f12013-08-20 11:48:36 +0200574/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100575void mbedtls_mpi_set_bit( char * input_X, int pos, int val,
576 char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000577{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_mpi X, Y;
579 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000580
Werner Lewis24b60782022-07-07 15:08:17 +0100581 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
582 TEST_ASSERT( mbedtls_test_read_mpi( &Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100583 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
584
585 if( result == 0 )
586 {
Gilles Peskineb53b2182021-06-10 15:34:15 +0200587 TEST_ASSERT( sign_is_valid( &X ) );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100588 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
589 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000590
Paul Bakkerbd51b262014-07-10 15:26:12 +0200591exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000593}
Paul Bakker33b43f12013-08-20 11:48:36 +0200594/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000595
Paul Bakker33b43f12013-08-20 11:48:36 +0200596/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100597void mbedtls_mpi_lsb( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_mpi X;
600 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000601
Werner Lewis24b60782022-07-07 15:08:17 +0100602 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000604
Paul Bakkerbd51b262014-07-10 15:26:12 +0200605exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000607}
Paul Bakker33b43f12013-08-20 11:48:36 +0200608/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000609
Paul Bakker33b43f12013-08-20 11:48:36 +0200610/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100611void mbedtls_mpi_bitlen( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000612{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_mpi X;
614 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000615
Werner Lewis24b60782022-07-07 15:08:17 +0100616 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200617 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000618
Paul Bakkerbd51b262014-07-10 15:26:12 +0200619exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000621}
Paul Bakker33b43f12013-08-20 11:48:36 +0200622/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000623
Paul Bakker33b43f12013-08-20 11:48:36 +0200624/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100625void mbedtls_mpi_gcd( char * input_X, char * input_Y,
626 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_mpi A, X, Y, Z;
629 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000630
Werner Lewis24b60782022-07-07 15:08:17 +0100631 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
632 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
633 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200635 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
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( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000645{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_mpi X;
647 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
650 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000651
Paul Bakkerbd51b262014-07-10 15:26:12 +0200652exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000654}
Paul Bakker33b43f12013-08-20 11:48:36 +0200655/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000656
Paul Bakker33b43f12013-08-20 11:48:36 +0200657/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100658void mbedtls_mpi_cmp_mpi( char * input_X, char * input_Y,
659 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000660{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_mpi X, Y;
662 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000663
Werner Lewis24b60782022-07-07 15:08:17 +0100664 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
665 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000667
Paul Bakkerbd51b262014-07-10 15:26:12 +0200668exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000670}
Paul Bakker33b43f12013-08-20 11:48:36 +0200671/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000672
Paul Bakker33b43f12013-08-20 11:48:36 +0200673/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100674void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
675 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100676 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100677{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200678 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100679 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100680 mbedtls_mpi X, Y;
681 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
682
Werner Lewis24b60782022-07-07 15:08:17 +0100683 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
684 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100685
Gilles Peskine9018b112020-01-21 16:30:53 +0100686 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
687 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100688
Janos Follath0e5532d2019-10-11 14:21:53 +0100689 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100690 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100691 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100692
693exit:
694 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
695}
696/* END_CASE */
697
698/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100699void mbedtls_mpi_cmp_abs( char * input_X, char * input_Y,
700 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000701{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_mpi X, Y;
703 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000704
Werner Lewis24b60782022-07-07 15:08:17 +0100705 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
706 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000708
Paul Bakkerbd51b262014-07-10 15:26:12 +0200709exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000711}
Paul Bakker33b43f12013-08-20 11:48:36 +0200712/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000713
Paul Bakker33b43f12013-08-20 11:48:36 +0200714/* BEGIN_CASE */
Gilles Peskine77f55c92021-06-10 15:17:30 +0200715void mbedtls_mpi_copy( char *src_hex, char *dst_hex )
Paul Bakker367dae42009-06-28 21:50:27 +0000716{
Gilles Peskine50231672021-06-10 23:00:33 +0200717 mbedtls_mpi src, dst, ref;
Gilles Peskine77f55c92021-06-10 15:17:30 +0200718 mbedtls_mpi_init( &src );
719 mbedtls_mpi_init( &dst );
Gilles Peskine50231672021-06-10 23:00:33 +0200720 mbedtls_mpi_init( &ref );
Paul Bakker367dae42009-06-28 21:50:27 +0000721
Werner Lewis24b60782022-07-07 15:08:17 +0100722 TEST_ASSERT( mbedtls_test_read_mpi( &src, src_hex ) == 0 );
723 TEST_ASSERT( mbedtls_test_read_mpi( &ref, dst_hex ) == 0 );
Gilles Peskine50231672021-06-10 23:00:33 +0200724
725 /* mbedtls_mpi_copy() */
Werner Lewis24b60782022-07-07 15:08:17 +0100726 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine77f55c92021-06-10 15:17:30 +0200727 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
Gilles Peskine77f55c92021-06-10 15:17:30 +0200728 TEST_ASSERT( sign_is_valid( &dst ) );
729 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000730
Gilles Peskine50231672021-06-10 23:00:33 +0200731 /* mbedtls_mpi_safe_cond_assign(), assignment done */
732 mbedtls_mpi_free( &dst );
Werner Lewis24b60782022-07-07 15:08:17 +0100733 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine50231672021-06-10 23:00:33 +0200734 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
735 TEST_ASSERT( sign_is_valid( &dst ) );
736 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
737
738 /* mbedtls_mpi_safe_cond_assign(), assignment not done */
739 mbedtls_mpi_free( &dst );
Werner Lewis24b60782022-07-07 15:08:17 +0100740 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine50231672021-06-10 23:00:33 +0200741 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
742 TEST_ASSERT( sign_is_valid( &dst ) );
743 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
744
Paul Bakkerbd51b262014-07-10 15:26:12 +0200745exit:
Gilles Peskine77f55c92021-06-10 15:17:30 +0200746 mbedtls_mpi_free( &src );
747 mbedtls_mpi_free( &dst );
Gilles Peskine50231672021-06-10 23:00:33 +0200748 mbedtls_mpi_free( &ref );
Gilles Peskine7428b452020-01-20 21:01:51 +0100749}
750/* END_CASE */
751
752/* BEGIN_CASE */
Gilles Peskine77f55c92021-06-10 15:17:30 +0200753void mpi_copy_self( char *input_X )
Gilles Peskine7428b452020-01-20 21:01:51 +0100754{
Gilles Peskine77f55c92021-06-10 15:17:30 +0200755 mbedtls_mpi X, A;
756 mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000758
Werner Lewis24b60782022-07-07 15:08:17 +0100759 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
Gilles Peskine77f55c92021-06-10 15:17:30 +0200761
Werner Lewis24b60782022-07-07 15:08:17 +0100762 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_X ) == 0 );
Gilles Peskine77f55c92021-06-10 15:17:30 +0200763 TEST_ASSERT( sign_is_valid( &X ) );
764 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000765
Paul Bakkerbd51b262014-07-10 15:26:12 +0200766exit:
Gilles Peskine77f55c92021-06-10 15:17:30 +0200767 mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000769}
Paul Bakker33b43f12013-08-20 11:48:36 +0200770/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000771
Paul Bakker33b43f12013-08-20 11:48:36 +0200772/* BEGIN_CASE */
Gilles Peskined382c282021-06-10 22:29:57 +0200773void mbedtls_mpi_swap( char *X_hex, char *Y_hex )
774{
775 mbedtls_mpi X, Y, X0, Y0;
776 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
777 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
778
Werner Lewis24b60782022-07-07 15:08:17 +0100779 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
780 TEST_ASSERT( mbedtls_test_read_mpi( &Y0, Y_hex ) == 0 );
Gilles Peskined382c282021-06-10 22:29:57 +0200781
Gilles Peskine50231672021-06-10 23:00:33 +0200782 /* mbedtls_mpi_swap() */
Werner Lewis24b60782022-07-07 15:08:17 +0100783 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
784 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskined382c282021-06-10 22:29:57 +0200785 mbedtls_mpi_swap( &X, &Y );
786 TEST_ASSERT( sign_is_valid( &X ) );
787 TEST_ASSERT( sign_is_valid( &Y ) );
788 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
789 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
790
Gilles Peskine50231672021-06-10 23:00:33 +0200791 /* mbedtls_mpi_safe_cond_swap(), swap done */
792 mbedtls_mpi_free( &X );
793 mbedtls_mpi_free( &Y );
Werner Lewis24b60782022-07-07 15:08:17 +0100794 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
795 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskine50231672021-06-10 23:00:33 +0200796 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
797 TEST_ASSERT( sign_is_valid( &X ) );
798 TEST_ASSERT( sign_is_valid( &Y ) );
799 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
800 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
801
802 /* mbedtls_mpi_safe_cond_swap(), swap not done */
803 mbedtls_mpi_free( &X );
804 mbedtls_mpi_free( &Y );
Werner Lewis24b60782022-07-07 15:08:17 +0100805 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
806 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskine50231672021-06-10 23:00:33 +0200807 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
808 TEST_ASSERT( sign_is_valid( &X ) );
809 TEST_ASSERT( sign_is_valid( &Y ) );
810 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
811 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
812
Gilles Peskined382c282021-06-10 22:29:57 +0200813exit:
814 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
815 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
816}
817/* END_CASE */
818
819/* BEGIN_CASE */
820void mpi_swap_self( char *X_hex )
821{
822 mbedtls_mpi X, X0;
823 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
824
Werner Lewis24b60782022-07-07 15:08:17 +0100825 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
826 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
Gilles Peskined382c282021-06-10 22:29:57 +0200827
828 mbedtls_mpi_swap( &X, &X );
829 TEST_ASSERT( sign_is_valid( &X ) );
830 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
831
832exit:
833 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
834}
835/* END_CASE */
836
837/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100839{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_mpi X;
841 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Gilles Peskine399c8fa2021-06-15 21:19:18 +0200844 if( used > 0 )
845 {
846 size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint );
847 TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 );
848 }
849 TEST_EQUAL( X.n, (size_t) before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Gilles Peskine399c8fa2021-06-15 21:19:18 +0200851 TEST_EQUAL( X.n, (size_t) after );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100852
Paul Bakkerbd51b262014-07-10 15:26:12 +0200853exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100855}
856/* END_CASE */
857
858/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100859void mbedtls_mpi_add_mpi( char * input_X, char * input_Y,
860 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000861{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_mpi X, Y, Z, A;
863 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000864
Werner Lewis24b60782022-07-07 15:08:17 +0100865 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
866 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
867 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200869 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000871
Gilles Peskine56f943a2020-07-23 01:18:11 +0200872 /* result == first operand */
873 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200874 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200875 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis24b60782022-07-07 15:08:17 +0100876 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200877
878 /* result == second operand */
879 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200880 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200881 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
882
Paul Bakkerbd51b262014-07-10 15:26:12 +0200883exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000885}
Paul Bakker33b43f12013-08-20 11:48:36 +0200886/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000887
Paul Bakker33b43f12013-08-20 11:48:36 +0200888/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100889void mbedtls_mpi_add_mpi_inplace( char * input_X, char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100890{
891 mbedtls_mpi X, A;
892 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
893
Werner Lewis24b60782022-07-07 15:08:17 +0100894 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100895
Werner Lewis24b60782022-07-07 15:08:17 +0100896 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100897 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
898 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200899 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100900
Werner Lewis24b60782022-07-07 15:08:17 +0100901 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100902 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200903 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100904 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
905
Werner Lewis24b60782022-07-07 15:08:17 +0100906 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100907 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200908 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +0100909 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
910
911exit:
912 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
913}
914/* END_CASE */
915
916
917/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100918void mbedtls_mpi_add_abs( char * input_X, char * input_Y,
919 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_mpi X, Y, Z, A;
922 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000923
Werner Lewis24b60782022-07-07 15:08:17 +0100924 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
925 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
926 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200928 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000930
Gilles Peskine56f943a2020-07-23 01:18:11 +0200931 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200933 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis24b60782022-07-07 15:08:17 +0100935 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200936
937 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200939 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000941
Paul Bakkerbd51b262014-07-10 15:26:12 +0200942exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +0200943 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000944}
Paul Bakker33b43f12013-08-20 11:48:36 +0200945/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000946
Paul Bakker33b43f12013-08-20 11:48:36 +0200947/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100948void mbedtls_mpi_add_int( char * input_X, int input_Y,
949 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000950{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_mpi X, Z, A;
952 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000953
Werner Lewis24b60782022-07-07 15:08:17 +0100954 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
955 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200957 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000959
Paul Bakkerbd51b262014-07-10 15:26:12 +0200960exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000962}
Paul Bakker33b43f12013-08-20 11:48:36 +0200963/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000964
Paul Bakker33b43f12013-08-20 11:48:36 +0200965/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100966void mbedtls_mpi_sub_mpi( char * input_X, char * input_Y,
967 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_mpi X, Y, Z, A;
970 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000971
Werner Lewis24b60782022-07-07 15:08:17 +0100972 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
973 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
974 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200976 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000978
Gilles Peskine56f943a2020-07-23 01:18:11 +0200979 /* result == first operand */
980 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200981 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200982 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis24b60782022-07-07 15:08:17 +0100983 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200984
985 /* result == second operand */
986 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200987 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200988 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
989
Paul Bakkerbd51b262014-07-10 15:26:12 +0200990exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000992}
Paul Bakker33b43f12013-08-20 11:48:36 +0200993/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000994
Paul Bakker33b43f12013-08-20 11:48:36 +0200995/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100996void mbedtls_mpi_sub_abs( char * input_X, char * input_Y,
997 char * input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001000 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001002
Werner Lewis24b60782022-07-07 15:08:17 +01001003 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1004 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1005 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001008 TEST_ASSERT( res == sub_result );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001009 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +00001010 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001012
Gilles Peskine56f943a2020-07-23 01:18:11 +02001013 /* result == first operand */
1014 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001015 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001016 if( sub_result == 0 )
1017 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis24b60782022-07-07 15:08:17 +01001018 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001019
1020 /* result == second operand */
1021 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001022 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001023 if( sub_result == 0 )
1024 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1025
Paul Bakkerbd51b262014-07-10 15:26:12 +02001026exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001028}
Paul Bakker33b43f12013-08-20 11:48:36 +02001029/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001030
Paul Bakker33b43f12013-08-20 11:48:36 +02001031/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001032void mbedtls_mpi_sub_int( char * input_X, int input_Y,
1033 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_mpi X, Z, A;
1036 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001037
Werner Lewis24b60782022-07-07 15:08:17 +01001038 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1039 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001041 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001043
Paul Bakkerbd51b262014-07-10 15:26:12 +02001044exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001046}
Paul Bakker33b43f12013-08-20 11:48:36 +02001047/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001048
Paul Bakker33b43f12013-08-20 11:48:36 +02001049/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001050void mbedtls_mpi_mul_mpi( char * input_X, char * input_Y,
1051 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_mpi X, Y, Z, A;
1054 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001055
Werner Lewis24b60782022-07-07 15:08:17 +01001056 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1057 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1058 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001060 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001062
Paul Bakkerbd51b262014-07-10 15:26:12 +02001063exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001065}
Paul Bakker33b43f12013-08-20 11:48:36 +02001066/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001067
Paul Bakker33b43f12013-08-20 11:48:36 +02001068/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001069void mbedtls_mpi_mul_int( char * input_X, int input_Y,
Werner Lewis3d52e442022-07-06 13:03:36 +01001070 char * input_A, char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_mpi X, Z, A;
1073 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001074
Werner Lewis24b60782022-07-07 15:08:17 +01001075 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1076 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001078 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001079 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001081 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001083 else
1084 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001085
Paul Bakkerbd51b262014-07-10 15:26:12 +02001086exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001088}
Paul Bakker33b43f12013-08-20 11:48:36 +02001089/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001090
Paul Bakker33b43f12013-08-20 11:48:36 +02001091/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001092void mbedtls_mpi_div_mpi( char * input_X, char * input_Y,
1093 char * input_A, char * input_B,
1094 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001097 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1099 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001100
Werner Lewis24b60782022-07-07 15:08:17 +01001101 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1102 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1103 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1104 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001106 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001107 if( res == 0 )
1108 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001109 TEST_ASSERT( sign_is_valid( &Q ) );
1110 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1112 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001113 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001114
Paul Bakkerbd51b262014-07-10 15:26:12 +02001115exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1117 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001118}
Paul Bakker33b43f12013-08-20 11:48:36 +02001119/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001120
Paul Bakker33b43f12013-08-20 11:48:36 +02001121/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001122void mbedtls_mpi_div_int( char * input_X, int input_Y,
Werner Lewis3d52e442022-07-06 13:03:36 +01001123 char * input_A, char * input_B,
1124 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001127 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1129 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001130
Werner Lewis24b60782022-07-07 15:08:17 +01001131 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1132 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1133 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001135 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001136 if( res == 0 )
1137 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001138 TEST_ASSERT( sign_is_valid( &Q ) );
1139 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1141 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001142 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001143
Paul Bakkerbd51b262014-07-10 15:26:12 +02001144exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1146 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001147}
Paul Bakker33b43f12013-08-20 11:48:36 +02001148/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001149
Paul Bakker33b43f12013-08-20 11:48:36 +02001150/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001151void mbedtls_mpi_mod_mpi( char * input_X, char * input_Y,
1152 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001155 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001157
Werner Lewis24b60782022-07-07 15:08:17 +01001158 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1159 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1160 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001162 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001163 if( res == 0 )
1164 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001165 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001167 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001168
Paul Bakkerbd51b262014-07-10 15:26:12 +02001169exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001171}
Paul Bakker33b43f12013-08-20 11:48:36 +02001172/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001173
Paul Bakker33b43f12013-08-20 11:48:36 +02001174/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001175void mbedtls_mpi_mod_int( char * input_X, int input_Y,
Azim Khanf1aaec92017-05-30 14:23:15 +01001176 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001179 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 mbedtls_mpi_uint r;
1181 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001182
Werner Lewis24b60782022-07-07 15:08:17 +01001183 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001185 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001186 if( res == 0 )
1187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001189 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001190
Paul Bakkerbd51b262014-07-10 15:26:12 +02001191exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 mbedtls_mpi_free( &X );
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 */
Werner Lewis3d52e442022-07-06 13:03:36 +01001197void mbedtls_mpi_exp_mod( char * input_A, char * input_E,
1198 char * input_N, char * input_X,
1199 int exp_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001202 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1204 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001205
Werner Lewis24b60782022-07-07 15:08:17 +01001206 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1207 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
1208 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
1209 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001210
Gilles Peskine4cc80212021-06-09 18:31:35 +02001211 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
Gilles Peskine3df05542021-06-15 21:55:05 +02001212 TEST_ASSERT( res == exp_result );
Gilles Peskine4cc80212021-06-09 18:31:35 +02001213 if( res == 0 )
1214 {
1215 TEST_ASSERT( sign_is_valid( &Z ) );
1216 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1217 }
1218
1219 /* Now test again with the speed-up parameter supplied as an output. */
1220 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine3df05542021-06-15 21:55:05 +02001221 TEST_ASSERT( res == exp_result );
Gilles Peskine4cc80212021-06-09 18:31:35 +02001222 if( res == 0 )
1223 {
1224 TEST_ASSERT( sign_is_valid( &Z ) );
1225 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1226 }
1227
1228 /* Now test again with the speed-up parameter supplied in calculated form. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine3df05542021-06-15 21:55:05 +02001230 TEST_ASSERT( res == exp_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001231 if( res == 0 )
1232 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001233 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001235 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001236
Paul Bakkerbd51b262014-07-10 15:26:12 +02001237exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1239 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); 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 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001244void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Werner Lewis955a0bb2022-07-07 15:09:15 +01001245 char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001246{
1247 mbedtls_mpi A, E, N, RR, Z;
1248 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1249 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1250
Chris Jonesaa850cd2020-12-03 11:35:41 +00001251 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001252 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001253 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001254 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001255
1256 /* Set E to 2^(E_bytes - 1) + 1 */
1257 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1258 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001259 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001260
1261 /* Set N to 2^(N_bytes - 1) + 1 */
1262 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1263 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001264 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1265
1266 if( strlen( input_RR ) )
Werner Lewis24b60782022-07-07 15:08:17 +01001267 TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001268
Chris Jonesaa850cd2020-12-03 11:35:41 +00001269 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001270
1271exit:
1272 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1273 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1274}
1275/* END_CASE */
1276
1277/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001278void mbedtls_mpi_inv_mod( char * input_X, char * input_Y,
1279 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001282 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001284
Werner Lewis24b60782022-07-07 15:08:17 +01001285 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1286 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1287 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001289 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001290 if( res == 0 )
1291 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001292 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001294 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001295
Paul Bakkerbd51b262014-07-10 15:26:12 +02001296exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001298}
Paul Bakker33b43f12013-08-20 11:48:36 +02001299/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001302void mbedtls_mpi_is_prime( char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001305 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001307
Werner Lewis24b60782022-07-07 15:08:17 +01001308 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001309 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001310 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001311
Paul Bakkerbd51b262014-07-10 15:26:12 +02001312exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001314}
Paul Bakker33b43f12013-08-20 11:48:36 +02001315/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001318void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001319 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001320{
1321 mbedtls_mpi X;
1322 int res;
1323 mbedtls_test_mpi_random rand;
1324
1325 mbedtls_mpi_init( &X );
1326 rand.data = witnesses;
1327 rand.pos = 0;
1328 rand.chunk_len = chunk_len;
1329
1330 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001331 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1332 mbedtls_test_mpi_miller_rabin_determinizer,
1333 &rand );
1334 TEST_ASSERT( res == 0 );
1335
1336 rand.data = witnesses;
1337 rand.pos = 0;
1338 rand.chunk_len = chunk_len;
1339
Janos Follatha0b67c22018-09-18 14:48:23 +01001340 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1341 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001342 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001343 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001344
1345exit:
1346 mbedtls_mpi_free( &X );
1347}
1348/* END_CASE */
1349
1350/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001351void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001354 int my_ret;
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001357
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001358 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1359 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001360 TEST_ASSERT( my_ret == ref_ret );
1361
1362 if( ref_ret == 0 )
1363 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001364 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001365
1366 TEST_ASSERT( actual_bits >= (size_t) bits );
1367 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001368 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001369
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001370 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1371 mbedtls_test_rnd_std_rand,
1372 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001373 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001374 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001375 /* X = ( X - 1 ) / 2 */
1376 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001377 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1378 mbedtls_test_rnd_std_rand,
1379 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001380 }
1381 }
1382
Paul Bakkerbd51b262014-07-10 15:26:12 +02001383exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001385}
1386/* END_CASE */
1387
Paul Bakker33b43f12013-08-20 11:48:36 +02001388/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001389void mbedtls_mpi_shift_l( char * input_X, int shift_X,
1390 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 mbedtls_mpi X, A;
1393 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001394
Werner Lewis24b60782022-07-07 15:08:17 +01001395 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1396 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001398 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001400
Paul Bakkerbd51b262014-07-10 15:26:12 +02001401exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001403}
Paul Bakker33b43f12013-08-20 11:48:36 +02001404/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001405
Paul Bakker33b43f12013-08-20 11:48:36 +02001406/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001407void mbedtls_mpi_shift_r( char * input_X, int shift_X,
1408 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 mbedtls_mpi X, A;
1411 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001412
Werner Lewis24b60782022-07-07 15:08:17 +01001413 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1414 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001416 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001418
Paul Bakkerbd51b262014-07-10 15:26:12 +02001419exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001421}
Paul Bakker33b43f12013-08-20 11:48:36 +02001422/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001423
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001424/* BEGIN_CASE */
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001425void mpi_fill_random( int wanted_bytes, int rng_bytes,
1426 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001427{
1428 mbedtls_mpi X;
1429 int ret;
1430 size_t bytes_left = rng_bytes;
1431 mbedtls_mpi_init( &X );
1432
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001433 if( before != 0 )
1434 {
1435 /* Set X to sign(before) * 2^(|before|-1) */
1436 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1437 if( before < 0 )
1438 before = - before;
1439 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1440 }
1441
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001442 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1443 f_rng_bytes_left, &bytes_left );
1444 TEST_ASSERT( ret == expected_ret );
1445
1446 if( expected_ret == 0 )
1447 {
1448 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1449 * as a big-endian representation of the number. We know when
1450 * our RNG function returns null bytes, so we know how many
1451 * leading zero bytes the number has. */
1452 size_t leading_zeros = 0;
1453 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1454 leading_zeros = 1;
1455 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1456 (size_t) wanted_bytes );
1457 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001458 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001459 }
1460
1461exit:
1462 mbedtls_mpi_free( &X );
1463}
1464/* END_CASE */
1465
Gilles Peskine4699fa42021-03-29 22:02:55 +02001466/* BEGIN_CASE */
1467void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1468{
1469 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1470 * This function assumes that the value of bound is at least 2 and
1471 * that iterations is large enough that a one-in-2^iterations chance
1472 * effectively never occurs.
1473 */
1474
1475 mbedtls_mpi upper_bound;
1476 size_t n_bits;
1477 mbedtls_mpi result;
1478 size_t b;
1479 /* If upper_bound is small, stats[b] is the number of times the value b
1480 * has been generated. Otherwise stats[b] is the number of times a
1481 * value with bit b set has been generated. */
1482 size_t *stats = NULL;
1483 size_t stats_len;
1484 int full_stats;
1485 size_t i;
1486
1487 mbedtls_mpi_init( &upper_bound );
1488 mbedtls_mpi_init( &result );
1489
1490 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1491 bound_bytes->x, bound_bytes->len ) );
1492 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1493 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1494 * to be small enough that the probability of missing one value is
1495 * negligible given the number of iterations. It must be less than
1496 * 256 because some of the code below assumes that "small" values
1497 * fit in a byte. */
1498 if( n_bits <= 5 )
1499 {
1500 full_stats = 1;
1501 stats_len = bound_bytes->x[bound_bytes->len - 1];
1502 }
1503 else
1504 {
1505 full_stats = 0;
1506 stats_len = n_bits;
1507 }
1508 ASSERT_ALLOC( stats, stats_len );
1509
1510 for( i = 0; i < (size_t) iterations; i++ )
1511 {
1512 mbedtls_test_set_step( i );
1513 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1514 mbedtls_test_rnd_std_rand, NULL ) );
1515
Gilles Peskineb53b2182021-06-10 15:34:15 +02001516 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine4699fa42021-03-29 22:02:55 +02001517 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1518 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1519 if( full_stats )
1520 {
1521 uint8_t value;
1522 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1523 TEST_ASSERT( value < stats_len );
1524 ++stats[value];
1525 }
1526 else
1527 {
1528 for( b = 0; b < n_bits; b++ )
1529 stats[b] += mbedtls_mpi_get_bit( &result, b );
1530 }
1531 }
1532
1533 if( full_stats )
1534 {
Gilles Peskinec520d7a2021-04-13 20:45:05 +02001535 for( b = min; b < stats_len; b++ )
Gilles Peskine4699fa42021-03-29 22:02:55 +02001536 {
1537 mbedtls_test_set_step( 1000000 + b );
1538 /* Assert that each value has been reached at least once.
1539 * This is almost guaranteed if the iteration count is large
1540 * enough. This is a very crude way of checking the distribution.
1541 */
1542 TEST_ASSERT( stats[b] > 0 );
1543 }
1544 }
1545 else
1546 {
Gilles Peskinee4f937f2021-06-02 21:24:04 +02001547 int statistically_safe_all_the_way =
1548 is_significantly_above_a_power_of_2( bound_bytes );
Gilles Peskine4699fa42021-03-29 22:02:55 +02001549 for( b = 0; b < n_bits; b++ )
1550 {
1551 mbedtls_test_set_step( 1000000 + b );
1552 /* Assert that each bit has been set in at least one result and
1553 * clear in at least one result. Provided that iterations is not
1554 * too small, it would be extremely unlikely for this not to be
1555 * the case if the results are uniformly distributed.
1556 *
1557 * As an exception, the top bit may legitimately never be set
1558 * if bound is a power of 2 or only slightly above.
1559 */
Gilles Peskinee4f937f2021-06-02 21:24:04 +02001560 if( statistically_safe_all_the_way || b != n_bits - 1 )
Gilles Peskine4699fa42021-03-29 22:02:55 +02001561 {
1562 TEST_ASSERT( stats[b] > 0 );
1563 }
1564 TEST_ASSERT( stats[b] < (size_t) iterations );
1565 }
1566 }
1567
1568exit:
1569 mbedtls_mpi_free( &upper_bound );
1570 mbedtls_mpi_free( &result );
1571 mbedtls_free( stats );
1572}
1573/* END_CASE */
1574
Gilles Peskine9312ba52021-03-29 22:14:51 +02001575/* BEGIN_CASE */
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001576void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine8f454702021-04-01 15:57:18 +02001577{
1578 mbedtls_mpi upper_bound;
1579 mbedtls_mpi result;
1580
1581 mbedtls_mpi_init( &upper_bound );
1582 mbedtls_mpi_init( &result );
1583
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001584 if( before != 0 )
1585 {
1586 /* Set result to sign(before) * 2^(|before|-1) */
1587 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1588 if( before < 0 )
1589 before = - before;
1590 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1591 }
1592
Gilles Peskine8f454702021-04-01 15:57:18 +02001593 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1594 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1595 bound_bytes->x, bound_bytes->len ) );
1596 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1597 mbedtls_test_rnd_std_rand, NULL ) );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001598 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine8f454702021-04-01 15:57:18 +02001599 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1600 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1601
1602exit:
1603 mbedtls_mpi_free( &upper_bound );
1604 mbedtls_mpi_free( &result );
1605}
1606/* END_CASE */
1607
1608/* BEGIN_CASE */
Gilles Peskine9312ba52021-03-29 22:14:51 +02001609void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1610{
1611 mbedtls_mpi upper_bound;
1612 mbedtls_mpi result;
1613 int actual_ret;
1614
1615 mbedtls_mpi_init( &upper_bound );
1616 mbedtls_mpi_init( &result );
1617
1618 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1619 bound_bytes->x, bound_bytes->len ) );
1620 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
1621 mbedtls_test_rnd_std_rand, NULL );
1622 TEST_EQUAL( expected_ret, actual_ret );
1623
1624exit:
1625 mbedtls_mpi_free( &upper_bound );
1626 mbedtls_mpi_free( &result );
1627}
1628/* END_CASE */
1629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001631void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001632{
Andres AG93012e82016-09-09 09:10:28 +01001633 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001634}
Paul Bakker33b43f12013-08-20 11:48:36 +02001635/* END_CASE */