diff --git a/wolfcrypt/src/dh.c b/wolfcrypt/src/dh.c index 99c09a97b..1035cbc65 100644 --- a/wolfcrypt/src/dh.c +++ b/wolfcrypt/src/dh.c @@ -3228,6 +3228,7 @@ int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh) return ret; } +#endif /* WOLFSSL_KEY_GEN */ /* Export raw DH parameters from DhKey structure * @@ -3325,6 +3326,4 @@ int wc_DhExportParamsRaw(DhKey* dh, byte* p, word32* pSz, return ret; } -#endif /* WOLFSSL_KEY_GEN */ - #endif /* NO_DH */ diff --git a/wolfssl/wolfcrypt/dh.h b/wolfssl/wolfcrypt/dh.h index 0bb8508a7..2b32c9c9b 100644 --- a/wolfssl/wolfcrypt/dh.h +++ b/wolfssl/wolfcrypt/dh.h @@ -196,11 +196,12 @@ WOLFSSL_API int wc_DhCheckPrivKey_ex(DhKey* key, const byte* priv, word32 privSz, const byte* prime, word32 primeSz); WOLFSSL_API int wc_DhCheckKeyPair(DhKey* key, const byte* pub, word32 pubSz, const byte* priv, word32 privSz); +#ifdef WOLFSSL_KEY_GEN WOLFSSL_API int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh); +#endif WOLFSSL_API int wc_DhExportParamsRaw(DhKey* dh, byte* p, word32* pSz, byte* q, word32* qSz, byte* g, word32* gSz); - #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/wolfssl/wolfcrypt/ecc.h b/wolfssl/wolfcrypt/ecc.h index 3f6273a1c..9089b876e 100644 --- a/wolfssl/wolfcrypt/ecc.h +++ b/wolfssl/wolfcrypt/ecc.h @@ -913,9 +913,11 @@ int wc_ecc_export_point_der_ex(const int curve_idx, ecc_point* point, byte* out, WOLFSSL_API int wc_ecc_export_point_der(const int curve_idx, ecc_point* point, byte* out, word32* outLen); +#ifdef HAVE_COMP_KEY WOLFSSL_LOCAL int wc_ecc_export_point_der_compressed(const int curve_idx, ecc_point* point, byte* out, word32* outLen); +#endif /* HAVE_COMP_KEY */ #endif /* HAVE_ECC_KEY_EXPORT */ diff --git a/wrapper/rust/wolfssl/Cargo.lock b/wrapper/rust/wolfssl/Cargo.lock index e1d06cdef..603e91f1d 100644 --- a/wrapper/rust/wolfssl/Cargo.lock +++ b/wrapper/rust/wolfssl/Cargo.lock @@ -290,4 +290,5 @@ name = "wolfssl" version = "0.1.0" dependencies = [ "bindgen", + "regex", ] diff --git a/wrapper/rust/wolfssl/Cargo.toml b/wrapper/rust/wolfssl/Cargo.toml index 6bea73696..78d876eff 100644 --- a/wrapper/rust/wolfssl/Cargo.toml +++ b/wrapper/rust/wolfssl/Cargo.toml @@ -8,6 +8,7 @@ std = [] [build-dependencies] bindgen = "0.72.1" +regex = "1.5" [profile.release] strip = true diff --git a/wrapper/rust/wolfssl/build.rs b/wrapper/rust/wolfssl/build.rs index bf362ff3b..de4f95c41 100644 --- a/wrapper/rust/wolfssl/build.rs +++ b/wrapper/rust/wolfssl/build.rs @@ -1,7 +1,9 @@ extern crate bindgen; +use regex::Regex; use std::env; -use std::io::{self, Result}; +use std::fs; +use std::io::{self, Read, Result}; use std::path::PathBuf; /// Perform crate build. @@ -18,9 +20,26 @@ fn main() { fn run_build() -> Result<()> { generate_bindings()?; setup_wolfssl_link()?; + scan_cfg()?; Ok(()) } +fn wrapper_dir() -> Result { + Ok(std::env::current_dir()?.display().to_string()) +} + +fn wolfssl_base_dir() -> Result { + Ok(format!("{}/../../..", wrapper_dir()?)) +} + +fn wolfssl_lib_dir() -> Result { + Ok(format!("{}/src/.libs", wolfssl_base_dir()?)) +} + +fn bindings_path() -> String { + PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs").display().to_string() +} + /// Generate Rust bindings for the wolfssl C library using bindgen. /// /// This function: @@ -31,19 +50,15 @@ fn run_build() -> Result<()> { /// /// Returns `Ok(())` if successful, or an error if binding generation fails. fn generate_bindings() -> Result<()> { - let wrapper_dir = std::env::current_dir()?.display().to_string(); - let wolfssl_base_dir = format!("{}/../../..", wrapper_dir); - let bindings = bindgen::Builder::default() .header("headers.h") - .clang_arg(format!("-I{}", wolfssl_base_dir)) + .clang_arg(format!("-I{}", wolfssl_base_dir()?)) .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) .generate() .map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to generate bindings"))?; - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); bindings - .write_to_file(out_path.join("bindings.rs")) + .write_to_file(bindings_path()) .map_err(|e| { io::Error::new( io::ErrorKind::Other, @@ -56,16 +71,112 @@ fn generate_bindings() -> Result<()> { /// /// Returns `Ok(())` if successful, or an error if any step fails. fn setup_wolfssl_link() -> Result<()> { - let wrapper_dir = std::env::current_dir()?.display().to_string(); - let wolfssl_base_dir = format!("{}/../../..", wrapper_dir); - let wolfssl_lib_dir = format!("{}/src/.libs", wolfssl_base_dir); - - println!("cargo:rustc-link-search={}", wolfssl_lib_dir); println!("cargo:rustc-link-lib=wolfssl"); - println!("cargo:rustc-link-arg=-Wl,-rpath,{}", wolfssl_lib_dir); + println!("cargo:rustc-link-search={}", wolfssl_lib_dir()?); + println!("cargo:rustc-link-arg=-Wl,-rpath,{}", wolfssl_lib_dir()?); // TODO: do we need this if only a static library is built? // println!("cargo:rustc-link-lib=static=wolfssl"); Ok(()) } + +fn read_file(path: String) -> Result { + let mut file = fs::File::open(path)?; + let mut content = String::new(); + file.read_to_string(&mut content)?; + Ok(content) +} + +fn check_cfg(binding: &str, function_name: &str, cfg_name: &str) { + let pattern = format!(r"\b{}\b", function_name); + let re = match Regex::new(&pattern) { + Ok(r) => r, + Err(e) => { + eprintln!("Error compiling regex '{}': {}", pattern, e); + return; + } + }; + println!("cargo::rustc-check-cfg=cfg({})", cfg_name); + if re.is_match(binding) { + println!("cargo:rustc-cfg={}", cfg_name); + } +} + +fn scan_cfg() -> Result<()> { + let binding = read_file(bindings_path())?; + + /* aes */ + check_cfg(&binding, "wc_AesSetKey", "aes"); + check_cfg(&binding, "wc_AesCbcEncrypt", "aes_cbc"); + check_cfg(&binding, "wc_AesCcmSetKey", "aes_ccm"); + check_cfg(&binding, "wc_AesCfbEncrypt", "aes_cfb"); + check_cfg(&binding, "wc_AesCtrEncrypt", "aes_ctr"); + check_cfg(&binding, "wc_AesCtsEncrypt", "aes_cts"); + check_cfg(&binding, "wc_AesCfbDecrypt", "aes_decrypt"); + check_cfg(&binding, "wc_AesEaxInit", "aes_eax"); + check_cfg(&binding, "wc_AesEcbEncrypt", "aes_ecb"); + check_cfg(&binding, "wc_AesGcmSetKey", "aes_gcm"); + check_cfg(&binding, "wc_AesGcmInit", "aes_gcm_stream"); + check_cfg(&binding, "wc_AesOfbEncrypt", "aes_ofb"); + check_cfg(&binding, "wc_AesXtsInit", "aes_xts"); + check_cfg(&binding, "wc_AesXtsEncryptInit", "aes_xts_stream"); + + /* cmac */ + check_cfg(&binding, "wc_InitCmac", "cmac"); + + /* dh */ + check_cfg(&binding, "wc_InitDhKey", "dh"); + check_cfg(&binding, "wc_DhGenerateParams", "dh_keygen"); + + /* ecc */ + check_cfg(&binding, "wc_ecc_init", "ecc"); + check_cfg(&binding, "wc_ecc_export_point_der_compressed", "ecc_comp_key"); + check_cfg(&binding, "wc_ecc_shared_secret", "ecc_dh"); + check_cfg(&binding, "wc_ecc_sign_hash", "ecc_sign"); + check_cfg(&binding, "wc_ecc_verify_hash", "ecc_verify"); + check_cfg(&binding, "wc_ecc_export_x963", "ecc_export"); + check_cfg(&binding, "wc_ecc_import_x963", "ecc_import"); + check_cfg(&binding, "ecc_curve_ids_ECC_X25519", "ecc_curve_25519"); + check_cfg(&binding, "ecc_curve_ids_ECC_X448", "ecc_curve_448"); + check_cfg(&binding, "ecc_curve_ids_ECC_SAKKE_1", "ecc_curve_sakke"); + check_cfg(&binding, "ecc_curve_ids_ECC_CURVE_CUSTOM", "ecc_custom_curves"); + + /* ed25519 */ + check_cfg(&binding, "wc_ed25519_init", "ed25519"); + check_cfg(&binding, "wc_ed25519_import_public", "ed25519_import"); + check_cfg(&binding, "wc_ed25519_export_public", "ed25519_export"); + check_cfg(&binding, "wc_ed25519_sign_msg", "ed25519_sign"); + check_cfg(&binding, "wc_ed25519_verify_msg_ex", "ed25519_verify"); + check_cfg(&binding, "wc_ed25519_verify_msg_init", "ed25519_streaming_verify"); + + /* ed448 */ + check_cfg(&binding, "wc_ed448_init", "ed448"); + check_cfg(&binding, "wc_ed448_import_public", "ed448_import"); + check_cfg(&binding, "wc_ed448_export_public", "ed448_export"); + check_cfg(&binding, "wc_ed448_sign_msg", "ed448_sign"); + check_cfg(&binding, "wc_ed448_verify_msg_ex", "ed448_verify"); + check_cfg(&binding, "wc_ed448_verify_msg_init", "ed448_streaming_verify"); + + /* kdf */ + check_cfg(&binding, "wc_PBKDF2", "kdf_pbkdf2"); + check_cfg(&binding, "wc_PKCS12_PBKDF_ex", "kdf_pkcs12"); + check_cfg(&binding, "wc_SRTP_KDF", "kdf_srtp"); + check_cfg(&binding, "wc_SSH_KDF", "kdf_ssh"); + check_cfg(&binding, "wc_Tls13_HKDF_Extract_ex", "kdf_tls13"); + + /* rsa */ + check_cfg(&binding, "wc_InitRsaKey", "rsa"); + check_cfg(&binding, "wc_RsaDirect", "rsa_direct"); + check_cfg(&binding, "wc_MakeRsaKey", "rsa_keygen"); + + /* sha */ + check_cfg(&binding, "wc_InitSha", "sha"); + check_cfg(&binding, "wc_InitSha256", "sha256"); + check_cfg(&binding, "wc_InitSha512", "sha512"); + check_cfg(&binding, "wc_InitSha3_224", "sha3"); + check_cfg(&binding, "wc_InitShake128", "shake128"); + check_cfg(&binding, "wc_InitShake256", "shake256"); + + Ok(()) +} diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs b/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs index c8a77c438..02f83bb2a 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs @@ -23,6 +23,8 @@ This module provides a Rust wrapper for the wolfCrypt library's Advanced Encryption Standard (AES) functionality. */ +#![cfg(aes)] + use crate::sys; use std::mem::{size_of, MaybeUninit}; @@ -30,6 +32,8 @@ use std::mem::{size_of, MaybeUninit}; /// /// # Example /// ```rust +/// #[cfg(aes_cbc)] +/// { /// use wolfssl::wolfcrypt::aes::CBC; /// let mut cbc = CBC::new().expect("Failed to create CBC"); /// let key: &[u8; 16] = b"0123456789abcdef"; @@ -50,10 +54,13 @@ use std::mem::{size_of, MaybeUninit}; /// cbc.init_decrypt(key, iv).expect("Error with init_decrypt()"); /// cbc.decrypt(&cipher, &mut plain_out).expect("Error with decrypt()"); /// assert_eq!(&plain_out, &msg); +/// } /// ``` +#[cfg(aes_cbc)] pub struct CBC { ws_aes: sys::Aes, } +#[cfg(aes_cbc)] impl CBC { /// Create a new `CBC` instance. /// @@ -200,6 +207,7 @@ impl CBC { Ok(()) } } +#[cfg(aes_cbc)] impl Drop for CBC { /// Safely free the wolfSSL resources. fn drop(&mut self) { @@ -211,6 +219,8 @@ impl Drop for CBC { /// /// # Example /// ```rust +/// #[cfg(aes_ccm)] +/// { /// use wolfssl::wolfcrypt::aes::CCM; /// let key: [u8; 16] = [ /// 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, @@ -218,8 +228,7 @@ impl Drop for CBC { /// ]; /// let nonce: [u8; 13] = [ /// 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0, -/// 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 -/// ]; +/// 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 ]; /// let plaintext: [u8; 23] = [ /// 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /// 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, @@ -250,10 +259,13 @@ impl Drop for CBC { /// ccm.decrypt(&cipher_out, &mut plain_out, /// &nonce, &auth_data, &auth_tag_out).expect("Error with decrypt()"); /// assert_eq!(plain_out, plaintext); +/// } /// ``` +#[cfg(aes_ccm)] pub struct CCM { ws_aes: sys::Aes, } +#[cfg(aes_ccm)] impl CCM { /// Create a new `CCM` instance. /// @@ -395,6 +407,7 @@ impl CCM { Ok(()) } } +#[cfg(aes_ccm)] impl Drop for CCM { /// Safely free the wolfSSL resources. fn drop(&mut self) { @@ -406,6 +419,8 @@ impl Drop for CCM { /// /// # Example /// ```rust +/// #[cfg(aes_cfb)] +/// { /// use wolfssl::wolfcrypt::aes::CFB; /// let mut cfb = CFB::new().expect("Failed to create CFB"); /// let key: [u8; 16] = [ @@ -439,12 +454,18 @@ impl Drop for CCM { /// assert_eq!(outbuf, cipher); /// cfb.init(&key, &iv).expect("Error with init()"); /// let mut plain: [u8; 48] = [0; 48]; +/// #[cfg(aes_decrypt)] +/// { /// cfb.decrypt(&outbuf, &mut plain).expect("Error with decrypt()"); /// assert_eq!(plain, msg); +/// } +/// } /// ``` +#[cfg(aes_cfb)] pub struct CFB { ws_aes: sys::Aes, } +#[cfg(aes_cfb)] impl CFB { /// Create a new `CFB` instance. /// @@ -613,6 +634,7 @@ impl CFB { /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. + #[cfg(aes_decrypt)] pub fn decrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; let in_size = (din.len() * size_of::()) as u32; @@ -644,6 +666,7 @@ impl CFB { /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. + #[cfg(aes_decrypt)] pub fn decrypt1(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; let in_size = (din.len() * size_of::()) as u32; @@ -675,6 +698,7 @@ impl CFB { /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. + #[cfg(aes_decrypt)] pub fn decrypt8(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; let in_size = (din.len() * size_of::()) as u32; @@ -692,6 +716,7 @@ impl CFB { Ok(()) } } +#[cfg(aes_cfb)] impl Drop for CFB { /// Safely free the wolfSSL resources. fn drop(&mut self) { @@ -703,6 +728,8 @@ impl Drop for CFB { /// /// # Example /// ```rust +/// #[cfg(aes_ctr)] +/// { /// use wolfssl::wolfcrypt::aes::CTR; /// let iv: [u8; 16] = [ /// 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7, @@ -741,10 +768,13 @@ impl Drop for CFB { /// let mut plain: [u8; 64] = [0; 64]; /// ctr.decrypt(&outbuf, &mut plain).expect("Error with decrypt()"); /// assert_eq!(plain, msg); +/// } /// ``` +#[cfg(aes_ctr)] pub struct CTR { ws_aes: sys::Aes, } +#[cfg(aes_ctr)] impl CTR { /// Create a new `CTR` instance. /// @@ -858,6 +888,7 @@ impl CTR { return self.encrypt_decrypt(din, dout); } } +#[cfg(aes_ctr)] impl Drop for CTR { /// Safely free the wolfSSL resources. fn drop(&mut self) { @@ -869,6 +900,8 @@ impl Drop for CTR { /// /// # Example /// ```rust +/// #[cfg(aes_eax)] +/// { /// use wolfssl::wolfcrypt::aes::EAX; /// let key: [u8; 16] = [ /// 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, @@ -903,9 +936,12 @@ impl Drop for CTR { /// let mut plain: [u8; 32] = [0; 32]; /// EAX::decrypt(&cipher, &mut plain, &key, &nonce, auth, &auth_tag).expect("Error with decrypt()"); /// assert_eq!(plain, msg); +/// } /// ``` +#[cfg(aes_eax)] pub struct EAX { } +#[cfg(aes_eax)] impl EAX { /// Encrypt data. /// @@ -1004,6 +1040,8 @@ impl EAX { /// /// # Example /// ```rust +/// #[cfg(aes_ecb)] +/// { /// use wolfssl::wolfcrypt::aes::ECB; /// let mut ecb = ECB::new().expect("Failed to create ECB"); /// let key_128: &[u8; 16] = b"0123456789abcdef"; @@ -1023,10 +1061,13 @@ impl EAX { /// ecb.init_decrypt(key_128).expect("Error with init_decrypt()"); /// ecb.decrypt(&verify_ecb_128, &mut outbuf).expect("Error with decrypt()"); /// assert_eq!(&outbuf, &msg); +/// } /// ``` +#[cfg(aes_ecb)] pub struct ECB { ws_aes: sys::Aes, } +#[cfg(aes_ecb)] impl ECB { /// Create a new `ECB` instance. /// @@ -1166,6 +1207,7 @@ impl ECB { Ok(()) } } +#[cfg(aes_ecb)] impl Drop for ECB { /// Safely free the wolfSSL resources. fn drop(&mut self) { @@ -1180,6 +1222,8 @@ impl Drop for ECB { /// /// # Example /// ```rust +/// #[cfg(aes_gcm)] +/// { /// use wolfssl::wolfcrypt::aes::GCM; /// let key: [u8; 16] = [ /// 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62, @@ -1219,10 +1263,13 @@ impl Drop for ECB { /// let mut plain_out: [u8; 32] = [0; 32]; /// gcm.decrypt(&cipher, &mut plain_out, &iv, &auth, &auth_tag).expect("Error with decrypt()"); /// assert_eq!(plain_out, plain); +/// } /// ``` +#[cfg(aes_gcm)] pub struct GCM { ws_aes: sys::Aes, } +#[cfg(aes_gcm)] impl GCM { /// Create a new `GCM` instance. /// @@ -1366,6 +1413,7 @@ impl GCM { Ok(()) } } +#[cfg(aes_gcm)] impl Drop for GCM { /// Safely free the wolfSSL resources. fn drop(&mut self) { @@ -1380,6 +1428,8 @@ impl Drop for GCM { /// /// # Example /// ```rust +/// #[cfg(aes_gcm_stream)] +/// { /// use wolfssl::wolfcrypt::aes::GCMStream; /// let plain: [u8; 60] = [ /// 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, @@ -1447,10 +1497,13 @@ impl Drop for GCM { /// assert_eq!(cipher, expected_cipher); /// assert_eq!(auth_tag, expected_auth_tag); /// } +/// } /// ``` +#[cfg(aes_gcm_stream)] pub struct GCMStream { ws_aes: sys::Aes, } +#[cfg(aes_gcm_stream)] impl GCMStream { /// Create a new `GCMStream` instance. /// @@ -1648,6 +1701,7 @@ impl GCMStream { Ok(()) } } +#[cfg(aes_gcm_stream)] impl Drop for GCMStream { /// Safely free the wolfSSL resources. fn drop(&mut self) { @@ -1659,6 +1713,8 @@ impl Drop for GCMStream { /// /// # Example /// ```rust +/// #[cfg(aes_ofb)] +/// { /// use wolfssl::wolfcrypt::aes::OFB; /// let key: [u8; 32] = [ /// 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71, @@ -1693,12 +1749,18 @@ impl Drop for GCMStream { /// assert_eq!(cipher, expected_cipher); /// ofb.init(&key, &iv).expect("Error with init()"); /// let mut plain_out: [u8; 48] = [0; 48]; +/// #[cfg(aes_decrypt)] +/// { /// ofb.decrypt(&cipher, &mut plain_out).expect("Error with decrypt()"); /// assert_eq!(plain_out, plain); +/// } +/// } /// ``` +#[cfg(aes_ofb)] pub struct OFB { ws_aes: sys::Aes, } +#[cfg(aes_ofb)] impl OFB { /// Create a new `OFB` instance. /// @@ -1804,6 +1866,7 @@ impl OFB { /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. + #[cfg(aes_decrypt)] pub fn decrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; let in_size = (din.len() * size_of::()) as u32; @@ -1821,6 +1884,7 @@ impl OFB { Ok(()) } } +#[cfg(aes_ofb)] impl Drop for OFB { /// Safely free the wolfSSL resources. fn drop(&mut self) { @@ -1836,6 +1900,8 @@ impl Drop for OFB { /// /// # Example /// ```rust +/// #[cfg(aes_xts)] +/// { /// use wolfssl::wolfcrypt::aes::XTS; /// let key: [u8; 32] = [ /// 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35, @@ -1884,10 +1950,13 @@ impl Drop for OFB { /// let mut partial_out: [u8; 24] = [0; 24]; /// xts.decrypt(&partial_cipher, &mut partial_out, &tweak).expect("Error with decrypt()"); /// assert_eq!(partial_out, partial); +/// } /// ``` +#[cfg(aes_xts)] pub struct XTS { ws_xtsaes: sys::XtsAes, } +#[cfg(aes_xts)] impl XTS { /// Create a new `XTS` instance. /// @@ -2191,6 +2260,7 @@ impl XTS { Ok(()) } } +#[cfg(aes_xts)] impl Drop for XTS { /// Safely free the wolfSSL resources. fn drop(&mut self) { @@ -2206,6 +2276,8 @@ impl Drop for XTS { /// /// # Example /// ```rust +/// #[cfg(aes_xts_stream)] +/// { /// use wolfssl::wolfcrypt::aes::XTSStream; /// let keys: [u8; 32] = [ /// 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, @@ -2244,11 +2316,14 @@ impl Drop for XTS { /// xtsstream.decrypt_update(&cipher[0..16], &mut plain_out[0..16]).expect("Error with decrypt_update()"); /// xtsstream.decrypt_final(&cipher[16..40], &mut plain_out[16..40]).expect("Error with decrypt_final()"); /// assert_eq!(plain_out, plain); +/// } /// ``` +#[cfg(aes_xts_stream)] pub struct XTSStream { ws_xtsaes: sys::XtsAes, ws_xtsaesstreamdata: sys::XtsAesStreamData, } +#[cfg(aes_xts_stream)] impl XTSStream { /// Create a new `XTSStream` instance. /// @@ -2493,6 +2568,7 @@ impl XTSStream { Ok(()) } } +#[cfg(aes_xts_stream)] impl Drop for XTSStream { /// Safely free the wolfSSL resources. fn drop(&mut self) { @@ -2520,6 +2596,7 @@ fn new_ws_aes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> R Ok(ws_aes) } +#[cfg(any(aes_xts, aes_xts_stream))] fn new_ws_xtsaes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let heap = match heap { Some(heap) => heap, diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs b/wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs index 5b06e403e..fbb722176 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs @@ -23,6 +23,8 @@ This module provides a Rust wrapper for the wolfCrypt library's Cipher-based Message Authentication Code (CMAC) functionality. */ +#![cfg(cmac)] + use crate::sys; use std::mem::MaybeUninit; @@ -51,6 +53,8 @@ impl CMAC { /// # Example /// /// ```rust + /// #[cfg(aes)] + /// { /// use wolfssl::wolfcrypt::cmac::CMAC; /// let key = [ /// 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, @@ -62,7 +66,9 @@ impl CMAC { /// ]; /// let mut generate_out = [0u8; 16]; /// CMAC::generate(&key, &message, &mut generate_out).expect("Error with generate()"); + /// } /// ``` + #[cfg(aes)] pub fn generate(key: &[u8], data: &[u8], dout: &mut [u8]) -> Result<(), i32> { let key_size = key.len() as u32; let data_size = data.len() as u32; @@ -168,6 +174,8 @@ impl CMAC { /// # Example /// /// ```rust + /// #[cfg(aes)] + /// { /// use wolfssl::wolfcrypt::cmac::CMAC; /// let key = [ /// 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, @@ -181,7 +189,9 @@ impl CMAC { /// CMAC::generate(&key, &message, &mut generate_out).expect("Error with generate()"); /// let valid = CMAC::verify(&key, &message, &generate_out).expect("Error with verify()"); /// assert!(valid); + /// } /// ``` + #[cfg(aes)] pub fn verify(key: &[u8], data: &[u8], check: &[u8]) -> Result { let key_size = key.len() as u32; let data_size = data.len() as u32; @@ -215,6 +225,8 @@ impl CMAC { /// # Example /// /// ```rust + /// #[cfg(aes)] + /// { /// use wolfssl::wolfcrypt::cmac::CMAC; /// let key = [ /// 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, @@ -227,7 +239,9 @@ impl CMAC { /// let mut generate_out = [0u8; 16]; /// let mut cmac = CMAC::new(&key).expect("Error with new()"); /// cmac.generate_ex(&key, &message, &mut generate_out, None, None).expect("Error with generate_ex()"); + /// } /// ``` + #[cfg(aes)] pub fn generate_ex(&mut self, key: &[u8], data: &[u8], dout: &mut [u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result<(), i32> { let key_size = key.len() as u32; let data_size = data.len() as u32; @@ -351,6 +365,8 @@ impl CMAC { /// # Example /// /// ```rust + /// #[cfg(aes)] + /// { /// use wolfssl::wolfcrypt::cmac::CMAC; /// let key = [ /// 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, @@ -365,7 +381,9 @@ impl CMAC { /// let mut cmac = CMAC::new(&key).expect("Error with new()"); /// let valid = cmac.verify_ex(&key, &message, &generate_out, None, None).expect("Error with verify_ex()"); /// assert!(valid); + /// } /// ``` + #[cfg(aes)] pub fn verify_ex(&mut self, key: &[u8], data: &[u8], check: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let key_size = key.len() as u32; let data_size = data.len() as u32; diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/dh.rs b/wrapper/rust/wolfssl/src/wolfcrypt/dh.rs index 4739dd066..573d4179f 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/dh.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/dh.rs @@ -26,6 +26,8 @@ The primary component is the `DH` struct, which manages the lifecycle of a wolfSSL `DhKey` object. It ensures proper initialization and deallocation. */ +#![cfg(dh)] + use crate::sys; use crate::wolfcrypt::random::RNG; use std::mem::{MaybeUninit}; @@ -164,11 +166,15 @@ impl DH { /// # Example /// /// ```rust + /// #[cfg(dh_keygen)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::dh::DH; /// let mut rng = RNG::new().expect("Error with RNG::new()"); /// let mut dh = DH::generate(&mut rng, 2048).expect("Error with generate()"); + /// } /// ``` + #[cfg(dh_keygen)] pub fn generate(rng: &mut RNG, modulus_size: i32) -> Result { Self::generate_ex(rng, modulus_size, None, None) } @@ -191,11 +197,15 @@ impl DH { /// # Example /// /// ```rust + /// #[cfg(dh_keygen)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::dh::DH; /// let mut rng = RNG::new().expect("Error with RNG::new()"); /// let mut dh = DH::generate_ex(&mut rng, 2048, None, None).expect("Error with generate_ex()"); + /// } /// ``` + #[cfg(dh_keygen)] pub fn generate_ex(rng: &mut RNG, modulus_size: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let mut wc_dhkey: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs b/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs index ebde297c1..f65129fef 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs @@ -26,6 +26,8 @@ The primary component is the `ECC` struct, which manages the lifecycle of a wolfSSL `ecc_key` object. It ensures proper initialization and deallocation. */ +#![cfg(ecc)] + use crate::sys; use crate::wolfcrypt::random::RNG; use std::mem::{MaybeUninit}; @@ -53,6 +55,8 @@ impl ECCPoint { /// # Example /// /// ```rust + /// #[cfg(ecc_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::{ECC,ECCPoint}; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -63,7 +67,9 @@ impl ECCPoint { /// let mut der = [0u8; 128]; /// let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()"); /// ECCPoint::import_der(&der[0..size], curve_id, None).expect("Error with import_der()"); + /// } /// ``` + #[cfg(ecc_import)] pub fn import_der(din: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>) -> Result { let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) }; if curve_idx < 0 { @@ -107,6 +113,8 @@ impl ECCPoint { /// # Example /// /// ```rust + /// #[cfg(all(ecc_import, ecc_export, ecc_comp_key))] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::{ECC,ECCPoint}; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -117,7 +125,9 @@ impl ECCPoint { /// let mut der = [0u8; 128]; /// let size = ecc_point.export_der_compressed(&mut der, curve_id).expect("Error with export_der_compressed()"); /// ECCPoint::import_der_ex(&der[0..size], curve_id, 1, None).expect("Error with import_der_ex()"); + /// } /// ``` + #[cfg(ecc_import)] pub fn import_der_ex(din: &[u8], curve_id: i32, short_key_size: i32, heap: Option<*mut std::os::raw::c_void>) -> Result { let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) }; if curve_idx < 0 { @@ -158,6 +168,8 @@ impl ECCPoint { /// # Example /// /// ```rust + /// #[cfg(ecc_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::{ECC,ECCPoint}; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -169,7 +181,9 @@ impl ECCPoint { /// let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()"); /// assert!(size > 0 && size <= der.len()); /// ECCPoint::import_der(&der[0..size], curve_id, None).expect("Error with import_der()"); + /// } /// ``` + #[cfg(ecc_export)] pub fn export_der(&self, dout: &mut [u8], curve_id: i32) -> Result { let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) }; if curve_idx < 0 { @@ -201,6 +215,8 @@ impl ECCPoint { /// # Example /// /// ```rust + /// #[cfg(all(ecc_export, ecc_comp_key))] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::{ECC,ECCPoint}; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -211,7 +227,9 @@ impl ECCPoint { /// let mut der = [0u8; 128]; /// let size = ecc_point.export_der_compressed(&mut der, curve_id).expect("Error with export_der_compressed()"); /// ECCPoint::import_der_ex(&der[0..size], curve_id, 1, None).expect("Error with import_der_ex()"); + /// } /// ``` + #[cfg(all(ecc_export, ecc_comp_key))] pub fn export_der_compressed(&self, dout: &mut [u8], curve_id: i32) -> Result { let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) }; if curve_idx < 0 { @@ -300,9 +318,13 @@ impl ECC { pub const BRAINPOOLP384R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP384R1; pub const BRAINPOOLP512R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP512R1; pub const SM2P256V1: i32 = sys::ecc_curve_ids_ECC_SM2P256V1; + #[cfg(ecc_curve_25519)] pub const X25519: i32 = sys::ecc_curve_ids_ECC_X25519; + #[cfg(ecc_curve_448)] pub const X448: i32 = sys::ecc_curve_ids_ECC_X448; + #[cfg(ecc_curve_sakke)] pub const SAKKE_1: i32 = sys::ecc_curve_ids_ECC_SAKKE_1; + #[cfg(ecc_custom_curves)] pub const CURVE_CUSTOM: i32 = sys::ecc_curve_ids_ECC_CURVE_CUSTOM; pub const CURVE_MAX: i32 = sys::ecc_curve_ids_ECC_CURVE_MAX; @@ -628,6 +650,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -644,7 +668,9 @@ impl ECC { /// let mut ecc2 = ECC::import_private_key(&d, x963, None, None).expect("Error with import_private_key()"); /// let valid = ecc2.verify_hash(&signature, &hash).expect("Error with verify_hash()"); /// assert_eq!(valid, true); + /// } /// ``` + #[cfg(ecc_import)] pub fn import_private_key(priv_buf: &[u8], pub_buf: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { @@ -695,6 +721,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -713,7 +741,9 @@ impl ECC { /// let mut ecc2 = ECC::import_private_key_ex(&d, x963, curve_id, None, None).expect("Error with import_private_key_ex()"); /// let valid = ecc2.verify_hash(&signature, &hash).expect("Error with verify_hash()"); /// assert_eq!(valid, true); + /// } /// ``` + #[cfg(ecc_import)] pub fn import_private_key_ex(priv_buf: &[u8], pub_buf: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { @@ -762,13 +792,17 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let qx = b"7a4e287890a1a47ad3457e52f2f76a83ce46cbc947616d0cbaa82323818a793d\0"; /// let qy = b"eec4084f5b29ebf29c44cce3b3059610922f8b30ea6e8811742ac7238fe87308\0"; /// let d = b"8c14b793cb19137e323a6d2e2a870bca2e7a493ec1153b3a95feb8a4873f8d08\0"; /// ECC::import_raw(qx, qy, d, b"SECP256R1\0", None, None).expect("Error with import_raw()"); + /// } /// ``` + #[cfg(ecc_import)] pub fn import_raw(qx: &[u8], qy: &[u8], d: &[u8], curve_name: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { @@ -819,13 +853,17 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let qx = b"7a4e287890a1a47ad3457e52f2f76a83ce46cbc947616d0cbaa82323818a793d\0"; /// let qy = b"eec4084f5b29ebf29c44cce3b3059610922f8b30ea6e8811742ac7238fe87308\0"; /// let d = b"8c14b793cb19137e323a6d2e2a870bca2e7a493ec1153b3a95feb8a4873f8d08\0"; /// ECC::import_raw_ex(qx, qy, d, ECC::SECP256R1, None, None).expect("Error with import_raw_ex()"); + /// } /// ``` + #[cfg(ecc_import)] pub fn import_raw_ex(qx: &[u8], qy: &[u8], d: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { @@ -875,6 +913,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -889,7 +929,9 @@ impl ECC { /// let mut d_len = 0u32; /// ecc.export_ex(&mut qx, &mut qx_len, &mut qy, &mut qy_len, &mut d, &mut d_len, false).expect("Error with export_ex()"); /// let mut ecc2 = ECC::import_unsigned(&qx, &qy, &d, curve_id, None, None).expect("Error with import_unsigned()"); + /// } /// ``` + #[cfg(ecc_import)] pub fn import_unsigned(qx: &[u8], qy: &[u8], d: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { @@ -935,6 +977,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -943,7 +987,9 @@ impl ECC { /// let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()"); /// let x963 = &x963[0..x963_size]; /// let _ecc2 = ECC::import_x963(x963, None, None).expect("Error with import_x963()"); + /// } /// ``` + #[cfg(ecc_import)] pub fn import_x963(din: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let din_size = din.len() as u32; let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); @@ -993,6 +1039,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -1003,7 +1051,9 @@ impl ECC { /// let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()"); /// let x963 = &x963[0..x963_size]; /// let _ecc2 = ECC::import_x963_ex(x963, curve_id, None, None).expect("Error with import_x963_ex()"); + /// } /// ``` + #[cfg(ecc_import)] pub fn import_x963_ex(din: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let din_size = din.len() as u32; let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); @@ -1241,6 +1291,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -1252,7 +1304,9 @@ impl ECC { /// let mut d = [0u8; 32]; /// let mut d_len = 0u32; /// ecc.export(&mut qx, &mut qx_len, &mut qy, &mut qy_len, &mut d, &mut d_len).expect("Error with export()"); + /// } /// ``` + #[cfg(ecc_import)] pub fn export(&mut self, qx: &mut [u8], qx_len: &mut u32, qy: &mut [u8], qy_len: &mut u32, d: &mut [u8], d_len: &mut u32) -> Result<(), i32> { *qx_len = qx.len() as u32; @@ -1292,6 +1346,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -1303,7 +1359,9 @@ impl ECC { /// let mut d = [0u8; 32]; /// let mut d_len = 0u32; /// ecc.export_ex(&mut qx, &mut qx_len, &mut qy, &mut qy_len, &mut d, &mut d_len, false).expect("Error with export_ex()"); + /// } /// ``` + #[cfg(ecc_import)] pub fn export_ex(&mut self, qx: &mut [u8], qx_len: &mut u32, qy: &mut [u8], qy_len: &mut u32, d: &mut [u8], d_len: &mut u32, hex: bool) -> Result<(), i32> { @@ -1343,6 +1401,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -1350,7 +1410,9 @@ impl ECC { /// let mut d = [0u8; 32]; /// let d_size = ecc.export_private(&mut d).expect("Error with export_private()"); /// assert_eq!(d_size, 32); + /// } /// ``` + #[cfg(ecc_export)] pub fn export_private(&mut self, d: &mut [u8]) -> Result { let mut d_size = d.len() as u32; let rc = unsafe { @@ -1380,6 +1442,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -1389,7 +1453,9 @@ impl ECC { /// let mut qy = [0u8; 32]; /// let mut qy_len = 0u32; /// ecc.export_public(&mut qx, &mut qx_len, &mut qy, &mut qy_len).expect("Error with export_public()"); + /// } /// ``` + #[cfg(ecc_export)] pub fn export_public(&mut self, qx: &mut [u8], qx_len: &mut u32, qy: &mut [u8], qy_len: &mut u32) -> Result<(), i32> { *qx_len = qx.len() as u32; @@ -1419,13 +1485,17 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); /// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()"); /// let mut x963 = [0u8; 128]; /// let _x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()"); + /// } /// ``` + #[cfg(ecc_export)] pub fn export_x963(&mut self, dout: &mut [u8]) -> Result { let mut out_len: u32 = dout.len() as u32; let rc = unsafe { @@ -1451,13 +1521,17 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(all(ecc_export, ecc_comp_key))] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); /// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()"); /// let mut x963 = [0u8; 128]; /// let _x963_size = ecc.export_x963_compressed(&mut x963).expect("Error with export_x963_compressed()"); + /// } /// ``` + #[cfg(all(ecc_export, ecc_comp_key))] pub fn export_x963_compressed(&mut self, dout: &mut [u8]) -> Result { let mut out_len: u32 = dout.len() as u32; let rc = unsafe { @@ -1606,6 +1680,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_dh)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -1613,13 +1689,17 @@ impl ECC { /// let mut ecc1 = ECC::generate(32, &mut rng, None, None).expect("Error with generate()"); /// let mut ss0 = [0u8; 128]; /// let mut ss1 = [0u8; 128]; + /// ecc0.set_rng(&mut rng).expect("Error with set_rng()"); + /// ecc1.set_rng(&mut rng).expect("Error with set_rng()"); /// let ss0_size = ecc0.shared_secret(&mut ecc1, &mut ss0).expect("Error with shared_secret()"); /// let ss1_size = ecc1.shared_secret(&mut ecc0, &mut ss1).expect("Error with shared_secret()"); /// assert_eq!(ss0_size, ss1_size); /// let ss0 = &ss0[0..ss0_size]; /// let ss1 = &ss1[0..ss1_size]; /// assert_eq!(*ss0, *ss1); + /// } /// ``` + #[cfg(ecc_dh)] pub fn shared_secret(&mut self, peer_key: &mut ECC, dout: &mut [u8]) -> Result { let mut out_len = dout.len() as u32; let rc = unsafe { @@ -1649,6 +1729,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_dh)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -1657,13 +1739,17 @@ impl ECC { /// let ecc1_point = ecc1.make_pub_to_point(None, None).expect("Error with make_pub_to_point()"); /// let mut ss0 = [0u8; 128]; /// let mut ss1 = [0u8; 128]; + /// ecc0.set_rng(&mut rng).expect("Error with set_rng()"); + /// ecc1.set_rng(&mut rng).expect("Error with set_rng()"); /// let ss0_size = ecc0.shared_secret_ex(&ecc1_point, &mut ss0).expect("Error with shared_secret_ex()"); /// let ss1_size = ecc1.shared_secret(&mut ecc0, &mut ss1).expect("Error with shared_secret()"); /// assert_eq!(ss0_size, ss1_size); /// let ss0 = &ss0[0..ss0_size]; /// let ss1 = &ss1[0..ss1_size]; /// assert_eq!(*ss0, *ss1); + /// } /// ``` + #[cfg(ecc_dh)] pub fn shared_secret_ex(&mut self, peer: &ECCPoint, dout: &mut [u8]) -> Result { let mut out_len = dout.len() as u32; let rc = unsafe { @@ -1692,6 +1778,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_sign)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -1702,7 +1790,9 @@ impl ECC { /// let signature = &mut signature[0..signature_length]; /// let valid = ecc.verify_hash(&signature, &hash).expect("Error with verify_hash()"); /// assert_eq!(valid, true); + /// } /// ``` + #[cfg(ecc_sign)] pub fn sign_hash(&mut self, din: &[u8], dout: &mut [u8], rng: &mut RNG) -> Result { let din_size = din.len() as u32; let mut dout_size = dout.len() as u32; @@ -1731,6 +1821,8 @@ impl ECC { /// # Example /// /// ```rust + /// #[cfg(ecc_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ecc::ECC; /// let mut rng = RNG::new().expect("Failed to create RNG"); @@ -1741,7 +1833,9 @@ impl ECC { /// let signature = &mut signature[0..signature_length]; /// let valid = ecc.verify_hash(&signature, &hash).expect("Error with verify_hash()"); /// assert_eq!(valid, true); + /// } /// ``` + #[cfg(ecc_verify)] pub fn verify_hash(&mut self, sig: &[u8], hash: &[u8]) -> Result { let mut res: i32 = 0; let sig_len = sig.len() as u32; diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs b/wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs index aa73056b1..327a8416e 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs @@ -23,6 +23,8 @@ This module provides a Rust wrapper for the wolfCrypt library's EdDSA Curve 25519 (Ed25519) functionality. */ +#![cfg(ed25519)] + use crate::sys; use crate::wolfcrypt::random::RNG; use std::mem::MaybeUninit; @@ -227,6 +229,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -234,7 +238,9 @@ impl Ed25519 { /// let mut private = [0u8; Ed25519::PRV_KEY_SIZE]; /// let mut public = [0u8; Ed25519::PUB_KEY_SIZE]; /// ed.export_key(&mut private, &mut public).expect("Error with export_key()"); + /// } /// ``` + #[cfg(ed25519_export)] pub fn export_key(&self, private: &mut [u8], public: &mut [u8]) -> Result<(), i32> { let mut private_size = private.len() as u32; let mut public_size = public.len() as u32; @@ -265,13 +271,17 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); /// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()"); /// let mut public = [0u8; Ed25519::PUB_KEY_SIZE]; /// ed.export_public(&mut public).expect("Error with export_public()"); + /// } /// ``` + #[cfg(ed25519_export)] pub fn export_public(&self, public: &mut [u8]) -> Result<(), i32> { let mut public_size = public.len() as u32; let rc = unsafe { @@ -300,13 +310,17 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); /// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()"); /// let mut private = [0u8; Ed25519::PRV_KEY_SIZE]; /// ed.export_private(&mut private).expect("Error with export_private()"); + /// } /// ``` + #[cfg(ed25519_export)] pub fn export_private(&self, keyout: &mut [u8]) -> Result<(), i32> { let mut keyout_size = keyout.len() as u32; let rc = unsafe { @@ -335,13 +349,17 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); /// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()"); /// let mut private_only = [0u8; Ed25519::KEY_SIZE]; /// ed.export_private_only(&mut private_only).expect("Error with export_private_only()"); + /// } /// ``` + #[cfg(ed25519_export)] pub fn export_private_only(&self, private: &mut [u8]) -> Result<(), i32> { let mut private_size = private.len() as u32; let rc = unsafe { @@ -372,6 +390,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -381,7 +401,9 @@ impl Ed25519 { /// ed.export_key(&mut private, &mut public).expect("Error with export_key()"); /// let mut ed = Ed25519::new().expect("Error with new()"); /// ed.import_public(&public).expect("Error with import_public()"); + /// } /// ``` + #[cfg(ed25519_import)] pub fn import_public(&mut self, public: &[u8]) -> Result<(), i32> { let public_size = public.len() as u32; let rc = unsafe { @@ -412,6 +434,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -421,7 +445,9 @@ impl Ed25519 { /// ed.export_key(&mut private, &mut public).expect("Error with export_key()"); /// let mut ed = Ed25519::new().expect("Error with new()"); /// ed.import_public_ex(&public, false).expect("Error with import_public_ex()"); + /// } /// ``` + #[cfg(ed25519_import)] pub fn import_public_ex(&mut self, public: &[u8], trusted: bool) -> Result<(), i32> { let public_size = public.len() as u32; let rc = unsafe { @@ -448,6 +474,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -456,7 +484,9 @@ impl Ed25519 { /// ed.export_private_only(&mut private_only).expect("Error with export_private_only()"); /// let mut ed = Ed25519::new().expect("Error with new()"); /// ed.import_private_only(&private_only).expect("Error with import_private_only()"); + /// } /// ``` + #[cfg(ed25519_import)] pub fn import_private_only(&mut self, private: &[u8]) -> Result<(), i32> { let private_size = private.len() as u32; let rc = unsafe { @@ -488,6 +518,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -497,7 +529,9 @@ impl Ed25519 { /// ed.export_key(&mut private, &mut public).expect("Error with export_key()"); /// let mut ed = Ed25519::new().expect("Error with new()"); /// ed.import_private_key(&private, Some(&public)).expect("Error with import_private_key()"); + /// } /// ``` + #[cfg(ed25519_import)] pub fn import_private_key(&mut self, private: &[u8], public: Option<&[u8]>) -> Result<(), i32> { let private_size = private.len() as u32; let mut public_ptr: *const u8 = core::ptr::null(); @@ -535,6 +569,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -544,7 +580,9 @@ impl Ed25519 { /// ed.export_key(&mut private, &mut public).expect("Error with export_key()"); /// let mut ed = Ed25519::new().expect("Error with new()"); /// ed.import_private_key_ex(&private, Some(&public), false).expect("Error with import_private_key_ex()"); + /// } /// ``` + #[cfg(ed25519_import)] pub fn import_private_key_ex(&mut self, private: &[u8], public: Option<&[u8]>, trusted: bool) -> Result<(), i32> { let private_size = private.len() as u32; let mut public_ptr: *const u8 = core::ptr::null(); @@ -619,6 +657,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_sign)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -626,7 +666,9 @@ impl Ed25519 { /// let message = [0x42u8, 33, 55, 66]; /// let mut signature = [0u8; Ed25519::SIG_SIZE]; /// ed.sign_msg(&message, &mut signature).expect("Error with sign_msg()"); + /// } /// ``` + #[cfg(ed25519_sign)] pub fn sign_msg(&mut self, message: &[u8], signature: &mut [u8]) -> Result { let message_size = message.len() as u32; let mut signature_size = signature.len() as u32; @@ -659,6 +701,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_sign)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -667,7 +711,9 @@ impl Ed25519 { /// let context = b"context"; /// let mut signature = [0u8; Ed25519::SIG_SIZE]; /// ed.sign_msg_ctx(&message, context, &mut signature).expect("Error with sign_msg_ctx()"); + /// } /// ``` + #[cfg(ed25519_sign)] pub fn sign_msg_ctx(&mut self, message: &[u8], context: &[u8], signature: &mut [u8]) -> Result { let message_size = message.len() as u32; let context_size = context.len() as u8; @@ -703,6 +749,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_sign)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -720,7 +768,9 @@ impl Ed25519 { /// let context = b"context"; /// let mut signature = [0u8; Ed25519::SIG_SIZE]; /// ed.sign_hash_ph(&hash, Some(context), &mut signature).expect("Error with sign_hash_ph()"); + /// } /// ``` + #[cfg(ed25519_sign)] pub fn sign_hash_ph(&mut self, hash: &[u8], context: Option<&[u8]>, signature: &mut [u8]) -> Result { let hash_size = hash.len() as u32; let mut context_ptr: *const u8 = core::ptr::null(); @@ -761,6 +811,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_sign)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -769,7 +821,9 @@ impl Ed25519 { /// let context = b"context"; /// let mut signature = [0u8; Ed25519::SIG_SIZE]; /// ed.sign_msg_ph(&message, Some(context), &mut signature).expect("Error with sign_msg_ph()"); + /// } /// ``` + #[cfg(ed25519_sign)] pub fn sign_msg_ph(&mut self, message: &[u8], context: Option<&[u8]>, signature: &mut [u8]) -> Result { let message_size = message.len() as u32; let mut context_ptr: *const u8 = core::ptr::null(); @@ -810,6 +864,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_sign)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -818,7 +874,9 @@ impl Ed25519 { /// let context = b"context"; /// let mut signature = [0u8; Ed25519::SIG_SIZE]; /// ed.sign_msg_ex(&message, Some(context), Ed25519::ED25519, &mut signature).expect("Error with sign_msg_ex()"); + /// } /// ``` + #[cfg(ed25519_sign)] pub fn sign_msg_ex(&mut self, din: &[u8], context: Option<&[u8]>, typ: u8, signature: &mut [u8]) -> Result { let din_size = din.len() as u32; let mut context_ptr: *const u8 = core::ptr::null(); @@ -854,6 +912,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -863,7 +923,9 @@ impl Ed25519 { /// ed.sign_msg(&message, &mut signature).expect("Error with sign_msg()"); /// let signature_valid = ed.verify_msg(&signature, &message).expect("Error with verify_msg()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed25519_verify)] pub fn verify_msg(&mut self, signature: &[u8], message: &[u8]) -> Result { let signature_size = signature.len() as u32; let message_size = message.len() as u32; @@ -896,6 +958,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -906,7 +970,9 @@ impl Ed25519 { /// ed.sign_msg_ctx(message, context, &mut signature).expect("Error with sign_msg()"); /// let signature_valid = ed.verify_msg_ctx(&signature, message, context).expect("Error with verify_msg_ctx()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed25519_verify)] pub fn verify_msg_ctx(&mut self, signature: &[u8], message: &[u8], context: &[u8]) -> Result { let signature_size = signature.len() as u32; let message_size = message.len() as u32; @@ -943,6 +1009,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -962,7 +1030,9 @@ impl Ed25519 { /// ed.sign_hash_ph(&hash, Some(context), &mut signature).expect("Error with sign_hash_ph()"); /// let signature_valid = ed.verify_hash_ph(&signature, &hash, Some(context)).expect("Error with verify_hash_ph()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed25519_verify)] pub fn verify_hash_ph(&mut self, signature: &[u8], hash: &[u8], context: Option<&[u8]>) -> Result { let signature_size = signature.len() as u32; let hash_size = hash.len() as u32; @@ -1003,6 +1073,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -1013,7 +1085,9 @@ impl Ed25519 { /// ed.sign_msg_ph(&message, Some(context), &mut signature).expect("Error with sign_msg_ph()"); /// let signature_valid = ed.verify_msg_ph(&signature, &message, Some(context)).expect("Error with verify_msg_ph()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed25519_verify)] pub fn verify_msg_ph(&mut self, signature: &[u8], message: &[u8], context: Option<&[u8]>) -> Result { let signature_size = signature.len() as u32; let message_size = message.len() as u32; @@ -1054,6 +1128,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -1064,7 +1140,9 @@ impl Ed25519 { /// ed.sign_msg_ex(&message, Some(context), Ed25519::ED25519, &mut signature).expect("Error with sign_msg_ex()"); /// let signature_valid = ed.verify_msg_ex(&signature, &message, Some(context), Ed25519::ED25519).expect("Error with verify_msg_ex()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed25519_verify)] pub fn verify_msg_ex(&mut self, signature: &[u8], din: &[u8], context: Option<&[u8]>, typ: u8) -> Result { let signature_size = signature.len() as u32; let din_size = din.len() as u32; @@ -1102,6 +1180,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_streaming_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -1114,7 +1194,9 @@ impl Ed25519 { /// ed.verify_msg_update(&message[2..4]).expect("Error with verify_msg_update()"); /// let signature_valid = ed.verify_msg_final(&signature).expect("Error with verify_msg_final()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed25519_streaming_verify)] pub fn verify_msg_init(&mut self, signature: &[u8], context: Option<&[u8]>, typ: u8) -> Result<(), i32> { let signature_size = signature.len() as u32; let mut context_ptr: *const u8 = core::ptr::null(); @@ -1147,6 +1229,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_streaming_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -1159,7 +1243,9 @@ impl Ed25519 { /// ed.verify_msg_update(&message[2..4]).expect("Error with verify_msg_update()"); /// let signature_valid = ed.verify_msg_final(&signature).expect("Error with verify_msg_final()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed25519_streaming_verify)] pub fn verify_msg_update(&mut self, din: &[u8]) -> Result<(), i32> { let din_size = din.len() as u32; let rc = unsafe { @@ -1186,6 +1272,8 @@ impl Ed25519 { /// # Example /// /// ```rust + /// #[cfg(ed25519_streaming_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed25519::Ed25519; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -1198,7 +1286,9 @@ impl Ed25519 { /// ed.verify_msg_update(&message[2..4]).expect("Error with verify_msg_update()"); /// let signature_valid = ed.verify_msg_final(&signature).expect("Error with verify_msg_final()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed25519_streaming_verify)] pub fn verify_msg_final(&mut self, signature: &[u8]) -> Result { let signature_size = signature.len() as u32; let mut res = 0i32; diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs b/wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs index 33c576117..104c92808 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs @@ -23,6 +23,8 @@ This module provides a Rust wrapper for the wolfCrypt library's EdDSA Curve 448 (Ed448) functionality. */ +#![cfg(ed448)] + use crate::sys; use crate::wolfcrypt::random::RNG; use std::mem::MaybeUninit; @@ -226,6 +228,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -233,7 +237,9 @@ impl Ed448 { /// let mut private = [0u8; Ed448::PRV_KEY_SIZE]; /// let mut public = [0u8; Ed448::PUB_KEY_SIZE]; /// ed.export_key(&mut private, &mut public).expect("Error with export_key()"); + /// } /// ``` + #[cfg(ed448_export)] pub fn export_key(&self, private: &mut [u8], public: &mut [u8]) -> Result<(), i32> { let mut private_size = private.len() as u32; let mut public_size = public.len() as u32; @@ -263,13 +269,17 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); /// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()"); /// let mut public = [0u8; Ed448::PUB_KEY_SIZE]; /// ed.export_public(&mut public).expect("Error with export_public()"); + /// } /// ``` + #[cfg(ed448_export)] pub fn export_public(&self, public: &mut [u8]) -> Result<(), i32> { let mut public_size = public.len() as u32; let rc = unsafe { @@ -297,13 +307,17 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); /// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()"); /// let mut private = [0u8; Ed448::PRV_KEY_SIZE]; /// ed.export_private(&mut private).expect("Error with export_private()"); + /// } /// ``` + #[cfg(ed448_export)] pub fn export_private(&self, keyout: &mut [u8]) -> Result<(), i32> { let mut keyout_size = keyout.len() as u32; let rc = unsafe { @@ -331,13 +345,17 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_export)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); /// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()"); /// let mut private_only = [0u8; Ed448::KEY_SIZE]; /// ed.export_private_only(&mut private_only).expect("Error with export_private_only()"); + /// } /// ``` + #[cfg(ed448_export)] pub fn export_private_only(&self, private: &mut [u8]) -> Result<(), i32> { let mut private_size = private.len() as u32; let rc = unsafe { @@ -368,6 +386,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -377,7 +397,9 @@ impl Ed448 { /// ed.export_key(&mut private, &mut public).expect("Error with export_key()"); /// let mut ed = Ed448::new().expect("Error with new()"); /// ed.import_public(&public).expect("Error with import_public()"); + /// } /// ``` + #[cfg(ed448_import)] pub fn import_public(&mut self, public: &[u8]) -> Result<(), i32> { let public_size = public.len() as u32; let rc = unsafe { @@ -408,6 +430,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -417,7 +441,9 @@ impl Ed448 { /// ed.export_key(&mut private, &mut public).expect("Error with export_key()"); /// let mut ed = Ed448::new().expect("Error with new()"); /// ed.import_public_ex(&public, false).expect("Error with import_public_ex()"); + /// } /// ``` + #[cfg(ed448_import)] pub fn import_public_ex(&mut self, public: &[u8], trusted: bool) -> Result<(), i32> { let public_size = public.len() as u32; let rc = unsafe { @@ -444,6 +470,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -452,7 +480,9 @@ impl Ed448 { /// ed.export_private_only(&mut private_only).expect("Error with export_private_only()"); /// let mut ed = Ed448::new().expect("Error with new()"); /// ed.import_private_only(&private_only).expect("Error with import_private_only()"); + /// } /// ``` + #[cfg(ed448_import)] pub fn import_private_only(&mut self, private: &[u8]) -> Result<(), i32> { let private_size = private.len() as u32; let rc = unsafe { @@ -484,6 +514,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -493,7 +525,9 @@ impl Ed448 { /// ed.export_key(&mut private, &mut public).expect("Error with export_key()"); /// let mut ed = Ed448::new().expect("Error with new()"); /// ed.import_private_key(&private, Some(&public)).expect("Error with import_private_key()"); + /// } /// ``` + #[cfg(ed448_import)] pub fn import_private_key(&mut self, private: &[u8], public: Option<&[u8]>) -> Result<(), i32> { let private_size = private.len() as u32; let mut public_ptr: *const u8 = core::ptr::null(); @@ -531,6 +565,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_import)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -540,7 +576,9 @@ impl Ed448 { /// ed.export_key(&mut private, &mut public).expect("Error with export_key()"); /// let mut ed = Ed448::new().expect("Error with new()"); /// ed.import_private_key_ex(&private, Some(&public), false).expect("Error with import_private_key_ex()"); + /// } /// ``` + #[cfg(ed448_import)] pub fn import_private_key_ex(&mut self, private: &[u8], public: Option<&[u8]>, trusted: bool) -> Result<(), i32> { let private_size = private.len() as u32; let mut public_ptr: *const u8 = core::ptr::null(); @@ -618,6 +656,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_sign)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -626,7 +666,9 @@ impl Ed448 { /// let context = b"context"; /// let mut signature = [0u8; Ed448::SIG_SIZE]; /// ed.sign_msg(&message, Some(context), &mut signature).expect("Error with sign_msg()"); + /// } /// ``` + #[cfg(ed448_sign)] pub fn sign_msg(&mut self, message: &[u8], context: Option<&[u8]>, signature: &mut [u8]) -> Result { let message_size = message.len() as u32; let mut context_ptr: *const u8 = core::ptr::null(); @@ -667,6 +709,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_sign)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -684,7 +728,9 @@ impl Ed448 { /// let context = b"context"; /// let mut signature = [0u8; Ed448::SIG_SIZE]; /// ed.sign_hash_ph(&hash, Some(context), &mut signature).expect("Error with sign_hash_ph()"); + /// } /// ``` + #[cfg(ed448_sign)] pub fn sign_hash_ph(&mut self, hash: &[u8], context: Option<&[u8]>, signature: &mut [u8]) -> Result { let hash_size = hash.len() as u32; let mut context_ptr: *const u8 = core::ptr::null(); @@ -725,6 +771,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_sign)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -733,7 +781,9 @@ impl Ed448 { /// let context = b"context"; /// let mut signature = [0u8; Ed448::SIG_SIZE]; /// ed.sign_msg_ph(&message, Some(context), &mut signature).expect("Error with sign_msg_ph()"); + /// } /// ``` + #[cfg(ed448_sign)] pub fn sign_msg_ph(&mut self, message: &[u8], context: Option<&[u8]>, signature: &mut [u8]) -> Result { let message_size = message.len() as u32; let mut context_ptr: *const u8 = core::ptr::null(); @@ -774,6 +824,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_sign)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -782,7 +834,9 @@ impl Ed448 { /// let context = b"context"; /// let mut signature = [0u8; Ed448::SIG_SIZE]; /// ed.sign_msg_ex(&message, Some(context), Ed448::ED448, &mut signature).expect("Error with sign_msg_ex()"); + /// } /// ``` + #[cfg(ed448_sign)] pub fn sign_msg_ex(&mut self, din: &[u8], context: Option<&[u8]>, typ: u8, signature: &mut [u8]) -> Result { let din_size = din.len() as u32; let mut context_ptr: *const u8 = core::ptr::null(); @@ -821,6 +875,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -831,7 +887,9 @@ impl Ed448 { /// ed.sign_msg(message, Some(context), &mut signature).expect("Error with sign_msg()"); /// let signature_valid = ed.verify_msg(&signature, message, Some(context)).expect("Error with verify_msg()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed448_verify)] pub fn verify_msg(&mut self, signature: &[u8], message: &[u8], context: Option<&[u8]>) -> Result { let signature_size = signature.len() as u32; let message_size = message.len() as u32; @@ -873,6 +931,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -892,7 +952,9 @@ impl Ed448 { /// ed.sign_hash_ph(&hash, Some(context), &mut signature).expect("Error with sign_hash_ph()"); /// let signature_valid = ed.verify_hash_ph(&signature, &hash, Some(context)).expect("Error with verify_hash_ph()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed448_verify)] pub fn verify_hash_ph(&mut self, signature: &[u8], hash: &[u8], context: Option<&[u8]>) -> Result { let signature_size = signature.len() as u32; let hash_size = hash.len() as u32; @@ -933,6 +995,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -943,7 +1007,9 @@ impl Ed448 { /// ed.sign_msg_ph(&message, Some(context), &mut signature).expect("Error with sign_msg_ph()"); /// let signature_valid = ed.verify_msg_ph(&signature, &message, Some(context)).expect("Error with verify_msg_ph()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed448_verify)] pub fn verify_msg_ph(&mut self, signature: &[u8], message: &[u8], context: Option<&[u8]>) -> Result { let signature_size = signature.len() as u32; let message_size = message.len() as u32; @@ -984,6 +1050,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -994,7 +1062,9 @@ impl Ed448 { /// ed.sign_msg_ex(&message, Some(context), Ed448::ED448, &mut signature).expect("Error with sign_msg_ex()"); /// let signature_valid = ed.verify_msg_ex(&signature, &message, Some(context), Ed448::ED448).expect("Error with verify_msg_ex()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed448_verify)] pub fn verify_msg_ex(&mut self, signature: &[u8], din: &[u8], context: Option<&[u8]>, typ: u8) -> Result { let signature_size = signature.len() as u32; let din_size = din.len() as u32; @@ -1032,6 +1102,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_streaming_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -1045,7 +1117,9 @@ impl Ed448 { /// ed.verify_msg_update(&message[2..4]).expect("Error with verify_msg_update()"); /// let signature_valid = ed.verify_msg_final(&signature).expect("Error with verify_msg_final()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed448_streaming_verify)] pub fn verify_msg_init(&mut self, signature: &[u8], context: Option<&[u8]>, typ: u8) -> Result<(), i32> { let signature_size = signature.len() as u32; let mut context_ptr: *const u8 = core::ptr::null(); @@ -1078,6 +1152,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_streaming_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -1091,7 +1167,9 @@ impl Ed448 { /// ed.verify_msg_update(&message[2..4]).expect("Error with verify_msg_update()"); /// let signature_valid = ed.verify_msg_final(&signature).expect("Error with verify_msg_final()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed448_streaming_verify)] pub fn verify_msg_update(&mut self, din: &[u8]) -> Result<(), i32> { let din_size = din.len() as u32; let rc = unsafe { @@ -1118,6 +1196,8 @@ impl Ed448 { /// # Example /// /// ```rust + /// #[cfg(ed448_streaming_verify)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::ed448::Ed448; /// let mut rng = RNG::new().expect("Error creating RNG"); @@ -1131,7 +1211,9 @@ impl Ed448 { /// ed.verify_msg_update(&message[2..4]).expect("Error with verify_msg_update()"); /// let signature_valid = ed.verify_msg_final(&signature).expect("Error with verify_msg_final()"); /// assert!(signature_valid); + /// } /// ``` + #[cfg(ed448_streaming_verify)] pub fn verify_msg_final(&mut self, signature: &[u8]) -> Result { let signature_size = signature.len() as u32; let mut res = 0i32; diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs b/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs index 19f10a18f..ba434024a 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs @@ -26,13 +26,21 @@ Function (KDF) functionality. use crate::sys; use crate::wolfcrypt::hmac::HMAC; +#[cfg(kdf_srtp)] pub const SRTP_LABEL_ENCRYPTION: u8 = sys::WC_SRTP_LABEL_ENCRYPTION as u8; +#[cfg(kdf_srtp)] pub const SRTP_LABEL_MSG_AUTH: u8 = sys::WC_SRTP_LABEL_MSG_AUTH as u8; +#[cfg(kdf_srtp)] pub const SRTP_LABEL_SALT: u8 = sys::WC_SRTP_LABEL_SALT as u8; +#[cfg(kdf_srtp)] pub const SRTCP_LABEL_ENCRYPTION: u8 = sys::WC_SRTCP_LABEL_ENCRYPTION as u8; +#[cfg(kdf_srtp)] pub const SRTCP_LABEL_MSG_AUTH: u8 = sys::WC_SRTCP_LABEL_MSG_AUTH as u8; +#[cfg(kdf_srtp)] pub const SRTCP_LABEL_SALT: u8 = sys::WC_SRTCP_LABEL_SALT as u8; +#[cfg(kdf_srtp)] pub const SRTP_LABEL_HDR_ENCRYPTION: u8 = sys::WC_SRTP_LABEL_HDR_ENCRYPTION as u8; +#[cfg(kdf_srtp)] pub const SRTP_LABEL_HDR_SALT: u8 = sys::WC_SRTP_LABEL_HDR_SALT as u8; /// Implement Password Based Key Derivation Function 2 (PBKDF2) converting an @@ -55,6 +63,8 @@ pub const SRTP_LABEL_HDR_SALT: u8 = sys::WC_SRTP_LABEL_HDR_SALT as u8; /// # Example /// /// ```rust +/// #[cfg(kdf_pbkdf2)] +/// { /// use wolfssl::wolfcrypt::kdf::pbkdf2; /// use wolfssl::wolfcrypt::hmac::HMAC; /// let password = b"passwordpassword"; @@ -67,7 +77,9 @@ pub const SRTP_LABEL_HDR_SALT: u8 = sys::WC_SRTP_LABEL_HDR_SALT as u8; /// let mut keyout = [0u8; 24]; /// pbkdf2(password, &salt, iterations, HMAC::TYPE_SHA256, &mut keyout).expect("Error with pbkdf2()"); /// assert_eq!(keyout, expected_key); +/// } /// ``` +#[cfg(kdf_pbkdf2)] pub fn pbkdf2(password: &[u8], salt: &[u8], iterations: i32, typ: i32, out: &mut [u8]) -> Result<(), i32> { pbkdf2_ex(password, salt, iterations, typ, None, None, out) } @@ -95,6 +107,8 @@ pub fn pbkdf2(password: &[u8], salt: &[u8], iterations: i32, typ: i32, out: &mut /// # Example /// /// ```rust +/// #[cfg(kdf_pbkdf2)] +/// { /// use wolfssl::wolfcrypt::kdf::pbkdf2_ex; /// use wolfssl::wolfcrypt::hmac::HMAC; /// let password = b"passwordpassword"; @@ -105,9 +119,11 @@ pub fn pbkdf2(password: &[u8], salt: &[u8], iterations: i32, typ: i32, out: &mut /// 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1 /// ]; /// let mut keyout = [0u8; 24]; -/// pbkdf2_ex(password, &salt, iterations, HMAC::TYPE_SHA256, None, None, &mut keyout).expect("Error with pbkdf2()"); +/// pbkdf2_ex(password, &salt, iterations, HMAC::TYPE_SHA256, None, None, &mut keyout).expect("Error with pbkdf2_ex()"); /// assert_eq!(keyout, expected_key); +/// } /// ``` +#[cfg(kdf_pbkdf2)] pub fn pbkdf2_ex(password: &[u8], salt: &[u8], iterations: i32, typ: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option, out: &mut [u8]) -> Result<(), i32> { let password_size = password.len() as i32; let salt_size = salt.len() as i32; @@ -159,6 +175,8 @@ pub fn pbkdf2_ex(password: &[u8], salt: &[u8], iterations: i32, typ: i32, heap: /// # Example /// /// ```rust +/// #[cfg(kdf_pkcs12)] +/// { /// use wolfssl::wolfcrypt::kdf::pkcs12_pbkdf; /// use wolfssl::wolfcrypt::hmac::HMAC; /// let password = [0x00u8, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67, 0x00, 0x00]; @@ -172,7 +190,9 @@ pub fn pbkdf2_ex(password: &[u8], salt: &[u8], iterations: i32, typ: i32, heap: /// let mut keyout = [0u8; 24]; /// pkcs12_pbkdf(&password, &salt, iterations, HMAC::TYPE_SHA256, 1, &mut keyout).expect("Error with pkcs12_pbkdf()"); /// assert_eq!(keyout, expected_key); +/// } /// ``` +#[cfg(kdf_pkcs12)] pub fn pkcs12_pbkdf(password: &[u8], salt: &[u8], iterations: i32, typ: i32, id: i32, out: &mut [u8]) -> Result<(), i32> { pkcs12_pbkdf_ex(password, salt, iterations, typ, id, None, out) } @@ -208,6 +228,8 @@ pub fn pkcs12_pbkdf(password: &[u8], salt: &[u8], iterations: i32, typ: i32, id: /// # Example /// /// ```rust +/// #[cfg(kdf_pkcs12)] +/// { /// use wolfssl::wolfcrypt::kdf::pkcs12_pbkdf_ex; /// use wolfssl::wolfcrypt::hmac::HMAC; /// let password = [0x00u8, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67, 0x00, 0x00]; @@ -221,7 +243,9 @@ pub fn pkcs12_pbkdf(password: &[u8], salt: &[u8], iterations: i32, typ: i32, id: /// let mut keyout = [0u8; 24]; /// pkcs12_pbkdf_ex(&password, &salt, iterations, HMAC::TYPE_SHA256, 1, None, &mut keyout).expect("Error with pkcs12_pbkdf_ex()"); /// assert_eq!(keyout, expected_key); +/// } /// ``` +#[cfg(kdf_pkcs12)] pub fn pkcs12_pbkdf_ex(password: &[u8], salt: &[u8], iterations: i32, typ: i32, id: i32, heap: Option<*mut std::os::raw::c_void>, out: &mut [u8]) -> Result<(), i32> { let password_size = password.len() as i32; let salt_size = salt.len() as i32; @@ -259,12 +283,16 @@ pub fn pkcs12_pbkdf_ex(password: &[u8], salt: &[u8], iterations: i32, typ: i32, /// # Example /// /// ```rust +/// #[cfg(kdf_tls13)] +/// { /// use wolfssl::wolfcrypt::hmac::HMAC; /// use wolfssl::wolfcrypt::kdf::*; /// use wolfssl::wolfcrypt::sha::SHA256; /// let mut secret = [0u8; SHA256::DIGEST_SIZE]; /// tls13_hkdf_extract(HMAC::TYPE_SHA256, None, None, &mut secret).expect("Error with tls13_hkdf_extract()"); +/// } /// ``` +#[cfg(kdf_tls13)] pub fn tls13_hkdf_extract(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8]>, out: &mut [u8]) -> Result<(), i32> { tls13_hkdf_extract_ex(typ, salt, key, out, None, None) } @@ -291,12 +319,16 @@ pub fn tls13_hkdf_extract(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8]>, /// # Example /// /// ```rust +/// #[cfg(kdf_tls13)] +/// { /// use wolfssl::wolfcrypt::hmac::HMAC; /// use wolfssl::wolfcrypt::kdf::*; /// use wolfssl::wolfcrypt::sha::SHA256; /// let mut secret = [0u8; SHA256::DIGEST_SIZE]; /// tls13_hkdf_extract_ex(HMAC::TYPE_SHA256, None, None, &mut secret, None, None).expect("Error with tls13_hkdf_extract_ex()"); +/// } /// ``` +#[cfg(kdf_tls13)] pub fn tls13_hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8]>, out: &mut [u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result<(), i32> { let mut salt_ptr = core::ptr::null(); let mut salt_size = 0u32; @@ -357,6 +389,8 @@ pub fn tls13_hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8 /// # Example /// /// ```rust +/// #[cfg(kdf_tls13)] +/// { /// use wolfssl::wolfcrypt::hmac::HMAC; /// use wolfssl::wolfcrypt::kdf::*; /// use wolfssl::wolfcrypt::sha::SHA256; @@ -379,7 +413,9 @@ pub fn tls13_hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8 /// tls13_hkdf_expand_label(HMAC::TYPE_SHA256, &secret, /// protocol_label, ce_traffic_label, /// &hash_hello1, &mut expand_out).expect("Error with tls13_hkdf_expand_label()"); +/// } /// ``` +#[cfg(kdf_tls13)] pub fn tls13_hkdf_expand_label(typ: i32, key: &[u8], protocol: &[u8], label: &[u8], info: &[u8], out: &mut [u8]) -> Result<(), i32> { tls13_hkdf_expand_label_ex(typ, key, protocol, label, info, out, None, None) } @@ -410,6 +446,8 @@ pub fn tls13_hkdf_expand_label(typ: i32, key: &[u8], protocol: &[u8], label: &[u /// # Example /// /// ```rust +/// #[cfg(kdf_tls13)] +/// { /// use wolfssl::wolfcrypt::hmac::HMAC; /// use wolfssl::wolfcrypt::kdf::*; /// use wolfssl::wolfcrypt::sha::SHA256; @@ -432,7 +470,9 @@ pub fn tls13_hkdf_expand_label(typ: i32, key: &[u8], protocol: &[u8], label: &[u /// tls13_hkdf_expand_label_ex(HMAC::TYPE_SHA256, &secret, /// protocol_label, ce_traffic_label, /// &hash_hello1, &mut expand_out, None, None).expect("Error with tls13_hkdf_expand_label_ex()"); +/// } /// ``` +#[cfg(kdf_tls13)] pub fn tls13_hkdf_expand_label_ex(typ: i32, key: &[u8], protocol: &[u8], label: &[u8], info: &[u8], out: &mut [u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result<(), i32> { let key_size = key.len() as u32; let protocol_size = protocol.len() as u32; @@ -478,6 +518,8 @@ pub fn tls13_hkdf_expand_label_ex(typ: i32, key: &[u8], protocol: &[u8], label: /// # Example /// /// ```rust +/// #[cfg(kdf_ssh)] +/// { /// use wolfssl::wolfcrypt::hmac::HMAC; /// use wolfssl::wolfcrypt::kdf::*; /// let k = [0x42u8; 256]; @@ -485,7 +527,9 @@ pub fn tls13_hkdf_expand_label_ex(typ: i32, key: &[u8], protocol: &[u8], label: /// let sid = [0x44u8; 32]; /// let mut out = [0u8; 16]; /// ssh_kdf(HMAC::TYPE_SHA256, b'A', &k, &h, &sid, &mut out).expect("Error with ssh_kdf()"); +/// } /// ``` +#[cfg(kdf_ssh)] pub fn ssh_kdf(typ: i32, key_id: u8, k: &[u8], h: &[u8], session_id: &[u8], key: &mut [u8]) -> Result<(), i32> { let key_size = key.len() as u32; let k_size = k.len() as u32; @@ -523,6 +567,8 @@ pub fn ssh_kdf(typ: i32, key_id: u8, k: &[u8], h: &[u8], session_id: &[u8], key: /// # Example /// /// ```rust +/// #[cfg(kdf_srtp)] +/// { /// use wolfssl::wolfcrypt::kdf::*; /// let key = [0xc4u8, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72, /// 0x8e, 0x26, 0xad, 0xb5, 0x32, 0x12, 0x98, 0x90]; @@ -533,7 +579,9 @@ pub fn ssh_kdf(typ: i32, key_id: u8, k: &[u8], h: &[u8], session_id: &[u8], key: /// let mut key_a = [0u8; 20]; /// let mut key_s = [0u8; 14]; /// srtp_kdf(&key, &salt, -1, &index, &mut key_e, &mut key_a, &mut key_s).expect("Error with srtp_kdf()"); +/// } /// ``` +#[cfg(kdf_srtp)] pub fn srtp_kdf(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], key1: &mut [u8], key2: &mut [u8], key3: &mut [u8]) -> Result<(), i32> { let key_size = key.len() as u32; @@ -571,6 +619,8 @@ pub fn srtp_kdf(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], /// # Example /// /// ```rust +/// #[cfg(kdf_srtp)] +/// { /// use wolfssl::wolfcrypt::kdf::*; /// let key = [0xc4u8, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72, /// 0x8e, 0x26, 0xad, 0xb5, 0x32, 0x12, 0x98, 0x90]; @@ -579,7 +629,9 @@ pub fn srtp_kdf(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], /// let index = [0x48u8, 0x71, 0x65, 0x64, 0x9c, 0xca]; /// let mut key_a = [0u8; 20]; /// srtp_kdf_label(&key, &salt, -1, &index, SRTP_LABEL_MSG_AUTH, &mut key_a).expect("Error with srtp_kdf_label()"); +/// } /// ``` +#[cfg(kdf_srtp)] pub fn srtp_kdf_label(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], label: u8, keyout: &mut [u8]) -> Result<(), i32> { let key_size = key.len() as u32; @@ -615,6 +667,8 @@ pub fn srtp_kdf_label(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], /// # Example /// /// ```rust +/// #[cfg(kdf_srtp)] +/// { /// use wolfssl::wolfcrypt::kdf::*; /// let key = [0xc4u8, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72, /// 0x8e, 0x26, 0xad, 0xb5, 0x32, 0x12, 0x98, 0x90]; @@ -625,7 +679,9 @@ pub fn srtp_kdf_label(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], /// let mut key_a = [0u8; 20]; /// let mut key_s = [0u8; 14]; /// srtcp_kdf(&key, &salt, -1, &index, &mut key_e, &mut key_a, &mut key_s).expect("Error with srtcp_kdf()"); +/// } /// ``` +#[cfg(kdf_srtp)] pub fn srtcp_kdf(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], key1: &mut [u8], key2: &mut [u8], key3: &mut [u8]) -> Result<(), i32> { let key_size = key.len() as u32; @@ -663,6 +719,8 @@ pub fn srtcp_kdf(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], /// # Example /// /// ```rust +/// #[cfg(kdf_srtp)] +/// { /// use wolfssl::wolfcrypt::kdf::*; /// let key = [0xc4u8, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72, /// 0x8e, 0x26, 0xad, 0xb5, 0x32, 0x12, 0x98, 0x90]; @@ -671,7 +729,9 @@ pub fn srtcp_kdf(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], /// let index = [0x48u8, 0x71, 0x65, 0x64, 0x9c, 0xca]; /// let mut key_a = [0u8; 20]; /// srtcp_kdf_label(&key, &salt, -1, &index, SRTCP_LABEL_MSG_AUTH, &mut key_a).expect("Error with srtcp_kdf_label()"); +/// } /// ``` +#[cfg(kdf_srtp)] pub fn srtcp_kdf_label(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], label: u8, keyout: &mut [u8]) -> Result<(), i32> { let key_size = key.len() as u32; @@ -701,9 +761,13 @@ pub fn srtcp_kdf_label(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], /// # Example /// /// ```rust +/// #[cfg(kdf_srtp)] +/// { /// use wolfssl::wolfcrypt::kdf::*; /// let kdr_index = srtp_kdr_to_index(16); +/// } /// ``` +#[cfg(kdf_srtp)] pub fn srtp_kdr_to_index(kdr: u32) -> i32 { unsafe { sys::wc_SRTP_KDF_kdr_to_idx(kdr) } } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs b/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs index 4a1830db4..e548a54d3 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs @@ -53,6 +53,8 @@ assert_eq!(plain_out[0..dec_len], *plain); ``` */ +#![cfg(rsa)] + use crate::sys; use crate::wolfcrypt::random::RNG; use std::mem::{MaybeUninit}; @@ -73,21 +75,32 @@ impl RSA { pub const HASH_TYPE_MD2 : u32 = sys::wc_HashType_WC_HASH_TYPE_MD2; pub const HASH_TYPE_MD4 : u32 = sys::wc_HashType_WC_HASH_TYPE_MD4; pub const HASH_TYPE_MD5 : u32 = sys::wc_HashType_WC_HASH_TYPE_MD5; + #[cfg(sha)] pub const HASH_TYPE_SHA : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA; + #[cfg(sha256)] pub const HASH_TYPE_SHA224 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA224; + #[cfg(sha256)] pub const HASH_TYPE_SHA256 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA256; + #[cfg(sha512)] pub const HASH_TYPE_SHA384 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA384; + #[cfg(sha512)] pub const HASH_TYPE_SHA512 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA512; pub const HASH_TYPE_MD5_SHA : u32 = sys::wc_HashType_WC_HASH_TYPE_MD5_SHA; + #[cfg(sha3)] pub const HASH_TYPE_SHA3_224 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_224; + #[cfg(sha3)] pub const HASH_TYPE_SHA3_256 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_256; + #[cfg(sha3)] pub const HASH_TYPE_SHA3_384 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_384; + #[cfg(sha3)] pub const HASH_TYPE_SHA3_512 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_512; pub const HASH_TYPE_BLAKE2B : u32 = sys::wc_HashType_WC_HASH_TYPE_BLAKE2B; pub const HASH_TYPE_BLAKE2S : u32 = sys::wc_HashType_WC_HASH_TYPE_BLAKE2S; pub const HASH_TYPE_SHA512_224 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA512_224; pub const HASH_TYPE_SHA512_256 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA512_256; + #[cfg(shake128)] pub const HASH_TYPE_SHAKE128 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHAKE128; + #[cfg(shake256)] pub const HASH_TYPE_SHAKE256 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHAKE256; // Mask generation function (MGF) constants used for PSS sign and verify methods. @@ -353,6 +366,8 @@ impl RSA { /// # Example /// /// ```rust + /// #[cfg(rsa_keygen)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::rsa::RSA; /// @@ -361,7 +376,9 @@ impl RSA { /// rsa.check().expect("Error with check()"); /// let encrypt_size = rsa.get_encrypt_size().expect("Error with get_encrypt_size()"); /// assert_eq!(encrypt_size, 256); + /// } /// ``` + #[cfg(rsa_keygen)] pub fn generate(size: i32, e: i64, rng: &mut RNG) -> Result { Self::generate_ex(size, e, rng, None, None) } @@ -396,6 +413,8 @@ impl RSA { /// # Example /// /// ```rust + /// #[cfg(rsa_keygen)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::rsa::RSA; /// @@ -404,7 +423,9 @@ impl RSA { /// rsa.check().expect("Error with check()"); /// let encrypt_size = rsa.get_encrypt_size().expect("Error with get_encrypt_size()"); /// assert_eq!(encrypt_size, 256); + /// } /// ``` + #[cfg(rsa_keygen)] pub fn generate_ex(size: i32, e: i64, rng: &mut RNG, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let mut wc_rsakey: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { @@ -454,6 +475,8 @@ impl RSA { /// # Example /// /// ```rust + /// #[cfg(rsa_keygen)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::rsa::RSA; /// @@ -471,6 +494,7 @@ impl RSA { /// let mut q_size: u32 = 0; /// rsa.export_key(&mut e, &mut e_size, &mut n, &mut n_size, /// &mut d, &mut d_size, &mut p, &mut p_size, &mut q, &mut q_size).expect("Error with export_key()"); + /// } /// ``` pub fn export_key(&mut self, e: &mut [u8], e_size: &mut u32, @@ -515,6 +539,8 @@ impl RSA { /// # Example /// /// ```rust + /// #[cfg(rsa_keygen)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::rsa::RSA; /// @@ -525,6 +551,7 @@ impl RSA { /// let mut n: [u8; 256] = [0; 256]; /// let mut n_size: u32 = 0; /// rsa.export_public_key(&mut e, &mut e_size, &mut n, &mut n_size).expect("Error with export_public_key()"); + /// } /// ``` pub fn export_public_key(&mut self, e: &mut [u8], e_size: &mut u32, @@ -553,6 +580,8 @@ impl RSA { /// # Example /// /// ```rust + /// #[cfg(rsa_keygen)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::rsa::RSA; /// @@ -560,6 +589,7 @@ impl RSA { /// let mut rsa = RSA::generate(2048, 65537, &mut rng).expect("Error with generate()"); /// let encrypt_size = rsa.get_encrypt_size().expect("Error with get_encrypt_size()"); /// assert_eq!(encrypt_size, 256); + /// } /// ``` pub fn get_encrypt_size(&self) -> Result { let rc = unsafe { sys::wc_RsaEncryptSize(&self.wc_rsakey) }; @@ -579,12 +609,15 @@ impl RSA { /// # Example /// /// ```rust + /// #[cfg(rsa_keygen)] + /// { /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::rsa::RSA; /// /// let mut rng = RNG::new().expect("Error creating RNG"); /// let mut rsa = RSA::generate(2048, 65537, &mut rng).expect("Error with generate()"); /// rsa.check().expect("Error with check()"); + /// } /// ``` pub fn check(&mut self) -> Result<(), i32> { let rc = unsafe { sys::wc_CheckRsaKey(&mut self.wc_rsakey) }; @@ -989,6 +1022,8 @@ impl RSA { /// # Example /// /// ```rust + /// #[cfg(rsa_direct)] + /// { /// use std::fs; /// use wolfssl::wolfcrypt::random::RNG; /// use wolfssl::wolfcrypt::rsa::RSA; @@ -1008,7 +1043,9 @@ impl RSA { /// let dec_len = rsa.rsa_direct(&enc, &mut plain_out, RSA::PUBLIC_DECRYPT, &mut rng).expect("Error with rsa_direct()"); /// assert_eq!(dec_len, 256); /// assert_eq!(plain_out, plain); + /// } /// ``` + #[cfg(rsa_direct)] pub fn rsa_direct(&mut self, din: &[u8], dout: &mut [u8], typ: i32, rng: &mut RNG) -> Result { let din_ptr = din.as_ptr() as *const u8; let din_size = din.len() as u32; diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs b/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs index 174ad11c5..63696d4c4 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs @@ -27,10 +27,12 @@ use crate::sys; use std::mem::MaybeUninit; /// Context for SHA-1 computation. +#[cfg(sha)] pub struct SHA { wc_sha: sys::wc_Sha, } +#[cfg(sha)] impl SHA { /// SHA-1 digest size in bytes. pub const DIGEST_SIZE: usize = sys::WC_SHA_DIGEST_SIZE as usize; @@ -213,6 +215,7 @@ impl SHA { } } +#[cfg(sha)] impl Drop for SHA { /// Safely free the underlying wolfSSL SHA context. /// @@ -227,10 +230,12 @@ impl Drop for SHA { } /// Context for SHA-224 (SHA-2) computation. +#[cfg(sha256)] pub struct SHA224 { wc_sha224: sys::wc_Sha224, } +#[cfg(sha256)] impl SHA224 { /// SHA-224 digest size in bytes. pub const DIGEST_SIZE: usize = sys::WC_SHA224_DIGEST_SIZE as usize; @@ -413,6 +418,7 @@ impl SHA224 { } } +#[cfg(sha256)] impl Drop for SHA224 { /// Safely free the underlying wolfSSL SHA224 context. /// @@ -427,10 +433,12 @@ impl Drop for SHA224 { } /// Context for SHA-256 (SHA-2) computation. +#[cfg(sha256)] pub struct SHA256 { wc_sha256: sys::wc_Sha256, } +#[cfg(sha256)] impl SHA256 { /// SHA-256 digest size in bytes. pub const DIGEST_SIZE: usize = sys::WC_SHA256_DIGEST_SIZE as usize; @@ -613,6 +621,7 @@ impl SHA256 { } } +#[cfg(sha256)] impl Drop for SHA256 { /// Safely free the underlying wolfSSL SHA256 context. /// @@ -627,10 +636,12 @@ impl Drop for SHA256 { } /// Context for SHA-384 (SHA-2) computation. +#[cfg(sha512)] pub struct SHA384 { wc_sha384: sys::wc_Sha384, } +#[cfg(sha512)] impl SHA384 { /// SHA-384 digest size in bytes. pub const DIGEST_SIZE: usize = sys::WC_SHA384_DIGEST_SIZE as usize; @@ -813,6 +824,7 @@ impl SHA384 { } } +#[cfg(sha512)] impl Drop for SHA384 { /// Safely free the underlying wolfSSL SHA384 context. /// @@ -827,10 +839,12 @@ impl Drop for SHA384 { } /// Context for SHA-512 (SHA-2) computation. +#[cfg(sha512)] pub struct SHA512 { wc_sha512: sys::wc_Sha512, } +#[cfg(sha512)] impl SHA512 { /// SHA-512 digest size in bytes. pub const DIGEST_SIZE: usize = sys::WC_SHA512_DIGEST_SIZE as usize; @@ -1013,6 +1027,7 @@ impl SHA512 { } } +#[cfg(sha512)] impl Drop for SHA512 { /// Safely free the underlying wolfSSL SHA512 context. /// @@ -1027,10 +1042,12 @@ impl Drop for SHA512 { } /// Context for SHA3-224 computation. +#[cfg(sha3)] pub struct SHA3_224 { wc_sha3: sys::wc_Sha3, } +#[cfg(sha3)] impl SHA3_224 { /// SHA3-224 digest size in bytes. pub const DIGEST_SIZE: usize = sys::WC_SHA3_224_DIGEST_SIZE as usize; @@ -1213,6 +1230,7 @@ impl SHA3_224 { } } +#[cfg(sha3)] impl Drop for SHA3_224 { /// Safely free the underlying wolfSSL SHA3_224 context. /// @@ -1227,10 +1245,12 @@ impl Drop for SHA3_224 { } /// Context for SHA3-256 computation. +#[cfg(sha3)] pub struct SHA3_256 { wc_sha3: sys::wc_Sha3, } +#[cfg(sha3)] impl SHA3_256 { /// SHA3-256 digest size in bytes. pub const DIGEST_SIZE: usize = sys::WC_SHA3_256_DIGEST_SIZE as usize; @@ -1413,6 +1433,7 @@ impl SHA3_256 { } } +#[cfg(sha3)] impl Drop for SHA3_256 { /// Safely free the underlying wolfSSL SHA3_256 context. /// @@ -1427,10 +1448,12 @@ impl Drop for SHA3_256 { } /// Context for SHA3-384 computation. +#[cfg(sha3)] pub struct SHA3_384 { wc_sha3: sys::wc_Sha3, } +#[cfg(sha3)] impl SHA3_384 { /// SHA3-384 digest size in bytes. pub const DIGEST_SIZE: usize = sys::WC_SHA3_384_DIGEST_SIZE as usize; @@ -1613,6 +1636,7 @@ impl SHA3_384 { } } +#[cfg(sha3)] impl Drop for SHA3_384 { /// Safely free the underlying wolfSSL SHA3_384 context. /// @@ -1627,10 +1651,12 @@ impl Drop for SHA3_384 { } /// Context for SHA3-512 computation. +#[cfg(sha3)] pub struct SHA3_512 { wc_sha3: sys::wc_Sha3, } +#[cfg(sha3)] impl SHA3_512 { /// SHA3-512 digest size in bytes. pub const DIGEST_SIZE: usize = sys::WC_SHA3_512_DIGEST_SIZE as usize; @@ -1813,6 +1839,7 @@ impl SHA3_512 { } } +#[cfg(sha3)] impl Drop for SHA3_512 { /// Safely free the underlying wolfSSL SHA3_512 context. /// @@ -1827,10 +1854,12 @@ impl Drop for SHA3_512 { } /// Context for SHAKE128 (SHA-3) computation. +#[cfg(shake128)] pub struct SHAKE128 { wc_shake: sys::wc_Shake, } +#[cfg(shake128)] impl SHAKE128 { /// Squeeze block size. pub const SQUEEZE_BLOCK_SIZE: usize = sys::WC_SHA3_128_BLOCK_SIZE as usize; @@ -2080,6 +2109,7 @@ impl SHAKE128 { } } +#[cfg(shake128)] impl Drop for SHAKE128 { /// Safely free the underlying wolfSSL SHAKE128 context. /// @@ -2094,10 +2124,12 @@ impl Drop for SHAKE128 { } /// Context for SHAKE256 (SHA-3) computation. +#[cfg(shake256)] pub struct SHAKE256 { wc_shake: sys::wc_Shake, } +#[cfg(shake256)] impl SHAKE256 { /// Squeeze block size. pub const SQUEEZE_BLOCK_SIZE: usize = sys::WC_SHA3_256_BLOCK_SIZE as usize; @@ -2347,6 +2379,7 @@ impl SHAKE256 { } } +#[cfg(shake256)] impl Drop for SHAKE256 { /// Safely free the underlying wolfSSL SHAKE256 context. /// diff --git a/wrapper/rust/wolfssl/tests/test_aes.rs b/wrapper/rust/wolfssl/tests/test_aes.rs index 87c41edb4..0f58c72de 100644 --- a/wrapper/rust/wolfssl/tests/test_aes.rs +++ b/wrapper/rust/wolfssl/tests/test_aes.rs @@ -1,3 +1,5 @@ +#![cfg(aes)] + use wolfssl::wolfcrypt::aes::*; const BIG_MSG: [u8; 384] = [ @@ -52,6 +54,7 @@ const BIG_MSG: [u8; 384] = [ ]; #[test] +#[cfg(aes_cbc)] fn test_cbc_encrypt_decrypt() { let mut cbc = CBC::new().expect("Failed to create CBC"); let key: &[u8; 16] = b"0123456789abcdef"; @@ -75,6 +78,7 @@ fn test_cbc_encrypt_decrypt() { } #[test] +#[cfg(aes_cbc)] fn test_cbc_big_msg() { let mut cbc = CBC::new().expect("Failed to create CBC"); let big_key = b"0123456789abcdeffedcba9876543210"; @@ -93,6 +97,7 @@ fn test_cbc_big_msg() { } #[test] +#[cfg(aes_ccm)] fn test_ccm_encrypt_decrypt() { let key: [u8; 16] = [ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, @@ -175,6 +180,7 @@ fn test_ccm_encrypt_decrypt() { } #[test] +#[cfg(aes_ccm)] fn test_ccm_big_msg() { let mut ccm = CCM::new().expect("Failed to create CCM"); let big_key = b"0123456789abcdeffedcba9876543210"; @@ -193,6 +199,7 @@ fn test_ccm_big_msg() { } #[test] +#[cfg(aes_cfb)] fn test_cfb_encrypt_decrypt() { let mut cfb = CFB::new().expect("Failed to create CFB"); let key: [u8; 16] = [ @@ -232,6 +239,7 @@ fn test_cfb_encrypt_decrypt() { } #[test] +#[cfg(aes_cfb)] fn test_cfb_big_msg() { let mut cfb = CFB::new().expect("Failed to create CFB"); let big_key = b"0123456789abcdeffedcba9876543210"; @@ -249,6 +257,7 @@ fn test_cfb_big_msg() { } #[test] +#[cfg(aes_ctr)] fn test_ctr_encrypt_decrypt() { let iv: [u8; 16] = [ 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7, @@ -290,6 +299,7 @@ fn test_ctr_encrypt_decrypt() { } #[test] +#[cfg(aes_ctr)] fn test_ctr_big_msg() { let mut ctr = CTR::new().expect("Failed to create CTR"); let big_key = b"0123456789abcdeffedcba9876543210"; @@ -307,6 +317,7 @@ fn test_ctr_big_msg() { } #[test] +#[cfg(aes_eax)] fn test_eax_one_shot_encrypt_decrypt() { let key: [u8; 16] = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, @@ -344,6 +355,7 @@ fn test_eax_one_shot_encrypt_decrypt() { } #[test] +#[cfg(aes_ecb)] fn test_ecb_encrypt_decrypt() { let mut ecb = ECB::new().expect("Failed to create ECB"); let key_128: &[u8; 16] = b"0123456789abcdef"; @@ -366,6 +378,7 @@ fn test_ecb_encrypt_decrypt() { } #[test] +#[cfg(aes_gcm)] fn test_gcm_encrypt_decrypt() { let key: [u8; 16] = [ 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62, @@ -408,6 +421,7 @@ fn test_gcm_encrypt_decrypt() { } #[test] +#[cfg(aes_gcm_stream)] fn test_gcmstream_encrypt_decrypt() { let plain: [u8; 60] = [ 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, @@ -478,6 +492,7 @@ fn test_gcmstream_encrypt_decrypt() { } #[test] +#[cfg(aes_ofb)] fn test_ofb_encrypt_decrypt() { let key: [u8; 32] = [ 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71, @@ -517,6 +532,7 @@ fn test_ofb_encrypt_decrypt() { } #[test] +#[cfg(aes_xts)] fn test_xts_one_shot() { let key: [u8; 32] = [ 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35, @@ -568,6 +584,7 @@ fn test_xts_one_shot() { } #[test] +#[cfg(aes_xts)] fn test_xts_sector_128() { let keys: [u8; 32] = [ 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed, @@ -598,6 +615,7 @@ fn test_xts_sector_128() { } #[test] +#[cfg(aes_xts)] fn test_xts_sector_256() { let keys: [u8; 64] = [ 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32, @@ -636,6 +654,7 @@ fn test_xts_sector_256() { } #[test] +#[cfg(aes_xts)] fn test_xts_consecutive_sectors() { let keys: [u8; 32] = [ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, @@ -758,6 +777,7 @@ fn test_xts_consecutive_sectors() { } #[test] +#[cfg(aes_xts_stream)] fn test_xtsstream() { let keys: [u8; 32] = [ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, @@ -799,6 +819,7 @@ fn test_xtsstream() { } #[test] +#[cfg(aes_xts_stream)] fn test_xtsstream_big_msg() { let key: [u8; 32] = [ 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35, diff --git a/wrapper/rust/wolfssl/tests/test_cmac.rs b/wrapper/rust/wolfssl/tests/test_cmac.rs index 13ed95137..9858c2c96 100644 --- a/wrapper/rust/wolfssl/tests/test_cmac.rs +++ b/wrapper/rust/wolfssl/tests/test_cmac.rs @@ -1,6 +1,9 @@ +#![cfg(cmac)] + use wolfssl::wolfcrypt::cmac::CMAC; #[test] +#[cfg(aes)] fn test_cmac() { let key = [ 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, diff --git a/wrapper/rust/wolfssl/tests/test_dh.rs b/wrapper/rust/wolfssl/tests/test_dh.rs index 7dc7cb745..88abf0291 100644 --- a/wrapper/rust/wolfssl/tests/test_dh.rs +++ b/wrapper/rust/wolfssl/tests/test_dh.rs @@ -1,8 +1,10 @@ -//use std::fs; +#![cfg(dh)] + use wolfssl::wolfcrypt::dh::DH; use wolfssl::wolfcrypt::random::RNG; #[test] +#[cfg(dh_keygen)] fn test_dh_named_parameters() { assert_eq!(DH::get_min_key_size_for_named_parameters(DH::FFDHE_2048), 29); @@ -25,6 +27,7 @@ fn test_dh_named_parameters() { } #[test] +#[cfg(dh_keygen)] fn test_generate_params() { let mut rng = RNG::new().expect("Error with RNG::new()"); let mut dh = DH::generate(&mut rng, 2048).expect("Error with generate()"); @@ -37,6 +40,7 @@ fn test_generate_params() { } #[test] +#[cfg(dh_keygen)] fn test_generate_key_pair() { let mut rng = RNG::new().expect("Error with RNG::new()"); let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()"); diff --git a/wrapper/rust/wolfssl/tests/test_ecc.rs b/wrapper/rust/wolfssl/tests/test_ecc.rs index 663c1e6cc..0dbd1300b 100644 --- a/wrapper/rust/wolfssl/tests/test_ecc.rs +++ b/wrapper/rust/wolfssl/tests/test_ecc.rs @@ -1,3 +1,5 @@ +#![cfg(ecc)] + use std::fs; use wolfssl::wolfcrypt::ecc::*; use wolfssl::wolfcrypt::random::RNG; @@ -17,6 +19,17 @@ fn test_ecc_generate_ex() { assert_eq!(curve_size, 32); let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate_ex()"); ecc.check().expect("Error with check()"); +} + +#[test] +#[cfg(all(ecc_import, ecc_export))] +fn test_ecc_import_x963() { + let mut rng = RNG::new().expect("Failed to create RNG"); + let curve_id = ECC::SECP256R1; + let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()"); + assert_eq!(curve_size, 32); + let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate_ex()"); + ecc.check().expect("Error with check()"); let mut x963 = [0u8; 128]; let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()"); @@ -45,6 +58,7 @@ fn bytes_to_asciiz_hex_string(bytes: &[u8]) -> String { } #[test] +#[cfg(all(ecc_import, ecc_export, ecc_sign, ecc_verify))] fn test_ecc_import_export_sign_verify() { let mut rng = RNG::new().expect("Failed to create RNG"); let key_path = "../../../certs/ecc-client-key.der"; @@ -69,12 +83,15 @@ fn test_ecc_import_export_sign_verify() { let valid = ecc.verify_hash(&signature, &hash).expect("Error with verify_hash()"); assert_eq!(valid, true); - let mut x963 = [0u8; 128]; - let x963_size = ecc.export_x963_compressed(&mut x963).expect("Error with export_x963_compressed()"); - let x963 = &x963[0..x963_size]; - let mut ecc = ECC::import_x963(x963, None, None).expect("Error with import_x963"); - let valid = ecc.verify_hash(&signature, &hash).expect("Error with verify_hash()"); - assert_eq!(valid, true); + #[cfg(ecc_comp_key)] + { + let mut x963 = [0u8; 128]; + let x963_size = ecc.export_x963_compressed(&mut x963).expect("Error with export_x963_compressed()"); + let x963 = &x963[0..x963_size]; + let mut ecc = ECC::import_x963(x963, None, None).expect("Error with import_x963"); + let valid = ecc.verify_hash(&signature, &hash).expect("Error with verify_hash()"); + assert_eq!(valid, true); + } let mut r = [0u8; 32]; let mut r_size = 0u32; @@ -104,12 +121,15 @@ fn test_ecc_import_export_sign_verify() { } #[test] +#[cfg(ecc_dh)] fn test_ecc_shared_secret() { let mut rng = RNG::new().expect("Failed to create RNG"); let mut ecc0 = ECC::generate(32, &mut rng, None, None).expect("Error with generate()"); let mut ecc1 = ECC::generate(32, &mut rng, None, None).expect("Error with generate()"); let mut ss0 = [0u8; 128]; let mut ss1 = [0u8; 128]; + ecc0.set_rng(&mut rng).expect("Error with set_rng()"); + ecc1.set_rng(&mut rng).expect("Error with set_rng()"); let ss0_size = ecc0.shared_secret(&mut ecc1, &mut ss0).expect("Error with shared_secret()"); let ss1_size = ecc1.shared_secret(&mut ecc0, &mut ss1).expect("Error with shared_secret()"); assert_eq!(ss0_size, ss1_size); @@ -125,6 +145,7 @@ fn test_ecc_shared_secret() { } #[test] +#[cfg(ecc_export)] fn test_ecc_export() { let mut rng = RNG::new().expect("Failed to create RNG"); let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()"); @@ -138,6 +159,7 @@ fn test_ecc_export() { } #[test] +#[cfg(ecc_export)] fn test_ecc_export_ex() { let mut rng = RNG::new().expect("Failed to create RNG"); let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()"); @@ -151,6 +173,7 @@ fn test_ecc_export_ex() { } #[test] +#[cfg(all(ecc_import, ecc_export, ecc_sign, ecc_verify))] fn test_ecc_import_export_private() { let mut rng = RNG::new().expect("Failed to create RNG"); let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()"); @@ -172,6 +195,7 @@ fn test_ecc_import_export_private() { } #[test] +#[cfg(all(ecc_import, ecc_export, ecc_sign, ecc_verify))] fn test_ecc_import_export_private_ex() { let mut rng = RNG::new().expect("Failed to create RNG"); let curve_id = ECC::SECP256R1; @@ -195,6 +219,7 @@ fn test_ecc_import_export_private_ex() { } #[test] +#[cfg(ecc_export)] fn test_ecc_export_public() { let mut rng = RNG::new().expect("Failed to create RNG"); let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()"); @@ -206,6 +231,7 @@ fn test_ecc_export_public() { } #[test] +#[cfg(all(ecc_import, ecc_export, ecc_sign, ecc_verify))] fn test_ecc_import_unsigned() { let mut rng = RNG::new().expect("Failed to create RNG"); let curve_id = ECC::SECP256R1; @@ -242,7 +268,22 @@ fn test_ecc_make_pub() { } #[test] +#[cfg(ecc_export)] fn test_ecc_point() { + let mut rng = RNG::new().expect("Failed to create RNG"); + let curve_id = ECC::SECP256R1; + let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()"); + let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate()"); + let mut ecc_point = ecc.make_pub_to_point(Some(&mut rng), None).expect("Error with make_pub_to_point()"); + let mut der = [0u8; 128]; + let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()"); + assert!(size > 0 && size <= der.len()); + ecc_point.forcezero(); +} + +#[test] +#[cfg(all(ecc_import, ecc_export))] +fn test_ecc_point_import() { let mut rng = RNG::new().expect("Failed to create RNG"); let curve_id = ECC::SECP256R1; let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()"); @@ -252,12 +293,25 @@ fn test_ecc_point() { let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()"); assert!(size > 0 && size <= der.len()); ECCPoint::import_der(&der[0..size], curve_id, None).expect("Error with import_der()"); + ecc_point.forcezero(); +} + +#[test] +#[cfg(all(ecc_import, ecc_export, ecc_comp_key))] +fn test_ecc_point_import_compressed() { + let mut rng = RNG::new().expect("Failed to create RNG"); + let curve_id = ECC::SECP256R1; + let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()"); + let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate()"); + let mut ecc_point = ecc.make_pub_to_point(Some(&mut rng), None).expect("Error with make_pub_to_point()"); + let mut der = [0u8; 128]; let size = ecc_point.export_der_compressed(&mut der, curve_id).expect("Error with export_der_compressed()"); ECCPoint::import_der_ex(&der[0..size], curve_id, 1, None).expect("Error with import_der_ex()"); ecc_point.forcezero(); } #[test] +#[cfg(ecc_import)] fn test_ecc_import() { let qx = b"7a4e287890a1a47ad3457e52f2f76a83ce46cbc947616d0cbaa82323818a793d\0"; let qy = b"eec4084f5b29ebf29c44cce3b3059610922f8b30ea6e8811742ac7238fe87308\0"; diff --git a/wrapper/rust/wolfssl/tests/test_ed25519.rs b/wrapper/rust/wolfssl/tests/test_ed25519.rs index fd4beaf00..880b0fc6b 100644 --- a/wrapper/rust/wolfssl/tests/test_ed25519.rs +++ b/wrapper/rust/wolfssl/tests/test_ed25519.rs @@ -1,7 +1,10 @@ +#![cfg(ed25519)] + use wolfssl::wolfcrypt::random::RNG; use wolfssl::wolfcrypt::ed25519::*; #[test] +#[cfg(all(ed25519_import, ed25519_export))] fn test_make_public() { let mut rng = RNG::new().expect("Error creating RNG"); let ed = Ed25519::generate(&mut rng).expect("Error with generate()"); @@ -21,6 +24,7 @@ fn test_check_key() { } #[test] +#[cfg(all(ed25519_import, ed25519_sign, ed25519_verify))] fn test_sign_verify() { let private_key = [ 0xc5u8,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b, @@ -62,6 +66,41 @@ fn test_sign_verify() { let signature_valid = ed.verify_msg_ex(&signature, &message, None, Ed25519::ED25519).expect("Error with verify_msg_ex()"); assert!(signature_valid); +} + +#[test] +#[cfg(all(ed25519_import, ed25519_sign, ed25519_streaming_verify))] +fn test_sign_streaming_verify() { + let private_key = [ + 0xc5u8,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b, + 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1, + 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b, + 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7 + ]; + let public_key = [ + 0xfcu8,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3, + 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58, + 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac, + 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25 + ]; + let message = [0xAFu8, 0x82]; + let expected_signature = [ + 0x62u8,0x91,0xd6,0x57,0xde,0xec,0x24,0x02, + 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3, + 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44, + 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac, + 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90, + 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59, + 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d, + 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a + ]; + + let mut ed = Ed25519::new().expect("Error with new()"); + ed.import_private_key(&private_key, Some(&public_key)).expect("Error with import_private_key()"); + + let mut signature = [0u8; Ed25519::SIG_SIZE]; + ed.sign_msg(&message, &mut signature).expect("Error with sign_msg()"); + assert_eq!(signature, expected_signature); ed.verify_msg_init(&signature, None, Ed25519::ED25519).expect("Error with verify_msg_init()"); ed.verify_msg_update(&message[0..1]).expect("Error with verify_msg_update()"); @@ -71,6 +110,7 @@ fn test_sign_verify() { } #[test] +#[cfg(all(ed25519_import, ed25519_sign, ed25519_verify))] fn test_ctx_sign_verify() { let private_key = [ 0x03u8,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f, @@ -112,6 +152,7 @@ fn test_ctx_sign_verify() { } #[test] +#[cfg(all(ed25519_import, ed25519_sign, ed25519_verify))] fn test_ph_sign_verify() { let private_key = [ 0x83u8,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d, @@ -167,6 +208,7 @@ fn test_ph_sign_verify() { } #[test] +#[cfg(all(ed25519_import, ed25519_export))] fn test_import_export() { let mut rng = RNG::new().expect("Error creating RNG"); let ed = Ed25519::generate(&mut rng).expect("Error with generate()"); diff --git a/wrapper/rust/wolfssl/tests/test_ed448.rs b/wrapper/rust/wolfssl/tests/test_ed448.rs index 08e5506a8..ca26d053e 100644 --- a/wrapper/rust/wolfssl/tests/test_ed448.rs +++ b/wrapper/rust/wolfssl/tests/test_ed448.rs @@ -1,7 +1,10 @@ +#![cfg(ed448)] + use wolfssl::wolfcrypt::random::RNG; use wolfssl::wolfcrypt::ed448::*; #[test] +#[cfg(all(ed448_import, ed448_export))] fn test_make_public() { let mut rng = RNG::new().expect("Error creating RNG"); let ed = Ed448::generate(&mut rng).expect("Error with generate()"); @@ -21,6 +24,7 @@ fn test_check_key() { } #[test] +#[cfg(all(ed448_import, ed448_sign, ed448_verify))] fn test_sign_verify() { let private_key = [ 0xc4u8, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6, @@ -78,6 +82,57 @@ fn test_sign_verify() { let signature_valid = ed.verify_msg_ex(&signature, &message, Some(&context), Ed448::ED448).expect("Error with verify_msg_ex()"); assert!(signature_valid); +} + +#[test] +#[cfg(all(ed448_import, ed448_sign, ed448_streaming_verify))] +fn test_sign_streaming_verify() { + let private_key = [ + 0xc4u8, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6, + 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d, + 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d, + 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a, + 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c, + 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e, + 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27, + 0x4e + ]; + let public_key = [ + 0x43u8, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45, + 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a, + 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37, + 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86, + 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02, + 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c, + 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94, + 0x80 + ]; + let message = [0x03u8]; + let context = [0x66u8,0x6f,0x6f]; + let expected_signature = [ + 0xd4u8, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46, + 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55, + 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab, + 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2, + 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2, + 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21, + 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea, + 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda, + 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d, + 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24, + 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f, + 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d, + 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98, + 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c, + 0x3c, 0x00 + ]; + + let mut ed = Ed448::new().expect("Error with new()"); + ed.import_private_key(&private_key, Some(&public_key)).expect("Error with import_private_key()"); + + let mut signature = [0u8; Ed448::SIG_SIZE]; + ed.sign_msg(&message, Some(&context), &mut signature).expect("Error with sign_msg()"); + assert_eq!(signature, expected_signature); ed.verify_msg_init(&signature, Some(&context), Ed448::ED448).expect("Error with verify_msg_init()"); ed.verify_msg_update(&message).expect("Error with verify_msg_update()"); @@ -86,6 +141,7 @@ fn test_sign_verify() { } #[test] +#[cfg(all(ed448_import, ed448_sign, ed448_verify))] fn test_ph_sign_verify() { let private_key = [ 0x83u8, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d, @@ -156,6 +212,7 @@ fn test_ph_sign_verify() { } #[test] +#[cfg(all(ed448_import, ed448_export))] fn test_import_export() { let mut rng = RNG::new().expect("Error creating RNG"); let ed = Ed448::generate(&mut rng).expect("Error with generate()"); diff --git a/wrapper/rust/wolfssl/tests/test_kdf.rs b/wrapper/rust/wolfssl/tests/test_kdf.rs index b075d7fdb..5b4ffe103 100644 --- a/wrapper/rust/wolfssl/tests/test_kdf.rs +++ b/wrapper/rust/wolfssl/tests/test_kdf.rs @@ -3,6 +3,7 @@ use wolfssl::wolfcrypt::kdf::*; use wolfssl::wolfcrypt::sha::SHA256; #[test] +#[cfg(kdf_pbkdf2)] fn test_pbkdf2() { let password = b"passwordpassword"; let salt = [0x78u8, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06]; @@ -17,11 +18,12 @@ fn test_pbkdf2() { assert_eq!(keyout, expected_key); let mut keyout = [0u8; 24]; - pbkdf2_ex(password, &salt, iterations, HMAC::TYPE_SHA256, None, None, &mut keyout).expect("Error with pbkdf2()"); + pbkdf2_ex(password, &salt, iterations, HMAC::TYPE_SHA256, None, None, &mut keyout).expect("Error with pbkdf2_ex()"); assert_eq!(keyout, expected_key); } #[test] +#[cfg(kdf_pbkdf2)] fn test_pkcs12_pbkdf() { let password = [0x00u8, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67, 0x00, 0x00]; let salt = [0x0au8, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f]; @@ -42,6 +44,7 @@ fn test_pkcs12_pbkdf() { } #[test] +#[cfg(kdf_tls13)] fn test_tls13_hkdf_extract_expand() { let hash_hello1 = [ 0x63u8, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3, @@ -75,6 +78,7 @@ fn test_tls13_hkdf_extract_expand() { } #[test] +#[cfg(kdf_ssh)] fn test_ssh_kdf() { let ssh_kdf_set3_k = [ 0x6Au8, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1, @@ -136,6 +140,7 @@ fn test_ssh_kdf() { } #[test] +#[cfg(kdf_srtp)] fn test_srtp_kdf() { let key = [ 0xc4u8, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72, @@ -183,6 +188,7 @@ fn test_srtp_kdf() { } #[test] +#[cfg(kdf_srtp)] fn test_srtcp_kdf() { let key = [ 0xc4u8, 0x80, 0x9f, 0x6d, 0x36, 0x98, 0x88, 0x72, @@ -230,6 +236,7 @@ fn test_srtcp_kdf() { } #[test] +#[cfg(kdf_srtp)] fn test_srtp_kdr_to_idx() { assert_eq!(srtp_kdr_to_index(0), -1); assert_eq!(srtp_kdr_to_index(1), 0); diff --git a/wrapper/rust/wolfssl/tests/test_rsa.rs b/wrapper/rust/wolfssl/tests/test_rsa.rs index 5f6d15b6b..db760b5cf 100644 --- a/wrapper/rust/wolfssl/tests/test_rsa.rs +++ b/wrapper/rust/wolfssl/tests/test_rsa.rs @@ -1,8 +1,11 @@ +#![cfg(rsa)] + use std::fs; use wolfssl::wolfcrypt::random::RNG; use wolfssl::wolfcrypt::rsa::*; #[test] +#[cfg(rsa_keygen)] fn test_rsa_generate() { let mut rng = RNG::new().expect("Error creating RNG"); let mut rsa = RSA::generate(2048, 65537, &mut rng).expect("Error with generate()"); @@ -68,6 +71,7 @@ fn test_rsa_encrypt_decrypt() { } #[test] +#[cfg(sha256)] fn test_rsa_pss() { let mut rng = RNG::new().expect("Error creating RNG"); @@ -94,6 +98,7 @@ fn test_rsa_pss() { } #[test] +#[cfg(rsa_direct)] fn test_rsa_direct() { let mut rng = RNG::new().expect("Error creating RNG"); diff --git a/wrapper/rust/wolfssl/tests/test_sha.rs b/wrapper/rust/wolfssl/tests/test_sha.rs index f617d02e9..748da48a5 100644 --- a/wrapper/rust/wolfssl/tests/test_sha.rs +++ b/wrapper/rust/wolfssl/tests/test_sha.rs @@ -1,6 +1,7 @@ use wolfssl::wolfcrypt::sha::*; #[test] +#[cfg(sha)] fn test_sha() { let mut sha = SHA::new().expect("Error with new()"); fn test1(sha: &mut SHA, input: &[u8], expected_hash: &[u8]) { @@ -26,6 +27,7 @@ fn test_sha() { } #[test] +#[cfg(sha256)] fn test_sha224() { let mut sha = SHA224::new().expect("Error with new()"); fn test1(sha: &mut SHA224, input: &[u8], expected_hash: &[u8]) { @@ -45,6 +47,7 @@ fn test_sha224() { } #[test] +#[cfg(sha256)] fn test_sha256() { let mut sha = SHA256::new().expect("Error with new()"); fn test1(sha: &mut SHA256, input: &[u8], expected_hash: &[u8]) { @@ -70,6 +73,7 @@ fn test_sha256() { } #[test] +#[cfg(sha512)] fn test_sha384() { let mut sha = SHA384::new().expect("Error with new()"); fn test1(sha: &mut SHA384, input: &[u8], expected_hash: &[u8]) { @@ -92,6 +96,7 @@ fn test_sha384() { } #[test] +#[cfg(sha512)] fn test_sha512() { let mut sha = SHA512::new().expect("Error with new()"); fn test1(sha: &mut SHA512, input: &[u8], expected_hash: &[u8]) { @@ -114,6 +119,7 @@ fn test_sha512() { } #[test] +#[cfg(sha3)] fn test_sha3_224() { let mut sha = SHA3_224::new().expect("Error with new()"); fn test1(sha: &mut SHA3_224, input: &[u8], expected_hash: &[u8]) { @@ -136,6 +142,7 @@ fn test_sha3_224() { } #[test] +#[cfg(sha3)] fn test_sha3_256() { let mut sha = SHA3_256::new().expect("Error with new()"); fn test1(sha: &mut SHA3_256, input: &[u8], expected_hash: &[u8]) { @@ -158,6 +165,7 @@ fn test_sha3_256() { } #[test] +#[cfg(sha3)] fn test_sha3_384() { let mut sha = SHA3_384::new().expect("Error with new()"); fn test1(sha: &mut SHA3_384, input: &[u8], expected_hash: &[u8]) { @@ -184,6 +192,7 @@ fn test_sha3_384() { } #[test] +#[cfg(sha3)] fn test_sha3_512() { let mut sha = SHA3_512::new().expect("Error with new()"); fn test1(sha: &mut SHA3_512, input: &[u8], expected_hash: &[u8]) { @@ -206,6 +215,7 @@ fn test_sha3_512() { } #[test] +#[cfg(shake128)] fn test_shake128() { let mut sha = SHAKE128::new().expect("Error with new()"); fn test1(sha: &mut SHAKE128, input: &[u8], expected_hash: &[u8]) { @@ -236,6 +246,7 @@ fn test_shake128() { } #[test] +#[cfg(shake128)] fn test_shake128_absorb_squeeze() { let mut sha = SHAKE128::new().expect("Error with new()"); fn test1(sha: &mut SHAKE128, input: &[u8], expected_squeeze_out: &[u8]) { @@ -266,6 +277,7 @@ fn test_shake128_absorb_squeeze() { } #[test] +#[cfg(shake256)] fn test_shake256() { let mut sha = SHAKE256::new().expect("Error with new()"); fn test1(sha: &mut SHAKE256, input: &[u8], expected_hash: &[u8]) { @@ -296,6 +308,7 @@ fn test_shake256() { } #[test] +#[cfg(shake256)] fn test_shake256_absorb_squeeze() { let mut sha = SHAKE256::new().expect("Error with new()"); fn test1(sha: &mut SHAKE256, input: &[u8], expected_squeeze_out: &[u8]) {