From 9310fe1262fafe7d0c01b73df40758b7b229cc6a Mon Sep 17 00:00:00 2001 From: JohnE Date: Mon, 16 Nov 2026 20:23:01 -0500 Subject: [PATCH] awslc: add AwsLcProvider and registry init Wires the primitives into CryptoProvider/KemProvider with cached self-test, benchmark, capabilities and FIPS status. Refs: R3 --- crates/ccc-crypto-awslc/src/lib.rs | 26 +++- crates/ccc-crypto-awslc/src/provider.rs | 190 ++++++++++++++++++++++++ 2 files changed, 213 insertions(+), 3 deletions(-) create mode 100644 crates/ccc-crypto-awslc/src/provider.rs diff --git a/crates/ccc-crypto-awslc/src/lib.rs b/crates/ccc-crypto-awslc/src/lib.rs index 98a6720..214fa29 100644 --- a/crates/ccc-crypto-awslc/src/lib.rs +++ b/crates/ccc-crypto-awslc/src/lib.rs @@ -1,8 +1,18 @@ //! # ccc-crypto-awslc //! -//! AWS-LC implementation of the CCC provider traits, built on `aws-lc-rs`. -//! It is the default vetted provider: with the `fips` feature it links -//! AWS-LC-FIPS, a FIPS 140-3 validated module. +//! AWS-LC implementation of the CCC provider traits, built on `aws-lc-rs` +//! (Apache-2.0 OR ISC). It is the default vetted provider and the FIPS +//! anchor: built with the `fips` feature it links AWS-LC-FIPS, a FIPS 140-3 +//! validated module, and [`AwsLcProvider`] reports `fips_mode()` only when +//! that module says it is in FIPS mode. +//! +//! Offered: AES-256-GCM, AES-256-GCM-SIV, ChaCha20-Poly1305, HKDF-SHA-256/ +//! 384/512, HMAC-SHA-256/384/512, SHA-256/384/512, SHA3-256/512, X25519 and +//! ML-KEM-768/1024. Each one is listed as available only after its +//! known-answer test passed. +//! +//! Call [`init()`] once at start-up to register it in the global +//! [`ProviderRegistry`][ccc_crypto_core::registry::ProviderRegistry]. #![forbid(unsafe_code)] @@ -13,7 +23,17 @@ pub mod hash; pub mod kdf; pub mod kem; pub mod mac; +pub mod provider; pub mod self_test; +pub use provider::AwsLcProvider; + /// The provider's registry name. pub const PROVIDER_NAME: &str = "awslc"; + +/// Register an [`AwsLcProvider`] in the global registry. +pub fn init() { + ccc_crypto_core::ProviderRegistry::global() + .register(PROVIDER_NAME, Box::new(AwsLcProvider::new())); + log::info!("[ccc-crypto-awslc] provider registered"); +} diff --git a/crates/ccc-crypto-awslc/src/provider.rs b/crates/ccc-crypto-awslc/src/provider.rs new file mode 100644 index 0000000..ffff54b --- /dev/null +++ b/crates/ccc-crypto-awslc/src/provider.rs @@ -0,0 +1,190 @@ +//! [`AwsLcProvider`]: the [`CryptoProvider`] implementation. + +use std::sync::OnceLock; + +use zeroize::Zeroizing; + +use ccc_crypto_core::{ + algorithms::{AeadAlgorithm, HashAlgorithm, KdfAlgorithm, KemAlgorithm, MacAlgorithm}, + capabilities::ProviderCapabilities, + error::CryptoError, + provider::{AeadProvider, CryptoProvider, HashProvider, KdfProvider, KemProvider, MacProvider}, + types::{BenchmarkReport, KemEncapResult, KemKeyPair, SelfTestReport}, +}; + +use crate::{aead, capabilities, fips, hash, kdf, kem, mac, self_test, PROVIDER_NAME}; + +/// The AWS-LC provider. Self-test, benchmark, capabilities and the FIPS +/// status are computed lazily on first use and cached. +#[derive(Default)] +pub struct AwsLcProvider { + caps: OnceLock, + bench: OnceLock, + kats: OnceLock, + fips: OnceLock, +} + +impl AwsLcProvider { + /// 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(self_test::run) + } + + fn bench(&self) -> &BenchmarkReport { + self.bench.get_or_init(|| { + log::info!("[ccc-crypto-awslc] running throughput benchmark…"); + capabilities::run_benchmark() + }) + } +} + +impl AeadProvider for AwsLcProvider { + fn encrypt_aead( + &self, + algo: AeadAlgorithm, + key: &[u8], + nonce: &[u8], + plaintext: &[u8], + aad: &[u8], + ) -> Result, CryptoError> { + aead::encrypt(algo, key, nonce, plaintext, aad) + } + + fn decrypt_aead( + &self, + algo: AeadAlgorithm, + key: &[u8], + nonce: &[u8], + ciphertext_and_tag: &[u8], + aad: &[u8], + ) -> Result, CryptoError> { + aead::decrypt(algo, key, nonce, ciphertext_and_tag, aad) + } +} + +impl KdfProvider for AwsLcProvider { + fn derive_key( + &self, + algo: KdfAlgorithm, + ikm: &[u8], + salt: &[u8], + info: &[u8], + length: usize, + ) -> Result>, CryptoError> { + kdf::derive_key(algo, ikm, salt, info, length) + } +} + +impl MacProvider for AwsLcProvider { + fn compute_mac( + &self, + algo: MacAlgorithm, + key: &[u8], + data: &[u8], + ) -> Result, CryptoError> { + mac::compute_mac(algo, key, data) + } + + fn verify_mac( + &self, + algo: MacAlgorithm, + key: &[u8], + data: &[u8], + mac_bytes: &[u8], + ) -> Result { + mac::verify_mac(algo, key, data, mac_bytes) + } +} + +impl HashProvider for AwsLcProvider { + fn hash(&self, algo: HashAlgorithm, data: &[u8]) -> Result, CryptoError> { + hash::hash(algo, data) + } +} + +impl KemProvider for AwsLcProvider { + fn generate_keypair(&self, algo: KemAlgorithm) -> Result { + kem::generate_keypair(algo) + } + + fn encapsulate( + &self, + algo: KemAlgorithm, + public_key: &[u8], + ) -> Result { + kem::encapsulate(algo, public_key) + } + + fn decapsulate( + &self, + algo: KemAlgorithm, + private_key: &[u8], + ciphertext: &[u8], + ) -> Result>, CryptoError> { + kem::decapsulate(algo, private_key, ciphertext) + } +} + +impl CryptoProvider for AwsLcProvider { + fn provider_name(&self) -> &'static str { + PROVIDER_NAME + } + + /// `true` only for a `fips` build whose AWS-LC-FIPS module reports FIPS + /// mode at runtime (D-26). + fn fips_mode(&self) -> bool { + *self.fips.get_or_init(|| { + let on = fips::fips_mode(); + log::info!( + "[ccc-crypto-awslc] AWS-LC {} FIPS mode: {on}", + fips::awslc_version() + ); + on + }) + } + + fn capabilities(&self) -> ProviderCapabilities { + self.caps + .get_or_init(|| capabilities::probe_capabilities(self.kats(), Some(self.bench()))) + .clone() + } + + fn self_test(&self) -> SelfTestReport { + self.kats().clone() + } + + fn benchmark(&self) -> BenchmarkReport { + self.bench().clone() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn identity_and_self_test() { + let p = AwsLcProvider::new(); + assert_eq!(p.provider_name(), "awslc"); + assert_eq!(p.fips_mode(), cfg!(feature = "fips")); + assert!(p.self_test().all_passed); + } + + #[test] + fn trait_objects_work() { + let p: Box = Box::new(AwsLcProvider::new()); + let key = [1u8; 32]; + let nonce = [2u8; 12]; + let ct = p + .encrypt_aead(AeadAlgorithm::AesGcm256, &key, &nonce, b"m", b"a") + .unwrap(); + let pt = p + .decrypt_aead(AeadAlgorithm::AesGcm256, &key, &nonce, &ct, b"a") + .unwrap(); + assert_eq!(pt, b"m"); + } +}