diff --git a/lib/internal/crypto/hkdf.js b/lib/internal/crypto/hkdf.js index d5596890754..fcff7070164 100644 --- a/lib/internal/crypto/hkdf.js +++ b/lib/internal/crypto/hkdf.js @@ -22,7 +22,6 @@ const { const { kMaxLength } = require('buffer'); const { - getDigestSizeInBytes, jobPromise, normalizeHashName, toBuf, @@ -142,6 +141,22 @@ function hkdfSync(hash, key, salt, info, length) { return bits; } +function getDigestSizeInBytes(name) { + switch (name) { + case 'SHA-1': + return 20; + case 'SHA-256': // Fall through + case 'SHA3-256': + return 32; + case 'SHA-384': // Fall through + case 'SHA3-384': + return 48; + case 'SHA-512': // Fall through + case 'SHA3-512': + return 64; + } +} + function validateHkdfDeriveBitsLength(length, hash) { if (length === null) throw lazyDOMException('length cannot be null', 'OperationError'); diff --git a/lib/internal/crypto/rsa.js b/lib/internal/crypto/rsa.js index 846b6b7bf74..6de51f6ecf6 100644 --- a/lib/internal/crypto/rsa.js +++ b/lib/internal/crypto/rsa.js @@ -1,7 +1,6 @@ 'use strict'; const { - MathCeil, TypedArrayPrototypeGetBuffer, Uint8Array, } = primordials; @@ -24,12 +23,11 @@ const { } = internalBinding('crypto'); const { - validateInt32, + isInt32, } = require('internal/validators'); const { bigIntArrayToUnsignedInt, - getDigestSizeInBytes, getUsagesMask, jobPromise, normalizeHashName, @@ -241,19 +239,16 @@ function rsaSignVerify(key, data, { saltLength }, signature) { throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError'); const algorithm = getCryptoKeyAlgorithm(key); - if (algorithm.name === 'RSA-PSS') { - try { - validateInt32( - saltLength, - 'algorithm.saltLength', - 0, - MathCeil((algorithm.modulusLength - 1) / 8) - - getDigestSizeInBytes(algorithm.hash.name) - 2); - } catch (err) { - throw lazyDOMException( - 'The operation failed for an operation-specific reason', - { name: 'OperationError', cause: err }); - } + // RsaPssParams converts saltLength to an unsigned long, but SignJob only + // accepts int32 values. + if (algorithm.name === 'RSA-PSS' && !isInt32(saltLength)) { + // EMSA-PSS-VERIFY treats an impossible salt length as inconsistent. + if (mode === kSignJobModeVerify) + return false; + + throw lazyDOMException( + 'The operation failed for an operation-specific reason', + 'OperationError'); } return jobPromise(() => new SignJob( diff --git a/lib/internal/crypto/util.js b/lib/internal/crypto/util.js index 01b14e0a35c..9cb0ae63d44 100644 --- a/lib/internal/crypto/util.js +++ b/lib/internal/crypto/util.js @@ -1058,22 +1058,6 @@ function getBlockSize(name) { } } -function getDigestSizeInBytes(name) { - switch (name) { - case 'SHA-1': - return 20; - case 'SHA-256': // Fall through - case 'SHA3-256': - return 32; - case 'SHA-384': // Fall through - case 'SHA3-384': - return 48; - case 'SHA-512': // Fall through - case 'SHA3-512': - return 64; - } -} - function validateKeyOps(keyOps, usagesSet) { if (keyOps === undefined) return; validateArray(keyOps, 'keyData.key_ops'); @@ -1145,7 +1129,6 @@ module.exports = { bigIntArrayToUnsignedBigInt, bigIntArrayToUnsignedInt, getBlockSize, - getDigestSizeInBytes, getStringOption, getUsagesMask, getUsagesFromMask, diff --git a/test/parallel/test-webcrypto-sign-verify-rsa.js b/test/parallel/test-webcrypto-sign-verify-rsa.js index 0ccbf431f14..3f8a916846c 100644 --- a/test/parallel/test-webcrypto-sign-verify-rsa.js +++ b/test/parallel/test-webcrypto-sign-verify-rsa.js @@ -205,22 +205,28 @@ async function testSaltLength(keyLength, hash, hLen) { const data = Buffer.from('Hello, world!'); const max = keyLength / 8 - hLen - 2; - const signature = await subtle.sign({ name: 'RSA-PSS', saltLength: max }, privateKey, data); - await assert.rejects( - subtle.sign({ name: 'RSA-PSS', saltLength: max + 1 }, privateKey, data), (err) => { - assert.strictEqual(err.name, 'OperationError'); - assert.strictEqual(err.cause?.code, 'ERR_OUT_OF_RANGE'); - assert.strictEqual(err.cause?.message, `The value of "algorithm.saltLength" is out of range. It must be >= 0 && <= ${max}. Received ${max + 1}`); - return true; - }); - await subtle.verify({ name: 'RSA-PSS', saltLength: max }, publicKey, signature, data); - await assert.rejects( - subtle.verify({ name: 'RSA-PSS', saltLength: max + 1 }, publicKey, signature, data), (err) => { - assert.strictEqual(err.name, 'OperationError'); - assert.strictEqual(err.cause?.code, 'ERR_OUT_OF_RANGE'); - assert.strictEqual(err.cause?.message, `The value of "algorithm.saltLength" is out of range. It must be >= 0 && <= ${max}. Received ${max + 1}`); - return true; - }); + const signature = await subtle.sign( + { name: 'RSA-PSS', saltLength: max }, privateKey, data); + assert.strictEqual(await subtle.verify( + { name: 'RSA-PSS', saltLength: max }, publicKey, signature, data), true); + + for (const saltLength of [max + 1, 0x7fffffff]) { + await assert.rejects( + subtle.sign({ name: 'RSA-PSS', saltLength }, privateKey, data), { + name: 'OperationError', + }); + assert.strictEqual(await subtle.verify( + { name: 'RSA-PSS', saltLength }, publicKey, signature, data), false); + } + + for (const saltLength of [0x80000000, 0xffffffff]) { + await assert.rejects( + subtle.sign({ name: 'RSA-PSS', saltLength }, privateKey, data), { + name: 'OperationError', + }); + assert.strictEqual(await subtle.verify( + { name: 'RSA-PSS', saltLength }, publicKey, signature, data), false); + } } (async function() {