disco_rs/
error.rs

1/*
2    Copyright David Huseby, All Rights Reserved.
3    SPDX-License-Identifier: Apache-2.0
4*/
5use thiserror::Error;
6
7/// Disco errors
8#[derive(Error, PartialEq, Copy, Clone, Debug)]
9pub enum Error {
10    /// A handshake pattern error occurred
11    #[error("parameter error")]
12    Param(#[from] ParamError),
13    /// A builder error occurred
14    #[error("builder error")]
15    Builder(#[from] BuilderError),
16    /// A protocol error occurred
17    #[error("protocol error")]
18    Protocol(#[from] ProtocolError),
19    /// A tag related error occurred
20    #[error("tag error")]
21    Tag(#[from] TagError),
22}
23
24/// Errors that can happen from invalid protocol strings
25#[derive(Error, PartialEq, Copy, Clone, Debug)]
26pub enum ParamError {
27    /// Too few parameters in the protocol string
28    #[error("not enough parameters given")]
29    TooFewParameters,
30    /// Invalid protocol name, must be "Noise"
31    #[error("invalid protocol identifier")]
32    InvalidProtocol,
33    /// Invalid, or unsuported, handshake pattern
34    #[error("invalid or unsupported handshake pattern")]
35    InvalidHandshake,
36    /// Invalid, or unsupported, key type
37    #[error("invalid key agreement protocol")]
38    InvalidKeyType,
39    /// Invalid STROBE protocol version
40    #[error("invalid strobe protocol version")]
41    InvalidStrobeVersion,
42    /// Invalid ephemeral key setup
43    #[error("invalid ephemeral key setup")]
44    InvalidEphemeralKeys,
45    /// Invalid duplex mode for this channel
46    #[error("invalid channel duplex mode")]
47    InvalidChannelDuplex,
48    /// Invalid transport order for this channel
49    #[error("invalid channel transport order")]
50    InvalidTransportOrder,
51}
52
53/// Errors that can happen during building
54#[derive(Error, PartialEq, Copy, Clone, Debug)]
55pub enum BuilderError {
56    /// Missing local secret key
57    #[error("missing local secret key needed for this handshake pattern")]
58    MissingLocalSecretKey,
59    /// Missing remote public key
60    #[error("missing remote public key needed for this handshake pattern")]
61    MissingRemotePublicKey,
62    /// Missing pre-shared key
63    #[error("missing pre-shared key needed for this handshake pattern")]
64    MissingPreSharedKey,
65    /// Invalid tag
66    #[error("invalid tag")]
67    InvalidTag,
68    /// Missing buffer
69    #[error("missing bytes")]
70    MissingBytes,
71}
72
73/// Errors that can happen during handshaking and transport
74#[derive(Error, PartialEq, Copy, Clone, Debug)]
75pub enum ProtocolError {
76    /// Incorrect state for this operation
77    #[error("incorrect state for this operation")]
78    InvalidState,
79    /// Invalid or missing key
80    #[error("invalid or missing key")]
81    InvalidKey,
82    /// Invalid tag
83    #[error("invalid tag error")]
84    InvalidTag,
85    /// Failed MAC of incoming message
86    #[error("invalid mac error")]
87    InvalidMac,
88    /// Invalid data
89    #[error("invalid data")]
90    InvalidData,
91    /// Buffer isn't empty
92    #[error("out buffer isn't empty")]
93    NonEmptyBuffer,
94    /// Invalid TaggedData length
95    #[error("invalid length for tagged data")]
96    InvalidBufferLen,
97
98    /// Sending a pre-shared key is a protocol error
99    #[error("sending pre-shared key error")]
100    SendingPsk,
101    /// Receiving a pre-shared key is a protocol error
102    #[error("receiving pre-shared key error")]
103    ReceivingPsk,
104    /// Sending the prologue is a protocol error
105    #[error("sending prologue error")]
106    SendingPrologue,
107    /// Receiving the prologue is a protocol error
108    #[error("receiving prologue error")]
109    ReceivingPrologue,
110    /// Sending a secret key is a protocol error
111    #[error("sending secret key error")]
112    SendingSecretKey,
113    /// Receiving a secret key is a protocol error
114    #[error("receiving secret key error")]
115    ReceivingSecretKey,
116
117    /// Invalid handshake operaion
118    #[error("invalid handshake operaion")]
119    InvalidHandshakeOp,
120    /// Invalid transport operation
121    #[error("invalid parameter for channel operation")]
122    InvalidTransportOp,
123    /// Shared secret calculation error
124    #[error("error calculating shared secret")]
125    SharedSecretCalculationFailed,
126    /// Message limit reached
127    #[error("message limit reached, repeat handshake")]
128    MessageLimitReached,
129    /// Invalid nonce encountered
130    #[error("invalid nonce")]
131    InvalidNonce,
132    /// The channel states aren't the same
133    #[error("channel state mismatch")]
134    ChannelStateMismatch,
135    /// Invalid send command
136    #[error("invalid send command")]
137    InvalidSend,
138    /// Invalid recv commmand
139    #[error("invalid recv command")]
140    InvalidRecv,
141}
142
143/// Errors related to tag parsing
144#[derive(Error, PartialEq, Copy, Clone, Debug)]
145pub enum TagError {
146    /// Parse error
147    #[error("tag parse error")]
148    ParseError,
149}