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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! `types` module provides base data types used at `ecdh` module

use rst_common::standard::bytes::Bytes;
use rst_common::with_cryptography::hex;

use crate::types::{ByteHex, BytesValue, Hexer, Value};

use errors::*;

pub mod errors {
    use rst_common::with_errors::thiserror::{self, Error};

    pub use crate::errors::CommonError;
    use crate::keysecure::types::errors::KeySecureError;

    /// `EcdhError` used specifically when manage public and private keys used
    /// through `ECDH` algorithm
    #[derive(Debug, Error, PartialEq)]
    pub enum EcdhError {
        #[error("ecdh: unable to parse public key: `{0}`")]
        ParsePublicKeyError(String),

        #[error("ecdh: unable to parse shared secret: `{0}`")]
        ParseSharedError(String),

        #[error("ecdh: unable to parse bytes: `{0}`")]
        ParseBytesError(String),

        #[error("ecdh: common error")]
        Common(#[from] CommonError),

        #[error("eddsa: keysecure error")]
        KeySecure(#[from] KeySecureError),
    }
}

#[derive(PartialEq, Debug, Clone)]
pub struct PublicKeyBytes([u8; 32]);

impl Value<[u8; 32]> for PublicKeyBytes {
    fn get(&self) -> Result<[u8; 32], CommonError> {
        let byte_slice = self.bytes().slice(0..32);
        let byte_output = &byte_slice[..];

        let output: Result<[u8; 32], CommonError> = <&[u8; 32]>::try_from(byte_output)
            .map(|val| val.to_owned())
            .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));

        output
    }
}

impl TryFrom<ByteHex> for PublicKeyBytes {
    type Error = EcdhError;

    fn try_from(value: ByteHex) -> Result<Self, Self::Error> {
        let result = hex::decode(value.hex())
            .map_err(|err| EcdhError::Common(CommonError::ParseHexError(err.to_string())))?;

        let peer_pub_bytes: [u8; 32] = match result.try_into() {
            Ok(value) => value,
            Err(_) => {
                return Err(EcdhError::ParsePublicKeyError(
                    "unable to parse given public key".to_string(),
                ))
            }
        };

        Ok(PublicKeyBytes(peer_pub_bytes))
    }
}

impl BytesValue for PublicKeyBytes {
    fn bytes(&self) -> Bytes {
        Bytes::from(self.0.to_vec())
    }
}

impl From<[u8; 32]> for PublicKeyBytes {
    fn from(value: [u8; 32]) -> Self {
        PublicKeyBytes(value)
    }
}

#[derive(PartialEq, Debug, Clone)]
pub struct PrivateKeyBytes([u8; 32]);

impl Value<[u8; 32]> for PrivateKeyBytes {
    fn get(&self) -> Result<[u8; 32], CommonError> {
        let byte_slice = self.bytes().slice(0..32);
        let byte_output = &byte_slice[..];

        let output: Result<[u8; 32], CommonError> = <&[u8; 32]>::try_from(byte_output)
            .map(|val| val.to_owned())
            .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));

        output
    }
}

impl BytesValue for PrivateKeyBytes {
    fn bytes(&self) -> Bytes {
        Bytes::from(self.0.to_vec())
    }
}

impl From<[u8; 32]> for PrivateKeyBytes {
    fn from(value: [u8; 32]) -> Self {
        PrivateKeyBytes(value)
    }
}