blob: 83c8011daf7b4a6077128b5945daafca43b179e2 [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{
Gilles Peskine53a72062022-11-09 21:08:44 +010014 /* Only +1 and -1 are valid sign bits, not e.g. 0 */
Gilles Peskineb53b2182021-06-10 15:34:15 +020015 if( X->s != 1 && X->s != -1 )
Gilles Peskine53a72062022-11-09 21:08:44 +010016 return( 0 );
17
18 /* The value 0 must be represented with the sign +1. A "negative zero"
19 * with s=-1 is an invalid representation. Forbid that. As an exception,
20 * we sometimes test the robustness of library functions when given
21 * a negative zero input. If a test case has a negative zero as input,
22 * we don't mind if the function has a negative zero output. */
23 if( ! mbedtls_test_case_uses_negative_0 &&
24 mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 )
25 {
26 return( 0 );
27 }
28
Gilles Peskineb53b2182021-06-10 15:34:15 +020029 return( 1 );
30}
31
Janos Follath64eca052018-09-05 17:04:49 +010032typedef struct mbedtls_test_mpi_random
33{
34 data_t *data;
35 size_t pos;
36 size_t chunk_len;
37} mbedtls_test_mpi_random;
38
39/*
40 * This function is called by the Miller-Rabin primality test each time it
41 * chooses a random witness. The witnesses (or non-witnesses as provided by the
42 * test) are stored in the data member of the state structure. Each number is in
43 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
44 */
45int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
46 unsigned char* buf,
47 size_t len )
48{
49 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
50
51 if( random == NULL || random->data->x == NULL || buf == NULL )
52 return( -1 );
53
54 if( random->pos + random->chunk_len > random->data->len
55 || random->chunk_len > len )
56 {
57 return( -1 );
58 }
59
60 memset( buf, 0, len );
61
62 /* The witness is written to the end of the buffer, since the buffer is
63 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
64 * Writing the witness to the start of the buffer would result in the
65 * buffer being 'witness 000...000', which would be treated as
66 * witness * 2^n for some n. */
67 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
68 random->chunk_len );
69
70 random->pos += random->chunk_len;
71
72 return( 0 );
73}
Gilles Peskine3cb1e292020-11-25 15:37:20 +010074
75/* Random generator that is told how many bytes to return. */
76static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
77{
78 size_t *bytes_left = state;
79 size_t i;
80 for( i = 0; i < len; i++ )
81 {
82 if( *bytes_left == 0 )
83 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
84 buf[i] = *bytes_left & 0xff;
85 --( *bytes_left );
86 }
87 return( 0 );
88}
89
Gilles Peskine3b056152021-04-13 19:50:04 +020090/* Test whether bytes represents (in big-endian base 256) a number b that
91 * is significantly above a power of 2. That is, b must not have a long run
92 * of unset bits after the most significant bit.
93 *
94 * Let n be the bit-size of b, i.e. the integer such that 2^n <= b < 2^{n+1}.
95 * This function returns 1 if, when drawing a number between 0 and b,
96 * the probability that this number is at least 2^n is not negligible.
97 * This probability is (b - 2^n) / b and this function checks that this
98 * number is above some threshold A. The threshold value is heuristic and
99 * based on the needs of mpi_random_many().
Gilles Peskine4699fa42021-03-29 22:02:55 +0200100 */
101static int is_significantly_above_a_power_of_2( data_t *bytes )
102{
103 const uint8_t *p = bytes->x;
104 size_t len = bytes->len;
105 unsigned x;
Gilles Peskine3b056152021-04-13 19:50:04 +0200106
107 /* Skip leading null bytes */
Gilles Peskine4699fa42021-03-29 22:02:55 +0200108 while( len > 0 && p[0] == 0 )
109 {
110 ++p;
111 --len;
112 }
Gilles Peskine3b056152021-04-13 19:50:04 +0200113 /* 0 is not significantly above a power of 2 */
Gilles Peskine4699fa42021-03-29 22:02:55 +0200114 if( len == 0 )
115 return( 0 );
Gilles Peskine3b056152021-04-13 19:50:04 +0200116 /* Extract the (up to) 2 most significant bytes */
117 if( len == 1 )
Gilles Peskine4699fa42021-03-29 22:02:55 +0200118 x = p[0];
119 else
120 x = ( p[0] << 8 ) | p[1];
121
Gilles Peskine3b056152021-04-13 19:50:04 +0200122 /* Shift the most significant bit of x to position 8 and mask it out */
123 while( ( x & 0xfe00 ) != 0 )
124 x >>= 1;
125 x &= 0x00ff;
Gilles Peskine4699fa42021-03-29 22:02:55 +0200126
Gilles Peskine3b056152021-04-13 19:50:04 +0200127 /* At this point, x = floor((b - 2^n) / 2^(n-8)). b is significantly above
128 * a power of 2 iff x is significantly above 0 compared to 2^8.
129 * Testing x >= 2^4 amounts to picking A = 1/16 in the function
130 * description above. */
131 return( x >= 0x10 );
Gilles Peskine4699fa42021-03-29 22:02:55 +0200132}
133
Paul Bakker33b43f12013-08-20 11:48:36 +0200134/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +0000135
Paul Bakker33b43f12013-08-20 11:48:36 +0200136/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200138 * END_DEPENDENCIES
139 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000140
Hanno Beckerb48e1aa2018-12-18 23:25:01 +0000141/* BEGIN_CASE */
142void mpi_valid_param( )
143{
144 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
145}
146/* END_CASE */
147
Hanno Beckerafb607b2018-12-11 14:27:08 +0000148/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
149void mpi_invalid_param( )
150{
151 mbedtls_mpi X;
152 const char *s_in = "00101000101010";
153 char s_out[16] = { 0 };
154 unsigned char u_out[16] = { 0 };
155 unsigned char u_in[16] = { 0 };
156 size_t olen;
157 mbedtls_mpi_uint mpi_uint;
158
159 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000160
Hanno Beckerafb607b2018-12-11 14:27:08 +0000161 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
162 mbedtls_mpi_grow( NULL, 42 ) );
163 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
164 mbedtls_mpi_copy( NULL, &X ) );
165 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
166 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000167
Hanno Beckerafb607b2018-12-11 14:27:08 +0000168 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
169 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000170
Hanno Beckerafb607b2018-12-11 14:27:08 +0000171 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
172 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
173 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
174 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000175
Hanno Beckerafb607b2018-12-11 14:27:08 +0000176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
177 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
179 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000180
Hanno Beckerafb607b2018-12-11 14:27:08 +0000181 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
182 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000183
Hanno Beckerafb607b2018-12-11 14:27:08 +0000184 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
185 mbedtls_mpi_get_bit( NULL, 42 ) );
186 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
187 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000188
Hanno Beckerafb607b2018-12-11 14:27:08 +0000189 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
190 mbedtls_mpi_read_string( NULL, 2, s_in ) );
191 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
192 mbedtls_mpi_read_string( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000193
Hanno Beckerafb607b2018-12-11 14:27:08 +0000194 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
195 mbedtls_mpi_write_string( NULL, 2,
196 s_out, sizeof( s_out ),
197 &olen ) );
198 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
199 mbedtls_mpi_write_string( &X, 2,
200 NULL, sizeof( s_out ),
201 &olen ) );
202 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
203 mbedtls_mpi_write_string( &X, 2,
204 s_out, sizeof( s_out ),
205 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000206
Hanno Beckerafb607b2018-12-11 14:27:08 +0000207#if defined(MBEDTLS_FS_IO)
208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
209 mbedtls_mpi_read_file( NULL, 2, stdin ) );
210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
211 mbedtls_mpi_read_file( &X, 2, NULL ) );
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_file( "", NULL, 2, NULL ) );
215#endif /* MBEDTLS_FS_IO */
216
217 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
218 mbedtls_mpi_read_binary( NULL, u_in,
219 sizeof( u_in ) ) );
220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
221 mbedtls_mpi_read_binary( &X, NULL,
222 sizeof( u_in ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000223
Hanno Beckerafb607b2018-12-11 14:27:08 +0000224 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
225 mbedtls_mpi_write_binary( NULL, u_out,
226 sizeof( u_out ) ) );
227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
228 mbedtls_mpi_write_binary( &X, NULL,
229 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000230
Hanno Beckerafb607b2018-12-11 14:27:08 +0000231 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
232 mbedtls_mpi_shift_l( NULL, 42 ) );
233 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
234 mbedtls_mpi_shift_r( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000235
Hanno Beckerafb607b2018-12-11 14:27:08 +0000236 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
237 mbedtls_mpi_cmp_abs( NULL, &X ) );
238 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
239 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000240
Hanno Beckerafb607b2018-12-11 14:27:08 +0000241 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
242 mbedtls_mpi_cmp_mpi( NULL, &X ) );
243 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
244 mbedtls_mpi_cmp_mpi( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000245
Hanno Beckerafb607b2018-12-11 14:27:08 +0000246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
247 mbedtls_mpi_cmp_int( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000248
Hanno Beckerafb607b2018-12-11 14:27:08 +0000249 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
250 mbedtls_mpi_add_abs( NULL, &X, &X ) );
251 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
252 mbedtls_mpi_add_abs( &X, NULL, &X ) );
253 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
254 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000255
Hanno Beckerafb607b2018-12-11 14:27:08 +0000256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
259 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
261 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000262
Hanno Beckerafb607b2018-12-11 14:27:08 +0000263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
264 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
265 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
266 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
267 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
268 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000269
Hanno Beckerafb607b2018-12-11 14:27:08 +0000270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
271 mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
272 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
273 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
274 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
275 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000276
Hanno Beckerafb607b2018-12-11 14:27:08 +0000277 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
278 mbedtls_mpi_add_int( NULL, &X, 42 ) );
279 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
280 mbedtls_mpi_add_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000281
Hanno Beckerafb607b2018-12-11 14:27:08 +0000282 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
283 mbedtls_mpi_sub_int( NULL, &X, 42 ) );
284 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
285 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000286
Hanno Beckerafb607b2018-12-11 14:27:08 +0000287 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
288 mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
289 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
290 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
291 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
292 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000293
Hanno Beckerafb607b2018-12-11 14:27:08 +0000294 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
295 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
296 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
297 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000298
Hanno Beckerafb607b2018-12-11 14:27:08 +0000299 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
300 mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) );
301 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
302 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000303
Hanno Beckerafb607b2018-12-11 14:27:08 +0000304 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
305 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000306
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000307 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
308
Hanno Beckerafb607b2018-12-11 14:27:08 +0000309 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
310 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
311 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
312 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
313 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
314 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000315
Hanno Beckerafb607b2018-12-11 14:27:08 +0000316 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
317 mbedtls_mpi_mod_int( NULL, &X, 42 ) );
318 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
319 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000320
Hanno Beckerafb607b2018-12-11 14:27:08 +0000321 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
322 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
323 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
324 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
325 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
326 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
327 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
328 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000329
Hanno Beckerafb607b2018-12-11 14:27:08 +0000330 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200331 mbedtls_mpi_fill_random( NULL, 42,
332 mbedtls_test_rnd_std_rand,
Hanno Beckerafb607b2018-12-11 14:27:08 +0000333 NULL ) );
334 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
335 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000336
Hanno Beckerafb607b2018-12-11 14:27:08 +0000337 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
338 mbedtls_mpi_gcd( NULL, &X, &X ) );
339 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
340 mbedtls_mpi_gcd( &X, NULL, &X ) );
341 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
342 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000343
Hanno Beckerafb607b2018-12-11 14:27:08 +0000344 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
345 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
346 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
347 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
348 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000349 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000350
351exit:
352 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000353}
354/* END_CASE */
355
Paul Bakker33b43f12013-08-20 11:48:36 +0200356/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100357void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200358{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200359 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200360
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200361 mbedtls_mpi_init( &X );
362 mbedtls_mpi_init( &Y );
363 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200364
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200365 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
366 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200367 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200368 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200369
370exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200371 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200372}
373/* END_CASE */
374
375/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100376void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
377 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200378 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000381 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100382 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000385
Janos Follath04dadb72019-03-06 12:29:37 +0000386 memset( str, '!', sizeof( str ) );
387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200389 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000390 {
Gilles Peskineb53b2182021-06-10 15:34:15 +0200391 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100392 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200393 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000394 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200395 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000396 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000397 }
398 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000399
Paul Bakkerbd51b262014-07-10 15:26:12 +0200400exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000402}
Paul Bakker33b43f12013-08-20 11:48:36 +0200403/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000404
Paul Bakker33b43f12013-08-20 11:48:36 +0200405/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100406void mbedtls_mpi_read_binary( data_t * buf, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000407{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000409 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100410 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000413
Paul Bakkere896fea2009-07-06 06:40:23 +0000414
Azim Khand30ca132017-06-09 04:32:58 +0100415 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200416 TEST_ASSERT( sign_is_valid( &X ) );
Werner Lewis3e005f32022-07-07 11:38:44 +0100417 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdf336842022-08-01 13:55:41 +0100418 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000419
Paul Bakkerbd51b262014-07-10 15:26:12 +0200420exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000422}
Paul Bakker33b43f12013-08-20 11:48:36 +0200423/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000424
Paul Bakker33b43f12013-08-20 11:48:36 +0200425/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100426void mbedtls_mpi_read_binary_le( data_t * buf, char * input_A )
Janos Follatha778a942019-02-13 10:28:28 +0000427{
428 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000429 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000430 size_t len;
431
432 mbedtls_mpi_init( &X );
433
434
435 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200436 TEST_ASSERT( sign_is_valid( &X ) );
Werner Lewis3e005f32022-07-07 11:38:44 +0100437 TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 );
Werner Lewisdf336842022-08-01 13:55:41 +0100438 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000439
440exit:
441 mbedtls_mpi_free( &X );
442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100446void mbedtls_mpi_write_binary( char * input_X, data_t * input_A,
447 int output_size, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000450 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000451 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000452
453 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000456
Werner Lewis24b60782022-07-07 15:08:17 +0100457 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200460 if( buflen > (size_t) output_size )
461 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200464 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000465 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000466
Ronald Cron2dbba992020-06-10 11:42:32 +0200467 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
468 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000469 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000470
Paul Bakkerbd51b262014-07-10 15:26:12 +0200471exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000473}
Paul Bakker33b43f12013-08-20 11:48:36 +0200474/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000475
Janos Follathe344d0f2019-02-19 16:17:40 +0000476/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100477void mbedtls_mpi_write_binary_le( char * input_X, data_t * input_A,
478 int output_size, int result )
Janos Follathe344d0f2019-02-19 16:17:40 +0000479{
480 mbedtls_mpi X;
481 unsigned char buf[1000];
482 size_t buflen;
483
484 memset( buf, 0x00, 1000 );
485
486 mbedtls_mpi_init( &X );
487
Werner Lewis24b60782022-07-07 15:08:17 +0100488 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000489
490 buflen = mbedtls_mpi_size( &X );
491 if( buflen > (size_t) output_size )
492 buflen = (size_t) output_size;
493
494 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
495 if( result == 0)
496 {
497
Ronald Cron2dbba992020-06-10 11:42:32 +0200498 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
499 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000500 }
501
502exit:
503 mbedtls_mpi_free( &X );
504}
505/* END_CASE */
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Werner Lewis3d52e442022-07-06 13:03:36 +0100508void mbedtls_mpi_read_file( char * input_file, data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000509{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000511 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000512 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000513 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000514 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000515
516 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000519
Paul Bakker33b43f12013-08-20 11:48:36 +0200520 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200521 TEST_ASSERT( file != NULL );
Werner Lewis3e005f32022-07-07 11:38:44 +0100522 ret = mbedtls_mpi_read_file( &X, 16, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000523 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000524 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000525
Paul Bakker33b43f12013-08-20 11:48:36 +0200526 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000527 {
Gilles Peskineb53b2182021-06-10 15:34:15 +0200528 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 buflen = mbedtls_mpi_size( &X );
530 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000531
Paul Bakkere896fea2009-07-06 06:40:23 +0000532
Ronald Cron2dbba992020-06-10 11:42:32 +0200533 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
534 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000535 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000536
Paul Bakkerbd51b262014-07-10 15:26:12 +0200537exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000539}
Paul Bakker33b43f12013-08-20 11:48:36 +0200540/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Werner Lewis3d52e442022-07-06 13:03:36 +0100543void mbedtls_mpi_write_file( char * input_X, char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000546 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200547 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000550
Werner Lewis24b60782022-07-07 15:08:17 +0100551 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000552
Paul Bakker33b43f12013-08-20 11:48:36 +0200553 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000554 TEST_ASSERT( file_out != NULL );
Werner Lewis3e005f32022-07-07 11:38:44 +0100555 ret = mbedtls_mpi_write_file( NULL, &X, 16, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000556 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200557 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000558
Paul Bakker33b43f12013-08-20 11:48:36 +0200559 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000560 TEST_ASSERT( file_in != NULL );
Werner Lewis3e005f32022-07-07 11:38:44 +0100561 ret = mbedtls_mpi_read_file( &Y, 16, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000562 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200563 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000566
Paul Bakkerbd51b262014-07-10 15:26:12 +0200567exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000569}
Paul Bakker33b43f12013-08-20 11:48:36 +0200570/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000571
Paul Bakker33b43f12013-08-20 11:48:36 +0200572/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100573void mbedtls_mpi_get_bit( char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000574{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_mpi X;
576 mbedtls_mpi_init( &X );
Werner Lewis24b60782022-07-07 15:08:17 +0100577 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000579
Paul Bakkerbd51b262014-07-10 15:26:12 +0200580exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000582}
Paul Bakker33b43f12013-08-20 11:48:36 +0200583/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000584
Paul Bakker33b43f12013-08-20 11:48:36 +0200585/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100586void mbedtls_mpi_set_bit( char * input_X, int pos, int val,
587 char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_mpi X, Y;
590 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000591
Werner Lewis24b60782022-07-07 15:08:17 +0100592 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
593 TEST_ASSERT( mbedtls_test_read_mpi( &Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100594 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
595
596 if( result == 0 )
597 {
Gilles Peskineb53b2182021-06-10 15:34:15 +0200598 TEST_ASSERT( sign_is_valid( &X ) );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100599 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
600 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000601
Paul Bakkerbd51b262014-07-10 15:26:12 +0200602exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000604}
Paul Bakker33b43f12013-08-20 11:48:36 +0200605/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000606
Paul Bakker33b43f12013-08-20 11:48:36 +0200607/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100608void mbedtls_mpi_lsb( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_mpi X;
611 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000612
Werner Lewis24b60782022-07-07 15:08:17 +0100613 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000615
Paul Bakkerbd51b262014-07-10 15:26:12 +0200616exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000618}
Paul Bakker33b43f12013-08-20 11:48:36 +0200619/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000620
Paul Bakker33b43f12013-08-20 11:48:36 +0200621/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100622void mbedtls_mpi_bitlen( char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000623{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_mpi X;
625 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000626
Werner Lewis24b60782022-07-07 15:08:17 +0100627 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200628 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000629
Paul Bakkerbd51b262014-07-10 15:26:12 +0200630exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000632}
Paul Bakker33b43f12013-08-20 11:48:36 +0200633/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000634
Paul Bakker33b43f12013-08-20 11:48:36 +0200635/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100636void mbedtls_mpi_gcd( char * input_X, char * input_Y,
637 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_mpi A, X, Y, Z;
640 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000641
Werner Lewis24b60782022-07-07 15:08:17 +0100642 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
643 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
644 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200646 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000648
Paul Bakkerbd51b262014-07-10 15:26:12 +0200649exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000651}
Paul Bakker33b43f12013-08-20 11:48:36 +0200652/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000653
Paul Bakker33b43f12013-08-20 11:48:36 +0200654/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_mpi X;
658 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
661 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000662
Paul Bakkerbd51b262014-07-10 15:26:12 +0200663exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000665}
Paul Bakker33b43f12013-08-20 11:48:36 +0200666/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000667
Paul Bakker33b43f12013-08-20 11:48:36 +0200668/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100669void mbedtls_mpi_cmp_mpi( char * input_X, char * input_Y,
670 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000671{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_mpi X, Y;
673 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000674
Werner Lewis24b60782022-07-07 15:08:17 +0100675 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
676 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000678
Paul Bakkerbd51b262014-07-10 15:26:12 +0200679exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000681}
Paul Bakker33b43f12013-08-20 11:48:36 +0200682/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000683
Paul Bakker33b43f12013-08-20 11:48:36 +0200684/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100685void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
686 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100687 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100688{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200689 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100690 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100691 mbedtls_mpi X, Y;
692 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
693
Werner Lewis24b60782022-07-07 15:08:17 +0100694 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
695 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100696
Gilles Peskine9018b112020-01-21 16:30:53 +0100697 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
698 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100699
Janos Follath0e5532d2019-10-11 14:21:53 +0100700 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100701 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100702 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100703
704exit:
705 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
706}
707/* END_CASE */
708
709/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100710void mbedtls_mpi_cmp_abs( char * input_X, char * input_Y,
711 int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000712{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_mpi X, Y;
714 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000715
Werner Lewis24b60782022-07-07 15:08:17 +0100716 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
717 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000719
Paul Bakkerbd51b262014-07-10 15:26:12 +0200720exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000722}
Paul Bakker33b43f12013-08-20 11:48:36 +0200723/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000724
Paul Bakker33b43f12013-08-20 11:48:36 +0200725/* BEGIN_CASE */
Gilles Peskine77f55c92021-06-10 15:17:30 +0200726void mbedtls_mpi_copy( char *src_hex, char *dst_hex )
Paul Bakker367dae42009-06-28 21:50:27 +0000727{
Gilles Peskine50231672021-06-10 23:00:33 +0200728 mbedtls_mpi src, dst, ref;
Gilles Peskine77f55c92021-06-10 15:17:30 +0200729 mbedtls_mpi_init( &src );
730 mbedtls_mpi_init( &dst );
Gilles Peskine50231672021-06-10 23:00:33 +0200731 mbedtls_mpi_init( &ref );
Paul Bakker367dae42009-06-28 21:50:27 +0000732
Werner Lewis24b60782022-07-07 15:08:17 +0100733 TEST_ASSERT( mbedtls_test_read_mpi( &src, src_hex ) == 0 );
734 TEST_ASSERT( mbedtls_test_read_mpi( &ref, dst_hex ) == 0 );
Gilles Peskine50231672021-06-10 23:00:33 +0200735
736 /* mbedtls_mpi_copy() */
Werner Lewis24b60782022-07-07 15:08:17 +0100737 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine77f55c92021-06-10 15:17:30 +0200738 TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
Gilles Peskine77f55c92021-06-10 15:17:30 +0200739 TEST_ASSERT( sign_is_valid( &dst ) );
740 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000741
Gilles Peskine50231672021-06-10 23:00:33 +0200742 /* mbedtls_mpi_safe_cond_assign(), assignment done */
743 mbedtls_mpi_free( &dst );
Werner Lewis24b60782022-07-07 15:08:17 +0100744 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine50231672021-06-10 23:00:33 +0200745 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
746 TEST_ASSERT( sign_is_valid( &dst ) );
747 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
748
749 /* mbedtls_mpi_safe_cond_assign(), assignment not done */
750 mbedtls_mpi_free( &dst );
Werner Lewis24b60782022-07-07 15:08:17 +0100751 TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 );
Gilles Peskine50231672021-06-10 23:00:33 +0200752 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
753 TEST_ASSERT( sign_is_valid( &dst ) );
754 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
755
Paul Bakkerbd51b262014-07-10 15:26:12 +0200756exit:
Gilles Peskine77f55c92021-06-10 15:17:30 +0200757 mbedtls_mpi_free( &src );
758 mbedtls_mpi_free( &dst );
Gilles Peskine50231672021-06-10 23:00:33 +0200759 mbedtls_mpi_free( &ref );
Gilles Peskine7428b452020-01-20 21:01:51 +0100760}
761/* END_CASE */
762
763/* BEGIN_CASE */
Gilles Peskine77f55c92021-06-10 15:17:30 +0200764void mpi_copy_self( char *input_X )
Gilles Peskine7428b452020-01-20 21:01:51 +0100765{
Gilles Peskine77f55c92021-06-10 15:17:30 +0200766 mbedtls_mpi X, A;
767 mbedtls_mpi_init( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000769
Werner Lewis24b60782022-07-07 15:08:17 +0100770 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
Gilles Peskine77f55c92021-06-10 15:17:30 +0200772
Werner Lewis24b60782022-07-07 15:08:17 +0100773 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_X ) == 0 );
Gilles Peskine77f55c92021-06-10 15:17:30 +0200774 TEST_ASSERT( sign_is_valid( &X ) );
775 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000776
Paul Bakkerbd51b262014-07-10 15:26:12 +0200777exit:
Gilles Peskine77f55c92021-06-10 15:17:30 +0200778 mbedtls_mpi_free( &A );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000780}
Paul Bakker33b43f12013-08-20 11:48:36 +0200781/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000782
Paul Bakker33b43f12013-08-20 11:48:36 +0200783/* BEGIN_CASE */
Gilles Peskined382c282021-06-10 22:29:57 +0200784void mbedtls_mpi_swap( char *X_hex, char *Y_hex )
785{
786 mbedtls_mpi X, Y, X0, Y0;
787 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
788 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
789
Werner Lewis24b60782022-07-07 15:08:17 +0100790 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
791 TEST_ASSERT( mbedtls_test_read_mpi( &Y0, Y_hex ) == 0 );
Gilles Peskined382c282021-06-10 22:29:57 +0200792
Gilles Peskine50231672021-06-10 23:00:33 +0200793 /* mbedtls_mpi_swap() */
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 Peskined382c282021-06-10 22:29:57 +0200796 mbedtls_mpi_swap( &X, &Y );
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
Gilles Peskine50231672021-06-10 23:00:33 +0200802 /* mbedtls_mpi_safe_cond_swap(), swap 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, 1 ) == 0 );
808 TEST_ASSERT( sign_is_valid( &X ) );
809 TEST_ASSERT( sign_is_valid( &Y ) );
810 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
811 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
812
813 /* mbedtls_mpi_safe_cond_swap(), swap not done */
814 mbedtls_mpi_free( &X );
815 mbedtls_mpi_free( &Y );
Werner Lewis24b60782022-07-07 15:08:17 +0100816 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
817 TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 );
Gilles Peskine50231672021-06-10 23:00:33 +0200818 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
819 TEST_ASSERT( sign_is_valid( &X ) );
820 TEST_ASSERT( sign_is_valid( &Y ) );
821 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
822 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
823
Gilles Peskined382c282021-06-10 22:29:57 +0200824exit:
825 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
826 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
827}
828/* END_CASE */
829
830/* BEGIN_CASE */
831void mpi_swap_self( char *X_hex )
832{
833 mbedtls_mpi X, X0;
834 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
835
Werner Lewis24b60782022-07-07 15:08:17 +0100836 TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 );
837 TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 );
Gilles Peskined382c282021-06-10 22:29:57 +0200838
839 mbedtls_mpi_swap( &X, &X );
840 TEST_ASSERT( sign_is_valid( &X ) );
841 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
842
843exit:
844 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
845}
846/* END_CASE */
847
848/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100850{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_mpi X;
852 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Gilles Peskine399c8fa2021-06-15 21:19:18 +0200855 if( used > 0 )
856 {
857 size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint );
858 TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 );
859 }
860 TEST_EQUAL( X.n, (size_t) before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Gilles Peskine399c8fa2021-06-15 21:19:18 +0200862 TEST_EQUAL( X.n, (size_t) after );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100863
Paul Bakkerbd51b262014-07-10 15:26:12 +0200864exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100866}
867/* END_CASE */
868
869/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100870void mbedtls_mpi_add_mpi( char * input_X, char * input_Y,
871 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000872{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_mpi X, Y, Z, A;
874 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000875
Werner Lewis24b60782022-07-07 15:08:17 +0100876 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
877 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
878 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200880 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000882
Gilles Peskine56f943a2020-07-23 01:18:11 +0200883 /* result == first operand */
884 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200885 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200886 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis24b60782022-07-07 15:08:17 +0100887 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200888
889 /* result == second operand */
890 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200891 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200892 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
893
Paul Bakkerbd51b262014-07-10 15:26:12 +0200894exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000896}
Paul Bakker33b43f12013-08-20 11:48:36 +0200897/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000898
Paul Bakker33b43f12013-08-20 11:48:36 +0200899/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100900void mbedtls_mpi_add_mpi_inplace( char * input_X, char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100901{
902 mbedtls_mpi X, A;
903 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
904
Werner Lewis24b60782022-07-07 15:08:17 +0100905 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100906
Werner Lewis24b60782022-07-07 15:08:17 +0100907 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100908 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
909 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200910 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100911
Werner Lewis24b60782022-07-07 15:08:17 +0100912 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100913 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200914 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100915 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
916
Werner Lewis24b60782022-07-07 15:08:17 +0100917 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100918 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200919 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +0100920 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
921
922exit:
923 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
924}
925/* END_CASE */
926
927
928/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100929void mbedtls_mpi_add_abs( char * input_X, char * input_Y,
930 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000931{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_mpi X, Y, Z, A;
933 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000934
Werner Lewis24b60782022-07-07 15:08:17 +0100935 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
936 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
937 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200939 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000941
Gilles Peskine56f943a2020-07-23 01:18:11 +0200942 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200944 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis24b60782022-07-07 15:08:17 +0100946 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200947
948 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200950 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000952
Paul Bakkerbd51b262014-07-10 15:26:12 +0200953exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +0200954 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000955}
Paul Bakker33b43f12013-08-20 11:48:36 +0200956/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000957
Paul Bakker33b43f12013-08-20 11:48:36 +0200958/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100959void mbedtls_mpi_add_int( char * input_X, int input_Y,
960 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_mpi X, Z, A;
963 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000964
Werner Lewis24b60782022-07-07 15:08:17 +0100965 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
966 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200968 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000970
Paul Bakkerbd51b262014-07-10 15:26:12 +0200971exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000973}
Paul Bakker33b43f12013-08-20 11:48:36 +0200974/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000975
Paul Bakker33b43f12013-08-20 11:48:36 +0200976/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +0100977void mbedtls_mpi_sub_mpi( char * input_X, char * input_Y,
978 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000979{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_mpi X, Y, Z, A;
981 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000982
Werner Lewis24b60782022-07-07 15:08:17 +0100983 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
984 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
985 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200987 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000989
Gilles Peskine56f943a2020-07-23 01:18:11 +0200990 /* result == first operand */
991 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200992 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200993 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis24b60782022-07-07 15:08:17 +0100994 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200995
996 /* result == second operand */
997 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +0200998 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200999 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1000
Paul Bakkerbd51b262014-07-10 15:26:12 +02001001exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001003}
Paul Bakker33b43f12013-08-20 11:48:36 +02001004/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001005
Paul Bakker33b43f12013-08-20 11:48:36 +02001006/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001007void mbedtls_mpi_sub_abs( char * input_X, char * input_Y,
1008 char * input_A, int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001011 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001013
Werner Lewis24b60782022-07-07 15:08:17 +01001014 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1015 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1016 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001019 TEST_ASSERT( res == sub_result );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001020 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +00001021 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001023
Gilles Peskine56f943a2020-07-23 01:18:11 +02001024 /* result == first operand */
1025 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001026 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001027 if( sub_result == 0 )
1028 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Werner Lewis24b60782022-07-07 15:08:17 +01001029 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001030
1031 /* result == second operand */
1032 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001033 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine56f943a2020-07-23 01:18:11 +02001034 if( sub_result == 0 )
1035 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1036
Paul Bakkerbd51b262014-07-10 15:26:12 +02001037exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001039}
Paul Bakker33b43f12013-08-20 11:48:36 +02001040/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001041
Paul Bakker33b43f12013-08-20 11:48:36 +02001042/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001043void mbedtls_mpi_sub_int( char * input_X, int input_Y,
1044 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_mpi X, Z, A;
1047 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001048
Werner Lewis24b60782022-07-07 15:08:17 +01001049 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1050 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001052 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001054
Paul Bakkerbd51b262014-07-10 15:26:12 +02001055exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001057}
Paul Bakker33b43f12013-08-20 11:48:36 +02001058/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001059
Paul Bakker33b43f12013-08-20 11:48:36 +02001060/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001061void mbedtls_mpi_mul_mpi( char * input_X, char * input_Y,
1062 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_mpi X, Y, Z, A;
1065 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001066
Werner Lewis24b60782022-07-07 15:08:17 +01001067 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1068 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1069 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001071 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001073
Paul Bakkerbd51b262014-07-10 15:26:12 +02001074exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001076}
Paul Bakker33b43f12013-08-20 11:48:36 +02001077/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001078
Paul Bakker33b43f12013-08-20 11:48:36 +02001079/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001080void mbedtls_mpi_mul_int( char * input_X, int input_Y,
Werner Lewis3d52e442022-07-06 13:03:36 +01001081 char * input_A, char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001082{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_mpi X, Z, A;
1084 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001085
Werner Lewis24b60782022-07-07 15:08:17 +01001086 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1087 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001089 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001090 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001092 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001094 else
1095 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001096
Paul Bakkerbd51b262014-07-10 15:26:12 +02001097exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001099}
Paul Bakker33b43f12013-08-20 11:48:36 +02001100/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001101
Paul Bakker33b43f12013-08-20 11:48:36 +02001102/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001103void mbedtls_mpi_div_mpi( char * input_X, char * input_Y,
1104 char * input_A, char * input_B,
1105 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001108 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1110 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001111
Werner Lewis24b60782022-07-07 15:08:17 +01001112 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1113 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1114 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1115 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001117 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001118 if( res == 0 )
1119 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001120 TEST_ASSERT( sign_is_valid( &Q ) );
1121 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1123 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001124 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001125
Paul Bakkerbd51b262014-07-10 15:26:12 +02001126exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1128 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001129}
Paul Bakker33b43f12013-08-20 11:48:36 +02001130/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001131
Paul Bakker33b43f12013-08-20 11:48:36 +02001132/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001133void mbedtls_mpi_div_int( char * input_X, int input_Y,
Werner Lewis3d52e442022-07-06 13:03:36 +01001134 char * input_A, char * input_B,
1135 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001136{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001138 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1140 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001141
Werner Lewis24b60782022-07-07 15:08:17 +01001142 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1143 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1144 TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001146 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001147 if( res == 0 )
1148 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001149 TEST_ASSERT( sign_is_valid( &Q ) );
1150 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1152 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001153 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001154
Paul Bakkerbd51b262014-07-10 15:26:12 +02001155exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1157 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001158}
Paul Bakker33b43f12013-08-20 11:48:36 +02001159/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001160
Paul Bakker33b43f12013-08-20 11:48:36 +02001161/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001162void mbedtls_mpi_mod_mpi( char * input_X, char * input_Y,
1163 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001166 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001168
Werner Lewis24b60782022-07-07 15:08:17 +01001169 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1170 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1171 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001173 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001174 if( res == 0 )
1175 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001176 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001178 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001179
Paul Bakkerbd51b262014-07-10 15:26:12 +02001180exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001182}
Paul Bakker33b43f12013-08-20 11:48:36 +02001183/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001184
Paul Bakker33b43f12013-08-20 11:48:36 +02001185/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001186void mbedtls_mpi_mod_int( char * input_X, int input_Y,
Azim Khanf1aaec92017-05-30 14:23:15 +01001187 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001190 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 mbedtls_mpi_uint r;
1192 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001193
Werner Lewis24b60782022-07-07 15:08:17 +01001194 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001196 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001197 if( res == 0 )
1198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001200 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001201
Paul Bakkerbd51b262014-07-10 15:26:12 +02001202exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001204}
Paul Bakker33b43f12013-08-20 11:48:36 +02001205/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001206
Paul Bakker33b43f12013-08-20 11:48:36 +02001207/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001208void mbedtls_mpi_exp_mod( char * input_A, char * input_E,
1209 char * input_N, char * input_X,
1210 int exp_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001213 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1215 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001216
Werner Lewis24b60782022-07-07 15:08:17 +01001217 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1218 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
1219 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
1220 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001221
Gilles Peskine4cc80212021-06-09 18:31:35 +02001222 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
Gilles Peskine3df05542021-06-15 21:55:05 +02001223 TEST_ASSERT( res == exp_result );
Gilles Peskine4cc80212021-06-09 18:31:35 +02001224 if( res == 0 )
1225 {
1226 TEST_ASSERT( sign_is_valid( &Z ) );
1227 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1228 }
1229
1230 /* Now test again with the speed-up parameter supplied as an output. */
1231 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine3df05542021-06-15 21:55:05 +02001232 TEST_ASSERT( res == exp_result );
Gilles Peskine4cc80212021-06-09 18:31:35 +02001233 if( res == 0 )
1234 {
1235 TEST_ASSERT( sign_is_valid( &Z ) );
1236 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1237 }
1238
1239 /* Now test again with the speed-up parameter supplied in calculated form. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine3df05542021-06-15 21:55:05 +02001241 TEST_ASSERT( res == exp_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001242 if( res == 0 )
1243 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001244 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001246 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001247
Paul Bakkerbd51b262014-07-10 15:26:12 +02001248exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1250 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001251}
Paul Bakker33b43f12013-08-20 11:48:36 +02001252/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001253
Paul Bakker33b43f12013-08-20 11:48:36 +02001254/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001255void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Werner Lewis955a0bb2022-07-07 15:09:15 +01001256 char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001257{
1258 mbedtls_mpi A, E, N, RR, Z;
1259 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1260 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1261
Chris Jonesaa850cd2020-12-03 11:35:41 +00001262 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001263 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001264 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001265 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001266
1267 /* Set E to 2^(E_bytes - 1) + 1 */
1268 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1269 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001270 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001271
1272 /* Set N to 2^(N_bytes - 1) + 1 */
1273 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1274 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001275 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1276
1277 if( strlen( input_RR ) )
Werner Lewis24b60782022-07-07 15:08:17 +01001278 TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001279
Chris Jonesaa850cd2020-12-03 11:35:41 +00001280 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001281
1282exit:
1283 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1284 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1285}
1286/* END_CASE */
1287
1288/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001289void mbedtls_mpi_inv_mod( char * input_X, char * input_Y,
1290 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001293 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001295
Werner Lewis24b60782022-07-07 15:08:17 +01001296 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1297 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1298 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001300 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001301 if( res == 0 )
1302 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001303 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001305 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001306
Paul Bakkerbd51b262014-07-10 15:26:12 +02001307exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001309}
Paul Bakker33b43f12013-08-20 11:48:36 +02001310/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001313void mbedtls_mpi_is_prime( char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001316 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001318
Werner Lewis24b60782022-07-07 15:08:17 +01001319 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001320 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001321 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001322
Paul Bakkerbd51b262014-07-10 15:26:12 +02001323exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001325}
Paul Bakker33b43f12013-08-20 11:48:36 +02001326/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001329void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001330 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001331{
1332 mbedtls_mpi X;
1333 int res;
1334 mbedtls_test_mpi_random rand;
1335
1336 mbedtls_mpi_init( &X );
1337 rand.data = witnesses;
1338 rand.pos = 0;
1339 rand.chunk_len = chunk_len;
1340
1341 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001342 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1343 mbedtls_test_mpi_miller_rabin_determinizer,
1344 &rand );
1345 TEST_ASSERT( res == 0 );
1346
1347 rand.data = witnesses;
1348 rand.pos = 0;
1349 rand.chunk_len = chunk_len;
1350
Janos Follatha0b67c22018-09-18 14:48:23 +01001351 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1352 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001353 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001354 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001355
1356exit:
1357 mbedtls_mpi_free( &X );
1358}
1359/* END_CASE */
1360
1361/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001362void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001365 int my_ret;
1366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001368
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001369 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1370 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001371 TEST_ASSERT( my_ret == ref_ret );
1372
1373 if( ref_ret == 0 )
1374 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001375 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001376
1377 TEST_ASSERT( actual_bits >= (size_t) bits );
1378 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001379 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001380
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001381 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1382 mbedtls_test_rnd_std_rand,
1383 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001384 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001385 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001386 /* X = ( X - 1 ) / 2 */
1387 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001388 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1389 mbedtls_test_rnd_std_rand,
1390 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001391 }
1392 }
1393
Paul Bakkerbd51b262014-07-10 15:26:12 +02001394exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001396}
1397/* END_CASE */
1398
Paul Bakker33b43f12013-08-20 11:48:36 +02001399/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001400void mbedtls_mpi_shift_l( char * input_X, int shift_X,
1401 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 mbedtls_mpi X, A;
1404 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001405
Werner Lewis24b60782022-07-07 15:08:17 +01001406 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1407 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001409 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001411
Paul Bakkerbd51b262014-07-10 15:26:12 +02001412exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001414}
Paul Bakker33b43f12013-08-20 11:48:36 +02001415/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001416
Paul Bakker33b43f12013-08-20 11:48:36 +02001417/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001418void mbedtls_mpi_shift_r( char * input_X, int shift_X,
1419 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001420{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 mbedtls_mpi X, A;
1422 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001423
Werner Lewis24b60782022-07-07 15:08:17 +01001424 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1425 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001427 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001429
Paul Bakkerbd51b262014-07-10 15:26:12 +02001430exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001432}
Paul Bakker33b43f12013-08-20 11:48:36 +02001433/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001434
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001435/* BEGIN_CASE */
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001436void mpi_fill_random( int wanted_bytes, int rng_bytes,
1437 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001438{
1439 mbedtls_mpi X;
1440 int ret;
1441 size_t bytes_left = rng_bytes;
1442 mbedtls_mpi_init( &X );
1443
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001444 if( before != 0 )
1445 {
1446 /* Set X to sign(before) * 2^(|before|-1) */
1447 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1448 if( before < 0 )
1449 before = - before;
1450 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1451 }
1452
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001453 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1454 f_rng_bytes_left, &bytes_left );
1455 TEST_ASSERT( ret == expected_ret );
1456
1457 if( expected_ret == 0 )
1458 {
1459 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1460 * as a big-endian representation of the number. We know when
1461 * our RNG function returns null bytes, so we know how many
1462 * leading zero bytes the number has. */
1463 size_t leading_zeros = 0;
1464 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1465 leading_zeros = 1;
1466 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1467 (size_t) wanted_bytes );
1468 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001469 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001470 }
1471
1472exit:
1473 mbedtls_mpi_free( &X );
1474}
1475/* END_CASE */
1476
Gilles Peskine4699fa42021-03-29 22:02:55 +02001477/* BEGIN_CASE */
1478void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1479{
1480 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1481 * This function assumes that the value of bound is at least 2 and
1482 * that iterations is large enough that a one-in-2^iterations chance
1483 * effectively never occurs.
1484 */
1485
1486 mbedtls_mpi upper_bound;
1487 size_t n_bits;
1488 mbedtls_mpi result;
1489 size_t b;
1490 /* If upper_bound is small, stats[b] is the number of times the value b
1491 * has been generated. Otherwise stats[b] is the number of times a
1492 * value with bit b set has been generated. */
1493 size_t *stats = NULL;
1494 size_t stats_len;
1495 int full_stats;
1496 size_t i;
1497
1498 mbedtls_mpi_init( &upper_bound );
1499 mbedtls_mpi_init( &result );
1500
1501 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1502 bound_bytes->x, bound_bytes->len ) );
1503 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1504 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1505 * to be small enough that the probability of missing one value is
1506 * negligible given the number of iterations. It must be less than
1507 * 256 because some of the code below assumes that "small" values
1508 * fit in a byte. */
1509 if( n_bits <= 5 )
1510 {
1511 full_stats = 1;
1512 stats_len = bound_bytes->x[bound_bytes->len - 1];
1513 }
1514 else
1515 {
1516 full_stats = 0;
1517 stats_len = n_bits;
1518 }
1519 ASSERT_ALLOC( stats, stats_len );
1520
1521 for( i = 0; i < (size_t) iterations; i++ )
1522 {
1523 mbedtls_test_set_step( i );
1524 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1525 mbedtls_test_rnd_std_rand, NULL ) );
1526
Gilles Peskineb53b2182021-06-10 15:34:15 +02001527 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine4699fa42021-03-29 22:02:55 +02001528 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1529 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1530 if( full_stats )
1531 {
1532 uint8_t value;
1533 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1534 TEST_ASSERT( value < stats_len );
1535 ++stats[value];
1536 }
1537 else
1538 {
1539 for( b = 0; b < n_bits; b++ )
1540 stats[b] += mbedtls_mpi_get_bit( &result, b );
1541 }
1542 }
1543
1544 if( full_stats )
1545 {
Gilles Peskinec520d7a2021-04-13 20:45:05 +02001546 for( b = min; b < stats_len; b++ )
Gilles Peskine4699fa42021-03-29 22:02:55 +02001547 {
1548 mbedtls_test_set_step( 1000000 + b );
1549 /* Assert that each value has been reached at least once.
1550 * This is almost guaranteed if the iteration count is large
1551 * enough. This is a very crude way of checking the distribution.
1552 */
1553 TEST_ASSERT( stats[b] > 0 );
1554 }
1555 }
1556 else
1557 {
Gilles Peskinee4f937f2021-06-02 21:24:04 +02001558 int statistically_safe_all_the_way =
1559 is_significantly_above_a_power_of_2( bound_bytes );
Gilles Peskine4699fa42021-03-29 22:02:55 +02001560 for( b = 0; b < n_bits; b++ )
1561 {
1562 mbedtls_test_set_step( 1000000 + b );
1563 /* Assert that each bit has been set in at least one result and
1564 * clear in at least one result. Provided that iterations is not
1565 * too small, it would be extremely unlikely for this not to be
1566 * the case if the results are uniformly distributed.
1567 *
1568 * As an exception, the top bit may legitimately never be set
1569 * if bound is a power of 2 or only slightly above.
1570 */
Gilles Peskinee4f937f2021-06-02 21:24:04 +02001571 if( statistically_safe_all_the_way || b != n_bits - 1 )
Gilles Peskine4699fa42021-03-29 22:02:55 +02001572 {
1573 TEST_ASSERT( stats[b] > 0 );
1574 }
1575 TEST_ASSERT( stats[b] < (size_t) iterations );
1576 }
1577 }
1578
1579exit:
1580 mbedtls_mpi_free( &upper_bound );
1581 mbedtls_mpi_free( &result );
1582 mbedtls_free( stats );
1583}
1584/* END_CASE */
1585
Gilles Peskine9312ba52021-03-29 22:14:51 +02001586/* BEGIN_CASE */
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001587void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine8f454702021-04-01 15:57:18 +02001588{
1589 mbedtls_mpi upper_bound;
1590 mbedtls_mpi result;
1591
1592 mbedtls_mpi_init( &upper_bound );
1593 mbedtls_mpi_init( &result );
1594
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001595 if( before != 0 )
1596 {
1597 /* Set result to sign(before) * 2^(|before|-1) */
1598 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1599 if( before < 0 )
1600 before = - before;
1601 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1602 }
1603
Gilles Peskine8f454702021-04-01 15:57:18 +02001604 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1605 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1606 bound_bytes->x, bound_bytes->len ) );
1607 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1608 mbedtls_test_rnd_std_rand, NULL ) );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001609 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine8f454702021-04-01 15:57:18 +02001610 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1611 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1612
1613exit:
1614 mbedtls_mpi_free( &upper_bound );
1615 mbedtls_mpi_free( &result );
1616}
1617/* END_CASE */
1618
1619/* BEGIN_CASE */
Gilles Peskine9312ba52021-03-29 22:14:51 +02001620void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1621{
1622 mbedtls_mpi upper_bound;
1623 mbedtls_mpi result;
1624 int actual_ret;
1625
1626 mbedtls_mpi_init( &upper_bound );
1627 mbedtls_mpi_init( &result );
1628
1629 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1630 bound_bytes->x, bound_bytes->len ) );
1631 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
1632 mbedtls_test_rnd_std_rand, NULL );
1633 TEST_EQUAL( expected_ret, actual_ret );
1634
1635exit:
1636 mbedtls_mpi_free( &upper_bound );
1637 mbedtls_mpi_free( &result );
1638}
1639/* END_CASE */
1640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001642void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001643{
Andres AG93012e82016-09-09 09:10:28 +01001644 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001645}
Paul Bakker33b43f12013-08-20 11:48:36 +02001646/* END_CASE */