Browse WireGuard specs (9 of 38)
Proofs / WireGuard / Replay protection
rp_fresh
A nonce below REJECT_AFTER_MESSAGES and at most 8128 below the newest accepted nonce (or with none accepted yet) is accepted, after any other nonces.
- spec
- 33 lines of C
- with its headers
- 73 lines
- status
- Proved
- 9 of 38
On this page
The spec
The spec is the C program harness/rp_fresh.c, 33 lines, shown whole as it is in the repository at 3c1fc13. It includes harness/wg_laws.h (40 lines). Its first comment states the property in full, with every bound.
// Law rp_fresh: For a new key's replay window (zero, as keypair_create makes it), any nonce n and any sequence of any
// number of other nonces given to counter_validate first, n is accepted when it is below REJECT_AFTER_MESSAGES
// (2^64 - 8130) and at most COUNTER_WINDOW_SIZE (8128) below the largest nonce accepted before it (or when none was
// accepted).
#include "wg_laws.h"
int main(void) {
// Choose the inputs: the nonce n, then any other nonces in any order; remember the largest one accepted
struct noise_replay_counter window;
new_counter(&window);
u64 n = any_u64();
u64 newest = 0;
int accepted_any = 0;
while (nondet_u8() != 0) {
u64 other = any_u64();
if (other != n) {
bool accepted = wg_counter_validate(&window, other);
if (accepted && (accepted_any == 0 || other > newest))
newest = other;
if (accepted)
accepted_any = 1;
}
}
// Check. RFC 6479, section 2: "If S is inside window (S<=WT and S>=WB), then the receiver checks the
// corresponding bit ... to see if this S has already been seen"; receive.c: "This is RFC6479"; the window is
// the newest nonce and the COUNTER_WINDOW_SIZE below it (selftest/counter.c, tests 1, 2, 4-6, 8, 9, 11, 12,
// 14, 17, 18, 22, 26, 29, 33).
if (n < REJECT_AFTER && (accepted_any == 0 || n + COUNTER_WINDOW_SIZE >= newest)) {
bool accepted = wg_counter_validate(&window, n);
gp_assert(accepted != 0);
}
return 0;
}harness/wg_laws.h, 40 lines
// wg_laws.h: trivial helpers shared by the WireGuard laws (each law includes it once). No models, no algorithms.
#include "quickproof.h"
#include "noise.h"
#include <crypto/blake2s.h>
#include <crypto/chacha.h>
#include <crypto/poly1305.h>
#include <crypto/chacha20poly1305.h>
bool wg_counter_validate(struct noise_replay_counter *counter, u64 nonce); // receive.c's counter_validate (S19)
#define REJECT_AFTER (U64_MAX - COUNTER_WINDOW_SIZE - 1) // messages.h: REJECT_AFTER_MESSAGES, 2^64 - 8130
static size_t upto(size_t max) { // any value from 0 to max
size_t value = nondet_size();
return value < max ? value : max;
}
static u64 any_u64(void) { // any 64-bit value
u64 high = nondet_u32();
return high << 32 | nondet_u32();
}
static u8 *any_bytes(size_t n) { // a new object of n arbitrary bytes (NULL if malloc fails)
u8 *bytes = malloc(n);
if (bytes != NULL && n != 0)
nondet_bytes(bytes, n);
return bytes;
}
static u8 *copy_of(const u8 *source, size_t n) { // a new object holding source[0..n-1]
u8 *copy = malloc(n);
for (size_t i = 0; copy != NULL && i < n; i++)
copy[i] = source[i];
return copy;
}
static int bytes_eq(const u8 *a, const u8 *b, size_t n) { // a[0..n-1] == b[0..n-1]
for (size_t i = 0; i < n; i++)
if (a[i] != b[i])
return 0;
return 1;
}
static void new_counter(struct noise_replay_counter *c) { // a new key's replay counter: zero (keypair_create)
c->counter = 0;
for (size_t i = 0; i < COUNTER_BITS_TOTAL / BITS_PER_LONG; i++)
c->backtrack[i] = 0;
}The theorem
QuickProof generated this statement from the spec, in src/contracts/rp_fresh.v:
Theorem rp_fresh : harness_safe whole_prog.src/contracts/laws.v states it on the spec's own program, whole_prog (the spec linked with the code under test), with its proof:
Theorem rp_fresh : harness_safe WgNoise.contracts.rp_fresh.whole_prog.
Proof. exact (proj1 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 WgNoise.contracts.suite.suite))))))))))))))))). Qed.Read as a statement about runs, it says that no run of the program has undefined behaviour or a failing check, for every value the spec can draw. That reading rests on VST's adequacy theorem, which is not yet instantiated in Rocq (formerly Coq) for QuickProof's environment.
The C files (units) its program links
patch/counter_validate.c
What it leaves out
- What the first comment of the spec excludes, and its bounds.
- Like every spec of this proof: runs of more than one thread (the locks and RCU are those of one 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 says what the proof as a whole covers.
How it was proved
AI agents wrote the proof, and Rocq checks it. qp verify passes on this repository: this statement is the one generated from the spec at the contract commit, and its proof uses only Rocq's standard axioms and VST's abstract malloc library.