GeezerVison is showing github.com — crypto: use backend cSHAKE and KMAC · nodejs/node@32ba6d5
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 |
|---|
@@ -1830,6 +1830,9 @@ changes: | 1830 | 1830 | description: Renamed `cShakeParams.length` to `cShakeParams.outputLength`. | 1831 | 1831 | --> | 1832 | 1832 |
| | 1833 | +When both `functionName` and `customization` are empty or `undefined`, cSHAKE is | | 1834 | +equivalent to plain SHAKE. | | 1835 | +
| 1833 | 1836 | #### `cShakeParams.name` | 1834 | 1837 |
| 1835 | 1838 | <!-- YAML | @@ -1844,7 +1847,8 @@ added: v24.7.0 | 1844 | 1847 | added: v25.9.0 | 1845 | 1848 | --> | 1846 | 1849 |
| 1847 | | -* Type: {number} represents the requested output length in bits. | | 1850 | +* Type: {number} represents the requested output length in bits. Must be a | | 1851 | + multiple of 8. | 1848 | 1852 |
| 1849 | 1853 | #### `cShakeParams.functionName` | 1850 | 1854 |
| @@ -1859,9 +1863,10 @@ changes: | 1859 | 1863 | * Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined} | 1860 | 1864 |
| 1861 | 1865 | The `functionName` member represents the NIST function-name byte string used to | 1862 | | -domain-separate functions built on top of cSHAKE. Accepted values are: | | 1866 | +domain-separate functions built on top of cSHAKE. Non-empty values require | | 1867 | +OpenSSL 4.0 or later. Accepted values are: | 1863 | 1868 |
| 1864 | | -* empty or `undefined`, in which case cSHAKE is equivalent to plain SHAKE | | 1869 | +* empty or `undefined` | 1865 | 1870 | * the ASCII byte sequence `'KMAC'` | 1866 | 1871 | * the ASCII byte sequence `'TupleHash'` | 1867 | 1872 | * the ASCII byte sequence `'ParallelHash'` | @@ -1878,11 +1883,11 @@ changes: | 1878 | 1883 |
| 1879 | 1884 | * Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined} | 1880 | 1885 |
| 1881 | | -The `customization` member represents the customization data. Accepted | 1882 | | -values are: | | 1886 | +The `customization` member represents the customization data. Non-empty values | | 1887 | +require OpenSSL 4.0 or later. Accepted values are: | 1883 | 1888 |
| 1884 | | -* empty or `undefined`, in which case cSHAKE is equivalent to plain SHAKE | 1885 | | -* up to 512 bytes of arbitrary data | | 1889 | +* empty or `undefined` | | 1890 | +* up to 512 bytes of data without null bytes | 1886 | 1891 |
| 1887 | 1892 | ### Class: `EcdhKeyDeriveParams` | 1888 | 1893 |
| @@ -2332,7 +2337,7 @@ added: v24.8.0 | 2332 | 2337 | * Type: {number} | 2333 | 2338 |
| 2334 | 2339 | The optional number of bits in the KMAC key. This is optional and should | 2335 | | -be omitted for most cases. | | 2340 | +be omitted for most cases. The key length must be at least 32 and a multiple of 8. | 2336 | 2341 |
| 2337 | 2342 | #### `kmacImportParams.name` | 2338 | 2343 |
| @@ -2382,7 +2387,8 @@ added: v24.8.0 | 2382 | 2387 |
| 2383 | 2388 | The number of bits to generate for the KMAC key. If omitted, | 2384 | 2389 | the length will be determined by the KMAC algorithm used. | 2385 | | -This is optional and should be omitted for most cases. | | 2390 | +This is optional and should be omitted for most cases. Must be at least 32 and a | | 2391 | +multiple of 8. | 2386 | 2392 |
| 2387 | 2393 | #### `kmacKeyGenParams.name` | 2388 | 2394 |
| @@ -2416,7 +2422,8 @@ added: v24.8.0 | 2416 | 2422 | added: v25.9.0 | 2417 | 2423 | --> | 2418 | 2424 |
| 2419 | | -* Type: {number} represents the requested output length in bits. | | 2425 | +* Type: {number} represents the requested output length in bits. Must be a | | 2426 | + multiple of 8. | 2420 | 2427 |
| 2421 | 2428 | #### `kmacParams.customization` | 2422 | 2429 |
| |
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 | getDeprecationWarningEmitter, | @@ -284,30 +278,20 @@ function asyncDigest(algorithm, data) { | 284 | 278 | const outputLength = algorithm.outputLength; | 285 | 279 | if (getOptionalByteLength(algorithm.functionName) || | 286 | 280 | getOptionalByteLength(algorithm.customization)) { | 287 | | - if (CShakeJob === undefined) { | 288 | | - throw lazyDOMException( | 289 | | - 'Non-empty CShakeParams functionName or customization is not supported', | 290 | | - 'NotSupportedError'); | 291 | | - } | 292 | | - | 293 | | - return jobPromise(() => new CShakeJob( | | 281 | + return jobPromise(() => new HashJob( | 294 | 282 | kCryptoJobWebCrypto, | 295 | | - algorithm.name, | | 283 | + StringPrototypeToLowerCase(algorithm.name), | 296 | 284 | data, | | 285 | + outputLength, | 297 | 286 | algorithm.functionName, | 298 | | - algorithm.customization, | 299 | | - outputLength)); | | 287 | + algorithm.customization)); | 300 | 288 | } | 301 | 289 |
| 302 | | - const bits = jobPromise(() => new HashJob( | | 290 | + return jobPromise(() => new HashJob( | 303 | 291 | kCryptoJobWebCrypto, | 304 | 292 | normalizeHashName(algorithm.name), | 305 | 293 | data, | 306 | | - numBitsToBytes(outputLength) * 8)); | 307 | | - if (outputLength % 8 === 0) | 308 | | - return bits; | 309 | | - return jobPromiseThen(bits, (bits) => | 310 | | - TypedArrayPrototypeGetBuffer(truncateToBitLength(outputLength, bits))); | | 294 | + outputLength)); | 311 | 295 | } | 312 | 296 | case 'TurboSHAKE128': | 313 | 297 | // 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 |
|---|
@@ -721,7 +721,7 @@ function validateMaxBufferLength(data, name, max = kMaxBufferLength) { | 721 | 721 | } | 722 | 722 |
| 723 | 723 | function validateKmacKeyLength(length) { | 724 | | - if ((length < 32 || length % 8) && isFips()) | | 724 | + if (length < 32 || length % 8 !== 0) | 725 | 725 | throw lazyDOMException('Invalid key length', 'NotSupportedError'); | 726 | 726 | } | 727 | 727 |
| |
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 |
|---|
@@ -23,7 +23,6 @@ const { | 23 | 23 | } = primordials; | 24 | 24 |
| 25 | 25 | const { | 26 | | - CShakeJob, | 27 | 26 | kWebCryptoKeyFormatRaw, | 28 | 27 | kWebCryptoKeyFormatPKCS8, | 29 | 28 | kWebCryptoKeyFormatSPKI, | @@ -72,7 +71,6 @@ const { | 72 | 71 | prepareWebCryptoResult, | 73 | 72 | validateAlgorithm, | 74 | 73 | validateMaxBufferLength, | 75 | | - getOptionalByteLength, | 76 | 74 | } = require('internal/crypto/util'); | 77 | 75 |
| 78 | 76 | const { | @@ -1923,15 +1921,7 @@ function check(op, alg, length) { | 1923 | 1921 | } | 1924 | 1922 |
| 1925 | 1923 | switch (op) { | 1926 | | - case 'digest': { | 1927 | | - if ((normalizedAlgorithm.name === 'cSHAKE128' || | 1928 | | - normalizedAlgorithm.name === 'cSHAKE256') && | 1929 | | - (getOptionalByteLength(normalizedAlgorithm.functionName) || | 1930 | | - getOptionalByteLength(normalizedAlgorithm.customization))) { | 1931 | | - return CShakeJob !== undefined; | 1932 | | - } | 1933 | | - return true; | 1934 | | - } | | 1924 | + case 'digest': | 1935 | 1925 | case 'decapsulate': | 1936 | 1926 | case 'decrypt': | 1937 | 1927 | 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'); | |
|