blob: b3c5cbab33cd5748682ec81eca9d4442e15033b5 [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
5typedef struct mbedtls_test_mpi_random
6{
7 data_t *data;
8 size_t pos;
9 size_t chunk_len;
10} mbedtls_test_mpi_random;
11
12/*
13 * This function is called by the Miller-Rabin primality test each time it
14 * chooses a random witness. The witnesses (or non-witnesses as provided by the
15 * test) are stored in the data member of the state structure. Each number is in
16 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
17 */
18int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
19 unsigned char* buf,
20 size_t len )
21{
22 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
23
24 if( random == NULL || random->data->x == NULL || buf == NULL )
25 return( -1 );
26
27 if( random->pos + random->chunk_len > random->data->len
28 || random->chunk_len > len )
29 {
30 return( -1 );
31 }
32
33 memset( buf, 0, len );
34
35 /* The witness is written to the end of the buffer, since the buffer is
36 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
37 * Writing the witness to the start of the buffer would result in the
38 * buffer being 'witness 000...000', which would be treated as
39 * witness * 2^n for some n. */
40 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
41 random->chunk_len );
42
43 random->pos += random->chunk_len;
44
45 return( 0 );
46}
Gilles Peskine3cb1e292020-11-25 15:37:20 +010047
48/* Random generator that is told how many bytes to return. */
49static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
50{
51 size_t *bytes_left = state;
52 size_t i;
53 for( i = 0; i < len; i++ )
54 {
55 if( *bytes_left == 0 )
56 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
57 buf[i] = *bytes_left & 0xff;
58 --( *bytes_left );
59 }
60 return( 0 );
61}
62
Paul Bakker33b43f12013-08-20 11:48:36 +020063/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000064
Paul Bakker33b43f12013-08-20 11:48:36 +020065/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020067 * END_DEPENDENCIES
68 */
Paul Bakker5690efc2011-05-26 13:16:06 +000069
Hanno Beckerb48e1aa2018-12-18 23:25:01 +000070/* BEGIN_CASE */
71void mpi_valid_param( )
72{
73 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
74}
75/* END_CASE */
76
Hanno Beckerafb607b2018-12-11 14:27:08 +000077/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
78void mpi_invalid_param( )
79{
80 mbedtls_mpi X;
81 const char *s_in = "00101000101010";
82 char s_out[16] = { 0 };
83 unsigned char u_out[16] = { 0 };
84 unsigned char u_in[16] = { 0 };
85 size_t olen;
86 mbedtls_mpi_uint mpi_uint;
87
88 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000089
Hanno Beckerafb607b2018-12-11 14:27:08 +000090 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
91 mbedtls_mpi_grow( NULL, 42 ) );
92 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
93 mbedtls_mpi_copy( NULL, &X ) );
94 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
95 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000096
Hanno Beckerafb607b2018-12-11 14:27:08 +000097 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
98 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000099
Hanno Beckerafb607b2018-12-11 14:27:08 +0000100 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
101 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
102 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
103 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000104
Hanno Beckerafb607b2018-12-11 14:27:08 +0000105 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
106 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
107 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
108 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000109
Hanno Beckerafb607b2018-12-11 14:27:08 +0000110 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
111 mbedtls_mpi_lset( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000112
Hanno Beckerafb607b2018-12-11 14:27:08 +0000113 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
114 mbedtls_mpi_get_bit( NULL, 42 ) );
115 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
116 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000117
Hanno Beckerafb607b2018-12-11 14:27:08 +0000118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
119 mbedtls_mpi_read_string( NULL, 2, s_in ) );
120 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
121 mbedtls_mpi_read_string( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000122
Hanno Beckerafb607b2018-12-11 14:27:08 +0000123 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
124 mbedtls_mpi_write_string( NULL, 2,
125 s_out, sizeof( s_out ),
126 &olen ) );
127 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
128 mbedtls_mpi_write_string( &X, 2,
129 NULL, sizeof( s_out ),
130 &olen ) );
131 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
132 mbedtls_mpi_write_string( &X, 2,
133 s_out, sizeof( s_out ),
134 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000135
Hanno Beckerafb607b2018-12-11 14:27:08 +0000136#if defined(MBEDTLS_FS_IO)
137 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
138 mbedtls_mpi_read_file( NULL, 2, stdin ) );
139 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
140 mbedtls_mpi_read_file( &X, 2, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000141
Hanno Beckerafb607b2018-12-11 14:27:08 +0000142 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
143 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
144#endif /* MBEDTLS_FS_IO */
145
146 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
147 mbedtls_mpi_read_binary( NULL, u_in,
148 sizeof( u_in ) ) );
149 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
150 mbedtls_mpi_read_binary( &X, NULL,
151 sizeof( u_in ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000152
Hanno Beckerafb607b2018-12-11 14:27:08 +0000153 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
154 mbedtls_mpi_write_binary( NULL, u_out,
155 sizeof( u_out ) ) );
156 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
157 mbedtls_mpi_write_binary( &X, NULL,
158 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000159
Hanno Beckerafb607b2018-12-11 14:27:08 +0000160 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
161 mbedtls_mpi_shift_l( NULL, 42 ) );
162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
163 mbedtls_mpi_shift_r( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000164
Hanno Beckerafb607b2018-12-11 14:27:08 +0000165 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
166 mbedtls_mpi_cmp_abs( NULL, &X ) );
167 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
168 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000169
Hanno Beckerafb607b2018-12-11 14:27:08 +0000170 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
171 mbedtls_mpi_cmp_mpi( NULL, &X ) );
172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
173 mbedtls_mpi_cmp_mpi( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000174
Hanno Beckerafb607b2018-12-11 14:27:08 +0000175 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
176 mbedtls_mpi_cmp_int( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000177
Hanno Beckerafb607b2018-12-11 14:27:08 +0000178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
179 mbedtls_mpi_add_abs( NULL, &X, &X ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
181 mbedtls_mpi_add_abs( &X, NULL, &X ) );
182 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
183 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000184
Hanno Beckerafb607b2018-12-11 14:27:08 +0000185 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
186 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
187 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
188 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
189 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
190 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000191
Hanno Beckerafb607b2018-12-11 14:27:08 +0000192 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
193 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
194 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
195 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
197 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000198
Hanno Beckerafb607b2018-12-11 14:27:08 +0000199 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
200 mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
202 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
203 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
204 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000205
Hanno Beckerafb607b2018-12-11 14:27:08 +0000206 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
207 mbedtls_mpi_add_int( NULL, &X, 42 ) );
208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
209 mbedtls_mpi_add_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000210
Hanno Beckerafb607b2018-12-11 14:27:08 +0000211 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
212 mbedtls_mpi_sub_int( NULL, &X, 42 ) );
213 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
214 mbedtls_mpi_sub_int( &X, 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_mul_mpi( NULL, &X, &X ) );
218 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
219 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
221 mbedtls_mpi_mul_mpi( &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_mul_int( NULL, &X, 42 ) );
225 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
226 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000227
Hanno Beckerafb607b2018-12-11 14:27:08 +0000228 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
229 mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) );
230 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
231 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000232
Hanno Beckerafb607b2018-12-11 14:27:08 +0000233 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
234 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000235
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000236 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
237
Hanno Beckerafb607b2018-12-11 14:27:08 +0000238 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
239 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
240 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
241 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
243 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000244
Hanno Beckerafb607b2018-12-11 14:27:08 +0000245 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
246 mbedtls_mpi_mod_int( NULL, &X, 42 ) );
247 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
248 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000249
Hanno Beckerafb607b2018-12-11 14:27:08 +0000250 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
251 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
252 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
253 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
255 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000258
Hanno Beckerafb607b2018-12-11 14:27:08 +0000259 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200260 mbedtls_mpi_fill_random( NULL, 42,
261 mbedtls_test_rnd_std_rand,
Hanno Beckerafb607b2018-12-11 14:27:08 +0000262 NULL ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
264 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000265
Hanno Beckerafb607b2018-12-11 14:27:08 +0000266 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
267 mbedtls_mpi_gcd( NULL, &X, &X ) );
268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
269 mbedtls_mpi_gcd( &X, NULL, &X ) );
270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
271 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000272
Hanno Beckerafb607b2018-12-11 14:27:08 +0000273 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
274 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
276 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
277 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000278 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000279
280exit:
281 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000282}
283/* END_CASE */
284
Paul Bakker33b43f12013-08-20 11:48:36 +0200285/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100286void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200287{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200288 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200289
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200290 mbedtls_mpi_init( &X );
291 mbedtls_mpi_init( &Y );
292 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200293
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200294 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
295 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200296 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200297 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200298
299exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200300 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200301}
302/* END_CASE */
303
304/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100305void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
306 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200307 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000308{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000310 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100311 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000314
Janos Follath04dadb72019-03-06 12:29:37 +0000315 memset( str, '!', sizeof( str ) );
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200318 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000319 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100320 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200321 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000322 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200323 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000324 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000325 }
326 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000327
Paul Bakkerbd51b262014-07-10 15:26:12 +0200328exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000330}
Paul Bakker33b43f12013-08-20 11:48:36 +0200331/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000332
Paul Bakker33b43f12013-08-20 11:48:36 +0200333/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100334void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000335{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000337 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100338 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000341
Paul Bakkere896fea2009-07-06 06:40:23 +0000342
Azim Khand30ca132017-06-09 04:32:58 +0100343 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000344 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200345 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000346
Paul Bakkerbd51b262014-07-10 15:26:12 +0200347exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000349}
Paul Bakker33b43f12013-08-20 11:48:36 +0200350/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000351
Paul Bakker33b43f12013-08-20 11:48:36 +0200352/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000353void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
354{
355 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000356 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000357 size_t len;
358
359 mbedtls_mpi_init( &X );
360
361
362 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000363 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000364 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
365
366exit:
367 mbedtls_mpi_free( &X );
368}
369/* END_CASE */
370
371/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100372void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100373 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100374 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000377 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000378 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000379
380 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200387 if( buflen > (size_t) output_size )
388 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200391 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000392 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000393
Ronald Cron2dbba992020-06-10 11:42:32 +0200394 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
395 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000396 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000397
Paul Bakkerbd51b262014-07-10 15:26:12 +0200398exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000400}
Paul Bakker33b43f12013-08-20 11:48:36 +0200401/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000402
Janos Follathe344d0f2019-02-19 16:17:40 +0000403/* BEGIN_CASE */
404void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
405 data_t * input_A, int output_size,
406 int result )
407{
408 mbedtls_mpi X;
409 unsigned char buf[1000];
410 size_t buflen;
411
412 memset( buf, 0x00, 1000 );
413
414 mbedtls_mpi_init( &X );
415
416 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
417
418 buflen = mbedtls_mpi_size( &X );
419 if( buflen > (size_t) output_size )
420 buflen = (size_t) output_size;
421
422 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
423 if( result == 0)
424 {
425
Ronald Cron2dbba992020-06-10 11:42:32 +0200426 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
427 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000428 }
429
430exit:
431 mbedtls_mpi_free( &X );
432}
433/* END_CASE */
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100436void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100437 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000440 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000441 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000442 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000443 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000444
445 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000448
Paul Bakker33b43f12013-08-20 11:48:36 +0200449 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200450 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000452 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000453 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000454
Paul Bakker33b43f12013-08-20 11:48:36 +0200455 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 buflen = mbedtls_mpi_size( &X );
458 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000459
Paul Bakkere896fea2009-07-06 06:40:23 +0000460
Ronald Cron2dbba992020-06-10 11:42:32 +0200461 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
462 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000463 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000464
Paul Bakkerbd51b262014-07-10 15:26:12 +0200465exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000467}
Paul Bakker33b43f12013-08-20 11:48:36 +0200468/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100471void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
472 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000473{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000475 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200476 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000481
Paul Bakker33b43f12013-08-20 11:48:36 +0200482 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000483 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200484 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000485 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200486 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000487
Paul Bakker33b43f12013-08-20 11:48:36 +0200488 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000489 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200490 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000491 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200492 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000495
Paul Bakkerbd51b262014-07-10 15:26:12 +0200496exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000498}
Paul Bakker33b43f12013-08-20 11:48:36 +0200499/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000500
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100502void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_mpi X;
505 mbedtls_mpi_init( &X );
506 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
507 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000508
Paul Bakkerbd51b262014-07-10 15:26:12 +0200509exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000511}
Paul Bakker33b43f12013-08-20 11:48:36 +0200512/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000513
Paul Bakker33b43f12013-08-20 11:48:36 +0200514/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100515void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
516 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_mpi X, Y;
519 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
522 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100523 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
524
525 if( result == 0 )
526 {
527 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
528 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000529
Paul Bakkerbd51b262014-07-10 15:26:12 +0200530exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000532}
Paul Bakker33b43f12013-08-20 11:48:36 +0200533/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000534
Paul Bakker33b43f12013-08-20 11:48:36 +0200535/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100536void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_mpi X;
539 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
542 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000543
Paul Bakkerbd51b262014-07-10 15:26:12 +0200544exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000546}
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000548
Paul Bakker33b43f12013-08-20 11:48:36 +0200549/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100550void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_mpi X;
553 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200556 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000557
Paul Bakkerbd51b262014-07-10 15:26:12 +0200558exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000560}
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000562
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100564void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
565 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_mpi A, X, Y, Z;
568 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
571 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
572 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
573 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
574 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000575
Paul Bakkerbd51b262014-07-10 15:26:12 +0200576exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000578}
Paul Bakker33b43f12013-08-20 11:48:36 +0200579/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000580
Paul Bakker33b43f12013-08-20 11:48:36 +0200581/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000583{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_mpi X;
585 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
588 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000589
Paul Bakkerbd51b262014-07-10 15:26:12 +0200590exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000592}
Paul Bakker33b43f12013-08-20 11:48:36 +0200593/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000594
Paul Bakker33b43f12013-08-20 11:48:36 +0200595/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100596void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
597 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_mpi X, Y;
600 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
603 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
604 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000605
Paul Bakkerbd51b262014-07-10 15:26:12 +0200606exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000608}
Paul Bakker33b43f12013-08-20 11:48:36 +0200609/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000610
Paul Bakker33b43f12013-08-20 11:48:36 +0200611/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100612void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
613 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100614 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100615{
Janos Follath0e5532d2019-10-11 14:21:53 +0100616 unsigned ret;
617 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100618 mbedtls_mpi X, Y;
619 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
620
Janos Follathb7e1b492019-10-14 09:21:49 +0100621 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
622 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100623
Gilles Peskine9018b112020-01-21 16:30:53 +0100624 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
625 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100626
Janos Follath0e5532d2019-10-11 14:21:53 +0100627 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100628 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100629 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100630
631exit:
632 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
633}
634/* END_CASE */
635
636/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100637void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
638 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_mpi X, Y;
641 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
644 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
645 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000646
Paul Bakkerbd51b262014-07-10 15:26:12 +0200647exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000649}
Paul Bakker33b43f12013-08-20 11:48:36 +0200650/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000651
Paul Bakker33b43f12013-08-20 11:48:36 +0200652/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100653void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000654{
Gilles Peskine7428b452020-01-20 21:01:51 +0100655 mbedtls_mpi X, Y;
656 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100659 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100662 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
663 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000664
Paul Bakkerbd51b262014-07-10 15:26:12 +0200665exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100666 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
667}
668/* END_CASE */
669
670/* BEGIN_CASE */
671void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
672{
673 mbedtls_mpi X, Y, X0;
674 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
675
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100676 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
677 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
678 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100679 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
680
681 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
682 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
683 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
684
685exit:
686 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
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 */
691void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000692{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_mpi X;
694 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
697 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
698 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000699
Paul Bakkerbd51b262014-07-10 15:26:12 +0200700exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000702}
Paul Bakker33b43f12013-08-20 11:48:36 +0200703/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000704
Paul Bakker33b43f12013-08-20 11:48:36 +0200705/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100707{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_mpi X;
709 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100712 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
714 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100715 TEST_ASSERT( X.n == (size_t) after );
716
Paul Bakkerbd51b262014-07-10 15:26:12 +0200717exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100719}
720/* END_CASE */
721
722/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100723void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
724 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100725{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_mpi X, Y, XX;
727 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100730 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100732 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
736 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
739 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100740
Paul Bakkerbd51b262014-07-10 15:26:12 +0200741exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100743}
744/* END_CASE */
745
746/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100747void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
748 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
753 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100756 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100758 Y.s = y_sign;
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
761 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
764 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
765 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
768 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
769 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100770
Paul Bakkerbd51b262014-07-10 15:26:12 +0200771exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
773 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100774}
775/* END_CASE */
776
777/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100778void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000779{
Gilles Peskine7428b452020-01-20 21:01:51 +0100780 mbedtls_mpi X, Y;
781 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
784 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100785 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
786 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine7428b452020-01-20 21:01:51 +0100789 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
790 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000791
Paul Bakkerbd51b262014-07-10 15:26:12 +0200792exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100793 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
794}
795/* END_CASE */
796
797/* BEGIN_CASE */
798void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
799{
800 mbedtls_mpi X, Y, X0, Y0;
801 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
802 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
803
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100804 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
805 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
806 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
807 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100808
809 mbedtls_mpi_swap( &X, &Y );
810 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
811 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
812
813exit:
814 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
815 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
816}
817/* END_CASE */
818
819/* BEGIN_CASE */
820void mpi_swap_self( data_t *input_X )
821{
822 mbedtls_mpi X, X0;
823 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
824
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100825 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
826 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100827
828 mbedtls_mpi_swap( &X, &X );
829 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
830
831exit:
832 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000833}
Paul Bakker33b43f12013-08-20 11:48:36 +0200834/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000835
Paul Bakker33b43f12013-08-20 11:48:36 +0200836/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100837void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
838 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000839{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_mpi X, Y, Z, A;
841 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
844 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
845 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
846 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
847 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000848
Paul Bakkerbd51b262014-07-10 15:26:12 +0200849exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000851}
Paul Bakker33b43f12013-08-20 11:48:36 +0200852/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000853
Paul Bakker33b43f12013-08-20 11:48:36 +0200854/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100855void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
856 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100857{
858 mbedtls_mpi X, A;
859 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
860
Janos Follath044a86b2015-10-25 10:58:03 +0100861 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100862
863 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
864 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
865 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
866
867 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
868 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
869 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
870
871 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100872 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
873 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
874
875exit:
876 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
877}
878/* END_CASE */
879
880
881/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100882void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
883 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000884{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_mpi X, Y, Z, A;
886 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
889 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
890 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
891 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
892 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000893
Paul Bakkerbd51b262014-07-10 15:26:12 +0200894exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000896}
Paul Bakker33b43f12013-08-20 11:48:36 +0200897/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000898
Paul Bakker33b43f12013-08-20 11:48:36 +0200899/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100900void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
901 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000902{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_mpi X, Y, A;
904 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
907 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
908 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
909 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
910 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000911
Paul Bakkerbd51b262014-07-10 15:26:12 +0200912exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000914}
Paul Bakker33b43f12013-08-20 11:48:36 +0200915/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000916
Paul Bakker33b43f12013-08-20 11:48:36 +0200917/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100918void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
919 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_mpi X, Y, A;
922 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
925 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
926 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
927 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
928 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000929
Paul Bakkerbd51b262014-07-10 15:26:12 +0200930exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000932}
Paul Bakker33b43f12013-08-20 11:48:36 +0200933/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000934
Paul Bakker33b43f12013-08-20 11:48:36 +0200935/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100936void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
937 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_mpi X, Z, A;
940 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
943 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
944 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
945 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000946
Paul Bakkerbd51b262014-07-10 15:26:12 +0200947exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000949}
Paul Bakker33b43f12013-08-20 11:48:36 +0200950/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000951
Paul Bakker33b43f12013-08-20 11:48:36 +0200952/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100953void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
954 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_mpi X, Y, Z, A;
957 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
960 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
961 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
962 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
963 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000964
Paul Bakkerbd51b262014-07-10 15:26:12 +0200965exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000967}
Paul Bakker33b43f12013-08-20 11:48:36 +0200968/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000969
Paul Bakker33b43f12013-08-20 11:48:36 +0200970/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100971void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
972 char * input_Y, int radix_A, char * input_A,
973 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000974{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000976 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 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 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200984 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000985 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000987
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1001 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1002 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
1003 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001004
Paul Bakkerbd51b262014-07-10 15:26:12 +02001005exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001007}
Paul Bakker33b43f12013-08-20 11:48:36 +02001008/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001009
Paul Bakker33b43f12013-08-20 11:48:36 +02001010/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001011void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
1012 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_mpi X, Y, Z, A;
1015 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1018 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1019 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1020 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
1021 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001022
Paul Bakkerbd51b262014-07-10 15:26:12 +02001023exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001025}
Paul Bakker33b43f12013-08-20 11:48:36 +02001026/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001027
Paul Bakker33b43f12013-08-20 11:48:36 +02001028/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001029void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1030 int radix_A, char * input_A,
1031 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001032{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 mbedtls_mpi X, Z, A;
1034 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1037 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1038 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001039 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001041 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001043 else
1044 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001045
Paul Bakkerbd51b262014-07-10 15:26:12 +02001046exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001048}
Paul Bakker33b43f12013-08-20 11:48:36 +02001049/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001050
Paul Bakker33b43f12013-08-20 11:48:36 +02001051/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001052void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1053 char * input_Y, int radix_A, char * input_A,
1054 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001057 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1059 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1062 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1063 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1064 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1065 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001066 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001067 if( res == 0 )
1068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1070 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001071 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001072
Paul Bakkerbd51b262014-07-10 15:26:12 +02001073exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1075 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001076}
Paul Bakker33b43f12013-08-20 11:48:36 +02001077/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001078
Paul Bakker33b43f12013-08-20 11:48:36 +02001079/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001080void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1081 int radix_A, char * input_A, int radix_B,
1082 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001085 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1087 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1090 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1091 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1092 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001093 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001094 if( res == 0 )
1095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1097 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001098 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001099
Paul Bakkerbd51b262014-07-10 15:26:12 +02001100exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1102 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001103}
Paul Bakker33b43f12013-08-20 11:48:36 +02001104/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001105
Paul Bakker33b43f12013-08-20 11:48:36 +02001106/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001107void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1108 char * input_Y, int radix_A, char * input_A,
1109 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001110{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001112 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1116 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1117 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1118 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001119 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001120 if( res == 0 )
1121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001123 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001124
Paul Bakkerbd51b262014-07-10 15:26:12 +02001125exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001127}
Paul Bakker33b43f12013-08-20 11:48:36 +02001128/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001129
Paul Bakker33b43f12013-08-20 11:48:36 +02001130/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001131void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1132 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001135 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 mbedtls_mpi_uint r;
1137 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1140 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001141 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001142 if( res == 0 )
1143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001145 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001146
Paul Bakkerbd51b262014-07-10 15:26:12 +02001147exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001149}
Paul Bakker33b43f12013-08-20 11:48:36 +02001150/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001151
Paul Bakker33b43f12013-08-20 11:48:36 +02001152/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001153void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1154 char * input_E, int radix_N, char * input_N,
1155 int radix_RR, char * input_RR, int radix_X,
1156 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001159 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1161 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1164 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1165 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1166 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001167
Paul Bakker33b43f12013-08-20 11:48:36 +02001168 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001172 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001173 if( res == 0 )
1174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001176 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001177
Paul Bakkerbd51b262014-07-10 15:26:12 +02001178exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1180 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001181}
Paul Bakker33b43f12013-08-20 11:48:36 +02001182/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001183
Paul Bakker33b43f12013-08-20 11:48:36 +02001184/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001185void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1186 char * input_Y, int radix_A, char * input_A,
1187 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001190 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1194 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1195 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1196 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001197 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001198 if( res == 0 )
1199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001201 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001202
Paul Bakkerbd51b262014-07-10 15:26:12 +02001203exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001205}
Paul Bakker33b43f12013-08-20 11:48:36 +02001206/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001209void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001212 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001216 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001217 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001218
Paul Bakkerbd51b262014-07-10 15:26:12 +02001219exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001225void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001226 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001227{
1228 mbedtls_mpi X;
1229 int res;
1230 mbedtls_test_mpi_random rand;
1231
1232 mbedtls_mpi_init( &X );
1233 rand.data = witnesses;
1234 rand.pos = 0;
1235 rand.chunk_len = chunk_len;
1236
1237 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001238 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1239 mbedtls_test_mpi_miller_rabin_determinizer,
1240 &rand );
1241 TEST_ASSERT( res == 0 );
1242
1243 rand.data = witnesses;
1244 rand.pos = 0;
1245 rand.chunk_len = chunk_len;
1246
Janos Follatha0b67c22018-09-18 14:48:23 +01001247 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1248 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001249 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001250 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001251
1252exit:
1253 mbedtls_mpi_free( &X );
1254}
1255/* END_CASE */
1256
1257/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001258void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001259{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001261 int my_ret;
1262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001264
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001265 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1266 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001267 TEST_ASSERT( my_ret == ref_ret );
1268
1269 if( ref_ret == 0 )
1270 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001271 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001272
1273 TEST_ASSERT( actual_bits >= (size_t) bits );
1274 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1275
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001276 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1277 mbedtls_test_rnd_std_rand,
1278 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001279 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001280 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001281 /* X = ( X - 1 ) / 2 */
1282 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001283 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1284 mbedtls_test_rnd_std_rand,
1285 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001286 }
1287 }
1288
Paul Bakkerbd51b262014-07-10 15:26:12 +02001289exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001291}
1292/* END_CASE */
1293
Paul Bakker33b43f12013-08-20 11:48:36 +02001294/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001295void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1296 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_mpi X, A;
1299 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1302 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1303 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1304 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001305
Paul Bakkerbd51b262014-07-10 15:26:12 +02001306exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001308}
Paul Bakker33b43f12013-08-20 11:48:36 +02001309/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001310
Paul Bakker33b43f12013-08-20 11:48:36 +02001311/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001312void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1313 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_mpi X, A;
1316 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1319 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1320 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1321 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001322
Paul Bakkerbd51b262014-07-10 15:26:12 +02001323exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001325}
Paul Bakker33b43f12013-08-20 11:48:36 +02001326/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001327
Gilles Peskine3cb1e292020-11-25 15:37:20 +01001328/* BEGIN_CASE */
1329void mpi_fill_random( int wanted_bytes, int rng_bytes, int expected_ret )
1330{
1331 mbedtls_mpi X;
1332 int ret;
1333 size_t bytes_left = rng_bytes;
1334 mbedtls_mpi_init( &X );
1335
1336 ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
1337 f_rng_bytes_left, &bytes_left );
1338 TEST_ASSERT( ret == expected_ret );
1339
1340 if( expected_ret == 0 )
1341 {
1342 /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
1343 * as a big-endian representation of the number. We know when
1344 * our RNG function returns null bytes, so we know how many
1345 * leading zero bytes the number has. */
1346 size_t leading_zeros = 0;
1347 if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
1348 leading_zeros = 1;
1349 TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
1350 (size_t) wanted_bytes );
1351 TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
1352 }
1353
1354exit:
1355 mbedtls_mpi_free( &X );
1356}
1357/* END_CASE */
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001360void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001361{
Andres AG93012e82016-09-09 09:10:28 +01001362 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001363}
Paul Bakker33b43f12013-08-20 11:48:36 +02001364/* END_CASE */