Skip to main content

soft_fido2_crypto/
error.rs

1//! Error types for cryptographic operations
2
3#[cfg(not(feature = "std"))]
4use core::fmt;
5
6#[cfg(feature = "std")]
7use thiserror::Error;
8
9/// Cryptographic operation errors
10#[cfg(feature = "std")]
11#[derive(Debug, Error)]
12#[non_exhaustive]
13pub enum CryptoError {
14    /// Invalid public key provided
15    #[error("Invalid public key")]
16    InvalidPublicKey,
17
18    /// Invalid private key provided
19    #[error("Invalid private key")]
20    InvalidPrivateKey,
21
22    /// Invalid signature format
23    #[error("Invalid signature")]
24    InvalidSignature,
25
26    /// Decryption failed
27    #[error("Decryption failed")]
28    DecryptionFailed,
29
30    /// Encryption failed
31    #[error("Encryption failed")]
32    EncryptionFailed,
33
34    /// Invalid key length
35    #[error("Invalid key length: expected {expected}, got {actual}")]
36    InvalidKeyLength { expected: usize, actual: usize },
37
38    /// ECDH key agreement failed
39    #[error("ECDH key agreement failed")]
40    KeyAgreementFailed,
41
42    /// Invalid COSE key format
43    #[error("Invalid COSE key format")]
44    InvalidCoseKey,
45}
46
47/// Cryptographic operation errors (no_std version)
48#[cfg(not(feature = "std"))]
49#[derive(Debug)]
50#[non_exhaustive]
51pub enum CryptoError {
52    /// Invalid public key provided
53    InvalidPublicKey,
54
55    /// Invalid private key provided
56    InvalidPrivateKey,
57
58    /// Invalid signature format
59    InvalidSignature,
60
61    /// Decryption failed
62    DecryptionFailed,
63
64    /// Encryption failed
65    EncryptionFailed,
66
67    /// Invalid key length
68    InvalidKeyLength { expected: usize, actual: usize },
69
70    /// ECDH key agreement failed
71    KeyAgreementFailed,
72
73    /// Invalid COSE key format
74    InvalidCoseKey,
75}
76
77// Manual Display implementation for no_std
78#[cfg(not(feature = "std"))]
79impl fmt::Display for CryptoError {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            Self::InvalidPublicKey => write!(f, "Invalid public key"),
83            Self::InvalidPrivateKey => write!(f, "Invalid private key"),
84            Self::InvalidSignature => write!(f, "Invalid signature"),
85            Self::DecryptionFailed => write!(f, "Decryption failed"),
86            Self::EncryptionFailed => write!(f, "Encryption failed"),
87            Self::InvalidKeyLength { expected, actual } => {
88                write!(
89                    f,
90                    "Invalid key length: expected {}, got {}",
91                    expected, actual
92                )
93            }
94            Self::KeyAgreementFailed => write!(f, "ECDH key agreement failed"),
95            Self::InvalidCoseKey => write!(f, "Invalid COSE key format"),
96        }
97    }
98}
99
100/// Result type alias for cryptographic operations
101pub type Result<T> = core::result::Result<T, CryptoError>;