Skip to content

Commit 3cc01aa

Browse files
panvaaduh95
authored andcommitted
crypto: make deriveBits length parameter optional and nullable
PR-URL: #53601 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent dd2157b commit 3cc01aa

7 files changed

+51
-9
lines changed

doc/api/webcrypto.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,15 @@ The algorithms currently supported include:
569569
* `'AES-CBC'`
570570
* `'AES-GCM`'
571571

572-
### `subtle.deriveBits(algorithm, baseKey, length)`
572+
### `subtle.deriveBits(algorithm, baseKey[, length])`
573573

574574
<!-- YAML
575575
added: v15.0.0
576576
changes:
577+
- version: REPLACEME
578+
pr-url: https://github.com/nodejs/node/pull/53601
579+
description: The length parameter is now optional for `'ECDH'`, `'X25519'`,
580+
and `'X448'`.
577581
- version:
578582
- v18.4.0
579583
- v16.17.0
@@ -585,7 +589,7 @@ changes:
585589

586590
* `algorithm`: {AlgorithmIdentifier|EcdhKeyDeriveParams|HkdfParams|Pbkdf2Params}
587591
* `baseKey`: {CryptoKey}
588-
* `length`: {number|null}
592+
* `length`: {number|null} **Default:** `null`
589593
* Returns: {Promise} Fulfills with an {ArrayBuffer}
590594

591595
<!--lint enable maximum-line-length remark-lint-->
@@ -594,12 +598,12 @@ Using the method and parameters specified in `algorithm` and the keying
594598
material provided by `baseKey`, `subtle.deriveBits()` attempts to generate
595599
`length` bits.
596600

597-
The Node.js implementation requires that when `length` is a
598-
number it must be multiple of `8`.
601+
The Node.js implementation requires that `length`, when a number, is a multiple
602+
of `8`.
599603

600-
When `length` is `null` the maximum number of bits for a given algorithm is
601-
generated. This is allowed for the `'ECDH'`, `'X25519'`, and `'X448'`
602-
algorithms.
604+
When `length` is not provided or `null` the maximum number of bits for a given
605+
algorithm is generated. This is allowed for the `'ECDH'`, `'X25519'`, and `'X448'`
606+
algorithms, for other algorithms `length` is required to be a number.
603607

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

lib/internal/crypto/webcrypto.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ async function generateKey(
178178
return result;
179179
}
180180

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

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

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

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

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

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

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

125+
{
126+
// Default length
127+
const bits = await subtle.deriveBits({
128+
name: 'ECDH',
129+
public: publicKey
130+
}, privateKey);
131+
132+
assert.strictEqual(Buffer.from(bits).toString('hex'), result);
133+
}
134+
125135
{
126136
// Short Result
127137
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/,

test/wpt/status/WebCryptoAPI.json

+8
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@
44
},
55
"historical.any.js": {
66
"skip": "Not relevant in Node.js context"
7+
},
8+
"idlharness.https.any.js": {
9+
"fail": {
10+
"note": "WPT not updated for https://github.com/w3c/webcrypto/pull/345 yet",
11+
"expected": [
12+
"SubtleCrypto interface: operation deriveBits(AlgorithmIdentifier, CryptoKey, unsigned long)"
13+
]
14+
}
715
}
816
}

0 commit comments

Comments
 (0)