TZH-nh/moonwire/protocol does not have a README file

    ProtocolError

    pub(all) suberror ProtocolError {
    InvalidKeyLength(field~ : String, actual~ : Int)
    InvalidMac1
    InvalidMac2
    CookieRequired
    HandshakeDecryptionFailed(stage~ : String)
    UnknownPeer(Bytes)
    ReplayedOrStaleTimestamp
    UnexpectedHandshakeResponse(UInt)
    MalformedTimestamp
    Crypto(
    CryptoError
    )
    Packet(
    PacketError
    )
    NoActiveSession
    UnknownReceiverIndex(UInt)
    ReplayedOrTooOldPacket(UInt64)
    CounterExhausted
    InvalidSourceIp(
    IpAddr
    )
    UnroutableDestination(
    IpAddr
    )
    } derive(Eq,
    Debug
    )

    ConsumedInitiation

    pub(all) struct ConsumedInitiation {
    initiator_static_public : Bytes
    initiator_ephemeral_public : Bytes
    initiator_index : UInt
    timestamp : Tai64n
    chaining_key : Bytes
    hash : Bytes
    } derive(Eq,
    Debug
    )

    Decoded result of consuming a valid HandshakeInitiation on the responder prior to generating a response.

    CookieValidator

    pub struct CookieValidator {
    local_cookie_key : Bytes
    // private fields
    } derive(Eq,
    Debug
    )

    Stateful responder-side cookie generator and mac2 validator (§5.4.7).
    fn CookieValidator::compute_cookie(self : CookieValidator, peer_endpoint_bytes : BytesView, now_ms : UInt64) -> Bytes

    Compute the 16-byte cookie tau = Mac(R_m, peer_endpoint_bytes) for peer_endpoint_bytes at now_ms.
    fn CookieValidator::create_cookie_reply(self : CookieValidator, sender_index : UInt, triggering_mac1 : BytesView, peer_endpoint_bytes : BytesView, now_ms : UInt64, explicit_nonce24? : BytesView) ->
    CookieReply
    raise ProtocolError

    Build a 64-byte CookieReply (Type 3) challenging the sender of a handshake packet whose mac1 was valid but whose mac2 was missing or stale under load (§5.4.7).

    CookieValidator::new

    fn CookieValidator::new(local_static_public : BytesView, secret_seed : BytesView, now_ms : UInt64) -> CookieValidator raise ProtocolError

    Create a CookieValidator bound to local_static_public and an initial 32-byte random secret_seed.

    CookieValidator::verify_initiation_mac2

    fn CookieValidator::verify_initiation_mac2(self : CookieValidator, msg :
    HandshakeInitiation
    , peer_endpoint_bytes : BytesView, now_ms : UInt64) -> Bool

    Verify mac2 on an incoming HandshakeInitiation from peer_endpoint_bytes.

    CookieValidator::verify_response_mac2

    fn CookieValidator::verify_response_mac2(self : CookieValidator, msg :
    HandshakeResponse
    , peer_endpoint_bytes : BytesView, now_ms : UInt64) -> Bool

    Verify mac2 on an incoming HandshakeResponse from peer_endpoint_bytes.

    DerivedSessionKeys

    pub(all) struct DerivedSessionKeys {
    local_index : UInt
    remote_index : UInt
    send_key : Bytes
    recv_key : Bytes
    is_initiator : Bool
    } derive(Eq,
    Debug
    )

    Derived directional transport session keys after a completed Noise_IKpsk2 handshake (§5.4.5).

    Device

    pub struct Device {
    local_static_private : Bytes
    local_static_public : Bytes
    cookies : CookieValidator
    routing :
    RoutingTable
    [Int]
    peers : Array[PeerTunnel]
    // private fields
    }

    Multi-peer Sans-IO WireGuard interface engine uniting per-peer Noise tunnels, CookieValidator DoS mitigation, and §4 Cryptokey Routing (outbound LPM + inbound source anti-spoofing).

    Device::add_peer

    fn Device::add_peer(self : Device, remote_static_public : BytesView, allowed_ips : Array[
    Cidr
    ], preshared_key? : BytesView, persistent_keepalive_ms? : UInt64) -> Int raise ProtocolError

    Register a peer and its AllowedIPs prefixes on the device, returning the assigned peer_id (0..N-1).

    Device::new

    fn Device::new(local_static_private : BytesView, entropy_seed : BytesView, now_ms : UInt64) -> Device raise ProtocolError

    Create a multi-peer Sans-IO Device from a 32-byte local_static_private key and 32-byte entropy_seed.

    Device::receive_wire

    fn Device::receive_wire(self : Device, wire_bytes : BytesView, now_ms : UInt64, under_load? : Bool, peer_endpoint_bytes? : BytesView) -> (Int, TunnelAction, Array[Bytes]) raise ProtocolError

    Process an inbound WireGuard UDP datagram across all peers on this Device, enforcing CookieReply under load and §4 Cryptokey Routing reverse-path source IP verification on decrypted inner packets. Returns (peer_id_or_neg1_for_cookie, action, flushed_datagrams).

    Device::route_and_encapsulate

    fn Device::route_and_encapsulate(self : Device, inner_ip : BytesView, now_ms : UInt64) -> (Int, Array[Bytes]) raise ProtocolError

    Route an outbound inner IPv4/IPv6 packet via Cryptokey Routing LPM and encapsulate it for the matched peer. Returns (peer_id, outbound_udp_datagrams).

    Device::tick_all

    fn Device::tick_all(self : Device, now_ms : UInt64) -> Array[(Int, Bytes)] raise ProtocolError

    Tick timers across all peers on this Device, returning (peer_id, wire_datagram) pairs to send.

    InitiatorHandshake

    pub struct InitiatorHandshake {
    local_index : UInt
    local_static_private : Bytes
    remote_static_public : Bytes
    ephemeral_private : Bytes
    preshared_key : Bytes
    chaining_key : Bytes
    hash : Bytes
    last_mac1 : Bytes
    } derive(Eq,
    Debug
    )

    In-flight initiator half-handshake state awaiting a HandshakeResponse (§5.4.2).

    PeerTunnel

    pub struct PeerTunnel {
    local_static_private : Bytes
    local_static_public : Bytes
    remote_static_public : Bytes
    preshared_key : Bytes?
    persistent_keepalive_ms : UInt64?
    // private fields
    }

    Sans-IO WireGuard single-peer state machine managing handshakes, 3-slot key rotation (previous / current / next), RFC 6479 anti-replay, TAI64N monotonic timestamps, Cookie Reply caching, and §6 timers.

    PeerTunnel::decapsulate

    fn PeerTunnel::decapsulate(self : PeerTunnel, wire_bytes : BytesView, now_ms : UInt64, under_load? : Bool, cookie_validator? : CookieValidator, peer_endpoint_bytes? : BytesView) -> (TunnelAction, Array[Bytes]) raise ProtocolError

    Decapsulate an inbound WireGuard UDP datagram at now_ms. Optionally accepts cookie_validator + under_load + peer_endpoint_bytes to enforce mac2 Cookie Reply under DoS load.

    PeerTunnel::encapsulate

    fn PeerTunnel::encapsulate(self : PeerTunnel, inner_ip : BytesView, now_ms : UInt64) -> Array[Bytes] raise ProtocolError

    Encapsulate an outbound inner IPv4/IPv6 packet into one or more outbound WireGuard UDP datagrams. If no active session exists yet, queues the packet (bounded to 16 packets) and emits a HandshakeInitiation. If the active session has crossed REKEY_AFTER_TIME_MS (120s) or REKEY_AFTER_MESSAGES, emits both the encrypted transport packet AND a background rekey HandshakeInitiation.

    PeerTunnel::has_established_session

    fn PeerTunnel::has_established_session(self : PeerTunnel, now_ms : UInt64) -> Bool

    Return true if there is a currently non-expired established TransportSession.

    PeerTunnel::initiate_handshake

    fn PeerTunnel::initiate_handshake(self : PeerTunnel, now_ms : UInt64) -> Bytes raise ProtocolError

    Explicitly initiate a HandshakeInitiation wire frame (148 bytes) at now_ms.

    PeerTunnel::new

    fn PeerTunnel::new(local_static_private : BytesView, remote_static_public : BytesView, entropy_seed : BytesView, initial_index? : UInt, preshared_key? : BytesView, persistent_keepalive_ms? : UInt64) -> PeerTunnel raise ProtocolError

    Construct a deterministic, Sans-IO PeerTunnel with a 32-byte entropy_seed.

    PeerTunnel::owns_receiver_index

    fn PeerTunnel::owns_receiver_index(self : PeerTunnel, idx : UInt) -> Bool

    Return the active local session receiver index (and next/previous indices) owned by this peer.

    PeerTunnel::tick

    fn PeerTunnel::tick(self : PeerTunnel, now_ms : UInt64) -> Array[Bytes] raise ProtocolError

    Advance WireGuard §6 protocol timers at now_ms, returning any wire frames that must be transmitted (handshake retransmission, passive keepalive, persistent keepalive, or session expiration cleanup).

    Tai64n

    pub(all) struct Tai64n {
    raw : Bytes
    } derive(Eq,
    Debug
    )

    TAI64N 12-byte External TAI64 format timestamp (WireGuard §5.4.2). First 8 bytes: big-endian seconds (0x4000000000000000 + unix_seconds + leap_offset). Last 4 bytes: big-endian nanoseconds (0..999_999_999), masked to 0xffffff00 or exact per sender policy (WireGuard Linux kernel masks the lowest 24 bits / ~16ms granularity or full ns; comparison is lexicographical over 12 bytes).

    Tai64n::compare_to

    fn Tai64n::compare_to(self : Tai64n, other : Tai64n) -> Int

    Lexicographical comparison of two 12-byte TAI64N timestamps (> 0 means self is strictly newer than other).

    Tai64n::from_bytes

    fn Tai64n::from_bytes(b : BytesView) -> Tai64n raise ProtocolError

    Wrap raw 12-byte TAI64N wire bytes.

    Tai64n::from_unix

    fn Tai64n::from_unix(unix_secs : UInt64, nanos : UInt) -> Tai64n

    Construct a 12-byte Tai64n timestamp from Unix epoch seconds and nanoseconds (0..999_999_999).

    Tai64n::is_newer_than

    fn Tai64n::is_newer_than(self : Tai64n, other : Tai64n) -> Bool

    TransportSession

    pub struct TransportSession {
    local_index : UInt
    remote_index : UInt
    send_key : Bytes
    recv_key : Bytes
    is_initiator : Bool
    created_ms : UInt64
    send_counter : UInt64
    replay :
    ReplayWindow

    } derive(Eq,
    Debug
    )

    Active transport session slot holding directional keys, counters, creation timestamp, and RFC 6479 replay window.

    TunnelAction

    pub(all) enum TunnelAction {
    NoOp
    SendWirePacket(Bytes)
    DeliverInnerPacket(inner_ip~ : Bytes, response_wire~ : Bytes?)
    ReceivedKeepalive(response_wire~ : Bytes?)
    HandshakeCompleted(response_wire~ : Bytes?)
    } derive(Eq,
    Debug
    )

    Result emitted by PeerTunnel when processing inbound UDP packets or timer ticks.
    let COOKIE_REFRESH_MS : UInt64

    Rotating cookie secret validity window in milliseconds (WireGuard §5.4.7: 2 minutes).

    KEEPALIVE_TIMEOUT_MS

    let KEEPALIVE_TIMEOUT_MS : UInt64

    REJECT_AFTER_TIME_MS

    let REJECT_AFTER_TIME_MS : UInt64

    REKEY_AFTER_MESSAGES

    let REKEY_AFTER_MESSAGES : UInt64

    REKEY_AFTER_TIME_MS

    let REKEY_AFTER_TIME_MS : UInt64

    WireGuard §6 timer & counter constants (milliseconds).

    REKEY_ATTEMPT_TIME_MS

    let REKEY_ATTEMPT_TIME_MS : UInt64

    REKEY_TIMEOUT_MS

    let REKEY_TIMEOUT_MS : UInt64

    TAI64_EPOCH_OFFSET

    let TAI64_EPOCH_OFFSET : UInt64

    WG_CONSTRUCTION_BYTES

    let WG_CONSTRUCTION_BYTES : Bytes

    WG_IDENTIFIER_BYTES

    let WG_IDENTIFIER_BYTES : Bytes

    let WG_LABEL_COOKIE : Bytes

    WG_LABEL_MAC1

    let WG_LABEL_MAC1 : Bytes

    fn compute_cookie_key(peer_static_public : BytesView) -> Bytes

    Compute Hash(LABEL_COOKIE || peer_static_public) (32 bytes, §5.4.7).

    compute_mac1_key

    fn compute_mac1_key(peer_static_public : BytesView) -> Bytes

    Compute Hash(LABEL_MAC1 || peer_static_public) (32 bytes, §5.4.4).
    fn consume_cookie_reply(reply :
    CookieReply
    , remote_static_public : BytesView, last_sent_mac1 : BytesView) -> Bytes raise ProtocolError

    Decrypt and authenticate a received CookieReply (Type 3) using the remote peer's static public key and the mac1 of the handshake message we previously transmitted (§5.4.7).

    consume_handshake_initiation

    fn consume_handshake_initiation(msg :
    HandshakeInitiation
    , responder_static_private : BytesView) -> ConsumedInitiation raise ProtocolError

    Verify and decrypt a HandshakeInitiation on the responder (§5.4.2). Validates mac1 BEFORE performing any Curve25519 scalar multiplications.

    consume_handshake_response

    Verify and consume a HandshakeResponse on the initiator, deriving Initiator DerivedSessionKeys (§5.4.3 & §5.4.5).

    create_handshake_initiation

    fn create_handshake_initiation(local_static_private : BytesView, remote_static_public : BytesView, ephemeral_private : BytesView, sender_index : UInt, timestamp : Tai64n, preshared_key? : BytesView, cookie? : BytesView) -> (
    HandshakeInitiation
    , InitiatorHandshake) raise ProtocolError

    Build a HandshakeInitiation (Type 1, 148 bytes) as the initiator (§5.4.2).

    create_handshake_response

    fn create_handshake_response(consumed : ConsumedInitiation, responder_ephemeral_private : BytesView, responder_index : UInt, preshared_key? : BytesView, cookie? : BytesView) -> (
    HandshakeResponse
    , DerivedSessionKeys) raise ProtocolError

    Build a HandshakeResponse (Type 2, 92 bytes) and derive Responder DerivedSessionKeys (§5.4.3 & §5.4.5).

    initial_noise_state

    fn initial_noise_state() -> (Bytes, Bytes)

    Compute the initial Noise chaining key C_0 = Hash(CONSTRUCTION) and H_0 = Hash(C_0 || IDENTIFIER).

    open_transport_packet

    fn open_transport_packet(recv_key : BytesView, pkt :
    TransportData
    ) -> Bytes raise ProtocolError

    Authenticate, decrypt, and strip WireGuard 16-byte padding from a TransportData packet (§5.4.6). Returns b"" for a valid authenticated 0-byte Keepalive frame.

    seal_transport_packet

    fn seal_transport_packet(remote_index : UInt, send_key : BytesView, counter : UInt64, inner_ip_or_empty : BytesView) ->
    TransportData
    raise ProtocolError

    Encrypt and frame an inner IP packet (or empty keepalive b"") as a TransportData packet (§5.4.6).

    verify_initiation_mac1

    fn verify_initiation_mac1(msg :
    HandshakeInitiation
    , receiver_static_public : BytesView) -> Bool

    Verify mac1 on a HandshakeInitiation against the receiver's static public key.

    verify_response_mac1

    fn verify_response_mac1(msg :
    HandshakeResponse
    , receiver_static_public : BytesView) -> Bool

    Verify mac1 on a HandshakeResponse against the receiver's (initiator's) static public key.