use crate::CryptoError;
pub fn fips_result(active: bool) -> Result<(), CryptoError> {
if active {
Ok(())
} else {
Err(CryptoError::NotFips)
}
}
pub fn assert_fips_module() -> Result<(), CryptoError> {
fips_result(aws_lc_rs::try_fips_mode().is_ok())
}
pub fn assert_fips_client_config(c: &rustls::ClientConfig) -> Result<(), CryptoError> {
fips_result(c.fips())
}
pub fn assert_fips_server_config(c: &rustls::ServerConfig) -> Result<(), CryptoError> {
fips_result(c.fips())
}
pub(crate) fn self_test_given(
runner: Option<fn() -> Result<(), ()>>,
fips_mode: impl FnOnce() -> bool,
) -> Result<(), CryptoError> {
let Some(run) = runner else {
return Err(CryptoError::SelfTestUnavailable);
};
if !fips_mode() {
return Err(CryptoError::NotFips);
}
run().map_err(|()| CryptoError::SelfTest)
}
pub fn self_test() -> Result<(), CryptoError> {
self_test_given(super::backend::self_test_runner(), || {
aws_lc_rs::try_fips_mode().is_ok()
})
}
pub fn certificate() -> crypto_profile::Certificate {
crypto_profile::certificate(crate::MODULE)
}
pub fn approval_given(fips_mode: bool, a: crypto_profile::Approval) -> crypto_profile::Approval {
if fips_mode {
a
} else {
crypto_profile::Approval::Unverified
}
}
pub fn approval(alg: crypto_profile::Algorithm) -> crypto_profile::Approval {
approval_given(
aws_lc_rs::try_fips_mode().is_ok(),
crypto_profile::capability(crate::MODULE, alg).approval,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_decision_has_both_branches() {
assert_eq!(fips_result(true), Ok(()));
assert_eq!(fips_result(false), Err(CryptoError::NotFips));
}
static RAN: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
fn passes() -> Result<(), ()> {
RAN.store(true, std::sync::atomic::Ordering::SeqCst);
Ok(())
}
fn fails() -> Result<(), ()> {
Err(())
}
fn never() -> Result<(), ()> {
unreachable!("the self-test ran before its checks passed")
}
#[test]
fn the_self_test_decision_refuses_before_it_runs() {
assert_eq!(
self_test_given(None, || unreachable!(
"the FIPS mode was read for an unavailable test"
)),
Err(CryptoError::SelfTestUnavailable)
);
assert_eq!(
self_test_given(Some(never), || false),
Err(CryptoError::NotFips)
);
assert_eq!(
self_test_given(Some(fails), || true),
Err(CryptoError::SelfTest)
);
RAN.store(false, std::sync::atomic::Ordering::SeqCst);
assert_eq!(self_test_given(Some(passes), || true), Ok(()));
assert!(RAN.load(std::sync::atomic::Ordering::SeqCst));
}
#[test]
fn the_self_test_matches_its_line() {
match crate::MODULE.line {
crypto_profile::ModuleLine::AwsLcFips4 => assert_eq!(self_test(), Ok(())),
crypto_profile::ModuleLine::AwsLcFips3 => {
assert_eq!(self_test(), Err(CryptoError::SelfTestUnavailable))
}
other => panic!("{other:?}: no expected self-test outcome"),
}
}
#[test]
fn the_fips_module_is_active_on_this_build() {
assert_eq!(assert_fips_module(), Ok(()));
}
#[test]
fn an_approval_is_void_outside_fips_mode() {
let a = crypto_profile::Approval::Approved { source: "x" };
assert_eq!(approval_given(true, a), a);
assert_eq!(
approval_given(false, a),
crypto_profile::Approval::Unverified
);
}
#[test]
fn approval_on_this_build_is_the_capability_cell() {
let a = crypto_profile::Algorithm::Hash(crypto_profile::HashAlgorithm::Sha256);
assert_eq!(
approval(a),
crypto_profile::capability(crate::MODULE, a).approval
);
}
#[test]
fn the_module_certificate_matches_its_line() {
if crate::MODULE.line == crypto_profile::ModuleLine::AwsLcFips3 {
assert_eq!(certificate().status, crypto_profile::CertStatus::Validated);
assert_eq!(
certificate().number,
Some(
if crate::MODULE.linkage == crypto_profile::Linkage::Static {
5314
} else {
5298
}
)
);
assert!(certificate().module_name.contains("3.1.0"));
} else {
assert_eq!(crate::MODULE.line, crypto_profile::ModuleLine::AwsLcFips4);
assert_eq!(certificate().status, crypto_profile::CertStatus::InProcess);
}
}
}