| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 90cd780 commit b10ac9a
17 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,7 +58,6 @@ const { | |||
| 58 | 58 | generateKey: _generateKey, | |
| 59 | 59 | } = require('internal/crypto/keygen'); | |
| 60 | 60 | ||
| 61 | - const kMaxCounterLength = 128; | ||
| 62 | 61 | const kTagLengths = [32, 64, 96, 104, 112, 120, 128]; | |
| 63 | 62 | const generateKey = promisify(_generateKey); | |
| 64 | 63 | ||
@@ -109,35 +108,43 @@ function getVariant(name, length) { | |||
| 109 | 108 | } | |
| 110 | 109 | } | |
| 111 | 110 | ||
| 112 | - function asyncAesCtrCipher(mode, key, data, { counter, length }) { | ||
| 113 | - validateByteLength(counter, 'algorithm.counter', 16); | ||
| 111 | + function validateAesCtrAlgorithm(algorithm) { | ||
| 112 | + validateByteLength(algorithm.counter, 'algorithm.counter', 16); | ||
| 114 | 113 | // The length must specify an integer between 1 and 128. While | |
| 115 | 114 | // there is no default, this should typically be 64. | |
| 116 | - if (length === 0 || length > kMaxCounterLength) { | ||
| 115 | + if (algorithm.length === 0 || algorithm.length > 128) { | ||
| 117 | 116 | throw lazyDOMException( | |
| 118 | 117 | 'AES-CTR algorithm.length must be between 1 and 128', | |
| 119 | 118 | 'OperationError'); | |
| 120 | 119 | } | |
| 120 | + } | ||
| 121 | + | ||
| 122 | + function asyncAesCtrCipher(mode, key, data, algorithm) { | ||
| 123 | + validateAesCtrAlgorithm(algorithm); | ||
| 121 | 124 | ||
| 122 | 125 | return jobPromise(() => new AESCipherJob( | |
| 123 | 126 | kCryptoJobAsync, | |
| 124 | 127 | mode, | |
| 125 | 128 | key[kKeyObject][kHandle], | |
| 126 | 129 | data, | |
| 127 | 130 | getVariant('AES-CTR', key.algorithm.length), | |
| 128 | - counter, | ||
| 129 | - length)); | ||
| 131 | + algorithm.counter, | ||
| 132 | + algorithm.length)); | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + function validateAesCbcAlgorithm(algorithm) { | ||
| 136 | + validateByteLength(algorithm.iv, 'algorithm.iv', 16); | ||
| 130 | 137 | } | |
| 131 | 138 | ||
| 132 | - function asyncAesCbcCipher(mode, key, data, { iv }) { | ||
| 133 | - validateByteLength(iv, 'algorithm.iv', 16); | ||
| 139 | + function asyncAesCbcCipher(mode, key, data, algorithm) { | ||
| 140 | + validateAesCbcAlgorithm(algorithm); | ||
| 134 | 141 | return jobPromise(() => new AESCipherJob( | |
| 135 | 142 | kCryptoJobAsync, | |
| 136 | 143 | mode, | |
| 137 | 144 | key[kKeyObject][kHandle], | |
| 138 | 145 | data, | |
| 139 | 146 | getVariant('AES-CBC', key.algorithm.length), | |
| 140 | - iv)); | ||
| 147 | + algorithm.iv)); | ||
| 141 | 148 | } | |
| 142 | 149 | ||
| 143 | 150 | function asyncAesKwCipher(mode, key, data) { | |
@@ -149,24 +156,25 @@ function asyncAesKwCipher(mode, key, data) { | |||
| 149 | 156 | getVariant('AES-KW', key.algorithm.length))); | |
| 150 | 157 | } | |
| 151 | 158 | ||
| 152 | - function asyncAesGcmCipher( | ||
| 153 | - mode, | ||
| 154 | - key, | ||
| 155 | - data, | ||
| 156 | - { iv, additionalData, tagLength = 128 }) { | ||
| 157 | - if (!ArrayPrototypeIncludes(kTagLengths, tagLength)) { | ||
| 158 | - return PromiseReject(lazyDOMException( | ||
| 159 | - `${tagLength} is not a valid AES-GCM tag length`, | ||
| 160 | - 'OperationError')); | ||
| 159 | + function validateAesGcmAlgorithm(algorithm) { | ||
| 160 | + if (!ArrayPrototypeIncludes(kTagLengths, algorithm.tagLength)) { | ||
| 161 | + throw lazyDOMException( | ||
| 162 | + `${algorithm.tagLength} is not a valid AES-GCM tag length`, | ||
| 163 | + 'OperationError'); | ||
| 161 | 164 | } | |
| 162 | 165 | ||
| 163 | - validateMaxBufferLength(iv, 'algorithm.iv'); | ||
| 166 | + validateMaxBufferLength(algorithm.iv, 'algorithm.iv'); | ||
| 164 | 167 | ||
| 165 | - if (additionalData !== undefined) { | ||
| 166 | - validateMaxBufferLength(additionalData, 'algorithm.additionalData'); | ||
| 168 | + if (algorithm.additionalData !== undefined) { | ||
| 169 | + validateMaxBufferLength(algorithm.additionalData, 'algorithm.additionalData'); | ||
| 167 | 170 | } | |
| 171 | + } | ||
| 168 | 172 | ||
| 169 | - const tagByteLength = MathFloor(tagLength / 8); | ||
| 173 | + function asyncAesGcmCipher(mode, key, data, algorithm) { | ||
| 174 | + algorithm.tagLength ??= 128; | ||
| 175 | + validateAesGcmAlgorithm(algorithm); | ||
| 176 | + | ||
| 177 | + const tagByteLength = MathFloor(algorithm.tagLength / 8); | ||
| 170 | 178 | let tag; | |
| 171 | 179 | switch (mode) { | |
| 172 | 180 | case kWebCryptoCipherDecrypt: { | |
@@ -198,9 +206,9 @@ function asyncAesGcmCipher( | |||
| 198 | 206 | key[kKeyObject][kHandle], | |
| 199 | 207 | data, | |
| 200 | 208 | getVariant('AES-GCM', key.algorithm.length), | |
| 201 | - iv, | ||
| 209 | + algorithm.iv, | ||
| 202 | 210 | tag, | |
| 203 | - additionalData)); | ||
| 211 | + algorithm.additionalData)); | ||
| 204 | 212 | } | |
| 205 | 213 | ||
| 206 | 214 | function aesCipher(mode, key, data, algorithm) { | |
@@ -212,13 +220,17 @@ function aesCipher(mode, key, data, algorithm) { | |||
| 212 | 220 | } | |
| 213 | 221 | } | |
| 214 | 222 | ||
| 215 | - async function aesGenerateKey(algorithm, extractable, keyUsages) { | ||
| 216 | - const { name, length } = algorithm; | ||
| 217 | - if (!ArrayPrototypeIncludes(kAesKeyLengths, length)) { | ||
| 223 | + function validateAesGenerateKeyAlgorithm(algorithm) { | ||
| 224 | + if (!ArrayPrototypeIncludes(kAesKeyLengths, algorithm.length)) { | ||
| 218 | 225 | throw lazyDOMException( | |
| 219 | 226 | 'AES key length must be 128, 192, or 256 bits', | |
| 220 | 227 | 'OperationError'); | |
| 221 | 228 | } | |
| 229 | + } | ||
| 230 | + | ||
| 231 | + async function aesGenerateKey(algorithm, extractable, keyUsages) { | ||
| 232 | + validateAesGenerateKeyAlgorithm(algorithm); | ||
| 233 | + const { name, length } = algorithm; | ||
| 222 | 234 | ||
| 223 | 235 | const checkUsages = ['wrapKey', 'unwrapKey']; | |
| 224 | 236 | if (name !== 'AES-KW') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -329,18 +329,21 @@ function cfrgImportKey( | |||
| 329 | 329 | extractable); | |
| 330 | 330 | } | |
| 331 | 331 | ||
| 332 | - function eddsaSignVerify(key, data, { name, context }, signature) { | ||
| 332 | + function validateEdDSASignVerifyAlgorithm(algorithm) { | ||
| 333 | + if (algorithm.name === 'Ed448' && algorithm.context?.byteLength) { | ||
| 334 | + throw lazyDOMException( | ||
| 335 | + 'Non zero-length context is not yet supported.', 'NotSupportedError'); | ||
| 336 | + } | ||
| 337 | + } | ||
| 338 | + | ||
| 339 | + function eddsaSignVerify(key, data, algorithm, signature) { | ||
| 340 | + validateEdDSASignVerifyAlgorithm(algorithm); | ||
| 333 | 341 | const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify; | |
| 334 | 342 | const type = mode === kSignJobModeSign ? 'private' : 'public'; | |
| 335 | 343 | ||
| 336 | 344 | if (key.type !== type) | |
| 337 | 345 | throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError'); | |
| 338 | 346 | ||
| 339 | - if (name === 'Ed448' && context?.byteLength) { | ||
| 340 | - throw lazyDOMException( | ||
| 341 | - 'Non zero-length context is not yet supported.', 'NotSupportedError'); | ||
| 342 | - } | ||
| 343 | - | ||
| 344 | 347 | return jobPromise(() => new SignJob( | |
| 345 | 348 | kCryptoJobAsync, | |
| 346 | 349 | mode, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -298,28 +298,28 @@ function diffieHellman(options) { | |||
| 298 | 298 | ||
| 299 | 299 | let masks; | |
| 300 | 300 | ||
| 301 | + function validateEcdhDeriveBitsAlgorithmAndLength(algorithm, length) { | ||
| 302 | + if (algorithm.public.type !== 'public') { | ||
| 303 | + throw lazyDOMException( | ||
| 304 | + 'algorithm.public must be a public key', 'InvalidAccessError'); | ||
| 305 | + } | ||
| 306 | + | ||
| 307 | + if (algorithm.name !== algorithm.public.algorithm.name) { | ||
| 308 | + throw lazyDOMException(`algorithm.public must be an ${algorithm.name} key`, 'InvalidAccessError'); | ||
| 309 | + } | ||
| 310 | + } | ||
| 311 | + | ||
| 301 | 312 | // The ecdhDeriveBits function is part of the Web Crypto API and serves both | |
| 302 | 313 | // deriveKeys and deriveBits functions. | |
| 303 | 314 | async function ecdhDeriveBits(algorithm, baseKey, length) { | |
| 315 | + validateEcdhDeriveBitsAlgorithmAndLength(algorithm, length); | ||
| 304 | 316 | const { 'public': key } = algorithm; | |
| 305 | 317 | ||
| 306 | - if (key.type !== 'public') { | ||
| 307 | - throw lazyDOMException( | ||
| 308 | - 'algorithm.public must be a public key', 'InvalidAccessError'); | ||
| 309 | - } | ||
| 310 | 318 | if (baseKey.type !== 'private') { | |
| 311 | 319 | throw lazyDOMException( | |
| 312 | 320 | 'baseKey must be a private key', 'InvalidAccessError'); | |
| 313 | 321 | } | |
| 314 | 322 | ||
| 315 | - if ( | ||
| 316 | - key.algorithm.name !== 'ECDH' && | ||
| 317 | - key.algorithm.name !== 'X25519' && | ||
| 318 | - key.algorithm.name !== 'X448' | ||
| 319 | - ) { | ||
| 320 | - throw lazyDOMException('Keys must be ECDH, X25519, or X448 keys', 'InvalidAccessError'); | ||
| 321 | - } | ||
| 322 | - | ||
| 323 | 323 | if (key.algorithm.name !== baseKey.algorithm.name) { | |
| 324 | 324 | throw lazyDOMException( | |
| 325 | 325 | 'The public and private keys must be of the same type', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,7 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | - ArrayPrototypeIncludes, | ||
| 5 | - ObjectKeys, | ||
| 4 | + ObjectPrototypeHasOwnProperty, | ||
| 6 | 5 | SafeSet, | |
| 7 | 6 | } = primordials; | |
| 8 | 7 | ||
@@ -77,14 +76,17 @@ function createECPublicKeyRaw(namedCurve, keyData) { | |||
| 77 | 76 | return new PublicKeyObject(handle); | |
| 78 | 77 | } | |
| 79 | 78 | ||
| 80 | - async function ecGenerateKey(algorithm, extractable, keyUsages) { | ||
| 81 | - const { name, namedCurve } = algorithm; | ||
| 82 | - | ||
| 83 | - if (!ArrayPrototypeIncludes(ObjectKeys(kNamedCurveAliases), namedCurve)) { | ||
| 79 | + function validateEcKeyAlgorithm(algorithm) { | ||
| 80 | + if (!ObjectPrototypeHasOwnProperty(kNamedCurveAliases, algorithm.namedCurve)) { | ||
| 84 | 81 | throw lazyDOMException( | |
| 85 | 82 | 'Unrecognized namedCurve', | |
| 86 | 83 | 'NotSupportedError'); | |
| 87 | 84 | } | |
| 85 | + } | ||
| 86 | + | ||
| 87 | + async function ecGenerateKey(algorithm, extractable, keyUsages) { | ||
| 88 | + validateEcKeyAlgorithm(algorithm); | ||
| 89 | + const { name, namedCurve } = algorithm; | ||
| 88 | 90 | ||
| 89 | 91 | const usageSet = new SafeSet(keyUsages); | |
| 90 | 92 | switch (name) { | |
@@ -154,16 +156,11 @@ function ecImportKey( | |||
| 154 | 156 | keyData, | |
| 155 | 157 | algorithm, | |
| 156 | 158 | extractable, | |
| 157 | - keyUsages) { | ||
| 158 | - | ||
| 159 | + keyUsages, | ||
| 160 | + ) { | ||
| 161 | + validateEcKeyAlgorithm(algorithm); | ||
| 159 | 162 | const { name, namedCurve } = algorithm; | |
| 160 | 163 | ||
| 161 | - if (!ArrayPrototypeIncludes(ObjectKeys(kNamedCurveAliases), namedCurve)) { | ||
| 162 | - throw lazyDOMException( | ||
| 163 | - 'Unrecognized namedCurve', | ||
| 164 | - 'NotSupportedError'); | ||
| 165 | - } | ||
| 166 | - | ||
| 167 | 164 | let keyObject; | |
| 168 | 165 | const usagesSet = new SafeSet(keyUsages); | |
| 169 | 166 | switch (format) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -138,18 +138,22 @@ function hkdfSync(hash, key, salt, info, length) { | |||
| 138 | 138 | } | |
| 139 | 139 | ||
| 140 | 140 | const hkdfPromise = promisify(hkdf); | |
| 141 | - async function hkdfDeriveBits(algorithm, baseKey, length) { | ||
| 142 | - const { hash, salt, info } = algorithm; | ||
| 143 | - | ||
| 144 | - if (length === 0) | ||
| 145 | - return new ArrayBuffer(0); | ||
| 141 | + function validateHkdfDeriveBitsAlgorithmAndLength(algorithm, length) { | ||
| 146 | 142 | if (length === null) | |
| 147 | 143 | throw lazyDOMException('length cannot be null', 'OperationError'); | |
| 148 | 144 | if (length % 8) { | |
| 149 | 145 | throw lazyDOMException( | |
| 150 | 146 | 'length must be a multiple of 8', | |
| 151 | 147 | 'OperationError'); | |
| 152 | 148 | } | |
| 149 | + } | ||
| 150 | + | ||
| 151 | + async function hkdfDeriveBits(algorithm, baseKey, length) { | ||
| 152 | + validateHkdfDeriveBitsAlgorithmAndLength(algorithm, length); | ||
| 153 | + const { hash, salt, info } = algorithm; | ||
| 154 | + | ||
| 155 | + if (length === 0) | ||
| 156 | + return new ArrayBuffer(0); | ||
| 153 | 157 | ||
| 154 | 158 | try { | |
| 155 | 159 | return await hkdfPromise( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -895,12 +895,14 @@ function isCryptoKey(obj) { | |||
| 895 | 895 | } | |
| 896 | 896 | ||
| 897 | 897 | function importGenericSecretKey( | |
| 898 | - { name, length }, | ||
| 898 | + algorithm, | ||
| 899 | 899 | format, | |
| 900 | 900 | keyData, | |
| 901 | 901 | extractable, | |
| 902 | - keyUsages) { | ||
| 902 | + keyUsages, | ||
| 903 | + ) { | ||
| 903 | 904 | const usagesSet = new SafeSet(keyUsages); | |
| 905 | + const { name } = algorithm; | ||
| 904 | 906 | if (extractable) | |
| 905 | 907 | throw lazyDOMException(`${name} keys are not extractable`, 'SyntaxError'); | |
| 906 | 908 | ||
@@ -910,47 +912,22 @@ function importGenericSecretKey( | |||
| 910 | 912 | 'SyntaxError'); | |
| 911 | 913 | } | |
| 912 | 914 | ||
| 915 | + let keyObject; | ||
| 913 | 916 | switch (format) { | |
| 914 | 917 | case 'KeyObject': { | |
| 915 | - if (hasAnyNotIn(usagesSet, ['deriveKey', 'deriveBits'])) { | ||
| 916 | - throw lazyDOMException( | ||
| 917 | - `Unsupported key usage for a ${name} key`, | ||
| 918 | - 'SyntaxError'); | ||
| 919 | - } | ||
| 920 | - | ||
| 921 | - const checkLength = keyData.symmetricKeySize * 8; | ||
| 922 | - | ||
| 923 | - // The Web Crypto spec allows for key lengths that are not multiples of | ||
| 924 | - // 8. We don't. Our check here is stricter than that defined by the spec | ||
| 925 | - // in that we require that algorithm.length match keyData.length * 8 if | ||
| 926 | - // algorithm.length is specified. | ||
| 927 | - if (length !== undefined && length !== checkLength) { | ||
| 928 | - throw lazyDOMException('Invalid key length', 'DataError'); | ||
| 929 | - } | ||
| 930 | - return new InternalCryptoKey(keyData, { name }, keyUsages, false); | ||
| 918 | + keyObject = keyData; | ||
| 919 | + break; | ||
| 931 | 920 | } | |
| 932 | 921 | case 'raw': { | |
| 933 | - if (hasAnyNotIn(usagesSet, ['deriveKey', 'deriveBits'])) { | ||
| 934 | - throw lazyDOMException( | ||
| 935 | - `Unsupported key usage for a ${name} key`, | ||
| 936 | - 'SyntaxError'); | ||
| 937 | - } | ||
| 938 | - | ||
| 939 | - const checkLength = keyData.byteLength * 8; | ||
| 940 | - | ||
| 941 | - // The Web Crypto spec allows for key lengths that are not multiples of | ||
| 942 | - // 8. We don't. Our check here is stricter than that defined by the spec | ||
| 943 | - // in that we require that algorithm.length match keyData.length * 8 if | ||
| 944 | - // algorithm.length is specified. | ||
| 945 | - if (length !== undefined && length !== checkLength) { | ||
| 946 | - throw lazyDOMException('Invalid key length', 'DataError'); | ||
| 947 | - } | ||
| 948 | - | ||
| 949 | - const keyObject = createSecretKey(keyData); | ||
| 950 | - return new InternalCryptoKey(keyObject, { name }, keyUsages, false); | ||
| 922 | + keyObject = createSecretKey(keyData); | ||
| 923 | + break; | ||
| 951 | 924 | } | |
| 952 | 925 | } | |
| 953 | 926 | ||
| 927 | + if (keyObject) { | ||
| 928 | + return new InternalCryptoKey(keyObject, { name }, keyUsages, false); | ||
| 929 | + } | ||
| 930 | + | ||
| 954 | 931 | throw lazyDOMException( | |
| 955 | 932 | `Unable to import ${name} key with format ${format}`, | |
| 956 | 933 | 'NotSupportedError'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments