Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 460240c

Browse filesBrowse files
panvamarco-ippolito
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 44d901a commit 460240c
Copy full SHA for 460240c

7 files changed

+51-9Lines changed: 51 additions & 9 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/webcrypto.md‎

Copy file name to clipboardExpand all lines: doc/api/webcrypto.md
+11-7Lines changed: 11 additions & 7 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,15 @@ The algorithms currently supported include:
567567
* `'AES-CBC'`
568568
* `'AES-GCM`'
569569

570-
### `subtle.deriveBits(algorithm, baseKey, length)`
570+
### `subtle.deriveBits(algorithm, baseKey[, length])`
571571

572572
<!-- YAML
573573
added: v15.0.0
574574
changes:
575+
- version: REPLACEME
576+
pr-url: https://github.com/nodejs/node/pull/53601
577+
description: The length parameter is now optional for `'ECDH'`, `'X25519'`,
578+
and `'X448'`.
575579
- version:
576580
- v18.4.0
577581
- v16.17.0
@@ -583,7 +587,7 @@ changes:
583587

584588
* `algorithm`: {AlgorithmIdentifier|EcdhKeyDeriveParams|HkdfParams|Pbkdf2Params}
585589
* `baseKey`: {CryptoKey}
586-
* `length`: {number|null}
590+
* `length`: {number|null} **Default:** `null`
587591
* Returns: {Promise} Fulfills with an {ArrayBuffer}
588592

589593
<!--lint enable maximum-line-length remark-lint-->
@@ -592,12 +596,12 @@ Using the method and parameters specified in `algorithm` and the keying
592596
material provided by `baseKey`, `subtle.deriveBits()` attempts to generate
593597
`length` bits.
594598

595-
The Node.js implementation requires that when `length` is a
596-
number it must be multiple of `8`.
599+
The Node.js implementation requires that `length`, when a number, is a multiple
600+
of `8`.
597601

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

602606
If successful, the returned promise will be resolved with an {ArrayBuffer}
603607
containing the generated data.
Collapse file

‎lib/internal/crypto/webcrypto.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/webcrypto.js
+2-2Lines changed: 2 additions & 2 deletions
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',
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-webcrypto-derivebits-cfrg.js
+10Lines changed: 10 additions & 0 deletions
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({
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-webcrypto-derivebits-ecdh.js
+10Lines changed: 10 additions & 0 deletions
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({
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-webcrypto-derivebits-hkdf.js
+5Lines changed: 5 additions & 0 deletions
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/,
Collapse file

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

Copy file name to clipboardExpand all lines: test/pummel/test-webcrypto-derivebits-pbkdf2.js
+5Lines changed: 5 additions & 0 deletions
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/,
Collapse file

‎test/wpt/status/WebCryptoAPI.json‎

Copy file name to clipboardExpand all lines: test/wpt/status/WebCryptoAPI.json
+8Lines changed: 8 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.