Skip to content

Commit 183bcc0

Browse files
tniessenRafaelGSS
authored andcommitted
crypto: clean up parameter validation in HKDF
PR-URL: #42924 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Zeyu Yang <[email protected]>
1 parent 924670f commit 183bcc0

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

lib/internal/crypto/hkdf.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,10 @@ const {
5353
} = require('internal/errors');
5454

5555
const validateParameters = hideStackFrames((hash, key, salt, info, length) => {
56-
key = prepareKey(key);
57-
salt = toBuf(salt);
58-
info = toBuf(info);
59-
6056
validateString(hash, 'digest');
61-
validateByteSource(salt, 'salt');
62-
validateByteSource(info, 'info');
57+
key = prepareKey(key);
58+
salt = validateByteSource(salt, 'salt');
59+
info = validateByteSource(info, 'info');
6360

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

lib/internal/crypto/util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ const validateByteSource = hideStackFrames((val, name) => {
271271
val = toBuf(val);
272272

273273
if (isAnyArrayBuffer(val) || isArrayBufferView(val))
274-
return;
274+
return val;
275275

276276
throw new ERR_INVALID_ARG_TYPE(
277277
name,

test/parallel/test-crypto-hkdf.js

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const {
1515
} = require('crypto');
1616

1717
{
18+
assert.throws(() => hkdf(), {
19+
code: 'ERR_INVALID_ARG_TYPE',
20+
message: /The "digest" argument must be of type string/
21+
});
22+
1823
[1, {}, [], false, Infinity].forEach((i) => {
1924
assert.throws(() => hkdf(i, 'a'), {
2025
code: 'ERR_INVALID_ARG_TYPE',

0 commit comments

Comments
 (0)