GeezerVison is showing github.com — crypto: make deriveBits length parameter optional and nullable · nodejs/node@5c46782
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 +12-8Lines changed: 12 additions & 8 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -552,11 +552,15 @@ The algorithms currently supported include: | 552 | 552 | * `'AES-CBC'` | 553 | 553 | * `'AES-GCM`' | 554 | 554 |
| 555 | | -### `subtle.deriveBits(algorithm, baseKey, length)` | | 555 | +### `subtle.deriveBits(algorithm, baseKey[, length])` | 556 | 556 |
| 557 | 557 | <!-- YAML | 558 | 558 | added: v15.0.0 | 559 | 559 | 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'`. | 560 | 564 | - version: v18.4.0 | 561 | 565 | pr-url: https://github.com/nodejs/node/pull/42507 | 562 | 566 | description: Added `'X25519'`, and `'X448'` algorithms. | @@ -566,21 +570,21 @@ changes: | 566 | 570 |
| 567 | 571 | * `algorithm`: {AlgorithmIdentifier|EcdhKeyDeriveParams|HkdfParams|Pbkdf2Params} | 568 | 572 | * `baseKey`: {CryptoKey} | 569 | | -* `length`: {number|null} | 570 | | -* Returns: {Promise} containing {ArrayBuffer} | | 573 | +* `length`: {number|null} **Default:** `null` | | 574 | +* Returns: {Promise} Fulfills with an {ArrayBuffer} | 571 | 575 |
| 572 | 576 | <!--lint enable maximum-line-length remark-lint--> | 573 | 577 |
| 574 | 578 | Using the method and parameters specified in `algorithm` and the keying | 575 | 579 | material provided by `baseKey`, `subtle.deriveBits()` attempts to generate | 576 | 580 | `length` bits. | 577 | 581 |
| 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`. | 580 | 584 |
| 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. | 584 | 588 |
| 585 | 589 | If successful, the returned promise will be resolved with an {ArrayBuffer} | 586 | 590 | 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 |
|---|
@@ -173,12 +173,12 @@ async function generateKey( | 173 | 173 | return result; | 174 | 174 | } | 175 | 175 |
| 176 | | -async function deriveBits(algorithm, baseKey, length) { | | 176 | +async function deriveBits(algorithm, baseKey, length = null) { | 177 | 177 | if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto'); | 178 | 178 |
| 179 | 179 | webidl ??= require('internal/crypto/webidl'); | 180 | 180 | const prefix = "Failed to execute 'deriveBits' on 'SubtleCrypto'"; | 181 | | - webidl.requiredArguments(arguments.length, 3, { prefix }); | | 181 | + webidl.requiredArguments(arguments.length, 2, { prefix }); | 182 | 182 | algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { | 183 | 183 | prefix, | 184 | 184 | 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 |
|---|
@@ -102,6 +102,16 @@ async function prepareKeys() { | 102 | 102 | assert.strictEqual(Buffer.from(bits).toString('hex'), result); | 103 | 103 | } | 104 | 104 |
| | 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 | + | 105 | 115 | { | 106 | 116 | // Short Result | 107 | 117 | 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 |
|---|
@@ -123,6 +123,16 @@ async function prepareKeys() { | 123 | 123 | assert.strictEqual(Buffer.from(bits).toString('hex'), result); | 124 | 124 | } | 125 | 125 |
| | 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 | + | 126 | 136 | { | 127 | 137 | // Short Result | 128 | 138 | 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/, | |
|