|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common.js'); |
| 4 | +const { hasOpenSSL } = require('../../test/common/crypto.js'); |
| 5 | +const assert = require('node:assert'); |
| 6 | +const { |
| 7 | + createHmac, |
| 8 | + createMac, |
| 9 | + getMacs, |
| 10 | +} = require('node:crypto'); |
| 11 | + |
| 12 | +if (!hasOpenSSL(3) || |
| 13 | + process.features.openssl_is_boringssl || |
| 14 | + typeof createMac !== 'function' || |
| 15 | + typeof getMacs !== 'function') { |
| 16 | + console.log('Skipping: generic MAC API requires OpenSSL >= 3'); |
| 17 | + process.exit(0); |
| 18 | +} |
| 19 | + |
| 20 | +const operations = [ |
| 21 | + 'get-macs-cold', |
| 22 | + 'get-macs-warm', |
| 23 | + 'create-cold', |
| 24 | + 'create-warm', |
| 25 | + 'hmac-lifecycle', |
| 26 | + 'mac-lifecycle', |
| 27 | + 'mac-stream-lifecycle', |
| 28 | + 'update', |
| 29 | + 'stream', |
| 30 | + 'final-buffer', |
| 31 | + 'final-hex', |
| 32 | +]; |
| 33 | +const configurations = { |
| 34 | + 'hmac-sha256': { |
| 35 | + algorithm: 'HMAC', |
| 36 | + key: Buffer.alloc(32, 0x42), |
| 37 | + options: { digest: 'SHA256' }, |
| 38 | + }, |
| 39 | + 'kmac-128': { |
| 40 | + algorithm: 'KMAC-128', |
| 41 | + key: Buffer.alloc(32, 0x42), |
| 42 | + options: { outputLength: 32 }, |
| 43 | + }, |
| 44 | +}; |
| 45 | + |
| 46 | +const bench = common.createBenchmark(main, { |
| 47 | + operation: operations, |
| 48 | + algorithm: Object.keys(configurations), |
| 49 | + length: [0, 64, 4096], |
| 50 | + n: [1, 10_000, 20_000, 500_000], |
| 51 | +}, { |
| 52 | + combinationFilter({ operation, algorithm, length, n }) { |
| 53 | + if (operation === 'get-macs-cold') { |
| 54 | + return algorithm === 'hmac-sha256' && length === 0 && n === 1; |
| 55 | + } |
| 56 | + if (operation === 'get-macs-warm') { |
| 57 | + return algorithm === 'hmac-sha256' && length === 0 && n === 500_000; |
| 58 | + } |
| 59 | + if (operation === 'create-cold') |
| 60 | + return length === 0 && n === 1; |
| 61 | + if (operation === 'create-warm') |
| 62 | + return length === 0 && n === 20_000; |
| 63 | + if (operation === 'hmac-lifecycle') { |
| 64 | + return algorithm === 'hmac-sha256' && n === 10_000; |
| 65 | + } |
| 66 | + if (operation === 'mac-lifecycle' || |
| 67 | + operation === 'mac-stream-lifecycle') { |
| 68 | + return n === 10_000; |
| 69 | + } |
| 70 | + if (operation === 'update' || operation === 'stream') { |
| 71 | + return length === 64 && n === 500_000; |
| 72 | + } |
| 73 | + if (operation === 'final-buffer' || operation === 'final-hex') { |
| 74 | + return algorithm === 'hmac-sha256' && |
| 75 | + length === 64 && |
| 76 | + n === 20_000; |
| 77 | + } |
| 78 | + return false; |
| 79 | + }, |
| 80 | + test: { |
| 81 | + operation: ['create-cold'], |
| 82 | + algorithm: ['hmac-sha256'], |
| 83 | + length: [0], |
| 84 | + n: [1], |
| 85 | + }, |
| 86 | +}); |
| 87 | + |
| 88 | +function main({ operation, algorithm, length, n }) { |
| 89 | + const configuration = configurations[algorithm]; |
| 90 | + const data = Buffer.alloc(length, 0x61); |
| 91 | + |
| 92 | + switch (operation) { |
| 93 | + case 'get-macs-cold': |
| 94 | + measureGetMacs(n, false); |
| 95 | + break; |
| 96 | + case 'get-macs-warm': |
| 97 | + measureGetMacs(n, true); |
| 98 | + break; |
| 99 | + case 'create-cold': |
| 100 | + measureCreate(configuration, n, false); |
| 101 | + break; |
| 102 | + case 'create-warm': |
| 103 | + measureCreate(configuration, n, true); |
| 104 | + break; |
| 105 | + case 'hmac-lifecycle': |
| 106 | + measureHmacLifecycle(configuration, data, n); |
| 107 | + break; |
| 108 | + case 'mac-lifecycle': |
| 109 | + measureMacLifecycle(configuration, data, n); |
| 110 | + break; |
| 111 | + case 'mac-stream-lifecycle': |
| 112 | + measureMacStreamLifecycle(configuration, data, n); |
| 113 | + break; |
| 114 | + case 'update': |
| 115 | + measureUpdate(configuration, data, n); |
| 116 | + break; |
| 117 | + case 'stream': |
| 118 | + measureStream(configuration, data, n); |
| 119 | + break; |
| 120 | + case 'final-buffer': |
| 121 | + measureFinal(configuration, data, n); |
| 122 | + break; |
| 123 | + case 'final-hex': |
| 124 | + measureFinal(configuration, data, n, 'hex'); |
| 125 | + break; |
| 126 | + default: |
| 127 | + throw new Error(`unknown operation: ${operation}`); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +function measureGetMacs(n, warm) { |
| 132 | + if (warm) |
| 133 | + getMacs(); |
| 134 | + |
| 135 | + let result; |
| 136 | + bench.start(); |
| 137 | + for (let i = 0; i < n; ++i) |
| 138 | + result = getMacs(); |
| 139 | + bench.end(n); |
| 140 | + |
| 141 | + assert(Array.isArray(result)); |
| 142 | +} |
| 143 | + |
| 144 | +function measureCreate({ algorithm, key, options }, n, warm) { |
| 145 | + if (warm) |
| 146 | + createMac(algorithm, key, options).final(); |
| 147 | + |
| 148 | + const contexts = new Array(n); |
| 149 | + bench.start(); |
| 150 | + for (let i = 0; i < n; ++i) |
| 151 | + contexts[i] = createMac(algorithm, key, options); |
| 152 | + bench.end(n); |
| 153 | + |
| 154 | + assert.strictEqual(typeof contexts[n - 1], 'object'); |
| 155 | +} |
| 156 | + |
| 157 | +function measureHmacLifecycle({ key, options }, data, n) { |
| 158 | + createHmac(options.digest, key).update(data).digest(); |
| 159 | + |
| 160 | + let result; |
| 161 | + bench.start(); |
| 162 | + for (let i = 0; i < n; ++i) |
| 163 | + result = createHmac(options.digest, key).update(data).digest(); |
| 164 | + bench.end(n); |
| 165 | + |
| 166 | + assert(Buffer.isBuffer(result)); |
| 167 | +} |
| 168 | + |
| 169 | +function measureMacLifecycle({ algorithm, key, options }, data, n) { |
| 170 | + createMac(algorithm, key, options).update(data).final(); |
| 171 | + |
| 172 | + let result; |
| 173 | + bench.start(); |
| 174 | + for (let i = 0; i < n; ++i) |
| 175 | + result = createMac(algorithm, key, options).update(data).final(); |
| 176 | + bench.end(n); |
| 177 | + |
| 178 | + assert(Buffer.isBuffer(result)); |
| 179 | +} |
| 180 | + |
| 181 | +function measureMacStreamLifecycle({ algorithm, key, options }, data, n) { |
| 182 | + const warmup = createMac(algorithm, key, options); |
| 183 | + warmup.end(data); |
| 184 | + warmup.read(); |
| 185 | + |
| 186 | + let result; |
| 187 | + bench.start(); |
| 188 | + for (let i = 0; i < n; ++i) { |
| 189 | + const context = createMac(algorithm, key, options); |
| 190 | + context.end(data); |
| 191 | + result = context.read(); |
| 192 | + } |
| 193 | + bench.end(n); |
| 194 | + |
| 195 | + assert(Buffer.isBuffer(result)); |
| 196 | +} |
| 197 | + |
| 198 | +function measureUpdate({ algorithm, key, options }, data, n) { |
| 199 | + const warmup = createMac(algorithm, key, options); |
| 200 | + warmup.update(data).final(); |
| 201 | + |
| 202 | + const context = createMac(algorithm, key, options); |
| 203 | + bench.start(); |
| 204 | + for (let i = 0; i < n; ++i) |
| 205 | + context.update(data); |
| 206 | + bench.end(n); |
| 207 | + |
| 208 | + assert(Buffer.isBuffer(context.final())); |
| 209 | +} |
| 210 | + |
| 211 | +function measureStream({ algorithm, key, options }, data, n) { |
| 212 | + const warmup = createMac(algorithm, key, options); |
| 213 | + warmup.end(data); |
| 214 | + warmup.read(); |
| 215 | + |
| 216 | + const context = createMac(algorithm, key, options); |
| 217 | + bench.start(); |
| 218 | + for (let i = 0; i < n; ++i) |
| 219 | + context.write(data); |
| 220 | + bench.end(n); |
| 221 | + |
| 222 | + context.end(); |
| 223 | + assert(Buffer.isBuffer(context.read())); |
| 224 | +} |
| 225 | + |
| 226 | +function measureFinal({ algorithm, key, options }, data, n, encoding) { |
| 227 | + const warmup = createMac(algorithm, key, options).update(data); |
| 228 | + if (encoding === undefined) |
| 229 | + warmup.final(); |
| 230 | + else |
| 231 | + warmup.final(encoding); |
| 232 | + |
| 233 | + const contexts = new Array(n); |
| 234 | + for (let i = 0; i < n; ++i) |
| 235 | + contexts[i] = createMac(algorithm, key, options).update(data); |
| 236 | + |
| 237 | + let result; |
| 238 | + if (encoding === undefined) { |
| 239 | + bench.start(); |
| 240 | + for (let i = 0; i < n; ++i) |
| 241 | + result = contexts[i].final(); |
| 242 | + bench.end(n); |
| 243 | + } else { |
| 244 | + bench.start(); |
| 245 | + for (let i = 0; i < n; ++i) |
| 246 | + result = contexts[i].final(encoding); |
| 247 | + bench.end(n); |
| 248 | + } |
| 249 | + |
| 250 | + if (encoding === undefined) |
| 251 | + assert(Buffer.isBuffer(result)); |
| 252 | + else |
| 253 | + assert.strictEqual(typeof result, 'string'); |
| 254 | +} |
0 commit comments