sare_core/hybrid_kem/
error.rs

1use std::fmt;
2
3use ed25519_compact::Error as X25519Error;
4use safe_pqc_kyber::KyberError;
5
6#[derive(Debug)]
7pub enum ErrSection {
8    KEM,
9    DH,
10}
11
12impl fmt::Display for ErrSection {
13    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14        match self {
15            ErrSection::KEM => write!(f, "KEM"),
16            ErrSection::DH => write!(f, "DH"),
17        }
18    }
19}
20
21#[derive(Debug)]
22pub enum HybridKEMError {
23    InvalidInput(ErrSection),
24    Decapsulation(ErrSection),
25    RandomBytesGeneration(ErrSection),
26    InvalidSeed(ErrSection),
27    InvalidSecretKey(ErrSection),
28    InvalidPublicKey(ErrSection),
29    Unexpected,
30}
31
32impl fmt::Display for HybridKEMError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            HybridKEMError::InvalidInput(section) => write!(f, "Invalid input in {}", section),
36            HybridKEMError::Decapsulation(section) => {
37                write!(f, "Decapsulation error in {}", section)
38            }
39            HybridKEMError::RandomBytesGeneration(section) => {
40                write!(f, "Random bytes generation error in {}", section)
41            }
42            HybridKEMError::InvalidSeed(section) => write!(f, "Invalid seed in {}", section),
43            HybridKEMError::InvalidSecretKey(section) => {
44                write!(f, "Invalid secret key in {}", section)
45            }
46            HybridKEMError::InvalidPublicKey(section) => {
47                write!(f, "Invalid public key in {}", section)
48            }
49            HybridKEMError::Unexpected => write!(f, "Unexpected error"),
50        }
51    }
52}
53
54impl From<KyberError> for HybridKEMError {
55    fn from(err: KyberError) -> Self {
56        match err {
57            KyberError::Decapsulation => HybridKEMError::Decapsulation(ErrSection::KEM),
58            KyberError::InvalidInput => HybridKEMError::InvalidInput(ErrSection::KEM),
59        }
60    }
61}
62
63impl From<X25519Error> for HybridKEMError {
64    fn from(err: X25519Error) -> Self {
65        match err {
66            X25519Error::InvalidSeed => HybridKEMError::InvalidSeed(ErrSection::DH),
67            X25519Error::InvalidSecretKey => HybridKEMError::InvalidSecretKey(ErrSection::DH),
68            X25519Error::InvalidPublicKey => HybridKEMError::InvalidPublicKey(ErrSection::DH),
69            _ => HybridKEMError::Unexpected,
70        }
71    }
72}