|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common.js'); |
| 4 | +const { hasOpenSSL, hasFIPS, isBoringSSL } = require('../../test/common/crypto.js'); |
| 5 | +const fixtures = require('../../test/common/fixtures.js'); |
| 6 | +const { createPrivateKey, createPublicKey } = require('crypto'); |
| 7 | + |
| 8 | +const keys = { |
| 9 | + 'rsa': 'rsa_private_2048', |
| 10 | + 'rsa-pss': 'rsa_pss_private_2048', |
| 11 | + 'p-256': 'ec_p256_private', |
| 12 | + 'p-384': 'ec_p384_private', |
| 13 | + 'p-521': 'ec_p521_private', |
| 14 | + 'ed25519': 'ed25519_private', |
| 15 | + 'x25519': 'x25519_private', |
| 16 | +}; |
| 17 | +if (!isBoringSSL) { |
| 18 | + keys.ed448 = 'ed448_private'; |
| 19 | + keys.x448 = 'x448_private'; |
| 20 | + if (!hasFIPS()) keys['rsa-multiprime'] = 'rsa_private_2048_3_primes'; |
| 21 | +} |
| 22 | +if (hasOpenSSL(3, 5) || isBoringSSL) { |
| 23 | + keys['ml-dsa-44'] = 'ml_dsa_44_private_seed_only'; |
| 24 | + keys['ml-dsa-87'] = 'ml_dsa_87_private_seed_only'; |
| 25 | + keys['ml-kem-768'] = 'ml_kem_768_private_seed_only'; |
| 26 | + keys['ml-kem-1024'] = 'ml_kem_1024_private_seed_only'; |
| 27 | +} |
| 28 | +if (hasOpenSSL(3, 5)) { |
| 29 | + keys['slh-dsa-sha2-128s'] = 'slh_dsa_sha2_128s_private'; |
| 30 | + keys['slh-dsa-shake-256s'] = 'slh_dsa_shake_256s_private'; |
| 31 | +} |
| 32 | + |
| 33 | +const bench = common.createBenchmark(main, { |
| 34 | + // Keep one size per family by default; other fixtures remain available via keyType. |
| 35 | + keyType: ['rsa', 'rsa-pss', 'p-256', 'ed25519', 'x25519', |
| 36 | + 'ml-dsa-44', 'ml-kem-768', 'slh-dsa-sha2-128s'].filter((type) => keys[type]), |
| 37 | + operation: ['import', 'export'], |
| 38 | + // PEM can be selected with format=pem; DER covers ASN.1 encoding by default. |
| 39 | + format: ['jwk', 'der', 'raw-public', 'raw-private', 'raw-seed'], |
| 40 | + type: ['public', 'private'], |
| 41 | + n: [1e4], |
| 42 | +}, { |
| 43 | + combinationFilter({ keyType, format, type }) { |
| 44 | + if (format === 'jwk') return keyType !== 'rsa-pss'; |
| 45 | + if (!format.startsWith('raw-')) return true; |
| 46 | + if (keyType.startsWith('rsa')) return false; |
| 47 | + if (format === 'raw-public') return type === 'public'; |
| 48 | + if (format === 'raw-private') return type === 'private' && !keyType.startsWith('ml-'); |
| 49 | + return type === 'private' && keyType.startsWith('ml-'); |
| 50 | + }, |
| 51 | +}); |
| 52 | + |
| 53 | +function main({ keyType, operation, format, type, n }) { |
| 54 | + const privateKey = createPrivateKey(fixtures.readKey(`${keys[keyType]}.pem`)); |
| 55 | + const key = type === 'private' ? privateKey : createPublicKey(privateKey); |
| 56 | + const options = { format }; |
| 57 | + if (format === 'pem' || format === 'der') { |
| 58 | + options.type = type === 'private' ? 'pkcs8' : 'spki'; |
| 59 | + } |
| 60 | + let run; |
| 61 | + if (operation === 'export') { |
| 62 | + run = () => key.export(options); |
| 63 | + } else { |
| 64 | + const input = { ...options, key: key.export(options) }; |
| 65 | + if (format.startsWith('raw-')) { |
| 66 | + input.asymmetricKeyType = key.asymmetricKeyType; |
| 67 | + if (input.asymmetricKeyType === 'ec') { |
| 68 | + input.namedCurve = key.asymmetricKeyDetails.namedCurve; |
| 69 | + } |
| 70 | + } |
| 71 | + const importKey = type === 'private' ? createPrivateKey : createPublicKey; |
| 72 | + run = () => importKey(input); |
| 73 | + } |
| 74 | + // Resolve provider operations and warm the JS path before timing. |
| 75 | + for (let i = 0; i < 100; i++) run(); |
| 76 | + bench.start(); |
| 77 | + for (let i = 0; i < n; i++) run(); |
| 78 | + bench.end(n); |
| 79 | +} |
0 commit comments