1 |
|
|
#ifndef SRC_CRYPTO_CRYPTO_DH_H_ |
2 |
|
|
#define SRC_CRYPTO_CRYPTO_DH_H_ |
3 |
|
|
|
4 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
5 |
|
|
|
6 |
|
|
#include "crypto/crypto_keys.h" |
7 |
|
|
#include "crypto/crypto_keygen.h" |
8 |
|
|
#include "crypto/crypto_util.h" |
9 |
|
|
#include "env.h" |
10 |
|
|
#include "memory_tracker.h" |
11 |
|
|
#include "v8.h" |
12 |
|
|
|
13 |
|
|
#include <variant> |
14 |
|
|
|
15 |
|
|
namespace node { |
16 |
|
|
namespace crypto { |
17 |
|
|
class DiffieHellman : public BaseObject { |
18 |
|
|
public: |
19 |
|
|
static void Initialize(Environment* env, v8::Local<v8::Object> target); |
20 |
|
|
static void RegisterExternalReferences(ExternalReferenceRegistry* registry); |
21 |
|
|
|
22 |
|
|
bool Init(int primeLength, int g); |
23 |
|
|
bool Init(const char* p, int p_len, int g); |
24 |
|
|
bool Init(const char* p, int p_len, const char* g, int g_len); |
25 |
|
|
|
26 |
|
|
static void Stateless(const v8::FunctionCallbackInfo<v8::Value>& args); |
27 |
|
|
|
28 |
|
|
protected: |
29 |
|
|
static void DiffieHellmanGroup( |
30 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args); |
31 |
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); |
32 |
|
|
static void GenerateKeys(const v8::FunctionCallbackInfo<v8::Value>& args); |
33 |
|
|
static void GetPrime(const v8::FunctionCallbackInfo<v8::Value>& args); |
34 |
|
|
static void GetGenerator(const v8::FunctionCallbackInfo<v8::Value>& args); |
35 |
|
|
static void GetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
36 |
|
|
static void GetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
37 |
|
|
static void ComputeSecret(const v8::FunctionCallbackInfo<v8::Value>& args); |
38 |
|
|
static void SetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
39 |
|
|
static void SetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
40 |
|
|
static void VerifyErrorGetter( |
41 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args); |
42 |
|
|
|
43 |
|
|
DiffieHellman(Environment* env, v8::Local<v8::Object> wrap); |
44 |
|
|
|
45 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
46 |
|
|
SET_MEMORY_INFO_NAME(DiffieHellman) |
47 |
|
|
SET_SELF_SIZE(DiffieHellman) |
48 |
|
|
|
49 |
|
|
private: |
50 |
|
|
static void GetField(const v8::FunctionCallbackInfo<v8::Value>& args, |
51 |
|
|
const BIGNUM* (*get_field)(const DH*), |
52 |
|
|
const char* err_if_null); |
53 |
|
|
static void SetKey(const v8::FunctionCallbackInfo<v8::Value>& args, |
54 |
|
|
int (*set_field)(DH*, BIGNUM*), const char* what); |
55 |
|
|
bool VerifyContext(); |
56 |
|
|
|
57 |
|
|
int verifyError_; |
58 |
|
|
DHPointer dh_; |
59 |
|
|
}; |
60 |
|
|
|
61 |
|
|
struct DhKeyPairParams final : public MemoryRetainer { |
62 |
|
|
// Diffie-Hellman can either generate keys using a fixed prime, or by first |
63 |
|
|
// generating a random prime of a given size (in bits). Only one of both |
64 |
|
|
// options may be specified. |
65 |
|
|
std::variant<BignumPointer, int> prime; |
66 |
|
|
unsigned int generator; |
67 |
|
|
SET_NO_MEMORY_INFO() |
68 |
|
|
SET_MEMORY_INFO_NAME(DhKeyPairParams) |
69 |
|
|
SET_SELF_SIZE(DhKeyPairParams) |
70 |
|
|
}; |
71 |
|
|
|
72 |
|
|
using DhKeyPairGenConfig = KeyPairGenConfig<DhKeyPairParams>; |
73 |
|
|
|
74 |
|
|
struct DhKeyGenTraits final { |
75 |
|
|
using AdditionalParameters = DhKeyPairGenConfig; |
76 |
|
|
static constexpr const char* JobName = "DhKeyPairGenJob"; |
77 |
|
|
|
78 |
|
|
static EVPKeyCtxPointer Setup(DhKeyPairGenConfig* params); |
79 |
|
|
|
80 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
81 |
|
|
CryptoJobMode mode, |
82 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
83 |
|
|
unsigned int* offset, |
84 |
|
|
DhKeyPairGenConfig* params); |
85 |
|
|
}; |
86 |
|
|
|
87 |
|
|
using DHKeyPairGenJob = KeyGenJob<KeyPairGenTraits<DhKeyGenTraits>>; |
88 |
|
|
|
89 |
|
|
struct DHKeyExportConfig final : public MemoryRetainer { |
90 |
|
|
SET_NO_MEMORY_INFO() |
91 |
|
|
SET_MEMORY_INFO_NAME(DHKeyExportConfig) |
92 |
|
|
SET_SELF_SIZE(DHKeyExportConfig) |
93 |
|
|
}; |
94 |
|
|
|
95 |
|
|
struct DHKeyExportTraits final { |
96 |
|
|
static constexpr const char* JobName = "DHKeyExportJob"; |
97 |
|
|
using AdditionalParameters = DHKeyExportConfig; |
98 |
|
|
|
99 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
100 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
101 |
|
|
unsigned int offset, |
102 |
|
|
DHKeyExportConfig* config); |
103 |
|
|
|
104 |
|
|
static WebCryptoKeyExportStatus DoExport( |
105 |
|
|
std::shared_ptr<KeyObjectData> key_data, |
106 |
|
|
WebCryptoKeyFormat format, |
107 |
|
|
const DHKeyExportConfig& params, |
108 |
|
|
ByteSource* out); |
109 |
|
|
}; |
110 |
|
|
|
111 |
|
|
using DHKeyExportJob = KeyExportJob<DHKeyExportTraits>; |
112 |
|
|
|
113 |
|
|
struct DHBitsConfig final : public MemoryRetainer { |
114 |
|
|
std::shared_ptr<KeyObjectData> private_key; |
115 |
|
|
std::shared_ptr<KeyObjectData> public_key; |
116 |
|
|
SET_NO_MEMORY_INFO() |
117 |
|
|
SET_MEMORY_INFO_NAME(DHBitsConfig) |
118 |
|
|
SET_SELF_SIZE(DHBitsConfig) |
119 |
|
|
}; |
120 |
|
|
|
121 |
|
|
struct DHBitsTraits final { |
122 |
|
|
using AdditionalParameters = DHBitsConfig; |
123 |
|
|
static constexpr const char* JobName = "DHBitsJob"; |
124 |
|
|
static constexpr AsyncWrap::ProviderType Provider = |
125 |
|
|
AsyncWrap::PROVIDER_DERIVEBITSREQUEST; |
126 |
|
|
|
127 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
128 |
|
|
CryptoJobMode mode, |
129 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
130 |
|
|
unsigned int offset, |
131 |
|
|
DHBitsConfig* params); |
132 |
|
|
|
133 |
|
|
static bool DeriveBits( |
134 |
|
|
Environment* env, |
135 |
|
|
const DHBitsConfig& params, |
136 |
|
|
ByteSource* out_); |
137 |
|
|
|
138 |
|
|
static v8::Maybe<bool> EncodeOutput( |
139 |
|
|
Environment* env, |
140 |
|
|
const DHBitsConfig& params, |
141 |
|
|
ByteSource* out, |
142 |
|
|
v8::Local<v8::Value>* result); |
143 |
|
|
}; |
144 |
|
|
|
145 |
|
|
using DHBitsJob = DeriveBitsJob<DHBitsTraits>; |
146 |
|
|
|
147 |
|
|
v8::Maybe<bool> GetDhKeyDetail( |
148 |
|
|
Environment* env, |
149 |
|
|
std::shared_ptr<KeyObjectData> key, |
150 |
|
|
v8::Local<v8::Object> target); |
151 |
|
|
|
152 |
|
|
} // namespace crypto |
153 |
|
|
} // namespace node |
154 |
|
|
|
155 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
156 |
|
|
#endif // SRC_CRYPTO_CRYPTO_DH_H_ |