From ad7f25ccba3d6b20105684080f8f9d99c2c8d9f5 Mon Sep 17 00:00:00 2001 From: WingZer0o Date: Tue, 14 Oct 2025 23:18:17 -0400 Subject: [PATCH 1/2] slh_dsa --- Cargo.toml | 2 +- src/lib.rs | 27 ++++++++++++----------- src/pqc/slh_dsa.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 src/pqc/slh_dsa.rs diff --git a/Cargo.toml b/Cargo.toml index bfb0590..006fb32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cas_core_lib" -version = "0.2.3" +version = "0.2.4" edition = "2021" description = "This is a Rust library providing external facing functions to performant and trusted encryption in Rust" license = "Apache-2.0" diff --git a/src/lib.rs b/src/lib.rs index 349b7cd..1de9ff9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,18 +2,21 @@ use zeroizing_alloc::ZeroAlloc; #[global_allocator] static ALLOC: ZeroAlloc = ZeroAlloc(std::alloc::System); -mod aes; -mod blake2; -mod digital_signature; -mod ed25519; -mod helpers; -mod hmac; -mod rsa; -mod sha; -mod x25519; -mod ascon_aead; -mod zstd; -mod hpke; +pub mod aes; +pub mod blake2; +pub mod digital_signature; +pub mod ed25519; +pub mod helpers; +pub mod hmac; +pub mod rsa; +pub mod sha; +pub mod x25519; +pub mod ascon_aead; +pub mod zstd; +pub mod hpke; +pub mod pqc { + pub mod slh_dsa; +} pub mod password_hashers { pub mod types; diff --git a/src/pqc/slh_dsa.rs b/src/pqc/slh_dsa.rs new file mode 100644 index 0000000..b230818 --- /dev/null +++ b/src/pqc/slh_dsa.rs @@ -0,0 +1,54 @@ +use cas_lib::pqc::cas_pqc::{SlhDsaKeyPair}; +use cas_lib::pqc::slh_dsa::{generate_signing_and_verification_key, sign_message, verify_signature}; + +#[no_mangle] +pub extern "C" fn slh_dsa_generate_signing_and_verification_key() -> SlhDsaKeyPair { + generate_signing_and_verification_key() +} + +#[no_mangle] +pub extern "C" fn slh_dsa_sign_message( + key_pair: *const u8, + key_pair_length: usize, + message: *const u8, + message_length: usize, +) -> Vec { + let key_pair_slice = unsafe { + assert!(!key_pair.is_null()); + std::slice::from_raw_parts(key_pair, key_pair_length) + } + .to_vec(); + let message_slice = unsafe { + assert!(!message.is_null()); + std::slice::from_raw_parts(message, message_length) + } + .to_vec(); + sign_message(key_pair_slice, message_slice) +} + +#[no_mangle] +pub extern "C" fn slh_dsa_verify_signature( + public_key: *const u8, + public_key_length: usize, + signature: *const u8, + signature_length: usize, + message: *const u8, + message_length: usize, +) -> bool { + let public_key_slice = unsafe { + assert!(!public_key.is_null()); + std::slice::from_raw_parts(public_key, public_key_length) + } + .to_vec(); + let signature_slice = unsafe { + assert!(!signature.is_null()); + std::slice::from_raw_parts(signature, signature_length) + } + .to_vec(); + let message_slice = unsafe { + assert!(!message.is_null()); + std::slice::from_raw_parts(message, message_length) + } + .to_vec(); + verify_signature(public_key_slice, signature_slice, message_slice) +} \ No newline at end of file From 762865dbd2dd01b6b2ec42bf009c2536f03c75a1 Mon Sep 17 00:00:00 2001 From: WingZer0o Date: Tue, 14 Oct 2025 23:53:55 -0400 Subject: [PATCH 2/2] changes to repr --- Cargo.toml | 2 +- src/lib.rs | 1 + src/pqc/slh_dsa.rs | 25 ++++++++++++++++++++----- src/pqc/types.rs | 13 +++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/pqc/types.rs diff --git a/Cargo.toml b/Cargo.toml index 006fb32..96af4db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,5 @@ crate-type = ["dylib"] [dependencies] libc = "0.2.146" -cas-lib = "0.2.63" +cas-lib = "0.2.64" zeroizing-alloc = "0.1.0" diff --git a/src/lib.rs b/src/lib.rs index 1de9ff9..cb020ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ pub mod zstd; pub mod hpke; pub mod pqc { pub mod slh_dsa; + pub mod types; } pub mod password_hashers { diff --git a/src/pqc/slh_dsa.rs b/src/pqc/slh_dsa.rs index b230818..942d70b 100644 --- a/src/pqc/slh_dsa.rs +++ b/src/pqc/slh_dsa.rs @@ -1,9 +1,18 @@ -use cas_lib::pqc::cas_pqc::{SlhDsaKeyPair}; use cas_lib::pqc::slh_dsa::{generate_signing_and_verification_key, sign_message, verify_signature}; +use crate::pqc::types::{SlhDsaKeyPairResult, SlhDsaSignature}; #[no_mangle] -pub extern "C" fn slh_dsa_generate_signing_and_verification_key() -> SlhDsaKeyPair { - generate_signing_and_verification_key() +pub extern "C" fn slh_dsa_generate_signing_and_verification_key() -> SlhDsaKeyPairResult { + let key_pair: cas_lib::pqc::cas_pqc::SlhDsaKeyPair = generate_signing_and_verification_key(); + let result = SlhDsaKeyPairResult { + signing_key_ptr: key_pair.signing_key.as_ptr(), + signing_key_length: key_pair.signing_key.len(), + verification_key_ptr: key_pair.verification_key.as_ptr(), + verification_key_length: key_pair.verification_key.len(), + }; + std::mem::forget(key_pair.signing_key); + std::mem::forget(key_pair.verification_key); + result } #[no_mangle] @@ -12,7 +21,7 @@ pub extern "C" fn slh_dsa_sign_message( key_pair_length: usize, message: *const u8, message_length: usize, -) -> Vec { +) -> SlhDsaSignature { let key_pair_slice = unsafe { assert!(!key_pair.is_null()); std::slice::from_raw_parts(key_pair, key_pair_length) @@ -23,7 +32,13 @@ pub extern "C" fn slh_dsa_sign_message( std::slice::from_raw_parts(message, message_length) } .to_vec(); - sign_message(key_pair_slice, message_slice) + let signature: Vec = sign_message(key_pair_slice, message_slice); + let result = SlhDsaSignature { + signature_ptr: signature.as_ptr(), + signature_length: signature.len(), + }; + std::mem::forget(signature); + result } #[no_mangle] diff --git a/src/pqc/types.rs b/src/pqc/types.rs new file mode 100644 index 0000000..2bb8888 --- /dev/null +++ b/src/pqc/types.rs @@ -0,0 +1,13 @@ +#[repr(C)] +pub struct SlhDsaKeyPairResult { + pub signing_key_ptr: *const u8, + pub signing_key_length: usize, + pub verification_key_ptr: *const u8, + pub verification_key_length: usize, +} + +#[repr(C)] +pub struct SlhDsaSignature { + pub signature_ptr: *const u8, + pub signature_length: usize, +} \ No newline at end of file