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

crypto: use byteLength in timingSafeEqual #23341

Closed
wants to merge 7 commits into from
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 @@ -92,7 +92,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
2 changes: 1 addition & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ E('ERR_CRYPTO_SCRYPT_NOT_SUPPORTED', 'Scrypt algorithm not supported', Error);
// Switch to TypeError. The current implementation does not seem right.
E('ERR_CRYPTO_SIGN_KEY_REQUIRED', 'No key provided to sign', Error);
E('ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH',
'Input buffers must have the same length', RangeError);
'Input buffers must have the same byteLength', RangeError);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to consider undoing this change. Error message changes are considered semver-major, meaning this couldn't be released until Node.js 12.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure, are we still doing that? I was under the impression that we don’t necessarily consider it semver-major when there’s an associated message code…

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that error message changes shouldn't be semver major if there is an error code, since that was (one of) the advertised advantage in migrating to them. That said, I'm not sure there is much benefit to changing this. IMO, the error message was better before, even if it didn't completely reflect the implementation details.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, Input buffers must have the same number of bytes? Same idea, a bit more human readable?

E('ERR_DNS_SET_SERVERS_FAILED', 'c-ares failed to set servers: "%s" [%s]',
Error);
E('ERR_DOMAIN_CALLBACK_NOT_AVAILABLE',
Expand Down
104 changes: 103 additions & 1 deletion test/sequential/test-crypto-timing-safe-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,114 @@ assert.strictEqual(
false
);

{
const ab32 = new ArrayBuffer(32);
const dv32 = new DataView(ab32);
dv32.setUint32(0, 1);
dv32.setUint32(4, 1, true);
dv32.setBigUint64(8, 1n);
dv32.setBigUint64(16, 1n, true);
dv32.setUint16(24, 1);
dv32.setUint16(26, 1, true);
dv32.setUint8(28, 1);
dv32.setUint8(29, 1);

// 'should consider equal buffers to be equal'
assert.strictEqual(
crypto.timingSafeEqual(Buffer.from(ab32), dv32),
true
);
assert.strictEqual(
crypto.timingSafeEqual(new Uint32Array(ab32), dv32),
true
);
assert.strictEqual(
crypto.timingSafeEqual(
new Uint8Array(ab32, 0, 16),
Buffer.from(ab32, 0, 16)
),
true
);
assert.strictEqual(
crypto.timingSafeEqual(
new Uint32Array(ab32, 0, 8),
Buffer.of(0, 0, 0, 1, // 4
1, 0, 0, 0, // 8
0, 0, 0, 0, 0, 0, 0, 1, // 16
1, 0, 0, 0, 0, 0, 0, 0, // 24
0, 1, // 26
1, 0, // 28
1, 1, // 30
0, 0) // 32
),
true
);

// 'should consider unequal buffer views to be unequal'
assert.strictEqual(
crypto.timingSafeEqual(
new Uint32Array(ab32, 16, 4),
Buffer.from(ab32, 0, 16)
),
false
);
assert.strictEqual(
crypto.timingSafeEqual(
new Uint8Array(ab32, 0, 16),
Buffer.from(ab32, 16, 16)
),
false
);
assert.strictEqual(
crypto.timingSafeEqual(
new Uint32Array(ab32, 0, 8),
Buffer.of(0, 0, 0, 1, // 4
1, 0, 0, 0, // 8
0, 0, 0, 0, 0, 0, 0, 1, // 16
1, 0, 0, 0, 0, 0, 0, 0, // 24
0, 1, // 26
1, 0, // 28
1, 1, // 30
0, 1) // 32
),
false
);
// 'buffers with differing byteLength must throw an equal length error'
common.expectsError(
() => crypto.timingSafeEqual(Buffer.from(ab32, 0, 8),
Buffer.from(ab32, 0, 9)),
{
code: 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH',
type: RangeError,
message: 'Input buffers must have the same byteLength'
}
);
common.expectsError(
() => crypto.timingSafeEqual(
new Uint32Array(ab32, 0, 8), // 32
Buffer.of(0, 0, 0, 1, // 4
1, 0, 0, 0, // 8
0, 0, 0, 0, 0, 0, 0, 1, // 16
1, 0, 0, 0, 0, 0, 0, 0, // 24
0, 1, // 26
1, 0, // 28
1, 1, // 30
0) // 31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you factor out these Buffer.of() calls? Just assign it once to a const expected = Buffer.of(...) and compare against that.

),
{
code: 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH',
type: RangeError,
message: 'Input buffers must have the same byteLength'
}
);
}

common.expectsError(
() => crypto.timingSafeEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 2])),
{
code: 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH',
type: RangeError,
message: 'Input buffers must have the same length'
message: 'Input buffers must have the same byteLength'
}
);

Expand Down