From 4b79e83063ba51d2ff968e96fe64f8ac95847b01 Mon Sep 17 00:00:00 2001 From: JohnE Date: Mon, 16 Nov 2026 21:04:42 -0500 Subject: [PATCH] boring: report FIPS mode only for a validated module A fips build from source is not a certified module; only fips-validated, which needs a prebuilt validated BoringCrypto, may report FIPS. Also wipes plaintext on a failed seal and the expected tag after verify. Refs: R3 --- crates/ccc-crypto-boring/Cargo.toml | 9 +++++++-- crates/ccc-crypto-boring/build.rs | 12 ++++++++++++ crates/ccc-crypto-boring/src/aead.rs | 15 +++++++++++---- crates/ccc-crypto-boring/src/lib.rs | 14 ++++++++------ crates/ccc-crypto-boring/src/mac.rs | 2 +- crates/ccc-crypto-boring/src/provider.rs | 9 +++++---- crates/ccc-crypto-boring/src/self_test.rs | 10 ++++++++-- 7 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 crates/ccc-crypto-boring/build.rs diff --git a/crates/ccc-crypto-boring/Cargo.toml b/crates/ccc-crypto-boring/Cargo.toml index 22d788a..57d3680 100644 --- a/crates/ccc-crypto-boring/Cargo.toml +++ b/crates/ccc-crypto-boring/Cargo.toml @@ -16,9 +16,14 @@ boring = { version = "=5.5.0", features = ["mlkem"] } [features] default = [] -# Build BoringCrypto, the FIPS 140 validated module (boring/fips). Needs the -# exact clang, Go and CMake versions of its security policy. +# Build BoringCrypto in FIPS mode (boring/fips) from the vendored source: the +# update stream of a validated module, not a certified build, so fips_mode() +# stays false. Linux and Android only. fips = ["boring/fips"] +# The builder attests that BORING_BSSL_FIPS_PATH points at a validated +# BoringCrypto build; only then does fips_mode() report true. build.rs +# refuses this feature without that variable. +fips-validated = ["fips"] [dev-dependencies] hex = "0.4" diff --git a/crates/ccc-crypto-boring/build.rs b/crates/ccc-crypto-boring/build.rs new file mode 100644 index 0000000..734b7cf --- /dev/null +++ b/crates/ccc-crypto-boring/build.rs @@ -0,0 +1,12 @@ +//! Refuses `fips-validated` unless the build links a prebuilt validated +//! BoringCrypto (`BORING_BSSL_FIPS_PATH`), so the provider never reports FIPS +//! mode for a module built from source. + +fn main() { + println!("cargo:rerun-if-env-changed=BORING_BSSL_FIPS_PATH"); + if std::env::var_os("CARGO_FEATURE_FIPS_VALIDATED").is_some() + && std::env::var_os("BORING_BSSL_FIPS_PATH").is_none_or(|p| p.is_empty()) + { + panic!("feature fips-validated needs BORING_BSSL_FIPS_PATH set to a validated BoringCrypto build"); + } +} diff --git a/crates/ccc-crypto-boring/src/aead.rs b/crates/ccc-crypto-boring/src/aead.rs index cf1f284..44c9f23 100644 --- a/crates/ccc-crypto-boring/src/aead.rs +++ b/crates/ccc-crypto-boring/src/aead.rs @@ -71,10 +71,17 @@ pub fn encrypt( out.extend_from_slice(plaintext); out.resize(plaintext.len() + TAG_LEN, 0); let (body, tag) = out.split_at_mut(plaintext.len()); - let written = ctx - .seal_in_place_mut(nonce, body, tag, aad) - .map_err(|e| CryptoError::InvalidInput(format!("{}: seal failed: {e}", algo.name())))? - .len(); + let written = match ctx.seal_in_place_mut(nonce, body, tag, aad) { + Ok(t) => t.len(), + Err(e) => { + // `out` may still hold the plaintext. + out.zeroize(); + return Err(CryptoError::InvalidInput(format!( + "{}: seal failed: {e}", + algo.name() + ))); + } + }; if written != TAG_LEN { out.zeroize(); return Err(CryptoError::InternalError(format!( diff --git a/crates/ccc-crypto-boring/src/lib.rs b/crates/ccc-crypto-boring/src/lib.rs index 61d46d9..17e0e54 100644 --- a/crates/ccc-crypto-boring/src/lib.rs +++ b/crates/ccc-crypto-boring/src/lib.rs @@ -6,18 +6,20 @@ //! //! 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. +//! certified build, so `fips_mode()` stays `false`. A certified build links +//! a prebuilt validated module through `BORING_BSSL_FIPS_PATH`; the +//! `fips-validated` feature attests that and is the only way `fips_mode()` +//! reports `true`. BoringSSL's FIPS build needs ELF objects, so it works on +//! Linux and Android only, not on macOS or iOS. //! //! 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 does not depend on the AWS-LC provider (itself a BoringSSL -//! fork); the two are separate builds of separate sources. +//! The crate does not depend on the AWS-LC provider, but AWS-LC is a +//! BoringSSL fork: they share code and assembly, so both report the lineage +//! `"boringssl"` and count as one library for diversity rules. //! //! Call [`init()`] once at start-up to register it in the global //! [`ProviderRegistry`][ccc_crypto_core::registry::ProviderRegistry]. diff --git a/crates/ccc-crypto-boring/src/mac.rs b/crates/ccc-crypto-boring/src/mac.rs index a49e402..ca70cfc 100644 --- a/crates/ccc-crypto-boring/src/mac.rs +++ b/crates/ccc-crypto-boring/src/mac.rs @@ -22,7 +22,7 @@ pub fn verify_mac( data: &[u8], mac: &[u8], ) -> Result { - let expected = compute_mac(algo, key, data)?; + let expected = zeroize::Zeroizing::new(compute_mac(algo, key, data)?); // `memcmp::eq` panics on unequal lengths; the length is public. Ok(expected.len() == mac.len() && boring::memcmp::eq(&expected, mac)) } diff --git a/crates/ccc-crypto-boring/src/provider.rs b/crates/ccc-crypto-boring/src/provider.rs index fd80dcd..035309a 100644 --- a/crates/ccc-crypto-boring/src/provider.rs +++ b/crates/ccc-crypto-boring/src/provider.rs @@ -138,10 +138,11 @@ impl CryptoProvider for BoringProvider { "boringssl" } - /// `true` only for a `fips` build whose BoringCrypto module reports FIPS - /// mode at run time (`FIPS_mode()`). A default build is never FIPS. + /// `true` only for a `fips-validated` build (a prebuilt validated + /// BoringCrypto, see build.rs) whose module reports FIPS mode at run time. + /// A `fips` build from source is not a certified module, so it is false. fn fips_mode(&self) -> bool { - cfg!(feature = "fips") && boring::fips::enabled() + cfg!(feature = "fips-validated") && boring::fips::enabled() } fn capabilities(&self) -> ProviderCapabilities { @@ -167,7 +168,7 @@ mod tests { fn identity_and_self_test() { let p = BoringProvider::new(); assert_eq!(p.provider_name(), "boring"); - assert_eq!(p.fips_mode(), cfg!(feature = "fips")); + assert_eq!(p.fips_mode(), cfg!(feature = "fips-validated")); assert!(p.self_test().all_passed); } diff --git a/crates/ccc-crypto-boring/src/self_test.rs b/crates/ccc-crypto-boring/src/self_test.rs index f6d11f9..9c1b12f 100644 --- a/crates/ccc-crypto-boring/src/self_test.rs +++ b/crates/ccc-crypto-boring/src/self_test.rs @@ -116,11 +116,17 @@ pub fn run() -> SelfTestReport { // ── Helpers ────────────────────────────────────────────────────────────────── +/// Decode a hex vector. Malformed hex yields an empty vector, which matches +/// no pinned output, so a typo fails the test instead of passing silently. fn hx(s: &str) -> Vec { let s = s.trim(); + if !s.len().is_multiple_of(2) { + return Vec::new(); + } (0..s.len() / 2) - .map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap_or(0)) - .collect() + .map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).ok()) + .collect::>>() + .unwrap_or_default() } fn expect(what: &str, got: &[u8], want_hex: &str) -> Result<(), String> {