arcanum_symmetric/lib.rs
1//! # Arcanum Symmetric Encryption
2//!
3//! High-performance symmetric encryption algorithms with a unified interface.
4//!
5//! ## Security Guarantees
6//!
7//! All cipher implementations in this crate provide:
8//!
9//! - **Constant-time operations**: Key comparison, authentication tag verification,
10//! and decryption are implemented in constant time to prevent timing attacks.
11//!
12//! - **Memory zeroization**: All secret key material is automatically zeroized
13//! when dropped using the `zeroize` crate via `SecretBytes`.
14//!
15//! - **Authenticated encryption**: All AEAD ciphers provide both confidentiality
16//! and integrity. Any tampering with ciphertext or AAD is detected on decryption.
17//!
18//! - **Cryptographically secure RNG**: All key and nonce generation uses `OsRng`
19//! from the operating system, never `thread_rng()` or other non-cryptographic sources.
20//!
21//! - **Input validation**: Plaintext and AAD sizes are validated against safe limits
22//! to prevent integer overflow attacks.
23//!
24//! ## Nonce Requirements
25//!
26//! **CRITICAL**: Nonce reuse with the same key is catastrophic for most AEAD ciphers:
27//!
28//! | Cipher | Nonce Size | Nonce Reuse Impact |
29//! |--------|------------|-------------------|
30//! | AES-256-GCM | 96-bit | **Complete key compromise** - attacker can forge messages and recover plaintext |
31//! | AES-128-GCM | 96-bit | **Complete key compromise** - same as above |
32//! | AES-256-GCM-SIV | 96-bit | Reveals if same message encrypted twice (deterministic) |
33//! | ChaCha20-Poly1305 | 96-bit | **Complete key compromise** - poly1305 key revealed |
34//! | XChaCha20-Poly1305 | 192-bit | **Complete key compromise** - but collision probability negligible |
35//!
36//! ### Nonce Selection Recommendations
37//!
38//! - **Counter-based nonces**: For high-volume encryption, use a monotonic counter.
39//! Never resets, never reuses. Best for databases, file encryption, network protocols.
40//!
41//! - **Random nonces with XChaCha20**: If you must use random nonces, use XChaCha20-Poly1305.
42//! With 192-bit nonces, collision probability is negligible even after 2^64 messages.
43//!
44//! - **Nonce-misuse resistance**: Use AES-256-GCM-SIV when accidental nonce reuse is possible
45//! (at ~15% performance cost). This is the safest choice for key-value stores.
46//!
47//! ## Algorithm Selection Guide
48//!
49//! | Use Case | Recommended Cipher | Reason |
50//! |----------|-------------------|--------|
51//! | General purpose | AES-256-GCM | Hardware-accelerated (AES-NI), widely supported, fast |
52//! | Random nonces required | XChaCha20-Poly1305 | 192-bit nonce prevents collisions up to 2^64 messages |
53//! | Nonce-misuse tolerance | AES-256-GCM-SIV | Deterministic encryption safe for repeated messages |
54//! | No hardware AES | ChaCha20-Poly1305 | Fast constant-time software implementation |
55//! | High-security 128-bit | AES-128-GCM | Slightly faster, 128-bit security sufficient for most uses |
56//! | Streaming encryption | AES-256-CTR or ChaCha20Stream | For encrypting large files in chunks |
57//!
58//! ### Performance Characteristics (Typical Desktop CPU)
59//!
60//! | Cipher | Throughput (with HW) | Throughput (software) |
61//! |--------|---------------------|----------------------|
62//! | AES-256-GCM | ~4 GiB/s | ~200 MiB/s |
63//! | AES-128-GCM | ~5 GiB/s | ~250 MiB/s |
64//! | ChaCha20-Poly1305 | ~1.5 GiB/s | ~1.5 GiB/s |
65//! | XChaCha20-Poly1305 | ~1.5 GiB/s | ~1.5 GiB/s |
66//!
67//! ## Supported Algorithms
68//!
69//! ### AEAD (Authenticated Encryption with Associated Data)
70//!
71//! - **AES-256-GCM**: Industry standard, hardware-accelerated on modern CPUs
72//! - **AES-128-GCM**: Faster variant with 128-bit keys
73//! - **AES-256-GCM-SIV**: Nonce-misuse resistant variant (RFC 8452)
74//! - **ChaCha20-Poly1305**: Fast software implementation, constant-time (RFC 8439)
75//! - **XChaCha20-Poly1305**: Extended nonce variant (192-bit nonces)
76//!
77//! ### Stream Ciphers
78//!
79//! - **AES-CTR**: Counter mode for streaming encryption
80//! - **ChaCha20**: Standalone stream cipher
81//!
82//! ## Example
83//!
84//! ```ignore
85//! use arcanum_symmetric::{Aes256Gcm, Cipher};
86//!
87//! // Generate a random key and nonce
88//! let key = Aes256Gcm::generate_key();
89//! let nonce = Aes256Gcm::generate_nonce();
90//!
91//! // Encrypt with optional associated data (AAD)
92//! let plaintext = b"secret message";
93//! let aad = b"additional authenticated data";
94//! let ciphertext = Aes256Gcm::encrypt(&key, &nonce, plaintext, Some(aad))?;
95//!
96//! // Decrypt (must provide same AAD)
97//! let decrypted = Aes256Gcm::decrypt(&key, &nonce, &ciphertext, Some(aad))?;
98//! assert_eq!(decrypted, plaintext);
99//! ```
100//!
101//! ## Security Best Practices
102//!
103//! 1. **Never reuse nonces** with the same key - this is catastrophic for GCM/Poly1305
104//! 2. **Use XChaCha20-Poly1305** if random nonces are required (192-bit nonce space)
105//! 3. **Use AES-256-GCM-SIV** for nonce-misuse resistance when safety is paramount
106//! 4. **Rotate keys periodically** - after 2^32 messages for GCM, 2^64 for XChaCha20
107//! 5. **Validate decryption errors** - authentication failures indicate tampering
108//! 6. **Use AAD appropriately** - bind ciphertext to context (user ID, timestamp, etc.)
109
110#![deny(unsafe_code)]
111#![warn(missing_docs, rust_2018_idioms)]
112#![allow(unused_imports)]
113
114#[cfg(feature = "aes")]
115pub mod aes_ciphers;
116
117#[cfg(feature = "chacha20")]
118pub mod chacha_ciphers;
119
120pub mod builder;
121mod encrypted;
122mod traits;
123pub mod types;
124
125pub use encrypted::{EncryptedData, EncryptedPayload};
126pub use traits::{
127 Cipher, MAX_AAD_SIZE, MAX_PLAINTEXT_SIZE, StreamCipher, validate_aad_size,
128 validate_input_sizes, validate_plaintext_size,
129};
130
131#[cfg(feature = "aes")]
132pub use aes_ciphers::{Aes128Gcm, Aes256Ctr, Aes256Gcm, Aes256GcmSiv};
133
134#[cfg(feature = "chacha20")]
135pub use chacha_ciphers::{ChaCha20Poly1305Cipher, ChaCha20Stream, XChaCha20Poly1305Cipher};
136
137// Re-export builder traits
138pub use builder::{CipherExt, EncryptionBuilder};
139
140// Re-export type aliases
141pub use types::{
142 Aes128Key, Aes256Key, AesCtrIv, AesGcmNonce, AuthTag, ChaChaKey, ChaChaNonce, CryptoResult,
143 GcmNonce, GcmTag, Poly1305Tag, XChaChaNonce,
144};
145
146/// Prelude for convenient imports.
147pub mod prelude {
148 pub use crate::builder::{CipherExt, EncryptionBuilder};
149 pub use crate::encrypted::{EncryptedData, EncryptedPayload};
150 pub use crate::traits::{Cipher, StreamCipher};
151 pub use crate::types::*;
152
153 #[cfg(feature = "aes")]
154 pub use crate::aes_ciphers::{Aes128Gcm, Aes256Gcm, Aes256GcmSiv};
155
156 #[cfg(feature = "chacha20")]
157 pub use crate::chacha_ciphers::{ChaCha20Poly1305Cipher, XChaCha20Poly1305Cipher};
158}