blob: 9473dd442715924a1d437bb516c8ccf811431750 [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 */
13void ecp_small_add( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
14 char *y_b, int c_zero, int x_c, int y_c )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010015{
16 ecp_group grp;
17 ecp_point A, B, C;
18
19 ecp_group_init( &grp );
20 ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
21
22 TEST_ASSERT( ecp_group_read_string( &grp, 10,
23 "47", "4", "17", "42", "13" ) == 0 );
24
Paul Bakker33b43f12013-08-20 11:48:36 +020025 if( a_zero )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010026 ecp_set_zero( &A );
27 else
Paul Bakker33b43f12013-08-20 11:48:36 +020028 TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010029
Paul Bakker33b43f12013-08-20 11:48:36 +020030 if( b_zero )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010031 ecp_set_zero( &B );
32 else
Paul Bakker33b43f12013-08-20 11:48:36 +020033 TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010034
35 TEST_ASSERT( ecp_add( &grp, &C, &A, &B ) == 0 );
36
Paul Bakker33b43f12013-08-20 11:48:36 +020037 if( c_zero )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010038 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010039 else
40 {
Paul Bakker33b43f12013-08-20 11:48:36 +020041 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
42 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010043 }
44
45 TEST_ASSERT( ecp_add( &grp, &C, &B, &A ) == 0 );
46
Paul Bakker33b43f12013-08-20 11:48:36 +020047 if( c_zero )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010048 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010049 else
50 {
Paul Bakker33b43f12013-08-20 11:48:36 +020051 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
52 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010053 }
54
55 ecp_group_free( &grp );
56 ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
57}
Paul Bakker33b43f12013-08-20 11:48:36 +020058/* END_CASE */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010059
Paul Bakker33b43f12013-08-20 11:48:36 +020060/* BEGIN_CASE */
61void ecp_small_sub( int a_zero, char *x_a, char *y_a, int b_zero, char *x_b,
62 char *y_b, int c_zero, int x_c, int y_c )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010063{
64 ecp_group grp;
65 ecp_point A, B, C;
66
67 ecp_group_init( &grp );
68 ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
69
70 TEST_ASSERT( ecp_group_read_string( &grp, 10,
71 "47", "4", "17", "42", "13" ) == 0 );
72
Paul Bakker33b43f12013-08-20 11:48:36 +020073 if( a_zero )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010074 ecp_set_zero( &A );
75 else
Paul Bakker33b43f12013-08-20 11:48:36 +020076 TEST_ASSERT( ecp_point_read_string( &A, 10, x_a, y_a ) == 0 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010077
Paul Bakker33b43f12013-08-20 11:48:36 +020078 if( b_zero )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010079 ecp_set_zero( &B );
80 else
Paul Bakker33b43f12013-08-20 11:48:36 +020081 TEST_ASSERT( ecp_point_read_string( &B, 10, x_b, y_b ) == 0 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010082
83 TEST_ASSERT( ecp_sub( &grp, &C, &A, &B ) == 0 );
84
Paul Bakker33b43f12013-08-20 11:48:36 +020085 if( c_zero )
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010086 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
87 else
88 {
Paul Bakker33b43f12013-08-20 11:48:36 +020089 TEST_ASSERT( mpi_cmp_int( &C.X, x_c ) == 0 );
90 TEST_ASSERT( mpi_cmp_int( &C.Y, y_c ) == 0 );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010091 }
92
93 ecp_group_free( &grp );
94 ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
95}
Paul Bakker33b43f12013-08-20 11:48:36 +020096/* END_CASE */
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010097
Paul Bakker33b43f12013-08-20 11:48:36 +020098/* BEGIN_CASE */
99void 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 +0100100{
101 ecp_group grp;
102 ecp_point R;
103 mpi m;
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200104 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100105
106 ecp_group_init( &grp );
107 ecp_point_init( &R );
108 mpi_init( &m );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200109 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100110
111 TEST_ASSERT( ecp_group_read_string( &grp, 10,
112 "47", "4", "17", "42", "13" ) == 0 );
113
Paul Bakker33b43f12013-08-20 11:48:36 +0200114 TEST_ASSERT( mpi_lset( &m, m_str ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100115
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200116 TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) == ret );
117
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100118 if( ret == 0 )
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200119 {
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100120 if( r_zero )
121 TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
122 else
123 {
124 TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
125 TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
126 }
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200127 }
128
129 /* try again with randomization */
130 ecp_point_free( &R );
131
132 TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G,
133 &rnd_pseudo_rand, &rnd_info ) == ret );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100134
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100135 if( ret == 0 )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100136 {
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100137 if( r_zero )
138 TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
139 else
140 {
141 TEST_ASSERT( mpi_cmp_int( &R.X, x_r ) == 0 );
142 TEST_ASSERT( mpi_cmp_int( &R.Y, y_r ) == 0 );
143 }
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100144 }
145
146 ecp_group_free( &grp );
147 ecp_point_free( &R );
148 mpi_free( &m );
149}
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* END_CASE */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100151
Paul Bakker33b43f12013-08-20 11:48:36 +0200152/* BEGIN_CASE */
153void ecp_small_check_pub( int x, int y, int z, int ret )
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100154{
155 ecp_group grp;
156 ecp_point P;
157
158 ecp_group_init( &grp );
159 ecp_point_init( &P );
160
161 TEST_ASSERT( ecp_group_read_string( &grp, 10,
162 "47", "4", "17", "42", "13" ) == 0 );
163
Paul Bakker33b43f12013-08-20 11:48:36 +0200164 TEST_ASSERT( mpi_lset( &P.X, x ) == 0 );
165 TEST_ASSERT( mpi_lset( &P.Y, y ) == 0 );
166 TEST_ASSERT( mpi_lset( &P.Z, z ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100167
Paul Bakker33b43f12013-08-20 11:48:36 +0200168 TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == ret );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100169
170 ecp_group_free( &grp );
171 ecp_point_free( &P );
172}
Paul Bakker33b43f12013-08-20 11:48:36 +0200173/* END_CASE */
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100174
Paul Bakker33b43f12013-08-20 11:48:36 +0200175/* BEGIN_CASE */
176void ecp_test_vect( int id, char *dA_str, char *xA_str, char *yA_str,
177 char *dB_str, char *xB_str, char *yB_str, char *xZ_str,
178 char *yZ_str )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100179{
180 ecp_group grp;
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100181 ecp_point R;
182 mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200183 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100184
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100185 ecp_group_init( &grp ); ecp_point_init( &R );
186 mpi_init( &dA ); mpi_init( &xA ); mpi_init( &yA ); mpi_init( &dB );
187 mpi_init( &xB ); mpi_init( &yB ); mpi_init( &xZ ); mpi_init( &yZ );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200188 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100189
Paul Bakker33b43f12013-08-20 11:48:36 +0200190 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100191
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100192 TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
193
Paul Bakker33b43f12013-08-20 11:48:36 +0200194 TEST_ASSERT( mpi_read_string( &dA, 16, dA_str ) == 0 );
195 TEST_ASSERT( mpi_read_string( &xA, 16, xA_str ) == 0 );
196 TEST_ASSERT( mpi_read_string( &yA, 16, yA_str ) == 0 );
197 TEST_ASSERT( mpi_read_string( &dB, 16, dB_str ) == 0 );
198 TEST_ASSERT( mpi_read_string( &xB, 16, xB_str ) == 0 );
199 TEST_ASSERT( mpi_read_string( &yB, 16, yB_str ) == 0 );
200 TEST_ASSERT( mpi_read_string( &xZ, 16, xZ_str ) == 0 );
201 TEST_ASSERT( mpi_read_string( &yZ, 16, yZ_str ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100202
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200203 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G,
204 &rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100205 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
206 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100207 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200208 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100209 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
210 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100211 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100212
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200213 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100214 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
215 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100216 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200217 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R,
218 &rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100219 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
220 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100221 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100222
223 ecp_group_free( &grp ); ecp_point_free( &R );
224 mpi_free( &dA ); mpi_free( &xA ); mpi_free( &yA ); mpi_free( &dB );
225 mpi_free( &xB ); mpi_free( &yB ); mpi_free( &xZ ); mpi_free( &yZ );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100226}
Paul Bakker33b43f12013-08-20 11:48:36 +0200227/* END_CASE */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100228
Paul Bakker33b43f12013-08-20 11:48:36 +0200229/* BEGIN_CASE */
230void ecp_fast_mod( int id, char *N_str )
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100231{
232 ecp_group grp;
233 mpi N, R;
234
235 mpi_init( &N ); mpi_init( &R );
236 ecp_group_init( &grp );
237
Paul Bakker33b43f12013-08-20 11:48:36 +0200238 TEST_ASSERT( mpi_read_string( &N, 16, N_str ) == 0 );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200239 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
240 TEST_ASSERT( grp.modp != NULL );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100241
242 /*
243 * Store correct result before we touch N
244 */
245 TEST_ASSERT( mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
246
247 TEST_ASSERT( grp.modp( &N ) == 0 );
248 TEST_ASSERT( mpi_msb( &N ) <= grp.pbits + 3 );
249
250 /*
251 * Use mod rather than addition/substraction in case previous test fails
252 */
253 TEST_ASSERT( mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
254 TEST_ASSERT( mpi_cmp_mpi( &N, &R ) == 0 );
255
256 mpi_free( &N ); mpi_free( &R );
257 ecp_group_free( &grp );
258}
Paul Bakker33b43f12013-08-20 11:48:36 +0200259/* END_CASE */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100260
Paul Bakker33b43f12013-08-20 11:48:36 +0200261/* BEGIN_CASE */
262void ecp_write_binary( int id, char *x, char *y, char *z, int format,
263 char *out, int blen, int ret )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100264{
265 ecp_group grp;
266 ecp_point P;
267 unsigned char buf[256], str[512];
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100268 size_t olen;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100269
270 memset( buf, 0, sizeof( buf ) );
271 memset( str, 0, sizeof( str ) );
272
273 ecp_group_init( &grp ); ecp_point_init( &P );
274
Paul Bakker33b43f12013-08-20 11:48:36 +0200275 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100276
Paul Bakker33b43f12013-08-20 11:48:36 +0200277 TEST_ASSERT( mpi_read_string( &P.X, 16, x ) == 0 );
278 TEST_ASSERT( mpi_read_string( &P.Y, 16, y ) == 0 );
279 TEST_ASSERT( mpi_read_string( &P.Z, 16, z ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100280
Paul Bakker33b43f12013-08-20 11:48:36 +0200281 TEST_ASSERT( ecp_point_write_binary( &grp, &P, format,
282 &olen, buf, blen ) == ret );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100283
Paul Bakker33b43f12013-08-20 11:48:36 +0200284 if( ret == 0 )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100285 {
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100286 hexify( str, buf, olen );
Paul Bakker33b43f12013-08-20 11:48:36 +0200287 TEST_ASSERT( strcasecmp( (char *) str, out ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100288 }
289
290 ecp_group_free( &grp ); ecp_point_free( &P );
291}
Paul Bakker33b43f12013-08-20 11:48:36 +0200292/* END_CASE */
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100293
Paul Bakker33b43f12013-08-20 11:48:36 +0200294/* BEGIN_CASE */
295void ecp_read_binary( int id, char *input, char *x, char *y, char *z,
296 int ret )
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100297{
298 ecp_group grp;
299 ecp_point P;
300 mpi X, Y, Z;
301 int ilen;
302 unsigned char buf[256];
303
304 memset( buf, 0, sizeof( buf ) );
305
306 ecp_group_init( &grp ); ecp_point_init( &P );
307 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
308
Paul Bakker33b43f12013-08-20 11:48:36 +0200309 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100310
Paul Bakker33b43f12013-08-20 11:48:36 +0200311 TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
312 TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
313 TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100314
Paul Bakker33b43f12013-08-20 11:48:36 +0200315 ilen = unhexify( buf, input );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100316
Paul Bakker33b43f12013-08-20 11:48:36 +0200317 TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == ret );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100318
Paul Bakker33b43f12013-08-20 11:48:36 +0200319 if( ret == 0 )
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100320 {
321 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
322 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
323 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
324 }
325
326 ecp_group_free( &grp ); ecp_point_free( &P );
327 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
328}
Paul Bakker33b43f12013-08-20 11:48:36 +0200329/* END_CASE */
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100330
Paul Bakker33b43f12013-08-20 11:48:36 +0200331/* BEGIN_CASE */
332void ecp_tls_read_point( int id, char *input, char *x, char *y, char *z,
333 int ret )
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100334{
335 ecp_group grp;
336 ecp_point P;
337 mpi X, Y, Z;
338 size_t ilen;
339 unsigned char buf[256];
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100340 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100341
342 memset( buf, 0, sizeof( buf ) );
343
344 ecp_group_init( &grp ); ecp_point_init( &P );
345 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
346
Paul Bakker33b43f12013-08-20 11:48:36 +0200347 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100348
Paul Bakker33b43f12013-08-20 11:48:36 +0200349 TEST_ASSERT( mpi_read_string( &X, 16, x ) == 0 );
350 TEST_ASSERT( mpi_read_string( &Y, 16, y ) == 0 );
351 TEST_ASSERT( mpi_read_string( &Z, 16, z ) == 0 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100352
Paul Bakker33b43f12013-08-20 11:48:36 +0200353 ilen = unhexify( buf, input );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100354
Paul Bakker33b43f12013-08-20 11:48:36 +0200355 TEST_ASSERT( ecp_tls_read_point( &grp, &P, &vbuf, ilen ) == ret );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100356
Paul Bakker33b43f12013-08-20 11:48:36 +0200357 if( ret == 0 )
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100358 {
359 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
360 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
361 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100362 TEST_ASSERT( *vbuf == 0x00 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100363 }
364
365 ecp_group_free( &grp ); ecp_point_free( &P );
366 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
367}
Paul Bakker33b43f12013-08-20 11:48:36 +0200368/* END_CASE */
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100369
Paul Bakker33b43f12013-08-20 11:48:36 +0200370/* BEGIN_CASE */
371void ecp_tls_write_read_point( int id )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100372{
373 ecp_group grp;
374 ecp_point pt;
375 unsigned char buf[256];
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100376 const unsigned char *vbuf;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100377 size_t olen;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100378
379 ecp_group_init( &grp );
380 ecp_point_init( &pt );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100381
Paul Bakker33b43f12013-08-20 11:48:36 +0200382 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100383
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100384 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100385 TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100386 POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100387 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100388 == POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100389 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100390
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100391 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100392 TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100393 POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100394 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100395 TEST_ASSERT( mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
396 TEST_ASSERT( mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
397 TEST_ASSERT( mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100398 TEST_ASSERT( vbuf == buf + olen );
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_set_zero( &pt ) == 0 );
402 TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100403 POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100404 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100405 TEST_ASSERT( ecp_is_zero( &pt ) );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100406 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100407
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100408 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100409 TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
410 TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100411 POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100412 TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100413 TEST_ASSERT( ecp_is_zero( &pt ) );
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
416 ecp_group_free( &grp );
417 ecp_point_free( &pt );
418}
Paul Bakker33b43f12013-08-20 11:48:36 +0200419/* END_CASE */
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100420
Paul Bakker33b43f12013-08-20 11:48:36 +0200421/* BEGIN_CASE */
422void ecp_tls_read_group( char *record, int result, int bits )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100423{
424 ecp_group grp;
425 unsigned char buf[10];
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100426 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100427 int len, ret;
428
429 ecp_group_init( &grp );
430 memset( buf, 0x00, sizeof( buf ) );
431
Paul Bakker33b43f12013-08-20 11:48:36 +0200432 len = unhexify( buf, record );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100433
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100434 ret = ecp_tls_read_group( &grp, &vbuf, len );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100435
Paul Bakker33b43f12013-08-20 11:48:36 +0200436 TEST_ASSERT( ret == result );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100437 if( ret == 0)
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100438 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200439 TEST_ASSERT( mpi_msb( &grp.P ) == (size_t) bits );
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100440 TEST_ASSERT( *vbuf == 0x00 );
441 }
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100442
443 ecp_group_free( &grp );
444}
Paul Bakker33b43f12013-08-20 11:48:36 +0200445/* END_CASE */
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100446
Paul Bakker33b43f12013-08-20 11:48:36 +0200447/* BEGIN_CASE */
448void ecp_tls_write_read_group( int id )
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100449{
450 ecp_group grp1, grp2;
451 unsigned char buf[10];
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100452 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100453 size_t len;
454 int ret;
455
456 ecp_group_init( &grp1 );
457 ecp_group_init( &grp2 );
458 memset( buf, 0x00, sizeof( buf ) );
459
Paul Bakker33b43f12013-08-20 11:48:36 +0200460 TEST_ASSERT( ecp_use_known_dp( &grp1, id ) == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100461
462 TEST_ASSERT( ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100463 TEST_ASSERT( ( ret = ecp_tls_read_group( &grp2, &vbuf, len ) ) == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100464
465 if( ret == 0 )
466 {
467 TEST_ASSERT( mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
468 TEST_ASSERT( grp1.id == grp2.id );
469 }
470
471 ecp_group_free( &grp1 );
472 ecp_group_free( &grp2 );
473}
Paul Bakker33b43f12013-08-20 11:48:36 +0200474/* END_CASE */
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100475
Paul Bakker33b43f12013-08-20 11:48:36 +0200476/* BEGIN_CASE */
477void ecp_check_privkey( int id )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200478{
479 ecp_group grp;
480 mpi d;
481
482 ecp_group_init( &grp );
483 mpi_init( &d );
484
Paul Bakker33b43f12013-08-20 11:48:36 +0200485 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200486
487 TEST_ASSERT( mpi_lset( &d, 0 ) == 0 );
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200488 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200489
490 TEST_ASSERT( mpi_copy( &d, &grp.N ) == 0 );
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200491 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200492
493 ecp_group_free( &grp );
494 mpi_free( &d );
495}
Paul Bakker33b43f12013-08-20 11:48:36 +0200496/* END_CASE */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200497
Paul Bakker33b43f12013-08-20 11:48:36 +0200498/* BEGIN_CASE */
499void ecp_gen_keypair( int id )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100500{
501 ecp_group grp;
502 ecp_point Q;
503 mpi d;
504 rnd_pseudo_info rnd_info;
505
506 ecp_group_init( &grp );
507 ecp_point_init( &Q );
508 mpi_init( &d );
509 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
510
Paul Bakker33b43f12013-08-20 11:48:36 +0200511 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100512
513 TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
514 == 0 );
515
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200516 TEST_ASSERT( ecp_check_pubkey( &grp, &Q ) == 0 );
Paul Bakker8ea6c612013-07-16 17:15:03 +0200517 TEST_ASSERT( ecp_check_privkey( &grp, &d ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100518
519 ecp_group_free( &grp );
520 ecp_point_free( &Q );
521 mpi_free( &d );
522}
Paul Bakker33b43f12013-08-20 11:48:36 +0200523/* END_CASE */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100524
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100525/* BEGIN_CASE */
526void ecp_gen_key( int id )
527{
528 ecp_keypair key;
529 rnd_pseudo_info rnd_info;
530
531 ecp_keypair_init( &key );
532 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
533
534 TEST_ASSERT( ecp_gen_key( id, &key, &rnd_pseudo_rand, &rnd_info ) == 0 );
535
536 TEST_ASSERT( ecp_check_pubkey( &key.grp, &key.Q ) == 0 );
537 TEST_ASSERT( ecp_check_privkey( &key.grp, &key.d ) == 0 );
538
539 ecp_keypair_free( &key );
540}
541/* END_CASE */
542
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200543/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200544void ecp_selftest()
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100545{
546 TEST_ASSERT( ecp_self_test( 0 ) == 0 );
547}
Paul Bakker33b43f12013-08-20 11:48:36 +0200548/* END_CASE */