1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
#include "crypto/crypto_random.h"
#include "async_wrap-inl.h"
#include "crypto/crypto_util.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "threadpoolwork-inl.h"
#include "v8.h"
#include <openssl/bn.h>
#include <openssl/rand.h>
namespace node {
using v8::ArrayBuffer;
using v8::BackingStore;
using v8::Boolean;
using v8::FunctionCallbackInfo;
using v8::Int32;
using v8::Just;
using v8::Local;
using v8::Maybe;
using v8::Nothing;
using v8::Object;
using v8::Uint32;
using v8::Value;
namespace crypto {
Maybe<bool> RandomBytesTraits::EncodeOutput(
Environment* env,
const RandomBytesConfig& params,
ByteSource* unused,
v8::Local<v8::Value>* result) {
*result = v8::Undefined(env->isolate());
return Just(!result->IsEmpty());
}
Maybe<bool> RandomBytesTraits::AdditionalConfig(
CryptoJobMode mode,
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
RandomBytesConfig* params) {
CHECK(IsAnyByteSource(args[offset])); // Buffer to fill
CHECK(args[offset + 1]->IsUint32()); // Offset
CHECK(args[offset + 2]->IsUint32()); // Size
ArrayBufferOrViewContents<unsigned char> in(args[offset]);
const uint32_t byte_offset = args[offset + 1].As<Uint32>()->Value();
const uint32_t size = args[offset + 2].As<Uint32>()->Value();
CHECK_GE(byte_offset + size, byte_offset); // Overflow check.
CHECK_LE(byte_offset + size, in.size()); // Bounds check.
params->buffer = in.data() + byte_offset;
params->size = size;
return Just(true);
}
bool RandomBytesTraits::DeriveBits(
Environment* env,
const RandomBytesConfig& params,
ByteSource* unused) {
return CSPRNG(params.buffer, params.size).is_ok();
}
void RandomPrimeConfig::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackFieldWithSize("prime", prime ? bits * 8 : 0);
}
Maybe<bool> RandomPrimeTraits::EncodeOutput(
Environment* env,
const RandomPrimeConfig& params,
ByteSource* unused,
v8::Local<v8::Value>* result) {
size_t size = BN_num_bytes(params.prime.get());
std::shared_ptr<BackingStore> store =
ArrayBuffer::NewBackingStore(env->isolate(), size);
BN_bn2binpad(
params.prime.get(),
reinterpret_cast<unsigned char*>(store->Data()),
size);
*result = ArrayBuffer::New(env->isolate(), store);
return Just(true);
}
Maybe<bool> RandomPrimeTraits::AdditionalConfig(
CryptoJobMode mode,
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
RandomPrimeConfig* params) {
ClearErrorOnReturn clear_error;
Environment* env = Environment::GetCurrent(args);
CHECK(args[offset]->IsUint32()); // Size
CHECK(args[offset + 1]->IsBoolean()); // Safe
const uint32_t size = args[offset].As<Uint32>()->Value();
bool safe = args[offset + 1]->IsTrue();
if (!args[offset + 2]->IsUndefined()) {
ArrayBufferOrViewContents<unsigned char> add(args[offset + 2]);
params->add.reset(BN_bin2bn(add.data(), add.size(), nullptr));
if (!params->add) {
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "could not generate prime");
return Nothing<bool>();
}
}
if (!args[offset + 3]->IsUndefined()) {
ArrayBufferOrViewContents<unsigned char> rem(args[offset + 3]);
params->rem.reset(BN_bin2bn(rem.data(), rem.size(), nullptr));
if (!params->rem) {
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "could not generate prime");
return Nothing<bool>();
}
}
// The JS interface already ensures that the (positive) size fits into an int.
int bits = static_cast<int>(size);
CHECK_GT(bits, 0);
if (params->add) {
if (BN_num_bits(params->add.get()) > bits) {
// If we allowed this, the best case would be returning a static prime
// that wasn't generated randomly. The worst case would be an infinite
// loop within OpenSSL, blocking the main thread or one of the threads
// in the thread pool.
THROW_ERR_OUT_OF_RANGE(env, "invalid options.add");
return Nothing<bool>();
}
if (params->rem) {
if (BN_cmp(params->add.get(), params->rem.get()) != 1) {
// This would definitely lead to an infinite loop if allowed since
// OpenSSL does not check this condition.
THROW_ERR_OUT_OF_RANGE(env, "invalid options.rem");
return Nothing<bool>();
}
}
}
params->bits = bits;
params->safe = safe;
params->prime.reset(BN_secure_new());
if (!params->prime) {
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "could not generate prime");
return Nothing<bool>();
}
return Just(true);
}
bool RandomPrimeTraits::DeriveBits(Environment* env,
const RandomPrimeConfig& params,
ByteSource* unused) {
// BN_generate_prime_ex() calls RAND_bytes_ex() internally.
// Make sure the CSPRNG is properly seeded.
CHECK(CSPRNG(nullptr, 0).is_ok());
if (BN_generate_prime_ex(
params.prime.get(),
params.bits,
params.safe ? 1 : 0,
params.add.get(),
params.rem.get(),
nullptr) == 0) {
return false;
}
return true;
}
void CheckPrimeConfig::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackFieldWithSize(
"prime", candidate ? BN_num_bytes(candidate.get()) : 0);
}
Maybe<bool> CheckPrimeTraits::AdditionalConfig(
CryptoJobMode mode,
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
CheckPrimeConfig* params) {
ArrayBufferOrViewContents<unsigned char> candidate(args[offset]);
params->candidate =
BignumPointer(BN_bin2bn(
candidate.data(),
candidate.size(),
nullptr));
CHECK(args[offset + 1]->IsInt32()); // Checks
params->checks = args[offset + 1].As<Int32>()->Value();
CHECK_GE(params->checks, 0);
return Just(true);
}
bool CheckPrimeTraits::DeriveBits(
Environment* env,
const CheckPrimeConfig& params,
ByteSource* out) {
BignumCtxPointer ctx(BN_CTX_new());
int ret = BN_is_prime_ex(
params.candidate.get(),
params.checks,
ctx.get(),
nullptr);
if (ret < 0) return false;
ByteSource::Builder buf(1);
buf.data<char>()[0] = ret;
*out = std::move(buf).release();
return true;
}
Maybe<bool> CheckPrimeTraits::EncodeOutput(
Environment* env,
const CheckPrimeConfig& params,
ByteSource* out,
v8::Local<v8::Value>* result) {
*result = Boolean::New(env->isolate(), out->data<char>()[0] != 0);
return Just(true);
}
namespace Random {
void Initialize(Environment* env, Local<Object> target) {
RandomBytesJob::Initialize(env, target);
RandomPrimeJob::Initialize(env, target);
CheckPrimeJob::Initialize(env, target);
}
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
RandomBytesJob::RegisterExternalReferences(registry);
RandomPrimeJob::RegisterExternalReferences(registry);
CheckPrimeJob::RegisterExternalReferences(registry);
}
} // namespace Random
} // namespace crypto
} // namespace node
|