seal_crypto_wrapper/
lib.rs

1//! # Seal Crypto Wrapper
2//!
3//! A high-level, misuse-resistant cryptographic wrapper library for Rust.
4//!
5//! 一个高级别、防误用的 Rust 加密包装库。
6//!
7//! ## Overview | 概述
8//!
9//! This library provides a safer and more user-friendly API for cryptographic operations
10//! by tightly binding algorithm information with keys themselves. This design prevents
11//! common cryptographic misuse patterns, such as using a key generated for one algorithm
12//! with a different algorithm.
13//!
14//! 本库通过将算法信息与密钥本身紧密绑定,为加密操作提供更安全、更用户友好的 API。
15//! 这种设计可以防止常见的加密误用模式,例如将为一种算法生成的密钥用于不同的算法。
16//!
17//! ## Core Design Philosophy | 核心设计理念
18//!
19//! ### Type Safety | 类型安全
20//!
21//! - **Typed Keys**: Each cryptographic primitive (aead encryption, signatures, KEM, etc.)
22//!   has dedicated key types like `TypedAeadKey` and `TypedSignatureKeyPair`.
23//! - **Algorithm Binding**: Every typed key is bound to the specific algorithm used to create it.
24//! - **Runtime Verification**: Before any cryptographic operation, the library automatically
25//!   verifies that the key's bound algorithm matches the current operation's algorithm.
26//!
27//! - **类型化密钥**:每种加密原语(对称加密、签名、KEM 等)都有专用的密钥类型,
28//!   如 `TypedAeadKey` 和 `TypedSignatureKeyPair`。
29//! - **算法绑定**:每个类型化密钥都绑定到用于创建它的特定算法。
30//! - **运行时验证**:在任何加密操作之前,库会自动验证密钥绑定的算法是否与当前操作的算法匹配。
31//!
32//! ### Convenience Features | 便利功能
33//!
34//! - **Serialization Support**: Key structures can be directly serialized/deserialized using `serde`.
35//! - **Unified Builder API**: Fluent, chainable API for selecting and constructing algorithm instances.
36//! - **Comprehensive Error Handling**: Clear error types with detailed information.
37//!
38//! - **序列化支持**:密钥结构可以直接使用 `serde` 进行序列化/反序列化。
39//! - **统一构建器 API**:用于选择和构建算法实例的流畅链式 API。
40//! - **全面的错误处理**:具有详细信息的清晰错误类型。
41//!
42//! ## Quick Start | 快速开始
43//!
44//! Add this to your `Cargo.toml`:
45//!
46//! ```toml
47//! [dependencies]
48//! seal-crypto-wrapper = { version = "0.1", features = ["full"] }
49//! ```
50//!
51//! ### Aead Encryption Example | 对称加密示例
52//!
53//! ```rust
54//! # #[cfg(feature = "aead")]
55//! # {
56//! use seal_crypto_wrapper::prelude::*;
57//! use seal_crypto_wrapper::error::Result;
58//!
59//! fn main() -> Result<()> {
60//!     // 1. Select a aead algorithm | 选择对称算法
61//!     let algorithm = AeadAlgorithm::build().aes256_gcm();
62//!
63//!     // 2. Get the algorithm wrapper | 获取算法包装器
64//!     let cipher = algorithm.into_wrapper();
65//!
66//!     // 3. Generate a typed key | 生成类型化密钥
67//!     let key = cipher.generate_typed_key()?;
68//!
69//!     // 4. Encrypt data | 加密数据
70//!     let plaintext = b"Hello, World!";
71//!     let nonce = vec![0u8; cipher.nonce_size()]; // Use random nonce in production
72//!     let aad = b"Additional Authenticated Data";
73//!     let ciphertext = cipher.encrypt(plaintext, &key, &nonce, Some(aad))?;
74//!
75//!     // 5. Decrypt data | 解密数据
76//!     let decrypted = cipher.decrypt(&ciphertext, &key, &nonce, Some(aad))?;
77//!     assert_eq!(plaintext, &decrypted[..]);
78//!
79//!     Ok(())
80//! }
81//! # }
82//! ```
83//!
84//! ### Digital Signature Example | 数字签名示例
85//!
86//! ```rust
87//! # #[cfg(feature = "asymmetric-signature")]
88//! # {
89//! use seal_crypto_wrapper::prelude::*;
90//! use seal_crypto_wrapper::error::Result;
91//!
92//! fn main() -> Result<()> {
93//!     // 1. Select a signature algorithm | 选择签名算法
94//!     let algorithm = AsymmetricAlgorithm::build().signature().ed25519();
95//!
96//!     // 2. Get the algorithm wrapper | 获取算法包装器
97//!     let signature_scheme = algorithm.into_wrapper();
98//!
99//!     // 3. Generate a key pair | 生成密钥对
100//!     let key_pair = signature_scheme.generate_keypair()?;
101//!     let (public_key, private_key) = key_pair.into_keypair();
102//!
103//!     // 4. Sign a message | 签名消息
104//!     let message = b"Important message";
105//!     let signature = signature_scheme.sign(message, &private_key)?;
106//!
107//!     // 5. Verify the signature | 验证签名
108//!     signature_scheme.verify(message, &public_key, &signature)?;
109//!
110//!     Ok(())
111//! }
112//! # }
113//! ```
114//!
115//! ## Feature Flags | 功能标志
116//!
117//! This library uses feature flags to enable specific cryptographic algorithms:
118//!
119//! 本库使用功能标志来启用特定的加密算法:
120//!
121//! - `aead` - Aead encryption algorithms (AES-GCM, ChaCha20-Poly1305)
122//! - `asymmetric-kem` - Key Encapsulation Mechanisms (RSA, Kyber)
123//! - `asymmetric-signature` - Digital signatures (Ed25519, ECDSA, Dilithium)
124//! - `asymmetric-key-agreement` - Key agreement protocols (ECDH)
125//! - `kdf` - Key Derivation Functions (HKDF, PBKDF2, Argon2)
126//! - `xof` - Extendable Output Functions (SHAKE)
127//! - `full` - Enable all features
128//!
129//! ## Security Considerations | 安全考虑
130//!
131//! - **Key Management**: Always use secure random number generation for keys and nonces.
132//! - **Algorithm Selection**: Choose algorithms appropriate for your security requirements.
133//! - **Side-Channel Attacks**: Be aware of timing attacks and other side-channel vulnerabilities.
134//! - **Post-Quantum Cryptography**: Consider using post-quantum algorithms (Kyber, Dilithium) for long-term security.
135//!
136//! - **密钥管理**:始终为密钥和随机数使用安全的随机数生成。
137//! - **算法选择**:选择适合您安全要求的算法。
138//! - **侧信道攻击**:注意时序攻击和其他侧信道漏洞。
139//! - **后量子密码学**:考虑使用后量子算法(Kyber、Dilithium)以获得长期安全性。
140//!
141//! ## Module Organization | 模块组织
142//!
143//! - [`algorithms`] - Algorithm definitions and builders | 算法定义和构建器
144//! - [`keys`] - Typed key structures | 类型化密钥结构
145//! - [`wrappers`] - Algorithm implementation wrappers | 算法实现包装器
146//! - [`traits`] - Core traits for type safety | 类型安全的核心 trait
147//! - [`error`] - Error types and handling | 错误类型和处理
148//! - [`prelude`] - Commonly used imports | 常用导入
149
150pub mod algorithms;
151pub mod error;
152pub mod keys;
153pub mod prelude;
154pub mod traits;
155pub mod wrappers;
156
157/// Re-export of the `bincode` crate for serialization convenience.
158///
159/// 重新导出 `bincode` crate 以便于序列化。
160///
161/// This allows users to access `bincode` functionality without adding it as a separate dependency.
162/// Useful for custom serialization scenarios beyond the built-in `serde` support.
163///
164/// 这允许用户访问 `bincode` 功能,而无需将其添加为单独的依赖项。
165/// 对于超出内置 `serde` 支持的自定义序列化场景很有用。
166pub use ::bincode;