@@ -927,3 +927,44 @@ TEST(KEMTest, InvalidSeedLength) {
927
927
928
928
OPENSSL_free (der_priv);
929
929
}
930
+
931
+ TEST_P (KEMTest, SetRawKeypairFromSeedOnInitializedKey) {
932
+ // Test that KEM_KEY_set_raw_keypair_from_seed fails when called on an
933
+ // already initialized key. This should trigger the "Ensure key is uninitialized"
934
+ // check and fail with ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
935
+
936
+ const KEMTestVector &test = GetParam ();
937
+
938
+ // ---- 1. Create and initialize a KEM_KEY ----
939
+ KEM_KEY *key = KEM_KEY_new ();
940
+ ASSERT_TRUE (key);
941
+
942
+ const KEM *kem = KEM_find_kem_by_nid (test.nid );
943
+ ASSERT_TRUE (kem);
944
+ ASSERT_TRUE (KEM_KEY_init (key, kem));
945
+
946
+ // ---- 2. Pre-initialize the key by setting a public key ----
947
+ std::vector<uint8_t > dummy_public_key (kem->public_key_len , 0x42 );
948
+ ASSERT_TRUE (KEM_KEY_set_raw_public_key (key, dummy_public_key.data ()));
949
+
950
+ // ---- 3. Create a valid 64-byte seed ----
951
+ std::vector<uint8_t > seed (64 );
952
+ for (size_t i = 0 ; i < 64 ; i++) {
953
+ seed[i] = static_cast <uint8_t >(i);
954
+ }
955
+
956
+ CBS seed_cbs;
957
+ CBS_init (&seed_cbs, seed.data (), seed.size ());
958
+
959
+ // ---- 4. Try to call KEM_KEY_set_raw_keypair_from_seed on the already initialized key ----
960
+ // This should fail because key->public_key is not NULL
961
+ ASSERT_FALSE (KEM_KEY_set_raw_keypair_from_seed (key, &seed_cbs));
962
+
963
+ // ---- 5. Verify the correct error was set ----
964
+ uint32_t err = ERR_get_error ();
965
+ EXPECT_EQ (ERR_GET_LIB (err), ERR_LIB_CRYPTO);
966
+ EXPECT_EQ (ERR_GET_REASON (err), ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
967
+
968
+ // ---- 6. Clean up ----
969
+ KEM_KEY_free (key);
970
+ }
0 commit comments