cryptimitives/
errors.rs

1//! Crate custom errors.
2
3#[cfg(feature = "std")]
4use std::fmt::Display;
5
6use cryptraits_macros::Error;
7
8/// AEAD algorithm error.
9#[derive(Debug, Error, Clone)]
10pub struct AeadError;
11
12/// KDF algorithm error.
13#[derive(Debug, Error, Clone)]
14pub enum KdfError {
15    InvalidLength,
16}
17
18/// KeyPair errors.
19#[derive(Debug, Error, Clone)]
20pub enum KeyPairError {
21    BytesLengthError,
22    UnknownError(String),
23    MnemonicPhraseError(String),
24    InvalidEntropy,
25    ScalarFormatError,
26    EquationFalse,
27    SignatureError,
28}
29
30/// HMAC algorithm errors.
31#[derive(Debug, Error, Clone)]
32pub enum HmacError {
33    InvalidLength,
34    MacError,
35}
36
37/// Stream cipher algorithm errors.
38#[derive(Debug, Error, Clone)]
39pub enum StreamCipherError {
40    /// The error returned when key or nonce used in stream cipher
41    /// has an invalid length.
42    InvalidLength,
43
44    /// The error returned when cipher reached the end of stream.
45    LoopError,
46}
47
48/// Errors which may occur while processing signatures.
49#[derive(Debug, Error, PartialEq, Clone)]
50pub enum SignatureError {
51    /// A signature verification equation failed.
52    EquationFalse,
53}
54
55impl From<bip39::Error> for KeyPairError {
56    fn from(e: bip39::Error) -> Self {
57        Self::MnemonicPhraseError(e.to_string())
58    }
59}
60
61impl From<schnorrkel::SignatureError> for KeyPairError {
62    fn from(e: schnorrkel::SignatureError) -> Self {
63        match e {
64            schnorrkel::SignatureError::EquationFalse => KeyPairError::EquationFalse,
65            schnorrkel::SignatureError::ScalarFormatError => KeyPairError::ScalarFormatError,
66            schnorrkel::SignatureError::BytesLengthError {
67                name: _,
68                description: _,
69                length: _,
70            } => KeyPairError::BytesLengthError,
71            schnorrkel::SignatureError::NotMarkedSchnorrkel => todo!(),
72            _ => KeyPairError::UnknownError(e.to_string()),
73        }
74    }
75}
76
77impl From<ed25519_dalek::ed25519::Error> for KeyPairError {
78    fn from(_: ed25519_dalek::ed25519::Error) -> Self {
79        Self::SignatureError
80    }
81}
82
83impl From<ed25519_dalek::ed25519::Error> for SignatureError {
84    fn from(_: ed25519_dalek::ed25519::Error) -> Self {
85        Self::EquationFalse
86    }
87}
88
89#[cfg(feature = "std")]
90impl Display for AeadError {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        f.write_str("AeadError")
93    }
94}
95
96#[cfg(feature = "std")]
97impl Display for KdfError {
98    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99        f.write_str(&format!("KdfError::{self:?}"))
100    }
101}
102
103#[cfg(feature = "std")]
104impl Display for KeyPairError {
105    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106        f.write_str(&format!("KeyPairError::{self:?}"))
107    }
108}
109
110#[cfg(feature = "std")]
111impl Display for HmacError {
112    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113        f.write_str(&format!("HmacError::{self:?}"))
114    }
115}
116
117#[cfg(feature = "std")]
118impl Display for StreamCipherError {
119    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120        f.write_str(&format!("StreamCipherError::{self:?}"))
121    }
122}
123
124#[cfg(feature = "std")]
125impl Display for SignatureError {
126    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127        f.write_str(&format!("SignatureErrors::{self:?}"))
128    }
129}
130
131#[cfg(feature = "std")]
132impl std::error::Error for AeadError {}
133
134#[cfg(feature = "std")]
135impl std::error::Error for KdfError {}
136
137#[cfg(feature = "std")]
138impl std::error::Error for KeyPairError {}
139
140#[cfg(feature = "std")]
141impl std::error::Error for HmacError {}
142
143#[cfg(feature = "std")]
144impl std::error::Error for StreamCipherError {}
145
146#[cfg(feature = "std")]
147impl std::error::Error for SignatureError {}