@@ -18820,6 +18820,70 @@ defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
1882018820#endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
1882118821#endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
1882218822
18823+ #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
18824+ !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
18825+
18826+ #if !defined(WOLFSSL_NO_ML_DSA_44)
18827+ /* ML-DSA-44 PUBKEY test (raw key bytes) */
18828+ ExpectIntGT(BIO_write(bio, bench_dilithium_level2_pubkey,
18829+ sizeof_bench_dilithium_level2_pubkey), 0);
18830+ ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
18831+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
18832+ EVP_PKEY_free(pkey);
18833+ pkey = NULL;
18834+
18835+ /* ML-DSA-44 PUBKEY test (LAMPS SubjectPublicKeyInfo DER) */
18836+ ExpectIntGT(BIO_write(bio, mldsa44_pub_spki, sizeof_mldsa44_pub_spki), 0);
18837+ ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
18838+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
18839+ EVP_PKEY_free(pkey);
18840+ pkey = NULL;
18841+ #endif
18842+
18843+ #if !defined(WOLFSSL_NO_ML_DSA_65)
18844+ /* ML-DSA-65 PUBKEY test (raw key bytes) */
18845+ ExpectIntGT(BIO_write(bio, bench_dilithium_level3_pubkey,
18846+ sizeof_bench_dilithium_level3_pubkey), 0);
18847+ ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
18848+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
18849+ EVP_PKEY_free(pkey);
18850+ pkey = NULL;
18851+
18852+ /* ML-DSA-65 PUBKEY test (LAMPS SubjectPublicKeyInfo DER) */
18853+ ExpectIntGT(BIO_write(bio, mldsa65_pub_spki, sizeof_mldsa65_pub_spki), 0);
18854+ ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
18855+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
18856+ EVP_PKEY_free(pkey);
18857+ pkey = NULL;
18858+ #endif
18859+
18860+ #if !defined(WOLFSSL_NO_ML_DSA_87)
18861+ /* ML-DSA-87 PUBKEY test (raw key bytes) */
18862+ ExpectIntGT(BIO_write(bio, bench_dilithium_level5_pubkey,
18863+ sizeof_bench_dilithium_level5_pubkey), 0);
18864+ ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
18865+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
18866+ EVP_PKEY_free(pkey);
18867+ pkey = NULL;
18868+
18869+ /* ML-DSA-87 PUBKEY test (LAMPS SubjectPublicKeyInfo DER) */
18870+ ExpectIntGT(BIO_write(bio, mldsa87_pub_spki, sizeof_mldsa87_pub_spki), 0);
18871+ ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
18872+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
18873+ EVP_PKEY_free(pkey);
18874+ pkey = NULL;
18875+ #endif
18876+
18877+ #endif /* HAVE_DILITHIUM && WOLFSSL_WC_DILITHIUM && !NO_VERIFY */
18878+
18879+ /* Negative test, invalid input must return NULL */
18880+ {
18881+ unsigned char garbage[64];
18882+ XMEMSET(garbage, 0xA5, sizeof(garbage));
18883+ ExpectIntGT(BIO_write(bio, garbage, (int)sizeof(garbage)), 0);
18884+ ExpectNull(d2i_PUBKEY_bio(bio, NULL));
18885+ }
18886+
1882318887 BIO_free(bio);
1882418888
1882518889 (void)pkey;
@@ -18906,6 +18970,156 @@ static int test_wolfSSL_d2i_PrivateKeys_bio(void)
1890618970 }
1890718971#endif
1890818972
18973+ #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
18974+ !defined(WOLFSSL_DILITHIUM_NO_SIGN)
18975+ #if !defined(WOLFSSL_NO_ML_DSA_44)
18976+ /* ML-DSA-44 PrivateKey test (raw bytes) */
18977+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
18978+ ExpectIntGT(BIO_write(bio, bench_dilithium_level2_key,
18979+ sizeof_bench_dilithium_level2_key), 0);
18980+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
18981+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
18982+ EVP_PKEY_free(pkey);
18983+ pkey = NULL;
18984+ BIO_free(bio);
18985+ bio = NULL;
18986+
18987+ /* ML-DSA-44 PrivateKey test (LAMPS PKCS#8 priv-only DER) */
18988+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
18989+ ExpectIntGT(BIO_write(bio, mldsa44_priv_only,
18990+ sizeof_mldsa44_priv_only), 0);
18991+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
18992+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
18993+ EVP_PKEY_free(pkey);
18994+ pkey = NULL;
18995+ BIO_free(bio);
18996+ bio = NULL;
18997+
18998+ /* ML-DSA-44 PrivateKey test (LAMPS PKCS#8 seed-priv DER) */
18999+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
19000+ ExpectIntGT(BIO_write(bio, mldsa44_seed_priv,
19001+ sizeof_mldsa44_seed_priv), 0);
19002+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
19003+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
19004+ EVP_PKEY_free(pkey);
19005+ pkey = NULL;
19006+ BIO_free(bio);
19007+ bio = NULL;
19008+
19009+ #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
19010+ /* ML-DSA-44 PrivateKey test (LAMPS PKCS#8 seed-only DER) --
19011+ * requires wc_dilithium_make_key_from_seed to expand the seed. */
19012+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
19013+ ExpectIntGT(BIO_write(bio, mldsa44_seed_only,
19014+ sizeof_mldsa44_seed_only), 0);
19015+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
19016+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
19017+ EVP_PKEY_free(pkey);
19018+ pkey = NULL;
19019+ BIO_free(bio);
19020+ bio = NULL;
19021+ #endif
19022+ #endif
19023+
19024+ #if !defined(WOLFSSL_NO_ML_DSA_65)
19025+ /* ML-DSA-65 PrivateKey test (raw bytes) */
19026+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
19027+ ExpectIntGT(BIO_write(bio, bench_dilithium_level3_key,
19028+ sizeof_bench_dilithium_level3_key), 0);
19029+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
19030+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
19031+ EVP_PKEY_free(pkey);
19032+ pkey = NULL;
19033+ BIO_free(bio);
19034+ bio = NULL;
19035+
19036+ /* ML-DSA-65 PrivateKey test (LAMPS PKCS#8 priv-only DER) */
19037+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
19038+ ExpectIntGT(BIO_write(bio, mldsa65_priv_only,
19039+ sizeof_mldsa65_priv_only), 0);
19040+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
19041+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
19042+ EVP_PKEY_free(pkey);
19043+ pkey = NULL;
19044+ BIO_free(bio);
19045+ bio = NULL;
19046+
19047+ /* ML-DSA-65 PrivateKey test (LAMPS PKCS#8 seed-priv DER) */
19048+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
19049+ ExpectIntGT(BIO_write(bio, mldsa65_seed_priv,
19050+ sizeof_mldsa65_seed_priv), 0);
19051+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
19052+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
19053+ EVP_PKEY_free(pkey);
19054+ pkey = NULL;
19055+ BIO_free(bio);
19056+ bio = NULL;
19057+
19058+ #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
19059+ /* ML-DSA-65 PrivateKey test (LAMPS PKCS#8 seed-only DER) --
19060+ * requires wc_dilithium_make_key_from_seed to expand the seed. */
19061+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
19062+ ExpectIntGT(BIO_write(bio, mldsa65_seed_only,
19063+ sizeof_mldsa65_seed_only), 0);
19064+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
19065+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
19066+ EVP_PKEY_free(pkey);
19067+ pkey = NULL;
19068+ BIO_free(bio);
19069+ bio = NULL;
19070+ #endif
19071+ #endif
19072+
19073+ #if !defined(WOLFSSL_NO_ML_DSA_87)
19074+ /* ML-DSA-87 PrivateKey test (raw bytes) */
19075+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
19076+ ExpectIntGT(BIO_write(bio, bench_dilithium_level5_key,
19077+ sizeof_bench_dilithium_level5_key), 0);
19078+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
19079+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
19080+ EVP_PKEY_free(pkey);
19081+ pkey = NULL;
19082+ BIO_free(bio);
19083+ bio = NULL;
19084+
19085+ /* ML-DSA-87 PrivateKey test (LAMPS PKCS#8 priv-only DER) */
19086+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
19087+ ExpectIntGT(BIO_write(bio, mldsa87_priv_only,
19088+ sizeof_mldsa87_priv_only), 0);
19089+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
19090+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
19091+ EVP_PKEY_free(pkey);
19092+ pkey = NULL;
19093+ BIO_free(bio);
19094+ bio = NULL;
19095+
19096+ /* ML-DSA-87 PrivateKey test (LAMPS PKCS#8 seed-priv DER) */
19097+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
19098+ ExpectIntGT(BIO_write(bio, mldsa87_seed_priv,
19099+ sizeof_mldsa87_seed_priv), 0);
19100+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
19101+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
19102+ EVP_PKEY_free(pkey);
19103+ pkey = NULL;
19104+ BIO_free(bio);
19105+ bio = NULL;
19106+
19107+ #ifndef WOLFSSL_DILITHIUM_NO_MAKE_KEY
19108+ /* ML-DSA-87 PrivateKey test (LAMPS PKCS#8 seed-only DER) --
19109+ * requires wc_dilithium_make_key_from_seed to expand the seed. */
19110+ ExpectNotNull(bio = BIO_new(BIO_s_mem()));
19111+ ExpectIntGT(BIO_write(bio, mldsa87_seed_only,
19112+ sizeof_mldsa87_seed_only), 0);
19113+ ExpectNotNull(pkey = d2i_PrivateKey_bio(bio, NULL));
19114+ ExpectIntEQ(EVP_PKEY_id(pkey), EVP_PKEY_DILITHIUM);
19115+ EVP_PKEY_free(pkey);
19116+ pkey = NULL;
19117+ BIO_free(bio);
19118+ bio = NULL;
19119+ #endif
19120+ #endif
19121+ #endif /* HAVE_DILITHIUM && WOLFSSL_WC_DILITHIUM && !NO_SIGN */
19122+
1890919123 ExpectNotNull(bio = BIO_new(BIO_s_mem()));
1891019124#ifndef NO_WOLFSSL_SERVER
1891119125 ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
@@ -18975,6 +19189,18 @@ static int test_wolfSSL_d2i_PrivateKeys_bio(void)
1897519189 RSA_free(rsa);
1897619190 }
1897719191#endif /* WOLFSSL_KEY_GEN && !NO_RSA */
19192+
19193+ /* Negative test, invalid input must return NULL */
19194+ {
19195+ BIO* nbio = NULL;
19196+ unsigned char garbage[64];
19197+ XMEMSET(garbage, 0xA5, sizeof(garbage));
19198+ ExpectNotNull(nbio = BIO_new(BIO_s_mem()));
19199+ ExpectIntGT(BIO_write(nbio, garbage, (int)sizeof(garbage)), 0);
19200+ ExpectNull(d2i_PrivateKey_bio(nbio, NULL));
19201+ BIO_free(nbio);
19202+ }
19203+
1897819204 SSL_CTX_free(ctx);
1897919205 ctx = NULL;
1898019206 BIO_free(bio);
0 commit comments