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

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
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);
Copy link
Member

@tniessen tniessen Apr 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, pbkdf2 is only required to give us a Buffer, which does not necessarily have the same byte length as the backing ArrayBuffer:

> Buffer.from([1, 2, 3]).buffer.byteLength
8192
> crypto.randomFillSync(Buffer.allocUnsafe(1024)).buffer.byteLength
8192

That's one of many reasons why WebCrypto doesn't align well with Node.js. However, we know that the implementation of PBKDF2 always returns a Buffer that has the same byteLength as the backing ArrayBuffer. And, hopefully, tests will catch it if that ever changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i've checked that they do. Alternatively we can do new Uint8Array(result).buffer

> const foo = Buffer.from('foo')
> foo.buffer.byteLength
8192
> new Uint8Array(foo).buffer
ArrayBuffer { [Uint8Contents]: <66 6f 6f>, byteLength: 3 }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these would be places where adding assertions about matching lengths and byteOffsets might make sense :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we can do new Uint8Array(result).buffer

That would create an unnecessary copy of sensitive data (the result of PBKDF2 is considered secret). And yes, I realize that arguing about memory safety is moot in JavaScript :)

});
});
}
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