GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_timing.cc Lines: 18 18 100.0 %
Date: 2021-08-24 04:12:49 Branches: 8 8 100.0 %

Line Branch Exec Source
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
39
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
39
  Environment* env = Environment::GetCurrent(args);
24
39
  if (!IsAnyByteSource(args[0])) {
25
1
    THROW_ERR_INVALID_ARG_TYPE(
26
      env, "The \"buf1\" argument must be an instance of "
27
      "ArrayBuffer, Buffer, TypedArray, or DataView.");
28
1
    return;
29
  }
30
38
  if (!IsAnyByteSource(args[1])) {
31
1
    THROW_ERR_INVALID_ARG_TYPE(
32
      env, "The \"buf2\" argument must be an instance of "
33
      "ArrayBuffer, Buffer, TypedArray, or DataView.");
34
1
    return;
35
  }
36
37
74
  ArrayBufferOrViewContents<char> buf1(args[0]);
38
74
  ArrayBufferOrViewContents<char> buf2(args[1]);
39
40
37
  if (buf1.size() != buf2.size()) {
41
1
    THROW_ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH(env);
42
1
    return;
43
  }
44
45
36
  return args.GetReturnValue().Set(
46
72
      CRYPTO_memcmp(buf1.data(), buf2.data(), buf1.size()) == 0);
47
}
48
49
4277
void Initialize(Environment* env, Local<Object> target) {
50
4277
  env->SetMethodNoSideEffect(target, "timingSafeEqual", TimingSafeEqual);
51
4277
}
52
}  // namespace Timing
53
54
}  // namespace crypto
55
}  // namespace node