11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common.js'); 41cb0ef41Sopenharmony_ciconst crypto = require('crypto'); 51cb0ef41Sopenharmony_ciconst fs = require('fs'); 61cb0ef41Sopenharmony_ciconst path = require('path'); 71cb0ef41Sopenharmony_ciconst fixtures_keydir = path.resolve(__dirname, '../../test/fixtures/keys/'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_cifunction readKey(name) { 101cb0ef41Sopenharmony_ci return fs.readFileSync(`${fixtures_keydir}/${name}.pem`, 'utf8'); 111cb0ef41Sopenharmony_ci} 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_cifunction readKeyPair(publicKeyName, privateKeyName) { 141cb0ef41Sopenharmony_ci return { 151cb0ef41Sopenharmony_ci publicKey: readKey(publicKeyName), 161cb0ef41Sopenharmony_ci privateKey: readKey(privateKeyName), 171cb0ef41Sopenharmony_ci }; 181cb0ef41Sopenharmony_ci} 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ciconst keyFixtures = { 211cb0ef41Sopenharmony_ci ec: readKeyPair('ec_p256_public', 'ec_p256_private'), 221cb0ef41Sopenharmony_ci rsa: readKeyPair('rsa_public_2048', 'rsa_private_2048'), 231cb0ef41Sopenharmony_ci ed25519: readKeyPair('ed25519_public', 'ed25519_private'), 241cb0ef41Sopenharmony_ci}; 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciconst data = crypto.randomBytes(256); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_cilet pems; 291cb0ef41Sopenharmony_cilet keyObjects; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 321cb0ef41Sopenharmony_ci keyType: ['rsa', 'ec', 'ed25519'], 331cb0ef41Sopenharmony_ci mode: ['sync', 'async', 'async-parallel'], 341cb0ef41Sopenharmony_ci keyFormat: ['pem', 'der', 'jwk', 'keyObject', 'keyObject.unique'], 351cb0ef41Sopenharmony_ci n: [1e3], 361cb0ef41Sopenharmony_ci}, { 371cb0ef41Sopenharmony_ci combinationFilter(p) { 381cb0ef41Sopenharmony_ci // "keyObject.unique" allows to compare the result with "keyObject" to 391cb0ef41Sopenharmony_ci // assess whether mutexes over the key material impact the operation 401cb0ef41Sopenharmony_ci return p.keyFormat !== 'keyObject.unique' || 411cb0ef41Sopenharmony_ci (p.keyFormat === 'keyObject.unique' && p.mode === 'async-parallel'); 421cb0ef41Sopenharmony_ci }, 431cb0ef41Sopenharmony_ci}); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_cifunction measureSync(n, digest, signature, publicKey, keys) { 461cb0ef41Sopenharmony_ci bench.start(); 471cb0ef41Sopenharmony_ci for (let i = 0; i < n; ++i) { 481cb0ef41Sopenharmony_ci crypto.verify( 491cb0ef41Sopenharmony_ci digest, 501cb0ef41Sopenharmony_ci data, 511cb0ef41Sopenharmony_ci publicKey || keys[i], 521cb0ef41Sopenharmony_ci signature); 531cb0ef41Sopenharmony_ci } 541cb0ef41Sopenharmony_ci bench.end(n); 551cb0ef41Sopenharmony_ci} 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_cifunction measureAsync(n, digest, signature, publicKey, keys) { 581cb0ef41Sopenharmony_ci let remaining = n; 591cb0ef41Sopenharmony_ci function done() { 601cb0ef41Sopenharmony_ci if (--remaining === 0) 611cb0ef41Sopenharmony_ci bench.end(n); 621cb0ef41Sopenharmony_ci else 631cb0ef41Sopenharmony_ci one(); 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci function one() { 671cb0ef41Sopenharmony_ci crypto.verify( 681cb0ef41Sopenharmony_ci digest, 691cb0ef41Sopenharmony_ci data, 701cb0ef41Sopenharmony_ci publicKey || keys[n - remaining], 711cb0ef41Sopenharmony_ci signature, 721cb0ef41Sopenharmony_ci done); 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci bench.start(); 751cb0ef41Sopenharmony_ci one(); 761cb0ef41Sopenharmony_ci} 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_cifunction measureAsyncParallel(n, digest, signature, publicKey, keys) { 791cb0ef41Sopenharmony_ci let remaining = n; 801cb0ef41Sopenharmony_ci function done() { 811cb0ef41Sopenharmony_ci if (--remaining === 0) 821cb0ef41Sopenharmony_ci bench.end(n); 831cb0ef41Sopenharmony_ci } 841cb0ef41Sopenharmony_ci bench.start(); 851cb0ef41Sopenharmony_ci for (let i = 0; i < n; ++i) { 861cb0ef41Sopenharmony_ci crypto.verify( 871cb0ef41Sopenharmony_ci digest, 881cb0ef41Sopenharmony_ci data, 891cb0ef41Sopenharmony_ci publicKey || keys[i], 901cb0ef41Sopenharmony_ci signature, 911cb0ef41Sopenharmony_ci done); 921cb0ef41Sopenharmony_ci } 931cb0ef41Sopenharmony_ci} 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_cifunction main({ n, mode, keyFormat, keyType }) { 961cb0ef41Sopenharmony_ci pems ||= [...Buffer.alloc(n)].map(() => keyFixtures[keyType].publicKey); 971cb0ef41Sopenharmony_ci keyObjects ||= pems.map(crypto.createPublicKey); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci let publicKey, keys, digest; 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci switch (keyType) { 1021cb0ef41Sopenharmony_ci case 'rsa': 1031cb0ef41Sopenharmony_ci case 'ec': 1041cb0ef41Sopenharmony_ci digest = 'sha256'; 1051cb0ef41Sopenharmony_ci break; 1061cb0ef41Sopenharmony_ci case 'ed25519': 1071cb0ef41Sopenharmony_ci break; 1081cb0ef41Sopenharmony_ci default: 1091cb0ef41Sopenharmony_ci throw new Error('not implemented'); 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci switch (keyFormat) { 1131cb0ef41Sopenharmony_ci case 'keyObject': 1141cb0ef41Sopenharmony_ci publicKey = keyObjects[0]; 1151cb0ef41Sopenharmony_ci break; 1161cb0ef41Sopenharmony_ci case 'pem': 1171cb0ef41Sopenharmony_ci publicKey = pems[0]; 1181cb0ef41Sopenharmony_ci break; 1191cb0ef41Sopenharmony_ci case 'jwk': { 1201cb0ef41Sopenharmony_ci publicKey = { key: keyObjects[0].export({ format: 'jwk' }), format: 'jwk' }; 1211cb0ef41Sopenharmony_ci break; 1221cb0ef41Sopenharmony_ci } 1231cb0ef41Sopenharmony_ci case 'der': { 1241cb0ef41Sopenharmony_ci publicKey = { key: keyObjects[0].export({ format: 'der', type: 'spki' }), format: 'der', type: 'spki' }; 1251cb0ef41Sopenharmony_ci break; 1261cb0ef41Sopenharmony_ci } 1271cb0ef41Sopenharmony_ci case 'keyObject.unique': 1281cb0ef41Sopenharmony_ci keys = keyObjects; 1291cb0ef41Sopenharmony_ci break; 1301cb0ef41Sopenharmony_ci default: 1311cb0ef41Sopenharmony_ci throw new Error('not implemented'); 1321cb0ef41Sopenharmony_ci } 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci const { privateKey } = keyFixtures[keyType]; 1361cb0ef41Sopenharmony_ci const signature = crypto.sign(digest, data, privateKey); 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci switch (mode) { 1391cb0ef41Sopenharmony_ci case 'sync': 1401cb0ef41Sopenharmony_ci measureSync(n, digest, signature, publicKey, keys); 1411cb0ef41Sopenharmony_ci break; 1421cb0ef41Sopenharmony_ci case 'async': 1431cb0ef41Sopenharmony_ci measureAsync(n, digest, signature, publicKey, keys); 1441cb0ef41Sopenharmony_ci break; 1451cb0ef41Sopenharmony_ci case 'async-parallel': 1461cb0ef41Sopenharmony_ci measureAsyncParallel(n, digest, signature, publicKey, keys); 1471cb0ef41Sopenharmony_ci break; 1481cb0ef41Sopenharmony_ci default: 1491cb0ef41Sopenharmony_ci throw new Error('not implemented'); 1501cb0ef41Sopenharmony_ci } 1511cb0ef41Sopenharmony_ci} 152