From f638633ba860c9a9c623024e235a5c679068d798 Mon Sep 17 00:00:00 2001 From: Thom Wiggers Date: Wed, 20 Dec 2023 09:42:47 +0100 Subject: [PATCH] Backport Kyber div patch to 0.7 for Signal --- Cargo.toml | 8 +- implementations.yaml | 99 +- pqclean | 2 +- pqcrypto-classicmceliece/Cargo.toml | 36 - pqcrypto-classicmceliece/README.md | 68 - pqcrypto-classicmceliece/build.rs | 142 - pqcrypto-classicmceliece/pqclean | 1 - pqcrypto-classicmceliece/src/ffi.rs | 1322 ------ pqcrypto-classicmceliece/src/lib.rs | 114 - .../src/mceliece348864.rs | 198 - .../src/mceliece348864f.rs | 201 - .../src/mceliece460896.rs | 198 - .../src/mceliece460896f.rs | 201 - .../src/mceliece6688128.rs | 201 - .../src/mceliece6688128f.rs | 201 - .../src/mceliece6960119.rs | 201 - .../src/mceliece6960119f.rs | 201 - .../src/mceliece8192128.rs | 201 - .../src/mceliece8192128f.rs | 201 - pqcrypto-dilithium/Cargo.toml | 38 - pqcrypto-dilithium/README.md | 45 - pqcrypto-dilithium/build.rs | 162 - pqcrypto-dilithium/pqclean | 1 - pqcrypto-dilithium/src/dilithium2.rs | 391 -- pqcrypto-dilithium/src/dilithium3.rs | 391 -- pqcrypto-dilithium/src/dilithium5.rs | 391 -- pqcrypto-dilithium/src/ffi.rs | 1467 ------ pqcrypto-dilithium/src/lib.rs | 48 - pqcrypto-falcon/Cargo.toml | 38 - pqcrypto-falcon/README.md | 41 - pqcrypto-falcon/build.rs | 155 - pqcrypto-falcon/pqclean | 1 - pqcrypto-falcon/src/falcon1024.rs | 391 -- pqcrypto-falcon/src/falcon512.rs | 391 -- pqcrypto-falcon/src/ffi.rs | 982 ---- pqcrypto-falcon/src/lib.rs | 38 - pqcrypto-hqc/Cargo.toml | 35 - pqcrypto-hqc/README.md | 39 - pqcrypto-hqc/build.rs | 58 - pqcrypto-hqc/pqclean | 1 - pqcrypto-hqc/src/ffi.rs | 144 - pqcrypto-hqc/src/hqc128.rs | 171 - pqcrypto-hqc/src/hqc192.rs | 171 - pqcrypto-hqc/src/hqc256.rs | 171 - pqcrypto-hqc/src/lib.rs | 45 - pqcrypto-kyber/Cargo.toml | 2 +- pqcrypto-sphincsplus/Cargo.toml | 37 - pqcrypto-sphincsplus/README.md | 69 - pqcrypto-sphincsplus/build.rs | 152 - pqcrypto-sphincsplus/pqclean | 1 - pqcrypto-sphincsplus/src/ffi.rs | 4062 ----------------- pqcrypto-sphincsplus/src/lib.rs | 135 - .../src/sphincssha2128fsimple.rs | 374 -- .../src/sphincssha2128ssimple.rs | 374 -- .../src/sphincssha2192fsimple.rs | 374 -- .../src/sphincssha2192ssimple.rs | 374 -- .../src/sphincssha2256fsimple.rs | 374 -- .../src/sphincssha2256ssimple.rs | 374 -- .../src/sphincsshake128fsimple.rs | 378 -- .../src/sphincsshake128ssimple.rs | 378 -- .../src/sphincsshake192fsimple.rs | 378 -- .../src/sphincsshake192ssimple.rs | 378 -- .../src/sphincsshake256fsimple.rs | 378 -- .../src/sphincsshake256ssimple.rs | 378 -- pqcrypto/Cargo.toml | 33 - pqcrypto/README.md | 38 - pqcrypto/examples/keygen.rs | 14 - pqcrypto/examples/signer.rs | 25 - pqcrypto/examples/verifier.rs | 27 - pqcrypto/src/lib.rs | 41 - 70 files changed, 5 insertions(+), 18745 deletions(-) delete mode 100644 pqcrypto-classicmceliece/Cargo.toml delete mode 100644 pqcrypto-classicmceliece/README.md delete mode 100644 pqcrypto-classicmceliece/build.rs delete mode 120000 pqcrypto-classicmceliece/pqclean delete mode 100644 pqcrypto-classicmceliece/src/ffi.rs delete mode 100644 pqcrypto-classicmceliece/src/lib.rs delete mode 100644 pqcrypto-classicmceliece/src/mceliece348864.rs delete mode 100644 pqcrypto-classicmceliece/src/mceliece348864f.rs delete mode 100644 pqcrypto-classicmceliece/src/mceliece460896.rs delete mode 100644 pqcrypto-classicmceliece/src/mceliece460896f.rs delete mode 100644 pqcrypto-classicmceliece/src/mceliece6688128.rs delete mode 100644 pqcrypto-classicmceliece/src/mceliece6688128f.rs delete mode 100644 pqcrypto-classicmceliece/src/mceliece6960119.rs delete mode 100644 pqcrypto-classicmceliece/src/mceliece6960119f.rs delete mode 100644 pqcrypto-classicmceliece/src/mceliece8192128.rs delete mode 100644 pqcrypto-classicmceliece/src/mceliece8192128f.rs delete mode 100644 pqcrypto-dilithium/Cargo.toml delete mode 100644 pqcrypto-dilithium/README.md delete mode 100644 pqcrypto-dilithium/build.rs delete mode 120000 pqcrypto-dilithium/pqclean delete mode 100644 pqcrypto-dilithium/src/dilithium2.rs delete mode 100644 pqcrypto-dilithium/src/dilithium3.rs delete mode 100644 pqcrypto-dilithium/src/dilithium5.rs delete mode 100644 pqcrypto-dilithium/src/ffi.rs delete mode 100644 pqcrypto-dilithium/src/lib.rs delete mode 100644 pqcrypto-falcon/Cargo.toml delete mode 100644 pqcrypto-falcon/README.md delete mode 100644 pqcrypto-falcon/build.rs delete mode 120000 pqcrypto-falcon/pqclean delete mode 100644 pqcrypto-falcon/src/falcon1024.rs delete mode 100644 pqcrypto-falcon/src/falcon512.rs delete mode 100644 pqcrypto-falcon/src/ffi.rs delete mode 100644 pqcrypto-falcon/src/lib.rs delete mode 100644 pqcrypto-hqc/Cargo.toml delete mode 100644 pqcrypto-hqc/README.md delete mode 100644 pqcrypto-hqc/build.rs delete mode 120000 pqcrypto-hqc/pqclean delete mode 100644 pqcrypto-hqc/src/ffi.rs delete mode 100644 pqcrypto-hqc/src/hqc128.rs delete mode 100644 pqcrypto-hqc/src/hqc192.rs delete mode 100644 pqcrypto-hqc/src/hqc256.rs delete mode 100644 pqcrypto-hqc/src/lib.rs delete mode 100644 pqcrypto-sphincsplus/Cargo.toml delete mode 100644 pqcrypto-sphincsplus/README.md delete mode 100644 pqcrypto-sphincsplus/build.rs delete mode 120000 pqcrypto-sphincsplus/pqclean delete mode 100644 pqcrypto-sphincsplus/src/ffi.rs delete mode 100644 pqcrypto-sphincsplus/src/lib.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincssha2128fsimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincssha2128ssimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincssha2192fsimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincssha2192ssimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincssha2256fsimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincssha2256ssimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincsshake128fsimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincsshake128ssimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincsshake192fsimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincsshake192ssimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincsshake256fsimple.rs delete mode 100644 pqcrypto-sphincsplus/src/sphincsshake256ssimple.rs delete mode 100644 pqcrypto/Cargo.toml delete mode 100644 pqcrypto/README.md delete mode 100644 pqcrypto/examples/keygen.rs delete mode 100644 pqcrypto/examples/signer.rs delete mode 100644 pqcrypto/examples/verifier.rs delete mode 100644 pqcrypto/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 2e0565c7..a8950126 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,8 @@ # This file has been generated by generate-implementations.py [workspace] members=[ - "pqcrypto", "pqcrypto-traits", "pqcrypto-internals", "pqcrypto-kyber", - "pqcrypto-classicmceliece", - "pqcrypto-hqc", - "pqcrypto-dilithium", - "pqcrypto-falcon", - "pqcrypto-sphincsplus", ] -resolver="2" \ No newline at end of file +resolver="2" diff --git a/implementations.yaml b/implementations.yaml index 2d4d29cf..df44e11f 100644 --- a/implementations.yaml +++ b/implementations.yaml @@ -5,7 +5,7 @@ traits_version: 0.3.5 kems: kyber: - version: 0.8.0 + version: 0.7.7 x86_avx2_guard: 'target_arch == "x86_64" && avx2_enabled && !is_windows && !is_macos' implementations: [clean, avx2, aarch64] schemes: @@ -15,99 +15,4 @@ kems: implementations: [clean, avx2, aarch64] - name: kyber1024 implementations: [clean, avx2, aarch64] - classicmceliece: - version: 0.2.0 - notes: | - This implementation requires a lot of stack space. - You need to specify ``RUST_MIN_STACK=800000000``, probably. - x86_avx2_guard: 'target_arch == "x86_64" && avx2_enabled && !is_windows' - implementations: [clean, avx2] - schemes: - - name: mceliece348864 - implementations: [clean, avx2] - - name: mceliece348864f - implementations: [clean, avx2] - - name: mceliece460896 - implementations: [clean, avx2] - - name: mceliece460896f - implementations: [clean, avx2] - - name: mceliece6688128 - implementations: [clean, avx2] - doctest: no - - name: mceliece6688128f - implementations: [clean, avx2] - doctest: no - - name: mceliece6960119 - implementations: [clean, avx2] - doctest: no - - name: mceliece6960119f - implementations: [clean, avx2] - doctest: no - - name: mceliece8192128 - implementations: [clean, avx2] - doctest: no - - name: mceliece8192128f - implementations: [clean, avx2] - doctest: no - hqc: - version: 0.2.0 - implementations: [clean] - schemes: - - name: hqc-128 - implementations: [clean] - - name: hqc-192 - implementations: [clean] - - name: hqc-256 - implementations: [clean] - - -signs: - dilithium: - version: 0.5.0 - x86_avx2_guard: 'target_arch == "x86_64" && avx2_enabled && !is_windows' - implementations: [clean, avx2, aarch64] - schemes: - - name: dilithium2 - implementations: [clean, avx2, aarch64] - - name: dilithium3 - implementations: [clean, avx2, aarch64] - - name: dilithium5 - implementations: [clean, avx2, aarch64] - falcon: - version: 0.3.0 - implementations: [clean, avx2, aarch64] - schemes: - - name: falcon-512 - implementations: [clean, avx2, aarch64] - - name: falcon-1024 - implementations: [clean, avx2, aarch64] - sphincsplus: - version: 0.7.0 - implementations: [clean, avx2] - schemes: - - name: sphincs-shake-128f-simple - implementations: [clean, avx2] - - name: sphincs-shake-128s-simple - implementations: [clean, avx2] - - name: sphincs-shake-192f-simple - implementations: [clean, avx2] - - name: sphincs-shake-192s-simple - implementations: [clean, avx2] - - name: sphincs-shake-256f-simple - implementations: [clean, avx2] - - name: sphincs-shake-256s-simple - implementations: [clean, avx2] - - name: sphincs-sha2-128f-simple - implementations: [clean, avx2] - - name: sphincs-sha2-128s-simple - implementations: [clean, avx2] - - name: sphincs-sha2-192f-simple - implementations: [clean, avx2] - - name: sphincs-sha2-192s-simple - implementations: [clean, avx2] - - name: sphincs-sha2-256f-simple - implementations: [clean, avx2] - - name: sphincs-sha2-256s-simple - implementations: [clean, avx2] - -# vim: set ft=yaml ts=2 sw=2 tw=0 et : +signs: {} diff --git a/pqclean b/pqclean index 0657749a..dd7f97d0 160000 --- a/pqclean +++ b/pqclean @@ -1 +1 @@ -Subproject commit 0657749a785db30e7f49e9435452cb042edb1852 +Subproject commit dd7f97d050e7ddd8c6fbd0ca2c66e00971c7e23e diff --git a/pqcrypto-classicmceliece/Cargo.toml b/pqcrypto-classicmceliece/Cargo.toml deleted file mode 100644 index 3b8eee8c..00000000 --- a/pqcrypto-classicmceliece/Cargo.toml +++ /dev/null @@ -1,36 +0,0 @@ -[package] -name = "pqcrypto-classicmceliece" -description = "Post-Quantum Key-Encapsulation Mechanism classicmceliece" -readme = "README.md" -version = "0.2.0" -authors = ["Thom Wiggers "] -edition = "2021" -license = "MIT OR Apache-2.0" -homepage = "https://github.com/rustpq/" -repository = "https://github.com/rustpq/pqcrypto/" -keywords = ["cryptography", "post-quantum", "security"] -categories = ["cryptography", "no-std"] - -[dependencies] -pqcrypto-internals = { path = "../pqcrypto-internals", version = "0.2" } -pqcrypto-traits = { path = "../pqcrypto-traits", version = "0.3.5", default-features = false } -libc = "0.2.0" -serde = { version = "1.0", features = ["derive"], optional = true } -serde-big-array = { version = "0.5.1", optional = true } - -[features] -default = ["avx2", "std"] -avx2 = ["std"] -std = ["pqcrypto-traits/std"] -serialization = ["serde", "serde-big-array"] - -[dev-dependencies] - -[build-dependencies] -cc = { version = "1.0", features = ["parallel"] } -glob = "0.3.0" - -[badges] -travis-ci = { repository = "rustpq/pqcrypto", branch = "master" } -maintenance = { status = "actively-developed" } - diff --git a/pqcrypto-classicmceliece/README.md b/pqcrypto-classicmceliece/README.md deleted file mode 100644 index 93a485b2..00000000 --- a/pqcrypto-classicmceliece/README.md +++ /dev/null @@ -1,68 +0,0 @@ -# classicmceliece - - -This crate contains bindings to the C implementations of the following schemes, -from [PQClean][pqclean]. - -This project packages Post-Quantum cryptographic algorithms that participate in -the [NIST PQC standardization effort][nistpqc]. It is currently a collection of -wrappers around C implementations from the [PQClean][pqclean] project. - -## Serialization - -If you want `serde` support, enable the `serialization` feature. - -## Included implementations from PQClean - -Below is a list of the included schemes and the corresponding implementations -sourced from [PQClean][pqclean]. The "default" implementation is used in the -Rust-friendly interface, alternative implementations are exposed as ``ffi`` -methods only. - -* ``mceliece348864`` - * ``clean`` - * ``avx2`` (if supported) -* ``mceliece348864f`` - * ``clean`` - * ``avx2`` (if supported) -* ``mceliece460896`` - * ``clean`` - * ``avx2`` (if supported) -* ``mceliece460896f`` - * ``clean`` - * ``avx2`` (if supported) -* ``mceliece6688128`` - * ``clean`` - * ``avx2`` (if supported) -* ``mceliece6688128f`` - * ``clean`` - * ``avx2`` (if supported) -* ``mceliece6960119`` - * ``clean`` - * ``avx2`` (if supported) -* ``mceliece6960119f`` - * ``clean`` - * ``avx2`` (if supported) -* ``mceliece8192128`` - * ``clean`` - * ``avx2`` (if supported) -* ``mceliece8192128f`` - * ``clean`` - * ``avx2`` (if supported) - -## Notes - -This implementation requires a lot of stack space. -You need to specify ``RUST_MIN_STACK=800000000``, probably. - - -## License - -The wrappers and wrapper generation scripts in this project are covered by the -MIT or Apache 2.0 licenses, at your choice. - -The implementations we link to are not, however. Please see the [PQClean][pqclean] -project for the appropriate licenses. - -[pqclean]: https://github.com/PQClean/PQClean/ -[nistpqc]: https://nist.gov/pqc/ \ No newline at end of file diff --git a/pqcrypto-classicmceliece/build.rs b/pqcrypto-classicmceliece/build.rs deleted file mode 100644 index 8167ef3d..00000000 --- a/pqcrypto-classicmceliece/build.rs +++ /dev/null @@ -1,142 +0,0 @@ -extern crate cc; -extern crate glob; - -use std::env; -use std::path::{Path, PathBuf}; - -macro_rules! build_clean { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_kem", $variant, "clean"] - .iter() - .collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.c").to_str().unwrap()).unwrap(); - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_clean", $variant).as_str()); - }; -} - -macro_rules! build_avx2 { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_kem", $variant, "avx2"].iter().collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.[csS]").to_str().unwrap()).unwrap(); - let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); - if target_env == "msvc" { - builder.flag("/arch:AVX2"); - } else { - builder - .flag("-mavx2") - .flag("-mbmi2") - .flag("-mbmi") - .flag("-maes") - .flag("-mpopcnt") - .flag("-mpclmul"); - } - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_avx2", $variant).as_str()); - }; -} - -fn main() { - #[allow(unused_variables)] - let aes_enabled = env::var("CARGO_FEATURE_AES").is_ok(); - #[allow(unused_variables)] - let avx2_enabled = env::var("CARGO_FEATURE_AVX2").is_ok(); - #[allow(unused_variables)] - let neon_enabled = env::var("CARGO_FEATURE_NEON").is_ok(); - #[allow(unused_variables)] - let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); - #[allow(unused_variables)] - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - #[allow(unused_variables)] - let is_windows = target_os == "windows"; - #[allow(unused_variables)] - let is_macos = target_os == "macos"; - - build_clean!("mceliece348864"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("mceliece348864"); - } - build_clean!("mceliece348864f"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("mceliece348864f"); - } - build_clean!("mceliece460896"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("mceliece460896"); - } - build_clean!("mceliece460896f"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("mceliece460896f"); - } - build_clean!("mceliece6688128"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("mceliece6688128"); - } - build_clean!("mceliece6688128f"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("mceliece6688128f"); - } - build_clean!("mceliece6960119"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("mceliece6960119"); - } - build_clean!("mceliece6960119f"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("mceliece6960119f"); - } - build_clean!("mceliece8192128"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("mceliece8192128"); - } - build_clean!("mceliece8192128f"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("mceliece8192128f"); - } - - if target_arch == "x86_64" && avx2_enabled && !is_windows { - // Print enableing flag for AVX2 implementation - println!("cargo:rustc-cfg=enable_x86_avx2"); - } -} diff --git a/pqcrypto-classicmceliece/pqclean b/pqcrypto-classicmceliece/pqclean deleted file mode 120000 index 60b92d49..00000000 --- a/pqcrypto-classicmceliece/pqclean +++ /dev/null @@ -1 +0,0 @@ -../pqclean \ No newline at end of file diff --git a/pqcrypto-classicmceliece/src/ffi.rs b/pqcrypto-classicmceliece/src/ffi.rs deleted file mode 100644 index 615cbcb0..00000000 --- a/pqcrypto-classicmceliece/src/ffi.rs +++ /dev/null @@ -1,1322 +0,0 @@ -//! Foreign function interfaces -//! -//! This module defines the foreign function interface for the following -//! crypto implementations from PQClean: -//! -//! * mceliece348864 -//! * mceliece348864f -//! * mceliece460896 -//! * mceliece460896f -//! * mceliece6688128 -//! * mceliece6688128f -//! * mceliece6960119 -//! * mceliece6960119f -//! * mceliece8192128 -//! * mceliece8192128f -// This file has been generated from PQClean. -// Find the templates in pqcrypto-template -use libc::c_int; - -// ensures we link correctly -#[allow(unused_imports)] -use pqcrypto_internals::*; - -pub const PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 6492; -pub const PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 261120; -pub const PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 96; -pub const PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_BYTES: usize = 32; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE348864_AVX2_CRYPTO_SECRETKEYBYTES: usize = 6492; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE348864_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 261120; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE348864_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 96; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE348864_AVX2_CRYPTO_BYTES: usize = 32; - -pub const PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 6492; -pub const PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 261120; -pub const PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 96; -pub const PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_BYTES: usize = 32; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE348864F_AVX2_CRYPTO_SECRETKEYBYTES: usize = 6492; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE348864F_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 261120; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE348864F_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 96; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE348864F_AVX2_CRYPTO_BYTES: usize = 32; - -pub const PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 13608; -pub const PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 524160; -pub const PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 156; -pub const PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_BYTES: usize = 32; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE460896_AVX2_CRYPTO_SECRETKEYBYTES: usize = 13608; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE460896_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 524160; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE460896_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 156; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE460896_AVX2_CRYPTO_BYTES: usize = 32; - -pub const PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 13608; -pub const PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 524160; -pub const PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 156; -pub const PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_BYTES: usize = 32; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE460896F_AVX2_CRYPTO_SECRETKEYBYTES: usize = 13608; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE460896F_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 524160; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE460896F_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 156; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE460896F_AVX2_CRYPTO_BYTES: usize = 32; - -pub const PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 13932; -pub const PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1044992; -pub const PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 208; -pub const PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_BYTES: usize = 32; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6688128_AVX2_CRYPTO_SECRETKEYBYTES: usize = 13932; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6688128_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 1044992; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6688128_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 208; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6688128_AVX2_CRYPTO_BYTES: usize = 32; - -pub const PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 13932; -pub const PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1044992; -pub const PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 208; -pub const PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_BYTES: usize = 32; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6688128F_AVX2_CRYPTO_SECRETKEYBYTES: usize = 13932; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6688128F_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 1044992; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6688128F_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 208; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6688128F_AVX2_CRYPTO_BYTES: usize = 32; - -pub const PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 13948; -pub const PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1047319; -pub const PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 194; -pub const PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_BYTES: usize = 32; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6960119_AVX2_CRYPTO_SECRETKEYBYTES: usize = 13948; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6960119_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 1047319; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6960119_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 194; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6960119_AVX2_CRYPTO_BYTES: usize = 32; - -pub const PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 13948; -pub const PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1047319; -pub const PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 194; -pub const PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_BYTES: usize = 32; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6960119F_AVX2_CRYPTO_SECRETKEYBYTES: usize = 13948; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6960119F_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 1047319; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6960119F_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 194; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE6960119F_AVX2_CRYPTO_BYTES: usize = 32; - -pub const PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 14120; -pub const PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1357824; -pub const PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 208; -pub const PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_BYTES: usize = 32; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE8192128_AVX2_CRYPTO_SECRETKEYBYTES: usize = 14120; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE8192128_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 1357824; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE8192128_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 208; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE8192128_AVX2_CRYPTO_BYTES: usize = 32; - -pub const PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 14120; -pub const PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1357824; -pub const PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 208; -pub const PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_BYTES: usize = 32; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE8192128F_AVX2_CRYPTO_SECRETKEYBYTES: usize = 14120; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE8192128F_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 1357824; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE8192128F_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 208; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_MCELIECE8192128F_AVX2_CRYPTO_BYTES: usize = 32; - -#[link(name = "mceliece348864_clean")] -extern "C" { - pub fn PQCLEAN_MCELIECE348864_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_MCELIECE348864_CLEAN_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_MCELIECE348864_CLEAN_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "mceliece348864_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE348864_AVX2_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE348864_AVX2_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE348864_AVX2_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[link(name = "mceliece348864f_clean")] -extern "C" { - pub fn PQCLEAN_MCELIECE348864F_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_MCELIECE348864F_CLEAN_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_MCELIECE348864F_CLEAN_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "mceliece348864f_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE348864F_AVX2_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE348864F_AVX2_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE348864F_AVX2_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[link(name = "mceliece460896_clean")] -extern "C" { - pub fn PQCLEAN_MCELIECE460896_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_MCELIECE460896_CLEAN_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_MCELIECE460896_CLEAN_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "mceliece460896_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE460896_AVX2_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE460896_AVX2_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE460896_AVX2_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[link(name = "mceliece460896f_clean")] -extern "C" { - pub fn PQCLEAN_MCELIECE460896F_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_MCELIECE460896F_CLEAN_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_MCELIECE460896F_CLEAN_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "mceliece460896f_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE460896F_AVX2_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE460896F_AVX2_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE460896F_AVX2_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[link(name = "mceliece6688128_clean")] -extern "C" { - pub fn PQCLEAN_MCELIECE6688128_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_MCELIECE6688128_CLEAN_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_MCELIECE6688128_CLEAN_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "mceliece6688128_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6688128_AVX2_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6688128_AVX2_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6688128_AVX2_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[link(name = "mceliece6688128f_clean")] -extern "C" { - pub fn PQCLEAN_MCELIECE6688128F_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_MCELIECE6688128F_CLEAN_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_MCELIECE6688128F_CLEAN_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "mceliece6688128f_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6688128F_AVX2_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6688128F_AVX2_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6688128F_AVX2_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[link(name = "mceliece6960119_clean")] -extern "C" { - pub fn PQCLEAN_MCELIECE6960119_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_MCELIECE6960119_CLEAN_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_MCELIECE6960119_CLEAN_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "mceliece6960119_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6960119_AVX2_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6960119_AVX2_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6960119_AVX2_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[link(name = "mceliece6960119f_clean")] -extern "C" { - pub fn PQCLEAN_MCELIECE6960119F_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_MCELIECE6960119F_CLEAN_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_MCELIECE6960119F_CLEAN_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "mceliece6960119f_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6960119F_AVX2_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6960119F_AVX2_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE6960119F_AVX2_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[link(name = "mceliece8192128_clean")] -extern "C" { - pub fn PQCLEAN_MCELIECE8192128_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_MCELIECE8192128_CLEAN_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_MCELIECE8192128_CLEAN_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "mceliece8192128_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE8192128_AVX2_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE8192128_AVX2_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE8192128_AVX2_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[link(name = "mceliece8192128f_clean")] -extern "C" { - pub fn PQCLEAN_MCELIECE8192128F_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_MCELIECE8192128F_CLEAN_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_MCELIECE8192128F_CLEAN_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "mceliece8192128f_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE8192128F_AVX2_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE8192128F_AVX2_crypto_kem_enc( - ct: *mut u8, - ss: *mut u8, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_MCELIECE8192128F_AVX2_crypto_kem_dec( - ss: *mut u8, - ct: *const u8, - sk: *const u8, - ) -> c_int; -} - -#[cfg(test)] -mod test_mceliece348864_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE348864_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE348864_CLEAN_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE348864_CLEAN_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_mceliece348864_avx2 { - use super::*; - use alloc::vec; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE348864_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE348864_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE348864_AVX2_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE348864_AVX2_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE348864_AVX2_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE348864_AVX2_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE348864_AVX2_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE348864_AVX2_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_mceliece348864f_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE348864F_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE348864F_CLEAN_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE348864F_CLEAN_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_mceliece348864f_avx2 { - use super::*; - use alloc::vec; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE348864F_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE348864F_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE348864F_AVX2_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE348864F_AVX2_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE348864F_AVX2_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE348864F_AVX2_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE348864F_AVX2_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE348864F_AVX2_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_mceliece460896_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE460896_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE460896_CLEAN_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE460896_CLEAN_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_mceliece460896_avx2 { - use super::*; - use alloc::vec; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE460896_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE460896_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE460896_AVX2_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE460896_AVX2_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE460896_AVX2_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE460896_AVX2_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE460896_AVX2_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE460896_AVX2_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_mceliece460896f_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE460896F_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE460896F_CLEAN_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE460896F_CLEAN_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_mceliece460896f_avx2 { - use super::*; - use alloc::vec; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE460896F_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE460896F_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE460896F_AVX2_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE460896F_AVX2_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE460896F_AVX2_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE460896F_AVX2_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE460896F_AVX2_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE460896F_AVX2_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_mceliece6688128_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128_CLEAN_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128_CLEAN_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_mceliece6688128_avx2 { - use super::*; - use alloc::vec; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE6688128_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE6688128_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE6688128_AVX2_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE6688128_AVX2_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE6688128_AVX2_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128_AVX2_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128_AVX2_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128_AVX2_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_mceliece6688128f_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128F_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128F_CLEAN_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128F_CLEAN_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_mceliece6688128f_avx2 { - use super::*; - use alloc::vec; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE6688128F_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE6688128F_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE6688128F_AVX2_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE6688128F_AVX2_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE6688128F_AVX2_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128F_AVX2_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128F_AVX2_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6688128F_AVX2_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_mceliece6960119_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119_CLEAN_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119_CLEAN_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_mceliece6960119_avx2 { - use super::*; - use alloc::vec; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE6960119_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE6960119_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE6960119_AVX2_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE6960119_AVX2_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE6960119_AVX2_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119_AVX2_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119_AVX2_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119_AVX2_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_mceliece6960119f_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119F_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119F_CLEAN_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119F_CLEAN_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_mceliece6960119f_avx2 { - use super::*; - use alloc::vec; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE6960119F_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE6960119F_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE6960119F_AVX2_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE6960119F_AVX2_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE6960119F_AVX2_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119F_AVX2_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119F_AVX2_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE6960119F_AVX2_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_mceliece8192128_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128_CLEAN_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128_CLEAN_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_mceliece8192128_avx2 { - use super::*; - use alloc::vec; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE8192128_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE8192128_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE8192128_AVX2_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE8192128_AVX2_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE8192128_AVX2_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128_AVX2_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128_AVX2_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128_AVX2_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_mceliece8192128f_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128F_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128F_CLEAN_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128F_CLEAN_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_mceliece8192128f_avx2 { - use super::*; - use alloc::vec; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut pk = vec![0u8; PQCLEAN_MCELIECE8192128F_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_MCELIECE8192128F_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_MCELIECE8192128F_AVX2_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_MCELIECE8192128F_AVX2_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_MCELIECE8192128F_AVX2_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128F_AVX2_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128F_AVX2_crypto_kem_enc( - ct.as_mut_ptr(), - ss1.as_mut_ptr(), - pk.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_MCELIECE8192128F_AVX2_crypto_kem_dec( - ss2.as_mut_ptr(), - ct.as_ptr(), - sk.as_ptr() - ) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} diff --git a/pqcrypto-classicmceliece/src/lib.rs b/pqcrypto-classicmceliece/src/lib.rs deleted file mode 100644 index e861cd69..00000000 --- a/pqcrypto-classicmceliece/src/lib.rs +++ /dev/null @@ -1,114 +0,0 @@ -//! # classicmceliece -//! -//! This crate provides bindings to and wrappers around the following -//! implementations from [PQClean][pqc]: -//! -//! * mceliece348864 - clean -//! * mceliece348864f - clean -//! * mceliece460896 - clean -//! * mceliece460896f - clean -//! * mceliece6688128 - clean -//! * mceliece6688128f - clean -//! * mceliece6960119 - clean -//! * mceliece6960119f - clean -//! * mceliece8192128 - clean -//! * mceliece8192128f - clean -//! -//! [pqc]: https://github.com/pqclean/pqclean/ -//! -//! # Notes -//! This implementation requires a lot of stack space. You need to specify -//! ``RUST_MIN_STACK=800000000``, probably. - -#![no_std] -#![allow(clippy::len_without_is_empty)] - -// For no-std vectors -extern crate alloc; - -// For tests -#[cfg(feature = "std")] -extern crate std; - -pub mod ffi; -pub mod mceliece348864; -pub mod mceliece348864f; -pub mod mceliece460896; -pub mod mceliece460896f; -pub mod mceliece6688128; -pub mod mceliece6688128f; -pub mod mceliece6960119; -pub mod mceliece6960119f; -pub mod mceliece8192128; -pub mod mceliece8192128f; - -pub use crate::mceliece348864::{ - ciphertext_bytes as mceliece348864_ciphertext_bytes, decapsulate as mceliece348864_decapsulate, - encapsulate as mceliece348864_encapsulate, keypair as mceliece348864_keypair, - public_key_bytes as mceliece348864_public_key_bytes, - secret_key_bytes as mceliece348864_secret_key_bytes, - shared_secret_bytes as mceliece348864_shared_secret_bytes, -}; -pub use crate::mceliece348864f::{ - ciphertext_bytes as mceliece348864f_ciphertext_bytes, - decapsulate as mceliece348864f_decapsulate, encapsulate as mceliece348864f_encapsulate, - keypair as mceliece348864f_keypair, public_key_bytes as mceliece348864f_public_key_bytes, - secret_key_bytes as mceliece348864f_secret_key_bytes, - shared_secret_bytes as mceliece348864f_shared_secret_bytes, -}; -pub use crate::mceliece460896::{ - ciphertext_bytes as mceliece460896_ciphertext_bytes, decapsulate as mceliece460896_decapsulate, - encapsulate as mceliece460896_encapsulate, keypair as mceliece460896_keypair, - public_key_bytes as mceliece460896_public_key_bytes, - secret_key_bytes as mceliece460896_secret_key_bytes, - shared_secret_bytes as mceliece460896_shared_secret_bytes, -}; -pub use crate::mceliece460896f::{ - ciphertext_bytes as mceliece460896f_ciphertext_bytes, - decapsulate as mceliece460896f_decapsulate, encapsulate as mceliece460896f_encapsulate, - keypair as mceliece460896f_keypair, public_key_bytes as mceliece460896f_public_key_bytes, - secret_key_bytes as mceliece460896f_secret_key_bytes, - shared_secret_bytes as mceliece460896f_shared_secret_bytes, -}; -pub use crate::mceliece6688128::{ - ciphertext_bytes as mceliece6688128_ciphertext_bytes, - decapsulate as mceliece6688128_decapsulate, encapsulate as mceliece6688128_encapsulate, - keypair as mceliece6688128_keypair, public_key_bytes as mceliece6688128_public_key_bytes, - secret_key_bytes as mceliece6688128_secret_key_bytes, - shared_secret_bytes as mceliece6688128_shared_secret_bytes, -}; -pub use crate::mceliece6688128f::{ - ciphertext_bytes as mceliece6688128f_ciphertext_bytes, - decapsulate as mceliece6688128f_decapsulate, encapsulate as mceliece6688128f_encapsulate, - keypair as mceliece6688128f_keypair, public_key_bytes as mceliece6688128f_public_key_bytes, - secret_key_bytes as mceliece6688128f_secret_key_bytes, - shared_secret_bytes as mceliece6688128f_shared_secret_bytes, -}; -pub use crate::mceliece6960119::{ - ciphertext_bytes as mceliece6960119_ciphertext_bytes, - decapsulate as mceliece6960119_decapsulate, encapsulate as mceliece6960119_encapsulate, - keypair as mceliece6960119_keypair, public_key_bytes as mceliece6960119_public_key_bytes, - secret_key_bytes as mceliece6960119_secret_key_bytes, - shared_secret_bytes as mceliece6960119_shared_secret_bytes, -}; -pub use crate::mceliece6960119f::{ - ciphertext_bytes as mceliece6960119f_ciphertext_bytes, - decapsulate as mceliece6960119f_decapsulate, encapsulate as mceliece6960119f_encapsulate, - keypair as mceliece6960119f_keypair, public_key_bytes as mceliece6960119f_public_key_bytes, - secret_key_bytes as mceliece6960119f_secret_key_bytes, - shared_secret_bytes as mceliece6960119f_shared_secret_bytes, -}; -pub use crate::mceliece8192128::{ - ciphertext_bytes as mceliece8192128_ciphertext_bytes, - decapsulate as mceliece8192128_decapsulate, encapsulate as mceliece8192128_encapsulate, - keypair as mceliece8192128_keypair, public_key_bytes as mceliece8192128_public_key_bytes, - secret_key_bytes as mceliece8192128_secret_key_bytes, - shared_secret_bytes as mceliece8192128_shared_secret_bytes, -}; -pub use crate::mceliece8192128f::{ - ciphertext_bytes as mceliece8192128f_ciphertext_bytes, - decapsulate as mceliece8192128f_decapsulate, encapsulate as mceliece8192128f_encapsulate, - keypair as mceliece8192128f_keypair, public_key_bytes as mceliece8192128f_public_key_bytes, - secret_key_bytes as mceliece8192128f_secret_key_bytes, - shared_secret_bytes as mceliece8192128f_shared_secret_bytes, -}; diff --git a/pqcrypto-classicmceliece/src/mceliece348864.rs b/pqcrypto-classicmceliece/src/mceliece348864.rs deleted file mode 100644 index bb24e41a..00000000 --- a/pqcrypto-classicmceliece/src/mceliece348864.rs +++ /dev/null @@ -1,198 +0,0 @@ -//! mceliece348864 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-classicmceliece -//! use pqcrypto_classicmceliece::mceliece348864::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::mceliece348864::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_SECRETKEYBYTES -); -simple_struct!( - Ciphertext, - ffi::PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_CIPHERTEXTBYTES -); -simple_struct!(SharedSecret, ffi::PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_BYTES); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_MCELIECE348864_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a mceliece348864 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_MCELIECE348864_AVX2_crypto_kem_keypair); - } - } - gen_keypair!(PQCLEAN_MCELIECE348864_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a mceliece348864 public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return encap!(PQCLEAN_MCELIECE348864_AVX2_crypto_kem_enc, pk); - } - } - encap!(PQCLEAN_MCELIECE348864_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received mceliece348864 ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return decap!(PQCLEAN_MCELIECE348864_AVX2_crypto_kem_dec, ct, sk); - } - } - decap!(PQCLEAN_MCELIECE348864_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-classicmceliece/src/mceliece348864f.rs b/pqcrypto-classicmceliece/src/mceliece348864f.rs deleted file mode 100644 index 56b463da..00000000 --- a/pqcrypto-classicmceliece/src/mceliece348864f.rs +++ /dev/null @@ -1,201 +0,0 @@ -//! mceliece348864f -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-classicmceliece -//! use pqcrypto_classicmceliece::mceliece348864f::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::mceliece348864f::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_SECRETKEYBYTES -); -simple_struct!( - Ciphertext, - ffi::PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_CIPHERTEXTBYTES -); -simple_struct!( - SharedSecret, - ffi::PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_BYTES -); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_MCELIECE348864F_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a mceliece348864f keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_MCELIECE348864F_AVX2_crypto_kem_keypair); - } - } - gen_keypair!(PQCLEAN_MCELIECE348864F_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a mceliece348864f public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return encap!(PQCLEAN_MCELIECE348864F_AVX2_crypto_kem_enc, pk); - } - } - encap!(PQCLEAN_MCELIECE348864F_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received mceliece348864f ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return decap!(PQCLEAN_MCELIECE348864F_AVX2_crypto_kem_dec, ct, sk); - } - } - decap!(PQCLEAN_MCELIECE348864F_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-classicmceliece/src/mceliece460896.rs b/pqcrypto-classicmceliece/src/mceliece460896.rs deleted file mode 100644 index 09e038d8..00000000 --- a/pqcrypto-classicmceliece/src/mceliece460896.rs +++ /dev/null @@ -1,198 +0,0 @@ -//! mceliece460896 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-classicmceliece -//! use pqcrypto_classicmceliece::mceliece460896::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::mceliece460896::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_SECRETKEYBYTES -); -simple_struct!( - Ciphertext, - ffi::PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_CIPHERTEXTBYTES -); -simple_struct!(SharedSecret, ffi::PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_BYTES); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_MCELIECE460896_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a mceliece460896 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_MCELIECE460896_AVX2_crypto_kem_keypair); - } - } - gen_keypair!(PQCLEAN_MCELIECE460896_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a mceliece460896 public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return encap!(PQCLEAN_MCELIECE460896_AVX2_crypto_kem_enc, pk); - } - } - encap!(PQCLEAN_MCELIECE460896_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received mceliece460896 ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return decap!(PQCLEAN_MCELIECE460896_AVX2_crypto_kem_dec, ct, sk); - } - } - decap!(PQCLEAN_MCELIECE460896_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-classicmceliece/src/mceliece460896f.rs b/pqcrypto-classicmceliece/src/mceliece460896f.rs deleted file mode 100644 index 7bb9a28b..00000000 --- a/pqcrypto-classicmceliece/src/mceliece460896f.rs +++ /dev/null @@ -1,201 +0,0 @@ -//! mceliece460896f -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-classicmceliece -//! use pqcrypto_classicmceliece::mceliece460896f::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::mceliece460896f::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_SECRETKEYBYTES -); -simple_struct!( - Ciphertext, - ffi::PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_CIPHERTEXTBYTES -); -simple_struct!( - SharedSecret, - ffi::PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_BYTES -); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_MCELIECE460896F_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a mceliece460896f keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_MCELIECE460896F_AVX2_crypto_kem_keypair); - } - } - gen_keypair!(PQCLEAN_MCELIECE460896F_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a mceliece460896f public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return encap!(PQCLEAN_MCELIECE460896F_AVX2_crypto_kem_enc, pk); - } - } - encap!(PQCLEAN_MCELIECE460896F_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received mceliece460896f ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return decap!(PQCLEAN_MCELIECE460896F_AVX2_crypto_kem_dec, ct, sk); - } - } - decap!(PQCLEAN_MCELIECE460896F_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-classicmceliece/src/mceliece6688128.rs b/pqcrypto-classicmceliece/src/mceliece6688128.rs deleted file mode 100644 index 673aabd5..00000000 --- a/pqcrypto-classicmceliece/src/mceliece6688128.rs +++ /dev/null @@ -1,201 +0,0 @@ -//! mceliece6688128 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ```no_run -//! // if using pqcrypto-classicmceliece -//! use pqcrypto_classicmceliece::mceliece6688128::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::mceliece6688128::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_SECRETKEYBYTES -); -simple_struct!( - Ciphertext, - ffi::PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_CIPHERTEXTBYTES -); -simple_struct!( - SharedSecret, - ffi::PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_BYTES -); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6688128_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a mceliece6688128 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_MCELIECE6688128_AVX2_crypto_kem_keypair); - } - } - gen_keypair!(PQCLEAN_MCELIECE6688128_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a mceliece6688128 public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return encap!(PQCLEAN_MCELIECE6688128_AVX2_crypto_kem_enc, pk); - } - } - encap!(PQCLEAN_MCELIECE6688128_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received mceliece6688128 ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return decap!(PQCLEAN_MCELIECE6688128_AVX2_crypto_kem_dec, ct, sk); - } - } - decap!(PQCLEAN_MCELIECE6688128_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-classicmceliece/src/mceliece6688128f.rs b/pqcrypto-classicmceliece/src/mceliece6688128f.rs deleted file mode 100644 index ffbffcdd..00000000 --- a/pqcrypto-classicmceliece/src/mceliece6688128f.rs +++ /dev/null @@ -1,201 +0,0 @@ -//! mceliece6688128f -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ```no_run -//! // if using pqcrypto-classicmceliece -//! use pqcrypto_classicmceliece::mceliece6688128f::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::mceliece6688128f::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_SECRETKEYBYTES -); -simple_struct!( - Ciphertext, - ffi::PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_CIPHERTEXTBYTES -); -simple_struct!( - SharedSecret, - ffi::PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_BYTES -); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6688128F_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a mceliece6688128f keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_MCELIECE6688128F_AVX2_crypto_kem_keypair); - } - } - gen_keypair!(PQCLEAN_MCELIECE6688128F_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a mceliece6688128f public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return encap!(PQCLEAN_MCELIECE6688128F_AVX2_crypto_kem_enc, pk); - } - } - encap!(PQCLEAN_MCELIECE6688128F_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received mceliece6688128f ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return decap!(PQCLEAN_MCELIECE6688128F_AVX2_crypto_kem_dec, ct, sk); - } - } - decap!(PQCLEAN_MCELIECE6688128F_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-classicmceliece/src/mceliece6960119.rs b/pqcrypto-classicmceliece/src/mceliece6960119.rs deleted file mode 100644 index 83a3c996..00000000 --- a/pqcrypto-classicmceliece/src/mceliece6960119.rs +++ /dev/null @@ -1,201 +0,0 @@ -//! mceliece6960119 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ```no_run -//! // if using pqcrypto-classicmceliece -//! use pqcrypto_classicmceliece::mceliece6960119::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::mceliece6960119::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_SECRETKEYBYTES -); -simple_struct!( - Ciphertext, - ffi::PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_CIPHERTEXTBYTES -); -simple_struct!( - SharedSecret, - ffi::PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_BYTES -); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6960119_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a mceliece6960119 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_MCELIECE6960119_AVX2_crypto_kem_keypair); - } - } - gen_keypair!(PQCLEAN_MCELIECE6960119_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a mceliece6960119 public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return encap!(PQCLEAN_MCELIECE6960119_AVX2_crypto_kem_enc, pk); - } - } - encap!(PQCLEAN_MCELIECE6960119_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received mceliece6960119 ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return decap!(PQCLEAN_MCELIECE6960119_AVX2_crypto_kem_dec, ct, sk); - } - } - decap!(PQCLEAN_MCELIECE6960119_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-classicmceliece/src/mceliece6960119f.rs b/pqcrypto-classicmceliece/src/mceliece6960119f.rs deleted file mode 100644 index 8f1e7207..00000000 --- a/pqcrypto-classicmceliece/src/mceliece6960119f.rs +++ /dev/null @@ -1,201 +0,0 @@ -//! mceliece6960119f -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ```no_run -//! // if using pqcrypto-classicmceliece -//! use pqcrypto_classicmceliece::mceliece6960119f::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::mceliece6960119f::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_SECRETKEYBYTES -); -simple_struct!( - Ciphertext, - ffi::PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_CIPHERTEXTBYTES -); -simple_struct!( - SharedSecret, - ffi::PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_BYTES -); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_MCELIECE6960119F_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a mceliece6960119f keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_MCELIECE6960119F_AVX2_crypto_kem_keypair); - } - } - gen_keypair!(PQCLEAN_MCELIECE6960119F_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a mceliece6960119f public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return encap!(PQCLEAN_MCELIECE6960119F_AVX2_crypto_kem_enc, pk); - } - } - encap!(PQCLEAN_MCELIECE6960119F_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received mceliece6960119f ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return decap!(PQCLEAN_MCELIECE6960119F_AVX2_crypto_kem_dec, ct, sk); - } - } - decap!(PQCLEAN_MCELIECE6960119F_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-classicmceliece/src/mceliece8192128.rs b/pqcrypto-classicmceliece/src/mceliece8192128.rs deleted file mode 100644 index 6820380b..00000000 --- a/pqcrypto-classicmceliece/src/mceliece8192128.rs +++ /dev/null @@ -1,201 +0,0 @@ -//! mceliece8192128 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ```no_run -//! // if using pqcrypto-classicmceliece -//! use pqcrypto_classicmceliece::mceliece8192128::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::mceliece8192128::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_SECRETKEYBYTES -); -simple_struct!( - Ciphertext, - ffi::PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_CIPHERTEXTBYTES -); -simple_struct!( - SharedSecret, - ffi::PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_BYTES -); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_MCELIECE8192128_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a mceliece8192128 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_MCELIECE8192128_AVX2_crypto_kem_keypair); - } - } - gen_keypair!(PQCLEAN_MCELIECE8192128_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a mceliece8192128 public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return encap!(PQCLEAN_MCELIECE8192128_AVX2_crypto_kem_enc, pk); - } - } - encap!(PQCLEAN_MCELIECE8192128_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received mceliece8192128 ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return decap!(PQCLEAN_MCELIECE8192128_AVX2_crypto_kem_dec, ct, sk); - } - } - decap!(PQCLEAN_MCELIECE8192128_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-classicmceliece/src/mceliece8192128f.rs b/pqcrypto-classicmceliece/src/mceliece8192128f.rs deleted file mode 100644 index e247b5c0..00000000 --- a/pqcrypto-classicmceliece/src/mceliece8192128f.rs +++ /dev/null @@ -1,201 +0,0 @@ -//! mceliece8192128f -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ```no_run -//! // if using pqcrypto-classicmceliece -//! use pqcrypto_classicmceliece::mceliece8192128f::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::mceliece8192128f::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_SECRETKEYBYTES -); -simple_struct!( - Ciphertext, - ffi::PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_CIPHERTEXTBYTES -); -simple_struct!( - SharedSecret, - ffi::PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_BYTES -); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_MCELIECE8192128F_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a mceliece8192128f keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_MCELIECE8192128F_AVX2_crypto_kem_keypair); - } - } - gen_keypair!(PQCLEAN_MCELIECE8192128F_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a mceliece8192128f public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return encap!(PQCLEAN_MCELIECE8192128F_AVX2_crypto_kem_enc, pk); - } - } - encap!(PQCLEAN_MCELIECE8192128F_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received mceliece8192128f ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return decap!(PQCLEAN_MCELIECE8192128F_AVX2_crypto_kem_dec, ct, sk); - } - } - decap!(PQCLEAN_MCELIECE8192128F_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-dilithium/Cargo.toml b/pqcrypto-dilithium/Cargo.toml deleted file mode 100644 index 835d2e40..00000000 --- a/pqcrypto-dilithium/Cargo.toml +++ /dev/null @@ -1,38 +0,0 @@ -[package] -name = "pqcrypto-dilithium" -description = "Post-Quantum Signature Scheme dilithium" -readme = "README.md" -version = "0.5.0" -authors = ["Thom Wiggers "] -edition = "2021" -license = "MIT OR Apache-2.0" -homepage = "https://github.com/rustpq/" -repository = "https://github.com/rustpq/pqcrypto/" -keywords = ["cryptography", "post-quantum", "security"] -categories = ["cryptography", "no-std"] - -[dependencies] -pqcrypto-internals = { path = "../pqcrypto-internals", version = "0.2" } -pqcrypto-traits = { path = "../pqcrypto-traits", version = "0.3.5", default-features = false } -libc = "0.2.0" -serde = { version = "1.0", features = ["derive"], optional = true } -serde-big-array = { version = "0.5.1", optional = true } - -[features] -default = ["avx2", "neon", "std"] -avx2 = ["std"] -neon = ["std"] -std = ["pqcrypto-traits/std"] -serialization = ["serde", "serde-big-array"] - -[dev-dependencies] -rand = "0.8.5" - -[build-dependencies] -cc = { version = "1.0", features = ["parallel"] } -glob = "0.3.0" - -[badges] -travis-ci = { repository = "rustpq/pqcrypto", branch = "master" } -maintenance = { status = "actively-developed" } - diff --git a/pqcrypto-dilithium/README.md b/pqcrypto-dilithium/README.md deleted file mode 100644 index 2a6df7bb..00000000 --- a/pqcrypto-dilithium/README.md +++ /dev/null @@ -1,45 +0,0 @@ -# dilithium - - -This crate contains bindings to the C implementations of the following schemes, -from [PQClean][pqclean]. - -This project packages Post-Quantum cryptographic algorithms that participate in -the [NIST PQC standardization effort][nistpqc]. It is currently a collection of -wrappers around C implementations from the [PQClean][pqclean] project. - -## Serialization - -If you want `serde` support, enable the `serialization` feature. - -## Included implementations from PQClean - -Below is a list of the included schemes and the corresponding implementations -sourced from [PQClean][pqclean]. The "default" implementation is used in the -Rust-friendly interface, alternative implementations are exposed as ``ffi`` -methods only. - -* ``dilithium2`` - * ``clean`` - * ``avx2`` (if supported) - * ``aarch64`` (if supported) -* ``dilithium3`` - * ``clean`` - * ``avx2`` (if supported) - * ``aarch64`` (if supported) -* ``dilithium5`` - * ``clean`` - * ``avx2`` (if supported) - * ``aarch64`` (if supported) - - -## License - -The wrappers and wrapper generation scripts in this project are covered by the -MIT or Apache 2.0 licenses, at your choice. - -The implementations we link to are not, however. Please see the [PQClean][pqclean] -project for the appropriate licenses. - -[pqclean]: https://github.com/PQClean/PQClean/ -[nistpqc]: https://nist.gov/pqc/ \ No newline at end of file diff --git a/pqcrypto-dilithium/build.rs b/pqcrypto-dilithium/build.rs deleted file mode 100644 index ef125f52..00000000 --- a/pqcrypto-dilithium/build.rs +++ /dev/null @@ -1,162 +0,0 @@ -extern crate cc; -extern crate glob; - -use std::env; -use std::path::{Path, PathBuf}; - -macro_rules! build_clean { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_sign", $variant, "clean"] - .iter() - .collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.c").to_str().unwrap()).unwrap(); - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_clean", $variant).as_str()); - }; -} - -macro_rules! build_avx2 { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_sign", $variant, "avx2"] - .iter() - .collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.[csS]").to_str().unwrap()).unwrap(); - let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); - if target_env == "msvc" { - builder.flag("/arch:AVX2"); - } else { - builder - .flag("-mavx2") - .flag("-mbmi2") - .flag("-mbmi") - .flag("-maes") - .flag("-mpopcnt") - .flag("-mpclmul"); - } - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_avx2", $variant).as_str()); - }; -} - -macro_rules! build_aarch64 { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_sign", $variant, "aarch64"] - .iter() - .collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.[csS]").to_str().unwrap()).unwrap(); - builder.flag("-march=armv8-a"); - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_aarch64", $variant).as_str()); - }; -} - -fn main() { - #[allow(unused_variables)] - let aes_enabled = env::var("CARGO_FEATURE_AES").is_ok(); - #[allow(unused_variables)] - let avx2_enabled = env::var("CARGO_FEATURE_AVX2").is_ok(); - #[allow(unused_variables)] - let neon_enabled = env::var("CARGO_FEATURE_NEON").is_ok(); - #[allow(unused_variables)] - let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); - #[allow(unused_variables)] - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - #[allow(unused_variables)] - let is_windows = target_os == "windows"; - #[allow(unused_variables)] - let is_macos = target_os == "macos"; - - build_clean!("dilithium2"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("dilithium2"); - } - if target_arch == "aarch64" && neon_enabled { - build_aarch64!("dilithium2"); - } - build_clean!("dilithium3"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("dilithium3"); - } - if target_arch == "aarch64" && neon_enabled { - build_aarch64!("dilithium3"); - } - build_clean!("dilithium5"); - if target_arch == "x86_64" && avx2_enabled && !is_windows { - build_avx2!("dilithium5"); - } - if target_arch == "aarch64" && neon_enabled { - build_aarch64!("dilithium5"); - } - - if target_arch == "x86_64" && avx2_enabled && !is_windows { - // Print enableing flag for AVX2 implementation - println!("cargo:rustc-cfg=enable_x86_avx2"); - } - if target_arch == "aarch64" && neon_enabled { - // Print enableing flag for AARCH64 implementation - println!("cargo:rustc-cfg=enable_aarch64_neon"); - } -} diff --git a/pqcrypto-dilithium/pqclean b/pqcrypto-dilithium/pqclean deleted file mode 120000 index 60b92d49..00000000 --- a/pqcrypto-dilithium/pqclean +++ /dev/null @@ -1 +0,0 @@ -../pqclean \ No newline at end of file diff --git a/pqcrypto-dilithium/src/dilithium2.rs b/pqcrypto-dilithium/src/dilithium2.rs deleted file mode 100644 index 8af2a129..00000000 --- a/pqcrypto-dilithium/src/dilithium2.rs +++ /dev/null @@ -1,391 +0,0 @@ -//! dilithium2 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-dilithium -//! use pqcrypto_dilithium::dilithium2::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::dilithium2::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature([0u8; ffi::PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_BYTES], 0) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a dilithium2 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_DILITHIUM2_AVX2_crypto_sign_keypair); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - // always use AArch64 code, when target is detected as all AArch64 targets have NEON - // support, and std::is_aarch64_feature_detected!("neon") works only with Rust nightly at - // the moment - if true { - return gen_keypair!(PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_DILITHIUM2_AVX2_crypto_sign, msg, sk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return gen_signature!(PQCLEAN_DILITHIUM2_AARCH64_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_DILITHIUM2_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_DILITHIUM2_AVX2_crypto_sign_open, sm, pk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return open_signed!(PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!(PQCLEAN_DILITHIUM2_AVX2_crypto_sign_signature, msg, sk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return detached_signature!(PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_signature, msg, sk); - } - } - detached_signature!(PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_signature, msg, sk) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!(PQCLEAN_DILITHIUM2_AVX2_crypto_sign_verify, sig, msg, pk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return verify_detached_sig!( - PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!(PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_verify, sig, msg, pk) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-dilithium/src/dilithium3.rs b/pqcrypto-dilithium/src/dilithium3.rs deleted file mode 100644 index e4500588..00000000 --- a/pqcrypto-dilithium/src/dilithium3.rs +++ /dev/null @@ -1,391 +0,0 @@ -//! dilithium3 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-dilithium -//! use pqcrypto_dilithium::dilithium3::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::dilithium3::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature([0u8; ffi::PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_BYTES], 0) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a dilithium3 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_DILITHIUM3_AVX2_crypto_sign_keypair); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - // always use AArch64 code, when target is detected as all AArch64 targets have NEON - // support, and std::is_aarch64_feature_detected!("neon") works only with Rust nightly at - // the moment - if true { - return gen_keypair!(PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_DILITHIUM3_AVX2_crypto_sign, msg, sk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return gen_signature!(PQCLEAN_DILITHIUM3_AARCH64_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_DILITHIUM3_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_DILITHIUM3_AVX2_crypto_sign_open, sm, pk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return open_signed!(PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!(PQCLEAN_DILITHIUM3_AVX2_crypto_sign_signature, msg, sk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return detached_signature!(PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_signature, msg, sk); - } - } - detached_signature!(PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_signature, msg, sk) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!(PQCLEAN_DILITHIUM3_AVX2_crypto_sign_verify, sig, msg, pk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return verify_detached_sig!( - PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!(PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_verify, sig, msg, pk) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-dilithium/src/dilithium5.rs b/pqcrypto-dilithium/src/dilithium5.rs deleted file mode 100644 index 7f27bb60..00000000 --- a/pqcrypto-dilithium/src/dilithium5.rs +++ /dev/null @@ -1,391 +0,0 @@ -//! dilithium5 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-dilithium -//! use pqcrypto_dilithium::dilithium5::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::dilithium5::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature([0u8; ffi::PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_BYTES], 0) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a dilithium5 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_DILITHIUM5_AVX2_crypto_sign_keypair); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - // always use AArch64 code, when target is detected as all AArch64 targets have NEON - // support, and std::is_aarch64_feature_detected!("neon") works only with Rust nightly at - // the moment - if true { - return gen_keypair!(PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_DILITHIUM5_AVX2_crypto_sign, msg, sk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return gen_signature!(PQCLEAN_DILITHIUM5_AARCH64_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_DILITHIUM5_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_DILITHIUM5_AVX2_crypto_sign_open, sm, pk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return open_signed!(PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!(PQCLEAN_DILITHIUM5_AVX2_crypto_sign_signature, msg, sk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return detached_signature!(PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_signature, msg, sk); - } - } - detached_signature!(PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_signature, msg, sk) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!(PQCLEAN_DILITHIUM5_AVX2_crypto_sign_verify, sig, msg, pk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return verify_detached_sig!( - PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!(PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_verify, sig, msg, pk) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-dilithium/src/ffi.rs b/pqcrypto-dilithium/src/ffi.rs deleted file mode 100644 index b5ef3353..00000000 --- a/pqcrypto-dilithium/src/ffi.rs +++ /dev/null @@ -1,1467 +0,0 @@ -//! Foreign function interfaces -//! -//! This module defines the foreign function interface for the following -//! crypto implementations from PQClean: -//! -//! * dilithium2 -//! * dilithium3 -//! * dilithium5 -// This file has been generated from PQClean. -// Find the templates in pqcrypto-template -use libc::c_int; - -// ensures we link correctly -#[allow(unused_imports)] -use pqcrypto_internals::*; - -pub const PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 2560; -pub const PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1312; -pub const PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_BYTES: usize = 2420; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_DILITHIUM2_AVX2_CRYPTO_SECRETKEYBYTES: usize = 2560; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_DILITHIUM2_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 1312; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_DILITHIUM2_AVX2_CRYPTO_BYTES: usize = 2420; - -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_SECRETKEYBYTES: usize = 2560; -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_PUBLICKEYBYTES: usize = 1312; -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_BYTES: usize = 2420; - -pub const PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 4032; -pub const PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1952; -pub const PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_BYTES: usize = 3309; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_DILITHIUM3_AVX2_CRYPTO_SECRETKEYBYTES: usize = 4032; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_DILITHIUM3_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 1952; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_DILITHIUM3_AVX2_CRYPTO_BYTES: usize = 3309; - -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_SECRETKEYBYTES: usize = 4032; -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_PUBLICKEYBYTES: usize = 1952; -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_BYTES: usize = 3309; - -pub const PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 4896; -pub const PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 2592; -pub const PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_BYTES: usize = 4627; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_DILITHIUM5_AVX2_CRYPTO_SECRETKEYBYTES: usize = 4896; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_DILITHIUM5_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 2592; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_DILITHIUM5_AVX2_CRYPTO_BYTES: usize = 4627; - -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_SECRETKEYBYTES: usize = 4896; -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_PUBLICKEYBYTES: usize = 2592; -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_BYTES: usize = 4627; - -#[link(name = "dilithium2_clean")] -extern "C" { - pub fn PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_DILITHIUM2_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "dilithium2_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM2_AVX2_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM2_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM2_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM2_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM2_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_aarch64_neon)] -#[link(name = "dilithium2_aarch64")] -extern "C" { - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM2_AARCH64_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "dilithium3_clean")] -extern "C" { - pub fn PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_DILITHIUM3_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "dilithium3_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM3_AVX2_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM3_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM3_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM3_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM3_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_aarch64_neon)] -#[link(name = "dilithium3_aarch64")] -extern "C" { - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM3_AARCH64_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "dilithium5_clean")] -extern "C" { - pub fn PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_DILITHIUM5_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "dilithium5_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM5_AVX2_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM5_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM5_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM5_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_DILITHIUM5_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_aarch64_neon)] -#[link(name = "dilithium5_aarch64")] -extern "C" { - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM5_AARCH64_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(test)] -mod test_dilithium2_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_DILITHIUM2_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM2_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_dilithium2_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_DILITHIUM2_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_DILITHIUM2_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_DILITHIUM2_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_DILITHIUM2_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_DILITHIUM2_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM2_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AVX2_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM2_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM2_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_DILITHIUM2_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM2_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM2_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_aarch64_neon, feature = "neon"))] -mod test_dilithium2_aarch64 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AARCH64_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_DILITHIUM2_AARCH64_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM2_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_dilithium3_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_DILITHIUM3_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM3_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_dilithium3_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_DILITHIUM3_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_DILITHIUM3_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_DILITHIUM3_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_DILITHIUM3_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_DILITHIUM3_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM3_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AVX2_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM3_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM3_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_DILITHIUM3_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM3_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM3_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_aarch64_neon, feature = "neon"))] -mod test_dilithium3_aarch64 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AARCH64_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_DILITHIUM3_AARCH64_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM3_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_dilithium5_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_DILITHIUM5_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM5_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_dilithium5_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_DILITHIUM5_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_DILITHIUM5_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_DILITHIUM5_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_DILITHIUM5_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_DILITHIUM5_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM5_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AVX2_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM5_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM5_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_DILITHIUM5_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM5_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM5_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_aarch64_neon, feature = "neon"))] -mod test_dilithium5_aarch64 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AARCH64_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_DILITHIUM5_AARCH64_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_DILITHIUM5_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} diff --git a/pqcrypto-dilithium/src/lib.rs b/pqcrypto-dilithium/src/lib.rs deleted file mode 100644 index 6913eb69..00000000 --- a/pqcrypto-dilithium/src/lib.rs +++ /dev/null @@ -1,48 +0,0 @@ -//! # dilithium -//! -//! This crate provides bindings to and wrappers around the following -//! implementations from [PQClean][pqc]: -//! -//! * dilithium2 - clean -//! * dilithium3 - clean -//! * dilithium5 - clean -//! -//! [pqc]: https://github.com/pqclean/pqclean/ -//! - -#![no_std] -#![allow(clippy::len_without_is_empty)] - -// For no-std vectors -extern crate alloc; - -// For tests -#[cfg(feature = "std")] -extern crate std; - -pub mod dilithium2; -pub mod dilithium3; -pub mod dilithium5; -pub mod ffi; - -pub use crate::dilithium2::{ - detached_sign as dilithium2_detached_sign, keypair as dilithium2_keypair, - open as dilithium2_open, public_key_bytes as dilithium2_public_key_bytes, - secret_key_bytes as dilithium2_secret_key_bytes, sign as dilithium2_sign, - signature_bytes as dilithium2_signature_bytes, - verify_detached_signature as dilithium2_verify_detached_signature, -}; -pub use crate::dilithium3::{ - detached_sign as dilithium3_detached_sign, keypair as dilithium3_keypair, - open as dilithium3_open, public_key_bytes as dilithium3_public_key_bytes, - secret_key_bytes as dilithium3_secret_key_bytes, sign as dilithium3_sign, - signature_bytes as dilithium3_signature_bytes, - verify_detached_signature as dilithium3_verify_detached_signature, -}; -pub use crate::dilithium5::{ - detached_sign as dilithium5_detached_sign, keypair as dilithium5_keypair, - open as dilithium5_open, public_key_bytes as dilithium5_public_key_bytes, - secret_key_bytes as dilithium5_secret_key_bytes, sign as dilithium5_sign, - signature_bytes as dilithium5_signature_bytes, - verify_detached_signature as dilithium5_verify_detached_signature, -}; diff --git a/pqcrypto-falcon/Cargo.toml b/pqcrypto-falcon/Cargo.toml deleted file mode 100644 index 269d16d5..00000000 --- a/pqcrypto-falcon/Cargo.toml +++ /dev/null @@ -1,38 +0,0 @@ -[package] -name = "pqcrypto-falcon" -description = "Post-Quantum Signature Scheme falcon" -readme = "README.md" -version = "0.3.0" -authors = ["Thom Wiggers "] -edition = "2021" -license = "MIT OR Apache-2.0" -homepage = "https://github.com/rustpq/" -repository = "https://github.com/rustpq/pqcrypto/" -keywords = ["cryptography", "post-quantum", "security"] -categories = ["cryptography", "no-std"] - -[dependencies] -pqcrypto-internals = { path = "../pqcrypto-internals", version = "0.2" } -pqcrypto-traits = { path = "../pqcrypto-traits", version = "0.3.5", default-features = false } -libc = "0.2.0" -serde = { version = "1.0", features = ["derive"], optional = true } -serde-big-array = { version = "0.5.1", optional = true } - -[features] -default = ["avx2", "neon", "std"] -avx2 = ["std"] -neon = ["std"] -std = ["pqcrypto-traits/std"] -serialization = ["serde", "serde-big-array"] - -[dev-dependencies] -rand = "0.8.5" - -[build-dependencies] -cc = { version = "1.0", features = ["parallel"] } -glob = "0.3.0" - -[badges] -travis-ci = { repository = "rustpq/pqcrypto", branch = "master" } -maintenance = { status = "actively-developed" } - diff --git a/pqcrypto-falcon/README.md b/pqcrypto-falcon/README.md deleted file mode 100644 index 371eb6c3..00000000 --- a/pqcrypto-falcon/README.md +++ /dev/null @@ -1,41 +0,0 @@ -# falcon - - -This crate contains bindings to the C implementations of the following schemes, -from [PQClean][pqclean]. - -This project packages Post-Quantum cryptographic algorithms that participate in -the [NIST PQC standardization effort][nistpqc]. It is currently a collection of -wrappers around C implementations from the [PQClean][pqclean] project. - -## Serialization - -If you want `serde` support, enable the `serialization` feature. - -## Included implementations from PQClean - -Below is a list of the included schemes and the corresponding implementations -sourced from [PQClean][pqclean]. The "default" implementation is used in the -Rust-friendly interface, alternative implementations are exposed as ``ffi`` -methods only. - -* ``falcon-512`` - * ``clean`` - * ``avx2`` (if supported) - * ``aarch64`` (if supported) -* ``falcon-1024`` - * ``clean`` - * ``avx2`` (if supported) - * ``aarch64`` (if supported) - - -## License - -The wrappers and wrapper generation scripts in this project are covered by the -MIT or Apache 2.0 licenses, at your choice. - -The implementations we link to are not, however. Please see the [PQClean][pqclean] -project for the appropriate licenses. - -[pqclean]: https://github.com/PQClean/PQClean/ -[nistpqc]: https://nist.gov/pqc/ \ No newline at end of file diff --git a/pqcrypto-falcon/build.rs b/pqcrypto-falcon/build.rs deleted file mode 100644 index 763f50aa..00000000 --- a/pqcrypto-falcon/build.rs +++ /dev/null @@ -1,155 +0,0 @@ -extern crate cc; -extern crate glob; - -use std::env; -use std::path::{Path, PathBuf}; - -macro_rules! build_clean { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_sign", $variant, "clean"] - .iter() - .collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.c").to_str().unwrap()).unwrap(); - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_clean", $variant).as_str()); - }; -} - -macro_rules! build_avx2 { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_sign", $variant, "avx2"] - .iter() - .collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.[csS]").to_str().unwrap()).unwrap(); - let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); - if target_env == "msvc" { - builder.flag("/arch:AVX2"); - } else { - builder - .flag("-mavx2") - .flag("-mbmi2") - .flag("-mbmi") - .flag("-maes") - .flag("-mpopcnt") - .flag("-mpclmul"); - } - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_avx2", $variant).as_str()); - }; -} - -macro_rules! build_aarch64 { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_sign", $variant, "aarch64"] - .iter() - .collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.[csS]").to_str().unwrap()).unwrap(); - builder.flag("-march=armv8-a"); - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_aarch64", $variant).as_str()); - }; -} - -fn main() { - #[allow(unused_variables)] - let aes_enabled = env::var("CARGO_FEATURE_AES").is_ok(); - #[allow(unused_variables)] - let avx2_enabled = env::var("CARGO_FEATURE_AVX2").is_ok(); - #[allow(unused_variables)] - let neon_enabled = env::var("CARGO_FEATURE_NEON").is_ok(); - #[allow(unused_variables)] - let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); - #[allow(unused_variables)] - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - #[allow(unused_variables)] - let is_windows = target_os == "windows"; - #[allow(unused_variables)] - let is_macos = target_os == "macos"; - - build_clean!("falcon-512"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("falcon-512"); - } - if target_arch == "aarch64" && neon_enabled { - build_aarch64!("falcon-512"); - } - build_clean!("falcon-1024"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("falcon-1024"); - } - if target_arch == "aarch64" && neon_enabled { - build_aarch64!("falcon-1024"); - } - - if target_arch == "x86_64" && avx2_enabled { - // Print enableing flag for AVX2 implementation - println!("cargo:rustc-cfg=enable_x86_avx2"); - } - if target_arch == "aarch64" && neon_enabled { - // Print enableing flag for AARCH64 implementation - println!("cargo:rustc-cfg=enable_aarch64_neon"); - } -} diff --git a/pqcrypto-falcon/pqclean b/pqcrypto-falcon/pqclean deleted file mode 120000 index 60b92d49..00000000 --- a/pqcrypto-falcon/pqclean +++ /dev/null @@ -1 +0,0 @@ -../pqclean \ No newline at end of file diff --git a/pqcrypto-falcon/src/falcon1024.rs b/pqcrypto-falcon/src/falcon1024.rs deleted file mode 100644 index 5f40fd2d..00000000 --- a/pqcrypto-falcon/src/falcon1024.rs +++ /dev/null @@ -1,391 +0,0 @@ -//! falcon-1024 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-falcon -//! use pqcrypto_falcon::falcon1024::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::falcon1024::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_FALCON1024_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_FALCON1024_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_FALCON1024_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature([0u8; ffi::PQCLEAN_FALCON1024_CLEAN_CRYPTO_BYTES], 0) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_FALCON1024_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_FALCON1024_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_FALCON1024_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_FALCON1024_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_FALCON1024_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a falcon-1024 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_FALCON1024_AVX2_crypto_sign_keypair); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - // always use AArch64 code, when target is detected as all AArch64 targets have NEON - // support, and std::is_aarch64_feature_detected!("neon") works only with Rust nightly at - // the moment - if true { - return gen_keypair!(PQCLEAN_FALCON1024_AARCH64_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_FALCON1024_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_FALCON1024_AVX2_crypto_sign, msg, sk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return gen_signature!(PQCLEAN_FALCON1024_AARCH64_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_FALCON1024_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_FALCON1024_AVX2_crypto_sign_open, sm, pk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return open_signed!(PQCLEAN_FALCON1024_AARCH64_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_FALCON1024_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!(PQCLEAN_FALCON1024_AVX2_crypto_sign_signature, msg, sk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return detached_signature!(PQCLEAN_FALCON1024_AARCH64_crypto_sign_signature, msg, sk); - } - } - detached_signature!(PQCLEAN_FALCON1024_CLEAN_crypto_sign_signature, msg, sk) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!(PQCLEAN_FALCON1024_AVX2_crypto_sign_verify, sig, msg, pk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return verify_detached_sig!( - PQCLEAN_FALCON1024_AARCH64_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!(PQCLEAN_FALCON1024_CLEAN_crypto_sign_verify, sig, msg, pk) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-falcon/src/falcon512.rs b/pqcrypto-falcon/src/falcon512.rs deleted file mode 100644 index e46c66f2..00000000 --- a/pqcrypto-falcon/src/falcon512.rs +++ /dev/null @@ -1,391 +0,0 @@ -//! falcon-512 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-falcon -//! use pqcrypto_falcon::falcon512::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::falcon512::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_FALCON512_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_FALCON512_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature([0u8; ffi::PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES], 0) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_FALCON512_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_FALCON512_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a falcon-512 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_FALCON512_AVX2_crypto_sign_keypair); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - // always use AArch64 code, when target is detected as all AArch64 targets have NEON - // support, and std::is_aarch64_feature_detected!("neon") works only with Rust nightly at - // the moment - if true { - return gen_keypair!(PQCLEAN_FALCON512_AARCH64_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_FALCON512_AVX2_crypto_sign, msg, sk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return gen_signature!(PQCLEAN_FALCON512_AARCH64_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_FALCON512_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_FALCON512_AVX2_crypto_sign_open, sm, pk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return open_signed!(PQCLEAN_FALCON512_AARCH64_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_FALCON512_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!(PQCLEAN_FALCON512_AVX2_crypto_sign_signature, msg, sk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return detached_signature!(PQCLEAN_FALCON512_AARCH64_crypto_sign_signature, msg, sk); - } - } - detached_signature!(PQCLEAN_FALCON512_CLEAN_crypto_sign_signature, msg, sk) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!(PQCLEAN_FALCON512_AVX2_crypto_sign_verify, sig, msg, pk); - } - } - #[cfg(all(enable_aarch64_neon, feature = "neon"))] - { - if true { - return verify_detached_sig!( - PQCLEAN_FALCON512_AARCH64_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!(PQCLEAN_FALCON512_CLEAN_crypto_sign_verify, sig, msg, pk) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-falcon/src/ffi.rs b/pqcrypto-falcon/src/ffi.rs deleted file mode 100644 index eddf6c31..00000000 --- a/pqcrypto-falcon/src/ffi.rs +++ /dev/null @@ -1,982 +0,0 @@ -//! Foreign function interfaces -//! -//! This module defines the foreign function interface for the following -//! crypto implementations from PQClean: -//! -//! * falcon-512 -//! * falcon-1024 -// This file has been generated from PQClean. -// Find the templates in pqcrypto-template -use libc::c_int; - -// ensures we link correctly -#[allow(unused_imports)] -use pqcrypto_internals::*; - -pub const PQCLEAN_FALCON512_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 1281; -pub const PQCLEAN_FALCON512_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 897; -pub const PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES: usize = 666; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_FALCON512_AVX2_CRYPTO_SECRETKEYBYTES: usize = 1281; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_FALCON512_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 897; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_FALCON512_AVX2_CRYPTO_BYTES: usize = 666; - -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_FALCON512_AARCH64_CRYPTO_SECRETKEYBYTES: usize = 1281; -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_FALCON512_AARCH64_CRYPTO_PUBLICKEYBYTES: usize = 897; -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_FALCON512_AARCH64_CRYPTO_BYTES: usize = 666; - -pub const PQCLEAN_FALCON1024_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 2305; -pub const PQCLEAN_FALCON1024_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1793; -pub const PQCLEAN_FALCON1024_CLEAN_CRYPTO_BYTES: usize = 1280; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_FALCON1024_AVX2_CRYPTO_SECRETKEYBYTES: usize = 2305; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_FALCON1024_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 1793; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_FALCON1024_AVX2_CRYPTO_BYTES: usize = 1280; - -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_FALCON1024_AARCH64_CRYPTO_SECRETKEYBYTES: usize = 2305; -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_FALCON1024_AARCH64_CRYPTO_PUBLICKEYBYTES: usize = 1793; -#[cfg(enable_aarch64_neon)] -pub const PQCLEAN_FALCON1024_AARCH64_CRYPTO_BYTES: usize = 1280; - -#[link(name = "falcon-512_clean")] -extern "C" { - pub fn PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_FALCON512_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_FALCON512_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_FALCON512_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_FALCON512_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "falcon-512_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_FALCON512_AVX2_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_FALCON512_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_FALCON512_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_FALCON512_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_FALCON512_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_aarch64_neon)] -#[link(name = "falcon-512_aarch64")] -extern "C" { - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_FALCON512_AARCH64_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_FALCON512_AARCH64_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_FALCON512_AARCH64_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_FALCON512_AARCH64_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_FALCON512_AARCH64_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "falcon-1024_clean")] -extern "C" { - pub fn PQCLEAN_FALCON1024_CLEAN_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_FALCON1024_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_FALCON1024_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_FALCON1024_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_FALCON1024_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "falcon-1024_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_FALCON1024_AVX2_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_FALCON1024_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_FALCON1024_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_FALCON1024_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_FALCON1024_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_aarch64_neon)] -#[link(name = "falcon-1024_aarch64")] -extern "C" { - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_FALCON1024_AARCH64_crypto_sign_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_FALCON1024_AARCH64_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_FALCON1024_AARCH64_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_FALCON1024_AARCH64_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_aarch64_neon)] - pub fn PQCLEAN_FALCON1024_AARCH64_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(test)] -mod test_falcon512_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_FALCON512_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_FALCON512_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_FALCON512_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_FALCON512_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_FALCON512_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_FALCON512_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_FALCON512_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON512_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_FALCON512_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_FALCON512_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_FALCON512_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON512_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON512_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_falcon512_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_FALCON512_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_FALCON512_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_FALCON512_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_FALCON512_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_FALCON512_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_FALCON512_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_FALCON512_AVX2_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_FALCON512_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_FALCON512_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_FALCON512_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_FALCON512_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON512_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_FALCON512_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_FALCON512_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_FALCON512_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON512_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON512_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_aarch64_neon, feature = "neon"))] -mod test_falcon512_aarch64 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_FALCON512_AARCH64_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_FALCON512_AARCH64_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_FALCON512_AARCH64_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_FALCON512_AARCH64_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_FALCON512_AARCH64_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_FALCON512_AARCH64_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_FALCON512_AARCH64_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_FALCON512_AARCH64_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_FALCON512_AARCH64_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_FALCON512_AARCH64_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_FALCON512_AARCH64_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON512_AARCH64_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_FALCON512_AARCH64_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_FALCON512_AARCH64_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_FALCON512_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON512_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON512_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_falcon1024_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_FALCON1024_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_FALCON1024_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_FALCON1024_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_FALCON1024_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_FALCON1024_CLEAN_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_FALCON1024_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_FALCON1024_CLEAN_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_FALCON1024_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_FALCON1024_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_FALCON1024_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_FALCON1024_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON1024_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_FALCON1024_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_FALCON1024_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_FALCON1024_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON1024_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON1024_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_falcon1024_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_FALCON1024_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_FALCON1024_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_FALCON1024_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_FALCON1024_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_FALCON1024_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_FALCON1024_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_FALCON1024_AVX2_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_FALCON1024_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_FALCON1024_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_FALCON1024_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_FALCON1024_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON1024_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_FALCON1024_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_FALCON1024_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_FALCON1024_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON1024_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON1024_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_aarch64_neon, feature = "neon"))] -mod test_falcon1024_aarch64 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_FALCON1024_AARCH64_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_FALCON1024_AARCH64_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_FALCON1024_AARCH64_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_FALCON1024_AARCH64_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_FALCON1024_AARCH64_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_FALCON1024_AARCH64_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_FALCON1024_AARCH64_crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_FALCON1024_AARCH64_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = Vec::with_capacity(mlen + PQCLEAN_FALCON1024_AARCH64_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_FALCON1024_AARCH64_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_FALCON1024_AARCH64_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON1024_AARCH64_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_FALCON1024_AARCH64_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_FALCON1024_AARCH64_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_FALCON1024_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON1024_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_FALCON1024_AARCH64_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} diff --git a/pqcrypto-falcon/src/lib.rs b/pqcrypto-falcon/src/lib.rs deleted file mode 100644 index 2ce3ddeb..00000000 --- a/pqcrypto-falcon/src/lib.rs +++ /dev/null @@ -1,38 +0,0 @@ -//! # falcon -//! -//! This crate provides bindings to and wrappers around the following -//! implementations from [PQClean][pqc]: -//! -//! * falcon-512 - clean -//! * falcon-1024 - clean -//! -//! [pqc]: https://github.com/pqclean/pqclean/ -//! - -#![no_std] -#![allow(clippy::len_without_is_empty)] - -// For no-std vectors -extern crate alloc; - -// For tests -#[cfg(feature = "std")] -extern crate std; - -pub mod falcon1024; -pub mod falcon512; -pub mod ffi; - -pub use crate::falcon1024::{ - detached_sign as falcon1024_detached_sign, keypair as falcon1024_keypair, - open as falcon1024_open, public_key_bytes as falcon1024_public_key_bytes, - secret_key_bytes as falcon1024_secret_key_bytes, sign as falcon1024_sign, - signature_bytes as falcon1024_signature_bytes, - verify_detached_signature as falcon1024_verify_detached_signature, -}; -pub use crate::falcon512::{ - detached_sign as falcon512_detached_sign, keypair as falcon512_keypair, open as falcon512_open, - public_key_bytes as falcon512_public_key_bytes, secret_key_bytes as falcon512_secret_key_bytes, - sign as falcon512_sign, signature_bytes as falcon512_signature_bytes, - verify_detached_signature as falcon512_verify_detached_signature, -}; diff --git a/pqcrypto-hqc/Cargo.toml b/pqcrypto-hqc/Cargo.toml deleted file mode 100644 index 1b89217b..00000000 --- a/pqcrypto-hqc/Cargo.toml +++ /dev/null @@ -1,35 +0,0 @@ -[package] -name = "pqcrypto-hqc" -description = "Post-Quantum Key-Encapsulation Mechanism hqc" -readme = "README.md" -version = "0.2.0" -authors = ["Thom Wiggers "] -edition = "2021" -license = "MIT OR Apache-2.0" -homepage = "https://github.com/rustpq/" -repository = "https://github.com/rustpq/pqcrypto/" -keywords = ["cryptography", "post-quantum", "security"] -categories = ["cryptography", "no-std"] - -[dependencies] -pqcrypto-internals = { path = "../pqcrypto-internals", version = "0.2" } -pqcrypto-traits = { path = "../pqcrypto-traits", version = "0.3.5", default-features = false } -libc = "0.2.0" -serde = { version = "1.0", features = ["derive"], optional = true } -serde-big-array = { version = "0.5.1", optional = true } - -[features] -default = ["std"] -std = ["pqcrypto-traits/std"] -serialization = ["serde", "serde-big-array"] - -[dev-dependencies] - -[build-dependencies] -cc = { version = "1.0", features = ["parallel"] } -glob = "0.3.0" - -[badges] -travis-ci = { repository = "rustpq/pqcrypto", branch = "master" } -maintenance = { status = "actively-developed" } - diff --git a/pqcrypto-hqc/README.md b/pqcrypto-hqc/README.md deleted file mode 100644 index 0bf17a9f..00000000 --- a/pqcrypto-hqc/README.md +++ /dev/null @@ -1,39 +0,0 @@ -# hqc - - -This crate contains bindings to the C implementations of the following schemes, -from [PQClean][pqclean]. - -This project packages Post-Quantum cryptographic algorithms that participate in -the [NIST PQC standardization effort][nistpqc]. It is currently a collection of -wrappers around C implementations from the [PQClean][pqclean] project. - -## Serialization - -If you want `serde` support, enable the `serialization` feature. - -## Included implementations from PQClean - -Below is a list of the included schemes and the corresponding implementations -sourced from [PQClean][pqclean]. The "default" implementation is used in the -Rust-friendly interface, alternative implementations are exposed as ``ffi`` -methods only. - -* ``hqc-128`` - * ``clean`` -* ``hqc-192`` - * ``clean`` -* ``hqc-256`` - * ``clean`` - - -## License - -The wrappers and wrapper generation scripts in this project are covered by the -MIT or Apache 2.0 licenses, at your choice. - -The implementations we link to are not, however. Please see the [PQClean][pqclean] -project for the appropriate licenses. - -[pqclean]: https://github.com/PQClean/PQClean/ -[nistpqc]: https://nist.gov/pqc/ \ No newline at end of file diff --git a/pqcrypto-hqc/build.rs b/pqcrypto-hqc/build.rs deleted file mode 100644 index 57e3210e..00000000 --- a/pqcrypto-hqc/build.rs +++ /dev/null @@ -1,58 +0,0 @@ -extern crate cc; -extern crate glob; - -use std::env; -use std::path::{Path, PathBuf}; - -macro_rules! build_clean { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_kem", $variant, "clean"] - .iter() - .collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.c").to_str().unwrap()).unwrap(); - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_clean", $variant).as_str()); - }; -} - -fn main() { - #[allow(unused_variables)] - let aes_enabled = env::var("CARGO_FEATURE_AES").is_ok(); - #[allow(unused_variables)] - let avx2_enabled = env::var("CARGO_FEATURE_AVX2").is_ok(); - #[allow(unused_variables)] - let neon_enabled = env::var("CARGO_FEATURE_NEON").is_ok(); - #[allow(unused_variables)] - let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); - #[allow(unused_variables)] - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - #[allow(unused_variables)] - let is_windows = target_os == "windows"; - #[allow(unused_variables)] - let is_macos = target_os == "macos"; - - build_clean!("hqc-128"); - build_clean!("hqc-192"); - build_clean!("hqc-256"); -} diff --git a/pqcrypto-hqc/pqclean b/pqcrypto-hqc/pqclean deleted file mode 120000 index 60b92d49..00000000 --- a/pqcrypto-hqc/pqclean +++ /dev/null @@ -1 +0,0 @@ -../pqclean \ No newline at end of file diff --git a/pqcrypto-hqc/src/ffi.rs b/pqcrypto-hqc/src/ffi.rs deleted file mode 100644 index 9eb95bc5..00000000 --- a/pqcrypto-hqc/src/ffi.rs +++ /dev/null @@ -1,144 +0,0 @@ -//! Foreign function interfaces -//! -//! This module defines the foreign function interface for the following -//! crypto implementations from PQClean: -//! -//! * hqc-128 -//! * hqc-192 -//! * hqc-256 -// This file has been generated from PQClean. -// Find the templates in pqcrypto-template -use libc::c_int; - -// ensures we link correctly -#[allow(unused_imports)] -use pqcrypto_internals::*; - -pub const PQCLEAN_HQC128_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 2305; -pub const PQCLEAN_HQC128_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 2249; -pub const PQCLEAN_HQC128_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 4433; -pub const PQCLEAN_HQC128_CLEAN_CRYPTO_BYTES: usize = 64; - -pub const PQCLEAN_HQC192_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 4586; -pub const PQCLEAN_HQC192_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 4522; -pub const PQCLEAN_HQC192_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 8978; -pub const PQCLEAN_HQC192_CLEAN_CRYPTO_BYTES: usize = 64; - -pub const PQCLEAN_HQC256_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 7317; -pub const PQCLEAN_HQC256_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 7245; -pub const PQCLEAN_HQC256_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 14421; -pub const PQCLEAN_HQC256_CLEAN_CRYPTO_BYTES: usize = 64; - -#[link(name = "hqc-128_clean")] -extern "C" { - pub fn PQCLEAN_HQC128_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_HQC128_CLEAN_crypto_kem_enc(ct: *mut u8, ss: *mut u8, pk: *const u8) -> c_int; - pub fn PQCLEAN_HQC128_CLEAN_crypto_kem_dec(ss: *mut u8, ct: *const u8, sk: *const u8) -> c_int; -} - -#[link(name = "hqc-192_clean")] -extern "C" { - pub fn PQCLEAN_HQC192_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_HQC192_CLEAN_crypto_kem_enc(ct: *mut u8, ss: *mut u8, pk: *const u8) -> c_int; - pub fn PQCLEAN_HQC192_CLEAN_crypto_kem_dec(ss: *mut u8, ct: *const u8, sk: *const u8) -> c_int; -} - -#[link(name = "hqc-256_clean")] -extern "C" { - pub fn PQCLEAN_HQC256_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int; - pub fn PQCLEAN_HQC256_CLEAN_crypto_kem_enc(ct: *mut u8, ss: *mut u8, pk: *const u8) -> c_int; - pub fn PQCLEAN_HQC256_CLEAN_crypto_kem_dec(ss: *mut u8, ct: *const u8, sk: *const u8) -> c_int; -} - -#[cfg(test)] -mod test_hqc128_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_HQC128_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_HQC128_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_HQC128_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_HQC128_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_HQC128_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_HQC128_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_HQC128_CLEAN_crypto_kem_enc(ct.as_mut_ptr(), ss1.as_mut_ptr(), pk.as_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_HQC128_CLEAN_crypto_kem_dec(ss2.as_mut_ptr(), ct.as_ptr(), sk.as_ptr()) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_hqc192_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_HQC192_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_HQC192_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_HQC192_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_HQC192_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_HQC192_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_HQC192_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_HQC192_CLEAN_crypto_kem_enc(ct.as_mut_ptr(), ss1.as_mut_ptr(), pk.as_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_HQC192_CLEAN_crypto_kem_dec(ss2.as_mut_ptr(), ct.as_ptr(), sk.as_ptr()) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} - -#[cfg(test)] -mod test_hqc256_clean { - use super::*; - use alloc::vec; - - #[test] - fn test_ffi() { - unsafe { - let mut pk = vec![0u8; PQCLEAN_HQC256_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_HQC256_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut ct = vec![0u8; PQCLEAN_HQC256_CLEAN_CRYPTO_CIPHERTEXTBYTES]; - let mut ss1 = vec![0u8; PQCLEAN_HQC256_CLEAN_CRYPTO_BYTES]; - let mut ss2 = vec![0u8; PQCLEAN_HQC256_CLEAN_CRYPTO_BYTES]; - - assert_eq!( - 0, - PQCLEAN_HQC256_CLEAN_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_HQC256_CLEAN_crypto_kem_enc(ct.as_mut_ptr(), ss1.as_mut_ptr(), pk.as_ptr()) - ); - assert_eq!( - 0, - PQCLEAN_HQC256_CLEAN_crypto_kem_dec(ss2.as_mut_ptr(), ct.as_ptr(), sk.as_ptr()) - ); - assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal"); - } - } -} diff --git a/pqcrypto-hqc/src/hqc128.rs b/pqcrypto-hqc/src/hqc128.rs deleted file mode 100644 index 119daaf3..00000000 --- a/pqcrypto-hqc/src/hqc128.rs +++ /dev/null @@ -1,171 +0,0 @@ -//! hqc-128 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-hqc -//! use pqcrypto_hqc::hqc128::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::hqc128::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!(PublicKey, ffi::PQCLEAN_HQC128_CLEAN_CRYPTO_PUBLICKEYBYTES); -simple_struct!(SecretKey, ffi::PQCLEAN_HQC128_CLEAN_CRYPTO_SECRETKEYBYTES); -simple_struct!(Ciphertext, ffi::PQCLEAN_HQC128_CLEAN_CRYPTO_CIPHERTEXTBYTES); -simple_struct!(SharedSecret, ffi::PQCLEAN_HQC128_CLEAN_CRYPTO_BYTES); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_HQC128_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_HQC128_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_HQC128_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_HQC128_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a hqc-128 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - gen_keypair!(PQCLEAN_HQC128_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a hqc-128 public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - encap!(PQCLEAN_HQC128_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received hqc-128 ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - decap!(PQCLEAN_HQC128_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-hqc/src/hqc192.rs b/pqcrypto-hqc/src/hqc192.rs deleted file mode 100644 index 98479e02..00000000 --- a/pqcrypto-hqc/src/hqc192.rs +++ /dev/null @@ -1,171 +0,0 @@ -//! hqc-192 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-hqc -//! use pqcrypto_hqc::hqc192::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::hqc192::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!(PublicKey, ffi::PQCLEAN_HQC192_CLEAN_CRYPTO_PUBLICKEYBYTES); -simple_struct!(SecretKey, ffi::PQCLEAN_HQC192_CLEAN_CRYPTO_SECRETKEYBYTES); -simple_struct!(Ciphertext, ffi::PQCLEAN_HQC192_CLEAN_CRYPTO_CIPHERTEXTBYTES); -simple_struct!(SharedSecret, ffi::PQCLEAN_HQC192_CLEAN_CRYPTO_BYTES); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_HQC192_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_HQC192_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_HQC192_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_HQC192_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a hqc-192 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - gen_keypair!(PQCLEAN_HQC192_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a hqc-192 public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - encap!(PQCLEAN_HQC192_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received hqc-192 ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - decap!(PQCLEAN_HQC192_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-hqc/src/hqc256.rs b/pqcrypto-hqc/src/hqc256.rs deleted file mode 100644 index 7a862f18..00000000 --- a/pqcrypto-hqc/src/hqc256.rs +++ /dev/null @@ -1,171 +0,0 @@ -//! hqc-256 -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-hqc -//! use pqcrypto_hqc::hqc256::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::kem::hqc256::*; -//! let (pk, sk) = keypair(); -//! let (ss1, ct) = encapsulate(&pk); -//! let ss2 = decapsulate(&ct, &sk); -//! assert!(ss1 == ss2); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use pqcrypto_traits::kem as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!(PublicKey, ffi::PQCLEAN_HQC256_CLEAN_CRYPTO_PUBLICKEYBYTES); -simple_struct!(SecretKey, ffi::PQCLEAN_HQC256_CLEAN_CRYPTO_SECRETKEYBYTES); -simple_struct!(Ciphertext, ffi::PQCLEAN_HQC256_CLEAN_CRYPTO_CIPHERTEXTBYTES); -simple_struct!(SharedSecret, ffi::PQCLEAN_HQC256_CLEAN_CRYPTO_BYTES); - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_HQC256_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_HQC256_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes for the encapsulated ciphertext -pub const fn ciphertext_bytes() -> usize { - ffi::PQCLEAN_HQC256_CLEAN_CRYPTO_CIPHERTEXTBYTES -} - -/// Get the number of bytes for the shared secret -pub const fn shared_secret_bytes() -> usize { - ffi::PQCLEAN_HQC256_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a hqc-256 keypair -pub fn keypair() -> (PublicKey, SecretKey) { - gen_keypair!(PQCLEAN_HQC256_CLEAN_crypto_kem_keypair) -} - -macro_rules! encap { - ($variant:ident, $pk:ident) => {{ - let mut ss = SharedSecret::new(); - let mut ct = Ciphertext::new(); - assert_eq!( - unsafe { ffi::$variant(ct.0.as_mut_ptr(), ss.0.as_mut_ptr(), $pk.0.as_ptr()) }, - 0, - ); - (ss, ct) - }}; -} - -/// Encapsulate to a hqc-256 public key -pub fn encapsulate(pk: &PublicKey) -> (SharedSecret, Ciphertext) { - encap!(PQCLEAN_HQC256_CLEAN_crypto_kem_enc, pk) -} - -macro_rules! decap { - ($variant:ident, $ct:ident, $sk:ident) => {{ - let mut ss = SharedSecret::new(); - assert_eq!( - unsafe { ffi::$variant(ss.0.as_mut_ptr(), $ct.0.as_ptr(), $sk.0.as_ptr(),) }, - 0 - ); - ss - }}; -} - -/// Decapsulate the received hqc-256 ciphertext -pub fn decapsulate(ct: &Ciphertext, sk: &SecretKey) -> SharedSecret { - decap!(PQCLEAN_HQC256_CLEAN_crypto_kem_dec, ct, sk) -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn test_kem() { - let (pk, sk) = keypair(); - let (ss1, ct) = encapsulate(&pk); - let ss2 = decapsulate(&ct, &sk); - assert_eq!(&ss1.0[..], &ss2.0[..], "Difference in shared secrets!"); - } -} diff --git a/pqcrypto-hqc/src/lib.rs b/pqcrypto-hqc/src/lib.rs deleted file mode 100644 index 7d263eb0..00000000 --- a/pqcrypto-hqc/src/lib.rs +++ /dev/null @@ -1,45 +0,0 @@ -//! # hqc -//! -//! This crate provides bindings to and wrappers around the following -//! implementations from [PQClean][pqc]: -//! -//! * hqc-128 - clean -//! * hqc-192 - clean -//! * hqc-256 - clean -//! -//! [pqc]: https://github.com/pqclean/pqclean/ -//! - -#![no_std] -#![allow(clippy::len_without_is_empty)] - -// For no-std vectors -extern crate alloc; - -// For tests -#[cfg(feature = "std")] -extern crate std; - -pub mod ffi; -pub mod hqc128; -pub mod hqc192; -pub mod hqc256; - -pub use crate::hqc128::{ - ciphertext_bytes as hqc128_ciphertext_bytes, decapsulate as hqc128_decapsulate, - encapsulate as hqc128_encapsulate, keypair as hqc128_keypair, - public_key_bytes as hqc128_public_key_bytes, secret_key_bytes as hqc128_secret_key_bytes, - shared_secret_bytes as hqc128_shared_secret_bytes, -}; -pub use crate::hqc192::{ - ciphertext_bytes as hqc192_ciphertext_bytes, decapsulate as hqc192_decapsulate, - encapsulate as hqc192_encapsulate, keypair as hqc192_keypair, - public_key_bytes as hqc192_public_key_bytes, secret_key_bytes as hqc192_secret_key_bytes, - shared_secret_bytes as hqc192_shared_secret_bytes, -}; -pub use crate::hqc256::{ - ciphertext_bytes as hqc256_ciphertext_bytes, decapsulate as hqc256_decapsulate, - encapsulate as hqc256_encapsulate, keypair as hqc256_keypair, - public_key_bytes as hqc256_public_key_bytes, secret_key_bytes as hqc256_secret_key_bytes, - shared_secret_bytes as hqc256_shared_secret_bytes, -}; diff --git a/pqcrypto-kyber/Cargo.toml b/pqcrypto-kyber/Cargo.toml index 5474acfa..41fc6f5d 100644 --- a/pqcrypto-kyber/Cargo.toml +++ b/pqcrypto-kyber/Cargo.toml @@ -2,7 +2,7 @@ name = "pqcrypto-kyber" description = "Post-Quantum Key-Encapsulation Mechanism kyber" readme = "README.md" -version = "0.8.0" +version = "0.7.7" authors = ["Thom Wiggers "] edition = "2021" license = "MIT OR Apache-2.0" diff --git a/pqcrypto-sphincsplus/Cargo.toml b/pqcrypto-sphincsplus/Cargo.toml deleted file mode 100644 index d2b373f7..00000000 --- a/pqcrypto-sphincsplus/Cargo.toml +++ /dev/null @@ -1,37 +0,0 @@ -[package] -name = "pqcrypto-sphincsplus" -description = "Post-Quantum Signature Scheme sphincsplus" -readme = "README.md" -version = "0.7.0" -authors = ["Thom Wiggers "] -edition = "2021" -license = "MIT OR Apache-2.0" -homepage = "https://github.com/rustpq/" -repository = "https://github.com/rustpq/pqcrypto/" -keywords = ["cryptography", "post-quantum", "security"] -categories = ["cryptography", "no-std"] - -[dependencies] -pqcrypto-internals = { path = "../pqcrypto-internals", version = "0.2" } -pqcrypto-traits = { path = "../pqcrypto-traits", version = "0.3.5", default-features = false } -libc = "0.2.0" -serde = { version = "1.0", features = ["derive"], optional = true } -serde-big-array = { version = "0.5.1", optional = true } - -[features] -default = ["avx2", "std"] -avx2 = ["std"] -std = ["pqcrypto-traits/std"] -serialization = ["serde", "serde-big-array"] - -[dev-dependencies] -rand = "0.8.5" - -[build-dependencies] -cc = { version = "1.0", features = ["parallel"] } -glob = "0.3.0" - -[badges] -travis-ci = { repository = "rustpq/pqcrypto", branch = "master" } -maintenance = { status = "actively-developed" } - diff --git a/pqcrypto-sphincsplus/README.md b/pqcrypto-sphincsplus/README.md deleted file mode 100644 index 7a365a06..00000000 --- a/pqcrypto-sphincsplus/README.md +++ /dev/null @@ -1,69 +0,0 @@ -# sphincsplus - - -This crate contains bindings to the C implementations of the following schemes, -from [PQClean][pqclean]. - -This project packages Post-Quantum cryptographic algorithms that participate in -the [NIST PQC standardization effort][nistpqc]. It is currently a collection of -wrappers around C implementations from the [PQClean][pqclean] project. - -## Serialization - -If you want `serde` support, enable the `serialization` feature. - -## Included implementations from PQClean - -Below is a list of the included schemes and the corresponding implementations -sourced from [PQClean][pqclean]. The "default" implementation is used in the -Rust-friendly interface, alternative implementations are exposed as ``ffi`` -methods only. - -* ``sphincs-shake-128f-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-shake-128s-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-shake-192f-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-shake-192s-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-shake-256f-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-shake-256s-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-sha2-128f-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-sha2-128s-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-sha2-192f-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-sha2-192s-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-sha2-256f-simple`` - * ``clean`` - * ``avx2`` (if supported) -* ``sphincs-sha2-256s-simple`` - * ``clean`` - * ``avx2`` (if supported) - - -## License - -The wrappers and wrapper generation scripts in this project are covered by the -MIT or Apache 2.0 licenses, at your choice. - -The implementations we link to are not, however. Please see the [PQClean][pqclean] -project for the appropriate licenses. - -[pqclean]: https://github.com/PQClean/PQClean/ -[nistpqc]: https://nist.gov/pqc/ \ No newline at end of file diff --git a/pqcrypto-sphincsplus/build.rs b/pqcrypto-sphincsplus/build.rs deleted file mode 100644 index 17530615..00000000 --- a/pqcrypto-sphincsplus/build.rs +++ /dev/null @@ -1,152 +0,0 @@ -extern crate cc; -extern crate glob; - -use std::env; -use std::path::{Path, PathBuf}; - -macro_rules! build_clean { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_sign", $variant, "clean"] - .iter() - .collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.c").to_str().unwrap()).unwrap(); - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_clean", $variant).as_str()); - }; -} - -macro_rules! build_avx2 { - ($variant:expr) => { - let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap(); - let common_dir = Path::new("pqclean/common"); - - let mut builder = cc::Build::new(); - let target_dir: PathBuf = ["pqclean", "crypto_sign", $variant, "avx2"] - .iter() - .collect(); - - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - if target_os == "wasi" { - let wasi_sdk_path = - &std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR"); - builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str()); - } - - let scheme_files = glob::glob(target_dir.join("*.[csS]").to_str().unwrap()).unwrap(); - let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); - if target_env == "msvc" { - builder.flag("/arch:AVX2"); - } else { - builder - .flag("-mavx2") - .flag("-mbmi2") - .flag("-mbmi") - .flag("-maes") - .flag("-mpopcnt") - .flag("-mpclmul"); - } - - builder - .include(internals_include_path) - .include(&common_dir) - .include(target_dir) - .files( - scheme_files - .into_iter() - .map(|p| p.unwrap().to_string_lossy().into_owned()), - ); - builder.compile(format!("{}_avx2", $variant).as_str()); - }; -} - -fn main() { - #[allow(unused_variables)] - let aes_enabled = env::var("CARGO_FEATURE_AES").is_ok(); - #[allow(unused_variables)] - let avx2_enabled = env::var("CARGO_FEATURE_AVX2").is_ok(); - #[allow(unused_variables)] - let neon_enabled = env::var("CARGO_FEATURE_NEON").is_ok(); - #[allow(unused_variables)] - let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); - #[allow(unused_variables)] - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - #[allow(unused_variables)] - let is_windows = target_os == "windows"; - #[allow(unused_variables)] - let is_macos = target_os == "macos"; - - build_clean!("sphincs-shake-128f-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-shake-128f-simple"); - } - build_clean!("sphincs-shake-128s-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-shake-128s-simple"); - } - build_clean!("sphincs-shake-192f-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-shake-192f-simple"); - } - build_clean!("sphincs-shake-192s-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-shake-192s-simple"); - } - build_clean!("sphincs-shake-256f-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-shake-256f-simple"); - } - build_clean!("sphincs-shake-256s-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-shake-256s-simple"); - } - build_clean!("sphincs-sha2-128f-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-sha2-128f-simple"); - } - build_clean!("sphincs-sha2-128s-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-sha2-128s-simple"); - } - build_clean!("sphincs-sha2-192f-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-sha2-192f-simple"); - } - build_clean!("sphincs-sha2-192s-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-sha2-192s-simple"); - } - build_clean!("sphincs-sha2-256f-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-sha2-256f-simple"); - } - build_clean!("sphincs-sha2-256s-simple"); - if target_arch == "x86_64" && avx2_enabled { - build_avx2!("sphincs-sha2-256s-simple"); - } - - if target_arch == "x86_64" && avx2_enabled { - // Print enableing flag for AVX2 implementation - println!("cargo:rustc-cfg=enable_x86_avx2"); - } -} diff --git a/pqcrypto-sphincsplus/pqclean b/pqcrypto-sphincsplus/pqclean deleted file mode 120000 index 60b92d49..00000000 --- a/pqcrypto-sphincsplus/pqclean +++ /dev/null @@ -1 +0,0 @@ -../pqclean \ No newline at end of file diff --git a/pqcrypto-sphincsplus/src/ffi.rs b/pqcrypto-sphincsplus/src/ffi.rs deleted file mode 100644 index 94554687..00000000 --- a/pqcrypto-sphincsplus/src/ffi.rs +++ /dev/null @@ -1,4062 +0,0 @@ -//! Foreign function interfaces -//! -//! This module defines the foreign function interface for the following -//! crypto implementations from PQClean: -//! -//! * sphincs-shake-128f-simple -//! * sphincs-shake-128s-simple -//! * sphincs-shake-192f-simple -//! * sphincs-shake-192s-simple -//! * sphincs-shake-256f-simple -//! * sphincs-shake-256s-simple -//! * sphincs-sha2-128f-simple -//! * sphincs-sha2-128s-simple -//! * sphincs-sha2-192f-simple -//! * sphincs-sha2-192s-simple -//! * sphincs-sha2-256f-simple -//! * sphincs-sha2-256s-simple -// This file has been generated from PQClean. -// Find the templates in pqcrypto-template -use libc::c_int; - -// ensures we link correctly -#[allow(unused_imports)] -use pqcrypto_internals::*; - -pub const PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 64; -pub const PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 32; -pub const PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_BYTES: usize = 17088; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 64; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 32; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_BYTES: usize = 17088; - -pub const PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 64; -pub const PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 32; -pub const PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_BYTES: usize = 7856; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 64; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 32; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_BYTES: usize = 7856; - -pub const PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 96; -pub const PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 48; -pub const PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_BYTES: usize = 35664; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 96; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 48; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_BYTES: usize = 35664; - -pub const PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 96; -pub const PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 48; -pub const PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_BYTES: usize = 16224; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 96; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 48; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_BYTES: usize = 16224; - -pub const PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 128; -pub const PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 64; -pub const PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_BYTES: usize = 49856; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 128; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 64; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_BYTES: usize = 49856; - -pub const PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 128; -pub const PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 64; -pub const PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_BYTES: usize = 29792; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 128; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 64; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_BYTES: usize = 29792; - -pub const PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 64; -pub const PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 32; -pub const PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_BYTES: usize = 17088; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 64; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 32; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_BYTES: usize = 17088; - -pub const PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 64; -pub const PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 32; -pub const PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_BYTES: usize = 7856; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 64; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 32; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_BYTES: usize = 7856; - -pub const PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 96; -pub const PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 48; -pub const PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_BYTES: usize = 35664; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 96; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 48; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_BYTES: usize = 35664; - -pub const PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 96; -pub const PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 48; -pub const PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_BYTES: usize = 16224; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 96; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 48; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_BYTES: usize = 16224; - -pub const PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 128; -pub const PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 64; -pub const PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_BYTES: usize = 49856; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 128; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 64; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_BYTES: usize = 49856; - -pub const PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 128; -pub const PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 64; -pub const PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_BYTES: usize = 29792; - -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES: usize = 128; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES: usize = 64; -#[cfg(enable_x86_avx2)] -pub const PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_BYTES: usize = 29792; - -#[link(name = "sphincs-shake-128f-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-shake-128f-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-shake-128s-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-shake-128s-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-shake-192f-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-shake-192f-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-shake-192s-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-shake-192s-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-shake-256f-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-shake-256f-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-shake-256s-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-shake-256s-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-sha2-128f-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-sha2-128f-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-sha2-128s-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-sha2-128s-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-sha2-192f-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-sha2-192f-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-sha2-192s-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-sha2-192s-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-sha2-256f-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-sha2-256f-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[link(name = "sphincs-sha2-256s-simple_clean")] -extern "C" { - pub fn PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - pub fn PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(enable_x86_avx2)] -#[link(name = "sphincs-sha2-256s-simple_avx2")] -extern "C" { - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_keypair( - pk: *mut u8, - sk: *mut u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign( - sm: *mut u8, - smlen: *mut usize, - msg: *const u8, - len: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_open( - m: *mut u8, - mlen: *mut usize, - sm: *const u8, - smlen: usize, - pk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_signature( - sig: *mut u8, - siglen: *mut usize, - m: *const u8, - mlen: usize, - sk: *const u8, - ) -> c_int; - #[cfg(enable_x86_avx2)] - pub fn PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_verify( - sig: *const u8, - siglen: usize, - m: *const u8, - mlen: usize, - pk: *const u8, - ) -> c_int; -} - -#[cfg(test)] -mod test_sphincsshake128fsimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincsshake128fsimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincsshake128ssimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincsshake128ssimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincsshake192fsimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincsshake192fsimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincsshake192ssimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincsshake192ssimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincsshake256fsimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincsshake256fsimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincsshake256ssimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincsshake256ssimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincssha2128fsimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincssha2128fsimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincssha2128ssimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincssha2128ssimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincssha2192fsimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincssha2192fsimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincssha2192ssimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincssha2192ssimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincssha2256fsimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincssha2256fsimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(test)] -mod test_sphincssha2256ssimple_clean { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - - #[test] - fn test_ffi() { - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_BYTES]; - let mut sm = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} - -#[cfg(all(test, enable_x86_avx2, feature = "avx2"))] -mod test_sphincssha2256ssimple_avx2 { - use super::*; - use alloc::vec; - use alloc::vec::Vec; - use rand::prelude::*; - use std::is_x86_feature_detected; - - #[test] - fn test_ffi() { - if !is_x86_feature_detected!("avx2") { - return; - } - unsafe { - let mut rng = rand::thread_rng(); - let mut mlen: usize = rng.gen::() as usize; - let msg: Vec = (0..mlen).map(|_| rng.gen()).collect(); - - let mut pk = vec![0u8; PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk = vec![0u8; PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut pk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_PUBLICKEYBYTES]; - let mut sk_alt = vec![0u8; PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_SECRETKEYBYTES]; - let mut detached_sig = vec![0u8; PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_BYTES]; - let mut sm = Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_BYTES); - let mut smlen = 0; - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_keypair( - pk.as_mut_ptr(), - sk.as_mut_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign( - sm.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - mlen, - sk.as_ptr() - ) - ); - sm.set_len(smlen); - - let mut unpacked_m = - Vec::with_capacity(mlen + PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_BYTES); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk.as_ptr() - ) - ); - unpacked_m.set_len(mlen); - assert_eq!(unpacked_m, msg); - - // check verification fails with wrong pk - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_keypair( - pk_alt.as_mut_ptr(), - sk_alt.as_mut_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_open( - unpacked_m.as_mut_ptr(), - &mut mlen as *mut usize, - sm.as_ptr(), - sm.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_signature( - detached_sig.as_mut_ptr(), - &mut smlen as *mut usize, - msg.as_ptr(), - msg.len(), - sk.as_ptr() - ) - ); - assert!( - smlen <= PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_CRYPTO_BYTES, - "Signed message length should be ≤ CRYPTO_BYTES" - ); - assert_eq!( - 0, - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len(), - pk_alt.as_ptr() - ) - ); - assert_eq!( - -1, - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_verify( - detached_sig.as_ptr(), - smlen, - msg.as_ptr(), - msg.len() - 1, - pk.as_ptr() - ) - ); - } - } -} diff --git a/pqcrypto-sphincsplus/src/lib.rs b/pqcrypto-sphincsplus/src/lib.rs deleted file mode 100644 index 3ff1daec..00000000 --- a/pqcrypto-sphincsplus/src/lib.rs +++ /dev/null @@ -1,135 +0,0 @@ -//! # sphincsplus -//! -//! This crate provides bindings to and wrappers around the following -//! implementations from [PQClean][pqc]: -//! -//! * sphincs-shake-128f-simple - clean -//! * sphincs-shake-128s-simple - clean -//! * sphincs-shake-192f-simple - clean -//! * sphincs-shake-192s-simple - clean -//! * sphincs-shake-256f-simple - clean -//! * sphincs-shake-256s-simple - clean -//! * sphincs-sha2-128f-simple - clean -//! * sphincs-sha2-128s-simple - clean -//! * sphincs-sha2-192f-simple - clean -//! * sphincs-sha2-192s-simple - clean -//! * sphincs-sha2-256f-simple - clean -//! * sphincs-sha2-256s-simple - clean -//! -//! [pqc]: https://github.com/pqclean/pqclean/ -//! - -#![no_std] -#![allow(clippy::len_without_is_empty)] - -// For no-std vectors -extern crate alloc; - -// For tests -#[cfg(feature = "std")] -extern crate std; - -pub mod ffi; -pub mod sphincssha2128fsimple; -pub mod sphincssha2128ssimple; -pub mod sphincssha2192fsimple; -pub mod sphincssha2192ssimple; -pub mod sphincssha2256fsimple; -pub mod sphincssha2256ssimple; -pub mod sphincsshake128fsimple; -pub mod sphincsshake128ssimple; -pub mod sphincsshake192fsimple; -pub mod sphincsshake192ssimple; -pub mod sphincsshake256fsimple; -pub mod sphincsshake256ssimple; - -pub use crate::sphincssha2128fsimple::{ - detached_sign as sphincssha2128fsimple_detached_sign, keypair as sphincssha2128fsimple_keypair, - open as sphincssha2128fsimple_open, public_key_bytes as sphincssha2128fsimple_public_key_bytes, - secret_key_bytes as sphincssha2128fsimple_secret_key_bytes, sign as sphincssha2128fsimple_sign, - signature_bytes as sphincssha2128fsimple_signature_bytes, - verify_detached_signature as sphincssha2128fsimple_verify_detached_signature, -}; -pub use crate::sphincssha2128ssimple::{ - detached_sign as sphincssha2128ssimple_detached_sign, keypair as sphincssha2128ssimple_keypair, - open as sphincssha2128ssimple_open, public_key_bytes as sphincssha2128ssimple_public_key_bytes, - secret_key_bytes as sphincssha2128ssimple_secret_key_bytes, sign as sphincssha2128ssimple_sign, - signature_bytes as sphincssha2128ssimple_signature_bytes, - verify_detached_signature as sphincssha2128ssimple_verify_detached_signature, -}; -pub use crate::sphincssha2192fsimple::{ - detached_sign as sphincssha2192fsimple_detached_sign, keypair as sphincssha2192fsimple_keypair, - open as sphincssha2192fsimple_open, public_key_bytes as sphincssha2192fsimple_public_key_bytes, - secret_key_bytes as sphincssha2192fsimple_secret_key_bytes, sign as sphincssha2192fsimple_sign, - signature_bytes as sphincssha2192fsimple_signature_bytes, - verify_detached_signature as sphincssha2192fsimple_verify_detached_signature, -}; -pub use crate::sphincssha2192ssimple::{ - detached_sign as sphincssha2192ssimple_detached_sign, keypair as sphincssha2192ssimple_keypair, - open as sphincssha2192ssimple_open, public_key_bytes as sphincssha2192ssimple_public_key_bytes, - secret_key_bytes as sphincssha2192ssimple_secret_key_bytes, sign as sphincssha2192ssimple_sign, - signature_bytes as sphincssha2192ssimple_signature_bytes, - verify_detached_signature as sphincssha2192ssimple_verify_detached_signature, -}; -pub use crate::sphincssha2256fsimple::{ - detached_sign as sphincssha2256fsimple_detached_sign, keypair as sphincssha2256fsimple_keypair, - open as sphincssha2256fsimple_open, public_key_bytes as sphincssha2256fsimple_public_key_bytes, - secret_key_bytes as sphincssha2256fsimple_secret_key_bytes, sign as sphincssha2256fsimple_sign, - signature_bytes as sphincssha2256fsimple_signature_bytes, - verify_detached_signature as sphincssha2256fsimple_verify_detached_signature, -}; -pub use crate::sphincssha2256ssimple::{ - detached_sign as sphincssha2256ssimple_detached_sign, keypair as sphincssha2256ssimple_keypair, - open as sphincssha2256ssimple_open, public_key_bytes as sphincssha2256ssimple_public_key_bytes, - secret_key_bytes as sphincssha2256ssimple_secret_key_bytes, sign as sphincssha2256ssimple_sign, - signature_bytes as sphincssha2256ssimple_signature_bytes, - verify_detached_signature as sphincssha2256ssimple_verify_detached_signature, -}; -pub use crate::sphincsshake128fsimple::{ - detached_sign as sphincsshake128fsimple_detached_sign, - keypair as sphincsshake128fsimple_keypair, open as sphincsshake128fsimple_open, - public_key_bytes as sphincsshake128fsimple_public_key_bytes, - secret_key_bytes as sphincsshake128fsimple_secret_key_bytes, - sign as sphincsshake128fsimple_sign, signature_bytes as sphincsshake128fsimple_signature_bytes, - verify_detached_signature as sphincsshake128fsimple_verify_detached_signature, -}; -pub use crate::sphincsshake128ssimple::{ - detached_sign as sphincsshake128ssimple_detached_sign, - keypair as sphincsshake128ssimple_keypair, open as sphincsshake128ssimple_open, - public_key_bytes as sphincsshake128ssimple_public_key_bytes, - secret_key_bytes as sphincsshake128ssimple_secret_key_bytes, - sign as sphincsshake128ssimple_sign, signature_bytes as sphincsshake128ssimple_signature_bytes, - verify_detached_signature as sphincsshake128ssimple_verify_detached_signature, -}; -pub use crate::sphincsshake192fsimple::{ - detached_sign as sphincsshake192fsimple_detached_sign, - keypair as sphincsshake192fsimple_keypair, open as sphincsshake192fsimple_open, - public_key_bytes as sphincsshake192fsimple_public_key_bytes, - secret_key_bytes as sphincsshake192fsimple_secret_key_bytes, - sign as sphincsshake192fsimple_sign, signature_bytes as sphincsshake192fsimple_signature_bytes, - verify_detached_signature as sphincsshake192fsimple_verify_detached_signature, -}; -pub use crate::sphincsshake192ssimple::{ - detached_sign as sphincsshake192ssimple_detached_sign, - keypair as sphincsshake192ssimple_keypair, open as sphincsshake192ssimple_open, - public_key_bytes as sphincsshake192ssimple_public_key_bytes, - secret_key_bytes as sphincsshake192ssimple_secret_key_bytes, - sign as sphincsshake192ssimple_sign, signature_bytes as sphincsshake192ssimple_signature_bytes, - verify_detached_signature as sphincsshake192ssimple_verify_detached_signature, -}; -pub use crate::sphincsshake256fsimple::{ - detached_sign as sphincsshake256fsimple_detached_sign, - keypair as sphincsshake256fsimple_keypair, open as sphincsshake256fsimple_open, - public_key_bytes as sphincsshake256fsimple_public_key_bytes, - secret_key_bytes as sphincsshake256fsimple_secret_key_bytes, - sign as sphincsshake256fsimple_sign, signature_bytes as sphincsshake256fsimple_signature_bytes, - verify_detached_signature as sphincsshake256fsimple_verify_detached_signature, -}; -pub use crate::sphincsshake256ssimple::{ - detached_sign as sphincsshake256ssimple_detached_sign, - keypair as sphincsshake256ssimple_keypair, open as sphincsshake256ssimple_open, - public_key_bytes as sphincsshake256ssimple_public_key_bytes, - secret_key_bytes as sphincsshake256ssimple_secret_key_bytes, - sign as sphincsshake256ssimple_sign, signature_bytes as sphincsshake256ssimple_signature_bytes, - verify_detached_signature as sphincsshake256ssimple_verify_detached_signature, -}; diff --git a/pqcrypto-sphincsplus/src/sphincssha2128fsimple.rs b/pqcrypto-sphincsplus/src/sphincssha2128fsimple.rs deleted file mode 100644 index 43b5d973..00000000 --- a/pqcrypto-sphincsplus/src/sphincssha2128fsimple.rs +++ /dev/null @@ -1,374 +0,0 @@ -//! sphincs-sha2-128f-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincssha2128fsimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincssha2128fsimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-sha2-128f-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHA2128FSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHA2128FSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincssha2128ssimple.rs b/pqcrypto-sphincsplus/src/sphincssha2128ssimple.rs deleted file mode 100644 index b8dd9c75..00000000 --- a/pqcrypto-sphincsplus/src/sphincssha2128ssimple.rs +++ /dev/null @@ -1,374 +0,0 @@ -//! sphincs-sha2-128s-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincssha2128ssimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincssha2128ssimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-sha2-128s-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHA2128SSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHA2128SSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincssha2192fsimple.rs b/pqcrypto-sphincsplus/src/sphincssha2192fsimple.rs deleted file mode 100644 index 70a04e3e..00000000 --- a/pqcrypto-sphincsplus/src/sphincssha2192fsimple.rs +++ /dev/null @@ -1,374 +0,0 @@ -//! sphincs-sha2-192f-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincssha2192fsimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincssha2192fsimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-sha2-192f-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHA2192FSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHA2192FSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincssha2192ssimple.rs b/pqcrypto-sphincsplus/src/sphincssha2192ssimple.rs deleted file mode 100644 index f23d110b..00000000 --- a/pqcrypto-sphincsplus/src/sphincssha2192ssimple.rs +++ /dev/null @@ -1,374 +0,0 @@ -//! sphincs-sha2-192s-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincssha2192ssimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincssha2192ssimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-sha2-192s-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHA2192SSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHA2192SSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincssha2256fsimple.rs b/pqcrypto-sphincsplus/src/sphincssha2256fsimple.rs deleted file mode 100644 index c62e93a1..00000000 --- a/pqcrypto-sphincsplus/src/sphincssha2256fsimple.rs +++ /dev/null @@ -1,374 +0,0 @@ -//! sphincs-sha2-256f-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincssha2256fsimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincssha2256fsimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-sha2-256f-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHA2256FSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHA2256FSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincssha2256ssimple.rs b/pqcrypto-sphincsplus/src/sphincssha2256ssimple.rs deleted file mode 100644 index a325c7c7..00000000 --- a/pqcrypto-sphincsplus/src/sphincssha2256ssimple.rs +++ /dev/null @@ -1,374 +0,0 @@ -//! sphincs-sha2-256s-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincssha2256ssimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincssha2256ssimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-sha2-256s-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!(PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_open, sm, pk) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHA2256SSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHA2256SSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincsshake128fsimple.rs b/pqcrypto-sphincsplus/src/sphincsshake128fsimple.rs deleted file mode 100644 index c76bf712..00000000 --- a/pqcrypto-sphincsplus/src/sphincsshake128fsimple.rs +++ /dev/null @@ -1,378 +0,0 @@ -//! sphincs-shake-128f-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincsshake128fsimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincsshake128fsimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-shake-128f-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!( - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_open, - sm, - pk - ) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE128FSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE128FSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincsshake128ssimple.rs b/pqcrypto-sphincsplus/src/sphincsshake128ssimple.rs deleted file mode 100644 index e40af481..00000000 --- a/pqcrypto-sphincsplus/src/sphincsshake128ssimple.rs +++ /dev/null @@ -1,378 +0,0 @@ -//! sphincs-shake-128s-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincsshake128ssimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincsshake128ssimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-shake-128s-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!( - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_open, - sm, - pk - ) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE128SSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE128SSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincsshake192fsimple.rs b/pqcrypto-sphincsplus/src/sphincsshake192fsimple.rs deleted file mode 100644 index db1d08d4..00000000 --- a/pqcrypto-sphincsplus/src/sphincsshake192fsimple.rs +++ /dev/null @@ -1,378 +0,0 @@ -//! sphincs-shake-192f-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincsshake192fsimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincsshake192fsimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-shake-192f-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!( - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_open, - sm, - pk - ) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE192FSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE192FSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincsshake192ssimple.rs b/pqcrypto-sphincsplus/src/sphincsshake192ssimple.rs deleted file mode 100644 index 9a287926..00000000 --- a/pqcrypto-sphincsplus/src/sphincsshake192ssimple.rs +++ /dev/null @@ -1,378 +0,0 @@ -//! sphincs-shake-192s-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincsshake192ssimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincsshake192ssimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-shake-192s-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!( - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_open, - sm, - pk - ) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE192SSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE192SSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincsshake256fsimple.rs b/pqcrypto-sphincsplus/src/sphincsshake256fsimple.rs deleted file mode 100644 index 2b65f1d5..00000000 --- a/pqcrypto-sphincsplus/src/sphincsshake256fsimple.rs +++ /dev/null @@ -1,378 +0,0 @@ -//! sphincs-shake-256f-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincsshake256fsimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincsshake256fsimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-shake-256f-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!( - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_open, - sm, - pk - ) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE256FSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE256FSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto-sphincsplus/src/sphincsshake256ssimple.rs b/pqcrypto-sphincsplus/src/sphincsshake256ssimple.rs deleted file mode 100644 index 50d66461..00000000 --- a/pqcrypto-sphincsplus/src/sphincsshake256ssimple.rs +++ /dev/null @@ -1,378 +0,0 @@ -//! sphincs-shake-256s-simple -//! -//! These bindings use the clean version from [PQClean][pqc] -//! -//! # Example -//! ``` -//! // if using pqcrypto-sphincsplus -//! use pqcrypto_sphincsplus::sphincsshake256ssimple::*; -//! // or if using the pqcrypto crate: -//! // use pqcrypto::sign::sphincsshake256ssimple::*; -//! let message = vec![0, 1, 2, 3, 4, 5]; -//! let (pk, sk) = keypair(); -//! let sm = sign(&message, &sk); -//! let verifiedmsg = open(&sm, &pk).unwrap(); -//! assert!(verifiedmsg == message); -//! ``` -//! -//! [pqc]: https://github.com/pqclean/pqclean/ - -// This file is generated. - -#[cfg(feature = "serialization")] -use serde::{Deserialize, Serialize}; -#[cfg(feature = "serialization")] -use serde_big_array::BigArray; - -use crate::ffi; -use alloc::vec::Vec; -use pqcrypto_traits::sign as primitive; -use pqcrypto_traits::{Error, Result}; - -macro_rules! simple_struct { - ($type: ident, $size: expr) => { - #[derive(Clone, Copy)] - #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] - pub struct $type( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] [u8; $size], - ); - - impl $type { - /// Generates an uninitialized object - /// - /// Used to pass to ``ffi`` interfaces. - /// - /// Internal use only! - fn new() -> Self { - $type([0u8; $size]) - } - } - - impl primitive::$type for $type { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// Construct this object from a byte slice - fn from_bytes(bytes: &[u8]) -> Result { - if bytes.len() != $size { - Err(Error::BadLength { - name: stringify!($type), - actual: bytes.len(), - expected: $size, - }) - } else { - let mut array = [0u8; $size]; - array.copy_from_slice(bytes); - Ok($type(array)) - } - } - } - - impl PartialEq for $type { - /// By no means constant time comparison - fn eq(&self, other: &Self) -> bool { - self.0 - .iter() - .zip(other.0.iter()) - .try_for_each(|(a, b)| if a == b { Ok(()) } else { Err(()) }) - .is_ok() - } - } - }; -} - -simple_struct!( - PublicKey, - ffi::PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -); -simple_struct!( - SecretKey, - ffi::PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -); - -#[derive(Clone, Copy)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct DetachedSignature( - #[cfg_attr(feature = "serialization", serde(with = "BigArray"))] - [u8; ffi::PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_BYTES], - usize, -); - -// for internal use -impl DetachedSignature { - fn new() -> Self { - DetachedSignature( - [0u8; ffi::PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_BYTES], - 0, - ) - } -} - -impl primitive::DetachedSignature for DetachedSignature { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.0[..self.1] - } - - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - let actual = bytes.len(); - let expected = ffi::PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_BYTES; - if actual > expected { - return Err(Error::BadLength { - name: "DetachedSignature", - actual, - expected, - }); - } - let mut array = [0u8; ffi::PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_BYTES]; - array[..bytes.len()].copy_from_slice(bytes); - Ok(DetachedSignature(array, actual)) - } -} - -#[derive(Clone)] -#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] -pub struct SignedMessage(Vec); -impl primitive::SignedMessage for SignedMessage { - /// Get this object as a byte slice - #[inline] - fn as_bytes(&self) -> &[u8] { - self.0.as_slice() - } - - /// Construct this object from a byte slice - #[inline] - fn from_bytes(bytes: &[u8]) -> Result { - Ok(SignedMessage(bytes.to_vec())) - } -} - -impl SignedMessage { - pub fn len(&self) -> usize { - self.0.len() - } -} - -/// Get the number of bytes for a public key -pub const fn public_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_PUBLICKEYBYTES -} - -/// Get the number of bytes for a secret key -pub const fn secret_key_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_SECRETKEYBYTES -} - -/// Get the number of bytes that a signature occupies -pub const fn signature_bytes() -> usize { - ffi::PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_CRYPTO_BYTES -} - -macro_rules! gen_keypair { - ($variant:ident) => {{ - let mut pk = PublicKey::new(); - let mut sk = SecretKey::new(); - assert_eq!( - unsafe { ffi::$variant(pk.0.as_mut_ptr(), sk.0.as_mut_ptr()) }, - 0 - ); - (pk, sk) - }}; -} - -/// Generate a sphincs-shake-256s-simple keypair -pub fn keypair() -> (PublicKey, SecretKey) { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_keypair!(PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_keypair); - } - } - gen_keypair!(PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_keypair) -} - -macro_rules! gen_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let max_len = $msg.len() + signature_bytes(); - let mut signed_msg = Vec::with_capacity(max_len); - let mut smlen: usize = 0; - unsafe { - ffi::$variant( - signed_msg.as_mut_ptr(), - &mut smlen as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - debug_assert!(smlen <= max_len, "exceeded vector capacity"); - signed_msg.set_len(smlen); - } - SignedMessage(signed_msg) - }}; -} - -/// Sign the message and return the signed message. -pub fn sign(msg: &[u8], sk: &SecretKey) -> SignedMessage { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return gen_signature!(PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign, msg, sk); - } - } - gen_signature!(PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign, msg, sk) -} - -macro_rules! open_signed { - ($variant:ident, $sm:ident, $pk:ident) => {{ - let mut m: Vec = Vec::with_capacity($sm.len()); - let mut mlen: usize = 0; - match unsafe { - ffi::$variant( - m.as_mut_ptr(), - &mut mlen as *mut usize, - $sm.0.as_ptr(), - $sm.len(), - $pk.0.as_ptr(), - ) - } { - 0 => { - unsafe { m.set_len(mlen) }; - Ok(m) - } - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Open the signed message and if verification succeeds return the message -pub fn open( - sm: &SignedMessage, - pk: &PublicKey, -) -> core::result::Result, primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return open_signed!(PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_open, sm, pk); - } - } - open_signed!( - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_open, - sm, - pk - ) -} - -macro_rules! detached_signature { - ($variant:ident, $msg:ident, $sk:ident) => {{ - let mut sig = DetachedSignature::new(); - unsafe { - ffi::$variant( - sig.0.as_mut_ptr(), - &mut sig.1 as *mut usize, - $msg.as_ptr(), - $msg.len(), - $sk.0.as_ptr(), - ); - } - sig - }}; -} - -/// Create a detached signature on the message -pub fn detached_sign(msg: &[u8], sk: &SecretKey) -> DetachedSignature { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return detached_signature!( - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_signature, - msg, - sk - ); - } - } - detached_signature!( - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_signature, - msg, - sk - ) -} - -macro_rules! verify_detached_sig { - ($variant:ident, $sig:ident, $msg:ident, $pk:ident) => {{ - let res = unsafe { - ffi::$variant( - $sig.0.as_ptr(), - $sig.1, - $msg.as_ptr(), - $msg.len(), - $pk.0.as_ptr(), - ) - }; - match res { - 0 => Ok(()), - -1 => Err(primitive::VerificationError::InvalidSignature), - _ => Err(primitive::VerificationError::UnknownVerificationError), - } - }}; -} - -/// Verify the detached signature -pub fn verify_detached_signature( - sig: &DetachedSignature, - msg: &[u8], - pk: &PublicKey, -) -> core::result::Result<(), primitive::VerificationError> { - #[cfg(all(enable_x86_avx2, feature = "avx2"))] - { - if std::is_x86_feature_detected!("avx2") { - return verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE256SSIMPLE_AVX2_crypto_sign_verify, - sig, - msg, - pk - ); - } - } - verify_detached_sig!( - PQCLEAN_SPHINCSSHAKE256SSIMPLE_CLEAN_crypto_sign_verify, - sig, - msg, - pk - ) -} - -#[cfg(test)] -mod test { - use super::*; - use rand::prelude::*; - - #[test] - pub fn test_sign() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - - let message = (0..len).map(|_| rng.gen::()).collect::>(); - let (pk, sk) = keypair(); - let sm = sign(&message, &sk); - let verifiedmsg = open(&sm, &pk).unwrap(); - assert!(verifiedmsg == message); - } - - #[test] - pub fn test_sign_detached() { - let mut rng = rand::thread_rng(); - let len: u16 = rng.gen(); - let message = (0..len).map(|_| rng.gen::()).collect::>(); - - let (pk, sk) = keypair(); - let sig = detached_sign(&message, &sk); - assert!(verify_detached_signature(&sig, &message, &pk).is_ok()); - assert!(!verify_detached_signature(&sig, &message[..message.len() - 1], &pk).is_ok()); - } -} diff --git a/pqcrypto/Cargo.toml b/pqcrypto/Cargo.toml deleted file mode 100644 index 2c157268..00000000 --- a/pqcrypto/Cargo.toml +++ /dev/null @@ -1,33 +0,0 @@ -[package] -name = "pqcrypto" -description = "Post-Quantum cryptographic primitives" -readme = "README.md" -version = "0.17.0" -authors = ["Thom Wiggers "] -edition = "2021" -license = "MIT OR Apache-2.0" -homepage = "https://github.com/rustpq/" -repository = "https://github.com/rustpq/pqcrypto/" -keywords = ["cryptography", "post-quantum", "security"] -categories = ["cryptography"] - -[dependencies] -pqcrypto-traits = { path = "../pqcrypto-traits", version = "0.3.5" } -pqcrypto-kyber = { path = "../pqcrypto-kyber", version = "0.8.0", optional = true } -pqcrypto-classicmceliece = { path = "../pqcrypto-classicmceliece", version = "0.2.0", optional = true } -pqcrypto-hqc = { path = "../pqcrypto-hqc", version = "0.2.0", optional = true } -pqcrypto-dilithium = { path = "../pqcrypto-dilithium", version = "0.5.0", optional = true } -pqcrypto-falcon = { path = "../pqcrypto-falcon", version = "0.3.0", optional = true } -pqcrypto-sphincsplus = { path = "../pqcrypto-sphincsplus", version = "0.7.0", optional = true } - -[features] -default = ["pqcrypto-kyber","pqcrypto-classicmceliece","pqcrypto-hqc","pqcrypto-dilithium","pqcrypto-falcon","pqcrypto-sphincsplus",] -cryptographically-insecure = [] -serialization = ["pqcrypto-kyber/serialization","pqcrypto-classicmceliece/serialization","pqcrypto-hqc/serialization","pqcrypto-dilithium/serialization","pqcrypto-falcon/serialization","pqcrypto-sphincsplus/serialization",] - -[badges] -travis-ci = { repository = "rustpq/pqcrypto", branch = "master" } -maintenance = { status = "actively-developed" } - -[package.metadata.docs.rs] -all-features = true \ No newline at end of file diff --git a/pqcrypto/README.md b/pqcrypto/README.md deleted file mode 100644 index 7e0d726a..00000000 --- a/pqcrypto/README.md +++ /dev/null @@ -1,38 +0,0 @@ -# Post-Quantum cryptographic algorithms - -This project contains Post-Quantum cryptographic algorithms that participate in -the [NIST PQC standardization effort][nistpqc]. It is currently a collection of -wrappers around C implementations from the [PQClean][pqclean] project. - - -## Included algorithms - -This super-crate contains the following cryptographic algorithms: - -## Key-Encapsulation Mechanisms - -* [``pqcrypto-kyber``](https://crates.io/crates/pqcrypto-kyber) -* [``pqcrypto-classicmceliece``](https://crates.io/crates/pqcrypto-classicmceliece) -* [``pqcrypto-hqc``](https://crates.io/crates/pqcrypto-hqc) - -## Signature Schemes - -* [``pqcrypto-dilithium``](https://crates.io/crates/pqcrypto-dilithium) -* [``pqcrypto-falcon``](https://crates.io/crates/pqcrypto-falcon) -* [``pqcrypto-sphincsplus``](https://crates.io/crates/pqcrypto-sphincsplus) - -## Serialization - -If you want `serde` support, enable the `serialization` feature. -You may also enable it for individual algorithms via `pqcrypto-{alg}/serialization`. - -## License - -The wrappers and wrapper generation scripts in this project are covered by the -MIT or Apache 2.0 licenses, at your choice. - -The implementations we link to are not, however. Please see the [PQClean][pqclean] -project for the appropriate licenses. - -[pqclean]: https://github.com/PQClean/PQClean/ -[nistpqc]: https://nist.gov/pqc/ \ No newline at end of file diff --git a/pqcrypto/examples/keygen.rs b/pqcrypto/examples/keygen.rs deleted file mode 100644 index 486b28ba..00000000 --- a/pqcrypto/examples/keygen.rs +++ /dev/null @@ -1,14 +0,0 @@ -use std::fs::File; -use std::io::prelude::*; - -use pqcrypto::prelude::*; -use pqcrypto::sign::dilithium2::*; - -fn main() -> std::io::Result<()> { - let mut pubfile = File::create("publickey.bin")?; - let mut secfile = File::create("secretkey.bin")?; - let (pk, sk) = keypair(); - pubfile.write_all(pk.as_bytes())?; - secfile.write_all(sk.as_bytes())?; - Ok(()) -} diff --git a/pqcrypto/examples/signer.rs b/pqcrypto/examples/signer.rs deleted file mode 100644 index 2d06302d..00000000 --- a/pqcrypto/examples/signer.rs +++ /dev/null @@ -1,25 +0,0 @@ -use std::fs::{self, File}; -use std::io::prelude::*; - -use pqcrypto::prelude::*; -use pqcrypto::sign::dilithium2::*; - -fn parseargs() -> (String, String, String) { - let args: Vec = std::env::args().collect(); - if args.len() != 4 { - panic!("Usage: {} sk in out", args[0]); - } - (args[1].clone(), args[2].clone(), args[3].clone()) -} - -fn main() -> Result<(), Box> { - let (sk_filename, in_filename, sig_filename) = parseargs(); - let mut sigfile = File::create(sig_filename)?; - - let sk = SecretKey::from_bytes(&fs::read(sk_filename)?)?; - let signature = detached_sign(&fs::read(in_filename)?, &sk); - - sigfile.write_all(signature.as_bytes())?; - - Ok(()) -} diff --git a/pqcrypto/examples/verifier.rs b/pqcrypto/examples/verifier.rs deleted file mode 100644 index 08444b80..00000000 --- a/pqcrypto/examples/verifier.rs +++ /dev/null @@ -1,27 +0,0 @@ -use std::fs; - -use pqcrypto::prelude::*; -use pqcrypto::sign::dilithium2::*; - -fn parseargs() -> (String, String, String) { - let args: Vec = std::env::args().collect(); - if args.len() != 4 { - panic!("Usage: {} pk msg sig", args[0]); - } - (args[1].clone(), args[2].clone(), args[3].clone()) -} - -fn main() -> Result<(), Box> { - let (pk_filename, in_filename, sig_filename) = parseargs(); - let pk = PublicKey::from_bytes(&fs::read(pk_filename)?)?; - let msg = &fs::read(in_filename)?; - let sig = DetachedSignature::from_bytes(&fs::read(sig_filename)?)?; - - if let Ok(()) = verify_detached_signature(&sig, msg, &pk) { - println!("Verification success!"); - } else { - println!("Verification failed!"); - } - - Ok(()) -} diff --git a/pqcrypto/src/lib.rs b/pqcrypto/src/lib.rs deleted file mode 100644 index 750db923..00000000 --- a/pqcrypto/src/lib.rs +++ /dev/null @@ -1,41 +0,0 @@ -/// Post-Quantum cryptographic primitives -/// -/// Packages the [PQClean][pqclean] project as Rust crates -/// -/// [pqclean]: https://github.com/PQClean/PQClean/ -pub use pqcrypto_traits as traits; - -pub mod prelude { - pub use pqcrypto_traits::kem::{ - Ciphertext as _, PublicKey as _, SecretKey as _, SharedSecret as _, - }; - pub use pqcrypto_traits::sign::{ - DetachedSignature as _, PublicKey as _, SecretKey as _, SignedMessage as _, - }; -} - -pub mod kem { - #[cfg(feature = "pqcrypto-classicmceliece")] - pub use pqcrypto_classicmceliece::{ - mceliece348864, mceliece348864f, mceliece460896, mceliece460896f, mceliece6688128, - mceliece6688128f, mceliece6960119, mceliece6960119f, mceliece8192128, mceliece8192128f, - }; - #[cfg(feature = "pqcrypto-hqc")] - pub use pqcrypto_hqc::{hqc128, hqc192, hqc256}; - #[cfg(feature = "pqcrypto-kyber")] - pub use pqcrypto_kyber::{kyber1024, kyber512, kyber768}; -} - -pub mod sign { - #[cfg(feature = "pqcrypto-dilithium")] - pub use pqcrypto_dilithium::{dilithium2, dilithium3, dilithium5}; - #[cfg(feature = "pqcrypto-falcon")] - pub use pqcrypto_falcon::{falcon1024, falcon512}; - #[cfg(feature = "pqcrypto-sphincsplus")] - pub use pqcrypto_sphincsplus::{ - sphincssha2128fsimple, sphincssha2128ssimple, sphincssha2192fsimple, sphincssha2192ssimple, - sphincssha2256fsimple, sphincssha2256ssimple, sphincsshake128fsimple, - sphincsshake128ssimple, sphincsshake192fsimple, sphincsshake192ssimple, - sphincsshake256fsimple, sphincsshake256ssimple, - }; -}