Skip to content
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

Change timingSafeEqual to use byteLength. #21397

Closed
wants to merge 4 commits 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
2 changes: 1 addition & 1 deletion lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ function timingSafeEqual(buf1, buf2) {
throw new ERR_INVALID_ARG_TYPE('buf2',
['Buffer', 'TypedArray', 'DataView'], buf2);
}
if (buf1.length !== buf2.length) {
if (buf1.byteLength !== buf2.byteLength) {
throw new ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH();
}
return _timingSafeEqual(buf1, buf2);
56 changes: 56 additions & 0 deletions test/sequential/test-crypto-timing-safe-equal.js
Original file line number Diff line number Diff line change
@@ -12,6 +12,50 @@ assert.strictEqual(
'should consider equal strings to be equal'
);

assert.strictEqual(
crypto.timingSafeEqual(Uint8Array.of(1, 0), Uint16Array.of(1)),
true,
'should consider equal binary views to be equal' +
' (Uint8Array 1 0, Uint16Array 1)'
);

assert.strictEqual(
crypto.timingSafeEqual(Uint8Array.of(0, 1), Uint16Array.of(1)),
false,
'should consider unequal binary views to be unequal' +
' (Uint8Array 0 1, Uint16Array 1)'
);

assert.strictEqual(
crypto.timingSafeEqual(
Uint8Array.of(1, 0, 0, 0, 0, 0, 0, 0),
BigUint64Array.of(1n)
),
true,
'should consider equal binary views to be equal' +
' (Uint8Array 1 ...0, BigUint64Array 1n)'
);

assert.strictEqual(
crypto.timingSafeEqual(
Buffer.alloc(8, 255),
BigInt64Array.of(-1n)
),
true,
'should consider equal binary views to be equal' +
' (Buffer 0xFF, BigInt64Array -1)'
);

assert.strictEqual(
crypto.timingSafeEqual(
new DataView(new ArrayBuffer(8)),
BigInt64Array.of(0n)
),
true,
'should consider equal binary views to be equal' +
' (DataView, BigInt64Array)'
);

assert.strictEqual(
crypto.timingSafeEqual(Buffer.from('foo'), Buffer.from('bar')),
false,
@@ -27,6 +71,18 @@ common.expectsError(
}
);

common.expectsError(
() => crypto.timingSafeEqual(
Uint8Array.of(1, 2, 3),
Uint16Array.of(1, 2, 3)
),
{
code: 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH',
type: RangeError,
message: 'Input buffers must have the same length'
}
);

common.expectsError(
() => crypto.timingSafeEqual('not a buffer', Buffer.from([1, 2])),
{