Commit Graph
3397 Commits
Author SHA1 Message Date
riccardom 8a04fa4d3f Addresses CI fixes 2026-09-11 14:48:54 +02:00
riccardom 11555c3139 Addresses CI fixes 2026-09-11 14:48:54 +02:00
riccardom 1dc48bcc06 Addresses CI fixes 2026-09-11 14:48:54 +02:00
riccardom 82699afb0f [client] pqkem: split handshaker Listen into per-message handlers
Reduce Listen's cognitive complexity (SonarCloud S3776, 30 -> under 25) by
extracting the offer and answer cases into handleRemoteOffer/handleRemoteAnswer,
with shared onSignalReceived/notifyListeners helpers and a pqControllerReoffer
helper for the controller re-offer branch. No functional change.
2026-09-11 14:48:54 +02:00
riccardom 4c6735e8ae [client] pqkem: don't let the responder's delayed update revert the PSK
The responder configures WireGuard with endpoint=nil first, then a delayed
update (scheduleDelayedUpdate) applies the real endpoint after fallbackDelay.
It captured the preshared key at schedule time and re-applied it. With the
post-quantum exchange the PSK can change within that window (a fresher PSK
derived and applied via SetPresharedKey), so re-applying the captured one
reverted WireGuard to a key the remote peer no longer used — a mismatch that
stalled the handshake until the WGWatcher timeout forced a retry (~30s).

Pass a nil PSK in the delayed update so it only sets the endpoint and leaves
the current PSK in place; the latest SetPresharedKey wins.
2026-09-11 14:48:54 +02:00
riccardom 3b61222b07 [client] pqkem: gate controller re-offer to kick the KEM exactly once
When the controller receives the responder's (KEM-less) offer it replies with
its own KEM offer instead of answering, so the only transaction that brings the
tunnel up is the one that also carries the PSK. Guard that reply with
ShouldSendBootstrapOffer so it fires only when no exchange is in flight: without
it, every responder offer triggered another offer (an offer-per-offer runaway).
The whole behaviour is isolated to the KEM path (config.PQ != nil); non-PQ
connections answer as before.
2026-09-11 14:48:54 +02:00
riccardom 1870b104ba [client] pqkem: make signalling bootstrap idempotent through awaitingRekey
The controller sends its KEM offer both on its own guard event and in reply
to the responder's offer. SignalOffer was idempotent only while awaiting the
answer; once the answer arrived (awaitingRekey) a repeat call started a fresh
exchange with a different PSK, desyncing the two peers (one on the old PSK,
one on the new) so WireGuard derived misaligned transport keys and dropped all
data. Treat awaitingRekey as in-flight too and return the same offer.
2026-09-11 14:48:54 +02:00
riccardom 4ffa2c724f Introduces a forced imparity on MLKEM bootstrap.
To ensure two peers agree on a key, we need asymmetry. one peer is
the controller ("initiator") the other is the "responder".

Otherwise imagine two offers in parallel driving two answers at the same time

   A                   B
   | <----B-OFFER----- |
   | -----A-OFFER----> |
   |                   |
   |                   |
   ---------------------------------
  |****** ICE + WG Handshake ****** |
   ---------------------------------
   |                   |
   | <----B-ANSWER---- |
   | -----A-ANSWER---> |

PSK is derived on receive of offer, so A and B derive different PSKs.
When WG handshake takes place it picks misaligned PSKs.

So we impair the two nodes and only the offer of one of the two (the controller/initiator)
is allowed to progress and drive the answer (and carry the KEM material).

If a responder initiates an offer, we redo the offer towards it. This is oK
since the ICEworker don't treat offer/answers differently.
2026-09-11 14:48:54 +02:00
riccardom 3ed4b4fb43 Revert "Introduces a forced WG handshake on initial MLKEM bootstrap."
This reverts commit b5a72eca65.
2026-09-11 14:48:54 +02:00
riccardom a78d587e7a Introduces a forced WG handshake on initial MLKEM bootstrap.
To ensure two peers agree on a key, we need asymmetry. one peer is
the controller ("initiator") the other is the "responder".

Otherwise imagine two offers in parallel driving two answers at the same time

   A                   B
   | <----B-OFFER----- |
   | -----A-OFFER----> |
   |                   |
   |                   |
   ---------------------------------
  |****** ICE + WG Handshake ****** |
   ---------------------------------
   |                   |
   | <----B-ANSWER---- |
   | -----A-ANSWER---> |

PSK is derived on receive of offer, so A and B derive different PSKs.
When WG handshake takes place it picks misaligned PSKs.

So we impair the two nodes and only the offer of one of the two (the controller/initiator)
carries the KEM material.

This means that if the responder OFFER/ANSWER comes first, when the controller/initiator's one
completes (and the genuine PSK is shared between A and B, we need to force a new WG handshake with
the proper keys.
2026-09-11 14:48:54 +02:00
riccardom 2b27040fb0 Anticipates PSK before WG does handshake so it finds it to set it 2026-09-11 14:48:54 +02:00
riccardom b93e8d6676 Skip default port send in signal proto 2026-09-11 14:48:54 +02:00
riccardom cfaf4f1bab Allow non strict mode 2026-09-11 14:48:54 +02:00
riccardom 5a6de00f7a Adds PQ connection tests 2026-09-11 14:48:54 +02:00
riccardom cb5ee8efa8 Remove obvious comments; leave only the why of things 2026-09-11 14:48:54 +02:00
riccardom 202176ec23 Be more explicit on names that is a fake key to ensure we don't communicate with others in strict mode 2026-09-11 14:48:54 +02:00
riccardom bfa36521da Adds test to validate compromised keys are not accepted 2026-09-11 14:48:54 +02:00
riccardom d988525850 Prioritize Kem over RP 2026-09-11 14:48:54 +02:00
riccardom 63d29966fd pqkem: concurrency tests (recovery + race)
- RecoversViaResignalAfterDataPathBreak: a data-path rotation that can no longer
  converge raises OnRekeyFailed, and re-bootstrapping over signalling resyncs both
  peers on a fresh PSK even while the data path stays broken.
- ConcurrentRekeysNoRace: hammers the single-lock state machine with concurrent
  rotation clocks from many goroutines (run with -race) and asserts no split-brain
  via a final deterministic bootstrap.
2026-09-11 14:48:54 +02:00
riccardom d485567978 pqkem: recover from persistent rekey failure by re-bootstrapping over signal
OnRekeyFailed now re-runs the KEM bootstrap over Signal (conn.RequestReoffer ->
handshaker.SendOffer) instead of only logging: a fresh signalling offer starts a new
exchange that overwrites the stalled PSK on both sides, resyncing after a persistent
data-path desync. Chosen over a responder-side awaitingAck revert (which fights the
confirm-less ack timing) and a full tunnel teardown (heavier). The tunnel stays up on
the previous PSK meanwhile since Signal is independent of the broken data path.
2026-09-11 14:48:54 +02:00
riccardom e1d5551ba5 Discriminate initial from rekey failure 2026-09-11 14:48:54 +02:00
riccardom f322be1944 pqkem: strict (fail-closed) mode + wire status Quantum resistance
Strict mode (NB_PQ_MLKEM_STRICT, default off) closes the initial PQ-vulnerable
window (NET-1408): when enabled, conn.presharedKey programs a per-conn random
sentinel PSK until the ML-KEM exchange derives the real one, so no session can form
on a non-PQ key (the real PSK is pushed via SetPresharedKey once it converges).
Default stays opportunistic.

Also surface PQ status: the peer 'Quantum resistance' flag (RosenpassEnabled) is now
true when an ML-KEM PSK has been derived for the peer, not only for Rosenpass.
2026-09-11 14:48:54 +02:00
riccardom b1b2cc14c1 pqkem: rotate PSK in kernel mode instead of skipping
The idle-gate reads LastActivities, which only tracks per-peer data in userspace;
in kernel mode it is empty, so the gate treated every kernel peer as idle and
disabled data-path rotation entirely. Detect the bind via IsUserspaceBind and, in
kernel mode, report zero activity age (always 'active') so rotation runs on every
rekey. Lazy back-to-idle is already limited in kernel; the eBPF WG-activity
detection will later supply a real signal that excludes handshake/pqkem traffic.
2026-09-11 14:48:54 +02:00
riccardom 8c8abfb2b1 pqkem: derive PSK with HKDF-SHA256
Replace the raw SHA-256 concat combiner with HKDF-SHA256 (crypto/hkdf, Go 1.24):
IKM = ML-KEM_ss || X25519_ss (draft-ietf-tls-ecdhe-mlkem order), salt = the
domain-separation label, info = full transcript (offer || answer) || canonicalised
peer identities. Keeps the transcript + identity binding while using a proper KDF.
2026-09-11 14:48:54 +02:00
riccardom b1c0a2bffe Don't rotate PQ keys if data path is idle for ~90s (less than a WG handhshake time 2026-09-11 14:48:54 +02:00
riccardom 562c02fa42 Adds log tracepoints
- Add a trace slog level (NB_PQ_MLKEM_LOG_LEVEL=trace) and move the verbose
  per-exchange lifecycle logs (offer/answer/PSK/ack/rotation) to it, so debug
  stays quiet and troubleshooting is opt-in.
- Stop logging the raw preshared key; drop the temporary pqkem-dbg OnRemoteOffer/
  OnRemoteAnswer probes.
- Demote the per-handshake conn log to trace.
2026-09-11 14:48:54 +02:00
riccardom b92b14543d Renames SetRemotePort to SetRemoteAddr 2026-09-11 14:48:54 +02:00
riccardom 1c46b4e9df pqkem: clock data-path PSK rotation from WireGuard handshakes
Source OnDataPathRekeyed from the WGWatcher's per-handshake callback
(onWGCheckSuccess), which fires only on a fresh handshake, and OnDataPathDown
from the handshake-timeout path. A fresh handshake clocks the next chained
KEM exchange pushed over the data-path UDP transport.
2026-09-11 14:48:54 +02:00
riccardom e3ab585c56 pqkem: register data-path endpoint from signalling
Learn the peer's data-path endpoint from the signalling offer/answer: its WG
overlay IP combined with the advertised pq UDP port (SetRemotePort -> AddPeer).
Registering here is safe before the tunnel is up because sends only ever fire
once it is (clocked by OnDataPathRekeyed). RemovePeer is wired at peer teardown
(engine.removePeer), not on transient disconnect.
2026-09-11 14:48:54 +02:00
riccardom 7c276ce727 pqkem: apply derived PSK at WG peer-config time (pull) + keep push for rekey 2026-09-11 14:48:54 +02:00
riccardom 555310bd56 pqkem: carry KEM offer/answer over the signalling exchange 2026-09-11 14:48:54 +02:00
riccardom 56b315d954 pqkem: dedicated slog logger via NB_PQ_MLKEM_LOG_LEVEL 2026-09-11 14:48:54 +02:00
riccardom bac6af349b Homogeneous logs prefix 2026-09-11 14:48:54 +02:00
riccardom 3270fc5e18 Bit of renaming
peer -> peerAddrs
have types for remoteID and localID
t.Close log error
Manager SetTransport -> Start
2026-09-11 14:48:54 +02:00
riccardom 539a1f44c8 Typo 2026-09-11 14:48:54 +02:00
riccardom ef29e996ce Race fix 2026-09-11 14:48:54 +02:00
riccardom cda3922478 Makes Transport just a UDP socket.
Manager owns maps for remoteID <-> remote UDP addr
Engine talks to manager only
2026-09-11 14:48:54 +02:00
riccardom d2db1c7ffc Adds transport 2026-09-11 14:48:54 +02:00
riccardom 6a42f7da00 Communicate the port over the signal exchange 2026-09-11 14:48:54 +02:00
riccardom 40b6a554c7 Ensure iface is up and with overlay ip assigned to get a valid UDP port 2026-09-11 14:48:54 +02:00
riccardom 0ae462238f Adds real callback setter for PSK on ready 2026-09-11 14:48:54 +02:00
riccardom 077d96ae03 Initializes PQ ML-KEM manager 2026-09-11 14:48:54 +02:00
riccardom dd5e9435e9 Adds no-op Transports and callbacks 2026-09-11 14:48:54 +02:00
riccardom 41298ac24d Added enabled env var 2026-09-11 14:48:54 +02:00
riccardom 30212ca92c Adds MLKEM Payload placeholder to client internals 2026-09-11 14:48:54 +02:00
riccardom 902daf61e6 Invert order of keys as per draft 2026-09-11 14:48:54 +02:00
riccardom 6a644df59b Protocol update 2026-09-11 14:48:54 +02:00
riccardom 0aeed6ae5b Removes confirm. Uses next offer to deliver confirmation/ack of previous round
We clock the next Offer initiation to the OnDataPathRekeyed, so we have 2 minutes
ahead of us to do our attempts and stuff before to give up.
On failure, we will know because we will not receive a new answer.. but more importantly
the wg handshake will fail :D
2026-09-11 14:48:54 +02:00
riccardom cec4bd49b4 Leave signal offer/answer as a pull/push operation not as an actual transport 2026-09-11 14:48:54 +02:00
riccardom 2ed0d3bf03 Assume two transports: initial "signal" (control plane) one (no data path established yet) + data path one
Define OnDataPathRekeyed event to transition from control plane path to data plane path over the WG tunnel.

Keep confirm ALWAYS on NEW established WG tunnel (posthandshake with rekeying). We keep an active method
irrelevant of the WG handshake (we might decide that the indirect wg handshake is sufficient in the future).

Optimistic commit on responder(when sending answer), while on initiator we set it on getting the answer
2026-09-11 14:48:54 +02:00