blob: 55354ad8bf48e5773a9406872aea79bacc9758ba [file] [log] [blame]
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +01001BEGIN_HEADER
2#include <polarssl/ecp.h>
3END_HEADER
4
5BEGIN_DEPENDENCIES
6depends_on:POLARSSL_ECP_C:POLARSSL_BIGNUM_C
7END_DEPENDENCIES
8
9BEGIN_CASE
10ecp_small_add:a_zero:x_a:y_a:b_zero:x_b:y_b:c_zero:x_c:y_c
11{
12 ecp_group grp;
13 ecp_point A, B, C;
14
15 ecp_group_init( &grp );
16 ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
17
18 TEST_ASSERT( ecp_group_read_string( &grp, 10,
19 "47", "4", "17", "42", "13" ) == 0 );
20
21 if( {a_zero} )
22 ecp_set_zero( &A );
23 else
24 TEST_ASSERT( ecp_point_read_string( &A, 10, {x_a}, {y_a} ) == 0 );
25
26 if( {b_zero} )
27 ecp_set_zero( &B );
28 else
29 TEST_ASSERT( ecp_point_read_string( &B, 10, {x_b}, {y_b} ) == 0 );
30
31 TEST_ASSERT( ecp_add( &grp, &C, &A, &B ) == 0 );
32
33 if( {c_zero} )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010034 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010035 else
36 {
37 TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 );
38 TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 );
39 }
40
41 TEST_ASSERT( ecp_add( &grp, &C, &B, &A ) == 0 );
42
43 if( {c_zero} )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010044 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010045 else
46 {
47 TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 );
48 TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 );
49 }
50
51 ecp_group_free( &grp );
52 ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
53}
54END_CASE
55
56BEGIN_CASE
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +010057ecp_small_sub:a_zero:x_a:y_a:b_zero:x_b:y_b:c_zero:x_c:y_c
58{
59 ecp_group grp;
60 ecp_point A, B, C;
61
62 ecp_group_init( &grp );
63 ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );
64
65 TEST_ASSERT( ecp_group_read_string( &grp, 10,
66 "47", "4", "17", "42", "13" ) == 0 );
67
68 if( {a_zero} )
69 ecp_set_zero( &A );
70 else
71 TEST_ASSERT( ecp_point_read_string( &A, 10, {x_a}, {y_a} ) == 0 );
72
73 if( {b_zero} )
74 ecp_set_zero( &B );
75 else
76 TEST_ASSERT( ecp_point_read_string( &B, 10, {x_b}, {y_b} ) == 0 );
77
78 TEST_ASSERT( ecp_sub( &grp, &C, &A, &B ) == 0 );
79
80 if( {c_zero} )
81 TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
82 else
83 {
84 TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 );
85 TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 );
86 }
87
88 ecp_group_free( &grp );
89 ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
90}
91END_CASE
92
93BEGIN_CASE
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +010094ecp_small_mul:m:r_zero:x_r:y_r:ret
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010095{
96 ecp_group grp;
97 ecp_point R;
98 mpi m;
99
100 ecp_group_init( &grp );
101 ecp_point_init( &R );
102 mpi_init( &m );
103
104 TEST_ASSERT( ecp_group_read_string( &grp, 10,
105 "47", "4", "17", "42", "13" ) == 0 );
106
107 TEST_ASSERT( mpi_lset( &m, {m} ) == 0 );
108
Manuel Pégourié-Gonnard4bdd47d2012-11-11 14:33:59 +0100109 TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G ) == {ret} );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100110
111 if( {r_zero} )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100112 TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100113 else
114 {
115 TEST_ASSERT( mpi_cmp_int( &R.X, {x_r} ) == 0 );
116 TEST_ASSERT( mpi_cmp_int( &R.Y, {y_r} ) == 0 );
117 }
118
119 ecp_group_free( &grp );
120 ecp_point_free( &R );
121 mpi_free( &m );
122}
123END_CASE
124
125BEGIN_CASE
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100126ecp_small_check_pub:x:y:z:ret
127{
128 ecp_group grp;
129 ecp_point P;
130
131 ecp_group_init( &grp );
132 ecp_point_init( &P );
133
134 TEST_ASSERT( ecp_group_read_string( &grp, 10,
135 "47", "4", "17", "42", "13" ) == 0 );
136
137 TEST_ASSERT( mpi_lset( &P.X, {x} ) == 0 );
138 TEST_ASSERT( mpi_lset( &P.Y, {y} ) == 0 );
139 TEST_ASSERT( mpi_lset( &P.Z, {z} ) == 0 );
140
141 TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == {ret} );
142
143 ecp_group_free( &grp );
144 ecp_point_free( &P );
145}
146END_CASE
147
148BEGIN_CASE
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100149ecp_test_vect:id:dA:xA:yA:dB:xB:yB:xZ:yZ
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100150{
151 ecp_group grp;
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100152 ecp_point R;
153 mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100154
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100155 ecp_group_init( &grp ); ecp_point_init( &R );
156 mpi_init( &dA ); mpi_init( &xA ); mpi_init( &yA ); mpi_init( &dB );
157 mpi_init( &xB ); mpi_init( &yB ); mpi_init( &xZ ); mpi_init( &yZ );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100158
159 TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
160
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100161 TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );
162
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100163 TEST_ASSERT( mpi_read_string( &dA, 16, {dA} ) == 0 );
164 TEST_ASSERT( mpi_read_string( &xA, 16, {xA} ) == 0 );
165 TEST_ASSERT( mpi_read_string( &yA, 16, {yA} ) == 0 );
166 TEST_ASSERT( mpi_read_string( &dB, 16, {dB} ) == 0 );
167 TEST_ASSERT( mpi_read_string( &xB, 16, {xB} ) == 0 );
168 TEST_ASSERT( mpi_read_string( &yB, 16, {yB} ) == 0 );
169 TEST_ASSERT( mpi_read_string( &xZ, 16, {xZ} ) == 0 );
170 TEST_ASSERT( mpi_read_string( &yZ, 16, {yZ} ) == 0 );
171
172 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G ) == 0 );
173 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
174 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100175 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100176 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R ) == 0 );
177 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
178 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100179 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100180
181 TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G ) == 0 );
182 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
183 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100184 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100185 TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R ) == 0 );
186 TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
187 TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100188 TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100189
190 ecp_group_free( &grp ); ecp_point_free( &R );
191 mpi_free( &dA ); mpi_free( &xA ); mpi_free( &yA ); mpi_free( &dB );
192 mpi_free( &xB ); mpi_free( &yB ); mpi_free( &xZ ); mpi_free( &yZ );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100193}
194END_CASE
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100195
196BEGIN_CASE
197ecp_fast_mod:id:N
198{
199 ecp_group grp;
200 mpi N, R;
201
202 mpi_init( &N ); mpi_init( &R );
203 ecp_group_init( &grp );
204
205 TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
206 TEST_ASSERT( mpi_read_string( &N, 16, {N} ) == 0 );
207
208 /*
209 * Store correct result before we touch N
210 */
211 TEST_ASSERT( mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
212
213 TEST_ASSERT( grp.modp( &N ) == 0 );
214 TEST_ASSERT( mpi_msb( &N ) <= grp.pbits + 3 );
215
216 /*
217 * Use mod rather than addition/substraction in case previous test fails
218 */
219 TEST_ASSERT( mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
220 TEST_ASSERT( mpi_cmp_mpi( &N, &R ) == 0 );
221
222 mpi_free( &N ); mpi_free( &R );
223 ecp_group_free( &grp );
224}
225END_CASE
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100226
227BEGIN_CASE
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100228ecp_write_binary:id:x:y:z:format:out:blen:ret
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100229{
230 ecp_group grp;
231 ecp_point P;
232 unsigned char buf[256], str[512];
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100233 uint8_t olen;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100234
235 memset( buf, 0, sizeof( buf ) );
236 memset( str, 0, sizeof( str ) );
237
238 ecp_group_init( &grp ); ecp_point_init( &P );
239
240 TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
241
242 TEST_ASSERT( mpi_read_string( &P.X, 16, {x} ) == 0 );
243 TEST_ASSERT( mpi_read_string( &P.Y, 16, {y} ) == 0 );
244 TEST_ASSERT( mpi_read_string( &P.Z, 16, {z} ) == 0 );
245
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100246#define POLARSSL_ECP_PF_UNKNOWN -1
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100247 TEST_ASSERT( ecp_point_write_binary( &grp, &P, POLARSSL_ECP_PF_{format},
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100248 &olen, buf, {blen} ) == {ret} );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100249
250 if( {ret} == 0 )
251 {
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100252 hexify( str, buf, olen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100253 TEST_ASSERT( strcasecmp( (char *) str, {out} ) == 0 );
254 }
255
256 ecp_group_free( &grp ); ecp_point_free( &P );
257}
258END_CASE
259
260BEGIN_CASE
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100261ecp_read_binary:id:input:x:y:z:ret
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100262{
263 ecp_group grp;
264 ecp_point P;
265 mpi X, Y, Z;
266 int ilen;
267 unsigned char buf[256];
268
269 memset( buf, 0, sizeof( buf ) );
270
271 ecp_group_init( &grp ); ecp_point_init( &P );
272 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
273
274 TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
275
276 TEST_ASSERT( mpi_read_string( &X, 16, {x} ) == 0 );
277 TEST_ASSERT( mpi_read_string( &Y, 16, {y} ) == 0 );
278 TEST_ASSERT( mpi_read_string( &Z, 16, {z} ) == 0 );
279
280 ilen = unhexify( buf, {input} );
281
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100282 TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == {ret} );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100283
284 if( {ret} == 0 )
285 {
286 TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
287 TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
288 TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
289 }
290
291 ecp_group_free( &grp ); ecp_point_free( &P );
292 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
293}
294END_CASE
295
296BEGIN_CASE
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100297ecp_gen_keypair:id
298{
299 ecp_group grp;
300 ecp_point Q;
301 mpi d;
302 rnd_pseudo_info rnd_info;
303
304 ecp_group_init( &grp );
305 ecp_point_init( &Q );
306 mpi_init( &d );
307 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
308
309 TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
310
311 TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
312 == 0 );
313
314 TEST_ASSERT( mpi_cmp_mpi( &d, &grp.N ) < 0 );
315 TEST_ASSERT( mpi_cmp_int( &d, 1 ) >= 0 );
316
317 ecp_group_free( &grp );
318 ecp_point_free( &Q );
319 mpi_free( &d );
320}
321END_CASE
322
323BEGIN_CASE
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100324ecp_read_params:record:ret:bits
325{
326 ecp_group grp;
327 unsigned char buf[10];
328 int len, ret;
329
330 ecp_group_init( &grp );
331 memset( buf, 0x00, sizeof( buf ) );
332
333 len = unhexify( buf, {record} );
334
335 ret = ecp_tls_read_group( &grp, buf, len );
336
337 TEST_ASSERT( ret == {ret} );
338 if( ret == 0)
339 TEST_ASSERT( mpi_msb( &grp.P ) == {bits} );
340
341 ecp_group_free( &grp );
342}
343END_CASE
344
345BEGIN_CASE
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100346ecp_selftest:
347{
348 TEST_ASSERT( ecp_self_test( 0 ) == 0 );
349}
350END_CASE