wolfssl: report fips_mode from the FIPS self-test status

fips_mode must mean a validated module whose POST passed, not just a
build flag, so the policy can trust it.

Refs: R3-7
This commit is contained in:
JohnE 2026-11-16 03:55:35 -05:00
parent e68c3f5d46
commit d92de9e60b
3 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,77 @@
//! FIPS 140-3 status of the linked wolfCrypt.
//!
//! Only a build with the `fips` feature links the commercial wolfCrypt FIPS
//! module; every other build reports `false` without asking the library.
#[cfg(feature = "fips")]
mod ffi {
use std::os::raw::{c_char, c_int};
extern "C" {
/// 0 once the power-on self-test and the in-core integrity check
/// passed; a negative wolfCrypt error code otherwise.
pub fn wolfCrypt_GetStatus_fips() -> c_int;
/// The in-core hash the last integrity check computed (hex).
pub fn wolfCrypt_GetCoreHash_fips() -> *const c_char;
}
}
/// The FIPS module status: `Some(0)` when the self-tests passed, `Some(err)`
/// when they failed, `None` without the `fips` feature.
pub fn fips_status() -> Option<i32> {
#[cfg(feature = "fips")]
{
// SAFETY: a plain status query without arguments.
Some(unsafe { ffi::wolfCrypt_GetStatus_fips() })
}
#[cfg(not(feature = "fips"))]
{
None
}
}
/// `true` only when built with the `fips` feature and the module's
/// power-on self-test (including the in-core integrity check) passed.
pub fn fips_mode() -> bool {
fips_status() == Some(0)
}
/// The in-core hash wolfCrypt computed for this binary, to set as
/// `WOLFSSL_FIPS_CORE_HASH` when the integrity check fails after a relink.
/// `None` without the `fips` feature.
pub fn fips_core_hash() -> Option<String> {
#[cfg(feature = "fips")]
{
// SAFETY: returns NULL or a NUL-terminated static string.
let p = unsafe { ffi::wolfCrypt_GetCoreHash_fips() };
if p.is_null() {
return None;
}
// SAFETY: checked non-null above; wolfCrypt owns the buffer.
let s = unsafe { std::ffi::CStr::from_ptr(p) };
Some(s.to_string_lossy().into_owned())
}
#[cfg(not(feature = "fips"))]
{
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(feature = "fips"))]
#[test]
fn open_source_build_is_not_fips() {
assert_eq!(fips_status(), None);
assert!(!fips_mode());
assert_eq!(fips_core_hash(), None);
}
#[cfg(feature = "fips")]
#[test]
fn fips_build_passes_its_self_test() {
assert!(fips_mode(), "status {:?}, core hash {:?}", fips_status(), fips_core_hash());
}
}

View File

@ -12,6 +12,7 @@
pub mod aead;
pub mod capabilities;
pub mod fips;
pub mod hash;
pub mod kdf;
pub mod kem;

View File

@ -167,6 +167,11 @@ impl CryptoProvider for WolfSslProvider {
"wolfssl"
}
/// `true` only for a `fips` build whose FIPS self-tests passed.
fn fips_mode(&self) -> bool {
crate::fips::fips_mode()
}
fn capabilities(&self) -> ProviderCapabilities {
self.caps
.get_or_init(|| {