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