Skip to content
Open
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
3 changes: 2 additions & 1 deletion lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const {
const {
getUsagesMask,
jobPromise,
getBufferSourceByteLength,
} = require('internal/crypto/util');

const {
Expand Down Expand Up @@ -218,7 +219,7 @@ function aesImportKey(
if (format === 'raw' && name === 'AES-OCB') {
return undefined;
}
length = keyData.byteLength * 8;
length = getBufferSourceByteLength(keyData) * 8;
validateKeyLength(length);
handle = importSecretKey(keyData);
break;
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/cfrg.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
SafeSet,
StringPrototypeToLowerCase,
TypedArrayPrototypeGetBuffer,
} = primordials;
Expand All @@ -27,6 +26,7 @@ const {
const {
getUsagesMask,
jobPromise,
toUsagesSet,
} = require('internal/crypto/util');

const {
Expand Down Expand Up @@ -124,7 +124,7 @@ function cfrgImportKey(
const { name } = algorithm;
let handle;
const allowedUsages = kUsages[name];
const usagesSet = new SafeSet(usages);
const usagesSet = toUsagesSet(usages);
switch (format) {
case 'KeyObjectHandle':
verifyAcceptableKeyUse(
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayBufferPrototypeGetByteLength,
ArrayBufferPrototypeSlice,
FunctionPrototypeCall,
ObjectDefineProperty,
Expand Down Expand Up @@ -373,7 +374,7 @@ function ecdhDeriveBits(algorithm, baseKey, length) {
return jobPromiseThen(bits, (bits) => {
const sliceLength = numBitsToBytes(length);

const { byteLength } = bits;
const byteLength = ArrayBufferPrototypeGetByteLength(bits);
// If the length is larger than the derived secret, throw.
if (byteLength < sliceLength)
throw lazyDOMException('derived bit length is too small', 'OperationError');
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/crypto/ec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
SafeSet,
TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetByteLength,
} = primordials;
Expand Down Expand Up @@ -33,6 +32,7 @@ const {
jobPromise,
normalizeHashName,
kNamedCurveAliases,
toUsagesSet,
} = require('internal/crypto/util');

const {
Expand Down Expand Up @@ -142,7 +142,7 @@ function ecImportKey(

let handle;
const allowedUsages = kUsages[name];
const usagesSet = new SafeSet(usages);
const usagesSet = toUsagesSet(usages);
switch (format) {
case 'KeyObjectHandle':
verifyAcceptableKeyUse(
Expand Down Expand Up @@ -215,7 +215,8 @@ function ecImportKey(
throw lazyDOMException('Invalid keyData', 'DataError');
}

if (kNamedCurveAliases[namedCurve] !== handle.keyDetail({}).namedCurve)
if (kNamedCurveAliases[namedCurve] !==
handle.keyDetail({ __proto__: null }).namedCurve)
throw lazyDOMException('Named curve mismatch', 'DataError');

return new InternalCryptoKey(
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
kHandle,
getCachedHashId,
getHashCache,
getOptionalByteLength,
} = require('internal/crypto/util');

const {
Expand Down Expand Up @@ -217,8 +218,8 @@ function asyncDigest(algorithm, data) {
// Fall through
case 'cSHAKE256': {
const outputLength = algorithm.outputLength;
if (algorithm.functionName?.byteLength ||
algorithm.customization?.byteLength) {
if (getOptionalByteLength(algorithm.functionName) ||
getOptionalByteLength(algorithm.customization)) {
if (CShakeJob === undefined) {
throw lazyDOMException(
'Non-empty CShakeParams functionName or customization is not supported',
Expand Down
14 changes: 11 additions & 3 deletions lib/internal/crypto/hashnames.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ObjectKeys,
ObjectSetPrototypeOf,
} = primordials;

const kHashContextNode = 1;
Expand Down Expand Up @@ -71,15 +72,22 @@ const kHashNames = {
},
};

// Both tables are indexed with computed keys, so a polluted %Object.prototype%
// key must not answer a miss. Detached here rather than declared
// `__proto__: null`: V8 puts that literal form in dictionary mode.
ObjectSetPrototypeOf(kHashNames, null);

{
// Index the aliases
const keys = ObjectKeys(kHashNames);
for (let n = 0; n < keys.length; n++) {
const contexts = ObjectKeys(kHashNames[keys[n]]);
const entry = kHashNames[keys[n]];
ObjectSetPrototypeOf(entry, null);
const contexts = ObjectKeys(entry);
for (let i = 0; i < contexts.length; i++) {
const alias = kHashNames[keys[n]][contexts[i]];
const alias = entry[contexts[i]];
if (kHashNames[alias] === undefined)
kHashNames[alias] = kHashNames[keys[n]];
kHashNames[alias] = entry;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {
ObjectDefineProperties,
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
SafeSet,
StringPrototypeIncludes,
StringPrototypeStartsWith,
SymbolToStringTag,
Expand Down Expand Up @@ -68,6 +67,7 @@ const {
getUsagesMask,
getUsagesFromMask,
hasUsage,
toUsagesSet,
} = require('internal/crypto/util');

const {
Expand Down Expand Up @@ -1306,7 +1306,7 @@ function importGenericSecretKey(
extractable,
keyUsages,
) {
const usagesSet = new SafeSet(keyUsages);
const usagesSet = toUsagesSet(keyUsages);
const { name } = algorithm;
if (extractable)
throw lazyDOMException(`${name} keys are not extractable`, 'SyntaxError');
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/crypto/ml_dsa.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
SafeSet,
StringPrototypeToLowerCase,
TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetByteLength,
Expand All @@ -28,6 +27,8 @@ const {
const {
getUsagesMask,
jobPromise,
toUsagesSet,
getBufferSourceByteLength,
} = require('internal/crypto/util');

const {
Expand Down Expand Up @@ -122,7 +123,7 @@ function mlDsaImportKey(

const { name } = algorithm;
let handle;
const usagesSet = new SafeSet(usages);
const usagesSet = toUsagesSet(usages);
switch (format) {
case 'KeyObjectHandle':
verifyAcceptableKeyUse(
Expand All @@ -147,7 +148,7 @@ function mlDsaImportKey(
'ML-DSA-65': 4060,
'ML-DSA-87': 4924,
};
if (keyData.byteLength === privOnlyLengths[name]) {
if (getBufferSourceByteLength(keyData) === privOnlyLengths[name]) {
throw lazyDOMException(
'Importing an ML-DSA PKCS#8 key without a seed is not supported',
'NotSupportedError');
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/crypto/ml_kem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const {
SafeSet,
StringPrototypeToLowerCase,
TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetByteLength,
Expand All @@ -27,6 +26,8 @@ const {
const {
getUsagesMask,
jobPromise,
toUsagesSet,
getBufferSourceByteLength,
} = require('internal/crypto/util');

const {
Expand Down Expand Up @@ -123,7 +124,7 @@ function mlKemImportKey(

const { name } = algorithm;
let handle;
const usagesSet = new SafeSet(usages);
const usagesSet = toUsagesSet(usages);
switch (format) {
case 'KeyObjectHandle':
verifyAcceptableKeyUse(
Expand All @@ -148,7 +149,7 @@ function mlKemImportKey(
'ML-KEM-768': 2428,
'ML-KEM-1024': 3196,
};
if (keyData.byteLength === privOnlyLengths[name]) {
if (getBufferSourceByteLength(keyData) === privOnlyLengths[name]) {
throw lazyDOMException(
'Importing an ML-KEM PKCS#8 key without a seed is not supported',
'NotSupportedError');
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/crypto/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const {
MathCeil,
SafeSet,
TypedArrayPrototypeGetBuffer,
Uint8Array,
} = primordials;
Expand Down Expand Up @@ -35,6 +34,7 @@ const {
jobPromise,
normalizeHashName,
validateMaxBufferLength,
toUsagesSet,
} = require('internal/crypto/util');

const {
Expand Down Expand Up @@ -174,7 +174,7 @@ function rsaImportKey(
extractable,
usages) {
const allowedUsages = kUsages[algorithm.name];
const usagesSet = new SafeSet(usages);
const usagesSet = toUsagesSet(usages);
let handle;
switch (format) {
case 'KeyObjectHandle':
Expand Down Expand Up @@ -234,7 +234,7 @@ function rsaImportKey(
const {
modulusLength,
publicExponent,
} = handle.keyDetail({});
} = handle.keyDetail({ __proto__: null });

return new InternalCryptoKey(handle, {
name: algorithm.name,
Expand Down
Loading
Loading