blob: e82fe99b50d30fbd73a0878ffc2f2b1f2061b405 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/bignum.h"
Gilles Peskine3cb1e292020-11-25 15:37:20 +01003#include "mbedtls/entropy.h"
Janos Follath64eca052018-09-05 17:04:49 +01004
Chris Jonese64a46f2020-12-03 17:44:03 +00005#if MBEDTLS_MPI_MAX_BITS > 792
6#define MPI_MAX_BITS_LARGER_THAN_792
Chris Jones4592bd82020-12-03 14:24:33 +00007#endif
Janos Follath64eca052018-09-05 17:04:49 +01008
9typedef struct mbedtls_test_mpi_random
10{
11 data_t *data;
12 size_t pos;
13 size_t chunk_len;
14} mbedtls_test_mpi_random;
15
16/*
17 * This function is called by the Miller-Rabin primality test each time it
18 * chooses a random witness. The witnesses (or non-witnesses as provided by the
19 * test) are stored in the data member of the state structure. Each number is in
20 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
21 */
22int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
23 unsigned char* buf,
24 size_t len )
25{
26 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
27
28 if( random == NULL || random->data->x == NULL || buf == NULL )
29 return( -1 );
30
31 if( random->pos + random->chunk_len > random->data->len
32 || random->chunk_len > len )
33 {
34 return( -1 );
35 }
36
37 memset( buf, 0, len );
38
39 /* The witness is written to the end of the buffer, since the buffer is
40 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
41 * Writing the witness to the start of the buffer would result in the
42 * buffer being 'witness 000...000', which would be treated as
43 * witness * 2^n for some n. */
44 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
45 random->chunk_len );
46
47 random->pos += random->chunk_len;
48
49 return( 0 );
50}
Gilles Peskine3cb1e292020-11-25 15:37:20 +010051
52/* Random generator that is told how many bytes to return. */
53static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
54{
55 size_t *bytes_left = state;
56 size_t i;
57 for( i = 0; i < len; i++ )
58 {
59 if( *bytes_left == 0 )
60 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
61 buf[i] = *bytes_left & 0xff;
62 --( *bytes_left );
63 }
64 return( 0 );
65}
66
Gilles Peskine02ac93a2021-03-29 22:02:55 +020067/* Test whether bytes represents (in big-endian base 256) a number B that
68 * is "significantly" above a power of 2, which is defined as follows.
69 * Let n be the integer such that 2^n <= B < 2^{n+1}. B is significantly
70 * above a power of 2 if (B - 2^n) / 2^n is not negligible. "Negligible"
71 * is defined as having a negligible chance that if you draw an integer
72 * in the range [1, B-1] K times, the number will always be less than 2^n,
73 * where K is the iteration count passed to genkey_sw_many.
74 */
75static int is_significantly_above_a_power_of_2( data_t *bytes )
76{
77 const uint8_t *p = bytes->x;
78 size_t len = bytes->len;
79 unsigned x;
80 while( len > 0 && p[0] == 0 )
81 {
82 ++p;
83 --len;
84 }
85 if( len == 0 )
86 return( 0 );
87 else if( len == 1 )
88 x = p[0];
89 else
90 x = ( p[0] << 8 ) | p[1];
91
92 if( x <= 4 )
93 return( 0 );
94
95 while( ( x & 0x8000 ) == 0 )
96 x <<= 1;
97 x &= 0x7fff;
98 return( x >= 0x1000 );
99}
100
Paul Bakker33b43f12013-08-20 11:48:36 +0200101/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +0000102
Paul Bakker33b43f12013-08-20 11:48:36 +0200103/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200105 * END_DEPENDENCIES
106 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000107
Hanno Beckerb48e1aa2018-12-18 23:25:01 +0000108/* BEGIN_CASE */
109void mpi_valid_param( )
110{
111 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
112}
113/* END_CASE */
114
Hanno Beckerafb607b2018-12-11 14:27:08 +0000115/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
116void mpi_invalid_param( )
117{
118 mbedtls_mpi X;
119 const char *s_in = "00101000101010";
120 char s_out[16] = { 0 };
121 unsigned char u_out[16] = { 0 };
122 unsigned char u_in[16] = { 0 };
123 size_t olen;
124 mbedtls_mpi_uint mpi_uint;
125
126 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000127
Hanno Beckerafb607b2018-12-11 14:27:08 +0000128 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
129 mbedtls_mpi_grow( NULL, 42 ) );
130 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
131 mbedtls_mpi_copy( NULL, &X ) );
132 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
133 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000134
Hanno Beckerafb607b2018-12-11 14:27:08 +0000135 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
136 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000137
Hanno Beckerafb607b2018-12-11 14:27:08 +0000138 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
139 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
140 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
141 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000142
Hanno Beckerafb607b2018-12-11 14:27:08 +0000143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
144 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
145 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
146 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000147
Hanno Beckerafb607b2018-12-11 14:27:08 +0000148 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
149 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000150
Hanno Beckerafb607b2018-12-11 14:27:08 +0000151 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
152 mbedtls_mpi_get_bit( NULL, 42 ) );
153 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
154 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000155
Hanno Beckerafb607b2018-12-11 14:27:08 +0000156 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
157 mbedtls_mpi_read_string( NULL, 2, s_in ) );
158 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
159 mbedtls_mpi_read_string( &X, 2, 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_write_string( NULL, 2,
163 s_out, sizeof( s_out ),
164 &olen ) );
165 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
166 mbedtls_mpi_write_string( &X, 2,
167 NULL, sizeof( s_out ),
168 &olen ) );
169 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
170 mbedtls_mpi_write_string( &X, 2,
171 s_out, sizeof( s_out ),
172 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000173
Hanno Beckerafb607b2018-12-11 14:27:08 +0000174#if defined(MBEDTLS_FS_IO)
175 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
176 mbedtls_mpi_read_file( NULL, 2, stdin ) );
177 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
178 mbedtls_mpi_read_file( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000179
Hanno Beckerafb607b2018-12-11 14:27:08 +0000180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
181 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
182#endif /* MBEDTLS_FS_IO */
183
184 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
185 mbedtls_mpi_read_binary( NULL, u_in,
186 sizeof( u_in ) ) );
187 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
188 mbedtls_mpi_read_binary( &X, NULL,
189 sizeof( u_in ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000190
Hanno Beckerafb607b2018-12-11 14:27:08 +0000191 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
192 mbedtls_mpi_write_binary( NULL, u_out,
193 sizeof( u_out ) ) );
194 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
195 mbedtls_mpi_write_binary( &X, NULL,
196 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000197
Hanno Beckerafb607b2018-12-11 14:27:08 +0000198 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
199 mbedtls_mpi_shift_l( NULL, 42 ) );
200 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
201 mbedtls_mpi_shift_r( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000202
Hanno Beckerafb607b2018-12-11 14:27:08 +0000203 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
204 mbedtls_mpi_cmp_abs( NULL, &X ) );
205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
206 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000207
Hanno Beckerafb607b2018-12-11 14:27:08 +0000208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
209 mbedtls_mpi_cmp_mpi( NULL, &X ) );
210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
211 mbedtls_mpi_cmp_mpi( &X, 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_cmp_int( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000215
Hanno Beckerafb607b2018-12-11 14:27:08 +0000216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
217 mbedtls_mpi_add_abs( NULL, &X, &X ) );
218 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
219 mbedtls_mpi_add_abs( &X, NULL, &X ) );
220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
221 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000222
Hanno Beckerafb607b2018-12-11 14:27:08 +0000223 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
224 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
225 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
226 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
228 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000229
Hanno Beckerafb607b2018-12-11 14:27:08 +0000230 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
231 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
232 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
233 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
234 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
235 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000236
Hanno Beckerafb607b2018-12-11 14:27:08 +0000237 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
238 mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
239 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
240 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
241 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
242 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000243
Hanno Beckerafb607b2018-12-11 14:27:08 +0000244 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
245 mbedtls_mpi_add_int( NULL, &X, 42 ) );
246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
247 mbedtls_mpi_add_int( &X, 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_sub_int( NULL, &X, 42 ) );
251 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
252 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000253
Hanno Beckerafb607b2018-12-11 14:27:08 +0000254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
255 mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
259 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000260
Hanno Beckerafb607b2018-12-11 14:27:08 +0000261 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
262 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
264 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000265
Hanno Beckerafb607b2018-12-11 14:27:08 +0000266 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
267 mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) );
268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
269 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000270
Hanno Beckerafb607b2018-12-11 14:27:08 +0000271 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
272 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000273
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000274 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
275
Hanno Beckerafb607b2018-12-11 14:27:08 +0000276 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
277 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
278 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
279 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
280 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
281 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000282
Hanno Beckerafb607b2018-12-11 14:27:08 +0000283 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
284 mbedtls_mpi_mod_int( NULL, &X, 42 ) );
285 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
286 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000287
Hanno Beckerafb607b2018-12-11 14:27:08 +0000288 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
289 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
291 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
292 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
293 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
294 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
295 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000296
Hanno Beckerafb607b2018-12-11 14:27:08 +0000297 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200298 mbedtls_mpi_fill_random( NULL, 42,
299 mbedtls_test_rnd_std_rand,
Hanno Beckerafb607b2018-12-11 14:27:08 +0000300 NULL ) );
301 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
302 mbedtls_mpi_fill_random( &X, 42, NULL, 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_gcd( NULL, &X, &X ) );
306 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
307 mbedtls_mpi_gcd( &X, NULL, &X ) );
308 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
309 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000310
Hanno Beckerafb607b2018-12-11 14:27:08 +0000311 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
312 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
313 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
314 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
315 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000316 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000317
318exit:
319 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000320}
321/* END_CASE */
322
Paul Bakker33b43f12013-08-20 11:48:36 +0200323/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100324void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200325{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200326 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200327
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200328 mbedtls_mpi_init( &X );
329 mbedtls_mpi_init( &Y );
330 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200331
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200332 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
333 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200334 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200335 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200336
337exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200338 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200339}
340/* END_CASE */
341
342/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100343void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
344 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200345 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000346{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000348 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100349 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000352
Janos Follath04dadb72019-03-06 12:29:37 +0000353 memset( str, '!', sizeof( str ) );
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200356 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000357 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100358 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200359 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000360 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200361 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000362 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000363 }
364 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000365
Paul Bakkerbd51b262014-07-10 15:26:12 +0200366exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000368}
Paul Bakker33b43f12013-08-20 11:48:36 +0200369/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000370
Paul Bakker33b43f12013-08-20 11:48:36 +0200371/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100372void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000375 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100376 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000379
Paul Bakkere896fea2009-07-06 06:40:23 +0000380
Azim Khand30ca132017-06-09 04:32:58 +0100381 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000382 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200383 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000384
Paul Bakkerbd51b262014-07-10 15:26:12 +0200385exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000387}
Paul Bakker33b43f12013-08-20 11:48:36 +0200388/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000389
Paul Bakker33b43f12013-08-20 11:48:36 +0200390/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000391void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
392{
393 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000394 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000395 size_t len;
396
397 mbedtls_mpi_init( &X );
398
399
400 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000401 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000402 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
403
404exit:
405 mbedtls_mpi_free( &X );
406}
407/* END_CASE */
408
409/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100410void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100411 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100412 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000413{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000415 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000416 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000417
418 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200425 if( buflen > (size_t) output_size )
426 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200429 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000430 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000431
Ronald Cron2dbba992020-06-10 11:42:32 +0200432 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
433 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000434 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000435
Paul Bakkerbd51b262014-07-10 15:26:12 +0200436exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000438}
Paul Bakker33b43f12013-08-20 11:48:36 +0200439/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000440
Janos Follathe344d0f2019-02-19 16:17:40 +0000441/* BEGIN_CASE */
442void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
443 data_t * input_A, int output_size,
444 int result )
445{
446 mbedtls_mpi X;
447 unsigned char buf[1000];
448 size_t buflen;
449
450 memset( buf, 0x00, 1000 );
451
452 mbedtls_mpi_init( &X );
453
454 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
455
456 buflen = mbedtls_mpi_size( &X );
457 if( buflen > (size_t) output_size )
458 buflen = (size_t) output_size;
459
460 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
461 if( result == 0)
462 {
463
Ronald Cron2dbba992020-06-10 11:42:32 +0200464 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
465 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000466 }
467
468exit:
469 mbedtls_mpi_free( &X );
470}
471/* END_CASE */
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100474void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100475 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000478 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000479 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000480 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000481 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000482
483 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000486
Paul Bakker33b43f12013-08-20 11:48:36 +0200487 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200488 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000490 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000491 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000492
Paul Bakker33b43f12013-08-20 11:48:36 +0200493 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 buflen = mbedtls_mpi_size( &X );
496 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000497
Paul Bakkere896fea2009-07-06 06:40:23 +0000498
Ronald Cron2dbba992020-06-10 11:42:32 +0200499 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
500 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000501 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000502
Paul Bakkerbd51b262014-07-10 15:26:12 +0200503exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000505}
Paul Bakker33b43f12013-08-20 11:48:36 +0200506/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100509void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
510 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000511{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000513 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200514 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000519
Paul Bakker33b43f12013-08-20 11:48:36 +0200520 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000521 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200522 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000523 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200524 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000525
Paul Bakker33b43f12013-08-20 11:48:36 +0200526 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000527 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200528 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000529 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200530 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000533
Paul Bakkerbd51b262014-07-10 15:26:12 +0200534exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000536}
Paul Bakker33b43f12013-08-20 11:48:36 +0200537/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000538
Paul Bakker33b43f12013-08-20 11:48:36 +0200539/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100540void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000541{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_mpi X;
543 mbedtls_mpi_init( &X );
544 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
545 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000546
Paul Bakkerbd51b262014-07-10 15:26:12 +0200547exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000549}
Paul Bakker33b43f12013-08-20 11:48:36 +0200550/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000551
Paul Bakker33b43f12013-08-20 11:48:36 +0200552/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100553void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
554 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_mpi X, Y;
557 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
560 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100561 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
562
563 if( result == 0 )
564 {
565 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
566 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000567
Paul Bakkerbd51b262014-07-10 15:26:12 +0200568exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000570}
Paul Bakker33b43f12013-08-20 11:48:36 +0200571/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000572
Paul Bakker33b43f12013-08-20 11:48:36 +0200573/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100574void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000575{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_mpi X;
577 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
580 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000581
Paul Bakkerbd51b262014-07-10 15:26:12 +0200582exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000584}
Paul Bakker33b43f12013-08-20 11:48:36 +0200585/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000586
Paul Bakker33b43f12013-08-20 11:48:36 +0200587/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100588void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000589{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_mpi X;
591 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200594 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000595
Paul Bakkerbd51b262014-07-10 15:26:12 +0200596exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000598}
Paul Bakker33b43f12013-08-20 11:48:36 +0200599/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000600
Paul Bakker33b43f12013-08-20 11:48:36 +0200601/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100602void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
603 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_mpi A, X, Y, Z;
606 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
609 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
610 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
611 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
612 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000613
Paul Bakkerbd51b262014-07-10 15:26:12 +0200614exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000616}
Paul Bakker33b43f12013-08-20 11:48:36 +0200617/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000618
Paul Bakker33b43f12013-08-20 11:48:36 +0200619/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000621{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_mpi X;
623 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
626 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000627
Paul Bakkerbd51b262014-07-10 15:26:12 +0200628exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000630}
Paul Bakker33b43f12013-08-20 11:48:36 +0200631/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000632
Paul Bakker33b43f12013-08-20 11:48:36 +0200633/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100634void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
635 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_mpi X, Y;
638 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
641 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
642 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000643
Paul Bakkerbd51b262014-07-10 15:26:12 +0200644exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000646}
Paul Bakker33b43f12013-08-20 11:48:36 +0200647/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000648
Paul Bakker33b43f12013-08-20 11:48:36 +0200649/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100650void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
651 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100652 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100653{
Gilles Peskine0deccf12020-09-02 15:18:07 +0200654 unsigned ret = -1;
Janos Follath0e5532d2019-10-11 14:21:53 +0100655 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100656 mbedtls_mpi X, Y;
657 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
658
Janos Follathb7e1b492019-10-14 09:21:49 +0100659 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
660 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100661
Gilles Peskine9018b112020-01-21 16:30:53 +0100662 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
663 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100664
Janos Follath0e5532d2019-10-11 14:21:53 +0100665 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100666 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100667 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100668
669exit:
670 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
671}
672/* END_CASE */
673
674/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100675void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
676 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_mpi X, Y;
679 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
682 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
683 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000684
Paul Bakkerbd51b262014-07-10 15:26:12 +0200685exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000687}
Paul Bakker33b43f12013-08-20 11:48:36 +0200688/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000689
Paul Bakker33b43f12013-08-20 11:48:36 +0200690/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100691void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000692{
Gilles Peskine7428b452020-01-20 21:01:51 +0100693 mbedtls_mpi X, Y;
694 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100697 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100700 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
701 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000702
Paul Bakkerbd51b262014-07-10 15:26:12 +0200703exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100704 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
705}
706/* END_CASE */
707
708/* BEGIN_CASE */
709void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
710{
711 mbedtls_mpi X, Y, X0;
712 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
713
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100714 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
715 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
716 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100717 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
718
719 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
720 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
721 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
722
723exit:
724 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000725}
Paul Bakker33b43f12013-08-20 11:48:36 +0200726/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000727
Paul Bakker33b43f12013-08-20 11:48:36 +0200728/* BEGIN_CASE */
729void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_mpi X;
732 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
735 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
736 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000737
Paul Bakkerbd51b262014-07-10 15:26:12 +0200738exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000740}
Paul Bakker33b43f12013-08-20 11:48:36 +0200741/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000742
Paul Bakker33b43f12013-08-20 11:48:36 +0200743/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100745{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_mpi X;
747 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100750 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
752 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100753 TEST_ASSERT( X.n == (size_t) after );
754
Paul Bakkerbd51b262014-07-10 15:26:12 +0200755exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100757}
758/* END_CASE */
759
760/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100761void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
762 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100763{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_mpi X, Y, XX;
765 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100768 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100770 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
774 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
777 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100778
Paul Bakkerbd51b262014-07-10 15:26:12 +0200779exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100781}
782/* END_CASE */
783
784/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100785void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
786 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
791 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100794 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100796 Y.s = y_sign;
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
799 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
802 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
803 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
806 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
807 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100808
Paul Bakkerbd51b262014-07-10 15:26:12 +0200809exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
811 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100812}
813/* END_CASE */
814
815/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100816void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000817{
Gilles Peskine7428b452020-01-20 21:01:51 +0100818 mbedtls_mpi X, Y;
819 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
822 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100823 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
824 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine7428b452020-01-20 21:01:51 +0100827 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
828 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000829
Paul Bakkerbd51b262014-07-10 15:26:12 +0200830exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100831 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
832}
833/* END_CASE */
834
835/* BEGIN_CASE */
836void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
837{
838 mbedtls_mpi X, Y, X0, Y0;
839 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
840 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
841
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100842 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
843 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
844 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
845 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100846
847 mbedtls_mpi_swap( &X, &Y );
848 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
849 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
850
851exit:
852 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
853 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
854}
855/* END_CASE */
856
857/* BEGIN_CASE */
858void mpi_swap_self( data_t *input_X )
859{
860 mbedtls_mpi X, X0;
861 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
862
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100863 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
864 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100865
866 mbedtls_mpi_swap( &X, &X );
867 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
868
869exit:
870 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000871}
Paul Bakker33b43f12013-08-20 11:48:36 +0200872/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000873
Paul Bakker33b43f12013-08-20 11:48:36 +0200874/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100875void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
876 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000877{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_mpi X, Y, Z, A;
879 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
882 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
883 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
884 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
885 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000886
Gilles Peskine56f943a2020-07-23 01:18:11 +0200887 /* result == first operand */
888 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
889 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
890 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
891
892 /* result == second operand */
893 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
894 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
895
Paul Bakkerbd51b262014-07-10 15:26:12 +0200896exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000898}
Paul Bakker33b43f12013-08-20 11:48:36 +0200899/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000900
Paul Bakker33b43f12013-08-20 11:48:36 +0200901/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100902void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
903 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100904{
905 mbedtls_mpi X, A;
906 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
907
Janos Follath044a86b2015-10-25 10:58:03 +0100908 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100909
910 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
911 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
912 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
913
914 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
915 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
916 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
917
918 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100919 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
920 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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100929void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
930 char * input_Y, int radix_A, 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
936 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
937 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
938 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
939 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000940
Gilles Peskine56f943a2020-07-23 01:18:11 +0200941 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
943 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Gilles Peskine56f943a2020-07-23 01:18:11 +0200945
946 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
948 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000949
Paul Bakkerbd51b262014-07-10 15:26:12 +0200950exit:
Gilles Peskine56f943a2020-07-23 01:18:11 +0200951 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000952}
Paul Bakker33b43f12013-08-20 11:48:36 +0200953/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000954
Paul Bakker33b43f12013-08-20 11:48:36 +0200955/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100956void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
957 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000958{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 mbedtls_mpi X, Z, A;
960 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
963 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
964 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
965 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000966
Paul Bakkerbd51b262014-07-10 15:26:12 +0200967exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000969}
Paul Bakker33b43f12013-08-20 11:48:36 +0200970/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000971
Paul Bakker33b43f12013-08-20 11:48:36 +0200972/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100973void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
974 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000975{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_mpi X, Y, Z, A;
977 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
980 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
981 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
982 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
983 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000984
Gilles Peskine56f943a2020-07-23 01:18:11 +0200985 /* result == first operand */
986 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
987 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
988 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
989
990 /* result == second operand */
991 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
992 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
993
Paul Bakkerbd51b262014-07-10 15:26:12 +0200994exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000996}
Paul Bakker33b43f12013-08-20 11:48:36 +0200997/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000998
Paul Bakker33b43f12013-08-20 11:48:36 +0200999/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001000void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
1001 char * input_Y, int radix_A, char * input_A,
1002 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001003{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001005 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1009 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1010 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +01001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001013 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001014 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001016
Gilles Peskine56f943a2020-07-23 01:18:11 +02001017 /* result == first operand */
1018 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
1019 if( sub_result == 0 )
1020 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
1021 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1022
1023 /* result == second operand */
1024 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
1025 if( sub_result == 0 )
1026 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
1027
Paul Bakkerbd51b262014-07-10 15:26:12 +02001028exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001030}
Paul Bakker33b43f12013-08-20 11:48:36 +02001031/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001032
Paul Bakker33b43f12013-08-20 11:48:36 +02001033/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001034void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
1035 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001036{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 mbedtls_mpi X, Z, A;
1038 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1041 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1042 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
1043 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001044
Paul Bakkerbd51b262014-07-10 15:26:12 +02001045exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001047}
Paul Bakker33b43f12013-08-20 11:48:36 +02001048/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001049
Paul Bakker33b43f12013-08-20 11:48:36 +02001050/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001051void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
1052 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_mpi X, Y, Z, A;
1055 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1058 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1059 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1060 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
1061 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001062
Paul Bakkerbd51b262014-07-10 15:26:12 +02001063exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001065}
Paul Bakker33b43f12013-08-20 11:48:36 +02001066/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001067
Paul Bakker33b43f12013-08-20 11:48:36 +02001068/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001069void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1070 int radix_A, char * input_A,
1071 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001072{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_mpi X, Z, A;
1074 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1077 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1078 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001079 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001081 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001083 else
1084 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001085
Paul Bakkerbd51b262014-07-10 15:26:12 +02001086exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001088}
Paul Bakker33b43f12013-08-20 11:48:36 +02001089/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001090
Paul Bakker33b43f12013-08-20 11:48:36 +02001091/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001092void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1093 char * input_Y, int radix_A, char * input_A,
1094 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001097 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1099 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1102 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1103 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1104 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1105 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001106 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001107 if( res == 0 )
1108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1110 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001111 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001112
Paul Bakkerbd51b262014-07-10 15:26:12 +02001113exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1115 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001116}
Paul Bakker33b43f12013-08-20 11:48:36 +02001117/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001118
Paul Bakker33b43f12013-08-20 11:48:36 +02001119/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001120void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1121 int radix_A, char * input_A, int radix_B,
1122 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001125 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1127 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1130 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1131 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1132 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001133 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001134 if( res == 0 )
1135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1137 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001138 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001139
Paul Bakkerbd51b262014-07-10 15:26:12 +02001140exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1142 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001143}
Paul Bakker33b43f12013-08-20 11:48:36 +02001144/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001145
Paul Bakker33b43f12013-08-20 11:48:36 +02001146/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001147void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1148 char * input_Y, int radix_A, char * input_A,
1149 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001152 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1156 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1157 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1158 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001159 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001160 if( res == 0 )
1161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001163 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001164
Paul Bakkerbd51b262014-07-10 15:26:12 +02001165exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001167}
Paul Bakker33b43f12013-08-20 11:48:36 +02001168/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001169
Paul Bakker33b43f12013-08-20 11:48:36 +02001170/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001171void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1172 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001173{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001175 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 mbedtls_mpi_uint r;
1177 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1180 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001181 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001182 if( res == 0 )
1183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001185 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001186
Paul Bakkerbd51b262014-07-10 15:26:12 +02001187exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001189}
Paul Bakker33b43f12013-08-20 11:48:36 +02001190/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001191
Paul Bakker33b43f12013-08-20 11:48:36 +02001192/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001193void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1194 char * input_E, int radix_N, char * input_N,
1195 int radix_RR, char * input_RR, int radix_X,
1196 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001197{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001199 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1201 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1204 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1205 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1206 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001207
Paul Bakker33b43f12013-08-20 11:48:36 +02001208 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001212 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001213 if( res == 0 )
1214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001216 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001217
Paul Bakkerbd51b262014-07-10 15:26:12 +02001218exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1220 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001221}
Paul Bakker33b43f12013-08-20 11:48:36 +02001222/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001223
Paul Bakker33b43f12013-08-20 11:48:36 +02001224/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001225void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesaa850cd2020-12-03 11:35:41 +00001226 int radix_RR, char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001227{
1228 mbedtls_mpi A, E, N, RR, Z;
1229 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1230 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1231
Chris Jonesaa850cd2020-12-03 11:35:41 +00001232 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001233 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001234 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001235 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001236
1237 /* Set E to 2^(E_bytes - 1) + 1 */
1238 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1239 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001240 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001241
1242 /* Set N to 2^(N_bytes - 1) + 1 */
1243 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1244 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001245 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1246
1247 if( strlen( input_RR ) )
1248 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
1249
Chris Jonesaa850cd2020-12-03 11:35:41 +00001250 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001251
1252exit:
1253 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1254 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1255}
1256/* END_CASE */
1257
1258/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001259void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1260 char * input_Y, int radix_A, char * input_A,
1261 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001264 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1268 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1269 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1270 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001271 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001272 if( res == 0 )
1273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001275 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001276
Paul Bakkerbd51b262014-07-10 15:26:12 +02001277exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001279}
Paul Bakker33b43f12013-08-20 11:48:36 +02001280/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001283void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001286 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001290 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001291 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001292
Paul Bakkerbd51b262014-07-10 15:26:12 +02001293exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001295}
Paul Bakker33b43f12013-08-20 11:48:36 +02001296/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001299void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001300 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001301{
1302 mbedtls_mpi X;
1303 int res;
1304 mbedtls_test_mpi_random rand;
1305
1306 mbedtls_mpi_init( &X );
1307 rand.data = witnesses;
1308 rand.pos = 0;
1309 rand.chunk_len = chunk_len;
1310
1311 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001312 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1313 mbedtls_test_mpi_miller_rabin_determinizer,
1314 &rand );
1315 TEST_ASSERT( res == 0 );
1316
1317 rand.data = witnesses;
1318 rand.pos = 0;
1319 rand.chunk_len = chunk_len;
1320
Janos Follatha0b67c22018-09-18 14:48:23 +01001321 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1322 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001323 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001324 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001325
1326exit:
1327 mbedtls_mpi_free( &X );
1328}
1329/* END_CASE */
1330
1331/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001332void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001335 int my_ret;
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001338
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001339 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1340 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001341 TEST_ASSERT( my_ret == ref_ret );
1342
1343 if( ref_ret == 0 )
1344 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001345 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001346
1347 TEST_ASSERT( actual_bits >= (size_t) bits );
1348 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1349
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001350 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1351 mbedtls_test_rnd_std_rand,
1352 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001353 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001354 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001355 /* X = ( X - 1 ) / 2 */
1356 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001357 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1358 mbedtls_test_rnd_std_rand,
1359 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001360 }
1361 }
1362
Paul Bakkerbd51b262014-07-10 15:26:12 +02001363exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001365}
1366/* END_CASE */
1367
Paul Bakker33b43f12013-08-20 11:48:36 +02001368/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001369void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1370 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001371{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 mbedtls_mpi X, A;
1373 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1376 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1377 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1378 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001379
Paul Bakkerbd51b262014-07-10 15:26:12 +02001380exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001382}
Paul Bakker33b43f12013-08-20 11:48:36 +02001383/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001384
Paul Bakker33b43f12013-08-20 11:48:36 +02001385/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001386void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1387 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 mbedtls_mpi X, A;
1390 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1393 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1394 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1395 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001396
Paul Bakkerbd51b262014-07-10 15:26:12 +02001397exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001399}
Paul Bakker33b43f12013-08-20 11:48:36 +02001400/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001401
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001402/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001403void mpi_fill_random( int wanted_bytes, int rng_bytes,
1404 int before, int expected_ret )
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001405{
1406 mbedtls_mpi X;
1407 int ret;
1408 size_t bytes_left = rng_bytes;
1409 mbedtls_mpi_init( &X );
1410
Gilles Peskine422e8672021-04-02 00:02:27 +02001411 if( before != 0 )
1412 {
1413 /* Set X to sign(before) * 2^(|before|-1) */
1414 TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
1415 if( before < 0 )
1416 before = - before;
1417 TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
1418 }
1419
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001420 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1421 f_rng_bytes_left, &bytes_left );
1422 TEST_ASSERT( ret == expected_ret );
1423
1424 if( expected_ret == 0 )
1425 {
1426 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1427 * as a big-endian representation of the number. We know when
1428 * our RNG function returns null bytes, so we know how many
1429 * leading zero bytes the number has. */
1430 size_t leading_zeros = 0;
1431 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1432 leading_zeros = 1;
1433 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1434 (size_t) wanted_bytes );
1435 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
1436 }
1437
1438exit:
1439 mbedtls_mpi_free( &X );
1440}
1441/* END_CASE */
1442
Gilles Peskine02ac93a2021-03-29 22:02:55 +02001443/* BEGIN_CASE */
1444void mpi_random_many( int min, data_t *bound_bytes, int iterations )
1445{
1446 /* Generate numbers in the range 1..bound-1. Do it iterations times.
1447 * This function assumes that the value of bound is at least 2 and
1448 * that iterations is large enough that a one-in-2^iterations chance
1449 * effectively never occurs.
1450 */
1451
1452 mbedtls_mpi upper_bound;
1453 size_t n_bits;
1454 mbedtls_mpi result;
1455 size_t b;
1456 /* If upper_bound is small, stats[b] is the number of times the value b
1457 * has been generated. Otherwise stats[b] is the number of times a
1458 * value with bit b set has been generated. */
1459 size_t *stats = NULL;
1460 size_t stats_len;
1461 int full_stats;
1462 size_t i;
1463
1464 mbedtls_mpi_init( &upper_bound );
1465 mbedtls_mpi_init( &result );
1466
1467 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1468 bound_bytes->x, bound_bytes->len ) );
1469 n_bits = mbedtls_mpi_bitlen( &upper_bound );
1470 /* Consider a bound "small" if it's less than 2^5. This value is chosen
1471 * to be small enough that the probability of missing one value is
1472 * negligible given the number of iterations. It must be less than
1473 * 256 because some of the code below assumes that "small" values
1474 * fit in a byte. */
1475 if( n_bits <= 5 )
1476 {
1477 full_stats = 1;
1478 stats_len = bound_bytes->x[bound_bytes->len - 1];
1479 }
1480 else
1481 {
1482 full_stats = 0;
1483 stats_len = n_bits;
1484 }
1485 ASSERT_ALLOC( stats, stats_len );
1486
1487 for( i = 0; i < (size_t) iterations; i++ )
1488 {
1489 mbedtls_test_set_step( i );
1490 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1491 mbedtls_test_rnd_std_rand, NULL ) );
1492
1493 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1494 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1495 if( full_stats )
1496 {
1497 uint8_t value;
1498 TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
1499 TEST_ASSERT( value < stats_len );
1500 ++stats[value];
1501 }
1502 else
1503 {
1504 for( b = 0; b < n_bits; b++ )
1505 stats[b] += mbedtls_mpi_get_bit( &result, b );
1506 }
1507 }
1508
1509 if( full_stats )
1510 {
1511 for( b = 1; b < stats_len; b++ )
1512 {
1513 mbedtls_test_set_step( 1000000 + b );
1514 /* Assert that each value has been reached at least once.
1515 * This is almost guaranteed if the iteration count is large
1516 * enough. This is a very crude way of checking the distribution.
1517 */
1518 TEST_ASSERT( stats[b] > 0 );
1519 }
1520 }
1521 else
1522 {
1523 for( b = 0; b < n_bits; b++ )
1524 {
1525 mbedtls_test_set_step( 1000000 + b );
1526 /* Assert that each bit has been set in at least one result and
1527 * clear in at least one result. Provided that iterations is not
1528 * too small, it would be extremely unlikely for this not to be
1529 * the case if the results are uniformly distributed.
1530 *
1531 * As an exception, the top bit may legitimately never be set
1532 * if bound is a power of 2 or only slightly above.
1533 */
1534 if( b != n_bits - 1 ||
1535 is_significantly_above_a_power_of_2( bound_bytes ) )
1536 {
1537 TEST_ASSERT( stats[b] > 0 );
1538 }
1539 TEST_ASSERT( stats[b] < (size_t) iterations );
1540 }
1541 }
1542
1543exit:
1544 mbedtls_mpi_free( &upper_bound );
1545 mbedtls_mpi_free( &result );
1546 mbedtls_free( stats );
1547}
1548/* END_CASE */
1549
Gilles Peskine1e918f42021-03-29 22:14:51 +02001550/* BEGIN_CASE */
Gilles Peskine422e8672021-04-02 00:02:27 +02001551void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001552{
1553 mbedtls_mpi upper_bound;
1554 mbedtls_mpi result;
1555
1556 mbedtls_mpi_init( &upper_bound );
1557 mbedtls_mpi_init( &result );
1558
Gilles Peskine422e8672021-04-02 00:02:27 +02001559 if( before != 0 )
1560 {
1561 /* Set result to sign(before) * 2^(|before|-1) */
1562 TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
1563 if( before < 0 )
1564 before = - before;
1565 TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
1566 }
1567
Gilles Peskine1a7df4e2021-04-01 15:57:18 +02001568 TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
1569 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1570 bound_bytes->x, bound_bytes->len ) );
1571 TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
1572 mbedtls_test_rnd_std_rand, NULL ) );
1573 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
1574 TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
1575
1576exit:
1577 mbedtls_mpi_free( &upper_bound );
1578 mbedtls_mpi_free( &result );
1579}
1580/* END_CASE */
1581
1582/* BEGIN_CASE */
Gilles Peskine1e918f42021-03-29 22:14:51 +02001583void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
1584{
1585 mbedtls_mpi upper_bound;
1586 mbedtls_mpi result;
1587 int actual_ret;
1588
1589 mbedtls_mpi_init( &upper_bound );
1590 mbedtls_mpi_init( &result );
1591
1592 TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
1593 bound_bytes->x, bound_bytes->len ) );
1594 actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
1595 mbedtls_test_rnd_std_rand, NULL );
1596 TEST_EQUAL( expected_ret, actual_ret );
1597
1598exit:
1599 mbedtls_mpi_free( &upper_bound );
1600 mbedtls_mpi_free( &result );
1601}
1602/* END_CASE */
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001605void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001606{
Andres AG93012e82016-09-09 09:10:28 +01001607 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001608}
Paul Bakker33b43f12013-08-20 11:48:36 +02001609/* END_CASE */