crypto: make deriveBits length parameter optional and nullable · nodejs/node@5c46782 · GitHub
Skip to content

Commit 5c46782

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 d6114cb commit 5c46782

6 files changed

Lines changed: 44 additions & 10 deletions

File tree

doc/api/webcrypto.md

Lines changed: 12 additions & 8 deletions

lib/internal/crypto/webcrypto.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ async function generateKey(
173173
return result;
174174
}
175175

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

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ async function prepareKeys() {
102102
assert.strictEqual(Buffer.from(bits).toString('hex'), result);
103103
}
104104

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+
105115
{
106116
// Short Result
107117
const bits = await subtle.deriveBits({

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ async function prepareKeys() {
123123
assert.strictEqual(Buffer.from(bits).toString('hex'), result);
124124
}
125125

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+
126136
{
127137
// Short Result
128138
const bits = await subtle.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: /length must be a multiple of 8/,

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

Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)