Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit460240c

Browse files
panvamarco-ippolito
authored andcommitted
crypto: make deriveBits length parameter optional and nullable
PR-URL:#53601Reviewed-By: Luigi Pinca <luigipinca@gmail.com>Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent44d901a commit460240c

File tree

7 files changed

+51
-9
lines changed

7 files changed

+51
-9
lines changed

‎doc/api/webcrypto.md

Lines changed: 11 additions & 7 deletions
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 thatwhen`length`is a
596-
number it must be multipleof`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` isnot provided or`null` the maximum number of bits for a given
603+
algorithm isgenerated. 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.

‎lib/internal/crypto/webcrypto.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ async function generateKey(
178178
returnresult;
179179
}
180180

181-
asyncfunctionderiveBits(algorithm,baseKey,length){
181+
asyncfunctionderiveBits(algorithm,baseKey,length=null){
182182
if(this!==subtle)thrownewERR_INVALID_THIS('SubtleCrypto');
183183

184184
webidl??=require('internal/crypto/webidl');
185185
constprefix="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

Lines 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+
constbits=awaitsubtle.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
constbits=awaitsubtle.deriveBits({

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

Lines 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+
constbits=awaitsubtle.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
constbits=awaitsubtle.deriveBits({

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

Lines 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:/lengthmustbeamultipleof8/,

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

Lines 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:/lengthmustbeamultipleof8/,

‎test/wpt/status/WebCryptoAPI.json

Lines 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)

[8]ページ先頭

©2009-2025 Movatter.jp