hpke 1.1.7 → 1.1.9
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/README.md +30 -19
- package/index.d.ts +133 -90
- package/index.d.ts.map +1 -1
- package/index.js +301 -253
- package/index.ts +389 -341
- package/package.json +12 -11
package/index.js
CHANGED
|
@@ -72,6 +72,30 @@ function IncrementSeq(seq , maxSeq ) {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
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
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
75
99
|
async function ContextExport(
|
|
76
100
|
suite ,
|
|
77
101
|
exporterSecret ,
|
|
@@ -255,6 +279,11 @@ class SenderContext {
|
|
|
255
279
|
|
|
256
280
|
|
|
257
281
|
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
258
287
|
|
|
259
288
|
|
|
260
289
|
|
|
@@ -410,6 +439,11 @@ class RecipientContext {
|
|
|
410
439
|
|
|
411
440
|
|
|
412
441
|
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
413
447
|
|
|
414
448
|
|
|
415
449
|
|
|
@@ -842,6 +876,9 @@ export class CipherSuite {
|
|
|
842
876
|
|
|
843
877
|
|
|
844
878
|
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
845
882
|
|
|
846
883
|
|
|
847
884
|
|
|
@@ -891,6 +928,9 @@ export class CipherSuite {
|
|
|
891
928
|
|
|
892
929
|
|
|
893
930
|
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
894
934
|
|
|
895
935
|
|
|
896
936
|
|
|
@@ -929,6 +969,19 @@ export class CipherSuite {
|
|
|
929
969
|
|
|
930
970
|
|
|
931
971
|
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
932
985
|
|
|
933
986
|
|
|
934
987
|
|
|
@@ -977,6 +1030,19 @@ export class CipherSuite {
|
|
|
977
1030
|
|
|
978
1031
|
|
|
979
1032
|
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
980
1046
|
|
|
981
1047
|
|
|
982
1048
|
|
|
@@ -1042,7 +1108,6 @@ export class CipherSuite {
|
|
|
1042
1108
|
|
|
1043
1109
|
|
|
1044
1110
|
|
|
1045
|
-
|
|
1046
1111
|
|
|
1047
1112
|
|
|
1048
1113
|
async SetupSender(
|
|
@@ -1123,7 +1188,6 @@ export class CipherSuite {
|
|
|
1123
1188
|
|
|
1124
1189
|
|
|
1125
1190
|
|
|
1126
|
-
|
|
1127
1191
|
|
|
1128
1192
|
|
|
1129
1193
|
async SetupRecipient(
|
|
@@ -1533,6 +1597,38 @@ export function encode(string ) {
|
|
|
1533
1597
|
return bytes
|
|
1534
1598
|
}
|
|
1535
1599
|
|
|
1600
|
+
|
|
1601
|
+
|
|
1602
|
+
|
|
1603
|
+
|
|
1604
|
+
|
|
1605
|
+
|
|
1606
|
+
|
|
1607
|
+
|
|
1608
|
+
|
|
1609
|
+
|
|
1610
|
+
|
|
1611
|
+
|
|
1612
|
+
export function I2OSP(n , w ) {
|
|
1613
|
+
if (!Number.isSafeInteger(w) || w <= 0) {
|
|
1614
|
+
throw new Error('w must be a positive safe integer')
|
|
1615
|
+
}
|
|
1616
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
1617
|
+
throw new Error('n must be a non-negative safe integer')
|
|
1618
|
+
}
|
|
1619
|
+
const max = Math.pow(256, w)
|
|
1620
|
+
if (n >= max) {
|
|
1621
|
+
throw new Error('n too large to fit in w-length byte string')
|
|
1622
|
+
}
|
|
1623
|
+
const ret = new Uint8Array(w)
|
|
1624
|
+
let num = n
|
|
1625
|
+
for (let i = 0; i < w && num; i++) {
|
|
1626
|
+
ret[w - (i + 1)] = num % 256
|
|
1627
|
+
num = Math.floor(num / 256)
|
|
1628
|
+
}
|
|
1629
|
+
return ret
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1536
1632
|
|
|
1537
1633
|
|
|
1538
1634
|
|
|
@@ -1563,85 +1659,10 @@ function lengthPrefixed(x ) {
|
|
|
1563
1659
|
|
|
1564
1660
|
|
|
1565
1661
|
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
export async function LabeledDerive(
|
|
1586
|
-
KDF ,
|
|
1587
|
-
suite_id ,
|
|
1588
|
-
ikm ,
|
|
1589
|
-
label ,
|
|
1590
|
-
context ,
|
|
1591
|
-
L ,
|
|
1592
|
-
) {
|
|
1593
|
-
const labeled_ikm = concat(ikm, L_HPKE_v1, suite_id, lengthPrefixed(label), I2OSP(L, 2), context)
|
|
1594
|
-
return await KDF.Derive(labeled_ikm, L)
|
|
1595
|
-
}
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
async function Export_OneStage(
|
|
1599
|
-
KDF ,
|
|
1600
|
-
suite_id ,
|
|
1601
|
-
exporter_secret ,
|
|
1602
|
-
exporter_context ,
|
|
1603
|
-
L ,
|
|
1604
|
-
) {
|
|
1605
|
-
checkLength(exporter_context, 'Exporter context', MAX_LENGTH_ONE_STAGE)
|
|
1606
|
-
return await LabeledDerive(KDF, suite_id, exporter_secret, L_sec, exporter_context, L)
|
|
1607
|
-
}
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
async function CombineSecrets_OneStage(
|
|
1611
|
-
suite ,
|
|
1612
|
-
mode ,
|
|
1613
|
-
shared_secret ,
|
|
1614
|
-
info ,
|
|
1615
|
-
psk ,
|
|
1616
|
-
psk_id ,
|
|
1617
|
-
) {
|
|
1618
|
-
checkLength(psk, 'PSK', MAX_LENGTH_ONE_STAGE)
|
|
1619
|
-
checkLength(psk_id, 'PSK ID', MAX_LENGTH_ONE_STAGE)
|
|
1620
|
-
checkLength(info, 'Info', MAX_LENGTH_ONE_STAGE)
|
|
1621
|
-
|
|
1622
|
-
const secrets = concat(lengthPrefixed(psk), lengthPrefixed(shared_secret))
|
|
1623
|
-
const context = concat(I2OSP(mode, 1), lengthPrefixed(psk_id), lengthPrefixed(info))
|
|
1624
|
-
|
|
1625
|
-
const secret = await LabeledDerive(
|
|
1626
|
-
suite.KDF,
|
|
1627
|
-
suite.id,
|
|
1628
|
-
secrets,
|
|
1629
|
-
L_secret,
|
|
1630
|
-
context,
|
|
1631
|
-
suite.AEAD.Nk + suite.AEAD.Nn + suite.KDF.Nh,
|
|
1632
|
-
)
|
|
1633
|
-
|
|
1634
|
-
const key = slice(secret, 0, suite.AEAD.Nk)
|
|
1635
|
-
const base_nonce = slice(secret, suite.AEAD.Nk, suite.AEAD.Nk + suite.AEAD.Nn)
|
|
1636
|
-
const exporter_secret = slice(secret, suite.AEAD.Nk + suite.AEAD.Nn)
|
|
1637
|
-
|
|
1638
|
-
return { key, base_nonce, exporter_secret }
|
|
1639
|
-
}
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
1662
|
|
|
1643
1663
|
const MAX_LENGTH_TWO_STAGE = 0xffff
|
|
1644
1664
|
|
|
1665
|
+
|
|
1645
1666
|
const MAX_LENGTH_ONE_STAGE = 0xffff
|
|
1646
1667
|
|
|
1647
1668
|
function checkLength(data , name , maxLength ) {
|
|
@@ -1664,59 +1685,11 @@ function checkExtractable(extractable )
|
|
|
1664
1685
|
}
|
|
1665
1686
|
|
|
1666
1687
|
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
mode ,
|
|
1670
|
-
shared_secret ,
|
|
1671
|
-
info ,
|
|
1672
|
-
psk ,
|
|
1673
|
-
psk_id ,
|
|
1674
|
-
) {
|
|
1675
|
-
checkLength(psk, 'PSK', MAX_LENGTH_TWO_STAGE)
|
|
1676
|
-
checkLength(psk_id, 'PSK ID', MAX_LENGTH_TWO_STAGE)
|
|
1677
|
-
checkLength(info, 'Info', MAX_LENGTH_TWO_STAGE)
|
|
1678
|
-
|
|
1679
|
-
const [psk_id_hash, info_hash] = await Promise.all([
|
|
1680
|
-
LabeledExtract(suite.KDF, suite.id, new Uint8Array(), L_psk_id_hash, psk_id),
|
|
1681
|
-
LabeledExtract(suite.KDF, suite.id, new Uint8Array(), L_info_hash, info),
|
|
1682
|
-
])
|
|
1683
|
-
|
|
1684
|
-
const key_schedule_context = concat(I2OSP(mode, 1), psk_id_hash, info_hash)
|
|
1685
|
-
const secret = await LabeledExtract(suite.KDF, suite.id, shared_secret, L_secret, psk)
|
|
1688
|
+
|
|
1689
|
+
|
|
1686
1690
|
|
|
1687
|
-
|
|
1688
|
-
if (suite.AEAD.id === EXPORT_ONLY) {
|
|
1689
|
-
const exporter_secret = await LabeledExpand(
|
|
1690
|
-
suite.KDF,
|
|
1691
|
-
suite.id,
|
|
1692
|
-
secret,
|
|
1693
|
-
L_exp,
|
|
1694
|
-
key_schedule_context,
|
|
1695
|
-
suite.KDF.Nh,
|
|
1696
|
-
)
|
|
1697
|
-
return { key: new Uint8Array(), base_nonce: new Uint8Array(), exporter_secret }
|
|
1698
|
-
}
|
|
1699
1691
|
|
|
1700
|
-
const [key, base_nonce, exporter_secret] = await Promise.all([
|
|
1701
|
-
LabeledExpand(suite.KDF, suite.id, secret, L_key, key_schedule_context, suite.AEAD.Nk),
|
|
1702
|
-
LabeledExpand(suite.KDF, suite.id, secret, L_base_nonce, key_schedule_context, suite.AEAD.Nn),
|
|
1703
|
-
LabeledExpand(suite.KDF, suite.id, secret, L_exp, key_schedule_context, suite.KDF.Nh),
|
|
1704
|
-
])
|
|
1705
1692
|
|
|
1706
|
-
return { key, base_nonce, exporter_secret }
|
|
1707
|
-
}
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
async function Export_TwoStage(
|
|
1711
|
-
KDF ,
|
|
1712
|
-
suite_id ,
|
|
1713
|
-
exporter_secret ,
|
|
1714
|
-
exporter_context ,
|
|
1715
|
-
L ,
|
|
1716
|
-
) {
|
|
1717
|
-
checkLength(exporter_context, 'Exporter context', MAX_LENGTH_TWO_STAGE)
|
|
1718
|
-
return await LabeledExpand(KDF, suite_id, exporter_secret, L_sec, exporter_context, L)
|
|
1719
|
-
}
|
|
1720
1693
|
|
|
1721
1694
|
|
|
1722
1695
|
|
|
@@ -1840,6 +1813,37 @@ async function Export_TwoStage(
|
|
|
1840
1813
|
|
|
1841
1814
|
|
|
1842
1815
|
|
|
1816
|
+
|
|
1817
|
+
|
|
1818
|
+
|
|
1819
|
+
|
|
1820
|
+
|
|
1821
|
+
|
|
1822
|
+
|
|
1823
|
+
|
|
1824
|
+
|
|
1825
|
+
|
|
1826
|
+
|
|
1827
|
+
|
|
1828
|
+
|
|
1829
|
+
|
|
1830
|
+
|
|
1831
|
+
|
|
1832
|
+
|
|
1833
|
+
|
|
1834
|
+
|
|
1835
|
+
|
|
1836
|
+
export async function LabeledDerive(
|
|
1837
|
+
KDF ,
|
|
1838
|
+
suite_id ,
|
|
1839
|
+
ikm ,
|
|
1840
|
+
label ,
|
|
1841
|
+
context ,
|
|
1842
|
+
L ,
|
|
1843
|
+
) {
|
|
1844
|
+
const labeled_ikm = concat(ikm, L_HPKE_v1, suite_id, lengthPrefixed(label), I2OSP(L, 2), context)
|
|
1845
|
+
return await KDF.Derive(labeled_ikm, L)
|
|
1846
|
+
}
|
|
1843
1847
|
|
|
1844
1848
|
|
|
1845
1849
|
|
|
@@ -2233,7 +2237,7 @@ function isKey(key , type , extractable )
|
|
|
2233
2237
|
|
|
2234
2238
|
|
|
2235
2239
|
|
|
2236
|
-
|
|
2240
|
+
|
|
2237
2241
|
|
|
2238
2242
|
|
|
2239
2243
|
|
|
@@ -2254,44 +2258,87 @@ function isKey(key , type , extractable )
|
|
|
2254
2258
|
|
|
2255
2259
|
|
|
2256
2260
|
|
|
2261
|
+
function KDFStages(KDF ) {
|
|
2262
|
+
if (KDF.stages === 1 || KDF.stages === 2) {
|
|
2263
|
+
return KDF.stages
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
throw new Error('unreachable')
|
|
2267
|
+
}
|
|
2257
2268
|
|
|
2269
|
+
|
|
2270
|
+
async function CombineSecrets_OneStage(
|
|
2271
|
+
suite ,
|
|
2272
|
+
mode ,
|
|
2273
|
+
shared_secret ,
|
|
2274
|
+
info ,
|
|
2275
|
+
psk ,
|
|
2276
|
+
psk_id ,
|
|
2277
|
+
) {
|
|
2278
|
+
checkLength(psk, 'PSK', MAX_LENGTH_ONE_STAGE)
|
|
2279
|
+
checkLength(psk_id, 'PSK ID', MAX_LENGTH_ONE_STAGE)
|
|
2280
|
+
checkLength(info, 'Info', MAX_LENGTH_ONE_STAGE)
|
|
2258
2281
|
|
|
2282
|
+
const secrets = concat(lengthPrefixed(psk), lengthPrefixed(shared_secret))
|
|
2283
|
+
const context = concat(I2OSP(mode, 1), lengthPrefixed(psk_id), lengthPrefixed(info))
|
|
2259
2284
|
|
|
2285
|
+
const secret = await LabeledDerive(
|
|
2286
|
+
suite.KDF,
|
|
2287
|
+
suite.id,
|
|
2288
|
+
secrets,
|
|
2289
|
+
L_secret,
|
|
2290
|
+
context,
|
|
2291
|
+
suite.AEAD.Nk + suite.AEAD.Nn + suite.KDF.Nh,
|
|
2292
|
+
)
|
|
2260
2293
|
|
|
2294
|
+
const key = slice(secret, 0, suite.AEAD.Nk)
|
|
2295
|
+
const base_nonce = slice(secret, suite.AEAD.Nk, suite.AEAD.Nk + suite.AEAD.Nn)
|
|
2296
|
+
const exporter_secret = slice(secret, suite.AEAD.Nk + suite.AEAD.Nn)
|
|
2261
2297
|
|
|
2298
|
+
return { key, base_nonce, exporter_secret }
|
|
2299
|
+
}
|
|
2262
2300
|
|
|
2301
|
+
|
|
2302
|
+
async function CombineSecrets_TwoStage(
|
|
2303
|
+
suite ,
|
|
2304
|
+
mode ,
|
|
2305
|
+
shared_secret ,
|
|
2306
|
+
info ,
|
|
2307
|
+
psk ,
|
|
2308
|
+
psk_id ,
|
|
2309
|
+
) {
|
|
2310
|
+
checkLength(psk, 'PSK', MAX_LENGTH_TWO_STAGE)
|
|
2311
|
+
checkLength(psk_id, 'PSK ID', MAX_LENGTH_TWO_STAGE)
|
|
2312
|
+
checkLength(info, 'Info', MAX_LENGTH_TWO_STAGE)
|
|
2263
2313
|
|
|
2314
|
+
const [psk_id_hash, info_hash] = await Promise.all([
|
|
2315
|
+
LabeledExtract(suite.KDF, suite.id, new Uint8Array(), L_psk_id_hash, psk_id),
|
|
2316
|
+
LabeledExtract(suite.KDF, suite.id, new Uint8Array(), L_info_hash, info),
|
|
2317
|
+
])
|
|
2264
2318
|
|
|
2319
|
+
const key_schedule_context = concat(I2OSP(mode, 1), psk_id_hash, info_hash)
|
|
2320
|
+
const secret = await LabeledExtract(suite.KDF, suite.id, shared_secret, L_secret, psk)
|
|
2265
2321
|
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
if (n >= max) {
|
|
2278
|
-
throw new Error('n too large to fit in w-length byte string')
|
|
2279
|
-
}
|
|
2280
|
-
const ret = new Uint8Array(w)
|
|
2281
|
-
let num = n
|
|
2282
|
-
for (let i = 0; i < w && num; i++) {
|
|
2283
|
-
ret[w - (i + 1)] = num % 256
|
|
2284
|
-
num = Math.floor(num / 256)
|
|
2322
|
+
|
|
2323
|
+
if (suite.AEAD.id === EXPORT_ONLY) {
|
|
2324
|
+
const exporter_secret = await LabeledExpand(
|
|
2325
|
+
suite.KDF,
|
|
2326
|
+
suite.id,
|
|
2327
|
+
secret,
|
|
2328
|
+
L_exp,
|
|
2329
|
+
key_schedule_context,
|
|
2330
|
+
suite.KDF.Nh,
|
|
2331
|
+
)
|
|
2332
|
+
return { key: new Uint8Array(), base_nonce: new Uint8Array(), exporter_secret }
|
|
2285
2333
|
}
|
|
2286
|
-
return ret
|
|
2287
|
-
}
|
|
2288
2334
|
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2335
|
+
const [key, base_nonce, exporter_secret] = await Promise.all([
|
|
2336
|
+
LabeledExpand(suite.KDF, suite.id, secret, L_key, key_schedule_context, suite.AEAD.Nk),
|
|
2337
|
+
LabeledExpand(suite.KDF, suite.id, secret, L_base_nonce, key_schedule_context, suite.AEAD.Nn),
|
|
2338
|
+
LabeledExpand(suite.KDF, suite.id, secret, L_exp, key_schedule_context, suite.KDF.Nh),
|
|
2339
|
+
])
|
|
2340
|
+
|
|
2341
|
+
return { key, base_nonce, exporter_secret }
|
|
2295
2342
|
}
|
|
2296
2343
|
|
|
2297
2344
|
|
|
@@ -3816,91 +3863,95 @@ export const AEAD_ChaCha20Poly1305 = function AEAD_ChaCha20Poly1305
|
|
|
3816
3863
|
|
|
3817
3864
|
|
|
3818
3865
|
|
|
3819
|
-
|
|
3820
|
-
const InvalidInvocation = (_ ) => {
|
|
3821
|
-
if (_ !== priv) {
|
|
3822
|
-
throw new Error('invalid invocation')
|
|
3823
|
-
}
|
|
3824
|
-
}
|
|
3825
|
-
const priv = /* @__PURE__ */ Symbol()
|
|
3826
|
-
class HybridKey {
|
|
3827
|
-
#algorithm
|
|
3828
|
-
#type
|
|
3829
|
-
#extractable
|
|
3830
|
-
#t
|
|
3831
|
-
#pq
|
|
3832
|
-
#seed
|
|
3833
|
-
#publicKey
|
|
3834
|
-
|
|
3835
|
-
static #isValid(key ) {
|
|
3836
|
-
return key.#algorithm !== undefined
|
|
3837
|
-
}
|
|
3866
|
+
|
|
3838
3867
|
|
|
3839
|
-
|
|
3840
|
-
|
|
3841
|
-
|
|
3842
|
-
|
|
3868
|
+
const hybridKeys = /* @__PURE__ */ (() => {
|
|
3869
|
+
const priv = Symbol()
|
|
3870
|
+
let validate
|
|
3871
|
+
let getPublicKey
|
|
3872
|
+
let getSeed
|
|
3873
|
+
let getTraditionalKey
|
|
3874
|
+
let getPostQuantumKey
|
|
3875
|
+
|
|
3876
|
+
class HybridKey {
|
|
3877
|
+
#algorithm
|
|
3878
|
+
#type
|
|
3879
|
+
#extractable
|
|
3880
|
+
#t
|
|
3881
|
+
#pq
|
|
3882
|
+
#seed
|
|
3883
|
+
#publicKey
|
|
3884
|
+
|
|
3885
|
+
static {
|
|
3886
|
+
validate = (key, extractable) => {
|
|
3887
|
+
if (typeof key !== 'object' || key === null || !(#extractable in key)) {
|
|
3888
|
+
throw new TypeError('unexpected key constructor')
|
|
3889
|
+
}
|
|
3890
|
+
if (extractable && !key.#extractable) {
|
|
3891
|
+
throw new TypeError('key must be extractable')
|
|
3892
|
+
}
|
|
3843
3893
|
}
|
|
3844
|
-
|
|
3845
|
-
|
|
3894
|
+
getPublicKey = (key) => key.#publicKey
|
|
3895
|
+
getSeed = (key) => slice(key.#seed )
|
|
3896
|
+
getTraditionalKey = (key) => key.#t
|
|
3897
|
+
getPostQuantumKey = (key) => key.#pq
|
|
3846
3898
|
}
|
|
3847
|
-
if (extractable && !(key ).extractable) {
|
|
3848
|
-
throw new TypeError('key must be extractable')
|
|
3849
|
-
}
|
|
3850
|
-
}
|
|
3851
|
-
|
|
3852
|
-
constructor(
|
|
3853
|
-
_ ,
|
|
3854
|
-
algorithm ,
|
|
3855
|
-
type ,
|
|
3856
|
-
extractable ,
|
|
3857
|
-
pq ,
|
|
3858
|
-
t ,
|
|
3859
|
-
seed ,
|
|
3860
|
-
publicKey ,
|
|
3861
|
-
) {
|
|
3862
|
-
InvalidInvocation(_)
|
|
3863
|
-
this.#algorithm = algorithm
|
|
3864
|
-
this.#type = type
|
|
3865
|
-
this.#extractable = extractable
|
|
3866
|
-
this.#pq = pq
|
|
3867
|
-
this.#t = t
|
|
3868
|
-
this.#seed = seed
|
|
3869
|
-
this.#publicKey = publicKey
|
|
3870
|
-
}
|
|
3871
3899
|
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3900
|
+
constructor(
|
|
3901
|
+
_ ,
|
|
3902
|
+
algorithm ,
|
|
3903
|
+
type ,
|
|
3904
|
+
extractable ,
|
|
3905
|
+
pq ,
|
|
3906
|
+
t ,
|
|
3907
|
+
seed ,
|
|
3908
|
+
publicKey ,
|
|
3909
|
+
) {
|
|
3910
|
+
if (_ !== priv) throw new Error('invalid invocation')
|
|
3911
|
+
this.#algorithm = algorithm
|
|
3912
|
+
this.#type = type
|
|
3913
|
+
this.#extractable = extractable
|
|
3914
|
+
this.#pq = pq
|
|
3915
|
+
this.#t = t
|
|
3916
|
+
this.#seed = seed
|
|
3917
|
+
this.#publicKey = publicKey
|
|
3918
|
+
Object.freeze(this)
|
|
3919
|
+
}
|
|
3883
3920
|
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
}
|
|
3921
|
+
get algorithm() {
|
|
3922
|
+
return { name: this.#algorithm.name }
|
|
3923
|
+
}
|
|
3888
3924
|
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
}
|
|
3925
|
+
get extractable() {
|
|
3926
|
+
return this.#extractable
|
|
3927
|
+
}
|
|
3893
3928
|
|
|
3894
|
-
|
|
3895
|
-
|
|
3896
|
-
|
|
3929
|
+
get type() {
|
|
3930
|
+
return this.#type
|
|
3931
|
+
}
|
|
3897
3932
|
}
|
|
3898
3933
|
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
|
|
3934
|
+
Object.freeze(HybridKey)
|
|
3935
|
+
Object.freeze(HybridKey.prototype)
|
|
3936
|
+
return {
|
|
3937
|
+
create(
|
|
3938
|
+
algorithm ,
|
|
3939
|
+
type ,
|
|
3940
|
+
extractable ,
|
|
3941
|
+
pq ,
|
|
3942
|
+
t ,
|
|
3943
|
+
seed ,
|
|
3944
|
+
publicKey ,
|
|
3945
|
+
) {
|
|
3946
|
+
return new HybridKey(priv, algorithm, type, extractable, pq, t, seed, publicKey)
|
|
3947
|
+
},
|
|
3948
|
+
validate,
|
|
3949
|
+
getPublicKey,
|
|
3950
|
+
getSeed,
|
|
3951
|
+
getTraditionalKey,
|
|
3952
|
+
getPostQuantumKey,
|
|
3902
3953
|
}
|
|
3903
|
-
}
|
|
3954
|
+
})()
|
|
3904
3955
|
|
|
3905
3956
|
function split(N1 , N2 , x ) {
|
|
3906
3957
|
if (x.byteLength !== N1 + N2) {
|
|
@@ -4062,7 +4113,6 @@ async function prepareDecapsG(
|
|
|
4062
4113
|
|
|
4063
4114
|
|
|
4064
4115
|
function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
4065
|
-
Object.freeze(HybridKey.prototype)
|
|
4066
4116
|
return {
|
|
4067
4117
|
async DeriveKeyPair( ikm , extractable) {
|
|
4068
4118
|
const seed = await LabeledDerive(
|
|
@@ -4076,9 +4126,8 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
|
4076
4126
|
|
|
4077
4127
|
const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, seed)
|
|
4078
4128
|
|
|
4079
|
-
const publicKey =
|
|
4080
|
-
const privateKey =
|
|
4081
|
-
priv,
|
|
4129
|
+
const publicKey = hybridKeys.create(this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
4130
|
+
const privateKey = hybridKeys.create(
|
|
4082
4131
|
this.algorithm,
|
|
4083
4132
|
'private',
|
|
4084
4133
|
extractable,
|
|
@@ -4095,14 +4144,14 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
|
4095
4144
|
},
|
|
4096
4145
|
async SerializePublicKey( key) {
|
|
4097
4146
|
assertKeyAlgorithm(key, this.algorithm)
|
|
4098
|
-
|
|
4147
|
+
hybridKeys.validate(key, true)
|
|
4099
4148
|
|
|
4100
4149
|
const format = 'raw-public'
|
|
4101
4150
|
const ek_PQ = new Uint8Array(
|
|
4102
|
-
await subtle((c) => c.exportKey(format,
|
|
4151
|
+
await subtle((c) => c.exportKey(format, hybridKeys.getPostQuantumKey(key)), this.name),
|
|
4103
4152
|
)
|
|
4104
4153
|
const ek_T = new Uint8Array(
|
|
4105
|
-
await subtle((c) => c.exportKey('raw',
|
|
4154
|
+
await subtle((c) => c.exportKey('raw', hybridKeys.getTraditionalKey(key)), this.name),
|
|
4106
4155
|
)
|
|
4107
4156
|
|
|
4108
4157
|
return concat(ek_PQ, ek_T)
|
|
@@ -4119,20 +4168,19 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
|
4119
4168
|
subtle((c) => c.importKey('raw', pubT, this.t.algorithm, true, []), this.name),
|
|
4120
4169
|
])
|
|
4121
4170
|
|
|
4122
|
-
return
|
|
4171
|
+
return hybridKeys.create(this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
4123
4172
|
},
|
|
4124
4173
|
async SerializePrivateKey( key) {
|
|
4125
4174
|
assertKeyAlgorithm(key, this.algorithm)
|
|
4126
|
-
|
|
4175
|
+
hybridKeys.validate(key, true)
|
|
4127
4176
|
|
|
4128
|
-
return
|
|
4177
|
+
return hybridKeys.getSeed(key)
|
|
4129
4178
|
},
|
|
4130
4179
|
async DeserializePrivateKey( key, extractable) {
|
|
4131
4180
|
const seed = slice(key)
|
|
4132
4181
|
const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, seed)
|
|
4133
|
-
const publicKey =
|
|
4134
|
-
const privateKey =
|
|
4135
|
-
priv,
|
|
4182
|
+
const publicKey = hybridKeys.create(this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
4183
|
+
const privateKey = hybridKeys.create(
|
|
4136
4184
|
this.algorithm,
|
|
4137
4185
|
'private',
|
|
4138
4186
|
extractable,
|
|
@@ -4146,10 +4194,10 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
|
4146
4194
|
},
|
|
4147
4195
|
async Encap( pkR) {
|
|
4148
4196
|
assertKeyAlgorithm(pkR, this.algorithm)
|
|
4149
|
-
|
|
4197
|
+
hybridKeys.validate(pkR)
|
|
4150
4198
|
|
|
4151
|
-
const ek_PQ =
|
|
4152
|
-
const ek_T =
|
|
4199
|
+
const ek_PQ = hybridKeys.getPostQuantumKey(pkR)
|
|
4200
|
+
const ek_T = hybridKeys.getTraditionalKey(pkR)
|
|
4153
4201
|
const [ss_PQ, ss_T, ct_PQ, ct_T] = await prepareEncapsG(this, ek_PQ, ek_T)
|
|
4154
4202
|
const ss_H = await C2PRICombiner(this, ss_PQ, ss_T, ct_T, ek_T, this.label)
|
|
4155
4203
|
const ct_H = concat(ct_PQ, ct_T)
|
|
@@ -4158,18 +4206,18 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
|
4158
4206
|
},
|
|
4159
4207
|
async Decap( enc, skR, pkR) {
|
|
4160
4208
|
assertKeyAlgorithm(skR, this.algorithm)
|
|
4161
|
-
|
|
4209
|
+
hybridKeys.validate(skR)
|
|
4162
4210
|
|
|
4163
4211
|
if (pkR) {
|
|
4164
4212
|
assertKeyAlgorithm(pkR, this.algorithm)
|
|
4165
|
-
|
|
4213
|
+
hybridKeys.validate(pkR)
|
|
4166
4214
|
}
|
|
4167
4215
|
|
|
4168
4216
|
const [ct_PQ, ct_T] = split(this.pq.Nct, this.t.Nct, enc)
|
|
4169
|
-
const ek = pkR ??
|
|
4170
|
-
const ek_T =
|
|
4171
|
-
const dk_PQ =
|
|
4172
|
-
const dk_T =
|
|
4217
|
+
const ek = pkR ?? hybridKeys.getPublicKey(skR)
|
|
4218
|
+
const ek_T = hybridKeys.getTraditionalKey(ek)
|
|
4219
|
+
const dk_PQ = hybridKeys.getPostQuantumKey(skR)
|
|
4220
|
+
const dk_T = hybridKeys.getTraditionalKey(skR)
|
|
4173
4221
|
const [ss_PQ, ss_T] = await prepareDecapsG(this, dk_PQ, dk_T, ct_PQ, ct_T)
|
|
4174
4222
|
const ss_H = await C2PRICombiner(this, ss_PQ, ss_T, ct_T, ek_T, this.label)
|
|
4175
4223
|
|