hpke 1.1.6 → 1.1.8

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/index.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * asymmetric key exchange with a symmetric cipher. This was originally defined in an Informational
6
6
  * document on the IRTF stream as [RFC 9180](https://www.rfc-editor.org/rfc/rfc9180.html) and is now
7
7
  * being republished as a Standards Track document of the IETF as
8
- * [draft-ietf-hpke-hpke](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04).
8
+ * [draft-ietf-hpke-hpke](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06).
9
9
  *
10
10
  * HPKE provides a variant of public key encryption for arbitrary-sized plaintexts using a recipient
11
11
  * public key.
@@ -42,7 +42,7 @@
42
42
  // HPKE Context Classes - Sender and Recipient Contexts
43
43
  // ============================================================================
44
44
 
45
- /** @see [ComputeNonce](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.2) */
45
+ /** @see [ComputeNonce](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.2) */
46
46
  function ComputeNonce(base_nonce: Uint8Array, seq: number, Nn: number): Uint8Array {
47
47
  // Equivalent to xor(base_nonce, I2OSP(seq, Nn)) but avoids allocating the
48
48
  // intermediate seq_bytes array and fuses the two byte-wise passes into one.
@@ -63,7 +63,7 @@ function MaxSeq(Nn: number): number {
63
63
  return Math.min(2 ** (8 * Nn) - 1, Number.MAX_SAFE_INTEGER)
64
64
  }
65
65
 
66
- /** @see [Context.IncrementSeq](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.2) */
66
+ /** @see [Context.IncrementSeq](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.2) */
67
67
  function IncrementSeq(seq: number, maxSeq: number): number {
68
68
  if (seq >= maxSeq) {
69
69
  throw new MessageLimitReachedError('Sequence number overflow')
@@ -71,7 +71,31 @@ function IncrementSeq(seq: number, maxSeq: number): number {
71
71
  return ++seq
72
72
  }
73
73
 
74
- /** @see [Context.Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.3) */
74
+ /** @see [Context.Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.3) */
75
+ async function Export_OneStage(
76
+ KDF: KDF,
77
+ suite_id: Uint8Array,
78
+ exporter_secret: Uint8Array,
79
+ exporter_context: Uint8Array,
80
+ L: number,
81
+ ) {
82
+ checkLength(exporter_context, 'Exporter context', MAX_LENGTH_ONE_STAGE)
83
+ return await LabeledDerive(KDF, suite_id, exporter_secret, L_sec, exporter_context, L)
84
+ }
85
+
86
+ /** @see [Context.Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.3) */
87
+ async function Export_TwoStage(
88
+ KDF: KDF,
89
+ suite_id: Uint8Array,
90
+ exporter_secret: Uint8Array,
91
+ exporter_context: Uint8Array,
92
+ L: number,
93
+ ) {
94
+ checkLength(exporter_context, 'Exporter context', MAX_LENGTH_TWO_STAGE)
95
+ return await LabeledExpand(KDF, suite_id, exporter_secret, L_sec, exporter_context, L)
96
+ }
97
+
98
+ /** @see [Context.Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.3) */
75
99
  async function ContextExport(
76
100
  suite: Triple,
77
101
  exporterSecret: Uint8Array,
@@ -105,6 +129,36 @@ class Mutex {
105
129
  }
106
130
  }
107
131
 
132
+ interface ContextState {
133
+ readonly suite: Triple
134
+ readonly mode: Mode
135
+ readonly key: Uint8Array
136
+ readonly base_nonce: Uint8Array
137
+ readonly exporter_secret: Uint8Array
138
+ readonly max_seq: number
139
+ seq: number
140
+ mutex?: Mutex
141
+ }
142
+
143
+ function createContextState(
144
+ suite: Triple,
145
+ mode: Mode,
146
+ key: Uint8Array,
147
+ base_nonce: Uint8Array,
148
+ exporter_secret: Uint8Array,
149
+ ): ContextState {
150
+ return {
151
+ suite,
152
+ mode,
153
+ key,
154
+ base_nonce,
155
+ exporter_secret,
156
+ max_seq: MaxSeq(suite.AEAD.Nn),
157
+ seq: 0,
158
+ mutex: undefined,
159
+ }
160
+ }
161
+
108
162
  /**
109
163
  * Context for encrypting multiple messages and exporting secrets on the sender side.
110
164
  *
@@ -125,14 +179,7 @@ class Mutex {
125
179
  * @group Core
126
180
  */
127
181
  class SenderContext {
128
- #suite: Triple
129
- #key: Uint8Array
130
- #base_nonce: Uint8Array
131
- #exporter_secret: Uint8Array
132
- #mode: Mode
133
- #seq: number = 0
134
- #max_seq: number
135
- #mutex?: Mutex
182
+ #state: ContextState
136
183
 
137
184
  constructor(
138
185
  suite: Triple,
@@ -141,12 +188,7 @@ class SenderContext {
141
188
  base_nonce: Uint8Array,
142
189
  exporter_secret: Uint8Array,
143
190
  ) {
144
- this.#suite = suite
145
- this.#mode = mode
146
- this.#key = key
147
- this.#base_nonce = base_nonce
148
- this.#exporter_secret = exporter_secret
149
- this.#max_seq = MaxSeq(suite.AEAD.Nn)
191
+ this.#state = createContextState(suite, mode, key, base_nonce, exporter_secret)
150
192
  }
151
193
 
152
194
  /**
@@ -155,7 +197,7 @@ class SenderContext {
155
197
  * @see {@link MODE_PSK}
156
198
  */
157
199
  get mode(): Mode {
158
- return this.#mode
200
+ return this.#state.mode
159
201
  }
160
202
 
161
203
  /**
@@ -165,7 +207,7 @@ class SenderContext {
165
207
  * and `2^53-1`.
166
208
  */
167
209
  get seq(): number {
168
- return this.#seq
210
+ return this.#state.seq
169
211
  }
170
212
 
171
213
  /**
@@ -192,27 +234,28 @@ class SenderContext {
192
234
  *
193
235
  * @returns A Promise that resolves to the ciphertext. The ciphertext is {@link Nt} bytes longer
194
236
  * than the plaintext.
195
- * @see [Context.Seal](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.2)
237
+ * @see [Context.Seal](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.2)
196
238
  */
197
239
  async Seal(plaintext: Uint8Array, aad?: Uint8Array): Promise<Uint8Array> {
198
240
  checkUint8Array(plaintext, 'plaintext')
199
241
  aad ??= new Uint8Array()
200
242
  checkUint8Array(aad, 'aad')
201
- if (this.#suite.AEAD.id === EXPORT_ONLY) {
243
+ const state = this.#state
244
+ if (state.suite.AEAD.id === EXPORT_ONLY) {
202
245
  throw new TypeError('Export-only AEAD cannot be used with Seal')
203
246
  }
204
247
 
205
- this.#mutex ??= new Mutex()
206
- const release = await this.#mutex.lock()
248
+ state.mutex ??= new Mutex()
249
+ const release = await state.mutex.lock()
207
250
  let ct: Uint8Array
208
251
  try {
209
- ct = await this.#suite.AEAD.Seal(
210
- this.#key,
211
- ComputeNonce(this.#base_nonce, this.#seq, this.#suite.AEAD.Nn),
252
+ ct = await state.suite.AEAD.Seal(
253
+ state.key,
254
+ ComputeNonce(state.base_nonce, state.seq, state.suite.AEAD.Nn),
212
255
  aad,
213
256
  plaintext,
214
257
  )
215
- this.#seq = IncrementSeq(this.#seq, this.#max_seq)
258
+ state.seq = IncrementSeq(state.seq, state.max_seq)
216
259
  return ct
217
260
  } finally {
218
261
  release()
@@ -224,6 +267,11 @@ class SenderContext {
224
267
  *
225
268
  * The exported secret is indistinguishable from a uniformly random bitstring of equal length.
226
269
  *
270
+ * HPKE does not provide replay protection. A replayed encapsulated secret produces the same
271
+ * exported secrets. Applications must only use these secrets where repeated values are safe.
272
+ * Deriving AEAD keys and nonces requires a fresh value for each derivation, such as a
273
+ * recipient-provided nonce.
274
+ *
227
275
  * @example
228
276
  *
229
277
  * ```ts
@@ -240,10 +288,15 @@ class SenderContext {
240
288
  * @param length - Desired length of exported secret in bytes
241
289
  *
242
290
  * @returns A Promise that resolves to the exported secret.
243
- * @see [Context.Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.3)
291
+ * @see [Context.Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.3)
244
292
  */
245
293
  async Export(exporterContext: Uint8Array, length: number): Promise<Uint8Array> {
246
- return await ContextExport(this.#suite, this.#exporter_secret, exporterContext, length)
294
+ return await ContextExport(
295
+ this.#state.suite,
296
+ this.#state.exporter_secret,
297
+ exporterContext,
298
+ length,
299
+ )
247
300
  }
248
301
 
249
302
  /**
@@ -251,7 +304,7 @@ class SenderContext {
251
304
  * context.
252
305
  */
253
306
  get Nt(): number {
254
- return this.#suite.AEAD.Nt
307
+ return this.#state.suite.AEAD.Nt
255
308
  }
256
309
  }
257
310
  export type { SenderContext }
@@ -276,14 +329,7 @@ export type { SenderContext }
276
329
  * @group Core
277
330
  */
278
331
  class RecipientContext {
279
- #suite: Triple
280
- #key: Uint8Array
281
- #base_nonce: Uint8Array
282
- #exporter_secret: Uint8Array
283
- #mode: Mode
284
- #seq: number = 0
285
- #max_seq: number
286
- #mutex?: Mutex
332
+ #state: ContextState
287
333
 
288
334
  constructor(
289
335
  suite: Triple,
@@ -292,12 +338,7 @@ class RecipientContext {
292
338
  base_nonce: Uint8Array,
293
339
  exporter_secret: Uint8Array,
294
340
  ) {
295
- this.#suite = suite
296
- this.#mode = mode
297
- this.#key = key
298
- this.#base_nonce = base_nonce
299
- this.#exporter_secret = exporter_secret
300
- this.#max_seq = MaxSeq(suite.AEAD.Nn)
341
+ this.#state = createContextState(suite, mode, key, base_nonce, exporter_secret)
301
342
  }
302
343
 
303
344
  /**
@@ -306,7 +347,7 @@ class RecipientContext {
306
347
  * @see {@link MODE_PSK}
307
348
  */
308
349
  get mode(): Mode {
309
- return this.#mode
350
+ return this.#state.mode
310
351
  }
311
352
 
312
353
  /**
@@ -316,7 +357,7 @@ class RecipientContext {
316
357
  * and `2^53-1`.
317
358
  */
318
359
  get seq(): number {
319
- return this.#seq
360
+ return this.#state.seq
320
361
  }
321
362
 
322
363
  /**
@@ -344,25 +385,26 @@ class RecipientContext {
344
385
  * @param aad - Additional authenticated data
345
386
  *
346
387
  * @returns A Promise that resolves to the decrypted plaintext.
347
- * @see [Context.Open](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.2)
388
+ * @see [Context.Open](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.2)
348
389
  */
349
390
  async Open(ciphertext: Uint8Array, aad?: Uint8Array): Promise<Uint8Array> {
350
391
  checkUint8Array(ciphertext, 'ciphertext')
351
392
  aad ??= new Uint8Array()
352
393
  checkUint8Array(aad, 'aad')
353
394
 
354
- if (this.#suite.AEAD.id === EXPORT_ONLY) {
395
+ const state = this.#state
396
+ if (state.suite.AEAD.id === EXPORT_ONLY) {
355
397
  throw new TypeError('Export-only AEAD cannot be used with Open')
356
398
  }
357
399
 
358
- this.#mutex ??= new Mutex()
359
- const release = await this.#mutex.lock()
400
+ state.mutex ??= new Mutex()
401
+ const release = await state.mutex.lock()
360
402
  try {
361
403
  let pt: Uint8Array
362
404
  try {
363
- pt = await this.#suite.AEAD.Open(
364
- this.#key,
365
- ComputeNonce(this.#base_nonce, this.#seq, this.#suite.AEAD.Nn),
405
+ pt = await state.suite.AEAD.Open(
406
+ state.key,
407
+ ComputeNonce(state.base_nonce, state.seq, state.suite.AEAD.Nn),
366
408
  aad,
367
409
  ciphertext,
368
410
  )
@@ -373,7 +415,7 @@ class RecipientContext {
373
415
 
374
416
  throw new OpenError('AEAD decryption failed', { cause })
375
417
  }
376
- this.#seq = IncrementSeq(this.#seq, this.#max_seq)
418
+ state.seq = IncrementSeq(state.seq, state.max_seq)
377
419
  return pt
378
420
  } finally {
379
421
  release()
@@ -385,6 +427,11 @@ class RecipientContext {
385
427
  *
386
428
  * The exported secret is indistinguishable from a uniformly random bitstring of equal length.
387
429
  *
430
+ * HPKE does not provide replay protection. A replayed encapsulated secret produces the same
431
+ * exported secrets. Applications must only use these secrets where repeated values are safe.
432
+ * Deriving AEAD keys and nonces requires a fresh value for each derivation, such as a
433
+ * recipient-provided nonce.
434
+ *
388
435
  * @example
389
436
  *
390
437
  * ```ts
@@ -401,10 +448,15 @@ class RecipientContext {
401
448
  * @param length - Desired length of exported secret in bytes
402
449
  *
403
450
  * @returns A Promise that resolves to the exported secret.
404
- * @see [Context.Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.3)
451
+ * @see [Context.Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.3)
405
452
  */
406
453
  async Export(exporterContext: Uint8Array, length: number): Promise<Uint8Array> {
407
- return await ContextExport(this.#suite, this.#exporter_secret, exporterContext, length)
454
+ return await ContextExport(
455
+ this.#state.suite,
456
+ this.#state.exporter_secret,
457
+ exporterContext,
458
+ length,
459
+ )
408
460
  }
409
461
  }
410
462
  export type { RecipientContext }
@@ -432,7 +484,7 @@ const validate = <T extends { type: string }>(factory: () => T, type: string): T
432
484
  * asymmetric key exchange with a symmetric cipher. This was originally defined in an Informational
433
485
  * document on the IRTF stream as [RFC 9180](https://www.rfc-editor.org/rfc/rfc9180.html) and is now
434
486
  * being republished as a Standards Track document of the IETF as
435
- * [draft-ietf-hpke-hpke](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04).
487
+ * [draft-ietf-hpke-hpke](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06).
436
488
  *
437
489
  * HPKE provides a variant of public key encryption for arbitrary-sized plaintexts using a recipient
438
490
  * public key. It supports two modes:
@@ -503,12 +555,12 @@ export class CipherSuite<K extends Key = Key> {
503
555
  * )
504
556
  * ```
505
557
  *
506
- * @param KEM - KEM implementation factory. Must return an object conforming to the {@link KEM}
507
- * interface.
508
- * @param KDF - KDF implementation factory. Must return an object conforming to the {@link KDF}
509
- * interface.
510
- * @param AEAD - AEAD implementation factory. Must return an object conforming to the {@link AEAD}
511
- * interface.
558
+ * @param KEM - KEM implementation factory. Must return an object conforming to the
559
+ * {@link !KEM KEM} interface.
560
+ * @param KDF - KDF implementation factory. Must return an object conforming to the
561
+ * {@link !KDF KDF} interface.
562
+ * @param AEAD - AEAD implementation factory. Must return an object conforming to the
563
+ * {@link !AEAD AEAD} interface.
512
564
  * @see {@link KEMFactory Available KEMs}
513
565
  * @see {@link KDFFactory Available KDFs}
514
566
  * @see {@link AEADFactory Available AEADs}
@@ -798,6 +850,9 @@ export class CipherSuite<K extends Key = Key> {
798
850
  * Single-shot API for encrypting a single message. It combines context setup and encryption in
799
851
  * one call.
800
852
  *
853
+ * Applications should prefer `options.info` for authenticated information that applies to the
854
+ * whole context, reserving `options.aad` for information specific to the message.
855
+ *
801
856
  * Mode selection:
802
857
  *
803
858
  * - If the options `psk` and `pskId` are omitted: Base mode (unauthenticated)
@@ -826,7 +881,8 @@ export class CipherSuite<K extends Key = Key> {
826
881
  * @returns A Promise that resolves to an object containing the encapsulated secret and
827
882
  * ciphertext. The ciphertext is {@link CipherSuite.AEAD Nt} bytes longer than the plaintext. The
828
883
  * encapsulated secret is {@link CipherSuite.KEM Nenc} bytes.
829
- * @see [Single-Shot Encryption](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-6.1)
884
+ * @see [Single-Shot Encryption](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-6.1)
885
+ * @see [Auxiliary Authenticated Application Information](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-8.1)
830
886
  */
831
887
  async Seal(
832
888
  publicKey: K,
@@ -846,6 +902,9 @@ export class CipherSuite<K extends Key = Key> {
846
902
  *
847
903
  * It combines context setup and decryption in one call.
848
904
  *
905
+ * Applications should prefer `options.info` for authenticated information that applies to the
906
+ * whole context, reserving `options.aad` for information specific to the message.
907
+ *
849
908
  * Mode selection:
850
909
  *
851
910
  * - If the options `psk` and `pskId` are omitted: Base mode (unauthenticated)
@@ -875,7 +934,8 @@ export class CipherSuite<K extends Key = Key> {
875
934
  * @param options.pskId - Pre-shared key identifier (for PSK mode)
876
935
  *
877
936
  * @returns A Promise that resolves to the decrypted plaintext.
878
- * @see [Single-Shot Decryption](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-6.1)
937
+ * @see [Single-Shot Decryption](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-6.1)
938
+ * @see [Auxiliary Authenticated Application Information](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-8.1)
879
939
  */
880
940
  async Open(
881
941
  privateKey: K | KeyPair<K>,
@@ -895,8 +955,16 @@ export class CipherSuite<K extends Key = Key> {
895
955
  *
896
956
  * It combines context setup and secret export in one call.
897
957
  *
958
+ * Applications should prefer `options.info` for authenticated information that applies to the
959
+ * whole context, reserving `exporterContext` for information specific to the exported secret.
960
+ *
898
961
  * The exported secret is indistinguishable from a uniformly random bitstring of equal length.
899
962
  *
963
+ * HPKE does not provide replay protection. A replayed encapsulated secret produces the same
964
+ * exported secrets. Applications must only use these secrets where repeated values are safe.
965
+ * Deriving AEAD keys and nonces requires a fresh value for each derivation, such as a
966
+ * recipient-provided nonce.
967
+ *
900
968
  * @category Single-Shot APIs
901
969
  * @example
902
970
  *
@@ -923,7 +991,8 @@ export class CipherSuite<K extends Key = Key> {
923
991
  *
924
992
  * @returns A Promise that resolves to an object containing the encapsulated secret and the
925
993
  * exported secret.
926
- * @see [Single-Shot Secret Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-6.2)
994
+ * @see [Single-Shot Secret Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-6.2)
995
+ * @see [Auxiliary Authenticated Application Information](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-8.1)
927
996
  */
928
997
  async SendExport(
929
998
  publicKey: K,
@@ -941,6 +1010,14 @@ export class CipherSuite<K extends Key = Key> {
941
1010
  *
942
1011
  * It combines context setup and secret export in one call.
943
1012
  *
1013
+ * Applications should prefer `options.info` for authenticated information that applies to the
1014
+ * whole context, reserving `exporterContext` for information specific to the exported secret.
1015
+ *
1016
+ * HPKE does not provide replay protection. A replayed encapsulated secret produces the same
1017
+ * exported secrets. Applications must only use these secrets where repeated values are safe.
1018
+ * Deriving AEAD keys and nonces requires a fresh value for each derivation, such as a
1019
+ * recipient-provided nonce.
1020
+ *
944
1021
  * @category Single-Shot APIs
945
1022
  * @example
946
1023
  *
@@ -971,7 +1048,8 @@ export class CipherSuite<K extends Key = Key> {
971
1048
  * @param options.pskId - Pre-shared key identifier (for PSK mode)
972
1049
  *
973
1050
  * @returns A Promise that resolves to the exported secret.
974
- * @see [Single-Shot Secret Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-6.2)
1051
+ * @see [Single-Shot Secret Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-6.2)
1052
+ * @see [Auxiliary Authenticated Application Information](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-8.1)
975
1053
  */
976
1054
  async ReceiveExport(
977
1055
  privateKey: K | KeyPair<K>,
@@ -1025,7 +1103,7 @@ export class CipherSuite<K extends Key = Key> {
1025
1103
  *
1026
1104
  * @returns A Promise that resolves to an object containing the encapsulated secret and the sender
1027
1105
  * context (`ctx`). The encapsulated secret is {@link CipherSuite.KEM Nenc} bytes.
1028
- * @see [SetupBaseS / SetupPSKS](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.1.1)
1106
+ * @see [SetupBaseS / SetupPSKS](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.1.1)
1029
1107
  */
1030
1108
  async SetupSender(
1031
1109
  publicKey: K,
@@ -1106,7 +1184,7 @@ export class CipherSuite<K extends Key = Key> {
1106
1184
  * @param options.pskId - Pre-shared key identifier (for PSK mode)
1107
1185
  *
1108
1186
  * @returns A Promise that resolves to the recipient context.
1109
- * @see [SetupBaseR / SetupPSKR](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.1.1)
1187
+ * @see [SetupBaseR / SetupPSKR](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.1.1)
1110
1188
  */
1111
1189
  async SetupRecipient(
1112
1190
  privateKey: K | KeyPair<K>,
@@ -1293,7 +1371,7 @@ interface Triple<K extends Key = Key> {
1293
1371
  * Base mode provides encryption to a public key without sender authentication. The recipient cannot
1294
1372
  * verify who encrypted the message, only that someone with access to their public key did.
1295
1373
  *
1296
- * @see [HPKE Modes](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5)
1374
+ * @see [HPKE Modes](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5)
1297
1375
  */
1298
1376
  export const MODE_BASE = 0x00
1299
1377
 
@@ -1304,7 +1382,7 @@ export const MODE_BASE = 0x00
1304
1382
  * sender and recipient must possess the same PSK and PSK ID. This provides implicit sender
1305
1383
  * authentication.
1306
1384
  *
1307
- * @see [HPKE Modes](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5)
1385
+ * @see [HPKE Modes](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5)
1308
1386
  */
1309
1387
  export const MODE_PSK = 0x01
1310
1388
 
@@ -1515,6 +1593,38 @@ export function encode(string: string): Uint8Array {
1515
1593
  return bytes
1516
1594
  }
1517
1595
 
1596
+ /**
1597
+ * Integer to Octet String Primitive (I2OSP) as defined in RFC 8017. Converts a non-negative integer
1598
+ * into a byte string of specified length. It's exported for use in custom KEM, KDF, or AEAD
1599
+ * implementations.
1600
+ *
1601
+ * @param n - Non-negative safe integer to convert
1602
+ * @param w - Desired length of output in bytes
1603
+ *
1604
+ * @returns A Uint8Array of length w containing the big-endian representation of n
1605
+ * @group Utilities
1606
+ * @see [I2OSP](https://www.rfc-editor.org/rfc/rfc8017#section-4.1)
1607
+ */
1608
+ export function I2OSP(n: number, w: number): Uint8Array {
1609
+ if (!Number.isSafeInteger(w) || w <= 0) {
1610
+ throw new Error('w must be a positive safe integer')
1611
+ }
1612
+ if (!Number.isSafeInteger(n) || n < 0) {
1613
+ throw new Error('n must be a non-negative safe integer')
1614
+ }
1615
+ const max = Math.pow(256, w)
1616
+ if (n >= max) {
1617
+ throw new Error('n too large to fit in w-length byte string')
1618
+ }
1619
+ const ret = new Uint8Array(w)
1620
+ let num = n
1621
+ for (let i = 0; i < w && num; i++) {
1622
+ ret[w - (i + 1)] = num % 256
1623
+ num = Math.floor(num / 256)
1624
+ }
1625
+ return ret
1626
+ }
1627
+
1518
1628
  // Module-level cached encodings of static ASCII labels used on HPKE hot paths.
1519
1629
  // Each is a freshly-allocated Uint8Array held as a constant to avoid repeating
1520
1630
  // the per-call `encode()` validation+allocation loop. These are only ever read
@@ -1542,88 +1652,13 @@ function lengthPrefixed(x: Uint8Array): Uint8Array {
1542
1652
  }
1543
1653
 
1544
1654
  // ============================================================================
1545
- // KDF (Key Derivation Function) - Helper Functions
1655
+ // Input Validation
1546
1656
  // ============================================================================
1547
1657
 
1548
- /**
1549
- * Performs labeled key derivation for one-stage KDFs.
1550
- *
1551
- * This function implements the LabeledDerive operation as specified in the HPKE specification for
1552
- * use with one-stage KDFs. It constructs a labeled input by concatenating:
1553
- *
1554
- * - The input keying material (`ikm`)
1555
- * - The version string "HPKE-v1"
1556
- * - The suite identifier (`suite_id`)
1557
- * - A length-prefixed label
1558
- * - The desired output length as a 2-byte encoding
1559
- * - Additional context
1560
- *
1561
- * The labeled input is then passed to the KDF's Derive function to produce L bytes of output. This
1562
- * ensures domain separation between different uses of the KDF in HPKE.
1563
- *
1564
- * @group Utilities
1565
- * @see [LabeledDerive](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-5)
1566
- */
1567
- export async function LabeledDerive(
1568
- KDF: Pick<KDF, 'Derive'>,
1569
- suite_id: Uint8Array,
1570
- ikm: Uint8Array,
1571
- label: Uint8Array,
1572
- context: Uint8Array,
1573
- L: number,
1574
- ): Promise<Uint8Array> {
1575
- const labeled_ikm = concat(ikm, L_HPKE_v1, suite_id, lengthPrefixed(label), I2OSP(L, 2), context)
1576
- return await KDF.Derive(labeled_ikm, L)
1577
- }
1578
-
1579
- /** @see [Export_OneStage](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-5) */
1580
- async function Export_OneStage(
1581
- KDF: KDF,
1582
- suite_id: Uint8Array,
1583
- exporter_secret: Uint8Array,
1584
- exporter_context: Uint8Array,
1585
- L: number,
1586
- ) {
1587
- checkLength(exporter_context, 'Exporter context', MAX_LENGTH_ONE_STAGE)
1588
- return await LabeledDerive(KDF, suite_id, exporter_secret, L_sec, exporter_context, L)
1589
- }
1590
-
1591
- /** @see [CombineSecrets_OneStage](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-5) */
1592
- async function CombineSecrets_OneStage(
1593
- suite: Triple,
1594
- mode: number,
1595
- shared_secret: Uint8Array,
1596
- info: Uint8Array,
1597
- psk: Uint8Array,
1598
- psk_id: Uint8Array,
1599
- ) {
1600
- checkLength(psk, 'PSK', MAX_LENGTH_ONE_STAGE)
1601
- checkLength(psk_id, 'PSK ID', MAX_LENGTH_ONE_STAGE)
1602
- checkLength(info, 'Info', MAX_LENGTH_ONE_STAGE)
1603
-
1604
- const secrets = concat(lengthPrefixed(psk), lengthPrefixed(shared_secret))
1605
- const context = concat(I2OSP(mode, 1), lengthPrefixed(psk_id), lengthPrefixed(info))
1606
-
1607
- const secret = await LabeledDerive(
1608
- suite.KDF,
1609
- suite.id,
1610
- secrets,
1611
- L_secret,
1612
- context,
1613
- suite.AEAD.Nk + suite.AEAD.Nn + suite.KDF.Nh,
1614
- )
1615
-
1616
- const key = slice(secret, 0, suite.AEAD.Nk)
1617
- const base_nonce = slice(secret, suite.AEAD.Nk, suite.AEAD.Nk + suite.AEAD.Nn)
1618
- const exporter_secret = slice(secret, suite.AEAD.Nk + suite.AEAD.Nn)
1619
-
1620
- return { key, base_nonce, exporter_secret }
1621
- }
1622
-
1623
- // Two-stage KDF input length limits (0xffff = 65535 bytes)
1624
- // this is an actual limit for One-Stage KDF
1658
+ // Apply a 65535-byte implementation limit to two-stage KDF inputs.
1625
1659
  const MAX_LENGTH_TWO_STAGE = 0xffff
1626
- // that is also applied to Two-Stage KDF for consistency
1660
+ // One-stage length-prefixed PSK, PSK ID, and info inputs are limited to 65535 bytes.
1661
+ // Apply the same bound to one-stage exporter contexts as an implementation limit.
1627
1662
  const MAX_LENGTH_ONE_STAGE = 0xffff
1628
1663
 
1629
1664
  function checkLength(data: Uint8Array, name: string, maxLength: number) {
@@ -1645,60 +1680,9 @@ function checkExtractable(extractable: unknown): asserts extractable is boolean
1645
1680
  }
1646
1681
  }
1647
1682
 
1648
- /** @see [KeySchedule (CombineSecrets)](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.1) */
1649
- async function CombineSecrets_TwoStage(
1650
- suite: Triple,
1651
- mode: number,
1652
- shared_secret: Uint8Array,
1653
- info: Uint8Array,
1654
- psk: Uint8Array,
1655
- psk_id: Uint8Array,
1656
- ) {
1657
- checkLength(psk, 'PSK', MAX_LENGTH_TWO_STAGE)
1658
- checkLength(psk_id, 'PSK ID', MAX_LENGTH_TWO_STAGE)
1659
- checkLength(info, 'Info', MAX_LENGTH_TWO_STAGE)
1660
-
1661
- const [psk_id_hash, info_hash] = await Promise.all([
1662
- LabeledExtract(suite.KDF, suite.id, new Uint8Array(), L_psk_id_hash, psk_id),
1663
- LabeledExtract(suite.KDF, suite.id, new Uint8Array(), L_info_hash, info),
1664
- ])
1665
-
1666
- const key_schedule_context = concat(I2OSP(mode, 1), psk_id_hash, info_hash)
1667
- const secret = await LabeledExtract(suite.KDF, suite.id, shared_secret, L_secret, psk)
1668
-
1669
- // For export-only AEAD, we only need the exporter_secret
1670
- if (suite.AEAD.id === EXPORT_ONLY) {
1671
- const exporter_secret = await LabeledExpand(
1672
- suite.KDF,
1673
- suite.id,
1674
- secret,
1675
- L_exp,
1676
- key_schedule_context,
1677
- suite.KDF.Nh,
1678
- )
1679
- return { key: new Uint8Array(), base_nonce: new Uint8Array(), exporter_secret }
1680
- }
1681
-
1682
- const [key, base_nonce, exporter_secret] = await Promise.all([
1683
- LabeledExpand(suite.KDF, suite.id, secret, L_key, key_schedule_context, suite.AEAD.Nk),
1684
- LabeledExpand(suite.KDF, suite.id, secret, L_base_nonce, key_schedule_context, suite.AEAD.Nn),
1685
- LabeledExpand(suite.KDF, suite.id, secret, L_exp, key_schedule_context, suite.KDF.Nh),
1686
- ])
1687
-
1688
- return { key, base_nonce, exporter_secret }
1689
- }
1690
-
1691
- /** @see [Context.Export](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.3) */
1692
- async function Export_TwoStage(
1693
- KDF: KDF,
1694
- suite_id: Uint8Array,
1695
- exporter_secret: Uint8Array,
1696
- exporter_context: Uint8Array,
1697
- L: number,
1698
- ) {
1699
- checkLength(exporter_context, 'Exporter context', MAX_LENGTH_TWO_STAGE)
1700
- return await LabeledExpand(KDF, suite_id, exporter_secret, L_sec, exporter_context, L)
1701
- }
1683
+ // ============================================================================
1684
+ // KDF (Key Derivation Function) - Types and Interfaces
1685
+ // ============================================================================
1702
1686
 
1703
1687
  /**
1704
1688
  * Key Derivation Function (KDF) implementation interface.
@@ -1719,6 +1703,9 @@ async function Export_TwoStage(
1719
1703
  * - Two-stage KDFs only implement {@link Extract} and {@link Expand}. The {@link Derive} method will not
1720
1704
  * be called and may be a no-op implementation.
1721
1705
  *
1706
+ * Custom KDF implementations must also meet the [KDF security
1707
+ * requirements](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-9.3).
1708
+ *
1722
1709
  * @example
1723
1710
  *
1724
1711
  * ```ts
@@ -1765,7 +1752,7 @@ async function Export_TwoStage(
1765
1752
  * )
1766
1753
  * ```
1767
1754
  *
1768
- * @see [HPKE Key Derivation Functions](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-4.2)
1755
+ * @see [HPKE Key Derivation Functions](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-4.2)
1769
1756
  */
1770
1757
  export interface KDF {
1771
1758
  /** KDF algorithm identifier */
@@ -1820,9 +1807,40 @@ export interface KDF {
1820
1807
  }
1821
1808
 
1822
1809
  // ============================================================================
1823
- // KDF (Key Derivation Function) - Labeled Extract/Expand Functions
1810
+ // KDF (Key Derivation Function) - Labeled Derivation Functions
1824
1811
  // ============================================================================
1825
1812
 
1813
+ /**
1814
+ * Performs labeled key derivation for one-stage KDFs.
1815
+ *
1816
+ * This function implements the LabeledDerive operation as specified in the HPKE specification for
1817
+ * use with one-stage KDFs. It constructs a labeled input by concatenating:
1818
+ *
1819
+ * - The input keying material (`ikm`)
1820
+ * - The version string "HPKE-v1"
1821
+ * - The suite identifier (`suite_id`)
1822
+ * - A length-prefixed label
1823
+ * - The desired output length as a 2-byte encoding
1824
+ * - Additional context
1825
+ *
1826
+ * The labeled input is then passed to the KDF's Derive function to produce L bytes of output. This
1827
+ * ensures domain separation between different uses of the KDF in HPKE.
1828
+ *
1829
+ * @group Utilities
1830
+ * @see [LabeledDerive](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-4.4)
1831
+ */
1832
+ export async function LabeledDerive(
1833
+ KDF: Pick<KDF, 'Derive'>,
1834
+ suite_id: Uint8Array,
1835
+ ikm: Uint8Array,
1836
+ label: Uint8Array,
1837
+ context: Uint8Array,
1838
+ L: number,
1839
+ ): Promise<Uint8Array> {
1840
+ const labeled_ikm = concat(ikm, L_HPKE_v1, suite_id, lengthPrefixed(label), I2OSP(L, 2), context)
1841
+ return await KDF.Derive(labeled_ikm, L)
1842
+ }
1843
+
1826
1844
  /**
1827
1845
  * Performs labeled extraction for two-stage KDFs.
1828
1846
  *
@@ -1838,7 +1856,7 @@ export interface KDF {
1838
1856
  * pseudorandom key. This ensures domain separation between different uses of the KDF in HPKE.
1839
1857
  *
1840
1858
  * @group Utilities
1841
- * @see [LabeledExtract](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-4.4)
1859
+ * @see [LabeledExtract](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-4.4)
1842
1860
  */
1843
1861
  export async function LabeledExtract(
1844
1862
  KDF: Pick<KDF, 'Extract'>,
@@ -1868,7 +1886,7 @@ export async function LabeledExtract(
1868
1886
  * of the KDF in HPKE.
1869
1887
  *
1870
1888
  * @group Utilities
1871
- * @see [LabeledExpand](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-4.4)
1889
+ * @see [LabeledExpand](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-4.4)
1872
1890
  */
1873
1891
  export async function LabeledExpand(
1874
1892
  KDF: Pick<KDF, 'Expand'>,
@@ -1978,7 +1996,7 @@ export async function LabeledExpand(
1978
1996
  * ```
1979
1997
  *
1980
1998
  * @typeParam K - Key representation produced and consumed by the KEM
1981
- * @see [HPKE Key Encapsulation Mechanisms](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-4.1)
1999
+ * @see [HPKE Key Encapsulation Mechanisms](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-4.1)
1982
2000
  */
1983
2001
  export interface KEM<K extends Key = Key> {
1984
2002
  /** KEM algorithm identifier */
@@ -2180,7 +2198,7 @@ function isKey(key: unknown, type: string, extractable?: boolean): asserts key i
2180
2198
  * )
2181
2199
  * ```
2182
2200
  *
2183
- * @see [HPKE AEAD Encryption Algorithm](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-4.3)
2201
+ * @see [HPKE AEAD Encryption Algorithm](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-4.3)
2184
2202
  */
2185
2203
  export interface AEAD {
2186
2204
  /** AEAD algorithm identifier */
@@ -2215,7 +2233,7 @@ export interface AEAD {
2215
2233
  * @param pt - Plaintext to encrypt
2216
2234
  *
2217
2235
  * @returns A promise resolving to the ciphertext with authentication tag appended
2218
- * @see [Context.Seal P_MAX handling](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.2)
2236
+ * @see [Context.Seal P_MAX handling](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.2)
2219
2237
  */
2220
2238
  Seal(key: Uint8Array, nonce: Uint8Array, aad: Uint8Array, pt: Uint8Array): Promise<Uint8Array>
2221
2239
 
@@ -2236,38 +2254,6 @@ export interface AEAD {
2236
2254
  // HPKE Core Functions - Key Schedule
2237
2255
  // ============================================================================
2238
2256
 
2239
- /**
2240
- * Integer to Octet String Primitive (I2OSP) as defined in RFC 8017. Converts a non-negative integer
2241
- * into a byte string of specified length. It's exported for use in custom KEM, KDF, or AEAD
2242
- * implementations.
2243
- *
2244
- * @param n - Non-negative safe integer to convert
2245
- * @param w - Desired length of output in bytes
2246
- *
2247
- * @returns A Uint8Array of length w containing the big-endian representation of n
2248
- * @group Utilities
2249
- * @see [I2OSP](https://www.rfc-editor.org/rfc/rfc8017#section-4.1)
2250
- */
2251
- export function I2OSP(n: number, w: number): Uint8Array {
2252
- if (!Number.isSafeInteger(w) || w <= 0) {
2253
- throw new Error('w must be a positive safe integer')
2254
- }
2255
- if (!Number.isSafeInteger(n) || n < 0) {
2256
- throw new Error('n must be a non-negative safe integer')
2257
- }
2258
- const max = Math.pow(256, w)
2259
- if (n >= max) {
2260
- throw new Error('n too large to fit in w-length byte string')
2261
- }
2262
- const ret = new Uint8Array(w)
2263
- let num = n
2264
- for (let i = 0; i < w && num; i++) {
2265
- ret[w - (i + 1)] = num % 256
2266
- num = Math.floor(num / 256)
2267
- }
2268
- return ret
2269
- }
2270
-
2271
2257
  function KDFStages(KDF: KDF): 1 | 2 {
2272
2258
  if (KDF.stages === 1 || KDF.stages === 2) {
2273
2259
  return KDF.stages
@@ -2276,7 +2262,82 @@ function KDFStages(KDF: KDF): 1 | 2 {
2276
2262
  throw new Error('unreachable')
2277
2263
  }
2278
2264
 
2279
- /** @see [KeySchedule](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.1) */
2265
+ /** @see [CombineSecrets](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.1) */
2266
+ async function CombineSecrets_OneStage(
2267
+ suite: Triple,
2268
+ mode: number,
2269
+ shared_secret: Uint8Array,
2270
+ info: Uint8Array,
2271
+ psk: Uint8Array,
2272
+ psk_id: Uint8Array,
2273
+ ) {
2274
+ checkLength(psk, 'PSK', MAX_LENGTH_ONE_STAGE)
2275
+ checkLength(psk_id, 'PSK ID', MAX_LENGTH_ONE_STAGE)
2276
+ checkLength(info, 'Info', MAX_LENGTH_ONE_STAGE)
2277
+
2278
+ const secrets = concat(lengthPrefixed(psk), lengthPrefixed(shared_secret))
2279
+ const context = concat(I2OSP(mode, 1), lengthPrefixed(psk_id), lengthPrefixed(info))
2280
+
2281
+ const secret = await LabeledDerive(
2282
+ suite.KDF,
2283
+ suite.id,
2284
+ secrets,
2285
+ L_secret,
2286
+ context,
2287
+ suite.AEAD.Nk + suite.AEAD.Nn + suite.KDF.Nh,
2288
+ )
2289
+
2290
+ const key = slice(secret, 0, suite.AEAD.Nk)
2291
+ const base_nonce = slice(secret, suite.AEAD.Nk, suite.AEAD.Nk + suite.AEAD.Nn)
2292
+ const exporter_secret = slice(secret, suite.AEAD.Nk + suite.AEAD.Nn)
2293
+
2294
+ return { key, base_nonce, exporter_secret }
2295
+ }
2296
+
2297
+ /** @see [CombineSecrets](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.1) */
2298
+ async function CombineSecrets_TwoStage(
2299
+ suite: Triple,
2300
+ mode: number,
2301
+ shared_secret: Uint8Array,
2302
+ info: Uint8Array,
2303
+ psk: Uint8Array,
2304
+ psk_id: Uint8Array,
2305
+ ) {
2306
+ checkLength(psk, 'PSK', MAX_LENGTH_TWO_STAGE)
2307
+ checkLength(psk_id, 'PSK ID', MAX_LENGTH_TWO_STAGE)
2308
+ checkLength(info, 'Info', MAX_LENGTH_TWO_STAGE)
2309
+
2310
+ const [psk_id_hash, info_hash] = await Promise.all([
2311
+ LabeledExtract(suite.KDF, suite.id, new Uint8Array(), L_psk_id_hash, psk_id),
2312
+ LabeledExtract(suite.KDF, suite.id, new Uint8Array(), L_info_hash, info),
2313
+ ])
2314
+
2315
+ const key_schedule_context = concat(I2OSP(mode, 1), psk_id_hash, info_hash)
2316
+ const secret = await LabeledExtract(suite.KDF, suite.id, shared_secret, L_secret, psk)
2317
+
2318
+ // For export-only AEAD, we only need the exporter_secret
2319
+ if (suite.AEAD.id === EXPORT_ONLY) {
2320
+ const exporter_secret = await LabeledExpand(
2321
+ suite.KDF,
2322
+ suite.id,
2323
+ secret,
2324
+ L_exp,
2325
+ key_schedule_context,
2326
+ suite.KDF.Nh,
2327
+ )
2328
+ return { key: new Uint8Array(), base_nonce: new Uint8Array(), exporter_secret }
2329
+ }
2330
+
2331
+ const [key, base_nonce, exporter_secret] = await Promise.all([
2332
+ LabeledExpand(suite.KDF, suite.id, secret, L_key, key_schedule_context, suite.AEAD.Nk),
2333
+ LabeledExpand(suite.KDF, suite.id, secret, L_base_nonce, key_schedule_context, suite.AEAD.Nn),
2334
+ LabeledExpand(suite.KDF, suite.id, secret, L_exp, key_schedule_context, suite.KDF.Nh),
2335
+ ])
2336
+
2337
+ return { key, base_nonce, exporter_secret }
2338
+ }
2339
+
2340
+ /** @see [KeySchedule](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.1) */
2280
2341
  async function KeySchedule(
2281
2342
  suite: Triple,
2282
2343
  mode: number,
@@ -2299,7 +2360,7 @@ async function KeySchedule(
2299
2360
  return await CombineSecrets(suite, mode, shared_secret, info, psk, pskId)
2300
2361
  }
2301
2362
 
2302
- /** @see [VerifyPSKInputs](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-5.1) */
2363
+ /** @see [VerifyPSKInputs](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-5.1) */
2303
2364
  function VerifyPSKInputs(psk: Uint8Array, psk_id: Uint8Array) {
2304
2365
  if (psk.byteLength && psk_id.byteLength) {
2305
2366
  if (psk.byteLength < 32) {
@@ -2331,7 +2392,7 @@ const CHACHA20_POLY1305_P_MAX = 2 ** 38 - 64
2331
2392
  * This is a factory function that must be passed to the {@link CipherSuite} constructor.
2332
2393
  *
2333
2394
  * @group AEAD Algorithms
2334
- * @see [HPKE AEAD Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.3)
2395
+ * @see [HPKE AEAD Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.3)
2335
2396
  */
2336
2397
  export const AEAD_EXPORT_ONLY: AEADFactory = function (): AEAD {
2337
2398
  return {
@@ -2370,14 +2431,34 @@ interface HKDF extends KDF {
2370
2431
 
2371
2432
  type KDF_BASE = Pick<KDF, 'Expand' | 'Extract' | 'Derive' | 'stages'>
2372
2433
 
2373
- async function cacheValue<K extends object, V>(
2374
- cache: WeakMap<K, V>,
2375
- key: K,
2376
- init: () => Promise<V>,
2377
- ): Promise<V> {
2378
- const result = await init()
2379
- cache.set(key, result)
2380
- return result
2434
+ interface CryptoKeyCacheEntry {
2435
+ bytes: Uint8Array
2436
+ key: Promise<CryptoKey>
2437
+ }
2438
+
2439
+ async function getCachedCryptoKey(
2440
+ cache: WeakMap<Uint8Array, CryptoKeyCacheEntry>,
2441
+ keyBytes: Uint8Array,
2442
+ init: (bytes: Uint8Array) => Promise<CryptoKey>,
2443
+ ): Promise<CryptoKey> {
2444
+ const cached = cache.get(keyBytes)
2445
+ if (
2446
+ cached &&
2447
+ cached.bytes.byteLength === keyBytes.byteLength &&
2448
+ cached.bytes.every((byte, index) => byte === keyBytes[index])
2449
+ ) {
2450
+ return cached.key
2451
+ }
2452
+
2453
+ const bytes = slice(keyBytes)
2454
+ const entry = { bytes, key: init(bytes) }
2455
+ cache.set(keyBytes, entry)
2456
+ try {
2457
+ return await entry.key
2458
+ } catch (cause) {
2459
+ if (cache.get(keyBytes) === entry) cache.delete(keyBytes)
2460
+ throw cause
2461
+ }
2381
2462
  }
2382
2463
 
2383
2464
  function HKDF_SHARED(): KDF_BASE {
@@ -2388,12 +2469,7 @@ function HKDF_SHARED(): KDF_BASE {
2388
2469
  this.name,
2389
2470
  )
2390
2471
  }
2391
- const cache = new WeakMap<Uint8Array, Promise<CryptoKey>>()
2392
- function importPrk(this: HKDF, prk: Uint8Array): Promise<CryptoKey> {
2393
- const key = importKey.call(this, prk as BufferSource)
2394
- cache.set(prk, key)
2395
- return key
2396
- }
2472
+ const cache = new WeakMap<Uint8Array, CryptoKeyCacheEntry>()
2397
2473
  return {
2398
2474
  stages: 2,
2399
2475
  Derive: NotApplicable,
@@ -2414,7 +2490,9 @@ function HKDF_SHARED(): KDF_BASE {
2414
2490
  throw new Error('L must be <= 255*Nh')
2415
2491
  }
2416
2492
  const N = Math.ceil(L / this.Nh)
2417
- const key = await (cache.get(prk) ?? importPrk.call(this, prk))
2493
+ const key = await getCachedCryptoKey(cache, prk, (bytes) =>
2494
+ importKey.call(this, bytes as BufferSource),
2495
+ )
2418
2496
 
2419
2497
  const T = new Uint8Array(N * this.Nh)
2420
2498
  let T_prev = new Uint8Array()
@@ -2457,7 +2535,7 @@ function HKDF_SHARED(): KDF_BASE {
2457
2535
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
2458
2536
  *
2459
2537
  * @group KDF Algorithms
2460
- * @see [HPKE KDF Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.2)
2538
+ * @see [HPKE KDF Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.2)
2461
2539
  */
2462
2540
  export const KDF_HKDF_SHA256: KDFFactory = function (): HKDF {
2463
2541
  return { id: 0x0001, type: 'KDF', name: 'HKDF-SHA256', Nh: 32, hash: 'SHA-256', ...HKDF_SHARED() }
@@ -2480,7 +2558,7 @@ export const KDF_HKDF_SHA256: KDFFactory = function (): HKDF {
2480
2558
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
2481
2559
  *
2482
2560
  * @group KDF Algorithms
2483
- * @see [HPKE KDF Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.2)
2561
+ * @see [HPKE KDF Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.2)
2484
2562
  */
2485
2563
  export const KDF_HKDF_SHA384: KDFFactory = function (): HKDF {
2486
2564
  return { id: 0x0002, type: 'KDF', name: 'HKDF-SHA384', Nh: 48, hash: 'SHA-384', ...HKDF_SHARED() }
@@ -2503,7 +2581,7 @@ export const KDF_HKDF_SHA384: KDFFactory = function (): HKDF {
2503
2581
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
2504
2582
  *
2505
2583
  * @group KDF Algorithms
2506
- * @see [HPKE KDF Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.2)
2584
+ * @see [HPKE KDF Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.2)
2507
2585
  */
2508
2586
  export const KDF_HKDF_SHA512: KDFFactory = function (): HKDF {
2509
2587
  return { id: 0x0003, type: 'KDF', name: 'HKDF-SHA512', Nh: 64, hash: 'SHA-512', ...HKDF_SHARED() }
@@ -2551,7 +2629,7 @@ function SHAKE_SHARED(): KDF_BASE {
2551
2629
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
2552
2630
  *
2553
2631
  * @group KDF Algorithms
2554
- * @see [HPKE-PQ One-Stage KDFs](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-5)
2632
+ * @see [HPKE-PQ One-Stage KDFs](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05#section-5)
2555
2633
  */
2556
2634
  export const KDF_SHAKE128: KDFFactory = function (): SHAKE {
2557
2635
  return {
@@ -2581,7 +2659,7 @@ export const KDF_SHAKE128: KDFFactory = function (): SHAKE {
2581
2659
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
2582
2660
  *
2583
2661
  * @group KDF Algorithms
2584
- * @see [HPKE-PQ One-Stage KDFs](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-5)
2662
+ * @see [HPKE-PQ One-Stage KDFs](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05#section-5)
2585
2663
  */
2586
2664
  export const KDF_SHAKE256: KDFFactory = function (): SHAKE {
2587
2665
  return {
@@ -2611,7 +2689,7 @@ export const KDF_SHAKE256: KDFFactory = function (): SHAKE {
2611
2689
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
2612
2690
  *
2613
2691
  * @group KDF Algorithms
2614
- * @see [HPKE-PQ One-Stage KDFs](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-5)
2692
+ * @see [HPKE-PQ One-Stage KDFs](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05#section-5)
2615
2693
  */
2616
2694
  export const KDF_TurboSHAKE128: KDFFactory = function (): SHAKE {
2617
2695
  return {
@@ -2641,7 +2719,7 @@ export const KDF_TurboSHAKE128: KDFFactory = function (): SHAKE {
2641
2719
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
2642
2720
  *
2643
2721
  * @group KDF Algorithms
2644
- * @see [HPKE-PQ One-Stage KDFs](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-5)
2722
+ * @see [HPKE-PQ One-Stage KDFs](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05#section-5)
2645
2723
  */
2646
2724
  export const KDF_TurboSHAKE256: KDFFactory = function (): SHAKE {
2647
2725
  return {
@@ -2685,17 +2763,6 @@ async function getPublicKey(name: string, key: CryptoKey, usages: KeyUsage[]): P
2685
2763
  )
2686
2764
  }
2687
2765
 
2688
- /** This is a last resort check, Web Cryptography implementers should already be checking it */
2689
- function checkNotAllZeros(buffer: Uint8Array): void {
2690
- let or = 0
2691
- for (let i = 0; i < buffer.length; i++) {
2692
- or |= buffer[i]!
2693
- }
2694
- if (or === 0) {
2695
- throw new ValidationError('DH shared secret is an all-zero value')
2696
- }
2697
- }
2698
-
2699
2766
  type KEM_BASE<K extends Key = Key> = Pick<
2700
2767
  KEM<K>,
2701
2768
  | 'GenerateKeyPair'
@@ -2747,7 +2814,7 @@ function b64u(input: string): Uint8Array {
2747
2814
  // KEM (Key Encapsulation Mechanism) - DHKEM Helper Functions
2748
2815
  // ============================================================================
2749
2816
 
2750
- /** @see [DeriveKeyPair](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.1.3) */
2817
+ /** @see [DeriveKeyPair](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.1.3) */
2751
2818
  async function DeriveKeyPairBytes(
2752
2819
  DHKEM: DHKEM,
2753
2820
  ikm: Uint8Array,
@@ -2804,7 +2871,7 @@ function assertCryptoKey(key: Key): asserts key is CryptoKey {
2804
2871
  }
2805
2872
  }
2806
2873
 
2807
- /** @see [ExtractAndExpand](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-4.5) */
2874
+ /** @see [ExtractAndExpand](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-4.5) */
2808
2875
  async function ExtractAndExpand_OneStage(
2809
2876
  DHKEM: DHKEM,
2810
2877
  dh: Uint8Array,
@@ -2820,7 +2887,7 @@ async function ExtractAndExpand_OneStage(
2820
2887
  )
2821
2888
  }
2822
2889
 
2823
- /** @see [ExtractAndExpand](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-4.5) */
2890
+ /** @see [ExtractAndExpand](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-4.5) */
2824
2891
  async function ExtractAndExpand_TwoStage(
2825
2892
  DHKEM: DHKEM,
2826
2893
  dh: Uint8Array,
@@ -2837,7 +2904,7 @@ async function ExtractAndExpand_TwoStage(
2837
2904
  )
2838
2905
  }
2839
2906
 
2840
- /** @see [ExtractAndExpand](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-4.5) */
2907
+ /** @see [ExtractAndExpand](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-4.5) */
2841
2908
  async function ExtractAndExpand(
2842
2909
  DHKEM: DHKEM,
2843
2910
  dh: Uint8Array,
@@ -2894,7 +2961,6 @@ function DHKEM_SHARED(): Required<
2894
2961
  this.name,
2895
2962
  ),
2896
2963
  )
2897
- checkNotAllZeros(dh)
2898
2964
 
2899
2965
  const enc = await this.SerializePublicKey(pkE)
2900
2966
  const pkRm = await this.SerializePublicKey(pkR)
@@ -2922,7 +2988,6 @@ function DHKEM_SHARED(): Required<
2922
2988
  this.name,
2923
2989
  ),
2924
2990
  )
2925
- checkNotAllZeros(dh)
2926
2991
 
2927
2992
  const pkRm = await this.SerializePublicKey(pkR)
2928
2993
  const kem_context = concat(enc, pkRm)
@@ -3097,7 +3162,7 @@ async function DeserializePrivateKeyNist(
3097
3162
  return (await importKeyNist(this, key, this.name, extractable, false)).privateKey
3098
3163
  }
3099
3164
 
3100
- /** @see [DeriveKeyPair](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.1.3) */
3165
+ /** @see [DeriveKeyPair](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.1.3) */
3101
3166
  async function DeriveKeyPairNist(
3102
3167
  this: DHKEM & NistCurveConfig,
3103
3168
  ikm: Uint8Array,
@@ -3132,7 +3197,7 @@ async function GetKeyPairNist(
3132
3197
  // KEM (Key Encapsulation Mechanism) - DHKEM X Curve Implementations
3133
3198
  // ============================================================================
3134
3199
 
3135
- /** @see [DeriveKeyPair](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.1.3) */
3200
+ /** @see [DeriveKeyPair](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.1.3) */
3136
3201
  async function DeriveKeyPairX(this: DHKEM, ikm: Uint8Array, extractable: boolean) {
3137
3202
  const sk = await DeriveKeyPairBytes(this, ikm, L_sk, new Uint8Array())
3138
3203
  return await createKeyPairFromPrivateKey(this, sk, extractable)
@@ -3172,7 +3237,7 @@ const P256: NistCurveConfig = {
3172
3237
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3173
3238
  *
3174
3239
  * @group KEM Algorithms
3175
- * @see [HPKE KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.1)
3240
+ * @see [HPKE KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.1)
3176
3241
  */
3177
3242
  export const KEM_DHKEM_P256_HKDF_SHA256: KEMFactory<CryptoKey> = function (): DHKEM &
3178
3243
  NistCurveConfig {
@@ -3228,7 +3293,7 @@ const P384: NistCurveConfig = {
3228
3293
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3229
3294
  *
3230
3295
  * @group KEM Algorithms
3231
- * @see [HPKE KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.1)
3296
+ * @see [HPKE KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.1)
3232
3297
  */
3233
3298
  export const KEM_DHKEM_P384_HKDF_SHA384: KEMFactory<CryptoKey> = function (): DHKEM &
3234
3299
  NistCurveConfig {
@@ -3284,7 +3349,7 @@ const P521: NistCurveConfig = {
3284
3349
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3285
3350
  *
3286
3351
  * @group KEM Algorithms
3287
- * @see [HPKE KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.1)
3352
+ * @see [HPKE KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.1)
3288
3353
  */
3289
3354
  export const KEM_DHKEM_P521_HKDF_SHA512: KEMFactory<CryptoKey> = function (): DHKEM &
3290
3355
  NistCurveConfig {
@@ -3331,7 +3396,7 @@ export const KEM_DHKEM_P521_HKDF_SHA512: KEMFactory<CryptoKey> = function (): DH
3331
3396
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3332
3397
  *
3333
3398
  * @group KEM Algorithms
3334
- * @see [HPKE KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.1)
3399
+ * @see [HPKE KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.1)
3335
3400
  */
3336
3401
  export const KEM_DHKEM_X25519_HKDF_SHA256: KEMFactory<CryptoKey> = function (): DHKEM & {
3337
3402
  pkcs8: Uint8Array
@@ -3380,7 +3445,7 @@ export const KEM_DHKEM_X25519_HKDF_SHA256: KEMFactory<CryptoKey> = function ():
3380
3445
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3381
3446
  *
3382
3447
  * @group KEM Algorithms
3383
- * @see [HPKE KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.1)
3448
+ * @see [HPKE KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.1)
3384
3449
  */
3385
3450
  export const KEM_DHKEM_X448_HKDF_SHA512: KEMFactory<CryptoKey> = function (): DHKEM & {
3386
3451
  pkcs8: Uint8Array
@@ -3552,7 +3617,7 @@ function createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk }: KEMParams): Dire
3552
3617
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3553
3618
  *
3554
3619
  * @group KEM Algorithms
3555
- * @see [HPKE-PQ KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-3)
3620
+ * @see [HPKE-PQ KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05#section-3)
3556
3621
  */
3557
3622
  export const KEM_ML_KEM_512: KEMFactory<CryptoKey> = function (): DirectKEM {
3558
3623
  const id = 0x0040
@@ -3581,7 +3646,7 @@ export const KEM_ML_KEM_512: KEMFactory<CryptoKey> = function (): DirectKEM {
3581
3646
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3582
3647
  *
3583
3648
  * @group KEM Algorithms
3584
- * @see [HPKE-PQ KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-3)
3649
+ * @see [HPKE-PQ KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05#section-3)
3585
3650
  */
3586
3651
  export const KEM_ML_KEM_768: KEMFactory<CryptoKey> = function (): DirectKEM {
3587
3652
  const id = 0x0041
@@ -3610,7 +3675,7 @@ export const KEM_ML_KEM_768: KEMFactory<CryptoKey> = function (): DirectKEM {
3610
3675
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3611
3676
  *
3612
3677
  * @group KEM Algorithms
3613
- * @see [HPKE-PQ KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-3)
3678
+ * @see [HPKE-PQ KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05#section-3)
3614
3679
  */
3615
3680
  export const KEM_ML_KEM_1024: KEMFactory<CryptoKey> = function (): DirectKEM {
3616
3681
  const id = 0x0042
@@ -3631,11 +3696,11 @@ type AEAD_BASE = Pick<AEAD, 'Seal' | 'Open'>
3631
3696
 
3632
3697
  function AEAD_SHARED(P_MAX: number): AEAD_BASE {
3633
3698
  // Cache the WebCrypto CryptoKey derived from a given key-bytes Uint8Array.
3634
- // Across the lifetime of a SenderContext/RecipientContext, `this.#key` is a
3699
+ // Across the lifetime of a SenderContext/RecipientContext, the key is a
3635
3700
  // stable Uint8Array, so crypto.subtle.importKey() would otherwise be called
3636
3701
  // on every Seal/Open. The WeakMap ensures the cache entry is reclaimed once
3637
3702
  // the underlying Uint8Array is unreachable.
3638
- const cache = new WeakMap<Uint8Array, CryptoKey>()
3703
+ const cache = new WeakMap<Uint8Array, CryptoKeyCacheEntry>()
3639
3704
  async function importKey(this: WebCryptoAEAD, key: Uint8Array): Promise<CryptoKey> {
3640
3705
  return await subtle(
3641
3706
  (c) =>
@@ -3651,8 +3716,7 @@ function AEAD_SHARED(P_MAX: number): AEAD_BASE {
3651
3716
  if (pt.byteLength > P_MAX) {
3652
3717
  throw new RangeError('"pt" exceeds P_MAX')
3653
3718
  }
3654
- const cryptoKey =
3655
- cache.get(key) ?? (await cacheValue(cache, key, () => importKey.call(this, key)))
3719
+ const cryptoKey = await getCachedCryptoKey(cache, key, (bytes) => importKey.call(this, bytes))
3656
3720
  return new Uint8Array(
3657
3721
  await subtle(
3658
3722
  (c) =>
@@ -3670,8 +3734,7 @@ function AEAD_SHARED(P_MAX: number): AEAD_BASE {
3670
3734
  )
3671
3735
  },
3672
3736
  async Open(this: WebCryptoAEAD, key, nonce, aad, ct) {
3673
- const cryptoKey =
3674
- cache.get(key) ?? (await cacheValue(cache, key, () => importKey.call(this, key)))
3737
+ const cryptoKey = await getCachedCryptoKey(cache, key, (bytes) => importKey.call(this, bytes))
3675
3738
  return new Uint8Array(
3676
3739
  await subtle(
3677
3740
  (c) =>
@@ -3711,7 +3774,7 @@ function AEAD_SHARED(P_MAX: number): AEAD_BASE {
3711
3774
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3712
3775
  *
3713
3776
  * @group AEAD Algorithms
3714
- * @see [HPKE AEAD Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.3)
3777
+ * @see [HPKE AEAD Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.3)
3715
3778
  */
3716
3779
  export const AEAD_AES_128_GCM: AEADFactory = function (): WebCryptoAEAD {
3717
3780
  return {
@@ -3743,7 +3806,7 @@ export const AEAD_AES_128_GCM: AEADFactory = function (): WebCryptoAEAD {
3743
3806
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3744
3807
  *
3745
3808
  * @group AEAD Algorithms
3746
- * @see [HPKE AEAD Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.3)
3809
+ * @see [HPKE AEAD Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.3)
3747
3810
  */
3748
3811
  export const AEAD_AES_256_GCM: AEADFactory = function (): WebCryptoAEAD {
3749
3812
  return {
@@ -3775,7 +3838,7 @@ export const AEAD_AES_256_GCM: AEADFactory = function (): WebCryptoAEAD {
3775
3838
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
3776
3839
  *
3777
3840
  * @group AEAD Algorithms
3778
- * @see [HPKE AEAD Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-04.html#section-7.3)
3841
+ * @see [HPKE AEAD Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-hpke-06#section-7.3)
3779
3842
  */
3780
3843
  export const AEAD_ChaCha20Poly1305: AEADFactory = function AEAD_ChaCha20Poly1305(): WebCryptoAEAD {
3781
3844
  return {
@@ -3910,7 +3973,7 @@ function RandomScalarNist(t: CompositeHybridKEM['t'], seed: Uint8Array): Uint8Ar
3910
3973
  return bigIntToUint8Array(sk_bigint, t.Nscalar!)
3911
3974
  }
3912
3975
 
3913
- /** @see [expandDecapsKeyG](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12.html#section-5.1.1) */
3976
+ /** @see [expandDecapsKeyG](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12#section-5.1.1) */
3914
3977
  async function expandDecapsKeyG(kem: CompositeHybridKEM, seed: Uint8Array) {
3915
3978
  const Nout = kem.pq.Nseed + kem.t.Nseed
3916
3979
  const bits = Nout << 3
@@ -3936,7 +3999,7 @@ async function expandDecapsKeyG(kem: CompositeHybridKEM, seed: Uint8Array) {
3936
3999
  return { ek_PQ, ek_T, dk_PQ, dk_T }
3937
4000
  }
3938
4001
 
3939
- /** @see [C2PRICombiner](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12.html#section-5.1.3) */
4002
+ /** @see [C2PRICombiner](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12#section-5.1.3) */
3940
4003
  async function C2PRICombiner(
3941
4004
  kem: CompositeHybridKEM,
3942
4005
  ss_PQ: Uint8Array,
@@ -3950,7 +4013,7 @@ async function C2PRICombiner(
3950
4013
  return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data), kem.name))
3951
4014
  }
3952
4015
 
3953
- /** @see [prepareEncapsG](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12.html#section-5.1.1) */
4016
+ /** @see [prepareEncapsG](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12#section-5.1.1) */
3954
4017
  async function prepareEncapsG(
3955
4018
  kem: CompositeHybridKEM,
3956
4019
  ek_PQ: CryptoKey,
@@ -3976,12 +4039,11 @@ async function prepareEncapsG(
3976
4039
  kem.name,
3977
4040
  ),
3978
4041
  )
3979
- checkNotAllZeros(ss_T)
3980
4042
 
3981
4043
  return [ss_PQ, ss_T, ct_PQ, ct_T]
3982
4044
  }
3983
4045
 
3984
- /** @see [prepareDecapsG](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12.html#section-5.1.1) */
4046
+ /** @see [prepareDecapsG](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hybrid-kems-12#section-5.1.1) */
3985
4047
  async function prepareDecapsG(
3986
4048
  kem: CompositeHybridKEM,
3987
4049
  dk_PQ: CryptoKey,
@@ -4008,7 +4070,6 @@ async function prepareDecapsG(
4008
4070
  kem.name,
4009
4071
  ),
4010
4072
  )
4011
- checkNotAllZeros(ss_T)
4012
4073
 
4013
4074
  return [ss_PQ, ss_T]
4014
4075
  }
@@ -4110,7 +4171,8 @@ function COMPOSITE_HYBRID_KEM_SHARED(): KEM_BASE<HybridKey> {
4110
4171
  return key.getSeed(priv)
4111
4172
  },
4112
4173
  async DeserializePrivateKey(this: CompositeHybridKEM, key, extractable) {
4113
- const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, key)
4174
+ const seed = slice(key)
4175
+ const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, seed)
4114
4176
  const publicKey = new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
4115
4177
  const privateKey = new HybridKey(
4116
4178
  priv,
@@ -4119,7 +4181,7 @@ function COMPOSITE_HYBRID_KEM_SHARED(): KEM_BASE<HybridKey> {
4119
4181
  extractable,
4120
4182
  dk_PQ,
4121
4183
  dk_T,
4122
- slice(key),
4184
+ seed,
4123
4185
  publicKey,
4124
4186
  )
4125
4187
 
@@ -4200,7 +4262,7 @@ function createCompositeHybridKEM(parameters: CompositeHybridKEMParams): Composi
4200
4262
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
4201
4263
  *
4202
4264
  * @group KEM Algorithms
4203
- * @see [HPKE-PQ Hybrid KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-4)
4265
+ * @see [HPKE-PQ Hybrid KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05#section-4)
4204
4266
  */
4205
4267
  export const KEM_MLKEM768_X25519: KEMFactory = function (): CompositeHybridKEM | DirectKEM {
4206
4268
  const id = 0x647a
@@ -4260,7 +4322,7 @@ export const KEM_MLKEM768_X25519: KEMFactory = function (): CompositeHybridKEM |
4260
4322
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
4261
4323
  *
4262
4324
  * @group KEM Algorithms
4263
- * @see [HPKE-PQ Hybrid KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-4)
4325
+ * @see [HPKE-PQ Hybrid KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05#section-4)
4264
4326
  */
4265
4327
  export const KEM_MLKEM768_P256: KEMFactory = function (): CompositeHybridKEM | DirectKEM {
4266
4328
  const id = 0x0050
@@ -4319,7 +4381,7 @@ export const KEM_MLKEM768_P256: KEMFactory = function (): CompositeHybridKEM | D
4319
4381
  * > [`@panva/hpke-noble`](https://www.npmjs.com/package/@panva/hpke-noble)
4320
4382
  *
4321
4383
  * @group KEM Algorithms
4322
- * @see [HPKE-PQ Hybrid KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05.html#section-4)
4384
+ * @see [HPKE-PQ Hybrid KEM Identifiers](https://datatracker.ietf.org/doc/html/draft-ietf-hpke-pq-05#section-4)
4323
4385
  */
4324
4386
  export const KEM_MLKEM1024_P384: KEMFactory = function (): CompositeHybridKEM | DirectKEM {
4325
4387
  const id = 0x0051