GeezerVison is showing github.com — crypto: wire up SHA-3 with BoringSSL · nodejs/node@12f0e03
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: deps/ncrypto/ncrypto.cc +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2804,6 +2804,13 @@ const EVP_MD* getDigestByName(const char* name) { | 2804 | 2804 | if (strcmp(name, "dss1") == 0 || strcmp(name, "DSS1") == 0) [[unlikely]] { | 2805 | 2805 | return EVP_sha1(); | 2806 | 2806 | } | | 2807 | +#if NCRYPTO_USE_BORINGSSL | | 2808 | + // BoringSSL exposes SHA-3 through EVP_MD descriptors without registering | | 2809 | + // their names or assigning them NIDs. | | 2810 | + if (CaseInsensitiveNameEqual()(name, "sha3-256")) return EVP_sha3_256(); | | 2811 | + if (CaseInsensitiveNameEqual()(name, "sha3-384")) return EVP_sha3_384(); | | 2812 | + if (CaseInsensitiveNameEqual()(name, "sha3-512")) return EVP_sha3_512(); | | 2813 | +#endif | 2807 | 2814 | return EVP_get_digestbyname(name); | 2808 | 2815 | } | 2809 | 2816 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js +11-4Lines changed: 11 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,8 @@ const { | 53 | 53 | isKeyAlgorithmAvailable, | 54 | 54 | } = internalBinding('crypto'); | 55 | 55 |
| | 56 | +const { openssl_is_boringssl: isBoringSSL } = process.features; | | 57 | + | 56 | 58 | let fips; | 57 | 59 | let fipsGeneration; | 58 | 60 | function isFips() { | @@ -525,7 +527,7 @@ function getConditionalAlgorithms() { | 525 | 527 | 'Argon2d': !!Argon2Job && !fips, | 526 | 528 | 'Argon2i': !!Argon2Job && !fips, | 527 | 529 | 'Argon2id': !!Argon2Job && !fips, | 528 | | - 'ChaCha20-Poly1305': process.features.openssl_is_boringssl || | | 530 | + 'ChaCha20-Poly1305': isBoringSSL || | 529 | 531 | has(ciphers, 'chacha20-poly1305'), | 530 | 532 | 'cSHAKE128': has(hashes, 'shake128'), | 531 | 533 | 'cSHAKE256': has(hashes, 'shake256'), | @@ -542,15 +544,15 @@ function getConditionalAlgorithms() { | 542 | 544 | 'ML-KEM-768': mlKem768, | 543 | 545 | 'ML-KEM-1024': mlKem1024, | 544 | 546 | 'MLKEM768-P256': !fips && mlKem768 && | 545 | | - (!process.features.openssl_is_boringssl || | | 547 | + (!isBoringSSL || | 546 | 548 | (has(hashes, 'sha3-256') && | 547 | 549 | has(hashes, 'shake256'))), | 548 | 550 | 'MLKEM768-X25519': !fips && mlKem768 && | 549 | | - (!process.features.openssl_is_boringssl || | | 551 | + (!isBoringSSL || | 550 | 552 | (has(hashes, 'sha3-256') && | 551 | 553 | has(hashes, 'shake256'))), | 552 | 554 | 'MLKEM1024-P384': !fips && mlKem1024 && | 553 | | - (!process.features.openssl_is_boringssl || | | 555 | + (!isBoringSSL || | 554 | 556 | (has(hashes, 'sha3-256') && | 555 | 557 | has(hashes, 'shake256'))), | 556 | 558 | 'SHA-1': has(hashes, 'sha1'), | @@ -839,6 +841,11 @@ function normalizeAlgorithm(algorithm, op) { | 839 | 841 | throw lazyDOMException( | 840 | 842 | `Only SHA hashes are supported in ${desiredType}`, 'NotSupportedError'); | 841 | 843 | } | | 844 | + if (isBoringSSL && StringPrototypeStartsWith(hash.name, 'SHA3-')) { | | 845 | + throw lazyDOMException( | | 846 | + 'SHA-3 is only supported for digest operations', | | 847 | + 'NotSupportedError'); | | 848 | + } | 842 | 849 | normalizedAlgorithm[member] = hash; | 843 | 850 | } else if (idlType === 'AlgorithmIdentifier') { | 844 | 851 | // This extension point is not used by any supported algorithm (yet?) | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webidl.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,8 @@ const { | 14 | 14 | TypedArrayPrototypeIncludes, | 15 | 15 | } = primordials; | 16 | 16 |
| | 17 | +const { openssl_is_boringssl: isBoringSSL } = process.features; | | 18 | + | 17 | 19 | const { | 18 | 20 | lazyDOMException, | 19 | 21 | kEmptyObject, | @@ -694,7 +696,7 @@ converters.ContextParams = createAlgorithmDictionaryConverter( | 694 | 696 | const validateLength = (V) => | 695 | 697 | validateMaxBufferLength(V, 'ContextParams.context', 255); | 696 | 698 |
| 697 | | - if (process.features.openssl_is_boringssl) { | | 699 | + if (isBoringSSL) { | 698 | 700 | this.validator = validateLength; | 699 | 701 | } else { | 700 | 702 | let { 0: major, 1: minor } = | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_hash.cc +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -255,6 +255,12 @@ const std::vector<std::string>& GetSupportedHashAlgorithms(Environment* env) { | 255 | 255 | EVP_MD_do_all_provided(nullptr, SaveSupportedProviderHashAlgorithms, env); | 256 | 256 | #else | 257 | 257 | EVP_MD_do_all_sorted(SaveSupportedHashAlgorithms, env); | | 258 | +#endif | | 259 | +#if NCRYPTO_USE_BORINGSSL | | 260 | + // SHA-3 descriptors are not included in BoringSSL's name registry. | | 261 | + for (const char* name : {"sha3-256", "sha3-384", "sha3-512"}) { | | 262 | + env->supported_hash_algorithms.emplace_back(name); | | 263 | + } | 258 | 264 | #endif | 259 | 265 | } | 260 | 266 | const uint64_t current_generation = ncrypto::getFipsStateGeneration(); | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/crypto/ecdsa.js +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { isBoringSSL } = require('../../common/crypto'); | 4 | 4 |
| 5 | | -module.exports = function() { | | 5 | +module.exports = function({ includeUnsupported = false } = {}) { | | 6 | + const includeSha3 = !isBoringSSL || includeUnsupported; | 6 | 7 | const pkcs8 = { | 7 | 8 | 'P-256': Buffer.from( | 8 | 9 | '308187020100301306072a8648ce3d020106082a8648ce3d030107046d306b020101' + | @@ -74,7 +75,7 @@ module.exports = function() { | 74 | 75 | 'b6a0a14d7e4bc6dd2eda82c9234f174b670b60c8f7d101f68fdf5889e02373b025' + | 75 | 76 | 'dcbc4c82f2929b8e06c68535da98e38fe399c53a814b097935581ef21535eb', | 76 | 77 | 'hex'), | 77 | | - ...(!isBoringSSL ? { | | 78 | + ...(includeSha3 ? { | 78 | 79 | 'SHA3-256': Buffer.from( | 79 | 80 | 'f6a48eb5557f484ed0c3e4b5c78a3cf497cbd346db06a4165d429248aa2cc51a69' + | 80 | 81 | '747d09f57af145469a8b607a9b8b9709629d74e8f5ca337c6ddc581b6f6103', | @@ -106,7 +107,7 @@ module.exports = function() { | 106 | 107 | '72fbdb369fd34c1c54264d07f4facd69b02e4206f8a8bb259b882a305c56fde2d3' + | 107 | 108 | '5107e493c53cd6b4af0b31306f4d03fd43cfc762a1030e17a3d775453a1212b142' + | 108 | 109 | '9f7b3d93066a5f42a10b138cd177dc09616e827d598822d78d4627b754e6', 'hex'), | 109 | | - ...(!isBoringSSL ? { | | 110 | + ...(includeSha3 ? { | 110 | 111 | 'SHA3-256': Buffer.from( | 111 | 112 | '0b07c078be30fa5925a307d6fc559c5f398e63fb5d007d6b24a834847f2d3d18d5' + | 112 | 113 | 'b5e840711c52a7bc6626c3ced93301e873c013a706f6b297c12cc6d47a71e0529e' + | @@ -146,7 +147,7 @@ module.exports = function() { | 146 | 147 | '01f0071e6a32867fa70f695cd39c4e87e142b9e4134d38740bd6fee354a575167e' + | 147 | 148 | '13524e94832637910fe11e53a85fb21b91adb81bb1779c4e2b8bc87c717dc35084', | 148 | 149 | 'hex'), | 149 | | - ...(!isBoringSSL ? { | | 150 | + ...(includeSha3 ? { | 150 | 151 | 'SHA3-256': Buffer.from( | 151 | 152 | '00463679f47a4c705e03447360dcf34d1743e0d4b2591cc66832a6bc80d92e538c' + | 152 | 153 | '169a1fd330f98e7235ca7fec7e16ac44fb13095b8edf2c76b75c4845177d59e425' + | @@ -172,7 +173,7 @@ module.exports = function() { | 172 | 173 | const curves = ['P-256', 'P-384', 'P-521']; | 173 | 174 | const hashes = ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512']; | 174 | 175 |
| 175 | | - if (!isBoringSSL) { | | 176 | + if (includeSha3) { | 176 | 177 | hashes.push('SHA3-256', 'SHA3-384', 'SHA3-512'); | 177 | 178 | } | 178 | 179 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/crypto/hmac.js +20-26Lines changed: 20 additions & 26 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -const { isBoringSSL } = require('../../common/crypto'); | 4 | | - | 5 | 3 | module.exports = function () { | 6 | 4 | const plaintext = Buffer.from( | 7 | 5 | '5f4dba4f320c0ce876725afce5fbd25bf83e5a7125a08cafe73c3ebac421779df9d55d' + | @@ -24,18 +22,16 @@ module.exports = function () { | 24 | 22 | '5dcc359443aaf652fa1375d6b3e61fdcf29bb4a28bd5d3dcfa40f82f906bb280' + | 25 | 23 | '0455db03b5d31fb972a15a6d0103a24e56d156a119c0e5a1e92a44c3c5657cf9', | 26 | 24 | 'hex'), | 27 | | - ...(!isBoringSSL ? { | 28 | | - 'SHA3-256': Buffer.from( | 29 | | - 'e588ec0811463d767241df1074b47ae4071b51f2ce36537ba69ccdc3fdc2b7a8', | 30 | | - 'hex'), | 31 | | - 'SHA3-384': Buffer.from( | 32 | | - '6b1da28eab1f582ad9718effe05e23d5fd2c9877a2d9443f90bec093bece2ea7' + | 33 | | - 'd2354cd0bdc5e147d2e9009373494488', 'hex'), | 34 | | - 'SHA3-512': Buffer.from( | 35 | | - '5dcc359443aaf652fa1375d6b3e61fdcf29bb4a28bd5d3dcfa40f82f906bb280' + | 36 | | - '0455db03b5d31fb972a15a6d0103a24e56d156a119c0e5a1e92a44c3c5657cf9', | 37 | | - 'hex') | 38 | | - } : {}) | | 25 | + 'SHA3-256': Buffer.from( | | 26 | + 'e588ec0811463d767241df1074b47ae4071b51f2ce36537ba69ccdc3fdc2b7a8', | | 27 | + 'hex'), | | 28 | + 'SHA3-384': Buffer.from( | | 29 | + '6b1da28eab1f582ad9718effe05e23d5fd2c9877a2d9443f90bec093bece2ea7' + | | 30 | + 'd2354cd0bdc5e147d2e9009373494488', 'hex'), | | 31 | + 'SHA3-512': Buffer.from( | | 32 | + '5dcc359443aaf652fa1375d6b3e61fdcf29bb4a28bd5d3dcfa40f82f906bb280' + | | 33 | + '0455db03b5d31fb972a15a6d0103a24e56d156a119c0e5a1e92a44c3c5657cf9', | | 34 | + 'hex') | 39 | 35 | } | 40 | 36 |
| 41 | 37 | const signatures = { | @@ -50,18 +46,16 @@ module.exports = function () { | 50 | 46 | '61fb278c3ffb0cce2bf1cf723ddfd8ef1f931c0c618c25907324605939e3f9a2' + | 51 | 47 | 'c6f4af690bda3407dc2f5770f6a0a44b954d64a332e3ee0821abf82b7f3e99c1', | 52 | 48 | 'hex'), | 53 | | - ...(!isBoringSSL ? { | 54 | | - 'SHA3-256': Buffer.from( | 55 | | - 'c1ac5e11fcd50c48bf567f6e296632f5801c4eb07a8a47579b41dee971a3099b', | 56 | | - 'hex'), | 57 | | - 'SHA3-384': Buffer.from( | 58 | | - 'ac8c97f6dd8d9e16101063077c16b23fe291a5e6d149653e9ac7002365159317' + | 59 | | - 'adcfad511996578b0053a5c14b75f16c', 'hex'), | 60 | | - 'SHA3-512': Buffer.from( | 61 | | - '2162c2a8907e6b2f68599a69e81a464d8f076b5eeb555d98b4d20330034df3c7' + | 62 | | - 'cf35b1fa958a074ca12f0d242df39f0da3d4f1dbfb3629057798fe1f883974ee', | 63 | | - 'hex') | 64 | | - } : {}) | | 49 | + 'SHA3-256': Buffer.from( | | 50 | + 'c1ac5e11fcd50c48bf567f6e296632f5801c4eb07a8a47579b41dee971a3099b', | | 51 | + 'hex'), | | 52 | + 'SHA3-384': Buffer.from( | | 53 | + 'ac8c97f6dd8d9e16101063077c16b23fe291a5e6d149653e9ac7002365159317' + | | 54 | + 'adcfad511996578b0053a5c14b75f16c', 'hex'), | | 55 | + 'SHA3-512': Buffer.from( | | 56 | + '2162c2a8907e6b2f68599a69e81a464d8f076b5eeb555d98b4d20330034df3c7' + | | 57 | + 'cf35b1fa958a074ca12f0d242df39f0da3d4f1dbfb3629057798fe1f883974ee', | | 58 | + 'hex') | 65 | 59 | } | 66 | 60 |
| 67 | 61 | const vectors = []; | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/crypto/rsa_pkcs.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { isBoringSSL } = require('../../common/crypto'); | 4 | 4 |
| 5 | | -module.exports = function () { | | 5 | +module.exports = function ({ includeUnsupported = false } = {}) { | | 6 | + const includeSha3 = !isBoringSSL || includeUnsupported; | 6 | 7 | const pkcs8 = Buffer.from( | 7 | 8 | '308204bf020100300d06092a864886f70d0101010500048204a9308204a50201000282' + | 8 | 9 | '010100d3576092e62957364544e7e4233b7bdb293db2085122c479328546f9f0f712f6' + | @@ -99,7 +100,7 @@ module.exports = function () { | 99 | 100 | '7a6335c70e193235dcda48add6858626bd96311e60f7e5ea4491b6c1e6248afe12b' + | 100 | 101 | 'bbd54f8869b043a5b0444562813f0a98b300356f306e6b783a29f3bec97ca40ea20' + | 101 | 102 | '062cab8926ec5d96aa387cc84821a6d72b8ea126e7d', 'hex'), | 102 | | - ...(!isBoringSSL ? { | | 103 | + ...(includeSha3 ? { | 103 | 104 | 'sha3-256': Buffer.from( | 104 | 105 | 'be1b476c1911a01d71710fd8a2f3158d6f7839e91443b01bed30dfdd04336d80c6b' + | 105 | 106 | 'f692c06fad254877901c10a73853e8fb202a29cddefdf16c3adcda1fc123625897d' + | @@ -163,7 +164,7 @@ module.exports = function () { | 163 | 164 | plaintext, | 164 | 165 | signature: signatures['sha-512'] | 165 | 166 | }, | 166 | | - ...(!isBoringSSL ? [ | | 167 | + ...(includeSha3 ? [ | 167 | 168 | { | 168 | 169 | publicKeyBuffer: spki, | 169 | 170 | privateKeyBuffer: pkcs8, | |
|