blob: 5d8cfc4c2a5596ac68e9bf5d1b6a40d4d3796ca9 [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"
Janos Follath64eca052018-09-05 17:04:49 +01003
Chris Jonesce6fa8f2020-12-03 14:24:33 +00004#if MBEDTLS_MPI_MAX_BITS > 256
5#define MPI_MAX_BITS_LARGER_THAN_256
6#endif
7
Janos Follath64eca052018-09-05 17:04:49 +01008typedef struct mbedtls_test_mpi_random
9{
10 data_t *data;
11 size_t pos;
12 size_t chunk_len;
13} mbedtls_test_mpi_random;
14
15/*
16 * This function is called by the Miller-Rabin primality test each time it
17 * chooses a random witness. The witnesses (or non-witnesses as provided by the
18 * test) are stored in the data member of the state structure. Each number is in
19 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
20 */
21int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
22 unsigned char* buf,
23 size_t len )
24{
25 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
26
27 if( random == NULL || random->data->x == NULL || buf == NULL )
28 return( -1 );
29
30 if( random->pos + random->chunk_len > random->data->len
31 || random->chunk_len > len )
32 {
33 return( -1 );
34 }
35
36 memset( buf, 0, len );
37
38 /* The witness is written to the end of the buffer, since the buffer is
39 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
40 * Writing the witness to the start of the buffer would result in the
41 * buffer being 'witness 000...000', which would be treated as
42 * witness * 2^n for some n. */
43 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
44 random->chunk_len );
45
46 random->pos += random->chunk_len;
47
48 return( 0 );
49}
Paul Bakker33b43f12013-08-20 11:48:36 +020050/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000051
Paul Bakker33b43f12013-08-20 11:48:36 +020052/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020054 * END_DEPENDENCIES
55 */
Paul Bakker5690efc2011-05-26 13:16:06 +000056
Hanno Beckerb48e1aa2018-12-18 23:25:01 +000057/* BEGIN_CASE */
58void mpi_valid_param( )
59{
60 TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
61}
62/* END_CASE */
63
Hanno Beckerafb607b2018-12-11 14:27:08 +000064/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
65void mpi_invalid_param( )
66{
67 mbedtls_mpi X;
68 const char *s_in = "00101000101010";
69 char s_out[16] = { 0 };
70 unsigned char u_out[16] = { 0 };
71 unsigned char u_in[16] = { 0 };
72 size_t olen;
73 mbedtls_mpi_uint mpi_uint;
74
75 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000076
Hanno Beckerafb607b2018-12-11 14:27:08 +000077 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
78 mbedtls_mpi_grow( NULL, 42 ) );
79 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
80 mbedtls_mpi_copy( NULL, &X ) );
81 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
82 mbedtls_mpi_copy( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000083
Hanno Beckerafb607b2018-12-11 14:27:08 +000084 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
85 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +000086
Hanno Beckerafb607b2018-12-11 14:27:08 +000087 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
88 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
89 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
90 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +000091
Hanno Beckerafb607b2018-12-11 14:27:08 +000092 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
93 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
94 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
95 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
Hanno Beckere1185042018-12-13 14:31:46 +000096
Hanno Beckerafb607b2018-12-11 14:27:08 +000097 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
98 mbedtls_mpi_lset( NULL, 42 ) );
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_get_bit( NULL, 42 ) );
102 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
103 mbedtls_mpi_set_bit( NULL, 42, 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_read_string( NULL, 2, s_in ) );
107 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
108 mbedtls_mpi_read_string( &X, 2, NULL ) );
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_write_string( NULL, 2,
112 s_out, sizeof( s_out ),
113 &olen ) );
114 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
115 mbedtls_mpi_write_string( &X, 2,
116 NULL, sizeof( s_out ),
117 &olen ) );
118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
119 mbedtls_mpi_write_string( &X, 2,
120 s_out, sizeof( s_out ),
121 NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000122
Hanno Beckerafb607b2018-12-11 14:27:08 +0000123#if defined(MBEDTLS_FS_IO)
124 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
125 mbedtls_mpi_read_file( NULL, 2, stdin ) );
126 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
127 mbedtls_mpi_read_file( &X, 2, NULL ) );
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_write_file( "", NULL, 2, NULL ) );
131#endif /* MBEDTLS_FS_IO */
132
133 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
134 mbedtls_mpi_read_binary( NULL, u_in,
135 sizeof( u_in ) ) );
136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
137 mbedtls_mpi_read_binary( &X, NULL,
138 sizeof( u_in ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000139
Hanno Beckerafb607b2018-12-11 14:27:08 +0000140 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
141 mbedtls_mpi_write_binary( NULL, u_out,
142 sizeof( u_out ) ) );
143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
144 mbedtls_mpi_write_binary( &X, NULL,
145 sizeof( u_out ) ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000146
Hanno Beckerafb607b2018-12-11 14:27:08 +0000147 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
148 mbedtls_mpi_shift_l( NULL, 42 ) );
149 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
150 mbedtls_mpi_shift_r( NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000151
Hanno Beckerafb607b2018-12-11 14:27:08 +0000152 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
153 mbedtls_mpi_cmp_abs( NULL, &X ) );
154 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
155 mbedtls_mpi_cmp_abs( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000156
Hanno Beckerafb607b2018-12-11 14:27:08 +0000157 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
158 mbedtls_mpi_cmp_mpi( NULL, &X ) );
159 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
160 mbedtls_mpi_cmp_mpi( &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000161
Hanno Beckerafb607b2018-12-11 14:27:08 +0000162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
163 mbedtls_mpi_cmp_int( 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_add_abs( NULL, &X, &X ) );
167 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
168 mbedtls_mpi_add_abs( &X, NULL, &X ) );
169 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
170 mbedtls_mpi_add_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000171
Hanno Beckerafb607b2018-12-11 14:27:08 +0000172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
173 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
174 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
175 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
177 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000178
Hanno Beckerafb607b2018-12-11 14:27:08 +0000179 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
180 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
181 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
182 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
183 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
184 mbedtls_mpi_add_mpi( &X, &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_sub_mpi( NULL, &X, &X ) );
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
189 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
190 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
191 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000192
Hanno Beckerafb607b2018-12-11 14:27:08 +0000193 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
194 mbedtls_mpi_add_int( NULL, &X, 42 ) );
195 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
196 mbedtls_mpi_add_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000197
Hanno Beckerafb607b2018-12-11 14:27:08 +0000198 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
199 mbedtls_mpi_sub_int( NULL, &X, 42 ) );
200 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
201 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000202
Hanno Beckerafb607b2018-12-11 14:27:08 +0000203 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
204 mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
206 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
207 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
208 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000209
Hanno Beckerafb607b2018-12-11 14:27:08 +0000210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
211 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
212 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
213 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
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_div_mpi( &X, &X, NULL, &X ) );
217 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
218 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000219
Hanno Beckerafb607b2018-12-11 14:27:08 +0000220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
221 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000222
Hanno Beckerf25ee7f2018-12-19 16:51:02 +0000223 TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
224
Hanno Beckerafb607b2018-12-11 14:27:08 +0000225 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
226 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
228 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
229 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
230 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
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_mod_int( NULL, &X, 42 ) );
234 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
235 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000236
Hanno Beckerafb607b2018-12-11 14:27:08 +0000237 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
238 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
239 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
240 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
241 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
242 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
243 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
244 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000245
Hanno Beckerafb607b2018-12-11 14:27:08 +0000246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
247 mbedtls_mpi_fill_random( NULL, 42, rnd_std_rand,
248 NULL ) );
249 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
250 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000251
Hanno Beckerafb607b2018-12-11 14:27:08 +0000252 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
253 mbedtls_mpi_gcd( NULL, &X, &X ) );
254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
255 mbedtls_mpi_gcd( &X, NULL, &X ) );
256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
257 mbedtls_mpi_gcd( &X, &X, 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,
260 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
261 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
262 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000264 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000265
266exit:
267 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000268}
269/* END_CASE */
270
Paul Bakker33b43f12013-08-20 11:48:36 +0200271/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100272void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200273{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200274 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200275
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200276 mbedtls_mpi_init( &X );
277 mbedtls_mpi_init( &Y );
278 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200279
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200280 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
281 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200282 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200283 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200284
285exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200286 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200287}
288/* END_CASE */
289
290/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100291void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
292 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200293 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000296 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100297 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000300
Janos Follath276284f2019-03-06 12:29:37 +0000301 memset( str, '!', sizeof( str ) );
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200304 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000305 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100306 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200307 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000308 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200309 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath276284f2019-03-06 12:29:37 +0000310 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000311 }
312 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000313
Paul Bakkerbd51b262014-07-10 15:26:12 +0200314exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000316}
Paul Bakker33b43f12013-08-20 11:48:36 +0200317/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000318
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100320void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000321{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000323 unsigned char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100324 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000327
Paul Bakkere896fea2009-07-06 06:40:23 +0000328
Azim Khand30ca132017-06-09 04:32:58 +0100329 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100330 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200331 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000332
Paul Bakkerbd51b262014-07-10 15:26:12 +0200333exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000335}
Paul Bakker33b43f12013-08-20 11:48:36 +0200336/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000337
Paul Bakker33b43f12013-08-20 11:48:36 +0200338/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100339void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100340 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100341 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000342{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000344 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000345 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000346
347 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200354 if( buflen > (size_t) output_size )
355 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200358 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000359 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000360
Azim Khand30ca132017-06-09 04:32:58 +0100361 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000362 }
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100370void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100371 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000374 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000375 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000376 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000377 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000378
379 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000382
Paul Bakker33b43f12013-08-20 11:48:36 +0200383 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200384 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000386 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000387 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000388
Paul Bakker33b43f12013-08-20 11:48:36 +0200389 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 buflen = mbedtls_mpi_size( &X );
392 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000393
Paul Bakkere896fea2009-07-06 06:40:23 +0000394
Azim Khand30ca132017-06-09 04:32:58 +0100395 TEST_ASSERT( hexcmp( buf, input_A->x, 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100404void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
405 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000406{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000408 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200409 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000414
Paul Bakker33b43f12013-08-20 11:48:36 +0200415 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000416 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200417 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000418 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200419 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000420
Paul Bakker33b43f12013-08-20 11:48:36 +0200421 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000422 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200423 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000424 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200425 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000428
Paul Bakkerbd51b262014-07-10 15:26:12 +0200429exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000431}
Paul Bakker33b43f12013-08-20 11:48:36 +0200432/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000433
Paul Bakker33b43f12013-08-20 11:48:36 +0200434/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100435void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000436{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_mpi X;
438 mbedtls_mpi_init( &X );
439 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
440 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000441
Paul Bakkerbd51b262014-07-10 15:26:12 +0200442exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000444}
Paul Bakker33b43f12013-08-20 11:48:36 +0200445/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000446
Paul Bakker33b43f12013-08-20 11:48:36 +0200447/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100448void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
449 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000450{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_mpi X, Y;
452 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
455 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100456 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
457
458 if( result == 0 )
459 {
460 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
461 }
Paul Bakker2f5947e2011-05-18 15:47:11 +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 Bakker2f5947e2011-05-18 15:47:11 +0000465}
Paul Bakker33b43f12013-08-20 11:48:36 +0200466/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000467
Paul Bakker33b43f12013-08-20 11:48:36 +0200468/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100469void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_mpi X;
472 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
475 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000476
Paul Bakkerbd51b262014-07-10 15:26:12 +0200477exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000479}
Paul Bakker33b43f12013-08-20 11:48:36 +0200480/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000481
Paul Bakker33b43f12013-08-20 11:48:36 +0200482/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100483void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000484{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_mpi X;
486 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200489 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000490
Paul Bakkerbd51b262014-07-10 15:26:12 +0200491exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000493}
Paul Bakker33b43f12013-08-20 11:48:36 +0200494/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000495
Paul Bakker33b43f12013-08-20 11:48:36 +0200496/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100497void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
498 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000499{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_mpi A, X, Y, Z;
501 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
504 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
505 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
506 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
507 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000508
Paul Bakkerbd51b262014-07-10 15:26:12 +0200509exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000511}
Paul Bakker33b43f12013-08-20 11:48:36 +0200512/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000513
Paul Bakker33b43f12013-08-20 11:48:36 +0200514/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_mpi X;
518 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
521 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000522
Paul Bakkerbd51b262014-07-10 15:26:12 +0200523exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000525}
Paul Bakker33b43f12013-08-20 11:48:36 +0200526/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000527
Paul Bakker33b43f12013-08-20 11:48:36 +0200528/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100529void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
530 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_mpi X, Y;
533 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
536 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
537 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000538
Paul Bakkerbd51b262014-07-10 15:26:12 +0200539exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000541}
Paul Bakker33b43f12013-08-20 11:48:36 +0200542/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000543
Paul Bakker33b43f12013-08-20 11:48:36 +0200544/* BEGIN_CASE */
Janos Follath27d221a2019-10-14 09:21:49 +0100545void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
546 int size_Y, char * input_Y,
Janos Follath867a3ab2019-10-11 14:21:53 +0100547 int input_ret, int input_err )
Janos Follathe9ae6302019-09-11 16:07:14 +0100548{
Janos Follath867a3ab2019-10-11 14:21:53 +0100549 unsigned ret;
550 unsigned input_uret = input_ret;
Janos Follathe9ae6302019-09-11 16:07:14 +0100551 mbedtls_mpi X, Y;
552 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
553
Janos Follath27d221a2019-10-14 09:21:49 +0100554 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
555 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100556
Gilles Peskine1a30fbb2020-01-21 16:30:53 +0100557 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
558 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follathe9ae6302019-09-11 16:07:14 +0100559
Janos Follath867a3ab2019-10-11 14:21:53 +0100560 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follathe9ae6302019-09-11 16:07:14 +0100561 if( input_err == 0 )
Janos Follath867a3ab2019-10-11 14:21:53 +0100562 TEST_ASSERT( ret == input_uret );
Janos Follathe9ae6302019-09-11 16:07:14 +0100563
564exit:
565 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
566}
567/* END_CASE */
568
569/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100570void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
571 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_mpi X, Y;
574 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
577 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
578 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000579
Paul Bakkerbd51b262014-07-10 15:26:12 +0200580exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000582}
Paul Bakker33b43f12013-08-20 11:48:36 +0200583/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000584
Paul Bakker33b43f12013-08-20 11:48:36 +0200585/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100586void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000587{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100588 mbedtls_mpi X, Y;
589 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100592 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100595 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
596 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000597
Paul Bakkerbd51b262014-07-10 15:26:12 +0200598exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100599 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
600}
601/* END_CASE */
602
603/* BEGIN_CASE */
604void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
605{
606 mbedtls_mpi X, Y, X0;
607 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
608
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100609 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
610 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
611 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100612 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
613
614 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
615 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
616 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
617
618exit:
619 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000620}
Paul Bakker33b43f12013-08-20 11:48:36 +0200621/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000622
Paul Bakker33b43f12013-08-20 11:48:36 +0200623/* BEGIN_CASE */
624void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_mpi X;
627 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
630 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
631 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000632
Paul Bakkerbd51b262014-07-10 15:26:12 +0200633exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000635}
Paul Bakker33b43f12013-08-20 11:48:36 +0200636/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000637
Paul Bakker33b43f12013-08-20 11:48:36 +0200638/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_mpi X;
642 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100645 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
647 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100648 TEST_ASSERT( X.n == (size_t) after );
649
Paul Bakkerbd51b262014-07-10 15:26:12 +0200650exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100652}
653/* END_CASE */
654
655/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100656void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
657 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100658{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_mpi X, Y, XX;
660 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100663 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100665 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
669 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
672 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100673
Paul Bakkerbd51b262014-07-10 15:26:12 +0200674exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100676}
677/* END_CASE */
678
679/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100680void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
681 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100682{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
686 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100689 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100691 Y.s = y_sign;
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
694 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
697 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
698 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
701 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
702 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100703
Paul Bakkerbd51b262014-07-10 15:26:12 +0200704exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
706 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100707}
708/* END_CASE */
709
710/* BEGIN_CASE */
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100711void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000712{
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100713 mbedtls_mpi X, Y;
714 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
717 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100718 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
719 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100722 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
723 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000724
Paul Bakkerbd51b262014-07-10 15:26:12 +0200725exit:
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100726 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
727}
728/* END_CASE */
729
730/* BEGIN_CASE */
731void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
732{
733 mbedtls_mpi X, Y, X0, Y0;
734 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
735 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
736
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100737 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
738 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
739 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
740 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100741
742 mbedtls_mpi_swap( &X, &Y );
743 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
744 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
745
746exit:
747 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
748 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
749}
750/* END_CASE */
751
752/* BEGIN_CASE */
753void mpi_swap_self( data_t *input_X )
754{
755 mbedtls_mpi X, X0;
756 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
757
Gilles Peskinee0ced3a2020-02-03 16:15:47 +0100758 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
759 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine8fe3b792020-01-20 21:01:51 +0100760
761 mbedtls_mpi_swap( &X, &X );
762 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
763
764exit:
765 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000766}
Paul Bakker33b43f12013-08-20 11:48:36 +0200767/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000768
Paul Bakker33b43f12013-08-20 11:48:36 +0200769/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100770void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
771 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000772{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_mpi X, Y, Z, A;
774 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
777 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
778 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
779 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
780 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000781
Paul Bakkerbd51b262014-07-10 15:26:12 +0200782exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000784}
Paul Bakker33b43f12013-08-20 11:48:36 +0200785/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000786
Paul Bakker33b43f12013-08-20 11:48:36 +0200787/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100788void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
789 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100790{
791 mbedtls_mpi X, A;
792 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
793
Janos Follath044a86b2015-10-25 10:58:03 +0100794 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100795
796 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
797 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
798 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
799
800 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
801 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
802 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
803
804 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100805 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
806 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
807
808exit:
809 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
810}
811/* END_CASE */
812
813
814/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100815void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
816 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000817{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_mpi X, Y, Z, A;
819 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
822 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
823 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
824 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
825 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000826
Paul Bakkerbd51b262014-07-10 15:26:12 +0200827exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000829}
Paul Bakker33b43f12013-08-20 11:48:36 +0200830/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000831
Paul Bakker33b43f12013-08-20 11:48:36 +0200832/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100833void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
834 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000835{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_mpi X, Y, A;
837 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
840 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
841 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
842 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
843 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000844
Paul Bakkerbd51b262014-07-10 15:26:12 +0200845exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000847}
Paul Bakker33b43f12013-08-20 11:48:36 +0200848/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000849
Paul Bakker33b43f12013-08-20 11:48:36 +0200850/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100851void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
852 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000853{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_mpi X, Y, A;
855 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
858 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
859 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
860 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
861 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000862
Paul Bakkerbd51b262014-07-10 15:26:12 +0200863exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000865}
Paul Bakker33b43f12013-08-20 11:48:36 +0200866/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000867
Paul Bakker33b43f12013-08-20 11:48:36 +0200868/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100869void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
870 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000871{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_mpi X, Z, A;
873 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
876 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
877 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
878 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000879
Paul Bakkerbd51b262014-07-10 15:26:12 +0200880exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000882}
Paul Bakker33b43f12013-08-20 11:48:36 +0200883/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000884
Paul Bakker33b43f12013-08-20 11:48:36 +0200885/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100886void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
887 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_mpi X, Y, Z, A;
890 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
893 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
894 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
895 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
896 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000897
Paul Bakkerbd51b262014-07-10 15:26:12 +0200898exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000900}
Paul Bakker33b43f12013-08-20 11:48:36 +0200901/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000902
Paul Bakker33b43f12013-08-20 11:48:36 +0200903/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100904void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
905 char * input_Y, int radix_A, char * input_A,
906 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000909 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
913 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
914 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200917 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000918 if( res == 0 )
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( &Y ); 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_int( int radix_X, char * input_X, int input_Y,
928 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, Z, A;
931 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
934 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
935 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
936 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000937
Paul Bakkerbd51b262014-07-10 15:26:12 +0200938exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000940}
Paul Bakker33b43f12013-08-20 11:48:36 +0200941/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000942
Paul Bakker33b43f12013-08-20 11:48:36 +0200943/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100944void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
945 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000946{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_mpi X, Y, Z, A;
948 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
951 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
952 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
953 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
954 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000955
Paul Bakkerbd51b262014-07-10 15:26:12 +0200956exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000958}
Paul Bakker33b43f12013-08-20 11:48:36 +0200959/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000960
Paul Bakker33b43f12013-08-20 11:48:36 +0200961/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100962void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
963 int radix_A, char * input_A,
964 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_mpi X, Z, A;
967 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
970 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
971 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200972 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200974 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200976 else
977 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000978
Paul Bakkerbd51b262014-07-10 15:26:12 +0200979exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000981}
Paul Bakker33b43f12013-08-20 11:48:36 +0200982/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000983
Paul Bakker33b43f12013-08-20 11:48:36 +0200984/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100985void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
986 char * input_Y, int radix_A, char * input_A,
987 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000988{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000990 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
992 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
995 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
996 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
997 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
998 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200999 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001000 if( res == 0 )
1001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1003 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001004 }
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( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1008 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001009}
Paul Bakker33b43f12013-08-20 11:48:36 +02001010/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001011
Paul Bakker33b43f12013-08-20 11:48:36 +02001012/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001013void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1014 int radix_A, char * input_A, int radix_B,
1015 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001018 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1020 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1023 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1024 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1025 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001026 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001027 if( res == 0 )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1030 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001031 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001032
Paul Bakkerbd51b262014-07-10 15:26:12 +02001033exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1035 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001036}
Paul Bakker33b43f12013-08-20 11:48:36 +02001037/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001038
Paul Bakker33b43f12013-08-20 11:48:36 +02001039/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001040void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1041 char * input_Y, int radix_A, char * input_A,
1042 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001045 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1049 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1050 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1051 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001052 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001053 if( res == 0 )
1054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001056 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001057
Paul Bakkerbd51b262014-07-10 15:26:12 +02001058exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001060}
Paul Bakker33b43f12013-08-20 11:48:36 +02001061/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001062
Paul Bakker33b43f12013-08-20 11:48:36 +02001063/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001064void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1065 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001068 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_mpi_uint r;
1070 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1073 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001074 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001075 if( res == 0 )
1076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001078 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001079
Paul Bakkerbd51b262014-07-10 15:26:12 +02001080exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001082}
Paul Bakker33b43f12013-08-20 11:48:36 +02001083/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001084
Chris Jonesce6fa8f2020-12-03 14:24:33 +00001085/* BEGIN_CASE depends_on:MPI_MAX_BITS_LARGER_THAN_256 */
Azim Khanf1aaec92017-05-30 14:23:15 +01001086void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1087 char * input_E, int radix_N, char * input_N,
1088 int radix_RR, char * input_RR, int radix_X,
1089 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001092 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1094 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1097 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1098 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1099 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001100
Paul Bakker33b43f12013-08-20 11:48:36 +02001101 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001105 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001106 if( res == 0 )
1107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001109 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001110
Paul Bakkerbd51b262014-07-10 15:26:12 +02001111exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1113 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001114}
Paul Bakker33b43f12013-08-20 11:48:36 +02001115/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001116
Paul Bakker33b43f12013-08-20 11:48:36 +02001117/* BEGIN_CASE */
Chris Jones415c7be2020-12-02 10:41:50 +00001118void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesa18813e2020-12-03 11:35:41 +00001119 int radix_RR, char * input_RR, int exp_result )
Chris Jones415c7be2020-12-02 10:41:50 +00001120{
1121 mbedtls_mpi A, E, N, RR, Z;
1122 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1123 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1124
Chris Jonesa18813e2020-12-03 11:35:41 +00001125 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jones415c7be2020-12-02 10:41:50 +00001126 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001127 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001128 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001129
1130 /* Set E to 2^(E_bytes - 1) + 1 */
1131 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1132 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001133 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesa18813e2020-12-03 11:35:41 +00001134
1135 /* Set N to 2^(N_bytes - 1) + 1 */
1136 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1137 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jones415c7be2020-12-02 10:41:50 +00001138 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1139
1140 if( strlen( input_RR ) )
1141 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
1142
Chris Jonesa18813e2020-12-03 11:35:41 +00001143 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jones415c7be2020-12-02 10:41:50 +00001144
1145exit:
1146 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1147 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001152void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1153 char * input_Y, int radix_A, char * input_A,
1154 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001157 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1161 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1162 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1163 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001164 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001165 if( res == 0 )
1166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001168 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001169
Paul Bakkerbd51b262014-07-10 15:26:12 +02001170exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001172}
Paul Bakker33b43f12013-08-20 11:48:36 +02001173/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001176void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001179 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001183 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001184 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001185
Paul Bakkerbd51b262014-07-10 15:26:12 +02001186exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001188}
Paul Bakker33b43f12013-08-20 11:48:36 +02001189/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001192void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001193 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001194{
1195 mbedtls_mpi X;
1196 int res;
1197 mbedtls_test_mpi_random rand;
1198
1199 mbedtls_mpi_init( &X );
1200 rand.data = witnesses;
1201 rand.pos = 0;
1202 rand.chunk_len = chunk_len;
1203
1204 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001205 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1206 mbedtls_test_mpi_miller_rabin_determinizer,
1207 &rand );
1208 TEST_ASSERT( res == 0 );
1209
1210 rand.data = witnesses;
1211 rand.pos = 0;
1212 rand.chunk_len = chunk_len;
1213
Janos Follatha0b67c22018-09-18 14:48:23 +01001214 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1215 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001216 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001217 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001218
1219exit:
1220 mbedtls_mpi_free( &X );
1221}
1222/* END_CASE */
1223
1224/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001225void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001228 int my_ret;
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001231
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001232 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001233 TEST_ASSERT( my_ret == ref_ret );
1234
1235 if( ref_ret == 0 )
1236 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001237 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001238
1239 TEST_ASSERT( actual_bits >= (size_t) bits );
1240 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1241
Janos Follatha0b67c22018-09-18 14:48:23 +01001242 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1243 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001244 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001245 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001246 /* X = ( X - 1 ) / 2 */
1247 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001248 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1249 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001250 }
1251 }
1252
Paul Bakkerbd51b262014-07-10 15:26:12 +02001253exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001255}
1256/* END_CASE */
1257
Paul Bakker33b43f12013-08-20 11:48:36 +02001258/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001259void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1260 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 mbedtls_mpi X, A;
1263 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1266 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1267 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1268 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001269
Paul Bakkerbd51b262014-07-10 15:26:12 +02001270exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001272}
Paul Bakker33b43f12013-08-20 11:48:36 +02001273/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001274
Paul Bakker33b43f12013-08-20 11:48:36 +02001275/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001276void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1277 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 mbedtls_mpi X, A;
1280 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1283 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1284 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1285 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001286
Paul Bakkerbd51b262014-07-10 15:26:12 +02001287exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001289}
Paul Bakker33b43f12013-08-20 11:48:36 +02001290/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001293void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001294{
Andres AG93012e82016-09-09 09:10:28 +01001295 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001296}
Paul Bakker33b43f12013-08-20 11:48:36 +02001297/* END_CASE */