crypto: improve accuracy of SubtleCrypto.supports

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/63104
Backport-PR-URL: https://github.com/nodejs/node/pull/64629
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Filip Skokan authored and Antoine du Hamel committed 2026-07-30 13:51:52 +02:00
1 parent b9e17dc424
commit 934fda64b9
3 files changed
+65 -17

No files matched your search

+28 -14
View File
@@ -1928,7 +1928,11 @@ class SubtleCrypto {
}
}
return check(operation, algorithm, length);
try {
return check(operation, algorithm, length);
} catch {
return false;
}
}
}
@@ -1971,25 +1975,35 @@ function check(op, alg, length) {
return true;
case 'deriveBits': {
if (normalizedAlgorithm.name === 'HKDF') {
try {
require('internal/crypto/hkdf').validateHkdfDeriveBitsLength(length);
} catch {
return false;
}
require('internal/crypto/hkdf').validateHkdfDeriveBitsLength(length);
}
if (normalizedAlgorithm.name === 'PBKDF2') {
try {
require('internal/crypto/pbkdf2').validatePbkdf2DeriveBitsLength(length);
} catch {
return false;
}
require('internal/crypto/pbkdf2').validatePbkdf2DeriveBitsLength(length);
}
if (StringPrototypeStartsWith(normalizedAlgorithm.name, 'Argon2')) {
try {
require('internal/crypto/argon2').validateArgon2DeriveBitsLength(length);
} catch {
require('internal/crypto/argon2').validateArgon2DeriveBitsLength(length);
}
if (normalizedAlgorithm.name === 'X25519' && length > 256) {
return false;
}
if (normalizedAlgorithm.name === 'X448' && length > 448) {
return false;
}
if (normalizedAlgorithm.name === 'ECDH') {
const namedCurve = getCryptoKeyAlgorithm(normalizedAlgorithm.public).namedCurve;
const maxLength = {
'__proto__': null,
'P-256': 256,
'P-384': 384,
'P-521': 528,
}[namedCurve];
if (length > maxLength) {
return false;
}
}
+28 -2
View File
@@ -103,18 +103,36 @@ export const vectors = {
[true,
{ name: 'X25519', public: X25519.publicKey },
{ name: 'AES-CBC', length: 128 }],
[true,
[false,
{ name: 'X25519', public: X25519.publicKey },
{ name: 'HMAC', hash: 'SHA-256' }],
[true,
{ name: 'X25519', public: X25519.publicKey },
{ name: 'HMAC', hash: 'SHA-256', length: 256 }],
[false,
{ name: 'X25519', public: X25519.publicKey },
{ name: 'HMAC', hash: 'SHA-256', length: 257 }],
[true,
{ name: 'X25519', public: X25519.publicKey },
'HKDF'],
[true,
{ name: 'ECDH', public: ECDH.publicKey },
{ name: 'AES-CBC', length: 128 }],
[true,
[false,
{ name: 'ECDH', public: ECDH.publicKey },
{ name: 'HMAC', hash: 'SHA-256' }],
[false,
{ name: 'ECDH', public: ECDH.publicKey },
{ name: 'HMAC', hash: 'SHA-256', length: 255 }],
[true,
{ name: 'ECDH', public: ECDH.publicKey },
{ name: 'HMAC', hash: 'SHA-256', length: 256 }],
[false,
{ name: 'ECDH', public: ECDH.publicKey },
{ name: 'HMAC', hash: 'SHA-256', length: 257 }],
[false,
{ name: 'ECDH', public: ECDH.publicKey },
{ name: 'HMAC', hash: 'SHA-256', length: 264 }],
[true,
{ name: 'ECDH', public: ECDH.publicKey },
'HKDF'],
@@ -143,10 +161,18 @@ export const vectors = {
[true,
{ name: 'ECDH', public: ECDH.publicKey }],
[true,
{ name: 'ECDH', public: ECDH.publicKey }, 256],
[false,
{ name: 'ECDH', public: ECDH.publicKey }, 257],
[false,
{ name: 'ECDH', public: ECDH.publicKey }, 264],
[false, { name: 'ECDH', public: ECDH.privateKey }],
[false, 'ECDH'],
[true, { name: 'X25519', public: X25519.publicKey }],
[true, { name: 'X25519', public: X25519.publicKey }, 256],
[false, { name: 'X25519', public: X25519.publicKey }, 257],
[false, { name: 'X25519', public: X25519.privateKey }],
[false, 'X25519'],
],
+9 -1
View File
@@ -28,15 +28,23 @@ export const vectors = {
[!boringSSL,
{ name: 'X448', public: X448?.publicKey },
{ name: 'AES-CBC', length: 128 }],
[!boringSSL,
[false,
{ name: 'X448', public: X448?.publicKey },
{ name: 'HMAC', hash: 'SHA-256' }],
[!boringSSL,
{ name: 'X448', public: X448?.publicKey },
{ name: 'HMAC', hash: 'SHA-256', length: 448 }],
[false,
{ name: 'X448', public: X448?.publicKey },
{ name: 'HMAC', hash: 'SHA-256', length: 449 }],
[!boringSSL,
{ name: 'X448', public: X448?.publicKey },
'HKDF'],
],
'deriveBits': [
[!boringSSL, { name: 'X448', public: X448?.publicKey }],
[!boringSSL, { name: 'X448', public: X448?.publicKey }, 448],
[false, { name: 'X448', public: X448?.publicKey }, 449],
[false, { name: 'X448', public: X25519.publicKey }],
[false, { name: 'X448', public: X448?.privateKey }],
[false, 'X448'],