Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
ArrayFrom,
ArrayPrototypePush,
SafeSet,
} = primordials;
Expand Down Expand Up @@ -210,7 +209,7 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) {
return new InternalCryptoKey(
key,
{ name, length },
ArrayFrom(usagesSet),
usagesSet,
extractable);
}

Expand Down Expand Up @@ -285,7 +284,7 @@ function aesImportKey(
return new InternalCryptoKey(
keyObject,
{ name, length },
keyUsages,
usagesSet,
extractable);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/cfrg.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) {
case 'X25519':
// Fall through
case 'X448':
publicUsages = [];
publicUsages = new SafeSet();
privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits');
break;
}
Expand Down Expand Up @@ -245,7 +245,7 @@ function cfrgImportKey(
return new InternalCryptoKey(
keyObject,
{ name },
keyUsages,
usagesSet,
extractable);
}

Expand Down
5 changes: 2 additions & 3 deletions lib/internal/crypto/chacha20_poly1305.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
ArrayFrom,
SafeSet,
} = primordials;

Expand Down Expand Up @@ -81,7 +80,7 @@ async function c20pGenerateKey(algorithm, extractable, keyUsages) {
return new InternalCryptoKey(
createSecretKey(keyData),
{ name },
ArrayFrom(usagesSet),
usagesSet,
extractable);
}

Expand Down Expand Up @@ -140,7 +139,7 @@ function c20pImportKey(
return new InternalCryptoKey(
keyObject,
{ name },
keyUsages,
usagesSet,
extractable);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/ec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
privateUsages = getUsagesUnion(usageSet, 'sign');
break;
case 'ECDH':
publicUsages = [];
publicUsages = new SafeSet();
privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits');
break;
}
Expand Down Expand Up @@ -271,7 +271,7 @@ function ecImportKey(
return new InternalCryptoKey(
keyObject,
{ name, namedCurve },
keyUsages,
usagesSet,
extractable);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const {
bigIntArrayToUnsignedBigInt,
normalizeAlgorithm,
hasAnyNotIn,
getSortedUsages,
} = require('internal/crypto/util');

const {
Expand Down Expand Up @@ -901,7 +902,7 @@ class InternalCryptoKey {
keyObject,
algorithm,
extractable,
keyUsages,
getSortedUsages(new SafeSet(keyUsages)),
);
}
}
Expand Down Expand Up @@ -967,7 +968,7 @@ function importGenericSecretKey(
return undefined;
}

return new InternalCryptoKey(keyObject, { name }, keyUsages, false);
return new InternalCryptoKey(keyObject, { name }, usagesSet, false);
}

module.exports = {
Expand Down
7 changes: 3 additions & 4 deletions lib/internal/crypto/mac.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
ArrayFrom,
SafeSet,
StringPrototypeSubstring,
} = primordials;
Expand Down Expand Up @@ -81,7 +80,7 @@ async function hmacGenerateKey(algorithm, extractable, keyUsages) {
return new InternalCryptoKey(
key,
{ name, length, hash },
ArrayFrom(usageSet),
usageSet,
extractable);
}

Expand Down Expand Up @@ -115,7 +114,7 @@ async function kmacGenerateKey(algorithm, extractable, keyUsages) {
return new InternalCryptoKey(
createSecretKey(keyData),
{ name, length },
ArrayFrom(usageSet),
usageSet,
extractable);
}

Expand Down Expand Up @@ -196,7 +195,7 @@ function macImportKey(
return new InternalCryptoKey(
keyObject,
algorithmObject,
keyUsages,
usagesSet,
extractable);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/ml_dsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function mlDsaImportKey(
return new InternalCryptoKey(
keyObject,
{ name },
keyUsages,
usagesSet,
extractable);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/internal/crypto/ml_kem.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ async function mlKemGenerateKey(algorithm, extractable, keyUsages) {
{ name: 'OperationError', cause: err });
}

const publicUsages = getUsagesUnion(usageSet, 'encapsulateBits', 'encapsulateKey');
const privateUsages = getUsagesUnion(usageSet, 'decapsulateBits', 'decapsulateKey');
const publicUsages = getUsagesUnion(usageSet, 'encapsulateKey', 'encapsulateBits');
const privateUsages = getUsagesUnion(usageSet, 'decapsulateKey', 'decapsulateBits');

const keyAlgorithm = { name };

Expand Down Expand Up @@ -202,7 +202,7 @@ function mlKemImportKey(
return new InternalCryptoKey(
keyObject,
{ name },
keyUsages,
usagesSet,
extractable);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function rsaImportKey(
modulusLength,
publicExponent: new Uint8Array(publicExponent),
hash: algorithm.hash,
}, keyUsages, extractable);
}, usagesSet, extractable);
}

async function rsaSignVerify(key, data, { saltLength }, signature) {
Expand Down
33 changes: 31 additions & 2 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayBufferIsView,
ArrayBufferPrototypeGetByteLength,
ArrayFrom,
ArrayPrototypeIncludes,
ArrayPrototypePush,
BigInt,
Expand All @@ -16,6 +17,7 @@ const {
ObjectKeys,
ObjectPrototypeHasOwnProperty,
PromiseWithResolvers,
SafeSet,
StringPrototypeToUpperCase,
Symbol,
TypedArrayPrototypeGetBuffer,
Expand Down Expand Up @@ -718,14 +720,40 @@ function getStringOption(options, key) {
}

function getUsagesUnion(usageSet, ...usages) {
const newset = [];
const newset = new SafeSet();
for (let n = 0; n < usages.length; n++) {
if (usageSet.has(usages[n]))
ArrayPrototypePush(newset, usages[n]);
newset.add(usages[n]);
}
return newset;
}

const kCanonicalUsageOrder = new SafeSet([
'encrypt', 'decrypt',
'sign', 'verify',
'deriveKey', 'deriveBits',
'wrapKey', 'unwrapKey',
'encapsulateKey', 'encapsulateBits',
'decapsulateKey', 'decapsulateBits',
]);

/**
* Returns the usages from `usageSet` as an array in the canonical order
* defined by {@link kCanonicalUsageOrder}.
* @param {SafeSet<string>} usageSet
* @returns {string[]}
*/
function getSortedUsages(usageSet) {
if (usageSet.size <= 1) {
return ArrayFrom(usageSet);
}
const result = [];
for (const usage of kCanonicalUsageOrder) {
if (usageSet.has(usage)) ArrayPrototypePush(result, usage);
}
return result;
}

function getBlockSize(name) {
switch (name) {
case 'SHA-1':
Expand Down Expand Up @@ -841,6 +869,7 @@ module.exports = {
getDigestSizeInBytes,
getStringOption,
getUsagesUnion,
getSortedUsages,
secureHeapUsed,
getCachedHashId,
getHashCache,
Expand Down
Loading
Loading