forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_random.cc
73 lines (62 loc) · 1.98 KB
/
crypto_random.cc
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
#include "crypto/crypto_random.h"
#include "crypto/crypto_util.h"
#include "allocated_buffer-inl.h"
#include "async_wrap-inl.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "threadpoolwork-inl.h"
#include "v8.h"
namespace node {
using v8::FunctionCallbackInfo;
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) {
Environment* env = Environment::GetCurrent(args);
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.
if (UNLIKELY(size > INT_MAX)) {
THROW_ERR_OUT_OF_RANGE(env, "buffer is too large");
return Nothing<bool>();
}
params->buffer = in.data() + byte_offset;
params->size = size;
return Just(true);
}
bool RandomBytesTraits::DeriveBits(
Environment* env,
const RandomBytesConfig& params,
ByteSource* unused) {
CheckEntropy(); // Ensure that OpenSSL's PRNG is properly seeded.
return RAND_bytes(params.buffer, params.size) != 0;
}
namespace Random {
void Initialize(Environment* env, Local<Object> target) {
RandomBytesJob::Initialize(env, target);
}
} // namespace Random
} // namespace crypto
} // namespace node