corevpn_crypto/
error.rs

1//! Cryptographic error types
2
3use thiserror::Error;
4
5/// Result type for cryptographic operations
6pub type Result<T> = std::result::Result<T, CryptoError>;
7
8/// Cryptographic errors
9///
10/// Uses `&'static str` for error messages on hot paths to avoid allocations.
11#[derive(Debug, Error)]
12pub enum CryptoError {
13    /// Invalid key length
14    #[error("invalid key length: expected {expected}, got {got}")]
15    InvalidKeyLength {
16        /// Expected key length
17        expected: usize,
18        /// Actual key length
19        got: usize,
20    },
21
22    /// Invalid signature
23    #[error("invalid signature")]
24    InvalidSignature,
25
26    /// Decryption failed (authentication tag mismatch)
27    #[error("decryption failed: authentication tag mismatch")]
28    DecryptionFailed,
29
30    /// Encryption failed
31    #[error("encryption failed: {0}")]
32    EncryptionFailed(&'static str),
33
34    /// Invalid nonce length
35    #[error("invalid nonce length")]
36    InvalidNonceLength,
37
38    /// Key derivation failed
39    #[error("key derivation failed: {0}")]
40    KeyDerivationFailed(&'static str),
41
42    /// Certificate error
43    #[error("certificate error: {0}")]
44    CertificateError(String),
45
46    /// Invalid PEM format
47    #[error("invalid PEM format: {0}")]
48    InvalidPem(String),
49
50    /// HMAC verification failed
51    #[error("HMAC verification failed")]
52    HmacVerificationFailed,
53
54    /// Replay attack detected
55    #[error("replay attack detected: packet ID already seen")]
56    ReplayDetected,
57
58    /// Key expired
59    #[error("key has expired")]
60    KeyExpired,
61
62    /// Random number generation failed
63    #[error("random number generation failed")]
64    RngFailed,
65}