Skip to content

Commit d14018e

Browse files
tniessenMoLow
authored andcommitted
doc: improve HMAC key recommendations
Add a reference to potential problems with using strings as HMAC keys. Also advise against exceeding the underlying hash function's block size when generating HMAC keys from a cryptographically secure source of entropy. Refs: #48052 Refs: #37248 PR-URL: #48121 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent e9d4baf commit d14018e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

doc/api/crypto.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -3410,7 +3410,11 @@ On recent releases of OpenSSL, `openssl list -digest-algorithms` will
34103410
display the available digest algorithms.
34113411

34123412
The `key` is the HMAC key used to generate the cryptographic HMAC hash. If it is
3413-
a [`KeyObject`][], its type must be `secret`.
3413+
a [`KeyObject`][], its type must be `secret`. If it is a string, please consider
3414+
[caveats when using strings as inputs to cryptographic APIs][]. If it was
3415+
obtained from a cryptographically secure source of entropy, such as
3416+
[`crypto.randomBytes()`][] or [`crypto.generateKey()`][], its length should not
3417+
exceed the block size of `algorithm` (e.g., 512 bits for SHA-256).
34143418

34153419
Example: generating the sha256 HMAC of a file
34163420

@@ -3682,6 +3686,9 @@ generateKey('hmac', { length: 512 }, (err, key) => {
36823686
});
36833687
```
36843688

3689+
The size of a generated HMAC key should not exceed the block size of the
3690+
underlying hash function. See [`crypto.createHmac()`][] for more information.
3691+
36853692
### `crypto.generateKeyPair(type, options, callback)`
36863693

36873694
<!-- YAML
@@ -3952,6 +3959,9 @@ const key = generateKeySync('hmac', { length: 512 });
39523959
console.log(key.export().toString('hex')); // e89..........41e
39533960
```
39543961

3962+
The size of a generated HMAC key should not exceed the block size of the
3963+
underlying hash function. See [`crypto.createHmac()`][] for more information.
3964+
39553965
### `crypto.generatePrime(size[, options[, callback]])`
39563966

39573967
<!-- YAML
@@ -6048,6 +6058,7 @@ See the [list of SSL OP Flags][] for details.
60486058
[`crypto.createSecretKey()`]: #cryptocreatesecretkeykey-encoding
60496059
[`crypto.createSign()`]: #cryptocreatesignalgorithm-options
60506060
[`crypto.createVerify()`]: #cryptocreateverifyalgorithm-options
6061+
[`crypto.generateKey()`]: #cryptogeneratekeytype-options-callback
60516062
[`crypto.getCurves()`]: #cryptogetcurves
60526063
[`crypto.getDiffieHellman()`]: #cryptogetdiffiehellmangroupname
60536064
[`crypto.getHashes()`]: #cryptogethashes

0 commit comments

Comments
 (0)