fast_chacha
High-performance ChaCha20 stream cipher implementation with optional assembly acceleration (leveraging OpenSSL's assembler modules) and pure Rust fallback.
Features
- OpenSSL Assembly Modules: Integrates optimized assembly routines sourced from OpenSSL for top-tier performance.
- Pure Rust Fallback: Portable implementation when assembly is not supported on the target.
- Runtime CPU Detection: Automatically selects the fastest backend at runtime.
no_stdSupport: Works in embedded and bare-metal environments (disable defaultstd).
Installation
Add fast_chacha to your Cargo.toml:
[]
= "0.1.0"
By default, the std feature is enabled. To use in no_std environments:
[]
= { = "0.1.0", = false }
Usage
Basic example:
use FastChaCha20;
let key = ;
let nonce = ;
let mut data = b"Secret message!".to_vec;
// Encrypt or decrypt in-place
let mut cipher = new;
cipher.apply_keystream;
If you need to force the pure Rust fallback implementation:
let mut data = b"Secret message!".to_vec;
let mut cipher = new;
cipher.apply_keystream_pure;
API Documentation
Full documentation is available on docs.rs:
Build & Assembly Acceleration
The build script (build.rs) will compile and link optimized assembly implementations when available. This crate integrates assembly modules directly from OpenSSL, ensuring battle-tested, platform-specific routines. If no assembly is detected for your architecture, the crate falls back to the pure Rust version and emits a warning:
cargo:warning=fast_chacha: no ASM for <target>
Conditional compilation flag fast_chacha_asm is enabled when assembly (from OpenSSL) is used.
Benchmark
Here is a sample result of the comparison test for encrypting a 1 MiB block:
chacha20 (RustCrypto) : 117.5313ms
fast_chacha ("ASM") : 1.6701ms
fast_chacha (Fallback) : 25.0589ms
Actual results of tests you can see on Github Actions page.
- fast_chacha ("ASM") is significantly faster than both the pure Rust fallback and the reference RustCrypto implementation.
- The pure Rust fallback is also noticeably faster than RustCrypto's chacha20.
Note: Actual performance may vary depending on your CPU and platform.
License
Licensed under the Apache License, Version 2.0 © 2025 sh0rch sh0rch@iwl.dev
Contributing
Contributions, issues, and feature requests are welcome! Please open an issue or pull request on GitHub:
https://github.com/sh0rch/fast_chacha
Acknowledgements
Based on the original ChaCha20 specification (RFC 8439) and assembly modules from OpenSSL.