| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent aa1e0bc commit 8752b60
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,6 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | - ArrayFrom, | ||
| 5 | 4 | ArrayPrototypePush, | |
| 6 | 5 | SafeSet, | |
| 7 | 6 | } = primordials; | |
@@ -205,7 +204,7 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) { | |||
| 205 | 204 | return new InternalCryptoKey( | |
| 206 | 205 | key, | |
| 207 | 206 | { name, length }, | |
| 208 | - ArrayFrom(usagesSet), | ||
| 207 | + usagesSet, | ||
| 209 | 208 | extractable); | |
| 210 | 209 | } | |
| 211 | 210 | ||
@@ -300,7 +299,7 @@ function aesImportKey( | |||
| 300 | 299 | return new InternalCryptoKey( | |
| 301 | 300 | keyObject, | |
| 302 | 301 | { name, length }, | |
| 303 | - keyUsages, | ||
| 302 | + usagesSet, | ||
| 304 | 303 | extractable); | |
| 305 | 304 | } | |
| 306 | 305 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -171,7 +171,7 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) { | |||
| 171 | 171 | case 'X25519': | |
| 172 | 172 | // Fall through | |
| 173 | 173 | case 'X448': | |
| 174 | - publicUsages = []; | ||
| 174 | + publicUsages = new SafeSet(); | ||
| 175 | 175 | privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits'); | |
| 176 | 176 | break; | |
| 177 | 177 | } | |
@@ -340,7 +340,7 @@ function cfrgImportKey( | |||
| 340 | 340 | return new InternalCryptoKey( | |
| 341 | 341 | keyObject, | |
| 342 | 342 | { name }, | |
| 343 | - keyUsages, | ||
| 343 | + usagesSet, | ||
| 344 | 344 | extractable); | |
| 345 | 345 | } | |
| 346 | 346 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,6 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | - ArrayFrom, | ||
| 5 | 4 | SafeSet, | |
| 6 | 5 | } = primordials; | |
| 7 | 6 | ||
@@ -76,7 +75,7 @@ async function c20pGenerateKey(algorithm, extractable, keyUsages) { | |||
| 76 | 75 | return new InternalCryptoKey( | |
| 77 | 76 | createSecretKey(keyData), | |
| 78 | 77 | { name }, | |
| 79 | - ArrayFrom(usagesSet), | ||
| 78 | + usagesSet, | ||
| 80 | 79 | extractable); | |
| 81 | 80 | } | |
| 82 | 81 | ||
@@ -155,7 +154,7 @@ function c20pImportKey( | |||
| 155 | 154 | return new InternalCryptoKey( | |
| 156 | 155 | keyObject, | |
| 157 | 156 | { name }, | |
| 158 | - keyUsages, | ||
| 157 | + usagesSet, | ||
| 159 | 158 | extractable); | |
| 160 | 159 | } | |
| 161 | 160 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -114,7 +114,7 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) { | |||
| 114 | 114 | privateUsages = getUsagesUnion(usageSet, 'sign'); | |
| 115 | 115 | break; | |
| 116 | 116 | case 'ECDH': | |
| 117 | - publicUsages = []; | ||
| 117 | + publicUsages = new SafeSet(); | ||
| 118 | 118 | privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits'); | |
| 119 | 119 | break; | |
| 120 | 120 | } | |
@@ -280,7 +280,7 @@ function ecImportKey( | |||
| 280 | 280 | return new InternalCryptoKey( | |
| 281 | 281 | keyObject, | |
| 282 | 282 | { name, namedCurve }, | |
| 283 | - keyUsages, | ||
| 283 | + usagesSet, | ||
| 284 | 284 | extractable); | |
| 285 | 285 | } | |
| 286 | 286 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,6 +62,7 @@ const { | |||
| 62 | 62 | bigIntArrayToUnsignedBigInt, | |
| 63 | 63 | normalizeAlgorithm, | |
| 64 | 64 | hasAnyNotIn, | |
| 65 | + getSortedUsages, | ||
| 65 | 66 | } = require('internal/crypto/util'); | |
| 66 | 67 | ||
| 67 | 68 | const { | |
@@ -1094,7 +1095,7 @@ class InternalCryptoKey { | |||
| 1094 | 1095 | keyObject, | |
| 1095 | 1096 | algorithm, | |
| 1096 | 1097 | extractable, | |
| 1097 | - keyUsages, | ||
| 1098 | + getSortedUsages(new SafeSet(keyUsages)), | ||
| 1098 | 1099 | ); | |
| 1099 | 1100 | } | |
| 1100 | 1101 | } | |
@@ -1160,7 +1161,7 @@ function importGenericSecretKey( | |||
| 1160 | 1161 | return undefined; | |
| 1161 | 1162 | } | |
| 1162 | 1163 | ||
| 1163 | - return new InternalCryptoKey(keyObject, { name }, keyUsages, false); | ||
| 1164 | + return new InternalCryptoKey(keyObject, { name }, usagesSet, false); | ||
| 1164 | 1165 | } | |
| 1165 | 1166 | ||
| 1166 | 1167 | module.exports = { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,6 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | - ArrayFrom, | ||
| 5 | 4 | SafeSet, | |
| 6 | 5 | StringPrototypeSubstring, | |
| 7 | 6 | } = primordials; | |
@@ -76,7 +75,7 @@ async function hmacGenerateKey(algorithm, extractable, keyUsages) { | |||
| 76 | 75 | return new InternalCryptoKey( | |
| 77 | 76 | key, | |
| 78 | 77 | { name, length, hash }, | |
| 79 | - ArrayFrom(usageSet), | ||
| 78 | + usageSet, | ||
| 80 | 79 | extractable); | |
| 81 | 80 | } | |
| 82 | 81 | ||
@@ -110,7 +109,7 @@ async function kmacGenerateKey(algorithm, extractable, keyUsages) { | |||
| 110 | 109 | return new InternalCryptoKey( | |
| 111 | 110 | createSecretKey(keyData), | |
| 112 | 111 | { name, length }, | |
| 113 | - ArrayFrom(usageSet), | ||
| 112 | + usageSet, | ||
| 114 | 113 | extractable); | |
| 115 | 114 | } | |
| 116 | 115 | ||
@@ -211,7 +210,7 @@ function macImportKey( | |||
| 211 | 210 | return new InternalCryptoKey( | |
| 212 | 211 | keyObject, | |
| 213 | 212 | algorithmObject, | |
| 214 | - keyUsages, | ||
| 213 | + usagesSet, | ||
| 215 | 214 | extractable); | |
| 216 | 215 | } | |
| 217 | 216 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -297,7 +297,7 @@ function mlDsaImportKey( | |||
| 297 | 297 | return new InternalCryptoKey( | |
| 298 | 298 | keyObject, | |
| 299 | 299 | { name }, | |
| 300 | - keyUsages, | ||
| 300 | + usagesSet, | ||
| 301 | 301 | extractable); | |
| 302 | 302 | } | |
| 303 | 303 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,8 +69,8 @@ async function mlKemGenerateKey(algorithm, extractable, keyUsages) { | |||
| 69 | 69 | { name: 'OperationError', cause: err }); | |
| 70 | 70 | } | |
| 71 | 71 | ||
| 72 | - const publicUsages = getUsagesUnion(usageSet, 'encapsulateBits', 'encapsulateKey'); | ||
| 73 | - const privateUsages = getUsagesUnion(usageSet, 'decapsulateBits', 'decapsulateKey'); | ||
| 72 | + const publicUsages = getUsagesUnion(usageSet, 'encapsulateKey', 'encapsulateBits'); | ||
| 73 | + const privateUsages = getUsagesUnion(usageSet, 'decapsulateKey', 'decapsulateBits'); | ||
| 74 | 74 | ||
| 75 | 75 | const keyAlgorithm = { name }; | |
| 76 | 76 | ||
@@ -230,7 +230,7 @@ function mlKemImportKey( | |||
| 230 | 230 | return new InternalCryptoKey( | |
| 231 | 231 | keyObject, | |
| 232 | 232 | { name }, | |
| 233 | - keyUsages, | ||
| 233 | + usagesSet, | ||
| 234 | 234 | extractable); | |
| 235 | 235 | } | |
| 236 | 236 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -327,7 +327,7 @@ function rsaImportKey( | |||
| 327 | 327 | modulusLength, | |
| 328 | 328 | publicExponent: new Uint8Array(publicExponent), | |
| 329 | 329 | hash: algorithm.hash, | |
| 330 | - }, keyUsages, extractable); | ||
| 330 | + }, usagesSet, extractable); | ||
| 331 | 331 | } | |
| 332 | 332 | ||
| 333 | 333 | async function rsaSignVerify(key, data, { saltLength }, signature) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | const { | |
| 4 | 4 | ArrayBufferIsView, | |
| 5 | 5 | ArrayBufferPrototypeGetByteLength, | |
| 6 | + ArrayFrom, | ||
| 6 | 7 | ArrayPrototypeIncludes, | |
| 7 | 8 | ArrayPrototypePush, | |
| 8 | 9 | BigInt, | |
@@ -16,6 +17,7 @@ const { | |||
| 16 | 17 | ObjectKeys, | |
| 17 | 18 | ObjectPrototypeHasOwnProperty, | |
| 18 | 19 | PromiseWithResolvers, | |
| 20 | + SafeSet, | ||
| 19 | 21 | StringPrototypeToUpperCase, | |
| 20 | 22 | Symbol, | |
| 21 | 23 | TypedArrayPrototypeGetBuffer, | |
@@ -706,14 +708,40 @@ function getStringOption(options, key) { | |||
| 706 | 708 | } | |
| 707 | 709 | ||
| 708 | 710 | function getUsagesUnion(usageSet, ...usages) { | |
| 709 | - const newset = []; | ||
| 711 | + const newset = new SafeSet(); | ||
| 710 | 712 | for (let n = 0; n < usages.length; n++) { | |
| 711 | 713 | if (usageSet.has(usages[n])) | |
| 712 | - ArrayPrototypePush(newset, usages[n]); | ||
| 714 | + newset.add(usages[n]); | ||
| 713 | 715 | } | |
| 714 | 716 | return newset; | |
| 715 | 717 | } | |
| 716 | 718 | ||
| 719 | + const kCanonicalUsageOrder = new SafeSet([ | ||
| 720 | + 'encrypt', 'decrypt', | ||
| 721 | + 'sign', 'verify', | ||
| 722 | + 'deriveKey', 'deriveBits', | ||
| 723 | + 'wrapKey', 'unwrapKey', | ||
| 724 | + 'encapsulateKey', 'encapsulateBits', | ||
| 725 | + 'decapsulateKey', 'decapsulateBits', | ||
| 726 | + ]); | ||
| 727 | + | ||
| 728 | + /** | ||
| 729 | + * Returns the usages from `usageSet` as an array in the canonical order | ||
| 730 | + * defined by {@link kCanonicalUsageOrder}. | ||
| 731 | + * @param {SafeSet<string>} usageSet | ||
| 732 | + * @returns {string[]} | ||
| 733 | + */ | ||
| 734 | + function getSortedUsages(usageSet) { | ||
| 735 | + if (usageSet.size <= 1) { | ||
| 736 | + return ArrayFrom(usageSet); | ||
| 737 | + } | ||
| 738 | + const result = []; | ||
| 739 | + for (const usage of kCanonicalUsageOrder) { | ||
| 740 | + if (usageSet.has(usage)) ArrayPrototypePush(result, usage); | ||
| 741 | + } | ||
| 742 | + return result; | ||
| 743 | + } | ||
| 744 | + | ||
| 717 | 745 | function getBlockSize(name) { | |
| 718 | 746 | switch (name) { | |
| 719 | 747 | case 'SHA-1': | |
@@ -830,6 +858,7 @@ module.exports = { | |||
| 830 | 858 | getDigestSizeInBytes, | |
| 831 | 859 | getStringOption, | |
| 832 | 860 | getUsagesUnion, | |
| 861 | + getSortedUsages, | ||
| 833 | 862 | secureHeapUsed, | |
| 834 | 863 | getCachedHashId, | |
| 835 | 864 | getHashCache, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments