forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate-keyobject.js
More file actions
127 lines (117 loc) · 3.88 KB
/
create-keyobject.js
File metadata and controls
127 lines (117 loc) · 3.88 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
'use strict';
const common = require('../common.js');
const { hasOpenSSL } = require('../../test/common/crypto.js');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const fixtures_keydir = path.resolve(__dirname, '../../test/fixtures/keys/');
function readKey(name) {
return fs.readFileSync(`${fixtures_keydir}/${name}.pem`, 'utf8');
}
function readKeyPair(publicKeyName, privateKeyName) {
return {
publicKey: readKey(publicKeyName),
privateKey: readKey(privateKeyName),
};
}
const keyFixtures = {
'ec': readKeyPair('ec_p256_public', 'ec_p256_private'),
'rsa': readKeyPair('rsa_public_2048', 'rsa_private_2048'),
'ed25519': readKeyPair('ed25519_public', 'ed25519_private'),
};
if (hasOpenSSL(3, 5)) {
keyFixtures['ml-dsa-44'] = readKeyPair('ml_dsa_44_public', 'ml_dsa_44_private');
}
const bench = common.createBenchmark(main, {
keyType: Object.keys(keyFixtures),
keyFormat: ['pkcs8', 'spki', 'der-pkcs8', 'der-spki', 'jwk-public', 'jwk-private',
'raw-public', 'raw-private', 'raw-seed'],
n: [1e3],
}, {
combinationFilter(p) {
// raw-private is not supported for rsa and ml-dsa
if (p.keyFormat === 'raw-private')
return p.keyType !== 'rsa' && !p.keyType.startsWith('ml-');
// raw-public is not supported by rsa
if (p.keyFormat === 'raw-public')
return p.keyType !== 'rsa';
// raw-seed is only supported for ml-dsa
if (p.keyFormat === 'raw-seed')
return p.keyType.startsWith('ml-');
return true;
},
});
function measure(n, fn, input) {
bench.start();
for (let i = 0; i < n; ++i) {
fn(input);
}
bench.end(n);
}
function main({ n, keyFormat, keyType }) {
const keyPair = {
publicKey: crypto.createPublicKey(keyFixtures[keyType].publicKey),
privateKey: crypto.createPrivateKey(keyFixtures[keyType].privateKey),
};
let key, fn;
switch (keyFormat) {
case 'spki':
key = keyPair.publicKey.export({ format: 'pem', type: 'spki' });
fn = crypto.createPublicKey;
break;
case 'pkcs8':
key = keyPair.privateKey.export({ format: 'pem', type: 'pkcs8' });
fn = crypto.createPrivateKey;
break;
case 'der-spki': {
const options = { format: 'der', type: 'spki' };
key = { ...options, key: keyPair.publicKey.export(options) };
fn = crypto.createPublicKey;
break;
}
case 'der-pkcs8': {
const options = { format: 'der', type: 'pkcs8' };
key = { ...options, key: keyPair.privateKey.export(options) };
fn = crypto.createPrivateKey;
break;
}
case 'jwk-public': {
const options = { format: 'jwk' };
key = { ...options, key: keyPair.publicKey.export(options) };
fn = crypto.createPublicKey;
break;
}
case 'jwk-private': {
const options = { format: 'jwk' };
key = { ...options, key: keyPair.privateKey.export(options) };
fn = crypto.createPrivateKey;
break;
}
case 'raw-public': {
const exportedKey = keyPair.publicKey.export({ format: 'raw-public' });
key = { key: exportedKey, format: 'raw-public', asymmetricKeyType: keyType };
if (keyType === 'ec') key.namedCurve = keyPair.publicKey.asymmetricKeyDetails.namedCurve;
fn = crypto.createPublicKey;
break;
}
case 'raw-private': {
const exportedKey = keyPair.privateKey.export({ format: 'raw-private' });
key = { key: exportedKey, format: 'raw-private', asymmetricKeyType: keyType };
if (keyType === 'ec') key.namedCurve = keyPair.privateKey.asymmetricKeyDetails.namedCurve;
fn = crypto.createPrivateKey;
break;
}
case 'raw-seed': {
key = {
key: keyPair.privateKey.export({ format: 'raw-seed' }),
format: 'raw-seed',
asymmetricKeyType: keyType,
};
fn = crypto.createPrivateKey;
break;
}
default:
throw new Error('not implemented');
}
measure(n, fn, key);
}