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

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

14532
}  // namespace node