| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -648,6 +648,12 @@ Used when an invalid crypto engine identifier is passed to | |||
| 648 | 648 | ||
| 649 | 649 | Used when an invalid [crypto digest algorithm][] is specified. | |
| 650 | 650 | ||
| 651 | + <a id="ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH"></a> | ||
| 652 | + ### ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH | ||
| 653 | + | ||
| 654 | + Used when calling [`crypto.timingSafeEqual()`][] with `Buffer`, `TypedArray`, | ||
| 655 | + or `DataView` arguments of different lengths. | ||
| 656 | + | ||
| 651 | 657 | <a id="ERR_DNS_SET_SERVERS_FAILED"></a> | |
| 652 | 658 | ### ERR_DNS_SET_SERVERS_FAILED | |
| 653 | 659 | ||
@@ -1348,6 +1354,7 @@ Used when a given value is out of the accepted range. | |||
| 1348 | 1354 | Used when an attempt is made to use a `zlib` object after it has already been | |
| 1349 | 1355 | closed. | |
| 1350 | 1356 | ||
| 1357 | + [`crypto.timingSafeEqual()`]: crypto.html#crypto_crypto_timingsafeequal_a_b | ||
| 1351 | 1358 | [`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE | |
| 1352 | 1359 | [`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal | |
| 1353 | 1360 | [`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,7 +34,6 @@ const constants = process.binding('constants').crypto; | |||
| 34 | 34 | const { | |
| 35 | 35 | getFipsCrypto, | |
| 36 | 36 | setFipsCrypto, | |
| 37 | - timingSafeEqual | ||
| 38 | 37 | } = process.binding('crypto'); | |
| 39 | 38 | const { | |
| 40 | 39 | randomBytes, | |
@@ -75,6 +74,7 @@ const { | |||
| 75 | 74 | getHashes, | |
| 76 | 75 | setDefaultEncoding, | |
| 77 | 76 | setEngine, | |
| 77 | + timingSafeEqual, | ||
| 78 | 78 | toBuf | |
| 79 | 79 | } = require('internal/crypto/util'); | |
| 80 | 80 | const Certificate = require('internal/crypto/certificate'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,8 @@ const { | |||
| 4 | 4 | getCiphers: _getCiphers, | |
| 5 | 5 | getCurves: _getCurves, | |
| 6 | 6 | getHashes: _getHashes, | |
| 7 | - setEngine: _setEngine | ||
| 7 | + setEngine: _setEngine, | ||
| 8 | + timingSafeEqual: _timingSafeEqual | ||
| 8 | 9 | } = process.binding('crypto'); | |
| 9 | 10 | ||
| 10 | 11 | const { | |
@@ -17,6 +18,9 @@ const { | |||
| 17 | 18 | cachedResult, | |
| 18 | 19 | filterDuplicateStrings | |
| 19 | 20 | } = require('internal/util'); | |
| 21 | + const { | ||
| 22 | + isArrayBufferView | ||
| 23 | + } = require('internal/util/types'); | ||
| 20 | 24 | ||
| 21 | 25 | var defaultEncoding = 'buffer'; | |
| 22 | 26 | ||
@@ -60,12 +64,28 @@ function setEngine(id, flags) { | |||
| 60 | 64 | throw new errors.Error('ERR_CRYPTO_ENGINE_UNKNOWN', id); | |
| 61 | 65 | } | |
| 62 | 66 | ||
| 67 | + function timingSafeEqual(a, b) { | ||
| 68 | + if (!isArrayBufferView(a)) { | ||
| 69 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'a', | ||
| 70 | + ['Buffer', 'TypedArray', 'DataView']); | ||
| 71 | + } | ||
| 72 | + if (!isArrayBufferView(b)) { | ||
| 73 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'b', | ||
| 74 | + ['Buffer', 'TypedArray', 'DataView']); | ||
| 75 | + } | ||
| 76 | + if (a.length !== b.length) { | ||
| 77 | + throw new errors.RangeError('ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH'); | ||
| 78 | + } | ||
| 79 | + return _timingSafeEqual(a, b); | ||
| 80 | + } | ||
| 81 | + | ||
| 63 | 82 | module.exports = { | |
| 64 | 83 | getCiphers, | |
| 65 | 84 | getCurves, | |
| 66 | 85 | getDefaultEncoding, | |
| 67 | 86 | getHashes, | |
| 68 | 87 | setDefaultEncoding, | |
| 69 | 88 | setEngine, | |
| 89 | + timingSafeEqual, | ||
| 70 | 90 | toBuf | |
| 71 | 91 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -160,6 +160,8 @@ E('ERR_CRYPTO_HASH_FINALIZED', 'Digest already called'); | |||
| 160 | 160 | E('ERR_CRYPTO_HASH_UPDATE_FAILED', 'Hash update failed'); | |
| 161 | 161 | E('ERR_CRYPTO_INVALID_DIGEST', 'Invalid digest: %s'); | |
| 162 | 162 | E('ERR_CRYPTO_SIGN_KEY_REQUIRED', 'No key provided to sign'); | |
| 163 | + E('ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH', | ||
| 164 | + 'Input buffers must have the same length'); | ||
| 163 | 165 | E('ERR_DNS_SET_SERVERS_FAILED', (err, servers) => | |
| 164 | 166 | `c-ares failed to set servers: "${err}" [${servers}]`); | |
| 165 | 167 | E('ERR_ENCODING_INVALID_ENCODED_DATA', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5848,15 +5848,11 @@ void ExportChallenge(const FunctionCallbackInfo<Value>& args) { | |||
| 5848 | 5848 | } | |
| 5849 | 5849 | ||
| 5850 | 5850 | void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) { | |
| 5851 | - Environment* env = Environment::GetCurrent(args); | ||
| 5852 | - | ||
| 5853 | - THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "First argument"); | ||
| 5854 | - THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Second argument"); | ||
| 5851 | + CHECK(Buffer::HasInstance(args[0])); | ||
| 5852 | + CHECK(Buffer::HasInstance(args[1])); | ||
| 5855 | 5853 | ||
| 5856 | 5854 | size_t buf_length = Buffer::Length(args[0]); | |
| 5857 | - if (buf_length != Buffer::Length(args[1])) { | ||
| 5858 | - return env->ThrowTypeError("Input buffers must have the same length"); | ||
| 5859 | - } | ||
| 5855 | + CHECK_EQ(buf_length, Buffer::Length(args[1])); | ||
| 5860 | 5856 | ||
| 5861 | 5857 | const char* buf1 = Buffer::Data(args[0]); | |
| 5862 | 5858 | const char* buf2 = Buffer::Data(args[1]); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,17 +18,31 @@ assert.strictEqual( | |||
| 18 | 18 | 'should consider unequal strings to be unequal' | |
| 19 | 19 | ); | |
| 20 | 20 | ||
| 21 | - assert.throws(function() { | ||
| 22 | - crypto.timingSafeEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 2])); | ||
| 23 | - }, /^TypeError: Input buffers must have the same length$/, | ||
| 24 | - 'should throw when given buffers with different lengths'); | ||
| 21 | + common.expectsError( | ||
| 22 | + () => crypto.timingSafeEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 2])), | ||
| 23 | + { | ||
| 24 | + code: 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH', | ||
| 25 | + type: RangeError, | ||
| 26 | + message: 'Input buffers must have the same length' | ||
| 27 | + } | ||
| 28 | + ); | ||
| 25 | 29 | ||
| 26 | - assert.throws(function() { | ||
| 27 | - crypto.timingSafeEqual('not a buffer', Buffer.from([1, 2])); | ||
| 28 | - }, /^TypeError: First argument must be a buffer$/, | ||
| 29 | - 'should throw if the first argument is not a buffer'); | ||
| 30 | + common.expectsError( | ||
| 31 | + () => crypto.timingSafeEqual('not a buffer', Buffer.from([1, 2])), | ||
| 32 | + { | ||
| 33 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 34 | + type: TypeError, | ||
| 35 | + message: | ||
| 36 | + 'The "a" argument must be one of type Buffer, TypedArray, or DataView' | ||
| 37 | + } | ||
| 38 | + ); | ||
| 30 | 39 | ||
| 31 | - assert.throws(function() { | ||
| 32 | - crypto.timingSafeEqual(Buffer.from([1, 2]), 'not a buffer'); | ||
| 33 | - }, /^TypeError: Second argument must be a buffer$/, | ||
| 34 | - 'should throw if the second argument is not a buffer'); | ||
| 40 | + common.expectsError( | ||
| 41 | + () => crypto.timingSafeEqual(Buffer.from([1, 2]), 'not a buffer'), | ||
| 42 | + { | ||
| 43 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 44 | + type: TypeError, | ||
| 45 | + message: | ||
| 46 | + 'The "b" argument must be one of type Buffer, TypedArray, or DataView' | ||
| 47 | + } | ||
| 48 | + ); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments