pub mod audit;
pub mod convenience;
pub mod crypto_types;
pub mod error;
pub mod key_format;
pub mod logging;
pub mod selector;
pub mod serialization;
pub mod types;
pub mod zero_trust;
use std::sync::atomic::{AtomicBool, Ordering};
#[allow(deprecated)]
pub use crate::types::config::{
CoreConfig, EncryptionConfig, HardwareConfig, ProofComplexity, SignatureConfig, UseCaseConfig,
ZeroTrustConfig,
};
pub use audit::{
AuditConfig, AuditEvent, AuditEventBuilder, AuditEventType, AuditOutcome, AuditStorage,
FileAuditStorage,
};
pub use error::{CoreError, Result};
pub use crate::types::key_lifecycle::{
CustodianRole, KeyCustodian, KeyLifecycleRecord, KeyLifecycleState, KeyStateMachine,
StateTransition,
};
pub use crate::types::traits::{
ContinuousVerifiable, DataCharacteristics, HardwareCapabilities, HardwareInfo, HardwareType,
PatternType, ProofOfPossession, SchemeSelector, VerificationStatus, ZeroTrustAuthenticable,
};
pub use selector::{
CLASSICAL_AES_GCM,
CLASSICAL_ED25519,
CryptoPolicyEngine,
DEFAULT_ENCRYPTION_SCHEME,
DEFAULT_PQ_ENCRYPTION_SCHEME,
DEFAULT_PQ_SIGNATURE_SCHEME,
DEFAULT_SIGNATURE_SCHEME,
HYBRID_ENCRYPTION_512,
HYBRID_ENCRYPTION_768,
HYBRID_ENCRYPTION_1024,
HYBRID_SIGNATURE_44,
HYBRID_SIGNATURE_65,
HYBRID_SIGNATURE_87,
PQ_ENCRYPTION_512,
PQ_ENCRYPTION_768,
PQ_ENCRYPTION_1024,
PQ_SIGNATURE_44,
PQ_SIGNATURE_65,
PQ_SIGNATURE_87,
PerformanceMetrics,
};
pub use types::{
AlgorithmSelection, ComplianceMode, CryptoConfig, CryptoContext, CryptoMode, CryptoPayload,
CryptoScheme, DecryptKey, EncryptKey, EncryptedData, EncryptedMetadata, EncryptedOutput,
EncryptionScheme, HashOutput, HybridComponents, KeyPair, PerformancePreference, PrivateKey,
PublicKey, SecurityLevel, SignedData, SignedMetadata, SymmetricKey, UseCase, ZeroizedBytes,
fips_available,
};
pub use zero_trust::{
Challenge, ContinuousSession, ProofOfPossessionData, SecurityMode, TrustLevel, VerifiedSession,
ZeroKnowledgeProof, ZeroTrustAuth, ZeroTrustSession,
};
pub use convenience::{decrypt, encrypt, generate_signing_keypair, sign_with_key, verify};
pub use convenience::{generate_hybrid_keypair, generate_hybrid_keypair_with_level};
pub use convenience::{
generate_hybrid_signing_keypair, generate_hybrid_signing_keypair_with_config, sign_hybrid,
sign_hybrid_with_config, verify_hybrid_signature, verify_hybrid_signature_with_config,
};
pub use convenience::{
generate_fn_dsa_keypair, generate_fn_dsa_keypair_with_config,
generate_fn_dsa_keypair_with_level, generate_keypair, generate_keypair_with_config,
generate_ml_dsa_keypair, generate_ml_dsa_keypair_with_config, generate_ml_kem_keypair,
generate_ml_kem_keypair_with_config, generate_slh_dsa_keypair,
generate_slh_dsa_keypair_with_config,
};
pub use convenience::{
derive_key, derive_key_with_config, derive_key_with_info, hash_data, hmac, hmac_check,
hmac_check_with_config, hmac_with_config,
};
pub use convenience::{
decrypt_aes_gcm,
decrypt_aes_gcm_with_aad,
decrypt_aes_gcm_with_config,
decrypt_pq_ml_kem,
decrypt_pq_ml_kem_with_config,
encrypt_aes_gcm,
encrypt_aes_gcm_with_aad,
encrypt_aes_gcm_with_config,
encrypt_pq_ml_kem,
encrypt_pq_ml_kem_with_config,
sign_ed25519,
sign_ed25519_with_config,
sign_pq_fn_dsa,
sign_pq_fn_dsa_with_config,
sign_pq_ml_dsa,
sign_pq_ml_dsa_with_config,
sign_pq_slh_dsa,
sign_pq_slh_dsa_with_config,
verify_ed25519,
verify_ed25519_with_config,
verify_pq_fn_dsa,
verify_pq_fn_dsa_with_config,
verify_pq_ml_dsa,
verify_pq_ml_dsa_with_config,
verify_pq_slh_dsa,
verify_pq_slh_dsa_with_config,
};
pub use convenience::{
decrypt_aes_gcm_unverified,
decrypt_aes_gcm_with_aad_unverified,
decrypt_aes_gcm_with_config_unverified,
decrypt_pq_ml_kem_unverified,
decrypt_pq_ml_kem_with_config_unverified,
derive_key_unverified,
derive_key_with_config_unverified,
derive_key_with_info_unverified,
encrypt_aes_gcm_unverified,
encrypt_aes_gcm_with_aad_unverified,
encrypt_aes_gcm_with_config_unverified,
encrypt_pq_ml_kem_unverified,
encrypt_pq_ml_kem_with_config_unverified,
generate_hybrid_signing_keypair_unverified,
hmac_check_unverified,
hmac_check_with_config_unverified,
hmac_unverified,
hmac_with_config_unverified,
sign_ed25519_unverified,
sign_ed25519_with_config_unverified,
sign_hybrid_unverified,
sign_pq_fn_dsa_unverified,
sign_pq_fn_dsa_with_config_unverified,
sign_pq_ml_dsa_unverified,
sign_pq_ml_dsa_with_config_unverified,
sign_pq_slh_dsa_unverified,
sign_pq_slh_dsa_with_config_unverified,
verify_ed25519_unverified,
verify_ed25519_with_config_unverified,
verify_hybrid_signature_unverified,
verify_pq_fn_dsa_unverified,
verify_pq_fn_dsa_with_config_unverified,
verify_pq_ml_dsa_unverified,
verify_pq_ml_dsa_with_config_unverified,
verify_pq_slh_dsa_unverified,
verify_pq_slh_dsa_with_config_unverified,
};
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
static SELF_TESTS_PASSED: AtomicBool = AtomicBool::new(false);
pub fn init() -> Result<()> {
let config = CoreConfig::default();
config.validate()?;
run_power_up_self_tests()?;
Ok(())
}
pub fn init_with_config(config: &CoreConfig) -> Result<()> {
config.validate()?;
run_power_up_self_tests()?;
Ok(())
}
#[must_use]
pub fn self_tests_passed() -> bool {
SELF_TESTS_PASSED.load(Ordering::SeqCst)
}
fn run_power_up_self_tests() -> Result<()> {
let hash = crate::primitives::hash::sha3::sha3_256(b"abc");
let expected_sha3 = [
0x3a, 0x98, 0x5d, 0xa7, 0x4f, 0xe2, 0x25, 0xb2, 0x04, 0x5c, 0x17, 0x2d, 0x6b, 0xd3, 0x90,
0xbd, 0x85, 0x5f, 0x08, 0x6e, 0x3e, 0x9d, 0x52, 0x5b, 0x46, 0xbf, 0xe2, 0x45, 0x11, 0x43,
0x15, 0x32,
];
if !bool::from(subtle::ConstantTimeEq::ct_eq(hash.as_slice(), expected_sha3.as_slice())) {
return Err(CoreError::SelfTestFailed {
component: "SHA-3".to_string(),
status: "KAT failed".to_string(),
});
}
use crate::primitives::aead::AeadCipher;
use crate::primitives::aead::aes_gcm::AesGcm256;
let key = AesGcm256::generate_key();
let cipher = AesGcm256::new(&*key).map_err(|e| CoreError::SelfTestFailed {
component: "AES-GCM".to_string(),
status: format!("key creation failed: {e}"),
})?;
let nonce = AesGcm256::generate_nonce();
let plaintext = b"test message for AES-GCM";
let (ct, tag) =
cipher.encrypt(&nonce, plaintext, None).map_err(|e| CoreError::SelfTestFailed {
component: "AES-GCM".to_string(),
status: format!("encryption failed: {e}"),
})?;
let decrypted =
cipher.decrypt(&nonce, &ct, &tag, None).map_err(|e| CoreError::SelfTestFailed {
component: "AES-GCM".to_string(),
status: format!("decryption failed: {e}"),
})?;
if !bool::from(subtle::ConstantTimeEq::ct_eq(decrypted.as_slice(), &plaintext[..])) {
return Err(CoreError::SelfTestFailed {
component: "AES-GCM".to_string(),
status: "decryption mismatch".to_string(),
});
}
generate_keypair()?;
SELF_TESTS_PASSED.store(true, Ordering::SeqCst);
Ok(())
}
#[cfg(test)]
#[allow(
clippy::panic,
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::arithmetic_side_effects,
clippy::panic_in_result_fn,
clippy::unnecessary_wraps,
clippy::redundant_clone,
clippy::useless_vec,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::clone_on_copy,
clippy::len_zero,
clippy::single_match,
clippy::unnested_or_patterns,
clippy::default_constructed_unit_structs,
clippy::redundant_closure_for_method_calls,
clippy::semicolon_if_nothing_returned,
clippy::unnecessary_unwrap,
clippy::redundant_pattern_matching,
clippy::missing_const_for_thread_local,
clippy::get_first,
clippy::float_cmp,
clippy::needless_borrows_for_generic_args,
unused_qualifications,
deprecated
)]
mod tests;