libsodium-rs
A comprehensive, idiomatic Rust wrapper for libsodium, providing a safe and ergonomic API for cryptographic operations.
Features
- Complete Coverage: Implements the entire libsodium API for Rust
- Memory Safety: Ensures secure memory handling with automatic clearing of sensitive data
- Type Safety: Leverages Rust's type system to prevent misuse of cryptographic primitives
- Extensive Testing: Comprehensive test suite covering all functionality
- Minimal Dependencies: Uses only a small set of carefully selected dependencies beyond libsodium itself
Supported Cryptographic Operations
Note: This is a non-exhaustive list of the supported algorithms and operations
- Public-key Cryptography: Encryption, signatures, and key exchange
- X25519, Ed25519, Curve25519, Ristretto255
- XSalsa20-Poly1305, XChaCha20-Poly1305
- Key exchange with X25519 and Ed25519 conversions
- Secret-key Cryptography: Authenticated encryption
- ChaCha20-Poly1305, XChaCha20-Poly1305
- AES-256-GCM
- AEGIS-128L and AEGIS-256
- Message Authentication: HMAC and Poly1305
- Hashing: SHA-256, SHA-512, BLAKE2b
- Password Hashing: Argon2, Scrypt
- Key Derivation: HKDF, BLAKE2b-based KDF
- Random Number Generation: Secure random bytes
- Secret Stream: XChaCha20-Poly1305 based streaming encryption
- One-time Authentication: Poly1305
- Stream Ciphers: ChaCha20, Salsa20, XSalsa20
- Secure Memory Management: Memory locking, secure zeroing, and protected vectors for sensitive data
Installation
Add the following to your Cargo.toml:
[]
= "0.2"
This crate requires libsodium to be installed on your system. Installation instructions for various platforms:
Linux
# Debian/Ubuntu
# Fedora
# Arch Linux
macOS
Windows
Install libsodium using vcpkg:
Usage Examples
Authenticated Encryption
use ;
use xchacha20poly1305;
Public-key Cryptography
use ;
use crypto_box;
Digital Signatures with Ed25519
use ;
use crypto_sign;
Key Exchange with X25519
use libsodium_rs as sodium;
use curve25519;
use ensure_init;
use blake2b;
Secure Memory Management
use ;
use vec_utils;
Documentation
For detailed documentation, visit docs.rs/libsodium-rs.
Testing
This library includes an extensive test suite that covers all functionality. Run the tests with:
Each cryptographic primitive has its own set of tests, including:
- Correctness tests for encryption/decryption
- Compatibility tests with NaCl
- Edge case handling
- Key and nonce generation
- Type safety and trait implementations
Security
This library is a wrapper around libsodium, which is widely regarded as a secure, audited cryptographic library. However, please note:
- Always keep private keys secure
- Use unique nonces for each encryption operation (use the provided
Nonce::generate()methods) - Never reuse nonces with the same key
- Always hash the output of scalar multiplication functions before using them as cryptographic keys
- Be aware of the cofactor issues when using Ed25519 and Curve25519 (cofactor of 8)
- For protocols requiring a prime-order group, consider using Ristretto255
- Always use cryptographically secure random values for secret keys
- Ed25519 signatures are deterministic, eliminating the need for a secure random number generator during signing
- The shared secret established through X25519 is automatically hashed before being used as an encryption key in crypto_box
- When implementing key exchange protocols manually, always hash the shared secret before using it as a key
- Verify that public keys are on the correct curve before using them
- Follow best practices for cryptographic implementations
- Report security issues responsibly
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.