From 355bfe3c2ec880931ec6648e9c8f4bcb93da8419 Mon Sep 17 00:00:00 2001 From: JohnE Date: Mon, 16 Nov 2026 03:51:42 -0500 Subject: [PATCH] oqs: add experimental liboqs provider crate as a stub Without the liboqs feature it offers nothing, so default builds need no cmake. The feature builds once the KEM and self-test land. Refs: R3-5 --- Cargo.lock | 89 +++++++- Cargo.toml | 1 + crates/ccc-crypto-oqs/Cargo.toml | 25 +++ crates/ccc-crypto-oqs/src/lib.rs | 44 ++++ crates/ccc-crypto-oqs/src/provider.rs | 289 ++++++++++++++++++++++++++ 5 files changed, 447 insertions(+), 1 deletion(-) create mode 100644 crates/ccc-crypto-oqs/Cargo.toml create mode 100644 crates/ccc-crypto-oqs/src/lib.rs create mode 100644 crates/ccc-crypto-oqs/src/provider.rs diff --git a/Cargo.lock b/Cargo.lock index 1ef2c9d..6ae35c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -179,6 +179,26 @@ version = "1.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" +[[package]] +name = "bindgen" +version = "0.71.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f58bf3d7db68cfbac37cfc485a8d711e87e064c3d0fe0435b92f7a407f9d6b3" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "itertools", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn", +] + [[package]] name = "bindgen" version = "0.72.1" @@ -242,6 +262,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "build-deps" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64f14468960818ce4f3e3553c32d524446687884f8e7af5d3e252331d8a87e43" +dependencies = [ + "glob", +] + [[package]] name = "cc" version = "1.2.56" @@ -292,6 +321,18 @@ dependencies = [ "zeroize", ] +[[package]] +name = "ccc-crypto-oqs" +version = "0.1.0" +dependencies = [ + "ccc-crypto-core", + "getrandom", + "log", + "oqs", + "sha3 0.12.0", + "zeroize", +] + [[package]] name = "ccc-crypto-rustcrypto" version = "0.1.0" @@ -325,7 +366,7 @@ name = "ccc-crypto-wolfssl" version = "0.2.0" dependencies = [ "argon2 0.5.3", - "bindgen", + "bindgen 0.72.1", "ccc-crypto-core", "cmake", "hex", @@ -478,6 +519,16 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "cstr_core" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd98742e4fdca832d40cab219dc2e3048de17d873248f83f17df47c1bea70956" +dependencies = [ + "cty", + "memchr", +] + [[package]] name = "ctr" version = "0.10.1" @@ -496,6 +547,12 @@ dependencies = [ "cmov", ] +[[package]] +name = "cty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" + [[package]] name = "curve25519-dalek" version = "5.0.0" @@ -870,6 +927,30 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "oqs" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48caac02cf42ba00b865a747e332828a75341d97ae35ad1ae9785e56de212e78" +dependencies = [ + "cstr_core", + "libc", + "oqs-sys", +] + +[[package]] +name = "oqs-sys" +version = "0.11.0+liboqs-0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac6d66ee528a895ce5cc08851698d109c5d7ee5d7a0b3b40d61550eda91e414f" +dependencies = [ + "bindgen 0.71.1", + "build-deps", + "cmake", + "libc", + "pkg-config", +] + [[package]] name = "password-hash" version = "0.5.0" @@ -910,6 +991,12 @@ dependencies = [ "spki", ] +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + [[package]] name = "poly1305" version = "0.9.1" diff --git a/Cargo.toml b/Cargo.toml index c8ddd20..5714764 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "crates/ccc-cascade", "crates/ccc-crypto-wolfssl", "crates/ccc-crypto-rustcrypto", + "crates/ccc-crypto-oqs", "tests/conformance", ] diff --git a/crates/ccc-crypto-oqs/Cargo.toml b/crates/ccc-crypto-oqs/Cargo.toml new file mode 100644 index 0000000..a26cdd0 --- /dev/null +++ b/crates/ccc-crypto-oqs/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "ccc-crypto-oqs" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +description = "Experimental liboqs provider for the CCC crypto system (post-quantum KEMs)." + +[dependencies] +ccc-crypto-core.workspace = true +zeroize.workspace = true +log.workspace = true + +# liboqs 0.13.0, built from the vendored C source with cmake (no OpenSSL). +oqs = { version = "0.11", default-features = false, features = ["std", "vendored", "classic_mceliece", "hqc", "frodokem"], optional = true } +# The self-test's seeded stream and digests. +sha3 = { version = "0.12", optional = true } +getrandom = { version = "0.4", optional = true } + +[features] +# Off by default: without it the crate is a stub provider that offers +# nothing, so a default workspace build never needs cmake or liboqs. +default = [] +liboqs = ["dep:oqs", "dep:sha3", "dep:getrandom"] + diff --git a/crates/ccc-crypto-oqs/src/lib.rs b/crates/ccc-crypto-oqs/src/lib.rs new file mode 100644 index 0000000..a465533 --- /dev/null +++ b/crates/ccc-crypto-oqs/src/lib.rs @@ -0,0 +1,44 @@ +//! # ccc-crypto-oqs +//! +//! Experimental provider over [liboqs](https://openquantumsafe.org) (R3-5). +//! It offers post-quantum KEMs that neither wolfCrypt nor RustCrypto has, and +//! it is the independent oracle for the RustCrypto Classic McEliece code. +//! +//! liboqs states that it is for prototyping, not production. So this +//! provider only adds layers: it is never a suite's vetted layer, and it +//! offers no AEAD, KDF, MAC or hash. +//! +//! # Features +//! +//! - `liboqs` (off by default): build liboqs 0.13.0 from the vendored C +//! source with cmake and offer Classic-McEliece-460896f, HQC-192 and +//! FrodoKEM-976-SHAKE. Without it the crate builds a stub provider that +//! offers nothing, so a default workspace build needs no cmake. +//! +//! Call [`init()`] once at start-up to register it in the global +//! [`ProviderRegistry`][ccc_crypto_core::registry::ProviderRegistry]. + +#[cfg(feature = "liboqs")] +pub mod kem; +pub mod provider; +#[cfg(feature = "liboqs")] +pub mod self_test; + +pub use provider::OqsProvider; + +/// The provider's registry name. +pub const PROVIDER_NAME: &str = "oqs"; + +/// Register an [`OqsProvider`] in the global registry. +pub fn init() { + ccc_crypto_core::ProviderRegistry::global() + .register(PROVIDER_NAME, Box::new(OqsProvider::new())); + log::info!( + "[ccc-crypto-oqs] provider registered (liboqs {})", + if cfg!(feature = "liboqs") { + "enabled" + } else { + "not compiled" + } + ); +} diff --git a/crates/ccc-crypto-oqs/src/provider.rs b/crates/ccc-crypto-oqs/src/provider.rs new file mode 100644 index 0000000..53ea3a7 --- /dev/null +++ b/crates/ccc-crypto-oqs/src/provider.rs @@ -0,0 +1,289 @@ +//! [`OqsProvider`]: the [`CryptoProvider`] implementation. +//! +//! Only KEMs are offered. Every AEAD, KDF, MAC and hash call fails with +//! `UnsupportedAlgorithm`; without the `liboqs` feature every KEM call fails +//! with `FeatureNotCompiled`. + +use std::sync::OnceLock; + +use zeroize::Zeroizing; + +use ccc_crypto_core::{ + algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, KemAlgorithm, MacAlgorithm}, + capabilities::{AlgorithmCapability, ProviderCapabilities}, + error::CryptoError, + provider::{AeadProvider, CryptoProvider, HashProvider, KdfProvider, KemProvider, MacProvider}, + types::{BenchmarkReport, KemEncapResult, KemKeyPair, SelfTestReport}, +}; + +use crate::PROVIDER_NAME; + +/// The liboqs provider. The self-test and capabilities are computed on +/// first use and cached. +#[derive(Default)] +pub struct OqsProvider { + caps: OnceLock, + kats: OnceLock, +} + +impl OqsProvider { + /// Create a provider; no work is done until it is queried. + pub fn new() -> Self { + Self::default() + } + + fn kats(&self) -> &SelfTestReport { + self.kats.get_or_init(run_self_test) + } +} + +#[cfg(feature = "liboqs")] +fn run_self_test() -> SelfTestReport { + crate::self_test::run() +} + +#[cfg(not(feature = "liboqs"))] +fn run_self_test() -> SelfTestReport { + SelfTestReport::finalise(PROVIDER_NAME, Vec::new()) +} + +fn unsupported(what: &str) -> Result { + Err(CryptoError::UnsupportedAlgorithm(format!( + "the oqs provider offers KEMs only, not {what}" + ))) +} + +impl AeadProvider for OqsProvider { + fn encrypt_aead( + &self, + algo: AeadAlgorithm, + _key: &[u8], + _nonce: &[u8], + _plaintext: &[u8], + _aad: &[u8], + ) -> Result, CryptoError> { + unsupported(algo.name()) + } + + fn decrypt_aead( + &self, + algo: AeadAlgorithm, + _key: &[u8], + _nonce: &[u8], + _ciphertext_and_tag: &[u8], + _aad: &[u8], + ) -> Result, CryptoError> { + unsupported(algo.name()) + } +} + +impl KdfProvider for OqsProvider { + fn derive_key( + &self, + algo: KdfAlgorithm, + _ikm: &[u8], + _salt: &[u8], + _info: &[u8], + _length: usize, + ) -> Result>, CryptoError> { + unsupported(algo.name()) + } +} + +impl MacProvider for OqsProvider { + fn compute_mac( + &self, + algo: MacAlgorithm, + _key: &[u8], + _data: &[u8], + ) -> Result, CryptoError> { + unsupported(algo.name()) + } + + fn verify_mac( + &self, + algo: MacAlgorithm, + _key: &[u8], + _data: &[u8], + _mac: &[u8], + ) -> Result { + unsupported(algo.name()) + } +} + +impl HashProvider for OqsProvider { + fn hash(&self, algo: HashAlgorithm, _data: &[u8]) -> Result, CryptoError> { + unsupported(algo.name()) + } +} + +#[cfg(feature = "liboqs")] +impl KemProvider for OqsProvider { + fn generate_keypair(&self, algo: KemAlgorithm) -> Result { + crate::kem::generate_keypair(algo) + } + + fn encapsulate( + &self, + algo: KemAlgorithm, + public_key: &[u8], + ) -> Result { + crate::kem::encapsulate(algo, public_key) + } + + fn decapsulate( + &self, + algo: KemAlgorithm, + private_key: &[u8], + ciphertext: &[u8], + ) -> Result>, CryptoError> { + crate::kem::decapsulate(algo, private_key, ciphertext) + } +} + +#[cfg(not(feature = "liboqs"))] +fn not_compiled(algo: KemAlgorithm) -> Result { + Err(CryptoError::FeatureNotCompiled(format!( + "{}: ccc-crypto-oqs was built without the `liboqs` feature", + algo.name() + ))) +} + +#[cfg(not(feature = "liboqs"))] +impl KemProvider for OqsProvider { + fn generate_keypair(&self, algo: KemAlgorithm) -> Result { + not_compiled(algo) + } + + fn encapsulate( + &self, + algo: KemAlgorithm, + _public_key: &[u8], + ) -> Result { + not_compiled(algo) + } + + fn decapsulate( + &self, + algo: KemAlgorithm, + _private_key: &[u8], + _ciphertext: &[u8], + ) -> Result>, CryptoError> { + not_compiled(algo) + } +} + +/// KEMs this build offers. +#[cfg(feature = "liboqs")] +fn offered() -> &'static [KemAlgorithm] { + &crate::kem::OFFERED +} + +#[cfg(not(feature = "liboqs"))] +fn offered() -> &'static [KemAlgorithm] { + &[] +} + +/// A KEM is available only when its KAT passed (fail closed, D-28). The +/// KAT runs keygen, encapsulation and decapsulation through the same code +/// as real calls, so no separate probe is needed. +fn probe_capabilities(self_test: &SelfTestReport) -> ProviderCapabilities { + let mut caps = ProviderCapabilities::empty(PROVIDER_NAME); + for algo in KemAlgorithm::ALL { + let mut kats = self_test + .results + .iter() + .filter(|r| r.algo_id == algo as u32) + .peekable(); + let ok = offered().contains(&algo) && kats.peek().is_some() && kats.all(|r| r.passed); + let cap = if ok { + AlgorithmCapability { + available: true, + deterministic_io: false, + efficiency_score: 50, + reliability_score: 100, + } + } else { + AlgorithmCapability::unavailable() + }; + caps = caps.with_kem(algo, cap); + } + caps +} + +impl CryptoProvider for OqsProvider { + fn provider_name(&self) -> &'static str { + PROVIDER_NAME + } + + /// liboqs is not a FIPS module. + fn fips_mode(&self) -> bool { + false + } + + fn capabilities(&self) -> ProviderCapabilities { + self.caps + .get_or_init(|| probe_capabilities(self.kats())) + .clone() + } + + fn self_test(&self) -> SelfTestReport { + self.kats().clone() + } + + /// No AEAD throughput to measure. + fn benchmark(&self) -> BenchmarkReport { + BenchmarkReport { + provider_name: PROVIDER_NAME.into(), + results: Vec::new(), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn identity_and_non_kem_calls() { + let p = OqsProvider::new(); + assert_eq!(p.provider_name(), "oqs"); + assert!(!p.fips_mode()); + assert!(matches!( + p.hash(HashAlgorithm::Sha256, b"x"), + Err(CryptoError::UnsupportedAlgorithm(_)) + )); + assert!(p + .encrypt_aead(AeadAlgorithm::AesGcm256, &[0; 32], &[0; 12], b"", b"") + .is_err()); + assert!(p.benchmark().results.is_empty()); + } + + #[test] + fn capabilities_match_the_build() { + let p = OqsProvider::new(); + assert!(p.self_test().all_passed); + let caps = p.capabilities(); + assert_eq!(caps.provider_name, "oqs"); + for algo in KemAlgorithm::ALL { + assert_eq!( + caps.kem[&algo].available, + offered().contains(&algo), + "{}", + algo.name() + ); + } + assert!(caps.aead.is_empty()); + } + + #[cfg(not(feature = "liboqs"))] + #[test] + fn stub_offers_nothing() { + let p = OqsProvider::new(); + assert!(matches!( + p.generate_keypair(KemAlgorithm::Hqc192), + Err(CryptoError::FeatureNotCompiled(_)) + )); + assert!(!p.capabilities().has_any_available()); + } +}