hpke 1.1.1 → 1.1.3
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 +12 -12
- package/index.d.ts +10 -2
- package/index.d.ts.map +1 -1
- package/index.js +161 -105
- package/index.ts +168 -112
- package/package.json +11 -11
package/index.js
CHANGED
|
@@ -57,12 +57,15 @@ function ComputeNonce(base_nonce , seq , Nn )
|
|
|
57
57
|
return nonce
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
function MaxSeq(Nn ) {
|
|
60
61
|
|
|
61
|
-
function IncrementSeq(seq ) {
|
|
62
62
|
|
|
63
|
+
return Math.min(2 ** (8 * Nn) - 1, Number.MAX_SAFE_INTEGER)
|
|
64
|
+
}
|
|
63
65
|
|
|
64
66
|
|
|
65
|
-
|
|
67
|
+
function IncrementSeq(seq , maxSeq ) {
|
|
68
|
+
if (seq >= maxSeq) {
|
|
66
69
|
throw new MessageLimitReachedError('Sequence number overflow')
|
|
67
70
|
}
|
|
68
71
|
return ++seq
|
|
@@ -80,6 +83,9 @@ async function ContextExport(
|
|
|
80
83
|
if (!Number.isInteger(L) || L <= 0 || L > 0xffff) {
|
|
81
84
|
throw new TypeError('"L" must be a positive integer not exceeding 65535')
|
|
82
85
|
}
|
|
86
|
+
if (stages === 2 && L > 255 * suite.KDF.Nh) {
|
|
87
|
+
throw new TypeError('"L" must not exceed 255*Nh of the cipher suite KDF')
|
|
88
|
+
}
|
|
83
89
|
const Export = stages === 1 ? Export_OneStage : Export_TwoStage
|
|
84
90
|
return await Export(suite.KDF, suite.id, exporterSecret, exporterContext, L)
|
|
85
91
|
}
|
|
@@ -125,6 +131,7 @@ class SenderContext {
|
|
|
125
131
|
#exporter_secret
|
|
126
132
|
#mode
|
|
127
133
|
#seq = 0
|
|
134
|
+
#max_seq
|
|
128
135
|
#mutex
|
|
129
136
|
|
|
130
137
|
constructor(
|
|
@@ -139,6 +146,7 @@ class SenderContext {
|
|
|
139
146
|
this.#key = key
|
|
140
147
|
this.#base_nonce = base_nonce
|
|
141
148
|
this.#exporter_secret = exporter_secret
|
|
149
|
+
this.#max_seq = MaxSeq(suite.AEAD.Nn)
|
|
142
150
|
}
|
|
143
151
|
|
|
144
152
|
|
|
@@ -155,6 +163,7 @@ class SenderContext {
|
|
|
155
163
|
|
|
156
164
|
|
|
157
165
|
|
|
166
|
+
|
|
158
167
|
get seq() {
|
|
159
168
|
return this.#seq
|
|
160
169
|
}
|
|
@@ -203,7 +212,7 @@ class SenderContext {
|
|
|
203
212
|
aad,
|
|
204
213
|
plaintext,
|
|
205
214
|
)
|
|
206
|
-
this.#seq = IncrementSeq(this.#seq)
|
|
215
|
+
this.#seq = IncrementSeq(this.#seq, this.#max_seq)
|
|
207
216
|
return ct
|
|
208
217
|
} finally {
|
|
209
218
|
release()
|
|
@@ -273,6 +282,7 @@ class RecipientContext {
|
|
|
273
282
|
#exporter_secret
|
|
274
283
|
#mode
|
|
275
284
|
#seq = 0
|
|
285
|
+
#max_seq
|
|
276
286
|
#mutex
|
|
277
287
|
|
|
278
288
|
constructor(
|
|
@@ -287,6 +297,7 @@ class RecipientContext {
|
|
|
287
297
|
this.#key = key
|
|
288
298
|
this.#base_nonce = base_nonce
|
|
289
299
|
this.#exporter_secret = exporter_secret
|
|
300
|
+
this.#max_seq = MaxSeq(suite.AEAD.Nn)
|
|
290
301
|
}
|
|
291
302
|
|
|
292
303
|
|
|
@@ -303,6 +314,7 @@ class RecipientContext {
|
|
|
303
314
|
|
|
304
315
|
|
|
305
316
|
|
|
317
|
+
|
|
306
318
|
get seq() {
|
|
307
319
|
return this.#seq
|
|
308
320
|
}
|
|
@@ -361,7 +373,7 @@ class RecipientContext {
|
|
|
361
373
|
|
|
362
374
|
throw new OpenError('AEAD decryption failed', { cause })
|
|
363
375
|
}
|
|
364
|
-
this.#seq = IncrementSeq(this.#seq)
|
|
376
|
+
this.#seq = IncrementSeq(this.#seq, this.#max_seq)
|
|
365
377
|
return pt
|
|
366
378
|
} finally {
|
|
367
379
|
release()
|
|
@@ -1586,6 +1598,9 @@ function checkUint8Array(input , name )
|
|
|
1586
1598
|
if (!(input instanceof Uint8Array)) {
|
|
1587
1599
|
throw new TypeError(`"${name}" must be Uint8Array`)
|
|
1588
1600
|
}
|
|
1601
|
+
if (typeof SharedArrayBuffer !== 'undefined' && input.buffer instanceof SharedArrayBuffer) {
|
|
1602
|
+
throw new TypeError(`"${name}" must not be backed by a SharedArrayBuffer`)
|
|
1603
|
+
}
|
|
1589
1604
|
}
|
|
1590
1605
|
function checkExtractable(extractable ) {
|
|
1591
1606
|
if (typeof extractable !== 'boolean') {
|
|
@@ -2182,6 +2197,12 @@ function isKey(key , type , extractable )
|
|
|
2182
2197
|
|
|
2183
2198
|
|
|
2184
2199
|
|
|
2200
|
+
|
|
2201
|
+
|
|
2202
|
+
|
|
2203
|
+
|
|
2204
|
+
|
|
2205
|
+
|
|
2185
2206
|
|
|
2186
2207
|
|
|
2187
2208
|
|
|
@@ -2260,6 +2281,8 @@ const NotApplicable = () => {
|
|
|
2260
2281
|
}
|
|
2261
2282
|
|
|
2262
2283
|
const EXPORT_ONLY = 0xffff
|
|
2284
|
+
const AES_GCM_P_MAX = 2 ** 36 - 31
|
|
2285
|
+
const CHACHA20_POLY1305_P_MAX = 2 ** 38 - 64
|
|
2263
2286
|
|
|
2264
2287
|
|
|
2265
2288
|
|
|
@@ -2309,57 +2332,51 @@ async function subtle (promise , name
|
|
|
2309
2332
|
|
|
2310
2333
|
|
|
2311
2334
|
|
|
2312
|
-
function
|
|
2313
|
-
|
|
2335
|
+
async function cacheValue (
|
|
2336
|
+
cache ,
|
|
2337
|
+
key ,
|
|
2338
|
+
init ,
|
|
2339
|
+
) {
|
|
2340
|
+
const result = await init()
|
|
2341
|
+
cache.set(key, result)
|
|
2342
|
+
return result
|
|
2314
2343
|
}
|
|
2315
2344
|
|
|
2316
|
-
function
|
|
2317
|
-
|
|
2318
|
-
|
|
2345
|
+
function HKDF_SHARED() {
|
|
2346
|
+
let emptySalt
|
|
2347
|
+
async function importKey( salt ) {
|
|
2348
|
+
return await subtle(
|
|
2349
|
+
(c) => c.importKey('raw', salt, { name: 'HMAC', hash: this.hash }, false, ['sign']),
|
|
2350
|
+
this.name,
|
|
2351
|
+
)
|
|
2319
2352
|
}
|
|
2320
|
-
|
|
2321
|
-
|
|
2353
|
+
const cache = new WeakMap ()
|
|
2354
|
+
function importPrk( prk ) {
|
|
2355
|
+
const key = importKey.call(this, prk )
|
|
2356
|
+
cache.set(prk, key)
|
|
2357
|
+
return key
|
|
2322
2358
|
}
|
|
2323
|
-
return input.buffer.slice(input.byteOffset, input.byteOffset + input.byteLength)
|
|
2324
|
-
}
|
|
2325
|
-
|
|
2326
|
-
function HKDF_SHARED() {
|
|
2327
2359
|
return {
|
|
2328
2360
|
stages: 2,
|
|
2329
2361
|
Derive: NotApplicable,
|
|
2330
|
-
async Extract(
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
salt = ab(_salt)
|
|
2336
|
-
}
|
|
2337
|
-
const ikm = ab(_ikm)
|
|
2362
|
+
async Extract( salt, ikm) {
|
|
2363
|
+
const key =
|
|
2364
|
+
salt.byteLength === 0
|
|
2365
|
+
? (emptySalt ??= await importKey.call(this, new ArrayBuffer(this.Nh)))
|
|
2366
|
+
: await importKey.call(this, salt )
|
|
2338
2367
|
return new Uint8Array(
|
|
2339
|
-
await subtle(
|
|
2340
|
-
async (c) =>
|
|
2341
|
-
c.sign(
|
|
2342
|
-
'HMAC',
|
|
2343
|
-
await c.importKey('raw', salt, { name: 'HMAC', hash: this.hash }, false, ['sign']),
|
|
2344
|
-
ikm,
|
|
2345
|
-
),
|
|
2346
|
-
this.name,
|
|
2347
|
-
),
|
|
2368
|
+
await subtle((c) => c.sign('HMAC', key, ikm ), this.name),
|
|
2348
2369
|
)
|
|
2349
2370
|
},
|
|
2350
|
-
async Expand(
|
|
2351
|
-
if (
|
|
2371
|
+
async Expand( prk, info, L) {
|
|
2372
|
+
if (prk.byteLength < this.Nh) {
|
|
2352
2373
|
throw new Error('prk.byteLength < this.Nh')
|
|
2353
2374
|
}
|
|
2354
2375
|
if (L > 255 * this.Nh) {
|
|
2355
2376
|
throw new Error('L must be <= 255*Nh')
|
|
2356
2377
|
}
|
|
2357
2378
|
const N = Math.ceil(L / this.Nh)
|
|
2358
|
-
const
|
|
2359
|
-
const key = await subtle(
|
|
2360
|
-
(c) => c.importKey('raw', prk, { name: 'HMAC', hash: this.hash }, false, ['sign']),
|
|
2361
|
-
this.name,
|
|
2362
|
-
)
|
|
2379
|
+
const key = await (cache.get(prk) ?? importPrk.call(this, prk))
|
|
2363
2380
|
|
|
2364
2381
|
const T = new Uint8Array(N * this.Nh)
|
|
2365
2382
|
let T_prev = new Uint8Array()
|
|
@@ -2462,7 +2479,7 @@ export const KDF_HKDF_SHA512 = function () {
|
|
|
2462
2479
|
|
|
2463
2480
|
|
|
2464
2481
|
|
|
2465
|
-
async function ShakeDerive(name , variant , ikm
|
|
2482
|
+
async function ShakeDerive(name , variant , ikm , L ) {
|
|
2466
2483
|
const bits = L << 3
|
|
2467
2484
|
const alg = { name: variant, length: bits, outputLength: bits }
|
|
2468
2485
|
return new Uint8Array(await subtle((c) => c.digest(alg, ikm), name))
|
|
@@ -2472,7 +2489,7 @@ function SHAKE_SHARED() {
|
|
|
2472
2489
|
return {
|
|
2473
2490
|
stages: 1,
|
|
2474
2491
|
async Derive( labeled_ikm, L ) {
|
|
2475
|
-
return await ShakeDerive(this.name, this.algorithm,
|
|
2492
|
+
return await ShakeDerive(this.name, this.algorithm, labeled_ikm , L)
|
|
2476
2493
|
},
|
|
2477
2494
|
Extract: NotApplicable,
|
|
2478
2495
|
Expand: NotApplicable,
|
|
@@ -2693,21 +2710,18 @@ function b64u(input ) {
|
|
|
2693
2710
|
|
|
2694
2711
|
|
|
2695
2712
|
|
|
2696
|
-
async function
|
|
2713
|
+
async function DeriveKeyPairBytes(
|
|
2697
2714
|
DHKEM ,
|
|
2698
|
-
suite_id ,
|
|
2699
2715
|
ikm ,
|
|
2700
|
-
|
|
2716
|
+
label ,
|
|
2717
|
+
context ,
|
|
2701
2718
|
) {
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
I2OSP(counter, 1),
|
|
2709
|
-
DHKEM.Nsk,
|
|
2710
|
-
)
|
|
2719
|
+
if (KDFStages(DHKEM.kdf) === 1) {
|
|
2720
|
+
return await LabeledDerive(DHKEM.kdf, DHKEM.suite_id, ikm, label, context, DHKEM.Nsk)
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2723
|
+
const dkp_prk = await LabeledExtract(DHKEM.kdf, DHKEM.suite_id, new Uint8Array(), L_dkp_prk, ikm)
|
|
2724
|
+
return await LabeledExpand(DHKEM.kdf, DHKEM.suite_id, dkp_prk, label, context, DHKEM.Nsk)
|
|
2711
2725
|
}
|
|
2712
2726
|
|
|
2713
2727
|
function OS2IP(x ) {
|
|
@@ -2753,6 +2767,22 @@ function assertCryptoKey(key ) {
|
|
|
2753
2767
|
}
|
|
2754
2768
|
|
|
2755
2769
|
|
|
2770
|
+
async function ExtractAndExpand_OneStage(
|
|
2771
|
+
DHKEM ,
|
|
2772
|
+
dh ,
|
|
2773
|
+
kem_context ,
|
|
2774
|
+
) {
|
|
2775
|
+
return await LabeledDerive(
|
|
2776
|
+
DHKEM.kdf,
|
|
2777
|
+
DHKEM.suite_id,
|
|
2778
|
+
dh,
|
|
2779
|
+
L_shared_secret,
|
|
2780
|
+
kem_context,
|
|
2781
|
+
DHKEM.Nsecret,
|
|
2782
|
+
)
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2785
|
+
|
|
2756
2786
|
async function ExtractAndExpand_TwoStage(
|
|
2757
2787
|
DHKEM ,
|
|
2758
2788
|
dh ,
|
|
@@ -2770,6 +2800,16 @@ async function ExtractAndExpand_TwoStage(
|
|
|
2770
2800
|
}
|
|
2771
2801
|
|
|
2772
2802
|
|
|
2803
|
+
async function ExtractAndExpand(
|
|
2804
|
+
DHKEM ,
|
|
2805
|
+
dh ,
|
|
2806
|
+
kem_context ,
|
|
2807
|
+
) {
|
|
2808
|
+
const Fn = KDFStages(DHKEM.kdf) === 1 ? ExtractAndExpand_OneStage : ExtractAndExpand_TwoStage
|
|
2809
|
+
return await Fn(DHKEM, dh, kem_context)
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
|
|
2773
2813
|
|
|
2774
2814
|
|
|
2775
2815
|
|
|
@@ -2786,9 +2826,11 @@ function DHKEM_SHARED()
|
|
|
2786
2826
|
assertCryptoKey(key)
|
|
2787
2827
|
return new Uint8Array(await subtle((c) => c.exportKey('raw', key), this.name))
|
|
2788
2828
|
},
|
|
2789
|
-
async DeserializePublicKey(
|
|
2790
|
-
|
|
2791
|
-
|
|
2829
|
+
async DeserializePublicKey( key) {
|
|
2830
|
+
return await subtle(
|
|
2831
|
+
(c) => c.importKey('raw', key , this.algorithm, true, []),
|
|
2832
|
+
this.name,
|
|
2833
|
+
)
|
|
2792
2834
|
},
|
|
2793
2835
|
async SerializePrivateKey( key) {
|
|
2794
2836
|
assertKeyAlgorithm(key, this.algorithm)
|
|
@@ -2817,7 +2859,7 @@ function DHKEM_SHARED()
|
|
|
2817
2859
|
const enc = await this.SerializePublicKey(pkE)
|
|
2818
2860
|
const pkRm = await this.SerializePublicKey(pkR)
|
|
2819
2861
|
const kem_context = concat(enc, pkRm)
|
|
2820
|
-
const shared_secret = await
|
|
2862
|
+
const shared_secret = await ExtractAndExpand(this, dh, kem_context)
|
|
2821
2863
|
return { shared_secret, enc }
|
|
2822
2864
|
},
|
|
2823
2865
|
async Decap( enc, skR, pkR) {
|
|
@@ -2844,7 +2886,7 @@ function DHKEM_SHARED()
|
|
|
2844
2886
|
|
|
2845
2887
|
const pkRm = await this.SerializePublicKey(pkR)
|
|
2846
2888
|
const kem_context = concat(enc, pkRm)
|
|
2847
|
-
const shared_secret = await
|
|
2889
|
+
const shared_secret = await ExtractAndExpand(this, dh, kem_context)
|
|
2848
2890
|
return shared_secret
|
|
2849
2891
|
},
|
|
2850
2892
|
}
|
|
@@ -3068,7 +3110,7 @@ async function DeriveKeyPairNist(
|
|
|
3068
3110
|
if (counter > 255) {
|
|
3069
3111
|
throw new DeriveKeyPairError('Key derivation exceeded maximum iterations')
|
|
3070
3112
|
}
|
|
3071
|
-
candidate = await
|
|
3113
|
+
candidate = await DeriveKeyPairBytes(this, ikm, L_candidate, I2OSP(counter, 1))
|
|
3072
3114
|
candidate[0] = candidate[0] & this.bitmask
|
|
3073
3115
|
sk = OS2IP(candidate)
|
|
3074
3116
|
counter = counter + 1
|
|
@@ -3105,8 +3147,7 @@ async function GetKeyPairNist(
|
|
|
3105
3147
|
|
|
3106
3148
|
|
|
3107
3149
|
async function DeriveKeyPairX( ikm , extractable ) {
|
|
3108
|
-
const
|
|
3109
|
-
const sk = await LabeledExpand(this.kdf, this.suite_id, dkp_prk, L_sk, new Uint8Array(), this.Nsk)
|
|
3150
|
+
const sk = await DeriveKeyPairBytes(this, ikm, L_sk, new Uint8Array())
|
|
3110
3151
|
return await createKeyPairFromPrivateKey(this, sk, extractable)
|
|
3111
3152
|
}
|
|
3112
3153
|
|
|
@@ -3419,13 +3460,15 @@ function MLKEM_SHARED() {
|
|
|
3419
3460
|
const format = 'raw-public'
|
|
3420
3461
|
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3421
3462
|
},
|
|
3422
|
-
async DeserializePublicKey(
|
|
3463
|
+
async DeserializePublicKey( key) {
|
|
3423
3464
|
|
|
3424
3465
|
const format = 'raw-public'
|
|
3425
3466
|
|
|
3426
3467
|
const usages = ['encapsulateBits']
|
|
3427
|
-
|
|
3428
|
-
|
|
3468
|
+
return await subtle(
|
|
3469
|
+
(c) => c.importKey(format, key , this.algorithm, true, usages),
|
|
3470
|
+
this.name,
|
|
3471
|
+
)
|
|
3429
3472
|
},
|
|
3430
3473
|
async SerializePrivateKey( key) {
|
|
3431
3474
|
assertKeyAlgorithm(key, this.algorithm)
|
|
@@ -3434,14 +3477,13 @@ function MLKEM_SHARED() {
|
|
|
3434
3477
|
const format = 'raw-seed'
|
|
3435
3478
|
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3436
3479
|
},
|
|
3437
|
-
async DeserializePrivateKey(
|
|
3480
|
+
async DeserializePrivateKey( key, extractable) {
|
|
3438
3481
|
|
|
3439
3482
|
const format = 'raw-seed'
|
|
3440
3483
|
|
|
3441
3484
|
const usages = ['decapsulateBits']
|
|
3442
|
-
const key = ab(_key)
|
|
3443
3485
|
return await subtle(
|
|
3444
|
-
(c) => c.importKey(format, key, this.algorithm, extractable, usages),
|
|
3486
|
+
(c) => c.importKey(format, key , this.algorithm, extractable, usages),
|
|
3445
3487
|
this.name,
|
|
3446
3488
|
)
|
|
3447
3489
|
},
|
|
@@ -3456,13 +3498,12 @@ function MLKEM_SHARED() {
|
|
|
3456
3498
|
|
|
3457
3499
|
return { shared_secret: new Uint8Array(sharedKey), enc: new Uint8Array(ciphertext) }
|
|
3458
3500
|
},
|
|
3459
|
-
async Decap(
|
|
3501
|
+
async Decap( enc, skR, _pkR) {
|
|
3460
3502
|
assertKeyAlgorithm(skR, this.algorithm)
|
|
3461
|
-
const enc = ab(_enc)
|
|
3462
3503
|
return new Uint8Array(
|
|
3463
3504
|
await subtle(
|
|
3464
3505
|
|
|
3465
|
-
(c) => c.decapsulateBits(this.algorithm, skR, enc),
|
|
3506
|
+
(c) => c.decapsulateBits(this.algorithm, skR, enc ),
|
|
3466
3507
|
this.name,
|
|
3467
3508
|
),
|
|
3468
3509
|
)
|
|
@@ -3605,43 +3646,61 @@ export const KEM_ML_KEM_1024 = function () {
|
|
|
3605
3646
|
|
|
3606
3647
|
|
|
3607
3648
|
|
|
3608
|
-
function AEAD_SHARED() {
|
|
3649
|
+
function AEAD_SHARED(P_MAX ) {
|
|
3609
3650
|
|
|
3610
3651
|
|
|
3611
3652
|
|
|
3612
3653
|
|
|
3613
3654
|
|
|
3614
3655
|
const cache = new WeakMap ()
|
|
3615
|
-
async function importKey(
|
|
3616
|
-
|
|
3617
|
-
|
|
3618
|
-
|
|
3656
|
+
async function importKey( key ) {
|
|
3657
|
+
return await subtle(
|
|
3658
|
+
(c) =>
|
|
3659
|
+
c.importKey(this.keyFormat, key , this.algorithm, false, [
|
|
3660
|
+
'encrypt',
|
|
3661
|
+
'decrypt',
|
|
3662
|
+
]),
|
|
3619
3663
|
this.name,
|
|
3620
3664
|
)
|
|
3621
|
-
cache.set(_key, cryptoKey)
|
|
3622
|
-
return cryptoKey
|
|
3623
3665
|
}
|
|
3624
3666
|
return {
|
|
3625
|
-
async Seal(
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3629
|
-
const cryptoKey =
|
|
3667
|
+
async Seal( key, nonce, aad, pt) {
|
|
3668
|
+
if (pt.byteLength > P_MAX) {
|
|
3669
|
+
throw new RangeError('"pt" exceeds P_MAX')
|
|
3670
|
+
}
|
|
3671
|
+
const cryptoKey =
|
|
3672
|
+
cache.get(key) ?? (await cacheValue(cache, key, () => importKey.call(this, key)))
|
|
3630
3673
|
return new Uint8Array(
|
|
3631
3674
|
await subtle(
|
|
3632
|
-
(c) =>
|
|
3675
|
+
(c) =>
|
|
3676
|
+
c.encrypt(
|
|
3677
|
+
{
|
|
3678
|
+
name: this.algorithm,
|
|
3679
|
+
iv: nonce ,
|
|
3680
|
+
additionalData: aad ,
|
|
3681
|
+
},
|
|
3682
|
+
cryptoKey,
|
|
3683
|
+
pt ,
|
|
3684
|
+
),
|
|
3633
3685
|
this.name,
|
|
3634
3686
|
),
|
|
3635
3687
|
)
|
|
3636
3688
|
},
|
|
3637
|
-
async Open(
|
|
3638
|
-
const
|
|
3639
|
-
|
|
3640
|
-
const ct = ab(_ct)
|
|
3641
|
-
const cryptoKey = cache.get(_key) ?? (await importKey.call(this, _key))
|
|
3689
|
+
async Open( key, nonce, aad, ct) {
|
|
3690
|
+
const cryptoKey =
|
|
3691
|
+
cache.get(key) ?? (await cacheValue(cache, key, () => importKey.call(this, key)))
|
|
3642
3692
|
return new Uint8Array(
|
|
3643
3693
|
await subtle(
|
|
3644
|
-
(c) =>
|
|
3694
|
+
(c) =>
|
|
3695
|
+
c.decrypt(
|
|
3696
|
+
{
|
|
3697
|
+
name: this.algorithm,
|
|
3698
|
+
iv: nonce ,
|
|
3699
|
+
additionalData: aad ,
|
|
3700
|
+
},
|
|
3701
|
+
cryptoKey,
|
|
3702
|
+
ct ,
|
|
3703
|
+
),
|
|
3645
3704
|
this.name,
|
|
3646
3705
|
),
|
|
3647
3706
|
)
|
|
@@ -3681,7 +3740,7 @@ export const AEAD_AES_128_GCM = function () {
|
|
|
3681
3740
|
Nt: 16,
|
|
3682
3741
|
algorithm: 'AES-GCM',
|
|
3683
3742
|
keyFormat: 'raw',
|
|
3684
|
-
...AEAD_SHARED(),
|
|
3743
|
+
...AEAD_SHARED(AES_GCM_P_MAX),
|
|
3685
3744
|
}
|
|
3686
3745
|
}
|
|
3687
3746
|
|
|
@@ -3713,7 +3772,7 @@ export const AEAD_AES_256_GCM = function () {
|
|
|
3713
3772
|
Nt: 16,
|
|
3714
3773
|
algorithm: 'AES-GCM',
|
|
3715
3774
|
keyFormat: 'raw',
|
|
3716
|
-
...AEAD_SHARED(),
|
|
3775
|
+
...AEAD_SHARED(AES_GCM_P_MAX),
|
|
3717
3776
|
}
|
|
3718
3777
|
}
|
|
3719
3778
|
|
|
@@ -3746,7 +3805,7 @@ export const AEAD_ChaCha20Poly1305 = function AEAD_ChaCha20Poly1305
|
|
|
3746
3805
|
algorithm: 'ChaCha20-Poly1305',
|
|
3747
3806
|
|
|
3748
3807
|
keyFormat: 'raw-secret',
|
|
3749
|
-
...AEAD_SHARED(),
|
|
3808
|
+
...AEAD_SHARED(CHACHA20_POLY1305_P_MAX),
|
|
3750
3809
|
}
|
|
3751
3810
|
}
|
|
3752
3811
|
|
|
@@ -3869,23 +3928,21 @@ function RandomScalarNist(t , seed ) {
|
|
|
3869
3928
|
}
|
|
3870
3929
|
|
|
3871
3930
|
|
|
3872
|
-
async function expandDecapsKeyG(PQTKEM ,
|
|
3931
|
+
async function expandDecapsKeyG(PQTKEM , seed ) {
|
|
3873
3932
|
const Nout = PQTKEM.pq.Nseed + PQTKEM.t.Nseed
|
|
3874
3933
|
const bits = Nout << 3
|
|
3875
3934
|
|
|
3876
3935
|
const algorithm = { name: 'cSHAKE256', length: bits, outputLength: bits }
|
|
3877
|
-
const
|
|
3878
|
-
const seed_full = await subtle((c) => c.digest(algorithm, seed), PQTKEM.name)
|
|
3936
|
+
const seed_full = await subtle((c) => c.digest(algorithm, seed ), PQTKEM.name)
|
|
3879
3937
|
|
|
3880
|
-
const [
|
|
3881
|
-
const seed_PQ = ab(_seed_PQ)
|
|
3938
|
+
const [seed_PQ, seed_T] = split(PQTKEM.pq.Nseed, PQTKEM.t.Nseed, new Uint8Array(seed_full))
|
|
3882
3939
|
|
|
3883
3940
|
|
|
3884
3941
|
const format = 'raw-seed'
|
|
3885
3942
|
|
|
3886
3943
|
const usages = ['decapsulateBits', 'encapsulateBits']
|
|
3887
3944
|
const dk_PQ = await subtle(
|
|
3888
|
-
(c) => c.importKey(format, seed_PQ, PQTKEM.pq.algorithm, true, [usages[0]]),
|
|
3945
|
+
(c) => c.importKey(format, seed_PQ , PQTKEM.pq.algorithm, true, [usages[0]]),
|
|
3889
3946
|
PQTKEM.name,
|
|
3890
3947
|
)
|
|
3891
3948
|
const ek_PQ = await getPublicKey(PQTKEM.name, dk_PQ, [usages[1]])
|
|
@@ -3906,7 +3963,7 @@ async function C2PRICombiner(
|
|
|
3906
3963
|
label ,
|
|
3907
3964
|
) {
|
|
3908
3965
|
const ek_T = new Uint8Array(await subtle((c) => c.exportKey('raw', _ek_T), PQTKEM.name))
|
|
3909
|
-
const data =
|
|
3966
|
+
const data = concat(ss_PQ, ss_T, ct_T, ek_T, label)
|
|
3910
3967
|
return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data), PQTKEM.name))
|
|
3911
3968
|
}
|
|
3912
3969
|
|
|
@@ -3925,7 +3982,7 @@ async function prepareEncapsG(
|
|
|
3925
3982
|
const ct_PQ = new Uint8Array(res.ciphertext)
|
|
3926
3983
|
|
|
3927
3984
|
const { privateKey: sk_E, publicKey } = (await subtle(
|
|
3928
|
-
(c) => c.generateKey(PQTKEM.t.algorithm,
|
|
3985
|
+
(c) => c.generateKey(PQTKEM.t.algorithm, false, ['deriveBits']),
|
|
3929
3986
|
PQTKEM.name,
|
|
3930
3987
|
))
|
|
3931
3988
|
const ct_T = new Uint8Array(await subtle((c) => c.exportKey('raw', publicKey), PQTKEM.name))
|
|
@@ -3947,7 +4004,7 @@ async function prepareDecapsG(
|
|
|
3947
4004
|
dk_PQ ,
|
|
3948
4005
|
dk_T ,
|
|
3949
4006
|
ct_PQ ,
|
|
3950
|
-
|
|
4007
|
+
ct_T ,
|
|
3951
4008
|
) {
|
|
3952
4009
|
const ss_PQ = new Uint8Array(
|
|
3953
4010
|
await subtle(
|
|
@@ -3957,9 +4014,8 @@ async function prepareDecapsG(
|
|
|
3957
4014
|
),
|
|
3958
4015
|
)
|
|
3959
4016
|
|
|
3960
|
-
const ct_T = ab(_ct_T)
|
|
3961
4017
|
const pub = await subtle(
|
|
3962
|
-
(c) => c.importKey('raw', ct_T, PQTKEM.t.algorithm, true, []),
|
|
4018
|
+
(c) => c.importKey('raw', ct_T , PQTKEM.t.algorithm, true, []),
|
|
3963
4019
|
PQTKEM.name,
|
|
3964
4020
|
)
|
|
3965
4021
|
|
|
@@ -4053,8 +4109,8 @@ function PQTKEM_SHARED() {
|
|
|
4053
4109
|
const format = 'raw-public'
|
|
4054
4110
|
|
|
4055
4111
|
const usages = ['encapsulateBits']
|
|
4056
|
-
const pubPq =
|
|
4057
|
-
const pubT =
|
|
4112
|
+
const pubPq = key.subarray(0, this.pq.Npk)
|
|
4113
|
+
const pubT = key.subarray(this.pq.Npk)
|
|
4058
4114
|
const [ek_PQ, ek_T] = await Promise.all([
|
|
4059
4115
|
subtle((c) => c.importKey(format, pubPq, this.pq.algorithm, true, usages), this.name),
|
|
4060
4116
|
subtle((c) => c.importKey('raw', pubT, this.t.algorithm, true, []), this.name),
|