Skip to content

Commit 9e227d5

Browse files
tniessenTrott
authored andcommittedOct 20, 2020
test: refactor test-crypto-pbkdf2
PR-URL: nodejs#35693 Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 3d9310d commit 9e227d5

File tree

1 file changed

+27
-53
lines changed

1 file changed

+27
-53
lines changed
 

‎test/parallel/test-crypto-pbkdf2.js

+27-53
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,26 @@ if (!common.hasCrypto)
66
const assert = require('assert');
77
const crypto = require('crypto');
88

9-
//
10-
// Test PBKDF2 with RFC 6070 test vectors (except #4)
11-
//
12-
function testPBKDF2(password, salt, iterations, keylen, expected) {
13-
const actual =
14-
crypto.pbkdf2Sync(password, salt, iterations, keylen, 'sha256');
15-
assert.strictEqual(actual.toString('latin1'), expected);
9+
function runPBKDF2(password, salt, iterations, keylen, hash) {
10+
const syncResult =
11+
crypto.pbkdf2Sync(password, salt, iterations, keylen, hash);
1612

17-
crypto.pbkdf2(password, salt, iterations, keylen, 'sha256', (err, actual) => {
18-
assert.strictEqual(actual.toString('latin1'), expected);
19-
});
13+
crypto.pbkdf2(password, salt, iterations, keylen, hash,
14+
common.mustSucceed((asyncResult) => {
15+
assert.deepStrictEqual(asyncResult, syncResult);
16+
}));
17+
18+
return syncResult;
19+
}
20+
21+
function testPBKDF2(password, salt, iterations, keylen, expected, encoding) {
22+
const actual = runPBKDF2(password, salt, iterations, keylen, 'sha256');
23+
assert.strictEqual(actual.toString(encoding || 'latin1'), expected);
2024
}
2125

26+
//
27+
// Test PBKDF2 with RFC 6070 test vectors (except #4)
28+
//
2229

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

46-
const expected =
47-
'64c486c55d30d4c5a079b8823b7d7cb37ff0556f537da8410233bcec330ed956';
48-
const key = crypto.pbkdf2Sync('password', 'salt', 32, 32, 'sha256');
49-
assert.strictEqual(key.toString('hex'), expected);
50-
51-
crypto.pbkdf2('password', 'salt', 32, 32, 'sha256', common.mustSucceed(ondone));
52-
function ondone(key) {
53-
assert.strictEqual(key.toString('hex'), expected);
54-
}
53+
testPBKDF2('password', 'salt', 32, 32,
54+
'64c486c55d30d4c5a079b8823b7d7cb37ff0556f537da8410233bcec330ed956',
55+
'hex');
5556

5657
// Error path should not leak memory (check with valgrind).
5758
assert.throws(
@@ -197,38 +198,12 @@ assert.throws(
197198
);
198199
});
199200

200-
// Any TypedArray should work for password and salt
201-
crypto.pbkdf2(new Uint8Array(10), 'salt', 8, 8, 'sha256', common.mustSucceed());
202-
crypto.pbkdf2('pass', new Uint8Array(10), 8, 8, 'sha256', common.mustSucceed());
203-
crypto.pbkdf2(new Uint16Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
204-
crypto.pbkdf2('pass', new Uint16Array(10), 8, 8, 'sha1', common.mustSucceed());
205-
crypto.pbkdf2(new Uint32Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
206-
crypto.pbkdf2('pass', new Uint32Array(10), 8, 8, 'sha1', common.mustSucceed());
207-
crypto.pbkdf2(new Float32Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
208-
crypto.pbkdf2('pass', new Float32Array(10), 8, 8, 'sha1', common.mustSucceed());
209-
crypto.pbkdf2(new Float64Array(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
210-
crypto.pbkdf2('pass', new Float64Array(10), 8, 8, 'sha1', common.mustSucceed());
211-
crypto.pbkdf2(new ArrayBuffer(10), 'salt', 8, 8, 'sha1', common.mustSucceed());
212-
crypto.pbkdf2('pass', new ArrayBuffer(10), 8, 8, 'sha1', common.mustSucceed());
213-
crypto.pbkdf2(new SharedArrayBuffer(10), 'salt', 8, 8, 'sha256',
214-
common.mustSucceed());
215-
crypto.pbkdf2('pass', new SharedArrayBuffer(10), 8, 8, 'sha256',
216-
common.mustSucceed());
217-
218-
crypto.pbkdf2Sync(new Uint8Array(10), 'salt', 8, 8, 'sha256');
219-
crypto.pbkdf2Sync('pass', new Uint8Array(10), 8, 8, 'sha256');
220-
crypto.pbkdf2Sync(new Uint16Array(10), 'salt', 8, 8, 'sha256');
221-
crypto.pbkdf2Sync('pass', new Uint16Array(10), 8, 8, 'sha256');
222-
crypto.pbkdf2Sync(new Uint32Array(10), 'salt', 8, 8, 'sha256');
223-
crypto.pbkdf2Sync('pass', new Uint32Array(10), 8, 8, 'sha256');
224-
crypto.pbkdf2Sync(new Float32Array(10), 'salt', 8, 8, 'sha256');
225-
crypto.pbkdf2Sync('pass', new Float32Array(10), 8, 8, 'sha256');
226-
crypto.pbkdf2Sync(new Float64Array(10), 'salt', 8, 8, 'sha256');
227-
crypto.pbkdf2Sync('pass', new Float64Array(10), 8, 8, 'sha256');
228-
crypto.pbkdf2Sync(new ArrayBuffer(10), 'salt', 8, 8, 'sha256');
229-
crypto.pbkdf2Sync('pass', new ArrayBuffer(10), 8, 8, 'sha256');
230-
crypto.pbkdf2Sync(new SharedArrayBuffer(10), 'salt', 8, 8, 'sha256');
231-
crypto.pbkdf2Sync('pass', new SharedArrayBuffer(10), 8, 8, 'sha256');
201+
// Any TypedArray should work for password and salt.
202+
for (const SomeArray of [Uint8Array, Uint16Array, Uint32Array, Float32Array,
203+
Float64Array, ArrayBuffer, SharedArrayBuffer]) {
204+
runPBKDF2(new SomeArray(10), 'salt', 8, 8, 'sha256');
205+
runPBKDF2('pass', new SomeArray(10), 8, 8, 'sha256');
206+
}
232207

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

0 commit comments

Comments
 (0)
Please sign in to comment.