Update mbedTLS sources
[platform/upstream/iotivity.git] / extlibs / mbedtls / mbedtls / tests / suites / test_suite_ecdh.function
1 /* BEGIN_HEADER */
2 #include "mbedtls/ecdh.h"
3
4 static int load_public_key( int grp_id, data_t *point,
5                             mbedtls_ecp_keypair *ecp )
6 {
7     int ok = 0;
8     TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
9     TEST_ASSERT( mbedtls_ecp_point_read_binary( &ecp->grp,
10                                                 &ecp->Q,
11                                                 point->x,
12                                                 point->len ) == 0 );
13     TEST_ASSERT( mbedtls_ecp_check_pubkey( &ecp->grp,
14                                            &ecp->Q ) == 0 );
15     ok = 1;
16 exit:
17     return( ok );
18 }
19
20 static int load_private_key( int grp_id, data_t *private_key,
21                              mbedtls_ecp_keypair *ecp,
22                              rnd_pseudo_info *rnd_info )
23 {
24     int ok = 0;
25     TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
26     TEST_ASSERT( mbedtls_mpi_read_binary( &ecp->d,
27                                           private_key->x,
28                                           private_key->len ) == 0 );
29     TEST_ASSERT( mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) == 0 );
30     /* Calculate the public key from the private key. */
31     TEST_ASSERT( mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d,
32                                   &ecp->grp.G,
33                                   &rnd_pseudo_rand, rnd_info ) == 0 );
34     ok = 1;
35 exit:
36     return( ok );
37 }
38
39 /* END_HEADER */
40
41 /* BEGIN_DEPENDENCIES
42  * depends_on:MBEDTLS_ECDH_C
43  * END_DEPENDENCIES
44  */
45
46 /* BEGIN_CASE */
47 void ecdh_valid_param( )
48 {
49     TEST_VALID_PARAM( mbedtls_ecdh_free( NULL ) );
50 }
51 /* END_CASE */
52
53 /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
54 void ecdh_invalid_param( )
55 {
56     mbedtls_ecp_group grp;
57     mbedtls_ecdh_context ctx;
58     mbedtls_mpi m;
59     mbedtls_ecp_point P;
60     mbedtls_ecp_keypair kp;
61     size_t olen;
62     unsigned char buf[42] = { 0 };
63     const unsigned char *buf_null = NULL;
64     size_t const buflen = sizeof( buf );
65     int invalid_side = 42;
66     mbedtls_ecp_group_id valid_grp = MBEDTLS_ECP_DP_SECP192R1;
67
68     TEST_INVALID_PARAM( mbedtls_ecdh_init( NULL ) );
69
70 #if defined(MBEDTLS_ECP_RESTARTABLE)
71     TEST_INVALID_PARAM( mbedtls_ecdh_enable_restart( NULL ) );
72 #endif /* MBEDTLS_ECP_RESTARTABLE */
73
74     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
75                             mbedtls_ecdh_gen_public( NULL, &m, &P,
76                                                      rnd_std_rand, NULL ) );
77     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
78                             mbedtls_ecdh_gen_public( &grp, NULL, &P,
79                                                      rnd_std_rand, NULL ) );
80     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
81                             mbedtls_ecdh_gen_public( &grp, &m, NULL,
82                                                      rnd_std_rand, NULL ) );
83     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
84                             mbedtls_ecdh_gen_public( &grp, &m, &P,
85                                                      NULL, NULL ) );
86
87     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
88                             mbedtls_ecdh_compute_shared( NULL, &m, &P, &m,
89                                                          rnd_std_rand, NULL ) );
90     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
91                             mbedtls_ecdh_compute_shared( &grp, NULL, &P, &m,
92                                                          rnd_std_rand, NULL ) );
93     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
94                             mbedtls_ecdh_compute_shared( &grp, &m, NULL, &m,
95                                                          rnd_std_rand, NULL ) );
96     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
97                             mbedtls_ecdh_compute_shared( &grp, &m, &P, NULL,
98                                                          rnd_std_rand, NULL ) );
99
100     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
101                             mbedtls_ecdh_setup( NULL, valid_grp ) );
102
103     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
104                             mbedtls_ecdh_make_params( NULL, &olen,
105                                                       buf, buflen,
106                                                       rnd_std_rand, NULL ) );
107     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
108                             mbedtls_ecdh_make_params( &ctx, NULL,
109                                                       buf, buflen,
110                                                       rnd_std_rand, NULL ) );
111     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
112                             mbedtls_ecdh_make_params( &ctx, &olen,
113                                                       NULL, buflen,
114                                                       rnd_std_rand, NULL ) );
115     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
116                             mbedtls_ecdh_make_params( &ctx, &olen,
117                                                       buf, buflen,
118                                                       NULL, NULL ) );
119
120     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
121                             mbedtls_ecdh_read_params( NULL,
122                                                   (const unsigned char**) &buf,
123                                                   buf ) );
124     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
125                             mbedtls_ecdh_read_params( &ctx, &buf_null,
126                                                       buf ) );
127     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
128                             mbedtls_ecdh_read_params( &ctx, NULL, buf ) );
129     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
130                             mbedtls_ecdh_read_params( &ctx,
131                                                   (const unsigned char**) &buf,
132                                                   NULL ) );
133
134     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
135                             mbedtls_ecdh_get_params( NULL, &kp,
136                                                      MBEDTLS_ECDH_OURS ) );
137     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
138                             mbedtls_ecdh_get_params( &ctx, NULL,
139                                                      MBEDTLS_ECDH_OURS ) );
140     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
141                             mbedtls_ecdh_get_params( &ctx, &kp,
142                                                      invalid_side ) );
143
144     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
145                             mbedtls_ecdh_make_public( NULL, &olen,
146                                                       buf, buflen,
147                                                       rnd_std_rand,
148                                                       NULL ) );
149     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
150                             mbedtls_ecdh_make_public( &ctx, NULL,
151                                                       buf, buflen,
152                                                       rnd_std_rand,
153                                                       NULL ) );
154     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
155                             mbedtls_ecdh_make_public( &ctx, &olen,
156                                                       NULL, buflen,
157                                                       rnd_std_rand,
158                                                       NULL ) );
159     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
160                             mbedtls_ecdh_make_public( &ctx, &olen,
161                                                       buf, buflen,
162                                                       NULL,
163                                                       NULL ) );
164
165     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
166                             mbedtls_ecdh_read_public( NULL, buf, buflen ) );
167     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
168                             mbedtls_ecdh_read_public( &ctx, NULL, buflen ) );
169
170     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
171                             mbedtls_ecdh_calc_secret( NULL, &olen, buf, buflen,
172                                                       rnd_std_rand,
173                                                       NULL ) );
174     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
175                             mbedtls_ecdh_calc_secret( &ctx, NULL, buf, buflen,
176                                                       rnd_std_rand,
177                                                       NULL ) );
178     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
179                             mbedtls_ecdh_calc_secret( &ctx, &olen, NULL, buflen,
180                                                       rnd_std_rand,
181                                                       NULL ) );
182
183 exit:
184     return;
185 }
186 /* END_CASE */
187
188 /* BEGIN_CASE */
189 void ecdh_primitive_random( int id )
190 {
191     mbedtls_ecp_group grp;
192     mbedtls_ecp_point qA, qB;
193     mbedtls_mpi dA, dB, zA, zB;
194     rnd_pseudo_info rnd_info;
195
196     mbedtls_ecp_group_init( &grp );
197     mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB );
198     mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB );
199     mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB );
200     memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
201
202     TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
203
204     TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA, &rnd_pseudo_rand, &rnd_info )
205                  == 0 );
206     TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB, &rnd_pseudo_rand, &rnd_info )
207                  == 0 );
208     TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA,
209                                       &rnd_pseudo_rand, &rnd_info ) == 0 );
210     TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB,
211                                       NULL, NULL ) == 0 );
212
213     TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &zB ) == 0 );
214
215 exit:
216     mbedtls_ecp_group_free( &grp );
217     mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB );
218     mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB );
219     mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB );
220 }
221 /* END_CASE */
222
223 /* BEGIN_CASE */
224 void ecdh_primitive_testvec( int id, data_t * rnd_buf_A, char * xA_str,
225                              char * yA_str, data_t * rnd_buf_B,
226                              char * xB_str, char * yB_str, char * z_str )
227 {
228     mbedtls_ecp_group grp;
229     mbedtls_ecp_point qA, qB;
230     mbedtls_mpi dA, dB, zA, zB, check;
231     rnd_buf_info rnd_info_A, rnd_info_B;
232
233     mbedtls_ecp_group_init( &grp );
234     mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB );
235     mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB );
236     mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB ); mbedtls_mpi_init( &check );
237
238     TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
239
240     rnd_info_A.buf = rnd_buf_A->x;
241     rnd_info_A.length = rnd_buf_A->len;
242
243     /* Fix rnd_buf_A->x by shifting it left if necessary */
244     if( grp.nbits % 8 != 0 )
245     {
246         unsigned char shift = 8 - ( grp.nbits % 8 );
247         size_t i;
248
249         for( i = 0; i < rnd_info_A.length - 1; i++ )
250             rnd_buf_A->x[i] = rnd_buf_A->x[i] << shift
251                          | rnd_buf_A->x[i+1] >> ( 8 - shift );
252
253         rnd_buf_A->x[rnd_info_A.length-1] <<= shift;
254     }
255
256     rnd_info_B.buf = rnd_buf_B->x;
257     rnd_info_B.length = rnd_buf_B->len;
258
259     /* Fix rnd_buf_B->x by shifting it left if necessary */
260     if( grp.nbits % 8 != 0 )
261     {
262         unsigned char shift = 8 - ( grp.nbits % 8 );
263         size_t i;
264
265         for( i = 0; i < rnd_info_B.length - 1; i++ )
266             rnd_buf_B->x[i] = rnd_buf_B->x[i] << shift
267                          | rnd_buf_B->x[i+1] >> ( 8 - shift );
268
269         rnd_buf_B->x[rnd_info_B.length-1] <<= shift;
270     }
271
272     TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA,
273                                   rnd_buffer_rand, &rnd_info_A ) == 0 );
274     TEST_ASSERT( ! mbedtls_ecp_is_zero( &qA ) );
275     TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, xA_str ) == 0 );
276     TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.X, &check ) == 0 );
277     TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, yA_str ) == 0 );
278     TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.Y, &check ) == 0 );
279
280     TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB,
281                                   rnd_buffer_rand, &rnd_info_B ) == 0 );
282     TEST_ASSERT( ! mbedtls_ecp_is_zero( &qB ) );
283     TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, xB_str ) == 0 );
284     TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.X, &check ) == 0 );
285     TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, yB_str ) == 0 );
286     TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.Y, &check ) == 0 );
287
288     TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, z_str ) == 0 );
289     TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA, NULL, NULL ) == 0 );
290     TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &check ) == 0 );
291     TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB, NULL, NULL ) == 0 );
292     TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zB, &check ) == 0 );
293
294 exit:
295     mbedtls_ecp_group_free( &grp );
296     mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB );
297     mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB );
298     mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB ); mbedtls_mpi_free( &check );
299 }
300 /* END_CASE */
301
302 /* BEGIN_CASE */
303 void ecdh_exchange( int id )
304 {
305     mbedtls_ecdh_context srv, cli;
306     unsigned char buf[1000];
307     const unsigned char *vbuf;
308     size_t len;
309     rnd_pseudo_info rnd_info;
310     unsigned char res_buf[1000];
311     size_t res_len;
312
313     mbedtls_ecdh_init( &srv );
314     mbedtls_ecdh_init( &cli );
315     memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
316
317     TEST_ASSERT( mbedtls_ecdh_setup( &srv, id ) == 0 );
318
319     memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
320     TEST_ASSERT( mbedtls_ecdh_make_params( &srv, &len, buf, 1000,
321                                            &rnd_pseudo_rand, &rnd_info ) == 0 );
322     TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
323
324     memset( buf, 0x00, sizeof( buf ) );
325     TEST_ASSERT( mbedtls_ecdh_make_public( &cli, &len, buf, 1000,
326                                            &rnd_pseudo_rand, &rnd_info ) == 0 );
327     TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
328
329     TEST_ASSERT( mbedtls_ecdh_calc_secret( &srv, &len, buf, 1000,
330                                            &rnd_pseudo_rand, &rnd_info ) == 0 );
331     TEST_ASSERT( mbedtls_ecdh_calc_secret( &cli, &res_len, res_buf, 1000,
332                                            NULL, NULL ) == 0 );
333     TEST_ASSERT( len == res_len );
334     TEST_ASSERT( memcmp( buf, res_buf, len ) == 0 );
335
336 exit:
337     mbedtls_ecdh_free( &srv );
338     mbedtls_ecdh_free( &cli );
339 }
340 /* END_CASE */
341
342 /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
343 void ecdh_restart( int id, char *dA_str, char *dB_str, char *z_str,
344                    int enable, int max_ops, int min_restart, int max_restart )
345 {
346     int ret;
347     mbedtls_ecdh_context srv, cli;
348     unsigned char buf[1000];
349     const unsigned char *vbuf;
350     size_t len;
351     unsigned char z[MBEDTLS_ECP_MAX_BYTES];
352     size_t z_len;
353     unsigned char rnd_buf_A[MBEDTLS_ECP_MAX_BYTES];
354     unsigned char rnd_buf_B[MBEDTLS_ECP_MAX_BYTES];
355     rnd_buf_info rnd_info_A, rnd_info_B;
356     int cnt_restart;
357     mbedtls_ecp_group grp;
358
359     mbedtls_ecp_group_init( &grp );
360     mbedtls_ecdh_init( &srv );
361     mbedtls_ecdh_init( &cli );
362
363     z_len = unhexify( z, z_str );
364
365     rnd_info_A.buf = rnd_buf_A;
366     rnd_info_A.length = unhexify( rnd_buf_A, dA_str );
367
368     rnd_info_B.buf = rnd_buf_B;
369     rnd_info_B.length = unhexify( rnd_buf_B, dB_str );
370
371     /* The ECDH context is not guaranteed ot have an mbedtls_ecp_group structure
372      * in every configuration, therefore we load it separately. */
373     TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
374
375     /* Otherwise we would have to fix the random buffer,
376      * as in ecdh_primitive_testvec. */
377     TEST_ASSERT( grp.nbits % 8 == 0 );
378
379     TEST_ASSERT( mbedtls_ecdh_setup( &srv, id ) == 0 );
380
381     /* set up restart parameters */
382     mbedtls_ecp_set_max_ops( max_ops );
383
384     if( enable )
385     {
386         mbedtls_ecdh_enable_restart( &srv );
387         mbedtls_ecdh_enable_restart( &cli );
388     }
389
390     /* server writes its paramaters */
391     memset( buf, 0x00, sizeof( buf ) );
392     len = 0;
393
394     cnt_restart = 0;
395     do {
396         ret = mbedtls_ecdh_make_params( &srv, &len, buf, sizeof( buf ),
397                                         rnd_buffer_rand, &rnd_info_A );
398     } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
399
400     TEST_ASSERT( ret == 0 );
401     TEST_ASSERT( cnt_restart >= min_restart );
402     TEST_ASSERT( cnt_restart <= max_restart );
403
404     /* client read server params */
405     vbuf = buf;
406     TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
407
408     /* client writes its key share */
409     memset( buf, 0x00, sizeof( buf ) );
410     len = 0;
411
412     cnt_restart = 0;
413     do {
414         ret = mbedtls_ecdh_make_public( &cli, &len, buf, sizeof( buf ),
415                                         rnd_buffer_rand, &rnd_info_B );
416     } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
417
418     TEST_ASSERT( ret == 0 );
419     TEST_ASSERT( cnt_restart >= min_restart );
420     TEST_ASSERT( cnt_restart <= max_restart );
421
422     /* server reads client key share */
423     TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
424
425     /* server computes shared secret */
426     memset( buf, 0, sizeof( buf ) );
427     len = 0;
428
429     cnt_restart = 0;
430     do {
431         ret = mbedtls_ecdh_calc_secret( &srv, &len, buf, sizeof( buf ),
432                                               NULL, NULL );
433     } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
434
435     TEST_ASSERT( ret == 0 );
436     TEST_ASSERT( cnt_restart >= min_restart );
437     TEST_ASSERT( cnt_restart <= max_restart );
438
439     TEST_ASSERT( len == z_len );
440     TEST_ASSERT( memcmp( buf, z, len ) == 0 );
441
442     /* client computes shared secret */
443     memset( buf, 0, sizeof( buf ) );
444     len = 0;
445
446     cnt_restart = 0;
447     do {
448         ret = mbedtls_ecdh_calc_secret( &cli, &len, buf, sizeof( buf ),
449                                               NULL, NULL );
450     } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
451
452     TEST_ASSERT( ret == 0 );
453     TEST_ASSERT( cnt_restart >= min_restart );
454     TEST_ASSERT( cnt_restart <= max_restart );
455
456     TEST_ASSERT( len == z_len );
457     TEST_ASSERT( memcmp( buf, z, len ) == 0 );
458
459 exit:
460     mbedtls_ecp_group_free( &grp );
461     mbedtls_ecdh_free( &srv );
462     mbedtls_ecdh_free( &cli );
463 }
464 /* END_CASE */
465
466 /* BEGIN_CASE depends_on:MBEDTLS_ECDH_LEGACY_CONTEXT */
467 void ecdh_exchange_legacy( int id )
468 {
469     mbedtls_ecdh_context srv, cli;
470     unsigned char buf[1000];
471     const unsigned char *vbuf;
472     size_t len;
473
474     rnd_pseudo_info rnd_info;
475
476     mbedtls_ecdh_init( &srv );
477     mbedtls_ecdh_init( &cli );
478     memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
479
480     TEST_ASSERT( mbedtls_ecp_group_load( &srv.grp, id ) == 0 );
481
482     memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
483     TEST_ASSERT( mbedtls_ecdh_make_params( &srv, &len, buf, 1000,
484                                    &rnd_pseudo_rand, &rnd_info ) == 0 );
485     TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
486
487     memset( buf, 0x00, sizeof( buf ) );
488     TEST_ASSERT( mbedtls_ecdh_make_public( &cli, &len, buf, 1000,
489                                            &rnd_pseudo_rand, &rnd_info ) == 0 );
490     TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
491
492     TEST_ASSERT( mbedtls_ecdh_calc_secret( &srv, &len, buf, 1000,
493                                            &rnd_pseudo_rand, &rnd_info ) == 0 );
494     TEST_ASSERT( mbedtls_ecdh_calc_secret( &cli, &len, buf, 1000, NULL,
495                                            NULL ) == 0 );
496     TEST_ASSERT( mbedtls_mpi_cmp_mpi( &srv.z, &cli.z ) == 0 );
497
498 exit:
499     mbedtls_ecdh_free( &srv );
500     mbedtls_ecdh_free( &cli );
501 }
502 /* END_CASE */
503
504 /* BEGIN_CASE */
505 void ecdh_exchange_calc_secret( int grp_id,
506                                 data_t *our_private_key,
507                                 data_t *their_point,
508                                 int ours_first,
509                                 data_t *expected )
510 {
511     rnd_pseudo_info rnd_info;
512     mbedtls_ecp_keypair our_key;
513     mbedtls_ecp_keypair their_key;
514     mbedtls_ecdh_context ecdh;
515     unsigned char shared_secret[MBEDTLS_ECP_MAX_BYTES];
516     size_t shared_secret_length = 0;
517
518     memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
519     mbedtls_ecdh_init( &ecdh );
520     mbedtls_ecp_keypair_init( &our_key );
521     mbedtls_ecp_keypair_init( &their_key );
522
523     if( ! load_private_key( grp_id, our_private_key, &our_key, &rnd_info ) )
524         goto exit;
525     if( ! load_public_key( grp_id, their_point, &their_key ) )
526         goto exit;
527
528     /* Import the keys to the ECDH calculation. */
529     if( ours_first )
530     {
531         TEST_ASSERT( mbedtls_ecdh_get_params(
532                          &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
533         TEST_ASSERT( mbedtls_ecdh_get_params(
534                          &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
535     }
536     else
537     {
538         TEST_ASSERT( mbedtls_ecdh_get_params(
539                          &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
540         TEST_ASSERT( mbedtls_ecdh_get_params(
541                          &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
542     }
543
544     /* Perform the ECDH calculation. */
545     TEST_ASSERT( mbedtls_ecdh_calc_secret(
546                      &ecdh,
547                      &shared_secret_length,
548                      shared_secret, sizeof( shared_secret ),
549                      &rnd_pseudo_rand, &rnd_info ) == 0 );
550     TEST_ASSERT( shared_secret_length == expected->len );
551     TEST_ASSERT( memcmp( expected->x, shared_secret,
552                          shared_secret_length ) == 0 );
553
554 exit:
555     mbedtls_ecdh_free( &ecdh );
556     mbedtls_ecp_keypair_free( &our_key );
557     mbedtls_ecp_keypair_free( &their_key );
558 }
559 /* END_CASE */
560
561 /* BEGIN_CASE */
562 void ecdh_exchange_get_params_fail( int our_grp_id,
563                                     data_t *our_private_key,
564                                     int their_grp_id,
565                                     data_t *their_point,
566                                     int ours_first,
567                                     int expected_ret )
568 {
569     rnd_pseudo_info rnd_info;
570     mbedtls_ecp_keypair our_key;
571     mbedtls_ecp_keypair their_key;
572     mbedtls_ecdh_context ecdh;
573
574     memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
575     mbedtls_ecdh_init( &ecdh );
576     mbedtls_ecp_keypair_init( &our_key );
577     mbedtls_ecp_keypair_init( &their_key );
578
579     if( ! load_private_key( our_grp_id, our_private_key, &our_key, &rnd_info ) )
580         goto exit;
581     if( ! load_public_key( their_grp_id, their_point, &their_key ) )
582         goto exit;
583
584     if( ours_first )
585     {
586         TEST_ASSERT( mbedtls_ecdh_get_params(
587                          &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
588         TEST_ASSERT( mbedtls_ecdh_get_params(
589                          &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) ==
590                      expected_ret );
591     }
592     else
593     {
594         TEST_ASSERT( mbedtls_ecdh_get_params(
595                          &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
596         TEST_ASSERT( mbedtls_ecdh_get_params(
597                          &ecdh, &our_key, MBEDTLS_ECDH_OURS ) ==
598                      expected_ret );
599     }
600
601 exit:
602     mbedtls_ecdh_free( &ecdh );
603     mbedtls_ecp_keypair_free( &our_key );
604     mbedtls_ecp_keypair_free( &their_key );
605 }
606 /* END_CASE */