GeezerVison is showing github.com — crypto: separate conversion from validation · nodejs/node@06254be
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 +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,6 +25,7 @@ const { | 25 | 25 | getUsagesMask, | 26 | 26 | jobPromise, | 27 | 27 | getBufferSourceByteLength, | | 28 | + validateAlgorithm, | 28 | 29 | } = require('internal/crypto/util'); | 29 | 30 |
| 30 | 31 | const { | @@ -173,6 +174,7 @@ function asyncAesOcbCipher(mode, key, data, algorithm) { | 173 | 174 | } | 174 | 175 |
| 175 | 176 | function aesCipher(mode, key, data, algorithm) { | | 177 | + validateAlgorithm(algorithm, 'encrypt'); | 176 | 178 | switch (algorithm.name) { | 177 | 179 | case 'AES-CTR': return asyncAesCtrCipher(mode, key, data, algorithm); | 178 | 180 | case 'AES-CBC': return asyncAesCbcCipher(mode, key, data, algorithm); | @@ -185,8 +187,9 @@ function aesCipher(mode, key, data, algorithm) { | 185 | 187 | function aesGenerateKey(algorithm, extractable, usages) { | 186 | 188 | const { name, length } = algorithm; | 187 | 189 |
| 188 | | - const usagesSet = validateUsagesNotEmpty( | 189 | | - validateKeyUsages(usages, kUsages[name], name)); | | 190 | + const usagesSet = validateKeyUsages(usages, kUsages[name], name); | | 191 | + validateAlgorithm(algorithm, 'generateKey'); | | 192 | + validateUsagesNotEmpty(usagesSet); | 190 | 193 |
| 191 | 194 | return jobPromise(() => new SecretKeyGenJob( | 192 | 195 | kCryptoJobWebCrypto, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/argon2.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ const { | 29 | 29 | const { | 30 | 30 | getArrayBufferOrView, | 31 | 31 | jobPromise, | | 32 | + validateAlgorithm, | 32 | 33 | } = require('internal/crypto/util'); | 33 | 34 |
| 34 | 35 | const { | @@ -203,6 +204,7 @@ function validateArgon2DeriveBitsLength(length) { | 203 | 204 |
| 204 | 205 | function argon2DeriveBits(algorithm, baseKey, length) { | 205 | 206 | validateArgon2DeriveBitsLength(length); | | 207 | + validateAlgorithm(algorithm, 'deriveBits'); | 206 | 208 |
| 207 | 209 | const type = { | 208 | 210 | '__proto__': null, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/cfrg.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,6 +25,7 @@ const { | 25 | 25 | getUsagesMask, | 26 | 26 | jobPromise, | 27 | 27 | toUsagesSet, | | 28 | + validateAlgorithm, | 28 | 29 | } = require('internal/crypto/util'); | 29 | 30 |
| 30 | 31 | const { | @@ -192,6 +193,8 @@ function eddsaSignVerify(key, data, algorithm, signature) { | 192 | 193 | if (getCryptoKeyType(key) !== type) | 193 | 194 | throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError'); | 194 | 195 |
| | 196 | + validateAlgorithm(algorithm, mode === kSignJobModeSign ? 'sign' : 'verify'); | | 197 | + | 195 | 198 | return jobPromise(() => new SignJob( | 196 | 199 | kCryptoJobWebCrypto, | 197 | 200 | mode, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/chacha20_poly1305.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | const { | 10 | 10 | getUsagesMask, | 11 | 11 | jobPromise, | | 12 | + validateAlgorithm, | 12 | 13 | } = require('internal/crypto/util'); | 13 | 14 |
| 14 | 15 | const { | @@ -36,6 +37,7 @@ function validateKeyLength(length) { | 36 | 37 | } | 37 | 38 |
| 38 | 39 | function c20pCipher(mode, key, data, algorithm) { | | 40 | + validateAlgorithm(algorithm, 'encrypt'); | 39 | 41 | return jobPromise(() => new ChaCha20Poly1305CipherJob( | 40 | 42 | kCryptoJobWebCrypto, | 41 | 43 | mode, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/diffiehellman.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,6 +66,7 @@ const { | 66 | 66 | numBitsToBytes, | 67 | 67 | toBuf, | 68 | 68 | truncateToBitLength, | | 69 | + validateAlgorithm, | 69 | 70 | kHandle, | 70 | 71 | } = require('internal/crypto/util'); | 71 | 72 |
| @@ -368,6 +369,7 @@ function diffieHellman(options, callback) { | 368 | 369 | // The ecdhDeriveBits function is part of the Web Crypto API and serves both | 369 | 370 | // deriveKeys and deriveBits functions. | 370 | 371 | function ecdhDeriveBits(algorithm, baseKey, length) { | | 372 | + validateAlgorithm(algorithm, 'deriveBits'); | 371 | 373 | const { 'public': key } = algorithm; | 372 | 374 |
| 373 | 375 | if (getCryptoKeyType(baseKey) !== 'private') { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/ec.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,7 @@ const { | 31 | 31 | getUsagesMask, | 32 | 32 | jobPromise, | 33 | 33 | normalizeHashName, | | 34 | + validateAlgorithm, | 34 | 35 | kNamedCurveAliases, | 35 | 36 | toUsagesSet, | 36 | 37 | } = require('internal/crypto/util'); | @@ -72,6 +73,7 @@ function ecGenerateKey(algorithm, extractable, usages) { | 72 | 73 | const { name, namedCurve } = algorithm; | 73 | 74 | const allowedUsages = kUsages[name]; | 74 | 75 | const usagesSet = validateKeyUsages(usages, allowedUsages.keygen, name); | | 76 | + validateAlgorithm(algorithm, 'generateKey'); | 75 | 77 |
| 76 | 78 | const keyAlgorithm = { name, namedCurve }; | 77 | 79 | const keyUsages = getKeyPairUsages(usagesSet, allowedUsages); | @@ -139,6 +141,7 @@ function ecImportKey( | 139 | 141 | usages, | 140 | 142 | ) { | 141 | 143 | const { name, namedCurve } = algorithm; | | 144 | + validateAlgorithm(algorithm, 'importKey'); | 142 | 145 |
| 143 | 146 | let handle; | 144 | 147 | const allowedUsages = kUsages[name]; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/hash.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ const { | 28 | 28 | normalizeHashName, | 29 | 29 | numBitsToBytes, | 30 | 30 | truncateToBitLength, | | 31 | + validateAlgorithm, | 31 | 32 | validateMaxBufferLength, | 32 | 33 | kHandle, | 33 | 34 | getCachedHashId, | @@ -259,6 +260,7 @@ Hmac.prototype._transform = Hash.prototype._transform; | 259 | 260 |
| 260 | 261 | function asyncDigest(algorithm, data) { | 261 | 262 | validateMaxBufferLength(data, 'data'); | | 263 | + validateAlgorithm(algorithm, 'digest'); | 262 | 264 |
| 263 | 265 | switch (algorithm.name) { | 264 | 266 | case 'SHA-1': | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/hkdf.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ const { | 29 | 29 | normalizeHashName, | 30 | 30 | toBuf, | 31 | 31 | validateByteSource, | | 32 | + validateAlgorithm, | 32 | 33 | } = require('internal/crypto/util'); | 33 | 34 |
| 34 | 35 | const { | @@ -182,6 +183,7 @@ function validateHkdfDeriveBitsLength(length, hash) { | 182 | 183 | function hkdfDeriveBits(algorithm, baseKey, length) { | 183 | 184 | const { hash, salt, info } = algorithm; | 184 | 185 | validateHkdfDeriveBitsLength(length, hash); | | 186 | + validateAlgorithm(algorithm, 'deriveBits'); | 185 | 187 |
| 186 | 188 | if (length === 0) | 187 | 189 | return PromiseResolve(new ArrayBuffer(0)); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/mac.js +10-5Lines changed: 10 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,7 @@ const { | 21 | 21 | numBitsToBytes, | 22 | 22 | truncateToBitLength, | 23 | 23 | validateKmacKeyLength, | | 24 | + validateAlgorithm, | 24 | 25 | } = require('internal/crypto/util'); | 25 | 26 |
| 26 | 27 | const { | @@ -71,11 +72,12 @@ function hmacGenerateKey(algorithm, extractable, usages) { | 71 | 72 | const { | 72 | 73 | hash, | 73 | 74 | name, | 74 | | - length = getBlockSize(hash.name), | 75 | 75 | } = algorithm; | 76 | 76 |
| 77 | | - const usageSet = validateUsagesNotEmpty( | 78 | | - validateKeyUsages(usages, kUsages, name)); | | 77 | + const usageSet = validateKeyUsages(usages, kUsages, name); | | 78 | + validateAlgorithm(algorithm, 'generateKey'); | | 79 | + const { length = getBlockSize(hash.name) } = algorithm; | | 80 | + validateUsagesNotEmpty(usageSet); | 79 | 81 |
| 80 | 82 | return jobPromise(() => new SecretKeyGenJob( | 81 | 83 | kCryptoJobWebCrypto, | @@ -95,8 +97,9 @@ function kmacGenerateKey(algorithm, extractable, usages) { | 95 | 97 | }[name], | 96 | 98 | } = algorithm; | 97 | 99 |
| 98 | | - const usageSet = validateUsagesNotEmpty( | 99 | | - validateKeyUsages(usages, kUsages, name)); | | 100 | + const usageSet = validateKeyUsages(usages, kUsages, name); | | 101 | + validateAlgorithm(algorithm, 'generateKey'); | | 102 | + validateUsagesNotEmpty(usageSet); | 100 | 103 |
| 101 | 104 | return jobPromise(() => new SecretKeyGenJob( | 102 | 105 | kCryptoJobWebCrypto, | @@ -114,6 +117,7 @@ function macImportKey( | 114 | 117 | usages, | 115 | 118 | ) { | 116 | 119 | const isHmac = algorithm.name === 'HMAC'; | | 120 | + validateAlgorithm(algorithm, 'importKey'); | 117 | 121 | const usagesSet = validateKeyUsages( | 118 | 122 | usages, kUsages, algorithm.name); | 119 | 123 | let handle; | @@ -181,6 +185,7 @@ function hmacSignVerify(key, data, algorithm, signature) { | 181 | 185 | } | 182 | 186 |
| 183 | 187 | function kmacSignVerify(key, data, algorithm, signature) { | | 188 | + validateAlgorithm(algorithm, 'sign'); | 184 | 189 | const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify; | 185 | 190 | return jobPromise(() => new KmacJob( | 186 | 191 | kCryptoJobWebCrypto, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/ml_dsa.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ const { | 28 | 28 | getUsagesMask, | 29 | 29 | jobPromise, | 30 | 30 | toUsagesSet, | | 31 | + validateAlgorithm, | 31 | 32 | getBufferSourceByteLength, | 32 | 33 | } = require('internal/crypto/util'); | 33 | 34 |
| @@ -211,6 +212,8 @@ function mlDsaSignVerify(key, data, algorithm, signature) { | 211 | 212 | if (getCryptoKeyType(key) !== type) | 212 | 213 | throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError'); | 213 | 214 |
| | 215 | + validateAlgorithm(algorithm, mode === kSignJobModeSign ? 'sign' : 'verify'); | | 216 | + | 214 | 217 | return jobPromise(() => new SignJob( | 215 | 218 | kCryptoJobWebCrypto, | 216 | 219 | mode, | |
|