hpke 1.0.5 → 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 +695 -695
- package/package.json +5 -5
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
|
|
|
@@ -1514,132 +1514,132 @@ export async function LabeledDerive(
|
|
|
1514
1514
|
}
|
|
1515
1515
|
|
|
1516
1516
|
|
|
1517
|
+
async function Export_OneStage(
|
|
1518
|
+
KDF ,
|
|
1519
|
+
suite_id ,
|
|
1520
|
+
exporter_secret ,
|
|
1521
|
+
exporter_context ,
|
|
1522
|
+
L ,
|
|
1523
|
+
) {
|
|
1524
|
+
checkLength(exporter_context, 'Exporter context', MAX_LENGTH_ONE_STAGE)
|
|
1525
|
+
return await LabeledDerive(KDF, suite_id, exporter_secret, encode('sec'), exporter_context, L)
|
|
1526
|
+
}
|
|
1517
1527
|
|
|
1518
1528
|
|
|
1529
|
+
async function CombineSecrets_OneStage(
|
|
1530
|
+
suite ,
|
|
1531
|
+
mode ,
|
|
1532
|
+
shared_secret ,
|
|
1533
|
+
info ,
|
|
1534
|
+
psk ,
|
|
1535
|
+
psk_id ,
|
|
1536
|
+
) {
|
|
1537
|
+
checkLength(psk, 'PSK', MAX_LENGTH_ONE_STAGE)
|
|
1538
|
+
checkLength(psk_id, 'PSK ID', MAX_LENGTH_ONE_STAGE)
|
|
1539
|
+
checkLength(info, 'Info', MAX_LENGTH_ONE_STAGE)
|
|
1540
|
+
|
|
1541
|
+
const secrets = concat(lengthPrefixed(psk), lengthPrefixed(shared_secret))
|
|
1542
|
+
const context = concat(I2OSP(mode, 1), lengthPrefixed(psk_id), lengthPrefixed(info))
|
|
1543
|
+
|
|
1544
|
+
const secret = await LabeledDerive(
|
|
1545
|
+
suite.KDF,
|
|
1546
|
+
suite.id,
|
|
1547
|
+
secrets,
|
|
1548
|
+
encode('secret'),
|
|
1549
|
+
context,
|
|
1550
|
+
suite.AEAD.Nk + suite.AEAD.Nn + suite.KDF.Nh,
|
|
1551
|
+
)
|
|
1519
1552
|
|
|
1553
|
+
const key = slice(secret, 0, suite.AEAD.Nk)
|
|
1554
|
+
const base_nonce = slice(secret, suite.AEAD.Nk, suite.AEAD.Nk + suite.AEAD.Nn)
|
|
1555
|
+
const exporter_secret = slice(secret, suite.AEAD.Nk + suite.AEAD.Nn)
|
|
1520
1556
|
|
|
1557
|
+
return { key, base_nonce, exporter_secret }
|
|
1558
|
+
}
|
|
1521
1559
|
|
|
1522
1560
|
|
|
1523
1561
|
|
|
1562
|
+
const MAX_LENGTH_TWO_STAGE = 0xffff
|
|
1524
1563
|
|
|
1564
|
+
const MAX_LENGTH_ONE_STAGE = 0xffff
|
|
1525
1565
|
|
|
1566
|
+
function checkLength(data , name , maxLength ) {
|
|
1567
|
+
if (data.byteLength > maxLength) {
|
|
1568
|
+
throw new TypeError(`${name} length must not exceed ${maxLength} bytes`)
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
function checkUint8Array(input , name ) {
|
|
1572
|
+
if (!(input instanceof Uint8Array)) {
|
|
1573
|
+
throw new TypeError(`"${name}" must be Uint8Array`)
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
function checkExtractable(extractable ) {
|
|
1577
|
+
if (typeof extractable !== 'boolean') {
|
|
1578
|
+
throw new TypeError('"extractable" must be boolean')
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1526
1581
|
|
|
1527
1582
|
|
|
1583
|
+
async function CombineSecrets_TwoStage(
|
|
1584
|
+
suite ,
|
|
1585
|
+
mode ,
|
|
1586
|
+
shared_secret ,
|
|
1587
|
+
info ,
|
|
1588
|
+
psk ,
|
|
1589
|
+
psk_id ,
|
|
1590
|
+
) {
|
|
1591
|
+
checkLength(psk, 'PSK', MAX_LENGTH_TWO_STAGE)
|
|
1592
|
+
checkLength(psk_id, 'PSK ID', MAX_LENGTH_TWO_STAGE)
|
|
1593
|
+
checkLength(info, 'Info', MAX_LENGTH_TWO_STAGE)
|
|
1594
|
+
|
|
1595
|
+
const [psk_id_hash, info_hash] = await Promise.all([
|
|
1596
|
+
LabeledExtract(suite.KDF, suite.id, new Uint8Array(), encode('psk_id_hash'), psk_id),
|
|
1597
|
+
LabeledExtract(suite.KDF, suite.id, new Uint8Array(), encode('info_hash'), info),
|
|
1598
|
+
])
|
|
1599
|
+
|
|
1600
|
+
const key_schedule_context = concat(I2OSP(mode, 1), psk_id_hash, info_hash)
|
|
1601
|
+
const secret = await LabeledExtract(suite.KDF, suite.id, shared_secret, encode('secret'), psk)
|
|
1602
|
+
|
|
1603
|
+
|
|
1604
|
+
if (suite.AEAD.id === EXPORT_ONLY) {
|
|
1605
|
+
const exporter_secret = await LabeledExpand(
|
|
1606
|
+
suite.KDF,
|
|
1607
|
+
suite.id,
|
|
1608
|
+
secret,
|
|
1609
|
+
encode('exp'),
|
|
1610
|
+
key_schedule_context,
|
|
1611
|
+
suite.KDF.Nh,
|
|
1612
|
+
)
|
|
1613
|
+
return { key: new Uint8Array(), base_nonce: new Uint8Array(), exporter_secret }
|
|
1614
|
+
}
|
|
1528
1615
|
|
|
1616
|
+
const [key, base_nonce, exporter_secret] = await Promise.all([
|
|
1617
|
+
LabeledExpand(suite.KDF, suite.id, secret, encode('key'), key_schedule_context, suite.AEAD.Nk),
|
|
1618
|
+
LabeledExpand(
|
|
1619
|
+
suite.KDF,
|
|
1620
|
+
suite.id,
|
|
1621
|
+
secret,
|
|
1622
|
+
encode('base_nonce'),
|
|
1623
|
+
key_schedule_context,
|
|
1624
|
+
suite.AEAD.Nn,
|
|
1625
|
+
),
|
|
1626
|
+
LabeledExpand(suite.KDF, suite.id, secret, encode('exp'), key_schedule_context, suite.KDF.Nh),
|
|
1627
|
+
])
|
|
1628
|
+
|
|
1629
|
+
return { key, base_nonce, exporter_secret }
|
|
1630
|
+
}
|
|
1529
1631
|
|
|
1530
1632
|
|
|
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
|
-
|
|
1633
|
+
async function Export_TwoStage(
|
|
1634
|
+
KDF ,
|
|
1635
|
+
suite_id ,
|
|
1636
|
+
exporter_secret ,
|
|
1637
|
+
exporter_context ,
|
|
1638
|
+
L ,
|
|
1639
|
+
) {
|
|
1640
|
+
checkLength(exporter_context, 'Exporter context', MAX_LENGTH_TWO_STAGE)
|
|
1641
|
+
return await LabeledExpand(KDF, suite_id, exporter_secret, encode('sec'), exporter_context, L)
|
|
1642
|
+
}
|
|
1643
1643
|
|
|
1644
1644
|
|
|
1645
1645
|
|
|
@@ -2211,41 +2211,41 @@ function KDFStages(KDF ) {
|
|
|
2211
2211
|
}
|
|
2212
2212
|
|
|
2213
2213
|
|
|
2214
|
+
async function KeySchedule(
|
|
2215
|
+
suite ,
|
|
2216
|
+
mode ,
|
|
2217
|
+
shared_secret ,
|
|
2218
|
+
info ,
|
|
2219
|
+
psk ,
|
|
2220
|
+
pskId ,
|
|
2221
|
+
) {
|
|
2222
|
+
info ??= new Uint8Array()
|
|
2223
|
+
checkUint8Array(info, 'info')
|
|
2224
|
+
psk ??= new Uint8Array()
|
|
2225
|
+
checkUint8Array(psk, 'psk')
|
|
2226
|
+
pskId ??= new Uint8Array()
|
|
2227
|
+
checkUint8Array(pskId, 'pskId')
|
|
2228
|
+
|
|
2229
|
+
const stages = KDFStages(suite.KDF)
|
|
2230
|
+
const CombineSecrets = stages === 1 ? CombineSecrets_OneStage : CombineSecrets_TwoStage
|
|
2231
|
+
|
|
2232
|
+
VerifyPSKInputs(psk, pskId)
|
|
2233
|
+
return await CombineSecrets(suite, mode, shared_secret, info, psk, pskId)
|
|
2234
|
+
}
|
|
2214
2235
|
|
|
2215
2236
|
|
|
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
|
-
|
|
2237
|
+
function VerifyPSKInputs(psk , psk_id ) {
|
|
2238
|
+
if (psk.byteLength && psk_id.byteLength) {
|
|
2239
|
+
if (psk.byteLength < 32) {
|
|
2240
|
+
throw new TypeError('Insufficient PSK length')
|
|
2241
|
+
}
|
|
2242
|
+
return
|
|
2243
|
+
}
|
|
2244
|
+
if (!psk.byteLength && !psk_id.byteLength) {
|
|
2245
|
+
return
|
|
2246
|
+
}
|
|
2247
|
+
throw new TypeError('Inconsistent PSK inputs')
|
|
2248
|
+
}
|
|
2249
2249
|
|
|
2250
2250
|
|
|
2251
2251
|
const NotApplicable = () => {
|
|
@@ -2626,37 +2626,215 @@ function b64u(input ) {
|
|
|
2626
2626
|
|
|
2627
2627
|
|
|
2628
2628
|
|
|
2629
|
+
async function DeriveCandidate_TwoStage(
|
|
2630
|
+
DHKEM ,
|
|
2631
|
+
suite_id ,
|
|
2632
|
+
ikm ,
|
|
2633
|
+
counter ,
|
|
2634
|
+
) {
|
|
2635
|
+
const dkp_prk = await LabeledExtract(
|
|
2636
|
+
DHKEM.kdf,
|
|
2637
|
+
suite_id,
|
|
2638
|
+
new Uint8Array(),
|
|
2639
|
+
encode('dkp_prk'),
|
|
2640
|
+
ikm,
|
|
2641
|
+
)
|
|
2642
|
+
return await LabeledExpand(
|
|
2643
|
+
DHKEM.kdf,
|
|
2644
|
+
suite_id,
|
|
2645
|
+
dkp_prk,
|
|
2646
|
+
encode('candidate'),
|
|
2647
|
+
I2OSP(counter, 1),
|
|
2648
|
+
DHKEM.Nsk,
|
|
2649
|
+
)
|
|
2650
|
+
}
|
|
2629
2651
|
|
|
2652
|
+
function OS2IP(x ) {
|
|
2653
|
+
let result = 0n
|
|
2654
|
+
for (let i = 0; i < x.byteLength; i++) {
|
|
2655
|
+
result = result * 256n + BigInt(x[i] )
|
|
2656
|
+
}
|
|
2657
|
+
return result
|
|
2658
|
+
}
|
|
2630
2659
|
|
|
2660
|
+
function bigIntToUint8Array(value , byteLength ) {
|
|
2661
|
+
const result = new Uint8Array(byteLength)
|
|
2662
|
+
let n = value
|
|
2631
2663
|
|
|
2664
|
+
for (let i = byteLength - 1; i >= 0; i--) {
|
|
2665
|
+
result[i] = Number(n & 0xffn)
|
|
2666
|
+
n = n >> 8n
|
|
2667
|
+
}
|
|
2632
2668
|
|
|
2669
|
+
return result
|
|
2670
|
+
}
|
|
2633
2671
|
|
|
2672
|
+
function assertKeyAlgorithm(key , expectedAlgorithm ) {
|
|
2673
|
+
if (key.algorithm.name !== expectedAlgorithm.name) {
|
|
2674
|
+
throw new TypeError(`key algorithm must be ${expectedAlgorithm.name}`)
|
|
2675
|
+
}
|
|
2676
|
+
if (
|
|
2677
|
+
(key.algorithm ).namedCurve !==
|
|
2678
|
+
(expectedAlgorithm ).namedCurve
|
|
2679
|
+
) {
|
|
2680
|
+
throw new TypeError(
|
|
2681
|
+
`key namedCurve must be ${(expectedAlgorithm ).namedCurve}`,
|
|
2682
|
+
)
|
|
2683
|
+
}
|
|
2684
|
+
}
|
|
2634
2685
|
|
|
2686
|
+
function assertCryptoKey(key ) {
|
|
2635
2687
|
|
|
2688
|
+
if (key[Symbol.toStringTag] !== 'CryptoKey') {
|
|
2689
|
+
if (key instanceof CryptoKey) return
|
|
2690
|
+
throw new TypeError('unexpected key constructor')
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2636
2693
|
|
|
2637
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
|
+
}
|
|
2638
2716
|
|
|
2639
2717
|
|
|
2640
2718
|
|
|
2641
2719
|
|
|
2642
2720
|
|
|
2721
|
+
function DHKEM_SHARED() {
|
|
2722
|
+
return {
|
|
2723
|
+
async GenerateKeyPair( extractable) {
|
|
2724
|
+
return (await subtle(
|
|
2725
|
+
(c) => c.generateKey(this.algorithm, extractable, ['deriveBits']),
|
|
2726
|
+
this.name,
|
|
2727
|
+
))
|
|
2728
|
+
},
|
|
2729
|
+
async SerializePublicKey( key) {
|
|
2730
|
+
assertKeyAlgorithm(key, this.algorithm)
|
|
2731
|
+
assertCryptoKey(key)
|
|
2732
|
+
return new Uint8Array(await subtle((c) => c.exportKey('raw', key), this.name))
|
|
2733
|
+
},
|
|
2734
|
+
async DeserializePublicKey( _key) {
|
|
2735
|
+
const key = ab(_key)
|
|
2736
|
+
return await subtle((c) => c.importKey('raw', key, this.algorithm, true, []), this.name)
|
|
2737
|
+
},
|
|
2738
|
+
async SerializePrivateKey( key) {
|
|
2739
|
+
assertKeyAlgorithm(key, this.algorithm)
|
|
2740
|
+
assertCryptoKey(key)
|
|
2741
|
+
const { d } = await subtle((c) => c.exportKey('jwk', key), this.name)
|
|
2742
|
+
return b64u(d )
|
|
2743
|
+
},
|
|
2744
|
+
async Encap( pkR) {
|
|
2745
|
+
assertKeyAlgorithm(pkR, this.algorithm)
|
|
2746
|
+
assertCryptoKey(pkR)
|
|
2643
2747
|
|
|
2748
|
+
const ekp = (await this.GenerateKeyPair(false))
|
|
2749
|
+
const skE = ekp.privateKey
|
|
2750
|
+
const pkE = ekp.publicKey
|
|
2644
2751
|
|
|
2645
2752
|
|
|
2646
2753
|
|
|
2754
|
+
const dh = new Uint8Array(
|
|
2755
|
+
await subtle(
|
|
2756
|
+
(c) => c.deriveBits({ name: skE.algorithm.name, public: pkR }, skE, this.Ndh << 3),
|
|
2757
|
+
this.name,
|
|
2758
|
+
),
|
|
2759
|
+
)
|
|
2760
|
+
checkNotAllZeros(dh)
|
|
2647
2761
|
|
|
2762
|
+
const enc = await this.SerializePublicKey(pkE)
|
|
2763
|
+
const pkRm = await this.SerializePublicKey(pkR)
|
|
2764
|
+
const kem_context = concat(enc, pkRm)
|
|
2765
|
+
const shared_secret = await ExtractAndExpand_TwoStage(this, dh, kem_context)
|
|
2766
|
+
return { shared_secret, enc }
|
|
2767
|
+
},
|
|
2768
|
+
async Decap( enc, skR, pkR) {
|
|
2769
|
+
assertKeyAlgorithm(skR, this.algorithm)
|
|
2770
|
+
assertCryptoKey(skR)
|
|
2771
|
+
if (pkR) {
|
|
2772
|
+
assertKeyAlgorithm(pkR, this.algorithm)
|
|
2773
|
+
assertCryptoKey(pkR)
|
|
2774
|
+
} else {
|
|
2775
|
+
pkR = await getPublicKey(this.name, skR, [])
|
|
2776
|
+
}
|
|
2648
2777
|
|
|
2778
|
+
const pkE = (await this.DeserializePublicKey(enc))
|
|
2649
2779
|
|
|
2650
2780
|
|
|
2651
2781
|
|
|
2782
|
+
const dh = new Uint8Array(
|
|
2783
|
+
await subtle(
|
|
2784
|
+
(c) => c.deriveBits({ name: skR.algorithm.name, public: pkE }, skR, this.Ndh << 3),
|
|
2785
|
+
this.name,
|
|
2786
|
+
),
|
|
2787
|
+
)
|
|
2788
|
+
checkNotAllZeros(dh)
|
|
2652
2789
|
|
|
2790
|
+
const pkRm = await this.SerializePublicKey(pkR)
|
|
2791
|
+
const kem_context = concat(enc, pkRm)
|
|
2792
|
+
const shared_secret = await ExtractAndExpand_TwoStage(this, dh, kem_context)
|
|
2793
|
+
return shared_secret
|
|
2794
|
+
},
|
|
2795
|
+
}
|
|
2796
|
+
}
|
|
2653
2797
|
|
|
2654
2798
|
|
|
2655
2799
|
|
|
2656
2800
|
|
|
2657
2801
|
|
|
2802
|
+
async function createKeyPairFromPrivateKey(
|
|
2803
|
+
DHKEM ,
|
|
2804
|
+
key ,
|
|
2805
|
+
extractable ,
|
|
2806
|
+
) {
|
|
2807
|
+
let privateKey
|
|
2808
|
+
let publicKey
|
|
2658
2809
|
|
|
2810
|
+
if (!extractable && typeof crypto.subtle.getPublicKey !== 'function') {
|
|
2811
|
+
privateKey = (await DHKEM.DeserializePrivateKey(key, true))
|
|
2812
|
+
publicKey = await getPublicKey(DHKEM.name, privateKey, [])
|
|
2813
|
+
privateKey = (await DHKEM.DeserializePrivateKey(key, false))
|
|
2814
|
+
} else {
|
|
2815
|
+
privateKey = (await DHKEM.DeserializePrivateKey(key, extractable))
|
|
2816
|
+
publicKey = await getPublicKey(DHKEM.name, privateKey, [])
|
|
2817
|
+
}
|
|
2818
|
+
return { privateKey, publicKey }
|
|
2819
|
+
}
|
|
2659
2820
|
|
|
2821
|
+
async function CurveKeyFromD(
|
|
2822
|
+
name ,
|
|
2823
|
+
Nsk ,
|
|
2824
|
+
template ,
|
|
2825
|
+
algorithm ,
|
|
2826
|
+
key ,
|
|
2827
|
+
extractable ,
|
|
2828
|
+
) {
|
|
2829
|
+
const tmpl = slice(template)
|
|
2830
|
+
const pkcs8 = new Uint8Array(Nsk + tmpl.byteLength)
|
|
2831
|
+
pkcs8.set(tmpl)
|
|
2832
|
+
pkcs8.set(key, tmpl.byteLength)
|
|
2833
|
+
return await subtle(
|
|
2834
|
+
(c) => c.importKey('pkcs8', pkcs8, algorithm, extractable, ['deriveBits']),
|
|
2835
|
+
name,
|
|
2836
|
+
)
|
|
2837
|
+
}
|
|
2660
2838
|
|
|
2661
2839
|
|
|
2662
2840
|
|
|
@@ -2668,35 +2846,90 @@ function b64u(input ) {
|
|
|
2668
2846
|
|
|
2669
2847
|
|
|
2670
2848
|
|
|
2849
|
+
function modInverse(a , m ) {
|
|
2850
|
+
a = ((a % m) + m) % m
|
|
2851
|
+
let [t, newT] = [0n, 1n]
|
|
2852
|
+
let [r, newR] = [m, a]
|
|
2671
2853
|
|
|
2854
|
+
while (newR !== 0n) {
|
|
2855
|
+
const quotient = r / newR
|
|
2856
|
+
;[t, newT] = [newT, t - quotient * newT]
|
|
2857
|
+
;[r, newR] = [newR, r - quotient * newR]
|
|
2858
|
+
}
|
|
2672
2859
|
|
|
2860
|
+
if (r > 1n) throw new Error('a is not invertible')
|
|
2861
|
+
if (t < 0n) t = t + m
|
|
2862
|
+
return t
|
|
2863
|
+
}
|
|
2673
2864
|
|
|
2674
2865
|
|
|
2866
|
+
function pointDouble(p , prime , a ) {
|
|
2867
|
+
const { x, y } = p
|
|
2675
2868
|
|
|
2676
2869
|
|
|
2870
|
+
const numerator = (((3n * x * x) % prime) + a) % prime
|
|
2871
|
+
const denominator = (2n * y) % prime
|
|
2872
|
+
const s = (numerator * modInverse(denominator, prime)) % prime
|
|
2677
2873
|
|
|
2678
2874
|
|
|
2875
|
+
const x3 = (((s * s) % prime) - ((2n * x) % prime) + prime) % prime
|
|
2679
2876
|
|
|
2680
2877
|
|
|
2878
|
+
const y3 = (((s * ((x - x3 + prime) % prime)) % prime) - y + prime) % prime
|
|
2681
2879
|
|
|
2880
|
+
return { x: x3, y: y3 }
|
|
2881
|
+
}
|
|
2682
2882
|
|
|
2683
2883
|
|
|
2884
|
+
function pointAdd(p , q , prime , a ) {
|
|
2885
|
+
if (p.x === q.x && p.y === q.y) {
|
|
2886
|
+
return pointDouble(p, prime, a)
|
|
2887
|
+
}
|
|
2684
2888
|
|
|
2889
|
+
const { x: x1, y: y1 } = p
|
|
2890
|
+
const { x: x2, y: y2 } = q
|
|
2685
2891
|
|
|
2686
2892
|
|
|
2687
2893
|
|
|
2894
|
+
if (p.x === q.x) {
|
|
2895
|
+
throw new Error('Point addition resulted in point at infinity')
|
|
2896
|
+
}
|
|
2688
2897
|
|
|
2689
2898
|
|
|
2899
|
+
const numerator = (((y2 - y1) % prime) + prime) % prime
|
|
2900
|
+
const denominator = (((x2 - x1) % prime) + prime) % prime
|
|
2901
|
+
const s = (numerator * modInverse(denominator, prime)) % prime
|
|
2690
2902
|
|
|
2691
2903
|
|
|
2904
|
+
const x3 = (((s * s) % prime) - x1 - x2 + prime + prime) % prime
|
|
2692
2905
|
|
|
2693
2906
|
|
|
2907
|
+
const y3 = (((s * ((x1 - x3 + prime) % prime)) % prime) - y1 + prime) % prime
|
|
2694
2908
|
|
|
2909
|
+
return { x: x3, y: y3 }
|
|
2910
|
+
}
|
|
2695
2911
|
|
|
2696
2912
|
|
|
2913
|
+
function scalarMult(k , G , prime , a , order ) {
|
|
2914
|
+
if (k === 0n || k >= order) {
|
|
2915
|
+
throw new Error('Invalid scalar')
|
|
2916
|
+
}
|
|
2697
2917
|
|
|
2918
|
+
let result = null
|
|
2919
|
+
let addend = G
|
|
2920
|
+
let scalar = k
|
|
2698
2921
|
|
|
2922
|
+
while (scalar > 0n) {
|
|
2923
|
+
if (scalar & 1n) {
|
|
2924
|
+
result = result === null ? addend : pointAdd(result, addend, prime, a)
|
|
2925
|
+
}
|
|
2926
|
+
addend = pointDouble(addend, prime, a)
|
|
2927
|
+
scalar = scalar >> 1n
|
|
2928
|
+
}
|
|
2699
2929
|
|
|
2930
|
+
if (result === null) throw new Error('Invalid result')
|
|
2931
|
+
return result
|
|
2932
|
+
}
|
|
2700
2933
|
|
|
2701
2934
|
|
|
2702
2935
|
|
|
@@ -2710,357 +2943,124 @@ function b64u(input ) {
|
|
|
2710
2943
|
|
|
2711
2944
|
|
|
2712
2945
|
|
|
2946
|
+
function getPrivateJwkNist(DHKEM , d ) {
|
|
2713
2947
|
|
|
2948
|
+
const G = { x: DHKEM.Gx, y: DHKEM.Gy }
|
|
2949
|
+
const publicPoint = scalarMult(d, G, DHKEM.prime, DHKEM.prime - 3n, DHKEM.order)
|
|
2714
2950
|
|
|
2951
|
+
const coordSize = (DHKEM.Npk - 1) / 2
|
|
2952
|
+
const xBytes = bigIntToUint8Array(publicPoint.x, coordSize)
|
|
2953
|
+
const yBytes = bigIntToUint8Array(publicPoint.y, coordSize)
|
|
2954
|
+
const dBytes = bigIntToUint8Array(d, DHKEM.Nsk)
|
|
2715
2955
|
|
|
2716
2956
|
|
|
2957
|
+
return {
|
|
2958
|
+
kty: 'EC',
|
|
2959
|
+
crv: DHKEM.algorithm.namedCurve,
|
|
2960
|
+
x: toB64u(xBytes),
|
|
2961
|
+
y: toB64u(yBytes),
|
|
2962
|
+
d: toB64u(dBytes),
|
|
2963
|
+
}
|
|
2964
|
+
}
|
|
2717
2965
|
|
|
2966
|
+
async function DeserializePrivateKeyNist(
|
|
2718
2967
|
|
|
2968
|
+
key ,
|
|
2969
|
+
extractable ,
|
|
2970
|
+
) {
|
|
2971
|
+
const d = OS2IP(key)
|
|
2972
|
+
const jwk = getPrivateJwkNist(this, d)
|
|
2719
2973
|
|
|
2974
|
+
const privateKey = await subtle(
|
|
2975
|
+
(c) => c.importKey('jwk', jwk, this.algorithm, extractable, ['deriveBits']),
|
|
2976
|
+
this.name,
|
|
2977
|
+
)
|
|
2720
2978
|
|
|
2979
|
+
return privateKey
|
|
2980
|
+
}
|
|
2721
2981
|
|
|
2722
2982
|
|
|
2983
|
+
async function DeriveKeyPairNist(
|
|
2723
2984
|
|
|
2985
|
+
ikm ,
|
|
2986
|
+
extractable ,
|
|
2987
|
+
) {
|
|
2988
|
+
let sk = 0n
|
|
2989
|
+
let counter = 0
|
|
2990
|
+
let candidate
|
|
2991
|
+
while (sk === 0n || sk >= this.order) {
|
|
2992
|
+
if (counter > 255) {
|
|
2993
|
+
throw new DeriveKeyPairError('Key derivation exceeded maximum iterations')
|
|
2994
|
+
}
|
|
2995
|
+
candidate = await DeriveCandidate_TwoStage(this, this.suite_id, ikm, counter)
|
|
2996
|
+
candidate[0] = candidate[0] & this.bitmask
|
|
2997
|
+
sk = OS2IP(candidate)
|
|
2998
|
+
counter = counter + 1
|
|
2999
|
+
}
|
|
2724
3000
|
|
|
3001
|
+
return GetKeyPairNist(this, candidate , extractable, this.name)
|
|
3002
|
+
}
|
|
2725
3003
|
|
|
3004
|
+
async function GetKeyPairNist(
|
|
3005
|
+
curveConfig ,
|
|
3006
|
+
sk ,
|
|
3007
|
+
extractable ,
|
|
3008
|
+
name ,
|
|
3009
|
+
) {
|
|
3010
|
+
const jwk = getPrivateJwkNist(curveConfig, OS2IP(sk))
|
|
2726
3011
|
|
|
3012
|
+
const privateKey = await subtle(
|
|
3013
|
+
(c) => c.importKey('jwk', jwk, curveConfig.algorithm, extractable, ['deriveBits']),
|
|
3014
|
+
name,
|
|
3015
|
+
)
|
|
2727
3016
|
|
|
3017
|
+
delete jwk.d
|
|
3018
|
+
const publicKey = await subtle(
|
|
3019
|
+
(c) => c.importKey('jwk', jwk, curveConfig.algorithm, true, []),
|
|
3020
|
+
name,
|
|
3021
|
+
)
|
|
2728
3022
|
|
|
3023
|
+
return { privateKey, publicKey }
|
|
3024
|
+
}
|
|
2729
3025
|
|
|
2730
3026
|
|
|
2731
3027
|
|
|
2732
3028
|
|
|
2733
3029
|
|
|
2734
3030
|
|
|
3031
|
+
async function DeriveKeyPairX( ikm , extractable ) {
|
|
3032
|
+
const dkp_prk = await LabeledExtract(
|
|
3033
|
+
this.kdf,
|
|
3034
|
+
this.suite_id,
|
|
3035
|
+
new Uint8Array(),
|
|
3036
|
+
encode('dkp_prk'),
|
|
3037
|
+
ikm,
|
|
3038
|
+
)
|
|
3039
|
+
const sk = await LabeledExpand(
|
|
3040
|
+
this.kdf,
|
|
3041
|
+
this.suite_id,
|
|
3042
|
+
dkp_prk,
|
|
3043
|
+
encode('sk'),
|
|
3044
|
+
new Uint8Array(),
|
|
3045
|
+
this.Nsk,
|
|
3046
|
+
)
|
|
3047
|
+
return await createKeyPairFromPrivateKey(this, sk, extractable)
|
|
3048
|
+
}
|
|
2735
3049
|
|
|
2736
3050
|
|
|
2737
3051
|
|
|
2738
3052
|
|
|
2739
3053
|
|
|
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
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3054
|
+
const P256 = {
|
|
3055
|
+
algorithm: { name: 'ECDH', namedCurve: 'P-256' },
|
|
3056
|
+
Npk: 65,
|
|
3057
|
+
Nsk: 32,
|
|
3058
|
+
order: 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n,
|
|
3059
|
+
bitmask: 0xff,
|
|
3060
|
+
prime: 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffffn,
|
|
3061
|
+
Gx: 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296n,
|
|
3062
|
+
Gy: 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5n,
|
|
3063
|
+
}
|
|
3064
3064
|
|
|
3065
3065
|
|
|
3066
3066
|
|
|
@@ -3797,24 +3797,110 @@ function RandomScalarNist(t , seed ) {
|
|
|
3797
3797
|
}
|
|
3798
3798
|
|
|
3799
3799
|
|
|
3800
|
+
async function expandDecapsKeyG(PQTKEM , _seed ) {
|
|
3801
|
+
const Nout = PQTKEM.pq.Nseed + PQTKEM.t.Nseed
|
|
3802
|
+
const bits = Nout << 3
|
|
3800
3803
|
|
|
3804
|
+
const algorithm = { name: 'cSHAKE256', length: bits, outputLength: bits }
|
|
3805
|
+
const seed = ab(_seed)
|
|
3806
|
+
const seed_full = await subtle((c) => c.digest(algorithm, seed), PQTKEM.name)
|
|
3801
3807
|
|
|
3808
|
+
const [_seed_PQ, seed_T] = split(PQTKEM.pq.Nseed, PQTKEM.t.Nseed, new Uint8Array(seed_full))
|
|
3809
|
+
const seed_PQ = ab(_seed_PQ)
|
|
3802
3810
|
|
|
3803
3811
|
|
|
3812
|
+
const format = 'raw-seed'
|
|
3804
3813
|
|
|
3814
|
+
const usages = ['decapsulateBits', 'encapsulateBits']
|
|
3815
|
+
const dk_PQ = await subtle(
|
|
3816
|
+
(c) => c.importKey(format, seed_PQ, PQTKEM.pq.algorithm, true, [usages[0]]),
|
|
3817
|
+
PQTKEM.name,
|
|
3818
|
+
)
|
|
3819
|
+
const ek_PQ = await getPublicKey(PQTKEM.name, dk_PQ, [usages[1]])
|
|
3805
3820
|
|
|
3821
|
+
const sk = PQTKEM.t.RandomScalar?.(seed_T) ?? seed_T
|
|
3822
|
+
const { privateKey: dk_T, publicKey: ek_T } = await PQTKEM.t.GetKeyPair(sk)
|
|
3806
3823
|
|
|
3824
|
+
return { ek_PQ, ek_T, dk_PQ, dk_T }
|
|
3825
|
+
}
|
|
3807
3826
|
|
|
3808
3827
|
|
|
3828
|
+
async function C2PRICombiner(
|
|
3829
|
+
PQTKEM ,
|
|
3830
|
+
ss_PQ ,
|
|
3831
|
+
ss_T ,
|
|
3832
|
+
ct_T ,
|
|
3833
|
+
_ek_T ,
|
|
3834
|
+
label ,
|
|
3835
|
+
) {
|
|
3836
|
+
const ek_T = new Uint8Array(await subtle((c) => c.exportKey('raw', _ek_T), PQTKEM.name))
|
|
3837
|
+
const data = ab(concat(ss_PQ, ss_T, ct_T, ek_T, label))
|
|
3838
|
+
return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data), PQTKEM.name))
|
|
3839
|
+
}
|
|
3809
3840
|
|
|
3810
3841
|
|
|
3842
|
+
async function prepareEncapsG(
|
|
3843
|
+
PQTKEM ,
|
|
3844
|
+
ek_PQ ,
|
|
3845
|
+
ek_T ,
|
|
3846
|
+
) {
|
|
3847
|
+
const res = (await subtle(
|
|
3848
|
+
|
|
3849
|
+
(c) => c.encapsulateBits(PQTKEM.pq.algorithm, ek_PQ),
|
|
3850
|
+
PQTKEM.name,
|
|
3851
|
+
))
|
|
3852
|
+
const ss_PQ = new Uint8Array(res.sharedKey)
|
|
3853
|
+
const ct_PQ = new Uint8Array(res.ciphertext)
|
|
3854
|
+
|
|
3855
|
+
const { privateKey: sk_E, publicKey } = (await subtle(
|
|
3856
|
+
(c) => c.generateKey(PQTKEM.t.algorithm, true, ['deriveBits']),
|
|
3857
|
+
PQTKEM.name,
|
|
3858
|
+
))
|
|
3859
|
+
const ct_T = new Uint8Array(await subtle((c) => c.exportKey('raw', publicKey), PQTKEM.name))
|
|
3860
|
+
|
|
3861
|
+
const ss_T = new Uint8Array(
|
|
3862
|
+
await subtle(
|
|
3863
|
+
(c) => c.deriveBits({ name: PQTKEM.t.algorithm.name, public: ek_T }, sk_E, PQTKEM.t.Nss << 3),
|
|
3864
|
+
PQTKEM.name,
|
|
3865
|
+
),
|
|
3866
|
+
)
|
|
3867
|
+
checkNotAllZeros(ss_T)
|
|
3811
3868
|
|
|
3869
|
+
return [ss_PQ, ss_T, ct_PQ, ct_T]
|
|
3870
|
+
}
|
|
3812
3871
|
|
|
3813
3872
|
|
|
3873
|
+
async function prepareDecapsG(
|
|
3874
|
+
PQTKEM ,
|
|
3875
|
+
dk_PQ ,
|
|
3876
|
+
dk_T ,
|
|
3877
|
+
ct_PQ ,
|
|
3878
|
+
_ct_T ,
|
|
3879
|
+
) {
|
|
3880
|
+
const ss_PQ = new Uint8Array(
|
|
3881
|
+
await subtle(
|
|
3814
3882
|
|
|
3883
|
+
(c) => c.decapsulateBits(PQTKEM.pq.algorithm, dk_PQ, ct_PQ),
|
|
3884
|
+
PQTKEM.name,
|
|
3885
|
+
),
|
|
3886
|
+
)
|
|
3815
3887
|
|
|
3888
|
+
const ct_T = ab(_ct_T)
|
|
3889
|
+
const pub = await subtle(
|
|
3890
|
+
(c) => c.importKey('raw', ct_T, PQTKEM.t.algorithm, true, []),
|
|
3891
|
+
PQTKEM.name,
|
|
3892
|
+
)
|
|
3816
3893
|
|
|
3894
|
+
const ss_T = new Uint8Array(
|
|
3895
|
+
await subtle(
|
|
3896
|
+
(c) => c.deriveBits({ name: PQTKEM.t.algorithm.name, public: pub }, dk_T, PQTKEM.t.Nss << 3),
|
|
3897
|
+
PQTKEM.name,
|
|
3898
|
+
),
|
|
3899
|
+
)
|
|
3900
|
+
checkNotAllZeros(ss_T)
|
|
3817
3901
|
|
|
3902
|
+
return [ss_PQ, ss_T]
|
|
3903
|
+
}
|
|
3818
3904
|
|
|
3819
3905
|
|
|
3820
3906
|
|
|
@@ -3844,207 +3930,121 @@ function RandomScalarNist(t , seed ) {
|
|
|
3844
3930
|
|
|
3845
3931
|
|
|
3846
3932
|
|
|
3933
|
+
function PQTKEM_SHARED() {
|
|
3934
|
+
Object.freeze(HybridKey.prototype)
|
|
3935
|
+
return {
|
|
3936
|
+
async DeriveKeyPair( ikm , extractable) {
|
|
3937
|
+
const seed = await LabeledDerive(
|
|
3938
|
+
this.kdf,
|
|
3939
|
+
this.suite_id,
|
|
3940
|
+
ikm,
|
|
3941
|
+
encode('DeriveKeyPair'),
|
|
3942
|
+
new Uint8Array(),
|
|
3943
|
+
32,
|
|
3944
|
+
)
|
|
3847
3945
|
|
|
3946
|
+
const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, seed)
|
|
3947
|
+
|
|
3948
|
+
const publicKey = new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
3949
|
+
const privateKey = new HybridKey(
|
|
3950
|
+
priv,
|
|
3951
|
+
this.algorithm,
|
|
3952
|
+
'private',
|
|
3953
|
+
extractable,
|
|
3954
|
+
dk_PQ,
|
|
3955
|
+
dk_T,
|
|
3956
|
+
seed,
|
|
3957
|
+
publicKey,
|
|
3958
|
+
)
|
|
3848
3959
|
|
|
3960
|
+
return { privateKey, publicKey }
|
|
3961
|
+
},
|
|
3962
|
+
async GenerateKeyPair( extractable) {
|
|
3963
|
+
return await this.DeriveKeyPair(crypto.getRandomValues(new Uint8Array(32)), extractable)
|
|
3964
|
+
},
|
|
3965
|
+
async SerializePublicKey( key) {
|
|
3966
|
+
assertKeyAlgorithm(key, this.algorithm)
|
|
3967
|
+
HybridKey.validate(key, true)
|
|
3849
3968
|
|
|
3969
|
+
const format = 'raw-public'
|
|
3970
|
+
const ek_PQ = new Uint8Array(
|
|
3971
|
+
await subtle((c) => c.exportKey(format, key.getPq(priv)), this.name),
|
|
3972
|
+
)
|
|
3973
|
+
const ek_T = new Uint8Array(
|
|
3974
|
+
await subtle((c) => c.exportKey('raw', key.getT(priv)), this.name),
|
|
3975
|
+
)
|
|
3850
3976
|
|
|
3977
|
+
return concat(ek_PQ, ek_T)
|
|
3978
|
+
},
|
|
3979
|
+
async DeserializePublicKey( key) {
|
|
3851
3980
|
|
|
3981
|
+
const format = 'raw-public'
|
|
3852
3982
|
|
|
3983
|
+
const usages = ['encapsulateBits']
|
|
3984
|
+
const pubPq = ab(key.subarray(0, this.pq.Npk))
|
|
3985
|
+
const pubT = ab(key.subarray(this.pq.Npk))
|
|
3986
|
+
const [ek_PQ, ek_T] = await Promise.all([
|
|
3987
|
+
subtle((c) => c.importKey(format, pubPq, this.pq.algorithm, true, usages), this.name),
|
|
3988
|
+
subtle((c) => c.importKey('raw', pubT, this.t.algorithm, true, []), this.name),
|
|
3989
|
+
])
|
|
3990
|
+
|
|
3991
|
+
return new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
3992
|
+
},
|
|
3993
|
+
async SerializePrivateKey( key) {
|
|
3994
|
+
assertKeyAlgorithm(key, this.algorithm)
|
|
3995
|
+
HybridKey.validate(key, true)
|
|
3853
3996
|
|
|
3997
|
+
return key.getSeed(priv)
|
|
3998
|
+
},
|
|
3999
|
+
async DeserializePrivateKey( key, extractable) {
|
|
4000
|
+
const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, key)
|
|
4001
|
+
const publicKey = new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
4002
|
+
const privateKey = new HybridKey(
|
|
4003
|
+
priv,
|
|
4004
|
+
this.algorithm,
|
|
4005
|
+
'private',
|
|
4006
|
+
extractable,
|
|
4007
|
+
dk_PQ,
|
|
4008
|
+
dk_T,
|
|
4009
|
+
slice(key),
|
|
4010
|
+
publicKey,
|
|
4011
|
+
)
|
|
3854
4012
|
|
|
4013
|
+
return privateKey
|
|
4014
|
+
},
|
|
4015
|
+
async Encap( pkR) {
|
|
4016
|
+
assertKeyAlgorithm(pkR, this.algorithm)
|
|
4017
|
+
HybridKey.validate(pkR)
|
|
3855
4018
|
|
|
4019
|
+
const ek_PQ = pkR.getPq(priv)
|
|
4020
|
+
const ek_T = pkR.getT(priv)
|
|
4021
|
+
const [ss_PQ, ss_T, ct_PQ, ct_T] = await prepareEncapsG(this, ek_PQ, ek_T)
|
|
4022
|
+
const ss_H = await C2PRICombiner(this, ss_PQ, ss_T, ct_T, ek_T, this.label)
|
|
4023
|
+
const ct_H = concat(ct_PQ, ct_T)
|
|
3856
4024
|
|
|
4025
|
+
return { shared_secret: ss_H, enc: ct_H }
|
|
4026
|
+
},
|
|
4027
|
+
async Decap( enc, skR, pkR) {
|
|
4028
|
+
assertKeyAlgorithm(skR, this.algorithm)
|
|
4029
|
+
HybridKey.validate(skR)
|
|
3857
4030
|
|
|
4031
|
+
if (pkR) {
|
|
4032
|
+
assertKeyAlgorithm(pkR, this.algorithm)
|
|
4033
|
+
HybridKey.validate(pkR)
|
|
4034
|
+
}
|
|
3858
4035
|
|
|
4036
|
+
const [ct_PQ, ct_T] = split(this.pq.Nct, this.t.Nct, enc)
|
|
4037
|
+
const ek = pkR ?? skR.getPublicKey(priv)
|
|
4038
|
+
const ek_T = ek.getT(priv)
|
|
4039
|
+
const dk_PQ = skR.getPq(priv)
|
|
4040
|
+
const dk_T = skR.getT(priv)
|
|
4041
|
+
const [ss_PQ, ss_T] = await prepareDecapsG(this, dk_PQ, dk_T, ct_PQ, ct_T)
|
|
4042
|
+
const ss_H = await C2PRICombiner(this, ss_PQ, ss_T, ct_T, ek_T, this.label)
|
|
3859
4043
|
|
|
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
|
-
|
|
4044
|
+
return ss_H
|
|
4045
|
+
},
|
|
4046
|
+
}
|
|
4047
|
+
}
|
|
4048
4048
|
|
|
4049
4049
|
|
|
4050
4050
|
|