blob: 9de9827beeb7da78f5a671f1aef1879049556a31 [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 Peskine2f780622020-11-25 15:37:20 +01003#include "mbedtls/entropy.h"
Janos Follath64eca052018-09-05 17:04:49 +01004
Chris Jones5dd1e262020-12-03 17:44:03 +00005#if MBEDTLS_MPI_MAX_BITS > 792
6#define MPI_MAX_BITS_LARGER_THAN_792
Chris Jonesce6fa8f2020-12-03 14:24:33 +00007#endif
Janos Follath64eca052018-09-05 17:04:49 +01008
Gilles Peskine8854c5d2021-06-10 15:34:15 +02009/* Check the validity of the sign bit in an MPI object. Reject representations
10 * that are not supported by the rest of the library and indicate a bug when
11 * constructing the value. */
12static int sign_is_valid( const mbedtls_mpi *X )
13{
14 if( X->s != 1 && X->s != -1 )
15 return( 0 ); // invalid sign bit, e.g. 0
16 if( mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 )
17 return( 0 ); // negative zero
18 return( 1 );
19}
20
Janos Follath64eca052018-09-05 17:04:49 +010021typedef struct mbedtls_test_mpi_random
22{
23 data_t *data;
24 size_t pos;
25 size_t chunk_len;
26} mbedtls_test_mpi_random;
27
28/*
29 * This function is called by the Miller-Rabin primality test each time it
30 * chooses a random witness. The witnesses (or non-witnesses as provided by the
31 * test) are stored in the data member of the state structure. Each number is in
32 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
33 */
34int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
35 unsigned char* buf,
36 size_t len )
37{
38 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
39
40 if( random == NULL || random->data->x == NULL || buf == NULL )
41 return( -1 );
42
43 if( random->pos + random->chunk_len > random->data->len
44 || random->chunk_len > len )
45 {
46 return( -1 );
47 }
48
49 memset( buf, 0, len );
50
51 /* The witness is written to the end of the buffer, since the buffer is
52 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
53 * Writing the witness to the start of the buffer would result in the
54 * buffer being 'witness 000...000', which would be treated as
55 * witness * 2^n for some n. */
56 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
57 random->chunk_len );
58
59 random->pos += random->chunk_len;
60
61 return( 0 );
62}
Gilles Peskine2f780622020-11-25 15:37:20 +010063
64/* Random generator that is told how many bytes to return. */
65static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
66{
67 size_t *bytes_left = state;
68 size_t i;
69 for( i = 0; i < len; i++ )
70 {
71 if( *bytes_left == 0 )
72 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
73 buf[i] = *bytes_left & 0xff;
74 --( *bytes_left );
75 }
76 return( 0 );
77}
78
Paul Bakker33b43f12013-08-20 11:48:36 +020079/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000080
Paul Bakker33b43f12013-08-20 11:48:36 +020081/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020083 * END_DEPENDENCIES
84 */
Paul Bakker5690efc2011-05-26 13:16:06 +000085
Hanno Beckerb48e1aa2018-12-18 23:25:01 +000086/* BEGIN_CASE */
87void mpi_valid_param( )
88{
89 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
90}
91/* END_CASE */
92
Hanno Beckerafb607b2018-12-11 14:27:08 +000093/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
94void mpi_invalid_param( )
95{
96 mbedtls_mpi X;
97 const char *s_in = "00101000101010";
98 char s_out[16] = { 0 };
99 unsigned char u_out[16] = { 0 };
100 unsigned char u_in[16] = { 0 };
101 size_t olen;
102 mbedtls_mpi_uint mpi_uint;
103
104 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000105
Hanno Beckerafb607b2018-12-11 14:27:08 +0000106 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
107 mbedtls_mpi_grow( NULL, 42 ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
109 mbedtls_mpi_copy( NULL, &X ) );
110 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
111 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000112
Hanno Beckerafb607b2018-12-11 14:27:08 +0000113 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
114 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000115
Hanno Beckerafb607b2018-12-11 14:27:08 +0000116 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
117 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
119 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000120
Hanno Beckerafb607b2018-12-11 14:27:08 +0000121 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
122 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
123 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
124 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000125
Hanno Beckerafb607b2018-12-11 14:27:08 +0000126 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
127 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000128
Hanno Beckerafb607b2018-12-11 14:27:08 +0000129 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
130 mbedtls_mpi_get_bit( NULL, 42 ) );
131 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
132 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000133
Hanno Beckerafb607b2018-12-11 14:27:08 +0000134 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
135 mbedtls_mpi_read_string( NULL, 2, s_in ) );
136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
137 mbedtls_mpi_read_string( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000138
Hanno Beckerafb607b2018-12-11 14:27:08 +0000139 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
140 mbedtls_mpi_write_string( NULL, 2,
141 s_out, sizeof( s_out ),
142 &olen ) );
143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
144 mbedtls_mpi_write_string( &X, 2,
145 NULL, sizeof( s_out ),
146 &olen ) );
147 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
148 mbedtls_mpi_write_string( &X, 2,
149 s_out, sizeof( s_out ),
150 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000151
Hanno Beckerafb607b2018-12-11 14:27:08 +0000152#if defined(MBEDTLS_FS_IO)
153 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
154 mbedtls_mpi_read_file( NULL, 2, stdin ) );
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
156 mbedtls_mpi_read_file( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000157
Hanno Beckerafb607b2018-12-11 14:27:08 +0000158 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
159 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
160#endif /* MBEDTLS_FS_IO */
161
162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
163 mbedtls_mpi_read_binary( NULL, u_in,
164 sizeof( u_in ) ) );
165 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
166 mbedtls_mpi_read_binary( &X, NULL,
167 sizeof( u_in ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000168
Hanno Beckerafb607b2018-12-11 14:27:08 +0000169 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
170 mbedtls_mpi_write_binary( NULL, u_out,
171 sizeof( u_out ) ) );
172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
173 mbedtls_mpi_write_binary( &X, NULL,
174 sizeof( u_out ) ) );
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_shift_l( NULL, 42 ) );
178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
179 mbedtls_mpi_shift_r( NULL, 42 ) );
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_cmp_abs( NULL, &X ) );
183 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
184 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000185
Hanno Beckerafb607b2018-12-11 14:27:08 +0000186 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
187 mbedtls_mpi_cmp_mpi( NULL, &X ) );
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
189 mbedtls_mpi_cmp_mpi( &X, NULL ) );
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_cmp_int( NULL, 42 ) );
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_add_abs( NULL, &X, &X ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
197 mbedtls_mpi_add_abs( &X, NULL, &X ) );
198 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
199 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000200
Hanno Beckerafb607b2018-12-11 14:27:08 +0000201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
202 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
203 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
204 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
206 mbedtls_mpi_sub_abs( &X, &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_add_mpi( NULL, &X, &X ) );
210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
211 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
212 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
213 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000214
Hanno Beckerafb607b2018-12-11 14:27:08 +0000215 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
216 mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
217 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
218 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
219 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
220 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000221
Hanno Beckerafb607b2018-12-11 14:27:08 +0000222 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
223 mbedtls_mpi_add_int( NULL, &X, 42 ) );
224 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
225 mbedtls_mpi_add_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000226
Hanno Beckerafb607b2018-12-11 14:27:08 +0000227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
228 mbedtls_mpi_sub_int( NULL, &X, 42 ) );
229 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
230 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000231
Hanno Beckerafb607b2018-12-11 14:27:08 +0000232 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
233 mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
234 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
235 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
236 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
237 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000238
Hanno Beckerafb607b2018-12-11 14:27:08 +0000239 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
240 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
241 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
242 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
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_div_mpi( &X, &X, NULL, &X ) );
246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
247 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
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_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000251
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000252 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
253
Hanno Beckerafb607b2018-12-11 14:27:08 +0000254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
255 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
259 mbedtls_mpi_mod_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_mod_int( NULL, &X, 42 ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
264 mbedtls_mpi_mod_int( &mpi_uint, 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_exp_mod( NULL, &X, &X, &X, NULL ) );
268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
269 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
271 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
272 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
273 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000274
Hanno Beckerafb607b2018-12-11 14:27:08 +0000275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
276 mbedtls_mpi_fill_random( NULL, 42, rnd_std_rand,
277 NULL ) );
278 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
279 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000280
Hanno Beckerafb607b2018-12-11 14:27:08 +0000281 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
282 mbedtls_mpi_gcd( NULL, &X, &X ) );
283 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
284 mbedtls_mpi_gcd( &X, NULL, &X ) );
285 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
286 mbedtls_mpi_gcd( &X, &X, NULL ) );
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_inv_mod( NULL, &X, &X ) );
290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
291 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
292 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000293 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000294
295exit:
296 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000297}
298/* END_CASE */
299
Paul Bakker33b43f12013-08-20 11:48:36 +0200300/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100301void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200302{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200303 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200304
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200305 mbedtls_mpi_init( &X );
306 mbedtls_mpi_init( &Y );
307 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200308
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200309 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
310 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200311 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200312 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200313
314exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200315 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200316}
317/* END_CASE */
318
319/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100320void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
321 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200322 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000323{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000325 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100326 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000329
Janos Follath276284f2019-03-06 12:29:37 +0000330 memset( str, '!', sizeof( str ) );
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200333 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000334 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200335 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100336 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200337 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000338 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200339 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath276284f2019-03-06 12:29:37 +0000340 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000341 }
342 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000343
Paul Bakkerbd51b262014-07-10 15:26:12 +0200344exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000346}
Paul Bakker33b43f12013-08-20 11:48:36 +0200347/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000348
Paul Bakker33b43f12013-08-20 11:48:36 +0200349/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100350void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000353 unsigned char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100354 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000357
Paul Bakkere896fea2009-07-06 06:40:23 +0000358
Azim Khand30ca132017-06-09 04:32:58 +0100359 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200360 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100361 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200362 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000363
Paul Bakkerbd51b262014-07-10 15:26:12 +0200364exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000366}
Paul Bakker33b43f12013-08-20 11:48:36 +0200367/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000368
Paul Bakker33b43f12013-08-20 11:48:36 +0200369/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100370void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100371 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100372 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000375 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000376 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000377
378 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000381
Gilles Peskineb8e15342021-06-10 23:18:39 +0200382 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200385 if( buflen > (size_t) output_size )
386 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200389 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000390 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000391
Ronald Cron9fde3532020-06-10 11:42:32 +0200392 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
393 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000394 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000395
Paul Bakkerbd51b262014-07-10 15:26:12 +0200396exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000398}
Paul Bakker33b43f12013-08-20 11:48:36 +0200399/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100402void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100403 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000406 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000407 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000408 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000409 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000410
411 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000414
Paul Bakker33b43f12013-08-20 11:48:36 +0200415 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200416 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000418 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000419 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000420
Paul Bakker33b43f12013-08-20 11:48:36 +0200421 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000422 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200423 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 buflen = mbedtls_mpi_size( &X );
425 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000426
Paul Bakkere896fea2009-07-06 06:40:23 +0000427
Ronald Cron9fde3532020-06-10 11:42:32 +0200428 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
429 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000430 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000431
Paul Bakkerbd51b262014-07-10 15:26:12 +0200432exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000434}
Paul Bakker33b43f12013-08-20 11:48:36 +0200435/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100438void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
439 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000440{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000442 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200443 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000446
Gilles Peskineb8e15342021-06-10 23:18:39 +0200447 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000448
Paul Bakker33b43f12013-08-20 11:48:36 +0200449 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000450 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200451 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000452 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200453 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000454
Paul Bakker33b43f12013-08-20 11:48:36 +0200455 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000456 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200457 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000458 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200459 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000462
Paul Bakkerbd51b262014-07-10 15:26:12 +0200463exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000465}
Paul Bakker33b43f12013-08-20 11:48:36 +0200466/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000467
Paul Bakker33b43f12013-08-20 11:48:36 +0200468/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100469void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_mpi X;
472 mbedtls_mpi_init( &X );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200473 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000475
Paul Bakkerbd51b262014-07-10 15:26:12 +0200476exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000478}
Paul Bakker33b43f12013-08-20 11:48:36 +0200479/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000480
Paul Bakker33b43f12013-08-20 11:48:36 +0200481/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100482void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
483 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_mpi X, Y;
486 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000487
Gilles Peskineb8e15342021-06-10 23:18:39 +0200488 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
489 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100490 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
491
492 if( result == 0 )
493 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200494 TEST_ASSERT( sign_is_valid( &X ) );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100495 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
496 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000497
Paul Bakkerbd51b262014-07-10 15:26:12 +0200498exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000500}
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000502
Paul Bakker33b43f12013-08-20 11:48:36 +0200503/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100504void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000505{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_mpi X;
507 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000508
Gilles Peskineb8e15342021-06-10 23:18:39 +0200509 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000511
Paul Bakkerbd51b262014-07-10 15:26:12 +0200512exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000514}
Paul Bakker33b43f12013-08-20 11:48:36 +0200515/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000516
Paul Bakker33b43f12013-08-20 11:48:36 +0200517/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100518void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_mpi X;
521 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000522
Gilles Peskineb8e15342021-06-10 23:18:39 +0200523 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200524 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000525
Paul Bakkerbd51b262014-07-10 15:26:12 +0200526exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000528}
Paul Bakker33b43f12013-08-20 11:48:36 +0200529/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000530
Paul Bakker33b43f12013-08-20 11:48:36 +0200531/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100532void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
533 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_mpi A, X, Y, Z;
536 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000537
Gilles Peskineb8e15342021-06-10 23:18:39 +0200538 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
539 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
540 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200542 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000544
Paul Bakkerbd51b262014-07-10 15:26:12 +0200545exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000547}
Paul Bakker33b43f12013-08-20 11:48:36 +0200548/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000549
Paul Bakker33b43f12013-08-20 11:48:36 +0200550/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_mpi X;
554 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
557 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000558
Paul Bakkerbd51b262014-07-10 15:26:12 +0200559exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000561}
Paul Bakker33b43f12013-08-20 11:48:36 +0200562/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000563
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100565void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
566 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_mpi X, Y;
569 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000570
Gilles Peskineb8e15342021-06-10 23:18:39 +0200571 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
572 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000574
Paul Bakkerbd51b262014-07-10 15:26:12 +0200575exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000577}
Paul Bakker33b43f12013-08-20 11:48:36 +0200578/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000579
Paul Bakker33b43f12013-08-20 11:48:36 +0200580/* BEGIN_CASE */
Janos Follath27d221a2019-10-14 09:21:49 +0100581void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
582 int size_Y, char * input_Y,
Janos Follath867a3ab2019-10-11 14:21:53 +0100583 int input_ret, int input_err )
Janos Follathe9ae6302019-09-11 16:07:14 +0100584{
Gilles Peskine319ecf32020-09-02 15:18:07 +0200585 unsigned ret = -1;
Janos Follath867a3ab2019-10-11 14:21:53 +0100586 unsigned input_uret = input_ret;
Janos Follathe9ae6302019-09-11 16:07:14 +0100587 mbedtls_mpi X, Y;
588 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
589
Gilles Peskineb8e15342021-06-10 23:18:39 +0200590 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 );
591 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, input_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100592
Gilles Peskine1a30fbb2020-01-21 16:30:53 +0100593 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
594 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100595
Janos Follath867a3ab2019-10-11 14:21:53 +0100596 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follathe9ae6302019-09-11 16:07:14 +0100597 if( input_err == 0 )
Janos Follath867a3ab2019-10-11 14:21:53 +0100598 TEST_ASSERT( ret == input_uret );
Janos Follathe9ae6302019-09-11 16:07:14 +0100599
600exit:
601 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
602}
603/* END_CASE */
604
605/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100606void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
607 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_mpi X, Y;
610 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000611
Gilles Peskineb8e15342021-06-10 23:18:39 +0200612 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
613 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
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 ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000618}
Paul Bakker33b43f12013-08-20 11:48:36 +0200619/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000620
Paul Bakker33b43f12013-08-20 11:48:36 +0200621/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100622void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000623{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100624 mbedtls_mpi X, Y;
625 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100628 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200631 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100632 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
633 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000634
Paul Bakkerbd51b262014-07-10 15:26:12 +0200635exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100636 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
637}
638/* END_CASE */
639
640/* BEGIN_CASE */
641void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
642{
643 mbedtls_mpi X, Y, X0;
644 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
645
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100646 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
647 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
648 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100649 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
650
651 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200652 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100653 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
654 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
655
656exit:
657 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000658}
Paul Bakker33b43f12013-08-20 11:48:36 +0200659/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000660
Paul Bakker33b43f12013-08-20 11:48:36 +0200661/* BEGIN_CASE */
662void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000663{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_mpi X;
665 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
668 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
669 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000670
Paul Bakkerbd51b262014-07-10 15:26:12 +0200671exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000673}
Paul Bakker33b43f12013-08-20 11:48:36 +0200674/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000675
Paul Bakker33b43f12013-08-20 11:48:36 +0200676/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_mpi X;
680 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100683 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
685 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100686 TEST_ASSERT( X.n == (size_t) after );
687
Paul Bakkerbd51b262014-07-10 15:26:12 +0200688exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100690}
691/* END_CASE */
692
693/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100694void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
695 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100696{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_mpi X, Y, XX;
698 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100699
Gilles Peskineb8e15342021-06-10 23:18:39 +0200700 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100701 X.s = x_sign;
Gilles Peskineb8e15342021-06-10 23:18:39 +0200702 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100703 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200707 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200711 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100713
Paul Bakkerbd51b262014-07-10 15:26:12 +0200714exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100716}
717/* END_CASE */
718
719/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100720void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
721 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100722{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
726 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100727
Gilles Peskineb8e15342021-06-10 23:18:39 +0200728 TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100729 X.s = x_sign;
Gilles Peskineb8e15342021-06-10 23:18:39 +0200730 TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100731 Y.s = y_sign;
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
734 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200737 TEST_ASSERT( sign_is_valid( &X ) );
738 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
740 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200743 TEST_ASSERT( sign_is_valid( &X ) );
744 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
746 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100747
Paul Bakkerbd51b262014-07-10 15:26:12 +0200748exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
750 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100751}
752/* END_CASE */
753
754/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100755void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000756{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100757 mbedtls_mpi X, Y;
758 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
761 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100762 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
763 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200766 TEST_ASSERT( sign_is_valid( &X ) );
767 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100768 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
769 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000770
Paul Bakkerbd51b262014-07-10 15:26:12 +0200771exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100772 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
773}
774/* END_CASE */
775
776/* BEGIN_CASE */
777void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
778{
779 mbedtls_mpi X, Y, X0, Y0;
780 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
781 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
782
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100783 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
784 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
785 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
786 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100787
788 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200789 TEST_ASSERT( sign_is_valid( &X ) );
790 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100791 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
792 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
793
794exit:
795 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
796 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
797}
798/* END_CASE */
799
800/* BEGIN_CASE */
801void mpi_swap_self( data_t *input_X )
802{
803 mbedtls_mpi X, X0;
804 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
805
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100806 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
807 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100808
809 mbedtls_mpi_swap( &X, &X );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200810 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100811 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
812
813exit:
814 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000815}
Paul Bakker33b43f12013-08-20 11:48:36 +0200816/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000817
Paul Bakker33b43f12013-08-20 11:48:36 +0200818/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100819void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
820 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000821{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_mpi X, Y, Z, A;
823 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000824
Gilles Peskineb8e15342021-06-10 23:18:39 +0200825 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
826 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
827 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200829 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000831
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200832 /* result == first operand */
833 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200834 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200835 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200836 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200837
838 /* result == second operand */
839 TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200840 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200841 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
842
Paul Bakkerbd51b262014-07-10 15:26:12 +0200843exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000845}
Paul Bakker33b43f12013-08-20 11:48:36 +0200846/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000847
Paul Bakker33b43f12013-08-20 11:48:36 +0200848/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100849void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
850 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100851{
852 mbedtls_mpi X, A;
853 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
854
Gilles Peskineb8e15342021-06-10 23:18:39 +0200855 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100856
Gilles Peskineb8e15342021-06-10 23:18:39 +0200857 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100858 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
859 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200860 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100861
Gilles Peskineb8e15342021-06-10 23:18:39 +0200862 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100863 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200864 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath6cbacec2015-10-25 12:29:13 +0100865 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
866
Gilles Peskineb8e15342021-06-10 23:18:39 +0200867 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100868 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200869 TEST_ASSERT( sign_is_valid( &X ) );
Janos Follath044a86b2015-10-25 10:58:03 +0100870 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
871
872exit:
873 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
874}
875/* END_CASE */
876
877
878/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100879void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
880 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000881{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_mpi X, Y, Z, A;
883 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000884
Gilles Peskineb8e15342021-06-10 23:18:39 +0200885 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
886 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
887 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200889 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000891
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200892 /* result == first operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200894 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200896 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200897
898 /* result == second operand */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200900 TEST_ASSERT( sign_is_valid( &Y ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000902
Paul Bakkerbd51b262014-07-10 15:26:12 +0200903exit:
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200904 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000905}
Paul Bakker33b43f12013-08-20 11:48:36 +0200906/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000907
Paul Bakker33b43f12013-08-20 11:48:36 +0200908/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100909void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
910 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_mpi X, Z, A;
913 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000914
Gilles Peskineb8e15342021-06-10 23:18:39 +0200915 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
916 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200918 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000920
Paul Bakkerbd51b262014-07-10 15:26:12 +0200921exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000923}
Paul Bakker33b43f12013-08-20 11:48:36 +0200924/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000925
Paul Bakker33b43f12013-08-20 11:48:36 +0200926/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100927void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
928 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000929{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_mpi X, Y, Z, A;
931 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000932
Gilles Peskineb8e15342021-06-10 23:18:39 +0200933 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
934 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
935 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200937 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000939
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200940 /* result == first operand */
941 TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200942 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200943 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200944 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200945
946 /* result == second operand */
947 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200948 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200949 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
950
Paul Bakkerbd51b262014-07-10 15:26:12 +0200951exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000953}
Paul Bakker33b43f12013-08-20 11:48:36 +0200954/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000955
Paul Bakker33b43f12013-08-20 11:48:36 +0200956/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100957void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
958 char * input_Y, int radix_A, char * input_A,
959 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000962 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000964
Gilles Peskineb8e15342021-06-10 23:18:39 +0200965 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
966 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
967 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200970 TEST_ASSERT( res == sub_result );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200971 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakker367dae42009-06-28 21:50:27 +0000972 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000974
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200975 /* result == first operand */
976 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200977 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200978 if( sub_result == 0 )
979 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Gilles Peskineb8e15342021-06-10 23:18:39 +0200980 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200981
982 /* result == second operand */
983 TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
Gilles Peskine8854c5d2021-06-10 15:34:15 +0200984 TEST_ASSERT( sign_is_valid( &Y ) );
Gilles Peskine2845fcc2020-07-23 01:18:11 +0200985 if( sub_result == 0 )
986 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
987
Paul Bakkerbd51b262014-07-10 15:26:12 +0200988exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000990}
Paul Bakker33b43f12013-08-20 11:48:36 +0200991/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000992
Paul Bakker33b43f12013-08-20 11:48:36 +0200993/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100994void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
995 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000996{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_mpi X, Z, A;
998 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000999
Gilles Peskineb8e15342021-06-10 23:18:39 +02001000 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1001 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001003 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001005
Paul Bakkerbd51b262014-07-10 15:26:12 +02001006exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001008}
Paul Bakker33b43f12013-08-20 11:48:36 +02001009/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001010
Paul Bakker33b43f12013-08-20 11:48:36 +02001011/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001012void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
1013 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_mpi X, Y, Z, A;
1016 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001017
Gilles Peskineb8e15342021-06-10 23:18:39 +02001018 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1019 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1020 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001022 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001024
Paul Bakkerbd51b262014-07-10 15:26:12 +02001025exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001027}
Paul Bakker33b43f12013-08-20 11:48:36 +02001028/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001029
Paul Bakker33b43f12013-08-20 11:48:36 +02001030/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001031void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1032 int radix_A, char * input_A,
1033 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_mpi X, Z, A;
1036 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001037
Gilles Peskineb8e15342021-06-10 23:18:39 +02001038 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1039 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001041 TEST_ASSERT( sign_is_valid( &Z ) );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001042 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001044 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001046 else
1047 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001048
Paul Bakkerbd51b262014-07-10 15:26:12 +02001049exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001051}
Paul Bakker33b43f12013-08-20 11:48:36 +02001052/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001053
Paul Bakker33b43f12013-08-20 11:48:36 +02001054/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001055void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1056 char * input_Y, int radix_A, char * input_A,
1057 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001058{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001060 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1062 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001063
Gilles Peskineb8e15342021-06-10 23:18:39 +02001064 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1065 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1066 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1067 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001069 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001070 if( res == 0 )
1071 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001072 TEST_ASSERT( sign_is_valid( &Q ) );
1073 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1075 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001076 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001077
Paul Bakkerbd51b262014-07-10 15:26:12 +02001078exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1080 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001081}
Paul Bakker33b43f12013-08-20 11:48:36 +02001082/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001083
Paul Bakker33b43f12013-08-20 11:48:36 +02001084/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001085void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1086 int radix_A, char * input_A, int radix_B,
1087 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001090 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1092 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001093
Gilles Peskineb8e15342021-06-10 23:18:39 +02001094 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1095 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1096 TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001098 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001099 if( res == 0 )
1100 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001101 TEST_ASSERT( sign_is_valid( &Q ) );
1102 TEST_ASSERT( sign_is_valid( &R ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1104 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001105 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001106
Paul Bakkerbd51b262014-07-10 15:26:12 +02001107exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1109 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001110}
Paul Bakker33b43f12013-08-20 11:48:36 +02001111/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001112
Paul Bakker33b43f12013-08-20 11:48:36 +02001113/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001114void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1115 char * input_Y, int radix_A, char * input_A,
1116 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001119 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001121
Gilles Peskineb8e15342021-06-10 23:18:39 +02001122 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1123 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1124 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001126 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001127 if( res == 0 )
1128 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001129 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001131 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001132
Paul Bakkerbd51b262014-07-10 15:26:12 +02001133exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001135}
Paul Bakker33b43f12013-08-20 11:48:36 +02001136/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001137
Paul Bakker33b43f12013-08-20 11:48:36 +02001138/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001139void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1140 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001143 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 mbedtls_mpi_uint r;
1145 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001146
Gilles Peskineb8e15342021-06-10 23:18:39 +02001147 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001149 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001150 if( res == 0 )
1151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
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 );
Paul Bakker367dae42009-06-28 21:50:27 +00001157}
Paul Bakker33b43f12013-08-20 11:48:36 +02001158/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001159
Paul Bakker33b43f12013-08-20 11:48:36 +02001160/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001161void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1162 char * input_E, int radix_N, char * input_N,
1163 int radix_RR, char * input_RR, int radix_X,
1164 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001167 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1169 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001170
Gilles Peskineb8e15342021-06-10 23:18:39 +02001171 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
1172 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
1173 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
1174 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001175
Paul Bakker33b43f12013-08-20 11:48:36 +02001176 if( strlen( input_RR ) )
Gilles Peskineb8e15342021-06-10 23:18:39 +02001177 TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001180 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001181 if( res == 0 )
1182 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001183 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
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( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1189 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001190}
Paul Bakker33b43f12013-08-20 11:48:36 +02001191/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001192
Paul Bakker33b43f12013-08-20 11:48:36 +02001193/* BEGIN_CASE */
Chris Jones415c7be2020-12-02 10:41:50 +00001194void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesa18813e2020-12-03 11:35:41 +00001195 int radix_RR, char * input_RR, int exp_result )
Chris Jones415c7be2020-12-02 10:41:50 +00001196{
1197 mbedtls_mpi A, E, N, RR, Z;
1198 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1199 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1200
Chris Jonesa18813e2020-12-03 11:35:41 +00001201 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jones415c7be2020-12-02 10:41:50 +00001202 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001203 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001204 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001205
1206 /* Set E to 2^(E_bytes - 1) + 1 */
1207 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1208 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001209 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001210
1211 /* Set N to 2^(N_bytes - 1) + 1 */
1212 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1213 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001214 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1215
1216 if( strlen( input_RR ) )
Gilles Peskineb8e15342021-06-10 23:18:39 +02001217 TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001218
Chris Jonesa18813e2020-12-03 11:35:41 +00001219 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jones415c7be2020-12-02 10:41:50 +00001220
1221exit:
1222 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1223 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1224}
1225/* END_CASE */
1226
1227/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001228void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1229 char * input_Y, int radix_A, char * input_A,
1230 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001233 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001235
Gilles Peskineb8e15342021-06-10 23:18:39 +02001236 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1237 TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
1238 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001240 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001241 if( res == 0 )
1242 {
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001243 TEST_ASSERT( sign_is_valid( &Z ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001245 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001246
Paul Bakkerbd51b262014-07-10 15:26:12 +02001247exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001249}
Paul Bakker33b43f12013-08-20 11:48:36 +02001250/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001253void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001254{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001256 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001258
Gilles Peskineb8e15342021-06-10 23:18:39 +02001259 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001260 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001261 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001262
Paul Bakkerbd51b262014-07-10 15:26:12 +02001263exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001265}
Paul Bakker33b43f12013-08-20 11:48:36 +02001266/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001269void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001270 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001271{
1272 mbedtls_mpi X;
1273 int res;
1274 mbedtls_test_mpi_random rand;
1275
1276 mbedtls_mpi_init( &X );
1277 rand.data = witnesses;
1278 rand.pos = 0;
1279 rand.chunk_len = chunk_len;
1280
1281 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001282 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1283 mbedtls_test_mpi_miller_rabin_determinizer,
1284 &rand );
1285 TEST_ASSERT( res == 0 );
1286
1287 rand.data = witnesses;
1288 rand.pos = 0;
1289 rand.chunk_len = chunk_len;
1290
Janos Follatha0b67c22018-09-18 14:48:23 +01001291 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1292 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001293 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001294 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001295
1296exit:
1297 mbedtls_mpi_free( &X );
1298}
1299/* END_CASE */
1300
1301/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001302void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001305 int my_ret;
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001308
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001309 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001310 TEST_ASSERT( my_ret == ref_ret );
1311
1312 if( ref_ret == 0 )
1313 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001314 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001315
1316 TEST_ASSERT( actual_bits >= (size_t) bits );
1317 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001318 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001319
Janos Follatha0b67c22018-09-18 14:48:23 +01001320 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1321 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001322 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001323 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001324 /* X = ( X - 1 ) / 2 */
1325 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001326 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1327 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001328 }
1329 }
1330
Paul Bakkerbd51b262014-07-10 15:26:12 +02001331exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001333}
1334/* END_CASE */
1335
Paul Bakker33b43f12013-08-20 11:48:36 +02001336/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001337void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1338 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001339{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 mbedtls_mpi X, A;
1341 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001342
Gilles Peskineb8e15342021-06-10 23:18:39 +02001343 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1344 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001346 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001348
Paul Bakkerbd51b262014-07-10 15:26:12 +02001349exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001351}
Paul Bakker33b43f12013-08-20 11:48:36 +02001352/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001353
Paul Bakker33b43f12013-08-20 11:48:36 +02001354/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001355void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1356 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 mbedtls_mpi X, A;
1359 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001360
Gilles Peskineb8e15342021-06-10 23:18:39 +02001361 TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
1362 TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001364 TEST_ASSERT( sign_is_valid( &X ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001366
Paul Bakkerbd51b262014-07-10 15:26:12 +02001367exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001369}
Paul Bakker33b43f12013-08-20 11:48:36 +02001370/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001371
Gilles Peskine2f780622020-11-25 15:37:20 +01001372/* BEGIN_CASE */
1373void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1374{
1375 mbedtls_mpi X;
1376 int ret;
1377 size_t bytes_left = rng_bytes;
1378 mbedtls_mpi_init( &X );
1379
1380 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1381 f_rng_bytes_left, &bytes_left );
1382 TEST_ASSERT( ret == expected_ret );
1383
1384 if( expected_ret == 0 )
1385 {
1386 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1387 * as a big-endian representation of the number. We know when
1388 * our RNG function returns null bytes, so we know how many
1389 * leading zero bytes the number has. */
1390 size_t leading_zeros = 0;
1391 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1392 leading_zeros = 1;
1393 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1394 (size_t) wanted_bytes );
1395 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
Gilles Peskine8854c5d2021-06-10 15:34:15 +02001396 TEST_ASSERT( sign_is_valid( &X ) );
Gilles Peskine2f780622020-11-25 15:37:20 +01001397 }
1398
1399exit:
1400 mbedtls_mpi_free( &X );
1401}
1402/* END_CASE */
1403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001405void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001406{
Andres AG93012e82016-09-09 09:10:28 +01001407 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001408}
Paul Bakker33b43f12013-08-20 11:48:36 +02001409/* END_CASE */