seal_crypto/
errors.rs

1//! Defines the top-level error type for the `seal-crypto` crate.
2//!
3//! 为 `seal-crypto` crate 定义了顶层错误类型。
4
5use crate::traits::kdf::KdfError;
6use crate::traits::asymmetric::KemError;
7use crate::traits::asymmetric::KeyAgreementError;
8use crate::traits::key::KeyError;
9use crate::traits::asymmetric::SignatureError;
10use crate::traits::symmetric::SymmetricError;
11
12#[cfg(feature = "std")]
13use thiserror::Error;
14
15/// The primary error type for the `seal-crypto` library.
16///
17/// This enum consolidates all possible failures from the underlying
18/// cryptographic traits into a single, unified error type.
19///
20/// `seal-crypto` 库的主要错误类型。
21///
22/// 此枚举将来自底层加密 trait 的所有可能失败合并为一个统一的错误类型。
23#[cfg_attr(feature = "std", derive(Error))]
24#[derive(Debug, PartialEq, Eq)]
25pub enum Error {
26    /// An error occurred during a key operation.
27    ///
28    /// 在密钥操作期间发生错误。
29    #[cfg_attr(feature = "std", error("Key operation failed"))]
30    Key(#[cfg_attr(feature = "std", from)] KeyError),
31
32    /// An error occurred during a Key Encapsulation Mechanism (KEM) operation.
33    ///
34    /// 在密钥封装机制 (KEM) 操作期间发生错误。
35    #[cfg_attr(feature = "std", error("KEM operation failed"))]
36    Kem(#[cfg_attr(feature = "std", from)] KemError),
37
38    /// An error occurred during a digital signature operation.
39    ///
40    /// 在数字签名操作期间发生错误。
41    #[cfg_attr(feature = "std", error("Signature operation failed"))]
42    Signature(#[cfg_attr(feature = "std", from)] SignatureError),
43
44    /// An error occurred during a symmetric encryption or decryption operation.
45    ///
46    /// 在对称加密或解密操作期间发生错误。
47    #[cfg_attr(feature = "std", error("Aead encryption/decryption error"))]
48    Symmetric(#[cfg_attr(feature = "std", from)] SymmetricError),
49
50    /// An error occurred during a key agreement operation.
51    ///
52    /// 在密钥协商操作期间发生错误。
53    #[cfg_attr(feature = "std", error("Key agreement operation failed"))]
54    KeyAgreement(#[cfg_attr(feature = "std", from)] KeyAgreementError),
55
56    /// KDF error.
57    ///
58    /// 密钥派生函数 (KDF) 错误。
59    #[cfg_attr(feature = "std", error("KDF error"))]
60    Kdf(#[cfg_attr(feature = "std", from)] KdfError),
61
62}
63
64// Manual From impls for no_std
65#[cfg(not(feature = "std"))]
66impl From<KeyError> for Error {
67    fn from(e: KeyError) -> Self {
68        Error::Key(e)
69    }
70}
71
72#[cfg(not(feature = "std"))]
73impl From<KemError> for Error {
74    fn from(e: KemError) -> Self {
75        Error::Kem(e)
76    }
77}
78
79#[cfg(not(feature = "std"))]
80impl From<SignatureError> for Error {
81    fn from(e: SignatureError) -> Self {
82        Error::Signature(e)
83    }
84}
85
86#[cfg(not(feature = "std"))]
87impl From<SymmetricError> for Error {
88    fn from(e: SymmetricError) -> Self {
89        Error::Symmetric(e)
90    }
91}
92
93#[cfg(not(feature = "std"))]
94impl From<KeyAgreementError> for Error {
95    fn from(e: KeyAgreementError) -> Self {
96        Error::KeyAgreement(e)
97    }
98}
99
100#[cfg(not(feature = "std"))]
101impl From<KdfError> for Error {
102    fn from(e: KdfError) -> Self {
103        Error::Kdf(e)
104    }
105}