Coverage Report

Created: 2025-08-03 10:06

/src/node/src/crypto/crypto_timing.cc
Line
Count
Source (jump to first uncovered line)
1
#include "crypto/crypto_timing.h"
2
#include "crypto/crypto_util.h"
3
#include "env-inl.h"
4
#include "node_errors.h"
5
#include "v8.h"
6
#include "node.h"
7
8
#include <openssl/crypto.h>
9
10
namespace node {
11
12
using v8::FunctionCallbackInfo;
13
using v8::Local;
14
using v8::Object;
15
using v8::Value;
16
17
namespace crypto {
18
namespace Timing {
19
0
void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) {
20
  // Moving the type checking into JS leads to test failures, most likely due
21
  // to V8 inlining certain parts of the wrapper. Therefore, keep them in C++.
22
  // Refs: https://github.com/nodejs/node/issues/34073.
23
0
  Environment* env = Environment::GetCurrent(args);
24
0
  if (!IsAnyBufferSource(args[0])) {
25
0
    THROW_ERR_INVALID_ARG_TYPE(
26
0
      env, "The \"buf1\" argument must be an instance of "
27
0
      "ArrayBuffer, Buffer, TypedArray, or DataView.");
28
0
    return;
29
0
  }
30
0
  if (!IsAnyBufferSource(args[1])) {
31
0
    THROW_ERR_INVALID_ARG_TYPE(
32
0
      env, "The \"buf2\" argument must be an instance of "
33
0
      "ArrayBuffer, Buffer, TypedArray, or DataView.");
34
0
    return;
35
0
  }
36
37
0
  ArrayBufferOrViewContents<char> buf1(args[0]);
38
0
  ArrayBufferOrViewContents<char> buf2(args[1]);
39
40
0
  if (buf1.size() != buf2.size()) {
41
0
    THROW_ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH(env);
42
0
    return;
43
0
  }
44
45
0
  return args.GetReturnValue().Set(
46
0
      CRYPTO_memcmp(buf1.data(), buf2.data(), buf1.size()) == 0);
47
0
}
48
49
8.84k
void Initialize(Environment* env, Local<Object> target) {
50
8.84k
  SetMethodNoSideEffect(
51
8.84k
      env->context(), target, "timingSafeEqual", TimingSafeEqual);
52
8.84k
}
53
0
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
54
0
  registry->Register(TimingSafeEqual);
55
0
}
56
}  // namespace Timing
57
58
}  // namespace crypto
59
}  // namespace node