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