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_random.cc Lines: 99 117 84.6 %
Date: 2021-04-25 04:11:48 Branches: 36 60 60.0 %

Line Branch Exec Source
1
#include "crypto/crypto_random.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 "threadpoolwork-inl.h"
8
#include "v8.h"
9
10
#include <openssl/bn.h>
11
12
namespace node {
13
14
using v8::ArrayBuffer;
15
using v8::BackingStore;
16
using v8::False;
17
using v8::FunctionCallbackInfo;
18
using v8::Just;
19
using v8::Local;
20
using v8::Maybe;
21
using v8::Nothing;
22
using v8::Object;
23
using v8::True;
24
using v8::Uint32;
25
using v8::Value;
26
27
namespace crypto {
28
117
Maybe<bool> RandomBytesTraits::EncodeOutput(
29
    Environment* env,
30
    const RandomBytesConfig& params,
31
    ByteSource* unused,
32
    v8::Local<v8::Value>* result) {
33
234
  *result = v8::Undefined(env->isolate());
34
117
  return Just(!result->IsEmpty());
35
}
36
37
117
Maybe<bool> RandomBytesTraits::AdditionalConfig(
38
    CryptoJobMode mode,
39
    const FunctionCallbackInfo<Value>& args,
40
    unsigned int offset,
41
    RandomBytesConfig* params) {
42
117
  Environment* env = Environment::GetCurrent(args);
43
234
  CHECK(IsAnyByteSource(args[offset]));  // Buffer to fill
44
351
  CHECK(args[offset + 1]->IsUint32());  // Offset
45
351
  CHECK(args[offset + 2]->IsUint32());  // Size
46
47
351
  ArrayBufferOrViewContents<unsigned char> in(args[offset]);
48
49
468
  const uint32_t byte_offset = args[offset + 1].As<Uint32>()->Value();
50
468
  const uint32_t size = args[offset + 2].As<Uint32>()->Value();
51
117
  CHECK_GE(byte_offset + size, byte_offset);  // Overflow check.
52
117
  CHECK_LE(byte_offset + size, in.size());  // Bounds check.
53
54
117
  if (UNLIKELY(size > INT_MAX)) {
55
    THROW_ERR_OUT_OF_RANGE(env, "buffer is too large");
56
    return Nothing<bool>();
57
  }
58
59
117
  params->buffer = in.data() + byte_offset;
60
117
  params->size = size;
61
62
117
  return Just(true);
63
}
64
65
117
bool RandomBytesTraits::DeriveBits(
66
    Environment* env,
67
    const RandomBytesConfig& params,
68
    ByteSource* unused) {
69
117
  CheckEntropy();  // Ensure that OpenSSL's PRNG is properly seeded.
70
117
  return RAND_bytes(params.buffer, params.size) != 0;
71
}
72
73
void RandomPrimeConfig::MemoryInfo(MemoryTracker* tracker) const {
74
  tracker->TrackFieldWithSize("prime", prime ? bits * 8 : 0);
75
}
76
77
15
Maybe<bool> RandomPrimeTraits::EncodeOutput(
78
    Environment* env,
79
    const RandomPrimeConfig& params,
80
    ByteSource* unused,
81
    v8::Local<v8::Value>* result) {
82
15
  size_t size = BN_num_bytes(params.prime.get());
83
  std::shared_ptr<BackingStore> store =
84
30
      ArrayBuffer::NewBackingStore(env->isolate(), size);
85
15
  BN_bn2binpad(
86
15
      params.prime.get(),
87
15
      reinterpret_cast<unsigned char*>(store->Data()),
88
15
      size);
89
30
  *result = ArrayBuffer::New(env->isolate(), store);
90
30
  return Just(true);
91
}
92
93
21
Maybe<bool> RandomPrimeTraits::AdditionalConfig(
94
    CryptoJobMode mode,
95
    const FunctionCallbackInfo<Value>& args,
96
    unsigned int offset,
97
    RandomPrimeConfig* params) {
98
21
  ClearErrorOnReturn clear_error;
99
21
  Environment* env = Environment::GetCurrent(args);
100
63
  CHECK(args[offset]->IsUint32());  // Size
101
63
  CHECK(args[offset + 1]->IsBoolean());  // Safe
102
103
84
  const uint32_t size = args[offset].As<Uint32>()->Value();
104
63
  bool safe = args[offset + 1]->IsTrue();
105
106
84
  if (!args[offset + 2]->IsUndefined()) {
107
36
    ArrayBufferOrViewContents<unsigned char> add(args[offset + 2]);
108
12
    params->add.reset(BN_bin2bn(add.data(), add.size(), nullptr));
109
12
    if (!params->add) {
110
      THROW_ERR_CRYPTO_OPERATION_FAILED(env, "could not generate prime");
111
      return Nothing<bool>();
112
    }
113
  }
114
115
84
  if (!args[offset + 3]->IsUndefined()) {
116
24
    ArrayBufferOrViewContents<unsigned char> rem(args[offset + 3]);
117
8
    params->rem.reset(BN_bin2bn(rem.data(), rem.size(), nullptr));
118
8
    if (!params->rem) {
119
      THROW_ERR_CRYPTO_OPERATION_FAILED(env, "could not generate prime");
120
      return Nothing<bool>();
121
    }
122
  }
123
124
21
  int bits = static_cast<int>(size);
125
21
  if (bits < 0) {
126
    THROW_ERR_OUT_OF_RANGE(env, "invalid size");
127
    return Nothing<bool>();
128
  }
129
130
21
  if (params->add) {
131
12
    if (BN_num_bits(params->add.get()) > bits) {
132
      // If we allowed this, the best case would be returning a static prime
133
      // that wasn't generated randomly. The worst case would be an infinite
134
      // loop within OpenSSL, blocking the main thread or one of the threads
135
      // in the thread pool.
136
3
      THROW_ERR_OUT_OF_RANGE(env, "invalid options.add");
137
3
      return Nothing<bool>();
138
    }
139
140
9
    if (params->rem) {
141
7
      if (BN_cmp(params->add.get(), params->rem.get()) != 1) {
142
        // This would definitely lead to an infinite loop if allowed since
143
        // OpenSSL does not check this condition.
144
3
        THROW_ERR_OUT_OF_RANGE(env, "invalid options.rem");
145
3
        return Nothing<bool>();
146
      }
147
    }
148
  }
149
150
15
  params->bits = bits;
151
15
  params->safe = safe;
152
15
  params->prime.reset(BN_secure_new());
153
15
  if (!params->prime) {
154
    THROW_ERR_CRYPTO_OPERATION_FAILED(env, "could not generate prime");
155
    return Nothing<bool>();
156
  }
157
158
15
  return Just(true);
159
}
160
161
15
bool RandomPrimeTraits::DeriveBits(
162
    Environment* env,
163
    const RandomPrimeConfig& params,
164
    ByteSource* unused) {
165
166
15
  CheckEntropy();
167
168

30
  if (BN_generate_prime_ex(
169
          params.prime.get(),
170
15
          params.bits,
171
15
          params.safe ? 1 : 0,
172
15
          params.add.get(),
173
15
          params.rem.get(),
174
          nullptr) == 0) {
175
    return false;
176
  }
177
178
15
  return true;
179
}
180
181
void CheckPrimeConfig::MemoryInfo(MemoryTracker* tracker) const {
182
  tracker->TrackFieldWithSize(
183
      "prime", candidate ? BN_num_bytes(candidate.get()) : 0);
184
}
185
186
24
Maybe<bool> CheckPrimeTraits::AdditionalConfig(
187
    CryptoJobMode mode,
188
    const FunctionCallbackInfo<Value>& args,
189
    unsigned int offset,
190
    CheckPrimeConfig* params) {
191
24
  Environment* env = Environment::GetCurrent(args);
192
193
72
  ArrayBufferOrViewContents<unsigned char> candidate(args[offset]);
194
195
  params->candidate =
196
72
      BignumPointer(BN_bin2bn(
197
24
          candidate.data(),
198
24
          candidate.size(),
199
24
          nullptr));
200
201
72
  CHECK(args[offset + 1]->IsUint32());  // Checks
202
203
96
  const int checks = static_cast<int>(args[offset + 1].As<Uint32>()->Value());
204
24
  if (checks < 0) {
205
    THROW_ERR_OUT_OF_RANGE(env, "invalid options.checks");
206
    return Nothing<bool>();
207
  }
208
209
24
  params->checks = checks;
210
211
24
  return Just(true);
212
}
213
214
24
bool CheckPrimeTraits::DeriveBits(
215
    Environment* env,
216
    const CheckPrimeConfig& params,
217
    ByteSource* out) {
218
219
48
  BignumCtxPointer ctx(BN_CTX_new());
220
221
24
  int ret = BN_is_prime_ex(
222
24
            params.candidate.get(),
223
24
            params.checks,
224
            ctx.get(),
225
24
            nullptr);
226
24
  if (ret < 0) return false;
227
24
  char* data = MallocOpenSSL<char>(1);
228
24
  data[0] = ret;
229
24
  *out = ByteSource::Allocated(data, 1);
230
24
  return true;
231
}
232
233
24
Maybe<bool> CheckPrimeTraits::EncodeOutput(
234
    Environment* env,
235
    const CheckPrimeConfig& params,
236
    ByteSource* out,
237
    v8::Local<v8::Value>* result) {
238
51
  *result = out->get()[0] ? True(env->isolate()) : False(env->isolate());
239
24
  return Just(true);
240
}
241
242
namespace Random {
243
4100
void Initialize(Environment* env, Local<Object> target) {
244
4100
  RandomBytesJob::Initialize(env, target);
245
4100
  RandomPrimeJob::Initialize(env, target);
246
4100
  CheckPrimeJob::Initialize(env, target);
247
4100
}
248
}  // namespace Random
249
}  // namespace crypto
250

14316
}  // namespace node