@johnhenry/browsermesh-core 0.6.0 → 0.7.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/README.md +18 -0
- package/package.json +2 -2
- package/src/group-keys.mjs +42 -5
- package/src/handshake.mjs +8 -3
- package/src/peer-tools.mjs +20 -5
package/README.md
CHANGED
|
@@ -50,6 +50,24 @@ npm install @johnhenry/browsermesh-core @johnhenry/browsermesh-primitives
|
|
|
50
50
|
import { MeshIdentityManager, MeshKeyring, TrustGraph } from '@johnhenry/browsermesh-core';
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
## Padding group-key envelopes
|
|
54
|
+
|
|
55
|
+
`GroupKeyManager.encrypt()` leaks the exact plaintext length to anything that
|
|
56
|
+
sees the ciphertext (a relay, for instance). Pass `padding` to round the
|
|
57
|
+
plaintext up to a size bucket before sealing it:
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
const { ciphertext, iv, epoch } = await keys.encrypt(bytes, { padding: true });
|
|
61
|
+
const back = await keys.decrypt(ciphertext, iv, epoch, { padding: true });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`padding: true` uses buckets of 256 / 1024 / 4096 / 16384 bytes (larger
|
|
65
|
+
payloads round up to a multiple of 16384); `{ buckets: [...] }` picks your own;
|
|
66
|
+
`new GroupKeyManager({ ..., padding })` sets the default. Ciphertexts are then
|
|
67
|
+
`bucket + 16` bytes. Sender and receiver must agree on the setting (the padded
|
|
68
|
+
plaintext is not self-describing). Off by default; no cover traffic or timing
|
|
69
|
+
protection is provided.
|
|
70
|
+
|
|
53
71
|
## License
|
|
54
72
|
|
|
55
73
|
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/browsermesh-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Identity, crypto, peer management, and trust primitives for BrowserMesh",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"directory": "packages/browsermesh-core"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@johnhenry/browsermesh-primitives": ">=0.
|
|
23
|
+
"@johnhenry/browsermesh-primitives": ">=0.3.0 <1.0.0",
|
|
24
24
|
"@johnhenry/browsermesh-apps": ">=0.4.0 <1.0.0"
|
|
25
25
|
},
|
|
26
26
|
"peerDependenciesMeta": {
|
package/src/group-keys.mjs
CHANGED
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-group-keys.test.mjs
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
+
import { padTo, unpad } from '@johnhenry/browsermesh-primitives'
|
|
17
|
+
|
|
16
18
|
// ---------------------------------------------------------------------------
|
|
17
19
|
// Wire constants
|
|
18
20
|
// ---------------------------------------------------------------------------
|
|
@@ -227,6 +229,17 @@ export class GroupState {
|
|
|
227
229
|
// GroupKeyManager
|
|
228
230
|
// ---------------------------------------------------------------------------
|
|
229
231
|
|
|
232
|
+
/**
|
|
233
|
+
* @param {boolean|{ buckets?: number[] }|null|undefined} padding
|
|
234
|
+
* @returns {false|{ buckets?: number[] }}
|
|
235
|
+
*/
|
|
236
|
+
function normalizePadding(padding) {
|
|
237
|
+
if (!padding) return false
|
|
238
|
+
if (padding === true) return {}
|
|
239
|
+
if (typeof padding === 'object') return { buckets: padding.buckets }
|
|
240
|
+
throw new TypeError('padding must be a boolean or { buckets }')
|
|
241
|
+
}
|
|
242
|
+
|
|
230
243
|
/**
|
|
231
244
|
* Manages symmetric group keys with epoch-based rotation.
|
|
232
245
|
* Each epoch has a unique AES-GCM-256 key shared by all group members.
|
|
@@ -263,20 +276,26 @@ export class GroupKeyManager {
|
|
|
263
276
|
/** @type {function|null} */
|
|
264
277
|
#onLog
|
|
265
278
|
|
|
279
|
+
/** @type {false|{ buckets?: number[] }} Default padding for encrypt()/decrypt() */
|
|
280
|
+
#padding
|
|
281
|
+
|
|
266
282
|
/**
|
|
267
283
|
* @param {object} opts
|
|
268
284
|
* @param {string} opts.localPodId
|
|
269
285
|
* @param {string} opts.groupId
|
|
270
286
|
* @param {number} [opts.maxEpochHistory=10]
|
|
271
287
|
* @param {function} [opts.onLog]
|
|
288
|
+
* @param {boolean|{ buckets?: number[] }} [opts.padding=false] - Default for
|
|
289
|
+
* the `padding` option of `encrypt()`/`decrypt()`. Off unless set.
|
|
272
290
|
*/
|
|
273
|
-
constructor({ localPodId, groupId, maxEpochHistory = 10, onLog } = {}) {
|
|
291
|
+
constructor({ localPodId, groupId, maxEpochHistory = 10, onLog, padding = false } = {}) {
|
|
274
292
|
if (!localPodId) throw new Error('localPodId is required')
|
|
275
293
|
if (!groupId) throw new Error('groupId is required')
|
|
276
294
|
this.#localPodId = localPodId
|
|
277
295
|
this.#groupId = groupId
|
|
278
296
|
this.#maxEpochHistory = maxEpochHistory
|
|
279
297
|
this.#onLog = onLog || null
|
|
298
|
+
this.#padding = normalizePadding(padding)
|
|
280
299
|
}
|
|
281
300
|
|
|
282
301
|
get localPodId() { return this.#localPodId }
|
|
@@ -498,19 +517,32 @@ export class GroupKeyManager {
|
|
|
498
517
|
/**
|
|
499
518
|
* Encrypt data with the current epoch key.
|
|
500
519
|
*
|
|
520
|
+
* With `padding` on, the plaintext is rounded up to a size bucket (see
|
|
521
|
+
* `padTo()` in `@johnhenry/browsermesh-primitives`) BEFORE sealing, so a
|
|
522
|
+
* relay that only sees ciphertext learns the bucket, not the exact length:
|
|
523
|
+
* the ciphertext is always `bucket + 16` bytes (the AES-GCM tag). Every
|
|
524
|
+
* receiver must decrypt with the same `padding` setting. Off by default.
|
|
525
|
+
*
|
|
501
526
|
* @param {Uint8Array} plaintext
|
|
527
|
+
* @param {object} [opts]
|
|
528
|
+
* @param {boolean|{ buckets?: number[] }} [opts.padding] - Overrides the
|
|
529
|
+
* constructor default; `true` uses the default buckets
|
|
530
|
+
* (256/1024/4096/16384), `{ buckets }` custom ones.
|
|
502
531
|
* @returns {Promise<{ ciphertext: Uint8Array, iv: Uint8Array, epoch: number }>}
|
|
503
532
|
*/
|
|
504
|
-
async encrypt(plaintext) {
|
|
533
|
+
async encrypt(plaintext, { padding } = {}) {
|
|
505
534
|
const state = this.getCurrentState()
|
|
506
535
|
if (!state || !state.key) throw new Error('No active group key')
|
|
507
536
|
|
|
537
|
+
const pad = padding === undefined ? this.#padding : normalizePadding(padding)
|
|
538
|
+
const sealed = pad ? padTo(plaintext, pad) : plaintext
|
|
539
|
+
|
|
508
540
|
const iv = crypto.getRandomValues(new Uint8Array(12))
|
|
509
541
|
const ciphertext = new Uint8Array(
|
|
510
542
|
await crypto.subtle.encrypt(
|
|
511
543
|
{ name: 'AES-GCM', iv },
|
|
512
544
|
state.key,
|
|
513
|
-
|
|
545
|
+
sealed,
|
|
514
546
|
),
|
|
515
547
|
)
|
|
516
548
|
|
|
@@ -523,9 +555,13 @@ export class GroupKeyManager {
|
|
|
523
555
|
* @param {Uint8Array} ciphertext
|
|
524
556
|
* @param {Uint8Array} iv
|
|
525
557
|
* @param {number} epoch
|
|
558
|
+
* @param {object} [opts]
|
|
559
|
+
* @param {boolean|{ buckets?: number[] }} [opts.padding] - Must match the
|
|
560
|
+
* sender's `encrypt()` setting: when on, the padding is stripped after
|
|
561
|
+
* decryption. Overrides the constructor default.
|
|
526
562
|
* @returns {Promise<Uint8Array>}
|
|
527
563
|
*/
|
|
528
|
-
async decrypt(ciphertext, iv, epoch) {
|
|
564
|
+
async decrypt(ciphertext, iv, epoch, { padding } = {}) {
|
|
529
565
|
const state = this.#epochs.get(epoch)
|
|
530
566
|
if (!state || !state.key) throw new Error(`No key for epoch ${epoch}`)
|
|
531
567
|
|
|
@@ -537,7 +573,8 @@ export class GroupKeyManager {
|
|
|
537
573
|
),
|
|
538
574
|
)
|
|
539
575
|
|
|
540
|
-
|
|
576
|
+
const pad = padding === undefined ? this.#padding : normalizePadding(padding)
|
|
577
|
+
return pad ? unpad(plaintext) : plaintext
|
|
541
578
|
}
|
|
542
579
|
|
|
543
580
|
/**
|
package/src/handshake.mjs
CHANGED
|
@@ -282,25 +282,28 @@ export class SignalingClient {
|
|
|
282
282
|
/**
|
|
283
283
|
* Register callback for incoming SDP offers.
|
|
284
284
|
* @param {Function} cb - Receives (offer, fromPodId)
|
|
285
|
+
* @returns {() => void} Unsubscribe
|
|
285
286
|
*/
|
|
286
287
|
onOffer(cb) {
|
|
287
|
-
this.on('offer', cb)
|
|
288
|
+
return this.on('offer', cb)
|
|
288
289
|
}
|
|
289
290
|
|
|
290
291
|
/**
|
|
291
292
|
* Register callback for incoming SDP answers.
|
|
292
293
|
* @param {Function} cb - Receives (answer, fromPodId)
|
|
294
|
+
* @returns {() => void} Unsubscribe
|
|
293
295
|
*/
|
|
294
296
|
onAnswer(cb) {
|
|
295
|
-
this.on('answer', cb)
|
|
297
|
+
return this.on('answer', cb)
|
|
296
298
|
}
|
|
297
299
|
|
|
298
300
|
/**
|
|
299
301
|
* Register callback for incoming ICE candidates.
|
|
300
302
|
* @param {Function} cb - Receives (candidate, fromPodId)
|
|
303
|
+
* @returns {() => void} Unsubscribe
|
|
301
304
|
*/
|
|
302
305
|
onIceCandidate(cb) {
|
|
303
|
-
this.on('ice-candidate', cb)
|
|
306
|
+
return this.on('ice-candidate', cb)
|
|
304
307
|
}
|
|
305
308
|
|
|
306
309
|
// -- Generic send / event system ------------------------------------------
|
|
@@ -339,12 +342,14 @@ export class SignalingClient {
|
|
|
339
342
|
* Register a listener for a given event type.
|
|
340
343
|
* @param {string} event
|
|
341
344
|
* @param {Function} cb
|
|
345
|
+
* @returns {() => void} Unsubscribe (same as `off(event, cb)`)
|
|
342
346
|
*/
|
|
343
347
|
on(event, cb) {
|
|
344
348
|
if (!this.#listeners.has(event)) {
|
|
345
349
|
this.#listeners.set(event, new Set())
|
|
346
350
|
}
|
|
347
351
|
this.#listeners.get(event).add(cb)
|
|
352
|
+
return () => this.off(event, cb)
|
|
348
353
|
}
|
|
349
354
|
|
|
350
355
|
/**
|
package/src/peer-tools.mjs
CHANGED
|
@@ -560,9 +560,14 @@ export class TimestampProofTool extends BrowserTool {
|
|
|
560
560
|
|
|
561
561
|
// ── Stealth tools ─────────────────────────────────────────────────────
|
|
562
562
|
|
|
563
|
+
// The agent is a `StealthAgent` from @johnhenry/browsermesh-discovery:
|
|
564
|
+
// `hide(stateString)` / `reconstitute()`. It shards the state across the DHT
|
|
565
|
+
// as plaintext slices plus XOR parity. There is NO encryption, so these tools
|
|
566
|
+
// say so; encrypt the state first if it is sensitive.
|
|
567
|
+
|
|
563
568
|
export class StealthSaveTool extends BrowserTool {
|
|
564
569
|
get name() { return 'stealth_save' }
|
|
565
|
-
get description() { return 'Save agent state as
|
|
570
|
+
get description() { return 'Save agent state as XOR-parity shards distributed across the DHT. The shards are NOT encrypted: anyone who can read the DHT entries can read the state.' }
|
|
566
571
|
get parameters() {
|
|
567
572
|
return {
|
|
568
573
|
type: 'object',
|
|
@@ -578,8 +583,11 @@ export class StealthSaveTool extends BrowserTool {
|
|
|
578
583
|
const agent = peerToolsContext.getStealthAgent()
|
|
579
584
|
if (!agent) return { success: false, output: '', error: 'Stealth agent not initialized.' }
|
|
580
585
|
try {
|
|
581
|
-
|
|
582
|
-
|
|
586
|
+
const manifest = typeof agent.hide === 'function'
|
|
587
|
+
? await agent.hide(JSON.stringify(state))
|
|
588
|
+
: await agent.saveState(state) // duck-typed agent from before the tool matched StealthAgent
|
|
589
|
+
const shards = manifest?.shardIds?.length
|
|
590
|
+
return { success: true, output: `State saved as ${shards ?? ''}${shards ? ' ' : ''}unencrypted distributed shards.` }
|
|
583
591
|
} catch (err) {
|
|
584
592
|
return { success: false, output: '', error: err.message }
|
|
585
593
|
}
|
|
@@ -588,7 +596,7 @@ export class StealthSaveTool extends BrowserTool {
|
|
|
588
596
|
|
|
589
597
|
export class StealthRestoreTool extends BrowserTool {
|
|
590
598
|
get name() { return 'stealth_restore' }
|
|
591
|
-
get description() { return 'Restore agent state from
|
|
599
|
+
get description() { return 'Restore agent state from XOR-parity shards in the DHT (shards are not encrypted).' }
|
|
592
600
|
get parameters() { return { type: 'object', properties: {} } }
|
|
593
601
|
get permission() { return 'approve' }
|
|
594
602
|
|
|
@@ -596,7 +604,14 @@ export class StealthRestoreTool extends BrowserTool {
|
|
|
596
604
|
const agent = peerToolsContext.getStealthAgent()
|
|
597
605
|
if (!agent) return { success: false, output: '', error: 'Stealth agent not initialized.' }
|
|
598
606
|
try {
|
|
599
|
-
|
|
607
|
+
if (typeof agent.reconstitute === 'function') {
|
|
608
|
+
if (typeof agent.isViable === 'function' && !agent.isViable()) {
|
|
609
|
+
return { success: true, output: 'No state found.' }
|
|
610
|
+
}
|
|
611
|
+
const blob = await agent.reconstitute()
|
|
612
|
+
return { success: true, output: blob ? blob : 'No state found.' }
|
|
613
|
+
}
|
|
614
|
+
const state = await agent.restoreState() // duck-typed agent from before the tool matched StealthAgent
|
|
600
615
|
return { success: true, output: state ? JSON.stringify(state) : 'No state found.' }
|
|
601
616
|
} catch (err) {
|
|
602
617
|
return { success: false, output: '', error: err.message }
|