crypto: ensure HKDF and PBKDF2 limits are validated by panva · Pull Request #65704 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions lib/internal/crypto/hkdf.js
6 changes: 6 additions & 0 deletions lib/internal/crypto/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
lazyDOMException,
} = require('internal/util');
const {
isInt32,
isUint32,
} = require('internal/validators');
const { CryptoKey } = require('internal/crypto/webcrypto');
Expand Down Expand Up @@ -489,6 +490,11 @@ converters.Pbkdf2Params = createDictionaryConverter(
validator: (V, dict) => {
if (V === 0)
throw lazyDOMException('iterations cannot be zero', 'OperationError');
if (!isInt32(V)) {
throw lazyDOMException(
'iterations exceeds the implementation limit',
'NotSupportedError');
}
},
required: true,
},
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/webcrypto/supports-level-2.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ export const vectors = {
[true,
{ name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 },
{ name: 'HMAC', hash: 'SHA-256' }],
[false,
{ name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 2 ** 31 },
{ name: 'AES-CBC', length: 128 }],
[false,
{ name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 },
'HKDF'],
Expand Down Expand Up @@ -183,6 +186,7 @@ export const vectors = {
[true, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, 8],
[true, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, 0],
[false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 0 }, 8],
[false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 2 ** 31 }, 8],
[false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, null],
[false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, 7],
[false, { name: 'PBKDF2', hash: 'Invalid', salt: Buffer.alloc(0), iterations: 1 }, 8],
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-crypto-hkdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ const { hasOpenSSL } = require('../common/crypto');
code: 'ERR_OUT_OF_RANGE'
});

{
const info = new Uint8Array(2048);
Object.defineProperty(info, 'byteLength', {
__proto__: null,
get() {
return 1;
},
});

const calls = [
() => hkdf('sha256', 'a', '', info, 10, common.mustNotCall()),
() => hkdfSync('sha256', 'a', '', info, 10),
];
for (const call of calls)
assert.throws(call, { code: 'ERR_OUT_OF_RANGE' });
}

assert.throws(
() => hkdf('sha512', 'a', '', '', 64 * 255 + 1, common.mustNotCall()), {
code: 'ERR_CRYPTO_INVALID_KEYLEN'
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-webcrypto-derivebits.js
Loading