2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecdh.h"
|
2018-11-07 22:07:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int load_public_key(int grp_id, data_t *point,
|
|
|
|
mbedtls_ecp_keypair *ecp)
|
2018-11-07 22:07:58 +01:00
|
|
|
{
|
|
|
|
int ok = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&ecp->grp, grp_id) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecp_point_read_binary(&ecp->grp,
|
|
|
|
&ecp->Q,
|
|
|
|
point->x,
|
|
|
|
point->len) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecp_check_pubkey(&ecp->grp,
|
|
|
|
&ecp->Q) == 0);
|
2018-11-07 22:07:58 +01:00
|
|
|
ok = 1;
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ok;
|
2018-11-07 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int load_private_key(int grp_id, data_t *private_key,
|
|
|
|
mbedtls_ecp_keypair *ecp,
|
|
|
|
mbedtls_test_rnd_pseudo_info *rnd_info)
|
2018-11-07 22:07:58 +01:00
|
|
|
{
|
|
|
|
int ok = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecp_read_key(grp_id, ecp,
|
|
|
|
private_key->x,
|
|
|
|
private_key->len) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecp_check_privkey(&ecp->grp, &ecp->d) == 0);
|
2018-11-07 22:07:58 +01:00
|
|
|
/* Calculate the public key from the private key. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecp_mul(&ecp->grp, &ecp->Q, &ecp->d,
|
|
|
|
&ecp->grp.G,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
rnd_info) == 0);
|
2018-11-07 22:07:58 +01:00
|
|
|
ok = 1;
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ok;
|
2018-11-07 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2013-01-26 16:20:32 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_ECDH_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2013-01-26 16:20:32 +01:00
|
|
|
|
2022-07-27 16:19:46 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdh_invalid_param()
|
2018-12-17 19:32:22 +01:00
|
|
|
{
|
|
|
|
mbedtls_ecdh_context ctx;
|
|
|
|
mbedtls_ecp_keypair kp;
|
|
|
|
int invalid_side = 42;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_init(&ctx);
|
|
|
|
mbedtls_ecp_keypair_init(&kp);
|
2022-09-23 15:25:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
|
|
|
|
mbedtls_ecdh_get_params(&ctx, &kp,
|
|
|
|
invalid_side));
|
2018-12-17 19:32:22 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdh_primitive_random(int id)
|
2013-01-26 16:20:32 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point qA, qB;
|
|
|
|
mbedtls_mpi dA, dB, zA, zB;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2013-01-26 16:20:32 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_init(&grp);
|
|
|
|
mbedtls_ecp_point_init(&qA); mbedtls_ecp_point_init(&qB);
|
|
|
|
mbedtls_mpi_init(&dA); mbedtls_mpi_init(&dB);
|
|
|
|
mbedtls_mpi_init(&zA); mbedtls_mpi_init(&zB);
|
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_gen_public(&grp, &dA, &qA,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_gen_public(&grp, &dB, &qB,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_compute_shared(&grp, &zA, &qB, &dA,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_compute_shared(&grp, &zB, &qA, &dB,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&zA, &zB) == 0);
|
2013-01-26 16:20:32 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_free(&grp);
|
|
|
|
mbedtls_ecp_point_free(&qA); mbedtls_ecp_point_free(&qB);
|
|
|
|
mbedtls_mpi_free(&dA); mbedtls_mpi_free(&dB);
|
|
|
|
mbedtls_mpi_free(&zA); mbedtls_mpi_free(&zB);
|
2013-01-26 16:20:32 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-01-27 08:56:21 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdh_primitive_testvec(int id, data_t *rnd_buf_A, char *xA_str,
|
|
|
|
char *yA_str, data_t *rnd_buf_B,
|
|
|
|
char *xB_str, char *yB_str, char *z_str)
|
2013-01-27 08:56:21 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point qA, qB;
|
|
|
|
mbedtls_mpi dA, dB, zA, zB, check;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_buf_info rnd_info_A, rnd_info_B;
|
2021-06-15 11:29:26 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2013-01-27 08:56:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_init(&grp);
|
|
|
|
mbedtls_ecp_point_init(&qA); mbedtls_ecp_point_init(&qB);
|
|
|
|
mbedtls_mpi_init(&dA); mbedtls_mpi_init(&dB);
|
|
|
|
mbedtls_mpi_init(&zA); mbedtls_mpi_init(&zB); mbedtls_mpi_init(&check);
|
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
2013-01-27 08:56:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
|
2013-01-27 08:56:21 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_info_A.buf = rnd_buf_A->x;
|
|
|
|
rnd_info_A.length = rnd_buf_A->len;
|
2021-03-24 00:48:57 +01:00
|
|
|
rnd_info_A.fallback_f_rng = mbedtls_test_rnd_std_rand;
|
|
|
|
rnd_info_A.fallback_p_rng = NULL;
|
2014-01-23 16:55:18 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
/* Fix rnd_buf_A->x by shifting it left if necessary */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (grp.nbits % 8 != 0) {
|
|
|
|
unsigned char shift = 8 - (grp.nbits % 8);
|
2014-01-23 16:55:18 +01:00
|
|
|
size_t i;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < rnd_info_A.length - 1; i++) {
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_buf_A->x[i] = rnd_buf_A->x[i] << shift
|
2023-01-11 14:50:10 +01:00
|
|
|
| rnd_buf_A->x[i+1] >> (8 - shift);
|
|
|
|
}
|
2014-01-23 16:55:18 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_buf_A->x[rnd_info_A.length-1] <<= shift;
|
2014-01-23 16:55:18 +01:00
|
|
|
}
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_info_B.buf = rnd_buf_B->x;
|
|
|
|
rnd_info_B.length = rnd_buf_B->len;
|
2021-03-24 00:48:57 +01:00
|
|
|
rnd_info_B.fallback_f_rng = mbedtls_test_rnd_std_rand;
|
|
|
|
rnd_info_B.fallback_p_rng = NULL;
|
2014-01-23 16:55:18 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
/* Fix rnd_buf_B->x by shifting it left if necessary */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (grp.nbits % 8 != 0) {
|
|
|
|
unsigned char shift = 8 - (grp.nbits % 8);
|
2014-01-23 16:55:18 +01:00
|
|
|
size_t i;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < rnd_info_B.length - 1; i++) {
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_buf_B->x[i] = rnd_buf_B->x[i] << shift
|
2023-01-11 14:50:10 +01:00
|
|
|
| rnd_buf_B->x[i+1] >> (8 - shift);
|
|
|
|
}
|
2014-01-23 16:55:18 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_buf_B->x[rnd_info_B.length-1] <<= shift;
|
2014-01-23 16:55:18 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdh_gen_public(&grp, &dA, &qA,
|
|
|
|
mbedtls_test_rnd_buffer_rand,
|
|
|
|
&rnd_info_A) == 0);
|
|
|
|
TEST_ASSERT(!mbedtls_ecp_is_zero(&qA));
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&check, xA_str) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&qA.X, &check) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&check, yA_str) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&qA.Y, &check) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_gen_public(&grp, &dB, &qB,
|
|
|
|
mbedtls_test_rnd_buffer_rand,
|
|
|
|
&rnd_info_B) == 0);
|
|
|
|
TEST_ASSERT(!mbedtls_ecp_is_zero(&qB));
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&check, xB_str) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&qB.X, &check) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&check, yB_str) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&qB.Y, &check) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&check, z_str) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_compute_shared(&grp, &zA, &qB, &dA,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&zA, &check) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_compute_shared(&grp, &zB, &qA, &dB,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&zB, &check) == 0);
|
2013-01-27 08:56:21 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_free(&grp);
|
|
|
|
mbedtls_ecp_point_free(&qA); mbedtls_ecp_point_free(&qB);
|
|
|
|
mbedtls_mpi_free(&dA); mbedtls_mpi_free(&dB);
|
|
|
|
mbedtls_mpi_free(&zA); mbedtls_mpi_free(&zB); mbedtls_mpi_free(&check);
|
2013-01-27 08:56:21 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-02-11 20:28:55 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdh_exchange(int id)
|
2013-02-11 20:28:55 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecdh_context srv, cli;
|
2013-02-11 20:28:55 +01:00
|
|
|
unsigned char buf[1000];
|
|
|
|
const unsigned char *vbuf;
|
|
|
|
size_t len;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2018-10-30 15:08:52 +01:00
|
|
|
unsigned char res_buf[1000];
|
|
|
|
size_t res_len;
|
2013-02-11 20:28:55 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_init(&srv);
|
|
|
|
mbedtls_ecdh_init(&cli);
|
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_setup(&srv, id) == 0);
|
|
|
|
|
|
|
|
memset(buf, 0x00, sizeof(buf)); vbuf = buf;
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_make_params(&srv, &len, buf, 1000,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_read_params(&cli, &vbuf, buf + len) == 0);
|
|
|
|
|
|
|
|
memset(buf, 0x00, sizeof(buf));
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_make_public(&cli, &len, buf, 1000,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_read_public(&srv, buf, len) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_calc_secret(&srv, &len, buf, 1000,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_calc_secret(&cli, &res_len, res_buf, 1000,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(len == res_len);
|
|
|
|
TEST_ASSERT(memcmp(buf, res_buf, len) == 0);
|
2013-02-11 22:05:42 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_free(&srv);
|
|
|
|
mbedtls_ecdh_free(&cli);
|
2013-02-11 20:28:55 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2017-04-27 10:38:52 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdh_restart(int id, data_t *dA, data_t *dB, data_t *z,
|
|
|
|
int enable, int max_ops, int min_restart, int max_restart)
|
2017-04-27 10:38:52 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_ecdh_context srv, cli;
|
|
|
|
unsigned char buf[1000];
|
|
|
|
const unsigned char *vbuf;
|
|
|
|
size_t len;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_buf_info rnd_info_A, rnd_info_B;
|
2021-06-15 11:29:26 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2017-04-27 10:38:52 +02:00
|
|
|
int cnt_restart;
|
2018-10-30 15:08:52 +01:00
|
|
|
mbedtls_ecp_group grp;
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_init(&grp);
|
|
|
|
mbedtls_ecdh_init(&srv);
|
|
|
|
mbedtls_ecdh_init(&cli);
|
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2021-03-24 00:48:57 +01:00
|
|
|
rnd_info_A.fallback_f_rng = mbedtls_test_rnd_std_rand;
|
|
|
|
rnd_info_A.fallback_p_rng = NULL;
|
2020-06-25 09:03:34 +02:00
|
|
|
rnd_info_A.buf = dA->x;
|
|
|
|
rnd_info_A.length = dA->len;
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2021-03-24 00:48:57 +01:00
|
|
|
rnd_info_B.fallback_f_rng = mbedtls_test_rnd_std_rand;
|
|
|
|
rnd_info_B.fallback_p_rng = NULL;
|
2020-06-25 09:03:34 +02:00
|
|
|
rnd_info_B.buf = dB->x;
|
|
|
|
rnd_info_B.length = dB->len;
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2022-04-13 20:28:52 +02:00
|
|
|
/* The ECDH context is not guaranteed to have an mbedtls_ecp_group structure
|
2018-10-30 15:08:52 +01:00
|
|
|
* in every configuration, therefore we load it separately. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
|
2018-10-30 15:08:52 +01:00
|
|
|
|
|
|
|
/* Otherwise we would have to fix the random buffer,
|
|
|
|
* as in ecdh_primitive_testvec. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(grp.nbits % 8 == 0);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdh_setup(&srv, id) == 0);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2017-05-18 12:35:37 +02:00
|
|
|
/* set up restart parameters */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_set_max_ops(max_ops);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (enable) {
|
|
|
|
mbedtls_ecdh_enable_restart(&srv);
|
|
|
|
mbedtls_ecdh_enable_restart(&cli);
|
2017-05-18 12:35:37 +02:00
|
|
|
}
|
|
|
|
|
2019-01-23 15:24:37 +01:00
|
|
|
/* server writes its parameters */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0x00, sizeof(buf));
|
2017-04-27 10:38:52 +02:00
|
|
|
len = 0;
|
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ecdh_make_params(&srv, &len, buf, sizeof(buf),
|
|
|
|
mbedtls_test_rnd_buffer_rand,
|
|
|
|
&rnd_info_A);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == 0);
|
|
|
|
TEST_ASSERT(cnt_restart >= min_restart);
|
|
|
|
TEST_ASSERT(cnt_restart <= max_restart);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
|
|
|
/* client read server params */
|
|
|
|
vbuf = buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdh_read_params(&cli, &vbuf, buf + len) == 0);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
|
|
|
/* client writes its key share */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0x00, sizeof(buf));
|
2017-04-27 10:38:52 +02:00
|
|
|
len = 0;
|
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ecdh_make_public(&cli, &len, buf, sizeof(buf),
|
|
|
|
mbedtls_test_rnd_buffer_rand,
|
|
|
|
&rnd_info_B);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == 0);
|
|
|
|
TEST_ASSERT(cnt_restart >= min_restart);
|
|
|
|
TEST_ASSERT(cnt_restart <= max_restart);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
|
|
|
/* server reads client key share */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdh_read_public(&srv, buf, len) == 0);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
|
|
|
/* server computes shared secret */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2017-04-27 10:38:52 +02:00
|
|
|
len = 0;
|
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ecdh_calc_secret(&srv, &len, buf, sizeof(buf),
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == 0);
|
|
|
|
TEST_ASSERT(cnt_restart >= min_restart);
|
|
|
|
TEST_ASSERT(cnt_restart <= max_restart);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(len == z->len);
|
|
|
|
TEST_ASSERT(memcmp(buf, z->x, len) == 0);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
|
|
|
/* client computes shared secret */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2017-04-27 10:38:52 +02:00
|
|
|
len = 0;
|
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ecdh_calc_secret(&cli, &len, buf, sizeof(buf),
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == 0);
|
|
|
|
TEST_ASSERT(cnt_restart >= min_restart);
|
|
|
|
TEST_ASSERT(cnt_restart <= max_restart);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(len == z->len);
|
|
|
|
TEST_ASSERT(memcmp(buf, z->x, len) == 0);
|
2017-04-27 10:38:52 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_free(&grp);
|
|
|
|
mbedtls_ecdh_free(&srv);
|
|
|
|
mbedtls_ecdh_free(&cli);
|
2017-04-27 10:38:52 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-10-04 18:17:54 +02:00
|
|
|
|
2018-11-07 22:07:58 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdh_exchange_calc_secret(int grp_id,
|
|
|
|
data_t *our_private_key,
|
|
|
|
data_t *their_point,
|
|
|
|
int ours_first,
|
|
|
|
data_t *expected)
|
2018-11-07 22:07:58 +01:00
|
|
|
{
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2018-11-07 22:07:58 +01:00
|
|
|
mbedtls_ecp_keypair our_key;
|
|
|
|
mbedtls_ecp_keypair their_key;
|
|
|
|
mbedtls_ecdh_context ecdh;
|
|
|
|
unsigned char shared_secret[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
size_t shared_secret_length = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
|
|
|
mbedtls_ecdh_init(&ecdh);
|
|
|
|
mbedtls_ecp_keypair_init(&our_key);
|
|
|
|
mbedtls_ecp_keypair_init(&their_key);
|
2018-11-07 22:07:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!load_private_key(grp_id, our_private_key, &our_key, &rnd_info)) {
|
2018-11-07 22:07:58 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (!load_public_key(grp_id, their_point, &their_key)) {
|
2018-11-07 22:07:58 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-07 22:07:58 +01:00
|
|
|
|
|
|
|
/* Import the keys to the ECDH calculation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ours_first) {
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &our_key, MBEDTLS_ECDH_OURS) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS) == 0);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &our_key, MBEDTLS_ECDH_OURS) == 0);
|
2018-11-07 22:07:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the ECDH calculation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdh_calc_secret(
|
|
|
|
&ecdh,
|
|
|
|
&shared_secret_length,
|
|
|
|
shared_secret, sizeof(shared_secret),
|
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0);
|
|
|
|
TEST_ASSERT(shared_secret_length == expected->len);
|
|
|
|
TEST_ASSERT(memcmp(expected->x, shared_secret,
|
|
|
|
shared_secret_length) == 0);
|
2018-11-07 22:07:58 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_free(&ecdh);
|
|
|
|
mbedtls_ecp_keypair_free(&our_key);
|
|
|
|
mbedtls_ecp_keypair_free(&their_key);
|
2018-11-07 22:07:58 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-11-07 22:09:29 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdh_exchange_get_params_fail(int our_grp_id,
|
|
|
|
data_t *our_private_key,
|
|
|
|
int their_grp_id,
|
|
|
|
data_t *their_point,
|
|
|
|
int ours_first,
|
|
|
|
int expected_ret)
|
2018-11-07 22:09:29 +01:00
|
|
|
{
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2018-11-07 22:09:29 +01:00
|
|
|
mbedtls_ecp_keypair our_key;
|
|
|
|
mbedtls_ecp_keypair their_key;
|
|
|
|
mbedtls_ecdh_context ecdh;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
|
|
|
mbedtls_ecdh_init(&ecdh);
|
|
|
|
mbedtls_ecp_keypair_init(&our_key);
|
|
|
|
mbedtls_ecp_keypair_init(&their_key);
|
2018-11-07 22:09:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!load_private_key(our_grp_id, our_private_key, &our_key, &rnd_info)) {
|
2018-11-07 22:09:29 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (!load_public_key(their_grp_id, their_point, &their_key)) {
|
2018-11-07 22:09:29 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
|
|
|
if (ours_first) {
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &our_key, MBEDTLS_ECDH_OURS) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS) ==
|
|
|
|
expected_ret);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &their_key, MBEDTLS_ECDH_THEIRS) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdh_get_params(
|
|
|
|
&ecdh, &our_key, MBEDTLS_ECDH_OURS) ==
|
|
|
|
expected_ret);
|
2018-11-07 22:09:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_free(&ecdh);
|
|
|
|
mbedtls_ecp_keypair_free(&our_key);
|
|
|
|
mbedtls_ecp_keypair_free(&their_key);
|
2018-11-07 22:09:29 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|