1 |
|
|
#ifndef SRC_CRYPTO_CRYPTO_RANDOM_H_ |
2 |
|
|
#define SRC_CRYPTO_CRYPTO_RANDOM_H_ |
3 |
|
|
|
4 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
5 |
|
|
|
6 |
|
|
#include "crypto/crypto_util.h" |
7 |
|
|
#include "base_object.h" |
8 |
|
|
#include "allocated_buffer.h" |
9 |
|
|
#include "env.h" |
10 |
|
|
#include "memory_tracker.h" |
11 |
|
|
#include "node_internals.h" |
12 |
|
|
#include "v8.h" |
13 |
|
|
|
14 |
|
|
namespace node { |
15 |
|
|
namespace crypto { |
16 |
|
|
struct RandomBytesConfig final : public MemoryRetainer { |
17 |
|
|
unsigned char* buffer; |
18 |
|
|
size_t size; |
19 |
|
1 |
SET_NO_MEMORY_INFO() |
20 |
|
1 |
SET_MEMORY_INFO_NAME(RandomBytesConfig) |
21 |
|
1 |
SET_SELF_SIZE(RandomBytesConfig) |
22 |
|
|
}; |
23 |
|
|
|
24 |
|
|
struct RandomBytesTraits final { |
25 |
|
|
using AdditionalParameters = RandomBytesConfig; |
26 |
|
|
static constexpr const char* JobName = "RandomBytesJob"; |
27 |
|
|
static constexpr AsyncWrap::ProviderType Provider = |
28 |
|
|
AsyncWrap::PROVIDER_RANDOMBYTESREQUEST; |
29 |
|
|
|
30 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
31 |
|
|
CryptoJobMode mode, |
32 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
33 |
|
|
unsigned int offset, |
34 |
|
|
RandomBytesConfig* params); |
35 |
|
|
|
36 |
|
|
static bool DeriveBits( |
37 |
|
|
Environment* env, |
38 |
|
|
const RandomBytesConfig& params, |
39 |
|
|
ByteSource* out_); |
40 |
|
|
|
41 |
|
|
static v8::Maybe<bool> EncodeOutput( |
42 |
|
|
Environment* env, |
43 |
|
|
const RandomBytesConfig& params, |
44 |
|
|
ByteSource* unused, |
45 |
|
|
v8::Local<v8::Value>* result); |
46 |
|
|
}; |
47 |
|
|
|
48 |
|
|
using RandomBytesJob = DeriveBitsJob<RandomBytesTraits>; |
49 |
|
|
|
50 |
|
|
struct RandomPrimeConfig final : public MemoryRetainer { |
51 |
|
|
BignumPointer prime; |
52 |
|
|
BignumPointer rem; |
53 |
|
|
BignumPointer add; |
54 |
|
|
int bits; |
55 |
|
|
bool safe; |
56 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
57 |
|
|
SET_MEMORY_INFO_NAME(RandomPrimeConfig) |
58 |
|
|
SET_SELF_SIZE(RandomPrimeConfig) |
59 |
|
|
}; |
60 |
|
|
|
61 |
|
|
struct RandomPrimeTraits final { |
62 |
|
|
using AdditionalParameters = RandomPrimeConfig; |
63 |
|
|
static constexpr const char* JobName = "RandomPrimeJob"; |
64 |
|
|
static constexpr AsyncWrap::ProviderType Provider = |
65 |
|
|
AsyncWrap::PROVIDER_RANDOMPRIMEREQUEST; |
66 |
|
|
|
67 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
68 |
|
|
CryptoJobMode mode, |
69 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
70 |
|
|
unsigned int offset, |
71 |
|
|
RandomPrimeConfig* params); |
72 |
|
|
|
73 |
|
|
static bool DeriveBits( |
74 |
|
|
Environment* env, |
75 |
|
|
const RandomPrimeConfig& params, |
76 |
|
|
ByteSource* out_); |
77 |
|
|
|
78 |
|
|
static v8::Maybe<bool> EncodeOutput( |
79 |
|
|
Environment* env, |
80 |
|
|
const RandomPrimeConfig& params, |
81 |
|
|
ByteSource* unused, |
82 |
|
|
v8::Local<v8::Value>* result); |
83 |
|
|
}; |
84 |
|
|
|
85 |
|
|
using RandomPrimeJob = DeriveBitsJob<RandomPrimeTraits>; |
86 |
|
|
|
87 |
|
|
struct CheckPrimeConfig final : public MemoryRetainer { |
88 |
|
|
BignumPointer candidate; |
89 |
|
|
int checks = 1; |
90 |
|
|
|
91 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
92 |
|
|
SET_MEMORY_INFO_NAME(CheckPrimeConfig) |
93 |
|
|
SET_SELF_SIZE(CheckPrimeConfig) |
94 |
|
|
}; |
95 |
|
|
|
96 |
|
|
struct CheckPrimeTraits final { |
97 |
|
|
using AdditionalParameters = CheckPrimeConfig; |
98 |
|
|
static constexpr const char* JobName = "CheckPrimeJob"; |
99 |
|
|
|
100 |
|
|
static constexpr AsyncWrap::ProviderType Provider = |
101 |
|
|
AsyncWrap::PROVIDER_CHECKPRIMEREQUEST; |
102 |
|
|
|
103 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
104 |
|
|
CryptoJobMode mode, |
105 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
106 |
|
|
unsigned int offset, |
107 |
|
|
CheckPrimeConfig* params); |
108 |
|
|
|
109 |
|
|
static bool DeriveBits( |
110 |
|
|
Environment* env, |
111 |
|
|
const CheckPrimeConfig& params, |
112 |
|
|
ByteSource* out); |
113 |
|
|
|
114 |
|
|
static v8::Maybe<bool> EncodeOutput( |
115 |
|
|
Environment* env, |
116 |
|
|
const CheckPrimeConfig& params, |
117 |
|
|
ByteSource* out, |
118 |
|
|
v8::Local<v8::Value>* result); |
119 |
|
|
}; |
120 |
|
|
|
121 |
|
|
using CheckPrimeJob = DeriveBitsJob<CheckPrimeTraits>; |
122 |
|
|
|
123 |
|
|
namespace Random { |
124 |
|
|
void Initialize(Environment* env, v8::Local<v8::Object> target); |
125 |
|
|
void RegisterExternalReferences(ExternalReferenceRegistry* registry); |
126 |
|
|
} // namespace Random |
127 |
|
|
} // namespace crypto |
128 |
|
|
} // namespace node |
129 |
|
|
|
130 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
131 |
|
|
#endif // SRC_CRYPTO_CRYPTO_RANDOM_H_ |