blob: a82bf8181af395181a5806d4e8196dc4e9d2fbff [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
4typedef struct mbedtls_test_mpi_random
5{
6 data_t *data;
7 size_t pos;
8 size_t chunk_len;
9} mbedtls_test_mpi_random;
10
11/*
12 * This function is called by the Miller-Rabin primality test each time it
13 * chooses a random witness. The witnesses (or non-witnesses as provided by the
14 * test) are stored in the data member of the state structure. Each number is in
15 * the format that mbedtls_mpi_read_string understands and is chunk_len long.
16 */
17int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
18 unsigned char* buf,
19 size_t len )
20{
21 mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
22
23 if( random == NULL || random->data->x == NULL || buf == NULL )
24 return( -1 );
25
26 if( random->pos + random->chunk_len > random->data->len
27 || random->chunk_len > len )
28 {
29 return( -1 );
30 }
31
32 memset( buf, 0, len );
33
34 /* The witness is written to the end of the buffer, since the buffer is
35 * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
36 * Writing the witness to the start of the buffer would result in the
37 * buffer being 'witness 000...000', which would be treated as
38 * witness * 2^n for some n. */
39 memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
40 random->chunk_len );
41
42 random->pos += random->chunk_len;
43
44 return( 0 );
45}
Paul Bakker33b43f12013-08-20 11:48:36 +020046/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +000047
Paul Bakker33b43f12013-08-20 11:48:36 +020048/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 * depends_on:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +020050 * END_DEPENDENCIES
51 */
Paul Bakker5690efc2011-05-26 13:16:06 +000052
Hanno Beckerafb607b2018-12-11 14:27:08 +000053/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
54void mpi_invalid_param( )
55{
56 mbedtls_mpi X;
57 const char *s_in = "00101000101010";
58 char s_out[16] = { 0 };
59 unsigned char u_out[16] = { 0 };
60 unsigned char u_in[16] = { 0 };
61 size_t olen;
62 mbedtls_mpi_uint mpi_uint;
63
64 TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
65 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
66 mbedtls_mpi_grow( NULL, 42 ) );
67 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
68 mbedtls_mpi_copy( NULL, &X ) );
69 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
70 mbedtls_mpi_copy( &X, NULL ) );
71 TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
72 TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
73 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
74 mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
75 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
76 mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
77 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
78 mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
79 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
80 mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
81 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
82 mbedtls_mpi_lset( NULL, 42 ) );
83 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
84 mbedtls_mpi_get_bit( NULL, 42 ) );
85 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
86 mbedtls_mpi_set_bit( NULL, 42, 0 ) );
87 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
88 mbedtls_mpi_read_string( NULL, 2, s_in ) );
89 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
90 mbedtls_mpi_read_string( &X, 2, NULL ) );
91 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
92 mbedtls_mpi_write_string( NULL, 2,
93 s_out, sizeof( s_out ),
94 &olen ) );
95 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
96 mbedtls_mpi_write_string( &X, 2,
97 NULL, sizeof( s_out ),
98 &olen ) );
99 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
100 mbedtls_mpi_write_string( &X, 2,
101 s_out, sizeof( s_out ),
102 NULL ) );
103#if defined(MBEDTLS_FS_IO)
104 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
105 mbedtls_mpi_read_file( NULL, 2, stdin ) );
106 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
107 mbedtls_mpi_read_file( &X, 2, NULL ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
109 mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
110#endif /* MBEDTLS_FS_IO */
111
112 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
113 mbedtls_mpi_read_binary( NULL, u_in,
114 sizeof( u_in ) ) );
115 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
116 mbedtls_mpi_read_binary( &X, NULL,
117 sizeof( u_in ) ) );
118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
119 mbedtls_mpi_write_binary( NULL, u_out,
120 sizeof( u_out ) ) );
121 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
122 mbedtls_mpi_write_binary( &X, NULL,
123 sizeof( u_out ) ) );
124 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
125 mbedtls_mpi_shift_l( NULL, 42 ) );
126 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
127 mbedtls_mpi_shift_r( NULL, 42 ) );
128 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
129 mbedtls_mpi_cmp_abs( NULL, &X ) );
130 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
131 mbedtls_mpi_cmp_abs( &X, NULL ) );
132 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
133 mbedtls_mpi_cmp_mpi( NULL, &X ) );
134 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
135 mbedtls_mpi_cmp_mpi( &X, NULL ) );
136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
137 mbedtls_mpi_cmp_int( NULL, 42 ) );
138 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
139 mbedtls_mpi_add_abs( NULL, &X, &X ) );
140 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
141 mbedtls_mpi_add_abs( &X, NULL, &X ) );
142 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
143 mbedtls_mpi_add_abs( &X, &X, NULL ) );
144 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
145 mbedtls_mpi_sub_abs( NULL, &X, &X ) );
146 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
147 mbedtls_mpi_sub_abs( &X, NULL, &X ) );
148 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
149 mbedtls_mpi_sub_abs( &X, &X, NULL ) );
150 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
151 mbedtls_mpi_add_mpi( NULL, &X, &X ) );
152 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
153 mbedtls_mpi_add_mpi( &X, NULL, &X ) );
154 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
155 mbedtls_mpi_add_mpi( &X, &X, NULL ) );
156 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
157 mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
158 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
159 mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
160 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
161 mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
163 mbedtls_mpi_add_int( NULL, &X, 42 ) );
164 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
165 mbedtls_mpi_add_int( &X, NULL, 42 ) );
166 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
167 mbedtls_mpi_sub_int( NULL, &X, 42 ) );
168 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
169 mbedtls_mpi_sub_int( &X, NULL, 42 ) );
170 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
171 mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
173 mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
174 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
175 mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
177 mbedtls_mpi_mul_int( NULL, &X, 42 ) );
178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
179 mbedtls_mpi_mul_int( &X, NULL, 42 ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
181 mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) );
182 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
183 mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
184 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
185 mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
186 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
187 mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
189 mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
190 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
191 mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
192 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
193 mbedtls_mpi_mod_int( NULL, &X, 42 ) );
194 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
195 mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
197 mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
198 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
199 mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
200 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
201 mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
202 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
203 mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
204 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
205 mbedtls_mpi_fill_random( NULL, 42, rnd_std_rand,
206 NULL ) );
207 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
208 mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
209 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
210 mbedtls_mpi_gcd( NULL, &X, &X ) );
211 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
212 mbedtls_mpi_gcd( &X, NULL, &X ) );
213 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
214 mbedtls_mpi_gcd( &X, &X, NULL ) );
215 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
216 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
217 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
218 mbedtls_mpi_inv_mod( &X, NULL, &X ) );
219 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
220 mbedtls_mpi_inv_mod( NULL, &X, &X ) );
221
222exit:
223 return;
224
225}
226/* END_CASE */
227
Paul Bakker33b43f12013-08-20 11:48:36 +0200228/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100229void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200230{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200231 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200232
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200233 mbedtls_mpi_init( &X );
234 mbedtls_mpi_init( &Y );
235 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200236
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200237 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
238 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200239 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200240 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200241
242exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200243 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200244}
245/* END_CASE */
246
247/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100248void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
249 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200250 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000251{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000253 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100254 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200259 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000260 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100261 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200262 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000263 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200264 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000265 }
266 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000267
Paul Bakkerbd51b262014-07-10 15:26:12 +0200268exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000270}
Paul Bakker33b43f12013-08-20 11:48:36 +0200271/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000272
Paul Bakker33b43f12013-08-20 11:48:36 +0200273/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100274void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000277 unsigned char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100278 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000281
Paul Bakkere896fea2009-07-06 06:40:23 +0000282
Azim Khand30ca132017-06-09 04:32:58 +0100283 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100284 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200285 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000286
Paul Bakkerbd51b262014-07-10 15:26:12 +0200287exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000289}
Paul Bakker33b43f12013-08-20 11:48:36 +0200290/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000291
Paul Bakker33b43f12013-08-20 11:48:36 +0200292/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100293void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100294 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100295 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000296{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000298 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000299 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000300
301 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200308 if( buflen > (size_t) output_size )
309 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200312 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000313 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000314
Azim Khand30ca132017-06-09 04:32:58 +0100315 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000316 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000317
Paul Bakkerbd51b262014-07-10 15:26:12 +0200318exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000320}
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100324void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100325 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000326{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000328 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000329 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000330 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000331 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000332
333 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000336
Paul Bakker33b43f12013-08-20 11:48:36 +0200337 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200338 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000340 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000341 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000342
Paul Bakker33b43f12013-08-20 11:48:36 +0200343 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 buflen = mbedtls_mpi_size( &X );
346 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000347
Paul Bakkere896fea2009-07-06 06:40:23 +0000348
Azim Khand30ca132017-06-09 04:32:58 +0100349 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000350 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000351
Paul Bakkerbd51b262014-07-10 15:26:12 +0200352exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000354}
Paul Bakker33b43f12013-08-20 11:48:36 +0200355/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100358void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
359 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000362 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200363 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000368
Paul Bakker33b43f12013-08-20 11:48:36 +0200369 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000370 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200371 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000372 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200373 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000374
Paul Bakker33b43f12013-08-20 11:48:36 +0200375 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000376 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200377 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000378 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200379 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000382
Paul Bakkerbd51b262014-07-10 15:26:12 +0200383exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000385}
Paul Bakker33b43f12013-08-20 11:48:36 +0200386/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000387
Paul Bakker33b43f12013-08-20 11:48:36 +0200388/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100389void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_mpi X;
392 mbedtls_mpi_init( &X );
393 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
394 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000395
Paul Bakkerbd51b262014-07-10 15:26:12 +0200396exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000398}
Paul Bakker33b43f12013-08-20 11:48:36 +0200399/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000400
Paul Bakker33b43f12013-08-20 11:48:36 +0200401/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100402void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
403 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_mpi X, Y;
406 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
409 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100410 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
411
412 if( result == 0 )
413 {
414 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
415 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000416
Paul Bakkerbd51b262014-07-10 15:26:12 +0200417exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000419}
Paul Bakker33b43f12013-08-20 11:48:36 +0200420/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000421
Paul Bakker33b43f12013-08-20 11:48:36 +0200422/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100423void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000424{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_mpi X;
426 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
429 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000430
Paul Bakkerbd51b262014-07-10 15:26:12 +0200431exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000433}
Paul Bakker33b43f12013-08-20 11:48:36 +0200434/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000435
Paul Bakker33b43f12013-08-20 11:48:36 +0200436/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100437void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000438{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_mpi X;
440 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200443 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000444
Paul Bakkerbd51b262014-07-10 15:26:12 +0200445exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000447}
Paul Bakker33b43f12013-08-20 11:48:36 +0200448/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000449
Paul Bakker33b43f12013-08-20 11:48:36 +0200450/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100451void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
452 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000453{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_mpi A, X, Y, Z;
455 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
458 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
459 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
460 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
461 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000462
Paul Bakkerbd51b262014-07-10 15:26:12 +0200463exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000465}
Paul Bakker33b43f12013-08-20 11:48:36 +0200466/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000467
Paul Bakker33b43f12013-08-20 11:48:36 +0200468/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_mpi X;
472 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
475 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
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 Bakker367dae42009-06-28 21:50:27 +0000479}
Paul Bakker33b43f12013-08-20 11:48:36 +0200480/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000481
Paul Bakker33b43f12013-08-20 11:48:36 +0200482/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100483void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
484 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_mpi X, Y;
487 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
490 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
491 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000492
Paul Bakkerbd51b262014-07-10 15:26:12 +0200493exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000495}
Paul Bakker33b43f12013-08-20 11:48:36 +0200496/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000497
Paul Bakker33b43f12013-08-20 11:48:36 +0200498/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100499void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
500 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000501{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_mpi X, Y;
503 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
506 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
507 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
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( &X ); mbedtls_mpi_free( &Y );
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_copy( int input_X, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_mpi X, Y, A;
518 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
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_lset( &Y, input_A ) == 0 );
522 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 );
523 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
524 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
525 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
526 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
527 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000528
Paul Bakkerbd51b262014-07-10 15:26:12 +0200529exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000531}
Paul Bakker33b43f12013-08-20 11:48:36 +0200532/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000533
Paul Bakker33b43f12013-08-20 11:48:36 +0200534/* BEGIN_CASE */
535void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000536{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_mpi X;
538 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
541 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
542 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000543
Paul Bakkerbd51b262014-07-10 15:26:12 +0200544exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000546}
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000548
Paul Bakker33b43f12013-08-20 11:48:36 +0200549/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_mpi X;
553 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100556 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
558 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100559 TEST_ASSERT( X.n == (size_t) after );
560
Paul Bakkerbd51b262014-07-10 15:26:12 +0200561exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100563}
564/* END_CASE */
565
566/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100567void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
568 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_mpi X, Y, XX;
571 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100574 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100576 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
580 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
583 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100584
Paul Bakkerbd51b262014-07-10 15:26:12 +0200585exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100587}
588/* END_CASE */
589
590/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100591void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
592 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
597 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100600 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100602 Y.s = y_sign;
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
605 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
608 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
609 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
612 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
613 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100614
Paul Bakkerbd51b262014-07-10 15:26:12 +0200615exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
617 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100618}
619/* END_CASE */
620
621/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100622void mbedtls_mpi_swap( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000623{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_mpi X, Y, A;
625 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
628 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
629 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 );
630 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
631 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
632 mbedtls_mpi_swap( &X, &Y );
633 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
634 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000635
Paul Bakkerbd51b262014-07-10 15:26:12 +0200636exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000638}
Paul Bakker33b43f12013-08-20 11:48:36 +0200639/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000640
Paul Bakker33b43f12013-08-20 11:48:36 +0200641/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100642void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
643 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_mpi X, Y, Z, A;
646 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
649 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
650 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
651 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
652 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000653
Paul Bakkerbd51b262014-07-10 15:26:12 +0200654exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000656}
Paul Bakker33b43f12013-08-20 11:48:36 +0200657/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000658
Paul Bakker33b43f12013-08-20 11:48:36 +0200659/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100660void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
661 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100662{
663 mbedtls_mpi X, A;
664 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
665
Janos Follath044a86b2015-10-25 10:58:03 +0100666 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100667
668 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
669 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
670 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
671
672 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
673 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
674 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
675
676 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100677 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
678 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
679
680exit:
681 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
682}
683/* END_CASE */
684
685
686/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100687void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
688 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_mpi X, Y, Z, A;
691 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
694 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
695 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
696 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
697 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000698
Paul Bakkerbd51b262014-07-10 15:26:12 +0200699exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000701}
Paul Bakker33b43f12013-08-20 11:48:36 +0200702/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000703
Paul Bakker33b43f12013-08-20 11:48:36 +0200704/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100705void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
706 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000707{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_mpi X, Y, A;
709 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
712 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
713 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
714 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
715 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000716
Paul Bakkerbd51b262014-07-10 15:26:12 +0200717exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000719}
Paul Bakker33b43f12013-08-20 11:48:36 +0200720/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000721
Paul Bakker33b43f12013-08-20 11:48:36 +0200722/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100723void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
724 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000725{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_mpi X, Y, A;
727 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
730 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
731 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
732 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
733 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000734
Paul Bakkerbd51b262014-07-10 15:26:12 +0200735exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000737}
Paul Bakker33b43f12013-08-20 11:48:36 +0200738/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000739
Paul Bakker33b43f12013-08-20 11:48:36 +0200740/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100741void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
742 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000743{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_mpi X, Z, A;
745 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
748 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
749 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
750 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000751
Paul Bakkerbd51b262014-07-10 15:26:12 +0200752exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000754}
Paul Bakker33b43f12013-08-20 11:48:36 +0200755/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000756
Paul Bakker33b43f12013-08-20 11:48:36 +0200757/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100758void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
759 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000760{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_mpi X, Y, Z, A;
762 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
765 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
766 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
767 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
768 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000769
Paul Bakkerbd51b262014-07-10 15:26:12 +0200770exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000772}
Paul Bakker33b43f12013-08-20 11:48:36 +0200773/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000774
Paul Bakker33b43f12013-08-20 11:48:36 +0200775/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100776void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
777 char * input_Y, int radix_A, char * input_A,
778 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000779{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000781 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
785 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
786 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200789 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000790 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000792
Paul Bakkerbd51b262014-07-10 15:26:12 +0200793exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000795}
Paul Bakker33b43f12013-08-20 11:48:36 +0200796/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000797
Paul Bakker33b43f12013-08-20 11:48:36 +0200798/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100799void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
800 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000801{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_mpi X, Z, A;
803 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
806 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
807 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
808 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000809
Paul Bakkerbd51b262014-07-10 15:26:12 +0200810exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000812}
Paul Bakker33b43f12013-08-20 11:48:36 +0200813/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000814
Paul Bakker33b43f12013-08-20 11:48:36 +0200815/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100816void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
817 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000818{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_mpi X, Y, Z, A;
820 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
823 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
824 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
825 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
826 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000827
Paul Bakkerbd51b262014-07-10 15:26:12 +0200828exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000830}
Paul Bakker33b43f12013-08-20 11:48:36 +0200831/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000832
Paul Bakker33b43f12013-08-20 11:48:36 +0200833/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100834void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
835 int radix_A, char * input_A,
836 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000837{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_mpi X, Z, A;
839 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
842 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
843 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200844 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200846 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200848 else
849 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000850
Paul Bakkerbd51b262014-07-10 15:26:12 +0200851exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000853}
Paul Bakker33b43f12013-08-20 11:48:36 +0200854/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000855
Paul Bakker33b43f12013-08-20 11:48:36 +0200856/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100857void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
858 char * input_Y, int radix_A, char * input_A,
859 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000860{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000862 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
864 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
867 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
868 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
869 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
870 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200871 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000872 if( res == 0 )
873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
875 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000876 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000877
Paul Bakkerbd51b262014-07-10 15:26:12 +0200878exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
880 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000881}
Paul Bakker33b43f12013-08-20 11:48:36 +0200882/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000883
Paul Bakker33b43f12013-08-20 11:48:36 +0200884/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100885void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
886 int radix_A, char * input_A, int radix_B,
887 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000890 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
892 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
895 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
896 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
897 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200898 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000899 if( res == 0 )
900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
902 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000903 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000904
Paul Bakkerbd51b262014-07-10 15:26:12 +0200905exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
907 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000908}
Paul Bakker33b43f12013-08-20 11:48:36 +0200909/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000910
Paul Bakker33b43f12013-08-20 11:48:36 +0200911/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100912void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
913 char * input_Y, int radix_A, char * input_A,
914 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000917 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
921 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
922 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
923 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200924 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000925 if( res == 0 )
926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000928 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000929
Paul Bakkerbd51b262014-07-10 15:26:12 +0200930exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000932}
Paul Bakker33b43f12013-08-20 11:48:36 +0200933/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000934
Paul Bakker33b43f12013-08-20 11:48:36 +0200935/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100936void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
937 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000940 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_mpi_uint r;
942 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
945 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200946 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000947 if( res == 0 )
948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000950 }
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 );
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_exp_mod( int radix_A, char * input_A, int radix_E,
959 char * input_E, int radix_N, char * input_N,
960 int radix_RR, char * input_RR, int radix_X,
961 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000962{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000964 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
966 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
969 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
970 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
971 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000972
Paul Bakker33b43f12013-08-20 11:48:36 +0200973 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +0200977 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000978 if( res == 0 )
979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000981 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000982
Paul Bakkerbd51b262014-07-10 15:26:12 +0200983exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
985 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000986}
Paul Bakker33b43f12013-08-20 11:48:36 +0200987/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000988
Paul Bakker33b43f12013-08-20 11:48:36 +0200989/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100990void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
991 char * input_Y, int radix_A, char * input_A,
992 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000995 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
999 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1000 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1001 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001002 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001003 if( res == 0 )
1004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001006 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001007
Paul Bakkerbd51b262014-07-10 15:26:12 +02001008exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001010}
Paul Bakker33b43f12013-08-20 11:48:36 +02001011/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001014void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001017 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001021 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001022 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001023
Paul Bakkerbd51b262014-07-10 15:26:12 +02001024exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001026}
Paul Bakker33b43f12013-08-20 11:48:36 +02001027/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001030void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001031 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001032{
1033 mbedtls_mpi X;
1034 int res;
1035 mbedtls_test_mpi_random rand;
1036
1037 mbedtls_mpi_init( &X );
1038 rand.data = witnesses;
1039 rand.pos = 0;
1040 rand.chunk_len = chunk_len;
1041
1042 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001043 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1044 mbedtls_test_mpi_miller_rabin_determinizer,
1045 &rand );
1046 TEST_ASSERT( res == 0 );
1047
1048 rand.data = witnesses;
1049 rand.pos = 0;
1050 rand.chunk_len = chunk_len;
1051
Janos Follatha0b67c22018-09-18 14:48:23 +01001052 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1053 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001054 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001055 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001056
1057exit:
1058 mbedtls_mpi_free( &X );
1059}
1060/* END_CASE */
1061
1062/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001063void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001066 int my_ret;
1067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001069
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001070 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001071 TEST_ASSERT( my_ret == ref_ret );
1072
1073 if( ref_ret == 0 )
1074 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001075 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001076
1077 TEST_ASSERT( actual_bits >= (size_t) bits );
1078 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1079
Janos Follatha0b67c22018-09-18 14:48:23 +01001080 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1081 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001082 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001083 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001084 /* X = ( X - 1 ) / 2 */
1085 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001086 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1087 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001088 }
1089 }
1090
Paul Bakkerbd51b262014-07-10 15:26:12 +02001091exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001093}
1094/* END_CASE */
1095
Paul Bakker33b43f12013-08-20 11:48:36 +02001096/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001097void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1098 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 mbedtls_mpi X, A;
1101 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1104 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1105 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1106 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001107
Paul Bakkerbd51b262014-07-10 15:26:12 +02001108exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001110}
Paul Bakker33b43f12013-08-20 11:48:36 +02001111/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001112
Paul Bakker33b43f12013-08-20 11:48:36 +02001113/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001114void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1115 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 mbedtls_mpi X, A;
1118 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1121 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1122 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1123 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001124
Paul Bakkerbd51b262014-07-10 15:26:12 +02001125exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001127}
Paul Bakker33b43f12013-08-20 11:48:36 +02001128/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001131void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001132{
Andres AG93012e82016-09-09 09:10:28 +01001133 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001134}
Paul Bakker33b43f12013-08-20 11:48:36 +02001135/* END_CASE */