blob: ddab63e079be94f11c9a6699cbfbbfdc44555fc4 [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
Hanno Becker56b661c2018-12-12 13:37:25 +0000222 mbedtls_mpi_free( NULL );
223
Hanno Beckerafb607b2018-12-11 14:27:08 +0000224exit:
225 return;
Hanno Beckerafb607b2018-12-11 14:27:08 +0000226}
227/* END_CASE */
228
Paul Bakker33b43f12013-08-20 11:48:36 +0200229/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100230void mpi_null( )
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200231{
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200232 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200233
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200234 mbedtls_mpi_init( &X );
235 mbedtls_mpi_init( &Y );
236 mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200237
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200238 TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
239 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200240 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200241 TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200242
243exit:
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200244 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200245}
246/* END_CASE */
247
248/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100249void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
250 char * input_A, int output_size, int result_read,
Paul Bakker33b43f12013-08-20 11:48:36 +0200251 int result_write )
Paul Bakker367dae42009-06-28 21:50:27 +0000252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000254 char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100255 size_t len;
Paul Bakker367dae42009-06-28 21:50:27 +0000256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
Paul Bakker33b43f12013-08-20 11:48:36 +0200260 if( result_read == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000261 {
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100262 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
Paul Bakker33b43f12013-08-20 11:48:36 +0200263 if( result_write == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000264 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200265 TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000266 }
267 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000268
Paul Bakkerbd51b262014-07-10 15:26:12 +0200269exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000271}
Paul Bakker33b43f12013-08-20 11:48:36 +0200272/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000273
Paul Bakker33b43f12013-08-20 11:48:36 +0200274/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100275void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
Paul Bakkere896fea2009-07-06 06:40:23 +0000276{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000278 unsigned char str[1000];
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100279 size_t len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000282
Paul Bakkere896fea2009-07-06 06:40:23 +0000283
Azim Khand30ca132017-06-09 04:32:58 +0100284 TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100285 TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200286 TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000287
Paul Bakkerbd51b262014-07-10 15:26:12 +0200288exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000290}
Paul Bakker33b43f12013-08-20 11:48:36 +0200291/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000292
Paul Bakker33b43f12013-08-20 11:48:36 +0200293/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +0100294void mbedtls_mpi_write_binary( int radix_X, char * input_X,
Azim Khan5fcca462018-06-29 11:05:32 +0100295 data_t * input_A, int output_size,
Azim Khanf1aaec92017-05-30 14:23:15 +0100296 int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000299 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000300 size_t buflen;
Paul Bakkere896fea2009-07-06 06:40:23 +0000301
302 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 buflen = mbedtls_mpi_size( &X );
Paul Bakker33b43f12013-08-20 11:48:36 +0200309 if( buflen > (size_t) output_size )
310 buflen = (size_t) output_size;
Paul Bakkere896fea2009-07-06 06:40:23 +0000311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200313 if( result == 0)
Paul Bakkerba48cb22009-07-12 11:01:32 +0000314 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000315
Azim Khand30ca132017-06-09 04:32:58 +0100316 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000317 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000318
Paul Bakkerbd51b262014-07-10 15:26:12 +0200319exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000321}
Paul Bakker33b43f12013-08-20 11:48:36 +0200322/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khand30ca132017-06-09 04:32:58 +0100325void mbedtls_mpi_read_file( int radix_X, char * input_file,
Azim Khan5fcca462018-06-29 11:05:32 +0100326 data_t * input_A, int result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_mpi X;
Paul Bakkere896fea2009-07-06 06:40:23 +0000329 unsigned char buf[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000330 size_t buflen;
Paul Bakker69998dd2009-07-11 19:15:20 +0000331 FILE *file;
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000332 int ret;
Paul Bakkere896fea2009-07-06 06:40:23 +0000333
334 memset( buf, 0x00, 1000 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000337
Paul Bakker33b43f12013-08-20 11:48:36 +0200338 file = fopen( input_file, "r" );
Paul Bakker8a0c0a92014-04-17 16:08:20 +0200339 TEST_ASSERT( file != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 ret = mbedtls_mpi_read_file( &X, radix_X, file );
Paul Bakkere896fea2009-07-06 06:40:23 +0000341 fclose(file);
Manuel Pégourié-Gonnarde43187d2015-02-14 16:01:34 +0000342 TEST_ASSERT( ret == result );
Paul Bakkere896fea2009-07-06 06:40:23 +0000343
Paul Bakker33b43f12013-08-20 11:48:36 +0200344 if( result == 0 )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 buflen = mbedtls_mpi_size( &X );
347 TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000348
Paul Bakkere896fea2009-07-06 06:40:23 +0000349
Azim Khand30ca132017-06-09 04:32:58 +0100350 TEST_ASSERT( hexcmp( buf, input_A->x, buflen, input_A->len ) == 0 );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000351 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000352
Paul Bakkerbd51b262014-07-10 15:26:12 +0200353exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000355}
Paul Bakker33b43f12013-08-20 11:48:36 +0200356/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100359void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
360 char * output_file )
Paul Bakkere896fea2009-07-06 06:40:23 +0000361{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_mpi X, Y;
Paul Bakker69998dd2009-07-11 19:15:20 +0000363 FILE *file_out, *file_in;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200364 int ret;
Paul Bakker69998dd2009-07-11 19:15:20 +0000365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000369
Paul Bakker33b43f12013-08-20 11:48:36 +0200370 file_out = fopen( output_file, "w" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000371 TEST_ASSERT( file_out != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200372 ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
Paul Bakkere896fea2009-07-06 06:40:23 +0000373 fclose(file_out);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200374 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000375
Paul Bakker33b43f12013-08-20 11:48:36 +0200376 file_in = fopen( output_file, "r" );
Paul Bakker5690efc2011-05-26 13:16:06 +0000377 TEST_ASSERT( file_in != NULL );
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200378 ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
Paul Bakkere896fea2009-07-06 06:40:23 +0000379 fclose(file_in);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200380 TEST_ASSERT( ret == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000383
Paul Bakkerbd51b262014-07-10 15:26:12 +0200384exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakkere896fea2009-07-06 06:40:23 +0000386}
Paul Bakker33b43f12013-08-20 11:48:36 +0200387/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000388
Paul Bakker33b43f12013-08-20 11:48:36 +0200389/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100390void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_mpi X;
393 mbedtls_mpi_init( &X );
394 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
395 TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000396
Paul Bakkerbd51b262014-07-10 15:26:12 +0200397exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_mpi_free( &X );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000399}
Paul Bakker33b43f12013-08-20 11:48:36 +0200400/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000401
Paul Bakker33b43f12013-08-20 11:48:36 +0200402/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100403void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
404 int radix_Y, char * output_Y, int result )
Paul Bakker2f5947e2011-05-18 15:47:11 +0000405{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_mpi X, Y;
407 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
410 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
Paul Bakkerec5ceb62016-07-14 12:47:07 +0100411 TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
412
413 if( result == 0 )
414 {
415 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
416 }
Paul Bakker2f5947e2011-05-18 15:47:11 +0000417
Paul Bakkerbd51b262014-07-10 15:26:12 +0200418exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000420}
Paul Bakker33b43f12013-08-20 11:48:36 +0200421/* END_CASE */
Paul Bakker2f5947e2011-05-18 15:47:11 +0000422
Paul Bakker33b43f12013-08-20 11:48:36 +0200423/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100424void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000425{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_mpi X;
427 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
430 TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000431
Paul Bakkerbd51b262014-07-10 15:26:12 +0200432exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000434}
Paul Bakker33b43f12013-08-20 11:48:36 +0200435/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000436
Paul Bakker33b43f12013-08-20 11:48:36 +0200437/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100438void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
Paul Bakkere896fea2009-07-06 06:40:23 +0000439{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_mpi X;
441 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200444 TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000445
Paul Bakkerbd51b262014-07-10 15:26:12 +0200446exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000448}
Paul Bakker33b43f12013-08-20 11:48:36 +0200449/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000450
Paul Bakker33b43f12013-08-20 11:48:36 +0200451/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100452void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
453 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000454{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_mpi A, X, Y, Z;
456 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
459 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
460 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
461 TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
462 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000463
Paul Bakkerbd51b262014-07-10 15:26:12 +0200464exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Paul Bakker367dae42009-06-28 21:50:27 +0000466}
Paul Bakker33b43f12013-08-20 11:48:36 +0200467/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000468
Paul Bakker33b43f12013-08-20 11:48:36 +0200469/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
Paul Bakker367dae42009-06-28 21:50:27 +0000471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_mpi X;
473 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
476 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000477
Paul Bakkerbd51b262014-07-10 15:26:12 +0200478exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000480}
Paul Bakker33b43f12013-08-20 11:48:36 +0200481/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000482
Paul Bakker33b43f12013-08-20 11:48:36 +0200483/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100484void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
485 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_mpi X, Y;
488 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
491 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
492 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000493
Paul Bakkerbd51b262014-07-10 15:26:12 +0200494exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000496}
Paul Bakker33b43f12013-08-20 11:48:36 +0200497/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000498
Paul Bakker33b43f12013-08-20 11:48:36 +0200499/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100500void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
501 char * input_Y, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_mpi X, Y;
504 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
507 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
508 TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000509
Paul Bakkerbd51b262014-07-10 15:26:12 +0200510exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
Paul Bakker367dae42009-06-28 21:50:27 +0000512}
Paul Bakker33b43f12013-08-20 11:48:36 +0200513/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000514
Paul Bakker33b43f12013-08-20 11:48:36 +0200515/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516void mbedtls_mpi_copy( int input_X, int input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_mpi X, Y, A;
519 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
522 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_A ) == 0 );
523 TEST_ASSERT( mbedtls_mpi_lset( &A, input_A ) == 0 );
524 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
525 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
526 TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
527 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
528 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) != 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000529
Paul Bakkerbd51b262014-07-10 15:26:12 +0200530exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000532}
Paul Bakker33b43f12013-08-20 11:48:36 +0200533/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000534
Paul Bakker33b43f12013-08-20 11:48:36 +0200535/* BEGIN_CASE */
536void mpi_copy_self( int input_X )
Paul Bakkere896fea2009-07-06 06:40:23 +0000537{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_mpi X;
539 mbedtls_mpi_init( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
542 TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
543 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000544
Paul Bakkerbd51b262014-07-10 15:26:12 +0200545exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_mpi_free( &X );
Paul Bakkere896fea2009-07-06 06:40:23 +0000547}
Paul Bakker33b43f12013-08-20 11:48:36 +0200548/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000549
Paul Bakker33b43f12013-08-20 11:48:36 +0200550/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551void mbedtls_mpi_shrink( int before, int used, int min, int after )
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_mpi X;
554 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100557 TEST_ASSERT( used <= before );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
559 TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100560 TEST_ASSERT( X.n == (size_t) after );
561
Paul Bakkerbd51b262014-07-10 15:26:12 +0200562exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100564}
565/* END_CASE */
566
567/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100568void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign,
569 char * y_str )
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100570{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_mpi X, Y, XX;
572 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100575 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnard3e3d2b82013-11-21 21:12:26 +0100577 Y.s = y_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
581 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
584 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100585
Paul Bakkerbd51b262014-07-10 15:26:12 +0200586exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100588}
589/* END_CASE */
590
591/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100592void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign,
593 char * y_str )
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100594{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_mpi X, Y, XX, YY;
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
598 mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100601 X.s = x_sign;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100603 Y.s = y_sign;
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
606 TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
609 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
610 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
613 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
614 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100615
Paul Bakkerbd51b262014-07-10 15:26:12 +0200616exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
618 mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100619}
620/* END_CASE */
621
622/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100623void mbedtls_mpi_swap( int input_X, int input_Y )
Paul Bakker367dae42009-06-28 21:50:27 +0000624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_mpi X, Y, A;
626 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
629 TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
630 TEST_ASSERT( mbedtls_mpi_lset( &A, input_X ) == 0 );
631 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
632 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
633 mbedtls_mpi_swap( &X, &Y );
634 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) != 0 );
635 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000636
Paul Bakkerbd51b262014-07-10 15:26:12 +0200637exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000639}
Paul Bakker33b43f12013-08-20 11:48:36 +0200640/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000641
Paul Bakker33b43f12013-08-20 11:48:36 +0200642/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100643void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
644 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000645{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_mpi X, Y, Z, A;
647 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
650 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
651 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
652 TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
653 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000654
Paul Bakkerbd51b262014-07-10 15:26:12 +0200655exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000657}
Paul Bakker33b43f12013-08-20 11:48:36 +0200658/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000659
Paul Bakker33b43f12013-08-20 11:48:36 +0200660/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100661void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
662 char * input_A )
Janos Follath044a86b2015-10-25 10:58:03 +0100663{
664 mbedtls_mpi X, A;
665 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
666
Janos Follath044a86b2015-10-25 10:58:03 +0100667 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Janos Follath6cbacec2015-10-25 12:29:13 +0100668
669 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
670 TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
671 TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
672
673 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
674 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
675 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
676
677 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follath044a86b2015-10-25 10:58:03 +0100678 TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
679 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
680
681exit:
682 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
683}
684/* END_CASE */
685
686
687/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100688void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
689 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_mpi X, Y, Z, A;
692 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
695 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
696 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
697 TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
698 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000699
Paul Bakkerbd51b262014-07-10 15:26:12 +0200700exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000702}
Paul Bakker33b43f12013-08-20 11:48:36 +0200703/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000704
Paul Bakker33b43f12013-08-20 11:48:36 +0200705/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100706void mpi_add_abs_add_first( int radix_X, char * input_X, int radix_Y,
707 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_mpi X, Y, A;
710 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
713 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
714 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
715 TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
716 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000717
Paul Bakkerbd51b262014-07-10 15:26:12 +0200718exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000720}
Paul Bakker33b43f12013-08-20 11:48:36 +0200721/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000722
Paul Bakker33b43f12013-08-20 11:48:36 +0200723/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100724void mpi_add_abs_add_second( int radix_X, char * input_X, int radix_Y,
725 char * input_Y, int radix_A, char * input_A )
Paul Bakkerba48cb22009-07-12 11:01:32 +0000726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_mpi X, Y, A;
728 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
731 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
732 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
733 TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
734 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000735
Paul Bakkerbd51b262014-07-10 15:26:12 +0200736exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakkerba48cb22009-07-12 11:01:32 +0000738}
Paul Bakker33b43f12013-08-20 11:48:36 +0200739/* END_CASE */
Paul Bakkerba48cb22009-07-12 11:01:32 +0000740
Paul Bakker33b43f12013-08-20 11:48:36 +0200741/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100742void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
743 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000744{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_mpi X, Z, A;
746 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
749 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
750 TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
751 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000752
Paul Bakkerbd51b262014-07-10 15:26:12 +0200753exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000755}
Paul Bakker33b43f12013-08-20 11:48:36 +0200756/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000757
Paul Bakker33b43f12013-08-20 11:48:36 +0200758/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100759void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
760 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_mpi X, Y, Z, A;
763 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
766 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
767 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
768 TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
769 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000770
Paul Bakkerbd51b262014-07-10 15:26:12 +0200771exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000773}
Paul Bakker33b43f12013-08-20 11:48:36 +0200774/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000775
Paul Bakker33b43f12013-08-20 11:48:36 +0200776/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100777void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
778 char * input_Y, int radix_A, char * input_A,
779 int sub_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000780{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000782 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
786 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
787 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200790 TEST_ASSERT( res == sub_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000791 if( res == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000793
Paul Bakkerbd51b262014-07-10 15:26:12 +0200794exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000796}
Paul Bakker33b43f12013-08-20 11:48:36 +0200797/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000798
Paul Bakker33b43f12013-08-20 11:48:36 +0200799/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100800void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
801 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000802{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_mpi X, Z, A;
804 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
807 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
808 TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
809 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000810
Paul Bakkerbd51b262014-07-10 15:26:12 +0200811exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000813}
Paul Bakker33b43f12013-08-20 11:48:36 +0200814/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000815
Paul Bakker33b43f12013-08-20 11:48:36 +0200816/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100817void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
818 char * input_Y, int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +0000819{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_mpi X, Y, Z, A;
821 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
824 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
825 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
826 TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
827 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000828
Paul Bakkerbd51b262014-07-10 15:26:12 +0200829exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000831}
Paul Bakker33b43f12013-08-20 11:48:36 +0200832/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000833
Paul Bakker33b43f12013-08-20 11:48:36 +0200834/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100835void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
836 int radix_A, char * input_A,
837 char * result_comparison )
Paul Bakker367dae42009-06-28 21:50:27 +0000838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_mpi X, Z, A;
840 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
843 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
844 TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200845 if( strcmp( result_comparison, "==" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200847 else if( strcmp( result_comparison, "!=" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200849 else
850 TEST_ASSERT( "unknown operator" == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000851
Paul Bakkerbd51b262014-07-10 15:26:12 +0200852exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000854}
Paul Bakker33b43f12013-08-20 11:48:36 +0200855/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000856
Paul Bakker33b43f12013-08-20 11:48:36 +0200857/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100858void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
859 char * input_Y, int radix_A, char * input_A,
860 int radix_B, char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000861{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_mpi X, Y, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000863 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
865 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
868 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
869 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
870 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
871 res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200872 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000873 if( res == 0 )
874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
876 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000877 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000878
Paul Bakkerbd51b262014-07-10 15:26:12 +0200879exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
881 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000882}
Paul Bakker33b43f12013-08-20 11:48:36 +0200883/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000884
Paul Bakker33b43f12013-08-20 11:48:36 +0200885/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100886void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
887 int radix_A, char * input_A, int radix_B,
888 char * input_B, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_mpi X, Q, R, A, B;
Paul Bakker367dae42009-06-28 21:50:27 +0000891 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
893 mbedtls_mpi_init( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
896 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
897 TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
898 res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200899 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000900 if( res == 0 )
901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
903 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000904 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000905
Paul Bakkerbd51b262014-07-10 15:26:12 +0200906exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
908 mbedtls_mpi_free( &B );
Paul Bakker367dae42009-06-28 21:50:27 +0000909}
Paul Bakker33b43f12013-08-20 11:48:36 +0200910/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000911
Paul Bakker33b43f12013-08-20 11:48:36 +0200912/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100913void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
914 char * input_Y, int radix_A, char * input_A,
915 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000916{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_mpi X, Y, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000918 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
922 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
923 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
924 res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200925 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000926 if( res == 0 )
927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000929 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000930
Paul Bakkerbd51b262014-07-10 15:26:12 +0200931exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000933}
Paul Bakker33b43f12013-08-20 11:48:36 +0200934/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000935
Paul Bakker33b43f12013-08-20 11:48:36 +0200936/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100937void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
938 int input_A, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000939{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +0000941 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_mpi_uint r;
943 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
946 res = mbedtls_mpi_mod_int( &r, &X, input_Y );
Paul Bakker33b43f12013-08-20 11:48:36 +0200947 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000948 if( res == 0 )
949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
Paul Bakker367dae42009-06-28 21:50:27 +0000951 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000952
Paul Bakkerbd51b262014-07-10 15:26:12 +0200953exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000955}
Paul Bakker33b43f12013-08-20 11:48:36 +0200956/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000957
Paul Bakker33b43f12013-08-20 11:48:36 +0200958/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100959void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
960 char * input_E, int radix_N, char * input_N,
961 int radix_RR, char * input_RR, int radix_X,
962 char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000963{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_mpi A, E, N, RR, Z, X;
Paul Bakker367dae42009-06-28 21:50:27 +0000965 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
967 mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
970 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
971 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
972 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000973
Paul Bakker33b43f12013-08-20 11:48:36 +0200974 if( strlen( input_RR ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
Paul Bakker33b43f12013-08-20 11:48:36 +0200978 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +0000979 if( res == 0 )
980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000982 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000983
Paul Bakkerbd51b262014-07-10 15:26:12 +0200984exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
986 mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +0000987}
Paul Bakker33b43f12013-08-20 11:48:36 +0200988/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000989
Paul Bakker33b43f12013-08-20 11:48:36 +0200990/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100991void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
992 char * input_Y, int radix_A, char * input_A,
993 int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000994{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_mpi X, Y, Z, A;
Paul Bakker367dae42009-06-28 21:50:27 +0000996 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +0000998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1000 TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
1001 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1002 res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
Paul Bakker33b43f12013-08-20 11:48:36 +02001003 TEST_ASSERT( res == div_result );
Paul Bakker367dae42009-06-28 21:50:27 +00001004 if( res == 0 )
1005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +00001007 }
Paul Bakker6c591fa2011-05-05 11:49:20 +00001008
Paul Bakkerbd51b262014-07-10 15:26:12 +02001009exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001011}
Paul Bakker33b43f12013-08-20 11:48:36 +02001012/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Azim Khanf1aaec92017-05-30 14:23:15 +01001015void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
Paul Bakker367dae42009-06-28 21:50:27 +00001016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_mpi X;
Paul Bakker367dae42009-06-28 21:50:27 +00001018 int res;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_mpi_init( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001022 res = mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL );
Paul Bakker33b43f12013-08-20 11:48:36 +02001023 TEST_ASSERT( res == div_result );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001024
Paul Bakkerbd51b262014-07-10 15:26:12 +02001025exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_mpi_free( &X );
Paul Bakker367dae42009-06-28 21:50:27 +00001027}
Paul Bakker33b43f12013-08-20 11:48:36 +02001028/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follath64eca052018-09-05 17:04:49 +01001031void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
Darryl Greenac2ead02018-10-02 15:30:39 +01001032 int chunk_len, int rounds )
Janos Follath64eca052018-09-05 17:04:49 +01001033{
1034 mbedtls_mpi X;
1035 int res;
1036 mbedtls_test_mpi_random rand;
1037
1038 mbedtls_mpi_init( &X );
1039 rand.data = witnesses;
1040 rand.pos = 0;
1041 rand.chunk_len = chunk_len;
1042
1043 TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
Darryl Greenac2ead02018-10-02 15:30:39 +01001044 res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
1045 mbedtls_test_mpi_miller_rabin_determinizer,
1046 &rand );
1047 TEST_ASSERT( res == 0 );
1048
1049 rand.data = witnesses;
1050 rand.pos = 0;
1051 rand.chunk_len = chunk_len;
1052
Janos Follatha0b67c22018-09-18 14:48:23 +01001053 res = mbedtls_mpi_is_prime_ext( &X, rounds,
1054 mbedtls_test_mpi_miller_rabin_determinizer,
Janos Follath64eca052018-09-05 17:04:49 +01001055 &rand );
Darryl Greenac2ead02018-10-02 15:30:39 +01001056 TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Janos Follath64eca052018-09-05 17:04:49 +01001057
1058exit:
1059 mbedtls_mpi_free( &X );
1060}
1061/* END_CASE */
1062
1063/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001064void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 mbedtls_mpi X;
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001067 int my_ret;
1068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_mpi_init( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001070
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001071 my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, rnd_std_rand, NULL );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001072 TEST_ASSERT( my_ret == ref_ret );
1073
1074 if( ref_ret == 0 )
1075 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001076 size_t actual_bits = mbedtls_mpi_bitlen( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001077
1078 TEST_ASSERT( actual_bits >= (size_t) bits );
1079 TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
1080
Janos Follatha0b67c22018-09-18 14:48:23 +01001081 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1082 == 0 );
Janos Follatha3cb7eb2018-08-14 15:31:54 +01001083 if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001084 {
Hanno Beckerd4d60572018-01-10 07:12:01 +00001085 /* X = ( X - 1 ) / 2 */
1086 TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
Janos Follatha0b67c22018-09-18 14:48:23 +01001087 TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, rnd_std_rand, NULL )
1088 == 0 );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001089 }
1090 }
1091
Paul Bakkerbd51b262014-07-10 15:26:12 +02001092exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 mbedtls_mpi_free( &X );
Manuel Pégourié-Gonnard15f58a82014-06-16 17:12:40 +02001094}
1095/* END_CASE */
1096
Paul Bakker33b43f12013-08-20 11:48:36 +02001097/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001098void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
1099 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 mbedtls_mpi X, A;
1102 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1105 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1106 TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
1107 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001108
Paul Bakkerbd51b262014-07-10 15:26:12 +02001109exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001111}
Paul Bakker33b43f12013-08-20 11:48:36 +02001112/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001113
Paul Bakker33b43f12013-08-20 11:48:36 +02001114/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001115void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
1116 int radix_A, char * input_A )
Paul Bakker367dae42009-06-28 21:50:27 +00001117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 mbedtls_mpi X, A;
1119 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
1122 TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
1123 TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
1124 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +00001125
Paul Bakkerbd51b262014-07-10 15:26:12 +02001126exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
Paul Bakker367dae42009-06-28 21:50:27 +00001128}
Paul Bakker33b43f12013-08-20 11:48:36 +02001129/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +00001130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001132void mpi_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +00001133{
Andres AG93012e82016-09-09 09:10:28 +01001134 TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +00001135}
Paul Bakker33b43f12013-08-20 11:48:36 +02001136/* END_CASE */