blob: 35952f02a783d118330e5f92bf7ab3ccc8d7ac2b [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 */
Tom Cosgrovec2c6fcb2022-11-09 12:59:33 +00001186void mbedtls_mpi_mod_int( char * input_X, char * input_Y,
1187 char * input_A, int mod_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_mpi X;
Tom Cosgrovec2c6fcb2022-11-09 12:59:33 +00001190 mbedtls_mpi Y;
1191 mbedtls_mpi A;
Paul Bakker367dae42009-06-28 21:50:27 +00001192 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_mpi_uint r;
Paul Bakker367dae42009-06-28 21:50:27 +00001194
Tom Cosgrovec2c6fcb2022-11-09 12:59:33 +00001195 mbedtls_mpi_init( &X );
1196 mbedtls_mpi_init( &Y );
1197 mbedtls_mpi_init( &A );
1198
1199 /* We use MPIs to read Y and A since the test framework limits us to
1200 * ints, so we can't have 64-bit values */
1201 TEST_EQUAL( mbedtls_test_read_mpi( &X, input_X ), 0 );
1202 TEST_EQUAL( mbedtls_test_read_mpi( &Y, input_Y ), 0 );
1203 TEST_EQUAL( mbedtls_test_read_mpi( &A, input_A ), 0 );
1204
1205 TEST_EQUAL( Y.n, 1 );
1206 TEST_EQUAL( A.n, 1 );
1207
Tom Cosgrove5c307b92022-11-10 12:05:55 +00001208 /* Convert the MPIs for Y and A to (signed) mbedtls_mpi_sints */
1209
1210 /* Since we're converting sign+magnitude to two's complement, we lose one
1211 * bit of value in the output. This means there are some values we can't
1212 * represent, e.g. (hex) -A0000000 on 32-bit systems. These are technically
1213 * invalid test cases, so could be considered "won't happen", but they are
1214 * easy to test for, and this helps guard against human error. */
1215
1216 mbedtls_mpi_sint y = (mbedtls_mpi_sint) Y.p[0];
1217 TEST_ASSERT( y >= 0 ); /* If y < 0 here, we can't make negative y */
Tom Cosgrovec2c6fcb2022-11-09 12:59:33 +00001218 if( Y.s == -1 )
1219 y = -y;
Tom Cosgrove5c307b92022-11-10 12:05:55 +00001220
1221 mbedtls_mpi_sint a = (mbedtls_mpi_sint) A.p[0];
1222 TEST_ASSERT( a >= 0 ); /* Same goes for a */
Tom Cosgrovec2c6fcb2022-11-09 12:59:33 +00001223 if( A.s == -1 )
1224 a = -a;
1225
1226 res = mbedtls_mpi_mod_int( &r, &X, y );
1227 TEST_EQUAL( res, mod_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001228 if( res == 0 )
1229 {
Tom Cosgrovec2c6fcb2022-11-09 12:59:33 +00001230 TEST_EQUAL( r, a );
Paul Bakker367dae42009-06-28 21:50:27 +00001231 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001232
Paul Bakkerbd51b262014-07-10 15:26:12 +02001233exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_mpi_free( &X );
Tom Cosgrovec2c6fcb2022-11-09 12:59:33 +00001235 mbedtls_mpi_free( &Y );
1236 mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001237}
Paul Bakker33b43f12013-08-20 11:48:36 +02001238/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001239
Paul Bakker33b43f12013-08-20 11:48:36 +02001240/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001241void mbedtls_mpi_exp_mod( char * input_A, char * input_E,
1242 char * input_N, char * input_X,
1243 int exp_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001246 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1248 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001249
Werner Lewis24b60782022-07-07 15:08:17 +01001250 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
1251 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
1252 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
1253 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001254
Gilles Peskine4cc80212021-06-09 18:31:35 +02001255 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
Gilles Peskine3df05542021-06-15 21:55:05 +02001256 TEST_ASSERT( res == exp_result );
Gilles Peskine4cc80212021-06-09 18:31:35 +02001257 if( res == 0 )
1258 {
1259 TEST_ASSERT( sign_is_valid( &Z ) );
1260 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1261 }
1262
1263 /* Now test again with the speed-up parameter supplied as an output. */
1264 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine3df05542021-06-15 21:55:05 +02001265 TEST_ASSERT( res == exp_result );
Gilles Peskine4cc80212021-06-09 18:31:35 +02001266 if( res == 0 )
1267 {
1268 TEST_ASSERT( sign_is_valid( &Z ) );
1269 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
1270 }
1271
1272 /* Now test again with the speed-up parameter supplied in calculated form. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Gilles Peskine3df05542021-06-15 21:55:05 +02001274 TEST_ASSERT( res == exp_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001275 if( res == 0 )
1276 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001277 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001279 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001280
Paul Bakkerbd51b262014-07-10 15:26:12 +02001281exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1283 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001284}
Paul Bakker33b43f12013-08-20 11:48:36 +02001285/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001286
Paul Bakker33b43f12013-08-20 11:48:36 +02001287/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001288void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Werner Lewis955a0bb2022-07-07 15:09:15 +01001289 char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001290{
1291 mbedtls_mpi A, E, N, RR, Z;
1292 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1293 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1294
Chris Jonesaa850cd2020-12-03 11:35:41 +00001295 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001296 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001297 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001298 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001299
1300 /* Set E to 2^(E_bytes - 1) + 1 */
1301 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1302 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001303 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001304
1305 /* Set N to 2^(N_bytes - 1) + 1 */
1306 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1307 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001308 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1309
1310 if( strlen( input_RR ) )
Werner Lewis24b60782022-07-07 15:08:17 +01001311 TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001312
Chris Jonesaa850cd2020-12-03 11:35:41 +00001313 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001314
1315exit:
1316 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1317 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1318}
1319/* END_CASE */
1320
1321/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001322void mbedtls_mpi_inv_mod( char * input_X, char * input_Y,
1323 char * input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001326 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001328
Werner Lewis24b60782022-07-07 15:08:17 +01001329 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1330 TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 );
1331 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001333 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001334 if( res == 0 )
1335 {
Gilles Peskineb53b2182021-06-10 15:34:15 +02001336 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001338 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001339
Paul Bakkerbd51b262014-07-10 15:26:12 +02001340exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001342}
Paul Bakker33b43f12013-08-20 11:48:36 +02001343/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001346void mbedtls_mpi_is_prime( char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001349 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001351
Werner Lewis24b60782022-07-07 15:08:17 +01001352 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001353 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001354 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001355
Paul Bakkerbd51b262014-07-10 15:26:12 +02001356exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001358}
Paul Bakker33b43f12013-08-20 11:48:36 +02001359/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001362void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001363 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001364{
1365 mbedtls_mpi X;
1366 int res;
1367 mbedtls_test_mpi_random rand;
1368
1369 mbedtls_mpi_init( &X );
1370 rand.data = witnesses;
1371 rand.pos = 0;
1372 rand.chunk_len = chunk_len;
1373
1374 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001375 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1376 mbedtls_test_mpi_miller_rabin_determinizer,
1377 &rand );
1378 TEST_ASSERT( res == 0 );
1379
1380 rand.data = witnesses;
1381 rand.pos = 0;
1382 rand.chunk_len = chunk_len;
1383
Janos Follatha0b67c22018-09-18 14:48:23 +01001384 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1385 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001386 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001387 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001388
1389exit:
1390 mbedtls_mpi_free( &X );
1391}
1392/* END_CASE */
1393
1394/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001395void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001398 int my_ret;
1399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001401
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001402 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1403 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001404 TEST_ASSERT( my_ret == ref_ret );
1405
1406 if( ref_ret == 0 )
1407 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001408 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001409
1410 TEST_ASSERT( actual_bits >= (size_t) bits );
1411 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001412 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001413
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001414 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1415 mbedtls_test_rnd_std_rand,
1416 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001417 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001418 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001419 /* X = ( X - 1 ) / 2 */
1420 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001421 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1422 mbedtls_test_rnd_std_rand,
1423 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001424 }
1425 }
1426
Paul Bakkerbd51b262014-07-10 15:26:12 +02001427exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001429}
1430/* END_CASE */
1431
Paul Bakker33b43f12013-08-20 11:48:36 +02001432/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001433void mbedtls_mpi_shift_l( char * input_X, int shift_X,
1434 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001435{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_mpi X, A;
1437 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001438
Werner Lewis24b60782022-07-07 15:08:17 +01001439 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1440 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001442 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001444
Paul Bakkerbd51b262014-07-10 15:26:12 +02001445exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001447}
Paul Bakker33b43f12013-08-20 11:48:36 +02001448/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001449
Paul Bakker33b43f12013-08-20 11:48:36 +02001450/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001451void mbedtls_mpi_shift_r( char * input_X, int shift_X,
1452 char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_mpi X, A;
1455 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001456
Werner Lewis24b60782022-07-07 15:08:17 +01001457 TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1458 TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001460 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001462
Paul Bakkerbd51b262014-07-10 15:26:12 +02001463exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001465}
Paul Bakker33b43f12013-08-20 11:48:36 +02001466/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001467
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001468/* BEGIN_CASE */
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001469void mpi_fill_random( int wanted_bytes, int rng_bytes,
1470 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001471{
1472 mbedtls_mpi X;
1473 int ret;
1474 size_t bytes_left = rng_bytes;
1475 mbedtls_mpi_init( &X );
1476
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001477 if( before != 0 )
1478 {
1479 /* Set X to sign(before) * 2^(|before|-1) */
1480 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1481 if( before < 0 )
1482 before = - before;
1483 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1484 }
1485
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001486 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1487 f_rng_bytes_left, &bytes_left );
1488 TEST_ASSERT( ret == expected_ret );
1489
1490 if( expected_ret == 0 )
1491 {
1492 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1493 * as a big-endian representation of the number. We know when
1494 * our RNG function returns null bytes, so we know how many
1495 * leading zero bytes the number has. */
1496 size_t leading_zeros = 0;
1497 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1498 leading_zeros = 1;
1499 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1500 (size_t) wanted_bytes );
1501 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001502 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001503 }
1504
1505exit:
1506 mbedtls_mpi_free( &X );
1507}
1508/* END_CASE */
1509
Gilles Peskine4699fa42021-03-29 22:02:55 +02001510/* BEGIN_CASE */
1511void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1512{
1513 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1514 * This function assumes that the value of bound is at least 2 and
1515 * that iterations is large enough that a one-in-2^iterations chance
1516 * effectively never occurs.
1517 */
1518
1519 mbedtls_mpi upper_bound;
1520 size_t n_bits;
1521 mbedtls_mpi result;
1522 size_t b;
1523 /* If upper_bound is small, stats[b] is the number of times the value b
1524 * has been generated. Otherwise stats[b] is the number of times a
1525 * value with bit b set has been generated. */
1526 size_t *stats = NULL;
1527 size_t stats_len;
1528 int full_stats;
1529 size_t i;
1530
1531 mbedtls_mpi_init( &upper_bound );
1532 mbedtls_mpi_init( &result );
1533
1534 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1535 bound_bytes->x, bound_bytes->len ) );
1536 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1537 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1538 * to be small enough that the probability of missing one value is
1539 * negligible given the number of iterations. It must be less than
1540 * 256 because some of the code below assumes that "small" values
1541 * fit in a byte. */
1542 if( n_bits <= 5 )
1543 {
1544 full_stats = 1;
1545 stats_len = bound_bytes->x[bound_bytes->len - 1];
1546 }
1547 else
1548 {
1549 full_stats = 0;
1550 stats_len = n_bits;
1551 }
1552 ASSERT_ALLOC( stats, stats_len );
1553
1554 for( i = 0; i < (size_t) iterations; i++ )
1555 {
1556 mbedtls_test_set_step( i );
1557 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1558 mbedtls_test_rnd_std_rand, NULL ) );
1559
Gilles Peskineb53b2182021-06-10 15:34:15 +02001560 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine4699fa42021-03-29 22:02:55 +02001561 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1562 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1563 if( full_stats )
1564 {
1565 uint8_t value;
1566 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1567 TEST_ASSERT( value < stats_len );
1568 ++stats[value];
1569 }
1570 else
1571 {
1572 for( b = 0; b < n_bits; b++ )
1573 stats[b] += mbedtls_mpi_get_bit( &result, b );
1574 }
1575 }
1576
1577 if( full_stats )
1578 {
Gilles Peskinec520d7a2021-04-13 20:45:05 +02001579 for( b = min; b < stats_len; b++ )
Gilles Peskine4699fa42021-03-29 22:02:55 +02001580 {
1581 mbedtls_test_set_step( 1000000 + b );
1582 /* Assert that each value has been reached at least once.
1583 * This is almost guaranteed if the iteration count is large
1584 * enough. This is a very crude way of checking the distribution.
1585 */
1586 TEST_ASSERT( stats[b] > 0 );
1587 }
1588 }
1589 else
1590 {
Gilles Peskinee4f937f2021-06-02 21:24:04 +02001591 int statistically_safe_all_the_way =
1592 is_significantly_above_a_power_of_2( bound_bytes );
Gilles Peskine4699fa42021-03-29 22:02:55 +02001593 for( b = 0; b < n_bits; b++ )
1594 {
1595 mbedtls_test_set_step( 1000000 + b );
1596 /* Assert that each bit has been set in at least one result and
1597 * clear in at least one result. Provided that iterations is not
1598 * too small, it would be extremely unlikely for this not to be
1599 * the case if the results are uniformly distributed.
1600 *
1601 * As an exception, the top bit may legitimately never be set
1602 * if bound is a power of 2 or only slightly above.
1603 */
Gilles Peskinee4f937f2021-06-02 21:24:04 +02001604 if( statistically_safe_all_the_way || b != n_bits - 1 )
Gilles Peskine4699fa42021-03-29 22:02:55 +02001605 {
1606 TEST_ASSERT( stats[b] > 0 );
1607 }
1608 TEST_ASSERT( stats[b] < (size_t) iterations );
1609 }
1610 }
1611
1612exit:
1613 mbedtls_mpi_free( &upper_bound );
1614 mbedtls_mpi_free( &result );
1615 mbedtls_free( stats );
1616}
1617/* END_CASE */
1618
Gilles Peskine9312ba52021-03-29 22:14:51 +02001619/* BEGIN_CASE */
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001620void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine8f454702021-04-01 15:57:18 +02001621{
1622 mbedtls_mpi upper_bound;
1623 mbedtls_mpi result;
1624
1625 mbedtls_mpi_init( &upper_bound );
1626 mbedtls_mpi_init( &result );
1627
Gilles Peskinef467e1a2021-04-02 00:02:27 +02001628 if( before != 0 )
1629 {
1630 /* Set result to sign(before) * 2^(|before|-1) */
1631 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1632 if( before < 0 )
1633 before = - before;
1634 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1635 }
1636
Gilles Peskine8f454702021-04-01 15:57:18 +02001637 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1638 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1639 bound_bytes->x, bound_bytes->len ) );
1640 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1641 mbedtls_test_rnd_std_rand, NULL ) );
Gilles Peskineb53b2182021-06-10 15:34:15 +02001642 TEST_ASSERT( sign_is_valid( &result ) );
Gilles Peskine8f454702021-04-01 15:57:18 +02001643 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1644 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1645
1646exit:
1647 mbedtls_mpi_free( &upper_bound );
1648 mbedtls_mpi_free( &result );
1649}
1650/* END_CASE */
1651
1652/* BEGIN_CASE */
Gilles Peskine9312ba52021-03-29 22:14:51 +02001653void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1654{
1655 mbedtls_mpi upper_bound;
1656 mbedtls_mpi result;
1657 int actual_ret;
1658
1659 mbedtls_mpi_init( &upper_bound );
1660 mbedtls_mpi_init( &result );
1661
1662 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1663 bound_bytes->x, bound_bytes->len ) );
1664 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
1665 mbedtls_test_rnd_std_rand, NULL );
1666 TEST_EQUAL( expected_ret, actual_ret );
1667
1668exit:
1669 mbedtls_mpi_free( &upper_bound );
1670 mbedtls_mpi_free( &result );
1671}
1672/* END_CASE */
1673
Gilles Peskinede1629a2022-11-15 23:02:14 +01001674/* BEGIN_CASE */
1675void most_negative_mpi_sint( )
1676{
1677 /* Ad hoc tests for n = -p = -2^(biL-1) as a mbedtls_mpi_sint. We
1678 * guarantee that mbedtls_mpi_sint is a two's complement type, so this
1679 * is a valid value. However, negating it (`-n`) has undefined behavior
1680 * (although in practice `-n` evaluates to the value n).
1681 *
1682 * This function has ad hoc tests for this value. It's separated from other
1683 * functions because the test framework makes it hard to pass this value
1684 * into test cases.
1685 *
1686 * In the comments here:
1687 * - biL = number of bits in limbs
1688 * - p = 2^(biL-1) (smallest positive value not in mbedtls_mpi_sint range)
1689 * - n = -2^(biL-1) (largest negative value in mbedtls_mpi_sint range)
1690 */
1691
1692 mbedtls_mpi A, R, X;
1693 mbedtls_mpi_init( &A );
1694 mbedtls_mpi_init( &R );
1695 mbedtls_mpi_init( &X );
1696
1697 const size_t biL = 8 * sizeof( mbedtls_mpi_sint );
1698 mbedtls_mpi_uint most_positive_plus_1 = (mbedtls_mpi_uint) 1 << ( biL - 1 );
1699 const mbedtls_mpi_sint most_positive = most_positive_plus_1 - 1;
1700 const mbedtls_mpi_sint most_negative = - most_positive - 1;
1701 TEST_EQUAL( (mbedtls_mpi_uint) most_negative,
1702 (mbedtls_mpi_uint) 1 << ( biL - 1 ) );
1703 TEST_EQUAL( (mbedtls_mpi_uint) most_negative << 1, 0 );
1704
1705 /* Test mbedtls_mpi_lset() */
1706 TEST_EQUAL( mbedtls_mpi_lset( &A, most_negative ), 0 );
1707 TEST_EQUAL( A.s, -1 );
1708 TEST_EQUAL( A.n, 1 );
1709 TEST_EQUAL( A.p[0], most_positive_plus_1 );
1710
1711 /* Test mbedtls_mpi_cmp_int(): -p == -p */
1712 TEST_EQUAL( mbedtls_mpi_cmp_int( &A, most_negative ), 0 );
1713
1714 /* Test mbedtls_mpi_cmp_int(): -(p+1) < -p */
1715 A.p[0] = most_positive_plus_1 + 1;
1716 TEST_EQUAL( mbedtls_mpi_cmp_int( &A, most_negative ), -1 );
1717
1718 /* Test mbedtls_mpi_cmp_int(): -(p-1) > -p */
1719 A.p[0] = most_positive_plus_1 - 1;
1720 TEST_EQUAL( mbedtls_mpi_cmp_int( &A, most_negative ), 1 );
1721
1722 /* Test mbedtls_mpi_add_int(): (p-1) + (-p) */
1723 TEST_EQUAL( mbedtls_mpi_lset( &A, most_positive ), 0 );
1724 TEST_EQUAL( mbedtls_mpi_add_int( &X, &A, most_negative ), 0 );
1725 TEST_EQUAL( mbedtls_mpi_cmp_int( &X, -1 ), 0 );
1726
1727 /* Test mbedtls_mpi_add_int(): (0) + (-p) */
1728 TEST_EQUAL( mbedtls_mpi_lset( &A, 0 ), 0 );
1729 TEST_EQUAL( mbedtls_mpi_add_int( &X, &A, most_negative ), 0 );
1730 TEST_EQUAL( mbedtls_mpi_cmp_int( &X, most_negative ), 0 );
1731
1732 /* Test mbedtls_mpi_add_int(): (-p) + (-p) */
1733 TEST_EQUAL( mbedtls_mpi_lset( &A, most_negative ), 0 );
1734 TEST_EQUAL( mbedtls_mpi_add_int( &X, &A, most_negative ), 0 );
1735 TEST_EQUAL( X.s, -1 );
1736 TEST_EQUAL( X.n, 2 );
1737 TEST_EQUAL( X.p[0], 0 );
1738 TEST_EQUAL( X.p[1], 1 );
1739
1740 /* Test mbedtls_mpi_sub_int(): (p) - (-p) */
1741 mbedtls_mpi_free( &X );
1742 TEST_EQUAL( mbedtls_mpi_lset( &A, most_positive ), 0 );
1743 TEST_EQUAL( mbedtls_mpi_sub_int( &X, &A, most_negative ), 0 );
1744 TEST_EQUAL( X.s, 1 );
1745 TEST_EQUAL( X.n, 1 );
1746 TEST_EQUAL( X.p[0], ~(mbedtls_mpi_uint)0 );
1747
1748 /* Test mbedtls_mpi_sub_int(): (0) - (-p) */
1749 TEST_EQUAL( mbedtls_mpi_lset( &A, 0 ), 0 );
1750 TEST_EQUAL( mbedtls_mpi_sub_int( &X, &A, most_negative ), 0 );
1751 TEST_EQUAL( X.s, 1 );
1752 TEST_EQUAL( X.n, 1 );
1753 TEST_EQUAL( X.p[0], most_positive_plus_1 );
1754
1755 /* Test mbedtls_mpi_sub_int(): (-p) - (-p) */
1756 TEST_EQUAL( mbedtls_mpi_lset( &A, most_negative ), 0 );
1757 TEST_EQUAL( mbedtls_mpi_sub_int( &X, &A, most_negative ), 0 );
1758 TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 0 ), 0 );
1759
1760 /* Test mbedtls_mpi_div_int(): (-p+1) / (-p) */
1761 TEST_EQUAL( mbedtls_mpi_lset( &A, -most_positive ), 0 );
1762 TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 );
1763 TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 0 ), 0 );
1764 TEST_EQUAL( mbedtls_mpi_cmp_int( &R, -most_positive ), 0 );
1765
1766 /* Test mbedtls_mpi_div_int(): (-p) / (-p) */
1767 TEST_EQUAL( mbedtls_mpi_lset( &A, most_negative ), 0 );
1768 TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 );
1769 TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 1 ), 0 );
1770 TEST_EQUAL( mbedtls_mpi_cmp_int( &R, 0 ), 0 );
1771
1772 /* Test mbedtls_mpi_div_int(): (-2*p) / (-p) */
1773 TEST_EQUAL( mbedtls_mpi_shift_l( &A, 1 ), 0 );
1774 TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 );
1775 TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 2 ), 0 );
1776 TEST_EQUAL( mbedtls_mpi_cmp_int( &R, 0 ), 0 );
1777
1778 /* Test mbedtls_mpi_div_int(): (-2*p+1) / (-p) */
1779 TEST_EQUAL( mbedtls_mpi_add_int( &A, &A, 1 ), 0 );
1780 TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 );
1781 TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 1 ), 0 );
1782 TEST_EQUAL( mbedtls_mpi_cmp_int( &R, -most_positive ), 0 );
1783
1784 /* Test mbedtls_mpi_div_int(): (p-1) / (-p) */
1785 TEST_EQUAL( mbedtls_mpi_lset( &A, most_positive ), 0 );
1786 TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 );
1787 TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 0 ), 0 );
1788 TEST_EQUAL( mbedtls_mpi_cmp_int( &R, most_positive ), 0 );
1789
1790 /* Test mbedtls_mpi_div_int(): (p) / (-p) */
1791 TEST_EQUAL( mbedtls_mpi_add_int( &A, &A, 1 ), 0 );
1792 TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 );
1793 TEST_EQUAL( mbedtls_mpi_cmp_int( &X, -1 ), 0 );
1794 TEST_EQUAL( mbedtls_mpi_cmp_int( &R, 0 ), 0 );
1795
1796 /* Test mbedtls_mpi_div_int(): (2*p) / (-p) */
1797 TEST_EQUAL( mbedtls_mpi_shift_l( &A, 1 ), 0 );
1798 TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 );
1799 TEST_EQUAL( mbedtls_mpi_cmp_int( &X, -2 ), 0 );
1800 TEST_EQUAL( mbedtls_mpi_cmp_int( &R, 0 ), 0 );
1801
1802 /* Test mbedtls_mpi_mod_int(): never valid */
1803 TEST_EQUAL( mbedtls_mpi_mod_int( X.p, &A, most_negative ),
1804 MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
1805
1806 /* Test mbedtls_mpi_random(): never valid */
1807 TEST_EQUAL( mbedtls_mpi_random( &X, most_negative, &A,
1808 mbedtls_test_rnd_std_rand, NULL ),
1809 MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
1810
1811exit:
1812 mbedtls_mpi_free( &A );
1813 mbedtls_mpi_free( &R );
1814 mbedtls_mpi_free( &X );
1815}
1816/* END_CASE */
1817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001819void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001820{
Andres AG93012e82016-09-09 09:10:28 +01001821 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001822}
Paul Bakker33b43f12013-08-20 11:48:36 +02001823/* END_CASE */