ECDH: add tests for static ECDH with context reuse

A bug there was fixed in a previous commit, but without complete tests
or a ChangeLog entry. This commit completes the fix.

Signed-off-by: Manuel Pégourié-Gonnard <[email protected]>
This commit is contained in:
Manuel Pégourié-Gonnard
2026-04-13 11:23:17 +02:00
parent 559525cd7f
commit 7e45882dc2
3 changed files with 93 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
Bugfix
* Fix bug in configurations with MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED that
prevented re-use of an mbedtls_ecdh_context for static ECDH: our secret key
was wiped after its first use to compute a shared secret.
+16
View File
@@ -113,6 +113,22 @@ ecdh calc_secret: MSB zero, curve448
depends_on:MBEDTLS_ECP_DP_CURVE448_ENABLED
ecdh_exchange_calc_secret:MBEDTLS_ECP_DP_CURVE448:"d805000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080":"0500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":0:"517b6be2c2edd157319082d4ddc688add71297284b6da74b050170035d764c19b8a14da4600715b18d8a3320a8e18a1d80fc9e6f7ea85600"
ECDH context re-use: secp256r1
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED
ecdh_context_reuse:MBEDTLS_ECP_DP_SECP256R1:"8000000000000000000000000000000000000000000000000000000000000000":"046b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c2964fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5":"77b20a912e6b23135066e911891524bc4efe3560e3e92350b52dec8f375f2b54":"047cf27b188d034f7e8a52380304b51ac3c08969e277f21b35a60b48fc4766997807775510db8ed040293d9ac69f7430dbba7dade63ce982299e04b79d227873d1":"0b197a2e1e67a44b5afb62de48adde6400b60867487cab5739912513c420924a"
ECDH context re-use: secp521r1
depends_on:MBEDTLS_ECP_DP_SECP521R1_ENABLED
ecdh_context_reuse:MBEDTLS_ECP_DP_SECP521R1:"010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"0400c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650":"002da7db02840f023a36e1fffeaee16d3c47bb435bec6a231d4aab1ec5412f56fb90fcc4eaab9fd8571084cb9da252466c052d21913ce0fda47e61829972ce8f9a17":"0400433c219024277e7e682fcb288148c282747403279b1ccc06352c6e5505d769be97b3b204da6ef55507aa104a3a35c5af41cf2fa364d60fd967f43e3933ba6d783d00f4bb8cc7f86db26700a7f3eceeeed3f0b5c6b5107c4da97740ab21a29906c42dbbb3e377de9f251f6b93937fa99a3248f4eafcbe95edc0f4f71be356d661f41b02":"0033ffb964e05d5f6799c7865c906e2a0bd0c9b131eef6bf6453c960bca9bf06dea4650bd0df069416992b17027d972d1c60830492593fc3431582e051426b4c3f67"
ECDH context re-use: x25519
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
ecdh_context_reuse:MBEDTLS_ECP_DP_CURVE25519:"0000000000000000000000000000000000000000000000000000000000000040":"0900000000000000000000000000000000000000000000000000000000000000":"2fe57da347cd62431528daac5fbb290730fff684afc4cfc2ed90995f58cb3b74":"fb4e68dd9c46ae5c5c0b351eed5c3f8f1471157d680c75d9b7f17318d542d320":"9d8e35e77dfa6c16ed6df587251ee0d6379bd2556344aecf8f85d83fb6fa6476"
ECDH context re-use: x448
depends_on:MBEDTLS_ECP_DP_CURVE448_ENABLED
ecdh_context_reuse:MBEDTLS_ECP_DP_CURVE448:"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080":"0500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"e9b820a44dba3bc569bee7214b62b09ee239b50978a7a1c69a9ade46858cc37c48eb03fd88c289badd708fc635c7d863cc40e4dfdd6d5d40":"b63c741cbca4327d0808125a25f0e82571ed00daf14737bfc16f261666763c5fd7831e51fbbe9aaccc5cbdd86546ef9ad4e3ca5722329163":"b23511da3c119e32b7b146122b59d1f122f22008f02715326e62d6ca162b4f20188a95a52dd9e40cb5b729f1f153da0d51c3acfc4c81944c"
ECDH get_params with mismatched groups: our BP256R1, their SECP256R1
depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_ECP_DP_BP256R1_ENABLED
ecdh_exchange_get_params_fail:MBEDTLS_ECP_DP_BP256R1:"1234567812345678123456781234567812345678123456781234567812345678":MBEDTLS_ECP_DP_SECP256R1:"04dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3":0:MBEDTLS_ERR_ECP_BAD_INPUT_DATA
+73
View File
@@ -363,6 +363,79 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE */
void ecdh_context_reuse(int grp_id, data_t *our_private_key,
data_t *their_point1, data_t *shared1,
data_t *their_point2, data_t *shared2)
{
/* This test exercises re-using the context for static ECDH */
mbedtls_test_rnd_pseudo_info rnd_info;
mbedtls_ecp_keypair our_key;
mbedtls_ecp_keypair their_key1;
mbedtls_ecp_keypair their_key2;
mbedtls_ecdh_context ecdh;
unsigned char *buf = NULL;
size_t min_buf_size = 0;
size_t shared_secret_length = 0;
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_key1);
mbedtls_ecp_keypair_init(&their_key2);
if (!load_private_key(grp_id, our_private_key, &our_key, &rnd_info)) {
goto exit;
}
if (!load_public_key(grp_id, their_point1, &their_key1)) {
goto exit;
}
if (!load_public_key(grp_id, their_point2, &their_key2)) {
goto exit;
}
/* Import our long-term private key */
TEST_ASSERT(mbedtls_ecdh_get_params(
&ecdh, &our_key, MBEDTLS_ECDH_OURS) == 0);
/* Allocate output buffer of minimal size */
min_buf_size = (our_key.grp.pbits + 7) / 8;
TEST_LE_U(min_buf_size, MBEDTLS_ECP_MAX_BYTES);
TEST_CALLOC(buf, min_buf_size);
/* Import first peer key and perform first ECDH calculation */
TEST_ASSERT(mbedtls_ecdh_get_params(
&ecdh, &their_key1, MBEDTLS_ECDH_THEIRS) == 0);
TEST_EQUAL(0,
mbedtls_ecdh_calc_secret(
&ecdh, &shared_secret_length,
buf, min_buf_size,
&mbedtls_test_rnd_pseudo_rand, &rnd_info));
TEST_MEMORY_COMPARE(shared1->x, shared1->len,
buf, shared_secret_length);
shared_secret_length = 0;
/* Import 2nd peer key and perform 2nd ECDH calculation */
TEST_ASSERT(mbedtls_ecdh_get_params(
&ecdh, &their_key2, MBEDTLS_ECDH_THEIRS) == 0);
TEST_EQUAL(0,
mbedtls_ecdh_calc_secret(
&ecdh, &shared_secret_length,
buf, min_buf_size,
&mbedtls_test_rnd_pseudo_rand, &rnd_info));
TEST_MEMORY_COMPARE(shared2->x, shared2->len,
buf, shared_secret_length);
exit:
mbedtls_free(buf);
mbedtls_ecdh_free(&ecdh);
mbedtls_ecp_keypair_free(&our_key);
mbedtls_ecp_keypair_free(&their_key1);
mbedtls_ecp_keypair_free(&their_key2);
}
/* END_CASE */
/* BEGIN_CASE */
void ecdh_exchange_calc_secret(int grp_id,
data_t *our_private_key,