Skip to content

Commit 6a479d8

Browse files
LiviaMedeirosjuanarbol
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 3abaf4d commit 6a479d8

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
@@ -3600,7 +3600,7 @@ added: v15.0.0
36003600
* `options`: {Object}
36013601
* `length`: {number} The bit length of the key to generate. This must be a
36023602
value greater than 0.
3603-
* If `type` is `'hmac'`, the minimum is 1, and the maximum length is
3603+
* If `type` is `'hmac'`, the minimum is 8, and the maximum length is
36043604
2<sup>31</sup>-1. If the value is not a multiple of 8, the generated
36053605
key will be truncated to `Math.floor(length / 8)`.
36063606
* If `type` is `'aes'`, the length must be one of `128`, `192`, or `256`.
@@ -3867,7 +3867,7 @@ added: v15.0.0
38673867
accepted values are `'hmac'` and `'aes'`.
38683868
* `options`: {Object}
38693869
* `length`: {number} The bit length of the key to generate.
3870-
* If `type` is `'hmac'`, the minimum is 1, and the maximum length is
3870+
* If `type` is `'hmac'`, the minimum is 8, and the maximum length is
38713871
2<sup>31</sup>-1. If the value is not a multiple of 8, the generated
38723872
key will be truncated to `Math.floor(length / 8)`.
38733873
* 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
@@ -356,7 +356,7 @@ function generateKeyJob(mode, keyType, options) {
356356
const { length } = options;
357357
switch (keyType) {
358358
case 'hmac':
359-
validateInteger(length, 'options.length', 1, 2 ** 31 - 1);
359+
validateInteger(length, 'options.length', 8, 2 ** 31 - 1);
360360
break;
361361
case 'aes':
362362
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)