Skip to content

Commit e5c9975

Browse files
panvajuanarbol
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]> Backport-PR-URL: #44872
1 parent 7e705d8 commit e5c9975

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
@@ -3530,6 +3530,9 @@ and it will be impossible to extract the private key from the returned object.
35303530
<!-- YAML
35313531
added: v11.6.0
35323532
changes:
3533+
- version: REPLACEME
3534+
pr-url: https://github.com/nodejs/node/pull/44201
3535+
description: The key can now be zero-length.
35333536
- version: v15.0.0
35343537
pr-url: https://github.com/nodejs/node/pull/35093
35353538
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

@@ -592,8 +591,6 @@ function prepareSecretKey(key, encoding, bufferOnly = false) {
592591

593592
function createSecretKey(key, encoding) {
594593
key = prepareSecretKey(key, encoding, true);
595-
if (key.byteLength === 0)
596-
throw new ERR_OUT_OF_RANGE('key.byteLength', '> 0', key.byteLength);
597594
const handle = new KeyObjectHandle();
598595
handle.init(kKeyTypeSecret, key);
599596
return new SecretKeyObject(handle);

src/crypto/crypto_keys.cc

-1
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,6 @@ void KeyObjectData::MemoryInfo(MemoryTracker* tracker) const {
862862
}
863863

864864
std::shared_ptr<KeyObjectData> KeyObjectData::CreateSecret(ByteSource key) {
865-
CHECK(key);
866865
return std::shared_ptr<KeyObjectData>(new KeyObjectData(std::move(key)));
867866
}
868867

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';
@@ -871,6 +859,16 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
871859
assert(!first.privateKey.equals(second.publicKey));
872860
}
873861

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+
}
871+
874872
{
875873
// This should not cause a crash: https://github.com/nodejs/node/issues/44471
876874
for (const key of ['', 'foo', null, undefined, true, Boolean]) {

0 commit comments

Comments
 (0)