Commit 6c59c6c0e7 for openssl.org
commit 6c59c6c0e71d819d7f81831ba148b37d52896957
Author: Viktor Dukhovni <viktor@openssl.org>
Date: Thu Nov 5 20:00:34 2026 +1100
Make initialised PQ keys immutable
This applies to SLH-DSA, LMS and hybrid ML-KEM, matching
existing behaviour in ML-KEM and ML-DSA.
Tests added.
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Andrew Dinh <andrewd@openssl.org>
Merge-date: Mon Nov 16 02:13:18 2026
Merged-from: https://github.com/openssl/openssl/pull/33551
diff --git a/providers/implementations/keymgmt/lms_kmgmt.c b/providers/implementations/keymgmt/lms_kmgmt.c
index f8297e1146..cfcbcb91b5 100644
--- a/providers/implementations/keymgmt/lms_kmgmt.c
+++ b/providers/implementations/keymgmt/lms_kmgmt.c
@@ -9,6 +9,7 @@
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
+#include <openssl/err.h>
#include <openssl/param_build.h>
#include <openssl/proverr.h>
#include "crypto/lms.h"
@@ -77,6 +78,13 @@ static int lms_import(void *keydata, int selection, const OSSL_PARAM params[])
|| !lms_import_decoder(params, &p))
return 0;
+ /* Once a key has a public component, it is no longer changed */
+ if (ossl_lms_key_has(key, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) {
+ ERR_raise_data(ERR_LIB_PROV, PROV_R_KEY_IMMUTABLE_ONCE_SET,
+ "Keys are immutable once key material has been loaded or generated");
+ return 0;
+ }
+
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
return 0;
diff --git a/providers/implementations/keymgmt/mlx_kmgmt.c b/providers/implementations/keymgmt/mlx_kmgmt.c
index 822f01965c..53bfd7bb22 100644
--- a/providers/implementations/keymgmt/mlx_kmgmt.c
+++ b/providers/implementations/keymgmt/mlx_kmgmt.c
@@ -602,23 +602,28 @@ static int mlx_kem_set_params(void *vkey, const OSSL_PARAM params[])
if (key == NULL || !mlx_set_params_decoder(params, &p))
return 0;
+ if (p.propq == NULL && p.pub == NULL)
+ return 1;
+
+ /* Neither the property query nor the public key can change once set */
+ if (mlx_kem_have_pubkey(key)) {
+ ERR_raise_data(ERR_LIB_PROV, PROV_R_KEY_IMMUTABLE_ONCE_SET,
+ "Keys are immutable once key material has been loaded or generated");
+ return 0;
+ }
+
if (p.propq != NULL) {
- OPENSSL_free(key->propq);
- key->propq = NULL;
- if (!OSSL_PARAM_get_utf8_string(p.propq, &key->propq, 0))
+ char *propq = NULL;
+
+ if (!OSSL_PARAM_get_utf8_string(p.propq, &propq, 0))
return 0;
+ OPENSSL_free(key->propq);
+ key->propq = propq;
}
if (p.pub == NULL)
return 1;
- /* Key mutation is reportedly generally not allowed */
- if (mlx_kem_have_pubkey(key)) {
- ERR_raise_data(ERR_LIB_PROV,
- PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
- "keys cannot be mutated");
- return 0;
- }
/* An unlikely failure mode is the parameter having some unexpected type */
if (!OSSL_PARAM_get_octet_string_ptr(p.pub, &pubenc, &publen))
return 0;
diff --git a/providers/implementations/keymgmt/slh_dsa_kmgmt.c b/providers/implementations/keymgmt/slh_dsa_kmgmt.c
index e1473fe962..6eba67433d 100644
--- a/providers/implementations/keymgmt/slh_dsa_kmgmt.c
+++ b/providers/implementations/keymgmt/slh_dsa_kmgmt.c
@@ -9,6 +9,7 @@
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
+#include <openssl/err.h>
#include <openssl/param_build.h>
#include <openssl/proverr.h>
#include <openssl/self_test.h>
@@ -133,6 +134,13 @@ static int slh_dsa_import(void *keydata, int selection, const OSSL_PARAM params[
|| !slh_dsa_import_decoder(params, &p))
return 0;
+ /* Once a key has a public component, it is no longer changed */
+ if (ossl_slh_dsa_key_has(key, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) {
+ ERR_raise_data(ERR_LIB_PROV, PROV_R_KEY_IMMUTABLE_ONCE_SET,
+ "Keys are immutable once key material has been loaded or generated");
+ return 0;
+ }
+
if ((selection & SLH_DSA_POSSIBLE_SELECTIONS) == 0)
return 0;
diff --git a/test/build.info b/test/build.info
index 81bed16dd8..134d87ea76 100644
--- a/test/build.info
+++ b/test/build.info
@@ -340,7 +340,7 @@ IF[{- !$disabled{tests} -}]
PROGRAMS{noinst}=slh_dsa_test
SOURCE[slh_dsa_test]=slh_dsa_test.c
INCLUDE[slh_dsa_test]=../include ../apps/include
- DEPEND[slh_dsa_test]=../libcrypto libtestutil.a
+ DEPEND[slh_dsa_test]=../libcrypto.a libtestutil.a
ENDIF
IF[{- !$disabled{'deprecated-3.0'} -}]
diff --git a/test/lms_test.c b/test/lms_test.c
index d86e7ae6d8..facc603f2d 100644
--- a/test/lms_test.c
+++ b/test/lms_test.c
@@ -9,7 +9,10 @@
#include <openssl/core_names.h>
#include <openssl/decoder.h>
+#include <openssl/err.h>
#include <openssl/evp.h>
+#include <openssl/proverr.h>
+#include "crypto/evp.h"
#include "crypto/lms.h"
#include "internal/nelem.h"
#include "testutil.h"
@@ -235,6 +238,43 @@ end:
return ret;
}
+/* Keys are immutable once they have a public key */
+static int lms_key_immutable_test(void)
+{
+ int ret = 0;
+ EVP_KEYMGMT *keymgmt = NULL;
+ void *keydata = NULL;
+ LMS_ACVP_TEST_DATA *td = &lms_testdata[1];
+ OSSL_PARAM pub_params[2];
+
+ pub_params[0] = OSSL_PARAM_construct_octet_string(
+ OSSL_PKEY_PARAM_PUB_KEY, (void *)td->pub, td->publen);
+ pub_params[1] = OSSL_PARAM_construct_end();
+
+ if (!TEST_ptr(keymgmt = EVP_KEYMGMT_fetch(libctx, "LMS", propq))
+ || !TEST_ptr(keydata = evp_keymgmt_newdata(keymgmt, NULL))
+ || !TEST_true(evp_keymgmt_import(keymgmt, keydata,
+ OSSL_KEYMGMT_SELECT_PUBLIC_KEY, pub_params)))
+ goto end;
+
+ /* A second import must fail and leave the key as it was */
+ if (!TEST_false(evp_keymgmt_import(keymgmt, keydata,
+ OSSL_KEYMGMT_SELECT_PUBLIC_KEY, pub_params))
+ || !TEST_int_eq(ERR_GET_REASON(ERR_peek_last_error()),
+ PROV_R_KEY_IMMUTABLE_ONCE_SET)
+ || !TEST_true(evp_keymgmt_has(keymgmt, keydata,
+ OSSL_KEYMGMT_SELECT_PUBLIC_KEY)))
+ goto end;
+
+ ret = 1;
+end:
+ ERR_clear_error();
+ if (keymgmt != NULL)
+ evp_keymgmt_freedata(keymgmt, keydata);
+ EVP_KEYMGMT_free(keymgmt);
+ return ret;
+}
+
static int lms_key_validate_test(void)
{
int ret = 0;
@@ -594,6 +634,12 @@ int setup_tests(void)
ADD_TEST(lms_bad_pub_len_test);
ADD_TEST(lms_key_validate_test);
ADD_TEST(lms_key_eq_test);
+ /*
+ * Older FIPS providers do not enforce the immutability checks, so only
+ * run this test where the provider (if any) is at least 4.2.0.
+ */
+ if (fips_provider_version_ge(libctx, 4, 2, 0))
+ ADD_TEST(lms_key_immutable_test);
ADD_TEST(lms_key_decode_test);
ADD_TEST(lms_pubkey_decoder_test);
ADD_TEST(lms_pubkey_decoder_fail_test);
diff --git a/test/ml_kem_evp_extra_test.c b/test/ml_kem_evp_extra_test.c
index 1f282034cf..4ff9268e9f 100644
--- a/test/ml_kem_evp_extra_test.c
+++ b/test/ml_kem_evp_extra_test.c
@@ -492,6 +492,70 @@ err:
EVP_PKEY_CTX_free(genctx);
return ret;
}
+
+/*
+ * The property query and the public key of an MLX key can be set only
+ * until the key has a public key. Afterwards both are refused with
+ * PROV_R_KEY_IMMUTABLE_ONCE_SET.
+ */
+static int mlx_set_params_must_fail(EVP_KEYMGMT *keymgmt, void *keydata,
+ const OSSL_PARAM params[])
+{
+ int ret = TEST_false(evp_keymgmt_set_params(keymgmt, keydata, params))
+ && TEST_int_eq(ERR_GET_REASON(ERR_peek_last_error()),
+ PROV_R_KEY_IMMUTABLE_ONCE_SET);
+
+ ERR_clear_error();
+ return ret;
+}
+
+static int test_mlx_kem_set_params_immutable(int idx)
+{
+ const char *alg = mlx_kem_algs[idx];
+ EVP_PKEY *keypair = NULL;
+ EVP_KEYMGMT *keymgmt = NULL;
+ void *keydata = NULL;
+ unsigned char *pub = NULL;
+ size_t publen = 0;
+ OSSL_PARAM propq_params[2], pub_params[2];
+ char propq[] = "fips=no";
+ int ret = 0;
+
+ /* A source of public key bytes of the right size */
+ if (!TEST_ptr(keypair = EVP_PKEY_Q_keygen(testctx, NULL, alg))
+ || !TEST_size_t_gt(publen = EVP_PKEY_get1_encoded_public_key(keypair,
+ &pub),
+ 0))
+ goto err;
+
+ propq_params[0] = OSSL_PARAM_construct_utf8_string(
+ OSSL_PKEY_PARAM_PROPERTIES, propq, 0);
+ propq_params[1] = OSSL_PARAM_construct_end();
+ pub_params[0] = OSSL_PARAM_construct_octet_string(
+ OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, pub, publen);
+ pub_params[1] = OSSL_PARAM_construct_end();
+
+ /* An empty key accepts the property query, then the public key */
+ if (!TEST_ptr(keymgmt = EVP_KEYMGMT_fetch(testctx, alg, NULL))
+ || !TEST_ptr(keydata = evp_keymgmt_newdata(keymgmt, NULL))
+ || !TEST_true(evp_keymgmt_set_params(keymgmt, keydata, propq_params))
+ || !TEST_true(evp_keymgmt_set_params(keymgmt, keydata, pub_params)))
+ goto err;
+
+ /* Once it has a public key, neither can be set again */
+ if (!mlx_set_params_must_fail(keymgmt, keydata, propq_params)
+ || !mlx_set_params_must_fail(keymgmt, keydata, pub_params))
+ goto err;
+
+ ret = 1;
+err:
+ if (keymgmt != NULL)
+ evp_keymgmt_freedata(keymgmt, keydata);
+ EVP_KEYMGMT_free(keymgmt);
+ OPENSSL_free(pub);
+ EVP_PKEY_free(keypair);
+ return ret;
+}
#endif /* OPENSSL_NO_EC */
/*
@@ -673,6 +737,7 @@ int setup_tests(void)
ADD_TEST(test_ml_kem_key_immutable);
#ifndef OPENSSL_NO_EC
ADD_ALL_TESTS(test_mlx_kem_dup_partial_selection, OSSL_NELEM(mlx_kem_algs));
+ ADD_ALL_TESTS(test_mlx_kem_set_params_immutable, OSSL_NELEM(mlx_kem_algs));
#endif
return 1;
}
diff --git a/test/slh_dsa_test.c b/test/slh_dsa_test.c
index 75b7e3ba4d..c18febb8ae 100644
--- a/test/slh_dsa_test.c
+++ b/test/slh_dsa_test.c
@@ -8,10 +8,13 @@
*/
#include <openssl/core_names.h>
+#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/param_build.h>
#include <openssl/rand.h>
#include <openssl/pem.h>
+#include <openssl/proverr.h>
+#include "crypto/evp.h"
#include "crypto/slh_dsa.h"
#include "internal/nelem.h"
#include "testutil.h"
@@ -116,6 +119,73 @@ end:
return ret == 1;
}
+/* An import into a key that already has a public component must fail */
+static int slh_dsa_import_must_fail(EVP_KEYMGMT *keymgmt, void *keydata,
+ int selection, const OSSL_PARAM params[])
+{
+ int ret = TEST_false(evp_keymgmt_import(keymgmt, keydata, selection, params))
+ && TEST_int_eq(ERR_GET_REASON(ERR_peek_last_error()),
+ PROV_R_KEY_IMMUTABLE_ONCE_SET);
+
+ ERR_clear_error();
+ return ret;
+}
+
+/*
+ * Keys are immutable once they have a public component, whether that came
+ * from a public key or from a key pair import.
+ */
+static int slh_dsa_key_immutable_test(void)
+{
+ int ret = 0;
+ EVP_KEYMGMT *keymgmt = NULL;
+ void *keydata = NULL;
+ const SLH_DSA_SIG_TEST_DATA *td = &slh_dsa_sig_testdata[0];
+ OSSL_PARAM pub_params[2], keypair_params[3];
+
+ pub_params[0] = OSSL_PARAM_construct_octet_string(
+ OSSL_PKEY_PARAM_PUB_KEY, (void *)td->pub, td->pub_len);
+ pub_params[1] = OSSL_PARAM_construct_end();
+
+ keypair_params[0] = OSSL_PARAM_construct_octet_string(
+ OSSL_PKEY_PARAM_PRIV_KEY, (void *)td->priv, td->priv_len);
+ keypair_params[1] = OSSL_PARAM_construct_octet_string(
+ OSSL_PKEY_PARAM_PUB_KEY, (void *)td->pub, td->pub_len);
+ keypair_params[2] = OSSL_PARAM_construct_end();
+
+ if (!TEST_ptr(keymgmt = EVP_KEYMGMT_fetch(lib_ctx, td->alg, NULL)))
+ goto end;
+
+ /* Public key only, then a second public key or a key pair */
+ if (!TEST_ptr(keydata = evp_keymgmt_newdata(keymgmt, NULL))
+ || !TEST_true(evp_keymgmt_import(keymgmt, keydata,
+ OSSL_KEYMGMT_SELECT_PUBLIC_KEY, pub_params))
+ || !slh_dsa_import_must_fail(keymgmt, keydata,
+ OSSL_KEYMGMT_SELECT_PUBLIC_KEY, pub_params)
+ || !slh_dsa_import_must_fail(keymgmt, keydata,
+ OSSL_KEYMGMT_SELECT_KEYPAIR, keypair_params))
+ goto end;
+ evp_keymgmt_freedata(keymgmt, keydata);
+ keydata = NULL;
+
+ /* Key pair, then a second key pair or a public key */
+ if (!TEST_ptr(keydata = evp_keymgmt_newdata(keymgmt, NULL))
+ || !TEST_true(evp_keymgmt_import(keymgmt, keydata,
+ OSSL_KEYMGMT_SELECT_KEYPAIR, keypair_params))
+ || !slh_dsa_import_must_fail(keymgmt, keydata,
+ OSSL_KEYMGMT_SELECT_KEYPAIR, keypair_params)
+ || !slh_dsa_import_must_fail(keymgmt, keydata,
+ OSSL_KEYMGMT_SELECT_PUBLIC_KEY, pub_params))
+ goto end;
+
+ ret = 1;
+end:
+ if (keymgmt != NULL)
+ evp_keymgmt_freedata(keymgmt, keydata);
+ EVP_KEYMGMT_free(keymgmt);
+ return ret;
+}
+
static int slh_dsa_key_eq_test(void)
{
int ret = 0;
@@ -665,6 +735,12 @@ int setup_tests(void)
ADD_TEST(slh_dsa_key_validate_test);
ADD_TEST(slh_dsa_key_validate_failure_test);
ADD_TEST(slh_dsa_key_eq_test);
+ /*
+ * Older FIPS providers do not enforce the immutability checks, so only
+ * run this test where the provider (if any) is at least 4.2.0.
+ */
+ if (fips_provider_version_ge(lib_ctx, 4, 2, 0))
+ ADD_TEST(slh_dsa_key_immutable_test);
ADD_TEST(slh_dsa_usage_test);
ADD_TEST(slh_dsa_deterministic_usage_test);
ADD_ALL_TESTS(slh_dsa_sign_verify_test, OSSL_NELEM(slh_dsa_sig_testdata));