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
//! Key-based Key Derivation Function (KDF) algorithm wrappers for high-entropy inputs.
//!
//! 用于高熵输入的基于密钥的密钥派生函数 (KDF) 算法包装器。
//!
//! ## Overview | 概述
//!
//! This module provides concrete implementations of key-based KDF algorithms
//! that are designed to work with high-entropy input key material. These
//! algorithms efficiently expand or derive multiple keys from a single
//! master key or shared secret.
//!
//! 此模块提供基于密钥的 KDF 算法的具体实现,
//! 设计用于处理高熵输入密钥材料。这些算法有效地从单个主密钥或共享密钥
//! 扩展或派生多个密钥。
//!
//! ## Supported Algorithms | 支持的算法
//!
//! ### HKDF (HMAC-based Key Derivation Function)
//! - **HKDF-SHA256**: Fast, widely supported, 128-bit security
//! - **HKDF-SHA384**: Higher security margin, 192-bit security
//! - **HKDF-SHA512**: Maximum security, 256-bit security
//!
//! ## HKDF Process | HKDF 过程
//!
//! HKDF operates in two phases:
//! 1. **Extract**: `PRK = HKDF-Extract(salt, IKM)`
//! 2. **Expand**: `OKM = HKDF-Expand(PRK, info, L)`
//!
//! HKDF 分两个阶段运行:
//! 1. **提取**: `PRK = HKDF-Extract(salt, IKM)`
//! 2. **扩展**: `OKM = HKDF-Expand(PRK, info, L)`
//!
//! ## Use Cases | 使用场景
//!
//! - Key expansion in cryptographic protocols
//! - Deriving multiple keys from shared secrets
//! - Domain separation for different key purposes
//! - Converting high-entropy sources to usable keys
//!
//! - 密码协议中的密钥扩展
//! - 从共享密钥派生多个密钥
//! - 不同密钥用途的域分离
//! - 将高熵源转换为可用密钥
//!
//! ## Security Properties | 安全属性
//!
//! - **Pseudorandomness**: Output indistinguishable from random
//! - **Key Separation**: Different contexts produce independent keys
//! - **Forward Security**: Compromise of derived keys doesn't affect others
//! - **Efficiency**: Fast computation suitable for real-time use
//!
//! - **伪随机性**: 输出与随机数据无法区分
//! - **密钥分离**: 不同上下文产生独立密钥
//! - **前向安全**: 派生密钥的泄露不影响其他密钥
//! - **效率**: 适用于实时使用的快速计算
use crateHashAlgorithm;
use crateKdfKeyAlgorithm;
use cratedefine_wrapper;
use crate;
use crateKdfKeyAlgorithmTrait;
use KeyBasedDerivation;
use ;
use Zeroizing;
use Deref;
/// Macro for implementing key-based KDF algorithm wrappers.
///
/// 用于实现基于密钥的 KDF 算法包装器的宏。
///
/// This macro generates a complete wrapper implementation for a key-based KDF algorithm,
/// including all required trait methods, input validation, and error handling.
/// It ensures secure key derivation with proper memory management.
///
/// 此宏为基于密钥的 KDF 算法生成完整的包装器实现,
/// 包括所有必需的 trait 方法、输入验证和错误处理。
/// 它确保通过适当的内存管理进行安全密钥派生。
///
/// ## Parameters | 参数
///
/// - `$wrapper`: The name of the wrapper struct to generate
/// - `$algo`: The underlying algorithm type from seal-crypto
/// - `$algo_enum`: The corresponding algorithm enum variant
///
/// - `$wrapper`: 要生成的包装器结构体名称
/// - `$algo`: 来自 seal-crypto 的底层算法类型
/// - `$algo_enum`: 对应的算法枚举变体
///
/// ## Generated Methods | 生成的方法
///
/// - `derive()`: Derives key material from input with optional salt and context
/// - `algorithm()`: Returns the algorithm identifier
/// - `clone_box()`: Creates a boxed clone for trait objects
///
/// - `derive()`: 从输入派生密钥材料,可选盐和上下文
/// - `algorithm()`: 返回算法标识符
/// - `clone_box()`: 为 trait 对象创建 boxed 克隆
impl_kdf_key_algorithm!;
impl_kdf_key_algorithm!;
impl_kdf_key_algorithm!;
/// Universal wrapper for key-based KDF algorithms providing runtime algorithm selection.
///
/// 提供运行时算法选择的基于密钥的 KDF 算法通用包装器。
///
/// ## Purpose | 目的
///
/// This wrapper provides a unified interface for all key-based KDF algorithms,
/// allowing runtime algorithm selection while maintaining type safety. It acts
/// as a bridge between algorithm enums and concrete implementations.
///
/// 此包装器为所有基于密钥的 KDF 算法提供统一接口,
/// 允许运行时算法选择同时保持类型安全。它充当算法枚举和具体实现之间的桥梁。
///
/// ## Features | 特性
///
/// - **Runtime Polymorphism**: Switch between algorithms at runtime
/// - **Unified Interface**: Same API for all key-based KDF algorithms
/// - **Memory Safety**: Automatic zeroing of derived key material
/// - **Performance**: Efficient implementation with minimal overhead
///
/// - **运行时多态性**: 在运行时切换算法
/// - **统一接口**: 所有基于密钥的 KDF 算法的相同 API
/// - **内存安全**: 派生密钥材料的自动清零
/// - **性能**: 高效实现,开销最小
///
/// ## Examples | 示例
///
/// ```rust
/// use seal_crypto_wrapper::algorithms::kdf::key::KdfKeyAlgorithm;
/// use seal_crypto_wrapper::wrappers::kdf::key::KdfKeyWrapper;
///
/// // Create from algorithm enum
/// let algorithm = KdfKeyAlgorithm::build().hkdf_sha256();
/// let wrapper = algorithm.into_wrapper();
///
/// // Derive keys from master key
/// let master_key = b"high-entropy-master-key-material";
/// let salt = Some(b"unique-salt".as_slice());
/// let info = Some(b"application-context".as_slice());
///
/// let derived_key = wrapper.derive(master_key, salt, info, 32)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```