Rust wrapper: merge wolfssl-sys crate into wolfssl crate

This commit is contained in:
Josh Holtrop
2025-11-12 14:50:57 -05:00
parent 50c5028c5a
commit dd3b9260f9
26 changed files with 732 additions and 1118 deletions

View File

@@ -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

View File

@@ -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. |

View File

@@ -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

View File

@@ -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",
]

View File

@@ -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"

View File

@@ -1,7 +0,0 @@
.PHONY: all
all:
cargo build
.PHONY: clean
clean:
cargo clean

View File

@@ -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),
)
})
}

View File

@@ -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",
]

View File

@@ -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"

View File

@@ -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(())
}

View File

@@ -19,3 +19,4 @@
*/
pub mod wolfcrypt;
pub mod sys;

View File

@@ -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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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<i32>) -> Result<Self, i32> {
let ws_xtsaes = new_ws_xtsaes(heap, dev_id)?;
let ws_xtsaesstreamdata: MaybeUninit<ws::XtsAesStreamData> = MaybeUninit::uninit();
let ws_xtsaesstreamdata: MaybeUninit<sys::XtsAesStreamData> = 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::<O>()) 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::<O>()) 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::<O>()) 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::<O>()) 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<i32>) -> Result<ws::Aes, i32> {
fn new_ws_aes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<sys::Aes, 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 mut ws_aes: MaybeUninit<ws::Aes> = MaybeUninit::uninit();
let mut ws_aes: MaybeUninit<sys::Aes> = 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<i32>) -> R
Ok(ws_aes)
}
fn new_ws_xtsaes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<ws::XtsAes, i32> {
fn new_ws_xtsaes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<sys::XtsAes, 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 mut ws_xtsaes: MaybeUninit<ws::XtsAes> = MaybeUninit::uninit();
let mut ws_xtsaes: MaybeUninit<sys::XtsAes> = 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);

View File

@@ -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<i32>) -> Result<Self, i32> {
let key_size = key.len() as u32;
let mut ws_cmac: MaybeUninit<ws::Cmac> = MaybeUninit::uninit();
let typ = ws::CmacType_WC_CMAC_AES as i32;
let mut ws_cmac: MaybeUninit<sys::Cmac> = 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); }
}
}

View File

@@ -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<i32>) -> Result<Self, i32> {
let mut wc_dhkey: MaybeUninit<ws::DhKey> = MaybeUninit::uninit();
let mut wc_dhkey: MaybeUninit<sys::DhKey> = 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<i32>) -> Result<Self, i32> {
let mut wc_dhkey: MaybeUninit<ws::DhKey> = MaybeUninit::uninit();
let mut wc_dhkey: MaybeUninit<sys::DhKey> = 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<i32>) -> Result<Self, i32> {
let p_size = p.len() as u32;
let g_size = g.len() as u32;
let mut wc_dhkey: MaybeUninit<ws::DhKey> = MaybeUninit::uninit();
let mut wc_dhkey: MaybeUninit<sys::DhKey> = 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<ws::DhKey> = MaybeUninit::uninit();
let mut wc_dhkey: MaybeUninit<sys::DhKey> = 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<ws::DhKey> = MaybeUninit::uninit();
let mut wc_dhkey: MaybeUninit<sys::DhKey> = 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); }
}
}

View File

@@ -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<Self, i32> {
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<Self, i32> {
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<usize, i32> {
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<usize, i32> {
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<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32, i32> {
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<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32>) -> Result<ECC, i32> {
let din_size = din.len() as u32;
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<i32>) -> Result<ECC, i32> {
let din_size = din.len() as u32;
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = 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<usize, i32> {
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<usize, i32> {
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<usize, i32> {
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<usize, i32> {
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<usize, i32> {
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); }
}
}

View File

@@ -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<i32>) -> Result<Self, i32> {
let mut ws_key: MaybeUninit<ws::ed25519_key> = MaybeUninit::uninit();
let mut ws_key: MaybeUninit<sys::ed25519_key> = 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<i32>) -> Result<Self, i32> {
let mut ws_key: MaybeUninit<ws::ed25519_key> = MaybeUninit::uninit();
let mut ws_key: MaybeUninit<sys::ed25519_key> = 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<usize, i32> {
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<usize, i32> {
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<usize, i32> {
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<usize, i32> {
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); }
}
}

View File

@@ -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<i32>) -> Result<Self, i32> {
let mut ws_key: MaybeUninit<ws::ed448_key> = MaybeUninit::uninit();
let mut ws_key: MaybeUninit<sys::ed448_key> = 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<i32>) -> Result<Self, i32> {
let mut ws_key: MaybeUninit<ws::ed448_key> = MaybeUninit::uninit();
let mut ws_key: MaybeUninit<sys::ed448_key> = 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<usize, i32> {
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<usize, i32> {
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<usize, i32> {
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<usize, i32> {
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); }
}
}

View File

@@ -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 {

View File

@@ -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<usize, i32> {
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<i32>) -> Result<Self, i32> {
let key_size = key.len() as u32;
let mut wc_hmac: MaybeUninit<ws::Hmac> = MaybeUninit::uninit();
let mut wc_hmac: MaybeUninit<sys::Hmac> = 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<i32>) -> Result<Self, i32> {
let key_size = key.len() as u32;
let mut wc_hmac: MaybeUninit<ws::Hmac> = MaybeUninit::uninit();
let mut wc_hmac: MaybeUninit<sys::Hmac> = 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<usize, i32> {
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); }
}
}

View File

@@ -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) }
}

View File

@@ -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)

View File

@@ -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<u8, i32> {
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<T>(&mut self, buf: &mut [T]) -> Result<(), i32> {
let ptr = buf.as_mut_ptr() as *mut u8;
let size: u32 = (buf.len() * size_of::<T>()) 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); }
}
}

View File

@@ -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<i32>) -> Result<Self, i32> {
let mut wc_rsakey: MaybeUninit<ws::RsaKey> = MaybeUninit::uninit();
let mut wc_rsakey: MaybeUninit<sys::RsaKey> = 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<i32>) -> Result<Self, i32> {
let mut wc_rsakey: MaybeUninit<ws::RsaKey> = MaybeUninit::uninit();
let mut wc_rsakey: MaybeUninit<sys::RsaKey> = 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<i32>) -> Result<Self, i32> {
let mut wc_rsakey: MaybeUninit<ws::RsaKey> = MaybeUninit::uninit();
let mut wc_rsakey: MaybeUninit<sys::RsaKey> = 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<usize, i32> {
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); }
}
}

View File

@@ -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<i32>) -> Result<Self, i32> {
let mut wc_sha: MaybeUninit<ws::wc_Sha> = MaybeUninit::uninit();
let mut wc_sha: MaybeUninit<sys::wc_Sha> = 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<i32>) -> Result<Self, i32> {
let mut wc_sha224: MaybeUninit<ws::wc_Sha224> = MaybeUninit::uninit();
let mut wc_sha224: MaybeUninit<sys::wc_Sha224> = 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<i32>) -> Result<Self, i32> {
let mut wc_sha256: MaybeUninit<ws::wc_Sha256> = MaybeUninit::uninit();
let mut wc_sha256: MaybeUninit<sys::wc_Sha256> = 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<i32>) -> Result<Self, i32> {
let mut wc_sha384: MaybeUninit<ws::wc_Sha384> = MaybeUninit::uninit();
let mut wc_sha384: MaybeUninit<sys::wc_Sha384> = 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<i32>) -> Result<Self, i32> {
let mut wc_sha512: MaybeUninit<ws::wc_Sha512> = MaybeUninit::uninit();
let mut wc_sha512: MaybeUninit<sys::wc_Sha512> = 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<i32>) -> Result<Self, i32> {
let mut wc_sha3: MaybeUninit<ws::wc_Sha3> = MaybeUninit::uninit();
let mut wc_sha3: MaybeUninit<sys::wc_Sha3> = 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<i32>) -> Result<Self, i32> {
let mut wc_sha3: MaybeUninit<ws::wc_Sha3> = MaybeUninit::uninit();
let mut wc_sha3: MaybeUninit<sys::wc_Sha3> = 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<i32>) -> Result<Self, i32> {
let mut wc_sha3: MaybeUninit<ws::wc_Sha3> = MaybeUninit::uninit();
let mut wc_sha3: MaybeUninit<sys::wc_Sha3> = 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<i32>) -> Result<Self, i32> {
let mut wc_sha3: MaybeUninit<ws::wc_Sha3> = MaybeUninit::uninit();
let mut wc_sha3: MaybeUninit<sys::wc_Sha3> = 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<i32>) -> Result<Self, i32> {
let mut wc_shake: MaybeUninit<ws::wc_Shake> = MaybeUninit::uninit();
let mut wc_shake: MaybeUninit<sys::wc_Shake> = 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<i32>) -> Result<Self, i32> {
let mut wc_shake: MaybeUninit<ws::wc_Shake> = MaybeUninit::uninit();
let mut wc_shake: MaybeUninit<sys::wc_Shake> = 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); }
}
}