hpke 1.0.4 → 1.0.6

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
@@ -40,6 +40,7 @@
40
40
 
41
41
 
42
42
 
43
+
43
44
 
44
45
 
45
46
  function ComputeNonce(base_nonce , seq , Nn ) {
@@ -47,6 +48,7 @@ function ComputeNonce(base_nonce , seq , Nn )
47
48
  return xor(base_nonce, seq_bytes)
48
49
  }
49
50
 
51
+
50
52
  function IncrementSeq(seq ) {
51
53
 
52
54
 
@@ -57,6 +59,7 @@ function IncrementSeq(seq ) {
57
59
  return ++seq
58
60
  }
59
61
 
62
+
60
63
  async function ContextExport(
61
64
  suite ,
62
65
  exporterSecret ,
@@ -68,7 +71,7 @@ async function ContextExport(
68
71
  if (!Number.isInteger(L) || L <= 0 || L > 0xffff) {
69
72
  throw new TypeError('"L" must be a positive integer not exceeding 65535')
70
73
  }
71
- const Export = stages === 1 ? ExportOneStage : ExportTwoStage
74
+ const Export = stages === 1 ? Export_OneStage : Export_TwoStage
72
75
  return await Export(suite.KDF, suite.id, exporterSecret, exporterContext, L)
73
76
  }
74
77
 
@@ -170,6 +173,7 @@ class SenderContext {
170
173
 
171
174
 
172
175
 
176
+
173
177
 
174
178
 
175
179
  async Seal(plaintext , aad ) {
@@ -217,6 +221,7 @@ class SenderContext {
217
221
 
218
222
 
219
223
 
224
+
220
225
 
221
226
 
222
227
  async Export(exporterContext , length ) {
@@ -317,6 +322,7 @@ class RecipientContext {
317
322
 
318
323
 
319
324
 
325
+
320
326
 
321
327
 
322
328
  async Open(ciphertext , aad ) {
@@ -373,6 +379,7 @@ class RecipientContext {
373
379
 
374
380
 
375
381
 
382
+
376
383
 
377
384
 
378
385
  async Export(exporterContext , length ) {
@@ -796,6 +803,7 @@ export class CipherSuite {
796
803
 
797
804
 
798
805
 
806
+
799
807
 
800
808
 
801
809
  async Seal(
@@ -844,6 +852,7 @@ export class CipherSuite {
844
852
 
845
853
 
846
854
 
855
+
847
856
 
848
857
 
849
858
  async Open(
@@ -891,6 +900,7 @@ export class CipherSuite {
891
900
 
892
901
 
893
902
 
903
+
894
904
 
895
905
 
896
906
  async SendExport(
@@ -938,6 +948,7 @@ export class CipherSuite {
938
948
 
939
949
 
940
950
 
951
+
941
952
 
942
953
 
943
954
  async ReceiveExport(
@@ -991,6 +1002,7 @@ export class CipherSuite {
991
1002
 
992
1003
 
993
1004
 
1005
+
994
1006
 
995
1007
 
996
1008
  async SetupSender(
@@ -1071,6 +1083,7 @@ export class CipherSuite {
1071
1083
 
1072
1084
 
1073
1085
 
1086
+
1074
1087
 
1075
1088
 
1076
1089
  async SetupRecipient(
@@ -1258,6 +1271,8 @@ export class NotSupportedError extends Error {
1258
1271
 
1259
1272
 
1260
1273
 
1274
+
1275
+
1261
1276
  export const MODE_BASE = 0x00
1262
1277
 
1263
1278
 
@@ -1267,6 +1282,8 @@ export const MODE_BASE = 0x00
1267
1282
 
1268
1283
 
1269
1284
 
1285
+
1286
+
1270
1287
  export const MODE_PSK = 0x01
1271
1288
 
1272
1289
 
@@ -1474,6 +1491,7 @@ function lengthPrefixed(x ) {
1474
1491
 
1475
1492
 
1476
1493
 
1494
+
1477
1495
 
1478
1496
 
1479
1497
  export async function LabeledDerive(
@@ -1495,7 +1513,8 @@ export async function LabeledDerive(
1495
1513
  return await KDF.Derive(labeled_ikm, L)
1496
1514
  }
1497
1515
 
1498
- async function ExportOneStage(
1516
+
1517
+ async function Export_OneStage(
1499
1518
  KDF ,
1500
1519
  suite_id ,
1501
1520
  exporter_secret ,
@@ -1506,7 +1525,8 @@ async function ExportOneStage(
1506
1525
  return await LabeledDerive(KDF, suite_id, exporter_secret, encode('sec'), exporter_context, L)
1507
1526
  }
1508
1527
 
1509
- async function CombineSecretsOneStage(
1528
+
1529
+ async function CombineSecrets_OneStage(
1510
1530
  suite ,
1511
1531
  mode ,
1512
1532
  shared_secret ,
@@ -1559,7 +1579,8 @@ function checkExtractable(extractable )
1559
1579
  }
1560
1580
  }
1561
1581
 
1562
- async function CombineSecretsTwoStage(
1582
+
1583
+ async function CombineSecrets_TwoStage(
1563
1584
  suite ,
1564
1585
  mode ,
1565
1586
  shared_secret ,
@@ -1608,7 +1629,8 @@ async function CombineSecretsTwoStage(
1608
1629
  return { key, base_nonce, exporter_secret }
1609
1630
  }
1610
1631
 
1611
- async function ExportTwoStage(
1632
+
1633
+ async function Export_TwoStage(
1612
1634
  KDF ,
1613
1635
  suite_id ,
1614
1636
  exporter_secret ,
@@ -1756,6 +1778,7 @@ async function ExportTwoStage(
1756
1778
 
1757
1779
 
1758
1780
 
1781
+
1759
1782
 
1760
1783
 
1761
1784
  export async function LabeledExtract(
@@ -1787,6 +1810,7 @@ export async function LabeledExtract(
1787
1810
 
1788
1811
 
1789
1812
 
1813
+
1790
1814
  export async function LabeledExpand(
1791
1815
  KDF ,
1792
1816
  suite_id ,
@@ -2155,6 +2179,7 @@ function isKey(key , type , extractable )
2155
2179
 
2156
2180
 
2157
2181
 
2182
+
2158
2183
 
2159
2184
 
2160
2185
  export function I2OSP(n , w ) {
@@ -2185,6 +2210,7 @@ function KDFStages(KDF ) {
2185
2210
  throw new Error('unreachable')
2186
2211
  }
2187
2212
 
2213
+
2188
2214
  async function KeySchedule(
2189
2215
  suite ,
2190
2216
  mode ,
@@ -2201,12 +2227,13 @@ async function KeySchedule(
2201
2227
  checkUint8Array(pskId, 'pskId')
2202
2228
 
2203
2229
  const stages = KDFStages(suite.KDF)
2204
- const CombineSecrets = stages === 1 ? CombineSecretsOneStage : CombineSecretsTwoStage
2230
+ const CombineSecrets = stages === 1 ? CombineSecrets_OneStage : CombineSecrets_TwoStage
2205
2231
 
2206
2232
  VerifyPSKInputs(psk, pskId)
2207
2233
  return await CombineSecrets(suite, mode, shared_secret, info, psk, pskId)
2208
2234
  }
2209
2235
 
2236
+
2210
2237
  function VerifyPSKInputs(psk , psk_id ) {
2211
2238
  if (psk.byteLength && psk_id.byteLength) {
2212
2239
  if (psk.byteLength < 32) {
@@ -2237,6 +2264,7 @@ const EXPORT_ONLY = 0xffff
2237
2264
 
2238
2265
 
2239
2266
 
2267
+
2240
2268
  export const AEAD_EXPORT_ONLY = function () {
2241
2269
  return {
2242
2270
  id: EXPORT_ONLY,
@@ -2366,6 +2394,7 @@ function HKDF_SHARED() {
2366
2394
 
2367
2395
 
2368
2396
 
2397
+
2369
2398
 
2370
2399
 
2371
2400
  export const KDF_HKDF_SHA256 = function () {
@@ -2390,6 +2419,7 @@ export const KDF_HKDF_SHA256 = function () {
2390
2419
 
2391
2420
 
2392
2421
 
2422
+
2393
2423
  export const KDF_HKDF_SHA384 = function () {
2394
2424
  return { id: 0x0002, type: 'KDF', name: 'HKDF-SHA384', Nh: 48, hash: 'SHA-384', ...HKDF_SHARED() }
2395
2425
  }
@@ -2412,6 +2442,7 @@ export const KDF_HKDF_SHA384 = function () {
2412
2442
 
2413
2443
 
2414
2444
 
2445
+
2415
2446
  export const KDF_HKDF_SHA512 = function () {
2416
2447
  return { id: 0x0003, type: 'KDF', name: 'HKDF-SHA512', Nh: 64, hash: 'SHA-512', ...HKDF_SHARED() }
2417
2448
  }
@@ -2459,6 +2490,7 @@ function SHAKE_SHARED() {
2459
2490
 
2460
2491
 
2461
2492
 
2493
+
2462
2494
  export const KDF_SHAKE128 = function () {
2463
2495
  return {
2464
2496
  id: 0x0010,
@@ -2488,6 +2520,7 @@ export const KDF_SHAKE128 = function () {
2488
2520
 
2489
2521
 
2490
2522
 
2523
+
2491
2524
  export const KDF_SHAKE256 = function () {
2492
2525
  return {
2493
2526
  id: 0x0011,
@@ -2592,7 +2625,8 @@ function b64u(input ) {
2592
2625
 
2593
2626
 
2594
2627
 
2595
- async function DeriveCandidate(
2628
+
2629
+ async function DeriveCandidate_TwoStage(
2596
2630
  DHKEM ,
2597
2631
  suite_id ,
2598
2632
  ikm ,
@@ -2658,6 +2692,29 @@ function assertCryptoKey(key ) {
2658
2692
  }
2659
2693
 
2660
2694
 
2695
+ async function ExtractAndExpand_TwoStage(
2696
+ DHKEM ,
2697
+ dh ,
2698
+ kem_context ,
2699
+ ) {
2700
+ const eae_prk = await LabeledExtract(
2701
+ DHKEM.kdf,
2702
+ DHKEM.suite_id,
2703
+ new Uint8Array(),
2704
+ encode('eae_prk'),
2705
+ dh,
2706
+ )
2707
+ return await LabeledExpand(
2708
+ DHKEM.kdf,
2709
+ DHKEM.suite_id,
2710
+ eae_prk,
2711
+ encode('shared_secret'),
2712
+ kem_context,
2713
+ DHKEM.Nsecret,
2714
+ )
2715
+ }
2716
+
2717
+
2661
2718
 
2662
2719
 
2663
2720
 
@@ -2705,21 +2762,7 @@ function DHKEM_SHARED()
2705
2762
  const enc = await this.SerializePublicKey(pkE)
2706
2763
  const pkRm = await this.SerializePublicKey(pkR)
2707
2764
  const kem_context = concat(enc, pkRm)
2708
- const eae_prk = await LabeledExtract(
2709
- this.kdf,
2710
- this.suite_id,
2711
- new Uint8Array(),
2712
- encode('eae_prk'),
2713
- dh,
2714
- )
2715
- const shared_secret = await LabeledExpand(
2716
- this.kdf,
2717
- this.suite_id,
2718
- eae_prk,
2719
- encode('shared_secret'),
2720
- kem_context,
2721
- this.Nsecret,
2722
- )
2765
+ const shared_secret = await ExtractAndExpand_TwoStage(this, dh, kem_context)
2723
2766
  return { shared_secret, enc }
2724
2767
  },
2725
2768
  async Decap( enc, skR, pkR) {
@@ -2746,21 +2789,7 @@ function DHKEM_SHARED()
2746
2789
 
2747
2790
  const pkRm = await this.SerializePublicKey(pkR)
2748
2791
  const kem_context = concat(enc, pkRm)
2749
- const eae_prk = await LabeledExtract(
2750
- this.kdf,
2751
- this.suite_id,
2752
- new Uint8Array(),
2753
- encode('eae_prk'),
2754
- dh,
2755
- )
2756
- const shared_secret = await LabeledExpand(
2757
- this.kdf,
2758
- this.suite_id,
2759
- eae_prk,
2760
- encode('shared_secret'),
2761
- kem_context,
2762
- this.Nsecret,
2763
- )
2792
+ const shared_secret = await ExtractAndExpand_TwoStage(this, dh, kem_context)
2764
2793
  return shared_secret
2765
2794
  },
2766
2795
  }
@@ -2950,6 +2979,7 @@ async function DeserializePrivateKeyNist(
2950
2979
  return privateKey
2951
2980
  }
2952
2981
 
2982
+
2953
2983
  async function DeriveKeyPairNist(
2954
2984
 
2955
2985
  ikm ,
@@ -2957,18 +2987,18 @@ async function DeriveKeyPairNist(
2957
2987
  ) {
2958
2988
  let sk = 0n
2959
2989
  let counter = 0
2960
- let bytes
2990
+ let candidate
2961
2991
  while (sk === 0n || sk >= this.order) {
2962
2992
  if (counter > 255) {
2963
2993
  throw new DeriveKeyPairError('Key derivation exceeded maximum iterations')
2964
2994
  }
2965
- bytes = await DeriveCandidate(this, this.suite_id, ikm, counter)
2966
- bytes[0] = bytes[0] & this.bitmask
2967
- sk = OS2IP(bytes)
2995
+ candidate = await DeriveCandidate_TwoStage(this, this.suite_id, ikm, counter)
2996
+ candidate[0] = candidate[0] & this.bitmask
2997
+ sk = OS2IP(candidate)
2968
2998
  counter = counter + 1
2969
2999
  }
2970
3000
 
2971
- return GetKeyPairNist(this, bytes , extractable, this.name)
3001
+ return GetKeyPairNist(this, candidate , extractable, this.name)
2972
3002
  }
2973
3003
 
2974
3004
  async function GetKeyPairNist(
@@ -2997,6 +3027,7 @@ async function GetKeyPairNist(
2997
3027
 
2998
3028
 
2999
3029
 
3030
+
3000
3031
  async function DeriveKeyPairX( ikm , extractable ) {
3001
3032
  const dkp_prk = await LabeledExtract(
3002
3033
  this.kdf,
@@ -3049,6 +3080,7 @@ const P256 = {
3049
3080
 
3050
3081
 
3051
3082
 
3083
+
3052
3084
 
3053
3085
  export const KEM_DHKEM_P256_HKDF_SHA256 = function () {
3054
3086
  const id = 0x0010
@@ -3103,6 +3135,7 @@ const P384 = {
3103
3135
 
3104
3136
 
3105
3137
 
3138
+
3106
3139
 
3107
3140
  export const KEM_DHKEM_P384_HKDF_SHA384 = function () {
3108
3141
  const id = 0x0011
@@ -3157,6 +3190,7 @@ const P521 = {
3157
3190
 
3158
3191
 
3159
3192
 
3193
+
3160
3194
 
3161
3195
  export const KEM_DHKEM_P521_HKDF_SHA512 = function () {
3162
3196
  const id = 0x0012
@@ -3201,6 +3235,7 @@ export const KEM_DHKEM_P521_HKDF_SHA512 = function ()
3201
3235
 
3202
3236
 
3203
3237
 
3238
+
3204
3239
 
3205
3240
 
3206
3241
  export const KEM_DHKEM_X25519_HKDF_SHA256 = function () {
@@ -3248,6 +3283,7 @@ export const KEM_DHKEM_X25519_HKDF_SHA256 = function ()
3248
3283
 
3249
3284
 
3250
3285
 
3286
+
3251
3287
 
3252
3288
  export const KEM_DHKEM_X448_HKDF_SHA512 = function () {
3253
3289
  const id = 0x0021
@@ -3391,6 +3427,7 @@ function MLKEM_SHARED() {
3391
3427
 
3392
3428
 
3393
3429
 
3430
+
3394
3431
 
3395
3432
 
3396
3433
  export const KEM_ML_KEM_512 = function () {
@@ -3432,6 +3469,7 @@ export const KEM_ML_KEM_512 = function () {
3432
3469
 
3433
3470
 
3434
3471
 
3472
+
3435
3473
  export const KEM_ML_KEM_768 = function () {
3436
3474
  const id = 0x0041
3437
3475
  const name = 'ML-KEM-768'
@@ -3471,6 +3509,7 @@ export const KEM_ML_KEM_768 = function () {
3471
3509
 
3472
3510
 
3473
3511
 
3512
+
3474
3513
  export const KEM_ML_KEM_1024 = function () {
3475
3514
  const id = 0x0042
3476
3515
  const name = 'ML-KEM-1024'
@@ -3557,6 +3596,7 @@ function AEAD_SHARED() {
3557
3596
 
3558
3597
 
3559
3598
 
3599
+
3560
3600
 
3561
3601
 
3562
3602
  export const AEAD_AES_128_GCM = function () {
@@ -3590,6 +3630,7 @@ export const AEAD_AES_128_GCM = function () {
3590
3630
 
3591
3631
 
3592
3632
 
3633
+
3593
3634
  export const AEAD_AES_256_GCM = function () {
3594
3635
  return {
3595
3636
  id: 0x0002,
@@ -3621,6 +3662,7 @@ export const AEAD_AES_256_GCM = function () {
3621
3662
 
3622
3663
 
3623
3664
 
3665
+
3624
3666
  export const AEAD_ChaCha20Poly1305 = function AEAD_ChaCha20Poly1305() {
3625
3667
  return {
3626
3668
  id: 0x0003,
@@ -3754,6 +3796,7 @@ function RandomScalarNist(t , seed ) {
3754
3796
  return bigIntToUint8Array(sk_bigint, t.Nscalar )
3755
3797
  }
3756
3798
 
3799
+
3757
3800
  async function expandDecapsKeyG(PQTKEM , _seed ) {
3758
3801
  const Nout = PQTKEM.pq.Nseed + PQTKEM.t.Nseed
3759
3802
  const bits = Nout << 3
@@ -3781,6 +3824,7 @@ async function expandDecapsKeyG(PQTKEM , _seed ) {
3781
3824
  return { ek_PQ, ek_T, dk_PQ, dk_T }
3782
3825
  }
3783
3826
 
3827
+
3784
3828
  async function C2PRICombiner(
3785
3829
  PQTKEM ,
3786
3830
  ss_PQ ,
@@ -3794,6 +3838,7 @@ async function C2PRICombiner(
3794
3838
  return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data), PQTKEM.name))
3795
3839
  }
3796
3840
 
3841
+
3797
3842
  async function prepareEncapsG(
3798
3843
  PQTKEM ,
3799
3844
  ek_PQ ,
@@ -3824,6 +3869,7 @@ async function prepareEncapsG(
3824
3869
  return [ss_PQ, ss_T, ct_PQ, ct_T]
3825
3870
  }
3826
3871
 
3872
+
3827
3873
  async function prepareDecapsG(
3828
3874
  PQTKEM ,
3829
3875
  dk_PQ ,
@@ -4020,6 +4066,7 @@ function PQTKEM_SHARED() {
4020
4066
 
4021
4067
 
4022
4068
 
4069
+
4023
4070
 
4024
4071
 
4025
4072
  export const KEM_MLKEM768_X25519 = function () {
@@ -4078,6 +4125,7 @@ export const KEM_MLKEM768_X25519 = function () {
4078
4125
 
4079
4126
 
4080
4127
 
4128
+
4081
4129
  export const KEM_MLKEM768_P256 = function () {
4082
4130
  const id = 0x0050
4083
4131
  const name = 'MLKEM768-P256'
@@ -4133,6 +4181,7 @@ export const KEM_MLKEM768_P256 = function () {
4133
4181
 
4134
4182
 
4135
4183
 
4184
+
4136
4185
  export const KEM_MLKEM1024_P384 = function () {
4137
4186
  const id = 0x0051
4138
4187
  const name = 'MLKEM1024-P384'