Skip to main content

quantum_shield/
error.rs

1//! Error types for quantum-shield.
2//!
3//! Decryption and verification failures are deliberately opaque: they carry
4//! no algorithm-specific detail, so callers cannot accidentally build a
5//! padding/format oracle out of the error messages.
6
7/// Result type alias for quantum-shield operations.
8pub type Result<T> = core::result::Result<T, Error>;
9
10/// Errors that can occur during cryptographic operations.
11#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
12#[non_exhaustive]
13pub enum Error {
14    /// The wire object declares a format version this crate does not support.
15    #[error("unsupported wire format version {0}")]
16    UnsupportedVersion(u8),
17
18    /// The wire object declares a cipher suite this crate does not support.
19    #[error("unsupported cipher suite {0}")]
20    UnsupportedSuite(u8),
21
22    /// The input is a quantum-shield 0.1.x JSON artifact. The v1 format is
23    /// cryptographically broken and unsupported by design.
24    #[error(
25        "quantum-shield v1 (0.1.x) artifacts are not supported; \
26         decrypt with a 0.1.x build and re-encrypt with v2"
27    )]
28    LegacyV1Artifact,
29
30    /// Key material failed to parse or validate.
31    #[error("invalid key material")]
32    InvalidKey,
33
34    /// An envelope failed to parse (wrong magic, truncated, or malformed).
35    #[error("invalid envelope encoding")]
36    InvalidEnvelope,
37
38    /// A signature failed to parse (wrong magic, truncated, or malformed).
39    #[error("invalid signature encoding")]
40    InvalidSignature,
41
42    /// Decryption failed. No further detail is provided by design.
43    #[error("decryption failed")]
44    DecryptionFailed,
45
46    /// Signature verification failed. No further detail is provided by design.
47    #[error("signature verification failed")]
48    VerificationFailed,
49
50    /// The plaintext exceeds [`MAX_PLAINTEXT_LEN`](crate::MAX_PLAINTEXT_LEN).
51    #[error("message too large: {len} bytes exceeds the {max}-byte limit")]
52    MessageTooLarge {
53        /// Length of the rejected message in bytes.
54        len: usize,
55        /// The enforced limit in bytes.
56        max: usize,
57    },
58
59    /// The signing context exceeds [`MAX_CONTEXT_LEN`](crate::MAX_CONTEXT_LEN).
60    #[error("context longer than 255 bytes")]
61    ContextTooLong,
62
63    /// The operating system's secure random number generator failed.
64    #[error("operating system randomness unavailable")]
65    RandomnessUnavailable,
66
67    /// A multi-recipient envelope was requested with no recipients.
68    #[error("no recipients")]
69    NoRecipients,
70
71    /// A multi-recipient envelope exceeds [`MAX_RECIPIENTS`](crate::MAX_RECIPIENTS).
72    #[error("too many recipients: {count} exceeds the {max} limit")]
73    TooManyRecipients {
74        /// The rejected recipient count.
75        count: usize,
76        /// The enforced limit.
77        max: usize,
78    },
79
80    /// A streaming chunk was submitted after the final chunk.
81    #[error("stream already finished")]
82    StreamFinished,
83
84    /// A stream ended without a final chunk (possible truncation).
85    #[error("stream truncated")]
86    StreamTruncated,
87}