GeezerVison is showing github.com — crypto: make deriveBits length parameter optional and nullable · nodejs/node@3cc01aa
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -569,11 +569,15 @@ The algorithms currently supported include: | 569 | 569 | * `'AES-CBC'` | 570 | 570 | * `'AES-GCM`' | 571 | 571 |
| 572 | | -### `subtle.deriveBits(algorithm, baseKey, length)` | | 572 | +### `subtle.deriveBits(algorithm, baseKey[, length])` | 573 | 573 |
| 574 | 574 | <!-- YAML | 575 | 575 | added: v15.0.0 | 576 | 576 | 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'`. | 577 | 581 | - version: | 578 | 582 | - v18.4.0 | 579 | 583 | - v16.17.0 | @@ -585,7 +589,7 @@ changes: | 585 | 589 |
| 586 | 590 | * `algorithm`: {AlgorithmIdentifier|EcdhKeyDeriveParams|HkdfParams|Pbkdf2Params} | 587 | 591 | * `baseKey`: {CryptoKey} | 588 | | -* `length`: {number|null} | | 592 | +* `length`: {number|null} **Default:** `null` | 589 | 593 | * Returns: {Promise} Fulfills with an {ArrayBuffer} | 590 | 594 |
| 591 | 595 | <!--lint enable maximum-line-length remark-lint--> | @@ -594,12 +598,12 @@ Using the method and parameters specified in `algorithm` and the keying | 594 | 598 | material provided by `baseKey`, `subtle.deriveBits()` attempts to generate | 595 | 599 | `length` bits. | 596 | 600 |
| 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`. | 599 | 603 |
| 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. | 603 | 607 |
| 604 | 608 | If successful, the returned promise will be resolved with an {ArrayBuffer} | 605 | 609 | containing the generated data. | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webcrypto.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -178,12 +178,12 @@ async function generateKey( | 178 | 178 | return result; | 179 | 179 | } | 180 | 180 |
| 181 | | -async function deriveBits(algorithm, baseKey, length) { | | 181 | +async function deriveBits(algorithm, baseKey, length = null) { | 182 | 182 | if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto'); | 183 | 183 |
| 184 | 184 | webidl ??= require('internal/crypto/webidl'); | 185 | 185 | const prefix = "Failed to execute 'deriveBits' on 'SubtleCrypto'"; | 186 | | - webidl.requiredArguments(arguments.length, 3, { prefix }); | | 186 | + webidl.requiredArguments(arguments.length, 2, { prefix }); | 187 | 187 | algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { | 188 | 188 | prefix, | 189 | 189 | context: '1st argument', | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-webcrypto-derivebits-cfrg.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -101,6 +101,16 @@ async function prepareKeys() { | 101 | 101 | assert.strictEqual(Buffer.from(bits).toString('hex'), result); | 102 | 102 | } | 103 | 103 |
| | 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 | + | 104 | 114 | { | 105 | 115 | // Short Result | 106 | 116 | const bits = await subtle.deriveBits({ | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-webcrypto-derivebits-ecdh.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -122,6 +122,16 @@ async function prepareKeys() { | 122 | 122 | assert.strictEqual(Buffer.from(bits).toString('hex'), result); | 123 | 123 | } | 124 | 124 |
| | 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 | + | 125 | 135 | { | 126 | 136 | // Short Result | 127 | 137 | const bits = await subtle.deriveBits({ | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-webcrypto-derivebits-hkdf.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -271,6 +271,11 @@ async function testDeriveBitsBadLengths( | 271 | 271 | message: 'length cannot be null', | 272 | 272 | name: 'OperationError', | 273 | 273 | }), | | 274 | + assert.rejects( | | 275 | + subtle.deriveBits(algorithm, baseKeys[size]), { | | 276 | + message: 'length cannot be null', | | 277 | + name: 'OperationError', | | 278 | + }), | 274 | 279 | assert.rejects( | 275 | 280 | subtle.deriveBits(algorithm, baseKeys[size], 15), { | 276 | 281 | message: /length must be a multiple of 8/, | |
Collapse file Copy file name to clipboardExpand all lines: test/pummel/test-webcrypto-derivebits-pbkdf2.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -459,6 +459,11 @@ async function testDeriveBitsBadLengths( | 459 | 459 | message: 'length cannot be null', | 460 | 460 | name: 'OperationError', | 461 | 461 | }), | | 462 | + assert.rejects( | | 463 | + subtle.deriveBits(algorithm, baseKeys[size]), { | | 464 | + message: 'length cannot be null', | | 465 | + name: 'OperationError', | | 466 | + }), | 462 | 467 | assert.rejects( | 463 | 468 | subtle.deriveBits(algorithm, baseKeys[size], 15), { | 464 | 469 | message: /length must be a multiple of 8/, | |
Collapse file Copy file name to clipboardExpand all lines: test/wpt/status/WebCryptoAPI.json +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | }, | 5 | 5 | "historical.any.js": { | 6 | 6 | "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 | + } | 7 | 15 | } | 8 | 16 | } |
|