blob: e64bc8feef28dae6c262291da17d21d79ed2c07d [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/ecp.h"
Paul Bakkerdbd443d2013-08-16 13:38:47 +02003
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02004#define 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
Paul Bakker94b916c2014-04-17 16:07:20 +020017 by_id = ecp_curve_info_from_grp_id( id );
18 by_tls = ecp_curve_info_from_tls_id( tls_id );
19 by_name = ecp_curve_info_from_name( name );
20 TEST_ASSERT( by_id != NULL );
21 TEST_ASSERT( by_tls != NULL );
22 TEST_ASSERT( by_name != NULL );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010023
24 TEST_ASSERT( by_id == by_tls );
25 TEST_ASSERT( by_id == by_name );
26
27 TEST_ASSERT( by_id->size == size );
28}
29/* END_CASE */
30
31/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020032void ecp_small_add( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
33 char *y_b, int c_zero, int x_c, int y_c )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010034{
35 ecp_group grp;
36 ecp_point A, B, C;
37
38 ecp_group_init( &grp );
39 ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
40
41 TEST_ASSERT( ecp_group_read_string( &grp, 10,
42 "47", "4", "17", "42", "13" ) == 0 );
43
Paul Bakker33b43f12013-08-20 11:48:36 +020044 if( a_zero )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010045 ecp_set_zero( &A );
46 else
Paul Bakker33b43f12013-08-20 11:48:36 +020047 TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010048
Paul Bakker33b43f12013-08-20 11:48:36 +020049 if( b_zero )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010050 ecp_set_zero( &B );
51 else
Paul Bakker33b43f12013-08-20 11:48:36 +020052 TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010053
54 TEST_ASSERT( ecp_add( &grp, &C, &A, &B ) == 0 );
55
Paul Bakker33b43f12013-08-20 11:48:36 +020056 if( c_zero )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010057 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010058 else
59 {
Paul Bakker33b43f12013-08-20 11:48:36 +020060 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
61 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010062 }
63
64 TEST_ASSERT( ecp_add( &grp, &C, &B, &A ) == 0 );
65
Paul Bakker33b43f12013-08-20 11:48:36 +020066 if( c_zero )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010067 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010068 else
69 {
Paul Bakker33b43f12013-08-20 11:48:36 +020070 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
71 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010072 }
73
Paul Bakkerbd51b262014-07-10 15:26:12 +020074exit:
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010075 ecp_group_free( &grp );
76 ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
77}
Paul Bakker33b43f12013-08-20 11:48:36 +020078/* END_CASE */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010079
Paul Bakker33b43f12013-08-20 11:48:36 +020080/* BEGIN_CASE */
81void ecp_small_sub( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
82 char *y_b, int c_zero, int x_c, int y_c )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010083{
84 ecp_group grp;
85 ecp_point A, B, C;
86
87 ecp_group_init( &grp );
88 ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
89
90 TEST_ASSERT( ecp_group_read_string( &grp, 10,
91 "47", "4", "17", "42", "13" ) == 0 );
92
Paul Bakker33b43f12013-08-20 11:48:36 +020093 if( a_zero )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010094 ecp_set_zero( &A );
95 else
Paul Bakker33b43f12013-08-20 11:48:36 +020096 TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010097
Paul Bakker33b43f12013-08-20 11:48:36 +020098 if( b_zero )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010099 ecp_set_zero( &B );
100 else
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100102
103 TEST_ASSERT( ecp_sub( &grp, &C, &A, &B ) == 0 );
104
Paul Bakker33b43f12013-08-20 11:48:36 +0200105 if( c_zero )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100106 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
107 else
108 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200109 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
110 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100111 }
112
Paul Bakkerbd51b262014-07-10 15:26:12 +0200113exit:
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100114 ecp_group_free( &grp );
115 ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
116}
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* END_CASE */
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100118
Paul Bakker33b43f12013-08-20 11:48:36 +0200119/* BEGIN_CASE */
120void 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 +0100121{
122 ecp_group grp;
123 ecp_point R;
124 mpi m;
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200125 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100126
127 ecp_group_init( &grp );
128 ecp_point_init( &R );
129 mpi_init( &m );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200130 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100131
132 TEST_ASSERT( ecp_group_read_string( &grp, 10,
133 "47", "4", "17", "42", "13" ) == 0 );
134
Paul Bakker33b43f12013-08-20 11:48:36 +0200135 TEST_ASSERT( mpi_lset( &m, m_str ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100136
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200137 TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) == ret );
138
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100139 if( ret == 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200140 {
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100141 if( r_zero )
142 TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
143 else
144 {
145 TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
146 TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
147 }
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200148 }
149
150 /* try again with randomization */
151 ecp_point_free( &R );
152
153 TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G,
154 &rnd_pseudo_rand, &rnd_info ) == ret );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100155
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100156 if( ret == 0 )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100157 {
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100158 if( r_zero )
159 TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
160 else
161 {
162 TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
163 TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
164 }
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100165 }
166
Paul Bakkerbd51b262014-07-10 15:26:12 +0200167exit:
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100168 ecp_group_free( &grp );
169 ecp_point_free( &R );
170 mpi_free( &m );
171}
Paul Bakker33b43f12013-08-20 11:48:36 +0200172/* END_CASE */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100173
Paul Bakker33b43f12013-08-20 11:48:36 +0200174/* BEGIN_CASE */
175void ecp_small_check_pub( int x, int y, int z, int ret )
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100176{
177 ecp_group grp;
178 ecp_point P;
179
180 ecp_group_init( &grp );
181 ecp_point_init( &P );
182
183 TEST_ASSERT( ecp_group_read_string( &grp, 10,
184 "47", "4", "17", "42", "13" ) == 0 );
185
Paul Bakker33b43f12013-08-20 11:48:36 +0200186 TEST_ASSERT( mpi_lset( &P.X, x ) == 0 );
187 TEST_ASSERT( mpi_lset( &P.Y, y ) == 0 );
188 TEST_ASSERT( mpi_lset( &P.Z, z ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100189
Paul Bakker33b43f12013-08-20 11:48:36 +0200190 TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == ret );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100191
Paul Bakkerbd51b262014-07-10 15:26:12 +0200192exit:
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100193 ecp_group_free( &grp );
194 ecp_point_free( &P );
195}
Paul Bakker33b43f12013-08-20 11:48:36 +0200196/* END_CASE */
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100197
Paul Bakker33b43f12013-08-20 11:48:36 +0200198/* BEGIN_CASE */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100199void ecp_check_pub_mx( int grp_id, char *key_hex, int ret )
200{
201 ecp_group grp;
202 ecp_point P;
203
204 ecp_group_init( &grp );
205 ecp_point_init( &P );
206
207 TEST_ASSERT( ecp_use_known_dp( &grp, grp_id ) == 0 );
208
209 TEST_ASSERT( mpi_read_string( &P.X, 16, key_hex ) == 0 );
210 TEST_ASSERT( mpi_lset( &P.Z, 1 ) == 0 );
211
212 TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == ret );
213
Paul Bakkerbd51b262014-07-10 15:26:12 +0200214exit:
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100215 ecp_group_free( &grp );
216 ecp_point_free( &P );
217}
218/* END_CASE */
219
220/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200221void ecp_test_vect( int id, char *dA_str, char *xA_str, char *yA_str,
222 char *dB_str, char *xB_str, char *yB_str, char *xZ_str,
223 char *yZ_str )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100224{
225 ecp_group grp;
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100226 ecp_point R;
227 mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200228 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100229
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100230 ecp_group_init( &grp ); ecp_point_init( &R );
231 mpi_init( &dA ); mpi_init( &xA ); mpi_init( &yA ); mpi_init( &dB );
232 mpi_init( &xB ); mpi_init( &yB ); mpi_init( &xZ ); mpi_init( &yZ );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200233 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100234
Paul Bakker33b43f12013-08-20 11:48:36 +0200235 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100236
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100237 TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
238
Paul Bakker33b43f12013-08-20 11:48:36 +0200239 TEST_ASSERT( mpi_read_string( &dA, 16, dA_str ) == 0 );
240 TEST_ASSERT( mpi_read_string( &xA, 16, xA_str ) == 0 );
241 TEST_ASSERT( mpi_read_string( &yA, 16, yA_str ) == 0 );
242 TEST_ASSERT( mpi_read_string( &dB, 16, dB_str ) == 0 );
243 TEST_ASSERT( mpi_read_string( &xB, 16, xB_str ) == 0 );
244 TEST_ASSERT( mpi_read_string( &yB, 16, yB_str ) == 0 );
245 TEST_ASSERT( mpi_read_string( &xZ, 16, xZ_str ) == 0 );
246 TEST_ASSERT( mpi_read_string( &yZ, 16, yZ_str ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100247
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200248 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G,
249 &rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100250 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
251 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100252 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200253 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100254 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
255 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100256 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100257
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200258 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100259 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
260 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100261 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200262 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R,
263 &rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100264 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
265 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100266 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100267
Paul Bakkerbd51b262014-07-10 15:26:12 +0200268exit:
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100269 ecp_group_free( &grp ); ecp_point_free( &R );
270 mpi_free( &dA ); mpi_free( &xA ); mpi_free( &yA ); mpi_free( &dB );
271 mpi_free( &xB ); mpi_free( &yB ); mpi_free( &xZ ); mpi_free( &yZ );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100272}
Paul Bakker33b43f12013-08-20 11:48:36 +0200273/* END_CASE */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100274
Paul Bakker33b43f12013-08-20 11:48:36 +0200275/* BEGIN_CASE */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100276void ecp_test_vec_x( int id, char *dA_hex, char *xA_hex,
277 char *dB_hex, char *xB_hex, char *xS_hex )
278{
279 ecp_group grp;
280 ecp_point R;
281 mpi dA, xA, dB, xB, xS;
282 rnd_pseudo_info rnd_info;
283
284 ecp_group_init( &grp ); ecp_point_init( &R );
285 mpi_init( &dA ); mpi_init( &xA );
286 mpi_init( &dB ); mpi_init( &xB );
287 mpi_init( &xS );
288 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
289
290 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
291
292 TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
293
294 TEST_ASSERT( mpi_read_string( &dA, 16, dA_hex ) == 0 );
295 TEST_ASSERT( mpi_read_string( &dB, 16, dB_hex ) == 0 );
296 TEST_ASSERT( mpi_read_string( &xA, 16, xA_hex ) == 0 );
297 TEST_ASSERT( mpi_read_string( &xB, 16, xB_hex ) == 0 );
298 TEST_ASSERT( mpi_read_string( &xS, 16, xS_hex ) == 0 );
299
300 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G,
301 &rnd_pseudo_rand, &rnd_info ) == 0 );
302 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
303 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
304
305 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R,
306 &rnd_pseudo_rand, &rnd_info ) == 0 );
307 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
308 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xS ) == 0 );
309
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +0100310 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100311 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
312 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
313
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +0100314 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100315 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
316 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xS ) == 0 );
317
Paul Bakkerbd51b262014-07-10 15:26:12 +0200318exit:
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100319 ecp_group_free( &grp ); ecp_point_free( &R );
320 mpi_free( &dA ); mpi_free( &xA );
321 mpi_free( &dB ); mpi_free( &xB );
322 mpi_free( &xS );
323}
324/* END_CASE */
325
326/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200327void ecp_fast_mod( int id, char *N_str )
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100328{
329 ecp_group grp;
330 mpi N, R;
331
332 mpi_init( &N ); mpi_init( &R );
333 ecp_group_init( &grp );
334
Paul Bakker33b43f12013-08-20 11:48:36 +0200335 TEST_ASSERT( mpi_read_string( &N, 16, N_str ) == 0 );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200336 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
337 TEST_ASSERT( grp.modp != NULL );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100338
339 /*
340 * Store correct result before we touch N
341 */
342 TEST_ASSERT( mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
343
344 TEST_ASSERT( grp.modp( &N ) == 0 );
345 TEST_ASSERT( mpi_msb( &N ) <= grp.pbits + 3 );
346
347 /*
Paul Bakkerd8b0c5e2014-04-11 15:31:33 +0200348 * Use mod rather than addition/subtraction in case previous test fails
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100349 */
350 TEST_ASSERT( mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
351 TEST_ASSERT( mpi_cmp_mpi( &N, &R ) == 0 );
352
Paul Bakkerbd51b262014-07-10 15:26:12 +0200353exit:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100354 mpi_free( &N ); mpi_free( &R );
355 ecp_group_free( &grp );
356}
Paul Bakker33b43f12013-08-20 11:48:36 +0200357/* END_CASE */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100358
Paul Bakker33b43f12013-08-20 11:48:36 +0200359/* BEGIN_CASE */
360void ecp_write_binary( int id, char *x, char *y, char *z, int format,
361 char *out, int blen, int ret )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100362{
363 ecp_group grp;
364 ecp_point P;
365 unsigned char buf[256], str[512];
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100366 size_t olen;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100367
368 memset( buf, 0, sizeof( buf ) );
369 memset( str, 0, sizeof( str ) );
370
371 ecp_group_init( &grp ); ecp_point_init( &P );
372
Paul Bakker33b43f12013-08-20 11:48:36 +0200373 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100374
Paul Bakker33b43f12013-08-20 11:48:36 +0200375 TEST_ASSERT( mpi_read_string( &P.X, 16, x ) == 0 );
376 TEST_ASSERT( mpi_read_string( &P.Y, 16, y ) == 0 );
377 TEST_ASSERT( mpi_read_string( &P.Z, 16, z ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100378
Paul Bakker33b43f12013-08-20 11:48:36 +0200379 TEST_ASSERT( ecp_point_write_binary( &grp, &P, format,
380 &olen, buf, blen ) == ret );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100381
Paul Bakker33b43f12013-08-20 11:48:36 +0200382 if( ret == 0 )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100383 {
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100384 hexify( str, buf, olen );
Paul Bakker33b43f12013-08-20 11:48:36 +0200385 TEST_ASSERT( strcasecmp( (char *) str, out ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100386 }
387
Paul Bakkerbd51b262014-07-10 15:26:12 +0200388exit:
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100389 ecp_group_free( &grp ); ecp_point_free( &P );
390}
Paul Bakker33b43f12013-08-20 11:48:36 +0200391/* END_CASE */
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100392
Paul Bakker33b43f12013-08-20 11:48:36 +0200393/* BEGIN_CASE */
394void ecp_read_binary( int id, char *input, char *x, char *y, char *z,
395 int ret )
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100396{
397 ecp_group grp;
398 ecp_point P;
399 mpi X, Y, Z;
400 int ilen;
401 unsigned char buf[256];
402
403 memset( buf, 0, sizeof( buf ) );
404
405 ecp_group_init( &grp ); ecp_point_init( &P );
406 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
407
Paul Bakker33b43f12013-08-20 11:48:36 +0200408 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100409
Paul Bakker33b43f12013-08-20 11:48:36 +0200410 TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
411 TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
412 TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100413
Paul Bakker33b43f12013-08-20 11:48:36 +0200414 ilen = unhexify( buf, input );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100415
Paul Bakker33b43f12013-08-20 11:48:36 +0200416 TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == ret );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100417
Paul Bakker33b43f12013-08-20 11:48:36 +0200418 if( ret == 0 )
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100419 {
420 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
421 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
422 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
423 }
424
Paul Bakkerbd51b262014-07-10 15:26:12 +0200425exit:
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100426 ecp_group_free( &grp ); ecp_point_free( &P );
427 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
428}
Paul Bakker33b43f12013-08-20 11:48:36 +0200429/* END_CASE */
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100430
Paul Bakker33b43f12013-08-20 11:48:36 +0200431/* BEGIN_CASE */
432void ecp_tls_read_point( int id, char *input, char *x, char *y, char *z,
433 int ret )
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100434{
435 ecp_group grp;
436 ecp_point P;
437 mpi X, Y, Z;
438 size_t ilen;
439 unsigned char buf[256];
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100440 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100441
442 memset( buf, 0, sizeof( buf ) );
443
444 ecp_group_init( &grp ); ecp_point_init( &P );
445 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
446
Paul Bakker33b43f12013-08-20 11:48:36 +0200447 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100448
Paul Bakker33b43f12013-08-20 11:48:36 +0200449 TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
450 TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
451 TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100452
Paul Bakker33b43f12013-08-20 11:48:36 +0200453 ilen = unhexify( buf, input );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100454
Paul Bakker33b43f12013-08-20 11:48:36 +0200455 TEST_ASSERT( ecp_tls_read_point( &grp, &P, &vbuf, ilen ) == ret );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100456
Paul Bakker33b43f12013-08-20 11:48:36 +0200457 if( ret == 0 )
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100458 {
459 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
460 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
461 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100462 TEST_ASSERT( *vbuf == 0x00 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100463 }
464
Paul Bakkerbd51b262014-07-10 15:26:12 +0200465exit:
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100466 ecp_group_free( &grp ); ecp_point_free( &P );
467 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
468}
Paul Bakker33b43f12013-08-20 11:48:36 +0200469/* END_CASE */
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100470
Paul Bakker33b43f12013-08-20 11:48:36 +0200471/* BEGIN_CASE */
472void ecp_tls_write_read_point( int id )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100473{
474 ecp_group grp;
475 ecp_point pt;
476 unsigned char buf[256];
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100477 const unsigned char *vbuf;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100478 size_t olen;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100479
480 ecp_group_init( &grp );
481 ecp_point_init( &pt );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100482
Paul Bakker33b43f12013-08-20 11:48:36 +0200483 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100484
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100485 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100486 TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100487 POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100488 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen )
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100489 == POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100490 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100491
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100492 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100493 TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100494 POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100495 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100496 TEST_ASSERT( mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
497 TEST_ASSERT( mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
498 TEST_ASSERT( mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100499 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100500
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100501 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100502 TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
503 TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100504 POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100505 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100506 TEST_ASSERT( ecp_is_zero( &pt ) );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100507 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100508
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100509 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100510 TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
511 TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100512 POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100513 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100514 TEST_ASSERT( ecp_is_zero( &pt ) );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100515 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100516
Paul Bakkerbd51b262014-07-10 15:26:12 +0200517exit:
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100518 ecp_group_free( &grp );
519 ecp_point_free( &pt );
520}
Paul Bakker33b43f12013-08-20 11:48:36 +0200521/* END_CASE */
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100522
Paul Bakker33b43f12013-08-20 11:48:36 +0200523/* BEGIN_CASE */
524void ecp_tls_read_group( char *record, int result, int bits )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100525{
526 ecp_group grp;
527 unsigned char buf[10];
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100528 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100529 int len, ret;
530
531 ecp_group_init( &grp );
532 memset( buf, 0x00, sizeof( buf ) );
533
Paul Bakker33b43f12013-08-20 11:48:36 +0200534 len = unhexify( buf, record );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100535
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100536 ret = ecp_tls_read_group( &grp, &vbuf, len );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100537
Paul Bakker33b43f12013-08-20 11:48:36 +0200538 TEST_ASSERT( ret == result );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100539 if( ret == 0)
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100540 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200541 TEST_ASSERT( mpi_msb( &grp.P ) == (size_t) bits );
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100542 TEST_ASSERT( *vbuf == 0x00 );
543 }
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100544
Paul Bakkerbd51b262014-07-10 15:26:12 +0200545exit:
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100546 ecp_group_free( &grp );
547}
Paul Bakker33b43f12013-08-20 11:48:36 +0200548/* END_CASE */
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100549
Paul Bakker33b43f12013-08-20 11:48:36 +0200550/* BEGIN_CASE */
551void ecp_tls_write_read_group( int id )
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100552{
553 ecp_group grp1, grp2;
554 unsigned char buf[10];
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100555 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100556 size_t len;
557 int ret;
558
559 ecp_group_init( &grp1 );
560 ecp_group_init( &grp2 );
561 memset( buf, 0x00, sizeof( buf ) );
562
Paul Bakker33b43f12013-08-20 11:48:36 +0200563 TEST_ASSERT( ecp_use_known_dp( &grp1, id ) == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100564
565 TEST_ASSERT( ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
Paul Bakker94b916c2014-04-17 16:07:20 +0200566 ret = ecp_tls_read_group( &grp2, &vbuf, len );
567 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100568
569 if( ret == 0 )
570 {
571 TEST_ASSERT( mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
572 TEST_ASSERT( grp1.id == grp2.id );
573 }
574
Paul Bakkerbd51b262014-07-10 15:26:12 +0200575exit:
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100576 ecp_group_free( &grp1 );
577 ecp_group_free( &grp2 );
578}
Paul Bakker33b43f12013-08-20 11:48:36 +0200579/* END_CASE */
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100580
Paul Bakker33b43f12013-08-20 11:48:36 +0200581/* BEGIN_CASE */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100582void ecp_check_privkey( int id, char *key_hex, int ret )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200583{
584 ecp_group grp;
585 mpi d;
586
587 ecp_group_init( &grp );
588 mpi_init( &d );
589
Paul Bakker33b43f12013-08-20 11:48:36 +0200590 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100591 TEST_ASSERT( mpi_read_string( &d, 16, key_hex ) == 0 );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200592
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100593 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == ret );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200594
Paul Bakkerbd51b262014-07-10 15:26:12 +0200595exit:
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200596 ecp_group_free( &grp );
597 mpi_free( &d );
598}
Paul Bakker33b43f12013-08-20 11:48:36 +0200599/* END_CASE */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200600
Paul Bakker33b43f12013-08-20 11:48:36 +0200601/* BEGIN_CASE */
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100602void ecp_check_pub_priv( int id_pub, char *Qx_pub, char *Qy_pub,
603 int id, char *d, char *Qx, char *Qy, int ret )
604{
605 ecp_keypair pub, prv;
606
607 ecp_keypair_init( &pub );
608 ecp_keypair_init( &prv );
609
610 if( id_pub != POLARSSL_ECP_DP_NONE )
611 TEST_ASSERT( ecp_use_known_dp( &pub.grp, id_pub ) == 0 );
612 TEST_ASSERT( ecp_point_read_string( &pub.Q, 16, Qx_pub, Qy_pub ) == 0 );
613
614 if( id != POLARSSL_ECP_DP_NONE )
615 TEST_ASSERT( ecp_use_known_dp( &prv.grp, id ) == 0 );
616 TEST_ASSERT( ecp_point_read_string( &prv.Q, 16, Qx, Qy ) == 0 );
617 TEST_ASSERT( mpi_read_string( &prv.d, 16, d ) == 0 );
618
619 TEST_ASSERT( ecp_check_pub_priv( &pub, &prv ) == ret );
620
621exit:
622 ecp_keypair_free( &pub );
623 ecp_keypair_free( &prv );
624}
625/* END_CASE */
626
627/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200628void ecp_gen_keypair( int id )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100629{
630 ecp_group grp;
631 ecp_point Q;
632 mpi d;
633 rnd_pseudo_info rnd_info;
634
635 ecp_group_init( &grp );
636 ecp_point_init( &Q );
637 mpi_init( &d );
638 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
639
Paul Bakker33b43f12013-08-20 11:48:36 +0200640 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100641
642 TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
643 == 0 );
644
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200645 TEST_ASSERT( ecp_check_pubkey( &grp, &Q ) == 0 );
Paul Bakker8ea6c612013-07-16 17:15:03 +0200646 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100647
Paul Bakkerbd51b262014-07-10 15:26:12 +0200648exit:
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100649 ecp_group_free( &grp );
650 ecp_point_free( &Q );
651 mpi_free( &d );
652}
Paul Bakker33b43f12013-08-20 11:48:36 +0200653/* END_CASE */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100654
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100655/* BEGIN_CASE */
656void ecp_gen_key( int id )
657{
658 ecp_keypair key;
659 rnd_pseudo_info rnd_info;
660
661 ecp_keypair_init( &key );
662 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
663
664 TEST_ASSERT( ecp_gen_key( id, &key, &rnd_pseudo_rand, &rnd_info ) == 0 );
665
666 TEST_ASSERT( ecp_check_pubkey( &key.grp, &key.Q ) == 0 );
667 TEST_ASSERT( ecp_check_privkey( &key.grp, &key.d ) == 0 );
668
Paul Bakkerbd51b262014-07-10 15:26:12 +0200669exit:
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100670 ecp_keypair_free( &key );
671}
672/* END_CASE */
673
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200674/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200675void ecp_selftest()
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100676{
677 TEST_ASSERT( ecp_self_test( 0 ) == 0 );
678}
Paul Bakker33b43f12013-08-20 11:48:36 +0200679/* END_CASE */