prople_crypto/ecdh/
types.rs

1//! `types` module provides base data types used at `ecdh` module
2
3use rst_common::standard::bytes::Bytes;
4use rst_common::with_cryptography::hex;
5
6use crate::types::{ByteHex, BytesValue, Hexer, Value};
7
8use errors::*;
9
10pub mod errors {
11    use rst_common::with_errors::thiserror::{self, Error};
12
13    pub use crate::errors::CommonError;
14    use crate::keysecure::types::errors::KeySecureError;
15
16    /// `EcdhError` used specifically when manage public and private keys used
17    /// through `ECDH` algorithm
18    #[derive(Debug, Error, PartialEq)]
19    pub enum EcdhError {
20        #[error("ecdh: unable to parse public key: `{0}`")]
21        ParsePublicKeyError(String),
22
23        #[error("ecdh: unable to parse shared secret: `{0}`")]
24        ParseSharedError(String),
25
26        #[error("ecdh: unable to parse bytes: `{0}`")]
27        ParseBytesError(String),
28
29        #[error("ecdh: common error")]
30        Common(#[from] CommonError),
31
32        #[error("eddsa: keysecure error")]
33        KeySecure(#[from] KeySecureError),
34    }
35}
36
37#[derive(PartialEq, Debug, Clone)]
38pub struct PublicKeyBytes([u8; 32]);
39
40impl Value<[u8; 32]> for PublicKeyBytes {
41    fn get(&self) -> Result<[u8; 32], CommonError> {
42        let byte_slice = self.bytes().slice(0..32);
43        let byte_output = &byte_slice[..];
44
45        let output: Result<[u8; 32], CommonError> = <&[u8; 32]>::try_from(byte_output)
46            .map(|val| val.to_owned())
47            .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));
48
49        output
50    }
51}
52
53impl TryFrom<ByteHex> for PublicKeyBytes {
54    type Error = EcdhError;
55
56    fn try_from(value: ByteHex) -> Result<Self, Self::Error> {
57        let result = hex::decode(value.hex())
58            .map_err(|err| EcdhError::Common(CommonError::ParseHexError(err.to_string())))?;
59
60        let peer_pub_bytes: [u8; 32] = match result.try_into() {
61            Ok(value) => value,
62            Err(_) => {
63                return Err(EcdhError::ParsePublicKeyError(
64                    "unable to parse given public key".to_string(),
65                ))
66            }
67        };
68
69        Ok(PublicKeyBytes(peer_pub_bytes))
70    }
71}
72
73impl BytesValue for PublicKeyBytes {
74    fn bytes(&self) -> Bytes {
75        Bytes::from(self.0.to_vec())
76    }
77}
78
79impl From<[u8; 32]> for PublicKeyBytes {
80    fn from(value: [u8; 32]) -> Self {
81        PublicKeyBytes(value)
82    }
83}
84
85#[derive(PartialEq, Debug, Clone)]
86pub struct PrivateKeyBytes([u8; 32]);
87
88impl Value<[u8; 32]> for PrivateKeyBytes {
89    fn get(&self) -> Result<[u8; 32], CommonError> {
90        let byte_slice = self.bytes().slice(0..32);
91        let byte_output = &byte_slice[..];
92
93        let output: Result<[u8; 32], CommonError> = <&[u8; 32]>::try_from(byte_output)
94            .map(|val| val.to_owned())
95            .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));
96
97        output
98    }
99}
100
101impl BytesValue for PrivateKeyBytes {
102    fn bytes(&self) -> Bytes {
103        Bytes::from(self.0.to_vec())
104    }
105}
106
107impl From<[u8; 32]> for PrivateKeyBytes {
108    fn from(value: [u8; 32]) -> Self {
109        PrivateKeyBytes(value)
110    }
111}