GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [v26.x backport] crypto: fix various edge cases by panva · Pull Request #66391 · nodejs/node

[v26.x backport] crypto: fix various edge cases #66391

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.

Sign up for GitHub

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

Closed
panva wants to merge 2 commits into nodejs:v26.x-staging from panva:backport-66237-to-v26.x

Uh oh!

There was an error while loading. Please reload this page.

Closed

[v26.x backport] crypto: fix various edge cases #66391

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
de47538
crypto: separate conversion from validation
panva Sep 22, 2026
b81beca
crypto: use backend cSHAKE and KMAC
panva Sep 23, 2026
File filter
Filter by extension

Clear filters
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
Diff view
Next Next commit
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
PR-URL: #66237
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Aviv Keller <[email protected]>
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@panva
panva committed Sep 29, 2026
commit de47538f66acac25d9ff664fd20f517c60234846

There are no files selected for viewing

7 changes: 5 additions & 2 deletions 7 lib/internal/crypto/aes.js
Show comments
View file
Open in desktop
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
2 changes: 2 additions & 0 deletions 2 lib/internal/crypto/argon2.js
Show comments
View file
Open in desktop
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
3 changes: 3 additions & 0 deletions 3 lib/internal/crypto/cfrg.js
Show comments
View file
Open in desktop
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
2 changes: 2 additions & 0 deletions 2 lib/internal/crypto/chacha20_poly1305.js
Show comments
View file
Open in desktop
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
2 changes: 2 additions & 0 deletions 2 lib/internal/crypto/diffiehellman.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const {
numBitsToBytes,
toBuf,
truncateToBitLength,
validateAlgorithm,
kHandle,
} = require('internal/crypto/util');

Expand Down Expand Up @@ -372,6 +373,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
3 changes: 3 additions & 0 deletions 3 lib/internal/crypto/ec.js
Show comments
View file
Open in desktop
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
2 changes: 2 additions & 0 deletions 2 lib/internal/crypto/hash.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
normalizeHashName,
numBitsToBytes,
truncateToBitLength,
validateAlgorithm,
validateMaxBufferLength,
kHandle,
getCachedHashId,
Expand Down Expand Up @@ -257,6 +258,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
2 changes: 2 additions & 0 deletions 2 lib/internal/crypto/hkdf.js
Show comments
View file
Open in desktop
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
15 changes: 10 additions & 5 deletions 15 lib/internal/crypto/mac.js
Show comments
View file
Open in desktop
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
3 changes: 3 additions & 0 deletions 3 lib/internal/crypto/ml_dsa.js
Show comments
View file
Open in desktop
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
2 changes: 2 additions & 0 deletions 2 lib/internal/crypto/rsa.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const {
jobPromise,
jobPromiseThen,
normalizeHashName,
validateAlgorithm,
validateMaxBufferLength,
toUsagesSet,
} = require('internal/crypto/util');
Expand Down Expand Up @@ -113,6 +114,7 @@ function rsaKeyGenerate(

const allowedUsages = kUsages[name];
const usagesSet = validateKeyUsages(usages, allowedUsages.keygen, name);
validateAlgorithm(algorithm, 'generateKey');
const publicExponentConverted = bigIntArrayToUnsignedInt(publicExponent);
let firstNonzeroByte = 0;
while (publicExponent[firstNonzeroByte] === 0)
Expand Down
15 changes: 13 additions & 2 deletions 15 lib/internal/crypto/util.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,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 @@ -849,6 +849,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 @@ -1274,6 +1284,7 @@ module.exports = {
hasAnyNotIn,
validateByteSource,
validateKeyOps,
validateAlgorithm,
jobPromise,
jobPromiseThen,
cleanupWebCryptoResult,
Expand Down
10 changes: 9 additions & 1 deletion 10 lib/internal/crypto/webcrypto.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const {
codes: {
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_THIS,
},
} = require('internal/errors');
Expand Down Expand Up @@ -69,6 +70,7 @@ const {
normalizeHashName,
numBitsToBytes,
prepareWebCryptoResult,
validateAlgorithm,
validateMaxBufferLength,
getOptionalByteLength,
} = require('internal/crypto/util');
Expand Down Expand Up @@ -318,7 +320,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 @@ -330,6 +334,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 @@ -1874,6 +1880,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 @@ -1902,6 +1909,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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.