Skip to content

Commit 19c060f

Browse files
LiviaMedeirosRafaelGSS
authored andcommitted
crypto: adjust minimum length in generateKey('hmac', ...)
Also affects generateKeySync('hmac', ...) PR-URL: #42944 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent d746207 commit 19c060f

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

doc/api/crypto.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3624,7 +3624,7 @@ changes:
36243624
* `options`: {Object}
36253625
* `length`: {number} The bit length of the key to generate. This must be a
36263626
value greater than 0.
3627-
* If `type` is `'hmac'`, the minimum is 1, and the maximum length is
3627+
* If `type` is `'hmac'`, the minimum is 8, and the maximum length is
36283628
2<sup>31</sup>-1. If the value is not a multiple of 8, the generated
36293629
key will be truncated to `Math.floor(length / 8)`.
36303630
* If `type` is `'aes'`, the length must be one of `128`, `192`, or `256`.
@@ -3896,7 +3896,7 @@ added: v15.0.0
38963896
accepted values are `'hmac'` and `'aes'`.
38973897
* `options`: {Object}
38983898
* `length`: {number} The bit length of the key to generate.
3899-
* If `type` is `'hmac'`, the minimum is 1, and the maximum length is
3899+
* If `type` is `'hmac'`, the minimum is 8, and the maximum length is
39003900
2<sup>31</sup>-1. If the value is not a multiple of 8, the generated
39013901
key will be truncated to `Math.floor(length / 8)`.
39023902
* If `type` is `'aes'`, the length must be one of `128`, `192`, or `256`.

lib/internal/crypto/keygen.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ function generateKeyJob(mode, keyType, options) {
349349
const { length } = options;
350350
switch (keyType) {
351351
case 'hmac':
352-
validateInteger(length, 'options.length', 1, 2 ** 31 - 1);
352+
validateInteger(length, 'options.length', 8, 2 ** 31 - 1);
353353
break;
354354
case 'aes':
355355
validateOneOf(length, 'options.length', kAesKeyLengths);

test/parallel/test-crypto-secret-keygen.js

+16
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ assert.throws(() => generateKey('hmac', { length: -1 }, common.mustNotCall()), {
5151
code: 'ERR_OUT_OF_RANGE'
5252
});
5353

54+
assert.throws(() => generateKey('hmac', { length: 4 }, common.mustNotCall()), {
55+
code: 'ERR_OUT_OF_RANGE'
56+
});
57+
58+
assert.throws(() => generateKey('hmac', { length: 7 }, common.mustNotCall()), {
59+
code: 'ERR_OUT_OF_RANGE'
60+
});
61+
5462
assert.throws(
5563
() => generateKey('hmac', { length: 2 ** 31 }, common.mustNotCall()), {
5664
code: 'ERR_OUT_OF_RANGE'
@@ -60,6 +68,14 @@ assert.throws(() => generateKeySync('hmac', { length: -1 }), {
6068
code: 'ERR_OUT_OF_RANGE'
6169
});
6270

71+
assert.throws(() => generateKeySync('hmac', { length: 4 }), {
72+
code: 'ERR_OUT_OF_RANGE'
73+
});
74+
75+
assert.throws(() => generateKeySync('hmac', { length: 7 }), {
76+
code: 'ERR_OUT_OF_RANGE'
77+
});
78+
6379
assert.throws(
6480
() => generateKeySync('hmac', { length: 2 ** 31 }), {
6581
code: 'ERR_OUT_OF_RANGE'

0 commit comments

Comments
 (0)