Skip to content
Closed
Show file tree
Hide file tree
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
Expand Up @@ -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);
Expand Down
56 changes: 56 additions & 0 deletions test/sequential/test-crypto-timing-safe-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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])),
{
Expand Down