From dd3b9260f94a2940d971a47c21cbaa146d53652c Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 12 Nov 2025 14:50:57 -0500 Subject: [PATCH 1/2] Rust wrapper: merge wolfssl-sys crate into wolfssl crate --- wrapper/rust/Makefile | 2 - wrapper/rust/README.md | 3 +- wrapper/rust/include.am | 7 +- wrapper/rust/wolfssl-sys/Cargo.lock | 293 ------------------ wrapper/rust/wolfssl-sys/Cargo.toml | 17 - wrapper/rust/wolfssl-sys/Makefile | 7 - wrapper/rust/wolfssl-sys/build.rs | 58 ---- wrapper/rust/wolfssl/Cargo.lock | 7 - wrapper/rust/wolfssl/Cargo.toml | 14 +- wrapper/rust/wolfssl/build.rs | 41 ++- .../rust/{wolfssl-sys => wolfssl}/headers.h | 0 wrapper/rust/wolfssl/src/lib.rs | 1 + .../src/lib.rs => wolfssl/src/sys.rs} | 0 wrapper/rust/wolfssl/src/wolfcrypt/aes.rs | 257 ++++++++------- wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs | 33 +- wrapper/rust/wolfssl/src/wolfcrypt/dh.rs | 86 +++-- wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs | 268 ++++++++-------- wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs | 95 +++--- wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs | 89 +++--- wrapper/rust/wolfssl/src/wolfcrypt/hkdf.rs | 17 +- wrapper/rust/wolfssl/src/wolfcrypt/hmac.rs | 62 ++-- wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs | 51 ++- wrapper/rust/wolfssl/src/wolfcrypt/prf.rs | 31 +- wrapper/rust/wolfssl/src/wolfcrypt/random.rs | 21 +- wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs | 134 ++++---- wrapper/rust/wolfssl/src/wolfcrypt/sha.rs | 256 ++++++++------- 26 files changed, 732 insertions(+), 1118 deletions(-) delete mode 100644 wrapper/rust/wolfssl-sys/Cargo.lock delete mode 100644 wrapper/rust/wolfssl-sys/Cargo.toml delete mode 100644 wrapper/rust/wolfssl-sys/Makefile delete mode 100644 wrapper/rust/wolfssl-sys/build.rs rename wrapper/rust/{wolfssl-sys => wolfssl}/headers.h (100%) rename wrapper/rust/{wolfssl-sys/src/lib.rs => wolfssl/src/sys.rs} (100%) diff --git a/wrapper/rust/Makefile b/wrapper/rust/Makefile index 8ac8e3e1a1..0c07042306 100644 --- a/wrapper/rust/Makefile +++ b/wrapper/rust/Makefile @@ -1,6 +1,5 @@ .PHONY: all all: - +$(MAKE) -C wolfssl-sys +$(MAKE) -C wolfssl .PHONY: test @@ -9,5 +8,4 @@ test: .PHONY: clean clean: - +$(MAKE) -C wolfssl-sys clean +$(MAKE) -C wolfssl clean diff --git a/wrapper/rust/README.md b/wrapper/rust/README.md index e7ec52dc2a..49d7efc3e3 100644 --- a/wrapper/rust/README.md +++ b/wrapper/rust/README.md @@ -17,7 +17,6 @@ Run tests with: | Repository Directory | Description | | --- | --- | | `/wrapper/rust` | Top level container for all Rust wrapper functionality. | -| `/wrapper/rust/wolfssl` | Top level for the `wolfssl` library crate. This crate contains high-level Rust sources that use the bindings from the `wolfssl-sys` crate. | +| `/wrapper/rust/wolfssl` | Top level for the `wolfssl` library crate. | | `/wrapper/rust/wolfssl/src` | Source directory for `wolfssl` crate top-level modules. | | `/wrapper/rust/wolfssl/src/wolfcrypt` | Source directory for submodules of `wolfssl::wolfcrypt` module. | -| `/wrapper/rust/wolfssl-sys` | Top level for the `wolfssl-sys` library crate. This crate contains only automatically generated bindings to the `wolfssl` C library. | diff --git a/wrapper/rust/include.am b/wrapper/rust/include.am index a91452f40f..77c7b5c424 100644 --- a/wrapper/rust/include.am +++ b/wrapper/rust/include.am @@ -4,16 +4,11 @@ EXTRA_DIST += wrapper/rust/Makefile EXTRA_DIST += wrapper/rust/README.md -EXTRA_DIST += wrapper/rust/wolfssl-sys/Cargo.lock -EXTRA_DIST += wrapper/rust/wolfssl-sys/Cargo.toml -EXTRA_DIST += wrapper/rust/wolfssl-sys/Makefile -EXTRA_DIST += wrapper/rust/wolfssl-sys/build.rs -EXTRA_DIST += wrapper/rust/wolfssl-sys/headers.h -EXTRA_DIST += wrapper/rust/wolfssl-sys/src/lib.rs EXTRA_DIST += wrapper/rust/wolfssl/Cargo.lock EXTRA_DIST += wrapper/rust/wolfssl/Cargo.toml EXTRA_DIST += wrapper/rust/wolfssl/Makefile EXTRA_DIST += wrapper/rust/wolfssl/build.rs +EXTRA_DIST += wrapper/rust/wolfssl/headers.h EXTRA_DIST += wrapper/rust/wolfssl/src/lib.rs EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt.rs EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/aes.rs diff --git a/wrapper/rust/wolfssl-sys/Cargo.lock b/wrapper/rust/wolfssl-sys/Cargo.lock deleted file mode 100644 index d43a739f4c..0000000000 --- a/wrapper/rust/wolfssl-sys/Cargo.lock +++ /dev/null @@ -1,293 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "aho-corasick" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" -dependencies = [ - "memchr", -] - -[[package]] -name = "bindgen" -version = "0.72.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "993776b509cfb49c750f11b8f07a46fa23e0a1386ffc01fb1e7d343efc387895" -dependencies = [ - "bitflags", - "cexpr", - "clang-sys", - "itertools", - "log", - "prettyplease", - "proc-macro2", - "quote", - "regex", - "rustc-hash", - "shlex", - "syn", -] - -[[package]] -name = "bitflags" -version = "2.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2261d10cca569e4643e526d8dc2e62e433cc8aba21ab764233731f8d369bf394" - -[[package]] -name = "cexpr" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" -dependencies = [ - "nom", -] - -[[package]] -name = "cfg-if" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9" - -[[package]] -name = "clang-sys" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" -dependencies = [ - "glob", - "libc", - "libloading", -] - -[[package]] -name = "either" -version = "1.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" - -[[package]] -name = "glob" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" - -[[package]] -name = "itertools" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" -dependencies = [ - "either", -] - -[[package]] -name = "libc" -version = "0.2.175" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543" - -[[package]] -name = "libloading" -version = "0.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07033963ba89ebaf1584d767badaa2e8fcec21aedea6b8c0346d487d49c28667" -dependencies = [ - "cfg-if", - "windows-targets", -] - -[[package]] -name = "log" -version = "0.4.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" - -[[package]] -name = "memchr" -version = "2.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" - -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - -[[package]] -name = "nom" -version = "7.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" -dependencies = [ - "memchr", - "minimal-lexical", -] - -[[package]] -name = "prettyplease" -version = "0.2.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" -dependencies = [ - "proc-macro2", - "syn", -] - -[[package]] -name = "proc-macro2" -version = "1.0.101" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "regex" -version = "1.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23d7fd106d8c02486a8d64e778353d1cffe08ce79ac2e82f540c86d0facf6912" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b9458fa0bfeeac22b5ca447c63aaf45f28439a709ccd244698632f9aa6394d6" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "caf4aa5b0f434c91fe5c7f1ecb6a5ece2130b02ad2a590589dda5146df959001" - -[[package]] -name = "rustc-hash" -version = "2.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "syn" -version = "2.0.106" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" - -[[package]] -name = "windows-link" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" - -[[package]] -name = "windows-targets" -version = "0.53.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5fe6031c4041849d7c496a8ded650796e7b6ecc19df1a431c1a363342e5dc91" -dependencies = [ - "windows-link", - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" - -[[package]] -name = "windows_i686_gnu" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" - -[[package]] -name = "windows_i686_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.53.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" - -[[package]] -name = "wolfssl-sys" -version = "0.1.0" -dependencies = [ - "bindgen", -] diff --git a/wrapper/rust/wolfssl-sys/Cargo.toml b/wrapper/rust/wolfssl-sys/Cargo.toml deleted file mode 100644 index c959a2d990..0000000000 --- a/wrapper/rust/wolfssl-sys/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "wolfssl-sys" -version = "0.1.0" -edition = "2024" - -[features] -std = [] - -[build-dependencies] -bindgen = "0.72.1" - -[profile.release] -strip = true -opt-level = "s" -lto = true -codegen-units = 1 -panic = "abort" diff --git a/wrapper/rust/wolfssl-sys/Makefile b/wrapper/rust/wolfssl-sys/Makefile deleted file mode 100644 index 6414a60f6c..0000000000 --- a/wrapper/rust/wolfssl-sys/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -.PHONY: all -all: - cargo build - -.PHONY: clean -clean: - cargo clean diff --git a/wrapper/rust/wolfssl-sys/build.rs b/wrapper/rust/wolfssl-sys/build.rs deleted file mode 100644 index 3f902be0cb..0000000000 --- a/wrapper/rust/wolfssl-sys/build.rs +++ /dev/null @@ -1,58 +0,0 @@ -extern crate bindgen; - -use std::env; -use std::io::{self, Result}; -use std::path::PathBuf; - -/// Perform crate build. -fn main() { - if let Err(e) = run_build() { - eprintln!("Build failed: {}", e); - std::process::exit(1); - } -} - -/// Perform all build steps. -/// -/// Returns `Ok(())` if successful, or an error if any step fails. -fn run_build() -> Result<()> { - // Generate Rust bindings for wolfssl C library. - generate_bindings()?; - Ok(()) -} - -/// Generate Rust bindings for the wolfssl C library using bindgen. -/// -/// This function: -/// 1. Sets up the library and include paths -/// 2. Configures the build environment -/// 3. Generates Rust bindings using bindgen -/// 4. Writes the bindings to a file -/// -/// 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 wolfssl_lib_dir = format!("{}/src/.libs", wolfssl_base_dir); - - println!("cargo:rustc-link-search={}", wolfssl_lib_dir); -// TODO: do we need this if only a static library is built? -// println!("cargo:rustc-link-lib=static=wolfssl"); - - let bindings = bindgen::Builder::default() - .header("headers.h") - .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")) - .map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("Couldn't write bindings: {}", e), - ) - }) -} diff --git a/wrapper/rust/wolfssl/Cargo.lock b/wrapper/rust/wolfssl/Cargo.lock index d9a2ea87cd..e1d06cdef8 100644 --- a/wrapper/rust/wolfssl/Cargo.lock +++ b/wrapper/rust/wolfssl/Cargo.lock @@ -288,13 +288,6 @@ checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" [[package]] name = "wolfssl" version = "0.1.0" -dependencies = [ - "wolfssl-sys", -] - -[[package]] -name = "wolfssl-sys" -version = "0.1.0" dependencies = [ "bindgen", ] diff --git a/wrapper/rust/wolfssl/Cargo.toml b/wrapper/rust/wolfssl/Cargo.toml index 596cfc16d0..6bea736960 100644 --- a/wrapper/rust/wolfssl/Cargo.toml +++ b/wrapper/rust/wolfssl/Cargo.toml @@ -3,5 +3,15 @@ name = "wolfssl" version = "0.1.0" edition = "2024" -[dependencies] -wolfssl-sys = { path = "../wolfssl-sys" } +[features] +std = [] + +[build-dependencies] +bindgen = "0.72.1" + +[profile.release] +strip = true +opt-level = "s" +lto = true +codegen-units = 1 +panic = "abort" diff --git a/wrapper/rust/wolfssl/build.rs b/wrapper/rust/wolfssl/build.rs index 5720041412..bf362ff3b2 100644 --- a/wrapper/rust/wolfssl/build.rs +++ b/wrapper/rust/wolfssl/build.rs @@ -1,4 +1,8 @@ -use std::io::Result; +extern crate bindgen; + +use std::env; +use std::io::{self, Result}; +use std::path::PathBuf; /// Perform crate build. fn main() { @@ -12,10 +16,42 @@ fn main() { /// /// Returns `Ok(())` if successful, or an error if any step fails. fn run_build() -> Result<()> { + generate_bindings()?; setup_wolfssl_link()?; Ok(()) } +/// Generate Rust bindings for the wolfssl C library using bindgen. +/// +/// This function: +/// 1. Sets up the library and include paths +/// 2. Configures the build environment +/// 3. Generates Rust bindings using bindgen +/// 4. Writes the bindings to a file +/// +/// 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)) + .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")) + .map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("Couldn't write bindings: {}", e), + ) + }) +} + /// Instruct cargo to link against wolfssl C library /// /// Returns `Ok(())` if successful, or an error if any step fails. @@ -28,5 +64,8 @@ fn setup_wolfssl_link() -> Result<()> { println!("cargo:rustc-link-lib=wolfssl"); 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(()) } diff --git a/wrapper/rust/wolfssl-sys/headers.h b/wrapper/rust/wolfssl/headers.h similarity index 100% rename from wrapper/rust/wolfssl-sys/headers.h rename to wrapper/rust/wolfssl/headers.h diff --git a/wrapper/rust/wolfssl/src/lib.rs b/wrapper/rust/wolfssl/src/lib.rs index 3dc469b3e1..2be221d415 100644 --- a/wrapper/rust/wolfssl/src/lib.rs +++ b/wrapper/rust/wolfssl/src/lib.rs @@ -19,3 +19,4 @@ */ pub mod wolfcrypt; +pub mod sys; diff --git a/wrapper/rust/wolfssl-sys/src/lib.rs b/wrapper/rust/wolfssl/src/sys.rs similarity index 100% rename from wrapper/rust/wolfssl-sys/src/lib.rs rename to wrapper/rust/wolfssl/src/sys.rs diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs b/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs index 286fe93fdb..c8a77c4382 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs @@ -21,13 +21,10 @@ /*! This module provides a Rust wrapper for the wolfCrypt library's Advanced Encryption Standard (AES) functionality. - -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. */ +use crate::sys; use std::mem::{size_of, MaybeUninit}; -use wolfssl_sys as ws; /// AES Cipher Block Chaining (CBC) mode. /// @@ -55,7 +52,7 @@ use wolfssl_sys as ws; /// assert_eq!(&plain_out, &msg); /// ``` pub struct CBC { - ws_aes: ws::Aes, + ws_aes: sys::Aes, } impl CBC { /// Create a new `CBC` instance. @@ -89,11 +86,11 @@ impl CBC { let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let iv_ptr = iv.as_ptr() as *const u8; - if iv.len() as u32 != ws::WC_AES_BLOCK_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + if iv.len() as u32 != sys::WC_AES_BLOCK_SIZE { + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, iv_ptr, dir) + sys::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, iv_ptr, dir) }; if rc != 0 { return Err(rc); @@ -117,7 +114,7 @@ impl CBC { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_encrypt(&mut self, key: &[u8], iv: &[u8]) -> Result<(), i32> { - return self.init(key, iv, ws::AES_ENCRYPTION as i32); + return self.init(key, iv, sys::AES_ENCRYPTION as i32); } /// Initialize a CBC instance for decryption. @@ -136,7 +133,7 @@ impl CBC { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_decrypt(&mut self, key: &[u8], iv: &[u8]) -> Result<(), i32> { - return self.init(key, iv, ws::AES_DECRYPTION as i32); + return self.init(key, iv, sys::AES_DECRYPTION as i32); } /// Encrypt data. @@ -160,10 +157,10 @@ impl CBC { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCbcEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesCbcEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -192,10 +189,10 @@ impl CBC { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCbcDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesCbcDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -206,7 +203,7 @@ impl CBC { impl Drop for CBC { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_AesFree(&mut self.ws_aes); } + unsafe { sys::wc_AesFree(&mut self.ws_aes); } } } @@ -255,7 +252,7 @@ impl Drop for CBC { /// assert_eq!(plain_out, plaintext); /// ``` pub struct CCM { - ws_aes: ws::Aes, + ws_aes: sys::Aes, } impl CCM { /// Create a new `CCM` instance. @@ -302,7 +299,7 @@ impl CCM { let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - ws::wc_AesCcmSetKey(&mut self.ws_aes, key_ptr, key_size) + sys::wc_AesCcmSetKey(&mut self.ws_aes, key_ptr, key_size) }; if rc != 0 { return Err(rc); @@ -339,10 +336,10 @@ impl CCM { let auth_tag_ptr = auth_tag.as_ptr() as *mut u8; let auth_tag_size = (auth_tag.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCcmEncrypt(&mut self.ws_aes, out_ptr, + sys::wc_AesCcmEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size, nonce_ptr, nonce_size, auth_tag_ptr, auth_tag_size, @@ -383,10 +380,10 @@ impl CCM { let auth_tag_ptr = auth_tag.as_ptr() as *const u8; let auth_tag_size = (auth_tag.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCcmDecrypt(&mut self.ws_aes, out_ptr, + sys::wc_AesCcmDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size, nonce_ptr, nonce_size, auth_tag_ptr, auth_tag_size, @@ -401,7 +398,7 @@ impl CCM { impl Drop for CCM { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_AesFree(&mut self.ws_aes); } + unsafe { sys::wc_AesFree(&mut self.ws_aes); } } } @@ -446,7 +443,7 @@ impl Drop for CCM { /// assert_eq!(plain, msg); /// ``` pub struct CFB { - ws_aes: ws::Aes, + ws_aes: sys::Aes, } impl CFB { /// Create a new `CFB` instance. @@ -496,12 +493,12 @@ impl CFB { let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let iv_ptr = iv.as_ptr() as *const u8; - if iv.len() as u32 != ws::WC_AES_BLOCK_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + if iv.len() as u32 != sys::WC_AES_BLOCK_SIZE { + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, - iv_ptr, ws::AES_ENCRYPTION as i32) + sys::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, + iv_ptr, sys::AES_ENCRYPTION as i32) }; if rc != 0 { return Err(rc); @@ -529,10 +526,10 @@ impl CFB { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCfbEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesCfbEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -560,10 +557,10 @@ impl CFB { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCfb1Encrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesCfb1Encrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -591,10 +588,10 @@ impl CFB { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCfb8Encrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesCfb8Encrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -622,10 +619,10 @@ impl CFB { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCfbDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesCfbDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -653,10 +650,10 @@ impl CFB { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCfb1Decrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesCfb1Decrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -684,10 +681,10 @@ impl CFB { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCfb8Decrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesCfb8Decrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -698,7 +695,7 @@ impl CFB { impl Drop for CFB { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_AesFree(&mut self.ws_aes); } + unsafe { sys::wc_AesFree(&mut self.ws_aes); } } } @@ -746,7 +743,7 @@ impl Drop for CFB { /// assert_eq!(plain, msg); /// ``` pub struct CTR { - ws_aes: ws::Aes, + ws_aes: sys::Aes, } impl CTR { /// Create a new `CTR` instance. @@ -795,12 +792,12 @@ impl CTR { let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let iv_ptr = iv.as_ptr() as *const u8; - if iv.len() as u32 != ws::WC_AES_BLOCK_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + if iv.len() as u32 != sys::WC_AES_BLOCK_SIZE { + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesSetKeyDirect(&mut self.ws_aes, key_ptr, key_size, - iv_ptr, ws::AES_ENCRYPTION as i32) + sys::wc_AesSetKeyDirect(&mut self.ws_aes, key_ptr, key_size, + iv_ptr, sys::AES_ENCRYPTION as i32) }; if rc != 0 { return Err(rc); @@ -814,10 +811,10 @@ impl CTR { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesCtrEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesCtrEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -864,7 +861,7 @@ impl CTR { impl Drop for CTR { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_AesFree(&mut self.ws_aes); } + unsafe { sys::wc_AesFree(&mut self.ws_aes); } } } @@ -942,10 +939,10 @@ impl EAX { let out_ptr = dout.as_ptr() as *mut u8; let out_size = dout.len() as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesEaxEncryptAuth(key_ptr, key_size, out_ptr, + sys::wc_AesEaxEncryptAuth(key_ptr, key_size, out_ptr, in_ptr, in_size, nonce_ptr, nonce_size, auth_tag_ptr, auth_tag_size, auth_ptr, auth_size) @@ -988,10 +985,10 @@ impl EAX { let out_ptr = dout.as_ptr() as *mut u8; let out_size = dout.len() as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesEaxDecryptAuth(key_ptr, key_size, out_ptr, + sys::wc_AesEaxDecryptAuth(key_ptr, key_size, out_ptr, in_ptr, in_size, nonce_ptr, nonce_size, auth_tag_ptr, auth_tag_size, auth_ptr, auth_size) @@ -1028,7 +1025,7 @@ impl EAX { /// assert_eq!(&outbuf, &msg); /// ``` pub struct ECB { - ws_aes: ws::Aes, + ws_aes: sys::Aes, } impl ECB { /// Create a new `ECB` instance. @@ -1062,7 +1059,7 @@ impl ECB { let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - ws::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, + sys::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, core::ptr::null(), dir) }; if rc != 0 { @@ -1085,7 +1082,7 @@ impl ECB { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_encrypt(&mut self, key: &[u8]) -> Result<(), i32> { - return self.init(key, ws::AES_ENCRYPTION as i32); + return self.init(key, sys::AES_ENCRYPTION as i32); } /// Initialize a ECB instance for decryption. @@ -1102,7 +1099,7 @@ impl ECB { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_decrypt(&mut self, key: &[u8]) -> Result<(), i32> { - return self.init(key, ws::AES_DECRYPTION as i32); + return self.init(key, sys::AES_DECRYPTION as i32); } /// Encrypt data. @@ -1126,10 +1123,10 @@ impl ECB { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesEcbEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesEcbEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -1158,10 +1155,10 @@ impl ECB { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesEcbDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesEcbDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -1172,7 +1169,7 @@ impl ECB { impl Drop for ECB { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_AesFree(&mut self.ws_aes); } + unsafe { sys::wc_AesFree(&mut self.ws_aes); } } } @@ -1224,7 +1221,7 @@ impl Drop for ECB { /// assert_eq!(plain_out, plain); /// ``` pub struct GCM { - ws_aes: ws::Aes, + ws_aes: sys::Aes, } impl GCM { /// Create a new `GCM` instance. @@ -1271,7 +1268,7 @@ impl GCM { let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - ws::wc_AesGcmSetKey(&mut self.ws_aes, key_ptr, key_size) + sys::wc_AesGcmSetKey(&mut self.ws_aes, key_ptr, key_size) }; if rc != 0 { return Err(rc); @@ -1309,10 +1306,10 @@ impl GCM { let auth_tag_ptr = auth_tag.as_ptr() as *mut u8; let auth_tag_size = auth_tag.len() as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesGcmEncrypt(&mut self.ws_aes, out_ptr, + sys::wc_AesGcmEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size, iv_ptr, iv_size, auth_tag_ptr, auth_tag_size, @@ -1354,10 +1351,10 @@ impl GCM { let auth_tag_ptr = auth_tag.as_ptr() as *const u8; let auth_tag_size = auth_tag.len() as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesGcmDecrypt(&mut self.ws_aes, out_ptr, + sys::wc_AesGcmDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size, iv_ptr, iv_size, auth_tag_ptr, auth_tag_size, @@ -1372,7 +1369,7 @@ impl GCM { impl Drop for GCM { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_AesFree(&mut self.ws_aes); } + unsafe { sys::wc_AesFree(&mut self.ws_aes); } } } @@ -1452,7 +1449,7 @@ impl Drop for GCM { /// } /// ``` pub struct GCMStream { - ws_aes: ws::Aes, + ws_aes: sys::Aes, } impl GCMStream { /// Create a new `GCMStream` instance. @@ -1503,7 +1500,7 @@ impl GCMStream { let iv_ptr = iv.as_ptr() as *const u8; let iv_size = iv.len() as u32; let rc = unsafe { - ws::wc_AesGcmInit(&mut self.ws_aes, key_ptr, key_size, iv_ptr, iv_size) + sys::wc_AesGcmInit(&mut self.ws_aes, key_ptr, key_size, iv_ptr, iv_size) }; if rc != 0 { return Err(rc); @@ -1541,10 +1538,10 @@ impl GCMStream { let auth_ptr = auth.as_ptr() as *const u8; let auth_size = auth.len() as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesGcmEncryptUpdate(&mut self.ws_aes, out_ptr, + sys::wc_AesGcmEncryptUpdate(&mut self.ws_aes, out_ptr, in_ptr, in_size, auth_ptr, auth_size) }; @@ -1573,7 +1570,7 @@ impl GCMStream { let auth_tag_ptr = auth_tag.as_ptr() as *mut u8; let auth_tag_size = auth_tag.len() as u32; let rc = unsafe { - ws::wc_AesGcmEncryptFinal(&mut self.ws_aes, auth_tag_ptr, auth_tag_size) + sys::wc_AesGcmEncryptFinal(&mut self.ws_aes, auth_tag_ptr, auth_tag_size) }; if rc != 0 { return Err(rc); @@ -1611,10 +1608,10 @@ impl GCMStream { let auth_ptr = auth.as_ptr() as *const u8; let auth_size = auth.len() as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesGcmDecryptUpdate(&mut self.ws_aes, out_ptr, + sys::wc_AesGcmDecryptUpdate(&mut self.ws_aes, out_ptr, in_ptr, in_size, auth_ptr, auth_size) }; @@ -1643,7 +1640,7 @@ impl GCMStream { let auth_tag_ptr = auth_tag.as_ptr() as *const u8; let auth_tag_size = auth_tag.len() as u32; let rc = unsafe { - ws::wc_AesGcmDecryptFinal(&mut self.ws_aes, auth_tag_ptr, auth_tag_size) + sys::wc_AesGcmDecryptFinal(&mut self.ws_aes, auth_tag_ptr, auth_tag_size) }; if rc != 0 { return Err(rc); @@ -1654,7 +1651,7 @@ impl GCMStream { impl Drop for GCMStream { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_AesFree(&mut self.ws_aes); } + unsafe { sys::wc_AesFree(&mut self.ws_aes); } } } @@ -1700,7 +1697,7 @@ impl Drop for GCMStream { /// assert_eq!(plain_out, plain); /// ``` pub struct OFB { - ws_aes: ws::Aes, + ws_aes: sys::Aes, } impl OFB { /// Create a new `OFB` instance. @@ -1749,12 +1746,12 @@ impl OFB { let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let iv_ptr = iv.as_ptr() as *const u8; - if iv.len() as u32 != ws::WC_AES_BLOCK_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + if iv.len() as u32 != sys::WC_AES_BLOCK_SIZE { + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, iv_ptr, - ws::AES_ENCRYPTION as i32) + sys::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, iv_ptr, + sys::AES_ENCRYPTION as i32) }; if rc != 0 { return Err(rc); @@ -1782,10 +1779,10 @@ impl OFB { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesOfbEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesOfbEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -1813,10 +1810,10 @@ impl OFB { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesOfbDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) + sys::wc_AesOfbDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size) }; if rc != 0 { return Err(rc); @@ -1827,7 +1824,7 @@ impl OFB { impl Drop for OFB { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_AesFree(&mut self.ws_aes); } + unsafe { sys::wc_AesFree(&mut self.ws_aes); } } } @@ -1889,7 +1886,7 @@ impl Drop for OFB { /// assert_eq!(partial_out, partial); /// ``` pub struct XTS { - ws_xtsaes: ws::XtsAes, + ws_xtsaes: sys::XtsAes, } impl XTS { /// Create a new `XTS` instance. @@ -1923,7 +1920,7 @@ impl XTS { let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - ws::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, key_ptr, key_size, + sys::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, key_ptr, key_size, dir) }; if rc != 0 { @@ -1946,7 +1943,7 @@ impl XTS { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_encrypt(&mut self, key: &[u8]) -> Result<(), i32> { - return self.init(key, ws::AES_ENCRYPTION as i32); + return self.init(key, sys::AES_ENCRYPTION as i32); } /// Initialize a XTS instance for decryption. @@ -1963,7 +1960,7 @@ impl XTS { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_decrypt(&mut self, key: &[u8]) -> Result<(), i32> { - return self.init(key, ws::AES_DECRYPTION as i32); + return self.init(key, sys::AES_DECRYPTION as i32); } /// Encrypt data. @@ -1989,10 +1986,10 @@ impl XTS { let tweak_ptr = tweak.as_ptr() as *const u8; let tweak_size = tweak.len() as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesXtsEncrypt(&mut self.ws_xtsaes, out_ptr, + sys::wc_AesXtsEncrypt(&mut self.ws_xtsaes, out_ptr, in_ptr, in_size, tweak_ptr, tweak_size) }; @@ -2028,10 +2025,10 @@ impl XTS { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesXtsEncryptSector(&mut self.ws_xtsaes, out_ptr, + sys::wc_AesXtsEncryptSector(&mut self.ws_xtsaes, out_ptr, in_ptr, in_size, sector) }; if rc != 0 { @@ -2068,10 +2065,10 @@ impl XTS { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesXtsEncryptConsecutiveSectors(&mut self.ws_xtsaes, out_ptr, + sys::wc_AesXtsEncryptConsecutiveSectors(&mut self.ws_xtsaes, out_ptr, in_ptr, in_size, sector, sector_size) }; if rc != 0 { @@ -2103,10 +2100,10 @@ impl XTS { let tweak_ptr = tweak.as_ptr() as *const u8; let tweak_size = tweak.len() as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesXtsDecrypt(&mut self.ws_xtsaes, out_ptr, + sys::wc_AesXtsDecrypt(&mut self.ws_xtsaes, out_ptr, in_ptr, in_size, tweak_ptr, tweak_size) }; @@ -2142,10 +2139,10 @@ impl XTS { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesXtsDecryptSector(&mut self.ws_xtsaes, out_ptr, + sys::wc_AesXtsDecryptSector(&mut self.ws_xtsaes, out_ptr, in_ptr, in_size, sector) }; if rc != 0 { @@ -2182,10 +2179,10 @@ impl XTS { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesXtsDecryptConsecutiveSectors(&mut self.ws_xtsaes, out_ptr, + sys::wc_AesXtsDecryptConsecutiveSectors(&mut self.ws_xtsaes, out_ptr, in_ptr, in_size, sector, sector_size) }; if rc != 0 { @@ -2197,7 +2194,7 @@ impl XTS { impl Drop for XTS { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_AesXtsFree(&mut self.ws_xtsaes); } + unsafe { sys::wc_AesXtsFree(&mut self.ws_xtsaes); } } } @@ -2249,8 +2246,8 @@ impl Drop for XTS { /// assert_eq!(plain_out, plain); /// ``` pub struct XTSStream { - ws_xtsaes: ws::XtsAes, - ws_xtsaesstreamdata: ws::XtsAesStreamData, + ws_xtsaes: sys::XtsAes, + ws_xtsaesstreamdata: sys::XtsAesStreamData, } impl XTSStream { /// Create a new `XTSStream` instance. @@ -2276,7 +2273,7 @@ impl XTSStream { /// wolfSSL library return code on failure. pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let ws_xtsaes = new_ws_xtsaes(heap, dev_id)?; - let ws_xtsaesstreamdata: MaybeUninit = MaybeUninit::uninit(); + let ws_xtsaesstreamdata: MaybeUninit = MaybeUninit::uninit(); let ws_xtsaesstreamdata = unsafe { ws_xtsaesstreamdata.assume_init() }; let xtsstream = XTSStream {ws_xtsaes, ws_xtsaesstreamdata}; Ok(xtsstream) @@ -2300,8 +2297,8 @@ impl XTSStream { let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - ws::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, key_ptr, key_size, - ws::AES_ENCRYPTION as i32) + sys::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, key_ptr, key_size, + sys::AES_ENCRYPTION as i32) }; if rc != 0 { return Err(rc); @@ -2309,7 +2306,7 @@ impl XTSStream { let tweak_ptr = tweak.as_ptr() as *const u8; let tweak_size = tweak.len() as u32; let rc = unsafe { - ws::wc_AesXtsEncryptInit(&mut self.ws_xtsaes, tweak_ptr, tweak_size, + sys::wc_AesXtsEncryptInit(&mut self.ws_xtsaes, tweak_ptr, tweak_size, &mut self.ws_xtsaesstreamdata) }; if rc != 0 { @@ -2336,8 +2333,8 @@ impl XTSStream { let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - ws::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, key_ptr, key_size, - ws::AES_DECRYPTION as i32) + sys::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, key_ptr, key_size, + sys::AES_DECRYPTION as i32) }; if rc != 0 { return Err(rc); @@ -2345,7 +2342,7 @@ impl XTSStream { let tweak_ptr = tweak.as_ptr() as *const u8; let tweak_size = tweak.len() as u32; let rc = unsafe { - ws::wc_AesXtsDecryptInit(&mut self.ws_xtsaes, tweak_ptr, tweak_size, + sys::wc_AesXtsDecryptInit(&mut self.ws_xtsaes, tweak_ptr, tweak_size, &mut self.ws_xtsaesstreamdata) }; if rc != 0 { @@ -2378,10 +2375,10 @@ impl XTSStream { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesXtsEncryptUpdate(&mut self.ws_xtsaes, out_ptr, + sys::wc_AesXtsEncryptUpdate(&mut self.ws_xtsaes, out_ptr, in_ptr, in_size, &mut self.ws_xtsaesstreamdata) }; if rc != 0 { @@ -2413,10 +2410,10 @@ impl XTSStream { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesXtsEncryptFinal(&mut self.ws_xtsaes, out_ptr, + sys::wc_AesXtsEncryptFinal(&mut self.ws_xtsaes, out_ptr, in_ptr, in_size, &mut self.ws_xtsaesstreamdata) }; if rc != 0 { @@ -2449,10 +2446,10 @@ impl XTSStream { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesXtsDecryptUpdate(&mut self.ws_xtsaes, out_ptr, + sys::wc_AesXtsDecryptUpdate(&mut self.ws_xtsaes, out_ptr, in_ptr, in_size, &mut self.ws_xtsaesstreamdata) }; if rc != 0 { @@ -2484,10 +2481,10 @@ impl XTSStream { let out_ptr = dout.as_ptr() as *mut u8; let out_size = (dout.len() * size_of::()) as u32; if in_size != out_size { - return Err(ws::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); + return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - ws::wc_AesXtsDecryptFinal(&mut self.ws_xtsaes, out_ptr, + sys::wc_AesXtsDecryptFinal(&mut self.ws_xtsaes, out_ptr, in_ptr, in_size, &mut self.ws_xtsaesstreamdata) }; if rc != 0 { @@ -2499,22 +2496,22 @@ impl XTSStream { impl Drop for XTSStream { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_AesXtsFree(&mut self.ws_xtsaes); } + unsafe { sys::wc_AesXtsFree(&mut self.ws_xtsaes); } } } -fn new_ws_aes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { +fn new_ws_aes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let mut ws_aes: MaybeUninit = MaybeUninit::uninit(); + let mut ws_aes: MaybeUninit = MaybeUninit::uninit(); let rc = unsafe { - ws::wc_AesInit(ws_aes.as_mut_ptr(), heap, dev_id) + sys::wc_AesInit(ws_aes.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); @@ -2523,18 +2520,18 @@ fn new_ws_aes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> R Ok(ws_aes) } -fn new_ws_xtsaes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { +fn new_ws_xtsaes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let mut ws_xtsaes: MaybeUninit = MaybeUninit::uninit(); + let mut ws_xtsaes: MaybeUninit = MaybeUninit::uninit(); let rc = unsafe { - ws::wc_AesXtsInit(ws_xtsaes.as_mut_ptr(), heap, dev_id) + sys::wc_AesXtsInit(ws_xtsaes.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs b/wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs index a81d5095fa..5b06e403e4 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs @@ -21,13 +21,10 @@ /*! This module provides a Rust wrapper for the wolfCrypt library's Cipher-based Message Authentication Code (CMAC) functionality. - -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. */ +use crate::sys; use std::mem::MaybeUninit; -use wolfssl_sys as ws; /// The `CMAC` struct manages the lifecycle of a wolfSSL `Cmac` object. /// @@ -35,7 +32,7 @@ use wolfssl_sys as ws; /// /// An instance can be created with `new()`. pub struct CMAC { - ws_cmac: ws::Cmac, + ws_cmac: sys::Cmac, } impl CMAC { /// One-shot CMAC generation function. @@ -71,7 +68,7 @@ impl CMAC { let data_size = data.len() as u32; let mut dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_AesCmacGenerate(dout.as_mut_ptr(), &mut dout_size, + sys::wc_AesCmacGenerate(dout.as_mut_ptr(), &mut dout_size, data.as_ptr(), data_size, key.as_ptr(), key_size) }; @@ -132,18 +129,18 @@ impl CMAC { /// ``` pub fn new_ex(key: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let key_size = key.len() as u32; - let mut ws_cmac: MaybeUninit = MaybeUninit::uninit(); - let typ = ws::CmacType_WC_CMAC_AES as i32; + let mut ws_cmac: MaybeUninit = MaybeUninit::uninit(); + let typ = sys::CmacType_WC_CMAC_AES as i32; let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_InitCmac_ex(ws_cmac.as_mut_ptr(), key.as_ptr(), key_size, + sys::wc_InitCmac_ex(ws_cmac.as_mut_ptr(), key.as_ptr(), key_size, typ, core::ptr::null_mut(), heap, dev_id) }; if rc != 0 { @@ -190,7 +187,7 @@ impl CMAC { let data_size = data.len() as u32; let check_size = check.len() as u32; let rc = unsafe { - ws::wc_AesCmacVerify(check.as_ptr(), check_size, + sys::wc_AesCmacVerify(check.as_ptr(), check_size, data.as_ptr(), data_size, key.as_ptr(), key_size) }; @@ -241,10 +238,10 @@ impl CMAC { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_AesCmacGenerate_ex(&mut self.ws_cmac, + sys::wc_AesCmacGenerate_ex(&mut self.ws_cmac, dout.as_mut_ptr(), &mut dout_size, data.as_ptr(), data_size, key.as_ptr(), key_size, heap, dev_id) @@ -284,7 +281,7 @@ impl CMAC { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_CmacUpdate(&mut self.ws_cmac, data.as_ptr(), data_size) + sys::wc_CmacUpdate(&mut self.ws_cmac, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -326,7 +323,7 @@ impl CMAC { pub fn finalize(mut self, dout: &mut [u8]) -> Result<(), i32> { let mut dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_CmacFinalNoFree(&mut self.ws_cmac, + sys::wc_CmacFinalNoFree(&mut self.ws_cmac, dout.as_mut_ptr(), &mut dout_size) }; if rc != 0 { @@ -379,10 +376,10 @@ impl CMAC { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_AesCmacVerify_ex(&mut self.ws_cmac, + sys::wc_AesCmacVerify_ex(&mut self.ws_cmac, check.as_ptr(), check_size, data.as_ptr(), data_size, key.as_ptr(), key_size, heap, dev_id) @@ -396,6 +393,6 @@ impl CMAC { impl Drop for CMAC { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_CmacFree(&mut self.ws_cmac); } + unsafe { sys::wc_CmacFree(&mut self.ws_cmac); } } } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/dh.rs b/wrapper/rust/wolfssl/src/wolfcrypt/dh.rs index 939ca059d0..4739dd0666 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/dh.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/dh.rs @@ -22,33 +22,29 @@ This module provides a Rust wrapper for the wolfCrypt library's Diffie-Hellman (DH) functionality. -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. - The primary component is the `DH` struct, which manages the lifecycle of a wolfSSL `DhKey` object. It ensures proper initialization and deallocation. */ -use wolfssl_sys as ws; - -use std::mem::{MaybeUninit}; +use crate::sys; use crate::wolfcrypt::random::RNG; +use std::mem::{MaybeUninit}; pub struct DH { - wc_dhkey: ws::DhKey, + wc_dhkey: sys::DhKey, } impl DH { /// ffdhe2048 named parameter group. - pub const FFDHE_2048: i32 = ws::WC_FFDHE_2048 as i32; + pub const FFDHE_2048: i32 = sys::WC_FFDHE_2048 as i32; /// ffdhe3072 named parameter group. - pub const FFDHE_3072: i32 = ws::WC_FFDHE_3072 as i32; + pub const FFDHE_3072: i32 = sys::WC_FFDHE_3072 as i32; /// ffdhe4096 named parameter group. - pub const FFDHE_4096: i32 = ws::WC_FFDHE_4096 as i32; + pub const FFDHE_4096: i32 = sys::WC_FFDHE_4096 as i32; /// ffdhe6144 named parameter group. - pub const FFDHE_6144: i32 = ws::WC_FFDHE_6144 as i32; + pub const FFDHE_6144: i32 = sys::WC_FFDHE_6144 as i32; /// ffdhe8192 named parameter group. - pub const FFDHE_8192: i32 = ws::WC_FFDHE_8192 as i32; + pub const FFDHE_8192: i32 = sys::WC_FFDHE_8192 as i32; /// Perform quick validity check of public key value against prime. /// @@ -94,7 +90,7 @@ impl DH { let prime_size = prime.len() as u32; let public_size = public.len() as u32; let rc = unsafe { - ws::wc_DhCheckPubValue(prime.as_ptr(), prime_size, + sys::wc_DhCheckPubValue(prime.as_ptr(), prime_size, public.as_ptr(), public_size) }; if rc != 0 { @@ -145,7 +141,7 @@ impl DH { q_size = q.len() as u32; } let rc = unsafe { - ws::wc_DhCmpNamedKey(name, no_q, + sys::wc_DhCmpNamedKey(name, no_q, p.as_ptr(), p_size, g.as_ptr(), g_size, q_ptr, q_size) @@ -201,23 +197,23 @@ impl DH { /// let mut dh = DH::generate_ex(&mut rng, 2048, None, None).expect("Error with generate_ex()"); /// ``` 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 mut wc_dhkey: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let wc_dhkey = unsafe { wc_dhkey.assume_init() }; let mut dh = DH { wc_dhkey }; let rc = unsafe { - ws::wc_DhGenerateParams(&mut rng.wc_rng, modulus_size, &mut dh.wc_dhkey) + sys::wc_DhGenerateParams(&mut rng.wc_rng, modulus_size, &mut dh.wc_dhkey) }; if rc != 0 { return Err(rc); @@ -244,7 +240,7 @@ impl DH { /// assert_eq!(min_key_size, 29); /// ``` pub fn get_min_key_size_for_named_parameters(name: i32) -> u32 { - unsafe { ws::wc_DhGetNamedKeyMinSize(name) } + unsafe { sys::wc_DhGetNamedKeyMinSize(name) } } /// Get parameter sizes for a named parameter set. @@ -268,7 +264,7 @@ impl DH { /// ``` pub fn get_named_parameter_sizes(name: i32, p_size: &mut u32, g_size: &mut u32, q_size: &mut u32) { unsafe { - ws::wc_DhGetNamedKeyParamSize(name, p_size, g_size, q_size) + sys::wc_DhGetNamedKeyParamSize(name, p_size, g_size, q_size) }; } @@ -316,22 +312,22 @@ impl DH { /// let mut dh = DH::new_named_ex(DH::FFDHE_2048, None, None).expect("Error with new_named_ex()"); /// ``` pub fn new_named_ex(name: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_dhkey: MaybeUninit = MaybeUninit::uninit(); + let mut wc_dhkey: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let wc_dhkey = unsafe { wc_dhkey.assume_init() }; let mut dh = DH { wc_dhkey }; - let rc = unsafe { ws::wc_DhSetNamedKey(&mut dh.wc_dhkey, name) }; + let rc = unsafe { sys::wc_DhSetNamedKey(&mut dh.wc_dhkey, name) }; if rc != 0 { return Err(rc); } @@ -522,23 +518,23 @@ impl DH { pub fn new_from_pg_ex(p: &[u8], g: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let p_size = p.len() as u32; let g_size = g.len() as u32; - let mut wc_dhkey: MaybeUninit = MaybeUninit::uninit(); + let mut wc_dhkey: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let wc_dhkey = unsafe { wc_dhkey.assume_init() }; let mut dh = DH { wc_dhkey }; let rc = unsafe { - ws::wc_DhSetKey(&mut dh.wc_dhkey, p.as_ptr(), p_size, g.as_ptr(), g_size) + sys::wc_DhSetKey(&mut dh.wc_dhkey, p.as_ptr(), p_size, g.as_ptr(), g_size) }; if rc != 0 { return Err(rc); @@ -745,23 +741,23 @@ impl DH { let p_size = p.len() as u32; let g_size = g.len() as u32; let q_size = q.len() as u32; - let mut wc_dhkey: MaybeUninit = MaybeUninit::uninit(); + let mut wc_dhkey: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let wc_dhkey = unsafe { wc_dhkey.assume_init() }; let mut dh = DH { wc_dhkey }; let rc = unsafe { - ws::wc_DhSetKey_ex(&mut dh.wc_dhkey, p.as_ptr(), p_size, g.as_ptr(), g_size, q.as_ptr(), q_size) + sys::wc_DhSetKey_ex(&mut dh.wc_dhkey, p.as_ptr(), p_size, g.as_ptr(), g_size, q.as_ptr(), q_size) }; if rc != 0 { return Err(rc); @@ -977,23 +973,23 @@ impl DH { let p_size = p.len() as u32; let g_size = g.len() as u32; let q_size = q.len() as u32; - let mut wc_dhkey: MaybeUninit = MaybeUninit::uninit(); + let mut wc_dhkey: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let wc_dhkey = unsafe { wc_dhkey.assume_init() }; let mut dh = DH { wc_dhkey }; let rc = unsafe { - ws::wc_DhSetCheckKey(&mut dh.wc_dhkey, p.as_ptr(), p_size, g.as_ptr(), g_size, q.as_ptr(), q_size, trusted, &mut rng.wc_rng) + sys::wc_DhSetCheckKey(&mut dh.wc_dhkey, p.as_ptr(), p_size, g.as_ptr(), g_size, q.as_ptr(), q_size, trusted, &mut rng.wc_rng) }; if rc != 0 { return Err(rc); @@ -1034,7 +1030,7 @@ impl DH { let public_size = public.len() as u32; let private_size = private.len() as u32; let rc = unsafe { - ws::wc_DhCheckKeyPair(&mut self.wc_dhkey, + sys::wc_DhCheckKeyPair(&mut self.wc_dhkey, public.as_ptr(), public_size, private.as_ptr(), private_size) }; @@ -1075,7 +1071,7 @@ impl DH { pub fn check_priv_key(&mut self, private: &[u8]) -> Result<(), i32> { let private_size = private.len() as u32; let rc = unsafe { - ws::wc_DhCheckPrivKey(&mut self.wc_dhkey, + sys::wc_DhCheckPrivKey(&mut self.wc_dhkey, private.as_ptr(), private_size) }; if rc != 0 { @@ -1198,7 +1194,7 @@ impl DH { prime_size = prime.len() as u32; } let rc = unsafe { - ws::wc_DhCheckPrivKey_ex(&mut self.wc_dhkey, + sys::wc_DhCheckPrivKey_ex(&mut self.wc_dhkey, private.as_ptr(), private_size, prime_ptr, prime_size) }; @@ -1239,7 +1235,7 @@ impl DH { pub fn check_pub_key(&mut self, public: &[u8]) -> Result<(), i32> { let public_size = public.len() as u32; let rc = unsafe { - ws::wc_DhCheckPubKey(&mut self.wc_dhkey, public.as_ptr(), public_size) + sys::wc_DhCheckPubKey(&mut self.wc_dhkey, public.as_ptr(), public_size) }; if rc != 0 { return Err(rc); @@ -1361,7 +1357,7 @@ impl DH { let public_size = public.len() as u32; let prime_size = prime.len() as u32; let rc = unsafe { - ws::wc_DhCheckPubKey_ex(&mut self.wc_dhkey, + sys::wc_DhCheckPubKey_ex(&mut self.wc_dhkey, public.as_ptr(), public_size, prime.as_ptr(), prime_size) }; @@ -1394,7 +1390,7 @@ impl DH { *q_size = q.len() as u32; *g_size = g.len() as u32; let rc = unsafe { - ws::wc_DhExportParamsRaw(&mut self.wc_dhkey, + sys::wc_DhExportParamsRaw(&mut self.wc_dhkey, p.as_mut_ptr(), p_size, q.as_mut_ptr(), q_size, g.as_mut_ptr(), g_size) @@ -1439,7 +1435,7 @@ impl DH { *private_size = private.len() as u32; *public_size = public.len() as u32; let rc = unsafe { - ws::wc_DhGenerateKeyPair(&mut self.wc_dhkey, &mut rng.wc_rng, + sys::wc_DhGenerateKeyPair(&mut self.wc_dhkey, &mut rng.wc_rng, private.as_mut_ptr(), private_size, public.as_mut_ptr(), public_size) }; @@ -1488,7 +1484,7 @@ impl DH { let private_size = private.len() as u32; let other_pub_size = other_pub.len() as u32; let rc = unsafe { - ws::wc_DhAgree(&mut self.wc_dhkey, + sys::wc_DhAgree(&mut self.wc_dhkey, dout.as_mut_ptr(), &mut dout_size, private.as_ptr(), private_size, other_pub.as_ptr(), other_pub_size) @@ -1509,6 +1505,6 @@ impl Drop for DH { /// DH struct instance goes out of scope, automatically cleaning up /// resources and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_FreeDhKey(&mut self.wc_dhkey); } + unsafe { sys::wc_FreeDhKey(&mut self.wc_dhkey); } } } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs b/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs index 257d81f378..ebde297c11 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs @@ -22,21 +22,17 @@ This module provides a Rust wrapper for the wolfCrypt library's ECC functionality. -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. - The primary component is the `ECC` struct, which manages the lifecycle of a wolfSSL `ecc_key` object. It ensures proper initialization and deallocation. */ -use wolfssl_sys as ws; - -use std::mem::{MaybeUninit}; +use crate::sys; use crate::wolfcrypt::random::RNG; +use std::mem::{MaybeUninit}; /// Rust wrapper for wolfSSL `ecc_point` object. pub struct ECCPoint { - wc_ecc_point: *mut ws::ecc_point, + wc_ecc_point: *mut sys::ecc_point, heap: *mut std::os::raw::c_void, } @@ -69,7 +65,7 @@ impl ECCPoint { /// ECCPoint::import_der(&der[0..size], curve_id, None).expect("Error with import_der()"); /// ``` pub fn import_der(din: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>) -> Result { - let curve_idx = unsafe { ws::wc_ecc_get_curve_idx(curve_id) }; + let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) }; if curve_idx < 0 { return Err(curve_idx); } @@ -77,14 +73,14 @@ impl ECCPoint { Some(heap) => heap, None => core::ptr::null_mut(), }; - let wc_ecc_point = unsafe { ws::wc_ecc_new_point_h(heap) }; + let wc_ecc_point = unsafe { sys::wc_ecc_new_point_h(heap) }; if wc_ecc_point.is_null() { - return Err(ws::wolfCrypt_ErrorCodes_MEMORY_E); + return Err(sys::wolfCrypt_ErrorCodes_MEMORY_E); } let eccpoint = ECCPoint { wc_ecc_point, heap }; let din_size = din.len() as u32; let rc = unsafe { - ws::wc_ecc_import_point_der(din.as_ptr(), din_size, curve_idx, + sys::wc_ecc_import_point_der(din.as_ptr(), din_size, curve_idx, eccpoint.wc_ecc_point) }; if rc != 0 { @@ -123,7 +119,7 @@ impl ECCPoint { /// ECCPoint::import_der_ex(&der[0..size], curve_id, 1, None).expect("Error with import_der_ex()"); /// ``` 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 { ws::wc_ecc_get_curve_idx(curve_id) }; + let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) }; if curve_idx < 0 { return Err(curve_idx); } @@ -131,14 +127,14 @@ impl ECCPoint { Some(heap) => heap, None => core::ptr::null_mut(), }; - let wc_ecc_point = unsafe { ws::wc_ecc_new_point_h(heap) }; + let wc_ecc_point = unsafe { sys::wc_ecc_new_point_h(heap) }; if wc_ecc_point.is_null() { - return Err(ws::wolfCrypt_ErrorCodes_MEMORY_E); + return Err(sys::wolfCrypt_ErrorCodes_MEMORY_E); } let eccpoint = ECCPoint { wc_ecc_point, heap }; let din_size = din.len() as u32; let rc = unsafe { - ws::wc_ecc_import_point_der_ex(din.as_ptr(), din_size, curve_idx, + sys::wc_ecc_import_point_der_ex(din.as_ptr(), din_size, curve_idx, wc_ecc_point, short_key_size) }; if rc != 0 { @@ -175,13 +171,13 @@ impl ECCPoint { /// ECCPoint::import_der(&der[0..size], curve_id, None).expect("Error with import_der()"); /// ``` pub fn export_der(&self, dout: &mut [u8], curve_id: i32) -> Result { - let curve_idx = unsafe { ws::wc_ecc_get_curve_idx(curve_id) }; + let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) }; if curve_idx < 0 { return Err(curve_idx); } let mut dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_ecc_export_point_der(curve_idx, self.wc_ecc_point, + sys::wc_ecc_export_point_der(curve_idx, self.wc_ecc_point, dout.as_mut_ptr(), &mut dout_size) }; if rc != 0 { @@ -217,13 +213,13 @@ impl ECCPoint { /// ECCPoint::import_der_ex(&der[0..size], curve_id, 1, None).expect("Error with import_der_ex()"); /// ``` pub fn export_der_compressed(&self, dout: &mut [u8], curve_id: i32) -> Result { - let curve_idx = unsafe { ws::wc_ecc_get_curve_idx(curve_id) }; + let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) }; if curve_idx < 0 { return Err(curve_idx); } let mut dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_ecc_export_point_der_ex(curve_idx, self.wc_ecc_point, + sys::wc_ecc_export_point_der_ex(curve_idx, self.wc_ecc_point, dout.as_mut_ptr(), &mut dout_size, 1) }; if rc != 0 { @@ -245,7 +241,7 @@ impl ECCPoint { /// ecc_point.forcezero(); /// ``` pub fn forcezero(&mut self) { - unsafe { ws::wc_ecc_forcezero_point(self.wc_ecc_point) }; + unsafe { sys::wc_ecc_forcezero_point(self.wc_ecc_point) }; } } @@ -258,7 +254,7 @@ impl Drop for ECCPoint { /// ECCPoint struct instance goes out of scope, automatically cleaning up /// resources and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_ecc_del_point_h(self.wc_ecc_point, self.heap); } + unsafe { sys::wc_ecc_del_point_h(self.wc_ecc_point, self.heap); } } } @@ -270,49 +266,49 @@ impl Drop for ECCPoint { /// `import_x963_ex()`, `import_private_key()`, `import_private_key_ex()`, /// `import_raw()`, or `import_raw_ex()`. pub struct ECC { - wc_ecc_key: ws::ecc_key, + wc_ecc_key: sys::ecc_key, } impl ECC { - pub const CURVE_INVALID: i32 = ws::ecc_curve_ids_ECC_CURVE_INVALID; - pub const CURVE_DEF: i32 = ws::ecc_curve_ids_ECC_CURVE_DEF; - pub const SECP192R1: i32 = ws::ecc_curve_ids_ECC_SECP192R1; - pub const PRIME192V2: i32 = ws::ecc_curve_ids_ECC_PRIME192V2; - pub const PRIME192V3: i32 = ws::ecc_curve_ids_ECC_PRIME192V3; - pub const PRIME239V1: i32 = ws::ecc_curve_ids_ECC_PRIME239V1; - pub const PRIME239V2: i32 = ws::ecc_curve_ids_ECC_PRIME239V2; - pub const PRIME239V3: i32 = ws::ecc_curve_ids_ECC_PRIME239V3; - pub const SECP256R1: i32 = ws::ecc_curve_ids_ECC_SECP256R1; - pub const SECP112R1: i32 = ws::ecc_curve_ids_ECC_SECP112R1; - pub const SECP112R2: i32 = ws::ecc_curve_ids_ECC_SECP112R2; - pub const SECP128R1: i32 = ws::ecc_curve_ids_ECC_SECP128R1; - pub const SECP128R2: i32 = ws::ecc_curve_ids_ECC_SECP128R2; - pub const SECP160R1: i32 = ws::ecc_curve_ids_ECC_SECP160R1; - pub const SECP160R2: i32 = ws::ecc_curve_ids_ECC_SECP160R2; - pub const SECP224R1: i32 = ws::ecc_curve_ids_ECC_SECP224R1; - pub const SECP384R1: i32 = ws::ecc_curve_ids_ECC_SECP384R1; - pub const SECP521R1: i32 = ws::ecc_curve_ids_ECC_SECP521R1; - pub const SECP160K1: i32 = ws::ecc_curve_ids_ECC_SECP160K1; - pub const SECP192K1: i32 = ws::ecc_curve_ids_ECC_SECP192K1; - pub const SECP224K1: i32 = ws::ecc_curve_ids_ECC_SECP224K1; - pub const SECP256K1: i32 = ws::ecc_curve_ids_ECC_SECP256K1; - pub const BRAINPOOLP160R1: i32 = ws::ecc_curve_ids_ECC_BRAINPOOLP160R1; - pub const BRAINPOOLP192R1: i32 = ws::ecc_curve_ids_ECC_BRAINPOOLP192R1; - pub const BRAINPOOLP224R1: i32 = ws::ecc_curve_ids_ECC_BRAINPOOLP224R1; - pub const BRAINPOOLP256R1: i32 = ws::ecc_curve_ids_ECC_BRAINPOOLP256R1; - pub const BRAINPOOLP320R1: i32 = ws::ecc_curve_ids_ECC_BRAINPOOLP320R1; - pub const BRAINPOOLP384R1: i32 = ws::ecc_curve_ids_ECC_BRAINPOOLP384R1; - pub const BRAINPOOLP512R1: i32 = ws::ecc_curve_ids_ECC_BRAINPOOLP512R1; - pub const SM2P256V1: i32 = ws::ecc_curve_ids_ECC_SM2P256V1; - pub const X25519: i32 = ws::ecc_curve_ids_ECC_X25519; - pub const X448: i32 = ws::ecc_curve_ids_ECC_X448; - pub const SAKKE_1: i32 = ws::ecc_curve_ids_ECC_SAKKE_1; - pub const CURVE_CUSTOM: i32 = ws::ecc_curve_ids_ECC_CURVE_CUSTOM; - pub const CURVE_MAX: i32 = ws::ecc_curve_ids_ECC_CURVE_MAX; + pub const CURVE_INVALID: i32 = sys::ecc_curve_ids_ECC_CURVE_INVALID; + pub const CURVE_DEF: i32 = sys::ecc_curve_ids_ECC_CURVE_DEF; + pub const SECP192R1: i32 = sys::ecc_curve_ids_ECC_SECP192R1; + pub const PRIME192V2: i32 = sys::ecc_curve_ids_ECC_PRIME192V2; + pub const PRIME192V3: i32 = sys::ecc_curve_ids_ECC_PRIME192V3; + pub const PRIME239V1: i32 = sys::ecc_curve_ids_ECC_PRIME239V1; + pub const PRIME239V2: i32 = sys::ecc_curve_ids_ECC_PRIME239V2; + pub const PRIME239V3: i32 = sys::ecc_curve_ids_ECC_PRIME239V3; + pub const SECP256R1: i32 = sys::ecc_curve_ids_ECC_SECP256R1; + pub const SECP112R1: i32 = sys::ecc_curve_ids_ECC_SECP112R1; + pub const SECP112R2: i32 = sys::ecc_curve_ids_ECC_SECP112R2; + pub const SECP128R1: i32 = sys::ecc_curve_ids_ECC_SECP128R1; + pub const SECP128R2: i32 = sys::ecc_curve_ids_ECC_SECP128R2; + pub const SECP160R1: i32 = sys::ecc_curve_ids_ECC_SECP160R1; + pub const SECP160R2: i32 = sys::ecc_curve_ids_ECC_SECP160R2; + pub const SECP224R1: i32 = sys::ecc_curve_ids_ECC_SECP224R1; + pub const SECP384R1: i32 = sys::ecc_curve_ids_ECC_SECP384R1; + pub const SECP521R1: i32 = sys::ecc_curve_ids_ECC_SECP521R1; + pub const SECP160K1: i32 = sys::ecc_curve_ids_ECC_SECP160K1; + pub const SECP192K1: i32 = sys::ecc_curve_ids_ECC_SECP192K1; + pub const SECP224K1: i32 = sys::ecc_curve_ids_ECC_SECP224K1; + pub const SECP256K1: i32 = sys::ecc_curve_ids_ECC_SECP256K1; + pub const BRAINPOOLP160R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP160R1; + pub const BRAINPOOLP192R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP192R1; + pub const BRAINPOOLP224R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP224R1; + pub const BRAINPOOLP256R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP256R1; + pub const BRAINPOOLP320R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP320R1; + 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; + pub const X25519: i32 = sys::ecc_curve_ids_ECC_X25519; + pub const X448: i32 = sys::ecc_curve_ids_ECC_X448; + pub const SAKKE_1: i32 = sys::ecc_curve_ids_ECC_SAKKE_1; + pub const CURVE_CUSTOM: i32 = sys::ecc_curve_ids_ECC_CURVE_CUSTOM; + pub const CURVE_MAX: i32 = sys::ecc_curve_ids_ECC_CURVE_MAX; - pub const FLAG_NONE: i32 = ws::WC_ECC_FLAG_NONE as i32; - pub const FLAG_COFACTOR: i32 = ws::WC_ECC_FLAG_COFACTOR as i32; - pub const FLAG_DEC_SIGN: i32 = ws::WC_ECC_FLAG_DEC_SIGN as i32; + pub const FLAG_NONE: i32 = sys::WC_ECC_FLAG_NONE as i32; + pub const FLAG_COFACTOR: i32 = sys::WC_ECC_FLAG_COFACTOR as i32; + pub const FLAG_DEC_SIGN: i32 = sys::WC_ECC_FLAG_DEC_SIGN as i32; /// Generate a new ECC key with the given size. /// @@ -339,25 +335,25 @@ impl ECC { /// ecc.check().expect("Error with check()"); /// ``` pub fn generate(size: i32, rng: &mut RNG, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); + let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let mut wc_ecc_key = unsafe { wc_ecc_key.assume_init() }; let rc = unsafe { - ws::wc_ecc_make_key(&mut rng.wc_rng, size, &mut wc_ecc_key) + sys::wc_ecc_make_key(&mut rng.wc_rng, size, &mut wc_ecc_key) }; if rc != 0 { - unsafe { ws::wc_ecc_free(&mut wc_ecc_key); } + unsafe { sys::wc_ecc_free(&mut wc_ecc_key); } return Err(rc); } let ecc = ECC { wc_ecc_key }; @@ -392,25 +388,25 @@ impl ECC { /// ecc.check().expect("Error with check()"); /// ``` pub fn generate_ex(size: i32, rng: &mut RNG, curve_id: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); + let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let mut wc_ecc_key = unsafe { wc_ecc_key.assume_init() }; let rc = unsafe { - ws::wc_ecc_make_key_ex(&mut rng.wc_rng, size, &mut wc_ecc_key, curve_id) + sys::wc_ecc_make_key_ex(&mut rng.wc_rng, size, &mut wc_ecc_key, curve_id) }; if rc != 0 { - unsafe { ws::wc_ecc_free(&mut wc_ecc_key); } + unsafe { sys::wc_ecc_free(&mut wc_ecc_key); } return Err(rc); } let ecc = ECC { wc_ecc_key }; @@ -446,25 +442,25 @@ impl ECC { /// ecc.check().expect("Error with check()"); /// ``` pub fn generate_ex2(size: i32, rng: &mut RNG, curve_id: i32, flags: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); + let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let mut wc_ecc_key = unsafe { wc_ecc_key.assume_init() }; let rc = unsafe { - ws::wc_ecc_make_key_ex2(&mut rng.wc_rng, size, &mut wc_ecc_key, curve_id, flags) + sys::wc_ecc_make_key_ex2(&mut rng.wc_rng, size, &mut wc_ecc_key, curve_id, flags) }; if rc != 0 { - unsafe { ws::wc_ecc_free(&mut wc_ecc_key); } + unsafe { sys::wc_ecc_free(&mut wc_ecc_key); } return Err(rc); } let ecc = ECC { wc_ecc_key }; @@ -494,7 +490,7 @@ impl ECC { /// ecc.check().expect("Error with check()"); /// ``` pub fn get_curve_size_from_id(curve_id: i32) -> Result { - let rc = unsafe { ws::wc_ecc_get_curve_size_from_id(curve_id) }; + let rc = unsafe { sys::wc_ecc_get_curve_size_from_id(curve_id) }; if rc < 0 { return Err(rc); } @@ -526,16 +522,16 @@ impl ECC { /// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()"); /// ``` pub fn import_der(der: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); + let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -543,7 +539,7 @@ impl ECC { let mut idx = 0u32; let der_size = der.len() as u32; let rc = unsafe { - ws::wc_EccPrivateKeyDecode(der.as_ptr(), &mut idx, &mut wc_ecc_key, der_size) + sys::wc_EccPrivateKeyDecode(der.as_ptr(), &mut idx, &mut wc_ecc_key, der_size) }; if rc != 0 { return Err(rc); @@ -585,16 +581,16 @@ impl ECC { /// let mut ecc = ECC::import_public_der(&der, None, None).expect("Error with import_public_der()"); /// ``` pub fn import_public_der(der: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); + let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -602,7 +598,7 @@ impl ECC { let mut idx = 0u32; let der_size = der.len() as u32; let rc = unsafe { - ws::wc_EccPublicKeyDecode(der.as_ptr(), &mut idx, &mut wc_ecc_key, der_size) + sys::wc_EccPublicKeyDecode(der.as_ptr(), &mut idx, &mut wc_ecc_key, der_size) }; if rc != 0 { return Err(rc); @@ -650,16 +646,16 @@ impl ECC { /// assert_eq!(valid, true); /// ``` 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 mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -667,7 +663,7 @@ impl ECC { let priv_size = priv_buf.len() as u32; let pub_size = pub_buf.len() as u32; let rc = unsafe { - ws::wc_ecc_import_private_key(priv_buf.as_ptr(), priv_size, + sys::wc_ecc_import_private_key(priv_buf.as_ptr(), priv_size, pub_buf.as_ptr(), pub_size, &mut wc_ecc_key) }; if rc != 0 { @@ -719,16 +715,16 @@ impl ECC { /// assert_eq!(valid, true); /// ``` 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 mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -736,7 +732,7 @@ impl ECC { let priv_size = priv_buf.len() as u32; let pub_size = pub_buf.len() as u32; let rc = unsafe { - ws::wc_ecc_import_private_key_ex(priv_buf.as_ptr(), priv_size, + sys::wc_ecc_import_private_key_ex(priv_buf.as_ptr(), priv_size, pub_buf.as_ptr(), pub_size, &mut wc_ecc_key, curve_id) }; if rc != 0 { @@ -774,16 +770,16 @@ impl ECC { /// ECC::import_raw(qx, qy, d, b"SECP256R1\0", None, None).expect("Error with import_raw()"); /// ``` 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 mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -793,7 +789,7 @@ impl ECC { let d_ptr = d.as_ptr() as *const i8; let curve_name_ptr = curve_name.as_ptr() as *const i8; let rc = unsafe { - ws::wc_ecc_import_raw(&mut wc_ecc_key, qx_ptr, qy_ptr, d_ptr, + sys::wc_ecc_import_raw(&mut wc_ecc_key, qx_ptr, qy_ptr, d_ptr, curve_name_ptr) }; if rc != 0 { @@ -831,16 +827,16 @@ impl ECC { /// ECC::import_raw_ex(qx, qy, d, ECC::SECP256R1, None, None).expect("Error with import_raw_ex()"); /// ``` 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 mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -849,7 +845,7 @@ impl ECC { let qy_ptr = qy.as_ptr() as *const i8; let d_ptr = d.as_ptr() as *const i8; let rc = unsafe { - ws::wc_ecc_import_raw_ex(&mut wc_ecc_key, qx_ptr, qy_ptr, d_ptr, + sys::wc_ecc_import_raw_ex(&mut wc_ecc_key, qx_ptr, qy_ptr, d_ptr, curve_id) }; if rc != 0 { @@ -895,22 +891,22 @@ impl ECC { /// let mut ecc2 = ECC::import_unsigned(&qx, &qy, &d, curve_id, None, None).expect("Error with import_unsigned()"); /// ``` 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 mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let mut wc_ecc_key = unsafe { wc_ecc_key.assume_init() }; let rc = unsafe { - ws::wc_ecc_import_unsigned(&mut wc_ecc_key, qx.as_ptr(), qy.as_ptr(), + sys::wc_ecc_import_unsigned(&mut wc_ecc_key, qx.as_ptr(), qy.as_ptr(), d.as_ptr(), curve_id) }; if rc != 0 { @@ -950,25 +946,25 @@ impl ECC { /// ``` 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(); + let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let mut wc_ecc_key = unsafe { wc_ecc_key.assume_init() }; let rc = unsafe { - ws::wc_ecc_import_x963(din.as_ptr(), din_size, &mut wc_ecc_key) + sys::wc_ecc_import_x963(din.as_ptr(), din_size, &mut wc_ecc_key) }; if rc != 0 { - unsafe { ws::wc_ecc_free(&mut wc_ecc_key); } + unsafe { sys::wc_ecc_free(&mut wc_ecc_key); } return Err(rc); } let ecc = ECC { wc_ecc_key }; @@ -1010,25 +1006,25 @@ impl ECC { /// ``` 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(); + let mut wc_ecc_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let mut wc_ecc_key = unsafe { wc_ecc_key.assume_init() }; let rc = unsafe { - ws::wc_ecc_import_x963_ex(din.as_ptr(), din_size, &mut wc_ecc_key, curve_id) + sys::wc_ecc_import_x963_ex(din.as_ptr(), din_size, &mut wc_ecc_key, curve_id) }; if rc != 0 { - unsafe { ws::wc_ecc_free(&mut wc_ecc_key); } + unsafe { sys::wc_ecc_free(&mut wc_ecc_key); } return Err(rc); } let ecc = ECC { wc_ecc_key }; @@ -1091,7 +1087,7 @@ impl ECC { let r_ptr = r.as_ptr() as *const i8; let s_ptr = s.as_ptr() as *const i8; let rc = unsafe { - ws::wc_ecc_rs_to_sig(r_ptr, s_ptr, dout.as_mut_ptr(), + sys::wc_ecc_rs_to_sig(r_ptr, s_ptr, dout.as_mut_ptr(), &mut dout_size) }; if rc != 0 { @@ -1144,7 +1140,7 @@ impl ECC { let s_size = s.len() as u32; let mut dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_ecc_rs_raw_to_sig(r.as_ptr(), r_size, s.as_ptr(), s_size, + sys::wc_ecc_rs_raw_to_sig(r.as_ptr(), r_size, s.as_ptr(), s_size, dout.as_mut_ptr(), &mut dout_size) }; if rc != 0 { @@ -1193,7 +1189,7 @@ impl ECC { *r_size = r.len() as u32; *s_size = s.len() as u32; let rc = unsafe { - ws::wc_ecc_sig_to_rs(sig.as_ptr(), sig_len, + sys::wc_ecc_sig_to_rs(sig.as_ptr(), sig_len, r.as_mut_ptr(), r_size, s.as_mut_ptr(), s_size) }; if rc != 0 { @@ -1219,7 +1215,7 @@ impl ECC { /// ecc.check().expect("Error with check()"); /// ``` pub fn check(&mut self) -> Result<(), i32> { - let rc = unsafe { ws::wc_ecc_check_key(&mut self.wc_ecc_key) }; + let rc = unsafe { sys::wc_ecc_check_key(&mut self.wc_ecc_key) }; if rc != 0 { return Err(rc); } @@ -1263,7 +1259,7 @@ impl ECC { *qy_len = qy.len() as u32; *d_len = d.len() as u32; let rc = unsafe { - ws::wc_ecc_export_private_raw(&mut self.wc_ecc_key, + sys::wc_ecc_export_private_raw(&mut self.wc_ecc_key, qx.as_mut_ptr(), qx_len, qy.as_mut_ptr(), qy_len, d.as_mut_ptr(), d_len) @@ -1316,12 +1312,12 @@ impl ECC { *d_len = d.len() as u32; let enc_type = if hex { - ws::WC_TYPE_HEX_STR as i32 + sys::WC_TYPE_HEX_STR as i32 } else { - ws::WC_TYPE_UNSIGNED_BIN as i32 + sys::WC_TYPE_UNSIGNED_BIN as i32 }; let rc = unsafe { - ws::wc_ecc_export_ex(&mut self.wc_ecc_key, + sys::wc_ecc_export_ex(&mut self.wc_ecc_key, qx.as_mut_ptr(), qx_len, qy.as_mut_ptr(), qy_len, d.as_mut_ptr(), d_len, @@ -1358,7 +1354,7 @@ impl ECC { pub fn export_private(&mut self, d: &mut [u8]) -> Result { let mut d_size = d.len() as u32; let rc = unsafe { - ws::wc_ecc_export_private_only(&mut self.wc_ecc_key, + sys::wc_ecc_export_private_only(&mut self.wc_ecc_key, d.as_mut_ptr(), &mut d_size) }; if rc != 0 { @@ -1399,7 +1395,7 @@ impl ECC { *qx_len = qx.len() as u32; *qy_len = qy.len() as u32; let rc = unsafe { - ws::wc_ecc_export_public_raw(&mut self.wc_ecc_key, + sys::wc_ecc_export_public_raw(&mut self.wc_ecc_key, qx.as_mut_ptr(), qx_len, qy.as_mut_ptr(), qy_len) }; @@ -1433,7 +1429,7 @@ impl ECC { pub fn export_x963(&mut self, dout: &mut [u8]) -> Result { let mut out_len: u32 = dout.len() as u32; let rc = unsafe { - ws::wc_ecc_export_x963(&mut self.wc_ecc_key, dout.as_mut_ptr(), &mut out_len) + sys::wc_ecc_export_x963(&mut self.wc_ecc_key, dout.as_mut_ptr(), &mut out_len) }; if rc != 0 { return Err(rc); @@ -1465,7 +1461,7 @@ impl ECC { pub fn export_x963_compressed(&mut self, dout: &mut [u8]) -> Result { let mut out_len: u32 = dout.len() as u32; let rc = unsafe { - ws::wc_ecc_export_x963_ex(&mut self.wc_ecc_key, dout.as_mut_ptr(), &mut out_len, 1) + sys::wc_ecc_export_x963_ex(&mut self.wc_ecc_key, dout.as_mut_ptr(), &mut out_len, 1) }; if rc != 0 { return Err(rc); @@ -1503,7 +1499,7 @@ impl ECC { None => core::ptr::null_mut(), }; let rc = unsafe { - ws::wc_ecc_make_pub_ex(&mut self.wc_ecc_key, core::ptr::null_mut(), rng_ptr) + sys::wc_ecc_make_pub_ex(&mut self.wc_ecc_key, core::ptr::null_mut(), rng_ptr) }; if rc != 0 { return Err(rc); @@ -1545,13 +1541,13 @@ impl ECC { Some(heap) => heap, None => core::ptr::null_mut(), }; - let wc_ecc_point = unsafe { ws::wc_ecc_new_point_h(heap) }; + let wc_ecc_point = unsafe { sys::wc_ecc_new_point_h(heap) }; if wc_ecc_point.is_null() { - return Err(ws::wolfCrypt_ErrorCodes_MEMORY_E); + return Err(sys::wolfCrypt_ErrorCodes_MEMORY_E); } let ecc_point = ECCPoint { wc_ecc_point, heap }; let rc = unsafe { - ws::wc_ecc_make_pub_ex(&mut self.wc_ecc_key, wc_ecc_point, rng_ptr) + sys::wc_ecc_make_pub_ex(&mut self.wc_ecc_key, wc_ecc_point, rng_ptr) }; if rc != 0 { return Err(rc); @@ -1586,7 +1582,7 @@ impl ECC { /// ``` pub fn set_rng(&mut self, rng: &mut RNG) -> Result<(), i32> { let rc = unsafe { - ws::wc_ecc_set_rng(&mut self.wc_ecc_key, &mut rng.wc_rng) + sys::wc_ecc_set_rng(&mut self.wc_ecc_key, &mut rng.wc_rng) }; if rc != 0 { return Err(rc); @@ -1627,7 +1623,7 @@ impl ECC { 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 { - ws::wc_ecc_shared_secret(&mut self.wc_ecc_key, + sys::wc_ecc_shared_secret(&mut self.wc_ecc_key, &mut peer_key.wc_ecc_key, dout.as_mut_ptr(), &mut out_len) }; if rc < 0 { @@ -1671,7 +1667,7 @@ impl ECC { pub fn shared_secret_ex(&mut self, peer: &ECCPoint, dout: &mut [u8]) -> Result { let mut out_len = dout.len() as u32; let rc = unsafe { - ws::wc_ecc_shared_secret_ex(&mut self.wc_ecc_key, + sys::wc_ecc_shared_secret_ex(&mut self.wc_ecc_key, peer.wc_ecc_point, dout.as_mut_ptr(), &mut out_len) }; if rc != 0 { @@ -1711,7 +1707,7 @@ impl ECC { let din_size = din.len() as u32; let mut dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_ecc_sign_hash(din.as_ptr(), din_size, dout.as_mut_ptr(), + sys::wc_ecc_sign_hash(din.as_ptr(), din_size, dout.as_mut_ptr(), &mut dout_size, &mut rng.wc_rng, &mut self.wc_ecc_key) }; if rc != 0 { @@ -1751,7 +1747,7 @@ impl ECC { let sig_len = sig.len() as u32; let hash_len = hash.len() as u32; let rc = unsafe { - ws::wc_ecc_verify_hash(sig.as_ptr(), sig_len, + sys::wc_ecc_verify_hash(sig.as_ptr(), sig_len, hash.as_ptr(), hash_len, &mut res, &mut self.wc_ecc_key) }; if rc != 0 { @@ -1770,6 +1766,6 @@ impl Drop for ECC { /// struct goes out of scope, automatically cleaning up resources and /// preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_ecc_free(&mut self.wc_ecc_key); } + unsafe { sys::wc_ecc_free(&mut self.wc_ecc_key); } } } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs b/wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs index 3b0fcebe1f..aa73056b1c 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs @@ -21,14 +21,11 @@ /*! This module provides a Rust wrapper for the wolfCrypt library's EdDSA Curve 25519 (Ed25519) functionality. - -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. */ +use crate::sys; use crate::wolfcrypt::random::RNG; use std::mem::MaybeUninit; -use wolfssl_sys as ws; /// The `Ed25519` struct manages the lifecycle of a wolfSSL `ed25519_key` /// object. @@ -37,22 +34,22 @@ use wolfssl_sys as ws; /// /// An instance can be created with `generate()` or `new()`. pub struct Ed25519 { - ws_key: ws::ed25519_key, + ws_key: sys::ed25519_key, } impl Ed25519 { /** Size of private key only. */ - pub const KEY_SIZE: usize = ws::ED25519_KEY_SIZE as usize; + pub const KEY_SIZE: usize = sys::ED25519_KEY_SIZE as usize; /** Size of signature. */ - pub const SIG_SIZE: usize = ws::ED25519_SIG_SIZE as usize; + pub const SIG_SIZE: usize = sys::ED25519_SIG_SIZE as usize; /** Compressed public key size. */ - pub const PUB_KEY_SIZE: usize = ws::ED25519_PUB_KEY_SIZE as usize; + pub const PUB_KEY_SIZE: usize = sys::ED25519_PUB_KEY_SIZE as usize; /** Size of both private and public key. */ - pub const PRV_KEY_SIZE: usize = ws::ED25519_PRV_KEY_SIZE as usize; + pub const PRV_KEY_SIZE: usize = sys::ED25519_PRV_KEY_SIZE as usize; - pub const ED25519: u8 = ws::Ed25519 as u8; - pub const ED25519CTX: u8 = ws::Ed25519ctx as u8; - pub const ED25519PH: u8 = ws::Ed25519ph as u8; + pub const ED25519: u8 = sys::Ed25519 as u8; + pub const ED25519CTX: u8 = sys::Ed25519ctx as u8; + pub const ED25519PH: u8 = sys::Ed25519ph as u8; /// Generate a new Ed25519 key. /// @@ -99,24 +96,24 @@ impl Ed25519 { /// let ed = Ed25519::generate_ex(&mut rng, None, None).expect("Error with generate_ex()"); /// ``` pub fn generate_ex(rng: &mut RNG, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut ws_key: MaybeUninit = MaybeUninit::uninit(); + let mut ws_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ed25519_init_ex(ws_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ed25519_init_ex(ws_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let ws_key = unsafe { ws_key.assume_init() }; let mut ed25519 = Ed25519 { ws_key }; let rc = unsafe { - ws::wc_ed25519_make_key(&mut rng.wc_rng, - ws::ED25519_KEY_SIZE as i32, &mut ed25519.ws_key) + sys::wc_ed25519_make_key(&mut rng.wc_rng, + sys::ED25519_KEY_SIZE as i32, &mut ed25519.ws_key) }; if rc != 0 { return Err(rc); @@ -167,16 +164,16 @@ impl Ed25519 { /// let ed = Ed25519::new_ex(None, None).expect("Error with new()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut ws_key: MaybeUninit = MaybeUninit::uninit(); + let mut ws_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ed25519_init_ex(ws_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ed25519_init_ex(ws_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -206,7 +203,7 @@ impl Ed25519 { /// ed.check_key().expect("Error with check_key()"); /// ``` pub fn check_key(&mut self) -> Result<(), i32> { - let rc = unsafe { ws::wc_ed25519_check_key(&mut self.ws_key) }; + let rc = unsafe { sys::wc_ed25519_check_key(&mut self.ws_key) }; if rc != 0 { return Err(rc); } @@ -242,7 +239,7 @@ impl Ed25519 { let mut private_size = private.len() as u32; let mut public_size = public.len() as u32; let rc = unsafe { - ws::wc_ed25519_export_key(&self.ws_key, + sys::wc_ed25519_export_key(&self.ws_key, private.as_mut_ptr(), &mut private_size, public.as_mut_ptr(), &mut public_size) }; @@ -278,7 +275,7 @@ impl Ed25519 { pub fn export_public(&self, public: &mut [u8]) -> Result<(), i32> { let mut public_size = public.len() as u32; let rc = unsafe { - ws::wc_ed25519_export_public(&self.ws_key, public.as_mut_ptr(), + sys::wc_ed25519_export_public(&self.ws_key, public.as_mut_ptr(), &mut public_size) }; if rc != 0 { @@ -313,7 +310,7 @@ impl Ed25519 { pub fn export_private(&self, keyout: &mut [u8]) -> Result<(), i32> { let mut keyout_size = keyout.len() as u32; let rc = unsafe { - ws::wc_ed25519_export_private(&self.ws_key, keyout.as_mut_ptr(), + sys::wc_ed25519_export_private(&self.ws_key, keyout.as_mut_ptr(), &mut keyout_size) }; if rc != 0 { @@ -348,7 +345,7 @@ impl Ed25519 { pub fn export_private_only(&self, private: &mut [u8]) -> Result<(), i32> { let mut private_size = private.len() as u32; let rc = unsafe { - ws::wc_ed25519_export_private_only(&self.ws_key, + sys::wc_ed25519_export_private_only(&self.ws_key, private.as_mut_ptr(), &mut private_size) }; if rc != 0 { @@ -388,7 +385,7 @@ impl Ed25519 { pub fn import_public(&mut self, public: &[u8]) -> Result<(), i32> { let public_size = public.len() as u32; let rc = unsafe { - ws::wc_ed25519_import_public(public.as_ptr(), public_size, &mut self.ws_key) + sys::wc_ed25519_import_public(public.as_ptr(), public_size, &mut self.ws_key) }; if rc != 0 { return Err(rc); @@ -428,7 +425,7 @@ impl Ed25519 { pub fn import_public_ex(&mut self, public: &[u8], trusted: bool) -> Result<(), i32> { let public_size = public.len() as u32; let rc = unsafe { - ws::wc_ed25519_import_public_ex(public.as_ptr(), public_size, + sys::wc_ed25519_import_public_ex(public.as_ptr(), public_size, &mut self.ws_key, if trusted {1} else {0}) }; if rc != 0 { @@ -463,7 +460,7 @@ impl Ed25519 { pub fn import_private_only(&mut self, private: &[u8]) -> Result<(), i32> { let private_size = private.len() as u32; let rc = unsafe { - ws::wc_ed25519_import_private_only(private.as_ptr(), private_size, + sys::wc_ed25519_import_private_only(private.as_ptr(), private_size, &mut self.ws_key) }; if rc != 0 { @@ -510,7 +507,7 @@ impl Ed25519 { public_size = public.len() as u32; } let rc = unsafe { - ws::wc_ed25519_import_private_key(private.as_ptr(), private_size, + sys::wc_ed25519_import_private_key(private.as_ptr(), private_size, public_ptr, public_size, &mut self.ws_key) }; if rc != 0 { @@ -557,7 +554,7 @@ impl Ed25519 { public_size = public.len() as u32; } let rc = unsafe { - ws::wc_ed25519_import_private_key_ex(private.as_ptr(), private_size, + sys::wc_ed25519_import_private_key_ex(private.as_ptr(), private_size, public_ptr, public_size, &mut self.ws_key, if trusted {1} else {0}) }; if rc != 0 { @@ -597,7 +594,7 @@ impl Ed25519 { pub fn make_public(&mut self, pubkey: &mut [u8]) -> Result<(), i32> { let pubkey_size = pubkey.len() as u32; let rc = unsafe { - ws::wc_ed25519_make_public(&mut self.ws_key, + sys::wc_ed25519_make_public(&mut self.ws_key, pubkey.as_mut_ptr(), pubkey_size) }; if rc != 0 { @@ -634,7 +631,7 @@ impl Ed25519 { let message_size = message.len() as u32; let mut signature_size = signature.len() as u32; let rc = unsafe { - ws::wc_ed25519_sign_msg(message.as_ptr(), message_size, + sys::wc_ed25519_sign_msg(message.as_ptr(), message_size, signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key) }; if rc != 0 { @@ -676,7 +673,7 @@ impl Ed25519 { let context_size = context.len() as u8; let mut signature_size = signature.len() as u32; let rc = unsafe { - ws::wc_ed25519ctx_sign_msg(message.as_ptr(), message_size, + sys::wc_ed25519ctx_sign_msg(message.as_ptr(), message_size, signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key, context.as_ptr(), context_size) }; @@ -734,7 +731,7 @@ impl Ed25519 { } let mut signature_size = signature.len() as u32; let rc = unsafe { - ws::wc_ed25519ph_sign_hash(hash.as_ptr(), hash_size, + sys::wc_ed25519ph_sign_hash(hash.as_ptr(), hash_size, signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key, context_ptr, context_size) }; @@ -783,7 +780,7 @@ impl Ed25519 { } let mut signature_size = signature.len() as u32; let rc = unsafe { - ws::wc_ed25519ph_sign_msg(message.as_ptr(), message_size, + sys::wc_ed25519ph_sign_msg(message.as_ptr(), message_size, signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key, context_ptr, context_size) }; @@ -832,7 +829,7 @@ impl Ed25519 { } let mut signature_size = signature.len() as u32; let rc = unsafe { - ws::wc_ed25519_sign_msg_ex(din.as_ptr(), din_size, + sys::wc_ed25519_sign_msg_ex(din.as_ptr(), din_size, signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key, typ, context_ptr, context_size) }; @@ -872,7 +869,7 @@ impl Ed25519 { let message_size = message.len() as u32; let mut res = 0i32; let rc = unsafe { - ws::wc_ed25519_verify_msg(signature.as_ptr(), signature_size, + sys::wc_ed25519_verify_msg(signature.as_ptr(), signature_size, message.as_ptr(), message_size, &mut res, &mut self.ws_key) }; if rc != 0 { @@ -916,7 +913,7 @@ impl Ed25519 { let context_size = context.len() as u8; let mut res = 0i32; let rc = unsafe { - ws::wc_ed25519ctx_verify_msg(signature.as_ptr(), signature_size, + sys::wc_ed25519ctx_verify_msg(signature.as_ptr(), signature_size, message.as_ptr(), message_size, &mut res, &mut self.ws_key, context.as_ptr(), context_size) }; @@ -977,7 +974,7 @@ impl Ed25519 { } let mut res = 0i32; let rc = unsafe { - ws::wc_ed25519ph_verify_hash(signature.as_ptr(), signature_size, + sys::wc_ed25519ph_verify_hash(signature.as_ptr(), signature_size, hash.as_ptr(), hash_size, &mut res, &mut self.ws_key, context_ptr, context_size) }; @@ -1028,7 +1025,7 @@ impl Ed25519 { } let mut res = 0i32; let rc = unsafe { - ws::wc_ed25519ph_verify_msg(signature.as_ptr(), signature_size, + sys::wc_ed25519ph_verify_msg(signature.as_ptr(), signature_size, message.as_ptr(), message_size, &mut res, &mut self.ws_key, context_ptr, context_size) }; @@ -1079,7 +1076,7 @@ impl Ed25519 { } let mut res = 0i32; let rc = unsafe { - ws::wc_ed25519_verify_msg_ex(signature.as_ptr(), signature_size, + sys::wc_ed25519_verify_msg_ex(signature.as_ptr(), signature_size, din.as_ptr(), din_size, &mut res, &mut self.ws_key, typ, context_ptr, context_size) }; @@ -1127,7 +1124,7 @@ impl Ed25519 { context_size = context.len() as u8; } let rc = unsafe { - ws::wc_ed25519_verify_msg_init(signature.as_ptr(), signature_size, + sys::wc_ed25519_verify_msg_init(signature.as_ptr(), signature_size, &mut self.ws_key, typ, context_ptr, context_size) }; if rc != 0 { @@ -1166,7 +1163,7 @@ impl Ed25519 { pub fn verify_msg_update(&mut self, din: &[u8]) -> Result<(), i32> { let din_size = din.len() as u32; let rc = unsafe { - ws::wc_ed25519_verify_msg_update(din.as_ptr(), din_size, + sys::wc_ed25519_verify_msg_update(din.as_ptr(), din_size, &mut self.ws_key) }; if rc != 0 { @@ -1206,7 +1203,7 @@ impl Ed25519 { let signature_size = signature.len() as u32; let mut res = 0i32; let rc = unsafe { - ws::wc_ed25519_verify_msg_final(signature.as_ptr(), signature_size, + sys::wc_ed25519_verify_msg_final(signature.as_ptr(), signature_size, &mut res, &mut self.ws_key) }; if rc != 0 { @@ -1233,7 +1230,7 @@ impl Ed25519 { /// assert_eq!(key_size, Ed25519::KEY_SIZE); /// ``` pub fn size(&self) -> Result { - let rc = unsafe { ws::wc_ed25519_size(&self.ws_key) }; + let rc = unsafe { sys::wc_ed25519_size(&self.ws_key) }; if rc < 0 { return Err(rc); } @@ -1258,7 +1255,7 @@ impl Ed25519 { /// assert_eq!(priv_size, Ed25519::PRV_KEY_SIZE); /// ``` pub fn priv_size(&self) -> Result { - let rc = unsafe { ws::wc_ed25519_priv_size(&self.ws_key) }; + let rc = unsafe { sys::wc_ed25519_priv_size(&self.ws_key) }; if rc < 0 { return Err(rc); } @@ -1283,7 +1280,7 @@ impl Ed25519 { /// assert_eq!(pub_size, Ed25519::PUB_KEY_SIZE); /// ``` pub fn pub_size(&self) -> Result { - let rc = unsafe { ws::wc_ed25519_pub_size(&self.ws_key) }; + let rc = unsafe { sys::wc_ed25519_pub_size(&self.ws_key) }; if rc < 0 { return Err(rc); } @@ -1308,7 +1305,7 @@ impl Ed25519 { /// assert_eq!(sig_size, Ed25519::SIG_SIZE); /// ``` pub fn sig_size(&self) -> Result { - let rc = unsafe { ws::wc_ed25519_sig_size(&self.ws_key) }; + let rc = unsafe { sys::wc_ed25519_sig_size(&self.ws_key) }; if rc < 0 { return Err(rc); } @@ -1319,6 +1316,6 @@ impl Ed25519 { impl Drop for Ed25519 { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_ed25519_free(&mut self.ws_key); } + unsafe { sys::wc_ed25519_free(&mut self.ws_key); } } } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs b/wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs index 650131c968..33c576117c 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs @@ -21,14 +21,11 @@ /*! This module provides a Rust wrapper for the wolfCrypt library's EdDSA Curve 448 (Ed448) functionality. - -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. */ +use crate::sys; use crate::wolfcrypt::random::RNG; use std::mem::MaybeUninit; -use wolfssl_sys as ws; /// The `Ed448` struct manages the lifecycle of a wolfSSL `ed448_key` /// object. @@ -37,21 +34,21 @@ use wolfssl_sys as ws; /// /// An instance can be created with `generate()` or `new()`. pub struct Ed448 { - ws_key: ws::ed448_key, + ws_key: sys::ed448_key, } impl Ed448 { /** Size of private key only. */ - pub const KEY_SIZE: usize = ws::ED448_KEY_SIZE as usize; + pub const KEY_SIZE: usize = sys::ED448_KEY_SIZE as usize; /** Size of signature. */ - pub const SIG_SIZE: usize = ws::ED448_SIG_SIZE as usize; + pub const SIG_SIZE: usize = sys::ED448_SIG_SIZE as usize; /** Compressed public key size. */ - pub const PUB_KEY_SIZE: usize = ws::ED448_PUB_KEY_SIZE as usize; + pub const PUB_KEY_SIZE: usize = sys::ED448_PUB_KEY_SIZE as usize; /** Size of both private and public key. */ - pub const PRV_KEY_SIZE: usize = ws::ED448_PRV_KEY_SIZE as usize; + pub const PRV_KEY_SIZE: usize = sys::ED448_PRV_KEY_SIZE as usize; - pub const ED448: u8 = ws::Ed448 as u8; - pub const ED448PH: u8 = ws::Ed448ph as u8; + pub const ED448: u8 = sys::Ed448 as u8; + pub const ED448PH: u8 = sys::Ed448ph as u8; /// Generate a new Ed448 key. /// @@ -98,24 +95,24 @@ impl Ed448 { /// let ed = Ed448::generate_ex(&mut rng, None, None).expect("Error with generate_ex()"); /// ``` pub fn generate_ex(rng: &mut RNG, heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut ws_key: MaybeUninit = MaybeUninit::uninit(); + let mut ws_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ed448_init_ex(ws_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ed448_init_ex(ws_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let ws_key = unsafe { ws_key.assume_init() }; let mut ed448 = Ed448 { ws_key }; let rc = unsafe { - ws::wc_ed448_make_key(&mut rng.wc_rng, - ws::ED448_KEY_SIZE as i32, &mut ed448.ws_key) + sys::wc_ed448_make_key(&mut rng.wc_rng, + sys::ED448_KEY_SIZE as i32, &mut ed448.ws_key) }; if rc != 0 { return Err(rc); @@ -166,16 +163,16 @@ impl Ed448 { /// let ed = Ed448::new_ex(None, None).expect("Error with new()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut ws_key: MaybeUninit = MaybeUninit::uninit(); + let mut ws_key: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_ed448_init_ex(ws_key.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_ed448_init_ex(ws_key.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -205,7 +202,7 @@ impl Ed448 { /// ed.check_key().expect("Error with check_key()"); /// ``` pub fn check_key(&mut self) -> Result<(), i32> { - let rc = unsafe { ws::wc_ed448_check_key(&mut self.ws_key) }; + let rc = unsafe { sys::wc_ed448_check_key(&mut self.ws_key) }; if rc != 0 { return Err(rc); } @@ -241,7 +238,7 @@ impl Ed448 { let mut private_size = private.len() as u32; let mut public_size = public.len() as u32; let rc = unsafe { - ws::wc_ed448_export_key(&self.ws_key, + sys::wc_ed448_export_key(&self.ws_key, private.as_mut_ptr(), &mut private_size, public.as_mut_ptr(), &mut public_size) }; @@ -276,7 +273,7 @@ impl Ed448 { pub fn export_public(&self, public: &mut [u8]) -> Result<(), i32> { let mut public_size = public.len() as u32; let rc = unsafe { - ws::wc_ed448_export_public(&self.ws_key, public.as_mut_ptr(), + sys::wc_ed448_export_public(&self.ws_key, public.as_mut_ptr(), &mut public_size) }; if rc != 0 { @@ -310,7 +307,7 @@ impl Ed448 { pub fn export_private(&self, keyout: &mut [u8]) -> Result<(), i32> { let mut keyout_size = keyout.len() as u32; let rc = unsafe { - ws::wc_ed448_export_private(&self.ws_key, keyout.as_mut_ptr(), + sys::wc_ed448_export_private(&self.ws_key, keyout.as_mut_ptr(), &mut keyout_size) }; if rc != 0 { @@ -344,7 +341,7 @@ impl Ed448 { pub fn export_private_only(&self, private: &mut [u8]) -> Result<(), i32> { let mut private_size = private.len() as u32; let rc = unsafe { - ws::wc_ed448_export_private_only(&self.ws_key, + sys::wc_ed448_export_private_only(&self.ws_key, private.as_mut_ptr(), &mut private_size) }; if rc != 0 { @@ -384,7 +381,7 @@ impl Ed448 { pub fn import_public(&mut self, public: &[u8]) -> Result<(), i32> { let public_size = public.len() as u32; let rc = unsafe { - ws::wc_ed448_import_public(public.as_ptr(), public_size, &mut self.ws_key) + sys::wc_ed448_import_public(public.as_ptr(), public_size, &mut self.ws_key) }; if rc != 0 { return Err(rc); @@ -424,7 +421,7 @@ impl Ed448 { pub fn import_public_ex(&mut self, public: &[u8], trusted: bool) -> Result<(), i32> { let public_size = public.len() as u32; let rc = unsafe { - ws::wc_ed448_import_public_ex(public.as_ptr(), public_size, + sys::wc_ed448_import_public_ex(public.as_ptr(), public_size, &mut self.ws_key, if trusted {1} else {0}) }; if rc != 0 { @@ -459,7 +456,7 @@ impl Ed448 { pub fn import_private_only(&mut self, private: &[u8]) -> Result<(), i32> { let private_size = private.len() as u32; let rc = unsafe { - ws::wc_ed448_import_private_only(private.as_ptr(), private_size, + sys::wc_ed448_import_private_only(private.as_ptr(), private_size, &mut self.ws_key) }; if rc != 0 { @@ -506,7 +503,7 @@ impl Ed448 { public_size = public.len() as u32; } let rc = unsafe { - ws::wc_ed448_import_private_key(private.as_ptr(), private_size, + sys::wc_ed448_import_private_key(private.as_ptr(), private_size, public_ptr, public_size, &mut self.ws_key) }; if rc != 0 { @@ -553,7 +550,7 @@ impl Ed448 { public_size = public.len() as u32; } let rc = unsafe { - ws::wc_ed448_import_private_key_ex(private.as_ptr(), private_size, + sys::wc_ed448_import_private_key_ex(private.as_ptr(), private_size, public_ptr, public_size, &mut self.ws_key, if trusted {1} else {0}) }; if rc != 0 { @@ -593,7 +590,7 @@ impl Ed448 { pub fn make_public(&mut self, pubkey: &mut [u8]) -> Result<(), i32> { let pubkey_size = pubkey.len() as u32; let rc = unsafe { - ws::wc_ed448_make_public(&mut self.ws_key, + sys::wc_ed448_make_public(&mut self.ws_key, pubkey.as_mut_ptr(), pubkey_size) }; if rc != 0 { @@ -640,7 +637,7 @@ impl Ed448 { } let mut signature_size = signature.len() as u32; let rc = unsafe { - ws::wc_ed448_sign_msg(message.as_ptr(), message_size, + sys::wc_ed448_sign_msg(message.as_ptr(), message_size, signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key, context_ptr, context_size) }; @@ -698,7 +695,7 @@ impl Ed448 { } let mut signature_size = signature.len() as u32; let rc = unsafe { - ws::wc_ed448ph_sign_hash(hash.as_ptr(), hash_size, + sys::wc_ed448ph_sign_hash(hash.as_ptr(), hash_size, signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key, context_ptr, context_size) }; @@ -747,7 +744,7 @@ impl Ed448 { } let mut signature_size = signature.len() as u32; let rc = unsafe { - ws::wc_ed448ph_sign_msg(message.as_ptr(), message_size, + sys::wc_ed448ph_sign_msg(message.as_ptr(), message_size, signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key, context_ptr, context_size) }; @@ -796,7 +793,7 @@ impl Ed448 { } let mut signature_size = signature.len() as u32; let rc = unsafe { - ws::wc_ed448_sign_msg_ex(din.as_ptr(), din_size, + sys::wc_ed448_sign_msg_ex(din.as_ptr(), din_size, signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key, typ, context_ptr, context_size) }; @@ -846,7 +843,7 @@ impl Ed448 { } let mut res = 0i32; let rc = unsafe { - ws::wc_ed448_verify_msg(signature.as_ptr(), signature_size, + sys::wc_ed448_verify_msg(signature.as_ptr(), signature_size, message.as_ptr(), message_size, &mut res, &mut self.ws_key, context_ptr, context_size) }; @@ -907,7 +904,7 @@ impl Ed448 { } let mut res = 0i32; let rc = unsafe { - ws::wc_ed448ph_verify_hash(signature.as_ptr(), signature_size, + sys::wc_ed448ph_verify_hash(signature.as_ptr(), signature_size, hash.as_ptr(), hash_size, &mut res, &mut self.ws_key, context_ptr, context_size) }; @@ -958,7 +955,7 @@ impl Ed448 { } let mut res = 0i32; let rc = unsafe { - ws::wc_ed448ph_verify_msg(signature.as_ptr(), signature_size, + sys::wc_ed448ph_verify_msg(signature.as_ptr(), signature_size, message.as_ptr(), message_size, &mut res, &mut self.ws_key, context_ptr, context_size) }; @@ -1009,7 +1006,7 @@ impl Ed448 { } let mut res = 0i32; let rc = unsafe { - ws::wc_ed448_verify_msg_ex(signature.as_ptr(), signature_size, + sys::wc_ed448_verify_msg_ex(signature.as_ptr(), signature_size, din.as_ptr(), din_size, &mut res, &mut self.ws_key, typ, context_ptr, context_size) }; @@ -1058,7 +1055,7 @@ impl Ed448 { context_size = context.len() as u8; } let rc = unsafe { - ws::wc_ed448_verify_msg_init(signature.as_ptr(), signature_size, + sys::wc_ed448_verify_msg_init(signature.as_ptr(), signature_size, &mut self.ws_key, typ, context_ptr, context_size) }; if rc != 0 { @@ -1098,7 +1095,7 @@ impl Ed448 { pub fn verify_msg_update(&mut self, din: &[u8]) -> Result<(), i32> { let din_size = din.len() as u32; let rc = unsafe { - ws::wc_ed448_verify_msg_update(din.as_ptr(), din_size, + sys::wc_ed448_verify_msg_update(din.as_ptr(), din_size, &mut self.ws_key) }; if rc != 0 { @@ -1139,7 +1136,7 @@ impl Ed448 { let signature_size = signature.len() as u32; let mut res = 0i32; let rc = unsafe { - ws::wc_ed448_verify_msg_final(signature.as_ptr(), signature_size, + sys::wc_ed448_verify_msg_final(signature.as_ptr(), signature_size, &mut res, &mut self.ws_key) }; if rc != 0 { @@ -1166,7 +1163,7 @@ impl Ed448 { /// assert_eq!(key_size, Ed448::KEY_SIZE); /// ``` pub fn size(&self) -> Result { - let rc = unsafe { ws::wc_ed448_size(&self.ws_key) }; + let rc = unsafe { sys::wc_ed448_size(&self.ws_key) }; if rc < 0 { return Err(rc); } @@ -1191,7 +1188,7 @@ impl Ed448 { /// assert_eq!(priv_size, Ed448::PRV_KEY_SIZE); /// ``` pub fn priv_size(&self) -> Result { - let rc = unsafe { ws::wc_ed448_priv_size(&self.ws_key) }; + let rc = unsafe { sys::wc_ed448_priv_size(&self.ws_key) }; if rc < 0 { return Err(rc); } @@ -1216,7 +1213,7 @@ impl Ed448 { /// assert_eq!(pub_size, Ed448::PUB_KEY_SIZE); /// ``` pub fn pub_size(&self) -> Result { - let rc = unsafe { ws::wc_ed448_pub_size(&self.ws_key) }; + let rc = unsafe { sys::wc_ed448_pub_size(&self.ws_key) }; if rc < 0 { return Err(rc); } @@ -1241,7 +1238,7 @@ impl Ed448 { /// assert_eq!(sig_size, Ed448::SIG_SIZE); /// ``` pub fn sig_size(&self) -> Result { - let rc = unsafe { ws::wc_ed448_sig_size(&self.ws_key) }; + let rc = unsafe { sys::wc_ed448_sig_size(&self.ws_key) }; if rc < 0 { return Err(rc); } @@ -1252,6 +1249,6 @@ impl Ed448 { impl Drop for Ed448 { /// Safely free the wolfSSL resources. fn drop(&mut self) { - unsafe { ws::wc_ed448_free(&mut self.ws_key); } + unsafe { sys::wc_ed448_free(&mut self.ws_key); } } } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/hkdf.rs b/wrapper/rust/wolfssl/src/wolfcrypt/hkdf.rs index ca350abad3..4f7c43e0a8 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/hkdf.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/hkdf.rs @@ -21,13 +21,10 @@ /*! This module provides a Rust wrapper for the wolfCrypt library's HMAC Key Derivation Function (HKDF) functionality. - -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. */ +use crate::sys; use crate::wolfcrypt::hmac::HMAC; -use wolfssl_sys as ws; /// Perform HKDF-Extract operation. /// @@ -102,7 +99,7 @@ pub fn hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: &[u8], out: &mut [u8] } let key_size = key.len() as u32; if out.len() != HMAC::get_hmac_size_by_type(typ)? { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let heap = match heap { Some(heap) => heap, @@ -110,10 +107,10 @@ pub fn hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: &[u8], out: &mut [u8] }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_HKDF_Extract_ex(typ, salt_ptr, salt_size, + sys::wc_HKDF_Extract_ex(typ, salt_ptr, salt_size, key.as_ptr(), key_size, out.as_mut_ptr(), heap, dev_id) }; if rc != 0 { @@ -207,10 +204,10 @@ pub fn hkdf_expand_ex(typ: i32, key: &[u8], info: Option<&[u8]>, out: &mut [u8], }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_HKDF_Expand_ex(typ, key.as_ptr(), key_size, + sys::wc_HKDF_Expand_ex(typ, key.as_ptr(), key_size, info_ptr, info_size, out.as_mut_ptr(), out_size, heap, dev_id) }; if rc != 0 { @@ -266,7 +263,7 @@ pub fn hkdf(typ: i32, key: &[u8], salt: Option<&[u8]>, info: Option<&[u8]>, out: } let out_size = out.len() as u32; let rc = unsafe { - ws::wc_HKDF(typ, key.as_ptr(), key_size, salt_ptr, salt_size, + sys::wc_HKDF(typ, key.as_ptr(), key_size, salt_ptr, salt_size, info_ptr, info_size, out.as_mut_ptr(), out_size) }; if rc != 0 { diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/hmac.rs b/wrapper/rust/wolfssl/src/wolfcrypt/hmac.rs index 89e123e40b..d40c368749 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/hmac.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/hmac.rs @@ -21,33 +21,29 @@ /*! This module provides a Rust wrapper for the wolfCrypt library's HMAC functionality. - -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. */ -use wolfssl_sys as ws; - +use crate::sys; use std::mem::MaybeUninit; /// Rust wrapper for wolfSSL `Hmac` object. pub struct HMAC { - wc_hmac: ws::Hmac, + wc_hmac: sys::Hmac, } impl HMAC { - pub const TYPE_MD5: i32 = ws::wc_HashType_WC_HASH_TYPE_MD5 as i32; - pub const TYPE_SHA: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA as i32; - pub const TYPE_SHA256: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA256 as i32; - pub const TYPE_SHA512: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA512 as i32; - pub const TYPE_SHA512_224: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA512_224 as i32; - pub const TYPE_SHA512_256: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA512_256 as i32; - pub const TYPE_SHA384: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA384 as i32; - pub const TYPE_SHA224: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA224 as i32; - pub const TYPE_SHA3_224: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA3_224 as i32; - pub const TYPE_SHA3_256: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA3_256 as i32; - pub const TYPE_SHA3_384: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA3_384 as i32; - pub const TYPE_SHA3_512: i32 = ws::wc_HashType_WC_HASH_TYPE_SHA3_512 as i32; + pub const TYPE_MD5: i32 = sys::wc_HashType_WC_HASH_TYPE_MD5 as i32; + pub const TYPE_SHA: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA as i32; + pub const TYPE_SHA256: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA256 as i32; + pub const TYPE_SHA512: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA512 as i32; + pub const TYPE_SHA512_224: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA512_224 as i32; + pub const TYPE_SHA512_256: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA512_256 as i32; + pub const TYPE_SHA384: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA384 as i32; + pub const TYPE_SHA224: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA224 as i32; + pub const TYPE_SHA3_224: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_224 as i32; + pub const TYPE_SHA3_256: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_256 as i32; + pub const TYPE_SHA3_384: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_384 as i32; + pub const TYPE_SHA3_512: i32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_512 as i32; /// Get HMAC hash size by type. /// @@ -60,7 +56,7 @@ impl HMAC { /// Returns either Ok(size) containing the HMAC hash size or Err(e) /// containing the wolfSSL library error code value. pub fn get_hmac_size_by_type(typ: i32) -> Result { - let rc = unsafe { ws::wc_HmacSizeByType(typ) }; + let rc = unsafe { sys::wc_HmacSizeByType(typ) }; if rc < 0 { return Err(rc); } @@ -114,17 +110,17 @@ impl HMAC { /// ``` pub fn new_ex(typ: i32, key: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let key_size = key.len() as u32; - let mut wc_hmac: MaybeUninit = MaybeUninit::uninit(); + let mut wc_hmac: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_HmacInit(wc_hmac.as_mut_ptr(), heap, dev_id) + sys::wc_HmacInit(wc_hmac.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); @@ -132,7 +128,7 @@ impl HMAC { let wc_hmac = unsafe { wc_hmac.assume_init() }; let mut hmac = HMAC { wc_hmac }; let rc = unsafe { - ws::wc_HmacSetKey(&mut hmac.wc_hmac, typ, key.as_ptr(), key_size) + sys::wc_HmacSetKey(&mut hmac.wc_hmac, typ, key.as_ptr(), key_size) }; if rc != 0 { return Err(rc); @@ -188,17 +184,17 @@ impl HMAC { /// ``` pub fn new_allow_short_key_ex(typ: i32, key: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let key_size = key.len() as u32; - let mut wc_hmac: MaybeUninit = MaybeUninit::uninit(); + let mut wc_hmac: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_HmacInit(wc_hmac.as_mut_ptr(), heap, dev_id) + sys::wc_HmacInit(wc_hmac.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); @@ -206,7 +202,7 @@ impl HMAC { let wc_hmac = unsafe { wc_hmac.assume_init() }; let mut hmac = HMAC { wc_hmac }; let rc = unsafe { - ws::wc_HmacSetKey_ex(&mut hmac.wc_hmac, typ, key.as_ptr(), key_size, 1) + sys::wc_HmacSetKey_ex(&mut hmac.wc_hmac, typ, key.as_ptr(), key_size, 1) }; if rc != 0 { return Err(rc); @@ -239,7 +235,7 @@ impl HMAC { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_HmacUpdate(&mut self.wc_hmac, data.as_ptr(), data_size) + sys::wc_HmacUpdate(&mut self.wc_hmac, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -274,16 +270,16 @@ impl HMAC { // Check the output buffer size since wc_HmacFinal() does not accept // a length parameter. let typ = self.wc_hmac.macType as i32; - let rc = unsafe { ws::wc_HmacSizeByType(typ) }; + let rc = unsafe { sys::wc_HmacSizeByType(typ) }; if rc < 0 { return Err(rc); } let expected_size = rc as usize; if hash.len() != expected_size { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let rc = unsafe { - ws::wc_HmacFinal(&mut self.wc_hmac, hash.as_mut_ptr()) + sys::wc_HmacFinal(&mut self.wc_hmac, hash.as_mut_ptr()) }; if rc != 0 { return Err(rc); @@ -311,7 +307,7 @@ impl HMAC { /// ``` pub fn get_hmac_size(&self) -> Result { let typ = self.wc_hmac.macType as u32 as i32; - let rc = unsafe { ws::wc_HmacSizeByType(typ) }; + let rc = unsafe { sys::wc_HmacSizeByType(typ) }; if rc < 0 { return Err(rc); } @@ -329,6 +325,6 @@ impl Drop for HMAC { /// HMAC struct instance goes out of scope, automatically cleaning up /// resources and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_HmacFree(&mut self.wc_hmac); } + unsafe { sys::wc_HmacFree(&mut self.wc_hmac); } } } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs b/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs index ceb2a6284c..19f10a18f1 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs @@ -21,22 +21,19 @@ /*! This module provides a Rust wrapper for the wolfCrypt library's Key Derivation Function (KDF) functionality. - -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. */ +use crate::sys; use crate::wolfcrypt::hmac::HMAC; -use wolfssl_sys as ws; -pub const SRTP_LABEL_ENCRYPTION: u8 = ws::WC_SRTP_LABEL_ENCRYPTION as u8; -pub const SRTP_LABEL_MSG_AUTH: u8 = ws::WC_SRTP_LABEL_MSG_AUTH as u8; -pub const SRTP_LABEL_SALT: u8 = ws::WC_SRTP_LABEL_SALT as u8; -pub const SRTCP_LABEL_ENCRYPTION: u8 = ws::WC_SRTCP_LABEL_ENCRYPTION as u8; -pub const SRTCP_LABEL_MSG_AUTH: u8 = ws::WC_SRTCP_LABEL_MSG_AUTH as u8; -pub const SRTCP_LABEL_SALT: u8 = ws::WC_SRTCP_LABEL_SALT as u8; -pub const SRTP_LABEL_HDR_ENCRYPTION: u8 = ws::WC_SRTP_LABEL_HDR_ENCRYPTION as u8; -pub const SRTP_LABEL_HDR_SALT: u8 = ws::WC_SRTP_LABEL_HDR_SALT as u8; +pub const SRTP_LABEL_ENCRYPTION: u8 = sys::WC_SRTP_LABEL_ENCRYPTION as u8; +pub const SRTP_LABEL_MSG_AUTH: u8 = sys::WC_SRTP_LABEL_MSG_AUTH as u8; +pub const SRTP_LABEL_SALT: u8 = sys::WC_SRTP_LABEL_SALT as u8; +pub const SRTCP_LABEL_ENCRYPTION: u8 = sys::WC_SRTCP_LABEL_ENCRYPTION as u8; +pub const SRTCP_LABEL_MSG_AUTH: u8 = sys::WC_SRTCP_LABEL_MSG_AUTH as u8; +pub const SRTCP_LABEL_SALT: u8 = sys::WC_SRTCP_LABEL_SALT as u8; +pub const SRTP_LABEL_HDR_ENCRYPTION: u8 = sys::WC_SRTP_LABEL_HDR_ENCRYPTION as u8; +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 /// input password with a concatenated salt into a more secure key which is @@ -121,10 +118,10 @@ pub fn pbkdf2_ex(password: &[u8], salt: &[u8], iterations: i32, typ: i32, heap: }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_PBKDF2_ex(out.as_mut_ptr(), password.as_ptr(), password_size, + sys::wc_PBKDF2_ex(out.as_mut_ptr(), password.as_ptr(), password_size, salt.as_ptr(), salt_size, iterations, out_size, typ, heap, dev_id) }; if rc != 0 { @@ -234,7 +231,7 @@ pub fn pkcs12_pbkdf_ex(password: &[u8], salt: &[u8], iterations: i32, typ: i32, None => core::ptr::null_mut(), }; let rc = unsafe { - ws::wc_PKCS12_PBKDF_ex(out.as_mut_ptr(), password.as_ptr(), password_size, + sys::wc_PKCS12_PBKDF_ex(out.as_mut_ptr(), password.as_ptr(), password_size, salt.as_ptr(), salt_size, iterations, out_size, typ, id, heap) }; if rc != 0 { @@ -307,7 +304,7 @@ pub fn tls13_hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8 salt_ptr = salt.as_ptr(); salt_size = salt.len() as u32; } - let mut ikm_buf = [0u8; ws::WC_MAX_DIGEST_SIZE as usize]; + let mut ikm_buf = [0u8; sys::WC_MAX_DIGEST_SIZE as usize]; let mut ikm_ptr = ikm_buf.as_mut_ptr(); let mut ikm_size = 0u32; if let Some(key) = key { @@ -317,7 +314,7 @@ pub fn tls13_hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8 } } if out.len() != HMAC::get_hmac_size_by_type(typ)? { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let heap = match heap { Some(heap) => heap, @@ -325,10 +322,10 @@ pub fn tls13_hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8 }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_Tls13_HKDF_Extract_ex(out.as_mut_ptr(), salt_ptr, salt_size, + sys::wc_Tls13_HKDF_Extract_ex(out.as_mut_ptr(), salt_ptr, salt_size, ikm_ptr, ikm_size, typ, heap, dev_id) }; if rc != 0 { @@ -448,10 +445,10 @@ pub fn tls13_hkdf_expand_label_ex(typ: i32, key: &[u8], protocol: &[u8], label: }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_Tls13_HKDF_Expand_Label_ex(out.as_mut_ptr(), out_size, + sys::wc_Tls13_HKDF_Expand_Label_ex(out.as_mut_ptr(), out_size, key.as_ptr(), key_size, protocol.as_ptr(), protocol_size, label.as_ptr(), label_size, info.as_ptr(), info_size, typ, heap, dev_id) @@ -495,7 +492,7 @@ pub fn ssh_kdf(typ: i32, key_id: u8, k: &[u8], h: &[u8], session_id: &[u8], key: let h_size = h.len() as u32; let session_size = session_id.len() as u32; let rc = unsafe { - ws::wc_SSH_KDF(typ as u8, key_id, + sys::wc_SSH_KDF(typ as u8, key_id, key.as_mut_ptr(), key_size, k.as_ptr(), k_size, h.as_ptr(), h_size, session_id.as_ptr(), session_size) @@ -545,7 +542,7 @@ pub fn srtp_kdf(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], let key2_size = key2.len() as u32; let key3_size = key3.len() as u32; let rc = unsafe { - ws::wc_SRTP_KDF(key.as_ptr(), key_size, salt.as_ptr(), salt_size, + sys::wc_SRTP_KDF(key.as_ptr(), key_size, salt.as_ptr(), salt_size, kdr_index, idx.as_ptr(), key1.as_mut_ptr(), key1_size, key2.as_mut_ptr(), key2_size, key3.as_mut_ptr(), key3_size) }; @@ -589,7 +586,7 @@ pub fn srtp_kdf_label(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], let salt_size = salt.len() as u32; let keyout_size = keyout.len() as u32; let rc = unsafe { - ws::wc_SRTP_KDF_label(key.as_ptr(), key_size, salt.as_ptr(), salt_size, + sys::wc_SRTP_KDF_label(key.as_ptr(), key_size, salt.as_ptr(), salt_size, kdr_index, idx.as_ptr(), label, keyout.as_mut_ptr(), keyout_size) }; if rc != 0 { @@ -637,7 +634,7 @@ pub fn srtcp_kdf(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], let key2_size = key2.len() as u32; let key3_size = key3.len() as u32; let rc = unsafe { - ws::wc_SRTCP_KDF(key.as_ptr(), key_size, salt.as_ptr(), salt_size, + sys::wc_SRTCP_KDF(key.as_ptr(), key_size, salt.as_ptr(), salt_size, kdr_index, idx.as_ptr(), key1.as_mut_ptr(), key1_size, key2.as_mut_ptr(), key2_size, key3.as_mut_ptr(), key3_size) }; @@ -681,7 +678,7 @@ pub fn srtcp_kdf_label(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], let salt_size = salt.len() as u32; let keyout_size = keyout.len() as u32; let rc = unsafe { - ws::wc_SRTCP_KDF_label(key.as_ptr(), key_size, salt.as_ptr(), salt_size, + sys::wc_SRTCP_KDF_label(key.as_ptr(), key_size, salt.as_ptr(), salt_size, kdr_index, idx.as_ptr(), label, keyout.as_mut_ptr(), keyout_size) }; if rc != 0 { @@ -708,5 +705,5 @@ pub fn srtcp_kdf_label(key: &[u8], salt: &[u8], kdr_index: i32, idx: &[u8], /// let kdr_index = srtp_kdr_to_index(16); /// ``` pub fn srtp_kdr_to_index(kdr: u32) -> i32 { - unsafe { ws::wc_SRTP_KDF_kdr_to_idx(kdr) } + unsafe { sys::wc_SRTP_KDF_kdr_to_idx(kdr) } } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/prf.rs b/wrapper/rust/wolfssl/src/wolfcrypt/prf.rs index b93ee0509b..9fbd1af611 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/prf.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/prf.rs @@ -21,23 +21,20 @@ /*! This module provides a Rust wrapper for the wolfCrypt library's Pseudo Random Function (PRF) functionality. - -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. */ -use wolfssl_sys as ws; +use crate::sys; -pub const PRF_HASH_NONE: i32 = ws::wc_MACAlgorithm_no_mac as i32; -pub const PRF_HASH_MD5: i32 = ws::wc_MACAlgorithm_md5_mac as i32; -pub const PRF_HASH_SHA: i32 = ws::wc_MACAlgorithm_sha_mac as i32; -pub const PRF_HASH_SHA224: i32 = ws::wc_MACAlgorithm_sha224_mac as i32; -pub const PRF_HASH_SHA256: i32 = ws::wc_MACAlgorithm_sha256_mac as i32; -pub const PRF_HASH_SHA384: i32 = ws::wc_MACAlgorithm_sha384_mac as i32; -pub const PRF_HASH_SHA512: i32 = ws::wc_MACAlgorithm_sha512_mac as i32; -pub const PRF_HASH_RMD: i32 = ws::wc_MACAlgorithm_rmd_mac as i32; -pub const PRF_HASH_BLAKE2B: i32 = ws::wc_MACAlgorithm_blake2b_mac as i32; -pub const PRF_HASH_SM3: i32 = ws::wc_MACAlgorithm_sm3_mac as i32; +pub const PRF_HASH_NONE: i32 = sys::wc_MACAlgorithm_no_mac as i32; +pub const PRF_HASH_MD5: i32 = sys::wc_MACAlgorithm_md5_mac as i32; +pub const PRF_HASH_SHA: i32 = sys::wc_MACAlgorithm_sha_mac as i32; +pub const PRF_HASH_SHA224: i32 = sys::wc_MACAlgorithm_sha224_mac as i32; +pub const PRF_HASH_SHA256: i32 = sys::wc_MACAlgorithm_sha256_mac as i32; +pub const PRF_HASH_SHA384: i32 = sys::wc_MACAlgorithm_sha384_mac as i32; +pub const PRF_HASH_SHA512: i32 = sys::wc_MACAlgorithm_sha512_mac as i32; +pub const PRF_HASH_RMD: i32 = sys::wc_MACAlgorithm_rmd_mac as i32; +pub const PRF_HASH_BLAKE2B: i32 = sys::wc_MACAlgorithm_blake2b_mac as i32; +pub const PRF_HASH_SM3: i32 = sys::wc_MACAlgorithm_sm3_mac as i32; /// Pseudo Random Function for MD5, SHA-1, SHA-256, SHA-384, or SHA-512. /// @@ -57,7 +54,6 @@ pub const PRF_HASH_SM3: i32 = ws::wc_MACAlgorithm_sm3_mac as i32; /// /// ```rust /// use wolfssl::wolfcrypt::prf::*; -/// use wolfssl_sys as ws; /// let secret = [0x10u8, 0xbc, 0xb4, 0xa2, 0xe8, 0xdc, 0xf1, 0x9b, 0x4c, /// 0x51, 0x9c, 0xed, 0x31, 0x1b, 0x51, 0x57, 0x02, 0x3f, /// 0xa1, 0x7d, 0xfb, 0x0e, 0xf3, 0x4e, 0x8f, 0x6f, 0x71, @@ -99,7 +95,6 @@ pub fn prf(secret: &[u8], seed: &[u8], hash_type: i32, dout: &mut [u8]) -> Resul /// /// ```rust /// use wolfssl::wolfcrypt::prf::*; -/// use wolfssl_sys as ws; /// let secret = [0x10u8, 0xbc, 0xb4, 0xa2, 0xe8, 0xdc, 0xf1, 0x9b, 0x4c, /// 0x51, 0x9c, 0xed, 0x31, 0x1b, 0x51, 0x57, 0x02, 0x3f, /// 0xa1, 0x7d, 0xfb, 0x0e, 0xf3, 0x4e, 0x8f, 0x6f, 0x71, @@ -126,10 +121,10 @@ pub fn prf_ex(secret: &[u8], seed: &[u8], hash_type: i32, heap: Option<*mut ::st }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_PRF(dout.as_mut_ptr(), dout_size, + sys::wc_PRF(dout.as_mut_ptr(), dout_size, secret.as_ptr(), secret_size, seed.as_ptr(), seed_size, hash_type, heap, dev_id) diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/random.rs b/wrapper/rust/wolfssl/src/wolfcrypt/random.rs index eb128cba75..b3dafe5264 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/random.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/random.rs @@ -22,9 +22,6 @@ This module provides a Rust wrapper for the wolfCrypt library's random number generator (RNG). -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. - The primary component is the `RNG` struct, which manages the lifecycle of a wolfSSL `WC_RNG` object. It ensures proper initialization and deallocation. @@ -46,8 +43,8 @@ fn main() { } ``` */ -use wolfssl_sys as ws; +use crate::sys; use std::mem::{size_of, MaybeUninit}; /// A cryptographically secure random number generator based on the wolfSSL @@ -58,7 +55,7 @@ use std::mem::{size_of, MaybeUninit}; /// ensures that the underlying wolfSSL RNG context is correctly freed when the /// `RNG` struct goes out of scope, preventing memory leaks. pub struct RNG { - pub(crate) wc_rng: ws::WC_RNG, + pub(crate) wc_rng: sys::WC_RNG, } impl RNG { @@ -97,10 +94,10 @@ impl RNG { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_InitRng_ex(&mut (*rng.as_mut_ptr()).wc_rng, heap, dev_id) + sys::wc_InitRng_ex(&mut (*rng.as_mut_ptr()).wc_rng, heap, dev_id) }; if rc == 0 { let rng = unsafe { rng.assume_init() }; @@ -149,10 +146,10 @@ impl RNG { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_InitRngNonce_ex(&mut (*rng.as_mut_ptr()).wc_rng, ptr, size, heap, dev_id) + sys::wc_InitRngNonce_ex(&mut (*rng.as_mut_ptr()).wc_rng, ptr, size, heap, dev_id) }; if rc == 0 { let rng = unsafe { rng.assume_init() }; @@ -173,7 +170,7 @@ impl RNG { /// an `Err` with the wolfssl library return code on failure. pub fn generate_byte(&mut self) -> Result { let mut b: u8 = 0; - let rc = unsafe { ws::wc_RNG_GenerateByte(&mut self.wc_rng, &mut b) }; + let rc = unsafe { sys::wc_RNG_GenerateByte(&mut self.wc_rng, &mut b) }; if rc == 0 { Ok(b) } else { @@ -198,7 +195,7 @@ impl RNG { pub fn generate_block(&mut self, buf: &mut [T]) -> Result<(), i32> { let ptr = buf.as_mut_ptr() as *mut u8; let size: u32 = (buf.len() * size_of::()) as u32; - let rc = unsafe { ws::wc_RNG_GenerateBlock(&mut self.wc_rng, ptr, size) }; + let rc = unsafe { sys::wc_RNG_GenerateBlock(&mut self.wc_rng, ptr, size) }; if rc == 0 { Ok(()) } else { @@ -216,6 +213,6 @@ impl Drop for RNG { /// struct goes out of scope, automatically cleaning up resources and /// preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_FreeRng(&mut self.wc_rng); } + unsafe { sys::wc_FreeRng(&mut self.wc_rng); } } } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs b/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs index 43ea0224ca..4a1830db42 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs @@ -22,9 +22,6 @@ This module provides a Rust wrapper for the wolfCrypt library's RSA functionality. -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. - The primary component is the `RSA` struct, which manages the lifecycle of a wolfSSL `RsaKey` object. It ensures proper initialization and deallocation. @@ -56,10 +53,9 @@ assert_eq!(plain_out[0..dec_len], *plain); ``` */ -use wolfssl_sys as ws; - -use std::mem::{MaybeUninit}; +use crate::sys; use crate::wolfcrypt::random::RNG; +use std::mem::{MaybeUninit}; /// The `RSA` struct manages the lifecycle of a wolfSSL `RsaKey` object. /// @@ -68,47 +64,47 @@ use crate::wolfcrypt::random::RNG; /// An instance can be created with `new_from_der()`, `new_public_from_der()`, /// or `generate()`. pub struct RSA { - wc_rsakey: ws::RsaKey, + wc_rsakey: sys::RsaKey, } impl RSA { // Hash type constants used for PSS sign and verify methods. - pub const HASH_TYPE_NONE : u32 = ws::wc_HashType_WC_HASH_TYPE_NONE; - pub const HASH_TYPE_MD2 : u32 = ws::wc_HashType_WC_HASH_TYPE_MD2; - pub const HASH_TYPE_MD4 : u32 = ws::wc_HashType_WC_HASH_TYPE_MD4; - pub const HASH_TYPE_MD5 : u32 = ws::wc_HashType_WC_HASH_TYPE_MD5; - pub const HASH_TYPE_SHA : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA; - pub const HASH_TYPE_SHA224 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA224; - pub const HASH_TYPE_SHA256 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA256; - pub const HASH_TYPE_SHA384 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA384; - pub const HASH_TYPE_SHA512 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA512; - pub const HASH_TYPE_MD5_SHA : u32 = ws::wc_HashType_WC_HASH_TYPE_MD5_SHA; - pub const HASH_TYPE_SHA3_224 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA3_224; - pub const HASH_TYPE_SHA3_256 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA3_256; - pub const HASH_TYPE_SHA3_384 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA3_384; - pub const HASH_TYPE_SHA3_512 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA3_512; - pub const HASH_TYPE_BLAKE2B : u32 = ws::wc_HashType_WC_HASH_TYPE_BLAKE2B; - pub const HASH_TYPE_BLAKE2S : u32 = ws::wc_HashType_WC_HASH_TYPE_BLAKE2S; - pub const HASH_TYPE_SHA512_224 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA512_224; - pub const HASH_TYPE_SHA512_256 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHA512_256; - pub const HASH_TYPE_SHAKE128 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHAKE128; - pub const HASH_TYPE_SHAKE256 : u32 = ws::wc_HashType_WC_HASH_TYPE_SHAKE256; + pub const HASH_TYPE_NONE : u32 = sys::wc_HashType_WC_HASH_TYPE_NONE; + 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; + pub const HASH_TYPE_SHA : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA; + pub const HASH_TYPE_SHA224 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA224; + pub const HASH_TYPE_SHA256 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA256; + pub const HASH_TYPE_SHA384 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA384; + 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; + pub const HASH_TYPE_SHA3_224 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_224; + pub const HASH_TYPE_SHA3_256 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_256; + pub const HASH_TYPE_SHA3_384 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHA3_384; + 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; + pub const HASH_TYPE_SHAKE128 : u32 = sys::wc_HashType_WC_HASH_TYPE_SHAKE128; + 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. - pub const MGF1NONE : i32 = ws::WC_MGF1NONE as i32; - pub const MGF1SHA1 : i32 = ws::WC_MGF1SHA1 as i32; - pub const MGF1SHA224 : i32 = ws::WC_MGF1SHA224 as i32; - pub const MGF1SHA256 : i32 = ws::WC_MGF1SHA256 as i32; - pub const MGF1SHA384 : i32 = ws::WC_MGF1SHA384 as i32; - pub const MGF1SHA512 : i32 = ws::WC_MGF1SHA512 as i32; - pub const MGF1SHA512_224 : i32 = ws::WC_MGF1SHA512_224 as i32; - pub const MGF1SHA512_256 : i32 = ws::WC_MGF1SHA512_256 as i32; + pub const MGF1NONE : i32 = sys::WC_MGF1NONE as i32; + pub const MGF1SHA1 : i32 = sys::WC_MGF1SHA1 as i32; + pub const MGF1SHA224 : i32 = sys::WC_MGF1SHA224 as i32; + pub const MGF1SHA256 : i32 = sys::WC_MGF1SHA256 as i32; + pub const MGF1SHA384 : i32 = sys::WC_MGF1SHA384 as i32; + pub const MGF1SHA512 : i32 = sys::WC_MGF1SHA512 as i32; + pub const MGF1SHA512_224 : i32 = sys::WC_MGF1SHA512_224 as i32; + pub const MGF1SHA512_256 : i32 = sys::WC_MGF1SHA512_256 as i32; // Type constants used for `rsa_direct()`. - pub const PUBLIC_ENCRYPT : i32 = ws::RSA_PUBLIC_ENCRYPT; - pub const PUBLIC_DECRYPT : i32 = ws::RSA_PUBLIC_DECRYPT; - pub const PRIVATE_ENCRYPT : i32 = ws::RSA_PRIVATE_ENCRYPT; - pub const PRIVATE_DECRYPT : i32 = ws::RSA_PRIVATE_DECRYPT; + pub const PUBLIC_ENCRYPT : i32 = sys::RSA_PUBLIC_ENCRYPT; + pub const PUBLIC_DECRYPT : i32 = sys::RSA_PUBLIC_DECRYPT; + pub const PRIVATE_ENCRYPT : i32 = sys::RSA_PRIVATE_ENCRYPT; + pub const PRIVATE_DECRYPT : i32 = sys::RSA_PRIVATE_DECRYPT; /// Load a public and private RSA keypair from DER-encoded buffer. /// @@ -192,16 +188,16 @@ impl RSA { /// assert_eq!(plain_out[0..dec_len], *plain); /// ``` pub fn new_from_der_ex(der: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_rsakey: MaybeUninit = MaybeUninit::uninit(); + let mut wc_rsakey: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitRsaKey_ex(wc_rsakey.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitRsaKey_ex(wc_rsakey.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -210,10 +206,10 @@ impl RSA { let der_size = der.len() as u32; let mut idx: u32 = 0; let rc = unsafe { - ws::wc_RsaPrivateKeyDecode(der_ptr, &mut idx, &mut wc_rsakey, der_size) + sys::wc_RsaPrivateKeyDecode(der_ptr, &mut idx, &mut wc_rsakey, der_size) }; if rc != 0 { - unsafe { ws::wc_FreeRsaKey(&mut wc_rsakey); } + unsafe { sys::wc_FreeRsaKey(&mut wc_rsakey); } return Err(rc); } let rsa = RSA { wc_rsakey }; @@ -302,16 +298,16 @@ impl RSA { /// assert_eq!(plain_out[0..dec_len], *plain); /// ``` pub fn new_public_from_der_ex(der: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_rsakey: MaybeUninit = MaybeUninit::uninit(); + let mut wc_rsakey: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitRsaKey_ex(wc_rsakey.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitRsaKey_ex(wc_rsakey.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -320,10 +316,10 @@ impl RSA { let der_size = der.len() as u32; let mut idx: u32 = 0; let rc = unsafe { - ws::wc_RsaPublicKeyDecode(der_ptr, &mut idx, &mut wc_rsakey, der_size) + sys::wc_RsaPublicKeyDecode(der_ptr, &mut idx, &mut wc_rsakey, der_size) }; if rc != 0 { - unsafe { ws::wc_FreeRsaKey(&mut wc_rsakey); } + unsafe { sys::wc_FreeRsaKey(&mut wc_rsakey); } return Err(rc); } let rsa = RSA { wc_rsakey }; @@ -410,25 +406,25 @@ impl RSA { /// assert_eq!(encrypt_size, 256); /// ``` 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 mut wc_rsakey: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitRsaKey_ex(wc_rsakey.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitRsaKey_ex(wc_rsakey.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } let mut wc_rsakey = unsafe { wc_rsakey.assume_init() }; let rc = unsafe { - ws::wc_MakeRsaKey(&mut wc_rsakey, size, e, &mut rng.wc_rng) + sys::wc_MakeRsaKey(&mut wc_rsakey, size, e, &mut rng.wc_rng) }; if rc != 0 { - unsafe { ws::wc_FreeRsaKey(&mut wc_rsakey); } + unsafe { sys::wc_FreeRsaKey(&mut wc_rsakey); } return Err(rc); } let rsa = RSA { wc_rsakey }; @@ -493,7 +489,7 @@ impl RSA { let q_ptr = q.as_ptr() as *mut u8; *q_size = q.len() as u32; let rc = unsafe { - ws::wc_RsaExportKey(&mut self.wc_rsakey, e_ptr, e_size, + sys::wc_RsaExportKey(&mut self.wc_rsakey, e_ptr, e_size, n_ptr, n_size, d_ptr, d_size, p_ptr, p_size, q_ptr, q_size) }; if rc != 0 { @@ -538,7 +534,7 @@ impl RSA { let n_ptr = n.as_ptr() as *mut u8; *n_size = n.len() as u32; let rc = unsafe { - ws::wc_RsaFlattenPublicKey(&mut self.wc_rsakey, e_ptr, e_size, + sys::wc_RsaFlattenPublicKey(&mut self.wc_rsakey, e_ptr, e_size, n_ptr, n_size) }; if rc != 0 { @@ -566,7 +562,7 @@ impl RSA { /// assert_eq!(encrypt_size, 256); /// ``` pub fn get_encrypt_size(&self) -> Result { - let rc = unsafe { ws::wc_RsaEncryptSize(&self.wc_rsakey) }; + let rc = unsafe { sys::wc_RsaEncryptSize(&self.wc_rsakey) }; if rc < 0 { return Err(rc); } @@ -591,7 +587,7 @@ impl RSA { /// rsa.check().expect("Error with check()"); /// ``` pub fn check(&mut self) -> Result<(), i32> { - let rc = unsafe { ws::wc_CheckRsaKey(&mut self.wc_rsakey) }; + let rc = unsafe { sys::wc_CheckRsaKey(&mut self.wc_rsakey) }; if rc != 0 { return Err(rc); } @@ -646,7 +642,7 @@ impl RSA { let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_RsaPublicEncrypt(din_ptr, din_size, dout_ptr, dout_size, + sys::wc_RsaPublicEncrypt(din_ptr, din_size, dout_ptr, dout_size, &mut self.wc_rsakey, &mut rng.wc_rng) }; if rc < 0 { @@ -701,7 +697,7 @@ impl RSA { let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_RsaPrivateDecrypt(din_ptr, din_size, dout_ptr, dout_size, + sys::wc_RsaPrivateDecrypt(din_ptr, din_size, dout_ptr, dout_size, &mut self.wc_rsakey) }; if rc < 0 { @@ -765,7 +761,7 @@ impl RSA { let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_RsaPSS_Sign(din_ptr, din_size, dout_ptr, dout_size, + sys::wc_RsaPSS_Sign(din_ptr, din_size, dout_ptr, dout_size, hash_algo, mgf, &mut self.wc_rsakey, &mut rng.wc_rng) }; if rc < 0 { @@ -826,7 +822,7 @@ impl RSA { let sig_ptr = sig.as_ptr() as *const u8; let sig_size = sig.len() as u32; let rc = unsafe { - ws::wc_RsaPSS_CheckPadding(din_ptr, din_size, sig_ptr, sig_size, + sys::wc_RsaPSS_CheckPadding(din_ptr, din_size, sig_ptr, sig_size, hash_algo) }; if rc != 0 { @@ -890,7 +886,7 @@ impl RSA { let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_RsaPSS_Verify(din_ptr, din_size, dout_ptr, dout_size, + sys::wc_RsaPSS_Verify(din_ptr, din_size, dout_ptr, dout_size, hash_algo, mgf, &mut self.wc_rsakey) }; if rc < 0 { @@ -960,7 +956,7 @@ impl RSA { let digest_ptr = digest.as_ptr() as *const u8; let digest_size = digest.len() as u32; let rc = unsafe { - ws::wc_RsaPSS_VerifyCheck(din_ptr, din_size, dout_ptr, dout_size, + sys::wc_RsaPSS_VerifyCheck(din_ptr, din_size, dout_ptr, dout_size, digest_ptr, digest_size, hash_algo, mgf, &mut self.wc_rsakey) }; if rc < 0 { @@ -1019,7 +1015,7 @@ impl RSA { let dout_ptr = dout.as_ptr() as *mut u8; let mut dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_RsaDirect(din_ptr, din_size, dout_ptr, &mut dout_size, + sys::wc_RsaDirect(din_ptr, din_size, dout_ptr, &mut dout_size, &mut self.wc_rsakey, typ, &mut rng.wc_rng) }; if rc < 0 { @@ -1072,7 +1068,7 @@ impl RSA { /// ``` pub fn set_rng(&mut self, rng: &mut RNG) -> Result<(), i32> { let rc = unsafe { - ws::wc_RsaSetRNG(&mut self.wc_rsakey, &mut rng.wc_rng) + sys::wc_RsaSetRNG(&mut self.wc_rsakey, &mut rng.wc_rng) }; if rc != 0 { return Err(rc); @@ -1128,7 +1124,7 @@ impl RSA { let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_RsaSSL_Sign(din_ptr, din_size, dout_ptr, dout_size, + sys::wc_RsaSSL_Sign(din_ptr, din_size, dout_ptr, dout_size, &mut self.wc_rsakey, &mut rng.wc_rng) }; if rc < 0 { @@ -1186,7 +1182,7 @@ impl RSA { let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - ws::wc_RsaSSL_Verify(din_ptr, din_size, dout_ptr, dout_size, + sys::wc_RsaSSL_Verify(din_ptr, din_size, dout_ptr, dout_size, &mut self.wc_rsakey) }; if rc < 0 { @@ -1205,6 +1201,6 @@ impl Drop for RSA { /// struct goes out of scope, automatically cleaning up resources and /// preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_FreeRsaKey(&mut self.wc_rsakey); } + unsafe { sys::wc_FreeRsaKey(&mut self.wc_rsakey); } } } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs b/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs index 239e8e331f..174ad11c5f 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs @@ -21,23 +21,19 @@ /*! This module provides a Rust wrapper for the wolfCrypt library's Secure Hash Algorithm (SHA) functionality. - -It leverages the `wolfssl-sys` crate for low-level FFI bindings, encapsulating -the raw C functions in a memory-safe and easy-to-use Rust API. */ -use wolfssl_sys as ws; - +use crate::sys; use std::mem::MaybeUninit; /// Context for SHA-1 computation. pub struct SHA { - wc_sha: ws::wc_Sha, + wc_sha: sys::wc_Sha, } impl SHA { /// SHA-1 digest size in bytes. - pub const DIGEST_SIZE: usize = ws::WC_SHA_DIGEST_SIZE as usize; + pub const DIGEST_SIZE: usize = sys::WC_SHA_DIGEST_SIZE as usize; /// Build a new SHA instance. /// @@ -75,16 +71,16 @@ impl SHA { /// let sha = SHA::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_sha: MaybeUninit = MaybeUninit::uninit(); + let mut wc_sha: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha_ex(wc_sha.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha_ex(wc_sha.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -144,9 +140,9 @@ impl SHA { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha_ex(&mut self.wc_sha, heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha_ex(&mut self.wc_sha, heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -174,7 +170,7 @@ impl SHA { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_ShaUpdate(&mut self.wc_sha, data.as_ptr(), data_size) + sys::wc_ShaUpdate(&mut self.wc_sha, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -205,10 +201,10 @@ impl SHA { /// ``` pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { if hash.len() != Self::DIGEST_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let rc = unsafe { - ws::wc_ShaFinal(&mut self.wc_sha, hash.as_mut_ptr()) + sys::wc_ShaFinal(&mut self.wc_sha, hash.as_mut_ptr()) }; if rc != 0 { return Err(rc); @@ -226,18 +222,18 @@ impl Drop for SHA { /// struct goes out of scope, automatically cleaning up resources and /// preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_ShaFree(&mut self.wc_sha); } + unsafe { sys::wc_ShaFree(&mut self.wc_sha); } } } /// Context for SHA-224 (SHA-2) computation. pub struct SHA224 { - wc_sha224: ws::wc_Sha224, + wc_sha224: sys::wc_Sha224, } impl SHA224 { /// SHA-224 digest size in bytes. - pub const DIGEST_SIZE: usize = ws::WC_SHA224_DIGEST_SIZE as usize; + pub const DIGEST_SIZE: usize = sys::WC_SHA224_DIGEST_SIZE as usize; /// Build a new SHA224 instance. /// @@ -275,16 +271,16 @@ impl SHA224 { /// let sha = SHA224::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_sha224: MaybeUninit = MaybeUninit::uninit(); + let mut wc_sha224: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha224_ex(wc_sha224.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha224_ex(wc_sha224.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -344,9 +340,9 @@ impl SHA224 { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha224_ex(&mut self.wc_sha224, heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha224_ex(&mut self.wc_sha224, heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -374,7 +370,7 @@ impl SHA224 { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Sha224Update(&mut self.wc_sha224, data.as_ptr(), data_size) + sys::wc_Sha224Update(&mut self.wc_sha224, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -405,10 +401,10 @@ impl SHA224 { /// ``` pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { if hash.len() != Self::DIGEST_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let rc = unsafe { - ws::wc_Sha224Final(&mut self.wc_sha224, hash.as_mut_ptr()) + sys::wc_Sha224Final(&mut self.wc_sha224, hash.as_mut_ptr()) }; if rc != 0 { return Err(rc); @@ -426,18 +422,18 @@ impl Drop for SHA224 { /// SHA224 struct goes out of scope, automatically cleaning up resources /// and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_Sha224Free(&mut self.wc_sha224); } + unsafe { sys::wc_Sha224Free(&mut self.wc_sha224); } } } /// Context for SHA-256 (SHA-2) computation. pub struct SHA256 { - wc_sha256: ws::wc_Sha256, + wc_sha256: sys::wc_Sha256, } impl SHA256 { /// SHA-256 digest size in bytes. - pub const DIGEST_SIZE: usize = ws::WC_SHA256_DIGEST_SIZE as usize; + pub const DIGEST_SIZE: usize = sys::WC_SHA256_DIGEST_SIZE as usize; /// Build a new SHA256 instance. /// @@ -475,16 +471,16 @@ impl SHA256 { /// let sha = SHA256::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_sha256: MaybeUninit = MaybeUninit::uninit(); + let mut wc_sha256: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha256_ex(wc_sha256.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha256_ex(wc_sha256.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -544,9 +540,9 @@ impl SHA256 { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha256_ex(&mut self.wc_sha256, heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha256_ex(&mut self.wc_sha256, heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -574,7 +570,7 @@ impl SHA256 { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Sha256Update(&mut self.wc_sha256, data.as_ptr(), data_size) + sys::wc_Sha256Update(&mut self.wc_sha256, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -605,10 +601,10 @@ impl SHA256 { /// ``` pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { if hash.len() != Self::DIGEST_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let rc = unsafe { - ws::wc_Sha256Final(&mut self.wc_sha256, hash.as_mut_ptr()) + sys::wc_Sha256Final(&mut self.wc_sha256, hash.as_mut_ptr()) }; if rc != 0 { return Err(rc); @@ -626,18 +622,18 @@ impl Drop for SHA256 { /// SHA256 struct goes out of scope, automatically cleaning up resources /// and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_Sha256Free(&mut self.wc_sha256); } + unsafe { sys::wc_Sha256Free(&mut self.wc_sha256); } } } /// Context for SHA-384 (SHA-2) computation. pub struct SHA384 { - wc_sha384: ws::wc_Sha384, + wc_sha384: sys::wc_Sha384, } impl SHA384 { /// SHA-384 digest size in bytes. - pub const DIGEST_SIZE: usize = ws::WC_SHA384_DIGEST_SIZE as usize; + pub const DIGEST_SIZE: usize = sys::WC_SHA384_DIGEST_SIZE as usize; /// Build a new SHA384 instance. /// @@ -675,16 +671,16 @@ impl SHA384 { /// let sha = SHA384::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_sha384: MaybeUninit = MaybeUninit::uninit(); + let mut wc_sha384: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha384_ex(wc_sha384.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha384_ex(wc_sha384.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -744,9 +740,9 @@ impl SHA384 { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha384_ex(&mut self.wc_sha384, heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha384_ex(&mut self.wc_sha384, heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -774,7 +770,7 @@ impl SHA384 { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Sha384Update(&mut self.wc_sha384, data.as_ptr(), data_size) + sys::wc_Sha384Update(&mut self.wc_sha384, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -805,10 +801,10 @@ impl SHA384 { /// ``` pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { if hash.len() != Self::DIGEST_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let rc = unsafe { - ws::wc_Sha384Final(&mut self.wc_sha384, hash.as_mut_ptr()) + sys::wc_Sha384Final(&mut self.wc_sha384, hash.as_mut_ptr()) }; if rc != 0 { return Err(rc); @@ -826,18 +822,18 @@ impl Drop for SHA384 { /// SHA384 struct goes out of scope, automatically cleaning up resources /// and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_Sha384Free(&mut self.wc_sha384); } + unsafe { sys::wc_Sha384Free(&mut self.wc_sha384); } } } /// Context for SHA-512 (SHA-2) computation. pub struct SHA512 { - wc_sha512: ws::wc_Sha512, + wc_sha512: sys::wc_Sha512, } impl SHA512 { /// SHA-512 digest size in bytes. - pub const DIGEST_SIZE: usize = ws::WC_SHA512_DIGEST_SIZE as usize; + pub const DIGEST_SIZE: usize = sys::WC_SHA512_DIGEST_SIZE as usize; /// Build a new SHA512 instance. /// @@ -875,16 +871,16 @@ impl SHA512 { /// let sha = SHA512::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_sha512: MaybeUninit = MaybeUninit::uninit(); + let mut wc_sha512: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha512_ex(wc_sha512.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha512_ex(wc_sha512.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -944,9 +940,9 @@ impl SHA512 { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha512_ex(&mut self.wc_sha512, heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha512_ex(&mut self.wc_sha512, heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -974,7 +970,7 @@ impl SHA512 { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Sha512Update(&mut self.wc_sha512, data.as_ptr(), data_size) + sys::wc_Sha512Update(&mut self.wc_sha512, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -1005,10 +1001,10 @@ impl SHA512 { /// ``` pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { if hash.len() != Self::DIGEST_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let rc = unsafe { - ws::wc_Sha512Final(&mut self.wc_sha512, hash.as_mut_ptr()) + sys::wc_Sha512Final(&mut self.wc_sha512, hash.as_mut_ptr()) }; if rc != 0 { return Err(rc); @@ -1026,18 +1022,18 @@ impl Drop for SHA512 { /// SHA512 struct goes out of scope, automatically cleaning up resources /// and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_Sha512Free(&mut self.wc_sha512); } + unsafe { sys::wc_Sha512Free(&mut self.wc_sha512); } } } /// Context for SHA3-224 computation. pub struct SHA3_224 { - wc_sha3: ws::wc_Sha3, + wc_sha3: sys::wc_Sha3, } impl SHA3_224 { /// SHA3-224 digest size in bytes. - pub const DIGEST_SIZE: usize = ws::WC_SHA3_224_DIGEST_SIZE as usize; + pub const DIGEST_SIZE: usize = sys::WC_SHA3_224_DIGEST_SIZE as usize; /// Build a new SHA3_224 instance. /// @@ -1075,16 +1071,16 @@ impl SHA3_224 { /// let sha = SHA3_224::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_sha3: MaybeUninit = MaybeUninit::uninit(); + let mut wc_sha3: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha3_224(wc_sha3.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha3_224(wc_sha3.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -1144,9 +1140,9 @@ impl SHA3_224 { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha3_224(&mut self.wc_sha3, heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha3_224(&mut self.wc_sha3, heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -1174,7 +1170,7 @@ impl SHA3_224 { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Sha3_224_Update(&mut self.wc_sha3, data.as_ptr(), data_size) + sys::wc_Sha3_224_Update(&mut self.wc_sha3, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -1205,10 +1201,10 @@ impl SHA3_224 { /// ``` pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { if hash.len() != Self::DIGEST_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let rc = unsafe { - ws::wc_Sha3_224_Final(&mut self.wc_sha3, hash.as_mut_ptr()) + sys::wc_Sha3_224_Final(&mut self.wc_sha3, hash.as_mut_ptr()) }; if rc != 0 { return Err(rc); @@ -1226,18 +1222,18 @@ impl Drop for SHA3_224 { /// SHA3_224 struct goes out of scope, automatically cleaning up resources /// and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_Sha3_224_Free(&mut self.wc_sha3); } + unsafe { sys::wc_Sha3_224_Free(&mut self.wc_sha3); } } } /// Context for SHA3-256 computation. pub struct SHA3_256 { - wc_sha3: ws::wc_Sha3, + wc_sha3: sys::wc_Sha3, } impl SHA3_256 { /// SHA3-256 digest size in bytes. - pub const DIGEST_SIZE: usize = ws::WC_SHA3_256_DIGEST_SIZE as usize; + pub const DIGEST_SIZE: usize = sys::WC_SHA3_256_DIGEST_SIZE as usize; /// Build a new SHA3_256 instance. /// @@ -1275,16 +1271,16 @@ impl SHA3_256 { /// let sha = SHA3_256::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_sha3: MaybeUninit = MaybeUninit::uninit(); + let mut wc_sha3: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha3_256(wc_sha3.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha3_256(wc_sha3.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -1344,9 +1340,9 @@ impl SHA3_256 { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha3_256(&mut self.wc_sha3, heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha3_256(&mut self.wc_sha3, heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -1374,7 +1370,7 @@ impl SHA3_256 { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Sha3_256_Update(&mut self.wc_sha3, data.as_ptr(), data_size) + sys::wc_Sha3_256_Update(&mut self.wc_sha3, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -1405,10 +1401,10 @@ impl SHA3_256 { /// ``` pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { if hash.len() != Self::DIGEST_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let rc = unsafe { - ws::wc_Sha3_256_Final(&mut self.wc_sha3, hash.as_mut_ptr()) + sys::wc_Sha3_256_Final(&mut self.wc_sha3, hash.as_mut_ptr()) }; if rc != 0 { return Err(rc); @@ -1426,18 +1422,18 @@ impl Drop for SHA3_256 { /// SHA3_256 struct goes out of scope, automatically cleaning up resources /// and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_Sha3_256_Free(&mut self.wc_sha3); } + unsafe { sys::wc_Sha3_256_Free(&mut self.wc_sha3); } } } /// Context for SHA3-384 computation. pub struct SHA3_384 { - wc_sha3: ws::wc_Sha3, + wc_sha3: sys::wc_Sha3, } impl SHA3_384 { /// SHA3-384 digest size in bytes. - pub const DIGEST_SIZE: usize = ws::WC_SHA3_384_DIGEST_SIZE as usize; + pub const DIGEST_SIZE: usize = sys::WC_SHA3_384_DIGEST_SIZE as usize; /// Build a new SHA3_384 instance. /// @@ -1475,16 +1471,16 @@ impl SHA3_384 { /// let sha = SHA3_384::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_sha3: MaybeUninit = MaybeUninit::uninit(); + let mut wc_sha3: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha3_384(wc_sha3.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha3_384(wc_sha3.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -1544,9 +1540,9 @@ impl SHA3_384 { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha3_384(&mut self.wc_sha3, heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha3_384(&mut self.wc_sha3, heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -1574,7 +1570,7 @@ impl SHA3_384 { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Sha3_384_Update(&mut self.wc_sha3, data.as_ptr(), data_size) + sys::wc_Sha3_384_Update(&mut self.wc_sha3, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -1605,10 +1601,10 @@ impl SHA3_384 { /// ``` pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { if hash.len() != Self::DIGEST_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let rc = unsafe { - ws::wc_Sha3_384_Final(&mut self.wc_sha3, hash.as_mut_ptr()) + sys::wc_Sha3_384_Final(&mut self.wc_sha3, hash.as_mut_ptr()) }; if rc != 0 { return Err(rc); @@ -1626,18 +1622,18 @@ impl Drop for SHA3_384 { /// SHA3_384 struct goes out of scope, automatically cleaning up resources /// and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_Sha3_384_Free(&mut self.wc_sha3); } + unsafe { sys::wc_Sha3_384_Free(&mut self.wc_sha3); } } } /// Context for SHA3-512 computation. pub struct SHA3_512 { - wc_sha3: ws::wc_Sha3, + wc_sha3: sys::wc_Sha3, } impl SHA3_512 { /// SHA3-512 digest size in bytes. - pub const DIGEST_SIZE: usize = ws::WC_SHA3_512_DIGEST_SIZE as usize; + pub const DIGEST_SIZE: usize = sys::WC_SHA3_512_DIGEST_SIZE as usize; /// Build a new SHA3_512 instance. /// @@ -1675,16 +1671,16 @@ impl SHA3_512 { /// let sha = SHA3_512::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_sha3: MaybeUninit = MaybeUninit::uninit(); + let mut wc_sha3: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha3_512(wc_sha3.as_mut_ptr(), heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha3_512(wc_sha3.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -1744,9 +1740,9 @@ impl SHA3_512 { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; - let rc = unsafe { ws::wc_InitSha3_512(&mut self.wc_sha3, heap, dev_id) }; + let rc = unsafe { sys::wc_InitSha3_512(&mut self.wc_sha3, heap, dev_id) }; if rc != 0 { return Err(rc); } @@ -1774,7 +1770,7 @@ impl SHA3_512 { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Sha3_512_Update(&mut self.wc_sha3, data.as_ptr(), data_size) + sys::wc_Sha3_512_Update(&mut self.wc_sha3, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -1805,10 +1801,10 @@ impl SHA3_512 { /// ``` pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { if hash.len() != Self::DIGEST_SIZE { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let rc = unsafe { - ws::wc_Sha3_512_Final(&mut self.wc_sha3, hash.as_mut_ptr()) + sys::wc_Sha3_512_Final(&mut self.wc_sha3, hash.as_mut_ptr()) }; if rc != 0 { return Err(rc); @@ -1826,18 +1822,18 @@ impl Drop for SHA3_512 { /// SHA3_512 struct goes out of scope, automatically cleaning up resources /// and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_Sha3_512_Free(&mut self.wc_sha3); } + unsafe { sys::wc_Sha3_512_Free(&mut self.wc_sha3); } } } /// Context for SHAKE128 (SHA-3) computation. pub struct SHAKE128 { - wc_shake: ws::wc_Shake, + wc_shake: sys::wc_Shake, } impl SHAKE128 { /// Squeeze block size. - pub const SQUEEZE_BLOCK_SIZE: usize = ws::WC_SHA3_128_BLOCK_SIZE as usize; + pub const SQUEEZE_BLOCK_SIZE: usize = sys::WC_SHA3_128_BLOCK_SIZE as usize; /// Build a new SHAKE128 instance. /// @@ -1875,17 +1871,17 @@ impl SHAKE128 { /// let sha = SHAKE128::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_shake: MaybeUninit = MaybeUninit::uninit(); + let mut wc_shake: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_InitShake128(wc_shake.as_mut_ptr(), heap, dev_id) + sys::wc_InitShake128(wc_shake.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); @@ -1946,10 +1942,10 @@ impl SHAKE128 { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_InitShake128(&mut self.wc_shake, heap, dev_id) + sys::wc_InitShake128(&mut self.wc_shake, heap, dev_id) }; if rc != 0 { return Err(rc); @@ -1978,7 +1974,7 @@ impl SHAKE128 { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Shake128_Update(&mut self.wc_shake, data.as_ptr(), data_size) + sys::wc_Shake128_Update(&mut self.wc_shake, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -2009,7 +2005,7 @@ impl SHAKE128 { pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { let hash_size = hash.len() as u32; let rc = unsafe { - ws::wc_Shake128_Final(&mut self.wc_shake, hash.as_mut_ptr(), hash_size) + sys::wc_Shake128_Final(&mut self.wc_shake, hash.as_mut_ptr(), hash_size) }; if rc != 0 { return Err(rc); @@ -2038,7 +2034,7 @@ impl SHAKE128 { pub fn absorb(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Shake128_Absorb(&mut self.wc_shake, data.as_ptr(), data_size) + sys::wc_Shake128_Absorb(&mut self.wc_shake, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -2071,11 +2067,11 @@ impl SHAKE128 { pub fn squeeze_blocks(&mut self, dout: &mut [u8]) -> Result<(), i32> { let dout_size = dout.len() as u32; if dout_size % (Self::SQUEEZE_BLOCK_SIZE as u32) != 0 { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let n_blocks = (dout_size / (Self::SQUEEZE_BLOCK_SIZE as u32)) as u32; let rc = unsafe { - ws::wc_Shake128_SqueezeBlocks(&mut self.wc_shake, dout.as_mut_ptr(), n_blocks) + sys::wc_Shake128_SqueezeBlocks(&mut self.wc_shake, dout.as_mut_ptr(), n_blocks) }; if rc != 0 { return Err(rc); @@ -2093,18 +2089,18 @@ impl Drop for SHAKE128 { /// SHAKE128 struct goes out of scope, automatically cleaning up resources /// and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_Shake128_Free(&mut self.wc_shake); } + unsafe { sys::wc_Shake128_Free(&mut self.wc_shake); } } } /// Context for SHAKE256 (SHA-3) computation. pub struct SHAKE256 { - wc_shake: ws::wc_Shake, + wc_shake: sys::wc_Shake, } impl SHAKE256 { /// Squeeze block size. - pub const SQUEEZE_BLOCK_SIZE: usize = ws::WC_SHA3_256_BLOCK_SIZE as usize; + pub const SQUEEZE_BLOCK_SIZE: usize = sys::WC_SHA3_256_BLOCK_SIZE as usize; /// Build a new SHAKE256 instance. /// @@ -2142,17 +2138,17 @@ impl SHAKE256 { /// let sha = SHAKE256::new_ex(None, None).expect("Error with new_ex()"); /// ``` pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { - let mut wc_shake: MaybeUninit = MaybeUninit::uninit(); + let mut wc_shake: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, None => core::ptr::null_mut(), }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_InitShake256(wc_shake.as_mut_ptr(), heap, dev_id) + sys::wc_InitShake256(wc_shake.as_mut_ptr(), heap, dev_id) }; if rc != 0 { return Err(rc); @@ -2213,10 +2209,10 @@ impl SHAKE256 { }; let dev_id = match dev_id { Some(dev_id) => dev_id, - None => ws::INVALID_DEVID, + None => sys::INVALID_DEVID, }; let rc = unsafe { - ws::wc_InitShake256(&mut self.wc_shake, heap, dev_id) + sys::wc_InitShake256(&mut self.wc_shake, heap, dev_id) }; if rc != 0 { return Err(rc); @@ -2245,7 +2241,7 @@ impl SHAKE256 { pub fn update(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Shake256_Update(&mut self.wc_shake, data.as_ptr(), data_size) + sys::wc_Shake256_Update(&mut self.wc_shake, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -2276,7 +2272,7 @@ impl SHAKE256 { pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> { let hash_size = hash.len() as u32; let rc = unsafe { - ws::wc_Shake256_Final(&mut self.wc_shake, hash.as_mut_ptr(), hash_size) + sys::wc_Shake256_Final(&mut self.wc_shake, hash.as_mut_ptr(), hash_size) }; if rc != 0 { return Err(rc); @@ -2305,7 +2301,7 @@ impl SHAKE256 { pub fn absorb(&mut self, data: &[u8]) -> Result<(), i32> { let data_size = data.len() as u32; let rc = unsafe { - ws::wc_Shake256_Absorb(&mut self.wc_shake, data.as_ptr(), data_size) + sys::wc_Shake256_Absorb(&mut self.wc_shake, data.as_ptr(), data_size) }; if rc != 0 { return Err(rc); @@ -2338,11 +2334,11 @@ impl SHAKE256 { pub fn squeeze_blocks(&mut self, dout: &mut [u8]) -> Result<(), i32> { let dout_size = dout.len() as u32; if dout_size % (Self::SQUEEZE_BLOCK_SIZE as u32) != 0 { - return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E); + return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } let n_blocks = (dout_size / (Self::SQUEEZE_BLOCK_SIZE as u32)) as u32; let rc = unsafe { - ws::wc_Shake256_SqueezeBlocks(&mut self.wc_shake, dout.as_mut_ptr(), n_blocks) + sys::wc_Shake256_SqueezeBlocks(&mut self.wc_shake, dout.as_mut_ptr(), n_blocks) }; if rc != 0 { return Err(rc); @@ -2360,6 +2356,6 @@ impl Drop for SHAKE256 { /// SHAKE256 struct goes out of scope, automatically cleaning up resources /// and preventing memory leaks. fn drop(&mut self) { - unsafe { ws::wc_Shake256_Free(&mut self.wc_shake); } + unsafe { sys::wc_Shake256_Free(&mut self.wc_shake); } } } From c61ac22e8903b1ebf9431918606cc09de8467a05 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 14 Nov 2025 09:26:43 -0500 Subject: [PATCH 2/2] Rust wrapper: enable conditional compilation based on C library build options --- wolfcrypt/src/dh.c | 3 +- wolfssl/wolfcrypt/dh.h | 3 +- wolfssl/wolfcrypt/ecc.h | 2 + wrapper/rust/wolfssl/Cargo.lock | 1 + wrapper/rust/wolfssl/Cargo.toml | 1 + wrapper/rust/wolfssl/build.rs | 137 ++++++++++++++++-- wrapper/rust/wolfssl/src/wolfcrypt/aes.rs | 81 ++++++++++- wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs | 18 +++ wrapper/rust/wolfssl/src/wolfcrypt/dh.rs | 10 ++ wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs | 94 ++++++++++++ wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs | 90 ++++++++++++ wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs | 82 +++++++++++ wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs | 66 ++++++++- wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs | 37 +++++ wrapper/rust/wolfssl/src/wolfcrypt/sha.rs | 33 +++++ wrapper/rust/wolfssl/tests/test_aes.rs | 21 +++ wrapper/rust/wolfssl/tests/test_cmac.rs | 3 + wrapper/rust/wolfssl/tests/test_dh.rs | 6 +- wrapper/rust/wolfssl/tests/test_ecc.rs | 66 ++++++++- wrapper/rust/wolfssl/tests/test_ed25519.rs | 42 ++++++ wrapper/rust/wolfssl/tests/test_ed448.rs | 57 ++++++++ wrapper/rust/wolfssl/tests/test_kdf.rs | 9 +- wrapper/rust/wolfssl/tests/test_rsa.rs | 5 + wrapper/rust/wolfssl/tests/test_sha.rs | 13 ++ 24 files changed, 853 insertions(+), 27 deletions(-) diff --git a/wolfcrypt/src/dh.c b/wolfcrypt/src/dh.c index 99c09a97bb..1035cbc658 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 0bb8508a79..2b32c9c9be 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 3f6273a1c8..9089b876ef 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 e1d06cdef8..603e91f1d0 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 6bea736960..78d876eff4 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 bf362ff3b2..de4f95c419 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 c8a77c4382..02f83bb2ad 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 5b06e403e4..fbb7221767 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 4739dd0666..573d4179f8 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 ebde297c11..f65129fef6 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 aa73056b1c..327a8416ea 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 33c576117c..104c92808c 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 19f10a18f1..ba434024aa 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 4a1830db42..e548a54d3a 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 174ad11c5f..63696d4c4e 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 87c41edb47..0f58c72de7 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 13ed95137e..9858c2c966 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 7dc7cb7458..88abf0291a 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 663c1e6cc0..0dbd1300b8 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 fd4beaf002..880b0fc6bd 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 08e5506a82..ca26d053e8 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 b075d7fdb4..5b4ffe1036 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 5f6d15b6b1..db760b5cfc 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 f617d02e93..748da48a53 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]) {