Userspace Sans-IO WireGuard protocol engine, configuration auditor, and PCAP packet decryptor in pure MoonBit.
| Package | Targets | Responsibility |
|---|---|---|
| TZH-nh/moonwire | wasm-gc, wasm, js, native | Public facade re-exporting core types and high-level helpers |
| TZH-nh/moonwire/crypto | All targets | X25519, ChaCha20-Poly1305, XChaCha20-Poly1305, BLAKE2s, HKDF, SipHash-2-4, Hex/Base64 |
| TZH-nh/moonwire/packet | All targets | WireGuard Type 1–4 codecs, 16-byte block padding, IPv4/IPv6 header trimming, ICMPv4 Echo builder |
| TZH-nh/moonwire/replay | All targets | Two-phase RFC 6479 2048-packet sliding anti-replay window |
| TZH-nh/moonwire/routing | All targets | IPv4/IPv6 CIDR radix trie (LPM, reverse-path source check, route overlap/shadow detector) |
| TZH-nh/moonwire/config | All targets | wg / wg-quick INI parser and multi-peer static security/routing auditor |
| TZH-nh/moonwire/protocol | All targets | Noise_IKpsk2 handshake, TAI64N, Cookie Reply DoS challenge, 3-slot session timers, multi-peer Device |
| TZH-nh/moonwire/pcap | All targets | Libpcap reader/writer and Wireshark wg.keylog_file / session-key trace decryptor |
| TZH-nh/moonwire/cmd/moonwire | native | CLI tool (genkey, pubkey, check-config, inspect-pcap, udp-demo, udp-client) |
Scope & Platform Boundary: MoonWire intentionally decouples the WireGuard protocol state machine from OS kernel virtual NIC drivers (wintun.dll on Windows or /dev/net/tun on Linux) and does not execute wg-quick shell hooks (PostUp/PostDown).
///|
test "end-to-end sans-io WireGuard tunnel over in-memory datagrams" {
let alice_priv = @moonwire.from_hex(
"77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a",
)
let bob_priv = @moonwire.from_hex(
"5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb",
)
let alice_pub = @moonwire.x25519_public_key(alice_priv)
let bob_pub = @moonwire.x25519_public_key(bob_priv)
let alice = @moonwire.Device::new(
alice_priv,
Bytes::from_array(FixedArray::make(32, b'\x01')),
0UL,
)
let _ = alice.add_peer(bob_pub, [@moonwire.parse_cidr("10.10.0.2/32")])
let bob = @moonwire.Device::new(
bob_priv,
Bytes::from_array(FixedArray::make(32, b'\x02')),
0UL,
)
let _ = bob.add_peer(alice_pub, [@moonwire.parse_cidr("10.10.0.1/32")])
// 1. Alice encapsulates an inner IPv4 ICMP Echo Request (10.10.0.1 -> 10.10.0.2)
let icmp_req = @moonwire.build_ipv4_icmp_echo(
0x0a0a0001U, 0x0a0a0002U, 0x1234, 1, b"hello-wireguard",
)
let (_, init_wire) = alice.route_and_encapsulate(icmp_req, 100UL)
// 2. Bob processes HandshakeInitiation and emits 92-byte HandshakeResponse
let (_, bob_hs_act, _) = bob.receive_wire(init_wire[0], 110UL)
guard bob_hs_act is HandshakeCompleted(response_wire=Some(resp_wire)) else {
fail("expected HandshakeResponse")
}
// 3. Alice processes HandshakeResponse -> automatically flushes queued encrypted ICMP packet
let (_, _, flushed_data) = alice.receive_wire(resp_wire, 120UL)
// 4. Bob authenticates, checks Cryptokey Routing source IP, and delivers plaintext IPv4 packet
let (_, bob_data_act, _) = bob.receive_wire(flushed_data[0], 130UL)
guard bob_data_act is DeliverInnerPacket(inner_ip=decrypted_ip, ..) else {
fail("expected DeliverInnerPacket")
}
inspect(
@moonwire.describe_inner_ip(decrypted_ip),
content="IPv4 10.10.0.1 -> 10.10.0.2 (ICMPv4 Echo Request seq=1, 43B)",
)
}# 1. Generate keypair & audit a wg-quick configuration (clean vs misconfigured)
moon run cmd/moonwire --target native -- genkey
moon run cmd/moonwire --target native -- check-config fixtures/sample-hub.conf
moon run cmd/moonwire --target native -- check-config fixtures/sample-audit-issues.conf
# 2. Decrypt an OpenSSL-generated WireGuard PCAP capture using a Wireshark wg.keylog_file
moon run cmd/moonwire --target native -- inspect-pcap fixtures/openssl-wg-session.pcap --keylog fixtures/openssl-wg-session.keys
# 3. Run a live Windows localhost UDP tunnel (with DoS CookieReply challenge + 3 ICMP roundtrips)
moon run cmd/moonwire --target native -- udp-demo --export-pcap _build/demo.pcap --export-keys _build/demo.keys
# 4. Cross-verify live UDP interoperability against an independent OpenSSL 3.0.x WireGuard responder
node scripts/verify_with_openssl.mjs interop _build/native/debug/build/cmd/moonwire/moonwire.exemoon check --deny-warn
moon test --target wasm-gc
moon test --target wasm
moon test --target js
moon test --target nativelet WG_CONSTRUCTION : Stringlet WG_IDENTIFIER : Stringfn blake2s_256(data : BytesView) -> Bytesfn build_ipv4_icmp_echo(src_v4 : UInt, dst_v4 : UInt, identifier : Int, sequence : Int, payload : BytesView, is_reply? : Bool) -> Bytesfn chacha20_poly1305_open(key : BytesView, nonce12 : BytesView, ciphertext_and_tag : BytesView, aad : BytesView) -> Bytes raise CryptoErrorfn chacha20_poly1305_seal(key : BytesView, nonce12 : BytesView, plaintext : BytesView, aad : BytesView) -> Bytes raise CryptoErrorfn describe_inner_ip(decrypted : BytesView) -> Stringfn format_wireshark_keylog(local_static_private : BytesView, remote_static_public : BytesView, ephemeral_privates : Array[Bytes], preshared_key? : BytesView) -> Stringfn inspect_wireguard_pcap(pcap_bytes : BytesView, keylog? : KeyLog) -> CaptureAuditSummary raise PcapErrorfn to_base64(data : BytesView) -> Stringfn xchacha20_poly1305_open(key : BytesView, nonce24 : BytesView, ciphertext_and_tag : BytesView, aad : BytesView) -> Bytes raise CryptoErrorfn xchacha20_poly1305_seal(key : BytesView, nonce24 : BytesView, plaintext : BytesView, aad : BytesView) -> Bytes raise CryptoErrorInstall
Download zipUserspace Sans-IO WireGuard protocol engine, configuration auditor, and PCAP packet decryptor in pure MoonBit.