@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.
@@ -908,6 +908,7 @@ var rstream = $root.rstream = (() => {
908
908
  values[valuesById[0] = "PAYLOAD_CIPHER_SUITE_UNSPECIFIED"] = 0;
909
909
  values[valuesById[1] = "PAYLOAD_CIPHER_SUITE_AES_256_GCM"] = 1;
910
910
  values[valuesById[2] = "PAYLOAD_CIPHER_SUITE_CHACHA20_POLY1305"] = 2;
911
+ values[valuesById[3] = "PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE"] = 3;
911
912
  return values;
912
913
  })();
913
914
  protobuf.KeyEnvelopeSuite = (function() {
@@ -915,6 +916,7 @@ var rstream = $root.rstream = (() => {
915
916
  values[valuesById[0] = "KEY_ENVELOPE_SUITE_UNSPECIFIED"] = 0;
916
917
  values[valuesById[1] = "KEY_ENVELOPE_SUITE_HPKE_X25519_HKDF_SHA256_AES_256_GCM"] = 1;
917
918
  values[valuesById[2] = "KEY_ENVELOPE_SUITE_HPKE_X25519_HKDF_SHA256_CHACHA20_POLY1305"] = 2;
919
+ values[valuesById[3] = "KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE"] = 3;
918
920
  return values;
919
921
  })();
920
922
  protobuf.ProtocolVersion = (function() {
@@ -1417,6 +1419,10 @@ var rstream = $root.rstream = (() => {
1417
1419
  case 2:
1418
1420
  message.keyEnvelopeSuite = 2;
1419
1421
  break;
1422
+ case "KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE":
1423
+ case 3:
1424
+ message.keyEnvelopeSuite = 3;
1425
+ break;
1420
1426
  default:
1421
1427
  if (typeof object.keyEnvelopeSuite === "number" && (object.keyEnvelopeSuite | 0) === object.keyEnvelopeSuite)
1422
1428
  message.keyEnvelopeSuite = object.keyEnvelopeSuite;
@@ -2101,6 +2107,10 @@ var rstream = $root.rstream = (() => {
2101
2107
  case 2:
2102
2108
  message.payloadSuites[message.payloadSuites.length] = 2;
2103
2109
  break;
2110
+ case "PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE":
2111
+ case 3:
2112
+ message.payloadSuites[message.payloadSuites.length] = 3;
2113
+ break;
2104
2114
  default:
2105
2115
  if (typeof object.payloadSuites[i] === "number" && (object.payloadSuites[i] | 0) === object.payloadSuites[i])
2106
2116
  message.payloadSuites[message.payloadSuites.length] = object.payloadSuites[i];
@@ -2124,6 +2134,10 @@ var rstream = $root.rstream = (() => {
2124
2134
  case 2:
2125
2135
  message.keyEnvelopeSuites[message.keyEnvelopeSuites.length] = 2;
2126
2136
  break;
2137
+ case "KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE":
2138
+ case 3:
2139
+ message.keyEnvelopeSuites[message.keyEnvelopeSuites.length] = 3;
2140
+ break;
2127
2141
  default:
2128
2142
  if (typeof object.keyEnvelopeSuites[i] === "number" && (object.keyEnvelopeSuites[i] | 0) === object.keyEnvelopeSuites[i])
2129
2143
  message.keyEnvelopeSuites[message.keyEnvelopeSuites.length] = object.keyEnvelopeSuites[i];
@@ -2597,6 +2611,10 @@ var rstream = $root.rstream = (() => {
2597
2611
  case 2:
2598
2612
  message.payloadSuite = 2;
2599
2613
  break;
2614
+ case "PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE":
2615
+ case 3:
2616
+ message.payloadSuite = 3;
2617
+ break;
2600
2618
  default:
2601
2619
  if (typeof object.payloadSuite === "number" && (object.payloadSuite | 0) === object.payloadSuite)
2602
2620
  message.payloadSuite = object.payloadSuite;
@@ -2641,6 +2659,10 @@ var rstream = $root.rstream = (() => {
2641
2659
  case 2:
2642
2660
  message.keyEnvelopeSuite = 2;
2643
2661
  break;
2662
+ case "KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE":
2663
+ case 3:
2664
+ message.keyEnvelopeSuite = 3;
2665
+ break;
2644
2666
  default:
2645
2667
  if (typeof object.keyEnvelopeSuite === "number" && (object.keyEnvelopeSuite | 0) === object.keyEnvelopeSuite)
2646
2668
  message.keyEnvelopeSuite = object.keyEnvelopeSuite;
@@ -2868,6 +2890,10 @@ var rstream = $root.rstream = (() => {
2868
2890
  case 2:
2869
2891
  message.payloadSuite = 2;
2870
2892
  break;
2893
+ case "PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE":
2894
+ case 3:
2895
+ message.payloadSuite = 3;
2896
+ break;
2871
2897
  default:
2872
2898
  if (typeof object.payloadSuite === "number" && (object.payloadSuite | 0) === object.payloadSuite)
2873
2899
  message.payloadSuite = object.payloadSuite;
@@ -7378,6 +7404,7 @@ function authRequirementCode(value) {
7378
7404
  function payloadSuiteCode(value) {
7379
7405
  if (value === "aes-256-gcm" || value === void 0) return 1;
7380
7406
  if (value === "chacha20-poly1305") return 2;
7407
+ if (value === "aes-256-gcm-random-nonce") return 3;
7381
7408
  return 0;
7382
7409
  }
7383
7410
  function keyEnvelopeSuiteCode(value) {
@@ -7385,6 +7412,7 @@ function keyEnvelopeSuiteCode(value) {
7385
7412
  return 1;
7386
7413
  }
7387
7414
  if (value === "hpke-x25519-hkdf-sha256-chacha20-poly1305") return 2;
7415
+ if (value === "p256-hkdf-sha256-aes-256-gcm-random-nonce") return 3;
7388
7416
  return 0;
7389
7417
  }
7390
7418
  function signatureSuiteCode(value) {
@@ -7585,6 +7613,8 @@ function normalizeSessionKeyGrant(grant) {
7585
7613
  // src/e2e-crypto.ts
7586
7614
  var x25519PublicKeySize = 32;
7587
7615
  var x25519PrivateKeySize = 32;
7616
+ var p256PublicKeySize = 65;
7617
+ var p256PrivateKeySize = 32;
7588
7618
  var payloadKeySize = 32;
7589
7619
  var payloadKeyIDSize = 16;
7590
7620
  var aesGCMNonceSize = 12;
@@ -7597,21 +7627,24 @@ var keyIDDomain = "rstream-webtty-e2e-key-id/v1";
7597
7627
  var hpkeVersionLabel = "HPKE-v1";
7598
7628
  var nominalPayloadSuite = "aes-256-gcm";
7599
7629
  var nominalKeyEnvelopeSuite = "hpke-x25519-hkdf-sha256-aes-256-gcm";
7600
- async function generateWebTTYE2EIdentity() {
7601
- const pair = await getSubtle().generateKey({ name: "X25519" }, true, [
7602
- "deriveBits"
7603
- ]);
7604
- if (!isX25519KeyPair(pair)) {
7605
- throw new Error("WebCrypto returned an unexpected X25519 key pair");
7630
+ var webTTYFIPSCompatiblePayloadSuite = "aes-256-gcm-random-nonce";
7631
+ var webTTYFIPSCompatibleKeyEnvelopeSuite = "p256-hkdf-sha256-aes-256-gcm-random-nonce";
7632
+ async function generateWebTTYE2EIdentity(keyEnvelopeSuite = nominalKeyEnvelopeSuite) {
7633
+ payloadSuiteForKeyEnvelopeSuite(keyEnvelopeSuite);
7634
+ const algorithm = keyEnvelopeSuite === webTTYFIPSCompatibleKeyEnvelopeSuite ? { name: "ECDH", namedCurve: "P-256" } : { name: "X25519" };
7635
+ const pair = await getSubtle().generateKey(algorithm, true, ["deriveBits"]);
7636
+ if (!isCryptoKeyPair(pair)) {
7637
+ throw new Error("WebCrypto returned an unexpected E2E key pair");
7606
7638
  }
7607
7639
  const publicKey = new Uint8Array(
7608
7640
  await getSubtle().exportKey("raw", pair.publicKey)
7609
7641
  );
7610
7642
  const jwk = await getSubtle().exportKey("jwk", pair.privateKey);
7611
7643
  if (typeof jwk.d !== "string") {
7612
- throw new Error("WebCrypto X25519 private JWK is missing d");
7644
+ throw new Error("WebCrypto E2E private JWK is missing d");
7613
7645
  }
7614
7646
  return {
7647
+ keyEnvelopeSuite,
7615
7648
  keyId: await webTTYE2EKeyID(publicKey),
7616
7649
  privateKey: base64URLDecode(jwk.d),
7617
7650
  publicKey
@@ -7652,8 +7685,8 @@ function createWebTTYE2EKeyContext(config) {
7652
7685
  );
7653
7686
  }
7654
7687
  async function createWebTTYE2EClientPayloadCrypto(config) {
7655
- const payloadSuite = config.payloadSuite ?? nominalPayloadSuite;
7656
- const keyEnvelopeSuite = config.keyEnvelopeSuite ?? nominalKeyEnvelopeSuite;
7688
+ const keyEnvelopeSuite = config.keyEnvelopeSuite ?? inferRecipientSuite(config.recipients);
7689
+ const payloadSuite = config.payloadSuite ?? payloadSuiteForKeyEnvelopeSuite(keyEnvelopeSuite);
7657
7690
  validateSuites(payloadSuite, keyEnvelopeSuite);
7658
7691
  const payloadKey = config.payloadKey ? cloneKeyMaterial(config.payloadKey) : randomBytes(payloadKeySize);
7659
7692
  if (payloadKey.byteLength !== payloadKeySize) {
@@ -7745,7 +7778,7 @@ function createPayloadCrypto(options) {
7745
7778
  payloadSuite: options.payloadSuite
7746
7779
  };
7747
7780
  const cryptoInfo = {
7748
- keyAgreement: "HPKE X25519",
7781
+ keyAgreement: options.keyEnvelopeSuite === webTTYFIPSCompatibleKeyEnvelopeSuite ? "ECDH P-256" : "HPKE X25519",
7749
7782
  keyDerivation: "HKDF-SHA256",
7750
7783
  keyEncryption: "AES-256-GCM",
7751
7784
  keyEnvelopeSuite: options.keyEnvelopeSuite,
@@ -7760,7 +7793,7 @@ function createPayloadCrypto(options) {
7760
7793
  if (chunk.byteLength > 4294967295) {
7761
7794
  throw new Error(`E2E payload is too large: ${chunk.byteLength} bytes`);
7762
7795
  }
7763
- const nonce = randomBytes(aesGCMNonceSize);
7796
+ const nonce = options.payloadSuite === webTTYFIPSCompatiblePayloadSuite ? new Uint8Array() : randomBytes(aesGCMNonceSize);
7764
7797
  const payloadCrypto = {
7765
7798
  aadContext: cloneBytes(options.keyContext),
7766
7799
  nonce,
@@ -7768,7 +7801,8 @@ function createPayloadCrypto(options) {
7768
7801
  payloadSuite: options.payloadSuite
7769
7802
  };
7770
7803
  return {
7771
- ciphertext: await aesGCMEncrypt(
7804
+ ciphertext: await encryptPayloadForSuite(
7805
+ options.payloadSuite,
7772
7806
  options.payloadKey,
7773
7807
  nonce,
7774
7808
  cloneBytes(chunk),
@@ -7788,19 +7822,21 @@ function createPayloadCrypto(options) {
7788
7822
  const decrypt = (stream) => async (payload) => {
7789
7823
  validatePayloadEnvelope(payload, options);
7790
7824
  const crypto = payload.payloadCrypto;
7791
- if (crypto === void 0 || crypto.nonce === void 0) {
7792
- throw new Error("missing E2E payload nonce");
7825
+ if (crypto === void 0) {
7826
+ throw new Error("missing E2E payload crypto");
7793
7827
  }
7794
- const plaintext = await aesGCMDecrypt(
7828
+ const nonce = crypto.nonce ?? new Uint8Array();
7829
+ const plaintext = await decryptPayloadForSuite(
7830
+ options.payloadSuite,
7795
7831
  options.payloadKey,
7796
- crypto.nonce,
7832
+ nonce,
7797
7833
  payload.ciphertext,
7798
7834
  payloadAAD(
7799
7835
  stream,
7800
7836
  options.payloadSuite,
7801
7837
  options.payloadKeyId,
7802
7838
  options.keyContext,
7803
- crypto.nonce,
7839
+ nonce,
7804
7840
  payload.plaintextLength
7805
7841
  )
7806
7842
  );
@@ -7845,21 +7881,29 @@ async function implicitKeyContextFromRecipients(recipients) {
7845
7881
  }
7846
7882
  async function wrapPayloadKey(payloadKey, payloadSuite, payloadKeyId, keyContext, suite, recipient) {
7847
7883
  const recipientPublicKey = cloneKeyMaterial(recipient.publicKey);
7848
- if (recipientPublicKey.byteLength !== x25519PublicKeySize) {
7884
+ const recipientSuite = recipient.keyEnvelopeSuite ?? inferKeyEnvelopeSuite(recipientPublicKey);
7885
+ if (recipientSuite !== suite) {
7886
+ throw new Error("E2E recipient suite does not match key envelope suite");
7887
+ }
7888
+ const expectedPublicKeySize = publicKeySizeForSuite(suite);
7889
+ if (recipientPublicKey.byteLength !== expectedPublicKeySize) {
7849
7890
  throw new Error(
7850
- `E2E recipient public key must be ${x25519PublicKeySize} bytes`
7891
+ `E2E recipient public key must be ${expectedPublicKeySize} bytes`
7851
7892
  );
7852
7893
  }
7853
7894
  const recipientKeyId = recipient.keyId ? cloneKeyMaterial(recipient.keyId) : await webTTYE2EKeyID(recipientPublicKey);
7854
7895
  if (recipientKeyId.byteLength !== payloadKeyIDSize) {
7855
7896
  throw new Error(`E2E recipient key id must be ${payloadKeyIDSize} bytes`);
7856
7897
  }
7857
- const sealed = await hpkeSeal(
7858
- recipientPublicKey,
7859
- hpkeInfo(payloadSuite, payloadKeyId, keyContext, suite),
7860
- hpkeAAD(recipientKeyId, payloadSuite, payloadKeyId, keyContext, suite),
7861
- payloadKey
7898
+ const info = hpkeInfo(payloadSuite, payloadKeyId, keyContext, suite);
7899
+ const aad = hpkeAAD(
7900
+ recipientKeyId,
7901
+ payloadSuite,
7902
+ payloadKeyId,
7903
+ keyContext,
7904
+ suite
7862
7905
  );
7906
+ const sealed = suite === webTTYFIPSCompatibleKeyEnvelopeSuite ? await p256Seal(recipientPublicKey, info, aad, payloadKey) : await hpkeSeal(recipientPublicKey, info, aad, payloadKey);
7863
7907
  return {
7864
7908
  encapsulatedKey: sealed.encapsulatedKey,
7865
7909
  recipientKeyId,
@@ -7876,17 +7920,28 @@ async function unwrapPayloadKey(envelope, payloadSuite, payloadKeyId, keyContext
7876
7920
  if (envelope.wrappedKey === void 0) {
7877
7921
  throw new Error("E2E key envelope is missing wrapped key");
7878
7922
  }
7879
- const payloadKey = await hpkeOpen(
7923
+ if (identity.keyEnvelopeSuite !== suite) {
7924
+ throw new Error("E2E identity suite does not match key envelope suite");
7925
+ }
7926
+ const info = hpkeInfo(payloadSuite, payloadKeyId, keyContext, suite);
7927
+ const aad = hpkeAAD(
7928
+ envelope.recipientKeyId,
7929
+ payloadSuite,
7930
+ payloadKeyId,
7931
+ keyContext,
7932
+ suite
7933
+ );
7934
+ const payloadKey = suite === webTTYFIPSCompatibleKeyEnvelopeSuite ? await p256Open(
7880
7935
  identity,
7881
7936
  envelope.encapsulatedKey,
7882
- hpkeInfo(payloadSuite, payloadKeyId, keyContext, suite),
7883
- hpkeAAD(
7884
- envelope.recipientKeyId,
7885
- payloadSuite,
7886
- payloadKeyId,
7887
- keyContext,
7888
- suite
7889
- ),
7937
+ info,
7938
+ aad,
7939
+ envelope.wrappedKey
7940
+ ) : await hpkeOpen(
7941
+ identity,
7942
+ envelope.encapsulatedKey,
7943
+ info,
7944
+ aad,
7890
7945
  envelope.wrappedKey
7891
7946
  );
7892
7947
  if (payloadKey.byteLength !== payloadKeySize) {
@@ -7968,6 +8023,95 @@ async function hpkeOpen(identity, encapsulatedKey, info, aad, ciphertext) {
7968
8023
  const schedule = await hpkeKeySchedule(sharedSecret, info);
7969
8024
  return aesGCMDecrypt(schedule.key, schedule.baseNonce, ciphertext, aad);
7970
8025
  }
8026
+ async function p256Seal(publicKey, info, aad, plaintext) {
8027
+ const recipientPublic = await getSubtle().importKey(
8028
+ "raw",
8029
+ bufferSource(publicKey),
8030
+ { name: "ECDH", namedCurve: "P-256" },
8031
+ false,
8032
+ []
8033
+ );
8034
+ const ephemeral = await getSubtle().generateKey(
8035
+ { name: "ECDH", namedCurve: "P-256" },
8036
+ true,
8037
+ ["deriveBits"]
8038
+ );
8039
+ if (!isCryptoKeyPair(ephemeral)) {
8040
+ throw new Error("WebCrypto returned an unexpected P-256 key pair");
8041
+ }
8042
+ const encapsulatedKey = new Uint8Array(
8043
+ await getSubtle().exportKey("raw", ephemeral.publicKey)
8044
+ );
8045
+ const sharedSecret = new Uint8Array(
8046
+ await getSubtle().deriveBits(
8047
+ { name: "ECDH", public: recipientPublic },
8048
+ ephemeral.privateKey,
8049
+ 256
8050
+ )
8051
+ );
8052
+ const wrappingKey = await p256WrappingKey(sharedSecret, info);
8053
+ return {
8054
+ encapsulatedKey,
8055
+ wrappedKey: await aesGCMEncryptWithPrefixedRandomNonce(
8056
+ wrappingKey,
8057
+ plaintext,
8058
+ aad
8059
+ )
8060
+ };
8061
+ }
8062
+ async function p256Open(identity, encapsulatedKey, info, aad, ciphertext) {
8063
+ if (identity.privateKey.byteLength !== p256PrivateKeySize) {
8064
+ throw new Error(
8065
+ `E2E identity private key must be ${p256PrivateKeySize} bytes`
8066
+ );
8067
+ }
8068
+ if (identity.publicKey.byteLength !== p256PublicKeySize || identity.publicKey[0] !== 4) {
8069
+ throw new Error(
8070
+ `E2E identity public key must be an uncompressed ${p256PublicKeySize}-byte P-256 point`
8071
+ );
8072
+ }
8073
+ const recipientPrivate = await importP256PrivateKey(identity);
8074
+ const ephemeralPublic = await getSubtle().importKey(
8075
+ "raw",
8076
+ bufferSource(encapsulatedKey),
8077
+ { name: "ECDH", namedCurve: "P-256" },
8078
+ false,
8079
+ []
8080
+ );
8081
+ const sharedSecret = new Uint8Array(
8082
+ await getSubtle().deriveBits(
8083
+ { name: "ECDH", public: ephemeralPublic },
8084
+ recipientPrivate,
8085
+ 256
8086
+ )
8087
+ );
8088
+ return aesGCMDecryptWithPrefixedRandomNonce(
8089
+ await p256WrappingKey(sharedSecret, info),
8090
+ ciphertext,
8091
+ aad
8092
+ );
8093
+ }
8094
+ async function p256WrappingKey(sharedSecret, info) {
8095
+ const key = await getSubtle().importKey(
8096
+ "raw",
8097
+ bufferSource(sharedSecret),
8098
+ "HKDF",
8099
+ false,
8100
+ ["deriveBits"]
8101
+ );
8102
+ return new Uint8Array(
8103
+ await getSubtle().deriveBits(
8104
+ {
8105
+ hash: "SHA-256",
8106
+ info: bufferSource(info),
8107
+ name: "HKDF",
8108
+ salt: new Uint8Array()
8109
+ },
8110
+ key,
8111
+ 256
8112
+ )
8113
+ );
8114
+ }
7971
8115
  async function dhkemExtractAndExpand(dh, kemContext) {
7972
8116
  const eaePRK = await labeledExtract(
7973
8117
  dhkemSuiteID(),
@@ -8120,6 +8264,33 @@ async function aesGCMDecrypt(key, nonce, ciphertext, aad) {
8120
8264
  )
8121
8265
  );
8122
8266
  }
8267
+ async function aesGCMEncryptWithPrefixedRandomNonce(key, plaintext, aad) {
8268
+ const nonce = randomBytes(aesGCMNonceSize);
8269
+ return concat2(nonce, await aesGCMEncrypt(key, nonce, plaintext, aad));
8270
+ }
8271
+ async function aesGCMDecryptWithPrefixedRandomNonce(key, ciphertext, aad) {
8272
+ if (ciphertext.byteLength < aesGCMNonceSize + 16) {
8273
+ throw new Error("E2E random-nonce AES-GCM ciphertext is too short");
8274
+ }
8275
+ return aesGCMDecrypt(
8276
+ key,
8277
+ ciphertext.slice(0, aesGCMNonceSize),
8278
+ ciphertext.slice(aesGCMNonceSize),
8279
+ aad
8280
+ );
8281
+ }
8282
+ async function encryptPayloadForSuite(suite, key, nonce, plaintext, aad) {
8283
+ if (suite === webTTYFIPSCompatiblePayloadSuite) {
8284
+ return aesGCMEncryptWithPrefixedRandomNonce(key, plaintext, aad);
8285
+ }
8286
+ return aesGCMEncrypt(key, nonce, plaintext, aad);
8287
+ }
8288
+ async function decryptPayloadForSuite(suite, key, nonce, ciphertext, aad) {
8289
+ if (suite === webTTYFIPSCompatiblePayloadSuite) {
8290
+ return aesGCMDecryptWithPrefixedRandomNonce(key, ciphertext, aad);
8291
+ }
8292
+ return aesGCMDecrypt(key, nonce, ciphertext, aad);
8293
+ }
8123
8294
  async function importX25519PrivateKey(identity) {
8124
8295
  return getSubtle().importKey(
8125
8296
  "jwk",
@@ -8136,6 +8307,23 @@ async function importX25519PrivateKey(identity) {
8136
8307
  ["deriveBits"]
8137
8308
  );
8138
8309
  }
8310
+ async function importP256PrivateKey(identity) {
8311
+ return getSubtle().importKey(
8312
+ "jwk",
8313
+ {
8314
+ crv: "P-256",
8315
+ d: base64URLEncode(identity.privateKey),
8316
+ ext: true,
8317
+ key_ops: ["deriveBits"],
8318
+ kty: "EC",
8319
+ x: base64URLEncode(identity.publicKey.slice(1, 33)),
8320
+ y: base64URLEncode(identity.publicKey.slice(33, 65))
8321
+ },
8322
+ { name: "ECDH", namedCurve: "P-256" },
8323
+ false,
8324
+ ["deriveBits"]
8325
+ );
8326
+ }
8139
8327
  function validatePayloadEnvelope(payload, options) {
8140
8328
  const crypto = payload.payloadCrypto;
8141
8329
  if (crypto === void 0) throw new Error("missing E2E payload crypto");
@@ -8148,18 +8336,64 @@ function validatePayloadEnvelope(payload, options) {
8148
8336
  if (!bytesEqual(crypto.aadContext, options.keyContext)) {
8149
8337
  throw new Error("unexpected E2E key context");
8150
8338
  }
8151
- if (crypto.nonce === void 0 || crypto.nonce.byteLength !== aesGCMNonceSize) {
8152
- throw new Error(`E2E AES-GCM nonce must be ${aesGCMNonceSize} bytes`);
8339
+ const expectedNonceSize = options.payloadSuite === webTTYFIPSCompatiblePayloadSuite ? 0 : aesGCMNonceSize;
8340
+ if (crypto.nonce === void 0 || crypto.nonce.byteLength !== expectedNonceSize) {
8341
+ throw new Error(`E2E AES-GCM nonce must be ${expectedNonceSize} bytes`);
8153
8342
  }
8154
8343
  }
8155
- function validateSuites(payloadSuite, keyEnvelopeSuite) {
8156
- if (payloadSuite !== nominalPayloadSuite) {
8157
- throw new Error(`unsupported E2E payload suite ${payloadSuite}`);
8344
+ function inferRecipientSuite(recipients) {
8345
+ if (recipients.length === 0) {
8346
+ throw new Error(
8347
+ "E2E client payload crypto requires at least one recipient"
8348
+ );
8349
+ }
8350
+ const suites = recipients.map(
8351
+ (recipient) => recipient.keyEnvelopeSuite ?? inferKeyEnvelopeSuite(cloneKeyMaterial(recipient.publicKey))
8352
+ );
8353
+ const suite = suites[0];
8354
+ if (suite === void 0 || suites.some((candidate) => candidate !== suite)) {
8355
+ throw new Error("E2E recipients must use the same key envelope suite");
8356
+ }
8357
+ return suite;
8358
+ }
8359
+ function inferKeyEnvelopeSuite(publicKey) {
8360
+ if (publicKey.byteLength === x25519PublicKeySize) {
8361
+ return nominalKeyEnvelopeSuite;
8158
8362
  }
8159
- if (keyEnvelopeSuite !== nominalKeyEnvelopeSuite) {
8160
- throw new Error(`unsupported E2E key envelope suite ${keyEnvelopeSuite}`);
8363
+ if (publicKey.byteLength === p256PublicKeySize && publicKey[0] === 4) {
8364
+ return webTTYFIPSCompatibleKeyEnvelopeSuite;
8161
8365
  }
8162
- return { keyEnvelopeSuite, payloadSuite };
8366
+ throw new Error(
8367
+ `unsupported E2E recipient public key length ${publicKey.byteLength}`
8368
+ );
8369
+ }
8370
+ function payloadSuiteForKeyEnvelopeSuite(suite) {
8371
+ switch (suite) {
8372
+ case nominalKeyEnvelopeSuite:
8373
+ return nominalPayloadSuite;
8374
+ case webTTYFIPSCompatibleKeyEnvelopeSuite:
8375
+ return webTTYFIPSCompatiblePayloadSuite;
8376
+ default:
8377
+ throw new Error(`unsupported E2E key envelope suite ${suite}`);
8378
+ }
8379
+ }
8380
+ function publicKeySizeForSuite(suite) {
8381
+ switch (suite) {
8382
+ case nominalKeyEnvelopeSuite:
8383
+ return x25519PublicKeySize;
8384
+ case webTTYFIPSCompatibleKeyEnvelopeSuite:
8385
+ return p256PublicKeySize;
8386
+ default:
8387
+ throw new Error(`unsupported E2E key envelope suite ${suite}`);
8388
+ }
8389
+ }
8390
+ function validateSuites(payloadSuite, keyEnvelopeSuite) {
8391
+ if (payloadSuite === nominalPayloadSuite && keyEnvelopeSuite === nominalKeyEnvelopeSuite || payloadSuite === webTTYFIPSCompatiblePayloadSuite && keyEnvelopeSuite === webTTYFIPSCompatibleKeyEnvelopeSuite) {
8392
+ return { keyEnvelopeSuite, payloadSuite };
8393
+ }
8394
+ throw new Error(
8395
+ `unsupported E2E suite pair ${payloadSuite}/${keyEnvelopeSuite}`
8396
+ );
8163
8397
  }
8164
8398
  function hpkeInfo(payloadSuite, payloadKeyId, keyContext, suite) {
8165
8399
  return concat2(
@@ -8194,11 +8428,13 @@ function payloadAAD(stream, suite, payloadKeyId, keyContext, nonce, plaintextLen
8194
8428
  function payloadSuiteCode2(suite) {
8195
8429
  if (suite === "aes-256-gcm") return 1;
8196
8430
  if (suite === "chacha20-poly1305") return 2;
8431
+ if (suite === "aes-256-gcm-random-nonce") return 3;
8197
8432
  return unsupportedPayloadSuite(suite);
8198
8433
  }
8199
8434
  function keyEnvelopeSuiteCode2(suite) {
8200
8435
  if (suite === "hpke-x25519-hkdf-sha256-aes-256-gcm") return 1;
8201
8436
  if (suite === "hpke-x25519-hkdf-sha256-chacha20-poly1305") return 2;
8437
+ if (suite === "p256-hkdf-sha256-aes-256-gcm-random-nonce") return 3;
8202
8438
  return unsupportedKeyEnvelopeSuite(suite);
8203
8439
  }
8204
8440
  function unsupportedPayloadSuite(suite) {
@@ -8274,10 +8510,12 @@ function nonEmptyString(value, label) {
8274
8510
  return void 0;
8275
8511
  }
8276
8512
  function normalizeIdentity(identity) {
8513
+ const publicKey = cloneKeyMaterial(identity.publicKey);
8277
8514
  return {
8515
+ keyEnvelopeSuite: identity.keyEnvelopeSuite ?? inferKeyEnvelopeSuite(publicKey),
8278
8516
  keyId: cloneKeyMaterial(identity.keyId),
8279
8517
  privateKey: cloneKeyMaterial(identity.privateKey),
8280
- publicKey: cloneKeyMaterial(identity.publicKey)
8518
+ publicKey
8281
8519
  };
8282
8520
  }
8283
8521
  function bufferSource(value) {
@@ -8331,9 +8569,10 @@ function getSubtle() {
8331
8569
  }
8332
8570
  return subtle;
8333
8571
  }
8334
- function isX25519KeyPair(value) {
8572
+ function isCryptoKeyPair(value) {
8335
8573
  return "privateKey" in value && "publicKey" in value;
8336
8574
  }
8575
+ var isX25519KeyPair = isCryptoKeyPair;
8337
8576
  function base64URLEncode(value) {
8338
8577
  const binary = String.fromCharCode(...value);
8339
8578
  return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
@@ -8398,6 +8637,8 @@ function payloadCipherSuiteToProto(suite) {
8398
8637
  return rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_AES_256_GCM;
8399
8638
  case "chacha20-poly1305":
8400
8639
  return rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_CHACHA20_POLY1305;
8640
+ case "aes-256-gcm-random-nonce":
8641
+ return rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE;
8401
8642
  default:
8402
8643
  return rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_UNSPECIFIED;
8403
8644
  }
@@ -8408,6 +8649,8 @@ function payloadCipherSuiteFromProto(suite) {
8408
8649
  return "aes-256-gcm";
8409
8650
  case rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_CHACHA20_POLY1305:
8410
8651
  return "chacha20-poly1305";
8652
+ case rstream.webtty.protobuf.PayloadCipherSuite.PAYLOAD_CIPHER_SUITE_AES_256_GCM_RANDOM_NONCE:
8653
+ return "aes-256-gcm-random-nonce";
8411
8654
  default:
8412
8655
  return void 0;
8413
8656
  }
@@ -8418,6 +8661,8 @@ function keyEnvelopeSuiteToProto(suite) {
8418
8661
  return rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_HPKE_X25519_HKDF_SHA256_AES_256_GCM;
8419
8662
  case "hpke-x25519-hkdf-sha256-chacha20-poly1305":
8420
8663
  return rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_HPKE_X25519_HKDF_SHA256_CHACHA20_POLY1305;
8664
+ case "p256-hkdf-sha256-aes-256-gcm-random-nonce":
8665
+ return rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE;
8421
8666
  default:
8422
8667
  return rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_UNSPECIFIED;
8423
8668
  }
@@ -8428,10 +8673,72 @@ function keyEnvelopeSuiteFromProto(suite) {
8428
8673
  return "hpke-x25519-hkdf-sha256-aes-256-gcm";
8429
8674
  case rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_HPKE_X25519_HKDF_SHA256_CHACHA20_POLY1305:
8430
8675
  return "hpke-x25519-hkdf-sha256-chacha20-poly1305";
8676
+ case rstream.webtty.protobuf.KeyEnvelopeSuite.KEY_ENVELOPE_SUITE_P256_HKDF_SHA256_AES_256_GCM_RANDOM_NONCE:
8677
+ return "p256-hkdf-sha256-aes-256-gcm-random-nonce";
8431
8678
  default:
8432
8679
  return void 0;
8433
8680
  }
8434
8681
  }
8682
+ var defaultE2EPayloadSuite = "aes-256-gcm";
8683
+ var defaultE2EKeyEnvelopeSuite = "hpke-x25519-hkdf-sha256-aes-256-gcm";
8684
+ var fipsCompatibleE2EPayloadSuite = "aes-256-gcm-random-nonce";
8685
+ var fipsCompatibleE2EKeyEnvelopeSuite = "p256-hkdf-sha256-aes-256-gcm-random-nonce";
8686
+ function payloadSuiteForKeyEnvelopeSuite2(suite) {
8687
+ switch (suite) {
8688
+ case defaultE2EKeyEnvelopeSuite:
8689
+ return defaultE2EPayloadSuite;
8690
+ case fipsCompatibleE2EKeyEnvelopeSuite:
8691
+ return fipsCompatibleE2EPayloadSuite;
8692
+ default:
8693
+ throw new Error(`Unsupported WebTTY E2E key envelope suite ${suite}.`);
8694
+ }
8695
+ }
8696
+ function asE2EKeyEnvelopeSuite(suite) {
8697
+ if (suite === defaultE2EKeyEnvelopeSuite || suite === fipsCompatibleE2EKeyEnvelopeSuite) {
8698
+ return suite;
8699
+ }
8700
+ throw new Error(`Unsupported WebTTY E2E key envelope suite ${suite}.`);
8701
+ }
8702
+ function inferE2EKeyEnvelopeSuite(publicKey) {
8703
+ if (publicKey?.byteLength === 32) return defaultE2EKeyEnvelopeSuite;
8704
+ if (publicKey?.byteLength === 65 && publicKey[0] === 4) {
8705
+ return fipsCompatibleE2EKeyEnvelopeSuite;
8706
+ }
8707
+ return void 0;
8708
+ }
8709
+ function proofSuitesFromGrant(grant) {
8710
+ const rawKeyEnvelopeSuite = keyEnvelopeSuiteFromProto(
8711
+ grant?.keyEnvelopeSuite
8712
+ );
8713
+ const payloadSuite = payloadCipherSuiteFromProto(grant?.payloadSuite);
8714
+ if (rawKeyEnvelopeSuite === void 0 && payloadSuite === void 0) {
8715
+ return {
8716
+ keyEnvelopeSuite: defaultE2EKeyEnvelopeSuite,
8717
+ payloadSuite: defaultE2EPayloadSuite
8718
+ };
8719
+ }
8720
+ if (rawKeyEnvelopeSuite === void 0 || payloadSuite === void 0) {
8721
+ throw new Error("WebTTY session key grant uses an unsupported suite pair.");
8722
+ }
8723
+ const keyEnvelopeSuite = asE2EKeyEnvelopeSuite(rawKeyEnvelopeSuite);
8724
+ const expectedPayloadSuite = payloadSuiteForKeyEnvelopeSuite2(keyEnvelopeSuite);
8725
+ if (expectedPayloadSuite !== payloadSuite) {
8726
+ throw new Error("WebTTY session key grant uses an unsupported suite pair.");
8727
+ }
8728
+ return { keyEnvelopeSuite, payloadSuite: expectedPayloadSuite };
8729
+ }
8730
+ function proofSuitesFromClientConfig(config) {
8731
+ const configured = config.endpointIdentity?.keyEnvelopeSuite;
8732
+ const expected = config.expectedServerIdentity;
8733
+ const expectedSuite = expected?.keyEnvelopeSuite ?? inferE2EKeyEnvelopeSuite(expected?.encryptionPublicKey);
8734
+ const keyEnvelopeSuite = asE2EKeyEnvelopeSuite(
8735
+ configured ?? expectedSuite ?? defaultE2EKeyEnvelopeSuite
8736
+ );
8737
+ return {
8738
+ keyEnvelopeSuite,
8739
+ payloadSuite: payloadSuiteForKeyEnvelopeSuite2(keyEnvelopeSuite)
8740
+ };
8741
+ }
8435
8742
  function signatureSuiteFromProto(suite) {
8436
8743
  switch (suite) {
8437
8744
  case rstream.webtty.protobuf.SignatureSuite.SIGNATURE_SUITE_ECDSA_P256_SHA256:
@@ -9112,6 +9419,16 @@ var WebTTY = class {
9112
9419
  if (!bytesEqual2(actual.signingKeyId, expected.signingKeyId) || !bytesEqual2(actual.signingPublicKey, expected.signingPublicKey) || !bytesEqual2(actual.encryptionKeyId, expected.encryptionKeyId) || !bytesEqual2(actual.encryptionPublicKey, expected.encryptionPublicKey)) {
9113
9420
  return false;
9114
9421
  }
9422
+ const expectedKeyEnvelopeSuite = expected.keyEnvelopeSuite ?? inferE2EKeyEnvelopeSuite(expected.encryptionPublicKey) ?? defaultE2EKeyEnvelopeSuite;
9423
+ const advertisedKeyEnvelopeSuites = (serverHello.keyEnvelopeSuites ?? []).map(keyEnvelopeSuiteFromProto).filter((value) => value !== void 0);
9424
+ const advertisedPayloadSuites = (serverHello.payloadSuites ?? []).map(payloadCipherSuiteFromProto).filter(
9425
+ (value) => value !== void 0
9426
+ );
9427
+ if (!advertisedKeyEnvelopeSuites.includes(expectedKeyEnvelopeSuite) || !advertisedPayloadSuites.includes(
9428
+ payloadSuiteForKeyEnvelopeSuite2(expectedKeyEnvelopeSuite)
9429
+ )) {
9430
+ return false;
9431
+ }
9115
9432
  const transcript = {
9116
9433
  authRequirement: authRequirementFromProto(serverHello.authRequirement),
9117
9434
  keyEnvelopeSuites: (serverHello.keyEnvelopeSuites ?? []).map(keyEnvelopeSuiteFromProto).filter(
@@ -9166,6 +9483,7 @@ var WebTTY = class {
9166
9483
  }
9167
9484
  const clientPrincipalId = trimOptional(this.clientConfig.clientPrincipalId);
9168
9485
  const clientCredential = this.clientConfig.clientCredential;
9486
+ const proofSuites = proofSuitesFromGrant(open.sessionKeyGrant);
9169
9487
  const transcript = {
9170
9488
  authRequirement: "client-proof",
9171
9489
  clientCredentialHash: await hashWebTTYClientCredential(clientCredential),
@@ -9174,8 +9492,8 @@ var WebTTY = class {
9174
9492
  commandConfigHash,
9175
9493
  expiresAt: expiresAt.toISOString().replace(".000Z", "Z"),
9176
9494
  issuedAt: issuedAt.toISOString().replace(".000Z", "Z"),
9177
- keyEnvelopeSuite: "hpke-x25519-hkdf-sha256-aes-256-gcm",
9178
- payloadSuite: "aes-256-gcm",
9495
+ keyEnvelopeSuite: proofSuites.keyEnvelopeSuite,
9496
+ payloadSuite: proofSuites.payloadSuite,
9179
9497
  projectId: optionalString(serverHello.projectId),
9180
9498
  protocolVersion: "webtty-1",
9181
9499
  serverEncryptionKeyId: optionalBytes(serverIdentity.encryptionKeyId),
@@ -9224,6 +9542,7 @@ var WebTTY = class {
9224
9542
  issuedAt.setMilliseconds(0);
9225
9543
  const expiresAt = new Date(issuedAt.getTime() + 3e4);
9226
9544
  const clientPrincipalId = trimOptional(this.clientConfig.clientPrincipalId);
9545
+ const proofSuites = proofSuitesFromClientConfig(this.clientConfig);
9227
9546
  const transcript = {
9228
9547
  attachGrantHash: await hashWebTTYAttachGrant(attach.attachGrant),
9229
9548
  authRequirement: "client-proof",
@@ -9232,8 +9551,8 @@ var WebTTY = class {
9232
9551
  clientSigningKeyId: signing.keyId,
9233
9552
  expiresAt: expiresAt.toISOString().replace(".000Z", "Z"),
9234
9553
  issuedAt: issuedAt.toISOString().replace(".000Z", "Z"),
9235
- keyEnvelopeSuite: "hpke-x25519-hkdf-sha256-aes-256-gcm",
9236
- payloadSuite: "aes-256-gcm",
9554
+ keyEnvelopeSuite: proofSuites.keyEnvelopeSuite,
9555
+ payloadSuite: proofSuites.payloadSuite,
9237
9556
  projectId: attachConfig.projectId,
9238
9557
  protocolVersion: "webtty-1",
9239
9558
  requestedRole: attachRoleTranscriptValue(attach.requestedRole),
@@ -10882,6 +11201,8 @@ export {
10882
11201
  hashWebTTYSessionKeyGrant,
10883
11202
  webTTYServerProofTranscriptBytes,
10884
11203
  hashWebTTYServerProofTranscript,
11204
+ webTTYFIPSCompatiblePayloadSuite,
11205
+ webTTYFIPSCompatibleKeyEnvelopeSuite,
10885
11206
  generateWebTTYE2EIdentity,
10886
11207
  webTTYE2EKeyID,
10887
11208
  encodeWebTTYE2EKeyMaterial,