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.js CHANGED
@@ -38,15 +38,15 @@
38
38
 
39
39
 
40
40
 
41
+
42
+
43
+
41
44
 
42
-
43
-
44
-
45
-
45
+
46
46
  function ComputeNonce(base_nonce , seq , Nn ) {
47
-
48
-
49
-
47
+
48
+
49
+
50
50
  const nonce = new Uint8Array(Nn)
51
51
  nonce.set(base_nonce)
52
52
  let s = seq
@@ -58,12 +58,12 @@ function ComputeNonce(base_nonce , seq , Nn )
58
58
  }
59
59
 
60
60
  function MaxSeq(Nn ) {
61
-
62
-
61
+
62
+
63
63
  return Math.min(2 ** (8 * Nn) - 1, Number.MAX_SAFE_INTEGER)
64
64
  }
65
65
 
66
-
66
+
67
67
  function IncrementSeq(seq , maxSeq ) {
68
68
  if (seq >= maxSeq) {
69
69
  throw new MessageLimitReachedError('Sequence number overflow')
@@ -71,7 +71,31 @@ function IncrementSeq(seq , maxSeq ) {
71
71
  return ++seq
72
72
  }
73
73
 
74
+
75
+ async function Export_OneStage(
76
+ KDF ,
77
+ suite_id ,
78
+ exporter_secret ,
79
+ exporter_context ,
80
+ L ,
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
+
87
+ async function Export_TwoStage(
88
+ KDF ,
89
+ suite_id ,
90
+ exporter_secret ,
91
+ exporter_context ,
92
+ L ,
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
+ }
74
97
 
98
+
75
99
  async function ContextExport(
76
100
  suite ,
77
101
  exporterSecret ,
@@ -105,6 +129,36 @@ class Mutex {
105
129
  }
106
130
  }
107
131
 
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+ function createContextState(
144
+ suite ,
145
+ mode ,
146
+ key ,
147
+ base_nonce ,
148
+ exporter_secret ,
149
+ ) {
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
 
110
164
 
@@ -125,14 +179,7 @@ class Mutex {
125
179
 
126
180
 
127
181
  class SenderContext {
128
- #suite
129
- #key
130
- #base_nonce
131
- #exporter_secret
132
- #mode
133
- #seq = 0
134
- #max_seq
135
- #mutex
182
+ #state
136
183
 
137
184
  constructor(
138
185
  suite ,
@@ -141,34 +188,29 @@ class SenderContext {
141
188
  base_nonce ,
142
189
  exporter_secret ,
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
+
153
195
 
154
196
 
155
197
 
156
198
 
157
199
  get mode() {
158
- return this.#mode
200
+ return this.#state.mode
159
201
  }
160
202
 
161
-
203
+
162
204
 
163
205
 
164
206
 
165
207
 
166
208
 
167
209
  get seq() {
168
- return this.#seq
210
+ return this.#state.seq
169
211
  }
170
212
 
171
-
213
+
172
214
 
173
215
 
174
216
 
@@ -198,27 +240,33 @@ class SenderContext {
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
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()
219
262
  }
220
263
  }
221
264
 
265
+
266
+
267
+
268
+
269
+
222
270
 
223
271
 
224
272
 
@@ -243,18 +291,23 @@ class SenderContext {
243
291
 
244
292
 
245
293
  async Export(exporterContext , length ) {
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
+
250
303
 
251
304
 
252
305
 
253
306
  get Nt() {
254
- return this.#suite.AEAD.Nt
307
+ return this.#state.suite.AEAD.Nt
255
308
  }
256
309
  }
257
-
310
+
258
311
 
259
312
 
260
313
 
@@ -276,14 +329,7 @@ class SenderContext {
276
329
 
277
330
 
278
331
  class RecipientContext {
279
- #suite
280
- #key
281
- #base_nonce
282
- #exporter_secret
283
- #mode
284
- #seq = 0
285
- #max_seq
286
- #mutex
332
+ #state
287
333
 
288
334
  constructor(
289
335
  suite ,
@@ -292,34 +338,29 @@ class RecipientContext {
292
338
  base_nonce ,
293
339
  exporter_secret ,
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
+
304
345
 
305
346
 
306
347
 
307
348
 
308
349
  get mode() {
309
- return this.#mode
350
+ return this.#state.mode
310
351
  }
311
352
 
312
-
353
+
313
354
 
314
355
 
315
356
 
316
357
 
317
358
 
318
359
  get seq() {
319
- return this.#seq
360
+ return this.#state.seq
320
361
  }
321
362
 
322
-
363
+
323
364
 
324
365
 
325
366
 
@@ -351,18 +392,19 @@ class RecipientContext {
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
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,13 +415,14 @@ 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()
380
422
  }
381
423
  }
382
424
 
425
+
383
426
 
384
427
 
385
428
 
@@ -403,15 +446,24 @@ class RecipientContext {
403
446
 
404
447
 
405
448
 
406
- async Export(exporterContext , length ) {
407
- return await ContextExport(this.#suite, this.#exporter_secret, exporterContext, length)
408
- }
409
- }
410
449
 
411
450
 
412
451
 
413
452
 
453
+ async Export(exporterContext , length ) {
454
+ return await ContextExport(
455
+ this.#state.suite,
456
+ this.#state.exporter_secret,
457
+ exporterContext,
458
+ length,
459
+ )
460
+ }
461
+ }
462
+
414
463
 
464
+
465
+
466
+
415
467
 
416
468
  const validate = (factory , type ) => {
417
469
  try {
@@ -452,7 +504,7 @@ const validate = (factory , type )
452
504
  export class CipherSuite {
453
505
  #suite
454
506
 
455
-
507
+
456
508
 
457
509
 
458
510
 
@@ -526,24 +578,24 @@ export class CipherSuite {
526
578
  }
527
579
  }
528
580
 
529
-
581
+
530
582
 
531
583
 
532
584
 
533
585
 
534
586
  get KEM()
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
587
+
588
+
589
+
590
+
591
+
592
+
593
+
594
+
595
+
596
+
597
+
598
+
547
599
  {
548
600
  return {
549
601
  id: this.#suite.KEM.id,
@@ -555,28 +607,28 @@ export class CipherSuite {
555
607
  }
556
608
  }
557
609
 
558
-
610
+
559
611
 
560
612
 
561
613
 
562
614
 
563
615
  get KDF()
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
616
+
617
+
618
+
619
+
620
+
621
+
622
+
623
+
624
+
625
+
626
+
627
+
628
+
629
+
630
+
631
+
580
632
  {
581
633
  return {
582
634
  id: this.#suite.KDF.id,
@@ -586,23 +638,23 @@ export class CipherSuite {
586
638
  }
587
639
  }
588
640
 
589
-
641
+
590
642
 
591
643
 
592
644
 
593
645
 
594
646
 
595
647
  get AEAD()
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
648
+
649
+
650
+
651
+
652
+
653
+
654
+
655
+
656
+
657
+
606
658
  {
607
659
  return {
608
660
  id: this.#suite.AEAD.id,
@@ -613,7 +665,7 @@ export class CipherSuite {
613
665
  }
614
666
  }
615
667
 
616
-
668
+
617
669
 
618
670
 
619
671
 
@@ -636,7 +688,7 @@ export class CipherSuite {
636
688
  return await this.#suite.KEM.GenerateKeyPair(extractable)
637
689
  }
638
690
 
639
-
691
+
640
692
 
641
693
 
642
694
 
@@ -680,7 +732,7 @@ export class CipherSuite {
680
732
  }
681
733
  }
682
734
 
683
-
735
+
684
736
 
685
737
 
686
738
 
@@ -702,7 +754,7 @@ export class CipherSuite {
702
754
  return await this.#suite.KEM.SerializePrivateKey(privateKey)
703
755
  }
704
756
 
705
-
757
+
706
758
 
707
759
 
708
760
 
@@ -724,7 +776,7 @@ export class CipherSuite {
724
776
  return await this.#suite.KEM.SerializePublicKey(publicKey)
725
777
  }
726
778
 
727
-
779
+
728
780
 
729
781
 
730
782
 
@@ -761,7 +813,7 @@ export class CipherSuite {
761
813
  }
762
814
  }
763
815
 
764
-
816
+
765
817
 
766
818
 
767
819
 
@@ -794,6 +846,10 @@ export class CipherSuite {
794
846
  }
795
847
  }
796
848
 
849
+
850
+
851
+
852
+
797
853
 
798
854
 
799
855
 
@@ -841,6 +897,10 @@ export class CipherSuite {
841
897
  return { encapsulatedSecret, ciphertext }
842
898
  }
843
899
 
900
+
901
+
902
+
903
+
844
904
 
845
905
 
846
906
 
@@ -890,6 +950,15 @@ export class CipherSuite {
890
950
  return await ctx.Open(ciphertext, options?.aad)
891
951
  }
892
952
 
953
+
954
+
955
+
956
+
957
+
958
+
959
+
960
+
961
+
893
962
 
894
963
 
895
964
 
@@ -936,6 +1005,15 @@ export class CipherSuite {
936
1005
  return { encapsulatedSecret, exportedSecret }
937
1006
  }
938
1007
 
1008
+
1009
+
1010
+
1011
+
1012
+
1013
+
1014
+
1015
+
1016
+
939
1017
 
940
1018
 
941
1019
 
@@ -984,7 +1062,7 @@ export class CipherSuite {
984
1062
  return await ctx.Export(exporterContext, length)
985
1063
  }
986
1064
 
987
-
1065
+
988
1066
 
989
1067
 
990
1068
 
@@ -1060,7 +1138,7 @@ export class CipherSuite {
1060
1138
  return { encapsulatedSecret: enc, ctx }
1061
1139
  }
1062
1140
 
1063
-
1141
+
1064
1142
 
1065
1143
 
1066
1144
 
@@ -1152,9 +1230,9 @@ export class CipherSuite {
1152
1230
  }
1153
1231
  }
1154
1232
 
1155
-
1156
-
1157
-
1233
+
1234
+
1235
+
1158
1236
 
1159
1237
 
1160
1238
 
@@ -1166,7 +1244,7 @@ export class ValidationError extends Error {
1166
1244
  constructor(message , options ) {
1167
1245
  super(message, options)
1168
1246
  this.name = 'ValidationError'
1169
-
1247
+
1170
1248
  Error.captureStackTrace?.(this, ValidationError)
1171
1249
  }
1172
1250
  }
@@ -1181,7 +1259,7 @@ export class DeserializeError extends Error {
1181
1259
  constructor(message , options ) {
1182
1260
  super(message, options)
1183
1261
  this.name = 'DeserializeError'
1184
-
1262
+
1185
1263
  Error.captureStackTrace?.(this, DeserializeError)
1186
1264
  }
1187
1265
  }
@@ -1196,7 +1274,7 @@ export class EncapError extends Error {
1196
1274
  constructor(message , options ) {
1197
1275
  super(message, options)
1198
1276
  this.name = 'EncapError'
1199
-
1277
+
1200
1278
  Error.captureStackTrace?.(this, EncapError)
1201
1279
  }
1202
1280
  }
@@ -1211,7 +1289,7 @@ export class DecapError extends Error {
1211
1289
  constructor(message , options ) {
1212
1290
  super(message, options)
1213
1291
  this.name = 'DecapError'
1214
-
1292
+
1215
1293
  Error.captureStackTrace?.(this, DecapError)
1216
1294
  }
1217
1295
  }
@@ -1226,7 +1304,7 @@ export class OpenError extends Error {
1226
1304
  constructor(message , options ) {
1227
1305
  super(message, options)
1228
1306
  this.name = 'OpenError'
1229
-
1307
+
1230
1308
  Error.captureStackTrace?.(this, OpenError)
1231
1309
  }
1232
1310
  }
@@ -1241,7 +1319,7 @@ export class MessageLimitReachedError extends Error {
1241
1319
  constructor(message , options ) {
1242
1320
  super(message, options)
1243
1321
  this.name = 'MessageLimitReachedError'
1244
-
1322
+
1245
1323
  Error.captureStackTrace?.(this, MessageLimitReachedError)
1246
1324
  }
1247
1325
  }
@@ -1256,7 +1334,7 @@ export class DeriveKeyPairError extends Error {
1256
1334
  constructor(message , options ) {
1257
1335
  super(message, options)
1258
1336
  this.name = 'DeriveKeyPairError'
1259
-
1337
+
1260
1338
  Error.captureStackTrace?.(this, DeriveKeyPairError)
1261
1339
  }
1262
1340
  }
@@ -1271,21 +1349,21 @@ export class NotSupportedError extends Error {
1271
1349
  constructor(message , options ) {
1272
1350
  super(message, options)
1273
1351
  this.name = 'NotSupportedError'
1274
-
1352
+
1275
1353
  Error.captureStackTrace?.(this, NotSupportedError)
1276
1354
  }
1277
1355
  }
1278
1356
 
1357
+
1358
+
1359
+
1279
1360
 
1280
-
1281
-
1282
-
1283
-
1284
-
1285
-
1286
-
1287
-
1288
-
1361
+
1362
+
1363
+
1364
+
1365
+
1366
+
1289
1367
 
1290
1368
 
1291
1369
 
@@ -1308,6 +1386,8 @@ export const MODE_BASE = 0x00
1308
1386
 
1309
1387
  export const MODE_PSK = 0x01
1310
1388
 
1389
+
1390
+
1311
1391
 
1312
1392
 
1313
1393
 
@@ -1343,6 +1423,7 @@ export const MODE_PSK = 0x01
1343
1423
 
1344
1424
 
1345
1425
 
1426
+
1346
1427
 
1347
1428
 
1348
1429
 
@@ -1364,6 +1445,7 @@ export const MODE_PSK = 0x01
1364
1445
 
1365
1446
 
1366
1447
 
1448
+
1367
1449
 
1368
1450
 
1369
1451
 
@@ -1382,6 +1464,7 @@ export const MODE_PSK = 0x01
1382
1464
 
1383
1465
 
1384
1466
 
1467
+
1385
1468
 
1386
1469
 
1387
1470
 
@@ -1400,6 +1483,12 @@ export const MODE_PSK = 0x01
1400
1483
 
1401
1484
 
1402
1485
 
1486
+
1487
+
1488
+
1489
+
1490
+
1491
+
1403
1492
 
1404
1493
 
1405
1494
 
@@ -1411,8 +1500,19 @@ export const MODE_PSK = 0x01
1411
1500
 
1412
1501
 
1413
1502
 
1503
+
1504
+
1505
+
1506
+
1507
+
1508
+
1414
1509
 
1510
+
1511
+
1415
1512
 
1513
+
1514
+
1515
+
1416
1516
 
1417
1517
 
1418
1518
 
@@ -1421,6 +1521,11 @@ export const MODE_PSK = 0x01
1421
1521
 
1422
1522
 
1423
1523
 
1524
+
1525
+
1526
+
1527
+
1528
+
1424
1529
 
1425
1530
 
1426
1531
 
@@ -1430,43 +1535,16 @@ export const MODE_PSK = 0x01
1430
1535
 
1431
1536
 
1432
1537
 
1538
+
1539
+
1540
+
1541
+
1542
+
1543
+
1433
1544
 
1434
-
1435
-
1436
-
1437
-
1438
-
1439
-
1440
-
1441
-
1442
-
1443
-
1444
-
1445
-
1446
-
1447
-
1448
-
1449
-
1450
-
1451
-
1452
-
1453
-
1454
-
1455
-
1456
-
1457
-
1458
-
1459
-
1460
-
1461
-
1462
-
1463
-
1464
-
1465
-
1466
-
1467
-
1468
-
1469
-
1545
+
1546
+
1547
+
1470
1548
 
1471
1549
 
1472
1550
 
@@ -1519,6 +1597,38 @@ export function encode(string ) {
1519
1597
 
1520
1598
 
1521
1599
 
1600
+
1601
+
1602
+
1603
+
1604
+
1605
+
1606
+
1607
+
1608
+ export function I2OSP(n , w ) {
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
+
1628
+
1629
+
1630
+
1631
+
1522
1632
  const L_HPKE_v1 = /* @__PURE__ */ encode('HPKE-v1')
1523
1633
  const L_HPKE = /* @__PURE__ */ encode('HPKE')
1524
1634
  const L_KEM = /* @__PURE__ */ encode('KEM')
@@ -1541,188 +1651,38 @@ function lengthPrefixed(x ) {
1541
1651
  return concat(I2OSP(x.byteLength, 2), x)
1542
1652
  }
1543
1653
 
1654
+
1655
+
1656
+
1544
1657
 
1658
+
1659
+ const MAX_LENGTH_TWO_STAGE = 0xffff
1660
+
1661
+
1662
+ const MAX_LENGTH_ONE_STAGE = 0xffff
1545
1663
 
1664
+ function checkLength(data , name , maxLength ) {
1665
+ if (data.byteLength > maxLength) {
1666
+ throw new TypeError(`${name} length must not exceed ${maxLength} bytes`)
1667
+ }
1668
+ }
1669
+ function checkUint8Array(input , name ) {
1670
+ if (!(input instanceof Uint8Array)) {
1671
+ throw new TypeError(`"${name}" must be Uint8Array`)
1672
+ }
1673
+ if (typeof SharedArrayBuffer !== 'undefined' && input.buffer instanceof SharedArrayBuffer) {
1674
+ throw new TypeError(`"${name}" must not be backed by a SharedArrayBuffer`)
1675
+ }
1676
+ }
1677
+ function checkExtractable(extractable ) {
1678
+ if (typeof extractable !== 'boolean') {
1679
+ throw new TypeError('"extractable" must be boolean')
1680
+ }
1681
+ }
1546
1682
 
1547
-
1548
-
1549
-
1550
-
1551
-
1552
-
1553
-
1554
-
1555
-
1556
-
1557
-
1558
-
1559
-
1560
-
1561
-
1562
-
1563
-
1564
-
1565
-
1566
-
1567
- export async function LabeledDerive(
1568
- KDF ,
1569
- suite_id ,
1570
- ikm ,
1571
- label ,
1572
- context ,
1573
- L ,
1574
- ) {
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
-
1580
- async function Export_OneStage(
1581
- KDF ,
1582
- suite_id ,
1583
- exporter_secret ,
1584
- exporter_context ,
1585
- L ,
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
-
1592
- async function CombineSecrets_OneStage(
1593
- suite ,
1594
- mode ,
1595
- shared_secret ,
1596
- info ,
1597
- psk ,
1598
- psk_id ,
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
-
1624
-
1625
- const MAX_LENGTH_TWO_STAGE = 0xffff
1626
-
1627
- const MAX_LENGTH_ONE_STAGE = 0xffff
1628
-
1629
- function checkLength(data , name , maxLength ) {
1630
- if (data.byteLength > maxLength) {
1631
- throw new TypeError(`${name} length must not exceed ${maxLength} bytes`)
1632
- }
1633
- }
1634
- function checkUint8Array(input , name ) {
1635
- if (!(input instanceof Uint8Array)) {
1636
- throw new TypeError(`"${name}" must be Uint8Array`)
1637
- }
1638
- if (typeof SharedArrayBuffer !== 'undefined' && input.buffer instanceof SharedArrayBuffer) {
1639
- throw new TypeError(`"${name}" must not be backed by a SharedArrayBuffer`)
1640
- }
1641
- }
1642
- function checkExtractable(extractable ) {
1643
- if (typeof extractable !== 'boolean') {
1644
- throw new TypeError('"extractable" must be boolean')
1645
- }
1646
- }
1647
-
1648
-
1649
- async function CombineSecrets_TwoStage(
1650
- suite ,
1651
- mode ,
1652
- shared_secret ,
1653
- info ,
1654
- psk ,
1655
- psk_id ,
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
-
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
-
1692
- async function Export_TwoStage(
1693
- KDF ,
1694
- suite_id ,
1695
- exporter_secret ,
1696
- exporter_context ,
1697
- L ,
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
- }
1702
-
1703
-
1704
-
1705
-
1706
-
1707
-
1708
-
1709
-
1710
-
1711
-
1712
-
1713
-
1714
-
1715
-
1716
-
1717
-
1718
-
1719
-
1720
-
1721
-
1722
-
1723
-
1724
-
1725
-
1683
+
1684
+
1685
+
1726
1686
 
1727
1687
 
1728
1688
 
@@ -1794,14 +1754,61 @@ async function Export_TwoStage(
1794
1754
 
1795
1755
 
1796
1756
 
1757
+
1758
+
1759
+
1797
1760
 
1761
+
1762
+
1798
1763
 
1764
+
1765
+
1799
1766
 
1767
+
1768
+
1769
+
1770
+
1771
+
1772
+
1800
1773
 
1774
+
1775
+
1801
1776
 
1777
+
1778
+
1779
+
1780
+
1781
+
1782
+
1783
+
1784
+
1785
+
1802
1786
 
1787
+
1788
+
1789
+
1790
+
1791
+
1792
+
1793
+
1794
+
1795
+
1796
+
1803
1797
 
1798
+
1799
+
1800
+
1801
+
1802
+
1803
+
1804
+
1805
+
1806
+
1807
+
1804
1808
 
1809
+
1810
+
1811
+
1805
1812
 
1806
1813
 
1807
1814
 
@@ -1822,6 +1829,17 @@ async function Export_TwoStage(
1822
1829
 
1823
1830
 
1824
1831
 
1832
+ export async function LabeledDerive(
1833
+ KDF ,
1834
+ suite_id ,
1835
+ ikm ,
1836
+ label ,
1837
+ context ,
1838
+ L ,
1839
+ ) {
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
+ }
1825
1843
 
1826
1844
 
1827
1845
 
@@ -1882,6 +1900,9 @@ export async function LabeledExpand(
1882
1900
  return await KDF.Expand(prk, labeled_info, L)
1883
1901
  }
1884
1902
 
1903
+
1904
+
1905
+
1885
1906
 
1886
1907
 
1887
1908
 
@@ -1977,115 +1998,112 @@ export async function LabeledExpand(
1977
1998
 
1978
1999
 
1979
2000
 
2001
+
2002
+
2003
+
1980
2004
 
2005
+
2006
+
1981
2007
 
2008
+
2009
+
1982
2010
 
2011
+
2012
+
1983
2013
 
2014
+
2015
+
1984
2016
 
2017
+
2018
+
1985
2019
 
2020
+
2021
+
1986
2022
 
2023
+
2024
+
2025
+
2026
+
2027
+
2028
+
2029
+
2030
+
2031
+
1987
2032
 
2033
+
2034
+
2035
+
2036
+
2037
+
2038
+
2039
+
2040
+
1988
2041
 
2042
+
2043
+
2044
+
2045
+
2046
+
2047
+
2048
+
2049
+
1989
2050
 
2051
+
2052
+
2053
+
2054
+
2055
+
2056
+
2057
+
2058
+
1990
2059
 
2060
+
2061
+
2062
+
2063
+
2064
+
2065
+
2066
+
2067
+
1991
2068
 
2069
+
2070
+
2071
+
2072
+
2073
+
2074
+
2075
+
2076
+
2077
+
1992
2078
 
1993
-
1994
-
1995
-
1996
-
1997
-
1998
-
1999
-
2000
-
2001
-
2002
-
2003
-
2004
-
2005
-
2006
-
2007
-
2008
-
2009
-
2010
-
2011
-
2012
-
2013
-
2014
-
2015
-
2016
-
2017
-
2018
-
2019
-
2020
-
2021
-
2022
-
2023
-
2024
-
2025
-
2026
-
2027
-
2028
-
2029
-
2030
-
2031
-
2032
-
2033
-
2034
-
2035
-
2036
-
2037
-
2038
-
2039
-
2040
-
2041
-
2042
-
2043
-
2044
-
2045
-
2046
-
2047
-
2048
-
2049
-
2050
-
2051
-
2052
-
2053
-
2054
-
2055
-
2056
-
2057
-
2058
-
2059
-
2060
-
2061
-
2062
-
2063
-
2064
-
2065
-
2066
-
2067
-
2068
-
2069
-
2070
-
2071
-
2072
-
2073
-
2074
-
2075
-
2076
-
2077
-
2078
-
2079
-
2080
-
2081
-
2082
-
2083
-
2084
-
2085
-
2086
-
2087
-
2088
-
2079
+
2080
+
2081
+
2082
+
2083
+
2084
+
2085
+
2086
+
2087
+
2088
+
2089
+
2090
+
2091
+
2092
+
2093
+
2094
+
2095
+
2096
+
2097
+
2098
+
2099
+
2100
+
2101
+
2102
+
2103
+
2104
+
2105
+
2106
+
2089
2107
 
2090
2108
  function isKeyPair (skR ) {
2091
2109
  if (!skR || typeof skR !== 'object') return false
@@ -2123,6 +2141,9 @@ function isKey(key , type , extractable )
2123
2141
  }
2124
2142
  }
2125
2143
 
2144
+
2145
+
2146
+
2126
2147
 
2127
2148
 
2128
2149
 
@@ -2179,104 +2200,144 @@ function isKey(key , type , extractable )
2179
2200
 
2180
2201
 
2181
2202
 
2203
+
2204
+
2205
+
2182
2206
 
2207
+
2208
+
2183
2209
 
2210
+
2211
+
2184
2212
 
2213
+
2214
+
2185
2215
 
2216
+
2217
+
2186
2218
 
2219
+
2220
+
2187
2221
 
2222
+
2223
+
2224
+
2225
+
2226
+
2227
+
2228
+
2229
+
2230
+
2231
+
2232
+
2233
+
2234
+
2235
+
2236
+
2237
+
2238
+
2188
2239
 
2240
+
2241
+
2242
+
2243
+
2244
+
2245
+
2246
+
2247
+
2248
+
2249
+
2250
+
2251
+
2189
2252
 
2253
+
2254
+
2255
+
2190
2256
 
2257
+ function KDFStages(KDF ) {
2258
+ if (KDF.stages === 1 || KDF.stages === 2) {
2259
+ return KDF.stages
2260
+ }
2261
+
2262
+ throw new Error('unreachable')
2263
+ }
2191
2264
 
2265
+
2266
+ async function CombineSecrets_OneStage(
2267
+ suite ,
2268
+ mode ,
2269
+ shared_secret ,
2270
+ info ,
2271
+ psk ,
2272
+ psk_id ,
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)
2192
2277
 
2278
+ const secrets = concat(lengthPrefixed(psk), lengthPrefixed(shared_secret))
2279
+ const context = concat(I2OSP(mode, 1), lengthPrefixed(psk_id), lengthPrefixed(info))
2193
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
+ )
2194
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)
2195
2293
 
2294
+ return { key, base_nonce, exporter_secret }
2295
+ }
2196
2296
 
2297
+
2298
+ async function CombineSecrets_TwoStage(
2299
+ suite ,
2300
+ mode ,
2301
+ shared_secret ,
2302
+ info ,
2303
+ psk ,
2304
+ psk_id ,
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)
2197
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
+ ])
2198
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)
2199
2317
 
2200
-
2201
-
2202
-
2203
-
2204
-
2205
-
2206
-
2207
-
2208
-
2209
-
2210
-
2211
-
2212
-
2213
-
2214
-
2215
-
2216
-
2217
-
2218
-
2219
-
2220
-
2221
-
2222
-
2223
-
2224
-
2225
-
2226
-
2227
-
2228
-
2229
-
2230
-
2231
-
2232
-
2233
-
2234
-
2235
-
2236
-
2237
-
2238
-
2239
-
2240
-
2241
-
2242
-
2243
-
2244
-
2245
-
2246
-
2247
-
2248
-
2249
-
2250
-
2251
- export function I2OSP(n , w ) {
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)
2318
+
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 }
2267
2329
  }
2268
- return ret
2269
- }
2270
2330
 
2271
- function KDFStages(KDF ) {
2272
- if (KDF.stages === 1 || KDF.stages === 2) {
2273
- return KDF.stages
2274
- }
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
+ ])
2275
2336
 
2276
- throw new Error('unreachable')
2337
+ return { key, base_nonce, exporter_secret }
2277
2338
  }
2278
2339
 
2279
-
2340
+
2280
2341
  async function KeySchedule(
2281
2342
  suite ,
2282
2343
  mode ,
@@ -2299,7 +2360,7 @@ async function KeySchedule(
2299
2360
  return await CombineSecrets(suite, mode, shared_secret, info, psk, pskId)
2300
2361
  }
2301
2362
 
2302
-
2363
+
2303
2364
  function VerifyPSKInputs(psk , psk_id ) {
2304
2365
  if (psk.byteLength && psk_id.byteLength) {
2305
2366
  if (psk.byteLength < 32) {
@@ -2313,7 +2374,7 @@ function VerifyPSKInputs(psk , psk_id ) {
2313
2374
  throw new TypeError('Inconsistent PSK inputs')
2314
2375
  }
2315
2376
 
2316
-
2377
+
2317
2378
  const NotApplicable = () => {
2318
2379
  throw new Error('unreachable')
2319
2380
  }
@@ -2346,9 +2407,9 @@ export const AEAD_EXPORT_ONLY = function () {
2346
2407
  }
2347
2408
  }
2348
2409
 
2349
-
2350
-
2351
-
2410
+
2411
+
2412
+
2352
2413
 
2353
2414
  async function subtle (promise , name ) {
2354
2415
  try {
@@ -2364,20 +2425,40 @@ async function subtle (promise , name
2364
2425
  }
2365
2426
  }
2366
2427
 
2428
+
2429
+
2430
+
2367
2431
 
2432
+
2368
2433
 
2434
+
2435
+
2436
+
2437
+
2369
2438
 
2439
+ async function getCachedCryptoKey(
2440
+ cache ,
2441
+ keyBytes ,
2442
+ init ,
2443
+ ) {
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
+ }
2370
2452
 
2371
-
2372
-
2373
- async function cacheValue (
2374
- cache ,
2375
- key ,
2376
- init ,
2377
- ) {
2378
- const result = await init()
2379
- cache.set(key, result)
2380
- return result
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() {
@@ -2388,12 +2469,7 @@ function HKDF_SHARED() {
2388
2469
  this.name,
2389
2470
  )
2390
2471
  }
2391
- const cache = new WeakMap ()
2392
- function importPrk( prk ) {
2393
- const key = importKey.call(this, prk )
2394
- cache.set(prk, key)
2395
- return key
2396
- }
2472
+ const cache = new WeakMap ()
2397
2473
  return {
2398
2474
  stages: 2,
2399
2475
  Derive: NotApplicable,
@@ -2414,7 +2490,9 @@ function HKDF_SHARED() {
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 ),
2495
+ )
2418
2496
 
2419
2497
  const T = new Uint8Array(N * this.Nh)
2420
2498
  let T_prev = new Uint8Array()
@@ -2436,9 +2514,9 @@ function HKDF_SHARED() {
2436
2514
  }
2437
2515
  }
2438
2516
 
2439
-
2440
-
2441
-
2517
+
2518
+
2519
+
2442
2520
 
2443
2521
 
2444
2522
 
@@ -2509,13 +2587,13 @@ export const KDF_HKDF_SHA512 = function () {
2509
2587
  return { id: 0x0003, type: 'KDF', name: 'HKDF-SHA512', Nh: 64, hash: 'SHA-512', ...HKDF_SHARED() }
2510
2588
  }
2511
2589
 
2590
+
2591
+
2592
+
2512
2593
 
2513
-
2514
-
2515
-
2516
-
2517
-
2518
-
2594
+
2595
+
2596
+
2519
2597
 
2520
2598
  async function ShakeDerive(name , variant , ikm , L ) {
2521
2599
  const bits = L << 3
@@ -2679,41 +2757,30 @@ async function getPublicKeyByExport(
2679
2757
 
2680
2758
  async function getPublicKey(name , key , usages ) {
2681
2759
  return (
2682
-
2760
+
2683
2761
  ((await subtle((c) => c.getPublicKey?.(key, usages), name)) ) ||
2684
2762
  (await getPublicKeyByExport(name, key, usages))
2685
2763
  )
2686
2764
  }
2687
2765
 
2688
-
2689
- function checkNotAllZeros(buffer ) {
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
-
2700
-
2701
-
2702
-
2703
-
2704
-
2705
-
2706
-
2707
-
2708
-
2709
-
2710
-
2711
-
2712
-
2713
-
2714
-
2715
-
2716
-
2766
+
2767
+
2768
+
2769
+
2770
+
2771
+
2772
+
2773
+
2774
+
2775
+
2776
+
2777
+
2778
+
2779
+
2780
+
2781
+
2782
+
2783
+
2717
2784
 
2718
2785
  function fromBase64(input ) {
2719
2786
  input = input.replace(/-/g, '+').replace(/_/g, '/')
@@ -2734,20 +2801,20 @@ function toBase64Url(bytes ) {
2734
2801
  }
2735
2802
 
2736
2803
  function toB64u(input ) {
2737
-
2804
+
2738
2805
  return input.toBase64?.({ alphabet: 'base64url', omitPadding: true }) || toBase64Url(input)
2739
2806
  }
2740
2807
 
2741
2808
  function b64u(input ) {
2742
-
2809
+
2743
2810
  return Uint8Array.fromBase64?.(input, { alphabet: 'base64url' }) || fromBase64(input)
2744
2811
  }
2745
2812
 
2813
+
2814
+
2815
+
2746
2816
 
2747
-
2748
-
2749
-
2750
-
2817
+
2751
2818
  async function DeriveKeyPairBytes(
2752
2819
  DHKEM ,
2753
2820
  ikm ,
@@ -2797,14 +2864,14 @@ function assertKeyAlgorithm(key , expectedAlgorithm ) {
2797
2864
  }
2798
2865
 
2799
2866
  function assertCryptoKey(key ) {
2800
-
2867
+
2801
2868
  if (key[Symbol.toStringTag] !== 'CryptoKey') {
2802
2869
  if (key instanceof CryptoKey) return
2803
2870
  throw new TypeError('unexpected key constructor')
2804
2871
  }
2805
2872
  }
2806
2873
 
2807
-
2874
+
2808
2875
  async function ExtractAndExpand_OneStage(
2809
2876
  DHKEM ,
2810
2877
  dh ,
@@ -2820,7 +2887,7 @@ async function ExtractAndExpand_OneStage(
2820
2887
  )
2821
2888
  }
2822
2889
 
2823
-
2890
+
2824
2891
  async function ExtractAndExpand_TwoStage(
2825
2892
  DHKEM ,
2826
2893
  dh ,
@@ -2837,7 +2904,7 @@ async function ExtractAndExpand_TwoStage(
2837
2904
  )
2838
2905
  }
2839
2906
 
2840
-
2907
+
2841
2908
  async function ExtractAndExpand(
2842
2909
  DHKEM ,
2843
2910
  dh ,
@@ -2847,12 +2914,12 @@ async function ExtractAndExpand(
2847
2914
  return await Fn(DHKEM, dh, kem_context)
2848
2915
  }
2849
2916
 
2850
-
2851
-
2852
-
2917
+
2918
+
2919
+
2853
2920
 
2854
2921
  function DHKEM_SHARED()
2855
-
2922
+
2856
2923
  {
2857
2924
  return {
2858
2925
  async GenerateKeyPair( extractable) {
@@ -2886,15 +2953,14 @@ function DHKEM_SHARED()
2886
2953
  const skE = ekp.privateKey
2887
2954
  const pkE = ekp.publicKey
2888
2955
 
2889
-
2890
-
2956
+
2957
+
2891
2958
  const dh = new Uint8Array(
2892
2959
  await subtle(
2893
2960
  (c) => c.deriveBits({ name: skE.algorithm.name, public: pkR }, skE, this.Ndh << 3),
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)
@@ -2914,15 +2980,14 @@ function DHKEM_SHARED()
2914
2980
 
2915
2981
  const pkE = (await this.DeserializePublicKey(enc))
2916
2982
 
2917
-
2918
-
2983
+
2984
+
2919
2985
  const dh = new Uint8Array(
2920
2986
  await subtle(
2921
2987
  (c) => c.deriveBits({ name: skR.algorithm.name, public: pkE }, skR, this.Ndh << 3),
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)
@@ -2932,9 +2997,9 @@ function DHKEM_SHARED()
2932
2997
  }
2933
2998
  }
2934
2999
 
2935
-
2936
-
2937
-
3000
+
3001
+
3002
+
2938
3003
 
2939
3004
  async function createKeyPairFromPrivateKey(
2940
3005
  DHKEM ,
@@ -2943,7 +3008,7 @@ async function createKeyPairFromPrivateKey(
2943
3008
  ) {
2944
3009
  let privateKey
2945
3010
  let publicKey
2946
-
3011
+
2947
3012
  if (!extractable && typeof crypto.subtle.getPublicKey !== 'function') {
2948
3013
  privateKey = (await DHKEM.DeserializePrivateKey(key, true))
2949
3014
  publicKey = await getPublicKey(DHKEM.name, privateKey, [])
@@ -2973,21 +3038,21 @@ async function CurveKeyFromD(
2973
3038
  )
2974
3039
  }
2975
3040
 
2976
-
2977
-
2978
-
2979
-
2980
-
2981
-
2982
-
2983
-
2984
-
2985
-
2986
-
2987
-
2988
-
2989
-
2990
-
3041
+
3042
+
3043
+
3044
+
3045
+
3046
+
3047
+
3048
+
3049
+
3050
+
3051
+
3052
+
3053
+
3054
+
3055
+
2991
3056
 
2992
3057
  async function importKeyNist(
2993
3058
  curve ,
@@ -3001,7 +3066,7 @@ async function importKeyNist(
3001
3066
  throw new Error('Invalid scalar')
3002
3067
  }
3003
3068
 
3004
-
3069
+
3005
3070
  const pkcs8 = concat(curve.pkcs8, key)
3006
3071
  return await subtle(async (c) => {
3007
3072
  async function fromJwk(jwk , privateKey ) {
@@ -3016,7 +3081,7 @@ async function importKeyNist(
3016
3081
  }
3017
3082
 
3018
3083
  let privateKey
3019
-
3084
+
3020
3085
  const nativePublicKey = typeof c.getPublicKey === 'function'
3021
3086
  try {
3022
3087
  privateKey = await c.importKey(
@@ -3027,8 +3092,8 @@ async function importKeyNist(
3027
3092
  ['deriveBits'],
3028
3093
  )
3029
3094
  if (nativePublicKey) {
3030
-
3031
-
3095
+
3096
+
3032
3097
  const recovered = await c.getPublicKey(privateKey, [])
3033
3098
  return { privateKey, publicKey: includePublicKey ? recovered : undefined }
3034
3099
  }
@@ -3038,10 +3103,10 @@ async function importKeyNist(
3038
3103
  extractable ? privateKey : undefined,
3039
3104
  )
3040
3105
  } catch (cause) {
3041
-
3042
-
3043
-
3044
-
3106
+
3107
+
3108
+
3109
+
3045
3110
  if (
3046
3111
  !(cause instanceof DOMException) ||
3047
3112
  (cause.name !== 'DataError' && cause.name !== 'OperationError')
@@ -3063,15 +3128,15 @@ async function importKeyNist(
3063
3128
  : await c.importKey('jwk', jwk, signingAlgorithm, false, ['sign'])
3064
3129
  privateKey ??= await c.importKey('jwk', jwk, curve.algorithm, false, ['deriveBits'])
3065
3130
 
3066
-
3067
-
3131
+
3132
+
3068
3133
  const generatorKey = await c.importKey('jwk', generator, curve.algorithm, true, [])
3069
3134
  const x = new Uint8Array(
3070
3135
  await c.deriveBits({ name: 'ECDH', public: generatorKey }, privateKey, curve.Nsk << 3),
3071
3136
  )
3072
3137
 
3073
-
3074
-
3138
+
3139
+
3075
3140
  const message = L_public_key_recovery
3076
3141
  const signatureAlgorithm = { name: 'ECDSA', hash: 'SHA-256' }
3077
3142
  const signature = await c.sign(signatureAlgorithm, signingKey, message)
@@ -3090,16 +3155,16 @@ async function importKeyNist(
3090
3155
  }
3091
3156
 
3092
3157
  async function DeserializePrivateKeyNist(
3093
-
3158
+
3094
3159
  key ,
3095
3160
  extractable ,
3096
3161
  ) {
3097
3162
  return (await importKeyNist(this, key, this.name, extractable, false)).privateKey
3098
3163
  }
3099
3164
 
3100
-
3165
+
3101
3166
  async function DeriveKeyPairNist(
3102
-
3167
+
3103
3168
  ikm ,
3104
3169
  extractable ,
3105
3170
  ) {
@@ -3128,19 +3193,19 @@ async function GetKeyPairNist(
3128
3193
  return (await importKeyNist(curveConfig, sk, name, extractable, true))
3129
3194
  }
3130
3195
 
3196
+
3197
+
3198
+
3131
3199
 
3132
-
3133
-
3134
-
3135
-
3200
+
3136
3201
  async function DeriveKeyPairX( ikm , extractable ) {
3137
3202
  const sk = await DeriveKeyPairBytes(this, ikm, L_sk, new Uint8Array())
3138
3203
  return await createKeyPairFromPrivateKey(this, sk, extractable)
3139
3204
  }
3140
3205
 
3141
-
3142
-
3143
-
3206
+
3207
+
3208
+
3144
3209
 
3145
3210
  const P256 = {
3146
3211
  algorithm: { name: 'ECDH', namedCurve: 'P-256' },
@@ -3148,8 +3213,8 @@ const P256 = {
3148
3213
  Nsk: 32,
3149
3214
  order: 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n,
3150
3215
  bitmask: 0xff,
3151
- pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x41, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, 0x27, 0x30, 0x25, 0x02, 0x01, 0x01, 0x04, 0x20),
3152
- spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x39, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x22, 0x00),
3216
+ pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x41, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, 0x27, 0x30, 0x25, 0x02, 0x01, 0x01, 0x04, 0x20),
3217
+ spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x39, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x22, 0x00),
3153
3218
  Gx: 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296n,
3154
3219
  Gy: 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5n,
3155
3220
  }
@@ -3179,7 +3244,7 @@ export const KEM_DHKEM_P256_HKDF_SHA256 = function ()
3179
3244
  const id = 0x0010
3180
3245
  const name = 'DHKEM(P-256, HKDF-SHA256)'
3181
3246
  const kdf = KDF_HKDF_SHA256()
3182
-
3247
+
3183
3248
  kdf.name = name
3184
3249
  return {
3185
3250
  id,
@@ -3204,8 +3269,8 @@ const P384 = {
3204
3269
  order:
3205
3270
  0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973n,
3206
3271
  bitmask: 0xff,
3207
- pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x4e, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22, 0x04, 0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30),
3208
- spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x46, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22, 0x03, 0x32, 0x00),
3272
+ pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x4e, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22, 0x04, 0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30),
3273
+ spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x46, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22, 0x03, 0x32, 0x00),
3209
3274
  Gx: 0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7n,
3210
3275
  Gy: 0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5fn,
3211
3276
  }
@@ -3235,7 +3300,7 @@ export const KEM_DHKEM_P384_HKDF_SHA384 = function ()
3235
3300
  const id = 0x0011
3236
3301
  const name = 'DHKEM(P-384, HKDF-SHA384)'
3237
3302
  const kdf = KDF_HKDF_SHA384()
3238
-
3303
+
3239
3304
  kdf.name = name
3240
3305
  return {
3241
3306
  id,
@@ -3260,8 +3325,8 @@ const P521 = {
3260
3325
  order:
3261
3326
  0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409n,
3262
3327
  bitmask: 0x01,
3263
- pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x60, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23, 0x04, 0x49, 0x30, 0x47, 0x02, 0x01, 0x01, 0x04, 0x42),
3264
- spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x58, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23, 0x03, 0x44, 0x00),
3328
+ pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x60, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23, 0x04, 0x49, 0x30, 0x47, 0x02, 0x01, 0x01, 0x04, 0x42),
3329
+ spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x58, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23, 0x03, 0x44, 0x00),
3265
3330
  Gx: 0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66n,
3266
3331
  Gy: 0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650n,
3267
3332
  }
@@ -3291,7 +3356,7 @@ export const KEM_DHKEM_P521_HKDF_SHA512 = function ()
3291
3356
  const id = 0x0012
3292
3357
  const name = 'DHKEM(P-521, HKDF-SHA512)'
3293
3358
  const kdf = KDF_HKDF_SHA512()
3294
-
3359
+
3295
3360
  kdf.name = name
3296
3361
  return {
3297
3362
  id,
@@ -3309,9 +3374,9 @@ export const KEM_DHKEM_P521_HKDF_SHA512 = function ()
3309
3374
  }
3310
3375
  }
3311
3376
 
3312
-
3313
-
3314
-
3377
+
3378
+
3379
+
3315
3380
 
3316
3381
 
3317
3382
 
@@ -3334,12 +3399,12 @@ export const KEM_DHKEM_P521_HKDF_SHA512 = function ()
3334
3399
 
3335
3400
 
3336
3401
  export const KEM_DHKEM_X25519_HKDF_SHA256 = function ()
3337
-
3402
+
3338
3403
  {
3339
3404
  const id = 0x0020
3340
3405
  const name = 'DHKEM(X25519, HKDF-SHA256)'
3341
3406
  const kdf = KDF_HKDF_SHA256()
3342
-
3407
+
3343
3408
  kdf.name = name
3344
3409
  return {
3345
3410
  id,
@@ -3353,7 +3418,7 @@ export const KEM_DHKEM_X25519_HKDF_SHA256 = function ()
3353
3418
  Nsk: 32,
3354
3419
  Ndh: 32,
3355
3420
  algorithm: { name: 'X25519' },
3356
- pkcs8: Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20),
3421
+ pkcs8: Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20),
3357
3422
  DeriveKeyPair: DeriveKeyPairX,
3358
3423
  async DeserializePrivateKey(key, extractable) {
3359
3424
  return await CurveKeyFromD(name, this.Nsk, this.pkcs8, this.algorithm, key, extractable)
@@ -3383,12 +3448,12 @@ export const KEM_DHKEM_X25519_HKDF_SHA256 = function ()
3383
3448
 
3384
3449
 
3385
3450
  export const KEM_DHKEM_X448_HKDF_SHA512 = function ()
3386
-
3451
+
3387
3452
  {
3388
3453
  const id = 0x0021
3389
3454
  const name = 'DHKEM(X448, HKDF-SHA512)'
3390
3455
  const kdf = KDF_HKDF_SHA512()
3391
-
3456
+
3392
3457
  kdf.name = name
3393
3458
  return {
3394
3459
  id,
@@ -3402,7 +3467,7 @@ export const KEM_DHKEM_X448_HKDF_SHA512 = function ()
3402
3467
  Nsk: 56,
3403
3468
  Ndh: 56,
3404
3469
  algorithm: { name: 'X448' },
3405
- pkcs8: Uint8Array.of(0x30, 0x46, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6f, 0x04, 0x3a, 0x04, 0x38),
3470
+ pkcs8: Uint8Array.of(0x30, 0x46, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6f, 0x04, 0x3a, 0x04, 0x38),
3406
3471
  DeriveKeyPair: DeriveKeyPairX,
3407
3472
  async DeserializePrivateKey(key, extractable) {
3408
3473
  return await CurveKeyFromD(name, this.Nsk, this.pkcs8, this.algorithm, key, extractable)
@@ -3411,17 +3476,17 @@ export const KEM_DHKEM_X448_HKDF_SHA512 = function ()
3411
3476
  }
3412
3477
  }
3413
3478
 
3479
+
3480
+
3481
+
3414
3482
 
3483
+
3415
3484
 
3416
-
3417
-
3418
-
3419
-
3420
-
3421
-
3422
-
3423
-
3424
-
3485
+
3486
+
3487
+
3488
+
3489
+
3425
3490
 
3426
3491
  function DIRECT_KEM_SHARED() {
3427
3492
  return {
@@ -3436,14 +3501,14 @@ function DIRECT_KEM_SHARED() {
3436
3501
  )
3437
3502
 
3438
3503
  const privateKey = (await this.DeserializePrivateKey(dk, extractable))
3439
-
3504
+
3440
3505
  const usages = ['encapsulateBits']
3441
3506
  const publicKey = await getPublicKey(this.name, privateKey, usages)
3442
3507
 
3443
3508
  return { privateKey, publicKey }
3444
3509
  },
3445
3510
  async GenerateKeyPair( extractable) {
3446
-
3511
+
3447
3512
  const usages = ['encapsulateBits', 'decapsulateBits']
3448
3513
  return (await subtle(
3449
3514
  (c) => c.generateKey(this.algorithm, extractable, usages),
@@ -3453,14 +3518,14 @@ function DIRECT_KEM_SHARED() {
3453
3518
  async SerializePublicKey( key) {
3454
3519
  assertKeyAlgorithm(key, this.algorithm)
3455
3520
  assertCryptoKey(key)
3456
-
3521
+
3457
3522
  const format = 'raw-public'
3458
3523
  return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
3459
3524
  },
3460
3525
  async DeserializePublicKey( key) {
3461
-
3526
+
3462
3527
  const format = 'raw-public'
3463
-
3528
+
3464
3529
  const usages = ['encapsulateBits']
3465
3530
  return await subtle(
3466
3531
  (c) => c.importKey(format, key , this.algorithm, true, usages),
@@ -3470,14 +3535,14 @@ function DIRECT_KEM_SHARED() {
3470
3535
  async SerializePrivateKey( key) {
3471
3536
  assertKeyAlgorithm(key, this.algorithm)
3472
3537
  assertCryptoKey(key)
3473
-
3538
+
3474
3539
  const format = 'raw-seed'
3475
3540
  return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
3476
3541
  },
3477
3542
  async DeserializePrivateKey( key, extractable) {
3478
-
3543
+
3479
3544
  const format = 'raw-seed'
3480
-
3545
+
3481
3546
  const usages = ['decapsulateBits']
3482
3547
  return await subtle(
3483
3548
  (c) => c.importKey(format, key , this.algorithm, extractable, usages),
@@ -3488,7 +3553,7 @@ function DIRECT_KEM_SHARED() {
3488
3553
  assertKeyAlgorithm(pkR, this.algorithm)
3489
3554
 
3490
3555
  const { sharedKey, ciphertext } = (await subtle(
3491
-
3556
+
3492
3557
  (c) => c.encapsulateBits(this.algorithm, pkR),
3493
3558
  this.name,
3494
3559
  ))
@@ -3499,7 +3564,7 @@ function DIRECT_KEM_SHARED() {
3499
3564
  assertKeyAlgorithm(skR, this.algorithm)
3500
3565
  return new Uint8Array(
3501
3566
  await subtle(
3502
-
3567
+
3503
3568
  (c) => c.decapsulateBits(this.algorithm, skR, enc ),
3504
3569
  this.name,
3505
3570
  ),
@@ -3510,7 +3575,7 @@ function DIRECT_KEM_SHARED() {
3510
3575
 
3511
3576
  function createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk } ) {
3512
3577
  const kdf = KDF_SHAKE256()
3513
-
3578
+
3514
3579
  kdf.name = name
3515
3580
  return {
3516
3581
  id,
@@ -3527,9 +3592,9 @@ function createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk } )
3527
3592
  }
3528
3593
  }
3529
3594
 
3530
-
3531
-
3532
-
3595
+
3596
+
3597
+
3533
3598
 
3534
3599
 
3535
3600
 
@@ -3622,20 +3687,20 @@ export const KEM_ML_KEM_1024 = function () {
3622
3687
  return createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk })
3623
3688
  }
3624
3689
 
3690
+
3691
+
3692
+
3693
+
3625
3694
 
3626
-
3627
-
3628
-
3629
-
3630
-
3695
+
3631
3696
 
3632
3697
  function AEAD_SHARED(P_MAX ) {
3633
-
3634
-
3635
-
3636
-
3637
-
3638
- const cache = new WeakMap ()
3698
+
3699
+
3700
+
3701
+
3702
+
3703
+ const cache = new WeakMap ()
3639
3704
  async function importKey( key ) {
3640
3705
  return await subtle(
3641
3706
  (c) =>
@@ -3651,8 +3716,7 @@ function AEAD_SHARED(P_MAX ) {
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 ) {
3670
3734
  )
3671
3735
  },
3672
3736
  async Open( 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) =>
@@ -3691,9 +3754,9 @@ function AEAD_SHARED(P_MAX ) {
3691
3754
  }
3692
3755
  }
3693
3756
 
3694
-
3695
-
3696
-
3757
+
3758
+
3759
+
3697
3760
 
3698
3761
 
3699
3762
 
@@ -3786,17 +3849,17 @@ export const AEAD_ChaCha20Poly1305 = function AEAD_ChaCha20Poly1305
3786
3849
  Nn: 12,
3787
3850
  Nt: 16,
3788
3851
  algorithm: 'ChaCha20-Poly1305',
3789
-
3852
+
3790
3853
  keyFormat: 'raw-secret',
3791
3854
  ...AEAD_SHARED(CHACHA20_POLY1305_P_MAX),
3792
3855
  }
3793
3856
  }
3794
3857
 
3858
+
3859
+
3860
+
3795
3861
 
3796
-
3797
-
3798
-
3799
-
3862
+
3800
3863
  const InvalidInvocation = (_ ) => {
3801
3864
  if (_ !== priv) {
3802
3865
  throw new Error('invalid invocation')
@@ -3910,19 +3973,19 @@ function RandomScalarNist(t , seed )
3910
3973
  return bigIntToUint8Array(sk_bigint, t.Nscalar )
3911
3974
  }
3912
3975
 
3913
-
3976
+
3914
3977
  async function expandDecapsKeyG(kem , seed ) {
3915
3978
  const Nout = kem.pq.Nseed + kem.t.Nseed
3916
3979
  const bits = Nout << 3
3917
-
3980
+
3918
3981
  const algorithm = { name: 'cSHAKE256', length: bits, outputLength: bits }
3919
3982
  const seed_full = await subtle((c) => c.digest(algorithm, seed ), kem.name)
3920
3983
 
3921
3984
  const [seed_PQ, seed_T] = split(kem.pq.Nseed, kem.t.Nseed, new Uint8Array(seed_full))
3922
3985
 
3923
-
3986
+
3924
3987
  const format = 'raw-seed'
3925
-
3988
+
3926
3989
  const usages = ['decapsulateBits', 'encapsulateBits']
3927
3990
  const dk_PQ = await subtle(
3928
3991
  (c) => c.importKey(format, seed_PQ , kem.pq.algorithm, true, [usages[0]]),
@@ -3936,7 +3999,7 @@ async function expandDecapsKeyG(kem , seed ) {
3936
3999
  return { ek_PQ, ek_T, dk_PQ, dk_T }
3937
4000
  }
3938
4001
 
3939
-
4002
+
3940
4003
  async function C2PRICombiner(
3941
4004
  kem ,
3942
4005
  ss_PQ ,
@@ -3950,14 +4013,14 @@ async function C2PRICombiner(
3950
4013
  return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data), kem.name))
3951
4014
  }
3952
4015
 
3953
-
4016
+
3954
4017
  async function prepareEncapsG(
3955
4018
  kem ,
3956
4019
  ek_PQ ,
3957
4020
  ek_T ,
3958
4021
  ) {
3959
4022
  const res = (await subtle(
3960
-
4023
+
3961
4024
  (c) => c.encapsulateBits(kem.pq.algorithm, ek_PQ),
3962
4025
  kem.name,
3963
4026
  ))
@@ -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
-
4046
+
3985
4047
  async function prepareDecapsG(
3986
4048
  kem ,
3987
4049
  dk_PQ ,
@@ -3991,7 +4053,7 @@ async function prepareDecapsG(
3991
4053
  ) {
3992
4054
  const ss_PQ = new Uint8Array(
3993
4055
  await subtle(
3994
-
4056
+
3995
4057
  (c) => c.decapsulateBits(kem.pq.algorithm, dk_PQ, ct_PQ),
3996
4058
  kem.name,
3997
4059
  ),
@@ -4008,40 +4070,39 @@ 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
  }
4015
4076
 
4016
-
4017
-
4018
-
4019
-
4020
-
4021
-
4022
-
4023
-
4024
-
4025
-
4026
-
4027
-
4028
-
4029
-
4030
-
4031
-
4032
-
4033
-
4034
-
4035
-
4036
-
4037
-
4038
-
4039
-
4040
-
4041
-
4042
-
4043
-
4044
-
4077
+
4078
+
4079
+
4080
+
4081
+
4082
+
4083
+
4084
+
4085
+
4086
+
4087
+
4088
+
4089
+
4090
+
4091
+
4092
+
4093
+
4094
+
4095
+
4096
+
4097
+
4098
+
4099
+
4100
+
4101
+
4102
+
4103
+
4104
+
4105
+
4045
4106
 
4046
4107
  function COMPOSITE_HYBRID_KEM_SHARED() {
4047
4108
  Object.freeze(HybridKey.prototype)
@@ -4078,7 +4139,7 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
4078
4139
  async SerializePublicKey( key) {
4079
4140
  assertKeyAlgorithm(key, this.algorithm)
4080
4141
  HybridKey.validate(key, true)
4081
-
4142
+
4082
4143
  const format = 'raw-public'
4083
4144
  const ek_PQ = new Uint8Array(
4084
4145
  await subtle((c) => c.exportKey(format, key.getPq(priv)), this.name),
@@ -4090,9 +4151,9 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
4090
4151
  return concat(ek_PQ, ek_T)
4091
4152
  },
4092
4153
  async DeserializePublicKey( key) {
4093
-
4154
+
4094
4155
  const format = 'raw-public'
4095
-
4156
+
4096
4157
  const usages = ['encapsulateBits']
4097
4158
  const pubPq = key.subarray(0, this.pq.Npk)
4098
4159
  const pubT = key.subarray(this.pq.Npk)
@@ -4110,7 +4171,8 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
4110
4171
  return key.getSeed(priv)
4111
4172
  },
4112
4173
  async DeserializePrivateKey( 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() {
4119
4181
  extractable,
4120
4182
  dk_PQ,
4121
4183
  dk_T,
4122
- slice(key),
4184
+ seed,
4123
4185
  publicKey,
4124
4186
  )
4125
4187
 
@@ -4161,7 +4223,7 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
4161
4223
 
4162
4224
  function createCompositeHybridKEM(parameters ) {
4163
4225
  const kdf = KDF_SHAKE256()
4164
-
4226
+
4165
4227
  kdf.name = parameters.name
4166
4228
  return {
4167
4229
  ...parameters,
@@ -4173,9 +4235,9 @@ function createCompositeHybridKEM(parameters )
4173
4235
  }
4174
4236
  }
4175
4237
 
4176
-
4177
-
4178
-
4238
+
4239
+
4240
+
4179
4241
 
4180
4242
 
4181
4243
 
@@ -4210,12 +4272,12 @@ export const KEM_MLKEM768_X25519 = function ()
4210
4272
  const Npk = 1216
4211
4273
  const Nsk = 32
4212
4274
  const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
4213
-
4275
+
4214
4276
  if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
4215
4277
  return createDirectKEM(kem)
4216
4278
  }
4217
4279
 
4218
- const pkcs8 = Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20)
4280
+ const pkcs8 = Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20)
4219
4281
  return createCompositeHybridKEM({
4220
4282
  ...kem,
4221
4283
  pq: { algorithm: { name: 'ML-KEM-768' }, Nseed: 64, Npk: 1184, Nct: 1088 },
@@ -4233,7 +4295,7 @@ export const KEM_MLKEM768_X25519 = function ()
4233
4295
  return { privateKey, publicKey }
4234
4296
  },
4235
4297
  },
4236
- label: Uint8Array.of(0x5c, 0x2e, 0x2f, 0x2f, 0x5e, 0x5c),
4298
+ label: Uint8Array.of(0x5c, 0x2e, 0x2f, 0x2f, 0x5e, 0x5c),
4237
4299
  })
4238
4300
  }
4239
4301
 
@@ -4270,7 +4332,7 @@ export const KEM_MLKEM768_P256 = function ()
4270
4332
  const Npk = 1249
4271
4333
  const Nsk = 32
4272
4334
  const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
4273
-
4335
+
4274
4336
  if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
4275
4337
  return createDirectKEM(kem)
4276
4338
  }
@@ -4292,7 +4354,7 @@ export const KEM_MLKEM768_P256 = function ()
4292
4354
  return GetKeyPairNist(P256, sk, true, name)
4293
4355
  },
4294
4356
  },
4295
- label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x37, 0x36, 0x38, 0x2d, 0x50, 0x32, 0x35, 0x36),
4357
+ label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x37, 0x36, 0x38, 0x2d, 0x50, 0x32, 0x35, 0x36),
4296
4358
  })
4297
4359
  }
4298
4360
 
@@ -4329,7 +4391,7 @@ export const KEM_MLKEM1024_P384 = function ()
4329
4391
  const Npk = 1665
4330
4392
  const Nsk = 32
4331
4393
  const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
4332
-
4394
+
4333
4395
  if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
4334
4396
  return createDirectKEM(kem)
4335
4397
  }
@@ -4352,6 +4414,6 @@ export const KEM_MLKEM1024_P384 = function ()
4352
4414
  return GetKeyPairNist(P384, sk, true, name)
4353
4415
  },
4354
4416
  },
4355
- label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x31, 0x30, 0x32, 0x34, 0x2d, 0x50, 0x33, 0x38, 0x34),
4417
+ label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x31, 0x30, 0x32, 0x34, 0x2d, 0x50, 0x33, 0x38, 0x34),
4356
4418
  })
4357
4419
  }