GeezerVison is showing github.com — crypto: deduplicate and canonicalize CryptoKey usages · nodejs/node@80d4836
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/aes.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | 4 | | - ArrayFrom, | 5 | 4 | ArrayPrototypePush, | 6 | 5 | SafeSet, | 7 | 6 | } = primordials; | @@ -210,7 +209,7 @@ async function aesGenerateKey(algorithm, extractable, keyUsages) { | 210 | 209 | return new InternalCryptoKey( | 211 | 210 | key, | 212 | 211 | { name, length }, | 213 | | - ArrayFrom(usagesSet), | | 212 | + usagesSet, | 214 | 213 | extractable); | 215 | 214 | } | 216 | 215 |
| @@ -285,7 +284,7 @@ function aesImportKey( | 285 | 284 | return new InternalCryptoKey( | 286 | 285 | keyObject, | 287 | 286 | { name, length }, | 288 | | - keyUsages, | | 287 | + usagesSet, | 289 | 288 | extractable); | 290 | 289 | } | 291 | 290 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/cfrg.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -134,7 +134,7 @@ async function cfrgGenerateKey(algorithm, extractable, keyUsages) { | 134 | 134 | case 'X25519': | 135 | 135 | // Fall through | 136 | 136 | case 'X448': | 137 | | - publicUsages = []; | | 137 | + publicUsages = new SafeSet(); | 138 | 138 | privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits'); | 139 | 139 | break; | 140 | 140 | } | @@ -245,7 +245,7 @@ function cfrgImportKey( | 245 | 245 | return new InternalCryptoKey( | 246 | 246 | keyObject, | 247 | 247 | { name }, | 248 | | - keyUsages, | | 248 | + usagesSet, | 249 | 249 | extractable); | 250 | 250 | } | 251 | 251 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/chacha20_poly1305.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | 4 | | - ArrayFrom, | 5 | 4 | SafeSet, | 6 | 5 | } = primordials; | 7 | 6 |
| @@ -81,7 +80,7 @@ async function c20pGenerateKey(algorithm, extractable, keyUsages) { | 81 | 80 | return new InternalCryptoKey( | 82 | 81 | createSecretKey(keyData), | 83 | 82 | { name }, | 84 | | - ArrayFrom(usagesSet), | | 83 | + usagesSet, | 85 | 84 | extractable); | 86 | 85 | } | 87 | 86 |
| @@ -140,7 +139,7 @@ function c20pImportKey( | 140 | 139 | return new InternalCryptoKey( | 141 | 140 | keyObject, | 142 | 141 | { name }, | 143 | | - keyUsages, | | 142 | + usagesSet, | 144 | 143 | extractable); | 145 | 144 | } | 146 | 145 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/ec.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -119,7 +119,7 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) { | 119 | 119 | privateUsages = getUsagesUnion(usageSet, 'sign'); | 120 | 120 | break; | 121 | 121 | case 'ECDH': | 122 | | - publicUsages = []; | | 122 | + publicUsages = new SafeSet(); | 123 | 123 | privateUsages = getUsagesUnion(usageSet, 'deriveKey', 'deriveBits'); | 124 | 124 | break; | 125 | 125 | } | @@ -271,7 +271,7 @@ function ecImportKey( | 271 | 271 | return new InternalCryptoKey( | 272 | 272 | keyObject, | 273 | 273 | { name, namedCurve }, | 274 | | - keyUsages, | | 274 | + usagesSet, | 275 | 275 | extractable); | 276 | 276 | } | 277 | 277 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/keys.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,6 +61,7 @@ const { | 61 | 61 | bigIntArrayToUnsignedBigInt, | 62 | 62 | normalizeAlgorithm, | 63 | 63 | hasAnyNotIn, | | 64 | + getSortedUsages, | 64 | 65 | } = require('internal/crypto/util'); | 65 | 66 |
| 66 | 67 | const { | @@ -901,7 +902,7 @@ class InternalCryptoKey { | 901 | 902 | keyObject, | 902 | 903 | algorithm, | 903 | 904 | extractable, | 904 | | - keyUsages, | | 905 | + getSortedUsages(new SafeSet(keyUsages)), | 905 | 906 | ); | 906 | 907 | } | 907 | 908 | } | @@ -967,7 +968,7 @@ function importGenericSecretKey( | 967 | 968 | return undefined; | 968 | 969 | } | 969 | 970 |
| 970 | | - return new InternalCryptoKey(keyObject, { name }, keyUsages, false); | | 971 | + return new InternalCryptoKey(keyObject, { name }, usagesSet, false); | 971 | 972 | } | 972 | 973 |
| 973 | 974 | module.exports = { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/mac.js +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | 4 | | - ArrayFrom, | 5 | 4 | SafeSet, | 6 | 5 | StringPrototypeSubstring, | 7 | 6 | } = primordials; | @@ -81,7 +80,7 @@ async function hmacGenerateKey(algorithm, extractable, keyUsages) { | 81 | 80 | return new InternalCryptoKey( | 82 | 81 | key, | 83 | 82 | { name, length, hash }, | 84 | | - ArrayFrom(usageSet), | | 83 | + usageSet, | 85 | 84 | extractable); | 86 | 85 | } | 87 | 86 |
| @@ -115,7 +114,7 @@ async function kmacGenerateKey(algorithm, extractable, keyUsages) { | 115 | 114 | return new InternalCryptoKey( | 116 | 115 | createSecretKey(keyData), | 117 | 116 | { name, length }, | 118 | | - ArrayFrom(usageSet), | | 117 | + usageSet, | 119 | 118 | extractable); | 120 | 119 | } | 121 | 120 |
| @@ -196,7 +195,7 @@ function macImportKey( | 196 | 195 | return new InternalCryptoKey( | 197 | 196 | keyObject, | 198 | 197 | algorithmObject, | 199 | | - keyUsages, | | 198 | + usagesSet, | 200 | 199 | extractable); | 201 | 200 | } | 202 | 201 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/ml_dsa.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -203,7 +203,7 @@ function mlDsaImportKey( | 203 | 203 | return new InternalCryptoKey( | 204 | 204 | keyObject, | 205 | 205 | { name }, | 206 | | - keyUsages, | | 206 | + usagesSet, | 207 | 207 | extractable); | 208 | 208 | } | 209 | 209 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/ml_kem.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -67,8 +67,8 @@ async function mlKemGenerateKey(algorithm, extractable, keyUsages) { | 67 | 67 | { name: 'OperationError', cause: err }); | 68 | 68 | } | 69 | 69 |
| 70 | | - const publicUsages = getUsagesUnion(usageSet, 'encapsulateBits', 'encapsulateKey'); | 71 | | - const privateUsages = getUsagesUnion(usageSet, 'decapsulateBits', 'decapsulateKey'); | | 70 | + const publicUsages = getUsagesUnion(usageSet, 'encapsulateKey', 'encapsulateBits'); | | 71 | + const privateUsages = getUsagesUnion(usageSet, 'decapsulateKey', 'decapsulateBits'); | 72 | 72 |
| 73 | 73 | const keyAlgorithm = { name }; | 74 | 74 |
| @@ -188,7 +188,7 @@ function mlKemImportKey( | 188 | 188 | return new InternalCryptoKey( | 189 | 189 | keyObject, | 190 | 190 | { name }, | 191 | | - keyUsages, | | 191 | + usagesSet, | 192 | 192 | extractable); | 193 | 193 | } | 194 | 194 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/rsa.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -281,7 +281,7 @@ function rsaImportKey( | 281 | 281 | modulusLength, | 282 | 282 | publicExponent: new Uint8Array(publicExponent), | 283 | 283 | hash: algorithm.hash, | 284 | | - }, keyUsages, extractable); | | 284 | + }, usagesSet, extractable); | 285 | 285 | } | 286 | 286 |
| 287 | 287 | async function rsaSignVerify(key, data, { saltLength }, signature) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js +31-2Lines changed: 31 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 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, | @@ -718,14 +720,40 @@ function getStringOption(options, key) { | 718 | 720 | } | 719 | 721 |
| 720 | 722 | function getUsagesUnion(usageSet, ...usages) { | 721 | | - const newset = []; | | 723 | + const newset = new SafeSet(); | 722 | 724 | for (let n = 0; n < usages.length; n++) { | 723 | 725 | if (usageSet.has(usages[n])) | 724 | | - ArrayPrototypePush(newset, usages[n]); | | 726 | + newset.add(usages[n]); | 725 | 727 | } | 726 | 728 | return newset; | 727 | 729 | } | 728 | 730 |
| | 731 | +const kCanonicalUsageOrder = new SafeSet([ | | 732 | + 'encrypt', 'decrypt', | | 733 | + 'sign', 'verify', | | 734 | + 'deriveKey', 'deriveBits', | | 735 | + 'wrapKey', 'unwrapKey', | | 736 | + 'encapsulateKey', 'encapsulateBits', | | 737 | + 'decapsulateKey', 'decapsulateBits', | | 738 | +]); | | 739 | + | | 740 | +/** | | 741 | + * Returns the usages from `usageSet` as an array in the canonical order | | 742 | + * defined by {@link kCanonicalUsageOrder}. | | 743 | + * @param {SafeSet<string>} usageSet | | 744 | + * @returns {string[]} | | 745 | + */ | | 746 | +function getSortedUsages(usageSet) { | | 747 | + if (usageSet.size <= 1) { | | 748 | + return ArrayFrom(usageSet); | | 749 | + } | | 750 | + const result = []; | | 751 | + for (const usage of kCanonicalUsageOrder) { | | 752 | + if (usageSet.has(usage)) ArrayPrototypePush(result, usage); | | 753 | + } | | 754 | + return result; | | 755 | +} | | 756 | + | 729 | 757 | function getBlockSize(name) { | 730 | 758 | switch (name) { | 731 | 759 | case 'SHA-1': | @@ -842,6 +870,7 @@ module.exports = { | 842 | 870 | getDigestSizeInBytes, | 843 | 871 | getStringOption, | 844 | 872 | getUsagesUnion, | | 873 | + getSortedUsages, | 845 | 874 | secureHeapUsed, | 846 | 875 | getCachedHashId, | 847 | 876 | getHashCache, | |
|