GeezerVison is showing github.com — crypto: move typechecking for timingSafeEqual into C++ · lerouxb/node@1d7be32
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/crypto.js +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,7 +44,11 @@ const { getOptionValue } = require('internal/options'); | 44 | 44 | const pendingDeprecation = getOptionValue('--pending-deprecation'); | 45 | 45 | const { fipsMode } = internalBinding('config'); | 46 | 46 | const fipsForced = getOptionValue('--force-fips'); | 47 | | -const { getFipsCrypto, setFipsCrypto } = internalBinding('crypto'); | | 47 | +const { | | 48 | + getFipsCrypto, | | 49 | + setFipsCrypto, | | 50 | + timingSafeEqual, | | 51 | +} = internalBinding('crypto'); | 48 | 52 | const { | 49 | 53 | randomBytes, | 50 | 54 | randomFill, | @@ -101,7 +105,6 @@ const { | 101 | 105 | getHashes, | 102 | 106 | setDefaultEncoding, | 103 | 107 | setEngine, | 104 | | - timingSafeEqual | 105 | 108 | } = require('internal/crypto/util'); | 106 | 109 | const Certificate = require('internal/crypto/certificate'); | 107 | 110 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/util.js -18Lines changed: 0 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | getCurves: _getCurves, | 10 | 10 | getHashes: _getHashes, | 11 | 11 | setEngine: _setEngine, | 12 | | - timingSafeEqual: _timingSafeEqual | 13 | 12 | } = internalBinding('crypto'); | 14 | 13 |
| 15 | 14 | const { | @@ -20,7 +19,6 @@ const { | 20 | 19 | hideStackFrames, | 21 | 20 | codes: { | 22 | 21 | ERR_CRYPTO_ENGINE_UNKNOWN, | 23 | | - ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, | 24 | 22 | ERR_INVALID_ARG_TYPE, | 25 | 23 | } | 26 | 24 | } = require('internal/errors'); | @@ -76,21 +74,6 @@ function setEngine(id, flags) { | 76 | 74 | throw new ERR_CRYPTO_ENGINE_UNKNOWN(id); | 77 | 75 | } | 78 | 76 |
| 79 | | -function timingSafeEqual(buf1, buf2) { | 80 | | - if (!isArrayBufferView(buf1)) { | 81 | | - throw new ERR_INVALID_ARG_TYPE('buf1', | 82 | | - ['Buffer', 'TypedArray', 'DataView'], buf1); | 83 | | - } | 84 | | - if (!isArrayBufferView(buf2)) { | 85 | | - throw new ERR_INVALID_ARG_TYPE('buf2', | 86 | | - ['Buffer', 'TypedArray', 'DataView'], buf2); | 87 | | - } | 88 | | - if (buf1.byteLength !== buf2.byteLength) { | 89 | | - throw new ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH(); | 90 | | - } | 91 | | - return _timingSafeEqual(buf1, buf2); | 92 | | -} | 93 | | - | 94 | 77 | const getArrayBufferView = hideStackFrames((buffer, name, encoding) => { | 95 | 78 | if (typeof buffer === 'string') { | 96 | 79 | if (encoding === 'buffer') | @@ -116,6 +99,5 @@ module.exports = { | 116 | 99 | kHandle, | 117 | 100 | setDefaultEncoding, | 118 | 101 | setEngine, | 119 | | - timingSafeEqual, | 120 | 102 | toBuf | 121 | 103 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/errors.js -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -808,8 +808,6 @@ E('ERR_CRYPTO_SCRYPT_INVALID_PARAMETER', 'Invalid scrypt parameter', Error); | 808 | 808 | E('ERR_CRYPTO_SCRYPT_NOT_SUPPORTED', 'Scrypt algorithm not supported', Error); | 809 | 809 | // Switch to TypeError. The current implementation does not seem right. | 810 | 810 | E('ERR_CRYPTO_SIGN_KEY_REQUIRED', 'No key provided to sign', Error); | 811 | | -E('ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH', | 812 | | - 'Input buffers must have the same byte length', RangeError); | 813 | 811 | E('ERR_DIR_CLOSED', 'Directory handle was closed', Error); | 814 | 812 | E('ERR_DIR_CONCURRENT_OPERATION', | 815 | 813 | 'Cannot do synchronous work on directory handle with concurrent ' + | |
Collapse file Copy file name to clipboardExpand all lines: src/node_crypto.cc +23-3Lines changed: 23 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6831,10 +6831,30 @@ void StatelessDiffieHellman(const FunctionCallbackInfo<Value>& args) { | 6831 | 6831 |
| 6832 | 6832 |
| 6833 | 6833 | void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) { | 6834 | | - ArrayBufferViewContents<char> buf1(args[0]); | 6835 | | - ArrayBufferViewContents<char> buf2(args[1]); | | 6834 | + // Moving the type checking into JS leads to test failures, most likely due | | 6835 | + // to V8 inlining certain parts of the wrapper. Therefore, keep them in C++. | | 6836 | + // Refs: https://github.com/nodejs/node/issues/34073. | | 6837 | + Environment* env = Environment::GetCurrent(args); | | 6838 | + if (!args[0]->IsArrayBufferView()) { | | 6839 | + THROW_ERR_INVALID_ARG_TYPE( | | 6840 | + env, "The \"buf1\" argument must be an instance of " | | 6841 | + "Buffer, TypedArray, or DataView."); | | 6842 | + return; | | 6843 | + } | | 6844 | + if (!args[1]->IsArrayBufferView()) { | | 6845 | + THROW_ERR_INVALID_ARG_TYPE( | | 6846 | + env, "The \"buf2\" argument must be an instance of " | | 6847 | + "Buffer, TypedArray, or DataView."); | | 6848 | + return; | | 6849 | + } | | 6850 | + | | 6851 | + ArrayBufferViewContents<char> buf1(args[0].As<ArrayBufferView>()); | | 6852 | + ArrayBufferViewContents<char> buf2(args[1].As<ArrayBufferView>()); | 6836 | 6853 |
| 6837 | | - CHECK_EQ(buf1.length(), buf2.length()); | | 6854 | + if (buf1.length() != buf2.length()) { | | 6855 | + THROW_ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH(env); | | 6856 | + return; | | 6857 | + } | 6838 | 6858 |
| 6839 | 6859 | return args.GetReturnValue().Set( | 6840 | 6860 | CRYPTO_memcmp(buf1.data(), buf2.data(), buf1.length()) == 0); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_errors.h +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,6 +33,7 @@ void OnFatalError(const char* location, const char* message); | 33 | 33 | V(ERR_BUFFER_TOO_LARGE, Error) \ | 34 | 34 | V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError) \ | 35 | 35 | V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \ | | 36 | + V(ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, RangeError) \ | 36 | 37 | V(ERR_CRYPTO_UNKNOWN_CIPHER, Error) \ | 37 | 38 | V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \ | 38 | 39 | V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error) \ | @@ -88,6 +89,8 @@ void OnFatalError(const char* location, const char* message); | 88 | 89 | "Buffer is not available for the current Context") \ | 89 | 90 | V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \ | 90 | 91 | V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \ | | 92 | + V(ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH, \ | | 93 | + "Input buffers must have the same byte length") \ | 91 | 94 | V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \ | 92 | 95 | V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \ | 93 | 96 | V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, \ | |
Collapse file Copy file name to clipboardExpand all lines: test/sequential/test-crypto-timing-safe-equal.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,7 +48,7 @@ assert.throws( | 48 | 48 | name: 'TypeError', | 49 | 49 | message: | 50 | 50 | 'The "buf1" argument must be an instance of Buffer, TypedArray, or ' + | 51 | | - "DataView. Received type string ('not a buffer')" | | 51 | + 'DataView.' | 52 | 52 | } | 53 | 53 | ); | 54 | 54 |
| @@ -59,6 +59,6 @@ assert.throws( | 59 | 59 | name: 'TypeError', | 60 | 60 | message: | 61 | 61 | 'The "buf2" argument must be an instance of Buffer, TypedArray, or ' + | 62 | | - "DataView. Received type string ('not a buffer')" | | 62 | + 'DataView.' | 63 | 63 | } | 64 | 64 | ); |
|