@totalreclaw/totalreclaw 3.3.1-rc.2 → 3.3.1-rc.21
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/CHANGELOG.md +330 -0
- package/SKILL.md +50 -83
- package/api-client.ts +18 -11
- package/config.ts +117 -3
- package/crypto.ts +10 -2
- package/dist/api-client.js +226 -0
- package/dist/billing-cache.js +100 -0
- package/dist/claims-helper.js +606 -0
- package/dist/config.js +280 -0
- package/dist/consolidation.js +258 -0
- package/dist/contradiction-sync.js +1034 -0
- package/dist/crypto.js +138 -0
- package/dist/digest-sync.js +361 -0
- package/dist/download-ux.js +63 -0
- package/dist/embedding.js +86 -0
- package/dist/extractor.js +1225 -0
- package/dist/first-run.js +103 -0
- package/dist/fs-helpers.js +563 -0
- package/dist/gateway-url.js +197 -0
- package/dist/generate-mnemonic.js +13 -0
- package/dist/hot-cache-wrapper.js +101 -0
- package/dist/import-adapters/base-adapter.js +64 -0
- package/dist/import-adapters/chatgpt-adapter.js +238 -0
- package/dist/import-adapters/claude-adapter.js +114 -0
- package/dist/import-adapters/gemini-adapter.js +201 -0
- package/dist/import-adapters/index.js +26 -0
- package/dist/import-adapters/mcp-memory-adapter.js +219 -0
- package/dist/import-adapters/mem0-adapter.js +158 -0
- package/dist/import-adapters/types.js +1 -0
- package/dist/index.js +5348 -0
- package/dist/llm-client.js +686 -0
- package/dist/llm-profile-reader.js +346 -0
- package/dist/lsh.js +62 -0
- package/dist/onboarding-cli.js +750 -0
- package/dist/pair-cli.js +344 -0
- package/dist/pair-crypto.js +359 -0
- package/dist/pair-http.js +404 -0
- package/dist/pair-page.js +826 -0
- package/dist/pair-qr.js +107 -0
- package/dist/pair-remote-client.js +410 -0
- package/dist/pair-session-store.js +566 -0
- package/dist/pin.js +542 -0
- package/dist/qa-bug-report.js +301 -0
- package/dist/relay-headers.js +44 -0
- package/dist/reranker.js +442 -0
- package/dist/retype-setscope.js +348 -0
- package/dist/semantic-dedup.js +75 -0
- package/dist/subgraph-search.js +289 -0
- package/dist/subgraph-store.js +694 -0
- package/dist/tool-gating.js +58 -0
- package/download-ux.ts +91 -0
- package/embedding.ts +32 -9
- package/fs-helpers.ts +124 -0
- package/gateway-url.ts +57 -9
- package/index.ts +586 -357
- package/llm-client.ts +211 -23
- package/lsh.ts +7 -2
- package/onboarding-cli.ts +114 -1
- package/package.json +19 -5
- package/pair-cli.ts +76 -8
- package/pair-crypto.ts +34 -24
- package/pair-page.ts +28 -17
- package/pair-qr.ts +152 -0
- package/pair-remote-client.ts +540 -0
- package/qa-bug-report.ts +381 -0
- package/relay-headers.ts +50 -0
- package/reranker.ts +73 -0
- package/retype-setscope.ts +12 -0
- package/subgraph-search.ts +4 -3
- package/subgraph-store.ts +109 -16
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pair-crypto — gateway-side cryptographic primitives for the v3.3.x
|
|
3
|
+
* relay-brokered pair flow.
|
|
4
|
+
*
|
|
5
|
+
* Cipher suite (design doc 3a-3b, cipher swap ratified 2026-04-23 / rc.12):
|
|
6
|
+
* - ECDH on x25519 for key agreement.
|
|
7
|
+
* - HKDF-SHA256 for symmetric-key derivation from the shared secret.
|
|
8
|
+
* - AES-256-GCM AEAD for the ciphertext payload, with the sid bound as
|
|
9
|
+
* associated data (AD = sid UTF-8 bytes, 12-byte nonce, 16-byte tag).
|
|
10
|
+
*
|
|
11
|
+
* rc.4..rc.11 used ChaCha20-Poly1305, but the Web Crypto API does NOT
|
|
12
|
+
* implement ChaCha20-Poly1305 in Chrome / Safari / Edge. The pair-page
|
|
13
|
+
* submit path silently threw `Algorithm: Unrecognized name` before
|
|
14
|
+
* reaching the network. rc.12 swaps the cipher suite to AES-256-GCM
|
|
15
|
+
* (universally supported in WebCrypto) and bumps HKDF_INFO to v2 so
|
|
16
|
+
* cross-version mis-pairs fail closed rather than garble.
|
|
17
|
+
*
|
|
18
|
+
* Every primitive is provided by the Node built-in `node:crypto` module
|
|
19
|
+
* on Node 18.19+ and above. NO third-party crypto dependency is added
|
|
20
|
+
* to the plugin for the gateway side. (The BROWSER side of the flow uses
|
|
21
|
+
* WebCrypto's AES-GCM directly — no shim needed.)
|
|
22
|
+
*
|
|
23
|
+
* Scope and guarantees
|
|
24
|
+
* --------------------
|
|
25
|
+
* - This module ONLY runs on the gateway host. The device side
|
|
26
|
+
* (browser) implements the mirror of these functions in a separate
|
|
27
|
+
* bundle served by pair-http.ts.
|
|
28
|
+
* - Round-trip correctness: deriveSessionKey on both sides with
|
|
29
|
+
* matching (sk_local, pk_remote) produces the same key.
|
|
30
|
+
* - AD (sid) binding: a ciphertext encrypted for session A CANNOT
|
|
31
|
+
* decrypt under session B's keys, even with identical plaintext
|
|
32
|
+
* and nonce. This is the AEAD contract; validated by tests.
|
|
33
|
+
* - No logging of key material. The only public thing this module
|
|
34
|
+
* emits is base64url-encoded raw public keys (32 bytes → 43 chars),
|
|
35
|
+
* which are safe for the QR payload.
|
|
36
|
+
* - No `fs.*` calls, no `process.env` reads, no network primitives.
|
|
37
|
+
* Keeps scanner surface isolated (see `check-scanner.mjs`).
|
|
38
|
+
*
|
|
39
|
+
* Interoperability with browser WebCrypto
|
|
40
|
+
* ---------------------------------------
|
|
41
|
+
* The WebCrypto x25519 + HKDF + AES-GCM APIs are bit-for-bit compatible
|
|
42
|
+
* with Node's `crypto` as long as:
|
|
43
|
+
* - Raw 32-byte public/private keys are used (not DER/SPKI).
|
|
44
|
+
* - HKDF parameters are (hash=SHA-256, salt=sid bytes, info fixed
|
|
45
|
+
* ASCII string "totalreclaw-pair-v2", length=32 bytes).
|
|
46
|
+
* - AEAD uses a 12-byte random nonce + 16-byte tag, AD = sid bytes.
|
|
47
|
+
* See tests for fixed test vectors.
|
|
48
|
+
*/
|
|
49
|
+
import { createPrivateKey, createPublicKey, diffieHellman, generateKeyPairSync, hkdfSync, createCipheriv, createDecipheriv, randomBytes, timingSafeEqual, } from 'node:crypto';
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// Constants
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
/**
|
|
54
|
+
* HKDF "info" parameter — fixes the domain separation for this protocol.
|
|
55
|
+
* MUST match the browser-side constant in the pair-page bundle + the
|
|
56
|
+
* relay-served pair-html page.
|
|
57
|
+
*
|
|
58
|
+
* Versioned so we can roll to a new KDF or cipher suite without silently
|
|
59
|
+
* producing garbage with old ciphertexts. rc.12: bumped from v1 to v2
|
|
60
|
+
* after cipher-suite swap from ChaCha20-Poly1305 → AES-256-GCM (see
|
|
61
|
+
* module header comment for context).
|
|
62
|
+
*/
|
|
63
|
+
export const HKDF_INFO = 'totalreclaw-pair-v2';
|
|
64
|
+
/** HKDF output length — 32 bytes = 256-bit AES-256-GCM key. */
|
|
65
|
+
export const AEAD_KEY_BYTES = 32;
|
|
66
|
+
/** AES-GCM nonce length — 12 bytes (SP 800-38D recommendation). */
|
|
67
|
+
export const AEAD_NONCE_BYTES = 12;
|
|
68
|
+
/** AES-GCM auth tag length — 16 bytes (128 bits, standard). */
|
|
69
|
+
export const AEAD_TAG_BYTES = 16;
|
|
70
|
+
/** Raw x25519 public/private key length — 32 bytes per RFC 7748. */
|
|
71
|
+
export const X25519_KEY_BYTES = 32;
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
// Key generation / conversion
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
/**
|
|
76
|
+
* Generate a fresh ephemeral x25519 keypair for a pairing session.
|
|
77
|
+
*
|
|
78
|
+
* Returns raw 32-byte values base64url-encoded. The caller persists the
|
|
79
|
+
* private half in pair-session-store (under the session record's 0600
|
|
80
|
+
* file) and embeds the public half in the QR URL fragment.
|
|
81
|
+
*
|
|
82
|
+
* The returned keys are raw x25519 — NOT DER/SPKI/PEM. The browser
|
|
83
|
+
* side's WebCrypto needs raw bytes to import.
|
|
84
|
+
*/
|
|
85
|
+
export function generateGatewayKeypair() {
|
|
86
|
+
const { publicKey, privateKey } = generateKeyPairSync('x25519');
|
|
87
|
+
return {
|
|
88
|
+
skB64: extractRawPrivate(privateKey),
|
|
89
|
+
pkB64: extractRawPublic(publicKey),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Re-constitute a Node `KeyObject` from raw base64url public-key bytes.
|
|
94
|
+
* Uses the JWK OKP encoding because Node doesn't accept raw-format
|
|
95
|
+
* inputs to `createPublicKey` directly.
|
|
96
|
+
*/
|
|
97
|
+
function publicKeyFromB64(pkB64) {
|
|
98
|
+
const raw = Buffer.from(pkB64, 'base64url');
|
|
99
|
+
if (raw.length !== X25519_KEY_BYTES) {
|
|
100
|
+
throw new Error(`pair-crypto: public key must be ${X25519_KEY_BYTES} bytes (got ${raw.length})`);
|
|
101
|
+
}
|
|
102
|
+
return createPublicKey({
|
|
103
|
+
key: { kty: 'OKP', crv: 'X25519', x: raw.toString('base64url') },
|
|
104
|
+
format: 'jwk',
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Re-constitute a Node `KeyObject` from raw base64url private-key bytes.
|
|
109
|
+
*
|
|
110
|
+
* JWK OKP private keys require the `x` (public) field too; we derive it
|
|
111
|
+
* by first constructing a temporary KeyObject from `d` alone, then
|
|
112
|
+
* exporting its public half. This is cheap (one scalarmult) and keeps
|
|
113
|
+
* the call sites clean.
|
|
114
|
+
*
|
|
115
|
+
* Mirror of the browser-side WebCrypto `importKey('raw', ...)` path.
|
|
116
|
+
*/
|
|
117
|
+
function privateKeyFromB64(skB64) {
|
|
118
|
+
const raw = Buffer.from(skB64, 'base64url');
|
|
119
|
+
if (raw.length !== X25519_KEY_BYTES) {
|
|
120
|
+
throw new Error(`pair-crypto: private key must be ${X25519_KEY_BYTES} bytes (got ${raw.length})`);
|
|
121
|
+
}
|
|
122
|
+
// The JWK OKP format requires `x` (public) alongside `d` (private).
|
|
123
|
+
// Derive `x` by first constructing a public KeyObject from the scalar
|
|
124
|
+
// via the OKP JWK path — Node accepts `d` alone and returns a usable
|
|
125
|
+
// private KeyObject from which we can derive the public half.
|
|
126
|
+
const tempPriv = createPrivateKey({
|
|
127
|
+
key: { kty: 'OKP', crv: 'X25519', d: raw.toString('base64url'), x: '' },
|
|
128
|
+
format: 'jwk',
|
|
129
|
+
});
|
|
130
|
+
// Derive the public half.
|
|
131
|
+
const pubObj = createPublicKey(tempPriv);
|
|
132
|
+
const pubJwk = pubObj.export({ format: 'jwk' });
|
|
133
|
+
// Re-construct with the full (x, d) pair. This is the canonical form
|
|
134
|
+
// the rest of the code holds onto.
|
|
135
|
+
return createPrivateKey({
|
|
136
|
+
key: { kty: 'OKP', crv: 'X25519', d: raw.toString('base64url'), x: pubJwk.x },
|
|
137
|
+
format: 'jwk',
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/** Extract the raw 32-byte public-key bytes from a Node KeyObject → base64url. */
|
|
141
|
+
function extractRawPublic(pk) {
|
|
142
|
+
const jwk = pk.export({ format: 'jwk' });
|
|
143
|
+
if (!jwk.x)
|
|
144
|
+
throw new Error('pair-crypto: public key JWK is missing the x field');
|
|
145
|
+
return jwk.x; // JWK `x` is already base64url-encoded raw bytes.
|
|
146
|
+
}
|
|
147
|
+
/** Extract the raw 32-byte private-key bytes from a Node KeyObject → base64url. */
|
|
148
|
+
function extractRawPrivate(sk) {
|
|
149
|
+
const jwk = sk.export({ format: 'jwk' });
|
|
150
|
+
if (!jwk.d)
|
|
151
|
+
throw new Error('pair-crypto: private key JWK is missing the d field');
|
|
152
|
+
return jwk.d;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Derive the public key from a raw base64url private key. Exposed for
|
|
156
|
+
* tests and for the session-store's self-consistency checks — the
|
|
157
|
+
* default `createPairSession` doesn't call this (it generates both
|
|
158
|
+
* halves at once via `generateGatewayKeypair`).
|
|
159
|
+
*/
|
|
160
|
+
export function derivePublicFromPrivate(skB64) {
|
|
161
|
+
const sk = privateKeyFromB64(skB64);
|
|
162
|
+
const pk = createPublicKey(sk);
|
|
163
|
+
return extractRawPublic(pk);
|
|
164
|
+
}
|
|
165
|
+
// ---------------------------------------------------------------------------
|
|
166
|
+
// ECDH + HKDF
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
/**
|
|
169
|
+
* Perform x25519 ECDH between (local private key, remote public key),
|
|
170
|
+
* producing a 32-byte shared secret.
|
|
171
|
+
*
|
|
172
|
+
* BOTH sides running this with swapped halves MUST produce the same
|
|
173
|
+
* shared secret. This is the foundation of the pairing key agreement.
|
|
174
|
+
*
|
|
175
|
+
* Validation: throws if either key is the wrong byte length, or if
|
|
176
|
+
* the underlying `diffieHellman` call fails (which Node will do for
|
|
177
|
+
* invalid curve points).
|
|
178
|
+
*/
|
|
179
|
+
export function computeSharedSecret(opts) {
|
|
180
|
+
const sk = privateKeyFromB64(opts.skLocalB64);
|
|
181
|
+
const pk = publicKeyFromB64(opts.pkRemoteB64);
|
|
182
|
+
const shared = diffieHellman({ privateKey: sk, publicKey: pk });
|
|
183
|
+
if (shared.length !== X25519_KEY_BYTES) {
|
|
184
|
+
throw new Error(`pair-crypto: ECDH output wrong length (got ${shared.length}, expected ${X25519_KEY_BYTES})`);
|
|
185
|
+
}
|
|
186
|
+
return shared;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Derive the AEAD key from a shared secret via HKDF-SHA256. Uses the
|
|
190
|
+
* session id as the salt and the fixed protocol tag as the info.
|
|
191
|
+
*
|
|
192
|
+
* Mathematical sketch (per RFC 5869):
|
|
193
|
+
* PRK = HMAC-SHA256(salt, shared)
|
|
194
|
+
* OKM = HMAC-SHA256(PRK, info || 0x01)[:L]
|
|
195
|
+
*
|
|
196
|
+
* Where L = 32 bytes = AEAD_KEY_BYTES.
|
|
197
|
+
*
|
|
198
|
+
* The sid binding means a ciphertext encrypted for session A is
|
|
199
|
+
* DECRYPTABLE only under session A's derived key. Replaying ct from
|
|
200
|
+
* session A into session B's decrypt path produces a different key →
|
|
201
|
+
* AEAD tag fails → rejected.
|
|
202
|
+
*/
|
|
203
|
+
export function deriveSessionKeys(opts) {
|
|
204
|
+
if (opts.sharedSecret.length !== X25519_KEY_BYTES) {
|
|
205
|
+
throw new Error('pair-crypto: shared secret must be 32 bytes');
|
|
206
|
+
}
|
|
207
|
+
if (typeof opts.sid !== 'string' || opts.sid.length === 0) {
|
|
208
|
+
throw new Error('pair-crypto: sid is required for HKDF salt binding');
|
|
209
|
+
}
|
|
210
|
+
const salt = Buffer.from(opts.sid, 'utf-8');
|
|
211
|
+
const info = Buffer.from(HKDF_INFO, 'utf-8');
|
|
212
|
+
const okm = hkdfSync('sha256', opts.sharedSecret, salt, info, AEAD_KEY_BYTES);
|
|
213
|
+
return { kEnc: Buffer.from(okm) };
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* One-shot convenience: ECDH + HKDF in a single call. Used by both the
|
|
217
|
+
* HTTP respond handler (decrypt side) and the tests.
|
|
218
|
+
*/
|
|
219
|
+
export function deriveAeadKeyFromEcdh(opts) {
|
|
220
|
+
const shared = computeSharedSecret({
|
|
221
|
+
skLocalB64: opts.skLocalB64,
|
|
222
|
+
pkRemoteB64: opts.pkRemoteB64,
|
|
223
|
+
});
|
|
224
|
+
return deriveSessionKeys({ sharedSecret: shared, sid: opts.sid });
|
|
225
|
+
}
|
|
226
|
+
// ---------------------------------------------------------------------------
|
|
227
|
+
// AEAD
|
|
228
|
+
// ---------------------------------------------------------------------------
|
|
229
|
+
/**
|
|
230
|
+
* Decrypt an AES-256-GCM AEAD ciphertext. Returns the plaintext on
|
|
231
|
+
* success; throws if the tag is invalid (which includes both tampering
|
|
232
|
+
* and wrong-key attempts).
|
|
233
|
+
*
|
|
234
|
+
* Ciphertext is expected in the combined form `plaintext || tag`, where
|
|
235
|
+
* tag is the trailing 16 bytes. The caller MUST supply the same
|
|
236
|
+
* (kEnc, nonce, sid-as-AD) used for encryption.
|
|
237
|
+
*/
|
|
238
|
+
export function aeadDecrypt(opts) {
|
|
239
|
+
const nonce = Buffer.from(opts.nonceB64, 'base64url');
|
|
240
|
+
if (nonce.length !== AEAD_NONCE_BYTES) {
|
|
241
|
+
throw new Error(`pair-crypto: nonce must be ${AEAD_NONCE_BYTES} bytes (got ${nonce.length})`);
|
|
242
|
+
}
|
|
243
|
+
if (opts.kEnc.length !== AEAD_KEY_BYTES) {
|
|
244
|
+
throw new Error(`pair-crypto: AEAD key must be ${AEAD_KEY_BYTES} bytes`);
|
|
245
|
+
}
|
|
246
|
+
const combined = Buffer.from(opts.ciphertextB64, 'base64url');
|
|
247
|
+
if (combined.length < AEAD_TAG_BYTES) {
|
|
248
|
+
throw new Error('pair-crypto: ciphertext too short to contain tag');
|
|
249
|
+
}
|
|
250
|
+
const ct = combined.subarray(0, combined.length - AEAD_TAG_BYTES);
|
|
251
|
+
const tag = combined.subarray(combined.length - AEAD_TAG_BYTES);
|
|
252
|
+
const decipher = createDecipheriv('aes-256-gcm', opts.kEnc, nonce, {
|
|
253
|
+
authTagLength: AEAD_TAG_BYTES,
|
|
254
|
+
});
|
|
255
|
+
decipher.setAAD(Buffer.from(opts.sid, 'utf-8'), { plaintextLength: ct.length });
|
|
256
|
+
decipher.setAuthTag(tag);
|
|
257
|
+
const pt1 = decipher.update(ct);
|
|
258
|
+
const pt2 = decipher.final();
|
|
259
|
+
return Buffer.concat([pt1, pt2]);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* One-shot decrypt: ECDH + HKDF + AEAD in one call. This is what the
|
|
263
|
+
* HTTP respond handler calls. Returns the plaintext Buffer on success.
|
|
264
|
+
*
|
|
265
|
+
* Throws on ANY failure (wrong key length, invalid curve point, tag
|
|
266
|
+
* mismatch). The caller catches and returns 400 to the device.
|
|
267
|
+
*/
|
|
268
|
+
export function decryptPairingPayload(inputs) {
|
|
269
|
+
const { kEnc } = deriveAeadKeyFromEcdh({
|
|
270
|
+
skLocalB64: inputs.skGatewayB64,
|
|
271
|
+
pkRemoteB64: inputs.pkDeviceB64,
|
|
272
|
+
sid: inputs.sid,
|
|
273
|
+
});
|
|
274
|
+
return aeadDecrypt({
|
|
275
|
+
kEnc,
|
|
276
|
+
nonceB64: inputs.nonceB64,
|
|
277
|
+
sid: inputs.sid,
|
|
278
|
+
ciphertextB64: inputs.ciphertextB64,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Encrypt a plaintext payload. Used by tests; also used by any future
|
|
283
|
+
* gateway-to-device encrypted ACK path.
|
|
284
|
+
*
|
|
285
|
+
* Emits (nonce, ciphertext||tag) both base64url-encoded. If the caller
|
|
286
|
+
* passes an explicit `nonceB64`, it is used verbatim; otherwise a
|
|
287
|
+
* fresh 12-byte random nonce is generated.
|
|
288
|
+
*/
|
|
289
|
+
export function aeadEncryptWithSessionKey(opts) {
|
|
290
|
+
if (opts.kEnc.length !== AEAD_KEY_BYTES) {
|
|
291
|
+
throw new Error(`pair-crypto: AEAD key must be ${AEAD_KEY_BYTES} bytes`);
|
|
292
|
+
}
|
|
293
|
+
const nonceBuf = opts.nonceB64 !== undefined
|
|
294
|
+
? Buffer.from(opts.nonceB64, 'base64url')
|
|
295
|
+
: randomBytes(AEAD_NONCE_BYTES);
|
|
296
|
+
if (nonceBuf.length !== AEAD_NONCE_BYTES) {
|
|
297
|
+
throw new Error(`pair-crypto: nonce must be ${AEAD_NONCE_BYTES} bytes`);
|
|
298
|
+
}
|
|
299
|
+
const pt = Buffer.isBuffer(opts.plaintext) ? opts.plaintext : Buffer.from(opts.plaintext);
|
|
300
|
+
const cipher = createCipheriv('aes-256-gcm', opts.kEnc, nonceBuf, {
|
|
301
|
+
authTagLength: AEAD_TAG_BYTES,
|
|
302
|
+
});
|
|
303
|
+
cipher.setAAD(Buffer.from(opts.sid, 'utf-8'), { plaintextLength: pt.length });
|
|
304
|
+
const ct = Buffer.concat([cipher.update(pt), cipher.final()]);
|
|
305
|
+
const tag = cipher.getAuthTag();
|
|
306
|
+
return {
|
|
307
|
+
nonceB64: nonceBuf.toString('base64url'),
|
|
308
|
+
ciphertextB64: Buffer.concat([ct, tag]).toString('base64url'),
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* One-shot encrypt: ECDH + HKDF + AEAD (caller supplies both sides).
|
|
313
|
+
* Used by the test vectors and future 4.x features. The real device
|
|
314
|
+
* side does this in the browser, not here.
|
|
315
|
+
*/
|
|
316
|
+
export function encryptPairingPayload(inputs) {
|
|
317
|
+
const { kEnc } = deriveAeadKeyFromEcdh({
|
|
318
|
+
skLocalB64: inputs.skLocalB64,
|
|
319
|
+
pkRemoteB64: inputs.pkRemoteB64,
|
|
320
|
+
sid: inputs.sid,
|
|
321
|
+
});
|
|
322
|
+
return aeadEncryptWithSessionKey({
|
|
323
|
+
kEnc,
|
|
324
|
+
sid: inputs.sid,
|
|
325
|
+
plaintext: inputs.plaintext,
|
|
326
|
+
nonceB64: inputs.nonceB64,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
// ---------------------------------------------------------------------------
|
|
330
|
+
// Constant-time secondary-code comparison
|
|
331
|
+
// ---------------------------------------------------------------------------
|
|
332
|
+
/**
|
|
333
|
+
* Constant-time compare two 6-digit numeric strings. Used by the HTTP
|
|
334
|
+
* start/respond handlers to check the user-supplied secondary code
|
|
335
|
+
* against the session's expected code.
|
|
336
|
+
*
|
|
337
|
+
* Returns true on match, false otherwise. Length mismatch returns
|
|
338
|
+
* false without short-circuit leak of which side was shorter.
|
|
339
|
+
*
|
|
340
|
+
* Uses Node `timingSafeEqual`, which requires equal-length inputs.
|
|
341
|
+
* We pad the SHORTER input with NULs and always compare a fixed
|
|
342
|
+
* 6-byte window — the length check happens BEFORE the actual compare
|
|
343
|
+
* and uses boolean AND at the end so both branches run to completion.
|
|
344
|
+
*/
|
|
345
|
+
export function compareSecondaryCodesCT(a, b) {
|
|
346
|
+
const aBuf = Buffer.from(a, 'utf-8');
|
|
347
|
+
const bBuf = Buffer.from(b, 'utf-8');
|
|
348
|
+
const lenMatch = aBuf.length === bBuf.length;
|
|
349
|
+
// Always compare a constant window so the total work is independent
|
|
350
|
+
// of the actual input lengths. Pad the shorter to the longer with
|
|
351
|
+
// zero bytes; if lengths diverge we force lenMatch=false below.
|
|
352
|
+
const max = Math.max(aBuf.length, bBuf.length, 6);
|
|
353
|
+
const aPad = Buffer.alloc(max);
|
|
354
|
+
const bPad = Buffer.alloc(max);
|
|
355
|
+
aBuf.copy(aPad);
|
|
356
|
+
bBuf.copy(bPad);
|
|
357
|
+
const byteMatch = timingSafeEqual(aPad, bPad);
|
|
358
|
+
return lenMatch && byteMatch;
|
|
359
|
+
}
|