Skip to main content

metamorphic_crypto/
error.rs

1//! Error types for the crypto library.
2
3use thiserror::Error;
4
5/// All possible errors from cryptographic operations.
6#[derive(Debug, Error)]
7pub enum CryptoError {
8    /// Base64 decoding failed.
9    #[error("base64 decode: {0}")]
10    Base64(#[from] base64::DecodeError),
11
12    /// Decryption failed (wrong key, corrupted ciphertext, or MAC mismatch).
13    #[error("decryption failed: invalid ciphertext or wrong key")]
14    Decryption,
15
16    /// A key or salt has the wrong byte length.
17    #[error("invalid length: expected {expected} bytes, got {got}")]
18    InvalidLength {
19        /// Expected byte count.
20        expected: usize,
21        /// Actual byte count.
22        got: usize,
23    },
24
25    /// Ciphertext is too short to contain the expected components.
26    #[error("ciphertext too short to be valid")]
27    TooShort,
28
29    /// The key_hash string is not in the expected `{salt}$argon2id` format.
30    #[error("invalid key_hash format (expected `salt$argon2id`)")]
31    InvalidKeyHash,
32
33    /// A recovery key contains characters outside the allowed base32 alphabet.
34    #[error("invalid recovery key character")]
35    InvalidRecoveryKey,
36
37    /// The Argon2id key derivation failed.
38    #[error("key derivation failed: {0}")]
39    Kdf(String),
40
41    /// An error in the hybrid PQ KEM layer.
42    #[error("hybrid KEM: {0}")]
43    Hybrid(String),
44
45    /// An error in the hybrid PQ signature layer.
46    #[error("signature: {0}")]
47    Signature(String),
48
49    /// An error in the verifiable random function (ECVRF) layer that is not a
50    /// plain length mismatch — e.g. a proof component that is not a valid curve
51    /// point, or hash-to-curve exhausting its counter budget. A *verification*
52    /// failure of an otherwise well-formed proof is reported as `Ok(None)` from
53    /// `ecvrf_verify`, not as this error.
54    #[error("vrf: {0}")]
55    Vrf(String),
56
57    /// Decrypted bytes are not valid UTF-8.
58    #[error("UTF-8: {0}")]
59    Utf8(#[from] std::string::FromUtf8Error),
60}