Skip to content

Commit

Permalink
crypto: clean up parameter validation in HKDF
Browse files Browse the repository at this point in the history
PR-URL: #42924
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
  • Loading branch information
tniessen authored and targos committed Jul 12, 2022
1 parent 4e87d34 commit 0038348
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
9 changes: 3 additions & 6 deletions lib/internal/crypto/hkdf.js
Expand Up @@ -53,13 +53,10 @@ const {
} = require('internal/errors');

const validateParameters = hideStackFrames((hash, key, salt, info, length) => {
key = prepareKey(key);
salt = toBuf(salt);
info = toBuf(info);

validateString(hash, 'digest');
validateByteSource(salt, 'salt');
validateByteSource(info, 'info');
key = prepareKey(key);
salt = validateByteSource(salt, 'salt');
info = validateByteSource(info, 'info');

validateInteger(length, 'length', 0, kMaxLength);

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/util.js
Expand Up @@ -271,7 +271,7 @@ const validateByteSource = hideStackFrames((val, name) => {
val = toBuf(val);

if (isAnyArrayBuffer(val) || isArrayBufferView(val))
return;
return val;

throw new ERR_INVALID_ARG_TYPE(
name,
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-crypto-hkdf.js
Expand Up @@ -15,6 +15,11 @@ const {
} = require('crypto');

{
assert.throws(() => hkdf(), {
code: 'ERR_INVALID_ARG_TYPE',
message: /The "digest" argument must be of type string/
});

[1, {}, [], false, Infinity].forEach((i) => {
assert.throws(() => hkdf(i, 'a'), {
code: 'ERR_INVALID_ARG_TYPE',
Expand Down

0 comments on commit 0038348

Please sign in to comment.