hpke 0.5.0 → 1.0.1
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/LICENSE.md +21 -0
- package/README.md +123 -58
- package/index.d.ts +1824 -0
- package/index.d.ts.map +1 -0
- package/index.js +4170 -0
- package/index.ts +4170 -0
- package/package.json +68 -38
- package/LICENSE +0 -375
- package/pkg/node/.gitignore +0 -1
- package/pkg/node/LICENSE +0 -375
- package/pkg/node/README.md +0 -83
- package/pkg/node/hpke_dispatch.d.ts +0 -241
- package/pkg/node/hpke_dispatch.js +0 -812
- package/pkg/node/hpke_dispatch_bg.wasm +0 -0
- package/pkg/node/hpke_dispatch_bg.wasm.d.ts +0 -32
- package/pkg/node/index.spec.ts +0 -82
- package/pkg/node/package.json +0 -21
- package/pkg/web/.gitignore +0 -1
- package/pkg/web/LICENSE +0 -375
- package/pkg/web/README.md +0 -83
- package/pkg/web/hpke_dispatch.d.ts +0 -297
- package/pkg/web/hpke_dispatch.js +0 -852
- package/pkg/web/hpke_dispatch_bg.wasm +0 -0
- package/pkg/web/hpke_dispatch_bg.wasm.d.ts +0 -32
- package/pkg/web/index.mjs +0 -2
- package/pkg/web/index.mjs.map +0 -7
- package/pkg/web/index.ts +0 -5
- package/pkg/web/package.json +0 -22
package/index.js
ADDED
|
@@ -0,0 +1,4170 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
function ComputeNonce(base_nonce , seq , Nn ) {
|
|
46
|
+
const seq_bytes = I2OSP(seq, Nn)
|
|
47
|
+
return xor(base_nonce, seq_bytes)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function IncrementSeq(seq ) {
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
if (seq >= Number.MAX_SAFE_INTEGER) {
|
|
55
|
+
throw new MessageLimitReachedError('Sequence number overflow')
|
|
56
|
+
}
|
|
57
|
+
return ++seq
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function ContextExport(
|
|
61
|
+
suite ,
|
|
62
|
+
exporterSecret ,
|
|
63
|
+
exporterContext ,
|
|
64
|
+
L ,
|
|
65
|
+
) {
|
|
66
|
+
checkUint8Array(exporterContext, 'exporterContext')
|
|
67
|
+
const stages = KDFStages(suite.KDF)
|
|
68
|
+
if (!Number.isInteger(L) || L <= 0 || L > 0xffff) {
|
|
69
|
+
throw new TypeError('"L" must be a positive integer not exceeding 65535')
|
|
70
|
+
}
|
|
71
|
+
const Export = stages === 1 ? ExportOneStage : ExportTwoStage
|
|
72
|
+
return await Export(suite.KDF, suite.id, exporterSecret, exporterContext, L)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
class Mutex {
|
|
76
|
+
#locked = Promise.resolve()
|
|
77
|
+
|
|
78
|
+
async lock() {
|
|
79
|
+
let releaseLock
|
|
80
|
+
const nextLock = new Promise ((resolve) => {
|
|
81
|
+
releaseLock = resolve
|
|
82
|
+
})
|
|
83
|
+
const previousLock = this.#locked
|
|
84
|
+
this.#locked = nextLock
|
|
85
|
+
await previousLock
|
|
86
|
+
return releaseLock
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class SenderContext {
|
|
110
|
+
#suite
|
|
111
|
+
#key
|
|
112
|
+
#base_nonce
|
|
113
|
+
#exporter_secret
|
|
114
|
+
#mode
|
|
115
|
+
#seq = 0
|
|
116
|
+
#mutex
|
|
117
|
+
|
|
118
|
+
constructor(
|
|
119
|
+
suite ,
|
|
120
|
+
mode ,
|
|
121
|
+
key ,
|
|
122
|
+
base_nonce ,
|
|
123
|
+
exporter_secret ,
|
|
124
|
+
) {
|
|
125
|
+
this.#suite = suite
|
|
126
|
+
this.#mode = mode
|
|
127
|
+
this.#key = key
|
|
128
|
+
this.#base_nonce = base_nonce
|
|
129
|
+
this.#exporter_secret = exporter_secret
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
get mode() {
|
|
138
|
+
return this.#mode
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
get seq() {
|
|
147
|
+
return this.#seq
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
async Seal(plaintext , aad ) {
|
|
176
|
+
checkUint8Array(plaintext, 'plaintext')
|
|
177
|
+
aad ??= new Uint8Array()
|
|
178
|
+
checkUint8Array(aad, 'aad')
|
|
179
|
+
if (this.#suite.AEAD.id === EXPORT_ONLY) {
|
|
180
|
+
throw new TypeError('Export-only AEAD cannot be used with Seal')
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
this.#mutex ??= new Mutex()
|
|
184
|
+
const release = await this.#mutex.lock()
|
|
185
|
+
let ct
|
|
186
|
+
try {
|
|
187
|
+
ct = await this.#suite.AEAD.Seal(
|
|
188
|
+
this.#key,
|
|
189
|
+
ComputeNonce(this.#base_nonce, this.#seq, this.#suite.AEAD.Nn),
|
|
190
|
+
aad,
|
|
191
|
+
plaintext,
|
|
192
|
+
)
|
|
193
|
+
this.#seq = IncrementSeq(this.#seq)
|
|
194
|
+
return ct
|
|
195
|
+
} finally {
|
|
196
|
+
release()
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
async Export(exporterContext , length ) {
|
|
223
|
+
return await ContextExport(this.#suite, this.#exporter_secret, exporterContext, length)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
get Nt() {
|
|
231
|
+
return this.#suite.AEAD.Nt
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
class RecipientContext {
|
|
256
|
+
#suite
|
|
257
|
+
#key
|
|
258
|
+
#base_nonce
|
|
259
|
+
#exporter_secret
|
|
260
|
+
#mode
|
|
261
|
+
#seq = 0
|
|
262
|
+
#mutex
|
|
263
|
+
|
|
264
|
+
constructor(
|
|
265
|
+
suite ,
|
|
266
|
+
mode ,
|
|
267
|
+
key ,
|
|
268
|
+
base_nonce ,
|
|
269
|
+
exporter_secret ,
|
|
270
|
+
) {
|
|
271
|
+
this.#suite = suite
|
|
272
|
+
this.#mode = mode
|
|
273
|
+
this.#key = key
|
|
274
|
+
this.#base_nonce = base_nonce
|
|
275
|
+
this.#exporter_secret = exporter_secret
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
get mode() {
|
|
284
|
+
return this.#mode
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
get seq() {
|
|
293
|
+
return this.#seq
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
async Open(ciphertext , aad ) {
|
|
323
|
+
checkUint8Array(ciphertext, 'ciphertext')
|
|
324
|
+
aad ??= new Uint8Array()
|
|
325
|
+
checkUint8Array(aad, 'aad')
|
|
326
|
+
|
|
327
|
+
if (this.#suite.AEAD.id === EXPORT_ONLY) {
|
|
328
|
+
throw new TypeError('Export-only AEAD cannot be used with Open')
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
this.#mutex ??= new Mutex()
|
|
332
|
+
const release = await this.#mutex.lock()
|
|
333
|
+
try {
|
|
334
|
+
let pt
|
|
335
|
+
try {
|
|
336
|
+
pt = await this.#suite.AEAD.Open(
|
|
337
|
+
this.#key,
|
|
338
|
+
ComputeNonce(this.#base_nonce, this.#seq, this.#suite.AEAD.Nn),
|
|
339
|
+
aad,
|
|
340
|
+
ciphertext,
|
|
341
|
+
)
|
|
342
|
+
} catch (cause) {
|
|
343
|
+
if (cause instanceof MessageLimitReachedError || cause instanceof NotSupportedError) {
|
|
344
|
+
throw cause
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
throw new OpenError('AEAD decryption failed', { cause })
|
|
348
|
+
}
|
|
349
|
+
this.#seq = IncrementSeq(this.#seq)
|
|
350
|
+
return pt
|
|
351
|
+
} finally {
|
|
352
|
+
release()
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
async Export(exporterContext , length ) {
|
|
379
|
+
return await ContextExport(this.#suite, this.#exporter_secret, exporterContext, length)
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
const validate = (factory , type ) => {
|
|
389
|
+
try {
|
|
390
|
+
const result = factory()
|
|
391
|
+
if (result.type !== type) {
|
|
392
|
+
throw new Error(`Invalid "${type}" return discriminator`)
|
|
393
|
+
}
|
|
394
|
+
return result
|
|
395
|
+
} catch (cause) {
|
|
396
|
+
throw new TypeError(`Invalid "${type}"`, { cause })
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
export class CipherSuite {
|
|
424
|
+
#suite
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
constructor(KEM , KDF , AEAD ) {
|
|
488
|
+
const kem = validate(KEM, 'KEM')
|
|
489
|
+
const kdf = validate(KDF, 'KDF')
|
|
490
|
+
const aead = validate(AEAD, 'AEAD')
|
|
491
|
+
|
|
492
|
+
this.#suite = {
|
|
493
|
+
KEM: kem,
|
|
494
|
+
KDF: kdf,
|
|
495
|
+
AEAD: aead,
|
|
496
|
+
id: concat(encode('HPKE'), I2OSP(kem.id, 2), I2OSP(kdf.id, 2), I2OSP(aead.id, 2)),
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
get KEM()
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
{
|
|
519
|
+
return {
|
|
520
|
+
id: this.#suite.KEM.id,
|
|
521
|
+
name: this.#suite.KEM.name,
|
|
522
|
+
Nsecret: this.#suite.KEM.Nsecret,
|
|
523
|
+
Nenc: this.#suite.KEM.Nenc,
|
|
524
|
+
Npk: this.#suite.KEM.Npk,
|
|
525
|
+
Nsk: this.#suite.KEM.Nsk,
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
get KDF()
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
{
|
|
552
|
+
return {
|
|
553
|
+
id: this.#suite.KDF.id,
|
|
554
|
+
name: this.#suite.KDF.name,
|
|
555
|
+
stages: this.#suite.KDF.stages,
|
|
556
|
+
Nh: this.#suite.KDF.Nh,
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
get AEAD()
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
{
|
|
578
|
+
return {
|
|
579
|
+
id: this.#suite.AEAD.id,
|
|
580
|
+
name: this.#suite.AEAD.name,
|
|
581
|
+
Nk: this.#suite.AEAD.Nk,
|
|
582
|
+
Nn: this.#suite.AEAD.Nn,
|
|
583
|
+
Nt: this.#suite.AEAD.Nt,
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
async GenerateKeyPair(extractable ) {
|
|
605
|
+
extractable ??= false
|
|
606
|
+
checkExtractable(extractable)
|
|
607
|
+
return await this.#suite.KEM.GenerateKeyPair(extractable)
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
async DeriveKeyPair(ikm , extractable ) {
|
|
638
|
+
extractable ??= false
|
|
639
|
+
checkExtractable(extractable)
|
|
640
|
+
checkUint8Array(ikm, 'ikm')
|
|
641
|
+
if (ikm.byteLength < this.#suite.KEM.Nsk) {
|
|
642
|
+
throw new DeriveKeyPairError('Insufficient "ikm" length')
|
|
643
|
+
}
|
|
644
|
+
try {
|
|
645
|
+
return await this.#suite.KEM.DeriveKeyPair(ikm, extractable)
|
|
646
|
+
} catch (cause) {
|
|
647
|
+
if (cause instanceof NotSupportedError) {
|
|
648
|
+
throw cause
|
|
649
|
+
}
|
|
650
|
+
throw new DeriveKeyPairError('Key derivation failed', { cause })
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
async SerializePrivateKey(privateKey ) {
|
|
671
|
+
isKey(privateKey, 'private', true)
|
|
672
|
+
|
|
673
|
+
return await this.#suite.KEM.SerializePrivateKey(privateKey)
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
async SerializePublicKey(publicKey ) {
|
|
693
|
+
isKey(publicKey, 'public', true)
|
|
694
|
+
|
|
695
|
+
return await this.#suite.KEM.SerializePublicKey(publicKey)
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
async DeserializePrivateKey(privateKey , extractable ) {
|
|
718
|
+
extractable ??= false
|
|
719
|
+
checkExtractable(extractable)
|
|
720
|
+
checkUint8Array(privateKey, 'privateKey')
|
|
721
|
+
|
|
722
|
+
try {
|
|
723
|
+
if (privateKey.byteLength !== this.#suite.KEM.Nsk) {
|
|
724
|
+
throw new Error('Invalid "privateKey" length')
|
|
725
|
+
}
|
|
726
|
+
return await this.#suite.KEM.DeserializePrivateKey(privateKey, extractable)
|
|
727
|
+
} catch (cause) {
|
|
728
|
+
if (cause instanceof NotSupportedError) {
|
|
729
|
+
throw cause
|
|
730
|
+
}
|
|
731
|
+
throw new DeserializeError('Private key deserialization failed', { cause })
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
async DeserializePublicKey(publicKey ) {
|
|
753
|
+
checkUint8Array(publicKey, 'publicKey')
|
|
754
|
+
|
|
755
|
+
try {
|
|
756
|
+
if (publicKey.byteLength !== this.#suite.KEM.Npk) {
|
|
757
|
+
throw new Error('Invalid "publicKey" length')
|
|
758
|
+
}
|
|
759
|
+
return await this.#suite.KEM.DeserializePublicKey(publicKey)
|
|
760
|
+
} catch (cause) {
|
|
761
|
+
if (cause instanceof NotSupportedError) {
|
|
762
|
+
throw cause
|
|
763
|
+
}
|
|
764
|
+
throw new DeserializeError('Public key deserialization failed', { cause })
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
async Seal(
|
|
802
|
+
publicKey ,
|
|
803
|
+
plaintext ,
|
|
804
|
+
options ,
|
|
805
|
+
) {
|
|
806
|
+
if (this.#suite.AEAD.id === EXPORT_ONLY) {
|
|
807
|
+
throw new TypeError('Export-only AEAD cannot be used with Seal')
|
|
808
|
+
}
|
|
809
|
+
const { encapsulatedSecret, ctx } = await this.SetupSender(publicKey, options)
|
|
810
|
+
const ciphertext = await ctx.Seal(plaintext, options?.aad)
|
|
811
|
+
return { encapsulatedSecret, ciphertext }
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
async Open(
|
|
850
|
+
privateKey ,
|
|
851
|
+
encapsulatedSecret ,
|
|
852
|
+
ciphertext ,
|
|
853
|
+
options ,
|
|
854
|
+
) {
|
|
855
|
+
if (this.#suite.AEAD.id === EXPORT_ONLY) {
|
|
856
|
+
throw new TypeError('Export-only AEAD cannot be used with Open')
|
|
857
|
+
}
|
|
858
|
+
const ctx = await this.SetupRecipient(privateKey, encapsulatedSecret, options)
|
|
859
|
+
return await ctx.Open(ciphertext, options?.aad)
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
async SendExport(
|
|
897
|
+
publicKey ,
|
|
898
|
+
exporterContext ,
|
|
899
|
+
length ,
|
|
900
|
+
options ,
|
|
901
|
+
) {
|
|
902
|
+
const { encapsulatedSecret, ctx } = await this.SetupSender(publicKey, options)
|
|
903
|
+
const exportedSecret = await ctx.Export(exporterContext, length)
|
|
904
|
+
return { encapsulatedSecret, exportedSecret }
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
async ReceiveExport(
|
|
944
|
+
privateKey ,
|
|
945
|
+
encapsulatedSecret ,
|
|
946
|
+
exporterContext ,
|
|
947
|
+
length ,
|
|
948
|
+
options ,
|
|
949
|
+
) {
|
|
950
|
+
const ctx = await this.SetupRecipient(privateKey, encapsulatedSecret, options)
|
|
951
|
+
return await ctx.Export(exporterContext, length)
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
async SetupSender(
|
|
997
|
+
publicKey ,
|
|
998
|
+
options ,
|
|
999
|
+
) {
|
|
1000
|
+
isKey(publicKey, 'public')
|
|
1001
|
+
|
|
1002
|
+
let shared_secret
|
|
1003
|
+
let enc
|
|
1004
|
+
try {
|
|
1005
|
+
const result = await this.#suite.KEM.Encap(publicKey)
|
|
1006
|
+
shared_secret = result.shared_secret
|
|
1007
|
+
enc = result.enc
|
|
1008
|
+
} catch (cause) {
|
|
1009
|
+
if (cause instanceof ValidationError || cause instanceof NotSupportedError) {
|
|
1010
|
+
throw cause
|
|
1011
|
+
}
|
|
1012
|
+
throw new EncapError('Encapsulation failed', { cause })
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
const mode = options?.psk?.byteLength ? MODE_PSK : MODE_BASE
|
|
1016
|
+
const { key, base_nonce, exporter_secret } = await KeySchedule(
|
|
1017
|
+
this.#suite,
|
|
1018
|
+
mode,
|
|
1019
|
+
shared_secret,
|
|
1020
|
+
options?.info,
|
|
1021
|
+
options?.psk,
|
|
1022
|
+
options?.pskId,
|
|
1023
|
+
)
|
|
1024
|
+
|
|
1025
|
+
const ctx = new SenderContext(this.#suite, mode, key, base_nonce, exporter_secret)
|
|
1026
|
+
return { encapsulatedSecret: enc, ctx }
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
|
|
1045
|
+
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
async SetupRecipient(
|
|
1077
|
+
privateKey ,
|
|
1078
|
+
encapsulatedSecret ,
|
|
1079
|
+
options ,
|
|
1080
|
+
) {
|
|
1081
|
+
const { skR, pkR } = this.#extractRecipientKeys(privateKey)
|
|
1082
|
+
checkUint8Array(encapsulatedSecret, 'encapsulatedSecret')
|
|
1083
|
+
if (encapsulatedSecret.byteLength !== this.#suite.KEM.Nenc) {
|
|
1084
|
+
throw new DecapError('Invalid encapsulated secret length')
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
let shared_secret
|
|
1088
|
+
try {
|
|
1089
|
+
shared_secret = await this.#suite.KEM.Decap(encapsulatedSecret, skR, pkR)
|
|
1090
|
+
} catch (cause) {
|
|
1091
|
+
if (cause instanceof ValidationError || cause instanceof NotSupportedError) {
|
|
1092
|
+
throw cause
|
|
1093
|
+
}
|
|
1094
|
+
throw new DecapError('Decapsulation failed', { cause })
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
const mode = options?.psk?.byteLength ? MODE_PSK : MODE_BASE
|
|
1098
|
+
const { key, base_nonce, exporter_secret } = await KeySchedule(
|
|
1099
|
+
this.#suite,
|
|
1100
|
+
mode,
|
|
1101
|
+
shared_secret,
|
|
1102
|
+
options?.info,
|
|
1103
|
+
options?.psk,
|
|
1104
|
+
options?.pskId,
|
|
1105
|
+
)
|
|
1106
|
+
|
|
1107
|
+
return new RecipientContext(this.#suite, mode, key, base_nonce, exporter_secret)
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
#extractRecipientKeys(skR ) {
|
|
1111
|
+
if (isKeyPair(skR)) {
|
|
1112
|
+
return { skR: skR.privateKey, pkR: skR.publicKey }
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
isKey(skR, 'private')
|
|
1116
|
+
return { skR, pkR: undefined }
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
export class ValidationError extends Error {
|
|
1131
|
+
constructor(message , options ) {
|
|
1132
|
+
super(message, options)
|
|
1133
|
+
this.name = 'ValidationError'
|
|
1134
|
+
|
|
1135
|
+
Error.captureStackTrace?.(this, ValidationError)
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
export class DeserializeError extends Error {
|
|
1146
|
+
constructor(message , options ) {
|
|
1147
|
+
super(message, options)
|
|
1148
|
+
this.name = 'DeserializeError'
|
|
1149
|
+
|
|
1150
|
+
Error.captureStackTrace?.(this, DeserializeError)
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
export class EncapError extends Error {
|
|
1161
|
+
constructor(message , options ) {
|
|
1162
|
+
super(message, options)
|
|
1163
|
+
this.name = 'EncapError'
|
|
1164
|
+
|
|
1165
|
+
Error.captureStackTrace?.(this, EncapError)
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
export class DecapError extends Error {
|
|
1176
|
+
constructor(message , options ) {
|
|
1177
|
+
super(message, options)
|
|
1178
|
+
this.name = 'DecapError'
|
|
1179
|
+
|
|
1180
|
+
Error.captureStackTrace?.(this, DecapError)
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
export class OpenError extends Error {
|
|
1191
|
+
constructor(message , options ) {
|
|
1192
|
+
super(message, options)
|
|
1193
|
+
this.name = 'OpenError'
|
|
1194
|
+
|
|
1195
|
+
Error.captureStackTrace?.(this, OpenError)
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
export class MessageLimitReachedError extends Error {
|
|
1206
|
+
constructor(message , options ) {
|
|
1207
|
+
super(message, options)
|
|
1208
|
+
this.name = 'MessageLimitReachedError'
|
|
1209
|
+
|
|
1210
|
+
Error.captureStackTrace?.(this, MessageLimitReachedError)
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
export class DeriveKeyPairError extends Error {
|
|
1221
|
+
constructor(message , options ) {
|
|
1222
|
+
super(message, options)
|
|
1223
|
+
this.name = 'DeriveKeyPairError'
|
|
1224
|
+
|
|
1225
|
+
Error.captureStackTrace?.(this, DeriveKeyPairError)
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
|
|
1235
|
+
export class NotSupportedError extends Error {
|
|
1236
|
+
constructor(message , options ) {
|
|
1237
|
+
super(message, options)
|
|
1238
|
+
this.name = 'NotSupportedError'
|
|
1239
|
+
|
|
1240
|
+
Error.captureStackTrace?.(this, NotSupportedError)
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
export const MODE_BASE = 0x00
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
export const MODE_PSK = 0x01
|
|
1271
|
+
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
|
|
1275
|
+
|
|
1276
|
+
|
|
1277
|
+
|
|
1278
|
+
|
|
1279
|
+
|
|
1280
|
+
|
|
1281
|
+
|
|
1282
|
+
|
|
1283
|
+
|
|
1284
|
+
|
|
1285
|
+
|
|
1286
|
+
|
|
1287
|
+
|
|
1288
|
+
|
|
1289
|
+
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
|
|
1308
|
+
|
|
1309
|
+
|
|
1310
|
+
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
|
|
1315
|
+
|
|
1316
|
+
|
|
1317
|
+
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
|
|
1321
|
+
|
|
1322
|
+
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
|
|
1326
|
+
|
|
1327
|
+
|
|
1328
|
+
|
|
1329
|
+
|
|
1330
|
+
|
|
1331
|
+
|
|
1332
|
+
|
|
1333
|
+
|
|
1334
|
+
|
|
1335
|
+
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
|
|
1339
|
+
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
|
|
1386
|
+
|
|
1387
|
+
|
|
1388
|
+
|
|
1389
|
+
|
|
1390
|
+
|
|
1391
|
+
|
|
1392
|
+
|
|
1393
|
+
|
|
1394
|
+
|
|
1395
|
+
|
|
1396
|
+
|
|
1397
|
+
|
|
1398
|
+
|
|
1399
|
+
|
|
1400
|
+
|
|
1401
|
+
|
|
1402
|
+
|
|
1403
|
+
|
|
1404
|
+
export function concat(...buffers ) {
|
|
1405
|
+
const size = buffers.reduce((acc, { length }) => acc + length, 0)
|
|
1406
|
+
const buf = new Uint8Array(size)
|
|
1407
|
+
let i = 0
|
|
1408
|
+
for (const buffer of buffers) {
|
|
1409
|
+
buf.set(buffer, i)
|
|
1410
|
+
i += buffer.length
|
|
1411
|
+
}
|
|
1412
|
+
return buf
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
function slice(buffer , start , end ) {
|
|
1416
|
+
return Uint8Array.prototype.slice.call(buffer, start, end)
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
|
|
1420
|
+
|
|
1421
|
+
|
|
1422
|
+
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
|
|
1427
|
+
|
|
1428
|
+
|
|
1429
|
+
|
|
1430
|
+
export function encode(string ) {
|
|
1431
|
+
const bytes = new Uint8Array(string.length)
|
|
1432
|
+
for (let i = 0; i < string.length; i++) {
|
|
1433
|
+
const code = string.charCodeAt(i)
|
|
1434
|
+
if (code > 0x7f) {
|
|
1435
|
+
throw new TypeError('Input string must contain only ASCII characters')
|
|
1436
|
+
}
|
|
1437
|
+
bytes[i] = code
|
|
1438
|
+
}
|
|
1439
|
+
return bytes
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
function xor(a , b ) {
|
|
1443
|
+
if (a.byteLength !== b.byteLength) {
|
|
1444
|
+
throw new Error('XOR operands must have equal length')
|
|
1445
|
+
}
|
|
1446
|
+
const buf = new Uint8Array(a.byteLength)
|
|
1447
|
+
for (let i = 0; i < a.byteLength; i++) {
|
|
1448
|
+
buf[i] = a[i] ^ b[i]
|
|
1449
|
+
}
|
|
1450
|
+
return buf
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
function lengthPrefixed(x ) {
|
|
1454
|
+
return concat(I2OSP(x.byteLength, 2), x)
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
|
|
1462
|
+
|
|
1463
|
+
|
|
1464
|
+
|
|
1465
|
+
|
|
1466
|
+
|
|
1467
|
+
|
|
1468
|
+
|
|
1469
|
+
|
|
1470
|
+
|
|
1471
|
+
|
|
1472
|
+
|
|
1473
|
+
|
|
1474
|
+
|
|
1475
|
+
|
|
1476
|
+
|
|
1477
|
+
|
|
1478
|
+
|
|
1479
|
+
export async function LabeledDerive(
|
|
1480
|
+
KDF ,
|
|
1481
|
+
suite_id ,
|
|
1482
|
+
ikm ,
|
|
1483
|
+
label ,
|
|
1484
|
+
context ,
|
|
1485
|
+
L ,
|
|
1486
|
+
) {
|
|
1487
|
+
const labeled_ikm = concat(
|
|
1488
|
+
ikm,
|
|
1489
|
+
encode('HPKE-v1'),
|
|
1490
|
+
suite_id,
|
|
1491
|
+
lengthPrefixed(label),
|
|
1492
|
+
I2OSP(L, 2),
|
|
1493
|
+
context,
|
|
1494
|
+
)
|
|
1495
|
+
return await KDF.Derive(labeled_ikm, L)
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
async function ExportOneStage(
|
|
1499
|
+
KDF ,
|
|
1500
|
+
suite_id ,
|
|
1501
|
+
exporter_secret ,
|
|
1502
|
+
exporter_context ,
|
|
1503
|
+
L ,
|
|
1504
|
+
) {
|
|
1505
|
+
checkLength(exporter_context, 'Exporter context', MAX_LENGTH_ONE_STAGE)
|
|
1506
|
+
return await LabeledDerive(KDF, suite_id, exporter_secret, encode('sec'), exporter_context, L)
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
async function CombineSecretsOneStage(
|
|
1510
|
+
suite ,
|
|
1511
|
+
mode ,
|
|
1512
|
+
shared_secret ,
|
|
1513
|
+
info ,
|
|
1514
|
+
psk ,
|
|
1515
|
+
psk_id ,
|
|
1516
|
+
) {
|
|
1517
|
+
checkLength(psk, 'PSK', MAX_LENGTH_ONE_STAGE)
|
|
1518
|
+
checkLength(psk_id, 'PSK ID', MAX_LENGTH_ONE_STAGE)
|
|
1519
|
+
checkLength(info, 'Info', MAX_LENGTH_ONE_STAGE)
|
|
1520
|
+
|
|
1521
|
+
const secrets = concat(lengthPrefixed(psk), lengthPrefixed(shared_secret))
|
|
1522
|
+
const context = concat(I2OSP(mode, 1), lengthPrefixed(psk_id), lengthPrefixed(info))
|
|
1523
|
+
|
|
1524
|
+
const secret = await LabeledDerive(
|
|
1525
|
+
suite.KDF,
|
|
1526
|
+
suite.id,
|
|
1527
|
+
secrets,
|
|
1528
|
+
encode('secret'),
|
|
1529
|
+
context,
|
|
1530
|
+
suite.AEAD.Nk + suite.AEAD.Nn + suite.KDF.Nh,
|
|
1531
|
+
)
|
|
1532
|
+
|
|
1533
|
+
const key = slice(secret, 0, suite.AEAD.Nk)
|
|
1534
|
+
const base_nonce = slice(secret, suite.AEAD.Nk, suite.AEAD.Nk + suite.AEAD.Nn)
|
|
1535
|
+
const exporter_secret = slice(secret, suite.AEAD.Nk + suite.AEAD.Nn)
|
|
1536
|
+
|
|
1537
|
+
return { key, base_nonce, exporter_secret }
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
|
|
1541
|
+
|
|
1542
|
+
const MAX_LENGTH_TWO_STAGE = 0xffff
|
|
1543
|
+
|
|
1544
|
+
const MAX_LENGTH_ONE_STAGE = 0xffff
|
|
1545
|
+
|
|
1546
|
+
function checkLength(data , name , maxLength ) {
|
|
1547
|
+
if (data.byteLength > maxLength) {
|
|
1548
|
+
throw new TypeError(`${name} length must not exceed ${maxLength} bytes`)
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
function checkUint8Array(input , name ) {
|
|
1552
|
+
if (!(input instanceof Uint8Array)) {
|
|
1553
|
+
throw new TypeError(`"${name}" must be Uint8Array`)
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
function checkExtractable(extractable ) {
|
|
1557
|
+
if (typeof extractable !== 'boolean') {
|
|
1558
|
+
throw new TypeError('"extractable" must be boolean')
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
async function CombineSecretsTwoStage(
|
|
1563
|
+
suite ,
|
|
1564
|
+
mode ,
|
|
1565
|
+
shared_secret ,
|
|
1566
|
+
info ,
|
|
1567
|
+
psk ,
|
|
1568
|
+
psk_id ,
|
|
1569
|
+
) {
|
|
1570
|
+
checkLength(psk, 'PSK', MAX_LENGTH_TWO_STAGE)
|
|
1571
|
+
checkLength(psk_id, 'PSK ID', MAX_LENGTH_TWO_STAGE)
|
|
1572
|
+
checkLength(info, 'Info', MAX_LENGTH_TWO_STAGE)
|
|
1573
|
+
|
|
1574
|
+
const [psk_id_hash, info_hash] = await Promise.all([
|
|
1575
|
+
LabeledExtract(suite.KDF, suite.id, new Uint8Array(), encode('psk_id_hash'), psk_id),
|
|
1576
|
+
LabeledExtract(suite.KDF, suite.id, new Uint8Array(), encode('info_hash'), info),
|
|
1577
|
+
])
|
|
1578
|
+
|
|
1579
|
+
const key_schedule_context = concat(I2OSP(mode, 1), psk_id_hash, info_hash)
|
|
1580
|
+
const secret = await LabeledExtract(suite.KDF, suite.id, shared_secret, encode('secret'), psk)
|
|
1581
|
+
|
|
1582
|
+
|
|
1583
|
+
if (suite.AEAD.id === EXPORT_ONLY) {
|
|
1584
|
+
const exporter_secret = await LabeledExpand(
|
|
1585
|
+
suite.KDF,
|
|
1586
|
+
suite.id,
|
|
1587
|
+
secret,
|
|
1588
|
+
encode('exp'),
|
|
1589
|
+
key_schedule_context,
|
|
1590
|
+
suite.KDF.Nh,
|
|
1591
|
+
)
|
|
1592
|
+
return { key: new Uint8Array(), base_nonce: new Uint8Array(), exporter_secret }
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
const [key, base_nonce, exporter_secret] = await Promise.all([
|
|
1596
|
+
LabeledExpand(suite.KDF, suite.id, secret, encode('key'), key_schedule_context, suite.AEAD.Nk),
|
|
1597
|
+
LabeledExpand(
|
|
1598
|
+
suite.KDF,
|
|
1599
|
+
suite.id,
|
|
1600
|
+
secret,
|
|
1601
|
+
encode('base_nonce'),
|
|
1602
|
+
key_schedule_context,
|
|
1603
|
+
suite.AEAD.Nn,
|
|
1604
|
+
),
|
|
1605
|
+
LabeledExpand(suite.KDF, suite.id, secret, encode('exp'), key_schedule_context, suite.KDF.Nh),
|
|
1606
|
+
])
|
|
1607
|
+
|
|
1608
|
+
return { key, base_nonce, exporter_secret }
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
async function ExportTwoStage(
|
|
1612
|
+
KDF ,
|
|
1613
|
+
suite_id ,
|
|
1614
|
+
exporter_secret ,
|
|
1615
|
+
exporter_context ,
|
|
1616
|
+
L ,
|
|
1617
|
+
) {
|
|
1618
|
+
checkLength(exporter_context, 'Exporter context', MAX_LENGTH_TWO_STAGE)
|
|
1619
|
+
return await LabeledExpand(KDF, suite_id, exporter_secret, encode('sec'), exporter_context, L)
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
|
|
1623
|
+
|
|
1624
|
+
|
|
1625
|
+
|
|
1626
|
+
|
|
1627
|
+
|
|
1628
|
+
|
|
1629
|
+
|
|
1630
|
+
|
|
1631
|
+
|
|
1632
|
+
|
|
1633
|
+
|
|
1634
|
+
|
|
1635
|
+
|
|
1636
|
+
|
|
1637
|
+
|
|
1638
|
+
|
|
1639
|
+
|
|
1640
|
+
|
|
1641
|
+
|
|
1642
|
+
|
|
1643
|
+
|
|
1644
|
+
|
|
1645
|
+
|
|
1646
|
+
|
|
1647
|
+
|
|
1648
|
+
|
|
1649
|
+
|
|
1650
|
+
|
|
1651
|
+
|
|
1652
|
+
|
|
1653
|
+
|
|
1654
|
+
|
|
1655
|
+
|
|
1656
|
+
|
|
1657
|
+
|
|
1658
|
+
|
|
1659
|
+
|
|
1660
|
+
|
|
1661
|
+
|
|
1662
|
+
|
|
1663
|
+
|
|
1664
|
+
|
|
1665
|
+
|
|
1666
|
+
|
|
1667
|
+
|
|
1668
|
+
|
|
1669
|
+
|
|
1670
|
+
|
|
1671
|
+
|
|
1672
|
+
|
|
1673
|
+
|
|
1674
|
+
|
|
1675
|
+
|
|
1676
|
+
|
|
1677
|
+
|
|
1678
|
+
|
|
1679
|
+
|
|
1680
|
+
|
|
1681
|
+
|
|
1682
|
+
|
|
1683
|
+
|
|
1684
|
+
|
|
1685
|
+
|
|
1686
|
+
|
|
1687
|
+
|
|
1688
|
+
|
|
1689
|
+
|
|
1690
|
+
|
|
1691
|
+
|
|
1692
|
+
|
|
1693
|
+
|
|
1694
|
+
|
|
1695
|
+
|
|
1696
|
+
|
|
1697
|
+
|
|
1698
|
+
|
|
1699
|
+
|
|
1700
|
+
|
|
1701
|
+
|
|
1702
|
+
|
|
1703
|
+
|
|
1704
|
+
|
|
1705
|
+
|
|
1706
|
+
|
|
1707
|
+
|
|
1708
|
+
|
|
1709
|
+
|
|
1710
|
+
|
|
1711
|
+
|
|
1712
|
+
|
|
1713
|
+
|
|
1714
|
+
|
|
1715
|
+
|
|
1716
|
+
|
|
1717
|
+
|
|
1718
|
+
|
|
1719
|
+
|
|
1720
|
+
|
|
1721
|
+
|
|
1722
|
+
|
|
1723
|
+
|
|
1724
|
+
|
|
1725
|
+
|
|
1726
|
+
|
|
1727
|
+
|
|
1728
|
+
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
|
|
1732
|
+
|
|
1733
|
+
|
|
1734
|
+
|
|
1735
|
+
|
|
1736
|
+
|
|
1737
|
+
|
|
1738
|
+
|
|
1739
|
+
|
|
1740
|
+
|
|
1741
|
+
|
|
1742
|
+
|
|
1743
|
+
|
|
1744
|
+
|
|
1745
|
+
|
|
1746
|
+
|
|
1747
|
+
|
|
1748
|
+
|
|
1749
|
+
|
|
1750
|
+
|
|
1751
|
+
|
|
1752
|
+
|
|
1753
|
+
|
|
1754
|
+
|
|
1755
|
+
|
|
1756
|
+
|
|
1757
|
+
|
|
1758
|
+
|
|
1759
|
+
|
|
1760
|
+
|
|
1761
|
+
export async function LabeledExtract(
|
|
1762
|
+
KDF ,
|
|
1763
|
+
suite_id ,
|
|
1764
|
+
salt ,
|
|
1765
|
+
label ,
|
|
1766
|
+
ikm ,
|
|
1767
|
+
) {
|
|
1768
|
+
const labeled_ikm = concat(encode('HPKE-v1'), suite_id, label, ikm)
|
|
1769
|
+
return await KDF.Extract(salt, labeled_ikm)
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
|
|
1773
|
+
|
|
1774
|
+
|
|
1775
|
+
|
|
1776
|
+
|
|
1777
|
+
|
|
1778
|
+
|
|
1779
|
+
|
|
1780
|
+
|
|
1781
|
+
|
|
1782
|
+
|
|
1783
|
+
|
|
1784
|
+
|
|
1785
|
+
|
|
1786
|
+
|
|
1787
|
+
|
|
1788
|
+
|
|
1789
|
+
|
|
1790
|
+
export async function LabeledExpand(
|
|
1791
|
+
KDF ,
|
|
1792
|
+
suite_id ,
|
|
1793
|
+
prk ,
|
|
1794
|
+
label ,
|
|
1795
|
+
info ,
|
|
1796
|
+
L ,
|
|
1797
|
+
) {
|
|
1798
|
+
const labeled_info = concat(I2OSP(L, 2), encode('HPKE-v1'), suite_id, label, info)
|
|
1799
|
+
return await KDF.Expand(prk, labeled_info, L)
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
|
|
1803
|
+
|
|
1804
|
+
|
|
1805
|
+
|
|
1806
|
+
|
|
1807
|
+
|
|
1808
|
+
|
|
1809
|
+
|
|
1810
|
+
|
|
1811
|
+
|
|
1812
|
+
|
|
1813
|
+
|
|
1814
|
+
|
|
1815
|
+
|
|
1816
|
+
|
|
1817
|
+
|
|
1818
|
+
|
|
1819
|
+
|
|
1820
|
+
|
|
1821
|
+
|
|
1822
|
+
|
|
1823
|
+
|
|
1824
|
+
|
|
1825
|
+
|
|
1826
|
+
|
|
1827
|
+
|
|
1828
|
+
|
|
1829
|
+
|
|
1830
|
+
|
|
1831
|
+
|
|
1832
|
+
|
|
1833
|
+
|
|
1834
|
+
|
|
1835
|
+
|
|
1836
|
+
|
|
1837
|
+
|
|
1838
|
+
|
|
1839
|
+
|
|
1840
|
+
|
|
1841
|
+
|
|
1842
|
+
|
|
1843
|
+
|
|
1844
|
+
|
|
1845
|
+
|
|
1846
|
+
|
|
1847
|
+
|
|
1848
|
+
|
|
1849
|
+
|
|
1850
|
+
|
|
1851
|
+
|
|
1852
|
+
|
|
1853
|
+
|
|
1854
|
+
|
|
1855
|
+
|
|
1856
|
+
|
|
1857
|
+
|
|
1858
|
+
|
|
1859
|
+
|
|
1860
|
+
|
|
1861
|
+
|
|
1862
|
+
|
|
1863
|
+
|
|
1864
|
+
|
|
1865
|
+
|
|
1866
|
+
|
|
1867
|
+
|
|
1868
|
+
|
|
1869
|
+
|
|
1870
|
+
|
|
1871
|
+
|
|
1872
|
+
|
|
1873
|
+
|
|
1874
|
+
|
|
1875
|
+
|
|
1876
|
+
|
|
1877
|
+
|
|
1878
|
+
|
|
1879
|
+
|
|
1880
|
+
|
|
1881
|
+
|
|
1882
|
+
|
|
1883
|
+
|
|
1884
|
+
|
|
1885
|
+
|
|
1886
|
+
|
|
1887
|
+
|
|
1888
|
+
|
|
1889
|
+
|
|
1890
|
+
|
|
1891
|
+
|
|
1892
|
+
|
|
1893
|
+
|
|
1894
|
+
|
|
1895
|
+
|
|
1896
|
+
|
|
1897
|
+
|
|
1898
|
+
|
|
1899
|
+
|
|
1900
|
+
|
|
1901
|
+
|
|
1902
|
+
|
|
1903
|
+
|
|
1904
|
+
|
|
1905
|
+
|
|
1906
|
+
|
|
1907
|
+
|
|
1908
|
+
|
|
1909
|
+
|
|
1910
|
+
|
|
1911
|
+
|
|
1912
|
+
|
|
1913
|
+
|
|
1914
|
+
|
|
1915
|
+
|
|
1916
|
+
|
|
1917
|
+
|
|
1918
|
+
|
|
1919
|
+
|
|
1920
|
+
|
|
1921
|
+
|
|
1922
|
+
|
|
1923
|
+
|
|
1924
|
+
|
|
1925
|
+
|
|
1926
|
+
|
|
1927
|
+
|
|
1928
|
+
|
|
1929
|
+
|
|
1930
|
+
|
|
1931
|
+
|
|
1932
|
+
|
|
1933
|
+
|
|
1934
|
+
|
|
1935
|
+
|
|
1936
|
+
|
|
1937
|
+
|
|
1938
|
+
|
|
1939
|
+
|
|
1940
|
+
|
|
1941
|
+
|
|
1942
|
+
|
|
1943
|
+
|
|
1944
|
+
|
|
1945
|
+
|
|
1946
|
+
|
|
1947
|
+
|
|
1948
|
+
|
|
1949
|
+
|
|
1950
|
+
|
|
1951
|
+
|
|
1952
|
+
|
|
1953
|
+
|
|
1954
|
+
|
|
1955
|
+
|
|
1956
|
+
|
|
1957
|
+
|
|
1958
|
+
|
|
1959
|
+
|
|
1960
|
+
|
|
1961
|
+
|
|
1962
|
+
|
|
1963
|
+
|
|
1964
|
+
|
|
1965
|
+
|
|
1966
|
+
|
|
1967
|
+
|
|
1968
|
+
|
|
1969
|
+
|
|
1970
|
+
|
|
1971
|
+
|
|
1972
|
+
|
|
1973
|
+
|
|
1974
|
+
|
|
1975
|
+
|
|
1976
|
+
|
|
1977
|
+
|
|
1978
|
+
|
|
1979
|
+
|
|
1980
|
+
|
|
1981
|
+
|
|
1982
|
+
|
|
1983
|
+
|
|
1984
|
+
|
|
1985
|
+
|
|
1986
|
+
|
|
1987
|
+
|
|
1988
|
+
|
|
1989
|
+
|
|
1990
|
+
|
|
1991
|
+
|
|
1992
|
+
|
|
1993
|
+
|
|
1994
|
+
|
|
1995
|
+
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
|
|
1999
|
+
|
|
2000
|
+
|
|
2001
|
+
|
|
2002
|
+
|
|
2003
|
+
|
|
2004
|
+
|
|
2005
|
+
|
|
2006
|
+
function isKeyPair(skR ) {
|
|
2007
|
+
if (!skR || typeof skR !== 'object') return false
|
|
2008
|
+
if ('publicKey' in skR && 'privateKey' in skR) {
|
|
2009
|
+
const pkR = skR.publicKey
|
|
2010
|
+
skR = skR.privateKey
|
|
2011
|
+
try {
|
|
2012
|
+
isKey(pkR, 'public')
|
|
2013
|
+
isKey(skR, 'private')
|
|
2014
|
+
if (pkR.algorithm.name !== skR.algorithm.name) {
|
|
2015
|
+
throw new TypeError('key pair algorithms do not match')
|
|
2016
|
+
}
|
|
2017
|
+
} catch (cause) {
|
|
2018
|
+
throw new TypeError('Invalid "privateKey"', { cause })
|
|
2019
|
+
}
|
|
2020
|
+
return true
|
|
2021
|
+
}
|
|
2022
|
+
return false
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
function isKey(key , type , extractable ) {
|
|
2026
|
+
const k = key
|
|
2027
|
+
if (
|
|
2028
|
+
typeof k.algorithm !== 'object' ||
|
|
2029
|
+
typeof k.algorithm.name !== 'string' ||
|
|
2030
|
+
typeof k.extractable !== 'boolean' ||
|
|
2031
|
+
typeof k.type !== 'string' ||
|
|
2032
|
+
k.type !== type
|
|
2033
|
+
) {
|
|
2034
|
+
throw new TypeError(`Invalid "${type}Key"`)
|
|
2035
|
+
}
|
|
2036
|
+
|
|
2037
|
+
if (extractable && k.extractable !== true) {
|
|
2038
|
+
throw new TypeError(`"${type}Key" must be extractable`)
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
|
|
2043
|
+
|
|
2044
|
+
|
|
2045
|
+
|
|
2046
|
+
|
|
2047
|
+
|
|
2048
|
+
|
|
2049
|
+
|
|
2050
|
+
|
|
2051
|
+
|
|
2052
|
+
|
|
2053
|
+
|
|
2054
|
+
|
|
2055
|
+
|
|
2056
|
+
|
|
2057
|
+
|
|
2058
|
+
|
|
2059
|
+
|
|
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
|
+
|
|
2090
|
+
|
|
2091
|
+
|
|
2092
|
+
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
|
|
2096
|
+
|
|
2097
|
+
|
|
2098
|
+
|
|
2099
|
+
|
|
2100
|
+
|
|
2101
|
+
|
|
2102
|
+
|
|
2103
|
+
|
|
2104
|
+
|
|
2105
|
+
|
|
2106
|
+
|
|
2107
|
+
|
|
2108
|
+
|
|
2109
|
+
|
|
2110
|
+
|
|
2111
|
+
|
|
2112
|
+
|
|
2113
|
+
|
|
2114
|
+
|
|
2115
|
+
|
|
2116
|
+
|
|
2117
|
+
|
|
2118
|
+
|
|
2119
|
+
|
|
2120
|
+
|
|
2121
|
+
|
|
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
|
+
export function I2OSP(n , w ) {
|
|
2161
|
+
if (!Number.isSafeInteger(w) || w <= 0) {
|
|
2162
|
+
throw new Error('w must be a positive safe integer')
|
|
2163
|
+
}
|
|
2164
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
2165
|
+
throw new Error('n must be a non-negative safe integer')
|
|
2166
|
+
}
|
|
2167
|
+
const max = Math.pow(256, w)
|
|
2168
|
+
if (n >= max) {
|
|
2169
|
+
throw new Error('n too large to fit in w-length byte string')
|
|
2170
|
+
}
|
|
2171
|
+
const ret = new Uint8Array(w)
|
|
2172
|
+
let num = n
|
|
2173
|
+
for (let i = 0; i < w && num; i++) {
|
|
2174
|
+
ret[w - (i + 1)] = num % 256
|
|
2175
|
+
num = Math.floor(num / 256)
|
|
2176
|
+
}
|
|
2177
|
+
return ret
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
function KDFStages(KDF ) {
|
|
2181
|
+
if (KDF.stages === 1 || KDF.stages === 2) {
|
|
2182
|
+
return KDF.stages
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
throw new Error('unreachable')
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
async function KeySchedule(
|
|
2189
|
+
suite ,
|
|
2190
|
+
mode ,
|
|
2191
|
+
shared_secret ,
|
|
2192
|
+
info ,
|
|
2193
|
+
psk ,
|
|
2194
|
+
pskId ,
|
|
2195
|
+
) {
|
|
2196
|
+
info ??= new Uint8Array()
|
|
2197
|
+
checkUint8Array(info, 'info')
|
|
2198
|
+
psk ??= new Uint8Array()
|
|
2199
|
+
checkUint8Array(psk, 'psk')
|
|
2200
|
+
pskId ??= new Uint8Array()
|
|
2201
|
+
checkUint8Array(pskId, 'pskId')
|
|
2202
|
+
|
|
2203
|
+
const stages = KDFStages(suite.KDF)
|
|
2204
|
+
const CombineSecrets = stages === 1 ? CombineSecretsOneStage : CombineSecretsTwoStage
|
|
2205
|
+
|
|
2206
|
+
VerifyPSKInputs(psk, pskId)
|
|
2207
|
+
return await CombineSecrets(suite, mode, shared_secret, info, psk, pskId)
|
|
2208
|
+
}
|
|
2209
|
+
|
|
2210
|
+
function VerifyPSKInputs(psk , psk_id ) {
|
|
2211
|
+
if (psk.byteLength && psk_id.byteLength) {
|
|
2212
|
+
if (psk.byteLength < 32) {
|
|
2213
|
+
throw new TypeError('Insufficient PSK length')
|
|
2214
|
+
}
|
|
2215
|
+
return
|
|
2216
|
+
}
|
|
2217
|
+
if (!psk.byteLength && !psk_id.byteLength) {
|
|
2218
|
+
return
|
|
2219
|
+
}
|
|
2220
|
+
throw new TypeError('Inconsistent PSK inputs')
|
|
2221
|
+
}
|
|
2222
|
+
|
|
2223
|
+
|
|
2224
|
+
const NotApplicable = () => {
|
|
2225
|
+
throw new Error('unreachable')
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2228
|
+
const EXPORT_ONLY = 0xffff
|
|
2229
|
+
|
|
2230
|
+
|
|
2231
|
+
|
|
2232
|
+
|
|
2233
|
+
|
|
2234
|
+
|
|
2235
|
+
|
|
2236
|
+
|
|
2237
|
+
|
|
2238
|
+
|
|
2239
|
+
|
|
2240
|
+
export const AEAD_EXPORT_ONLY = function () {
|
|
2241
|
+
return {
|
|
2242
|
+
id: EXPORT_ONLY,
|
|
2243
|
+
type: 'AEAD',
|
|
2244
|
+
name: 'Export-only',
|
|
2245
|
+
Nk: 0,
|
|
2246
|
+
Nn: 0,
|
|
2247
|
+
Nt: 0,
|
|
2248
|
+
Seal: NotApplicable,
|
|
2249
|
+
Open: NotApplicable,
|
|
2250
|
+
}
|
|
2251
|
+
}
|
|
2252
|
+
|
|
2253
|
+
|
|
2254
|
+
|
|
2255
|
+
|
|
2256
|
+
|
|
2257
|
+
async function subtle (promise , name ) {
|
|
2258
|
+
try {
|
|
2259
|
+
return await promise(crypto.subtle)
|
|
2260
|
+
} catch (cause) {
|
|
2261
|
+
if (
|
|
2262
|
+
cause instanceof TypeError ||
|
|
2263
|
+
(cause instanceof DOMException && cause.name === 'NotSupportedError')
|
|
2264
|
+
) {
|
|
2265
|
+
throw new NotSupportedError(`${name} is unsupported in this runtime`, { cause })
|
|
2266
|
+
}
|
|
2267
|
+
throw cause
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
|
|
2271
|
+
|
|
2272
|
+
|
|
2273
|
+
|
|
2274
|
+
|
|
2275
|
+
|
|
2276
|
+
|
|
2277
|
+
function sab(input ) {
|
|
2278
|
+
return typeof SharedArrayBuffer !== 'undefined' && input instanceof SharedArrayBuffer
|
|
2279
|
+
}
|
|
2280
|
+
|
|
2281
|
+
function ab(input ) {
|
|
2282
|
+
if (sab(input.buffer)) {
|
|
2283
|
+
throw new TypeError('input must not be a SharedArrayBuffer')
|
|
2284
|
+
}
|
|
2285
|
+
if (input.byteLength === input.buffer.byteLength) {
|
|
2286
|
+
return input.buffer
|
|
2287
|
+
}
|
|
2288
|
+
return input.buffer.slice(input.byteOffset, input.byteOffset + input.byteLength)
|
|
2289
|
+
}
|
|
2290
|
+
|
|
2291
|
+
function HKDF_SHARED() {
|
|
2292
|
+
return {
|
|
2293
|
+
stages: 2,
|
|
2294
|
+
Derive: NotApplicable,
|
|
2295
|
+
async Extract( _salt, _ikm) {
|
|
2296
|
+
let salt
|
|
2297
|
+
if (_salt.byteLength === 0) {
|
|
2298
|
+
salt = new ArrayBuffer(this.Nh)
|
|
2299
|
+
} else {
|
|
2300
|
+
salt = ab(_salt)
|
|
2301
|
+
}
|
|
2302
|
+
const ikm = ab(_ikm)
|
|
2303
|
+
return new Uint8Array(
|
|
2304
|
+
await subtle(
|
|
2305
|
+
async (c) =>
|
|
2306
|
+
c.sign(
|
|
2307
|
+
'HMAC',
|
|
2308
|
+
await c.importKey('raw', salt, { name: 'HMAC', hash: this.hash }, false, ['sign']),
|
|
2309
|
+
ikm,
|
|
2310
|
+
),
|
|
2311
|
+
this.name,
|
|
2312
|
+
),
|
|
2313
|
+
)
|
|
2314
|
+
},
|
|
2315
|
+
async Expand( _prk, info, L) {
|
|
2316
|
+
if (_prk.byteLength < this.Nh) {
|
|
2317
|
+
throw new Error('prk.byteLength < this.Nh')
|
|
2318
|
+
}
|
|
2319
|
+
if (L > 255 * this.Nh) {
|
|
2320
|
+
throw new Error('L must be <= 255*Nh')
|
|
2321
|
+
}
|
|
2322
|
+
const N = Math.ceil(L / this.Nh)
|
|
2323
|
+
const prk = ab(_prk)
|
|
2324
|
+
const key = await subtle(
|
|
2325
|
+
(c) => c.importKey('raw', prk, { name: 'HMAC', hash: this.hash }, false, ['sign']),
|
|
2326
|
+
this.name,
|
|
2327
|
+
)
|
|
2328
|
+
|
|
2329
|
+
const T = new Uint8Array(N * this.Nh)
|
|
2330
|
+
let T_prev = new Uint8Array()
|
|
2331
|
+
|
|
2332
|
+
for (let i = 0; i < N; i++) {
|
|
2333
|
+
const input = new Uint8Array(T_prev.byteLength + info.byteLength + 1)
|
|
2334
|
+
input.set(T_prev)
|
|
2335
|
+
input.set(info, T_prev.byteLength)
|
|
2336
|
+
input[T_prev.byteLength + info.byteLength] = i + 1
|
|
2337
|
+
|
|
2338
|
+
const T_i = new Uint8Array(await subtle((c) => c.sign('HMAC', key, input), this.name))
|
|
2339
|
+
|
|
2340
|
+
T.set(T_i, i * this.Nh)
|
|
2341
|
+
T_prev = T_i
|
|
2342
|
+
}
|
|
2343
|
+
|
|
2344
|
+
return slice(T, 0, L)
|
|
2345
|
+
},
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
|
|
2349
|
+
|
|
2350
|
+
|
|
2351
|
+
|
|
2352
|
+
|
|
2353
|
+
|
|
2354
|
+
|
|
2355
|
+
|
|
2356
|
+
|
|
2357
|
+
|
|
2358
|
+
|
|
2359
|
+
|
|
2360
|
+
|
|
2361
|
+
|
|
2362
|
+
|
|
2363
|
+
|
|
2364
|
+
|
|
2365
|
+
|
|
2366
|
+
|
|
2367
|
+
|
|
2368
|
+
|
|
2369
|
+
|
|
2370
|
+
|
|
2371
|
+
export const KDF_HKDF_SHA256 = function () {
|
|
2372
|
+
return { id: 0x0001, type: 'KDF', name: 'HKDF-SHA256', Nh: 32, hash: 'SHA-256', ...HKDF_SHARED() }
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
|
|
2376
|
+
|
|
2377
|
+
|
|
2378
|
+
|
|
2379
|
+
|
|
2380
|
+
|
|
2381
|
+
|
|
2382
|
+
|
|
2383
|
+
|
|
2384
|
+
|
|
2385
|
+
|
|
2386
|
+
|
|
2387
|
+
|
|
2388
|
+
|
|
2389
|
+
|
|
2390
|
+
|
|
2391
|
+
|
|
2392
|
+
|
|
2393
|
+
export const KDF_HKDF_SHA384 = function () {
|
|
2394
|
+
return { id: 0x0002, type: 'KDF', name: 'HKDF-SHA384', Nh: 48, hash: 'SHA-384', ...HKDF_SHARED() }
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
|
|
2398
|
+
|
|
2399
|
+
|
|
2400
|
+
|
|
2401
|
+
|
|
2402
|
+
|
|
2403
|
+
|
|
2404
|
+
|
|
2405
|
+
|
|
2406
|
+
|
|
2407
|
+
|
|
2408
|
+
|
|
2409
|
+
|
|
2410
|
+
|
|
2411
|
+
|
|
2412
|
+
|
|
2413
|
+
|
|
2414
|
+
|
|
2415
|
+
export const KDF_HKDF_SHA512 = function () {
|
|
2416
|
+
return { id: 0x0003, type: 'KDF', name: 'HKDF-SHA512', Nh: 64, hash: 'SHA-512', ...HKDF_SHARED() }
|
|
2417
|
+
}
|
|
2418
|
+
|
|
2419
|
+
|
|
2420
|
+
|
|
2421
|
+
|
|
2422
|
+
|
|
2423
|
+
|
|
2424
|
+
|
|
2425
|
+
|
|
2426
|
+
|
|
2427
|
+
async function ShakeDerive(name , variant , ikm , L ) {
|
|
2428
|
+
const alg = { name: variant, length: L << 3 }
|
|
2429
|
+
return new Uint8Array(await subtle((c) => c.digest(alg, ikm), name))
|
|
2430
|
+
}
|
|
2431
|
+
|
|
2432
|
+
function SHAKE_SHARED() {
|
|
2433
|
+
return {
|
|
2434
|
+
stages: 1,
|
|
2435
|
+
async Derive( labeled_ikm, L ) {
|
|
2436
|
+
return await ShakeDerive(this.name, this.algorithm, ab(labeled_ikm), L)
|
|
2437
|
+
},
|
|
2438
|
+
Extract: NotApplicable,
|
|
2439
|
+
Expand: NotApplicable,
|
|
2440
|
+
}
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
|
|
2444
|
+
|
|
2445
|
+
|
|
2446
|
+
|
|
2447
|
+
|
|
2448
|
+
|
|
2449
|
+
|
|
2450
|
+
|
|
2451
|
+
|
|
2452
|
+
|
|
2453
|
+
|
|
2454
|
+
|
|
2455
|
+
|
|
2456
|
+
|
|
2457
|
+
|
|
2458
|
+
|
|
2459
|
+
|
|
2460
|
+
|
|
2461
|
+
export const KDF_SHAKE128 = function () {
|
|
2462
|
+
return {
|
|
2463
|
+
id: 0x0010,
|
|
2464
|
+
type: 'KDF',
|
|
2465
|
+
name: 'SHAKE128',
|
|
2466
|
+
Nh: 32,
|
|
2467
|
+
algorithm: 'cSHAKE128',
|
|
2468
|
+
...SHAKE_SHARED(),
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
|
|
2472
|
+
|
|
2473
|
+
|
|
2474
|
+
|
|
2475
|
+
|
|
2476
|
+
|
|
2477
|
+
|
|
2478
|
+
|
|
2479
|
+
|
|
2480
|
+
|
|
2481
|
+
|
|
2482
|
+
|
|
2483
|
+
|
|
2484
|
+
|
|
2485
|
+
|
|
2486
|
+
|
|
2487
|
+
|
|
2488
|
+
|
|
2489
|
+
|
|
2490
|
+
export const KDF_SHAKE256 = function () {
|
|
2491
|
+
return {
|
|
2492
|
+
id: 0x0011,
|
|
2493
|
+
type: 'KDF',
|
|
2494
|
+
name: 'SHAKE256',
|
|
2495
|
+
Nh: 64,
|
|
2496
|
+
algorithm: 'cSHAKE256',
|
|
2497
|
+
...SHAKE_SHARED(),
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2501
|
+
async function getPublicKeyByExport(
|
|
2502
|
+
name ,
|
|
2503
|
+
key ,
|
|
2504
|
+
usages ,
|
|
2505
|
+
) {
|
|
2506
|
+
if (!key.extractable) {
|
|
2507
|
+
throw new TypeError(
|
|
2508
|
+
'"privateKey" must be extractable or a Key Pair must be used in this runtime',
|
|
2509
|
+
)
|
|
2510
|
+
}
|
|
2511
|
+
|
|
2512
|
+
return await subtle(async (c) => {
|
|
2513
|
+
const jwk = await c.exportKey('jwk', key)
|
|
2514
|
+
return c.importKey(
|
|
2515
|
+
'jwk',
|
|
2516
|
+
{ kty: jwk.kty, crv: jwk.crv, x: jwk.x, y: jwk.y } ,
|
|
2517
|
+
key.algorithm,
|
|
2518
|
+
true,
|
|
2519
|
+
usages,
|
|
2520
|
+
)
|
|
2521
|
+
}, name)
|
|
2522
|
+
}
|
|
2523
|
+
|
|
2524
|
+
async function getPublicKey(name , key , usages ) {
|
|
2525
|
+
return (
|
|
2526
|
+
|
|
2527
|
+
((await subtle((c) => c.getPublicKey?.(key, usages), name)) ) ||
|
|
2528
|
+
(await getPublicKeyByExport(name, key, usages))
|
|
2529
|
+
)
|
|
2530
|
+
}
|
|
2531
|
+
|
|
2532
|
+
|
|
2533
|
+
function checkNotAllZeros(buffer ) {
|
|
2534
|
+
let or = 0
|
|
2535
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
2536
|
+
or |= buffer[i]
|
|
2537
|
+
}
|
|
2538
|
+
if (or === 0) {
|
|
2539
|
+
throw new ValidationError('DH shared secret is an all-zero value')
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
|
|
2543
|
+
|
|
2544
|
+
|
|
2545
|
+
|
|
2546
|
+
|
|
2547
|
+
|
|
2548
|
+
|
|
2549
|
+
|
|
2550
|
+
|
|
2551
|
+
|
|
2552
|
+
|
|
2553
|
+
|
|
2554
|
+
|
|
2555
|
+
|
|
2556
|
+
|
|
2557
|
+
|
|
2558
|
+
|
|
2559
|
+
|
|
2560
|
+
|
|
2561
|
+
|
|
2562
|
+
function fromBase64(input ) {
|
|
2563
|
+
input = input.replace(/-/g, '+').replace(/_/g, '/')
|
|
2564
|
+
const binary = atob(input)
|
|
2565
|
+
const bytes = new Uint8Array(binary.length)
|
|
2566
|
+
for (let i = 0; i < binary.length; i++) {
|
|
2567
|
+
bytes[i] = binary.charCodeAt(i)
|
|
2568
|
+
}
|
|
2569
|
+
return bytes
|
|
2570
|
+
}
|
|
2571
|
+
|
|
2572
|
+
function toBase64Url(bytes ) {
|
|
2573
|
+
let binary = ''
|
|
2574
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2575
|
+
binary += String.fromCharCode(bytes[i] )
|
|
2576
|
+
}
|
|
2577
|
+
return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '')
|
|
2578
|
+
}
|
|
2579
|
+
|
|
2580
|
+
function toB64u(input ) {
|
|
2581
|
+
|
|
2582
|
+
return input.toBase64?.({ alphabet: 'base64url', omitPadding: true }) || toBase64Url(input)
|
|
2583
|
+
}
|
|
2584
|
+
|
|
2585
|
+
function b64u(input ) {
|
|
2586
|
+
|
|
2587
|
+
return Uint8Array.fromBase64?.(input, { alphabet: 'base64url' }) || fromBase64(input)
|
|
2588
|
+
}
|
|
2589
|
+
|
|
2590
|
+
|
|
2591
|
+
|
|
2592
|
+
|
|
2593
|
+
|
|
2594
|
+
async function DeriveCandidate(
|
|
2595
|
+
DHKEM ,
|
|
2596
|
+
suite_id ,
|
|
2597
|
+
ikm ,
|
|
2598
|
+
counter ,
|
|
2599
|
+
) {
|
|
2600
|
+
const dkp_prk = await LabeledExtract(
|
|
2601
|
+
DHKEM.kdf,
|
|
2602
|
+
suite_id,
|
|
2603
|
+
new Uint8Array(),
|
|
2604
|
+
encode('dkp_prk'),
|
|
2605
|
+
ikm,
|
|
2606
|
+
)
|
|
2607
|
+
return await LabeledExpand(
|
|
2608
|
+
DHKEM.kdf,
|
|
2609
|
+
suite_id,
|
|
2610
|
+
dkp_prk,
|
|
2611
|
+
encode('candidate'),
|
|
2612
|
+
I2OSP(counter, 1),
|
|
2613
|
+
DHKEM.Nsk,
|
|
2614
|
+
)
|
|
2615
|
+
}
|
|
2616
|
+
|
|
2617
|
+
function OS2IP(x ) {
|
|
2618
|
+
let result = 0n
|
|
2619
|
+
for (let i = 0; i < x.byteLength; i++) {
|
|
2620
|
+
result = result * 256n + BigInt(x[i] )
|
|
2621
|
+
}
|
|
2622
|
+
return result
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
function bigIntToUint8Array(value , byteLength ) {
|
|
2626
|
+
const result = new Uint8Array(byteLength)
|
|
2627
|
+
let n = value
|
|
2628
|
+
|
|
2629
|
+
for (let i = byteLength - 1; i >= 0; i--) {
|
|
2630
|
+
result[i] = Number(n & 0xffn)
|
|
2631
|
+
n = n >> 8n
|
|
2632
|
+
}
|
|
2633
|
+
|
|
2634
|
+
return result
|
|
2635
|
+
}
|
|
2636
|
+
|
|
2637
|
+
function assertKeyAlgorithm(key , expectedAlgorithm ) {
|
|
2638
|
+
if (key.algorithm.name !== expectedAlgorithm.name) {
|
|
2639
|
+
throw new TypeError(`key algorithm must be ${expectedAlgorithm.name}`)
|
|
2640
|
+
}
|
|
2641
|
+
if (
|
|
2642
|
+
(key.algorithm ).namedCurve !==
|
|
2643
|
+
(expectedAlgorithm ).namedCurve
|
|
2644
|
+
) {
|
|
2645
|
+
throw new TypeError(
|
|
2646
|
+
`key namedCurve must be ${(expectedAlgorithm ).namedCurve}`,
|
|
2647
|
+
)
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
|
|
2651
|
+
function assertCryptoKey(key ) {
|
|
2652
|
+
|
|
2653
|
+
if (key[Symbol.toStringTag] !== 'CryptoKey') {
|
|
2654
|
+
if (key instanceof CryptoKey) return
|
|
2655
|
+
throw new TypeError('unexpected key constructor')
|
|
2656
|
+
}
|
|
2657
|
+
}
|
|
2658
|
+
|
|
2659
|
+
|
|
2660
|
+
|
|
2661
|
+
|
|
2662
|
+
|
|
2663
|
+
function DHKEM_SHARED() {
|
|
2664
|
+
return {
|
|
2665
|
+
async GenerateKeyPair( extractable) {
|
|
2666
|
+
return (await subtle(
|
|
2667
|
+
(c) => c.generateKey(this.algorithm, extractable, ['deriveBits']),
|
|
2668
|
+
this.name,
|
|
2669
|
+
))
|
|
2670
|
+
},
|
|
2671
|
+
async SerializePublicKey( key) {
|
|
2672
|
+
assertKeyAlgorithm(key, this.algorithm)
|
|
2673
|
+
assertCryptoKey(key)
|
|
2674
|
+
return new Uint8Array(await subtle((c) => c.exportKey('raw', key), this.name))
|
|
2675
|
+
},
|
|
2676
|
+
async DeserializePublicKey( _key) {
|
|
2677
|
+
const key = ab(_key)
|
|
2678
|
+
return await subtle((c) => c.importKey('raw', key, this.algorithm, true, []), this.name)
|
|
2679
|
+
},
|
|
2680
|
+
async SerializePrivateKey( key) {
|
|
2681
|
+
assertKeyAlgorithm(key, this.algorithm)
|
|
2682
|
+
assertCryptoKey(key)
|
|
2683
|
+
const { d } = await subtle((c) => c.exportKey('jwk', key), this.name)
|
|
2684
|
+
return b64u(d )
|
|
2685
|
+
},
|
|
2686
|
+
async Encap( pkR) {
|
|
2687
|
+
assertKeyAlgorithm(pkR, this.algorithm)
|
|
2688
|
+
assertCryptoKey(pkR)
|
|
2689
|
+
|
|
2690
|
+
const ekp = (await this.GenerateKeyPair(false))
|
|
2691
|
+
const skE = ekp.privateKey
|
|
2692
|
+
const pkE = ekp.publicKey
|
|
2693
|
+
|
|
2694
|
+
|
|
2695
|
+
|
|
2696
|
+
const dh = new Uint8Array(
|
|
2697
|
+
await subtle(
|
|
2698
|
+
(c) => c.deriveBits({ name: skE.algorithm.name, public: pkR }, skE, this.Ndh << 3),
|
|
2699
|
+
this.name,
|
|
2700
|
+
),
|
|
2701
|
+
)
|
|
2702
|
+
checkNotAllZeros(dh)
|
|
2703
|
+
|
|
2704
|
+
const enc = await this.SerializePublicKey(pkE)
|
|
2705
|
+
const pkRm = await this.SerializePublicKey(pkR)
|
|
2706
|
+
const kem_context = concat(enc, pkRm)
|
|
2707
|
+
const eae_prk = await LabeledExtract(
|
|
2708
|
+
this.kdf,
|
|
2709
|
+
this.suite_id,
|
|
2710
|
+
new Uint8Array(),
|
|
2711
|
+
encode('eae_prk'),
|
|
2712
|
+
dh,
|
|
2713
|
+
)
|
|
2714
|
+
const shared_secret = await LabeledExpand(
|
|
2715
|
+
this.kdf,
|
|
2716
|
+
this.suite_id,
|
|
2717
|
+
eae_prk,
|
|
2718
|
+
encode('shared_secret'),
|
|
2719
|
+
kem_context,
|
|
2720
|
+
this.Nsecret,
|
|
2721
|
+
)
|
|
2722
|
+
return { shared_secret, enc }
|
|
2723
|
+
},
|
|
2724
|
+
async Decap( enc, skR, pkR) {
|
|
2725
|
+
assertKeyAlgorithm(skR, this.algorithm)
|
|
2726
|
+
assertCryptoKey(skR)
|
|
2727
|
+
if (pkR) {
|
|
2728
|
+
assertKeyAlgorithm(pkR, this.algorithm)
|
|
2729
|
+
assertCryptoKey(pkR)
|
|
2730
|
+
} else {
|
|
2731
|
+
pkR = await getPublicKey(this.name, skR, [])
|
|
2732
|
+
}
|
|
2733
|
+
|
|
2734
|
+
const pkE = (await this.DeserializePublicKey(enc))
|
|
2735
|
+
|
|
2736
|
+
|
|
2737
|
+
|
|
2738
|
+
const dh = new Uint8Array(
|
|
2739
|
+
await subtle(
|
|
2740
|
+
(c) => c.deriveBits({ name: skR.algorithm.name, public: pkE }, skR, this.Ndh << 3),
|
|
2741
|
+
this.name,
|
|
2742
|
+
),
|
|
2743
|
+
)
|
|
2744
|
+
checkNotAllZeros(dh)
|
|
2745
|
+
|
|
2746
|
+
const pkRm = await this.SerializePublicKey(pkR)
|
|
2747
|
+
const kem_context = concat(enc, pkRm)
|
|
2748
|
+
const eae_prk = await LabeledExtract(
|
|
2749
|
+
this.kdf,
|
|
2750
|
+
this.suite_id,
|
|
2751
|
+
new Uint8Array(),
|
|
2752
|
+
encode('eae_prk'),
|
|
2753
|
+
dh,
|
|
2754
|
+
)
|
|
2755
|
+
const shared_secret = await LabeledExpand(
|
|
2756
|
+
this.kdf,
|
|
2757
|
+
this.suite_id,
|
|
2758
|
+
eae_prk,
|
|
2759
|
+
encode('shared_secret'),
|
|
2760
|
+
kem_context,
|
|
2761
|
+
this.Nsecret,
|
|
2762
|
+
)
|
|
2763
|
+
return shared_secret
|
|
2764
|
+
},
|
|
2765
|
+
}
|
|
2766
|
+
}
|
|
2767
|
+
|
|
2768
|
+
|
|
2769
|
+
|
|
2770
|
+
|
|
2771
|
+
|
|
2772
|
+
async function createKeyPairFromPrivateKey(
|
|
2773
|
+
DHKEM ,
|
|
2774
|
+
key ,
|
|
2775
|
+
extractable ,
|
|
2776
|
+
) {
|
|
2777
|
+
let privateKey
|
|
2778
|
+
let publicKey
|
|
2779
|
+
|
|
2780
|
+
if (!extractable && typeof crypto.subtle.getPublicKey !== 'function') {
|
|
2781
|
+
privateKey = (await DHKEM.DeserializePrivateKey(key, true))
|
|
2782
|
+
publicKey = await getPublicKey(DHKEM.name, privateKey, [])
|
|
2783
|
+
privateKey = (await DHKEM.DeserializePrivateKey(key, false))
|
|
2784
|
+
} else {
|
|
2785
|
+
privateKey = (await DHKEM.DeserializePrivateKey(key, extractable))
|
|
2786
|
+
publicKey = await getPublicKey(DHKEM.name, privateKey, [])
|
|
2787
|
+
}
|
|
2788
|
+
return { privateKey, publicKey }
|
|
2789
|
+
}
|
|
2790
|
+
|
|
2791
|
+
async function CurveKeyFromD(
|
|
2792
|
+
name ,
|
|
2793
|
+
Nsk ,
|
|
2794
|
+
template ,
|
|
2795
|
+
algorithm ,
|
|
2796
|
+
key ,
|
|
2797
|
+
extractable ,
|
|
2798
|
+
) {
|
|
2799
|
+
const tmpl = slice(template)
|
|
2800
|
+
const pkcs8 = new Uint8Array(Nsk + tmpl.byteLength)
|
|
2801
|
+
pkcs8.set(tmpl)
|
|
2802
|
+
pkcs8.set(key, tmpl.byteLength)
|
|
2803
|
+
return await subtle(
|
|
2804
|
+
(c) => c.importKey('pkcs8', pkcs8, algorithm, extractable, ['deriveBits']),
|
|
2805
|
+
name,
|
|
2806
|
+
)
|
|
2807
|
+
}
|
|
2808
|
+
|
|
2809
|
+
|
|
2810
|
+
|
|
2811
|
+
|
|
2812
|
+
|
|
2813
|
+
|
|
2814
|
+
|
|
2815
|
+
|
|
2816
|
+
|
|
2817
|
+
|
|
2818
|
+
|
|
2819
|
+
function modInverse(a , m ) {
|
|
2820
|
+
a = ((a % m) + m) % m
|
|
2821
|
+
let [t, newT] = [0n, 1n]
|
|
2822
|
+
let [r, newR] = [m, a]
|
|
2823
|
+
|
|
2824
|
+
while (newR !== 0n) {
|
|
2825
|
+
const quotient = r / newR
|
|
2826
|
+
;[t, newT] = [newT, t - quotient * newT]
|
|
2827
|
+
;[r, newR] = [newR, r - quotient * newR]
|
|
2828
|
+
}
|
|
2829
|
+
|
|
2830
|
+
if (r > 1n) throw new Error('a is not invertible')
|
|
2831
|
+
if (t < 0n) t = t + m
|
|
2832
|
+
return t
|
|
2833
|
+
}
|
|
2834
|
+
|
|
2835
|
+
|
|
2836
|
+
function pointDouble(p , prime , a ) {
|
|
2837
|
+
const { x, y } = p
|
|
2838
|
+
|
|
2839
|
+
|
|
2840
|
+
const numerator = (((3n * x * x) % prime) + a) % prime
|
|
2841
|
+
const denominator = (2n * y) % prime
|
|
2842
|
+
const s = (numerator * modInverse(denominator, prime)) % prime
|
|
2843
|
+
|
|
2844
|
+
|
|
2845
|
+
const x3 = (((s * s) % prime) - ((2n * x) % prime) + prime) % prime
|
|
2846
|
+
|
|
2847
|
+
|
|
2848
|
+
const y3 = (((s * ((x - x3 + prime) % prime)) % prime) - y + prime) % prime
|
|
2849
|
+
|
|
2850
|
+
return { x: x3, y: y3 }
|
|
2851
|
+
}
|
|
2852
|
+
|
|
2853
|
+
|
|
2854
|
+
function pointAdd(p , q , prime , a ) {
|
|
2855
|
+
if (p.x === q.x && p.y === q.y) {
|
|
2856
|
+
return pointDouble(p, prime, a)
|
|
2857
|
+
}
|
|
2858
|
+
|
|
2859
|
+
const { x: x1, y: y1 } = p
|
|
2860
|
+
const { x: x2, y: y2 } = q
|
|
2861
|
+
|
|
2862
|
+
|
|
2863
|
+
|
|
2864
|
+
if (p.x === q.x) {
|
|
2865
|
+
throw new Error('Point addition resulted in point at infinity')
|
|
2866
|
+
}
|
|
2867
|
+
|
|
2868
|
+
|
|
2869
|
+
const numerator = (((y2 - y1) % prime) + prime) % prime
|
|
2870
|
+
const denominator = (((x2 - x1) % prime) + prime) % prime
|
|
2871
|
+
const s = (numerator * modInverse(denominator, prime)) % prime
|
|
2872
|
+
|
|
2873
|
+
|
|
2874
|
+
const x3 = (((s * s) % prime) - x1 - x2 + prime + prime) % prime
|
|
2875
|
+
|
|
2876
|
+
|
|
2877
|
+
const y3 = (((s * ((x1 - x3 + prime) % prime)) % prime) - y1 + prime) % prime
|
|
2878
|
+
|
|
2879
|
+
return { x: x3, y: y3 }
|
|
2880
|
+
}
|
|
2881
|
+
|
|
2882
|
+
|
|
2883
|
+
function scalarMult(k , G , prime , a , order ) {
|
|
2884
|
+
if (k === 0n || k >= order) {
|
|
2885
|
+
throw new Error('Invalid scalar')
|
|
2886
|
+
}
|
|
2887
|
+
|
|
2888
|
+
let result = null
|
|
2889
|
+
let addend = G
|
|
2890
|
+
let scalar = k
|
|
2891
|
+
|
|
2892
|
+
while (scalar > 0n) {
|
|
2893
|
+
if (scalar & 1n) {
|
|
2894
|
+
result = result === null ? addend : pointAdd(result, addend, prime, a)
|
|
2895
|
+
}
|
|
2896
|
+
addend = pointDouble(addend, prime, a)
|
|
2897
|
+
scalar = scalar >> 1n
|
|
2898
|
+
}
|
|
2899
|
+
|
|
2900
|
+
if (result === null) throw new Error('Invalid result')
|
|
2901
|
+
return result
|
|
2902
|
+
}
|
|
2903
|
+
|
|
2904
|
+
|
|
2905
|
+
|
|
2906
|
+
|
|
2907
|
+
|
|
2908
|
+
|
|
2909
|
+
|
|
2910
|
+
|
|
2911
|
+
|
|
2912
|
+
|
|
2913
|
+
|
|
2914
|
+
|
|
2915
|
+
|
|
2916
|
+
function getPrivateJwkNist(DHKEM , d ) {
|
|
2917
|
+
|
|
2918
|
+
const G = { x: DHKEM.Gx, y: DHKEM.Gy }
|
|
2919
|
+
const publicPoint = scalarMult(d, G, DHKEM.prime, DHKEM.prime - 3n, DHKEM.order)
|
|
2920
|
+
|
|
2921
|
+
const coordSize = (DHKEM.Npk - 1) / 2
|
|
2922
|
+
const xBytes = bigIntToUint8Array(publicPoint.x, coordSize)
|
|
2923
|
+
const yBytes = bigIntToUint8Array(publicPoint.y, coordSize)
|
|
2924
|
+
const dBytes = bigIntToUint8Array(d, DHKEM.Nsk)
|
|
2925
|
+
|
|
2926
|
+
|
|
2927
|
+
return {
|
|
2928
|
+
kty: 'EC',
|
|
2929
|
+
crv: DHKEM.algorithm.namedCurve,
|
|
2930
|
+
x: toB64u(xBytes),
|
|
2931
|
+
y: toB64u(yBytes),
|
|
2932
|
+
d: toB64u(dBytes),
|
|
2933
|
+
}
|
|
2934
|
+
}
|
|
2935
|
+
|
|
2936
|
+
async function DeserializePrivateKeyNist(
|
|
2937
|
+
|
|
2938
|
+
key ,
|
|
2939
|
+
extractable ,
|
|
2940
|
+
) {
|
|
2941
|
+
const d = OS2IP(key)
|
|
2942
|
+
const jwk = getPrivateJwkNist(this, d)
|
|
2943
|
+
|
|
2944
|
+
const privateKey = await subtle(
|
|
2945
|
+
(c) => c.importKey('jwk', jwk, this.algorithm, extractable, ['deriveBits']),
|
|
2946
|
+
this.name,
|
|
2947
|
+
)
|
|
2948
|
+
|
|
2949
|
+
return privateKey
|
|
2950
|
+
}
|
|
2951
|
+
|
|
2952
|
+
async function DeriveKeyPairNist(
|
|
2953
|
+
|
|
2954
|
+
ikm ,
|
|
2955
|
+
extractable ,
|
|
2956
|
+
) {
|
|
2957
|
+
let sk = 0n
|
|
2958
|
+
let counter = 0
|
|
2959
|
+
let bytes
|
|
2960
|
+
while (sk === 0n || sk >= this.order) {
|
|
2961
|
+
if (counter > 255) {
|
|
2962
|
+
throw new DeriveKeyPairError('Key derivation exceeded maximum iterations')
|
|
2963
|
+
}
|
|
2964
|
+
bytes = await DeriveCandidate(this, this.suite_id, ikm, counter)
|
|
2965
|
+
bytes[0] = bytes[0] & this.bitmask
|
|
2966
|
+
sk = OS2IP(bytes)
|
|
2967
|
+
counter = counter + 1
|
|
2968
|
+
}
|
|
2969
|
+
|
|
2970
|
+
return GetKeyPairNist(this, bytes , extractable, this.name)
|
|
2971
|
+
}
|
|
2972
|
+
|
|
2973
|
+
async function GetKeyPairNist(
|
|
2974
|
+
curveConfig ,
|
|
2975
|
+
sk ,
|
|
2976
|
+
extractable ,
|
|
2977
|
+
name ,
|
|
2978
|
+
) {
|
|
2979
|
+
const jwk = getPrivateJwkNist(curveConfig, OS2IP(sk))
|
|
2980
|
+
|
|
2981
|
+
const privateKey = await subtle(
|
|
2982
|
+
(c) => c.importKey('jwk', jwk, curveConfig.algorithm, extractable, ['deriveBits']),
|
|
2983
|
+
name,
|
|
2984
|
+
)
|
|
2985
|
+
|
|
2986
|
+
delete jwk.d
|
|
2987
|
+
const publicKey = await subtle(
|
|
2988
|
+
(c) => c.importKey('jwk', jwk, curveConfig.algorithm, true, []),
|
|
2989
|
+
name,
|
|
2990
|
+
)
|
|
2991
|
+
|
|
2992
|
+
return { privateKey, publicKey }
|
|
2993
|
+
}
|
|
2994
|
+
|
|
2995
|
+
|
|
2996
|
+
|
|
2997
|
+
|
|
2998
|
+
|
|
2999
|
+
async function DeriveKeyPairX( ikm , extractable ) {
|
|
3000
|
+
const dkp_prk = await LabeledExtract(
|
|
3001
|
+
this.kdf,
|
|
3002
|
+
this.suite_id,
|
|
3003
|
+
new Uint8Array(),
|
|
3004
|
+
encode('dkp_prk'),
|
|
3005
|
+
ikm,
|
|
3006
|
+
)
|
|
3007
|
+
const sk = await LabeledExpand(
|
|
3008
|
+
this.kdf,
|
|
3009
|
+
this.suite_id,
|
|
3010
|
+
dkp_prk,
|
|
3011
|
+
encode('sk'),
|
|
3012
|
+
new Uint8Array(),
|
|
3013
|
+
this.Nsk,
|
|
3014
|
+
)
|
|
3015
|
+
return await createKeyPairFromPrivateKey(this, sk, extractable)
|
|
3016
|
+
}
|
|
3017
|
+
|
|
3018
|
+
|
|
3019
|
+
|
|
3020
|
+
|
|
3021
|
+
|
|
3022
|
+
const P256 = {
|
|
3023
|
+
algorithm: { name: 'ECDH', namedCurve: 'P-256' },
|
|
3024
|
+
Npk: 65,
|
|
3025
|
+
Nsk: 32,
|
|
3026
|
+
order: 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n,
|
|
3027
|
+
bitmask: 0xff,
|
|
3028
|
+
prime: 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffffn,
|
|
3029
|
+
Gx: 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296n,
|
|
3030
|
+
Gy: 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5n,
|
|
3031
|
+
}
|
|
3032
|
+
|
|
3033
|
+
|
|
3034
|
+
|
|
3035
|
+
|
|
3036
|
+
|
|
3037
|
+
|
|
3038
|
+
|
|
3039
|
+
|
|
3040
|
+
|
|
3041
|
+
|
|
3042
|
+
|
|
3043
|
+
|
|
3044
|
+
|
|
3045
|
+
|
|
3046
|
+
|
|
3047
|
+
|
|
3048
|
+
|
|
3049
|
+
|
|
3050
|
+
|
|
3051
|
+
|
|
3052
|
+
export const KEM_DHKEM_P256_HKDF_SHA256 = function () {
|
|
3053
|
+
const id = 0x0010
|
|
3054
|
+
const name = 'DHKEM(P-256, HKDF-SHA256)'
|
|
3055
|
+
const kdf = KDF_HKDF_SHA256()
|
|
3056
|
+
|
|
3057
|
+
kdf.name = name
|
|
3058
|
+
return {
|
|
3059
|
+
id,
|
|
3060
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
3061
|
+
type: 'KEM',
|
|
3062
|
+
name,
|
|
3063
|
+
kdf,
|
|
3064
|
+
Nsecret: 32,
|
|
3065
|
+
Nenc: 65,
|
|
3066
|
+
Ndh: 32,
|
|
3067
|
+
...P256,
|
|
3068
|
+
DeriveKeyPair: DeriveKeyPairNist,
|
|
3069
|
+
DeserializePrivateKey: DeserializePrivateKeyNist,
|
|
3070
|
+
...DHKEM_SHARED(),
|
|
3071
|
+
}
|
|
3072
|
+
}
|
|
3073
|
+
|
|
3074
|
+
const P384 = {
|
|
3075
|
+
algorithm: { name: 'ECDH', namedCurve: 'P-384' },
|
|
3076
|
+
Npk: 97,
|
|
3077
|
+
Nsk: 48,
|
|
3078
|
+
order:
|
|
3079
|
+
0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973n,
|
|
3080
|
+
bitmask: 0xff,
|
|
3081
|
+
prime:
|
|
3082
|
+
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffffn,
|
|
3083
|
+
Gx: 0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7n,
|
|
3084
|
+
Gy: 0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5fn,
|
|
3085
|
+
}
|
|
3086
|
+
|
|
3087
|
+
|
|
3088
|
+
|
|
3089
|
+
|
|
3090
|
+
|
|
3091
|
+
|
|
3092
|
+
|
|
3093
|
+
|
|
3094
|
+
|
|
3095
|
+
|
|
3096
|
+
|
|
3097
|
+
|
|
3098
|
+
|
|
3099
|
+
|
|
3100
|
+
|
|
3101
|
+
|
|
3102
|
+
|
|
3103
|
+
|
|
3104
|
+
|
|
3105
|
+
|
|
3106
|
+
export const KEM_DHKEM_P384_HKDF_SHA384 = function () {
|
|
3107
|
+
const id = 0x0011
|
|
3108
|
+
const name = 'DHKEM(P-384, HKDF-SHA384)'
|
|
3109
|
+
const kdf = KDF_HKDF_SHA384()
|
|
3110
|
+
|
|
3111
|
+
kdf.name = name
|
|
3112
|
+
return {
|
|
3113
|
+
id,
|
|
3114
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
3115
|
+
type: 'KEM',
|
|
3116
|
+
name,
|
|
3117
|
+
kdf,
|
|
3118
|
+
Nsecret: 48,
|
|
3119
|
+
Nenc: 97,
|
|
3120
|
+
Ndh: 48,
|
|
3121
|
+
...P384,
|
|
3122
|
+
DeriveKeyPair: DeriveKeyPairNist,
|
|
3123
|
+
DeserializePrivateKey: DeserializePrivateKeyNist,
|
|
3124
|
+
...DHKEM_SHARED(),
|
|
3125
|
+
}
|
|
3126
|
+
}
|
|
3127
|
+
|
|
3128
|
+
const P521 = {
|
|
3129
|
+
Npk: 133,
|
|
3130
|
+
Nsk: 66,
|
|
3131
|
+
algorithm: { name: 'ECDH', namedCurve: 'P-521' },
|
|
3132
|
+
order:
|
|
3133
|
+
0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409n,
|
|
3134
|
+
bitmask: 0x01,
|
|
3135
|
+
prime:
|
|
3136
|
+
0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn,
|
|
3137
|
+
Gx: 0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66n,
|
|
3138
|
+
Gy: 0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650n,
|
|
3139
|
+
}
|
|
3140
|
+
|
|
3141
|
+
|
|
3142
|
+
|
|
3143
|
+
|
|
3144
|
+
|
|
3145
|
+
|
|
3146
|
+
|
|
3147
|
+
|
|
3148
|
+
|
|
3149
|
+
|
|
3150
|
+
|
|
3151
|
+
|
|
3152
|
+
|
|
3153
|
+
|
|
3154
|
+
|
|
3155
|
+
|
|
3156
|
+
|
|
3157
|
+
|
|
3158
|
+
|
|
3159
|
+
|
|
3160
|
+
export const KEM_DHKEM_P521_HKDF_SHA512 = function () {
|
|
3161
|
+
const id = 0x0012
|
|
3162
|
+
const name = 'DHKEM(P-521, HKDF-SHA512)'
|
|
3163
|
+
const kdf = KDF_HKDF_SHA512()
|
|
3164
|
+
|
|
3165
|
+
kdf.name = name
|
|
3166
|
+
return {
|
|
3167
|
+
id,
|
|
3168
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
3169
|
+
type: 'KEM',
|
|
3170
|
+
name,
|
|
3171
|
+
kdf,
|
|
3172
|
+
Nsecret: 64,
|
|
3173
|
+
Nenc: 133,
|
|
3174
|
+
Ndh: 66,
|
|
3175
|
+
...P521,
|
|
3176
|
+
DeriveKeyPair: DeriveKeyPairNist,
|
|
3177
|
+
DeserializePrivateKey: DeserializePrivateKeyNist,
|
|
3178
|
+
...DHKEM_SHARED(),
|
|
3179
|
+
}
|
|
3180
|
+
}
|
|
3181
|
+
|
|
3182
|
+
|
|
3183
|
+
|
|
3184
|
+
|
|
3185
|
+
|
|
3186
|
+
|
|
3187
|
+
|
|
3188
|
+
|
|
3189
|
+
|
|
3190
|
+
|
|
3191
|
+
|
|
3192
|
+
|
|
3193
|
+
|
|
3194
|
+
|
|
3195
|
+
|
|
3196
|
+
|
|
3197
|
+
|
|
3198
|
+
|
|
3199
|
+
|
|
3200
|
+
|
|
3201
|
+
|
|
3202
|
+
|
|
3203
|
+
|
|
3204
|
+
|
|
3205
|
+
export const KEM_DHKEM_X25519_HKDF_SHA256 = function () {
|
|
3206
|
+
const id = 0x0020
|
|
3207
|
+
const name = 'DHKEM(X25519, HKDF-SHA256)'
|
|
3208
|
+
const kdf = KDF_HKDF_SHA256()
|
|
3209
|
+
|
|
3210
|
+
kdf.name = name
|
|
3211
|
+
return {
|
|
3212
|
+
id,
|
|
3213
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
3214
|
+
type: 'KEM',
|
|
3215
|
+
name,
|
|
3216
|
+
kdf,
|
|
3217
|
+
Nsecret: 32,
|
|
3218
|
+
Nenc: 32,
|
|
3219
|
+
Npk: 32,
|
|
3220
|
+
Nsk: 32,
|
|
3221
|
+
Ndh: 32,
|
|
3222
|
+
algorithm: { name: 'X25519' },
|
|
3223
|
+
pkcs8: Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20),
|
|
3224
|
+
DeriveKeyPair: DeriveKeyPairX,
|
|
3225
|
+
async DeserializePrivateKey(key, extractable) {
|
|
3226
|
+
return await CurveKeyFromD(name, this.Nsk, this.pkcs8, this.algorithm, key, extractable)
|
|
3227
|
+
},
|
|
3228
|
+
...DHKEM_SHARED(),
|
|
3229
|
+
}
|
|
3230
|
+
}
|
|
3231
|
+
|
|
3232
|
+
|
|
3233
|
+
|
|
3234
|
+
|
|
3235
|
+
|
|
3236
|
+
|
|
3237
|
+
|
|
3238
|
+
|
|
3239
|
+
|
|
3240
|
+
|
|
3241
|
+
|
|
3242
|
+
|
|
3243
|
+
|
|
3244
|
+
|
|
3245
|
+
|
|
3246
|
+
|
|
3247
|
+
|
|
3248
|
+
|
|
3249
|
+
|
|
3250
|
+
|
|
3251
|
+
export const KEM_DHKEM_X448_HKDF_SHA512 = function () {
|
|
3252
|
+
const id = 0x0021
|
|
3253
|
+
const name = 'DHKEM(X448, HKDF-SHA512)'
|
|
3254
|
+
const kdf = KDF_HKDF_SHA512()
|
|
3255
|
+
|
|
3256
|
+
kdf.name = name
|
|
3257
|
+
return {
|
|
3258
|
+
id,
|
|
3259
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
3260
|
+
type: 'KEM',
|
|
3261
|
+
name,
|
|
3262
|
+
kdf,
|
|
3263
|
+
Nsecret: 64,
|
|
3264
|
+
Nenc: 56,
|
|
3265
|
+
Npk: 56,
|
|
3266
|
+
Nsk: 56,
|
|
3267
|
+
Ndh: 56,
|
|
3268
|
+
algorithm: { name: 'X448' },
|
|
3269
|
+
pkcs8: Uint8Array.of(0x30, 0x46, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6f, 0x04, 0x3a, 0x04, 0x38),
|
|
3270
|
+
DeriveKeyPair: DeriveKeyPairX,
|
|
3271
|
+
async DeserializePrivateKey(key, extractable) {
|
|
3272
|
+
return await CurveKeyFromD(name, this.Nsk, this.pkcs8, this.algorithm, key, extractable)
|
|
3273
|
+
},
|
|
3274
|
+
...DHKEM_SHARED(),
|
|
3275
|
+
}
|
|
3276
|
+
}
|
|
3277
|
+
|
|
3278
|
+
|
|
3279
|
+
|
|
3280
|
+
|
|
3281
|
+
|
|
3282
|
+
|
|
3283
|
+
|
|
3284
|
+
|
|
3285
|
+
|
|
3286
|
+
|
|
3287
|
+
|
|
3288
|
+
function MLKEM_SHARED() {
|
|
3289
|
+
return {
|
|
3290
|
+
async DeriveKeyPair( ikm, extractable) {
|
|
3291
|
+
const dk = await LabeledDerive(
|
|
3292
|
+
this.kdf,
|
|
3293
|
+
this.suite_id,
|
|
3294
|
+
ikm,
|
|
3295
|
+
encode('DeriveKeyPair'),
|
|
3296
|
+
new Uint8Array(),
|
|
3297
|
+
this.Nsk,
|
|
3298
|
+
)
|
|
3299
|
+
|
|
3300
|
+
const privateKey = (await this.DeserializePrivateKey(dk, extractable))
|
|
3301
|
+
|
|
3302
|
+
const usages = ['encapsulateBits']
|
|
3303
|
+
const publicKey = await getPublicKey(this.name, privateKey, usages)
|
|
3304
|
+
|
|
3305
|
+
return { privateKey, publicKey }
|
|
3306
|
+
},
|
|
3307
|
+
async GenerateKeyPair( extractable) {
|
|
3308
|
+
|
|
3309
|
+
const usages = ['encapsulateBits', 'decapsulateBits']
|
|
3310
|
+
return (await subtle(
|
|
3311
|
+
(c) => c.generateKey(this.algorithm, extractable, usages),
|
|
3312
|
+
this.name,
|
|
3313
|
+
))
|
|
3314
|
+
},
|
|
3315
|
+
async SerializePublicKey( key) {
|
|
3316
|
+
assertKeyAlgorithm(key, this.algorithm)
|
|
3317
|
+
assertCryptoKey(key)
|
|
3318
|
+
|
|
3319
|
+
const format = 'raw-public'
|
|
3320
|
+
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3321
|
+
},
|
|
3322
|
+
async DeserializePublicKey( _key) {
|
|
3323
|
+
|
|
3324
|
+
const format = 'raw-public'
|
|
3325
|
+
|
|
3326
|
+
const usages = ['encapsulateBits']
|
|
3327
|
+
const key = ab(_key)
|
|
3328
|
+
return await subtle((c) => c.importKey(format, key, this.algorithm, true, usages), this.name)
|
|
3329
|
+
},
|
|
3330
|
+
async SerializePrivateKey( key) {
|
|
3331
|
+
assertKeyAlgorithm(key, this.algorithm)
|
|
3332
|
+
assertCryptoKey(key)
|
|
3333
|
+
|
|
3334
|
+
const format = 'raw-seed'
|
|
3335
|
+
return new Uint8Array(await subtle((c) => c.exportKey(format, key), this.name))
|
|
3336
|
+
},
|
|
3337
|
+
async DeserializePrivateKey( _key, extractable) {
|
|
3338
|
+
|
|
3339
|
+
const format = 'raw-seed'
|
|
3340
|
+
|
|
3341
|
+
const usages = ['decapsulateBits']
|
|
3342
|
+
const key = ab(_key)
|
|
3343
|
+
return await subtle(
|
|
3344
|
+
(c) => c.importKey(format, key, this.algorithm, extractable, usages),
|
|
3345
|
+
this.name,
|
|
3346
|
+
)
|
|
3347
|
+
},
|
|
3348
|
+
async Encap( pkR) {
|
|
3349
|
+
assertKeyAlgorithm(pkR, this.algorithm)
|
|
3350
|
+
|
|
3351
|
+
const { sharedKey, ciphertext } = (await subtle(
|
|
3352
|
+
|
|
3353
|
+
(c) => c.encapsulateBits(this.algorithm, pkR),
|
|
3354
|
+
this.name,
|
|
3355
|
+
))
|
|
3356
|
+
|
|
3357
|
+
return { shared_secret: new Uint8Array(sharedKey), enc: new Uint8Array(ciphertext) }
|
|
3358
|
+
},
|
|
3359
|
+
async Decap( _enc, skR, _pkR) {
|
|
3360
|
+
assertKeyAlgorithm(skR, this.algorithm)
|
|
3361
|
+
const enc = ab(_enc)
|
|
3362
|
+
return new Uint8Array(
|
|
3363
|
+
await subtle(
|
|
3364
|
+
|
|
3365
|
+
(c) => c.decapsulateBits(this.algorithm, skR, enc),
|
|
3366
|
+
this.name,
|
|
3367
|
+
),
|
|
3368
|
+
)
|
|
3369
|
+
},
|
|
3370
|
+
}
|
|
3371
|
+
}
|
|
3372
|
+
|
|
3373
|
+
|
|
3374
|
+
|
|
3375
|
+
|
|
3376
|
+
|
|
3377
|
+
|
|
3378
|
+
|
|
3379
|
+
|
|
3380
|
+
|
|
3381
|
+
|
|
3382
|
+
|
|
3383
|
+
|
|
3384
|
+
|
|
3385
|
+
|
|
3386
|
+
|
|
3387
|
+
|
|
3388
|
+
|
|
3389
|
+
|
|
3390
|
+
|
|
3391
|
+
|
|
3392
|
+
|
|
3393
|
+
|
|
3394
|
+
|
|
3395
|
+
export const KEM_ML_KEM_512 = function () {
|
|
3396
|
+
const id = 0x0040
|
|
3397
|
+
const name = 'ML-KEM-512'
|
|
3398
|
+
const kdf = KDF_SHAKE256()
|
|
3399
|
+
|
|
3400
|
+
kdf.name = name
|
|
3401
|
+
return {
|
|
3402
|
+
id,
|
|
3403
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
3404
|
+
type: 'KEM',
|
|
3405
|
+
name,
|
|
3406
|
+
Nsecret: 32,
|
|
3407
|
+
Nenc: 768,
|
|
3408
|
+
Npk: 800,
|
|
3409
|
+
Nsk: 64,
|
|
3410
|
+
algorithm: { name: 'ML-KEM-512' },
|
|
3411
|
+
kdf,
|
|
3412
|
+
...MLKEM_SHARED(),
|
|
3413
|
+
}
|
|
3414
|
+
}
|
|
3415
|
+
|
|
3416
|
+
|
|
3417
|
+
|
|
3418
|
+
|
|
3419
|
+
|
|
3420
|
+
|
|
3421
|
+
|
|
3422
|
+
|
|
3423
|
+
|
|
3424
|
+
|
|
3425
|
+
|
|
3426
|
+
|
|
3427
|
+
|
|
3428
|
+
|
|
3429
|
+
|
|
3430
|
+
|
|
3431
|
+
|
|
3432
|
+
|
|
3433
|
+
|
|
3434
|
+
export const KEM_ML_KEM_768 = function () {
|
|
3435
|
+
const id = 0x0041
|
|
3436
|
+
const name = 'ML-KEM-768'
|
|
3437
|
+
const kdf = KDF_SHAKE256()
|
|
3438
|
+
|
|
3439
|
+
kdf.name = name
|
|
3440
|
+
return {
|
|
3441
|
+
id,
|
|
3442
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
3443
|
+
type: 'KEM',
|
|
3444
|
+
name,
|
|
3445
|
+
Nsecret: 32,
|
|
3446
|
+
Nenc: 1088,
|
|
3447
|
+
Npk: 1184,
|
|
3448
|
+
Nsk: 64,
|
|
3449
|
+
algorithm: { name: 'ML-KEM-768' },
|
|
3450
|
+
kdf,
|
|
3451
|
+
...MLKEM_SHARED(),
|
|
3452
|
+
}
|
|
3453
|
+
}
|
|
3454
|
+
|
|
3455
|
+
|
|
3456
|
+
|
|
3457
|
+
|
|
3458
|
+
|
|
3459
|
+
|
|
3460
|
+
|
|
3461
|
+
|
|
3462
|
+
|
|
3463
|
+
|
|
3464
|
+
|
|
3465
|
+
|
|
3466
|
+
|
|
3467
|
+
|
|
3468
|
+
|
|
3469
|
+
|
|
3470
|
+
|
|
3471
|
+
|
|
3472
|
+
|
|
3473
|
+
export const KEM_ML_KEM_1024 = function () {
|
|
3474
|
+
const id = 0x0042
|
|
3475
|
+
const name = 'ML-KEM-1024'
|
|
3476
|
+
const kdf = KDF_SHAKE256()
|
|
3477
|
+
|
|
3478
|
+
kdf.name = name
|
|
3479
|
+
return {
|
|
3480
|
+
id,
|
|
3481
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
3482
|
+
type: 'KEM',
|
|
3483
|
+
name,
|
|
3484
|
+
Nsecret: 32,
|
|
3485
|
+
Nenc: 1568,
|
|
3486
|
+
Npk: 1568,
|
|
3487
|
+
Nsk: 64,
|
|
3488
|
+
algorithm: { name: 'ML-KEM-1024' },
|
|
3489
|
+
kdf,
|
|
3490
|
+
...MLKEM_SHARED(),
|
|
3491
|
+
}
|
|
3492
|
+
}
|
|
3493
|
+
|
|
3494
|
+
|
|
3495
|
+
|
|
3496
|
+
|
|
3497
|
+
|
|
3498
|
+
|
|
3499
|
+
|
|
3500
|
+
|
|
3501
|
+
function AEAD_SHARED() {
|
|
3502
|
+
return {
|
|
3503
|
+
async Seal( _key, _nonce, _aad, _pt) {
|
|
3504
|
+
const nonce = ab(_nonce)
|
|
3505
|
+
const aad = ab(_aad)
|
|
3506
|
+
const key = ab(_key)
|
|
3507
|
+
const pt = ab(_pt)
|
|
3508
|
+
return new Uint8Array(
|
|
3509
|
+
await subtle(
|
|
3510
|
+
async (c) =>
|
|
3511
|
+
c.encrypt(
|
|
3512
|
+
{ name: this.algorithm, iv: nonce, additionalData: aad },
|
|
3513
|
+
await c.importKey(this.keyFormat, key, this.algorithm, false, ['encrypt']),
|
|
3514
|
+
pt,
|
|
3515
|
+
),
|
|
3516
|
+
this.name,
|
|
3517
|
+
),
|
|
3518
|
+
)
|
|
3519
|
+
},
|
|
3520
|
+
async Open( _key, _nonce, _aad, _ct) {
|
|
3521
|
+
const nonce = ab(_nonce)
|
|
3522
|
+
const aad = ab(_aad)
|
|
3523
|
+
const key = ab(_key)
|
|
3524
|
+
const ct = ab(_ct)
|
|
3525
|
+
return new Uint8Array(
|
|
3526
|
+
await subtle(
|
|
3527
|
+
async (c) =>
|
|
3528
|
+
c.decrypt(
|
|
3529
|
+
{ name: this.algorithm, iv: nonce, additionalData: aad },
|
|
3530
|
+
await c.importKey(this.keyFormat, key, this.algorithm, false, ['decrypt']),
|
|
3531
|
+
ct,
|
|
3532
|
+
),
|
|
3533
|
+
this.name,
|
|
3534
|
+
),
|
|
3535
|
+
)
|
|
3536
|
+
},
|
|
3537
|
+
}
|
|
3538
|
+
}
|
|
3539
|
+
|
|
3540
|
+
|
|
3541
|
+
|
|
3542
|
+
|
|
3543
|
+
|
|
3544
|
+
|
|
3545
|
+
|
|
3546
|
+
|
|
3547
|
+
|
|
3548
|
+
|
|
3549
|
+
|
|
3550
|
+
|
|
3551
|
+
|
|
3552
|
+
|
|
3553
|
+
|
|
3554
|
+
|
|
3555
|
+
|
|
3556
|
+
|
|
3557
|
+
|
|
3558
|
+
|
|
3559
|
+
|
|
3560
|
+
|
|
3561
|
+
export const AEAD_AES_128_GCM = function () {
|
|
3562
|
+
return {
|
|
3563
|
+
id: 0x0001,
|
|
3564
|
+
type: 'AEAD',
|
|
3565
|
+
name: 'AES-128-GCM',
|
|
3566
|
+
Nk: 16,
|
|
3567
|
+
Nn: 12,
|
|
3568
|
+
Nt: 16,
|
|
3569
|
+
algorithm: 'AES-GCM',
|
|
3570
|
+
keyFormat: 'raw',
|
|
3571
|
+
...AEAD_SHARED(),
|
|
3572
|
+
}
|
|
3573
|
+
}
|
|
3574
|
+
|
|
3575
|
+
|
|
3576
|
+
|
|
3577
|
+
|
|
3578
|
+
|
|
3579
|
+
|
|
3580
|
+
|
|
3581
|
+
|
|
3582
|
+
|
|
3583
|
+
|
|
3584
|
+
|
|
3585
|
+
|
|
3586
|
+
|
|
3587
|
+
|
|
3588
|
+
|
|
3589
|
+
|
|
3590
|
+
|
|
3591
|
+
|
|
3592
|
+
export const AEAD_AES_256_GCM = function () {
|
|
3593
|
+
return {
|
|
3594
|
+
id: 0x0002,
|
|
3595
|
+
type: 'AEAD',
|
|
3596
|
+
name: 'AES-256-GCM',
|
|
3597
|
+
Nk: 32,
|
|
3598
|
+
Nn: 12,
|
|
3599
|
+
Nt: 16,
|
|
3600
|
+
algorithm: 'AES-GCM',
|
|
3601
|
+
keyFormat: 'raw',
|
|
3602
|
+
...AEAD_SHARED(),
|
|
3603
|
+
}
|
|
3604
|
+
}
|
|
3605
|
+
|
|
3606
|
+
|
|
3607
|
+
|
|
3608
|
+
|
|
3609
|
+
|
|
3610
|
+
|
|
3611
|
+
|
|
3612
|
+
|
|
3613
|
+
|
|
3614
|
+
|
|
3615
|
+
|
|
3616
|
+
|
|
3617
|
+
|
|
3618
|
+
|
|
3619
|
+
|
|
3620
|
+
|
|
3621
|
+
|
|
3622
|
+
|
|
3623
|
+
export const AEAD_ChaCha20Poly1305 = function AEAD_ChaCha20Poly1305() {
|
|
3624
|
+
return {
|
|
3625
|
+
id: 0x0003,
|
|
3626
|
+
type: 'AEAD',
|
|
3627
|
+
name: 'ChaCha20Poly1305',
|
|
3628
|
+
Nk: 32,
|
|
3629
|
+
Nn: 12,
|
|
3630
|
+
Nt: 16,
|
|
3631
|
+
algorithm: 'ChaCha20-Poly1305',
|
|
3632
|
+
|
|
3633
|
+
keyFormat: 'raw-secret',
|
|
3634
|
+
...AEAD_SHARED(),
|
|
3635
|
+
}
|
|
3636
|
+
}
|
|
3637
|
+
|
|
3638
|
+
|
|
3639
|
+
|
|
3640
|
+
|
|
3641
|
+
|
|
3642
|
+
|
|
3643
|
+
const InvalidInvocation = (_ ) => {
|
|
3644
|
+
if (_ !== priv) {
|
|
3645
|
+
throw new Error('invalid invocation')
|
|
3646
|
+
}
|
|
3647
|
+
}
|
|
3648
|
+
const priv = Symbol()
|
|
3649
|
+
class HybridKey {
|
|
3650
|
+
#algorithm
|
|
3651
|
+
#type
|
|
3652
|
+
#extractable
|
|
3653
|
+
#t
|
|
3654
|
+
#pq
|
|
3655
|
+
#seed
|
|
3656
|
+
#publicKey
|
|
3657
|
+
|
|
3658
|
+
static #isValid(key ) {
|
|
3659
|
+
return key.#algorithm !== undefined
|
|
3660
|
+
}
|
|
3661
|
+
|
|
3662
|
+
static validate(key , extractable ) {
|
|
3663
|
+
try {
|
|
3664
|
+
if (!HybridKey.#isValid(key )) {
|
|
3665
|
+
throw new TypeError('unexpected key constructor')
|
|
3666
|
+
}
|
|
3667
|
+
} catch {
|
|
3668
|
+
throw new TypeError('unexpected key constructor')
|
|
3669
|
+
}
|
|
3670
|
+
if (extractable && !(key ).extractable) {
|
|
3671
|
+
throw new TypeError('key must be extractable')
|
|
3672
|
+
}
|
|
3673
|
+
}
|
|
3674
|
+
|
|
3675
|
+
constructor(
|
|
3676
|
+
_ ,
|
|
3677
|
+
algorithm ,
|
|
3678
|
+
type ,
|
|
3679
|
+
extractable ,
|
|
3680
|
+
pq ,
|
|
3681
|
+
t ,
|
|
3682
|
+
seed ,
|
|
3683
|
+
publicKey ,
|
|
3684
|
+
) {
|
|
3685
|
+
InvalidInvocation(_)
|
|
3686
|
+
this.#algorithm = algorithm
|
|
3687
|
+
this.#type = type
|
|
3688
|
+
this.#extractable = extractable
|
|
3689
|
+
this.#pq = pq
|
|
3690
|
+
this.#t = t
|
|
3691
|
+
this.#seed = seed
|
|
3692
|
+
this.#publicKey = publicKey
|
|
3693
|
+
}
|
|
3694
|
+
|
|
3695
|
+
get algorithm() {
|
|
3696
|
+
return { name: this.#algorithm.name }
|
|
3697
|
+
}
|
|
3698
|
+
|
|
3699
|
+
get extractable() {
|
|
3700
|
+
return this.#extractable
|
|
3701
|
+
}
|
|
3702
|
+
|
|
3703
|
+
get type() {
|
|
3704
|
+
return this.#type
|
|
3705
|
+
}
|
|
3706
|
+
|
|
3707
|
+
getPublicKey(_ ) {
|
|
3708
|
+
InvalidInvocation(_)
|
|
3709
|
+
return this.#publicKey
|
|
3710
|
+
}
|
|
3711
|
+
|
|
3712
|
+
getSeed(_ ) {
|
|
3713
|
+
InvalidInvocation(_)
|
|
3714
|
+
return slice(this.#seed )
|
|
3715
|
+
}
|
|
3716
|
+
|
|
3717
|
+
getT(_ ) {
|
|
3718
|
+
InvalidInvocation(_)
|
|
3719
|
+
return this.#t
|
|
3720
|
+
}
|
|
3721
|
+
|
|
3722
|
+
getPq(_ ) {
|
|
3723
|
+
InvalidInvocation(_)
|
|
3724
|
+
return this.#pq
|
|
3725
|
+
}
|
|
3726
|
+
}
|
|
3727
|
+
|
|
3728
|
+
function split(N1 , N2 , x ) {
|
|
3729
|
+
if (x.byteLength !== N1 + N2) {
|
|
3730
|
+
throw new Error('x.byteLength !== N1 + N2')
|
|
3731
|
+
}
|
|
3732
|
+
|
|
3733
|
+
const x1 = slice(x, 0, N1)
|
|
3734
|
+
const x2 = slice(x, -N2)
|
|
3735
|
+
|
|
3736
|
+
return [x1, x2]
|
|
3737
|
+
}
|
|
3738
|
+
|
|
3739
|
+
function RandomScalarNist(t , seed ) {
|
|
3740
|
+
let sk_bigint = 0n
|
|
3741
|
+
let start = 0
|
|
3742
|
+
let end = t.Nscalar
|
|
3743
|
+
sk_bigint = OS2IP(slice(seed, start, end))
|
|
3744
|
+
|
|
3745
|
+
while (sk_bigint === 0n || sk_bigint >= t.order ) {
|
|
3746
|
+
start = end
|
|
3747
|
+
end = end + t.Nscalar
|
|
3748
|
+
if (end > seed.byteLength) {
|
|
3749
|
+
throw new DeriveKeyPairError('Rejection sampling failed')
|
|
3750
|
+
}
|
|
3751
|
+
sk_bigint = OS2IP(slice(seed, start, end))
|
|
3752
|
+
}
|
|
3753
|
+
return bigIntToUint8Array(sk_bigint, t.Nscalar )
|
|
3754
|
+
}
|
|
3755
|
+
|
|
3756
|
+
async function expandDecapsKeyG(PQTKEM , _seed ) {
|
|
3757
|
+
const Nout = PQTKEM.pq.Nseed + PQTKEM.t.Nseed
|
|
3758
|
+
|
|
3759
|
+
const algorithm = { name: 'cSHAKE256', length: Nout << 3 }
|
|
3760
|
+
const seed = ab(_seed)
|
|
3761
|
+
const seed_full = await subtle((c) => c.digest(algorithm, seed), PQTKEM.name)
|
|
3762
|
+
|
|
3763
|
+
const [_seed_PQ, seed_T] = split(PQTKEM.pq.Nseed, PQTKEM.t.Nseed, new Uint8Array(seed_full))
|
|
3764
|
+
const seed_PQ = ab(_seed_PQ)
|
|
3765
|
+
|
|
3766
|
+
|
|
3767
|
+
const format = 'raw-seed'
|
|
3768
|
+
|
|
3769
|
+
const usages = ['decapsulateBits', 'encapsulateBits']
|
|
3770
|
+
const dk_PQ = await subtle(
|
|
3771
|
+
(c) => c.importKey(format, seed_PQ, PQTKEM.pq.algorithm, true, [usages[0]]),
|
|
3772
|
+
PQTKEM.name,
|
|
3773
|
+
)
|
|
3774
|
+
const ek_PQ = await getPublicKey(PQTKEM.name, dk_PQ, [usages[1]])
|
|
3775
|
+
|
|
3776
|
+
const sk = PQTKEM.t.RandomScalar?.(seed_T) ?? seed_T
|
|
3777
|
+
const { privateKey: dk_T, publicKey: ek_T } = await PQTKEM.t.GetKeyPair(sk)
|
|
3778
|
+
|
|
3779
|
+
return { ek_PQ, ek_T, dk_PQ, dk_T }
|
|
3780
|
+
}
|
|
3781
|
+
|
|
3782
|
+
async function C2PRICombiner(
|
|
3783
|
+
PQTKEM ,
|
|
3784
|
+
ss_PQ ,
|
|
3785
|
+
ss_T ,
|
|
3786
|
+
ct_T ,
|
|
3787
|
+
_ek_T ,
|
|
3788
|
+
label ,
|
|
3789
|
+
) {
|
|
3790
|
+
const ek_T = new Uint8Array(await subtle((c) => c.exportKey('raw', _ek_T), PQTKEM.name))
|
|
3791
|
+
const data = ab(concat(ss_PQ, ss_T, ct_T, ek_T, label))
|
|
3792
|
+
return new Uint8Array(await subtle((c) => c.digest('SHA3-256', data), PQTKEM.name))
|
|
3793
|
+
}
|
|
3794
|
+
|
|
3795
|
+
async function prepareEncapsG(
|
|
3796
|
+
PQTKEM ,
|
|
3797
|
+
ek_PQ ,
|
|
3798
|
+
ek_T ,
|
|
3799
|
+
) {
|
|
3800
|
+
const res = (await subtle(
|
|
3801
|
+
|
|
3802
|
+
(c) => c.encapsulateBits(PQTKEM.pq.algorithm, ek_PQ),
|
|
3803
|
+
PQTKEM.name,
|
|
3804
|
+
))
|
|
3805
|
+
const ss_PQ = new Uint8Array(res.sharedKey)
|
|
3806
|
+
const ct_PQ = new Uint8Array(res.ciphertext)
|
|
3807
|
+
|
|
3808
|
+
const { privateKey: sk_E, publicKey } = (await subtle(
|
|
3809
|
+
(c) => c.generateKey(PQTKEM.t.algorithm, true, ['deriveBits']),
|
|
3810
|
+
PQTKEM.name,
|
|
3811
|
+
))
|
|
3812
|
+
const ct_T = new Uint8Array(await subtle((c) => c.exportKey('raw', publicKey), PQTKEM.name))
|
|
3813
|
+
|
|
3814
|
+
const ss_T = new Uint8Array(
|
|
3815
|
+
await subtle(
|
|
3816
|
+
(c) => c.deriveBits({ name: PQTKEM.t.algorithm.name, public: ek_T }, sk_E, PQTKEM.t.Nss << 3),
|
|
3817
|
+
PQTKEM.name,
|
|
3818
|
+
),
|
|
3819
|
+
)
|
|
3820
|
+
checkNotAllZeros(ss_T)
|
|
3821
|
+
|
|
3822
|
+
return [ss_PQ, ss_T, ct_PQ, ct_T]
|
|
3823
|
+
}
|
|
3824
|
+
|
|
3825
|
+
async function prepareDecapsG(
|
|
3826
|
+
PQTKEM ,
|
|
3827
|
+
dk_PQ ,
|
|
3828
|
+
dk_T ,
|
|
3829
|
+
ct_PQ ,
|
|
3830
|
+
_ct_T ,
|
|
3831
|
+
) {
|
|
3832
|
+
const ss_PQ = new Uint8Array(
|
|
3833
|
+
await subtle(
|
|
3834
|
+
|
|
3835
|
+
(c) => c.decapsulateBits(PQTKEM.pq.algorithm, dk_PQ, ct_PQ),
|
|
3836
|
+
PQTKEM.name,
|
|
3837
|
+
),
|
|
3838
|
+
)
|
|
3839
|
+
|
|
3840
|
+
const ct_T = ab(_ct_T)
|
|
3841
|
+
const pub = await subtle(
|
|
3842
|
+
(c) => c.importKey('raw', ct_T, PQTKEM.t.algorithm, true, []),
|
|
3843
|
+
PQTKEM.name,
|
|
3844
|
+
)
|
|
3845
|
+
|
|
3846
|
+
const ss_T = new Uint8Array(
|
|
3847
|
+
await subtle(
|
|
3848
|
+
(c) => c.deriveBits({ name: PQTKEM.t.algorithm.name, public: pub }, dk_T, PQTKEM.t.Nss << 3),
|
|
3849
|
+
PQTKEM.name,
|
|
3850
|
+
),
|
|
3851
|
+
)
|
|
3852
|
+
checkNotAllZeros(ss_T)
|
|
3853
|
+
|
|
3854
|
+
return [ss_PQ, ss_T]
|
|
3855
|
+
}
|
|
3856
|
+
|
|
3857
|
+
|
|
3858
|
+
|
|
3859
|
+
|
|
3860
|
+
|
|
3861
|
+
|
|
3862
|
+
|
|
3863
|
+
|
|
3864
|
+
|
|
3865
|
+
|
|
3866
|
+
|
|
3867
|
+
|
|
3868
|
+
|
|
3869
|
+
|
|
3870
|
+
|
|
3871
|
+
|
|
3872
|
+
|
|
3873
|
+
|
|
3874
|
+
|
|
3875
|
+
|
|
3876
|
+
|
|
3877
|
+
|
|
3878
|
+
|
|
3879
|
+
|
|
3880
|
+
|
|
3881
|
+
|
|
3882
|
+
|
|
3883
|
+
|
|
3884
|
+
|
|
3885
|
+
function PQTKEM_SHARED() {
|
|
3886
|
+
Object.freeze(HybridKey.prototype)
|
|
3887
|
+
return {
|
|
3888
|
+
async DeriveKeyPair( ikm , extractable) {
|
|
3889
|
+
const seed = await LabeledDerive(
|
|
3890
|
+
this.kdf,
|
|
3891
|
+
this.suite_id,
|
|
3892
|
+
ikm,
|
|
3893
|
+
encode('DeriveKeyPair'),
|
|
3894
|
+
new Uint8Array(),
|
|
3895
|
+
32,
|
|
3896
|
+
)
|
|
3897
|
+
|
|
3898
|
+
const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, seed)
|
|
3899
|
+
|
|
3900
|
+
const publicKey = new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
3901
|
+
const privateKey = new HybridKey(
|
|
3902
|
+
priv,
|
|
3903
|
+
this.algorithm,
|
|
3904
|
+
'private',
|
|
3905
|
+
extractable,
|
|
3906
|
+
dk_PQ,
|
|
3907
|
+
dk_T,
|
|
3908
|
+
seed,
|
|
3909
|
+
publicKey,
|
|
3910
|
+
)
|
|
3911
|
+
|
|
3912
|
+
return { privateKey, publicKey }
|
|
3913
|
+
},
|
|
3914
|
+
async GenerateKeyPair( extractable) {
|
|
3915
|
+
return await this.DeriveKeyPair(crypto.getRandomValues(new Uint8Array(32)), extractable)
|
|
3916
|
+
},
|
|
3917
|
+
async SerializePublicKey( key) {
|
|
3918
|
+
assertKeyAlgorithm(key, this.algorithm)
|
|
3919
|
+
HybridKey.validate(key, true)
|
|
3920
|
+
|
|
3921
|
+
const format = 'raw-public'
|
|
3922
|
+
const ek_PQ = new Uint8Array(
|
|
3923
|
+
await subtle((c) => c.exportKey(format, key.getPq(priv)), this.name),
|
|
3924
|
+
)
|
|
3925
|
+
const ek_T = new Uint8Array(
|
|
3926
|
+
await subtle((c) => c.exportKey('raw', key.getT(priv)), this.name),
|
|
3927
|
+
)
|
|
3928
|
+
|
|
3929
|
+
return concat(ek_PQ, ek_T)
|
|
3930
|
+
},
|
|
3931
|
+
async DeserializePublicKey( key) {
|
|
3932
|
+
|
|
3933
|
+
const format = 'raw-public'
|
|
3934
|
+
|
|
3935
|
+
const usages = ['encapsulateBits']
|
|
3936
|
+
const pubPq = ab(key.subarray(0, this.pq.Npk))
|
|
3937
|
+
const pubT = ab(key.subarray(this.pq.Npk))
|
|
3938
|
+
const [ek_PQ, ek_T] = await Promise.all([
|
|
3939
|
+
subtle((c) => c.importKey(format, pubPq, this.pq.algorithm, true, usages), this.name),
|
|
3940
|
+
subtle((c) => c.importKey('raw', pubT, this.t.algorithm, true, []), this.name),
|
|
3941
|
+
])
|
|
3942
|
+
|
|
3943
|
+
return new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
3944
|
+
},
|
|
3945
|
+
async SerializePrivateKey( key) {
|
|
3946
|
+
assertKeyAlgorithm(key, this.algorithm)
|
|
3947
|
+
HybridKey.validate(key, true)
|
|
3948
|
+
|
|
3949
|
+
return key.getSeed(priv)
|
|
3950
|
+
},
|
|
3951
|
+
async DeserializePrivateKey( key, extractable) {
|
|
3952
|
+
const { ek_PQ, ek_T, dk_PQ, dk_T } = await expandDecapsKeyG(this, key)
|
|
3953
|
+
const publicKey = new HybridKey(priv, this.algorithm, 'public', true, ek_PQ, ek_T)
|
|
3954
|
+
const privateKey = new HybridKey(
|
|
3955
|
+
priv,
|
|
3956
|
+
this.algorithm,
|
|
3957
|
+
'private',
|
|
3958
|
+
extractable,
|
|
3959
|
+
dk_PQ,
|
|
3960
|
+
dk_T,
|
|
3961
|
+
slice(key),
|
|
3962
|
+
publicKey,
|
|
3963
|
+
)
|
|
3964
|
+
|
|
3965
|
+
return privateKey
|
|
3966
|
+
},
|
|
3967
|
+
async Encap( pkR) {
|
|
3968
|
+
assertKeyAlgorithm(pkR, this.algorithm)
|
|
3969
|
+
HybridKey.validate(pkR)
|
|
3970
|
+
|
|
3971
|
+
const ek_PQ = pkR.getPq(priv)
|
|
3972
|
+
const ek_T = pkR.getT(priv)
|
|
3973
|
+
const [ss_PQ, ss_T, ct_PQ, ct_T] = await prepareEncapsG(this, ek_PQ, ek_T)
|
|
3974
|
+
const ss_H = await C2PRICombiner(this, ss_PQ, ss_T, ct_T, ek_T, this.label)
|
|
3975
|
+
const ct_H = concat(ct_PQ, ct_T)
|
|
3976
|
+
|
|
3977
|
+
return { shared_secret: ss_H, enc: ct_H }
|
|
3978
|
+
},
|
|
3979
|
+
async Decap( enc, skR, pkR) {
|
|
3980
|
+
assertKeyAlgorithm(skR, this.algorithm)
|
|
3981
|
+
HybridKey.validate(skR)
|
|
3982
|
+
|
|
3983
|
+
if (pkR) {
|
|
3984
|
+
assertKeyAlgorithm(pkR, this.algorithm)
|
|
3985
|
+
HybridKey.validate(pkR)
|
|
3986
|
+
}
|
|
3987
|
+
|
|
3988
|
+
const [ct_PQ, ct_T] = split(this.pq.Nct, this.t.Nct, enc)
|
|
3989
|
+
const ek = pkR ?? skR.getPublicKey(priv)
|
|
3990
|
+
const ek_T = ek.getT(priv)
|
|
3991
|
+
const dk_PQ = skR.getPq(priv)
|
|
3992
|
+
const dk_T = skR.getT(priv)
|
|
3993
|
+
const [ss_PQ, ss_T] = await prepareDecapsG(this, dk_PQ, dk_T, ct_PQ, ct_T)
|
|
3994
|
+
const ss_H = await C2PRICombiner(this, ss_PQ, ss_T, ct_T, ek_T, this.label)
|
|
3995
|
+
|
|
3996
|
+
return ss_H
|
|
3997
|
+
},
|
|
3998
|
+
}
|
|
3999
|
+
}
|
|
4000
|
+
|
|
4001
|
+
|
|
4002
|
+
|
|
4003
|
+
|
|
4004
|
+
|
|
4005
|
+
|
|
4006
|
+
|
|
4007
|
+
|
|
4008
|
+
|
|
4009
|
+
|
|
4010
|
+
|
|
4011
|
+
|
|
4012
|
+
|
|
4013
|
+
|
|
4014
|
+
|
|
4015
|
+
|
|
4016
|
+
|
|
4017
|
+
|
|
4018
|
+
|
|
4019
|
+
|
|
4020
|
+
|
|
4021
|
+
|
|
4022
|
+
|
|
4023
|
+
export const KEM_MLKEM768_X25519 = function () {
|
|
4024
|
+
const id = 0x647a
|
|
4025
|
+
const name = 'MLKEM768-X25519'
|
|
4026
|
+
const kdf = KDF_SHAKE256()
|
|
4027
|
+
const pkcs8 = Uint8Array.of(0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x04, 0x22, 0x04, 0x20);
|
|
4028
|
+
|
|
4029
|
+
kdf.name = name
|
|
4030
|
+
return {
|
|
4031
|
+
id,
|
|
4032
|
+
kdf,
|
|
4033
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
4034
|
+
type: 'KEM',
|
|
4035
|
+
name,
|
|
4036
|
+
Nsecret: 32,
|
|
4037
|
+
Nenc: 1120,
|
|
4038
|
+
Npk: 1216,
|
|
4039
|
+
Nsk: 32,
|
|
4040
|
+
algorithm: { name: 'MLKEM768-X25519' },
|
|
4041
|
+
pq: { algorithm: { name: 'ML-KEM-768' }, Nseed: 64, Npk: 1184, Nct: 1088 },
|
|
4042
|
+
t: {
|
|
4043
|
+
algorithm: { name: 'X25519' },
|
|
4044
|
+
Nseed: 32,
|
|
4045
|
+
Npk: 32,
|
|
4046
|
+
Nss: 32,
|
|
4047
|
+
Nsk: 32,
|
|
4048
|
+
Nct: 32,
|
|
4049
|
+
async GetKeyPair(sk) {
|
|
4050
|
+
const privateKey = await CurveKeyFromD(name, this.Nsk, pkcs8, this.algorithm, sk, true)
|
|
4051
|
+
const publicKey = await getPublicKey(name, privateKey, [])
|
|
4052
|
+
|
|
4053
|
+
return { privateKey, publicKey }
|
|
4054
|
+
},
|
|
4055
|
+
},
|
|
4056
|
+
label: Uint8Array.of(0x5c, 0x2e, 0x2f, 0x2f, 0x5e, 0x5c),
|
|
4057
|
+
...PQTKEM_SHARED(),
|
|
4058
|
+
}
|
|
4059
|
+
}
|
|
4060
|
+
|
|
4061
|
+
|
|
4062
|
+
|
|
4063
|
+
|
|
4064
|
+
|
|
4065
|
+
|
|
4066
|
+
|
|
4067
|
+
|
|
4068
|
+
|
|
4069
|
+
|
|
4070
|
+
|
|
4071
|
+
|
|
4072
|
+
|
|
4073
|
+
|
|
4074
|
+
|
|
4075
|
+
|
|
4076
|
+
|
|
4077
|
+
|
|
4078
|
+
|
|
4079
|
+
export const KEM_MLKEM768_P256 = function () {
|
|
4080
|
+
const id = 0x0050
|
|
4081
|
+
const name = 'MLKEM768-P256'
|
|
4082
|
+
const kdf = KDF_SHAKE256()
|
|
4083
|
+
|
|
4084
|
+
kdf.name = name
|
|
4085
|
+
return {
|
|
4086
|
+
id,
|
|
4087
|
+
kdf,
|
|
4088
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
4089
|
+
type: 'KEM',
|
|
4090
|
+
name,
|
|
4091
|
+
Nsecret: 32,
|
|
4092
|
+
Nenc: 1153,
|
|
4093
|
+
Npk: 1249,
|
|
4094
|
+
Nsk: 32,
|
|
4095
|
+
algorithm: { name: 'MLKEM768-P256' },
|
|
4096
|
+
pq: { algorithm: { name: 'ML-KEM-768' }, Nseed: 64, Npk: 1184, Nct: 1088 },
|
|
4097
|
+
t: {
|
|
4098
|
+
...P256,
|
|
4099
|
+
Nseed: 128,
|
|
4100
|
+
Nss: 32,
|
|
4101
|
+
Nct: 65,
|
|
4102
|
+
Nscalar: 32,
|
|
4103
|
+
order: 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n,
|
|
4104
|
+
RandomScalar(seed) {
|
|
4105
|
+
return RandomScalarNist(this, seed)
|
|
4106
|
+
},
|
|
4107
|
+
GetKeyPair(sk) {
|
|
4108
|
+
return GetKeyPairNist(P256, sk, true, name)
|
|
4109
|
+
},
|
|
4110
|
+
},
|
|
4111
|
+
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x37, 0x36, 0x38, 0x2d, 0x50, 0x32, 0x35, 0x36),
|
|
4112
|
+
...PQTKEM_SHARED(),
|
|
4113
|
+
}
|
|
4114
|
+
}
|
|
4115
|
+
|
|
4116
|
+
|
|
4117
|
+
|
|
4118
|
+
|
|
4119
|
+
|
|
4120
|
+
|
|
4121
|
+
|
|
4122
|
+
|
|
4123
|
+
|
|
4124
|
+
|
|
4125
|
+
|
|
4126
|
+
|
|
4127
|
+
|
|
4128
|
+
|
|
4129
|
+
|
|
4130
|
+
|
|
4131
|
+
|
|
4132
|
+
|
|
4133
|
+
|
|
4134
|
+
export const KEM_MLKEM1024_P384 = function () {
|
|
4135
|
+
const id = 0x0051
|
|
4136
|
+
const name = 'MLKEM1024-P384'
|
|
4137
|
+
const kdf = KDF_SHAKE256()
|
|
4138
|
+
|
|
4139
|
+
kdf.name = name
|
|
4140
|
+
return {
|
|
4141
|
+
id,
|
|
4142
|
+
kdf,
|
|
4143
|
+
suite_id: concat(encode('KEM'), I2OSP(id, 2)),
|
|
4144
|
+
type: 'KEM',
|
|
4145
|
+
name,
|
|
4146
|
+
Nsecret: 32,
|
|
4147
|
+
Nenc: 1665,
|
|
4148
|
+
Npk: 1665,
|
|
4149
|
+
Nsk: 32,
|
|
4150
|
+
algorithm: { name: 'MLKEM1024-P384' },
|
|
4151
|
+
pq: { algorithm: { name: 'ML-KEM-1024' }, Nseed: 64, Npk: 1568, Nct: 1568 },
|
|
4152
|
+
t: {
|
|
4153
|
+
...P384,
|
|
4154
|
+
Nseed: 48,
|
|
4155
|
+
Nss: 48,
|
|
4156
|
+
Nct: 97,
|
|
4157
|
+
Nscalar: 48,
|
|
4158
|
+
order:
|
|
4159
|
+
0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973n,
|
|
4160
|
+
RandomScalar(seed) {
|
|
4161
|
+
return RandomScalarNist(this, seed)
|
|
4162
|
+
},
|
|
4163
|
+
GetKeyPair(sk) {
|
|
4164
|
+
return GetKeyPairNist(P384, sk, true, name)
|
|
4165
|
+
},
|
|
4166
|
+
},
|
|
4167
|
+
label: Uint8Array.of(0x4d, 0x4c, 0x4b, 0x45, 0x4d, 0x31, 0x30, 0x32, 0x34, 0x2d, 0x50, 0x33, 0x38, 0x34),
|
|
4168
|
+
...PQTKEM_SHARED(),
|
|
4169
|
+
}
|
|
4170
|
+
}
|