Repository navigation
Expand file tree
/
Copy pathe2e.ts
More file actions
195 lines (180 loc) · 7.85 KB
/
Copy pathe2e.ts
File metadata and controls
195 lines (180 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* End-to-end encryption for the phone↔server channel.
*
* When the transport is a public tunnel (Cloudflare), TLS terminates at the
* vendor's edge, so the vendor would see the plaintext. Since `pane.send_text`
* is arbitrary shell execution, a relay that can read or inject into this
* channel is a remote-code-execution surface. So the payload is encrypted here,
* above the transport, with a key only the phone and the server hold — the
* tunnel only ever brokers ciphertext. See `docs/connectivity.md`.
*
* The construction, chosen to be small and auditable rather than clever:
*
* - A **pairing secret** (32 random bytes, carried in the QR) is the only
* long-lived shared value.
* - Each connection does an **ephemeral X25519** handshake, and the pairing
* secret is mixed into the key derivation (HKDF ikm = ECDH‖secret). That mix
* is what authenticates the exchange: a man in the middle without the pairing
* secret derives a different key and every decrypt fails. Because the DH keys
* are ephemeral, a pairing secret that leaks *later* does not expose past
* sessions — forward secrecy.
* - Messages are sealed with **ChaCha20-Poly1305**, a separate key per
* direction, and a per-message counter as the nonce. Different key per
* direction plus a monotonic counter means a nonce is never reused; the
* counter travels with the message and a receiver rejects any counter it has
* already passed, so a replayed frame is refused.
*
* This module is deliberately platform-neutral: it imports only pure-JS crypto
* and takes randomness as a parameter, so the same code runs in the Bun server
* and the React Native app. Neither `node:` nor `bun:` nor `expo-*` appears
* here; the caller supplies 32 random bytes for the ephemeral key.
*/
import { x25519 } from "@noble/curves/ed25519.js";
import { hkdf } from "@noble/hashes/hkdf.js";
import { sha256 } from "@noble/hashes/sha2.js";
import { chacha20poly1305 } from "@noble/ciphers/chacha.js";
const HKDF_INFO = new TextEncoder().encode("shahi/e2e/v1");
const KEY_LEN = 32;
export const PAIRING_SECRET_LEN = 32;
export const PUBLIC_KEY_LEN = 32;
/** One direction's key and its running nonce counter. */
interface Channel {
key: Uint8Array;
next: bigint;
}
/**
* A live encrypted session: the keys derived for this connection, plus the send
* and receive counters. Mutable — `seal`/`open` advance the counters.
*/
export interface Session {
send: Channel;
recv: Channel;
}
/** An ephemeral keypair. `priv` never leaves the device; `pub` is sent once. */
export interface Ephemeral {
priv: Uint8Array;
pub: Uint8Array;
}
/**
* Derives an ephemeral keypair from 32 caller-supplied random bytes.
*
* Randomness is injected rather than read from a global so this module stays
* platform-neutral: the server passes `crypto.getRandomValues`, the app passes
* `expo-crypto`'s bytes. Reusing a private key across connections would sacrifice
* forward secrecy, so callers must pass fresh randomness each time.
*/
export function ephemeral(random32: Uint8Array): Ephemeral {
if (random32.length < 32) throw new Error("e2e: need 32 random bytes for an ephemeral key");
const priv = random32.slice(0, 32);
return { priv, pub: x25519.getPublicKey(priv) };
}
function deriveMaster(
shared: Uint8Array,
pairingSecret: Uint8Array,
clientPub: Uint8Array,
serverPub: Uint8Array,
): { c2s: Uint8Array; s2c: Uint8Array } {
// ikm binds the pairing secret to the ECDH result — this is the authentication
// step, and it only authenticates if the secret is actually there: with an
// empty one the handshake is unauthenticated Diffie–Hellman and nothing
// would say so (2026-09-02 review, E3). Fixed length also keeps
// `ECDH ‖ secret` unambiguous.
if (pairingSecret.length !== PAIRING_SECRET_LEN) {
throw new Error(`e2e: the pairing secret must be ${PAIRING_SECRET_LEN} bytes`);
}
// salt binds both public keys so the derived keys are unique to this exact
// exchange.
const ikm = concat(shared, pairingSecret);
const salt = concat(clientPub, serverPub);
const master = hkdf(sha256, ikm, salt, HKDF_INFO, 2 * KEY_LEN);
return { c2s: master.slice(0, KEY_LEN), s2c: master.slice(KEY_LEN, 2 * KEY_LEN) };
}
/** The phone's side of the handshake, given its ephemeral key and the server's public key. */
export function clientSession(
self: Ephemeral,
serverPub: Uint8Array,
pairingSecret: Uint8Array,
): Session {
const shared = x25519.getSharedSecret(self.priv, serverPub);
const { c2s, s2c } = deriveMaster(shared, pairingSecret, self.pub, serverPub);
return { send: { key: c2s, next: 0n }, recv: { key: s2c, next: 0n } };
}
/** The server's side of the handshake, given its ephemeral key and the phone's public key. */
export function serverSession(
self: Ephemeral,
clientPub: Uint8Array,
pairingSecret: Uint8Array,
): Session {
const shared = x25519.getSharedSecret(self.priv, clientPub);
const { c2s, s2c } = deriveMaster(shared, pairingSecret, clientPub, self.pub);
return { send: { key: s2c, next: 0n }, recv: { key: c2s, next: 0n } };
}
/**
* Seals a message for transmission. Wire form is `counter(8) ‖ ciphertext+tag`.
* The counter is the nonce and advances every call, so a nonce is never reused
* on one key.
*/
export function seal(session: Session, plaintext: Uint8Array): Uint8Array {
const counter = session.send.next;
session.send.next = counter + 1n;
const nonce = nonceFor(counter);
const ct = chacha20poly1305(session.send.key, nonce).encrypt(plaintext);
return concat(counterBytes(counter), ct);
}
/**
* Opens a received message, throwing on a bad tag (tamper) or on any counter
* but the next one. On success the receive counter advances past the accepted
* message.
*
* Exactly the next counter, not "any later one": a session is one ordered
* WebSocket link per direction, sealed at the moment of sending, so a gap can
* only be a frame the relay dropped or reordered — and a blind relay that can
* silently withhold a `status`, an `unwatch` or a refusal is not blind enough
* (2026-09-02 review, E1). Refusing the frame after a gap ends the link, and
* a link that reconnects is the honest recovery. The counter is the nonce, so
* no additional authenticated data is needed to bind a frame to its place:
* a frame opens only at its own counter, and only on this direction's key.
*/
export function open(session: Session, wire: Uint8Array): Uint8Array {
if (wire.length < 8 + 16) throw new Error("e2e: message too short");
const counter = readCounter(wire);
if (counter !== session.recv.next) {
throw new Error(
counter < session.recv.next
? "e2e: replayed or out-of-order message"
: "e2e: a message is missing — the link dropped or reordered a frame",
);
}
const nonce = nonceFor(counter);
const plaintext = chacha20poly1305(session.recv.key, nonce).decrypt(wire.slice(8));
session.recv.next = counter + 1n;
return plaintext;
}
/* -------------------------------------------------------------------------- */
function concat(...parts: Uint8Array[]): Uint8Array {
const total = parts.reduce((n, p) => n + p.length, 0);
const out = new Uint8Array(total);
let at = 0;
for (const p of parts) {
out.set(p, at);
at += p.length;
}
return out;
}
// 12-byte ChaCha20-Poly1305 nonce: 4 zero bytes then the 8-byte big-endian
// counter. The high 4 bytes stay zero; a session that sent 2^64 messages has
// bigger problems.
function nonceFor(counter: bigint): Uint8Array {
const nonce = new Uint8Array(12);
const view = new DataView(nonce.buffer);
view.setBigUint64(4, counter, false);
return nonce;
}
function counterBytes(counter: bigint): Uint8Array {
const out = new Uint8Array(8);
new DataView(out.buffer).setBigUint64(0, counter, false);
return out;
}
function readCounter(wire: Uint8Array): bigint {
return new DataView(wire.buffer, wire.byteOffset, 8).getBigUint64(0, false);
}