| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9ef724b commit 08609b5
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,22 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common.js'); | ||
| 4 | + const assert = require('node:assert'); | ||
| 5 | + const { randomBytes, timingSafeEqual } = require('node:crypto'); | ||
| 6 | + | ||
| 7 | + const bench = common.createBenchmark(main, { | ||
| 8 | + n: [1e5], | ||
| 9 | + bufferSize: [10, 100, 200, 2_100, 22_023], | ||
| 10 | + }); | ||
| 11 | + | ||
| 12 | + function main({ n, bufferSize }) { | ||
| 13 | + const bufs = [randomBytes(bufferSize), randomBytes(bufferSize)]; | ||
| 14 | + bench.start(); | ||
| 15 | + let count = 0; | ||
| 16 | + for (let i = 0; i < n; i++) { | ||
| 17 | + const ret = timingSafeEqual(bufs[i % 2], bufs[1]); | ||
| 18 | + if (ret) count++; | ||
| 19 | + } | ||
| 20 | + bench.end(n); | ||
| 21 | + assert.strictEqual(count, Math.floor(n / 2)); | ||
| 22 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,8 @@ | |||
| 9 | 9 | ||
| 10 | 10 | namespace node { | |
| 11 | 11 | ||
| 12 | + using v8::FastApiCallbackOptions; | ||
| 13 | + using v8::FastApiTypedArray; | ||
| 12 | 14 | using v8::FunctionCallbackInfo; | |
| 13 | 15 | using v8::Local; | |
| 14 | 16 | using v8::Object; | |
@@ -46,12 +48,32 @@ void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) { | |||
| 46 | 48 | CRYPTO_memcmp(buf1.data(), buf2.data(), buf1.size()) == 0); | |
| 47 | 49 | } | |
| 48 | 50 | ||
| 51 | + bool FastTimingSafeEqual(Local<Value> receiver, | ||
| 52 | + const FastApiTypedArray<uint8_t>& a, | ||
| 53 | + const FastApiTypedArray<uint8_t>& b, | ||
| 54 | + // NOLINTNEXTLINE(runtime/references) | ||
| 55 | + FastApiCallbackOptions& options) { | ||
| 56 | + uint8_t* data_a; | ||
| 57 | + uint8_t* data_b; | ||
| 58 | + if (a.length() != b.length() || !a.getStorageIfAligned(&data_a) || | ||
| 59 | + !b.getStorageIfAligned(&data_b)) { | ||
| 60 | + options.fallback = true; | ||
| 61 | + return false; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + return CRYPTO_memcmp(data_a, data_b, a.length()) == 0; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + static v8::CFunction fast_equal(v8::CFunction::Make(FastTimingSafeEqual)); | ||
| 68 | + | ||
| 49 | 69 | void Initialize(Environment* env, Local<Object> target) { | |
| 50 | - SetMethodNoSideEffect( | ||
| 51 | - env->context(), target, "timingSafeEqual", TimingSafeEqual); | ||
| 70 | + SetFastMethodNoSideEffect( | ||
| 71 | + env->context(), target, "timingSafeEqual", TimingSafeEqual, &fast_equal); | ||
| 52 | 72 | } | |
| 53 | 73 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |
| 54 | 74 | registry->Register(TimingSafeEqual); | |
| 75 | + registry->Register(FastTimingSafeEqual); | ||
| 76 | + registry->Register(fast_equal.GetTypeInfo()); | ||
| 55 | 77 | } | |
| 56 | 78 | } // namespace Timing | |
| 57 | 79 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,11 @@ using CFunctionCallbackWithStrings = | |||
| 27 | 27 | bool (*)(v8::Local<v8::Value>, | |
| 28 | 28 | const v8::FastOneByteString& input, | |
| 29 | 29 | const v8::FastOneByteString& base); | |
| 30 | + using CFunctionCallbackWithTwoUint8ArraysFallback = | ||
| 31 | + bool (*)(v8::Local<v8::Value>, | ||
| 32 | + const v8::FastApiTypedArray<uint8_t>&, | ||
| 33 | + const v8::FastApiTypedArray<uint8_t>&, | ||
| 34 | + v8::FastApiCallbackOptions&); | ||
| 30 | 35 | using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>, | |
| 31 | 36 | const uint32_t input); | |
| 32 | 37 | using CFunctionWithDoubleReturnDouble = double (*)(v8::Local<v8::Value>, | |
@@ -51,6 +56,7 @@ class ExternalReferenceRegistry { | |||
| 51 | 56 | V(CFunctionCallbackWithBool) \ | |
| 52 | 57 | V(CFunctionCallbackWithString) \ | |
| 53 | 58 | V(CFunctionCallbackWithStrings) \ | |
| 59 | + V(CFunctionCallbackWithTwoUint8ArraysFallback) \ | ||
| 54 | 60 | V(CFunctionWithUint32) \ | |
| 55 | 61 | V(CFunctionWithDoubleReturnDouble) \ | |
| 56 | 62 | V(CFunctionWithInt64Fallback) \ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments