diff --git a/crates/ccc-crypto-boring/src/lib.rs b/crates/ccc-crypto-boring/src/lib.rs index c63e14c..61d46d9 100644 --- a/crates/ccc-crypto-boring/src/lib.rs +++ b/crates/ccc-crypto-boring/src/lib.rs @@ -2,17 +2,25 @@ //! //! BoringSSL implementation of the CCC provider traits, through Cloudflare's //! [`boring`] crate. It adds a native library family next to wolfSSL and -//! RustCrypto (design: "Provider diversity"). Built with the `fips` -//! feature it links BoringCrypto, the FIPS 140 validated module, and can -//! supply a suite's vetted layer; otherwise `fips_mode()` is `false`. +//! RustCrypto (design: "Provider diversity"). +//! +//! With the `fips` feature, `boring-sys` builds BoringCrypto in FIPS mode. +//! The vendored source is the update stream of a validated module, not a +//! certified build; a certified build links a validated `bcm.o` (`boring`'s +//! `fips-precompiled`). BoringSSL's FIPS build needs ELF objects, so it +//! works on Linux and Android only, not on macOS or iOS. `fips_mode()` is +//! `true` only for a `fips` build that reports FIPS mode at run time. //! //! Licences: `boring` is Apache-2.0 and `boring-sys` is MIT. The vendored //! BoringSSL (libcrypto) is Apache-2.0 throughout, including the files that //! came from OpenSSL; only its Go test harness, which is not linked, is under //! the Go licence. //! -//! The crate is independent of the AWS-LC provider (itself a BoringSSL fork): -//! the two never share a build or a symbol namespace. +//! The crate does not depend on the AWS-LC provider (itself a BoringSSL +//! fork); the two are separate builds of separate sources. +//! +//! Call [`init()`] once at start-up to register it in the global +//! [`ProviderRegistry`][ccc_crypto_core::registry::ProviderRegistry]. #![forbid(unsafe_code)] @@ -22,7 +30,17 @@ pub mod hash; pub mod kdf; pub mod kem; pub mod mac; +pub mod provider; pub mod self_test; +pub use provider::BoringProvider; + /// The provider's registry name. pub const PROVIDER_NAME: &str = "boring"; + +/// Register a [`BoringProvider`] in the global registry. +pub fn init() { + ccc_crypto_core::ProviderRegistry::global() + .register(PROVIDER_NAME, Box::new(BoringProvider::new())); + log::info!("[ccc-crypto-boring] provider registered"); +} diff --git a/crates/ccc-crypto-boring/src/provider.rs b/crates/ccc-crypto-boring/src/provider.rs new file mode 100644 index 0000000..97d822a --- /dev/null +++ b/crates/ccc-crypto-boring/src/provider.rs @@ -0,0 +1,182 @@ +//! [`BoringProvider`]: 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, hash, kdf, kem, mac, self_test, PROVIDER_NAME}; + +/// The BoringSSL provider. Self-test, benchmark and capabilities are +/// computed lazily on first use and cached. +#[derive(Default)] +pub struct BoringProvider { + caps: OnceLock, + bench: OnceLock, + kats: OnceLock, +} + +impl BoringProvider { + /// 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-boring] running throughput benchmark…"); + capabilities::run_benchmark() + }) + } +} + +impl AeadProvider for BoringProvider { + 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 BoringProvider { + 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 BoringProvider { + 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 BoringProvider { + fn hash(&self, algo: HashAlgorithm, data: &[u8]) -> Result, CryptoError> { + hash::hash(algo, data) + } +} + +impl KemProvider for BoringProvider { + 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 BoringProvider { + fn provider_name(&self) -> &'static str { + PROVIDER_NAME + } + + /// `true` only for a `fips` build whose BoringCrypto module reports FIPS + /// mode at run time (`FIPS_mode()`). A default build is never FIPS. + fn fips_mode(&self) -> bool { + cfg!(feature = "fips") && boring::fips::enabled() + } + + 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 = BoringProvider::new(); + assert_eq!(p.provider_name(), "boring"); + assert_eq!(p.fips_mode(), cfg!(feature = "fips")); + assert!(p.self_test().all_passed); + } + + #[test] + fn trait_objects_work() { + let p: Box = Box::new(BoringProvider::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"); + } +}