Skip to content

Commit 4600892

Browse files
LiviaMedeirostargos
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 42783cc commit 4600892

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
@@ -3584,7 +3584,7 @@ added: v15.0.0
35843584
* `options`: {Object}
35853585
* `length`: {number} The bit length of the key to generate. This must be a
35863586
value greater than 0.
3587-
* If `type` is `'hmac'`, the minimum is 1, and the maximum length is
3587+
* If `type` is `'hmac'`, the minimum is 8, and the maximum length is
35883588
2<sup>31</sup>-1. If the value is not a multiple of 8, the generated
35893589
key will be truncated to `Math.floor(length / 8)`.
35903590
* If `type` is `'aes'`, the length must be one of `128`, `192`, or `256`.
@@ -3851,7 +3851,7 @@ added: v15.0.0
38513851
accepted values are `'hmac'` and `'aes'`.
38523852
* `options`: {Object}
38533853
* `length`: {number} The bit length of the key to generate.
3854-
* If `type` is `'hmac'`, the minimum is 1, and the maximum length is
3854+
* If `type` is `'hmac'`, the minimum is 8, and the maximum length is
38553855
2<sup>31</sup>-1. If the value is not a multiple of 8, the generated
38563856
key will be truncated to `Math.floor(length / 8)`.
38573857
* 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)