noise/
error.rs

1// Set of libraries for privacy-preserving networking apps
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2023 by
6//     Dr. Maxim Orlovsky <orlovsky@cyphernet.org>
7//
8// Copyright 2023 Cyphernet DAO, Switzerland
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14//     http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21
22use cypher::{EcPkInvalid, EcSkInvalid, EcdhError};
23
24#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
25#[display(doc_comments)]
26pub enum EncryptionError {
27    /// message length {0} exceeds maximum size allowed for the encryption
28    /// protocol frame.
29    ExceedingMaxLength(usize),
30
31    /// invalid keys for ECDH: {0}
32    #[from]
33    Ecdh(EcdhError),
34
35    /// invalid remote public key
36    #[from]
37    InvalidPk(EcPkInvalid),
38
39    /// invalid local secret key
40    #[from]
41    InvalidSk(EcSkInvalid),
42
43    /// ChaCha20Poly1305 AEAD encryptor error.
44    #[from]
45    ChaCha(chacha20poly1305::aead::Error),
46}
47
48#[derive(Debug, Clone, PartialEq, Eq, Display, Error, From)]
49#[display(doc_comments)]
50pub enum HandshakeError {
51    /// unexpected version of noise protocol {version} in act {act} of handshake.
52    UnexpectedVersion { version: u8, act: u8 },
53
54    /// invalid remote ephemeral pubkey provided during noise handshake.
55    InvalidEphemeralPubkey,
56
57    /// the initiator has provided an invalid pubkey
58    InvalidInitiatorPubkey,
59
60    /// invalid length of handshake act {act}: expected {expected}, provided {found}
61    InvalidActLen {
62        act: u8,
63        expected: usize,
64        found: usize,
65    },
66
67    #[from]
68    #[from(chacha20poly1305::aead::Error)]
69    #[display(inner)]
70    Encryption(EncryptionError),
71
72    /// noise handshake is complete, nothing to process.
73    Complete,
74}
75
76#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display, Error)]
77#[display("incomplete Noise handshake")]
78pub struct IncompleteHandshake;
79
80#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
81#[display(doc_comments)]
82pub enum NoiseError {
83    /// received a non-empty payload from the remote peer when an empty payload
84    /// was expected
85    PayloadNotEmpty,
86
87    /// handshake is complete, no further advance is possible
88    HandshakeComplete,
89
90    #[display(inner)]
91    #[from]
92    Encryption(EncryptionError),
93}