blob: f44db7022a81133e4de77dd2f8ac93c1c18a8a90 [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 Jones4592bd82020-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,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200247 mbedtls_mpi_fill_random( NULL, 42,
248 mbedtls_test_rnd_std_rand,
Hanno Beckerafb607b2018-12-11 14:27:08 +0000249 NULL ) );
250 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
251 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000252
Hanno Beckerafb607b2018-12-11 14:27:08 +0000253 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
254 mbedtls_mpi_gcd( NULL, &X, &X ) );
255 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
256 mbedtls_mpi_gcd( &X, NULL, &X ) );
257 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
258 mbedtls_mpi_gcd( &X, &X, NULL ) );
Hanno Beckere1185042018-12-13 14:31:46 +0000259
Hanno Beckerafb607b2018-12-11 14:27:08 +0000260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
261 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
262 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
263 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
264 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
Hanno Beckere1185042018-12-13 14:31:46 +0000265 mbedtls_mpi_inv_mod( &X, &X, NULL ) );
Hanno Beckerafb607b2018-12-11 14:27:08 +0000266
267exit:
268 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000269}
270/* END_CASE */
271
Paul Bakker33b43f12013-08-20 11:48:36 +0200272/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100273void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200274{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200275 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200276
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200277 mbedtls_mpi_init( &X );
278 mbedtls_mpi_init( &Y );
279 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200280
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200281 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
282 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200283 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200284 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200285
286exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200287 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200288}
289/* END_CASE */
290
291/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100292void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
293 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200294 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000295{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000297 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100298 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000301
Janos Follath04dadb72019-03-06 12:29:37 +0000302 memset( str, '!', sizeof( str ) );
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200305 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000306 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100307 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200308 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000309 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200310 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Janos Follath04dadb72019-03-06 12:29:37 +0000311 TEST_ASSERT( str[len] == '!' );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000312 }
313 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000314
Paul Bakkerbd51b262014-07-10 15:26:12 +0200315exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000317}
Paul Bakker33b43f12013-08-20 11:48:36 +0200318/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000319
Paul Bakker33b43f12013-08-20 11:48:36 +0200320/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100321void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000322{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000324 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100325 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000328
Paul Bakkere896fea2009-07-06 06:40:23 +0000329
Azim Khand30ca132017-06-09 04:32:58 +0100330 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000331 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200332 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000333
Paul Bakkerbd51b262014-07-10 15:26:12 +0200334exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000336}
Paul Bakker33b43f12013-08-20 11:48:36 +0200337/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000338
Paul Bakker33b43f12013-08-20 11:48:36 +0200339/* BEGIN_CASE */
Janos Follatha778a942019-02-13 10:28:28 +0000340void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
341{
342 mbedtls_mpi X;
Janos Follathe5670f22019-02-25 16:11:58 +0000343 char str[1000];
Janos Follatha778a942019-02-13 10:28:28 +0000344 size_t len;
345
346 mbedtls_mpi_init( &X );
347
348
349 TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
Janos Follathe5670f22019-02-25 16:11:58 +0000350 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
Janos Follatha778a942019-02-13 10:28:28 +0000351 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
352
353exit:
354 mbedtls_mpi_free( &X );
355}
356/* END_CASE */
357
358/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100359void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100360 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100361 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000362{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000364 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000365 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000366
367 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200374 if( buflen > (size_t) output_size )
375 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200378 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000379 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000380
Ronald Cron2dbba992020-06-10 11:42:32 +0200381 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
382 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000383 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000384
Paul Bakkerbd51b262014-07-10 15:26:12 +0200385exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000387}
Paul Bakker33b43f12013-08-20 11:48:36 +0200388/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000389
Janos Follathe344d0f2019-02-19 16:17:40 +0000390/* BEGIN_CASE */
391void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
392 data_t * input_A, int output_size,
393 int result )
394{
395 mbedtls_mpi X;
396 unsigned char buf[1000];
397 size_t buflen;
398
399 memset( buf, 0x00, 1000 );
400
401 mbedtls_mpi_init( &X );
402
403 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
404
405 buflen = mbedtls_mpi_size( &X );
406 if( buflen > (size_t) output_size )
407 buflen = (size_t) output_size;
408
409 TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
410 if( result == 0)
411 {
412
Ronald Cron2dbba992020-06-10 11:42:32 +0200413 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
414 buflen, input_A->len ) == 0 );
Janos Follathe344d0f2019-02-19 16:17:40 +0000415 }
416
417exit:
418 mbedtls_mpi_free( &X );
419}
420/* END_CASE */
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100423void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100424 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000425{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000427 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000428 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000429 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000430 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000431
432 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000435
Paul Bakker33b43f12013-08-20 11:48:36 +0200436 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200437 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000439 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000440 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000441
Paul Bakker33b43f12013-08-20 11:48:36 +0200442 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 buflen = mbedtls_mpi_size( &X );
445 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000446
Paul Bakkere896fea2009-07-06 06:40:23 +0000447
Ronald Cron2dbba992020-06-10 11:42:32 +0200448 TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
449 buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000450 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000451
Paul Bakkerbd51b262014-07-10 15:26:12 +0200452exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000454}
Paul Bakker33b43f12013-08-20 11:48:36 +0200455/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100458void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
459 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000462 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200463 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000468
Paul Bakker33b43f12013-08-20 11:48:36 +0200469 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000470 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200471 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000472 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200473 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000474
Paul Bakker33b43f12013-08-20 11:48:36 +0200475 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000476 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200477 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000478 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200479 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000482
Paul Bakkerbd51b262014-07-10 15:26:12 +0200483exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000485}
Paul Bakker33b43f12013-08-20 11:48:36 +0200486/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000487
Paul Bakker33b43f12013-08-20 11:48:36 +0200488/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100489void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000490{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_mpi X;
492 mbedtls_mpi_init( &X );
493 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
494 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000495
Paul Bakkerbd51b262014-07-10 15:26:12 +0200496exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000498}
Paul Bakker33b43f12013-08-20 11:48:36 +0200499/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000500
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100502void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
503 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000504{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_mpi X, Y;
506 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
509 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100510 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
511
512 if( result == 0 )
513 {
514 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
515 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000516
Paul Bakkerbd51b262014-07-10 15:26:12 +0200517exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000519}
Paul Bakker33b43f12013-08-20 11:48:36 +0200520/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000521
Paul Bakker33b43f12013-08-20 11:48:36 +0200522/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100523void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_mpi X;
526 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
529 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000530
Paul Bakkerbd51b262014-07-10 15:26:12 +0200531exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000533}
Paul Bakker33b43f12013-08-20 11:48:36 +0200534/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000535
Paul Bakker33b43f12013-08-20 11:48:36 +0200536/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100537void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_mpi X;
540 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200543 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000544
Paul Bakkerbd51b262014-07-10 15:26:12 +0200545exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000547}
Paul Bakker33b43f12013-08-20 11:48:36 +0200548/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000549
Paul Bakker33b43f12013-08-20 11:48:36 +0200550/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100551void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
552 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_mpi A, X, Y, Z;
555 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
558 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
559 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
560 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
561 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000562
Paul Bakkerbd51b262014-07-10 15:26:12 +0200563exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000565}
Paul Bakker33b43f12013-08-20 11:48:36 +0200566/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000567
Paul Bakker33b43f12013-08-20 11:48:36 +0200568/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000570{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_mpi X;
572 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
575 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000576
Paul Bakkerbd51b262014-07-10 15:26:12 +0200577exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000579}
Paul Bakker33b43f12013-08-20 11:48:36 +0200580/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000581
Paul Bakker33b43f12013-08-20 11:48:36 +0200582/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100583void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
584 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_mpi X, Y;
587 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
590 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
591 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000592
Paul Bakkerbd51b262014-07-10 15:26:12 +0200593exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000595}
Paul Bakker33b43f12013-08-20 11:48:36 +0200596/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000597
Paul Bakker33b43f12013-08-20 11:48:36 +0200598/* BEGIN_CASE */
Janos Follathb7e1b492019-10-14 09:21:49 +0100599void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
600 int size_Y, char * input_Y,
Janos Follath0e5532d2019-10-11 14:21:53 +0100601 int input_ret, int input_err )
Janos Follath385d5b82019-09-11 16:07:14 +0100602{
Janos Follath0e5532d2019-10-11 14:21:53 +0100603 unsigned ret;
604 unsigned input_uret = input_ret;
Janos Follath385d5b82019-09-11 16:07:14 +0100605 mbedtls_mpi X, Y;
606 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
607
Janos Follathb7e1b492019-10-14 09:21:49 +0100608 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
609 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100610
Gilles Peskine9018b112020-01-21 16:30:53 +0100611 TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
612 TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
Janos Follath385d5b82019-09-11 16:07:14 +0100613
Janos Follath0e5532d2019-10-11 14:21:53 +0100614 TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
Janos Follath385d5b82019-09-11 16:07:14 +0100615 if( input_err == 0 )
Janos Follath0e5532d2019-10-11 14:21:53 +0100616 TEST_ASSERT( ret == input_uret );
Janos Follath385d5b82019-09-11 16:07:14 +0100617
618exit:
619 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
620}
621/* END_CASE */
622
623/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100624void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
625 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_mpi X, Y;
628 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
631 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
632 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000633
Paul Bakkerbd51b262014-07-10 15:26:12 +0200634exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000636}
Paul Bakker33b43f12013-08-20 11:48:36 +0200637/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000638
Paul Bakker33b43f12013-08-20 11:48:36 +0200639/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100640void mbedtls_mpi_copy_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000641{
Gilles Peskine7428b452020-01-20 21:01:51 +0100642 mbedtls_mpi X, Y;
643 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100646 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100649 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
650 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000651
Paul Bakkerbd51b262014-07-10 15:26:12 +0200652exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100653 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
654}
655/* END_CASE */
656
657/* BEGIN_CASE */
658void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y )
659{
660 mbedtls_mpi X, Y, X0;
661 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
662
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100663 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
664 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
665 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100666 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
667
668 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
669 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
670 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
671
672exit:
673 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000674}
Paul Bakker33b43f12013-08-20 11:48:36 +0200675/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000676
Paul Bakker33b43f12013-08-20 11:48:36 +0200677/* BEGIN_CASE */
678void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000679{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_mpi X;
681 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
684 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
685 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000686
Paul Bakkerbd51b262014-07-10 15:26:12 +0200687exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000689}
Paul Bakker33b43f12013-08-20 11:48:36 +0200690/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000691
Paul Bakker33b43f12013-08-20 11:48:36 +0200692/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_mpi X;
696 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100699 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
701 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100702 TEST_ASSERT( X.n == (size_t) after );
703
Paul Bakkerbd51b262014-07-10 15:26:12 +0200704exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100706}
707/* END_CASE */
708
709/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100710void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
711 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100712{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_mpi X, Y, XX;
714 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100717 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100719 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
723 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
726 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100727
Paul Bakkerbd51b262014-07-10 15:26:12 +0200728exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100730}
731/* END_CASE */
732
733/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100734void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
735 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
740 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100743 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100745 Y.s = y_sign;
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
748 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
751 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
752 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
755 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
756 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100757
Paul Bakkerbd51b262014-07-10 15:26:12 +0200758exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
760 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100761}
762/* END_CASE */
763
764/* BEGIN_CASE */
Gilles Peskine7428b452020-01-20 21:01:51 +0100765void mbedtls_mpi_swap_sint( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000766{
Gilles Peskine7428b452020-01-20 21:01:51 +0100767 mbedtls_mpi X, Y;
768 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
771 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100772 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
773 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_mpi_swap( &X, &Y );
Gilles Peskine7428b452020-01-20 21:01:51 +0100776 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
777 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000778
Paul Bakkerbd51b262014-07-10 15:26:12 +0200779exit:
Gilles Peskine7428b452020-01-20 21:01:51 +0100780 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
781}
782/* END_CASE */
783
784/* BEGIN_CASE */
785void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y )
786{
787 mbedtls_mpi X, Y, X0, Y0;
788 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
789 mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
790
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100791 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
792 TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
793 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
794 TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100795
796 mbedtls_mpi_swap( &X, &Y );
797 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
798 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
799
800exit:
801 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
802 mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
803}
804/* END_CASE */
805
806/* BEGIN_CASE */
807void mpi_swap_self( data_t *input_X )
808{
809 mbedtls_mpi X, X0;
810 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
811
Gilles Peskine9a6ecee2020-02-03 16:15:47 +0100812 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
813 TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
Gilles Peskine7428b452020-01-20 21:01:51 +0100814
815 mbedtls_mpi_swap( &X, &X );
816 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
817
818exit:
819 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000820}
Paul Bakker33b43f12013-08-20 11:48:36 +0200821/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000822
Paul Bakker33b43f12013-08-20 11:48:36 +0200823/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100824void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
825 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000826{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_mpi X, Y, Z, A;
828 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
831 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
832 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
833 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
834 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000835
Paul Bakkerbd51b262014-07-10 15:26:12 +0200836exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000838}
Paul Bakker33b43f12013-08-20 11:48:36 +0200839/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000840
Paul Bakker33b43f12013-08-20 11:48:36 +0200841/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100842void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
843 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100844{
845 mbedtls_mpi X, A;
846 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
847
Janos Follath044a86b2015-10-25 10:58:03 +0100848 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100849
850 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
851 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
852 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
853
854 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
855 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
856 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
857
858 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100859 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
860 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
861
862exit:
863 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
864}
865/* END_CASE */
866
867
868/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100869void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
870 char * input_Y, 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, Y, Z, A;
873 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); 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( &Y, radix_Y, input_Y ) == 0 );
877 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
878 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
879 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000880
Paul Bakkerbd51b262014-07-10 15:26:12 +0200881exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000883}
Paul Bakker33b43f12013-08-20 11:48:36 +0200884/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000885
Paul Bakker33b43f12013-08-20 11:48:36 +0200886/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100887void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
888 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_mpi X, Y, A;
891 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
894 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
895 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
896 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
897 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000898
Paul Bakkerbd51b262014-07-10 15:26:12 +0200899exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000901}
Paul Bakker33b43f12013-08-20 11:48:36 +0200902/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000903
Paul Bakker33b43f12013-08-20 11:48:36 +0200904/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100905void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
906 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000907{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_mpi X, Y, A;
909 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
912 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
913 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
914 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
915 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000916
Paul Bakkerbd51b262014-07-10 15:26:12 +0200917exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000919}
Paul Bakker33b43f12013-08-20 11:48:36 +0200920/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000921
Paul Bakker33b43f12013-08-20 11:48:36 +0200922/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100923void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
924 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000925{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 mbedtls_mpi X, Z, A;
927 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
930 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
931 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
932 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000933
Paul Bakkerbd51b262014-07-10 15:26:12 +0200934exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000936}
Paul Bakker33b43f12013-08-20 11:48:36 +0200937/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000938
Paul Bakker33b43f12013-08-20 11:48:36 +0200939/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100940void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
941 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_mpi X, Y, Z, A;
944 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
947 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
948 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
949 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
950 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000951
Paul Bakkerbd51b262014-07-10 15:26:12 +0200952exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000954}
Paul Bakker33b43f12013-08-20 11:48:36 +0200955/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000956
Paul Bakker33b43f12013-08-20 11:48:36 +0200957/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100958void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
959 char * input_Y, int radix_A, char * input_A,
960 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000963 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
967 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
968 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200971 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000972 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000974
Paul Bakkerbd51b262014-07-10 15:26:12 +0200975exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000977}
Paul Bakker33b43f12013-08-20 11:48:36 +0200978/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000979
Paul Bakker33b43f12013-08-20 11:48:36 +0200980/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100981void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
982 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000983{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_mpi X, Z, A;
985 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
988 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
989 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
990 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000991
Paul Bakkerbd51b262014-07-10 15:26:12 +0200992exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000994}
Paul Bakker33b43f12013-08-20 11:48:36 +0200995/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000996
Paul Bakker33b43f12013-08-20 11:48:36 +0200997/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100998void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
999 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001000{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_mpi X, Y, Z, A;
1002 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1005 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1006 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1007 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
1008 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001009
Paul Bakkerbd51b262014-07-10 15:26:12 +02001010exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001012}
Paul Bakker33b43f12013-08-20 11:48:36 +02001013/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001014
Paul Bakker33b43f12013-08-20 11:48:36 +02001015/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001016void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
1017 int radix_A, char * input_A,
1018 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +00001019{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 mbedtls_mpi X, Z, A;
1021 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1024 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1025 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001026 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001028 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001030 else
1031 TEST_ASSERT( "unknown operator" == 0 );
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( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001035}
Paul Bakker33b43f12013-08-20 11:48:36 +02001036/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001037
Paul Bakker33b43f12013-08-20 11:48:36 +02001038/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001039void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
1040 char * input_Y, int radix_A, char * input_A,
1041 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001044 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
1046 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
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 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1052 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001053 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001054 if( res == 0 )
1055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1057 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001058 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001059
Paul Bakkerbd51b262014-07-10 15:26:12 +02001060exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
1062 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001063}
Paul Bakker33b43f12013-08-20 11:48:36 +02001064/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001065
Paul Bakker33b43f12013-08-20 11:48:36 +02001066/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001067void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
1068 int radix_A, char * input_A, int radix_B,
1069 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +00001072 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
1074 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1077 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1078 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
1079 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001080 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001081 if( res == 0 )
1082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
1084 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001085 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001086
Paul Bakkerbd51b262014-07-10 15:26:12 +02001087exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
1089 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +00001090}
Paul Bakker33b43f12013-08-20 11:48:36 +02001091/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001092
Paul Bakker33b43f12013-08-20 11:48:36 +02001093/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001094void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
1095 char * input_Y, int radix_A, char * input_A,
1096 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001099 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1103 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1104 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1105 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001106 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001107 if( res == 0 )
1108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001110 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001111
Paul Bakkerbd51b262014-07-10 15:26:12 +02001112exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
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 */
Azim Khanf1aaec92017-05-30 14:23:15 +01001118void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
1119 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001122 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 mbedtls_mpi_uint r;
1124 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1127 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001128 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001129 if( res == 0 )
1130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +00001132 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001133
Paul Bakkerbd51b262014-07-10 15:26:12 +02001134exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001136}
Paul Bakker33b43f12013-08-20 11:48:36 +02001137/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001138
Chris Jones4592bd82020-12-03 14:24:33 +00001139/* BEGIN_CASE depends_on:MPI_MAX_BITS_LARGER_THAN_256 */
Azim Khanf1aaec92017-05-30 14:23:15 +01001140void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
1141 char * input_E, int radix_N, char * input_N,
1142 int radix_RR, char * input_RR, int radix_X,
1143 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +00001146 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1148 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1151 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1152 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1153 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001154
Paul Bakker33b43f12013-08-20 11:48:36 +02001155 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +02001159 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001160 if( res == 0 )
1161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001163 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001164
Paul Bakkerbd51b262014-07-10 15:26:12 +02001165exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1167 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001168}
Paul Bakker33b43f12013-08-20 11:48:36 +02001169/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001170
Paul Bakker33b43f12013-08-20 11:48:36 +02001171/* BEGIN_CASE */
Chris Jonesd10b3312020-12-02 10:41:50 +00001172void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
Chris Jonesaa850cd2020-12-03 11:35:41 +00001173 int radix_RR, char * input_RR, int exp_result )
Chris Jonesd10b3312020-12-02 10:41:50 +00001174{
1175 mbedtls_mpi A, E, N, RR, Z;
1176 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
1177 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
1178
Chris Jonesaa850cd2020-12-03 11:35:41 +00001179 /* Set A to 2^(A_bytes - 1) + 1 */
Chris Jonesd10b3312020-12-02 10:41:50 +00001180 TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001181 TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001182 TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001183
1184 /* Set E to 2^(E_bytes - 1) + 1 */
1185 TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
1186 TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001187 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
Chris Jonesaa850cd2020-12-03 11:35:41 +00001188
1189 /* Set N to 2^(N_bytes - 1) + 1 */
1190 TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
1191 TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
Chris Jonesd10b3312020-12-02 10:41:50 +00001192 TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
1193
1194 if( strlen( input_RR ) )
1195 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
1196
Chris Jonesaa850cd2020-12-03 11:35:41 +00001197 TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
Chris Jonesd10b3312020-12-02 10:41:50 +00001198
1199exit:
1200 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
1201 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
1202}
1203/* END_CASE */
1204
1205/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001206void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
1207 char * input_Y, int radix_A, char * input_A,
1208 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +00001211 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1215 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1216 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1217 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001218 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001219 if( res == 0 )
1220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001222 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001223
Paul Bakkerbd51b262014-07-10 15:26:12 +02001224exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001226}
Paul Bakker33b43f12013-08-20 11:48:36 +02001227/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001230void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001233 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001237 res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001238 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001239
Paul Bakkerbd51b262014-07-10 15:26:12 +02001240exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001242}
Paul Bakker33b43f12013-08-20 11:48:36 +02001243/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001246void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001247 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001248{
1249 mbedtls_mpi X;
1250 int res;
1251 mbedtls_test_mpi_random rand;
1252
1253 mbedtls_mpi_init( &X );
1254 rand.data = witnesses;
1255 rand.pos = 0;
1256 rand.chunk_len = chunk_len;
1257
1258 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001259 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1260 mbedtls_test_mpi_miller_rabin_determinizer,
1261 &rand );
1262 TEST_ASSERT( res == 0 );
1263
1264 rand.data = witnesses;
1265 rand.pos = 0;
1266 rand.chunk_len = chunk_len;
1267
Janos Follatha0b67c22018-09-18 14:48:23 +01001268 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1269 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001270 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001271 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001272
1273exit:
1274 mbedtls_mpi_free( &X );
1275}
1276/* END_CASE */
1277
1278/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001279void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001282 int my_ret;
1283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001285
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001286 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
1287 mbedtls_test_rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001288 TEST_ASSERT( my_ret == ref_ret );
1289
1290 if( ref_ret == 0 )
1291 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001292 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001293
1294 TEST_ASSERT( actual_bits >= (size_t) bits );
1295 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1296
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001297 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1298 mbedtls_test_rnd_std_rand,
1299 NULL ) == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001300 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001301 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001302 /* X = ( X - 1 ) / 2 */
1303 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001304 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
1305 mbedtls_test_rnd_std_rand,
1306 NULL ) == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001307 }
1308 }
1309
Paul Bakkerbd51b262014-07-10 15:26:12 +02001310exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001312}
1313/* END_CASE */
1314
Paul Bakker33b43f12013-08-20 11:48:36 +02001315/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001316void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1317 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 mbedtls_mpi X, A;
1320 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1323 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1324 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1325 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001326
Paul Bakkerbd51b262014-07-10 15:26:12 +02001327exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001329}
Paul Bakker33b43f12013-08-20 11:48:36 +02001330/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001331
Paul Bakker33b43f12013-08-20 11:48:36 +02001332/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001333void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1334 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001335{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 mbedtls_mpi X, A;
1337 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1340 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1341 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1342 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001343
Paul Bakkerbd51b262014-07-10 15:26:12 +02001344exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001346}
Paul Bakker33b43f12013-08-20 11:48:36 +02001347/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001350void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001351{
Andres AG93012e82016-09-09 09:10:28 +01001352 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001353}
Paul Bakker33b43f12013-08-20 11:48:36 +02001354/* END_CASE */