Skip to main content

quantaegis_core/
error.rs

1//! Structured error types shared across the `quantaegis-*` workspace.
2
3use core::fmt;
4
5/// Result alias using [`Error`].
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// Cryptographic and validation error conditions.
9#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10#[non_exhaustive]
11pub enum Error {
12    /// Input byte slice had an unexpected length for the target key, ciphertext, or signature.
13    InvalidLength {
14        /// Expected exact byte length.
15        expected: usize,
16        /// Actual provided byte length.
17        actual: usize,
18    },
19    /// Encapsulation (public) key failed specification validation (e.g., modulus check in FIPS 203).
20    InvalidEncapsulationKey,
21    /// Decapsulation (private) key failed integrity checks (e.g., hash check in FIPS 203).
22    InvalidDecapsulationKey,
23    /// Ciphertext was malformed or failed structural checks.
24    InvalidCiphertext,
25    /// Verification (public) key failed specification validation.
26    InvalidVerifyingKey,
27    /// Signing (private) key failed specification validation or integrity checks.
28    InvalidSigningKey,
29    /// Digital signature was malformed or failed verification.
30    InvalidSignature,
31    /// Cryptographic random number generator failed.
32    RngFailure,
33    /// Requested algorithm or parameter set violates the active security policy.
34    PolicyViolation,
35    /// Domain-separation context string exceeded the maximum permitted length
36    /// (255 bytes for ML-DSA, SLH-DSA and the composite signature schemes).
37    ContextTooLong {
38        /// Maximum permitted context length in bytes.
39        max: usize,
40        /// Actual provided context length in bytes.
41        actual: usize,
42    },
43    /// A FIPS 140-3 pre-operational, conditional (CAST), or software integrity self-test failed.
44    SelfTestFailed,
45    /// A FIPS 140-3 pairwise consistency test (PCT) failed during key generation.
46    PairwiseConsistencyFailed,
47    /// An entropy source health check (e.g., SP 800-90B RCT/APT) failed.
48    EntropyHealthCheckFailed,
49    /// The cryptographic module is locked in its error state and refuses all cryptographic services.
50    ModuleInErrorState,
51    /// A DRBG instance reached its reseed interval or SP 800-90C output budget and must be reseeded.
52    DrbgReseedRequired,
53    /// A DRBG request violated SP 800-90A/90C bounds (e.g., insufficient entropy/nonce or excessive length).
54    DrbgInvalidRequest,
55    /// The requested operation is not an approved service in the active FIPS 140-3 mode.
56    NonApprovedService,
57}
58
59impl fmt::Display for Error {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            Self::InvalidLength { expected, actual } => {
63                write!(f, "invalid byte length: expected {expected}, got {actual}")
64            }
65            Self::InvalidEncapsulationKey => write!(f, "invalid encapsulation key"),
66            Self::InvalidDecapsulationKey => write!(f, "invalid decapsulation key"),
67            Self::InvalidCiphertext => write!(f, "invalid ciphertext"),
68            Self::InvalidVerifyingKey => write!(f, "invalid verifying key"),
69            Self::InvalidSigningKey => write!(f, "invalid signing key"),
70            Self::InvalidSignature => write!(f, "invalid signature"),
71            Self::RngFailure => write!(f, "cryptographic RNG failure"),
72            Self::PolicyViolation => write!(f, "algorithm violates active security policy"),
73            Self::ContextTooLong { max, actual } => {
74                write!(
75                    f,
76                    "context string too long: at most {max} bytes allowed, got {actual}"
77                )
78            }
79            Self::SelfTestFailed => write!(f, "cryptographic self-test failed"),
80            Self::PairwiseConsistencyFailed => write!(f, "pairwise consistency test failed"),
81            Self::EntropyHealthCheckFailed => write!(f, "entropy source health check failed"),
82            Self::ModuleInErrorState => write!(f, "cryptographic module is in error state"),
83            Self::DrbgReseedRequired => write!(f, "DRBG reseed required before further output"),
84            Self::DrbgInvalidRequest => write!(f, "invalid DRBG request parameters"),
85            Self::NonApprovedService => {
86                write!(f, "service is not permitted in FIPS 140-3 approved mode")
87            }
88        }
89    }
90}
91
92impl core::error::Error for Error {}