e2e

package
v0.3.29 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package e2e end-to-end encrypts one terminal socket at a time between a paired browser and the desktop it is paired with, so that the relay carrying it -- shared or run by a customer alike -- can never read what crosses it, the same guarantee push notifications already have. It is a byte-for-byte port of flockdeck-relay's own internal/e2e package, which is the reference implementation of the scheme: what this package does is exactly what a browser (WebCrypto) and this desktop have to do to interoperate.

Why this is a copy, not an import

flockdeck-relay's internal/e2e lives at github.com/jmwri/flockdeck-relay/internal/e2e, in a different module from this one (github.com/jmwri/flockdeck). Go's internal-import rule only allows a package rooted at ".../internal/x" to be imported by code whose own import path shares everything before "internal" -- which, across two separate modules and repositories, this desktop's code never can. There is no way to depend on that package from here, however the module graph is arranged, so this file exists instead: the same algorithm, the same constants, the same wire format, kept in step by hand. Anyone changing one must change the other identically, and the tests in e2e_test.go -- ported the same way, from flockdeck-relay's internal/e2e/e2e_test.go -- are what catches a copy that has drifted.

Keys

Every device (a paired browser) and every host (a desktop, this one included) holds one long-term P-256 key pair, generated locally with GenerateStaticKey. The private half never leaves the machine that made it -- not in memory, not on disk, and never over the wire. The public half is registered with the relay once (POST /api/v1/device/e2e-key or /api/v1/host/e2e-key) and from then on travels exactly where a device's or a host's name already travels: in AccountView, to whichever of the account's browsers or desktops asks. The relay's part in all of this is exactly its part in push notifications -- a switchboard for public keys, never a party that could compute a shared secret from them, because it never holds a private one.

Handshake

A browser opening a terminal runs a fresh handshake over that socket before anything is typed: StartDeviceHandshake makes an ephemeral key pair, good for this one socket alone, and sends its public half as the first frame (the "hello"). The desktop reads it, and RespondHostHandshake makes its own ephemeral pair and answers with its public half (the "response"). Both sides then hold four public keys between them -- the device's static and ephemeral, the host's static and ephemeral -- and each holds two of the matching private keys, which is enough for each to compute the same three Diffie-Hellman terms the other computes from its own two:

T1 = ECDH(device ephemeral, host static)
T2 = ECDH(device static,    host ephemeral)
T3 = ECDH(device ephemeral, host ephemeral)

T3 alone would be an ordinary anonymous ECDH handshake: secret against eavesdropping, but not proof of who is on the other end, since the relay forwards the ephemeral keys and could in principle have substituted its own. T1 and T2 fold in the two sides' long-term keys, so deriving the right session keys from all three terms together needs the device's static private key and the host's static private key -- exactly the two the relay has never had. That makes the handshake mutually authenticated against a relay that merely stores and forwards keys and frames as written, though, as with any scheme whose only root of trust is a key registered with a server, it does not defend against a relay that actively swaps a static public key for its own the moment it is registered; closing that gap needs an out-of-band check -- comparing a fingerprint on both screens -- which Fingerprint, below, computes from the same two static keys the handshake already trusts the relay to hand out.

Fingerprint

Fingerprint(devicePub, hostPub) reduces both static public keys to six five-digit groups, such as "04217 91002 55810 30021 88452 71390": a SHA-256 of the two keys concatenated, device first, cut into six 5-byte chunks, each read as a big-endian integer and taken modulo 100000. A browser and a desktop that hold the keys each believes the other's to be compute the same thirty digits from them alone -- no session, no handshake, nothing that has to have happened yet -- so the two can be compared once, at pairing, or at any later moment either side is unsure, without opening a terminal first. A relay that swapped a static key at registration is holding a different key than the genuine device or host now has, on at least one side of the pair, and that shows up as two different fingerprints on the two screens: the one part of this scheme a person, not a computation, has to check, because a relay that lies about a key can just as well have this package compute an agreeing answer from the lie. Thirty decimal digits is a search space -- 10^30 -- no attacker picking a substitute key gets to brute-force into matching the genuine one's fingerprint, which is what makes comparing thirty digits as good as comparing the keys themselves byte for byte.

T1, T2 and T3 are concatenated and fed to HKDF-SHA256 (RFC 5869), salted with both ephemeral public keys so the extract is bound to this exact exchange, to make two independent AES-256-GCM keys -- one for each direction, so the two ends never encrypt with the same key -- and two 4-byte nonce prefixes to go with them. Losing an ephemeral private key later (a browser tab closed, a desktop restarted) reveals nothing about a past session: T3, and with it the whole derived secret, cannot be recomputed without it. That is this scheme's forward secrecy.

Frames

Once a Session is derived, every message that follows on the same socket -- one terminal keystroke, one chunk of output -- is one call to Seal on the way out and Open on the way in. A frame is a version byte, an 8-byte big-endian counter and an AES-256-GCM-sealed body; the version and which of the two sides sent it are folded into the AEAD's associated data, and the counter must strictly increase by exactly one each time, which an ordered, reliable WebSocket -- what every terminal socket already is -- never gives a legitimate frame reason to violate. A relay that dropped, reordered, duplicated or altered a single byte of a frame would only make Open refuse it; there is nothing in the scheme it could change instead.

What the relay does with any of this

Nothing. The relay never imports this package (nor could it import this copy of it, being in the other module). It stores and hands out public keys exactly as it already does with a device's push subscription keys, and it carries a terminal socket's bytes exactly as it already carries every other stream through the tunnel -- opaque past the outer framing smux and WebSocket need to deliver them at all. A relay that held every byte this package ever sent or received could not read a single terminal keystroke without one of the two static private keys, which it was never given.

Index

Constants

View Source
const Version byte = 1

Version is the wire version of every handshake message and frame this package makes. It travels in a data frame's first byte and in the AEAD's associated data, so a frame made under a later or earlier version of this scheme is refused rather than misread as this one's.

Variables

This section is empty.

Functions

func DecodePublicKey

func DecodePublicKey(s string) (*ecdh.PublicKey, error)

DecodePublicKey reads a public key as EncodePublicKey writes it, refusing anything that is not base64url or not a point on the curve -- which is all the relay itself ever needs to check of a key it is asked to store, having no way to know anything more about whether it is genuinely the sender's.

func EncodePublicKey

func EncodePublicKey(pub *ecdh.PublicKey) string

EncodePublicKey is how a public key is given to the relay to store, and how the relay gives one back: unpadded base64url of the uncompressed P-256 point (65 bytes), the same encoding flockdeck-relay's push.go already uses for a subscription's p256dh.

func Fingerprint added in v0.3.29

func Fingerprint(devicePub, hostPub *ecdh.PublicKey) string

Fingerprint reduces a device's and a host's long-term public keys to a short code a person can compare on two screens: the out-of-band check the handshake's own mutual authentication cannot make for itself against a relay that swapped a static key the moment it was registered (see this package's own doc for what it closes and why thirty digits is enough).

Both ends of a pairing compute the same code from the same two keys regardless of which one calls it -- devicePub and hostPub always mean the device's and the host's static keys, never "mine" and "theirs" -- so it is devicePub first, hostPub second, on both sides, every time.

func GenerateStaticKey

func GenerateStaticKey() (*ecdh.PrivateKey, error)

GenerateStaticKey makes a fresh long-term P-256 key pair: what a device or a host generates once, keeps the private half of forever (until it chooses to rotate), and registers the public half of with the relay.

Types

type DeviceHandshake

type DeviceHandshake struct {
	// contains filtered or unexported fields
}

DeviceHandshake is a browser's side of opening one terminal session, from the moment it makes its ephemeral key until the desktop's response completes it.

func StartDeviceHandshake

func StartDeviceHandshake(deviceStaticPriv *ecdh.PrivateKey, hostStaticPub *ecdh.PublicKey) (*DeviceHandshake, []byte, error)

StartDeviceHandshake begins a browser's side of a fresh terminal session with the desktop whose long-term public key is hostStaticPub -- read from the relay's own account view, where every desktop's key already travels. It returns the hello message to send as the terminal socket's very first frame, ahead of anything a person types or the desktop sends back.

func (*DeviceHandshake) Finish

func (h *DeviceHandshake) Finish(response []byte) (*Session, error)

Finish completes a browser's handshake with the desktop's response -- its ephemeral public key, exactly as StartDeviceHandshake's hello was -- and returns the session the two now independently share.

type Role

type Role byte

Role says which end of a session a Session is, which decides which of its two directional keys it seals with and which it opens with, and which side's long-term and ephemeral keys take which position in the handshake both ends must compute identically.

const (
	RoleDevice Role = 0
	RoleHost   Role = 1
)

The two roles. Their numeric value is part of the wire format -- it goes into the AEAD's associated data as the sender's role -- and so is fixed.

func (Role) String

func (r Role) String() string

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is one terminal socket's derived keys: two AES-256-GCM ciphers, one for each direction, each with its own nonce prefix and its own frame counter. It is good for the life of the socket it was made for alone -- nothing in this package ever persists one -- and is not safe for concurrent use: a socket's reader calls Open and its writer calls Seal, each from one goroutine of its own, exactly as a typical connection's read and write sides already run.

func RespondHostHandshake

func RespondHostHandshake(hostStaticPriv *ecdh.PrivateKey, deviceStaticPub *ecdh.PublicKey, hello []byte) (*Session, []byte, error)

RespondHostHandshake is a desktop's whole side of the handshake in one call: given the browser's hello and the long-term public key the relay says that device holds (fetched from /api/v1/host/devices and matched to this socket by the Flockdeck-Remote-Device header the relay already sets, which names the device without the device having to say so itself), it returns the session and the response to send back as the terminal socket's second frame.

func (*Session) Open

func (s *Session) Open(frame []byte) ([]byte, error)

Open authenticates and decrypts a frame Seal made of this session's incoming direction. It refuses one of the wrong wire version, one whose counter is not exactly the next expected -- an old frame replayed, or one out of order, which the ordered, reliable socket a terminal already runs on gives a genuine frame no way to arrive as -- or one that does not authenticate at all, which is any tampering with it, the relay's included: nothing this package ever gives the relay lets it produce a frame that passes this check.

func (*Session) Seal

func (s *Session) Seal(plaintext []byte) []byte

Seal encrypts plaintext as the next frame of this session's outgoing direction: a wire version byte, an 8-byte big-endian counter -- 0 for the first frame after the handshake, then strictly one more each call -- and the AEAD's output, ciphertext followed by its 16-byte tag. The counter is never reused for the life of a Session; at one frame a nanosecond it would take longer than the age of the universe to repeat, which is the only way two frames could ever share a nonce.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL