libsodium_clone

An attempt to make libsodium in rust

Repo
libsodium_clone banner

About

## The problem it solves

![image](https://assets.devfolio.co/content/12e05b74085449b18c41c381360bfc0d/ee7689cb-f2d6-4d7b-b109-9acc2ec1ea83.png)

# 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](https://github.com/jedisct1/libsodium)

## 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

```mermaid

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

```mermaid

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.
  • `thiserror` for typed errors.
  • `zeroize` for sensitive data handling.
  • `getrandom` for operating-system entropy.
  • `subtle` for 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

```powershell

cargo fmt --all --check cargo clippy --all-targets --all-features -- -D warnings cargo test --all-features ```

Or run the bundled script:

```powershell

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 `libsodium` library 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 `unsafe` blocks 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 `Result` types for better reliability and debugging.
  • **Dependency & Version Compatibility:** Resolving compatibility issues between different versions of Rust crates and the underlying `libsodium` library.
  • **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.

Team

From the public Devfolio project page. Votes, swipes and the judge score are hackfeed's own.

AI judge

Not judged yet. A free model reads this page, compares it with similar projects and this event's winners, then scores it and says what would make it win.

Still being built?

Not checked yet.

Swipe stats

0upvotes
0skips
0saves

Discussion 0

No comments yet. Ask the team something, or say what you'd use it for.

Posting as a guest. Plain text, 1,000 characters max.

Already built nearby

Closest projects by meaning and shared words. Similar isn't the same as copied: read them.

UniteSwap

Rust CLI enabling trustless atomic swaps between EVM & non-EVM chains via 1inch Fusion+ extension

sdk-rust

This is a fork of the rust sdk of the blockless protocol. With the implementation of new native features that are missing in the sdk which streamline the development process for the rust devs on the blockless network.