summaryrefslogtreecommitdiffstats
path: root/src/handshake/types.rs
blob: 0d9a5d311f50dd4bd23a47f640353dfc9eedc862 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::error::Error;
use std::fmt;

use crate::types::KeyPair;

/* Internal types for the noise IKpsk2 implementation */

// config error

#[derive(Debug)]
pub struct ConfigError(String);

impl ConfigError {
    pub fn new(s: &str) -> Self {
        ConfigError(s.to_string())
    }
}

impl fmt::Display for ConfigError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ConfigError({})", self.0)
    }
}

impl Error for ConfigError {
    fn description(&self) -> &str {
        &self.0
    }

    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }
}

// handshake error

#[derive(Debug)]
pub enum HandshakeError {
    DecryptionFailure,
    UnknownPublicKey,
    UnknownReceiverId,
    InvalidMessageFormat,
    OldTimestamp,
    InvalidState,
}

impl fmt::Display for HandshakeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            HandshakeError::DecryptionFailure => write!(f, "Failed to AEAD:OPEN"),
            HandshakeError::UnknownPublicKey => write!(f, "Unknown public key"),
            HandshakeError::UnknownReceiverId => {
                write!(f, "Receiver id not allocated to any handshake")
            }
            HandshakeError::InvalidMessageFormat => write!(f, "Invalid handshake message format"),
            HandshakeError::OldTimestamp => write!(f, "Timestamp is less/equal to the newest"),
            HandshakeError::InvalidState => write!(f, "Message does not apply to handshake state"),
        }
    }
}

impl Error for HandshakeError {
    fn description(&self) -> &str {
        "Generic Handshake Error"
    }

    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }
}

pub type Output<T> = (
    T, // external identifier associated with peer
    // (e.g. a reference or vector index)
    Option<Vec<u8>>, // message to send
    Option<KeyPair>, // resulting key-pair of successful handshake
);

// preshared key

pub type Psk = [u8; 32];