prople_crypto/eddsa/
types.rs

1//! `types` is a module that provides all base types used at `eddsa` module
2use rst_common::standard::bytes::Bytes;
3use rst_common::with_cryptography::ed25519_dalek::{
4    PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, SIGNATURE_LENGTH,
5};
6use rst_common::with_cryptography::hex;
7
8pub mod errors {
9    use rst_common::with_errors::thiserror::{self, Error};
10
11    pub use crate::errors::CommonError;
12    use crate::keysecure::types::errors::KeySecureError;
13    use crate::passphrase::types::errors::PassphraseError;
14
15    /// `EddsaError` used specifically when manage public and private keys through
16    /// `Eddsa` algorithm
17    #[derive(Debug, Error, PartialEq)]
18    pub enum EddsaError {
19        #[error("eddsa: unable to parse signature: `{0}`")]
20        ParseSignatureError(String),
21
22        #[error("eddsa: unable to encode pem: `{0}`")]
23        EncodePemError(String),
24
25        #[error("eddsa: unable to decode pem: `{0}`")]
26        DecodePemError(String),
27
28        #[error("eddsa: invalid given signature: `{0}`")]
29        InvalidSignatureError(String),
30
31        #[error("eddsa: invalid given public key: `{0}`")]
32        InvalidPubKeyError(String),
33
34        #[error("eddsa: unable to parse public key: `{0}`")]
35        ParsePublicKeyError(String),
36
37        #[error("eddsa: common error")]
38        Common(#[from] CommonError),
39
40        #[error("eddsa: keysecure error")]
41        KeySecure(#[from] KeySecureError),
42
43        #[error("eddsa: passphrase error")]
44        Passphrase(#[from] PassphraseError),
45    }
46}
47
48use crate::types::{ByteHex, BytesValue, Hexer, Value};
49use errors::{CommonError, EddsaError};
50
51#[derive(PartialEq, Debug, Clone)]
52pub struct PublicKeyBytes([u8; PUBLIC_KEY_LENGTH]);
53
54impl Value<[u8; PUBLIC_KEY_LENGTH]> for PublicKeyBytes {
55    fn get(&self) -> Result<[u8; PUBLIC_KEY_LENGTH], CommonError> {
56        let byte_slice = self.bytes().slice(0..PUBLIC_KEY_LENGTH);
57        let byte_output = &byte_slice[..];
58
59        let output: Result<[u8; PUBLIC_KEY_LENGTH], CommonError> =
60            <&[u8; PUBLIC_KEY_LENGTH]>::try_from(byte_output)
61                .map(|val| val.to_owned())
62                .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));
63
64        output
65    }
66}
67
68impl TryFrom<ByteHex> for PublicKeyBytes {
69    type Error = EddsaError;
70
71    fn try_from(value: ByteHex) -> Result<Self, Self::Error> {
72        let result = hex::decode(value.hex())
73            .map_err(|err| EddsaError::Common(CommonError::ParseHexError(err.to_string())))?;
74
75        let peer_pub_bytes: [u8; PUBLIC_KEY_LENGTH] = match result.try_into() {
76            Ok(value) => value,
77            Err(_) => {
78                return Err(EddsaError::ParsePublicKeyError(
79                    "unable to parse given public key".to_string(),
80                ))
81            }
82        };
83
84        Ok(PublicKeyBytes(peer_pub_bytes))
85    }
86}
87
88impl BytesValue for PublicKeyBytes {
89    fn bytes(&self) -> Bytes {
90        Bytes::from(self.0.to_vec())
91    }
92}
93
94impl From<[u8; PUBLIC_KEY_LENGTH]> for PublicKeyBytes {
95    fn from(value: [u8; PUBLIC_KEY_LENGTH]) -> Self {
96        PublicKeyBytes(value)
97    }
98}
99
100#[derive(PartialEq, Debug, Clone)]
101pub struct PrivateKeyBytes([u8; SECRET_KEY_LENGTH]);
102
103impl Value<[u8; PUBLIC_KEY_LENGTH]> for PrivateKeyBytes {
104    fn get(&self) -> Result<[u8; SECRET_KEY_LENGTH], CommonError> {
105        let byte_slice = self.bytes().slice(0..SECRET_KEY_LENGTH);
106        let byte_output = &byte_slice[..];
107
108        let output: Result<[u8; SECRET_KEY_LENGTH], CommonError> =
109            <&[u8; SECRET_KEY_LENGTH]>::try_from(byte_output)
110                .map(|val| val.to_owned())
111                .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));
112
113        output
114    }
115}
116
117impl BytesValue for PrivateKeyBytes {
118    fn bytes(&self) -> Bytes {
119        Bytes::from(self.0.to_vec())
120    }
121}
122
123impl From<[u8; SECRET_KEY_LENGTH]> for PrivateKeyBytes {
124    fn from(value: [u8; SECRET_KEY_LENGTH]) -> Self {
125        PrivateKeyBytes(value)
126    }
127}
128
129#[derive(PartialEq, Debug, Clone)]
130pub struct SignatureBytes([u8; SIGNATURE_LENGTH]);
131
132impl Value<[u8; SIGNATURE_LENGTH]> for SignatureBytes {
133    fn get(&self) -> Result<[u8; SIGNATURE_LENGTH], CommonError> {
134        let byte_slice = self.bytes().slice(0..SIGNATURE_LENGTH);
135        let byte_output = &byte_slice[..];
136
137        let output: Result<[u8; SIGNATURE_LENGTH], CommonError> =
138            <&[u8; SIGNATURE_LENGTH]>::try_from(byte_output)
139                .map(|val| val.to_owned())
140                .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));
141
142        output
143    }
144}
145
146impl TryFrom<ByteHex> for SignatureBytes {
147    type Error = EddsaError;
148
149    fn try_from(value: ByteHex) -> Result<Self, Self::Error> {
150        let result = hex::decode(value.hex())
151            .map_err(|err| EddsaError::Common(CommonError::ParseHexError(err.to_string())))?;
152
153        let peer_sig_bytes: [u8; SIGNATURE_LENGTH] = match result.try_into() {
154            Ok(value) => value,
155            Err(_) => {
156                return Err(EddsaError::ParsePublicKeyError(
157                    "unable to parse given public key".to_string(),
158                ))
159            }
160        };
161
162        Ok(SignatureBytes(peer_sig_bytes))
163    }
164}
165
166impl BytesValue for SignatureBytes {
167    fn bytes(&self) -> Bytes {
168        Bytes::from(self.0.to_vec())
169    }
170}
171
172impl From<[u8; SIGNATURE_LENGTH]> for SignatureBytes {
173    fn from(value: [u8; SIGNATURE_LENGTH]) -> Self {
174        SignatureBytes(value)
175    }
176}