File syn_wg.c¶
Go to the source code of this file
WireGuard client — Noise_IKpsk2 handshake + transport. More...
#include "../crypto/syn_blake2s.h"#include "../crypto/syn_chacha20poly1305.h"#include "../crypto/syn_x25519.h"#include "../port/syn_port_socket.h"#include "../port/syn_port_system.h"#include "../util/syn_assert.h"#include "../util/syn_metrics.h"#include "../util/syn_random.h"#include "syn_wg.h"#include <string.h>
Public Static Attributes¶
| Type | Name |
|---|---|
| const uint8_t WG_LABEL_COOKIE[] | SYN_UNUSED = "cookie--" |
| const uint8_t | WG_CONSTRUCTION = /* multi line expression */ |
| const uint8_t | WG_IDENTIFIER = /* multi line expression */ |
| const uint8_t | WG_LABEL_MAC1 = "mac1----" |
Public Functions¶
| Type | Name |
|---|---|
| void | syn_wg_disconnect (SYN_WG * wg) Disconnect the WireGuard tunnel and close socket. |
| SYN_Status | syn_wg_get_stats (const SYN_WG * wg, SYN_WgStats * stats) Retrieve WireGuard tunnel metrics and statistics. |
| void | syn_wg_init (SYN_WG * wg, const SYN_WgConfig * config, SYN_SNTP * sntp, uint8_t * rx_buf, size_t rx_buf_size, uint8_t * tx_buf, size_t tx_buf_size) Initialize the WireGuard client. |
| SYN_Status | syn_wg_send (SYN_WG * wg, const uint8_t * ip_packet, size_t len) Send an IP packet through the WireGuard tunnel. |
| SYN_PT_Status | syn_wg_task (SYN_PT * pt, SYN_Task * task) Cooperative protothread task — drives the WireGuard client. |
Public Static Functions¶
| Type | Name |
|---|---|
| uint32_t | load32_le (const uint8_t * p) Load 32-bit little-endian word. |
| uint64_t | load64_le (const uint8_t * p) Load 64-bit little-endian word. |
| void | store32_be (uint8_t * p, uint32_t v) Store 32-bit big-endian word. |
| void | store32_le (uint8_t * p, uint32_t v) Store 32-bit little-endian word. |
| void | store64_be (uint8_t * p, uint64_t v) Store 64-bit big-endian word. |
| void | store64_le (uint8_t * p, uint64_t v) Store 64-bit little-endian word. |
| bool | wg_consume_response (SYN_WG * wg, const uint8_t * msg, size_t len) Parse and validate a handshake response, deriving session keys. |
| bool | wg_decrypt_and_hash (uint8_t h, const uint8_t k, const uint8_t * ct, size_t ct_len, const uint8_t tag, uint8_t * plain) Decrypt-and-hash: verify + decrypt, mix ciphertext+tag into hash. |
| void | wg_encrypt_and_hash (uint8_t h, const uint8_t k, const uint8_t * plain, size_t plain_len, uint8_t * ct, uint8_t tag) Encrypt-and-hash: encrypt plaintext, mix ciphertext+tag into hash. |
| bool | wg_handle_transport (SYN_WG * wg, const uint8_t * msg, size_t len) Decrypt and deliver an incoming WireGuard transport message. |
| void | wg_hkdf2 (uint8_t out1, uint8_t out2, const uint8_t ck, const uint8_t * input, size_t input_len) |
| void | wg_hkdf3 (uint8_t out1, uint8_t out2, uint8_t out3, const uint8_t ck, const uint8_t * input, size_t input_len) |
| void | wg_mac1 (uint8_t mac, const uint8_t peer_pub, const uint8_t * msg, size_t msg_len) Compute mac1 = keyed BLAKE2s(HASH("mac1----" || peer_pub), msg). |
| void | wg_mix_hash (uint8_t h, const void * data, size_t len) Mix hash: H = BLAKE2s(H || data). |
| void | wg_mix_key (uint8_t ck, uint8_t k, const uint8_t * input, size_t len) Mix key: (CK, k) = HKDF(CK, input). |
| bool | wg_replay_check (SYN_WgSession * s, uint64_t counter) Anti-replay check using a sliding window bitmap. |
| bool | wg_send_initiation (SYN_WG * wg) Build and send a Noise_IKpsk2 handshake initiation message. |
| void | wg_send_keepalive (SYN_WG * wg) Send an empty (keepalive) transport message. |
| void | wg_tai64n (uint8_t out, const SYN_SNTP * sntp) Write a 12-byte TAI64N timestamp from NTP time. |
Detailed Description¶
Implements the WireGuard protocol per the whitepaper: https://www.wireguard.com/papers/wireguard.pdf
Noise protocol pattern: IKpsk2 Initiator (us) knows responder's static public key. PSK mixed in at handshake step 2.
Public Static Attributes Documentation¶
variable SYN_UNUSED¶
Cookie label
variable WG_CONSTRUCTION¶
Noise construction string
variable WG_IDENTIFIER¶
Protocol identifier string
variable WG_LABEL_MAC1¶
MAC1 label
Public Functions Documentation¶
function syn_wg_disconnect¶
Disconnect the WireGuard tunnel and close socket.
Parameters:
wgClient context.
function syn_wg_get_stats¶
Retrieve WireGuard tunnel metrics and statistics.
Parameters:
wgClient context.stats[out] Output statistics structure.
Returns:
SYN_OK on success.
function syn_wg_init¶
Initialize the WireGuard client.
void syn_wg_init (
SYN_WG * wg,
const SYN_WgConfig * config,
SYN_SNTP * sntp,
uint8_t * rx_buf,
size_t rx_buf_size,
uint8_t * tx_buf,
size_t tx_buf_size
)
Derives the public key from the private key and prepares state. Does NOT open a socket or start a handshake — that happens when the protothread task runs.
Parameters:
wgClient context.configPeer configuration (copied).sntpSNTP time source (must be initialized, may not be synced yet).rx_bufReceive buffer (at least SYN_WG_MTU + SYN_WG_TRANSPORT_OVERHEAD).rx_buf_sizeReceive buffer capacity.tx_bufTransmit buffer (same sizing).tx_buf_sizeTransmit buffer capacity.
function syn_wg_send¶
Send an IP packet through the WireGuard tunnel.
Encrypts the packet with the current session key and sends it as a WireGuard transport message.
Parameters:
wgClient context.ip_packetRaw IP packet to send.lenPacket length.
Returns:
SYN_OK on success, SYN_ERROR if no session or send failed.
function syn_wg_task¶
Cooperative protothread task — drives the WireGuard client.
Handles: NTP sync wait, handshake, incoming packet processing, keepalive, and rekeying. Pass the SYN_WG context via task->user_data.
Parameters:
ptProtothread.taskTask descriptor.
Returns:
PT status.
Public Static Functions Documentation¶
function load32_le¶
Load 32-bit little-endian word.
Parameters:
pSource bytes.
Returns:
32-bit value.
function load64_le¶
Load 64-bit little-endian word.
Parameters:
pSource bytes.
Returns:
64-bit value.
function store32_be¶
Store 32-bit big-endian word.
Parameters:
pDestination bytes.vValue to store.
function store32_le¶
Store 32-bit little-endian word.
Parameters:
pDestination bytes.vValue to store.
function store64_be¶
Store 64-bit big-endian word.
Parameters:
pDestination bytes.vValue to store.
function store64_le¶
Store 64-bit little-endian word.
Parameters:
pDestination bytes.vValue to store.
function wg_consume_response¶
Parse and validate a handshake response, deriving session keys.
Parameters:
wgClient context.msgReceived message data.lenReceived message length.
Returns:
true if response was valid and session established.
function wg_decrypt_and_hash¶
Decrypt-and-hash: verify + decrypt, mix ciphertext+tag into hash.
static bool wg_decrypt_and_hash (
uint8_t h,
const uint8_t k,
const uint8_t * ct,
size_t ct_len,
const uint8_t tag,
uint8_t * plain
)
Parameters:
hRunning hash (32 bytes).kDecryption key (32 bytes).ctCiphertext input.ct_lenCiphertext length.tagMAC tag input (16 bytes).plainPlaintext output.
Returns:
true if decryption and verification succeeded.
function wg_encrypt_and_hash¶
Encrypt-and-hash: encrypt plaintext, mix ciphertext+tag into hash.
static void wg_encrypt_and_hash (
uint8_t h,
const uint8_t k,
const uint8_t * plain,
size_t plain_len,
uint8_t * ct,
uint8_t tag
)
Parameters:
hRunning hash (32 bytes).kEncryption key (32 bytes).plainPlaintext input.plain_lenPlaintext length.ctCiphertext output.tagMAC tag output (16 bytes).
function wg_handle_transport¶
Decrypt and deliver an incoming WireGuard transport message.
Parameters:
wgClient context.msgReceived message data.lenReceived message length.
Returns:
true if message was valid and delivered.
function wg_hkdf2¶
static void wg_hkdf2 (
uint8_t out1,
uint8_t out2,
const uint8_t ck,
const uint8_t * input,
size_t input_len
)
HKDF-Extract + Expand producing 2 outputs.
Parameters:
out1Output buffers (32 bytes each).ckChaining key (32 bytes).inputInput data.input_lenLength of input data.
function wg_hkdf3¶
static void wg_hkdf3 (
uint8_t out1,
uint8_t out2,
uint8_t out3,
const uint8_t ck,
const uint8_t * input,
size_t input_len
)
HKDF-Extract + Expand producing 3 outputs.
Parameters:
out1Output buffers (32 bytes each).ckChaining key (32 bytes).inputInput data.input_lenLength of input data.
function wg_mac1¶
Compute mac1 = keyed BLAKE2s(HASH("mac1----" || peer_pub), msg).
Parameters:
macOutput MAC (16 bytes).peer_pubPeer's public key.msgMessage data.msg_lenLength of message data.
function wg_mix_hash¶
Mix hash: H = BLAKE2s(H || data).
Parameters:
hRunning hash (32 bytes).dataInput data.lenLength of input data.
function wg_mix_key¶
Mix key: (CK, k) = HKDF(CK, input).
Parameters:
ckRunning chaining key (32 bytes).kOutput key (32 bytes).inputInput data.lenLength of input data.
function wg_replay_check¶
Anti-replay check using a sliding window bitmap.
Parameters:
sActive session.counterReceived counter value.
Returns:
true if counter is new and within window.
function wg_send_initiation¶
Build and send a Noise_IKpsk2 handshake initiation message.
Parameters:
wgClient context.
Returns:
true if message was built and sent.
function wg_send_keepalive¶
Send an empty (keepalive) transport message.
Parameters:
wgClient context.
function wg_tai64n¶
Write a 12-byte TAI64N timestamp from NTP time.
Parameters:
outOutput buffer (12 bytes).sntpNTP time source.
The documentation for this class was generated from the following file src/syntropic/net/syn_wg.c