crypto: validate the limit of PBKDF2 iterations

Reject iteration counts outside of OpenSSL supported range

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65704
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
Filip Skokan authored and Antoine du Hamel committed 2026-09-16 21:12:05 +02:00
1 parent cd5a7611e9
commit e44669bb7d
3 files changed
+32

No files matched your search

+6
View File
@@ -16,6 +16,7 @@ const {
kEmptyObject,
} = require('internal/util');
const {
isInt32,
isUint32,
} = require('internal/validators');
const {
@@ -513,6 +514,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,
},
+4
View File
@@ -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'],
@@ -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],
@@ -120,6 +120,28 @@ const rejectsXCurves = hasFIPS(3, 5);
}
}
// Test PBKDF2 rejects iteration counts beyond the native signed int range
{
async function test() {
const key = await subtle.importKey(
'raw',
new Uint8Array([1]),
'PBKDF2',
false,
['deriveBits']);
await assert.rejects(
subtle.deriveBits({
name: 'PBKDF2',
hash: 'SHA-256',
salt: new Uint8Array([2]),
iterations: 2 ** 31,
}, key, 8),
{ name: 'NotSupportedError' });
}
test().then(common.mustCall());
}
// Test X25519 and X448 bit derivation
{
async function test(name) {