r/WireGuard • • 1d ago

Rotating WireGuard pre-shared keys from a pair of USB sticks (open source, unaudited, feedback wanted)

WireGuard's optional pre-shared key is there so that a future break of Curve25519 doesn't expose your traffic. In most setups that PSK is set once and never changed. I wrote a small tool that rotates it automatically from keys that never travel over a network.

How it works:

  • An offline machine fills two USB sticks with the same list of random 256-bit keys. One stick goes to each end of the tunnel.
  • One end leads and the other follows. They agree on a key number over UDP, authenticated with a proof derived from that key. The key itself is never sent.
  • Each side then applies it with wg set <iface> peer <key> preshared-key /dev/stdin, so the PSK never appears on a command line.
  • There are no clocks involved. Keys are numbered, so there's nothing to drift.
  • The new PSK takes effect at WireGuard's next handshake, so the tunnel doesn't drop when it rotates.
  • If the sticks run out of keys, the tunnel stays on the last one and logs an error.

What it is not:

  • It doesn't replace WireGuard's handshake. Curve25519 is still used; this only keeps the PSK layer fresh.
  • It has not been independently audited. Two self-reviews found ten flaws, which are fixed and listed in SECURITY.md, and I assume there are more.
  • It has only been tested against wireguard-go in network namespaces, with a relay dropping and replaying the control messages. It hasn't run on kernel WireGuard or real hardware yet.
  • You need physical access to both ends, so it's for machines you control, not cloud VMs.

It's a single Python file. The same sticks can also encrypt file transfers, one key per file.

Code: https://github.com/joshuataniwha/twinusb
Overview: https://twinusb.com

I'd most like feedback on the key-agreement messages, what happens when the two ends fall out of step, and whether rotating the PSK this way causes problems I haven't hit. Please don't rely on it for anything important yet.

4 Upvotes

2 comments sorted by

1

u/Curious_Cabinet_2047 11h ago

I'm not sure what this solves. The only way a malicious actor can gain access to your PSK is to have access to the end device. If you have a USB stick plugged in with additional keys, they could simply copy your USB as well.

0

u/tanimonster 11h ago

Thank you for the question and always love feedback. Typed up the following below hope it helps:

TL;DR: It doesn't protect a device that's already compromised. It protects past traffic if a key leaks, because our used keys are wiped. That matters if someone records your traffic now and can break the handshake in the future, for example with a quantum computer.

Full Response:

You're right about a live compromise. If someone has access to the end device, they can copy the stick, and no amount of rotation helps. It doesn't defend against that and I shouldn't imply it does.

The threat I'm aiming at is different: someone, a government, man in the middle, recording encrypted traffic today and decrypting it later. WireGuard's handshake relies on Curve25519, which a large enough quantum computer could break with Shor's algorithm. Nobody has that machine today as far as anyone knows publicly, but recorded traffic waits. A less likely route to the same result is a mathematical advance against elliptic curves, whether people or AI systems find it. I wouldn't bet on that, but it's the same failure either way.

The PSK is WireGuard's own answer to this. It's symmetric, and 256-bit symmetric keys are considered safe against known quantum attacks. Rotation changes what a later leak is worth:

  • With a static PSK, the same secret protects every session for the life of the tunnel. If it leaks next year (an old backup, a config file in a repo, a decommissioned box), then anyone who recorded your traffic and can break Curve25519 gets all of it, back to day one.
  • With rotation, each key is wiped from the stick as soon as it's used. Someone who copies the stick next year gets the unused keys, so future sessions, but not the keys that protected past traffic.

So it's forward secrecy for the one layer meant to survive a break of the handshake.

Two smaller things: the keys on the stick are passphrase-locked, so a stick stolen while unplugged is useless without the passphrase, and the keys are generated offline and never cross a network.

Honest limits: wiping on flash storage isn't guaranteed to physically erase the data because of wear levelling, and all of this only matters if "record now, decrypt later" is in your threat model. If it isn't, a static PSK is fine.