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 |
|
|
namespace node { |
11 |
|
|
|
12 |
|
|
using v8::FunctionCallbackInfo; |
13 |
|
|
using v8::Just; |
14 |
|
|
using v8::Local; |
15 |
|
|
using v8::Maybe; |
16 |
|
|
using v8::Nothing; |
17 |
|
|
using v8::Object; |
18 |
|
|
using v8::Uint32; |
19 |
|
|
using v8::Value; |
20 |
|
|
|
21 |
|
|
namespace crypto { |
22 |
|
518 |
Maybe<bool> RandomBytesTraits::EncodeOutput( |
23 |
|
|
Environment* env, |
24 |
|
|
const RandomBytesConfig& params, |
25 |
|
|
ByteSource* unused, |
26 |
|
|
v8::Local<v8::Value>* result) { |
27 |
|
1036 |
*result = v8::Undefined(env->isolate()); |
28 |
|
518 |
return Just(!result->IsEmpty()); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
518 |
Maybe<bool> RandomBytesTraits::AdditionalConfig( |
32 |
|
|
CryptoJobMode mode, |
33 |
|
|
const FunctionCallbackInfo<Value>& args, |
34 |
|
|
unsigned int offset, |
35 |
|
|
RandomBytesConfig* params) { |
36 |
|
518 |
Environment* env = Environment::GetCurrent(args); |
37 |
✗✓ |
1036 |
CHECK(IsAnyByteSource(args[offset])); // Buffer to fill |
38 |
✗✓ |
1554 |
CHECK(args[offset + 1]->IsUint32()); // Offset |
39 |
✗✓ |
1554 |
CHECK(args[offset + 2]->IsUint32()); // Size |
40 |
|
|
|
41 |
|
1554 |
ArrayBufferOrViewContents<unsigned char> in(args[offset]); |
42 |
|
|
|
43 |
|
2072 |
const uint32_t byte_offset = args[offset + 1].As<Uint32>()->Value(); |
44 |
|
2072 |
const uint32_t size = args[offset + 2].As<Uint32>()->Value(); |
45 |
✗✓ |
518 |
CHECK_GE(byte_offset + size, byte_offset); // Overflow check. |
46 |
✗✓ |
518 |
CHECK_LE(byte_offset + size, in.size()); // Bounds check. |
47 |
|
|
|
48 |
✗✓ |
518 |
if (UNLIKELY(size > INT_MAX)) { |
49 |
|
|
THROW_ERR_OUT_OF_RANGE(env, "buffer is too large"); |
50 |
|
|
return Nothing<bool>(); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
518 |
params->buffer = in.data() + byte_offset; |
54 |
|
518 |
params->size = size; |
55 |
|
|
|
56 |
|
518 |
return Just(true); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
518 |
bool RandomBytesTraits::DeriveBits( |
60 |
|
|
Environment* env, |
61 |
|
|
const RandomBytesConfig& params, |
62 |
|
|
ByteSource* unused) { |
63 |
|
518 |
CheckEntropy(); // Ensure that OpenSSL's PRNG is properly seeded. |
64 |
|
518 |
return RAND_bytes(params.buffer, params.size) != 0; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
namespace Random { |
68 |
|
654 |
void Initialize(Environment* env, Local<Object> target) { |
69 |
|
654 |
RandomBytesJob::Initialize(env, target); |
70 |
|
654 |
} |
71 |
|
|
} // namespace Random |
72 |
|
|
} // namespace crypto |
73 |
✓✗✓✗
|
14034 |
} // namespace node |