crypto: fix webcrypto derive(Bits|Key) resolve values and docs

fixes #38115

PR-URL: https://github.com/nodejs/node/pull/38148
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Filip Skokan 2021-04-08 10:25:25 +02:00
parent f46d29360c
commit 896dc39951
8 changed files with 11 additions and 4 deletions

View File

@ -275,7 +275,7 @@ async function pbkdf2Key(pass, salt, iterations = 1000, length = 256) {
ec.encode(pass), ec.encode(pass),
'PBKDF2', 'PBKDF2',
false, false,
['deriveBits']); ['deriveKey']);
const key = await subtle.deriveKey({ const key = await subtle.deriveKey({
name: 'PBKDF2', name: 'PBKDF2',
hash: 'SHA-512', hash: 'SHA-512',
@ -536,7 +536,7 @@ added: v15.0.0
* `derivedKeyAlgorithm`: {HmacKeyGenParams|AesKeyGenParams} * `derivedKeyAlgorithm`: {HmacKeyGenParams|AesKeyGenParams}
* `extractable`: {boolean} * `extractable`: {boolean}
* `keyUsages`: {string[]} See [Key usages][]. * `keyUsages`: {string[]} See [Key usages][].
* Returns: {Promise} containing {ArrayBuffer} * Returns: {Promise} containing {CryptoKey}
<!--lint enable maximum-line-length remark-lint--> <!--lint enable maximum-line-length remark-lint-->
Using the method and parameters specified in `algorithm`, and the keying Using the method and parameters specified in `algorithm`, and the keying

View File

@ -122,7 +122,7 @@ async function pbkdf2DeriveBits(algorithm, baseKey, length) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
pbkdf2(raw, salt, iterations, byteLength, hash, (err, result) => { pbkdf2(raw, salt, iterations, byteLength, hash, (err, result) => {
if (err) return reject(err); if (err) return reject(err);
resolve(result); resolve(result.buffer);
}); });
}); });
} }

View File

@ -167,7 +167,7 @@ async function scryptDeriveBits(algorithm, baseKey, length) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
scrypt(raw, salt, byteLength, { N, r, p, maxmem }, (err, result) => { scrypt(raw, salt, byteLength, { N, r, p, maxmem }, (err, result) => {
if (err) return reject(err); if (err) return reject(err);
resolve(result); resolve(result.buffer);
}); });
}); });
} }

View File

@ -98,6 +98,7 @@ async function prepareKeys() {
public: publicKey public: publicKey
}, privateKey, 8 * size); }, privateKey, 8 * size);
assert(bits instanceof ArrayBuffer);
assert.strictEqual(Buffer.from(bits).toString('hex'), result); assert.strictEqual(Buffer.from(bits).toString('hex'), result);
} }

View File

@ -237,6 +237,7 @@ async function testDeriveBits(
baseKeys[size], baseKeys[size],
256); 256);
assert(bits instanceof ArrayBuffer);
assert.strictEqual( assert.strictEqual(
Buffer.from(bits).toString('hex'), Buffer.from(bits).toString('hex'),
kDerivations[size][saltSize][hash][infoSize]); kDerivations[size][saltSize][hash][infoSize]);

View File

@ -112,6 +112,7 @@ async function prepareKeys() {
public: publicKey public: publicKey
}, privateKey, null); }, privateKey, null);
assert(bits instanceof ArrayBuffer);
assert.strictEqual(Buffer.from(bits).toString('hex'), result); assert.strictEqual(Buffer.from(bits).toString('hex'), result);
} }

View File

@ -421,6 +421,7 @@ async function testDeriveBits(
const bits = await subtle.deriveBits(algorithm, baseKeys[size], 256); const bits = await subtle.deriveBits(algorithm, baseKeys[size], 256);
assert(bits instanceof ArrayBuffer);
assert.strictEqual( assert.strictEqual(
Buffer.from(bits).toString('hex'), Buffer.from(bits).toString('hex'),
kDerivations[size][saltSize][hash][iterations]); kDerivations[size][saltSize][hash][iterations]);

View File

@ -31,6 +31,8 @@ const { internalBinding } = require('internal/test/binding');
}, alice.privateKey, 128), }, alice.privateKey, 128),
]); ]);
assert(secret1 instanceof ArrayBuffer);
assert(secret2 instanceof ArrayBuffer);
assert.deepStrictEqual(secret1, secret2); assert.deepStrictEqual(secret1, secret2);
} }
@ -114,6 +116,7 @@ if (typeof internalBinding('crypto').ScryptJob === 'function') {
name: 'NODE-SCRYPT', name: 'NODE-SCRYPT',
salt: ec.encode(salt), salt: ec.encode(salt),
}, key, length); }, key, length);
assert(secret instanceof ArrayBuffer);
assert.strictEqual(Buffer.from(secret).toString('hex'), expected); assert.strictEqual(Buffer.from(secret).toString('hex'), expected);
} }