Browse WireGuard specs (22 of 38)

Proofs / WireGuard / Earlier, longer properties

Accepting an initiation

wg_noise_handshake_consume_initiation on any 148 bytes, with any peers in any state, accepts exactly when the model does (the static key decrypts to a known peer, the timestamp is newer, the 20 ms flood limit holds, ...); it then updates that peer exactly as the model does, and on rejection it changes nothing.

  • As noise.c computes the 20 ms limit, an initiation exactly 20 ms after the last one is accepted. The spec draws the clock at that boundary.
  • Besides the handshake that changes, every other peer, the device, the index table and every keypair stay unchanged byte for byte.
spec
x_consume_initiation_any
length
194 lines of C
own proof
10,963 lines of Rocq
status
Proved
22 of 38
On this page

The spec

The property is stated by the C program harness/x_consume_initiation_any.c, 194 lines. Here is its main cut down to the inputs it draws, the calls into the code under test and the checks (lines 48–194; ... marks lines left out). The checks call helpers and the model in the spec and in harness/common.h.

C
int main(void)
{
	...
	wg_shim_boottime_ns = draw_boottime();
	wg_noise_init();
	nondet_bytes(raw_a, 32);
	nondet_bytes(raw_b, 32);
	...
	n = draw_steps();
	for (i = 0; i < n; i++) {
		nondet_bytes(pub, 32);
	...
	for (it = peers; it; it = it->next) {
		...
		if (nondet_u8() % 4 != 0)
	...
	mode = nondet_u8() % 3;
		...
		nondet_bytes(eraw, 32);
	...
	ret = wg_noise_handshake_consume_initiation(msg, &wb);
	...
	CHECK(bytes_eq(img, img_after, INIT_LEN));
	CHECK(dev_same(&ds, &wb) && itb.head == head0 && kents_same(kes) && shim_same(&ss));
		...
		CHECK(ret == NULL && hnodes_same_except(peers, NULL));
		...
		CHECK(ret == target->p && target->p->refcount.refcount == target->refs + 1);
		...
		CHECK(hnodes_same_except(peers, target->p));
		CHECK(h->state == HANDSHAKE_CONSUMED_INITIATION);
		...
		CHECK(bytes_eq(h->latest_timestamp, ci.ts, 12));
		CHECK(bytes_eq(h->hash, ci.st.h, 32) && bytes_eq(h->chaining_key, ci.st.ck, 32));
		CHECK(h->remote_index == msg->sender_index);
		CHECK(h->last_initiation_consumption == m_last_consumption(s->last_initiation_consumption, now));
		CHECK(h->entry.peer == s->peer && h->entry.next == s->next && h->entry.hashed == s->hashed &&
		...
		CHECK(target->p->refcount.refcount == target->refs && pr_same(&target->rest, target->p));
...
}

The full spec at commit 3c1fc13 ↗

The property as the spec's first comment states it
Law `consume_initiation_any`: wg_noise_handshake_consume_initiation on ANY message and ANY prior state.

State: responder device B with ANY static private key; ANY number of peers (unique public keys: the device's
invariant) with ANY public keys and preshared keys (or none), ANY mutable handshake state (common.h hs_randomize),
ANY other mutable peer state (common.h peer_randomize_rest: dead flag, last sent handshake, cookie state) and any
reference count below INT_MAX (common.h draw_peer_refs); each peer's handshake entry is in B's index table or not
(any), and ANY number of keypairs (whole objects of ANY content, common.h kents_add) of ANY peers are in the table
too, possibly in their peers' slots; one peer (A) has a public key whose private key the harness knows; ANY boot
clock.  Message: ANY 148 bytes (all fields drawn), or A's valid initiation (built by the model, any ephemeral
randomness, any timestamp), optionally with one byte changed.
Claims (exact agreement with the model, common.h):
 - the result is the peer whose handshake the model accepts, or NULL; acceptance exactly when: B has an
   identity, DH(S_b, E) is not zero, the static decrypts to the public key of some peer P, P's precomputed
   static-static DH is not zero, the timestamp decrypts, it is greater than P's latest timestamp
   (lexicographic, TAI64N: an EQUAL timestamp is rejected) and P's last consumption is at least 20 ms before
   now, exactly as noise.c compares it: rejected iff (s64)(last + 20000000 ns) > (s64)now, so an initiation
   exactly 20 ms after the last one is ACCEPTED (wireguard-go rejects at exactly 20 ms; whitepaper: no such
   rule);
 - on acceptance, P's handshake is exactly: state CONSUMED_INITIATION, remote ephemeral = E, latest timestamp =
   the decrypted one, hash and chaining key = the model's, remote index = the message's sender index, last
   consumption = now if later; every other field of P's handshake and ALL the rest of P (cookie state, keypair
   slots, last sent handshake, dead flag, ...), and every other peer as a whole, unchanged; P holds one more
   reference (the returned one);
 - on rejection NOTHING changes: every peer as a whole (handshake byte for byte, reference count, cookie state,
   keypair slots, ...);
 - in both cases (whole-state frame): the device (identity, cookie checker, public-key table) and
   every entry of the index table and every keypair (byte for byte) are unchanged, nothing is allocated or freed
   and no peer is released (an initiation never creates state by itself), no reference count saturates or warns
   (common.h shim_same); the message is never modified.
(The harness draws the boundaries on purpose: the boot clock exactly 20 ms +- 2 ns after A's last consumption,
and A's latest timestamp equal to the message's or one below / above it.)

The code it is about

wg_noise_handshake_consume_initiation in drivers/net/wireguard/noise.c, lines 583–668 (... marks lines left out):

C
struct wg_peer *
wg_noise_handshake_consume_initiation(struct message_handshake_initiation *src,
				      struct wg_device *wg)
{
	...
	down_read(&handshake->lock);
	replay_attack = memcmp(t, handshake->latest_timestamp,
			       NOISE_TIMESTAMP_LEN) <= 0;
	flood_attack = (s64)handshake->last_initiation_consumption +
			       NSEC_PER_SEC / INITIATIONS_PER_SECOND >
		       (s64)ktime_get_coarse_boottime_ns();
	up_read(&handshake->lock);
	if (replay_attack || flood_attack)
		break;

...
}

This is the code under test as the proof sees it: Linux at 551c722f with the two lib/crypto patches, and with the idiom rewrites that VST needs, each marked QUICKPROOF in the code and listed in DEVIATIONS.md.

The theorem

QuickProof generated this statement from the spec in src/contracts/x_consume_initiation_any.v:

Rocq
Theorem x_consume_initiation_any : harness_safe whole_prog.

whole_prog is the spec linked with the code under test. The 19 x_ specs are proved together, as the theorem suite_x, from one specification of every function of the code. src/contracts/laws.v states each spec on its own program and takes it from suite_x:

Rocq
Theorem x_consume_initiation_any : harness_safe WgNoise.contracts.x_consume_initiation_any.whole_prog.
Proof. exact (proj1 (proj2 WgNoise.contracts.suite.suite_x)). Qed.

qp verify checks that this statement is the one generated from the spec at the contract commit, and that its proof uses no axiom beyond the 13 that every spec here uses: standard logical axioms and VST's abstract malloc library.

The C files (units) its program links
  • drivers/net/wireguard/noise.c
  • drivers/net/wireguard/cookie.c
  • patch/counter_validate.c
  • lib/crypto/blake2s.c
  • lib/crypto/chacha.c
  • lib/crypto/chacha-block-generic.c
  • lib/crypto/poly1305.c
  • lib/crypto/poly1305-donna32.c
  • lib/crypto/chacha20poly1305.c
  • lib/crypto/curve25519.c
  • lib/crypto/curve25519-fiat32.c
  • patch/wg_shim.c
  • quickproof/libc/libc.c
  • quickproof/libc/malloc_unit.c

What it leaves out

  • Like every spec of this proof: runs of more than one thread (locks and RCU are those of a single thread), termination (a run that never returns is not a counterexample), and binaries built by a compiler (the theorem is about the code as CompCert defines C for 64-bit x86).
  • The kernel around the code is a shim of plain C for one thread, and the code carries rewrites of idioms that VST cannot handle. Both are tested against the kernel's code, not proved.

The summary page lists what the proof as a whole does not cover.

How it was proved

src/proof/harness/consume_initiation_any/ holds 10,963 lines of Rocq (formerly Coq) that are specific to this spec. It builds on the specifications and proofs of the functions of noise.c, cookie.c and lib/crypto, the protocol model, and the Diffie-Hellman agreement of X25519. The whole proof of the 38 specs is about 300,000 lines. AI agents wrote it, Rocq checks all of it, and nobody has to read it. What people have to read is the spec above, with harness/common.h, and the kernel shim that DEVIATIONS.md describes.

Other specs about noise.c: see the list on the left, or the summary.