Commit 449c566185 for strongswan.org
commit 449c566185179f6933ffc60056f50e08bb1b4c56
Author: Tobias Brunner <tobias@strongswan.org>
Date: Wed Nov 25 14:05:45 2026 +0100
ml: Avoid memory leak if get_public_key() is called multiple times for ML-KEM
This could happen if ML-KEM is used during IKE_SA_INIT (which is not
recommended due to its size and the computational burden on the
responder) and the SA is reset.
The tests now include a second call to `get_public_key()` on the
initiator to ensure it produces the same value (for all key exchange
implementations).
Fixes: 89f4b345e336 ("ml: Add software implementation of ML-KEM")
diff --git a/src/libstrongswan/crypto/crypto_tester.c b/src/libstrongswan/crypto/crypto_tester.c
index 2b0e2f716e..d906c9cc53 100644
--- a/src/libstrongswan/crypto/crypto_tester.c
+++ b/src/libstrongswan/crypto/crypto_tester.c
@@ -1741,6 +1741,11 @@ static bool test_single_ke(key_exchange_method_t method, ke_test_vector_t *v,
{
goto failure;
}
+ chunk_free(&a_pub);
+ if (!a->get_public_key(a, &a_pub) || !chunk_equals(a_pub, v->pub_i))
+ {
+ goto failure;
+ }
if (!b->set_public_key(b, a_pub))
{
goto failure;
@@ -1779,10 +1784,10 @@ failure:
static bool test_single_ke(key_exchange_method_t method, ke_constructor_t create)
{
key_exchange_t *a = NULL, *b = NULL;
- chunk_t a_pub, b_pub, a_sec, b_sec;
+ chunk_t a_pub, a_pub2, b_pub, a_sec, b_sec;
bool success = FALSE;
- a_pub = b_pub = a_sec = b_sec = chunk_empty;
+ a_pub = a_pub2 = b_pub = a_sec = b_sec = chunk_empty;
a = create(method);
b = create(method);
if (!a || !b)
@@ -1790,6 +1795,8 @@ static bool test_single_ke(key_exchange_method_t method, ke_constructor_t create
goto failure;
}
if (!a->get_public_key(a, &a_pub) ||
+ !a->get_public_key(a, &a_pub2) ||
+ !chunk_equals(a_pub, a_pub2) ||
!b->set_public_key(b, a_pub) ||
!b->get_shared_secret(b, &b_sec) ||
!b->get_public_key(b, &b_pub) ||
@@ -1806,6 +1813,7 @@ failure:
DESTROY_IF(a);
DESTROY_IF(b);
chunk_free(&a_pub);
+ chunk_free(&a_pub2);
chunk_free(&b_pub);
chunk_free(&a_sec);
chunk_free(&b_sec);
diff --git a/src/libstrongswan/crypto/key_exchange.h b/src/libstrongswan/crypto/key_exchange.h
index cd99a24eec..dddd65d63a 100644
--- a/src/libstrongswan/crypto/key_exchange.h
+++ b/src/libstrongswan/crypto/key_exchange.h
@@ -147,6 +147,9 @@ struct key_exchange_t {
*
* See the interface description on how this method is used with KEMs.
*
+ * @note On initiators, this is expected to return the same value when
+ * called multiple times.
+ *
* @param value public key (allocated)
* @return TRUE if public key retrieved
*/
diff --git a/src/libstrongswan/plugins/ml/ml_kem.c b/src/libstrongswan/plugins/ml/ml_kem.c
index 60e425f6a4..a3d98462e8 100644
--- a/src/libstrongswan/plugins/ml/ml_kem.c
+++ b/src/libstrongswan/plugins/ml/ml_kem.c
@@ -49,7 +49,7 @@ struct private_key_exchange_t {
/**
* Encryption/public key and matrix A as initiator (array of k polynomials,
- * followed by a matrix of k*k polynomials).
+ * followed by seed rho, followed by a matrix of k*k polynomials).
*/
chunk_t public_key;
@@ -529,6 +529,22 @@ static void poly_to_message(ml_poly_t *p, uint8_t *m)
}
}
+/**
+ * Encode the public key from the stored polynomial t and rho.
+ */
+static chunk_t encode_public_key(private_key_exchange_t *this)
+{
+ const uint8_t k = this->params->k;
+
+ chunk_t ek;
+
+ ek = chunk_alloc(k * ML_KEM_POLY_LEN + ML_KEM_SEED_LEN);
+ encode_poly_arr(k, (ml_poly_t*)this->public_key.ptr, ek.ptr);
+ memcpy(ek.ptr + k * ML_KEM_POLY_LEN,
+ this->public_key.ptr + k * sizeof(ml_poly_t), ML_KEM_SEED_LEN);
+ return ek;
+}
+
/**
* Generate a key pair from the given random seed d. Returns the encoded public
* key.
@@ -541,7 +557,7 @@ static bool pke_keygen(private_key_exchange_t *this, chunk_t d, chunk_t *ek)
const uint8_t eta1 = this->params->eta1;
uint8_t seeds[2 * ML_KEM_SEED_LEN];
- uint8_t *rho = seeds;
+ uint8_t *rho = seeds, *rho_ek;
uint8_t *sigma = seeds + ML_KEM_SEED_LEN;
uint8_t N = 0;
ml_poly_t *a, *s, e[k], *t;
@@ -556,9 +572,11 @@ static bool pke_keygen(private_key_exchange_t *this, chunk_t d, chunk_t *ek)
goto err;
}
- this->public_key = chunk_alloc((k+1) * k * sizeof(ml_poly_t));
+ this->public_key = chunk_alloc((k+1) * k * sizeof(ml_poly_t) +
+ ML_KEM_SEED_LEN);
t = (ml_poly_t*)this->public_key.ptr;
- a = (ml_poly_t*)this->public_key.ptr + k;
+ rho_ek = this->public_key.ptr + k * sizeof(ml_poly_t);
+ a = (ml_poly_t*)(rho_ek + ML_KEM_SEED_LEN);
/* generate matrix A */
if (!generate_a(this, a, rho))
@@ -600,17 +618,15 @@ static bool pke_keygen(private_key_exchange_t *this, chunk_t d, chunk_t *ek)
}
ml_poly_add_arr(k, t, e, t);
- /* pack public key and rho */
- *ek = chunk_alloc(k * ML_KEM_POLY_LEN + ML_KEM_SEED_LEN);
- encode_poly_arr(k, t, ek->ptr);
- memcpy(ek->ptr + k * ML_KEM_POLY_LEN, rho, ML_KEM_SEED_LEN);
+ memcpy(rho_ek, rho, ML_KEM_SEED_LEN);
+ *ek = encode_public_key(this);
success = TRUE;
err:
memwipe(seeds, sizeof(seeds));
- memwipe(sigma, ML_KEM_SEED_LEN);
memwipe(e, sizeof(e));
+
return success;
}
@@ -652,7 +668,8 @@ static bool pke_encrypt(private_key_exchange_t *this, chunk_t ek, uint8_t *m,
{
/* as initiator, we already have the decoded polynomial and matrix A */
t = (ml_poly_t*)this->public_key.ptr;
- a = (ml_poly_t*)this->public_key.ptr + k;
+ a = (ml_poly_t*)(this->public_key.ptr + k * sizeof(ml_poly_t) +
+ ML_KEM_SEED_LEN);
}
/* sample y from CBD using noise seed r and nonce N as input */
@@ -750,12 +767,12 @@ static bool pke_decrypt(private_key_exchange_t *this, chunk_t ciphertext,
*
* Algorithm 16/19 in FIPS 203.
*/
-static bool generate_keypair(private_key_exchange_t *this, chunk_t *ek)
+static bool generate_keypair(private_key_exchange_t *this, chunk_t *out)
{
uint8_t dz[2*ML_KEM_SEED_LEN];
chunk_t d = chunk_create(dz, ML_KEM_SEED_LEN);
chunk_t z = chunk_create(dz + ML_KEM_SEED_LEN, ML_KEM_SEED_LEN);
- chunk_t Hek;
+ chunk_t ek = chunk_empty, Hek;
bool success = FALSE;
/* get random seeds d and z */
@@ -766,12 +783,19 @@ static bool generate_keypair(private_key_exchange_t *this, chunk_t *ek)
/* generate a key pair and generate a hash of the latter to be stored
* together with the rejection seed z */
- if (pke_keygen(this, d, ek) &&
- this->H->allocate_hash(this->H, *ek, &Hek))
+ if (pke_keygen(this, d, &ek) &&
+ this->H->allocate_hash(this->H, ek, &Hek))
{
+ *out = ek;
this->key_data = chunk_cat("mc", Hek, z);
success = TRUE;
}
+ else
+ {
+ chunk_free(&ek);
+ chunk_free(&this->public_key);
+ chunk_clear(&this->private_key);
+ }
memwipe(dz, sizeof(dz));
return success;
@@ -789,6 +813,11 @@ METHOD(key_exchange_t, get_public_key, bool,
}
/* as initiator, we generate a key pair and return the public key */
+ if (this->public_key.ptr)
+ {
+ *value = encode_public_key(this);
+ return TRUE;
+ }
return generate_keypair(this, value);
}