blob: 6e8459dcb3ad1a2a52b782443067976903cf4228 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/ecdh.h"
Gilles Peskine552563b2018-11-07 22:07:58 +01003
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,
Ronald Cron351f0ee2020-06-10 12:12:18 +020022 mbedtls_test_rnd_pseudo_info *rnd_info )
Gilles Peskine552563b2018-11-07 22:07:58 +010023{
24 int ok = 0;
Janos Follath171a7ef2019-02-15 16:17:45 +000025 TEST_ASSERT( mbedtls_ecp_read_key( grp_id, ecp,
26 private_key->x,
27 private_key->len ) == 0 );
Gilles Peskine552563b2018-11-07 22:07:58 +010028 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,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +020032 &mbedtls_test_rnd_pseudo_rand,
33 rnd_info ) == 0 );
Gilles Peskine552563b2018-11-07 22:07:58 +010034 ok = 1;
35exit:
36 return( ok );
37}
38
Paul Bakker33b43f12013-08-20 11:48:36 +020039/* END_HEADER */
Manuel Pégourié-Gonnard61ce13b2013-01-26 16:20:32 +010040
Paul Bakker33b43f12013-08-20 11:48:36 +020041/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 * depends_on:MBEDTLS_ECDH_C
Paul Bakker33b43f12013-08-20 11:48:36 +020043 * END_DEPENDENCIES
44 */
Manuel Pégourié-Gonnard61ce13b2013-01-26 16:20:32 +010045
TRodziewicz062f3532021-05-25 15:15:57 +020046/* BEGIN_CASE depends_on:NOT_DEFINED */
Hanno Becker4c818482018-12-17 18:32:22 +000047void ecdh_invalid_param( )
48{
Hanno Becker4c818482018-12-17 18:32:22 +000049 mbedtls_ecdh_context ctx;
Hanno Becker4c818482018-12-17 18:32:22 +000050 mbedtls_ecp_keypair kp;
Hanno Becker4c818482018-12-17 18:32:22 +000051 int invalid_side = 42;
Hanno Becker4c818482018-12-17 18:32:22 +000052
Ronald Cron875b5fb2021-05-21 08:50:00 +020053 TEST_EQUAL( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
Hanno Becker4c818482018-12-17 18:32:22 +000054 mbedtls_ecdh_get_params( &ctx, &kp,
55 invalid_side ) );
56
Hanno Becker4c818482018-12-17 18:32:22 +000057exit:
58 return;
59}
60/* END_CASE */
61
62/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020063void ecdh_primitive_random( int id )
Manuel Pégourié-Gonnard61ce13b2013-01-26 16:20:32 +010064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_ecp_group grp;
66 mbedtls_ecp_point qA, qB;
67 mbedtls_mpi dA, dB, zA, zB;
Ronald Cron351f0ee2020-06-10 12:12:18 +020068 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard61ce13b2013-01-26 16:20:32 +010069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_ecp_group_init( &grp );
71 mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB );
72 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB );
73 mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB );
Ronald Cron351f0ee2020-06-10 12:12:18 +020074 memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
Manuel Pégourié-Gonnard61ce13b2013-01-26 16:20:32 +010075
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +020076 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnard61ce13b2013-01-26 16:20:32 +010077
Ronald Cron6c5bd7f2020-06-10 14:08:26 +020078 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA,
79 &mbedtls_test_rnd_pseudo_rand,
80 &rnd_info ) == 0 );
81 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB,
82 &mbedtls_test_rnd_pseudo_rand,
83 &rnd_info ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +020085 &mbedtls_test_rnd_pseudo_rand,
86 &rnd_info ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020088 NULL, NULL ) == 0 );
Manuel Pégourié-Gonnard61ce13b2013-01-26 16:20:32 +010089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &zB ) == 0 );
Manuel Pégourié-Gonnard61ce13b2013-01-26 16:20:32 +010091
Paul Bakkerbd51b262014-07-10 15:26:12 +020092exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_ecp_group_free( &grp );
94 mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB );
95 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB );
96 mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB );
Manuel Pégourié-Gonnard61ce13b2013-01-26 16:20:32 +010097}
Paul Bakker33b43f12013-08-20 11:48:36 +020098/* END_CASE */
Manuel Pégourié-Gonnard007b7172013-01-27 08:56:21 +010099
Paul Bakker33b43f12013-08-20 11:48:36 +0200100/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100101void ecdh_primitive_testvec( int id, data_t * rnd_buf_A, char * xA_str,
102 char * yA_str, data_t * rnd_buf_B,
Azim Khand30ca132017-06-09 04:32:58 +0100103 char * xB_str, char * yB_str, char * z_str )
Manuel Pégourié-Gonnard007b7172013-01-27 08:56:21 +0100104{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_ecp_group grp;
106 mbedtls_ecp_point qA, qB;
107 mbedtls_mpi dA, dB, zA, zB, check;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200108 mbedtls_test_rnd_buf_info rnd_info_A, rnd_info_B;
Manuel Pégourié-Gonnard007b7172013-01-27 08:56:21 +0100109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_ecp_group_init( &grp );
111 mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB );
112 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB );
113 mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB ); mbedtls_mpi_init( &check );
Manuel Pégourié-Gonnard007b7172013-01-27 08:56:21 +0100114
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200115 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnard007b7172013-01-27 08:56:21 +0100116
Azim Khand30ca132017-06-09 04:32:58 +0100117 rnd_info_A.buf = rnd_buf_A->x;
118 rnd_info_A.length = rnd_buf_A->len;
Gilles Peskineecacc3c2021-03-24 00:48:57 +0100119 rnd_info_A.fallback_f_rng = mbedtls_test_rnd_std_rand;
120 rnd_info_A.fallback_p_rng = NULL;
Manuel Pégourié-Gonnard544416a2014-01-23 16:55:18 +0100121
Azim Khand30ca132017-06-09 04:32:58 +0100122 /* Fix rnd_buf_A->x by shifting it left if necessary */
Manuel Pégourié-Gonnard544416a2014-01-23 16:55:18 +0100123 if( grp.nbits % 8 != 0 )
124 {
125 unsigned char shift = 8 - ( grp.nbits % 8 );
126 size_t i;
127
128 for( i = 0; i < rnd_info_A.length - 1; i++ )
Azim Khand30ca132017-06-09 04:32:58 +0100129 rnd_buf_A->x[i] = rnd_buf_A->x[i] << shift
130 | rnd_buf_A->x[i+1] >> ( 8 - shift );
Manuel Pégourié-Gonnard544416a2014-01-23 16:55:18 +0100131
Azim Khand30ca132017-06-09 04:32:58 +0100132 rnd_buf_A->x[rnd_info_A.length-1] <<= shift;
Manuel Pégourié-Gonnard544416a2014-01-23 16:55:18 +0100133 }
134
Azim Khand30ca132017-06-09 04:32:58 +0100135 rnd_info_B.buf = rnd_buf_B->x;
136 rnd_info_B.length = rnd_buf_B->len;
Gilles Peskineecacc3c2021-03-24 00:48:57 +0100137 rnd_info_B.fallback_f_rng = mbedtls_test_rnd_std_rand;
138 rnd_info_B.fallback_p_rng = NULL;
Manuel Pégourié-Gonnard544416a2014-01-23 16:55:18 +0100139
Azim Khand30ca132017-06-09 04:32:58 +0100140 /* Fix rnd_buf_B->x by shifting it left if necessary */
Manuel Pégourié-Gonnard544416a2014-01-23 16:55:18 +0100141 if( grp.nbits % 8 != 0 )
142 {
143 unsigned char shift = 8 - ( grp.nbits % 8 );
144 size_t i;
145
146 for( i = 0; i < rnd_info_B.length - 1; i++ )
Azim Khand30ca132017-06-09 04:32:58 +0100147 rnd_buf_B->x[i] = rnd_buf_B->x[i] << shift
148 | rnd_buf_B->x[i+1] >> ( 8 - shift );
Manuel Pégourié-Gonnard544416a2014-01-23 16:55:18 +0100149
Azim Khand30ca132017-06-09 04:32:58 +0100150 rnd_buf_B->x[rnd_info_B.length-1] <<= shift;
Manuel Pégourié-Gonnard544416a2014-01-23 16:55:18 +0100151 }
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200154 mbedtls_test_rnd_buffer_rand,
155 &rnd_info_A ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 TEST_ASSERT( ! mbedtls_ecp_is_zero( &qA ) );
157 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, xA_str ) == 0 );
158 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.X, &check ) == 0 );
159 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, yA_str ) == 0 );
160 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.Y, &check ) == 0 );
Manuel Pégourié-Gonnard007b7172013-01-27 08:56:21 +0100161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200163 mbedtls_test_rnd_buffer_rand,
164 &rnd_info_B ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 TEST_ASSERT( ! mbedtls_ecp_is_zero( &qB ) );
166 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, xB_str ) == 0 );
167 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.X, &check ) == 0 );
168 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, yB_str ) == 0 );
169 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.Y, &check ) == 0 );
Manuel Pégourié-Gonnard007b7172013-01-27 08:56:21 +0100170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, z_str ) == 0 );
172 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA, NULL, NULL ) == 0 );
173 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &check ) == 0 );
174 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB, NULL, NULL ) == 0 );
175 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zB, &check ) == 0 );
Manuel Pégourié-Gonnard007b7172013-01-27 08:56:21 +0100176
Paul Bakkerbd51b262014-07-10 15:26:12 +0200177exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_ecp_group_free( &grp );
179 mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB );
180 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB );
181 mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB ); mbedtls_mpi_free( &check );
Manuel Pégourié-Gonnard007b7172013-01-27 08:56:21 +0100182}
Paul Bakker33b43f12013-08-20 11:48:36 +0200183/* END_CASE */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100184
Paul Bakker33b43f12013-08-20 11:48:36 +0200185/* BEGIN_CASE */
186void ecdh_exchange( int id )
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_ecdh_context srv, cli;
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100189 unsigned char buf[1000];
190 const unsigned char *vbuf;
191 size_t len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200192 mbedtls_test_rnd_pseudo_info rnd_info;
Janos Follath36c5f7f2018-10-30 14:08:52 +0000193 unsigned char res_buf[1000];
194 size_t res_len;
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_ecdh_init( &srv );
197 mbedtls_ecdh_init( &cli );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200198 memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100199
Janos Follathfc03e8d2018-10-04 17:17:54 +0100200 TEST_ASSERT( mbedtls_ecdh_setup( &srv, id ) == 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100201
202 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 TEST_ASSERT( mbedtls_ecdh_make_params( &srv, &len, buf, 1000,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200204 &mbedtls_test_rnd_pseudo_rand,
205 &rnd_info ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100207
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100208 memset( buf, 0x00, sizeof( buf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 TEST_ASSERT( mbedtls_ecdh_make_public( &cli, &len, buf, 1000,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200210 &mbedtls_test_rnd_pseudo_rand,
211 &rnd_info ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 TEST_ASSERT( mbedtls_ecdh_calc_secret( &srv, &len, buf, 1000,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200215 &mbedtls_test_rnd_pseudo_rand,
216 &rnd_info ) == 0 );
Janos Follath36c5f7f2018-10-30 14:08:52 +0000217 TEST_ASSERT( mbedtls_ecdh_calc_secret( &cli, &res_len, res_buf, 1000,
218 NULL, NULL ) == 0 );
219 TEST_ASSERT( len == res_len );
220 TEST_ASSERT( memcmp( buf, res_buf, len ) == 0 );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100221
Paul Bakkerbd51b262014-07-10 15:26:12 +0200222exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_ecdh_free( &srv );
224 mbedtls_ecdh_free( &cli );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100225}
Paul Bakker33b43f12013-08-20 11:48:36 +0200226/* END_CASE */
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200227
228/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Ronald Cron9ed40732020-06-25 09:03:34 +0200229void ecdh_restart( int id, data_t *dA, data_t *dB, data_t *z,
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200230 int enable, int max_ops, int min_restart, int max_restart )
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200231{
232 int ret;
233 mbedtls_ecdh_context srv, cli;
234 unsigned char buf[1000];
235 const unsigned char *vbuf;
236 size_t len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200237 mbedtls_test_rnd_buf_info rnd_info_A, rnd_info_B;
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200238 int cnt_restart;
Janos Follath36c5f7f2018-10-30 14:08:52 +0000239 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200240
Janos Follath36c5f7f2018-10-30 14:08:52 +0000241 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200242 mbedtls_ecdh_init( &srv );
243 mbedtls_ecdh_init( &cli );
244
Gilles Peskineecacc3c2021-03-24 00:48:57 +0100245 rnd_info_A.fallback_f_rng = mbedtls_test_rnd_std_rand;
246 rnd_info_A.fallback_p_rng = NULL;
Ronald Cron9ed40732020-06-25 09:03:34 +0200247 rnd_info_A.buf = dA->x;
248 rnd_info_A.length = dA->len;
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200249
Gilles Peskineecacc3c2021-03-24 00:48:57 +0100250 rnd_info_B.fallback_f_rng = mbedtls_test_rnd_std_rand;
251 rnd_info_B.fallback_p_rng = NULL;
Ronald Cron9ed40732020-06-25 09:03:34 +0200252 rnd_info_B.buf = dB->x;
253 rnd_info_B.length = dB->len;
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200254
Janos Follath36c5f7f2018-10-30 14:08:52 +0000255 /* The ECDH context is not guaranteed ot have an mbedtls_ecp_group structure
256 * in every configuration, therefore we load it separately. */
257 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200258
Janos Follath36c5f7f2018-10-30 14:08:52 +0000259 /* Otherwise we would have to fix the random buffer,
260 * as in ecdh_primitive_testvec. */
261 TEST_ASSERT( grp.nbits % 8 == 0 );
262
263 TEST_ASSERT( mbedtls_ecdh_setup( &srv, id ) == 0 );
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200264
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200265 /* set up restart parameters */
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200266 mbedtls_ecp_set_max_ops( max_ops );
267
Janos Follath36c5f7f2018-10-30 14:08:52 +0000268 if( enable )
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200269 {
270 mbedtls_ecdh_enable_restart( &srv );
271 mbedtls_ecdh_enable_restart( &cli );
272 }
273
Antonin Décimo36e89b52019-01-23 15:24:37 +0100274 /* server writes its parameters */
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200275 memset( buf, 0x00, sizeof( buf ) );
276 len = 0;
277
278 cnt_restart = 0;
279 do {
280 ret = mbedtls_ecdh_make_params( &srv, &len, buf, sizeof( buf ),
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200281 mbedtls_test_rnd_buffer_rand,
282 &rnd_info_A );
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200283 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
284
285 TEST_ASSERT( ret == 0 );
286 TEST_ASSERT( cnt_restart >= min_restart );
287 TEST_ASSERT( cnt_restart <= max_restart );
288
289 /* client read server params */
290 vbuf = buf;
291 TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
292
293 /* client writes its key share */
294 memset( buf, 0x00, sizeof( buf ) );
295 len = 0;
296
297 cnt_restart = 0;
298 do {
299 ret = mbedtls_ecdh_make_public( &cli, &len, buf, sizeof( buf ),
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200300 mbedtls_test_rnd_buffer_rand,
301 &rnd_info_B );
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200302 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
303
304 TEST_ASSERT( ret == 0 );
305 TEST_ASSERT( cnt_restart >= min_restart );
306 TEST_ASSERT( cnt_restart <= max_restart );
307
308 /* server reads client key share */
309 TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
310
311 /* server computes shared secret */
312 memset( buf, 0, sizeof( buf ) );
313 len = 0;
314
315 cnt_restart = 0;
316 do {
317 ret = mbedtls_ecdh_calc_secret( &srv, &len, buf, sizeof( buf ),
318 NULL, NULL );
319 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
320
321 TEST_ASSERT( ret == 0 );
322 TEST_ASSERT( cnt_restart >= min_restart );
323 TEST_ASSERT( cnt_restart <= max_restart );
324
Ronald Cron9ed40732020-06-25 09:03:34 +0200325 TEST_ASSERT( len == z->len );
326 TEST_ASSERT( memcmp( buf, z->x, len ) == 0 );
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200327
328 /* client computes shared secret */
329 memset( buf, 0, sizeof( buf ) );
330 len = 0;
331
332 cnt_restart = 0;
333 do {
334 ret = mbedtls_ecdh_calc_secret( &cli, &len, buf, sizeof( buf ),
335 NULL, NULL );
336 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
337
338 TEST_ASSERT( ret == 0 );
339 TEST_ASSERT( cnt_restart >= min_restart );
340 TEST_ASSERT( cnt_restart <= max_restart );
341
Ronald Cron9ed40732020-06-25 09:03:34 +0200342 TEST_ASSERT( len == z->len );
343 TEST_ASSERT( memcmp( buf, z->x, len ) == 0 );
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200344
345exit:
Janos Follath36c5f7f2018-10-30 14:08:52 +0000346 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard71b2c532017-04-27 10:38:52 +0200347 mbedtls_ecdh_free( &srv );
348 mbedtls_ecdh_free( &cli );
349}
350/* END_CASE */
Janos Follathfc03e8d2018-10-04 17:17:54 +0100351
Gilles Peskine552563b2018-11-07 22:07:58 +0100352/* BEGIN_CASE */
353void ecdh_exchange_calc_secret( int grp_id,
354 data_t *our_private_key,
355 data_t *their_point,
356 int ours_first,
357 data_t *expected )
358{
Ronald Cron351f0ee2020-06-10 12:12:18 +0200359 mbedtls_test_rnd_pseudo_info rnd_info;
Gilles Peskine552563b2018-11-07 22:07:58 +0100360 mbedtls_ecp_keypair our_key;
361 mbedtls_ecp_keypair their_key;
362 mbedtls_ecdh_context ecdh;
363 unsigned char shared_secret[MBEDTLS_ECP_MAX_BYTES];
364 size_t shared_secret_length = 0;
365
Ronald Cron351f0ee2020-06-10 12:12:18 +0200366 memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
Gilles Peskine552563b2018-11-07 22:07:58 +0100367 mbedtls_ecdh_init( &ecdh );
368 mbedtls_ecp_keypair_init( &our_key );
369 mbedtls_ecp_keypair_init( &their_key );
370
371 if( ! load_private_key( grp_id, our_private_key, &our_key, &rnd_info ) )
372 goto exit;
373 if( ! load_public_key( grp_id, their_point, &their_key ) )
374 goto exit;
375
376 /* Import the keys to the ECDH calculation. */
377 if( ours_first )
378 {
379 TEST_ASSERT( mbedtls_ecdh_get_params(
380 &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
381 TEST_ASSERT( mbedtls_ecdh_get_params(
382 &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
383 }
384 else
385 {
386 TEST_ASSERT( mbedtls_ecdh_get_params(
387 &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
388 TEST_ASSERT( mbedtls_ecdh_get_params(
389 &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
390 }
391
392 /* Perform the ECDH calculation. */
393 TEST_ASSERT( mbedtls_ecdh_calc_secret(
394 &ecdh,
395 &shared_secret_length,
396 shared_secret, sizeof( shared_secret ),
Ronald Cron351f0ee2020-06-10 12:12:18 +0200397 &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
Gilles Peskine552563b2018-11-07 22:07:58 +0100398 TEST_ASSERT( shared_secret_length == expected->len );
399 TEST_ASSERT( memcmp( expected->x, shared_secret,
400 shared_secret_length ) == 0 );
401
402exit:
403 mbedtls_ecdh_free( &ecdh );
404 mbedtls_ecp_keypair_free( &our_key );
405 mbedtls_ecp_keypair_free( &their_key );
406}
407/* END_CASE */
Gilles Peskinec4dff062018-11-07 22:09:29 +0100408
409/* BEGIN_CASE */
410void ecdh_exchange_get_params_fail( int our_grp_id,
411 data_t *our_private_key,
412 int their_grp_id,
413 data_t *their_point,
414 int ours_first,
415 int expected_ret )
416{
Ronald Cron351f0ee2020-06-10 12:12:18 +0200417 mbedtls_test_rnd_pseudo_info rnd_info;
Gilles Peskinec4dff062018-11-07 22:09:29 +0100418 mbedtls_ecp_keypair our_key;
419 mbedtls_ecp_keypair their_key;
420 mbedtls_ecdh_context ecdh;
421
Ronald Cron351f0ee2020-06-10 12:12:18 +0200422 memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
Gilles Peskinec4dff062018-11-07 22:09:29 +0100423 mbedtls_ecdh_init( &ecdh );
424 mbedtls_ecp_keypair_init( &our_key );
425 mbedtls_ecp_keypair_init( &their_key );
426
427 if( ! load_private_key( our_grp_id, our_private_key, &our_key, &rnd_info ) )
428 goto exit;
429 if( ! load_public_key( their_grp_id, their_point, &their_key ) )
430 goto exit;
431
432 if( ours_first )
433 {
434 TEST_ASSERT( mbedtls_ecdh_get_params(
435 &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
436 TEST_ASSERT( mbedtls_ecdh_get_params(
437 &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) ==
438 expected_ret );
439 }
440 else
441 {
442 TEST_ASSERT( mbedtls_ecdh_get_params(
443 &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
444 TEST_ASSERT( mbedtls_ecdh_get_params(
445 &ecdh, &our_key, MBEDTLS_ECDH_OURS ) ==
446 expected_ret );
447 }
448
449exit:
450 mbedtls_ecdh_free( &ecdh );
451 mbedtls_ecp_keypair_free( &our_key );
452 mbedtls_ecp_keypair_free( &their_key );
453}
454/* END_CASE */