Skip to content

Commit 5c46782

Browse files
panvaaduh95
authored andcommitted
crypto: make deriveBits length parameter optional and nullable
PR-URL: #53601 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent d6114cb commit 5c46782

6 files changed

+44
-10
lines changed

doc/api/webcrypto.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,15 @@ The algorithms currently supported include:
552552
* `'AES-CBC'`
553553
* `'AES-GCM`'
554554

555-
### `subtle.deriveBits(algorithm, baseKey, length)`
555+
### `subtle.deriveBits(algorithm, baseKey[, length])`
556556

557557
<!-- YAML
558558
added: v15.0.0
559559
changes:
560+
- version: REPLACEME
561+
pr-url: https://github.com/nodejs/node/pull/53601
562+
description: The length parameter is now optional for `'ECDH'`, `'X25519'`,
563+
and `'X448'`.
560564
- version: v18.4.0
561565
pr-url: https://github.com/nodejs/node/pull/42507
562566
description: Added `'X25519'`, and `'X448'` algorithms.
@@ -566,21 +570,21 @@ changes:
566570

567571
* `algorithm`: {AlgorithmIdentifier|EcdhKeyDeriveParams|HkdfParams|Pbkdf2Params}
568572
* `baseKey`: {CryptoKey}
569-
* `length`: {number|null}
570-
* Returns: {Promise} containing {ArrayBuffer}
573+
* `length`: {number|null} **Default:** `null`
574+
* Returns: {Promise} Fulfills with an {ArrayBuffer}
571575

572576
<!--lint enable maximum-line-length remark-lint-->
573577

574578
Using the method and parameters specified in `algorithm` and the keying
575579
material provided by `baseKey`, `subtle.deriveBits()` attempts to generate
576580
`length` bits.
577581

578-
The Node.js implementation requires that when `length` is a
579-
number it must be multiple of `8`.
582+
The Node.js implementation requires that `length`, when a number, is a multiple
583+
of `8`.
580584

581-
When `length` is `null` the maximum number of bits for a given algorithm is
582-
generated. This is allowed for the `'ECDH'`, `'X25519'`, and `'X448'`
583-
algorithms.
585+
When `length` is not provided or `null` the maximum number of bits for a given
586+
algorithm is generated. This is allowed for the `'ECDH'`, `'X25519'`, and `'X448'`
587+
algorithms, for other algorithms `length` is required to be a number.
584588

585589
If successful, the returned promise will be resolved with an {ArrayBuffer}
586590
containing the generated data.

lib/internal/crypto/webcrypto.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ async function generateKey(
173173
return result;
174174
}
175175

176-
async function deriveBits(algorithm, baseKey, length) {
176+
async function deriveBits(algorithm, baseKey, length = null) {
177177
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');
178178

179179
webidl ??= require('internal/crypto/webidl');
180180
const prefix = "Failed to execute 'deriveBits' on 'SubtleCrypto'";
181-
webidl.requiredArguments(arguments.length, 3, { prefix });
181+
webidl.requiredArguments(arguments.length, 2, { prefix });
182182
algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {
183183
prefix,
184184
context: '1st argument',

test/parallel/test-webcrypto-derivebits-cfrg.js

+10
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ async function prepareKeys() {
102102
assert.strictEqual(Buffer.from(bits).toString('hex'), result);
103103
}
104104

105+
{
106+
// Default length
107+
const bits = await subtle.deriveBits({
108+
name,
109+
public: publicKey
110+
}, privateKey);
111+
112+
assert.strictEqual(Buffer.from(bits).toString('hex'), result);
113+
}
114+
105115
{
106116
// Short Result
107117
const bits = await subtle.deriveBits({

test/parallel/test-webcrypto-derivebits-ecdh.js

+10
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ async function prepareKeys() {
123123
assert.strictEqual(Buffer.from(bits).toString('hex'), result);
124124
}
125125

126+
{
127+
// Default length
128+
const bits = await subtle.deriveBits({
129+
name: 'ECDH',
130+
public: publicKey
131+
}, privateKey);
132+
133+
assert.strictEqual(Buffer.from(bits).toString('hex'), result);
134+
}
135+
126136
{
127137
// Short Result
128138
const bits = await subtle.deriveBits({

test/parallel/test-webcrypto-derivebits-hkdf.js

+5
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ async function testDeriveBitsBadLengths(
271271
message: 'length cannot be null',
272272
name: 'OperationError',
273273
}),
274+
assert.rejects(
275+
subtle.deriveBits(algorithm, baseKeys[size]), {
276+
message: 'length cannot be null',
277+
name: 'OperationError',
278+
}),
274279
assert.rejects(
275280
subtle.deriveBits(algorithm, baseKeys[size], 15), {
276281
message: /length must be a multiple of 8/,

test/pummel/test-webcrypto-derivebits-pbkdf2.js

+5
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ async function testDeriveBitsBadLengths(
459459
message: 'length cannot be null',
460460
name: 'OperationError',
461461
}),
462+
assert.rejects(
463+
subtle.deriveBits(algorithm, baseKeys[size]), {
464+
message: 'length cannot be null',
465+
name: 'OperationError',
466+
}),
462467
assert.rejects(
463468
subtle.deriveBits(algorithm, baseKeys[size], 15), {
464469
message: /length must be a multiple of 8/,

0 commit comments

Comments
 (0)