English
ours-wireguard
libs/wireguard — the WireGuard protocol without I/O: what the wireguard module runs in its data path. #![forbid(unsafe_code)].
Modules
| File | What |
|---|---|
crypto.rs | BLAKE2s (HASH, keyed MAC), HMAC-BLAKE2s and KDF1..3, ChaCha20-Poly1305 (ring), XChaCha20-Poly1305 for the cookie, TAI64N with the low 24 bits of nanoseconds zeroed, as in wireguard-go and the kernel |
noise.rs | Noise IKpsk2: initiate, receive_initiation, respond, receive_response; StaticKeys, PeerKeys (the DH of the static keys is computed once), Session |
cookie.rs | MAC1/MAC2, the responder's Checker (the secret changes every 2 minutes, cookie reply), the initiator's Jar |
replay.rs | the RFC 6479 counter window of 8128 bits, as in wireguard-go |
session.rs | Keypair: seal/open a transport message in place, the send counter (reserve — consecutive for a batch), the receive window |
allowed_ips.rs | cryptokey routing: a binary prefix tree per family, the longest prefix |
device.rs | Device: peers, the index table, handshakes, previous/current/next rotation, roaming, the section 6 timers (tick → Action; Timings — their values, ranges in AmneziaWG 3), the peer's window for AmneziaWG padding |
offload.rs | the virtio header, split of a TSO/USO super-packet into segments with checksums, Coalescer — merging TCP segments into a super-packet for writing to TUN |
ext.rs | the extension points Wire (the on-the-wire look) and Negotiation (AEAD negotiation) with the defaults of plain WireGuard |
ciphers.rs | negotiating the transport AEAD (ChaCha20-Poly1305, AES-GCM-128, AES-GCM-256) as a Negotiation, following the cipher negotiation of janeblower/amneziawg-go (its CIPHERS.md); aes_accelerated and the default derived from it |
awg.rs | AmneziaWG 3.1 as a Wire: Params (Jc/Jmin/Jmax, S1–S4, the H1–H4 ranges, I1–I5, the header protection key, content_padding, random_trailers, disable_cookies) with amneziawg-go's checks, Signature (the I1–I5 tags and which of them the kernel module will not take) |
range.rs | Range — a number or an N-M range (timers, AmneziaWG message types), a per-thread ChaCha20 random stream for picking from ranges and random bytes per packet |
keys.rs | keys in base64 |
Features
awg (awg.rs) and ciphers (ciphers.rs), both in default. Without them only Plain and Vanilla from ext.rs remain; the data path of plain WireGuard does not depend on them. The dependency in the workspace is declared with default-features = false: the wireguard module enables them.
Extension points
ext.rs; Device::new takes both implementations:
Wire— how messages look on the wire:header(the type value, H1–H4),prefix_len(S1–S4),finish(the handshake: prefix, header protection, trailer),transport_padding/varies(transport alignment),finish_transport,classify(recognize what was received, remove header protection in place, find the start of the message),before_initiation(I1–I5 and the Jc junk),cookies,plain(the fast data path). MAC1/MAC2 are computed over the message with the header written, before protection. Implementations:Plainandawg::Awg(AmneziaWG 3.1).Negotiation— choosing the transport AEAD in the handshake:stamp(the initiator writes the list into the low bits of TAI64N before sealing),choose(the responder chooses by the timestamp andtau;Choice— theringalgorithm, the bytes formixHashbeforeEmpty, the sender index bits),recognize(the initiator reads the choice from the index andtau, checking first by the choice, then vanilla),accepts_plain(whether the initiator accepts a vanilla response).choosemay refuse (Refused): there is no response. The session key is aring::aead::LessSafeKeyof the chosen algorithm (AES-GCM-128 takes the first 16 bytes), so the data path does not change: the AEAD is a property of theKeypair,ring's choice insideseal/openis the same as for ChaCha20 (AES-NI/VAES on x86, ARMv8 Crypto on aarch64), no per-packet branching is added. Implementations:Vanillaandciphers::Ciphers.
Cipher negotiation
ciphers.rs follows the AES-GCM extension of the amneziawg-go fork (janeblower/amneziawg-go, CIPHERS.md, device/cipher.go) byte for byte: cipher numbers 1/2/3 as nibbles, the list in the low 12 bits of the nanoseconds (chacha20-poly1305 alone is tag 0, as in vanilla), decoding discards a tag with bits 12–23 set, an unknown or a repeated cipher; the responder takes the first from the initiator's list that it has (an empty list — ChaCha20 if it is in its list, otherwise a refusal), the slot 1…3 in the low nibble of the index ^ tau[0] & 0xf, mixHash([slot]) before Empty; for a vanilla initiator the index is not restricted and the hash is untouched. The initiator tries Empty with the slot from the index (if it is within its list), then vanilla — the latter only if its list has ChaCha20.
AmneziaWG data path
With awg the data path stays batched and allocation-free: offload::split lays out segments with room for the S4 prefix and the header, seal_padded seals in place, finish_transports writes random prefixes (a per-thread ChaCha20 stream, the key from the OS) and protects the headers of the whole batch. Protection needs one ChaCha20 block per datagram (the nonce is its prefix); the crate's vector ChaCha20 computes four blocks to take 16 bytes of the first, so the batch's blocks are computed 16 side by side (awg::LANES, every state word is an array over the datagrams, which the compiler vectorizes on SSE2/NEON); a remainder of less than half — one at a time. Receiving likewise removes protection from a whole GRO batch (classify_batch). As long as all datagrams of a batch have the same length, sending is UDP GSO; with content_padding_addition or random_trailers the lengths differ, and the batch leaves in runs: each run of equal lengths (and a shorter last one) is one sendmsg with UDP GSO, and the datagrams are gathered with iovecs where they lie. Full TCP segments take the whole window and get no padding, so on a flow this is almost always one call per batch. The peer's window (Peer::window, 500 by default, reset when the address changes) grows with sent and received transport datagrams, like amneziawg-go's udpWindow. Receiving: classify checks the lengths and the H ranges in amneziawg-go's order (initiation, response, cookie, transport) — a comparison with a range, O(1); a keepalive with zero padding is recognized by a zero first byte.
Concurrency
Device is shared by the shards. The hot path has no shared lock: the peer's current session is an ArcSwapOption<Keypair>, the send counter is atomic, the receive window is the session's own mutex (one shard per peer), the timers are atomic timestamps. The handshake state is the peer's mutex.
Dependencies
ring, x25519-dalek, blake2, hkdf, chacha20 (XChaCha20-Poly1305 for the cookie together with ring, header protection and the AmneziaWG random stream), rand_core + getrandom, zeroize, arc-swap, base64, ours-atomic64.
Tests
cargo test -p ours-wireguard: the handshake and sessions in both directions, a repeated initiation, cookies under load, session rotation without losses, the replay window, the prefix tree, split/Coalescer with checksum verification; AmneziaWG — amneziawg-go datagrams with header protection, tags, checks, the padding window.
Fuzz targets in fuzz/: awg_classify (single and batched AmneziaWG datagrams), handshake, device (two devices over a link that drops, duplicates, reorders and damages datagrams), replay, offload.