Expand description
FastChaCha20: High-performance ChaCha20 stream cipher implementation with optional assembly optimizations.
This module provides a ChaCha20 implementation that can use optimized assembly routines if available, and falls back to a pure Rust implementation otherwise. It exposes a simple API for encryption and decryption using the ChaCha20 stream cipher, suitable for cryptographic applications.
§Examples
Basic usage with a 32-byte key and 12-byte nonce:
use fast_chacha::FastChaCha20;
let key = [0u8; 32];
let nonce = [0u8; 12];
let mut data = b"plaintext data to encrypt".to_vec();
let mut cipher = FastChaCha20::new(&key, &nonce);
cipher.apply_keystream(&mut data);
// `data` now contains the encrypted bytes
// To decrypt, reinitialize with the same key/nonce and apply again:
let mut cipher = FastChaCha20::new(&key, &nonce);
cipher.apply_keystream(&mut data);
// `data` is now the original plaintext
To use the pure Rust fallback implementation explicitly:
use fast_chacha::FastChaCha20;
let key = [0u8; 32];
let nonce = [0u8; 12];
let mut data = b"some data".to_vec();
let mut cipher = FastChaCha20::new(&key, &nonce);
cipher.apply_keystream_pure(&mut data, 10); // 10 double rounds
// `data` is now encrypted using the pure Rust implementation
Modules§
- fallback_
chacha20 - Fallback ChaCha20 Implementation (Pure Rust)
Structs§
- Fast
ChaCha20 - FastChaCha20: Main struct representing a ChaCha20 cipher instance.
Functions§
- ChaCha20_
ctr32_ ⚠c - C-compatible ChaCha20 function, used as a fallback or as the main implementation if assembly is not available.
- init_
cpu_ caps - Re-export all public items from the
x86
module for x86/x86_64 architectures. Initializes CPU feature detection and populatesOPENSSL_ia32cap_P
. - is_
asm_ available - Checks if the assembly-optimized implementation is available at runtime.