Skip to content

test: make test-crypto-timing-safe-equal-benchmarks robust #38493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions test/pummel/test-crypto-timing-safe-equal-benchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ if (!common.enoughTestMem)

const assert = require('assert');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const file1 = path.join(tmpdir.path, 'file1');
const file2 = path.join(tmpdir.path, 'file2');
const file3 = path.join(tmpdir.path, 'file3');

function runOneBenchmark(compareFunc, firstBufFill, secondBufFill, bufSize) {
return eval(`
Expand All @@ -27,23 +35,43 @@ function runOneBenchmark(compareFunc, firstBufFill, secondBufFill, bufSize) {
}

function getTValue(compareFunc) {
const numTrials = 1e5;
const numTrials = 1e4;
const bufSize = 10000;
// Perform benchmarks to verify that timingSafeEqual is actually timing-safe.

const rawEqualBenches = Array(numTrials);
const rawUnequalBenches = Array(numTrials);

for (let i = 0; i < numTrials; i++) {
const buf = crypto.randomUUID();
fs.writeFileSync(file1, buf);
fs.writeFileSync(file2, buf);
fs.writeFileSync(file3, crypto.randomUUID());
if (Math.random() < 0.5) {
// First benchmark: comparing two equal buffers
rawEqualBenches[i] = runOneBenchmark(compareFunc, 'A', 'A', bufSize);
rawEqualBenches[i] = runOneBenchmark(
compareFunc,
fs.readFileSync(file1, 'utf8'),
fs.readFileSync(file2, 'utf8'),
bufSize);
// Second benchmark: comparing two unequal buffers
rawUnequalBenches[i] = runOneBenchmark(compareFunc, 'B', 'C', bufSize);
rawUnequalBenches[i] = runOneBenchmark(
compareFunc,
fs.readFileSync(file1, 'utf8'),
fs.readFileSync(file3, 'utf8'),
bufSize);
} else {
// Flip the order of the benchmarks half of the time.
rawUnequalBenches[i] = runOneBenchmark(compareFunc, 'B', 'C', bufSize);
rawEqualBenches[i] = runOneBenchmark(compareFunc, 'A', 'A', bufSize);
rawUnequalBenches[i] = runOneBenchmark(
compareFunc,
fs.readFileSync(file2, 'utf8'),
fs.readFileSync(file3, 'utf8'),
bufSize);
rawEqualBenches[i] = runOneBenchmark(
compareFunc,
fs.readFileSync(file2, 'utf8'),
fs.readFileSync(file1, 'utf8'),
bufSize);
}
}

Expand Down