@rstreamlabs/webtty 1.9.1 → 1.10.0

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/dist/index.js CHANGED
@@ -64,6 +64,8 @@ __export(index_exports, {
64
64
  verifyWebTTYServerProofTranscript: () => verifyWebTTYServerProofTranscript,
65
65
  webTTYClientProofTranscriptBytes: () => webTTYClientProofTranscriptBytes,
66
66
  webTTYE2EKeyID: () => webTTYE2EKeyID,
67
+ webTTYFIPSCompatibleKeyEnvelopeSuite: () => webTTYFIPSCompatibleKeyEnvelopeSuite,
68
+ webTTYFIPSCompatiblePayloadSuite: () => webTTYFIPSCompatiblePayloadSuite,
67
69
  webTTYRecordedEventEncryptedPayload: () => webTTYRecordedEventEncryptedPayload,
68
70
  webTTYServerProofTranscriptBytes: () => webTTYServerProofTranscriptBytes,
69
71
  webTTYSigningKeyID: () => webTTYSigningKeyID
@@ -980,6 +982,7 @@ var rstream = $root.rstream = (() => {
980
982
  values[valuesById[0] = "PAYLOAD_CIPHER_SUITE_UNSPECIFIED"] = 0;
981
983
  values[valuesById[1] = "PAYLOAD_CIPHER_SUITE_AES_256_GCM"] = 1;
982
984
  values[valuesById[2] = "PAYLOAD_CIPHER_SUITE_CHACHA20_POLY1305"] = 2;
985
+ values[valuesById[3] = "PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE"] = 3;
983
986
  return values;
984
987
  })();
985
988
  protobuf.KeyEnvelopeSuite = (function() {
@@ -987,6 +990,7 @@ var rstream = $root.rstream = (() => {
987
990
  values[valuesById[0] = "KEY_ENVELOPE_SUITE_UNSPECIFIED"] = 0;
988
991
  values[valuesById[1] = "KEY_ENVELOPE_SUITE_HPKE_X25519_HKDF_SHA256_AES_256_GCM"] = 1;
989
992
  values[valuesById[2] = "KEY_ENVELOPE_SUITE_HPKE_X25519_HKDF_SHA256_CHACHA20_POLY1305"] = 2;
993
+ values[valuesById[3] = "KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE"] = 3;
990
994
  return values;
991
995
  })();
992
996
  protobuf.ProtocolVersion = (function() {
@@ -1489,6 +1493,10 @@ var rstream = $root.rstream = (() => {
1489
1493
  case 2:
1490
1494
  message.keyEnvelopeSuite = 2;
1491
1495
  break;
1496
+ case "KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE":
1497
+ case 3:
1498
+ message.keyEnvelopeSuite = 3;
1499
+ break;
1492
1500
  default:
1493
1501
  if (typeof object.keyEnvelopeSuite === "number" && (object.keyEnvelopeSuite | 0) === object.keyEnvelopeSuite)
1494
1502
  message.keyEnvelopeSuite = object.keyEnvelopeSuite;
@@ -2173,6 +2181,10 @@ var rstream = $root.rstream = (() => {
2173
2181
  case 2:
2174
2182
  message.payloadSuites[message.payloadSuites.length] = 2;
2175
2183
  break;
2184
+ case "PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE":
2185
+ case 3:
2186
+ message.payloadSuites[message.payloadSuites.length] = 3;
2187
+ break;
2176
2188
  default:
2177
2189
  if (typeof object.payloadSuites[i] === "number" && (object.payloadSuites[i] | 0) === object.payloadSuites[i])
2178
2190
  message.payloadSuites[message.payloadSuites.length] = object.payloadSuites[i];
@@ -2196,6 +2208,10 @@ var rstream = $root.rstream = (() => {
2196
2208
  case 2:
2197
2209
  message.keyEnvelopeSuites[message.keyEnvelopeSuites.length] = 2;
2198
2210
  break;
2211
+ case "KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE":
2212
+ case 3:
2213
+ message.keyEnvelopeSuites[message.keyEnvelopeSuites.length] = 3;
2214
+ break;
2199
2215
  default:
2200
2216
  if (typeof object.keyEnvelopeSuites[i] === "number" && (object.keyEnvelopeSuites[i] | 0) === object.keyEnvelopeSuites[i])
2201
2217
  message.keyEnvelopeSuites[message.keyEnvelopeSuites.length] = object.keyEnvelopeSuites[i];
@@ -2669,6 +2685,10 @@ var rstream = $root.rstream = (() => {
2669
2685
  case 2:
2670
2686
  message.payloadSuite = 2;
2671
2687
  break;
2688
+ case "PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE":
2689
+ case 3:
2690
+ message.payloadSuite = 3;
2691
+ break;
2672
2692
  default:
2673
2693
  if (typeof object.payloadSuite === "number" && (object.payloadSuite | 0) === object.payloadSuite)
2674
2694
  message.payloadSuite = object.payloadSuite;
@@ -2713,6 +2733,10 @@ var rstream = $root.rstream = (() => {
2713
2733
  case 2:
2714
2734
  message.keyEnvelopeSuite = 2;
2715
2735
  break;
2736
+ case "KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE":
2737
+ case 3:
2738
+ message.keyEnvelopeSuite = 3;
2739
+ break;
2716
2740
  default:
2717
2741
  if (typeof object.keyEnvelopeSuite === "number" && (object.keyEnvelopeSuite | 0) === object.keyEnvelopeSuite)
2718
2742
  message.keyEnvelopeSuite = object.keyEnvelopeSuite;
@@ -2940,6 +2964,10 @@ var rstream = $root.rstream = (() => {
2940
2964
  case 2:
2941
2965
  message.payloadSuite = 2;
2942
2966
  break;
2967
+ case "PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE":
2968
+ case 3:
2969
+ message.payloadSuite = 3;
2970
+ break;
2943
2971
  default:
2944
2972
  if (typeof object.payloadSuite === "number" && (object.payloadSuite | 0) === object.payloadSuite)
2945
2973
  message.payloadSuite = object.payloadSuite;
@@ -7450,6 +7478,7 @@ function authRequirementCode(value) {
7450
7478
  function payloadSuiteCode(value) {
7451
7479
  if (value === "aes-256-gcm" || value === void 0) return 1;
7452
7480
  if (value === "chacha20-poly1305") return 2;
7481
+ if (value === "aes-256-gcm-random-nonce") return 3;
7453
7482
  return 0;
7454
7483
  }
7455
7484
  function keyEnvelopeSuiteCode(value) {
@@ -7457,6 +7486,7 @@ function keyEnvelopeSuiteCode(value) {
7457
7486
  return 1;
7458
7487
  }
7459
7488
  if (value === "hpke-x25519-hkdf-sha256-chacha20-poly1305") return 2;
7489
+ if (value === "p256-hkdf-sha256-aes-256-gcm-random-nonce") return 3;
7460
7490
  return 0;
7461
7491
  }
7462
7492
  function signatureSuiteCode(value) {
@@ -7657,6 +7687,8 @@ function normalizeSessionKeyGrant(grant) {
7657
7687
  // src/e2e-crypto.ts
7658
7688
  var x25519PublicKeySize = 32;
7659
7689
  var x25519PrivateKeySize = 32;
7690
+ var p256PublicKeySize = 65;
7691
+ var p256PrivateKeySize = 32;
7660
7692
  var payloadKeySize = 32;
7661
7693
  var payloadKeyIDSize = 16;
7662
7694
  var aesGCMNonceSize = 12;
@@ -7669,21 +7701,24 @@ var keyIDDomain = "rstream-webtty-e2e-key-id/v1";
7669
7701
  var hpkeVersionLabel = "HPKE-v1";
7670
7702
  var nominalPayloadSuite = "aes-256-gcm";
7671
7703
  var nominalKeyEnvelopeSuite = "hpke-x25519-hkdf-sha256-aes-256-gcm";
7672
- async function generateWebTTYE2EIdentity() {
7673
- const pair = await getSubtle().generateKey({ name: "X25519" }, true, [
7674
- "deriveBits"
7675
- ]);
7676
- if (!isX25519KeyPair(pair)) {
7677
- throw new Error("WebCrypto returned an unexpected X25519 key pair");
7704
+ var webTTYFIPSCompatiblePayloadSuite = "aes-256-gcm-random-nonce";
7705
+ var webTTYFIPSCompatibleKeyEnvelopeSuite = "p256-hkdf-sha256-aes-256-gcm-random-nonce";
7706
+ async function generateWebTTYE2EIdentity(keyEnvelopeSuite = nominalKeyEnvelopeSuite) {
7707
+ payloadSuiteForKeyEnvelopeSuite(keyEnvelopeSuite);
7708
+ const algorithm = keyEnvelopeSuite === webTTYFIPSCompatibleKeyEnvelopeSuite ? { name: "ECDH", namedCurve: "P-256" } : { name: "X25519" };
7709
+ const pair = await getSubtle().generateKey(algorithm, true, ["deriveBits"]);
7710
+ if (!isCryptoKeyPair(pair)) {
7711
+ throw new Error("WebCrypto returned an unexpected E2E key pair");
7678
7712
  }
7679
7713
  const publicKey = new Uint8Array(
7680
7714
  await getSubtle().exportKey("raw", pair.publicKey)
7681
7715
  );
7682
7716
  const jwk = await getSubtle().exportKey("jwk", pair.privateKey);
7683
7717
  if (typeof jwk.d !== "string") {
7684
- throw new Error("WebCrypto X25519 private JWK is missing d");
7718
+ throw new Error("WebCrypto E2E private JWK is missing d");
7685
7719
  }
7686
7720
  return {
7721
+ keyEnvelopeSuite,
7687
7722
  keyId: await webTTYE2EKeyID(publicKey),
7688
7723
  privateKey: base64URLDecode(jwk.d),
7689
7724
  publicKey
@@ -7724,8 +7759,8 @@ function createWebTTYE2EKeyContext(config) {
7724
7759
  );
7725
7760
  }
7726
7761
  async function createWebTTYE2EClientPayloadCrypto(config) {
7727
- const payloadSuite = config.payloadSuite ?? nominalPayloadSuite;
7728
- const keyEnvelopeSuite = config.keyEnvelopeSuite ?? nominalKeyEnvelopeSuite;
7762
+ const keyEnvelopeSuite = config.keyEnvelopeSuite ?? inferRecipientSuite(config.recipients);
7763
+ const payloadSuite = config.payloadSuite ?? payloadSuiteForKeyEnvelopeSuite(keyEnvelopeSuite);
7729
7764
  validateSuites(payloadSuite, keyEnvelopeSuite);
7730
7765
  const payloadKey = config.payloadKey ? cloneKeyMaterial(config.payloadKey) : randomBytes(payloadKeySize);
7731
7766
  if (payloadKey.byteLength !== payloadKeySize) {
@@ -7817,7 +7852,7 @@ function createPayloadCrypto(options) {
7817
7852
  payloadSuite: options.payloadSuite
7818
7853
  };
7819
7854
  const cryptoInfo = {
7820
- keyAgreement: "HPKE X25519",
7855
+ keyAgreement: options.keyEnvelopeSuite === webTTYFIPSCompatibleKeyEnvelopeSuite ? "ECDH P-256" : "HPKE X25519",
7821
7856
  keyDerivation: "HKDF-SHA256",
7822
7857
  keyEncryption: "AES-256-GCM",
7823
7858
  keyEnvelopeSuite: options.keyEnvelopeSuite,
@@ -7832,7 +7867,7 @@ function createPayloadCrypto(options) {
7832
7867
  if (chunk.byteLength > 4294967295) {
7833
7868
  throw new Error(`E2E payload is too large: ${chunk.byteLength} bytes`);
7834
7869
  }
7835
- const nonce = randomBytes(aesGCMNonceSize);
7870
+ const nonce = options.payloadSuite === webTTYFIPSCompatiblePayloadSuite ? new Uint8Array() : randomBytes(aesGCMNonceSize);
7836
7871
  const payloadCrypto = {
7837
7872
  aadContext: cloneBytes(options.keyContext),
7838
7873
  nonce,
@@ -7840,7 +7875,8 @@ function createPayloadCrypto(options) {
7840
7875
  payloadSuite: options.payloadSuite
7841
7876
  };
7842
7877
  return {
7843
- ciphertext: await aesGCMEncrypt(
7878
+ ciphertext: await encryptPayloadForSuite(
7879
+ options.payloadSuite,
7844
7880
  options.payloadKey,
7845
7881
  nonce,
7846
7882
  cloneBytes(chunk),
@@ -7860,19 +7896,21 @@ function createPayloadCrypto(options) {
7860
7896
  const decrypt = (stream) => async (payload) => {
7861
7897
  validatePayloadEnvelope(payload, options);
7862
7898
  const crypto = payload.payloadCrypto;
7863
- if (crypto === void 0 || crypto.nonce === void 0) {
7864
- throw new Error("missing E2E payload nonce");
7899
+ if (crypto === void 0) {
7900
+ throw new Error("missing E2E payload crypto");
7865
7901
  }
7866
- const plaintext = await aesGCMDecrypt(
7902
+ const nonce = crypto.nonce ?? new Uint8Array();
7903
+ const plaintext = await decryptPayloadForSuite(
7904
+ options.payloadSuite,
7867
7905
  options.payloadKey,
7868
- crypto.nonce,
7906
+ nonce,
7869
7907
  payload.ciphertext,
7870
7908
  payloadAAD(
7871
7909
  stream,
7872
7910
  options.payloadSuite,
7873
7911
  options.payloadKeyId,
7874
7912
  options.keyContext,
7875
- crypto.nonce,
7913
+ nonce,
7876
7914
  payload.plaintextLength
7877
7915
  )
7878
7916
  );
@@ -7917,21 +7955,29 @@ async function implicitKeyContextFromRecipients(recipients) {
7917
7955
  }
7918
7956
  async function wrapPayloadKey(payloadKey, payloadSuite, payloadKeyId, keyContext, suite, recipient) {
7919
7957
  const recipientPublicKey = cloneKeyMaterial(recipient.publicKey);
7920
- if (recipientPublicKey.byteLength !== x25519PublicKeySize) {
7958
+ const recipientSuite = recipient.keyEnvelopeSuite ?? inferKeyEnvelopeSuite(recipientPublicKey);
7959
+ if (recipientSuite !== suite) {
7960
+ throw new Error("E2E recipient suite does not match key envelope suite");
7961
+ }
7962
+ const expectedPublicKeySize = publicKeySizeForSuite(suite);
7963
+ if (recipientPublicKey.byteLength !== expectedPublicKeySize) {
7921
7964
  throw new Error(
7922
- `E2E recipient public key must be ${x25519PublicKeySize} bytes`
7965
+ `E2E recipient public key must be ${expectedPublicKeySize} bytes`
7923
7966
  );
7924
7967
  }
7925
7968
  const recipientKeyId = recipient.keyId ? cloneKeyMaterial(recipient.keyId) : await webTTYE2EKeyID(recipientPublicKey);
7926
7969
  if (recipientKeyId.byteLength !== payloadKeyIDSize) {
7927
7970
  throw new Error(`E2E recipient key id must be ${payloadKeyIDSize} bytes`);
7928
7971
  }
7929
- const sealed = await hpkeSeal(
7930
- recipientPublicKey,
7931
- hpkeInfo(payloadSuite, payloadKeyId, keyContext, suite),
7932
- hpkeAAD(recipientKeyId, payloadSuite, payloadKeyId, keyContext, suite),
7933
- payloadKey
7972
+ const info = hpkeInfo(payloadSuite, payloadKeyId, keyContext, suite);
7973
+ const aad = hpkeAAD(
7974
+ recipientKeyId,
7975
+ payloadSuite,
7976
+ payloadKeyId,
7977
+ keyContext,
7978
+ suite
7934
7979
  );
7980
+ const sealed = suite === webTTYFIPSCompatibleKeyEnvelopeSuite ? await p256Seal(recipientPublicKey, info, aad, payloadKey) : await hpkeSeal(recipientPublicKey, info, aad, payloadKey);
7935
7981
  return {
7936
7982
  encapsulatedKey: sealed.encapsulatedKey,
7937
7983
  recipientKeyId,
@@ -7948,17 +7994,28 @@ async function unwrapPayloadKey(envelope, payloadSuite, payloadKeyId, keyContext
7948
7994
  if (envelope.wrappedKey === void 0) {
7949
7995
  throw new Error("E2E key envelope is missing wrapped key");
7950
7996
  }
7951
- const payloadKey = await hpkeOpen(
7997
+ if (identity.keyEnvelopeSuite !== suite) {
7998
+ throw new Error("E2E identity suite does not match key envelope suite");
7999
+ }
8000
+ const info = hpkeInfo(payloadSuite, payloadKeyId, keyContext, suite);
8001
+ const aad = hpkeAAD(
8002
+ envelope.recipientKeyId,
8003
+ payloadSuite,
8004
+ payloadKeyId,
8005
+ keyContext,
8006
+ suite
8007
+ );
8008
+ const payloadKey = suite === webTTYFIPSCompatibleKeyEnvelopeSuite ? await p256Open(
7952
8009
  identity,
7953
8010
  envelope.encapsulatedKey,
7954
- hpkeInfo(payloadSuite, payloadKeyId, keyContext, suite),
7955
- hpkeAAD(
7956
- envelope.recipientKeyId,
7957
- payloadSuite,
7958
- payloadKeyId,
7959
- keyContext,
7960
- suite
7961
- ),
8011
+ info,
8012
+ aad,
8013
+ envelope.wrappedKey
8014
+ ) : await hpkeOpen(
8015
+ identity,
8016
+ envelope.encapsulatedKey,
8017
+ info,
8018
+ aad,
7962
8019
  envelope.wrappedKey
7963
8020
  );
7964
8021
  if (payloadKey.byteLength !== payloadKeySize) {
@@ -8040,6 +8097,95 @@ async function hpkeOpen(identity, encapsulatedKey, info, aad, ciphertext) {
8040
8097
  const schedule = await hpkeKeySchedule(sharedSecret, info);
8041
8098
  return aesGCMDecrypt(schedule.key, schedule.baseNonce, ciphertext, aad);
8042
8099
  }
8100
+ async function p256Seal(publicKey, info, aad, plaintext) {
8101
+ const recipientPublic = await getSubtle().importKey(
8102
+ "raw",
8103
+ bufferSource(publicKey),
8104
+ { name: "ECDH", namedCurve: "P-256" },
8105
+ false,
8106
+ []
8107
+ );
8108
+ const ephemeral = await getSubtle().generateKey(
8109
+ { name: "ECDH", namedCurve: "P-256" },
8110
+ true,
8111
+ ["deriveBits"]
8112
+ );
8113
+ if (!isCryptoKeyPair(ephemeral)) {
8114
+ throw new Error("WebCrypto returned an unexpected P-256 key pair");
8115
+ }
8116
+ const encapsulatedKey = new Uint8Array(
8117
+ await getSubtle().exportKey("raw", ephemeral.publicKey)
8118
+ );
8119
+ const sharedSecret = new Uint8Array(
8120
+ await getSubtle().deriveBits(
8121
+ { name: "ECDH", public: recipientPublic },
8122
+ ephemeral.privateKey,
8123
+ 256
8124
+ )
8125
+ );
8126
+ const wrappingKey = await p256WrappingKey(sharedSecret, info);
8127
+ return {
8128
+ encapsulatedKey,
8129
+ wrappedKey: await aesGCMEncryptWithPrefixedRandomNonce(
8130
+ wrappingKey,
8131
+ plaintext,
8132
+ aad
8133
+ )
8134
+ };
8135
+ }
8136
+ async function p256Open(identity, encapsulatedKey, info, aad, ciphertext) {
8137
+ if (identity.privateKey.byteLength !== p256PrivateKeySize) {
8138
+ throw new Error(
8139
+ `E2E identity private key must be ${p256PrivateKeySize} bytes`
8140
+ );
8141
+ }
8142
+ if (identity.publicKey.byteLength !== p256PublicKeySize || identity.publicKey[0] !== 4) {
8143
+ throw new Error(
8144
+ `E2E identity public key must be an uncompressed ${p256PublicKeySize}-byte P-256 point`
8145
+ );
8146
+ }
8147
+ const recipientPrivate = await importP256PrivateKey(identity);
8148
+ const ephemeralPublic = await getSubtle().importKey(
8149
+ "raw",
8150
+ bufferSource(encapsulatedKey),
8151
+ { name: "ECDH", namedCurve: "P-256" },
8152
+ false,
8153
+ []
8154
+ );
8155
+ const sharedSecret = new Uint8Array(
8156
+ await getSubtle().deriveBits(
8157
+ { name: "ECDH", public: ephemeralPublic },
8158
+ recipientPrivate,
8159
+ 256
8160
+ )
8161
+ );
8162
+ return aesGCMDecryptWithPrefixedRandomNonce(
8163
+ await p256WrappingKey(sharedSecret, info),
8164
+ ciphertext,
8165
+ aad
8166
+ );
8167
+ }
8168
+ async function p256WrappingKey(sharedSecret, info) {
8169
+ const key = await getSubtle().importKey(
8170
+ "raw",
8171
+ bufferSource(sharedSecret),
8172
+ "HKDF",
8173
+ false,
8174
+ ["deriveBits"]
8175
+ );
8176
+ return new Uint8Array(
8177
+ await getSubtle().deriveBits(
8178
+ {
8179
+ hash: "SHA-256",
8180
+ info: bufferSource(info),
8181
+ name: "HKDF",
8182
+ salt: new Uint8Array()
8183
+ },
8184
+ key,
8185
+ 256
8186
+ )
8187
+ );
8188
+ }
8043
8189
  async function dhkemExtractAndExpand(dh, kemContext) {
8044
8190
  const eaePRK = await labeledExtract(
8045
8191
  dhkemSuiteID(),
@@ -8192,6 +8338,33 @@ async function aesGCMDecrypt(key, nonce, ciphertext, aad) {
8192
8338
  )
8193
8339
  );
8194
8340
  }
8341
+ async function aesGCMEncryptWithPrefixedRandomNonce(key, plaintext, aad) {
8342
+ const nonce = randomBytes(aesGCMNonceSize);
8343
+ return concat2(nonce, await aesGCMEncrypt(key, nonce, plaintext, aad));
8344
+ }
8345
+ async function aesGCMDecryptWithPrefixedRandomNonce(key, ciphertext, aad) {
8346
+ if (ciphertext.byteLength < aesGCMNonceSize + 16) {
8347
+ throw new Error("E2E random-nonce AES-GCM ciphertext is too short");
8348
+ }
8349
+ return aesGCMDecrypt(
8350
+ key,
8351
+ ciphertext.slice(0, aesGCMNonceSize),
8352
+ ciphertext.slice(aesGCMNonceSize),
8353
+ aad
8354
+ );
8355
+ }
8356
+ async function encryptPayloadForSuite(suite, key, nonce, plaintext, aad) {
8357
+ if (suite === webTTYFIPSCompatiblePayloadSuite) {
8358
+ return aesGCMEncryptWithPrefixedRandomNonce(key, plaintext, aad);
8359
+ }
8360
+ return aesGCMEncrypt(key, nonce, plaintext, aad);
8361
+ }
8362
+ async function decryptPayloadForSuite(suite, key, nonce, ciphertext, aad) {
8363
+ if (suite === webTTYFIPSCompatiblePayloadSuite) {
8364
+ return aesGCMDecryptWithPrefixedRandomNonce(key, ciphertext, aad);
8365
+ }
8366
+ return aesGCMDecrypt(key, nonce, ciphertext, aad);
8367
+ }
8195
8368
  async function importX25519PrivateKey(identity) {
8196
8369
  return getSubtle().importKey(
8197
8370
  "jwk",
@@ -8208,6 +8381,23 @@ async function importX25519PrivateKey(identity) {
8208
8381
  ["deriveBits"]
8209
8382
  );
8210
8383
  }
8384
+ async function importP256PrivateKey(identity) {
8385
+ return getSubtle().importKey(
8386
+ "jwk",
8387
+ {
8388
+ crv: "P-256",
8389
+ d: base64URLEncode(identity.privateKey),
8390
+ ext: true,
8391
+ key_ops: ["deriveBits"],
8392
+ kty: "EC",
8393
+ x: base64URLEncode(identity.publicKey.slice(1, 33)),
8394
+ y: base64URLEncode(identity.publicKey.slice(33, 65))
8395
+ },
8396
+ { name: "ECDH", namedCurve: "P-256" },
8397
+ false,
8398
+ ["deriveBits"]
8399
+ );
8400
+ }
8211
8401
  function validatePayloadEnvelope(payload, options) {
8212
8402
  const crypto = payload.payloadCrypto;
8213
8403
  if (crypto === void 0) throw new Error("missing E2E payload crypto");
@@ -8220,18 +8410,64 @@ function validatePayloadEnvelope(payload, options) {
8220
8410
  if (!bytesEqual(crypto.aadContext, options.keyContext)) {
8221
8411
  throw new Error("unexpected E2E key context");
8222
8412
  }
8223
- if (crypto.nonce === void 0 || crypto.nonce.byteLength !== aesGCMNonceSize) {
8224
- throw new Error(`E2E AES-GCM nonce must be ${aesGCMNonceSize} bytes`);
8413
+ const expectedNonceSize = options.payloadSuite === webTTYFIPSCompatiblePayloadSuite ? 0 : aesGCMNonceSize;
8414
+ if (crypto.nonce === void 0 || crypto.nonce.byteLength !== expectedNonceSize) {
8415
+ throw new Error(`E2E AES-GCM nonce must be ${expectedNonceSize} bytes`);
8225
8416
  }
8226
8417
  }
8227
- function validateSuites(payloadSuite, keyEnvelopeSuite) {
8228
- if (payloadSuite !== nominalPayloadSuite) {
8229
- throw new Error(`unsupported E2E payload suite ${payloadSuite}`);
8418
+ function inferRecipientSuite(recipients) {
8419
+ if (recipients.length === 0) {
8420
+ throw new Error(
8421
+ "E2E client payload crypto requires at least one recipient"
8422
+ );
8423
+ }
8424
+ const suites = recipients.map(
8425
+ (recipient) => recipient.keyEnvelopeSuite ?? inferKeyEnvelopeSuite(cloneKeyMaterial(recipient.publicKey))
8426
+ );
8427
+ const suite = suites[0];
8428
+ if (suite === void 0 || suites.some((candidate) => candidate !== suite)) {
8429
+ throw new Error("E2E recipients must use the same key envelope suite");
8430
+ }
8431
+ return suite;
8432
+ }
8433
+ function inferKeyEnvelopeSuite(publicKey) {
8434
+ if (publicKey.byteLength === x25519PublicKeySize) {
8435
+ return nominalKeyEnvelopeSuite;
8230
8436
  }
8231
- if (keyEnvelopeSuite !== nominalKeyEnvelopeSuite) {
8232
- throw new Error(`unsupported E2E key envelope suite ${keyEnvelopeSuite}`);
8437
+ if (publicKey.byteLength === p256PublicKeySize && publicKey[0] === 4) {
8438
+ return webTTYFIPSCompatibleKeyEnvelopeSuite;
8233
8439
  }
8234
- return { keyEnvelopeSuite, payloadSuite };
8440
+ throw new Error(
8441
+ `unsupported E2E recipient public key length ${publicKey.byteLength}`
8442
+ );
8443
+ }
8444
+ function payloadSuiteForKeyEnvelopeSuite(suite) {
8445
+ switch (suite) {
8446
+ case nominalKeyEnvelopeSuite:
8447
+ return nominalPayloadSuite;
8448
+ case webTTYFIPSCompatibleKeyEnvelopeSuite:
8449
+ return webTTYFIPSCompatiblePayloadSuite;
8450
+ default:
8451
+ throw new Error(`unsupported E2E key envelope suite ${suite}`);
8452
+ }
8453
+ }
8454
+ function publicKeySizeForSuite(suite) {
8455
+ switch (suite) {
8456
+ case nominalKeyEnvelopeSuite:
8457
+ return x25519PublicKeySize;
8458
+ case webTTYFIPSCompatibleKeyEnvelopeSuite:
8459
+ return p256PublicKeySize;
8460
+ default:
8461
+ throw new Error(`unsupported E2E key envelope suite ${suite}`);
8462
+ }
8463
+ }
8464
+ function validateSuites(payloadSuite, keyEnvelopeSuite) {
8465
+ if (payloadSuite === nominalPayloadSuite && keyEnvelopeSuite === nominalKeyEnvelopeSuite || payloadSuite === webTTYFIPSCompatiblePayloadSuite && keyEnvelopeSuite === webTTYFIPSCompatibleKeyEnvelopeSuite) {
8466
+ return { keyEnvelopeSuite, payloadSuite };
8467
+ }
8468
+ throw new Error(
8469
+ `unsupported E2E suite pair ${payloadSuite}/${keyEnvelopeSuite}`
8470
+ );
8235
8471
  }
8236
8472
  function hpkeInfo(payloadSuite, payloadKeyId, keyContext, suite) {
8237
8473
  return concat2(
@@ -8266,11 +8502,13 @@ function payloadAAD(stream, suite, payloadKeyId, keyContext, nonce, plaintextLen
8266
8502
  function payloadSuiteCode2(suite) {
8267
8503
  if (suite === "aes-256-gcm") return 1;
8268
8504
  if (suite === "chacha20-poly1305") return 2;
8505
+ if (suite === "aes-256-gcm-random-nonce") return 3;
8269
8506
  return unsupportedPayloadSuite(suite);
8270
8507
  }
8271
8508
  function keyEnvelopeSuiteCode2(suite) {
8272
8509
  if (suite === "hpke-x25519-hkdf-sha256-aes-256-gcm") return 1;
8273
8510
  if (suite === "hpke-x25519-hkdf-sha256-chacha20-poly1305") return 2;
8511
+ if (suite === "p256-hkdf-sha256-aes-256-gcm-random-nonce") return 3;
8274
8512
  return unsupportedKeyEnvelopeSuite(suite);
8275
8513
  }
8276
8514
  function unsupportedPayloadSuite(suite) {
@@ -8346,10 +8584,12 @@ function nonEmptyString(value, label) {
8346
8584
  return void 0;
8347
8585
  }
8348
8586
  function normalizeIdentity(identity) {
8587
+ const publicKey = cloneKeyMaterial(identity.publicKey);
8349
8588
  return {
8589
+ keyEnvelopeSuite: identity.keyEnvelopeSuite ?? inferKeyEnvelopeSuite(publicKey),
8350
8590
  keyId: cloneKeyMaterial(identity.keyId),
8351
8591
  privateKey: cloneKeyMaterial(identity.privateKey),
8352
- publicKey: cloneKeyMaterial(identity.publicKey)
8592
+ publicKey
8353
8593
  };
8354
8594
  }
8355
8595
  function bufferSource(value) {
@@ -8403,9 +8643,10 @@ function getSubtle() {
8403
8643
  }
8404
8644
  return subtle;
8405
8645
  }
8406
- function isX25519KeyPair(value) {
8646
+ function isCryptoKeyPair(value) {
8407
8647
  return "privateKey" in value && "publicKey" in value;
8408
8648
  }
8649
+ var isX25519KeyPair = isCryptoKeyPair;
8409
8650
  function base64URLEncode(value) {
8410
8651
  const binary = String.fromCharCode(...value);
8411
8652
  return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
@@ -8470,6 +8711,8 @@ function payloadCipherSuiteToProto(suite) {
8470
8711
  return rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_AES_256_GCM;
8471
8712
  case "chacha20-poly1305":
8472
8713
  return rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_CHACHA20_POLY1305;
8714
+ case "aes-256-gcm-random-nonce":
8715
+ return rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE;
8473
8716
  default:
8474
8717
  return rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_UNSPECIFIED;
8475
8718
  }
@@ -8480,6 +8723,8 @@ function payloadCipherSuiteFromProto(suite) {
8480
8723
  return "aes-256-gcm";
8481
8724
  case rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_CHACHA20_POLY1305:
8482
8725
  return "chacha20-poly1305";
8726
+ case rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE:
8727
+ return "aes-256-gcm-random-nonce";
8483
8728
  default:
8484
8729
  return void 0;
8485
8730
  }
@@ -8490,6 +8735,8 @@ function keyEnvelopeSuiteToProto(suite) {
8490
8735
  return rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_HPKE_X25519_HKDF_SHA256_AES_256_GCM;
8491
8736
  case "hpke-x25519-hkdf-sha256-chacha20-poly1305":
8492
8737
  return rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_HPKE_X25519_HKDF_SHA256_CHACHA20_POLY1305;
8738
+ case "p256-hkdf-sha256-aes-256-gcm-random-nonce":
8739
+ return rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE;
8493
8740
  default:
8494
8741
  return rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_UNSPECIFIED;
8495
8742
  }
@@ -8500,10 +8747,72 @@ function keyEnvelopeSuiteFromProto(suite) {
8500
8747
  return "hpke-x25519-hkdf-sha256-aes-256-gcm";
8501
8748
  case rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_HPKE_X25519_HKDF_SHA256_CHACHA20_POLY1305:
8502
8749
  return "hpke-x25519-hkdf-sha256-chacha20-poly1305";
8750
+ case rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE:
8751
+ return "p256-hkdf-sha256-aes-256-gcm-random-nonce";
8503
8752
  default:
8504
8753
  return void 0;
8505
8754
  }
8506
8755
  }
8756
+ var defaultE2EPayloadSuite = "aes-256-gcm";
8757
+ var defaultE2EKeyEnvelopeSuite = "hpke-x25519-hkdf-sha256-aes-256-gcm";
8758
+ var fipsCompatibleE2EPayloadSuite = "aes-256-gcm-random-nonce";
8759
+ var fipsCompatibleE2EKeyEnvelopeSuite = "p256-hkdf-sha256-aes-256-gcm-random-nonce";
8760
+ function payloadSuiteForKeyEnvelopeSuite2(suite) {
8761
+ switch (suite) {
8762
+ case defaultE2EKeyEnvelopeSuite:
8763
+ return defaultE2EPayloadSuite;
8764
+ case fipsCompatibleE2EKeyEnvelopeSuite:
8765
+ return fipsCompatibleE2EPayloadSuite;
8766
+ default:
8767
+ throw new Error(`Unsupported WebTTY E2E key envelope suite ${suite}.`);
8768
+ }
8769
+ }
8770
+ function asE2EKeyEnvelopeSuite(suite) {
8771
+ if (suite === defaultE2EKeyEnvelopeSuite || suite === fipsCompatibleE2EKeyEnvelopeSuite) {
8772
+ return suite;
8773
+ }
8774
+ throw new Error(`Unsupported WebTTY E2E key envelope suite ${suite}.`);
8775
+ }
8776
+ function inferE2EKeyEnvelopeSuite(publicKey) {
8777
+ if (publicKey?.byteLength === 32) return defaultE2EKeyEnvelopeSuite;
8778
+ if (publicKey?.byteLength === 65 && publicKey[0] === 4) {
8779
+ return fipsCompatibleE2EKeyEnvelopeSuite;
8780
+ }
8781
+ return void 0;
8782
+ }
8783
+ function proofSuitesFromGrant(grant) {
8784
+ const rawKeyEnvelopeSuite = keyEnvelopeSuiteFromProto(
8785
+ grant?.keyEnvelopeSuite
8786
+ );
8787
+ const payloadSuite = payloadCipherSuiteFromProto(grant?.payloadSuite);
8788
+ if (rawKeyEnvelopeSuite === void 0 && payloadSuite === void 0) {
8789
+ return {
8790
+ keyEnvelopeSuite: defaultE2EKeyEnvelopeSuite,
8791
+ payloadSuite: defaultE2EPayloadSuite
8792
+ };
8793
+ }
8794
+ if (rawKeyEnvelopeSuite === void 0 || payloadSuite === void 0) {
8795
+ throw new Error("WebTTY session key grant uses an unsupported suite pair.");
8796
+ }
8797
+ const keyEnvelopeSuite = asE2EKeyEnvelopeSuite(rawKeyEnvelopeSuite);
8798
+ const expectedPayloadSuite = payloadSuiteForKeyEnvelopeSuite2(keyEnvelopeSuite);
8799
+ if (expectedPayloadSuite !== payloadSuite) {
8800
+ throw new Error("WebTTY session key grant uses an unsupported suite pair.");
8801
+ }
8802
+ return { keyEnvelopeSuite, payloadSuite: expectedPayloadSuite };
8803
+ }
8804
+ function proofSuitesFromClientConfig(config) {
8805
+ const configured = config.endpointIdentity?.keyEnvelopeSuite;
8806
+ const expected = config.expectedServerIdentity;
8807
+ const expectedSuite = expected?.keyEnvelopeSuite ?? inferE2EKeyEnvelopeSuite(expected?.encryptionPublicKey);
8808
+ const keyEnvelopeSuite = asE2EKeyEnvelopeSuite(
8809
+ configured ?? expectedSuite ?? defaultE2EKeyEnvelopeSuite
8810
+ );
8811
+ return {
8812
+ keyEnvelopeSuite,
8813
+ payloadSuite: payloadSuiteForKeyEnvelopeSuite2(keyEnvelopeSuite)
8814
+ };
8815
+ }
8507
8816
  function signatureSuiteFromProto(suite) {
8508
8817
  switch (suite) {
8509
8818
  case rstream.webtty.protobuf.SignatureSuite.SIGNATURE_SUITE_ECDSA_P256_SHA256:
@@ -9184,6 +9493,16 @@ var WebTTY = class {
9184
9493
  if (!bytesEqual2(actual.signingKeyId, expected.signingKeyId) || !bytesEqual2(actual.signingPublicKey, expected.signingPublicKey) || !bytesEqual2(actual.encryptionKeyId, expected.encryptionKeyId) || !bytesEqual2(actual.encryptionPublicKey, expected.encryptionPublicKey)) {
9185
9494
  return false;
9186
9495
  }
9496
+ const expectedKeyEnvelopeSuite = expected.keyEnvelopeSuite ?? inferE2EKeyEnvelopeSuite(expected.encryptionPublicKey) ?? defaultE2EKeyEnvelopeSuite;
9497
+ const advertisedKeyEnvelopeSuites = (serverHello.keyEnvelopeSuites ?? []).map(keyEnvelopeSuiteFromProto).filter((value) => value !== void 0);
9498
+ const advertisedPayloadSuites = (serverHello.payloadSuites ?? []).map(payloadCipherSuiteFromProto).filter(
9499
+ (value) => value !== void 0
9500
+ );
9501
+ if (!advertisedKeyEnvelopeSuites.includes(expectedKeyEnvelopeSuite) || !advertisedPayloadSuites.includes(
9502
+ payloadSuiteForKeyEnvelopeSuite2(expectedKeyEnvelopeSuite)
9503
+ )) {
9504
+ return false;
9505
+ }
9187
9506
  const transcript = {
9188
9507
  authRequirement: authRequirementFromProto(serverHello.authRequirement),
9189
9508
  keyEnvelopeSuites: (serverHello.keyEnvelopeSuites ?? []).map(keyEnvelopeSuiteFromProto).filter(
@@ -9238,6 +9557,7 @@ var WebTTY = class {
9238
9557
  }
9239
9558
  const clientPrincipalId = trimOptional(this.clientConfig.clientPrincipalId);
9240
9559
  const clientCredential = this.clientConfig.clientCredential;
9560
+ const proofSuites = proofSuitesFromGrant(open.sessionKeyGrant);
9241
9561
  const transcript = {
9242
9562
  authRequirement: "client-proof",
9243
9563
  clientCredentialHash: await hashWebTTYClientCredential(clientCredential),
@@ -9246,8 +9566,8 @@ var WebTTY = class {
9246
9566
  commandConfigHash,
9247
9567
  expiresAt: expiresAt.toISOString().replace(".000Z", "Z"),
9248
9568
  issuedAt: issuedAt.toISOString().replace(".000Z", "Z"),
9249
- keyEnvelopeSuite: "hpke-x25519-hkdf-sha256-aes-256-gcm",
9250
- payloadSuite: "aes-256-gcm",
9569
+ keyEnvelopeSuite: proofSuites.keyEnvelopeSuite,
9570
+ payloadSuite: proofSuites.payloadSuite,
9251
9571
  projectId: optionalString(serverHello.projectId),
9252
9572
  protocolVersion: "webtty-1",
9253
9573
  serverEncryptionKeyId: optionalBytes(serverIdentity.encryptionKeyId),
@@ -9296,6 +9616,7 @@ var WebTTY = class {
9296
9616
  issuedAt.setMilliseconds(0);
9297
9617
  const expiresAt = new Date(issuedAt.getTime() + 3e4);
9298
9618
  const clientPrincipalId = trimOptional(this.clientConfig.clientPrincipalId);
9619
+ const proofSuites = proofSuitesFromClientConfig(this.clientConfig);
9299
9620
  const transcript = {
9300
9621
  attachGrantHash: await hashWebTTYAttachGrant(attach.attachGrant),
9301
9622
  authRequirement: "client-proof",
@@ -9304,8 +9625,8 @@ var WebTTY = class {
9304
9625
  clientSigningKeyId: signing.keyId,
9305
9626
  expiresAt: expiresAt.toISOString().replace(".000Z", "Z"),
9306
9627
  issuedAt: issuedAt.toISOString().replace(".000Z", "Z"),
9307
- keyEnvelopeSuite: "hpke-x25519-hkdf-sha256-aes-256-gcm",
9308
- payloadSuite: "aes-256-gcm",
9628
+ keyEnvelopeSuite: proofSuites.keyEnvelopeSuite,
9629
+ payloadSuite: proofSuites.payloadSuite,
9309
9630
  projectId: attachConfig.projectId,
9310
9631
  protocolVersion: "webtty-1",
9311
9632
  requestedRole: attachRoleTranscriptValue(attach.requestedRole),
@@ -10974,6 +11295,8 @@ function numberValue(value) {
10974
11295
  verifyWebTTYServerProofTranscript,
10975
11296
  webTTYClientProofTranscriptBytes,
10976
11297
  webTTYE2EKeyID,
11298
+ webTTYFIPSCompatibleKeyEnvelopeSuite,
11299
+ webTTYFIPSCompatiblePayloadSuite,
10977
11300
  webTTYRecordedEventEncryptedPayload,
10978
11301
  webTTYServerProofTranscriptBytes,
10979
11302
  webTTYSigningKeyID