snow/
error.rs

1//! All error types used by Snow operations.
2
3use std::fmt;
4
5/// All errors in snow will include an `ErrorKind`.
6#[allow(missing_docs)]
7#[derive(Debug, PartialEq)]
8#[non_exhaustive]
9pub enum Error {
10    /// The noise pattern failed to parse.
11    Pattern(PatternProblem),
12
13    /// Initialization failure, at a provided stage.
14    Init(InitStage),
15
16    /// Missing prerequisite.
17    Prereq(Prerequisite),
18
19    /// A state error.
20    State(StateProblem),
21
22    /// Invalid input.
23    Input,
24
25    /// Diffie-hellman failed.
26    Dh,
27
28    /// Decryption failed.
29    Decrypt,
30
31    /// Key-encapsulation failed
32    #[cfg(feature = "hfs")]
33    Kem,
34}
35
36/// The various stages of initialization used to help identify
37/// the specific cause of an `Init` error.
38#[allow(missing_docs)]
39#[derive(Debug, PartialEq)]
40pub enum PatternProblem {
41    TooFewParameters,
42    UnsupportedHandshakeType,
43    UnsupportedBaseType,
44    UnsupportedHashType,
45    UnsupportedDhType,
46    UnsupportedCipherType,
47    InvalidPsk,
48    UnsupportedModifier,
49    #[cfg(feature = "hfs")]
50    UnsupportedKemType,
51}
52
53impl From<PatternProblem> for Error {
54    fn from(reason: PatternProblem) -> Self {
55        Error::Pattern(reason)
56    }
57}
58
59/// The various stages of initialization used to help identify
60/// the specific cause of an `Init` error.
61#[allow(missing_docs)]
62#[derive(Debug, PartialEq)]
63pub enum InitStage {
64    ValidateKeyLengths,
65    ValidatePskLengths,
66    ValidateCipherTypes,
67    GetRngImpl,
68    GetDhImpl,
69    GetCipherImpl,
70    GetHashImpl,
71    #[cfg(feature = "hfs")]
72    GetKemImpl,
73    ValidatePskPosition,
74}
75
76impl From<InitStage> for Error {
77    fn from(reason: InitStage) -> Self {
78        Error::Init(reason)
79    }
80}
81
82/// A prerequisite that may be missing.
83#[allow(missing_docs)]
84#[derive(Debug, PartialEq)]
85pub enum Prerequisite {
86    LocalPrivateKey,
87    RemotePublicKey,
88}
89
90impl From<Prerequisite> for Error {
91    fn from(reason: Prerequisite) -> Self {
92        Error::Prereq(reason)
93    }
94}
95
96/// Specific errors in the state machine.
97#[allow(missing_docs)]
98#[derive(Debug, PartialEq)]
99pub enum StateProblem {
100    MissingKeyMaterial,
101    MissingPsk,
102    NotTurnToWrite,
103    NotTurnToRead,
104    HandshakeNotFinished,
105    HandshakeAlreadyFinished,
106    OneWay,
107    StatelessTransportMode,
108    /// The nonce counter attempted to go higher than (2^64) - 1
109    Exhausted,
110}
111
112impl From<StateProblem> for Error {
113    fn from(reason: StateProblem) -> Self {
114        Error::State(reason)
115    }
116}
117
118impl fmt::Display for Error {
119    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120        match self {
121            Error::Pattern(reason) => write!(f, "pattern error: {:?}", reason),
122            Error::Init(reason) => {
123                write!(f, "initialization error: {:?}", reason)
124            },
125            Error::Prereq(reason) => {
126                write!(f, "prerequisite error: {:?}", reason)
127            },
128            Error::State(reason) => write!(f, "state error: {:?}", reason),
129            Error::Input => write!(f, "input error"),
130            Error::Dh => write!(f, "diffie-hellman error"),
131            Error::Decrypt => write!(f, "decrypt error"),
132            #[cfg(feature = "hfs")]
133            Error::Kem => write!(f, "kem error"),
134        }
135    }
136}
137
138impl std::error::Error for Error {}