GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/crypto/crypto_scrypt.cc Lines: 59 70 84.3 %
Date: 2021-04-27 04:12:29 Branches: 18 34 52.9 %

Line Branch Exec Source
1
#include "crypto/crypto_scrypt.h"
2
#include "crypto/crypto_util.h"
3
#include "allocated_buffer-inl.h"
4
#include "async_wrap-inl.h"
5
#include "env-inl.h"
6
#include "memory_tracker-inl.h"
7
#include "node_buffer.h"
8
#include "threadpoolwork-inl.h"
9
#include "v8.h"
10
11
namespace node {
12
13
using v8::FunctionCallbackInfo;
14
using v8::Int32;
15
using v8::Just;
16
using v8::Maybe;
17
using v8::Nothing;
18
using v8::Uint32;
19
using v8::Value;
20
21
namespace crypto {
22
#ifndef OPENSSL_NO_SCRYPT
23
24
31
ScryptConfig::ScryptConfig(ScryptConfig&& other) noexcept
25
31
  : mode(other.mode),
26
31
    pass(std::move(other.pass)),
27
31
    salt(std::move(other.salt)),
28
31
    N(other.N),
29
31
    r(other.r),
30
31
    p(other.p),
31
31
    maxmem(other.maxmem),
32
248
    length(other.length) {}
33
34
ScryptConfig& ScryptConfig::operator=(ScryptConfig&& other) noexcept {
35
  if (&other == this) return *this;
36
  this->~ScryptConfig();
37
  return *new (this) ScryptConfig(std::move(other));
38
}
39
40
1
void ScryptConfig::MemoryInfo(MemoryTracker* tracker) const {
41
1
  if (mode == kCryptoJobAsync) {
42
1
    tracker->TrackFieldWithSize("pass", pass.size());
43
1
    tracker->TrackFieldWithSize("salt", salt.size());
44
  }
45
1
}
46
47
31
Maybe<bool> ScryptTraits::EncodeOutput(
48
    Environment* env,
49
    const ScryptConfig& params,
50
    ByteSource* out,
51
    v8::Local<v8::Value>* result) {
52
62
  *result = out->ToArrayBuffer(env);
53
31
  return Just(!result->IsEmpty());
54
}
55
56
43
Maybe<bool> ScryptTraits::AdditionalConfig(
57
    CryptoJobMode mode,
58
    const FunctionCallbackInfo<Value>& args,
59
    unsigned int offset,
60
    ScryptConfig* params) {
61
43
  Environment* env = Environment::GetCurrent(args);
62
63
43
  params->mode = mode;
64
65
129
  ArrayBufferOrViewContents<char> pass(args[offset]);
66
129
  ArrayBufferOrViewContents<char> salt(args[offset + 1]);
67
68
43
  if (UNLIKELY(!pass.CheckSizeInt32())) {
69
    THROW_ERR_OUT_OF_RANGE(env, "pass is too large");
70
    return Nothing<bool>();
71
  }
72
73
43
  if (UNLIKELY(!salt.CheckSizeInt32())) {
74
    THROW_ERR_OUT_OF_RANGE(env, "salt is too large");
75
    return Nothing<bool>();
76
  }
77
78
  params->pass = mode == kCryptoJobAsync
79
86
      ? pass.ToCopy()
80
43
      : pass.ToByteSource();
81
82
  params->salt = mode == kCryptoJobAsync
83
86
      ? salt.ToCopy()
84
43
      : salt.ToByteSource();
85
86
129
  CHECK(args[offset + 2]->IsUint32());  // N
87
129
  CHECK(args[offset + 3]->IsUint32());  // r
88
129
  CHECK(args[offset + 4]->IsUint32());  // p
89
129
  CHECK(args[offset + 5]->IsNumber());  // maxmem
90
129
  CHECK(args[offset + 6]->IsInt32());  // length
91
92
172
  params->N = args[offset + 2].As<Uint32>()->Value();
93
172
  params->r = args[offset + 3].As<Uint32>()->Value();
94
172
  params->p = args[offset + 4].As<Uint32>()->Value();
95
172
  params->maxmem = args[offset + 5]->IntegerValue(env->context()).ToChecked();
96
97
172
  if (EVP_PBE_scrypt(
98
          nullptr,
99
          0,
100
          nullptr,
101
          0,
102
43
          params->N,
103
43
          params->r,
104
43
          params->p,
105
          params->maxmem,
106
          nullptr,
107
          0) != 1) {
108
12
    THROW_ERR_CRYPTO_INVALID_SCRYPT_PARAMS(env);
109
12
    return Nothing<bool>();
110
  }
111
112
124
  params->length = args[offset + 6].As<Int32>()->Value();
113
31
  if (params->length < 0) {
114
    THROW_ERR_OUT_OF_RANGE(env, "length must be <= %d", INT_MAX);
115
    return Nothing<bool>();
116
  }
117
118
31
  return Just(true);
119
}
120
121
31
bool ScryptTraits::DeriveBits(
122
    Environment* env,
123
    const ScryptConfig& params,
124
    ByteSource* out) {
125
31
  char* data = MallocOpenSSL<char>(params.length);
126
62
  ByteSource buf = ByteSource::Allocated(data, params.length);
127
31
  unsigned char* ptr = reinterpret_cast<unsigned char*>(data);
128
129
  // Both the pass and salt may be zero-length at this point
130
131
124
  if (!EVP_PBE_scrypt(
132
          params.pass.get(),
133
          params.pass.size(),
134
          params.salt.data<unsigned char>(),
135
          params.salt.size(),
136
31
          params.N,
137
31
          params.r,
138
31
          params.p,
139
31
          params.maxmem,
140
          ptr,
141
31
          params.length)) {
142
    return false;
143
  }
144
31
  *out = std::move(buf);
145
31
  return true;
146
}
147
148
#endif  // !OPENSSL_NO_SCRYPT
149
150
}  // namespace crypto
151

14487
}  // namespace node