1 |
|
|
#ifndef SRC_CRYPTO_CRYPTO_EC_H_ |
2 |
|
|
#define SRC_CRYPTO_CRYPTO_EC_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 "allocated_buffer.h" |
10 |
|
|
#include "async_wrap.h" |
11 |
|
|
#include "base_object.h" |
12 |
|
|
#include "env.h" |
13 |
|
|
#include "memory_tracker.h" |
14 |
|
|
#include "node_internals.h" |
15 |
|
|
#include "v8.h" |
16 |
|
|
|
17 |
|
|
namespace node { |
18 |
|
|
namespace crypto { |
19 |
|
|
int GetCurveFromName(const char* name); |
20 |
|
|
int GetOKPCurveFromName(const char* name); |
21 |
|
|
|
22 |
|
|
class ECDH final : public BaseObject { |
23 |
|
|
public: |
24 |
|
|
~ECDH() override; |
25 |
|
|
|
26 |
|
|
static void Initialize(Environment* env, v8::Local<v8::Object> target); |
27 |
|
|
static void RegisterExternalReferences(ExternalReferenceRegistry* registry); |
28 |
|
|
|
29 |
|
|
static ECPointPointer BufferToPoint(Environment* env, |
30 |
|
|
const EC_GROUP* group, |
31 |
|
|
v8::Local<v8::Value> buf); |
32 |
|
|
|
33 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
34 |
|
|
SET_MEMORY_INFO_NAME(ECDH) |
35 |
|
|
SET_SELF_SIZE(ECDH) |
36 |
|
|
|
37 |
|
|
static void ConvertKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
38 |
|
|
|
39 |
|
|
static void GetCurves(const v8::FunctionCallbackInfo<v8::Value>& args); |
40 |
|
|
|
41 |
|
|
protected: |
42 |
|
|
ECDH(Environment* env, v8::Local<v8::Object> wrap, ECKeyPointer&& key); |
43 |
|
|
|
44 |
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); |
45 |
|
|
static void GenerateKeys(const v8::FunctionCallbackInfo<v8::Value>& args); |
46 |
|
|
static void ComputeSecret(const v8::FunctionCallbackInfo<v8::Value>& args); |
47 |
|
|
static void GetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
48 |
|
|
static void SetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
49 |
|
|
static void GetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
50 |
|
|
static void SetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args); |
51 |
|
|
|
52 |
|
|
bool IsKeyPairValid(); |
53 |
|
|
bool IsKeyValidForCurve(const BignumPointer& private_key); |
54 |
|
|
|
55 |
|
|
ECKeyPointer key_; |
56 |
|
|
const EC_GROUP* group_; |
57 |
|
|
}; |
58 |
|
|
|
59 |
|
|
struct ECDHBitsConfig final : public MemoryRetainer { |
60 |
|
|
int id_; |
61 |
|
|
std::shared_ptr<KeyObjectData> private_; |
62 |
|
|
std::shared_ptr<KeyObjectData> public_; |
63 |
|
|
|
64 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
65 |
|
|
SET_MEMORY_INFO_NAME(ECDHBitsConfig) |
66 |
|
|
SET_SELF_SIZE(ECDHBitsConfig) |
67 |
|
|
}; |
68 |
|
|
|
69 |
|
|
struct ECDHBitsTraits final { |
70 |
|
|
using AdditionalParameters = ECDHBitsConfig; |
71 |
|
|
static constexpr const char* JobName = "ECDHBitsJob"; |
72 |
|
|
static constexpr AsyncWrap::ProviderType Provider = |
73 |
|
|
AsyncWrap::PROVIDER_DERIVEBITSREQUEST; |
74 |
|
|
|
75 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
76 |
|
|
CryptoJobMode mode, |
77 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
78 |
|
|
unsigned int offset, |
79 |
|
|
ECDHBitsConfig* params); |
80 |
|
|
|
81 |
|
|
static bool DeriveBits( |
82 |
|
|
Environment* env, |
83 |
|
|
const ECDHBitsConfig& params, |
84 |
|
|
ByteSource* out_); |
85 |
|
|
|
86 |
|
|
static v8::Maybe<bool> EncodeOutput( |
87 |
|
|
Environment* env, |
88 |
|
|
const ECDHBitsConfig& params, |
89 |
|
|
ByteSource* out, |
90 |
|
|
v8::Local<v8::Value>* result); |
91 |
|
|
}; |
92 |
|
|
|
93 |
|
|
using ECDHBitsJob = DeriveBitsJob<ECDHBitsTraits>; |
94 |
|
|
|
95 |
|
|
struct EcKeyPairParams final : public MemoryRetainer { |
96 |
|
|
int curve_nid; |
97 |
|
|
int param_encoding; |
98 |
|
|
SET_NO_MEMORY_INFO() |
99 |
|
|
SET_MEMORY_INFO_NAME(EcKeyPairParams) |
100 |
|
|
SET_SELF_SIZE(EcKeyPairParams) |
101 |
|
|
}; |
102 |
|
|
|
103 |
|
|
using EcKeyPairGenConfig = KeyPairGenConfig<EcKeyPairParams>; |
104 |
|
|
|
105 |
|
|
struct EcKeyGenTraits final { |
106 |
|
|
using AdditionalParameters = EcKeyPairGenConfig; |
107 |
|
|
static constexpr const char* JobName = "EcKeyPairGenJob"; |
108 |
|
|
|
109 |
|
|
static EVPKeyCtxPointer Setup(EcKeyPairGenConfig* params); |
110 |
|
|
|
111 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
112 |
|
|
CryptoJobMode mode, |
113 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
114 |
|
|
unsigned int* offset, |
115 |
|
|
EcKeyPairGenConfig* params); |
116 |
|
|
}; |
117 |
|
|
|
118 |
|
|
using ECKeyPairGenJob = KeyGenJob<KeyPairGenTraits<EcKeyGenTraits>>; |
119 |
|
|
|
120 |
|
|
// There is currently no additional information that the |
121 |
|
|
// ECKeyExport needs to collect, but we need to provide |
122 |
|
|
// the base struct anyway. |
123 |
|
|
struct ECKeyExportConfig final : public MemoryRetainer { |
124 |
|
|
SET_NO_MEMORY_INFO() |
125 |
|
|
SET_MEMORY_INFO_NAME(ECKeyExportConfig) |
126 |
|
|
SET_SELF_SIZE(ECKeyExportConfig) |
127 |
|
|
}; |
128 |
|
|
|
129 |
|
|
struct ECKeyExportTraits final { |
130 |
|
|
static constexpr const char* JobName = "ECKeyExportJob"; |
131 |
|
|
using AdditionalParameters = ECKeyExportConfig; |
132 |
|
|
|
133 |
|
|
static v8::Maybe<bool> AdditionalConfig( |
134 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
135 |
|
|
unsigned int offset, |
136 |
|
|
ECKeyExportConfig* config); |
137 |
|
|
|
138 |
|
|
static WebCryptoKeyExportStatus DoExport( |
139 |
|
|
std::shared_ptr<KeyObjectData> key_data, |
140 |
|
|
WebCryptoKeyFormat format, |
141 |
|
|
const ECKeyExportConfig& params, |
142 |
|
|
ByteSource* out); |
143 |
|
|
}; |
144 |
|
|
|
145 |
|
|
using ECKeyExportJob = KeyExportJob<ECKeyExportTraits>; |
146 |
|
|
|
147 |
|
|
v8::Maybe<bool> ExportJWKEcKey( |
148 |
|
|
Environment* env, |
149 |
|
|
std::shared_ptr<KeyObjectData> key, |
150 |
|
|
v8::Local<v8::Object> target); |
151 |
|
|
|
152 |
|
|
v8::Maybe<bool> ExportJWKEdKey( |
153 |
|
|
Environment* env, |
154 |
|
|
std::shared_ptr<KeyObjectData> key, |
155 |
|
|
v8::Local<v8::Object> target); |
156 |
|
|
|
157 |
|
|
std::shared_ptr<KeyObjectData> ImportJWKEcKey( |
158 |
|
|
Environment* env, |
159 |
|
|
v8::Local<v8::Object> jwk, |
160 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args, |
161 |
|
|
unsigned int offset); |
162 |
|
|
|
163 |
|
|
v8::Maybe<bool> GetEcKeyDetail( |
164 |
|
|
Environment* env, |
165 |
|
|
std::shared_ptr<KeyObjectData> key, |
166 |
|
|
v8::Local<v8::Object> target); |
167 |
|
|
} // namespace crypto |
168 |
|
|
} // namespace node |
169 |
|
|
|
170 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
171 |
|
|
#endif // SRC_CRYPTO_CRYPTO_EC_H_ |