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 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; 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 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 183exit: 184 return; 185} 186/* END_CASE */ 187 188/* BEGIN_CASE */ 189void 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 215exit: 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 */ 224void 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_test_read_mpi( &check, 16, xA_str ) == 0 ); 276 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.X, &check ) == 0 ); 277 TEST_ASSERT( mbedtls_test_read_mpi( &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_test_read_mpi( &check, 16, xB_str ) == 0 ); 284 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.X, &check ) == 0 ); 285 TEST_ASSERT( mbedtls_test_read_mpi( &check, 16, yB_str ) == 0 ); 286 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.Y, &check ) == 0 ); 287 288 TEST_ASSERT( mbedtls_test_read_mpi( &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 294exit: 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 */ 303void 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 336exit: 337 mbedtls_ecdh_free( &srv ); 338 mbedtls_ecdh_free( &cli ); 339} 340/* END_CASE */ 341 342/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ 343void ecdh_restart( int id, data_t *dA, data_t *dB, data_t *z, 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 rnd_buf_info rnd_info_A, rnd_info_B; 352 int cnt_restart; 353 mbedtls_ecp_group grp; 354 355 mbedtls_ecp_group_init( &grp ); 356 mbedtls_ecdh_init( &srv ); 357 mbedtls_ecdh_init( &cli ); 358 359 rnd_info_A.buf = dA->x; 360 rnd_info_A.length = dA->len; 361 362 rnd_info_B.buf = dB->x; 363 rnd_info_B.length = dB->len; 364 365 /* The ECDH context is not guaranteed ot have an mbedtls_ecp_group structure 366 * in every configuration, therefore we load it separately. */ 367 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); 368 369 /* Otherwise we would have to fix the random buffer, 370 * as in ecdh_primitive_testvec. */ 371 TEST_ASSERT( grp.nbits % 8 == 0 ); 372 373 TEST_ASSERT( mbedtls_ecdh_setup( &srv, id ) == 0 ); 374 375 /* set up restart parameters */ 376 mbedtls_ecp_set_max_ops( max_ops ); 377 378 if( enable ) 379 { 380 mbedtls_ecdh_enable_restart( &srv ); 381 mbedtls_ecdh_enable_restart( &cli ); 382 } 383 384 /* server writes its parameters */ 385 memset( buf, 0x00, sizeof( buf ) ); 386 len = 0; 387 388 cnt_restart = 0; 389 do { 390 ret = mbedtls_ecdh_make_params( &srv, &len, buf, sizeof( buf ), 391 rnd_buffer_rand, &rnd_info_A ); 392 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart ); 393 394 TEST_ASSERT( ret == 0 ); 395 TEST_ASSERT( cnt_restart >= min_restart ); 396 TEST_ASSERT( cnt_restart <= max_restart ); 397 398 /* client read server params */ 399 vbuf = buf; 400 TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 ); 401 402 /* client writes its key share */ 403 memset( buf, 0x00, sizeof( buf ) ); 404 len = 0; 405 406 cnt_restart = 0; 407 do { 408 ret = mbedtls_ecdh_make_public( &cli, &len, buf, sizeof( buf ), 409 rnd_buffer_rand, &rnd_info_B ); 410 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart ); 411 412 TEST_ASSERT( ret == 0 ); 413 TEST_ASSERT( cnt_restart >= min_restart ); 414 TEST_ASSERT( cnt_restart <= max_restart ); 415 416 /* server reads client key share */ 417 TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 ); 418 419 /* server computes shared secret */ 420 memset( buf, 0, sizeof( buf ) ); 421 len = 0; 422 423 cnt_restart = 0; 424 do { 425 ret = mbedtls_ecdh_calc_secret( &srv, &len, buf, sizeof( buf ), 426 NULL, NULL ); 427 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart ); 428 429 TEST_ASSERT( ret == 0 ); 430 TEST_ASSERT( cnt_restart >= min_restart ); 431 TEST_ASSERT( cnt_restart <= max_restart ); 432 433 TEST_ASSERT( len == z->len ); 434 TEST_ASSERT( memcmp( buf, z->x, len ) == 0 ); 435 436 /* client computes shared secret */ 437 memset( buf, 0, sizeof( buf ) ); 438 len = 0; 439 440 cnt_restart = 0; 441 do { 442 ret = mbedtls_ecdh_calc_secret( &cli, &len, buf, sizeof( buf ), 443 NULL, NULL ); 444 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart ); 445 446 TEST_ASSERT( ret == 0 ); 447 TEST_ASSERT( cnt_restart >= min_restart ); 448 TEST_ASSERT( cnt_restart <= max_restart ); 449 450 TEST_ASSERT( len == z->len ); 451 TEST_ASSERT( memcmp( buf, z->x, len ) == 0 ); 452 453exit: 454 mbedtls_ecp_group_free( &grp ); 455 mbedtls_ecdh_free( &srv ); 456 mbedtls_ecdh_free( &cli ); 457} 458/* END_CASE */ 459 460/* BEGIN_CASE depends_on:MBEDTLS_ECDH_LEGACY_CONTEXT */ 461void ecdh_exchange_legacy( int id ) 462{ 463 mbedtls_ecdh_context srv, cli; 464 unsigned char buf[1000]; 465 const unsigned char *vbuf; 466 size_t len; 467 468 rnd_pseudo_info rnd_info; 469 470 mbedtls_ecdh_init( &srv ); 471 mbedtls_ecdh_init( &cli ); 472 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) ); 473 474 TEST_ASSERT( mbedtls_ecp_group_load( &srv.grp, id ) == 0 ); 475 476 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; 477 TEST_ASSERT( mbedtls_ecdh_make_params( &srv, &len, buf, 1000, 478 &rnd_pseudo_rand, &rnd_info ) == 0 ); 479 TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 ); 480 481 memset( buf, 0x00, sizeof( buf ) ); 482 TEST_ASSERT( mbedtls_ecdh_make_public( &cli, &len, buf, 1000, 483 &rnd_pseudo_rand, &rnd_info ) == 0 ); 484 TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 ); 485 486 TEST_ASSERT( mbedtls_ecdh_calc_secret( &srv, &len, buf, 1000, 487 &rnd_pseudo_rand, &rnd_info ) == 0 ); 488 TEST_ASSERT( mbedtls_ecdh_calc_secret( &cli, &len, buf, 1000, NULL, 489 NULL ) == 0 ); 490 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &srv.z, &cli.z ) == 0 ); 491 492exit: 493 mbedtls_ecdh_free( &srv ); 494 mbedtls_ecdh_free( &cli ); 495} 496/* END_CASE */ 497 498/* BEGIN_CASE */ 499void ecdh_exchange_calc_secret( int grp_id, 500 data_t *our_private_key, 501 data_t *their_point, 502 int ours_first, 503 data_t *expected ) 504{ 505 rnd_pseudo_info rnd_info; 506 mbedtls_ecp_keypair our_key; 507 mbedtls_ecp_keypair their_key; 508 mbedtls_ecdh_context ecdh; 509 unsigned char shared_secret[MBEDTLS_ECP_MAX_BYTES]; 510 size_t shared_secret_length = 0; 511 512 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) ); 513 mbedtls_ecdh_init( &ecdh ); 514 mbedtls_ecp_keypair_init( &our_key ); 515 mbedtls_ecp_keypair_init( &their_key ); 516 517 if( ! load_private_key( grp_id, our_private_key, &our_key, &rnd_info ) ) 518 goto exit; 519 if( ! load_public_key( grp_id, their_point, &their_key ) ) 520 goto exit; 521 522 /* Import the keys to the ECDH calculation. */ 523 if( ours_first ) 524 { 525 TEST_ASSERT( mbedtls_ecdh_get_params( 526 &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 ); 527 TEST_ASSERT( mbedtls_ecdh_get_params( 528 &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 ); 529 } 530 else 531 { 532 TEST_ASSERT( mbedtls_ecdh_get_params( 533 &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 ); 534 TEST_ASSERT( mbedtls_ecdh_get_params( 535 &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 ); 536 } 537 538 /* Perform the ECDH calculation. */ 539 TEST_ASSERT( mbedtls_ecdh_calc_secret( 540 &ecdh, 541 &shared_secret_length, 542 shared_secret, sizeof( shared_secret ), 543 &rnd_pseudo_rand, &rnd_info ) == 0 ); 544 TEST_ASSERT( shared_secret_length == expected->len ); 545 TEST_ASSERT( memcmp( expected->x, shared_secret, 546 shared_secret_length ) == 0 ); 547 548exit: 549 mbedtls_ecdh_free( &ecdh ); 550 mbedtls_ecp_keypair_free( &our_key ); 551 mbedtls_ecp_keypair_free( &their_key ); 552} 553/* END_CASE */ 554 555/* BEGIN_CASE */ 556void ecdh_exchange_get_params_fail( int our_grp_id, 557 data_t *our_private_key, 558 int their_grp_id, 559 data_t *their_point, 560 int ours_first, 561 int expected_ret ) 562{ 563 rnd_pseudo_info rnd_info; 564 mbedtls_ecp_keypair our_key; 565 mbedtls_ecp_keypair their_key; 566 mbedtls_ecdh_context ecdh; 567 568 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) ); 569 mbedtls_ecdh_init( &ecdh ); 570 mbedtls_ecp_keypair_init( &our_key ); 571 mbedtls_ecp_keypair_init( &their_key ); 572 573 if( ! load_private_key( our_grp_id, our_private_key, &our_key, &rnd_info ) ) 574 goto exit; 575 if( ! load_public_key( their_grp_id, their_point, &their_key ) ) 576 goto exit; 577 578 if( ours_first ) 579 { 580 TEST_ASSERT( mbedtls_ecdh_get_params( 581 &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 ); 582 TEST_ASSERT( mbedtls_ecdh_get_params( 583 &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 584 expected_ret ); 585 } 586 else 587 { 588 TEST_ASSERT( mbedtls_ecdh_get_params( 589 &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 ); 590 TEST_ASSERT( mbedtls_ecdh_get_params( 591 &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 592 expected_ret ); 593 } 594 595exit: 596 mbedtls_ecdh_free( &ecdh ); 597 mbedtls_ecp_keypair_free( &our_key ); 598 mbedtls_ecp_keypair_free( &their_key ); 599} 600/* END_CASE */ 601