-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathgenerateKey.ts
More file actions
829 lines (737 loc) · 22.5 KB
/
generateKey.ts
File metadata and controls
829 lines (737 loc) · 22.5 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
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
424
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/* eslint-disable @typescript-eslint/no-explicit-any */
import { expect } from 'chai';
import {
subtle,
// type AESAlgorithm,
// type AESLength,
type AnyAlgorithm,
type NamedCurve,
} from 'react-native-quick-crypto';
import type { CryptoKey, KeyUsage } from 'react-native-quick-crypto';
// Local interface to match what subtle.generateKey actually returns
interface TestCryptoKeyPair {
publicKey: CryptoKey;
privateKey: CryptoKey;
}
import { MLKEM_VARIANTS } from './mlkem_constants';
import { test, assertThrowsAsync } from '../util';
const SUITE = 'subtle.generateKey';
const allUsages: KeyUsage[] = [
'encrypt',
'decrypt',
'sign',
'verify',
'deriveBits',
'deriveKey',
'wrapKey',
'unwrapKey',
];
type Vector = {
algorithm?: object;
result: string;
usages: KeyUsage[];
};
type Vectors = {
[key in string]: Vector;
};
const vectors: Vectors = {
// 'AES-CTR': {
// algorithm: { length: 256 },
// result: 'CryptoKey',
// usages: ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'],
// },
// 'AES-CBC': {
// algorithm: { length: 256 },
// result: 'CryptoKey',
// usages: ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'],
// },
// 'AES-GCM': {
// algorithm: { length: 256 },
// result: 'CryptoKey',
// usages: ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'],
// },
// 'AES-KW': {
// algorithm: { length: 256 },
// result: 'CryptoKey',
// usages: ['wrapKey', 'unwrapKey'],
// },
// HMAC: {
// algorithm: { length: 256, hash: 'SHA-256' },
// result: 'CryptoKey',
// usages: ['sign', 'verify'],
// },
'RSASSA-PKCS1-v1_5': {
algorithm: {
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: 'SHA-256',
},
result: 'CryptoKeyPair',
usages: ['sign', 'verify'],
},
'RSA-PSS': {
algorithm: {
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: 'SHA-256',
},
result: 'CryptoKeyPair',
usages: ['sign', 'verify'],
},
'RSA-OAEP': {
algorithm: {
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: 'SHA-256',
},
result: 'CryptoKeyPair',
usages: ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'],
},
ECDSA: {
algorithm: { namedCurve: 'P-521' },
result: 'CryptoKeyPair',
usages: ['sign', 'verify'],
},
ECDH: {
algorithm: { namedCurve: 'P-521' },
result: 'CryptoKeyPair',
usages: ['deriveKey', 'deriveBits'],
},
// Ed25519: {
// result: 'CryptoKeyPair',
// usages: ['sign', 'verify'],
// },
// Ed448: {
// result: 'CryptoKeyPair',
// usages: ['sign', 'verify'],
// },
// X25519: {
// result: 'CryptoKeyPair',
// usages: ['deriveKey', 'deriveBits'],
// },
// X448: {
// result: 'CryptoKeyPair',
// usages: ['deriveKey', 'deriveBits'],
// },
};
// Test invalid algorithms
async function testInvalidAlgorithm(algorithm: any) {
// Tests with invalid hash algorithms get a different error message
const errorText =
algorithm.hash === 'SHA' || algorithm.hash === 'MD5'
? 'Invalid Hash Algorithm'
: 'Unrecognized algorithm name';
const algo = JSON.stringify(algorithm);
test(SUITE, `invalid algo: ${algo}`, async () => {
await assertThrowsAsync(
async () =>
// @ts-expect-error bad extractable
// The extractable and usages values are invalid here also,
// but the unrecognized algorithm name should be caught first.
await subtle.generateKey(algorithm, 7, []),
errorText,
);
});
}
const invalidAlgoTests = [
'AES',
{ name: 'AES' },
{ name: 'AES-CMAC' },
{ name: 'AES-CFB' },
{ name: 'HMAC', hash: 'MD5' },
{
name: 'RSA',
hash: 'SHA-256',
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
},
{
name: 'RSA-PSS',
hash: 'SHA',
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
},
{
name: 'EC',
namedCurve: 'P521',
},
];
invalidAlgoTests.map(testInvalidAlgorithm);
// Test bad usages
async function testBadUsage(name: string) {
test(SUITE, `bad usages: ${name}`, async () => {
await assertThrowsAsync(
async () =>
await subtle.generateKey(
{
name: name as AnyAlgorithm,
...vectors[name]?.algorithm,
},
true,
[],
),
'Usages cannot be empty',
);
// For CryptoKeyPair results the private key
// usages must not be empty.
// - ECDH(-like) algorithm key pairs only have private key usages
// - Signing algorithm key pairs may pass a non-empty array but
// with only a public key usage
if (
vectors[name]?.result === 'CryptoKeyPair' &&
vectors[name]?.usages.includes('verify')
) {
await assertThrowsAsync(
async () =>
await subtle.generateKey(
{
name: name as AnyAlgorithm,
...vectors[name]?.algorithm,
},
true,
['verify'],
),
'Usages cannot be empty',
);
}
const invalidUsages: KeyUsage[] = [];
allUsages.forEach(usage => {
if (!vectors[name]?.usages.includes(usage)) {
invalidUsages.push(usage);
}
});
for (const invalidUsage of invalidUsages) {
await assertThrowsAsync(
async () =>
await subtle.generateKey(
{
name: name as AnyAlgorithm,
...vectors[name]?.algorithm,
},
true,
[...(vectors[name]?.usages as KeyUsage[]), invalidUsage],
),
'Unsupported key usage',
);
}
});
}
const badUsageTests = Object.keys(vectors);
badUsageTests.map(testBadUsage);
// Test EC Key Generation
async function testECKeyGen(
name: AnyAlgorithm,
namedCurve: NamedCurve,
privateUsages: KeyUsage[],
publicUsages: KeyUsage[] = privateUsages,
) {
test(
SUITE,
`EC keygen: ${name} ${namedCurve} ${privateUsages} ${publicUsages}`,
async () => {
let usages = privateUsages;
if (publicUsages !== privateUsages) {
usages = usages.concat(publicUsages);
}
const pair = await subtle.generateKey(
{
name,
namedCurve,
},
true,
usages,
);
const { publicKey, privateKey } = pair as TestCryptoKeyPair;
const pub = publicKey;
const priv = privateKey;
expect(pub !== undefined);
expect(priv !== undefined);
expect(pub instanceof Object);
expect(priv instanceof Object);
expect(pub.type).to.equal('public');
expect(priv.type).to.equal('private');
expect(pub.keyExtractable).to.equal(true);
expect(priv.keyExtractable).to.equal(true);
expect(pub.keyUsages).to.deep.equal(publicUsages);
expect(priv.keyUsages).to.deep.equal(privateUsages);
expect(pub.algorithm.name, name);
expect(priv.algorithm.name, name);
expect(pub.algorithm.namedCurve, namedCurve);
expect(priv.algorithm.namedCurve, namedCurve);
// Invalid parameters
[1, true, {}, [], null].forEach(async curve => {
await assertThrowsAsync(
async () =>
await subtle.generateKey(
// @ts-expect-error bad named curve
{ name, namedCurve: curve },
true,
privateUsages,
),
'NotSupportedError',
);
});
// Strict WebIDL normalization (#1025): an explicit `undefined` is
// indistinguishable from omission, so EcKeyGenParams.namedCurve is
// reported as missing rather than as an unrecognized curve.
await assertThrowsAsync(
async () =>
subtle.generateKey(
{ name, namedCurve: undefined },
true,
privateUsages,
),
"'namedCurve' is required",
);
},
);
}
testECKeyGen('ECDSA', 'P-384', ['sign'], ['verify']);
testECKeyGen('ECDSA', 'P-521', ['sign'], ['verify']);
testECKeyGen('ECDH', 'P-384', ['deriveKey', 'deriveBits'], []);
testECKeyGen('ECDH', 'P-521', ['deriveKey', 'deriveBits'], []);
// Test RSA Key Generation
async function testRSAKeyGen(
name: 'RSASSA-PKCS1-v1_5' | 'RSA-PSS' | 'RSA-OAEP',
modulusLength: number,
publicExponent: Uint8Array,
hash: string,
privateUsages: KeyUsage[],
publicUsages: KeyUsage[] = privateUsages,
) {
test(
SUITE,
`RSA keygen: ${name} ${modulusLength} ${hash} ${privateUsages} ${publicUsages}`,
async () => {
let usages = privateUsages;
if (publicUsages !== privateUsages) {
usages = usages.concat(publicUsages);
}
const keyPair = await subtle.generateKey(
{
name,
modulusLength,
publicExponent,
hash,
} as any,
true,
usages,
);
const { publicKey, privateKey } = keyPair as TestCryptoKeyPair;
expect(publicKey !== undefined);
expect(privateKey !== undefined);
expect(publicKey instanceof Object);
expect(privateKey instanceof Object);
expect(publicKey.type).to.equal('public');
expect(privateKey.type).to.equal('private');
expect(publicKey.extractable).to.equal(true);
expect(privateKey.extractable).to.equal(true);
expect(publicKey.usages).to.deep.equal(publicUsages);
expect(privateKey.usages).to.deep.equal(privateUsages);
expect(publicKey.algorithm.name).to.equal(name);
expect(privateKey.algorithm.name).to.equal(name);
expect((publicKey.algorithm as any).modulusLength).to.equal(
modulusLength,
);
expect((privateKey.algorithm as any).modulusLength).to.equal(
modulusLength,
);
expect((publicKey.algorithm as any).publicExponent).to.deep.equal(
publicExponent,
);
expect((privateKey.algorithm as any).publicExponent).to.deep.equal(
publicExponent,
);
expect((publicKey.algorithm as any).hash.name).to.equal(hash);
expect((privateKey.algorithm as any).hash.name).to.equal(hash);
// Test invalid usage
await assertThrowsAsync(
async () =>
subtle.generateKey(
{ name, modulusLength, publicExponent, hash } as any,
true,
name === 'RSA-OAEP' ? ['sign'] : ['encrypt'],
),
`Unsupported key usage for a ${name} key`,
);
// Test invalid modulus length (Phase 3.3: must be ≥ 2048 bits)
await assertThrowsAsync(
async () =>
subtle.generateKey(
{ name, modulusLength: 0, publicExponent, hash } as any,
true,
usages,
),
'RSA modulusLength must be at least 2048 bits',
);
// Phase 3.3: 1024-bit RSA is below modern minimums and must be rejected.
await assertThrowsAsync(
async () =>
subtle.generateKey(
{ name, modulusLength: 1024, publicExponent, hash } as any,
true,
usages,
),
'RSA modulusLength must be at least 2048 bits',
);
},
);
}
testRSAKeyGen(
'RSASSA-PKCS1-v1_5',
2048,
new Uint8Array([1, 0, 1]),
'SHA-256',
['sign'],
['verify'],
);
testRSAKeyGen(
'RSA-PSS',
2048,
new Uint8Array([1, 0, 1]),
'SHA-512',
['sign'],
['verify'],
);
testRSAKeyGen(
'RSA-OAEP',
2048,
new Uint8Array([3]),
'SHA-384',
['decrypt', 'unwrapKey'],
['encrypt', 'wrapKey'],
);
// --- X25519/X448 Key Generation Tests (from subtle.cfrg suite) ---
test(
SUITE,
'X25519 - generateKey, exportKey, importKey, deriveBits',
async () => {
const format = 'raw';
const algorithm = { name: 'X25519' } as const;
const aliceKeys = (await subtle.generateKey(algorithm, true, [
'deriveKey',
'deriveBits',
])) as TestCryptoKeyPair;
const bobKeys = (await subtle.generateKey(algorithm, true, [
'deriveKey',
'deriveBits',
])) as TestCryptoKeyPair;
expect(aliceKeys.publicKey.algorithm.name).to.equal('X25519');
expect(aliceKeys.privateKey.algorithm.name).to.equal('X25519');
const alicePubRaw = await subtle.exportKey(format, aliceKeys.publicKey);
const bobPubRaw = await subtle.exportKey(format, bobKeys.publicKey);
const alicePubImported = await subtle.importKey(
format,
alicePubRaw,
algorithm,
true,
[],
);
const bobPubImported = await subtle.importKey(
format,
bobPubRaw,
algorithm,
true,
[],
);
const bitsLength = 256;
const aliceShared = await subtle.deriveBits(
{ name: 'X25519', public: bobPubImported } as any,
aliceKeys.privateKey,
bitsLength,
);
const bobShared = await subtle.deriveBits(
{ name: 'X25519', public: alicePubImported } as any,
bobKeys.privateKey,
bitsLength,
);
const aliceSharedView = new Uint8Array(aliceShared);
const bobSharedView = new Uint8Array(bobShared);
expect(aliceSharedView.length).to.equal(bitsLength / 8);
expect(aliceSharedView).to.deep.equal(bobSharedView);
},
);
test(
SUITE,
'X448 - generateKey, exportKey, importKey, deriveBits',
async () => {
const format = 'spki';
const algorithm = { name: 'X448' } as const;
const aliceKeys = (await subtle.generateKey(algorithm, true, [
'deriveKey',
'deriveBits',
])) as TestCryptoKeyPair;
const bobKeys = (await subtle.generateKey(algorithm, true, [
'deriveKey',
'deriveBits',
])) as TestCryptoKeyPair;
expect(aliceKeys.publicKey.algorithm.name).to.equal('X448');
expect(aliceKeys.privateKey.algorithm.name).to.equal('X448');
const alicePubSpki = await subtle.exportKey(format, aliceKeys.publicKey);
const bobPubSpki = await subtle.exportKey(format, bobKeys.publicKey);
const alicePubImported = await subtle.importKey(
format,
alicePubSpki,
algorithm,
true,
[],
);
const bobPubImported = await subtle.importKey(
format,
bobPubSpki,
algorithm,
true,
[],
);
const bitsLength = 448;
const aliceShared = await subtle.deriveBits(
{ name: 'X448', public: bobPubImported } as any,
aliceKeys.privateKey,
bitsLength,
);
const bobShared = await subtle.deriveBits(
{ name: 'X448', public: alicePubImported } as any,
bobKeys.privateKey,
bitsLength,
);
const aliceSharedView = new Uint8Array(aliceShared);
const bobSharedView = new Uint8Array(bobShared);
expect(aliceSharedView.length).to.equal(56);
expect(aliceSharedView).to.deep.equal(bobSharedView);
},
);
// --- ML-DSA Key Generation Tests ---
type MlDsaVariant = 'ML-DSA-44' | 'ML-DSA-65' | 'ML-DSA-87';
const MLDSA_VARIANTS: MlDsaVariant[] = ['ML-DSA-44', 'ML-DSA-65', 'ML-DSA-87'];
interface TestCryptoKeyPairMlDsa {
publicKey: CryptoKey;
privateKey: CryptoKey;
}
for (const variant of MLDSA_VARIANTS) {
test(SUITE, `ML-DSA keygen: ${variant}`, async () => {
const keyPair = await subtle.generateKey({ name: variant }, true, [
'sign',
'verify',
]);
const { publicKey, privateKey } = keyPair as TestCryptoKeyPairMlDsa;
expect(publicKey !== undefined);
expect(privateKey !== undefined);
expect(publicKey instanceof Object);
expect(privateKey instanceof Object);
expect(publicKey.type).to.equal('public');
expect(privateKey.type).to.equal('private');
expect(publicKey.extractable).to.equal(true);
expect(privateKey.extractable).to.equal(true);
expect(publicKey.usages).to.deep.equal(['verify']);
expect(privateKey.usages).to.deep.equal(['sign']);
expect(publicKey.algorithm.name).to.equal(variant);
expect(privateKey.algorithm.name).to.equal(variant);
});
test(SUITE, `ML-DSA keygen non-extractable: ${variant}`, async () => {
const keyPair = await subtle.generateKey({ name: variant }, false, [
'sign',
'verify',
]);
const { publicKey, privateKey } = keyPair as TestCryptoKeyPairMlDsa;
// Public key is always extractable
expect(publicKey.extractable).to.equal(true);
// Private key respects extractable parameter
expect(privateKey.extractable).to.equal(false);
});
}
// Test bad usages for ML-DSA
test(SUITE, 'ML-DSA bad usages', async () => {
await assertThrowsAsync(
async () => await subtle.generateKey({ name: 'ML-DSA-44' }, true, []),
'Usages cannot be empty',
);
await assertThrowsAsync(
async () =>
await subtle.generateKey({ name: 'ML-DSA-44' }, true, ['encrypt']),
'Unsupported key usage',
);
});
// --- ML-KEM Key Generation Tests ---
for (const variant of MLKEM_VARIANTS) {
test(SUITE, `ML-KEM keygen: ${variant}`, async () => {
const keyPair = await subtle.generateKey({ name: variant }, true, [
'encapsulateBits',
'decapsulateBits',
]);
const { publicKey, privateKey } = keyPair as TestCryptoKeyPair;
expect(publicKey.type).to.equal('public');
expect(privateKey.type).to.equal('private');
expect(publicKey.algorithm.name).to.equal(variant);
expect(privateKey.algorithm.name).to.equal(variant);
expect(publicKey.extractable).to.equal(true);
});
test(SUITE, `ML-KEM keygen non-extractable: ${variant}`, async () => {
const keyPair = await subtle.generateKey({ name: variant }, false, [
'encapsulateBits',
'decapsulateBits',
]);
const { publicKey, privateKey } = keyPair as TestCryptoKeyPair;
expect(publicKey.extractable).to.equal(true);
expect(privateKey.extractable).to.equal(false);
});
}
test(SUITE, 'ML-KEM bad usages', async () => {
await assertThrowsAsync(
async () => await subtle.generateKey({ name: 'ML-KEM-768' }, true, []),
'Usages cannot be empty',
);
await assertThrowsAsync(
async () =>
await subtle.generateKey({ name: 'ML-KEM-768' }, true, [
'sign',
] as KeyUsage[]),
'Unsupported key usage',
);
});
/*
// Test AES Key Generation
type AESArgs = [AESAlgorithm, AESLength, KeyUsage[]];
async function testAesKeyGen(args: AESArgs) {
const [name, length, usages] = args;
test(SUITE, `AES keygen: ${name} ${length} ${usages}`, async () => {
const key = await subtle.generateKey({ name, length }, true, usages);
const k = key as CryptoKey;
expect(k !== undefined);
expect(k instanceof Object);
expect(k.type).to.equal('secret');
expect(k.extractable).to.equal(true);
expect(k.usages).to.deep.equal(usages);
expect(k.algorithm.name).to.equal(name);
expect(k.algorithm.length).to.equal(length);
// Invalid parameters
[1, 100, 257, '', false, null, undefined].forEach(async invalidParam => {
await assertThrowsAsync(
async () =>
subtle.generateKey(
// @ts-expect-error bad length
{ name, length: invalidParam },
true,
usages,
),
'AES key length must be 128, 192, or 256 bits',
);
});
});
}
const aesTests: AESArgs[] = [
['AES-CTR', 128, ['encrypt', 'decrypt', 'wrapKey']],
['AES-CTR', 256, ['encrypt', 'decrypt', 'unwrapKey']],
['AES-CBC', 128, ['encrypt', 'decrypt']],
['AES-CBC', 256, ['encrypt', 'decrypt']],
['AES-GCM', 128, ['encrypt', 'decrypt']],
['AES-GCM', 256, ['encrypt', 'decrypt']],
['AES-KW', 128, ['wrapKey', 'unwrapKey']],
['AES-KW', 256, ['wrapKey', 'unwrapKey']],
];
aesTests.map(args => testAesKeyGen(args));
*/
/*
// Test HMAC Key Generation
{
async function test(length, hash, usages) {
const key = await subtle.generateKey(
{
name: 'HMAC',
length,
hash,
},
true,
usages
);
if (length === undefined) {
switch (hash) {
case 'SHA-1':
length = 512;
break;
case 'SHA-256':
length = 512;
break;
case 'SHA-384':
length = 1024;
break;
case 'SHA-512':
length = 1024;
break;
}
}
assert(key);
assert(isCryptoKey(key));
assert.strictEqual(key.type, 'secret');
assert.strictEqual(key.toString(), '[object CryptoKey]');
assert.strictEqual(key.extractable, true);
assert.deepStrictEqual(key.usages, usages);
assert.strictEqual(key.algorithm.name, 'HMAC');
assert.strictEqual(key.algorithm.length, length);
assert.strictEqual(key.algorithm.hash.name, hash);
[1, false, null].forEach(async (hash) => {
await assert.rejects(
subtle.generateKey({ name: 'HMAC', length, hash }, true, usages),
{
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
}
);
});
}
const kTests = [
[undefined, 'SHA-1', ['sign', 'verify']],
[undefined, 'SHA-256', ['sign', 'verify']],
[undefined, 'SHA-384', ['sign', 'verify']],
[undefined, 'SHA-512', ['sign', 'verify']],
[128, 'SHA-256', ['sign', 'verify']],
[1024, 'SHA-512', ['sign', 'verify']],
];
const tests = Promise.all(kTests.map((args) => test(...args)));
tests.then(common.mustCall());
}
// End user code cannot create CryptoKey directly
assert.throws(() => new CryptoKey(), { code: 'ERR_ILLEGAL_CONSTRUCTOR' });
{
const buffer = Buffer.from('Hello World');
const keyObject = createSecretKey(buffer);
assert(!isCryptoKey(buffer));
assert(!isCryptoKey(keyObject));
}
// Test OKP Key Generation
{
async function test(name, privateUsages, publicUsages = privateUsages) {
let usages = privateUsages;
if (publicUsages !== privateUsages) usages = usages.concat(publicUsages);
const { publicKey, privateKey } = await subtle.generateKey(
{
name,
},
true,
usages
);
assert(publicKey);
assert(privateKey);
assert(isCryptoKey(publicKey));
assert(isCryptoKey(privateKey));
assert.strictEqual(publicKey.type, 'public');
assert.strictEqual(privateKey.type, 'private');
assert.strictEqual(publicKey.toString(), '[object CryptoKey]');
assert.strictEqual(privateKey.toString(), '[object CryptoKey]');
assert.strictEqual(publicKey.extractable, true);
assert.strictEqual(privateKey.extractable, true);
assert.deepStrictEqual(publicKey.usages, publicUsages);
assert.deepStrictEqual(privateKey.usages, privateUsages);
assert.strictEqual(publicKey.algorithm.name, name);
assert.strictEqual(privateKey.algorithm.name, name);
}
const kTests = [
['Ed25519', ['sign'], ['verify']],
['Ed448', ['sign'], ['verify']],
['X25519', ['deriveKey', 'deriveBits'], []],
['X448', ['deriveKey', 'deriveBits'], []],
];
const tests = kTests.map((args) => test(...args));
// Test bad parameters
Promise.all(tests).then(common.mustCall());
}
*/