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 07d90c8

Browse filesBrowse files
panvaruyadorno
authored andcommitted
crypto: allow zero-length secret KeyObject
PR-URL: #44201 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent ac2b10e commit 07d90c8
Copy full SHA for 07d90c8

File tree

Expand file treeCollapse file tree

5 files changed

+22
-16
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+22
-16
lines changed
Open diff view settings
Collapse file

‎doc/api/crypto.md‎

Copy file name to clipboardExpand all lines: doc/api/crypto.md
+3Lines changed: 3 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -3533,6 +3533,9 @@ and it will be impossible to extract the private key from the returned object.
35333533
<!-- YAML
35343534
added: v11.6.0
35353535
changes:
3536+
- version: REPLACEME
3537+
pr-url: https://github.com/nodejs/node/pull/44201
3538+
description: The key can now be zero-length.
35363539
- version: v15.0.0
35373540
pr-url: https://github.com/nodejs/node/pull/35093
35383541
description: The key can also be an ArrayBuffer or string. The encoding
Collapse file

‎lib/internal/crypto/keys.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/keys.js
-3Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const {
3838
ERR_ILLEGAL_CONSTRUCTOR,
3939
ERR_INVALID_ARG_TYPE,
4040
ERR_INVALID_ARG_VALUE,
41-
ERR_OUT_OF_RANGE,
4241
}
4342
} = require('internal/errors');
4443

@@ -588,8 +587,6 @@ function prepareSecretKey(key, encoding, bufferOnly = false) {
588587

589588
function createSecretKey(key, encoding) {
590589
key = prepareSecretKey(key, encoding, true);
591-
if (key.byteLength === 0)
592-
throw new ERR_OUT_OF_RANGE('key.byteLength', '> 0', key.byteLength);
593590
const handle = new KeyObjectHandle();
594591
handle.init(kKeyTypeSecret, key);
595592
return new SecretKeyObject(handle);
Collapse file

‎src/crypto/crypto_keys.cc‎

Copy file name to clipboardExpand all lines: src/crypto/crypto_keys.cc
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,6 @@ void KeyObjectData::MemoryInfo(MemoryTracker* tracker) const {
872872
}
873873

874874
std::shared_ptr<KeyObjectData> KeyObjectData::CreateSecret(ByteSource key) {
875-
CHECK(key);
876875
return std::shared_ptr<KeyObjectData>(new KeyObjectData(std::move(key)));
877876
}
878877

Collapse file

‎test/parallel/test-crypto-hmac.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-hmac.js
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,12 @@ assert.strictEqual(
450450
() => crypto.createHmac('sha7', 'key'),
451451
/Invalid digest/);
452452
}
453+
454+
{
455+
const buf = Buffer.alloc(0);
456+
const keyObject = crypto.createSecretKey(Buffer.alloc(0));
457+
assert.deepStrictEqual(
458+
crypto.createHmac('sha256', buf).update('foo').digest(),
459+
crypto.createHmac('sha256', keyObject).update('foo').digest(),
460+
);
461+
}
Collapse file

‎test/parallel/test-crypto-key-objects.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-key-objects.js
+10-12Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@ const publicDsa = fixtures.readKey('dsa_public_1025.pem', 'ascii');
3333
const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
3434
'ascii');
3535

36-
{
37-
// Attempting to create an empty key should throw.
38-
assert.throws(() => {
39-
createSecretKey(Buffer.alloc(0));
40-
}, {
41-
name: 'RangeError',
42-
code: 'ERR_OUT_OF_RANGE',
43-
message: 'The value of "key.byteLength" is out of range. ' +
44-
'It must be > 0. Received 0'
45-
});
46-
}
47-
4836
{
4937
// Attempting to create a key of a wrong type should throw
5038
const TYPE = 'wrong_type';
@@ -870,3 +858,13 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
870858
assert(!first.privateKey.equals(second.privateKey));
871859
assert(!first.privateKey.equals(second.publicKey));
872860
}
861+
862+
{
863+
const first = createSecretKey(Buffer.alloc(0));
864+
const second = createSecretKey(new ArrayBuffer(0));
865+
const third = createSecretKey(Buffer.alloc(1));
866+
assert(first.equals(first));
867+
assert(first.equals(second));
868+
assert(!first.equals(third));
869+
assert(!third.equals(first));
870+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.