Clipboard Share (https://www.clipboardapp.org), an online clipboard with no login, now has "rooms". Files and text you exchange in a room are encrypted on your device, file names included, and the room key is never sent to the server. The server only holds ciphertext.
Up to here, browser Web Crypto gets you there quickly. The hard part came next: how do you move the room key to a new device?
Three ways to move the key
- Same Wi-Fi: press "Connect in a room" on a device card. The key is wrapped with a one-time ECDH (P-256) key and handed over. Both screens must show the same 4 pictures, which you check by eye to block a man in the middle.
-
QR code or invite link: the key goes after the
#in the URL. Everything after#is not included in the request to the server. The app strips it from the address bar before any analytics script runs. - Then there was the hard case: PC to PC, or phone to a PC with no camera. You can't scan a QR code, and nobody wants to retype a long link by hand.
So I copied the join code idea from Minecraft Education: a device already in the room shows 4 pictures, and the new device taps those 4 pictures, in order, out of 24. No memorizing, just look and tap.
4 pictures is too short for a password
Pick 4 out of 24 in order (repeats allowed) and you get 24⁴ = 331,776 possibilities. If you derived a key straight from that, anyone holding a single ciphertext could try all 330,000 in about a second.
Having the server ask "is this picture code right?" doesn't work either, because then the server learns the pictures, and could intercept the key handoff.
The properties I needed:
- Only two devices that both know the short secret (4 pictures) end up with the same key.
- Eavesdropping on the messages gives you nothing to brute-force offline.
- An attacker can test only one guess per attempt.
That is a PAKE (Password-Authenticated Key Exchange). I picked SPAKE2 (RFC 9382), because the spec is short and standardized as an RFC.
SPAKE2 in a nutshell
On curve P-256, each side picks a random number, x and y. They mix the password w with public constant points M and N and send the result.
- Joining side (A):
pA = x·G + w·M - Room side (B):
pB = y·G + w·N
The receiver subtracts the other side's blinding term w·M (or w·N) and multiplies by its own secret. If w matches, both land on K = x·y·G. If w differs even slightly, they get unrelated points, the confirmation MAC fails, and the exchange ends immediately. Someone who only sees pA and pB has no way to try candidate values of w one by one.
The implementation follows the RFC step by step on top of the P-256 point operations in @noble/curves (MIT). Hashing, HKDF and HMAC use browser Web Crypto. The module is loaded with a dynamic import only when a picture code is used, so it is not in the first-screen bundle.
// frontend/src/utils/spake2.js (excerpt)
const ownPoint = Point.BASE.multiply(mine).add(mulOrZero(myBlind, w))
...
const diff = peerPoint.subtract(mulOrZero(peerBlind, w))
if (diff.is0()) throw new Error('SPAKE2_BAD_POINT')
const K = diff.multiply(mine) // h = 1 (P-256 has cofactor 1)
A received point is rejected if it is not on the curve, if it is the identity, or if it becomes the identity after the subtraction. There are separate tests for this.
4 pictures → w
// w = SHA-256("cs-code-v1" || 4 picture-index bytes || sessionId) mod n
export async function derivePassword(code, sessionId) { ... }
The RFC says to stretch the password with a slow hash (MHF). I deliberately didn't. This code can only be tried online: the server counts wrong attempts and blocks them, the code changes within 5 minutes, and every join attempt has a different sessionId, so nothing can be precomputed. Offline brute force is already ruled out by SPAKE2 itself.
The server only knows "which room"
The server has to find the room and connect the two devices, so it needs to be told something. Only the first 2 of the 4 pictures are sent (routeOf). The server uses them to find the room and relay messages, and it can't decrypt the contents.
// frontend/src/utils/pictureCode.js
export function routeOf(code) { return [code[0], code[1]] }
The first two pictures give only 24 × 24 = 576 combinations. So two codes open at the same time must not share the same first two. If they collide, the server returns ROUTE_TAKEN and the room-side device reopens with new pictures (up to 12 times). The server accepts at most 500 codes open at once.
Here is the cost of this design. Since the server knows the first two, a server that deliberately sat in the middle would have to guess the last two. That is 1 in 576 per attempt, and a wrong guess breaks the confirmation MAC, so the failed join becomes visible.
Four messages
| Step | Direction | Content |
|---|---|---|
| A1 | new device → room | pA |
| B1 | room → new device |
pB + room-side confirmation MAC |
| A2 | new device → room | new-device confirmation MAC |
| P | room → new device |
{room key, entry ticket, room name} wrapped with AES-GCM |
A successful key exchange doesn't let anyone in by itself. After A2 passes, the room-side screen shows "Sunny Otter wants to join" with [Decline] [Let in], and P is sent only after a person taps Let in. Even with the right pictures, a device the room's owner doesn't recognize can't get in.
Rate limiting has two layers. Each device gets a warning after 3 wrong tries and has to wait 3 minutes after 5. Each IP is limited to 30 wrong tries per 10 minutes. With many people behind one router, such as a school, one student's mistakes shouldn't lock out the whole class, so the per-IP limit is set separately and higher.
I matched the RFC test vectors first
The scariest state when writing crypto yourself is "it works, but it isn't the spec." So I put in the 4 sets of P-256 test vectors from RFC 9382 Appendix B as they are, and made the test pass first: pA, pB, K, TT, Ke, Ka, KcA, KcB and the confirmation MAC all have to match byte for byte.
it(`vector ${i + 1} (A='${v.idA}', B='${v.idB}'): pA, pB, K, TT, Hash(TT), Ke, Ka, KcA, KcB, confirmation MAC match`, ...)
Other tests:
- 20 round trips with the same code always succeed, and changing even one picture always fails.
- A different sessionId fails even with the same pictures.
- Invalid points are rejected.
I built this feature with Claude Code. I checked the test vectors line by line against the RFC text myself.
What's still lacking
- It's a web app. Browser-side encryption only holds if you trust the JavaScript the site serves you. That differs from end-to-end encryption in an installed app. The code is open source under MIT.
- What the server sees. File size, upload time, the device name tag (animal, device, browser) and the IP address.
- The default tabs are different. The default tabs, "Same network" and "Everyone", are encrypted only if you turn on "Lock with a password". Outside a room it is not automatic.
- No way to remove a device. There is no feature yet to kick a device out of a room, which is why it asks once more before letting a device in.
- Not enough real-device testing. I still need to test more on a real iPhone with Safari.
Try it at https://www.clipboardapp.org and I'd appreciate your feedback. You can also send it through "Send feedback" at the bottom of the site.
Originally published in Korean on velog.
Top comments (0)