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
329
330
331
332
333
334
//! Key agreement protocol key types and operations.
//!
//! 密钥协商协议密钥类型和操作。
//!
//! ## Overview | 概述
//!
//! This module provides type-safe key management for key agreement protocols,
//! which allow two or more parties to establish a shared secret over an insecure
//! communication channel. The shared secret can then be used for aead encryption
//! or other cryptographic operations.
//!
//! 此模块为密钥协商协议提供类型安全的密钥管理,
//! 允许两方或多方通过不安全的通信通道建立共享密钥。
//! 然后可以将共享密钥用于对称加密或其他密码操作。
//!
//! ## Key Agreement Operations | 密钥协商操作
//!
//! ### Key Generation | 密钥生成
//! Each party generates their own public-private key pair.
//!
//! 每一方生成自己的公私钥对。
//!
//! ### Key Exchange | 密钥交换
//! Parties exchange their public keys over the communication channel.
//!
//! 各方通过通信通道交换其公钥。
//!
//! ### Shared Secret Derivation | 共享密钥派生
//! Each party uses their private key and the other party's public key to derive the same shared secret.
//!
//! 每一方使用其私钥和对方的公钥派生相同的共享密钥。
//!
//! ## Supported Algorithms | 支持的算法
//!
//! - **ECDH P-256**: Elliptic Curve Diffie-Hellman over NIST P-256 curve
//! - Provides 128-bit security level
//! - Widely supported and standardized
//! - Efficient implementation
//!
//! ## Security Considerations | 安全考虑
//!
//! - **Forward Secrecy**: Use ephemeral keys for forward secrecy
//! - **Authentication**: Key agreement alone doesn't provide authentication
//! - **Man-in-the-Middle**: Combine with authentication to prevent MITM attacks
//! - **Key Derivation**: Use proper KDF to derive actual encryption keys
//!
//! - **前向保密**: 使用临时密钥实现前向保密
//! - **认证**: 密钥协商本身不提供认证
//! - **中间人攻击**: 结合认证防止中间人攻击
//! - **密钥派生**: 使用适当的 KDF 派生实际的加密密钥
//!
//! ## Examples | 示例
//!
//! ```rust
//! use seal_crypto_wrapper::algorithms::asymmetric::AsymmetricAlgorithm;
//!
//! // Alice generates her key pair
//! let algorithm = AsymmetricAlgorithm::build().key_agreement().ecdh_p256();
//! let ka = algorithm.into_wrapper();
//! let alice_keypair = ka.generate_keypair()?;
//! let (alice_public, alice_private) = alice_keypair.into_keypair();
//!
//! // Bob generates his key pair
//! let bob_keypair = ka.generate_keypair()?;
//! let (bob_public, bob_private) = bob_keypair.into_keypair();
//!
//! // Both parties derive the same shared secret
//! let alice_shared = ka.agree(&alice_private, &bob_public)?;
//! let bob_shared = ka.agree(&bob_private, &alice_public)?;
//! assert_eq!(alice_shared, bob_shared);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
use crateKeyAgreementAlgorithm;
use cratedispatch_key_agreement;
use crateError;
use crateimpl_typed_asymmetric_private_key;
use crateimpl_typed_asymmetric_public_key;
use crate;
use ;
/// Algorithm-bound key agreement key pair for shared secret establishment.
///
/// 用于共享密钥建立的算法绑定密钥协商密钥对。
///
/// ## Purpose | 目的
///
/// This type represents a complete key agreement key pair (public and private keys) that is
/// bound to a specific key agreement algorithm. It ensures that both keys can only be used
/// with the algorithm they were generated for, preventing cryptographic misuse.
///
/// 此类型表示绑定到特定密钥协商算法的完整密钥协商密钥对(公钥和私钥)。
/// 它确保两个密钥只能与生成它们的算法一起使用,防止密码误用。
///
/// ## Key Features | 关键特性
///
/// - **Algorithm Binding**: Both keys are bound to the same key agreement algorithm
/// - **Type Safety**: Prevents using keys with incompatible algorithms
/// - **Serialization**: Supports secure serialization with algorithm metadata
/// - **Memory Safety**: Private key material is automatically zeroed on drop
///
/// - **算法绑定**: 两个密钥都绑定到相同的密钥协商算法
/// - **类型安全**: 防止将密钥与不兼容的算法一起使用
/// - **序列化**: 支持带算法元数据的安全序列化
/// - **内存安全**: 私钥材料在丢弃时自动清零
///
/// ## Usage Pattern | 使用模式
///
/// ```rust
/// use seal_crypto_wrapper::keys::asymmetric::key_agreement::TypedKeyAgreementKeyPair;
/// use seal_crypto_wrapper::algorithms::asymmetric::key_agreement::KeyAgreementAlgorithm;
///
/// // Generate a new key pair
/// let algorithm = KeyAgreementAlgorithm::build().ecdh_p256();
/// let keypair = TypedKeyAgreementKeyPair::generate(algorithm)?;
///
/// // Access individual keys
/// let public_key = keypair.public_key();
/// let private_key = keypair.private_key();
///
/// // Or consume the pair to get owned keys
/// let (public_key, private_key) = keypair.into_keypair();
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
/// Algorithm-bound key agreement public key for shared secret establishment.
///
/// 用于共享密钥建立的算法绑定密钥协商公钥。
///
/// ## Purpose | 目的
///
/// This type represents a key agreement public key that is bound to a specific algorithm.
/// It is used in key agreement protocols where parties exchange public keys to establish
/// a shared secret. Public keys can be safely distributed and shared.
///
/// 此类型表示绑定到特定算法的密钥协商公钥。
/// 它用于密钥协商协议,各方交换公钥以建立共享密钥。
/// 公钥可以安全分发和共享。
///
/// ## Security | 安全性
///
/// Public keys are safe to distribute and store without special protection,
/// but algorithm binding ensures they can only be used with compatible operations.
///
/// 公钥可以安全分发和存储而无需特殊保护,
/// 但算法绑定确保它们只能与兼容的操作一起使用。
///
/// ## Use Cases | 使用场景
///
/// - **Key Exchange**: Share with other parties for key agreement
/// - **Ephemeral Keys**: Use for forward secrecy in protocols
/// - **Static Keys**: Use for long-term key agreement relationships
/// - **Hybrid Protocols**: Combine with other cryptographic primitives
///
/// - **密钥交换**: 与其他方共享用于密钥协商
/// - **临时密钥**: 用于协议中的前向保密
/// - **静态密钥**: 用于长期密钥协商关系
/// - **混合协议**: 与其他密码原语结合
impl_typed_asymmetric_public_key!;
/// Algorithm-bound key agreement private key for shared secret derivation.
///
/// 用于共享密钥派生的算法绑定密钥协商私钥。
///
/// ## Purpose | 目的
///
/// This type represents a key agreement private key that is bound to a specific algorithm.
/// It is used to derive shared secrets from other parties' public keys in key agreement
/// protocols. Private keys must be kept secret and secure.
///
/// 此类型表示绑定到特定算法的密钥协商私钥。
/// 它用于在密钥协商协议中从其他方的公钥派生共享密钥。
/// 私钥必须保密和安全。
///
/// ## Security | 安全性
///
/// Private keys contain sensitive material and are automatically zeroed when dropped.
/// They should be protected with appropriate access controls and secure storage.
/// Compromise of a private key allows an attacker to derive shared secrets.
///
/// 私钥包含敏感材料,在丢弃时自动清零。
/// 应使用适当的访问控制和安全存储来保护它们。
/// 私钥的泄露允许攻击者派生共享密钥。
///
/// ## Use Cases | 使用场景
///
/// - **Shared Secret Derivation**: Combine with others' public keys
/// - **Forward Secrecy**: Use ephemeral keys for each session
/// - **Perfect Forward Secrecy**: Delete after use to prevent future compromise
/// - **Key Derivation**: Use derived secrets for aead encryption
///
/// - **共享密钥派生**: 与他人的公钥结合
/// - **前向保密**: 为每个会话使用临时密钥
/// - **完美前向保密**: 使用后删除以防止未来泄露
/// - **密钥派生**: 使用派生的密钥进行对称加密
impl_typed_asymmetric_private_key!;