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