1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//! Error types and handling for the seal-crypto-wrapper library.
//!
//! seal-crypto-wrapper 库的错误类型和处理。
//!
//! ## Overview | 概述
//!
//! This module defines a comprehensive error hierarchy that covers all possible
//! failure modes in cryptographic operations. The error types are designed to
//! provide clear, actionable information while maintaining security by not
//! leaking sensitive details.
//!
//! 此模块定义了一个全面的错误层次结构,涵盖密码操作中所有可能的失败模式。
//! 错误类型旨在提供清晰、可操作的信息,同时通过不泄露敏感细节来维护安全性。
//!
//! ## Error Categories | 错误分类
//!
//! - **Cryptographic Errors**: Algorithm-specific failures from the underlying crypto library
//! - **OS Errors**: System-level failures (e.g., random number generation)
//! - **Format Errors**: Data serialization, key format, and ciphertext structure issues
//!
//! - **密码学错误**: 来自底层密码库的算法特定失败
//! - **操作系统错误**: 系统级失败(例如随机数生成)
//! - **格式错误**: 数据序列化、密钥格式和密文结构问题
//!
//! ## Error Handling Best Practices | 错误处理最佳实践
//!
//! - Always handle cryptographic errors gracefully
//! - Never ignore authentication failures
//! - Log errors appropriately without exposing sensitive data
//! - Use proper error propagation with the `?` operator
//!
//! - 始终优雅地处理密码学错误
//! - 永远不要忽略认证失败
//! - 适当记录错误而不暴露敏感数据
//! - 使用 `?` 操作符进行适当的错误传播
use OsError;
use Error;
/// Main error type for the seal-crypto-wrapper library.
///
/// seal-crypto-wrapper 库的主要错误类型。
///
/// This enum encompasses all possible errors that can occur during cryptographic
/// operations. Each variant provides specific context about the failure mode.
///
/// 此枚举包含密码操作期间可能发生的所有错误。
/// 每个变体都提供有关失败模式的特定上下文。
/// Convenient Result type alias for this library.
///
/// 此库的便捷 Result 类型别名。
///
/// This type alias reduces boilerplate when working with functions that
/// return results from this library. Use this instead of `std::result::Result<T, Error>`.
///
/// 此类型别名在使用返回此库结果的函数时减少样板代码。
/// 使用此类型而不是 `std::result::Result<T, Error>`。
///
/// # Examples | 示例
///
/// ```rust
/// use seal_crypto_wrapper::error::Result;
///
/// fn encrypt_data(data: &[u8]) -> Result<Vec<u8>> {
/// // ... encryption logic
/// Ok(vec![])
/// }
/// ```
pub type Result<T> = Result;
/// Errors related to data formatting, serialization, and structure validation.
///
/// 与数据格式、序列化和结构验证相关的错误。
///
/// ## Overview | 概述
///
/// These errors occur when data doesn't conform to expected formats or when
/// serialization/deserialization operations fail. They typically indicate
/// either corrupted data or version incompatibilities.
///
/// 当数据不符合预期格式或序列化/反序列化操作失败时会发生这些错误。
/// 它们通常表示数据损坏或版本不兼容。
///
/// ## Common Causes | 常见原因
///
/// - Corrupted or truncated data
/// - Version mismatches between serialization formats
/// - Invalid key material or algorithm parameters
/// - Malformed ciphertext or signature data
///
/// - 损坏或截断的数据
/// - 序列化格式之间的版本不匹配
/// - 无效的密钥材料或算法参数
/// - 格式错误的密文或签名数据
/// Wrapper for `bincode` serialization and deserialization errors.
///
/// `bincode` 序列化和反序列化错误的包装器。
///
/// ## Purpose | 目的
///
/// This wrapper provides a consistent error handling interface for `bincode`
/// operations within the library. It abstracts the underlying `bincode` error
/// types while preserving error information and source chains.
///
/// 此包装器为库内的 `bincode` 操作提供一致的错误处理接口。
/// 它抽象了底层的 `bincode` 错误类型,同时保留错误信息和源链。
///
/// ## Usage Context | 使用上下文
///
/// These errors typically occur during:
/// - Key serialization for storage or transmission
/// - Key deserialization from stored or received data
/// - Algorithm parameter encoding/decoding
/// - Ciphertext metadata serialization
///
/// 这些错误通常发生在:
/// - 用于存储或传输的密钥序列化
/// - 从存储或接收的数据进行密钥反序列化
/// - 算法参数编码/解码
/// - 密文元数据序列化