pq_envelope/
error.rs

1use thiserror::Error;
2
3/// The error type for this crate.
4#[derive(Copy, Clone, Debug, Error)]
5pub enum Error {
6    /// Error from the OQS library
7    #[error("OQS error")]
8    Oqs,
9    /// Error from the AES Key Wrap library
10    #[error("AES Key Wrap error")]
11    AesKw,
12    /// Error from the AES GCM library
13    #[error("AES GCM error")]
14    AesGcm,
15    /// Invalid scheme name when using Scheme.parse()
16    #[error("Invalid scheme name: {0}")]
17    InvalidSchemeName(#[from] derive_more::FromStrError),
18    /// Invalid scheme value when using Scheme::try_from(u8)
19    #[error("Invalid scheme value: {0}")]
20    InvalidSchemeValue(#[from] derive_more::TryFromReprError<u8>),
21    /// Invalid scheme type when using Scheme::try_into(u8)
22    #[error("Invalid scheme type: {0}")]
23    InvalidSchemeType(#[from] derive_more::TryIntoError<u8>),
24    /// Invalid encapsulation key size
25    #[error("Invalid encapsulation key size {0}")]
26    InvalidEncapsulationKey(usize),
27    /// Invalid capsule size
28    #[error("Invalid capsule size {0}")]
29    InvalidCapsule(usize),
30    /// Error converting bytes to a capsule
31    #[error("Capsule conversion error")]
32    CapsuleConversion,
33    /// Error converting bytes to an encapsulation key
34    #[error("Encapsulation key conversion error")]
35    EncapsulationKeyConversion,
36    /// Mismatched schemes among recipients
37    #[error("Mismatched schemes among recipients")]
38    SchemeMismatch,
39    /// No recipients provided when creating an envelope
40    #[error("No recipients provided for envelope")]
41    NoRecipients,
42    /// Invalid decapsulation key
43    #[error("Decapsulation key does not correspond to any capsule")]
44    InvalidDecapsulationKey,
45}
46
47impl From<oqs::Error> for Error {
48    fn from(_: oqs::Error) -> Self {
49        Error::Oqs
50    }
51}
52
53impl From<aes_kw::Error> for Error {
54    fn from(_: aes_kw::Error) -> Self {
55        Error::AesKw
56    }
57}
58
59impl From<aes_gcm::Error> for Error {
60    fn from(_: aes_gcm::Error) -> Self {
61        Error::AesGcm
62    }
63}
64
65/// A specialized `Result` type for this crate.
66pub type Result<T> = std::result::Result<T, Error>;