hpke 1.1.5 → 1.1.7
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.d.ts +9 -6
- package/index.d.ts.map +1 -1
- package/index.js +787 -816
- package/index.ts +239 -268
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -38,15 +38,15 @@
|
|
|
38
38
|
|
|
39
39
|
|
|
40
40
|
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
41
44
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
|
|
46
46
|
function ComputeNonce(base_nonce , seq , Nn ) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
50
|
const nonce = new Uint8Array(Nn)
|
|
51
51
|
nonce.set(base_nonce)
|
|
52
52
|
let s = seq
|
|
@@ -58,12 +58,12 @@ function ComputeNonce(base_nonce , seq , Nn )
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
function MaxSeq(Nn ) {
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
|
|
62
|
+
|
|
63
63
|
return Math.min(2 ** (8 * Nn) - 1, Number.MAX_SAFE_INTEGER)
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
|
|
67
67
|
function IncrementSeq(seq , maxSeq ) {
|
|
68
68
|
if (seq >= maxSeq) {
|
|
69
69
|
throw new MessageLimitReachedError('Sequence number overflow')
|
|
@@ -71,7 +71,7 @@ function IncrementSeq(seq , maxSeq ) {
|
|
|
71
71
|
return ++seq
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
|
|
75
75
|
async function ContextExport(
|
|
76
76
|
suite ,
|
|
77
77
|
exporterSecret ,
|
|
@@ -105,6 +105,36 @@ class Mutex {
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
function createContextState(
|
|
120
|
+
suite ,
|
|
121
|
+
mode ,
|
|
122
|
+
key ,
|
|
123
|
+
base_nonce ,
|
|
124
|
+
exporter_secret ,
|
|
125
|
+
) {
|
|
126
|
+
return {
|
|
127
|
+
suite,
|
|
128
|
+
mode,
|
|
129
|
+
key,
|
|
130
|
+
base_nonce,
|
|
131
|
+
exporter_secret,
|
|
132
|
+
max_seq: MaxSeq(suite.AEAD.Nn),
|
|
133
|
+
seq: 0,
|
|
134
|
+
mutex: undefined,
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
108
138
|
|
|
109
139
|
|
|
110
140
|
|
|
@@ -125,14 +155,7 @@ class Mutex {
|
|
|
125
155
|
|
|
126
156
|
|
|
127
157
|
class SenderContext {
|
|
128
|
-
#
|
|
129
|
-
#key
|
|
130
|
-
#base_nonce
|
|
131
|
-
#exporter_secret
|
|
132
|
-
#mode
|
|
133
|
-
#seq = 0
|
|
134
|
-
#max_seq
|
|
135
|
-
#mutex
|
|
158
|
+
#state
|
|
136
159
|
|
|
137
160
|
constructor(
|
|
138
161
|
suite ,
|
|
@@ -141,34 +164,29 @@ class SenderContext {
|
|
|
141
164
|
base_nonce ,
|
|
142
165
|
exporter_secret ,
|
|
143
166
|
) {
|
|
144
|
-
this.#
|
|
145
|
-
this.#mode = mode
|
|
146
|
-
this.#key = key
|
|
147
|
-
this.#base_nonce = base_nonce
|
|
148
|
-
this.#exporter_secret = exporter_secret
|
|
149
|
-
this.#max_seq = MaxSeq(suite.AEAD.Nn)
|
|
167
|
+
this.#state = createContextState(suite, mode, key, base_nonce, exporter_secret)
|
|
150
168
|
}
|
|
151
169
|
|
|
152
|
-
|
|
170
|
+
|
|
153
171
|
|
|
154
172
|
|
|
155
173
|
|
|
156
174
|
|
|
157
175
|
get mode() {
|
|
158
|
-
return this.#mode
|
|
176
|
+
return this.#state.mode
|
|
159
177
|
}
|
|
160
178
|
|
|
161
|
-
|
|
179
|
+
|
|
162
180
|
|
|
163
181
|
|
|
164
182
|
|
|
165
183
|
|
|
166
184
|
|
|
167
185
|
get seq() {
|
|
168
|
-
return this.#seq
|
|
186
|
+
return this.#state.seq
|
|
169
187
|
}
|
|
170
188
|
|
|
171
|
-
|
|
189
|
+
|
|
172
190
|
|
|
173
191
|
|
|
174
192
|
|
|
@@ -198,28 +216,29 @@ class SenderContext {
|
|
|
198
216
|
checkUint8Array(plaintext, 'plaintext')
|
|
199
217
|
aad ??= new Uint8Array()
|
|
200
218
|
checkUint8Array(aad, 'aad')
|
|
201
|
-
|
|
219
|
+
const state = this.#state
|
|
220
|
+
if (state.suite.AEAD.id === EXPORT_ONLY) {
|
|
202
221
|
throw new TypeError('Export-only AEAD cannot be used with Seal')
|
|
203
222
|
}
|
|
204
223
|
|
|
205
|
-
|
|
206
|
-
const release = await
|
|
224
|
+
state.mutex ??= new Mutex()
|
|
225
|
+
const release = await state.mutex.lock()
|
|
207
226
|
let ct
|
|
208
227
|
try {
|
|
209
|
-
ct = await
|
|
210
|
-
|
|
211
|
-
ComputeNonce(
|
|
228
|
+
ct = await state.suite.AEAD.Seal(
|
|
229
|
+
state.key,
|
|
230
|
+
ComputeNonce(state.base_nonce, state.seq, state.suite.AEAD.Nn),
|
|
212
231
|
aad,
|
|
213
232
|
plaintext,
|
|
214
233
|
)
|
|
215
|
-
|
|
234
|
+
state.seq = IncrementSeq(state.seq, state.max_seq)
|
|
216
235
|
return ct
|
|
217
236
|
} finally {
|
|
218
237
|
release()
|
|
219
238
|
}
|
|
220
239
|
}
|
|
221
240
|
|
|
222
|
-
|
|
241
|
+
|
|
223
242
|
|
|
224
243
|
|
|
225
244
|
|
|
@@ -243,18 +262,23 @@ class SenderContext {
|
|
|
243
262
|
|
|
244
263
|
|
|
245
264
|
async Export(exporterContext , length ) {
|
|
246
|
-
return await ContextExport(
|
|
265
|
+
return await ContextExport(
|
|
266
|
+
this.#state.suite,
|
|
267
|
+
this.#state.exporter_secret,
|
|
268
|
+
exporterContext,
|
|
269
|
+
length,
|
|
270
|
+
)
|
|
247
271
|
}
|
|
248
272
|
|
|
249
|
-
|
|
273
|
+
|
|
250
274
|
|
|
251
275
|
|
|
252
276
|
|
|
253
277
|
get Nt() {
|
|
254
|
-
return this.#suite.AEAD.Nt
|
|
278
|
+
return this.#state.suite.AEAD.Nt
|
|
255
279
|
}
|
|
256
280
|
}
|
|
257
|
-
|
|
281
|
+
|
|
258
282
|
|
|
259
283
|
|
|
260
284
|
|
|
@@ -276,14 +300,7 @@ class SenderContext {
|
|
|
276
300
|
|
|
277
301
|
|
|
278
302
|
class RecipientContext {
|
|
279
|
-
#
|
|
280
|
-
#key
|
|
281
|
-
#base_nonce
|
|
282
|
-
#exporter_secret
|
|
283
|
-
#mode
|
|
284
|
-
#seq = 0
|
|
285
|
-
#max_seq
|
|
286
|
-
#mutex
|
|
303
|
+
#state
|
|
287
304
|
|
|
288
305
|
constructor(
|
|
289
306
|
suite ,
|
|
@@ -292,34 +309,29 @@ class RecipientContext {
|
|
|
292
309
|
base_nonce ,
|
|
293
310
|
exporter_secret ,
|
|
294
311
|
) {
|
|
295
|
-
this.#
|
|
296
|
-
this.#mode = mode
|
|
297
|
-
this.#key = key
|
|
298
|
-
this.#base_nonce = base_nonce
|
|
299
|
-
this.#exporter_secret = exporter_secret
|
|
300
|
-
this.#max_seq = MaxSeq(suite.AEAD.Nn)
|
|
312
|
+
this.#state = createContextState(suite, mode, key, base_nonce, exporter_secret)
|
|
301
313
|
}
|
|
302
314
|
|
|
303
|
-
|
|
315
|
+
|
|
304
316
|
|
|
305
317
|
|
|
306
318
|
|
|
307
319
|
|
|
308
320
|
get mode() {
|
|
309
|
-
return this.#mode
|
|
321
|
+
return this.#state.mode
|
|
310
322
|
}
|
|
311
323
|
|
|
312
|
-
|
|
324
|
+
|
|
313
325
|
|
|
314
326
|
|
|
315
327
|
|
|
316
328
|
|
|
317
329
|
|
|
318
330
|
get seq() {
|
|
319
|
-
return this.#seq
|
|
331
|
+
return this.#state.seq
|
|
320
332
|
}
|
|
321
333
|
|
|
322
|
-
|
|
334
|
+
|
|
323
335
|
|
|
324
336
|
|
|
325
337
|
|
|
@@ -351,18 +363,19 @@ class RecipientContext {
|
|
|
351
363
|
aad ??= new Uint8Array()
|
|
352
364
|
checkUint8Array(aad, 'aad')
|
|
353
365
|
|
|
354
|
-
|
|
366
|
+
const state = this.#state
|
|
367
|
+
if (state.suite.AEAD.id === EXPORT_ONLY) {
|
|
355
368
|
throw new TypeError('Export-only AEAD cannot be used with Open')
|
|
356
369
|
}
|
|
357
370
|
|
|
358
|
-
|
|
359
|
-
const release = await
|
|
371
|
+
state.mutex ??= new Mutex()
|
|
372
|
+
const release = await state.mutex.lock()
|
|
360
373
|
try {
|
|
361
374
|
let pt
|
|
362
375
|
try {
|
|
363
|
-
pt = await
|
|
364
|
-
|
|
365
|
-
ComputeNonce(
|
|
376
|
+
pt = await state.suite.AEAD.Open(
|
|
377
|
+
state.key,
|
|
378
|
+
ComputeNonce(state.base_nonce, state.seq, state.suite.AEAD.Nn),
|
|
366
379
|
aad,
|
|
367
380
|
ciphertext,
|
|
368
381
|
)
|
|
@@ -373,14 +386,14 @@ class RecipientContext {
|
|
|
373
386
|
|
|
374
387
|
throw new OpenError('AEAD decryption failed', { cause })
|
|
375
388
|
}
|
|
376
|
-
|
|
389
|
+
state.seq = IncrementSeq(state.seq, state.max_seq)
|
|
377
390
|
return pt
|
|
378
391
|
} finally {
|
|
379
392
|
release()
|
|
380
393
|
}
|
|
381
394
|
}
|
|
382
395
|
|
|
383
|
-
|
|
396
|
+
|
|
384
397
|
|
|
385
398
|
|
|
386
399
|
|
|
@@ -404,14 +417,19 @@ class RecipientContext {
|
|
|
404
417
|
|
|
405
418
|
|
|
406
419
|
async Export(exporterContext , length ) {
|
|
407
|
-
return await ContextExport(
|
|
420
|
+
return await ContextExport(
|
|
421
|
+
this.#state.suite,
|
|
422
|
+
this.#state.exporter_secret,
|
|
423
|
+
exporterContext,
|
|
424
|
+
length,
|
|
425
|
+
)
|
|
408
426
|
}
|
|
409
427
|
}
|
|
428
|
+
|
|
410
429
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
415
433
|
|
|
416
434
|
const validate = (factory , type ) => {
|
|
417
435
|
try {
|
|
@@ -452,7 +470,7 @@ const validate = (factory , type )
|
|
|
452
470
|
export class CipherSuite {
|
|
453
471
|
#suite
|
|
454
472
|
|
|
455
|
-
|
|
473
|
+
|
|
456
474
|
|
|
457
475
|
|
|
458
476
|
|
|
@@ -526,24 +544,24 @@ export class CipherSuite {
|
|
|
526
544
|
}
|
|
527
545
|
}
|
|
528
546
|
|
|
529
|
-
|
|
547
|
+
|
|
530
548
|
|
|
531
549
|
|
|
532
550
|
|
|
533
551
|
|
|
534
552
|
get KEM()
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
547
565
|
{
|
|
548
566
|
return {
|
|
549
567
|
id: this.#suite.KEM.id,
|
|
@@ -555,28 +573,28 @@ export class CipherSuite {
|
|
|
555
573
|
}
|
|
556
574
|
}
|
|
557
575
|
|
|
558
|
-
|
|
576
|
+
|
|
559
577
|
|
|
560
578
|
|
|
561
579
|
|
|
562
580
|
|
|
563
581
|
get KDF()
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
580
598
|
{
|
|
581
599
|
return {
|
|
582
600
|
id: this.#suite.KDF.id,
|
|
@@ -586,23 +604,23 @@ export class CipherSuite {
|
|
|
586
604
|
}
|
|
587
605
|
}
|
|
588
606
|
|
|
589
|
-
|
|
607
|
+
|
|
590
608
|
|
|
591
609
|
|
|
592
610
|
|
|
593
611
|
|
|
594
612
|
|
|
595
613
|
get AEAD()
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
606
624
|
{
|
|
607
625
|
return {
|
|
608
626
|
id: this.#suite.AEAD.id,
|
|
@@ -613,7 +631,7 @@ export class CipherSuite {
|
|
|
613
631
|
}
|
|
614
632
|
}
|
|
615
633
|
|
|
616
|
-
|
|
634
|
+
|
|
617
635
|
|
|
618
636
|
|
|
619
637
|
|
|
@@ -636,7 +654,7 @@ export class CipherSuite {
|
|
|
636
654
|
return await this.#suite.KEM.GenerateKeyPair(extractable)
|
|
637
655
|
}
|
|
638
656
|
|
|
639
|
-
|
|
657
|
+
|
|
640
658
|
|
|
641
659
|
|
|
642
660
|
|
|
@@ -680,7 +698,7 @@ export class CipherSuite {
|
|
|
680
698
|
}
|
|
681
699
|
}
|
|
682
700
|
|
|
683
|
-
|
|
701
|
+
|
|
684
702
|
|
|
685
703
|
|
|
686
704
|
|
|
@@ -702,7 +720,7 @@ export class CipherSuite {
|
|
|
702
720
|
return await this.#suite.KEM.SerializePrivateKey(privateKey)
|
|
703
721
|
}
|
|
704
722
|
|
|
705
|
-
|
|
723
|
+
|
|
706
724
|
|
|
707
725
|
|
|
708
726
|
|
|
@@ -724,7 +742,7 @@ export class CipherSuite {
|
|
|
724
742
|
return await this.#suite.KEM.SerializePublicKey(publicKey)
|
|
725
743
|
}
|
|
726
744
|
|
|
727
|
-
|
|
745
|
+
|
|
728
746
|
|
|
729
747
|
|
|
730
748
|
|
|
@@ -761,7 +779,7 @@ export class CipherSuite {
|
|
|
761
779
|
}
|
|
762
780
|
}
|
|
763
781
|
|
|
764
|
-
|
|
782
|
+
|
|
765
783
|
|
|
766
784
|
|
|
767
785
|
|
|
@@ -794,7 +812,7 @@ export class CipherSuite {
|
|
|
794
812
|
}
|
|
795
813
|
}
|
|
796
814
|
|
|
797
|
-
|
|
815
|
+
|
|
798
816
|
|
|
799
817
|
|
|
800
818
|
|
|
@@ -841,7 +859,7 @@ export class CipherSuite {
|
|
|
841
859
|
return { encapsulatedSecret, ciphertext }
|
|
842
860
|
}
|
|
843
861
|
|
|
844
|
-
|
|
862
|
+
|
|
845
863
|
|
|
846
864
|
|
|
847
865
|
|
|
@@ -890,7 +908,7 @@ export class CipherSuite {
|
|
|
890
908
|
return await ctx.Open(ciphertext, options?.aad)
|
|
891
909
|
}
|
|
892
910
|
|
|
893
|
-
|
|
911
|
+
|
|
894
912
|
|
|
895
913
|
|
|
896
914
|
|
|
@@ -936,7 +954,7 @@ export class CipherSuite {
|
|
|
936
954
|
return { encapsulatedSecret, exportedSecret }
|
|
937
955
|
}
|
|
938
956
|
|
|
939
|
-
|
|
957
|
+
|
|
940
958
|
|
|
941
959
|
|
|
942
960
|
|
|
@@ -984,7 +1002,7 @@ export class CipherSuite {
|
|
|
984
1002
|
return await ctx.Export(exporterContext, length)
|
|
985
1003
|
}
|
|
986
1004
|
|
|
987
|
-
|
|
1005
|
+
|
|
988
1006
|
|
|
989
1007
|
|
|
990
1008
|
|
|
@@ -1060,7 +1078,7 @@ export class CipherSuite {
|
|
|
1060
1078
|
return { encapsulatedSecret: enc, ctx }
|
|
1061
1079
|
}
|
|
1062
1080
|
|
|
1063
|
-
|
|
1081
|
+
|
|
1064
1082
|
|
|
1065
1083
|
|
|
1066
1084
|
|
|
@@ -1152,9 +1170,9 @@ export class CipherSuite {
|
|
|
1152
1170
|
}
|
|
1153
1171
|
}
|
|
1154
1172
|
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
|
|
1158
1176
|
|
|
1159
1177
|
|
|
1160
1178
|
|
|
@@ -1166,7 +1184,7 @@ export class ValidationError extends Error {
|
|
|
1166
1184
|
constructor(message , options ) {
|
|
1167
1185
|
super(message, options)
|
|
1168
1186
|
this.name = 'ValidationError'
|
|
1169
|
-
|
|
1187
|
+
|
|
1170
1188
|
Error.captureStackTrace?.(this, ValidationError)
|
|
1171
1189
|
}
|
|
1172
1190
|
}
|
|
@@ -1181,7 +1199,7 @@ export class DeserializeError extends Error {
|
|
|
1181
1199
|
constructor(message , options ) {
|
|
1182
1200
|
super(message, options)
|
|
1183
1201
|
this.name = 'DeserializeError'
|
|
1184
|
-
|
|
1202
|
+
|
|
1185
1203
|
Error.captureStackTrace?.(this, DeserializeError)
|
|
1186
1204
|
}
|
|
1187
1205
|
}
|
|
@@ -1196,7 +1214,7 @@ export class EncapError extends Error {
|
|
|
1196
1214
|
constructor(message , options ) {
|
|
1197
1215
|
super(message, options)
|
|
1198
1216
|
this.name = 'EncapError'
|
|
1199
|
-
|
|
1217
|
+
|
|
1200
1218
|
Error.captureStackTrace?.(this, EncapError)
|
|
1201
1219
|
}
|
|
1202
1220
|
}
|
|
@@ -1211,7 +1229,7 @@ export class DecapError extends Error {
|
|
|
1211
1229
|
constructor(message , options ) {
|
|
1212
1230
|
super(message, options)
|
|
1213
1231
|
this.name = 'DecapError'
|
|
1214
|
-
|
|
1232
|
+
|
|
1215
1233
|
Error.captureStackTrace?.(this, DecapError)
|
|
1216
1234
|
}
|
|
1217
1235
|
}
|
|
@@ -1226,7 +1244,7 @@ export class OpenError extends Error {
|
|
|
1226
1244
|
constructor(message , options ) {
|
|
1227
1245
|
super(message, options)
|
|
1228
1246
|
this.name = 'OpenError'
|
|
1229
|
-
|
|
1247
|
+
|
|
1230
1248
|
Error.captureStackTrace?.(this, OpenError)
|
|
1231
1249
|
}
|
|
1232
1250
|
}
|
|
@@ -1241,7 +1259,7 @@ export class MessageLimitReachedError extends Error {
|
|
|
1241
1259
|
constructor(message , options ) {
|
|
1242
1260
|
super(message, options)
|
|
1243
1261
|
this.name = 'MessageLimitReachedError'
|
|
1244
|
-
|
|
1262
|
+
|
|
1245
1263
|
Error.captureStackTrace?.(this, MessageLimitReachedError)
|
|
1246
1264
|
}
|
|
1247
1265
|
}
|
|
@@ -1256,7 +1274,7 @@ export class DeriveKeyPairError extends Error {
|
|
|
1256
1274
|
constructor(message , options ) {
|
|
1257
1275
|
super(message, options)
|
|
1258
1276
|
this.name = 'DeriveKeyPairError'
|
|
1259
|
-
|
|
1277
|
+
|
|
1260
1278
|
Error.captureStackTrace?.(this, DeriveKeyPairError)
|
|
1261
1279
|
}
|
|
1262
1280
|
}
|
|
@@ -1271,21 +1289,21 @@ export class NotSupportedError extends Error {
|
|
|
1271
1289
|
constructor(message , options ) {
|
|
1272
1290
|
super(message, options)
|
|
1273
1291
|
this.name = 'NotSupportedError'
|
|
1274
|
-
|
|
1292
|
+
|
|
1275
1293
|
Error.captureStackTrace?.(this, NotSupportedError)
|
|
1276
1294
|
}
|
|
1277
1295
|
}
|
|
1278
1296
|
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
|
|
1279
1300
|
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
|
|
1289
1307
|
|
|
1290
1308
|
|
|
1291
1309
|
|
|
@@ -1308,6 +1326,8 @@ export const MODE_BASE = 0x00
|
|
|
1308
1326
|
|
|
1309
1327
|
export const MODE_PSK = 0x01
|
|
1310
1328
|
|
|
1329
|
+
|
|
1330
|
+
|
|
1311
1331
|
|
|
1312
1332
|
|
|
1313
1333
|
|
|
@@ -1343,6 +1363,7 @@ export const MODE_PSK = 0x01
|
|
|
1343
1363
|
|
|
1344
1364
|
|
|
1345
1365
|
|
|
1366
|
+
|
|
1346
1367
|
|
|
1347
1368
|
|
|
1348
1369
|
|
|
@@ -1364,6 +1385,7 @@ export const MODE_PSK = 0x01
|
|
|
1364
1385
|
|
|
1365
1386
|
|
|
1366
1387
|
|
|
1388
|
+
|
|
1367
1389
|
|
|
1368
1390
|
|
|
1369
1391
|
|
|
@@ -1382,6 +1404,7 @@ export const MODE_PSK = 0x01
|
|
|
1382
1404
|
|
|
1383
1405
|
|
|
1384
1406
|
|
|
1407
|
+
|
|
1385
1408
|
|
|
1386
1409
|
|
|
1387
1410
|
|
|
@@ -1400,6 +1423,12 @@ export const MODE_PSK = 0x01
|
|
|
1400
1423
|
|
|
1401
1424
|
|
|
1402
1425
|
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
|
|
1431
|
+
|
|
1403
1432
|
|
|
1404
1433
|
|
|
1405
1434
|
|
|
@@ -1411,8 +1440,19 @@ export const MODE_PSK = 0x01
|
|
|
1411
1440
|
|
|
1412
1441
|
|
|
1413
1442
|
|
|
1443
|
+
|
|
1444
|
+
|
|
1445
|
+
|
|
1446
|
+
|
|
1447
|
+
|
|
1448
|
+
|
|
1414
1449
|
|
|
1450
|
+
|
|
1451
|
+
|
|
1415
1452
|
|
|
1453
|
+
|
|
1454
|
+
|
|
1455
|
+
|
|
1416
1456
|
|
|
1417
1457
|
|
|
1418
1458
|
|
|
@@ -1421,6 +1461,11 @@ export const MODE_PSK = 0x01
|
|
|
1421
1461
|
|
|
1422
1462
|
|
|
1423
1463
|
|
|
1464
|
+
|
|
1465
|
+
|
|
1466
|
+
|
|
1467
|
+
|
|
1468
|
+
|
|
1424
1469
|
|
|
1425
1470
|
|
|
1426
1471
|
|
|
@@ -1430,43 +1475,16 @@ export const MODE_PSK = 0x01
|
|
|
1430
1475
|
|
|
1431
1476
|
|
|
1432
1477
|
|
|
1478
|
+
|
|
1479
|
+
|
|
1480
|
+
|
|
1481
|
+
|
|
1482
|
+
|
|
1483
|
+
|
|
1433
1484
|
|
|
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
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1485
|
+
|
|
1486
|
+
|
|
1487
|
+
|
|
1470
1488
|
|
|
1471
1489
|
|
|
1472
1490
|
|
|
@@ -1515,34 +1533,35 @@ export function encode(string ) {
|
|
|
1515
1533
|
return bytes
|
|
1516
1534
|
}
|
|
1517
1535
|
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
const L_HPKE_v1 = encode('HPKE-v1')
|
|
1523
|
-
const L_HPKE = encode('HPKE')
|
|
1524
|
-
const L_KEM = encode('KEM')
|
|
1525
|
-
const L_sec = encode('sec')
|
|
1526
|
-
const L_secret = encode('secret')
|
|
1527
|
-
const L_key = encode('key')
|
|
1528
|
-
const L_base_nonce = encode('base_nonce')
|
|
1529
|
-
const L_exp = encode('exp')
|
|
1530
|
-
const L_psk_id_hash = encode('psk_id_hash')
|
|
1531
|
-
const L_info_hash = encode('info_hash')
|
|
1532
|
-
const L_dkp_prk = encode('dkp_prk')
|
|
1533
|
-
const L_candidate = encode('candidate')
|
|
1534
|
-
const L_eae_prk = encode('eae_prk')
|
|
1535
|
-
const L_shared_secret = encode('shared_secret')
|
|
1536
|
-
const L_sk = encode('sk')
|
|
1537
|
-
const
|
|
1536
|
+
|
|
1537
|
+
|
|
1538
|
+
|
|
1539
|
+
|
|
1540
|
+
const L_HPKE_v1 = /* @__PURE__ */ encode('HPKE-v1')
|
|
1541
|
+
const L_HPKE = /* @__PURE__ */ encode('HPKE')
|
|
1542
|
+
const L_KEM = /* @__PURE__ */ encode('KEM')
|
|
1543
|
+
const L_sec = /* @__PURE__ */ encode('sec')
|
|
1544
|
+
const L_secret = /* @__PURE__ */ encode('secret')
|
|
1545
|
+
const L_key = /* @__PURE__ */ encode('key')
|
|
1546
|
+
const L_base_nonce = /* @__PURE__ */ encode('base_nonce')
|
|
1547
|
+
const L_exp = /* @__PURE__ */ encode('exp')
|
|
1548
|
+
const L_psk_id_hash = /* @__PURE__ */ encode('psk_id_hash')
|
|
1549
|
+
const L_info_hash = /* @__PURE__ */ encode('info_hash')
|
|
1550
|
+
const L_dkp_prk = /* @__PURE__ */ encode('dkp_prk')
|
|
1551
|
+
const L_candidate = /* @__PURE__ */ encode('candidate')
|
|
1552
|
+
const L_eae_prk = /* @__PURE__ */ encode('eae_prk')
|
|
1553
|
+
const L_shared_secret = /* @__PURE__ */ encode('shared_secret')
|
|
1554
|
+
const L_sk = /* @__PURE__ */ encode('sk')
|
|
1555
|
+
const L_public_key_recovery = /* @__PURE__ */ encode('HPKE public key recovery')
|
|
1556
|
+
const L_DeriveKeyPair = /* @__PURE__ */ encode('DeriveKeyPair')
|
|
1538
1557
|
|
|
1539
1558
|
function lengthPrefixed(x ) {
|
|
1540
1559
|
return concat(I2OSP(x.byteLength, 2), x)
|
|
1541
1560
|
}
|
|
1542
1561
|
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1562
|
+
|
|
1563
|
+
|
|
1564
|
+
|
|
1546
1565
|
|
|
1547
1566
|
|
|
1548
1567
|
|
|
@@ -1575,7 +1594,7 @@ export async function LabeledDerive(
|
|
|
1575
1594
|
return await KDF.Derive(labeled_ikm, L)
|
|
1576
1595
|
}
|
|
1577
1596
|
|
|
1578
|
-
|
|
1597
|
+
|
|
1579
1598
|
async function Export_OneStage(
|
|
1580
1599
|
KDF ,
|
|
1581
1600
|
suite_id ,
|
|
@@ -1587,7 +1606,7 @@ async function Export_OneStage(
|
|
|
1587
1606
|
return await LabeledDerive(KDF, suite_id, exporter_secret, L_sec, exporter_context, L)
|
|
1588
1607
|
}
|
|
1589
1608
|
|
|
1590
|
-
|
|
1609
|
+
|
|
1591
1610
|
async function CombineSecrets_OneStage(
|
|
1592
1611
|
suite ,
|
|
1593
1612
|
mode ,
|
|
@@ -1619,10 +1638,10 @@ async function CombineSecrets_OneStage(
|
|
|
1619
1638
|
return { key, base_nonce, exporter_secret }
|
|
1620
1639
|
}
|
|
1621
1640
|
|
|
1622
|
-
|
|
1623
|
-
|
|
1641
|
+
|
|
1642
|
+
|
|
1624
1643
|
const MAX_LENGTH_TWO_STAGE = 0xffff
|
|
1625
|
-
|
|
1644
|
+
|
|
1626
1645
|
const MAX_LENGTH_ONE_STAGE = 0xffff
|
|
1627
1646
|
|
|
1628
1647
|
function checkLength(data , name , maxLength ) {
|
|
@@ -1644,7 +1663,7 @@ function checkExtractable(extractable )
|
|
|
1644
1663
|
}
|
|
1645
1664
|
}
|
|
1646
1665
|
|
|
1647
|
-
|
|
1666
|
+
|
|
1648
1667
|
async function CombineSecrets_TwoStage(
|
|
1649
1668
|
suite ,
|
|
1650
1669
|
mode ,
|
|
@@ -1665,7 +1684,7 @@ async function CombineSecrets_TwoStage(
|
|
|
1665
1684
|
const key_schedule_context = concat(I2OSP(mode, 1), psk_id_hash, info_hash)
|
|
1666
1685
|
const secret = await LabeledExtract(suite.KDF, suite.id, shared_secret, L_secret, psk)
|
|
1667
1686
|
|
|
1668
|
-
|
|
1687
|
+
|
|
1669
1688
|
if (suite.AEAD.id === EXPORT_ONLY) {
|
|
1670
1689
|
const exporter_secret = await LabeledExpand(
|
|
1671
1690
|
suite.KDF,
|
|
@@ -1687,7 +1706,7 @@ async function CombineSecrets_TwoStage(
|
|
|
1687
1706
|
return { key, base_nonce, exporter_secret }
|
|
1688
1707
|
}
|
|
1689
1708
|
|
|
1690
|
-
|
|
1709
|
+
|
|
1691
1710
|
async function Export_TwoStage(
|
|
1692
1711
|
KDF ,
|
|
1693
1712
|
suite_id ,
|
|
@@ -1766,61 +1785,61 @@ async function Export_TwoStage(
|
|
|
1766
1785
|
|
|
1767
1786
|
|
|
1768
1787
|
|
|
1788
|
+
|
|
1789
|
+
|
|
1790
|
+
|
|
1769
1791
|
|
|
1792
|
+
|
|
1793
|
+
|
|
1770
1794
|
|
|
1795
|
+
|
|
1796
|
+
|
|
1771
1797
|
|
|
1798
|
+
|
|
1799
|
+
|
|
1800
|
+
|
|
1801
|
+
|
|
1802
|
+
|
|
1803
|
+
|
|
1772
1804
|
|
|
1805
|
+
|
|
1806
|
+
|
|
1773
1807
|
|
|
1808
|
+
|
|
1809
|
+
|
|
1810
|
+
|
|
1811
|
+
|
|
1812
|
+
|
|
1813
|
+
|
|
1814
|
+
|
|
1815
|
+
|
|
1816
|
+
|
|
1774
1817
|
|
|
1818
|
+
|
|
1819
|
+
|
|
1820
|
+
|
|
1821
|
+
|
|
1822
|
+
|
|
1823
|
+
|
|
1824
|
+
|
|
1825
|
+
|
|
1826
|
+
|
|
1827
|
+
|
|
1775
1828
|
|
|
1829
|
+
|
|
1830
|
+
|
|
1831
|
+
|
|
1832
|
+
|
|
1833
|
+
|
|
1834
|
+
|
|
1835
|
+
|
|
1836
|
+
|
|
1837
|
+
|
|
1838
|
+
|
|
1776
1839
|
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1840
|
+
|
|
1841
|
+
|
|
1842
|
+
|
|
1824
1843
|
|
|
1825
1844
|
|
|
1826
1845
|
|
|
@@ -1881,6 +1900,9 @@ export async function LabeledExpand(
|
|
|
1881
1900
|
return await KDF.Expand(prk, labeled_info, L)
|
|
1882
1901
|
}
|
|
1883
1902
|
|
|
1903
|
+
|
|
1904
|
+
|
|
1905
|
+
|
|
1884
1906
|
|
|
1885
1907
|
|
|
1886
1908
|
|
|
@@ -1976,23 +1998,152 @@ export async function LabeledExpand(
|
|
|
1976
1998
|
|
|
1977
1999
|
|
|
1978
2000
|
|
|
2001
|
+
|
|
2002
|
+
|
|
2003
|
+
|
|
1979
2004
|
|
|
2005
|
+
|
|
2006
|
+
|
|
1980
2007
|
|
|
2008
|
+
|
|
2009
|
+
|
|
1981
2010
|
|
|
2011
|
+
|
|
2012
|
+
|
|
1982
2013
|
|
|
2014
|
+
|
|
2015
|
+
|
|
1983
2016
|
|
|
2017
|
+
|
|
2018
|
+
|
|
1984
2019
|
|
|
2020
|
+
|
|
2021
|
+
|
|
1985
2022
|
|
|
2023
|
+
|
|
2024
|
+
|
|
2025
|
+
|
|
2026
|
+
|
|
2027
|
+
|
|
2028
|
+
|
|
2029
|
+
|
|
2030
|
+
|
|
2031
|
+
|
|
1986
2032
|
|
|
2033
|
+
|
|
2034
|
+
|
|
2035
|
+
|
|
2036
|
+
|
|
2037
|
+
|
|
2038
|
+
|
|
2039
|
+
|
|
2040
|
+
|
|
1987
2041
|
|
|
2042
|
+
|
|
2043
|
+
|
|
2044
|
+
|
|
2045
|
+
|
|
2046
|
+
|
|
2047
|
+
|
|
2048
|
+
|
|
2049
|
+
|
|
1988
2050
|
|
|
2051
|
+
|
|
2052
|
+
|
|
2053
|
+
|
|
2054
|
+
|
|
2055
|
+
|
|
2056
|
+
|
|
2057
|
+
|
|
2058
|
+
|
|
1989
2059
|
|
|
2060
|
+
|
|
2061
|
+
|
|
2062
|
+
|
|
2063
|
+
|
|
2064
|
+
|
|
2065
|
+
|
|
2066
|
+
|
|
2067
|
+
|
|
1990
2068
|
|
|
2069
|
+
|
|
2070
|
+
|
|
2071
|
+
|
|
2072
|
+
|
|
2073
|
+
|
|
2074
|
+
|
|
2075
|
+
|
|
2076
|
+
|
|
2077
|
+
|
|
1991
2078
|
|
|
2079
|
+
|
|
2080
|
+
|
|
2081
|
+
|
|
2082
|
+
|
|
2083
|
+
|
|
2084
|
+
|
|
2085
|
+
|
|
2086
|
+
|
|
2087
|
+
|
|
2088
|
+
|
|
2089
|
+
|
|
2090
|
+
|
|
2091
|
+
|
|
2092
|
+
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
|
|
2096
|
+
|
|
2097
|
+
|
|
2098
|
+
|
|
2099
|
+
|
|
2100
|
+
|
|
2101
|
+
|
|
2102
|
+
|
|
2103
|
+
|
|
2104
|
+
|
|
2105
|
+
|
|
2106
|
+
|
|
1992
2107
|
|
|
2108
|
+
function isKeyPair (skR ) {
|
|
2109
|
+
if (!skR || typeof skR !== 'object') return false
|
|
2110
|
+
if ('publicKey' in skR && 'privateKey' in skR) {
|
|
2111
|
+
const pkR = skR.publicKey
|
|
2112
|
+
skR = skR.privateKey
|
|
2113
|
+
try {
|
|
2114
|
+
isKey(pkR, 'public')
|
|
2115
|
+
isKey(skR, 'private')
|
|
2116
|
+
if (pkR.algorithm.name !== skR.algorithm.name) {
|
|
2117
|
+
throw new TypeError('key pair algorithms do not match')
|
|
2118
|
+
}
|
|
2119
|
+
} catch (cause) {
|
|
2120
|
+
throw new TypeError('Invalid "privateKey"', { cause })
|
|
2121
|
+
}
|
|
2122
|
+
return true
|
|
2123
|
+
}
|
|
2124
|
+
return false
|
|
2125
|
+
}
|
|
1993
2126
|
|
|
2127
|
+
function isKey(key , type , extractable ) {
|
|
2128
|
+
const k = key
|
|
2129
|
+
if (
|
|
2130
|
+
typeof k.algorithm !== 'object' ||
|
|
2131
|
+
typeof k.algorithm.name !== 'string' ||
|
|
2132
|
+
typeof k.extractable !== 'boolean' ||
|
|
2133
|
+
typeof k.type !== 'string' ||
|
|
2134
|
+
k.type !== type
|
|
2135
|
+
) {
|
|
2136
|
+
throw new TypeError(`Invalid "${type}Key"`)
|
|
2137
|
+
}
|
|
1994
2138
|
|
|
2139
|
+
if (extractable && k.extractable !== true) {
|
|
2140
|
+
throw new TypeError(`"${type}Key" must be extractable`)
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
1995
2143
|
|
|
2144
|
+
|
|
2145
|
+
|
|
2146
|
+
|
|
1996
2147
|
|
|
1997
2148
|
|
|
1998
2149
|
|
|
@@ -2049,191 +2200,59 @@ export async function LabeledExpand(
|
|
|
2049
2200
|
|
|
2050
2201
|
|
|
2051
2202
|
|
|
2203
|
+
|
|
2204
|
+
|
|
2205
|
+
|
|
2052
2206
|
|
|
2207
|
+
|
|
2208
|
+
|
|
2053
2209
|
|
|
2210
|
+
|
|
2211
|
+
|
|
2054
2212
|
|
|
2213
|
+
|
|
2214
|
+
|
|
2055
2215
|
|
|
2216
|
+
|
|
2217
|
+
|
|
2056
2218
|
|
|
2219
|
+
|
|
2220
|
+
|
|
2057
2221
|
|
|
2222
|
+
|
|
2223
|
+
|
|
2224
|
+
|
|
2225
|
+
|
|
2226
|
+
|
|
2227
|
+
|
|
2228
|
+
|
|
2229
|
+
|
|
2230
|
+
|
|
2231
|
+
|
|
2232
|
+
|
|
2233
|
+
|
|
2234
|
+
|
|
2235
|
+
|
|
2236
|
+
|
|
2237
|
+
|
|
2238
|
+
|
|
2058
2239
|
|
|
2240
|
+
|
|
2241
|
+
|
|
2242
|
+
|
|
2243
|
+
|
|
2244
|
+
|
|
2245
|
+
|
|
2246
|
+
|
|
2247
|
+
|
|
2248
|
+
|
|
2249
|
+
|
|
2250
|
+
|
|
2251
|
+
|
|
2059
2252
|
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
function isKeyPair (skR ) {
|
|
2090
|
-
if (!skR || typeof skR !== 'object') return false
|
|
2091
|
-
if ('publicKey' in skR && 'privateKey' in skR) {
|
|
2092
|
-
const pkR = skR.publicKey
|
|
2093
|
-
skR = skR.privateKey
|
|
2094
|
-
try {
|
|
2095
|
-
isKey(pkR, 'public')
|
|
2096
|
-
isKey(skR, 'private')
|
|
2097
|
-
if (pkR.algorithm.name !== skR.algorithm.name) {
|
|
2098
|
-
throw new TypeError('key pair algorithms do not match')
|
|
2099
|
-
}
|
|
2100
|
-
} catch (cause) {
|
|
2101
|
-
throw new TypeError('Invalid "privateKey"', { cause })
|
|
2102
|
-
}
|
|
2103
|
-
return true
|
|
2104
|
-
}
|
|
2105
|
-
return false
|
|
2106
|
-
}
|
|
2107
|
-
|
|
2108
|
-
function isKey(key , type , extractable ) {
|
|
2109
|
-
const k = key
|
|
2110
|
-
if (
|
|
2111
|
-
typeof k.algorithm !== 'object' ||
|
|
2112
|
-
typeof k.algorithm.name !== 'string' ||
|
|
2113
|
-
typeof k.extractable !== 'boolean' ||
|
|
2114
|
-
typeof k.type !== 'string' ||
|
|
2115
|
-
k.type !== type
|
|
2116
|
-
) {
|
|
2117
|
-
throw new TypeError(`Invalid "${type}Key"`)
|
|
2118
|
-
}
|
|
2119
|
-
|
|
2120
|
-
if (extractable && k.extractable !== true) {
|
|
2121
|
-
throw new TypeError(`"${type}Key" must be extractable`)
|
|
2122
|
-
}
|
|
2123
|
-
}
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2253
|
+
|
|
2254
|
+
|
|
2255
|
+
|
|
2237
2256
|
|
|
2238
2257
|
|
|
2239
2258
|
|
|
@@ -2271,11 +2290,11 @@ function KDFStages(KDF ) {
|
|
|
2271
2290
|
if (KDF.stages === 1 || KDF.stages === 2) {
|
|
2272
2291
|
return KDF.stages
|
|
2273
2292
|
}
|
|
2274
|
-
|
|
2293
|
+
|
|
2275
2294
|
throw new Error('unreachable')
|
|
2276
2295
|
}
|
|
2277
2296
|
|
|
2278
|
-
|
|
2297
|
+
|
|
2279
2298
|
async function KeySchedule(
|
|
2280
2299
|
suite ,
|
|
2281
2300
|
mode ,
|
|
@@ -2298,7 +2317,7 @@ async function KeySchedule(
|
|
|
2298
2317
|
return await CombineSecrets(suite, mode, shared_secret, info, psk, pskId)
|
|
2299
2318
|
}
|
|
2300
2319
|
|
|
2301
|
-
|
|
2320
|
+
|
|
2302
2321
|
function VerifyPSKInputs(psk , psk_id ) {
|
|
2303
2322
|
if (psk.byteLength && psk_id.byteLength) {
|
|
2304
2323
|
if (psk.byteLength < 32) {
|
|
@@ -2312,7 +2331,7 @@ function VerifyPSKInputs(psk , psk_id ) {
|
|
|
2312
2331
|
throw new TypeError('Inconsistent PSK inputs')
|
|
2313
2332
|
}
|
|
2314
2333
|
|
|
2315
|
-
|
|
2334
|
+
|
|
2316
2335
|
const NotApplicable = () => {
|
|
2317
2336
|
throw new Error('unreachable')
|
|
2318
2337
|
}
|
|
@@ -2345,9 +2364,9 @@ export const AEAD_EXPORT_ONLY = function () {
|
|
|
2345
2364
|
}
|
|
2346
2365
|
}
|
|
2347
2366
|
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2367
|
+
|
|
2368
|
+
|
|
2369
|
+
|
|
2351
2370
|
|
|
2352
2371
|
async function subtle (promise , name ) {
|
|
2353
2372
|
try {
|
|
@@ -2363,20 +2382,40 @@ async function subtle (promise , name
|
|
|
2363
2382
|
}
|
|
2364
2383
|
}
|
|
2365
2384
|
|
|
2385
|
+
|
|
2386
|
+
|
|
2387
|
+
|
|
2366
2388
|
|
|
2389
|
+
|
|
2367
2390
|
|
|
2391
|
+
|
|
2392
|
+
|
|
2393
|
+
|
|
2394
|
+
|
|
2368
2395
|
|
|
2396
|
+
async function getCachedCryptoKey(
|
|
2397
|
+
cache ,
|
|
2398
|
+
keyBytes ,
|
|
2399
|
+
init ,
|
|
2400
|
+
) {
|
|
2401
|
+
const cached = cache.get(keyBytes)
|
|
2402
|
+
if (
|
|
2403
|
+
cached &&
|
|
2404
|
+
cached.bytes.byteLength === keyBytes.byteLength &&
|
|
2405
|
+
cached.bytes.every((byte, index) => byte === keyBytes[index])
|
|
2406
|
+
) {
|
|
2407
|
+
return cached.key
|
|
2408
|
+
}
|
|
2369
2409
|
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
)
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
return result
|
|
2410
|
+
const bytes = slice(keyBytes)
|
|
2411
|
+
const entry = { bytes, key: init(bytes) }
|
|
2412
|
+
cache.set(keyBytes, entry)
|
|
2413
|
+
try {
|
|
2414
|
+
return await entry.key
|
|
2415
|
+
} catch (cause) {
|
|
2416
|
+
if (cache.get(keyBytes) === entry) cache.delete(keyBytes)
|
|
2417
|
+
throw cause
|
|
2418
|
+
}
|
|
2380
2419
|
}
|
|
2381
2420
|
|
|
2382
2421
|
function HKDF_SHARED() {
|
|
@@ -2387,12 +2426,7 @@ function HKDF_SHARED() {
|
|
|
2387
2426
|
this.name,
|
|
2388
2427
|
)
|
|
2389
2428
|
}
|
|
2390
|
-
const cache = new WeakMap
|
|
2391
|
-
function importPrk( prk ) {
|
|
2392
|
-
const key = importKey.call(this, prk )
|
|
2393
|
-
cache.set(prk, key)
|
|
2394
|
-
return key
|
|
2395
|
-
}
|
|
2429
|
+
const cache = new WeakMap ()
|
|
2396
2430
|
return {
|
|
2397
2431
|
stages: 2,
|
|
2398
2432
|
Derive: NotApplicable,
|
|
@@ -2413,7 +2447,9 @@ function HKDF_SHARED() {
|
|
|
2413
2447
|
throw new Error('L must be <= 255*Nh')
|
|
2414
2448
|
}
|
|
2415
2449
|
const N = Math.ceil(L / this.Nh)
|
|
2416
|
-
const key = await (cache
|
|
2450
|
+
const key = await getCachedCryptoKey(cache, prk, (bytes) =>
|
|
2451
|
+
importKey.call(this, bytes ),
|
|
2452
|
+
)
|
|
2417
2453
|
|
|
2418
2454
|
const T = new Uint8Array(N * this.Nh)
|
|
2419
2455
|
let T_prev = new Uint8Array()
|
|
@@ -2435,9 +2471,9 @@ function HKDF_SHARED() {
|
|
|
2435
2471
|
}
|
|
2436
2472
|
}
|
|
2437
2473
|
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2474
|
+
|
|
2475
|
+
|
|
2476
|
+
|
|
2441
2477
|
|
|
2442
2478
|
|
|
2443
2479
|
|
|
@@ -2508,13 +2544,13 @@ export const KDF_HKDF_SHA512 = function () {
|
|
|
2508
2544
|
return { id: 0x0003, type: 'KDF', name: 'HKDF-SHA512', Nh: 64, hash: 'SHA-512', ...HKDF_SHARED() }
|
|
2509
2545
|
}
|
|
2510
2546
|
|
|
2547
|
+
|
|
2548
|
+
|
|
2549
|
+
|
|
2511
2550
|
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2551
|
+
|
|
2552
|
+
|
|
2553
|
+
|
|
2518
2554
|
|
|
2519
2555
|
async function ShakeDerive(name , variant , ikm , L ) {
|
|
2520
2556
|
const bits = L << 3
|
|
@@ -2678,41 +2714,30 @@ async function getPublicKeyByExport(
|
|
|
2678
2714
|
|
|
2679
2715
|
async function getPublicKey(name , key , usages ) {
|
|
2680
2716
|
return (
|
|
2681
|
-
|
|
2717
|
+
|
|
2682
2718
|
((await subtle((c) => c.getPublicKey?.(key, usages), name)) ) ||
|
|
2683
2719
|
(await getPublicKeyByExport(name, key, usages))
|
|
2684
2720
|
)
|
|
2685
2721
|
}
|
|
2686
2722
|
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2723
|
+
|
|
2724
|
+
|
|
2725
|
+
|
|
2726
|
+
|
|
2727
|
+
|
|
2728
|
+
|
|
2729
|
+
|
|
2730
|
+
|
|
2731
|
+
|
|
2732
|
+
|
|
2733
|
+
|
|
2734
|
+
|
|
2735
|
+
|
|
2736
|
+
|
|
2737
|
+
|
|
2738
|
+
|
|
2739
|
+
|
|
2740
|
+
|
|
2716
2741
|
|
|
2717
2742
|
function fromBase64(input ) {
|
|
2718
2743
|
input = input.replace(/-/g, '+').replace(/_/g, '/')
|
|
@@ -2733,20 +2758,20 @@ function toBase64Url(bytes ) {
|
|
|
2733
2758
|
}
|
|
2734
2759
|
|
|
2735
2760
|
function toB64u(input ) {
|
|
2736
|
-
|
|
2761
|
+
|
|
2737
2762
|
return input.toBase64?.({ alphabet: 'base64url', omitPadding: true }) || toBase64Url(input)
|
|
2738
2763
|
}
|
|
2739
2764
|
|
|
2740
2765
|
function b64u(input ) {
|
|
2741
|
-
|
|
2766
|
+
|
|
2742
2767
|
return Uint8Array.fromBase64?.(input, { alphabet: 'base64url' }) || fromBase64(input)
|
|
2743
2768
|
}
|
|
2744
2769
|
|
|
2770
|
+
|
|
2771
|
+
|
|
2772
|
+
|
|
2745
2773
|
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2774
|
+
|
|
2750
2775
|
async function DeriveKeyPairBytes(
|
|
2751
2776
|
DHKEM ,
|
|
2752
2777
|
ikm ,
|
|
@@ -2796,14 +2821,14 @@ function assertKeyAlgorithm(key , expectedAlgorithm ) {
|
|
|
2796
2821
|
}
|
|
2797
2822
|
|
|
2798
2823
|
function assertCryptoKey(key ) {
|
|
2799
|
-
|
|
2824
|
+
|
|
2800
2825
|
if (key[Symbol.toStringTag] !== 'CryptoKey') {
|
|
2801
2826
|
if (key instanceof CryptoKey) return
|
|
2802
2827
|
throw new TypeError('unexpected key constructor')
|
|
2803
2828
|
}
|
|
2804
2829
|
}
|
|
2805
2830
|
|
|
2806
|
-
|
|
2831
|
+
|
|
2807
2832
|
async function ExtractAndExpand_OneStage(
|
|
2808
2833
|
DHKEM ,
|
|
2809
2834
|
dh ,
|
|
@@ -2819,7 +2844,7 @@ async function ExtractAndExpand_OneStage(
|
|
|
2819
2844
|
)
|
|
2820
2845
|
}
|
|
2821
2846
|
|
|
2822
|
-
|
|
2847
|
+
|
|
2823
2848
|
async function ExtractAndExpand_TwoStage(
|
|
2824
2849
|
DHKEM ,
|
|
2825
2850
|
dh ,
|
|
@@ -2836,7 +2861,7 @@ async function ExtractAndExpand_TwoStage(
|
|
|
2836
2861
|
)
|
|
2837
2862
|
}
|
|
2838
2863
|
|
|
2839
|
-
|
|
2864
|
+
|
|
2840
2865
|
async function ExtractAndExpand(
|
|
2841
2866
|
DHKEM ,
|
|
2842
2867
|
dh ,
|
|
@@ -2846,12 +2871,12 @@ async function ExtractAndExpand(
|
|
|
2846
2871
|
return await Fn(DHKEM, dh, kem_context)
|
|
2847
2872
|
}
|
|
2848
2873
|
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2874
|
+
|
|
2875
|
+
|
|
2876
|
+
|
|
2852
2877
|
|
|
2853
2878
|
function DHKEM_SHARED()
|
|
2854
|
-
|
|
2879
|
+
|
|
2855
2880
|
{
|
|
2856
2881
|
return {
|
|
2857
2882
|
async GenerateKeyPair( extractable) {
|
|
@@ -2885,15 +2910,14 @@ function DHKEM_SHARED()
|
|
|
2885
2910
|
const skE = ekp.privateKey
|
|
2886
2911
|
const pkE = ekp.publicKey
|
|
2887
2912
|
|
|
2888
|
-
|
|
2889
|
-
|
|
2913
|
+
|
|
2914
|
+
|
|
2890
2915
|
const dh = new Uint8Array(
|
|
2891
2916
|
await subtle(
|
|
2892
2917
|
(c) => c.deriveBits({ name: skE.algorithm.name, public: pkR }, skE, this.Ndh << 3),
|
|
2893
2918
|
this.name,
|
|
2894
2919
|
),
|
|
2895
2920
|
)
|
|
2896
|
-
checkNotAllZeros(dh)
|
|
2897
2921
|
|
|
2898
2922
|
const enc = await this.SerializePublicKey(pkE)
|
|
2899
2923
|
const pkRm = await this.SerializePublicKey(pkR)
|
|
@@ -2913,15 +2937,14 @@ function DHKEM_SHARED()
|
|
|
2913
2937
|
|
|
2914
2938
|
const pkE = (await this.DeserializePublicKey(enc))
|
|
2915
2939
|
|
|
2916
|
-
|
|
2917
|
-
|
|
2940
|
+
|
|
2941
|
+
|
|
2918
2942
|
const dh = new Uint8Array(
|
|
2919
2943
|
await subtle(
|
|
2920
2944
|
(c) => c.deriveBits({ name: skR.algorithm.name, public: pkE }, skR, this.Ndh << 3),
|
|
2921
2945
|
this.name,
|
|
2922
2946
|
),
|
|
2923
2947
|
)
|
|
2924
|
-
checkNotAllZeros(dh)
|
|
2925
2948
|
|
|
2926
2949
|
const pkRm = await this.SerializePublicKey(pkR)
|
|
2927
2950
|
const kem_context = concat(enc, pkRm)
|
|
@@ -2931,9 +2954,9 @@ function DHKEM_SHARED()
|
|
|
2931
2954
|
}
|
|
2932
2955
|
}
|
|
2933
2956
|
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2957
|
+
|
|
2958
|
+
|
|
2959
|
+
|
|
2937
2960
|
|
|
2938
2961
|
async function createKeyPairFromPrivateKey(
|
|
2939
2962
|
DHKEM ,
|
|
@@ -2942,7 +2965,7 @@ async function createKeyPairFromPrivateKey(
|
|
|
2942
2965
|
) {
|
|
2943
2966
|
let privateKey
|
|
2944
2967
|
let publicKey
|
|
2945
|
-
|
|
2968
|
+
|
|
2946
2969
|
if (!extractable && typeof crypto.subtle.getPublicKey !== 'function') {
|
|
2947
2970
|
privateKey = (await DHKEM.DeserializePrivateKey(key, true))
|
|
2948
2971
|
publicKey = await getPublicKey(DHKEM.name, privateKey, [])
|
|
@@ -2972,173 +2995,133 @@ async function CurveKeyFromD(
|
|
|
2972
2995
|
)
|
|
2973
2996
|
}
|
|
2974
2997
|
|
|
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
|
-
while (newR !== 0n) {
|
|
3001
|
-
const quotient = r / newR
|
|
3002
|
-
;[t, newT] = [newT, t - quotient * newT]
|
|
3003
|
-
;[r, newR] = [newR, r - quotient * newR]
|
|
3004
|
-
}
|
|
3005
|
-
|
|
3006
|
-
if (r > 1n) throw new Error('a is not invertible')
|
|
3007
|
-
if (t < 0n) t = t + m
|
|
3008
|
-
return t
|
|
3009
|
-
}
|
|
3010
|
-
|
|
3011
|
-
function jDouble(p , P , a ) {
|
|
3012
|
-
const [X, Y, Z] = p
|
|
3013
|
-
if (Y === 0n) return [1n, 1n, 0n]
|
|
3014
|
-
const Y2 = mod(Y * Y, P)
|
|
3015
|
-
const S = mod(4n * X * Y2, P)
|
|
3016
|
-
const Z2 = mod(Z * Z, P)
|
|
3017
|
-
const M = mod(3n * X * X + a * Z2 * Z2, P)
|
|
3018
|
-
const X3 = mod(M * M - 2n * S, P)
|
|
3019
|
-
return [X3, mod(M * (S - X3) - 8n * Y2 * Y2, P), mod(2n * Y * Z, P)]
|
|
3020
|
-
}
|
|
3021
|
-
|
|
3022
|
-
function jAdd(p , q , P , a ) {
|
|
3023
|
-
if (p[2] === 0n) return q
|
|
3024
|
-
if (q[2] === 0n) return p
|
|
3025
|
-
const pZ2 = mod(p[2] * p[2], P)
|
|
3026
|
-
const qZ2 = mod(q[2] * q[2], P)
|
|
3027
|
-
const U1 = mod(p[0] * qZ2, P)
|
|
3028
|
-
const U2 = mod(q[0] * pZ2, P)
|
|
3029
|
-
const S1 = mod(p[1] * qZ2 * q[2], P)
|
|
3030
|
-
const S2 = mod(q[1] * pZ2 * p[2], P)
|
|
3031
|
-
if (U1 === U2) return S1 === S2 ? jDouble(p, P, a) : [1n, 1n, 0n]
|
|
3032
|
-
const H = mod(U2 - U1, P)
|
|
3033
|
-
const R = mod(S2 - S1, P)
|
|
3034
|
-
const H2 = mod(H * H, P)
|
|
3035
|
-
const H3 = mod(H * H2, P)
|
|
3036
|
-
const U1H2 = mod(U1 * H2, P)
|
|
3037
|
-
const X3 = mod(R * R - H3 - 2n * U1H2, P)
|
|
3038
|
-
return [X3, mod(R * (U1H2 - X3) - S1 * H3, P), mod(H * p[2] * q[2], P)]
|
|
3039
|
-
}
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
function scalarMult(k , G , prime , a , order ) {
|
|
3049
|
-
if (k === 0n || k >= order) {
|
|
2998
|
+
|
|
2999
|
+
|
|
3000
|
+
|
|
3001
|
+
|
|
3002
|
+
|
|
3003
|
+
|
|
3004
|
+
|
|
3005
|
+
|
|
3006
|
+
|
|
3007
|
+
|
|
3008
|
+
|
|
3009
|
+
|
|
3010
|
+
|
|
3011
|
+
|
|
3012
|
+
|
|
3013
|
+
|
|
3014
|
+
async function importKeyNist(
|
|
3015
|
+
curve ,
|
|
3016
|
+
key ,
|
|
3017
|
+
name ,
|
|
3018
|
+
extractable ,
|
|
3019
|
+
includePublicKey ,
|
|
3020
|
+
) {
|
|
3021
|
+
const d = OS2IP(key)
|
|
3022
|
+
if (d === 0n || d >= curve.order) {
|
|
3050
3023
|
throw new Error('Invalid scalar')
|
|
3051
3024
|
}
|
|
3052
3025
|
|
|
3053
|
-
|
|
3054
|
-
const
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
let d = Number(s & 15n)
|
|
3066
|
-
if (d >= 8) d -= 16
|
|
3067
|
-
naf.push(d)
|
|
3068
|
-
s -= BigInt(d)
|
|
3069
|
-
} else {
|
|
3070
|
-
naf.push(0)
|
|
3026
|
+
|
|
3027
|
+
const pkcs8 = concat(curve.pkcs8, key)
|
|
3028
|
+
return await subtle(async (c) => {
|
|
3029
|
+
async function fromJwk(jwk , privateKey ) {
|
|
3030
|
+
privateKey ??= await c.importKey('jwk', jwk, curve.algorithm, extractable, ['deriveBits'])
|
|
3031
|
+
delete jwk.d
|
|
3032
|
+
return {
|
|
3033
|
+
privateKey,
|
|
3034
|
+
publicKey: includePublicKey
|
|
3035
|
+
? await c.importKey('jwk', jwk, curve.algorithm, true, [])
|
|
3036
|
+
: undefined,
|
|
3037
|
+
}
|
|
3071
3038
|
}
|
|
3072
|
-
s >>= 1n
|
|
3073
|
-
}
|
|
3074
3039
|
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3040
|
+
let privateKey
|
|
3041
|
+
|
|
3042
|
+
const nativePublicKey = typeof c.getPublicKey === 'function'
|
|
3043
|
+
try {
|
|
3044
|
+
privateKey = await c.importKey(
|
|
3045
|
+
'pkcs8',
|
|
3046
|
+
pkcs8 ,
|
|
3047
|
+
curve.algorithm,
|
|
3048
|
+
nativePublicKey ? extractable : true,
|
|
3049
|
+
['deriveBits'],
|
|
3050
|
+
)
|
|
3051
|
+
if (nativePublicKey) {
|
|
3052
|
+
|
|
3053
|
+
|
|
3054
|
+
const recovered = await c.getPublicKey(privateKey, [])
|
|
3055
|
+
return { privateKey, publicKey: includePublicKey ? recovered : undefined }
|
|
3056
|
+
}
|
|
3057
|
+
const { x, y, d } = await c.exportKey('jwk', privateKey)
|
|
3058
|
+
return fromJwk(
|
|
3059
|
+
{ kty: 'EC', crv: curve.algorithm.namedCurve, x, y, d },
|
|
3060
|
+
extractable ? privateKey : undefined,
|
|
3061
|
+
)
|
|
3062
|
+
} catch (cause) {
|
|
3063
|
+
|
|
3064
|
+
|
|
3065
|
+
|
|
3066
|
+
|
|
3067
|
+
if (
|
|
3068
|
+
!(cause instanceof DOMException) ||
|
|
3069
|
+
(cause.name !== 'DataError' && cause.name !== 'OperationError')
|
|
3070
|
+
) {
|
|
3071
|
+
throw cause
|
|
3072
|
+
}
|
|
3083
3073
|
}
|
|
3084
|
-
}
|
|
3085
|
-
|
|
3086
|
-
const zI = modInverse(r[2], prime)
|
|
3087
|
-
const zI2 = mod(zI * zI, prime)
|
|
3088
|
-
return { x: mod(r[0] * zI2, prime), y: mod(r[1] * zI2 * zI, prime) }
|
|
3089
|
-
}
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
function getPrivateJwkNist(DHKEM , d ) {
|
|
3104
|
-
|
|
3105
|
-
const G = { x: DHKEM.Gx, y: DHKEM.Gy }
|
|
3106
|
-
const publicPoint = scalarMult(d, G, DHKEM.prime, DHKEM.prime - 3n, DHKEM.order)
|
|
3107
|
-
|
|
3108
|
-
const coordSize = (DHKEM.Npk - 1) / 2
|
|
3109
|
-
const xBytes = bigIntToUint8Array(publicPoint.x, coordSize)
|
|
3110
|
-
const yBytes = bigIntToUint8Array(publicPoint.y, coordSize)
|
|
3111
|
-
const dBytes = bigIntToUint8Array(d, DHKEM.Nsk)
|
|
3112
3074
|
|
|
3075
|
+
const generator = {
|
|
3076
|
+
kty: 'EC',
|
|
3077
|
+
crv: curve.algorithm.namedCurve,
|
|
3078
|
+
x: toB64u(bigIntToUint8Array(curve.Gx, curve.Nsk)),
|
|
3079
|
+
y: toB64u(bigIntToUint8Array(curve.Gy, curve.Nsk)),
|
|
3080
|
+
}
|
|
3081
|
+
const jwk = { ...generator, d: toB64u(slice(pkcs8, curve.pkcs8.length)) }
|
|
3082
|
+
const signingAlgorithm = { name: 'ECDSA', namedCurve: curve.algorithm.namedCurve }
|
|
3083
|
+
const signingKey = privateKey
|
|
3084
|
+
? await c.importKey('pkcs8', pkcs8 , signingAlgorithm, false, ['sign'])
|
|
3085
|
+
: await c.importKey('jwk', jwk, signingAlgorithm, false, ['sign'])
|
|
3086
|
+
privateKey ??= await c.importKey('jwk', jwk, curve.algorithm, false, ['deriveBits'])
|
|
3087
|
+
|
|
3088
|
+
|
|
3089
|
+
|
|
3090
|
+
const generatorKey = await c.importKey('jwk', generator, curve.algorithm, true, [])
|
|
3091
|
+
const x = new Uint8Array(
|
|
3092
|
+
await c.deriveBits({ name: 'ECDH', public: generatorKey }, privateKey, curve.Nsk << 3),
|
|
3093
|
+
)
|
|
3113
3094
|
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3095
|
+
|
|
3096
|
+
|
|
3097
|
+
const message = L_public_key_recovery
|
|
3098
|
+
const signatureAlgorithm = { name: 'ECDSA', hash: 'SHA-256' }
|
|
3099
|
+
const signature = await c.sign(signatureAlgorithm, signingKey, message)
|
|
3100
|
+
for (const prefix of [0x02, 0x03]) {
|
|
3101
|
+
const spki = concat(curve.spki, Uint8Array.of(prefix), x)
|
|
3102
|
+
const candidate = await c.importKey('spki', spki , signingAlgorithm, true, [
|
|
3103
|
+
'verify',
|
|
3104
|
+
])
|
|
3105
|
+
if (await c.verify(signatureAlgorithm, candidate, signature, message)) {
|
|
3106
|
+
const { x, y } = await c.exportKey('jwk', candidate)
|
|
3107
|
+
return fromJwk({ kty: 'EC', crv: generator.crv, d: jwk.d, x, y })
|
|
3108
|
+
}
|
|
3109
|
+
}
|
|
3110
|
+
throw new Error('Public key recovery failed')
|
|
3111
|
+
}, name)
|
|
3121
3112
|
}
|
|
3122
3113
|
|
|
3123
3114
|
async function DeserializePrivateKeyNist(
|
|
3124
|
-
|
|
3115
|
+
|
|
3125
3116
|
key ,
|
|
3126
3117
|
extractable ,
|
|
3127
3118
|
) {
|
|
3128
|
-
|
|
3129
|
-
const jwk = getPrivateJwkNist(this, d)
|
|
3130
|
-
|
|
3131
|
-
const privateKey = await subtle(
|
|
3132
|
-
(c) => c.importKey('jwk', jwk, this.algorithm, extractable, ['deriveBits']),
|
|
3133
|
-
this.name,
|
|
3134
|
-
)
|
|
3135
|
-
|
|
3136
|
-
return privateKey
|
|
3119
|
+
return (await importKeyNist(this, key, this.name, extractable, false)).privateKey
|
|
3137
3120
|
}
|
|
3138
3121
|
|
|
3139
|
-
|
|
3122
|
+
|
|
3140
3123
|
async function DeriveKeyPairNist(
|
|
3141
|
-
|
|
3124
|
+
|
|
3142
3125
|
ikm ,
|
|
3143
3126
|
extractable ,
|
|
3144
3127
|
) {
|
|
@@ -3164,35 +3147,22 @@ async function GetKeyPairNist(
|
|
|
3164
3147
|
extractable ,
|
|
3165
3148
|
name ,
|
|
3166
3149
|
) {
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
const privateKey = await subtle(
|
|
3170
|
-
(c) => c.importKey('jwk', jwk, curveConfig.algorithm, extractable, ['deriveBits']),
|
|
3171
|
-
name,
|
|
3172
|
-
)
|
|
3173
|
-
|
|
3174
|
-
delete jwk.d
|
|
3175
|
-
const publicKey = await subtle(
|
|
3176
|
-
(c) => c.importKey('jwk', jwk, curveConfig.algorithm, true, []),
|
|
3177
|
-
name,
|
|
3178
|
-
)
|
|
3179
|
-
|
|
3180
|
-
return { privateKey, publicKey }
|
|
3150
|
+
return (await importKeyNist(curveConfig, sk, name, extractable, true))
|
|
3181
3151
|
}
|
|
3182
3152
|
|
|
3153
|
+
|
|
3154
|
+
|
|
3155
|
+
|
|
3183
3156
|
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3157
|
+
|
|
3188
3158
|
async function DeriveKeyPairX( ikm , extractable ) {
|
|
3189
3159
|
const sk = await DeriveKeyPairBytes(this, ikm, L_sk, new Uint8Array())
|
|
3190
3160
|
return await createKeyPairFromPrivateKey(this, sk, extractable)
|
|
3191
3161
|
}
|
|
3192
3162
|
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3163
|
+
|
|
3164
|
+
|
|
3165
|
+
|
|
3196
3166
|
|
|
3197
3167
|
const P256 = {
|
|
3198
3168
|
algorithm: { name: 'ECDH', namedCurve: 'P-256' },
|
|
@@ -3200,7 +3170,8 @@ const P256 = {
|
|
|
3200
3170
|
Nsk: 32,
|
|
3201
3171
|
order: 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n,
|
|
3202
3172
|
bitmask: 0xff,
|
|
3203
|
-
|
|
3173
|
+
pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x41, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, 0x27, 0x30, 0x25, 0x02, 0x01, 0x01, 0x04, 0x20),
|
|
3174
|
+
spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x39, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x22, 0x00),
|
|
3204
3175
|
Gx: 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296n,
|
|
3205
3176
|
Gy: 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5n,
|
|
3206
3177
|
}
|
|
@@ -3230,7 +3201,7 @@ export const KEM_DHKEM_P256_HKDF_SHA256 = function ()
|
|
|
3230
3201
|
const id = 0x0010
|
|
3231
3202
|
const name = 'DHKEM(P-256, HKDF-SHA256)'
|
|
3232
3203
|
const kdf = KDF_HKDF_SHA256()
|
|
3233
|
-
|
|
3204
|
+
|
|
3234
3205
|
kdf.name = name
|
|
3235
3206
|
return {
|
|
3236
3207
|
id,
|
|
@@ -3255,8 +3226,8 @@ const P384 = {
|
|
|
3255
3226
|
order:
|
|
3256
3227
|
0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973n,
|
|
3257
3228
|
bitmask: 0xff,
|
|
3258
|
-
|
|
3259
|
-
|
|
3229
|
+
pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x4e, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22, 0x04, 0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30),
|
|
3230
|
+
spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x46, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22, 0x03, 0x32, 0x00),
|
|
3260
3231
|
Gx: 0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7n,
|
|
3261
3232
|
Gy: 0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5fn,
|
|
3262
3233
|
}
|
|
@@ -3286,7 +3257,7 @@ export const KEM_DHKEM_P384_HKDF_SHA384 = function ()
|
|
|
3286
3257
|
const id = 0x0011
|
|
3287
3258
|
const name = 'DHKEM(P-384, HKDF-SHA384)'
|
|
3288
3259
|
const kdf = KDF_HKDF_SHA384()
|
|
3289
|
-
|
|
3260
|
+
|
|
3290
3261
|
kdf.name = name
|
|
3291
3262
|
return {
|
|
3292
3263
|
id,
|
|
@@ -3311,8 +3282,8 @@ const P521 = {
|
|
|
3311
3282
|
order:
|
|
3312
3283
|
0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409n,
|
|
3313
3284
|
bitmask: 0x01,
|
|
3314
|
-
|
|
3315
|
-
|
|
3285
|
+
pkcs8: /* @__PURE__ */ Uint8Array.of(0x30, 0x60, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23, 0x04, 0x49, 0x30, 0x47, 0x02, 0x01, 0x01, 0x04, 0x42),
|
|
3286
|
+
spki: /* @__PURE__ */ Uint8Array.of(0x30, 0x58, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23, 0x03, 0x44, 0x00),
|
|
3316
3287
|
Gx: 0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66n,
|
|
3317
3288
|
Gy: 0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650n,
|
|
3318
3289
|
}
|
|
@@ -3342,7 +3313,7 @@ export const KEM_DHKEM_P521_HKDF_SHA512 = function ()
|
|
|
3342
3313
|
const id = 0x0012
|
|
3343
3314
|
const name = 'DHKEM(P-521, HKDF-SHA512)'
|
|
3344
3315
|
const kdf = KDF_HKDF_SHA512()
|
|
3345
|
-
|
|
3316
|
+
|
|
3346
3317
|
kdf.name = name
|
|
3347
3318
|
return {
|
|
3348
3319
|
id,
|
|
@@ -3360,9 +3331,9 @@ export const KEM_DHKEM_P521_HKDF_SHA512 = function ()
|
|
|
3360
3331
|
}
|
|
3361
3332
|
}
|
|
3362
3333
|
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3334
|
+
|
|
3335
|
+
|
|
3336
|
+
|
|
3366
3337
|
|
|
3367
3338
|
|
|
3368
3339
|
|
|
@@ -3385,12 +3356,12 @@ export const KEM_DHKEM_P521_HKDF_SHA512 = function ()
|
|
|
3385
3356
|
|
|
3386
3357
|
|
|
3387
3358
|
export const KEM_DHKEM_X25519_HKDF_SHA256 = function ()
|
|
3388
|
-
|
|
3359
|
+
|
|
3389
3360
|
{
|
|
3390
3361
|
const id = 0x0020
|
|
3391
3362
|
const name = 'DHKEM(X25519, HKDF-SHA256)'
|
|
3392
3363
|
const kdf = KDF_HKDF_SHA256()
|
|
3393
|
-
|
|
3364
|
+
|
|
3394
3365
|
kdf.name = name
|
|
3395
3366
|
return {
|
|
3396
3367
|
id,
|
|
@@ -3404,7 +3375,7 @@ export const KEM_DHKEM_X25519_HKDF_SHA256 = function ()
|
|
|
3404
3375
|
Nsk: 32,
|
|
3405
3376
|
Ndh: 32,
|
|
3406
3377
|
algorithm: { name: 'X25519' },
|
|
3407
|
-
pkcs8: Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20),
|
|
3378
|
+
pkcs8: Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20),
|
|
3408
3379
|
DeriveKeyPair: DeriveKeyPairX,
|
|
3409
3380
|
async DeserializePrivateKey(key, extractable) {
|
|
3410
3381
|
return await CurveKeyFromD(name, this.Nsk, this.pkcs8, this.algorithm, key, extractable)
|
|
@@ -3434,12 +3405,12 @@ export const KEM_DHKEM_X25519_HKDF_SHA256 = function ()
|
|
|
3434
3405
|
|
|
3435
3406
|
|
|
3436
3407
|
export const KEM_DHKEM_X448_HKDF_SHA512 = function ()
|
|
3437
|
-
|
|
3408
|
+
|
|
3438
3409
|
{
|
|
3439
3410
|
const id = 0x0021
|
|
3440
3411
|
const name = 'DHKEM(X448, HKDF-SHA512)'
|
|
3441
3412
|
const kdf = KDF_HKDF_SHA512()
|
|
3442
|
-
|
|
3413
|
+
|
|
3443
3414
|
kdf.name = name
|
|
3444
3415
|
return {
|
|
3445
3416
|
id,
|
|
@@ -3453,7 +3424,7 @@ export const KEM_DHKEM_X448_HKDF_SHA512 = function ()
|
|
|
3453
3424
|
Nsk: 56,
|
|
3454
3425
|
Ndh: 56,
|
|
3455
3426
|
algorithm: { name: 'X448' },
|
|
3456
|
-
pkcs8: Uint8Array.of(0x30, 0x46, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6f, 0x04, 0x3a, 0x04, 0x38),
|
|
3427
|
+
pkcs8: Uint8Array.of(0x30, 0x46, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6f, 0x04, 0x3a, 0x04, 0x38),
|
|
3457
3428
|
DeriveKeyPair: DeriveKeyPairX,
|
|
3458
3429
|
async DeserializePrivateKey(key, extractable) {
|
|
3459
3430
|
return await CurveKeyFromD(name, this.Nsk, this.pkcs8, this.algorithm, key, extractable)
|
|
@@ -3462,17 +3433,17 @@ export const KEM_DHKEM_X448_HKDF_SHA512 = function ()
|
|
|
3462
3433
|
}
|
|
3463
3434
|
}
|
|
3464
3435
|
|
|
3436
|
+
|
|
3437
|
+
|
|
3438
|
+
|
|
3465
3439
|
|
|
3440
|
+
|
|
3466
3441
|
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3442
|
+
|
|
3443
|
+
|
|
3444
|
+
|
|
3445
|
+
|
|
3446
|
+
|
|
3476
3447
|
|
|
3477
3448
|
function DIRECT_KEM_SHARED() {
|
|
3478
3449
|
return {
|
|
@@ -3487,14 +3458,14 @@ function DIRECT_KEM_SHARED() {
|
|
|
3487
3458
|
)
|
|
3488
3459
|
|
|
3489
3460
|
const privateKey = (await this.DeserializePrivateKey(dk, extractable))
|
|
3490
|
-
|
|
3461
|
+
|
|
3491
3462
|
const usages = ['encapsulateBits']
|
|
3492
3463
|
const publicKey = await getPublicKey(this.name, privateKey, usages)
|
|
3493
3464
|
|
|
3494
3465
|
return { privateKey, publicKey }
|
|
3495
3466
|
},
|
|
3496
3467
|
async GenerateKeyPair( extractable) {
|
|
3497
|
-
|
|
3468
|
+
|
|
3498
3469
|
const usages = ['encapsulateBits', 'decapsulateBits']
|
|
3499
3470
|
return (await subtle(
|
|
3500
3471
|
(c) => c.generateKey(this.algorithm, extractable, usages),
|
|
@@ -3504,14 +3475,14 @@ function DIRECT_KEM_SHARED() {
|
|
|
3504
3475
|
async SerializePublicKey( key) {
|
|
3505
3476
|
assertKeyAlgorithm(key, this.algorithm)
|
|
3506
3477
|
assertCryptoKey(key)
|
|
3507
|
-
|
|
3478
|
+
|
|
3508
3479
|
const format = 'raw-public'
|
|
3509
3480
|
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3510
3481
|
},
|
|
3511
3482
|
async DeserializePublicKey( key) {
|
|
3512
|
-
|
|
3483
|
+
|
|
3513
3484
|
const format = 'raw-public'
|
|
3514
|
-
|
|
3485
|
+
|
|
3515
3486
|
const usages = ['encapsulateBits']
|
|
3516
3487
|
return await subtle(
|
|
3517
3488
|
(c) => c.importKey(format, key , this.algorithm, true, usages),
|
|
@@ -3521,14 +3492,14 @@ function DIRECT_KEM_SHARED() {
|
|
|
3521
3492
|
async SerializePrivateKey( key) {
|
|
3522
3493
|
assertKeyAlgorithm(key, this.algorithm)
|
|
3523
3494
|
assertCryptoKey(key)
|
|
3524
|
-
|
|
3495
|
+
|
|
3525
3496
|
const format = 'raw-seed'
|
|
3526
3497
|
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3527
3498
|
},
|
|
3528
3499
|
async DeserializePrivateKey( key, extractable) {
|
|
3529
|
-
|
|
3500
|
+
|
|
3530
3501
|
const format = 'raw-seed'
|
|
3531
|
-
|
|
3502
|
+
|
|
3532
3503
|
const usages = ['decapsulateBits']
|
|
3533
3504
|
return await subtle(
|
|
3534
3505
|
(c) => c.importKey(format, key , this.algorithm, extractable, usages),
|
|
@@ -3539,7 +3510,7 @@ function DIRECT_KEM_SHARED() {
|
|
|
3539
3510
|
assertKeyAlgorithm(pkR, this.algorithm)
|
|
3540
3511
|
|
|
3541
3512
|
const { sharedKey, ciphertext } = (await subtle(
|
|
3542
|
-
|
|
3513
|
+
|
|
3543
3514
|
(c) => c.encapsulateBits(this.algorithm, pkR),
|
|
3544
3515
|
this.name,
|
|
3545
3516
|
))
|
|
@@ -3550,7 +3521,7 @@ function DIRECT_KEM_SHARED() {
|
|
|
3550
3521
|
assertKeyAlgorithm(skR, this.algorithm)
|
|
3551
3522
|
return new Uint8Array(
|
|
3552
3523
|
await subtle(
|
|
3553
|
-
|
|
3524
|
+
|
|
3554
3525
|
(c) => c.decapsulateBits(this.algorithm, skR, enc ),
|
|
3555
3526
|
this.name,
|
|
3556
3527
|
),
|
|
@@ -3561,7 +3532,7 @@ function DIRECT_KEM_SHARED() {
|
|
|
3561
3532
|
|
|
3562
3533
|
function createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk } ) {
|
|
3563
3534
|
const kdf = KDF_SHAKE256()
|
|
3564
|
-
|
|
3535
|
+
|
|
3565
3536
|
kdf.name = name
|
|
3566
3537
|
return {
|
|
3567
3538
|
id,
|
|
@@ -3578,9 +3549,9 @@ function createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk } )
|
|
|
3578
3549
|
}
|
|
3579
3550
|
}
|
|
3580
3551
|
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3552
|
+
|
|
3553
|
+
|
|
3554
|
+
|
|
3584
3555
|
|
|
3585
3556
|
|
|
3586
3557
|
|
|
@@ -3673,20 +3644,20 @@ export const KEM_ML_KEM_1024 = function () {
|
|
|
3673
3644
|
return createDirectKEM({ id, name, Nsecret, Nenc, Npk, Nsk })
|
|
3674
3645
|
}
|
|
3675
3646
|
|
|
3647
|
+
|
|
3648
|
+
|
|
3649
|
+
|
|
3650
|
+
|
|
3676
3651
|
|
|
3677
|
-
|
|
3678
|
-
|
|
3679
|
-
|
|
3680
|
-
|
|
3681
|
-
|
|
3652
|
+
|
|
3682
3653
|
|
|
3683
3654
|
function AEAD_SHARED(P_MAX ) {
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
const cache = new WeakMap
|
|
3655
|
+
|
|
3656
|
+
|
|
3657
|
+
|
|
3658
|
+
|
|
3659
|
+
|
|
3660
|
+
const cache = new WeakMap ()
|
|
3690
3661
|
async function importKey( key ) {
|
|
3691
3662
|
return await subtle(
|
|
3692
3663
|
(c) =>
|
|
@@ -3702,8 +3673,7 @@ function AEAD_SHARED(P_MAX ) {
|
|
|
3702
3673
|
if (pt.byteLength > P_MAX) {
|
|
3703
3674
|
throw new RangeError('"pt" exceeds P_MAX')
|
|
3704
3675
|
}
|
|
3705
|
-
const cryptoKey =
|
|
3706
|
-
cache.get(key) ?? (await cacheValue(cache, key, () => importKey.call(this, key)))
|
|
3676
|
+
const cryptoKey = await getCachedCryptoKey(cache, key, (bytes) => importKey.call(this, bytes))
|
|
3707
3677
|
return new Uint8Array(
|
|
3708
3678
|
await subtle(
|
|
3709
3679
|
(c) =>
|
|
@@ -3721,8 +3691,7 @@ function AEAD_SHARED(P_MAX ) {
|
|
|
3721
3691
|
)
|
|
3722
3692
|
},
|
|
3723
3693
|
async Open( key, nonce, aad, ct) {
|
|
3724
|
-
const cryptoKey =
|
|
3725
|
-
cache.get(key) ?? (await cacheValue(cache, key, () => importKey.call(this, key)))
|
|
3694
|
+
const cryptoKey = await getCachedCryptoKey(cache, key, (bytes) => importKey.call(this, bytes))
|
|
3726
3695
|
return new Uint8Array(
|
|
3727
3696
|
await subtle(
|
|
3728
3697
|
(c) =>
|
|
@@ -3742,9 +3711,9 @@ function AEAD_SHARED(P_MAX ) {
|
|
|
3742
3711
|
}
|
|
3743
3712
|
}
|
|
3744
3713
|
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
3714
|
+
|
|
3715
|
+
|
|
3716
|
+
|
|
3748
3717
|
|
|
3749
3718
|
|
|
3750
3719
|
|
|
@@ -3837,23 +3806,23 @@ export const AEAD_ChaCha20Poly1305 = function AEAD_ChaCha20Poly1305
|
|
|
3837
3806
|
Nn: 12,
|
|
3838
3807
|
Nt: 16,
|
|
3839
3808
|
algorithm: 'ChaCha20-Poly1305',
|
|
3840
|
-
|
|
3809
|
+
|
|
3841
3810
|
keyFormat: 'raw-secret',
|
|
3842
3811
|
...AEAD_SHARED(CHACHA20_POLY1305_P_MAX),
|
|
3843
3812
|
}
|
|
3844
3813
|
}
|
|
3845
3814
|
|
|
3815
|
+
|
|
3816
|
+
|
|
3817
|
+
|
|
3846
3818
|
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3819
|
+
|
|
3851
3820
|
const InvalidInvocation = (_ ) => {
|
|
3852
3821
|
if (_ !== priv) {
|
|
3853
3822
|
throw new Error('invalid invocation')
|
|
3854
3823
|
}
|
|
3855
3824
|
}
|
|
3856
|
-
const priv = Symbol()
|
|
3825
|
+
const priv = /* @__PURE__ */ Symbol()
|
|
3857
3826
|
class HybridKey {
|
|
3858
3827
|
#algorithm
|
|
3859
3828
|
#type
|
|
@@ -3961,19 +3930,19 @@ function RandomScalarNist(t , seed )
|
|
|
3961
3930
|
return bigIntToUint8Array(sk_bigint, t.Nscalar )
|
|
3962
3931
|
}
|
|
3963
3932
|
|
|
3964
|
-
|
|
3933
|
+
|
|
3965
3934
|
async function expandDecapsKeyG(kem , seed ) {
|
|
3966
3935
|
const Nout = kem.pq.Nseed + kem.t.Nseed
|
|
3967
3936
|
const bits = Nout << 3
|
|
3968
|
-
|
|
3937
|
+
|
|
3969
3938
|
const algorithm = { name: 'cSHAKE256', length: bits, outputLength: bits }
|
|
3970
3939
|
const seed_full = await subtle((c) => c.digest(algorithm, seed ), kem.name)
|
|
3971
3940
|
|
|
3972
3941
|
const [seed_PQ, seed_T] = split(kem.pq.Nseed, kem.t.Nseed, new Uint8Array(seed_full))
|
|
3973
3942
|
|
|
3974
|
-
|
|
3943
|
+
|
|
3975
3944
|
const format = 'raw-seed'
|
|
3976
|
-
|
|
3945
|
+
|
|
3977
3946
|
const usages = ['decapsulateBits', 'encapsulateBits']
|
|
3978
3947
|
const dk_PQ = await subtle(
|
|
3979
3948
|
(c) => c.importKey(format, seed_PQ , kem.pq.algorithm, true, [usages[0]]),
|
|
@@ -3987,7 +3956,7 @@ async function expandDecapsKeyG(kem , seed ) {
|
|
|
3987
3956
|
return { ek_PQ, ek_T, dk_PQ, dk_T }
|
|
3988
3957
|
}
|
|
3989
3958
|
|
|
3990
|
-
|
|
3959
|
+
|
|
3991
3960
|
async function C2PRICombiner(
|
|
3992
3961
|
kem ,
|
|
3993
3962
|
ss_PQ ,
|
|
@@ -4001,14 +3970,14 @@ async function C2PRICombiner(
|
|
|
4001
3970
|
return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data), kem.name))
|
|
4002
3971
|
}
|
|
4003
3972
|
|
|
4004
|
-
|
|
3973
|
+
|
|
4005
3974
|
async function prepareEncapsG(
|
|
4006
3975
|
kem ,
|
|
4007
3976
|
ek_PQ ,
|
|
4008
3977
|
ek_T ,
|
|
4009
3978
|
) {
|
|
4010
3979
|
const res = (await subtle(
|
|
4011
|
-
|
|
3980
|
+
|
|
4012
3981
|
(c) => c.encapsulateBits(kem.pq.algorithm, ek_PQ),
|
|
4013
3982
|
kem.name,
|
|
4014
3983
|
))
|
|
@@ -4027,12 +3996,11 @@ async function prepareEncapsG(
|
|
|
4027
3996
|
kem.name,
|
|
4028
3997
|
),
|
|
4029
3998
|
)
|
|
4030
|
-
checkNotAllZeros(ss_T)
|
|
4031
3999
|
|
|
4032
4000
|
return [ss_PQ, ss_T, ct_PQ, ct_T]
|
|
4033
4001
|
}
|
|
4034
4002
|
|
|
4035
|
-
|
|
4003
|
+
|
|
4036
4004
|
async function prepareDecapsG(
|
|
4037
4005
|
kem ,
|
|
4038
4006
|
dk_PQ ,
|
|
@@ -4042,7 +4010,7 @@ async function prepareDecapsG(
|
|
|
4042
4010
|
) {
|
|
4043
4011
|
const ss_PQ = new Uint8Array(
|
|
4044
4012
|
await subtle(
|
|
4045
|
-
|
|
4013
|
+
|
|
4046
4014
|
(c) => c.decapsulateBits(kem.pq.algorithm, dk_PQ, ct_PQ),
|
|
4047
4015
|
kem.name,
|
|
4048
4016
|
),
|
|
@@ -4059,40 +4027,39 @@ async function prepareDecapsG(
|
|
|
4059
4027
|
kem.name,
|
|
4060
4028
|
),
|
|
4061
4029
|
)
|
|
4062
|
-
checkNotAllZeros(ss_T)
|
|
4063
4030
|
|
|
4064
4031
|
return [ss_PQ, ss_T]
|
|
4065
4032
|
}
|
|
4066
4033
|
|
|
4067
|
-
|
|
4068
|
-
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
|
|
4074
|
-
|
|
4075
|
-
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
|
|
4080
|
-
|
|
4081
|
-
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
|
|
4091
|
-
|
|
4092
|
-
|
|
4093
|
-
|
|
4094
|
-
|
|
4095
|
-
|
|
4034
|
+
|
|
4035
|
+
|
|
4036
|
+
|
|
4037
|
+
|
|
4038
|
+
|
|
4039
|
+
|
|
4040
|
+
|
|
4041
|
+
|
|
4042
|
+
|
|
4043
|
+
|
|
4044
|
+
|
|
4045
|
+
|
|
4046
|
+
|
|
4047
|
+
|
|
4048
|
+
|
|
4049
|
+
|
|
4050
|
+
|
|
4051
|
+
|
|
4052
|
+
|
|
4053
|
+
|
|
4054
|
+
|
|
4055
|
+
|
|
4056
|
+
|
|
4057
|
+
|
|
4058
|
+
|
|
4059
|
+
|
|
4060
|
+
|
|
4061
|
+
|
|
4062
|
+
|
|
4096
4063
|
|
|
4097
4064
|
function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
4098
4065
|
Object.freeze(HybridKey.prototype)
|
|
@@ -4129,7 +4096,7 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
|
4129
4096
|
async SerializePublicKey( key) {
|
|
4130
4097
|
assertKeyAlgorithm(key, this.algorithm)
|
|
4131
4098
|
HybridKey.validate(key, true)
|
|
4132
|
-
|
|
4099
|
+
|
|
4133
4100
|
const format = 'raw-public'
|
|
4134
4101
|
const ek_PQ = new Uint8Array(
|
|
4135
4102
|
await subtle((c) => c.exportKey(format, key.getPq(priv)), this.name),
|
|
@@ -4141,9 +4108,9 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
|
4141
4108
|
return concat(ek_PQ, ek_T)
|
|
4142
4109
|
},
|
|
4143
4110
|
async DeserializePublicKey( key) {
|
|
4144
|
-
|
|
4111
|
+
|
|
4145
4112
|
const format = 'raw-public'
|
|
4146
|
-
|
|
4113
|
+
|
|
4147
4114
|
const usages = ['encapsulateBits']
|
|
4148
4115
|
const pubPq = key.subarray(0, this.pq.Npk)
|
|
4149
4116
|
const pubT = key.subarray(this.pq.Npk)
|
|
@@ -4161,7 +4128,8 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
|
4161
4128
|
return key.getSeed(priv)
|
|
4162
4129
|
},
|
|
4163
4130
|
async DeserializePrivateKey( key, extractable) {
|
|
4164
|
-
const
|
|
4131
|
+
const seed = slice(key)
|
|
4132
|
+
const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, seed)
|
|
4165
4133
|
const publicKey = new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
4166
4134
|
const privateKey = new HybridKey(
|
|
4167
4135
|
priv,
|
|
@@ -4170,7 +4138,7 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
|
4170
4138
|
extractable,
|
|
4171
4139
|
dk_PQ,
|
|
4172
4140
|
dk_T,
|
|
4173
|
-
|
|
4141
|
+
seed,
|
|
4174
4142
|
publicKey,
|
|
4175
4143
|
)
|
|
4176
4144
|
|
|
@@ -4212,7 +4180,7 @@ function COMPOSITE_HYBRID_KEM_SHARED() {
|
|
|
4212
4180
|
|
|
4213
4181
|
function createCompositeHybridKEM(parameters ) {
|
|
4214
4182
|
const kdf = KDF_SHAKE256()
|
|
4215
|
-
|
|
4183
|
+
|
|
4216
4184
|
kdf.name = parameters.name
|
|
4217
4185
|
return {
|
|
4218
4186
|
...parameters,
|
|
@@ -4224,8 +4192,9 @@ function createCompositeHybridKEM(parameters )
|
|
|
4224
4192
|
}
|
|
4225
4193
|
}
|
|
4226
4194
|
|
|
4227
|
-
|
|
4228
|
-
|
|
4195
|
+
|
|
4196
|
+
|
|
4197
|
+
|
|
4229
4198
|
|
|
4230
4199
|
|
|
4231
4200
|
|
|
@@ -4260,12 +4229,12 @@ export const KEM_MLKEM768_X25519 = function ()
|
|
|
4260
4229
|
const Npk = 1216
|
|
4261
4230
|
const Nsk = 32
|
|
4262
4231
|
const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
|
|
4263
|
-
|
|
4232
|
+
|
|
4264
4233
|
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4265
4234
|
return createDirectKEM(kem)
|
|
4266
4235
|
}
|
|
4267
4236
|
|
|
4268
|
-
const pkcs8 = Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20)
|
|
4237
|
+
const pkcs8 = Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20)
|
|
4269
4238
|
return createCompositeHybridKEM({
|
|
4270
4239
|
...kem,
|
|
4271
4240
|
pq: { algorithm: { name: 'ML-KEM-768' }, Nseed: 64, Npk: 1184, Nct: 1088 },
|
|
@@ -4283,7 +4252,7 @@ export const KEM_MLKEM768_X25519 = function ()
|
|
|
4283
4252
|
return { privateKey, publicKey }
|
|
4284
4253
|
},
|
|
4285
4254
|
},
|
|
4286
|
-
label: Uint8Array.of(0x5c, 0x2e, 0x2f, 0x2f, 0x5e, 0x5c),
|
|
4255
|
+
label: Uint8Array.of(0x5c, 0x2e, 0x2f, 0x2f, 0x5e, 0x5c),
|
|
4287
4256
|
})
|
|
4288
4257
|
}
|
|
4289
4258
|
|
|
@@ -4309,6 +4278,7 @@ export const KEM_MLKEM768_X25519 = function ()
|
|
|
4309
4278
|
|
|
4310
4279
|
|
|
4311
4280
|
|
|
4281
|
+
|
|
4312
4282
|
|
|
4313
4283
|
|
|
4314
4284
|
export const KEM_MLKEM768_P256 = function () {
|
|
@@ -4319,7 +4289,7 @@ export const KEM_MLKEM768_P256 = function ()
|
|
|
4319
4289
|
const Npk = 1249
|
|
4320
4290
|
const Nsk = 32
|
|
4321
4291
|
const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
|
|
4322
|
-
|
|
4292
|
+
|
|
4323
4293
|
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4324
4294
|
return createDirectKEM(kem)
|
|
4325
4295
|
}
|
|
@@ -4341,7 +4311,7 @@ export const KEM_MLKEM768_P256 = function ()
|
|
|
4341
4311
|
return GetKeyPairNist(P256, sk, true, name)
|
|
4342
4312
|
},
|
|
4343
4313
|
},
|
|
4344
|
-
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x37, 0x36, 0x38, 0x2d, 0x50, 0x32, 0x35, 0x36),
|
|
4314
|
+
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x37, 0x36, 0x38, 0x2d, 0x50, 0x32, 0x35, 0x36),
|
|
4345
4315
|
})
|
|
4346
4316
|
}
|
|
4347
4317
|
|
|
@@ -4367,6 +4337,7 @@ export const KEM_MLKEM768_P256 = function ()
|
|
|
4367
4337
|
|
|
4368
4338
|
|
|
4369
4339
|
|
|
4340
|
+
|
|
4370
4341
|
|
|
4371
4342
|
|
|
4372
4343
|
export const KEM_MLKEM1024_P384 = function () {
|
|
@@ -4377,7 +4348,7 @@ export const KEM_MLKEM1024_P384 = function ()
|
|
|
4377
4348
|
const Npk = 1665
|
|
4378
4349
|
const Nsk = 32
|
|
4379
4350
|
const kem = { id, name, Nsecret, Nenc, Npk, Nsk }
|
|
4380
|
-
|
|
4351
|
+
|
|
4381
4352
|
if (SubtleCrypto.supports?.('generateKey', name) ?? false) {
|
|
4382
4353
|
return createDirectKEM(kem)
|
|
4383
4354
|
}
|
|
@@ -4400,6 +4371,6 @@ export const KEM_MLKEM1024_P384 = function ()
|
|
|
4400
4371
|
return GetKeyPairNist(P384, sk, true, name)
|
|
4401
4372
|
},
|
|
4402
4373
|
},
|
|
4403
|
-
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x31, 0x30, 0x32, 0x34, 0x2d, 0x50, 0x33, 0x38, 0x34),
|
|
4374
|
+
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x31, 0x30, 0x32, 0x34, 0x2d, 0x50, 0x33, 0x38, 0x34),
|
|
4404
4375
|
})
|
|
4405
4376
|
}
|