pub mod selftests;
pub use selftests::{run_power_up_tests, FipsError};
const MODULE_HASH: &str = env!("FIPS_MODULE_HASH");
const BUILD_TIMESTAMP: &str = env!("FIPS_BUILD_TIMESTAMP");
pub fn verify_module_integrity() -> Result<(), FipsError> {
if MODULE_HASH == "UNKNOWN" {
eprintln!("WARNING: Module integrity hash not available");
eprintln!("This may indicate a build system issue or development build");
return Err(FipsError::IntegrityCheckFailed);
}
if MODULE_HASH.len() != 64 {
return Err(FipsError::IntegrityCheckFailed);
}
if !MODULE_HASH.chars().all(|c| c.is_ascii_hexdigit()) {
return Err(FipsError::IntegrityCheckFailed);
}
Ok(())
}
pub fn module_integrity_info() -> ModuleIntegrityInfo {
ModuleIntegrityInfo {
hash: MODULE_HASH,
build_timestamp: BUILD_TIMESTAMP,
fips_mode: is_fips_mode(),
}
}
#[derive(Debug, Clone)]
pub struct ModuleIntegrityInfo {
pub hash: &'static str,
pub build_timestamp: &'static str,
pub fips_mode: bool,
}
impl std::fmt::Display for ModuleIntegrityInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "FIPS 140-3 Module Integrity Information:")?;
writeln!(f, " Module Hash: {}", self.hash)?;
writeln!(f, " Build Time: {}", self.build_timestamp)?;
writeln!(
f,
" FIPS Mode: {}",
if self.fips_mode { "Enabled" } else { "Ready" }
)?;
Ok(())
}
}
pub fn is_fips_mode() -> bool {
cfg!(feature = "fips")
}
pub fn fips_status() -> &'static str {
if is_fips_mode() {
"FIPS 140-3 Mode Enabled"
} else {
"FIPS 140-3 Ready"
}
}