# Introducing Sanstun: A Parallel-Friendly Rust WireGuard Implementation

A sans-IO WireGuard state machine designed for high performance and secure Rust development.

- **Source:** [Hacker News: Front Page](https://brifing.si/source/b07aa772-b1a0-4a76-aa18-719c6f9e2dde)
- **Published:** 2026-10-30
- **Topic:** [Software & Engineering](https://brifing.si/c/engineering)
- **Original article:** <https://www.firezone.dev/blog/introducing-sanstun>
- **On Brifing:** https://brifing.si/s/a295487a-17b2-4247-86cf-8c05a6438c83

## The sans-IO Architecture Shift

Sanstun implements the WireGuard state machine using a sans-IO design, which strictly separates IO and side-effects from business logic. By excluding network, time, and randomness operations from the core, the library becomes significantly easier to test. This architectural choice avoids function coloring and aligns perfectly with Rust's strict ownership and borrowing rules.

## Evolution from Boringtun Fork

Firezone developers originally utilized boringtun but faced challenges maintaining 100% API compatibility while evolving product requirements. After submitting over 70 pull requests, the team identified the need for a cleaner, sans-IO approach. Sanstun represents the culmination of two years of experience, replacing the deprecated fork with a more robust, idiomatic implementation.

## Eliminating Internal Buffering Issues

Internal buffering in previous implementations created complex state dependencies and API constraints. Sanstun removes this entirely, simplifying the interface by requiring users to handle packet buffering externally. This change eliminates the need for specific loop-based API calls, allowing developers to initiate handshakes or encapsulate packets at any point in their application flow.

## WireGuard Protocol and Security

WireGuard relies on Diffie-Hellman key exchanges, which are computationally expensive and vulnerable to CPU exhaustion attacks. Sanstun integrates a mandatory cookie mechanism to verify source IP ownership before processing intensive handshake operations. This rate-limiting is applied at the interface level, protecting the entire system from bogus handshake initiations and potential DoS vectors.

## Type-Safe Interface Design

Sanstun leverages Rust's type system to enforce protocol invariants at compile-time. The Interface::receive function serves as the sole entry point for raw datagrams, applying rate limits and parsing headers automatically. By keeping constructors crate-private, the library ensures that developers cannot bypass security checks, effectively preventing common implementation bugs before they reach production.

## Parallel-Friendly Crypto Operations

While WireGuard tunnels are independent, parallelizing work within a single tunnel is difficult due to sequential packet counters. Sanstun solves this by separating bookkeeping from encryption. The library provides a PendingEncapsulate type, allowing the heavy ChaCha20-Poly1305 encryption tasks to be offloaded to worker threads while maintaining thread-safe state management for the interface.

## Performance Benchmarking Results

Benchmarking shows that bookkeeping functions like begin_encapsulate and begin_decapsulate are extremely lightweight, costing roughly 20-38 nanoseconds. In contrast, the actual encryption and decryption of 1280-byte packets take over 900 nanoseconds. This disparity confirms that moving crypto tasks to separate threads offers significant throughput improvements, potentially scaling performance by up to 45 times.

## Decoupled Bookkeeping and Encryption

The library's design ensures the owner thread handles only the essential bookkeeping, such as reserving packet counters and advancing timer states. Because the encryption process is decoupled, developers have the flexibility to run crypto inline for simplicity or distribute it across multiple threads for high-performance scenarios, without needing complex locks or atomic counters.

## Simplified Tunnel Management

Every inbound packet is processed through Interface::receive, which extracts the tunnel index and validates the message type. This design maps directly to the Tunnel type, where specific functions handle handshake initiations or transport data. By centralizing the parsing logic, Sanstun ensures that all tunnels are managed consistently without exposing internal implementation details.

## Open Source Availability

Sanstun is now available on crates.io and is licensed under Apache 2.0. By deprecating the internal Firezone fork, the team aims to provide a standardized, high-performance tool for anyone building VPNs or mesh networks. The project welcomes community contributions via GitHub to further refine the API and support diverse networking use cases.

## Sources covering this story

- https://www.firezone.dev/blog/introducing-sanstun — 

---

_Summary written by Brifing from the original reporting. Copyright in the original article belongs to its publisher; read the full piece at the link above._
