@totalreclaw/totalreclaw 3.2.3 → 3.3.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/pair-crypto.ts ADDED
@@ -0,0 +1,474 @@
1
+ /**
2
+ * pair-crypto — gateway-side cryptographic primitives for the v3.3.0
3
+ * QR-pairing flow.
4
+ *
5
+ * Cipher suite (per design doc section 3a-3b, ratified 2026-04-20):
6
+ * - ECDH on x25519 for key agreement.
7
+ * - HKDF-SHA256 for symmetric-key derivation from the shared secret.
8
+ * - ChaCha20-Poly1305 AEAD for the ciphertext payload, with the sid
9
+ * bound as associated data (AD = sid UTF-8 bytes).
10
+ *
11
+ * Every primitive is provided by the Node built-in `node:crypto` module
12
+ * on Node 18.19+ and above. NO third-party crypto dependency is added
13
+ * to the plugin for the gateway side. (The BROWSER side of the flow in
14
+ * P2 uses WebCrypto with a `@noble/curves` + `@noble/ciphers` fallback
15
+ * for older Safari — those ship as part of the served pairing page and
16
+ * do NOT affect the plugin's server-side dep tree.)
17
+ *
18
+ * Scope and guarantees
19
+ * --------------------
20
+ * - This module ONLY runs on the gateway host. The device side
21
+ * (browser) implements the mirror of these functions in a separate
22
+ * bundle served by pair-http.ts.
23
+ * - Round-trip correctness: deriveSessionKey on both sides with
24
+ * matching (sk_local, pk_remote) produces the same key.
25
+ * - AD (sid) binding: a ciphertext encrypted for session A CANNOT
26
+ * decrypt under session B's keys, even with identical plaintext
27
+ * and nonce. This is the AEAD contract; validated by tests.
28
+ * - No logging of key material. The only public thing this module
29
+ * emits is base64url-encoded raw public keys (32 bytes → 43 chars),
30
+ * which are safe for the QR payload.
31
+ * - No `fs.*` calls, no `process.env` reads, no network primitives.
32
+ * Keeps scanner surface isolated (see `check-scanner.mjs`).
33
+ *
34
+ * Interoperability with browser WebCrypto
35
+ * ---------------------------------------
36
+ * The WebCrypto x25519 + HKDF + ChaCha20-Poly1305 APIs are bit-for-bit
37
+ * compatible with Node's `crypto` as long as:
38
+ * - Raw 32-byte public/private keys are used (not DER/SPKI).
39
+ * - HKDF parameters are (hash=SHA-256, salt=sid bytes, info fixed
40
+ * ASCII string, length=32 bytes → 256-bit AEAD key).
41
+ * - AEAD uses a 12-byte random nonce + 16-byte tag, AD = sid bytes.
42
+ * See tests for fixed test vectors.
43
+ */
44
+
45
+ import {
46
+ createPrivateKey,
47
+ createPublicKey,
48
+ diffieHellman,
49
+ generateKeyPairSync,
50
+ hkdfSync,
51
+ createCipheriv,
52
+ createDecipheriv,
53
+ randomBytes,
54
+ timingSafeEqual,
55
+ } from 'node:crypto';
56
+
57
+ // ---------------------------------------------------------------------------
58
+ // Constants
59
+ // ---------------------------------------------------------------------------
60
+
61
+ /**
62
+ * HKDF "info" parameter — fixes the domain separation for this protocol.
63
+ * MUST match the browser-side constant in the pair-page bundle (P2).
64
+ * Versioned so we can roll to a new KDF without breaking old ciphertexts.
65
+ */
66
+ export const HKDF_INFO = 'totalreclaw-pair-v1';
67
+
68
+ /** HKDF output length — 32 bytes = 256-bit ChaCha20-Poly1305 key. */
69
+ export const AEAD_KEY_BYTES = 32;
70
+
71
+ /** ChaCha20-Poly1305 nonce length — 12 bytes per RFC 7539. */
72
+ export const AEAD_NONCE_BYTES = 12;
73
+
74
+ /** ChaCha20-Poly1305 auth tag length — 16 bytes standard. */
75
+ export const AEAD_TAG_BYTES = 16;
76
+
77
+ /** Raw x25519 public/private key length — 32 bytes per RFC 7748. */
78
+ export const X25519_KEY_BYTES = 32;
79
+
80
+ // ---------------------------------------------------------------------------
81
+ // Types
82
+ // ---------------------------------------------------------------------------
83
+
84
+ /** Raw 32-byte x25519 public key, base64url-encoded. */
85
+ export type PublicKeyB64 = string;
86
+
87
+ /** Raw 32-byte x25519 private key, base64url-encoded. */
88
+ export type PrivateKeyB64 = string;
89
+
90
+ /** Raw 12-byte AEAD nonce, base64url-encoded. */
91
+ export type NonceB64 = string;
92
+
93
+ /** AEAD ciphertext + appended tag, base64url-encoded. */
94
+ export type CiphertextB64 = string;
95
+
96
+ /** An ephemeral gateway keypair, both halves base64url-encoded. */
97
+ export interface GatewayKeypair {
98
+ skB64: PrivateKeyB64;
99
+ pkB64: PublicKeyB64;
100
+ }
101
+
102
+ /** Fully-derived session keys — caller uses kEnc for AEAD ops. */
103
+ export interface SessionKeys {
104
+ /** 32-byte ChaCha20-Poly1305 key. */
105
+ kEnc: Buffer;
106
+ }
107
+
108
+ /** Inputs to the gateway-side decryption happy path. */
109
+ export interface DecryptInputs {
110
+ /** Gateway's base64url private key (from pair-session-store). */
111
+ skGatewayB64: PrivateKeyB64;
112
+ /** Device's ephemeral public key, base64url. */
113
+ pkDeviceB64: PublicKeyB64;
114
+ /** Session id, used as HKDF salt AND AEAD AD. */
115
+ sid: string;
116
+ /** Base64url nonce (12 bytes). */
117
+ nonceB64: NonceB64;
118
+ /** Base64url ciphertext (plaintext || 16-byte tag). */
119
+ ciphertextB64: CiphertextB64;
120
+ }
121
+
122
+ /**
123
+ * Inputs for the gateway-side encrypt helper. Only used by tests (the
124
+ * actual encrypt side lives in the browser bundle) — exposed because
125
+ * the test vectors need a round-trip, and because future "gateway
126
+ * replies with an encrypted ACK" flows (4.x) could reuse it.
127
+ */
128
+ export interface EncryptInputs {
129
+ /** Gateway's private key (or ephemeral-side's private key). */
130
+ skLocalB64: PrivateKeyB64;
131
+ /** Peer's public key. */
132
+ pkRemoteB64: PublicKeyB64;
133
+ sid: string;
134
+ plaintext: Buffer | Uint8Array;
135
+ /** Override the 12-byte nonce. Used for tests that need deterministic output. */
136
+ nonceB64?: NonceB64;
137
+ }
138
+
139
+ export interface EncryptOutput {
140
+ nonceB64: NonceB64;
141
+ ciphertextB64: CiphertextB64;
142
+ }
143
+
144
+ // ---------------------------------------------------------------------------
145
+ // Key generation / conversion
146
+ // ---------------------------------------------------------------------------
147
+
148
+ /**
149
+ * Generate a fresh ephemeral x25519 keypair for a pairing session.
150
+ *
151
+ * Returns raw 32-byte values base64url-encoded. The caller persists the
152
+ * private half in pair-session-store (under the session record's 0600
153
+ * file) and embeds the public half in the QR URL fragment.
154
+ *
155
+ * The returned keys are raw x25519 — NOT DER/SPKI/PEM. The browser
156
+ * side's WebCrypto needs raw bytes to import.
157
+ */
158
+ export function generateGatewayKeypair(): GatewayKeypair {
159
+ const { publicKey, privateKey } = generateKeyPairSync('x25519');
160
+ return {
161
+ skB64: extractRawPrivate(privateKey),
162
+ pkB64: extractRawPublic(publicKey),
163
+ };
164
+ }
165
+
166
+ /**
167
+ * Re-constitute a Node `KeyObject` from raw base64url public-key bytes.
168
+ * Uses the JWK OKP encoding because Node doesn't accept raw-format
169
+ * inputs to `createPublicKey` directly.
170
+ */
171
+ function publicKeyFromB64(pkB64: PublicKeyB64): ReturnType<typeof createPublicKey> {
172
+ const raw = Buffer.from(pkB64, 'base64url');
173
+ if (raw.length !== X25519_KEY_BYTES) {
174
+ throw new Error(`pair-crypto: public key must be ${X25519_KEY_BYTES} bytes (got ${raw.length})`);
175
+ }
176
+ return createPublicKey({
177
+ key: { kty: 'OKP', crv: 'X25519', x: raw.toString('base64url') },
178
+ format: 'jwk',
179
+ });
180
+ }
181
+
182
+ /**
183
+ * Re-constitute a Node `KeyObject` from raw base64url private-key bytes.
184
+ *
185
+ * JWK OKP private keys require the `x` (public) field too; we derive it
186
+ * by first constructing a temporary KeyObject from `d` alone, then
187
+ * exporting its public half. This is cheap (one scalarmult) and keeps
188
+ * the call sites clean.
189
+ *
190
+ * Mirror of the browser-side WebCrypto `importKey('raw', ...)` path.
191
+ */
192
+ function privateKeyFromB64(skB64: PrivateKeyB64): ReturnType<typeof createPrivateKey> {
193
+ const raw = Buffer.from(skB64, 'base64url');
194
+ if (raw.length !== X25519_KEY_BYTES) {
195
+ throw new Error(`pair-crypto: private key must be ${X25519_KEY_BYTES} bytes (got ${raw.length})`);
196
+ }
197
+
198
+ // The JWK OKP format requires `x` (public) alongside `d` (private).
199
+ // Derive `x` by first constructing a public KeyObject from the scalar
200
+ // via the OKP JWK path — Node accepts `d` alone and returns a usable
201
+ // private KeyObject from which we can derive the public half.
202
+ const tempPriv = createPrivateKey({
203
+ key: { kty: 'OKP', crv: 'X25519', d: raw.toString('base64url'), x: '' },
204
+ format: 'jwk',
205
+ });
206
+ // Derive the public half.
207
+ const pubObj = createPublicKey(tempPriv);
208
+ const pubJwk = pubObj.export({ format: 'jwk' }) as { x: string };
209
+
210
+ // Re-construct with the full (x, d) pair. This is the canonical form
211
+ // the rest of the code holds onto.
212
+ return createPrivateKey({
213
+ key: { kty: 'OKP', crv: 'X25519', d: raw.toString('base64url'), x: pubJwk.x },
214
+ format: 'jwk',
215
+ });
216
+ }
217
+
218
+ /** Extract the raw 32-byte public-key bytes from a Node KeyObject → base64url. */
219
+ function extractRawPublic(pk: ReturnType<typeof createPublicKey>): PublicKeyB64 {
220
+ const jwk = pk.export({ format: 'jwk' }) as { x?: string };
221
+ if (!jwk.x) throw new Error('pair-crypto: public key JWK is missing the x field');
222
+ return jwk.x; // JWK `x` is already base64url-encoded raw bytes.
223
+ }
224
+
225
+ /** Extract the raw 32-byte private-key bytes from a Node KeyObject → base64url. */
226
+ function extractRawPrivate(sk: ReturnType<typeof createPrivateKey>): PrivateKeyB64 {
227
+ const jwk = sk.export({ format: 'jwk' }) as { d?: string };
228
+ if (!jwk.d) throw new Error('pair-crypto: private key JWK is missing the d field');
229
+ return jwk.d;
230
+ }
231
+
232
+ /**
233
+ * Derive the public key from a raw base64url private key. Exposed for
234
+ * tests and for the session-store's self-consistency checks — the
235
+ * default `createPairSession` doesn't call this (it generates both
236
+ * halves at once via `generateGatewayKeypair`).
237
+ */
238
+ export function derivePublicFromPrivate(skB64: PrivateKeyB64): PublicKeyB64 {
239
+ const sk = privateKeyFromB64(skB64);
240
+ const pk = createPublicKey(sk);
241
+ return extractRawPublic(pk);
242
+ }
243
+
244
+ // ---------------------------------------------------------------------------
245
+ // ECDH + HKDF
246
+ // ---------------------------------------------------------------------------
247
+
248
+ /**
249
+ * Perform x25519 ECDH between (local private key, remote public key),
250
+ * producing a 32-byte shared secret.
251
+ *
252
+ * BOTH sides running this with swapped halves MUST produce the same
253
+ * shared secret. This is the foundation of the pairing key agreement.
254
+ *
255
+ * Validation: throws if either key is the wrong byte length, or if
256
+ * the underlying `diffieHellman` call fails (which Node will do for
257
+ * invalid curve points).
258
+ */
259
+ export function computeSharedSecret(opts: {
260
+ skLocalB64: PrivateKeyB64;
261
+ pkRemoteB64: PublicKeyB64;
262
+ }): Buffer {
263
+ const sk = privateKeyFromB64(opts.skLocalB64);
264
+ const pk = publicKeyFromB64(opts.pkRemoteB64);
265
+ const shared = diffieHellman({ privateKey: sk, publicKey: pk });
266
+ if (shared.length !== X25519_KEY_BYTES) {
267
+ throw new Error(
268
+ `pair-crypto: ECDH output wrong length (got ${shared.length}, expected ${X25519_KEY_BYTES})`,
269
+ );
270
+ }
271
+ return shared;
272
+ }
273
+
274
+ /**
275
+ * Derive the AEAD key from a shared secret via HKDF-SHA256. Uses the
276
+ * session id as the salt and the fixed protocol tag as the info.
277
+ *
278
+ * Mathematical sketch (per RFC 5869):
279
+ * PRK = HMAC-SHA256(salt, shared)
280
+ * OKM = HMAC-SHA256(PRK, info || 0x01)[:L]
281
+ *
282
+ * Where L = 32 bytes = AEAD_KEY_BYTES.
283
+ *
284
+ * The sid binding means a ciphertext encrypted for session A is
285
+ * DECRYPTABLE only under session A's derived key. Replaying ct from
286
+ * session A into session B's decrypt path produces a different key →
287
+ * AEAD tag fails → rejected.
288
+ */
289
+ export function deriveSessionKeys(opts: {
290
+ sharedSecret: Buffer;
291
+ sid: string;
292
+ }): SessionKeys {
293
+ if (opts.sharedSecret.length !== X25519_KEY_BYTES) {
294
+ throw new Error('pair-crypto: shared secret must be 32 bytes');
295
+ }
296
+ if (typeof opts.sid !== 'string' || opts.sid.length === 0) {
297
+ throw new Error('pair-crypto: sid is required for HKDF salt binding');
298
+ }
299
+ const salt = Buffer.from(opts.sid, 'utf-8');
300
+ const info = Buffer.from(HKDF_INFO, 'utf-8');
301
+ const okm = hkdfSync('sha256', opts.sharedSecret, salt, info, AEAD_KEY_BYTES);
302
+ return { kEnc: Buffer.from(okm) };
303
+ }
304
+
305
+ /**
306
+ * One-shot convenience: ECDH + HKDF in a single call. Used by both the
307
+ * HTTP respond handler (decrypt side) and the tests.
308
+ */
309
+ export function deriveAeadKeyFromEcdh(opts: {
310
+ skLocalB64: PrivateKeyB64;
311
+ pkRemoteB64: PublicKeyB64;
312
+ sid: string;
313
+ }): SessionKeys {
314
+ const shared = computeSharedSecret({
315
+ skLocalB64: opts.skLocalB64,
316
+ pkRemoteB64: opts.pkRemoteB64,
317
+ });
318
+ return deriveSessionKeys({ sharedSecret: shared, sid: opts.sid });
319
+ }
320
+
321
+ // ---------------------------------------------------------------------------
322
+ // AEAD
323
+ // ---------------------------------------------------------------------------
324
+
325
+ /**
326
+ * Decrypt a ChaCha20-Poly1305 AEAD ciphertext. Returns the plaintext
327
+ * on success; throws if the tag is invalid (which includes both
328
+ * tampering and wrong-key attempts).
329
+ *
330
+ * Ciphertext is expected in the combined form `plaintext || tag`, where
331
+ * tag is the trailing 16 bytes. The caller MUST supply the same
332
+ * (kEnc, nonce, sid-as-AD) used for encryption.
333
+ */
334
+ export function aeadDecrypt(opts: {
335
+ kEnc: Buffer;
336
+ nonceB64: NonceB64;
337
+ sid: string;
338
+ ciphertextB64: CiphertextB64;
339
+ }): Buffer {
340
+ const nonce = Buffer.from(opts.nonceB64, 'base64url');
341
+ if (nonce.length !== AEAD_NONCE_BYTES) {
342
+ throw new Error(`pair-crypto: nonce must be ${AEAD_NONCE_BYTES} bytes (got ${nonce.length})`);
343
+ }
344
+ if (opts.kEnc.length !== AEAD_KEY_BYTES) {
345
+ throw new Error(`pair-crypto: AEAD key must be ${AEAD_KEY_BYTES} bytes`);
346
+ }
347
+ const combined = Buffer.from(opts.ciphertextB64, 'base64url');
348
+ if (combined.length < AEAD_TAG_BYTES) {
349
+ throw new Error('pair-crypto: ciphertext too short to contain tag');
350
+ }
351
+ const ct = combined.subarray(0, combined.length - AEAD_TAG_BYTES);
352
+ const tag = combined.subarray(combined.length - AEAD_TAG_BYTES);
353
+
354
+ const decipher = createDecipheriv('chacha20-poly1305', opts.kEnc, nonce, {
355
+ authTagLength: AEAD_TAG_BYTES,
356
+ });
357
+ decipher.setAAD(Buffer.from(opts.sid, 'utf-8'), { plaintextLength: ct.length });
358
+ decipher.setAuthTag(tag);
359
+
360
+ const pt1 = decipher.update(ct);
361
+ const pt2 = decipher.final();
362
+ return Buffer.concat([pt1, pt2]);
363
+ }
364
+
365
+ /**
366
+ * One-shot decrypt: ECDH + HKDF + AEAD in one call. This is what the
367
+ * HTTP respond handler calls. Returns the plaintext Buffer on success.
368
+ *
369
+ * Throws on ANY failure (wrong key length, invalid curve point, tag
370
+ * mismatch). The caller catches and returns 400 to the device.
371
+ */
372
+ export function decryptPairingPayload(inputs: DecryptInputs): Buffer {
373
+ const { kEnc } = deriveAeadKeyFromEcdh({
374
+ skLocalB64: inputs.skGatewayB64,
375
+ pkRemoteB64: inputs.pkDeviceB64,
376
+ sid: inputs.sid,
377
+ });
378
+ return aeadDecrypt({
379
+ kEnc,
380
+ nonceB64: inputs.nonceB64,
381
+ sid: inputs.sid,
382
+ ciphertextB64: inputs.ciphertextB64,
383
+ });
384
+ }
385
+
386
+ /**
387
+ * Encrypt a plaintext payload. Used by tests; also used by any future
388
+ * gateway-to-device encrypted ACK path.
389
+ *
390
+ * Emits (nonce, ciphertext||tag) both base64url-encoded. If the caller
391
+ * passes an explicit `nonceB64`, it is used verbatim; otherwise a
392
+ * fresh 12-byte random nonce is generated.
393
+ */
394
+ export function aeadEncryptWithSessionKey(opts: {
395
+ kEnc: Buffer;
396
+ sid: string;
397
+ plaintext: Buffer | Uint8Array;
398
+ nonceB64?: NonceB64;
399
+ }): EncryptOutput {
400
+ if (opts.kEnc.length !== AEAD_KEY_BYTES) {
401
+ throw new Error(`pair-crypto: AEAD key must be ${AEAD_KEY_BYTES} bytes`);
402
+ }
403
+ const nonceBuf =
404
+ opts.nonceB64 !== undefined
405
+ ? Buffer.from(opts.nonceB64, 'base64url')
406
+ : randomBytes(AEAD_NONCE_BYTES);
407
+ if (nonceBuf.length !== AEAD_NONCE_BYTES) {
408
+ throw new Error(`pair-crypto: nonce must be ${AEAD_NONCE_BYTES} bytes`);
409
+ }
410
+
411
+ const pt = Buffer.isBuffer(opts.plaintext) ? opts.plaintext : Buffer.from(opts.plaintext);
412
+ const cipher = createCipheriv('chacha20-poly1305', opts.kEnc, nonceBuf, {
413
+ authTagLength: AEAD_TAG_BYTES,
414
+ });
415
+ cipher.setAAD(Buffer.from(opts.sid, 'utf-8'), { plaintextLength: pt.length });
416
+ const ct = Buffer.concat([cipher.update(pt), cipher.final()]);
417
+ const tag = cipher.getAuthTag();
418
+ return {
419
+ nonceB64: nonceBuf.toString('base64url'),
420
+ ciphertextB64: Buffer.concat([ct, tag]).toString('base64url'),
421
+ };
422
+ }
423
+
424
+ /**
425
+ * One-shot encrypt: ECDH + HKDF + AEAD (caller supplies both sides).
426
+ * Used by the test vectors and future 4.x features. The real device
427
+ * side does this in the browser, not here.
428
+ */
429
+ export function encryptPairingPayload(inputs: EncryptInputs): EncryptOutput {
430
+ const { kEnc } = deriveAeadKeyFromEcdh({
431
+ skLocalB64: inputs.skLocalB64,
432
+ pkRemoteB64: inputs.pkRemoteB64,
433
+ sid: inputs.sid,
434
+ });
435
+ return aeadEncryptWithSessionKey({
436
+ kEnc,
437
+ sid: inputs.sid,
438
+ plaintext: inputs.plaintext,
439
+ nonceB64: inputs.nonceB64,
440
+ });
441
+ }
442
+
443
+ // ---------------------------------------------------------------------------
444
+ // Constant-time secondary-code comparison
445
+ // ---------------------------------------------------------------------------
446
+
447
+ /**
448
+ * Constant-time compare two 6-digit numeric strings. Used by the HTTP
449
+ * start/respond handlers to check the user-supplied secondary code
450
+ * against the session's expected code.
451
+ *
452
+ * Returns true on match, false otherwise. Length mismatch returns
453
+ * false without short-circuit leak of which side was shorter.
454
+ *
455
+ * Uses Node `timingSafeEqual`, which requires equal-length inputs.
456
+ * We pad the SHORTER input with NULs and always compare a fixed
457
+ * 6-byte window — the length check happens BEFORE the actual compare
458
+ * and uses boolean AND at the end so both branches run to completion.
459
+ */
460
+ export function compareSecondaryCodesCT(a: string, b: string): boolean {
461
+ const aBuf = Buffer.from(a, 'utf-8');
462
+ const bBuf = Buffer.from(b, 'utf-8');
463
+ const lenMatch = aBuf.length === bBuf.length;
464
+ // Always compare a constant window so the total work is independent
465
+ // of the actual input lengths. Pad the shorter to the longer with
466
+ // zero bytes; if lengths diverge we force lenMatch=false below.
467
+ const max = Math.max(aBuf.length, bBuf.length, 6);
468
+ const aPad = Buffer.alloc(max);
469
+ const bPad = Buffer.alloc(max);
470
+ aBuf.copy(aPad);
471
+ bBuf.copy(bPad);
472
+ const byteMatch = timingSafeEqual(aPad, bPad);
473
+ return lenMatch && byteMatch;
474
+ }