hpke 1.1.3 → 1.1.5
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 +5 -5
- package/index.d.ts +157 -102
- package/index.d.ts.map +1 -1
- package/index.js +231 -175
- package/index.ts +338 -282
- package/package.json +20 -13
package/index.js
CHANGED
|
@@ -129,14 +129,14 @@ class SenderContext {
|
|
|
129
129
|
#key
|
|
130
130
|
#base_nonce
|
|
131
131
|
#exporter_secret
|
|
132
|
-
#mode
|
|
132
|
+
#mode
|
|
133
133
|
#seq = 0
|
|
134
134
|
#max_seq
|
|
135
135
|
#mutex
|
|
136
136
|
|
|
137
137
|
constructor(
|
|
138
138
|
suite ,
|
|
139
|
-
mode
|
|
139
|
+
mode ,
|
|
140
140
|
key ,
|
|
141
141
|
base_nonce ,
|
|
142
142
|
exporter_secret ,
|
|
@@ -154,7 +154,7 @@ class SenderContext {
|
|
|
154
154
|
|
|
155
155
|
|
|
156
156
|
|
|
157
|
-
get mode()
|
|
157
|
+
get mode() {
|
|
158
158
|
return this.#mode
|
|
159
159
|
}
|
|
160
160
|
|
|
@@ -280,14 +280,14 @@ class RecipientContext {
|
|
|
280
280
|
#key
|
|
281
281
|
#base_nonce
|
|
282
282
|
#exporter_secret
|
|
283
|
-
#mode
|
|
283
|
+
#mode
|
|
284
284
|
#seq = 0
|
|
285
285
|
#max_seq
|
|
286
286
|
#mutex
|
|
287
287
|
|
|
288
288
|
constructor(
|
|
289
289
|
suite ,
|
|
290
|
-
mode
|
|
290
|
+
mode ,
|
|
291
291
|
key ,
|
|
292
292
|
base_nonce ,
|
|
293
293
|
exporter_secret ,
|
|
@@ -305,7 +305,7 @@ class RecipientContext {
|
|
|
305
305
|
|
|
306
306
|
|
|
307
307
|
|
|
308
|
-
get mode()
|
|
308
|
+
get mode() {
|
|
309
309
|
return this.#mode
|
|
310
310
|
}
|
|
311
311
|
|
|
@@ -448,8 +448,9 @@ const validate = (factory , type )
|
|
|
448
448
|
|
|
449
449
|
|
|
450
450
|
|
|
451
|
-
|
|
452
|
-
|
|
451
|
+
|
|
452
|
+
export class CipherSuite {
|
|
453
|
+
#suite
|
|
453
454
|
|
|
454
455
|
|
|
455
456
|
|
|
@@ -512,7 +513,7 @@ export class CipherSuite {
|
|
|
512
513
|
|
|
513
514
|
|
|
514
515
|
|
|
515
|
-
constructor(KEM
|
|
516
|
+
constructor(KEM , KDF , AEAD ) {
|
|
516
517
|
const kem = validate(KEM, 'KEM')
|
|
517
518
|
const kdf = validate(KDF, 'KDF')
|
|
518
519
|
const aead = validate(AEAD, 'AEAD')
|
|
@@ -629,7 +630,7 @@ export class CipherSuite {
|
|
|
629
630
|
|
|
630
631
|
|
|
631
632
|
|
|
632
|
-
async GenerateKeyPair(extractable )
|
|
633
|
+
async GenerateKeyPair(extractable ) {
|
|
633
634
|
extractable ??= false
|
|
634
635
|
checkExtractable(extractable)
|
|
635
636
|
return await this.#suite.KEM.GenerateKeyPair(extractable)
|
|
@@ -662,7 +663,7 @@ export class CipherSuite {
|
|
|
662
663
|
|
|
663
664
|
|
|
664
665
|
|
|
665
|
-
async DeriveKeyPair(ikm , extractable )
|
|
666
|
+
async DeriveKeyPair(ikm , extractable ) {
|
|
666
667
|
extractable ??= false
|
|
667
668
|
checkExtractable(extractable)
|
|
668
669
|
checkUint8Array(ikm, 'ikm')
|
|
@@ -695,7 +696,7 @@ export class CipherSuite {
|
|
|
695
696
|
|
|
696
697
|
|
|
697
698
|
|
|
698
|
-
async SerializePrivateKey(privateKey
|
|
699
|
+
async SerializePrivateKey(privateKey ) {
|
|
699
700
|
isKey(privateKey, 'private', true)
|
|
700
701
|
|
|
701
702
|
return await this.#suite.KEM.SerializePrivateKey(privateKey)
|
|
@@ -717,7 +718,7 @@ export class CipherSuite {
|
|
|
717
718
|
|
|
718
719
|
|
|
719
720
|
|
|
720
|
-
async SerializePublicKey(publicKey
|
|
721
|
+
async SerializePublicKey(publicKey ) {
|
|
721
722
|
isKey(publicKey, 'public', true)
|
|
722
723
|
|
|
723
724
|
return await this.#suite.KEM.SerializePublicKey(publicKey)
|
|
@@ -742,7 +743,7 @@ export class CipherSuite {
|
|
|
742
743
|
|
|
743
744
|
|
|
744
745
|
|
|
745
|
-
async DeserializePrivateKey(privateKey , extractable )
|
|
746
|
+
async DeserializePrivateKey(privateKey , extractable ) {
|
|
746
747
|
extractable ??= false
|
|
747
748
|
checkExtractable(extractable)
|
|
748
749
|
checkUint8Array(privateKey, 'privateKey')
|
|
@@ -777,7 +778,7 @@ export class CipherSuite {
|
|
|
777
778
|
|
|
778
779
|
|
|
779
780
|
|
|
780
|
-
async DeserializePublicKey(publicKey )
|
|
781
|
+
async DeserializePublicKey(publicKey ) {
|
|
781
782
|
checkUint8Array(publicKey, 'publicKey')
|
|
782
783
|
|
|
783
784
|
try {
|
|
@@ -828,7 +829,7 @@ export class CipherSuite {
|
|
|
828
829
|
|
|
829
830
|
|
|
830
831
|
async Seal(
|
|
831
|
-
publicKey
|
|
832
|
+
publicKey ,
|
|
832
833
|
plaintext ,
|
|
833
834
|
options ,
|
|
834
835
|
) {
|
|
@@ -877,7 +878,7 @@ export class CipherSuite {
|
|
|
877
878
|
|
|
878
879
|
|
|
879
880
|
async Open(
|
|
880
|
-
privateKey
|
|
881
|
+
privateKey ,
|
|
881
882
|
encapsulatedSecret ,
|
|
882
883
|
ciphertext ,
|
|
883
884
|
options ,
|
|
@@ -925,7 +926,7 @@ export class CipherSuite {
|
|
|
925
926
|
|
|
926
927
|
|
|
927
928
|
async SendExport(
|
|
928
|
-
publicKey
|
|
929
|
+
publicKey ,
|
|
929
930
|
exporterContext ,
|
|
930
931
|
length ,
|
|
931
932
|
options ,
|
|
@@ -973,7 +974,7 @@ export class CipherSuite {
|
|
|
973
974
|
|
|
974
975
|
|
|
975
976
|
async ReceiveExport(
|
|
976
|
-
privateKey
|
|
977
|
+
privateKey ,
|
|
977
978
|
encapsulatedSecret ,
|
|
978
979
|
exporterContext ,
|
|
979
980
|
length ,
|
|
@@ -1027,7 +1028,7 @@ export class CipherSuite {
|
|
|
1027
1028
|
|
|
1028
1029
|
|
|
1029
1030
|
async SetupSender(
|
|
1030
|
-
publicKey
|
|
1031
|
+
publicKey ,
|
|
1031
1032
|
options ,
|
|
1032
1033
|
) {
|
|
1033
1034
|
isKey(publicKey, 'public')
|
|
@@ -1108,7 +1109,7 @@ export class CipherSuite {
|
|
|
1108
1109
|
|
|
1109
1110
|
|
|
1110
1111
|
async SetupRecipient(
|
|
1111
|
-
privateKey
|
|
1112
|
+
privateKey ,
|
|
1112
1113
|
encapsulatedSecret ,
|
|
1113
1114
|
options ,
|
|
1114
1115
|
) {
|
|
@@ -1141,7 +1142,7 @@ export class CipherSuite {
|
|
|
1141
1142
|
return new RecipientContext(this.#suite, mode, key, base_nonce, exporter_secret)
|
|
1142
1143
|
}
|
|
1143
1144
|
|
|
1144
|
-
#extractRecipientKeys(skR
|
|
1145
|
+
#extractRecipientKeys(skR ) {
|
|
1145
1146
|
if (isKeyPair(skR)) {
|
|
1146
1147
|
return { skR: skR.privateKey, pkR: skR.publicKey }
|
|
1147
1148
|
}
|
|
@@ -1420,6 +1421,41 @@ export const MODE_PSK = 0x01
|
|
|
1420
1421
|
|
|
1421
1422
|
|
|
1422
1423
|
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
|
|
1431
|
+
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
|
|
1435
|
+
|
|
1436
|
+
|
|
1437
|
+
|
|
1438
|
+
|
|
1439
|
+
|
|
1440
|
+
|
|
1441
|
+
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
|
|
1445
|
+
|
|
1446
|
+
|
|
1447
|
+
|
|
1448
|
+
|
|
1449
|
+
|
|
1450
|
+
|
|
1451
|
+
|
|
1452
|
+
|
|
1453
|
+
|
|
1454
|
+
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
|
|
1423
1459
|
|
|
1424
1460
|
|
|
1425
1461
|
|
|
@@ -2049,7 +2085,8 @@ export async function LabeledExpand(
|
|
|
2049
2085
|
|
|
2050
2086
|
|
|
2051
2087
|
|
|
2052
|
-
|
|
2088
|
+
|
|
2089
|
+
function isKeyPair (skR ) {
|
|
2053
2090
|
if (!skR || typeof skR !== 'object') return false
|
|
2054
2091
|
if ('publicKey' in skR && 'privateKey' in skR) {
|
|
2055
2092
|
const pkR = skR.publicKey
|
|
@@ -2813,7 +2850,9 @@ async function ExtractAndExpand(
|
|
|
2813
2850
|
|
|
2814
2851
|
|
|
2815
2852
|
|
|
2816
|
-
function DHKEM_SHARED()
|
|
2853
|
+
function DHKEM_SHARED()
|
|
2854
|
+
|
|
2855
|
+
{
|
|
2817
2856
|
return {
|
|
2818
2857
|
async GenerateKeyPair( extractable) {
|
|
2819
2858
|
return (await subtle(
|
|
@@ -3186,7 +3225,8 @@ const P256 = {
|
|
|
3186
3225
|
|
|
3187
3226
|
|
|
3188
3227
|
|
|
3189
|
-
export const KEM_DHKEM_P256_HKDF_SHA256
|
|
3228
|
+
export const KEM_DHKEM_P256_HKDF_SHA256 = function ()
|
|
3229
|
+
{
|
|
3190
3230
|
const id = 0x0010
|
|
3191
3231
|
const name = 'DHKEM(P-256, HKDF-SHA256)'
|
|
3192
3232
|
const kdf = KDF_HKDF_SHA256()
|
|
@@ -3241,7 +3281,8 @@ const P384 = {
|
|
|
3241
3281
|
|
|
3242
3282
|
|
|
3243
3283
|
|
|
3244
|
-
export const KEM_DHKEM_P384_HKDF_SHA384
|
|
3284
|
+
export const KEM_DHKEM_P384_HKDF_SHA384 = function ()
|
|
3285
|
+
{
|
|
3245
3286
|
const id = 0x0011
|
|
3246
3287
|
const name = 'DHKEM(P-384, HKDF-SHA384)'
|
|
3247
3288
|
const kdf = KDF_HKDF_SHA384()
|
|
@@ -3296,7 +3337,8 @@ const P521 = {
|
|
|
3296
3337
|
|
|
3297
3338
|
|
|
3298
3339
|
|
|
3299
|
-
export const KEM_DHKEM_P521_HKDF_SHA512
|
|
3340
|
+
export const KEM_DHKEM_P521_HKDF_SHA512 = function ()
|
|
3341
|
+
{
|
|
3300
3342
|
const id = 0x0012
|
|
3301
3343
|
const name = 'DHKEM(P-521, HKDF-SHA512)'
|
|
3302
3344
|
const kdf = KDF_HKDF_SHA512()
|
|
@@ -3342,7 +3384,9 @@ export const KEM_DHKEM_P521_HKDF_SHA512 = function ()
|
|
|
3342
3384
|
|
|
3343
3385
|
|
|
3344
3386
|
|
|
3345
|
-
export const KEM_DHKEM_X25519_HKDF_SHA256
|
|
3387
|
+
export const KEM_DHKEM_X25519_HKDF_SHA256 = function ()
|
|
3388
|
+
|
|
3389
|
+
{
|
|
3346
3390
|
const id = 0x0020
|
|
3347
3391
|
const name = 'DHKEM(X25519, HKDF-SHA256)'
|
|
3348
3392
|
const kdf = KDF_HKDF_SHA256()
|
|
@@ -3389,7 +3433,9 @@ export const KEM_DHKEM_X25519_HKDF_SHA256 = function ()
|
|
|
3389
3433
|
|
|
3390
3434
|
|
|
3391
3435
|
|
|
3392
|
-
export const KEM_DHKEM_X448_HKDF_SHA512
|
|
3436
|
+
export const KEM_DHKEM_X448_HKDF_SHA512 = function ()
|
|
3437
|
+
|
|
3438
|
+
{
|
|
3393
3439
|
const id = 0x0021
|
|
3394
3440
|
const name = 'DHKEM(X448, HKDF-SHA512)'
|
|
3395
3441
|
const kdf = KDF_HKDF_SHA512()
|
|
@@ -3426,9 +3472,11 @@ export const KEM_DHKEM_X448_HKDF_SHA512 = function ()
|
|
|
3426
3472
|
|
|
3427
3473
|
|
|
3428
3474
|
|
|
3429
|
-
|
|
3475
|
+
|
|
3476
|
+
|
|
3477
|
+
function DIRECT_KEM_SHARED() {
|
|
3430
3478
|
return {
|
|
3431
|
-
async DeriveKeyPair(
|
|
3479
|
+
async DeriveKeyPair( ikm, extractable) {
|
|
3432
3480
|
const dk = await LabeledDerive(
|
|
3433
3481
|
this.kdf,
|
|
3434
3482
|
this.suite_id,
|
|
@@ -3445,7 +3493,7 @@ function MLKEM_SHARED() {
|
|
|
3445
3493
|
|
|
3446
3494
|
return { privateKey, publicKey }
|
|
3447
3495
|
},
|
|
3448
|
-
async GenerateKeyPair(
|
|
3496
|
+
async GenerateKeyPair( extractable) {
|
|
3449
3497
|
|
|
3450
3498
|
const usages = ['encapsulateBits', 'decapsulateBits']
|
|
3451
3499
|
return (await subtle(
|
|
@@ -3453,14 +3501,14 @@ function MLKEM_SHARED() {
|
|
|
3453
3501
|
this.name,
|
|
3454
3502
|
))
|
|
3455
3503
|
},
|
|
3456
|
-
async SerializePublicKey(
|
|
3504
|
+
async SerializePublicKey( key) {
|
|
3457
3505
|
assertKeyAlgorithm(key, this.algorithm)
|
|
3458
3506
|
assertCryptoKey(key)
|
|
3459
3507
|
|
|
3460
3508
|
const format = 'raw-public'
|
|
3461
3509
|
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3462
3510
|
},
|
|
3463
|
-
async DeserializePublicKey(
|
|
3511
|
+
async DeserializePublicKey( key) {
|
|
3464
3512
|
|
|
3465
3513
|
const format = 'raw-public'
|
|
3466
3514
|
|
|
@@ -3470,14 +3518,14 @@ function MLKEM_SHARED() {
|
|
|
3470
3518
|
this.name,
|
|
3471
3519
|
)
|
|
3472
3520
|
},
|
|
3473
|
-
async SerializePrivateKey(
|
|
3521
|
+
async SerializePrivateKey( key) {
|
|
3474
3522
|
assertKeyAlgorithm(key, this.algorithm)
|
|
3475
3523
|
assertCryptoKey(key)
|
|
3476
3524
|
|
|
3477
3525
|
const format = 'raw-seed'
|
|
3478
3526
|
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3479
3527
|
},
|
|
3480
|
-
async DeserializePrivateKey(
|
|
3528
|
+
async DeserializePrivateKey( key, extractable) {
|
|
3481
3529
|
|
|
3482
3530
|
const format = 'raw-seed'
|
|
3483
3531
|
|
|
@@ -3487,7 +3535,7 @@ function MLKEM_SHARED() {
|
|
|
3487
3535
|
this.name,
|
|
3488
3536
|
)
|
|
3489
3537
|
},
|
|
3490
|
-
async Encap(
|
|
3538
|
+
async Encap( pkR) {
|
|
3491
3539
|
assertKeyAlgorithm(pkR, this.algorithm)
|
|
3492
3540
|
|
|
3493
3541
|
const { sharedKey, ciphertext } = (await subtle(
|
|
@@ -3498,7 +3546,7 @@ function MLKEM_SHARED() {
|
|
|
3498
3546
|
|
|
3499
3547
|
return { shared_secret: new Uint8Array(sharedKey), enc: new Uint8Array(ciphertext) }
|
|
3500
3548
|
},
|
|
3501
|
-
async Decap(
|
|
3549
|
+
async Decap( enc, skR, _pkR) {
|
|
3502
3550
|
assertKeyAlgorithm(skR, this.algorithm)
|
|
3503
3551
|
return new Uint8Array(
|
|
3504
3552
|
await subtle(
|
|
@@ -3511,6 +3559,25 @@ function MLKEM_SHARED() {
|
|
|
3511
3559
|
}
|
|
3512
3560
|
}
|
|
3513
3561
|
|
|
3562
|
+
function createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk } ) {
|
|
3563
|
+
const kdf = KDF_SHAKE256()
|
|
3564
|
+
|
|
3565
|
+
kdf.name = name
|
|
3566
|
+
return {
|
|
3567
|
+
id,
|
|
3568
|
+
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
3569
|
+
type: 'KEM',
|
|
3570
|
+
name,
|
|
3571
|
+
Nsecret,
|
|
3572
|
+
Nenc,
|
|
3573
|
+
Npk,
|
|
3574
|
+
Nsk,
|
|
3575
|
+
algorithm: { name },
|
|
3576
|
+
kdf,
|
|
3577
|
+
...DIRECT_KEM_SHARED(),
|
|
3578
|
+
}
|
|
3579
|
+
}
|
|
3580
|
+
|
|
3514
3581
|
|
|
3515
3582
|
|
|
3516
3583
|
|
|
@@ -3538,25 +3605,14 @@ function MLKEM_SHARED() {
|
|
|
3538
3605
|
|
|
3539
3606
|
|
|
3540
3607
|
|
|
3541
|
-
export const KEM_ML_KEM_512
|
|
3608
|
+
export const KEM_ML_KEM_512 = function () {
|
|
3542
3609
|
const id = 0x0040
|
|
3543
3610
|
const name = 'ML-KEM-512'
|
|
3544
|
-
const
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
3550
|
-
type: 'KEM',
|
|
3551
|
-
name,
|
|
3552
|
-
Nsecret: 32,
|
|
3553
|
-
Nenc: 768,
|
|
3554
|
-
Npk: 800,
|
|
3555
|
-
Nsk: 64,
|
|
3556
|
-
algorithm: { name: 'ML-KEM-512' },
|
|
3557
|
-
kdf,
|
|
3558
|
-
...MLKEM_SHARED(),
|
|
3559
|
-
}
|
|
3611
|
+
const Nsecret = 32
|
|
3612
|
+
const Nenc = 768
|
|
3613
|
+
const Npk = 800
|
|
3614
|
+
const Nsk = 64
|
|
3615
|
+
return createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk })
|
|
3560
3616
|
}
|
|
3561
3617
|
|
|
3562
3618
|
|
|
@@ -3578,25 +3634,14 @@ export const KEM_ML_KEM_512 = function () {
|
|
|
3578
3634
|
|
|
3579
3635
|
|
|
3580
3636
|
|
|
3581
|
-
export const KEM_ML_KEM_768
|
|
3637
|
+
export const KEM_ML_KEM_768 = function () {
|
|
3582
3638
|
const id = 0x0041
|
|
3583
3639
|
const name = 'ML-KEM-768'
|
|
3584
|
-
const
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
|
|
3589
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
3590
|
-
type: 'KEM',
|
|
3591
|
-
name,
|
|
3592
|
-
Nsecret: 32,
|
|
3593
|
-
Nenc: 1088,
|
|
3594
|
-
Npk: 1184,
|
|
3595
|
-
Nsk: 64,
|
|
3596
|
-
algorithm: { name: 'ML-KEM-768' },
|
|
3597
|
-
kdf,
|
|
3598
|
-
...MLKEM_SHARED(),
|
|
3599
|
-
}
|
|
3640
|
+
const Nsecret = 32
|
|
3641
|
+
const Nenc = 1088
|
|
3642
|
+
const Npk = 1184
|
|
3643
|
+
const Nsk = 64
|
|
3644
|
+
return createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk })
|
|
3600
3645
|
}
|
|
3601
3646
|
|
|
3602
3647
|
|
|
@@ -3618,25 +3663,14 @@ export const KEM_ML_KEM_768 = function () {
|
|
|
3618
3663
|
|
|
3619
3664
|
|
|
3620
3665
|
|
|
3621
|
-
export const KEM_ML_KEM_1024
|
|
3666
|
+
export const KEM_ML_KEM_1024 = function () {
|
|
3622
3667
|
const id = 0x0042
|
|
3623
3668
|
const name = 'ML-KEM-1024'
|
|
3624
|
-
const
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3629
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
3630
|
-
type: 'KEM',
|
|
3631
|
-
name,
|
|
3632
|
-
Nsecret: 32,
|
|
3633
|
-
Nenc: 1568,
|
|
3634
|
-
Npk: 1568,
|
|
3635
|
-
Nsk: 64,
|
|
3636
|
-
algorithm: { name: 'ML-KEM-1024' },
|
|
3637
|
-
kdf,
|
|
3638
|
-
...MLKEM_SHARED(),
|
|
3639
|
-
}
|
|
3669
|
+
const Nsecret = 32
|
|
3670
|
+
const Nenc = 1568
|
|
3671
|
+
const Npk = 1568
|
|
3672
|
+
const Nsk = 64
|
|
3673
|
+
return createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk })
|
|
3640
3674
|
}
|
|
3641
3675
|
|
|
3642
3676
|
|
|
@@ -3910,7 +3944,7 @@ function split(N1 , N2 , x )
|
|
|
3910
3944
|
return [x1, x2]
|
|
3911
3945
|
}
|
|
3912
3946
|
|
|
3913
|
-
function RandomScalarNist(t
|
|
3947
|
+
function RandomScalarNist(t , seed ) {
|
|
3914
3948
|
let sk_bigint = 0n
|
|
3915
3949
|
let start = 0
|
|
3916
3950
|
let end = t.Nscalar
|
|
@@ -3928,69 +3962,69 @@ function RandomScalarNist(t , seed ) {
|
|
|
3928
3962
|
}
|
|
3929
3963
|
|
|
3930
3964
|
|
|
3931
|
-
async function expandDecapsKeyG(
|
|
3932
|
-
const Nout =
|
|
3965
|
+
async function expandDecapsKeyG(kem , seed ) {
|
|
3966
|
+
const Nout = kem.pq.Nseed + kem.t.Nseed
|
|
3933
3967
|
const bits = Nout << 3
|
|
3934
3968
|
|
|
3935
3969
|
const algorithm = { name: 'cSHAKE256', length: bits, outputLength: bits }
|
|
3936
|
-
const seed_full = await subtle((c) => c.digest(algorithm, seed ),
|
|
3970
|
+
const seed_full = await subtle((c) => c.digest(algorithm, seed ), kem.name)
|
|
3937
3971
|
|
|
3938
|
-
const [seed_PQ, seed_T] = split(
|
|
3972
|
+
const [seed_PQ, seed_T] = split(kem.pq.Nseed, kem.t.Nseed, new Uint8Array(seed_full))
|
|
3939
3973
|
|
|
3940
3974
|
|
|
3941
3975
|
const format = 'raw-seed'
|
|
3942
3976
|
|
|
3943
3977
|
const usages = ['decapsulateBits', 'encapsulateBits']
|
|
3944
3978
|
const dk_PQ = await subtle(
|
|
3945
|
-
(c) => c.importKey(format, seed_PQ ,
|
|
3946
|
-
|
|
3979
|
+
(c) => c.importKey(format, seed_PQ , kem.pq.algorithm, true, [usages[0]]),
|
|
3980
|
+
kem.name,
|
|
3947
3981
|
)
|
|
3948
|
-
const ek_PQ = await getPublicKey(
|
|
3982
|
+
const ek_PQ = await getPublicKey(kem.name, dk_PQ, [usages[1]])
|
|
3949
3983
|
|
|
3950
|
-
const sk =
|
|
3951
|
-
const { privateKey: dk_T, publicKey: ek_T } = await
|
|
3984
|
+
const sk = kem.t.RandomScalar?.(seed_T) ?? seed_T
|
|
3985
|
+
const { privateKey: dk_T, publicKey: ek_T } = await kem.t.GetKeyPair(sk)
|
|
3952
3986
|
|
|
3953
3987
|
return { ek_PQ, ek_T, dk_PQ, dk_T }
|
|
3954
3988
|
}
|
|
3955
3989
|
|
|
3956
3990
|
|
|
3957
3991
|
async function C2PRICombiner(
|
|
3958
|
-
|
|
3992
|
+
kem ,
|
|
3959
3993
|
ss_PQ ,
|
|
3960
3994
|
ss_T ,
|
|
3961
3995
|
ct_T ,
|
|
3962
3996
|
_ek_T ,
|
|
3963
3997
|
label ,
|
|
3964
3998
|
) {
|
|
3965
|
-
const ek_T = new Uint8Array(await subtle((c) => c.exportKey('raw', _ek_T),
|
|
3999
|
+
const ek_T = new Uint8Array(await subtle((c) => c.exportKey('raw', _ek_T), kem.name))
|
|
3966
4000
|
const data = concat(ss_PQ, ss_T, ct_T, ek_T, label)
|
|
3967
|
-
return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data),
|
|
4001
|
+
return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data), kem.name))
|
|
3968
4002
|
}
|
|
3969
4003
|
|
|
3970
4004
|
|
|
3971
4005
|
async function prepareEncapsG(
|
|
3972
|
-
|
|
4006
|
+
kem ,
|
|
3973
4007
|
ek_PQ ,
|
|
3974
4008
|
ek_T ,
|
|
3975
4009
|
) {
|
|
3976
4010
|
const res = (await subtle(
|
|
3977
4011
|
|
|
3978
|
-
(c) => c.encapsulateBits(
|
|
3979
|
-
|
|
4012
|
+
(c) => c.encapsulateBits(kem.pq.algorithm, ek_PQ),
|
|
4013
|
+
kem.name,
|
|
3980
4014
|
))
|
|
3981
4015
|
const ss_PQ = new Uint8Array(res.sharedKey)
|
|
3982
4016
|
const ct_PQ = new Uint8Array(res.ciphertext)
|
|
3983
4017
|
|
|
3984
4018
|
const { privateKey: sk_E, publicKey } = (await subtle(
|
|
3985
|
-
(c) => c.generateKey(
|
|
3986
|
-
|
|
4019
|
+
(c) => c.generateKey(kem.t.algorithm, true, ['deriveBits']),
|
|
4020
|
+
kem.name,
|
|
3987
4021
|
))
|
|
3988
|
-
const ct_T = new Uint8Array(await subtle((c) => c.exportKey('raw', publicKey),
|
|
4022
|
+
const ct_T = new Uint8Array(await subtle((c) => c.exportKey('raw', publicKey), kem.name))
|
|
3989
4023
|
|
|
3990
4024
|
const ss_T = new Uint8Array(
|
|
3991
4025
|
await subtle(
|
|
3992
|
-
(c) => c.deriveBits({ name:
|
|
3993
|
-
|
|
4026
|
+
(c) => c.deriveBits({ name: kem.t.algorithm.name, public: ek_T }, sk_E, kem.t.Nss << 3),
|
|
4027
|
+
kem.name,
|
|
3994
4028
|
),
|
|
3995
4029
|
)
|
|
3996
4030
|
checkNotAllZeros(ss_T)
|
|
@@ -4000,7 +4034,7 @@ async function prepareEncapsG(
|
|
|
4000
4034
|
|
|
4001
4035
|
|
|
4002
4036
|
async function prepareDecapsG(
|
|
4003
|
-
|
|
4037
|
+
kem ,
|
|
4004
4038
|
dk_PQ ,
|
|
4005
4039
|
dk_T ,
|
|
4006
4040
|
ct_PQ ,
|
|
@@ -4009,20 +4043,20 @@ async function prepareDecapsG(
|
|
|
4009
4043
|
const ss_PQ = new Uint8Array(
|
|
4010
4044
|
await subtle(
|
|
4011
4045
|
|
|
4012
|
-
(c) => c.decapsulateBits(
|
|
4013
|
-
|
|
4046
|
+
(c) => c.decapsulateBits(kem.pq.algorithm, dk_PQ, ct_PQ),
|
|
4047
|
+
kem.name,
|
|
4014
4048
|
),
|
|
4015
4049
|
)
|
|
4016
4050
|
|
|
4017
4051
|
const pub = await subtle(
|
|
4018
|
-
(c) => c.importKey('raw', ct_T ,
|
|
4019
|
-
|
|
4052
|
+
(c) => c.importKey('raw', ct_T , kem.t.algorithm, true, []),
|
|
4053
|
+
kem.name,
|
|
4020
4054
|
)
|
|
4021
4055
|
|
|
4022
4056
|
const ss_T = new Uint8Array(
|
|
4023
4057
|
await subtle(
|
|
4024
|
-
(c) => c.deriveBits({ name:
|
|
4025
|
-
|
|
4058
|
+
(c) => c.deriveBits({ name: kem.t.algorithm.name, public: pub }, dk_T, kem.t.Nss << 3),
|
|
4059
|
+
kem.name,
|
|
4026
4060
|
),
|
|
4027
4061
|
)
|
|
4028
4062
|
checkNotAllZeros(ss_T)
|
|
@@ -4058,10 +4092,12 @@ async function prepareDecapsG(
|
|
|
4058
4092
|
|
|
4059
4093
|
|
|
4060
4094
|
|
|
4061
|
-
|
|
4095
|
+
|
|
4096
|
+
|
|
4097
|
+
function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
4062
4098
|
Object.freeze(HybridKey.prototype)
|
|
4063
4099
|
return {
|
|
4064
|
-
async DeriveKeyPair(
|
|
4100
|
+
async DeriveKeyPair( ikm , extractable) {
|
|
4065
4101
|
const seed = await LabeledDerive(
|
|
4066
4102
|
this.kdf,
|
|
4067
4103
|
this.suite_id,
|
|
@@ -4087,10 +4123,10 @@ function PQTKEM_SHARED() {
|
|
|
4087
4123
|
|
|
4088
4124
|
return { privateKey, publicKey }
|
|
4089
4125
|
},
|
|
4090
|
-
async GenerateKeyPair(
|
|
4126
|
+
async GenerateKeyPair( extractable) {
|
|
4091
4127
|
return await this.DeriveKeyPair(crypto.getRandomValues(new Uint8Array(32)), extractable)
|
|
4092
4128
|
},
|
|
4093
|
-
async SerializePublicKey(
|
|
4129
|
+
async SerializePublicKey( key) {
|
|
4094
4130
|
assertKeyAlgorithm(key, this.algorithm)
|
|
4095
4131
|
HybridKey.validate(key, true)
|
|
4096
4132
|
|
|
@@ -4104,7 +4140,7 @@ function PQTKEM_SHARED() {
|
|
|
4104
4140
|
|
|
4105
4141
|
return concat(ek_PQ, ek_T)
|
|
4106
4142
|
},
|
|
4107
|
-
async DeserializePublicKey(
|
|
4143
|
+
async DeserializePublicKey( key) {
|
|
4108
4144
|
|
|
4109
4145
|
const format = 'raw-public'
|
|
4110
4146
|
|
|
@@ -4118,13 +4154,13 @@ function PQTKEM_SHARED() {
|
|
|
4118
4154
|
|
|
4119
4155
|
return new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
4120
4156
|
},
|
|
4121
|
-
async SerializePrivateKey(
|
|
4157
|
+
async SerializePrivateKey( key) {
|
|
4122
4158
|
assertKeyAlgorithm(key, this.algorithm)
|
|
4123
4159
|
HybridKey.validate(key, true)
|
|
4124
4160
|
|
|
4125
4161
|
return key.getSeed(priv)
|
|
4126
4162
|
},
|
|
4127
|
-
async DeserializePrivateKey(
|
|
4163
|
+
async DeserializePrivateKey( key, extractable) {
|
|
4128
4164
|
const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, key)
|
|
4129
4165
|
const publicKey = new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
4130
4166
|
const privateKey = new HybridKey(
|
|
@@ -4140,7 +4176,7 @@ function PQTKEM_SHARED() {
|
|
|
4140
4176
|
|
|
4141
4177
|
return privateKey
|
|
4142
4178
|
},
|
|
4143
|
-
async Encap(
|
|
4179
|
+
async Encap( pkR) {
|
|
4144
4180
|
assertKeyAlgorithm(pkR, this.algorithm)
|
|
4145
4181
|
HybridKey.validate(pkR)
|
|
4146
4182
|
|
|
@@ -4152,7 +4188,7 @@ function PQTKEM_SHARED() {
|
|
|
4152
4188
|
|
|
4153
4189
|
return { shared_secret: ss_H, enc: ct_H }
|
|
4154
4190
|
},
|
|
4155
|
-
async Decap(
|
|
4191
|
+
async Decap( enc, skR, pkR) {
|
|
4156
4192
|
assertKeyAlgorithm(skR, this.algorithm)
|
|
4157
4193
|
HybridKey.validate(skR)
|
|
4158
4194
|
|
|
@@ -4174,6 +4210,20 @@ function PQTKEM_SHARED() {
|
|
|
4174
4210
|
}
|
|
4175
4211
|
}
|
|
4176
4212
|
|
|
4213
|
+
function createCompositeHybridKEM(parameters ) {
|
|
4214
|
+
const kdf = KDF_SHAKE256()
|
|
4215
|
+
|
|
4216
|
+
kdf.name = parameters.name
|
|
4217
|
+
return {
|
|
4218
|
+
...parameters,
|
|
4219
|
+
suite_id: concat(L_KEM, I2OSP(parameters.id, 2)),
|
|
4220
|
+
type: 'KEM',
|
|
4221
|
+
algorithm: { name: parameters.name },
|
|
4222
|
+
kdf,
|
|
4223
|
+
...COMPOSITE_HYBRID_KEM_SHARED(),
|
|
4224
|
+
}
|
|
4225
|
+
}
|
|
4226
|
+
|
|
4177
4227
|
|
|
4178
4228
|
|
|
4179
4229
|
|
|
@@ -4197,24 +4247,27 @@ function PQTKEM_SHARED() {
|
|
|
4197
4247
|
|
|
4198
4248
|
|
|
4199
4249
|
|
|
4200
|
-
|
|
4250
|
+
|
|
4251
|
+
|
|
4252
|
+
|
|
4253
|
+
|
|
4254
|
+
|
|
4255
|
+
export const KEM_MLKEM768_X25519 = function () {
|
|
4201
4256
|
const id = 0x647a
|
|
4202
4257
|
const name = 'MLKEM768-X25519'
|
|
4203
|
-
const
|
|
4204
|
-
const
|
|
4258
|
+
const Nsecret = 32
|
|
4259
|
+
const Nenc = 1120
|
|
4260
|
+
const Npk = 1216
|
|
4261
|
+
const Nsk = 32
|
|
4262
|
+
const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
|
|
4263
|
+
|
|
4264
|
+
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4265
|
+
return createDirectKEM(kem)
|
|
4266
|
+
}
|
|
4205
4267
|
|
|
4206
|
-
|
|
4207
|
-
return {
|
|
4208
|
-
|
|
4209
|
-
kdf,
|
|
4210
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
4211
|
-
type: 'KEM',
|
|
4212
|
-
name,
|
|
4213
|
-
Nsecret: 32,
|
|
4214
|
-
Nenc: 1120,
|
|
4215
|
-
Npk: 1216,
|
|
4216
|
-
Nsk: 32,
|
|
4217
|
-
algorithm: { name: 'MLKEM768-X25519' },
|
|
4268
|
+
const pkcs8 = Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20)
|
|
4269
|
+
return createCompositeHybridKEM({
|
|
4270
|
+
...kem,
|
|
4218
4271
|
pq: { algorithm: { name: 'ML-KEM-768' }, Nseed: 64, Npk: 1184, Nct: 1088 },
|
|
4219
4272
|
t: {
|
|
4220
4273
|
algorithm: { name: 'X25519' },
|
|
@@ -4231,8 +4284,7 @@ export const KEM_MLKEM768_X25519 = function () {
|
|
|
4231
4284
|
},
|
|
4232
4285
|
},
|
|
4233
4286
|
label: Uint8Array.of(0x5c, 0x2e, 0x2f, 0x2f, 0x5e, 0x5c),
|
|
4234
|
-
|
|
4235
|
-
}
|
|
4287
|
+
})
|
|
4236
4288
|
}
|
|
4237
4289
|
|
|
4238
4290
|
|
|
@@ -4254,23 +4306,26 @@ export const KEM_MLKEM768_X25519 = function () {
|
|
|
4254
4306
|
|
|
4255
4307
|
|
|
4256
4308
|
|
|
4257
|
-
|
|
4309
|
+
|
|
4310
|
+
|
|
4311
|
+
|
|
4312
|
+
|
|
4313
|
+
|
|
4314
|
+
export const KEM_MLKEM768_P256 = function () {
|
|
4258
4315
|
const id = 0x0050
|
|
4259
4316
|
const name = 'MLKEM768-P256'
|
|
4260
|
-
const
|
|
4317
|
+
const Nsecret = 32
|
|
4318
|
+
const Nenc = 1153
|
|
4319
|
+
const Npk = 1249
|
|
4320
|
+
const Nsk = 32
|
|
4321
|
+
const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
|
|
4322
|
+
|
|
4323
|
+
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4324
|
+
return createDirectKEM(kem)
|
|
4325
|
+
}
|
|
4261
4326
|
|
|
4262
|
-
|
|
4263
|
-
|
|
4264
|
-
id,
|
|
4265
|
-
kdf,
|
|
4266
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
4267
|
-
type: 'KEM',
|
|
4268
|
-
name,
|
|
4269
|
-
Nsecret: 32,
|
|
4270
|
-
Nenc: 1153,
|
|
4271
|
-
Npk: 1249,
|
|
4272
|
-
Nsk: 32,
|
|
4273
|
-
algorithm: { name: 'MLKEM768-P256' },
|
|
4327
|
+
return createCompositeHybridKEM({
|
|
4328
|
+
...kem,
|
|
4274
4329
|
pq: { algorithm: { name: 'ML-KEM-768' }, Nseed: 64, Npk: 1184, Nct: 1088 },
|
|
4275
4330
|
t: {
|
|
4276
4331
|
...P256,
|
|
@@ -4287,8 +4342,7 @@ export const KEM_MLKEM768_P256 = function () {
|
|
|
4287
4342
|
},
|
|
4288
4343
|
},
|
|
4289
4344
|
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x37, 0x36, 0x38, 0x2d, 0x50, 0x32, 0x35, 0x36),
|
|
4290
|
-
|
|
4291
|
-
}
|
|
4345
|
+
})
|
|
4292
4346
|
}
|
|
4293
4347
|
|
|
4294
4348
|
|
|
@@ -4310,23 +4364,26 @@ export const KEM_MLKEM768_P256 = function () {
|
|
|
4310
4364
|
|
|
4311
4365
|
|
|
4312
4366
|
|
|
4313
|
-
|
|
4367
|
+
|
|
4368
|
+
|
|
4369
|
+
|
|
4370
|
+
|
|
4371
|
+
|
|
4372
|
+
export const KEM_MLKEM1024_P384 = function () {
|
|
4314
4373
|
const id = 0x0051
|
|
4315
4374
|
const name = 'MLKEM1024-P384'
|
|
4316
|
-
const
|
|
4375
|
+
const Nsecret = 32
|
|
4376
|
+
const Nenc = 1665
|
|
4377
|
+
const Npk = 1665
|
|
4378
|
+
const Nsk = 32
|
|
4379
|
+
const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
|
|
4380
|
+
|
|
4381
|
+
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4382
|
+
return createDirectKEM(kem)
|
|
4383
|
+
}
|
|
4317
4384
|
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
id,
|
|
4321
|
-
kdf,
|
|
4322
|
-
suite_id: concat(L_KEM, I2OSP(id, 2)),
|
|
4323
|
-
type: 'KEM',
|
|
4324
|
-
name,
|
|
4325
|
-
Nsecret: 32,
|
|
4326
|
-
Nenc: 1665,
|
|
4327
|
-
Npk: 1665,
|
|
4328
|
-
Nsk: 32,
|
|
4329
|
-
algorithm: { name: 'MLKEM1024-P384' },
|
|
4385
|
+
return createCompositeHybridKEM({
|
|
4386
|
+
...kem,
|
|
4330
4387
|
pq: { algorithm: { name: 'ML-KEM-1024' }, Nseed: 64, Npk: 1568, Nct: 1568 },
|
|
4331
4388
|
t: {
|
|
4332
4389
|
...P384,
|
|
@@ -4344,6 +4401,5 @@ export const KEM_MLKEM1024_P384 = function () {
|
|
|
4344
4401
|
},
|
|
4345
4402
|
},
|
|
4346
4403
|
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x31, 0x30, 0x32, 0x34, 0x2d, 0x50, 0x33, 0x38, 0x34),
|
|
4347
|
-
|
|
4348
|
-
}
|
|
4404
|
+
})
|
|
4349
4405
|
}
|