crypto-profile-awslc3 0.10.2

The crypto-profile backend on the AWS-LC FIPS 3.x module, through aws-lc-rs 1.17
Documentation
//! The crypto-profile backend on the AWS-LC FIPS 3.x module (aws-lc-rs 1.17, `fips`).
//! `rustls` is re-exported because its types are this crate's API; `aws-lc-rs` is not.
#![deny(missing_docs)]

pub use crypto_profile::*;
#[cfg(feature = "test-support")]
pub use rcgen;
pub use rustls;

/// `Admitted` is built only by `admit`, and its lists are read-only:
///
/// ```
/// use crypto_profile_awslc3::{KdfAlgorithm, MacAlgorithm, Options, Profile, admit};
/// let a = admit(Profile::Cnsa2, Options::default());
/// assert!(!a.macs().contains(&MacAlgorithm::HmacSha256));
/// assert!(!a.kdfs().contains(&KdfAlgorithm::HkdfSha256));
/// ```
///
/// ```
/// use crypto_profile_awslc3::{Options, Profile, admit};
/// let mut a = admit(Profile::Cnsa2, Options::default());
/// let x = a.macs()[0];
/// let _ = (&mut a, x);
/// ```
///
/// ```compile_fail,E0616
/// use crypto_profile_awslc3::{Options, Profile, admit};
/// let mut a = admit(Profile::Cnsa2, Options::default());
/// let x = a.macs()[0];
/// a.macs.push(x);
/// ```
///
/// ```
/// use crypto_profile_awslc3::{Options, Profile, admit};
/// let mut a = admit(Profile::Cnsa2, Options::default());
/// let x = a.kdfs()[0];
/// let _ = (&mut a, x);
/// ```
///
/// ```compile_fail,E0616
/// use crypto_profile_awslc3::{Options, Profile, admit};
/// let mut a = admit(Profile::Cnsa2, Options::default());
/// let x = a.kdfs()[0];
/// a.kdfs.push(x);
/// ```
#[allow(dead_code)]
mod admitted_is_read_only {}

/// An `ecdh::EphemeralKey` agrees once: `agree` consumes it.
///
/// ```
/// use crypto_profile_awslc3::{KdfAlgorithm, KeyAgreementAlgorithm, Options, Profile, admit, ecdh, kdf};
/// let a = admit(Profile::Fips1403, Options::default());
/// let peer = ecdh::EphemeralKey::generate(&a, KeyAgreementAlgorithm::EcdhP384).unwrap();
/// let spec = kdf::Spec::Hkdf { algorithm: KdfAlgorithm::HkdfSha384, salt: None, info: b"doc" };
/// let mine = ecdh::EphemeralKey::generate(&a, KeyAgreementAlgorithm::EcdhP384).unwrap();
/// let key = mine.agree(peer.public_key(), &spec, 32).unwrap();
/// assert_eq!(key.as_bytes().len(), 32);
/// ```
///
/// ```compile_fail,E0382
/// use crypto_profile_awslc3::{KdfAlgorithm, KeyAgreementAlgorithm, Options, Profile, admit, ecdh, kdf};
/// let a = admit(Profile::Fips1403, Options::default());
/// let peer = ecdh::EphemeralKey::generate(&a, KeyAgreementAlgorithm::EcdhP384).unwrap();
/// let spec = kdf::Spec::Hkdf { algorithm: KdfAlgorithm::HkdfSha384, salt: None, info: b"doc" };
/// let mine = ecdh::EphemeralKey::generate(&a, KeyAgreementAlgorithm::EcdhP384).unwrap();
/// let key = mine.agree(peer.public_key(), &spec, 32).unwrap();
/// let again = mine.agree(peer.public_key(), &spec, 32).unwrap();
/// ```
#[allow(dead_code)]
mod ephemeral_keys_agree_once {}

/// An `aead::Key` is not `Clone`, so its seal count cannot be copied:
///
/// ```
/// use crypto_profile_awslc3::{AeadAlgorithm, Options, Profile, admit, aead};
/// let a = admit(Profile::Fips1403, Options::default());
/// let key = aead::Key::new(&a, AeadAlgorithm::Aes256Gcm, &[7; 32]).unwrap();
/// assert_eq!(key.algorithm(), AeadAlgorithm::Aes256Gcm);
/// ```
///
/// ```compile_fail,E0599
/// use crypto_profile_awslc3::{AeadAlgorithm, Options, Profile, admit, aead};
/// let a = admit(Profile::Fips1403, Options::default());
/// let key = aead::Key::new(&a, AeadAlgorithm::Aes256Gcm, &[7; 32]).unwrap();
/// let copy = key.clone();
/// ```
#[allow(dead_code)]
mod aead_keys_are_not_clone {}

/// A `digest::Hasher` is not `Clone`, so a partial hash cannot be forked:
///
/// ```
/// use crypto_profile_awslc3::{HashAlgorithm, Options, Profile, admit, digest};
/// let a = admit(Profile::Fips1403, Options::default());
/// let hasher = digest::Hasher::new(&a, HashAlgorithm::Sha384).unwrap();
/// assert_eq!(hasher.algorithm(), HashAlgorithm::Sha384);
/// ```
///
/// ```compile_fail,E0599
/// use crypto_profile_awslc3::{HashAlgorithm, Options, Profile, admit, digest};
/// let a = admit(Profile::Fips1403, Options::default());
/// let hasher = digest::Hasher::new(&a, HashAlgorithm::Sha384).unwrap();
/// let copy = hasher.clone();
/// ```
#[allow(dead_code)]
mod hashers_are_not_clone {}

/// A `signature::KeyPair` is not `Clone`, so its private key is held once:
///
/// ```
/// use crypto_profile_awslc3::{Options, Profile, SignatureAlgorithm, admit, signature};
/// let a = admit(Profile::Fips1403, Options::default());
/// let key = signature::KeyPair::generate(&a, SignatureAlgorithm::EcdsaP384Sha384).unwrap();
/// assert_eq!(key.algorithm(), SignatureAlgorithm::EcdsaP384Sha384);
/// ```
///
/// ```compile_fail,E0599
/// use crypto_profile_awslc3::{Options, Profile, SignatureAlgorithm, admit, signature};
/// let a = admit(Profile::Fips1403, Options::default());
/// let key = signature::KeyPair::generate(&a, SignatureAlgorithm::EcdsaP384Sha384).unwrap();
/// let copy = key.clone();
/// ```
#[allow(dead_code)]
mod signature_key_pairs_are_not_clone {}

crypto_profile::provider!(crate::provider_arms);
mod provider_arms;

crypto_profile::backend_body!(crate::backend_arms);
mod backend_arms;
#[cfg(any(test, feature = "test-support"))]
pub use backend_body::test_support;
pub use backend_body::{
    Admitted, Resolved, admit, assert_admitted_approved, assert_module_approves, resolve,
};
pub use backend_body::{aead, csr, digest, ecdh, fips, hmac, kdf, random, sealbox, signature, tls};

/// The AWS-LC FIPS module (line and linkage) this build links. Its certificate status is
/// [`fips::certificate()`]; for 3.x the certified release is 3.1.0.
pub const MODULE: Module = Module {
    line: ModuleLine::AwsLcFips3,
    linkage: if cfg!(crypto_profile_static) {
        Linkage::Static
    } else {
        Linkage::Dynamic
    },
};

/// Returns the exact runtime-linked AWS-LC release, independently of its certificate release.
///
/// This initializes the linked library and also reflects its version with dynamic linkage.
pub fn awslc_version() -> &'static str {
    aws_lc_rs::awslc_version()
}

pub(crate) fn ensure(alg: Algorithm) -> Result<(), CryptoError> {
    ensure_available(MODULE, alg)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ensure_refuses_unavailable_keys_and_schemes() {
        for algorithm in [
            Algorithm::Signature(SignatureAlgorithm::MlDsa87),
            Algorithm::Scheme(TlsSignatureScheme::MlDsa87),
        ] {
            assert_eq!(ensure(algorithm), Err(CryptoError::Unavailable(algorithm)));
        }
        assert_eq!(
            ensure(Algorithm::Signature(SignatureAlgorithm::EcdsaP384Sha384)),
            Ok(())
        );
    }
}