libsodium_clone
An attempt to make libsodium in rust
Created on 1st August 2026
•
libsodium_clone
An attempt to make libsodium in rust
The problem libsodium_clone solves

libsodium_clone
libsodium_clone is a Rust-native cryptographic library that mirrors the public API families, operational rules, and packaging expectations of libsodium.
The project is designed for production use: explicit initialization, safe-by-default primitives, strict linting, cross-platform CI, and release automation are first-class concerns.
Original libsodium repo: link
Product Overview
Problem Statement
Many applications need a low-friction cryptographic API that is hard to misuse, portable across major platforms, and stable enough to support long-lived production deployments. libsodium solved that problem in C. libsodium_clone brings the same design goals into Rust while keeping the module boundaries familiar to existing libsodium users.
What This Project Provides
- A Rust-first public API modeled after libsodium's feature families.
- A stable initialization boundary and version reporting.
- A clean separation between helpers, memory utilities, randomness, symmetric crypto, public-key crypto, hashing, password hashing, key derivation, and advanced primitives.
- Cross-platform build and release automation.
- Documentation and scripts that treat the crate like a real production library.
Architecture
flowchart TB
App[Applications] --> Facade[libsodium_clone public API]
Facade --> Init[init + version]
Facade --> Helpers[helpers + padding + memory]
Facade --> RNG[randombytes]
Facade --> Symm[secret-key crypto]
Facade --> Pub[public-key crypto]
Facade --> Hash[hashing + KDF + password hashing]
Facade --> Adv[advanced primitives]
RNG --> OSRNG[OS entropy sources]
Symm --> Backend[crypto backend]
Pub --> Backend
Hash --> Backend
Adv --> Backend
subgraph Targets
Windows[Windows x86/x64/arm64]
Apple[iOS / macOS]
Android[Android]
Wasm[WebAssembly]
Unix[Linux / BSD / Unix]
end
Facade -. portable builds .-> Targets
Inter-Contract Communication Flow
sequenceDiagram
participant App as Application
participant Init as init
participant RNG as randombytes
participant Symm as secret-key crypto
participant Pub as public-key crypto
participant Hash as hashing/password
App->>Init: sodium_init()
Init-->>App: initialized
App->>RNG: secure random bytes
RNG-->>App: nonce / key material
App->>Symm: encrypt/decrypt
Symm-->>App: ciphertext / plaintext
App->>Pub: sign/verify or box/seal
Pub-->>App: signature / shared secret
App->>Hash: hash / KDF / password ops
Hash-->>App: digest / derived key
Features
- Secret-key authenticated encryption.
- Public-key authenticated encryption and signatures.
- Password hashing and key derivation.
- Secure memory and zeroization.
- Random byte generation with test hooks.
- Cross-platform build and release workflows.
- Clear module boundaries that follow the libsodium documentation structure.
Tech Stack
- Rust 2021 edition.
thiserrorfor typed errors.zeroizefor sensitive data handling.getrandomfor operating-system entropy.subtlefor constant-time comparison helpers.- GitHub Actions for CI and release automation.
Local Development
Prerequisites
- Rust toolchain from
rust-toolchain.toml. - PowerShell 7 or newer on Windows.
- A POSIX shell on Unix-like systems if you want to run equivalent cargo commands manually.
Environment Variables
Copy .env.example and set values for your environment.
RUST_LOG: log verbosity for local tooling.LIBSODIUM_CLONE_DETERMINISTIC_TESTS: enables deterministic test behavior where supported.LIBSODIUM_CLONE_TEST_SEED: optional seed for deterministic tests.CARGO_REGISTRY_TOKEN: token used by the release workflow.GITHUB_TOKEN: optional token for repository automation.
Build and Test
cargo fmt --all --check
cargo clippy --all-targets --all-features -- -D warnings
cargo test --all-features
Or run the bundled script:
powershell -ExecutionPolicy Bypass -File scripts/deploy-local.ps1
CI/CD and Deployment
Continuous Integration
The CI workflow runs on Windows, Linux, and macOS and verifies formatting, linting, and tests before merge.
Release Deployment
- Validate the crate locally.
- Tag the release with a semantic version.
- Push the tag to trigger the release workflow.
- Publish the crate to crates.io using the release token.
Local and Prerelease Deployment Scripts
scripts/deploy-local.ps1: local validation and release packaging.scripts/deploy-testnet.ps1: prerelease publish flow for a non-production registry.scripts/build-release.ps1: optimized release build.scripts/upgrade-strategy.ps1: dependency refresh and validation.
Challenges we ran into
Challenges Faced
During the development of this project, integrating libsodium with Rust presented several technical challenges. Since libsodium is a native C library, connecting it with Rust required careful handling of dependencies, build configurations, and memory safety. Below are the key challenges encountered:
- Native Library Integration: Configuring Rust to correctly detect and link the native
libsodiumlibrary across different operating systems. - Cross-Platform Compatibility: Ensuring consistent builds and behavior on Windows, Linux, and macOS despite differences in compilers and package managers.
- FFI (Foreign Function Interface): Safely interfacing with C functions using Rust's
unsafeblocks while maintaining Rust's safety guarantees. - Build Configuration: Managing
build.rs, linker settings, and environment variables to support both local development and deployment. - Cryptographic Memory Management: Handling sensitive data such as encryption keys securely and minimizing the risk of exposing them in memory.
- Error Handling: Converting low-level C error codes into idiomatic Rust
Resulttypes for better reliability and debugging. - Dependency & Version Compatibility: Resolving compatibility issues between different versions of Rust crates and the underlying
libsodiumlibrary. - Documentation & Learning Curve: Adapting C-based cryptographic documentation and examples to Rust's ownership, borrowing, and lifetime model.
- Deployment Challenges: Packaging the application with the required native libraries while ensuring seamless execution on end-user systems.
Despite these challenges, integrating libsodium with Rust provided a secure and high-performance cryptographic foundation, enabling reliable encryption, digital signatures, and secure key management for the application.
Technologies used