hpke 1.0.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -43,52 +43,52 @@
43
43
 
44
44
 
45
45
 
46
+ function ComputeNonce(base_nonce , seq , Nn ) {
47
+ const seq_bytes = I2OSP(seq, Nn)
48
+ return xor(base_nonce, seq_bytes)
49
+ }
46
50
 
47
51
 
52
+ function IncrementSeq(seq ) {
48
53
 
49
54
 
50
55
 
56
+ if (seq >= Number.MAX_SAFE_INTEGER) {
57
+ throw new MessageLimitReachedError('Sequence number overflow')
58
+ }
59
+ return ++seq
60
+ }
51
61
 
52
62
 
63
+ async function ContextExport(
64
+ suite ,
65
+ exporterSecret ,
66
+ exporterContext ,
67
+ L ,
68
+ ) {
69
+ checkUint8Array(exporterContext, 'exporterContext')
70
+ const stages = KDFStages(suite.KDF)
71
+ if (!Number.isInteger(L) || L <= 0 || L > 0xffff) {
72
+ throw new TypeError('"L" must be a positive integer not exceeding 65535')
73
+ }
74
+ const Export = stages === 1 ? Export_OneStage : Export_TwoStage
75
+ return await Export(suite.KDF, suite.id, exporterSecret, exporterContext, L)
76
+ }
53
77
 
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
78
+ class Mutex {
79
+ #locked = Promise.resolve()
80
+
81
+ async lock() {
82
+ let releaseLock
83
+ const nextLock = new Promise ((resolve) => {
84
+ releaseLock = resolve
85
+ })
86
+ const previousLock = this.#locked
87
+ this.#locked = nextLock
88
+ await previousLock
89
+ return releaseLock
90
+ }
91
+ }
92
92
 
93
93
 
94
94
 
@@ -1415,6 +1415,8 @@ export const MODE_PSK = 0x01
1415
1415
 
1416
1416
 
1417
1417
 
1418
+
1419
+
1418
1420
 
1419
1421
 
1420
1422
 
@@ -1514,132 +1516,132 @@ export async function LabeledDerive(
1514
1516
  }
1515
1517
 
1516
1518
 
1519
+ async function Export_OneStage(
1520
+ KDF ,
1521
+ suite_id ,
1522
+ exporter_secret ,
1523
+ exporter_context ,
1524
+ L ,
1525
+ ) {
1526
+ checkLength(exporter_context, 'Exporter context', MAX_LENGTH_ONE_STAGE)
1527
+ return await LabeledDerive(KDF, suite_id, exporter_secret, encode('sec'), exporter_context, L)
1528
+ }
1517
1529
 
1518
1530
 
1531
+ async function CombineSecrets_OneStage(
1532
+ suite ,
1533
+ mode ,
1534
+ shared_secret ,
1535
+ info ,
1536
+ psk ,
1537
+ psk_id ,
1538
+ ) {
1539
+ checkLength(psk, 'PSK', MAX_LENGTH_ONE_STAGE)
1540
+ checkLength(psk_id, 'PSK ID', MAX_LENGTH_ONE_STAGE)
1541
+ checkLength(info, 'Info', MAX_LENGTH_ONE_STAGE)
1542
+
1543
+ const secrets = concat(lengthPrefixed(psk), lengthPrefixed(shared_secret))
1544
+ const context = concat(I2OSP(mode, 1), lengthPrefixed(psk_id), lengthPrefixed(info))
1545
+
1546
+ const secret = await LabeledDerive(
1547
+ suite.KDF,
1548
+ suite.id,
1549
+ secrets,
1550
+ encode('secret'),
1551
+ context,
1552
+ suite.AEAD.Nk + suite.AEAD.Nn + suite.KDF.Nh,
1553
+ )
1519
1554
 
1555
+ const key = slice(secret, 0, suite.AEAD.Nk)
1556
+ const base_nonce = slice(secret, suite.AEAD.Nk, suite.AEAD.Nk + suite.AEAD.Nn)
1557
+ const exporter_secret = slice(secret, suite.AEAD.Nk + suite.AEAD.Nn)
1520
1558
 
1559
+ return { key, base_nonce, exporter_secret }
1560
+ }
1521
1561
 
1522
1562
 
1523
1563
 
1564
+ const MAX_LENGTH_TWO_STAGE = 0xffff
1524
1565
 
1566
+ const MAX_LENGTH_ONE_STAGE = 0xffff
1525
1567
 
1568
+ function checkLength(data , name , maxLength ) {
1569
+ if (data.byteLength > maxLength) {
1570
+ throw new TypeError(`${name} length must not exceed ${maxLength} bytes`)
1571
+ }
1572
+ }
1573
+ function checkUint8Array(input , name ) {
1574
+ if (!(input instanceof Uint8Array)) {
1575
+ throw new TypeError(`"${name}" must be Uint8Array`)
1576
+ }
1577
+ }
1578
+ function checkExtractable(extractable ) {
1579
+ if (typeof extractable !== 'boolean') {
1580
+ throw new TypeError('"extractable" must be boolean')
1581
+ }
1582
+ }
1526
1583
 
1527
1584
 
1585
+ async function CombineSecrets_TwoStage(
1586
+ suite ,
1587
+ mode ,
1588
+ shared_secret ,
1589
+ info ,
1590
+ psk ,
1591
+ psk_id ,
1592
+ ) {
1593
+ checkLength(psk, 'PSK', MAX_LENGTH_TWO_STAGE)
1594
+ checkLength(psk_id, 'PSK ID', MAX_LENGTH_TWO_STAGE)
1595
+ checkLength(info, 'Info', MAX_LENGTH_TWO_STAGE)
1596
+
1597
+ const [psk_id_hash, info_hash] = await Promise.all([
1598
+ LabeledExtract(suite.KDF, suite.id, new Uint8Array(), encode('psk_id_hash'), psk_id),
1599
+ LabeledExtract(suite.KDF, suite.id, new Uint8Array(), encode('info_hash'), info),
1600
+ ])
1601
+
1602
+ const key_schedule_context = concat(I2OSP(mode, 1), psk_id_hash, info_hash)
1603
+ const secret = await LabeledExtract(suite.KDF, suite.id, shared_secret, encode('secret'), psk)
1604
+
1605
+
1606
+ if (suite.AEAD.id === EXPORT_ONLY) {
1607
+ const exporter_secret = await LabeledExpand(
1608
+ suite.KDF,
1609
+ suite.id,
1610
+ secret,
1611
+ encode('exp'),
1612
+ key_schedule_context,
1613
+ suite.KDF.Nh,
1614
+ )
1615
+ return { key: new Uint8Array(), base_nonce: new Uint8Array(), exporter_secret }
1616
+ }
1528
1617
 
1618
+ const [key, base_nonce, exporter_secret] = await Promise.all([
1619
+ LabeledExpand(suite.KDF, suite.id, secret, encode('key'), key_schedule_context, suite.AEAD.Nk),
1620
+ LabeledExpand(
1621
+ suite.KDF,
1622
+ suite.id,
1623
+ secret,
1624
+ encode('base_nonce'),
1625
+ key_schedule_context,
1626
+ suite.AEAD.Nn,
1627
+ ),
1628
+ LabeledExpand(suite.KDF, suite.id, secret, encode('exp'), key_schedule_context, suite.KDF.Nh),
1629
+ ])
1630
+
1631
+ return { key, base_nonce, exporter_secret }
1632
+ }
1529
1633
 
1530
1634
 
1531
-
1532
-
1533
-
1534
-
1535
-
1536
-
1537
-
1538
-
1539
-
1540
-
1541
-
1542
-
1543
-
1544
-
1545
-
1546
-
1547
-
1548
-
1549
-
1550
-
1551
-
1552
-
1553
-
1554
-
1555
-
1556
-
1557
-
1558
-
1559
-
1560
-
1561
-
1562
-
1563
-
1564
-
1565
-
1566
-
1567
-
1568
-
1569
-
1570
-
1571
-
1572
-
1573
-
1574
-
1575
-
1576
-
1577
-
1578
-
1579
-
1580
-
1581
-
1582
-
1583
-
1584
-
1585
-
1586
-
1587
-
1588
-
1589
-
1590
-
1591
-
1592
-
1593
-
1594
-
1595
-
1596
-
1597
-
1598
-
1599
-
1600
-
1601
-
1602
-
1603
-
1604
-
1605
-
1606
-
1607
-
1608
-
1609
-
1610
-
1611
-
1612
-
1613
-
1614
-
1615
-
1616
-
1617
-
1618
-
1619
-
1620
-
1621
-
1622
-
1623
-
1624
-
1625
-
1626
-
1627
-
1628
-
1629
-
1630
-
1631
-
1632
-
1633
-
1634
-
1635
-
1636
-
1637
-
1638
-
1639
-
1640
-
1641
-
1642
-
1635
+ async function Export_TwoStage(
1636
+ KDF ,
1637
+ suite_id ,
1638
+ exporter_secret ,
1639
+ exporter_context ,
1640
+ L ,
1641
+ ) {
1642
+ checkLength(exporter_context, 'Exporter context', MAX_LENGTH_TWO_STAGE)
1643
+ return await LabeledExpand(KDF, suite_id, exporter_secret, encode('sec'), exporter_context, L)
1644
+ }
1643
1645
 
1644
1646
 
1645
1647
 
@@ -2211,41 +2213,41 @@ function KDFStages(KDF ) {
2211
2213
  }
2212
2214
 
2213
2215
 
2216
+ async function KeySchedule(
2217
+ suite ,
2218
+ mode ,
2219
+ shared_secret ,
2220
+ info ,
2221
+ psk ,
2222
+ pskId ,
2223
+ ) {
2224
+ info ??= new Uint8Array()
2225
+ checkUint8Array(info, 'info')
2226
+ psk ??= new Uint8Array()
2227
+ checkUint8Array(psk, 'psk')
2228
+ pskId ??= new Uint8Array()
2229
+ checkUint8Array(pskId, 'pskId')
2230
+
2231
+ const stages = KDFStages(suite.KDF)
2232
+ const CombineSecrets = stages === 1 ? CombineSecrets_OneStage : CombineSecrets_TwoStage
2233
+
2234
+ VerifyPSKInputs(psk, pskId)
2235
+ return await CombineSecrets(suite, mode, shared_secret, info, psk, pskId)
2236
+ }
2214
2237
 
2215
2238
 
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
-
2239
+ function VerifyPSKInputs(psk , psk_id ) {
2240
+ if (psk.byteLength && psk_id.byteLength) {
2241
+ if (psk.byteLength < 32) {
2242
+ throw new TypeError('Insufficient PSK length')
2243
+ }
2244
+ return
2245
+ }
2246
+ if (!psk.byteLength && !psk_id.byteLength) {
2247
+ return
2248
+ }
2249
+ throw new TypeError('Inconsistent PSK inputs')
2250
+ }
2249
2251
 
2250
2252
 
2251
2253
  const NotApplicable = () => {
@@ -2532,47 +2534,9 @@ export const KDF_SHAKE256 = function () {
2532
2534
  }
2533
2535
  }
2534
2536
 
2535
- async function getPublicKeyByExport(
2536
- name ,
2537
- key ,
2538
- usages ,
2539
- ) {
2540
- if (!key.extractable) {
2541
- throw new TypeError(
2542
- '"privateKey" must be extractable or a Key Pair must be used in this runtime',
2543
- )
2544
- }
2545
-
2546
- return await subtle(async (c) => {
2547
- const jwk = await c.exportKey('jwk', key)
2548
- return c.importKey(
2549
- 'jwk',
2550
- { kty: jwk.kty, crv: jwk.crv, x: jwk.x, y: jwk.y } ,
2551
- key.algorithm,
2552
- true,
2553
- usages,
2554
- )
2555
- }, name)
2556
- }
2557
-
2558
- async function getPublicKey(name , key , usages ) {
2559
- return (
2560
2537
 
2561
- ((await subtle((c) => c.getPublicKey?.(key, usages), name)) ) ||
2562
- (await getPublicKeyByExport(name, key, usages))
2563
- )
2564
- }
2565
2538
 
2566
2539
 
2567
- function checkNotAllZeros(buffer ) {
2568
- let or = 0
2569
- for (let i = 0; i < buffer.length; i++) {
2570
- or |= buffer[i]
2571
- }
2572
- if (or === 0) {
2573
- throw new ValidationError('DH shared secret is an all-zero value')
2574
- }
2575
- }
2576
2540
 
2577
2541
 
2578
2542
 
@@ -2589,311 +2553,17 @@ function checkNotAllZeros(buffer ) {
2589
2553
 
2590
2554
 
2591
2555
 
2592
-
2593
-
2594
-
2595
-
2596
- function fromBase64(input ) {
2597
- input = input.replace(/-/g, '+').replace(/_/g, '/')
2598
- const binary = atob(input)
2599
- const bytes = new Uint8Array(binary.length)
2600
- for (let i = 0; i < binary.length; i++) {
2601
- bytes[i] = binary.charCodeAt(i)
2602
- }
2603
- return bytes
2604
- }
2605
-
2606
- function toBase64Url(bytes ) {
2607
- let binary = ''
2608
- for (let i = 0; i < bytes.length; i++) {
2609
- binary += String.fromCharCode(bytes[i] )
2556
+ export const KDF_TurboSHAKE128 = function () {
2557
+ return {
2558
+ id: 0x0012,
2559
+ type: 'KDF',
2560
+ name: 'TurboSHAKE128',
2561
+ Nh: 32,
2562
+ algorithm: 'TurboSHAKE128',
2563
+ ...SHAKE_SHARED(),
2610
2564
  }
2611
- return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '')
2612
- }
2613
-
2614
- function toB64u(input ) {
2615
-
2616
- return input.toBase64?.({ alphabet: 'base64url', omitPadding: true }) || toBase64Url(input)
2617
2565
  }
2618
2566
 
2619
- function b64u(input ) {
2620
-
2621
- return Uint8Array.fromBase64?.(input, { alphabet: 'base64url' }) || fromBase64(input)
2622
- }
2623
-
2624
-
2625
-
2626
-
2627
-
2628
-
2629
-
2630
-
2631
-
2632
-
2633
-
2634
-
2635
-
2636
-
2637
-
2638
-
2639
-
2640
-
2641
-
2642
-
2643
-
2644
-
2645
-
2646
-
2647
-
2648
-
2649
-
2650
-
2651
-
2652
-
2653
-
2654
-
2655
-
2656
-
2657
-
2658
-
2659
-
2660
-
2661
-
2662
-
2663
-
2664
-
2665
-
2666
-
2667
-
2668
-
2669
-
2670
-
2671
-
2672
-
2673
-
2674
-
2675
-
2676
-
2677
-
2678
-
2679
-
2680
-
2681
-
2682
-
2683
-
2684
-
2685
-
2686
-
2687
-
2688
-
2689
-
2690
-
2691
-
2692
-
2693
-
2694
-
2695
-
2696
-
2697
-
2698
-
2699
-
2700
-
2701
-
2702
-
2703
-
2704
-
2705
-
2706
-
2707
-
2708
-
2709
-
2710
-
2711
-
2712
-
2713
-
2714
-
2715
-
2716
-
2717
-
2718
-
2719
-
2720
-
2721
-
2722
-
2723
-
2724
-
2725
-
2726
-
2727
-
2728
-
2729
-
2730
-
2731
-
2732
-
2733
-
2734
-
2735
-
2736
-
2737
-
2738
-
2739
-
2740
-
2741
-
2742
-
2743
-
2744
-
2745
-
2746
-
2747
-
2748
-
2749
-
2750
-
2751
-
2752
-
2753
-
2754
-
2755
-
2756
-
2757
-
2758
-
2759
-
2760
-
2761
-
2762
-
2763
-
2764
-
2765
-
2766
-
2767
-
2768
-
2769
-
2770
-
2771
-
2772
-
2773
-
2774
-
2775
-
2776
-
2777
-
2778
-
2779
-
2780
-
2781
-
2782
-
2783
-
2784
-
2785
-
2786
-
2787
-
2788
-
2789
-
2790
-
2791
-
2792
-
2793
-
2794
-
2795
-
2796
-
2797
-
2798
-
2799
-
2800
-
2801
-
2802
-
2803
-
2804
-
2805
-
2806
-
2807
-
2808
-
2809
-
2810
-
2811
-
2812
-
2813
-
2814
-
2815
-
2816
-
2817
-
2818
-
2819
-
2820
-
2821
-
2822
-
2823
-
2824
-
2825
-
2826
-
2827
-
2828
-
2829
-
2830
-
2831
-
2832
-
2833
-
2834
-
2835
-
2836
-
2837
-
2838
-
2839
-
2840
-
2841
-
2842
-
2843
-
2844
-
2845
-
2846
-
2847
-
2848
-
2849
-
2850
-
2851
-
2852
-
2853
-
2854
-
2855
-
2856
-
2857
-
2858
-
2859
-
2860
-
2861
-
2862
-
2863
-
2864
-
2865
-
2866
-
2867
-
2868
-
2869
-
2870
-
2871
-
2872
-
2873
-
2874
-
2875
-
2876
-
2877
-
2878
-
2879
-
2880
-
2881
-
2882
-
2883
-
2884
-
2885
-
2886
-
2887
-
2888
-
2889
-
2890
-
2891
-
2892
-
2893
-
2894
-
2895
-
2896
-
2897
2567
 
2898
2568
 
2899
2569
 
@@ -2913,12 +2583,58 @@ function b64u(input ) {
2913
2583
 
2914
2584
 
2915
2585
 
2586
+ export const KDF_TurboSHAKE256 = function () {
2587
+ return {
2588
+ id: 0x0013,
2589
+ type: 'KDF',
2590
+ name: 'TurboSHAKE256',
2591
+ Nh: 64,
2592
+ algorithm: 'TurboSHAKE256',
2593
+ ...SHAKE_SHARED(),
2594
+ }
2595
+ }
2916
2596
 
2597
+ async function getPublicKeyByExport(
2598
+ name ,
2599
+ key ,
2600
+ usages ,
2601
+ ) {
2602
+ if (!key.extractable) {
2603
+ throw new TypeError(
2604
+ '"privateKey" must be extractable or a Key Pair must be used in this runtime',
2605
+ )
2606
+ }
2917
2607
 
2608
+ return await subtle(async (c) => {
2609
+ const jwk = await c.exportKey('jwk', key)
2610
+ return c.importKey(
2611
+ 'jwk',
2612
+ { kty: jwk.kty, crv: jwk.crv, x: jwk.x, y: jwk.y } ,
2613
+ key.algorithm,
2614
+ true,
2615
+ usages,
2616
+ )
2617
+ }, name)
2618
+ }
2918
2619
 
2620
+ async function getPublicKey(name , key , usages ) {
2621
+ return (
2919
2622
 
2623
+ ((await subtle((c) => c.getPublicKey?.(key, usages), name)) ) ||
2624
+ (await getPublicKeyByExport(name, key, usages))
2625
+ )
2626
+ }
2920
2627
 
2921
2628
 
2629
+ function checkNotAllZeros(buffer ) {
2630
+ let or = 0
2631
+ for (let i = 0; i < buffer.length; i++) {
2632
+ or |= buffer[i]
2633
+ }
2634
+ if (or === 0) {
2635
+ throw new ValidationError('DH shared secret is an all-zero value')
2636
+ }
2637
+ }
2922
2638
 
2923
2639
 
2924
2640
 
@@ -2939,48 +2655,248 @@ function b64u(input ) {
2939
2655
 
2940
2656
 
2941
2657
 
2658
+ function fromBase64(input ) {
2659
+ input = input.replace(/-/g, '+').replace(/_/g, '/')
2660
+ const binary = atob(input)
2661
+ const bytes = new Uint8Array(binary.length)
2662
+ for (let i = 0; i < binary.length; i++) {
2663
+ bytes[i] = binary.charCodeAt(i)
2664
+ }
2665
+ return bytes
2666
+ }
2942
2667
 
2668
+ function toBase64Url(bytes ) {
2669
+ let binary = ''
2670
+ for (let i = 0; i < bytes.length; i++) {
2671
+ binary += String.fromCharCode(bytes[i] )
2672
+ }
2673
+ return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '')
2674
+ }
2943
2675
 
2676
+ function toB64u(input ) {
2944
2677
 
2678
+ return input.toBase64?.({ alphabet: 'base64url', omitPadding: true }) || toBase64Url(input)
2679
+ }
2945
2680
 
2681
+ function b64u(input ) {
2946
2682
 
2683
+ return Uint8Array.fromBase64?.(input, { alphabet: 'base64url' }) || fromBase64(input)
2684
+ }
2947
2685
 
2948
2686
 
2949
2687
 
2950
2688
 
2951
2689
 
2952
2690
 
2691
+ async function DeriveCandidate_TwoStage(
2692
+ DHKEM ,
2693
+ suite_id ,
2694
+ ikm ,
2695
+ counter ,
2696
+ ) {
2697
+ const dkp_prk = await LabeledExtract(
2698
+ DHKEM.kdf,
2699
+ suite_id,
2700
+ new Uint8Array(),
2701
+ encode('dkp_prk'),
2702
+ ikm,
2703
+ )
2704
+ return await LabeledExpand(
2705
+ DHKEM.kdf,
2706
+ suite_id,
2707
+ dkp_prk,
2708
+ encode('candidate'),
2709
+ I2OSP(counter, 1),
2710
+ DHKEM.Nsk,
2711
+ )
2712
+ }
2953
2713
 
2714
+ function OS2IP(x ) {
2715
+ let result = 0n
2716
+ for (let i = 0; i < x.byteLength; i++) {
2717
+ result = result * 256n + BigInt(x[i] )
2718
+ }
2719
+ return result
2720
+ }
2954
2721
 
2722
+ function bigIntToUint8Array(value , byteLength ) {
2723
+ const result = new Uint8Array(byteLength)
2724
+ let n = value
2955
2725
 
2726
+ for (let i = byteLength - 1; i >= 0; i--) {
2727
+ result[i] = Number(n & 0xffn)
2728
+ n = n >> 8n
2729
+ }
2956
2730
 
2731
+ return result
2732
+ }
2957
2733
 
2734
+ function assertKeyAlgorithm(key , expectedAlgorithm ) {
2735
+ if (key.algorithm.name !== expectedAlgorithm.name) {
2736
+ throw new TypeError(`key algorithm must be ${expectedAlgorithm.name}`)
2737
+ }
2738
+ if (
2739
+ (key.algorithm ).namedCurve !==
2740
+ (expectedAlgorithm ).namedCurve
2741
+ ) {
2742
+ throw new TypeError(
2743
+ `key namedCurve must be ${(expectedAlgorithm ).namedCurve}`,
2744
+ )
2745
+ }
2746
+ }
2958
2747
 
2748
+ function assertCryptoKey(key ) {
2959
2749
 
2750
+ if (key[Symbol.toStringTag] !== 'CryptoKey') {
2751
+ if (key instanceof CryptoKey) return
2752
+ throw new TypeError('unexpected key constructor')
2753
+ }
2754
+ }
2960
2755
 
2961
2756
 
2757
+ async function ExtractAndExpand_TwoStage(
2758
+ DHKEM ,
2759
+ dh ,
2760
+ kem_context ,
2761
+ ) {
2762
+ const eae_prk = await LabeledExtract(
2763
+ DHKEM.kdf,
2764
+ DHKEM.suite_id,
2765
+ new Uint8Array(),
2766
+ encode('eae_prk'),
2767
+ dh,
2768
+ )
2769
+ return await LabeledExpand(
2770
+ DHKEM.kdf,
2771
+ DHKEM.suite_id,
2772
+ eae_prk,
2773
+ encode('shared_secret'),
2774
+ kem_context,
2775
+ DHKEM.Nsecret,
2776
+ )
2777
+ }
2962
2778
 
2963
2779
 
2964
2780
 
2965
2781
 
2966
2782
 
2783
+ function DHKEM_SHARED() {
2784
+ return {
2785
+ async GenerateKeyPair( extractable) {
2786
+ return (await subtle(
2787
+ (c) => c.generateKey(this.algorithm, extractable, ['deriveBits']),
2788
+ this.name,
2789
+ ))
2790
+ },
2791
+ async SerializePublicKey( key) {
2792
+ assertKeyAlgorithm(key, this.algorithm)
2793
+ assertCryptoKey(key)
2794
+ return new Uint8Array(await subtle((c) => c.exportKey('raw', key), this.name))
2795
+ },
2796
+ async DeserializePublicKey( _key) {
2797
+ const key = ab(_key)
2798
+ return await subtle((c) => c.importKey('raw', key, this.algorithm, true, []), this.name)
2799
+ },
2800
+ async SerializePrivateKey( key) {
2801
+ assertKeyAlgorithm(key, this.algorithm)
2802
+ assertCryptoKey(key)
2803
+ const { d } = await subtle((c) => c.exportKey('jwk', key), this.name)
2804
+ return b64u(d )
2805
+ },
2806
+ async Encap( pkR) {
2807
+ assertKeyAlgorithm(pkR, this.algorithm)
2808
+ assertCryptoKey(pkR)
2967
2809
 
2810
+ const ekp = (await this.GenerateKeyPair(false))
2811
+ const skE = ekp.privateKey
2812
+ const pkE = ekp.publicKey
2968
2813
 
2969
2814
 
2970
2815
 
2816
+ const dh = new Uint8Array(
2817
+ await subtle(
2818
+ (c) => c.deriveBits({ name: skE.algorithm.name, public: pkR }, skE, this.Ndh << 3),
2819
+ this.name,
2820
+ ),
2821
+ )
2822
+ checkNotAllZeros(dh)
2971
2823
 
2824
+ const enc = await this.SerializePublicKey(pkE)
2825
+ const pkRm = await this.SerializePublicKey(pkR)
2826
+ const kem_context = concat(enc, pkRm)
2827
+ const shared_secret = await ExtractAndExpand_TwoStage(this, dh, kem_context)
2828
+ return { shared_secret, enc }
2829
+ },
2830
+ async Decap( enc, skR, pkR) {
2831
+ assertKeyAlgorithm(skR, this.algorithm)
2832
+ assertCryptoKey(skR)
2833
+ if (pkR) {
2834
+ assertKeyAlgorithm(pkR, this.algorithm)
2835
+ assertCryptoKey(pkR)
2836
+ } else {
2837
+ pkR = await getPublicKey(this.name, skR, [])
2838
+ }
2972
2839
 
2840
+ const pkE = (await this.DeserializePublicKey(enc))
2973
2841
 
2974
2842
 
2975
2843
 
2844
+ const dh = new Uint8Array(
2845
+ await subtle(
2846
+ (c) => c.deriveBits({ name: skR.algorithm.name, public: pkE }, skR, this.Ndh << 3),
2847
+ this.name,
2848
+ ),
2849
+ )
2850
+ checkNotAllZeros(dh)
2976
2851
 
2852
+ const pkRm = await this.SerializePublicKey(pkR)
2853
+ const kem_context = concat(enc, pkRm)
2854
+ const shared_secret = await ExtractAndExpand_TwoStage(this, dh, kem_context)
2855
+ return shared_secret
2856
+ },
2857
+ }
2858
+ }
2977
2859
 
2978
2860
 
2979
2861
 
2980
2862
 
2981
2863
 
2864
+ async function createKeyPairFromPrivateKey(
2865
+ DHKEM ,
2866
+ key ,
2867
+ extractable ,
2868
+ ) {
2869
+ let privateKey
2870
+ let publicKey
2982
2871
 
2872
+ if (!extractable && typeof crypto.subtle.getPublicKey !== 'function') {
2873
+ privateKey = (await DHKEM.DeserializePrivateKey(key, true))
2874
+ publicKey = await getPublicKey(DHKEM.name, privateKey, [])
2875
+ privateKey = (await DHKEM.DeserializePrivateKey(key, false))
2876
+ } else {
2877
+ privateKey = (await DHKEM.DeserializePrivateKey(key, extractable))
2878
+ publicKey = await getPublicKey(DHKEM.name, privateKey, [])
2879
+ }
2880
+ return { privateKey, publicKey }
2881
+ }
2983
2882
 
2883
+ async function CurveKeyFromD(
2884
+ name ,
2885
+ Nsk ,
2886
+ template ,
2887
+ algorithm ,
2888
+ key ,
2889
+ extractable ,
2890
+ ) {
2891
+ const tmpl = slice(template)
2892
+ const pkcs8 = new Uint8Array(Nsk + tmpl.byteLength)
2893
+ pkcs8.set(tmpl)
2894
+ pkcs8.set(key, tmpl.byteLength)
2895
+ return await subtle(
2896
+ (c) => c.importKey('pkcs8', pkcs8, algorithm, extractable, ['deriveBits']),
2897
+ name,
2898
+ )
2899
+ }
2984
2900
 
2985
2901
 
2986
2902
 
@@ -2992,35 +2908,90 @@ function b64u(input ) {
2992
2908
 
2993
2909
 
2994
2910
 
2911
+ function modInverse(a , m ) {
2912
+ a = ((a % m) + m) % m
2913
+ let [t, newT] = [0n, 1n]
2914
+ let [r, newR] = [m, a]
2995
2915
 
2916
+ while (newR !== 0n) {
2917
+ const quotient = r / newR
2918
+ ;[t, newT] = [newT, t - quotient * newT]
2919
+ ;[r, newR] = [newR, r - quotient * newR]
2920
+ }
2996
2921
 
2922
+ if (r > 1n) throw new Error('a is not invertible')
2923
+ if (t < 0n) t = t + m
2924
+ return t
2925
+ }
2997
2926
 
2998
2927
 
2928
+ function pointDouble(p , prime , a ) {
2929
+ const { x, y } = p
2999
2930
 
3000
2931
 
2932
+ const numerator = (((3n * x * x) % prime) + a) % prime
2933
+ const denominator = (2n * y) % prime
2934
+ const s = (numerator * modInverse(denominator, prime)) % prime
3001
2935
 
3002
2936
 
2937
+ const x3 = (((s * s) % prime) - ((2n * x) % prime) + prime) % prime
3003
2938
 
3004
2939
 
2940
+ const y3 = (((s * ((x - x3 + prime) % prime)) % prime) - y + prime) % prime
3005
2941
 
2942
+ return { x: x3, y: y3 }
2943
+ }
3006
2944
 
3007
2945
 
2946
+ function pointAdd(p , q , prime , a ) {
2947
+ if (p.x === q.x && p.y === q.y) {
2948
+ return pointDouble(p, prime, a)
2949
+ }
3008
2950
 
2951
+ const { x: x1, y: y1 } = p
2952
+ const { x: x2, y: y2 } = q
3009
2953
 
3010
2954
 
3011
2955
 
2956
+ if (p.x === q.x) {
2957
+ throw new Error('Point addition resulted in point at infinity')
2958
+ }
3012
2959
 
3013
2960
 
2961
+ const numerator = (((y2 - y1) % prime) + prime) % prime
2962
+ const denominator = (((x2 - x1) % prime) + prime) % prime
2963
+ const s = (numerator * modInverse(denominator, prime)) % prime
3014
2964
 
3015
2965
 
2966
+ const x3 = (((s * s) % prime) - x1 - x2 + prime + prime) % prime
3016
2967
 
3017
2968
 
2969
+ const y3 = (((s * ((x1 - x3 + prime) % prime)) % prime) - y1 + prime) % prime
3018
2970
 
2971
+ return { x: x3, y: y3 }
2972
+ }
3019
2973
 
3020
2974
 
2975
+ function scalarMult(k , G , prime , a , order ) {
2976
+ if (k === 0n || k >= order) {
2977
+ throw new Error('Invalid scalar')
2978
+ }
3021
2979
 
2980
+ let result = null
2981
+ let addend = G
2982
+ let scalar = k
3022
2983
 
2984
+ while (scalar > 0n) {
2985
+ if (scalar & 1n) {
2986
+ result = result === null ? addend : pointAdd(result, addend, prime, a)
2987
+ }
2988
+ addend = pointDouble(addend, prime, a)
2989
+ scalar = scalar >> 1n
2990
+ }
3023
2991
 
2992
+ if (result === null) throw new Error('Invalid result')
2993
+ return result
2994
+ }
3024
2995
 
3025
2996
 
3026
2997
 
@@ -3034,33 +3005,124 @@ function b64u(input ) {
3034
3005
 
3035
3006
 
3036
3007
 
3008
+ function getPrivateJwkNist(DHKEM , d ) {
3037
3009
 
3010
+ const G = { x: DHKEM.Gx, y: DHKEM.Gy }
3011
+ const publicPoint = scalarMult(d, G, DHKEM.prime, DHKEM.prime - 3n, DHKEM.order)
3038
3012
 
3013
+ const coordSize = (DHKEM.Npk - 1) / 2
3014
+ const xBytes = bigIntToUint8Array(publicPoint.x, coordSize)
3015
+ const yBytes = bigIntToUint8Array(publicPoint.y, coordSize)
3016
+ const dBytes = bigIntToUint8Array(d, DHKEM.Nsk)
3039
3017
 
3040
3018
 
3019
+ return {
3020
+ kty: 'EC',
3021
+ crv: DHKEM.algorithm.namedCurve,
3022
+ x: toB64u(xBytes),
3023
+ y: toB64u(yBytes),
3024
+ d: toB64u(dBytes),
3025
+ }
3026
+ }
3041
3027
 
3028
+ async function DeserializePrivateKeyNist(
3042
3029
 
3030
+ key ,
3031
+ extractable ,
3032
+ ) {
3033
+ const d = OS2IP(key)
3034
+ const jwk = getPrivateJwkNist(this, d)
3043
3035
 
3036
+ const privateKey = await subtle(
3037
+ (c) => c.importKey('jwk', jwk, this.algorithm, extractable, ['deriveBits']),
3038
+ this.name,
3039
+ )
3044
3040
 
3041
+ return privateKey
3042
+ }
3045
3043
 
3046
3044
 
3045
+ async function DeriveKeyPairNist(
3047
3046
 
3047
+ ikm ,
3048
+ extractable ,
3049
+ ) {
3050
+ let sk = 0n
3051
+ let counter = 0
3052
+ let candidate
3053
+ while (sk === 0n || sk >= this.order) {
3054
+ if (counter > 255) {
3055
+ throw new DeriveKeyPairError('Key derivation exceeded maximum iterations')
3056
+ }
3057
+ candidate = await DeriveCandidate_TwoStage(this, this.suite_id, ikm, counter)
3058
+ candidate[0] = candidate[0] & this.bitmask
3059
+ sk = OS2IP(candidate)
3060
+ counter = counter + 1
3061
+ }
3048
3062
 
3063
+ return GetKeyPairNist(this, candidate , extractable, this.name)
3064
+ }
3049
3065
 
3066
+ async function GetKeyPairNist(
3067
+ curveConfig ,
3068
+ sk ,
3069
+ extractable ,
3070
+ name ,
3071
+ ) {
3072
+ const jwk = getPrivateJwkNist(curveConfig, OS2IP(sk))
3050
3073
 
3074
+ const privateKey = await subtle(
3075
+ (c) => c.importKey('jwk', jwk, curveConfig.algorithm, extractable, ['deriveBits']),
3076
+ name,
3077
+ )
3051
3078
 
3079
+ delete jwk.d
3080
+ const publicKey = await subtle(
3081
+ (c) => c.importKey('jwk', jwk, curveConfig.algorithm, true, []),
3082
+ name,
3083
+ )
3052
3084
 
3085
+ return { privateKey, publicKey }
3086
+ }
3053
3087
 
3054
3088
 
3055
3089
 
3056
3090
 
3057
3091
 
3058
3092
 
3093
+ async function DeriveKeyPairX( ikm , extractable ) {
3094
+ const dkp_prk = await LabeledExtract(
3095
+ this.kdf,
3096
+ this.suite_id,
3097
+ new Uint8Array(),
3098
+ encode('dkp_prk'),
3099
+ ikm,
3100
+ )
3101
+ const sk = await LabeledExpand(
3102
+ this.kdf,
3103
+ this.suite_id,
3104
+ dkp_prk,
3105
+ encode('sk'),
3106
+ new Uint8Array(),
3107
+ this.Nsk,
3108
+ )
3109
+ return await createKeyPairFromPrivateKey(this, sk, extractable)
3110
+ }
3059
3111
 
3060
3112
 
3061
3113
 
3062
3114
 
3063
3115
 
3116
+ const P256 = {
3117
+ algorithm: { name: 'ECDH', namedCurve: 'P-256' },
3118
+ Npk: 65,
3119
+ Nsk: 32,
3120
+ order: 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n,
3121
+ bitmask: 0xff,
3122
+ prime: 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffffn,
3123
+ Gx: 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296n,
3124
+ Gy: 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5n,
3125
+ }
3064
3126
 
3065
3127
 
3066
3128
 
@@ -3797,24 +3859,110 @@ function RandomScalarNist(t , seed ) {
3797
3859
  }
3798
3860
 
3799
3861
 
3862
+ async function expandDecapsKeyG(PQTKEM , _seed ) {
3863
+ const Nout = PQTKEM.pq.Nseed + PQTKEM.t.Nseed
3864
+ const bits = Nout << 3
3800
3865
 
3866
+ const algorithm = { name: 'cSHAKE256', length: bits, outputLength: bits }
3867
+ const seed = ab(_seed)
3868
+ const seed_full = await subtle((c) => c.digest(algorithm, seed), PQTKEM.name)
3801
3869
 
3870
+ const [_seed_PQ, seed_T] = split(PQTKEM.pq.Nseed, PQTKEM.t.Nseed, new Uint8Array(seed_full))
3871
+ const seed_PQ = ab(_seed_PQ)
3802
3872
 
3803
3873
 
3874
+ const format = 'raw-seed'
3804
3875
 
3876
+ const usages = ['decapsulateBits', 'encapsulateBits']
3877
+ const dk_PQ = await subtle(
3878
+ (c) => c.importKey(format, seed_PQ, PQTKEM.pq.algorithm, true, [usages[0]]),
3879
+ PQTKEM.name,
3880
+ )
3881
+ const ek_PQ = await getPublicKey(PQTKEM.name, dk_PQ, [usages[1]])
3805
3882
 
3883
+ const sk = PQTKEM.t.RandomScalar?.(seed_T) ?? seed_T
3884
+ const { privateKey: dk_T, publicKey: ek_T } = await PQTKEM.t.GetKeyPair(sk)
3806
3885
 
3886
+ return { ek_PQ, ek_T, dk_PQ, dk_T }
3887
+ }
3807
3888
 
3808
3889
 
3890
+ async function C2PRICombiner(
3891
+ PQTKEM ,
3892
+ ss_PQ ,
3893
+ ss_T ,
3894
+ ct_T ,
3895
+ _ek_T ,
3896
+ label ,
3897
+ ) {
3898
+ const ek_T = new Uint8Array(await subtle((c) => c.exportKey('raw', _ek_T), PQTKEM.name))
3899
+ const data = ab(concat(ss_PQ, ss_T, ct_T, ek_T, label))
3900
+ return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data), PQTKEM.name))
3901
+ }
3809
3902
 
3810
3903
 
3904
+ async function prepareEncapsG(
3905
+ PQTKEM ,
3906
+ ek_PQ ,
3907
+ ek_T ,
3908
+ ) {
3909
+ const res = (await subtle(
3910
+
3911
+ (c) => c.encapsulateBits(PQTKEM.pq.algorithm, ek_PQ),
3912
+ PQTKEM.name,
3913
+ ))
3914
+ const ss_PQ = new Uint8Array(res.sharedKey)
3915
+ const ct_PQ = new Uint8Array(res.ciphertext)
3916
+
3917
+ const { privateKey: sk_E, publicKey } = (await subtle(
3918
+ (c) => c.generateKey(PQTKEM.t.algorithm, true, ['deriveBits']),
3919
+ PQTKEM.name,
3920
+ ))
3921
+ const ct_T = new Uint8Array(await subtle((c) => c.exportKey('raw', publicKey), PQTKEM.name))
3922
+
3923
+ const ss_T = new Uint8Array(
3924
+ await subtle(
3925
+ (c) => c.deriveBits({ name: PQTKEM.t.algorithm.name, public: ek_T }, sk_E, PQTKEM.t.Nss << 3),
3926
+ PQTKEM.name,
3927
+ ),
3928
+ )
3929
+ checkNotAllZeros(ss_T)
3811
3930
 
3931
+ return [ss_PQ, ss_T, ct_PQ, ct_T]
3932
+ }
3812
3933
 
3813
3934
 
3935
+ async function prepareDecapsG(
3936
+ PQTKEM ,
3937
+ dk_PQ ,
3938
+ dk_T ,
3939
+ ct_PQ ,
3940
+ _ct_T ,
3941
+ ) {
3942
+ const ss_PQ = new Uint8Array(
3943
+ await subtle(
3814
3944
 
3945
+ (c) => c.decapsulateBits(PQTKEM.pq.algorithm, dk_PQ, ct_PQ),
3946
+ PQTKEM.name,
3947
+ ),
3948
+ )
3815
3949
 
3950
+ const ct_T = ab(_ct_T)
3951
+ const pub = await subtle(
3952
+ (c) => c.importKey('raw', ct_T, PQTKEM.t.algorithm, true, []),
3953
+ PQTKEM.name,
3954
+ )
3816
3955
 
3956
+ const ss_T = new Uint8Array(
3957
+ await subtle(
3958
+ (c) => c.deriveBits({ name: PQTKEM.t.algorithm.name, public: pub }, dk_T, PQTKEM.t.Nss << 3),
3959
+ PQTKEM.name,
3960
+ ),
3961
+ )
3962
+ checkNotAllZeros(ss_T)
3817
3963
 
3964
+ return [ss_PQ, ss_T]
3965
+ }
3818
3966
 
3819
3967
 
3820
3968
 
@@ -3844,207 +3992,121 @@ function RandomScalarNist(t , seed ) {
3844
3992
 
3845
3993
 
3846
3994
 
3995
+ function PQTKEM_SHARED() {
3996
+ Object.freeze(HybridKey.prototype)
3997
+ return {
3998
+ async DeriveKeyPair( ikm , extractable) {
3999
+ const seed = await LabeledDerive(
4000
+ this.kdf,
4001
+ this.suite_id,
4002
+ ikm,
4003
+ encode('DeriveKeyPair'),
4004
+ new Uint8Array(),
4005
+ 32,
4006
+ )
3847
4007
 
4008
+ const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, seed)
4009
+
4010
+ const publicKey = new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
4011
+ const privateKey = new HybridKey(
4012
+ priv,
4013
+ this.algorithm,
4014
+ 'private',
4015
+ extractable,
4016
+ dk_PQ,
4017
+ dk_T,
4018
+ seed,
4019
+ publicKey,
4020
+ )
3848
4021
 
4022
+ return { privateKey, publicKey }
4023
+ },
4024
+ async GenerateKeyPair( extractable) {
4025
+ return await this.DeriveKeyPair(crypto.getRandomValues(new Uint8Array(32)), extractable)
4026
+ },
4027
+ async SerializePublicKey( key) {
4028
+ assertKeyAlgorithm(key, this.algorithm)
4029
+ HybridKey.validate(key, true)
3849
4030
 
4031
+ const format = 'raw-public'
4032
+ const ek_PQ = new Uint8Array(
4033
+ await subtle((c) => c.exportKey(format, key.getPq(priv)), this.name),
4034
+ )
4035
+ const ek_T = new Uint8Array(
4036
+ await subtle((c) => c.exportKey('raw', key.getT(priv)), this.name),
4037
+ )
3850
4038
 
4039
+ return concat(ek_PQ, ek_T)
4040
+ },
4041
+ async DeserializePublicKey( key) {
3851
4042
 
4043
+ const format = 'raw-public'
3852
4044
 
4045
+ const usages = ['encapsulateBits']
4046
+ const pubPq = ab(key.subarray(0, this.pq.Npk))
4047
+ const pubT = ab(key.subarray(this.pq.Npk))
4048
+ const [ek_PQ, ek_T] = await Promise.all([
4049
+ subtle((c) => c.importKey(format, pubPq, this.pq.algorithm, true, usages), this.name),
4050
+ subtle((c) => c.importKey('raw', pubT, this.t.algorithm, true, []), this.name),
4051
+ ])
4052
+
4053
+ return new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
4054
+ },
4055
+ async SerializePrivateKey( key) {
4056
+ assertKeyAlgorithm(key, this.algorithm)
4057
+ HybridKey.validate(key, true)
3853
4058
 
4059
+ return key.getSeed(priv)
4060
+ },
4061
+ async DeserializePrivateKey( key, extractable) {
4062
+ const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, key)
4063
+ const publicKey = new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
4064
+ const privateKey = new HybridKey(
4065
+ priv,
4066
+ this.algorithm,
4067
+ 'private',
4068
+ extractable,
4069
+ dk_PQ,
4070
+ dk_T,
4071
+ slice(key),
4072
+ publicKey,
4073
+ )
3854
4074
 
4075
+ return privateKey
4076
+ },
4077
+ async Encap( pkR) {
4078
+ assertKeyAlgorithm(pkR, this.algorithm)
4079
+ HybridKey.validate(pkR)
3855
4080
 
4081
+ const ek_PQ = pkR.getPq(priv)
4082
+ const ek_T = pkR.getT(priv)
4083
+ const [ss_PQ, ss_T, ct_PQ, ct_T] = await prepareEncapsG(this, ek_PQ, ek_T)
4084
+ const ss_H = await C2PRICombiner(this, ss_PQ, ss_T, ct_T, ek_T, this.label)
4085
+ const ct_H = concat(ct_PQ, ct_T)
3856
4086
 
4087
+ return { shared_secret: ss_H, enc: ct_H }
4088
+ },
4089
+ async Decap( enc, skR, pkR) {
4090
+ assertKeyAlgorithm(skR, this.algorithm)
4091
+ HybridKey.validate(skR)
3857
4092
 
4093
+ if (pkR) {
4094
+ assertKeyAlgorithm(pkR, this.algorithm)
4095
+ HybridKey.validate(pkR)
4096
+ }
3858
4097
 
4098
+ const [ct_PQ, ct_T] = split(this.pq.Nct, this.t.Nct, enc)
4099
+ const ek = pkR ?? skR.getPublicKey(priv)
4100
+ const ek_T = ek.getT(priv)
4101
+ const dk_PQ = skR.getPq(priv)
4102
+ const dk_T = skR.getT(priv)
4103
+ const [ss_PQ, ss_T] = await prepareDecapsG(this, dk_PQ, dk_T, ct_PQ, ct_T)
4104
+ const ss_H = await C2PRICombiner(this, ss_PQ, ss_T, ct_T, ek_T, this.label)
3859
4105
 
3860
-
3861
-
3862
-
3863
-
3864
-
3865
-
3866
-
3867
-
3868
-
3869
-
3870
-
3871
-
3872
-
3873
-
3874
-
3875
-
3876
-
3877
-
3878
-
3879
-
3880
-
3881
-
3882
-
3883
-
3884
-
3885
-
3886
-
3887
-
3888
-
3889
-
3890
-
3891
-
3892
-
3893
-
3894
-
3895
-
3896
-
3897
-
3898
-
3899
-
3900
-
3901
-
3902
-
3903
-
3904
-
3905
-
3906
-
3907
-
3908
-
3909
-
3910
-
3911
-
3912
-
3913
-
3914
-
3915
-
3916
-
3917
-
3918
-
3919
-
3920
-
3921
-
3922
-
3923
-
3924
-
3925
-
3926
-
3927
-
3928
-
3929
-
3930
-
3931
-
3932
-
3933
-
3934
-
3935
-
3936
-
3937
-
3938
-
3939
-
3940
-
3941
-
3942
-
3943
-
3944
-
3945
-
3946
-
3947
-
3948
-
3949
-
3950
-
3951
-
3952
-
3953
-
3954
-
3955
-
3956
-
3957
-
3958
-
3959
-
3960
-
3961
-
3962
-
3963
-
3964
-
3965
-
3966
-
3967
-
3968
-
3969
-
3970
-
3971
-
3972
-
3973
-
3974
-
3975
-
3976
-
3977
-
3978
-
3979
-
3980
-
3981
-
3982
-
3983
-
3984
-
3985
-
3986
-
3987
-
3988
-
3989
-
3990
-
3991
-
3992
-
3993
-
3994
-
3995
-
3996
-
3997
-
3998
-
3999
-
4000
-
4001
-
4002
-
4003
-
4004
-
4005
-
4006
-
4007
-
4008
-
4009
-
4010
-
4011
-
4012
-
4013
-
4014
-
4015
-
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
-
4045
-
4046
-
4047
-
4106
+ return ss_H
4107
+ },
4108
+ }
4109
+ }
4048
4110
 
4049
4111
 
4050
4112