GeezerVison is showing github.com — crypto: use backend cSHAKE and KMAC · panva/node@9194871
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: doc/api/webcrypto.md +17-10Lines changed: 17 additions & 10 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1829,6 +1829,9 @@ changes: | 1829 | 1829 | description: Renamed `cShakeParams.length` to `cShakeParams.outputLength`. | 1830 | 1830 | --> | 1831 | 1831 |
| | 1832 | +When both `functionName` and `customization` are empty or `undefined`, cSHAKE is | | 1833 | +equivalent to plain SHAKE. | | 1834 | +
| 1832 | 1835 | #### `cShakeParams.name` | 1833 | 1836 |
| 1834 | 1837 | <!-- YAML | @@ -1843,7 +1846,8 @@ added: v24.7.0 | 1843 | 1846 | added: v24.15.0 | 1844 | 1847 | --> | 1845 | 1848 |
| 1846 | | -* Type: {number} represents the requested output length in bits. | | 1849 | +* Type: {number} represents the requested output length in bits. Must be a | | 1850 | + multiple of 8. | 1847 | 1851 |
| 1848 | 1852 | #### `cShakeParams.functionName` | 1849 | 1853 |
| @@ -1858,9 +1862,10 @@ changes: | 1858 | 1862 | * Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined} | 1859 | 1863 |
| 1860 | 1864 | The `functionName` member represents the NIST function-name byte string used to | 1861 | | -domain-separate functions built on top of cSHAKE. Accepted values are: | | 1865 | +domain-separate functions built on top of cSHAKE. Non-empty values require | | 1866 | +OpenSSL 4.0 or later. Accepted values are: | 1862 | 1867 |
| 1863 | | -* empty or `undefined`, in which case cSHAKE is equivalent to plain SHAKE | | 1868 | +* empty or `undefined` | 1864 | 1869 | * the ASCII byte sequence `'KMAC'` | 1865 | 1870 | * the ASCII byte sequence `'TupleHash'` | 1866 | 1871 | * the ASCII byte sequence `'ParallelHash'` | @@ -1877,11 +1882,11 @@ changes: | 1877 | 1882 |
| 1878 | 1883 | * Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined} | 1879 | 1884 |
| 1880 | | -The `customization` member represents the customization data. Accepted | 1881 | | -values are: | | 1885 | +The `customization` member represents the customization data. Non-empty values | | 1886 | +require OpenSSL 4.0 or later. Accepted values are: | 1882 | 1887 |
| 1883 | | -* empty or `undefined`, in which case cSHAKE is equivalent to plain SHAKE | 1884 | | -* up to 512 bytes of arbitrary data | | 1888 | +* empty or `undefined` | | 1889 | +* up to 512 bytes of data without null bytes | 1885 | 1890 |
| 1886 | 1891 | ### Class: `EcdhKeyDeriveParams` | 1887 | 1892 |
| @@ -2331,7 +2336,7 @@ added: v24.8.0 | 2331 | 2336 | * Type: {number} | 2332 | 2337 |
| 2333 | 2338 | The optional number of bits in the KMAC key. This is optional and should | 2334 | | -be omitted for most cases. | | 2339 | +be omitted for most cases. The key length must be at least 32 and a multiple of 8. | 2335 | 2340 |
| 2336 | 2341 | #### `kmacImportParams.name` | 2337 | 2342 |
| @@ -2381,7 +2386,8 @@ added: v24.8.0 | 2381 | 2386 |
| 2382 | 2387 | The number of bits to generate for the KMAC key. If omitted, | 2383 | 2388 | the length will be determined by the KMAC algorithm used. | 2384 | | -This is optional and should be omitted for most cases. | | 2389 | +This is optional and should be omitted for most cases. Must be at least 32 and a | | 2390 | +multiple of 8. | 2385 | 2391 |
| 2386 | 2392 | #### `kmacKeyGenParams.name` | 2387 | 2393 |
| @@ -2415,7 +2421,8 @@ added: v24.8.0 | 2415 | 2421 | added: v24.15.0 | 2416 | 2422 | --> | 2417 | 2423 |
| 2418 | | -* Type: {number} represents the requested output length in bits. | | 2424 | +* Type: {number} represents the requested output length in bits. Must be a | | 2425 | + multiple of 8. | 2419 | 2426 |
| 2420 | 2427 | #### `kmacParams.customization` | 2421 | 2428 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/hash.js +6-22Lines changed: 6 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,12 +6,10 @@ const { | 6 | 6 | StringPrototypeReplace, | 7 | 7 | StringPrototypeToLowerCase, | 8 | 8 | Symbol, | 9 | | - TypedArrayPrototypeGetBuffer, | 10 | 9 | TypedArrayPrototypeIncludes, | 11 | 10 | } = primordials; | 12 | 11 |
| 13 | 12 | const { | 14 | | - CShakeJob, | 15 | 13 | Hash: _Hash, | 16 | 14 | HashJob, | 17 | 15 | Hmac: _Hmac, | @@ -24,10 +22,7 @@ const { | 24 | 22 | const { | 25 | 23 | getStringOption, | 26 | 24 | jobPromise, | 27 | | - jobPromiseThen, | 28 | 25 | normalizeHashName, | 29 | | - numBitsToBytes, | 30 | | - truncateToBitLength, | 31 | 26 | validateAlgorithm, | 32 | 27 | validateMaxBufferLength, | 33 | 28 | kHandle, | @@ -43,7 +38,6 @@ const { | 43 | 38 | } = require('internal/crypto/keys'); | 44 | 39 |
| 45 | 40 | const { | 46 | | - lazyDOMException, | 47 | 41 | normalizeEncoding, | 48 | 42 | encodingsMap, | 49 | 43 | isPendingDeprecation, | @@ -286,30 +280,20 @@ function asyncDigest(algorithm, data) { | 286 | 280 | const outputLength = algorithm.outputLength; | 287 | 281 | if (getOptionalByteLength(algorithm.functionName) || | 288 | 282 | getOptionalByteLength(algorithm.customization)) { | 289 | | - if (CShakeJob === undefined) { | 290 | | - throw lazyDOMException( | 291 | | - 'Non-empty CShakeParams functionName or customization is not supported', | 292 | | - 'NotSupportedError'); | 293 | | - } | 294 | | - | 295 | | - return jobPromise(() => new CShakeJob( | | 283 | + return jobPromise(() => new HashJob( | 296 | 284 | kCryptoJobWebCrypto, | 297 | | - algorithm.name, | | 285 | + StringPrototypeToLowerCase(algorithm.name), | 298 | 286 | data, | | 287 | + outputLength, | 299 | 288 | algorithm.functionName, | 300 | | - algorithm.customization, | 301 | | - outputLength)); | | 289 | + algorithm.customization)); | 302 | 290 | } | 303 | 291 |
| 304 | | - const bits = jobPromise(() => new HashJob( | | 292 | + return jobPromise(() => new HashJob( | 305 | 293 | kCryptoJobWebCrypto, | 306 | 294 | normalizeHashName(algorithm.name), | 307 | 295 | data, | 308 | | - numBitsToBytes(outputLength) * 8)); | 309 | | - if (outputLength % 8 === 0) | 310 | | - return bits; | 311 | | - return jobPromiseThen(bits, (bits) => | 312 | | - TypedArrayPrototypeGetBuffer(truncateToBitLength(outputLength, bits))); | | 296 | + outputLength)); | 313 | 297 | } | 314 | 298 | case 'TurboSHAKE128': | 315 | 299 | // Fall through | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/mac.js -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -193,7 +193,6 @@ function kmacSignVerify(key, data, algorithm, signature) { | 193 | 193 | getCryptoKeyHandle(key), | 194 | 194 | algorithm.name, | 195 | 195 | algorithm.customization, | 196 | | - getCryptoKeyAlgorithm(key).length, | 197 | 196 | algorithm.outputLength, | 198 | 197 | data, | 199 | 198 | signature)); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -722,7 +722,7 @@ function validateMaxBufferLength(data, name, max = kMaxBufferLength) { | 722 | 722 | } | 723 | 723 |
| 724 | 724 | function validateKmacKeyLength(length) { | 725 | | - if ((length < 32 || length % 8) && isFips()) | | 725 | + if (length < 32 || length % 8 !== 0) | 726 | 726 | throw lazyDOMException('Invalid key length', 'NotSupportedError'); | 727 | 727 | } | 728 | 728 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webcrypto.js +1-11Lines changed: 1 addition & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,7 +24,6 @@ const { | 24 | 24 | } = primordials; | 25 | 25 |
| 26 | 26 | const { | 27 | | - CShakeJob, | 28 | 27 | kWebCryptoKeyFormatRaw, | 29 | 28 | kWebCryptoKeyFormatPKCS8, | 30 | 29 | kWebCryptoKeyFormatSPKI, | @@ -73,7 +72,6 @@ const { | 73 | 72 | prepareWebCryptoResult, | 74 | 73 | validateAlgorithm, | 75 | 74 | validateMaxBufferLength, | 76 | | - getOptionalByteLength, | 77 | 75 | } = require('internal/crypto/util'); | 78 | 76 |
| 79 | 77 | const { | @@ -1926,15 +1924,7 @@ function check(op, alg, length) { | 1926 | 1924 | } | 1927 | 1925 |
| 1928 | 1926 | switch (op) { | 1929 | | - case 'digest': { | 1930 | | - if ((normalizedAlgorithm.name === 'cSHAKE128' || | 1931 | | - normalizedAlgorithm.name === 'cSHAKE256') && | 1932 | | - (getOptionalByteLength(normalizedAlgorithm.functionName) || | 1933 | | - getOptionalByteLength(normalizedAlgorithm.customization))) { | 1934 | | - return CShakeJob !== undefined; | 1935 | | - } | 1936 | | - return true; | 1937 | | - } | | 1927 | + case 'digest': | 1938 | 1928 | case 'decapsulate': | 1939 | 1929 | case 'decrypt': | 1940 | 1930 | case 'encapsulate': | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webidl.js +14-11Lines changed: 14 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,15 +11,13 @@ const { | 11 | 11 | StringPrototypeCharCodeAt, | 12 | 12 | StringPrototypeSplit, | 13 | 13 | StringPrototypeToLowerCase, | | 14 | + TypedArrayPrototypeIncludes, | 14 | 15 | } = primordials; | 15 | 16 |
| 16 | 17 | const { | 17 | 18 | lazyDOMException, | 18 | 19 | kEmptyObject, | 19 | 20 | } = require('internal/util'); | 20 | | -const { | 21 | | - isUint32, | 22 | | -} = require('internal/validators'); | 23 | 21 | const { | 24 | 22 | getCryptoKeyAlgorithm, | 25 | 23 | getCryptoKeyType, | @@ -30,9 +28,9 @@ const { | 30 | 28 | validateMaxBufferLength, | 31 | 29 | getBufferSourceByteLength, | 32 | 30 | getBufferSourceBytes, | | 31 | + getHashes, | 33 | 32 | isFips, | 34 | 33 | kNamedCurveAliases, | 35 | | - numBitsToBytes, | 36 | 34 | validateKmacKeyLength, | 37 | 35 | } = require('internal/crypto/util'); | 38 | 36 | const { | @@ -317,20 +315,20 @@ function validateZeroLength(parameterName) { | 317 | 315 | } | 318 | 316 |
| 319 | 317 | function validateCShakeOutputLength(V) { | 320 | | - if (!isUint32(numBitsToBytes(V) * 8)) { | | 318 | + if (V % 8 !== 0) { | 321 | 319 | throw lazyDOMException( | 322 | 320 | 'Invalid CShakeParams outputLength', | 323 | | - 'OperationError'); | | 321 | + 'NotSupportedError'); | 324 | 322 | } | 325 | 323 | } | 326 | 324 |
| 327 | 325 | const kCShakeFunctionNames = ['KMAC', 'TupleHash', 'ParallelHash']; | 328 | 326 |
| 329 | | -function validateCShakeFunctionName(V) { | | 327 | +function validateCShakeFunctionName(V, dict) { | 330 | 328 | const length = getBufferSourceByteLength(V); | 331 | 329 | if (length === 0) return; | 332 | 330 |
| 333 | | - if (!isFips()) { | | 331 | + if (ArrayPrototypeIncludes(getHashes(), StringPrototypeToLowerCase(dict.name))) { | 334 | 332 | const bytes = getBufferSourceBytes(V); | 335 | 333 | for (let i = 0; i < kCShakeFunctionNames.length; i++) { | 336 | 334 | const functionName = kCShakeFunctionNames[i]; | @@ -349,12 +347,17 @@ function validateCShakeFunctionName(V) { | 349 | 347 | 'NotSupportedError'); | 350 | 348 | } | 351 | 349 |
| 352 | | -function validateCShakeCustomization(V) { | 353 | | - if (isFips() && getBufferSourceByteLength(V) !== 0) | | 350 | +function validateCShakeCustomization(V, dict) { | | 351 | + if (getBufferSourceByteLength(V) === 0) return; | | 352 | + if (!ArrayPrototypeIncludes(getHashes(), StringPrototypeToLowerCase(dict.name))) | 354 | 353 | throw lazyDOMException( | 355 | 354 | 'Unsupported CShakeParams customization', | 356 | 355 | 'NotSupportedError'); | 357 | 356 | validateMaxBufferLength(V, 'CShakeParams.customization', 512); | | 357 | + if (TypedArrayPrototypeIncludes(getBufferSourceBytes(V), 0)) | | 358 | + throw lazyDOMException( | | 359 | + 'Unsupported CShakeParams customization', | | 360 | + 'NotSupportedError'); | 358 | 361 | } | 359 | 362 |
| 360 | 363 | converters.RsaPssParams = createAlgorithmDictionaryConverter( | @@ -814,7 +817,7 @@ converters.KmacParams = createAlgorithmDictionaryConverter( | 814 | 817 | converter: (V, opts) => | 815 | 818 | converters['unsigned long'](V, enforceRangeOptions(opts)), | 816 | 819 | validator: (V) => { | 817 | | - if ((V === 0 || V % 8) && isFips()) | | 820 | + if (V % 8 !== 0 || (V === 0 && isFips())) | 818 | 821 | throw lazyDOMException( | 819 | 822 | 'Invalid KmacParams outputLength', | 820 | 823 | 'NotSupportedError'); | |
|