@openllmsh/sdk 2.10.19-alpha.1 → 2.10.19-alpha.2

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.
@@ -26779,27 +26779,6 @@
26779
26779
  "id": {
26780
26780
  "type": "string"
26781
26781
  },
26782
- "kind": {
26783
- "type": "string",
26784
- "enum": [
26785
- "routing"
26786
- ]
26787
- },
26788
- "capabilities": {
26789
- "type": "object",
26790
- "properties": {
26791
- "device_sessions": {
26792
- "type": "boolean",
26793
- "enum": [
26794
- false
26795
- ]
26796
- }
26797
- },
26798
- "required": [
26799
- "device_sessions"
26800
- ],
26801
- "additionalProperties": false
26802
- },
26803
26782
  "state": {
26804
26783
  "type": "string",
26805
26784
  "enum": [
@@ -26914,8 +26893,6 @@
26914
26893
  },
26915
26894
  "required": [
26916
26895
  "id",
26917
- "kind",
26918
- "capabilities",
26919
26896
  "state",
26920
26897
  "generation",
26921
26898
  "public_ipv4",
@@ -26933,6 +26910,22 @@
26933
26910
  "type": "null"
26934
26911
  }
26935
26912
  ]
26913
+ },
26914
+ "notice": {
26915
+ "anyOf": [
26916
+ {
26917
+ "type": "string",
26918
+ "enum": [
26919
+ "machine_gone"
26920
+ ]
26921
+ },
26922
+ {
26923
+ "type": "null"
26924
+ },
26925
+ {
26926
+ "type": "null"
26927
+ }
26928
+ ]
26936
26929
  }
26937
26930
  },
26938
26931
  "required": [
@@ -27352,27 +27345,6 @@
27352
27345
  "id": {
27353
27346
  "type": "string"
27354
27347
  },
27355
- "kind": {
27356
- "type": "string",
27357
- "enum": [
27358
- "routing"
27359
- ]
27360
- },
27361
- "capabilities": {
27362
- "type": "object",
27363
- "properties": {
27364
- "device_sessions": {
27365
- "type": "boolean",
27366
- "enum": [
27367
- false
27368
- ]
27369
- }
27370
- },
27371
- "required": [
27372
- "device_sessions"
27373
- ],
27374
- "additionalProperties": false
27375
- },
27376
27348
  "state": {
27377
27349
  "type": "string",
27378
27350
  "enum": [
@@ -27487,8 +27459,6 @@
27487
27459
  },
27488
27460
  "required": [
27489
27461
  "id",
27490
- "kind",
27491
- "capabilities",
27492
27462
  "state",
27493
27463
  "generation",
27494
27464
  "public_ipv4",
@@ -27506,6 +27476,22 @@
27506
27476
  "type": "null"
27507
27477
  }
27508
27478
  ]
27479
+ },
27480
+ "notice": {
27481
+ "anyOf": [
27482
+ {
27483
+ "type": "string",
27484
+ "enum": [
27485
+ "machine_gone"
27486
+ ]
27487
+ },
27488
+ {
27489
+ "type": "null"
27490
+ },
27491
+ {
27492
+ "type": "null"
27493
+ }
27494
+ ]
27509
27495
  }
27510
27496
  },
27511
27497
  "required": [
@@ -27531,27 +27517,6 @@
27531
27517
  "id": {
27532
27518
  "type": "string"
27533
27519
  },
27534
- "kind": {
27535
- "type": "string",
27536
- "enum": [
27537
- "routing"
27538
- ]
27539
- },
27540
- "capabilities": {
27541
- "type": "object",
27542
- "properties": {
27543
- "device_sessions": {
27544
- "type": "boolean",
27545
- "enum": [
27546
- false
27547
- ]
27548
- }
27549
- },
27550
- "required": [
27551
- "device_sessions"
27552
- ],
27553
- "additionalProperties": false
27554
- },
27555
27520
  "state": {
27556
27521
  "type": "string",
27557
27522
  "enum": [
@@ -27666,8 +27631,6 @@
27666
27631
  },
27667
27632
  "required": [
27668
27633
  "id",
27669
- "kind",
27670
- "capabilities",
27671
27634
  "state",
27672
27635
  "generation",
27673
27636
  "public_ipv4",
@@ -27685,6 +27648,22 @@
27685
27648
  "type": "null"
27686
27649
  }
27687
27650
  ]
27651
+ },
27652
+ "notice": {
27653
+ "anyOf": [
27654
+ {
27655
+ "type": "string",
27656
+ "enum": [
27657
+ "machine_gone"
27658
+ ]
27659
+ },
27660
+ {
27661
+ "type": "null"
27662
+ },
27663
+ {
27664
+ "type": "null"
27665
+ }
27666
+ ]
27688
27667
  }
27689
27668
  },
27690
27669
  "required": [
@@ -28115,27 +28094,6 @@
28115
28094
  "id": {
28116
28095
  "type": "string"
28117
28096
  },
28118
- "kind": {
28119
- "type": "string",
28120
- "enum": [
28121
- "routing"
28122
- ]
28123
- },
28124
- "capabilities": {
28125
- "type": "object",
28126
- "properties": {
28127
- "device_sessions": {
28128
- "type": "boolean",
28129
- "enum": [
28130
- false
28131
- ]
28132
- }
28133
- },
28134
- "required": [
28135
- "device_sessions"
28136
- ],
28137
- "additionalProperties": false
28138
- },
28139
28097
  "state": {
28140
28098
  "type": "string",
28141
28099
  "enum": [
@@ -28250,8 +28208,6 @@
28250
28208
  },
28251
28209
  "required": [
28252
28210
  "id",
28253
- "kind",
28254
- "capabilities",
28255
28211
  "state",
28256
28212
  "generation",
28257
28213
  "public_ipv4",
@@ -28269,6 +28225,22 @@
28269
28225
  "type": "null"
28270
28226
  }
28271
28227
  ]
28228
+ },
28229
+ "notice": {
28230
+ "anyOf": [
28231
+ {
28232
+ "type": "string",
28233
+ "enum": [
28234
+ "machine_gone"
28235
+ ]
28236
+ },
28237
+ {
28238
+ "type": "null"
28239
+ },
28240
+ {
28241
+ "type": "null"
28242
+ }
28243
+ ]
28272
28244
  }
28273
28245
  },
28274
28246
  "required": [
@@ -28699,27 +28671,6 @@
28699
28671
  "id": {
28700
28672
  "type": "string"
28701
28673
  },
28702
- "kind": {
28703
- "type": "string",
28704
- "enum": [
28705
- "routing"
28706
- ]
28707
- },
28708
- "capabilities": {
28709
- "type": "object",
28710
- "properties": {
28711
- "device_sessions": {
28712
- "type": "boolean",
28713
- "enum": [
28714
- false
28715
- ]
28716
- }
28717
- },
28718
- "required": [
28719
- "device_sessions"
28720
- ],
28721
- "additionalProperties": false
28722
- },
28723
28674
  "state": {
28724
28675
  "type": "string",
28725
28676
  "enum": [
@@ -28834,8 +28785,6 @@
28834
28785
  },
28835
28786
  "required": [
28836
28787
  "id",
28837
- "kind",
28838
- "capabilities",
28839
28788
  "state",
28840
28789
  "generation",
28841
28790
  "public_ipv4",
@@ -28853,6 +28802,22 @@
28853
28802
  "type": "null"
28854
28803
  }
28855
28804
  ]
28805
+ },
28806
+ "notice": {
28807
+ "anyOf": [
28808
+ {
28809
+ "type": "string",
28810
+ "enum": [
28811
+ "machine_gone"
28812
+ ]
28813
+ },
28814
+ {
28815
+ "type": "null"
28816
+ },
28817
+ {
28818
+ "type": "null"
28819
+ }
28820
+ ]
28856
28821
  }
28857
28822
  },
28858
28823
  "required": [
@@ -29283,27 +29248,6 @@
29283
29248
  "id": {
29284
29249
  "type": "string"
29285
29250
  },
29286
- "kind": {
29287
- "type": "string",
29288
- "enum": [
29289
- "routing"
29290
- ]
29291
- },
29292
- "capabilities": {
29293
- "type": "object",
29294
- "properties": {
29295
- "device_sessions": {
29296
- "type": "boolean",
29297
- "enum": [
29298
- false
29299
- ]
29300
- }
29301
- },
29302
- "required": [
29303
- "device_sessions"
29304
- ],
29305
- "additionalProperties": false
29306
- },
29307
29251
  "state": {
29308
29252
  "type": "string",
29309
29253
  "enum": [
@@ -29418,8 +29362,6 @@
29418
29362
  },
29419
29363
  "required": [
29420
29364
  "id",
29421
- "kind",
29422
- "capabilities",
29423
29365
  "state",
29424
29366
  "generation",
29425
29367
  "public_ipv4",
@@ -29437,6 +29379,22 @@
29437
29379
  "type": "null"
29438
29380
  }
29439
29381
  ]
29382
+ },
29383
+ "notice": {
29384
+ "anyOf": [
29385
+ {
29386
+ "type": "string",
29387
+ "enum": [
29388
+ "machine_gone"
29389
+ ]
29390
+ },
29391
+ {
29392
+ "type": "null"
29393
+ },
29394
+ {
29395
+ "type": "null"
29396
+ }
29397
+ ]
29440
29398
  }
29441
29399
  },
29442
29400
  "required": [
@@ -7,8 +7,8 @@
7
7
  * so connector and CLI ciphertext are not interchangeable. The server is a
8
8
  * mailbox: it stores only the JSON envelope.
9
9
  */
10
- import { aesGcmDecrypt, aesGcmEncrypt, fromB64, hkdfSha256, randomBytes, toB64, zero, } from "./crypto.js";
11
10
  import { generateResumableX25519Keypair, generateX25519Keypair, importX25519PrivateKey, } from "./handoff.js";
11
+ import { decodeCanonicalB64, openX25519, sealX25519 } from "./x25519-seal.js";
12
12
  export const CLI_AUTH_SEAL_DOMAIN = "openllm-cli-auth-seal-v1";
13
13
  const aad = (binding) => new TextEncoder().encode(JSON.stringify([
14
14
  CLI_AUTH_SEAL_DOMAIN,
@@ -16,59 +16,34 @@ const aad = (binding) => new TextEncoder().encode(JSON.stringify([
16
16
  binding.authorizationId,
17
17
  binding.recipientPublicKeyB64,
18
18
  ]));
19
- const decode = (value) => {
20
- if (typeof value !== "string" ||
21
- !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value))
22
- throw new Error("Invalid sealed envelope encoding");
23
- const bytes = fromB64(value);
24
- if (toB64(bytes) !== value)
25
- throw new Error("Invalid sealed envelope encoding");
26
- return bytes;
27
- };
28
- const deriveKey = async (privateKey, peerB64, epk, recipient) => {
29
- const peer = await crypto.subtle.importKey("spki", decode(peerB64), { name: "X25519" }, false, []);
30
- const shared = new Uint8Array(await crypto.subtle.deriveBits({ name: "X25519", public: peer }, privateKey, 256));
31
- try {
32
- return await hkdfSha256(shared, new Uint8Array(0), JSON.stringify([CLI_AUTH_SEAL_DOMAIN, epk, recipient]), 32);
33
- }
34
- finally {
35
- zero(shared);
36
- }
37
- };
38
19
  /** Caller retains ownership of plaintext; the temporary copy is wiped. */
39
- export const sealCliAuth = async (binding, plaintext) => {
40
- const eph = await generateX25519Keypair();
41
- const key = await deriveKey(eph.privateKey, binding.recipientPublicKeyB64, eph.publicKeyB64, binding.recipientPublicKeyB64);
42
- const copy = new Uint8Array(plaintext);
43
- try {
44
- const nonce = randomBytes(12);
45
- return {
46
- v: 1,
47
- epk: eph.publicKeyB64,
48
- nonce: toB64(nonce),
49
- ct: toB64(await aesGcmEncrypt(key, nonce, copy, aad(binding))),
50
- };
51
- }
52
- finally {
53
- zero(key);
54
- zero(copy);
55
- }
56
- };
20
+ export const sealCliAuth = async (binding, plaintext) => sealX25519({
21
+ domain: CLI_AUTH_SEAL_DOMAIN,
22
+ recipientB64: binding.recipientPublicKeyB64,
23
+ recipientBytes: decodeCanonicalB64(binding.recipientPublicKeyB64),
24
+ format: "spki",
25
+ aad: aad(binding),
26
+ plaintext,
27
+ });
57
28
  /** Caller owns the returned plaintext and must wipe it when finished. */
58
29
  export const openCliAuth = async (binding, sealed, privateKey) => {
59
30
  if (typeof sealed !== "object" || sealed === null || sealed.v !== 1)
60
31
  throw new Error("Invalid sealed envelope");
61
- const nonce = decode(sealed.nonce);
62
- const ct = decode(sealed.ct);
32
+ const nonce = decodeCanonicalB64(sealed.nonce);
33
+ const ct = decodeCanonicalB64(sealed.ct);
63
34
  if (nonce.length !== 12 || ct.length < 16 || typeof sealed.epk !== "string")
64
35
  throw new Error("Invalid sealed envelope");
65
- const key = await deriveKey(privateKey, sealed.epk, sealed.epk, binding.recipientPublicKeyB64);
66
- try {
67
- return await aesGcmDecrypt(key, nonce, ct, aad(binding));
68
- }
69
- finally {
70
- zero(key);
71
- }
36
+ return openX25519({
37
+ domain: CLI_AUTH_SEAL_DOMAIN,
38
+ privateKey,
39
+ recipientB64: binding.recipientPublicKeyB64,
40
+ epkBytes: decodeCanonicalB64(sealed.epk),
41
+ epkB64: sealed.epk,
42
+ format: "spki",
43
+ aad: aad(binding),
44
+ nonce,
45
+ ct,
46
+ });
72
47
  };
73
48
  /** Alias kept for CLI call-site parity with the old `cli-auth-crypto` names. */
74
49
  export const generateCliAuthKeypair = generateX25519Keypair;
@@ -35,7 +35,6 @@ export const DEVICE_CONTROL_HKDF_SALT = "openllm-device-control-salt-v1";
35
35
  export const DEVICE_CONTROL_KEEP_HKDF_INFO = "openllm-device-control-keep-v1";
36
36
  /** Length of the device-control seed (and of the Routebox key's input). */
37
37
  export const DEVICE_CONTROL_SEED_BYTES = 32;
38
- const SEED_BYTES = DEVICE_CONTROL_SEED_BYTES;
39
38
  /**
40
39
  * `HKDF-SHA256(ikm = BIP39 entropy(phrase), salt = DEVICE_CONTROL_HKDF_SALT,
41
40
  * info = DEVICE_CONTROL_HKDF_INFO, 32)` — the Ed25519 private seed.
@@ -57,7 +56,7 @@ export const deriveDeviceControlSeed = async (phrase) => {
57
56
  * BIP39 entropy, `./seed.ts`) — the same key, for callers that hold the seed
58
57
  * bytes rather than the words. Never zeroes its input.
59
58
  */
60
- export const deriveDeviceControlSeedFromVaultSeed = (vaultSeed) => hkdfSha256(vaultSeed, new TextEncoder().encode(DEVICE_CONTROL_HKDF_SALT), DEVICE_CONTROL_HKDF_INFO, SEED_BYTES);
59
+ export const deriveDeviceControlSeedFromVaultSeed = (vaultSeed) => hkdfSha256(vaultSeed, new TextEncoder().encode(DEVICE_CONTROL_HKDF_SALT), DEVICE_CONTROL_HKDF_INFO, DEVICE_CONTROL_SEED_BYTES);
61
60
  /** The control public key as base64(SPKI DER) — the value daemons pin. */
62
61
  export const deviceControlPublicKeySpkiB64 = (seed) => deviceAccessPublicKeySpkiB64(seed);
63
62
  /**
@@ -74,7 +73,7 @@ export const controlKeepOuterKey = (dek, userId) => {
74
73
  if (userId.length === 0) {
75
74
  throw new Error("control keep requires a non-empty userId");
76
75
  }
77
- return hkdfSha256(dek, new TextEncoder().encode(userId), DEVICE_CONTROL_KEEP_HKDF_INFO, SEED_BYTES);
76
+ return hkdfSha256(dek, new TextEncoder().encode(userId), DEVICE_CONTROL_KEEP_HKDF_INFO, DEVICE_CONTROL_SEED_BYTES);
78
77
  };
79
78
  /**
80
79
  * Sign a rotation statement with the OLD control seed, vouching for `new`.
@@ -1,59 +1,32 @@
1
- import { aesGcmDecrypt, aesGcmEncrypt, fromB64, hkdfSha256, randomBytes, toB64, zero, } from "./crypto.js";
2
- import { generateX25519Keypair } from "./handoff.js";
1
+ import { decodeCanonicalB64, openX25519, sealX25519 } from "./x25519-seal.js";
3
2
  const DOMAIN = "openllm-connector-seal-v1";
4
3
  const aad = (binding) => new TextEncoder().encode(JSON.stringify([DOMAIN, binding.clientId, binding.userId, binding.grantId]));
5
- const decode = (value) => {
6
- if (typeof value !== "string" ||
7
- !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value))
8
- throw new Error("Invalid sealed envelope encoding");
9
- const bytes = fromB64(value);
10
- if (toB64(bytes) !== value)
11
- throw new Error("Invalid sealed envelope encoding");
12
- return bytes;
13
- };
14
- const deriveKey = async (privateKey, peerB64, epk, recipient) => {
15
- const peer = await crypto.subtle.importKey("spki", decode(peerB64), { name: "X25519" }, false, []);
16
- const shared = new Uint8Array(await crypto.subtle.deriveBits({ name: "X25519", public: peer }, privateKey, 256));
17
- try {
18
- // JSON array framing makes the public-key boundaries unambiguous.
19
- return await hkdfSha256(shared, new Uint8Array(0), JSON.stringify([DOMAIN, epk, recipient]), 32);
20
- }
21
- finally {
22
- zero(shared);
23
- }
24
- };
25
4
  /** Caller retains ownership of plaintext; the temporary copy is wiped. */
26
- export const sealToPublicKey = async (binding, plaintext) => {
27
- const eph = await generateX25519Keypair();
28
- const key = await deriveKey(eph.privateKey, binding.recipientPublicKeyB64, eph.publicKeyB64, binding.recipientPublicKeyB64);
29
- const copy = new Uint8Array(plaintext);
30
- try {
31
- const nonce = randomBytes(12);
32
- return {
33
- v: 1,
34
- epk: eph.publicKeyB64,
35
- nonce: toB64(nonce),
36
- ct: toB64(await aesGcmEncrypt(key, nonce, copy, aad(binding))),
37
- };
38
- }
39
- finally {
40
- zero(key);
41
- zero(copy);
42
- }
43
- };
5
+ export const sealToPublicKey = async (binding, plaintext) => sealX25519({
6
+ domain: DOMAIN,
7
+ recipientB64: binding.recipientPublicKeyB64,
8
+ recipientBytes: decodeCanonicalB64(binding.recipientPublicKeyB64),
9
+ format: "spki",
10
+ aad: aad(binding),
11
+ plaintext,
12
+ });
44
13
  /** Caller owns the returned plaintext and must wipe it when finished. */
45
14
  export const openSealed = async (binding, sealed, privateKey) => {
46
15
  if (typeof sealed !== "object" || sealed === null || sealed.v !== 1)
47
16
  throw new Error("Invalid sealed envelope");
48
- const nonce = decode(sealed.nonce);
49
- const ct = decode(sealed.ct);
17
+ const nonce = decodeCanonicalB64(sealed.nonce);
18
+ const ct = decodeCanonicalB64(sealed.ct);
50
19
  if (nonce.length !== 12 || ct.length < 16 || typeof sealed.epk !== "string")
51
20
  throw new Error("Invalid sealed envelope");
52
- const key = await deriveKey(privateKey, sealed.epk, sealed.epk, binding.recipientPublicKeyB64);
53
- try {
54
- return await aesGcmDecrypt(key, nonce, ct, aad(binding));
55
- }
56
- finally {
57
- zero(key);
58
- }
21
+ return openX25519({
22
+ domain: DOMAIN,
23
+ privateKey,
24
+ recipientB64: binding.recipientPublicKeyB64,
25
+ epkBytes: decodeCanonicalB64(sealed.epk),
26
+ epkB64: sealed.epk,
27
+ format: "spki",
28
+ aad: aad(binding),
29
+ nonce,
30
+ ct,
31
+ });
59
32
  };
@@ -19,7 +19,8 @@
19
19
  * The box side mirrors `open_unlock` in
20
20
  * `packages/api/lib/routebox/box-agent.ts` (Python).
21
21
  */
22
- import { aesGcmDecrypt, aesGcmEncrypt, fromB64, hkdfSha256, randomBytes, toB64, zero, } from "./crypto.js";
22
+ import { fromB64, toB64, zero } from "./crypto.js";
23
+ import { openX25519, sealX25519 } from "./x25519-seal.js";
23
24
  export const ROUTEBOX_SEAL_DOMAIN = "openllm-routebox-seal-v1";
24
25
  const encoder = new TextEncoder();
25
26
  /** Canonical associated data; the Python side builds the same bytes. */
@@ -39,37 +40,21 @@ const raw32 = (b64, what) => {
39
40
  }
40
41
  return bytes;
41
42
  };
42
- const deriveKey = async (privateKey, peerRaw, epkB64, recipientB64) => {
43
- const peer = await crypto.subtle.importKey("raw", peerRaw, { name: "X25519" }, false, []);
44
- const shared = new Uint8Array(await crypto.subtle.deriveBits({ name: "X25519", public: peer }, privateKey, 256));
45
- try {
46
- return await hkdfSha256(shared, new Uint8Array(0), JSON.stringify([ROUTEBOX_SEAL_DOMAIN, epkB64, recipientB64]), 32);
47
- }
48
- finally {
49
- zero(shared);
50
- }
51
- };
52
43
  /** Seal `unlock` to the boot in `binding`. */
53
44
  export const sealRouteboxUnlock = async (binding, unlock) => {
54
45
  raw32(binding.challengeB64, "challenge");
55
- const recipient = raw32(binding.bootPubkeyB64, "boot public key");
56
- const eph = (await crypto.subtle.generateKey({ name: "X25519" }, true, [
57
- "deriveBits",
58
- ]));
59
- const epkB64 = toB64(new Uint8Array(await crypto.subtle.exportKey("raw", eph.publicKey)));
60
- const key = await deriveKey(eph.privateKey, recipient, epkB64, binding.bootPubkeyB64);
61
46
  const plaintext = encoder.encode(JSON.stringify(unlock));
62
47
  try {
63
- const nonce = randomBytes(12);
64
- return {
65
- v: 1,
66
- epk: epkB64,
67
- nonce: toB64(nonce),
68
- ct: toB64(await aesGcmEncrypt(key, nonce, plaintext, routeboxSealAad(binding))),
69
- };
48
+ return await sealX25519({
49
+ domain: ROUTEBOX_SEAL_DOMAIN,
50
+ recipientB64: binding.bootPubkeyB64,
51
+ recipientBytes: raw32(binding.bootPubkeyB64, "boot public key"),
52
+ format: "raw",
53
+ aad: routeboxSealAad(binding),
54
+ plaintext,
55
+ });
70
56
  }
71
57
  finally {
72
- zero(key);
73
58
  zero(plaintext);
74
59
  }
75
60
  };
@@ -77,17 +62,30 @@ export const sealRouteboxUnlock = async (binding, unlock) => {
77
62
  export const openRouteboxUnlock = async (binding, envelope, bootPrivateKey) => {
78
63
  if (envelope.v !== 1)
79
64
  throw new Error("unknown envelope version");
80
- const key = await deriveKey(bootPrivateKey, raw32(envelope.epk, "ephemeral key"), envelope.epk, binding.bootPubkeyB64);
65
+ const plaintext = await openX25519({
66
+ domain: ROUTEBOX_SEAL_DOMAIN,
67
+ privateKey: bootPrivateKey,
68
+ recipientB64: binding.bootPubkeyB64,
69
+ epkBytes: raw32(envelope.epk, "ephemeral key"),
70
+ epkB64: envelope.epk,
71
+ format: "raw",
72
+ aad: routeboxSealAad(binding),
73
+ nonce: fromB64(envelope.nonce),
74
+ ct: fromB64(envelope.ct),
75
+ });
81
76
  try {
82
- const plaintext = await aesGcmDecrypt(key, fromB64(envelope.nonce), fromB64(envelope.ct), routeboxSealAad(binding));
83
- try {
84
- return JSON.parse(new TextDecoder().decode(plaintext));
85
- }
86
- finally {
87
- zero(plaintext);
77
+ const parsed = JSON.parse(new TextDecoder().decode(plaintext));
78
+ if (typeof parsed !== "object" ||
79
+ parsed === null ||
80
+ !("home_key" in parsed) ||
81
+ !("api_key" in parsed) ||
82
+ typeof parsed.home_key !== "string" ||
83
+ typeof parsed.api_key !== "string") {
84
+ throw new Error("malformed routebox unlock payload");
88
85
  }
86
+ return { home_key: parsed.home_key, api_key: parsed.api_key };
89
87
  }
90
88
  finally {
91
- zero(key);
89
+ zero(plaintext);
92
90
  }
93
91
  };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * The one ephemeral-X25519 + HKDF-SHA-256 + AES-256-GCM seal behind
3
+ * `public-key-seal.ts`, `cli-auth-seal.ts` and `routebox-seal.ts`.
4
+ *
5
+ * Callers differ only in their domain string, associated data and the
6
+ * encoding of the recipient / ephemeral public key (`spki` DER or `raw`
7
+ * 32 bytes); the key derivation and ciphertext layout are shared, so every
8
+ * wire format stays exactly as each seal documents it.
9
+ */
10
+ export type TX25519KeyFormat = "spki" | "raw";
11
+ export type TX25519Sealed = {
12
+ readonly v: 1;
13
+ readonly epk: string;
14
+ readonly nonce: string;
15
+ readonly ct: string;
16
+ };
17
+ /** The HKDF + ECDH half: both sides derive the same 32-byte AES key. */
18
+ export declare const deriveX25519SealKey: (params: {
19
+ readonly domain: string;
20
+ readonly privateKey: CryptoKey;
21
+ readonly peerKeyBytes: Uint8Array;
22
+ readonly peerFormat: TX25519KeyFormat;
23
+ readonly epkB64: string;
24
+ readonly recipientB64: string;
25
+ }) => Promise<Uint8Array>;
26
+ /** Seal `plaintext` to `recipientB64`; the caller keeps ownership of it. */
27
+ export declare const sealX25519: (params: {
28
+ readonly domain: string;
29
+ readonly recipientB64: string;
30
+ readonly recipientBytes: Uint8Array;
31
+ readonly format: TX25519KeyFormat;
32
+ readonly aad: Uint8Array;
33
+ readonly plaintext: Uint8Array;
34
+ }) => Promise<TX25519Sealed>;
35
+ /** Open an envelope; the caller owns the returned plaintext and wipes it. */
36
+ export declare const openX25519: (params: {
37
+ readonly domain: string;
38
+ readonly privateKey: CryptoKey;
39
+ readonly recipientB64: string;
40
+ readonly epkBytes: Uint8Array;
41
+ readonly epkB64: string;
42
+ readonly format: TX25519KeyFormat;
43
+ readonly aad: Uint8Array;
44
+ readonly nonce: Uint8Array;
45
+ readonly ct: Uint8Array;
46
+ }) => Promise<Uint8Array>;
47
+ /** Strict canonical-base64 decode shared by the SPKI-format seals. */
48
+ export declare const decodeCanonicalB64: (value: unknown) => Uint8Array;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * The one ephemeral-X25519 + HKDF-SHA-256 + AES-256-GCM seal behind
3
+ * `public-key-seal.ts`, `cli-auth-seal.ts` and `routebox-seal.ts`.
4
+ *
5
+ * Callers differ only in their domain string, associated data and the
6
+ * encoding of the recipient / ephemeral public key (`spki` DER or `raw`
7
+ * 32 bytes); the key derivation and ciphertext layout are shared, so every
8
+ * wire format stays exactly as each seal documents it.
9
+ */
10
+ import { aesGcmDecrypt, aesGcmEncrypt, fromB64, hkdfSha256, randomBytes, toB64, zero, } from "./crypto.js";
11
+ /** The HKDF + ECDH half: both sides derive the same 32-byte AES key. */
12
+ export const deriveX25519SealKey = async (params) => {
13
+ const peer = await crypto.subtle.importKey(params.peerFormat, params.peerKeyBytes, { name: "X25519" }, false, []);
14
+ const shared = new Uint8Array(await crypto.subtle.deriveBits({ name: "X25519", public: peer }, params.privateKey, 256));
15
+ try {
16
+ // JSON array framing makes the public-key boundaries unambiguous.
17
+ return await hkdfSha256(shared, new Uint8Array(0), JSON.stringify([params.domain, params.epkB64, params.recipientB64]), 32);
18
+ }
19
+ finally {
20
+ zero(shared);
21
+ }
22
+ };
23
+ /** Seal `plaintext` to `recipientB64`; the caller keeps ownership of it. */
24
+ export const sealX25519 = async (params) => {
25
+ const eph = (await crypto.subtle.generateKey({ name: "X25519" }, false, [
26
+ "deriveBits",
27
+ ]));
28
+ const epk = toB64(new Uint8Array(await crypto.subtle.exportKey(params.format, eph.publicKey)));
29
+ const key = await deriveX25519SealKey({
30
+ domain: params.domain,
31
+ privateKey: eph.privateKey,
32
+ peerKeyBytes: params.recipientBytes,
33
+ peerFormat: params.format,
34
+ epkB64: epk,
35
+ recipientB64: params.recipientB64,
36
+ });
37
+ const copy = new Uint8Array(params.plaintext);
38
+ try {
39
+ const nonce = randomBytes(12);
40
+ return {
41
+ v: 1,
42
+ epk,
43
+ nonce: toB64(nonce),
44
+ ct: toB64(await aesGcmEncrypt(key, nonce, copy, params.aad)),
45
+ };
46
+ }
47
+ finally {
48
+ zero(key);
49
+ zero(copy);
50
+ }
51
+ };
52
+ /** Open an envelope; the caller owns the returned plaintext and wipes it. */
53
+ export const openX25519 = async (params) => {
54
+ const key = await deriveX25519SealKey({
55
+ domain: params.domain,
56
+ privateKey: params.privateKey,
57
+ peerKeyBytes: params.epkBytes,
58
+ peerFormat: params.format,
59
+ epkB64: params.epkB64,
60
+ recipientB64: params.recipientB64,
61
+ });
62
+ try {
63
+ return await aesGcmDecrypt(key, params.nonce, params.ct, params.aad);
64
+ }
65
+ finally {
66
+ zero(key);
67
+ }
68
+ };
69
+ /** Strict canonical-base64 decode shared by the SPKI-format seals. */
70
+ export const decodeCanonicalB64 = (value) => {
71
+ if (typeof value !== "string" ||
72
+ !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value))
73
+ throw new Error("Invalid sealed envelope encoding");
74
+ const bytes = fromB64(value);
75
+ if (toB64(bytes) !== value)
76
+ throw new Error("Invalid sealed envelope encoding");
77
+ return bytes;
78
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openllmsh/sdk",
3
- "version": "2.10.19-alpha.1",
3
+ "version": "2.10.19-alpha.2",
4
4
  "private": false,
5
5
  "description": "The OpenLLM SDK: typed API contracts, wire-format adapters, the device tunnel, client-side vault crypto, the OpenAPI spec and MCP tools.",
6
6
  "keywords": [