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
This commit is contained in:
JohnE 2026-11-16 21:04:42 -05:00
parent a3b33ae10c
commit 4b79e83063
7 changed files with 52 additions and 19 deletions

View File

@ -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"

View File

@ -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");
}
}

View File

@ -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!(

View File

@ -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].

View File

@ -22,7 +22,7 @@ pub fn verify_mac(
data: &[u8],
mac: &[u8],
) -> Result<bool, CryptoError> {
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))
}

View File

@ -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);
}

View File

@ -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<u8> {
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::<Option<Vec<u8>>>()
.unwrap_or_default()
}
fn expect(what: &str, got: &[u8], want_hex: &str) -> Result<(), String> {