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