Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
51 changes: 51 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,45 @@ 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 integers to be equal'
);

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

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

assert.strictEqual(
crypto.timingSafeEqual(
Buffer.allocUnsafe(8).fill(255),
BigInt64Array.of(-1n)
),
true,
'should consider equal binary integers to be equal'
);

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

assert.strictEqual(
crypto.timingSafeEqual(Buffer.from('foo'), Buffer.from('bar')),
false,
Expand All @@ -27,6 +66,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