Skip to content

Commit 07d90c8

Browse files
panvaruyadorno
authored andcommitted
crypto: allow zero-length secret KeyObject
PR-URL: #44201 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent ac2b10e commit 07d90c8

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

doc/api/crypto.md

+3
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

lib/internal/crypto/keys.js

-3
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);

src/crypto/crypto_keys.cc

-1
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

test/parallel/test-crypto-hmac.js

+9
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+
}

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

+10-12
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)