Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: simplify timingSafeEqual test case
  • Loading branch information
tniessen committed Sep 23, 2019
commit e1ada5bf7c596f3a71e872d8e52cfce5eb7a66a4
106 changes: 9 additions & 97 deletions test/sequential/test-crypto-timing-safe-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,105 +19,17 @@ assert.strictEqual(
);

{
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);
// Test TypedArrays with different lengths but equal byteLengths.
const buf = crypto.randomBytes(16).buffer;
const a1 = new Uint8Array(buf);
const a2 = new Uint16Array(buf);
const a3 = new Uint32Array(buf);

// '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 number of bytes'
}
);
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
),
{
code: 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH',
type: RangeError,
message: 'Input buffers must have the same number of bytes'
for (const left of [a1, a2, a3]) {
for (const right of [a1, a2, a3]) {
assert.strictEqual(crypto.timingSafeEqual(left, right), true);
}
);
}
}

common.expectsError(
Expand Down