Skip to content

Commit

Permalink
crypto: fix webcrypto derive(Bits|Key) resolve values and docs
Browse files Browse the repository at this point in the history
fixes #38115

PR-URL: #38148
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
panva committed Apr 12, 2021
1 parent f46d293 commit 896dc39
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions doc/api/webcrypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ async function pbkdf2Key(pass, salt, iterations = 1000, length = 256) {
ec.encode(pass),
'PBKDF2',
false,
['deriveBits']);
['deriveKey']);
const key = await subtle.deriveKey({
name: 'PBKDF2',
hash: 'SHA-512',
Expand Down Expand Up @@ -536,7 +536,7 @@ added: v15.0.0
* `derivedKeyAlgorithm`: {HmacKeyGenParams|AesKeyGenParams}
* `extractable`: {boolean}
* `keyUsages`: {string[]} See [Key usages][].
* Returns: {Promise} containing {ArrayBuffer}
* Returns: {Promise} containing {CryptoKey}
<!--lint enable maximum-line-length remark-lint-->

Using the method and parameters specified in `algorithm`, and the keying
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async function pbkdf2DeriveBits(algorithm, baseKey, length) {
return new Promise((resolve, reject) => {
pbkdf2(raw, salt, iterations, byteLength, hash, (err, result) => {
if (err) return reject(err);
resolve(result);
resolve(result.buffer);
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/scrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ async function scryptDeriveBits(algorithm, baseKey, length) {
return new Promise((resolve, reject) => {
scrypt(raw, salt, byteLength, { N, r, p, maxmem }, (err, result) => {
if (err) return reject(err);
resolve(result);
resolve(result.buffer);
});
});
}
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-webcrypto-derivebits-ecdh.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async function prepareKeys() {
public: publicKey
}, privateKey, 8 * size);

assert(bits instanceof ArrayBuffer);
assert.strictEqual(Buffer.from(bits).toString('hex'), result);
}

Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-webcrypto-derivebits-hkdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ async function testDeriveBits(
baseKeys[size],
256);

assert(bits instanceof ArrayBuffer);
assert.strictEqual(
Buffer.from(bits).toString('hex'),
kDerivations[size][saltSize][hash][infoSize]);
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-webcrypto-derivebits-node-dh.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ async function prepareKeys() {
public: publicKey
}, privateKey, null);

assert(bits instanceof ArrayBuffer);
assert.strictEqual(Buffer.from(bits).toString('hex'), result);
}

Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-webcrypto-derivebits-pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ async function testDeriveBits(

const bits = await subtle.deriveBits(algorithm, baseKeys[size], 256);

assert(bits instanceof ArrayBuffer);
assert.strictEqual(
Buffer.from(bits).toString('hex'),
kDerivations[size][saltSize][hash][iterations]);
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-webcrypto-derivebits.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const { internalBinding } = require('internal/test/binding');
}, alice.privateKey, 128),
]);

assert(secret1 instanceof ArrayBuffer);
assert(secret2 instanceof ArrayBuffer);
assert.deepStrictEqual(secret1, secret2);
}

Expand Down Expand Up @@ -114,6 +116,7 @@ if (typeof internalBinding('crypto').ScryptJob === 'function') {
name: 'NODE-SCRYPT',
salt: ec.encode(salt),
}, key, length);
assert(secret instanceof ArrayBuffer);
assert.strictEqual(Buffer.from(secret).toString('hex'), expected);
}

Expand Down

0 comments on commit 896dc39

Please sign in to comment.