|
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 |
|
|
0 commit comments