blob: cb93e850a28bbc86de9b5f6d33b12615e7abca26 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +01002#include <polarssl/ecp.h>
Paul Bakkerdbd443d2013-08-16 13:38:47 +02003
4#define POLARSSL_ECP_PF_UNKNOWN -1
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* END_HEADER */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +01006
Paul Bakker33b43f12013-08-20 11:48:36 +02007/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnarda0f07472013-08-23 16:35:32 +02008 * depends_on:POLARSSL_ECP_C
Paul Bakker33b43f12013-08-20 11:48:36 +02009 * END_DEPENDENCIES
10 */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010011
Paul Bakker33b43f12013-08-20 11:48:36 +020012/* BEGIN_CASE */
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010013void ecp_curve_info( int id, int tls_id, int size, char *name )
14{
15 const ecp_curve_info *by_id, *by_tls, *by_name;
16
17 TEST_ASSERT( ( by_id = ecp_curve_info_from_grp_id( id ) ) != NULL );
18 TEST_ASSERT( ( by_tls = ecp_curve_info_from_tls_id( tls_id ) ) != NULL );
19 TEST_ASSERT( ( by_name = ecp_curve_info_from_name( name ) ) != NULL );
20
21 TEST_ASSERT( by_id == by_tls );
22 TEST_ASSERT( by_id == by_name );
23
24 TEST_ASSERT( by_id->size == size );
25}
26/* END_CASE */
27
28/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020029void ecp_small_add( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
30 char *y_b, int c_zero, int x_c, int y_c )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010031{
32 ecp_group grp;
33 ecp_point A, B, C;
34
35 ecp_group_init( &grp );
36 ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
37
38 TEST_ASSERT( ecp_group_read_string( &grp, 10,
39 "47", "4", "17", "42", "13" ) == 0 );
40
Paul Bakker33b43f12013-08-20 11:48:36 +020041 if( a_zero )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010042 ecp_set_zero( &A );
43 else
Paul Bakker33b43f12013-08-20 11:48:36 +020044 TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010045
Paul Bakker33b43f12013-08-20 11:48:36 +020046 if( b_zero )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010047 ecp_set_zero( &B );
48 else
Paul Bakker33b43f12013-08-20 11:48:36 +020049 TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010050
51 TEST_ASSERT( ecp_add( &grp, &C, &A, &B ) == 0 );
52
Paul Bakker33b43f12013-08-20 11:48:36 +020053 if( c_zero )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010054 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010055 else
56 {
Paul Bakker33b43f12013-08-20 11:48:36 +020057 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
58 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010059 }
60
61 TEST_ASSERT( ecp_add( &grp, &C, &B, &A ) == 0 );
62
Paul Bakker33b43f12013-08-20 11:48:36 +020063 if( c_zero )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010064 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010065 else
66 {
Paul Bakker33b43f12013-08-20 11:48:36 +020067 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
68 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010069 }
70
71 ecp_group_free( &grp );
72 ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
73}
Paul Bakker33b43f12013-08-20 11:48:36 +020074/* END_CASE */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010075
Paul Bakker33b43f12013-08-20 11:48:36 +020076/* BEGIN_CASE */
77void ecp_small_sub( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
78 char *y_b, int c_zero, int x_c, int y_c )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010079{
80 ecp_group grp;
81 ecp_point A, B, C;
82
83 ecp_group_init( &grp );
84 ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
85
86 TEST_ASSERT( ecp_group_read_string( &grp, 10,
87 "47", "4", "17", "42", "13" ) == 0 );
88
Paul Bakker33b43f12013-08-20 11:48:36 +020089 if( a_zero )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010090 ecp_set_zero( &A );
91 else
Paul Bakker33b43f12013-08-20 11:48:36 +020092 TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010093
Paul Bakker33b43f12013-08-20 11:48:36 +020094 if( b_zero )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010095 ecp_set_zero( &B );
96 else
Paul Bakker33b43f12013-08-20 11:48:36 +020097 TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010098
99 TEST_ASSERT( ecp_sub( &grp, &C, &A, &B ) == 0 );
100
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 if( c_zero )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100102 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
103 else
104 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200105 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
106 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100107 }
108
109 ecp_group_free( &grp );
110 ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
111}
Paul Bakker33b43f12013-08-20 11:48:36 +0200112/* END_CASE */
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100113
Paul Bakker33b43f12013-08-20 11:48:36 +0200114/* BEGIN_CASE */
115void ecp_small_mul( int m_str, int r_zero, int x_r, int y_r, int ret )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100116{
117 ecp_group grp;
118 ecp_point R;
119 mpi m;
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200120 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100121
122 ecp_group_init( &grp );
123 ecp_point_init( &R );
124 mpi_init( &m );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200125 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100126
127 TEST_ASSERT( ecp_group_read_string( &grp, 10,
128 "47", "4", "17", "42", "13" ) == 0 );
129
Paul Bakker33b43f12013-08-20 11:48:36 +0200130 TEST_ASSERT( mpi_lset( &m, m_str ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100131
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200132 TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) == ret );
133
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100134 if( ret == 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200135 {
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100136 if( r_zero )
137 TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
138 else
139 {
140 TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
141 TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
142 }
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200143 }
144
145 /* try again with randomization */
146 ecp_point_free( &R );
147
148 TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G,
149 &rnd_pseudo_rand, &rnd_info ) == ret );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100150
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100151 if( ret == 0 )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100152 {
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100153 if( r_zero )
154 TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
155 else
156 {
157 TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
158 TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
159 }
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100160 }
161
162 ecp_group_free( &grp );
163 ecp_point_free( &R );
164 mpi_free( &m );
165}
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* END_CASE */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100167
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* BEGIN_CASE */
169void ecp_small_check_pub( int x, int y, int z, int ret )
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100170{
171 ecp_group grp;
172 ecp_point P;
173
174 ecp_group_init( &grp );
175 ecp_point_init( &P );
176
177 TEST_ASSERT( ecp_group_read_string( &grp, 10,
178 "47", "4", "17", "42", "13" ) == 0 );
179
Paul Bakker33b43f12013-08-20 11:48:36 +0200180 TEST_ASSERT( mpi_lset( &P.X, x ) == 0 );
181 TEST_ASSERT( mpi_lset( &P.Y, y ) == 0 );
182 TEST_ASSERT( mpi_lset( &P.Z, z ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100183
Paul Bakker33b43f12013-08-20 11:48:36 +0200184 TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == ret );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100185
186 ecp_group_free( &grp );
187 ecp_point_free( &P );
188}
Paul Bakker33b43f12013-08-20 11:48:36 +0200189/* END_CASE */
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100190
Paul Bakker33b43f12013-08-20 11:48:36 +0200191/* BEGIN_CASE */
192void ecp_test_vect( int id, char *dA_str, char *xA_str, char *yA_str,
193 char *dB_str, char *xB_str, char *yB_str, char *xZ_str,
194 char *yZ_str )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100195{
196 ecp_group grp;
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100197 ecp_point R;
198 mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200199 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100200
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100201 ecp_group_init( &grp ); ecp_point_init( &R );
202 mpi_init( &dA ); mpi_init( &xA ); mpi_init( &yA ); mpi_init( &dB );
203 mpi_init( &xB ); mpi_init( &yB ); mpi_init( &xZ ); mpi_init( &yZ );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200204 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100205
Paul Bakker33b43f12013-08-20 11:48:36 +0200206 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100207
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100208 TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
209
Paul Bakker33b43f12013-08-20 11:48:36 +0200210 TEST_ASSERT( mpi_read_string( &dA, 16, dA_str ) == 0 );
211 TEST_ASSERT( mpi_read_string( &xA, 16, xA_str ) == 0 );
212 TEST_ASSERT( mpi_read_string( &yA, 16, yA_str ) == 0 );
213 TEST_ASSERT( mpi_read_string( &dB, 16, dB_str ) == 0 );
214 TEST_ASSERT( mpi_read_string( &xB, 16, xB_str ) == 0 );
215 TEST_ASSERT( mpi_read_string( &yB, 16, yB_str ) == 0 );
216 TEST_ASSERT( mpi_read_string( &xZ, 16, xZ_str ) == 0 );
217 TEST_ASSERT( mpi_read_string( &yZ, 16, yZ_str ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100218
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200219 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G,
220 &rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100221 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
222 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100223 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200224 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100225 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
226 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100227 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100228
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200229 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100230 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
231 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100232 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200233 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R,
234 &rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100235 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
236 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100237 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100238
239 ecp_group_free( &grp ); ecp_point_free( &R );
240 mpi_free( &dA ); mpi_free( &xA ); mpi_free( &yA ); mpi_free( &dB );
241 mpi_free( &xB ); mpi_free( &yB ); mpi_free( &xZ ); mpi_free( &yZ );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100242}
Paul Bakker33b43f12013-08-20 11:48:36 +0200243/* END_CASE */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100244
Paul Bakker33b43f12013-08-20 11:48:36 +0200245/* BEGIN_CASE */
246void ecp_fast_mod( int id, char *N_str )
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100247{
248 ecp_group grp;
249 mpi N, R;
250
251 mpi_init( &N ); mpi_init( &R );
252 ecp_group_init( &grp );
253
Paul Bakker33b43f12013-08-20 11:48:36 +0200254 TEST_ASSERT( mpi_read_string( &N, 16, N_str ) == 0 );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200255 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
256 TEST_ASSERT( grp.modp != NULL );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100257
258 /*
259 * Store correct result before we touch N
260 */
261 TEST_ASSERT( mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
262
263 TEST_ASSERT( grp.modp( &N ) == 0 );
264 TEST_ASSERT( mpi_msb( &N ) <= grp.pbits + 3 );
265
266 /*
267 * Use mod rather than addition/substraction in case previous test fails
268 */
269 TEST_ASSERT( mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
270 TEST_ASSERT( mpi_cmp_mpi( &N, &R ) == 0 );
271
272 mpi_free( &N ); mpi_free( &R );
273 ecp_group_free( &grp );
274}
Paul Bakker33b43f12013-08-20 11:48:36 +0200275/* END_CASE */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100276
Paul Bakker33b43f12013-08-20 11:48:36 +0200277/* BEGIN_CASE */
278void ecp_write_binary( int id, char *x, char *y, char *z, int format,
279 char *out, int blen, int ret )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100280{
281 ecp_group grp;
282 ecp_point P;
283 unsigned char buf[256], str[512];
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100284 size_t olen;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100285
286 memset( buf, 0, sizeof( buf ) );
287 memset( str, 0, sizeof( str ) );
288
289 ecp_group_init( &grp ); ecp_point_init( &P );
290
Paul Bakker33b43f12013-08-20 11:48:36 +0200291 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100292
Paul Bakker33b43f12013-08-20 11:48:36 +0200293 TEST_ASSERT( mpi_read_string( &P.X, 16, x ) == 0 );
294 TEST_ASSERT( mpi_read_string( &P.Y, 16, y ) == 0 );
295 TEST_ASSERT( mpi_read_string( &P.Z, 16, z ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100296
Paul Bakker33b43f12013-08-20 11:48:36 +0200297 TEST_ASSERT( ecp_point_write_binary( &grp, &P, format,
298 &olen, buf, blen ) == ret );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100299
Paul Bakker33b43f12013-08-20 11:48:36 +0200300 if( ret == 0 )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100301 {
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100302 hexify( str, buf, olen );
Paul Bakker33b43f12013-08-20 11:48:36 +0200303 TEST_ASSERT( strcasecmp( (char *) str, out ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100304 }
305
306 ecp_group_free( &grp ); ecp_point_free( &P );
307}
Paul Bakker33b43f12013-08-20 11:48:36 +0200308/* END_CASE */
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100309
Paul Bakker33b43f12013-08-20 11:48:36 +0200310/* BEGIN_CASE */
311void ecp_read_binary( int id, char *input, char *x, char *y, char *z,
312 int ret )
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100313{
314 ecp_group grp;
315 ecp_point P;
316 mpi X, Y, Z;
317 int ilen;
318 unsigned char buf[256];
319
320 memset( buf, 0, sizeof( buf ) );
321
322 ecp_group_init( &grp ); ecp_point_init( &P );
323 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
324
Paul Bakker33b43f12013-08-20 11:48:36 +0200325 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100326
Paul Bakker33b43f12013-08-20 11:48:36 +0200327 TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
328 TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
329 TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100330
Paul Bakker33b43f12013-08-20 11:48:36 +0200331 ilen = unhexify( buf, input );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100332
Paul Bakker33b43f12013-08-20 11:48:36 +0200333 TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == ret );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100334
Paul Bakker33b43f12013-08-20 11:48:36 +0200335 if( ret == 0 )
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100336 {
337 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
338 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
339 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
340 }
341
342 ecp_group_free( &grp ); ecp_point_free( &P );
343 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
344}
Paul Bakker33b43f12013-08-20 11:48:36 +0200345/* END_CASE */
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100346
Paul Bakker33b43f12013-08-20 11:48:36 +0200347/* BEGIN_CASE */
348void ecp_tls_read_point( int id, char *input, char *x, char *y, char *z,
349 int ret )
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100350{
351 ecp_group grp;
352 ecp_point P;
353 mpi X, Y, Z;
354 size_t ilen;
355 unsigned char buf[256];
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100356 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100357
358 memset( buf, 0, sizeof( buf ) );
359
360 ecp_group_init( &grp ); ecp_point_init( &P );
361 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
362
Paul Bakker33b43f12013-08-20 11:48:36 +0200363 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100364
Paul Bakker33b43f12013-08-20 11:48:36 +0200365 TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
366 TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
367 TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100368
Paul Bakker33b43f12013-08-20 11:48:36 +0200369 ilen = unhexify( buf, input );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100370
Paul Bakker33b43f12013-08-20 11:48:36 +0200371 TEST_ASSERT( ecp_tls_read_point( &grp, &P, &vbuf, ilen ) == ret );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100372
Paul Bakker33b43f12013-08-20 11:48:36 +0200373 if( ret == 0 )
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100374 {
375 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
376 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
377 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100378 TEST_ASSERT( *vbuf == 0x00 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100379 }
380
381 ecp_group_free( &grp ); ecp_point_free( &P );
382 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
383}
Paul Bakker33b43f12013-08-20 11:48:36 +0200384/* END_CASE */
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100385
Paul Bakker33b43f12013-08-20 11:48:36 +0200386/* BEGIN_CASE */
387void ecp_tls_write_read_point( int id )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100388{
389 ecp_group grp;
390 ecp_point pt;
391 unsigned char buf[256];
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100392 const unsigned char *vbuf;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100393 size_t olen;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100394
395 ecp_group_init( &grp );
396 ecp_point_init( &pt );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100397
Paul Bakker33b43f12013-08-20 11:48:36 +0200398 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100399
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100400 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100401 TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100402 POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100403 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100404 == POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100405 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100406
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100407 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100408 TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100409 POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100410 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100411 TEST_ASSERT( mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
412 TEST_ASSERT( mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
413 TEST_ASSERT( mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100414 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100415
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100416 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100417 TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
418 TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100419 POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100420 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100421 TEST_ASSERT( ecp_is_zero( &pt ) );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100422 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100423
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100424 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100425 TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
426 TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100427 POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100428 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100429 TEST_ASSERT( ecp_is_zero( &pt ) );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100430 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100431
432 ecp_group_free( &grp );
433 ecp_point_free( &pt );
434}
Paul Bakker33b43f12013-08-20 11:48:36 +0200435/* END_CASE */
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100436
Paul Bakker33b43f12013-08-20 11:48:36 +0200437/* BEGIN_CASE */
438void ecp_tls_read_group( char *record, int result, int bits )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100439{
440 ecp_group grp;
441 unsigned char buf[10];
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100442 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100443 int len, ret;
444
445 ecp_group_init( &grp );
446 memset( buf, 0x00, sizeof( buf ) );
447
Paul Bakker33b43f12013-08-20 11:48:36 +0200448 len = unhexify( buf, record );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100449
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100450 ret = ecp_tls_read_group( &grp, &vbuf, len );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100451
Paul Bakker33b43f12013-08-20 11:48:36 +0200452 TEST_ASSERT( ret == result );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100453 if( ret == 0)
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100454 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200455 TEST_ASSERT( mpi_msb( &grp.P ) == (size_t) bits );
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100456 TEST_ASSERT( *vbuf == 0x00 );
457 }
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100458
459 ecp_group_free( &grp );
460}
Paul Bakker33b43f12013-08-20 11:48:36 +0200461/* END_CASE */
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100462
Paul Bakker33b43f12013-08-20 11:48:36 +0200463/* BEGIN_CASE */
464void ecp_tls_write_read_group( int id )
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100465{
466 ecp_group grp1, grp2;
467 unsigned char buf[10];
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100468 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100469 size_t len;
470 int ret;
471
472 ecp_group_init( &grp1 );
473 ecp_group_init( &grp2 );
474 memset( buf, 0x00, sizeof( buf ) );
475
Paul Bakker33b43f12013-08-20 11:48:36 +0200476 TEST_ASSERT( ecp_use_known_dp( &grp1, id ) == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100477
478 TEST_ASSERT( ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100479 TEST_ASSERT( ( ret = ecp_tls_read_group( &grp2, &vbuf, len ) ) == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100480
481 if( ret == 0 )
482 {
483 TEST_ASSERT( mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
484 TEST_ASSERT( grp1.id == grp2.id );
485 }
486
487 ecp_group_free( &grp1 );
488 ecp_group_free( &grp2 );
489}
Paul Bakker33b43f12013-08-20 11:48:36 +0200490/* END_CASE */
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100491
Paul Bakker33b43f12013-08-20 11:48:36 +0200492/* BEGIN_CASE */
493void ecp_check_privkey( int id )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200494{
495 ecp_group grp;
496 mpi d;
497
498 ecp_group_init( &grp );
499 mpi_init( &d );
500
Paul Bakker33b43f12013-08-20 11:48:36 +0200501 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200502
503 TEST_ASSERT( mpi_lset( &d, 0 ) == 0 );
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200504 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200505
506 TEST_ASSERT( mpi_copy( &d, &grp.N ) == 0 );
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200507 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200508
509 ecp_group_free( &grp );
510 mpi_free( &d );
511}
Paul Bakker33b43f12013-08-20 11:48:36 +0200512/* END_CASE */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200513
Paul Bakker33b43f12013-08-20 11:48:36 +0200514/* BEGIN_CASE */
515void ecp_gen_keypair( int id )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100516{
517 ecp_group grp;
518 ecp_point Q;
519 mpi d;
520 rnd_pseudo_info rnd_info;
521
522 ecp_group_init( &grp );
523 ecp_point_init( &Q );
524 mpi_init( &d );
525 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
526
Paul Bakker33b43f12013-08-20 11:48:36 +0200527 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100528
529 TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
530 == 0 );
531
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200532 TEST_ASSERT( ecp_check_pubkey( &grp, &Q ) == 0 );
Paul Bakker8ea6c612013-07-16 17:15:03 +0200533 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100534
535 ecp_group_free( &grp );
536 ecp_point_free( &Q );
537 mpi_free( &d );
538}
Paul Bakker33b43f12013-08-20 11:48:36 +0200539/* END_CASE */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100540
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100541/* BEGIN_CASE */
542void ecp_gen_key( int id )
543{
544 ecp_keypair key;
545 rnd_pseudo_info rnd_info;
546
547 ecp_keypair_init( &key );
548 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
549
550 TEST_ASSERT( ecp_gen_key( id, &key, &rnd_pseudo_rand, &rnd_info ) == 0 );
551
552 TEST_ASSERT( ecp_check_pubkey( &key.grp, &key.Q ) == 0 );
553 TEST_ASSERT( ecp_check_privkey( &key.grp, &key.d ) == 0 );
554
555 ecp_keypair_free( &key );
556}
557/* END_CASE */
558
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200559/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200560void ecp_selftest()
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100561{
562 TEST_ASSERT( ecp_self_test( 0 ) == 0 );
563}
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* END_CASE */