From 18396b098490011eed7e7fead180e3c0067443ce Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 17 Sep 2026 10:23:38 -0400 Subject: [PATCH] Rust wrapper: mldsa: fix cfg detection for public and private build options Fixes F-13035. --- wrapper/rust/wolfssl-wolfcrypt/build.rs | 10 ++++- wrapper/rust/wolfssl-wolfcrypt/src/mldsa.rs | 41 +++++++++++-------- .../wolfssl-wolfcrypt/tests/test_mldsa.rs | 20 ++++++--- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/wrapper/rust/wolfssl-wolfcrypt/build.rs b/wrapper/rust/wolfssl-wolfcrypt/build.rs index 88dd53a621d..2965a1bf494 100644 --- a/wrapper/rust/wolfssl-wolfcrypt/build.rs +++ b/wrapper/rust/wolfssl-wolfcrypt/build.rs @@ -609,8 +609,14 @@ fn scan_cfg() -> Result<()> { check_cfg(&binding, "wc_MlDsaKey_SignCtx", "mldsa_sign"); check_cfg(&binding, "wc_MlDsaKey_SignCtxWithSeed", "mldsa_sign_with_seed"); check_cfg(&binding, "wc_MlDsaKey_VerifyCtx", "mldsa_verify"); - check_cfg(&binding, "wc_MlDsaKey_ImportPubRaw", "mldsa_import"); - check_cfg(&binding, "wc_MlDsaKey_ExportPubRaw", "mldsa_export"); + check_cfg(&binding, "wc_MlDsaKey_Size", "mldsa_size"); + check_cfg(&binding, "wc_MlDsaKey_PrivSize", "mldsa_priv_size"); + check_cfg(&binding, "wc_MlDsaKey_PubSize", "mldsa_pub_size"); + check_cfg(&binding, "wc_MlDsaKey_SigSize", "mldsa_sig_size"); + check_cfg(&binding, "wc_MlDsaKey_ImportPubRaw", "mldsa_import_public"); + check_cfg(&binding, "wc_MlDsaKey_ImportPrivRaw", "mldsa_import_private"); + check_cfg(&binding, "wc_MlDsaKey_ExportPubRaw", "mldsa_export_public"); + check_cfg(&binding, "wc_MlDsaKey_ExportPrivRaw", "mldsa_export_private"); check_cfg(&binding, "wc_MlDsaKey_CheckKey", "mldsa_check_key"); check_cfg(&binding, "WC_MLDSA_44_KEY_SIZE", "mldsa_level2"); check_cfg(&binding, "WC_MLDSA_65_KEY_SIZE", "mldsa_level3"); diff --git a/wrapper/rust/wolfssl-wolfcrypt/src/mldsa.rs b/wrapper/rust/wolfssl-wolfcrypt/src/mldsa.rs index b5ad884faf6..5c909b18b80 100644 --- a/wrapper/rust/wolfssl-wolfcrypt/src/mldsa.rs +++ b/wrapper/rust/wolfssl-wolfcrypt/src/mldsa.rs @@ -429,7 +429,7 @@ impl MlDsa { /// # Example /// /// ```rust - /// #[cfg(all(mldsa, mldsa_make_key, random))] + /// #[cfg(all(mldsa, mldsa_make_key, mldsa_size, random))] /// { /// use wolfssl_wolfcrypt::random::RNG; /// use wolfssl_wolfcrypt::mldsa::MlDsa; @@ -440,6 +440,7 @@ impl MlDsa { /// assert_eq!(sz, MlDsa::LEVEL2_KEY_SIZE); /// } /// ``` + #[cfg(mldsa_size)] pub fn size(&mut self) -> Result { let rc = unsafe { sys::wc_MlDsaKey_Size(&mut self.ws_key) }; if rc < 0 { @@ -459,7 +460,7 @@ impl MlDsa { /// # Example /// /// ```rust - /// #[cfg(all(mldsa, mldsa_make_key, random))] + /// #[cfg(all(mldsa, mldsa_make_key, mldsa_priv_size, random))] /// { /// use wolfssl_wolfcrypt::random::RNG; /// use wolfssl_wolfcrypt::mldsa::MlDsa; @@ -470,6 +471,7 @@ impl MlDsa { /// assert_eq!(sz, MlDsa::LEVEL2_PRV_KEY_SIZE); /// } /// ``` + #[cfg(mldsa_priv_size)] pub fn priv_size(&mut self) -> Result { let rc = unsafe { sys::wc_MlDsaKey_PrivSize(&mut self.ws_key) }; if rc < 0 { @@ -488,7 +490,7 @@ impl MlDsa { /// # Example /// /// ```rust - /// #[cfg(all(mldsa, mldsa_make_key, random))] + /// #[cfg(all(mldsa, mldsa_make_key, mldsa_pub_size, random))] /// { /// use wolfssl_wolfcrypt::random::RNG; /// use wolfssl_wolfcrypt::mldsa::MlDsa; @@ -499,6 +501,7 @@ impl MlDsa { /// assert_eq!(sz, MlDsa::LEVEL2_PUB_KEY_SIZE); /// } /// ``` + #[cfg(mldsa_pub_size)] pub fn pub_size(&mut self) -> Result { let rc = unsafe { sys::wc_MlDsaKey_PubSize(&mut self.ws_key) }; if rc < 0 { @@ -517,7 +520,7 @@ impl MlDsa { /// # Example /// /// ```rust - /// #[cfg(all(mldsa, mldsa_make_key, random))] + /// #[cfg(all(mldsa, mldsa_make_key, mldsa_sig_size, random))] /// { /// use wolfssl_wolfcrypt::random::RNG; /// use wolfssl_wolfcrypt::mldsa::MlDsa; @@ -528,6 +531,7 @@ impl MlDsa { /// assert_eq!(sz, MlDsa::LEVEL2_SIG_SIZE); /// } /// ``` + #[cfg(mldsa_sig_size)] pub fn sig_size(&mut self) -> Result { let rc = unsafe { sys::wc_MlDsaKey_SigSize(&mut self.ws_key) }; if rc < 0 { @@ -579,7 +583,8 @@ impl MlDsa { /// # Example /// /// ```rust - /// #[cfg(all(mldsa, mldsa_make_key, mldsa_import, mldsa_export, random))] + /// #[cfg(all(mldsa, mldsa_make_key, mldsa_pub_size, mldsa_export_public, + /// mldsa_import_public, random))] /// { /// use wolfssl_wolfcrypt::random::RNG; /// use wolfssl_wolfcrypt::mldsa::MlDsa; @@ -593,7 +598,7 @@ impl MlDsa { /// key2.import_public(&pub_buf).expect("Error with import_public()"); /// } /// ``` - #[cfg(mldsa_import)] + #[cfg(mldsa_import_public)] pub fn import_public(&mut self, public: &[u8]) -> Result<(), i32> { let public_size = crate::buffer_len_to_u32(public.len())?; let rc = unsafe { @@ -622,7 +627,8 @@ impl MlDsa { /// # Example /// /// ```rust - /// #[cfg(all(mldsa, mldsa_make_key, mldsa_import, mldsa_export, random))] + /// #[cfg(all(mldsa, mldsa_make_key, mldsa_size, mldsa_export_private, + /// mldsa_import_private, random))] /// { /// use wolfssl_wolfcrypt::random::RNG; /// use wolfssl_wolfcrypt::mldsa::MlDsa; @@ -636,7 +642,7 @@ impl MlDsa { /// key2.import_private(&priv_buf).expect("Error with import_private()"); /// } /// ``` - #[cfg(mldsa_import)] + #[cfg(mldsa_import_private)] pub fn import_private(&mut self, private: &[u8]) -> Result<(), i32> { let private_size = crate::buffer_len_to_u32(private.len())?; let rc = unsafe { @@ -663,7 +669,9 @@ impl MlDsa { /// # Example /// /// ```rust - /// #[cfg(all(mldsa, mldsa_make_key, mldsa_import, mldsa_export, random))] + /// #[cfg(all(mldsa, mldsa_make_key, mldsa_size, mldsa_pub_size, + /// mldsa_export_private, mldsa_import_private, + /// mldsa_import_public, random))] /// { /// use wolfssl_wolfcrypt::random::RNG; /// use wolfssl_wolfcrypt::mldsa::MlDsa; @@ -678,7 +686,7 @@ impl MlDsa { /// key2.import_key(&priv_buf, &pub_buf).expect("Error with import_key()"); /// } /// ``` - #[cfg(mldsa_import)] + #[cfg(all(mldsa_import_private, mldsa_import_public))] pub fn import_key(&mut self, private: &[u8], public: &[u8]) -> Result<(), i32> { let private_size = crate::buffer_len_to_u32(private.len())?; let public_size = crate::buffer_len_to_u32(public.len())?; @@ -710,7 +718,7 @@ impl MlDsa { /// # Example /// /// ```rust - /// #[cfg(all(mldsa, mldsa_make_key, mldsa_export, random))] + /// #[cfg(all(mldsa, mldsa_make_key, mldsa_pub_size, mldsa_export_public, random))] /// { /// use wolfssl_wolfcrypt::random::RNG; /// use wolfssl_wolfcrypt::mldsa::MlDsa; @@ -722,7 +730,7 @@ impl MlDsa { /// assert_eq!(written, MlDsa::LEVEL2_PUB_KEY_SIZE); /// } /// ``` - #[cfg(mldsa_export)] + #[cfg(mldsa_export_public)] pub fn export_public(&mut self, public: &mut [u8]) -> Result { let mut public_size = crate::buffer_len_to_u32(public.len())?; let rc = unsafe { @@ -749,7 +757,7 @@ impl MlDsa { /// # Example /// /// ```rust - /// #[cfg(all(mldsa, mldsa_make_key, mldsa_export, random))] + /// #[cfg(all(mldsa, mldsa_make_key, mldsa_size, mldsa_export_private, random))] /// { /// use wolfssl_wolfcrypt::random::RNG; /// use wolfssl_wolfcrypt::mldsa::MlDsa; @@ -761,7 +769,7 @@ impl MlDsa { /// assert_eq!(written, MlDsa::LEVEL2_KEY_SIZE); /// } /// ``` - #[cfg(mldsa_export)] + #[cfg(mldsa_export_private)] pub fn export_private(&mut self, private: &mut [u8]) -> Result { let mut private_size = crate::buffer_len_to_u32(private.len())?; let rc = unsafe { @@ -793,7 +801,8 @@ impl MlDsa { /// # Example /// /// ```rust - /// #[cfg(all(mldsa, mldsa_make_key, mldsa_export, random))] + /// #[cfg(all(mldsa, mldsa_make_key, mldsa_size, mldsa_pub_size, + /// mldsa_export_private, mldsa_export_public, random))] /// { /// use wolfssl_wolfcrypt::random::RNG; /// use wolfssl_wolfcrypt::mldsa::MlDsa; @@ -805,7 +814,7 @@ impl MlDsa { /// key.export_key(&mut priv_buf, &mut pub_buf).expect("Error with export_key()"); /// } /// ``` - #[cfg(mldsa_export)] + #[cfg(all(mldsa_export_private, mldsa_export_public))] pub fn export_key(&mut self, private: &mut [u8], public: &mut [u8]) -> Result<(), i32> { let mut private_size = crate::buffer_len_to_u32(private.len())?; let mut public_size = crate::buffer_len_to_u32(public.len())?; diff --git a/wrapper/rust/wolfssl-wolfcrypt/tests/test_mldsa.rs b/wrapper/rust/wolfssl-wolfcrypt/tests/test_mldsa.rs index 99ab17b6d55..017d82057fb 100644 --- a/wrapper/rust/wolfssl-wolfcrypt/tests/test_mldsa.rs +++ b/wrapper/rust/wolfssl-wolfcrypt/tests/test_mldsa.rs @@ -64,7 +64,8 @@ fn test_new_ex() { /// Verify the runtime size queries match the compile-time constants for /// ML-DSA-44. #[test] -#[cfg(all(mldsa_make_key, mldsa_level2))] +#[cfg(all(mldsa_make_key, mldsa_level2, mldsa_size, mldsa_priv_size, + mldsa_pub_size, mldsa_sig_size))] fn test_sizes_level44() { common::setup(); let mut rng = RNG::new().expect("Error creating RNG"); @@ -79,7 +80,8 @@ fn test_sizes_level44() { /// Verify the runtime size queries match the compile-time constants for /// ML-DSA-65. #[test] -#[cfg(all(mldsa_make_key, mldsa_level3))] +#[cfg(all(mldsa_make_key, mldsa_level3, mldsa_size, mldsa_priv_size, + mldsa_pub_size, mldsa_sig_size))] fn test_sizes_level65() { common::setup(); let mut rng = RNG::new().expect("Error creating RNG"); @@ -94,7 +96,8 @@ fn test_sizes_level65() { /// Verify the runtime size queries match the compile-time constants for /// ML-DSA-87. #[test] -#[cfg(all(mldsa_make_key, mldsa_level5))] +#[cfg(all(mldsa_make_key, mldsa_level5, mldsa_size, mldsa_priv_size, + mldsa_pub_size, mldsa_sig_size))] fn test_sizes_level87() { common::setup(); let mut rng = RNG::new().expect("Error creating RNG"); @@ -237,7 +240,9 @@ fn test_sign_ctx_verify_level44() { /// - the re-imported private key can sign messages that verify with the /// original public key. #[test] -#[cfg(all(mldsa_make_key, mldsa_import, mldsa_export, mldsa_sign, mldsa_verify))] +#[cfg(all(mldsa_make_key, mldsa_size, mldsa_pub_size, mldsa_sig_size, + mldsa_import_public, mldsa_import_private, mldsa_export_public, + mldsa_export_private, mldsa_sign, mldsa_verify))] fn test_import_export_level44() { common::setup(); let mut rng = RNG::new().expect("Error creating RNG"); @@ -292,7 +297,9 @@ fn test_import_export_level44() { /// Export both keys, import them together via `import_key()`, then sign and /// verify using the re-imported key pair. #[test] -#[cfg(all(mldsa_make_key, mldsa_import, mldsa_export, mldsa_sign, mldsa_verify))] +#[cfg(all(mldsa_make_key, mldsa_size, mldsa_pub_size, mldsa_sig_size, + mldsa_import_public, mldsa_import_private, mldsa_export_public, + mldsa_export_private, mldsa_sign, mldsa_verify))] fn test_import_key_level44() { common::setup(); let mut rng = RNG::new().expect("Error creating RNG"); @@ -323,7 +330,8 @@ fn test_import_key_level44() { /// Verify that `generate_from_seed()` is deterministic: the same seed /// produces the same key pair on repeated calls. #[test] -#[cfg(all(mldsa_make_key_from_seed, mldsa_export))] +#[cfg(all(mldsa_make_key_from_seed, mldsa_size, mldsa_pub_size, + mldsa_export_public, mldsa_export_private))] fn test_generate_from_seed_determinism() { common::setup(); // MLDSA_SEED_SZ = 32 bytes