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

test: refactor test-crypto-pbkdf2 #35693

Merged
merged 1 commit into from Oct 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 27 additions & 53 deletions test/parallel/test-crypto-pbkdf2.js
Expand Up @@ -6,19 +6,26 @@ if (!common.hasCrypto)
const assert = require('assert');
const crypto = require('crypto');

//
// Test PBKDF2 with RFC 6070 test vectors (except #4)
//
function testPBKDF2(password, salt, iterations, keylen, expected) {
const actual =
crypto.pbkdf2Sync(password, salt, iterations, keylen, 'sha256');
assert.strictEqual(actual.toString('latin1'), expected);
function runPBKDF2(password, salt, iterations, keylen, hash) {
const syncResult =
crypto.pbkdf2Sync(password, salt, iterations, keylen, hash);

crypto.pbkdf2(password, salt, iterations, keylen, 'sha256', (err, actual) => {
assert.strictEqual(actual.toString('latin1'), expected);
});
crypto.pbkdf2(password, salt, iterations, keylen, hash,
common.mustSucceed((asyncResult) => {
assert.deepStrictEqual(asyncResult, syncResult);
}));

return syncResult;
}

function testPBKDF2(password, salt, iterations, keylen, expected, encoding) {
const actual = runPBKDF2(password, salt, iterations, keylen, 'sha256');
assert.strictEqual(actual.toString(encoding || 'latin1'), expected);
}

//
// Test PBKDF2 with RFC 6070 test vectors (except #4)
//

testPBKDF2('password', 'salt', 1, 20,
'\x12\x0f\xb6\xcf\xfc\xf8\xb3\x2c\x43\xe7\x22\x52' +
Expand All @@ -43,15 +50,9 @@ testPBKDF2('pass\0word', 'sa\0lt', 4096, 16,
'\x89\xb6\x9d\x05\x16\xf8\x29\x89\x3c\x69\x62\x26\x65' +
'\x0a\x86\x87');

const expected =
'64c486c55d30d4c5a079b8823b7d7cb37ff0556f537da8410233bcec330ed956';
const key = crypto.pbkdf2Sync('password', 'salt', 32, 32, 'sha256');
assert.strictEqual(key.toString('hex'), expected);

crypto.pbkdf2('password', 'salt', 32, 32, 'sha256', common.mustSucceed(ondone));
function ondone(key) {
assert.strictEqual(key.toString('hex'), expected);
}
testPBKDF2('password', 'salt', 32, 32,
'64c486c55d30d4c5a079b8823b7d7cb37ff0556f537da8410233bcec330ed956',
'hex');

// Error path should not leak memory (check with valgrind).
assert.throws(
Expand Down Expand Up @@ -197,38 +198,12 @@ assert.throws(
);
});

// Any TypedArray should work for password and salt
crypto.pbkdf2(new Uint8Array(10), 'salt', 8, 8, 'sha256', common.mustSucceed());
crypto.pbkdf2('pass', new Uint8Array(10), 8, 8, 'sha256', common.mustSucceed());
crypto.pbkdf2(new Uint16Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
crypto.pbkdf2('pass', new Uint16Array(10), 8, 8, 'sha1', common.mustSucceed());
crypto.pbkdf2(new Uint32Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
crypto.pbkdf2('pass', new Uint32Array(10), 8, 8, 'sha1', common.mustSucceed());
crypto.pbkdf2(new Float32Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
crypto.pbkdf2('pass', new Float32Array(10), 8, 8, 'sha1', common.mustSucceed());
crypto.pbkdf2(new Float64Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
crypto.pbkdf2('pass', new Float64Array(10), 8, 8, 'sha1', common.mustSucceed());
crypto.pbkdf2(new ArrayBuffer(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
crypto.pbkdf2('pass', new ArrayBuffer(10), 8, 8, 'sha1', common.mustSucceed());
crypto.pbkdf2(new SharedArrayBuffer(10), 'salt', 8, 8, 'sha256',
common.mustSucceed());
crypto.pbkdf2('pass', new SharedArrayBuffer(10), 8, 8, 'sha256',
common.mustSucceed());

crypto.pbkdf2Sync(new Uint8Array(10), 'salt', 8, 8, 'sha256');
crypto.pbkdf2Sync('pass', new Uint8Array(10), 8, 8, 'sha256');
crypto.pbkdf2Sync(new Uint16Array(10), 'salt', 8, 8, 'sha256');
crypto.pbkdf2Sync('pass', new Uint16Array(10), 8, 8, 'sha256');
crypto.pbkdf2Sync(new Uint32Array(10), 'salt', 8, 8, 'sha256');
crypto.pbkdf2Sync('pass', new Uint32Array(10), 8, 8, 'sha256');
crypto.pbkdf2Sync(new Float32Array(10), 'salt', 8, 8, 'sha256');
crypto.pbkdf2Sync('pass', new Float32Array(10), 8, 8, 'sha256');
crypto.pbkdf2Sync(new Float64Array(10), 'salt', 8, 8, 'sha256');
crypto.pbkdf2Sync('pass', new Float64Array(10), 8, 8, 'sha256');
crypto.pbkdf2Sync(new ArrayBuffer(10), 'salt', 8, 8, 'sha256');
crypto.pbkdf2Sync('pass', new ArrayBuffer(10), 8, 8, 'sha256');
crypto.pbkdf2Sync(new SharedArrayBuffer(10), 'salt', 8, 8, 'sha256');
crypto.pbkdf2Sync('pass', new SharedArrayBuffer(10), 8, 8, 'sha256');
// Any TypedArray should work for password and salt.
for (const SomeArray of [Uint8Array, Uint16Array, Uint32Array, Float32Array,
Float64Array, ArrayBuffer, SharedArrayBuffer]) {
runPBKDF2(new SomeArray(10), 'salt', 8, 8, 'sha256');
runPBKDF2('pass', new SomeArray(10), 8, 8, 'sha256');
}

assert.throws(
() => crypto.pbkdf2('pass', 'salt', 8, 8, 'md55', common.mustNotCall()),
Expand All @@ -252,6 +227,5 @@ const kNotPBKDF2Supported = ['shake128', 'shake256'];
crypto.getHashes()
.filter((hash) => !kNotPBKDF2Supported.includes(hash))
.forEach((hash) => {
crypto.pbkdf2Sync(new Uint8Array(10), 'salt', 8, 8, hash);
crypto.pbkdf2(new Uint8Array(10), 'salt', 8, 8, hash, common.mustCall());
runPBKDF2(new Uint8Array(10), 'salt', 8, 8, hash);
});