blob: edb4b61d647696974cf257010552b1fd5aae1e12 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Rich Evansce2f2372015-02-06 13:57:42 +00002#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
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 */
Janos Follath0990a8b2017-01-27 15:51:14 +0000199void ecp_check_pub( int grp_id, char *x_hex, char *y_hex, char *z_hex, int ret )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100200{
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
Janos Follath0990a8b2017-01-27 15:51:14 +0000209 TEST_ASSERT( mpi_read_string( &P.X, 16, x_hex ) == 0 );
210 TEST_ASSERT( mpi_read_string( &P.Y, 16, y_hex ) == 0 );
211 TEST_ASSERT( mpi_read_string( &P.Z, 16, z_hex ) == 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100212
213 TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == ret );
214
Paul Bakkerbd51b262014-07-10 15:26:12 +0200215exit:
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100216 ecp_group_free( &grp );
217 ecp_point_free( &P );
218}
219/* END_CASE */
220
221/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200222void ecp_test_vect( int id, char *dA_str, char *xA_str, char *yA_str,
223 char *dB_str, char *xB_str, char *yB_str, char *xZ_str,
224 char *yZ_str )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100225{
226 ecp_group grp;
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100227 ecp_point R;
228 mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200229 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100230
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100231 ecp_group_init( &grp ); ecp_point_init( &R );
232 mpi_init( &dA ); mpi_init( &xA ); mpi_init( &yA ); mpi_init( &dB );
233 mpi_init( &xB ); mpi_init( &yB ); mpi_init( &xZ ); mpi_init( &yZ );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200234 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100235
Paul Bakker33b43f12013-08-20 11:48:36 +0200236 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100237
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100238 TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
239
Paul Bakker33b43f12013-08-20 11:48:36 +0200240 TEST_ASSERT( mpi_read_string( &dA, 16, dA_str ) == 0 );
241 TEST_ASSERT( mpi_read_string( &xA, 16, xA_str ) == 0 );
242 TEST_ASSERT( mpi_read_string( &yA, 16, yA_str ) == 0 );
243 TEST_ASSERT( mpi_read_string( &dB, 16, dB_str ) == 0 );
244 TEST_ASSERT( mpi_read_string( &xB, 16, xB_str ) == 0 );
245 TEST_ASSERT( mpi_read_string( &yB, 16, yB_str ) == 0 );
246 TEST_ASSERT( mpi_read_string( &xZ, 16, xZ_str ) == 0 );
247 TEST_ASSERT( mpi_read_string( &yZ, 16, yZ_str ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100248
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200249 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G,
250 &rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100251 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
252 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100253 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200254 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100255 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
256 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100257 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100258
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200259 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100260 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
261 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100262 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200263 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R,
264 &rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100265 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
266 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100267 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100268
Paul Bakkerbd51b262014-07-10 15:26:12 +0200269exit:
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100270 ecp_group_free( &grp ); ecp_point_free( &R );
271 mpi_free( &dA ); mpi_free( &xA ); mpi_free( &yA ); mpi_free( &dB );
272 mpi_free( &xB ); mpi_free( &yB ); mpi_free( &xZ ); mpi_free( &yZ );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100273}
Paul Bakker33b43f12013-08-20 11:48:36 +0200274/* END_CASE */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100275
Paul Bakker33b43f12013-08-20 11:48:36 +0200276/* BEGIN_CASE */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100277void ecp_test_vec_x( int id, char *dA_hex, char *xA_hex,
278 char *dB_hex, char *xB_hex, char *xS_hex )
279{
280 ecp_group grp;
281 ecp_point R;
282 mpi dA, xA, dB, xB, xS;
283 rnd_pseudo_info rnd_info;
284
285 ecp_group_init( &grp ); ecp_point_init( &R );
286 mpi_init( &dA ); mpi_init( &xA );
287 mpi_init( &dB ); mpi_init( &xB );
288 mpi_init( &xS );
289 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
290
291 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
292
293 TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
294
295 TEST_ASSERT( mpi_read_string( &dA, 16, dA_hex ) == 0 );
296 TEST_ASSERT( mpi_read_string( &dB, 16, dB_hex ) == 0 );
297 TEST_ASSERT( mpi_read_string( &xA, 16, xA_hex ) == 0 );
298 TEST_ASSERT( mpi_read_string( &xB, 16, xB_hex ) == 0 );
299 TEST_ASSERT( mpi_read_string( &xS, 16, xS_hex ) == 0 );
300
301 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G,
302 &rnd_pseudo_rand, &rnd_info ) == 0 );
303 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
304 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
305
306 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R,
307 &rnd_pseudo_rand, &rnd_info ) == 0 );
308 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
309 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xS ) == 0 );
310
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +0100311 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100312 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
313 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
314
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +0100315 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100316 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
317 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xS ) == 0 );
318
Paul Bakkerbd51b262014-07-10 15:26:12 +0200319exit:
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100320 ecp_group_free( &grp ); ecp_point_free( &R );
321 mpi_free( &dA ); mpi_free( &xA );
322 mpi_free( &dB ); mpi_free( &xB );
323 mpi_free( &xS );
324}
325/* END_CASE */
326
327/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200328void ecp_fast_mod( int id, char *N_str )
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100329{
330 ecp_group grp;
331 mpi N, R;
332
333 mpi_init( &N ); mpi_init( &R );
334 ecp_group_init( &grp );
335
Paul Bakker33b43f12013-08-20 11:48:36 +0200336 TEST_ASSERT( mpi_read_string( &N, 16, N_str ) == 0 );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200337 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
338 TEST_ASSERT( grp.modp != NULL );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100339
340 /*
341 * Store correct result before we touch N
342 */
343 TEST_ASSERT( mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
344
345 TEST_ASSERT( grp.modp( &N ) == 0 );
346 TEST_ASSERT( mpi_msb( &N ) <= grp.pbits + 3 );
347
348 /*
Paul Bakkerd8b0c5e2014-04-11 15:31:33 +0200349 * Use mod rather than addition/subtraction in case previous test fails
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100350 */
351 TEST_ASSERT( mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
352 TEST_ASSERT( mpi_cmp_mpi( &N, &R ) == 0 );
353
Paul Bakkerbd51b262014-07-10 15:26:12 +0200354exit:
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100355 mpi_free( &N ); mpi_free( &R );
356 ecp_group_free( &grp );
357}
Paul Bakker33b43f12013-08-20 11:48:36 +0200358/* END_CASE */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100359
Paul Bakker33b43f12013-08-20 11:48:36 +0200360/* BEGIN_CASE */
361void ecp_write_binary( int id, char *x, char *y, char *z, int format,
362 char *out, int blen, int ret )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100363{
364 ecp_group grp;
365 ecp_point P;
366 unsigned char buf[256], str[512];
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100367 size_t olen;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100368
369 memset( buf, 0, sizeof( buf ) );
370 memset( str, 0, sizeof( str ) );
371
372 ecp_group_init( &grp ); ecp_point_init( &P );
373
Paul Bakker33b43f12013-08-20 11:48:36 +0200374 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100375
Paul Bakker33b43f12013-08-20 11:48:36 +0200376 TEST_ASSERT( mpi_read_string( &P.X, 16, x ) == 0 );
377 TEST_ASSERT( mpi_read_string( &P.Y, 16, y ) == 0 );
378 TEST_ASSERT( mpi_read_string( &P.Z, 16, z ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100379
Paul Bakker33b43f12013-08-20 11:48:36 +0200380 TEST_ASSERT( ecp_point_write_binary( &grp, &P, format,
381 &olen, buf, blen ) == ret );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100382
Paul Bakker33b43f12013-08-20 11:48:36 +0200383 if( ret == 0 )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100384 {
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100385 hexify( str, buf, olen );
Paul Bakker33b43f12013-08-20 11:48:36 +0200386 TEST_ASSERT( strcasecmp( (char *) str, out ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100387 }
388
Paul Bakkerbd51b262014-07-10 15:26:12 +0200389exit:
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100390 ecp_group_free( &grp ); ecp_point_free( &P );
391}
Paul Bakker33b43f12013-08-20 11:48:36 +0200392/* END_CASE */
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100393
Paul Bakker33b43f12013-08-20 11:48:36 +0200394/* BEGIN_CASE */
395void ecp_read_binary( int id, char *input, char *x, char *y, char *z,
396 int ret )
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100397{
398 ecp_group grp;
399 ecp_point P;
400 mpi X, Y, Z;
401 int ilen;
402 unsigned char buf[256];
403
404 memset( buf, 0, sizeof( buf ) );
405
406 ecp_group_init( &grp ); ecp_point_init( &P );
407 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
408
Paul Bakker33b43f12013-08-20 11:48:36 +0200409 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100410
Paul Bakker33b43f12013-08-20 11:48:36 +0200411 TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
412 TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
413 TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100414
Paul Bakker33b43f12013-08-20 11:48:36 +0200415 ilen = unhexify( buf, input );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100416
Paul Bakker33b43f12013-08-20 11:48:36 +0200417 TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == ret );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100418
Paul Bakker33b43f12013-08-20 11:48:36 +0200419 if( ret == 0 )
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100420 {
421 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
422 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
423 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
424 }
425
Paul Bakkerbd51b262014-07-10 15:26:12 +0200426exit:
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100427 ecp_group_free( &grp ); ecp_point_free( &P );
428 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
429}
Paul Bakker33b43f12013-08-20 11:48:36 +0200430/* END_CASE */
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100431
Paul Bakker33b43f12013-08-20 11:48:36 +0200432/* BEGIN_CASE */
433void ecp_tls_read_point( int id, char *input, char *x, char *y, char *z,
434 int ret )
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100435{
436 ecp_group grp;
437 ecp_point P;
438 mpi X, Y, Z;
439 size_t ilen;
440 unsigned char buf[256];
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100441 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100442
443 memset( buf, 0, sizeof( buf ) );
444
445 ecp_group_init( &grp ); ecp_point_init( &P );
446 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
447
Paul Bakker33b43f12013-08-20 11:48:36 +0200448 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100449
Paul Bakker33b43f12013-08-20 11:48:36 +0200450 TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
451 TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
452 TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100453
Paul Bakker33b43f12013-08-20 11:48:36 +0200454 ilen = unhexify( buf, input );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100455
Paul Bakker33b43f12013-08-20 11:48:36 +0200456 TEST_ASSERT( ecp_tls_read_point( &grp, &P, &vbuf, ilen ) == ret );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100457
Paul Bakker33b43f12013-08-20 11:48:36 +0200458 if( ret == 0 )
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100459 {
460 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
461 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
462 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100463 TEST_ASSERT( *vbuf == 0x00 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100464 }
465
Paul Bakkerbd51b262014-07-10 15:26:12 +0200466exit:
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100467 ecp_group_free( &grp ); ecp_point_free( &P );
468 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
469}
Paul Bakker33b43f12013-08-20 11:48:36 +0200470/* END_CASE */
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100471
Paul Bakker33b43f12013-08-20 11:48:36 +0200472/* BEGIN_CASE */
473void ecp_tls_write_read_point( int id )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100474{
475 ecp_group grp;
476 ecp_point pt;
477 unsigned char buf[256];
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100478 const unsigned char *vbuf;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100479 size_t olen;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100480
481 ecp_group_init( &grp );
482 ecp_point_init( &pt );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100483
Paul Bakker33b43f12013-08-20 11:48:36 +0200484 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100485
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100486 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100487 TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100488 POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100489 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen )
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100490 == POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100491 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100492
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100493 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100494 TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100495 POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100496 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100497 TEST_ASSERT( mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
498 TEST_ASSERT( mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
499 TEST_ASSERT( mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100500 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100501
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100502 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100503 TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
504 TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100505 POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100506 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100507 TEST_ASSERT( ecp_is_zero( &pt ) );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100508 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100509
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100510 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100511 TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
512 TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100513 POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100514 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100515 TEST_ASSERT( ecp_is_zero( &pt ) );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100516 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100517
Paul Bakkerbd51b262014-07-10 15:26:12 +0200518exit:
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100519 ecp_group_free( &grp );
520 ecp_point_free( &pt );
521}
Paul Bakker33b43f12013-08-20 11:48:36 +0200522/* END_CASE */
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100523
Paul Bakker33b43f12013-08-20 11:48:36 +0200524/* BEGIN_CASE */
525void ecp_tls_read_group( char *record, int result, int bits )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100526{
527 ecp_group grp;
528 unsigned char buf[10];
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100529 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100530 int len, ret;
531
532 ecp_group_init( &grp );
533 memset( buf, 0x00, sizeof( buf ) );
534
Paul Bakker33b43f12013-08-20 11:48:36 +0200535 len = unhexify( buf, record );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100536
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100537 ret = ecp_tls_read_group( &grp, &vbuf, len );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100538
Paul Bakker33b43f12013-08-20 11:48:36 +0200539 TEST_ASSERT( ret == result );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100540 if( ret == 0)
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100541 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200542 TEST_ASSERT( mpi_msb( &grp.P ) == (size_t) bits );
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100543 TEST_ASSERT( *vbuf == 0x00 );
544 }
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100545
Paul Bakkerbd51b262014-07-10 15:26:12 +0200546exit:
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100547 ecp_group_free( &grp );
548}
Paul Bakker33b43f12013-08-20 11:48:36 +0200549/* END_CASE */
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100550
Paul Bakker33b43f12013-08-20 11:48:36 +0200551/* BEGIN_CASE */
552void ecp_tls_write_read_group( int id )
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100553{
554 ecp_group grp1, grp2;
555 unsigned char buf[10];
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100556 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100557 size_t len;
558 int ret;
559
560 ecp_group_init( &grp1 );
561 ecp_group_init( &grp2 );
562 memset( buf, 0x00, sizeof( buf ) );
563
Paul Bakker33b43f12013-08-20 11:48:36 +0200564 TEST_ASSERT( ecp_use_known_dp( &grp1, id ) == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100565
566 TEST_ASSERT( ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
Paul Bakker94b916c2014-04-17 16:07:20 +0200567 ret = ecp_tls_read_group( &grp2, &vbuf, len );
568 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100569
570 if( ret == 0 )
571 {
572 TEST_ASSERT( mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
573 TEST_ASSERT( grp1.id == grp2.id );
574 }
575
Paul Bakkerbd51b262014-07-10 15:26:12 +0200576exit:
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100577 ecp_group_free( &grp1 );
578 ecp_group_free( &grp2 );
579}
Paul Bakker33b43f12013-08-20 11:48:36 +0200580/* END_CASE */
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100581
Paul Bakker33b43f12013-08-20 11:48:36 +0200582/* BEGIN_CASE */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100583void ecp_check_privkey( int id, char *key_hex, int ret )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200584{
585 ecp_group grp;
586 mpi d;
587
588 ecp_group_init( &grp );
589 mpi_init( &d );
590
Paul Bakker33b43f12013-08-20 11:48:36 +0200591 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100592 TEST_ASSERT( mpi_read_string( &d, 16, key_hex ) == 0 );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200593
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100594 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == ret );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200595
Paul Bakkerbd51b262014-07-10 15:26:12 +0200596exit:
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200597 ecp_group_free( &grp );
598 mpi_free( &d );
599}
Paul Bakker33b43f12013-08-20 11:48:36 +0200600/* END_CASE */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200601
Paul Bakker33b43f12013-08-20 11:48:36 +0200602/* BEGIN_CASE */
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +0100603void ecp_check_pub_priv( int id_pub, char *Qx_pub, char *Qy_pub,
604 int id, char *d, char *Qx, char *Qy, int ret )
605{
606 ecp_keypair pub, prv;
607
608 ecp_keypair_init( &pub );
609 ecp_keypair_init( &prv );
610
611 if( id_pub != POLARSSL_ECP_DP_NONE )
612 TEST_ASSERT( ecp_use_known_dp( &pub.grp, id_pub ) == 0 );
613 TEST_ASSERT( ecp_point_read_string( &pub.Q, 16, Qx_pub, Qy_pub ) == 0 );
614
615 if( id != POLARSSL_ECP_DP_NONE )
616 TEST_ASSERT( ecp_use_known_dp( &prv.grp, id ) == 0 );
617 TEST_ASSERT( ecp_point_read_string( &prv.Q, 16, Qx, Qy ) == 0 );
618 TEST_ASSERT( mpi_read_string( &prv.d, 16, d ) == 0 );
619
620 TEST_ASSERT( ecp_check_pub_priv( &pub, &prv ) == ret );
621
622exit:
623 ecp_keypair_free( &pub );
624 ecp_keypair_free( &prv );
625}
626/* END_CASE */
627
628/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200629void ecp_gen_keypair( int id )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100630{
631 ecp_group grp;
632 ecp_point Q;
633 mpi d;
634 rnd_pseudo_info rnd_info;
635
636 ecp_group_init( &grp );
637 ecp_point_init( &Q );
638 mpi_init( &d );
639 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
640
Paul Bakker33b43f12013-08-20 11:48:36 +0200641 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100642
643 TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
644 == 0 );
645
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200646 TEST_ASSERT( ecp_check_pubkey( &grp, &Q ) == 0 );
Paul Bakker8ea6c612013-07-16 17:15:03 +0200647 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100648
Paul Bakkerbd51b262014-07-10 15:26:12 +0200649exit:
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100650 ecp_group_free( &grp );
651 ecp_point_free( &Q );
652 mpi_free( &d );
653}
Paul Bakker33b43f12013-08-20 11:48:36 +0200654/* END_CASE */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100655
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100656/* BEGIN_CASE */
657void ecp_gen_key( int id )
658{
659 ecp_keypair key;
660 rnd_pseudo_info rnd_info;
661
662 ecp_keypair_init( &key );
663 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
664
665 TEST_ASSERT( ecp_gen_key( id, &key, &rnd_pseudo_rand, &rnd_info ) == 0 );
666
667 TEST_ASSERT( ecp_check_pubkey( &key.grp, &key.Q ) == 0 );
668 TEST_ASSERT( ecp_check_privkey( &key.grp, &key.d ) == 0 );
669
Paul Bakkerbd51b262014-07-10 15:26:12 +0200670exit:
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100671 ecp_keypair_free( &key );
672}
673/* END_CASE */
674
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200675/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200676void ecp_selftest()
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100677{
678 TEST_ASSERT( ecp_self_test( 0 ) == 0 );
679}
Paul Bakker33b43f12013-08-20 11:48:36 +0200680/* END_CASE */