GeezerVison is showing github.com — crypto: fix various edge cases by panva · Pull Request #66237 · nodejs/node
crypto: fix various edge cases
#66237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
f82a804
crypto: use internal random buffer bounds
panva Sep 22, 2026
1cbd118
crypto: match algorithm names as ASCII
panva Sep 22, 2026
131cc41
crypto: reject short AES-KW inputs
panva Sep 22, 2026
0e286f0
crypto: isolate cipher job errors
panva Sep 22, 2026
5d64d76
crypto: convert getRandomValues views
panva Sep 22, 2026
bd27c23
crypto: validate normalized hash names
panva Sep 22, 2026
7129683
crypto: allow short AES-GCM IVs
panva Sep 22, 2026
3d8975b
crypto: allow unbound SubtleCrypto.supports
panva Sep 22, 2026
7f417a0
crypto: resolve supports overloads by type
panva Sep 22, 2026
9b6c75e
crypto: copy detached parameters as empty
panva Sep 22, 2026
a45414a
crypto: convert importKey data as a union
panva Sep 22, 2026
677404d
crypto: copy parameters without array species
panva Sep 22, 2026
4f7f16f
crypto: check RSA JWK alg with SHA-3 hashes
panva Sep 22, 2026
191d52a
crypto: reject duplicate unknown key usages
panva Sep 22, 2026
2ae6b20
crypto: handle PBKDF2 iteration limits
panva Sep 22, 2026
e937421
crypto: separate conversion from validation
panva Sep 22, 2026
36bb4f6
crypto: copy derived bits without species
panva Sep 22, 2026
716fc4e
crypto: minimize RSA public exponent metadata
panva Sep 22, 2026
02d6adf
crypto: create key usage arrays without species
panva Sep 22, 2026
6e9984c
crypto: derive Argon2 without worker threads
panva Sep 22, 2026
baaf5fc
crypto: report actual RSA modulus lengths
panva Sep 22, 2026
ac0931c
crypto: split hybrid keys without species
panva Sep 22, 2026
a95665b
crypto: use the public CryptoKey prototype
panva Sep 22, 2026
3e45b42
crypto: include EC public keys in PKCS8 exports
panva Sep 22, 2026
1f701b0
crypto: map JWK export failures to OperationError
panva Sep 22, 2026
36d932d
crypto: check EC coordinate conversion results
panva Sep 22, 2026
4a5c46f
benchmark: cover Web Crypto conversion costs
panva Sep 23, 2026
a216416
crypto: use backend cSHAKE and KMAC
panva Sep 23, 2026
4f26a35
crypto: verify empty KMAC outputs
panva Sep 23, 2026
File filter
Filter by extension
.js
(19)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
crypto: separate conversion from validation
Convert algorithm dictionaries on the original receiver, with name
read once. Validate normalized parameters in their operation steps
after the method-level key checks and generation usage checks.
This also makes Argon2 validation use converted parallelism and keeps
later dictionary conversion errors ahead of semantic parameter errors.
Signed-off-by: Filip Skokan <[email protected] >
Assisted-by: Codex
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
panva
committed
Sep 24, 2026
commit e937421ca77b74ff11cef1f252a4b0b01028a5f3
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -25,6 +25,7 @@ const {
getUsagesMask,
jobPromise,
getBufferSourceByteLength,
validateAlgorithm,
} = require('internal/crypto/util');
const {
Expand Down
Expand Up
@@ -173,6 +174,7 @@ function asyncAesOcbCipher(mode, key, data, algorithm) {
}
function aesCipher(mode, key, data, algorithm) {
validateAlgorithm(algorithm, 'encrypt');
switch (algorithm.name) {
case 'AES-CTR': return asyncAesCtrCipher(mode, key, data, algorithm);
case 'AES-CBC': return asyncAesCbcCipher(mode, key, data, algorithm);
Expand All
@@ -185,8 +187,9 @@ function aesCipher(mode, key, data, algorithm) {
function aesGenerateKey(algorithm, extractable, usages) {
const { name, length } = algorithm;
const usagesSet = validateUsagesNotEmpty(
validateKeyUsages(usages, kUsages[name], name));
const usagesSet = validateKeyUsages(usages, kUsages[name], name);
validateAlgorithm(algorithm, 'generateKey');
validateUsagesNotEmpty(usagesSet);
return jobPromise(() => new SecretKeyGenJob(
kCryptoJobWebCrypto,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -29,6 +29,7 @@ const {
const {
getArrayBufferOrView,
jobPromise,
validateAlgorithm,
} = require('internal/crypto/util');
const {
Expand Down
Expand Up
@@ -203,6 +204,7 @@ function validateArgon2DeriveBitsLength(length) {
function argon2DeriveBits(algorithm, baseKey, length) {
validateArgon2DeriveBitsLength(length);
validateAlgorithm(algorithm, 'deriveBits');
const type = {
'__proto__': null,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -23,6 +23,7 @@ const {
getUsagesMask,
jobPromise,
toUsagesSet,
validateAlgorithm,
} = require('internal/crypto/util');
const {
Expand Down
Expand Up
@@ -184,6 +185,8 @@ function eddsaSignVerify(key, data, algorithm, signature) {
if (getCryptoKeyType(key) !== type)
throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');
validateAlgorithm(algorithm, mode === kSignJobModeSign ? 'sign' : 'verify');
return jobPromise(() => new SignJob(
kCryptoJobWebCrypto,
mode,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -9,6 +9,7 @@ const {
const {
getUsagesMask,
jobPromise,
validateAlgorithm,
} = require('internal/crypto/util');
const {
Expand Down
Expand Up
@@ -36,6 +37,7 @@ function validateKeyLength(length) {
}
function c20pCipher(mode, key, data, algorithm) {
validateAlgorithm(algorithm, 'encrypt');
return jobPromise(() => new ChaCha20Poly1305CipherJob(
kCryptoJobWebCrypto,
mode,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -61,6 +61,7 @@ const {
numBitsToBytes,
toBuf,
truncateToBitLength,
validateAlgorithm,
kHandle,
} = require('internal/crypto/util');
Expand Down
Expand Up
@@ -332,6 +333,7 @@ function diffieHellman(options, callback) {
// The ecdhDeriveBits function is part of the Web Crypto API and serves both
// deriveKeys and deriveBits functions.
function ecdhDeriveBits(algorithm, baseKey, length) {
validateAlgorithm(algorithm, 'deriveBits');
const { 'public': key } = algorithm;
if (getCryptoKeyType(baseKey) !== 'private') {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -31,6 +31,7 @@ const {
getUsagesMask,
jobPromise,
normalizeHashName,
validateAlgorithm,
kNamedCurveAliases,
toUsagesSet,
} = require('internal/crypto/util');
Expand Down
Expand Up
@@ -72,6 +73,7 @@ function ecGenerateKey(algorithm, extractable, usages) {
const { name, namedCurve } = algorithm;
const allowedUsages = kUsages[name];
const usagesSet = validateKeyUsages(usages, allowedUsages.keygen, name);
validateAlgorithm(algorithm, 'generateKey');
const keyAlgorithm = { name, namedCurve };
const keyUsages = getKeyPairUsages(usagesSet, allowedUsages);
Expand Down
Expand Up
@@ -139,6 +141,7 @@ function ecImportKey(
usages,
) {
const { name, namedCurve } = algorithm;
validateAlgorithm(algorithm, 'importKey');
let handle;
const allowedUsages = kUsages[name];
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -27,6 +27,7 @@ const {
normalizeHashName,
numBitsToBytes,
truncateToBitLength,
validateAlgorithm,
validateMaxBufferLength,
kHandle,
getCachedHashId,
Expand Down
Expand Up
@@ -240,6 +241,7 @@ Hmac.prototype._transform = Hash.prototype._transform;
function asyncDigest(algorithm, data) {
validateMaxBufferLength(data, 'data');
validateAlgorithm(algorithm, 'digest');
switch (algorithm.name) {
case 'SHA-1':
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -29,6 +29,7 @@ const {
normalizeHashName,
toBuf,
validateByteSource,
validateAlgorithm,
} = require('internal/crypto/util');
const {
Expand Down
Expand Up
@@ -182,6 +183,7 @@ function validateHkdfDeriveBitsLength(length, hash) {
function hkdfDeriveBits(algorithm, baseKey, length) {
const { hash, salt, info } = algorithm;
validateHkdfDeriveBitsLength(length, hash);
validateAlgorithm(algorithm, 'deriveBits');
if (length === 0)
return PromiseResolve(new ArrayBuffer(0));
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -21,6 +21,7 @@ const {
numBitsToBytes,
truncateToBitLength,
validateKmacKeyLength,
validateAlgorithm,
} = require('internal/crypto/util');
const {
Expand Down
Expand Up
@@ -71,11 +72,12 @@ function hmacGenerateKey(algorithm, extractable, usages) {
const {
hash,
name,
length = getBlockSize(hash.name),
} = algorithm;
const usageSet = validateUsagesNotEmpty(
validateKeyUsages(usages, kUsages, name));
const usageSet = validateKeyUsages(usages, kUsages, name);
validateAlgorithm(algorithm, 'generateKey');
const { length = getBlockSize(hash.name) } = algorithm;
validateUsagesNotEmpty(usageSet);
return jobPromise(() => new SecretKeyGenJob(
kCryptoJobWebCrypto,
Expand All
@@ -95,8 +97,9 @@ function kmacGenerateKey(algorithm, extractable, usages) {
}[name],
} = algorithm;
const usageSet = validateUsagesNotEmpty(
validateKeyUsages(usages, kUsages, name));
const usageSet = validateKeyUsages(usages, kUsages, name);
validateAlgorithm(algorithm, 'generateKey');
validateUsagesNotEmpty(usageSet);
return jobPromise(() => new SecretKeyGenJob(
kCryptoJobWebCrypto,
Expand All
@@ -114,6 +117,7 @@ function macImportKey(
usages,
) {
const isHmac = algorithm.name === 'HMAC';
validateAlgorithm(algorithm, 'importKey');
const usagesSet = validateKeyUsages(
usages, kUsages, algorithm.name);
let handle;
Expand Down
Expand Up
@@ -181,6 +185,7 @@ function hmacSignVerify(key, data, algorithm, signature) {
}
function kmacSignVerify(key, data, algorithm, signature) {
validateAlgorithm(algorithm, 'sign');
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
return jobPromise(() => new KmacJob(
kCryptoJobWebCrypto,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -25,6 +25,7 @@ const {
getUsagesMask,
jobPromise,
toUsagesSet,
validateAlgorithm,
getBufferSourceByteLength,
} = require('internal/crypto/util');
Expand Down
Expand Up
@@ -197,6 +198,8 @@ function mlDsaSignVerify(key, data, algorithm, signature) {
if (getCryptoKeyType(key) !== type)
throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');
validateAlgorithm(algorithm, mode === kSignJobModeSign ? 'sign' : 'verify');
return jobPromise(() => new SignJob(
kCryptoJobWebCrypto,
mode,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -31,6 +31,7 @@ const {
getUsagesMask,
jobPromise,
normalizeHashName,
validateAlgorithm,
validateMaxBufferLength,
toUsagesSet,
} = require('internal/crypto/util');
Expand Down
Expand Up
@@ -110,6 +111,7 @@ function rsaKeyGenerate(
const allowedUsages = kUsages[name];
const usagesSet = validateKeyUsages(usages, allowedUsages.keygen, name);
validateAlgorithm(algorithm, 'generateKey');
const publicExponentConverted = bigIntArrayToUnsignedInt(publicExponent);
const keyAlgorithm = {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -820,8 +820,8 @@ function normalizeAlgorithm(algorithm, op) {
return { name: algName };
// 6.
const normalizedAlgorithm = webidl.converters [desiredType](
{ __proto__: algorithm, name: algName } ,
const normalizedAlgorithm = webidl.algorithmConverters [desiredType](
algorithm,
kNormalizeAlgorithmOpts,
);
// 7.
Expand Down
Expand Up
@@ -857,6 +857,16 @@ function normalizeAlgorithm(algorithm, op) {
return normalizedAlgorithm;
}
function validateAlgorithm(algorithm, op) {
// HMAC's get key length operation handles zero with a TypeError, while
// HmacImportParams otherwise uses the import operation's DataError.
if (op === 'get key length' && algorithm.name === 'HMAC')
return;
webidl ??= require('internal/crypto/webidl');
const desiredType = getSupportedAlgorithms().algorithms[op]?.[algorithm.name];
webidl.validators[desiredType]?.(algorithm);
}
function getDataViewOrTypedArrayBuffer(V) {
return isDataView(V) ?
DataViewPrototypeGetBuffer(V) : TypedArrayPrototypeGetBuffer(V);
Expand Down
Expand Up
@@ -1283,6 +1293,7 @@ module.exports = {
hasAnyNotIn,
validateByteSource,
validateKeyOps,
validateAlgorithm,
jobPromise,
jobPromiseThen,
cleanupWebCryptoResult,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -41,6 +41,7 @@ const {
codes: {
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_THIS,
},
} = require('internal/errors');
Expand Down
Expand Up
@@ -70,6 +71,7 @@ const {
normalizeHashName,
numBitsToBytes,
prepareWebCryptoResult,
validateAlgorithm,
validateMaxBufferLength,
getOptionalByteLength,
} = require('internal/crypto/util');
Expand Down
Expand Up
@@ -319,7 +321,9 @@ function deriveBitsImpl(algorithm, baseKey, length = null) {
}
}
function getKeyLength({ name, length, hash }) {
function getKeyLength(algorithm) {
validateAlgorithm(algorithm, 'get key length');
const { name, length, hash } = algorithm;
switch (name) {
case 'AES-CTR':
case 'AES-CBC':
Expand All
@@ -331,6 +335,8 @@ function getKeyLength({ name, length, hash }) {
return length;
case 'HMAC':
if (length === 0)
throw new ERR_INVALID_ARG_VALUE('algorithm.length', length, 'must not be zero');
if (length === undefined) {
return getBlockSize(hash?.name);
}
Expand Down
Expand Up
@@ -1869,6 +1875,7 @@ class SubtleCrypto {
normalizeAlgorithm(algorithm, 'get shared key length');
sharedKeyLength = getSharedKeyLength(normalizedAlgorithm);
normalizedAdditionalAlgorithm = normalizeAlgorithm(additionalAlgorithm, 'importKey');
validateAlgorithm(normalizedAdditionalAlgorithm, 'importKey');
} catch {
return false;
}
Expand Down
Expand Up
@@ -1897,6 +1904,7 @@ function check(op, alg, length) {
let normalizedAlgorithm;
try {
normalizedAlgorithm = normalizeAlgorithm(alg, op);
validateAlgorithm(normalizedAlgorithm, op);
} catch {
if (op === 'wrapKey') {
return check('encrypt', alg);
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.