Skip to content

Commit a0e11d7

Browse files
authored
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 c3b84f1 commit a0e11d7

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
@@ -3391,7 +3391,11 @@ On recent releases of OpenSSL, `openssl list -digest-algorithms` will
33913391
display the available digest algorithms.
33923392

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

33963400
Example: generating the sha256 HMAC of a file
33973401

@@ -3665,6 +3669,9 @@ generateKey('hmac', { length: 512 }, (err, key) => {
36653669
});
36663670
```
36673671

3672+
The size of a generated HMAC key should not exceed the block size of the
3673+
underlying hash function. See [`crypto.createHmac()`][] for more information.
3674+
36683675
### `crypto.generateKeyPair(type, options, callback)`
36693676

36703677
<!-- YAML
@@ -3935,6 +3942,9 @@ const key = generateKeySync('hmac', { length: 512 });
39353942
console.log(key.export().toString('hex')); // e89..........41e
39363943
```
39373944

3945+
The size of a generated HMAC key should not exceed the block size of the
3946+
underlying hash function. See [`crypto.createHmac()`][] for more information.
3947+
39383948
### `crypto.generatePrime(size[, options[, callback]])`
39393949

39403950
<!-- YAML
@@ -6030,6 +6040,7 @@ See the [list of SSL OP Flags][] for details.
60306040
[`crypto.createSecretKey()`]: #cryptocreatesecretkeykey-encoding
60316041
[`crypto.createSign()`]: #cryptocreatesignalgorithm-options
60326042
[`crypto.createVerify()`]: #cryptocreateverifyalgorithm-options
6043+
[`crypto.generateKey()`]: #cryptogeneratekeytype-options-callback
60336044
[`crypto.getCurves()`]: #cryptogetcurves
60346045
[`crypto.getDiffieHellman()`]: #cryptogetdiffiehellmangroupname
60356046
[`crypto.getHashes()`]: #cryptogethashes

0 commit comments

Comments
 (0)